Skip to content

Comments

Fix:修复新建对话时因缺少会话ID导致配置绑定失败的问题#5292

Merged
Soulter merged 9 commits intoAstrBotDevs:masterfrom
Li-shi-ling:fix/config-binding-empty-session
Feb 21, 2026
Merged

Fix:修复新建对话时因缺少会话ID导致配置绑定失败的问题#5292
Soulter merged 9 commits intoAstrBotDevs:masterfrom
Li-shi-ling:fix/config-binding-empty-session

Conversation

@Li-shi-ling
Copy link
Contributor

@Li-shi-ling Li-shi-ling commented Feb 21, 2026

Fixes #5267

创建新会话(GET /api/chat/new_session)后没有把“当前选择的配置文件(abconf)”绑定到该会话,导致新会话始终使用默认配置。
本 PR 在新会话创建成功后,自动调用 POST /api/config/umo_abconf_route/update 将所选配置绑定到该会话对应的 UMO 路由;同时对 StandaloneChat(测试配置用)补齐相同的绑定逻辑,并统一 UMO/Storage Key 的生成规则,避免多处实现不一致导致绑定无效。

Modifications / 改动点

  • dashboard/src/composables/useSessions.ts

    • newSession() 创建会话成功后,读取最近一次选择的配置 id(localStorage: chat.selectedConfigId),并为 webchat 平台自动执行配置绑定:
      • 构造 UMO(与 ConfigSelector.vue 一致的 platformId:messageType:sessionKey 格式)
      • 调用 POST /api/config/umo_abconf_route/update 写入 { umo, conf_id }
    • 绑定失败不会阻断会话创建(best-effort,保留默认配置作为回退)
  • dashboard/src/components/chat/StandaloneChat.vue

    • Standalone 模式不走 useSessions,因此在其 newSession() 中同样在会话创建后调用 /api/config/umo_abconf_route/update,将 props.configId 绑定到新会话
  • dashboard/src/utils/chatConfigBinding.ts

    • 抽出并复用公共逻辑:Storage Key 常量、读取当前所选配置 id、以及 webchat UMO 构造(避免多处手写字符串导致不一致)
  • dashboard/src/components/chat/ConfigSelector.vue

    • 复用公共 Storage Key 常量(功能无变化,减少硬编码)
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

以下是直接在新对话切换了配置文件会进行对话的效果

4c3433a02fffba92014dc85552192cac 36038a764f114186576f71c4f994c8d7

Verification Steps / 验证步骤

  1. 启动AstrBot
  2. 进入 Chat 页面,使用配置选择器选择一个非 default 的配置文件。
  3. 创建新会话(点击“新会话/新对话”,或在无会话状态下直接发送消息触发创建)。
  4. 在浏览器 DevTools → Network 中确认创建会话后出现一次:
    • POST /api/config/umo_abconf_route/update
    • 请求体中的 conf_id 为所选配置 id

Local Checks / 本地检查

  • pnpm run typecheck(通过)
  • ruff format .(通过)

Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    没有新功能
  • 👀 我的更改经过了良好的测试,并已在上方提供了验证步骤和运行截图。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    有进行编译后的测试
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    没有引入依赖库
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.
    没有引入恶意代码

由 Sourcery 提供的摘要

确保新创建的聊天会话自动绑定到当前选中的配置,并集中管理聊天配置绑定工具。

Bug 修复:

  • 将当前选中的聊天配置绑定到新的网页聊天会话上,避免由于缺少基于会话的路由而回退到默认配置。
  • 将提供的配置 ID 绑定到新的 StandaloneChat 会话,使其行为与常规聊天会话保持一致。

增强功能:

  • 引入共享的聊天配置绑定工具模块,在各组件之间统一存储键、选中配置的获取方式,以及网页聊天 UMO 的构建逻辑。
Original summary in English

Summary by Sourcery

Ensure newly created chat sessions are automatically bound to the currently selected configuration and centralize chat configuration binding utilities.

Bug Fixes:

  • Bind the currently selected chat configuration to new webchat sessions so they no longer fall back to the default config due to missing session-based routing.
  • Bind the provided config ID to new StandaloneChat sessions to align their behavior with regular chat sessions.

Enhancements:

  • Introduce a shared chat configuration binding utility module to standardize storage keys, selected-config retrieval, and webchat UMO construction across components.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Feb 21, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Li-shi-ling, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求解决了新建对话时,已选择的配置未能正确绑定到新会话的问题,导致新会话始终使用默认配置。通过在会话创建后自动执行配置绑定,并统一相关逻辑,确保用户选择的配置能在新会话中生效,提升了用户体验和系统一致性。

Highlights

  • 配置绑定修复: 修复了新建对话时,已选择的配置未能正确绑定到新会话的问题,确保新会话能使用用户选择的配置而非默认配置。
  • StandaloneChat 模式支持: 为独立测试模式下的 StandaloneChat 补齐了相同的配置绑定逻辑,使其也能正确绑定所选配置。
  • 公共逻辑抽取: 新增了一个工具文件 chatConfigBinding.ts,用于统一管理配置相关的 Storage Key 和 UMO 路由的生成规则,避免多处实现不一致。
  • 代码复用与一致性: ConfigSelector.vue 现在复用 chatConfigBinding.ts 中定义的 Storage Key 常量,减少了硬编码并提高了代码一致性。
Changelog
  • dashboard/src/components/chat/ConfigSelector.vue
    • 更新了 STORAGE_KEY 常量,现在从 chatConfigBinding.ts 导入。
  • dashboard/src/components/chat/StandaloneChat.vue
    • 引入 buildWebchatUmoDetails 函数。
    • newSession 函数中添加了配置绑定逻辑,调用 /api/config/umo_abconf_route/update
  • dashboard/src/composables/useSessions.ts
    • 引入 buildWebchatUmoDetailsgetStoredSelectedChatConfigId 函数。
    • newSession 函数中添加了配置绑定逻辑,根据存储的配置ID和平台ID调用 /api/config/umo_abconf_route/update
  • dashboard/src/utils/chatConfigBinding.ts
    • 新增文件,定义了 CHAT_SELECTED_CONFIG_STORAGE_KEY 常量。
    • 定义了 ChatMessageType 类型和 WebchatUmoDetails 接口。
    • 导出了 getStoredDashboardUsernamegetStoredSelectedChatConfigIdbuildWebchatUmoDetails 函数,用于获取用户名、配置ID和构建 UMO 详情。
Activity
  • 没有检测到此拉取请求的任何活动。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了 1 个问题,并给出了一些高层次的反馈:

  • 标记默认配置的 'default' 配置 ID 在多个地方被硬编码(bindConfigToSessionuseSessions 中的 newSessiongetStoredSelectedChatConfigId);建议把它提取成一个共享常量,这样如果默认 ID 以后发生变化就不会出现不一致。
  • getStoredDashboardUsernamegetStoredSelectedChatConfigId 假定 localStorage 总是可用,并且键名('user''chat.selectedConfigId')是稳定不变的;如果这些逻辑有可能在非浏览器环境中运行,或者存储结构将来发生变化,建议在访问前增加防护和/或像 CHAT_SELECTED_CONFIG_STORAGE_KEY 那样集中管理存储键名。
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The sentinel `'default'` config ID is hard-coded in multiple places (`bindConfigToSession`, `newSession` in `useSessions`, `getStoredSelectedChatConfigId`); consider extracting it to a shared constant to avoid divergence if the default ID ever changes.
- `getStoredDashboardUsername` and `getStoredSelectedChatConfigId` assume `localStorage` is always available and that the keys (`'user'`, `'chat.selectedConfigId'`) are stable; if there’s any chance of these running in non-browser contexts or the storage schema changing, consider guarding access and/or centralizing the storage key definitions similarly to `CHAT_SELECTED_CONFIG_STORAGE_KEY`.

## Individual Comments

### Comment 1
<location> `dashboard/src/utils/chatConfigBinding.ts:13-14` </location>
<code_context>
+  umo: string;
+}
+
+export function getStoredDashboardUsername(): string {
+  return (localStorage.getItem('user') || '').trim() || 'guest';
+}
+
</code_context>

<issue_to_address>
**issue:** Direct `localStorage` access without guarding can throw in some environments (e.g., restricted cookies / storage).

This can cause `buildWebchatUmoDetails` (and any other callers) to throw in environments like Safari private mode or with strict privacy settings. Consider wrapping `localStorage.getItem` in a try/catch and defaulting to `'guest'` on error. Applying the same pattern (or a shared `getFromLocalStorage(key, fallback)` helper) would make other call sites like `getStoredSelectedChatConfigId` safer too.
</issue_to_address>

Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享给更多人 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进以后的评审。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • The sentinel 'default' config ID is hard-coded in multiple places (bindConfigToSession, newSession in useSessions, getStoredSelectedChatConfigId); consider extracting it to a shared constant to avoid divergence if the default ID ever changes.
  • getStoredDashboardUsername and getStoredSelectedChatConfigId assume localStorage is always available and that the keys ('user', 'chat.selectedConfigId') are stable; if there’s any chance of these running in non-browser contexts or the storage schema changing, consider guarding access and/or centralizing the storage key definitions similarly to CHAT_SELECTED_CONFIG_STORAGE_KEY.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The sentinel `'default'` config ID is hard-coded in multiple places (`bindConfigToSession`, `newSession` in `useSessions`, `getStoredSelectedChatConfigId`); consider extracting it to a shared constant to avoid divergence if the default ID ever changes.
- `getStoredDashboardUsername` and `getStoredSelectedChatConfigId` assume `localStorage` is always available and that the keys (`'user'`, `'chat.selectedConfigId'`) are stable; if there’s any chance of these running in non-browser contexts or the storage schema changing, consider guarding access and/or centralizing the storage key definitions similarly to `CHAT_SELECTED_CONFIG_STORAGE_KEY`.

## Individual Comments

### Comment 1
<location> `dashboard/src/utils/chatConfigBinding.ts:13-14` </location>
<code_context>
+  umo: string;
+}
+
+export function getStoredDashboardUsername(): string {
+  return (localStorage.getItem('user') || '').trim() || 'guest';
+}
+
</code_context>

<issue_to_address>
**issue:** Direct `localStorage` access without guarding can throw in some environments (e.g., restricted cookies / storage).

This can cause `buildWebchatUmoDetails` (and any other callers) to throw in environments like Safari private mode or with strict privacy settings. Consider wrapping `localStorage.getItem` in a try/catch and defaulting to `'guest'` on error. Applying the same pattern (or a shared `getFromLocalStorage(key, fallback)` helper) would make other call sites like `getStoredSelectedChatConfigId` safer too.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot dosubot bot added area:webui The bug / feature is about webui(dashboard) of astrbot. feature:chatui The bug / feature is about astrbot's chatui, webchat labels Feb 21, 2026
@dosubot
Copy link

dosubot bot commented Feb 21, 2026

Related Documentation

Checked 1 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 旨在修复新建会话时未绑定配置文件的问题,通过在创建新会话后自动调用 API 绑定配置,解决了新会话始终使用默认配置的 bug。

主要改动包括:

  • useSessionsStandaloneChat 中,于 newSession 成功后增加绑定配置的逻辑。
  • 新增 chatConfigBinding.ts 工具文件,统一了 UMO 和 LocalStorage Key 的生成规则,提高了代码的内聚性和可维护性。
  • ConfigSelector.vue 中使用新的常量,减少了硬编码。

代码整体结构清晰,改动符合预期。我发现一处可以进一步改进的地方,以完全实现“统一 UMO 生成规则”的目标,具体请见我的评论。

import axios from 'axios';
import { useToast } from '@/utils/toast';
import { useModuleI18n } from '@/i18n/composables';
import { CHAT_SELECTED_CONFIG_STORAGE_KEY } from '@/utils/chatConfigBinding';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

感谢您抽出公共逻辑!这里有一个小建议,为了完全统一 UMO 的生成规则,ConfigSelector.vueusername 的获取方式也应该使用新的工具函数。

当前 ConfigSelector.vue 中(132行)获取用户名的逻辑是:

const username = computed(() => localStorage.getItem('user') || 'guest');

这与新的工具函数 getStoredDashboardUsername(包含了 .trim() 来移除前后空格)不完全一致。如果用户名在 localStorage 中包含前后空格,这可能导致 ConfigSelector.vue 和其他部分生成的 UMO 不匹配,与本次 PR 的目标相悖。

建议进行如下修改以保持一致:

  1. 在此处额外导入 getStoredDashboardUsername
  2. username 的定义(132行)更新为使用 getStoredDashboardUsername

由于相关代码行不在本次变更范围内,您可以在此文件的任意变更处进行修改,或者将其作为一次新的提交。

- 给 localStorage 访问加了 try/catch + 可用性判断:dashboard/src/utils/chatConfigBinding.ts:13
- 新增 getFromLocalStorage/setToLocalStorage(在受限存储/无痕模式下异常时回退/忽略)
- getStoredDashboardUsername() / getStoredSelectedChatConfigId() 改为走安全读取:dashboard/src/utils/chatConfigBinding.ts:36       - 新增 setStoredSelectedChatConfigId(),写入失败静默忽略:dashboard/src/utils/chatConfigBinding.ts:44
- 把 ConfigSelector.vue 里直接 localStorage.getItem/setItem 全部替换为上述安全方法:dashboard/src/components/chat/ConfigSelector.vue:81
- 已重新跑过 pnpm run typecheck,通过。
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Feb 21, 2026
@Li-shi-ling
Copy link
Contributor Author

修改后也进行了测试
如下图,在进行新建会话的后面紧跟着发送了绑定配置文件的请求
image

@Li-shi-ling
Copy link
Contributor Author

我去!才发现删除错文件了!

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 21, 2026
@Soulter Soulter merged commit d02ee7b into AstrBotDevs:master Feb 21, 2026
6 checks passed
@Li-shi-ling Li-shi-ling deleted the fix/config-binding-empty-session branch February 21, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:webui The bug / feature is about webui(dashboard) of astrbot. feature:chatui The bug / feature is about astrbot's chatui, webchat lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] ChatUI的“项目”中修改配置文件结果异常

2 participants