openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4 kB
JavaScript
import{n as containsAsciiControlCharacter}from"./string-normalization-D_oBUpO3.mjs";import{r as resolveStateDir}from"./state-dir-BwIClCab.mjs";import{x as root}from"./fs-safe-Bxzu1j4h.mjs";import"./paths-D_FOvGon.mjs";import{t as CANVAS_DOCUMENTS_PATH}from"./constants--GNaxU9G.mjs";import{n as WIDGET_HTML_MAX_UTF8_BYTES}from"./canvas-limits-DXBtyMMW.mjs";import path from"node:path";import{randomUUID}from"node:crypto";import fs from"node:fs/promises";
//!src/canvas/documents.ts
const CANVAS_DOCUMENT_MANIFEST_MAX_BYTES=2097152;const WIDGET_MAX_PER_SCOPE=32;function normalizeLogicalPath(e){const t=e.replaceAll(`\\`,`/`).replace(/^\/+/,``).split(`/`).filter(Boolean);if(t.length===0||t.some(e=>e===`.`||e===`..`||e.includes(`:`)||containsAsciiControlCharacter(e)))throw new Error(`canvas document logicalPath invalid`);return t.join(`/`)}function normalizeCanvasDocumentId(e){const t=e.trim();if(!t||t===`.`||t===`..`||!/^[A-Za-z0-9._-]+$/.test(t))throw new Error(`canvas document id invalid`);return t}function resolveCanvasDocumentsDir(e=resolveStateDir()){return path.resolve(e,`canvas`,`documents`)}async function readCanvasDocumentHtmlSource(e){const t=normalizeCanvasDocumentId(e);const n=await root(resolveCanvasDocumentsDir(),{maxBytes:CANVAS_DOCUMENT_MANIFEST_MAX_BYTES});const r=await n.readJson(`${t}/manifest.json`);if(r.id!==t||typeof r.localEntrypoint!==`string`)throw new Error(`canvas document has no local entrypoint: ${t}`);const i=normalizeLogicalPath(r.localEntrypoint);if(!i.toLowerCase().endsWith(`.html`))throw new Error(`canvas document entrypoint is not HTML: ${t}`);return{html:await n.readText(`${t}/${i}`,{maxBytes:WIDGET_HTML_MAX_UTF8_BYTES}),...r.cspSandbox===`scripts`?{cspSandbox:`scripts`}:{}}}async function pruneCanvasDocumentsForScope(e){const t=await fs.readdir(e.documentsDir,{withFileTypes:true});const n=(await Promise.all(t.filter(e=>e.isDirectory()).map(async t=>{try{const n=JSON.parse(await fs.readFile(path.join(e.documentsDir,t.name,`manifest.json`),`utf8`));if(n.retentionScope!==e.retentionScope||typeof n.createdAt!==`string`)return null;return{id:t.name,createdAt:n.createdAt}}catch{return null}}))).filter(e=>e!==null);const r=Math.max(0,n.length-WIDGET_MAX_PER_SCOPE);const i=n.toSorted((e,t)=>e.createdAt.localeCompare(t.createdAt)||e.id.localeCompare(t.id)).slice(0,r);await Promise.all(i.map(t=>fs.rm(path.join(e.documentsDir,t.id),{recursive:true,force:true})))}function parseCanvasDocumentHttpPath(e){const t=e.trim();const n=`${CANVAS_DOCUMENTS_PATH}/`;if(!t.startsWith(n))return null;const r=t.replace(/[?#].*$/,``).slice(n.length);try{const[e,...t]=r.split(`/`).filter(Boolean).map(decodeURIComponent);if(!e||t.length===0)return null;return{documentId:normalizeCanvasDocumentId(e),entrypoint:normalizeLogicalPath(t.join(`/`))}}catch{return null}}function resolveCanvasHttpPathToLocalPath(e){const t=parseCanvasDocumentHttpPath(e);if(!t)return null;const n=resolveCanvasDocumentsDir();const r=path.resolve(n,t.documentId,t.entrypoint);return r.startsWith(`${n}${path.sep}`)?r:null}async function createCanvasDocument(e){const t=`cv_${randomUUID().replaceAll(`-`,``)}`;const n=path.join(resolveCanvasDocumentsDir(),t);await fs.mkdir(n,{recursive:true});const r=await root(n);await r.write(`index.html`,e.html);const i={id:t,kind:`html_bundle`,...e.title?.trim()?{title:e.title.trim()}:{},surface:`assistant_message`,...e.retentionScope?{retentionScope:e.retentionScope}:{},...e.cspSandbox?{cspSandbox:e.cspSandbox}:{},createdAt:(/* @__PURE__ */ new Date()).toISOString(),entryUrl:`${CANVAS_DOCUMENTS_PATH}/${encodeURIComponent(t)}/index.html`,localEntrypoint:`index.html`,assets:[]};await r.writeJson(`manifest.json`,i,{space:2});if(e.retentionScope)await pruneCanvasDocumentsForScope({documentsDir:resolveCanvasDocumentsDir(),retentionScope:e.retentionScope});return i}export{resolveCanvasHttpPathToLocalPath as a,resolveCanvasDocumentsDir as i,parseCanvasDocumentHttpPath as n,readCanvasDocumentHtmlSource as r,createCanvasDocument as t};