@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
97 lines • 3.46 kB
JavaScript
const IMAGE_MEDIA_TYPES = ["image/jpeg", "image/png", "image/gif", "image/webp"];
const TOOL_OUTPUT_MARKER = "[tool output] ";
/**
* Claude Code runs a user text block that starts with `/` as one of its own slash commands. Tool output is
* untrusted text, so every non-empty text block appended from `from` on gets a marker first, whatever its first
* character is (invisible characters such as a zero-width space or a BOM included).
*/
export function neutralizeToolResultText(blocks, from) {
for (let index = from; index < blocks.length; index++) {
const block = blocks[index];
if (block?.type === "text" && block.text.length > 0) {
blocks[index] = { ...block, text: `${TOOL_OUTPUT_MARKER}${block.text}` };
}
}
}
export function appendSdkContentBlocks(blocks, content) {
if (typeof content === "string") {
if (content.length > 0)
blocks.push({ type: "text", text: content });
return content.trim().length > 0;
}
let hasText = false;
for (const entry of content) {
hasText = appendEntry(blocks, entry) || hasText;
}
return hasText;
}
function isRecord(value) {
return typeof value === "object" && value !== null;
}
export function isSdkImageMediaType(value) {
return parseImageMediaType(value) !== undefined;
}
function parseImageMediaType(value) {
for (const mediaType of IMAGE_MEDIA_TYPES) {
if (mediaType === value)
return mediaType;
}
return undefined;
}
function omittedPlaceholder(entry) {
if (isRecord(entry) && typeof entry.type === "string") {
if (entry.type === "image") {
const complete = typeof entry.mimeType === "string" && typeof entry.data === "string";
return complete
? `[image block omitted: unsupported media type ${entry.mimeType}]`
: "[image block omitted: missing data]";
}
return `[unsupported content block omitted: ${entry.type}]`;
}
return "[unsupported content block omitted]";
}
function imageFromEntry(entry) {
if (typeof entry.mimeType !== "string" || typeof entry.data !== "string")
return undefined;
const mediaType = parseImageMediaType(entry.mimeType);
if (mediaType === undefined)
return undefined;
return {
type: "image",
source: {
type: "base64",
media_type: mediaType,
data: entry.data,
},
};
}
function appendEntry(blocks, entry) {
if (typeof entry === "string") {
blocks.push({ type: "text", text: entry });
return entry.trim().length > 0;
}
if (!isRecord(entry)) {
blocks.push({ type: "text", text: omittedPlaceholder(entry) });
return true;
}
if (entry.type === "text") {
if (typeof entry.text === "string") {
blocks.push({ type: "text", text: entry.text });
return entry.text.trim().length > 0;
}
blocks.push({ type: "text", text: omittedPlaceholder(entry) });
return true;
}
if (entry.type === "image") {
const image = imageFromEntry(entry);
if (image) {
blocks.push(image);
return false;
}
blocks.push({ type: "text", text: omittedPlaceholder(entry) });
return true;
}
blocks.push({ type: "text", text: omittedPlaceholder(entry) });
return true;
}
//# sourceMappingURL=content-blocks.js.map