UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

97 lines • 4.81 kB
import { CALL_NAMED_RECORDS } from "./compact-call-identity.js"; import { CALL_UNKNOWN, callIdentity, } from "./compact-call-model.js"; const field = (record, key) => Reflect.get(record, key); const text = (record, key) => String(field(record, key) ?? ""); const ownedRaw = (block) => block ? { kind: "owned", root: block.root, tailRoot: block.tailRoot } : null; /** The announcement side: the seeded block records and the terminal message of one assistant stream. */ export function foldCallBlock(calls, t, step, block) { const address = { messageId: t.messageId, contentIndex: t.event.contentIndex }; const correlation = t.correlation && { ...("clientMessageId" in t.correlation ? { clientMessageId: field(t.correlation, "clientMessageId") } : {}), ...("clientTurnId" in t.correlation ? { clientTurnId: field(t.correlation, "clientTurnId") } : {}), }; switch (t.event.type) { case "toolcall_start": return calls.toolStart(step, { ...address, id: t.event.id, name: t.event.toolName, arguments: t.event.seed.block.arguments, ...(t.resolvedToolName === undefined ? {} : { resolvedName: t.resolvedToolName }), ...(correlation ? { correlation } : {}), }); case "toolcall_end": return calls.announce(step, { ...address, id: t.event.toolCall.id, name: t.event.toolCall.name, arguments: t.event.toolCall.arguments, ...(t.resolvedToolName === undefined ? {} : { resolvedName: t.resolvedToolName }), }, ownedRaw(block)); case "message_block_metadata": if (t.event.name !== undefined) calls.requestedName(step, address.messageId, address.contentIndex, t.event.name); return; default: return; } } /** The terminal payload is authoritative for every toolCall block; a block no event announced gets its call here. */ export function foldCallTerminal(calls, messageId, message, step, live) { if (message.role !== "assistant") return; for (const [contentIndex, block] of message.content.entries()) if (block.type === "toolCall") calls.announce(step, { messageId, contentIndex, id: block.id, name: block.name, arguments: block.arguments }, ownedRaw(live?.get(contentIndex))); } /** The execution side: a JSON-normal legacy record folded at one lane step (RULING-13 Q9 reports `changed`). */ export function foldCallRecord(calls, record, scope, sequence, callKey) { const step = { sequence, scope }; const before = calls.table.counters.replacements; let projection; let call; const type = field(record, "type"); switch (type) { case "tool_execution_start": { const parentToolCallId = field(record, "parentToolCallId"); call = calls.executionStart(step, { toolCallId: text(record, "toolCallId"), toolName: text(record, "toolName"), args: field(record, "args") ?? null, ...(typeof parentToolCallId === "string" ? { parentToolCallId } : {}), ...(callKey === undefined ? {} : { key: callKey }), }); break; } case "tool_execution_update": call = calls.executionUpdate(step, text(record, "toolCallId"), field(record, "args") ?? null, field(record, "partialResult") ?? null); break; case "tool_execution_end": call = calls.executionEnd(step, text(record, "toolCallId"), field(record, "result") ?? null, field(record, "isError") === true); break; case "tool_hook_status": call = calls.hook(step, text(record, "toolCallId"), record); break; case "turn_end": calls.turnEnd(step); break; case "agent_settled": calls.agentSettled(step); break; case "message_end": { const message = Object(field(record, "message")); if (field(message, "role") === "toolResult") projection = calls.settle(step, text(message, "toolCallId"), field(message, "isError") === true); break; } } const named = CALL_NAMED_RECORDS.has(String(type)); // C3.4: a call sharing its toolCallId inside its message was picked by order, which a stamp never vouches for. const identity = call && calls.table.sharesIdInMessage(call) ? CALL_UNKNOWN : callIdentity(call); return { changed: calls.table.counters.replacements !== before, ...(projection ? { projection } : {}), ...(named ? { identity } : {}), }; } //# sourceMappingURL=compact-call-fold.js.map