@crewx/sdk 0.8.9-rc.9 → 0.8.9
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.
- package/dist/activity-log/builder.d.ts +23 -0
- package/dist/config/pricing.d.ts +9 -0
- package/dist/conversation/sqlite-provider.d.ts +1 -0
- package/dist/esm/index.js +164 -45
- package/dist/esm/plugins/index.js +153 -27
- package/dist/esm/repository/index.js +149 -24
- package/dist/facade/Crewx.d.ts +1 -0
- package/dist/index.browser.d.ts +1 -0
- package/dist/index.browser.js +2 -2
- package/dist/index.d.ts +3 -1
- package/dist/index.js +164 -45
- package/dist/plugins/index.js +153 -27
- package/dist/provider/acp/adapters/index.d.ts +0 -1
- package/dist/provider/acp/index.d.ts +1 -1
- package/dist/provider/acp/meta.d.ts +3 -0
- package/dist/provider/bridge.browser.d.ts +1 -10
- package/dist/provider/bridge.d.ts +1 -10
- package/dist/provider/cli/adapter.types.d.ts +15 -2
- package/dist/provider/cli/adapters/claude.d.ts +1 -0
- package/dist/provider/cli/adapters/cli-knob.util.d.ts +7 -0
- package/dist/provider/cli/adapters/gemini.d.ts +4 -2
- package/dist/provider/cli/adapters/index.d.ts +0 -1
- package/dist/provider/cli/index.d.ts +1 -1
- package/dist/provider/cli/meta.d.ts +2 -0
- package/dist/provider/errors.d.ts +20 -0
- package/dist/provider/order.d.ts +3 -0
- package/dist/repository/index.d.ts +1 -1
- package/dist/repository/index.js +149 -24
- package/dist/repository/task.repository.d.ts +14 -0
- package/dist/repository/thread.repository.d.ts +1 -1
- package/package.json +1 -1
- package/templates/agents/default.yaml +8 -4
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import*as
|
|
1
|
+
import*as H from'path';import H__default,{join,dirname,basename}from'path';import {fileURLToPath}from'url';import zt,{existsSync,mkdirSync,readFileSync}from'fs';import se from'os';import {sql,and,eq,ne,isNull,desc,isNotNull,or,like,asc,inArray,gte,lt}from'drizzle-orm';import {randomUUID,createHash}from'crypto';import {sqliteTable,text,integer,real,index,unique,getTableConfig}from'drizzle-orm/sqlite-core';var nt=(a=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(a,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):a)(function(a){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+a+'" is not supported')});var Zt=()=>fileURLToPath(import.meta.url),te=()=>H__default.dirname(Zt()),b=te();var T=class{resolveDbPath(){return process.env.CREWX_DB?process.env.CREWX_DB:process.env.CREWX_TRACES_DB?process.env.CREWX_TRACES_DB:join(se.homedir(),".crewx","crewx.db")}resolveDbPaths(){return [this.resolveDbPath()]}isMissingTableError(t){return t instanceof Error&&/no such table:/i.test(t.message)}dbExists(t){return existsSync(t??this.resolveDbPath())}};var d=class extends Error{code;cause;constructor(t,e,n){super(e),this.name="RepositoryError",this.code=t,this.cause=n,Object.setPrototypeOf(this,new.target.prototype);}};function k(a){let t=nt("better-sqlite3"),{drizzle:e}=nt("drizzle-orm/better-sqlite3"),n=new t(a);return n.exec("PRAGMA journal_mode = WAL"),n.exec("PRAGMA busy_timeout = 5000"),n.exec("PRAGMA foreign_keys = ON"),{db:e(n),runRaw:(s,r=[])=>n.prepare(s).run(...r),close:()=>n.close()}}var At=new Set,re={agent_id:"TEXT",status:"TEXT DEFAULT 'running'",started_at:"TEXT",trace_id:"TEXT",parent_task_id:"TEXT",crewx_version:"TEXT",pid:"INTEGER",thread_id:"TEXT",workspace_id:"TEXT",workspace_ref:"TEXT",workspace_name:"TEXT",project_id:"TEXT",project_name:"TEXT"};function ht(a,t){return (a.get(`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='${t}'`)?.cnt??0)>0}function oe(a,t){if(t>0||!ht(a,"tasks"))return;let e=a.all("PRAGMA table_info(tasks)"),n=new Set(e.map(s=>s.name));for(let[s,r]of Object.entries(re))n.has(s)||a.run(`ALTER TABLE tasks ADD COLUMN ${s} ${r}`);}var ie={"0002_normalize_task_names":{workspace_name:"TEXT",project_name:"TEXT"}};function ae(a,t,e){if(!ht(a,"__drizzle_migrations")||!ht(a,"tasks"))return;let n=a.all(e`SELECT hash FROM __drizzle_migrations`),s=new Set(n.map(o=>o.hash)),r=JSON.parse(readFileSync(H__default.join(t,"meta/_journal.json"),"utf-8"));for(let o of r.entries){let l=ie[o.tag];if(!l)continue;let u=H__default.join(t,`${o.tag}.sql`);if(!existsSync(u))continue;let c=readFileSync(u,"utf-8"),h=createHash("sha256").update(c).digest("hex");if(s.has(h))continue;let m=a.all("PRAGMA table_info(tasks)"),w=new Set(m.map(y=>y.name));for(let[y,q]of Object.entries(l))w.has(y)||(a.run(`ALTER TABLE tasks ADD COLUMN ${y} ${q}`),w.add(y));}}function de(a,t,e){let n=a.all(e`SELECT hash FROM __drizzle_migrations`),s=new Set(n.map(o=>o.hash)),r=JSON.parse(readFileSync(H__default.join(t,"meta/_journal.json"),"utf-8"));for(let o of r.entries){let l=H__default.join(t,`${o.tag}.sql`);if(!existsSync(l))continue;let u=readFileSync(l,"utf-8"),c=createHash("sha256").update(u).digest("hex");if(s.has(c))continue;let h=/ALTER\s+TABLE\s+[`"]?(\w+)[`"]?\s+ADD\s+[`"]?(\w+)[`"]?/gi,m=[],w;for(;(w=h.exec(u))!==null;)m.push({table:w[1],column:w[2]});if(m.length===0||!u.split(/-->\s*statement-breakpoint/).map(F=>F.trim()).filter(Boolean).every(F=>/^ALTER\s+TABLE\s+.+\s+ADD\s+/i.test(F)))continue;m.every(({table:F,column:pt})=>a.all(`PRAGMA table_info("${F}")`).some(_t=>_t.name===pt))&&a.run(e`INSERT INTO __drizzle_migrations (hash, created_at) VALUES (${c}, ${o.when})`);}}function J(a){let{migrate:t}=nt("drizzle-orm/better-sqlite3/migrator"),{sql:e}=nt("drizzle-orm"),n=[H__default.join(b,"../migrations"),H__default.join(b,"migrations"),H__default.join(b,"../../../../drizzle/migrations"),H__default.join(process.cwd(),"drizzle/migrations")],s=n.find(c=>existsSync(H__default.join(c,"meta/_journal.json")));if(!s)throw new Error(`migrations folder not found. Searched:
|
|
2
2
|
${n.join(`
|
|
3
|
-
`)}`);let r=
|
|
3
|
+
`)}`);let r=a.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),o=0;r?.cnt&&(o=a.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),oe(a,o),r?.cnt&&(de(a,s,e),ae(a,s,e)),t(a,{migrationsFolder:s});let u=(a.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0)-o;if(u>0){let c=r?.cnt?"Database migrated":"Database initialized";console.log(`[crewx] ${c} (${u} migration${u>1?"s":""} applied).`);}}function O(a,t){At.has(t)||(J(a),At.add(t));}var _=sqliteTable("workspaces",{id:text("id").primaryKey(),slug:text("slug").notNull().unique(),name:text("name").notNull(),workspace_path:text("workspace_path"),description:text("description"),is_active:integer("is_active").notNull().default(1),created_at:text("created_at").notNull(),updated_at:text("updated_at").notNull()});var i=sqliteTable("tasks",{id:text("id").primaryKey(),agent_id:text("agent_id").notNull(),user_id:text("user_id"),prompt:text("prompt").notNull(),mode:text("mode").notNull().default("execute"),status:text("status").notNull().default("running"),result:text("result"),error:text("error"),started_at:text("started_at").notNull(),completed_at:text("completed_at"),duration_ms:integer("duration_ms"),metadata:text("metadata"),workspace_id:text("workspace_id"),trace_id:text("trace_id"),parent_task_id:text("parent_task_id"),caller_agent_id:text("caller_agent_id"),model:text("model"),platform:text("platform").default("cli"),crewx_version:text("crewx_version"),input_tokens:integer("input_tokens").default(0),output_tokens:integer("output_tokens").default(0),cost_usd:real("cost_usd").default(0),pid:integer("pid"),rendered_prompt:text("rendered_prompt"),command:text("command"),coding_agent_command:text("coding_agent_command"),exit_code:integer("exit_code"),logs:text("logs"),thread_id:text("thread_id"),workspace_ref:text("workspace_ref"),project_id:text("project_id"),project_ref:text("project_ref"),cached_input_tokens:integer("cached_input_tokens").default(0)},a=>({idx_tasks_agent_id:index("idx_tasks_agent_id").on(a.agent_id),idx_tasks_status:index("idx_tasks_status").on(a.status),idx_tasks_started_at:index("idx_tasks_started_at").on(a.started_at),idx_tasks_trace_id:index("idx_tasks_trace_id").on(a.trace_id),idx_tasks_parent_task_id:index("idx_tasks_parent_task_id").on(a.parent_task_id),idx_tasks_crewx_version:index("idx_tasks_crewx_version").on(a.crewx_version),idx_tasks_pid:index("idx_tasks_pid").on(a.pid),idx_tasks_thread_id:index("idx_tasks_thread_id").on(a.thread_id),idx_tasks_workspace_id:index("idx_tasks_workspace_id").on(a.workspace_id),idx_tasks_workspace_ref:index("idx_tasks_workspace_ref").on(a.workspace_ref),idx_tasks_project_id:index("idx_tasks_project_id").on(a.project_id),idx_tasks_ws_started:index("idx_tasks_ws_started").on(a.workspace_id,a.started_at)}));var p=sqliteTable("threads",{id:text("id").primaryKey(),workspace_id:text("workspace_id").references(()=>_.id,{onDelete:"set null"}),platform:text("platform").notNull().default("cli"),title:text("title"),first_message:text("first_message"),last_message:text("last_message"),message_count:integer("message_count").notNull().default(0),created_at:text("created_at").notNull(),updated_at:text("updated_at").notNull(),metadata:text("metadata"),title_locked:integer("title_locked").notNull().default(0),pinned:integer("pinned").notNull().default(0),starred:integer("starred").notNull().default(0)},a=>({idx_threads_updated_at:index("idx_threads_updated_at").on(a.updated_at),idx_threads_workspace_id:index("idx_threads_workspace_id").on(a.workspace_id)}));var U=sqliteTable("spans",{id:text("id").primaryKey(),task_id:text("task_id").references(()=>i.id,{onDelete:"set null"}),parent_span_id:text("parent_span_id").references(()=>U.id,{onDelete:"set null"}),name:text("name").notNull(),kind:text("kind").notNull().default("internal"),status:text("status").notNull().default("ok"),started_at:text("started_at").notNull(),completed_at:text("completed_at"),duration_ms:integer("duration_ms"),input:text("input"),output:text("output"),error:text("error"),attributes:text("attributes")},a=>({idx_spans_task_id:index("idx_spans_task_id").on(a.task_id),idx_spans_parent_span_id:index("idx_spans_parent_span_id").on(a.parent_span_id)}));var $=sqliteTable("tool_calls",{id:text("id").primaryKey(),task_id:text("task_id").references(()=>i.id,{onDelete:"cascade"}),session_id:text("session_id"),tool_name:text("tool_name").notNull(),files:text("files"),input:text("input"),output:text("output"),duration_ms:integer("duration_ms"),timestamp:text("timestamp").notNull()},a=>({idx_tool_calls_task_id:index("idx_tool_calls_task_id").on(a.task_id),idx_tool_calls_tool_name:index("idx_tool_calls_tool_name").on(a.tool_name),idx_tool_calls_timestamp:index("idx_tool_calls_timestamp").on(a.timestamp)}));var L=sqliteTable("thread_boxes",{id:text("id").primaryKey(),thread_id:text("thread_id").notNull().references(()=>p.id,{onDelete:"cascade"}),seq:integer("seq").notNull(),first_task_id:text("first_task_id").notNull(),mid_task_id:text("mid_task_id").notNull(),last_task_id:text("last_task_id").notNull(),task_count:integer("task_count").notNull(),summary:text("summary"),source_tokens:integer("source_tokens").notNull(),summary_tokens:integer("summary_tokens"),created_at:text("created_at").notNull()},a=>({idx_thread_boxes_thread_id:index("idx_thread_boxes_thread_id").on(a.thread_id),idx_thread_boxes_seq:index("idx_thread_boxes_seq").on(a.thread_id,a.seq),uniq_thread_boxes_thread_seq:unique().on(a.thread_id,a.seq)}));var Q=sqliteTable("request_logs",{id:text("id").primaryKey(),path:text("path").notNull(),method:text("method").notNull(),status_code:integer("status_code").notNull(),duration_ms:integer("duration_ms").notNull(),ip:text("ip"),request_headers:text("request_headers"),response_headers:text("response_headers"),request_body:text("request_body"),response_body:text("response_body"),query:text("query"),user_id:text("user_id"),project_id:text("project_id"),partition_key:text("partition_key").notNull(),timestamp:text("timestamp").notNull().default(sql`(datetime('now'))`),metadata:text("metadata")},a=>({idx_request_logs_timestamp:index("idx_request_logs_timestamp").on(a.timestamp),idx_request_logs_path:index("idx_request_logs_path").on(a.path),idx_request_logs_status_code:index("idx_request_logs_status_code").on(a.status_code),idx_request_logs_partition_key:index("idx_request_logs_partition_key").on(a.partition_key)}));function wt(a){let t=H.resolve(a);return process.platform==="win32"&&(t=t.replace(/\\/g,"/"),t=t.replace(/^([A-Z]):/,(e,n)=>`${n.toLowerCase()}:`)),t.length>1&&!/^[a-zA-Z]:\/$/.test(t)&&(t=t.replace(/\/+$/,"")),t}function Bt(a){let t=wt(a);return createHash("sha256").update(t).digest("hex")}function at(a){return a.toLowerCase().replace(/[^a-z0-9-]/g,"-").replace(/-{2,}/g,"-").replace(/^-+|-+$/g,"")}var Et=class extends T{dbRoot;constructor(t={}){super(),this.dbRoot=t.dbRoot;}resolveDbPath(){return this.dbRoot?join(this.dbRoot,".crewx","crewx.db"):super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let s=dirname(e);existsSync(s)||mkdirSync(s,{recursive:true});}else if(!existsSync(e))throw new d("NOT_FOUND","Database not found");let n=k(e);if(t)try{J(n.db);}catch(s){throw n.close(),s}return n}resolveSlug(t,e,n){let s=at(basename(n)),o=`${at(basename(dirname(n)))}-${s}`,l=[s,o];try{let u=c=>t.select({id:_.id}).from(_).where(and(eq(_.slug,c),ne(_.id,e))).limit(1).all().length>0;for(let c of l)if(!u(c))return c;for(let c=2;c<1e3;c+=1){let h=`${o}-${c}`;if(!u(h))return h}}catch{}return s}normalizeLegacySlugs(){if(!this.dbExists())return {updated:0,checked:0};let t=this.openHandle(true);try{let e=t.db.select({id:_.id,slug:_.slug}).from(_).all(),n=0;for(let s of e)if(s.slug.includes("/")){let r=at(s.slug.replace(/\//g,"-"));t.db.update(_).set({slug:r,updated_at:new Date().toISOString()}).where(eq(_.id,s.id)).run(),n+=1;}return {updated:n,checked:e.length}}catch(e){throw new d("DB_ERROR","Failed to normalize legacy slugs",e)}finally{t.close();}}ensureRow(t,e){let{id:n,slug:s,name:r,workspacePath:o}=e,l=new Date().toISOString();t.insert(_).values({id:n,slug:s,name:r,workspace_path:o,is_active:1,created_at:l,updated_at:l}).onConflictDoNothing().run(),t.update(_).set({workspace_path:o,updated_at:l}).where(and(eq(_.id,n),isNull(_.workspace_path))).run();}registerWorkspace(t){let e=wt(t),n=this.openHandle(true);try{let s=Bt(e),r=basename(e),o=this.resolveSlug(n.db,s,e);return this.ensureRow(n.db,{id:s,slug:o,name:r,workspacePath:e}),{id:s,slug:o}}catch(s){throw s instanceof d?s:new d("DB_ERROR","Failed to register workspace",s)}finally{n.close();}}listProjects(t){if(!this.dbExists())return {rows:[],total:0};let e=this.openHandle(false);try{let n=t.isActive!==void 0?eq(_.is_active,t.isActive?1:0):void 0,s=e.db.select({count:sql`count(*)`}).from(_).where(n).get();return {rows:e.db.select().from(_).where(n).orderBy(desc(_.updated_at)).limit(t.limit).offset(t.offset).all(),total:s?.count??0}}catch(n){throw new d("DB_ERROR","Failed to list projects",n)}finally{e.close();}}findById(t){if(!this.dbExists())return;let e=this.openHandle(false);try{return e.db.select().from(_).where(eq(_.id,t)).limit(1).get()??void 0}catch(n){throw new d("DB_ERROR","Failed to find workspace",n)}finally{e.close();}}findAgentsByProject(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.all(sql`SELECT DISTINCT agent_id FROM tasks WHERE workspace_id = ${t} AND agent_id IS NOT NULL ORDER BY agent_id`).map(s=>s.agent_id)}catch(n){throw new d("DB_ERROR","Failed to find agents by project",n)}finally{e.close();}}findThreadsByProject(t,e){if(!this.dbExists())return {rows:[],total:0};let n=this.openHandle(false);try{let s=n.db.get(sql`SELECT COUNT(*) as count FROM threads WHERE workspace_id = ${t}`);return {rows:n.db.all(sql`SELECT t.*,
|
|
4
4
|
(SELECT agent_id FROM tasks tk WHERE tk.thread_id = t.id AND tk.agent_id IS NOT NULL ORDER BY tk.started_at ASC LIMIT 1) AS agent_id
|
|
5
5
|
FROM threads t
|
|
6
6
|
WHERE t.workspace_id = ${t}
|
|
7
7
|
ORDER BY t.updated_at DESC
|
|
8
|
-
LIMIT ${e.limit} OFFSET ${e.offset}`),total:s?.count??0}}catch(s){throw new
|
|
8
|
+
LIMIT ${e.limit} OFFSET ${e.offset}`),total:s?.count??0}}catch(s){throw new d("DB_ERROR","Failed to find threads by project",s)}finally{n.close();}}findBySlug(t){if(!this.dbExists())return;let e=this.openHandle(false);try{return e.db.select().from(_).where(eq(_.slug,t)).limit(1).get()??void 0}catch(n){throw new d("DB_ERROR","Failed to find workspace by slug",n)}finally{e.close();}}slugExists(t,e){if(!this.dbExists())return false;let n=this.openHandle(false);try{let s=e?and(eq(_.slug,t),ne(_.id,e)):eq(_.slug,t);return !!n.db.select({id:_.id}).from(_).where(s).limit(1).get()}catch(s){throw new d("DB_ERROR","Failed to check slug",s)}finally{n.close();}}insert(t,e,n,s){let r=this.openHandle(true);try{let o=new Date().toISOString();r.db.insert(_).values({id:t,slug:e,name:n,workspace_path:s,is_active:1,created_at:o,updated_at:o}).run();let l=r.db.select().from(_).where(eq(_.id,t)).limit(1).get();if(!l)throw new d("DB_ERROR","Insert did not return a row");return l}catch(o){throw o instanceof d?o:new d("DB_ERROR","Failed to insert workspace",o)}finally{r.close();}}update(t,e,n){let s=this.openHandle(true);try{s.runRaw(`UPDATE workspaces SET ${e.join(", ")} WHERE id = ?`,n);let r=s.db.select().from(_).where(eq(_.id,t)).limit(1).get();if(!r)throw new d("NOT_FOUND",`Workspace ${t} not found`);return r}catch(r){throw r instanceof d?r:new d("DB_ERROR","Failed to update workspace",r)}finally{s.close();}}cleanupOrphanWorkspaces(){if(!this.dbExists())return {softDeleted:0,checked:0};let t=this.openHandle(true);try{let e=t.db.select({id:_.id,workspace_path:_.workspace_path}).from(_).where(and(eq(_.is_active,1),isNotNull(_.workspace_path))).all(),n=0;for(let s of e){let r=s.workspace_path;existsSync(join(r,"crewx.yaml"))||existsSync(join(r,"crewx.yml"))||(t.db.update(_).set({is_active:0,updated_at:new Date().toISOString()}).where(eq(_.id,s.id)).run(),n+=1);}return {softDeleted:n,checked:e.length}}catch(e){throw new d("DB_ERROR","Failed to cleanup orphan workspaces",e)}finally{t.close();}}delete(t){let e=this.openHandle(true);try{e.db.run(sql`UPDATE threads SET workspace_id = NULL WHERE workspace_id = ${t}`),e.db.delete(_).where(eq(_.id,t)).run();}catch(n){throw n instanceof d?n:new d("DB_ERROR","Failed to delete workspace",n)}finally{e.close();}}};var Mt=[_,i,p,U,$,L,Q];function Ne(){return H__default.join(se.homedir(),".crewx","crewx.db")}function Ce(a){if(!zt.existsSync(a))return null;let t=Math.floor(Date.now()/1e3),e=`${a}.bak-${t}`;return zt.copyFileSync(a,e),e}function qt(a){let t=a.all("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'");return new Set(t.map(e=>e.name))}function Le(a){return a==null||typeof a=="object"&&"queryChunks"in a?null:typeof a=="number"?String(a):typeof a=="boolean"?a?"1":"0":typeof a=="string"?`'${a.replace(/'/g,"''")}'`:String(a)}function ve(a){let t=a.getSQLType(),e=`"${a.name}" ${t}`,n=Le(a.default);return n!==null&&(e+=` DEFAULT ${n}`),a.notNull&&(e+=" NOT NULL"),e}function Pe(a,t){let e=t?.dbPath??Ne(),n=t?.force??false,s=t?.dryRun??false,r=s?null:Ce(e);if(n&&!s)try{a.run("DELETE FROM __drizzle_migrations");}catch{}let o=qt(a);if(!s)try{J(a);}catch(m){let w=m instanceof Error?`${m.message} ${m.cause?.message??""}`:"";if(!n||!w.includes("duplicate column"))throw m}let l,u;s?(l=Mt.map(w=>getTableConfig(w).name).filter(w=>!o.has(w)),u=o):(u=qt(a),l=[...u].filter(m=>!o.has(m)));let c=[],h=[];for(let m of Mt){let w=getTableConfig(m),y=w.name;if(!u.has(y))continue;let q=a.all(`PRAGMA table_info("${y}")`),F=new Set(q.map(P=>P.name)),pt=new Set(w.columns.map(P=>P.name));for(let P of w.columns)if(!F.has(P.name)){if(!s){let _t=ve(P);a.run(`ALTER TABLE "${y}" ADD COLUMN ${_t}`);}c.push({table:y,column:P.name});}for(let P of q)pt.has(P.name)||h.push(`${y}.${P.name} exists in DB but not in schema (untouched)`);}return {created:l,altered:c,warnings:h,backupPath:r}}var G="2026-05-09",Ue="0.8.9-rc.13",Y=10,X=parseInt(Ue.split("rc.")[1]),yt=class extends T{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let s=dirname(e);existsSync(s)||mkdirSync(s,{recursive:true});}else if(!existsSync(e))throw new d("NOT_FOUND","Database not found");let n=k(e);if(t)try{O(n.db,e);}catch(s){throw n.close(),s}return n}startTask(t){let e=this.openHandle(true);try{e.db.insert(i).values({id:t.id,agent_id:t.agentId,prompt:t.prompt,mode:t.mode,status:t.status,started_at:t.startedAt,pid:t.pid??null,parent_task_id:t.parentTaskId??null,caller_agent_id:t.callerAgentId??null,trace_id:t.traceId??null,command:t.command??null,metadata:t.metadata??null,workspace_id:t.workspaceId??null,platform:t.platform??"cli",crewx_version:t.crewxVersion??null,thread_id:t.threadId??null,model:t.model??null,rendered_prompt:t.renderedPrompt??null,coding_agent_command:t.codingAgentCommand??null}).onConflictDoNothing().run();}catch(n){throw n instanceof d?n:new d("DB_ERROR","Failed to start task",n)}finally{e.close();}}finishTask(t){let e=this.openHandle(true);try{e.runRaw(`UPDATE tasks SET status=?, result=?, error=?, completed_at=?, duration_ms=?,
|
|
9
9
|
exit_code=?, input_tokens=?, output_tokens=?, cached_input_tokens=?, cost_usd=?,
|
|
10
|
-
model=COALESCE(?, model) WHERE id
|
|
10
|
+
model=COALESCE(?, model) WHERE id=? AND status='running'`,[t.status,t.result??null,t.error??null,t.completedAt,t.durationMs??null,t.exitCode??null,t.inputTokens??0,t.outputTokens??0,t.cachedInputTokens??0,t.costUsd??0,t.model??null,t.id]);}catch(n){throw n instanceof d?n:new d("DB_ERROR","Failed to finish task",n)}finally{e.close();}}appendLog(t,e){let n=this.openHandle(true);try{n.db.transaction(s=>{let r=s.select({logs:i.logs}).from(i).where(eq(i.id,t)).limit(1).get(),o=r?.logs?JSON.parse(r.logs):[];o.push(e),s.update(i).set({logs:JSON.stringify(o)}).where(eq(i.id,t)).run();},{behavior:"immediate"});}catch(s){throw s instanceof d?s:new d("DB_ERROR","Failed to append log",s)}finally{n.close();}}getRunningTasks(){if(!this.dbExists())return [];let t=this.openHandle(false);try{return t.db.select().from(i).where(eq(i.status,"running")).orderBy(desc(i.started_at)).all()}catch(e){throw new d("DB_ERROR","Failed to get running tasks",e)}finally{t.close();}}getAllTasks(){if(!this.dbExists())return [];let t=this.openHandle(false);try{return t.db.select().from(i).orderBy(desc(i.started_at)).limit(100).all()}catch(e){throw new d("DB_ERROR","Failed to get all tasks",e)}finally{t.close();}}getTask(t){if(!this.dbExists())return;let e=this.openHandle(false);try{return e.db.select().from(i).where(eq(i.id,t)).limit(1).get()??void 0}catch(n){throw new d("DB_ERROR","Failed to get task",n)}finally{e.close();}}killTask(t){if(!this.dbExists())return {killed:false};let e=this.openHandle(true);try{let n=e.db.select({id:i.id,status:i.status,pid:i.pid}).from(i).where(eq(i.id,t)).limit(1).get();if(!n||n.status!=="running")return {killed:!1};if(n.pid)try{process.kill(n.pid,"SIGTERM");}catch{}return e.db.update(i).set({status:"failed",error:"Killed by user",completed_at:new Date().toISOString()}).where(and(eq(i.id,t),eq(i.status,"running"))).run(),{killed:!0,pid:n.pid??void 0}}catch(n){throw n instanceof d?n:new d("DB_ERROR","Failed to kill task",n)}finally{e.close();}}reapOrphanedTasks(){if(!this.dbExists())return 0;let t=this.openHandle(true);try{let e=t.db.select({id:i.id,pid:i.pid}).from(i).where(eq(i.status,"running")).all(),n=0;for(let s of e){if(!s.pid)continue;let r=!1;try{process.kill(s.pid,0),r=!0;}catch{}r||(t.db.update(i).set({status:"failed",error:"Reaped: process not found (orphaned task)",completed_at:new Date().toISOString()}).where(and(eq(i.id,s.id),eq(i.status,"running"))).run(),n++);}return n}finally{t.close();}}findTaskStatus(t,e){let n=this.resolveDbPaths();for(let s of n){if(!existsSync(s))continue;let r=k(s);try{let o=e?eq(i.workspace_id,e):void 0,l=o?and(eq(i.id,t),o):eq(i.id,t),u=r.db.select().from(i).where(l).limit(1).get()??void 0;if(!u){let c=or(eq(i.thread_id,t),and(isNull(i.thread_id),like(i.command,`%--thread=${t}%`))),h=o?and(c,o):c;u=r.db.select().from(i).where(h).orderBy(desc(i.started_at)).limit(1).get()??void 0;}if(u)return u}catch(o){throw new d("DB_ERROR","Failed to find task status",o)}finally{r.close();}}}findChildTasks(t,e){let n=this.resolveDbPaths(),s=new Set,r=[];for(let o of n){if(!existsSync(o))continue;let l=k(o);try{let u=e?and(eq(i.parent_task_id,t),eq(i.workspace_id,e)):eq(i.parent_task_id,t),c=l.db.select().from(i).where(u).orderBy(asc(i.started_at)).all();for(let h of c)s.has(h.id)||(s.add(h.id),r.push(h));}catch(u){throw new d("DB_ERROR","Failed to find child tasks",u)}finally{l.close();}}return r}getWorkspaceUsageSummary(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.all(t?sql`
|
|
11
11
|
SELECT
|
|
12
12
|
COALESCE(workspace_id, 'unknown') AS workspace_id,
|
|
13
13
|
COALESCE(SUM(input_tokens), 0) AS input_tokens,
|
|
@@ -28,12 +28,24 @@ ${n.join(`
|
|
|
28
28
|
FROM tasks
|
|
29
29
|
GROUP BY workspace_id
|
|
30
30
|
ORDER BY (COALESCE(SUM(input_tokens), 0) + COALESCE(SUM(output_tokens), 0)) DESC
|
|
31
|
-
`)}catch(n){throw new
|
|
31
|
+
`)}catch(n){throw new d("DB_ERROR","Failed to get workspace usage summary",n)}finally{e.close();}}getThreadTokenUsage(t,e){let n=this.resolveDbPaths(),s=new Set,r=0,o=0,l=0;for(let u of n){if(!existsSync(u))continue;let c=k(u);try{let h=or(eq(i.thread_id,t),and(isNull(i.thread_id),like(i.command,`%--thread=${t}%`))),m=e?and(h,eq(i.workspace_id,e)):h,w=c.db.select({id:i.id,input_tokens:i.input_tokens,output_tokens:i.output_tokens,cost_usd:i.cost_usd}).from(i).where(m).all();for(let y of w)s.has(y.id)||(s.add(y.id),r+=y.input_tokens??0,o+=y.output_tokens??0,l+=y.cost_usd??0);}catch(h){throw new d("DB_ERROR","Failed to get thread token usage",h)}finally{c.close();}}return {inputTokens:r,outputTokens:o,costUsd:l}}findTasksByThread(t,e){let n=this.resolveDbPaths(),s=new Set,r=[];for(let o of n){if(!existsSync(o))continue;let l=k(o);try{let u=or(eq(i.thread_id,t),and(isNull(i.thread_id),like(i.command,`%--thread=${t}%`))),c=e?and(u,eq(i.workspace_id,e)):u,h=l.db.select().from(i).where(c).orderBy(asc(i.started_at)).all();for(let m of h)s.has(m.id)||(s.add(m.id),r.push(m));}catch(u){throw new d("DB_ERROR","Failed to find tasks by thread",u)}finally{l.close();}}return r}findAllTasks(t){if(!this.dbExists())return {rows:[],total:0};let e=this.openHandle(false);try{let n=[];t.workspaceId&&n.push(eq(i.workspace_id,t.workspaceId));let s=t.agents&&t.agents.length>0?t.agents:t.agentId?[t.agentId]:null;s&&n.push(inArray(i.agent_id,s));let r=t.statuses&&t.statuses.length>0?t.statuses:t.status?[t.status]:null;r&&n.push(inArray(i.status,r));let o=t.q??t.search;o&&n.push(like(i.prompt,`%${o}%`)),t.from&&n.push(gte(i.started_at,t.from)),t.to&&n.push(lt(i.started_at,t.to));let l=n.length>0?and(...n):void 0,u=e.db.select({count:sql`count(*)`}).from(i).where(l).get(),c=(t.sortDir??"DESC")==="ASC"?asc(i.started_at):desc(i.started_at);return {rows:e.db.select().from(i).where(l).orderBy(c).limit(t.limit).offset(t.offset).all(),total:u?.count??0}}catch(n){throw new d("DB_ERROR","Failed to find all tasks",n)}finally{e.close();}}getAgentUsage(t,e,n){if(!this.dbExists())return [];let s=this.openHandle(false);try{return s.db.all(n?sql`
|
|
32
32
|
SELECT
|
|
33
33
|
t.agent_id,
|
|
34
34
|
t.workspace_id,
|
|
35
35
|
COUNT(*) AS total_tasks,
|
|
36
|
-
COALESCE(SUM(
|
|
36
|
+
COALESCE(SUM(
|
|
37
|
+
COALESCE(t.input_tokens, 0)
|
|
38
|
+
+ CASE
|
|
39
|
+
WHEN t.started_at >= ${G}
|
|
40
|
+
AND (
|
|
41
|
+
t.crewx_version IS NULL
|
|
42
|
+
OR (t.crewx_version LIKE '0.8.%' AND t.crewx_version NOT LIKE '0.8.9%')
|
|
43
|
+
OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${Y}) AS INTEGER) < ${X})
|
|
44
|
+
)
|
|
45
|
+
THEN COALESCE(t.cached_input_tokens, 0)
|
|
46
|
+
ELSE 0
|
|
47
|
+
END
|
|
48
|
+
), 0) AS input_tokens,
|
|
37
49
|
COALESCE(SUM(t.output_tokens), 0) AS output_tokens,
|
|
38
50
|
COALESCE(SUM(t.cached_input_tokens), 0) AS cached_input_tokens,
|
|
39
51
|
COALESCE(SUM(t.cost_usd), 0) AS cost_usd
|
|
@@ -43,13 +55,40 @@ ${n.join(`
|
|
|
43
55
|
AND t.started_at < ${e}
|
|
44
56
|
AND t.workspace_id = ${n}
|
|
45
57
|
GROUP BY t.agent_id, t.workspace_id
|
|
46
|
-
ORDER BY (
|
|
58
|
+
ORDER BY (
|
|
59
|
+
COALESCE(SUM(
|
|
60
|
+
COALESCE(t.input_tokens, 0)
|
|
61
|
+
+ CASE
|
|
62
|
+
WHEN t.started_at >= ${G}
|
|
63
|
+
AND (
|
|
64
|
+
t.crewx_version IS NULL
|
|
65
|
+
OR (t.crewx_version LIKE '0.8.%' AND t.crewx_version NOT LIKE '0.8.9%')
|
|
66
|
+
OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${Y}) AS INTEGER) < ${X})
|
|
67
|
+
)
|
|
68
|
+
THEN COALESCE(t.cached_input_tokens, 0)
|
|
69
|
+
ELSE 0
|
|
70
|
+
END
|
|
71
|
+
), 0)
|
|
72
|
+
+ COALESCE(SUM(t.output_tokens), 0)
|
|
73
|
+
) DESC
|
|
47
74
|
`:sql`
|
|
48
75
|
SELECT
|
|
49
76
|
t.agent_id,
|
|
50
77
|
t.workspace_id,
|
|
51
78
|
COUNT(*) AS total_tasks,
|
|
52
|
-
COALESCE(SUM(
|
|
79
|
+
COALESCE(SUM(
|
|
80
|
+
COALESCE(t.input_tokens, 0)
|
|
81
|
+
+ CASE
|
|
82
|
+
WHEN t.started_at >= ${G}
|
|
83
|
+
AND (
|
|
84
|
+
t.crewx_version IS NULL
|
|
85
|
+
OR (t.crewx_version LIKE '0.8.%' AND t.crewx_version NOT LIKE '0.8.9%')
|
|
86
|
+
OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${Y}) AS INTEGER) < ${X})
|
|
87
|
+
)
|
|
88
|
+
THEN COALESCE(t.cached_input_tokens, 0)
|
|
89
|
+
ELSE 0
|
|
90
|
+
END
|
|
91
|
+
), 0) AS input_tokens,
|
|
53
92
|
COALESCE(SUM(t.output_tokens), 0) AS output_tokens,
|
|
54
93
|
COALESCE(SUM(t.cached_input_tokens), 0) AS cached_input_tokens,
|
|
55
94
|
COALESCE(SUM(t.cost_usd), 0) AS cost_usd
|
|
@@ -58,12 +97,39 @@ ${n.join(`
|
|
|
58
97
|
AND t.started_at >= ${t}
|
|
59
98
|
AND t.started_at < ${e}
|
|
60
99
|
GROUP BY t.agent_id, t.workspace_id
|
|
61
|
-
ORDER BY (
|
|
62
|
-
|
|
100
|
+
ORDER BY (
|
|
101
|
+
COALESCE(SUM(
|
|
102
|
+
COALESCE(t.input_tokens, 0)
|
|
103
|
+
+ CASE
|
|
104
|
+
WHEN t.started_at >= ${G}
|
|
105
|
+
AND (
|
|
106
|
+
t.crewx_version IS NULL
|
|
107
|
+
OR (t.crewx_version LIKE '0.8.%' AND t.crewx_version NOT LIKE '0.8.9%')
|
|
108
|
+
OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${Y}) AS INTEGER) < ${X})
|
|
109
|
+
)
|
|
110
|
+
THEN COALESCE(t.cached_input_tokens, 0)
|
|
111
|
+
ELSE 0
|
|
112
|
+
END
|
|
113
|
+
), 0)
|
|
114
|
+
+ COALESCE(SUM(t.output_tokens), 0)
|
|
115
|
+
) DESC
|
|
116
|
+
`).map(o=>({agentId:o.agent_id,workspaceId:o.workspace_id??null,totalTasks:o.total_tasks,inputTokens:o.input_tokens,outputTokens:o.output_tokens,cachedInputTokens:o.cached_input_tokens,costUsd:o.cost_usd,totalTokens:o.input_tokens+o.output_tokens}))}catch(r){throw new d("DB_ERROR","Failed to get agent usage",r)}finally{s.close();}}getAgentUsageTrendRaw(t,e,n){if(!this.dbExists())return [];let s=this.openHandle(false);try{return s.db.all(n?sql`
|
|
63
117
|
SELECT
|
|
64
118
|
date(t.started_at) AS date,
|
|
65
119
|
t.agent_id,
|
|
66
|
-
COALESCE(SUM(
|
|
120
|
+
COALESCE(SUM(
|
|
121
|
+
COALESCE(t.input_tokens, 0)
|
|
122
|
+
+ CASE
|
|
123
|
+
WHEN t.started_at >= ${G}
|
|
124
|
+
AND (
|
|
125
|
+
t.crewx_version IS NULL
|
|
126
|
+
OR (t.crewx_version LIKE '0.8.%' AND t.crewx_version NOT LIKE '0.8.9%')
|
|
127
|
+
OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${Y}) AS INTEGER) < ${X})
|
|
128
|
+
)
|
|
129
|
+
THEN COALESCE(t.cached_input_tokens, 0)
|
|
130
|
+
ELSE 0
|
|
131
|
+
END
|
|
132
|
+
), 0) AS input_tokens,
|
|
67
133
|
COALESCE(SUM(t.output_tokens), 0) AS output_tokens,
|
|
68
134
|
COALESCE(SUM(t.cached_input_tokens), 0) AS cached_input_tokens,
|
|
69
135
|
COALESCE(SUM(t.cost_usd), 0) AS cost_usd
|
|
@@ -78,7 +144,19 @@ ${n.join(`
|
|
|
78
144
|
SELECT
|
|
79
145
|
date(t.started_at) AS date,
|
|
80
146
|
t.agent_id,
|
|
81
|
-
COALESCE(SUM(
|
|
147
|
+
COALESCE(SUM(
|
|
148
|
+
COALESCE(t.input_tokens, 0)
|
|
149
|
+
+ CASE
|
|
150
|
+
WHEN t.started_at >= ${G}
|
|
151
|
+
AND (
|
|
152
|
+
t.crewx_version IS NULL
|
|
153
|
+
OR (t.crewx_version LIKE '0.8.%' AND t.crewx_version NOT LIKE '0.8.9%')
|
|
154
|
+
OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${Y}) AS INTEGER) < ${X})
|
|
155
|
+
)
|
|
156
|
+
THEN COALESCE(t.cached_input_tokens, 0)
|
|
157
|
+
ELSE 0
|
|
158
|
+
END
|
|
159
|
+
), 0) AS input_tokens,
|
|
82
160
|
COALESCE(SUM(t.output_tokens), 0) AS output_tokens,
|
|
83
161
|
COALESCE(SUM(t.cached_input_tokens), 0) AS cached_input_tokens,
|
|
84
162
|
COALESCE(SUM(t.cost_usd), 0) AS cost_usd
|
|
@@ -88,7 +166,43 @@ ${n.join(`
|
|
|
88
166
|
AND t.started_at < ${e}
|
|
89
167
|
GROUP BY date(t.started_at), t.agent_id
|
|
90
168
|
ORDER BY date(t.started_at) ASC
|
|
91
|
-
`).map(o=>({date:o.date,agentId:o.agent_id,inputTokens:o.input_tokens,outputTokens:o.output_tokens,cachedInputTokens:o.cached_input_tokens,costUsd:o.cost_usd}))}catch(r){throw new
|
|
169
|
+
`).map(o=>({date:o.date,agentId:o.agent_id,inputTokens:o.input_tokens,outputTokens:o.output_tokens,cachedInputTokens:o.cached_input_tokens,costUsd:o.cost_usd,totalTokens:o.input_tokens+o.output_tokens}))}catch(r){throw new d("DB_ERROR","Failed to get agent usage trend",r)}finally{s.close();}}findTaskForStop(t,e){if(!this.dbExists())return;let n=this.openHandle(false);try{return n.db.select().from(i).where(and(eq(i.id,t),eq(i.workspace_id,e))).limit(1).get()??void 0}catch(s){throw new d("DB_ERROR","Failed to find task for stop",s)}finally{n.close();}}markTaskFailed(t,e,n){if(!this.dbExists())return;let s=this.openHandle(true);try{let r=new Date().toISOString(),o=n?and(eq(i.id,t),eq(i.status,"running"),eq(i.workspace_id,n)):and(eq(i.id,t),eq(i.status,"running"));s.db.update(i).set({status:"failed",error:e,completed_at:r}).where(o).run();}catch(r){throw r instanceof d?r:new d("DB_ERROR","Failed to mark task failed",r)}finally{s.close();}}findTasksByPromptHint(t,e){let n=this.resolveDbPaths(),s=new Set,r=[];for(let o of n){if(!existsSync(o))continue;let l=k(o);try{let u=e?and(like(i.prompt,`%${t}%`),eq(i.workspace_id,e)):like(i.prompt,`%${t}%`),c=l.db.select().from(i).where(u).orderBy(asc(i.started_at)).all();for(let h of c)s.has(h.id)||(s.add(h.id),r.push(h));}catch(u){throw new d("DB_ERROR","Failed to find tasks by prompt hint",u)}finally{l.close();}}return r}getProviderUsage(t,e,n){if(!this.dbExists())return [];let s=this.openHandle(false);try{let r=sql`
|
|
170
|
+
CASE
|
|
171
|
+
WHEN ${i.model} LIKE 'claude-%' OR ${i.model} IN ('opus', 'sonnet', 'haiku', 'opus[1m]', 'sonnet[1m]') THEN 'claude'
|
|
172
|
+
WHEN ${i.model} LIKE 'gpt-%' OR ${i.model} LIKE 'codex-%' THEN 'codex'
|
|
173
|
+
WHEN ${i.model} LIKE 'gemini-%' THEN 'gemini'
|
|
174
|
+
WHEN ${i.model} LIKE 'zai-%' OR ${i.model} LIKE 'openrouter/z-ai/%' THEN 'opencode'
|
|
175
|
+
WHEN ${i.model} LIKE 'minimax/%' THEN 'minimax'
|
|
176
|
+
WHEN ${i.model} LIKE 'qwen%' THEN 'qwen'
|
|
177
|
+
ELSE 'unknown'
|
|
178
|
+
END
|
|
179
|
+
`,o=sql`
|
|
180
|
+
COALESCE(${i.input_tokens}, 0)
|
|
181
|
+
+ CASE
|
|
182
|
+
WHEN ${i.started_at} >= ${G}
|
|
183
|
+
AND (
|
|
184
|
+
${i.crewx_version} IS NULL
|
|
185
|
+
OR (${i.crewx_version} LIKE '0.8.%' AND ${i.crewx_version} NOT LIKE '0.8.9%')
|
|
186
|
+
OR (${i.crewx_version} LIKE '0.8.9-rc.%' AND CAST(SUBSTR(${i.crewx_version}, ${Y}) AS INTEGER) < ${X})
|
|
187
|
+
)
|
|
188
|
+
THEN COALESCE(${i.cached_input_tokens}, 0)
|
|
189
|
+
ELSE 0
|
|
190
|
+
END
|
|
191
|
+
`,l=n?sql`WHERE ${i.status} IN ('completed', 'success') AND ${i.started_at} >= ${t} AND ${i.started_at} < ${e} AND ${i.workspace_id} = ${n}`:sql`WHERE ${i.status} IN ('completed', 'success') AND ${i.started_at} >= ${t} AND ${i.started_at} < ${e}`;return s.db.all(sql`
|
|
192
|
+
SELECT
|
|
193
|
+
${r} AS provider,
|
|
194
|
+
COUNT(*) AS total_tasks,
|
|
195
|
+
COALESCE(SUM(${o}), 0) AS input_tokens,
|
|
196
|
+
COALESCE(SUM(${i.output_tokens}), 0) AS output_tokens,
|
|
197
|
+
COALESCE(SUM(${i.cached_input_tokens}), 0) AS cached_input_tokens,
|
|
198
|
+
COALESCE(SUM(${i.cost_usd}), 0) AS cost_usd,
|
|
199
|
+
COALESCE(SUM(${i.duration_ms}), 0) AS active_duration_ms,
|
|
200
|
+
MAX(${i.completed_at}) AS last_active_at
|
|
201
|
+
FROM ${i}
|
|
202
|
+
${l}
|
|
203
|
+
GROUP BY provider
|
|
204
|
+
ORDER BY (COALESCE(SUM(${o}), 0) + COALESCE(SUM(${i.output_tokens}), 0)) DESC
|
|
205
|
+
`).map(c=>({provider:c.provider,totalTasks:c.total_tasks,inputTokens:c.input_tokens,outputTokens:c.output_tokens,cachedInputTokens:c.cached_input_tokens,costUsd:c.cost_usd,totalTokens:c.input_tokens+c.output_tokens,activeDurationMs:c.active_duration_ms??0,lastActiveAt:c.last_active_at??null}))}catch(r){throw new d("DB_ERROR","Failed to get provider usage",r)}finally{s.close();}}};var St=class extends T{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let s=dirname(e);existsSync(s)||mkdirSync(s,{recursive:true});}else if(!existsSync(e))throw new d("NOT_FOUND","Database not found");let n=k(e);if(t)try{O(n.db,e);}catch(s){throw n.close(),s}return n}validateWorkspaceId(t,e){return t.db.select({id:_.id}).from(_).where(eq(_.id,e)).limit(1).get()?e:null}topLevelTaskPredicateSql(t="child"){return sql.raw(`(
|
|
92
206
|
${t}.parent_task_id IS NULL
|
|
93
207
|
OR ${t}.parent_task_id = ''
|
|
94
208
|
OR NOT EXISTS (
|
|
@@ -96,7 +210,7 @@ ${n.join(`
|
|
|
96
210
|
WHERE parent.id = ${t}.parent_task_id
|
|
97
211
|
AND parent.thread_id = ${t}.thread_id
|
|
98
212
|
)
|
|
99
|
-
)`)}findAllThreads(t){let e=this.resolveDbPaths(),n=new Set,s=[];for(let r of e){if(!existsSync(r))continue;let o=k(r);try{let l=t?eq(
|
|
213
|
+
)`)}findAllThreads(t){let e=this.resolveDbPaths(),n=new Set,s=[];for(let r of e){if(!existsSync(r))continue;let o=k(r);try{let l=t?eq(p.workspace_id,t):void 0,u=o.db.select().from(p).where(l).orderBy(desc(p.updated_at)).all();for(let c of u)n.has(c.id)||(n.add(c.id),s.push(c));}catch(l){throw new d("DB_ERROR","Failed to find all threads",l)}finally{o.close();}}return s}findThreadById(t,e){let n=this.resolveDbPaths();for(let s of n){if(!existsSync(s))continue;let r=k(s);try{let o=eq(p.id,t),l=e?and(o,eq(p.workspace_id,e)):o,u=r.db.select().from(p).where(l).limit(1).get()??void 0;if(u)return u}catch(o){throw new d("DB_ERROR","Failed to find thread by id",o)}finally{r.close();}}}threadExists(t,e){let n=this.resolveDbPaths();for(let s of n){if(!existsSync(s))continue;let r=k(s);try{let o=eq(p.id,t),l=e?and(o,eq(p.workspace_id,e)):o;if(r.db.select({id:p.id}).from(p).where(l).limit(1).get())return !0}catch(o){throw new d("DB_ERROR","Failed to check thread existence",o)}finally{r.close();}}return false}aggregateTaskStats(t,e){let n=this.resolveDbPaths(),s=0,r=0,o=0,l=0,u=0,c=new Set;for(let h of n){if(!existsSync(h))continue;let m=k(h);try{let w=m.db.get(sql`
|
|
100
214
|
SELECT
|
|
101
215
|
count(*) AS cnt,
|
|
102
216
|
COALESCE(SUM(child.input_tokens), 0) AS total_input,
|
|
@@ -107,25 +221,36 @@ ${n.join(`
|
|
|
107
221
|
WHERE child.thread_id = ${t}
|
|
108
222
|
AND ${this.topLevelTaskPredicateSql()}
|
|
109
223
|
${e?sql`AND child.workspace_id = ${e}`:sql``}
|
|
110
|
-
`);
|
|
224
|
+
`);w&&(s+=w.cnt,r+=w.total_input,o+=w.total_output,l+=w.total_cached,u+=w.total_cost);let y=m.db.all(sql`
|
|
111
225
|
SELECT DISTINCT child.agent_id FROM tasks child
|
|
112
226
|
WHERE child.thread_id = ${t}
|
|
113
227
|
AND child.agent_id IS NOT NULL AND child.agent_id != ''
|
|
114
228
|
AND ${this.topLevelTaskPredicateSql()}
|
|
115
229
|
${e?sql`AND child.workspace_id = ${e}`:sql``}
|
|
116
|
-
`);for(let
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
230
|
+
`);for(let q of y)c.add(q.agent_id);}catch(w){throw new d("DB_ERROR","Failed to aggregate task stats",w)}finally{m.close();}}return {taskCount:s,inputTokens:r,outputTokens:o,cachedInputTokens:l,costUsd:u,agentIds:Array.from(c)}}findTopLevelTasks(t,e,n){let s=this.resolveDbPaths(),r=new Set,o=[];for(let l of s){if(!existsSync(l))continue;let u=k(l);try{let c;n!==void 0?c=u.db.all(sql`
|
|
231
|
+
SELECT * FROM (
|
|
232
|
+
SELECT child.*,
|
|
233
|
+
ROW_NUMBER() OVER (PARTITION BY child.agent_id ORDER BY child.started_at DESC) AS rn
|
|
234
|
+
FROM tasks child
|
|
235
|
+
WHERE child.thread_id = ${t}
|
|
236
|
+
AND ${this.topLevelTaskPredicateSql()}
|
|
237
|
+
${e?sql`AND child.workspace_id = ${e}`:sql``}
|
|
238
|
+
) ranked
|
|
239
|
+
WHERE rn <= ${n}
|
|
240
|
+
ORDER BY started_at ASC
|
|
241
|
+
`):c=u.db.all(sql`
|
|
242
|
+
SELECT child.* FROM tasks child
|
|
243
|
+
WHERE child.thread_id = ${t}
|
|
244
|
+
AND ${this.topLevelTaskPredicateSql()}
|
|
245
|
+
${e?sql`AND child.workspace_id = ${e}`:sql``}
|
|
246
|
+
ORDER BY child.started_at ASC
|
|
247
|
+
`);for(let h of c)r.has(h.id)||(r.add(h.id),o.push(h));}catch(c){throw new d("DB_ERROR","Failed to find top-level tasks",c)}finally{u.close();}}if(n!==void 0&&s.length>1){let l=new Map;for(let u of o){let c=u.agent_id??"";l.has(c)||l.set(c,[]),l.get(c).push(u);}o=[];for(let u of l.values())u.sort((c,h)=>(h.started_at??"").localeCompare(c.started_at??"")),o.push(...u.slice(0,n));o.sort((u,c)=>(u.started_at??"").localeCompare(c.started_at??""));}return o}findAllTasks(t,e){let n=this.resolveDbPaths(),s=new Set,r=[];for(let o of n){if(!existsSync(o))continue;let l=k(o);try{let u=eq(i.thread_id,t),c=e?and(u,eq(i.workspace_id,e)):u,h=l.db.select().from(i).where(c).orderBy(asc(i.started_at)).all();for(let m of h)s.has(m.id)||(s.add(m.id),r.push(m));}catch(u){throw new d("DB_ERROR","Failed to find all tasks for thread",u)}finally{l.close();}}return r}findTaskById(t,e,n){let s=this.resolveDbPaths();for(let r of s){if(!existsSync(r))continue;let o=k(r);try{let l=and(eq(i.id,e),eq(i.thread_id,t)),u=n?and(l,eq(i.workspace_id,n)):l,c=o.db.select().from(i).where(u).limit(1).get();if(!c)continue;let h=o.db.select().from(i).where(eq(i.parent_task_id,c.id)).orderBy(asc(i.started_at)).all();return {task:c,children:h}}catch(l){throw new d("DB_ERROR","Failed to find task by id",l)}finally{o.close();}}}batchFetchTasks(t,e){let n=new Map;if(t.length===0)return n;let s=this.resolveDbPaths();for(let r of s){if(!existsSync(r))continue;let o=k(r);try{let l=o.db.all(sql`
|
|
123
248
|
SELECT child.* FROM tasks child
|
|
124
249
|
WHERE child.thread_id IN (${sql.join(t.map(u=>sql`${u}`),sql`, `)})
|
|
125
250
|
AND ${this.topLevelTaskPredicateSql()}
|
|
126
251
|
${e?sql`AND child.workspace_id = ${e}`:sql``}
|
|
127
252
|
ORDER BY child.started_at ASC
|
|
128
|
-
`);for(let u of l){let
|
|
253
|
+
`);for(let u of l){let c=u.thread_id;n.has(c)||n.set(c,[]),n.get(c).push(u);}}catch(l){throw new d("DB_ERROR","Failed to batch fetch tasks",l)}finally{o.close();}}return n}updateThreadTitle(t,e,n){if(!this.dbExists())return;let s=this.openHandle(true);try{let r=eq(p.id,t),o=n?and(r,eq(p.workspace_id,n)):r;if(!s.db.select({id:p.id}).from(p).where(o).limit(1).get())return;s.db.update(p).set({title:e,title_locked:1,updated_at:new Date().toISOString()}).where(eq(p.id,t)).run();}catch(r){throw r instanceof d?r:new d("DB_ERROR","Failed to update thread title",r)}finally{s.close();}}upsertThread(t,e){let n=this.openHandle(true);try{let s=e.workspaceId?this.validateWorkspaceId(n,e.workspaceId):null,r=new Date().toISOString();if(n.db.select({id:p.id,message_count:p.message_count}).from(p).where(eq(p.id,t)).limit(1).get()){let l={updated_at:r};e.title!==void 0&&(l.title=e.title),e.titleLocked!==void 0&&(l.title_locked=e.titleLocked?1:0),n.db.update(p).set(l).where(eq(p.id,t)).run();}else n.db.insert(p).values({id:t,platform:e.platform,workspace_id:s,title:e.title??null,title_locked:e.titleLocked?1:0,message_count:0,created_at:r,updated_at:r}).run();}catch(s){throw s instanceof d?s:new d("DB_ERROR","Failed to upsert thread",s)}finally{n.close();}}ensureThread(t,e,n){let s=this.openHandle(true);try{let r=n?this.validateWorkspaceId(s,n):null,o=s.db.select({id:p.id,platform:p.platform,workspace_id:p.workspace_id}).from(p).where(eq(p.id,t)).limit(1).get();if(o){r&&!o.workspace_id&&s.db.update(p).set({workspace_id:r}).where(eq(p.id,t)).run();return}let l=new Date().toISOString();s.db.insert(p).values({id:t,platform:e,workspace_id:r,message_count:0,created_at:l,updated_at:l}).run();}catch(r){throw r instanceof d?r:new d("DB_ERROR","Failed to ensure thread",r)}finally{s.close();}}saveUserMessage(t,e,n){if(!this.dbExists())return {firstMessage:false};let s=this.openHandle(true);try{let r=new Date().toISOString();return {firstMessage:s.db.transaction(l=>{let c=l.select({message_count:p.message_count}).from(p).where(eq(p.id,t)).limit(1).get()?.message_count===0;return l.run(sql`
|
|
129
254
|
UPDATE threads
|
|
130
255
|
SET first_message = COALESCE(first_message, ${e}),
|
|
131
256
|
title = CASE WHEN title_locked = 0 AND title IS NULL THEN substr(${e}, 1, 60) ELSE title END,
|
|
@@ -133,4 +258,4 @@ ${n.join(`
|
|
|
133
258
|
message_count = message_count + 1,
|
|
134
259
|
updated_at = ${r}
|
|
135
260
|
WHERE id = ${t}
|
|
136
|
-
`),
|
|
261
|
+
`),c},{behavior:"immediate"})}}catch(r){throw r instanceof d?r:new d("DB_ERROR","Failed to save user message",r)}finally{s.close();}}saveAssistantMessage(t,e,n){if(!this.dbExists())return;let s=this.openHandle(true);try{let r=new Date().toISOString();s.db.update(p).set({last_message:e,updated_at:r}).where(eq(p.id,t)).run();}catch(r){throw r instanceof d?r:new d("DB_ERROR","Failed to save assistant message",r)}finally{s.close();}}updateThread(t,e){if(!this.dbExists())return;let n=this.openHandle(true);try{let s={updated_at:new Date().toISOString()};e.title!==void 0&&(s.title=e.title,s.title_locked=1),e.titleLocked!==void 0&&(s.title_locked=e.titleLocked?1:0),n.db.update(p).set(s).where(eq(p.id,t)).run();}catch(s){throw s instanceof d?s:new d("DB_ERROR","Failed to update thread",s)}finally{n.close();}}togglePin(t,e){let n=this.openHandle(true);try{let s=e?and(eq(p.id,t),eq(p.workspace_id,e)):eq(p.id,t),r=n.db.select({pinned:p.pinned,metadata:p.metadata}).from(p).where(s).get();if(!r)return null;let o=r.pinned?0:1,l=r.metadata?JSON.parse(r.metadata):{};if(o){let u=e?and(eq(p.pinned,1),eq(p.workspace_id,e)):eq(p.pinned,1),c=n.db.select({metadata:p.metadata}).from(p).where(u).all(),h=0;for(let m of c){let w=m.metadata?JSON.parse(m.metadata):{};typeof w.pinOrder=="number"&&w.pinOrder>h&&(h=w.pinOrder);}l.pinOrder=h+1;}else delete l.pinOrder;return n.db.update(p).set({pinned:o,metadata:Object.keys(l).length>0?JSON.stringify(l):null}).where(s).run(),{pinned:!!o}}catch(s){throw s instanceof d?s:new d("DB_ERROR","Failed to toggle pin",s)}finally{n.close();}}reorderPins(t,e){let n=this.openHandle(true);try{for(let s=0;s<t.length;s++){let r=e?and(eq(p.id,t[s]),eq(p.workspace_id,e)):eq(p.id,t[s]),o=n.db.select({metadata:p.metadata}).from(p).where(r).get();if(!o)continue;let l=o.metadata?JSON.parse(o.metadata):{};l.pinOrder=s+1,n.db.update(p).set({metadata:JSON.stringify(l)}).where(r).run();}}catch(s){throw s instanceof d?s:new d("DB_ERROR","Failed to reorder pins",s)}finally{n.close();}}toggleStar(t,e){let n=this.openHandle(true);try{let s=e?and(eq(p.id,t),eq(p.workspace_id,e)):eq(p.id,t),r=n.db.select({starred:p.starred}).from(p).where(s).get();if(!r)return null;let o=r.starred?0:1;return n.db.update(p).set({starred:o}).where(s).run(),{starred:!!o}}catch(s){throw s instanceof d?s:new d("DB_ERROR","Failed to toggle star",s)}finally{n.close();}}};var Tt=class extends T{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let s=dirname(e);existsSync(s)||mkdirSync(s,{recursive:true});}else if(!existsSync(e))throw new d("NOT_FOUND","Database not found");let n=k(e);if(t)try{O(n.db,e);}catch(s){throw n.close(),s}return n}insertSpan(t){let e=this.openHandle(true);try{e.db.insert(U).values(t).run();}catch(n){throw n instanceof d?n:new d("DB_ERROR","Failed to insert span",n)}finally{e.close();}}findByTaskId(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.select().from(U).where(eq(U.task_id,t)).all()}catch(n){throw new d("DB_ERROR","Failed to find spans by task id",n)}finally{e.close();}}findById(t){if(!this.dbExists())return;let e=this.openHandle(false);try{return e.db.select().from(U).where(eq(U.id,t)).limit(1).get()??void 0}catch(n){throw new d("DB_ERROR","Failed to find span by id",n)}finally{e.close();}}};var Dt=class extends T{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let s=dirname(e);existsSync(s)||mkdirSync(s,{recursive:true});}else if(!existsSync(e))throw new d("NOT_FOUND","Database not found");let n=k(e);if(t)try{O(n.db,e);}catch(s){throw n.close(),s}return n}insertToolCall(t){let e=this.openHandle(true);try{e.db.insert($).values(t).run();}catch(n){throw n instanceof d?n:new d("DB_ERROR","Failed to insert tool call",n)}finally{e.close();}}findByTaskId(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.select().from($).where(eq($.task_id,t)).all()}catch(n){throw new d("DB_ERROR","Failed to find tool calls by task id",n)}finally{e.close();}}aggregateByName(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.select({toolName:$.tool_name,count:sql`count(*)`}).from($).where(eq($.task_id,t)).groupBy($.tool_name).all().map(n=>({toolName:n.toolName,count:n.count}))}catch(n){throw new d("DB_ERROR","Failed to aggregate tool calls by name",n)}finally{e.close();}}};var xt=class extends T{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let s=dirname(e);existsSync(s)||mkdirSync(s,{recursive:true});}else if(!existsSync(e))throw new d("NOT_FOUND","Database not found");let n=k(e);if(t)try{O(n.db,e);}catch(s){throw n.close(),s}return n}ensureThreadExists(t,e){if(!t.db.select({id:p.id}).from(p).where(eq(p.id,e)).limit(1).get())throw new d("NOT_FOUND",`Thread not found: ${e}`)}findByThreadId(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return this.ensureThreadExists(e,t),e.db.select().from(L).where(eq(L.thread_id,t)).orderBy(asc(L.seq)).all()}catch(n){throw n instanceof d?n:new d("DB_ERROR","Failed to find boxes by thread id",n)}finally{e.close();}}findById(t,e){if(!this.dbExists())return;let n=this.openHandle(false);try{return n.db.select().from(L).where(and(eq(L.id,e),eq(L.thread_id,t))).limit(1).get()??void 0}catch(s){throw s instanceof d?s:new d("DB_ERROR","Failed to find box by id",s)}finally{n.close();}}insert(t){let e=this.openHandle(true);try{this.ensureThreadExists(e,t.threadId);try{e.db.insert(L).values({id:t.id,thread_id:t.threadId,seq:t.seq,first_task_id:t.first_task_id,mid_task_id:t.mid_task_id,last_task_id:t.last_task_id,task_count:t.task_count,summary:t.summary??null,source_tokens:t.source_tokens,summary_tokens:t.summary_tokens??null,created_at:t.created_at}).run();}catch(s){throw s instanceof Error&&/UNIQUE constraint failed/i.test(s.message)?new d("CONFLICT",`Duplicate seq ${String(t.seq)} for thread ${t.threadId}`,s):s}let n=e.db.select().from(L).where(eq(L.id,t.id)).limit(1).get();if(!n)throw new d("DB_ERROR","Insert did not return a row");return n}catch(n){throw n instanceof d?n:new d("DB_ERROR","Failed to insert thread box",n)}finally{e.close();}}};var Ot=class extends T{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let s=dirname(e);existsSync(s)||mkdirSync(s,{recursive:true});}else if(!existsSync(e))throw new d("NOT_FOUND","Database not found");let n=k(e);if(t)try{O(n.db,e);}catch(s){throw n.close(),s}return n}bulkInsert(t){if(t.length===0)return;let e=this.openHandle(true);try{let n=new Date().toISOString();e.db.transaction(s=>{for(let r of t)s.insert(Q).values({id:randomUUID(),path:r.path,method:r.method,status_code:r.statusCode,duration_ms:r.durationMs,ip:r.ip??null,request_headers:r.requestHeaders??null,response_headers:r.responseHeaders??null,request_body:r.requestBody??null,response_body:r.responseBody??null,query:r.query??null,user_id:r.userId??null,project_id:r.projectId??null,partition_key:r.partitionKey??"default",timestamp:n,metadata:r.metadata??null}).run();});}catch(n){throw n instanceof d?n:new d("DB_ERROR","Failed to bulk insert request logs",n)}finally{e.close();}}findRecent(t=100){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.select().from(Q).orderBy(desc(Q.timestamp)).limit(t).all()}catch(n){throw new d("DB_ERROR","Failed to find recent request logs",n)}finally{e.close();}}};export{T as BaseSqliteRepository,d as RepositoryError,Ot as RequestLogRepository,Tt as SpanRepository,yt as TaskRepository,xt as ThreadBoxRepository,St as ThreadRepository,Dt as ToolCallRepository,Et as WorkspaceRepository,k as openDrizzleDb,Pe as pushSchema,J as runMigrations,O as runMigrationsOnce};
|
package/dist/facade/Crewx.d.ts
CHANGED
|
@@ -97,6 +97,7 @@ export declare class Crewx extends TypedEventEmitter {
|
|
|
97
97
|
private _getConversationProvider;
|
|
98
98
|
private _fetchHistoryMessages;
|
|
99
99
|
private _resolveTimeout;
|
|
100
|
+
private _lookupCliAdapter;
|
|
100
101
|
private _resolveEffectiveCwd;
|
|
101
102
|
private _buildFileRemoteOptions;
|
|
102
103
|
private emitToolEvents;
|
package/dist/index.browser.d.ts
CHANGED
|
@@ -5,4 +5,5 @@ export type { ProviderRuntime, ProviderFactory, ProviderQueryOptions } from './p
|
|
|
5
5
|
export { parseYamlContent, ConfigLoadError } from './config/loader.browser.js';
|
|
6
6
|
export { BrowserFsAdapter } from './platform/BrowserFsAdapter.js';
|
|
7
7
|
export type { IFsAdapter } from './platform/IFsAdapter.js';
|
|
8
|
+
export { PROVIDER_ORDER, providerRank, compareProviders } from './provider/order.js';
|
|
8
9
|
export type { AgentConfig, CrewxProjectConfig, CrewxOptions, QueryOptions, QueryResult, ExecuteOptions, ExecuteResult, } from './types/index.js';
|
package/dist/index.browser.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {load}from'js-yaml';import {z}from'zod';var m=class extends Error{constructor(e,
|
|
2
|
-
`);y=
|
|
1
|
+
import {load}from'js-yaml';import {z}from'zod';var m=class extends Error{constructor(e,r){let t=r.length>0?` Available: ${r.join(", ")}`:"";super(`Agent not found: "${e}".${t}`),this.name="AgentNotFoundError";}},D={claude:{id:"claude",provider:"cli/claude"},copilot:{id:"copilot",provider:"cli/copilot"},codex:{id:"codex",provider:"cli/codex"}};function w(s,e){let r=s.startsWith("@")?s.slice(1):s,t=e.find(o=>o.id===r);if(t)return t;let i=D[r];if(i)return i;throw new m(s,e.map(o=>o.id))}var u=class extends Error{constructor(r,t,i){super(r);this.providerStr=t;this.name="ProviderError",this.code=i?.code??"UNKNOWN",this.kind=i?.kind??(this.code==="UNKNOWN"?"transient":void 0),this.retryAfterMs=i?.retryAfterMs;}providerStr;code;kind;retryAfterMs};var I=new Map;function P(s,e){I.set(s,e);}function x(s){let e=s.split("/");if(e.length!==2)throw new u(`Invalid provider format: "${s}". Expected namespace/id (e.g., api/webllm)`,s);let[r,t]=e,i=I.get(r);if(i)return i(t,s);throw new u(`Unsupported provider namespace: "${r}". Register a factory with registerProviderFactory('${r}', factory).`,s)}var j=new Function("u","return import(u)"),A=class s{_agents;_config;_tools=new Map;_apiProviders=new Map;constructor(e,r){this._agents=new Map(e.map(t=>[t.id,t])),this._config=r;}static async fromConfig(e,r){let t=new s(e.agents??[],e),i=e.agents??[],o=a=>Array.isArray(a.provider)?a.provider[0]:a.provider,p=i.filter(a=>o(a)==="api/webllm"),c=i.filter(a=>o(a)==="api/openrouter");if(p.length>0&&await t._initWebLLM(p,r?.onProgress),c.length>0){if(!r?.openrouterApiKey)throw new Error("openrouterApiKey is required when using api/openrouter provider");t._initOpenRouter(c,r.openrouterApiKey);}return t._apiProviders.size>0&&P("api",(a,g)=>{let d=t._apiProviders.get(a);if(!d)throw new u(`Unknown api provider: ${a}`,g);return d}),t}async _initWebLLM(e,r){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:r?c=>r(c):void 0}),p=[];this._apiProviders.set("webllm",{async query(c,a){let g=[];a?.systemPrompt&&g.push({role:"system",content:a.systemPrompt}),g.push(...p),g.push({role:"user",content:c});let d="",l=await o.chat.completions.create({messages:g,stream:true,temperature:.7,max_tokens:4096});for await(let y of l){let v=y.choices?.[0]?.delta?.content??"";d+=v,a?.onOutput&&a.onOutput(d,"stdout");}return p.push({role:"user",content:c}),p.push({role:"assistant",content:d}),d},async execute(c,a){return this.query(c,a)}});}_initOpenRouter(e,r){let t=e[0]?.inline?.model??"google/gemma-2-2b-it",i=[];this._apiProviders.set("openrouter",{async query(o,p){let c=[];p?.systemPrompt&&c.push({role:"system",content:p.systemPrompt}),c.push(...i),c.push({role:"user",content:o});let a=await fetch("https://openrouter.ai/api/v1/chat/completions",{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify({model:p?.model||t,messages:c,stream:true})});if(!a.ok){let v=await a.text();throw new Error(`OpenRouter API error (${a.status}): ${v}`)}let g=a.body.getReader(),d=new TextDecoder,l="",y="";for(;;){let{done:v,value:T}=await g.read();if(v)break;y+=d.decode(T,{stream:true});let O=y.split(`
|
|
2
|
+
`);y=O.pop();for(let M of O){let h=M.trim();if(!h||!h.startsWith("data: "))continue;let k=h.slice(6);if(k!=="[DONE]")try{let b=JSON.parse(k).choices?.[0]?.delta?.content??"";l+=b,b&&p?.onOutput&&p.onOutput(l,"stdout");}catch{}}}return i.push({role:"user",content:o}),i.push({role:"assistant",content:l}),l},async execute(o,p){return this.query(o,p)}});}get agents(){return this._agents}get config(){return this._config}get tools(){return this._tools}registerTool(e,r){this._tools.set(e,{name:e,...r});}async query(e,r,t){let i=Date.now(),o;try{o=w(e,Array.from(this._agents.values()));}catch(d){if(d instanceof m)return {ok:false,data:"",error:{code:"AGENT_NOT_FOUND",message:d.message},meta:{agentId:e.replace(/^@/,""),provider:"",durationMs:Date.now()-i}};throw d}let p=Array.isArray(o.provider)?o.provider[0]??"api/default":o.provider,c=t?.provider??p,a=t?.model??o.inline?.model,g;try{g=x(c);}catch(d){return {ok:false,data:"",error:{code:"PROVIDER_ERROR",message:d.message},meta:{agentId:o.id,provider:c,model:a,durationMs:Date.now()-i}}}try{return {ok:!0,data:await g.query(r,{model:a,context:t?.context,systemPrompt:o.inline?.system_prompt??o.inline?.prompt,onOutput:t?.onOutput?l=>t.onOutput(l):void 0}),meta:{agentId:o.id,provider:c,model:a,durationMs:Date.now()-i}}}catch(d){return {ok:false,data:"",error:{code:"QUERY_FAILED",message:d.message},meta:{agentId:o.id,provider:c,model:a,durationMs:Date.now()-i}}}}async execute(e,r,t){let i=Date.now(),o;try{o=w(e,Array.from(this._agents.values()));}catch(d){if(d instanceof m)return {ok:false,data:"",error:{code:"AGENT_NOT_FOUND",message:d.message},meta:{agentId:e.replace(/^@/,""),provider:"",durationMs:Date.now()-i}};throw d}let p=Array.isArray(o.provider)?o.provider[0]??"api/default":o.provider,c=t?.provider??p,a=t?.model??o.inline?.model,g;try{g=x(c);}catch(d){return {ok:false,data:"",error:{code:"PROVIDER_ERROR",message:d.message},meta:{agentId:o.id,provider:c,model:a,durationMs:Date.now()-i}}}try{return {ok:!0,data:await g.execute(r,{model:a,context:t?.context,systemPrompt:o.inline?.system_prompt??o.inline?.prompt}),meta:{agentId:o.id,provider:c,model:a,durationMs:Date.now()-i}}}catch(d){return {ok:false,data:"",error:{code:"EXECUTE_FAILED",message:d.message},meta:{agentId:o.id,provider:c,model:a,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()),U=z.object({include:z.array(z.string()).optional()}).optional(),N=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:U}).catchall(z.unknown()),_=z.object({agents:z.array(N).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(r,t){super(r);this.cause=t;this.name="ConfigLoadError";}cause};function Q(s){if(!s||typeof s!="string"||!s.trim())throw new f("YAML content must be a non-empty string");let e;try{e=load(s);}catch(i){throw new f(`YAML parse error: ${i.message}`,i)}let r=K(e),t=_.safeParse(r);if(!t.success)throw new f(`Config validation error: ${t.error.message}`);return t.data}function K(s){if(!s||typeof s!="object")return {agents:[]};let e=s;if(e.agents&&typeof e.agents=="object"&&!Array.isArray(e.agents)){let r=e.agents,t=Object.entries(r).map(([i,o])=>({id:i,...o&&typeof o=="object"?o:{}}));return {...e,agents:t}}return e.agents?e:{...e,agents:[]}}var $="crewx:fs:",E=class{prefix;store;constructor(e){this.prefix=e?.prefix??$,this.store=e?.storage??new Map;}async readFile(e){let r=this.toKey(e),t=this.store.get(r);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 r=e.map(p=>p.replace(/\\/g,"/")).join("/").replace(/\/+/g,"/"),t=r.split("/"),i=[];for(let p of t)p==="."||p===""||(p===".."?i.pop():i.push(p));let o=i.join("/");return r.startsWith("/")?`/${o}`:o}isAbsolute(e){return e.startsWith("/")}setItem(e,r){this.store.set(this.toKey(e),r);}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))}async readdir(e){let r=e.replace(/\\/g,"/").replace(/\/$/,""),t=this.toKey(r+"/"),i=[];for(let o of this.store.keys())if(o.startsWith(t)){let c=o.slice(t.length).split("/")[0];c&&!i.includes(c)&&i.push(c);}return i}toKey(e){return `${this.prefix}${e.replace(/\\/g,"/")}`}};var C=["codex","claude","opencode","antigravity","copilot"];function S(s){let e=s.indexOf("/");return e===-1?s:s.slice(e+1)}function R(s){let e=S(s),r=C.indexOf(e);return r===-1?C.length:r}function B(s,e){return R(s)-R(e)}export{E as BrowserFsAdapter,f as ConfigLoadError,A as Crewx,C as PROVIDER_ORDER,u as ProviderError,B as compareProviders,x as createProvider,Q as parseYamlContent,R as providerRank,P as registerProviderFactory};
|
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export { AgentSkillsSchema } from './types';
|
|
|
23
23
|
export { loadYamlFile, parseYamlContent, ConfigLoadError } from './config/loader';
|
|
24
24
|
export { resolveAgent, AgentNotFoundError } from './agent/resolver';
|
|
25
25
|
export { resolveAgentSkills } from './agent/resolve-skills';
|
|
26
|
+
export { PROVIDER_ORDER, providerRank, compareProviders } from './provider/order';
|
|
26
27
|
export { createProvider, ProviderError, ClientToolCallRequiredError, registerProviderFactory } from './provider/bridge';
|
|
27
28
|
export type { ProviderRuntime, ProviderQueryOptions, ProviderFactory, ProviderUsage, ClientToolCall, ContinuationState } from './provider/bridge';
|
|
28
29
|
export { VercelProviderRuntime } from './provider/vercel-runtime';
|
|
@@ -31,7 +32,8 @@ export { registerApiProviders } from './provider/register-api';
|
|
|
31
32
|
export * from './provider/acp/index.js';
|
|
32
33
|
export type { CliProviderAdapter } from './provider/cli/adapter.types';
|
|
33
34
|
export { RateLimitError } from './provider/cli/adapter.types';
|
|
34
|
-
export { BUILTIN_ADAPTERS, claudeAdapter,
|
|
35
|
+
export { BUILTIN_ADAPTERS, claudeAdapter, copilotAdapter, codexAdapter, opencodeAdapter } from './provider/cli/adapters/index';
|
|
36
|
+
export { queryAllCliProviderMetas } from './provider/cli/meta';
|
|
35
37
|
export { createDelegateTool } from './tools/delegate';
|
|
36
38
|
export { resolveTemplatesPath, resolveDefaultAgentsYaml } from './paths';
|
|
37
39
|
export { resolveCrewxCli, resolveCrewxWorkspace } from './utils/env-defaults';
|