@crewx/sdk 0.8.2-rc.4 → 0.8.2-rc.5

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.
@@ -95,7 +95,7 @@ export declare class Crewx extends TypedEventEmitter {
95
95
  private _extractLayoutProps;
96
96
  private _getConversationProvider;
97
97
  private _fetchHistoryMessages;
98
- private _resolveTimeoutConfig;
98
+ private _resolveTimeout;
99
99
  private emitToolEvents;
100
100
  private tryParseJson;
101
101
  query(agentRef: string, message: string, options?: QueryOptions): Promise<QueryResult>;
@@ -1,2 +1,2 @@
1
1
  import {load}from'js-yaml';import {z}from'zod';var m=class extends Error{constructor(e,n){let t=n.length>0?` Available: ${n.join(", ")}`:"";super(`Agent not found: "${e}".${t}`),this.name="AgentNotFoundError";}},I={claude:{id:"claude",provider:"cli/claude"},gemini:{id:"gemini",provider:"cli/gemini"},copilot:{id:"copilot",provider:"cli/copilot"},codex:{id:"codex",provider:"cli/codex"}};function x(a,e){let n=a.startsWith("@")?a.slice(1):a,t=e.find(o=>o.id===n);if(t)return t;let i=I[n];if(i)return i;throw new m(a,e.map(o=>o.id))}var l=class extends Error{constructor(n,t){super(n);this.providerStr=t;this.name="ProviderError";}providerStr};var O=new Map;function P(a,e){O.set(a,e);}function w(a){let e=a.split("/");if(e.length!==2)throw new l(`Invalid provider format: "${a}". Expected namespace/id (e.g., api/webllm)`,a);let[n,t]=e,i=O.get(n);if(i)return i(t,a);throw new l(`Unsupported provider namespace: "${n}". Register a factory with registerProviderFactory('${n}', factory).`,a)}var j=new Function("u","return import(u)"),C=class a{_agents;_config;_tools=new Map;_apiProviders=new Map;constructor(e,n){this._agents=new Map(e.map(t=>[t.id,t])),this._config=n;}static async fromConfig(e,n){let t=new a(e.agents??[],e),i=e.agents??[],o=s=>Array.isArray(s.provider)?s.provider[0]:s.provider,c=i.filter(s=>o(s)==="api/webllm"),g=i.filter(s=>o(s)==="api/openrouter");if(c.length>0&&await t._initWebLLM(c,n?.onProgress),g.length>0){if(!n?.openrouterApiKey)throw new Error("openrouterApiKey is required when using api/openrouter provider");t._initOpenRouter(g,n.openrouterApiKey);}return t._apiProviders.size>0&&P("api",(s,d)=>{let p=t._apiProviders.get(s);if(!p)throw new l(`Unknown api provider: ${s}`,d);return p}),t}async _initWebLLM(e,n){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not available. Chrome 113+ or Edge 113+ required.");let t=await j("https://esm.run/@mlc-ai/web-llm"),i=e[0]?.inline?.model??"gemma-2-2b-it-q4f16_1-MLC",o=await t.CreateMLCEngine(i,{initProgressCallback:n?g=>n(g):void 0}),c=[];this._apiProviders.set("webllm",{async query(g,s){let d=[];s?.systemPrompt&&d.push({role:"system",content:s.systemPrompt}),d.push(...c),d.push({role:"user",content:g});let p="",u=await o.chat.completions.create({messages:d,stream:true,temperature:.7,max_tokens:4096});for await(let y of u){let v=y.choices?.[0]?.delta?.content??"";p+=v,s?.onOutput&&s.onOutput(p,"stdout");}return c.push({role:"user",content:g}),c.push({role:"assistant",content:p}),p},async execute(g,s){return this.query(g,s)}});}_initOpenRouter(e,n){let t=e[0]?.inline?.model??"google/gemma-2-2b-it",i=[];this._apiProviders.set("openrouter",{async query(o,c){let g=[];c?.systemPrompt&&g.push({role:"system",content:c.systemPrompt}),g.push(...i),g.push({role:"user",content:o});let s=await fetch("https://openrouter.ai/api/v1/chat/completions",{method:"POST",headers:{Authorization:`Bearer ${n}`,"Content-Type":"application/json"},body:JSON.stringify({model:c?.model||t,messages:g,stream:true})});if(!s.ok){let v=await s.text();throw new Error(`OpenRouter API error (${s.status}): ${v}`)}let d=s.body.getReader(),p=new TextDecoder,u="",y="";for(;;){let{done:v,value:M}=await d.read();if(v)break;y+=p.decode(M,{stream:true});let b=y.split(`
2
- `);y=b.pop();for(let _ of b){let h=_.trim();if(!h||!h.startsWith("data: "))continue;let k=h.slice(6);if(k!=="[DONE]")try{let R=JSON.parse(k).choices?.[0]?.delta?.content??"";u+=R,R&&c?.onOutput&&c.onOutput(u,"stdout");}catch{}}}return i.push({role:"user",content:o}),i.push({role:"assistant",content:u}),u},async execute(o,c){return this.query(o,c)}});}get agents(){return this._agents}get config(){return this._config}get tools(){return this._tools}registerTool(e,n){this._tools.set(e,{name:e,...n});}async query(e,n,t){let i=Date.now(),o;try{o=x(e,Array.from(this._agents.values()));}catch(p){if(p instanceof m)return {ok:false,data:"",error:{code:"AGENT_NOT_FOUND",message:p.message},meta:{agentId:e.replace(/^@/,""),provider:"",durationMs:Date.now()-i}};throw p}let c=Array.isArray(o.provider)?o.provider[0]??"api/default":o.provider,g=t?.provider??c,s=t?.model??o.inline?.model,d;try{d=w(g);}catch(p){return {ok:false,data:"",error:{code:"PROVIDER_ERROR",message:p.message},meta:{agentId:o.id,provider:g,model:s,durationMs:Date.now()-i}}}try{return {ok:!0,data:await d.query(n,{model:s,context:t?.context,systemPrompt:o.inline?.system_prompt??o.inline?.prompt,onOutput:t?.onOutput?u=>t.onOutput(u):void 0}),meta:{agentId:o.id,provider:g,model:s,durationMs:Date.now()-i}}}catch(p){return {ok:false,data:"",error:{code:"QUERY_FAILED",message:p.message},meta:{agentId:o.id,provider:g,model:s,durationMs:Date.now()-i}}}}async execute(e,n,t){let i=Date.now(),o;try{o=x(e,Array.from(this._agents.values()));}catch(p){if(p instanceof m)return {ok:false,data:"",error:{code:"AGENT_NOT_FOUND",message:p.message},meta:{agentId:e.replace(/^@/,""),provider:"",durationMs:Date.now()-i}};throw p}let c=Array.isArray(o.provider)?o.provider[0]??"api/default":o.provider,g=t?.provider??c,s=t?.model??o.inline?.model,d;try{d=w(g);}catch(p){return {ok:false,data:"",error:{code:"PROVIDER_ERROR",message:p.message},meta:{agentId:o.id,provider:g,model:s,durationMs:Date.now()-i}}}try{return {ok:!0,data:await d.execute(n,{model:s,context:t?.context,systemPrompt:o.inline?.system_prompt??o.inline?.prompt}),meta:{agentId:o.id,provider:g,model:s,durationMs:Date.now()-i}}}catch(p){return {ok:false,data:"",error:{code:"EXECUTE_FAILED",message:p.message},meta:{agentId:o.id,provider:g,model:s,durationMs:Date.now()-i}}}}};var F=z.object({model:z.string().optional(),system_prompt:z.string().optional(),prompt:z.string().optional(),layout:z.union([z.string(),z.object({id:z.string(),props:z.record(z.unknown()).optional()}),z.object({props:z.record(z.unknown())}),z.object({template:z.string()})]).optional()}).catchall(z.unknown()),D=z.object({include:z.array(z.string()).optional()}).optional(),T=z.object({id:z.string(),name:z.string().optional(),role:z.string().optional(),team:z.string().optional(),provider:z.union([z.string(),z.array(z.string())]),working_directory:z.string().optional(),description:z.string().optional(),tags:z.array(z.string()).optional(),inline:F.optional(),timeoutMs:z.number().optional(),idleTimeoutMs:z.number().optional(),skills:D}).catchall(z.unknown()),E=z.object({agents:z.array(T).optional(),hooks:z.array(z.unknown()).optional(),settings:z.record(z.unknown()).optional(),skills:z.unknown().optional(),layouts:z.record(z.unknown()).optional(),documents:z.record(z.unknown()).optional()}).catchall(z.unknown());var f=class extends Error{constructor(n,t){super(n);this.cause=t;this.name="ConfigLoadError";}cause};function $(a){if(!a||typeof a!="string"||!a.trim())throw new f("YAML content must be a non-empty string");let e;try{e=load(a);}catch(i){throw new f(`YAML parse error: ${i.message}`,i)}let n=S(e),t=E.safeParse(n);if(!t.success)throw new f(`Config validation error: ${t.error.message}`);return t.data}function S(a){if(!a||typeof a!="object")return {agents:[]};let e=a;if(e.agents&&typeof e.agents=="object"&&!Array.isArray(e.agents)){let n=e.agents,t=Object.entries(n).map(([i,o])=>({id:i,...o&&typeof o=="object"?o:{}}));return {...e,agents:t}}return e.agents?e:{...e,agents:[]}}var B="crewx:fs:",A=class{prefix;store;constructor(e){this.prefix=e?.prefix??B,this.store=e?.storage??new Map;}async readFile(e){let n=this.toKey(e),t=this.store.get(n);if(t===void 0)throw new Error(`BrowserFsAdapter: file not found: ${e}`);return t}async exists(e){return this.store.has(this.toKey(e))}resolvePath(...e){let n=e.map(c=>c.replace(/\\/g,"/")).join("/").replace(/\/+/g,"/"),t=n.split("/"),i=[];for(let c of t)c==="."||c===""||(c===".."?i.pop():i.push(c));let o=i.join("/");return n.startsWith("/")?`/${o}`:o}isAbsolute(e){return e.startsWith("/")}setItem(e,n){this.store.set(this.toKey(e),n);}removeItem(e){this.store.delete(this.toKey(e));}keys(){return Array.from(this.store.keys()).filter(e=>e.startsWith(this.prefix)).map(e=>e.slice(this.prefix.length))}toKey(e){return `${this.prefix}${e.replace(/\\/g,"/")}`}};export{A as BrowserFsAdapter,f as ConfigLoadError,C as Crewx,l as ProviderError,w as createProvider,$ as parseYamlContent,P as registerProviderFactory};
2
+ `);y=b.pop();for(let _ of b){let h=_.trim();if(!h||!h.startsWith("data: "))continue;let k=h.slice(6);if(k!=="[DONE]")try{let R=JSON.parse(k).choices?.[0]?.delta?.content??"";u+=R,R&&c?.onOutput&&c.onOutput(u,"stdout");}catch{}}}return i.push({role:"user",content:o}),i.push({role:"assistant",content:u}),u},async execute(o,c){return this.query(o,c)}});}get agents(){return this._agents}get config(){return this._config}get tools(){return this._tools}registerTool(e,n){this._tools.set(e,{name:e,...n});}async query(e,n,t){let i=Date.now(),o;try{o=x(e,Array.from(this._agents.values()));}catch(p){if(p instanceof m)return {ok:false,data:"",error:{code:"AGENT_NOT_FOUND",message:p.message},meta:{agentId:e.replace(/^@/,""),provider:"",durationMs:Date.now()-i}};throw p}let c=Array.isArray(o.provider)?o.provider[0]??"api/default":o.provider,g=t?.provider??c,s=t?.model??o.inline?.model,d;try{d=w(g);}catch(p){return {ok:false,data:"",error:{code:"PROVIDER_ERROR",message:p.message},meta:{agentId:o.id,provider:g,model:s,durationMs:Date.now()-i}}}try{return {ok:!0,data:await d.query(n,{model:s,context:t?.context,systemPrompt:o.inline?.system_prompt??o.inline?.prompt,onOutput:t?.onOutput?u=>t.onOutput(u):void 0}),meta:{agentId:o.id,provider:g,model:s,durationMs:Date.now()-i}}}catch(p){return {ok:false,data:"",error:{code:"QUERY_FAILED",message:p.message},meta:{agentId:o.id,provider:g,model:s,durationMs:Date.now()-i}}}}async execute(e,n,t){let i=Date.now(),o;try{o=x(e,Array.from(this._agents.values()));}catch(p){if(p instanceof m)return {ok:false,data:"",error:{code:"AGENT_NOT_FOUND",message:p.message},meta:{agentId:e.replace(/^@/,""),provider:"",durationMs:Date.now()-i}};throw p}let c=Array.isArray(o.provider)?o.provider[0]??"api/default":o.provider,g=t?.provider??c,s=t?.model??o.inline?.model,d;try{d=w(g);}catch(p){return {ok:false,data:"",error:{code:"PROVIDER_ERROR",message:p.message},meta:{agentId:o.id,provider:g,model:s,durationMs:Date.now()-i}}}try{return {ok:!0,data:await d.execute(n,{model:s,context:t?.context,systemPrompt:o.inline?.system_prompt??o.inline?.prompt}),meta:{agentId:o.id,provider:g,model:s,durationMs:Date.now()-i}}}catch(p){return {ok:false,data:"",error:{code:"EXECUTE_FAILED",message:p.message},meta:{agentId:o.id,provider:g,model:s,durationMs:Date.now()-i}}}}};var F=z.object({model:z.string().optional(),system_prompt:z.string().optional(),prompt:z.string().optional(),layout:z.union([z.string(),z.object({id:z.string(),props:z.record(z.unknown()).optional()}),z.object({props:z.record(z.unknown())}),z.object({template:z.string()})]).optional()}).catchall(z.unknown()),D=z.object({include:z.array(z.string()).optional()}).optional(),T=z.object({id:z.string(),name:z.string().optional(),role:z.string().optional(),team:z.string().optional(),provider:z.union([z.string(),z.array(z.string())]),working_directory:z.string().optional(),description:z.string().optional(),tags:z.array(z.string()).optional(),inline:F.optional(),skills:D}).catchall(z.unknown()),E=z.object({agents:z.array(T).optional(),hooks:z.array(z.unknown()).optional(),settings:z.record(z.unknown()).optional(),skills:z.unknown().optional(),layouts:z.record(z.unknown()).optional(),documents:z.record(z.unknown()).optional()}).catchall(z.unknown());var f=class extends Error{constructor(n,t){super(n);this.cause=t;this.name="ConfigLoadError";}cause};function $(a){if(!a||typeof a!="string"||!a.trim())throw new f("YAML content must be a non-empty string");let e;try{e=load(a);}catch(i){throw new f(`YAML parse error: ${i.message}`,i)}let n=S(e),t=E.safeParse(n);if(!t.success)throw new f(`Config validation error: ${t.error.message}`);return t.data}function S(a){if(!a||typeof a!="object")return {agents:[]};let e=a;if(e.agents&&typeof e.agents=="object"&&!Array.isArray(e.agents)){let n=e.agents,t=Object.entries(n).map(([i,o])=>({id:i,...o&&typeof o=="object"?o:{}}));return {...e,agents:t}}return e.agents?e:{...e,agents:[]}}var B="crewx:fs:",A=class{prefix;store;constructor(e){this.prefix=e?.prefix??B,this.store=e?.storage??new Map;}async readFile(e){let n=this.toKey(e),t=this.store.get(n);if(t===void 0)throw new Error(`BrowserFsAdapter: file not found: ${e}`);return t}async exists(e){return this.store.has(this.toKey(e))}resolvePath(...e){let n=e.map(c=>c.replace(/\\/g,"/")).join("/").replace(/\/+/g,"/"),t=n.split("/"),i=[];for(let c of t)c==="."||c===""||(c===".."?i.pop():i.push(c));let o=i.join("/");return n.startsWith("/")?`/${o}`:o}isAbsolute(e){return e.startsWith("/")}setItem(e,n){this.store.set(this.toKey(e),n);}removeItem(e){this.store.delete(this.toKey(e));}keys(){return Array.from(this.store.keys()).filter(e=>e.startsWith(this.prefix)).map(e=>e.slice(this.prefix.length))}toKey(e){return `${this.prefix}${e.replace(/\\/g,"/")}`}};export{A as BrowserFsAdapter,f as ConfigLoadError,C as Crewx,l as ProviderError,w as createProvider,$ as parseYamlContent,P as registerProviderFactory};