Add the Claude SDK sidecar: framing, the closed schema converter, the query
`sidecar/claude-sdk` is the repo's second TypeScript package, wired to the root only through `npm --prefix`. It reads length-prefixed MessagePack frames on stdin, runs one SDK query, forwards every tool call to Rust, and returns Rust's answer to the model verbatim. It computes no tool result, reads no credential, logs no prompt, argument, or result, and writes nothing outside its own stdout. The query is opened with a custom system prompt, `settingSources: []`, `tools: []`, `strictMcpConfig`, `persistSession: false`, and `alwaysLoad` on every tool, so no CLAUDE.md, settings file, output style, built-in tool, or persisted transcript reaches a caller-derived prompt. `schema.ts` converts exactly the grammar `tool_schema.rs` emits and throws on anything else, so an emitter that outgrows it fails a test rather than registering a looser tool. Two Rust tests publish the fixtures the sidecar tests read — the frame bytes and every real `parameters_json` — so the halves cannot drift apart silently. Dependencies are exact-pinned: @anthropic-ai/claude-agent-sdk 0.3.261, @msgpack/msgpack 3.1.3, zod 4.5.4, and dev typescript 5.9.3 with @types/node 24.13.3. `engines.node` is the installed major. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
b150b5e38e
commit
2ff1d435d8
13 changed files with 2911 additions and 0 deletions
158
sidecar/claude-sdk/src/frames.ts
Normal file
158
sidecar/claude-sdk/src/frames.ts
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
// The wire between Rust and this sidecar: a 4-byte big-endian length prefix
|
||||
// followed by MessagePack with string keys, matching `rmp_serde::to_vec_named`.
|
||||
// Rust owns the shapes; this file only encodes and decodes them.
|
||||
|
||||
import { decode, encode } from "@msgpack/msgpack";
|
||||
|
||||
/** The connector's own frame cap. Rust refuses anything larger on its side too. */
|
||||
export const MAX_FRAME_BYTES = 1_052_672;
|
||||
|
||||
export interface SidecarTool {
|
||||
name: string;
|
||||
description: string;
|
||||
parameters_json: string;
|
||||
}
|
||||
|
||||
export type SidecarEvent =
|
||||
| { kind: "text"; text: string }
|
||||
| {
|
||||
kind: "tool_call";
|
||||
call_id: string;
|
||||
name: string;
|
||||
arguments: string;
|
||||
dispatched: boolean;
|
||||
};
|
||||
|
||||
export interface SdkModelUsage {
|
||||
model: string;
|
||||
input_tokens: number;
|
||||
output_tokens: number;
|
||||
cache_read_input_tokens: number;
|
||||
cache_creation_input_tokens: number;
|
||||
}
|
||||
|
||||
export interface SdkResultMaterial {
|
||||
session_id: string;
|
||||
result_uuid: string;
|
||||
subtype: string;
|
||||
stop_reason: string | null;
|
||||
num_turns: number;
|
||||
assistant_message_uuids: string[];
|
||||
assistant_request_ids: string[];
|
||||
usage: SdkModelUsage[];
|
||||
total_cost_usd_estimate: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The closed set of failures this sidecar may name. It reports a reason; Rust
|
||||
* assigns the disposition, so nothing here can quarantine the world.
|
||||
*/
|
||||
export type SidecarFaultReason =
|
||||
| "rate_limited"
|
||||
| "overloaded"
|
||||
| "server_error"
|
||||
| "api_timeout"
|
||||
| "authentication_failed"
|
||||
| "org_not_allowed"
|
||||
| "billing_error"
|
||||
| "invalid_request"
|
||||
| "model_not_found"
|
||||
| "max_output_tokens"
|
||||
| "max_budget_usd"
|
||||
| "execution_error"
|
||||
| "unknown"
|
||||
| "protocol_violation"
|
||||
| "tool_registration_failed"
|
||||
| "turn_cap_refused";
|
||||
|
||||
export type SidecarFrame =
|
||||
| {
|
||||
kind: "query";
|
||||
query_id: number;
|
||||
model: string;
|
||||
instructions: string;
|
||||
prompt: string;
|
||||
transcript: string[];
|
||||
tools: SidecarTool[];
|
||||
effort: string | null;
|
||||
max_output_tokens: number | null;
|
||||
turn_cap: number;
|
||||
}
|
||||
| {
|
||||
kind: "tool_call";
|
||||
query_id: number;
|
||||
call_id: string;
|
||||
name: string;
|
||||
arguments: string;
|
||||
}
|
||||
| {
|
||||
kind: "tool_result";
|
||||
query_id: number;
|
||||
call_id: string;
|
||||
output: string;
|
||||
}
|
||||
| {
|
||||
kind: "output";
|
||||
query_id: number;
|
||||
events: SidecarEvent[];
|
||||
receipt: SdkResultMaterial;
|
||||
}
|
||||
| {
|
||||
kind: "fault";
|
||||
query_id: number;
|
||||
reason: SidecarFaultReason;
|
||||
detail: string;
|
||||
};
|
||||
|
||||
export function encodeFrame(frame: SidecarFrame): Uint8Array {
|
||||
const body = encode(frame, { useBigInt64: false });
|
||||
if (body.length > MAX_FRAME_BYTES) {
|
||||
throw new Error(`frame of ${body.length} bytes exceeds ${MAX_FRAME_BYTES}`);
|
||||
}
|
||||
const framed = new Uint8Array(4 + body.length);
|
||||
new DataView(framed.buffer).setUint32(0, body.length, false);
|
||||
framed.set(body, 4);
|
||||
return framed;
|
||||
}
|
||||
|
||||
export function decodeFrameBody(body: Uint8Array): SidecarFrame {
|
||||
const value = decode(body);
|
||||
if (typeof value !== "object" || value === null || !("kind" in value)) {
|
||||
throw new Error("a sidecar frame decoded to something without a kind");
|
||||
}
|
||||
return value as SidecarFrame;
|
||||
}
|
||||
|
||||
/** Reassembles frames from an arbitrarily chunked byte stream. */
|
||||
export class FrameReader {
|
||||
#buffer = new Uint8Array(0);
|
||||
|
||||
push(chunk: Uint8Array): void {
|
||||
const grown = new Uint8Array(this.#buffer.length + chunk.length);
|
||||
grown.set(this.#buffer, 0);
|
||||
grown.set(chunk, this.#buffer.length);
|
||||
this.#buffer = grown;
|
||||
}
|
||||
|
||||
/** The next whole frame, or null while one is still arriving. */
|
||||
next(): SidecarFrame | null {
|
||||
if (this.#buffer.length < 4) {
|
||||
return null;
|
||||
}
|
||||
const length = new DataView(
|
||||
this.#buffer.buffer,
|
||||
this.#buffer.byteOffset,
|
||||
4,
|
||||
).getUint32(0, false);
|
||||
if (length === 0 || length > MAX_FRAME_BYTES) {
|
||||
throw new Error(`frame length ${length} is outside 1..=${MAX_FRAME_BYTES}`);
|
||||
}
|
||||
if (this.#buffer.length < 4 + length) {
|
||||
return null;
|
||||
}
|
||||
const body = this.#buffer.subarray(4, 4 + length);
|
||||
const frame = decodeFrameBody(body);
|
||||
this.#buffer = this.#buffer.slice(4 + length);
|
||||
return frame;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue