@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
69 lines • 2.77 kB
JavaScript
import { LEAK_ERROR_MESSAGE, renderSystemInterrupt } from "./prompts.js";
import { TTSR_INJECTION_CUSTOM_TYPE } from "./types.js";
const TRUNCATION_MARKER = "[output interrupted by stream rule]";
const TRANSPORT_TIMEOUT_ERROR_PATTERN = /^Request timed out\.?$/i;
function streamBlockText(block, kind) {
if (kind === "tool")
return undefined;
if (typeof block !== "object" || block === null)
return undefined;
if (!("type" in block) || block.type !== kind)
return undefined;
const value = Reflect.get(block, kind);
return typeof value === "string" ? value : undefined;
}
function replaceStreamBlockText(block, kind, text) {
if (kind === "text" && block.type === "text")
return { ...block, text };
if (kind === "thinking" && block.type === "thinking")
return { ...block, thinking: text };
return block;
}
export function buildTruncateReplacement(message, garbageStartOffset, streamKind) {
if (streamKind === "tool") {
const content = message.content.filter((block) => {
if (typeof block !== "object" || block === null || !("type" in block))
return true;
return block.type !== "toolCall";
});
return {
...message,
content: [...content, { type: "text", text: TRUNCATION_MARKER }],
stopReason: message.stopReason === "toolUse" ? "aborted" : message.stopReason,
};
}
let remaining = Math.max(0, garbageStartOffset);
let truncated = false;
const content = [];
for (const block of message.content) {
const text = streamBlockText(block, streamKind);
if (truncated && text !== undefined)
continue;
if (text === undefined || remaining > text.length) {
content.push(block);
if (text !== undefined)
remaining -= text.length;
continue;
}
content.push(replaceStreamBlockText(block, streamKind, text.slice(0, remaining)));
truncated = true;
}
content.push({ type: "text", text: TRUNCATION_MARKER });
const result = { ...message, content };
if (result.errorMessage !== undefined && TRANSPORT_TIMEOUT_ERROR_PATTERN.test(result.errorMessage)) {
delete result.errorMessage;
}
return result;
}
export function buildErrorShellReplacement() {
return { role: "assistant", content: [], stopReason: "error", errorMessage: LEAK_ERROR_MESSAGE };
}
export function buildNudgeMessage(ruleName, ruleContent) {
return {
customType: TTSR_INJECTION_CUSTOM_TYPE,
content: renderSystemInterrupt(ruleName, ruleContent),
display: false,
details: { rules: [ruleName] },
};
}
//# sourceMappingURL=remediation.js.map