mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 10:05:14 +00:00
fix(coding-agent): support dynamic tool registration and tool prompt snippets closes #1720
This commit is contained in:
parent
ca5510158d
commit
bc2fa8d6d0
12 changed files with 285 additions and 47 deletions
|
|
@ -33,6 +33,7 @@ cp permission-gate.ts ~/.pi/agent/extensions/
|
|||
| `question.ts` | Demonstrates `ctx.ui.select()` for asking the user questions with custom UI |
|
||||
| `questionnaire.ts` | Multi-question input with tab bar navigation between questions |
|
||||
| `tool-override.ts` | Override built-in tools (e.g., add logging/access control to `read`) |
|
||||
| `dynamic-tools.ts` | Register tools after startup (`session_start`) and at runtime via command |
|
||||
| `built-in-tool-renderer.ts` | Custom compact rendering for built-in tools (read, bash, edit, write) while keeping original behavior |
|
||||
| `minimal-mode.ts` | Override built-in tool rendering for minimal display (only tool calls, no output in collapsed mode) |
|
||||
| `truncated-tool.ts` | Wraps ripgrep with proper output truncation (50KB/2000 lines) |
|
||||
|
|
|
|||
73
packages/coding-agent/examples/extensions/dynamic-tools.ts
Normal file
73
packages/coding-agent/examples/extensions/dynamic-tools.ts
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/**
|
||||
* Dynamic Tools Extension
|
||||
*
|
||||
* Demonstrates registering tools after session initialization.
|
||||
*
|
||||
* - Registers one tool during session_start
|
||||
* - Registers additional tools at runtime via /add-echo-tool <name>
|
||||
*/
|
||||
|
||||
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
||||
import { Type } from "@sinclair/typebox";
|
||||
|
||||
const ECHO_PARAMS = Type.Object({
|
||||
message: Type.String({ description: "Message to echo" }),
|
||||
});
|
||||
|
||||
function normalizeToolName(input: string): string | undefined {
|
||||
const trimmed = input.trim().toLowerCase();
|
||||
if (!trimmed) return undefined;
|
||||
if (!/^[a-z0-9_]+$/.test(trimmed)) return undefined;
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
export default function dynamicToolsExtension(pi: ExtensionAPI) {
|
||||
const registeredToolNames = new Set<string>();
|
||||
|
||||
const registerEchoTool = (name: string, label: string, prefix: string): boolean => {
|
||||
if (registeredToolNames.has(name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
registeredToolNames.add(name);
|
||||
pi.registerTool({
|
||||
name,
|
||||
label,
|
||||
description: `Echo a message with prefix: ${prefix}`,
|
||||
promptSnippet: `Echo back user-provided text with ${prefix.trim()} prefix`,
|
||||
parameters: ECHO_PARAMS,
|
||||
async execute(_toolCallId, params) {
|
||||
return {
|
||||
content: [{ type: "text", text: `${prefix}${params.message}` }],
|
||||
details: { tool: name, prefix },
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
pi.on("session_start", (_event, ctx) => {
|
||||
registerEchoTool("echo_session", "Echo Session", "[session] ");
|
||||
ctx.ui.notify("Registered dynamic tool: echo_session", "info");
|
||||
});
|
||||
|
||||
pi.registerCommand("add-echo-tool", {
|
||||
description: "Register a new echo tool dynamically: /add-echo-tool <tool_name>",
|
||||
handler: async (args, ctx) => {
|
||||
const toolName = normalizeToolName(args);
|
||||
if (!toolName) {
|
||||
ctx.ui.notify("Usage: /add-echo-tool <tool_name> (lowercase, numbers, underscores)", "warning");
|
||||
return;
|
||||
}
|
||||
|
||||
const created = registerEchoTool(toolName, `Echo ${toolName}`, `[${toolName}] `);
|
||||
if (!created) {
|
||||
ctx.ui.notify(`Tool already registered: ${toolName}`, "warning");
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.ui.notify(`Registered dynamic tool: ${toolName}`, "info");
|
||||
},
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue