/** * Stub generator — produces a TypeScript module the script imports as * `memi_tools`. Each tool in the per-call allowlist becomes a typed * async function that round-trips a request to the parent over the * Unix socket and returns the result. * * Usage from parent: * const stub = generateMemiToolsStub({ * socketEnvVar: "MEMI_TOOLS_SOCKET", * tools: [ * { name: "Read", argsType: "{ path: string }", resultType: "string" }, * { name: "Write", argsType: "{ path: string; content: string }", resultType: "void" }, * { name: "Bash", argsType: "{ command: string; timeoutMs?: number }", * resultType: "{ stdout: string; stderr: string; code: number }" }, * ], * }); * await writeFile("/tmp/memi_tools.ts", stub); * * Then the user script: * import { Read, Write, Bash, log, exit } from "./memi_tools.ts"; * const files = await Bash({ command: "find . -name '*.ts'" }); * for (const f of files.stdout.split("\n").filter(Boolean)) { * log("info", `processing ${f}`); * const content = await Read({ path: f }); * await Write({ path: f, content: content.toUpperCase() }); * } * exit(true, { fileCount: files.stdout.split("\n").length }); * * The stub uses a single Unix-socket connection (created lazily on first * call) and multiplexes tool calls with monotonic ids. log() fires a * one-way log message; exit() sends the final result and closes the * socket. Calling exit() more than once is a no-op. */ export interface StubToolSpec { /** Tool name as exported on the stub module. Must be a valid TS identifier. */ readonly name: string; /** TypeScript type for the args object. */ readonly argsType: string; /** TypeScript type for the result. */ readonly resultType: string; /** Optional one-line doc rendered as a TSDoc comment above the function. */ readonly description?: string; } export interface StubGeneratorOptions { /** Env var the script reads to find the socket path. */ readonly socketEnvVar: string; /** Tool surface for the script. */ readonly tools: ReadonlyArray; /** Optional file header comment. */ readonly header?: string; } export declare function generateMemiToolsStub(options: StubGeneratorOptions): string;