agent-contracts-runtime
Version:
Runtime bridge for executing agent-contracts workflows on Agent SDKs
97 lines (93 loc) • 3.49 kB
TypeScript
import { S as SdkAdapter, C as CacheConfig, A as AdapterSendOptions, a as AgentExecutionRequest, M as MemoryRef } from '../task-runner-Bo37lS9q.js';
import { G as GuardrailHooks } from '../guardrail-hooks-C8G5NGsj.js';
import 'zod';
/**
* Claude Agent SDK Adapter — implements SdkAdapter for @anthropic-ai/claude-agent-sdk
*
* Uses the `query()` function from the Claude Agent SDK to run Claude as a
* stateful coding agent with tool execution (Read, Edit, Bash, etc.).
*
* Supports:
* - send() — start a new query session and return the result text
* - followUp() — resume the same session for output-format corrections
* - sendExecution() — rich path using full contract context
*
* The SDK returns an AsyncGenerator of SDKMessage events. The adapter iterates
* the stream, extracts the final result text from the SDKResultSuccess message,
* and captures the session_id for followUp via `resume`.
*
* GuardrailHooks are mapped to the SDK's PreToolUse hook system.
*/
type SDKContentBlock = {
type: string;
name?: string;
text?: string;
input?: unknown;
};
type SDKBetaMessage = {
content?: SDKContentBlock[];
};
type SDKMessage = {
type: string;
subtype?: string;
result?: string;
error?: string;
session_id?: string;
tool_name?: string;
summary?: string;
message?: SDKBetaMessage;
};
type QueryOptions = Record<string, unknown>;
type QueryFn = (params: {
prompt: string;
options?: QueryOptions;
}) => AsyncGenerator<SDKMessage, void>;
interface ClaudeAgentSdkAdapterConfig {
/** Working directory for the agent. Defaults to process.cwd(). */
cwd?: string;
/** Model identifier (e.g. "claude-sonnet-4-20250514"). Uses SDK default if omitted. */
model?: string;
/**
* Available tools. Defaults to read-only or read-write set based on AdapterSendOptions.
* Set to `{ type: 'preset', preset: 'claude_code' }` for all Claude Code tools.
*/
tools?: string[] | {
type: "preset";
preset: "claude_code";
};
/** Permission mode. Defaults to "bypassPermissions" for automated workflows. */
permissionMode?: "default" | "acceptEdits" | "bypassPermissions" | "plan";
/** Max turns before stopping. */
maxTurns?: number;
/** Runtime guardrail hooks enforced via PreToolUse SDK hooks. */
guardrailHooks?: GuardrailHooks;
/** Prompt caching configuration. Enabled by default. */
cacheConfig?: CacheConfig;
}
declare class ClaudeAgentSdkAdapter implements SdkAdapter {
private cwd;
private model;
private tools;
private permissionMode;
private maxTurns;
private guardrailHooks;
private cacheConfig;
private lastSessionId;
private lastMemoryRef;
private queryFn;
constructor(config?: ClaudeAgentSdkAdapterConfig);
private resolveQueryFn;
private buildOptions;
private runQuery;
send(prompt: string, options: AdapterSendOptions): Promise<string>;
followUp(message: string): Promise<string>;
sendExecution(request: AgentExecutionRequest): Promise<string>;
getLastMemoryRef(): MemoryRef | null;
isCompatible(compat: string): boolean;
/**
* Inject a custom query function for testing.
* Bypasses the dynamic import of @anthropic-ai/claude-agent-sdk.
*/
static withQueryFn(queryFn: QueryFn, config?: ClaudeAgentSdkAdapterConfig): ClaudeAgentSdkAdapter;
}
export { ClaudeAgentSdkAdapter, type ClaudeAgentSdkAdapterConfig };