Agent adapters API
Generated public TypeScript reference for relkit/agent-adapters.
For configuration and examples, read ai, tools, function tools.
Public imports
relkit/agent-adapters— 40 exports
All exported APIs
Names link to their complete contracts. Re-exports share their original contract; legacy exports keep their separate identity. Examples link to the API’s inline sample, a maintained repository usage, or the shared module walkthrough.
adapter-bridge.ts
Bridges model tool calls to the common engine. Approvals, the engine’s access check and tool failures are handled here, so every adapter calls tools the same way.
createAgentToolBridge
Creates the tool bridge for one agent invocation.
Example
const bridge = createAgentToolBridge({ agentId, invocationId, tools, engine });
Signature
declare const createAgentToolBridge: (options: AgentToolBridgeOptions) => AgentToolBridge;
modelToolName
Derives the model-facing name of a tool; dots and other separators become underscores.
Example
modelToolName("orders.lookup"); // "orders_lookup"
Signature
declare const modelToolName: (toolId: string) => string;
adapter-bridge.types.ts
Inputs of the RelKit-owned tool bridge an adapter run calls for every model tool call.
AgentToolBridgeOptions (interface)
Options for createAgentToolBridge.
Signature
export interface AgentToolBridgeOptions {
readonly agentId: string;
/** Invocation identity copied into approval records. */
readonly invocationId: string;
/** Tool descriptors the agent declared, in declaration order. */
readonly tools: readonly ToolDescriptor<string>[];
/** Common function engine every tool call runs through. */
readonly engine: ToolEngine;
/** Engine access check run before each tool; group 18's access rules plug in here. */
readonly access?: AgentAccessCheck;
/** Caller principal forwarded to the access check. */
readonly principal?: unknown;
readonly trace?: AgentTraceHooks;
}
adapter-events.ts
Maps neutral adapter frames to AG-UI events. An adapter frame first becomes the journal frames the server stores for it, and those are encoded with the server’s own encoder, so a client of the generated server receives the events the conformance suite asserts.
adapterProtocolFrames
Translates one adapter frame into the journal frames the server streams for it.
Signature
declare const adapterProtocolFrames: (frame: AgentAdapterFrame) => Iterable<AgentProtocolFrame>;
agUiEvents
Translates one adapter frame into ordered AG-UI events, with the encoder the server uses.
Example
[...agUiEvents({ kind: "text", messageId: "m", delta: "hi" })];
Signature
declare const agUiEvents: (frame: AgentAdapterFrame) => Iterable<Readonly<Record<string, unknown>>>;
adapter-execution.types.ts
Execution half of the agent adapter interface: stream frames, run results, the context RelKit hands an adapter, and the run, stream, resume and stop contract.
AgentAdapterAgent (interface)
Agent fields an adapter reads at execution time.
Signature
export interface AgentAdapterAgent {
readonly id: string;
readonly model?: string;
readonly instructions: unknown;
readonly output: StandardSchemaV1;
readonly limits: {
readonly maxSteps: number;
readonly maxToolCalls: number;
readonly timeoutMs: number;
};
readonly runtime: AgentRuntimeReference;
}
AgentAdapterContext (interface)
Everything RelKit hands an adapter for one agent.
Signature
export interface AgentAdapterContext {
readonly agent: AgentAdapterAgent;
readonly models: AgentModelResolver;
readonly tools: AgentToolBridge;
readonly state: AgentStateHooks;
readonly trace?: AgentTraceHooks;
}
AgentAdapterExecution (interface)
Execution contract every adapter runtime implements.
Signature
export interface AgentAdapterExecution {
readonly run: (request: AgentRunRequest) => Promise<AgentRunResult>;
readonly stream: (request: AgentRunRequest) => AsyncIterable<AgentAdapterFrame>;
readonly resume: (request: AgentResumeRequest) => Promise<AgentRunResult>;
readonly stop: (runId: string) => Promise<void>;
}
AgentAdapterFrame (type alias)
Neutral stream frames an adapter run emits; RelKit maps them to AG-UI events.
Signature
type AgentAdapterFrame =
| { readonly kind: "run-started"; readonly threadId: string; readonly runId: string }
| {
readonly kind: "text";
readonly messageId: string;
readonly delta: string;
/**
* Where this frame sits in a streamed message: `start` opens it, `delta` continues it and
* `end` closes it. Absent, the frame is a whole message.
*/
readonly phase?: "start" | "delta" | "end";
}
| { readonly kind: "tool-call"; readonly call: AgentToolCall; readonly toolId: string }
| { readonly kind: "tool-result"; readonly outcome: AgentToolOutcome; readonly toolId: string }
| { readonly kind: "approval"; readonly approvals: readonly PendingApproval[] }
| {
readonly kind: "run-finished";
readonly threadId: string;
readonly runId: string;
readonly result: AgentRunResult;
}
| { readonly kind: "run-error"; readonly code: string; readonly message: string };
AgentAdapterRuntime (interface)
Runtime half of an adapter, exported from the adapter package’s runtime entry.
Signature
export interface AgentAdapterRuntime<AdapterId extends string = string> {
readonly adapterId: AdapterId;
readonly protocolVersion: 1;
/** Creates the run, stream, resume and stop contract for one agent. */
readonly execution: (context: AgentAdapterContext) => AgentAdapterExecution;
}
AgentModelTurn (interface)
What a model returned in one turn.
Signature
export interface AgentModelTurn {
readonly text: string;
readonly toolCalls: readonly AgentToolCall[];
}
AgentModelTurnPart (type alias)
One piece of a streamed model turn: text as the model produces it, or a finished tool call. The turn’s text is the concatenation of its text pieces.
Signature
type AgentModelTurnPart =
| { readonly kind: "text"; readonly delta: string }
| { readonly kind: "tool-call"; readonly call: AgentToolCall };
AgentModelTurnRequest (interface)
One model turn: the only SDK-specific step of an agent loop.
Signature
export interface AgentModelTurnRequest {
readonly model: ResolvedAgentModel;
readonly messages: readonly AgentThreadMessage[];
readonly tools: readonly AgentToolDefinition[];
readonly signal: AbortSignal;
}
AgentResumeRequest (interface)
Continuation of an interrupted run with approval decisions.
Signature
export interface AgentResumeRequest {
readonly threadId: string;
readonly decisions: Readonly<Record<string, "approved" | "denied">>;
readonly signal?: AbortSignal;
}
AgentRunRequest (interface)
Start of a run.
Signature
export interface AgentRunRequest {
readonly input: unknown;
readonly threadId: string;
readonly runId?: string;
readonly signal?: AbortSignal;
}
AgentRunResult (type alias)
Result of a run or resume.
Signature
type AgentRunResult =
| { readonly status: "completed"; readonly threadId: string; readonly output: unknown }
| {
readonly status: "interrupted";
readonly threadId: string;
readonly runId: string;
readonly approvals: readonly PendingApproval[];
}
| { readonly status: "stopped"; readonly threadId: string; readonly runId: string };
adapter-identity.ts
Recognizes adapter values and copies their identity onto descriptors; internal to RelKit.
agentRuntimeReference
Copies the serializable adapter identity stored on descriptors and graph nodes.
Example
agentRuntimeReference(runtime).adapterId;
Signature
declare const agentRuntimeReference: (runtime: AgentRuntimeAdapter) => AgentRuntimeReference;
isAgentRuntimeAdapter
Recognizes an agent adapter value.
Example
isAgentRuntimeAdapter(runtime);
Signature
declare const isAgentRuntimeAdapter: (value: unknown) => value is AgentRuntimeAdapter;
adapter-loop-support.ts
Helpers of the shared agent loop: instructions, input text, the output tool and output checks.
AgentAdapterError (class)
Error carrying a stable code from an adapter run.
Signature
declare class AgentAdapterError {
constructor(code: string, message: string);
}
code (property)
Signature
readonly code: string
OUTPUT_TOOL_NAME
Model-facing name of the tool a model calls to return the agent’s final output.
Signature
declare const OUTPUT_TOOL_NAME: "relkit_output";
inputText
Renders run input as a user message.
Signature
declare const inputText: (input: unknown) => string;
instructionText
Renders authored instructions as one system message.
Signature
declare const instructionText: (instructions: unknown) => string;
outputTool
Builds the output tool definition from the agent’s output schema.
Signature
declare const outputTool: (output: StandardSchemaV1) => AgentToolDefinition;
textCandidates
Parses model text as JSON when possible.
Signature
declare const textCandidates: (text: string) => readonly unknown[];
validOutput
Validates a final output, accepting raw text, JSON text or the output tool’s value.
Throws
AgentAdapterError with RELKIT_AGENT_OUTPUT_VALIDATION.
Signature
declare const validOutput: (
output: StandardSchemaV1,
candidates: readonly unknown[],
) => Promise<unknown>;
adapter-loop-turns.ts
Turn and tool-call steps of the shared agent loop: model turns, bridged tool calls, approval interruption, output checks and thread saves.
begin
Starts a run: loads the thread and appends the user input.
Signature
declare const begin: (
options: AgentLoopOptions<string>,
loop: Loop,
input: unknown,
) => AsyncGenerator<AgentAdapterFrame, AgentRunResult, any>;
resumed
Continues pending tool calls with approval decisions, then further turns.
Signature
declare const resumed: (
options: AgentLoopOptions<string>,
loop: Loop,
calls: readonly AgentToolCall[],
decisions: ReadonlyMap<string, ApprovalRecord>,
) => AsyncGenerator<AgentAdapterFrame, AgentRunResult>;
adapter-loop.ts
Shared agent loop behind every first-party adapter. RelKit owns threads, approvals, tool calls, limits, stop, traces and stream frames; the adapter supplies one model turn.
AgentAdapterError
Signature
declare const AgentAdapterError: typeof AgentAdapterError;
createAgentAdapterRuntime
Builds an adapter runtime from one SDK-specific model turn.
Example
createAgentAdapterRuntime({ adapterId: "echo", turn: async () => ({ text: "hi", toolCalls: [] }) });
Signature
declare const createAgentAdapterRuntime: <const AdapterId extends string>(
options: AgentLoopOptions<AdapterId>,
) => AgentAdapterRuntime<AdapterId>;
adapter-loop.types.ts
Inputs of the shared agent loop that adapters build on; only the model turn is SDK-specific.
AgentAdapterFailure (interface)
Error raised by a run that cannot finish.
Signature
export interface AgentAdapterFailure {
readonly code: string;
readonly message: string;
}
AgentLoopOptions (interface)
Options for createAgentAdapterRuntime.
Signature
export interface AgentLoopOptions<AdapterId extends string> {
readonly adapterId: AdapterId;
/**
* Runs one model turn with the adapter's SDK.
* @param request - Resolved model, thread messages, bridged tool definitions and cancellation.
* @returns Text and tool calls the model produced, or the turn's parts as the model produces
* them, so the loop streams its text.
*/
readonly turn: (
request: AgentModelTurnRequest,
) => Promise<AgentModelTurn> | AsyncIterable<AgentModelTurnPart>;
}
AgentLoopState (interface)
Mutable state of one run inside the shared loop.
Signature
export interface AgentLoopState {
readonly context: AgentAdapterContext;
readonly threadId: string;
readonly runId: string;
readonly signal: AbortSignal;
readonly model: ResolvedAgentModel;
messages: AgentThreadMessage[];
steps: number;
toolCalls: number;
}
adapter.ts
Builds agent adapter values. Adapter packages call defineAgentRuntime from their authoring
entry, so declaring an agent never loads an execution SDK.
“./adapter.types.js” (namespace export)
Re-exports all named exports from the “./adapter.types.js” module.
Signature
export * from "./adapter.types.js";
AgentAdapterError
Signature
declare const AgentAdapterError: typeof AgentAdapterError;
AgentLoopOptions
Options for createAgentAdapterRuntime.
Signature
export interface AgentLoopOptions<AdapterId extends string> {
readonly adapterId: AdapterId;
/**
* Runs one model turn with the adapter's SDK.
* @param request - Resolved model, thread messages, bridged tool definitions and cancellation.
* @returns Text and tool calls the model produced, or the turn's parts as the model produces
* them, so the loop streams its text.
*/
readonly turn: (
request: AgentModelTurnRequest,
) => Promise<AgentModelTurn> | AsyncIterable<AgentModelTurnPart>;
}
AgentToolBridgeOptions
Options for createAgentToolBridge.
Signature
export interface AgentToolBridgeOptions {
readonly agentId: string;
/** Invocation identity copied into approval records. */
readonly invocationId: string;
/** Tool descriptors the agent declared, in declaration order. */
readonly tools: readonly ToolDescriptor<string>[];
/** Common function engine every tool call runs through. */
readonly engine: ToolEngine;
/** Engine access check run before each tool; group 18's access rules plug in here. */
readonly access?: AgentAccessCheck;
/** Caller principal forwarded to the access check. */
readonly principal?: unknown;
readonly trace?: AgentTraceHooks;
}
DefineAgentRuntimeOptions (interface)
Options an adapter package passes to defineAgentRuntime.
Signature
export interface DefineAgentRuntimeOptions<
AdapterId extends string,
Options,
StateKey extends string,
> {
readonly adapterId: AdapterId;
readonly integrationId: string;
readonly options: Options;
/** Adapter-specific option keys this value uses; they tie the agent to the adapter. */
readonly nativeOptions?: readonly string[];
readonly clientStateKeys?: readonly StateKey[];
readonly extend?: (input: AgentAdapterDefinitionInput) => Readonly<Record<string, unknown>>;
}
agUiEvents
Signature
declare const agUiEvents: (frame: AgentAdapterFrame) => Iterable<Readonly<Record<string, unknown>>>;
createAgentAdapterRuntime
Signature
declare const createAgentAdapterRuntime: <const AdapterId extends string>(
options: AgentLoopOptions<AdapterId>,
) => AgentAdapterRuntime<AdapterId>;
createAgentToolBridge
Signature
declare const createAgentToolBridge: (options: AgentToolBridgeOptions) => AgentToolBridge;
defineAgentRuntime
Creates the frozen value an agent’s runtime field holds.
Example
import { defineAgentRuntime } from "relkit/agent-adapters";
const runtime = defineAgentRuntime({ adapterId: "echo", integrationId: "echo", options: {} });
void runtime;
Signature
declare const defineAgentRuntime: <
const AdapterId extends string,
Options,
Native extends object = {},
const StateKey extends string = never,
>(
options: DefineAgentRuntimeOptions<AdapterId, Options, StateKey>,
) => AgentRuntimeAdapter<AdapterId, Options, Native, StateKey>;
modelToolName
Signature
declare const modelToolName: (toolId: string) => string;
adapter.types.ts
Declares the agent adapter interface: the authoring value an agent’s runtime field holds, and
the execution contract (run, stream, resume, stop) an adapter implements on top of RelKit’s tool
bridge, model profiles, state hooks and AG-UI event mapping.
“./adapter-execution.types.js” (namespace export)
Re-exports all named exports from the “./adapter-execution.types.js” module.
Signature
export * from "./adapter-execution.types.js";
AgentAccessCheck (type alias)
The engine hook the tool bridge calls before every tool execution. Access rules are owned by the engine; the bridge only calls this check and reports a denial to the model.
Signature
type AgentAccessCheck = (request: AgentAccessRequest) => MaybePromise<AgentAccessDecision>;
AgentAccessDecision (type alias)
Decision returned by the engine’s access check.
Signature
type AgentAccessDecision =
| { readonly allowed: true }
| { readonly allowed: false; readonly code: string; readonly message: string };
AgentAccessRequest (interface)
Request passed to the engine’s access check before a tool runs.
Signature
export interface AgentAccessRequest {
readonly source: "agent";
readonly agentId: string;
readonly toolId: string;
readonly functionId: string;
readonly input: unknown;
readonly principal?: unknown;
}
AgentAdapterDefinitionInput (interface)
Portable fields every adapter receives when an agent is defined.
Signature
export interface AgentAdapterDefinitionInput {
readonly id: string;
/** The authored `defineAgent` options without `runtime`. */
readonly options: Readonly<Record<string, unknown>>;
/** The portable descriptor RelKit built before the adapter adds its fields. */
readonly descriptor: Readonly<Record<string, unknown>>;
}
AgentModelResolver (interface)
Resolves the model profile an agent references; adapters never read configuration directly.
Signature
export interface AgentModelResolver {
/**
* @param selector - The agent's `model` value, such as `default` or `default:gpt-5`.
* @returns The bound model for that profile.
*/
readonly resolve: (selector: string | undefined) => MaybePromise<ResolvedAgentModel>;
}
AgentRuntimeAdapter (interface)
Value stored in an agent’s runtime field. It carries adapter options only and never loads an
SDK: execution lives in the adapter package’s runtime entry.
Signature
export interface AgentRuntimeAdapter<
AdapterId extends string = string,
Options = unknown,
Native extends object = {},
StateKey extends string = never,
> extends AgentRuntimeReference<AdapterId> {
readonly options: Options;
/** Client state keys an agent may expose through `client.state`. */
readonly clientStateKeys: readonly StateKey[];
/**
* Adds adapter-owned descriptor fields, validating adapter options against the portable ones.
* @param input - Agent identity, authored options and the portable descriptor.
* @returns Fields merged over the portable descriptor.
*/
readonly extend?: (input: AgentAdapterDefinitionInput) => Readonly<Record<string, unknown>>;
/** Phantom marker carrying the native descriptor fields; never present at runtime. */
readonly "~native"?: Native;
}
AgentRuntimeAdapterAny (type alias)
Any agent adapter value.
Signature
type AgentRuntimeAdapterAny = AgentRuntimeAdapter<string, unknown, object, string>;
AgentRuntimeNativeFields (type alias)
Native descriptor fields an adapter value adds.
Signature
type AgentRuntimeNativeFields<Runtime> =
Runtime extends AgentRuntimeAdapter<string, unknown, infer Native, string> ? Native : {};
AgentRuntimeReference (interface)
Serializable identity of an agent adapter, copied into descriptors and the graph.
Signature
export interface AgentRuntimeReference<AdapterId extends string = string> {
readonly kind: "agent-runtime";
readonly adapterId: AdapterId;
/** Package integration that registers the adapter's `agent-execution` capability. */
readonly integrationId: string;
readonly protocolVersion: 1;
/** Adapter-specific option keys the agent uses; a non-empty list ties the agent to this adapter. */
readonly nativeOptions: readonly string[];
}
AgentRuntimeStateKey (type alias)
Client state keys an adapter value exposes.
Signature
type AgentRuntimeStateKey<Runtime> =
Runtime extends AgentRuntimeAdapter<string, unknown, object, infer StateKey> ? StateKey : never;
AgentStateHooks (interface)
State persistence hooks backed by the application’s agent-state profile.
Signature
export interface AgentStateHooks {
readonly load: (threadId: string) => MaybePromise<AgentThreadState | undefined>;
readonly save: (state: AgentThreadState) => MaybePromise<void>;
}
AgentThreadMessage (type alias)
Neutral conversation message stored in a thread.
Signature
type AgentThreadMessage =
| { readonly role: "system" | "user"; readonly content: string }
| {
readonly role: "assistant";
readonly content: string;
readonly toolCalls?: readonly AgentToolCall[];
}
| {
readonly role: "tool";
readonly toolCallId: string;
readonly name: string;
readonly content: string;
};
AgentThreadState (interface)
Durable thread state the adapter loads and saves through state hooks.
Signature
export interface AgentThreadState {
readonly threadId: string;
readonly messages: readonly AgentThreadMessage[];
/** Tool calls waiting for approval decisions, with the calls still to run after them. */
readonly pending?: {
readonly runId: string;
readonly approvals: readonly PendingApproval[];
readonly calls: readonly AgentToolCall[];
readonly steps: number;
readonly toolCalls: number;
};
}
AgentToolBridge (interface)
RelKit-owned bridge from model tool calls to the common engine.
Signature
export interface AgentToolBridge {
readonly definitions: readonly AgentToolDefinition[];
/**
* Runs one tool call through access checks, approvals and the engine.
* @param call - Model tool call.
* @param options - Recorded approval decision and cancellation.
*/
readonly call: (
call: AgentToolCall,
options?: { readonly approval?: ApprovalRecord; readonly signal?: AbortSignal },
) => Promise<AgentToolOutcome>;
}
AgentToolCall (interface)
One tool call requested by a model.
Signature
export interface AgentToolCall {
readonly toolCallId: string;
/** Model-facing tool name. */
readonly name: string;
readonly input: unknown;
}
AgentToolDefinition (interface)
Tool exposed to a model through the bridge.
Signature
export interface AgentToolDefinition {
readonly id: string;
/** Model-facing name; dots are replaced so every SDK accepts it. */
readonly name: string;
readonly description: string;
/** JSON Schema of the tool input. */
readonly inputSchema: Readonly<Record<string, unknown>>;
readonly requiresApproval: boolean;
}
AgentToolOutcome (type alias)
Outcome of one bridged tool call.
Signature
type AgentToolOutcome =
| { readonly status: "succeeded"; readonly toolCallId: string; readonly output: unknown }
| {
readonly status: "failed" | "denied";
readonly toolCallId: string;
readonly code: string;
readonly message: string;
}
| { readonly status: "approval-required"; readonly approval: PendingApproval };
AgentTraceHooks (interface)
Trace hooks; RelKit’s observability implements them, tests record them.
Signature
export interface AgentTraceHooks {
readonly start: (
span: AgentTraceSpan,
attributes: Readonly<Record<string, string | number | boolean>>,
) => { readonly end: (outcome: "ok" | "error") => void };
}
AgentTraceSpan (type alias)
Span names an adapter run records.
Signature
type AgentTraceSpan = "agent.run" | "agent.model" | "agent.tool";
ResolvedAgentModel (interface)
A model selected from a profile bound in relkit.config.ts.
Signature
export interface ResolvedAgentModel {
/** Stable model identity, such as `openai:gpt-5`. */
readonly id: string;
/** Profile the model was resolved from. */
readonly profile: string;
/** Adapter-native model value or a provider-qualified model name. */
readonly model: unknown;
}
index.ts
Labs agent adapter interface: adapter values, the tool bridge, the shared agent loop and AG-UI mapping.
“./adapter.js” (namespace export)
Re-exports all named exports from the “./adapter.js” module.
Signature
export * from "./adapter.js";
protocol-events.ts
Encodes journal frames as AG-UI events: the one mapping the generated server and agUiEvents share.
Event types are the string values of @ag-ui/core’s EventType; this module does not import it,
because every agent loads relkit/agent-adapters and @ag-ui/core is an optional peer that only
the AG-UI transport needs. agent-protocol-event-types.test.ts keeps the two in step.
agUiFrames
Translate one journal frame into AG-UI protocol events. The generated server encodes its stream
with it, and agUiEvents encodes an adapter’s frames with it, so both send the same events.
Signature
declare const agUiFrames: (
frame: AgentProtocolFrame,
) => Iterable<Readonly<Record<string, unknown>>>;
protocol-frame.types.ts
AgentProtocolFrame (type alias)
One frame of a run’s stream as the server journals it; AG-UI events are encoded from it.
Signature
type AgentProtocolFrame =
| { readonly kind: "run-started"; readonly threadId: string; readonly runId: string }
| { readonly kind: "state"; readonly value: unknown }
| { readonly kind: "text-start"; readonly messageId: string }
| { readonly kind: "text-delta"; readonly messageId: string; readonly delta: string }
| { readonly kind: "text-end"; readonly messageId: string }
| ({
readonly kind: "progress";
readonly partId: string;
readonly value: unknown;
} & import("../agents/index.js").AgentProgressScope)
| { readonly kind: "approval"; readonly approvalId: string; readonly value: unknown }
| {
readonly kind: "event";
readonly event: JournalRecord["kind"] | "execution" | "execution-snapshot" | "observation";
readonly value: unknown;
readonly eventId?: string;
readonly recordId?: string;
readonly runId?: string;
readonly createdAt?: string;
}
| {
readonly kind: "tool";
readonly toolCallId: string;
readonly toolId: string;
readonly state: import("../agents/index.js").ToolPartState;
readonly value?: unknown;
readonly inputStarted?: boolean;
readonly inputDelta?: string;
}
| {
readonly kind: "terminal";
readonly threadId: string;
readonly runId: string;
readonly status: string;
readonly text?: string;
/** Structured output of a run that did not end in text; it takes the place of `text`. */
readonly output?: unknown;
readonly code?: string;
readonly message?: string;
readonly approvals?: readonly unknown[];
};