@crewx/sdk 0.8.7-rc.23 → 0.8.7-rc.24
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,4 +1,4 @@
|
|
|
1
|
-
import*as z from'path';import z__default,{join,dirname}from'path';import {fileURLToPath}from'url';import {existsSync,mkdirSync,writeFileSync,appendFileSync}from'fs';import Lt,{homedir}from'os';import {createHash}from'crypto';import {sql,eq,desc,and,or,isNull,like,asc,inArray,gte,lt as lt$1}from'drizzle-orm';import {sqliteTable,text,integer,real,index,unique}from'drizzle-orm/sqlite-core';var H=(d=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(d,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):d)(function(d){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+d+'" is not supported')});var St=()=>fileURLToPath(import.meta.url),xt=()=>z__default.dirname(St()),
|
|
1
|
+
import*as z from'path';import z__default,{join,dirname}from'path';import {fileURLToPath}from'url';import {existsSync,mkdirSync,writeFileSync,appendFileSync}from'fs';import Lt,{homedir}from'os';import {createHash}from'crypto';import {sql,eq,desc,and,or,isNull,like,asc,inArray,gte,lt as lt$1}from'drizzle-orm';import {sqliteTable,text,integer,real,index,unique}from'drizzle-orm/sqlite-core';var H=(d=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(d,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):d)(function(d){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+d+'" is not supported')});var St=()=>fileURLToPath(import.meta.url),xt=()=>z__default.dirname(St()),k=xt();var A=class{detach(t){}};function lt(d){let t=e=>String(e).padStart(2,"0");return `${d.getFullYear()}${t(d.getMonth()+1)}${t(d.getDate())}T${t(d.getHours())}${t(d.getMinutes())}${t(d.getSeconds())}`}var tt=class extends A{name="file-logger";unsubs=[];logFiles=new Map;logsDir;version;constructor(t){super(),this.logsDir=join(t?.workspaceRoot??process.cwd(),".crewx","logs"),this.version=t?.version??"unknown";}attach(t){this.unsubs.push(t.on("task:start",e=>{try{existsSync(this.logsDir)||mkdirSync(this.logsDir,{recursive:!0});let s=lt(e.timestamp),n=join(this.logsDir,`${s}_${e.traceId}.log`);this.logFiles.set(e.traceId,n);let r=`=== TASK LOG: ${e.traceId} ===
|
|
2
2
|
CrewX Version: ${this.version}
|
|
3
3
|
Mode: ${e.mode}
|
|
4
4
|
Agent: ${e.agentRef}
|
|
@@ -8,11 +8,11 @@ Message: ${e.message}
|
|
|
8
8
|
`;writeFileSync(n,r,{encoding:"utf8",mode:384});}catch{}}),t.on("task:output",e=>{try{let s=this.logFiles.get(e.traceId);if(!s)return;let n=new Date().toISOString();appendFileSync(s,`[${n}] STDOUT: ${e.output}
|
|
9
9
|
`,"utf8");}catch{}}),t.on("task:end",e=>{try{let s=this.logFiles.get(e.traceId);if(!s)return;let n=new Date().toLocaleString(),r=e.error?`failed: ${e.error.message}`:"completed successfully",i=`[${n}] INFO: Task ${r} in ${e.durationMs}ms
|
|
10
10
|
[${n}] INFO: Process closed with exit code: ${e.error?1:0}
|
|
11
|
-
`;appendFileSync(s,i,"utf8"),this.logFiles.delete(e.traceId);}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[],this.logFiles.clear();}};function vt(d){let t=z.resolve(d);return process.platform==="win32"&&(t=t.replace(/\\/g,"/"),t=t.replace(/^([A-Z]):/,(e,s)=>`${s.toLowerCase()}:`)),t.length>1&&!/^[a-zA-Z]:\/$/.test(t)&&(t=t.replace(/\/+$/,"")),t}function _t(d){let t=vt(d);return createHash("sha256").update(t).digest("hex")}var F=class{resolveDbPath(){return process.env.CREWX_DB?process.env.CREWX_DB:process.env.CREWX_TRACES_DB?process.env.CREWX_TRACES_DB:join(Lt.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())}};function b(d){let t=H("better-sqlite3"),{drizzle:e}=H("drizzle-orm/better-sqlite3"),s=new t(d);return s.exec("PRAGMA journal_mode = WAL"),s.exec("PRAGMA busy_timeout = 5000"),s.exec("PRAGMA foreign_keys = ON"),{db:e(s),runRaw:(n,r=[])=>s.prepare(n).run(...r),close:()=>s.close()}}var
|
|
11
|
+
`;appendFileSync(s,i,"utf8"),this.logFiles.delete(e.traceId);}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[],this.logFiles.clear();}};function vt(d){let t=z.resolve(d);return process.platform==="win32"&&(t=t.replace(/\\/g,"/"),t=t.replace(/^([A-Z]):/,(e,s)=>`${s.toLowerCase()}:`)),t.length>1&&!/^[a-zA-Z]:\/$/.test(t)&&(t=t.replace(/\/+$/,"")),t}function _t(d){let t=vt(d);return createHash("sha256").update(t).digest("hex")}var F=class{resolveDbPath(){return process.env.CREWX_DB?process.env.CREWX_DB:process.env.CREWX_TRACES_DB?process.env.CREWX_TRACES_DB:join(Lt.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())}};function b(d){let t=H("better-sqlite3"),{drizzle:e}=H("drizzle-orm/better-sqlite3"),s=new t(d);return s.exec("PRAGMA journal_mode = WAL"),s.exec("PRAGMA busy_timeout = 5000"),s.exec("PRAGMA foreign_keys = ON"),{db:e(s),runRaw:(n,r=[])=>s.prepare(n).run(...r),close:()=>s.close()}}var mt=new Set,Ut={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 Mt(d,t){return (d.get(`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='${t}'`)?.cnt??0)>0}function Ft(d,t){if(t>0||!Mt(d,"tasks"))return;let e=d.all("PRAGMA table_info(tasks)"),s=new Set(e.map(n=>n.name));for(let[n,r]of Object.entries(Ut))s.has(n)||d.run(`ALTER TABLE tasks ADD COLUMN ${n} ${r}`);}function $t(d){let{migrate:t}=H("drizzle-orm/better-sqlite3/migrator"),{sql:e}=H("drizzle-orm"),s=[z__default.join(k,"../migrations"),z__default.join(k,"migrations"),z__default.join(k,"../../../../drizzle/migrations"),z__default.join(process.cwd(),"drizzle/migrations")],n=s.find(_=>existsSync(z__default.join(_,"meta/_journal.json")));if(!n)throw new Error(`migrations folder not found. Searched:
|
|
12
12
|
${s.join(`
|
|
13
|
-
`)}`);let r=d.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),i=0;r?.cnt&&(i=d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),Ft(d,i),t(d,{migrationsFolder:n});let
|
|
13
|
+
`)}`);let r=d.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),i=0;r?.cnt&&(i=d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),Ft(d,i),t(d,{migrationsFolder:n});let c=(d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0)-i;if(c>0){let _=r?.cnt?"Database migrated":"Database initialized";console.log(`[crewx] ${_} (${c} migration${c>1?"s":""} applied).`);}}function j(d,t){mt.has(t)||($t(d),mt.add(t));}var u=class extends Error{code;cause;constructor(t,e,s){super(e),this.name="RepositoryError",this.code=t,this.cause=s,Object.setPrototypeOf(this,new.target.prototype);}};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 o=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)},d=>({idx_tasks_agent_id:index("idx_tasks_agent_id").on(d.agent_id),idx_tasks_status:index("idx_tasks_status").on(d.status),idx_tasks_started_at:index("idx_tasks_started_at").on(d.started_at),idx_tasks_trace_id:index("idx_tasks_trace_id").on(d.trace_id),idx_tasks_parent_task_id:index("idx_tasks_parent_task_id").on(d.parent_task_id),idx_tasks_crewx_version:index("idx_tasks_crewx_version").on(d.crewx_version),idx_tasks_pid:index("idx_tasks_pid").on(d.pid),idx_tasks_thread_id:index("idx_tasks_thread_id").on(d.thread_id),idx_tasks_workspace_id:index("idx_tasks_workspace_id").on(d.workspace_id),idx_tasks_workspace_ref:index("idx_tasks_workspace_ref").on(d.workspace_ref),idx_tasks_project_id:index("idx_tasks_project_id").on(d.project_id),idx_tasks_ws_started:index("idx_tasks_ws_started").on(d.workspace_id,d.started_at)}));var l=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)},d=>({idx_threads_updated_at:index("idx_threads_updated_at").on(d.updated_at),idx_threads_workspace_id:index("idx_threads_workspace_id").on(d.workspace_id)}));var Gt=sqliteTable("spans",{id:text("id").primaryKey(),task_id:text("task_id").references(()=>o.id,{onDelete:"set null"}),parent_span_id:text("parent_span_id").references(()=>Gt.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")},d=>({idx_spans_task_id:index("idx_spans_task_id").on(d.task_id),idx_spans_parent_span_id:index("idx_spans_parent_span_id").on(d.parent_span_id)}));sqliteTable("tool_calls",{id:text("id").primaryKey(),task_id:text("task_id").references(()=>o.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()},d=>({idx_tool_calls_task_id:index("idx_tool_calls_task_id").on(d.task_id),idx_tool_calls_tool_name:index("idx_tool_calls_tool_name").on(d.tool_name),idx_tool_calls_timestamp:index("idx_tool_calls_timestamp").on(d.timestamp)}));sqliteTable("thread_boxes",{id:text("id").primaryKey(),thread_id:text("thread_id").notNull().references(()=>l.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()},d=>({idx_thread_boxes_thread_id:index("idx_thread_boxes_thread_id").on(d.thread_id),idx_thread_boxes_seq:index("idx_thread_boxes_seq").on(d.thread_id,d.seq),uniq_thread_boxes_thread_seq:unique().on(d.thread_id,d.seq)}));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")},d=>({idx_request_logs_timestamp:index("idx_request_logs_timestamp").on(d.timestamp),idx_request_logs_path:index("idx_request_logs_path").on(d.path),idx_request_logs_status_code:index("idx_request_logs_status_code").on(d.status_code),idx_request_logs_partition_key:index("idx_request_logs_partition_key").on(d.partition_key)}));var K=class extends F{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 n=dirname(e);existsSync(n)||mkdirSync(n,{recursive:true});}else if(!existsSync(e))throw new u("NOT_FOUND","Database not found");let s=b(e);if(t)try{j(s.db,e);}catch(n){throw s.close(),n}return s}startTask(t){let e=this.openHandle(true);try{e.db.insert(o).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(s){throw s instanceof u?s:new u("DB_ERROR","Failed to start task",s)}finally{e.close();}}finishTask(t){let e=this.openHandle(true);try{e.runRaw(`UPDATE tasks SET status=?, result=?, error=?, completed_at=?, duration_ms=?,
|
|
14
14
|
exit_code=?, input_tokens=?, output_tokens=?, cached_input_tokens=?, cost_usd=?,
|
|
15
|
-
model=COALESCE(?, model) WHERE id=?`,[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(s){throw s instanceof
|
|
15
|
+
model=COALESCE(?, model) WHERE id=?`,[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(s){throw s instanceof u?s:new u("DB_ERROR","Failed to finish task",s)}finally{e.close();}}appendLog(t,e){let s=this.openHandle(true);try{s.db.transaction(n=>{let r=n.select({logs:o.logs}).from(o).where(eq(o.id,t)).limit(1).get(),i=r?.logs?JSON.parse(r.logs):[];i.push(e),n.update(o).set({logs:JSON.stringify(i)}).where(eq(o.id,t)).run();},{behavior:"immediate"});}catch(n){throw n instanceof u?n:new u("DB_ERROR","Failed to append log",n)}finally{s.close();}}getRunningTasks(){if(!this.dbExists())return [];let t=this.openHandle(false);try{return t.db.select().from(o).where(eq(o.status,"running")).orderBy(desc(o.started_at)).all()}catch(e){throw new u("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(o).orderBy(desc(o.started_at)).limit(100).all()}catch(e){throw new u("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(o).where(eq(o.id,t)).limit(1).get()??void 0}catch(s){throw new u("DB_ERROR","Failed to get task",s)}finally{e.close();}}killTask(t){if(!this.dbExists())return {killed:false};let e=this.openHandle(true);try{let s=e.db.select({id:o.id,status:o.status,pid:o.pid}).from(o).where(eq(o.id,t)).limit(1).get();if(!s||s.status!=="running")return {killed:!1};if(s.pid)try{process.kill(s.pid,"SIGTERM");}catch{}return e.db.update(o).set({status:"failed",error:"Killed by user",completed_at:new Date().toISOString()}).where(and(eq(o.id,t),eq(o.status,"running"))).run(),{killed:!0,pid:s.pid??void 0}}catch(s){throw s instanceof u?s:new u("DB_ERROR","Failed to kill task",s)}finally{e.close();}}reapOrphanedTasks(){if(!this.dbExists())return 0;let t=this.openHandle(true);try{let e=t.db.select({id:o.id,pid:o.pid}).from(o).where(eq(o.status,"running")).all(),s=0;for(let n of e){let r=!1;if(n.pid)try{process.kill(n.pid,0),r=!0;}catch{}r||(t.db.update(o).set({status:"failed",error:"Reaped: process not found (orphaned task)",completed_at:new Date().toISOString()}).where(and(eq(o.id,n.id),eq(o.status,"running"))).run(),s++);}return s}finally{t.close();}}findTaskStatus(t,e){let s=this.resolveDbPaths();for(let n of s){if(!existsSync(n))continue;let r=b(n);try{let i=e?eq(o.workspace_id,e):void 0,a=i?and(eq(o.id,t),i):eq(o.id,t),c=r.db.select().from(o).where(a).limit(1).get()??void 0;if(!c){let _=or(eq(o.thread_id,t),and(isNull(o.thread_id),like(o.command,`%--thread=${t}%`))),m=i?and(_,i):_;c=r.db.select().from(o).where(m).orderBy(desc(o.started_at)).limit(1).get()??void 0;}if(c)return c}catch(i){throw new u("DB_ERROR","Failed to find task status",i)}finally{r.close();}}}findChildTasks(t,e){let s=this.resolveDbPaths(),n=new Set,r=[];for(let i of s){if(!existsSync(i))continue;let a=b(i);try{let c=e?and(eq(o.parent_task_id,t),eq(o.workspace_id,e)):eq(o.parent_task_id,t),_=a.db.select().from(o).where(c).orderBy(asc(o.started_at)).all();for(let m of _)n.has(m.id)||(n.add(m.id),r.push(m));}catch(c){throw new u("DB_ERROR","Failed to find child tasks",c)}finally{a.close();}}return r}getWorkspaceUsageSummary(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.all(t?sql`
|
|
16
16
|
SELECT
|
|
17
17
|
COALESCE(workspace_id, 'unknown') AS workspace_id,
|
|
18
18
|
COALESCE(SUM(input_tokens), 0) AS input_tokens,
|
|
@@ -33,7 +33,7 @@ ${s.join(`
|
|
|
33
33
|
FROM tasks
|
|
34
34
|
GROUP BY workspace_id
|
|
35
35
|
ORDER BY (COALESCE(SUM(input_tokens), 0) + COALESCE(SUM(output_tokens), 0)) DESC
|
|
36
|
-
`)}catch(s){throw new
|
|
36
|
+
`)}catch(s){throw new u("DB_ERROR","Failed to get workspace usage summary",s)}finally{e.close();}}getThreadTokenUsage(t,e){let s=this.resolveDbPaths(),n=new Set,r=0,i=0,a=0;for(let c of s){if(!existsSync(c))continue;let _=b(c);try{let m=or(eq(o.thread_id,t),and(isNull(o.thread_id),like(o.command,`%--thread=${t}%`))),h=e?and(m,eq(o.workspace_id,e)):m,x=_.db.select({id:o.id,input_tokens:o.input_tokens,output_tokens:o.output_tokens,cost_usd:o.cost_usd}).from(o).where(h).all();for(let P of x)n.has(P.id)||(n.add(P.id),r+=P.input_tokens??0,i+=P.output_tokens??0,a+=P.cost_usd??0);}catch(m){throw new u("DB_ERROR","Failed to get thread token usage",m)}finally{_.close();}}return {inputTokens:r,outputTokens:i,costUsd:a}}findTasksByThread(t,e){let s=this.resolveDbPaths(),n=new Set,r=[];for(let i of s){if(!existsSync(i))continue;let a=b(i);try{let c=or(eq(o.thread_id,t),and(isNull(o.thread_id),like(o.command,`%--thread=${t}%`))),_=e?and(c,eq(o.workspace_id,e)):c,m=a.db.select().from(o).where(_).orderBy(asc(o.started_at)).all();for(let h of m)n.has(h.id)||(n.add(h.id),r.push(h));}catch(c){throw new u("DB_ERROR","Failed to find tasks by thread",c)}finally{a.close();}}return r}findAllTasks(t){if(!this.dbExists())return {rows:[],total:0};let e=this.openHandle(false);try{let s=[];t.workspaceId&&s.push(eq(o.workspace_id,t.workspaceId));let n=t.agents&&t.agents.length>0?t.agents:t.agentId?[t.agentId]:null;n&&s.push(inArray(o.agent_id,n));let r=t.statuses&&t.statuses.length>0?t.statuses:t.status?[t.status]:null;r&&s.push(inArray(o.status,r));let i=t.q??t.search;i&&s.push(like(o.prompt,`%${i}%`)),t.from&&s.push(gte(o.started_at,t.from)),t.to&&s.push(lt$1(o.started_at,t.to));let a=s.length>0?and(...s):void 0,c=e.db.select({count:sql`count(*)`}).from(o).where(a).get(),_=(t.sortDir??"DESC")==="ASC"?asc(o.started_at):desc(o.started_at);return {rows:e.db.select().from(o).where(a).orderBy(_).limit(t.limit).offset(t.offset).all(),total:c?.count??0}}catch(s){throw new u("DB_ERROR","Failed to find all tasks",s)}finally{e.close();}}getAgentUsage(t,e,s){if(!this.dbExists())return [];let n=this.openHandle(false);try{return n.db.all(s?sql`
|
|
37
37
|
SELECT
|
|
38
38
|
t.agent_id,
|
|
39
39
|
t.workspace_id,
|
|
@@ -64,7 +64,7 @@ ${s.join(`
|
|
|
64
64
|
AND t.started_at < ${e}
|
|
65
65
|
GROUP BY t.agent_id, t.workspace_id
|
|
66
66
|
ORDER BY (COALESCE(SUM(t.input_tokens), 0) + COALESCE(SUM(t.output_tokens), 0)) DESC
|
|
67
|
-
`).map(i=>({agentId:i.agent_id,workspaceId:i.workspace_id??null,totalTasks:i.total_tasks,inputTokens:i.input_tokens,outputTokens:i.output_tokens,cachedInputTokens:i.cached_input_tokens,costUsd:i.cost_usd}))}catch(r){throw new
|
|
67
|
+
`).map(i=>({agentId:i.agent_id,workspaceId:i.workspace_id??null,totalTasks:i.total_tasks,inputTokens:i.input_tokens,outputTokens:i.output_tokens,cachedInputTokens:i.cached_input_tokens,costUsd:i.cost_usd}))}catch(r){throw new u("DB_ERROR","Failed to get agent usage",r)}finally{n.close();}}getAgentUsageTrendRaw(t,e,s){if(!this.dbExists())return [];let n=this.openHandle(false);try{return n.db.all(s?sql`
|
|
68
68
|
SELECT
|
|
69
69
|
date(t.started_at) AS date,
|
|
70
70
|
t.agent_id,
|
|
@@ -93,13 +93,13 @@ ${s.join(`
|
|
|
93
93
|
AND t.started_at < ${e}
|
|
94
94
|
GROUP BY date(t.started_at), t.agent_id
|
|
95
95
|
ORDER BY date(t.started_at) ASC
|
|
96
|
-
`).map(i=>({date:i.date,agentId:i.agent_id,inputTokens:i.input_tokens,outputTokens:i.output_tokens,cachedInputTokens:i.cached_input_tokens,costUsd:i.cost_usd}))}catch(r){throw new
|
|
96
|
+
`).map(i=>({date:i.date,agentId:i.agent_id,inputTokens:i.input_tokens,outputTokens:i.output_tokens,cachedInputTokens:i.cached_input_tokens,costUsd:i.cost_usd}))}catch(r){throw new u("DB_ERROR","Failed to get agent usage trend",r)}finally{n.close();}}findTaskForStop(t,e){if(!this.dbExists())return;let s=this.openHandle(false);try{return s.db.select().from(o).where(and(eq(o.id,t),eq(o.workspace_id,e))).limit(1).get()??void 0}catch(n){throw new u("DB_ERROR","Failed to find task for stop",n)}finally{s.close();}}markTaskFailed(t,e,s){if(!this.dbExists())return;let n=this.openHandle(true);try{let r=new Date().toISOString(),i=s?and(eq(o.id,t),eq(o.status,"running"),eq(o.workspace_id,s)):and(eq(o.id,t),eq(o.status,"running"));n.db.update(o).set({status:"failed",error:e,completed_at:r}).where(i).run();}catch(r){throw r instanceof u?r:new u("DB_ERROR","Failed to mark task failed",r)}finally{n.close();}}findTasksByPromptHint(t,e){let s=this.resolveDbPaths(),n=new Set,r=[];for(let i of s){if(!existsSync(i))continue;let a=b(i);try{let c=e?and(like(o.prompt,`%${t}%`),eq(o.workspace_id,e)):like(o.prompt,`%${t}%`),_=a.db.select().from(o).where(c).orderBy(asc(o.started_at)).all();for(let m of _)n.has(m.id)||(n.add(m.id),r.push(m));}catch(c){throw new u("DB_ERROR","Failed to find tasks by prompt hint",c)}finally{a.close();}}return r}};var ot=class extends A{name="sqlite-tracing";unsubs=[];dbPath;version;constructor(t){super(),this.dbPath=join(t?.dbRoot??homedir(),".crewx","crewx.db"),this.version=t?.version??"unknown";}attach(t){let e=new K({dbPath:this.dbPath}),s=process.cwd(),r=existsSync(join(s,"crewx.yaml"))||existsSync(join(s,"crewx.yml"))?_t(s):null,i=process.argv.join(" ");this.unsubs.push(t.on("task:start",a=>{try{let c=process.env.CREWX_CALLER_AGENT_ID||null,_=process.env.CREWX_PARENT_TASK_ID||null,m=process.env.CREWX_TRACE_ID||a.traceId,h=a.metadata?JSON.stringify(a.metadata):JSON.stringify({provider:a.provider??"cli/claude"});e.startTask({id:a.traceId,agentId:a.agentRef.replace(/^@/,""),prompt:a.message,mode:a.mode,status:"running",pid:a.pid??null,startedAt:a.timestamp.toISOString(),crewxVersion:this.version,platform:a.platform??"cli",model:a.model??null,renderedPrompt:a.renderedPrompt??null,command:i,codingAgentCommand:a.codingAgentCommand??null,workspaceId:a.workspaceId??r,callerAgentId:c,parentTaskId:_,traceId:m,metadata:h,threadId:a.threadId??null});}catch{}}),t.on("task:output",a=>{try{e.appendLog(a.traceId,{timestamp:a.timestamp.toISOString(),level:a.level??"stdout",message:a.output});}catch{}}),t.on("task:end",a=>{try{e.finishTask({id:a.traceId,status:a.error?"failed":"success",result:a.result??null,error:a.error?JSON.stringify(a.error):null,completedAt:a.timestamp.toISOString(),durationMs:a.durationMs,exitCode:a.exitCode??null,inputTokens:a.inputTokens??0,outputTokens:a.outputTokens??0,cachedInputTokens:a.cachedInputTokens??0,costUsd:a.costUsd??0,model:a.model??null});}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[];}};var Q=class extends F{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 n=dirname(e);existsSync(n)||mkdirSync(n,{recursive:true});}else if(!existsSync(e))throw new u("NOT_FOUND","Database not found");let s=b(e);if(t)try{j(s.db,e);}catch(n){throw s.close(),n}return s}validateWorkspaceId(t,e){return t.db.select({id:$.id}).from($).where(eq($.id,e)).limit(1).get()?e:null}findAllThreads(t){let e=this.resolveDbPaths(),s=new Set,n=[];for(let r of e){if(!existsSync(r))continue;let i=b(r);try{let a=t?eq(l.workspace_id,t):void 0,c=i.db.select().from(l).where(a).orderBy(desc(l.updated_at)).all();for(let _ of c)s.has(_.id)||(s.add(_.id),n.push(_));}catch(a){throw new u("DB_ERROR","Failed to find all threads",a)}finally{i.close();}}return n}findThreadById(t,e){let s=this.resolveDbPaths();for(let n of s){if(!existsSync(n))continue;let r=b(n);try{let i=eq(l.id,t),a=e?and(i,eq(l.workspace_id,e)):i,c=r.db.select().from(l).where(a).limit(1).get()??void 0;if(c)return c}catch(i){throw new u("DB_ERROR","Failed to find thread by id",i)}finally{r.close();}}}threadExists(t,e){let s=this.resolveDbPaths();for(let n of s){if(!existsSync(n))continue;let r=b(n);try{let i=eq(l.id,t),a=e?and(i,eq(l.workspace_id,e)):i;if(r.db.select({id:l.id}).from(l).where(a).limit(1).get())return !0}catch(i){throw new u("DB_ERROR","Failed to check thread existence",i)}finally{r.close();}}return false}aggregateTaskStats(t,e){let s=this.resolveDbPaths(),n=0,r=0,i=0,a=0,c=0,_=new Set;for(let m of s){if(!existsSync(m))continue;let h=b(m);try{let x=and(eq(o.thread_id,t),or(isNull(o.parent_task_id),eq(o.parent_task_id,""))),P=e?and(x,eq(o.workspace_id,e)):x,M=h.db.select({cnt:sql`count(*)`,total_input:sql`COALESCE(SUM(input_tokens), 0)`,total_output:sql`COALESCE(SUM(output_tokens), 0)`,total_cached:sql`COALESCE(SUM(cached_input_tokens), 0)`,total_cost:sql`COALESCE(SUM(cost_usd), 0)`}).from(o).where(P).get();M&&(n+=M.cnt,r+=M.total_input,i+=M.total_output,a+=M.total_cached,c+=M.total_cost);let yt=h.db.all(sql`
|
|
97
97
|
SELECT DISTINCT agent_id FROM tasks
|
|
98
98
|
WHERE thread_id = ${t}
|
|
99
99
|
AND agent_id IS NOT NULL AND agent_id != ''
|
|
100
100
|
AND (parent_task_id IS NULL OR parent_task_id = '')
|
|
101
101
|
${e?sql`AND workspace_id = ${e}`:sql``}
|
|
102
|
-
`);for(let Et of yt)
|
|
102
|
+
`);for(let Et of yt)_.add(Et.agent_id);}catch(x){throw new u("DB_ERROR","Failed to aggregate task stats",x)}finally{h.close();}}return {taskCount:n,inputTokens:r,outputTokens:i,cachedInputTokens:a,costUsd:c,agentIds:Array.from(_)}}findTopLevelTasks(t,e){let s=this.resolveDbPaths(),n=new Set,r=[];for(let i of s){if(!existsSync(i))continue;let a=b(i);try{let c=and(eq(o.thread_id,t),or(isNull(o.parent_task_id),eq(o.parent_task_id,""))),_=e?and(c,eq(o.workspace_id,e)):c,m=a.db.select().from(o).where(_).orderBy(asc(o.started_at)).all();for(let h of m)n.has(h.id)||(n.add(h.id),r.push(h));}catch(c){throw new u("DB_ERROR","Failed to find top-level tasks",c)}finally{a.close();}}return r}findAllTasks(t,e){let s=this.resolveDbPaths(),n=new Set,r=[];for(let i of s){if(!existsSync(i))continue;let a=b(i);try{let c=eq(o.thread_id,t),_=e?and(c,eq(o.workspace_id,e)):c,m=a.db.select().from(o).where(_).orderBy(asc(o.started_at)).all();for(let h of m)n.has(h.id)||(n.add(h.id),r.push(h));}catch(c){throw new u("DB_ERROR","Failed to find all tasks for thread",c)}finally{a.close();}}return r}findTaskById(t,e,s){let n=this.resolveDbPaths();for(let r of n){if(!existsSync(r))continue;let i=b(r);try{let a=and(eq(o.id,e),eq(o.thread_id,t)),c=s?and(a,eq(o.workspace_id,s)):a,_=i.db.select().from(o).where(c).limit(1).get();if(!_)continue;let m=i.db.select().from(o).where(eq(o.parent_task_id,_.id)).orderBy(asc(o.started_at)).all();return {task:_,children:m}}catch(a){throw new u("DB_ERROR","Failed to find task by id",a)}finally{i.close();}}}batchFetchTasks(t,e){let s=new Map;if(t.length===0)return s;let n=this.resolveDbPaths();for(let r of n){if(!existsSync(r))continue;let i=b(r);try{let a=and(inArray(o.thread_id,t),or(isNull(o.parent_task_id),eq(o.parent_task_id,""))),c=e?and(a,eq(o.workspace_id,e)):a,_=i.db.select().from(o).where(c).orderBy(asc(o.started_at)).all();for(let m of _){let h=m.thread_id;s.has(h)||s.set(h,[]),s.get(h).push(m);}}catch(a){throw new u("DB_ERROR","Failed to batch fetch tasks",a)}finally{i.close();}}return s}updateThreadTitle(t,e,s){if(!this.dbExists())return;let n=this.openHandle(true);try{let r=eq(l.id,t),i=s?and(r,eq(l.workspace_id,s)):r;if(!n.db.select({id:l.id}).from(l).where(i).limit(1).get())return;n.db.update(l).set({title:e,title_locked:1,updated_at:new Date().toISOString()}).where(eq(l.id,t)).run();}catch(r){throw r instanceof u?r:new u("DB_ERROR","Failed to update thread title",r)}finally{n.close();}}upsertThread(t,e){let s=this.openHandle(true);try{let n=e.workspaceId?this.validateWorkspaceId(s,e.workspaceId):null,r=new Date().toISOString();if(s.db.select({id:l.id,message_count:l.message_count}).from(l).where(eq(l.id,t)).limit(1).get()){let a={updated_at:r};e.title!==void 0&&(a.title=e.title),e.titleLocked!==void 0&&(a.title_locked=e.titleLocked?1:0),s.db.update(l).set(a).where(eq(l.id,t)).run();}else s.db.insert(l).values({id:t,platform:e.platform,workspace_id:n,title:e.title??null,title_locked:e.titleLocked?1:0,message_count:0,created_at:r,updated_at:r}).run();}catch(n){throw n instanceof u?n:new u("DB_ERROR","Failed to upsert thread",n)}finally{s.close();}}ensureThread(t,e,s){let n=this.openHandle(true);try{let r=s?this.validateWorkspaceId(n,s):null,i=n.db.select({id:l.id,platform:l.platform,workspace_id:l.workspace_id}).from(l).where(eq(l.id,t)).limit(1).get();if(i){r&&!i.workspace_id&&n.db.update(l).set({workspace_id:r}).where(eq(l.id,t)).run();return}let a=new Date().toISOString();n.db.insert(l).values({id:t,platform:e,workspace_id:r,message_count:0,created_at:a,updated_at:a}).run();}catch(r){throw r instanceof u?r:new u("DB_ERROR","Failed to ensure thread",r)}finally{n.close();}}saveUserMessage(t,e,s){if(!this.dbExists())return;let n=this.openHandle(true);try{let r=new Date().toISOString();n.db.run(sql`
|
|
103
103
|
UPDATE threads
|
|
104
104
|
SET first_message = COALESCE(first_message, ${e}),
|
|
105
105
|
title = CASE WHEN title_locked = 0 AND title IS NULL THEN substr(${e}, 1, 60) ELSE title END,
|
|
@@ -107,7 +107,7 @@ ${s.join(`
|
|
|
107
107
|
message_count = message_count + 1,
|
|
108
108
|
updated_at = ${r}
|
|
109
109
|
WHERE id = ${t}
|
|
110
|
-
`);}catch(r){throw r instanceof
|
|
110
|
+
`);}catch(r){throw r instanceof u?r:new u("DB_ERROR","Failed to save user message",r)}finally{n.close();}}saveAssistantMessage(t,e,s){if(!this.dbExists())return;let n=this.openHandle(true);try{let r=new Date().toISOString();n.db.update(l).set({last_message:e,updated_at:r}).where(eq(l.id,t)).run();}catch(r){throw r instanceof u?r:new u("DB_ERROR","Failed to save assistant message",r)}finally{n.close();}}updateThread(t,e){if(!this.dbExists())return;let s=this.openHandle(true);try{let n={updated_at:new Date().toISOString()};e.title!==void 0&&(n.title=e.title,n.title_locked=1),e.titleLocked!==void 0&&(n.title_locked=e.titleLocked?1:0),s.db.update(l).set(n).where(eq(l.id,t)).run();}catch(n){throw n instanceof u?n:new u("DB_ERROR","Failed to update thread",n)}finally{s.close();}}togglePin(t,e){let s=this.openHandle(true);try{let n=e?and(eq(l.id,t),eq(l.workspace_id,e)):eq(l.id,t),r=s.db.select({pinned:l.pinned,metadata:l.metadata}).from(l).where(n).get();if(!r)return null;let i=r.pinned?0:1,a=r.metadata?JSON.parse(r.metadata):{};if(i){let c=e?and(eq(l.pinned,1),eq(l.workspace_id,e)):eq(l.pinned,1),_=s.db.select({metadata:l.metadata}).from(l).where(c).all(),m=0;for(let h of _){let x=h.metadata?JSON.parse(h.metadata):{};typeof x.pinOrder=="number"&&x.pinOrder>m&&(m=x.pinOrder);}a.pinOrder=m+1;}else delete a.pinOrder;return s.db.update(l).set({pinned:i,metadata:Object.keys(a).length>0?JSON.stringify(a):null}).where(n).run(),{pinned:!!i}}catch(n){throw n instanceof u?n:new u("DB_ERROR","Failed to toggle pin",n)}finally{s.close();}}reorderPins(t,e){let s=this.openHandle(true);try{for(let n=0;n<t.length;n++){let r=e?and(eq(l.id,t[n]),eq(l.workspace_id,e)):eq(l.id,t[n]),i=s.db.select({metadata:l.metadata}).from(l).where(r).get();if(!i)continue;let a=i.metadata?JSON.parse(i.metadata):{};a.pinOrder=n+1,s.db.update(l).set({metadata:JSON.stringify(a)}).where(r).run();}}catch(n){throw n instanceof u?n:new u("DB_ERROR","Failed to reorder pins",n)}finally{s.close();}}toggleStar(t,e){let s=this.openHandle(true);try{let n=e?and(eq(l.id,t),eq(l.workspace_id,e)):eq(l.id,t),r=s.db.select({starred:l.starred}).from(l).where(n).get();if(!r)return null;let i=r.starred?0:1;return s.db.update(l).set({starred:i}).where(n).run(),{starred:!!i}}catch(n){throw n instanceof u?n:new u("DB_ERROR","Failed to toggle star",n)}finally{s.close();}}};function _e(d){return d.replace(/<conversation_history[^>]*>[\s\S]*?<\/conversation_history>/g,"").split(`
|
|
111
111
|
`).filter(n=>!(n.startsWith("Loaded ")&&n.includes("layouts from")||n.includes("[dotenv@")||n.includes("[Nest]")&&n.includes("DEBUG")||n.startsWith("Registered custom layout:")||n.startsWith("Updated custom layout:"))).join(`
|
|
112
|
-
`).trim()}function
|
|
113
|
-
`):e&&typeof e=="object"&&e.result!==void 0&&(t=e.result||"");}catch{t=_e(t);}return t}var Z=class{dbPath;constructor(t){this.dbPath=t??join(homedir(),".crewx","crewx.db");}getThreadRepo(){return new Q({dbPath:this.dbPath})}updateThread(t,e){this.getThreadRepo().updateThread(t,{title:e.title});}async ensureThread(t,e,s){let n=this.getThreadRepo(),r=n.findThreadById(t);if(r){if(r.platform!==e)throw new Error(`Thread '${t}' already exists with platform '${r.platform}' \u2014 cannot change to '${e}' (platform is immutable)`);return}n.ensureThread(t,e,s);}async fetchHistory(t,e){let s=e?.limit??100,n=this.getThreadRepo(),r=n.findThreadById(t),i=n.findTopLevelTasks(t),a=new Set(["done","completed","success"]);i=i.filter(
|
|
112
|
+
`).trim()}function me(d){if(!d)return "";let t=d;try{let e=JSON.parse(t);Array.isArray(e)?t=e.filter(s=>s?.type==="text"&&s?.text).map(s=>s.text).join(`
|
|
113
|
+
`):e&&typeof e=="object"&&e.result!==void 0&&(t=e.result||"");}catch{t=_e(t);}return t}var Z=class{dbPath;constructor(t){this.dbPath=t??join(homedir(),".crewx","crewx.db");}getThreadRepo(){return new Q({dbPath:this.dbPath})}updateThread(t,e){this.getThreadRepo().updateThread(t,{title:e.title});}async ensureThread(t,e,s){let n=this.getThreadRepo(),r=n.findThreadById(t);if(r){if(r.platform!==e)throw new Error(`Thread '${t}' already exists with platform '${r.platform}' \u2014 cannot change to '${e}' (platform is immutable)`);return}n.ensureThread(t,e,s);}async fetchHistory(t,e){let s=e?.limit??100,n=this.getThreadRepo(),r=n.findThreadById(t),i=n.findTopLevelTasks(t),a=new Set(["done","completed","success"]);i=i.filter(m=>(!m.status||a.has(m.status))&&(!e?.currentTraceId||m.trace_id!==e.currentTraceId)),i=i.slice(0,s);let c=r?.platform??"cli",_=this.rowsToMessages(i);return {threadId:t,platform:c,messages:_,metadata:{title:r?.title??void 0,firstMessage:r?.first_message??void 0,lastMessage:r?.last_message??void 0,messageCount:r?.message_count??0,updatedAt:r?.updated_at?new Date(r.updated_at).getTime():void 0}}}async saveUserMessage(t,e,s,n){this.getThreadRepo().saveUserMessage(t,e);}async saveAssistantMessage(t,e,s,n){this.getThreadRepo().saveAssistantMessage(t,e);}close(){}rowsToMessages(t){let e=[];for(let s of t){s.prompt&&e.push({id:`${s.id}-user`,text:s.prompt,isAssistant:false,timestamp:new Date(s.started_at).getTime()});let n=me(s.result);n&&e.push({id:`${s.id}-assistant`,text:n,isAssistant:true,timestamp:new Date(s.started_at).getTime()});}return e}};var dt=class extends A{name="conversation";_provider;unsubStart=null;unsubEnd=null;constructor(t){super(),this._provider=new Z(t?.dbPath);}get conversationProvider(){return this._provider}attach(t){this.unsubStart=t.on("task:start",async e=>{if(!e.threadId)return;let s=e.platform??"cli";try{await this._provider.ensureThread(e.threadId,s,e.workspaceId),await this._provider.saveUserMessage(e.threadId,e.message??"");}catch{}}),this.unsubEnd=t.on("task:end",async e=>{if(!e.result)return;let s=e.metadata?.threadId;if(!s)return;let n=e.agentRef?.replace(/^@/,"")??"";try{await this._provider.saveAssistantMessage(s,e.result,n);}catch{}});}detach(t){this.unsubStart?.(),this.unsubStart=null,this.unsubEnd?.(),this.unsubEnd=null,this._provider.close?.();}};export{dt as ConversationPlugin,tt as FileLoggerPlugin,ot as SqliteTracingPlugin};
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import*as K from'path';import K__default,{join,dirname,basename}from'path';import {fileURLToPath}from'url';import vt,{existsSync,mkdirSync}from'fs';import
|
|
1
|
+
import*as K from'path';import K__default,{join,dirname,basename}from'path';import {fileURLToPath}from'url';import vt,{existsSync,mkdirSync}from'fs';import Qt from'os';import {sql,and,eq,ne as ne$1,isNull,desc,isNotNull,or,like,asc,inArray,gte,lt}from'drizzle-orm';import {sqliteTable,text,integer,real,index,unique,getTableConfig}from'drizzle-orm/sqlite-core';import {randomUUID,createHash}from'crypto';var Q=(d=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(d,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):d)(function(d){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+d+'" is not supported')});var Gt=()=>fileURLToPath(import.meta.url),Kt=()=>K__default.dirname(Gt()),b=Kt();var E=class{resolveDbPath(){return process.env.CREWX_DB?process.env.CREWX_DB:process.env.CREWX_TRACES_DB?process.env.CREWX_TRACES_DB:join(Qt.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 a=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 w(d){let t=Q("better-sqlite3"),{drizzle:e}=Q("drizzle-orm/better-sqlite3"),n=new t(d);return n.exec("PRAGMA journal_mode = WAL"),n.exec("PRAGMA busy_timeout = 5000"),n.exec("PRAGMA foreign_keys = ON"),{db:e(n),runRaw:(r,s=[])=>n.prepare(r).run(...s),close:()=>n.close()}}var Dt=new Set,te={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 ee(d,t){return (d.get(`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='${t}'`)?.cnt??0)>0}function ne(d,t){if(t>0||!ee(d,"tasks"))return;let e=d.all("PRAGMA table_info(tasks)"),n=new Set(e.map(r=>r.name));for(let[r,s]of Object.entries(te))n.has(r)||d.run(`ALTER TABLE tasks ADD COLUMN ${r} ${s}`);}function W(d){let{migrate:t}=Q("drizzle-orm/better-sqlite3/migrator"),{sql:e}=Q("drizzle-orm"),n=[K__default.join(b,"../migrations"),K__default.join(b,"migrations"),K__default.join(b,"../../../../drizzle/migrations"),K__default.join(process.cwd(),"drizzle/migrations")],r=n.find(f=>existsSync(K__default.join(f,"meta/_journal.json")));if(!r)throw new Error(`migrations folder not found. Searched:
|
|
2
2
|
${n.join(`
|
|
3
|
-
`)}`);let s=d.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),i=0;s?.cnt&&(i=d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),ne(d,i),t(d,{migrationsFolder:r});let
|
|
3
|
+
`)}`);let s=d.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),i=0;s?.cnt&&(i=d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),ne(d,i),t(d,{migrationsFolder:r});let u=(d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0)-i;if(u>0){let f=s?.cnt?"Database migrated":"Database initialized";console.log(`[crewx] ${f} (${u} migration${u>1?"s":""} applied).`);}}function O(d,t){Dt.has(t)||(W(d),Dt.add(t));}var p=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 o=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)},d=>({idx_tasks_agent_id:index("idx_tasks_agent_id").on(d.agent_id),idx_tasks_status:index("idx_tasks_status").on(d.status),idx_tasks_started_at:index("idx_tasks_started_at").on(d.started_at),idx_tasks_trace_id:index("idx_tasks_trace_id").on(d.trace_id),idx_tasks_parent_task_id:index("idx_tasks_parent_task_id").on(d.parent_task_id),idx_tasks_crewx_version:index("idx_tasks_crewx_version").on(d.crewx_version),idx_tasks_pid:index("idx_tasks_pid").on(d.pid),idx_tasks_thread_id:index("idx_tasks_thread_id").on(d.thread_id),idx_tasks_workspace_id:index("idx_tasks_workspace_id").on(d.workspace_id),idx_tasks_workspace_ref:index("idx_tasks_workspace_ref").on(d.workspace_ref),idx_tasks_project_id:index("idx_tasks_project_id").on(d.project_id),idx_tasks_ws_started:index("idx_tasks_ws_started").on(d.workspace_id,d.started_at)}));var c=sqliteTable("threads",{id:text("id").primaryKey(),workspace_id:text("workspace_id").references(()=>p.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)},d=>({idx_threads_updated_at:index("idx_threads_updated_at").on(d.updated_at),idx_threads_workspace_id:index("idx_threads_workspace_id").on(d.workspace_id)}));var F=sqliteTable("spans",{id:text("id").primaryKey(),task_id:text("task_id").references(()=>o.id,{onDelete:"set null"}),parent_span_id:text("parent_span_id").references(()=>F.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")},d=>({idx_spans_task_id:index("idx_spans_task_id").on(d.task_id),idx_spans_parent_span_id:index("idx_spans_parent_span_id").on(d.parent_span_id)}));var v=sqliteTable("tool_calls",{id:text("id").primaryKey(),task_id:text("task_id").references(()=>o.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()},d=>({idx_tool_calls_task_id:index("idx_tool_calls_task_id").on(d.task_id),idx_tool_calls_tool_name:index("idx_tool_calls_tool_name").on(d.tool_name),idx_tool_calls_timestamp:index("idx_tool_calls_timestamp").on(d.timestamp)}));var A=sqliteTable("thread_boxes",{id:text("id").primaryKey(),thread_id:text("thread_id").notNull().references(()=>c.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()},d=>({idx_thread_boxes_thread_id:index("idx_thread_boxes_thread_id").on(d.thread_id),idx_thread_boxes_seq:index("idx_thread_boxes_seq").on(d.thread_id,d.seq),uniq_thread_boxes_thread_seq:unique().on(d.thread_id,d.seq)}));var Y=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")},d=>({idx_request_logs_timestamp:index("idx_request_logs_timestamp").on(d.timestamp),idx_request_logs_path:index("idx_request_logs_path").on(d.path),idx_request_logs_status_code:index("idx_request_logs_status_code").on(d.status_code),idx_request_logs_partition_key:index("idx_request_logs_partition_key").on(d.partition_key)}));function ut(d){let t=K.resolve(d);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 Nt(d){let t=ut(d);return createHash("sha256").update(t).digest("hex")}function nt(d){return d.toLowerCase().replace(/[^a-z0-9-]/g,"-").replace(/-{2,}/g,"-").replace(/^-+|-+$/g,"")}var ht=class extends E{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 r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true});}else if(!existsSync(e))throw new a("NOT_FOUND","Database not found");let n=w(e);if(t)try{W(n.db);}catch(r){throw n.close(),r}return n}resolveSlug(t,e,n){let r=nt(basename(n)),i=`${nt(basename(dirname(n)))}-${r}`,l=[r,i];try{let u=f=>t.select({id:p.id}).from(p).where(and(eq(p.slug,f),ne$1(p.id,e))).limit(1).all().length>0;for(let f of l)if(!u(f))return f;for(let f=2;f<1e3;f+=1){let _=`${i}-${f}`;if(!u(_))return _}}catch{}return r}normalizeLegacySlugs(){if(!this.dbExists())return {updated:0,checked:0};let t=this.openHandle(true);try{let e=t.db.select({id:p.id,slug:p.slug}).from(p).all(),n=0;for(let r of e)if(r.slug.includes("/")){let s=nt(r.slug.replace(/\//g,"-"));t.db.update(p).set({slug:s,updated_at:new Date().toISOString()}).where(eq(p.id,r.id)).run(),n+=1;}return {updated:n,checked:e.length}}catch(e){throw new a("DB_ERROR","Failed to normalize legacy slugs",e)}finally{t.close();}}ensureRow(t,e){let{id:n,slug:r,name:s,workspacePath:i}=e,l=new Date().toISOString();t.insert(p).values({id:n,slug:r,name:s,workspace_path:i,is_active:1,created_at:l,updated_at:l}).onConflictDoNothing().run(),t.update(p).set({workspace_path:i,updated_at:l}).where(and(eq(p.id,n),isNull(p.workspace_path))).run();}registerWorkspace(t){let e=ut(t),n=this.openHandle(true);try{let r=Nt(e),s=basename(e),i=this.resolveSlug(n.db,r,e);return this.ensureRow(n.db,{id:r,slug:i,name:s,workspacePath:e}),{id:r,slug:i}}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to register workspace",r)}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(p.is_active,t.isActive?1:0):void 0,r=e.db.select({count:sql`count(*)`}).from(p).where(n).get();return {rows:e.db.select().from(p).where(n).orderBy(desc(p.updated_at)).limit(t.limit).offset(t.offset).all(),total:r?.count??0}}catch(n){throw new a("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(p).where(eq(p.id,t)).limit(1).get()??void 0}catch(n){throw new a("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(r=>r.agent_id)}catch(n){throw new a("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 r=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:r?.count??0}}catch(r){throw new a("DB_ERROR","Failed to find threads by project",r)}finally{n.close();}}findBySlug(t){if(!this.dbExists())return;let e=this.openHandle(false);try{return e.db.select().from(p).where(eq(p.slug,t)).limit(1).get()??void 0}catch(n){throw new a("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 r=e?and(eq(p.slug,t),ne$1(p.id,e)):eq(p.slug,t);return !!n.db.select({id:p.id}).from(p).where(r).limit(1).get()}catch(r){throw new a("DB_ERROR","Failed to check slug",r)}finally{n.close();}}insert(t,e,n,r){let s=this.openHandle(true);try{let i=new Date().toISOString();s.db.insert(p).values({id:t,slug:e,name:n,workspace_path:r,is_active:1,created_at:i,updated_at:i}).run();let l=s.db.select().from(p).where(eq(p.id,t)).limit(1).get();if(!l)throw new a("DB_ERROR","Insert did not return a row");return l}catch(i){throw i instanceof a?i:new a("DB_ERROR","Failed to insert workspace",i)}finally{s.close();}}update(t,e,n){let r=this.openHandle(true);try{r.runRaw(`UPDATE workspaces SET ${e.join(", ")} WHERE id = ?`,n);let s=r.db.select().from(p).where(eq(p.id,t)).limit(1).get();if(!s)throw new a("NOT_FOUND",`Workspace ${t} not found`);return s}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to update workspace",s)}finally{r.close();}}cleanupOrphanWorkspaces(){if(!this.dbExists())return {softDeleted:0,checked:0};let t=this.openHandle(true);try{let e=t.db.select({id:p.id,workspace_path:p.workspace_path}).from(p).where(and(eq(p.is_active,1),isNotNull(p.workspace_path))).all(),n=0;for(let r of e){let s=r.workspace_path;existsSync(join(s,"crewx.yaml"))||existsSync(join(s,"crewx.yml"))||(t.db.update(p).set({is_active:0,updated_at:new Date().toISOString()}).where(eq(p.id,r.id)).run(),n+=1);}return {softDeleted:n,checked:e.length}}catch(e){throw new a("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(p).where(eq(p.id,t)).run();}catch(n){throw n instanceof a?n:new a("DB_ERROR","Failed to delete workspace",n)}finally{e.close();}}};var Lt=[p,o,
|
|
8
|
+
LIMIT ${e.limit} OFFSET ${e.offset}`),total:r?.count??0}}catch(r){throw new a("DB_ERROR","Failed to find threads by project",r)}finally{n.close();}}findBySlug(t){if(!this.dbExists())return;let e=this.openHandle(false);try{return e.db.select().from(p).where(eq(p.slug,t)).limit(1).get()??void 0}catch(n){throw new a("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 r=e?and(eq(p.slug,t),ne$1(p.id,e)):eq(p.slug,t);return !!n.db.select({id:p.id}).from(p).where(r).limit(1).get()}catch(r){throw new a("DB_ERROR","Failed to check slug",r)}finally{n.close();}}insert(t,e,n,r){let s=this.openHandle(true);try{let i=new Date().toISOString();s.db.insert(p).values({id:t,slug:e,name:n,workspace_path:r,is_active:1,created_at:i,updated_at:i}).run();let l=s.db.select().from(p).where(eq(p.id,t)).limit(1).get();if(!l)throw new a("DB_ERROR","Insert did not return a row");return l}catch(i){throw i instanceof a?i:new a("DB_ERROR","Failed to insert workspace",i)}finally{s.close();}}update(t,e,n){let r=this.openHandle(true);try{r.runRaw(`UPDATE workspaces SET ${e.join(", ")} WHERE id = ?`,n);let s=r.db.select().from(p).where(eq(p.id,t)).limit(1).get();if(!s)throw new a("NOT_FOUND",`Workspace ${t} not found`);return s}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to update workspace",s)}finally{r.close();}}cleanupOrphanWorkspaces(){if(!this.dbExists())return {softDeleted:0,checked:0};let t=this.openHandle(true);try{let e=t.db.select({id:p.id,workspace_path:p.workspace_path}).from(p).where(and(eq(p.is_active,1),isNotNull(p.workspace_path))).all(),n=0;for(let r of e){let s=r.workspace_path;existsSync(join(s,"crewx.yaml"))||existsSync(join(s,"crewx.yml"))||(t.db.update(p).set({is_active:0,updated_at:new Date().toISOString()}).where(eq(p.id,r.id)).run(),n+=1);}return {softDeleted:n,checked:e.length}}catch(e){throw new a("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(p).where(eq(p.id,t)).run();}catch(n){throw n instanceof a?n:new a("DB_ERROR","Failed to delete workspace",n)}finally{e.close();}}};var Lt=[p,o,c,F,v,A,Y];function xe(){return K__default.join(Qt.homedir(),".crewx","crewx.db")}function De(d){if(!vt.existsSync(d))return null;let t=Math.floor(Date.now()/1e3),e=`${d}.bak-${t}`;return vt.copyFileSync(d,e),e}function qt(d){let t=d.all("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'");return new Set(t.map(e=>e.name))}function Se(d){return d==null||typeof d=="object"&&"queryChunks"in d?null:typeof d=="number"?String(d):typeof d=="boolean"?d?"1":"0":typeof d=="string"?`'${d.replace(/'/g,"''")}'`:String(d)}function Te(d){let t=d.getSQLType(),e=`"${d.name}" ${t}`,n=Se(d.default);return n!==null&&(e+=` DEFAULT ${n}`),d.notNull&&(e+=" NOT NULL"),e}function Oe(d,t){let e=t?.dbPath??xe(),n=t?.force??false,r=t?.dryRun??false,s=r?null:De(e);if(n&&!r)try{d.run("DELETE FROM __drizzle_migrations");}catch{}let i=qt(d);r||W(d);let l,u;r?(l=Lt.map(R=>getTableConfig(R).name).filter(R=>!i.has(R)),u=i):(u=qt(d),l=[...u].filter(g=>!i.has(g)));let f=[],_=[];for(let g of Lt){let R=getTableConfig(g),T=R.name;if(!u.has(T))continue;let L=d.all(`PRAGMA table_info("${T}")`),dt=new Set(L.map(B=>B.name)),lt=new Set(R.columns.map(B=>B.name));for(let B of R.columns)if(!dt.has(B.name)){if(!r){let Wt=Te(B);d.run(`ALTER TABLE "${T}" ADD COLUMN ${Wt}`);}f.push({table:T,column:B.name});}for(let B of L)lt.has(B.name)||_.push(`${T}.${B.name} exists in DB but not in schema (untouched)`);}return {created:l,altered:f,warnings:_,backupPath:s}}var gt=class extends E{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 r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true});}else if(!existsSync(e))throw new a("NOT_FOUND","Database not found");let n=w(e);if(t)try{O(n.db,e);}catch(r){throw n.close(),r}return n}startTask(t){let e=this.openHandle(true);try{e.db.insert(o).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 a?n:new a("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=?`,[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 a?n:new a("DB_ERROR","Failed to finish task",n)}finally{e.close();}}appendLog(t,e){let n=this.openHandle(true);try{n.db.transaction(r=>{let s=r.select({logs:o.logs}).from(o).where(eq(o.id,t)).limit(1).get(),i=s?.logs?JSON.parse(s.logs):[];i.push(e),r.update(o).set({logs:JSON.stringify(i)}).where(eq(o.id,t)).run();},{behavior:"immediate"});}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to append log",r)}finally{n.close();}}getRunningTasks(){if(!this.dbExists())return [];let t=this.openHandle(false);try{return t.db.select().from(o).where(eq(o.status,"running")).orderBy(desc(o.started_at)).all()}catch(e){throw new a("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(o).orderBy(desc(o.started_at)).limit(100).all()}catch(e){throw new a("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(o).where(eq(o.id,t)).limit(1).get()??void 0}catch(n){throw new a("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:o.id,status:o.status,pid:o.pid}).from(o).where(eq(o.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(o).set({status:"failed",error:"Killed by user",completed_at:new Date().toISOString()}).where(and(eq(o.id,t),eq(o.status,"running"))).run(),{killed:!0,pid:n.pid??void 0}}catch(n){throw n instanceof a?n:new a("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:o.id,pid:o.pid}).from(o).where(eq(o.status,"running")).all(),n=0;for(let r of e){let s=!1;if(r.pid)try{process.kill(r.pid,0),s=!0;}catch{}s||(t.db.update(o).set({status:"failed",error:"Reaped: process not found (orphaned task)",completed_at:new Date().toISOString()}).where(and(eq(o.id,r.id),eq(o.status,"running"))).run(),n++);}return n}finally{t.close();}}findTaskStatus(t,e){let n=this.resolveDbPaths();for(let r of n){if(!existsSync(r))continue;let s=
|
|
10
|
+
model=COALESCE(?, model) WHERE id=?`,[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 a?n:new a("DB_ERROR","Failed to finish task",n)}finally{e.close();}}appendLog(t,e){let n=this.openHandle(true);try{n.db.transaction(r=>{let s=r.select({logs:o.logs}).from(o).where(eq(o.id,t)).limit(1).get(),i=s?.logs?JSON.parse(s.logs):[];i.push(e),r.update(o).set({logs:JSON.stringify(i)}).where(eq(o.id,t)).run();},{behavior:"immediate"});}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to append log",r)}finally{n.close();}}getRunningTasks(){if(!this.dbExists())return [];let t=this.openHandle(false);try{return t.db.select().from(o).where(eq(o.status,"running")).orderBy(desc(o.started_at)).all()}catch(e){throw new a("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(o).orderBy(desc(o.started_at)).limit(100).all()}catch(e){throw new a("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(o).where(eq(o.id,t)).limit(1).get()??void 0}catch(n){throw new a("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:o.id,status:o.status,pid:o.pid}).from(o).where(eq(o.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(o).set({status:"failed",error:"Killed by user",completed_at:new Date().toISOString()}).where(and(eq(o.id,t),eq(o.status,"running"))).run(),{killed:!0,pid:n.pid??void 0}}catch(n){throw n instanceof a?n:new a("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:o.id,pid:o.pid}).from(o).where(eq(o.status,"running")).all(),n=0;for(let r of e){let s=!1;if(r.pid)try{process.kill(r.pid,0),s=!0;}catch{}s||(t.db.update(o).set({status:"failed",error:"Reaped: process not found (orphaned task)",completed_at:new Date().toISOString()}).where(and(eq(o.id,r.id),eq(o.status,"running"))).run(),n++);}return n}finally{t.close();}}findTaskStatus(t,e){let n=this.resolveDbPaths();for(let r of n){if(!existsSync(r))continue;let s=w(r);try{let i=e?eq(o.workspace_id,e):void 0,l=i?and(eq(o.id,t),i):eq(o.id,t),u=s.db.select().from(o).where(l).limit(1).get()??void 0;if(!u){let f=or(eq(o.thread_id,t),and(isNull(o.thread_id),like(o.command,`%--thread=${t}%`))),_=i?and(f,i):f;u=s.db.select().from(o).where(_).orderBy(desc(o.started_at)).limit(1).get()??void 0;}if(u)return u}catch(i){throw new a("DB_ERROR","Failed to find task status",i)}finally{s.close();}}}findChildTasks(t,e){let n=this.resolveDbPaths(),r=new Set,s=[];for(let i of n){if(!existsSync(i))continue;let l=w(i);try{let u=e?and(eq(o.parent_task_id,t),eq(o.workspace_id,e)):eq(o.parent_task_id,t),f=l.db.select().from(o).where(u).orderBy(asc(o.started_at)).all();for(let _ of f)r.has(_.id)||(r.add(_.id),s.push(_));}catch(u){throw new a("DB_ERROR","Failed to find child tasks",u)}finally{l.close();}}return s}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,7 +28,7 @@ ${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 a("DB_ERROR","Failed to get workspace usage summary",n)}finally{e.close();}}getThreadTokenUsage(t,e){let n=this.resolveDbPaths(),r=new Set,s=0,i=0,l=0;for(let
|
|
31
|
+
`)}catch(n){throw new a("DB_ERROR","Failed to get workspace usage summary",n)}finally{e.close();}}getThreadTokenUsage(t,e){let n=this.resolveDbPaths(),r=new Set,s=0,i=0,l=0;for(let u of n){if(!existsSync(u))continue;let f=w(u);try{let _=or(eq(o.thread_id,t),and(isNull(o.thread_id),like(o.command,`%--thread=${t}%`))),g=e?and(_,eq(o.workspace_id,e)):_,R=f.db.select({id:o.id,input_tokens:o.input_tokens,output_tokens:o.output_tokens,cost_usd:o.cost_usd}).from(o).where(g).all();for(let T of R)r.has(T.id)||(r.add(T.id),s+=T.input_tokens??0,i+=T.output_tokens??0,l+=T.cost_usd??0);}catch(_){throw new a("DB_ERROR","Failed to get thread token usage",_)}finally{f.close();}}return {inputTokens:s,outputTokens:i,costUsd:l}}findTasksByThread(t,e){let n=this.resolveDbPaths(),r=new Set,s=[];for(let i of n){if(!existsSync(i))continue;let l=w(i);try{let u=or(eq(o.thread_id,t),and(isNull(o.thread_id),like(o.command,`%--thread=${t}%`))),f=e?and(u,eq(o.workspace_id,e)):u,_=l.db.select().from(o).where(f).orderBy(asc(o.started_at)).all();for(let g of _)r.has(g.id)||(r.add(g.id),s.push(g));}catch(u){throw new a("DB_ERROR","Failed to find tasks by thread",u)}finally{l.close();}}return s}findAllTasks(t){if(!this.dbExists())return {rows:[],total:0};let e=this.openHandle(false);try{let n=[];t.workspaceId&&n.push(eq(o.workspace_id,t.workspaceId));let r=t.agents&&t.agents.length>0?t.agents:t.agentId?[t.agentId]:null;r&&n.push(inArray(o.agent_id,r));let s=t.statuses&&t.statuses.length>0?t.statuses:t.status?[t.status]:null;s&&n.push(inArray(o.status,s));let i=t.q??t.search;i&&n.push(like(o.prompt,`%${i}%`)),t.from&&n.push(gte(o.started_at,t.from)),t.to&&n.push(lt(o.started_at,t.to));let l=n.length>0?and(...n):void 0,u=e.db.select({count:sql`count(*)`}).from(o).where(l).get(),f=(t.sortDir??"DESC")==="ASC"?asc(o.started_at):desc(o.started_at);return {rows:e.db.select().from(o).where(l).orderBy(f).limit(t.limit).offset(t.offset).all(),total:u?.count??0}}catch(n){throw new a("DB_ERROR","Failed to find all tasks",n)}finally{e.close();}}getAgentUsage(t,e,n){if(!this.dbExists())return [];let r=this.openHandle(false);try{return r.db.all(n?sql`
|
|
32
32
|
SELECT
|
|
33
33
|
t.agent_id,
|
|
34
34
|
t.workspace_id,
|
|
@@ -88,13 +88,13 @@ ${n.join(`
|
|
|
88
88
|
AND t.started_at < ${e}
|
|
89
89
|
GROUP BY date(t.started_at), t.agent_id
|
|
90
90
|
ORDER BY date(t.started_at) ASC
|
|
91
|
-
`).map(i=>({date:i.date,agentId:i.agent_id,inputTokens:i.input_tokens,outputTokens:i.output_tokens,cachedInputTokens:i.cached_input_tokens,costUsd:i.cost_usd}))}catch(s){throw new a("DB_ERROR","Failed to get agent usage trend",s)}finally{r.close();}}findTaskForStop(t,e){if(!this.dbExists())return;let n=this.openHandle(false);try{return n.db.select().from(o).where(and(eq(o.id,t),eq(o.workspace_id,e))).limit(1).get()??void 0}catch(r){throw new a("DB_ERROR","Failed to find task for stop",r)}finally{n.close();}}markTaskFailed(t,e,n){if(!this.dbExists())return;let r=this.openHandle(true);try{let s=new Date().toISOString(),i=n?and(eq(o.id,t),eq(o.status,"running"),eq(o.workspace_id,n)):and(eq(o.id,t),eq(o.status,"running"));r.db.update(o).set({status:"failed",error:e,completed_at:s}).where(i).run();}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to mark task failed",s)}finally{r.close();}}findTasksByPromptHint(t,e){let n=this.resolveDbPaths(),r=new Set,s=[];for(let i of n){if(!existsSync(i))continue;let l=
|
|
91
|
+
`).map(i=>({date:i.date,agentId:i.agent_id,inputTokens:i.input_tokens,outputTokens:i.output_tokens,cachedInputTokens:i.cached_input_tokens,costUsd:i.cost_usd}))}catch(s){throw new a("DB_ERROR","Failed to get agent usage trend",s)}finally{r.close();}}findTaskForStop(t,e){if(!this.dbExists())return;let n=this.openHandle(false);try{return n.db.select().from(o).where(and(eq(o.id,t),eq(o.workspace_id,e))).limit(1).get()??void 0}catch(r){throw new a("DB_ERROR","Failed to find task for stop",r)}finally{n.close();}}markTaskFailed(t,e,n){if(!this.dbExists())return;let r=this.openHandle(true);try{let s=new Date().toISOString(),i=n?and(eq(o.id,t),eq(o.status,"running"),eq(o.workspace_id,n)):and(eq(o.id,t),eq(o.status,"running"));r.db.update(o).set({status:"failed",error:e,completed_at:s}).where(i).run();}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to mark task failed",s)}finally{r.close();}}findTasksByPromptHint(t,e){let n=this.resolveDbPaths(),r=new Set,s=[];for(let i of n){if(!existsSync(i))continue;let l=w(i);try{let u=e?and(like(o.prompt,`%${t}%`),eq(o.workspace_id,e)):like(o.prompt,`%${t}%`),f=l.db.select().from(o).where(u).orderBy(asc(o.started_at)).all();for(let _ of f)r.has(_.id)||(r.add(_.id),s.push(_));}catch(u){throw new a("DB_ERROR","Failed to find tasks by prompt hint",u)}finally{l.close();}}return s}};var bt=class extends E{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 r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true});}else if(!existsSync(e))throw new a("NOT_FOUND","Database not found");let n=w(e);if(t)try{O(n.db,e);}catch(r){throw n.close(),r}return n}validateWorkspaceId(t,e){return t.db.select({id:p.id}).from(p).where(eq(p.id,e)).limit(1).get()?e:null}findAllThreads(t){let e=this.resolveDbPaths(),n=new Set,r=[];for(let s of e){if(!existsSync(s))continue;let i=w(s);try{let l=t?eq(c.workspace_id,t):void 0,u=i.db.select().from(c).where(l).orderBy(desc(c.updated_at)).all();for(let f of u)n.has(f.id)||(n.add(f.id),r.push(f));}catch(l){throw new a("DB_ERROR","Failed to find all threads",l)}finally{i.close();}}return r}findThreadById(t,e){let n=this.resolveDbPaths();for(let r of n){if(!existsSync(r))continue;let s=w(r);try{let i=eq(c.id,t),l=e?and(i,eq(c.workspace_id,e)):i,u=s.db.select().from(c).where(l).limit(1).get()??void 0;if(u)return u}catch(i){throw new a("DB_ERROR","Failed to find thread by id",i)}finally{s.close();}}}threadExists(t,e){let n=this.resolveDbPaths();for(let r of n){if(!existsSync(r))continue;let s=w(r);try{let i=eq(c.id,t),l=e?and(i,eq(c.workspace_id,e)):i;if(s.db.select({id:c.id}).from(c).where(l).limit(1).get())return !0}catch(i){throw new a("DB_ERROR","Failed to check thread existence",i)}finally{s.close();}}return false}aggregateTaskStats(t,e){let n=this.resolveDbPaths(),r=0,s=0,i=0,l=0,u=0,f=new Set;for(let _ of n){if(!existsSync(_))continue;let g=w(_);try{let R=and(eq(o.thread_id,t),or(isNull(o.parent_task_id),eq(o.parent_task_id,""))),T=e?and(R,eq(o.workspace_id,e)):R,L=g.db.select({cnt:sql`count(*)`,total_input:sql`COALESCE(SUM(input_tokens), 0)`,total_output:sql`COALESCE(SUM(output_tokens), 0)`,total_cached:sql`COALESCE(SUM(cached_input_tokens), 0)`,total_cost:sql`COALESCE(SUM(cost_usd), 0)`}).from(o).where(T).get();L&&(r+=L.cnt,s+=L.total_input,i+=L.total_output,l+=L.total_cached,u+=L.total_cost);let dt=g.db.all(sql`
|
|
92
92
|
SELECT DISTINCT agent_id FROM tasks
|
|
93
93
|
WHERE thread_id = ${t}
|
|
94
94
|
AND agent_id IS NOT NULL AND agent_id != ''
|
|
95
95
|
AND (parent_task_id IS NULL OR parent_task_id = '')
|
|
96
96
|
${e?sql`AND workspace_id = ${e}`:sql``}
|
|
97
|
-
`);for(let lt of dt)
|
|
97
|
+
`);for(let lt of dt)f.add(lt.agent_id);}catch(R){throw new a("DB_ERROR","Failed to aggregate task stats",R)}finally{g.close();}}return {taskCount:r,inputTokens:s,outputTokens:i,cachedInputTokens:l,costUsd:u,agentIds:Array.from(f)}}findTopLevelTasks(t,e){let n=this.resolveDbPaths(),r=new Set,s=[];for(let i of n){if(!existsSync(i))continue;let l=w(i);try{let u=and(eq(o.thread_id,t),or(isNull(o.parent_task_id),eq(o.parent_task_id,""))),f=e?and(u,eq(o.workspace_id,e)):u,_=l.db.select().from(o).where(f).orderBy(asc(o.started_at)).all();for(let g of _)r.has(g.id)||(r.add(g.id),s.push(g));}catch(u){throw new a("DB_ERROR","Failed to find top-level tasks",u)}finally{l.close();}}return s}findAllTasks(t,e){let n=this.resolveDbPaths(),r=new Set,s=[];for(let i of n){if(!existsSync(i))continue;let l=w(i);try{let u=eq(o.thread_id,t),f=e?and(u,eq(o.workspace_id,e)):u,_=l.db.select().from(o).where(f).orderBy(asc(o.started_at)).all();for(let g of _)r.has(g.id)||(r.add(g.id),s.push(g));}catch(u){throw new a("DB_ERROR","Failed to find all tasks for thread",u)}finally{l.close();}}return s}findTaskById(t,e,n){let r=this.resolveDbPaths();for(let s of r){if(!existsSync(s))continue;let i=w(s);try{let l=and(eq(o.id,e),eq(o.thread_id,t)),u=n?and(l,eq(o.workspace_id,n)):l,f=i.db.select().from(o).where(u).limit(1).get();if(!f)continue;let _=i.db.select().from(o).where(eq(o.parent_task_id,f.id)).orderBy(asc(o.started_at)).all();return {task:f,children:_}}catch(l){throw new a("DB_ERROR","Failed to find task by id",l)}finally{i.close();}}}batchFetchTasks(t,e){let n=new Map;if(t.length===0)return n;let r=this.resolveDbPaths();for(let s of r){if(!existsSync(s))continue;let i=w(s);try{let l=and(inArray(o.thread_id,t),or(isNull(o.parent_task_id),eq(o.parent_task_id,""))),u=e?and(l,eq(o.workspace_id,e)):l,f=i.db.select().from(o).where(u).orderBy(asc(o.started_at)).all();for(let _ of f){let g=_.thread_id;n.has(g)||n.set(g,[]),n.get(g).push(_);}}catch(l){throw new a("DB_ERROR","Failed to batch fetch tasks",l)}finally{i.close();}}return n}updateThreadTitle(t,e,n){if(!this.dbExists())return;let r=this.openHandle(true);try{let s=eq(c.id,t),i=n?and(s,eq(c.workspace_id,n)):s;if(!r.db.select({id:c.id}).from(c).where(i).limit(1).get())return;r.db.update(c).set({title:e,title_locked:1,updated_at:new Date().toISOString()}).where(eq(c.id,t)).run();}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to update thread title",s)}finally{r.close();}}upsertThread(t,e){let n=this.openHandle(true);try{let r=e.workspaceId?this.validateWorkspaceId(n,e.workspaceId):null,s=new Date().toISOString();if(n.db.select({id:c.id,message_count:c.message_count}).from(c).where(eq(c.id,t)).limit(1).get()){let l={updated_at:s};e.title!==void 0&&(l.title=e.title),e.titleLocked!==void 0&&(l.title_locked=e.titleLocked?1:0),n.db.update(c).set(l).where(eq(c.id,t)).run();}else n.db.insert(c).values({id:t,platform:e.platform,workspace_id:r,title:e.title??null,title_locked:e.titleLocked?1:0,message_count:0,created_at:s,updated_at:s}).run();}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to upsert thread",r)}finally{n.close();}}ensureThread(t,e,n){let r=this.openHandle(true);try{let s=n?this.validateWorkspaceId(r,n):null,i=r.db.select({id:c.id,platform:c.platform,workspace_id:c.workspace_id}).from(c).where(eq(c.id,t)).limit(1).get();if(i){s&&!i.workspace_id&&r.db.update(c).set({workspace_id:s}).where(eq(c.id,t)).run();return}let l=new Date().toISOString();r.db.insert(c).values({id:t,platform:e,workspace_id:s,message_count:0,created_at:l,updated_at:l}).run();}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to ensure thread",s)}finally{r.close();}}saveUserMessage(t,e,n){if(!this.dbExists())return;let r=this.openHandle(true);try{let s=new Date().toISOString();r.db.run(sql`
|
|
98
98
|
UPDATE threads
|
|
99
99
|
SET first_message = COALESCE(first_message, ${e}),
|
|
100
100
|
title = CASE WHEN title_locked = 0 AND title IS NULL THEN substr(${e}, 1, 60) ELSE title END,
|
|
@@ -102,4 +102,4 @@ ${n.join(`
|
|
|
102
102
|
message_count = message_count + 1,
|
|
103
103
|
updated_at = ${s}
|
|
104
104
|
WHERE id = ${t}
|
|
105
|
-
`);}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to save user message",s)}finally{r.close();}}saveAssistantMessage(t,e,n){if(!this.dbExists())return;let r=this.openHandle(true);try{let s=new Date().toISOString();r.db.update(
|
|
105
|
+
`);}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to save user message",s)}finally{r.close();}}saveAssistantMessage(t,e,n){if(!this.dbExists())return;let r=this.openHandle(true);try{let s=new Date().toISOString();r.db.update(c).set({last_message:e,updated_at:s}).where(eq(c.id,t)).run();}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to save assistant message",s)}finally{r.close();}}updateThread(t,e){if(!this.dbExists())return;let n=this.openHandle(true);try{let r={updated_at:new Date().toISOString()};e.title!==void 0&&(r.title=e.title,r.title_locked=1),e.titleLocked!==void 0&&(r.title_locked=e.titleLocked?1:0),n.db.update(c).set(r).where(eq(c.id,t)).run();}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to update thread",r)}finally{n.close();}}togglePin(t,e){let n=this.openHandle(true);try{let r=e?and(eq(c.id,t),eq(c.workspace_id,e)):eq(c.id,t),s=n.db.select({pinned:c.pinned,metadata:c.metadata}).from(c).where(r).get();if(!s)return null;let i=s.pinned?0:1,l=s.metadata?JSON.parse(s.metadata):{};if(i){let u=e?and(eq(c.pinned,1),eq(c.workspace_id,e)):eq(c.pinned,1),f=n.db.select({metadata:c.metadata}).from(c).where(u).all(),_=0;for(let g of f){let R=g.metadata?JSON.parse(g.metadata):{};typeof R.pinOrder=="number"&&R.pinOrder>_&&(_=R.pinOrder);}l.pinOrder=_+1;}else delete l.pinOrder;return n.db.update(c).set({pinned:i,metadata:Object.keys(l).length>0?JSON.stringify(l):null}).where(r).run(),{pinned:!!i}}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to toggle pin",r)}finally{n.close();}}reorderPins(t,e){let n=this.openHandle(true);try{for(let r=0;r<t.length;r++){let s=e?and(eq(c.id,t[r]),eq(c.workspace_id,e)):eq(c.id,t[r]),i=n.db.select({metadata:c.metadata}).from(c).where(s).get();if(!i)continue;let l=i.metadata?JSON.parse(i.metadata):{};l.pinOrder=r+1,n.db.update(c).set({metadata:JSON.stringify(l)}).where(s).run();}}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to reorder pins",r)}finally{n.close();}}toggleStar(t,e){let n=this.openHandle(true);try{let r=e?and(eq(c.id,t),eq(c.workspace_id,e)):eq(c.id,t),s=n.db.select({starred:c.starred}).from(c).where(r).get();if(!s)return null;let i=s.starred?0:1;return n.db.update(c).set({starred:i}).where(r).run(),{starred:!!i}}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to toggle star",r)}finally{n.close();}}};var yt=class extends E{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 r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true});}else if(!existsSync(e))throw new a("NOT_FOUND","Database not found");let n=w(e);if(t)try{O(n.db,e);}catch(r){throw n.close(),r}return n}insertSpan(t){let e=this.openHandle(true);try{e.db.insert(F).values(t).run();}catch(n){throw n instanceof a?n:new a("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(F).where(eq(F.task_id,t)).all()}catch(n){throw new a("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(F).where(eq(F.id,t)).limit(1).get()??void 0}catch(n){throw new a("DB_ERROR","Failed to find span by id",n)}finally{e.close();}}};var Rt=class extends E{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 r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true});}else if(!existsSync(e))throw new a("NOT_FOUND","Database not found");let n=w(e);if(t)try{O(n.db,e);}catch(r){throw n.close(),r}return n}insertToolCall(t){let e=this.openHandle(true);try{e.db.insert(v).values(t).run();}catch(n){throw n instanceof a?n:new a("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(v).where(eq(v.task_id,t)).all()}catch(n){throw new a("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:v.tool_name,count:sql`count(*)`}).from(v).where(eq(v.task_id,t)).groupBy(v.tool_name).all().map(n=>({toolName:n.toolName,count:n.count}))}catch(n){throw new a("DB_ERROR","Failed to aggregate tool calls by name",n)}finally{e.close();}}};var Et=class extends E{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 r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true});}else if(!existsSync(e))throw new a("NOT_FOUND","Database not found");let n=w(e);if(t)try{O(n.db,e);}catch(r){throw n.close(),r}return n}ensureThreadExists(t,e){if(!t.db.select({id:c.id}).from(c).where(eq(c.id,e)).limit(1).get())throw new a("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(A).where(eq(A.thread_id,t)).orderBy(asc(A.seq)).all()}catch(n){throw n instanceof a?n:new a("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(A).where(and(eq(A.id,e),eq(A.thread_id,t))).limit(1).get()??void 0}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to find box by id",r)}finally{n.close();}}insert(t){let e=this.openHandle(true);try{this.ensureThreadExists(e,t.threadId);try{e.db.insert(A).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(r){throw r instanceof Error&&/UNIQUE constraint failed/i.test(r.message)?new a("CONFLICT",`Duplicate seq ${String(t.seq)} for thread ${t.threadId}`,r):r}let n=e.db.select().from(A).where(eq(A.id,t.id)).limit(1).get();if(!n)throw new a("DB_ERROR","Insert did not return a row");return n}catch(n){throw n instanceof a?n:new a("DB_ERROR","Failed to insert thread box",n)}finally{e.close();}}};var xt=class extends E{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 r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true});}else if(!existsSync(e))throw new a("NOT_FOUND","Database not found");let n=w(e);if(t)try{O(n.db,e);}catch(r){throw n.close(),r}return n}bulkInsert(t){if(t.length===0)return;let e=this.openHandle(true);try{let n=new Date().toISOString();e.db.transaction(r=>{for(let s of t)r.insert(Y).values({id:randomUUID(),path:s.path,method:s.method,status_code:s.statusCode,duration_ms:s.durationMs,ip:s.ip??null,request_headers:s.requestHeaders??null,response_headers:s.responseHeaders??null,request_body:s.requestBody??null,response_body:s.responseBody??null,query:s.query??null,user_id:s.userId??null,project_id:s.projectId??null,partition_key:s.partitionKey??"default",timestamp:n,metadata:s.metadata??null}).run();});}catch(n){throw n instanceof a?n:new a("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(Y).orderBy(desc(Y.timestamp)).limit(t).all()}catch(n){throw new a("DB_ERROR","Failed to find recent request logs",n)}finally{e.close();}}};export{E as BaseSqliteRepository,a as RepositoryError,xt as RequestLogRepository,yt as SpanRepository,gt as TaskRepository,Et as ThreadBoxRepository,bt as ThreadRepository,Rt as ToolCallRepository,ht as WorkspaceRepository,w as openDrizzleDb,Oe as pushSchema,W as runMigrations,O as runMigrationsOnce};
|
package/dist/plugins/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
'use strict';var fs=require('fs'),q=require('path'),Ot=require('os'),crypto=require('crypto'),drizzleOrm=require('drizzle-orm'),sqliteCore=require('drizzle-orm/sqlite-core');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var q__namespace=/*#__PURE__*/_interopNamespace(q);var Ot__default=/*#__PURE__*/_interopDefault(Ot);var z=(d=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(d,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):d)(function(d){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+d+'" is not supported')});var
|
|
1
|
+
'use strict';var fs=require('fs'),q=require('path'),Ot=require('os'),crypto=require('crypto'),drizzleOrm=require('drizzle-orm'),sqliteCore=require('drizzle-orm/sqlite-core');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var q__namespace=/*#__PURE__*/_interopNamespace(q);var Ot__default=/*#__PURE__*/_interopDefault(Ot);var z=(d=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(d,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):d)(function(d){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+d+'" is not supported')});var O=class{detach(t){}};function dt(d){let t=e=>String(e).padStart(2,"0");return `${d.getFullYear()}${t(d.getMonth()+1)}${t(d.getDate())}T${t(d.getHours())}${t(d.getMinutes())}${t(d.getSeconds())}`}var Z=class extends O{name="file-logger";unsubs=[];logFiles=new Map;logsDir;version;constructor(t){super(),this.logsDir=q.join(t?.workspaceRoot??process.cwd(),".crewx","logs"),this.version=t?.version??"unknown";}attach(t){this.unsubs.push(t.on("task:start",e=>{try{fs.existsSync(this.logsDir)||fs.mkdirSync(this.logsDir,{recursive:!0});let s=dt(e.timestamp),n=q.join(this.logsDir,`${s}_${e.traceId}.log`);this.logFiles.set(e.traceId,n);let r=`=== TASK LOG: ${e.traceId} ===
|
|
2
2
|
CrewX Version: ${this.version}
|
|
3
3
|
Mode: ${e.mode}
|
|
4
4
|
Agent: ${e.agentRef}
|
|
@@ -8,11 +8,11 @@ Message: ${e.message}
|
|
|
8
8
|
`;fs.writeFileSync(n,r,{encoding:"utf8",mode:384});}catch{}}),t.on("task:output",e=>{try{let s=this.logFiles.get(e.traceId);if(!s)return;let n=new Date().toISOString();fs.appendFileSync(s,`[${n}] STDOUT: ${e.output}
|
|
9
9
|
`,"utf8");}catch{}}),t.on("task:end",e=>{try{let s=this.logFiles.get(e.traceId);if(!s)return;let n=new Date().toLocaleString(),r=e.error?`failed: ${e.error.message}`:"completed successfully",i=`[${n}] INFO: Task ${r} in ${e.durationMs}ms
|
|
10
10
|
[${n}] INFO: Process closed with exit code: ${e.error?1:0}
|
|
11
|
-
`;fs.appendFileSync(s,i,"utf8"),this.logFiles.delete(e.traceId);}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[],this.logFiles.clear();}};function xt(d){let t=q__namespace.resolve(d);return process.platform==="win32"&&(t=t.replace(/\\/g,"/"),t=t.replace(/^([A-Z]):/,(e,s)=>`${s.toLowerCase()}:`)),t.length>1&&!/^[a-zA-Z]:\/$/.test(t)&&(t=t.replace(/\/+$/,"")),t}function pt(d){let t=xt(d);return crypto.createHash("sha256").update(t).digest("hex")}var M=class{resolveDbPath(){return process.env.CREWX_DB?process.env.CREWX_DB:process.env.CREWX_TRACES_DB?process.env.CREWX_TRACES_DB:q.join(Ot__default.default.homedir(),".crewx","crewx.db")}resolveDbPaths(){return [this.resolveDbPath()]}isMissingTableError(t){return t instanceof Error&&/no such table:/i.test(t.message)}dbExists(t){return fs.existsSync(t??this.resolveDbPath())}};function w(d){let t=z("better-sqlite3"),{drizzle:e}=z("drizzle-orm/better-sqlite3"),s=new t(d);return s.exec("PRAGMA journal_mode = WAL"),s.exec("PRAGMA busy_timeout = 5000"),s.exec("PRAGMA foreign_keys = ON"),{db:e(s),runRaw:(n,r=[])=>s.prepare(n).run(...r),close:()=>s.close()}}var _t=new Set,vt={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 Pt(d,t){return (d.get(`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='${t}'`)?.cnt??0)>0}function Nt(d,t){if(t>0||!Pt(d,"tasks"))return;let e=d.all("PRAGMA table_info(tasks)"),s=new Set(e.map(n=>n.name));for(let[n,r]of Object.entries(vt))s.has(n)||d.run(`ALTER TABLE tasks ADD COLUMN ${n} ${r}`);}function Lt(d){let{migrate:t}=z("drizzle-orm/better-sqlite3/migrator"),{sql:e}=z("drizzle-orm"),s=[q__namespace.default.join(__dirname,"../migrations"),q__namespace.default.join(__dirname,"migrations"),q__namespace.default.join(__dirname,"../../../../drizzle/migrations"),q__namespace.default.join(process.cwd(),"drizzle/migrations")],n=s.find(
|
|
11
|
+
`;fs.appendFileSync(s,i,"utf8"),this.logFiles.delete(e.traceId);}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[],this.logFiles.clear();}};function xt(d){let t=q__namespace.resolve(d);return process.platform==="win32"&&(t=t.replace(/\\/g,"/"),t=t.replace(/^([A-Z]):/,(e,s)=>`${s.toLowerCase()}:`)),t.length>1&&!/^[a-zA-Z]:\/$/.test(t)&&(t=t.replace(/\/+$/,"")),t}function pt(d){let t=xt(d);return crypto.createHash("sha256").update(t).digest("hex")}var M=class{resolveDbPath(){return process.env.CREWX_DB?process.env.CREWX_DB:process.env.CREWX_TRACES_DB?process.env.CREWX_TRACES_DB:q.join(Ot__default.default.homedir(),".crewx","crewx.db")}resolveDbPaths(){return [this.resolveDbPath()]}isMissingTableError(t){return t instanceof Error&&/no such table:/i.test(t.message)}dbExists(t){return fs.existsSync(t??this.resolveDbPath())}};function w(d){let t=z("better-sqlite3"),{drizzle:e}=z("drizzle-orm/better-sqlite3"),s=new t(d);return s.exec("PRAGMA journal_mode = WAL"),s.exec("PRAGMA busy_timeout = 5000"),s.exec("PRAGMA foreign_keys = ON"),{db:e(s),runRaw:(n,r=[])=>s.prepare(n).run(...r),close:()=>s.close()}}var _t=new Set,vt={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 Pt(d,t){return (d.get(`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='${t}'`)?.cnt??0)>0}function Nt(d,t){if(t>0||!Pt(d,"tasks"))return;let e=d.all("PRAGMA table_info(tasks)"),s=new Set(e.map(n=>n.name));for(let[n,r]of Object.entries(vt))s.has(n)||d.run(`ALTER TABLE tasks ADD COLUMN ${n} ${r}`);}function Lt(d){let{migrate:t}=z("drizzle-orm/better-sqlite3/migrator"),{sql:e}=z("drizzle-orm"),s=[q__namespace.default.join(__dirname,"../migrations"),q__namespace.default.join(__dirname,"migrations"),q__namespace.default.join(__dirname,"../../../../drizzle/migrations"),q__namespace.default.join(process.cwd(),"drizzle/migrations")],n=s.find(_=>fs.existsSync(q__namespace.default.join(_,"meta/_journal.json")));if(!n)throw new Error(`migrations folder not found. Searched:
|
|
12
12
|
${s.join(`
|
|
13
|
-
`)}`);let r=d.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),i=0;r?.cnt&&(i=d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),Nt(d,i),t(d,{migrationsFolder:n});let
|
|
13
|
+
`)}`);let r=d.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),i=0;r?.cnt&&(i=d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),Nt(d,i),t(d,{migrationsFolder:n});let c=(d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0)-i;if(c>0){let _=r?.cnt?"Database migrated":"Database initialized";console.log(`[crewx] ${_} (${c} migration${c>1?"s":""} applied).`);}}function H(d,t){_t.has(t)||(Lt(d),_t.add(t));}var u=class extends Error{code;cause;constructor(t,e,s){super(e),this.name="RepositoryError",this.code=t,this.cause=s,Object.setPrototypeOf(this,new.target.prototype);}};var F=sqliteCore.sqliteTable("workspaces",{id:sqliteCore.text("id").primaryKey(),slug:sqliteCore.text("slug").notNull().unique(),name:sqliteCore.text("name").notNull(),workspace_path:sqliteCore.text("workspace_path"),description:sqliteCore.text("description"),is_active:sqliteCore.integer("is_active").notNull().default(1),created_at:sqliteCore.text("created_at").notNull(),updated_at:sqliteCore.text("updated_at").notNull()});var o=sqliteCore.sqliteTable("tasks",{id:sqliteCore.text("id").primaryKey(),agent_id:sqliteCore.text("agent_id").notNull(),user_id:sqliteCore.text("user_id"),prompt:sqliteCore.text("prompt").notNull(),mode:sqliteCore.text("mode").notNull().default("execute"),status:sqliteCore.text("status").notNull().default("running"),result:sqliteCore.text("result"),error:sqliteCore.text("error"),started_at:sqliteCore.text("started_at").notNull(),completed_at:sqliteCore.text("completed_at"),duration_ms:sqliteCore.integer("duration_ms"),metadata:sqliteCore.text("metadata"),workspace_id:sqliteCore.text("workspace_id"),trace_id:sqliteCore.text("trace_id"),parent_task_id:sqliteCore.text("parent_task_id"),caller_agent_id:sqliteCore.text("caller_agent_id"),model:sqliteCore.text("model"),platform:sqliteCore.text("platform").default("cli"),crewx_version:sqliteCore.text("crewx_version"),input_tokens:sqliteCore.integer("input_tokens").default(0),output_tokens:sqliteCore.integer("output_tokens").default(0),cost_usd:sqliteCore.real("cost_usd").default(0),pid:sqliteCore.integer("pid"),rendered_prompt:sqliteCore.text("rendered_prompt"),command:sqliteCore.text("command"),coding_agent_command:sqliteCore.text("coding_agent_command"),exit_code:sqliteCore.integer("exit_code"),logs:sqliteCore.text("logs"),thread_id:sqliteCore.text("thread_id"),workspace_ref:sqliteCore.text("workspace_ref"),project_id:sqliteCore.text("project_id"),project_ref:sqliteCore.text("project_ref"),cached_input_tokens:sqliteCore.integer("cached_input_tokens").default(0)},d=>({idx_tasks_agent_id:sqliteCore.index("idx_tasks_agent_id").on(d.agent_id),idx_tasks_status:sqliteCore.index("idx_tasks_status").on(d.status),idx_tasks_started_at:sqliteCore.index("idx_tasks_started_at").on(d.started_at),idx_tasks_trace_id:sqliteCore.index("idx_tasks_trace_id").on(d.trace_id),idx_tasks_parent_task_id:sqliteCore.index("idx_tasks_parent_task_id").on(d.parent_task_id),idx_tasks_crewx_version:sqliteCore.index("idx_tasks_crewx_version").on(d.crewx_version),idx_tasks_pid:sqliteCore.index("idx_tasks_pid").on(d.pid),idx_tasks_thread_id:sqliteCore.index("idx_tasks_thread_id").on(d.thread_id),idx_tasks_workspace_id:sqliteCore.index("idx_tasks_workspace_id").on(d.workspace_id),idx_tasks_workspace_ref:sqliteCore.index("idx_tasks_workspace_ref").on(d.workspace_ref),idx_tasks_project_id:sqliteCore.index("idx_tasks_project_id").on(d.project_id),idx_tasks_ws_started:sqliteCore.index("idx_tasks_ws_started").on(d.workspace_id,d.started_at)}));var l=sqliteCore.sqliteTable("threads",{id:sqliteCore.text("id").primaryKey(),workspace_id:sqliteCore.text("workspace_id").references(()=>F.id,{onDelete:"set null"}),platform:sqliteCore.text("platform").notNull().default("cli"),title:sqliteCore.text("title"),first_message:sqliteCore.text("first_message"),last_message:sqliteCore.text("last_message"),message_count:sqliteCore.integer("message_count").notNull().default(0),created_at:sqliteCore.text("created_at").notNull(),updated_at:sqliteCore.text("updated_at").notNull(),metadata:sqliteCore.text("metadata"),title_locked:sqliteCore.integer("title_locked").notNull().default(0),pinned:sqliteCore.integer("pinned").notNull().default(0),starred:sqliteCore.integer("starred").notNull().default(0)},d=>({idx_threads_updated_at:sqliteCore.index("idx_threads_updated_at").on(d.updated_at),idx_threads_workspace_id:sqliteCore.index("idx_threads_workspace_id").on(d.workspace_id)}));var zt=sqliteCore.sqliteTable("spans",{id:sqliteCore.text("id").primaryKey(),task_id:sqliteCore.text("task_id").references(()=>o.id,{onDelete:"set null"}),parent_span_id:sqliteCore.text("parent_span_id").references(()=>zt.id,{onDelete:"set null"}),name:sqliteCore.text("name").notNull(),kind:sqliteCore.text("kind").notNull().default("internal"),status:sqliteCore.text("status").notNull().default("ok"),started_at:sqliteCore.text("started_at").notNull(),completed_at:sqliteCore.text("completed_at"),duration_ms:sqliteCore.integer("duration_ms"),input:sqliteCore.text("input"),output:sqliteCore.text("output"),error:sqliteCore.text("error"),attributes:sqliteCore.text("attributes")},d=>({idx_spans_task_id:sqliteCore.index("idx_spans_task_id").on(d.task_id),idx_spans_parent_span_id:sqliteCore.index("idx_spans_parent_span_id").on(d.parent_span_id)}));sqliteCore.sqliteTable("tool_calls",{id:sqliteCore.text("id").primaryKey(),task_id:sqliteCore.text("task_id").references(()=>o.id,{onDelete:"cascade"}),session_id:sqliteCore.text("session_id"),tool_name:sqliteCore.text("tool_name").notNull(),files:sqliteCore.text("files"),input:sqliteCore.text("input"),output:sqliteCore.text("output"),duration_ms:sqliteCore.integer("duration_ms"),timestamp:sqliteCore.text("timestamp").notNull()},d=>({idx_tool_calls_task_id:sqliteCore.index("idx_tool_calls_task_id").on(d.task_id),idx_tool_calls_tool_name:sqliteCore.index("idx_tool_calls_tool_name").on(d.tool_name),idx_tool_calls_timestamp:sqliteCore.index("idx_tool_calls_timestamp").on(d.timestamp)}));sqliteCore.sqliteTable("thread_boxes",{id:sqliteCore.text("id").primaryKey(),thread_id:sqliteCore.text("thread_id").notNull().references(()=>l.id,{onDelete:"cascade"}),seq:sqliteCore.integer("seq").notNull(),first_task_id:sqliteCore.text("first_task_id").notNull(),mid_task_id:sqliteCore.text("mid_task_id").notNull(),last_task_id:sqliteCore.text("last_task_id").notNull(),task_count:sqliteCore.integer("task_count").notNull(),summary:sqliteCore.text("summary"),source_tokens:sqliteCore.integer("source_tokens").notNull(),summary_tokens:sqliteCore.integer("summary_tokens"),created_at:sqliteCore.text("created_at").notNull()},d=>({idx_thread_boxes_thread_id:sqliteCore.index("idx_thread_boxes_thread_id").on(d.thread_id),idx_thread_boxes_seq:sqliteCore.index("idx_thread_boxes_seq").on(d.thread_id,d.seq),uniq_thread_boxes_thread_seq:sqliteCore.unique().on(d.thread_id,d.seq)}));sqliteCore.sqliteTable("request_logs",{id:sqliteCore.text("id").primaryKey(),path:sqliteCore.text("path").notNull(),method:sqliteCore.text("method").notNull(),status_code:sqliteCore.integer("status_code").notNull(),duration_ms:sqliteCore.integer("duration_ms").notNull(),ip:sqliteCore.text("ip"),request_headers:sqliteCore.text("request_headers"),response_headers:sqliteCore.text("response_headers"),request_body:sqliteCore.text("request_body"),response_body:sqliteCore.text("response_body"),query:sqliteCore.text("query"),user_id:sqliteCore.text("user_id"),project_id:sqliteCore.text("project_id"),partition_key:sqliteCore.text("partition_key").notNull(),timestamp:sqliteCore.text("timestamp").notNull().default(drizzleOrm.sql`(datetime('now'))`),metadata:sqliteCore.text("metadata")},d=>({idx_request_logs_timestamp:sqliteCore.index("idx_request_logs_timestamp").on(d.timestamp),idx_request_logs_path:sqliteCore.index("idx_request_logs_path").on(d.path),idx_request_logs_status_code:sqliteCore.index("idx_request_logs_status_code").on(d.status_code),idx_request_logs_partition_key:sqliteCore.index("idx_request_logs_partition_key").on(d.partition_key)}));var J=class extends M{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=q.join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let n=q.dirname(e);fs.existsSync(n)||fs.mkdirSync(n,{recursive:true});}else if(!fs.existsSync(e))throw new u("NOT_FOUND","Database not found");let s=w(e);if(t)try{H(s.db,e);}catch(n){throw s.close(),n}return s}startTask(t){let e=this.openHandle(true);try{e.db.insert(o).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(s){throw s instanceof u?s:new u("DB_ERROR","Failed to start task",s)}finally{e.close();}}finishTask(t){let e=this.openHandle(true);try{e.runRaw(`UPDATE tasks SET status=?, result=?, error=?, completed_at=?, duration_ms=?,
|
|
14
14
|
exit_code=?, input_tokens=?, output_tokens=?, cached_input_tokens=?, cost_usd=?,
|
|
15
|
-
model=COALESCE(?, model) WHERE id=?`,[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(s){throw s instanceof
|
|
15
|
+
model=COALESCE(?, model) WHERE id=?`,[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(s){throw s instanceof u?s:new u("DB_ERROR","Failed to finish task",s)}finally{e.close();}}appendLog(t,e){let s=this.openHandle(true);try{s.db.transaction(n=>{let r=n.select({logs:o.logs}).from(o).where(drizzleOrm.eq(o.id,t)).limit(1).get(),i=r?.logs?JSON.parse(r.logs):[];i.push(e),n.update(o).set({logs:JSON.stringify(i)}).where(drizzleOrm.eq(o.id,t)).run();},{behavior:"immediate"});}catch(n){throw n instanceof u?n:new u("DB_ERROR","Failed to append log",n)}finally{s.close();}}getRunningTasks(){if(!this.dbExists())return [];let t=this.openHandle(false);try{return t.db.select().from(o).where(drizzleOrm.eq(o.status,"running")).orderBy(drizzleOrm.desc(o.started_at)).all()}catch(e){throw new u("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(o).orderBy(drizzleOrm.desc(o.started_at)).limit(100).all()}catch(e){throw new u("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(o).where(drizzleOrm.eq(o.id,t)).limit(1).get()??void 0}catch(s){throw new u("DB_ERROR","Failed to get task",s)}finally{e.close();}}killTask(t){if(!this.dbExists())return {killed:false};let e=this.openHandle(true);try{let s=e.db.select({id:o.id,status:o.status,pid:o.pid}).from(o).where(drizzleOrm.eq(o.id,t)).limit(1).get();if(!s||s.status!=="running")return {killed:!1};if(s.pid)try{process.kill(s.pid,"SIGTERM");}catch{}return e.db.update(o).set({status:"failed",error:"Killed by user",completed_at:new Date().toISOString()}).where(drizzleOrm.and(drizzleOrm.eq(o.id,t),drizzleOrm.eq(o.status,"running"))).run(),{killed:!0,pid:s.pid??void 0}}catch(s){throw s instanceof u?s:new u("DB_ERROR","Failed to kill task",s)}finally{e.close();}}reapOrphanedTasks(){if(!this.dbExists())return 0;let t=this.openHandle(true);try{let e=t.db.select({id:o.id,pid:o.pid}).from(o).where(drizzleOrm.eq(o.status,"running")).all(),s=0;for(let n of e){let r=!1;if(n.pid)try{process.kill(n.pid,0),r=!0;}catch{}r||(t.db.update(o).set({status:"failed",error:"Reaped: process not found (orphaned task)",completed_at:new Date().toISOString()}).where(drizzleOrm.and(drizzleOrm.eq(o.id,n.id),drizzleOrm.eq(o.status,"running"))).run(),s++);}return s}finally{t.close();}}findTaskStatus(t,e){let s=this.resolveDbPaths();for(let n of s){if(!fs.existsSync(n))continue;let r=w(n);try{let i=e?drizzleOrm.eq(o.workspace_id,e):void 0,a=i?drizzleOrm.and(drizzleOrm.eq(o.id,t),i):drizzleOrm.eq(o.id,t),c=r.db.select().from(o).where(a).limit(1).get()??void 0;if(!c){let _=drizzleOrm.or(drizzleOrm.eq(o.thread_id,t),drizzleOrm.and(drizzleOrm.isNull(o.thread_id),drizzleOrm.like(o.command,`%--thread=${t}%`))),m=i?drizzleOrm.and(_,i):_;c=r.db.select().from(o).where(m).orderBy(drizzleOrm.desc(o.started_at)).limit(1).get()??void 0;}if(c)return c}catch(i){throw new u("DB_ERROR","Failed to find task status",i)}finally{r.close();}}}findChildTasks(t,e){let s=this.resolveDbPaths(),n=new Set,r=[];for(let i of s){if(!fs.existsSync(i))continue;let a=w(i);try{let c=e?drizzleOrm.and(drizzleOrm.eq(o.parent_task_id,t),drizzleOrm.eq(o.workspace_id,e)):drizzleOrm.eq(o.parent_task_id,t),_=a.db.select().from(o).where(c).orderBy(drizzleOrm.asc(o.started_at)).all();for(let m of _)n.has(m.id)||(n.add(m.id),r.push(m));}catch(c){throw new u("DB_ERROR","Failed to find child tasks",c)}finally{a.close();}}return r}getWorkspaceUsageSummary(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.all(t?drizzleOrm.sql`
|
|
16
16
|
SELECT
|
|
17
17
|
COALESCE(workspace_id, 'unknown') AS workspace_id,
|
|
18
18
|
COALESCE(SUM(input_tokens), 0) AS input_tokens,
|
|
@@ -33,7 +33,7 @@ ${s.join(`
|
|
|
33
33
|
FROM tasks
|
|
34
34
|
GROUP BY workspace_id
|
|
35
35
|
ORDER BY (COALESCE(SUM(input_tokens), 0) + COALESCE(SUM(output_tokens), 0)) DESC
|
|
36
|
-
`)}catch(s){throw new
|
|
36
|
+
`)}catch(s){throw new u("DB_ERROR","Failed to get workspace usage summary",s)}finally{e.close();}}getThreadTokenUsage(t,e){let s=this.resolveDbPaths(),n=new Set,r=0,i=0,a=0;for(let c of s){if(!fs.existsSync(c))continue;let _=w(c);try{let m=drizzleOrm.or(drizzleOrm.eq(o.thread_id,t),drizzleOrm.and(drizzleOrm.isNull(o.thread_id),drizzleOrm.like(o.command,`%--thread=${t}%`))),h=e?drizzleOrm.and(m,drizzleOrm.eq(o.workspace_id,e)):m,S=_.db.select({id:o.id,input_tokens:o.input_tokens,output_tokens:o.output_tokens,cost_usd:o.cost_usd}).from(o).where(h).all();for(let v of S)n.has(v.id)||(n.add(v.id),r+=v.input_tokens??0,i+=v.output_tokens??0,a+=v.cost_usd??0);}catch(m){throw new u("DB_ERROR","Failed to get thread token usage",m)}finally{_.close();}}return {inputTokens:r,outputTokens:i,costUsd:a}}findTasksByThread(t,e){let s=this.resolveDbPaths(),n=new Set,r=[];for(let i of s){if(!fs.existsSync(i))continue;let a=w(i);try{let c=drizzleOrm.or(drizzleOrm.eq(o.thread_id,t),drizzleOrm.and(drizzleOrm.isNull(o.thread_id),drizzleOrm.like(o.command,`%--thread=${t}%`))),_=e?drizzleOrm.and(c,drizzleOrm.eq(o.workspace_id,e)):c,m=a.db.select().from(o).where(_).orderBy(drizzleOrm.asc(o.started_at)).all();for(let h of m)n.has(h.id)||(n.add(h.id),r.push(h));}catch(c){throw new u("DB_ERROR","Failed to find tasks by thread",c)}finally{a.close();}}return r}findAllTasks(t){if(!this.dbExists())return {rows:[],total:0};let e=this.openHandle(false);try{let s=[];t.workspaceId&&s.push(drizzleOrm.eq(o.workspace_id,t.workspaceId));let n=t.agents&&t.agents.length>0?t.agents:t.agentId?[t.agentId]:null;n&&s.push(drizzleOrm.inArray(o.agent_id,n));let r=t.statuses&&t.statuses.length>0?t.statuses:t.status?[t.status]:null;r&&s.push(drizzleOrm.inArray(o.status,r));let i=t.q??t.search;i&&s.push(drizzleOrm.like(o.prompt,`%${i}%`)),t.from&&s.push(drizzleOrm.gte(o.started_at,t.from)),t.to&&s.push(drizzleOrm.lt(o.started_at,t.to));let a=s.length>0?drizzleOrm.and(...s):void 0,c=e.db.select({count:drizzleOrm.sql`count(*)`}).from(o).where(a).get(),_=(t.sortDir??"DESC")==="ASC"?drizzleOrm.asc(o.started_at):drizzleOrm.desc(o.started_at);return {rows:e.db.select().from(o).where(a).orderBy(_).limit(t.limit).offset(t.offset).all(),total:c?.count??0}}catch(s){throw new u("DB_ERROR","Failed to find all tasks",s)}finally{e.close();}}getAgentUsage(t,e,s){if(!this.dbExists())return [];let n=this.openHandle(false);try{return n.db.all(s?drizzleOrm.sql`
|
|
37
37
|
SELECT
|
|
38
38
|
t.agent_id,
|
|
39
39
|
t.workspace_id,
|
|
@@ -64,7 +64,7 @@ ${s.join(`
|
|
|
64
64
|
AND t.started_at < ${e}
|
|
65
65
|
GROUP BY t.agent_id, t.workspace_id
|
|
66
66
|
ORDER BY (COALESCE(SUM(t.input_tokens), 0) + COALESCE(SUM(t.output_tokens), 0)) DESC
|
|
67
|
-
`).map(i=>({agentId:i.agent_id,workspaceId:i.workspace_id??null,totalTasks:i.total_tasks,inputTokens:i.input_tokens,outputTokens:i.output_tokens,cachedInputTokens:i.cached_input_tokens,costUsd:i.cost_usd}))}catch(r){throw new
|
|
67
|
+
`).map(i=>({agentId:i.agent_id,workspaceId:i.workspace_id??null,totalTasks:i.total_tasks,inputTokens:i.input_tokens,outputTokens:i.output_tokens,cachedInputTokens:i.cached_input_tokens,costUsd:i.cost_usd}))}catch(r){throw new u("DB_ERROR","Failed to get agent usage",r)}finally{n.close();}}getAgentUsageTrendRaw(t,e,s){if(!this.dbExists())return [];let n=this.openHandle(false);try{return n.db.all(s?drizzleOrm.sql`
|
|
68
68
|
SELECT
|
|
69
69
|
date(t.started_at) AS date,
|
|
70
70
|
t.agent_id,
|
|
@@ -93,13 +93,13 @@ ${s.join(`
|
|
|
93
93
|
AND t.started_at < ${e}
|
|
94
94
|
GROUP BY date(t.started_at), t.agent_id
|
|
95
95
|
ORDER BY date(t.started_at) ASC
|
|
96
|
-
`).map(i=>({date:i.date,agentId:i.agent_id,inputTokens:i.input_tokens,outputTokens:i.output_tokens,cachedInputTokens:i.cached_input_tokens,costUsd:i.cost_usd}))}catch(r){throw new
|
|
96
|
+
`).map(i=>({date:i.date,agentId:i.agent_id,inputTokens:i.input_tokens,outputTokens:i.output_tokens,cachedInputTokens:i.cached_input_tokens,costUsd:i.cost_usd}))}catch(r){throw new u("DB_ERROR","Failed to get agent usage trend",r)}finally{n.close();}}findTaskForStop(t,e){if(!this.dbExists())return;let s=this.openHandle(false);try{return s.db.select().from(o).where(drizzleOrm.and(drizzleOrm.eq(o.id,t),drizzleOrm.eq(o.workspace_id,e))).limit(1).get()??void 0}catch(n){throw new u("DB_ERROR","Failed to find task for stop",n)}finally{s.close();}}markTaskFailed(t,e,s){if(!this.dbExists())return;let n=this.openHandle(true);try{let r=new Date().toISOString(),i=s?drizzleOrm.and(drizzleOrm.eq(o.id,t),drizzleOrm.eq(o.status,"running"),drizzleOrm.eq(o.workspace_id,s)):drizzleOrm.and(drizzleOrm.eq(o.id,t),drizzleOrm.eq(o.status,"running"));n.db.update(o).set({status:"failed",error:e,completed_at:r}).where(i).run();}catch(r){throw r instanceof u?r:new u("DB_ERROR","Failed to mark task failed",r)}finally{n.close();}}findTasksByPromptHint(t,e){let s=this.resolveDbPaths(),n=new Set,r=[];for(let i of s){if(!fs.existsSync(i))continue;let a=w(i);try{let c=e?drizzleOrm.and(drizzleOrm.like(o.prompt,`%${t}%`),drizzleOrm.eq(o.workspace_id,e)):drizzleOrm.like(o.prompt,`%${t}%`),_=a.db.select().from(o).where(c).orderBy(drizzleOrm.asc(o.started_at)).all();for(let m of _)n.has(m.id)||(n.add(m.id),r.push(m));}catch(c){throw new u("DB_ERROR","Failed to find tasks by prompt hint",c)}finally{a.close();}}return r}};var rt=class extends O{name="sqlite-tracing";unsubs=[];dbPath;version;constructor(t){super(),this.dbPath=q.join(t?.dbRoot??Ot.homedir(),".crewx","crewx.db"),this.version=t?.version??"unknown";}attach(t){let e=new J({dbPath:this.dbPath}),s=process.cwd(),r=fs.existsSync(q.join(s,"crewx.yaml"))||fs.existsSync(q.join(s,"crewx.yml"))?pt(s):null,i=process.argv.join(" ");this.unsubs.push(t.on("task:start",a=>{try{let c=process.env.CREWX_CALLER_AGENT_ID||null,_=process.env.CREWX_PARENT_TASK_ID||null,m=process.env.CREWX_TRACE_ID||a.traceId,h=a.metadata?JSON.stringify(a.metadata):JSON.stringify({provider:a.provider??"cli/claude"});e.startTask({id:a.traceId,agentId:a.agentRef.replace(/^@/,""),prompt:a.message,mode:a.mode,status:"running",pid:a.pid??null,startedAt:a.timestamp.toISOString(),crewxVersion:this.version,platform:a.platform??"cli",model:a.model??null,renderedPrompt:a.renderedPrompt??null,command:i,codingAgentCommand:a.codingAgentCommand??null,workspaceId:a.workspaceId??r,callerAgentId:c,parentTaskId:_,traceId:m,metadata:h,threadId:a.threadId??null});}catch{}}),t.on("task:output",a=>{try{e.appendLog(a.traceId,{timestamp:a.timestamp.toISOString(),level:a.level??"stdout",message:a.output});}catch{}}),t.on("task:end",a=>{try{e.finishTask({id:a.traceId,status:a.error?"failed":"success",result:a.result??null,error:a.error?JSON.stringify(a.error):null,completedAt:a.timestamp.toISOString(),durationMs:a.durationMs,exitCode:a.exitCode??null,inputTokens:a.inputTokens??0,outputTokens:a.outputTokens??0,cachedInputTokens:a.cachedInputTokens??0,costUsd:a.costUsd??0,model:a.model??null});}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[];}};var V=class extends M{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=q.join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let n=q.dirname(e);fs.existsSync(n)||fs.mkdirSync(n,{recursive:true});}else if(!fs.existsSync(e))throw new u("NOT_FOUND","Database not found");let s=w(e);if(t)try{H(s.db,e);}catch(n){throw s.close(),n}return s}validateWorkspaceId(t,e){return t.db.select({id:F.id}).from(F).where(drizzleOrm.eq(F.id,e)).limit(1).get()?e:null}findAllThreads(t){let e=this.resolveDbPaths(),s=new Set,n=[];for(let r of e){if(!fs.existsSync(r))continue;let i=w(r);try{let a=t?drizzleOrm.eq(l.workspace_id,t):void 0,c=i.db.select().from(l).where(a).orderBy(drizzleOrm.desc(l.updated_at)).all();for(let _ of c)s.has(_.id)||(s.add(_.id),n.push(_));}catch(a){throw new u("DB_ERROR","Failed to find all threads",a)}finally{i.close();}}return n}findThreadById(t,e){let s=this.resolveDbPaths();for(let n of s){if(!fs.existsSync(n))continue;let r=w(n);try{let i=drizzleOrm.eq(l.id,t),a=e?drizzleOrm.and(i,drizzleOrm.eq(l.workspace_id,e)):i,c=r.db.select().from(l).where(a).limit(1).get()??void 0;if(c)return c}catch(i){throw new u("DB_ERROR","Failed to find thread by id",i)}finally{r.close();}}}threadExists(t,e){let s=this.resolveDbPaths();for(let n of s){if(!fs.existsSync(n))continue;let r=w(n);try{let i=drizzleOrm.eq(l.id,t),a=e?drizzleOrm.and(i,drizzleOrm.eq(l.workspace_id,e)):i;if(r.db.select({id:l.id}).from(l).where(a).limit(1).get())return !0}catch(i){throw new u("DB_ERROR","Failed to check thread existence",i)}finally{r.close();}}return false}aggregateTaskStats(t,e){let s=this.resolveDbPaths(),n=0,r=0,i=0,a=0,c=0,_=new Set;for(let m of s){if(!fs.existsSync(m))continue;let h=w(m);try{let S=drizzleOrm.and(drizzleOrm.eq(o.thread_id,t),drizzleOrm.or(drizzleOrm.isNull(o.parent_task_id),drizzleOrm.eq(o.parent_task_id,""))),v=e?drizzleOrm.and(S,drizzleOrm.eq(o.workspace_id,e)):S,U=h.db.select({cnt:drizzleOrm.sql`count(*)`,total_input:drizzleOrm.sql`COALESCE(SUM(input_tokens), 0)`,total_output:drizzleOrm.sql`COALESCE(SUM(output_tokens), 0)`,total_cached:drizzleOrm.sql`COALESCE(SUM(cached_input_tokens), 0)`,total_cost:drizzleOrm.sql`COALESCE(SUM(cost_usd), 0)`}).from(o).where(v).get();U&&(n+=U.cnt,r+=U.total_input,i+=U.total_output,a+=U.total_cached,c+=U.total_cost);let bt=h.db.all(drizzleOrm.sql`
|
|
97
97
|
SELECT DISTINCT agent_id FROM tasks
|
|
98
98
|
WHERE thread_id = ${t}
|
|
99
99
|
AND agent_id IS NOT NULL AND agent_id != ''
|
|
100
100
|
AND (parent_task_id IS NULL OR parent_task_id = '')
|
|
101
101
|
${e?drizzleOrm.sql`AND workspace_id = ${e}`:drizzleOrm.sql``}
|
|
102
|
-
`);for(let yt of bt)
|
|
102
|
+
`);for(let yt of bt)_.add(yt.agent_id);}catch(S){throw new u("DB_ERROR","Failed to aggregate task stats",S)}finally{h.close();}}return {taskCount:n,inputTokens:r,outputTokens:i,cachedInputTokens:a,costUsd:c,agentIds:Array.from(_)}}findTopLevelTasks(t,e){let s=this.resolveDbPaths(),n=new Set,r=[];for(let i of s){if(!fs.existsSync(i))continue;let a=w(i);try{let c=drizzleOrm.and(drizzleOrm.eq(o.thread_id,t),drizzleOrm.or(drizzleOrm.isNull(o.parent_task_id),drizzleOrm.eq(o.parent_task_id,""))),_=e?drizzleOrm.and(c,drizzleOrm.eq(o.workspace_id,e)):c,m=a.db.select().from(o).where(_).orderBy(drizzleOrm.asc(o.started_at)).all();for(let h of m)n.has(h.id)||(n.add(h.id),r.push(h));}catch(c){throw new u("DB_ERROR","Failed to find top-level tasks",c)}finally{a.close();}}return r}findAllTasks(t,e){let s=this.resolveDbPaths(),n=new Set,r=[];for(let i of s){if(!fs.existsSync(i))continue;let a=w(i);try{let c=drizzleOrm.eq(o.thread_id,t),_=e?drizzleOrm.and(c,drizzleOrm.eq(o.workspace_id,e)):c,m=a.db.select().from(o).where(_).orderBy(drizzleOrm.asc(o.started_at)).all();for(let h of m)n.has(h.id)||(n.add(h.id),r.push(h));}catch(c){throw new u("DB_ERROR","Failed to find all tasks for thread",c)}finally{a.close();}}return r}findTaskById(t,e,s){let n=this.resolveDbPaths();for(let r of n){if(!fs.existsSync(r))continue;let i=w(r);try{let a=drizzleOrm.and(drizzleOrm.eq(o.id,e),drizzleOrm.eq(o.thread_id,t)),c=s?drizzleOrm.and(a,drizzleOrm.eq(o.workspace_id,s)):a,_=i.db.select().from(o).where(c).limit(1).get();if(!_)continue;let m=i.db.select().from(o).where(drizzleOrm.eq(o.parent_task_id,_.id)).orderBy(drizzleOrm.asc(o.started_at)).all();return {task:_,children:m}}catch(a){throw new u("DB_ERROR","Failed to find task by id",a)}finally{i.close();}}}batchFetchTasks(t,e){let s=new Map;if(t.length===0)return s;let n=this.resolveDbPaths();for(let r of n){if(!fs.existsSync(r))continue;let i=w(r);try{let a=drizzleOrm.and(drizzleOrm.inArray(o.thread_id,t),drizzleOrm.or(drizzleOrm.isNull(o.parent_task_id),drizzleOrm.eq(o.parent_task_id,""))),c=e?drizzleOrm.and(a,drizzleOrm.eq(o.workspace_id,e)):a,_=i.db.select().from(o).where(c).orderBy(drizzleOrm.asc(o.started_at)).all();for(let m of _){let h=m.thread_id;s.has(h)||s.set(h,[]),s.get(h).push(m);}}catch(a){throw new u("DB_ERROR","Failed to batch fetch tasks",a)}finally{i.close();}}return s}updateThreadTitle(t,e,s){if(!this.dbExists())return;let n=this.openHandle(true);try{let r=drizzleOrm.eq(l.id,t),i=s?drizzleOrm.and(r,drizzleOrm.eq(l.workspace_id,s)):r;if(!n.db.select({id:l.id}).from(l).where(i).limit(1).get())return;n.db.update(l).set({title:e,title_locked:1,updated_at:new Date().toISOString()}).where(drizzleOrm.eq(l.id,t)).run();}catch(r){throw r instanceof u?r:new u("DB_ERROR","Failed to update thread title",r)}finally{n.close();}}upsertThread(t,e){let s=this.openHandle(true);try{let n=e.workspaceId?this.validateWorkspaceId(s,e.workspaceId):null,r=new Date().toISOString();if(s.db.select({id:l.id,message_count:l.message_count}).from(l).where(drizzleOrm.eq(l.id,t)).limit(1).get()){let a={updated_at:r};e.title!==void 0&&(a.title=e.title),e.titleLocked!==void 0&&(a.title_locked=e.titleLocked?1:0),s.db.update(l).set(a).where(drizzleOrm.eq(l.id,t)).run();}else s.db.insert(l).values({id:t,platform:e.platform,workspace_id:n,title:e.title??null,title_locked:e.titleLocked?1:0,message_count:0,created_at:r,updated_at:r}).run();}catch(n){throw n instanceof u?n:new u("DB_ERROR","Failed to upsert thread",n)}finally{s.close();}}ensureThread(t,e,s){let n=this.openHandle(true);try{let r=s?this.validateWorkspaceId(n,s):null,i=n.db.select({id:l.id,platform:l.platform,workspace_id:l.workspace_id}).from(l).where(drizzleOrm.eq(l.id,t)).limit(1).get();if(i){r&&!i.workspace_id&&n.db.update(l).set({workspace_id:r}).where(drizzleOrm.eq(l.id,t)).run();return}let a=new Date().toISOString();n.db.insert(l).values({id:t,platform:e,workspace_id:r,message_count:0,created_at:a,updated_at:a}).run();}catch(r){throw r instanceof u?r:new u("DB_ERROR","Failed to ensure thread",r)}finally{n.close();}}saveUserMessage(t,e,s){if(!this.dbExists())return;let n=this.openHandle(true);try{let r=new Date().toISOString();n.db.run(drizzleOrm.sql`
|
|
103
103
|
UPDATE threads
|
|
104
104
|
SET first_message = COALESCE(first_message, ${e}),
|
|
105
105
|
title = CASE WHEN title_locked = 0 AND title IS NULL THEN substr(${e}, 1, 60) ELSE title END,
|
|
@@ -107,7 +107,7 @@ ${s.join(`
|
|
|
107
107
|
message_count = message_count + 1,
|
|
108
108
|
updated_at = ${r}
|
|
109
109
|
WHERE id = ${t}
|
|
110
|
-
`);}catch(r){throw r instanceof
|
|
110
|
+
`);}catch(r){throw r instanceof u?r:new u("DB_ERROR","Failed to save user message",r)}finally{n.close();}}saveAssistantMessage(t,e,s){if(!this.dbExists())return;let n=this.openHandle(true);try{let r=new Date().toISOString();n.db.update(l).set({last_message:e,updated_at:r}).where(drizzleOrm.eq(l.id,t)).run();}catch(r){throw r instanceof u?r:new u("DB_ERROR","Failed to save assistant message",r)}finally{n.close();}}updateThread(t,e){if(!this.dbExists())return;let s=this.openHandle(true);try{let n={updated_at:new Date().toISOString()};e.title!==void 0&&(n.title=e.title,n.title_locked=1),e.titleLocked!==void 0&&(n.title_locked=e.titleLocked?1:0),s.db.update(l).set(n).where(drizzleOrm.eq(l.id,t)).run();}catch(n){throw n instanceof u?n:new u("DB_ERROR","Failed to update thread",n)}finally{s.close();}}togglePin(t,e){let s=this.openHandle(true);try{let n=e?drizzleOrm.and(drizzleOrm.eq(l.id,t),drizzleOrm.eq(l.workspace_id,e)):drizzleOrm.eq(l.id,t),r=s.db.select({pinned:l.pinned,metadata:l.metadata}).from(l).where(n).get();if(!r)return null;let i=r.pinned?0:1,a=r.metadata?JSON.parse(r.metadata):{};if(i){let c=e?drizzleOrm.and(drizzleOrm.eq(l.pinned,1),drizzleOrm.eq(l.workspace_id,e)):drizzleOrm.eq(l.pinned,1),_=s.db.select({metadata:l.metadata}).from(l).where(c).all(),m=0;for(let h of _){let S=h.metadata?JSON.parse(h.metadata):{};typeof S.pinOrder=="number"&&S.pinOrder>m&&(m=S.pinOrder);}a.pinOrder=m+1;}else delete a.pinOrder;return s.db.update(l).set({pinned:i,metadata:Object.keys(a).length>0?JSON.stringify(a):null}).where(n).run(),{pinned:!!i}}catch(n){throw n instanceof u?n:new u("DB_ERROR","Failed to toggle pin",n)}finally{s.close();}}reorderPins(t,e){let s=this.openHandle(true);try{for(let n=0;n<t.length;n++){let r=e?drizzleOrm.and(drizzleOrm.eq(l.id,t[n]),drizzleOrm.eq(l.workspace_id,e)):drizzleOrm.eq(l.id,t[n]),i=s.db.select({metadata:l.metadata}).from(l).where(r).get();if(!i)continue;let a=i.metadata?JSON.parse(i.metadata):{};a.pinOrder=n+1,s.db.update(l).set({metadata:JSON.stringify(a)}).where(r).run();}}catch(n){throw n instanceof u?n:new u("DB_ERROR","Failed to reorder pins",n)}finally{s.close();}}toggleStar(t,e){let s=this.openHandle(true);try{let n=e?drizzleOrm.and(drizzleOrm.eq(l.id,t),drizzleOrm.eq(l.workspace_id,e)):drizzleOrm.eq(l.id,t),r=s.db.select({starred:l.starred}).from(l).where(n).get();if(!r)return null;let i=r.starred?0:1;return s.db.update(l).set({starred:i}).where(n).run(),{starred:!!i}}catch(n){throw n instanceof u?n:new u("DB_ERROR","Failed to toggle star",n)}finally{s.close();}}};function de(d){return d.replace(/<conversation_history[^>]*>[\s\S]*?<\/conversation_history>/g,"").split(`
|
|
111
111
|
`).filter(n=>!(n.startsWith("Loaded ")&&n.includes("layouts from")||n.includes("[dotenv@")||n.includes("[Nest]")&&n.includes("DEBUG")||n.startsWith("Registered custom layout:")||n.startsWith("Updated custom layout:"))).join(`
|
|
112
112
|
`).trim()}function le(d){if(!d)return "";let t=d;try{let e=JSON.parse(t);Array.isArray(e)?t=e.filter(s=>s?.type==="text"&&s?.text).map(s=>s.text).join(`
|
|
113
|
-
`):e&&typeof e=="object"&&e.result!==void 0&&(t=e.result||"");}catch{t=de(t);}return t}var Q=class{dbPath;constructor(t){this.dbPath=t??q.join(Ot.homedir(),".crewx","crewx.db");}getThreadRepo(){return new
|
|
113
|
+
`):e&&typeof e=="object"&&e.result!==void 0&&(t=e.result||"");}catch{t=de(t);}return t}var Q=class{dbPath;constructor(t){this.dbPath=t??q.join(Ot.homedir(),".crewx","crewx.db");}getThreadRepo(){return new V({dbPath:this.dbPath})}updateThread(t,e){this.getThreadRepo().updateThread(t,{title:e.title});}async ensureThread(t,e,s){let n=this.getThreadRepo(),r=n.findThreadById(t);if(r){if(r.platform!==e)throw new Error(`Thread '${t}' already exists with platform '${r.platform}' \u2014 cannot change to '${e}' (platform is immutable)`);return}n.ensureThread(t,e,s);}async fetchHistory(t,e){let s=e?.limit??100,n=this.getThreadRepo(),r=n.findThreadById(t),i=n.findTopLevelTasks(t),a=new Set(["done","completed","success"]);i=i.filter(m=>(!m.status||a.has(m.status))&&(!e?.currentTraceId||m.trace_id!==e.currentTraceId)),i=i.slice(0,s);let c=r?.platform??"cli",_=this.rowsToMessages(i);return {threadId:t,platform:c,messages:_,metadata:{title:r?.title??void 0,firstMessage:r?.first_message??void 0,lastMessage:r?.last_message??void 0,messageCount:r?.message_count??0,updatedAt:r?.updated_at?new Date(r.updated_at).getTime():void 0}}}async saveUserMessage(t,e,s,n){this.getThreadRepo().saveUserMessage(t,e);}async saveAssistantMessage(t,e,s,n){this.getThreadRepo().saveAssistantMessage(t,e);}close(){}rowsToMessages(t){let e=[];for(let s of t){s.prompt&&e.push({id:`${s.id}-user`,text:s.prompt,isAssistant:false,timestamp:new Date(s.started_at).getTime()});let n=le(s.result);n&&e.push({id:`${s.id}-assistant`,text:n,isAssistant:true,timestamp:new Date(s.started_at).getTime()});}return e}};var at=class extends O{name="conversation";_provider;unsubStart=null;unsubEnd=null;constructor(t){super(),this._provider=new Q(t?.dbPath);}get conversationProvider(){return this._provider}attach(t){this.unsubStart=t.on("task:start",async e=>{if(!e.threadId)return;let s=e.platform??"cli";try{await this._provider.ensureThread(e.threadId,s,e.workspaceId),await this._provider.saveUserMessage(e.threadId,e.message??"");}catch{}}),this.unsubEnd=t.on("task:end",async e=>{if(!e.result)return;let s=e.metadata?.threadId;if(!s)return;let n=e.agentRef?.replace(/^@/,"")??"";try{await this._provider.saveAssistantMessage(s,e.result,n);}catch{}});}detach(t){this.unsubStart?.(),this.unsubStart=null,this.unsubEnd?.(),this.unsubEnd=null,this._provider.close?.();}};exports.ConversationPlugin=at;exports.FileLoggerPlugin=Z;exports.SqliteTracingPlugin=rt;
|
package/dist/repository/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
'use strict';var Bt=require('fs'),G=require('path'),Yt=require('os'),drizzleOrm=require('drizzle-orm'),sqliteCore=require('drizzle-orm/sqlite-core'),crypto=require('crypto');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var Bt__default=/*#__PURE__*/_interopDefault(Bt);var G__namespace=/*#__PURE__*/_interopNamespace(G);var Yt__default=/*#__PURE__*/_interopDefault(Yt);var
|
|
1
|
+
'use strict';var Bt=require('fs'),G=require('path'),Yt=require('os'),drizzleOrm=require('drizzle-orm'),sqliteCore=require('drizzle-orm/sqlite-core'),crypto=require('crypto');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var Bt__default=/*#__PURE__*/_interopDefault(Bt);var G__namespace=/*#__PURE__*/_interopNamespace(G);var Yt__default=/*#__PURE__*/_interopDefault(Yt);var J=(d=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(d,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):d)(function(d){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+d+'" is not supported')});var R=class{resolveDbPath(){return process.env.CREWX_DB?process.env.CREWX_DB:process.env.CREWX_TRACES_DB?process.env.CREWX_TRACES_DB:G.join(Yt__default.default.homedir(),".crewx","crewx.db")}resolveDbPaths(){return [this.resolveDbPath()]}isMissingTableError(t){return t instanceof Error&&/no such table:/i.test(t.message)}dbExists(t){return Bt.existsSync(t??this.resolveDbPath())}};var a=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 g(d){let t=J("better-sqlite3"),{drizzle:e}=J("drizzle-orm/better-sqlite3"),n=new t(d);return n.exec("PRAGMA journal_mode = WAL"),n.exec("PRAGMA busy_timeout = 5000"),n.exec("PRAGMA foreign_keys = ON"),{db:e(n),runRaw:(r,s=[])=>n.prepare(r).run(...s),close:()=>n.close()}}var xt=new Set,Kt={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 Vt(d,t){return (d.get(`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='${t}'`)?.cnt??0)>0}function Jt(d,t){if(t>0||!Vt(d,"tasks"))return;let e=d.all("PRAGMA table_info(tasks)"),n=new Set(e.map(r=>r.name));for(let[r,s]of Object.entries(Kt))n.has(r)||d.run(`ALTER TABLE tasks ADD COLUMN ${r} ${s}`);}function j(d){let{migrate:t}=J("drizzle-orm/better-sqlite3/migrator"),{sql:e}=J("drizzle-orm"),n=[G__namespace.default.join(__dirname,"../migrations"),G__namespace.default.join(__dirname,"migrations"),G__namespace.default.join(__dirname,"../../../../drizzle/migrations"),G__namespace.default.join(process.cwd(),"drizzle/migrations")],r=n.find(f=>Bt.existsSync(G__namespace.default.join(f,"meta/_journal.json")));if(!r)throw new Error(`migrations folder not found. Searched:
|
|
2
2
|
${n.join(`
|
|
3
|
-
`)}`);let s=d.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),i=0;s?.cnt&&(i=d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),Qt(d,i),t(d,{migrationsFolder:r});let c=(d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0)-i;if(c>0){let h=s?.cnt?"Database migrated":"Database initialized";console.log(`[crewx] ${h} (${c} migration${c>1?"s":""} applied).`);}}function T(d,t){xt.has(t)||(j(d),xt.add(t));}var p=sqliteCore.sqliteTable("workspaces",{id:sqliteCore.text("id").primaryKey(),slug:sqliteCore.text("slug").notNull().unique(),name:sqliteCore.text("name").notNull(),workspace_path:sqliteCore.text("workspace_path"),description:sqliteCore.text("description"),is_active:sqliteCore.integer("is_active").notNull().default(1),created_at:sqliteCore.text("created_at").notNull(),updated_at:sqliteCore.text("updated_at").notNull()});var o=sqliteCore.sqliteTable("tasks",{id:sqliteCore.text("id").primaryKey(),agent_id:sqliteCore.text("agent_id").notNull(),user_id:sqliteCore.text("user_id"),prompt:sqliteCore.text("prompt").notNull(),mode:sqliteCore.text("mode").notNull().default("execute"),status:sqliteCore.text("status").notNull().default("running"),result:sqliteCore.text("result"),error:sqliteCore.text("error"),started_at:sqliteCore.text("started_at").notNull(),completed_at:sqliteCore.text("completed_at"),duration_ms:sqliteCore.integer("duration_ms"),metadata:sqliteCore.text("metadata"),workspace_id:sqliteCore.text("workspace_id"),trace_id:sqliteCore.text("trace_id"),parent_task_id:sqliteCore.text("parent_task_id"),caller_agent_id:sqliteCore.text("caller_agent_id"),model:sqliteCore.text("model"),platform:sqliteCore.text("platform").default("cli"),crewx_version:sqliteCore.text("crewx_version"),input_tokens:sqliteCore.integer("input_tokens").default(0),output_tokens:sqliteCore.integer("output_tokens").default(0),cost_usd:sqliteCore.real("cost_usd").default(0),pid:sqliteCore.integer("pid"),rendered_prompt:sqliteCore.text("rendered_prompt"),command:sqliteCore.text("command"),coding_agent_command:sqliteCore.text("coding_agent_command"),exit_code:sqliteCore.integer("exit_code"),logs:sqliteCore.text("logs"),thread_id:sqliteCore.text("thread_id"),workspace_ref:sqliteCore.text("workspace_ref"),project_id:sqliteCore.text("project_id"),project_ref:sqliteCore.text("project_ref"),cached_input_tokens:sqliteCore.integer("cached_input_tokens").default(0)},d=>({idx_tasks_agent_id:sqliteCore.index("idx_tasks_agent_id").on(d.agent_id),idx_tasks_status:sqliteCore.index("idx_tasks_status").on(d.status),idx_tasks_started_at:sqliteCore.index("idx_tasks_started_at").on(d.started_at),idx_tasks_trace_id:sqliteCore.index("idx_tasks_trace_id").on(d.trace_id),idx_tasks_parent_task_id:sqliteCore.index("idx_tasks_parent_task_id").on(d.parent_task_id),idx_tasks_crewx_version:sqliteCore.index("idx_tasks_crewx_version").on(d.crewx_version),idx_tasks_pid:sqliteCore.index("idx_tasks_pid").on(d.pid),idx_tasks_thread_id:sqliteCore.index("idx_tasks_thread_id").on(d.thread_id),idx_tasks_workspace_id:sqliteCore.index("idx_tasks_workspace_id").on(d.workspace_id),idx_tasks_workspace_ref:sqliteCore.index("idx_tasks_workspace_ref").on(d.workspace_ref),idx_tasks_project_id:sqliteCore.index("idx_tasks_project_id").on(d.project_id),idx_tasks_ws_started:sqliteCore.index("idx_tasks_ws_started").on(d.workspace_id,d.started_at)}));var u=sqliteCore.sqliteTable("threads",{id:sqliteCore.text("id").primaryKey(),workspace_id:sqliteCore.text("workspace_id").references(()=>p.id,{onDelete:"set null"}),platform:sqliteCore.text("platform").notNull().default("cli"),title:sqliteCore.text("title"),first_message:sqliteCore.text("first_message"),last_message:sqliteCore.text("last_message"),message_count:sqliteCore.integer("message_count").notNull().default(0),created_at:sqliteCore.text("created_at").notNull(),updated_at:sqliteCore.text("updated_at").notNull(),metadata:sqliteCore.text("metadata"),title_locked:sqliteCore.integer("title_locked").notNull().default(0),pinned:sqliteCore.integer("pinned").notNull().default(0),starred:sqliteCore.integer("starred").notNull().default(0)},d=>({idx_threads_updated_at:sqliteCore.index("idx_threads_updated_at").on(d.updated_at),idx_threads_workspace_id:sqliteCore.index("idx_threads_workspace_id").on(d.workspace_id)}));var q=sqliteCore.sqliteTable("spans",{id:sqliteCore.text("id").primaryKey(),task_id:sqliteCore.text("task_id").references(()=>o.id,{onDelete:"set null"}),parent_span_id:sqliteCore.text("parent_span_id").references(()=>q.id,{onDelete:"set null"}),name:sqliteCore.text("name").notNull(),kind:sqliteCore.text("kind").notNull().default("internal"),status:sqliteCore.text("status").notNull().default("ok"),started_at:sqliteCore.text("started_at").notNull(),completed_at:sqliteCore.text("completed_at"),duration_ms:sqliteCore.integer("duration_ms"),input:sqliteCore.text("input"),output:sqliteCore.text("output"),error:sqliteCore.text("error"),attributes:sqliteCore.text("attributes")},d=>({idx_spans_task_id:sqliteCore.index("idx_spans_task_id").on(d.task_id),idx_spans_parent_span_id:sqliteCore.index("idx_spans_parent_span_id").on(d.parent_span_id)}));var B=sqliteCore.sqliteTable("tool_calls",{id:sqliteCore.text("id").primaryKey(),task_id:sqliteCore.text("task_id").references(()=>o.id,{onDelete:"cascade"}),session_id:sqliteCore.text("session_id"),tool_name:sqliteCore.text("tool_name").notNull(),files:sqliteCore.text("files"),input:sqliteCore.text("input"),output:sqliteCore.text("output"),duration_ms:sqliteCore.integer("duration_ms"),timestamp:sqliteCore.text("timestamp").notNull()},d=>({idx_tool_calls_task_id:sqliteCore.index("idx_tool_calls_task_id").on(d.task_id),idx_tool_calls_tool_name:sqliteCore.index("idx_tool_calls_tool_name").on(d.tool_name),idx_tool_calls_timestamp:sqliteCore.index("idx_tool_calls_timestamp").on(d.timestamp)}));var A=sqliteCore.sqliteTable("thread_boxes",{id:sqliteCore.text("id").primaryKey(),thread_id:sqliteCore.text("thread_id").notNull().references(()=>u.id,{onDelete:"cascade"}),seq:sqliteCore.integer("seq").notNull(),first_task_id:sqliteCore.text("first_task_id").notNull(),mid_task_id:sqliteCore.text("mid_task_id").notNull(),last_task_id:sqliteCore.text("last_task_id").notNull(),task_count:sqliteCore.integer("task_count").notNull(),summary:sqliteCore.text("summary"),source_tokens:sqliteCore.integer("source_tokens").notNull(),summary_tokens:sqliteCore.integer("summary_tokens"),created_at:sqliteCore.text("created_at").notNull()},d=>({idx_thread_boxes_thread_id:sqliteCore.index("idx_thread_boxes_thread_id").on(d.thread_id),idx_thread_boxes_seq:sqliteCore.index("idx_thread_boxes_seq").on(d.thread_id,d.seq),uniq_thread_boxes_thread_seq:sqliteCore.unique().on(d.thread_id,d.seq)}));var X=sqliteCore.sqliteTable("request_logs",{id:sqliteCore.text("id").primaryKey(),path:sqliteCore.text("path").notNull(),method:sqliteCore.text("method").notNull(),status_code:sqliteCore.integer("status_code").notNull(),duration_ms:sqliteCore.integer("duration_ms").notNull(),ip:sqliteCore.text("ip"),request_headers:sqliteCore.text("request_headers"),response_headers:sqliteCore.text("response_headers"),request_body:sqliteCore.text("request_body"),response_body:sqliteCore.text("response_body"),query:sqliteCore.text("query"),user_id:sqliteCore.text("user_id"),project_id:sqliteCore.text("project_id"),partition_key:sqliteCore.text("partition_key").notNull(),timestamp:sqliteCore.text("timestamp").notNull().default(drizzleOrm.sql`(datetime('now'))`),metadata:sqliteCore.text("metadata")},d=>({idx_request_logs_timestamp:sqliteCore.index("idx_request_logs_timestamp").on(d.timestamp),idx_request_logs_path:sqliteCore.index("idx_request_logs_path").on(d.path),idx_request_logs_status_code:sqliteCore.index("idx_request_logs_status_code").on(d.status_code),idx_request_logs_partition_key:sqliteCore.index("idx_request_logs_partition_key").on(d.partition_key)}));function ct(d){let t=G__namespace.resolve(d);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 Pt(d){let t=ct(d);return crypto.createHash("sha256").update(t).digest("hex")}function et(d){return d.toLowerCase().replace(/[^a-z0-9-]/g,"-").replace(/-{2,}/g,"-").replace(/^-+|-+$/g,"")}var ht=class extends y{dbRoot;constructor(t={}){super(),this.dbRoot=t.dbRoot;}resolveDbPath(){return this.dbRoot?G.join(this.dbRoot,".crewx","crewx.db"):super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let r=G.dirname(e);Bt.existsSync(r)||Bt.mkdirSync(r,{recursive:true});}else if(!Bt.existsSync(e))throw new a("NOT_FOUND","Database not found");let n=g(e);if(t)try{j(n.db);}catch(r){throw n.close(),r}return n}resolveSlug(t,e,n){let r=et(G.basename(n)),i=`${et(G.basename(G.dirname(n)))}-${r}`,l=[r,i];try{let c=h=>t.select({id:p.id}).from(p).where(drizzleOrm.and(drizzleOrm.eq(p.slug,h),drizzleOrm.ne(p.id,e))).limit(1).all().length>0;for(let h of l)if(!c(h))return h;for(let h=2;h<1e3;h+=1){let f=`${i}-${h}`;if(!c(f))return f}}catch{}return r}normalizeLegacySlugs(){if(!this.dbExists())return {updated:0,checked:0};let t=this.openHandle(true);try{let e=t.db.select({id:p.id,slug:p.slug}).from(p).all(),n=0;for(let r of e)if(r.slug.includes("/")){let s=et(r.slug.replace(/\//g,"-"));t.db.update(p).set({slug:s,updated_at:new Date().toISOString()}).where(drizzleOrm.eq(p.id,r.id)).run(),n+=1;}return {updated:n,checked:e.length}}catch(e){throw new a("DB_ERROR","Failed to normalize legacy slugs",e)}finally{t.close();}}ensureRow(t,e){let{id:n,slug:r,name:s,workspacePath:i}=e,l=new Date().toISOString();t.insert(p).values({id:n,slug:r,name:s,workspace_path:i,is_active:1,created_at:l,updated_at:l}).onConflictDoNothing().run(),t.update(p).set({workspace_path:i,updated_at:l}).where(drizzleOrm.and(drizzleOrm.eq(p.id,n),drizzleOrm.isNull(p.workspace_path))).run();}registerWorkspace(t){let e=ct(t),n=this.openHandle(true);try{let r=Pt(e),s=G.basename(e),i=this.resolveSlug(n.db,r,e);return this.ensureRow(n.db,{id:r,slug:i,name:s,workspacePath:e}),{id:r,slug:i}}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to register workspace",r)}finally{n.close();}}listProjects(t){if(!this.dbExists())return {rows:[],total:0};let e=this.openHandle(false);try{let n=t.isActive!==void 0?drizzleOrm.eq(p.is_active,t.isActive?1:0):void 0,r=e.db.select({count:drizzleOrm.sql`count(*)`}).from(p).where(n).get();return {rows:e.db.select().from(p).where(n).orderBy(drizzleOrm.desc(p.updated_at)).limit(t.limit).offset(t.offset).all(),total:r?.count??0}}catch(n){throw new a("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(p).where(drizzleOrm.eq(p.id,t)).limit(1).get()??void 0}catch(n){throw new a("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(drizzleOrm.sql`SELECT DISTINCT agent_id FROM tasks WHERE workspace_id = ${t} AND agent_id IS NOT NULL ORDER BY agent_id`).map(r=>r.agent_id)}catch(n){throw new a("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 r=n.db.get(drizzleOrm.sql`SELECT COUNT(*) as count FROM threads WHERE workspace_id = ${t}`);return {rows:n.db.all(drizzleOrm.sql`SELECT t.*,
|
|
3
|
+
`)}`);let s=d.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),i=0;s?.cnt&&(i=d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),Jt(d,i),t(d,{migrationsFolder:r});let u=(d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0)-i;if(u>0){let f=s?.cnt?"Database migrated":"Database initialized";console.log(`[crewx] ${f} (${u} migration${u>1?"s":""} applied).`);}}function T(d,t){xt.has(t)||(j(d),xt.add(t));}var p=sqliteCore.sqliteTable("workspaces",{id:sqliteCore.text("id").primaryKey(),slug:sqliteCore.text("slug").notNull().unique(),name:sqliteCore.text("name").notNull(),workspace_path:sqliteCore.text("workspace_path"),description:sqliteCore.text("description"),is_active:sqliteCore.integer("is_active").notNull().default(1),created_at:sqliteCore.text("created_at").notNull(),updated_at:sqliteCore.text("updated_at").notNull()});var o=sqliteCore.sqliteTable("tasks",{id:sqliteCore.text("id").primaryKey(),agent_id:sqliteCore.text("agent_id").notNull(),user_id:sqliteCore.text("user_id"),prompt:sqliteCore.text("prompt").notNull(),mode:sqliteCore.text("mode").notNull().default("execute"),status:sqliteCore.text("status").notNull().default("running"),result:sqliteCore.text("result"),error:sqliteCore.text("error"),started_at:sqliteCore.text("started_at").notNull(),completed_at:sqliteCore.text("completed_at"),duration_ms:sqliteCore.integer("duration_ms"),metadata:sqliteCore.text("metadata"),workspace_id:sqliteCore.text("workspace_id"),trace_id:sqliteCore.text("trace_id"),parent_task_id:sqliteCore.text("parent_task_id"),caller_agent_id:sqliteCore.text("caller_agent_id"),model:sqliteCore.text("model"),platform:sqliteCore.text("platform").default("cli"),crewx_version:sqliteCore.text("crewx_version"),input_tokens:sqliteCore.integer("input_tokens").default(0),output_tokens:sqliteCore.integer("output_tokens").default(0),cost_usd:sqliteCore.real("cost_usd").default(0),pid:sqliteCore.integer("pid"),rendered_prompt:sqliteCore.text("rendered_prompt"),command:sqliteCore.text("command"),coding_agent_command:sqliteCore.text("coding_agent_command"),exit_code:sqliteCore.integer("exit_code"),logs:sqliteCore.text("logs"),thread_id:sqliteCore.text("thread_id"),workspace_ref:sqliteCore.text("workspace_ref"),project_id:sqliteCore.text("project_id"),project_ref:sqliteCore.text("project_ref"),cached_input_tokens:sqliteCore.integer("cached_input_tokens").default(0)},d=>({idx_tasks_agent_id:sqliteCore.index("idx_tasks_agent_id").on(d.agent_id),idx_tasks_status:sqliteCore.index("idx_tasks_status").on(d.status),idx_tasks_started_at:sqliteCore.index("idx_tasks_started_at").on(d.started_at),idx_tasks_trace_id:sqliteCore.index("idx_tasks_trace_id").on(d.trace_id),idx_tasks_parent_task_id:sqliteCore.index("idx_tasks_parent_task_id").on(d.parent_task_id),idx_tasks_crewx_version:sqliteCore.index("idx_tasks_crewx_version").on(d.crewx_version),idx_tasks_pid:sqliteCore.index("idx_tasks_pid").on(d.pid),idx_tasks_thread_id:sqliteCore.index("idx_tasks_thread_id").on(d.thread_id),idx_tasks_workspace_id:sqliteCore.index("idx_tasks_workspace_id").on(d.workspace_id),idx_tasks_workspace_ref:sqliteCore.index("idx_tasks_workspace_ref").on(d.workspace_ref),idx_tasks_project_id:sqliteCore.index("idx_tasks_project_id").on(d.project_id),idx_tasks_ws_started:sqliteCore.index("idx_tasks_ws_started").on(d.workspace_id,d.started_at)}));var c=sqliteCore.sqliteTable("threads",{id:sqliteCore.text("id").primaryKey(),workspace_id:sqliteCore.text("workspace_id").references(()=>p.id,{onDelete:"set null"}),platform:sqliteCore.text("platform").notNull().default("cli"),title:sqliteCore.text("title"),first_message:sqliteCore.text("first_message"),last_message:sqliteCore.text("last_message"),message_count:sqliteCore.integer("message_count").notNull().default(0),created_at:sqliteCore.text("created_at").notNull(),updated_at:sqliteCore.text("updated_at").notNull(),metadata:sqliteCore.text("metadata"),title_locked:sqliteCore.integer("title_locked").notNull().default(0),pinned:sqliteCore.integer("pinned").notNull().default(0),starred:sqliteCore.integer("starred").notNull().default(0)},d=>({idx_threads_updated_at:sqliteCore.index("idx_threads_updated_at").on(d.updated_at),idx_threads_workspace_id:sqliteCore.index("idx_threads_workspace_id").on(d.workspace_id)}));var q=sqliteCore.sqliteTable("spans",{id:sqliteCore.text("id").primaryKey(),task_id:sqliteCore.text("task_id").references(()=>o.id,{onDelete:"set null"}),parent_span_id:sqliteCore.text("parent_span_id").references(()=>q.id,{onDelete:"set null"}),name:sqliteCore.text("name").notNull(),kind:sqliteCore.text("kind").notNull().default("internal"),status:sqliteCore.text("status").notNull().default("ok"),started_at:sqliteCore.text("started_at").notNull(),completed_at:sqliteCore.text("completed_at"),duration_ms:sqliteCore.integer("duration_ms"),input:sqliteCore.text("input"),output:sqliteCore.text("output"),error:sqliteCore.text("error"),attributes:sqliteCore.text("attributes")},d=>({idx_spans_task_id:sqliteCore.index("idx_spans_task_id").on(d.task_id),idx_spans_parent_span_id:sqliteCore.index("idx_spans_parent_span_id").on(d.parent_span_id)}));var B=sqliteCore.sqliteTable("tool_calls",{id:sqliteCore.text("id").primaryKey(),task_id:sqliteCore.text("task_id").references(()=>o.id,{onDelete:"cascade"}),session_id:sqliteCore.text("session_id"),tool_name:sqliteCore.text("tool_name").notNull(),files:sqliteCore.text("files"),input:sqliteCore.text("input"),output:sqliteCore.text("output"),duration_ms:sqliteCore.integer("duration_ms"),timestamp:sqliteCore.text("timestamp").notNull()},d=>({idx_tool_calls_task_id:sqliteCore.index("idx_tool_calls_task_id").on(d.task_id),idx_tool_calls_tool_name:sqliteCore.index("idx_tool_calls_tool_name").on(d.tool_name),idx_tool_calls_timestamp:sqliteCore.index("idx_tool_calls_timestamp").on(d.timestamp)}));var N=sqliteCore.sqliteTable("thread_boxes",{id:sqliteCore.text("id").primaryKey(),thread_id:sqliteCore.text("thread_id").notNull().references(()=>c.id,{onDelete:"cascade"}),seq:sqliteCore.integer("seq").notNull(),first_task_id:sqliteCore.text("first_task_id").notNull(),mid_task_id:sqliteCore.text("mid_task_id").notNull(),last_task_id:sqliteCore.text("last_task_id").notNull(),task_count:sqliteCore.integer("task_count").notNull(),summary:sqliteCore.text("summary"),source_tokens:sqliteCore.integer("source_tokens").notNull(),summary_tokens:sqliteCore.integer("summary_tokens"),created_at:sqliteCore.text("created_at").notNull()},d=>({idx_thread_boxes_thread_id:sqliteCore.index("idx_thread_boxes_thread_id").on(d.thread_id),idx_thread_boxes_seq:sqliteCore.index("idx_thread_boxes_seq").on(d.thread_id,d.seq),uniq_thread_boxes_thread_seq:sqliteCore.unique().on(d.thread_id,d.seq)}));var X=sqliteCore.sqliteTable("request_logs",{id:sqliteCore.text("id").primaryKey(),path:sqliteCore.text("path").notNull(),method:sqliteCore.text("method").notNull(),status_code:sqliteCore.integer("status_code").notNull(),duration_ms:sqliteCore.integer("duration_ms").notNull(),ip:sqliteCore.text("ip"),request_headers:sqliteCore.text("request_headers"),response_headers:sqliteCore.text("response_headers"),request_body:sqliteCore.text("request_body"),response_body:sqliteCore.text("response_body"),query:sqliteCore.text("query"),user_id:sqliteCore.text("user_id"),project_id:sqliteCore.text("project_id"),partition_key:sqliteCore.text("partition_key").notNull(),timestamp:sqliteCore.text("timestamp").notNull().default(drizzleOrm.sql`(datetime('now'))`),metadata:sqliteCore.text("metadata")},d=>({idx_request_logs_timestamp:sqliteCore.index("idx_request_logs_timestamp").on(d.timestamp),idx_request_logs_path:sqliteCore.index("idx_request_logs_path").on(d.path),idx_request_logs_status_code:sqliteCore.index("idx_request_logs_status_code").on(d.status_code),idx_request_logs_partition_key:sqliteCore.index("idx_request_logs_partition_key").on(d.partition_key)}));function ct(d){let t=G__namespace.resolve(d);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 Pt(d){let t=ct(d);return crypto.createHash("sha256").update(t).digest("hex")}function et(d){return d.toLowerCase().replace(/[^a-z0-9-]/g,"-").replace(/-{2,}/g,"-").replace(/^-+|-+$/g,"")}var ft=class extends R{dbRoot;constructor(t={}){super(),this.dbRoot=t.dbRoot;}resolveDbPath(){return this.dbRoot?G.join(this.dbRoot,".crewx","crewx.db"):super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let r=G.dirname(e);Bt.existsSync(r)||Bt.mkdirSync(r,{recursive:true});}else if(!Bt.existsSync(e))throw new a("NOT_FOUND","Database not found");let n=g(e);if(t)try{j(n.db);}catch(r){throw n.close(),r}return n}resolveSlug(t,e,n){let r=et(G.basename(n)),i=`${et(G.basename(G.dirname(n)))}-${r}`,l=[r,i];try{let u=f=>t.select({id:p.id}).from(p).where(drizzleOrm.and(drizzleOrm.eq(p.slug,f),drizzleOrm.ne(p.id,e))).limit(1).all().length>0;for(let f of l)if(!u(f))return f;for(let f=2;f<1e3;f+=1){let _=`${i}-${f}`;if(!u(_))return _}}catch{}return r}normalizeLegacySlugs(){if(!this.dbExists())return {updated:0,checked:0};let t=this.openHandle(true);try{let e=t.db.select({id:p.id,slug:p.slug}).from(p).all(),n=0;for(let r of e)if(r.slug.includes("/")){let s=et(r.slug.replace(/\//g,"-"));t.db.update(p).set({slug:s,updated_at:new Date().toISOString()}).where(drizzleOrm.eq(p.id,r.id)).run(),n+=1;}return {updated:n,checked:e.length}}catch(e){throw new a("DB_ERROR","Failed to normalize legacy slugs",e)}finally{t.close();}}ensureRow(t,e){let{id:n,slug:r,name:s,workspacePath:i}=e,l=new Date().toISOString();t.insert(p).values({id:n,slug:r,name:s,workspace_path:i,is_active:1,created_at:l,updated_at:l}).onConflictDoNothing().run(),t.update(p).set({workspace_path:i,updated_at:l}).where(drizzleOrm.and(drizzleOrm.eq(p.id,n),drizzleOrm.isNull(p.workspace_path))).run();}registerWorkspace(t){let e=ct(t),n=this.openHandle(true);try{let r=Pt(e),s=G.basename(e),i=this.resolveSlug(n.db,r,e);return this.ensureRow(n.db,{id:r,slug:i,name:s,workspacePath:e}),{id:r,slug:i}}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to register workspace",r)}finally{n.close();}}listProjects(t){if(!this.dbExists())return {rows:[],total:0};let e=this.openHandle(false);try{let n=t.isActive!==void 0?drizzleOrm.eq(p.is_active,t.isActive?1:0):void 0,r=e.db.select({count:drizzleOrm.sql`count(*)`}).from(p).where(n).get();return {rows:e.db.select().from(p).where(n).orderBy(drizzleOrm.desc(p.updated_at)).limit(t.limit).offset(t.offset).all(),total:r?.count??0}}catch(n){throw new a("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(p).where(drizzleOrm.eq(p.id,t)).limit(1).get()??void 0}catch(n){throw new a("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(drizzleOrm.sql`SELECT DISTINCT agent_id FROM tasks WHERE workspace_id = ${t} AND agent_id IS NOT NULL ORDER BY agent_id`).map(r=>r.agent_id)}catch(n){throw new a("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 r=n.db.get(drizzleOrm.sql`SELECT COUNT(*) as count FROM threads WHERE workspace_id = ${t}`);return {rows:n.db.all(drizzleOrm.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:r?.count??0}}catch(r){throw new a("DB_ERROR","Failed to find threads by project",r)}finally{n.close();}}findBySlug(t){if(!this.dbExists())return;let e=this.openHandle(false);try{return e.db.select().from(p).where(drizzleOrm.eq(p.slug,t)).limit(1).get()??void 0}catch(n){throw new a("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 r=e?drizzleOrm.and(drizzleOrm.eq(p.slug,t),drizzleOrm.ne(p.id,e)):drizzleOrm.eq(p.slug,t);return !!n.db.select({id:p.id}).from(p).where(r).limit(1).get()}catch(r){throw new a("DB_ERROR","Failed to check slug",r)}finally{n.close();}}insert(t,e,n,r){let s=this.openHandle(true);try{let i=new Date().toISOString();s.db.insert(p).values({id:t,slug:e,name:n,workspace_path:r,is_active:1,created_at:i,updated_at:i}).run();let l=s.db.select().from(p).where(drizzleOrm.eq(p.id,t)).limit(1).get();if(!l)throw new a("DB_ERROR","Insert did not return a row");return l}catch(i){throw i instanceof a?i:new a("DB_ERROR","Failed to insert workspace",i)}finally{s.close();}}update(t,e,n){let r=this.openHandle(true);try{r.runRaw(`UPDATE workspaces SET ${e.join(", ")} WHERE id = ?`,n);let s=r.db.select().from(p).where(drizzleOrm.eq(p.id,t)).limit(1).get();if(!s)throw new a("NOT_FOUND",`Workspace ${t} not found`);return s}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to update workspace",s)}finally{r.close();}}cleanupOrphanWorkspaces(){if(!this.dbExists())return {softDeleted:0,checked:0};let t=this.openHandle(true);try{let e=t.db.select({id:p.id,workspace_path:p.workspace_path}).from(p).where(drizzleOrm.and(drizzleOrm.eq(p.is_active,1),drizzleOrm.isNotNull(p.workspace_path))).all(),n=0;for(let r of e){let s=r.workspace_path;Bt.existsSync(G.join(s,"crewx.yaml"))||Bt.existsSync(G.join(s,"crewx.yml"))||(t.db.update(p).set({is_active:0,updated_at:new Date().toISOString()}).where(drizzleOrm.eq(p.id,r.id)).run(),n+=1);}return {softDeleted:n,checked:e.length}}catch(e){throw new a("DB_ERROR","Failed to cleanup orphan workspaces",e)}finally{t.close();}}delete(t){let e=this.openHandle(true);try{e.db.run(drizzleOrm.sql`UPDATE threads SET workspace_id = NULL WHERE workspace_id = ${t}`),e.db.delete(p).where(drizzleOrm.eq(p.id,t)).run();}catch(n){throw n instanceof a?n:new a("DB_ERROR","Failed to delete workspace",n)}finally{e.close();}}};var zt=[p,o,
|
|
8
|
+
LIMIT ${e.limit} OFFSET ${e.offset}`),total:r?.count??0}}catch(r){throw new a("DB_ERROR","Failed to find threads by project",r)}finally{n.close();}}findBySlug(t){if(!this.dbExists())return;let e=this.openHandle(false);try{return e.db.select().from(p).where(drizzleOrm.eq(p.slug,t)).limit(1).get()??void 0}catch(n){throw new a("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 r=e?drizzleOrm.and(drizzleOrm.eq(p.slug,t),drizzleOrm.ne(p.id,e)):drizzleOrm.eq(p.slug,t);return !!n.db.select({id:p.id}).from(p).where(r).limit(1).get()}catch(r){throw new a("DB_ERROR","Failed to check slug",r)}finally{n.close();}}insert(t,e,n,r){let s=this.openHandle(true);try{let i=new Date().toISOString();s.db.insert(p).values({id:t,slug:e,name:n,workspace_path:r,is_active:1,created_at:i,updated_at:i}).run();let l=s.db.select().from(p).where(drizzleOrm.eq(p.id,t)).limit(1).get();if(!l)throw new a("DB_ERROR","Insert did not return a row");return l}catch(i){throw i instanceof a?i:new a("DB_ERROR","Failed to insert workspace",i)}finally{s.close();}}update(t,e,n){let r=this.openHandle(true);try{r.runRaw(`UPDATE workspaces SET ${e.join(", ")} WHERE id = ?`,n);let s=r.db.select().from(p).where(drizzleOrm.eq(p.id,t)).limit(1).get();if(!s)throw new a("NOT_FOUND",`Workspace ${t} not found`);return s}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to update workspace",s)}finally{r.close();}}cleanupOrphanWorkspaces(){if(!this.dbExists())return {softDeleted:0,checked:0};let t=this.openHandle(true);try{let e=t.db.select({id:p.id,workspace_path:p.workspace_path}).from(p).where(drizzleOrm.and(drizzleOrm.eq(p.is_active,1),drizzleOrm.isNotNull(p.workspace_path))).all(),n=0;for(let r of e){let s=r.workspace_path;Bt.existsSync(G.join(s,"crewx.yaml"))||Bt.existsSync(G.join(s,"crewx.yml"))||(t.db.update(p).set({is_active:0,updated_at:new Date().toISOString()}).where(drizzleOrm.eq(p.id,r.id)).run(),n+=1);}return {softDeleted:n,checked:e.length}}catch(e){throw new a("DB_ERROR","Failed to cleanup orphan workspaces",e)}finally{t.close();}}delete(t){let e=this.openHandle(true);try{e.db.run(drizzleOrm.sql`UPDATE threads SET workspace_id = NULL WHERE workspace_id = ${t}`),e.db.delete(p).where(drizzleOrm.eq(p.id,t)).run();}catch(n){throw n instanceof a?n:new a("DB_ERROR","Failed to delete workspace",n)}finally{e.close();}}};var zt=[p,o,c,q,B,N,X];function ke(){return G__namespace.default.join(Yt__default.default.homedir(),".crewx","crewx.db")}function be(d){if(!Bt__default.default.existsSync(d))return null;let t=Math.floor(Date.now()/1e3),e=`${d}.bak-${t}`;return Bt__default.default.copyFileSync(d,e),e}function Lt(d){let t=d.all("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'");return new Set(t.map(e=>e.name))}function ye(d){return d==null||typeof d=="object"&&"queryChunks"in d?null:typeof d=="number"?String(d):typeof d=="boolean"?d?"1":"0":typeof d=="string"?`'${d.replace(/'/g,"''")}'`:String(d)}function Re(d){let t=d.getSQLType(),e=`"${d.name}" ${t}`,n=ye(d.default);return n!==null&&(e+=` DEFAULT ${n}`),d.notNull&&(e+=" NOT NULL"),e}function Ee(d,t){let e=t?.dbPath??ke(),n=t?.force??false,r=t?.dryRun??false,s=r?null:be(e);if(n&&!r)try{d.run("DELETE FROM __drizzle_migrations");}catch{}let i=Lt(d);r||j(d);let l,u;r?(l=zt.map(y=>sqliteCore.getTableConfig(y).name).filter(y=>!i.has(y)),u=i):(u=Lt(d),l=[...u].filter(w=>!i.has(w)));let f=[],_=[];for(let w of zt){let y=sqliteCore.getTableConfig(w),S=y.name;if(!u.has(S))continue;let z=d.all(`PRAGMA table_info("${S}")`),at=new Set(z.map(A=>A.name)),dt=new Set(y.columns.map(A=>A.name));for(let A of y.columns)if(!at.has(A.name)){if(!r){let jt=Re(A);d.run(`ALTER TABLE "${S}" ADD COLUMN ${jt}`);}f.push({table:S,column:A.name});}for(let A of z)dt.has(A.name)||_.push(`${S}.${A.name} exists in DB but not in schema (untouched)`);}return {created:l,altered:f,warnings:_,backupPath:s}}var mt=class extends R{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=G.join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let r=G.dirname(e);Bt.existsSync(r)||Bt.mkdirSync(r,{recursive:true});}else if(!Bt.existsSync(e))throw new a("NOT_FOUND","Database not found");let n=g(e);if(t)try{T(n.db,e);}catch(r){throw n.close(),r}return n}startTask(t){let e=this.openHandle(true);try{e.db.insert(o).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 a?n:new a("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=?`,[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 a?n:new a("DB_ERROR","Failed to finish task",n)}finally{e.close();}}appendLog(t,e){let n=this.openHandle(true);try{n.db.transaction(r=>{let s=r.select({logs:o.logs}).from(o).where(drizzleOrm.eq(o.id,t)).limit(1).get(),i=s?.logs?JSON.parse(s.logs):[];i.push(e),r.update(o).set({logs:JSON.stringify(i)}).where(drizzleOrm.eq(o.id,t)).run();},{behavior:"immediate"});}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to append log",r)}finally{n.close();}}getRunningTasks(){if(!this.dbExists())return [];let t=this.openHandle(false);try{return t.db.select().from(o).where(drizzleOrm.eq(o.status,"running")).orderBy(drizzleOrm.desc(o.started_at)).all()}catch(e){throw new a("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(o).orderBy(drizzleOrm.desc(o.started_at)).limit(100).all()}catch(e){throw new a("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(o).where(drizzleOrm.eq(o.id,t)).limit(1).get()??void 0}catch(n){throw new a("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:o.id,status:o.status,pid:o.pid}).from(o).where(drizzleOrm.eq(o.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(o).set({status:"failed",error:"Killed by user",completed_at:new Date().toISOString()}).where(drizzleOrm.and(drizzleOrm.eq(o.id,t),drizzleOrm.eq(o.status,"running"))).run(),{killed:!0,pid:n.pid??void 0}}catch(n){throw n instanceof a?n:new a("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:o.id,pid:o.pid}).from(o).where(drizzleOrm.eq(o.status,"running")).all(),n=0;for(let r of e){let s=!1;if(r.pid)try{process.kill(r.pid,0),s=!0;}catch{}s||(t.db.update(o).set({status:"failed",error:"Reaped: process not found (orphaned task)",completed_at:new Date().toISOString()}).where(drizzleOrm.and(drizzleOrm.eq(o.id,r.id),drizzleOrm.eq(o.status,"running"))).run(),n++);}return n}finally{t.close();}}findTaskStatus(t,e){let n=this.resolveDbPaths();for(let r of n){if(!Bt.existsSync(r))continue;let s=g(r);try{let i=e?drizzleOrm.eq(o.workspace_id,e):void 0,l=i?drizzleOrm.and(drizzleOrm.eq(o.id,t),i):drizzleOrm.eq(o.id,t),
|
|
10
|
+
model=COALESCE(?, model) WHERE id=?`,[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 a?n:new a("DB_ERROR","Failed to finish task",n)}finally{e.close();}}appendLog(t,e){let n=this.openHandle(true);try{n.db.transaction(r=>{let s=r.select({logs:o.logs}).from(o).where(drizzleOrm.eq(o.id,t)).limit(1).get(),i=s?.logs?JSON.parse(s.logs):[];i.push(e),r.update(o).set({logs:JSON.stringify(i)}).where(drizzleOrm.eq(o.id,t)).run();},{behavior:"immediate"});}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to append log",r)}finally{n.close();}}getRunningTasks(){if(!this.dbExists())return [];let t=this.openHandle(false);try{return t.db.select().from(o).where(drizzleOrm.eq(o.status,"running")).orderBy(drizzleOrm.desc(o.started_at)).all()}catch(e){throw new a("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(o).orderBy(drizzleOrm.desc(o.started_at)).limit(100).all()}catch(e){throw new a("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(o).where(drizzleOrm.eq(o.id,t)).limit(1).get()??void 0}catch(n){throw new a("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:o.id,status:o.status,pid:o.pid}).from(o).where(drizzleOrm.eq(o.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(o).set({status:"failed",error:"Killed by user",completed_at:new Date().toISOString()}).where(drizzleOrm.and(drizzleOrm.eq(o.id,t),drizzleOrm.eq(o.status,"running"))).run(),{killed:!0,pid:n.pid??void 0}}catch(n){throw n instanceof a?n:new a("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:o.id,pid:o.pid}).from(o).where(drizzleOrm.eq(o.status,"running")).all(),n=0;for(let r of e){let s=!1;if(r.pid)try{process.kill(r.pid,0),s=!0;}catch{}s||(t.db.update(o).set({status:"failed",error:"Reaped: process not found (orphaned task)",completed_at:new Date().toISOString()}).where(drizzleOrm.and(drizzleOrm.eq(o.id,r.id),drizzleOrm.eq(o.status,"running"))).run(),n++);}return n}finally{t.close();}}findTaskStatus(t,e){let n=this.resolveDbPaths();for(let r of n){if(!Bt.existsSync(r))continue;let s=g(r);try{let i=e?drizzleOrm.eq(o.workspace_id,e):void 0,l=i?drizzleOrm.and(drizzleOrm.eq(o.id,t),i):drizzleOrm.eq(o.id,t),u=s.db.select().from(o).where(l).limit(1).get()??void 0;if(!u){let f=drizzleOrm.or(drizzleOrm.eq(o.thread_id,t),drizzleOrm.and(drizzleOrm.isNull(o.thread_id),drizzleOrm.like(o.command,`%--thread=${t}%`))),_=i?drizzleOrm.and(f,i):f;u=s.db.select().from(o).where(_).orderBy(drizzleOrm.desc(o.started_at)).limit(1).get()??void 0;}if(u)return u}catch(i){throw new a("DB_ERROR","Failed to find task status",i)}finally{s.close();}}}findChildTasks(t,e){let n=this.resolveDbPaths(),r=new Set,s=[];for(let i of n){if(!Bt.existsSync(i))continue;let l=g(i);try{let u=e?drizzleOrm.and(drizzleOrm.eq(o.parent_task_id,t),drizzleOrm.eq(o.workspace_id,e)):drizzleOrm.eq(o.parent_task_id,t),f=l.db.select().from(o).where(u).orderBy(drizzleOrm.asc(o.started_at)).all();for(let _ of f)r.has(_.id)||(r.add(_.id),s.push(_));}catch(u){throw new a("DB_ERROR","Failed to find child tasks",u)}finally{l.close();}}return s}getWorkspaceUsageSummary(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.all(t?drizzleOrm.sql`
|
|
11
11
|
SELECT
|
|
12
12
|
COALESCE(workspace_id, 'unknown') AS workspace_id,
|
|
13
13
|
COALESCE(SUM(input_tokens), 0) AS input_tokens,
|
|
@@ -28,7 +28,7 @@ ${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 a("DB_ERROR","Failed to get workspace usage summary",n)}finally{e.close();}}getThreadTokenUsage(t,e){let n=this.resolveDbPaths(),r=new Set,s=0,i=0,l=0;for(let
|
|
31
|
+
`)}catch(n){throw new a("DB_ERROR","Failed to get workspace usage summary",n)}finally{e.close();}}getThreadTokenUsage(t,e){let n=this.resolveDbPaths(),r=new Set,s=0,i=0,l=0;for(let u of n){if(!Bt.existsSync(u))continue;let f=g(u);try{let _=drizzleOrm.or(drizzleOrm.eq(o.thread_id,t),drizzleOrm.and(drizzleOrm.isNull(o.thread_id),drizzleOrm.like(o.command,`%--thread=${t}%`))),w=e?drizzleOrm.and(_,drizzleOrm.eq(o.workspace_id,e)):_,y=f.db.select({id:o.id,input_tokens:o.input_tokens,output_tokens:o.output_tokens,cost_usd:o.cost_usd}).from(o).where(w).all();for(let S of y)r.has(S.id)||(r.add(S.id),s+=S.input_tokens??0,i+=S.output_tokens??0,l+=S.cost_usd??0);}catch(_){throw new a("DB_ERROR","Failed to get thread token usage",_)}finally{f.close();}}return {inputTokens:s,outputTokens:i,costUsd:l}}findTasksByThread(t,e){let n=this.resolveDbPaths(),r=new Set,s=[];for(let i of n){if(!Bt.existsSync(i))continue;let l=g(i);try{let u=drizzleOrm.or(drizzleOrm.eq(o.thread_id,t),drizzleOrm.and(drizzleOrm.isNull(o.thread_id),drizzleOrm.like(o.command,`%--thread=${t}%`))),f=e?drizzleOrm.and(u,drizzleOrm.eq(o.workspace_id,e)):u,_=l.db.select().from(o).where(f).orderBy(drizzleOrm.asc(o.started_at)).all();for(let w of _)r.has(w.id)||(r.add(w.id),s.push(w));}catch(u){throw new a("DB_ERROR","Failed to find tasks by thread",u)}finally{l.close();}}return s}findAllTasks(t){if(!this.dbExists())return {rows:[],total:0};let e=this.openHandle(false);try{let n=[];t.workspaceId&&n.push(drizzleOrm.eq(o.workspace_id,t.workspaceId));let r=t.agents&&t.agents.length>0?t.agents:t.agentId?[t.agentId]:null;r&&n.push(drizzleOrm.inArray(o.agent_id,r));let s=t.statuses&&t.statuses.length>0?t.statuses:t.status?[t.status]:null;s&&n.push(drizzleOrm.inArray(o.status,s));let i=t.q??t.search;i&&n.push(drizzleOrm.like(o.prompt,`%${i}%`)),t.from&&n.push(drizzleOrm.gte(o.started_at,t.from)),t.to&&n.push(drizzleOrm.lt(o.started_at,t.to));let l=n.length>0?drizzleOrm.and(...n):void 0,u=e.db.select({count:drizzleOrm.sql`count(*)`}).from(o).where(l).get(),f=(t.sortDir??"DESC")==="ASC"?drizzleOrm.asc(o.started_at):drizzleOrm.desc(o.started_at);return {rows:e.db.select().from(o).where(l).orderBy(f).limit(t.limit).offset(t.offset).all(),total:u?.count??0}}catch(n){throw new a("DB_ERROR","Failed to find all tasks",n)}finally{e.close();}}getAgentUsage(t,e,n){if(!this.dbExists())return [];let r=this.openHandle(false);try{return r.db.all(n?drizzleOrm.sql`
|
|
32
32
|
SELECT
|
|
33
33
|
t.agent_id,
|
|
34
34
|
t.workspace_id,
|
|
@@ -88,13 +88,13 @@ ${n.join(`
|
|
|
88
88
|
AND t.started_at < ${e}
|
|
89
89
|
GROUP BY date(t.started_at), t.agent_id
|
|
90
90
|
ORDER BY date(t.started_at) ASC
|
|
91
|
-
`).map(i=>({date:i.date,agentId:i.agent_id,inputTokens:i.input_tokens,outputTokens:i.output_tokens,cachedInputTokens:i.cached_input_tokens,costUsd:i.cost_usd}))}catch(s){throw new a("DB_ERROR","Failed to get agent usage trend",s)}finally{r.close();}}findTaskForStop(t,e){if(!this.dbExists())return;let n=this.openHandle(false);try{return n.db.select().from(o).where(drizzleOrm.and(drizzleOrm.eq(o.id,t),drizzleOrm.eq(o.workspace_id,e))).limit(1).get()??void 0}catch(r){throw new a("DB_ERROR","Failed to find task for stop",r)}finally{n.close();}}markTaskFailed(t,e,n){if(!this.dbExists())return;let r=this.openHandle(true);try{let s=new Date().toISOString(),i=n?drizzleOrm.and(drizzleOrm.eq(o.id,t),drizzleOrm.eq(o.status,"running"),drizzleOrm.eq(o.workspace_id,n)):drizzleOrm.and(drizzleOrm.eq(o.id,t),drizzleOrm.eq(o.status,"running"));r.db.update(o).set({status:"failed",error:e,completed_at:s}).where(i).run();}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to mark task failed",s)}finally{r.close();}}findTasksByPromptHint(t,e){let n=this.resolveDbPaths(),r=new Set,s=[];for(let i of n){if(!Bt.existsSync(i))continue;let l=g(i);try{let
|
|
91
|
+
`).map(i=>({date:i.date,agentId:i.agent_id,inputTokens:i.input_tokens,outputTokens:i.output_tokens,cachedInputTokens:i.cached_input_tokens,costUsd:i.cost_usd}))}catch(s){throw new a("DB_ERROR","Failed to get agent usage trend",s)}finally{r.close();}}findTaskForStop(t,e){if(!this.dbExists())return;let n=this.openHandle(false);try{return n.db.select().from(o).where(drizzleOrm.and(drizzleOrm.eq(o.id,t),drizzleOrm.eq(o.workspace_id,e))).limit(1).get()??void 0}catch(r){throw new a("DB_ERROR","Failed to find task for stop",r)}finally{n.close();}}markTaskFailed(t,e,n){if(!this.dbExists())return;let r=this.openHandle(true);try{let s=new Date().toISOString(),i=n?drizzleOrm.and(drizzleOrm.eq(o.id,t),drizzleOrm.eq(o.status,"running"),drizzleOrm.eq(o.workspace_id,n)):drizzleOrm.and(drizzleOrm.eq(o.id,t),drizzleOrm.eq(o.status,"running"));r.db.update(o).set({status:"failed",error:e,completed_at:s}).where(i).run();}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to mark task failed",s)}finally{r.close();}}findTasksByPromptHint(t,e){let n=this.resolveDbPaths(),r=new Set,s=[];for(let i of n){if(!Bt.existsSync(i))continue;let l=g(i);try{let u=e?drizzleOrm.and(drizzleOrm.like(o.prompt,`%${t}%`),drizzleOrm.eq(o.workspace_id,e)):drizzleOrm.like(o.prompt,`%${t}%`),f=l.db.select().from(o).where(u).orderBy(drizzleOrm.asc(o.started_at)).all();for(let _ of f)r.has(_.id)||(r.add(_.id),s.push(_));}catch(u){throw new a("DB_ERROR","Failed to find tasks by prompt hint",u)}finally{l.close();}}return s}};var kt=class extends R{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=G.join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let r=G.dirname(e);Bt.existsSync(r)||Bt.mkdirSync(r,{recursive:true});}else if(!Bt.existsSync(e))throw new a("NOT_FOUND","Database not found");let n=g(e);if(t)try{T(n.db,e);}catch(r){throw n.close(),r}return n}validateWorkspaceId(t,e){return t.db.select({id:p.id}).from(p).where(drizzleOrm.eq(p.id,e)).limit(1).get()?e:null}findAllThreads(t){let e=this.resolveDbPaths(),n=new Set,r=[];for(let s of e){if(!Bt.existsSync(s))continue;let i=g(s);try{let l=t?drizzleOrm.eq(c.workspace_id,t):void 0,u=i.db.select().from(c).where(l).orderBy(drizzleOrm.desc(c.updated_at)).all();for(let f of u)n.has(f.id)||(n.add(f.id),r.push(f));}catch(l){throw new a("DB_ERROR","Failed to find all threads",l)}finally{i.close();}}return r}findThreadById(t,e){let n=this.resolveDbPaths();for(let r of n){if(!Bt.existsSync(r))continue;let s=g(r);try{let i=drizzleOrm.eq(c.id,t),l=e?drizzleOrm.and(i,drizzleOrm.eq(c.workspace_id,e)):i,u=s.db.select().from(c).where(l).limit(1).get()??void 0;if(u)return u}catch(i){throw new a("DB_ERROR","Failed to find thread by id",i)}finally{s.close();}}}threadExists(t,e){let n=this.resolveDbPaths();for(let r of n){if(!Bt.existsSync(r))continue;let s=g(r);try{let i=drizzleOrm.eq(c.id,t),l=e?drizzleOrm.and(i,drizzleOrm.eq(c.workspace_id,e)):i;if(s.db.select({id:c.id}).from(c).where(l).limit(1).get())return !0}catch(i){throw new a("DB_ERROR","Failed to check thread existence",i)}finally{s.close();}}return false}aggregateTaskStats(t,e){let n=this.resolveDbPaths(),r=0,s=0,i=0,l=0,u=0,f=new Set;for(let _ of n){if(!Bt.existsSync(_))continue;let w=g(_);try{let y=drizzleOrm.and(drizzleOrm.eq(o.thread_id,t),drizzleOrm.or(drizzleOrm.isNull(o.parent_task_id),drizzleOrm.eq(o.parent_task_id,""))),S=e?drizzleOrm.and(y,drizzleOrm.eq(o.workspace_id,e)):y,z=w.db.select({cnt:drizzleOrm.sql`count(*)`,total_input:drizzleOrm.sql`COALESCE(SUM(input_tokens), 0)`,total_output:drizzleOrm.sql`COALESCE(SUM(output_tokens), 0)`,total_cached:drizzleOrm.sql`COALESCE(SUM(cached_input_tokens), 0)`,total_cost:drizzleOrm.sql`COALESCE(SUM(cost_usd), 0)`}).from(o).where(S).get();z&&(r+=z.cnt,s+=z.total_input,i+=z.total_output,l+=z.total_cached,u+=z.total_cost);let at=w.db.all(drizzleOrm.sql`
|
|
92
92
|
SELECT DISTINCT agent_id FROM tasks
|
|
93
93
|
WHERE thread_id = ${t}
|
|
94
94
|
AND agent_id IS NOT NULL AND agent_id != ''
|
|
95
95
|
AND (parent_task_id IS NULL OR parent_task_id = '')
|
|
96
96
|
${e?drizzleOrm.sql`AND workspace_id = ${e}`:drizzleOrm.sql``}
|
|
97
|
-
`);for(let dt of at)
|
|
97
|
+
`);for(let dt of at)f.add(dt.agent_id);}catch(y){throw new a("DB_ERROR","Failed to aggregate task stats",y)}finally{w.close();}}return {taskCount:r,inputTokens:s,outputTokens:i,cachedInputTokens:l,costUsd:u,agentIds:Array.from(f)}}findTopLevelTasks(t,e){let n=this.resolveDbPaths(),r=new Set,s=[];for(let i of n){if(!Bt.existsSync(i))continue;let l=g(i);try{let u=drizzleOrm.and(drizzleOrm.eq(o.thread_id,t),drizzleOrm.or(drizzleOrm.isNull(o.parent_task_id),drizzleOrm.eq(o.parent_task_id,""))),f=e?drizzleOrm.and(u,drizzleOrm.eq(o.workspace_id,e)):u,_=l.db.select().from(o).where(f).orderBy(drizzleOrm.asc(o.started_at)).all();for(let w of _)r.has(w.id)||(r.add(w.id),s.push(w));}catch(u){throw new a("DB_ERROR","Failed to find top-level tasks",u)}finally{l.close();}}return s}findAllTasks(t,e){let n=this.resolveDbPaths(),r=new Set,s=[];for(let i of n){if(!Bt.existsSync(i))continue;let l=g(i);try{let u=drizzleOrm.eq(o.thread_id,t),f=e?drizzleOrm.and(u,drizzleOrm.eq(o.workspace_id,e)):u,_=l.db.select().from(o).where(f).orderBy(drizzleOrm.asc(o.started_at)).all();for(let w of _)r.has(w.id)||(r.add(w.id),s.push(w));}catch(u){throw new a("DB_ERROR","Failed to find all tasks for thread",u)}finally{l.close();}}return s}findTaskById(t,e,n){let r=this.resolveDbPaths();for(let s of r){if(!Bt.existsSync(s))continue;let i=g(s);try{let l=drizzleOrm.and(drizzleOrm.eq(o.id,e),drizzleOrm.eq(o.thread_id,t)),u=n?drizzleOrm.and(l,drizzleOrm.eq(o.workspace_id,n)):l,f=i.db.select().from(o).where(u).limit(1).get();if(!f)continue;let _=i.db.select().from(o).where(drizzleOrm.eq(o.parent_task_id,f.id)).orderBy(drizzleOrm.asc(o.started_at)).all();return {task:f,children:_}}catch(l){throw new a("DB_ERROR","Failed to find task by id",l)}finally{i.close();}}}batchFetchTasks(t,e){let n=new Map;if(t.length===0)return n;let r=this.resolveDbPaths();for(let s of r){if(!Bt.existsSync(s))continue;let i=g(s);try{let l=drizzleOrm.and(drizzleOrm.inArray(o.thread_id,t),drizzleOrm.or(drizzleOrm.isNull(o.parent_task_id),drizzleOrm.eq(o.parent_task_id,""))),u=e?drizzleOrm.and(l,drizzleOrm.eq(o.workspace_id,e)):l,f=i.db.select().from(o).where(u).orderBy(drizzleOrm.asc(o.started_at)).all();for(let _ of f){let w=_.thread_id;n.has(w)||n.set(w,[]),n.get(w).push(_);}}catch(l){throw new a("DB_ERROR","Failed to batch fetch tasks",l)}finally{i.close();}}return n}updateThreadTitle(t,e,n){if(!this.dbExists())return;let r=this.openHandle(true);try{let s=drizzleOrm.eq(c.id,t),i=n?drizzleOrm.and(s,drizzleOrm.eq(c.workspace_id,n)):s;if(!r.db.select({id:c.id}).from(c).where(i).limit(1).get())return;r.db.update(c).set({title:e,title_locked:1,updated_at:new Date().toISOString()}).where(drizzleOrm.eq(c.id,t)).run();}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to update thread title",s)}finally{r.close();}}upsertThread(t,e){let n=this.openHandle(true);try{let r=e.workspaceId?this.validateWorkspaceId(n,e.workspaceId):null,s=new Date().toISOString();if(n.db.select({id:c.id,message_count:c.message_count}).from(c).where(drizzleOrm.eq(c.id,t)).limit(1).get()){let l={updated_at:s};e.title!==void 0&&(l.title=e.title),e.titleLocked!==void 0&&(l.title_locked=e.titleLocked?1:0),n.db.update(c).set(l).where(drizzleOrm.eq(c.id,t)).run();}else n.db.insert(c).values({id:t,platform:e.platform,workspace_id:r,title:e.title??null,title_locked:e.titleLocked?1:0,message_count:0,created_at:s,updated_at:s}).run();}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to upsert thread",r)}finally{n.close();}}ensureThread(t,e,n){let r=this.openHandle(true);try{let s=n?this.validateWorkspaceId(r,n):null,i=r.db.select({id:c.id,platform:c.platform,workspace_id:c.workspace_id}).from(c).where(drizzleOrm.eq(c.id,t)).limit(1).get();if(i){s&&!i.workspace_id&&r.db.update(c).set({workspace_id:s}).where(drizzleOrm.eq(c.id,t)).run();return}let l=new Date().toISOString();r.db.insert(c).values({id:t,platform:e,workspace_id:s,message_count:0,created_at:l,updated_at:l}).run();}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to ensure thread",s)}finally{r.close();}}saveUserMessage(t,e,n){if(!this.dbExists())return;let r=this.openHandle(true);try{let s=new Date().toISOString();r.db.run(drizzleOrm.sql`
|
|
98
98
|
UPDATE threads
|
|
99
99
|
SET first_message = COALESCE(first_message, ${e}),
|
|
100
100
|
title = CASE WHEN title_locked = 0 AND title IS NULL THEN substr(${e}, 1, 60) ELSE title END,
|
|
@@ -102,4 +102,4 @@ ${n.join(`
|
|
|
102
102
|
message_count = message_count + 1,
|
|
103
103
|
updated_at = ${s}
|
|
104
104
|
WHERE id = ${t}
|
|
105
|
-
`);}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to save user message",s)}finally{r.close();}}saveAssistantMessage(t,e,n){if(!this.dbExists())return;let r=this.openHandle(true);try{let s=new Date().toISOString();r.db.update(
|
|
105
|
+
`);}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to save user message",s)}finally{r.close();}}saveAssistantMessage(t,e,n){if(!this.dbExists())return;let r=this.openHandle(true);try{let s=new Date().toISOString();r.db.update(c).set({last_message:e,updated_at:s}).where(drizzleOrm.eq(c.id,t)).run();}catch(s){throw s instanceof a?s:new a("DB_ERROR","Failed to save assistant message",s)}finally{r.close();}}updateThread(t,e){if(!this.dbExists())return;let n=this.openHandle(true);try{let r={updated_at:new Date().toISOString()};e.title!==void 0&&(r.title=e.title,r.title_locked=1),e.titleLocked!==void 0&&(r.title_locked=e.titleLocked?1:0),n.db.update(c).set(r).where(drizzleOrm.eq(c.id,t)).run();}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to update thread",r)}finally{n.close();}}togglePin(t,e){let n=this.openHandle(true);try{let r=e?drizzleOrm.and(drizzleOrm.eq(c.id,t),drizzleOrm.eq(c.workspace_id,e)):drizzleOrm.eq(c.id,t),s=n.db.select({pinned:c.pinned,metadata:c.metadata}).from(c).where(r).get();if(!s)return null;let i=s.pinned?0:1,l=s.metadata?JSON.parse(s.metadata):{};if(i){let u=e?drizzleOrm.and(drizzleOrm.eq(c.pinned,1),drizzleOrm.eq(c.workspace_id,e)):drizzleOrm.eq(c.pinned,1),f=n.db.select({metadata:c.metadata}).from(c).where(u).all(),_=0;for(let w of f){let y=w.metadata?JSON.parse(w.metadata):{};typeof y.pinOrder=="number"&&y.pinOrder>_&&(_=y.pinOrder);}l.pinOrder=_+1;}else delete l.pinOrder;return n.db.update(c).set({pinned:i,metadata:Object.keys(l).length>0?JSON.stringify(l):null}).where(r).run(),{pinned:!!i}}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to toggle pin",r)}finally{n.close();}}reorderPins(t,e){let n=this.openHandle(true);try{for(let r=0;r<t.length;r++){let s=e?drizzleOrm.and(drizzleOrm.eq(c.id,t[r]),drizzleOrm.eq(c.workspace_id,e)):drizzleOrm.eq(c.id,t[r]),i=n.db.select({metadata:c.metadata}).from(c).where(s).get();if(!i)continue;let l=i.metadata?JSON.parse(i.metadata):{};l.pinOrder=r+1,n.db.update(c).set({metadata:JSON.stringify(l)}).where(s).run();}}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to reorder pins",r)}finally{n.close();}}toggleStar(t,e){let n=this.openHandle(true);try{let r=e?drizzleOrm.and(drizzleOrm.eq(c.id,t),drizzleOrm.eq(c.workspace_id,e)):drizzleOrm.eq(c.id,t),s=n.db.select({starred:c.starred}).from(c).where(r).get();if(!s)return null;let i=s.starred?0:1;return n.db.update(c).set({starred:i}).where(r).run(),{starred:!!i}}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to toggle star",r)}finally{n.close();}}};var bt=class extends R{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=G.join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let r=G.dirname(e);Bt.existsSync(r)||Bt.mkdirSync(r,{recursive:true});}else if(!Bt.existsSync(e))throw new a("NOT_FOUND","Database not found");let n=g(e);if(t)try{T(n.db,e);}catch(r){throw n.close(),r}return n}insertSpan(t){let e=this.openHandle(true);try{e.db.insert(q).values(t).run();}catch(n){throw n instanceof a?n:new a("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(q).where(drizzleOrm.eq(q.task_id,t)).all()}catch(n){throw new a("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(q).where(drizzleOrm.eq(q.id,t)).limit(1).get()??void 0}catch(n){throw new a("DB_ERROR","Failed to find span by id",n)}finally{e.close();}}};var yt=class extends R{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=G.join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let r=G.dirname(e);Bt.existsSync(r)||Bt.mkdirSync(r,{recursive:true});}else if(!Bt.existsSync(e))throw new a("NOT_FOUND","Database not found");let n=g(e);if(t)try{T(n.db,e);}catch(r){throw n.close(),r}return n}insertToolCall(t){let e=this.openHandle(true);try{e.db.insert(B).values(t).run();}catch(n){throw n instanceof a?n:new a("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(B).where(drizzleOrm.eq(B.task_id,t)).all()}catch(n){throw new a("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:B.tool_name,count:drizzleOrm.sql`count(*)`}).from(B).where(drizzleOrm.eq(B.task_id,t)).groupBy(B.tool_name).all().map(n=>({toolName:n.toolName,count:n.count}))}catch(n){throw new a("DB_ERROR","Failed to aggregate tool calls by name",n)}finally{e.close();}}};var Rt=class extends R{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=G.join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let r=G.dirname(e);Bt.existsSync(r)||Bt.mkdirSync(r,{recursive:true});}else if(!Bt.existsSync(e))throw new a("NOT_FOUND","Database not found");let n=g(e);if(t)try{T(n.db,e);}catch(r){throw n.close(),r}return n}ensureThreadExists(t,e){if(!t.db.select({id:c.id}).from(c).where(drizzleOrm.eq(c.id,e)).limit(1).get())throw new a("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(N).where(drizzleOrm.eq(N.thread_id,t)).orderBy(drizzleOrm.asc(N.seq)).all()}catch(n){throw n instanceof a?n:new a("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(N).where(drizzleOrm.and(drizzleOrm.eq(N.id,e),drizzleOrm.eq(N.thread_id,t))).limit(1).get()??void 0}catch(r){throw r instanceof a?r:new a("DB_ERROR","Failed to find box by id",r)}finally{n.close();}}insert(t){let e=this.openHandle(true);try{this.ensureThreadExists(e,t.threadId);try{e.db.insert(N).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(r){throw r instanceof Error&&/UNIQUE constraint failed/i.test(r.message)?new a("CONFLICT",`Duplicate seq ${String(t.seq)} for thread ${t.threadId}`,r):r}let n=e.db.select().from(N).where(drizzleOrm.eq(N.id,t.id)).limit(1).get();if(!n)throw new a("DB_ERROR","Insert did not return a row");return n}catch(n){throw n instanceof a?n:new a("DB_ERROR","Failed to insert thread box",n)}finally{e.close();}}};var Et=class extends R{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=G.join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let r=G.dirname(e);Bt.existsSync(r)||Bt.mkdirSync(r,{recursive:true});}else if(!Bt.existsSync(e))throw new a("NOT_FOUND","Database not found");let n=g(e);if(t)try{T(n.db,e);}catch(r){throw n.close(),r}return n}bulkInsert(t){if(t.length===0)return;let e=this.openHandle(true);try{let n=new Date().toISOString();e.db.transaction(r=>{for(let s of t)r.insert(X).values({id:crypto.randomUUID(),path:s.path,method:s.method,status_code:s.statusCode,duration_ms:s.durationMs,ip:s.ip??null,request_headers:s.requestHeaders??null,response_headers:s.responseHeaders??null,request_body:s.requestBody??null,response_body:s.responseBody??null,query:s.query??null,user_id:s.userId??null,project_id:s.projectId??null,partition_key:s.partitionKey??"default",timestamp:n,metadata:s.metadata??null}).run();});}catch(n){throw n instanceof a?n:new a("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(X).orderBy(drizzleOrm.desc(X.timestamp)).limit(t).all()}catch(n){throw new a("DB_ERROR","Failed to find recent request logs",n)}finally{e.close();}}};exports.BaseSqliteRepository=R;exports.RepositoryError=a;exports.RequestLogRepository=Et;exports.SpanRepository=bt;exports.TaskRepository=mt;exports.ThreadBoxRepository=Rt;exports.ThreadRepository=kt;exports.ToolCallRepository=yt;exports.WorkspaceRepository=ft;exports.openDrizzleDb=g;exports.pushSchema=Ee;exports.runMigrations=j;exports.runMigrationsOnce=T;
|
|
@@ -56,6 +56,7 @@ export declare class ThreadRepository extends BaseSqliteRepository {
|
|
|
56
56
|
togglePin(threadId: string, workspaceId?: string): {
|
|
57
57
|
pinned: boolean;
|
|
58
58
|
} | null;
|
|
59
|
+
reorderPins(threadIds: string[], workspaceId?: string): void;
|
|
59
60
|
toggleStar(threadId: string, workspaceId?: string): {
|
|
60
61
|
starred: boolean;
|
|
61
62
|
} | null;
|