#!/usr/bin/env node
import*as i from'fs';import*as b from'path';import*as l from'js-yaml';var p=class{logsDir;currentLog=null;constructor(t){this.logsDir=b.join(t,".paradigm","orchestrations");}startOrchestration(t,s,o){return this.ensureLogsDir(),this.currentLog={id:t,task:s,mode:o,started:new Date().toISOString(),status:"running",agents:[],totals:{duration_ms:0,tokens:0,cost_usd:0,agents_spawned:0,files_created:0,files_modified:0}},this.currentLog}logAgentCompletion(t,s){if(this.currentLog&&this.currentLog.id===t){this.currentLog.agents.push(s),this.currentLog.totals.agents_spawned++,this.currentLog.totals.tokens+=s.tokens.total,this.currentLog.totals.cost_usd+=s.cost_usd;for(let o of s.artifacts)o.action==="created"?this.currentLog.totals.files_created++:o.action==="modified"&&this.currentLog.totals.files_modified++;}}completeOrchestration(t,s){if(!this.currentLog)return null;this.currentLog.completed=new Date().toISOString(),this.currentLog.status=t,this.currentLog.totals.duration_ms=s,this.saveOrchestration(this.currentLog);let o=this.currentLog;return this.currentLog=null,o}saveOrchestration(t){this.ensureLogsDir();let s=`${t.started.slice(0,10)}-${this.sanitizeFilename(t.task)}.yaml`,o=b.join(this.logsDir,s),a=l.dump(t,{lineWidth:-1,noRefs:true,quotingType:'"'});i.writeFileSync(o,a);}loadOrchestration(t){let s=this.listLogFiles();for(let o of s)try{let a=i.readFileSync(o,"utf-8"),e=l.load(a);if(e.id===t)return e}catch{}return null}listOrchestrations(t){let s=this.listLogFiles(),o=[];for(let a of s)try{let e=i.readFileSync(a,"utf-8"),r=l.load(e);if(t?.from&&new Date(r.started)<t.from||t?.to&&new Date(r.started)>t.to||t?.status&&r.status!==t.status)continue;o.push({id:r.id,task:r.task,mode:r.mode,started:r.started,status:r.status,agents_spawned:r.totals.agents_spawned,tokens:r.totals.tokens,cost_usd:r.totals.cost_usd,duration_ms:r.totals.duration_ms});}catch{}return o.sort((a,e)=>new Date(e.started).getTime()-new Date(a.started).getTime()),t?.limit?o.slice(0,t.limit):o}getCostSummary(t,s){let o=this.listOrchestrations({from:t,to:s}),a=this.listLogFiles(),e=0,r=0,c={},d={},g={};for(let m of a)try{let f=i.readFileSync(m,"utf-8"),u=l.load(f),L=new Date(u.started);if(t&&L<t||s&&L>s)continue;e+=u.totals.cost_usd,r+=u.totals.tokens;let h=u.started.slice(0,10);g[h]||(g[h]={cost:0,tokens:0}),g[h].cost+=u.totals.cost_usd,g[h].tokens+=u.totals.tokens;for(let n of u.agents)c[n.name]||(c[n.name]={tokens:0,cost:0,count:0}),c[n.name].tokens+=n.tokens.total,c[n.name].cost+=n.cost_usd,c[n.name].count++,d[n.model]||(d[n.model]={tokens:0,cost:0,count:0}),d[n.model].tokens+=n.tokens.total,d[n.model].cost+=n.cost_usd,d[n.model].count++;}catch{}let k=Object.entries(g).map(([m,f])=>({date:m,...f})).sort((m,f)=>m.date.localeCompare(f.date));return {totalCost:e,totalTokens:r,orchestrationCount:o.length,byAgent:c,byModel:d,byDay:k}}exportToJson(t){let s=this.listLogFiles(),o=[];for(let a of s)try{let e=i.readFileSync(a,"utf-8"),r=l.load(e),c=new Date(r.started);if(t?.from&&c<t.from||t?.to&&c>t.to)continue;o.push(r);}catch{}return JSON.stringify(o,null,2)}exportToCsv(t){let s=this.listOrchestrations(t),o=["id","task","mode","started","status","agents_spawned","tokens","cost_usd","duration_ms"],a=s.map(e=>[e.id,`"${e.task.replace(/"/g,'""')}"`,e.mode,e.started,e.status,e.agents_spawned,e.tokens,e.cost_usd.toFixed(4),e.duration_ms]);return [o.join(","),...a.map(e=>e.join(","))].join(`
`)}ensureLogsDir(){i.existsSync(this.logsDir)||i.mkdirSync(this.logsDir,{recursive:true});}listLogFiles(){return i.existsSync(this.logsDir)?i.readdirSync(this.logsDir).filter(t=>t.endsWith(".yaml")).map(t=>b.join(this.logsDir,t)):[]}sanitizeFilename(t){return t.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,50)}};export{p as a};