@hydra-acp/budgeter 0.1.8 → 0.1.10

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 +1 @@
1
- import{relative as W,resolve as w}from"node:path";import{homedir as B}from"node:os";import{realpathSync as _}from"node:fs";function H(a){const u=a.match(/^(\d+)\s*([dhmwy])$/i);if(u!==null){const f=u[1],h=u[2];if(f===void 0||h===void 0)throw new Error(`invalid since spec: ${a}`);const k=parseInt(f,10),o=h.toLowerCase(),i=new Date;return o==="d"?i.setDate(i.getDate()-k):o==="h"?i.setHours(i.getHours()-k):o==="w"?i.setDate(i.getDate()-k*7):o==="m"?i.setMonth(i.getMonth()-k):o==="y"&&i.setFullYear(i.getFullYear()-k),i}const n=new Date(a);if(Number.isNaN(n.getTime()))throw new Error(`invalid since spec: ${a}`);return n}const S=new Map;function v(a){if(S.has(a))return S.get(a);try{const u=_(a);return S.set(a,u),u}catch{return S.set(a,a),a}}function F(a,u){let n=a;if(u.since!==void 0){const o=u.since.getTime(),i=[];for(const c of n)new Date(c.updatedAt).getTime()<o||i.push(c);n=i}if(u.dir!==void 0){const o=v(w(u.dir)),i=[];for(const c of n)c.cwd===void 0||c.cwd===""||(c.cwd.startsWith(o+"/")||c.cwd===o)&&i.push(c);n=i}if(u.host!==void 0&&u.host!=="all"){const o=u.host,i=[];for(const c of n)o==="local"?(!c.importedFromMachine||c.upstreamSessionId)&&i.push(c):c.importedFromMachine===o&&!c.upstreamSessionId&&i.push(c);n=i}if(u.interactive!==void 0){const o=u.interactive,i=[];for(const c of n)c.interactive===o&&i.push(c);n=i}const f=u.min??0,h=u.minMetric==="tokens",k=[];for(const o of n)(h?o.contextTokens:o.costAmount)>f&&k.push(o);return k}function G(a){return a.dir!==void 0?w(a.dir):w(B())}function E(a,u,n){if(a===void 0||a==="")return"<unknown>";const f=v(w(a));if(u===void 0){const p=v(w(B()));return f===p?"~":f.startsWith(p+"/")?"~/"+f.slice(p.length+1):f}const h=v(w(n));let k=W(h,f);if(k===""||k===".")return"<root>";if(k.startsWith(".."))return"<unknown>";const o=k.split("/");if(o.length===0)return"<root>";const i=Math.min(u,o.length),c=o.slice(0,i);return c.length===0?"<root>":c.join("/")}function $(a,u,n={}){let f=n.since;if(f===void 0&&n.bucket!==void 0){const e=new Date;n.bucket==="hour"?(e.setHours(e.getHours()-24),f=e):n.bucket==="day"?(e.setDate(e.getDate()-30),f=e):n.bucket==="week"?(e.setMonth(e.getMonth()-6),f=e):n.bucket==="month"&&(e.setFullYear(e.getFullYear()-2),f=e)}if(!(f!==void 0||n.bucket!==void 0||n.tokens===!0||n.by!==void 0||n.dir!==void 0||n.interactive!==void 0)){let e=0;for(const r of a)e+=r.costAmount;let s="";for(const r of a){s=r.costCurrency;break}return{kind:"total",row:{label:"All sessions",costAmount:e,sessionCount:a.length},currency:s}}const k={since:f,dir:n.dir,interactive:n.interactive,min:n.min,minMetric:n.tokens===!0?"tokens":"cost"},o=F(a,k),i=new Map;if(u!==void 0)for(const e of u){const s=i.get(e.sessionId);s===void 0?i.set(e.sessionId,[e]):s.push(e)}let c="";if(u!==void 0&&u.length>0)for(const e of u){c=e.currency;break}else if(o.length>0)for(const e of o){c=e.costCurrency;break}const p=e=>n.by==="dir"?E(e.cwd,n.depth,G(n)):n.by==="session"?e.sessionId.startsWith("hydra_session_")?e.sessionId.slice(14):e.sessionId:n.by==="model"?e.model===""?"<unknown>":e.model:n.by==="agent"?e.agentId===""?"<unknown>":e.agentId:"<all>",I=e=>{const s=new Date(e);if(Number.isNaN(s.getTime()))return"<invalid>";if(n.bucket==="hour"){const r=s.toLocaleDateString(void 0,{month:"2-digit",day:"2-digit"}),d=String(s.getHours()).padStart(2,"0");return`${r} ${d}:00`}if(n.bucket==="day")return s.toLocaleDateString(void 0,{year:"numeric",month:"2-digit",day:"2-digit"});if(n.bucket==="week"){const r=new Date(s),d=r.getDay(),t=r.getDate()-d+(d===0?-6:1);return r.setDate(t),r.toLocaleDateString(void 0,{year:"numeric",month:"2-digit",day:"2-digit"})}return n.bucket==="month"?s.toLocaleDateString(void 0,{year:"numeric",month:"2-digit"}):s.toLocaleDateString(void 0,{year:"numeric",month:"2-digit",day:"2-digit"})};if(n.by===void 0&&n.bucket===void 0){let e=0,s=0,r=0,d=0,t=0,g=0;for(const b of o){e+=b.costAmount;const T=i.get(b.sessionId);if(T!==void 0)for(const m of T)s+=m.deltaCost,n.tokens===!0&&(m.inputTokens!==void 0&&(r+=m.inputTokens),m.outputTokens!==void 0&&(d+=m.outputTokens),m.cacheReadTokens!==void 0&&(t+=m.cacheReadTokens),m.cacheWriteTokens!==void 0&&(g+=m.cacheWriteTokens))}const l={label:"All sessions",costAmount:e,deltaCost:s,sessionCount:o.length};return n.tokens===!0&&(l.inputTokens=r,l.outputTokens=d,l.cacheReadTokens=t,l.cacheWriteTokens=g),{kind:"total",row:l,currency:c}}if(n.by!==void 0&&n.bucket===void 0){const e=new Map;for(const r of o){const d=p(r);let t=e.get(d);t===void 0&&(t={label:d,rows:{label:d,costAmount:0,deltaCost:0,sessionCount:0}},e.set(d,t)),t.rows.costAmount+=r.costAmount,t.rows.sessionCount=(t.rows.sessionCount??0)+1,n.tokens===!0&&(t.rows.inputTokens===void 0&&(t.rows.inputTokens=0),t.rows.inputTokens+=r.contextTokens);const g=i.get(r.sessionId);if(g!==void 0)for(const l of g)t.rows.deltaCost+=l.deltaCost,n.tokens===!0&&(t.rows.inputTokens===void 0&&(t.rows.inputTokens=0),t.rows.outputTokens===void 0&&(t.rows.outputTokens=0),t.rows.cacheReadTokens===void 0&&(t.rows.cacheReadTokens=0),t.rows.cacheWriteTokens===void 0&&(t.rows.cacheWriteTokens=0),l.inputTokens!==void 0&&(t.rows.inputTokens+=l.inputTokens),l.outputTokens!==void 0&&(t.rows.outputTokens+=l.outputTokens),l.cacheReadTokens!==void 0&&(t.rows.cacheReadTokens+=l.cacheReadTokens),l.cacheWriteTokens!==void 0&&(t.rows.cacheWriteTokens+=l.cacheWriteTokens))}const s=[];for(const r of e.values())s.push({label:r.label,items:[r.rows]});return{kind:"grouped",groups:s,currency:c}}const M=new Map;for(const e of o)M.set(e.sessionId,e);const y=new Map;if(u!==void 0){for(const e of u){if(!M.has(e.sessionId))continue;const s=y.get(e.sessionId);s===void 0?y.set(e.sessionId,[e]):s.push(e)}for(const e of y.values())e.sort((s,r)=>s.ts.localeCompare(r.ts))}const x=(e,s)=>{e._sessions===void 0&&(e._sessions=new Set),e._sessions.add(s),e.sessionCount=e._sessions.size},R=e=>{delete e._sessions},A=(e,s,r)=>{const d=I(s.ts);let t=e.get(d);t===void 0&&(t={bucket:d,costAmount:0,deltaCost:0,sessionCount:0},e.set(d,t)),t.costAmount+=r,t.deltaCost+=r,x(t,s.sessionId),n.tokens===!0&&(t.inputTokens===void 0||(s.inputTokens??0)>t.inputTokens)&&(t.inputTokens=s.inputTokens??t.inputTokens??0)};if(n.by!==void 0&&n.bucket!==void 0){const e=new Map,s=d=>{const t=p(d);let g=e.get(t);return g===void 0&&(g={label:t,buckets:new Map},e.set(t,g)),g.buckets};for(const d of o){const t=y.get(d.sessionId);if(t===void 0||t.length===0)continue;const g=s(d);let l=t[0].cumulativeCost;for(let b=1;b<t.length;b++){const T=t[b],m=Math.max(0,T.cumulativeCost-l);l=T.cumulativeCost,!(f!==void 0&&new Date(T.ts)<f)&&A(g,T,m)}}const r=[];for(const d of e.values()){const t=Array.from(d.buckets.values());for(const g of t)R(g);t.sort((g,l)=>g.bucket.localeCompare(l.bucket)),t.length>0&&r.push({label:d.label,items:t})}return{kind:"timeSeriesGrouped",groups:r,currency:c}}const D=new Map;for(const e of o){const s=y.get(e.sessionId);if(s===void 0||s.length===0)continue;let r=s[0].cumulativeCost;for(let d=1;d<s.length;d++){const t=s[d],g=Math.max(0,t.cumulativeCost-r);r=t.cumulativeCost,!(f!==void 0&&new Date(t.ts)<f)&&A(D,t,g)}}const C=Array.from(D.values());for(const e of C)R(e);return C.sort((e,s)=>e.bucket.localeCompare(s.bucket)),{kind:"timeSeries",timeSeries:C,currency:c}}export{$ as aggregate,F as applyFilters,H as parseSince};
1
+ import{relative as L,resolve as C}from"node:path";import{homedir as W}from"node:os";import{realpathSync as j}from"node:fs";function K(m){const f=m.match(/^(\d+)\s*([dhmwy])$/i);if(f!==null){const v=f[1],l=f[2];if(v===void 0||l===void 0)throw new Error(`invalid since spec: ${m}`);const k=parseInt(v,10),g=l.toLowerCase(),i=new Date;return g==="d"?i.setDate(i.getDate()-k):g==="h"?i.setHours(i.getHours()-k):g==="w"?i.setDate(i.getDate()-k*7):g==="m"?i.setMonth(i.getMonth()-k):g==="y"&&i.setFullYear(i.getFullYear()-k),i}const t=new Date(m);if(Number.isNaN(t.getTime()))throw new Error(`invalid since spec: ${m}`);return t}function z(m){const f=m.locByFiletype;if(f===void 0)return 0;let t=0;for(const v of Object.keys(f)){const l=f[v];l!==void 0&&(t+=l.added-l.removed)}return t}const M=new Map;function B(m){if(M.has(m))return M.get(m);try{const f=j(m);return M.set(m,f),f}catch{return M.set(m,m),m}}function N(m,f){let t=m;if(f.since!==void 0){const k=f.since.getTime(),g=[];for(const i of t)new Date(i.updatedAt).getTime()<k||g.push(i);t=g}if(f.dir!==void 0){const k=B(C(f.dir)),g=[];for(const i of t)i.cwd===void 0||i.cwd===""||(i.cwd.startsWith(k+"/")||i.cwd===k)&&g.push(i);t=g}if(f.host!==void 0&&f.host!=="all"){const k=f.host,g=[];for(const i of t)k==="local"?(!i.importedFromMachine||i.upstreamSessionId)&&g.push(i):i.importedFromMachine===k&&!i.upstreamSessionId&&g.push(i);t=g}if(f.interactive!==void 0){const k=f.interactive,g=[];for(const i of t)i.interactive===k&&g.push(i);t=g}const v=f.min??0,l=[];for(const k of t){let g;f.minMetric==="tokens"?g=k.contextTokens:f.minMetric==="loc"?g=z(k):g=k.costAmount,g>v&&l.push(k)}return l}function H(m){return m.dir!==void 0?C(m.dir):C(W())}function $(m,f,t){if(m===void 0||m==="")return"<unknown>";const v=B(C(m));if(f===void 0){const T=B(C(W()));return v===T?"~":v.startsWith(T+"/")?"~/"+v.slice(T.length+1):v}const l=B(C(t));let k=L(l,v);if(k===""||k===".")return"<root>";if(k.startsWith(".."))return"<unknown>";const g=k.split("/");if(g.length===0)return"<root>";const i=Math.min(f,g.length),S=g.slice(0,i);return S.length===0?"<root>":S.join("/")}function J(m,f,t={},v){let l=t.since;if(l===void 0&&t.bucket!==void 0){const e=new Date;t.bucket==="hour"?(e.setHours(e.getHours()-24),l=e):t.bucket==="day"?(e.setDate(e.getDate()-30),l=e):t.bucket==="week"?(e.setMonth(e.getMonth()-6),l=e):t.bucket==="month"&&(e.setFullYear(e.getFullYear()-2),l=e)}if(!(l!==void 0||t.bucket!==void 0||t.tokens===!0||t.loc===!0||t.by!==void 0||t.dir!==void 0||t.interactive!==void 0)){let e=0;for(const r of m)e+=r.costAmount;let o="";for(const r of m){o=r.costCurrency;break}return{kind:"total",row:{label:"All sessions",costAmount:e,sessionCount:m.length},currency:o}}const g={since:l,dir:t.dir,interactive:t.interactive,min:t.min,minMetric:t.loc===!0?"loc":t.tokens===!0?"tokens":"cost"},i=N(m,g),S=new Map;if(f!==void 0)for(const e of f){const o=S.get(e.sessionId);o===void 0?S.set(e.sessionId,[e]):o.push(e)}let T="";if(f!==void 0&&f.length>0)for(const e of f){T=e.currency;break}else if(i.length>0)for(const e of i){T=e.costCurrency;break}if(t.by==="filetype"&&t.bucket===void 0){const e=new Map,o=new Set;for(const u of i){const n=u.locByFiletype;if(n===void 0)continue;let a=!1;for(const d of Object.keys(n)){const w=n[d];if(w===void 0)continue;let s=e.get(d);s===void 0&&(s={added:0,removed:0,sessions:new Set},e.set(d,s)),s.added+=w.added,s.removed+=w.removed,(w.added>0||w.removed>0)&&(s.sessions.add(u.sessionId),a=!0)}a&&o.add(u.sessionId)}const r=[];for(const[u,n]of e){const a={label:u,costAmount:0,sessionCount:n.sessions.size,linesAdded:n.added,linesRemoved:n.removed};r.push({label:u,items:[a]})}return{kind:"grouped",groups:r,currency:T,totalSessions:o.size}}const I=e=>t.by==="dir"?$(e.cwd,t.depth,H(t)):t.by==="session"?e.sessionId.startsWith("hydra_session_")?e.sessionId.slice(14):e.sessionId:t.by==="model"?e.model===""?"<unknown>":e.model:t.by==="agent"?e.agentId===""?"<unknown>":e.agentId:"<all>",R=e=>{const o=new Date(e);if(Number.isNaN(o.getTime()))return"<invalid>";if(t.bucket==="hour"){const r=o.toLocaleDateString(void 0,{month:"2-digit",day:"2-digit"}),u=String(o.getHours()).padStart(2,"0");return`${r} ${u}:00`}if(t.bucket==="day")return o.toLocaleDateString(void 0,{year:"numeric",month:"2-digit",day:"2-digit"});if(t.bucket==="week"){const r=new Date(o),u=r.getDay(),n=r.getDate()-u+(u===0?-6:1);return r.setDate(n),r.toLocaleDateString(void 0,{year:"numeric",month:"2-digit",day:"2-digit"})}return t.bucket==="month"?o.toLocaleDateString(void 0,{year:"numeric",month:"2-digit"}):o.toLocaleDateString(void 0,{year:"numeric",month:"2-digit",day:"2-digit"})};if(t.by===void 0&&t.bucket===void 0){let e=0,o=0,r=0,u=0,n=0,a=0;for(const w of i){e+=w.costAmount;const s=S.get(w.sessionId);if(s!==void 0)for(const c of s)o+=c.deltaCost,t.tokens===!0&&(c.inputTokens!==void 0&&(r+=c.inputTokens),c.outputTokens!==void 0&&(u+=c.outputTokens),c.cacheReadTokens!==void 0&&(n+=c.cacheReadTokens),c.cacheWriteTokens!==void 0&&(a+=c.cacheWriteTokens))}const d={label:"All sessions",costAmount:e,deltaCost:o,sessionCount:i.length};if(t.tokens===!0&&(d.inputTokens=r,d.outputTokens=u,d.cacheReadTokens=n,d.cacheWriteTokens=a),t.loc===!0){let w=0,s=0;for(const c of i){const p=c.locByFiletype;if(p!==void 0)for(const b of Object.keys(p)){const h=p[b];h!==void 0&&(w+=h.added,s+=h.removed)}}d.linesAdded=w,d.linesRemoved=s}return{kind:"total",row:d,currency:T}}if(t.by!==void 0&&t.bucket===void 0){const e=new Map;for(const r of i){const u=I(r);let n=e.get(u);if(n===void 0&&(n={label:u,rows:{label:u,costAmount:0,deltaCost:0,sessionCount:0}},e.set(u,n)),n.rows.costAmount+=r.costAmount,n.rows.sessionCount=(n.rows.sessionCount??0)+1,t.tokens===!0&&(n.rows.inputTokens===void 0&&(n.rows.inputTokens=0),n.rows.inputTokens+=r.contextTokens),t.loc===!0){const d=r.locByFiletype;if(d!==void 0){n.rows.linesAdded===void 0&&(n.rows.linesAdded=0),n.rows.linesRemoved===void 0&&(n.rows.linesRemoved=0);for(const w of Object.keys(d)){const s=d[w];s!==void 0&&(n.rows.linesAdded+=s.added,n.rows.linesRemoved+=s.removed)}}}const a=S.get(r.sessionId);if(a!==void 0)for(const d of a)n.rows.deltaCost+=d.deltaCost,t.tokens===!0&&(n.rows.inputTokens===void 0&&(n.rows.inputTokens=0),n.rows.outputTokens===void 0&&(n.rows.outputTokens=0),n.rows.cacheReadTokens===void 0&&(n.rows.cacheReadTokens=0),n.rows.cacheWriteTokens===void 0&&(n.rows.cacheWriteTokens=0),d.inputTokens!==void 0&&(n.rows.inputTokens+=d.inputTokens),d.outputTokens!==void 0&&(n.rows.outputTokens+=d.outputTokens),d.cacheReadTokens!==void 0&&(n.rows.cacheReadTokens+=d.cacheReadTokens),d.cacheWriteTokens!==void 0&&(n.rows.cacheWriteTokens+=d.cacheWriteTokens))}const o=[];for(const r of e.values())o.push({label:r.label,items:[r.rows]});return{kind:"grouped",groups:o,currency:T}}const E=new Map;for(const e of i)E.set(e.sessionId,e);const A=new Map;if(f!==void 0){for(const e of f){if(!E.has(e.sessionId))continue;const o=A.get(e.sessionId);o===void 0?A.set(e.sessionId,[e]):o.push(e)}for(const e of A.values())e.sort((o,r)=>o.ts.localeCompare(r.ts))}const O=(e,o)=>{e._sessions===void 0&&(e._sessions=new Set),e._sessions.add(o),e.sessionCount=e._sessions.size},_=e=>{delete e._sessions},F=(e,o,r)=>{const u=R(o.ts);let n=e.get(u);n===void 0&&(n={bucket:u,costAmount:0,deltaCost:0,sessionCount:0},e.set(u,n)),n.costAmount+=r,n.deltaCost+=r,O(n,o.sessionId),t.tokens===!0&&(n.inputTokens===void 0||(o.inputTokens??0)>n.inputTokens)&&(n.inputTokens=o.inputTokens??n.inputTokens??0)};if(t.loc===!0&&t.bucket!==void 0){const e=new Set;for(const s of i)e.add(s.sessionId);const o=new Map;for(const s of i)o.set(s.sessionId,s);const r=(s,c)=>{let p=s.get(c);return p===void 0&&(p={bucket:c,costAmount:0,deltaCost:0,sessionCount:0,linesAdded:0,linesRemoved:0},s.set(c,p)),(p.linesAdded??void 0)===void 0&&(p.linesAdded=0),(p.linesRemoved??void 0)===void 0&&(p.linesRemoved=0),p},u=(s,c)=>{s.linesAdded=(s.linesAdded??0)+c.linesAdded,s.linesRemoved=(s.linesRemoved??0)+c.linesRemoved,s._sessions===void 0&&(s._sessions=new Set),s._sessions.add(c.sessionId),s.sessionCount=s._sessions.size},n=s=>{delete s._sessions},a=v??[];if(t.by==="filetype"){const s=new Map,c=new Set;for(const b of a){if(!e.has(b.sessionId)||l!==void 0&&new Date(b.ts)<l)continue;let h=s.get(b.filetype);h===void 0&&(h={label:b.filetype,buckets:new Map},s.set(b.filetype,h));const y=r(h.buckets,R(b.ts));u(y,b),c.add(b.sessionId)}const p=[];for(const b of s.values()){const h=Array.from(b.buckets.values());for(const y of h)n(y);h.sort((y,x)=>y.bucket.localeCompare(x.bucket)),h.length>0&&p.push({label:b.label,items:h})}return{kind:"timeSeriesGrouped",groups:p,currency:T,totalSessions:c.size}}if(t.by!==void 0){const s=new Map;for(const p of a){if(!e.has(p.sessionId)||l!==void 0&&new Date(p.ts)<l)continue;const b=o.get(p.sessionId);if(b===void 0)continue;const h=I(b);let y=s.get(h);y===void 0&&(y={label:h,buckets:new Map},s.set(h,y));const x=r(y.buckets,R(p.ts));u(x,p)}const c=[];for(const p of s.values()){const b=Array.from(p.buckets.values());for(const h of b)n(h);b.sort((h,y)=>h.bucket.localeCompare(y.bucket)),b.length>0&&c.push({label:p.label,items:b})}return{kind:"timeSeriesGrouped",groups:c,currency:T}}const d=new Map;for(const s of a){if(!e.has(s.sessionId)||l!==void 0&&new Date(s.ts)<l)continue;const c=r(d,R(s.ts));u(c,s)}const w=Array.from(d.values());for(const s of w)n(s);return w.sort((s,c)=>s.bucket.localeCompare(c.bucket)),{kind:"timeSeries",timeSeries:w,currency:T}}if(t.by!==void 0&&t.bucket!==void 0){const e=new Map,o=u=>{const n=I(u);let a=e.get(n);return a===void 0&&(a={label:n,buckets:new Map},e.set(n,a)),a.buckets};for(const u of i){const n=A.get(u.sessionId);if(n===void 0||n.length===0)continue;const a=o(u),d=n[0];if(d===void 0)continue;let w=d.cumulativeCost;for(let s=1;s<n.length;s++){const c=n[s];if(c===void 0)continue;const p=Math.max(0,c.cumulativeCost-w);w=c.cumulativeCost,!(l!==void 0&&new Date(c.ts)<l)&&F(a,c,p)}}const r=[];for(const u of e.values()){const n=Array.from(u.buckets.values());for(const a of n)_(a);n.sort((a,d)=>a.bucket.localeCompare(d.bucket)),n.length>0&&r.push({label:u.label,items:n})}return{kind:"timeSeriesGrouped",groups:r,currency:T}}const G=new Map;for(const e of i){const o=A.get(e.sessionId);if(o===void 0||o.length===0)continue;const r=o[0];if(r===void 0)continue;let u=r.cumulativeCost;for(let n=1;n<o.length;n++){const a=o[n];if(a===void 0)continue;const d=Math.max(0,a.cumulativeCost-u);u=a.cumulativeCost,!(l!==void 0&&new Date(a.ts)<l)&&F(G,a,d)}}const D=Array.from(G.values());for(const e of D)_(e);return D.sort((e,o)=>e.bucket.localeCompare(o.bucket)),{kind:"timeSeries",timeSeries:D,currency:T}}export{J as aggregate,N as applyFilters,K as parseSince};
@@ -1,16 +1,16 @@
1
- function h(e){return"label"in e?e.label:e.bucket}function R(e){let t=0,u=!1;for(const c of e){if(c===void 0)continue;const o=String(c);o.length>t&&(t=o.length),c!==1&&(u=!0)}const s=u?8:7,i=e.map(c=>{if(c===void 0)return"";const o=String(c).padStart(t),n=(c===1?"session":"sessions").padEnd(s);return`${o} ${n}`}),r=t+1+s;return{strs:i,width:r}}function g(e){if(e<1e3)return String(e);let t,u;return e<1e6?(t=e/1e3,u="k"):e<1e9?(t=e/1e6,u="M"):(t=e/1e9,u="B"),(t%1===0?t.toFixed(0):t.toFixed(2)).replace(/\.?0+$/,"")+u}function _(e,t){let u=0;for(const r of e)r>u&&(u=r);if(u<=0||t<=0)return e.map(()=>"");const s=Math.max(1,t),i=[];for(const r of e){const c=r/u,o=Math.round(c*s),n=s-o;i.push("\u2588".repeat(o)+"\u2591".repeat(n))}return i}function v(e,t={}){const u=process.stdout.columns??80,s=t.histogram===!0,i=t.tokens===!0;let r="";const c=o=>`${o} session${o===1?"":"s"}`;if(e.kind==="total"){const o=m(e.row.costAmount),n=e.row.sessionCount,l=n!==void 0?c(n):e.row.label??"all sessions";r+=`Total: ${o} across ${l}
2
- `}else if(e.kind==="grouped"){const o=x(e.groups),n=m(o);let l=0;for(const a of e.groups)for(const d of a.items)l+=d.sessionCount??0;r+=`Total: ${n} across ${c(l)}
3
- `}else if(e.kind==="timeSeries"){const o=e.timeSeries.reduce((a,d)=>a+d.costAmount,0),n=m(o),l=e.timeSeries.reduce((a,d)=>a+(d.sessionCount??0),0);r+=`Total: ${n} across ${c(l)}
4
- `}else if(e.kind==="timeSeriesGrouped"){const o=x(e.groups),n=m(o);let l=0;for(const a of e.groups)for(const d of a.items)l+=d.sessionCount??0;r+=`Total: ${n} across ${c(l)}
5
- `}if(e.kind==="grouped")if(e.groups.every(n=>n.items.length===1)){const n=[];for(const l of e.groups){const a=l.items[0];a!==void 0&&n.push({...a,label:l.label})}n.sort((l,a)=>{const d=i?p(l):l.costAmount;return(i?p(a):a.costAmount)-d}),s?r+=T(n,u,i):r+=w(n,i)}else for(const n of e.groups){r+=`
6
- ${n.label}:
7
- `;const l=n.items,a=l.some(d=>d.inputTokens!==void 0||d.outputTokens!==void 0);s?r+=T(l,u,i):r+=S(l,a,i)}else if(e.kind==="timeSeries"){const o=e.timeSeries.some(n=>n.inputTokens!==void 0||n.outputTokens!==void 0);s?r+=T(e.timeSeries,u,i):r+=S(e.timeSeries,o,i)}else if(e.kind==="timeSeriesGrouped")for(const o of e.groups){r+=`
8
- ${o.label}:
9
- `;const n=o.items.some(l=>l.inputTokens!==void 0||l.outputTokens!==void 0);s?r+=T(o.items,u,i):r+=S(o.items,n,i)}return r}function w(e,t){if(e.length===0)return` (no data)
10
- `;let u=0;for(const n of e)n.label.length>u&&(u=n.label.length);const s=e.map(n=>t?g(p(n)):m(n.costAmount));let i=0;for(const n of s)n.length>i&&(i=n.length);const{strs:r,width:c}=R(e.map(n=>n.sessionCount)),o=[];for(let n=0;n<e.length;n++){const l=e[n];if(l===void 0)continue;let a=` ${l.label.padEnd(u)} ${s[n].padStart(i)}`;c>0&&(a+=` ${r[n]}`),o.push(a+`
11
- `)}return o.join("")}function T(e,t,u){if(e.length===0)return` (no data)
12
- `;let s=0;for(const f of e){const k=h(f);k.length>s&&(s=k.length)}const i=e.map(f=>u?p(f):f.costAmount),r=i.map(f=>u?g(f):m(f));let c=0;for(const f of r)f.length>c&&(c=f.length);const{strs:o,width:n}=R(e.map(f=>f.sessionCount)),l=n>0?n+2:0;let a=t-s-c-l-8;a<1&&(a=e.length>1?1:0);const d=_(i,a),b=[];for(let f=0;f<e.length;f++){const k=e[f];if(k===void 0)continue;const L=h(k).padEnd(s),W=r[f].padStart(c);let C=` ${L} ${W} ${d[f]}`;n>0&&(C+=` ${o[f]}`),b.push(C+`
13
- `)}return b.join("")}function S(e,t,u){if(e.length===0)return` (no data)
14
- `;let s=28;for(const r of e){const c=h(r);c.length>s&&(s=c.length)}let i=" ";i+=B("Label",s),i+=" ",u?i+=" Tokens":(i+=" Cost",t&&(i+=" Tokens")),i+=`
15
- `;for(const r of e){const c=h(r).padEnd(s);let o=" ";if(o+=c,o+=" ",u){const n=p(r);o+=g(n).padStart(12)}else{const n=r.costAmount??0;if(o+=m(n).padStart(10),t){const l=p(r);o+=" ".padStart(2),o+=g(l).padStart(12)}}i+=o+`
16
- `}return i}function G(e){const t={kind:e.kind,currency:e.currency};if(e.kind==="total")t.row=A(e.row);else if(e.kind==="grouped"){const u=e.groups.slice().sort((s,i)=>s.label.localeCompare(i.label));t.groups=u.map(s=>({label:s.label,items:s.items.map(A)}))}else if(e.kind==="timeSeries")t.timeSeries=e.timeSeries.map($);else if(e.kind==="timeSeriesGrouped"){const u=e.groups.slice().sort((s,i)=>s.label.localeCompare(i.label));t.groups=u.map(s=>({label:s.label,items:s.items.map($)}))}return JSON.stringify(t,null,2)}function A(e){const t={label:e.label,costAmount:e.costAmount};return e.deltaCost!==void 0&&(t.deltaCost=e.deltaCost),e.inputTokens!==void 0&&(t.inputTokens=e.inputTokens),e.outputTokens!==void 0&&(t.outputTokens=e.outputTokens),e.cacheReadTokens!==void 0&&(t.cacheReadTokens=e.cacheReadTokens),e.cacheWriteTokens!==void 0&&(t.cacheWriteTokens=e.cacheWriteTokens),t}function $(e){const t={bucket:e.bucket,costAmount:e.costAmount,deltaCost:e.deltaCost};return e.inputTokens!==void 0&&(t.inputTokens=e.inputTokens),e.outputTokens!==void 0&&(t.outputTokens=e.outputTokens),e.cacheReadTokens!==void 0&&(t.cacheReadTokens=e.cacheReadTokens),e.cacheWriteTokens!==void 0&&(t.cacheWriteTokens=e.cacheWriteTokens),t}function m(e){return`$${e.toFixed(2)}`}function p(e){let t=0;return e.inputTokens!==void 0&&(t+=e.inputTokens),e.outputTokens!==void 0&&(t+=e.outputTokens),t}function B(e,t){return e.length>=t?e:e+" ".repeat(t-e.length)}function x(e){let t=0;for(const u of e)for(const s of u.items)t+=s.costAmount;return t}export{G as renderJson,v as renderText};
1
+ function h(e){return"label"in e?e.label:e.bucket}function x(e){let n=0,i=!1;for(const o of e){if(o===void 0)continue;const f=String(o);f.length>n&&(n=f.length),o!==1&&(i=!0)}const r=i?8:7,l=e.map(o=>{if(o===void 0)return"";const f=String(o).padStart(n),a=(o===1?"session":"sessions").padEnd(r);return`${f} ${a}`}),u=n+1+r;return{strs:l,width:u}}function b(e){if(e<1e3)return String(e);let n,i;return e<1e6?(n=e/1e3,i="k"):e<1e9?(n=e/1e6,i="M"):(n=e/1e9,i="B"),(n%1===0?n.toFixed(0):n.toFixed(2)).replace(/\.?0+$/,"")+i}function w(e,n){let i=0;for(const u of e)u>i&&(i=u);if(i<=0||n<=0)return e.map(()=>"");const r=Math.max(1,n),l=[];for(const u of e){const o=u/i,f=Math.round(o*r),a=r-f;l.push("\u2588".repeat(f)+"\u2591".repeat(a))}return l}function E(e,n={}){const i=process.stdout.columns??80,r=n.histogram===!0,l=n.tokens===!0,u=n.loc===!0;let o="";const f=t=>`${t} session${t===1?"":"s"}`,a=(t,s)=>{if(u){let d=0;for(const c of s)d+=v(c);return`${$(d)} lines`}return A(t)};if(e.kind==="total"){const t=e.row.sessionCount,s=t!==void 0?f(t):e.row.label??"all sessions";o+=`Total: ${a(e.row.costAmount,[e.row])} across ${s}
2
+ `}else if(e.kind==="grouped"){const t=[];let s=0;for(const c of e.groups)for(const p of c.items)t.push(p),s+=p.sessionCount??0;e.totalSessions!==void 0&&(s=e.totalSessions);const d=_(e.groups);o+=`Total: ${a(d,t)} across ${f(s)}
3
+ `}else if(e.kind==="timeSeries"){const t=e.timeSeries.reduce((d,c)=>d+c.costAmount,0),s=e.timeSeries.reduce((d,c)=>d+(c.sessionCount??0),0);o+=`Total: ${a(t,e.timeSeries)} across ${f(s)}
4
+ `}else if(e.kind==="timeSeriesGrouped"){const t=[];let s=0;for(const c of e.groups)for(const p of c.items)t.push(p),s+=p.sessionCount??0;e.totalSessions!==void 0&&(s=e.totalSessions);const d=_(e.groups);o+=`Total: ${a(d,t)} across ${f(s)}
5
+ `}if(e.kind==="grouped")if(e.groups.every(s=>s.items.length===1)){const s=[];for(const d of e.groups){const c=d.items[0];c!==void 0&&s.push({...c,label:d.label})}s.sort((d,c)=>{const p=S(d,l,u);return S(c,l,u)-p}),r?o+=k(s,i,l,u):o+=j(s,l,u)}else for(const s of e.groups){o+=`
6
+ ${s.label}:
7
+ `;const d=s.items,c=d.some(p=>p.inputTokens!==void 0||p.outputTokens!==void 0);r?o+=k(d,i,l,u):o+=R(d,c,l,u)}else if(e.kind==="timeSeries"){const t=e.timeSeries.some(s=>s.inputTokens!==void 0||s.outputTokens!==void 0);r?o+=k(e.timeSeries,i,l,u):o+=R(e.timeSeries,t,l,u)}else if(e.kind==="timeSeriesGrouped")for(const t of e.groups){o+=`
8
+ ${t.label}:
9
+ `;const s=t.items.some(d=>d.inputTokens!==void 0||d.outputTokens!==void 0);r?o+=k(t.items,i,l,u):o+=R(t.items,s,l,u)}return o}function S(e,n,i){return i?v(e):n?T(e):e.costAmount}function B(e,n,i){return i?$(v(e)):n?b(T(e)):A(e.costAmount)}function j(e,n,i){if(e.length===0)return` (no data)
10
+ `;let r=0;for(const t of e)t.label.length>r&&(r=t.label.length);const l=e.map(t=>B(t,n,i));let u=0;for(const t of l)t.length>u&&(u=t.length);const{strs:o,width:f}=x(e.map(t=>t.sessionCount)),a=[];for(let t=0;t<e.length;t++){const s=e[t];if(s===void 0)continue;const d=l[t]??"";let c=` ${s.label.padEnd(r)} ${d.padStart(u)}`;f>0&&(c+=` ${o[t]}`),a.push(c+`
11
+ `)}return a.join("")}function k(e,n,i,r){if(e.length===0)return` (no data)
12
+ `;let l=0;for(const m of e){const g=h(m);g.length>l&&(l=g.length)}const u=e.map(m=>{const g=S(m,i,r);return r?Math.abs(g):g}),o=e.map(m=>B(m,i,r));let f=0;for(const m of o)m.length>f&&(f=m.length);const{strs:a,width:t}=x(e.map(m=>m.sessionCount)),s=t>0?t+2:0;let d=n-l-f-s-8;d<1&&(d=e.length>1?1:0);const c=w(u,d),p=[];for(let m=0;m<e.length;m++){const g=e[m];if(g===void 0)continue;const G=h(g).padEnd(l),y=(o[m]??"").padStart(f);let C=` ${G} ${y} ${c[m]??""}`;t>0&&(C+=` ${a[m]}`),p.push(C+`
13
+ `)}return p.join("")}function R(e,n,i,r){if(e.length===0)return` (no data)
14
+ `;let l=28;for(const o of e){const f=h(o);f.length>l&&(l=f.length)}let u=" ";u+=N("",l),u+=" ",r?u+=" +Added -Removed Net":i?u+=" Tokens":(u+=" Cost",n&&(u+=" Tokens")),u+=`
15
+ `;for(const o of e){const f=h(o).padEnd(l);let a=" ";if(a+=f,a+=" ",r){const t=o.linesAdded??0,s=o.linesRemoved??0;a+=t.toLocaleString().padStart(10),a+=" ",a+=s.toLocaleString().padStart(8),a+=" ",a+=$(t-s).padStart(8)}else if(i){const t=T(o);a+=b(t).padStart(12)}else{const t=o.costAmount??0;if(a+=A(t).padStart(10),n){const s=T(o);a+=" ".padStart(2),a+=b(s).padStart(12)}}u+=a+`
16
+ `}return u}function F(e){const n={kind:e.kind,currency:e.currency};if(e.kind==="total")n.row=L(e.row);else if(e.kind==="grouped"){const i=e.groups.slice().sort((r,l)=>r.label.localeCompare(l.label));n.groups=i.map(r=>({label:r.label,items:r.items.map(L)}))}else if(e.kind==="timeSeries")n.timeSeries=e.timeSeries.map(W);else if(e.kind==="timeSeriesGrouped"){const i=e.groups.slice().sort((r,l)=>r.label.localeCompare(l.label));n.groups=i.map(r=>({label:r.label,items:r.items.map(W)}))}return JSON.stringify(n,null,2)}function L(e){const n={label:e.label,costAmount:e.costAmount};return e.deltaCost!==void 0&&(n.deltaCost=e.deltaCost),e.inputTokens!==void 0&&(n.inputTokens=e.inputTokens),e.outputTokens!==void 0&&(n.outputTokens=e.outputTokens),e.cacheReadTokens!==void 0&&(n.cacheReadTokens=e.cacheReadTokens),e.cacheWriteTokens!==void 0&&(n.cacheWriteTokens=e.cacheWriteTokens),e.linesAdded!==void 0&&(n.linesAdded=e.linesAdded),e.linesRemoved!==void 0&&(n.linesRemoved=e.linesRemoved),(e.linesAdded!==void 0||e.linesRemoved!==void 0)&&(n.linesNet=(e.linesAdded??0)-(e.linesRemoved??0)),n}function W(e){const n={bucket:e.bucket,costAmount:e.costAmount,deltaCost:e.deltaCost};return e.inputTokens!==void 0&&(n.inputTokens=e.inputTokens),e.outputTokens!==void 0&&(n.outputTokens=e.outputTokens),e.cacheReadTokens!==void 0&&(n.cacheReadTokens=e.cacheReadTokens),e.cacheWriteTokens!==void 0&&(n.cacheWriteTokens=e.cacheWriteTokens),e.linesAdded!==void 0&&(n.linesAdded=e.linesAdded),e.linesRemoved!==void 0&&(n.linesRemoved=e.linesRemoved),(e.linesAdded!==void 0||e.linesRemoved!==void 0)&&(n.linesNet=(e.linesAdded??0)-(e.linesRemoved??0)),n}function A(e){return`$${e.toFixed(2)}`}function v(e){const n=e.linesAdded??0,i=e.linesRemoved??0;return n-i}function $(e){return`${e>0?"+":""}${e.toLocaleString()}`}function T(e){let n=0;return e.inputTokens!==void 0&&(n+=e.inputTokens),e.outputTokens!==void 0&&(n+=e.outputTokens),n}function N(e,n){return e.length>=n?e:e+" ".repeat(n-e.length)}function _(e){let n=0;for(const i of e)for(const r of i.items)n+=r.costAmount;return n}export{F as renderJson,E as renderText};
@@ -1 +1 @@
1
- import{createInterface as j}from"node:readline";import{createReadStream as E,statSync as N}from"node:fs";import{resolve as x}from"node:path";import{logger as O}from"../util/log.js";import{sessionsDir as $}from"./session-store.js";const b=O("cost/history-stream");function D(t){if(!t||typeof t!="object"||Array.isArray(t))return;const n=t["hydra-acp"];if(!n||typeof n!="object"||Array.isArray(n))return;const e=n.cumulativeCost;return typeof e=="number"?e:void 0}function J(t){const n=D(t._meta),e=t.cost??void 0,s=n!==void 0?n:typeof e?.amount=="number"?e.amount:void 0;if(s===void 0)return;const u=typeof e?.currency=="string"?e.currency:void 0;return{amount:s,currency:u}}function M(t){const n=t.recordedAt;return typeof n=="number"?new Date(n).toISOString():typeof n=="string"?n:""}async function*L(t){const n=Array.isArray(t)?t:[t];for(const e of n){const s=x($(),e.sessionId,"history.jsonl");try{N(s)}catch(c){const o=c;if(o.code==="ENOENT"){b.debug(`no history for ${e.sessionId}`);continue}b.debug(`stat failed for ${s}: ${o.message}`);continue}const u=E(s,{encoding:"utf8"}),k=j({input:u,crlfDelay:1/0}),h=new Map;try{for await(const c of k){if(c.length===0)continue;let o;try{o=JSON.parse(c)}catch{b.debug(`malformed JSON in history.jsonl for ${e.sessionId}`);continue}if(!o||typeof o!="object"||Array.isArray(o))continue;const a=o;if(a.method!=="session/update")continue;const d=a.params??void 0;if(!d||typeof d!="object"||Array.isArray(d))continue;const i=d.update??void 0;if(!i||typeof i!="object"||Array.isArray(i)||i.sessionUpdate!=="usage_update")continue;const I=M(a),f=J(i);if(f===void 0)continue;const T=f.currency??"",m=f.amount,C=h.get(e.sessionId)??0,S=Math.max(0,m-C);h.set(e.sessionId,m);const r=i.usage??void 0;let y,p,l,g;if(r&&typeof r=="object"&&!Array.isArray(r)){const w=r.inputTokens;typeof w=="number"&&(y=w);const A=r.outputTokens;typeof A=="number"&&(p=A);const R=r.cacheReadInputTokens;typeof R=="number"&&(l=R);const v=r.cacheCreationInputTokens;typeof v=="number"&&(g=v)}yield{sessionId:e.sessionId,ts:I,deltaCost:S,cumulativeCost:m,currency:T,...y!==void 0&&{inputTokens:y},...p!==void 0&&{outputTokens:p},...l!==void 0&&{cacheReadTokens:l},...g!==void 0&&{cacheWriteTokens:g}}}}finally{k.close(),u.destroy()}}}export{L as streamHistoryEvents};
1
+ import{createInterface as j}from"node:readline";import{createReadStream as S,statSync as x}from"node:fs";import{resolve as N}from"node:path";import{logger as _}from"../util/log.js";import{sessionsDir as $}from"./session-store.js";import{filetypeForPath as D}from"./language.js";const R=_("cost/history-stream");function M(n){if(!n||typeof n!="object"||Array.isArray(n))return;const t=n["hydra-acp"];if(!t||typeof t!="object"||Array.isArray(t))return;const e=t.cumulativeCost;return typeof e=="number"?e:void 0}function L(n){const t=M(n._meta),e=n.cost??void 0,c=t!==void 0?t:typeof e?.amount=="number"?e.amount:void 0;if(c===void 0)return;const f=typeof e?.currency=="string"?e.currency:void 0;return{amount:c,currency:f}}function O(n){const t=n.recordedAt;return typeof t=="number"?new Date(t).toISOString():typeof t=="string"?t:""}async function*q(n){const t=Array.isArray(n)?n:[n];for(const e of t){const c=N($(),e.sessionId,"history.jsonl");try{x(c)}catch(y){const o=y;if(o.code==="ENOENT"){R.debug(`no history for ${e.sessionId}`);continue}R.debug(`stat failed for ${c}: ${o.message}`);continue}const f=S(c,{encoding:"utf8"}),h=j({input:f,crlfDelay:1/0}),m=new Map;try{for await(const y of h){if(y.length===0)continue;let o;try{o=JSON.parse(y)}catch{R.debug(`malformed JSON in history.jsonl for ${e.sessionId}`);continue}if(!o||typeof o!="object"||Array.isArray(o))continue;const r=o;if(r.method!=="session/update")continue;const a=r.params??void 0;if(!a||typeof a!="object"||Array.isArray(a))continue;const i=a.update??void 0;if(!i||typeof i!="object"||Array.isArray(i)||i.sessionUpdate!=="usage_update")continue;const u=O(r),p=L(i);if(p===void 0)continue;const w=p.currency??"",b=p.amount,g=m.get(e.sessionId)??0,d=Math.max(0,b-g);m.set(e.sessionId,b);const s=i.usage??void 0;let A,k,l,v;if(s&&typeof s=="object"&&!Array.isArray(s)){const I=s.inputTokens;typeof I=="number"&&(A=I);const T=s.outputTokens;typeof T=="number"&&(k=T);const E=s.cacheReadInputTokens;typeof E=="number"&&(l=E);const C=s.cacheCreationInputTokens;typeof C=="number"&&(v=C)}yield{sessionId:e.sessionId,ts:u,deltaCost:d,cumulativeCost:b,currency:w,...A!==void 0&&{inputTokens:A},...k!==void 0&&{outputTokens:k},...l!==void 0&&{cacheReadTokens:l},...v!==void 0&&{cacheWriteTokens:v}}}}finally{h.close(),f.destroy()}}}function J(n){if(n.length===0)return 0;let t=1;for(let e=0;e<n.length;e++)n.charCodeAt(e)===10&&t++;return n.charCodeAt(n.length-1)===10&&t--,t}async function*z(n){const t=Array.isArray(n)?n:[n];for(const e of t){const c=N($(),e.sessionId,"history.jsonl");try{x(c)}catch(o){const r=o;if(r.code==="ENOENT")continue;R.debug(`stat failed for ${c}: ${r.message}`);continue}const f=S(c,{encoding:"utf8"}),h=j({input:f,crlfDelay:1/0}),m=new Map,y=[];try{for await(const o of h){if(o.length===0)continue;let r;try{r=JSON.parse(o)}catch{continue}if(!r||typeof r!="object"||Array.isArray(r))continue;const a=r;if(a.method!=="session/update")continue;const i=a.params??void 0;if(!i||typeof i!="object"||Array.isArray(i))continue;const u=i.update??void 0;if(!u||typeof u!="object"||Array.isArray(u)||u.sessionUpdate!=="tool_call"&&u.sessionUpdate!=="tool_call_update")continue;const p=u.content;if(!Array.isArray(p)||p.length===0)continue;const w=typeof u.toolCallId=="string"?u.toolCallId:"";if(w==="")continue;const b=O(a);for(const g of p){if(!g||typeof g!="object"||Array.isArray(g))continue;const d=g;if(d.type!=="diff")continue;const s=typeof d.path=="string"?d.path:"";if(s==="")continue;const A=typeof d.oldText=="string"?d.oldText:"",k=typeof d.newText=="string"?d.newText:"",l=`${w}|${s}`;m.has(l)||y.push(l),m.set(l,{sessionId:e.sessionId,ts:b,path:s,filetype:D(s),linesAdded:J(k),linesRemoved:J(A)})}}}finally{h.close(),f.destroy()}for(const o of y){const r=m.get(o);r!==void 0&&(yield r)}}}export{z as streamHistoryEditEvents,q as streamHistoryEvents};
@@ -0,0 +1 @@
1
+ const f={Dockerfile:"Dockerfile",Makefile:"Makefile",GNUmakefile:"Makefile",Rakefile:"Rakefile",Gemfile:"Gemfile","CMakeLists.txt":"CMakeLists.txt"};function l(e){const i=e.lastIndexOf("/"),t=i>=0?e.slice(i+1):e,s=f[t];if(s!==void 0)return s;const n=t.lastIndexOf(".");return n<=0?"<no ext>":t.slice(n).toLowerCase()}export{l as filetypeForPath};
@@ -1 +1 @@
1
- import{readdirSync as M,readFileSync as C,realpathSync as R,statSync as j}from"node:fs";import{isAbsolute as x,resolve as a}from"node:path";import{homedir as E}from"node:os";import{logger as $}from"../util/log.js";const c=$("cost/session-store"),f=new Map;function F(n){if(f.has(n))return f.get(n);try{const s=R(n);return f.set(n,s),s}catch{f.set(n,void 0);return}}function N(){const n=process.env.HYDRA_ACP_HOME??a(E(),".hydra-acp");return a(n,"sessions")}function J(n){const s=a(n,"meta.json");let i;try{i=C(s,"utf8")}catch(g){const p=g;if(p.code==="ENOENT")return;c.debug(`skipping ${n}: read meta.json failed: ${p.message}`);return}let t;try{t=JSON.parse(i)}catch{c.debug(`skipping ${n}: meta.json is not valid JSON`);return}if(!t||typeof t!="object"||Array.isArray(t)){c.debug(`skipping ${n}: meta.json is not an object`);return}const e=t,d=typeof e.sessionId=="string"?e.sessionId:void 0;if(d===void 0){c.debug(`skipping ${n}: missing sessionId`);return}let r;const u=typeof e.cwd=="string"?e.cwd:void 0;u!==void 0&&(x(u)?r=F(u):r=u);const m=typeof e.agentId=="string"?e.agentId:"",y=typeof e.currentModel=="string"?e.currentModel:"",l=e.interactive===!0,o=e.currentUsage??void 0,S=typeof o?.costAmount=="number"?o.costAmount:0,A=typeof o?.costCurrency=="string"?o.costCurrency:"",w=typeof o?.used=="number"?o.used:0,h=typeof e.title=="string"?e.title:"",b=typeof e.createdAt=="string"?e.createdAt:"",I=typeof e.updatedAt=="string"?e.updatedAt:"",k=typeof e.importedFromMachine=="string"&&e.importedFromMachine.length>0?e.importedFromMachine:void 0,v=typeof e.upstreamSessionId=="string"&&e.upstreamSessionId.length>0?e.upstreamSessionId:void 0;return{sessionId:d,cwd:r,agentId:m,model:y,interactive:l,costAmount:S,costCurrency:A,contextTokens:w,title:h,createdAt:b,updatedAt:I,importedFromMachine:k,upstreamSessionId:v}}function _(){const n=N();let s;try{s=M(n)}catch(t){const e=t;return c.debug(`session store not found at ${n}: ${e.message}`),[]}const i=[];for(const t of s){const e=a(n,t);let d;try{d=j(e)}catch{continue}if(!d.isDirectory())continue;const r=J(e);r!==void 0&&i.push(r)}return i}export{_ as scanSessions,N as sessionsDir};
1
+ import{readdirSync as k,readFileSync as M,realpathSync as C,statSync as E}from"node:fs";import{isAbsolute as x,resolve as a}from"node:path";import{homedir as F}from"node:os";import{logger as j}from"../util/log.js";const c=j("cost/session-store"),f=new Map;function $(n){if(f.has(n))return f.get(n);try{const s=C(n);return f.set(n,s),s}catch{f.set(n,void 0);return}}function N(){const n=process.env.HYDRA_ACP_HOME??a(F(),".hydra-acp");return a(n,"sessions")}function J(n){const s=a(n,"meta.json");let o;try{o=M(s,"utf8")}catch(p){const g=p;if(g.code==="ENOENT")return;c.debug(`skipping ${n}: read meta.json failed: ${g.message}`);return}let t;try{t=JSON.parse(o)}catch{c.debug(`skipping ${n}: meta.json is not valid JSON`);return}if(!t||typeof t!="object"||Array.isArray(t)){c.debug(`skipping ${n}: meta.json is not an object`);return}const e=t,r=typeof e.sessionId=="string"?e.sessionId:void 0;if(r===void 0){c.debug(`skipping ${n}: missing sessionId`);return}let i;const u=typeof e.cwd=="string"?e.cwd:void 0;u!==void 0&&(x(u)?i=$(u):i=u);const m=typeof e.agentId=="string"?e.agentId:"",y=typeof e.currentModel=="string"?e.currentModel:"",l=e.interactive===!0,d=e.currentUsage??void 0,S=typeof d?.costAmount=="number"?d.costAmount:0,A=typeof d?.costCurrency=="string"?d.costCurrency:"",b=typeof d?.used=="number"?d.used:0,w=typeof e.title=="string"?e.title:"",h=typeof e.createdAt=="string"?e.createdAt:"",v=typeof e.updatedAt=="string"?e.updatedAt:"",I=typeof e.importedFromMachine=="string"&&e.importedFromMachine.length>0?e.importedFromMachine:void 0,R=typeof e.upstreamSessionId=="string"&&e.upstreamSessionId.length>0?e.upstreamSessionId:void 0;return{sessionId:r,cwd:i,agentId:m,model:y,interactive:l,costAmount:S,costCurrency:A,contextTokens:b,title:w,createdAt:h,updatedAt:v,importedFromMachine:I,upstreamSessionId:R}}async function U(n){const{streamHistoryEditEvents:s}=await import("./history-stream.js");for(const o of n){const t={};for await(const e of s(o)){const r=t[e.filetype]??{added:0,removed:0};r.added+=e.linesAdded,r.removed+=e.linesRemoved,t[e.filetype]=r}o.locByFiletype=t}return n}function _(){const n=N();let s;try{s=k(n)}catch(t){const e=t;return c.debug(`session store not found at ${n}: ${e.message}`),[]}const o=[];for(const t of s){const e=a(n,t);let r;try{r=E(e)}catch{continue}if(!r.isDirectory())continue;const i=J(e);i!==void 0&&o.push(i)}return o}export{U as enrichSessionsWithLoc,_ as scanSessions,N as sessionsDir};
package/dist/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env node
2
- import{readFileSync as C}from"node:fs";import{dirname as R,resolve as U}from"node:path";import{fileURLToPath as $}from"node:url";import{loadConfig as L}from"./config.js";import{BudgeterBridge as P}from"./bridge.js";import{stateFilePath as k}from"./paths.js";import{DEFAULT_RULE as N}from"./rule.js";import{deleteStateFile as x}from"./tracker.js";import{logger as G,setDebug as M}from"./util/log.js";import{scanSessions as j}from"./cost/session-store.js";import{listSessionsFromDaemon as H,fetchUsageEventsFromDaemon as A}from"./cost/daemon-client.js";import{aggregate as W,applyFilters as _,parseSince as J}from"./cost/aggregate.js";import{renderText as Y,renderJson as B}from"./cost/format.js";const S=G("main");function E(){try{const e=R($(import.meta.url));return JSON.parse(C(U(e,"../package.json"),"utf8")).version??"unknown"}catch{return"unknown"}}function V(){x(k()),process.stdout.write(`hydra-acp-budgeter accumulated cost reset
3
- `)}const K=`Usage: hydra budgeter usage [OPTIONS]
2
+ import{readFileSync as $}from"node:fs";import{dirname as C,resolve as U}from"node:path";import{fileURLToPath as P}from"node:url";import{loadConfig as N}from"./config.js";import{BudgeterBridge as x}from"./bridge.js";import{stateFilePath as I}from"./paths.js";import{DEFAULT_RULE as M}from"./rule.js";import{deleteStateFile as G}from"./tracker.js";import{logger as H,setDebug as W}from"./util/log.js";import{scanSessions as j,enrichSessionsWithLoc as A}from"./cost/session-store.js";import{listSessionsFromDaemon as B,fetchUsageEventsFromDaemon as _}from"./cost/daemon-client.js";import{streamHistoryEditEvents as J}from"./cost/history-stream.js";import{aggregate as Y,applyFilters as q,parseSince as V}from"./cost/aggregate.js";import{renderText as K,renderJson as Z}from"./cost/format.js";const D=H("main");function O(){try{const t=C(P(import.meta.url));return JSON.parse($(U(t,"../package.json"),"utf8")).version??"unknown"}catch{return"unknown"}}function z(){G(I()),process.stdout.write(`hydra-acp-budgeter accumulated cost reset
3
+ `)}const Q=`Usage: hydra budgeter usage [OPTIONS]
4
4
 
5
5
  Options:
6
6
  --since <date|duration> Only include sessions updated after this date (e.g. 7d, 2024-01-01)
7
7
  --bucket <hour|day|week|month> Group results into time buckets (implies --since 24h/30d/6m/2y)
8
- --by <dir|session|model|agent> Group by dimension
8
+ --by <dir|session|model|agent|filetype> Group by dimension (filetype requires --metric loc)
9
9
  --depth <N> Depth for --by dir grouping (default: 1)
10
10
  --dir <path> Only include sessions under this directory prefix
11
11
  --interactive Only include interactive sessions (default: include both)
@@ -15,14 +15,17 @@ Options:
15
15
  that host.
16
16
  --min <N> Drop sessions whose active-metric value is <= N (default: 0)
17
17
  --histogram Show an ASCII histogram bar next to each row (implies --bucket week if no bucket given)
18
- --metric <cost|tokens> Display metric (default: cost)
18
+ --metric <cost|tokens|loc> Display metric (default: cost). "loc" counts net lines of code
19
+ from Edit/Write tool diffs in session history.
19
20
  --json Output as JSON
20
- --help Show this help message`;async function T(e){if(e.includes("--help")){process.stdout.write(K+`
21
- `);return}let r,n,a,d,c,m=!1,l,u=!1,o,h=!1,f;for(let t=0;t<e.length;t++){const s=e[t];if(s!==void 0){if(s==="--since")t+=1,r=e[t];else if(s==="--bucket")t+=1,n=e[t];else if(s==="--by")t+=1,a=e[t];else if(s==="--depth")t+=1,d=e[t];else if(s==="--dir")t+=1,c=e[t];else if(s==="--interactive")m=!0;else if(s==="--min")t+=1,l=e[t];else if(s==="--histogram")u=!0;else if(s==="--metric")t+=1,o=e[t];else if(s==="--json")h=!0;else if(s==="--host")t+=1,f=e[t];else if(s.startsWith("--host="))f=s.slice(7);else if(s.startsWith("--"))throw new Error(`Unknown option: ${s}
22
- Run "hydra budgeter usage --help" for usage.`)}}if(o!==void 0&&o!=="cost"&&o!=="tokens")throw new Error(`Invalid --metric value. Must be 'cost' or 'tokens'.
23
- Run "hydra budgeter usage --help" for usage.`);let p;if(r!==void 0)try{p=J(r)}catch(t){const s=t;throw new Error(`Invalid --since value: ${s.message}
24
- Run "hydra budgeter usage --help" for usage.`)}e.length===0&&(n="hour",u=!0),u&&n===void 0&&(n="week");let i=p;if(i===void 0&&n!==void 0){const t=new Date;n==="hour"?(t.setHours(t.getHours()-24),i=t):n==="day"?(t.setDate(t.getDate()-30),i=t):n==="week"?(t.setMonth(t.getMonth()-6),i=t):n==="month"&&(t.setFullYear(t.getFullYear()-2),i=t)}const g=m?!0:void 0,y=o==="tokens",w=l!==void 0?parseFloat(l):void 0,D=await H()??j(),I=_(D,{since:i,dir:c,interactive:g,min:w,minMetric:y?"tokens":"cost",host:f??"local"});let b;if(n!==void 0){const t=await A();t!==void 0&&(b=t.map(s=>({sessionId:s.sessionId,ts:s.ts,deltaCost:0,cumulativeCost:s.costCumulative,currency:s.costCurrency,inputTokens:s.contextTokens})))}const O=d!==void 0?parseInt(d,10):void 0,v=W(I,b,{by:a,depth:O,bucket:n,since:i,interactive:g,dir:c,tokens:y,min:w});if(h)process.stdout.write(B(v)+`
25
- `);else{const t=Y(v,{histogram:u,tokens:o==="tokens"});process.stdout.write(t)}}async function Z(){const e=L();M(e.debug);const r=k(),n=new P({daemonWsUrl:e.hydraWsUrl,token:e.hydraToken,softLimit:e.softLimit,hardLimit:e.hardLimit,currency:e.currency,rule:N,statePath:r});n.start();const a=d=>{S.info(`${d} received \u2014 shutting down`),n.stop(),setTimeout(()=>process.exit(0),200).unref()};process.on("SIGINT",()=>a("SIGINT")),process.on("SIGTERM",()=>a("SIGTERM")),S.info(`hydra-acp-budgeter up; daemon=${e.hydraDaemonUrl} soft=${e.softLimit} hard=${e.hardLimit} ${e.currency} state=${r}`)}function q(){process.stdout.write(`hydra-acp-budgeter ${E()}
21
+ --help Show this help message`;async function R(t){if(t.includes("--help")){process.stdout.write(Q+`
22
+ `);return}let r,n,i,u,l,p=!1,f,c=!1,o,g=!1,h;for(let e=0;e<t.length;e++){const s=t[e];if(s!==void 0){if(s==="--since")e+=1,r=t[e];else if(s==="--bucket")e+=1,n=t[e];else if(s==="--by")e+=1,i=t[e];else if(s==="--depth")e+=1,u=t[e];else if(s==="--dir")e+=1,l=t[e];else if(s==="--interactive")p=!0;else if(s==="--min")e+=1,f=t[e];else if(s==="--histogram")c=!0;else if(s==="--metric")e+=1,o=t[e];else if(s==="--json")g=!0;else if(s==="--host")e+=1,h=t[e];else if(s.startsWith("--host="))h=s.slice(7);else if(s.startsWith("--"))throw new Error(`Unknown option: ${s}
23
+ Run "hydra budgeter usage --help" for usage.`)}}if(o!==void 0&&o!=="cost"&&o!=="tokens"&&o!=="loc")throw new Error(`Invalid --metric value. Must be 'cost', 'tokens', or 'loc'.
24
+ Run "hydra budgeter usage --help" for usage.`);const a=o==="loc";if(i!==void 0&&!new Set(["dir","session","model","agent","filetype"]).has(i))throw new Error(`Invalid --by value: ${i}. Must be one of: dir, session, model, agent, filetype.
25
+ Run "hydra budgeter usage --help" for usage.`);if(i==="filetype"&&!a)throw new Error(`--by filetype requires --metric loc.
26
+ Run "hydra budgeter usage --help" for usage.`);let y;if(r!==void 0)try{y=V(r)}catch(e){const s=e;throw new Error(`Invalid --since value: ${s.message}
27
+ Run "hydra budgeter usage --help" for usage.`)}t.length===0&&(n="hour",c=!0),c&&n===void 0&&(n="week");let d=y;if(d===void 0&&n!==void 0){const e=new Date;n==="hour"?(e.setHours(e.getHours()-24),d=e):n==="day"?(e.setDate(e.getDate()-30),d=e):n==="week"?(e.setMonth(e.getMonth()-6),d=e):n==="month"&&(e.setFullYear(e.getFullYear()-2),d=e)}const w=p?!0:void 0,m=o==="tokens",v=f!==void 0?parseFloat(f):void 0,b=await B()??j();(a||i==="filetype")&&await A(b);const k=q(b,{since:d,dir:l,interactive:w,min:v,minMetric:a?"loc":m?"tokens":"cost",host:h??"local"});let E;if(n!==void 0){const e=await _();e!==void 0&&(E=e.map(s=>({sessionId:s.sessionId,ts:s.ts,deltaCost:0,cumulativeCost:s.costCumulative,currency:s.costCurrency,inputTokens:s.contextTokens})))}let S;if(a&&n!==void 0){const e=[];for await(const s of J(k))e.push(s);S=e}const F=u!==void 0?parseInt(u,10):void 0,T=Y(k,E,{by:i,depth:F,bucket:n,since:d,interactive:w,dir:l,tokens:m,loc:a,min:v},S);if(g)process.stdout.write(Z(T)+`
28
+ `);else{const e=K(T,{histogram:c,tokens:m,loc:a});process.stdout.write(e)}}async function X(){const t=N();W(t.debug);const r=I(),n=new x({daemonWsUrl:t.hydraWsUrl,token:t.hydraToken,softLimit:t.softLimit,hardLimit:t.hardLimit,currency:t.currency,rule:M,statePath:r});n.start();const i=u=>{D.info(`${u} received \u2014 shutting down`),n.stop(),setTimeout(()=>process.exit(0),200).unref()};process.on("SIGINT",()=>i("SIGINT")),process.on("SIGTERM",()=>i("SIGTERM")),D.info(`hydra-acp-budgeter up; daemon=${t.hydraDaemonUrl} soft=${t.softLimit} hard=${t.hardLimit} ${t.currency} state=${r}`)}function ee(){process.stdout.write(`hydra-acp-budgeter ${O()}
26
29
 
27
30
  Usage:
28
31
  hydra budgeter [usage] <flags> Report historical cost/usage across sessions
@@ -31,6 +34,6 @@ Usage:
31
34
  Flags:
32
35
  -v, --version Print version and exit
33
36
  -h, --help Show this help
34
- `)}async function z(){const e=process.argv.slice(2);if(e.includes("--version")||e.includes("-v")){process.stdout.write(`hydra-acp-budgeter ${E()}
35
- `);return}if((e[0]==="help"||e.includes("--help")||e.includes("-h"))&&e[0]!=="usage"&&e[0]!=="cost"){q();return}if(e[0]==="reset"){V();return}if(e[0]==="usage"||e[0]==="cost"){await T(e.slice(1));return}if(e[0]==="run"||process.env.HYDRA_ACP_TOKEN){await Z();return}await T(e)}z().catch(e=>{process.stderr.write(`hydra-acp-budgeter: ${e.message}
37
+ `)}async function te(){const t=process.argv.slice(2);if(t.includes("--version")||t.includes("-v")){process.stdout.write(`hydra-acp-budgeter ${O()}
38
+ `);return}if((t[0]==="help"||t.includes("--help")||t.includes("-h"))&&t[0]!=="usage"&&t[0]!=="cost"){ee();return}if(t[0]==="reset"){z();return}if(t[0]==="usage"||t[0]==="cost"){await R(t.slice(1));return}if(t[0]==="run"||process.env.HYDRA_ACP_TOKEN){await X();return}await R(t)}te().catch(t=>{process.stderr.write(`hydra-acp-budgeter: ${t.message}
36
39
  `),process.exit(1)});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hydra-acp/budgeter",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "Cost-budget transformer extension for hydra-acp — warns on soft limit, rejects further prompts/sessions on hard limit.",
5
5
  "license": "MIT",
6
6
  "type": "module",