openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
102 lines (101 loc) • 5.06 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { n as escapeXml } from "./xml-CkRZ_Nxa.mjs";
import { a as wrapExternalContent } from "./external-content-CGL7l-3s.mjs";
import { t as renderDocumentTruncationNotice } from "./document-extraction-metadata-91GyQK0U.mjs";
import { sanitizeUntrustedFileName } from "@openclaw/fs-safe/advanced";
//#region src/media/file-context.ts
/** Renders sanitized attachment text as a model-visible file block without allowing file-tag injection. */
function renderFileContextBlock(e) {
const t = normalizeOptionalString(e.fallbackName) ?? "attachment";
const n = normalizeOptionalString(typeof e.filename === "string" ? e.filename.replace(/[\r\n\t]+/g, " ") : void 0) ?? "";
const r = sanitizeUntrustedFileName(n, t);
const i = e.content.replace(/<\s*\/\s*file\s*>/gi, "</file>").replace(/<\s*file\b/gi, "<file");
const a = normalizeOptionalString(e.mimeType);
const o = [`name="${escapeXml(r)}"`, a ? `mime="${escapeXml(a)}"` : void 0].filter(Boolean).join(" ");
if (e.surroundContentWithNewlines === false) return `<file ${o}>${i}</file>`;
return `<file ${o}>\n${i}\n</file>`;
}
//#endregion
//#region src/media-understanding/file-attachment-outcomes.ts
const MIME_TYPE = String.raw`([a-z0-9!#$&^_.+-]+/[a-z0-9!#$&^_.+-]+)`;
const HTTP_TOKEN = String.raw`[a-z0-9!#$%&'*+.^_\x60|~-]+`;
const HTTP_QUOTED_STRING = String.raw`"(?:[\t !#-\[\]-~]|\\[\t -~])*"`;
const MIME_PARAMETER = String.raw`[ \t]*;[ \t]*${HTTP_TOKEN}=(?:${HTTP_TOKEN}|${HTTP_QUOTED_STRING})`;
const MIME_TYPE_WITH_OPTIONAL_PARAMS = new RegExp(String.raw`^${MIME_TYPE}(?:${MIME_PARAMETER})*$`, "i");
const MARKER_MIME_MAX_CHARS = 100;
function sanitizeMimeType(e) {
const t = normalizeOptionalString(e);
if (!t) return;
return t.match(MIME_TYPE_WITH_OPTIONAL_PARAMS)?.[1]?.toLowerCase();
}
function markerSafeMime(e) {
const t = sanitizeMimeType(e);
return t && t.length <= MARKER_MIME_MAX_CHARS ? t : void 0;
}
function resolveFileExtractionOutcome(e) {
const t = e.text ?? "";
const n = e.images ?? [];
const r = e.metadata ? { metadata: e.metadata } : {};
if (t.trim() || e.metadata?.textTruncated) return {
kind: "extracted",
text: t,
images: n,
...r
};
return n.length > 0 ? {
kind: "rendered-to-images",
images: n,
...r
} : {
kind: "no-extractable-text",
...r
};
}
function wrapUntrustedAttachmentContent(e) {
return wrapExternalContent(e, {
source: "unknown",
includeWarning: false
});
}
const MARKER_LOCAL_PATH_MAX_CHARS = 300;
const POSIX_ABSOLUTE_PATH = /^\//;
const WINDOWS_ABSOLUTE_PATH = /^[A-Za-z]:\\/;
const MARKER_PATH_SAFE = /^[\p{L}\p{M}\p{N} /\\:._-]+$/u;
function markerSafeLocalPath(e, t = false) {
if (!e || e.length > MARKER_LOCAL_PATH_MAX_CHARS) return;
if (!(POSIX_ABSOLUTE_PATH.test(e) || WINDOWS_ABSOLUTE_PATH.test(e)) && (!t || e.includes("\\") || e.split("/").some((e) => !e || e === "." || e === ".."))) return;
return MARKER_PATH_SAFE.test(e) ? e : void 0;
}
const SKIPPED_FILE_OUTCOME_KINDS = /* @__PURE__ */ new Set([
"unsupported-format",
"policy-rejected",
"read-failure",
"url-sources-disabled"
]);
function isSkippedFileOutcome(e) {
return SKIPPED_FILE_OUTCOME_KINDS.has(e.kind);
}
function renderFileAttachmentOutcome(e, t) {
switch (e.kind) {
case "extracted": return [renderDocumentTruncationNotice(e.metadata), wrapUntrustedAttachmentContent(e.text)].filter(Boolean).join("\n");
case "rendered-to-images": return [renderDocumentTruncationNotice(e.metadata), "[PDF content rendered to images]"].filter(Boolean).join("\n");
case "no-extractable-text": return [renderDocumentTruncationNotice(e.metadata), "[No extractable text]"].filter(Boolean).join("\n");
case "unsupported-format": {
const n = markerSafeMime(e.mime);
const r = n ? `Unsupported document format: ${n}.` : "Unsupported document format.";
const i = markerSafeLocalPath(t?.selfServeLocalPath === false ? void 0 : t?.selfServeLocalPath ?? e.localPath, typeof t?.selfServeLocalPath === "string");
const a = e.mime?.startsWith("application/vnd.openxmlformats-officedocument") ? " (this Office file is a zip archive containing XML)" : "";
return i ? [`[${r} The approved local file path follows as external attachment metadata. Its text is not extracted automatically. Read the file yourself with your tools before answering${a}; do not ask the user to paste the contents.]`, wrapUntrustedAttachmentContent(i)].join("") : `[${r} PDF and plain-text attachments can be read.]`;
}
case "policy-rejected": {
const t = markerSafeMime(e.mime);
return t ? `[Attachment type not allowed: ${t}]` : "[Attachment type not allowed]";
}
case "read-failure": return "[Attachment could not be read]";
case "url-sources-disabled": return "[Attachment skipped: URL file sources are disabled]";
case "claimed-elsewhere": return null;
default: return e;
}
}
//#endregion
export { renderFileContextBlock as a, sanitizeMimeType as i, renderFileAttachmentOutcome as n, resolveFileExtractionOutcome as r, isSkippedFileOutcome as t };