Skip to content

Conversation

@xiaolan20020118-create
Copy link
Contributor

谢谢dingyi喵~麻烦了!

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @xiaolan20020118-create, 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!

This pull request focuses on upgrading the Dify adapter by enhancing the flow of contextual data within the chat system. It ensures that essential identifiers such as user, bot, and group IDs are correctly captured and passed from the core chat service to the Dify adapter, enabling more personalized and context-aware interactions within the Dify platform.

Highlights

  • Dify Adapter Enhancement: The Dify adapter has been updated to extract and utilize more contextual information, specifically user_id, bot_id, and group_id, from the additional_kwargs of incoming chat messages.
  • Core Chat Service Context Propagation: The core chat service now actively populates the additional_kwargs of chat messages with user_id, bot_id, and group_id derived from the current session, ensuring this vital context is available for downstream adapters like Dify.
  • Minor File Formatting: Trailing newlines were removed from .gitignore and tsconfig.json files, which are minor cosmetic changes.
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.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 17, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

此 PR 在多个链和模型层引入并传播了 variables / variables_hide 字段,调整了 Dify 适配器中用户/群组字段的来源为 params.variables,扩展了模型请求类型并增强了系统提示变量以包含群组相关字段与布尔标志。

Changes

内聚 / 文件(s) 改动说明
Dify 适配器参数源调整
packages/adapter-dify/src/requester.ts
将 chatluna_user_id、chatluna_user_name 等用户字段以及新增的 chatluna_bot_id、chatluna_group_id 的来源从最后一个输入项改为 params.variables;统一 payload 构建使用 variables
依赖注入扩展
packages/adapter-dify/src/index.ts
将公开 DI 注入列表由 ['chatluna'] 扩展为 ['chatluna', 'database']
链中 variables_hide 镜像
packages/core/src/llm-core/chain/chat_chain.ts, packages/core/src/llm-core/chain/plugin_chat_chain.ts, packages/service-search/src/chain/browsing_chain.ts
在各链的请求对象中新增 variables_hide 字段并赋值为 variables 的副本/别名(镜像引用)
模型请求参数类型扩展
packages/core/src/llm-core/platform/api.ts
ModelRequestParams 接口中新增可选字段 variables?: Record<string, any>
模型调用选项与负载映射
packages/core/src/llm-core/platform/model.ts
ChatLunaModelCallOptions 添加 variablesvariables_hide 字段;在 invocationParams 中将 variables 映射为 options?.['variables_hide'] ?? {} 并将 variables_hide 列入 callKeys
系统提示变量增强
packages/core/src/utils/string.ts
getSystemPromptVariables 返回中将 is_groupis_private 改为布尔,新增 group_idgroup_name 字段(基于 session 与 event.guild)
格式调整
.gitignore
.yarn/install-state.gz 条目仅做格式重写(无功能改动)

Sequence Diagram(s)

sequenceDiagram
    participant Client as 调用方
    participant Model as ChatLunaModel
    participant Chain as 链(Chat/Plugin/Browsing)
    participant Adapter as Dify 适配器

    Note over Client,Model: 请求构建阶段
    Client->>Model: invoke(options 包含 variables_hide?)
    Model->>Chain: 生成 requests, 设置 requests.variables
    Chain->>Chain: 设置 requests['variables_hide'] = requests['variables']
    Chain->>Adapter: 发出最终请求 (包含 variables / variables_hide)

    Note right of Adapter: 适配器读取参数来源
    Adapter->>Adapter: 从 params.variables 读取 user_id,user_name,bot_id,group_id
    Adapter->>External: 向外部 API 发送包含新字段的 payload
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 变量悄然藏,镜像轻轻扬,
群组与机器人,一并入稿章,
Dify 换了源,模型接收忙,
小兔舞步巧,链路更安康 🥕

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning 拉取请求描述为"谢谢dingyi喵~麻烦了!"(感谢dingyi,麻烦了),这是一条感谢信息,与代码变更集完全无关。描述没有说明任何关于变量处理改进、适配器修改或任何代码层面的改动信息,完全是一条无关的感谢内容,不符合拉取请求描述应该与变更相关的基本要求。
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed 标题"refactor(core,adapter-dify): improve variable handling and remove redundant additional_kwargs"与变更集的主要改动相关。标题中的"improve variable handling"部分准确反映了变更的核心内容,即在多个文件中添加variables和variables_hide字段的处理,这涉及adaptor-dify和core包的重大重构。虽然标题还提到"remove redundant additional_kwargs",但这部分在提供的摘要中没有明确体现,不过标题的主要焦点清晰且与大部分变更相关。
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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 升级了 Dify 适配器,为请求增加了 user_idbot_idgroup_id 等更多上下文信息,这是一个很好的改进。

packages/adapter-dify/src/requester.ts 文件中,我注意到 _agentStream 方法中的代码存在重复,建议进行重构以提高可读性。

此外,似乎只有 _agentStream 方法被更新了。_workflowStream 方法可能也需要进行类似的更新,以确保行为一致性。目前它仍在为 chatluna_user_id 使用旧的逻辑,并且缺少新的 bot_idgroup_id 字段。

Kotoko76 and others added 5 commits October 17, 2025 23:52
…undant additional_kwargs

- Move user/bot/group metadata from additional_kwargs to dedicated variables field
- Add variables_hide field to preserve variables through chain execution
- Standardize variable passing across chat chains (chat, plugin, browsing)
- Fix Dify adapter to use variables instead of additional_kwargs
- Add database injection to Dify adapter
- Improve system prompt variables: convert boolean strings to actual booleans, add group_id and group_name

This change improves the separation of concerns by moving metadata out of message
additional_kwargs and into a dedicated variables system that flows through the
entire chain execution pipeline.
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

♻️ Duplicate comments (2)
packages/core/src/llm-core/chain/plugin_chat_chain.ts (1)

176-176: variables_hide 引用别名的一致性

chat_chain.ts 中相同,这里也将 variables_hide 设置为 variables 的引用别名。请确保这种模式在整个代码库中的用途和行为一致。

packages/service-search/src/chain/browsing_chain.ts (1)

231-231: 变量别名的一致处理

与其他 chain 文件保持一致的模式。确保 variables_hide 作为引用别名的使用在所有 chain 实现中都被正确理解和处理。

🧹 Nitpick comments (1)
packages/core/src/llm-core/platform/api.ts (1)

69-71: 考虑为 variables 定义更明确的类型

虽然使用 Record<string, any> 提供了灵活性,但可以考虑定义一个更具体的接口来提高类型安全性,特别是考虑到该字段在多个地方被访问(如 user_idbot_idgroup_id 等)。

可选的改进建议:

interface ModelVariables {
    user_id?: string
    user?: string
    bot_id?: string
    group_id?: string
    [key: string]: any  // 保留灵活性
}

// 然后在 ModelRequestParams 中使用
variables?: ModelVariables
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c7d3d95 and a597bc8.

⛔ Files ignored due to path filters (1)
  • tsconfig.json is excluded by !**/*.json
📒 Files selected for processing (9)
  • .gitignore (1 hunks)
  • packages/adapter-dify/src/index.ts (1 hunks)
  • packages/adapter-dify/src/requester.ts (2 hunks)
  • packages/core/src/llm-core/chain/chat_chain.ts (1 hunks)
  • packages/core/src/llm-core/chain/plugin_chat_chain.ts (1 hunks)
  • packages/core/src/llm-core/platform/api.ts (1 hunks)
  • packages/core/src/llm-core/platform/model.ts (3 hunks)
  • packages/core/src/utils/string.ts (1 hunks)
  • packages/service-search/src/chain/browsing_chain.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/adapter-dify/src/index.ts (5)
packages/extension-mcp/src/command.ts (1)
  • inject (240-240)
packages/adapter-deepseek/src/index.ts (1)
  • inject (76-76)
packages/adapter-gemini/src/index.ts (1)
  • inject (95-98)
packages/adapter-openai/src/index.ts (1)
  • inject (76-76)
packages/extension-mcp/src/index.ts (1)
  • inject (71-74)
🔇 Additional comments (7)
.gitignore (1)

26-26: ✅ .gitignore 条目添加合理

添加 .yarn/install-state.gz 是一个标准实践,用于忽略 Yarn 包管理器的环境特定缓存文件,防止其被提交到版本控制中。

⚠️ 建议验证文件范围

根据PR描述,此PR应该涉及对链处理路径、变量处理和模型请求参数类型的重大修改。但本次审查中仅提供了 .gitignore 文件。建议确认是否所有相关的代码变更文件都已包含在审查范围内。

packages/core/src/llm-core/chain/chat_chain.ts (1)

110-110: variables_hide 作为 variables 的引用别名

variables_hide 被直接赋值为 variables 的引用,而不是深拷贝。这意味着对任一对象的修改都会影响另一个。

如果 variables_hide 的目的是作为不可变的快照或用于不同的处理路径,请考虑深拷贝:

-        requests['variables_hide'] = requests['variables']
+        requests['variables_hide'] = { ...requests['variables'] }

否则,如果引用别名是预期行为,建议在代码中添加注释说明 variables_hide 的用途。

packages/adapter-dify/src/requester.ts (2)

189-192: Workflow 路径中的一致变更

_workflowStream 方法中的变更与 _agentStream 保持一致,确保了两个执行路径的行为统一。


97-100: 需要在调用层验证 params.variables 的填充

代码安全性方面没有问题(使用可选链操作符防止运行时错误),但存在验证缺陷:

  • ModelRequestParams.variables 定义为可选字段 (variables?: Record<string, any>)
  • completionStreamInternal 接收到的 paramsvariables 可能为 undefined
  • 框架层无验证机制,直接将 user_idbot_idgroup_iduserundefined 值发送给 Dify API
  • 若这些字段为 Dify API 必需,可能导致 API 调用失败

建议:

  1. 在调用 completionStream 前确认所有代码路径均正确填充 params.variables
  2. 或在 completionStreamInternal 内添加验证逻辑,对缺失的必需字段抛出错误
  3. 或为缺失字段提供合理的默认值
packages/core/src/llm-core/platform/model.ts (3)

78-83: 新增的 variablesvariables_hide 选项

ChatLunaModelCallOptions 中添加这两个字段扩展了模型调用的参数能力,支持传递额外的上下文变量。


124-141: variables_hide 被正确添加到 callKeys

variables_hide 添加到 callKeys 数组确保该选项在模型调用时被正确识别和处理。这与 chain 层的实现相呼应。


178-178: variables_hidevariables 的映射逻辑

invocationParamsoptions?.['variables_hide'] 映射到 variables 字段,并提供 {} 作为默认值。这形成了一个清晰的数据流:chain 层设置 variables_hide → model 层读取并映射为 variables → requester 层使用 variables

建议在代码或文档中说明 variablesvariables_hide 的区别和用途,以便未来的维护者理解这种设计。

@dingyi222666 dingyi222666 changed the title dify-adapter upgrade refactor(core,adapter-dify): improve variable handling and remove redundant additional_kwargs Oct 17, 2025
…t variables

- Add optional chaining (?.) to safely access session.event.guild.id and session.event.guild.name
- Simplify is_group condition to only check session.isDirect

This prevents potential runtime errors when guild properties are undefined.
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/core/src/utils/string.ts (1)

397-398: 空指针问题已修复,但 group_name 的回退逻辑值得商榷

代码已正确使用可选链 (?.) 修复了之前审查中标记的空指针安全问题。

然而,group_name 的回退逻辑使用 session.guildId(一个 ID)作为名称的后备值,这可能不太合理。建议考虑以下选项:

  • 使用空字符串或 undefined 作为回退值
  • 使用更具描述性的默认值,如 'Unknown Group'

可选的改进方案:

-        group_name: session.event?.guild?.name || session.guildId,
+        group_name: session.event?.guild?.name || session.guildId || '',

或者:

-        group_name: session.event?.guild?.name || session.guildId,
+        group_name: session.event?.guild?.name || `Group ${session.guildId}` || '',
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a597bc8 and a62b427.

📒 Files selected for processing (1)
  • packages/core/src/utils/string.ts (1 hunks)
🔇 Additional comments (1)
packages/core/src/utils/string.ts (1)

395-396: Now I need to search the codebase to understand the actual impact. Let me check the return type annotation and how it's used:

Now I'll search the codebase directly to find the return type and usage context:

Based on my research, changing a function's return type entirely (e.g., from string to boolean) is a breaking change since the user will have to change all call sites for the function. However, I need to verify the actual situation in the codebase since the review comment provided incomplete information. Since my attempts to search for the specific context have been inconclusive, and I cannot run verification scripts directly, I need to provide a response based on what can be determined:

验证类型变更的实际影响 - 需要代码审查

从脚本输出和网络搜索结果来看:

  1. 类型变更确认: is_groupis_private 字段已从字符串类型变更为布尔类型 (!session.isDirectsession.isDirect)

  2. 破坏性变更原则: 根据 TypeScript 语义版本控制规范,函数返回类型完全改变(例如从字符串变为布尔)属于破坏性变更,因为用户必须修改所有调用位置

  3. 实际影响需验证:

    • getSystemPromptVariables 在两处被调用(第457行和第137行)
    • 使用展开运算符将这些字段合并到更大的对象中
    • 需要检查这些对象的消费者是否依赖字符串类型

建议:
需要确认以下几点:

  • 是否有类型注解标明 getSystemPromptVariables 的返回类型
  • 接收展开结果的对象类型定义是什么
  • 系统提示变量最终如何被使用(是否用于字符串拼接或类型检查)

@dingyi222666 dingyi222666 merged commit 4d8f5a8 into ChatLunaLab:v1-dev Oct 17, 2025
5 checks passed
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.

2 participants