/** * Tools-RPC wire format — newline-delimited JSON between the agent host * (parent) and a Bun child process running an `execute_code` script. * * Pattern adapted from Hermes Agent's hermes_tools RPC. The script * imports a generated `memi_tools.ts` stub that translates each tool * call into a JSON request over a Unix domain socket; the parent runs * the actual tool against the engine's broker and sends back the * result. The script's perspective is "I'm calling local async * functions"; in reality every call round-trips to the parent. * * Why this matters: a multi-step pipeline that today costs N LLM turns * (one per tool call) collapses to ONE LLM turn. The model emits one * `execute_code` invocation; the script then runs as many tool calls * as it needs, all driven by deterministic code, all out of band of * the model's reasoning budget. * * Wire format: * request: { id: number, op: "tool", tool: string, args: unknown } * request: { id: number, op: "log", level: "info"|"warn"|"error", message: string } * request: { id: number, op: "exit", ok: boolean, result?: unknown, error?: string } * response: { id: number, ok: boolean, result?: unknown, error?: string } * * Each message is a single JSON object on its own line (newline- * terminated). The parent multiplexes responses to the correct script- * side promise via `id`. */ import { z } from "zod"; export interface ToolRequest { readonly id: number; readonly op: "tool"; readonly tool: string; readonly args: unknown; } export interface LogRequest { readonly id: number; readonly op: "log"; readonly level: "info" | "warn" | "error"; readonly message: string; readonly data?: unknown; } export interface ExitRequest { readonly id: number; readonly op: "exit"; readonly ok: boolean; readonly result?: unknown; readonly error?: string; } export type ToolsRpcRequest = ToolRequest | LogRequest | ExitRequest; export interface ToolsRpcResponse { readonly id: number; readonly ok: boolean; readonly result?: unknown; readonly error?: string; } export declare const toolRequestSchema: z.ZodObject<{ id: z.ZodNumber; } & { op: z.ZodLiteral<"tool">; tool: z.ZodString; args: z.ZodUnknown; }, "strip", z.ZodTypeAny, { id: number; tool: string; op: "tool"; args?: unknown; }, { id: number; tool: string; op: "tool"; args?: unknown; }>; export declare const logRequestSchema: z.ZodObject<{ id: z.ZodNumber; } & { op: z.ZodLiteral<"log">; level: z.ZodEnum<["info", "warn", "error"]>; message: z.ZodString; data: z.ZodOptional; }, "strip", z.ZodTypeAny, { message: string; level: "warn" | "error" | "info"; id: number; op: "log"; data?: unknown; }, { message: string; level: "warn" | "error" | "info"; id: number; op: "log"; data?: unknown; }>; export declare const exitRequestSchema: z.ZodObject<{ id: z.ZodNumber; } & { op: z.ZodLiteral<"exit">; ok: z.ZodBoolean; result: z.ZodOptional; error: z.ZodOptional; }, "strip", z.ZodTypeAny, { id: number; ok: boolean; op: "exit"; error?: string | undefined; result?: unknown; }, { id: number; ok: boolean; op: "exit"; error?: string | undefined; result?: unknown; }>; export declare const toolsRpcRequestSchema: z.ZodDiscriminatedUnion<"op", [z.ZodObject<{ id: z.ZodNumber; } & { op: z.ZodLiteral<"tool">; tool: z.ZodString; args: z.ZodUnknown; }, "strip", z.ZodTypeAny, { id: number; tool: string; op: "tool"; args?: unknown; }, { id: number; tool: string; op: "tool"; args?: unknown; }>, z.ZodObject<{ id: z.ZodNumber; } & { op: z.ZodLiteral<"log">; level: z.ZodEnum<["info", "warn", "error"]>; message: z.ZodString; data: z.ZodOptional; }, "strip", z.ZodTypeAny, { message: string; level: "warn" | "error" | "info"; id: number; op: "log"; data?: unknown; }, { message: string; level: "warn" | "error" | "info"; id: number; op: "log"; data?: unknown; }>, z.ZodObject<{ id: z.ZodNumber; } & { op: z.ZodLiteral<"exit">; ok: z.ZodBoolean; result: z.ZodOptional; error: z.ZodOptional; }, "strip", z.ZodTypeAny, { id: number; ok: boolean; op: "exit"; error?: string | undefined; result?: unknown; }, { id: number; ok: boolean; op: "exit"; error?: string | undefined; result?: unknown; }>]>; export declare const toolsRpcResponseSchema: z.ZodObject<{ id: z.ZodNumber; } & { ok: z.ZodBoolean; result: z.ZodOptional; error: z.ZodOptional; }, "strip", z.ZodTypeAny, { id: number; ok: boolean; error?: string | undefined; result?: unknown; }, { id: number; ok: boolean; error?: string | undefined; result?: unknown; }>; export declare function parseRequest(raw: unknown): ToolsRpcRequest; export declare function safeParseRequest(raw: unknown): { ok: true; req: ToolsRpcRequest; } | { ok: false; error: string; }; /** * Encode a single message for the wire. Always ends with `\n` so the * receiver's line-buffered reader can split cleanly. */ export declare function encodeMessage(msg: ToolsRpcRequest | ToolsRpcResponse): string; /** * Decode a chunk of wire data into messages. Buffers partial lines via * the `state` argument; pass the same state object across chunks. */ export interface DecoderState { buffer: string; } export declare function createDecoderState(): DecoderState; export declare function decodeChunk(state: DecoderState, chunk: string): unknown[];