openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines (4 loc) • 5.73 kB
JavaScript
import{d as normalizeMimeType,u as mimeTypeFromFilePath}from"./mime-ZmYZsEjx.mjs";import{i as shouldLogVerbose,r as logVerbose}from"./globals-l9I53ZKA.mjs";import{t as classifyMediaReferenceSource}from"./media-reference-CVYMjRlF.mjs";import{a as resolveAttachmentKind}from"./attachments.normalize-H6KPb1Ga.mjs";import{t as attachmentClassFromMime}from"./attachment-classify-BWbx74Ae.mjs";import{a as renderFileContextBlock,i as sanitizeMimeType,n as renderFileAttachmentOutcome,r as resolveFileExtractionOutcome,t as isSkippedFileOutcome}from"./file-attachment-outcomes-Dvp0qvlm.mjs";import{t as extractFileContentFromBuffer}from"./input-files-D1wMBrkl.mjs";import{t as resolveFileExtractionLimits}from"./file-extraction-limits-cxP57jw7.mjs";import{c as applyAttachmentMarkerBudget,n as createScopedMediaAttachmentCache,r as normalizeMediaAttachments}from"./runner.attachments-CNy5kvmU.mjs";
//!src/media-understanding/file-context.ts
function attachmentUrlDisplayName(e){try{return new URL(e).pathname.split(`/`).findLast(e=>e.length>0)||void 0}catch{return}}async function extractFileContext(e){const{attachments:t,cache:n,cfg:r,limits:i,skipAttachmentIndexes:a}=e;async function classifyFileAttachment(t){const o=e.assertCurrent;o?.();const s=t.path??(t.url?attachmentUrlDisplayName(t.url):void 0);const c=t.fileName??s;if(a?.has(t.index))return{outcome:{kind:`claimed-elsewhere`}};const l=mimeTypeFromFilePath(s);const u=attachmentClassFromMime(l)===`text`?l:void 0;const d=u?`document`:resolveAttachmentKind(t);if(!u&&(d===`image`||d===`video`||d===`audio`))return{outcome:{kind:`claimed-elsewhere`}};if(!i.allowUrl&&t.url&&!t.path&&!classifyMediaReferenceSource(t.url).isMediaStoreUrl){if(shouldLogVerbose())logVerbose(`media: file attachment skipped (url disabled) index=${t.index}`);return{outcome:{kind:`url-sources-disabled`},filename:c}}let f;try{f=await n.getBuffer({attachmentIndex:t.index,maxBytes:i.maxBytes,timeoutMs:i.timeoutMs,signal:e.signal})}catch(e){if(shouldLogVerbose())logVerbose(`media: file attachment skipped (buffer): ${String(e)}`);return{outcome:{kind:`read-failure`},filename:c}}o?.();const p=t.fileName??f.fileName;const m=f.classification;const h=sanitizeMimeType(m.mime);const g=sanitizeMimeType(normalizeMimeType(t.mime))??h;const _=f.localPath;if(m.class!==`text`&&!(m.class===`document`&&m.mime===`application/pdf`)){if(i.allowedMimesConfigured&&!(h&&i.allowedMimes.has(h)))return{outcome:{kind:`policy-rejected`,mime:h??g},filename:p,mimeType:h??g};return{outcome:{kind:`unsupported-format`,mime:g,..._?{localPath:_}:{}},filename:p,mimeType:g}}if(m.class===`text`&&t.mime&&normalizeMimeType(t.mime)!==m.mime)logVerbose(`media: MIME override from "${t.mime}" to "${m.mime}" for index=${t.index}`);if(!h){if(shouldLogVerbose())logVerbose(`media: file attachment skipped (unknown mime) index=${t.index}`);return{outcome:{kind:`unsupported-format`},filename:p}}const v=new Set(i.allowedMimes);if(!i.allowedMimesConfigured&&m.class===`text`)v.add(h);if(!v.has(h)){if(shouldLogVerbose())logVerbose(`media: file attachment skipped (unsupported mime ${h}) index=${t.index}`);return{outcome:i.allowedMimesConfigured?{kind:`policy-rejected`,mime:h}:{kind:`unsupported-format`,mime:h,..._?{localPath:_}:{}},filename:p,mimeType:h}}let y;try{const{allowedMimesConfigured:n,...a}=i;y=await extractFileContentFromBuffer({buffer:h===`application/pdf`?Buffer.from(f.buffer):f.buffer,filename:f.fileName,limits:{...a,allowedMimes:v},config:r,classification:m,mimeType:t.mime,signal:e.signal})}catch(e){if(shouldLogVerbose())logVerbose(`media: file attachment skipped (extract): ${String(e)}`);return{outcome:{kind:`read-failure`},filename:p,mimeType:h}}o?.();return{outcome:resolveFileExtractionOutcome({text:y?.text?.trim(),images:y?.images,metadata:y?.metadata}),filename:p,mimeType:h}}const o=[];const s=[];const c=[];for(const r of t){const{outcome:t,filename:i,mimeType:a}=await classifyFileAttachment(r).finally(()=>n.releaseBuffer(r.index));e.assertCurrent?.();if(t.kind===`extracted`||t.kind===`rendered-to-images`)s.push(...t.images.map(e=>({...e,attachmentIndex:r.index})));const l=renderFileAttachmentOutcome(t,{selfServeLocalPath:e.selfServePathsEnabled?void 0:false});if(l===null)continue;const renderBlock=e=>renderFileContextBlock({filename:i,fallbackName:`file-${r.index+1}`,mimeType:a,content:e});const u=renderBlock(l);o.push({text:u,consumesMarkerBudget:isSkippedFileOutcome(t)});if(t.kind===`unsupported-format`&&t.localPath){const e=renderFileAttachmentOutcome(t,{selfServeLocalPath:false});const n=renderFileAttachmentOutcome(t);if(e&&n)c.push({attachmentIndex:r.index,fallback:renderBlock(e),render:e=>{const n=renderFileAttachmentOutcome(t,e?{selfServeLocalPath:e}:void 0);return n?renderBlock(n):void 0}})}}return{blocks:o,images:s,localPathSelfServeUpgrades:c}}async function prepareFileContextFromMedia(e){return await renderInboundDocumentContext({ctx:{media:[...e.media],Provider:e.channelId,AccountId:e.accountId},cfg:e.config,workspaceDir:e.workspaceDir,maxChars:e.maxChars,assertCurrent:e.assertCurrent,signal:e.signal})}async function renderInboundDocumentContext(e){e.assertCurrent?.();const{ctx:t,cfg:n}=e;const r=resolveFileExtractionLimits(n);const i=normalizeMediaAttachments(t);const a=createScopedMediaAttachmentCache(i,e);try{const t=await extractFileContext({attachments:i,cache:a,cfg:n,limits:e.maxChars===void 0?r:{...r,maxChars:Math.min(r.maxChars,e.maxChars)},selfServePathsEnabled:false,assertCurrent:e.assertCurrent,signal:e.signal});e.assertCurrent?.();return{text:applyAttachmentMarkerBudget(t.blocks).join(`
`),images:t.images}}finally{await a.cleanup()}}export{prepareFileContextFromMedia as n,renderInboundDocumentContext as r,extractFileContext as t};