UNPKG

@anthropic-ai/sdk

Version:
87 lines 3.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.accumulateManagedAgentsEvent = accumulateManagedAgentsEvent; const error_1 = require("../../core/error.js"); function accumulateManagedAgentsEvent(accumulated, event) { switch (event.type) { case 'event_start': { if (event.event.type === 'agent.message') { return { id: event.event.id, type: 'agent.message', content: [], processed_at: '' }; } return accumulated; } case 'agent.message': { return { ...event, content: event.content.map((block) => ({ ...block })) }; } case 'event_delta': { if (accumulated === undefined) { throw new error_1.AnthropicError(`event_delta for ${event.event_id} received before its event_start`); } const idx = event.delta.index ?? 0; const fragment = event.delta.content; // Indices arrive in order — the first delta at a new index opens the slot. // A gap means deltas arrived out of order or were mis-routed. if (idx > accumulated.content.length) { throw new error_1.AnthropicError(`event_delta index ${idx} is beyond the end of content (length ${accumulated.content.length})`); } const existing = accumulated.content[idx]; if (existing === undefined) { // New index: pass the fragment through as a fresh block. return { ...accumulated, content: [...accumulated.content, { ...fragment }] }; } let updated = existing; switch (fragment.type) { case 'text': if (existing.type === 'text') { updated = { ...existing, text: existing.text + fragment.text }; } break; default: checkNever(fragment.type); } const content = accumulated.content.slice(); content[idx] = updated; return { ...accumulated, content }; } case 'user.message': case 'user.interrupt': case 'user.tool_confirmation': case 'user.tool_result': case 'user.custom_tool_result': case 'user.define_outcome': case 'agent.thinking': case 'agent.tool_use': case 'agent.tool_result': case 'agent.custom_tool_use': case 'agent.mcp_tool_use': case 'agent.mcp_tool_result': case 'agent.thread_message_received': case 'agent.thread_message_sent': case 'agent.thread_context_compacted': case 'session.error': case 'session.updated': case 'session.deleted': case 'session.status_running': case 'session.status_idle': case 'session.status_rescheduled': case 'session.status_terminated': case 'session.thread_created': case 'session.thread_status_running': case 'session.thread_status_idle': case 'session.thread_status_rescheduled': case 'session.thread_status_terminated': case 'span.model_request_start': case 'span.model_request_end': case 'span.outcome_evaluation_start': case 'span.outcome_evaluation_ongoing': case 'span.outcome_evaluation_end': case 'system.message': return accumulated; default: checkNever(event); return accumulated; } } // Compile-time exhaustiveness guard. function checkNever(_x) { } //# sourceMappingURL=accumulate.js.map