fix: load supported document attachments (#205)
This commit is contained in:
@@ -2,6 +2,7 @@ import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
import { Attachment } from 'discord.js';
|
||||
import { isModelDocumentPath } from 'ejclaw-runners-shared';
|
||||
|
||||
import { DATA_DIR } from '../config.js';
|
||||
import { logger } from '../logger.js';
|
||||
@@ -63,6 +64,15 @@ function isTextAttachment(att: Attachment, contentType: string): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
function isStructuredDocumentAttachment(
|
||||
att: Attachment,
|
||||
contentType: string,
|
||||
): boolean {
|
||||
if (contentType === 'application/pdf') return true;
|
||||
if (contentType.startsWith('text/')) return true;
|
||||
return isModelDocumentPath(att.name || '');
|
||||
}
|
||||
|
||||
function formatAttachmentReference(
|
||||
label: 'Audio' | 'File' | 'Image' | 'Video',
|
||||
att: Attachment,
|
||||
@@ -121,6 +131,9 @@ export async function describeDownloadedAttachment(
|
||||
attachmentDefaultExtension(contentType),
|
||||
);
|
||||
const reference = formatAttachmentReference(label, att, filePath);
|
||||
if (label === 'File' && isStructuredDocumentAttachment(att, contentType)) {
|
||||
if (!isTextAttachment(att, contentType)) return reference;
|
||||
}
|
||||
if (!isTextAttachment(att, contentType)) {
|
||||
if (label === 'Image') return reference;
|
||||
return formatUnavailableAttachmentReference(
|
||||
|
||||
@@ -681,7 +681,7 @@ describe('attachments', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('stores file attachment with a visible unsupported marker', async () => {
|
||||
it('stores PDF file attachment as a structured document reference', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new DiscordChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
@@ -707,9 +707,7 @@ describe('attachments', () => {
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'dc:1234567890123456',
|
||||
expect.objectContaining({
|
||||
content: expect.stringMatching(
|
||||
/^\[File unavailable: report\.pdf → .+\.pdf — application\/pdf is not loaded as structured model input\]$/,
|
||||
),
|
||||
content: expect.stringMatching(/^\[File: report\.pdf → .+\.pdf\]$/),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user