fix: preserve paired input evidence context (#204)

This commit is contained in:
Eyejoker
2026-05-31 17:17:38 +09:00
committed by GitHub
parent c0703836e1
commit 778ed9b94a
12 changed files with 287 additions and 23 deletions

View File

@@ -2,6 +2,7 @@ import fs from 'fs';
import path from 'path';
import {
expandImagePromptReferences,
extractImageTagPaths,
imageTagCaption,
missingImageTagCaption,
@@ -79,7 +80,8 @@ export function buildMultimodalContent(
text: string,
log: LogFn,
): StreamContent {
const { imagePaths } = extractImageTagPaths(text);
const expandedText = expandImagePromptReferences(text);
const { imagePaths } = extractImageTagPaths(expandedText);
if (imagePaths.length === 0) return text;
const blocks: ContentBlock[] = [];
@@ -88,7 +90,7 @@ export function buildMultimodalContent(
if (trimmed) blocks.push({ type: 'text', text: trimmed });
};
for (const part of splitImageTagPromptParts(text)) {
for (const part of splitImageTagPromptParts(expandedText)) {
if (part.type === 'text') {
pushText(part.text);
continue;

View File

@@ -94,4 +94,57 @@ describe('agent-runner multimodal prompts', () => {
]);
expect(logs).toContain(`Unsupported image type, skipping: ${imagePath}`);
});
it('loads MEDIA image directives as Claude image blocks', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-media-image-'));
cleanupDirs.push(dir);
const imagePath = path.join(dir, 'media-render.png');
fs.writeFileSync(imagePath, ONE_PIXEL_PNG);
const content = buildMultimodalContent(`증거\nMEDIA:${imagePath}`, () => {
// no-op
});
expect(content).toEqual([
{ type: 'text', text: '증거' },
{ type: 'text', text: 'Image evidence: media-render.png' },
{
type: 'image',
source: {
type: 'base64',
media_type: 'image/png',
data: ONE_PIXEL_PNG.toString('base64'),
},
},
]);
});
it('loads markdown image links as Claude image blocks', () => {
const dir = fs.mkdtempSync(
path.join(os.tmpdir(), 'ejclaw-markdown-image-'),
);
cleanupDirs.push(dir);
const imagePath = path.join(dir, 'markdown-render.png');
fs.writeFileSync(imagePath, ONE_PIXEL_PNG);
const content = buildMultimodalContent(
`증거 ![render](${imagePath})`,
() => {
// no-op
},
);
expect(content).toEqual([
{ type: 'text', text: '증거' },
{ type: 'text', text: 'Image evidence: markdown-render.png' },
{
type: 'image',
source: {
type: 'base64',
media_type: 'image/png',
data: ONE_PIXEL_PNG.toString('base64'),
},
},
]);
});
});

View File

@@ -2,6 +2,7 @@ import fs from 'fs';
import path from 'path';
import {
expandImagePromptReferences,
extractImageTagPaths,
imageTagCaption,
missingImageTagCaption,
@@ -22,7 +23,8 @@ export function parseAppServerInput(
text: string,
log: (message: string) => void = () => undefined,
): AppServerInputItem[] {
const { imagePaths } = extractImageTagPaths(text);
const expandedText = expandImagePromptReferences(text);
const { imagePaths } = extractImageTagPaths(expandedText);
const input: AppServerInputItem[] = [];
const pushText = (value: string) => {
const trimmed = value.trim();
@@ -30,7 +32,7 @@ export function parseAppServerInput(
};
if (imagePaths.length > 0) {
for (const part of splitImageTagPromptParts(text)) {
for (const part of splitImageTagPromptParts(expandedText)) {
if (part.type === 'text') {
pushText(part.text);
continue;

View File

@@ -86,4 +86,36 @@ describe('codex app-server input', () => {
]);
expect(logs).toContain(`Unsupported image type, skipping: ${imagePath}`);
});
it('loads MEDIA image directives as local image input items', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-codex-media-'));
cleanupDirs.push(dir);
const imagePath = path.join(dir, 'media-render.png');
fs.writeFileSync(imagePath, ONE_PIXEL_PNG);
const input = parseAppServerInput(`증거\nMEDIA:${imagePath}`);
expect(input).toEqual([
{ type: 'text', text: '증거' },
{ type: 'text', text: 'Image evidence: media-render.png' },
{ type: 'localImage', path: imagePath },
]);
});
it('loads markdown image links as local image input items', () => {
const dir = fs.mkdtempSync(
path.join(os.tmpdir(), 'ejclaw-codex-markdown-'),
);
cleanupDirs.push(dir);
const imagePath = path.join(dir, 'markdown-render.png');
fs.writeFileSync(imagePath, ONE_PIXEL_PNG);
const input = parseAppServerInput(`증거 ![render](${imagePath})`);
expect(input).toEqual([
{ type: 'text', text: '증거' },
{ type: 'text', text: 'Image evidence: markdown-render.png' },
{ type: 'localImage', path: imagePath },
]);
});
});

View File

@@ -92,6 +92,36 @@ export function extractImageTagPaths(text: string): {
};
}
export function expandImagePromptReferences(text: string): string {
const codeSpans = fencedCodeSpans(text);
const withMediaImages = text.replace(
MEDIA_TAG_RE,
(full: string, doubleQuoted, singleQuoted, backticked, bare, offset) => {
if (isInsideSpans(offset, codeSpans)) return full;
const filePath = String(
doubleQuoted ?? singleQuoted ?? backticked ?? bare ?? '',
).trim();
if (!filePath.startsWith('/') || !IMAGE_EXT_RE.test(filePath)) {
return full;
}
const name = attachmentName(filePath) ?? filePath;
return `[Image: ${name}${filePath}]`;
},
);
const markdownCodeSpans = fencedCodeSpans(withMediaImages);
return withMediaImages.replace(
MARKDOWN_IMAGE_ABSOLUTE_LINK_RE,
(full: string, rawPath: string, offset: number) => {
if (isInsideSpans(offset, markdownCodeSpans)) return full;
const filePath = rawPath.trim();
if (!IMAGE_EXT_RE.test(filePath)) return full;
const name = attachmentName(filePath) ?? filePath;
return `[Image: ${name}${filePath}]`;
},
);
}
export type ImageTagPromptPart =
| { type: 'text'; text: string }
| { type: 'image'; label: string | null; path: string; raw: string };

View File

@@ -38,6 +38,7 @@ export {
type TaskContextMode,
} from './task-runtime.js';
export {
expandImagePromptReferences,
extractMarkdownImageAttachments,
extractMediaAttachments,
extractImageTagPaths,

View File

@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import {
expandImagePromptReferences,
extractImageTagPaths,
imageTagCaption,
missingImageTagCaption,
@@ -59,6 +60,23 @@ describe('shared agent protocol helpers', () => {
);
});
it('expands MEDIA image directives into image prompt tags', () => {
expect(expandImagePromptReferences('증거\nMEDIA:/tmp/render.png\n끝')).toBe(
'증거\n[Image: render.png → /tmp/render.png]\n끝',
);
});
it('expands markdown image links into image prompt tags', () => {
expect(
expandImagePromptReferences('증거 ![render](/tmp/render.png) 끝'),
).toBe('증거 [Image: render.png → /tmp/render.png] 끝');
});
it('keeps non-image media and fenced media text unchanged', () => {
const text = 'MEDIA:/tmp/demo.mp4\n```text\nMEDIA:/tmp/render.png\n```';
expect(expandImagePromptReferences(text)).toBe(text);
});
it('normalizes plain text runner output as public text', () => {
expect(normalizePublicTextOutput('DONE')).toEqual({
result: 'DONE',