94 lines
3.6 KiB
JavaScript
94 lines
3.6 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Snapshot the selected tools from the same Pi package and extensions used by
|
|
* the rollout. Pi's events do not include tool definitions.
|
|
*
|
|
* node agent/dump_tools.js "read,edit,write,bash,grep,claim,oe_doc"
|
|
*/
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const { pathToFileURL } = require("node:url");
|
|
|
|
const DEFAULT_TOOLS = ["read", "edit", "write", "bash", "grep", "claim", "oe_doc"];
|
|
const EXTENSIONS = { claim: "claim-card.ts", oe_doc: "oe-doc.ts" };
|
|
|
|
function piRoot() {
|
|
// The configured Node/Pi live in one toolchain. Do not depend on npm being
|
|
// present in the caller's PATH (prepare() runs before agent_env()).
|
|
const bin = path.dirname(process.execPath);
|
|
const candidates = [path.resolve(bin, "../lib/node_modules/@mariozechner/pi-coding-agent")];
|
|
try {
|
|
candidates.unshift(path.resolve(path.dirname(fs.realpathSync(path.join(bin, "pi"))), ".."));
|
|
} catch { /* A normal global package can still exist without the CLI symlink. */ }
|
|
for (const root of candidates) {
|
|
try {
|
|
if (JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8")).name === "@mariozechner/pi-coding-agent") {
|
|
return root;
|
|
}
|
|
} catch { /* Try the next installation location. */ }
|
|
}
|
|
throw new Error(`Cannot find pi-coding-agent beside ${process.execPath}`);
|
|
}
|
|
|
|
function toFunction(tool) {
|
|
return {
|
|
type: "function",
|
|
function: {
|
|
name: tool.name,
|
|
description: tool.description || "",
|
|
// Preserve nested constraints/defaults and TypeBox unions exactly.
|
|
parameters: tool.parameters,
|
|
},
|
|
};
|
|
}
|
|
|
|
async function dumpTools(whitelist = DEFAULT_TOOLS) {
|
|
const root = piRoot();
|
|
const pi = await import(pathToFileURL(path.join(root, "dist/index.js")).href);
|
|
const selected = [...new Set(whitelist)];
|
|
const definitions = new Map();
|
|
const extensionNames = selected.filter((name) => Object.hasOwn(EXTENSIONS, name));
|
|
if (extensionNames.length) {
|
|
// Use Pi's loader so TypeScript and TypeBox resolution match the real agent.
|
|
// Loading registers tools/hooks only; it never executes a tool or agent hook.
|
|
const { loadExtensions } = await import(pathToFileURL(path.join(root, "dist/core/extensions/loader.js")).href);
|
|
const loaded = await loadExtensions(extensionNames.map((name) => path.join(__dirname, "extensions", EXTENSIONS[name])), process.cwd());
|
|
if (loaded.errors.length) {
|
|
throw new Error(`Cannot load tool extensions: ${JSON.stringify(loaded.errors)}`);
|
|
}
|
|
for (const extension of loaded.extensions) {
|
|
for (const [name, registered] of extension.tools) {
|
|
definitions.set(name, registered.definition);
|
|
}
|
|
}
|
|
}
|
|
const result = selected.map((name) => {
|
|
let tool = definitions.get(name);
|
|
if (!tool && !Object.hasOwn(EXTENSIONS, name)) {
|
|
const factory = pi[`create${name[0]?.toUpperCase()}${name.slice(1)}Tool`];
|
|
if (typeof factory === "function") tool = factory(process.cwd());
|
|
}
|
|
if (!tool || tool.name !== name || !tool.parameters) {
|
|
throw new Error(`Cannot snapshot selected tool: ${name}`);
|
|
}
|
|
return toFunction(tool);
|
|
});
|
|
result.sort((a, b) => a.function.name.localeCompare(b.function.name));
|
|
return result;
|
|
}
|
|
|
|
async function main() {
|
|
const whitelist = process.argv[2] === undefined
|
|
? DEFAULT_TOOLS
|
|
: process.argv[2].split(",").map((name) => name.trim()).filter(Boolean);
|
|
process.stdout.write(JSON.stringify(await dumpTools(whitelist), null, 2) + "\n");
|
|
}
|
|
|
|
module.exports = { dumpTools, toFunction, piRoot, main };
|
|
if (require.main === module) {
|
|
main().catch((error) => {
|
|
process.stderr.write(`${error.stack || error}\n`);
|
|
process.exitCode = 1;
|
|
});
|
|
}
|