UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

9 lines (7 loc) • 4.21 kB
import{l as isRecord}from"../../record-coerce-BTbqdJ3-.mjs";import{h as readRegularFileSync}from"../../fs-safe-Bxzu1j4h.mjs";import"../../string-coerce-runtime-BYDH9Quw.mjs";import{n as assertNoSymlinkParentsSync}from"../../file-access-runtime-Jlxwf1va.mjs";import{t as buildJsonPluginConfigSchema}from"../../config-schema-BQYgWf1D.mjs";import{t as definePluginEntry}from"../../plugin-entry-DhKiQ4Uu.mjs";import path from"node:path";import{TextDecoder}from"node:util"; //!extensions/agent-guidance/openclaw.plugin.json var id=`agent-guidance`;var name=`Agent Guidance`;var description=`Shares instruction files across selected agents without editing their workspace files.`;var configSchema={"type":`object`,"additionalProperties":false,"required":[`directory`,`agents`],"anyOf":[{"required":[`prepend`],"properties":{"prepend":{"minItems":1}}},{"required":[`append`],"properties":{"append":{"minItems":1}}}],"properties":{"directory":{"type":`string`,"minLength":1,"description":`Absolute, operator-controlled directory outside agent-writable workspaces.`},"agents":{"type":`array`,"minItems":1,"maxItems":64,"uniqueItems":true,"items":{"type":`string`,"minLength":1,"maxLength":128}},"prepend":{"type":`array`,"maxItems":8,"uniqueItems":true,"items":{"type":`string`,"minLength":1,"maxLength":256}},"append":{"type":`array`,"maxItems":8,"uniqueItems":true,"items":{"type":`string`,"minLength":1,"maxLength":256}}}}; //!extensions/agent-guidance/index.ts const MAX_FILES=8;const MAX_FILE_BYTES=16384;const MAX_TOTAL_BYTES=65536;function invalid(e){return/* @__PURE__ */ new Error(`agent-guidance: ${e}`)}function parseConfig(e){if(!isRecord(e))throw invalid(`configuration must be an object`);if(Object.keys(e).some(e=>![`directory`,`agents`,`prepend`,`append`].includes(e)))throw invalid(`unknown configuration field`);if(typeof e.directory!==`string`||!path.isAbsolute(e.directory)||e.directory.includes(`\0`))throw invalid(`directory must be an absolute operator-controlled path`);if(!Array.isArray(e.agents)||e.agents.length===0||e.agents.length>64||!e.agents.every(e=>typeof e===`string`&&Boolean(e.trim())&&e===e.trim()&&e.length<=128)||new Set(e.agents).size!==e.agents.length)throw invalid(`agents must contain 1 to 64 distinct explicit agent IDs`);const t=[];for(const n of[`prepend`,`append`]){const r=e[n]===void 0?[]:e[n];if(!Array.isArray(r)||!r.every(e=>typeof e===`string`&&e.length<=256&&/^[a-zA-Z0-9][a-zA-Z0-9._/-]*$/.test(e)&&e.split(`/`).every(e=>e&&e!==`.`&&e!==`..`)))throw invalid(`${n} must contain relative file names without traversal`);for(const e of r)t.push({name:e,placement:n})}if(t.length===0||t.length>MAX_FILES)throw invalid(`configure 1 to ${MAX_FILES} instruction files in total`);if(new Set(t.map(({name:e})=>e)).size!==t.length)throw invalid(`instruction file names must not repeat across either placement`);return{directory:path.normalize(e.directory),agents:e.agents,files:t}}var agent_guidance_default=definePluginEntry({id,name,description,configSchema:buildJsonPluginConfigSchema(configSchema),register(e){const t=parseConfig(e.pluginConfig);const n={prepend:[],append:[]};let r=0;for(const[e,i]of t.files.entries()){let a;try{const e=path.join(t.directory,i.name);assertNoSymlinkParentsSync({rootDir:path.parse(e).root,targetPath:path.dirname(e),allowMissing:false,requireDirectories:true});const{buffer:n,stat:r}=readRegularFileSync({filePath:e,maxBytes:MAX_FILE_BYTES});if(r.nlink!==1)throw invalid(`unsafe file`);a=new TextDecoder(`utf-8`,{fatal:true}).decode(n);if(!a.trim()||a.includes(`\0`))throw invalid(`invalid instruction text`)}catch{throw invalid(`instruction file ${e+1} could not be loaded; require a readable, nonempty UTF-8 regular file of at most ${MAX_FILE_BYTES} bytes with no symbolic or hard links`)}r+=Buffer.byteLength(a)+(n[i.placement].length?2:0);if(r>MAX_TOTAL_BYTES)throw invalid(`combined instructions exceed ${MAX_TOTAL_BYTES} bytes`);n[i.placement].push(a)}const i={};if(n.prepend.length)i.prependSystemContext=n.prepend.join(` `);if(n.append.length)i.appendSystemContext=n.append.join(` `);const a=new Set(t.agents);e.on(`before_prompt_build`,(e,t)=>t.agentId&&a.has(t.agentId)?i:void 0)}});export{agent_guidance_default as default};