@crewx/sdk 0.9.0-rc.73 → 0.9.0-rc.74

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,20 +1,20 @@
1
- import*as Q from'path';import Q__default,{join,dirname}from'path';import {fileURLToPath}from'url';import {existsSync,mkdirSync,writeFileSync,appendFileSync,readFileSync}from'fs';import Pe,{homedir}from'os';import {createHash}from'crypto';import {sql,eq,desc,and,isNull,lt,or,notLike,like,asc,inArray,gte,isNotNull,ne}from'drizzle-orm';import {sqliteTable,text,integer,real,index,check,uniqueIndex,unique,primaryKey}from'drizzle-orm/sqlite-core';var yt=(r=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(r,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):r)(function(r){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+r+'" is not supported')});var xe=()=>fileURLToPath(import.meta.url),Ae=()=>Q__default.dirname(xe()),k=Ae();var it=class{detach(t){}};function Jt(r){let t=e=>String(e).padStart(2,"0");return `${r.getFullYear()}${t(r.getMonth()+1)}${t(r.getDate())}T${t(r.getHours())}${t(r.getMinutes())}${t(r.getSeconds())}`}function It(r){let t=r.toISOString();return `${t.slice(0,10)} ${t.slice(11,19)} UTC`}var Pt=class extends it{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=Jt(e.timestamp),o=join(this.logsDir,`${s}_${e.traceId}.log`);this.logFiles.set(e.traceId,o);let i=`=== TASK LOG: ${e.traceId} ===
1
+ import*as J from'path';import J__default,{join,dirname}from'path';import {fileURLToPath}from'url';import {existsSync,mkdirSync,writeFileSync,appendFileSync,readFileSync}from'fs';import Fe,{homedir}from'os';import {createHash}from'crypto';import {sql,eq,desc,and,isNull,lt,or,notLike,asc,like,inArray,gte,isNotNull,ne}from'drizzle-orm';import {sqliteTable,text,integer,real,index,check,uniqueIndex,unique,primaryKey}from'drizzle-orm/sqlite-core';var bt=(r=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(r,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):r)(function(r){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+r+'" is not supported')});var $e=()=>fileURLToPath(import.meta.url),Ie=()=>J__default.dirname($e()),w=Ie();var rt=class{detach(t){}};function te(r){let t=e=>String(e).padStart(2,"0");return `${r.getFullYear()}${t(r.getMonth()+1)}${t(r.getDate())}T${t(r.getHours())}${t(r.getMinutes())}${t(r.getSeconds())}`}function jt(r){let t=r.toISOString();return `${t.slice(0,10)} ${t.slice(11,19)} UTC`}var Ut=class extends rt{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=te(e.timestamp),o=join(this.logsDir,`${s}_${e.traceId}.log`);this.logFiles.set(e.traceId,o);let i=`=== TASK LOG: ${e.traceId} ===
2
2
  CrewX Version: ${this.version}
3
3
  Mode: ${e.mode}
4
4
  Agent: ${e.agentRef}
5
- Started: ${It(e.timestamp)}
5
+ Started: ${jt(e.timestamp)}
6
6
  Message: ${e.message}
7
7
 
8
8
  `;writeFileSync(o,i,{encoding:"utf8",mode:384});}catch{}}),t.on("task:output",e=>{try{let s=this.logFiles.get(e.traceId);if(!s)return;let o=new Date().toISOString();appendFileSync(s,`[${o}] STDOUT: ${e.output}
9
- `,"utf8");}catch{}}),t.on("task:end",e=>{try{let s=this.logFiles.get(e.traceId);if(!s)return;let o=It(new Date),i=e.error?`failed: ${e.error.message}`:"completed successfully",d=`[${o}] INFO: Task ${i} in ${e.durationMs}ms
9
+ `,"utf8");}catch{}}),t.on("task:end",e=>{try{let s=this.logFiles.get(e.traceId);if(!s)return;let o=jt(new Date),i=e.error?`failed: ${e.error.message}`:"completed successfully",a=`[${o}] INFO: Task ${i} in ${e.durationMs}ms
10
10
  [${o}] INFO: Process closed with exit code: ${e.error?1:0}
11
- `;appendFileSync(s,d,"utf8"),this.logFiles.delete(e.traceId);}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[],this.logFiles.clear();}};function Le(r){let t=Q.resolve(r);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 Qt(r){let t=Le(r);return createHash("sha256").update(t).digest("hex")}var at=class{resolveDbPath(){return process.env.CREWX_DB?process.env.CREWX_DB:process.env.CREWX_TRACES_DB?process.env.CREWX_TRACES_DB:join(Pe.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 Ue=32*1024*1024;function N(r){let t=yt("better-sqlite3"),{drizzle:e}=yt("drizzle-orm/better-sqlite3"),s=new t(r);return s.exec("PRAGMA journal_mode = WAL"),s.exec(`PRAGMA journal_size_limit = ${Ue}`),s.exec("PRAGMA busy_timeout = 5000"),s.exec("PRAGMA foreign_keys = ON"),s.exec("PRAGMA analysis_limit = 400"),s.exec("PRAGMA optimize"),{db:e(s),runRaw:(o,i=[])=>s.prepare(o).run(...i),close:()=>s.close()}}var Zt=new Set,je={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(r,t){return (r.get(`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='${t}'`)?.cnt??0)>0}function qe(r,t){if(t>0||!Mt(r,"tasks"))return;let e=r.all("PRAGMA table_info(tasks)"),s=new Set(e.map(o=>o.name));for(let[o,i]of Object.entries(je))s.has(o)||r.run(`ALTER TABLE tasks ADD COLUMN ${o} ${i}`);}var Be={"0002_normalize_task_names":{workspace_name:"TEXT",project_name:"TEXT"},"0015_panoramic_gorilla_man":{project_id:"TEXT",workspace_id:"TEXT"}};function He(r,t,e){if(!Mt(r,"__drizzle_migrations")||!Mt(r,"tasks"))return;let s=r.all(e`SELECT hash FROM __drizzle_migrations`),o=new Set(s.map(d=>d.hash)),i=JSON.parse(readFileSync(Q__default.join(t,"meta/_journal.json"),"utf-8"));for(let d of i.entries){let a=Be[d.tag];if(!a)continue;let u=Q__default.join(t,`${d.tag}.sql`);if(!existsSync(u))continue;let c=readFileSync(u,"utf-8"),l=createHash("sha256").update(c).digest("hex");if(o.has(l))continue;let g=r.all("PRAGMA table_info(tasks)"),f=new Set(g.map(w=>w.name));for(let[w,L]of Object.entries(a))f.has(w)||(r.run(`ALTER TABLE tasks ADD COLUMN ${w} ${L}`),f.add(w));}}function Fe(r,t,e){let s=r.all(e`SELECT hash FROM __drizzle_migrations`),o=new Set(s.map(d=>d.hash)),i=JSON.parse(readFileSync(Q__default.join(t,"meta/_journal.json"),"utf-8"));for(let d of i.entries){let a=Q__default.join(t,`${d.tag}.sql`);if(!existsSync(a))continue;let u=readFileSync(a,"utf-8"),c=createHash("sha256").update(u).digest("hex");if(o.has(c))continue;let l=/ALTER\s+TABLE\s+[`"]?(\w+)[`"]?\s+ADD\s+[`"]?(\w+)[`"]?/gi,g=[],f;for(;(f=l.exec(u))!==null;)g.push({table:f[1],column:f[2]});if(g.length===0||!u.split(/-->\s*statement-breakpoint/).map(O=>O.trim()).filter(Boolean).every(O=>/^ALTER\s+TABLE\s+.+\s+ADD\s+/i.test(O)))continue;g.every(({table:O,column:q})=>r.all(`PRAGMA table_info("${O}")`).some(V=>V.name===q))&&r.run(e`INSERT INTO __drizzle_migrations (hash, created_at) VALUES (${c}, ${d.when})`);}}function ze(r){let{migrate:t}=yt("drizzle-orm/better-sqlite3/migrator"),{sql:e}=yt("drizzle-orm"),s=[Q__default.join(k,"../migrations"),Q__default.join(k,"migrations"),Q__default.join(k,"../../../../drizzle/migrations"),Q__default.join(process.cwd(),"drizzle/migrations")],o=s.find(c=>existsSync(Q__default.join(c,"meta/_journal.json")));if(!o)throw new Error(`migrations folder not found. Searched:
11
+ `;appendFileSync(s,a,"utf8"),this.logFiles.delete(e.traceId);}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[],this.logFiles.clear();}};function qe(r){let t=J.resolve(r);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 re(r){let t=qe(r);return createHash("sha256").update(t).digest("hex")}var ot=class{resolveDbPath(){return process.env.CREWX_DB?process.env.CREWX_DB:process.env.CREWX_TRACES_DB?process.env.CREWX_TRACES_DB:join(Fe.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 We=32*1024*1024;function N(r){let t=bt("better-sqlite3"),{drizzle:e}=bt("drizzle-orm/better-sqlite3"),s=new t(r);return s.exec("PRAGMA journal_mode = WAL"),s.exec(`PRAGMA journal_size_limit = ${We}`),s.exec("PRAGMA busy_timeout = 5000"),s.exec("PRAGMA foreign_keys = ON"),s.exec("PRAGMA analysis_limit = 400"),s.exec("PRAGMA optimize"),{db:e(s),runRaw:(o,i=[])=>s.prepare(o).run(...i),close:()=>s.close()}}var oe=new Set,Ke={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",command:"TEXT"};function qt(r,t){return (r.get(`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='${t}'`)?.cnt??0)>0}function Ge(r,t){if(t>0||!qt(r,"tasks"))return;let e=r.all("PRAGMA table_info(tasks)"),s=new Set(e.map(o=>o.name));for(let[o,i]of Object.entries(Ke))s.has(o)||r.run(`ALTER TABLE tasks ADD COLUMN ${o} ${i}`);}var Xe={"0002_normalize_task_names":{workspace_name:"TEXT",project_name:"TEXT"},"0015_panoramic_gorilla_man":{project_id:"TEXT",workspace_id:"TEXT"},"0019_backfill_task_thread_ids":{command:"TEXT",thread_id:"TEXT"}};function Je(r,t,e){if(!qt(r,"__drizzle_migrations")||!qt(r,"tasks"))return;let s=r.all(e`SELECT hash FROM __drizzle_migrations`),o=new Set(s.map(a=>a.hash)),i=JSON.parse(readFileSync(J__default.join(t,"meta/_journal.json"),"utf-8"));for(let a of i.entries){let d=Xe[a.tag];if(!d)continue;let l=J__default.join(t,`${a.tag}.sql`);if(!existsSync(l))continue;let c=readFileSync(l,"utf-8"),u=createHash("sha256").update(c).digest("hex");if(o.has(u))continue;let g=r.all("PRAGMA table_info(tasks)"),f=new Set(g.map(m=>m.name));for(let[m,C]of Object.entries(d))f.has(m)||(r.run(`ALTER TABLE tasks ADD COLUMN ${m} ${C}`),f.add(m));}}function Ye(r,t,e){let s=r.all(e`SELECT hash FROM __drizzle_migrations`),o=new Set(s.map(a=>a.hash)),i=JSON.parse(readFileSync(J__default.join(t,"meta/_journal.json"),"utf-8"));for(let a of i.entries){let d=J__default.join(t,`${a.tag}.sql`);if(!existsSync(d))continue;let l=readFileSync(d,"utf-8"),c=createHash("sha256").update(l).digest("hex");if(o.has(c))continue;let u=/ALTER\s+TABLE\s+[`"]?(\w+)[`"]?\s+ADD\s+[`"]?(\w+)[`"]?/gi,g=[],f;for(;(f=u.exec(l))!==null;)g.push({table:f[1],column:f[2]});if(g.length===0||!l.split(/-->\s*statement-breakpoint/).map(D=>D.trim()).filter(Boolean).every(D=>/^ALTER\s+TABLE\s+.+\s+ADD\s+/i.test(D)))continue;g.every(({table:D,column:O})=>r.all(`PRAGMA table_info("${D}")`).some(X=>X.name===O))&&r.run(e`INSERT INTO __drizzle_migrations (hash, created_at) VALUES (${c}, ${a.when})`);}}function Ve(r){let{migrate:t}=bt("drizzle-orm/better-sqlite3/migrator"),{sql:e}=bt("drizzle-orm"),s=[J__default.join(w,"../migrations"),J__default.join(w,"migrations"),J__default.join(w,"../../../../drizzle/migrations"),J__default.join(process.cwd(),"drizzle/migrations")],o=s.find(c=>existsSync(J__default.join(c,"meta/_journal.json")));if(!o)throw new Error(`migrations folder not found. Searched:
12
12
  ${s.join(`
13
- `)}`);let i=r.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),d=0;i?.cnt&&(d=r.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),qe(r,d),i?.cnt&&(Fe(r,o,e),He(r,o,e)),t(r,{migrationsFolder:o});let u=(r.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0)-d;if(u>0){let c=i?.cnt?"Database migrated":"Database initialized";console.error(`[crewx] ${c} (${u} migration${u>1?"s":""} applied).`);}}function pt(r,t){Zt.has(t)||(ze(r),Zt.add(t));}var _=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 gt=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 n=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"),log_storage:text("log_storage",{enum:["blob","events"]}).notNull().default("blob"),last_log_seq:integer("last_log_seq").notNull().default(0),log_revision:integer("log_revision").notNull().default(0),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),run_epoch:integer("run_epoch").default(0)},r=>({log_storage_check:check("tasks_log_storage_ck",sql`${r.log_storage} IN ('blob', 'events')`),idx_tasks_agent_id:index("idx_tasks_agent_id").on(r.agent_id),idx_tasks_status:index("idx_tasks_status").on(r.status),idx_tasks_started_at:index("idx_tasks_started_at").on(r.started_at),idx_tasks_trace_id:index("idx_tasks_trace_id").on(r.trace_id),idx_tasks_parent_task_id:index("idx_tasks_parent_task_id").on(r.parent_task_id),idx_tasks_crewx_version:index("idx_tasks_crewx_version").on(r.crewx_version),idx_tasks_pid:index("idx_tasks_pid").on(r.pid),idx_tasks_thread_id:index("idx_tasks_thread_id").on(r.thread_id),idx_tasks_workspace_id:index("idx_tasks_workspace_id").on(r.workspace_id),idx_tasks_workspace_ref:index("idx_tasks_workspace_ref").on(r.workspace_ref),idx_tasks_project_id:index("idx_tasks_project_id").on(r.project_id),idx_tasks_ws_started:index("idx_tasks_ws_started").on(r.workspace_id,r.started_at)}));var W=sqliteTable("task_log_events",{id:integer("id").primaryKey(),task_id:text("task_id").notNull().references(()=>n.id,{onDelete:"cascade"}),seq:integer("seq").notNull(),timestamp:text("timestamp").notNull(),level:text("level").notNull(),message:text("message").notNull()},r=>({taskSeqUnique:uniqueIndex("task_log_events_task_seq_uq").on(r.task_id,r.seq)}));var p=sqliteTable("threads",{id:text("id").primaryKey(),workspace_id:text("workspace_id").references(()=>gt.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)},r=>({idx_threads_updated_at:index("idx_threads_updated_at").on(r.updated_at),idx_threads_workspace_id:index("idx_threads_workspace_id").on(r.workspace_id),idx_threads_ws_updated:index("idx_threads_ws_updated").on(r.workspace_id,r.updated_at),idx_threads_title:index("idx_threads_title").on(r.title)}));var nn=sqliteTable("spans",{id:text("id").primaryKey(),task_id:text("task_id").references(()=>n.id,{onDelete:"set null"}),parent_span_id:text("parent_span_id").references(()=>nn.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")},r=>({idx_spans_task_id:index("idx_spans_task_id").on(r.task_id),idx_spans_parent_span_id:index("idx_spans_parent_span_id").on(r.parent_span_id)}));sqliteTable("tool_calls",{id:text("id").primaryKey(),task_id:text("task_id").references(()=>n.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()},r=>({idx_tool_calls_task_id:index("idx_tool_calls_task_id").on(r.task_id),idx_tool_calls_tool_name:index("idx_tool_calls_tool_name").on(r.tool_name),idx_tool_calls_timestamp:index("idx_tool_calls_timestamp").on(r.timestamp)}));sqliteTable("thread_boxes",{id:text("id").primaryKey(),thread_id:text("thread_id").notNull().references(()=>p.id,{onDelete:"cascade"}),seq:integer("seq").notNull(),first_task_id:text("first_task_id").notNull(),mid_task_id:text("mid_task_id").notNull(),last_task_id:text("last_task_id").notNull(),task_count:integer("task_count").notNull(),summary:text("summary"),source_tokens:integer("source_tokens").notNull(),summary_tokens:integer("summary_tokens"),created_at:text("created_at").notNull()},r=>({idx_thread_boxes_thread_id:index("idx_thread_boxes_thread_id").on(r.thread_id),idx_thread_boxes_seq:index("idx_thread_boxes_seq").on(r.thread_id,r.seq),uniq_thread_boxes_thread_seq:unique().on(r.thread_id,r.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")},r=>({idx_request_logs_timestamp:index("idx_request_logs_timestamp").on(r.timestamp),idx_request_logs_path:index("idx_request_logs_path").on(r.path),idx_request_logs_status_code:index("idx_request_logs_status_code").on(r.status_code),idx_request_logs_partition_key:index("idx_request_logs_partition_key").on(r.partition_key)}));sqliteTable("usage_limit_snapshots",{id:text("id").primaryKey(),provider:text("provider").notNull(),account_ref:text("account_ref").notNull().default("default"),limit_window:text("limit_window").notNull(),bucket_start:text("bucket_start").notNull(),captured_at:text("captured_at").notNull(),used_percent:integer("used_percent").notNull(),remaining_percent:integer("remaining_percent").notNull(),resets_at:text("resets_at"),source:text("source").notNull(),metadata:text("metadata")},r=>({uniq_usage_limit_snapshots_bucket:uniqueIndex("uniq_usage_limit_snapshots_bucket").on(r.provider,r.account_ref,r.limit_window,r.bucket_start),idx_usage_limit_snapshots_window_bucket:index("idx_usage_limit_snapshots_window_bucket").on(r.limit_window,r.bucket_start),idx_usage_limit_snapshots_provider_window_bucket:index("idx_usage_limit_snapshots_provider_window_bucket").on(r.provider,r.limit_window,r.bucket_start)}));sqliteTable("usage_reports",{id:text("id").primaryKey(),workspace_id:text("workspace_id").notNull(),month:text("month").notNull(),generated_at:text("generated_at").notNull(),source_range:text("source_range").notNull(),tier:text("tier").notNull(),total_tokens:integer("total_tokens").notNull(),total_cost_usd:real("total_cost_usd").notNull(),payload:text("payload").notNull(),payload_hash:text("payload_hash"),signature:text("signature"),signed_at:text("signed_at"),issuer:text("issuer")},r=>({uniq_usage_reports_ws_month:uniqueIndex("uniq_usage_reports_ws_month").on(r.workspace_id,r.month)}));sqliteTable("notifications",{id:text("id").primaryKey(),workspace_id:text("workspace_id").notNull(),agent_id:text("agent_id"),task_id:text("task_id"),thread_id:text("thread_id"),source:text("source").notNull().default("agent"),level:text("level").notNull().default("info"),title:text("title").notNull(),body:text("body"),target_user_id:text("target_user_id"),created_at:text("created_at").notNull(),metadata:text("metadata")},r=>({idx_notifications_ws_created:index("idx_notifications_ws_created").on(r.workspace_id,sql`${r.created_at} DESC`)}));sqliteTable("notification_reads",{notification_id:text("notification_id").notNull(),user_id:text("user_id").notNull(),read_at:text("read_at").notNull()},r=>({pk:primaryKey({columns:[r.notification_id,r.user_id]})}));sqliteTable("agent_suggestions",{id:text("id").primaryKey(),workspace_id:text("workspace_id").notNull(),agent_id:text("agent_id").notNull(),task_id:text("task_id"),type:text("type").notNull(),status:text("status").notNull().default("pending"),payload:text("payload").notNull(),applied_commit_sha:text("applied_commit_sha"),created_at:text("created_at").notNull(),updated_at:text("updated_at").notNull()},r=>({idx_agent_suggestions_ws_agent:index("idx_agent_suggestions_ws_agent").on(r.workspace_id,r.agent_id),idx_agent_suggestions_ws_created:index("idx_agent_suggestions_ws_created").on(r.workspace_id,r.created_at)}));sqliteTable("ui_instances",{id:text("id").primaryKey().notNull(),definition_id:text("definition_id").notNull(),definition_version:integer("definition_version").notNull(),definition_hash:text("definition_hash").notNull(),workspace_id:text("workspace_id").notNull(),thread_id:text("thread_id").notNull(),anchor_task_id:text("anchor_task_id"),domain_ref_json:text("domain_ref_json").notNull(),state_json:text("state_json").notNull(),revision:integer("revision").notNull().default(0),status:text("status").notNull().default("active"),granted_capabilities_json:text("granted_capabilities_json").notNull().default("[]"),created_at:integer("created_at").notNull(),updated_at:integer("updated_at").notNull()},r=>({idCheck:check("ui_instances_id_ck",sql`length(${r.id}) BETWEEN 5 AND 128 AND ${r.id} GLOB 'uic_*'`),definitionIdCheck:check("ui_instances_definition_id_ck",sql`length(${r.definition_id}) BETWEEN 1 AND 128`),definitionVersionCheck:check("ui_instances_definition_version_ck",sql`${r.definition_version} >= 1`),definitionHashCheck:check("ui_instances_definition_hash_ck",sql`length(${r.definition_hash}) = 64 AND ${r.definition_hash} NOT GLOB '*[^0-9a-f]*'`),workspaceIdCheck:check("ui_instances_workspace_id_ck",sql`length(${r.workspace_id}) BETWEEN 1 AND 256`),threadIdCheck:check("ui_instances_thread_id_ck",sql`length(${r.thread_id}) BETWEEN 1 AND 256`),domainRefJsonCheck:check("ui_instances_domain_ref_json_ck",sql`json_valid(${r.domain_ref_json}) AND json_type(${r.domain_ref_json}) = 'object'`),stateJsonCheck:check("ui_instances_state_json_ck",sql`json_valid(${r.state_json}) AND json_type(${r.state_json}) = 'object'`),revisionCheck:check("ui_instances_revision_ck",sql`${r.revision} >= 0`),statusCheck:check("ui_instances_status_ck",sql`${r.status} IN ('active', 'completed', 'expired', 'revoked', 'unsupported')`),grantedCapabilitiesJsonCheck:check("ui_instances_granted_capabilities_json_ck",sql`json_valid(${r.granted_capabilities_json}) AND json_type(${r.granted_capabilities_json}) = 'array'`),createdAtCheck:check("ui_instances_created_at_ck",sql`${r.created_at} >= 0`),updatedAtCheck:check("ui_instances_updated_at_ck",sql`${r.updated_at} >= ${r.created_at}`),workspaceThreadIdx:index("ui_instances_workspace_thread_idx").on(r.workspace_id,r.thread_id)}));sqliteTable("ui_action_receipts",{id:text("id").primaryKey().notNull(),instance_id:text("instance_id").notNull(),request_id:text("request_id").notNull(),action_id:text("action_id").notNull(),actor_json:text("actor_json").notNull(),source:text("source").notNull(),expected_revision:integer("expected_revision").notNull(),before_revision:integer("before_revision").notNull(),after_revision:integer("after_revision"),status:text("status").notNull(),outcome:text("outcome"),input_json:text("input_json").notNull(),result_json:text("result_json"),error_code:text("error_code"),requested_at:integer("requested_at").notNull(),settled_at:integer("settled_at")},r=>({idCheck:check("ui_action_receipts_id_ck",sql`length(${r.id}) BETWEEN 5 AND 128`),instanceIdCheck:check("ui_action_receipts_instance_id_ck",sql`length(${r.instance_id}) BETWEEN 1 AND 128`),requestIdCheck:check("ui_action_receipts_request_id_ck",sql`length(${r.request_id}) BETWEEN 1 AND 128`),actionIdCheck:check("ui_action_receipts_action_id_ck",sql`length(${r.action_id}) BETWEEN 1 AND 128`),actorJsonCheck:check("ui_action_receipts_actor_json_ck",sql`json_valid(${r.actor_json}) AND json_type(${r.actor_json}) = 'object'`),sourceCheck:check("ui_action_receipts_source_ck",sql`${r.source} IN ('ui_host', 'cli', 'system')`),expectedRevisionCheck:check("ui_action_receipts_expected_revision_ck",sql`${r.expected_revision} >= 0`),beforeRevisionCheck:check("ui_action_receipts_before_revision_ck",sql`${r.before_revision} >= 0`),afterRevisionCheck:check("ui_action_receipts_after_revision_ck",sql`${r.after_revision} IS NULL OR ${r.after_revision} >= 0`),statusCheck:check("ui_action_receipts_status_ck",sql`${r.status} IN ('requested', 'settled')`),outcomeCheck:check("ui_action_receipts_outcome_ck",sql`${r.outcome} IS NULL OR ${r.outcome} IN ('succeeded', 'rejected', 'failed', 'conflicted', 'expired')`),inputJsonCheck:check("ui_action_receipts_input_json_ck",sql`json_valid(${r.input_json}) AND json_type(${r.input_json}) = 'object'`),resultJsonCheck:check("ui_action_receipts_result_json_ck",sql`${r.result_json} IS NULL OR (json_valid(${r.result_json}) AND json_type(${r.result_json}) = 'object')`),requestedAtCheck:check("ui_action_receipts_requested_at_ck",sql`${r.requested_at} >= 0`),settledAtCheck:check("ui_action_receipts_settled_at_ck",sql`${r.settled_at} IS NULL OR ${r.settled_at} >= ${r.requested_at}`),statusTransitionCheck:check("ui_action_receipts_status_transition_ck",sql`(
13
+ `)}`);let i=r.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),a=0;i?.cnt&&(a=r.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),Ge(r,a),i?.cnt&&(Ye(r,o,e),Je(r,o,e)),t(r,{migrationsFolder:o});let l=(r.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0)-a;if(l>0){let c=i?.cnt?"Database migrated":"Database initialized";console.error(`[crewx] ${c} (${l} migration${l>1?"s":""} applied).`);}}function gt(r,t){oe.has(t)||(Ve(r),oe.add(t));}var _=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 ft=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 n=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"),log_storage:text("log_storage",{enum:["blob","events"]}).notNull().default("blob"),last_log_seq:integer("last_log_seq").notNull().default(0),log_revision:integer("log_revision").notNull().default(0),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),run_epoch:integer("run_epoch").default(0)},r=>({log_storage_check:check("tasks_log_storage_ck",sql`${r.log_storage} IN ('blob', 'events')`),idx_tasks_agent_id:index("idx_tasks_agent_id").on(r.agent_id),idx_tasks_status:index("idx_tasks_status").on(r.status),idx_tasks_started_at:index("idx_tasks_started_at").on(r.started_at),idx_tasks_trace_id:index("idx_tasks_trace_id").on(r.trace_id),idx_tasks_parent_task_id:index("idx_tasks_parent_task_id").on(r.parent_task_id),idx_tasks_crewx_version:index("idx_tasks_crewx_version").on(r.crewx_version),idx_tasks_pid:index("idx_tasks_pid").on(r.pid),idx_tasks_thread_id:index("idx_tasks_thread_id").on(r.thread_id),idx_tasks_workspace_id:index("idx_tasks_workspace_id").on(r.workspace_id),idx_tasks_workspace_ref:index("idx_tasks_workspace_ref").on(r.workspace_ref),idx_tasks_project_id:index("idx_tasks_project_id").on(r.project_id),idx_tasks_ws_started:index("idx_tasks_ws_started").on(r.workspace_id,r.started_at)}));var F=sqliteTable("task_log_events",{id:integer("id").primaryKey(),task_id:text("task_id").notNull().references(()=>n.id,{onDelete:"cascade"}),seq:integer("seq").notNull(),timestamp:text("timestamp").notNull(),level:text("level").notNull(),message:text("message").notNull()},r=>({taskSeqUnique:uniqueIndex("task_log_events_task_seq_uq").on(r.task_id,r.seq)}));var dn="json_extract(metadata, '$.threadTitle.state')",cn="json_extract(metadata, '$.threadTitle.priority')",p=sqliteTable("threads",{id:text("id").primaryKey(),workspace_id:text("workspace_id").references(()=>ft.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)},r=>({idx_threads_updated_at:index("idx_threads_updated_at").on(r.updated_at),idx_threads_workspace_id:index("idx_threads_workspace_id").on(r.workspace_id),idx_threads_ws_updated:index("idx_threads_ws_updated").on(r.workspace_id,r.updated_at),idx_threads_title:index("idx_threads_title").on(r.title),idx_threads_workspace_thread_state:index("idx_threads_workspace_thread_state").on(r.workspace_id,sql.raw(dn)),idx_threads_workspace_thread_priority:index("idx_threads_workspace_thread_priority").on(r.workspace_id,sql.raw(cn))}));var _n=sqliteTable("spans",{id:text("id").primaryKey(),task_id:text("task_id").references(()=>n.id,{onDelete:"set null"}),parent_span_id:text("parent_span_id").references(()=>_n.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")},r=>({idx_spans_task_id:index("idx_spans_task_id").on(r.task_id),idx_spans_parent_span_id:index("idx_spans_parent_span_id").on(r.parent_span_id)}));sqliteTable("tool_calls",{id:text("id").primaryKey(),task_id:text("task_id").references(()=>n.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()},r=>({idx_tool_calls_task_id:index("idx_tool_calls_task_id").on(r.task_id),idx_tool_calls_tool_name:index("idx_tool_calls_tool_name").on(r.tool_name),idx_tool_calls_timestamp:index("idx_tool_calls_timestamp").on(r.timestamp)}));sqliteTable("thread_boxes",{id:text("id").primaryKey(),thread_id:text("thread_id").notNull().references(()=>p.id,{onDelete:"cascade"}),seq:integer("seq").notNull(),first_task_id:text("first_task_id").notNull(),mid_task_id:text("mid_task_id").notNull(),last_task_id:text("last_task_id").notNull(),task_count:integer("task_count").notNull(),summary:text("summary"),source_tokens:integer("source_tokens").notNull(),summary_tokens:integer("summary_tokens"),created_at:text("created_at").notNull()},r=>({idx_thread_boxes_thread_id:index("idx_thread_boxes_thread_id").on(r.thread_id),idx_thread_boxes_seq:index("idx_thread_boxes_seq").on(r.thread_id,r.seq),uniq_thread_boxes_thread_seq:unique().on(r.thread_id,r.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")},r=>({idx_request_logs_timestamp:index("idx_request_logs_timestamp").on(r.timestamp),idx_request_logs_path:index("idx_request_logs_path").on(r.path),idx_request_logs_status_code:index("idx_request_logs_status_code").on(r.status_code),idx_request_logs_partition_key:index("idx_request_logs_partition_key").on(r.partition_key)}));sqliteTable("usage_limit_snapshots",{id:text("id").primaryKey(),provider:text("provider").notNull(),account_ref:text("account_ref").notNull().default("default"),limit_window:text("limit_window").notNull(),bucket_start:text("bucket_start").notNull(),captured_at:text("captured_at").notNull(),used_percent:integer("used_percent").notNull(),remaining_percent:integer("remaining_percent").notNull(),resets_at:text("resets_at"),source:text("source").notNull(),metadata:text("metadata")},r=>({uniq_usage_limit_snapshots_bucket:uniqueIndex("uniq_usage_limit_snapshots_bucket").on(r.provider,r.account_ref,r.limit_window,r.bucket_start),idx_usage_limit_snapshots_window_bucket:index("idx_usage_limit_snapshots_window_bucket").on(r.limit_window,r.bucket_start),idx_usage_limit_snapshots_provider_window_bucket:index("idx_usage_limit_snapshots_provider_window_bucket").on(r.provider,r.limit_window,r.bucket_start)}));sqliteTable("usage_reports",{id:text("id").primaryKey(),workspace_id:text("workspace_id").notNull(),month:text("month").notNull(),generated_at:text("generated_at").notNull(),source_range:text("source_range").notNull(),tier:text("tier").notNull(),total_tokens:integer("total_tokens").notNull(),total_cost_usd:real("total_cost_usd").notNull(),payload:text("payload").notNull(),payload_hash:text("payload_hash"),signature:text("signature"),signed_at:text("signed_at"),issuer:text("issuer")},r=>({uniq_usage_reports_ws_month:uniqueIndex("uniq_usage_reports_ws_month").on(r.workspace_id,r.month)}));sqliteTable("notifications",{id:text("id").primaryKey(),workspace_id:text("workspace_id").notNull(),agent_id:text("agent_id"),task_id:text("task_id"),thread_id:text("thread_id"),source:text("source").notNull().default("agent"),level:text("level").notNull().default("info"),title:text("title").notNull(),body:text("body"),target_user_id:text("target_user_id"),created_at:text("created_at").notNull(),metadata:text("metadata")},r=>({idx_notifications_ws_created:index("idx_notifications_ws_created").on(r.workspace_id,sql`${r.created_at} DESC`)}));sqliteTable("notification_reads",{notification_id:text("notification_id").notNull(),user_id:text("user_id").notNull(),read_at:text("read_at").notNull()},r=>({pk:primaryKey({columns:[r.notification_id,r.user_id]})}));sqliteTable("agent_suggestions",{id:text("id").primaryKey(),workspace_id:text("workspace_id").notNull(),agent_id:text("agent_id").notNull(),task_id:text("task_id"),type:text("type").notNull(),status:text("status").notNull().default("pending"),payload:text("payload").notNull(),applied_commit_sha:text("applied_commit_sha"),created_at:text("created_at").notNull(),updated_at:text("updated_at").notNull()},r=>({idx_agent_suggestions_ws_agent:index("idx_agent_suggestions_ws_agent").on(r.workspace_id,r.agent_id),idx_agent_suggestions_ws_created:index("idx_agent_suggestions_ws_created").on(r.workspace_id,r.created_at)}));sqliteTable("ui_instances",{id:text("id").primaryKey().notNull(),definition_id:text("definition_id").notNull(),definition_version:integer("definition_version").notNull(),definition_hash:text("definition_hash").notNull(),workspace_id:text("workspace_id").notNull(),thread_id:text("thread_id").notNull(),anchor_task_id:text("anchor_task_id"),domain_ref_json:text("domain_ref_json").notNull(),state_json:text("state_json").notNull(),revision:integer("revision").notNull().default(0),status:text("status").notNull().default("active"),granted_capabilities_json:text("granted_capabilities_json").notNull().default("[]"),created_at:integer("created_at").notNull(),updated_at:integer("updated_at").notNull()},r=>({idCheck:check("ui_instances_id_ck",sql`length(${r.id}) BETWEEN 5 AND 128 AND ${r.id} GLOB 'uic_*'`),definitionIdCheck:check("ui_instances_definition_id_ck",sql`length(${r.definition_id}) BETWEEN 1 AND 128`),definitionVersionCheck:check("ui_instances_definition_version_ck",sql`${r.definition_version} >= 1`),definitionHashCheck:check("ui_instances_definition_hash_ck",sql`length(${r.definition_hash}) = 64 AND ${r.definition_hash} NOT GLOB '*[^0-9a-f]*'`),workspaceIdCheck:check("ui_instances_workspace_id_ck",sql`length(${r.workspace_id}) BETWEEN 1 AND 256`),threadIdCheck:check("ui_instances_thread_id_ck",sql`length(${r.thread_id}) BETWEEN 1 AND 256`),domainRefJsonCheck:check("ui_instances_domain_ref_json_ck",sql`json_valid(${r.domain_ref_json}) AND json_type(${r.domain_ref_json}) = 'object'`),stateJsonCheck:check("ui_instances_state_json_ck",sql`json_valid(${r.state_json}) AND json_type(${r.state_json}) = 'object'`),revisionCheck:check("ui_instances_revision_ck",sql`${r.revision} >= 0`),statusCheck:check("ui_instances_status_ck",sql`${r.status} IN ('active', 'completed', 'expired', 'revoked', 'unsupported')`),grantedCapabilitiesJsonCheck:check("ui_instances_granted_capabilities_json_ck",sql`json_valid(${r.granted_capabilities_json}) AND json_type(${r.granted_capabilities_json}) = 'array'`),createdAtCheck:check("ui_instances_created_at_ck",sql`${r.created_at} >= 0`),updatedAtCheck:check("ui_instances_updated_at_ck",sql`${r.updated_at} >= ${r.created_at}`),workspaceThreadIdx:index("ui_instances_workspace_thread_idx").on(r.workspace_id,r.thread_id)}));sqliteTable("ui_action_receipts",{id:text("id").primaryKey().notNull(),instance_id:text("instance_id").notNull(),request_id:text("request_id").notNull(),action_id:text("action_id").notNull(),actor_json:text("actor_json").notNull(),source:text("source").notNull(),expected_revision:integer("expected_revision").notNull(),before_revision:integer("before_revision").notNull(),after_revision:integer("after_revision"),status:text("status").notNull(),outcome:text("outcome"),input_json:text("input_json").notNull(),result_json:text("result_json"),error_code:text("error_code"),requested_at:integer("requested_at").notNull(),settled_at:integer("settled_at")},r=>({idCheck:check("ui_action_receipts_id_ck",sql`length(${r.id}) BETWEEN 5 AND 128`),instanceIdCheck:check("ui_action_receipts_instance_id_ck",sql`length(${r.instance_id}) BETWEEN 1 AND 128`),requestIdCheck:check("ui_action_receipts_request_id_ck",sql`length(${r.request_id}) BETWEEN 1 AND 128`),actionIdCheck:check("ui_action_receipts_action_id_ck",sql`length(${r.action_id}) BETWEEN 1 AND 128`),actorJsonCheck:check("ui_action_receipts_actor_json_ck",sql`json_valid(${r.actor_json}) AND json_type(${r.actor_json}) = 'object'`),sourceCheck:check("ui_action_receipts_source_ck",sql`${r.source} IN ('ui_host', 'cli', 'system')`),expectedRevisionCheck:check("ui_action_receipts_expected_revision_ck",sql`${r.expected_revision} >= 0`),beforeRevisionCheck:check("ui_action_receipts_before_revision_ck",sql`${r.before_revision} >= 0`),afterRevisionCheck:check("ui_action_receipts_after_revision_ck",sql`${r.after_revision} IS NULL OR ${r.after_revision} >= 0`),statusCheck:check("ui_action_receipts_status_ck",sql`${r.status} IN ('requested', 'settled')`),outcomeCheck:check("ui_action_receipts_outcome_ck",sql`${r.outcome} IS NULL OR ${r.outcome} IN ('succeeded', 'rejected', 'failed', 'conflicted', 'expired')`),inputJsonCheck:check("ui_action_receipts_input_json_ck",sql`json_valid(${r.input_json}) AND json_type(${r.input_json}) = 'object'`),resultJsonCheck:check("ui_action_receipts_result_json_ck",sql`${r.result_json} IS NULL OR (json_valid(${r.result_json}) AND json_type(${r.result_json}) = 'object')`),requestedAtCheck:check("ui_action_receipts_requested_at_ck",sql`${r.requested_at} >= 0`),settledAtCheck:check("ui_action_receipts_settled_at_ck",sql`${r.settled_at} IS NULL OR ${r.settled_at} >= ${r.requested_at}`),statusTransitionCheck:check("ui_action_receipts_status_transition_ck",sql`(
14
14
  (${r.status} = 'requested' AND ${r.after_revision} IS NULL AND ${r.outcome} IS NULL AND ${r.settled_at} IS NULL)
15
15
  OR
16
16
  (${r.status} = 'settled' AND ${r.after_revision} IS NOT NULL AND ${r.outcome} IS NOT NULL AND ${r.settled_at} IS NOT NULL)
17
- )`),instanceRequestUnique:uniqueIndex("ui_action_receipts_instance_request_uq").on(r.instance_id,r.request_id),instanceIdIdx:index("ui_action_receipts_instance_id_idx").on(r.instance_id),requestedAtIdx:index("ui_action_receipts_requested_at_idx").on(r.requested_at)}));var ue=sqliteTable("projects",{id:text("id").primaryKey().notNull(),workspace_id:text("workspace_id").notNull(),name:text("name").notNull(),outcome:text("outcome").notNull(),due_date:text("due_date"),scene:text("scene").notNull().default("planning"),stages_json:text("stages_json").notNull(),stage_index:integer("stage_index").notNull(),created_at:text("created_at").notNull(),updated_at:text("updated_at").notNull(),accepted_at:text("accepted_at"),archived_at:text("archived_at")},r=>({idCheck:check("projects_id_ck",sql`${r.id} GLOB 'prj_*'`),sceneCheck:check("projects_scene_ck",sql`${r.scene} IN ('planning', 'plan_review', 'running', 'human_wait', 'limit_wait', 'failed_stop', 'analyzing_feedback', 'replanned', 'awaiting_review', 'accepted')`),stagesJsonCheck:check("projects_stages_json_ck",sql`json_valid(${r.stages_json}) AND json_type(${r.stages_json}) = 'array'`),stageIndexCheck:check("projects_stage_index_ck",sql`${r.stage_index} >= 1`)}));sqliteTable("project_criteria",{id:text("id").primaryKey().notNull(),project_id:text("project_id").notNull().references(()=>ue.id,{onDelete:"cascade"}),position:integer("position").notNull(),statement:text("statement").notNull(),verify_status:text("verify_status").notNull().default("pending"),verified_at:text("verified_at"),evidence_json:text("evidence_json").notNull().default("[]"),created_at:text("created_at").notNull(),updated_at:text("updated_at").notNull()},r=>({idCheck:check("project_criteria_id_ck",sql`${r.id} GLOB 'pjc_*'`),verifyStatusCheck:check("project_criteria_verify_status_ck",sql`${r.verify_status} IN ('pending', 'verifying', 'passed', 'failed')`),evidenceJsonCheck:check("project_criteria_evidence_json_ck",sql`json_valid(${r.evidence_json}) AND json_type(${r.evidence_json}) = 'array'`),projectPositionUnique:uniqueIndex("project_criteria_project_position_uq").on(r.project_id,r.position)}));sqliteTable("project_events",{id:text("id").primaryKey().notNull(),project_id:text("project_id").notNull().references(()=>ue.id,{onDelete:"restrict"}),seq:integer("seq").notNull(),actor_type:text("actor_type").notNull(),actor_id:text("actor_id"),type:text("type").notNull(),title:text("title").notNull(),summary:text("summary"),payload_json:text("payload_json").notNull().default("{}"),created_at:text("created_at").notNull()},r=>({idCheck:check("project_events_id_ck",sql`${r.id} GLOB 'pje_*'`),actorTypeCheck:check("project_events_actor_type_ck",sql`${r.actor_type} IN ('user', 'project_manager', 'system')`),payloadJsonCheck:check("project_events_payload_json_ck",sql`json_valid(${r.payload_json}) AND json_type(${r.payload_json}) = 'object'`),projectSeqUnique:uniqueIndex("project_events_project_seq_uq").on(r.project_id,r.seq)}));function _e(r){let t=r.toLowerCase().trim();t=t.replace(/\[[^\]]*\]$/g,"");let e=t.lastIndexOf("/");return e>=0&&(t=t.slice(e+1)),t=t.replace(/-\d{8}$/,""),t}function he(r,t){let e=t===void 0?"":` at index ${t}`;if(!r||typeof r!="object")throw new _("VALIDATION",`Task log entry${e} must be an object`);if(typeof r.timestamp!="string"||typeof r.level!="string"||typeof r.message!="string")throw new _("VALIDATION",`Task log entry${e} must contain string timestamp, level, and message fields`);return {timestamp:r.timestamp,level:r.level,message:r.message}}function fe(r,t){if(t===null||t.trim()==="")return [];let e;try{e=JSON.parse(t);}catch(s){throw new _("DB_ERROR",`Malformed legacy task logs: ${r}`,s)}if(!Array.isArray(e))throw new _("DB_ERROR",`Malformed legacy task logs: ${r} (expected an array)`);try{return e.map((s,o)=>he(s,o))}catch(s){throw s instanceof _?new _("DB_ERROR",`Malformed legacy task logs: ${r}`,s):s}}function ke(r){if(r.log_storage!=="blob"&&r.log_storage!=="events")throw new _("DB_ERROR",`Invalid task log storage for task: ${r.id}`);if(!Number.isInteger(r.last_log_seq)||r.last_log_seq<0)throw new _("DB_ERROR",`Invalid task log sequence for task: ${r.id}`);if(!Number.isInteger(r.log_revision)||r.log_revision<0)throw new _("DB_ERROR",`Invalid task log revision for task: ${r.id}`);return r.log_storage}function me(r){return {taskId:r.id,source:ke(r),revision:r.log_revision,lastSeq:r.last_log_seq}}function $n(r){if(!Number.isInteger(r)||r<0)throw new _("VALIDATION","Task log tail limit must be a non-negative integer")}var Ct=class extends at{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 o=dirname(e);existsSync(o)||mkdirSync(o,{recursive:true});}else if(!existsSync(e))throw new _("NOT_FOUND","Database not found");let s=N(e);if(t)try{pt(s.db,e);}catch(o){throw s.close(),o}return s}findStateRow(t,e){return t.db.select({id:n.id,log_storage:n.log_storage,last_log_seq:n.last_log_seq,log_revision:n.log_revision,logs:n.logs}).from(n).where(eq(n.id,e)).limit(1).get()}findStateRowInTransaction(t,e){return t.select({id:n.id,log_storage:n.log_storage,last_log_seq:n.last_log_seq,log_revision:n.log_revision,logs:n.logs}).from(n).where(eq(n.id,e)).limit(1).get()}requireStateRowInTransaction(t,e){let s=this.findStateRowInTransaction(t,e);if(!s)throw new _("NOT_FOUND",`Task not found: ${e}`);return s}appendLog(t,e){return this.appendLogs(t,[e])}appendLogs(t,e){let s=e.map((i,d)=>he(i,d));if(s.length===0){let i=this.getLogState(t);if(!i)throw new _("NOT_FOUND",`Task not found: ${t}`);return i}let o=this.openHandle(true);try{return o.db.transaction(d=>{let a=this.requireStateRowInTransaction(d,t),u=ke(a);if(u==="blob"){let f=fe(t,a.logs);if(d.update(n).set({logs:JSON.stringify([...f,...s]),log_revision:sql`${n.log_revision} + 1`}).where(eq(n.id,t)).run().changes!==1)throw new _("NOT_FOUND",`Task not found: ${t}`);return {taskId:t,source:u,revision:a.log_revision+1,lastSeq:a.last_log_seq}}let c=a.last_log_seq+1,l=a.last_log_seq+s.length;if(d.insert(W).values(s.map((f,w)=>({task_id:t,seq:c+w,timestamp:f.timestamp,level:f.level,message:f.message}))).run(),d.update(n).set({last_log_seq:l,log_revision:sql`${n.log_revision} + 1`}).where(eq(n.id,t)).run().changes!==1)throw new _("NOT_FOUND",`Task not found: ${t}`);return {taskId:t,source:u,revision:a.log_revision+1,lastSeq:l}},{behavior:"immediate"})}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to append task log",i)}finally{o.close();}}getLogState(t){if(!this.dbExists())return;let e=this.openHandle(false);try{let s=this.findStateRow(e,t);return s?me(s):void 0}catch(s){throw s instanceof _?s:new _("DB_ERROR","Failed to read task log state",s)}finally{e.close();}}readLogs(t){return this.readLogsInternal(t)}readLogsTail(t,e){return $n(e),this.readLogsInternal(t,e)}readTail(t,e){return this.readLogsTail(t,e)}readLogsInternal(t,e){if(!this.dbExists())return;let s=this.openHandle(false);try{return s.db.transaction(o=>{let i=this.findStateRowInTransaction(o,t);if(!i)return;let d=me(i);if(d.source==="blob"){let l=fe(t,i.logs);return {...d,entries:e===void 0?l:l.slice(Math.max(0,l.length-e))}}let a=o.select({seq:W.seq,timestamp:W.timestamp,level:W.level,message:W.message}).from(W).where(eq(W.task_id,t)).orderBy(e===void 0?asc(W.seq):desc(W.seq)),c=(e===void 0?a.all():a.limit(e).all()).map(({timestamp:l,level:g,message:f})=>({timestamp:l,level:g,message:f}));return e!==void 0&&c.reverse(),{...d,entries:c}},{behavior:"deferred"})}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to read task logs",o)}finally{s.close();}}};var Ft=40,st="2026-05-09",Mn="0.8.9-rc.13",rt=10,ot=parseInt(Mn.split("rc.")[1]);function we(){let r=sql`
17
+ )`),instanceRequestUnique:uniqueIndex("ui_action_receipts_instance_request_uq").on(r.instance_id,r.request_id),instanceIdIdx:index("ui_action_receipts_instance_id_idx").on(r.instance_id),requestedAtIdx:index("ui_action_receipts_requested_at_idx").on(r.requested_at)}));var ke=sqliteTable("projects",{id:text("id").primaryKey().notNull(),workspace_id:text("workspace_id").notNull(),name:text("name").notNull(),outcome:text("outcome").notNull(),due_date:text("due_date"),scene:text("scene").notNull().default("planning"),stages_json:text("stages_json").notNull(),stage_index:integer("stage_index").notNull(),created_at:text("created_at").notNull(),updated_at:text("updated_at").notNull(),accepted_at:text("accepted_at"),archived_at:text("archived_at")},r=>({idCheck:check("projects_id_ck",sql`${r.id} GLOB 'prj_*'`),sceneCheck:check("projects_scene_ck",sql`${r.scene} IN ('planning', 'plan_review', 'running', 'human_wait', 'limit_wait', 'failed_stop', 'analyzing_feedback', 'replanned', 'awaiting_review', 'accepted')`),stagesJsonCheck:check("projects_stages_json_ck",sql`json_valid(${r.stages_json}) AND json_type(${r.stages_json}) = 'array'`),stageIndexCheck:check("projects_stage_index_ck",sql`${r.stage_index} >= 1`)}));sqliteTable("project_criteria",{id:text("id").primaryKey().notNull(),project_id:text("project_id").notNull().references(()=>ke.id,{onDelete:"cascade"}),position:integer("position").notNull(),statement:text("statement").notNull(),verify_status:text("verify_status").notNull().default("pending"),verified_at:text("verified_at"),evidence_json:text("evidence_json").notNull().default("[]"),created_at:text("created_at").notNull(),updated_at:text("updated_at").notNull()},r=>({idCheck:check("project_criteria_id_ck",sql`${r.id} GLOB 'pjc_*'`),verifyStatusCheck:check("project_criteria_verify_status_ck",sql`${r.verify_status} IN ('pending', 'verifying', 'passed', 'failed')`),evidenceJsonCheck:check("project_criteria_evidence_json_ck",sql`json_valid(${r.evidence_json}) AND json_type(${r.evidence_json}) = 'array'`),projectPositionUnique:uniqueIndex("project_criteria_project_position_uq").on(r.project_id,r.position)}));sqliteTable("project_events",{id:text("id").primaryKey().notNull(),project_id:text("project_id").notNull().references(()=>ke.id,{onDelete:"restrict"}),seq:integer("seq").notNull(),actor_type:text("actor_type").notNull(),actor_id:text("actor_id"),type:text("type").notNull(),title:text("title").notNull(),summary:text("summary"),payload_json:text("payload_json").notNull().default("{}"),created_at:text("created_at").notNull()},r=>({idCheck:check("project_events_id_ck",sql`${r.id} GLOB 'pje_*'`),actorTypeCheck:check("project_events_actor_type_ck",sql`${r.actor_type} IN ('user', 'project_manager', 'system')`),payloadJsonCheck:check("project_events_payload_json_ck",sql`json_valid(${r.payload_json}) AND json_type(${r.payload_json}) = 'object'`),projectSeqUnique:uniqueIndex("project_events_project_seq_uq").on(r.project_id,r.seq)}));sqliteTable("common_codes",{code_group:text("code_group").notNull(),code:text("code").notNull(),code_name:text("code_name"),color:text("color").notNull().default("gray"),sort_order:integer("sort_order").notNull(),workspace_id:text("workspace_id"),is_builtin:integer("is_builtin").notNull().default(0),is_archived:integer("is_archived").notNull().default(0),metadata:text("metadata"),created_at:text("created_at").notNull(),updated_at:text("updated_at").notNull()},r=>({pk:primaryKey({columns:[r.code_group,r.code]}),metadataCheck:check("common_codes_metadata_ck",sql`${r.metadata} IS NULL OR (json_valid(${r.metadata}) AND json_type(${r.metadata}) = 'object')`),builtinCheck:check("common_codes_builtin_ck",sql`${r.is_builtin} IN (0, 1)`),archivedCheck:check("common_codes_archived_ck",sql`${r.is_archived} IN (0, 1)`),idxCommonCodesSelection:index("idx_common_codes_group_archived_order").on(r.code_group,r.is_archived,r.sort_order)}));function we(r){let t=r.toLowerCase().trim();t=t.replace(/\[[^\]]*\]$/g,"");let e=t.lastIndexOf("/");return e>=0&&(t=t.slice(e+1)),t=t.replace(/-\d{8}$/,""),t}function Se(r,t){let e=t===void 0?"":` at index ${t}`;if(!r||typeof r!="object")throw new _("VALIDATION",`Task log entry${e} must be an object`);if(typeof r.timestamp!="string"||typeof r.level!="string"||typeof r.message!="string")throw new _("VALIDATION",`Task log entry${e} must contain string timestamp, level, and message fields`);return {timestamp:r.timestamp,level:r.level,message:r.message}}function ye(r,t){if(t===null||t.trim()==="")return [];let e;try{e=JSON.parse(t);}catch(s){throw new _("DB_ERROR",`Malformed legacy task logs: ${r}`,s)}if(!Array.isArray(e))throw new _("DB_ERROR",`Malformed legacy task logs: ${r} (expected an array)`);try{return e.map((s,o)=>Se(s,o))}catch(s){throw s instanceof _?new _("DB_ERROR",`Malformed legacy task logs: ${r}`,s):s}}function Ne(r){if(r.log_storage!=="blob"&&r.log_storage!=="events")throw new _("DB_ERROR",`Invalid task log storage for task: ${r.id}`);if(!Number.isInteger(r.last_log_seq)||r.last_log_seq<0)throw new _("DB_ERROR",`Invalid task log sequence for task: ${r.id}`);if(!Number.isInteger(r.log_revision)||r.log_revision<0)throw new _("DB_ERROR",`Invalid task log revision for task: ${r.id}`);return r.log_storage}function Re(r){return {taskId:r.id,source:Ne(r),revision:r.log_revision,lastSeq:r.last_log_seq}}function Fn(r){if(!Number.isInteger(r)||r<0)throw new _("VALIDATION","Task log tail limit must be a non-negative integer")}var Lt=class extends ot{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 o=dirname(e);existsSync(o)||mkdirSync(o,{recursive:true});}else if(!existsSync(e))throw new _("NOT_FOUND","Database not found");let s=N(e);if(t)try{gt(s.db,e);}catch(o){throw s.close(),o}return s}findStateRow(t,e){return t.db.select({id:n.id,log_storage:n.log_storage,last_log_seq:n.last_log_seq,log_revision:n.log_revision,logs:n.logs}).from(n).where(eq(n.id,e)).limit(1).get()}findStateRowInTransaction(t,e){return t.select({id:n.id,log_storage:n.log_storage,last_log_seq:n.last_log_seq,log_revision:n.log_revision,logs:n.logs}).from(n).where(eq(n.id,e)).limit(1).get()}requireStateRowInTransaction(t,e){let s=this.findStateRowInTransaction(t,e);if(!s)throw new _("NOT_FOUND",`Task not found: ${e}`);return s}appendLog(t,e){return this.appendLogs(t,[e])}appendLogs(t,e){let s=e.map((i,a)=>Se(i,a));if(s.length===0){let i=this.getLogState(t);if(!i)throw new _("NOT_FOUND",`Task not found: ${t}`);return i}let o=this.openHandle(true);try{return o.db.transaction(a=>{let d=this.requireStateRowInTransaction(a,t),l=Ne(d);if(l==="blob"){let f=ye(t,d.logs);if(a.update(n).set({logs:JSON.stringify([...f,...s]),log_revision:sql`${n.log_revision} + 1`}).where(eq(n.id,t)).run().changes!==1)throw new _("NOT_FOUND",`Task not found: ${t}`);return {taskId:t,source:l,revision:d.log_revision+1,lastSeq:d.last_log_seq}}let c=d.last_log_seq+1,u=d.last_log_seq+s.length;if(a.insert(F).values(s.map((f,m)=>({task_id:t,seq:c+m,timestamp:f.timestamp,level:f.level,message:f.message}))).run(),a.update(n).set({last_log_seq:u,log_revision:sql`${n.log_revision} + 1`}).where(eq(n.id,t)).run().changes!==1)throw new _("NOT_FOUND",`Task not found: ${t}`);return {taskId:t,source:l,revision:d.log_revision+1,lastSeq:u}},{behavior:"immediate"})}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to append task log",i)}finally{o.close();}}getLogState(t){if(!this.dbExists())return;let e=this.openHandle(false);try{let s=this.findStateRow(e,t);return s?Re(s):void 0}catch(s){throw s instanceof _?s:new _("DB_ERROR","Failed to read task log state",s)}finally{e.close();}}readLogs(t){return this.readLogsInternal(t)}readLogsTail(t,e){return Fn(e),this.readLogsInternal(t,e)}readTail(t,e){return this.readLogsTail(t,e)}readLogsInternal(t,e){if(!this.dbExists())return;let s=this.openHandle(false);try{return s.db.transaction(o=>{let i=this.findStateRowInTransaction(o,t);if(!i)return;let a=Re(i);if(a.source==="blob"){let u=ye(t,i.logs);return {...a,entries:e===void 0?u:u.slice(Math.max(0,u.length-e))}}let d=o.select({seq:F.seq,timestamp:F.timestamp,level:F.level,message:F.message}).from(F).where(eq(F.task_id,t)).orderBy(e===void 0?asc(F.seq):desc(F.seq)),c=(e===void 0?d.all():d.limit(e).all()).map(({timestamp:u,level:g,message:f})=>({timestamp:u,level:g,message:f}));return e!==void 0&&c.reverse(),{...a,entries:c}},{behavior:"deferred"})}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to read task logs",o)}finally{s.close();}}};var Jt=40,et="2026-05-09",Jn="0.8.9-rc.13",nt=10,st=parseInt(Jn.split("rc.")[1]);function be(){let r=sql`
18
18
  CASE
19
19
  WHEN ${n.model} LIKE 'claude-%' OR ${n.model} IN ('opus', 'sonnet', 'haiku', 'opus[1m]', 'sonnet[1m]') THEN 'claude'
20
20
  WHEN ${n.model} LIKE 'gpt-%' OR ${n.model} LIKE 'codex-%' THEN 'codex'
@@ -43,7 +43,7 @@ ${s.join(`
43
43
  WHEN trim(${o}) = '' THEN 'unknown'
44
44
  ELSE ${o}
45
45
  END
46
- `}var Un=new Set(["workflow","mcp"]),Ee=600*1e3,Dt=class extends at{dbPath;taskLogRepository;pidNullObservedSince=new Map;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=join(t.dbRoot,".crewx","crewx.db")),this.taskLogRepository=new Ct({dbPath:this.dbPath});}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let o=dirname(e);existsSync(o)||mkdirSync(o,{recursive:true});}else if(!existsSync(e))throw new _("NOT_FOUND","Database not found");let s=N(e);if(t)try{pt(s.db,e);}catch(o){throw s.close(),o}return s}startTask(t){let e=this.openHandle(true);try{let s={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,project_id:t.projectId??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,log_storage:"events"},o=i=>{e.db.insert(n).values(i).onConflictDoUpdate({target:n.id,set:{pid:sql`COALESCE(excluded.pid, ${n.pid})`},setWhere:eq(n.status,"running")}).run();};try{o(s);}catch(i){let d=i instanceof Error?i.message:typeof i=="object"&&i!==null&&"message"in i?String(i.message):String(i);if(!/log_storage/i.test(d)||!/no such column|no column named/i.test(d))throw i;e.runRaw(`INSERT INTO tasks (
46
+ `}var Yn=new Set(["workflow","mcp"]),xe=600*1e3,It=class extends ot{dbPath;taskLogRepository;pidNullObservedSince=new Map;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=join(t.dbRoot,".crewx","crewx.db")),this.taskLogRepository=new Lt({dbPath:this.dbPath});}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let o=dirname(e);existsSync(o)||mkdirSync(o,{recursive:true});}else if(!existsSync(e))throw new _("NOT_FOUND","Database not found");let s=N(e);if(t)try{gt(s.db,e);}catch(o){throw s.close(),o}return s}startTask(t){let e=this.openHandle(true);try{let s={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,project_id:t.projectId??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,log_storage:"events"},o=i=>{e.db.insert(n).values(i).onConflictDoUpdate({target:n.id,set:{pid:sql`COALESCE(excluded.pid, ${n.pid})`},setWhere:eq(n.status,"running")}).run();};try{o(s);}catch(i){let a=i instanceof Error?i.message:typeof i=="object"&&i!==null&&"message"in i?String(i.message):String(i);if(!/log_storage/i.test(a)||!/no such column|no column named/i.test(a))throw i;e.runRaw(`INSERT INTO tasks (
47
47
  id, agent_id, prompt, mode, status, started_at, pid,
48
48
  parent_task_id, caller_agent_id, trace_id, command, metadata,
49
49
  workspace_id, project_id, platform, crewx_version, thread_id,
@@ -53,7 +53,7 @@ ${s.join(`
53
53
  WHERE tasks.status = 'running'`,[s.id,s.agent_id,s.prompt,s.mode,s.status,s.started_at,s.pid,s.parent_task_id,s.caller_agent_id,s.trace_id,s.command,s.metadata,s.workspace_id,s.project_id,s.platform,s.crewx_version,s.thread_id,s.model,s.rendered_prompt,s.coding_agent_command]);}}catch(s){throw s instanceof _?s:new _("DB_ERROR","Failed to start task",s)}finally{e.close();}}listTasksByProjectId(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.select().from(n).where(eq(n.project_id,t)).orderBy(desc(n.started_at)).all()}catch(s){throw s instanceof _?s:new _("DB_ERROR","Failed to list tasks by project",s)}finally{e.close();}}finishTask(t){let e=this.openHandle(true);try{let s=t.runEpoch??null;e.runRaw(`UPDATE tasks SET status=?, result=?, error=?, completed_at=?, duration_ms=?,
54
54
  exit_code=?, input_tokens=?, output_tokens=?, cached_input_tokens=?, cost_usd=?,
55
55
  model=COALESCE(?, model)
56
- WHERE id=? AND status='running' AND COALESCE(run_epoch, 0) = COALESCE(?, 0)`,[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,s]);}catch(s){throw s instanceof _?s:new _("DB_ERROR","Failed to finish task",s)}finally{e.close();}}appendLog(t,e){this.taskLogRepository.appendLog(t,e);}appendLogs(t,e){this.taskLogRepository.appendLogs(t,e);}readLogs(t){return this.taskLogRepository.readLogs(t)}readLogsTail(t,e){return this.taskLogRepository.readLogsTail(t,e)}getLogState(t){return this.taskLogRepository.getLogState(t)}getRunningTasks(){if(!this.dbExists())return [];let t=this.openHandle(false);try{return t.db.select().from(n).where(eq(n.status,"running")).orderBy(desc(n.started_at)).all()}catch(e){throw new _("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(n).orderBy(desc(n.started_at)).limit(100).all()}catch(e){throw new _("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(n).where(eq(n.id,t)).limit(1).get()??void 0}catch(s){throw new _("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:n.id,status:n.status,pid:n.pid}).from(n).where(eq(n.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(n).set({status:"failed",error:"Killed by user",completed_at:new Date().toISOString()}).where(and(eq(n.id,t),eq(n.status,"running"))).run(),{killed:!0,pid:s.pid??void 0}}catch(s){throw s instanceof _?s:new _("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:n.id,pid:n.pid,platform:n.platform,run_epoch:n.run_epoch}).from(n).where(eq(n.status,"running")).all(),s=Date.now(),o=new Set,i=0;for(let d of e){if(d.pid){let c=!1;try{process.kill(d.pid,0),c=!0;}catch{}c||(t.db.update(n).set({status:"failed",error:"Reaped: process not found (orphaned task)",completed_at:new Date().toISOString()}).where(and(eq(n.id,d.id),eq(n.status,"running"))).run(),i++);continue}if(Un.has(d.platform??"cli"))continue;let a=`${d.id}:${d.run_epoch??0}`;o.add(a);let u=this.pidNullObservedSince.get(a);if(u===void 0){this.pidNullObservedSince.set(a,s);continue}s-u>=Ee&&(t.db.update(n).set({status:"failed",error:"Reaped: pid never recorded after resume (stale running_instruction)",completed_at:new Date().toISOString()}).where(and(eq(n.id,d.id),eq(n.status,"running"))).run(),i++,this.pidNullObservedSince.delete(a));}for(let d of Array.from(this.pidNullObservedSince.keys()))o.has(d)||this.pidNullObservedSince.delete(d);return i}finally{t.close();}}reapRunningWorkflowTasks(t=Ee){if(!this.dbExists())return 0;let e=this.openHandle(true);try{let s=new Date(Date.now()-t).toISOString();return e.db.update(n).set({status:"failed",error:"Reaped: stale workflow run (finally not executed)",completed_at:new Date().toISOString()}).where(and(eq(n.status,"running"),eq(n.platform,"workflow"),isNull(n.pid),lt(n.started_at,s),or(isNull(n.metadata),and(notLike(n.metadata,'%"kind":"workflow_card"%'),notLike(n.metadata,'%"kind":"workflow_ui_instance"%'))))).run().changes??0}finally{e.close();}}findTaskStatus(t,e){let s=this.resolveDbPaths();for(let o of s){if(!existsSync(o))continue;let i=N(o);try{let d=e?eq(n.workspace_id,e):void 0,a=d?and(eq(n.id,t),d):eq(n.id,t),u=i.db.select().from(n).where(a).limit(1).get()??void 0;if(!u){let c=or(eq(n.thread_id,t),and(isNull(n.thread_id),like(n.command,`%--thread=${t}%`))),l=d?and(c,d):c;u=i.db.select().from(n).where(l).orderBy(desc(n.started_at)).limit(1).get()??void 0;}if(u)return u}catch(d){throw new _("DB_ERROR","Failed to find task status",d)}finally{i.close();}}}findChildTasks(t,e){let s=this.resolveDbPaths(),o=new Set,i=[];for(let d of s){if(!existsSync(d))continue;let a=N(d);try{let u=e?and(eq(n.parent_task_id,t),eq(n.workspace_id,e)):eq(n.parent_task_id,t),c=a.db.select().from(n).where(u).orderBy(asc(n.started_at)).all();for(let l of c)o.has(l.id)||(o.add(l.id),i.push(l));}catch(u){throw new _("DB_ERROR","Failed to find child tasks",u)}finally{a.close();}}return i}getWorkspaceUsageSummary(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.all(t?sql`
56
+ WHERE id=? AND status='running' AND COALESCE(run_epoch, 0) = COALESCE(?, 0)`,[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,s]);}catch(s){throw s instanceof _?s:new _("DB_ERROR","Failed to finish task",s)}finally{e.close();}}appendLog(t,e){this.taskLogRepository.appendLog(t,e);}appendLogs(t,e){this.taskLogRepository.appendLogs(t,e);}readLogs(t){return this.taskLogRepository.readLogs(t)}readLogsTail(t,e){return this.taskLogRepository.readLogsTail(t,e)}getLogState(t){return this.taskLogRepository.getLogState(t)}getRunningTasks(){if(!this.dbExists())return [];let t=this.openHandle(false);try{return t.db.select().from(n).where(eq(n.status,"running")).orderBy(desc(n.started_at)).all()}catch(e){throw new _("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(n).orderBy(desc(n.started_at)).limit(100).all()}catch(e){throw new _("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(n).where(eq(n.id,t)).limit(1).get()??void 0}catch(s){throw new _("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:n.id,status:n.status,pid:n.pid}).from(n).where(eq(n.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(n).set({status:"failed",error:"Killed by user",completed_at:new Date().toISOString()}).where(and(eq(n.id,t),eq(n.status,"running"))).run(),{killed:!0,pid:s.pid??void 0}}catch(s){throw s instanceof _?s:new _("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:n.id,pid:n.pid,platform:n.platform,run_epoch:n.run_epoch}).from(n).where(eq(n.status,"running")).all(),s=Date.now(),o=new Set,i=0;for(let a of e){if(a.pid){let c=!1;try{process.kill(a.pid,0),c=!0;}catch{}c||(t.db.update(n).set({status:"failed",error:"Reaped: process not found (orphaned task)",completed_at:new Date().toISOString()}).where(and(eq(n.id,a.id),eq(n.status,"running"))).run(),i++);continue}if(Yn.has(a.platform??"cli"))continue;let d=`${a.id}:${a.run_epoch??0}`;o.add(d);let l=this.pidNullObservedSince.get(d);if(l===void 0){this.pidNullObservedSince.set(d,s);continue}s-l>=xe&&(t.db.update(n).set({status:"failed",error:"Reaped: pid never recorded after resume (stale running_instruction)",completed_at:new Date().toISOString()}).where(and(eq(n.id,a.id),eq(n.status,"running"))).run(),i++,this.pidNullObservedSince.delete(d));}for(let a of Array.from(this.pidNullObservedSince.keys()))o.has(a)||this.pidNullObservedSince.delete(a);return i}finally{t.close();}}reapRunningWorkflowTasks(t=xe){if(!this.dbExists())return 0;let e=this.openHandle(true);try{let s=new Date(Date.now()-t).toISOString();return e.db.update(n).set({status:"failed",error:"Reaped: stale workflow run (finally not executed)",completed_at:new Date().toISOString()}).where(and(eq(n.status,"running"),eq(n.platform,"workflow"),isNull(n.pid),lt(n.started_at,s),or(isNull(n.metadata),and(notLike(n.metadata,'%"kind":"workflow_card"%'),notLike(n.metadata,'%"kind":"workflow_ui_instance"%'))))).run().changes??0}finally{e.close();}}findTaskStatus(t,e){let s=this.resolveDbPaths();for(let o of s){if(!existsSync(o))continue;let i=N(o);try{let a=e?eq(n.workspace_id,e):void 0,d=a?and(eq(n.id,t),a):eq(n.id,t),l=i.db.select().from(n).where(d).limit(1).get()??void 0;if(!l){let c=a?and(eq(n.thread_id,t),a):eq(n.thread_id,t);l=i.db.select().from(n).where(c).orderBy(desc(n.started_at)).limit(1).get()??void 0;}if(l)return l}catch(a){throw new _("DB_ERROR","Failed to find task status",a)}finally{i.close();}}}findChildTasks(t,e){let s=this.resolveDbPaths(),o=new Set,i=[];for(let a of s){if(!existsSync(a))continue;let d=N(a);try{let l=e?and(eq(n.parent_task_id,t),eq(n.workspace_id,e)):eq(n.parent_task_id,t),c=d.db.select().from(n).where(l).orderBy(asc(n.started_at)).all();for(let u of c)o.has(u.id)||(o.add(u.id),i.push(u));}catch(l){throw new _("DB_ERROR","Failed to find child tasks",l)}finally{d.close();}}return i}getWorkspaceUsageSummary(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.all(t?sql`
57
57
  SELECT
58
58
  COALESCE(workspace_id, 'unknown') AS workspace_id,
59
59
  COALESCE(SUM(input_tokens), 0) AS input_tokens,
@@ -74,7 +74,7 @@ ${s.join(`
74
74
  FROM tasks
75
75
  GROUP BY workspace_id
76
76
  ORDER BY (COALESCE(SUM(input_tokens), 0) + COALESCE(SUM(output_tokens), 0)) DESC
77
- `)}catch(s){throw new _("DB_ERROR","Failed to get workspace usage summary",s)}finally{e.close();}}getThreadTokenUsage(t,e){let s=this.resolveDbPaths(),o=new Set,i=0,d=0,a=0;for(let u of s){if(!existsSync(u))continue;let c=N(u);try{let l=or(eq(n.thread_id,t),and(isNull(n.thread_id),like(n.command,`%--thread=${t}%`))),g=e?and(l,eq(n.workspace_id,e)):l,f=c.db.select({id:n.id,input_tokens:n.input_tokens,output_tokens:n.output_tokens,cost_usd:n.cost_usd}).from(n).where(g).all();for(let w of f)o.has(w.id)||(o.add(w.id),i+=w.input_tokens??0,d+=w.output_tokens??0,a+=w.cost_usd??0);}catch(l){throw new _("DB_ERROR","Failed to get thread token usage",l)}finally{c.close();}}return {inputTokens:i,outputTokens:d,costUsd:a}}findTasksByThread(t,e){let s=this.resolveDbPaths(),o=new Set,i=[];for(let d of s){if(!existsSync(d))continue;let a=N(d);try{let u=or(eq(n.thread_id,t),and(isNull(n.thread_id),like(n.command,`%--thread=${t}%`))),c=e?and(u,eq(n.workspace_id,e)):u,l=a.db.select().from(n).where(c).orderBy(asc(n.started_at)).all();for(let g of l)o.has(g.id)||(o.add(g.id),i.push(g));}catch(u){throw new _("DB_ERROR","Failed to find tasks by thread",u)}finally{a.close();}}return i}parseWorkflowCardMetadata(t){if(!t)return {};try{let e=JSON.parse(t);return {kind:typeof e.kind=="string"?e.kind:void 0,workflowRunId:typeof e.workflowRunId=="string"?e.workflowRunId:void 0}}catch{return {}}}extractThreadIdFromCommand(t){return t?/--thread=(\S+)/.exec(t)?.[1]??null:null}findRunningWorkflowParentTasks(){if(!this.dbExists())return [];let t=[];for(let e of this.resolveDbPaths()){if(!existsSync(e))continue;let s=N(e);try{t.push(...s.db.select({id:n.id,agent_id:n.agent_id,thread_id:n.thread_id,workspace_id:n.workspace_id,platform:n.platform,status:n.status,error:n.error,completed_at:n.completed_at,duration_ms:n.duration_ms,started_at:n.started_at,metadata:n.metadata}).from(n).where(and(eq(n.platform,"workflow"),or(like(n.metadata,'%"kind":"workflow_card"%'),like(n.metadata,'%"kind":"workflow_ui_instance"%')))).orderBy(desc(n.started_at)).all());}catch(o){throw new _("DB_ERROR","Failed to find workflow parent tasks",o)}finally{s.close();}}return t}batchFetchWorkflowCards(t,e){let s=new Map;if(t.length===0)return s;let o=new Set(t),i=new Map,d=e?eq(n.workspace_id,e):void 0,a=(u,c)=>{if(!u)return;let l=i.get(u);if(l&&c.started_at<=l)return;let g=this.parseWorkflowCardMetadata(c.metadata);g.kind!=="workflow_card"&&g.kind!=="workflow_ui_instance"||(i.set(u,c.started_at),s.set(u,{taskId:c.id,workflowRunId:g.workflowRunId,raw:c.result??void 0}));};for(let u of this.resolveDbPaths()){if(!existsSync(u))continue;let c=N(u);try{let l=and(inArray(n.thread_id,t),or(like(n.metadata,'%"kind":"workflow_card"%'),like(n.metadata,'%"kind":"workflow_ui_instance"%'))),g=c.db.select({id:n.id,thread_id:n.thread_id,metadata:n.metadata,result:n.result,started_at:n.started_at}).from(n).where(d?and(l,d):l).all();for(let L of g)a(L.thread_id,L);let f=and(isNull(n.thread_id),or(like(n.metadata,'%"kind":"workflow_card"%'),like(n.metadata,'%"kind":"workflow_ui_instance"%'))),w=c.db.select({id:n.id,command:n.command,metadata:n.metadata,result:n.result,started_at:n.started_at}).from(n).where(d?and(f,d):f).all();for(let L of w){let O=this.extractThreadIdFromCommand(L.command);!O||!o.has(O)||a(O,L);}}catch(l){throw new _("DB_ERROR","Failed to batch fetch workflow cards",l)}finally{c.close();}}return s}findAllTasks(t){if(!this.dbExists())return {rows:[],total:0};let e=this.openHandle(false);try{let s=[];t.workspaceId&&s.push(eq(n.workspace_id,t.workspaceId));let o=t.agents&&t.agents.length>0?t.agents:t.agentId?[t.agentId]:null;o&&s.push(inArray(n.agent_id,o));let i=t.statuses&&t.statuses.length>0?t.statuses:t.status?[t.status]:null;i&&s.push(inArray(n.status,i));let d=t.q??t.search;d&&s.push(like(n.prompt,`%${d}%`)),t.from&&s.push(gte(n.started_at,t.from)),t.to&&s.push(lt(n.started_at,t.to));let a=s.length>0?and(...s):void 0,u=e.db.select({count:sql`count(*)`}).from(n).where(a).get(),c=(t.sortDir??"DESC")==="ASC"?asc(n.started_at):desc(n.started_at);return {rows:e.db.select().from(n).where(a).orderBy(c).limit(t.limit).offset(t.offset).all(),total:u?.count??0}}catch(s){throw new _("DB_ERROR","Failed to find all tasks",s)}finally{e.close();}}getAgentUsage(t,e,s){if(!this.dbExists())return [];let o=this.openHandle(false);try{return o.db.all(s?sql`
77
+ `)}catch(s){throw new _("DB_ERROR","Failed to get workspace usage summary",s)}finally{e.close();}}getThreadTokenUsage(t,e){let s=this.resolveDbPaths(),o=new Set,i=0,a=0,d=0;for(let l of s){if(!existsSync(l))continue;let c=N(l);try{let u=eq(n.thread_id,t),g=e?and(u,eq(n.workspace_id,e)):u,f=c.db.select({id:n.id,input_tokens:n.input_tokens,output_tokens:n.output_tokens,cost_usd:n.cost_usd}).from(n).where(g).all();for(let m of f)o.has(m.id)||(o.add(m.id),i+=m.input_tokens??0,a+=m.output_tokens??0,d+=m.cost_usd??0);}catch(u){throw new _("DB_ERROR","Failed to get thread token usage",u)}finally{c.close();}}return {inputTokens:i,outputTokens:a,costUsd:d}}findTasksByThread(t,e,s){let o=this.resolveDbPaths(),i=new Set,a=[],d=s?.projection==="activity-log";for(let l of o){if(!existsSync(l))continue;let c=N(l);try{let u=eq(n.thread_id,t),g=e?and(u,eq(n.workspace_id,e)):u,f=d?c.db.select({id:n.id,thread_id:n.thread_id,agent_id:n.agent_id,parent_task_id:n.parent_task_id,started_at:n.started_at,completed_at:n.completed_at}).from(n).where(g).orderBy(asc(n.started_at)).all():c.db.select().from(n).where(g).orderBy(asc(n.started_at)).all();for(let m of f)i.has(m.id)||(i.add(m.id),a.push(m));}catch(u){throw new _("DB_ERROR","Failed to find tasks by thread",u)}finally{c.close();}}return a}parseWorkflowCardMetadata(t){if(!t)return {};try{let e=JSON.parse(t);return {kind:typeof e.kind=="string"?e.kind:void 0,workflowRunId:typeof e.workflowRunId=="string"?e.workflowRunId:void 0}}catch{return {}}}extractThreadIdFromCommand(t){return t?/--thread=(\S+)/.exec(t)?.[1]??null:null}findRunningWorkflowParentTasks(){if(!this.dbExists())return [];let t=[];for(let e of this.resolveDbPaths()){if(!existsSync(e))continue;let s=N(e);try{t.push(...s.db.select({id:n.id,agent_id:n.agent_id,thread_id:n.thread_id,workspace_id:n.workspace_id,platform:n.platform,status:n.status,error:n.error,completed_at:n.completed_at,duration_ms:n.duration_ms,started_at:n.started_at,metadata:n.metadata}).from(n).where(and(eq(n.platform,"workflow"),or(like(n.metadata,'%"kind":"workflow_card"%'),like(n.metadata,'%"kind":"workflow_ui_instance"%')))).orderBy(desc(n.started_at)).all());}catch(o){throw new _("DB_ERROR","Failed to find workflow parent tasks",o)}finally{s.close();}}return t}batchFetchWorkflowCards(t,e){let s=new Map;if(t.length===0)return s;let o=new Set(t),i=new Map,a=e?eq(n.workspace_id,e):void 0,d=(l,c)=>{if(!l)return;let u=i.get(l);if(u&&c.started_at<=u)return;let g=this.parseWorkflowCardMetadata(c.metadata);g.kind!=="workflow_card"&&g.kind!=="workflow_ui_instance"||(i.set(l,c.started_at),s.set(l,{taskId:c.id,workflowRunId:g.workflowRunId,raw:c.result??void 0}));};for(let l of this.resolveDbPaths()){if(!existsSync(l))continue;let c=N(l);try{let u=and(inArray(n.thread_id,t),or(like(n.metadata,'%"kind":"workflow_card"%'),like(n.metadata,'%"kind":"workflow_ui_instance"%'))),g=c.db.select({id:n.id,thread_id:n.thread_id,metadata:n.metadata,result:n.result,started_at:n.started_at}).from(n).where(a?and(u,a):u).all();for(let C of g)d(C.thread_id,C);let f=and(isNull(n.thread_id),or(like(n.metadata,'%"kind":"workflow_card"%'),like(n.metadata,'%"kind":"workflow_ui_instance"%'))),m=c.db.select({id:n.id,command:n.command,metadata:n.metadata,result:n.result,started_at:n.started_at}).from(n).where(a?and(f,a):f).all();for(let C of m){let D=this.extractThreadIdFromCommand(C.command);!D||!o.has(D)||d(D,C);}}catch(u){throw new _("DB_ERROR","Failed to batch fetch workflow cards",u)}finally{c.close();}}return s}findAllTasks(t){if(!this.dbExists())return {rows:[],total:0};let e=this.openHandle(false);try{let s=[];t.workspaceId&&s.push(eq(n.workspace_id,t.workspaceId));let o=t.agents&&t.agents.length>0?t.agents:t.agentId?[t.agentId]:null;o&&s.push(inArray(n.agent_id,o));let i=t.statuses&&t.statuses.length>0?t.statuses:t.status?[t.status]:null;i&&s.push(inArray(n.status,i));let a=t.q??t.search;a&&s.push(like(n.prompt,`%${a}%`)),t.from&&s.push(gte(n.started_at,t.from)),t.to&&s.push(lt(n.started_at,t.to));let d=s.length>0?and(...s):void 0,l=e.db.select({count:sql`count(*)`}).from(n).where(d).get(),c=(t.sortDir??"DESC")==="ASC"?asc(n.started_at):desc(n.started_at);return {rows:e.db.select().from(n).where(d).orderBy(c).limit(t.limit).offset(t.offset).all(),total:l?.count??0}}catch(s){throw new _("DB_ERROR","Failed to find all tasks",s)}finally{e.close();}}getAgentUsage(t,e,s){if(!this.dbExists())return [];let o=this.openHandle(false);try{return o.db.all(s?sql`
78
78
  SELECT
79
79
  t.agent_id,
80
80
  t.workspace_id,
@@ -82,11 +82,11 @@ ${s.join(`
82
82
  COALESCE(SUM(
83
83
  COALESCE(t.input_tokens, 0)
84
84
  + CASE
85
- WHEN t.started_at >= ${st}
85
+ WHEN t.started_at >= ${et}
86
86
  AND (
87
87
  t.crewx_version IS NULL
88
88
  OR (t.crewx_version LIKE '0.8.%' AND t.crewx_version NOT LIKE '0.8.9%')
89
- OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${rt}) AS INTEGER) < ${ot})
89
+ OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${nt}) AS INTEGER) < ${st})
90
90
  )
91
91
  THEN COALESCE(t.cached_input_tokens, 0)
92
92
  ELSE 0
@@ -105,11 +105,11 @@ ${s.join(`
105
105
  COALESCE(SUM(
106
106
  COALESCE(t.input_tokens, 0)
107
107
  + CASE
108
- WHEN t.started_at >= ${st}
108
+ WHEN t.started_at >= ${et}
109
109
  AND (
110
110
  t.crewx_version IS NULL
111
111
  OR (t.crewx_version LIKE '0.8.%' AND t.crewx_version NOT LIKE '0.8.9%')
112
- OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${rt}) AS INTEGER) < ${ot})
112
+ OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${nt}) AS INTEGER) < ${st})
113
113
  )
114
114
  THEN COALESCE(t.cached_input_tokens, 0)
115
115
  ELSE 0
@@ -125,11 +125,11 @@ ${s.join(`
125
125
  COALESCE(SUM(
126
126
  COALESCE(t.input_tokens, 0)
127
127
  + CASE
128
- WHEN t.started_at >= ${st}
128
+ WHEN t.started_at >= ${et}
129
129
  AND (
130
130
  t.crewx_version IS NULL
131
131
  OR (t.crewx_version LIKE '0.8.%' AND t.crewx_version NOT LIKE '0.8.9%')
132
- OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${rt}) AS INTEGER) < ${ot})
132
+ OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${nt}) AS INTEGER) < ${st})
133
133
  )
134
134
  THEN COALESCE(t.cached_input_tokens, 0)
135
135
  ELSE 0
@@ -147,11 +147,11 @@ ${s.join(`
147
147
  COALESCE(SUM(
148
148
  COALESCE(t.input_tokens, 0)
149
149
  + CASE
150
- WHEN t.started_at >= ${st}
150
+ WHEN t.started_at >= ${et}
151
151
  AND (
152
152
  t.crewx_version IS NULL
153
153
  OR (t.crewx_version LIKE '0.8.%' AND t.crewx_version NOT LIKE '0.8.9%')
154
- OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${rt}) AS INTEGER) < ${ot})
154
+ OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${nt}) AS INTEGER) < ${st})
155
155
  )
156
156
  THEN COALESCE(t.cached_input_tokens, 0)
157
157
  ELSE 0
@@ -159,18 +159,18 @@ ${s.join(`
159
159
  ), 0)
160
160
  + COALESCE(SUM(t.output_tokens), 0)
161
161
  ) DESC
162
- `).map(d=>({agentId:d.agent_id,workspaceId:d.workspace_id??null,totalTasks:d.total_tasks,inputTokens:d.input_tokens,outputTokens:d.output_tokens,cachedInputTokens:d.cached_input_tokens,costUsd:d.cost_usd,totalTokens:d.input_tokens+d.output_tokens}))}catch(i){throw new _("DB_ERROR","Failed to get agent usage",i)}finally{o.close();}}getAgentUsageTrendRaw(t,e,s){if(!this.dbExists())return [];let o=this.openHandle(false);try{return o.db.all(s?sql`
162
+ `).map(a=>({agentId:a.agent_id,workspaceId:a.workspace_id??null,totalTasks:a.total_tasks,inputTokens:a.input_tokens,outputTokens:a.output_tokens,cachedInputTokens:a.cached_input_tokens,costUsd:a.cost_usd,totalTokens:a.input_tokens+a.output_tokens}))}catch(i){throw new _("DB_ERROR","Failed to get agent usage",i)}finally{o.close();}}getAgentUsageTrendRaw(t,e,s){if(!this.dbExists())return [];let o=this.openHandle(false);try{return o.db.all(s?sql`
163
163
  SELECT
164
164
  date(t.started_at) AS date,
165
165
  t.agent_id,
166
166
  COALESCE(SUM(
167
167
  COALESCE(t.input_tokens, 0)
168
168
  + CASE
169
- WHEN t.started_at >= ${st}
169
+ WHEN t.started_at >= ${et}
170
170
  AND (
171
171
  t.crewx_version IS NULL
172
172
  OR (t.crewx_version LIKE '0.8.%' AND t.crewx_version NOT LIKE '0.8.9%')
173
- OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${rt}) AS INTEGER) < ${ot})
173
+ OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${nt}) AS INTEGER) < ${st})
174
174
  )
175
175
  THEN COALESCE(t.cached_input_tokens, 0)
176
176
  ELSE 0
@@ -193,11 +193,11 @@ ${s.join(`
193
193
  COALESCE(SUM(
194
194
  COALESCE(t.input_tokens, 0)
195
195
  + CASE
196
- WHEN t.started_at >= ${st}
196
+ WHEN t.started_at >= ${et}
197
197
  AND (
198
198
  t.crewx_version IS NULL
199
199
  OR (t.crewx_version LIKE '0.8.%' AND t.crewx_version NOT LIKE '0.8.9%')
200
- OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${rt}) AS INTEGER) < ${ot})
200
+ OR (t.crewx_version LIKE '0.8.9-rc.%' AND CAST(SUBSTR(t.crewx_version, ${nt}) AS INTEGER) < ${st})
201
201
  )
202
202
  THEN COALESCE(t.cached_input_tokens, 0)
203
203
  ELSE 0
@@ -212,59 +212,59 @@ ${s.join(`
212
212
  AND t.started_at < ${e}
213
213
  GROUP BY date(t.started_at), t.agent_id
214
214
  ORDER BY date(t.started_at) ASC
215
- `).map(d=>({date:d.date,agentId:d.agent_id,inputTokens:d.input_tokens,outputTokens:d.output_tokens,cachedInputTokens:d.cached_input_tokens,costUsd:d.cost_usd,totalTokens:d.input_tokens+d.output_tokens}))}catch(i){throw new _("DB_ERROR","Failed to get agent usage trend",i)}finally{o.close();}}findTaskForStop(t,e){if(!this.dbExists())return;let s=this.openHandle(false);try{return s.db.select().from(n).where(and(eq(n.id,t),eq(n.workspace_id,e))).limit(1).get()??void 0}catch(o){throw new _("DB_ERROR","Failed to find task for stop",o)}finally{s.close();}}findLatestTaskByMetadata(t){if(!this.dbExists())return null;let e=this.openHandle(false);try{return e.db.select({id:n.id,thread_id:n.thread_id,status:n.status,result:n.result,error:n.error,started_at:n.started_at,completed_at:n.completed_at,metadata:n.metadata}).from(n).where(and(eq(n.agent_id,t.agentId),eq(n.workspace_id,t.workspaceId),sql`${n.metadata} IS NOT NULL AND json_valid(${n.metadata}) AND json_extract(${n.metadata}, '$.' || ${t.metaKey}) = ${t.metaValue}`)).orderBy(desc(n.started_at)).limit(1).get()??null}catch(s){throw new _("DB_ERROR","Failed to find latest task by metadata",s)}finally{e.close();}}markTaskFailed(t,e,s){if(!this.dbExists())return;let o=this.openHandle(true);try{let i=new Date().toISOString(),d=s?and(eq(n.id,t),eq(n.status,"running"),eq(n.workspace_id,s)):and(eq(n.id,t),eq(n.status,"running"));o.db.update(n).set({status:"failed",error:e,completed_at:i}).where(d).run();}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to mark task failed",i)}finally{o.close();}}findTasksByPromptHint(t,e){let s=this.resolveDbPaths(),o=new Set,i=[];for(let d of s){if(!existsSync(d))continue;let a=N(d);try{let u=e?and(like(n.prompt,`%${t}%`),eq(n.workspace_id,e)):like(n.prompt,`%${t}%`),c=a.db.select().from(n).where(u).orderBy(asc(n.started_at)).all();for(let l of c)o.has(l.id)||(o.add(l.id),i.push(l));}catch(u){throw new _("DB_ERROR","Failed to find tasks by prompt hint",u)}finally{a.close();}}return i}getProviderUsage(t,e,s){if(!this.dbExists())return [];let o=this.openHandle(false);try{let i=we(),d=sql`
215
+ `).map(a=>({date:a.date,agentId:a.agent_id,inputTokens:a.input_tokens,outputTokens:a.output_tokens,cachedInputTokens:a.cached_input_tokens,costUsd:a.cost_usd,totalTokens:a.input_tokens+a.output_tokens}))}catch(i){throw new _("DB_ERROR","Failed to get agent usage trend",i)}finally{o.close();}}findTaskForStop(t,e){if(!this.dbExists())return;let s=this.openHandle(false);try{return s.db.select().from(n).where(and(eq(n.id,t),eq(n.workspace_id,e))).limit(1).get()??void 0}catch(o){throw new _("DB_ERROR","Failed to find task for stop",o)}finally{s.close();}}findLatestTaskByMetadata(t){if(!this.dbExists())return null;let e=this.openHandle(false);try{return e.db.select({id:n.id,thread_id:n.thread_id,status:n.status,result:n.result,error:n.error,started_at:n.started_at,completed_at:n.completed_at,metadata:n.metadata}).from(n).where(and(eq(n.agent_id,t.agentId),eq(n.workspace_id,t.workspaceId),sql`${n.metadata} IS NOT NULL AND json_valid(${n.metadata}) AND json_extract(${n.metadata}, '$.' || ${t.metaKey}) = ${t.metaValue}`)).orderBy(desc(n.started_at)).limit(1).get()??null}catch(s){throw new _("DB_ERROR","Failed to find latest task by metadata",s)}finally{e.close();}}markTaskFailed(t,e,s){if(!this.dbExists())return;let o=this.openHandle(true);try{let i=new Date().toISOString(),a=s?and(eq(n.id,t),eq(n.status,"running"),eq(n.workspace_id,s)):and(eq(n.id,t),eq(n.status,"running"));o.db.update(n).set({status:"failed",error:e,completed_at:i}).where(a).run();}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to mark task failed",i)}finally{o.close();}}findTasksByPromptHint(t,e){let s=this.resolveDbPaths(),o=new Set,i=[];for(let a of s){if(!existsSync(a))continue;let d=N(a);try{let l=e?and(like(n.prompt,`%${t}%`),eq(n.workspace_id,e)):like(n.prompt,`%${t}%`),c=d.db.select().from(n).where(l).orderBy(asc(n.started_at)).all();for(let u of c)o.has(u.id)||(o.add(u.id),i.push(u));}catch(l){throw new _("DB_ERROR","Failed to find tasks by prompt hint",l)}finally{d.close();}}return i}getProviderUsage(t,e,s){if(!this.dbExists())return [];let o=this.openHandle(false);try{let i=be(),a=sql`
216
216
  COALESCE(${n.input_tokens}, 0)
217
217
  + CASE
218
- WHEN ${n.started_at} >= ${st}
218
+ WHEN ${n.started_at} >= ${et}
219
219
  AND (
220
220
  ${n.crewx_version} IS NULL
221
221
  OR (${n.crewx_version} LIKE '0.8.%' AND ${n.crewx_version} NOT LIKE '0.8.9%')
222
- OR (${n.crewx_version} LIKE '0.8.9-rc.%' AND CAST(SUBSTR(${n.crewx_version}, ${rt}) AS INTEGER) < ${ot})
222
+ OR (${n.crewx_version} LIKE '0.8.9-rc.%' AND CAST(SUBSTR(${n.crewx_version}, ${nt}) AS INTEGER) < ${st})
223
223
  )
224
224
  THEN COALESCE(${n.cached_input_tokens}, 0)
225
225
  ELSE 0
226
226
  END
227
- `,a=s?sql`WHERE ${n.status} IN ('completed', 'success') AND ${n.started_at} >= ${t} AND ${n.started_at} < ${e} AND ${n.workspace_id} = ${s}`:sql`WHERE ${n.status} IN ('completed', 'success') AND ${n.started_at} >= ${t} AND ${n.started_at} < ${e}`;return o.db.all(sql`
227
+ `,d=s?sql`WHERE ${n.status} IN ('completed', 'success') AND ${n.started_at} >= ${t} AND ${n.started_at} < ${e} AND ${n.workspace_id} = ${s}`:sql`WHERE ${n.status} IN ('completed', 'success') AND ${n.started_at} >= ${t} AND ${n.started_at} < ${e}`;return o.db.all(sql`
228
228
  SELECT
229
229
  ${i} AS provider,
230
230
  COUNT(*) AS total_tasks,
231
- COALESCE(SUM(${d}), 0) AS input_tokens,
231
+ COALESCE(SUM(${a}), 0) AS input_tokens,
232
232
  COALESCE(SUM(${n.output_tokens}), 0) AS output_tokens,
233
233
  COALESCE(SUM(${n.cached_input_tokens}), 0) AS cached_input_tokens,
234
234
  COALESCE(SUM(${n.cost_usd}), 0) AS cost_usd,
235
235
  COALESCE(SUM(${n.duration_ms}), 0) AS active_duration_ms,
236
236
  MAX(${n.completed_at}) AS last_active_at
237
237
  FROM ${n}
238
- ${a}
238
+ ${d}
239
239
  GROUP BY ${i}
240
- ORDER BY (COALESCE(SUM(${d}), 0) + COALESCE(SUM(${n.output_tokens}), 0)) DESC
241
- `).map(c=>({provider:c.provider,totalTasks:c.total_tasks,inputTokens:c.input_tokens,outputTokens:c.output_tokens,cachedInputTokens:c.cached_input_tokens,costUsd:c.cost_usd,totalTokens:c.input_tokens+c.output_tokens,activeDurationMs:c.active_duration_ms??0,lastActiveAt:c.last_active_at??null}))}catch(i){throw new _("DB_ERROR","Failed to get provider usage",i)}finally{o.close();}}getModelUsage(t,e,s){if(!this.dbExists())return [];let o=this.openHandle(false);try{let i=we(),d=sql`
240
+ ORDER BY (COALESCE(SUM(${a}), 0) + COALESCE(SUM(${n.output_tokens}), 0)) DESC
241
+ `).map(c=>({provider:c.provider,totalTasks:c.total_tasks,inputTokens:c.input_tokens,outputTokens:c.output_tokens,cachedInputTokens:c.cached_input_tokens,costUsd:c.cost_usd,totalTokens:c.input_tokens+c.output_tokens,activeDurationMs:c.active_duration_ms??0,lastActiveAt:c.last_active_at??null}))}catch(i){throw new _("DB_ERROR","Failed to get provider usage",i)}finally{o.close();}}getModelUsage(t,e,s){if(!this.dbExists())return [];let o=this.openHandle(false);try{let i=be(),a=sql`
242
242
  COALESCE(${n.input_tokens}, 0)
243
243
  + CASE
244
- WHEN ${n.started_at} >= ${st}
244
+ WHEN ${n.started_at} >= ${et}
245
245
  AND (
246
246
  ${n.crewx_version} IS NULL
247
247
  OR (${n.crewx_version} LIKE '0.8.%' AND ${n.crewx_version} NOT LIKE '0.8.9%')
248
- OR (${n.crewx_version} LIKE '0.8.9-rc.%' AND CAST(SUBSTR(${n.crewx_version}, ${rt}) AS INTEGER) < ${ot})
248
+ OR (${n.crewx_version} LIKE '0.8.9-rc.%' AND CAST(SUBSTR(${n.crewx_version}, ${nt}) AS INTEGER) < ${st})
249
249
  )
250
250
  THEN COALESCE(${n.cached_input_tokens}, 0)
251
251
  ELSE 0
252
252
  END
253
- `,a=s?sql`WHERE ${n.status} IN ('completed', 'success') AND ${n.started_at} >= ${t} AND ${n.started_at} < ${e} AND ${n.workspace_id} = ${s}`:sql`WHERE ${n.status} IN ('completed', 'success') AND ${n.started_at} >= ${t} AND ${n.started_at} < ${e}`,u=o.db.all(sql`
253
+ `,d=s?sql`WHERE ${n.status} IN ('completed', 'success') AND ${n.started_at} >= ${t} AND ${n.started_at} < ${e} AND ${n.workspace_id} = ${s}`:sql`WHERE ${n.status} IN ('completed', 'success') AND ${n.started_at} >= ${t} AND ${n.started_at} < ${e}`,l=o.db.all(sql`
254
254
  SELECT
255
255
  ${n.model} AS model,
256
256
  ${i} AS provider,
257
257
  COUNT(*) AS total_tasks,
258
- COALESCE(SUM(${d}), 0) AS input_tokens,
258
+ COALESCE(SUM(${a}), 0) AS input_tokens,
259
259
  COALESCE(SUM(${n.output_tokens}), 0) AS output_tokens,
260
260
  COALESCE(SUM(${n.cached_input_tokens}), 0) AS cached_input_tokens,
261
261
  COALESCE(SUM(${n.cost_usd}), 0) AS cost_usd,
262
262
  COALESCE(SUM(${n.duration_ms}), 0) AS active_duration_ms,
263
263
  MAX(${n.completed_at}) AS last_active_at
264
264
  FROM ${n}
265
- ${a}
265
+ ${d}
266
266
  GROUP BY ${n.model}, ${i}
267
- `),c=new Map;for(let l of u){let g=_e(l.model??""),f=JSON.stringify([l.provider,g]),w=c.get(f);w?(w.totalTasks+=l.total_tasks,w.inputTokens+=l.input_tokens,w.outputTokens+=l.output_tokens,w.cachedInputTokens+=l.cached_input_tokens,w.costUsd+=l.cost_usd,w.totalTokens+=l.input_tokens+l.output_tokens,w.activeDurationMs+=l.active_duration_ms??0,l.last_active_at&&(!w.lastActiveAt||l.last_active_at>w.lastActiveAt)&&(w.lastActiveAt=l.last_active_at)):c.set(f,{model:g,provider:l.provider,totalTasks:l.total_tasks,inputTokens:l.input_tokens,outputTokens:l.output_tokens,cachedInputTokens:l.cached_input_tokens,costUsd:l.cost_usd,totalTokens:l.input_tokens+l.output_tokens,activeDurationMs:l.active_duration_ms??0,lastActiveAt:l.last_active_at??null});}return Array.from(c.values()).sort((l,g)=>g.costUsd-l.costUsd)}catch(i){throw new _("DB_ERROR","Failed to get model usage",i)}finally{o.close();}}getTasksForReport(t,e,s){if(!this.dbExists())return [];let o=this.openHandle(false);try{let i=s?sql`WHERE ${n.started_at} >= ${t} AND ${n.started_at} < ${e} AND ${n.workspace_id} = ${s}`:sql`WHERE ${n.started_at} >= ${t} AND ${n.started_at} < ${e}`;return o.db.all(sql`
267
+ `),c=new Map;for(let u of l){let g=we(u.model??""),f=JSON.stringify([u.provider,g]),m=c.get(f);m?(m.totalTasks+=u.total_tasks,m.inputTokens+=u.input_tokens,m.outputTokens+=u.output_tokens,m.cachedInputTokens+=u.cached_input_tokens,m.costUsd+=u.cost_usd,m.totalTokens+=u.input_tokens+u.output_tokens,m.activeDurationMs+=u.active_duration_ms??0,u.last_active_at&&(!m.lastActiveAt||u.last_active_at>m.lastActiveAt)&&(m.lastActiveAt=u.last_active_at)):c.set(f,{model:g,provider:u.provider,totalTasks:u.total_tasks,inputTokens:u.input_tokens,outputTokens:u.output_tokens,cachedInputTokens:u.cached_input_tokens,costUsd:u.cost_usd,totalTokens:u.input_tokens+u.output_tokens,activeDurationMs:u.active_duration_ms??0,lastActiveAt:u.last_active_at??null});}return Array.from(c.values()).sort((u,g)=>g.costUsd-u.costUsd)}catch(i){throw new _("DB_ERROR","Failed to get model usage",i)}finally{o.close();}}getTasksForReport(t,e,s){if(!this.dbExists())return [];let o=this.openHandle(false);try{let i=s?sql`WHERE ${n.started_at} >= ${t} AND ${n.started_at} < ${e} AND ${n.workspace_id} = ${s}`:sql`WHERE ${n.started_at} >= ${t} AND ${n.started_at} < ${e}`;return o.db.all(sql`
268
268
  SELECT
269
269
  ${n.thread_id} AS thread_id,
270
270
  ${n.agent_id} AS agent_id,
@@ -275,19 +275,19 @@ ${s.join(`
275
275
  FROM ${n}
276
276
  ${i}
277
277
  ORDER BY ${n.started_at} ASC
278
- `).map(a=>({threadId:a.thread_id,agentId:a.agent_id,startedAt:a.started_at,status:a.status,error:a.error,exitCode:a.exit_code}))}catch(i){throw new _("DB_ERROR","Failed to get tasks for report",i)}finally{o.close();}}getDelegationMatrix(t,e,s){if(!this.dbExists())return {edges:[],truncated:false};let o=this.openHandle(false);try{let i=sql`
278
+ `).map(d=>({threadId:d.thread_id,agentId:d.agent_id,startedAt:d.started_at,status:d.status,error:d.error,exitCode:d.exit_code}))}catch(i){throw new _("DB_ERROR","Failed to get tasks for report",i)}finally{o.close();}}getDelegationMatrix(t,e,s){if(!this.dbExists())return {edges:[],truncated:false};let o=this.openHandle(false);try{let i=sql`
279
279
  COALESCE(${n.input_tokens}, 0)
280
280
  + CASE
281
- WHEN ${n.started_at} >= ${st}
281
+ WHEN ${n.started_at} >= ${et}
282
282
  AND (
283
283
  ${n.crewx_version} IS NULL
284
284
  OR (${n.crewx_version} LIKE '0.8.%' AND ${n.crewx_version} NOT LIKE '0.8.9%')
285
- OR (${n.crewx_version} LIKE '0.8.9-rc.%' AND CAST(SUBSTR(${n.crewx_version}, ${rt}) AS INTEGER) < ${ot})
285
+ OR (${n.crewx_version} LIKE '0.8.9-rc.%' AND CAST(SUBSTR(${n.crewx_version}, ${nt}) AS INTEGER) < ${st})
286
286
  )
287
287
  THEN COALESCE(${n.cached_input_tokens}, 0)
288
288
  ELSE 0
289
289
  END
290
- `,d=s?sql`WHERE ${n.status} IN ('completed', 'success') AND ${n.started_at} >= ${t} AND ${n.started_at} < ${e} AND ${n.workspace_id} = ${s}`:sql`WHERE ${n.status} IN ('completed', 'success') AND ${n.started_at} >= ${t} AND ${n.started_at} < ${e}`,a=o.db.all(sql`
290
+ `,a=s?sql`WHERE ${n.status} IN ('completed', 'success') AND ${n.started_at} >= ${t} AND ${n.started_at} < ${e} AND ${n.workspace_id} = ${s}`:sql`WHERE ${n.status} IN ('completed', 'success') AND ${n.started_at} >= ${t} AND ${n.started_at} < ${e}`,d=o.db.all(sql`
291
291
  SELECT
292
292
  ${n.caller_agent_id} AS caller_agent_id,
293
293
  ${n.agent_id} AS agent_id,
@@ -295,15 +295,15 @@ ${s.join(`
295
295
  COALESCE(SUM(${i}), 0) AS input_tokens,
296
296
  COALESCE(SUM(${n.output_tokens}), 0) AS output_tokens
297
297
  FROM ${n}
298
- ${d}
298
+ ${a}
299
299
  GROUP BY ${n.caller_agent_id}, ${n.agent_id}
300
300
  ORDER BY task_count DESC, ${n.caller_agent_id} ASC, ${n.agent_id} ASC
301
- LIMIT ${Ft+1}
302
- `),u=a.length>Ft;return {edges:a.slice(0,Ft).map(l=>({callerAgentId:l.caller_agent_id??null,agentId:l.agent_id,taskCount:l.task_count,totalTokens:l.input_tokens+l.output_tokens,outputTokens:l.output_tokens})),truncated:u}}catch(i){throw new _("DB_ERROR","Failed to get delegation matrix",i)}finally{o.close();}}findTaskEvidence(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{let s=[];t.workspaceId&&s.push(eq(n.workspace_id,t.workspaceId)),t.agents&&t.agents.length>0&&s.push(inArray(n.agent_id,t.agents)),t.statuses&&t.statuses.length>0&&s.push(inArray(n.status,t.statuses)),t.from&&s.push(gte(n.started_at,t.from)),t.to&&s.push(lt(n.started_at,t.to));let o=s.length>0?and(...s):void 0,i=(t.order??"DESC")==="ASC"?asc(n.started_at):desc(n.started_at),d={id:n.id,agent_id:n.agent_id,status:n.status,prompt:n.prompt,started_at:n.started_at,completed_at:n.completed_at,thread_id:n.thread_id};return t.limit?e.db.select(d).from(n).where(o).orderBy(i).limit(t.limit).all():e.db.select(d).from(n).where(o).orderBy(i).all()}catch(s){throw new _("DB_ERROR","Failed to find task evidence",s)}finally{e.close();}}findThreadChains(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{let s=t.maxThreads??8,o=t.maxTurnsPerThread??4,i=[isNotNull(n.thread_id),isNull(n.parent_task_id),isNull(n.caller_agent_id),or(isNull(n.platform),ne(n.platform,"workflow")),or(isNull(n.metadata),and(notLike(n.metadata,'%"kind":"workflow_card"%'),notLike(n.metadata,'%"kind":"workflow_ui_instance"%'))),or(isNull(n.metadata),notLike(n.metadata,'%"retriedFromTaskId"%'))];t.workspaceId&&i.push(eq(n.workspace_id,t.workspaceId)),t.from&&i.push(gte(n.started_at,t.from)),t.to&&i.push(lt(n.started_at,t.to));let d=e.db.select({id:n.id,agent_id:n.agent_id,status:n.status,prompt:n.prompt,started_at:n.started_at,completed_at:n.completed_at,thread_id:n.thread_id}).from(n).where(and(...i)).orderBy(asc(n.started_at),asc(n.id)).all(),a=new Map;for(let c of d){if(!c.thread_id)continue;let l={id:c.id,agentId:c.agent_id,status:c.status,prompt:c.prompt,startedAt:c.started_at,completedAt:c.completed_at},g=a.get(c.thread_id);g?g.push(l):a.set(c.thread_id,[l]);}let u=[];for(let[c,l]of a){let g=l.filter(f=>f.agentId===t.targetAgentId).length;g<2||u.push({threadId:c,targetAgentTurnCount:g,turns:l});}return u.sort((c,l)=>{let g=c.turns[c.turns.length-1].startedAt,f=l.turns[l.turns.length-1].startedAt;return g<f?1:g>f?-1:0}),u.slice(0,s).map(c=>({threadId:c.threadId,targetAgentTurnCount:c.targetAgentTurnCount,turns:c.turns.slice(-o)}))}catch(s){throw new _("DB_ERROR","Failed to find thread chains",s)}finally{e.close();}}};var Wt=class extends it{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 Dt({dbPath:this.dbPath}),s=process.cwd(),i=existsSync(join(s,"crewx.yaml"))||existsSync(join(s,"crewx.yml"))?Qt(s):null,d=process.argv.join(" ");this.unsubs.push(t.on("task:start",a=>{try{let u=a.callerAgentId??null,c=a.parentTaskId??null,l=a.rootTraceId??a.traceId,g=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:d,codingAgentCommand:a.codingAgentCommand??null,workspaceId:a.workspaceId??i,projectId:a.projectId??null,callerAgentId:u,parentTaskId:c,traceId:l,metadata:g,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{let u=typeof a.metadata?.runEpoch=="number"?a.metadata.runEpoch:null,c=a.error?JSON.stringify({code:a.error.code,message:a.error.message,...a.error.providerCode?{providerCode:a.error.providerCode}:{}}):null;e.finishTask({id:a.traceId,status:a.error?"failed":"success",result:a.result??null,error:c,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,runEpoch:u});}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[];}};var Lt=class extends at{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 o=dirname(e);existsSync(o)||mkdirSync(o,{recursive:true});}else if(!existsSync(e))throw new _("NOT_FOUND","Database not found");let s=N(e);if(t)try{pt(s.db,e);}catch(o){throw s.close(),o}return s}validateWorkspaceId(t,e){return t.db.select({id:gt.id}).from(gt).where(eq(gt.id,e)).limit(1).get()?e:null}topLevelTaskPredicateSql(t="child"){return sql.raw(`NOT EXISTS (
301
+ LIMIT ${Jt+1}
302
+ `),l=d.length>Jt;return {edges:d.slice(0,Jt).map(u=>({callerAgentId:u.caller_agent_id??null,agentId:u.agent_id,taskCount:u.task_count,totalTokens:u.input_tokens+u.output_tokens,outputTokens:u.output_tokens})),truncated:l}}catch(i){throw new _("DB_ERROR","Failed to get delegation matrix",i)}finally{o.close();}}findTaskEvidence(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{let s=[];t.workspaceId&&s.push(eq(n.workspace_id,t.workspaceId)),t.agents&&t.agents.length>0&&s.push(inArray(n.agent_id,t.agents)),t.statuses&&t.statuses.length>0&&s.push(inArray(n.status,t.statuses)),t.from&&s.push(gte(n.started_at,t.from)),t.to&&s.push(lt(n.started_at,t.to));let o=s.length>0?and(...s):void 0,i=(t.order??"DESC")==="ASC"?asc(n.started_at):desc(n.started_at),a={id:n.id,agent_id:n.agent_id,status:n.status,prompt:n.prompt,started_at:n.started_at,completed_at:n.completed_at,thread_id:n.thread_id};return t.limit?e.db.select(a).from(n).where(o).orderBy(i).limit(t.limit).all():e.db.select(a).from(n).where(o).orderBy(i).all()}catch(s){throw new _("DB_ERROR","Failed to find task evidence",s)}finally{e.close();}}findThreadChains(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{let s=t.maxThreads??8,o=t.maxTurnsPerThread??4,i=[isNotNull(n.thread_id),isNull(n.parent_task_id),isNull(n.caller_agent_id),or(isNull(n.platform),ne(n.platform,"workflow")),or(isNull(n.metadata),and(notLike(n.metadata,'%"kind":"workflow_card"%'),notLike(n.metadata,'%"kind":"workflow_ui_instance"%'))),or(isNull(n.metadata),notLike(n.metadata,'%"retriedFromTaskId"%'))];t.workspaceId&&i.push(eq(n.workspace_id,t.workspaceId)),t.from&&i.push(gte(n.started_at,t.from)),t.to&&i.push(lt(n.started_at,t.to));let a=e.db.select({id:n.id,agent_id:n.agent_id,status:n.status,prompt:n.prompt,started_at:n.started_at,completed_at:n.completed_at,thread_id:n.thread_id}).from(n).where(and(...i)).orderBy(asc(n.started_at),asc(n.id)).all(),d=new Map;for(let c of a){if(!c.thread_id)continue;let u={id:c.id,agentId:c.agent_id,status:c.status,prompt:c.prompt,startedAt:c.started_at,completedAt:c.completed_at},g=d.get(c.thread_id);g?g.push(u):d.set(c.thread_id,[u]);}let l=[];for(let[c,u]of d){let g=u.filter(f=>f.agentId===t.targetAgentId).length;g<2||l.push({threadId:c,targetAgentTurnCount:g,turns:u});}return l.sort((c,u)=>{let g=c.turns[c.turns.length-1].startedAt,f=u.turns[u.turns.length-1].startedAt;return g<f?1:g>f?-1:0}),l.slice(0,s).map(c=>({threadId:c.threadId,targetAgentTurnCount:c.targetAgentTurnCount,turns:c.turns.slice(-o)}))}catch(s){throw new _("DB_ERROR","Failed to find thread chains",s)}finally{e.close();}}};var Vt=class extends rt{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 It({dbPath:this.dbPath}),s=process.cwd(),i=existsSync(join(s,"crewx.yaml"))||existsSync(join(s,"crewx.yml"))?re(s):null,a=process.argv.join(" ");this.unsubs.push(t.on("task:start",d=>{try{let l=d.callerAgentId??null,c=d.parentTaskId??null,u=d.rootTraceId??d.traceId,g=d.metadata?JSON.stringify(d.metadata):JSON.stringify({provider:d.provider??"cli/claude"});e.startTask({id:d.traceId,agentId:d.agentRef.replace(/^@/,""),prompt:d.message,mode:d.mode,status:"running",pid:d.pid??null,startedAt:d.timestamp.toISOString(),crewxVersion:this.version,platform:d.platform??"cli",model:d.model??null,renderedPrompt:d.renderedPrompt??null,command:a,codingAgentCommand:d.codingAgentCommand??null,workspaceId:d.workspaceId??i,projectId:d.projectId??null,callerAgentId:l,parentTaskId:c,traceId:u,metadata:g,threadId:d.threadId??null});}catch{}}),t.on("task:output",d=>{try{e.appendLog(d.traceId,{timestamp:d.timestamp.toISOString(),level:d.level??"stdout",message:d.output});}catch{}}),t.on("task:end",d=>{try{let l=typeof d.metadata?.runEpoch=="number"?d.metadata.runEpoch:null,c=d.error?JSON.stringify({code:d.error.code,message:d.error.message,...d.error.providerCode?{providerCode:d.error.providerCode}:{}}):null;e.finishTask({id:d.traceId,status:d.error?"failed":"success",result:d.result??null,error:c,completedAt:d.timestamp.toISOString(),durationMs:d.durationMs,exitCode:d.exitCode??null,inputTokens:d.inputTokens??0,outputTokens:d.outputTokens??0,cachedInputTokens:d.cachedInputTokens??0,costUsd:d.costUsd??0,model:d.model??null,runEpoch:l});}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[];}};var ht=class extends _{reason;constructor(t,e=t){super("VALIDATION",e),this.name="ThreadTitleMetadataPatchError",this.reason=t;}};function Nt(r){if(!r)return {};try{let t=JSON.parse(r);return !t||typeof t!="object"||Array.isArray(t)?{}:t}catch{return {}}}function ns(r){let e=Nt(r).threadTitle;if(!e||typeof e!="object"||Array.isArray(e))return {priority:null,state:null};let s=e;return {priority:typeof s.priority=="string"?s.priority:null,state:typeof s.state=="string"?s.state:null}}var Pt=class extends ot{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 o=dirname(e);existsSync(o)||mkdirSync(o,{recursive:true});}else if(!existsSync(e))throw new _("NOT_FOUND","Database not found");let s=N(e);if(t)try{gt(s.db,e);}catch(o){throw s.close(),o}return s}validateWorkspaceId(t,e){return t.db.select({id:ft.id}).from(ft).where(eq(ft.id,e)).limit(1).get()?e:null}topLevelTaskPredicateSql(t="child"){return sql.raw(`NOT EXISTS (
303
303
  SELECT 1 FROM tasks parent
304
304
  WHERE parent.id = ${t}.parent_task_id
305
305
  AND parent.thread_id = ${t}.thread_id
306
- )`)}findAllThreads(t){let e=this.resolveDbPaths(),s=new Set,o=[];for(let i of e){if(!existsSync(i))continue;let d=N(i);try{let a=t?eq(p.workspace_id,t):void 0,u=d.db.select().from(p).where(a).orderBy(desc(p.updated_at)).all();for(let c of u)s.has(c.id)||(s.add(c.id),o.push(c));}catch(a){throw new _("DB_ERROR","Failed to find all threads",a)}finally{d.close();}}return o}findThreadsByIdsOrTitles(t,e){if(t.length===0)return [];let s=this.resolveDbPaths(),o=new Set,i=[];for(let d of s){if(!existsSync(d))continue;let a=N(d);try{let u=or(inArray(p.id,t),inArray(p.title,t)),c=e?and(u,eq(p.workspace_id,e)):u,l=a.db.select().from(p).where(c).orderBy(desc(p.updated_at)).all();for(let g of l)o.has(g.id)||(o.add(g.id),i.push(g));}catch(u){throw new _("DB_ERROR","Failed to find threads by ids or titles",u)}finally{a.close();}}return i}findThreadById(t,e){let s=this.resolveDbPaths();for(let o of s){if(!existsSync(o))continue;let i=N(o);try{let d=eq(p.id,t),a=e?and(d,eq(p.workspace_id,e)):d,u=i.db.select().from(p).where(a).limit(1).get()??void 0;if(u)return u}catch(d){throw new _("DB_ERROR","Failed to find thread by id",d)}finally{i.close();}}}threadExists(t,e){let s=this.resolveDbPaths();for(let o of s){if(!existsSync(o))continue;let i=N(o);try{let d=eq(p.id,t),a=e?and(d,eq(p.workspace_id,e)):d;if(i.db.select({id:p.id}).from(p).where(a).limit(1).get())return !0}catch(d){throw new _("DB_ERROR","Failed to check thread existence",d)}finally{i.close();}}return false}aggregateTaskStats(t,e){let s=this.resolveDbPaths(),o=0,i=0,d=0,a=0,u=0,c=new Set;for(let l of s){if(!existsSync(l))continue;let g=N(l);try{let f=g.db.get(sql`
306
+ )`)}findAllThreads(t){let e=this.resolveDbPaths(),s=new Set,o=[];for(let i of e){if(!existsSync(i))continue;let a=N(i);try{let d=t?eq(p.workspace_id,t):void 0,l=a.db.select().from(p).where(d).orderBy(desc(p.updated_at)).all();for(let c of l)s.has(c.id)||(s.add(c.id),o.push(c));}catch(d){throw new _("DB_ERROR","Failed to find all threads",d)}finally{a.close();}}return o}findThreadsByIdsOrTitles(t,e){if(t.length===0)return [];let s=this.resolveDbPaths(),o=new Set,i=[];for(let a of s){if(!existsSync(a))continue;let d=N(a);try{let l=or(inArray(p.id,t),inArray(p.title,t)),c=e?and(l,eq(p.workspace_id,e)):l,u=d.db.select().from(p).where(c).orderBy(desc(p.updated_at)).all();for(let g of u)o.has(g.id)||(o.add(g.id),i.push(g));}catch(l){throw new _("DB_ERROR","Failed to find threads by ids or titles",l)}finally{d.close();}}return i}findThreadById(t,e){let s=this.resolveDbPaths();for(let o of s){if(!existsSync(o))continue;let i=N(o);try{let a=eq(p.id,t),d=e?and(a,eq(p.workspace_id,e)):a,l=i.db.select().from(p).where(d).limit(1).get()??void 0;if(l)return l}catch(a){throw new _("DB_ERROR","Failed to find thread by id",a)}finally{i.close();}}}threadExists(t,e){let s=this.resolveDbPaths();for(let o of s){if(!existsSync(o))continue;let i=N(o);try{let a=eq(p.id,t),d=e?and(a,eq(p.workspace_id,e)):a;if(i.db.select({id:p.id}).from(p).where(d).limit(1).get())return !0}catch(a){throw new _("DB_ERROR","Failed to check thread existence",a)}finally{i.close();}}return false}aggregateTaskStats(t,e){let s=this.resolveDbPaths(),o=0,i=0,a=0,d=0,l=0,c=new Set;for(let u of s){if(!existsSync(u))continue;let g=N(u);try{let f=g.db.get(sql`
307
307
  SELECT
308
308
  count(*) AS cnt,
309
309
  COALESCE(SUM(child.input_tokens), 0) AS total_input,
@@ -314,13 +314,13 @@ ${s.join(`
314
314
  WHERE child.thread_id = ${t}
315
315
  AND ${this.topLevelTaskPredicateSql()}
316
316
  ${e?sql`AND child.workspace_id = ${e}`:sql``}
317
- `);f&&(o+=f.cnt,i+=f.total_input,d+=f.total_output,a+=f.total_cached,u+=f.total_cost);let w=g.db.all(sql`
317
+ `);f&&(o+=f.cnt,i+=f.total_input,a+=f.total_output,d+=f.total_cached,l+=f.total_cost);let m=g.db.all(sql`
318
318
  SELECT DISTINCT child.agent_id FROM tasks child
319
319
  WHERE child.thread_id = ${t}
320
320
  AND child.agent_id IS NOT NULL AND child.agent_id != ''
321
321
  AND ${this.topLevelTaskPredicateSql()}
322
322
  ${e?sql`AND child.workspace_id = ${e}`:sql``}
323
- `);for(let L of w)c.add(L.agent_id);}catch(f){throw new _("DB_ERROR","Failed to aggregate task stats",f)}finally{g.close();}}return {taskCount:o,inputTokens:i,outputTokens:d,cachedInputTokens:a,costUsd:u,agentIds:Array.from(c)}}findTopLevelTasks(t,e,s){let o=this.resolveDbPaths(),i=new Set,d=[];for(let a of o){if(!existsSync(a))continue;let u=N(a);try{let c;s!==void 0?c=u.db.all(sql`
323
+ `);for(let C of m)c.add(C.agent_id);}catch(f){throw new _("DB_ERROR","Failed to aggregate task stats",f)}finally{g.close();}}return {taskCount:o,inputTokens:i,outputTokens:a,cachedInputTokens:d,costUsd:l,agentIds:Array.from(c)}}findTopLevelTasks(t,e,s){let o=this.resolveDbPaths(),i=new Set,a=[];for(let d of o){if(!existsSync(d))continue;let l=N(d);try{let c;s!==void 0?c=l.db.all(sql`
324
324
  SELECT * FROM (
325
325
  SELECT child.*,
326
326
  ROW_NUMBER() OVER (PARTITION BY child.agent_id ORDER BY child.started_at DESC) AS rn
@@ -331,13 +331,13 @@ ${s.join(`
331
331
  ) ranked
332
332
  WHERE rn <= ${s}
333
333
  ORDER BY started_at ASC
334
- `):c=u.db.all(sql`
334
+ `):c=l.db.all(sql`
335
335
  SELECT child.* FROM tasks child
336
336
  WHERE child.thread_id = ${t}
337
337
  AND ${this.topLevelTaskPredicateSql()}
338
338
  ${e?sql`AND child.workspace_id = ${e}`:sql``}
339
339
  ORDER BY child.started_at ASC
340
- `);for(let l of c)i.has(l.id)||(i.add(l.id),d.push(l));}catch(c){throw new _("DB_ERROR","Failed to find top-level tasks",c)}finally{u.close();}}if(s!==void 0&&o.length>1){let a=new Map;for(let u of d){let c=u.agent_id??"";a.has(c)||a.set(c,[]),a.get(c).push(u);}d=[];for(let u of a.values())u.sort((c,l)=>(l.started_at??"").localeCompare(c.started_at??"")),d.push(...u.slice(0,s));d.sort((u,c)=>(u.started_at??"").localeCompare(c.started_at??""));}return d}findAllTasks(t,e){let s=this.resolveDbPaths(),o=new Set,i=[];for(let d of s){if(!existsSync(d))continue;let a=N(d);try{let u=eq(n.thread_id,t),c=e?and(u,eq(n.workspace_id,e)):u,l=a.db.select().from(n).where(c).orderBy(asc(n.started_at)).all();for(let g of l)o.has(g.id)||(o.add(g.id),i.push(g));}catch(u){throw new _("DB_ERROR","Failed to find all tasks for thread",u)}finally{a.close();}}return i}findTaskById(t,e,s){let o=this.resolveDbPaths();for(let i of o){if(!existsSync(i))continue;let d=N(i);try{let a=and(eq(n.id,e),eq(n.thread_id,t)),u=s?and(a,eq(n.workspace_id,s)):a,c=d.db.select().from(n).where(u).limit(1).get();if(!c)continue;let l=d.db.select().from(n).where(eq(n.parent_task_id,c.id)).orderBy(asc(n.started_at)).all();return {task:c,children:l}}catch(a){throw new _("DB_ERROR","Failed to find task by id",a)}finally{d.close();}}}batchFetchTasksQuery(t,e){return sql`
340
+ `);for(let u of c)i.has(u.id)||(i.add(u.id),a.push(u));}catch(c){throw new _("DB_ERROR","Failed to find top-level tasks",c)}finally{l.close();}}if(s!==void 0&&o.length>1){let d=new Map;for(let l of a){let c=l.agent_id??"";d.has(c)||d.set(c,[]),d.get(c).push(l);}a=[];for(let l of d.values())l.sort((c,u)=>(u.started_at??"").localeCompare(c.started_at??"")),a.push(...l.slice(0,s));a.sort((l,c)=>(l.started_at??"").localeCompare(c.started_at??""));}return a}findAllTasks(t,e){let s=this.resolveDbPaths(),o=new Set,i=[];for(let a of s){if(!existsSync(a))continue;let d=N(a);try{let l=eq(n.thread_id,t),c=e?and(l,eq(n.workspace_id,e)):l,u=d.db.select().from(n).where(c).orderBy(asc(n.started_at)).all();for(let g of u)o.has(g.id)||(o.add(g.id),i.push(g));}catch(l){throw new _("DB_ERROR","Failed to find all tasks for thread",l)}finally{d.close();}}return i}findTaskById(t,e,s){let o=this.resolveDbPaths();for(let i of o){if(!existsSync(i))continue;let a=N(i);try{let d=and(eq(n.id,e),eq(n.thread_id,t)),l=s?and(d,eq(n.workspace_id,s)):d,c=a.db.select().from(n).where(l).limit(1).get();if(!c)continue;let u=a.db.select().from(n).where(eq(n.parent_task_id,c.id)).orderBy(asc(n.started_at)).all();return {task:c,children:u}}catch(d){throw new _("DB_ERROR","Failed to find task by id",d)}finally{a.close();}}}batchFetchTasksQuery(t,e){return sql`
341
341
  SELECT child.id, child.thread_id, child.agent_id, child.status,
342
342
  child.parent_task_id, child.started_at, child.completed_at,
343
343
  child.duration_ms, child.input_tokens, child.output_tokens,
@@ -347,7 +347,7 @@ ${s.join(`
347
347
  AND ${this.topLevelTaskPredicateSql()}
348
348
  ${e?sql`AND child.workspace_id = ${e}`:sql``}
349
349
  ORDER BY child.started_at ASC
350
- `}batchFetchTasks(t,e){let s=new Map;if(t.length===0)return s;let o=this.resolveDbPaths();for(let i of o){if(!existsSync(i))continue;let d=N(i);try{let a=d.db.all(this.batchFetchTasksQuery(t,e));for(let u of a){let c=u.thread_id;s.has(c)||s.set(c,[]),s.get(c).push(u);}}catch(a){throw new _("DB_ERROR","Failed to batch fetch tasks",a)}finally{d.close();}}return s}explainBatchFetchTasksPlan(t,e){let s=this.resolveDbPaths();for(let o of s){if(!existsSync(o))continue;let i=N(o);try{return i.db.all(sql`EXPLAIN QUERY PLAN ${this.batchFetchTasksQuery(t,e)}`).map(a=>a.detail)}finally{i.close();}}return []}updateThreadTitle(t,e,s){if(!this.dbExists())return;let o=this.openHandle(true);try{let i=eq(p.id,t),d=s?and(i,eq(p.workspace_id,s)):i;if(!o.db.select({id:p.id}).from(p).where(d).limit(1).get())return;o.db.update(p).set({title:e,title_locked:1,updated_at:new Date().toISOString()}).where(eq(p.id,t)).run();}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to update thread title",i)}finally{o.close();}}upsertThread(t,e){let s=this.openHandle(true);try{let o=e.workspaceId?this.validateWorkspaceId(s,e.workspaceId):null,i=new Date().toISOString();if(s.db.select({id:p.id,message_count:p.message_count}).from(p).where(eq(p.id,t)).limit(1).get()){let a={updated_at:i};e.title!==void 0&&(a.title=e.title),e.titleLocked!==void 0&&(a.title_locked=e.titleLocked?1:0),s.db.update(p).set(a).where(eq(p.id,t)).run();}else s.db.insert(p).values({id:t,platform:e.platform,workspace_id:o,title:e.title??null,title_locked:e.titleLocked?1:0,message_count:0,created_at:i,updated_at:i}).run();}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to upsert thread",o)}finally{s.close();}}ensureThread(t,e,s){let o=this.openHandle(true);try{let i=s?this.validateWorkspaceId(o,s):null,d=o.db.select({id:p.id,platform:p.platform,workspace_id:p.workspace_id}).from(p).where(eq(p.id,t)).limit(1).get();if(d){i&&!d.workspace_id&&o.db.update(p).set({workspace_id:i}).where(eq(p.id,t)).run();return}let a=new Date().toISOString();o.db.insert(p).values({id:t,platform:e,workspace_id:i,message_count:0,created_at:a,updated_at:a}).run();}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to ensure thread",i)}finally{o.close();}}saveUserMessage(t,e,s){if(!this.dbExists())return {firstMessage:false};let o=this.openHandle(true);try{let i=new Date().toISOString();return {firstMessage:o.db.transaction(a=>{let c=a.select({message_count:p.message_count}).from(p).where(eq(p.id,t)).limit(1).get()?.message_count===0;return a.run(sql`
350
+ `}batchFetchTasks(t,e){let s=new Map;if(t.length===0)return s;let o=this.resolveDbPaths();for(let i of o){if(!existsSync(i))continue;let a=N(i);try{let d=a.db.all(this.batchFetchTasksQuery(t,e));for(let l of d){let c=l.thread_id;s.has(c)||s.set(c,[]),s.get(c).push(l);}}catch(d){throw new _("DB_ERROR","Failed to batch fetch tasks",d)}finally{a.close();}}return s}explainBatchFetchTasksPlan(t,e){let s=this.resolveDbPaths();for(let o of s){if(!existsSync(o))continue;let i=N(o);try{return i.db.all(sql`EXPLAIN QUERY PLAN ${this.batchFetchTasksQuery(t,e)}`).map(d=>d.detail)}finally{i.close();}}return []}updateThreadTitle(t,e,s){if(!this.dbExists())return;let o=this.openHandle(true);try{let i=eq(p.id,t),a=s?and(i,eq(p.workspace_id,s)):i;if(!o.db.select({id:p.id}).from(p).where(a).limit(1).get())return;o.db.update(p).set({title:e,title_locked:1,updated_at:new Date().toISOString()}).where(eq(p.id,t)).run();}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to update thread title",i)}finally{o.close();}}upsertThread(t,e){let s=this.openHandle(true);try{let o=e.workspaceId?this.validateWorkspaceId(s,e.workspaceId):null,i=new Date().toISOString();if(s.db.select({id:p.id,message_count:p.message_count}).from(p).where(eq(p.id,t)).limit(1).get()){let d={updated_at:i};e.title!==void 0&&(d.title=e.title),e.titleLocked!==void 0&&(d.title_locked=e.titleLocked?1:0),s.db.update(p).set(d).where(eq(p.id,t)).run();}else s.db.insert(p).values({id:t,platform:e.platform,workspace_id:o,title:e.title??null,title_locked:e.titleLocked?1:0,message_count:0,created_at:i,updated_at:i}).run();}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to upsert thread",o)}finally{s.close();}}ensureThread(t,e,s){let o=this.openHandle(true);try{let i=s?this.validateWorkspaceId(o,s):null,a=o.db.select({id:p.id,platform:p.platform,workspace_id:p.workspace_id}).from(p).where(eq(p.id,t)).limit(1).get();if(a){i&&!a.workspace_id&&o.db.update(p).set({workspace_id:i}).where(eq(p.id,t)).run();return}let d=new Date().toISOString();o.db.insert(p).values({id:t,platform:e,workspace_id:i,message_count:0,created_at:d,updated_at:d}).run();}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to ensure thread",i)}finally{o.close();}}saveUserMessage(t,e,s){if(!this.dbExists())return {firstMessage:false};let o=this.openHandle(true);try{let i=new Date().toISOString();return {firstMessage:o.db.transaction(d=>{let c=d.select({message_count:p.message_count}).from(p).where(eq(p.id,t)).limit(1).get()?.message_count===0;return d.run(sql`
351
351
  UPDATE threads
352
352
  SET first_message = COALESCE(first_message, ${e}),
353
353
  title = CASE WHEN title_locked = 0 AND title IS NULL THEN substr(${e}, 1, 60) ELSE title END,
@@ -355,8 +355,8 @@ ${s.join(`
355
355
  message_count = message_count + 1,
356
356
  updated_at = ${i}
357
357
  WHERE id = ${t}
358
- `),c},{behavior:"immediate"})}}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to save user message",i)}finally{o.close();}}saveAssistantMessage(t,e,s){if(!this.dbExists())return;let o=this.openHandle(true);try{let i=new Date().toISOString();o.db.update(p).set({last_message:e,updated_at:i}).where(eq(p.id,t)).run();}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to save assistant message",i)}finally{o.close();}}updateThread(t,e){if(!this.dbExists())return;let s=this.openHandle(true);try{let o={updated_at:new Date().toISOString()};e.title!==void 0&&(o.title=e.title,o.title_locked=1),e.titleLocked!==void 0&&(o.title_locked=e.titleLocked?1:0),s.db.update(p).set(o).where(eq(p.id,t)).run();}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to update thread",o)}finally{s.close();}}togglePin(t,e){let s=this.openHandle(true);try{let o=e?and(eq(p.id,t),eq(p.workspace_id,e)):eq(p.id,t),i=s.db.select({pinned:p.pinned,metadata:p.metadata}).from(p).where(o).get();if(!i)return null;let d=i.pinned?0:1,a=i.metadata?JSON.parse(i.metadata):{};if(d){let u=e?and(eq(p.pinned,1),eq(p.workspace_id,e)):eq(p.pinned,1),c=s.db.select({metadata:p.metadata}).from(p).where(u).all(),l=null;for(let g of c){let f=g.metadata?JSON.parse(g.metadata):{};typeof f.pinOrder=="number"&&(l===null||f.pinOrder<l)&&(l=f.pinOrder);}a.pinOrder=l===null?0:l-1;}else delete a.pinOrder;return s.db.update(p).set({pinned:d,metadata:Object.keys(a).length>0?JSON.stringify(a):null}).where(o).run(),{pinned:!!d}}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to toggle pin",o)}finally{s.close();}}reorderPins(t,e){let s=this.openHandle(true);try{for(let o=0;o<t.length;o++){let i=e?and(eq(p.id,t[o]),eq(p.workspace_id,e)):eq(p.id,t[o]),d=s.db.select({metadata:p.metadata}).from(p).where(i).get();if(!d)continue;let a=d.metadata?JSON.parse(d.metadata):{};a.pinOrder=o+1,s.db.update(p).set({metadata:JSON.stringify(a)}).where(i).run();}}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to reorder pins",o)}finally{s.close();}}toggleStar(t,e){let s=this.openHandle(true);try{let o=e?and(eq(p.id,t),eq(p.workspace_id,e)):eq(p.id,t),i=s.db.select({starred:p.starred}).from(p).where(o).get();if(!i)return null;let d=i.starred?0:1;return s.db.update(p).set({starred:d}).where(o).run(),{starred:!!d}}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to toggle star",o)}finally{s.close();}}resolveOverdriveForRequest(t,e,s,o){let d=this.openHandle(true);try{let a=e?and(eq(p.id,t),eq(p.workspace_id,e)):eq(p.id,t);return d.db.transaction(c=>{let l=c.select({metadata:p.metadata}).from(p).where(a).get();if(!l)return null;let g=l.metadata?JSON.parse(l.metadata):{},f=g.overdrive??{},w=o?.defaultTurns??f.defaultTurns??3,L=o?.updatedBy??"ui",O=new Date().toISOString(),q,z,V,ft;switch(s){case "enable-count":{let mt=o?.turns??w,ht=o?.consumeCurrent?Math.max(mt-1,0):mt;q=ht>0?"count":"off",z=ht,V=!0,ft="count";break}case "enable-latch":{q="latch",z=0,V=!0,ft="latch";break}case "disable":{q="off",z=0,V=!1;break}default:{let mt=f.state??"off",ht=typeof f.remaining=="number"?f.remaining:0;if(mt==="count"&&ht>0){let Gt=ht-1;Gt<=0?(q="off",z=0):(q="count",z=Gt),V=!0,ft="count";}else mt==="latch"?(q="latch",z=0,V=!0,ft="latch"):(q="off",z=0,V=!1);break}}return g.overdrive={state:q,remaining:z,defaultTurns:w,updatedAt:O,updatedBy:L},c.update(p).set({metadata:JSON.stringify(g),updated_at:O}).where(a).run(),{applied:V,appliedMode:ft,state:q,remaining:z}},{behavior:"immediate"})}catch(a){throw a instanceof _?a:new _("DB_ERROR","Failed to resolve overdrive",a)}finally{d.close();}}};function Kn(r){return r.replace(/<conversation_history[^>]*>[\s\S]*?<\/conversation_history>/g,"").split(`
358
+ `),c},{behavior:"immediate"})}}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to save user message",i)}finally{o.close();}}saveAssistantMessage(t,e,s){if(!this.dbExists())return;let o=this.openHandle(true);try{let i=new Date().toISOString();o.db.update(p).set({last_message:e,updated_at:i}).where(eq(p.id,t)).run();}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to save assistant message",i)}finally{o.close();}}updateThread(t,e){if(!this.dbExists())return;let s=this.openHandle(true);try{let o={updated_at:new Date().toISOString()};e.title!==void 0&&(o.title=e.title,o.title_locked=1),e.titleLocked!==void 0&&(o.title_locked=e.titleLocked?1:0),s.db.update(p).set(o).where(eq(p.id,t)).run();}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to update thread",o)}finally{s.close();}}patchThreadTitleMetadata(t,e,s){let o=e;if(!o||typeof o!="object"||Array.isArray(o))return new ht("THREAD_TITLE_PATCH_INVALID","Thread-title metadata patch must be an object");if(Object.keys(o).some(l=>l!=="priority"&&l!=="state"))return new ht("THREAD_TITLE_PATCH_INVALID","Thread-title metadata patch only supports priority and state");let a=o;if(a.priority===void 0&&a.state===void 0)return new ht("THREAD_TITLE_PATCH_EMPTY","Thread-title metadata patch must change priority or state");for(let l of [a.priority,a.state])if(l!=null&&(typeof l!="string"||l.length===0))return new ht("THREAD_TITLE_PATCH_INVALID","Thread-title metadata values must be non-empty strings or null");let d=this.openHandle(true);try{let l=s?and(eq(p.id,t),eq(p.workspace_id,s)):eq(p.id,t);return d.db.transaction(c=>{let u=c.select({metadata:p.metadata}).from(p).where(l).get();if(!u)return null;let g=Nt(u.metadata),f=g.threadTitle,m=f&&typeof f=="object"&&!Array.isArray(f)?{...f}:{};a.priority===null?delete m.priority:a.priority!==void 0&&(m.priority=a.priority),a.state===null?delete m.state:a.state!==void 0&&(m.state=a.state),Object.keys(m).length>0?g.threadTitle=m:delete g.threadTitle;let C=Object.keys(g).length>0?JSON.stringify(g):null,D=new Date().toISOString();c.update(p).set({metadata:C,updated_at:D}).where(l).run();let O=c.select({metadata:p.metadata}).from(p).where(l).get();if(!O)throw new _("DB_ERROR","Thread-title metadata update could not be read back");let $=ns(O.metadata);return {threadId:t,priority:$.priority,state:$.state}},{behavior:"immediate"})}catch(l){throw l instanceof _?l:new _("DB_ERROR","Failed to patch thread-title metadata",l)}finally{d.close();}}togglePin(t,e){let s=this.openHandle(true);try{let o=e?and(eq(p.id,t),eq(p.workspace_id,e)):eq(p.id,t);return s.db.transaction(i=>{let a=i.select({pinned:p.pinned,metadata:p.metadata}).from(p).where(o).get();if(!a)return null;let d=a.pinned?0:1,l=Nt(a.metadata);if(d){let c=e?and(eq(p.pinned,1),eq(p.workspace_id,e)):eq(p.pinned,1),u=i.select({metadata:p.metadata}).from(p).where(c).all(),g=null;for(let f of u){let m=Nt(f.metadata);typeof m.pinOrder=="number"&&(g===null||m.pinOrder<g)&&(g=m.pinOrder);}l.pinOrder=g===null?0:g-1;}else delete l.pinOrder;return i.update(p).set({pinned:d,metadata:Object.keys(l).length>0?JSON.stringify(l):null}).where(o).run(),{pinned:!!d}},{behavior:"immediate"})}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to toggle pin",o)}finally{s.close();}}reorderPins(t,e){let s=this.openHandle(true);try{s.db.transaction(o=>{for(let i=0;i<t.length;i++){let a=e?and(eq(p.id,t[i]),eq(p.workspace_id,e)):eq(p.id,t[i]),d=o.select({metadata:p.metadata}).from(p).where(a).get();if(!d)continue;let l=Nt(d.metadata);l.pinOrder=i+1,o.update(p).set({metadata:JSON.stringify(l)}).where(a).run();}},{behavior:"immediate"});}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to reorder pins",o)}finally{s.close();}}toggleStar(t,e){let s=this.openHandle(true);try{let o=e?and(eq(p.id,t),eq(p.workspace_id,e)):eq(p.id,t),i=s.db.select({starred:p.starred}).from(p).where(o).get();if(!i)return null;let a=i.starred?0:1;return s.db.update(p).set({starred:a}).where(o).run(),{starred:!!a}}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to toggle star",o)}finally{s.close();}}resolveOverdriveForRequest(t,e,s,o){let a=this.openHandle(true);try{let d=e?and(eq(p.id,t),eq(p.workspace_id,e)):eq(p.id,t);return a.db.transaction(c=>{let u=c.select({metadata:p.metadata}).from(p).where(d).get();if(!u)return null;let g=u.metadata?JSON.parse(u.metadata):{},f=g.overdrive??{},m=o?.defaultTurns??f.defaultTurns??3,C=o?.updatedBy??"ui",D=new Date().toISOString(),O,$,X,kt;switch(s){case "enable-count":{let wt=o?.turns??m,Et=o?.consumeCurrent?Math.max(wt-1,0):wt;O=Et>0?"count":"off",$=Et,X=!0,kt="count";break}case "enable-latch":{O="latch",$=0,X=!0,kt="latch";break}case "disable":{O="off",$=0,X=!1;break}default:{let wt=f.state??"off",Et=typeof f.remaining=="number"?f.remaining:0;if(wt==="count"&&Et>0){let Zt=Et-1;Zt<=0?(O="off",$=0):(O="count",$=Zt),X=!0,kt="count";}else wt==="latch"?(O="latch",$=0,X=!0,kt="latch"):(O="off",$=0,X=!1);break}}return g.overdrive={state:O,remaining:$,defaultTurns:m,updatedAt:D,updatedBy:C},c.update(p).set({metadata:JSON.stringify(g),updated_at:D}).where(d).run(),{applied:X,appliedMode:kt,state:O,remaining:$}},{behavior:"immediate"})}catch(d){throw d instanceof _?d:new _("DB_ERROR","Failed to resolve overdrive",d)}finally{a.close();}}};function os(r){return r.replace(/<conversation_history[^>]*>[\s\S]*?<\/conversation_history>/g,"").split(`
359
359
  `).filter(o=>!(o.startsWith("Loaded ")&&o.includes("layouts from")||o.includes("[dotenv@")||o.includes("[Nest]")&&o.includes("DEBUG")||o.startsWith("Registered custom layout:")||o.startsWith("Updated custom layout:"))).join(`
360
- `).trim()}function Gn(r){if(!r)return "";let t=r;try{let e=JSON.parse(t);Array.isArray(e)?t=e.filter(s=>s?.type==="text"&&s?.text).map(s=>s.text).join(`
361
- `):e&&typeof e=="object"&&e.result!==void 0&&(t=e.result||"");}catch{t=Kn(t);}return t}var Ot=class{dbPath;constructor(t){this.dbPath=t??join(homedir(),".crewx","crewx.db");}getThreadRepo(){return new Lt({dbPath:this.dbPath})}updateThread(t,e){this.getThreadRepo().updateThread(t,{title:e.title});}async ensureThread(t,e,s){let o=this.getThreadRepo(),i=o.findThreadById(t);if(i){if(i.platform!==e)throw new Error(`Thread '${t}' already exists with platform '${i.platform}' \u2014 cannot change to '${e}' (platform is immutable)`);return {created:false}}return o.ensureThread(t,e,s),{created:true}}async fetchHistory(t,e){let s=e?.limit??100,o=this.getThreadRepo(),i=o.findThreadById(t),d=o.findTopLevelTasks(t,void 0,s),a=new Set(["queued","cancelled"]);d=d.filter(l=>(!l.status||!a.has(l.status))&&(!e?.currentTraceId||l.trace_id!==e.currentTraceId));let u=i?.platform??"cli",c=this.rowsToMessages(d);return {threadId:t,platform:u,messages:c,metadata:{title:i?.title??void 0,firstMessage:i?.first_message??void 0,lastMessage:i?.last_message??void 0,messageCount:i?.message_count??0,updatedAt:i?.updated_at?new Date(i.updated_at).getTime():void 0}}}async saveUserMessage(t,e,s,o){let{firstMessage:i}=this.getThreadRepo().saveUserMessage(t,e);return {id:t,firstMessage:i}}async saveAssistantMessage(t,e,s,o){return this.getThreadRepo().saveAssistantMessage(t,e),{id:t}}close(){}normalizeStatus(t){if(t&&!["success","completed","done"].includes(t)){if(["failed","error"].includes(t))return "failed";if(t==="running")return "running"}}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(),metadata:{caller_agent_id:s.caller_agent_id}});let o=Gn(s.result),i=this.normalizeStatus(s.status);if(o||i==="running"||i==="failed"){let d={agent_id:s.agent_id,task_id:s.id};i&&(d.status=i),i==="failed"&&s.error&&(d.reason=s.error.split(`
362
- `)[0].slice(0,200)),e.push({id:`${s.id}-assistant`,text:o,isAssistant:true,timestamp:new Date(s.started_at).getTime(),metadata:d});}}return e}};var Kt=class extends it{name="conversation";_provider;unsubStart=null;unsubEnd=null;constructor(t){super(),this._provider=new Ot(t?.dbPath);}get conversationProvider(){return this._provider}async afterUserMessage(t,e,s,o){}async afterAssistantMessage(t,e,s){}attach(t){this.unsubStart=t.on("task:start",async e=>{if(!e.threadId)return;let s=e.platform??"cli";try{let o=await this._provider.ensureThread(e.threadId,s,e.workspaceId),i=await this._provider.saveUserMessage(e.threadId,e.message??"");await this.afterUserMessage(e.threadId,i.id,o.created||i.firstMessage,e);}catch{}}),this.unsubEnd=t.on("task:end",async e=>{if(!e.result)return;let s=e.metadata?.threadId;if(!s)return;let o=e.agentRef?.replace(/^@/,"")??"";try{let{id:i}=await this._provider.saveAssistantMessage(s,e.result,o);await this.afterAssistantMessage(s,i,e);}catch{}});}detach(t){this.unsubStart?.(),this.unsubStart=null,this.unsubEnd?.(),this.unsubEnd=null,this._provider.close?.();}};export{Kt as ConversationPlugin,Pt as FileLoggerPlugin,Wt as SqliteTracingPlugin};
360
+ `).trim()}function is(r){if(!r)return "";let t=r;try{let e=JSON.parse(t);Array.isArray(e)?t=e.filter(s=>s?.type==="text"&&s?.text).map(s=>s.text).join(`
361
+ `):e&&typeof e=="object"&&e.result!==void 0&&(t=e.result||"");}catch{t=os(t);}return t}var Mt=class{dbPath;constructor(t){this.dbPath=t??join(homedir(),".crewx","crewx.db");}getThreadRepo(){return new Pt({dbPath:this.dbPath})}updateThread(t,e){this.getThreadRepo().updateThread(t,{title:e.title});}async ensureThread(t,e,s){let o=this.getThreadRepo(),i=o.findThreadById(t);if(i){if(i.platform!==e)throw new Error(`Thread '${t}' already exists with platform '${i.platform}' \u2014 cannot change to '${e}' (platform is immutable)`);return {created:false}}return o.ensureThread(t,e,s),{created:true}}async fetchHistory(t,e){let s=e?.limit??100,o=this.getThreadRepo(),i=o.findThreadById(t),a=o.findTopLevelTasks(t,void 0,s),d=new Set(["queued","cancelled"]);a=a.filter(u=>(!u.status||!d.has(u.status))&&(!e?.currentTraceId||u.trace_id!==e.currentTraceId));let l=i?.platform??"cli",c=this.rowsToMessages(a);return {threadId:t,platform:l,messages:c,metadata:{title:i?.title??void 0,firstMessage:i?.first_message??void 0,lastMessage:i?.last_message??void 0,messageCount:i?.message_count??0,updatedAt:i?.updated_at?new Date(i.updated_at).getTime():void 0}}}async saveUserMessage(t,e,s,o){let{firstMessage:i}=this.getThreadRepo().saveUserMessage(t,e);return {id:t,firstMessage:i}}async saveAssistantMessage(t,e,s,o){return this.getThreadRepo().saveAssistantMessage(t,e),{id:t}}close(){}normalizeStatus(t){if(t&&!["success","completed","done"].includes(t)){if(["failed","error"].includes(t))return "failed";if(t==="running")return "running"}}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(),metadata:{caller_agent_id:s.caller_agent_id}});let o=is(s.result),i=this.normalizeStatus(s.status);if(o||i==="running"||i==="failed"){let a={agent_id:s.agent_id,task_id:s.id};i&&(a.status=i),i==="failed"&&s.error&&(a.reason=s.error.split(`
362
+ `)[0].slice(0,200)),e.push({id:`${s.id}-assistant`,text:o,isAssistant:true,timestamp:new Date(s.started_at).getTime(),metadata:a});}}return e}};var Qt=class extends rt{name="conversation";_provider;unsubStart=null;unsubEnd=null;constructor(t){super(),this._provider=new Mt(t?.dbPath);}get conversationProvider(){return this._provider}async afterUserMessage(t,e,s,o){}async afterAssistantMessage(t,e,s){}attach(t){this.unsubStart=t.on("task:start",async e=>{if(!e.threadId)return;let s=e.platform??"cli";try{let o=await this._provider.ensureThread(e.threadId,s,e.workspaceId),i=await this._provider.saveUserMessage(e.threadId,e.message??"");await this.afterUserMessage(e.threadId,i.id,o.created||i.firstMessage,e);}catch{}}),this.unsubEnd=t.on("task:end",async e=>{if(!e.result)return;let s=e.metadata?.threadId;if(!s)return;let o=e.agentRef?.replace(/^@/,"")??"";try{let{id:i}=await this._provider.saveAssistantMessage(s,e.result,o);await this.afterAssistantMessage(s,i,e);}catch{}});}detach(t){this.unsubStart?.(),this.unsubStart=null,this.unsubEnd?.(),this.unsubEnd=null,this._provider.close?.();}};export{Qt as ConversationPlugin,Ut as FileLoggerPlugin,Vt as SqliteTracingPlugin};