fix: make paired evidence loss visible (#202)
This commit is contained in:
@@ -3,7 +3,10 @@ import path from 'path';
|
||||
|
||||
import {
|
||||
extractImageTagPaths,
|
||||
imageTagCaption,
|
||||
missingImageTagCaption,
|
||||
normalizeAgentOutput,
|
||||
splitImageTagPromptParts,
|
||||
type RunnerStructuredOutput,
|
||||
writeProtocolOutput,
|
||||
} from 'ejclaw-runners-shared';
|
||||
@@ -76,36 +79,55 @@ export function buildMultimodalContent(
|
||||
text: string,
|
||||
log: LogFn,
|
||||
): StreamContent {
|
||||
const { cleanText, imagePaths } = extractImageTagPaths(text);
|
||||
const { imagePaths } = extractImageTagPaths(text);
|
||||
if (imagePaths.length === 0) return text;
|
||||
|
||||
const blocks: ContentBlock[] = [];
|
||||
if (cleanText) {
|
||||
blocks.push({ type: 'text', text: cleanText });
|
||||
}
|
||||
const pushText = (value: string) => {
|
||||
const trimmed = value.trim();
|
||||
if (trimmed) blocks.push({ type: 'text', text: trimmed });
|
||||
};
|
||||
|
||||
for (const part of splitImageTagPromptParts(text)) {
|
||||
if (part.type === 'text') {
|
||||
pushText(part.text);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const imgPath of imagePaths) {
|
||||
try {
|
||||
if (!fs.existsSync(imgPath)) {
|
||||
log(`Image not found, skipping: ${imgPath}`);
|
||||
if (!fs.existsSync(part.path)) {
|
||||
log(`Image not found, skipping: ${part.path}`);
|
||||
pushText(missingImageTagCaption(part, 'file not found'));
|
||||
continue;
|
||||
}
|
||||
const data = fs.readFileSync(imgPath).toString('base64');
|
||||
const ext = path.extname(imgPath).toLowerCase();
|
||||
const mediaType = (MIME_TYPES[ext] || 'image/png') as
|
||||
const ext = path.extname(part.path).toLowerCase();
|
||||
const mediaType = MIME_TYPES[ext] as
|
||||
| 'image/jpeg'
|
||||
| 'image/png'
|
||||
| 'image/gif'
|
||||
| 'image/webp';
|
||||
| 'image/webp'
|
||||
| undefined;
|
||||
if (!mediaType) {
|
||||
log(`Unsupported image type, skipping: ${part.path}`);
|
||||
pushText(
|
||||
missingImageTagCaption(
|
||||
part,
|
||||
`unsupported image type ${ext || 'unknown'}`,
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
const data = fs.readFileSync(part.path).toString('base64');
|
||||
pushText(imageTagCaption(part));
|
||||
blocks.push({
|
||||
type: 'image',
|
||||
source: { type: 'base64', media_type: mediaType, data },
|
||||
});
|
||||
log(`Added image block: ${imgPath} (${mediaType})`);
|
||||
log(`Added image block: ${part.path} (${mediaType})`);
|
||||
} catch (err) {
|
||||
log(
|
||||
`Failed to read image ${imgPath}: ${err instanceof Error ? err.message : String(err)}`,
|
||||
);
|
||||
const reason = err instanceof Error ? err.message : String(err);
|
||||
log(`Failed to read image ${part.path}: ${reason}`);
|
||||
pushText(missingImageTagCaption(part, `read failed: ${reason}`));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,10 @@ describe('agent-runner multimodal prompts', () => {
|
||||
expect(Array.isArray(content)).toBe(true);
|
||||
expect(content).toEqual([
|
||||
{ type: 'text', text: '리뷰 증거' },
|
||||
{
|
||||
type: 'text',
|
||||
text: 'Image evidence: settings-v0.1.92-deployed-390.png',
|
||||
},
|
||||
{
|
||||
type: 'image',
|
||||
source: {
|
||||
@@ -46,4 +50,48 @@ describe('agent-runner multimodal prompts', () => {
|
||||
]);
|
||||
expect(logs).toContain(`Added image block: ${imagePath} (image/png)`);
|
||||
});
|
||||
|
||||
it('keeps missing image evidence visible in Claude prompts', () => {
|
||||
const missingPath = path.join(
|
||||
os.tmpdir(),
|
||||
'ejclaw-missing-image-evidence.png',
|
||||
);
|
||||
const logs: string[] = [];
|
||||
|
||||
const content = buildMultimodalContent(
|
||||
`리뷰 증거\n[Image: expected-render.png → ${missingPath}]`,
|
||||
(message) => logs.push(message),
|
||||
);
|
||||
|
||||
expect(content).toEqual([
|
||||
{ type: 'text', text: '리뷰 증거' },
|
||||
{
|
||||
type: 'text',
|
||||
text: `[Image unavailable: expected-render.png → ${missingPath} — file not found]`,
|
||||
},
|
||||
]);
|
||||
expect(logs).toContain(`Image not found, skipping: ${missingPath}`);
|
||||
});
|
||||
|
||||
it('keeps unsupported image evidence visible instead of mislabeled', () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-bmp-image-'));
|
||||
cleanupDirs.push(dir);
|
||||
const imagePath = path.join(dir, 'settings.bmp');
|
||||
fs.writeFileSync(imagePath, Buffer.from('BMunsupported'));
|
||||
const logs: string[] = [];
|
||||
|
||||
const content = buildMultimodalContent(
|
||||
`리뷰 증거\n[Image: settings.bmp → ${imagePath}]`,
|
||||
(message) => logs.push(message),
|
||||
);
|
||||
|
||||
expect(content).toEqual([
|
||||
{ type: 'text', text: '리뷰 증거' },
|
||||
{
|
||||
type: 'text',
|
||||
text: `[Image unavailable: settings.bmp → ${imagePath} — unsupported image type .bmp]`,
|
||||
},
|
||||
]);
|
||||
expect(logs).toContain(`Unsupported image type, skipping: ${imagePath}`);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user