|
| 1 | +// @ts-nocheck |
| 2 | +/** |
| 3 | + * Remote-tools dispatcher extension loaded into every session Pi process via |
| 4 | + * `--extension`. |
| 5 | + * |
| 6 | + * Like the orchestrator/memory extensions, this is authored against Pi's |
| 7 | + * extension runtime (it imports modules Pi resolves when loading extensions, |
| 8 | + * e.g. `typebox`), not this repo's `node_modules`. It is excluded from our |
| 9 | + * type-check (`@ts-nocheck`) and never imported by the server — only passed as a |
| 10 | + * path to the Pi subprocess. |
| 11 | + * |
| 12 | + * A "remote tool" is a named async function hosted by a connected remote |
| 13 | + * environment (e.g. a browser embed) and invoked over the `/remote-env` |
| 14 | + * WebSocket — no second LLM, no browser sub-agent. Pi freezes its `--tools` |
| 15 | + * allowlist at spawn, and the host usually connects after Prime is already |
| 16 | + * running, so this ships a stable two-tool dispatcher instead of first-class |
| 17 | + * per-host tool names: |
| 18 | + * - `list_remote_tools` — the current catalog (changes as hosts connect/leave). |
| 19 | + * - `call_remote_tool` — invoke one by name with JSON arguments. |
| 20 | + * |
| 21 | + * Both are thin clients over this server's internal remote-tools API; the |
| 22 | + * gateway owns the socket and routes the call to the session's environment. |
| 23 | + */ |
| 24 | + |
| 25 | +import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; |
| 26 | +import { Type } from "typebox"; |
| 27 | + |
| 28 | +const SESSION_ID = process.env.TANGENT_SESSION_ID ?? ""; |
| 29 | +const AGENT_ID = process.env.TANGENT_AGENT_ID ?? ""; |
| 30 | +const INTERNAL_URL = process.env.TANGENT_INTERNAL_URL ?? ""; |
| 31 | +const INTERNAL_TOKEN = process.env.TANGENT_INTERNAL_TOKEN ?? ""; |
| 32 | + |
| 33 | +async function callApi( |
| 34 | + method: "GET" | "POST", |
| 35 | + endpoint: string, |
| 36 | + body?: Record<string, unknown>, |
| 37 | + query?: Record<string, string>, |
| 38 | +): Promise<unknown> { |
| 39 | + const url = new URL(`${INTERNAL_URL}/internal/remote-tools/${endpoint}`); |
| 40 | + for (const [key, value] of Object.entries(query ?? {})) { |
| 41 | + url.searchParams.set(key, value); |
| 42 | + } |
| 43 | + |
| 44 | + const response = await fetch(url, { |
| 45 | + method, |
| 46 | + headers: { |
| 47 | + "content-type": "application/json", |
| 48 | + authorization: `Bearer ${INTERNAL_TOKEN}`, |
| 49 | + }, |
| 50 | + body: body ? JSON.stringify(body) : undefined, |
| 51 | + }); |
| 52 | + |
| 53 | + if (!response.ok) { |
| 54 | + const text = await response.text().catch(() => ""); |
| 55 | + throw new Error( |
| 56 | + `internal API ${endpoint} failed (${response.status}): ${text}`, |
| 57 | + ); |
| 58 | + } |
| 59 | + return response.json(); |
| 60 | +} |
| 61 | + |
| 62 | +function textResult(text: string) { |
| 63 | + return { content: [{ type: "text", text }], details: {} }; |
| 64 | +} |
| 65 | + |
| 66 | +/** Renders one JSON-serializable tool result as readable text for the agent. */ |
| 67 | +function renderResult(value: unknown): string { |
| 68 | + if (value === undefined || value === null) return "(no result)"; |
| 69 | + if (typeof value === "string") return value; |
| 70 | + return JSON.stringify(value, null, 2); |
| 71 | +} |
| 72 | + |
| 73 | +export default function (pi: ExtensionAPI) { |
| 74 | + pi.registerTool({ |
| 75 | + name: "list_remote_tools", |
| 76 | + label: "List Remote Tools", |
| 77 | + description: |
| 78 | + "List the tools the connected host environment currently offers (e.g. a " + |
| 79 | + "browser embedding this session). The catalog is dynamic: it appears when " + |
| 80 | + "a host connects and is empty when none is. Call this before " + |
| 81 | + "call_remote_tool to see what is available and each tool's arguments.", |
| 82 | + promptSnippet: "List the RPC tools the connected host offers", |
| 83 | + parameters: Type.Object({}), |
| 84 | + async execute() { |
| 85 | + const data = (await callApi("GET", "list", undefined, { |
| 86 | + sessionId: SESSION_ID, |
| 87 | + })) as { |
| 88 | + tools: Array<{ |
| 89 | + name: string; |
| 90 | + description: string; |
| 91 | + inputSchema: unknown; |
| 92 | + }>; |
| 93 | + }; |
| 94 | + |
| 95 | + if (!data.tools.length) { |
| 96 | + return textResult( |
| 97 | + "No host environment is connected, so there are no remote tools right now.", |
| 98 | + ); |
| 99 | + } |
| 100 | + const lines = data.tools.map( |
| 101 | + (tool) => |
| 102 | + `- ${tool.name}: ${tool.description}\n arguments: ${JSON.stringify(tool.inputSchema)}`, |
| 103 | + ); |
| 104 | + return textResult(lines.join("\n")); |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + pi.registerTool({ |
| 109 | + name: "call_remote_tool", |
| 110 | + label: "Call Remote Tool", |
| 111 | + description: |
| 112 | + "Invoke one tool offered by the connected host environment by name, " + |
| 113 | + "passing its arguments as a JSON object. Call list_remote_tools first to " + |
| 114 | + "learn the available names and each tool's argument schema. Returns the " + |
| 115 | + "host's result. Fails clearly if no host is connected or the name is " + |
| 116 | + "unknown.", |
| 117 | + promptSnippet: "Invoke a host-provided remote tool by name", |
| 118 | + parameters: Type.Object({ |
| 119 | + name: Type.String({ |
| 120 | + description: "The tool name from list_remote_tools.", |
| 121 | + }), |
| 122 | + arguments: Type.Optional( |
| 123 | + Type.Unknown({ |
| 124 | + description: |
| 125 | + "The tool's arguments as a JSON object matching its inputSchema.", |
| 126 | + }), |
| 127 | + ), |
| 128 | + }), |
| 129 | + async execute(_toolCallId, params) { |
| 130 | + const data = (await callApi("POST", "call", { |
| 131 | + sessionId: SESSION_ID, |
| 132 | + agentId: AGENT_ID, |
| 133 | + name: params.name, |
| 134 | + arguments: params.arguments ?? {}, |
| 135 | + })) as { ok: boolean; result?: unknown; error?: string }; |
| 136 | + |
| 137 | + if (!data.ok) { |
| 138 | + return textResult( |
| 139 | + `Remote tool "${params.name}" failed: ${data.error ?? "unknown error"}`, |
| 140 | + ); |
| 141 | + } |
| 142 | + return textResult(renderResult(data.result)); |
| 143 | + }, |
| 144 | + }); |
| 145 | +} |
0 commit comments