fix: 修正工具类型可移植性、ToolResult 文档与 ESM 说明 - #3
Merged
Conversation
- src/index.ts: 从根入口 re-export JsonObject/JsonValue,修复使用 defineTool 时 "inferred type cannot be named"(TS2742)的可移植性报错 - docs/tools.md: 修正 ToolResult 形状(移除不存在的 displayContent,补 data/error 与判别联合);示例改用 ToolKind 枚举而非裸字符串字面量 - README.md: 标注 ESM-only,避免 require() 触发 ERR_PACKAGE_PATH_NOT_EXPORTED
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (10)
👮 Files not reviewed due to content moderation or server errors (10)
📝 Walkthrough🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 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 |
- SessionOptions.tools 改为 ToolDefinition<any>[]:execute 参数位逆变,原
ToolDefinition<JsonObject>[] 会拒绝 defineTool<{...}>() 的强类型工具,
迫使调用方在 execute 内 cast。放宽后带具体 TParams 的工具可直接放入。
- ToolDefinition / defineTool 增加可选 TData extends JsonValue 泛型并透传到
execute 返回的 ToolResult<TData>:让 params 与 data 精确类型化,无需 cast。
保留 JsonValue 约束(data 会序列化落盘),把序列化风险前移为定义期类型错误。
- docs/tools.md:补充 data 需可 JSON 序列化的说明与泛型用法示例。
|
🎉 This PR is included in version 1.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.
背景
在用本 SDK 实现一个视频生成 Agent(Node/TS)时,接入过程中遇到几处类型与文档不一致,逐一修复。
改动
commit 1 · 类型可移植性 + 文档修正
JsonObject/JsonValue,修复export const x = defineTool(...)时的TS2742(ToolDefinition<JsonObject>中JsonObject未在包根导出,推断类型不可移植)。ToolResult修正为真实形状(移除源码中不存在的displayContent,补全成功/失败判别联合,data?: JsonValue/ 失败必带error);示例kind改用ToolKind枚举而非裸字符串。require()会触发ERR_PACKAGE_PATH_NOT_EXPORTED)。commit 2 · 自定义工具类型 DX
ToolDefinition[]→ToolDefinition<any>[]。execute参数位逆变,原类型会拒绝defineTool<{...}>()得到的强类型工具,迫使调用方在 execute 内 cast。放宽后带具体TParams的工具可直接放入(any仅作用于数组元素参数位,不泄漏到调用方 execute)。TData extends JsonValue泛型并透传到execute返回的ToolResult<TData>,让params与data精确类型化、无需 cast。保留JsonValue约束(data会序列化落盘),把序列化风险前移为定义期类型错误,而非放宽成unknown。data需可 JSON 序列化的说明与defineTool<TParams, TData>用法示例。验证
defineTool<{...}>的params强类型可用、带TParams的工具可直接进tools数组,均无需 cast,typecheck 通过。JsonObject/JsonValue、ToolDefinition<TParams, TData>生效。A detailed high-level summary could not be generated for this review. Here is an overview derived from the analyzed file changes: