UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

4 lines • 3.4 kB
import{o as normalizeLowercaseStringOrEmpty}from"./string-coerce-CpTFcSwY.mjs";import{r as truncateUtf16Safe}from"./utf16-slice-DA3Mllbx.mjs";import{t as basenameFromAnyPath}from"./file-name-CEqyw6f8.mjs";import{o as isAudioFileName}from"./mime-ZmYZsEjx.mjs";import{d as normalizeMediaFacts}from"./media-facts-03ZYaqC1.mjs";import{t as getMediaDir}from"./store-path-CwOJyIi3.mjs";import"./store-wPqhY4JR.mjs";import path from"node:path"; //!src/auto-reply/media-note.ts function stripDarwinPrivatePrefix(e){return e.startsWith(`/private/var/`)?e.slice(8):e}function normalizeManagedInboundMediaRef(e){if(!path.isAbsolute(e))return e;const t=stripDarwinPrivatePrefix(path.resolve(getMediaDir()));const n=stripDarwinPrivatePrefix(path.resolve(e));const r=path.join(t,`inbound`);const i=path.relative(r,n);if(!i||i.startsWith(`..`)||path.isAbsolute(i))return e;return`media://inbound/${path.basename(n)}`}function sanitizeInlineMediaNoteValue(e){const t=e?.trim();if(!t)return``;return normalizeManagedInboundMediaRef(t).replace(/[\p{Cc}\]]+/gu,` `).replace(/\s+/g,` `).trim()}function formatMediaAttachedLine(e,t,n){const r=n>1?`[media attached ${t}/${n}: `:`[media attached: `;const i=sanitizeInlineMediaNoteValue(e.path);const a=sanitizeInlineMediaNoteValue(e.contentType??e.kind);const o=a?` (${a})`:``;const s=sanitizeInlineMediaNoteValue(e.url);const c=s&&s!==i?` | ${s}`:``;const l=truncateUtf16Safe(sanitizeInlineMediaNoteValue(basenameFromAnyPath(e.fileName??``)),256);return`${r}${i}${o}${c}${l?` ${JSON.stringify(l)}`:``}]`}const AUDIO_EXTENSIONS_WITHOUT_CANONICAL_MIME=[`.webm`,`.wma`,`.alac`];function isAudioPath(e){if(!e)return false;if(isAudioFileName(e))return true;const t=normalizeLowercaseStringOrEmpty(e);return AUDIO_EXTENSIONS_WITHOUT_CANONICAL_MIME.some(e=>t.endsWith(e))}function isValidAttachmentIndex(e,t){return Number.isSafeInteger(e)&&e>=0&&e<t}function collectTranscribedAudioAttachmentIndices(e,t){const n=/* @__PURE__ */ new Set;for(const r of e.MediaUnderstanding??[])if(r.kind===`audio.transcription`&&isValidAttachmentIndex(r.attachmentIndex,t))n.add(r.attachmentIndex);for(const r of e.MediaUnderstandingDecisions??[]){if(r.capability!==`audio`||r.outcome!==`success`)continue;for(const e of r.attachments)if(e.chosen?.outcome===`success`&&isValidAttachmentIndex(e.attachmentIndex,t))n.add(e.attachmentIndex)}return n}function buildInboundMediaNoteProjection(e){const t=normalizeMediaFacts(e.media);const n=t.flatMap((e,t)=>e.path||e.url?[{fact:e,index:t}]:[]);if(n.length===0)return{media:[],mediaIndexes:[]};const r=collectTranscribedAudioAttachmentIndices(e,t.length);const i=Boolean(e.Transcript?.trim())&&t.length===1;const a=n.filter(e=>{const t=normalizeLowercaseStringOrEmpty(e.fact.contentType??e.fact.kind);const n=t===`audio`||t.startsWith(`audio/`);return!(e.fact.kind===`audio`||isAudioPath(e.fact.path)||n)||!(e.fact.transcribed===true||r.has(e.index)||i&&e.index===0)});if(a.length===0)return{media:[],mediaIndexes:[]};const o=new Set(e.MediaUnderstanding?.flatMap(e=>e.kind===`image.description`?[e.attachmentIndex]:[])??[]);const s=a.map(e=>o.has(e.index)?{...e.fact,hydrationSuppressed:true}:e.fact);const c=a.map(e=>e.index);const l=a.length;const u=a.map((e,t)=>formatMediaAttachedLine(e.fact,t+1,l));if(l>1)u.unshift(`[media attached: ${l} files]`);return{text:u.join(` `),media:s,mediaIndexes:c}}export{buildInboundMediaNoteProjection as t};