@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
41 lines • 1.46 kB
JavaScript
import { mkdir, rm, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
export function collectImages(images) {
const collected = [];
let pendingText;
for (const block of images.output) {
if (block.type === "text") {
const text = block.text.trim();
pendingText = text.length > 0 ? text : undefined;
continue;
}
collected.push({
data: block.data,
mimeType: block.mimeType,
...(pendingText === undefined ? {} : { revisedPrompt: pendingText }),
});
pendingText = undefined;
}
return collected;
}
export async function writeImages(paths, images) {
const written = [];
for (const [index, image] of images.entries()) {
const target = paths[index];
if (target === undefined)
break;
try {
await mkdir(dirname(target), { recursive: true });
await writeFile(target, Buffer.from(image.data, "base64"), { flag: "wx" });
written.push(target);
}
catch (error) {
for (const path of written)
await rm(path, { force: true }).catch(() => undefined);
const reason = error instanceof Error ? error.message : String(error);
return `Error: failed to write generated image to ${target}: ${reason}`;
}
}
return undefined;
}
//# sourceMappingURL=saved-images.js.map