grix-connector 2.1.3 → 2.2.0

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,5 +1,5 @@
1
- import{createReadStream as A,readFileSync as S,statSync as k}from"node:fs";import{createInterface as j}from"node:readline";import T from"node:path";import E from"node:os";import{log as _}from"../../core/log/index.js";function I(e){return e.replace(/[/\\]/g,"-").replace(/:/g,"-")}function m(e,r){const t=T.join(E.homedir(),".claude"),u=T.join(t,"projects",I(r));return T.join(u,`${e}.jsonl`)}function R(){return{inputTokens:0,outputTokens:0,cacheReadInputTokens:0,cacheCreationInputTokens:0}}function b(e,r){e.inputTokens+=r.input_tokens??0,e.outputTokens+=r.output_tokens??0,e.cacheReadInputTokens+=r.cache_read_input_tokens??0,e.cacheCreationInputTokens+=r.cache_creation_input_tokens??0}async function $(e,r){const t=m(e,r);_.info("usage-parser",`Parsing session usage from ${t}`);const u=new Map,a=R();let n=0;try{const c=j({input:A(t,"utf8"),crlfDelay:1/0});for await(const s of c)if(s.trim())try{const l=JSON.parse(s);if(l.type!=="assistant")continue;const o=l.message?.usage;if(!o)continue;const f=l.message?.model??"unknown";n++,b(a,o);let p=u.get(f);p||(p={turns:0,usage:R()},u.set(f,p)),p.turns++,b(p.usage,o)}catch{}}catch(c){return c.code==="ENOENT"?(_.info("usage-parser",`Session JSONL not found: ${t}`),null):(_.error("usage-parser",`Failed to parse session usage: ${c}`),null)}return n===0?null:{models:[...u.entries()].map(([c,s])=>({model:c,turns:s.turns,total:s.usage})),total:a,turns:n}}const h=64*1024,M=9e4;function N(e,r,t){const u=m(e,r);try{const a=S(u);let n;t!==void 0&&t>0?n=t<a.length?a.subarray(t):Buffer.alloc(0):n=a.length>h?a.subarray(a.length-h):a;const i=n.toString("utf8").split(`
2
- `);for(let c=i.length-1;c>=0;c--){const s=i[c].trim();if(!s)continue;let l;try{l=JSON.parse(s)}catch{continue}if(l.type!=="assistant")continue;const o=l.message;if(!o?.content)continue;const f=Array.isArray(o.content)?o.content:[{type:"text",text:String(o.content)}],p=[];for(const g of f){const y=g;y.type==="text"&&y.text?.trim()&&p.push(y.text)}if(p.length>0)return{text:p.join(`
3
- `),stopReason:o.stop_reason??null}}return null}catch{return null}}function B(e,r,t){return N(e,r,t)?.text??null}function F(e,r,t){const u=m(e,r);let a=null,n;try{const s=k(u);a=Date.now()-s.mtimeMs,n=S(u)}catch{return{lastStopReason:null,freshMs:null}}let i;t!==void 0&&t>0?i=t<n.length?n.subarray(t):Buffer.alloc(0):i=n.length>h?n.subarray(n.length-h):n;const c=i.toString("utf8").split(`
4
- `);for(let s=c.length-1;s>=0;s--){const l=c[s].trim();if(!l)continue;let o;try{o=JSON.parse(l)}catch{continue}if(o.type!=="assistant")continue;const f=o.message;if(f)return{lastStopReason:f.stop_reason??null,freshMs:a}}return{lastStopReason:null,freshMs:a}}function v(e,r,t,u=!1){const a=m(e,r);try{const n=k(a);if(u&&Date.now()-n.mtimeMs<M)return!1;const i=S(a),s=(t>0&&t<i.length?i.subarray(t):i.length>h?i.subarray(i.length-h):i).toString("utf8").split(`
5
- `);let l=!1,o=!1;for(const f of s){const p=f.trim();if(!p)continue;let g;try{g=JSON.parse(p)}catch{continue}if(g.type==="user"){const x=g.message?.content??g.content;(Array.isArray(x)?x:[]).some(d=>typeof d=="object"&&d!==null&&d.type==="tool_result")&&(l=!0,o=!1)}else g.type==="assistant"&&l&&(o=!0)}return l&&!o}catch{return!1}}export{N as extractLastAssistantEntry,B as extractLastAssistantText,v as hasTerminalToolResultAfterOffset,$ as parseClaudeSessionUsage,F as probeSessionTurnState,m as resolveSessionJsonlPath};
1
+ import{createReadStream as A,readFileSync as S,realpathSync as j,statSync as k}from"node:fs";import{createInterface as E}from"node:readline";import T from"node:path";import I from"node:os";import{log as _}from"../../core/log/index.js";function M(e){let n=e;try{n=j(e)}catch{}return n.replace(/[^a-zA-Z0-9]/g,"-")}function m(e,n){const t=T.join(I.homedir(),".claude"),u=T.join(t,"projects",M(n));return T.join(u,`${e}.jsonl`)}function R(){return{inputTokens:0,outputTokens:0,cacheReadInputTokens:0,cacheCreationInputTokens:0}}function b(e,n){e.inputTokens+=n.input_tokens??0,e.outputTokens+=n.output_tokens??0,e.cacheReadInputTokens+=n.cache_read_input_tokens??0,e.cacheCreationInputTokens+=n.cache_creation_input_tokens??0}async function B(e,n){const t=m(e,n);_.info("usage-parser",`Parsing session usage from ${t}`);const u=new Map,a=R();let s=0;try{const c=E({input:A(t,"utf8"),crlfDelay:1/0});for await(const o of c)if(o.trim())try{const l=JSON.parse(o);if(l.type!=="assistant")continue;const r=l.message?.usage;if(!r)continue;const f=l.message?.model??"unknown";s++,b(a,r);let p=u.get(f);p||(p={turns:0,usage:R()},u.set(f,p)),p.turns++,b(p.usage,r)}catch{}}catch(c){return c.code==="ENOENT"?(_.info("usage-parser",`Session JSONL not found: ${t}`),null):(_.error("usage-parser",`Failed to parse session usage: ${c}`),null)}return s===0?null:{models:[...u.entries()].map(([c,o])=>({model:c,turns:o.turns,total:o.usage})),total:a,turns:s}}const g=64*1024,N=9e4;function P(e,n,t){const u=m(e,n);try{const a=S(u);let s;t!==void 0&&t>0?s=t<a.length?a.subarray(t):Buffer.alloc(0):s=a.length>g?a.subarray(a.length-g):a;const i=s.toString("utf8").split(`
2
+ `);for(let c=i.length-1;c>=0;c--){const o=i[c].trim();if(!o)continue;let l;try{l=JSON.parse(o)}catch{continue}if(l.type!=="assistant")continue;const r=l.message;if(!r?.content)continue;const f=Array.isArray(r.content)?r.content:[{type:"text",text:String(r.content)}],p=[];for(const h of f){const y=h;y.type==="text"&&y.text?.trim()&&p.push(y.text)}if(p.length>0)return{text:p.join(`
3
+ `),stopReason:r.stop_reason??null}}return null}catch{return null}}function z(e,n,t){return P(e,n,t)?.text??null}function F(e,n,t){const u=m(e,n);let a=null,s;try{const o=k(u);a=Date.now()-o.mtimeMs,s=S(u)}catch{return{lastStopReason:null,freshMs:null}}let i;t!==void 0&&t>0?i=t<s.length?s.subarray(t):Buffer.alloc(0):i=s.length>g?s.subarray(s.length-g):s;const c=i.toString("utf8").split(`
4
+ `);for(let o=c.length-1;o>=0;o--){const l=c[o].trim();if(!l)continue;let r;try{r=JSON.parse(l)}catch{continue}if(r.type!=="assistant")continue;const f=r.message;if(f)return{lastStopReason:f.stop_reason??null,freshMs:a}}return{lastStopReason:null,freshMs:a}}function v(e,n,t,u=!1){const a=m(e,n);try{const s=k(a);if(u&&Date.now()-s.mtimeMs<N)return!1;const i=S(a),o=(t>0&&t<i.length?i.subarray(t):i.length>g?i.subarray(i.length-g):i).toString("utf8").split(`
5
+ `);let l=!1,r=!1;for(const f of o){const p=f.trim();if(!p)continue;let h;try{h=JSON.parse(p)}catch{continue}if(h.type==="user"){const x=h.message?.content??h.content;(Array.isArray(x)?x:[]).some(d=>typeof d=="object"&&d!==null&&d.type==="tool_result")&&(l=!0,r=!1)}else h.type==="assistant"&&l&&(r=!0)}return l&&!r}catch{return!1}}export{P as extractLastAssistantEntry,z as extractLastAssistantText,v as hasTerminalToolResultAfterOffset,B as parseClaudeSessionUsage,F as probeSessionTurnState,m as resolveSessionJsonlPath};