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: supports ai history relative feature #3989

Merged
merged 2 commits into from
Sep 25, 2024
Merged

Conversation

MMhunter
Copy link
Contributor

@MMhunter MMhunter commented Sep 3, 2024

Types

  • 🎉 New Features

Background or solution

Support AI Chat History which allows switching between ai chat sessions.

Changelog

  • Support AI Chat History API which allows switching between ai chat sessions.

Summary by CodeRabbit

  • 新功能

    • 增强聊天模型的依赖注入和历史管理功能。
    • 添加会话ID以改进聊天会话跟踪和管理。
    • 引入事件驱动机制以响应欢迎消息的变化。
    • 支持新的聊天视图头部渲染类型。
    • 改进消息回复处理,支持更多数据格式。
  • 错误修复

    • 修复了消息历史管理中的状态同步问题。
  • 文档

    • 更新了相关接口和类型的定义,以反映新的属性和方法。

Copy link

railway-app bot commented Sep 3, 2024

This PR was not deployed automatically as @MMhunter does not have access to the Railway project.

In order to get automatic PR deploys, please add @MMhunter to the project inside the project settings page.

Copy link
Contributor

coderabbitai bot commented Sep 3, 2024

Walkthrough

此次更改涉及多个文件,主要集中在聊天服务和聊天界面的功能增强。引入了新的属性,如sessionId,以支持会话跟踪,并优化了消息历史管理。此外,更新了多个组件和接口,以改善数据结构和消息处理逻辑,增强了响应处理能力和事件驱动机制。

Changes

文件路径 更改摘要
packages/ai-native/src/browser/chat/chat-proxy.service.ts ChatProxyService类中新增sessionId属性,以支持会话信息的处理。
packages/ai-native/src/browser/chat/chat.view.tsx 重构了AIChatView组件,改善了消息处理逻辑,新增DefaultChatViewHeader组件和多个渲染回调函数。
packages/ai-native/src/browser/components/ChatReply.tsx 更新了IChatReplyProps接口,新增historymsgId属性,简化了消息更新逻辑。
packages/ai-native/src/browser/components/WelcomeMsg.tsx 引入useUpdateOnEvent钩子,以响应欢迎消息的变化。
packages/core-common/src/types/ai-native/index.ts 更新了IAIBackServiceOptionIHistoryChatMessage接口,新增sessionIdrequestIdreplyStartTime等属性,以改善数据结构。

Possibly related PRs

  • fix: improve chat reporter #3832: chat.view.tsxChatReply.tsx的更改相关,因为它们都涉及聊天功能的修改,包括更新消息和响应的处理,与主PR中的sessionId添加相一致。

Suggested reviewers

  • erha19
  • bytemain
  • hacke2

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

@opensumi opensumi bot added the 🎨 feature feature required label Sep 3, 2024
@MMhunter
Copy link
Contributor Author

MMhunter commented Sep 3, 2024

/next

@opensumi
Copy link
Contributor

opensumi bot commented Sep 3, 2024

🎉 PR Next publish successful!

3.2.5-next-1725353631.0

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

Outside diff range, codebase verification and nitpick comments (2)
packages/ai-native/src/browser/chat/chat.api.service.ts (1)

12-13: 代码更改已批准。

ChatService 类的更改增强了聊天系统的功能和灵活性:

  • 通过集成一个更复杂的内部服务来管理聊天会话和回复,从而提高了 ChatService 的功能。
  • 修改 _onChatReplyMessageLaunch 的事件类型,以及更新 sendReplyMessage 方法以处理字符串和复杂对象类型,提高了聊天系统的灵活性。
  • 修改 clearHistoryMessagesgetHistoryMessages 方法以使用 chatInternalService.sessionModel?.history,改进了聊天历史记录的管理。

这些更改与 PR 目标和 AI 生成的摘要是一致的,并且改进了聊天系统的整体架构。

此外,请考虑以下建议以进一步改进代码:

  • chatInternalService 添加 null 检查,以防止在服务未初始化时访问 sessionModelhistory 时出现潜在的 null 引用错误。
  • _onChatReplyMessageLaunch 事件添加更详细的 JSDoc,以清楚地说明何时触发事件以及有效的数据类型。
  • 如果 sendReplyMessage 方法的输入数据类型无效,则抛出一个错误或记录一个警告。这将有助于尽早发现潜在的错误。

Also applies to: 21-22, 47-47, 53-61, 69-69

packages/ai-native/src/browser/chat/chat-model.ts (1)

227-228: LGTM!

代码更改已批准。

建议: 考虑使用接口而不是具体类来声明依赖项。

为了进一步提高模块化和可测试性,建议使用接口而不是具体类 MsgHistoryManager 来声明 history 的返回类型。这将允许在不修改 ChatModel 的情况下,轻松地用不同的实现替换 MsgHistoryManager,例如用于测试目的。

例如,可以定义一个 IMsgHistoryManager 接口,并让 MsgHistoryManager 实现它:

interface IMsgHistoryManager {
  // 在此声明 MsgHistoryManager 的公共方法
}

class MsgHistoryManager implements IMsgHistoryManager {
  // 实现接口方法
}

然后更新 history 的返回类型为接口:

@memoize 
get history(): IMsgHistoryManager {
  return this.injector.get(MsgHistoryManager, []);
}

Also applies to: 240-243

Copy link

codecov bot commented Sep 3, 2024

Codecov Report

Attention: Patch coverage is 11.76471% with 30 lines in your changes missing coverage. Please review.

Project coverage is 54.41%. Comparing base (acc8715) to head (cf23d7e).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
...ges/ai-native/src/browser/chat/chat.api.service.ts 0.00% 9 Missing and 3 partials ⚠️
...i-native/src/browser/chat/chat.internal.service.ts 0.00% 8 Missing and 1 partial ⚠️
...i-native/src/browser/chat/chat.feature.registry.ts 0.00% 4 Missing ⚠️
packages/ai-native/src/browser/chat/chat-model.ts 66.66% 2 Missing ⚠️
...ai-native/src/browser/chat/chat.render.registry.ts 0.00% 2 Missing ⚠️
...ai-native/src/browser/model/msg-history-manager.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #3989       +/-   ##
===========================================
+ Coverage   15.58%   54.41%   +38.83%     
===========================================
  Files        1584     1584               
  Lines       97014    97035       +21     
  Branches    19860    19857        -3     
===========================================
+ Hits        15120    52802    +37682     
+ Misses      68788    36739    -32049     
+ Partials    13106     7494     -5612     
Flag Coverage Δ
jsdom 49.94% <11.76%> (?)
node 15.58% <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: 4

Outside diff range and nitpick comments (4)
packages/ai-native/src/browser/chat/chat-proxy.service.ts (1)

86-86: 添加 sessionId 支持聊天历史功能

这个更改看起来不错,它为 AI 聊天历史功能添加了必要的支持。

建议:

  1. 考虑在使用 request.sessionId 之前添加一个检查,以确保它始终存在。例如:
    sessionId: request.sessionId ?? generateDefaultSessionId(),
  2. 更新相关的文档或注释,以反映这个新增的 sessionId 参数及其用途。
packages/ai-native/src/browser/components/WelcomeMsg.tsx (1)

35-36: 新增的 hook 使用需要澄清

useUpdateOnEvent 的使用是正确的,它可能用于在欢迎消息发生变化时触发重新渲染。然而,这个更新的效果在组件逻辑中并不明显。

建议添加一个注释来解释这个 hook 的具体作用,例如:

// 当欢迎消息发生变化时触发组件重新渲染
useUpdateOnEvent(chatFeatureRegistry.onDidWelcomeMessageChange);

这将有助于其他开发者理解这段代码的目的。

packages/ai-native/src/browser/types.ts (2)

152-155: 新增的 ChatViewHeaderRender 类型定义良好,但可以进一步改进。

新增的 ChatViewHeaderRender 类型定义清晰,符合 React 组件的模式。它为聊天视图的头部渲染提供了灵活性。然而,有一个小的改进建议:

考虑将 handleClearhandleCloseChatView 的返回类型从 any 改为更具体的类型,比如 void。这样可以提高类型安全性和代码可读性。例如:

export type ChatViewHeaderRender = (props: {
  handleClear: () => void;
  handleCloseChatView: () => void;
}) => React.ReactElement | React.JSX.Element;

Line range hint 152-177: 总结:新增的类型和方法为 AI 聊天历史功能奠定了基础

这些更改通过引入 ChatViewHeaderRender 类型和在 IChatRenderRegistry 接口中添加 registerChatViewHeaderRender 方法,为支持 AI 聊天历史功能提供了必要的类型定义和结构。这些添加与现有代码结构保持一致,并遵循了既定的模式。

这些更改为实现 PR 中提到的 AI 聊天历史功能奠定了良好的基础。在进一步开发时,请考虑以下建议:

  1. 确保在实现 ChatViewHeaderRender 的组件中正确处理 handleClearhandleCloseChatView 方法。
  2. 考虑添加单元测试,以验证新增的类型和方法的正确使用。
  3. 更新相关文档,说明如何使用这些新增的类型和方法来自定义聊天视图的顶部栏。

这些更改为增强应用程序的 AI 聊天功能提供了良好的起点。

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ba22c57 and cf23d7e.

Files selected for processing (11)
  • packages/ai-native/src/browser/ai-core.contribution.ts (0 hunks)
  • packages/ai-native/src/browser/chat/chat-proxy.service.ts (1 hunks)
  • packages/ai-native/src/browser/chat/chat.view.tsx (10 hunks)
  • packages/ai-native/src/browser/components/ChatEditor.tsx (1 hunks)
  • packages/ai-native/src/browser/components/ChatReply.tsx (3 hunks)
  • packages/ai-native/src/browser/components/WelcomeMsg.tsx (2 hunks)
  • packages/ai-native/src/browser/types.ts (2 hunks)
  • packages/ai-native/src/browser/widget/inline-chat/inline-chat-editor.controller.ts (0 hunks)
  • packages/ai-native/src/browser/widget/inline-diff/inline-diff.controller.ts (0 hunks)
  • packages/core-common/src/types/ai-native/index.ts (2 hunks)
  • packages/extension/tests/browser/main.thread.workspace.test.ts (0 hunks)
Files not reviewed due to no reviewable changes (4)
  • packages/ai-native/src/browser/ai-core.contribution.ts
  • packages/ai-native/src/browser/widget/inline-chat/inline-chat-editor.controller.ts
  • packages/ai-native/src/browser/widget/inline-diff/inline-diff.controller.ts
  • packages/extension/tests/browser/main.thread.workspace.test.ts
Additional context used
Biome
packages/core-common/src/types/ai-native/index.ts

[error] 345-349: The enum declaration should not be const

Const enums are not supported by bundlers and are incompatible with the 'isolatedModules' mode. Their use can lead to import inexistent values.
See TypeScript Docs for more details.
Safe fix: Turn the const enum into a regular enum.

(lint/suspicious/noConstEnum)

Additional comments not posted (10)
packages/ai-native/src/browser/components/WelcomeMsg.tsx (1)

3-3: 导入语句更新正确

新增的 useUpdateOnEvent 导入是合理的,并且与现有的导入很好地组合在一起。这种做法有助于保持代码的整洁和组织性。

packages/core-common/src/types/ai-native/index.ts (3)

123-123: 添加 sessionId 属性以支持会话跟踪

添加 sessionId 可选属性是一个很好的改进。这将允许在 AI 后端服务选项中进行会话跟踪,有助于实现 PR 中提到的 AI 聊天历史功能。由于它是可选的,因此保持了与现有代码的向后兼容性。


344-345: IHistoryChatMessage 接口的重要更新

这些更改增强了 IHistoryChatMessage 接口的功能和灵活性:

  1. component 改为 componentId:这种变化表明我们现在使用标识符而不是直接使用 React 节点,这可能会提高性能和可序列化性。

  2. componentProps 改为 componentValue:这增加了存储的数据类型的灵活性。

  3. 新增 requestIdreplyStartTime:这些属性有助于更详细地跟踪聊天消息,支持更高级的功能如消息排序和响应时间分析。

这些变更与 PR 的目标一致,有效支持了 AI 聊天历史功能的实现。然而,请确保更新所有使用此接口的地方,以避免潜在的兼容性问题。

Also applies to: 349-350


123-123: 代码变更的整体影响和验证建议

这些更改很好地支持了 AI 聊天历史功能的实现,提供了更灵活和详细的聊天会话跟踪。然而,由于修改了关键接口,可能需要在代码库的其他部分进行相应的更新。

建议进行以下验证步骤:

  1. 检查所有使用 IAIBackServiceOptionIHistoryChatMessage 接口的地方,确保它们与新的结构兼容。
  2. 更新相关的单元测试和集成测试,以覆盖新增的属性和功能。
  3. 验证这些更改是否正确地支持了 AI 聊天历史的切换功能。

为了帮助验证这些更改的影响,您可以运行以下脚本:

这个脚本将帮助您识别可能需要更新的文件和潜在的兼容性问题。

Also applies to: 344-345, 349-350

Verification successful

为了继续验证这些更改的影响,请运行以下脚本:


验证通过,无发现问题

根据执行的脚本结果,所有相关接口的使用均已正确更新,且未发现任何旧属性名的使用。代码变更已经全面应用,没有兼容性问题。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 搜索使用了 IAIBackServiceOption 和 IHistoryChatMessage 的文件
echo "Files using IAIBackServiceOption:"
rg "IAIBackServiceOption" --type typescript

echo "\nFiles using IHistoryChatMessage:"
rg "IHistoryChatMessage" --type typescript

# 检查是否有地方仍在使用旧的属性名
echo "\nPotential uses of old property names:"
rg "component\?: React\.ReactNode|componentProps\?: \{ \[key in string\]: any; \}" --type typescript

Length of output: 539


Script:

#!/bin/bash
# 搜索使用了 IAIBackServiceOption 和 IHistoryChatMessage 的文件
echo "Files using IAIBackServiceOption:"
rg "IAIBackServiceOption" --glob "*.ts" --glob "*.tsx"

echo "\nFiles using IHistoryChatMessage:"
rg "IHistoryChatMessage" --glob "*.ts" --glob "*.tsx"

# 检查是否有地方仍在使用旧的属性名
echo "\nPotential uses of old property names:"
rg "component\?: React\.ReactNode|componentProps\?: \{ \[key in string\]: any; \}" --glob "*.ts" --glob "*.tsx"

Length of output: 2637

packages/ai-native/src/browser/types.ts (1)

173-177: IChatRenderRegistry 接口的更新恰当且一致。

IChatRenderRegistry 接口中新增的 registerChatViewHeaderRender 方法定义清晰,并且与接口的现有结构保持一致。方法名称和参数遵循了既定的模式,使用新定义的 ChatViewHeaderRender 类型作为参数。

添加的注释 "顶部栏渲染" 简洁地说明了该方法的用途,有助于开发者理解其功能。

这个更改增强了聊天渲染注册表的功能,允许自定义聊天视图的顶部栏渲染,这是一个很好的扩展。

packages/ai-native/src/browser/components/ChatReply.tsx (5)

51-51: 接口更新已批准,建议更新文档

新增的 historymsgId 属性改变了组件的使用方式。这是一个好的改进,有助于更好地管理聊天历史。

建议更新组件文档,说明这些新属性的用途和使用方法,以便其他开发者正确使用更新后的组件。

Also applies to: 58-58


162-162: 依赖数组更新已批准,需要澄清

props.componentprops.value 添加到 useEffect 的依赖数组中是正确的做法,这确保了当这些属性变化时重新渲染组件。

请澄清为什么这些依赖之前没有被包含在内。是否存在特定的原因或这是一个遗漏?


168-179: 属性解构更新已批准

新增的 historymsgId 属性已正确地从组件属性中解构出来,与接口更新保持一致。这为在组件内使用这些新属性做好了准备。


199-199: 消息更新逻辑改进已批准,建议小幅优化

使用 history.updateAssistantMessage 来更新助手消息是一个很好的改进。这种方法提高了代码的模块化程度,并允许更好地分离关注点。

建议考虑将 { content: request.response.responseText } 对象提取为一个常量或变量,以提高可读性和可维护性。例如:

const updatedContent = { content: request.response.responseText };
history.updateAssistantMessage(msgId, updatedContent);

这样可以使代码更清晰,并为将来可能的扩展(如添加更多属性)做好准备。


Line range hint 1-379: 总体评审结论

本次更改主要涉及 ChatReply 组件的改进,包括新增 historymsgId 属性,以及相应的逻辑更新。这些变更提高了组件的模块化程度和可维护性,是很好的改进。

主要亮点:

  1. 接口更新合理,增加了对聊天历史的管理能力。
  2. 组件内部逻辑更新与新属性一致,提高了代码的清晰度。
  3. 消息更新逻辑的改进增强了组件的灵活性。

建议:

  1. 更新组件文档,说明新属性的用途和使用方法。
  2. 澄清 ComponentRender 中 useEffect 依赖数组的变更原因。
  3. 考虑小幅优化消息更新逻辑,提高代码可读性。

总的来说,这些更改是积极的,提高了代码质量和组件的功能性。在解决了提出的小问题后,这些更改可以被批准合并。

Copy link
Member

@Ricbet Ricbet left a comment

Choose a reason for hiding this comment

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

LGTM

@Ricbet Ricbet changed the title WIP: feat: supports ai history relative feature feat: supports ai history relative feature Sep 24, 2024
@MMhunter
Copy link
Contributor Author

/next

@opensumi
Copy link
Contributor

opensumi bot commented Sep 25, 2024

🎉 PR Next publish successful!

3.3.4-next-1727236450.0

@bytemain bytemain merged commit 17643d8 into main Sep 25, 2024
14 checks passed
@bytemain bytemain deleted the feat/ai-chat-history branch September 25, 2024 09:29
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