Skip to content
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

Merged
merged 7 commits into from
Feb 24, 2025
Merged

Conversation

ensorrow
Copy link
Contributor

@ensorrow ensorrow commented Feb 21, 2025

Types

  • 🎉 New Features

Background or solution

feat: support maxInputTokens for agent
image

Changelog

feat: support maxInputTokens for agent

Summary by CodeRabbit

  • New Features

    • 优化了聊天历史消息管理,系统根据输入令牌限制智能裁剪历史记录,并在界面中实时显示消息计数提示,帮助用户了解消息截断情况。
    • 新增本地化提示,清晰告知用户当输入超限时部分早期消息将被删除。
  • Style

    • 新增聊天提示容器的视觉样式,改善提示信息的展示效果。

@opensumi opensumi bot added the 🎨 feature feature required label Feb 21, 2025
Copy link
Contributor

coderabbitai bot commented Feb 21, 2025

Walkthrough

本次改动为聊天历史检索逻辑引入了 token 限制。具体来说,在 ChatProxyService、ChatService 和 MsgHistoryManager 中更新了方法签名,新增了用于限制输入 token 数量的参数与常量。同时在 AIChatView 中添加了条件渲染,用于展示被截断的消息数量。此外,还新增了用于展示聊天提示的 CSS 样式和对应的本地化字符串,以便向用户提示因 token 限制而被丢弃的消息信息。

Changes

文件 修改描述
packages/ai-native/src/browser/chat/chat-proxy.service.ts, packages/ai-native/src/browser/chat/chat.api.service.ts, packages/ai-native/src/browser/model/msg-history-manager.ts 更新聊天历史检索逻辑:在 ChatProxyService 中新增常量 MAX_INPUT_TOKENS,在 ChatService 中修改 getHistoryMessages 方法签名,新增参数 maxInputTokens,同时在 MsgHistoryManager 中调整 getMessages 方法以支持 token 限制,并新增 slicedMessageCount 用于反馈消息截断数。
packages/ai-native/src/browser/chat/chat.view.tsx 在 AIChatView 组件中添加条件渲染逻辑,当消息截断数量大于零时显示计数信息。
packages/ai-native/src/browser/chat/chat.module.less 添加新 CSS 类 .chat_tips_container,用于设置聊天提示容器的布局、对齐方式、字体大小及颜色等样式。
packages/i18n/src/common/en-US.lang.ts, packages/i18n/src/common/zh-CN.lang.ts 在本地化资源中新增键 'aiNative.chat.ai.assistant.limit.message',用于提示因输入 token 限制而丢弃最早消息的说明。

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
Loading

Possibly related PRs

Suggested reviewers

  • Aaaaash
  • Ricbet

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

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

yarn install v1.22.22
[1/4] Resolving packages...
warning [email protected]: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning eslint > @humanwhocodes/[email protected]: Use @eslint/config-array instead
warning eslint > @humanwhocodes/config-array > @humanwhocodes/[email protected]: Use @eslint/object-schema instead
warning eslint > file-entry-cache > flat-cache > [email protected]: Rimraf versions prior to v4 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > [email protected]: Glob versions prior to v9 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob > [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning eslint-import-resolver-typescript > [email protected]: Glob versions prior to v9 are no longer supported
error Couldn't find any versions for "@opensumi/ide-dev-tool" that matches "workspace:*"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between 7094c8c and 5561e9b.

📒 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 变量保持主题一致性
  • 类名遵循项目命名规范

Copy link
Contributor

coderabbitai bot commented Feb 21, 2025

Walkthrough

此次变更主要更新了聊天历史消息的获取逻辑和相关的显示提示。修改在 chat-proxy.service.ts 中新增了 MAX_INPUT_TOKENS 常量,并在 chat.api.service.ts 中调整了 getHistoryMessages 方法以接受 maxInputTokens 参数。同时,在 msg-history-manager.ts 中重构了消息获取逻辑,引入 token 限制计算和新的 slicedMessageCount 属性。在 UI 组件 chat.view.tsx 中添加了条件渲染代码,显示截取后消息计数。此外,还在国际化文件中新增了对应的提示文本。

Changes

文件 更改摘要
packages/ai-native/src/browser/chat/{chat-proxy.service.ts, chat.api.service.ts} chat-proxy 中新增 MAX_INPUT_TOKENS 常量;在 chat.api.service.ts 中更新 getHistoryMessages 方法签名,添加 maxInputTokens 参数(默认128000)。
packages/ai-native/src/browser/chat/chat.module.less 新增 .chat_tips_container 样式类,用于设置聊天提示容器的布局和样式。
packages/ai-native/src/browser/chat/chat.view.tsx AIChatView 组件中增加条件渲染代码,当 msgHistoryManager.slicedMessageCount 大于零时显示消息计数及提示文本。
packages/ai-native/src/browser/model/msg-history-manager.ts 重构 getMessages 方法:引入消息排序和切片逻辑,新增 startIndex 以控制检索起始位置,并添加 slicedMessageCount 属性及 token 总计计算。
packages/i18n/src/common/{en-US.lang.ts, zh-CN.lang.ts} localizationBundle 中新增本地化条目 'aiNative.chat.ai.assistant.limit.message',用于提示因 token 限制导致早期消息被丢弃。

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: 响应历史消息请求
Loading
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
Loading

Possibly related PRs

Suggested reviewers

  • bytemain
  • Ricbet

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

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

yarn install v1.22.22
[1/4] Resolving packages...
warning [email protected]: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning eslint > @humanwhocodes/[email protected]: Use @eslint/config-array instead
warning eslint > @humanwhocodes/config-array > @humanwhocodes/[email protected]: Use @eslint/object-schema instead
warning eslint > file-entry-cache > flat-cache > [email protected]: Rimraf versions prior to v4 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > [email protected]: Glob versions prior to v9 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob > [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning eslint-import-resolver-typescript > [email protected]: Glob versions prior to v9 are no longer supported
error Couldn't find any versions for "@opensumi/ide-dev-tool" that matches "workspace:*"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between 7094c8c and 5561e9b.

📒 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.ts

Length of output: 170


翻译条目验证通过

  • packages/i18n/src/common/zh-CN.lang.ts 文件中已找到对应的翻译条目:
    'aiNative.chat.ai.assistant.limit.message': '条最早的消息因输入 Tokens 限制而被丢弃',
    
  • 请确认该翻译与预期含义一致,无需进一步修改。

Copy link

codecov bot commented Feb 21, 2025

Codecov Report

Attention: Patch coverage is 5.88235% with 16 lines in your changes missing coverage. Please review.

Project coverage is 53.55%. Comparing base (23e4557) to head (a438a0c).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...ai-native/src/browser/model/msg-history-manager.ts 7.69% 10 Missing and 2 partials ⚠️
...ges/ai-native/src/browser/chat/chat.api.service.ts 0.00% 1 Missing and 1 partial ⚠️
...s/ai-native/src/browser/chat/chat-proxy.service.ts 0.00% 1 Missing ⚠️
packages/ai-native/src/node/base-language-model.ts 0.00% 0 Missing and 1 partial ⚠️
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     
Flag Coverage Δ
jsdom 49.06% <5.88%> (-0.01%) ⬇️
node 12.22% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 限制被截断时,通过条件渲染向用户展示提示信息。建议考虑以下优化:

  1. 添加视觉反馈(如图标)以提高可见性
  2. 考虑添加交互功能,让用户可以查看被截断的消息
 {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

📥 Commits

Reviewing files that changed from the base of the PR and between 5561e9b and 0d234f3.

📒 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 函数以支持显示本地化的消息限制提示。

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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)过于简化,可能不够准确。建议:

  1. 考虑使用专门的 tokenizer 库进行更准确的计算
  2. 考虑不同语言和字符类型对 token 数量的影响
  3. 将计算逻辑抽象为可配置的策略,便于后续调整
- 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

📥 Commits

Reviewing files that changed from the base of the PR and between 0d234f3 and 5def79a.

📒 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,在消息量大的情况下可能影响性能。

Aaaaash
Aaaaash previously approved these changes Feb 24, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 数量,这种方法可能不够准确:

  1. 不同的语言和字符集可能需要不同的计算方式
  2. 特殊字符和表情符号可能需要特殊处理

建议:

  1. 添加注释说明当前计算方法的局限性
  2. 考虑使用更准确的 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

📥 Commits

Reviewing files that changed from the base of the PR and between 908cbc3 and a438a0c.

📒 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);
 }

@ensorrow ensorrow merged commit ebc1030 into main Feb 24, 2025
11 checks passed
@ensorrow ensorrow deleted the feat/chat-long-history branch February 24, 2025 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎨 feature feature required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants