UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

7 lines • 9.56 kB
import{i as createPluginCache,w as withPluginCache}from"../../plugin-cache-B6QVpzXr.mjs";import{r as isPathInside}from"../../path-guards-DGttsOJI.mjs";import{l as pinConfigDir}from"../../utils-Dlk0IYAa.mjs";import{t as applyProcessEnvKeys}from"../../process-env-BMQKxoSL.mjs";import{n as sha256Hex}from"../../node-crypto-DPk8agfR.mjs";import"../../crypto-digest-T8X5EZni.mjs";import{n as assertSandboxStateOwnerPathCurrent}from"../../state-owner-path-CGpPdjBt.mjs";import{n as encodeNativeWorkerFailure}from"../../worker-native-error-B1RuZbYc.mjs";import{r as resolveEffectiveAgentSkillsLimits}from"../../agent-filter-8R8dGJaG.mjs";import{r as resolveSkillFileHost}from"../../skill-file-host-D4Dltnrr.mjs";import{n as workerMutationAssertion}from"../../worker-mutation-authority-CXNUeM4P.mjs";import{r as resolveSkillKey}from"../../frontmatter-CvGHA96F.mjs";import{c as tryRealpath,t as compactPromptSkills}from"../../skill-paths-DEQTiKUZ.mjs";import{r as resolveWorkspaceSkillDirectories}from"../../workspace-skill-roots-CD1E2Fy3.mjs";import{o as resolvePluginSkillRoots,s as resolvePluginSkillRootsFromContributions}from"../../plugin-skills-DC2B4wJj.mjs";import{n as serveWorkerTasks}from"../../worker-task-server-BeWxX5OH.mjs";import{a as resolveSandboxFsPathWithMounts,o as resolveSandboxFsPreparation}from"../../fs-paths-Djl1qCxq.mjs";import{n as mergeSkillRecords,t as appendLowerPrecedenceSkillRecords}from"../../skill-precedence-D3UUmH9e.mjs";import{t as isSkillPromptVisible}from"../../skill-index-BJoqmyxP.mjs";import{n as prepareSkillsForPrompt}from"../../skill-prompt-limits-DcA60GFl.mjs";import{t as readWorkspaceSkillSources}from"../../workspace-skill-source-reader-C2k6RgTi.mjs";import{n as resolveWorkspaceSkillSourcePlan,r as splitSkillSourcePlan}from"../../workspace-skill-sources-ChHSrnQL.mjs";import path from"node:path";import{readDirectoryIdentity}from"@openclaw/fs-safe/advanced";import{FsSafeError}from"@openclaw/fs-safe/errors";import{root}from"@openclaw/fs-safe/root";import{configureFsSafeNative,getFsSafeNativeConfig}from"@openclaw/fs-safe/config"; //!src/infra/worker-fs-safe.ts async function withWorkerFsSafeNativeMode(e,t){const n=getFsSafeNativeConfig();configureFsSafeNative({mode:e});try{return await t()}finally{configureFsSafeNative(n)}} //!src/skills/loading/skill-snapshot-projection.ts function buildSkillSnapshotProjection(e){const{eligible:t,skillFilter:n}=e;const r=t.filter(isSkillPromptVisible);const i=e.eligibility?.remote?.note?.trim();const a=r.map(e=>e.skill);const o=e.config?.skills?.limits;const s=resolveEffectiveAgentSkillsLimits(e.config,e.agentId);const c=prepareSkillsForPrompt({skills:compactPromptSkills(a),maxSkillsInPrompt:o?.maxSkillsInPrompt,maxSkillsPromptChars:s?.maxSkillsPromptChars??o?.maxSkillsPromptChars,remoteNote:i,preserveOrder:e.preserveEntryOrder});const l=new Map(a.map(e=>[e.name,e]));return{prompt:c.prompt,skills:t.map(e=>({name:e.skill.name,source:{filePath:e.skill.filePath,fileHost:resolveSkillFileHost(e.skill)},gatewayFilePath:resolveSkillFileHost(e.skill)===`gateway`?e.skill.filePath:void 0,skillKey:resolveSkillKey(e),primaryEnv:e.metadata?.primaryEnv,requiredEnv:e.metadata?.requires?.env?.slice()})),...n===void 0?{}:{skillFilter:n},...e.skillOverrides?{skillOverrides:e.skillOverrides}:{},...e.eligibility?.nodeSkills?{nodeSkillsEligibility:e.eligibility.nodeSkills}:{},resolvedSkills:c.skills.map(e=>l.get(e.name)),discoverySkills:a,version:e.snapshotVersion,promptFormatVersion:8}} //!src/skills/runtime/discovery.worker.ts function applyEnvironment(e){applyProcessEnvKeys(Object.keys(e),e);pinConfigDir()}function resolveInputPlan(e,t=createPluginCache()){return withPluginCache(t,()=>{const t=e.options;const n=t?.workspaceOnly?[]:e.pluginContributions?resolvePluginSkillRootsFromContributions({contributions:e.pluginContributions,pluginSkillsDir:t?.pluginSkillsDir}):resolvePluginSkillRoots({workspaceDir:e.workspaceDir,config:t?.config,pluginSkillsDir:t?.pluginSkillsDir,acpRuntimeAvailable:e.acpRuntimeAvailable});return resolveWorkspaceSkillSourcePlan(e.workspaceDir,{...t,pluginSkillRoots:n,...e.agentSkillsDirs?{agentSkillsDirs:e.agentSkillsDirs}:{}})})}async function prepareResourceRootIdentity(e){if(e.disableModelInvocation||resolveSkillFileHost(e)===`workspace`||!path.isAbsolute(e.filePath))return;if(e.resourceRootIdentity)return e.resourceRootIdentity;try{const t=await readDirectoryIdentity(`${path.dirname(e.filePath)}${path.sep}.`);return{realPath:t.realPath,dev:t.dev.toString(),ino:t.ino.toString()}}catch(e){return{error:e instanceof Error?e.message:String(e)}}}const runTask=(e,t,n)=>{t?.consumeInput();if(e.kind===`companion`)return(async()=>{const t=await root(e.directoryPath);const n=await t.stat(`.`);if(t.rootReal!==e.identity.realPath||n.exactIdentity?.dev.toString()!==e.identity.dev||n.exactIdentity.ino.toString()!==e.identity.ino)throw new Error(`Skill resource directory changed after preparation`);const{buffer:r}=await t.read(e.relativePath,{maxBytes:e.maxBytes,symlinks:`reject`,hardlinks:`reject`});return{kind:`companion`,buffer:r}})();if(e.kind===`sandbox-path`){n.throwIfCancelled();return{kind:`sandbox-path`,path:resolveSandboxFsPathWithMounts(e.input)}}if(e.kind===`sandbox-projection`)return{kind:`sandbox-projection`,preparation:resolveSandboxFsPreparation(e.input)};if(e.kind===`sandbox-mounts`||e.kind===`sandbox-mount-sources`){if(e.authority&&!t)throw new Error(`Sandbox mount preparation requires its live host channel`);return n.runNativeSection(async()=>{const r=e.authority?workerMutationAssertion(e.authority,()=>t?.notify(`authority`),n.throwIfCancelled):n.throwIfCancelled;const assertCurrent=()=>{n.throwIfCancelled();if(e.stateOwnerPath)assertSandboxStateOwnerPathCurrent(e.stateOwnerPath);r()};try{if(e.kind===`sandbox-mount-sources`){const{ensureReadOnlyWorkspaceSkillMountSources:t}=await import(`../../workspace-mounts-BBOtDToN.mjs`);return{kind:`sandbox-mount-sources`,created:await t(e.input,assertCurrent)}}const{prepareSandboxMountPlanFilesystem:t}=await import(`../../mount-plan.prepare-DhBJHDCB.mjs`);return{kind:`sandbox-mounts`,plan:await t(e.input,assertCurrent)}}catch(e){return{kind:`sandbox-mounts-error`,error:encodeNativeWorkerFailure(e)}}})}if(e.kind===`library`)return import(`../../filesystem.kernel-Cb_sqOpK.mjs`).then(async({executeSkillLibraryFilesystemTask:t})=>({kind:`library`,result:await t(e.input)}));if(e.kind===`sync-filesystem`){if(e.authority&&!t)throw new Error(`Skills synchronization requires its live host channel`);return n.runNativeSection(async()=>{const r=e.authority?workerMutationAssertion(e.authority,()=>t?.notify(`authority`),n.throwIfCancelled):n.throwIfCancelled;const assertCurrent=()=>{n.throwIfCancelled();if(e.stateOwnerPath)assertSandboxStateOwnerPathCurrent(e.stateOwnerPath);r()};try{const{runSkillsSyncFilesystem:n}=await import(`../../workspace-skill-sync-files-DDrndH-z.mjs`);return{kind:`sync-filesystem`,prepared:await n(e.operation,assertCurrent,e=>t?.notify(e))}}catch(e){return{kind:`sync-filesystem-error`,error:encodeNativeWorkerFailure(e),code:e instanceof FsSafeError?e.code:void 0,details:e instanceof FsSafeError?e.details:void 0}}})}if(e.kind===`plan`){applyEnvironment(e.input.environment);return{kind:`plan`,plan:resolveInputPlan(e.input)}}if(e.kind===`project`){applyEnvironment(e.environment);const{resolvedSkills:t,discoverySkills:n,...r}=buildSkillSnapshotProjection(e.input);const i=new Map(e.input.eligible.map((e,t)=>[e.skill,t]));const a=new Set(n);return Promise.all(e.input.eligible.map(async({skill:e})=>a.has(e)?prepareResourceRootIdentity(e):void 0)).then(e=>({kind:`project`,snapshot:r,resolvedIndices:t.map(e=>i.get(e)),discoveryIndices:n.map(e=>i.get(e)),resourceRoots:e}))}if(e.kind===`status`){applyEnvironment(e.environment);return(async()=>{const{readWorkspaceSkillStatusFacts:t}=await import(`../../status-files-D4q6UUnk.mjs`);return{kind:`status`,facts:t(e.input)}})()}if(e.path===void 0)delete process.env.PATH;else process.env.PATH=e.path;const r=createPluginCache();const i=/* @__PURE__ */ new Map;const a=/* @__PURE__ */ new Map;const selectPlan=e=>{const t=JSON.stringify(e);const n=a.get(t)??resolveInputPlan(e,r);a.set(t,n);return n};return(async()=>{const t=[];for(const{key:n,environment:r,selection:a}of e.requests){applyEnvironment(r);const e=a.kind===`planned`?selectPlan(a.plan.input):a.plan?selectPlan(a.plan.input):a.request.sourcePlan;const o={...a.request,sourcePlan:a.kind===`selected`&&a.plan?.scope===`gateway`?splitSkillSourcePlan(e).gatewayPlan:e};const s=JSON.stringify(r);let c=i.get(s);if(!c){c=/* @__PURE__ */ new Map;i.set(s,c)}const l=await readWorkspaceSkillSources(o,c);await Promise.all([...l.entries,...l.executionEntries].map(async({skill:e})=>{e.resourceRootIdentity=await prepareResourceRootIdentity(e)}));const u=mergeSkillRecords(l.entries,n,false);appendLowerPrecedenceSkillRecords(u,mergeSkillRecords(l.executionEntries,n,false),()=>{});const d=/* @__PURE__ */ new Set([...o.sourcePlan.roots.map(e=>e.dir),...o.executionWorkspaceDir?resolveWorkspaceSkillDirectories(o.executionWorkspaceDir).map(e=>e.dir):[]]);const f=[...d].map(e=>tryRealpath(e));for(const{skill:e}of[...l.entries,...l.executionEntries]){const t=tryRealpath(e.baseDir);if(t&&!f.some(e=>e&&isPathInside(e,t)))d.add(t)}t.push({key:n,sources:l,watchDirectories:[...d],plan:a.plan?e:void 0,fingerprint:sha256Hex(JSON.stringify([u,l.runtime]))})}return{kind:`scan`,results:t}})()};serveWorkerTasks((e,t,n)=>`nativeMode`in e?withWorkerFsSafeNativeMode(e.nativeMode,()=>runTask(e,t,n)):runTask(e,t,n));export{};