Skip to content

Refactor: 统一工具 schema 注入并提升工具调用 JSON 捕获稳定性#59

Closed
Cai-Tang-www wants to merge 9 commits into1024XEngineer:mainfrom
Cai-Tang-www:main
Closed

Refactor: 统一工具 schema 注入并提升工具调用 JSON 捕获稳定性#59
Cai-Tang-www wants to merge 9 commits into1024XEngineer:mainfrom
Cai-Tang-www:main

Conversation

@Cai-Tang-www
Copy link
Copy Markdown
Collaborator

@Cai-Tang-www Cai-Tang-www commented Mar 25, 2026

背景

这是 #45 的后续收尾,主要解决两类遗留问题:

  1. configs/persona.txt 中仍保留了大量工具参数说明,和工具 schema 定义重复,容易产生双重维护和不一致。
  2. TUI 侧对工具调用 JSON 的捕获仍然偏脆弱,当模型输出中混入解释文本、<think> 块或代码围栏时,工具调用识别不稳定。

本次改动

1. 统一工具信息来源,注入 schema 上下文

  • 清理 configs/persona.txtconfigs/persona.txt.example 中重复的工具参数描述
  • 保留英文行为约束文档,并明确默认使用中文回复
  • 为工具参数 schema 补充 default / enum 等结构化字段
  • 新增 tool schema prompt 渲染逻辑,运行时从各工具 Definition() 统一生成模型可见的 schema 上下文
  • 在 server 和 TUI 本地聊天链路中注入 tool schema,避免继续依赖 persona 中的硬编码工具说明
  • 更新 README 与 TUI 架构文档,说明工具信息维护入口已迁移到 schema 定义

2. 优化工具调用 JSON 捕获器稳定性

  • 重构 TUI 流结束后的工具调用识别逻辑
  • 不再要求 assistant 最后一条消息必须是“纯 JSON”
  • 支持从混合输出中提取最后一个合法工具调用对象,包括:
    • <think> ... </think> + 自然语言 + JSON
    • Markdown fenced JSON
    • 前置说明文本后跟工具调用 JSON
  • 对被执行的工具调用做展示层清洗:
    • 去掉 <think>
    • 去掉原始工具 JSON
    • 去掉空代码围栏
  • 当 JSON 后仍跟随自然语言正文时,不触发工具执行,避免误判示例内容
  • 补充对应回归测试,覆盖混合输出、纯 JSON、围栏 JSON 和误触发场景

影响

  • 工具参数的唯一来源现在是各工具的 schema 定义,后续新增或修改参数时不需要再同步维护 persona 文本
  • 模型在工具调用场景下可以获得更完整、更一致的参数约束信息
  • 工具调用识别对真实模型输出更稳,减少因混合文本导致的漏抓或误抓

Close #51

验证

go test ./internal/tui/core ./internal/tui/services
go test ./cmd/tui ./internal/tui/... ./internal/server/infra/tools ./internal/server/service ./cmd/server

@Cai-Tang-www Cai-Tang-www changed the title feat: 统一工具 schema 注入并提升工具调用 JSON 捕获稳定性 Refactor: 统一工具 schema 注入并提升工具调用 JSON 捕获稳定性 Mar 25, 2026
Copy link
Copy Markdown
Collaborator

@minorcell minorcell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cai-Tang-www 工具哪里可以再调研一下,你们当前的参数传递方式是不对的哦~

Comment thread docs/tui-refined-architecture.md
Comment thread internal/server/infra/tools/schema_prompt.go Outdated
@Cai-Tang-www
Copy link
Copy Markdown
Collaborator Author

Cai-Tang-www commented Mar 25, 2026

这次是对工具系统的大改,把工具调用切到标准协议并彻底移除旧的 JSON 内联/捕获流程。
现在工具定义统一从 schema 注入 tools 参数,provider 按 tool_calls 事件流式回传;
TUI 侧改为接收 tool_call 事件并携带 tool_call_id 执行与回填,persona 和文档也同步到“schema 单一来源”

Copy link
Copy Markdown
Collaborator

@minorcell minorcell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cai-Tang-www 系统提示词也是一个重要的决策部分;

这里除了文档的评论之外,还有一点重要的是:你们做的是一个agent,并不是什么聊天Bot;

所以这里的描述:“角色提示词” 是不是不太合适?可以参考一下 codex 是如何做这部分的设计的:https://github.com/openai/codex/blob/main/codex-rs/core/prompt.md

Comment thread configs/persona.txt.example Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有点奇怪,为什么系统提示词是可修改的?
系统提示词本身是 agent 角色定义的重要组成部分,如果把这部分修改权直接暴露给用户,可能会带来不可预期的行为和结果。

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一开始可能为了方便人设写外面,我看看怎么改善一下

phantom5099 added a commit that referenced this pull request Apr 24, 2026
fix(security): reject backslash traversal in workspace path resolution
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.

Fix: 添加模型 Tool schema 上下文并清理人设中重复的工具说明

2 participants