UNPKG

@code-yeongyu/senpi

Version:

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

85 lines • 3.66 kB
import { CONTEXT_PROVENANCE_FIELD, contextProvenanceFingerprint, getContextProvenance, } from "@earendil-works/pi-ai"; function isUserWord(value) { return (typeof value === "object" && value !== null && "label" in value && typeof value.label === "string" && "text" in value && typeof value.text === "string"); } export function toolResultUserWords(message) { const details = message.details; if (typeof details !== "object" || details === null || !("userWords" in details)) return []; return Array.isArray(details.userWords) ? details.userWords.filter(isUserWord) : []; } /** The label block a reference names, then the words, for each word in order. */ export function userWordBlocks(words) { return words.flatMap((word) => [ { type: "text", text: `[${word.label}]` }, { type: "text", text: word.text }, ]); } /** * Inserts one user message after each contiguous run of tool results whose details carry user * words. It is derived only from persisted tool results, so live requests, resumed sessions and * compaction all see the same messages, and no queue operation can drop them. */ export function appendToolResultUserWords(messages) { if (!messages.some((message) => message.role === "toolResult" && toolResultUserWords(message).length > 0)) { return messages; } const result = []; let pending = []; let source; const flush = (next) => { if (source && !startsWithBlocks(next, pending)) result.push(wordMessage(pending, source)); pending = []; source = undefined; }; for (const message of messages) { if (message.role !== "toolResult") flush(message); result.push(message); if (message.role !== "toolResult") continue; const blocks = userWordBlocks(toolResultUserWords(message)); if (blocks.length === 0) continue; pending.push(...blocks); source = message; } flush(undefined); return result; } /** * The word message belongs to its tool result: it carries its own copy of that result's * request-local provenance, so a context producer that marks the result (OpenAI remote-compaction * replay) owns the word message too and replaces both together. A sealed result passes its seal on * only while the result still matches it; the copy is then sealed with the word message's own * fingerprint, since it is rebuilt on every conversion and cannot keep a seal from an earlier one. */ function wordMessage(content, source) { const message = { role: "user", content, timestamp: source.timestamp }; const provenance = getContextProvenance(source); if (!provenance) return message; const { integrity, ...owned } = provenance; const proven = integrity === undefined || integrity === contextProvenanceFingerprint(source); if (!proven) return message; const sealed = integrity === undefined ? owned : { ...owned, integrity: contextProvenanceFingerprint(message) }; return Object.assign(message, { [CONTEXT_PROVENANCE_FIELD]: sealed }); } /** Already converted output carries the word message right after the run; converting again keeps one. */ function startsWithBlocks(message, blocks) { if (message?.role !== "user" || typeof message.content === "string") return false; const content = message.content; return blocks.every((block, index) => { const candidate = content[index]; return candidate?.type === "text" && candidate.text === block.text; }); } //# sourceMappingURL=tool-result-user-words.js.map