fix: load supported document attachments (#205)

This commit is contained in:
Eyejoker
2026-05-31 22:30:49 +09:00
committed by GitHub
parent 778ed9b94a
commit b6e7e060cc
11 changed files with 398 additions and 30 deletions

View File

@@ -2,12 +2,12 @@ import fs from 'fs';
import path from 'path';
import {
expandImagePromptReferences,
extractImageTagPaths,
imageTagCaption,
missingImageTagCaption,
attachmentEvidenceCaption,
expandPromptAttachmentReferences,
missingAttachmentCaption,
normalizeAgentOutput,
splitImageTagPromptParts,
splitPromptAttachmentParts,
type PromptAttachmentPart,
type RunnerStructuredOutput,
writeProtocolOutput,
} from 'ejclaw-runners-shared';
@@ -51,6 +51,21 @@ type ContentBlock =
media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';
data: string;
};
}
| {
type: 'document';
title?: string | null;
source:
| {
type: 'base64';
media_type: 'application/pdf';
data: string;
}
| {
type: 'text';
media_type: 'text/plain';
data: string;
};
};
interface SDKUserMessage {
@@ -76,13 +91,72 @@ const MIME_TYPES: Record<string, string> = {
'.webp': 'image/webp',
};
const TEXT_DOCUMENT_EXTENSIONS = new Set([
'.txt',
'.md',
'.markdown',
'.csv',
'.json',
'.log',
'.xml',
'.yaml',
'.yml',
'.toml',
'.ini',
'.cfg',
'.conf',
]);
function loadDocumentBlock(
part: Extract<PromptAttachmentPart, { type: 'attachment' }>,
log: LogFn,
): ContentBlock | string {
try {
if (!fs.existsSync(part.path)) {
log(`Document not found, skipping: ${part.path}`);
return missingAttachmentCaption(part, 'file not found');
}
const ext = path.extname(part.path).toLowerCase();
if (ext === '.pdf') {
const data = fs.readFileSync(part.path).toString('base64');
log(`Added document block: ${part.path} (application/pdf)`);
return {
type: 'document',
title: part.label ?? path.basename(part.path),
source: { type: 'base64', media_type: 'application/pdf', data },
};
}
if (TEXT_DOCUMENT_EXTENSIONS.has(ext)) {
const data = fs.readFileSync(part.path, 'utf8');
log(`Added document block: ${part.path} (text/plain)`);
return {
type: 'document',
title: part.label ?? path.basename(part.path),
source: { type: 'text', media_type: 'text/plain', data },
};
}
log(`Unsupported document type, skipping: ${part.path}`);
return missingAttachmentCaption(
part,
`unsupported document type ${ext || 'unknown'}`,
);
} catch (err) {
const reason = err instanceof Error ? err.message : String(err);
log(`Failed to read document ${part.path}: ${reason}`);
return missingAttachmentCaption(part, `read failed: ${reason}`);
}
}
export function buildMultimodalContent(
text: string,
log: LogFn,
): StreamContent {
const expandedText = expandImagePromptReferences(text);
const { imagePaths } = extractImageTagPaths(expandedText);
if (imagePaths.length === 0) return text;
const expandedText = expandPromptAttachmentReferences(text);
const parts = splitPromptAttachmentParts(expandedText);
if (!parts.some((part) => part.type === 'attachment')) return text;
const blocks: ContentBlock[] = [];
const pushText = (value: string) => {
@@ -90,16 +164,32 @@ export function buildMultimodalContent(
if (trimmed) blocks.push({ type: 'text', text: trimmed });
};
for (const part of splitImageTagPromptParts(expandedText)) {
for (const part of parts) {
if (part.type === 'text') {
pushText(part.text);
continue;
}
if (part.kind === 'document') {
pushText(attachmentEvidenceCaption(part));
const block = loadDocumentBlock(part, log);
if (typeof block === 'string') {
pushText(block);
} else {
blocks.push(block);
}
continue;
}
if (part.kind !== 'image') {
pushText(part.raw);
continue;
}
try {
if (!fs.existsSync(part.path)) {
log(`Image not found, skipping: ${part.path}`);
pushText(missingImageTagCaption(part, 'file not found'));
pushText(missingAttachmentCaption(part, 'file not found'));
continue;
}
const ext = path.extname(part.path).toLowerCase();
@@ -112,7 +202,7 @@ export function buildMultimodalContent(
if (!mediaType) {
log(`Unsupported image type, skipping: ${part.path}`);
pushText(
missingImageTagCaption(
missingAttachmentCaption(
part,
`unsupported image type ${ext || 'unknown'}`,
),
@@ -120,7 +210,7 @@ export function buildMultimodalContent(
continue;
}
const data = fs.readFileSync(part.path).toString('base64');
pushText(imageTagCaption(part));
pushText(attachmentEvidenceCaption(part));
blocks.push({
type: 'image',
source: { type: 'base64', media_type: mediaType, data },
@@ -129,7 +219,7 @@ export function buildMultimodalContent(
} catch (err) {
const reason = err instanceof Error ? err.message : String(err);
log(`Failed to read image ${part.path}: ${reason}`);
pushText(missingImageTagCaption(part, `read failed: ${reason}`));
pushText(missingAttachmentCaption(part, `read failed: ${reason}`));
}
}

View File

@@ -10,6 +10,7 @@ const ONE_PIXEL_PNG = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII=',
'base64',
);
const MINIMAL_PDF = Buffer.from('%PDF-1.4\n1 0 obj\n<<>>\nendobj\n%%EOF\n');
const cleanupDirs: string[] = [];
@@ -147,4 +148,62 @@ describe('agent-runner multimodal prompts', () => {
},
]);
});
it('loads PDF file tags as Claude document blocks', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-pdf-doc-'));
cleanupDirs.push(dir);
const pdfPath = path.join(dir, 'report.pdf');
fs.writeFileSync(pdfPath, MINIMAL_PDF);
const logs: string[] = [];
const content = buildMultimodalContent(
`검토 자료\n[File: report.pdf → ${pdfPath}]`,
(message) => logs.push(message),
);
expect(content).toEqual([
{ type: 'text', text: '검토 자료' },
{ type: 'text', text: 'Document evidence: report.pdf' },
{
type: 'document',
title: 'report.pdf',
source: {
type: 'base64',
media_type: 'application/pdf',
data: MINIMAL_PDF.toString('base64'),
},
},
]);
expect(logs).toContain(
`Added document block: ${pdfPath} (application/pdf)`,
);
});
it('loads text file tags as Claude text document blocks', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-text-doc-'));
cleanupDirs.push(dir);
const textPath = path.join(dir, 'notes.md');
fs.writeFileSync(textPath, '# Notes\nEvidence text');
const content = buildMultimodalContent(
`검토 자료\nMEDIA:${textPath}`,
() => {
// no-op
},
);
expect(content).toEqual([
{ type: 'text', text: '검토 자료' },
{ type: 'text', text: 'Document evidence: notes.md' },
{
type: 'document',
title: 'notes.md',
source: {
type: 'text',
media_type: 'text/plain',
data: '# Notes\nEvidence text',
},
},
]);
});
});