Skip to content

Conversation

@dingyi222666
Copy link
Member

This PR adds a new tool listing command with pagination support and refactors the reactive chain management system to improve code maintainability and reduce potential memory leaks.

New Features

  • Tool listing command: Added chatluna.tool.list command with pagination support (--page and --limit options) to display all available tools
  • Added localization support for the new tool command in both English and Chinese
  • Implemented dedicated middleware list_all_tool for handling tool list requests with pagination

Bug Fixes

  • Fixed memory extraction validation in long-memory extension to properly check for array type
  • Added null check for tool lookup in platform service to prevent runtime errors
  • Improved error handling when tools are not found

Other Changes

  • Refactored reactive management: Simplified ChatInterface to use ComputedRef for chain wrapper instead of manual watchers
  • Removed manual watch subscriptions in favor of Vue's computed properties for better automatic reactivity
  • Simplified chain initialization logic and lifecycle management
  • Updated embeddings property to return ComputedRef for consistency across the codebase
  • Removed unnecessary debug logging in request_model middleware
  • Bumped koishi-plugin-chatluna-storage-service peer dependency from ^0.0.9 to ^0.0.10

Technical improvements: The reactive refactoring reduces complexity by leveraging Vue's computed properties instead of manual watch subscriptions, making the code more maintainable and reducing potential memory leaks from untracked watchers.

- Add tool.list command to display all available tools
- Support pagination with -p (page) and -l (limit) options, default limit is 5
- Implement alphabetical sorting for consistent tool ordering
- Add comprehensive i18n support for Chinese and English locales
- Add null safety check in tool creation to prevent runtime errors
- Follow the same design pattern as model.list command

The new command allows users to view all registered tools in the system
with proper pagination and sorting, enhancing tool discoverability.
- Refactor ChatInterface to use ComputedRef for chain wrapper
- Remove manual watchers in favor of computed-based reactivity
- Simplify chain initialization logic and lifecycle management
- Update embeddings property to return ComputedRef for consistency
- Remove unnecessary debug logging in request_model middleware
- Improve memory extraction validation in long-memory extension

This change reduces complexity by leveraging Vue's computed properties
instead of manual watch subscriptions, making the code more maintainable
and reducing potential memory leaks from untracked watchers.
Update koishi-plugin-chatluna-storage-service peer dependency
from ^0.0.9 to ^0.0.10 across all packages.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 12, 2025

Caution

Review failed

The pull request is closed.

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

在命令链与全局中间件序列中接入工具相关功能(命令与中间件);新增并注册 chatluna.toolchatluna.tool.list 命令并转发 list_tool 动作;将聊天链与 embeddings 重构为 Vue ComputedRef;在平台服务创建工具时添加显式存在性检查;删除一处调试日志并调整记忆提取重试的中断条件。

Changes

Cohort / File(s) Summary of changes
Tool 命令与中间件接入
packages/core/src/command.ts, packages/core/src/commands/tool.ts, packages/core/src/middleware.ts, packages/core/src/middlewares/model/list_all_tool.ts
在命令链中加入 tool;新增 chatluna.toolchatluna.tool.list(解析参数并通过 chain.receiveCommand 转发 list_tool);在全局中间件序列中插入 list_all_tool;实现 list_all_tool 中间件(分页列出可用工具、设置 context.message 并终止后续处理);导出 apply(ctx, config, chain) 并通过模块增补注册中间件名。
LLM Chat 应用响应式重构
packages/core/src/llm-core/chat/app.ts
将内部 chainembeddings 状态改为 ComputedRef 类型并改为通过 .value 访问;拆分/重命名初始化逻辑(新增 createChatLunaLLMChainWrapper(): Promise<void},对外提供 getChatLunaLLMChainWrapper());调整调用点及清理上下文时对 .value 的引用。
平台工具创建空检查
packages/core/src/llm-core/platform/service.ts
_createTool 明确读取 this._tools[name] 至局部变量并在不存在时抛出错误,随后调用 createTool(params)
日志删除
packages/core/src/middlewares/model/request_model.ts
移除创建请求时的 logger.debug 输出,其他逻辑不变。
长记忆提取循环条件
packages/extension-long-memory/src/utils/chat-history.ts
extractMemoriesFromChat 的重试循环中,将中断条件从“返回非空数组”改为“返回任意数组(包括空数组)”,因此一旦得到数组(即便为空)即停止重试。

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant U as 用户
    participant K as Koishi
    participant CMD as chatluna.tool.list
    participant CH as ChatChain
    participant MW as list_all_tool 中间件
    participant P as Pagination

    U->>K: 输入 `chatluna.tool.list` [--page --limit]
    K->>CMD: 解析命令并执行
    CMD->>CH: chain.receiveCommand(action: "list_tool", page, limit)
    CH->>MW: 触发中间件 `list_all_tool`
    MW->>MW: 收集并按自然数/不区分大小写排序工具列表
    MW->>P: 填充分页、格式化页眉/页脚/页码
    P-->>MW: 返回当前页文本
    MW->>CH: 设置 `context.message` 并结束后续中间件(stop)
    CH-->>K: 返回格式化工具列表消息
    K-->>U: 输出工具列表
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Poem

小兔捣鼓新工具,耳朵竖起听风语。
一声 list_tool 呼唤出,分页排队各显技。
链上脉络值为上,取值依赖点 .value。
噗通跳过调试影,欢跃提交庆功畏。 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed 标题简洁地概述了主要变更:新增工具列表命令和重构响应式链管理,与 PR 的核心内容高度匹配。
Description Check ✅ Passed 描述详细列出了工具列表命令、分页支持、本地化、内存提取修复、空校验、响应式重构、日志移除和依赖升级等改动,与实际代码变更高度相关且信息充足。

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dde14bf and 85b15c2.

⛔ Files ignored due to path filters (26)
  • packages/adapter-azure-openai/package.json is excluded by !**/*.json
  • packages/adapter-claude/package.json is excluded by !**/*.json
  • packages/adapter-deepseek/package.json is excluded by !**/*.json
  • packages/adapter-dify/package.json is excluded by !**/*.json
  • packages/adapter-doubao/package.json is excluded by !**/*.json
  • packages/adapter-gemini/package.json is excluded by !**/*.json
  • packages/adapter-hunyuan/package.json is excluded by !**/*.json
  • packages/adapter-ollama/package.json is excluded by !**/*.json
  • packages/adapter-openai-like/package.json is excluded by !**/*.json
  • packages/adapter-openai/package.json is excluded by !**/*.json
  • packages/adapter-qwen/package.json is excluded by !**/*.json
  • packages/adapter-rwkv/package.json is excluded by !**/*.json
  • packages/adapter-spark/package.json is excluded by !**/*.json
  • packages/adapter-wenxin/package.json is excluded by !**/*.json
  • packages/adapter-zhipu/package.json is excluded by !**/*.json
  • packages/core/package.json is excluded by !**/*.json
  • packages/extension-long-memory/package.json is excluded by !**/*.json
  • packages/extension-mcp/package.json is excluded by !**/*.json
  • packages/extension-tools/package.json is excluded by !**/*.json
  • packages/extension-variable/package.json is excluded by !**/*.json
  • packages/renderer-image/package.json is excluded by !**/*.json
  • packages/service-embeddings/package.json is excluded by !**/*.json
  • packages/service-image/package.json is excluded by !**/*.json
  • packages/service-search/package.json is excluded by !**/*.json
  • packages/service-vector-store/package.json is excluded by !**/*.json
  • packages/shared-adapter/package.json is excluded by !**/*.json
📒 Files selected for processing (1)
  • packages/core/src/middlewares/model/list_all_tool.ts (1 hunks)

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.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @dingyi222666, 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 introduces a new user-facing command for listing available tools with pagination, enhancing the discoverability and management of functionalities within the system. Concurrently, it significantly refactors the internal reactive chain management, transitioning from manual watcher subscriptions to Vue's ComputedRef properties. This technical improvement aims to reduce complexity, improve maintainability, and mitigate potential memory leaks, ensuring a more robust and efficient application core. Several bug fixes and minor improvements are also included to enhance overall stability.

Highlights

  • New Tool Listing Command: Introduced a new chatluna.tool.list command with pagination support (--page and --limit options) to display all available tools, enhancing discoverability.
  • Reactive Chain Management Refactor: The ChatInterface has been refactored to use Vue's ComputedRef for managing the chat chain and embeddings, replacing manual watch subscriptions. This simplifies reactivity, improves maintainability, and reduces potential memory leaks.
  • Localization Support: Added comprehensive English and Chinese localization entries for the new chatluna.tool command and its list subcommand.
  • Dedicated Tool Listing Middleware: A new list_all_tool middleware has been implemented to specifically handle tool list requests, including pagination logic.
  • Improved Tool Lookup Error Handling: A null check was added in the PlatformService for tool lookup, preventing runtime errors when a requested tool is not found and improving overall error handling.
  • Memory Extraction Validation Fix: Corrected the validation logic in the long-memory extension to properly check if extracted memories are an array using Array.isArray().
  • Dependency Update: The koishi-plugin-chatluna-storage-service peer dependency has been bumped from ^0.0.9 to ^0.0.10 across several package.json files.
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.

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

This pull request introduces a useful tool listing command and significantly refactors the reactive chain management, which is a great improvement for maintainability and stability. The implementation of the new feature is solid, and the bug fixes are correct. I've found a couple of potential issues: one is a possible race condition in the new tool listing middleware due to a shared object, and the other is a potential runtime error in the refactored chain management logic where a function might not adhere to its return type. My comments include suggestions to address these points.

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

🧹 Nitpick comments (1)
packages/core/src/middlewares/model/list_all_tool.ts (1)

42-49: LGTM!建议添加错误处理。

分页逻辑正确,中间件定位合理(在 lifecycle-handle_command 之后,lifecycle-request_model 之前)。但建议为分页操作添加错误处理以提高健壮性。

可选改进 - 添加错误处理:

             await pagination.push(tools)
 
-            context.message = await pagination.getFormattedPage(page, limit)
+            try {
+                context.message = await pagination.getFormattedPage(page, limit)
+            } catch (error) {
+                context.message = session.text('.pagination_error')
+                logger.error('Failed to format tool list:', error)
+            }
 
             return ChainMiddlewareRunStatus.STOP
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 94eae2f and dde14bf.

⛔ Files ignored due to path filters (7)
  • package.json is excluded by !**/*.json
  • packages/adapter-gemini/package.json is excluded by !**/*.json
  • packages/core/package.json is excluded by !**/*.json
  • packages/core/src/locales/en-US.yml is excluded by !**/*.yml
  • packages/core/src/locales/zh-CN.yml is excluded by !**/*.yml
  • packages/extension-mcp/package.json is excluded by !**/*.json
  • packages/extension-tools/package.json is excluded by !**/*.json
📒 Files selected for processing (8)
  • packages/core/src/command.ts (2 hunks)
  • packages/core/src/commands/tool.ts (1 hunks)
  • packages/core/src/llm-core/chat/app.ts (7 hunks)
  • packages/core/src/llm-core/platform/service.ts (1 hunks)
  • packages/core/src/middleware.ts (2 hunks)
  • packages/core/src/middlewares/model/list_all_tool.ts (1 hunks)
  • packages/core/src/middlewares/model/request_model.ts (0 hunks)
  • packages/extension-long-memory/src/utils/chat-history.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/core/src/middlewares/model/request_model.ts
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-09-17T00:25:27.195Z
Learnt from: dingyi222666
PR: ChatLunaLab/chatluna#548
File: packages/core/src/llm-core/chat/app.ts:0-0
Timestamp: 2025-09-17T00:25:27.195Z
Learning: 在 ChatInterface 类中,响应式 watch 调用已经通过 ctx.effect() 包装来自动处理清理工作,避免内存泄漏。字段 _chain 和 _embeddings 的类型已更新为可空类型。

Applied to files:

  • packages/core/src/llm-core/chat/app.ts
🧬 Code graph analysis (3)
packages/core/src/commands/tool.ts (3)
packages/core/src/middlewares/model/list_all_tool.ts (1)
  • apply (7-50)
packages/core/src/llm-core/platform/client.ts (1)
  • config (91-93)
packages/core/src/chains/chain.ts (1)
  • ChatChain (14-366)
packages/core/src/middlewares/model/list_all_tool.ts (4)
packages/core/src/commands/tool.ts (1)
  • apply (5-19)
packages/core/src/chains/chain.ts (1)
  • ChatChain (14-366)
packages/core/src/utils/pagination.ts (1)
  • Pagination (1-103)
packages/core/src/command.ts (1)
  • command (14-28)
packages/core/src/llm-core/chat/app.ts (3)
packages/core/src/llm-core/memory/message/database_history.ts (1)
  • KoishiChatMessageHistory (20-405)
packages/core/src/llm-core/utils/count_tokens.ts (1)
  • parseRawModelName (195-205)
packages/core/src/llm-core/memory/langchain/buffer_memory.ts (1)
  • BufferMemory (50-97)
🪛 ESLint
packages/core/src/middlewares/model/list_all_tool.ts

[error] 26-27: Delete ⏎···············

(prettier/prettier)

🪛 GitHub Check: CodeFactor
packages/core/src/middlewares/model/list_all_tool.ts

[warning] 26-26: packages/core/src/middlewares/model/list_all_tool.ts#L26
Delete ⏎··············· (prettier/prettier)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build
  • GitHub Check: lint
🔇 Additional comments (13)
packages/core/src/llm-core/platform/service.ts (1)

316-327: 改进了错误处理,增加了工具存在性检查

在调用 createTool 之前现在会先检查工具是否存在,这样可以避免潜在的空指针异常并提供明确的错误信息。

packages/core/src/llm-core/chat/app.ts (5)

37-38: 已将链和嵌入封装为响应式 ComputedRef

重构将 _chain_embeddings 从直接状态改为 Vue 的 ComputedRef 包装器,这与 Vue 的响应式模式更一致,并简化了生命周期管理。

基于 learnings 提供的上下文,ctx.effect() 包装确保了正确的清理工作。


332-334: 嵌入属性现在返回 ComputedRef

embeddings getter 现在返回 ComputedRef<Embeddings> 而不是直接的 Embeddings 实例,与 _chain 的重构保持一致。

调用者现在需要通过 .value 访问嵌入实例。


381-381: 清理上下文时正确访问链的值

clearChatHistory 方法现在使用 this._chain?.value?.model 来访问链,与新的 ComputedRef 类型一致。


384-424: 嵌入初始化返回计算属性

_initEmbeddings 现在返回 computed(() => {...}) 包装的嵌入实例,实现了响应式的客户端状态跟踪。当客户端引用更新时,嵌入会自动重新计算。


235-322: 确认所有 ComputedRef 实例的 .value 访问
脚本扫描出 13 处 this._chain/this._embeddings 直接访问,多数为非 ComputedRef 字段,仅需在 packages/core/src/llm-core/chat/app.ts(48、236、248、306、333、343)和 packages/service-search/src/provide.ts 对应行验证已使用 .value

packages/core/src/command.ts (1)

11-23: 已添加工具命令到中间件链

新的 tool 中间件已正确导入并添加到执行链中,与现有模式保持一致。

packages/core/src/middleware.ts (1)

27-93: 已添加工具列表中间件到执行链

新的 list_all_tool 中间件已正确导入并插入到中间件数组中,位于 list_all_modellist_all_vectorstore 之间,与相关功能逻辑上保持一致。

packages/extension-long-memory/src/utils/chat-history.ts (1)

141-151: 验证空数组重试逻辑

循环从 extractMemory() 返回的任何数组(包括空数组)时即停止重试,旧逻辑可能只对非空数组停止。请确认:

  • 空数组是否应视为“成功但无内容”直接停止
  • 还是空数组可能是暂时性失败,需要继续重试
packages/core/src/commands/tool.ts (2)

1-4: LGTM!

导入语句正确且必要。


6-8: LGTM!

父命令注册正确,权限级别设置合理。

packages/core/src/middlewares/model/list_all_tool.ts (2)

7-17: LGTM!

Pagination 实例的初始化合理,空的格式字符串稍后在中间件中动态更新。


52-56: LGTM!

模块扩展声明正确,遵循 TypeScript 标准模式。

Update core package version to 1.3.0-alpha.61 and update peer
dependencies across all adapter, extension, and service packages
to reference the new core version.

Updated packages:
- core: 1.3.0-alpha.60 -> 1.3.0-alpha.61
- extension-long-memory: 1.3.0-alpha.3 -> 1.3.0-alpha.4
- extension-mcp: 1.3.0-alpha.7 -> 1.3.0-alpha.8
- extension-tools: 1.3.0-alpha.17 -> 1.3.0-alpha.18
- adapter-gemini: 1.3.0-alpha.18 -> 1.3.0-alpha.19

This release includes the new tool listing command and reactive
chain management refactoring.
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