@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
107 lines • 5.01 kB
JavaScript
/**
* Provider image rejections (senpi#2170). A provider that rejects an image
* rejects every later request that replays it, and `dropFailedAssistantTurns`
* removes the failed turn but not the image, so one bad image bricked the
* session. The failed assistant turn is persisted with its error, so the
* rejection is derived from history on every request (and after a restart):
* images no successful response had accepted before an image-rejection failure
* are replaced by a placeholder naming their source.
*/
const PROVIDER_IMAGE_REJECTION_PATTERN = /does not represent a valid image|image_parse_error|invalid_image|unsupported image|could not process image|unable to process input image|image_url'?\.?[^\n]*invalid base64|expected a base64-encoded data url|image was specified using the image\/[a-z]+ media type|unsupported media type for base64 image|invalid data url for image/i;
export function isProviderImageRejection(errorMessage) {
return errorMessage !== undefined && PROVIDER_IMAGE_REJECTION_PATTERN.test(errorMessage);
}
export function rejectedImagePlaceholder(source) {
return `[Image omitted: the provider rejected this image (${source}) as invalid, so it is no longer sent with the conversation.]`;
}
function field(message, key) {
return key in message ? message[key] : undefined;
}
function contentBlocks(message) {
const content = field(message, "content");
return Array.isArray(content) ? content : [];
}
function toolCallSource(block) {
const args = block.arguments;
if (typeof args !== "object" || args === null)
return undefined;
const record = args;
for (const key of ["path", "file_path", "filePath"]) {
const value = record[key];
if (typeof value === "string" && value.trim().length > 0)
return value;
}
return undefined;
}
function imageSource(message, toolCallSources) {
if (message.role !== "toolResult")
return "an attached image";
const toolCallId = field(message, "toolCallId");
const known = typeof toolCallId === "string" ? toolCallSources.get(toolCallId) : undefined;
if (known)
return known;
const toolName = field(message, "toolName");
return typeof toolName === "string" ? `${toolName} tool result` : "a tool result";
}
/** Rejected images keyed by the index of the failed assistant turn that rejected them. */
function scanRejectedImages(messages) {
const toolCallSources = new Map();
const rejections = new Map();
let unconfirmed = [];
messages.forEach((message, messageIndex) => {
if (message.role === "assistant") {
for (const block of contentBlocks(message)) {
const source = block.type === "toolCall" ? toolCallSource(block) : undefined;
if (source && typeof block.id === "string")
toolCallSources.set(block.id, source);
}
const stopReason = field(message, "stopReason");
const errorMessage = field(message, "errorMessage");
if (stopReason === "error" &&
isProviderImageRejection(typeof errorMessage === "string" ? errorMessage : undefined)) {
if (unconfirmed.length > 0)
rejections.set(messageIndex, unconfirmed);
unconfirmed = [];
}
else if (stopReason !== "error" && stopReason !== "aborted") {
unconfirmed = [];
}
return;
}
if (message.role !== "user" && message.role !== "toolResult")
return;
contentBlocks(message).forEach((block, blockIndex) => {
if (block.type === "image") {
unconfirmed.push({ messageIndex, blockIndex, source: imageSource(message, toolCallSources) });
}
});
});
return rejections;
}
export function rejectedImageSources(messages, failedTurn) {
const failedIndex = messages.lastIndexOf(failedTurn);
return (scanRejectedImages(messages).get(failedIndex) ?? []).map((image) => image.source);
}
export function omitProviderRejectedImages(messages) {
const rejected = new Map();
for (const images of scanRejectedImages(messages).values()) {
for (const image of images) {
const blocks = rejected.get(image.messageIndex) ?? new Map();
blocks.set(image.blockIndex, image.source);
rejected.set(image.messageIndex, blocks);
}
}
if (rejected.size === 0)
return messages;
return messages.map((message, messageIndex) => {
const blocks = rejected.get(messageIndex);
if (!blocks)
return message;
const content = contentBlocks(message).map((block, blockIndex) => {
const source = blocks.get(blockIndex);
return source === undefined ? block : { type: "text", text: rejectedImagePlaceholder(source) };
});
return { ...message, content };
});
}
//# sourceMappingURL=provider-rejected-images.js.map