Type-safe message system for SDK communication.
type SDKMessage =
| SDKAssistantMessage // Assistant responses
| SDKUserMessage // User input
| SDKResultMessage // Final result (success or error)
| SDKSystemMessage // System info (init, hooks, status)
| SDKToolProgressMessage // Tool execution progress
| SDKAuthStatusMessage // Authentication status
| SDKPartialAssistantMessage // Streaming events (if enabled)
| SDKUserMessageReplay // Message replay marker
| SDKCompactBoundaryMessage // History compaction marker
| SDKStatusMessage // Status updates
| SDKHookResponseMessage // Hook execution resultinterface SDKAssistantMessage {
type: 'assistant';
message: APIAssistantMessage;
parent_tool_use_id: string | null;
error?: 'authentication_failed' | 'billing_error' | 'rate_limit' | 'invalid_request' | 'server_error' | 'unknown';
uuid: UUID;
session_id: string;
}Usage:
if (msg.type === 'assistant') {
if (msg.error) {
console.error('Error:', msg.error);
} else {
console.log('Response:', msg.message.content);
}
}interface SDKResultMessageSuccess {
type: 'result';
subtype: 'success';
duration_ms: number;
duration_api_ms: number;
num_turns: number;
result: string;
total_cost_usd: number;
usage: NonNullableUsage;
modelUsage: {[modelName: string]: ModelUsage};
permission_denials: SDKPermissionDenial[];
structured_output?: unknown;
uuid: UUID;
session_id: string;
}
interface SDKResultMessageError {
type: 'result';
subtype: 'error_during_execution' | 'error_max_turns' | 'error_max_budget_usd' | 'error_max_structured_output_retries';
duration_ms: number;
num_turns: number;
total_cost_usd: number;
usage: NonNullableUsage;
modelUsage: {[modelName: string]: ModelUsage};
permission_denials: SDKPermissionDenial[];
errors: string[];
uuid: UUID;
session_id: string;
}Usage:
if (msg.type === 'result') {
console.log('Duration:', msg.duration_ms, 'ms');
console.log('Cost: $', msg.total_cost_usd);
console.log('Turns:', msg.num_turns);
if (msg.subtype === 'success') {
console.log('Result:', msg.result);
if (msg.structured_output) {
console.log('Structured:', msg.structured_output);
}
} else {
console.error('Error:', msg.subtype, msg.errors);
}
if (msg.permission_denials.length > 0) {
msg.permission_denials.forEach(d => {
console.warn('Denied:', d.tool_name, d.tool_input);
});
}
}interface SDKSystemMessage {
type: 'system';
subtype: 'init';
agents?: string[];
apiKeySource: ApiKeySource;
betas?: string[];
claude_code_version: string;
cwd: string;
tools: string[];
mcp_servers: {name: string; status: string}[];
model: string;
permissionMode: PermissionMode;
slash_commands: string[];
output_style: string;
skills: string[];
plugins: {name: string; path: string}[];
uuid: UUID;
session_id: string;
}Usage:
if (msg.type === 'system' && msg.subtype === 'init') {
console.log('Session:', msg.session_id);
console.log('Model:', msg.model);
console.log('Tools:', msg.tools);
console.log('Agents:', msg.agents);
}interface SDKToolProgressMessage {
type: 'tool_progress';
tool_use_id: string;
tool_name: string;
parent_tool_use_id: string | null;
elapsed_time_seconds: number;
uuid: UUID;
session_id: string;
}Usage:
if (msg.type === 'tool_progress') {
console.log(`${msg.tool_name} running: ${msg.elapsed_time_seconds}s`);
}interface SDKUserMessage {
type: 'user';
message: APIUserMessage;
parent_tool_use_id: string | null;
isSynthetic?: boolean;
tool_use_result?: unknown;
uuid?: UUID;
session_id: string;
}interface SDKPermissionDenial {
tool_name: string;
tool_use_id: string;
tool_input: Record<string, unknown>;
}
interface ModelUsage {
inputTokens: number;
outputTokens: number;
cacheReadInputTokens: number;
cacheCreationInputTokens: number;
webSearchRequests: number;
costUSD: number;
contextWindow: number;
}
interface AccountInfo {
email?: string;
organization?: string;
subscriptionType?: string;
tokenSource?: string;
apiKeySource?: string;
}
interface ModelInfo {
value: string;
displayName: string;
description: string;
}
interface SlashCommand {
name: string;
description: string;
argumentHint: string;
}
interface McpServerStatus {
name: string;
status: 'connected' | 'failed' | 'needs-auth' | 'pending';
serverInfo?: {name: string; version: string};
}
type ApiKeySource = 'user' | 'project' | 'org' | 'temporary';
type PermissionMode = 'default' | 'acceptEdits' | 'bypassPermissions' | 'plan' | 'dontAsk';for await (const msg of result) {
switch (msg.type) {
case 'assistant':
console.log('Assistant:', msg.message.content);
break;
case 'result':
if (msg.subtype === 'success') {
console.log('Success:', msg.result);
console.log('Cost: $', msg.total_cost_usd);
} else {
console.error('Error:', msg.errors);
}
break;
case 'system':
if (msg.subtype === 'init') {
console.log('Session started:', msg.session_id);
}
break;
case 'tool_progress':
console.log(`${msg.tool_name}: ${msg.elapsed_time_seconds}s`);
break;
case 'auth_status':
if (msg.isAuthenticating) {
console.log('Authenticating...');
}
break;
}
}