-
-
Notifications
You must be signed in to change notification settings - Fork 41
refactor(core,adapter-dify): improve variable handling and remove redundant additional_kwargs #601
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
Conversation
Summary of ChangesHello @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
Using Gemini Code AssistThe 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
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 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
|
|
Note Other AI code review bot(s) detectedCodeRabbit 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 在多个链和模型层引入并传播了 Changes
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
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.
Code Review
本次 PR 升级了 Dify 适配器,为请求增加了 user_id、bot_id 和 group_id 等更多上下文信息,这是一个很好的改进。
在 packages/adapter-dify/src/requester.ts 文件中,我注意到 _agentStream 方法中的代码存在重复,建议进行重构以提高可读性。
此外,似乎只有 _agentStream 方法被更新了。_workflowStream 方法可能也需要进行类似的更新,以确保行为一致性。目前它仍在为 chatluna_user_id 使用旧的逻辑,并且缺少新的 bot_id 和 group_id 字段。
…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.
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
♻️ 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_id、bot_id、group_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
⛔ Files ignored due to path filters (1)
tsconfig.jsonis 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接收到的params中variables可能为undefined- 框架层无验证机制,直接将
user_id、bot_id、group_id、user以undefined值发送给 Dify API- 若这些字段为 Dify API 必需,可能导致 API 调用失败
建议:
- 在调用
completionStream前确认所有代码路径均正确填充params.variables- 或在
completionStreamInternal内添加验证逻辑,对缺失的必需字段抛出错误- 或为缺失字段提供合理的默认值
packages/core/src/llm-core/platform/model.ts (3)
78-83: 新增的variables和variables_hide选项在
ChatLunaModelCallOptions中添加这两个字段扩展了模型调用的参数能力,支持传递额外的上下文变量。
124-141:variables_hide被正确添加到callKeys中将
variables_hide添加到callKeys数组确保该选项在模型调用时被正确识别和处理。这与 chain 层的实现相呼应。
178-178:variables_hide到variables的映射逻辑
invocationParams将options?.['variables_hide']映射到variables字段,并提供{}作为默认值。这形成了一个清晰的数据流:chain 层设置variables_hide→ model 层读取并映射为variables→ requester 层使用variables。建议在代码或文档中说明
variables和variables_hide的区别和用途,以便未来的维护者理解这种设计。
…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.
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/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
📒 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:
验证类型变更的实际影响 - 需要代码审查
从脚本输出和网络搜索结果来看:
类型变更确认:
is_group和is_private字段已从字符串类型变更为布尔类型 (!session.isDirect和session.isDirect)破坏性变更原则: 根据 TypeScript 语义版本控制规范,函数返回类型完全改变(例如从字符串变为布尔)属于破坏性变更,因为用户必须修改所有调用位置
实际影响需验证:
getSystemPromptVariables在两处被调用(第457行和第137行)- 使用展开运算符将这些字段合并到更大的对象中
- 需要检查这些对象的消费者是否依赖字符串类型
建议:
需要确认以下几点:
- 是否有类型注解标明
getSystemPromptVariables的返回类型- 接收展开结果的对象类型定义是什么
- 系统提示变量最终如何被使用(是否用于字符串拼接或类型检查)
谢谢dingyi喵~麻烦了!