Skip to main content

Overview

The LLM Gateway provides a comprehensive type system for building harnesses, handling messages, managing tools, and controlling event flow. All types follow OpenAI’s messaging patterns with dedicated tool roles.

Message Types

ContentPart

Content parts for multipart messages supporting text, images, and documents.
type TextContentPart = { type: "text"; text: string };
type ImageContentPart = { type: "image"; mediaType: string; data: string };
type DocumentContentPart = { type: "document"; mediaType: string; data: string };
type ContentPart = TextContentPart | ImageContentPart | DocumentContentPart;
type
string
required
The content type: "text", "image", or "document"
text
string
Text content (only for type: "text")
mediaType
string
MIME type for images and documents (e.g., "image/png", "application/pdf")
data
string
Base64-encoded content for images and documents

Message

Message structure following OpenAI’s pattern with dedicated tool role.
type Message =
  | { role: "system"; content: string }
  | { role: "user"; content: string | ContentPart[] }
  | { role: "assistant"; content: string | null; tool_calls?: ToolCall[] }
  | { role: "tool"; tool_call_id: string; content: string | ContentPart[] };

System Message

role
'system'
required
System role identifier
content
string
required
System instructions or context

User Message

role
'user'
required
User role identifier
content
string | ContentPart[]
required
User message as plain text or multipart content

Assistant Message

role
'assistant'
required
Assistant role identifier
content
string | null
required
Assistant response text (null when only tool calls present)
tool_calls
ToolCall[]
Tool invocations requested by the assistant

Tool Message

role
'tool'
required
Tool role identifier
tool_call_id
string
required
ID of the tool call this message responds to
content
string | ContentPart[]
required
Tool execution result

Tool Types

ToolDefinition

Defines a tool that can be invoked during harness execution.
interface ToolDefinition<TSchema extends z.ZodTypeAny = z.ZodTypeAny, TResult = unknown> {
  name: string;
  description: string;
  schema: TSchema;
  execute?: (input: z.infer<TSchema>, ctx: ToolContext) => Promise<ToolExecutionResult<TResult>>;
  derivePermission?: (params: Record<string, unknown>) => ToolPermission;
}
name
string
required
Unique tool identifier
description
string
required
Natural language description shown to the model
schema
z.ZodTypeAny
required
Zod schema defining the tool’s input parameters
execute
function
Optional execution handler. Receives validated input and context, returns result.
derivePermission
function
Optional function to derive permission requirements from parameters

Example

import { z } from "zod";

const readFileTool: ToolDefinition = {
  name: "read_file",
  description: "Read contents of a file",
  schema: z.object({
    path: z.string(),
  }),
  execute: async (input, ctx) => {
    const contents = await fs.readFile(input.path, "utf-8");
    return { context: contents };
  },
};

ToolCall

Represents a tool invocation in an assistant message.
interface ToolCall<TSchema extends z.ZodTypeAny = z.ZodTypeAny> {
  id: string;
  name: string;
  arguments?: z.infer<TSchema>;
}
id
string
required
Unique identifier for this tool call
name
string
required
Name of the tool to invoke
arguments
z.infer<TSchema>
Parsed tool arguments matching the tool’s schema

ToolExecutionResult

Result returned from tool execution.
interface ToolExecutionResult<T = unknown> {
  context?: string;  // Injected into agent context window
  result?: T;        // Available to harness/application
}
context
string
Text injected into the agent’s context window (shown to the model)
result
T
Structured result available to the harness or application

ToolContext

Context passed to tool execute functions.
interface ToolContext {
  parentId?: string;
  spawn?: (task: string) => Promise<string>;
  fileTime?: FileTime;
}
parentId
string
The tool_call ID that spawned this execution context
spawn
function
Function to spawn subagent tasks
fileTime
FileTime
File timestamp tracking for deterministic execution

Permission Types

ToolPermission

Defines permission rules for tool execution control.
interface ToolPermission {
  tool: string;
  params?: Record<string, string>;  // param name → glob pattern
}
tool
string
required
Tool name this permission applies to
params
Record<string, string>
Parameter constraints using glob patterns (e.g., {"path": "src/**"})

Permissions

Complete permission configuration for harness invocation.
interface Permissions {
  allowlist?: ToolPermission[];
  allowOnce?: ToolPermission[];
  deny?: Array<{ toolCallId: string; reason?: string }>;
}
allowlist
ToolPermission[]
Tools always permitted without relay events
allowOnce
ToolPermission[]
Tools permitted once, then require relay approval
deny
Array<{ toolCallId: string; reason?: string }>
Explicitly denied tool calls with optional reasons

Event Types

HarnessEvent

Discriminated union of all events a harness can yield.
type HarnessEvent =
  | { type: "harness_start"; runId: string; parentId?: string; depth?: number; maxIterations?: number }
  | { type: "harness_end"; runId: string; parentId?: string; reason?: "final" | "max_iterations"; iterations?: number; totalUsage?: { inputTokens: number; outputTokens: number } }
  | { type: "reasoning"; runId: string; id: string; parentId?: string; content: string }
  | { type: "text"; runId: string; id: string; parentId?: string; content: string }
  | { type: "tool_call"; runId: string; id: string; parentId?: string; name: string; input: unknown }
  | { type: "tool_result"; runId: string; id: string; parentId?: string; name: string; output: unknown }
  | { type: "tool_progress"; runId: string; id: string; parentId?: string; toolCallId: string; name: string; content: unknown }
  | { type: "repl_input"; runId: string; id: string; parentId?: string; code: string; iteration?: number }
  | { type: "repl_progress"; runId: string; id: string; parentId?: string; chunk: string; stream: "stdout" | "stderr" }
  | { type: "repl_output"; runId: string; id: string; parentId?: string; stdout: string; error?: string; done: boolean; iteration?: number; durationMs?: number; truncated?: boolean }
  | { type: "usage"; runId: string; parentId?: string; inputTokens: number; outputTokens: number; cacheReadTokens?: number; cacheCreationTokens?: number }
  | { type: "error"; runId: string; parentId?: string; error: Error }
  | RelayEvent;
See the Events Reference for detailed documentation of each event type.

RelayEvent

Event requesting permission for tool execution.
type RelayEvent = {
  type: "relay";
  kind: "permission";
  runId: string;
  id: string;
  parentId?: string;
  toolCallId: string;
  tool: string;
  params: Record<string, unknown>;
  respond: (response: PermissionResponse) => void;
};
type
'relay'
required
Event type discriminator
kind
'permission'
required
Relay kind (currently only permission)
runId
string
required
Run identifier
id
string
required
Unique event ID
parentId
string
Parent context ID
toolCallId
string
required
Tool call awaiting permission
tool
string
required
Tool name
params
Record<string, unknown>
required
Tool parameters
respond
function
required
Callback to approve or deny the tool call

PermissionResponse

Response to a permission relay event.
type PermissionResponse = { approved: boolean; reason?: string };
approved
boolean
required
Whether the tool call is approved
reason
string
Optional explanation for approval or denial

Harness Interface

HarnessModule

Callback-based harness interface.
interface HarnessModule {
  invoke(params: InvokeParams): Promise<void>;
  supportedModels(): Promise<string[]>;
}

GeneratorHarnessModule

Generator-based harness interface (preferred).
interface GeneratorHarnessModule {
  invoke(params: GeneratorInvokeParams): AsyncIterable<HarnessEvent>;
  supportedModels(): Promise<string[]>;
}

InvokeParams

Parameters for callback-based harness invocation.
interface InvokeParams {
  model?: string;
  messages: Message[];
  tools?: ToolDefinition[];
  emit: (event: HarnessEvent) => void;
  context?: { parentId?: string };
  permissions?: Permissions;
}
model
string
Model identifier (e.g., "claude-3-5-sonnet-20241022")
messages
Message[]
required
Conversation history
tools
ToolDefinition[]
Available tools
emit
function
required
Callback to emit events
context
object
Execution context with optional parent ID
permissions
Permissions
Permission configuration

GeneratorInvokeParams

Parameters for generator-based harness invocation.
interface GeneratorInvokeParams {
  model?: string;
  messages: Message[];
  context?: string;
  tools?: ToolDefinition[];
  env?: {
    parentId?: string;
    spawn?: (task: string, parentId: string) => Promise<string>;
    fileTime?: FileTime;
  };
  permissions?: Permissions;
}
model
string
Model identifier
messages
Message[]
required
Conversation history
context
string
Additional context string injected into the conversation
tools
ToolDefinition[]
Available tools
env
object
Execution environment with spawn capability and file time tracking
permissions
Permissions
Permission configuration

Harness

Harness metadata.
interface Harness {
  id: string;
  name: string;
  description?: string;
  modulePath?: string;
  supportedModels?: string[];
  registeredAt?: number;
}
id
string
required
Unique harness identifier
name
string
required
Human-readable harness name
description
string
Harness description
modulePath
string
File system path to the harness module
supportedModels
string[]
List of supported model identifiers
registeredAt
number
Unix timestamp of registration

LoadedHarness

A loaded harness with its module.
interface LoadedHarness {
  harness: Harness;
  module: HarnessModule;
}
harness
Harness
required
Harness metadata
module
HarnessModule
required
The loaded harness module implementation

Build docs developers (and LLMs) love