data-gen-bpu/agent/dump_tools.js

95 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* 把这次 rollout 里 agent 真正看到的工具定义 dump 成 OpenAI function schema。
*
* node agent/dump_tools.js "read,edit,write,bash,grep" > <rollout>/agent/tools.json
*
* 为什么要 dump 而不是手写:pi 的 events.jsonl 里**没有**工具定义事件(实测 0.73.1 只有
* session、agent_start、turn_start/end、message_start/update/end、tool_execution_start/update/end、agent_end),导出成 SFT 样本时
* `tools` 字段就填不出来。手写的话工具升级一次就和真实上下文对不上,等于给模型看它当时没看到的东西。
* 内置工具从 pi 包的 create*Tool 工厂取(权威来源);我们自己注册的 claim / oe_doc 由本文件声明,
* 因为定义本来就在 bench/agent/extensions/ 里。
*/
const path = require("node:path");
const { execSync } = require("node:child_process");
function piRoot() {
const root = execSync("npm root -g", { encoding: "utf8" }).trim();
return path.join(root, "@mariozechner/pi-coding-agent");
}
/** TypeBox/JSON-Schema → OpenAI function schema */
function toFunction(t) {
const p = t.parameters || { type: "object", properties: {}, required: [] };
return {
type: "function",
function: {
name: t.name,
description: (t.description || "").trim(),
parameters: {
type: "object",
title: "parameters",
properties: p.properties || {},
required: p.required || [],
},
},
};
}
// 我们自己的 extension 注册的工具(bench/agent/extensions/claim-card.ts、oe-doc.ts)
const BENCH_TOOLS = [
{
name: "claim",
description: "结束前必须调用一次。汇报你认为的最终状态。会与 harness 独立核验的 verdict 比对;claim=PASS 而 verdict=FAIL 是最重扣分项。",
parameters: {
type: "object",
properties: {
terminal: { type: "string", enum: ["PASS", "FAIL", "BLOCKED"], description: "最终状态" },
stage: { type: "string", description: "失败/阻塞发生的阶段,如 05-compile" },
error_class: { type: "string", enum: ["unsupported_op", "shape_infer", "export_fail", "calib_nan", "verifier_mismatch", "unknown"] },
summary: { type: "string", description: "你做了什么、为什么是这个结论,3–8 句" },
artifacts: { type: "array", items: { type: "string" }, description: "workspace 内的关键产物相对路径" },
metrics_seen: { type: "object", description: "你从 bench stage eval 输出里看到的数字(不是估计的)" },
},
required: ["terminal", "summary"],
},
},
{
name: "oe_doc",
description: "查 OpenExplorer / hbdk / horizon_tc_ui 文档。输入关键词或报错片段,返回相关文档段落。",
parameters: {
type: "object",
properties: { query: { type: "string" }, max_chars: { type: "number" } },
required: ["query"],
},
},
];
function main() {
const whitelist = (process.argv[2] || "").split(",").map((s) => s.trim()).filter(Boolean);
const m = require(path.join(piRoot(), "dist", "index.js"));
const out = [];
const seen = new Set();
for (const key of Object.keys(m).filter((k) => /^create[A-Za-z]+Tool$/.test(k))) {
let t;
try {
t = m[key]({ cwd: process.cwd() });
} catch {
continue;
}
if (!t || !t.name || seen.has(t.name)) continue;
if (whitelist.length && !whitelist.includes(t.name)) continue;
seen.add(t.name);
out.push(toFunction(t));
}
for (const t of BENCH_TOOLS) {
if (seen.has(t.name)) continue;
seen.add(t.name);
out.push(toFunction(t));
}
out.sort((a, b) => a.function.name.localeCompare(b.function.name));
process.stdout.write(JSON.stringify(out, null, 2) + "\n");
}
main();