@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
41 lines • 2.25 kB
JavaScript
import { abortedMessageForRendering, unrunToolCallText } from "./aborted-error-label.js";
/** Keep text/thinking in its original position, matching the live head and trailing segments. */
export function replayAssistantTools(message, host) {
const firstToolIndex = message.content.findIndex((block) => block.type === "toolCall");
// Every part keeps the turn's stopReason, so one part shows the turn-ending notice: the text run that closes
// the message after a tool call, as in live mode, else the head row (or, for an abort, the tool card).
let lastRunStart = -1;
if (firstToolIndex >= 0 && message.content[message.content.length - 1].type !== "toolCall") {
lastRunStart = message.content.length - 1;
while (message.content[lastRunStart - 1].type !== "toolCall")
lastRunStart--;
}
host.addMessage(firstToolIndex < 0 ? message : { ...message, content: message.content.slice(0, firstToolIndex + 1) },
// An aborted turn that ends in a tool call shows its notice on the tool card, as in live mode.
firstToolIndex < 0 || (lastRunStart < 0 && message.stopReason !== "aborted"));
if (firstToolIndex < 0)
return;
let index = firstToolIndex;
while (index < message.content.length) {
const content = message.content[index];
if (content.type !== "toolCall") {
const start = index;
while (index < message.content.length && message.content[index].type !== "toolCall")
index++;
host.addMessage({ ...message, content: message.content.slice(start, index) }, start === lastRunStart);
continue;
}
index++;
const component = host.createTool(content.name, content.id, content.arguments);
component.setExpanded(host.expanded);
host.addChild(component);
if (message.stopReason === "aborted" || message.stopReason === "error") {
const text = unrunToolCallText(abortedMessageForRendering(message, 0, undefined));
component.updateResult({ content: [{ type: "text", text }], isError: true });
}
else {
host.pending.set(content.id, component);
}
}
}
//# sourceMappingURL=replay-assistant-tools.js.map