@xeno-corporation/xeno-agent-sdk 0.6.1 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
- import{existsSync as w,mkdirSync as T,readFileSync as v,readdirSync as k,writeFileSync as B}from"fs";import{homedir as I}from"os";import{basename as N,dirname as E,join as d,parse as O,resolve as C}from"path";import R from"gray-matter";var M=/^[a-z][a-z0-9_-]{0,63}$/;function L(){return process.env.XENO_AGENT_HOME?.trim()||d(I(),".xeno-agent")}function x(e=L()){return d(e,"agents")}function S(e){return C(e)}function F(e){let n=E(e);return n===e?e:n}function _(e=process.cwd()){let n=S(e),t=[],o=n;for(;;){let i=d(o,".xeno","agents");w(i)&&t.push(i);let r=F(o);if(r===o)break;o=r}return t.reverse()}function z(e=process.cwd()){return d(S(e),".xeno","agents")}function H(e){return w(e)?k(e,{withFileTypes:!0}).filter(n=>n.isFile()&&n.name.toLowerCase().endsWith(".md")).map(n=>d(e,n.name)).sort((n,t)=>n.localeCompare(t)):[]}function A(e){if(typeof e!="string")return;let n=e.trim();return n.length>0?n:void 0}function h(e){return Array.isArray(e)?e.map(n=>String(n).trim()).filter(Boolean):typeof e=="string"?e.split(",").map(n=>n.trim()).filter(Boolean):[]}function U(e){return O(N(e)).name.toLowerCase()}function b(e){return M.test(e)}function G(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function J(e){if(typeof e!="string")return;let n=e.trim().toLowerCase();if(n==="inherit")return"inherit";if(n==="none"||n==="off"||n==="disabled")return"none";if(n==="read"||n==="readonly"||n==="read-only")return"readOnly";if(n==="default"||n==="standard")return"default";if(n==="full"||n==="fullaccess"||n==="full-access")return"fullAccess"}function q(e){if(e===void 0)return;if(typeof e=="string"||Array.isArray(e)){let r=h(e);return r.length>0?{allow:r}:void 0}if(!G(e))return;let n=h(e.allow),t=h(e.deny),o=J(e.mode),i={};return o&&(i.mode=o),n.length>0&&(i.allow=n),t.length>0&&(i.deny=t),Object.keys(i).length>0?i:void 0}function V(e){let n=[];return b(e.name)||n.push(`Invalid agent name "${e.name}". Use 1-64 lowercase letters, digits, hyphens, or underscores, starting with a letter.`),e.prompt.trim().length===0&&n.push("Agent definition body is empty."),n}function W(e,n,t){let o=v(e,"utf8"),i=R(o),r=i.data,l=U(e),a=A(r.name)?.toLowerCase()??l,u=i.content.trim(),p=V({name:a,prompt:u});if(p.length>0)throw new Error(p.join(" "));let f=A(r.description),s=A(r.model),c=q(r.tools);return{name:a,scope:n,path:e,...f?{description:f}:{},...s?{model:s}:{},tags:h(r.tags),prompt:u,active:!0,priority:t,...c?{toolPolicy:c}:{}}}function m(e,n,t,o,i){for(let r of H(e))try{o.push(W(r,n,t))}catch(l){i.push({path:r,scope:n,message:l instanceof Error?l.message:String(l)})}}function X(e){let n=new Map;for(let t of e){let o=n.get(t.name);(!o||t.priority>o.priority)&&n.set(t.name,t)}return n}function Y(e,n){let t=n.get(e.name),o=t?.path===e.path;return{name:e.name,scope:e.scope,path:e.path,...e.description?{description:e.description}:{},...e.model?{model:e.model}:{},tags:e.tags,prompt:e.prompt,active:o,...e.toolPolicy?{toolPolicy:e.toolPolicy}:{},...!o&&t?{shadowedBy:{name:t.name,scope:t.scope,path:t.path}}:{}}}function j(e=process.cwd(),n={}){let t=n.cwd??e,o=n.userDir??x(n.agentHome),i=n.projectDirs??_(t),r=n.pluginDirs??[],l=n.builtInDirs??[],a=[],u=[];n.includeUser!==!1&&m(o,"user",0,a,u),n.includeProject!==!1&&i.forEach((s,c)=>m(s,"project",100+c,a,u)),n.includePlugins&&r.forEach((s,c)=>m(s,"plugin",1e3+c,a,u)),n.includeBuiltIns&&l.forEach((s,c)=>m(s,"built-in",2e3+c,a,u));let p=X(a),f=a.map(s=>Y(s,p));return f.sort((s,c)=>{if(s.active!==c.active)return s.active?-1:1;let P=s.name.localeCompare(c.name);return P!==0?P:s.path.localeCompare(c.path)}),{definitions:f,issues:u,searchPaths:{...n.includeUser!==!1?{user:o}:{},project:n.includeProject===!1?[]:i,plugin:n.includePlugins?r:[],builtIn:n.includeBuiltIns?l:[]}}}function oe(e,n=process.cwd(),t={}){let o=e.trim().toLowerCase(),i=j(n,t);return{definition:i.definitions.find(r=>r.active&&r.name===o)??null,scan:i}}var D=class{constructor(n={}){this.options=n}scan(n=this.options.cwd??process.cwd()){return j(n,this.options)}},$=class{constructor(n=new D){this.loader=n}resolve(n,t=process.cwd()){let o=this.loader.scan(t),i=n.trim().toLowerCase();return{definition:o.definitions.find(r=>r.active&&r.name===i)??null,scan:o}}};function y(e){if(!e||e.length===0)return;let n=Array.from(new Set(e.map(t=>t.trim()).filter(Boolean)));return n.length>0?n:void 0}function K(e,n){if(!e&&!n)return;if(!e)return y(n);if(!n)return y(e);let t=new Set(n);return y(e.filter(o=>t.has(o)))}function Q(e,n){let t={none:0,readOnly:1,default:2,fullAccess:3,inherit:4},o=n??"inherit",i=e??"inherit";return t[o]<=t[i]?o:i}function ie(e,n={}){let t=e.toolPolicy??{},o={mode:Q(t.mode,n.mode)},i=K(n.allow,t.allow),r=y([...n.deny??[],...t.deny??[]]);return i&&(o.allow=i),r&&(o.deny=r),o}function re(e){return{name:`custom-agent:${e.name}`,order:20,provide(){let n=["","","# Custom Agent",`Active agent: ${e.name}`,`Definition scope: ${e.scope}`,`Definition path: ${e.path}`];return e.description&&n.push(`Description: ${e.description}`),n.push("",e.prompt),n.join(`
2
- `)}}}function se(e){return{name:e.name,scope:e.scope,path:e.path,...e.description?{description:e.description}:{},...e.model?{model:e.model}:{},tags:e.tags,active:e.active,...e.toolPolicy?{toolPolicy:e.toolPolicy}:{},...e.shadowedBy?{shadowedBy:e.shadowedBy}:{}}}function g(e,n){return e.length>=n?e:e+" ".repeat(n-e.length)}function ce(e,n){let t=[];if(e.length===0)t.push("No custom agent definitions found.");else{t.push(`${g("name",22)} ${g("scope",8)} ${g("status",9)} description`),t.push(`${"-".repeat(22)} ${"-".repeat(8)} ${"-".repeat(9)} ${"-".repeat(32)}`);for(let o of e){let i=o.active?"active":"shadowed";t.push(`${g(o.name,22)} ${g(o.scope,8)} ${g(i,9)} ${o.description??""}`)}}if(n.length>0){t.push("","Invalid definitions:");for(let o of n)t.push(`- ${o.path}: ${o.message}`)}return t.join(`
1
+ import{existsSync as D,mkdirSync as M,readFileSync as B,readdirSync as N,writeFileSync as F}from"fs";import{homedir as C}from"os";import{basename as W,dirname as Y,join as g,parse as U,resolve as _}from"path";function $(n){let t=O(n);return{frontmatter:t.frontmatter,content:t.content.trim()}}function O(n){let t=n.replace(/^\uFEFF/,"");if(!t.startsWith("---"))return{frontmatter:{},content:n};let r=t.search(/\r?\n/);if(r<0||t.slice(0,r).trim()!=="---")return{frontmatter:{},content:n};let e=t.slice(r+(t[r]==="\r"?2:1)),i=e.match(/(?:^|\r?\n)---[ \t]*(?:\r?\n|$)/);if(!i||i.index===void 0)throw new Error("YAML frontmatter is not closed.");let o=e.slice(0,i.index),s=i[0],l=i.index+s.length;return{frontmatter:S(o),content:e.slice(l)}}function S(n){let t={},r=n.split(/\r?\n/);for(let e=0;e<r.length;e+=1){let i=r[e];if(!i.trim()||i.trimStart().startsWith("#"))continue;if(/^\s/.test(i))throw new Error(`Unexpected indentation at line ${e+1}.`);let o=L(i,e+1);if(o.key==="<<")throw new Error("YAML merge keys are not supported.");if(o.value!==""){t[o.key]=b(o.value,e+1);continue}let s=R(r,e+1);t[o.key]=s.value,e=s.nextIndex-1}return t}function R(n,t){let r=[],e=t;for(;e<n.length;e+=1){let o=n[e];if(!o.trim()){r.push(o);continue}if(!/^\s/.test(o))break;r.push(o.replace(/^ {2}/,""))}let i=r.filter(o=>o.trim()&&!o.trimStart().startsWith("#"));return i.length===0?{value:{},nextIndex:e}:i.every(o=>o.trimStart().startsWith("- "))?{value:i.map((o,s)=>b(o.trimStart().slice(2),t+s+1)),nextIndex:e}:{value:S(r.join(`
2
+ `)),nextIndex:e}}function L(n,t){let r=n.indexOf(":");if(r<=0)throw new Error(`Invalid YAML line ${t}.`);let e=n.slice(0,r).trim(),i=n.slice(r+1).trim();if(!e)throw new Error(`Invalid YAML key at line ${t}.`);return{key:e,value:i}}function b(n,t){if(n==="")return"";if(n==="[]")return[];if(n==="{}")return{};if(n==="true")return!0;if(n==="false")return!1;if(n==="null"||n==="~")return null;if(/^-?\d+(?:\.\d+)?$/.test(n))return Number(n);if(n.startsWith('"'))return x(n,t);if(n.startsWith("'")){if(!n.endsWith("'"))throw new Error(`Unclosed quoted scalar at line ${t}.`);return n.slice(1,-1).replace(/''/g,"'")}if(n.startsWith("[")||n.startsWith("{"))return x(n,t);if(n.includes("[")||n.includes("]")||n.includes("{")||n.includes("}"))throw new Error(`Unsupported YAML scalar at line ${t}.`);return n}function x(n,t){try{return JSON.parse(n)}catch{throw new Error(`Invalid YAML scalar at line ${t}.`)}}var v=/^[a-z][a-z0-9_-]{0,63}$/;function z(){return process.env.XENO_AGENT_HOME?.trim()||g(C(),".xeno-agent")}function j(n=z()){return g(n,"agents")}function T(n){return _(n)}function H(n){let t=Y(n);return t===n?n:t}function J(n=process.cwd()){let t=T(n),r=[],e=t;for(;;){let i=g(e,".xeno","agents");D(i)&&r.push(i);let o=H(e);if(o===e)break;e=o}return r.reverse()}function q(n=process.cwd()){return g(T(n),".xeno","agents")}function G(n){return D(n)?N(n,{withFileTypes:!0}).filter(t=>t.isFile()&&t.name.toLowerCase().endsWith(".md")).map(t=>g(n,t.name)).sort((t,r)=>t.localeCompare(r)):[]}function w(n){if(typeof n!="string")return;let t=n.trim();return t.length>0?t:void 0}function h(n){return Array.isArray(n)?n.map(t=>String(t).trim()).filter(Boolean):typeof n=="string"?n.split(",").map(t=>t.trim()).filter(Boolean):[]}function V(n){return U(W(n)).name.toLowerCase()}function E(n){return v.test(n)}function K(n){return typeof n=="object"&&n!==null&&!Array.isArray(n)}function X(n){if(typeof n!="string")return;let t=n.trim().toLowerCase();if(t==="inherit")return"inherit";if(t==="none"||t==="off"||t==="disabled")return"none";if(t==="read"||t==="readonly"||t==="read-only")return"readOnly";if(t==="default"||t==="standard")return"default";if(t==="full"||t==="fullaccess"||t==="full-access")return"fullAccess"}function Q(n){if(n===void 0)return;if(typeof n=="string"||Array.isArray(n)){let o=h(n);return o.length>0?{allow:o}:void 0}if(!K(n))return;let t=h(n.allow),r=h(n.deny),e=X(n.mode),i={};return e&&(i.mode=e),t.length>0&&(i.allow=t),r.length>0&&(i.deny=r),Object.keys(i).length>0?i:void 0}function Z(n){let t=[];return E(n.name)||t.push(`Invalid agent name "${n.name}". Use 1-64 lowercase letters, digits, hyphens, or underscores, starting with a letter.`),n.prompt.trim().length===0&&t.push("Agent definition body is empty."),t}function nn(n,t,r){let e=B(n,"utf8"),i=$(e),o=i.frontmatter,s=V(n),l=w(o.name)?.toLowerCase()??s,u=i.content.trim(),p=Z({name:l,prompt:u});if(p.length>0)throw new Error(p.join(" "));let d=w(o.description),c=w(o.model),a=Q(o.tools);return{name:l,scope:t,path:n,...d?{description:d}:{},...c?{model:c}:{},tags:h(o.tags),prompt:u,active:!0,priority:r,...a?{toolPolicy:a}:{}}}function m(n,t,r,e,i){for(let o of G(n))try{e.push(nn(o,t,r))}catch(s){i.push({path:o,scope:t,message:s instanceof Error?s.message:String(s)})}}function tn(n){let t=new Map;for(let r of n){let e=t.get(r.name);(!e||r.priority>e.priority)&&t.set(r.name,r)}return t}function en(n,t){let r=t.get(n.name),e=r?.path===n.path;return{name:n.name,scope:n.scope,path:n.path,...n.description?{description:n.description}:{},...n.model?{model:n.model}:{},tags:n.tags,prompt:n.prompt,active:e,...n.toolPolicy?{toolPolicy:n.toolPolicy}:{},...!e&&r?{shadowedBy:{name:r.name,scope:r.scope,path:r.path}}:{}}}function I(n=process.cwd(),t={}){let r=t.cwd??n,e=t.userDir??j(t.agentHome),i=t.projectDirs??J(r),o=t.pluginDirs??[],s=t.builtInDirs??[],l=[],u=[];t.includeUser!==!1&&m(e,"user",0,l,u),t.includeProject!==!1&&i.forEach((c,a)=>m(c,"project",100+a,l,u)),t.includePlugins&&o.forEach((c,a)=>m(c,"plugin",1e3+a,l,u)),t.includeBuiltIns&&s.forEach((c,a)=>m(c,"built-in",2e3+a,l,u));let p=tn(l),d=l.map(c=>en(c,p));return d.sort((c,a)=>{if(c.active!==a.active)return c.active?-1:1;let P=c.name.localeCompare(a.name);return P!==0?P:c.path.localeCompare(a.path)}),{definitions:d,issues:u,searchPaths:{...t.includeUser!==!1?{user:e}:{},project:t.includeProject===!1?[]:i,plugin:t.includePlugins?o:[],builtIn:t.includeBuiltIns?s:[]}}}function fn(n,t=process.cwd(),r={}){let e=n.trim().toLowerCase(),i=I(t,r);return{definition:i.definitions.find(o=>o.active&&o.name===e)??null,scan:i}}var A=class{constructor(t={}){this.options=t}scan(t=this.options.cwd??process.cwd()){return I(t,this.options)}},k=class{constructor(t=new A){this.loader=t}resolve(t,r=process.cwd()){let e=this.loader.scan(r),i=t.trim().toLowerCase();return{definition:e.definitions.find(o=>o.active&&o.name===i)??null,scan:e}}};function y(n){if(!n||n.length===0)return;let t=Array.from(new Set(n.map(r=>r.trim()).filter(Boolean)));return t.length>0?t:void 0}function rn(n,t){if(!n&&!t)return;if(!n)return y(t);if(!t)return y(n);let r=new Set(t);return y(n.filter(e=>r.has(e)))}function on(n,t){let r={none:0,readOnly:1,default:2,fullAccess:3,inherit:4},e=t??"inherit",i=n??"inherit";return r[e]<=r[i]?e:i}function gn(n,t={}){let r=n.toolPolicy??{},e={mode:on(r.mode,t.mode)},i=rn(t.allow,r.allow),o=y([...t.deny??[],...r.deny??[]]);return i&&(e.allow=i),o&&(e.deny=o),e}function dn(n){return{name:`custom-agent:${n.name}`,order:20,provide(){let t=["","","# Custom Agent",`Active agent: ${n.name}`,`Definition scope: ${n.scope}`,`Definition path: ${n.path}`];return n.description&&t.push(`Description: ${n.description}`),t.push("",n.prompt),t.join(`
3
+ `)}}}function pn(n){return{name:n.name,scope:n.scope,path:n.path,...n.description?{description:n.description}:{},...n.model?{model:n.model}:{},tags:n.tags,active:n.active,...n.toolPolicy?{toolPolicy:n.toolPolicy}:{},...n.shadowedBy?{shadowedBy:n.shadowedBy}:{}}}function f(n,t){return n.length>=t?n:n+" ".repeat(t-n.length)}function mn(n,t){let r=[];if(n.length===0)r.push("No custom agent definitions found.");else{r.push(`${f("name",22)} ${f("scope",8)} ${f("status",9)} description`),r.push(`${"-".repeat(22)} ${"-".repeat(8)} ${"-".repeat(9)} ${"-".repeat(32)}`);for(let e of n){let i=e.active?"active":"shadowed";r.push(`${f(e.name,22)} ${f(e.scope,8)} ${f(i,9)} ${e.description??""}`)}}if(t.length>0){r.push("","Invalid definitions:");for(let e of t)r.push(`- ${e.path}: ${e.message}`)}return r.join(`
3
4
  `)+`
4
- `}function ae(e){let n=[`Agent: ${e.name}`,`Scope: ${e.scope}`,`Path: ${e.path}`,`Status: ${e.active?"active":"shadowed"}`];return e.description&&n.push(`Description: ${e.description}`),e.model&&n.push(`Model: ${e.model}`),e.tags.length>0&&n.push(`Tags: ${e.tags.join(", ")}`),e.toolPolicy&&n.push(`Tools: ${JSON.stringify(e.toolPolicy)}`),e.shadowedBy&&n.push(`Shadowed by: ${e.shadowedBy.scope} ${e.shadowedBy.path}`),n.push("",e.prompt,""),n.join(`
5
- `)}function le(e){let n=e.name.trim().toLowerCase();if(!b(n))throw new Error(`Invalid agent name "${e.name}". Use 1-64 lowercase letters, digits, hyphens, or underscores, starting with a letter.`);let t=e.scope==="user"?x(e.agentHome):z(e.cwd??process.cwd());T(t,{recursive:!0});let o=d(t,`${n}.md`);if(w(o)&&!e.force)throw new Error(`Agent definition already exists: ${o}`);let i=e.description?.trim()||`${n} custom agent`,r=["---",`name: ${n}`,`description: ${JSON.stringify(i)}`,"tags: []","---","",`You are ${n}. Replace this paragraph with precise operating instructions, constraints, tools policy, verification expectations, and the kind of work this agent should own.`,""].join(`
6
- `);return B(o,r,"utf8"),o}export{D as AgentDefinitionLoader,$ as AgentDefinitionResolver,le as createAgentDefinitionFile,re as createAgentDefinitionPromptSection,_ as getProjectAgentDefinitionDirs,z as getProjectAgentDefinitionsDir,x as getUserAgentDefinitionsDir,b as isValidAgentDefinitionName,ae as renderAgentDefinition,ce as renderAgentDefinitions,oe as resolveAgentDefinition,ie as resolveAgentToolPolicy,j as scanAgentDefinitions,se as toAgentDefinitionMetadata,V as validateAgentDefinition};
5
+ `}function hn(n){let t=[`Agent: ${n.name}`,`Scope: ${n.scope}`,`Path: ${n.path}`,`Status: ${n.active?"active":"shadowed"}`];return n.description&&t.push(`Description: ${n.description}`),n.model&&t.push(`Model: ${n.model}`),n.tags.length>0&&t.push(`Tags: ${n.tags.join(", ")}`),n.toolPolicy&&t.push(`Tools: ${JSON.stringify(n.toolPolicy)}`),n.shadowedBy&&t.push(`Shadowed by: ${n.shadowedBy.scope} ${n.shadowedBy.path}`),t.push("",n.prompt,""),t.join(`
6
+ `)}function yn(n){let t=n.name.trim().toLowerCase();if(!E(t))throw new Error(`Invalid agent name "${n.name}". Use 1-64 lowercase letters, digits, hyphens, or underscores, starting with a letter.`);let r=n.scope==="user"?j(n.agentHome):q(n.cwd??process.cwd());M(r,{recursive:!0});let e=g(r,`${t}.md`);if(D(e)&&!n.force)throw new Error(`Agent definition already exists: ${e}`);let i=n.description?.trim()||`${t} custom agent`,o=["---",`name: ${t}`,`description: ${JSON.stringify(i)}`,"tags: []","---","",`You are ${t}. Replace this paragraph with precise operating instructions, constraints, tools policy, verification expectations, and the kind of work this agent should own.`,""].join(`
7
+ `);return F(e,o,"utf8"),e}export{A as AgentDefinitionLoader,k as AgentDefinitionResolver,yn as createAgentDefinitionFile,dn as createAgentDefinitionPromptSection,J as getProjectAgentDefinitionDirs,q as getProjectAgentDefinitionsDir,j as getUserAgentDefinitionsDir,E as isValidAgentDefinitionName,hn as renderAgentDefinition,mn as renderAgentDefinitions,fn as resolveAgentDefinition,gn as resolveAgentToolPolicy,I as scanAgentDefinitions,pn as toAgentDefinitionMetadata,Z as validateAgentDefinition};