@crewx/sdk 0.9.0-rc.37 → 0.9.0-rc.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/esm/index.js +41 -40
- package/dist/esm/plugins/index.js +14 -14
- package/dist/esm/repository/index.js +6 -6
- package/dist/esm/testing/index.js +1 -1
- package/dist/facade/Crewx.d.ts +1 -0
- package/dist/index.browser.js +1 -1
- package/dist/index.js +44 -43
- package/dist/provider/errors.d.ts +1 -0
- package/dist/provider/parse-usage.d.ts +6 -0
- package/dist/repository/agent-suggestion.repository.d.ts +1 -0
- package/dist/repository/index.js +6 -6
- package/dist/testing/index.js +1 -1
- package/package.json +1 -1
- package/templates/agents/default.yaml +31 -24
- package/templates/documents/crewx-manual.md +138 -138
|
@@ -8,12 +8,12 @@ Message: ${e.message}
|
|
|
8
8
|
`;writeFileSync(r,o,{encoding:"utf8",mode:384});}catch{}}),t.on("task:output",e=>{try{let s=this.logFiles.get(e.traceId);if(!s)return;let r=new Date().toISOString();appendFileSync(s,`[${r}] STDOUT: ${e.output}
|
|
9
9
|
`,"utf8");}catch{}}),t.on("task:end",e=>{try{let s=this.logFiles.get(e.traceId);if(!s)return;let r=new Date().toLocaleString(),o=e.error?`failed: ${e.error.message}`:"completed successfully",a=`[${r}] INFO: Task ${o} in ${e.durationMs}ms
|
|
10
10
|
[${r}] INFO: Process closed with exit code: ${e.error?1:0}
|
|
11
|
-
`;appendFileSync(s,a,"utf8"),this.logFiles.delete(e.traceId);}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[],this.logFiles.clear();}};function se(d){let t=F.resolve(d);return process.platform==="win32"&&(t=t.replace(/\\/g,"/"),t=t.replace(/^([A-Z]):/,(e,s)=>`${s.toLowerCase()}:`)),t.length>1&&!/^[a-zA-Z]:\/$/.test(t)&&(t=t.replace(/\/+$/,"")),t}function vt(d){let t=se(d);return createHash("sha256").update(t).digest("hex")}var tt=class{resolveDbPath(){return process.env.CREWX_DB?process.env.CREWX_DB:process.env.CREWX_TRACES_DB?process.env.CREWX_TRACES_DB:join(ae.homedir(),".crewx","crewx.db")}resolveDbPaths(){return [this.resolveDbPath()]}isMissingTableError(t){return t instanceof Error&&/no such table:/i.test(t.message)}dbExists(t){return existsSync(t??this.resolveDbPath())}};function R(d){let t=dt("better-sqlite3"),{drizzle:e}=dt("drizzle-orm/better-sqlite3"),s=new t(d);return s.exec("PRAGMA journal_mode = WAL"),s.exec("PRAGMA busy_timeout = 5000"),s.exec("PRAGMA foreign_keys = ON"),s.exec("PRAGMA analysis_limit = 400"),s.exec("PRAGMA optimize"),{db:e(s),runRaw:(r,o=[])=>s.prepare(r).run(...o),close:()=>s.close()}}var $t=new Set,ie={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 Et(d,t){return (d.get(`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='${t}'`)?.cnt??0)>0}function de(d,t){if(t>0||!Et(d,"tasks"))return;let e=d.all("PRAGMA table_info(tasks)"),s=new Set(e.map(r=>r.name));for(let[r,o]of Object.entries(ie))s.has(r)||d.run(`ALTER TABLE tasks ADD COLUMN ${r} ${o}`);}var le={"0002_normalize_task_names":{workspace_name:"TEXT",project_name:"TEXT"}};function ce(d,t,e){if(!Et(d,"__drizzle_migrations")||!Et(d,"tasks"))return;let s=d.all(e`SELECT hash FROM __drizzle_migrations`),r=new Set(s.map(a=>a.hash)),o=JSON.parse(readFileSync(F__default.join(t,"meta/_journal.json"),"utf-8"));for(let a of o.entries){let i=le[a.tag];if(!i)continue;let c=F__default.join(t,`${a.tag}.sql`);if(!existsSync(c))continue;let l=readFileSync(c,"utf-8"),u=createHash("sha256").update(l).digest("hex");if(r.has(u))continue;let h=d.all("PRAGMA table_info(tasks)"),f=new Set(h.map(y=>y.name));for(let[y,C]of Object.entries(i))f.has(y)||(d.run(`ALTER TABLE tasks ADD COLUMN ${y} ${C}`),f.add(y));}}function ue(d,t,e){let s=d.all(e`SELECT hash FROM __drizzle_migrations`),r=new Set(s.map(a=>a.hash)),o=JSON.parse(readFileSync(F__default.join(t,"meta/_journal.json"),"utf-8"));for(let a of o.entries){let i=F__default.join(t,`${a.tag}.sql`);if(!existsSync(i))continue;let c=readFileSync(i,"utf-8"),l=createHash("sha256").update(c).digest("hex");if(r.has(l))continue;let u=/ALTER\s+TABLE\s+[`"]?(\w+)[`"]?\s+ADD\s+[`"]?(\w+)[`"]?/gi,h=[],f;for(;(f=u.exec(c))!==null;)h.push({table:f[1],column:f[2]});if(h.length===0||!c.split(/-->\s*statement-breakpoint/).map(N=>N.trim()).filter(Boolean).every(N=>/^ALTER\s+TABLE\s+.+\s+ADD\s+/i.test(N)))continue;h.every(({table:N,column:I})=>d.all(`PRAGMA table_info("${N}")`).some(B=>B.name===I))&&d.run(e`INSERT INTO __drizzle_migrations (hash, created_at) VALUES (${l}, ${a.when})`);}}function
|
|
11
|
+
`;appendFileSync(s,a,"utf8"),this.logFiles.delete(e.traceId);}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[],this.logFiles.clear();}};function se(d){let t=F.resolve(d);return process.platform==="win32"&&(t=t.replace(/\\/g,"/"),t=t.replace(/^([A-Z]):/,(e,s)=>`${s.toLowerCase()}:`)),t.length>1&&!/^[a-zA-Z]:\/$/.test(t)&&(t=t.replace(/\/+$/,"")),t}function vt(d){let t=se(d);return createHash("sha256").update(t).digest("hex")}var tt=class{resolveDbPath(){return process.env.CREWX_DB?process.env.CREWX_DB:process.env.CREWX_TRACES_DB?process.env.CREWX_TRACES_DB:join(ae.homedir(),".crewx","crewx.db")}resolveDbPaths(){return [this.resolveDbPath()]}isMissingTableError(t){return t instanceof Error&&/no such table:/i.test(t.message)}dbExists(t){return existsSync(t??this.resolveDbPath())}};function R(d){let t=dt("better-sqlite3"),{drizzle:e}=dt("drizzle-orm/better-sqlite3"),s=new t(d);return s.exec("PRAGMA journal_mode = WAL"),s.exec("PRAGMA busy_timeout = 5000"),s.exec("PRAGMA foreign_keys = ON"),s.exec("PRAGMA analysis_limit = 400"),s.exec("PRAGMA optimize"),{db:e(s),runRaw:(r,o=[])=>s.prepare(r).run(...o),close:()=>s.close()}}var $t=new Set,ie={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 Et(d,t){return (d.get(`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='${t}'`)?.cnt??0)>0}function de(d,t){if(t>0||!Et(d,"tasks"))return;let e=d.all("PRAGMA table_info(tasks)"),s=new Set(e.map(r=>r.name));for(let[r,o]of Object.entries(ie))s.has(r)||d.run(`ALTER TABLE tasks ADD COLUMN ${r} ${o}`);}var le={"0002_normalize_task_names":{workspace_name:"TEXT",project_name:"TEXT"}};function ce(d,t,e){if(!Et(d,"__drizzle_migrations")||!Et(d,"tasks"))return;let s=d.all(e`SELECT hash FROM __drizzle_migrations`),r=new Set(s.map(a=>a.hash)),o=JSON.parse(readFileSync(F__default.join(t,"meta/_journal.json"),"utf-8"));for(let a of o.entries){let i=le[a.tag];if(!i)continue;let c=F__default.join(t,`${a.tag}.sql`);if(!existsSync(c))continue;let l=readFileSync(c,"utf-8"),u=createHash("sha256").update(l).digest("hex");if(r.has(u))continue;let h=d.all("PRAGMA table_info(tasks)"),f=new Set(h.map(y=>y.name));for(let[y,C]of Object.entries(i))f.has(y)||(d.run(`ALTER TABLE tasks ADD COLUMN ${y} ${C}`),f.add(y));}}function ue(d,t,e){let s=d.all(e`SELECT hash FROM __drizzle_migrations`),r=new Set(s.map(a=>a.hash)),o=JSON.parse(readFileSync(F__default.join(t,"meta/_journal.json"),"utf-8"));for(let a of o.entries){let i=F__default.join(t,`${a.tag}.sql`);if(!existsSync(i))continue;let c=readFileSync(i,"utf-8"),l=createHash("sha256").update(c).digest("hex");if(r.has(l))continue;let u=/ALTER\s+TABLE\s+[`"]?(\w+)[`"]?\s+ADD\s+[`"]?(\w+)[`"]?/gi,h=[],f;for(;(f=u.exec(c))!==null;)h.push({table:f[1],column:f[2]});if(h.length===0||!c.split(/-->\s*statement-breakpoint/).map(N=>N.trim()).filter(Boolean).every(N=>/^ALTER\s+TABLE\s+.+\s+ADD\s+/i.test(N)))continue;h.every(({table:N,column:I})=>d.all(`PRAGMA table_info("${N}")`).some(B=>B.name===I))&&d.run(e`INSERT INTO __drizzle_migrations (hash, created_at) VALUES (${l}, ${a.when})`);}}function pe(d){let{migrate:t}=dt("drizzle-orm/better-sqlite3/migrator"),{sql:e}=dt("drizzle-orm"),s=[F__default.join(k,"../migrations"),F__default.join(k,"migrations"),F__default.join(k,"../../../../drizzle/migrations"),F__default.join(process.cwd(),"drizzle/migrations")],r=s.find(l=>existsSync(F__default.join(l,"meta/_journal.json")));if(!r)throw new Error(`migrations folder not found. Searched:
|
|
12
12
|
${s.join(`
|
|
13
|
-
`)}`);let o=d.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),a=0;o?.cnt&&(a=d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),de(d,a),o?.cnt&&(ue(d,r,e),ce(d,r,e)),t(d,{migrationsFolder:r});let c=(d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0)-a;if(c>0){let l=o?.cnt?"Database migrated":"Database initialized";console.error(`[crewx] ${l} (${c} migration${c>1?"s":""} applied).`);}}function ct(d,t){$t.has(t)||(_e(d),$t.add(t));}var p=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 et=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"),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)},d=>({idx_tasks_agent_id:index("idx_tasks_agent_id").on(d.agent_id),idx_tasks_status:index("idx_tasks_status").on(d.status),idx_tasks_started_at:index("idx_tasks_started_at").on(d.started_at),idx_tasks_trace_id:index("idx_tasks_trace_id").on(d.trace_id),idx_tasks_parent_task_id:index("idx_tasks_parent_task_id").on(d.parent_task_id),idx_tasks_crewx_version:index("idx_tasks_crewx_version").on(d.crewx_version),idx_tasks_pid:index("idx_tasks_pid").on(d.pid),idx_tasks_thread_id:index("idx_tasks_thread_id").on(d.thread_id),idx_tasks_workspace_id:index("idx_tasks_workspace_id").on(d.workspace_id),idx_tasks_workspace_ref:index("idx_tasks_workspace_ref").on(d.workspace_ref),idx_tasks_project_id:index("idx_tasks_project_id").on(d.project_id),idx_tasks_ws_started:index("idx_tasks_ws_started").on(d.workspace_id,d.started_at)}));var _=sqliteTable("threads",{id:text("id").primaryKey(),workspace_id:text("workspace_id").references(()=>et.id,{onDelete:"set null"}),platform:text("platform").notNull().default("cli"),title:text("title"),first_message:text("first_message"),last_message:text("last_message"),message_count:integer("message_count").notNull().default(0),created_at:text("created_at").notNull(),updated_at:text("updated_at").notNull(),metadata:text("metadata"),title_locked:integer("title_locked").notNull().default(0),pinned:integer("pinned").notNull().default(0),starred:integer("starred").notNull().default(0)},d=>({idx_threads_updated_at:index("idx_threads_updated_at").on(d.updated_at),idx_threads_workspace_id:index("idx_threads_workspace_id").on(d.workspace_id),idx_threads_ws_updated:index("idx_threads_ws_updated").on(d.workspace_id,d.updated_at),idx_threads_title:index("idx_threads_title").on(d.title)}));var Ee=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(()=>Ee.id,{onDelete:"set null"}),name:text("name").notNull(),kind:text("kind").notNull().default("internal"),status:text("status").notNull().default("ok"),started_at:text("started_at").notNull(),completed_at:text("completed_at"),duration_ms:integer("duration_ms"),input:text("input"),output:text("output"),error:text("error"),attributes:text("attributes")},d=>({idx_spans_task_id:index("idx_spans_task_id").on(d.task_id),idx_spans_parent_span_id:index("idx_spans_parent_span_id").on(d.parent_span_id)}));sqliteTable("tool_calls",{id:text("id").primaryKey(),task_id:text("task_id").references(()=>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()},d=>({idx_tool_calls_task_id:index("idx_tool_calls_task_id").on(d.task_id),idx_tool_calls_tool_name:index("idx_tool_calls_tool_name").on(d.tool_name),idx_tool_calls_timestamp:index("idx_tool_calls_timestamp").on(d.timestamp)}));sqliteTable("thread_boxes",{id:text("id").primaryKey(),thread_id:text("thread_id").notNull().references(()=>_.id,{onDelete:"cascade"}),seq:integer("seq").notNull(),first_task_id:text("first_task_id").notNull(),mid_task_id:text("mid_task_id").notNull(),last_task_id:text("last_task_id").notNull(),task_count:integer("task_count").notNull(),summary:text("summary"),source_tokens:integer("source_tokens").notNull(),summary_tokens:integer("summary_tokens"),created_at:text("created_at").notNull()},d=>({idx_thread_boxes_thread_id:index("idx_thread_boxes_thread_id").on(d.thread_id),idx_thread_boxes_seq:index("idx_thread_boxes_seq").on(d.thread_id,d.seq),uniq_thread_boxes_thread_seq:unique().on(d.thread_id,d.seq)}));sqliteTable("request_logs",{id:text("id").primaryKey(),path:text("path").notNull(),method:text("method").notNull(),status_code:integer("status_code").notNull(),duration_ms:integer("duration_ms").notNull(),ip:text("ip"),request_headers:text("request_headers"),response_headers:text("response_headers"),request_body:text("request_body"),response_body:text("response_body"),query:text("query"),user_id:text("user_id"),project_id:text("project_id"),partition_key:text("partition_key").notNull(),timestamp:text("timestamp").notNull().default(sql`(datetime('now'))`),metadata:text("metadata")},d=>({idx_request_logs_timestamp:index("idx_request_logs_timestamp").on(d.timestamp),idx_request_logs_path:index("idx_request_logs_path").on(d.path),idx_request_logs_status_code:index("idx_request_logs_status_code").on(d.status_code),idx_request_logs_partition_key:index("idx_request_logs_partition_key").on(d.partition_key)}));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")},d=>({uniq_usage_limit_snapshots_bucket:uniqueIndex("uniq_usage_limit_snapshots_bucket").on(d.provider,d.account_ref,d.limit_window,d.bucket_start),idx_usage_limit_snapshots_window_bucket:index("idx_usage_limit_snapshots_window_bucket").on(d.limit_window,d.bucket_start),idx_usage_limit_snapshots_provider_window_bucket:index("idx_usage_limit_snapshots_provider_window_bucket").on(d.provider,d.limit_window,d.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")},d=>({uniq_usage_reports_ws_month:uniqueIndex("uniq_usage_reports_ws_month").on(d.workspace_id,d.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")},d=>({idx_notifications_ws_created:index("idx_notifications_ws_created").on(d.workspace_id,sql`${d.created_at} DESC`)}));sqliteTable("notification_reads",{notification_id:text("notification_id").notNull(),user_id:text("user_id").notNull(),read_at:text("read_at").notNull()},d=>({pk:primaryKey({columns:[d.notification_id,d.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()},d=>({idx_agent_suggestions_ws_agent:index("idx_agent_suggestions_ws_agent").on(d.workspace_id,d.agent_id),idx_agent_suggestions_ws_created:index("idx_agent_suggestions_ws_created").on(d.workspace_id,d.created_at)}));function qt(d){let t=d.toLowerCase().trim();t=t.replace(/\[[^\]]*\]$/g,"");let e=t.lastIndexOf("/");return e>=0&&(t=t.slice(e+1)),t=t.replace(/-\d{8}$/,""),t}var G="2026-05-09",ze="0.8.9-rc.13",X=10,Y=parseInt(ze.split("rc.")[1]),We=new Set(["workflow","mcp"]),Wt=600*1e3,mt=class extends tt{dbPath;pidNullObservedSince=new Map;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true});}else if(!existsSync(e))throw new p("NOT_FOUND","Database not found");let s=R(e);if(t)try{ct(s.db,e);}catch(r){throw s.close(),r}return s}startTask(t){let e=this.openHandle(true);try{e.db.insert(n).values({id:t.id,agent_id:t.agentId,prompt:t.prompt,mode:t.mode,status:t.status,started_at:t.startedAt,pid:t.pid??null,parent_task_id:t.parentTaskId??null,caller_agent_id:t.callerAgentId??null,trace_id:t.traceId??null,command:t.command??null,metadata:t.metadata??null,workspace_id:t.workspaceId??null,platform:t.platform??"cli",crewx_version:t.crewxVersion??null,thread_id:t.threadId??null,model:t.model??null,rendered_prompt:t.renderedPrompt??null,coding_agent_command:t.codingAgentCommand??null}).onConflictDoUpdate({target:n.id,set:{pid:sql`COALESCE(excluded.pid, ${n.pid})`},setWhere:eq(n.status,"running")}).run();}catch(s){throw s instanceof p?s:new p("DB_ERROR","Failed to start task",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=?,
|
|
13
|
+
`)}`);let o=d.get(e`SELECT count(*) as cnt FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'`),a=0;o?.cnt&&(a=d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0),de(d,a),o?.cnt&&(ue(d,r,e),ce(d,r,e)),t(d,{migrationsFolder:r});let c=(d.get(e`SELECT count(*) as cnt FROM __drizzle_migrations`)?.cnt??0)-a;if(c>0){let l=o?.cnt?"Database migrated":"Database initialized";console.error(`[crewx] ${l} (${c} migration${c>1?"s":""} applied).`);}}function ct(d,t){$t.has(t)||(pe(d),$t.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 et=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"),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)},d=>({idx_tasks_agent_id:index("idx_tasks_agent_id").on(d.agent_id),idx_tasks_status:index("idx_tasks_status").on(d.status),idx_tasks_started_at:index("idx_tasks_started_at").on(d.started_at),idx_tasks_trace_id:index("idx_tasks_trace_id").on(d.trace_id),idx_tasks_parent_task_id:index("idx_tasks_parent_task_id").on(d.parent_task_id),idx_tasks_crewx_version:index("idx_tasks_crewx_version").on(d.crewx_version),idx_tasks_pid:index("idx_tasks_pid").on(d.pid),idx_tasks_thread_id:index("idx_tasks_thread_id").on(d.thread_id),idx_tasks_workspace_id:index("idx_tasks_workspace_id").on(d.workspace_id),idx_tasks_workspace_ref:index("idx_tasks_workspace_ref").on(d.workspace_ref),idx_tasks_project_id:index("idx_tasks_project_id").on(d.project_id),idx_tasks_ws_started:index("idx_tasks_ws_started").on(d.workspace_id,d.started_at)}));var p=sqliteTable("threads",{id:text("id").primaryKey(),workspace_id:text("workspace_id").references(()=>et.id,{onDelete:"set null"}),platform:text("platform").notNull().default("cli"),title:text("title"),first_message:text("first_message"),last_message:text("last_message"),message_count:integer("message_count").notNull().default(0),created_at:text("created_at").notNull(),updated_at:text("updated_at").notNull(),metadata:text("metadata"),title_locked:integer("title_locked").notNull().default(0),pinned:integer("pinned").notNull().default(0),starred:integer("starred").notNull().default(0)},d=>({idx_threads_updated_at:index("idx_threads_updated_at").on(d.updated_at),idx_threads_workspace_id:index("idx_threads_workspace_id").on(d.workspace_id),idx_threads_ws_updated:index("idx_threads_ws_updated").on(d.workspace_id,d.updated_at),idx_threads_title:index("idx_threads_title").on(d.title)}));var Ee=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(()=>Ee.id,{onDelete:"set null"}),name:text("name").notNull(),kind:text("kind").notNull().default("internal"),status:text("status").notNull().default("ok"),started_at:text("started_at").notNull(),completed_at:text("completed_at"),duration_ms:integer("duration_ms"),input:text("input"),output:text("output"),error:text("error"),attributes:text("attributes")},d=>({idx_spans_task_id:index("idx_spans_task_id").on(d.task_id),idx_spans_parent_span_id:index("idx_spans_parent_span_id").on(d.parent_span_id)}));sqliteTable("tool_calls",{id:text("id").primaryKey(),task_id:text("task_id").references(()=>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()},d=>({idx_tool_calls_task_id:index("idx_tool_calls_task_id").on(d.task_id),idx_tool_calls_tool_name:index("idx_tool_calls_tool_name").on(d.tool_name),idx_tool_calls_timestamp:index("idx_tool_calls_timestamp").on(d.timestamp)}));sqliteTable("thread_boxes",{id:text("id").primaryKey(),thread_id:text("thread_id").notNull().references(()=>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()},d=>({idx_thread_boxes_thread_id:index("idx_thread_boxes_thread_id").on(d.thread_id),idx_thread_boxes_seq:index("idx_thread_boxes_seq").on(d.thread_id,d.seq),uniq_thread_boxes_thread_seq:unique().on(d.thread_id,d.seq)}));sqliteTable("request_logs",{id:text("id").primaryKey(),path:text("path").notNull(),method:text("method").notNull(),status_code:integer("status_code").notNull(),duration_ms:integer("duration_ms").notNull(),ip:text("ip"),request_headers:text("request_headers"),response_headers:text("response_headers"),request_body:text("request_body"),response_body:text("response_body"),query:text("query"),user_id:text("user_id"),project_id:text("project_id"),partition_key:text("partition_key").notNull(),timestamp:text("timestamp").notNull().default(sql`(datetime('now'))`),metadata:text("metadata")},d=>({idx_request_logs_timestamp:index("idx_request_logs_timestamp").on(d.timestamp),idx_request_logs_path:index("idx_request_logs_path").on(d.path),idx_request_logs_status_code:index("idx_request_logs_status_code").on(d.status_code),idx_request_logs_partition_key:index("idx_request_logs_partition_key").on(d.partition_key)}));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")},d=>({uniq_usage_limit_snapshots_bucket:uniqueIndex("uniq_usage_limit_snapshots_bucket").on(d.provider,d.account_ref,d.limit_window,d.bucket_start),idx_usage_limit_snapshots_window_bucket:index("idx_usage_limit_snapshots_window_bucket").on(d.limit_window,d.bucket_start),idx_usage_limit_snapshots_provider_window_bucket:index("idx_usage_limit_snapshots_provider_window_bucket").on(d.provider,d.limit_window,d.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")},d=>({uniq_usage_reports_ws_month:uniqueIndex("uniq_usage_reports_ws_month").on(d.workspace_id,d.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")},d=>({idx_notifications_ws_created:index("idx_notifications_ws_created").on(d.workspace_id,sql`${d.created_at} DESC`)}));sqliteTable("notification_reads",{notification_id:text("notification_id").notNull(),user_id:text("user_id").notNull(),read_at:text("read_at").notNull()},d=>({pk:primaryKey({columns:[d.notification_id,d.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()},d=>({idx_agent_suggestions_ws_agent:index("idx_agent_suggestions_ws_agent").on(d.workspace_id,d.agent_id),idx_agent_suggestions_ws_created:index("idx_agent_suggestions_ws_created").on(d.workspace_id,d.created_at)}));function qt(d){let t=d.toLowerCase().trim();t=t.replace(/\[[^\]]*\]$/g,"");let e=t.lastIndexOf("/");return e>=0&&(t=t.slice(e+1)),t=t.replace(/-\d{8}$/,""),t}var G="2026-05-09",ze="0.8.9-rc.13",X=10,Y=parseInt(ze.split("rc.")[1]),We=new Set(["workflow","mcp"]),Wt=600*1e3,mt=class extends tt{dbPath;pidNullObservedSince=new Map;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true});}else if(!existsSync(e))throw new _("NOT_FOUND","Database not found");let s=R(e);if(t)try{ct(s.db,e);}catch(r){throw s.close(),r}return s}startTask(t){let e=this.openHandle(true);try{e.db.insert(n).values({id:t.id,agent_id:t.agentId,prompt:t.prompt,mode:t.mode,status:t.status,started_at:t.startedAt,pid:t.pid??null,parent_task_id:t.parentTaskId??null,caller_agent_id:t.callerAgentId??null,trace_id:t.traceId??null,command:t.command??null,metadata:t.metadata??null,workspace_id:t.workspaceId??null,platform:t.platform??"cli",crewx_version:t.crewxVersion??null,thread_id:t.threadId??null,model:t.model??null,rendered_prompt:t.renderedPrompt??null,coding_agent_command:t.codingAgentCommand??null}).onConflictDoUpdate({target:n.id,set:{pid:sql`COALESCE(excluded.pid, ${n.pid})`},setWhere:eq(n.status,"running")}).run();}catch(s){throw s instanceof _?s:new _("DB_ERROR","Failed to start task",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=?,
|
|
14
14
|
exit_code=?, input_tokens=?, output_tokens=?, cached_input_tokens=?, cost_usd=?,
|
|
15
15
|
model=COALESCE(?, model)
|
|
16
|
-
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
|
|
16
|
+
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){let s=this.openHandle(true);try{s.db.transaction(r=>{let o=r.select({logs:n.logs}).from(n).where(eq(n.id,t)).limit(1).get(),a=o?.logs?JSON.parse(o.logs):[];a.push(e),r.update(n).set({logs:JSON.stringify(a)}).where(eq(n.id,t)).run();},{behavior:"immediate"});}catch(r){throw r instanceof _?r:new _("DB_ERROR","Failed to append log",r)}finally{s.close();}}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(),r=new Set,o=0;for(let a of e){if(a.pid){let l=!1;try{process.kill(a.pid,0),l=!0;}catch{}l||(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(),o++);continue}if(We.has(a.platform??"cli"))continue;let i=`${a.id}:${a.run_epoch??0}`;r.add(i);let c=this.pidNullObservedSince.get(i);if(c===void 0){this.pidNullObservedSince.set(i,s);continue}s-c>=Wt&&(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(),o++,this.pidNullObservedSince.delete(i));}for(let a of Array.from(this.pidNullObservedSince.keys()))r.has(a)||this.pidNullObservedSince.delete(a);return o}finally{t.close();}}reapRunningWorkflowTasks(t=Wt){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))).run().changes??0}finally{e.close();}}findTaskStatus(t,e){let s=this.resolveDbPaths();for(let r of s){if(!existsSync(r))continue;let o=R(r);try{let a=e?eq(n.workspace_id,e):void 0,i=a?and(eq(n.id,t),a):eq(n.id,t),c=o.db.select().from(n).where(i).limit(1).get()??void 0;if(!c){let l=or(eq(n.thread_id,t),and(isNull(n.thread_id),like(n.command,`%--thread=${t}%`))),u=a?and(l,a):l;c=o.db.select().from(n).where(u).orderBy(desc(n.started_at)).limit(1).get()??void 0;}if(c)return c}catch(a){throw new _("DB_ERROR","Failed to find task status",a)}finally{o.close();}}}findChildTasks(t,e){let s=this.resolveDbPaths(),r=new Set,o=[];for(let a of s){if(!existsSync(a))continue;let i=R(a);try{let c=e?and(eq(n.parent_task_id,t),eq(n.workspace_id,e)):eq(n.parent_task_id,t),l=i.db.select().from(n).where(c).orderBy(asc(n.started_at)).all();for(let u of l)r.has(u.id)||(r.add(u.id),o.push(u));}catch(c){throw new _("DB_ERROR","Failed to find child tasks",c)}finally{i.close();}}return o}getWorkspaceUsageSummary(t){if(!this.dbExists())return [];let e=this.openHandle(false);try{return e.db.all(t?sql`
|
|
17
17
|
SELECT
|
|
18
18
|
COALESCE(workspace_id, 'unknown') AS workspace_id,
|
|
19
19
|
COALESCE(SUM(input_tokens), 0) AS input_tokens,
|
|
@@ -34,7 +34,7 @@ ${s.join(`
|
|
|
34
34
|
FROM tasks
|
|
35
35
|
GROUP BY workspace_id
|
|
36
36
|
ORDER BY (COALESCE(SUM(input_tokens), 0) + COALESCE(SUM(output_tokens), 0)) DESC
|
|
37
|
-
`)}catch(s){throw new
|
|
37
|
+
`)}catch(s){throw new _("DB_ERROR","Failed to get workspace usage summary",s)}finally{e.close();}}getThreadTokenUsage(t,e){let s=this.resolveDbPaths(),r=new Set,o=0,a=0,i=0;for(let c of s){if(!existsSync(c))continue;let l=R(c);try{let u=or(eq(n.thread_id,t),and(isNull(n.thread_id),like(n.command,`%--thread=${t}%`))),h=e?and(u,eq(n.workspace_id,e)):u,f=l.db.select({id:n.id,input_tokens:n.input_tokens,output_tokens:n.output_tokens,cost_usd:n.cost_usd}).from(n).where(h).all();for(let y of f)r.has(y.id)||(r.add(y.id),o+=y.input_tokens??0,a+=y.output_tokens??0,i+=y.cost_usd??0);}catch(u){throw new _("DB_ERROR","Failed to get thread token usage",u)}finally{l.close();}}return {inputTokens:o,outputTokens:a,costUsd:i}}findTasksByThread(t,e){let s=this.resolveDbPaths(),r=new Set,o=[];for(let a of s){if(!existsSync(a))continue;let i=R(a);try{let c=or(eq(n.thread_id,t),and(isNull(n.thread_id),like(n.command,`%--thread=${t}%`))),l=e?and(c,eq(n.workspace_id,e)):c,u=i.db.select().from(n).where(l).orderBy(asc(n.started_at)).all();for(let h of u)r.has(h.id)||(r.add(h.id),o.push(h));}catch(c){throw new _("DB_ERROR","Failed to find tasks by thread",c)}finally{i.close();}}return o}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}batchFetchWorkflowCards(t,e){let s=new Map;if(t.length===0)return s;let r=new Set(t),o=new Map,a=e?eq(n.workspace_id,e):void 0,i=(c,l)=>{if(!c)return;let u=o.get(c);if(u&&l.started_at<=u)return;let h=this.parseWorkflowCardMetadata(l.metadata);h.kind==="workflow_card"&&(o.set(c,l.started_at),s.set(c,{taskId:l.id,workflowRunId:h.workflowRunId,raw:l.result??void 0}));};for(let c of this.resolveDbPaths()){if(!existsSync(c))continue;let l=R(c);try{let u=and(inArray(n.thread_id,t),like(n.metadata,'%"kind":"workflow_card"%')),h=l.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 h)i(C.thread_id,C);let f=and(isNull(n.thread_id),like(n.metadata,'%"kind":"workflow_card"%')),y=l.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 y){let N=this.extractThreadIdFromCommand(C.command);!N||!r.has(N)||i(N,C);}}catch(u){throw new _("DB_ERROR","Failed to batch fetch workflow cards",u)}finally{l.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 r=t.agents&&t.agents.length>0?t.agents:t.agentId?[t.agentId]:null;r&&s.push(inArray(n.agent_id,r));let o=t.statuses&&t.statuses.length>0?t.statuses:t.status?[t.status]:null;o&&s.push(inArray(n.status,o));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 i=s.length>0?and(...s):void 0,c=e.db.select({count:sql`count(*)`}).from(n).where(i).get(),l=(t.sortDir??"DESC")==="ASC"?asc(n.started_at):desc(n.started_at);return {rows:e.db.select().from(n).where(i).orderBy(l).limit(t.limit).offset(t.offset).all(),total:c?.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 r=this.openHandle(false);try{return r.db.all(s?sql`
|
|
38
38
|
SELECT
|
|
39
39
|
t.agent_id,
|
|
40
40
|
t.workspace_id,
|
|
@@ -119,7 +119,7 @@ ${s.join(`
|
|
|
119
119
|
), 0)
|
|
120
120
|
+ COALESCE(SUM(t.output_tokens), 0)
|
|
121
121
|
) DESC
|
|
122
|
-
`).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(o){throw new
|
|
122
|
+
`).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(o){throw new _("DB_ERROR","Failed to get agent usage",o)}finally{r.close();}}getAgentUsageTrendRaw(t,e,s){if(!this.dbExists())return [];let r=this.openHandle(false);try{return r.db.all(s?sql`
|
|
123
123
|
SELECT
|
|
124
124
|
date(t.started_at) AS date,
|
|
125
125
|
t.agent_id,
|
|
@@ -172,7 +172,7 @@ ${s.join(`
|
|
|
172
172
|
AND t.started_at < ${e}
|
|
173
173
|
GROUP BY date(t.started_at), t.agent_id
|
|
174
174
|
ORDER BY date(t.started_at) ASC
|
|
175
|
-
`).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(o){throw new
|
|
175
|
+
`).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(o){throw new _("DB_ERROR","Failed to get agent usage trend",o)}finally{r.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(r){throw new _("DB_ERROR","Failed to find task for stop",r)}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}).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 r=this.openHandle(true);try{let o=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"));r.db.update(n).set({status:"failed",error:e,completed_at:o}).where(a).run();}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to mark task failed",o)}finally{r.close();}}findTasksByPromptHint(t,e){let s=this.resolveDbPaths(),r=new Set,o=[];for(let a of s){if(!existsSync(a))continue;let i=R(a);try{let c=e?and(like(n.prompt,`%${t}%`),eq(n.workspace_id,e)):like(n.prompt,`%${t}%`),l=i.db.select().from(n).where(c).orderBy(asc(n.started_at)).all();for(let u of l)r.has(u.id)||(r.add(u.id),o.push(u));}catch(c){throw new _("DB_ERROR","Failed to find tasks by prompt hint",c)}finally{i.close();}}return o}getProviderUsage(t,e,s){if(!this.dbExists())return [];let r=this.openHandle(false);try{let o=sql`
|
|
176
176
|
CASE
|
|
177
177
|
WHEN ${n.model} LIKE 'claude-%' OR ${n.model} IN ('opus', 'sonnet', 'haiku', 'opus[1m]', 'sonnet[1m]') THEN 'claude'
|
|
178
178
|
WHEN ${n.model} LIKE 'gpt-%' OR ${n.model} LIKE 'codex-%' THEN 'codex'
|
|
@@ -208,7 +208,7 @@ ${s.join(`
|
|
|
208
208
|
${i}
|
|
209
209
|
GROUP BY provider
|
|
210
210
|
ORDER BY (COALESCE(SUM(${a}), 0) + COALESCE(SUM(${n.output_tokens}), 0)) DESC
|
|
211
|
-
`).map(l=>({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}))}catch(o){throw new
|
|
211
|
+
`).map(l=>({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}))}catch(o){throw new _("DB_ERROR","Failed to get provider usage",o)}finally{r.close();}}getModelUsage(t,e,s){if(!this.dbExists())return [];let r=this.openHandle(false);try{let o=sql`
|
|
212
212
|
CASE
|
|
213
213
|
WHEN ${n.model} LIKE 'claude-%' OR ${n.model} IN ('opus', 'sonnet', 'haiku', 'opus[1m]', 'sonnet[1m]') THEN 'claude'
|
|
214
214
|
WHEN ${n.model} LIKE 'gpt-%' OR ${n.model} LIKE 'codex-%' THEN 'codex'
|
|
@@ -244,7 +244,7 @@ ${s.join(`
|
|
|
244
244
|
FROM ${n}
|
|
245
245
|
${i}
|
|
246
246
|
GROUP BY ${n.model}
|
|
247
|
-
`),l=new Map;for(let u of c){let h=qt(u.model??""),f=l.get(h);f?(f.totalTasks+=u.total_tasks,f.inputTokens+=u.input_tokens,f.outputTokens+=u.output_tokens,f.cachedInputTokens+=u.cached_input_tokens,f.costUsd+=u.cost_usd,f.totalTokens+=u.input_tokens+u.output_tokens,f.activeDurationMs+=u.active_duration_ms??0,u.last_active_at&&(!f.lastActiveAt||u.last_active_at>f.lastActiveAt)&&(f.lastActiveAt=u.last_active_at)):l.set(h,{model:h,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(l.values()).sort((u,h)=>h.costUsd-u.costUsd)}catch(o){throw new
|
|
247
|
+
`),l=new Map;for(let u of c){let h=qt(u.model??""),f=l.get(h);f?(f.totalTasks+=u.total_tasks,f.inputTokens+=u.input_tokens,f.outputTokens+=u.output_tokens,f.cachedInputTokens+=u.cached_input_tokens,f.costUsd+=u.cost_usd,f.totalTokens+=u.input_tokens+u.output_tokens,f.activeDurationMs+=u.active_duration_ms??0,u.last_active_at&&(!f.lastActiveAt||u.last_active_at>f.lastActiveAt)&&(f.lastActiveAt=u.last_active_at)):l.set(h,{model:h,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(l.values()).sort((u,h)=>h.costUsd-u.costUsd)}catch(o){throw new _("DB_ERROR","Failed to get model usage",o)}finally{r.close();}}getTasksForReport(t,e,s){if(!this.dbExists())return [];let r=this.openHandle(false);try{let o=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 r.db.all(sql`
|
|
248
248
|
SELECT
|
|
249
249
|
${n.thread_id} AS thread_id,
|
|
250
250
|
${n.agent_id} AS agent_id,
|
|
@@ -255,11 +255,11 @@ ${s.join(`
|
|
|
255
255
|
FROM ${n}
|
|
256
256
|
${o}
|
|
257
257
|
ORDER BY ${n.started_at} ASC
|
|
258
|
-
`).map(i=>({threadId:i.thread_id,agentId:i.agent_id,startedAt:i.started_at,status:i.status,error:i.error,exitCode:i.exit_code}))}catch(o){throw new
|
|
258
|
+
`).map(i=>({threadId:i.thread_id,agentId:i.agent_id,startedAt:i.started_at,status:i.status,error:i.error,exitCode:i.exit_code}))}catch(o){throw new _("DB_ERROR","Failed to get tasks for report",o)}finally{r.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 r=s.length>0?and(...s):void 0,o=(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(r).orderBy(o).limit(t.limit).all():e.db.select(a).from(n).where(r).orderBy(o).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,r=t.maxTurnsPerThread??4,o=[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),notLike(n.metadata,'%"kind":"workflow_card"%')),or(isNull(n.metadata),notLike(n.metadata,'%"retriedFromTaskId"%'))];t.workspaceId&&o.push(eq(n.workspace_id,t.workspaceId)),t.from&&o.push(gte(n.started_at,t.from)),t.to&&o.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(...o)).orderBy(asc(n.started_at),asc(n.id)).all(),i=new Map;for(let l of a){if(!l.thread_id)continue;let u={id:l.id,agentId:l.agent_id,status:l.status,prompt:l.prompt,startedAt:l.started_at,completedAt:l.completed_at},h=i.get(l.thread_id);h?h.push(u):i.set(l.thread_id,[u]);}let c=[];for(let[l,u]of i){let h=u.filter(f=>f.agentId===t.targetAgentId).length;h<2||c.push({threadId:l,targetAgentTurnCount:h,turns:u});}return c.sort((l,u)=>{let h=l.turns[l.turns.length-1].startedAt,f=u.turns[u.turns.length-1].startedAt;return h<f?1:h>f?-1:0}),c.slice(0,s).map(l=>({threadId:l.threadId,targetAgentTurnCount:l.targetAgentTurnCount,turns:l.turns.slice(-r)}))}catch(s){throw new _("DB_ERROR","Failed to find thread chains",s)}finally{e.close();}}};var yt=class extends z{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 mt({dbPath:this.dbPath}),s=process.cwd(),o=existsSync(join(s,"crewx.yaml"))||existsSync(join(s,"crewx.yml"))?vt(s):null,a=process.argv.join(" ");this.unsubs.push(t.on("task:start",i=>{try{let c=i.callerAgentId??null,l=i.parentTaskId??null,u=i.rootTraceId??i.traceId,h=i.metadata?JSON.stringify(i.metadata):JSON.stringify({provider:i.provider??"cli/claude"});e.startTask({id:i.traceId,agentId:i.agentRef.replace(/^@/,""),prompt:i.message,mode:i.mode,status:"running",pid:i.pid??null,startedAt:i.timestamp.toISOString(),crewxVersion:this.version,platform:i.platform??"cli",model:i.model??null,renderedPrompt:i.renderedPrompt??null,command:a,codingAgentCommand:i.codingAgentCommand??null,workspaceId:i.workspaceId??o,callerAgentId:c,parentTaskId:l,traceId:u,metadata:h,threadId:i.threadId??null});}catch{}}),t.on("task:output",i=>{try{e.appendLog(i.traceId,{timestamp:i.timestamp.toISOString(),level:i.level??"stdout",message:i.output});}catch{}}),t.on("task:end",i=>{try{let c=typeof i.metadata?.runEpoch=="number"?i.metadata.runEpoch:null;e.finishTask({id:i.traceId,status:i.error?"failed":"success",result:i.result??null,error:i.error?JSON.stringify(i.error):null,completedAt:i.timestamp.toISOString(),durationMs:i.durationMs,exitCode:i.exitCode??null,inputTokens:i.inputTokens??0,outputTokens:i.outputTokens??0,cachedInputTokens:i.cachedInputTokens??0,costUsd:i.costUsd??0,model:i.model??null,runEpoch:c});}catch{}}));}detach(t){this.unsubs.forEach(e=>e()),this.unsubs=[];}};var gt=class extends tt{dbPath;constructor(t={}){super(),t.dbPath?this.dbPath=t.dbPath:t.dbRoot&&(this.dbPath=join(t.dbRoot,".crewx","crewx.db"));}resolveDbPath(){return this.dbPath?this.dbPath:super.resolveDbPath()}openHandle(t){let e=this.resolveDbPath();if(t){let r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true});}else if(!existsSync(e))throw new _("NOT_FOUND","Database not found");let s=R(e);if(t)try{ct(s.db,e);}catch(r){throw s.close(),r}return s}validateWorkspaceId(t,e){return t.db.select({id:et.id}).from(et).where(eq(et.id,e)).limit(1).get()?e:null}topLevelTaskPredicateSql(t="child"){return sql.raw(`NOT EXISTS (
|
|
259
259
|
SELECT 1 FROM tasks parent
|
|
260
260
|
WHERE parent.id = ${t}.parent_task_id
|
|
261
261
|
AND parent.thread_id = ${t}.thread_id
|
|
262
|
-
)`)}findAllThreads(t){let e=this.resolveDbPaths(),s=new Set,r=[];for(let o of e){if(!existsSync(o))continue;let a=R(o);try{let i=t?eq(
|
|
262
|
+
)`)}findAllThreads(t){let e=this.resolveDbPaths(),s=new Set,r=[];for(let o of e){if(!existsSync(o))continue;let a=R(o);try{let i=t?eq(p.workspace_id,t):void 0,c=a.db.select().from(p).where(i).orderBy(desc(p.updated_at)).all();for(let l of c)s.has(l.id)||(s.add(l.id),r.push(l));}catch(i){throw new _("DB_ERROR","Failed to find all threads",i)}finally{a.close();}}return r}findThreadsByIdsOrTitles(t,e){if(t.length===0)return [];let s=this.resolveDbPaths(),r=new Set,o=[];for(let a of s){if(!existsSync(a))continue;let i=R(a);try{let c=or(inArray(p.id,t),inArray(p.title,t)),l=e?and(c,eq(p.workspace_id,e)):c,u=i.db.select().from(p).where(l).orderBy(desc(p.updated_at)).all();for(let h of u)r.has(h.id)||(r.add(h.id),o.push(h));}catch(c){throw new _("DB_ERROR","Failed to find threads by ids or titles",c)}finally{i.close();}}return o}findThreadById(t,e){let s=this.resolveDbPaths();for(let r of s){if(!existsSync(r))continue;let o=R(r);try{let a=eq(p.id,t),i=e?and(a,eq(p.workspace_id,e)):a,c=o.db.select().from(p).where(i).limit(1).get()??void 0;if(c)return c}catch(a){throw new _("DB_ERROR","Failed to find thread by id",a)}finally{o.close();}}}threadExists(t,e){let s=this.resolveDbPaths();for(let r of s){if(!existsSync(r))continue;let o=R(r);try{let a=eq(p.id,t),i=e?and(a,eq(p.workspace_id,e)):a;if(o.db.select({id:p.id}).from(p).where(i).limit(1).get())return !0}catch(a){throw new _("DB_ERROR","Failed to check thread existence",a)}finally{o.close();}}return false}aggregateTaskStats(t,e){let s=this.resolveDbPaths(),r=0,o=0,a=0,i=0,c=0,l=new Set;for(let u of s){if(!existsSync(u))continue;let h=R(u);try{let f=h.db.get(sql`
|
|
263
263
|
SELECT
|
|
264
264
|
count(*) AS cnt,
|
|
265
265
|
COALESCE(SUM(child.input_tokens), 0) AS total_input,
|
|
@@ -276,7 +276,7 @@ ${s.join(`
|
|
|
276
276
|
AND child.agent_id IS NOT NULL AND child.agent_id != ''
|
|
277
277
|
AND ${this.topLevelTaskPredicateSql()}
|
|
278
278
|
${e?sql`AND child.workspace_id = ${e}`:sql``}
|
|
279
|
-
`);for(let C of y)l.add(C.agent_id);}catch(f){throw new
|
|
279
|
+
`);for(let C of y)l.add(C.agent_id);}catch(f){throw new _("DB_ERROR","Failed to aggregate task stats",f)}finally{h.close();}}return {taskCount:r,inputTokens:o,outputTokens:a,cachedInputTokens:i,costUsd:c,agentIds:Array.from(l)}}findTopLevelTasks(t,e,s){let r=this.resolveDbPaths(),o=new Set,a=[];for(let i of r){if(!existsSync(i))continue;let c=R(i);try{let l;s!==void 0?l=c.db.all(sql`
|
|
280
280
|
SELECT * FROM (
|
|
281
281
|
SELECT child.*,
|
|
282
282
|
ROW_NUMBER() OVER (PARTITION BY child.agent_id ORDER BY child.started_at DESC) AS rn
|
|
@@ -293,7 +293,7 @@ ${s.join(`
|
|
|
293
293
|
AND ${this.topLevelTaskPredicateSql()}
|
|
294
294
|
${e?sql`AND child.workspace_id = ${e}`:sql``}
|
|
295
295
|
ORDER BY child.started_at ASC
|
|
296
|
-
`);for(let u of l)o.has(u.id)||(o.add(u.id),a.push(u));}catch(l){throw new
|
|
296
|
+
`);for(let u of l)o.has(u.id)||(o.add(u.id),a.push(u));}catch(l){throw new _("DB_ERROR","Failed to find top-level tasks",l)}finally{c.close();}}if(s!==void 0&&r.length>1){let i=new Map;for(let c of a){let l=c.agent_id??"";i.has(l)||i.set(l,[]),i.get(l).push(c);}a=[];for(let c of i.values())c.sort((l,u)=>(u.started_at??"").localeCompare(l.started_at??"")),a.push(...c.slice(0,s));a.sort((c,l)=>(c.started_at??"").localeCompare(l.started_at??""));}return a}findAllTasks(t,e){let s=this.resolveDbPaths(),r=new Set,o=[];for(let a of s){if(!existsSync(a))continue;let i=R(a);try{let c=eq(n.thread_id,t),l=e?and(c,eq(n.workspace_id,e)):c,u=i.db.select().from(n).where(l).orderBy(asc(n.started_at)).all();for(let h of u)r.has(h.id)||(r.add(h.id),o.push(h));}catch(c){throw new _("DB_ERROR","Failed to find all tasks for thread",c)}finally{i.close();}}return o}findTaskById(t,e,s){let r=this.resolveDbPaths();for(let o of r){if(!existsSync(o))continue;let a=R(o);try{let i=and(eq(n.id,e),eq(n.thread_id,t)),c=s?and(i,eq(n.workspace_id,s)):i,l=a.db.select().from(n).where(c).limit(1).get();if(!l)continue;let u=a.db.select().from(n).where(eq(n.parent_task_id,l.id)).orderBy(asc(n.started_at)).all();return {task:l,children:u}}catch(i){throw new _("DB_ERROR","Failed to find task by id",i)}finally{a.close();}}}batchFetchTasksQuery(t,e){return sql`
|
|
297
297
|
SELECT child.id, child.thread_id, child.agent_id, child.status,
|
|
298
298
|
child.parent_task_id, child.started_at, child.completed_at,
|
|
299
299
|
child.duration_ms, child.input_tokens, child.output_tokens,
|
|
@@ -303,7 +303,7 @@ ${s.join(`
|
|
|
303
303
|
AND ${this.topLevelTaskPredicateSql()}
|
|
304
304
|
${e?sql`AND child.workspace_id = ${e}`:sql``}
|
|
305
305
|
ORDER BY child.started_at ASC
|
|
306
|
-
`}batchFetchTasks(t,e){let s=new Map;if(t.length===0)return s;let r=this.resolveDbPaths();for(let o of r){if(!existsSync(o))continue;let a=R(o);try{let i=a.db.all(this.batchFetchTasksQuery(t,e));for(let c of i){let l=c.thread_id;s.has(l)||s.set(l,[]),s.get(l).push(c);}}catch(i){throw new
|
|
306
|
+
`}batchFetchTasks(t,e){let s=new Map;if(t.length===0)return s;let r=this.resolveDbPaths();for(let o of r){if(!existsSync(o))continue;let a=R(o);try{let i=a.db.all(this.batchFetchTasksQuery(t,e));for(let c of i){let l=c.thread_id;s.has(l)||s.set(l,[]),s.get(l).push(c);}}catch(i){throw new _("DB_ERROR","Failed to batch fetch tasks",i)}finally{a.close();}}return s}explainBatchFetchTasksPlan(t,e){let s=this.resolveDbPaths();for(let r of s){if(!existsSync(r))continue;let o=R(r);try{return o.db.all(sql`EXPLAIN QUERY PLAN ${this.batchFetchTasksQuery(t,e)}`).map(i=>i.detail)}finally{o.close();}}return []}updateThreadTitle(t,e,s){if(!this.dbExists())return;let r=this.openHandle(true);try{let o=eq(p.id,t),a=s?and(o,eq(p.workspace_id,s)):o;if(!r.db.select({id:p.id}).from(p).where(a).limit(1).get())return;r.db.update(p).set({title:e,title_locked:1,updated_at:new Date().toISOString()}).where(eq(p.id,t)).run();}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to update thread title",o)}finally{r.close();}}upsertThread(t,e){let s=this.openHandle(true);try{let r=e.workspaceId?this.validateWorkspaceId(s,e.workspaceId):null,o=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 i={updated_at:o};e.title!==void 0&&(i.title=e.title),e.titleLocked!==void 0&&(i.title_locked=e.titleLocked?1:0),s.db.update(p).set(i).where(eq(p.id,t)).run();}else s.db.insert(p).values({id:t,platform:e.platform,workspace_id:r,title:e.title??null,title_locked:e.titleLocked?1:0,message_count:0,created_at:o,updated_at:o}).run();}catch(r){throw r instanceof _?r:new _("DB_ERROR","Failed to upsert thread",r)}finally{s.close();}}ensureThread(t,e,s){let r=this.openHandle(true);try{let o=s?this.validateWorkspaceId(r,s):null,a=r.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){o&&!a.workspace_id&&r.db.update(p).set({workspace_id:o}).where(eq(p.id,t)).run();return}let i=new Date().toISOString();r.db.insert(p).values({id:t,platform:e,workspace_id:o,message_count:0,created_at:i,updated_at:i}).run();}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to ensure thread",o)}finally{r.close();}}saveUserMessage(t,e,s){if(!this.dbExists())return {firstMessage:false};let r=this.openHandle(true);try{let o=new Date().toISOString();return {firstMessage:r.db.transaction(i=>{let l=i.select({message_count:p.message_count}).from(p).where(eq(p.id,t)).limit(1).get()?.message_count===0;return i.run(sql`
|
|
307
307
|
UPDATE threads
|
|
308
308
|
SET first_message = COALESCE(first_message, ${e}),
|
|
309
309
|
title = CASE WHEN title_locked = 0 AND title IS NULL THEN substr(${e}, 1, 60) ELSE title END,
|
|
@@ -311,7 +311,7 @@ ${s.join(`
|
|
|
311
311
|
message_count = message_count + 1,
|
|
312
312
|
updated_at = ${o}
|
|
313
313
|
WHERE id = ${t}
|
|
314
|
-
`),l},{behavior:"immediate"})}}catch(o){throw o instanceof
|
|
314
|
+
`),l},{behavior:"immediate"})}}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to save user message",o)}finally{r.close();}}saveAssistantMessage(t,e,s){if(!this.dbExists())return;let r=this.openHandle(true);try{let o=new Date().toISOString();r.db.update(p).set({last_message:e,updated_at:o}).where(eq(p.id,t)).run();}catch(o){throw o instanceof _?o:new _("DB_ERROR","Failed to save assistant message",o)}finally{r.close();}}updateThread(t,e){if(!this.dbExists())return;let s=this.openHandle(true);try{let r={updated_at:new Date().toISOString()};e.title!==void 0&&(r.title=e.title,r.title_locked=1),e.titleLocked!==void 0&&(r.title_locked=e.titleLocked?1:0),s.db.update(p).set(r).where(eq(p.id,t)).run();}catch(r){throw r instanceof _?r:new _("DB_ERROR","Failed to update thread",r)}finally{s.close();}}togglePin(t,e){let s=this.openHandle(true);try{let r=e?and(eq(p.id,t),eq(p.workspace_id,e)):eq(p.id,t),o=s.db.select({pinned:p.pinned,metadata:p.metadata}).from(p).where(r).get();if(!o)return null;let a=o.pinned?0:1,i=o.metadata?JSON.parse(o.metadata):{};if(a){let c=e?and(eq(p.pinned,1),eq(p.workspace_id,e)):eq(p.pinned,1),l=s.db.select({metadata:p.metadata}).from(p).where(c).all(),u=null;for(let h of l){let f=h.metadata?JSON.parse(h.metadata):{};typeof f.pinOrder=="number"&&(u===null||f.pinOrder<u)&&(u=f.pinOrder);}i.pinOrder=u===null?0:u-1;}else delete i.pinOrder;return s.db.update(p).set({pinned:a,metadata:Object.keys(i).length>0?JSON.stringify(i):null}).where(r).run(),{pinned:!!a}}catch(r){throw r instanceof _?r:new _("DB_ERROR","Failed to toggle pin",r)}finally{s.close();}}reorderPins(t,e){let s=this.openHandle(true);try{for(let r=0;r<t.length;r++){let o=e?and(eq(p.id,t[r]),eq(p.workspace_id,e)):eq(p.id,t[r]),a=s.db.select({metadata:p.metadata}).from(p).where(o).get();if(!a)continue;let i=a.metadata?JSON.parse(a.metadata):{};i.pinOrder=r+1,s.db.update(p).set({metadata:JSON.stringify(i)}).where(o).run();}}catch(r){throw r instanceof _?r:new _("DB_ERROR","Failed to reorder pins",r)}finally{s.close();}}toggleStar(t,e){let s=this.openHandle(true);try{let r=e?and(eq(p.id,t),eq(p.workspace_id,e)):eq(p.id,t),o=s.db.select({starred:p.starred}).from(p).where(r).get();if(!o)return null;let a=o.starred?0:1;return s.db.update(p).set({starred:a}).where(r).run(),{starred:!!a}}catch(r){throw r instanceof _?r:new _("DB_ERROR","Failed to toggle star",r)}finally{s.close();}}resolveOverdriveForRequest(t,e,s,r){let a=this.openHandle(true);try{let i=e?and(eq(p.id,t),eq(p.workspace_id,e)):eq(p.id,t);return a.db.transaction(l=>{let u=l.select({metadata:p.metadata}).from(p).where(i).get();if(!u)return null;let h=u.metadata?JSON.parse(u.metadata):{},f=h.overdrive??{},y=r?.defaultTurns??f.defaultTurns??3,C=r?.updatedBy??"ui",N=new Date().toISOString(),I,P,B,rt;switch(s){case "enable-count":{let ot=r?.turns??y,at=r?.consumeCurrent?Math.max(ot-1,0):ot;I=at>0?"count":"off",P=at,B=!0,rt="count";break}case "enable-latch":{I="latch",P=0,B=!0,rt="latch";break}case "disable":{I="off",P=0,B=!1;break}default:{let ot=f.state??"off",at=typeof f.remaining=="number"?f.remaining:0;if(ot==="count"&&at>0){let At=at-1;At<=0?(I="off",P=0):(I="count",P=At),B=!0,rt="count";}else ot==="latch"?(I="latch",P=0,B=!0,rt="latch"):(I="off",P=0,B=!1);break}}return h.overdrive={state:I,remaining:P,defaultTurns:y,updatedAt:N,updatedBy:C},l.update(p).set({metadata:JSON.stringify(h),updated_at:N}).where(i).run(),{applied:B,appliedMode:rt,state:I,remaining:P}},{behavior:"immediate"})}catch(i){throw i instanceof _?i:new _("DB_ERROR","Failed to resolve overdrive",i)}finally{a.close();}}};function Qe(d){return d.replace(/<conversation_history[^>]*>[\s\S]*?<\/conversation_history>/g,"").split(`
|
|
315
315
|
`).filter(r=>!(r.startsWith("Loaded ")&&r.includes("layouts from")||r.includes("[dotenv@")||r.includes("[Nest]")&&r.includes("DEBUG")||r.startsWith("Registered custom layout:")||r.startsWith("Updated custom layout:"))).join(`
|
|
316
316
|
`).trim()}function Ze(d){if(!d)return "";let t=d;try{let e=JSON.parse(t);Array.isArray(e)?t=e.filter(s=>s?.type==="text"&&s?.text).map(s=>s.text).join(`
|
|
317
317
|
`):e&&typeof e=="object"&&e.result!==void 0&&(t=e.result||"");}catch{t=Qe(t);}return t}var kt=class{dbPath;constructor(t){this.dbPath=t??join(homedir(),".crewx","crewx.db");}getThreadRepo(){return new gt({dbPath:this.dbPath})}updateThread(t,e){this.getThreadRepo().updateThread(t,{title:e.title});}async ensureThread(t,e,s){let r=this.getThreadRepo(),o=r.findThreadById(t);if(o){if(o.platform!==e)throw new Error(`Thread '${t}' already exists with platform '${o.platform}' \u2014 cannot change to '${e}' (platform is immutable)`);return {created:false}}return r.ensureThread(t,e,s),{created:true}}async fetchHistory(t,e){let s=e?.limit??100,r=this.getThreadRepo(),o=r.findThreadById(t),a=r.findTopLevelTasks(t,void 0,s),i=new Set(["queued","cancelled"]);a=a.filter(u=>(!u.status||!i.has(u.status))&&(!e?.currentTraceId||u.trace_id!==e.currentTraceId));let c=o?.platform??"cli",l=this.rowsToMessages(a);return {threadId:t,platform:c,messages:l,metadata:{title:o?.title??void 0,firstMessage:o?.first_message??void 0,lastMessage:o?.last_message??void 0,messageCount:o?.message_count??0,updatedAt:o?.updated_at?new Date(o.updated_at).getTime():void 0}}}async saveUserMessage(t,e,s,r){let{firstMessage:o}=this.getThreadRepo().saveUserMessage(t,e);return {id:t,firstMessage:o}}async saveAssistantMessage(t,e,s,r){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 r=Ze(s.result),o=this.normalizeStatus(s.status);if(r||o==="running"||o==="failed"){let a={agent_id:s.agent_id,task_id:s.id};o&&(a.status=o),o==="failed"&&s.error&&(a.reason=s.error.split(`
|