@owloops/claude-powerline 1.21.1 → 1.21.2

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.
Files changed (2) hide show
  1. package/dist/index.mjs +2 -2
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -3,7 +3,7 @@ import e from"node:process";import{json as t}from"node:stream/consumers";import
3
3
  `).length:0}catch{return 0}}async getUpstreamAsync(e){try{return(await this.execGitAsync(`git rev-parse --abbrev-ref @{u}`,{cwd:e,encoding:`utf8`,timeout:2e3})).stdout.trim()||null}catch{return null}}async getRepoNameAsync(e){try{let t=(await this.execGitAsync(`git config --get remote.origin.url`,{cwd:e,encoding:`utf8`,timeout:2e3})).stdout.trim();return t&&t.match(/\/([^/]+?)(\.git)?$/)?.[1]||u.basename(e)}catch{return u.basename(e)}}isWorktree(e){try{let t=u.join(e,`.git`);return!!(o.existsSync(t)&&o.statSync(t).isFile())}catch{return!1}}async getStatusWithBranchAsync(e){try{O(`[GIT-EXEC] Running git status in ${e}`);let t=(await this.execGitAsync(`git status --porcelain -b`,{cwd:e,encoding:`utf8`,timeout:2e3})).stdout.split(`
4
4
  `),n=null,r=`clean`,i=0,a=0,o=0,s=0;for(let e of t)if(e){if(e.startsWith(`## `)){let t=e.substring(3).split(`...`)[0];t&&t!==`HEAD (no branch)`&&(n=t);continue}if(e.length>=2){let t=e.charAt(0),n=e.charAt(1);if(t===`?`&&n===`?`){o++,r===`clean`&&(r=`dirty`);continue}let c=t+n;if([`DD`,`AU`,`UD`,`UA`,`DU`,`AA`,`UU`].includes(c)){s++,r=`conflicts`;continue}t!==` `&&t!==`?`&&(i++,r===`clean`&&(r=`dirty`)),n!==` `&&n!==`?`&&(a++,r===`clean`&&(r=`dirty`))}}return{branch:n||await this.getFallbackBranch(e),status:r,workingTree:{staged:i,unstaged:a,untracked:o,conflicts:s}}}catch(t){return O(`Git status with branch command failed in ${e}:`,t),{branch:await this.getFallbackBranch(e),status:`clean`}}}async getFallbackBranch(e){try{let t=(await this.execGitAsync(`git branch --show-current`,{cwd:e,encoding:`utf8`,timeout:2e3})).stdout.trim();if(t)return t}catch{try{let t=(await this.execGitAsync(`git symbolic-ref --short HEAD`,{cwd:e,encoding:`utf8`,timeout:2e3})).stdout.trim();if(t)return t}catch{return null}}return null}async getAheadBehindAsync(e){try{O(`[GIT-EXEC] Running git ahead/behind in ${e}`);let[t,n]=await Promise.all([this.execGitAsync(`git rev-list --count @{u}..HEAD`,{cwd:e,encoding:`utf8`,timeout:2e3}),this.execGitAsync(`git rev-list --count HEAD..@{u}`,{cwd:e,encoding:`utf8`,timeout:2e3})]);return{ahead:parseInt(t.stdout.trim())||0,behind:parseInt(n.stdout.trim())||0}}catch(t){return O(`Git ahead/behind command failed in ${e}:`,t),{ahead:0,behind:0}}}};const we=a(r);var Te=class{async getSessionId(){try{if(!process.env.TMUX_PANE)return O(`TMUX_PANE not set, not in tmux session`),null;O(`Getting tmux session ID, TMUX_PANE: ${process.env.TMUX_PANE}`);let e=(await we(`tmux display-message -p '#S'`,{encoding:`utf8`,timeout:1e3})).stdout.trim();return O(`Tmux session ID: ${e||`empty`}`),e||null}catch(e){return O(`Error getting tmux session ID:`,e),null}}isInTmux(){return!!process.env.TMUX_PANE}};function k(){let e=[],t=process.env.CLAUDE_CONFIG_DIR;if(t&&t.split(`,`).forEach(t=>{let n=t.trim();c(n)&&e.push(n)}),e.length===0){let t=_(),n=f(t,`.config`,`claude`),r=f(t,`.claude`);c(n)&&e.push(n),c(r)&&e.push(r)}return e}async function A(e){let t=[];for(let n of e){let e=f(n,`projects`);if(c(e))try{let n=await x(e,{withFileTypes:!0});for(let r of n)if(r.isDirectory()){let n=p.join(e,r.name);t.push(n)}}catch(t){O(`Failed to read projects directory ${e}:`,t)}}return t}async function Ee(e){let t=await A(k());for(let n of t){let t=p.join(n,`${e}.jsonl`);if(c(t))return t}return null}async function De(e,t){let n=[],r=p.join(t,e,`subagents`);try{let t=(await x(r)).filter(e=>e.startsWith(`agent-`)&&e.endsWith(`.jsonl`));for(let i of t){let t=p.join(r,i);try{let r=(await b(t,`utf-8`)).split(`
5
5
  `)[0];r&&JSON.parse(r).sessionId===e&&n.push(t)}catch{O(`Failed to check agent file ${t}`)}}}catch(e){O(`Failed to read subagents directory ${r}:`,e)}return n}async function Oe(e){try{return(await ee(e)).mtime}catch{return null}}function ke(e){let t=e.message?.id||(typeof e.raw.message==`object`&&e.raw.message!==null&&`id`in e.raw.message?e.raw.message.id:void 0),n=`requestId`in e.raw?e.raw.requestId:void 0;return!t||!n?null:`${t}:${n}`}async function j(e){try{let t=(await ee(e)).size,n;return t>1048576?(O(`Using streaming parser for large file ${e} (${Math.round(t/1024)}KB)`),n=await je(e)):n=await Ae(e),O(`Parsed ${n.length} entries from ${e}`),n}catch(t){return O(`Failed to read file ${e}:`,t),[]}}async function Ae(e){let t=(await b(e,`utf-8`)).trim().split(`
6
- `).filter(e=>e.trim()),n=[];for(let e of t)try{let t=JSON.parse(e);if(!t.timestamp)continue;let r={timestamp:new Date(t.timestamp),message:t.message,costUSD:typeof t.costUSD==`number`?t.costUSD:void 0,isSidechain:t.isSidechain===!0,raw:t};n.push(r)}catch(e){O(`Failed to parse JSONL line: ${e}`);continue}return n}async function je(e){return new Promise((t,n)=>{let r=[],i=s(e,{encoding:`utf8`}),a=te({input:i,crlfDelay:1/0});a.on(`line`,e=>{let t=e.trim();if(t)try{let e=JSON.parse(t);if(!e.timestamp)return;let n={timestamp:new Date(e.timestamp),message:e.message,costUSD:typeof e.costUSD==`number`?e.costUSD:void 0,isSidechain:e.isSidechain===!0,raw:e};r.push(n)}catch(e){O(`Failed to parse JSONL line: ${e}`)}}),a.on(`close`,()=>{t(r)}),a.on(`error`,t=>{O(`Streaming parser error for ${e}:`,t),n(t)}),i.on(`error`,t=>{O(`File stream error for ${e}:`,t),n(t)})})}async function Me(e){try{let t=await Oe(e);return t?{filePath:e,mtime:t}:null}catch{return null}}async function Ne(e,t){try{let n=await x(e,{withFileTypes:!0}),r=n.filter(e=>!e.isDirectory()&&e.name.endsWith(`.jsonl`)).map(t=>Me(p.join(e,t.name))),i=n.filter(e=>e.isDirectory()).map(async t=>{let n=p.join(e,t.name,`subagents`);try{return(await x(n)).filter(e=>e.startsWith(`agent-`)&&e.endsWith(`.jsonl`)).map(e=>Me(p.join(n,e)))}catch{return[]}}),[a,o]=await Promise.all([Promise.all(r),Promise.all(i).then(e=>Promise.all(e.flat()))]);return[...a,...o].filter(e=>e!==null&&(!t||t(e.filePath,e.mtime)))}catch(t){return O(`Failed to read project directory ${e}:`,t),[]}}async function M(e,t,n=!1){let r=await A(k()),i=new Set,a=r.map(e=>Ne(e,t)),o=(await Promise.all(a)).flat().filter(e=>e!==null);n&&o.sort((e,t)=>t.mtime.getTime()-e.mtime.getTime());let s=o.map(e=>e.filePath),c=[],l=s.map(async t=>(await j(t)).filter(t=>!e||e(t))),u=await Promise.all(l);for(let e of u)c.push(...e);c.sort((e,t)=>e.timestamp.getTime()-t.timestamp.getTime());let d=[];for(let e of c){let t=ke(e);t&&i.has(t)||(t&&i.add(t),d.push(e))}return d}var N=class{static CACHE_DIR=u.join(_(),`.claude`,`powerline`);static USAGE_CACHE_DIR=u.join(this.CACHE_DIR,`usage`);static LOCKS_DIR=u.join(this.CACHE_DIR,`locks`);static isLocked(e){let t=u.join(this.LOCKS_DIR,e);if(!o.existsSync(t))return!1;try{let n=o.readFileSync(t,`utf-8`),r=parseInt(n.trim(),10);if(isNaN(r))return O(`Invalid PID in lock file ${e}, removing stale lock`),o.unlinkSync(t),!1;try{return process.kill(r,0),!0}catch(n){return n.code===`ESRCH`?(O(`Removing stale lock file ${e} for dead process ${r}`),o.unlinkSync(t),!1):(O(`Error checking process ${r} for lock ${e}:`,n),!0)}}catch(t){return O(`Error reading lock file ${e}:`,t),!0}}static async acquireLock(e,t=5e3){await this.ensureCacheDirectories();let n=u.join(this.LOCKS_DIR,e),r=Date.now(),i=String(process.pid);for(;Date.now()-r<t;)try{return await o.promises.writeFile(n,i,{flag:`wx`}),O(`Lock acquired for ${e}`),!0}catch(e){if(e.code===`EEXIST`)await y(50);else throw e}return O(`Failed to acquire lock for ${e} within ${t}ms`),!1}static async releaseLock(e){let t=u.join(this.LOCKS_DIR,e);try{await o.promises.unlink(t),O(`Lock released for ${e}`)}catch(t){t.code!==`ENOENT`&&O(`Error releasing lock for ${e}:`,t)}}static async ensureCacheDirectories(){try{await Promise.all([o.promises.mkdir(this.CACHE_DIR,{recursive:!0}),o.promises.mkdir(this.USAGE_CACHE_DIR,{recursive:!0}),o.promises.mkdir(this.LOCKS_DIR,{recursive:!0})])}catch(e){O(`Failed to create cache directories:`,e)}}static createProjectHash(e){return v(`md5`).update(e).digest(`hex`).substring(0,8)}static async getUsageCache(e,t){await this.ensureCacheDirectories();let n=u.join(this.USAGE_CACHE_DIR,`${e}.json`),r=`${e}.usage.lock`;for(let i=0;i<3;i++){if(this.isLocked(r)){O(`Cache for ${e} is locked, waiting...`),await y(75);continue}try{let r=await o.promises.readFile(n,`utf-8`),i=JSON.parse(r);return!t||i.timestamp>=t?(O(`[CACHE-HIT] ${e} disk cache: found`),this.deserializeDates(i.data)):(O(`${e} cache outdated: cache=${i.timestamp}, latest=${t}`),null)}catch(t){if(t.code===`ENOENT`)return O(`No shared ${e} usage cache found`),null;O(`Attempt ${i+1} failed to read ${e} cache: ${t.message}. Retrying...`),await y(75)}}return O(`Failed to read ${e} cache after 3 attempts.`),null}static deserializeDates(e){return Array.isArray(e)?e.map(e=>({...e,timestamp:new Date(e.timestamp)})):e}static async setUsageCache(e,t,n){let r=`${e}.usage.lock`;if(!await this.acquireLock(r)){O(`Could not acquire lock to set usage cache for ${e}`);return}try{await this.ensureCacheDirectories();let r=u.join(this.USAGE_CACHE_DIR,`${e}.json`),i={data:t,timestamp:n||Date.now()},a=JSON.stringify(i);await o.promises.writeFile(r,a,`utf-8`),O(`[CACHE-SET] ${e} disk cache stored`)}catch(t){O(`Failed to save ${e} usage cache:`,t)}finally{await this.releaseLock(r)}}static async getLatestTranscriptMtime(){try{let e=await A(k()),t=0;for(let n of e)try{let e=(await o.promises.readdir(n)).filter(e=>e.endsWith(`.jsonl`));for(let r of e){let e=await Oe(u.join(n,r));e&&e.getTime()>t&&(t=e.getTime())}}catch(e){O(`Failed to read project directory ${n}:`,e);continue}return t}catch(e){return O(`Failed to get latest transcript mtime:`,e),Date.now()}}};const P={"claude-3-haiku-20240307":{name:`Claude 3 Haiku`,input:.25,output:1.25,cache_write_5m:.3,cache_write_1h:.5,cache_read:.03},"claude-3-5-haiku-20241022":{name:`Claude 3.5 Haiku`,input:.8,output:4,cache_write_5m:1,cache_write_1h:1.6,cache_read:.08},"claude-3-5-haiku-latest":{name:`Claude 3.5 Haiku Latest`,input:1,output:5,cache_write_5m:1.25,cache_write_1h:2,cache_read:.1},"claude-haiku-4-5-20251001":{name:`Claude Haiku 4.5`,input:1,output:5,cache_write_5m:1.25,cache_write_1h:2,cache_read:.1},"claude-haiku-4-5":{name:`Claude Haiku 4.5`,input:1,output:5,cache_write_5m:1.25,cache_write_1h:2,cache_read:.1},"claude-3-opus-latest":{name:`Claude 3 Opus Latest`,input:15,output:75,cache_write_5m:18.75,cache_write_1h:30,cache_read:1.5},"claude-3-opus-20240229":{name:`Claude 3 Opus`,input:15,output:75,cache_write_5m:18.75,cache_write_1h:30,cache_read:1.5},"claude-3-5-sonnet-latest":{name:`Claude 3.5 Sonnet Latest`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-3-5-sonnet-20240620":{name:`Claude 3.5 Sonnet`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-3-5-sonnet-20241022":{name:`Claude 3.5 Sonnet`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-opus-4-20250514":{name:`Claude Opus 4`,input:15,output:75,cache_write_5m:18.75,cache_write_1h:30,cache_read:1.5},"claude-opus-4-1":{name:`Claude Opus 4.1`,input:15,output:75,cache_write_5m:18.75,cache_write_1h:30,cache_read:1.5},"claude-opus-4-1-20250805":{name:`Claude Opus 4.1`,input:15,output:75,cache_write_5m:18.75,cache_write_1h:30,cache_read:1.5},"claude-sonnet-4-20250514":{name:`Claude Sonnet 4`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-4-opus-20250514":{name:`Claude 4 Opus`,input:15,output:75,cache_write_5m:18.75,cache_write_1h:30,cache_read:1.5},"claude-4-sonnet-20250514":{name:`Claude 4 Sonnet`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-sonnet-4-5":{name:`Claude Sonnet 4.5`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-sonnet-4-5-20250929":{name:`Claude Sonnet 4.5`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-opus-4-5":{name:`Claude Opus 4.5`,input:5,output:25,cache_write_5m:6.25,cache_write_1h:10,cache_read:.5},"claude-opus-4-5-20251101":{name:`Claude Opus 4.5`,input:5,output:25,cache_write_5m:6.25,cache_write_1h:10,cache_read:.5},"claude-opus-4-6":{name:`Claude Opus 4.6`,input:5,output:25,cache_write_5m:6.25,cache_write_1h:10,cache_read:.5},"claude-opus-4-6-20260205":{name:`Claude Opus 4.6`,input:5,output:25,cache_write_5m:6.25,cache_write_1h:10,cache_read:.5},"claude-sonnet-4-6":{name:`Claude Sonnet 4.6`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-3-7-sonnet-latest":{name:`Claude 3.7 Sonnet Latest`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-3-7-sonnet-20250219":{name:`Claude 3.7 Sonnet`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3}};var F=class{static executionCache=null;static modelPricingCache=new Map;static GITHUB_PRICING_URL=`https://raw.githubusercontent.com/Owloops/claude-powerline/main/pricing.json`;static async loadDiskCache(){let e=Date.now()-1440*60*1e3;return await N.getUsageCache(`pricing`,e)}static async saveDiskCache(e){await N.setUsageCache(`pricing`,e)}static async fetchPricingData(){return new Promise(e=>{let t=new h(this.GITHUB_PRICING_URL),n=m({hostname:t.hostname,path:t.pathname,headers:{"User-Agent":`claude-powerline`,"Cache-Control":`no-cache`},timeout:5e3},t=>{if(t.statusCode!==200){O(`HTTP ${t.statusCode}: ${t.statusMessage}`),e(null);return}let r=``,i=0;t.on(`data`,t=>{if(i+=t.length,i>1048576){O(`Response too large`),n.destroy(),e(null);return}r+=t}),t.on(`end`,()=>{try{let t=JSON.parse(r),n=t._meta,i={};for(let[e,n]of Object.entries(t))e!==`_meta`&&(i[e]=n);this.validatePricingData(i)?(O(`Fetched fresh pricing from GitHub for ${Object.keys(i).length} models`),O(`Pricing last updated: ${n?.updated||`unknown`}`),e(i)):(O(`Invalid pricing data structure`),e(null))}catch(t){O(`Failed to parse JSON:`,t),e(null)}}),t.on(`error`,t=>{O(`Response error:`,t),e(null)})});n.on(`error`,t=>{O(`Request error:`,t),e(null)}),n.on(`timeout`,()=>{O(`Request timeout`),n.destroy(),e(null)}),n.end()})}static async getCurrentPricing(){if(this.executionCache!==null)return O(`[CACHE-HIT] Pricing execution cache: ${Object.keys(this.executionCache).length} models`),this.executionCache;let e=await this.loadDiskCache();if(e)return O(`[CACHE-HIT] Pricing disk cache: ${Object.keys(e).length} models`),this.executionCache=e,O(`[CACHE-SET] Pricing execution cache stored: ${Object.keys(e).length} models`),e;let t=await this.fetchPricingData();return t?(await this.saveDiskCache(t),O(`[CACHE-SET] Pricing disk cache stored: ${Object.keys(t).length} models`),this.executionCache=t,O(`[CACHE-SET] Pricing execution cache stored: ${Object.keys(t).length} models`),t):(O(`[CACHE-FALLBACK] Using offline pricing data: ${Object.keys(P).length} models`),this.executionCache=P,O(`[CACHE-SET] Pricing execution cache stored: ${Object.keys(P).length} models`),P)}static validatePricingData(e){if(!e||typeof e!=`object`)return!1;for(let[,t]of Object.entries(e)){if(!t||typeof t!=`object`)return!1;let e=t;if(typeof e.input!=`number`||typeof e.output!=`number`||typeof e.cache_read!=`number`)return!1}return!0}static async getModelPricing(e){if(this.modelPricingCache.has(e))return O(`[CACHE-HIT] Model pricing cache: ${e}`),this.modelPricingCache.get(e);let t=await this.getCurrentPricing(),n;return n=t[e]?t[e]:this.fuzzyMatchModel(e,t),this.modelPricingCache.set(e,n),O(`[CACHE-SET] Model pricing cache: ${e}`),n}static fuzzyMatchModel(e,t){let n=e.toLowerCase();for(let[e,r]of Object.entries(t))if(e.toLowerCase()===n)return r;for(let{pattern:e,fallback:r}of[{pattern:[`opus-4-6`,`claude-opus-4-6`],fallback:`claude-opus-4-6-20260205`},{pattern:[`opus-4-5`,`claude-opus-4-5`],fallback:`claude-opus-4-5-20251101`},{pattern:[`opus-4-1`,`claude-opus-4-1`],fallback:`claude-opus-4-1-20250805`},{pattern:[`opus-4`,`claude-opus-4`],fallback:`claude-opus-4-20250514`},{pattern:[`sonnet-4-6`,`sonnet-4.6`,`claude-sonnet-4-6`],fallback:`claude-sonnet-4-6`},{pattern:[`sonnet-4.5`,`4-5-sonnet`,`sonnet-4-5`],fallback:`claude-sonnet-4-5-20250929`},{pattern:[`sonnet-4`,`claude-sonnet-4`],fallback:`claude-sonnet-4-20250514`},{pattern:[`sonnet-3.7`,`3-7-sonnet`],fallback:`claude-3-7-sonnet-20250219`},{pattern:[`3-5-sonnet`,`sonnet-3.5`],fallback:`claude-3-5-sonnet-20241022`},{pattern:[`haiku-4.5`,`4-5-haiku`,`haiku-4-5`],fallback:`claude-haiku-4-5-20251001`},{pattern:[`3-5-haiku`,`haiku-3.5`],fallback:`claude-3-5-haiku-20241022`},{pattern:[`haiku`,`3-haiku`],fallback:`claude-3-haiku-20240307`},{pattern:[`opus`],fallback:`claude-opus-4-20250514`},{pattern:[`sonnet`],fallback:`claude-3-5-sonnet-20241022`}])if(e.some(e=>n.includes(e))&&t[r])return t[r];return t[`claude-3-5-sonnet-20241022`]||{name:`${e} (Unknown Model)`,input:3,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3,output:15}}static async calculateCostForEntry(e){let t=e.message?.usage;if(!t)return 0;let n=this.extractModelId(e),r=await this.getModelPricing(n),i=t.input_tokens||0,a=t.output_tokens||0,o=t.cache_creation_input_tokens||0,s=t.cache_read_input_tokens||0,c=i/1e6*r.input,l=a/1e6*r.output,u=s/1e6*r.cache_read,d=o/1e6*r.cache_write_5m;return c+l+d+u}static extractModelId(e){if(e.model&&typeof e.model==`string`)return e.model;let t=e.message;if(t?.model){let e=t.model;return typeof e==`string`?e:e?.id||`claude-3-5-sonnet-20241022`}return e.model_id&&typeof e.model_id==`string`?e.model_id:`claude-3-5-sonnet-20241022`}};function Pe(e){return{timestamp:e.timestamp.toISOString(),message:{usage:{input_tokens:e.message?.usage?.input_tokens||0,output_tokens:e.message?.usage?.output_tokens||0,cache_creation_input_tokens:e.message?.usage?.cache_creation_input_tokens,cache_read_input_tokens:e.message?.usage?.cache_read_input_tokens}},costUSD:e.costUSD}}var Fe=class{async getSessionUsage(e){try{let t=await Ee(e);if(!t)return O(`No transcript found for session: ${e}`),null;O(`Found transcript at: ${t}`);let n=await j(t),r=await De(e,d(t));O(`Found ${r.length} agent transcripts for session`);for(let e of r){let t=await j(e);n.push(...t)}if(n.length===0)return{totalCost:0,entries:[]};let i=[],a=0;for(let e of n)if(e.message?.usage){let t=Pe(e);if(t.costUSD!==void 0)a+=t.costUSD;else{let n=await F.calculateCostForEntry(e.raw);t.costUSD=n,a+=n}i.push(t)}return O(`Parsed ${i.length} usage entries, total cost: $${a.toFixed(4)}`),{totalCost:a,entries:i}}catch(t){return O(`Error reading session usage for ${e}:`,t),null}}calculateTokenBreakdown(e){return e.reduce((e,t)=>({input:e.input+(t.message.usage.input_tokens||0),output:e.output+(t.message.usage.output_tokens||0),cacheCreation:e.cacheCreation+(t.message.usage.cache_creation_input_tokens||0),cacheRead:e.cacheRead+(t.message.usage.cache_read_input_tokens||0)}),{input:0,output:0,cacheCreation:0,cacheRead:0})}async getSessionInfo(e,t){let n=await this.getSessionUsage(e);if(!n||n.entries.length===0)return{cost:null,calculatedCost:null,officialCost:null,tokens:null,tokenBreakdown:null};let r=this.calculateTokenBreakdown(n.entries),i=r.input+r.output+r.cacheCreation+r.cacheRead,a=n.totalCost,o=t?.cost?.total_cost_usd??null;return{cost:a??o,calculatedCost:a,officialCost:o,tokens:i,tokenBreakdown:r}}},Ie=class{sessionProvider=new Fe;async getUsageInfo(e,t){try{return O(`Starting usage info retrieval for session: ${e}`),{session:await this.sessionProvider.getSessionInfo(e,t)}}catch(t){return O(`Error getting usage info for session ${e}:`,t),{session:{cost:null,calculatedCost:null,officialCost:null,tokens:null,tokenBreakdown:null}}}}},Le=class{thresholds={LOW:50,MEDIUM:80};config;constructor(e){this.config=e}getContextUsageThresholds(){return this.thresholds}getContextLimit(e){let t=this.config.modelContextLimits||{default:2e5};return t[this.getModelType(e)]||t.default||2e5}getModelType(e){let t=e.toLowerCase();return t.includes(`sonnet`)?`sonnet`:t.includes(`opus`)?`opus`:`default`}calculatePercentages(e,t,n=33e3){let r=Math.min(100,Math.max(0,Math.round(e/t*100))),i=Math.max(1,t-n),a=Math.min(100,Math.max(0,Math.round(e/i*100)));return{percentage:r,usablePercentage:a,contextLeftPercentage:Math.max(0,100-a),usableTokens:i}}calculateContextFromHookData(e,t=33e3){let n=e.context_window?.current_usage;if(!n)return O(`No current_usage in hook data, falling back to transcript parsing`),null;let r=e.context_window?.context_window_size||2e5,i=(n.input_tokens||0)+(n.cache_creation_input_tokens||0)+(n.cache_read_input_tokens||0);return O(`Native current_usage: input=${n.input_tokens}, cache_create=${n.cache_creation_input_tokens}, cache_read=${n.cache_read_input_tokens}, total=${i} (limit: ${r})`),{totalTokens:i,maxTokens:r,...this.calculatePercentages(i,r,t)}}async calculateContextTokensFromTranscript(e,t,n=33e3){try{O(`Calculating context tokens from transcript: ${e}`);try{if(!l(e,`utf-8`))return O(`Transcript file is empty`),null}catch{return O(`Could not read transcript file`),null}let r=await j(e);if(r.length===0)return O(`No entries in transcript`),null;let i=null;for(let e=r.length-1;e>=0;e--){let t=r[e];if(t&&t.message?.usage?.input_tokens&&t.isSidechain!==!0){i=t,O(`Context segment: Found most recent entry at ${t.timestamp.toISOString()}, stopping search`);break}}if(i?.message?.usage){let e=i.message.usage,r=(e.input_tokens||0)+(e.cache_read_input_tokens||0)+(e.cache_creation_input_tokens||0),a=t?this.getContextLimit(t):2e5;return O(`Most recent main chain context: ${r} tokens (limit: ${a})`),{totalTokens:r,maxTokens:a,...this.calculatePercentages(r,a,n)}}return O(`No main chain entries with usage data found`),null}catch(e){return O(`Error reading transcript: ${e instanceof Error?e.message:String(e)}`),null}}async getContextInfo(e,t=33e3){return this.calculateContextFromHookData(e,t)||this.calculateContextTokensFromTranscript(e.transcript_path,e.model?.id,t)}},Re=class{async loadTranscriptEntries(e){try{let t=await Ee(e);if(!t)return O(`No transcript found for session: ${e}`),[];O(`Loading transcript from: ${t}`);let n=(await b(t,`utf-8`)).trim().split(`
6
+ `).filter(e=>e.trim()),n=[];for(let e of t)try{let t=JSON.parse(e);if(!t.timestamp)continue;let r={timestamp:new Date(t.timestamp),message:t.message,costUSD:typeof t.costUSD==`number`?t.costUSD:void 0,isSidechain:t.isSidechain===!0,raw:t};n.push(r)}catch(e){O(`Failed to parse JSONL line: ${e}`);continue}return n}async function je(e){return new Promise((t,n)=>{let r=[],i=s(e,{encoding:`utf8`}),a=te({input:i,crlfDelay:1/0});a.on(`line`,e=>{let t=e.trim();if(t)try{let e=JSON.parse(t);if(!e.timestamp)return;let n={timestamp:new Date(e.timestamp),message:e.message,costUSD:typeof e.costUSD==`number`?e.costUSD:void 0,isSidechain:e.isSidechain===!0,raw:e};r.push(n)}catch(e){O(`Failed to parse JSONL line: ${e}`)}}),a.on(`close`,()=>{t(r)}),a.on(`error`,t=>{O(`Streaming parser error for ${e}:`,t),n(t)}),i.on(`error`,t=>{O(`File stream error for ${e}:`,t),n(t)})})}async function Me(e){try{let t=await Oe(e);return t?{filePath:e,mtime:t}:null}catch{return null}}async function Ne(e,t){try{let n=await x(e,{withFileTypes:!0}),r=n.filter(e=>!e.isDirectory()&&e.name.endsWith(`.jsonl`)).map(t=>Me(p.join(e,t.name))),i=n.filter(e=>e.isDirectory()).map(async t=>{let n=p.join(e,t.name,`subagents`);try{return(await x(n)).filter(e=>e.startsWith(`agent-`)&&e.endsWith(`.jsonl`)).map(e=>Me(p.join(n,e)))}catch{return[]}}),[a,o]=await Promise.all([Promise.all(r),Promise.all(i).then(e=>Promise.all(e.flat()))]);return[...a,...o].filter(e=>e!==null&&(!t||t(e.filePath,e.mtime)))}catch(t){return O(`Failed to read project directory ${e}:`,t),[]}}async function M(e,t,n=!1){let r=await A(k()),i=new Set,a=r.map(e=>Ne(e,t)),o=(await Promise.all(a)).flat().filter(e=>e!==null);n&&o.sort((e,t)=>t.mtime.getTime()-e.mtime.getTime());let s=o.map(e=>e.filePath),c=[],l=s.map(async t=>(await j(t)).filter(t=>!e||e(t))),u=await Promise.all(l);for(let e of u)c.push(...e);c.sort((e,t)=>e.timestamp.getTime()-t.timestamp.getTime());let d=[];for(let e of c){let t=ke(e);t&&i.has(t)||(t&&i.add(t),d.push(e))}return d}var N=class{static CACHE_DIR=u.join(_(),`.claude`,`powerline`);static USAGE_CACHE_DIR=u.join(this.CACHE_DIR,`usage`);static LOCKS_DIR=u.join(this.CACHE_DIR,`locks`);static isLocked(e){let t=u.join(this.LOCKS_DIR,e);if(!o.existsSync(t))return!1;try{let n=o.readFileSync(t,`utf-8`),r=parseInt(n.trim(),10);if(isNaN(r))return O(`Invalid PID in lock file ${e}, removing stale lock`),o.unlinkSync(t),!1;try{return process.kill(r,0),!0}catch(n){return n.code===`ESRCH`?(O(`Removing stale lock file ${e} for dead process ${r}`),o.unlinkSync(t),!1):(O(`Error checking process ${r} for lock ${e}:`,n),!0)}}catch(t){return O(`Error reading lock file ${e}:`,t),!0}}static async acquireLock(e,t=5e3){await this.ensureCacheDirectories();let n=u.join(this.LOCKS_DIR,e),r=Date.now(),i=String(process.pid);for(;Date.now()-r<t;)try{return await o.promises.writeFile(n,i,{flag:`wx`}),O(`Lock acquired for ${e}`),!0}catch(e){if(e.code===`EEXIST`)await y(50);else throw e}return O(`Failed to acquire lock for ${e} within ${t}ms`),!1}static async releaseLock(e){let t=u.join(this.LOCKS_DIR,e);try{await o.promises.unlink(t),O(`Lock released for ${e}`)}catch(t){t.code!==`ENOENT`&&O(`Error releasing lock for ${e}:`,t)}}static async ensureCacheDirectories(){try{await Promise.all([o.promises.mkdir(this.CACHE_DIR,{recursive:!0}),o.promises.mkdir(this.USAGE_CACHE_DIR,{recursive:!0}),o.promises.mkdir(this.LOCKS_DIR,{recursive:!0})])}catch(e){O(`Failed to create cache directories:`,e)}}static createProjectHash(e){return v(`md5`).update(e).digest(`hex`).substring(0,8)}static async getUsageCache(e,t){await this.ensureCacheDirectories();let n=u.join(this.USAGE_CACHE_DIR,`${e}.json`),r=`${e}.usage.lock`;for(let i=0;i<3;i++){if(this.isLocked(r)){O(`Cache for ${e} is locked, waiting...`),await y(75);continue}try{let r=await o.promises.readFile(n,`utf-8`),i=JSON.parse(r);return!t||i.timestamp>=t?(O(`[CACHE-HIT] ${e} disk cache: found`),this.deserializeDates(i.data)):(O(`${e} cache outdated: cache=${i.timestamp}, latest=${t}`),null)}catch(t){if(t.code===`ENOENT`)return O(`No shared ${e} usage cache found`),null;O(`Attempt ${i+1} failed to read ${e} cache: ${t.message}. Retrying...`),await y(75)}}return O(`Failed to read ${e} cache after 3 attempts.`),null}static deserializeDates(e){return Array.isArray(e)?e.map(e=>({...e,timestamp:new Date(e.timestamp)})):e}static async setUsageCache(e,t,n){let r=`${e}.usage.lock`;if(!await this.acquireLock(r)){O(`Could not acquire lock to set usage cache for ${e}`);return}try{await this.ensureCacheDirectories();let r=u.join(this.USAGE_CACHE_DIR,`${e}.json`),i={data:t,timestamp:n||Date.now()},a=JSON.stringify(i);await o.promises.writeFile(r,a,`utf-8`),O(`[CACHE-SET] ${e} disk cache stored`)}catch(t){O(`Failed to save ${e} usage cache:`,t)}finally{await this.releaseLock(r)}}static async getLatestTranscriptMtime(){try{let e=await A(k()),t=0;for(let n of e)try{let e=(await o.promises.readdir(n)).filter(e=>e.endsWith(`.jsonl`));for(let r of e){let e=await Oe(u.join(n,r));e&&e.getTime()>t&&(t=e.getTime())}}catch(e){O(`Failed to read project directory ${n}:`,e);continue}return t}catch(e){return O(`Failed to get latest transcript mtime:`,e),Date.now()}}};const P={"claude-haiku-4-5-20251001":{name:`Claude Haiku 4.5`,input:1,output:5,cache_write_5m:1.25,cache_write_1h:2,cache_read:.1},"claude-haiku-4-5":{name:`Claude Haiku 4.5`,input:1,output:5,cache_write_5m:1.25,cache_write_1h:2,cache_read:.1},"claude-opus-4-20250514":{name:`Claude Opus 4`,input:15,output:75,cache_write_5m:18.75,cache_write_1h:30,cache_read:1.5},"claude-opus-4-1":{name:`Claude Opus 4.1`,input:15,output:75,cache_write_5m:18.75,cache_write_1h:30,cache_read:1.5},"claude-opus-4-1-20250805":{name:`Claude Opus 4.1`,input:15,output:75,cache_write_5m:18.75,cache_write_1h:30,cache_read:1.5},"claude-sonnet-4-20250514":{name:`Claude Sonnet 4`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-4-opus-20250514":{name:`Claude 4 Opus`,input:15,output:75,cache_write_5m:18.75,cache_write_1h:30,cache_read:1.5},"claude-4-sonnet-20250514":{name:`Claude 4 Sonnet`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-sonnet-4-5":{name:`Claude Sonnet 4.5`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-sonnet-4-5-20250929":{name:`Claude Sonnet 4.5`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3},"claude-opus-4-5":{name:`Claude Opus 4.5`,input:5,output:25,cache_write_5m:6.25,cache_write_1h:10,cache_read:.5},"claude-opus-4-5-20251101":{name:`Claude Opus 4.5`,input:5,output:25,cache_write_5m:6.25,cache_write_1h:10,cache_read:.5},"claude-opus-4-6":{name:`Claude Opus 4.6`,input:5,output:25,cache_write_5m:6.25,cache_write_1h:10,cache_read:.5},"claude-opus-4-6-20260205":{name:`Claude Opus 4.6`,input:5,output:25,cache_write_5m:6.25,cache_write_1h:10,cache_read:.5},"claude-sonnet-4-6":{name:`Claude Sonnet 4.6`,input:3,output:15,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3}};var F=class{static executionCache=null;static modelPricingCache=new Map;static GITHUB_PRICING_URL=`https://raw.githubusercontent.com/Owloops/claude-powerline/main/pricing.json`;static async loadDiskCache(){let e=Date.now()-1440*60*1e3;return await N.getUsageCache(`pricing`,e)}static async saveDiskCache(e){await N.setUsageCache(`pricing`,e)}static async fetchPricingData(){return new Promise(e=>{let t=new h(this.GITHUB_PRICING_URL),n=m({hostname:t.hostname,path:t.pathname,headers:{"User-Agent":`claude-powerline`,"Cache-Control":`no-cache`},timeout:5e3},t=>{if(t.statusCode!==200){O(`HTTP ${t.statusCode}: ${t.statusMessage}`),e(null);return}let r=``,i=0;t.on(`data`,t=>{if(i+=t.length,i>1048576){O(`Response too large`),n.destroy(),e(null);return}r+=t}),t.on(`end`,()=>{try{let t=JSON.parse(r),n=t._meta,i={};for(let[e,n]of Object.entries(t))e!==`_meta`&&(i[e]=n);this.validatePricingData(i)?(O(`Fetched fresh pricing from GitHub for ${Object.keys(i).length} models`),O(`Pricing last updated: ${n?.updated||`unknown`}`),e(i)):(O(`Invalid pricing data structure`),e(null))}catch(t){O(`Failed to parse JSON:`,t),e(null)}}),t.on(`error`,t=>{O(`Response error:`,t),e(null)})});n.on(`error`,t=>{O(`Request error:`,t),e(null)}),n.on(`timeout`,()=>{O(`Request timeout`),n.destroy(),e(null)}),n.end()})}static async getCurrentPricing(){if(this.executionCache!==null)return O(`[CACHE-HIT] Pricing execution cache: ${Object.keys(this.executionCache).length} models`),this.executionCache;let e=await this.loadDiskCache();if(e)return O(`[CACHE-HIT] Pricing disk cache: ${Object.keys(e).length} models`),this.executionCache=e,O(`[CACHE-SET] Pricing execution cache stored: ${Object.keys(e).length} models`),e;let t=await this.fetchPricingData();return t?(await this.saveDiskCache(t),O(`[CACHE-SET] Pricing disk cache stored: ${Object.keys(t).length} models`),this.executionCache=t,O(`[CACHE-SET] Pricing execution cache stored: ${Object.keys(t).length} models`),t):(O(`[CACHE-FALLBACK] Using offline pricing data: ${Object.keys(P).length} models`),this.executionCache=P,O(`[CACHE-SET] Pricing execution cache stored: ${Object.keys(P).length} models`),P)}static validatePricingData(e){if(!e||typeof e!=`object`)return!1;for(let[,t]of Object.entries(e)){if(!t||typeof t!=`object`)return!1;let e=t;if(typeof e.input!=`number`||typeof e.output!=`number`||typeof e.cache_read!=`number`)return!1}return!0}static async getModelPricing(e){if(this.modelPricingCache.has(e))return O(`[CACHE-HIT] Model pricing cache: ${e}`),this.modelPricingCache.get(e);let t=await this.getCurrentPricing(),n;return n=t[e]?t[e]:this.fuzzyMatchModel(e,t),this.modelPricingCache.set(e,n),O(`[CACHE-SET] Model pricing cache: ${e}`),n}static fuzzyMatchModel(e,t){let n=e.toLowerCase();for(let[e,r]of Object.entries(t))if(e.toLowerCase()===n)return r;for(let{pattern:e,fallback:r}of[{pattern:[`opus-4-6`,`claude-opus-4-6`],fallback:`claude-opus-4-6-20260205`},{pattern:[`opus-4-5`,`claude-opus-4-5`],fallback:`claude-opus-4-5-20251101`},{pattern:[`opus-4-1`,`claude-opus-4-1`],fallback:`claude-opus-4-1-20250805`},{pattern:[`opus-4`,`claude-opus-4`],fallback:`claude-opus-4-20250514`},{pattern:[`sonnet-4-6`,`sonnet-4.6`,`claude-sonnet-4-6`],fallback:`claude-sonnet-4-6`},{pattern:[`sonnet-4.5`,`4-5-sonnet`,`sonnet-4-5`],fallback:`claude-sonnet-4-5-20250929`},{pattern:[`sonnet-4`,`claude-sonnet-4`],fallback:`claude-sonnet-4-20250514`},{pattern:[`haiku-4.5`,`4-5-haiku`,`haiku-4-5`],fallback:`claude-haiku-4-5-20251001`},{pattern:[`haiku`],fallback:`claude-haiku-4-5-20251001`},{pattern:[`opus`],fallback:`claude-opus-4-20250514`},{pattern:[`sonnet`],fallback:`claude-sonnet-4-5-20250929`}])if(e.some(e=>n.includes(e))&&t[r])return t[r];return t[`claude-sonnet-4-5-20250929`]||{name:`${e} (Unknown Model)`,input:3,cache_write_5m:3.75,cache_write_1h:6,cache_read:.3,output:15}}static async calculateCostForEntry(e){let t=e.message?.usage;if(!t)return 0;let n=this.extractModelId(e),r=await this.getModelPricing(n),i=t.input_tokens||0,a=t.output_tokens||0,o=t.cache_creation_input_tokens||0,s=t.cache_read_input_tokens||0,c=i/1e6*r.input,l=a/1e6*r.output,u=s/1e6*r.cache_read,d=o/1e6*r.cache_write_5m;return c+l+d+u}static extractModelId(e){if(e.model&&typeof e.model==`string`)return e.model;let t=e.message;if(t?.model){let e=t.model;return typeof e==`string`?e:e?.id||`claude-sonnet-4-5-20250929`}return e.model_id&&typeof e.model_id==`string`?e.model_id:`claude-sonnet-4-5-20250929`}};function Pe(e){return{timestamp:e.timestamp.toISOString(),message:{usage:{input_tokens:e.message?.usage?.input_tokens||0,output_tokens:e.message?.usage?.output_tokens||0,cache_creation_input_tokens:e.message?.usage?.cache_creation_input_tokens,cache_read_input_tokens:e.message?.usage?.cache_read_input_tokens}},costUSD:e.costUSD}}var Fe=class{async getSessionUsage(e){try{let t=await Ee(e);if(!t)return O(`No transcript found for session: ${e}`),null;O(`Found transcript at: ${t}`);let n=await j(t),r=await De(e,d(t));O(`Found ${r.length} agent transcripts for session`);for(let e of r){let t=await j(e);n.push(...t)}if(n.length===0)return{totalCost:0,entries:[]};let i=[],a=0;for(let e of n)if(e.message?.usage){let t=Pe(e);if(t.costUSD!==void 0)a+=t.costUSD;else{let n=await F.calculateCostForEntry(e.raw);t.costUSD=n,a+=n}i.push(t)}return O(`Parsed ${i.length} usage entries, total cost: $${a.toFixed(4)}`),{totalCost:a,entries:i}}catch(t){return O(`Error reading session usage for ${e}:`,t),null}}calculateTokenBreakdown(e){return e.reduce((e,t)=>({input:e.input+(t.message.usage.input_tokens||0),output:e.output+(t.message.usage.output_tokens||0),cacheCreation:e.cacheCreation+(t.message.usage.cache_creation_input_tokens||0),cacheRead:e.cacheRead+(t.message.usage.cache_read_input_tokens||0)}),{input:0,output:0,cacheCreation:0,cacheRead:0})}async getSessionInfo(e,t){let n=await this.getSessionUsage(e);if(!n||n.entries.length===0)return{cost:null,calculatedCost:null,officialCost:null,tokens:null,tokenBreakdown:null};let r=this.calculateTokenBreakdown(n.entries),i=r.input+r.output+r.cacheCreation+r.cacheRead,a=n.totalCost,o=t?.cost?.total_cost_usd??null;return{cost:a??o,calculatedCost:a,officialCost:o,tokens:i,tokenBreakdown:r}}},Ie=class{sessionProvider=new Fe;async getUsageInfo(e,t){try{return O(`Starting usage info retrieval for session: ${e}`),{session:await this.sessionProvider.getSessionInfo(e,t)}}catch(t){return O(`Error getting usage info for session ${e}:`,t),{session:{cost:null,calculatedCost:null,officialCost:null,tokens:null,tokenBreakdown:null}}}}},Le=class{thresholds={LOW:50,MEDIUM:80};config;constructor(e){this.config=e}getContextUsageThresholds(){return this.thresholds}getContextLimit(e){let t=this.config.modelContextLimits||{default:2e5};return t[this.getModelType(e)]||t.default||2e5}getModelType(e){let t=e.toLowerCase();return t.includes(`sonnet`)?`sonnet`:t.includes(`opus`)?`opus`:`default`}calculatePercentages(e,t,n=33e3){let r=Math.min(100,Math.max(0,Math.round(e/t*100))),i=Math.max(1,t-n),a=Math.min(100,Math.max(0,Math.round(e/i*100)));return{percentage:r,usablePercentage:a,contextLeftPercentage:Math.max(0,100-a),usableTokens:i}}calculateContextFromHookData(e,t=33e3){let n=e.context_window?.current_usage;if(!n)return O(`No current_usage in hook data, falling back to transcript parsing`),null;let r=e.context_window?.context_window_size||2e5,i=(n.input_tokens||0)+(n.cache_creation_input_tokens||0)+(n.cache_read_input_tokens||0);return O(`Native current_usage: input=${n.input_tokens}, cache_create=${n.cache_creation_input_tokens}, cache_read=${n.cache_read_input_tokens}, total=${i} (limit: ${r})`),{totalTokens:i,maxTokens:r,...this.calculatePercentages(i,r,t)}}async calculateContextTokensFromTranscript(e,t,n=33e3){try{O(`Calculating context tokens from transcript: ${e}`);try{if(!l(e,`utf-8`))return O(`Transcript file is empty`),null}catch{return O(`Could not read transcript file`),null}let r=await j(e);if(r.length===0)return O(`No entries in transcript`),null;let i=null;for(let e=r.length-1;e>=0;e--){let t=r[e];if(t&&t.message?.usage?.input_tokens&&t.isSidechain!==!0){i=t,O(`Context segment: Found most recent entry at ${t.timestamp.toISOString()}, stopping search`);break}}if(i?.message?.usage){let e=i.message.usage,r=(e.input_tokens||0)+(e.cache_read_input_tokens||0)+(e.cache_creation_input_tokens||0),a=t?this.getContextLimit(t):2e5;return O(`Most recent main chain context: ${r} tokens (limit: ${a})`),{totalTokens:r,maxTokens:a,...this.calculatePercentages(r,a,n)}}return O(`No main chain entries with usage data found`),null}catch(e){return O(`Error reading transcript: ${e instanceof Error?e.message:String(e)}`),null}}async getContextInfo(e,t=33e3){return this.calculateContextFromHookData(e,t)||this.calculateContextTokensFromTranscript(e.transcript_path,e.model?.id,t)}},Re=class{async loadTranscriptEntries(e){try{let t=await Ee(e);if(!t)return O(`No transcript found for session: ${e}`),[];O(`Loading transcript from: ${t}`);let n=(await b(t,`utf-8`)).trim().split(`
7
7
  `).filter(e=>e.trim()),r=[];for(let e of n)try{let t=JSON.parse(e);if(t.isSidechain===!0)continue;r.push(t)}catch(e){O(`Failed to parse JSONL line: ${e}`);continue}return O(`Loaded ${r.length} transcript entries`),r}catch(t){return O(`Error loading transcript for ${e}:`,t),[]}}calculateMessageCount(e){return e.filter(e=>{let t=e.type||e.message?.role||e.message?.type,n=e.type===`user`&&e.message?.content?.[0]?.type===`tool_result`;return t===`user`&&!n}).length}calculateLastResponseTime(e){if(e.length===0)return null;let t=e.slice(-20),n=null,r=null;for(let e of t)if(e.timestamp)try{let t=new Date(e.timestamp),i=e.type||e.message?.role||e.message?.type,a=e.type===`user`&&e.message?.content?.[0]?.type===`tool_result`;if(i===`user`&&!a)n=t;else if(i===`assistant`&&n){let e=(t.getTime()-n.getTime())/1e3;e>.1&&e<300&&(r=e)}}catch{continue}return r}async getMetricsInfo(e,t){try{if(O(`Getting metrics from hook data for session: ${e}`),!t.cost)return O(`No cost data available in hook data`),{responseTime:null,lastResponseTime:null,sessionDuration:null,messageCount:null,linesAdded:null,linesRemoved:null};let n=await this.loadTranscriptEntries(e),r=this.calculateMessageCount(n),i=this.calculateLastResponseTime(n);return{responseTime:t.cost.total_api_duration_ms/1e3,lastResponseTime:i,sessionDuration:t.cost.total_duration_ms/1e3,messageCount:r,linesAdded:t.cost.total_lines_added,linesRemoved:t.cost.total_lines_removed}}catch(t){return O(`Error getting metrics from hook data for session ${e}:`,t),{responseTime:null,lastResponseTime:null,sessionDuration:null,messageCount:null,linesAdded:null,linesRemoved:null}}}};function I(e){return e===null?`$0.00`:e<.01?`<$0.01`:`$${e.toFixed(2)}`}function L(e){return e===null||e===0?`0 tokens`:e>=1e6?`${(e/1e6).toFixed(1)}M tokens`:e>=1e3?`${(e/1e3).toFixed(1)}K tokens`:`${e} tokens`}function ze(e){if(!e)return`0 tokens`;let t=[];if(e.input>0&&t.push(`${L(e.input).replace(` tokens`,``)} in`),e.output>0&&t.push(`${L(e.output).replace(` tokens`,``)} out`),e.cacheCreation>0||e.cacheRead>0){let n=e.cacheCreation+e.cacheRead;t.push(`${L(n).replace(` tokens`,``)} cached`)}return t.length>0?t.join(` + `):`0 tokens`}function Be(e){return e<60?`${e}s`:e<3600?`${Math.floor(e/60)}m`:e<86400?`${Math.floor(e/3600)}h`:e<604800?`${Math.floor(e/86400)}d`:`${Math.floor(e/604800)}w`}function Ve(e){return e<60?`${e.toFixed(0)}s`:e<3600?`${(e/60).toFixed(0)}m`:e<86400?`${(e/3600).toFixed(1)}h`:`${(e/86400).toFixed(1)}d`}const He=/^(?:(?:global|apac|au|eu|us|us-east-\d|us-west-\d|eu-west-\d|eu-central-\d)\.)?(?:anthropic\.|azure_ai\/|bedrock\/|vertex_ai\/)?claude-(?:(?<family>opus|sonnet|haiku)-(?<newMajor>\d+)(?:-(?<newMinor>\d))?|(?<oldMajor>\d+)(?:-(?<oldMinor>\d))?-(?<oldFamily>opus|sonnet|haiku))(?:[-@]\d{8})?(?:-v\d+:\d+)?(?:-latest)?$/i;function Ue(e){if(!e)return`Claude`;let t=e.trim().match(He);if(!t?.groups)return e;let{family:n,newMajor:r,newMinor:i,oldMajor:a,oldMinor:o,oldFamily:s}=t.groups,c=n||s,l=r||a,u=i||o;return c&&l?`${c.charAt(0).toUpperCase()+c.slice(1).toLowerCase()} ${u?`${l}.${u}`:l}`:e}function We(e){let t=e.includes(`/`)?`/`:`\\`,n=e.split(t);return n.map((e,t)=>t===n.length-1||e===`~`||e===``?e:e.charAt(0)).join(t)}function Ge(e){return e<60?`${e.toFixed(1)}s`:`${(e/60).toFixed(1)}m`}function Ke(e){let t=Math.floor(e/60),n=e%60;return t>0?`${t}h ${n}m left`:`${n}m left`}function qe(e,t){return!t||t<=0||e<0?null:Math.min(100,e/t*100)}function R(e,t,n=80){let r=qe(e,t);if(r===null)return{percentage:null,isWarning:!1,displayText:``};let i=`${r.toFixed(0)}%`,a=r>=n,o=``;return o=a?` !${i}`:r>=50?` +${i}`:` ${i}`,{percentage:r,isWarning:a,displayText:o}}const Je={ball:{filled:`─`,empty:`─`,marker:`●`},blocks:{filled:`█`,empty:`░`},"blocks-line":{filled:`█`,empty:`─`},capped:{filled:`━`,empty:`┄`,cap:`╸`},dots:{filled:`●`,empty:`○`},filled:{filled:`■`,empty:`□`},geometric:{filled:`▰`,empty:`▱`},line:{filled:`━`,empty:`┄`},squares:{filled:`◼`,empty:`◻`}};var Ye=class{constructor(e,t){this.config=e,this.symbols=t}renderDirectory(e,t,n){let r=e.workspace?.current_dir||e.cwd||`/`,i=e.workspace?.project_dir,a=n?.style??(n?.showBasename?`basename`:`full`);if(a===`basename`)return{text:u.basename(r)||`root`,bgColor:t.modeBg,fgColor:t.modeFg};let o=process.env.HOME||process.env.USERPROFILE,s=r,c=i;o&&(r.startsWith(o)&&(s=r.replace(o,`~`)),i&&i.startsWith(o)&&(c=i.replace(o,`~`)));let l=this.getDisplayDirectoryName(s,c);return a===`fish`&&(l=We(l)),{text:l,bgColor:t.modeBg,fgColor:t.modeFg}}renderGit(e,t,n){if(!e)return null;let r=[];if(n?.showRepoName&&e.repoName&&(r.push(e.repoName),e.isWorktree&&r.push(this.symbols.git_worktree)),n?.showOperation&&e.operation&&r.push(`[${e.operation}]`),r.push(`${this.symbols.branch} ${e.branch}`),n?.showTag&&e.tag&&r.push(`${this.symbols.git_tag} ${e.tag}`),n?.showSha&&e.sha&&r.push(`${this.symbols.git_sha} ${e.sha}`),n?.showAheadBehind!==!1&&(e.ahead>0&&e.behind>0?r.push(`${this.symbols.git_ahead}${e.ahead}${this.symbols.git_behind}${e.behind}`):e.ahead>0?r.push(`${this.symbols.git_ahead}${e.ahead}`):e.behind>0&&r.push(`${this.symbols.git_behind}${e.behind}`)),n?.showWorkingTree){let t=[];e.staged&&e.staged>0&&t.push(`+${e.staged}`),e.unstaged&&e.unstaged>0&&t.push(`~${e.unstaged}`),e.untracked&&e.untracked>0&&t.push(`?${e.untracked}`),e.conflicts&&e.conflicts>0&&t.push(`!${e.conflicts}`),t.length>0&&r.push(`(${t.join(` `)})`)}if(n?.showUpstream&&e.upstream&&r.push(`${this.symbols.git_upstream}${e.upstream}`),n?.showStashCount&&e.stashCount&&e.stashCount>0&&r.push(`${this.symbols.git_stash} ${e.stashCount}`),n?.showTimeSinceCommit&&e.timeSinceCommit!==void 0){let t=Be(e.timeSinceCommit);r.push(`${this.symbols.git_time} ${t}`)}let i=this.symbols.git_clean;return e.status===`conflicts`?i=this.symbols.git_conflicts:e.status===`dirty`&&(i=this.symbols.git_dirty),r.push(i),{text:r.join(` `),bgColor:t.gitBg,fgColor:t.gitFg}}renderModel(e,t){let n=Ue(e.model?.display_name||`Claude`);return{text:`${this.symbols.model} ${n}`,bgColor:t.modelBg,fgColor:t.modelFg}}renderSession(e,t,n){let r=n?.type||`cost`,i=n?.costSource,a=this.config.budget?.session,o=this.formatUsageWithBudget(i===`calculated`?e.session.calculatedCost:i===`official`?e.session.officialCost:e.session.cost,e.session.tokens,e.session.tokenBreakdown,r,a?.amount,a?.warningThreshold||80,a?.type);return{text:`${this.symbols.session_cost} ${o}`,bgColor:t.sessionBg,fgColor:t.sessionFg}}renderSessionId(e,t,n){return{text:n?.showIdLabel===!1?e:`${this.symbols.session_id} ${e}`,bgColor:t.sessionBg,fgColor:t.sessionFg}}renderTmux(e,t){return e?{text:`tmux:${e}`,bgColor:t.tmuxBg,fgColor:t.tmuxFg}:{text:`tmux:none`,bgColor:t.tmuxBg,fgColor:t.tmuxFg}}renderContext(e,t,n){let r=n?.displayStyle??`text`,i=r===`text`?`remaining`:`used`,a=n?.percentageMode??i,o=r===`bar`?{filled:this.symbols.bar_filled,empty:this.symbols.bar_empty}:Je[r]??null,s=a===`remaining`?`100%`:`0%`;if(!e)return o?{text:`${o.empty.repeat(10)} ${s}`,bgColor:t.contextBg,fgColor:t.contextFg}:{text:`${this.symbols.context_time} 0 (${s})`,bgColor:t.contextBg,fgColor:t.contextFg};let c=t.contextBg,l=t.contextFg;e.contextLeftPercentage<=20?(c=t.contextCriticalBg,l=t.contextCriticalFg):e.contextLeftPercentage<=40&&(c=t.contextWarningBg,l=t.contextWarningFg);let u=a===`remaining`?e.contextLeftPercentage:e.usablePercentage,d=Math.round(e.usablePercentage/100*10),f=10-d;if(o){let t=this.buildBar(o,d,f,10);return{text:n?.showPercentageOnly?`${t} ${u}%`:`${t} ${e.totalTokens.toLocaleString()} (${u}%)`,bgColor:c,fgColor:l}}return{text:n?.showPercentageOnly?`${this.symbols.context_time} ${u}%`:`${this.symbols.context_time} ${e.totalTokens.toLocaleString()} (${u}%)`,bgColor:c,fgColor:l}}buildBar(e,t,n,r){if(e.marker){let n=Math.min(t,r-1);return e.filled.repeat(n)+e.marker+e.empty.repeat(r-n-1)}return e.cap?t===0?e.cap+e.empty.repeat(r-1):t>=r?e.filled.repeat(r):e.filled.repeat(t-1)+e.cap+e.empty.repeat(n):e.filled.repeat(t)+e.empty.repeat(n)}renderMetrics(e,t,n,r){if(!e)return{text:`${this.symbols.metrics_response} new`,bgColor:t.metricsBg,fgColor:t.metricsFg};let i=[];if(r?.showLastResponseTime&&e.lastResponseTime!==null){let t=e.lastResponseTime<60?`${e.lastResponseTime.toFixed(1)}s`:`${(e.lastResponseTime/60).toFixed(1)}m`;i.push(`${this.symbols.metrics_last_response} ${t}`)}if(r?.showResponseTime!==!1&&e.responseTime!==null){let t=e.responseTime<60?`${e.responseTime.toFixed(1)}s`:`${(e.responseTime/60).toFixed(1)}m`;i.push(`${this.symbols.metrics_response} ${t}`)}if(r?.showDuration!==!1&&e.sessionDuration!==null){let t=Ve(e.sessionDuration);i.push(`${this.symbols.metrics_duration} ${t}`)}return r?.showMessageCount!==!1&&e.messageCount!==null&&i.push(`${this.symbols.metrics_messages} ${e.messageCount}`),r?.showLinesAdded!==!1&&e.linesAdded!==null&&e.linesAdded>0&&i.push(`${this.symbols.metrics_lines_added} ${e.linesAdded}`),r?.showLinesRemoved!==!1&&e.linesRemoved!==null&&e.linesRemoved>0&&i.push(`${this.symbols.metrics_lines_removed} ${e.linesRemoved}`),i.length===0?{text:`${this.symbols.metrics_response} active`,bgColor:t.metricsBg,fgColor:t.metricsFg}:{text:i.join(` `),bgColor:t.metricsBg,fgColor:t.metricsFg}}renderBlock(e,t,n){let r;if(e.cost===null&&e.tokens===null)r=`No active block`;else{let t=n?.type||`cost`,i=n?.burnType,a=this.config.budget?.block,o=e.timeRemaining===null?null:(()=>{let t=Math.floor(e.timeRemaining/60),n=e.timeRemaining%60;return t>0?`${t}h ${n}m`:`${n}m`})(),s;switch(t){case`cost`:s=this.formatUsageWithBudget(e.cost,null,null,`cost`,a?.amount,a?.warningThreshold,a?.type);break;case`tokens`:s=this.formatUsageWithBudget(null,e.tokens,null,`tokens`,a?.amount,a?.warningThreshold,a?.type);break;case`weighted`:let t=a?.type===`tokens`?a.amount:void 0,n=L(e.weightedTokens);s=t&&e.weightedTokens!==null?`${n}${R(e.weightedTokens,t,a?.warningThreshold||80).displayText}`:`${n} (weighted)`;break;case`both`:s=this.formatUsageWithBudget(e.cost,e.tokens,null,`both`,a?.amount,a?.warningThreshold,a?.type);break;case`time`:s=o||`N/A`;break;default:s=this.formatUsageWithBudget(e.cost,null,null,`cost`,a?.amount,a?.warningThreshold,a?.type)}let c=``;if(i&&i!==`none`)switch(i){case`cost`:c=` | ${e.burnRate===null?`N/A`:e.burnRate<1?`${(e.burnRate*100).toFixed(0)}¢/h`:`$${e.burnRate.toFixed(2)}/h`}`;break;case`tokens`:c=` | ${e.tokenBurnRate===null?`N/A`:`${L(Math.round(e.tokenBurnRate))}/h`}`;break;case`both`:c=` | ${e.burnRate===null?`N/A`:e.burnRate<1?`${(e.burnRate*100).toFixed(0)}¢/h`:`$${e.burnRate.toFixed(2)}/h`} / ${e.tokenBurnRate===null?`N/A`:`${L(Math.round(e.tokenBurnRate))}/h`}`;break}r=t===`time`?s:o?`${s}${c} (${o} left)`:`${s}${c}`}return{text:`${this.symbols.block_cost} ${r}`,bgColor:t.blockBg,fgColor:t.blockFg}}renderToday(e,t,n=`cost`){let r=this.config.budget?.today;return{text:`${this.symbols.today_cost} ${this.formatUsageWithBudget(e.cost,e.tokens,e.tokenBreakdown,n,r?.amount,r?.warningThreshold,r?.type)}`,bgColor:t.todayBg,fgColor:t.todayFg}}getDisplayDirectoryName(e,t){return e.startsWith(`~`)?e:t&&t!==e&&e.startsWith(t)?e.slice(t.length+1)||u.basename(t)||`project`:e}formatUsageDisplay(e,t,n,r){switch(r){case`cost`:return I(e);case`tokens`:return L(t);case`both`:return`${I(e)} (${L(t)})`;case`breakdown`:return ze(n);default:return I(e)}}formatUsageWithBudget(e,t,n,r,i,a=80,o){let s=this.formatUsageDisplay(e,t,n,r);if(i&&i>0){let n=null;if(o===`tokens`&&t!==null?n=t:(o===`cost`&&e!==null||!o&&e!==null)&&(n=e),n!==null)return s+R(n,i,a).displayText}return s}renderVersion(e,t,n){return e.version?{text:`${this.symbols.version} v${e.version}`,bgColor:t.versionBg,fgColor:t.versionFg}:null}renderEnv(e,t){let n=process.env[t.variable];if(!n)return null;let r=t.prefix??t.variable;return{text:r?`${this.symbols.env} ${r}: ${n}`:`${this.symbols.env} ${n}`,bgColor:e.envBg,fgColor:e.envFg}}};function Xe(e){return e.includes(`opus`)?5:(e.includes(`sonnet`)||e.includes(`haiku`),1)}function Ze(e){return{timestamp:e.timestamp,usage:{inputTokens:e.message?.usage?.input_tokens||0,outputTokens:e.message?.usage?.output_tokens||0,cacheCreationInputTokens:e.message?.usage?.cache_creation_input_tokens||0,cacheReadInputTokens:e.message?.usage?.cache_read_input_tokens||0},costUSD:e.costUSD||0,model:e.message?.model||`unknown`}}var Qe=class{sessionDurationHours=5;floorToHour(e){let t=new Date(e);return t.setUTCMinutes(0,0,0),t}identifySessionBlocks(e){if(e.length===0)return[];let t=this.sessionDurationHours*60*60*1e3,n=[],r=[...e].sort((e,t)=>e.timestamp.getTime()-t.timestamp.getTime()),i=null,a=[];for(let e of r){let r=e.timestamp;if(i==null)i=this.floorToHour(r),a=[e];else{let o=r.getTime()-i.getTime(),s=a[a.length-1];if(s==null)continue;let c=s.timestamp,l=r.getTime()-c.getTime();o>t||l>t?(n.push(a),i=this.floorToHour(r),a=[e]):a.push(e)}}return i!=null&&a.length>0&&n.push(a),n}createBlockInfo(e,t){let n=new Date,r=this.sessionDurationHours*60*60*1e3,i=new Date(e.getTime()+r),a=t[t.length-1],o=a==null?e:a.timestamp;return{block:t,isActive:n.getTime()-o.getTime()<r&&n<i}}findActiveBlock(e){for(let t=e.length-1;t>=0;t--){let n=e[t];if(!n||n.length===0)continue;let r=n[0];if(!r)continue;let i=this.floorToHour(r.timestamp),a=this.createBlockInfo(i,n);if(a.isActive)return a.block}return null}async loadUsageEntries(){try{O(`Block segment: Loading entries for dynamic session blocks`);let e=new Date;e.setDate(e.getDate()-1);let t=await M(void 0,(t,n)=>n>=e,!0),n=[];for(let e of t)if(e.message?.usage){let t=Ze(e);!t.costUSD&&e.raw&&(t.costUSD=await F.calculateCostForEntry(e.raw)),n.push(t)}let r=this.identifySessionBlocks(n);O(`Block segment: Found ${r.length} session blocks`);let i=this.findActiveBlock(r),a=[];if(i&&i.length>0){O(`Block segment: Found active block with ${i.length} entries`);let e=i[0],t=i[i.length-1];e&&t&&O(`Block segment: Active block from ${e.timestamp.toISOString()} to ${t.timestamp.toISOString()}`),a=i}else O(`Block segment: No active block found`),a=[];return a}catch(e){return O(`Error loading block entries:`,e),[]}}async getActiveBlockInfo(){try{let e=await this.loadUsageEntries();if(e.length===0)return O(`Block segment: No entries in current block`),{cost:null,tokens:null,weightedTokens:null,timeRemaining:null,burnRate:null,tokenBurnRate:null};let t=e.reduce((e,t)=>e+t.costUSD,0),n=e.reduce((e,t)=>e+t.usage.inputTokens+t.usage.outputTokens+t.usage.cacheCreationInputTokens+t.usage.cacheReadInputTokens,0),r=e.reduce((e,t)=>e+(t.usage.inputTokens+t.usage.outputTokens+t.usage.cacheCreationInputTokens+t.usage.cacheReadInputTokens)*Xe(t.model),0),i=new Date,a=null;if(e.length>0){let t=e[0];if(t){let e=this.sessionDurationHours*60*60*1e3,n=this.floorToHour(t.timestamp),r=new Date(n.getTime()+e);a=Math.max(0,Math.round((r.getTime()-i.getTime())/(1e3*60)))}}let o=null,s=null;if(e.length>=1&&(t>0||n>0)){let r=e.map(e=>e.timestamp).sort((e,t)=>e.getTime()-t.getTime()),i=r[0],a=r[r.length-1];if(i&&a){let e=(a.getTime()-i.getTime())/(1e3*60);e>0&&(t>0&&(o=t/e*60),n>0&&(s=n/e*60))}}return O(`Block segment: $${t.toFixed(2)}, ${n} tokens, ${a}m remaining, burn rate: ${o?`$`+o.toFixed(2)+`/hr`:`N/A`}`),{cost:t,tokens:n,weightedTokens:r,timeRemaining:a,burnRate:o,tokenBurnRate:s}}catch(e){return O(`Error getting active block info:`,e),{cost:null,tokens:null,weightedTokens:null,timeRemaining:null,burnRate:null,tokenBurnRate:null}}}};function z(e){return`${e.getFullYear()}-${String(e.getMonth()+1).padStart(2,`0`)}-${String(e.getDate()).padStart(2,`0`)}`}function $e(e){return e.inputTokens+e.outputTokens+e.cacheCreationInputTokens+e.cacheReadInputTokens}function et(e){return{timestamp:e.timestamp,usage:{inputTokens:e.message?.usage?.input_tokens||0,outputTokens:e.message?.usage?.output_tokens||0,cacheCreationInputTokens:e.message?.usage?.cache_creation_input_tokens||0,cacheReadInputTokens:e.message?.usage?.cache_read_input_tokens||0},costUSD:e.costUSD||0,model:e.message?.model||`unknown`}}var tt=class{async loadTodayEntries(){let e=z(new Date);O(`Today segment: Loading entries for date ${e}`);let t=await N.getLatestTranscriptMtime(),n=await N.getUsageCache(`today`,t);if(n)return O(`Using shared today usage cache`),n;let r=new Date;r.setDate(r.getDate()-1),r.setHours(0,0,0,0);let i=(e,t)=>t>=r,a=new Date;a.setHours(0,0,0,0);let o=await M(e=>e.timestamp>=a,i,!0),s=[],c=0;for(let t of o)if(z(t.timestamp)===e&&t.message?.usage){let e=et(t);!e.costUSD&&t.raw&&(e.costUSD=await F.calculateCostForEntry(t.raw)),s.push(e),c++}return O(`Today segment: Found ${c} entries for today (${e})`),await N.setUsageCache(`today`,s,t),s}async getTodayEntries(){try{return await this.loadTodayEntries()}catch(e){return O(`Error loading today's entries:`,e),[]}}async getTodayInfo(){try{let e=await this.getTodayEntries();if(e.length===0)return{cost:null,tokens:null,tokenBreakdown:null,date:z(new Date)};let t=e.reduce((e,t)=>e+t.costUSD,0),n=e.reduce((e,t)=>e+$e(t.usage),0),r=e.reduce((e,t)=>({input:e.input+t.usage.inputTokens,output:e.output+t.usage.outputTokens,cacheCreation:e.cacheCreation+t.usage.cacheCreationInputTokens,cacheRead:e.cacheRead+t.usage.cacheReadInputTokens}),{input:0,output:0,cacheCreation:0,cacheRead:0});return O(`Today segment: $${t.toFixed(2)}, ${n} tokens total`),{cost:t,tokens:n,tokenBreakdown:r,date:z(new Date)}}catch(e){return O(`Error getting today's info:`,e),{cost:null,tokens:null,tokenBreakdown:null,date:z(new Date)}}}};const nt={right:``,left_rounded:``,right_rounded:``,branch:`⎇`,model:`✱`,git_clean:`✓`,git_dirty:`●`,git_conflicts:`⚠`,git_ahead:`↑`,git_behind:`↓`,git_worktree:`⧉`,git_tag:`⌂`,git_sha:`♯`,git_upstream:`→`,git_stash:`⧇`,git_time:`◷`,session_cost:`§`,block_cost:`◱`,today_cost:`☉`,context_time:`◔`,metrics_response:`⧖`,metrics_last_response:`Δ`,metrics_duration:`⧗`,metrics_messages:`◆`,metrics_lines_added:`+`,metrics_lines_removed:`-`,metrics_burn:`↗`,version:`◈`,bar_filled:`▪`,bar_empty:`▫`,env:`⚙`,session_id:`⌗`},rt={topLeft:`╭`,topRight:`╮`,bottomLeft:`╰`,bottomRight:`╯`,horizontal:`─`,vertical:`│`,teeLeft:`├`,teeRight:`┤`},it={topLeft:`+`,topRight:`+`,bottomLeft:`+`,bottomRight:`+`,horizontal:`-`,vertical:`|`,teeLeft:`+`,teeRight:`+`},at={right:``,left_rounded:``,right_rounded:``,branch:`~`,model:`M`,git_clean:`=`,git_dirty:`*`,git_conflicts:`!`,git_ahead:`^`,git_behind:`v`,git_worktree:`W`,git_tag:`T`,git_sha:`#`,git_upstream:`>>`,git_stash:`S`,git_time:`@`,session_cost:`S`,block_cost:`B`,today_cost:`D`,context_time:`C`,metrics_response:`R`,metrics_last_response:`L`,metrics_duration:`T`,metrics_messages:`#`,metrics_lines_added:`+`,metrics_lines_removed:`-`,metrics_burn:`~/h`,version:`V`,bar_filled:`=`,bar_empty:`-`,env:`$`,session_id:`#`},ot=RegExp(`\x1B\\[[0-9;]*m`,`g`),st=/^[a-zA-Z0-9/]+$/;function ct(){if(process.platform===`win32`)return null;let e=process.pid.toString();for(let t=0;t<10;t++)try{let t=i(`ps -o ppid=,tty= -p ${e}`,{encoding:`utf8`,stdio:[`pipe`,`pipe`,`ignore`]}).trim().split(/\s+/),n=t[0],r=t[1];if(r&&r!==`?`&&r!==`??`&&st.test(r))return r;if(!n||n===`1`||n===`0`)break;e=n}catch{break}return null}function lt(){try{let e=i(`mode con`,{encoding:`utf8`,stdio:[`pipe`,`pipe`,`ignore`],windowsHide:!0}).match(/Columns:\s*(\d+)/i);if(e?.[1]){let t=parseInt(e[1],10);if(!isNaN(t)&&t>0)return t}}catch{}return null}function ut(){let e=ct();if(e)try{let t=i(`stty size < /dev/${e}`,{encoding:`utf8`,stdio:[`pipe`,`pipe`,`ignore`],shell:`/bin/sh`}).trim().split(` `)[1];if(t){let e=parseInt(t,10);if(!isNaN(e)&&e>0)return e}}catch{}try{let e=i(`tput cols 2>/dev/null`,{encoding:`utf8`,stdio:[`pipe`,`pipe`,`ignore`]}).trim(),t=parseInt(e,10);if(!isNaN(t)&&t>0)return t}catch{}return null}function dt(){let e=e=>Math.floor(e*.7),t=process.env.COLUMNS;if(t){let n=parseInt(t,10);if(!isNaN(n)&&n>0)return e(n)}if(process.stdout.columns&&process.stdout.columns>0)return e(process.stdout.columns);if(process.platform===`win32`){let t=lt();if(t)return e(t)}let n=ut();return n?e(n):null}function B(e){return e.replace(ot,``)}function V(e){return B(e).length}function H(e,t,n){return t?`${t}${e}${n}`:e}function ft(e,t){let n=V(e);return n>=t?e:e+` `.repeat(t-n)}const pt=RegExp(`(\x1B\\[[0-9;]*m)`);function mt(e,t){if(B(e).length<=t)return e;let n=0,r=``,i=e.split(pt);for(let e of i){if(e.startsWith(`\x1B`)){r+=e;continue}for(let i of e){if(n>=t-1)return r+=`…`,r;r+=i,n++}}return r}function U(e,t,n){let r=n-2,i=ft(mt(t,r),r);return e.vertical+` `+i+` `+e.vertical}function W(e,t){return e.teeLeft+e.horizontal.repeat(t)+e.teeRight}function ht(e,t){return e.bottomLeft+e.horizontal.repeat(t)+e.bottomRight}function gt(e,t){if(e.length===0)return``;if(e.length===1)return e[0]??``;let n=e.map(e=>V(e)),r=t-n.reduce((e,t)=>e+t,0),i=Math.max(2,Math.floor(r/(e.length-1))),a=Array(e.length);a[e.length-1]=n[e.length-1]??0;for(let t=e.length-2;t>=0;t--)a[t]=(a[t+1]??0)+(n[t]??0);let o=e[0]??``,s=n[0]??0;for(let r=1;r<e.length;r++){let c=t-s-(a[r]??0)-(e.length-1-r)*2,l=Math.max(2,Math.min(i,c));o+=` `.repeat(l)+(e[r]??``),s+=l+(n[r]??0)}return o}function G(e,t,n){if(!t)return e;if(!e)return t;let r=V(e),i=V(t),a=n-r-i;return a<2?`${e} ${t}`:e+` `.repeat(a)+t}function _t(e,t,n){let r=` ${Ue(e.hookData.model?.display_name||`Claude`).toLowerCase()} `,i=n-1-r.length-18;if(i<2){let e=n-1-r.length;return t.topLeft+t.horizontal+r+t.horizontal.repeat(Math.max(0,e))+t.topRight}return t.topLeft+t.horizontal+r+t.horizontal.repeat(i)+` claude-powerline `+t.topRight}function vt(e,t,n,r,i){let a=Math.min(Math.floor(t*.45),40);if(!e.contextInfo)return null;let o=e.contextInfo.usablePercentage,s=Math.round(o/100*a),c=a-s,l=n.bar_filled.repeat(s)+n.bar_empty.repeat(c),u=e.contextInfo.totalTokens>=1e3?`${(e.contextInfo.totalTokens/1e3).toFixed(0)}k`:`${e.contextInfo.totalTokens}`,d=e.contextInfo.maxTokens>=1e3?`${(e.contextInfo.maxTokens/1e3).toFixed(0)}k`:`${e.contextInfo.maxTokens}`,f=i.contextFg;return o>=80?f=i.contextCriticalFg:o>=60&&(f=i.contextWarningFg),H(`${l} ${o}% ${u}/${d}`,f,r)}function yt(e){let t=e.workspace?.current_dir||e.cwd||`/`,n=process.env.HOME||process.env.USERPROFILE;return n&&t.startsWith(n)?t.replace(n,`~`):t}function bt(e,t,n,r,i){let a=[];e.blockInfo&&a.push(H(J(e.blockInfo,t,n),i.blockFg,r)),e.usageInfo&&a.push(H(Y(e.usageInfo,t,n),i.sessionFg,r)),e.todayInfo&&a.push(H(St(e.todayInfo,t,n),i.todayFg,r));let o=xt(e,t);return o.length>0&&a.push(H(o.join(` · `),i.metricsFg,r)),a}function xt(e,t){let n=[];return e.metricsInfo&&(e.metricsInfo.sessionDuration!==null&&e.metricsInfo.sessionDuration>0&&n.push(`${t.metrics_duration} ${Ve(e.metricsInfo.sessionDuration)}`),e.metricsInfo.messageCount!==null&&e.metricsInfo.messageCount>0&&n.push(`${t.metrics_messages} ${e.metricsInfo.messageCount}`)),n}function K(e,t,n,r){let i=[];if(e.gitInfo){let a=`${t.branch} ${e.gitInfo.branch}`;e.gitInfo.status===`conflicts`?a+=` ${t.git_conflicts}`:e.gitInfo.status===`dirty`?a+=` ${t.git_dirty}`:a+=` ${t.git_clean}`,e.gitInfo.ahead>0&&(a+=` ${t.git_ahead}${e.gitInfo.ahead}`),e.gitInfo.behind>0&&(a+=` ${t.git_behind}${e.gitInfo.behind}`);let o=[];e.gitInfo.staged&&e.gitInfo.staged>0&&o.push(`+${e.gitInfo.staged}`),e.gitInfo.unstaged&&e.gitInfo.unstaged>0&&o.push(`~${e.gitInfo.unstaged}`),e.gitInfo.untracked&&e.gitInfo.untracked>0&&o.push(`?${e.gitInfo.untracked}`),o.length>0&&(a+=` (${o.join(` `)})`),i.push(H(a,r.gitFg,n))}let a=We(yt(e.hookData));return i.push(H(a,r.modeFg,n)),i}function q(e,t,n,r,i){let a=[];if(e.hookData.version&&a.push(H(`${t.version} v${e.hookData.version}`,i.versionFg,r)),e.tmuxSessionId&&a.push(H(`tmux:${e.tmuxSessionId}`,i.tmuxFg,r)),e.metricsInfo){let n=[];if(e.metricsInfo.responseTime!==null&&!isNaN(e.metricsInfo.responseTime)&&e.metricsInfo.responseTime>0&&n.push(`${t.metrics_response} ${Ge(e.metricsInfo.responseTime)}`),e.metricsInfo.linesAdded!==null&&e.metricsInfo.linesAdded>0&&n.push(`${t.metrics_lines_added}${e.metricsInfo.linesAdded}`),e.metricsInfo.linesRemoved!==null&&e.metricsInfo.linesRemoved>0&&n.push(`${t.metrics_lines_removed}${e.metricsInfo.linesRemoved}`),e.blockInfo?.burnRate!==null&&e.blockInfo?.burnRate!==void 0&&e.blockInfo.burnRate>0){let r=e.blockInfo.burnRate<1?`${(e.blockInfo.burnRate*100).toFixed(0)}c/h`:`$${e.blockInfo.burnRate.toFixed(2)}/h`;n.push(`${t.metrics_burn} ${r}`)}n.length>0&&a.push(H(n.join(` · `),i.metricsFg,r))}let o=n.display.lines.map(e=>e.segments.env).find(e=>e?.enabled);if(o&&o.variable){let e=process.env[o.variable];if(e){let t=o.prefix??o.variable;a.push(H(t?`${t}:${e}`:e,i.envFg,r))}}return a}function J(e,t,n){let r=I(e.cost),i=`${t.block_cost} ${r}`;e.timeRemaining!==null&&(i+=` · ${Ke(e.timeRemaining)}`);let a=n.budget?.block;if(a?.amount&&e.cost!==null){let t=R(e.cost,a.amount,a.warningThreshold||80);i+=t.displayText}return i}function Y(e,t,n){let r=I(e.session.cost),i=e.session.tokens,a=i!==null&&i>0?L(i).replace(` tokens`,``):null,o=`${t.session_cost} ${r}`;a&&(o+=` · ${a}`);let s=n.budget?.session;if(s?.amount&&e.session.cost!==null){let t=R(e.session.cost,s.amount,s.warningThreshold||80);o+=t.displayText}return o}function St(e,t,n){let r=I(e.cost),i=`${t.today_cost} ${r} today`,a=n.budget?.today;if(a?.amount&&e.cost!==null){let t=R(e.cost,a.amount,a.warningThreshold||80);i+=t.displayText}return i}function Ct(e){let{lines:t,data:n,box:r,contentWidth:i,innerWidth:a,sym:o,config:s,reset:c,colors:l}=e,u=bt(n,o,s,c,l);u.length>0&&t.push(U(r,gt(u,i),a))}function wt(e){let{lines:t,data:n,box:r,contentWidth:i,innerWidth:a,sym:o,config:s,reset:c,colors:l}=e,u=K(n,o,c,l),d=q(n,o,s,c,l),f=u.join(` `),p=d.join(` · `);(f||p)&&(t.push(W(r,a)),t.push(U(r,G(f,p,i),a)))}function Tt(e){let{lines:t,data:n,box:r,contentWidth:i,innerWidth:a,sym:o,config:s,reset:c,colors:l}=e,u=[],d=[];n.blockInfo&&u.push(H(J(n.blockInfo,o,s),l.blockFg,c)),n.todayInfo&&u.push(H(St(n.todayInfo,o,s),l.todayFg,c)),n.usageInfo&&d.push(H(Y(n.usageInfo,o,s),l.sessionFg,c));let f=xt(n,o);f.length>0&&d.push(H(f.join(` · `),l.metricsFg,c)),u.length>0&&t.push(U(r,G(u[0]??``,u[1]??``,i),a)),d.length>0&&t.push(U(r,G(d[0]??``,d[1]??``,i),a))}function Et(e){let{lines:t,data:n,box:r,contentWidth:i,innerWidth:a,sym:o,config:s,reset:c,colors:l}=e,u=K(n,o,c,l);u.length>0&&(t.push(W(r,a)),t.push(U(r,G(u[0]??``,u[1]??``,i),a)));let d=q(n,o,s,c,l);d.length>0&&(t.push(W(r,a)),t.push(U(r,d.join(` · `),a)))}function Dt(e){let{lines:t,data:n,box:r,contentWidth:i,innerWidth:a,sym:o,config:s,reset:c,colors:l}=e;n.blockInfo&&t.push(U(r,H(J(n.blockInfo,o,s),l.blockFg,c),a));let u=[];n.usageInfo&&u.push(H(`${o.session_cost} ${I(n.usageInfo.session.cost)}`,l.sessionFg,c)),n.todayInfo&&u.push(H(`${o.today_cost} ${I(n.todayInfo.cost)} today`,l.todayFg,c)),u.length>0&&t.push(U(r,G(u[0]??``,u[1]??``,i),a))}function Ot(e){let{lines:t,data:n,box:r,contentWidth:i,innerWidth:a,sym:o,config:s,reset:c,colors:l}=e,u=K(n,o,c,l);u.length>0&&(t.push(W(r,a)),t.push(U(r,G(u[0]??``,u[1]??``,i),a)));let d=q(n,o,s,c,l);d.length>0&&t.push(U(r,d.join(` · `),a))}function kt(e){return e>=80?`wide`:e>=55?`medium`:`narrow`}function At(e){return e&&e>0?Math.max(32,e):80}function jt(e,t,n,r,i){let a=(i.display.charset||`unicode`)===`text`?at:nt,o=e.colors,s=At(r),c=s-2,l=c-2,u=kt(s),d=[];d.push(_t(e,t,c));let f=vt(e,l,a,n,o);f&&d.push(U(t,f,c));let p={lines:d,data:e,box:t,contentWidth:l,innerWidth:c,sym:a,config:i,reset:n,colors:o};return u===`wide`?(Ct(p),wt(p)):u===`medium`?(Tt(p),Et(p)):(Dt(p),Ot(p)),d.push(ht(t,c)),`\x1B[?2026h`+d.join(`
8
8
  `)+`\x1B[?2026l`}var Mt=class{symbols;_usageProvider;_blockProvider;_todayProvider;_contextProvider;_gitService;_tmuxService;_metricsProvider;_segmentRenderer;constructor(e){this.config=e,this.symbols=this.initializeSymbols()}get usageProvider(){return this._usageProvider||=new Ie,this._usageProvider}get blockProvider(){return this._blockProvider||=new Qe,this._blockProvider}get todayProvider(){return this._todayProvider||=new tt,this._todayProvider}get contextProvider(){return this._contextProvider||=new Le(this.config),this._contextProvider}get gitService(){return this._gitService||=new Ce,this._gitService}get tmuxService(){return this._tmuxService||=new Te,this._tmuxService}get metricsProvider(){return this._metricsProvider||=new Re,this._metricsProvider}get segmentRenderer(){return this._segmentRenderer||=new Ye(this.config,this.symbols),this._segmentRenderer}needsSegmentInfo(e){return this.config.display.lines.some(t=>t.segments[e]?.enabled)}async generateStatusline(e){if(this.config.display.style===`tui`)return this.generateTuiStatusline(e);let t=this.needsSegmentInfo(`session`)?await this.usageProvider.getUsageInfo(e.session_id,e):null,n=this.needsSegmentInfo(`block`)?await this.blockProvider.getActiveBlockInfo():null,r=this.needsSegmentInfo(`today`)?await this.todayProvider.getTodayInfo():null,i=this.config.display.lines.map(e=>e.segments.context).find(e=>e?.enabled)?.autocompactBuffer??33e3,a=this.needsSegmentInfo(`context`)?await this.contextProvider.getContextInfo(e,i):null,o=this.needsSegmentInfo(`metrics`)?await this.metricsProvider.getMetricsInfo(e.session_id,e):null;return this.config.display.autoWrap?this.generateAutoWrapStatusline(e,t,n,r,a,o):(await Promise.all(this.config.display.lines.map(i=>this.renderLine(i,e,t,n,r,a,o)))).filter(e=>e.length>0).join(`
9
9
  `)}async generateAutoWrapStatusline(e,t,n,r,i,a){let o=this.getThemeColors(),s=e.workspace?.current_dir||e.cwd||`/`,c=dt(),l=[];for(let u of this.config.display.lines){let d=Object.entries(u.segments).filter(([e,t])=>t?.enabled).map(([e,t])=>({type:e,config:t})),f=[];for(let c of d){let l=await this.renderSegment(c,e,t,n,r,i,a,o,s);l&&f.push({type:c.type,text:l.text,bgColor:l.bgColor,fgColor:l.fgColor})}if(f.length===0)continue;if(!c||c<=0){l.push(this.buildLineFromSegments(f,o));continue}let p=[],m=0;for(let e of f){let t=this.calculateSegmentWidth(e,p.length===0);p.length>0&&m+t>c&&(l.push(this.buildLineFromSegments(p,o)),p=[],m=0),p.push(e),m+=t}p.length>0&&l.push(this.buildLineFromSegments(p,o))}return l.join(`
@@ -42,4 +42,4 @@ Add to ~/.claude/settings.json:
42
42
  Run with --help for more options.
43
43
 
44
44
  To test output manually:
45
- echo '{"session_id":"test-session","workspace":{"project_dir":"/path/to/project"},"model":{"id":"claude-3-5-sonnet","display_name":"Claude"}}' | claude-powerline --style=powerline`),e.exit(1)),O(`Working directory: ${e.cwd()}`),O(`Process args:`,e.argv);let n=await t(e.stdin);O(`Received hook data:`,JSON.stringify(n,null,2)),n||(console.error(`Error: No input data received from stdin`),Ht(),e.exit(1));let r=n.workspace?.project_dir,i=await new Mt(Vt(e.argv,r)).generateStatusline(n);console.log(i)}catch(t){let n=t instanceof Error?t.message:String(t);console.error(`Error generating statusline:`,n),e.exit(1)}}Ut();export{};
45
+ echo '{"session_id":"test-session","workspace":{"project_dir":"/path/to/project"},"model":{"id":"claude-sonnet-4-5","display_name":"Claude"}}' | claude-powerline --style=powerline`),e.exit(1)),O(`Working directory: ${e.cwd()}`),O(`Process args:`,e.argv);let n=await t(e.stdin);O(`Received hook data:`,JSON.stringify(n,null,2)),n||(console.error(`Error: No input data received from stdin`),Ht(),e.exit(1));let r=n.workspace?.project_dir,i=await new Mt(Vt(e.argv,r)).generateStatusline(n);console.log(i)}catch(t){let n=t instanceof Error?t.message:String(t);console.error(`Error generating statusline:`,n),e.exit(1)}}Ut();export{};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owloops/claude-powerline",
3
- "version": "1.21.1",
3
+ "version": "1.21.2",
4
4
  "description": "Beautiful vim-style powerline statusline for Claude Code with real-time usage tracking, git integration, and custom themes",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",