feat(tools): 引入 ToolRegistry 工具注册系统与文件操作工具#4
Merged
Conversation
…on for tool management
- 新增 ListDirectoryTool:列出目录内容(只读、可并发) - 新增 ReadFileTool:读取文件内容,支持 maxResultChars 截断(只读、可并发) - 新增 WriteFileTool:写入文件内容(读写、串行) - 重构 CalculatorTool / WeatherTool:迁移至 ToolDefinition 接口,补充 name、isConcurrencySafe、isReadOnly 字段 - 新增 tools.ts:导出 allTools 工具列表 - 更新 tools/index.ts:统一重导出所有工具
- agentLoop 参数由 ToolSet + BudgetState 改为 ToolRegistry,调用时通过 registry.toAISDKFormat() 转换 - 移除导出的 BudgetState 接口,改为内部常量 TOKEN_BUDGET(50000),仅在用量超过 90% 时打印日志 - index.ts:用 ToolRegistry + allTools 替换手动构造的 ToolSet,启动时打印已注册工具及并发/只读属性
- 新增三个状态变量:exclusiveLock、concurrentCount、waitQueue - acquireConcurrent / releaseConcurrent:共享锁,只读工具可同时持有 - acquireExclusive / releaseExclusive:独占锁,写工具必须等所有共享锁释放 - drainQueue:锁释放后统一唤醒等待队列,让各方重新竞争 - toAISDKFormat 中根据 isConcurrencySafe 自动选择锁类型,锁在 finally 中释放以防泄漏
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
引入统一的工具注册系统(
ToolRegistry),新增文件操作工具,并为并发场景实现读写锁保障。变更内容
新增
ToolRegistry:统一管理工具的注册、查询与 AI SDK 格式转换;内置读写锁,确保并发安全ToolDefinition接口:扩展isConcurrencySafe、isReadOnly、maxResultChars元数据ReadFileTool:读取文件内容,支持maxResultChars自动截断WriteFileTool:写入文件内容(串行,独占锁)ListDirectoryTool:列出目录内容(只读,可并发)tools.ts:导出allTools工具列表,统一注册入口acquireConcurrent/acquireExclusive实现共享/独占语义,finally中释放防泄漏重构
CalculatorTool/WeatherTool迁移至ToolDefinition结构,补充name、并发属性agentLoop参数由ToolSet + BudgetState改为ToolRegistry,Token 预算内化为常量TOKEN_BUDGET(50000),仅在用量 >90% 时打印日志index.ts用ToolRegistry + allTools替换手动构造的工具集,启动时打印已注册工具及属性Mock Model(v0.4.0)
测试并发)、文件工具意图检测、截断验证(测试截断)hasToolResults逻辑,只检查最近一轮 tool 消息USAGE,移除makeUsage测试
pnpm run lint✅pnpm run format:check✅