-
Notifications
You must be signed in to change notification settings - Fork 403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support maxInputTokens for agent #4397
Conversation
Walkthrough本次改动为聊天历史检索逻辑引入了 token 限制。具体来说,在 ChatProxyService、ChatService 和 MsgHistoryManager 中更新了方法签名,新增了用于限制输入 token 数量的参数与常量。同时在 AIChatView 中添加了条件渲染,用于展示被截断的消息数量。此外,还新增了用于展示聊天提示的 CSS 样式和对应的本地化字符串,以便向用户提示因 token 限制而被丢弃的消息信息。 Changes
Sequence Diagram(s)sequenceDiagram
participant UI as AIChatView
participant Proxy as ChatProxyService
participant API as ChatService
participant Msg as MsgHistoryManager
UI->>Proxy: 触发获取聊天历史请求
Proxy->>API: 调用 getHistoryMessages(MAX_INPUT_TOKENS)
API->>Msg: 调用 getMessages(maxTokens)
Msg-->>API: 返回符合 token 限制的消息列表
API-->>Proxy: 返回消息列表
Proxy-->>UI: 返回消息列表
alt 存在消息截断
UI->>UI: 显示消息截断计数信息
end
Possibly related PRs
Suggested reviewers
Warning There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure. 🔧 ESLint
yarn install v1.22.22 ✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (4)
packages/ai-native/src/browser/model/msg-history-manager.ts (3)
46-48
: 建议减少重复排序以提升性能。当前
messageList
在每次访问时会对messageMap
中的所有消息进行完整排序,当消息数量较大时,可能会造成额外的性能开销。可考虑在插入或更新消息时就保持有序结构,或维护一个增量更新的排序结果,避免反复执行完整排序操作。
57-59
: 需要明确命名以表达业务含义。
slicedMessageCount
仅返回startIndex
,但startIndex
与实际被丢弃或被截断的消息数量并不一定一一对应。为增强可读性和精确性,可考虑使用更具描述性的名称,或者明确注释其含义和使用场景。
61-69
: 考虑采用更高效的截断逻辑以避免重复计算。
while (this.totalTokens > maxTokens)
循环中,每一次递增startIndex
都需要再次计算 totalTokens,并重复切片操作,可能造成性能瓶颈。可在累加阶段一次性确定需要截断的区间,或采用二分搜索等方法实现更高效的截断过程。packages/ai-native/src/browser/chat/chat.view.tsx (1)
692-698
: 代码实现正确且符合最佳实践!实现了一个优雅的消息限制提示 UI,使用了条件渲染和本地化字符串。建议考虑添加一个 tooltip 来提供更多关于 token 限制的详细信息。
建议添加 tooltip 提示:
- <div className={styles.chat_tips_text}> + <div className={styles.chat_tips_text} title={localize('aiNative.chat.ai.assistant.limit.message.tooltip')}>
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
packages/ai-native/src/browser/chat/chat-proxy.service.ts
(1 hunks)packages/ai-native/src/browser/chat/chat.api.service.ts
(1 hunks)packages/ai-native/src/browser/chat/chat.module.less
(1 hunks)packages/ai-native/src/browser/chat/chat.view.tsx
(1 hunks)packages/ai-native/src/browser/model/msg-history-manager.ts
(1 hunks)packages/i18n/src/common/en-US.lang.ts
(1 hunks)packages/i18n/src/common/zh-CN.lang.ts
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (7)
- GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
- GitHub Check: build (ubuntu-latest, 20.x)
- GitHub Check: unittest (ubuntu-latest, 18.x, node)
- GitHub Check: unittest (macos-latest, 18.x, jsdom)
- GitHub Check: build-windows
- GitHub Check: unittest (macos-latest, 18.x, node)
- GitHub Check: ubuntu-latest, Node.js 20.x
🔇 Additional comments (5)
packages/ai-native/src/browser/model/msg-history-manager.ts (1)
52-56
: 请确认基于字符串长度的 token 估算是否符合需求。此处将
content.length
之和再除以 3 来近似计算 token 数。若需要更精确的计算方式(例如支持多字节字符或使用实际 tokenizer 规则),建议改用正式的 tokenizer 库,否则可能导致不必要的截断或溢出。packages/ai-native/src/browser/chat/chat.api.service.ts (1)
68-69
: 确认默认的最大输入 token 值是否合理。此处默认值设置为
128000
,需要评估在实际环境下是否过大或过小,并考虑提供用户配置项或动态调整机制,以便在不同场景下获得最佳性能和准确度。packages/ai-native/src/browser/chat/chat-proxy.service.ts (1)
128-134
: 确保不同模块的最大输入 token 常量保持一致或可配置。此处
MAX_INPUT_TOKENS
被设为30720
,而在其他地方可能使用了128000
作为默认值。如果业务需要一个全局统一或可调整的限制,请在同一位置集中管理,避免混淆或遗漏更新。packages/i18n/src/common/zh-CN.lang.ts (1)
1257-1257
: 本地化字符串实现正确!消息文本清晰地表达了因 token 限制而丢弃早期消息的含义,符合中文语言习惯。
packages/ai-native/src/browser/chat/chat.module.less (1)
321-328
: 样式实现符合最佳实践新增的
.chat_tips_container
类实现合理:
- 使用 flexbox 实现居中布局
- 使用 CSS 变量保持主题一致性
- 类名遵循项目命名规范
Walkthrough此次变更主要更新了聊天历史消息的获取逻辑和相关的显示提示。修改在 Changes
Sequence Diagram(s)sequenceDiagram
participant CP as ChatProxyService
participant CS as ChatService
participant MH as MsgHistoryManager
participant UI as 客户端/UI
CP->>CS: 调用 getHistoryMessages(MAX_INPUT_TOKENS)
CS->>MH: 调用 getMessages(maxTokens)
MH-->>CS: 返回切片后的消息列表及 slicedMessageCount
CS-->>CP: 返回历史消息数据
CP-->>UI: 响应历史消息请求
sequenceDiagram
participant View as AIChatView
participant MHM as MsgHistoryManager
View->>MHM: 获取 slicedMessageCount
alt slicedMessageCount > 0
MHM-->>View: 返回消息计数
View->>View: 渲染消息计数和提示文本
else
MHM-->>View: 返回 0
View->>View: 不显示消息计数
end
Possibly related PRs
Suggested reviewers
Warning There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure. 🔧 ESLint
yarn install v1.22.22 ✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (4)
packages/ai-native/src/browser/model/msg-history-manager.ts (3)
50-51
:startIndex
的默认值应考虑越界检查
初始化startIndex = 0
没问题,但需要确保后续递增时不会超过messageList
的长度,进而导致slice
返回空数组或出现潜在边界错误。建议在递增前或后添加断言或限制,避免越界。
52-55
: 令牌总数的计算逻辑过于简单,可能导致不准确的截断
当前除以 3 的做法过于依赖消息的平均字节数,可能在语义或模型层面的实际 token 计算上存在偏差。如有需要更精确统计,可考虑调用第三方库或官方 token 计算函数;若此算法仅作近似衡量,则需在注释中标明其局限性。
57-59
:slicedMessageCount
提供了有用的可视化信息,但需注意对外暴露范围
该属性便于在外部了解截断数量。然而如果项目需要保留更多可追溯性或日志信息,考虑通过事件机制或其他方式,将更多上下文信息暴露给调用方。packages/ai-native/src/browser/chat/chat-proxy.service.ts (1)
134-134
: 调用getHistoryMessages(MAX_INPUT_TOKENS)
时可考虑确保历史记录精度
在这里强行使用MAX_INPUT_TOKENS
,会导致历史截断基于固定值。若希望更灵活,可从用户设置或其他配置处获取自定义值,以便灵活调整。
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
packages/ai-native/src/browser/chat/chat-proxy.service.ts
(1 hunks)packages/ai-native/src/browser/chat/chat.api.service.ts
(1 hunks)packages/ai-native/src/browser/chat/chat.module.less
(1 hunks)packages/ai-native/src/browser/chat/chat.view.tsx
(1 hunks)packages/ai-native/src/browser/model/msg-history-manager.ts
(1 hunks)packages/i18n/src/common/en-US.lang.ts
(1 hunks)packages/i18n/src/common/zh-CN.lang.ts
(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- packages/ai-native/src/browser/chat/chat.module.less
⏰ Context from checks skipped due to timeout of 90000ms (7)
- GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
- GitHub Check: build (ubuntu-latest, 20.x)
- GitHub Check: unittest (ubuntu-latest, 18.x, node)
- GitHub Check: unittest (macos-latest, 18.x, jsdom)
- GitHub Check: build-windows
- GitHub Check: unittest (macos-latest, 18.x, node)
- GitHub Check: ubuntu-latest, Node.js 20.x
🔇 Additional comments (6)
packages/ai-native/src/browser/model/msg-history-manager.ts (1)
46-48
: 在此使用私有属性简化获取逻辑是可取的,但请注意方法命名和可维护性
将messageList
抽离为私有 getter 简化了对内部数组的获取逻辑,也保证了对外只暴露必要接口。在后期扩充或重构时,可以避免直接暴露底层数据结构导致的可维护性问题。总体实现没有明显风险,可继续保持。packages/ai-native/src/browser/chat/chat.api.service.ts (1)
68-69
: 为getHistoryMessages
增加maxInputTokens
参数可增强可控性
新增maxInputTokens
默认值为 128000,提高了可配置性;调用时可在不同场景根据需求动态传参。请在调用方中确保传入值合理,并在相关文档或注释中注明默认值的来由。packages/ai-native/src/browser/chat/chat-proxy.service.ts (1)
128-128
: 使用常量MAX_INPUT_TOKENS
把默认值集中管理,做法合理
定义单独常量用来控制最大输入令牌数,既能避免散落的 magic number,也更便于后期通过配置或环境变量进行扩充或调整。packages/ai-native/src/browser/chat/chat.view.tsx (1)
692-698
: 代码实现正确且符合最佳实践!该实现通过条件渲染优雅地显示了因 token 限制而被丢弃的消息数量。使用了正确的本地化字符串,并遵循了 React 的最佳实践。
packages/i18n/src/common/zh-CN.lang.ts (1)
1257-1257
: 本地化字符串实现恰当!新增的本地化字符串表述清晰,符合中文语言习惯,并且正确放置在了 AI Native 相关的区域内。
packages/i18n/src/common/en-US.lang.ts (1)
1489-1489
:✅ Verification successful
验证中文语言文件中的对应条目
请确认在
zh-CN.lang.ts
文件中是否已添加相应的翻译条目。
🏁 Script executed:
#!/bin/bash # 在中文语言文件中查找对应的翻译条目 rg "'aiNative.chat.ai.assistant.limit.message':" packages/i18n/src/common/zh-CN.lang.tsLength of output: 170
翻译条目验证通过
- 在
packages/i18n/src/common/zh-CN.lang.ts
文件中已找到对应的翻译条目:'aiNative.chat.ai.assistant.limit.message': '条最早的消息因输入 Tokens 限制而被丢弃',
- 请确认该翻译与预期含义一致,无需进一步修改。
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4397 +/- ##
==========================================
- Coverage 53.56% 53.55% -0.01%
==========================================
Files 1651 1651
Lines 101866 101879 +13
Branches 22035 22038 +3
==========================================
Hits 54566 54566
- Misses 39351 39361 +10
- Partials 7949 7952 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/ai-native/src/browser/chat/chat.view.tsx (1)
693-699
: 新增消息限制提示的条件渲染良好的用户体验设计,当有消息因为 token 限制被截断时,通过条件渲染向用户展示提示信息。建议考虑以下优化:
- 添加视觉反馈(如图标)以提高可见性
- 考虑添加交互功能,让用户可以查看被截断的消息
{msgHistoryManager.slicedMessageCount ? ( <div className={styles.chat_tips_text}> <div className={styles.chat_tips_container}> + <span className={styles.chat_tips_icon}>ℹ️</span> {formatLocalize('aiNative.chat.ai.assistant.limit.message', msgHistoryManager.slicedMessageCount)} + <button + className={styles.chat_tips_action} + onClick={() => /* 显示被截断的消息 */} + > + {formatLocalize('aiNative.chat.ai.assistant.limit.view')} + </button> </div> </div> ) : null}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/ai-native/src/browser/chat/chat.view.tsx
(2 hunks)packages/i18n/src/common/en-US.lang.ts
(1 hunks)packages/i18n/src/common/zh-CN.lang.ts
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
- packages/i18n/src/common/en-US.lang.ts
- packages/i18n/src/common/zh-CN.lang.ts
⏰ Context from checks skipped due to timeout of 90000ms (8)
- GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
- GitHub Check: unittest (ubuntu-latest, 18.x, node)
- GitHub Check: unittest (macos-latest, 18.x, jsdom)
- GitHub Check: unittest (macos-latest, 18.x, node)
- GitHub Check: ubuntu-latest, Node.js 20.x
- GitHub Check: build (ubuntu-latest, 20.x)
- GitHub Check: build-windows
- GitHub Check: build (macos-latest, 20.x)
🔇 Additional comments (1)
packages/ai-native/src/browser/chat/chat.view.tsx (1)
28-28
: 导入formatLocalize
函数用于本地化支持导入
formatLocalize
函数以支持显示本地化的消息限制提示。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/ai-native/src/browser/model/msg-history-manager.ts (2)
53-56
: 建议改进 token 计算方法当前的 token 计算方法(内容长度除以 3)过于简化,可能不够准确。建议:
- 考虑使用专门的 tokenizer 库进行更准确的计算
- 考虑不同语言和字符类型对 token 数量的影响
- 将计算逻辑抽象为可配置的策略,便于后续调整
- return list.reduce((acc, msg) => acc + (msg.content.length || 0), 0) / 3; + // 使用 tokenizer 库计算 + return list.reduce((acc, msg) => acc + this.tokenizer.count(msg.content || ''), 0);
58-60
: 建议添加 JSDoc 文档说明为了提高代码可维护性,建议添加文档说明该 getter 的用途和返回值含义。
+ /** + * 获取因 token 限制而被截断的消息数量 + * @returns 被截断的消息数量 + */ public get slicedMessageCount(): number { return this.startIndex; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/ai-native/src/browser/chat/chat.api.service.ts
(1 hunks)packages/ai-native/src/browser/model/msg-history-manager.ts
(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/ai-native/src/browser/chat/chat.api.service.ts
⏰ Context from checks skipped due to timeout of 90000ms (8)
- GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
- GitHub Check: unittest (ubuntu-latest, 18.x, node)
- GitHub Check: unittest (macos-latest, 18.x, jsdom)
- GitHub Check: unittest (macos-latest, 18.x, node)
- GitHub Check: build (ubuntu-latest, 20.x)
- GitHub Check: build-windows
- GitHub Check: build (macos-latest, 20.x)
- GitHub Check: ubuntu-latest, Node.js 20.x
🔇 Additional comments (4)
packages/ai-native/src/browser/model/msg-history-manager.ts (4)
27-27
: 清理消息时重置startIndex
的实现很好!在清理消息时重置
startIndex
可以确保状态完全重置,避免后续使用时出现异常。
47-49
:messageList
getter 的封装很合理!将消息列表的获取和排序逻辑封装在 getter 中,提高了代码的可维护性和复用性。
51-51
:startIndex
的初始化设置合适!使用私有变量追踪消息截断位置,初始值设为 0 符合逻辑。
62-69
: 优化消息截断的性能当前实现使用 while 循环逐步增加 startIndex,在消息量大的情况下可能影响性能。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (5)
packages/ai-native/src/node/base-language-model.ts (1)
48-48
: 代码变更看起来不错!代码实现了禁用工具的功能,逻辑清晰简单。建议添加注释说明
noTool
选项的作用,以便其他开发者理解这个功能。建议添加如下注释:
+ // 当 noTool 为 true 时禁用所有工具 const allFunctions = options.noTool ? [] : registry.getAllFunctions();
packages/core-common/src/types/ai-native/index.ts (1)
185-185
: 建议添加属性文档注释为了提高代码的可维护性和可读性,建议为
noTool
属性添加 JSDoc 文档注释,说明其用途和影响。建议添加如下文档注释:
+ /** + * 是否禁用工具功能 + * 当设置为 true 时,将不会使用任何工具 + */ noTool?: boolean;packages/ai-native/src/browser/model/msg-history-manager.ts (3)
58-58
: 建议添加属性文档注释。为
startIndex
添加 JSDoc 注释,说明其用途和对消息检索的影响。+/** 消息检索的起始索引,用于实现 token 限制功能 */ private startIndex = 0;
60-63
: 建议改进 token 计算方法。当前实现使用字符串长度除以 3 来估算 token 数量,这种方法可能不够准确:
- 不同的语言和字符集可能需要不同的计算方式
- 特殊字符和表情符号可能需要特殊处理
建议:
- 添加注释说明当前计算方法的局限性
- 考虑使用更准确的 token 计算库
private get totalTokens(): number { const list = this.messageList.slice(this.startIndex); + // TODO: 当前使用字符长度除以 3 来估算 token 数量,这是一个简化的计算方法 + // 考虑使用更准确的 token 计算库,如 GPT Tokenizer return list.reduce((acc, msg) => acc + (msg.content.length || 0), 0) / 3; }
65-67
: 建议添加 getter 文档注释。为
slicedMessageCount
添加 JSDoc 注释,说明其返回值的含义。+/** + * 获取因 token 限制而被截断的消息数量 + * @returns 被截断的消息数量 + */ public get slicedMessageCount(): number { return this.startIndex; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (8)
packages/ai-native/src/browser/chat/chat-proxy.service.ts
(1 hunks)packages/ai-native/src/browser/chat/chat.module.less
(1 hunks)packages/ai-native/src/browser/chat/chat.view.tsx
(2 hunks)packages/ai-native/src/browser/model/msg-history-manager.ts
(2 hunks)packages/ai-native/src/node/base-language-model.ts
(1 hunks)packages/core-common/src/types/ai-native/index.ts
(1 hunks)packages/i18n/src/common/en-US.lang.ts
(1 hunks)packages/i18n/src/common/zh-CN.lang.ts
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
- packages/i18n/src/common/zh-CN.lang.ts
- packages/ai-native/src/browser/chat/chat.view.tsx
- packages/ai-native/src/browser/chat/chat-proxy.service.ts
- packages/ai-native/src/browser/chat/chat.module.less
- packages/i18n/src/common/en-US.lang.ts
🔇 Additional comments (3)
packages/ai-native/src/browser/model/msg-history-manager.ts (3)
34-34
: 很好!已修复startIndex
的重置问题。在清理消息时重置
startIndex
可以确保后续操作的正确性。
54-56
: 良好的封装设计!将消息排序逻辑封装在 getter 中提高了代码的可维护性和复用性。
69-76
: 优化 token 限制的实现方式。当前实现使用 while 循环逐步递增
startIndex
,这在消息数量较多时可能影响性能。建议采用二分查找方式优化性能:
public getMessages(maxTokens?: number): IHistoryChatMessage[] { if (maxTokens && this.totalTokens > maxTokens) { - while (this.totalTokens > maxTokens) { - this.startIndex++; - } + let left = this.startIndex; + let right = this.messageList.length; + while (left < right) { + const mid = Math.floor((left + right) / 2); + this.startIndex = mid; + if (this.totalTokens > maxTokens) { + left = mid + 1; + } else { + right = mid; + } + } } return this.messageList.slice(this.startIndex); }
Types
Background or solution
feat: support maxInputTokens for agent

Changelog
feat: support maxInputTokens for agent
Summary by CodeRabbit
New Features
Style