Skip to content

fix: 修正工具类型可移植性、ToolResult 文档与 ESM 说明 - #3

Merged
echoVic merged 3 commits into
mainfrom
fix/tool-types-and-docs
Jul 2, 2026
Merged

fix: 修正工具类型可移植性、ToolResult 文档与 ESM 说明#3
echoVic merged 3 commits into
mainfrom
fix/tool-types-and-docs

Conversation

@echoVic

@echoVic echoVic commented Jul 2, 2026

Copy link
Copy Markdown
Owner

背景

在用本 SDK 实现一个视频生成 Agent(Node/TS)时,接入过程中遇到几处类型与文档不一致,逐一修复。

改动

commit 1 · 类型可移植性 + 文档修正

  • src/index.ts:从根入口 re-export JsonObject / JsonValue,修复 export const x = defineTool(...) 时的 TS2742ToolDefinition<JsonObject>JsonObject 未在包根导出,推断类型不可移植)。
  • docs/tools.mdToolResult 修正为真实形状(移除源码中不存在的 displayContent,补全成功/失败判别联合,data?: JsonValue / 失败必带 error);示例 kind 改用 ToolKind 枚举而非裸字符串。
  • README.md:标注 ESM-only(require() 会触发 ERR_PACKAGE_PATH_NOT_EXPORTED)。

commit 2 · 自定义工具类型 DX

  • SessionOptions.toolsToolDefinition[]ToolDefinition<any>[]execute 参数位逆变,原类型会拒绝 defineTool<{...}>() 得到的强类型工具,迫使调用方在 execute 内 cast。放宽后带具体 TParams 的工具可直接放入(any 仅作用于数组元素参数位,不泄漏到调用方 execute)。
  • ToolDefinition / defineTool:新增可选 TData extends JsonValue 泛型并透传到 execute 返回的 ToolResult<TData>,让 paramsdata 精确类型化、无需 cast。保留 JsonValue 约束(data 会序列化落盘),把序列化风险前移为定义期类型错误,而非放宽成 unknown
  • docs/tools.md:补充 data 需可 JSON 序列化的说明与 defineTool<TParams, TData> 用法示例。

验证

  • pnpm run build:通过
  • pnpm test:1059 passed / 62 skipped,无回退
  • 真实回归:用本地构建版覆盖到一个实际的 Agent 后端工程,确认 defineTool<{...}>params 强类型可用、带 TParams 的工具可直接进 tools 数组,均无需 cast,typecheck 通过。
  • dist 声明确认导出 JsonObject / JsonValueToolDefinition<TParams, TData> 生效。

A detailed high-level summary could not be generated for this review. Here is an overview derived from the analyzed file changes:

  • README.md: ## AI-generated summary of changes
  • docs/tools.md: ## AI-generated summary of changes
  • src/index.ts: ## AI-generated summary of changes

- 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
@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 35c973b7-20d4-411a-aac6-0dc58dbce637

📥 Commits

Reviewing files that changed from the base of the PR and between f115f98 and 0896ccb.

📒 Files selected for processing (10)
  • .github/workflows/ci.yml
  • docs/tools.md
  • package.json
  • scripts/__tests__/node-version-policy.test.ts
  • src/__tests__/deepseek-agent.live.test.ts
  • src/services/__tests__/deepseek-deep.live.test.ts
  • src/session/__tests__/SessionModelConfig.test.ts
  • src/session/types.ts
  • src/tools/core/createTool.ts
  • src/tools/types/ToolDefinition.ts
👮 Files not reviewed due to content moderation or server errors (10)
  • src/services/tests/deepseek-deep.live.test.ts
  • src/tests/deepseek-agent.live.test.ts
  • src/tools/core/createTool.ts
  • src/session/types.ts
  • src/tools/types/ToolDefinition.ts
  • docs/tools.md
  • package.json
  • .github/workflows/ci.yml
  • src/session/tests/SessionModelConfig.test.ts
  • scripts/tests/node-version-policy.test.ts

📝 Walkthrough
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed 标题准确概括了工具类型可移植性、ToolResult 文档更新和 ESM 说明这三项主要改动。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/tool-types-and-docs

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.

echoVic and others added 2 commits July 2, 2026 16:24
- 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 序列化的说明与泛型用法示例。
@echoVic
echoVic merged commit b852f2a into main Jul 2, 2026
1 of 2 checks passed
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 1.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant