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

chore: improve chat context #4407

Open
wants to merge 11 commits into
base: v3.8
Choose a base branch
from
Open

chore: improve chat context #4407

wants to merge 11 commits into from

Conversation

Aaaaash
Copy link
Member

@Aaaaash Aaaaash commented Feb 25, 2025

Types

  • 🎉 New Features
  • 🐛 Bug Fixes
  • 📚 Documentation Changes
  • 💄 Code Style Changes
  • 💄 Style Changes
  • 🪚 Refactors
  • 🚀 Performance Improvements
  • 🏗️ Build System
  • ⏱ Tests
  • 🧹 Chores
  • Other Changes

Background or solution

Changelog

Summary by CodeRabbit

  • 新功能

    • 文件管理现支持分别显示附件文件与近期浏览文件,提升了文件信息展示的准确性。
  • 重构

    • 优化了文件添加及更新逻辑,使文件列表处理更精准、高效。

@Aaaaash
Copy link
Member Author

Aaaaash commented Feb 25, 2025

/next

@Aaaaash Aaaaash changed the base branch from main to v3.8 February 25, 2025 01:51
Copy link
Contributor

coderabbitai bot commented Feb 25, 2025

Walkthrough

本次变更主要在文件处理逻辑上进行调整。在 ChatContext 组件中,更新函数 updateAddedFiles 的参数由整个文件对象改为仅传递其 attached 属性。同时,在 llm-context 服务及相关公共接口中,对上下文文件进行重构:将单一数组拆分为“已附加”和“最近浏览”两个分类,更新了方法签名、事件 payload 和序列化逻辑,移除了冗余属性,提升了代码结构的清晰度。

Changes

文件 改动说明
packages/ai-native/.../ChatContext/index.tsx useEffect 中调用 updateAddedFiles 时,仅传入 files.attached,而不是整个 files 对象。
packages/ai-native/.../llm-context.service.ts
packages/ai-native/.../llm-context.ts
重构文件上下文管理:将文件分为“已附加”和“最近浏览”两类;修改方法签名及导入;新增辅助方法用于列表添加、序列化和错误处理;更新了事件 payload 与相关接口定义,移除 isManual 属性。

Sequence Diagram(s)

sequenceDiagram
    participant User as 用户
    participant LLM as LLMContextService
    participant List as addFileToList
    participant Notifier as 事件通知器

    User->>LLM: addFileToContext(uri, selection, isManual)
    LLM->>LLM: 判断文件类型(已附加 vs 最近浏览)
    LLM->>List: 调用 addFileToList 添加文件
    List-->>LLM: 返回更新后的列表
    LLM->>Notifier: 触发上下文文件更新事件
    Notifier-->>LLM: 事件确认
Loading

Suggested reviewers

  • life2015
  • hacke2

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: 0

🧹 Nitpick comments (5)
packages/ai-native/src/common/llm-context.ts (1)

46-46: attachedFiles 类型替换为 AttachFileContext[]

原先为行内定义;统一改成接口后,更易维护和扩展。请确保序列化、反序列化逻辑在其他地方也同步更新。

packages/ai-native/src/browser/context/llm-context.service.ts (4)

37-47: 新增私有方法 addFileToList

封装了插入和裁剪列表逻辑,易于维护;不过 list.shift() 会移除最早插入的项,请确认这样处理符合预期,避免误删除较为重要的文件。


62-75: 通知上下文变更与清理附加文件

  • notifyContextChange 统一触发 onDidContextFilesChangeEmitter,有助于事件订阅方获取最新文件上下文。
  • cleanFileContext 仅清空 attachedFiles,保留 recentlyViewFiles,避免“最近查看”丢失;如需进一步清理,也需在设计上明确。

161-165: 序列化方法:serializeRecentlyViewFilesserializeAttachedFiles

  • map(...).filter(Boolean) 可以安全过滤无效项,但如需调试原因,建议记录日志。
  • serializeAttachedFiles 显示转换为 AttachFileContext[],若后续增添字段,需同步修改此方法。

Also applies to: 167-171


191-199: 私有方法 getFileErrors

  • 根据 MarkerSeverity.Error 查找编辑器标记,可明确获取当前文件的错误信息。
  • 若需警告或信息级别的标记,也可扩展此方法以实现更丰富的上下文提示。
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 83d7b2e and cc4e3c8.

📒 Files selected for processing (3)
  • packages/ai-native/src/browser/components/ChatContext/index.tsx (1 hunks)
  • packages/ai-native/src/browser/context/llm-context.service.ts (6 hunks)
  • packages/ai-native/src/common/llm-context.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: 🚀🚀🚀 Next Version for pull request
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: build (macos-latest, 20.x)
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: unittest (macos-latest, 18.x, node)
  • GitHub Check: build-windows
  • GitHub Check: ubuntu-latest, Node.js 20.x
🔇 Additional comments (12)
packages/ai-native/src/browser/components/ChatContext/index.tsx (1)

42-42: 仅传入 files.attached

该改动确保 updateAddedFiles 只更新已附加文件列表,逻辑更清晰。请确认组件中确实只需要已附加文件数据,不会影响对“最近查看”文件的管理。

packages/ai-native/src/common/llm-context.ts (2)

18-18: 改动了上下文事件的载荷结构

将原本的 Event<FileContext[]> 改为 Event<{ viewed: FileContext[]; attached: FileContext[] }>,允许分开处理已附加与最近查看的文件。请确认所有订阅方已同步适配新的事件类型。


37-42: 新增 AttachFileContext 接口

定义了附加文件的内容、错误行、路径及语言等字段。此结构有利于更灵活地处理文件信息,但请验证下游对这些字段的使用场景,以免出现空字段或额外依赖。

packages/ai-native/src/browser/context/llm-context.service.ts (9)

15-15: 导入了新接口 AttachFileContext

此处仅增加新接口的 import,便于在序列化和上下文管理中使用。


30-34: 配置新上限与数组初始化

  • maxAttachFilesLimit = 10maxViewFilesLimit = 20:请确认是否符合业务需求,若数值过小或过大可能导致文件处理受限。
  • 初始化两个数组 attachedFilesrecentlyViewFiles,分类逻辑更清晰。

49-60: 变更 addFileToContext 逻辑

  • 默认 isManual = false,将文件区分到 recentlyViewFilesattachedFiles
  • 若对同一 URI 多次调用,可能覆盖原 selection 信息,请留意需求是否需要保留或合并之前的选区。

78-85: removeFileFromContext 基于 isManual 移除文件

addFileToContext 对应,注意手动与自动收集的文件可能混杂,确保外部调用方都正确传入 isManual 以免误删文件。


102-103: 自动收集事件:添加文件到查看列表

在文档模型创建事件中调用 addFileToContext(..., false),能保证每次打开文件自动进入“最近查看”。如需特殊处理临时文件,请另外判断。


112-113: 自动收集事件:移除文件

在文档模型移除事件中调用 removeFileFromContext(..., false),确保不再显示在“最近查看”列表。逻辑简单易懂,暂未发现问题。


134-134: 编辑器选区判断逻辑

当选区是单行时(start 行与 end 行相同),会将文件添加到“最近查看”。多行选区则连带区间一起存储。请确认这与实际需求匹配,尤其在大段选区时是否影响性能。

Also applies to: 139-140


153-153: 序列化上下文根路径与文件

  • 使用 URI.file(this.appConfig.workspaceDir) 作为根目录时,需确保 workspaceDir 已正确配置。
  • 分别序列化 recentlyViewFilesattachedFiles,结构更直观。

Also applies to: 156-157


173-189: 私有方法 serializeAttachedFile

  • 提取文档内容、语言、行级错误信息,整合到 AttachFileContext
  • 异常捕获返回 null,不会阻断流程,如需查看具体错误可考虑日志记录。

@opensumi
Copy link
Contributor

opensumi bot commented Feb 25, 2025

🎉 PR Next publish successful!

3.7.2-next-1740448398.0

Copy link

codecov bot commented Feb 25, 2025

Codecov Report

Attention: Patch coverage is 13.69863% with 63 lines in your changes missing coverage. Please review.

Project coverage is 53.44%. Comparing base (b61cba5) to head (359e34b).

Files with missing lines Patch % Lines
...-native/src/browser/context/llm-context.service.ts 0.00% 43 Missing and 9 partials ⚠️
...ai-native/src/browser/chat/chat-manager.service.ts 44.44% 5 Missing ⚠️
...tive/src/common/prompts/context-prompt-provider.ts 0.00% 3 Missing ⚠️
...ative/src/browser/mcp/tools/handlers/RunCommand.ts 0.00% 2 Missing ⚠️
packages/ai-native/src/browser/chat/chat-model.ts 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             v3.8    #4407      +/-   ##
==========================================
- Coverage   53.44%   53.44%   -0.01%     
==========================================
  Files        1657     1657              
  Lines      102103   102136      +33     
  Branches    22076    22095      +19     
==========================================
+ Hits        54572    54583      +11     
- Misses      39550    39565      +15     
- Partials     7981     7988       +7     
Flag Coverage Δ
jsdom 48.95% <13.69%> (-0.01%) ⬇️
node 12.19% <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.

@Aaaaash
Copy link
Member Author

Aaaaash commented Feb 25, 2025

/next

@opensumi
Copy link
Contributor

opensumi bot commented Feb 25, 2025

🎉 PR Next publish successful!

3.7.2-next-1740450374.0

@Aaaaash Aaaaash force-pushed the chore/improve-chat-context branch from 1245846 to 7141eb9 Compare February 25, 2025 02:50
@Aaaaash
Copy link
Member Author

Aaaaash commented Feb 25, 2025

/next

@opensumi
Copy link
Contributor

opensumi bot commented Feb 25, 2025

🎉 PR Next publish successful!

3.8.1-next-1740452092.0

@Aaaaash Aaaaash force-pushed the chore/improve-chat-context branch from 61bc212 to 6920039 Compare February 25, 2025 06:02
@Aaaaash
Copy link
Member Author

Aaaaash commented Feb 25, 2025

/next

@opensumi
Copy link
Contributor

opensumi bot commented Feb 25, 2025

🎉 PR Next publish successful!

3.8.1-next-1740463566.0

@Aaaaash
Copy link
Member Author

Aaaaash commented Feb 25, 2025

/next

@opensumi
Copy link
Contributor

opensumi bot commented Feb 25, 2025

🎉 PR Next publish successful!

3.8.1-next-1740465035.0

@Aaaaash
Copy link
Member Author

Aaaaash commented Feb 25, 2025

/next

@opensumi
Copy link
Contributor

opensumi bot commented Feb 25, 2025

🎉 PR Next publish successful!

3.8.1-next-1740467196.0

@Aaaaash
Copy link
Member Author

Aaaaash commented Feb 25, 2025

/next

@opensumi
Copy link
Contributor

opensumi bot commented Feb 25, 2025

🎉 PR Next publish successful!

3.8.1-next-1740475512.0

@Aaaaash
Copy link
Member Author

Aaaaash commented Feb 25, 2025

/next

@opensumi
Copy link
Contributor

opensumi bot commented Feb 25, 2025

🎉 PR Next publish successful!

3.8.1-next-1740478950.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant