@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
116 lines • 5.47 kB
JavaScript
import { getAssistantCapture, seedNextOffset, } from "@earendil-works/pi-ai";
function toJsonAssistantMessageEvent(event) {
if (event.type === "toolcall_start") {
const toolCall = event.partial.content[event.contentIndex];
if (toolCall?.type !== "toolCall") {
throw new Error(`toolcall_start content at index ${event.contentIndex} is not a tool call`);
}
const { partial: _partial, ...deltaEvent } = event;
return { ...deltaEvent, id: toolCall.id, toolName: toolCall.name };
}
if (!("partial" in event)) {
return event;
}
const { partial: _partial, ...deltaEvent } = event;
return deltaEvent;
}
/**
* Builds a seeded assistant event from the producer-time capture only: an ordinary delta reads its index, its
* delta string and the capture, never the shared partial, the block or the parsed arguments.
*/
function toSeededAssistantEvent(event, capture) {
if (event.type === "start")
return capture.message && { type: "start", seed: { message: capture.message } };
if (event.type === "done")
return { type: "done", reason: event.reason, message: event.message };
if (event.type === "error")
return { type: "error", reason: event.reason, error: event.error };
const contentIndex = capture.contentIndex ?? event.contentIndex;
if (event.type === "text_delta" || event.type === "thinking_delta" || event.type === "toolcall_delta") {
const { offset, parsedThrough } = capture;
if (offset === undefined)
return undefined;
if (event.type !== "toolcall_delta")
return { type: event.type, contentIndex, delta: event.delta, offset };
if (parsedThrough === undefined)
return undefined;
return { type: event.type, contentIndex, delta: event.delta, offset, parsedThrough };
}
const { block, coveredCodeUnits } = capture;
if (event.type === "toolcall_end")
return block?.type === "toolCall" ? { type: event.type, contentIndex, toolCall: block } : undefined;
if (event.type === "text_end")
return block?.type === "text" ? { type: event.type, contentIndex, content: event.content, block } : undefined;
if (event.type === "thinking_end")
return block?.type === "thinking" ? { type: event.type, contentIndex, content: event.content, block } : undefined;
if (coveredCodeUnits === undefined)
return undefined;
const seedLength = block?.type === "text"
? block.text.length
: block?.type === "thinking"
? block.thinking.length
: (capture.rawArgumentPrefix?.length ?? 0);
const nextOffset = seedNextOffset(capture.nextOffset, seedLength);
if (event.type === "text_start")
return block?.type === "text"
? { type: event.type, contentIndex, seed: { block, coveredCodeUnits, nextOffset } }
: undefined;
if (event.type === "thinking_start")
return block?.type === "thinking"
? { type: event.type, contentIndex, seed: { block, coveredCodeUnits, nextOffset } }
: undefined;
const { rawArgumentPrefix, parsedThrough } = capture;
if (block?.type !== "toolCall" || rawArgumentPrefix === undefined || parsedThrough === undefined)
return undefined;
return {
type: event.type,
contentIndex,
id: block.id,
toolName: block.name,
seed: { block, rawArgumentPrefix, parsedThrough, coveredCodeUnits, nextOffset },
};
}
/**
* An assistant record with no capture has no seeded form (`undefined`): its producer captured nothing, so any
* seed would be read from the live partial and could already hold later bytes. Identity is never invented.
*/
function toSeededJsonEvent(event) {
if ((event.type === "message_start" || event.type === "message_end") && event.message.role === "assistant") {
const message = getAssistantCapture(event)?.message;
if (message)
return { ...event, message };
// The terminal message is complete when emitted; only an uncaptured start may already hold later bytes.
return event.type === "message_end" ? event : undefined;
}
if (event.type !== "message_update")
return event;
const capture = getAssistantCapture(event);
const assistantMessageEvent = capture && toSeededAssistantEvent(event.assistantMessageEvent, capture);
if (!capture || !assistantMessageEvent)
return undefined;
const { message: _message, assistantMessageEvent: _event, ...outer } = event;
const changed = capture.metadata !== undefined && Object.keys(capture.metadata).length > 0;
return {
...outer,
usage: capture.usage,
...(changed ? { messageMetadata: capture.metadata } : {}),
assistantMessageEvent,
};
}
export function toJsonEvent(event, options) {
if (options)
return toSeededJsonEvent(event);
if (event.type !== "message_update") {
return event;
}
if (event.message.role !== "assistant") {
throw new Error("message_update message is not an assistant message");
}
return {
type: "message_update",
usage: event.message.usage,
assistantMessageEvent: toJsonAssistantMessageEvent(event.assistantMessageEvent),
...(event.resolvedToolName === undefined ? {} : { resolvedToolName: event.resolvedToolName }),
};
}
//# sourceMappingURL=json-event.js.map