UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

178 lines (177 loc) • 6.61 kB
import { n as runtimeProcessEntrypoints } from "./runtime-process-entrypoints-qiyJvDDT.mjs"; import { a as resolveRuntimeWorkerUrl, o as runtimeNeedsTypeScriptLoader, t as resolveRuntimeArgs } from "./runtime-worker-url-DRDkoS4J.mjs"; import "./workspace-bootstrap-policy-D7EJ00Ub.mjs"; import "./workspace-access-CnjZBg1i.mjs"; import { fileURLToPath } from "node:url"; import path from "node:path"; //#region src/agents/workspace-attachment-preparer.ts /** Reuse an owned filesystem transport for admitted inputs; this does not establish a backend. */ function createWorkspaceAttachmentPreparer(e) { if (!path.posix.isAbsolute(e.remoteRoot) && !path.win32.isAbsolute(e.remoteRoot)) throw new Error("Attachment workspace root must be absolute"); return async (t, n) => { const { prepareWorkspaceAttachments: r } = await import("./workspace-attachments-DKEW9jmV.mjs"); n(); return await r({ ...e, turn: t, assertCurrent: n }); }; } //#endregion //#region src/agents/workspace-memory-client.ts /** Client for the packaged, same-version Memory file worker, independent of its transport. */ function createWorkspaceMemoryFileClient(e) { const t = path.resolve(e.workspaceDir); const n = path.posix.resolve(e.remoteWorkspaceDir); const outside = (e, t) => e === ".." || e.startsWith(`..${t.sep}`) || t.isAbsolute(e); const host = (e) => { const r = path.relative(t, e); return outside(r, path) ? e : path.posix.join(n, ...r.split(path.sep)); }; const gateway = (e) => { const r = path.posix.relative(n, e); return outside(r, path.posix) ? e : path.join(t, ...r.split("/")); }; const extra = (e = []) => e.map((e) => { const mapped = (e) => path.isAbsolute(e) ? host(e) : e; return typeof e === "string" ? mapped(e) : { ...e, path: mapped(e.path) }; }); async function call(t, n) { e.signal.throwIfAborted(); n?.(); let r; try { const i = await e.request(JSON.stringify(t), e.signal, n); e.signal.throwIfAborted(); r = JSON.parse(i); } catch (e) { if (t.operation === "maintenance" && t.method === "commitContent") throw Object.assign(new Error("Memory write outcome is unknown", { cause: e }), { publication: "uncertain" }); throw e; } if (r.error) throw Object.assign(new Error(r.error.message), { ...r.error.code ? { code: r.error.code } : {}, ...r.error.name ? { name: r.error.name } : {}, ...["uncertain", "committed"].includes(r.error.publication ?? "") ? { publication: r.error.publication } : {} }); return r.result; } const maintain = (e, ...t) => call({ operation: "maintenance", method: e, args: t }); return { maintenance: { readFile: async (e) => Buffer.from(await maintain("readFile", host(e)), "base64"), stat: (e, t) => maintain("stat", host(e), t), listDirectory: (e) => maintain("listDirectory", host(e)), mkdir: (e) => maintain("mkdir", host(e)), rename: (e, t) => maintain("rename", host(e), host(t)), resolveWritePath: async (e) => gateway(await maintain("resolveWritePath", host(e))), commitContent: (e) => maintain("commitContent", { ...e, filePath: host(e.filePath) }), resolveDreamsPath: async () => gateway(await maintain("resolveDreamsPath")), readDreams: (e) => maintain("readDreams", host(e)), writeDreams: (e, t, n) => call({ operation: "maintenance", method: "writeDreams", args: [host(e), t] }, n), replaceReport: (e, t) => maintain("replaceReport", host(e), t), appendCorpus: (e, t, n) => call({ operation: "maintenance", method: "appendCorpus", args: [host(e), t] }, n) }, assertCurrent: () => e.signal.throwIfAborted(), async listFiles(e, t, n, r) { const i = await call({ operation: "list", extraPaths: extra(t), multimodal: n }); for (const e of i.skipped) r?.(gateway(e)); return i.files.map(gateway); }, async inspectFile(e, t, n) { const r = await call({ operation: "inspect", filePath: host(e), multimodal: n }); return r ? { ...r, absPath: gateway(r.absPath) } : null; }, readFile({ workspaceDir: e, extraPaths: t, relPath: n, ...r }) { return call({ operation: "read", params: { ...r, extraPaths: extra(t), relPath: path.isAbsolute(n) ? host(n) : n } }); }, readForIndexing: (e) => call({ operation: "readForIndexing", filePath: host(e) }), buildMultimodalChunk: (e) => call({ operation: "multimodal", entry: { ...e, absPath: host(e.absPath) } }), async watch(t, n, r) { const i = AbortSignal.any([e.signal, r]); i.throwIfAborted(); const a = { agentId: t.agentId, settings: { extraPaths: extra(t.settings.extraPaths), multimodal: t.settings.multimodal, sync: { watchDebounceMs: t.settings.sync.watchDebounceMs } } }; await e.subscribe(`${JSON.stringify(a)}\n`, (e) => { i.throwIfAborted(); const t = JSON.parse(e); if (t !== "change" && t !== "unavailable") throw new Error("Invalid Memory change notification"); n(t); }, i); if (!i.aborted) n("unavailable"); } }; } //#endregion //#region src/agents/workspace-worker.ts /** Resolve the installed or source worker without adapters depending on core file layout. */ function resolveWorkspaceWorkerUrl(e) { return resolveRuntimeWorkerUrl(e === "memory" ? runtimeProcessEntrypoints.workspaceMemory : runtimeProcessEntrypoints.workspaceSkills); } /** Preserve the source host's config when a worker imports source plugins. */ function resolveWorkspaceWorkerExecArgv(e) { if (!runtimeNeedsTypeScriptLoader(fileURLToPath(resolveWorkspaceWorkerUrl(e))) && !runtimeNeedsTypeScriptLoader(fileURLToPath(import.meta.url))) return resolveRuntimeArgs(); const t = "import { register } from " + JSON.stringify(import.meta.resolve("tsx/esm/api")) + "; register({ tsconfig: " + JSON.stringify(fileURLToPath(new URL("../../tsconfig.json", import.meta.url))) + " });"; return ["--import", `data:text/javascript,${encodeURIComponent(t)}`]; } function resolveWorkspaceWorkerArgv(e) { return [...resolveWorkspaceWorkerExecArgv(e), fileURLToPath(resolveWorkspaceWorkerUrl(e))]; } /** Reuse the native bounded Skill reader with host-owned per-file authorization. */ async function readWorkspaceSkillResources(...e) { const { readSkillResourceFiles: t } = await import("./resources-B7nByr9f.mjs"); return t(...e); } //#endregion export { createWorkspaceMemoryFileClient as a, resolveWorkspaceWorkerUrl as i, resolveWorkspaceWorkerArgv as n, createWorkspaceAttachmentPreparer as o, resolveWorkspaceWorkerExecArgv as r, readWorkspaceSkillResources as t };