|
| 1 | +import path from 'path' |
| 2 | +import { detectMimeType, isLikelyTextFile } from '@/presenter/filePresenter/mime' |
| 3 | + |
| 4 | +const TEXT_LIKE_MIMES = new Set([ |
| 5 | + 'application/json', |
| 6 | + 'application/xml', |
| 7 | + 'application/javascript', |
| 8 | + 'application/x-javascript', |
| 9 | + 'application/typescript', |
| 10 | + 'application/x-typescript', |
| 11 | + 'application/x-sh' |
| 12 | +]) |
| 13 | + |
| 14 | +const DOCUMENT_MIMES = new Set([ |
| 15 | + 'application/pdf', |
| 16 | + 'application/msword', |
| 17 | + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 18 | + 'application/vnd.ms-powerpoint', |
| 19 | + 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
| 20 | + 'application/vnd.ms-excel', |
| 21 | + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 22 | + 'application/vnd.oasis.opendocument.spreadsheet' |
| 23 | +]) |
| 24 | + |
| 25 | +const ALWAYS_BINARY_MIMES = new Set([ |
| 26 | + 'application/octet-stream', |
| 27 | + 'application/zip', |
| 28 | + 'application/x-zip', |
| 29 | + 'application/gzip', |
| 30 | + 'application/x-gzip', |
| 31 | + 'application/x-7z-compressed', |
| 32 | + 'application/x-rar-compressed', |
| 33 | + 'application/wasm' |
| 34 | +]) |
| 35 | + |
| 36 | +export function isTextLikeMime(mimeType: string): boolean { |
| 37 | + return mimeType.startsWith('text/') || TEXT_LIKE_MIMES.has(mimeType) |
| 38 | +} |
| 39 | + |
| 40 | +export function isDocumentMime(mimeType: string): boolean { |
| 41 | + return DOCUMENT_MIMES.has(mimeType) |
| 42 | +} |
| 43 | + |
| 44 | +export async function shouldRejectAcpTextRead(filePath: string): Promise<{ |
| 45 | + reject: boolean |
| 46 | + mimeType: string |
| 47 | +}> { |
| 48 | + const mimeType = await detectMimeType(filePath) |
| 49 | + |
| 50 | + if (isTextLikeMime(mimeType)) { |
| 51 | + return { reject: false, mimeType } |
| 52 | + } |
| 53 | + |
| 54 | + if (mimeType === 'application/octet-stream') { |
| 55 | + const likelyText = await isLikelyTextFile(filePath) |
| 56 | + return { reject: !likelyText, mimeType } |
| 57 | + } |
| 58 | + |
| 59 | + return { reject: true, mimeType } |
| 60 | +} |
| 61 | + |
| 62 | +export async function shouldRejectAgentBinaryRead( |
| 63 | + filePath: string, |
| 64 | + mimeType: string |
| 65 | +): Promise<boolean> { |
| 66 | + if (mimeType.startsWith('image/')) { |
| 67 | + return false |
| 68 | + } |
| 69 | + |
| 70 | + if (isTextLikeMime(mimeType) || isDocumentMime(mimeType) || mimeType === 'text/csv') { |
| 71 | + return false |
| 72 | + } |
| 73 | + |
| 74 | + if ( |
| 75 | + ALWAYS_BINARY_MIMES.has(mimeType) || |
| 76 | + mimeType.startsWith('audio/') || |
| 77 | + mimeType.startsWith('video/') |
| 78 | + ) { |
| 79 | + return true |
| 80 | + } |
| 81 | + |
| 82 | + if (mimeType === 'application/octet-stream') { |
| 83 | + return !(await isLikelyTextFile(filePath)) |
| 84 | + } |
| 85 | + |
| 86 | + return false |
| 87 | +} |
| 88 | + |
| 89 | +export function buildBinaryReadGuidance( |
| 90 | + filePath: string, |
| 91 | + mimeType: string, |
| 92 | + mode: 'agent' | 'acp' |
| 93 | +): string { |
| 94 | + const fileName = path.basename(filePath) |
| 95 | + const shared = `Cannot read "${fileName}" as plain text (detected MIME: ${mimeType}).` |
| 96 | + |
| 97 | + if (mode === 'acp') { |
| 98 | + return [ |
| 99 | + shared, |
| 100 | + '`fs/read_text_file` only supports text files.', |
| 101 | + 'Use OCR/image tooling for images, and convert or extract PDFs/binary formats before reading them as text.' |
| 102 | + ].join(' ') |
| 103 | + } |
| 104 | + |
| 105 | + return [ |
| 106 | + shared, |
| 107 | + 'Use image OCR/summary for images, or a dedicated conversion/extraction tool or skill script for binary formats.' |
| 108 | + ].join(' ') |
| 109 | +} |
0 commit comments