@chatpanel/events 0.143.0 → 0.150.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.
package/sources.js CHANGED
@@ -1 +1 @@
1
- const h=["device","trusted","any"];function v(n,r){const t=h.indexOf(n),e=h.indexOf(r);return t<0?h.includes(r)?r:"device":e<0?n:h[Math.min(t,e)]}const x=Object.freeze([{pattern:"localhost",label:"This machine, by name"},{pattern:"127.0.0.0/8",label:"Loopback"},{pattern:"::1",label:"Loopback (IPv6)"},{pattern:"0.0.0.0/8",label:"This network"},{pattern:"10.0.0.0/8",label:"Private network"},{pattern:"172.16.0.0/12",label:"Private network"},{pattern:"192.168.0.0/16",label:"Private network (home / office)"},{pattern:"100.64.0.0/10",label:"Carrier-grade NAT — used by some corporate networks"},{pattern:"169.254.0.0/16",label:"Link-local — never routes off this segment"},{pattern:"fe80::/10",label:"Link-local (IPv6)"},{pattern:"fc00::/7",label:"Unique local (IPv6) — the 10.x of IPv6"},{pattern:"*.internal",label:"Reserved name"},{pattern:"*.intranet",label:"Reserved name"},{pattern:"*.corp",label:"Conventional corporate suffix"},{pattern:"*.lan",label:"Conventional LAN suffix"},{pattern:"*.local",label:"mDNS / Bonjour"},{pattern:"*.localdomain",label:"Default suffix on many routers"},{pattern:"*.home",label:"Home network"},{pattern:"*.home.arpa",label:"Home network (RFC 8375)"},{pattern:"*.private",label:"Conventional private suffix"},{pattern:"*.test",label:"Reserved for testing (RFC 2606)"},{pattern:"*.invalid",label:"Reserved as never-resolvable"},{pattern:"<intranet>",label:"Any bare hostname with no dots, e.g. http://wiki/ — includes localhost"}]),w=Object.freeze(x.map(n=>n.pattern)),g=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;function m(n){const r=g.exec(n);if(!r)return null;const t=r.slice(1).map(Number);return t.some(e=>e>255)?null:t.reduce((e,l)=>e*256+l,0)}function d(n){const r=String(n||"").replace(/^\[|\]$/g,"").toLowerCase();if(!r.includes(":"))return null;const[t,e,...l]=r.split("::");if(l.length)return null;const o=u=>u?u.split(":").filter(p=>p!==""):[];let s=o(t),a=e===void 0?[]:o(e);const i=(a.length?a:s)[Math.max(0,(a.length?a:s).length-1)];if(i&&i.includes(".")){const u=m(i);if(u==null)return null;const p=[(u>>>16).toString(16),(u&65535).toString(16)];a.length?a=[...a.slice(0,-1),...p]:s=[...s.slice(0,-1),...p]}const c=8-s.length-a.length;if(e===void 0?c!==0:c<0)return null;const f=[...s,...Array(Math.max(0,c)).fill("0"),...a];if(f.length!==8)return null;let b="";for(const u of f){if(!/^[0-9a-f]{1,4}$/.test(u))return null;b+=parseInt(u,16).toString(2).padStart(16,"0")}return b}function y(n,r){const[t,e]=r.split("/"),l=d(n),o=d(t);if(l||o){if(!l||!o)return!1;const f=Number(e);return!Number.isFinite(f)||f<0||f>128?!1:l.slice(0,f)===o.slice(0,f)}const s=m(n),a=m(t);if(s==null||a==null)return!1;const i=Number(e);if(!Number.isFinite(i)||i<0||i>32)return!1;const c=i===0?0:-1<<32-i>>>0;return(s&c)>>>0===(a&c)>>>0}function R(n,r){const t=String(n||"").toLowerCase().replace(/\.$/,""),e=String(r||"").toLowerCase().trim();if(!t||!e)return!1;if(e==="<intranet>")return!t.includes(".")&&!t.includes(":")&&!t.startsWith("[")&&!g.test(t);if(e.includes("/"))return y(t,e);const l=d(e);if(l)return d(t)===l;const o=e.startsWith("*.")?e.slice(2):e;return!!(t===o||t.endsWith(`.${o}`))}function k(n,{patterns:r=w}={}){const t=String(n||"").trim();if(!t)return{internal:!1,host:"",matched:null};let e="",l="";try{const o=new URL(t);e=o.hostname,l=o.protocol.replace(":","")}catch{return{internal:!0,host:"",matched:"unparseable"}}if(l==="file")return{internal:!0,host:e,matched:"file:"};if(/^(chrome|edge|about|moz|chrome-extension|data|blob)$/.test(l))return{internal:!1,host:e,matched:null};for(const o of r)if(R(e,o))return{internal:!0,host:e,matched:o};return{internal:!1,host:e,matched:null}}function A(n){const r=[],t=/\b(?:https?|file):\/\/[^\s<>"'`)\]}]+/gi;for(const e of String(n||"").matchAll(t))r.push(e[0].replace(/[.,;:!?]+$/,""));return r}function N(n=[],{patterns:r,ceiling:t="device",base:e="any"}={}){const l=(Array.isArray(n)?n:[n]).filter(Boolean),o=[];for(const a of l){const i=typeof a=="string"?a:a?.url||a?.href||"";if(!i)continue;const c=k(i,r?{patterns:r}:void 0);c.internal&&o.push({url:i,host:c.host,matched:c.matched})}if(!o.length)return{internal:!1,reach:e,hits:[],why:null};const s=h.includes(t)?t:"device";return{internal:!0,reach:v(e,s),hits:o,why:`${o[0].host||"the source"} matches '${o[0].matched}' — kept ${s==="device"?"on this device":"inside your workspace"}`}}function C(n,r=[]){const t=[];for(const e of n||[]){for(const l of e?.attachments||[])l?.url&&t.push(l.url);for(const l of A(typeof e?.content=="string"?e.content:""))t.push(l)}for(const e of r||[])e&&t.push(typeof e=="string"?e:e.url||e.href||"");return t.filter(Boolean)}export{w as DEFAULT_INTERNAL_PATTERNS,x as INTERNAL_PATTERN_CATALOG,k as classifySource,A as extractUrls,R as hostMatches,v as meetReach,N as sourcePolicyFor,C as sourceUrlsOf};
1
+ const p=["device","trusted","any"];function v(r,n){const t=p.indexOf(r),e=p.indexOf(n);return t<0?p.includes(n)?n:"device":e<0?r:p[Math.min(t,e)]}const x=Object.freeze([{pattern:"localhost",label:"This machine, by name"},{pattern:"127.0.0.0/8",label:"Loopback"},{pattern:"::1",label:"Loopback (IPv6)"},{pattern:"0.0.0.0/8",label:"This network"},{pattern:"10.0.0.0/8",label:"Private network"},{pattern:"172.16.0.0/12",label:"Private network"},{pattern:"192.168.0.0/16",label:"Private network (home / office)"},{pattern:"100.64.0.0/10",label:"Carrier-grade NAT — used by some corporate networks"},{pattern:"169.254.0.0/16",label:"Link-local — never routes off this segment"},{pattern:"fe80::/10",label:"Link-local (IPv6)"},{pattern:"fc00::/7",label:"Unique local (IPv6) — the 10.x of IPv6"},{pattern:"*.internal",label:"Reserved name"},{pattern:"*.intranet",label:"Reserved name"},{pattern:"*.corp",label:"Conventional corporate suffix"},{pattern:"*.lan",label:"Conventional LAN suffix"},{pattern:"*.local",label:"mDNS / Bonjour"},{pattern:"*.localdomain",label:"Default suffix on many routers"},{pattern:"*.home",label:"Home network"},{pattern:"*.home.arpa",label:"Home network (RFC 8375)"},{pattern:"*.private",label:"Conventional private suffix"},{pattern:"*.test",label:"Reserved for testing (RFC 2606)"},{pattern:"*.invalid",label:"Reserved as never-resolvable"},{pattern:"<intranet>",label:"Any bare hostname with no dots, e.g. http://wiki/ — includes localhost"}]),w=Object.freeze(x.map(r=>r.pattern)),g=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;function b(r){const n=g.exec(r);if(!n)return null;const t=n.slice(1).map(Number);return t.some(e=>e>255)?null:t.reduce((e,l)=>e*256+l,0)}function m(r){const n=String(r||"").replace(/^\[|\]$/g,"").toLowerCase();if(!n.includes(":"))return null;const[t,e,...l]=n.split("::");if(l.length)return null;const a=f=>f?f.split(":").filter(d=>d!==""):[];let i=a(t),o=e===void 0?[]:a(e);const s=(o.length?o:i)[Math.max(0,(o.length?o:i).length-1)];if(s&&s.includes(".")){const f=b(s);if(f==null)return null;const d=[(f>>>16).toString(16),(f&65535).toString(16)];o.length?o=[...o.slice(0,-1),...d]:i=[...i.slice(0,-1),...d]}const u=8-i.length-o.length;if(e===void 0?u!==0:u<0)return null;const c=[...i,...Array(Math.max(0,u)).fill("0"),...o];if(c.length!==8)return null;let h="";for(const f of c){if(!/^[0-9a-f]{1,4}$/.test(f))return null;h+=parseInt(f,16).toString(2).padStart(16,"0")}return h}function y(r,n){const[t,e]=n.split("/"),l=m(r),a=m(t);if(l||a){if(!l||!a)return!1;const c=Number(e);return!Number.isFinite(c)||c<0||c>128?!1:l.slice(0,c)===a.slice(0,c)}const i=b(r),o=b(t);if(i==null||o==null)return!1;const s=Number(e);if(!Number.isFinite(s)||s<0||s>32)return!1;const u=s===0?0:-1<<32-s>>>0;return(i&u)>>>0===(o&u)>>>0}function R(r,n){const t=String(r||"").toLowerCase().replace(/\.$/,""),e=String(n||"").toLowerCase().trim();if(!t||!e)return!1;if(e==="<intranet>")return!t.includes(".")&&!t.includes(":")&&!t.startsWith("[")&&!g.test(t);if(e.includes("/"))return y(t,e);const l=m(e);if(l)return m(t)===l;const a=e.startsWith("*.")?e.slice(2):e;return!!(t===a||t.endsWith(`.${a}`))}function k(r,{patterns:n=w}={}){const t=String(r||"").trim();if(!t)return{internal:!1,host:"",matched:null};let e="",l="";try{const a=new URL(t);e=a.hostname,l=a.protocol.replace(":","")}catch{return{internal:!0,host:"",matched:"unparseable"}}if(l==="file")return{internal:!0,host:e,matched:"file:"};if(/^(chrome|edge|about|moz|chrome-extension|data|blob)$/.test(l))return{internal:!1,host:e,matched:null};for(const a of n)if(R(e,a))return{internal:!0,host:e,matched:a};return{internal:!1,host:e,matched:null}}function A(r){const n=[],t=/\b(?:https?|file):\/\/[^\s<>"'`)\]}]+/gi;for(const e of String(r||"").matchAll(t))n.push(e[0].replace(/[.,;:!?]+$/,""));return n}function C(r=[],{patterns:n,ceiling:t="device",base:e="any"}={}){const l=(Array.isArray(r)?r:[r]).filter(Boolean),a=[];for(const u of l){const c=typeof u=="string"?u:u?.url||u?.href||"";if(!c)continue;const h=k(c,n?{patterns:n}:void 0);h.internal&&a.push({url:c,host:h.host,matched:h.matched})}if(!a.length)return{internal:!1,reach:e,hits:[],why:null};const i=p.includes(t)?t:"device",o=i==="device"?"on this device":"inside your workspace",s=a[0];return{internal:!0,reach:v(e,i),hits:a,why:s.matched==="unparseable"?`"${N(s.url)}" is not a readable address, and an address that cannot be read is kept ${o}`:`${s.host||"the source"} matches '${s.matched}' — kept ${o}`}}function N(r){const n=String(r||"").trim();return n.length>72?`${n.slice(0,69)}…`:n}function L(r,n=[]){const t=[];for(const e of r||[]){for(const l of e?.attachments||[])l?.url&&t.push(l.url);for(const l of A(typeof e?.content=="string"?e.content:""))t.push(l)}for(const e of n||[])e&&t.push(typeof e=="string"?e:e.url||e.href||"");return t.filter(Boolean)}export{w as DEFAULT_INTERNAL_PATTERNS,x as INTERNAL_PATTERN_CATALOG,k as classifySource,A as extractUrls,R as hostMatches,v as meetReach,C as sourcePolicyFor,L as sourceUrlsOf};
package/usage-pricing.js CHANGED
@@ -1 +1 @@
1
- const _=.1,C=1.25,s=[[/claude.*fable.*5|claude.*mythos.*5/,{in:10,out:50}],[/claude.*opus.*4[.-]?[678]/,{in:5,out:25}],[/claude.*opus/,{in:5,out:25}],[/claude.*sonnet/,{in:3,out:15}],[/claude.*haiku/,{in:1,out:5}],[/gpt-4o-mini|o4-mini|gpt-4\.1-mini/,{in:.15,out:.6}],[/gpt-4o|gpt-4\.1(?!-mini)/,{in:2.5,out:10}],[/o3-mini/,{in:1.1,out:4.4}],[/gemini.*flash/,{in:.15,out:.6}],[/gemini.*pro/,{in:1.25,out:5}]];function m(n){const i=String(n||"").toLowerCase();if(!i)return null;for(const[o,e]of s)if(o.test(i))return e;return null}function E({model:n,inputTokens:i=0,outputTokens:o=0,cacheReadTokens:e=0,cacheWriteTokens:a=0,costUsd:r=null,estimated:l=!1}={}){if(r!=null&&Number.isFinite(r))return{usd:r,estimated:l};const t=m(n);if(!t)return{usd:null,estimated:!0};const u=(c,f)=>(Number(c)||0)*f/1e6;return{usd:u(i,t.in)+u(o,t.out)+u(e,t.in*.1)+u(a,t.in*1.25),estimated:l}}function g(n){return n==null||!Number.isFinite(n)?"n/a":n===0?"$0.00":n<.01?"<$0.01":`$${n.toFixed(n<1?4:2)}`}export{E as costFor,g as formatUsd,m as rateFor};
1
+ const _=.1,E=1.25,b=[[/claude.*fable.*5|claude.*mythos.*5/,{in:10,out:50}],[/claude.*opus.*4[.-]?[678]/,{in:5,out:25}],[/claude.*opus/,{in:5,out:25}],[/claude.*sonnet/,{in:3,out:15}],[/claude.*haiku/,{in:1,out:5}],[/gpt-4o-mini|o4-mini|gpt-4\.1-mini/,{in:.15,out:.6,cacheRead:.25,cacheWrite:0}],[/gpt-4o|gpt-4\.1(?!-mini)/,{in:2.5,out:10,cacheRead:.5,cacheWrite:0}],[/o3-mini/,{in:1.1,out:4.4,cacheRead:.5,cacheWrite:0}],[/gemini.*flash/,{in:.15,out:.6,cacheRead:.25,cacheWrite:0}],[/gemini.*pro/,{in:1.25,out:5,cacheRead:.25,cacheWrite:0}]];function h(t){const e=String(t||"").toLowerCase();if(!e)return null;for(const[o,n]of b)if(o.test(e))return n;return null}function a({model:t,inputTokens:e=0,outputTokens:o=0,cacheReadTokens:n=0,cacheWriteTokens:i=0,costUsd:s=null,estimated:r=!1,rate:m=null}={}){if(s!=null&&Number.isFinite(s))return{usd:s,estimated:r};const u=m||h(t);if(!u)return{usd:null,estimated:!0};const c=(f,d)=>(Number(f)||0)*d/1e6;return{usd:c(e,u.in)+c(o,u.out)+c(n,u.in*(u.cacheRead??.1))+c(i,u.in*(u.cacheWrite??1.25)),estimated:r}}const l=Object.freeze(["plan","list","custom"]),A=Object.freeze({plan:"Subscription plan — tokens only, no $",list:"API key — provider list price",custom:"Custom rates per 1M tokens"}),p=t=>t==null||t===""||!Number.isFinite(Number(t))||Number(t)<0?null:Number(t);function R(t,{defaultBasis:e="plan"}={}){const o=l.includes(t?.basis)?t.basis:l.includes(e)?e:"plan";if(o!=="custom")return{basis:o};const n=p(t?.in),i=p(t?.out);return n==null||i==null?{basis:"plan"}:{basis:"custom",in:n,out:i}}function N(t,e,{defaultBasis:o="plan"}={}){const n=R(e,{defaultBasis:o}),i=t||{};if(n.basis==="plan")return{costUsd:null,costBasis:"plan"};if(n.basis==="custom"){const{usd:r}=a({...i,costUsd:null,rate:{in:n.in,out:n.out}});return{costUsd:r,costBasis:"custom"}}const{usd:s}=a(i);return{costUsd:s,costBasis:"list"}}function x(t){return t==null||!Number.isFinite(t)?"n/a":t===0?"$0.00":t<.01?"<$0.01":`$${t.toFixed(t<1?4:2)}`}export{l as PRICING_BASES,A as PRICING_LABELS,a as costFor,x as formatUsd,R as normalizePricing,N as priceUsage,h as rateFor};
package/usage.js CHANGED
@@ -1 +1,2 @@
1
- import{costFor as h}from"./usage-pricing.js";const _=500,a=e=>Number.isFinite(Number(e))?Number(e):0,l=e=>new Date(e).toISOString().slice(0,10);function m(e={}){const t={ts:e.ts||Date.now(),surface:e.surface||"other",sourceId:e.sourceId||null,agentId:e.agentId||null,provider:e.provider||null,model:e.model||null,inputTokens:a(e.inputTokens),outputTokens:a(e.outputTokens),cacheReadTokens:a(e.cacheReadTokens),cacheWriteTokens:a(e.cacheWriteTokens),estimated:!!e.estimated,costUsd:e.costUsd!=null?a(e.costUsd):null};return!t.inputTokens&&!t.outputTokens&&!t.cacheReadTokens&&!t.cacheWriteTokens?null:t}function I(e,s={}){return!e||e.type!=="usage"?null:m({surface:s.surface||"other",sourceId:s.sourceId,agentId:s.agentId,provider:e.provider,model:e.model,inputTokens:e.inputTokens,outputTokens:e.outputTokens,cacheReadTokens:e.cacheReadTokens,cacheWriteTokens:e.cacheWriteTokens,costUsd:e.costUsd,estimated:e.estimated})}function W(e,s={}){return!e||typeof e!="object"?null:m({...s,inputTokens:e.prompt_tokens??e.input_tokens,outputTokens:e.completion_tokens??e.output_tokens,cacheReadTokens:e.prompt_tokens_details?.cached_tokens??e.cache_read_input_tokens,cacheWriteTokens:e.cache_creation_input_tokens,costUsd:e.cost_usd??e.costUsd??null})}function R(e){return`${e.surface}|${e.agentId||"?"}|${e.model||"?"}|${l(e.ts)}`}function g({rollups:e={},log:s=[]}={},t,{ringMax:u=_}={}){if(!t)return{rollups:e,log:s};const d=R(t),n=e[d]||{surface:t.surface,agentId:t.agentId,model:t.model,day:l(t.ts),calls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheWriteTokens:0,estimatedCalls:0,costUsd:0};return n.calls+=1,n.inputTokens+=t.inputTokens,n.outputTokens+=t.outputTokens,n.cacheReadTokens+=t.cacheReadTokens,n.cacheWriteTokens+=t.cacheWriteTokens,t.estimated&&(n.estimatedCalls+=1),t.costUsd!=null&&(n.costUsd+=t.costUsd),e[d]=n,s.push(t),s.length>u&&s.splice(0,s.length-u),{rollups:e,log:s}}function x(e={},{surface:s,sinceDays:t,groupBy:u="surface",now:d=Date.now()}={}){const n=t?l(d-t*864e5):null,T=new Map,i={calls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheWriteTokens:0,usd:0,estimated:!1};for(const o of Object.values(e||{})){if(!o||s&&o.surface!==s||n&&o.day<n)continue;const c=u==="model"?o.model||"?":u==="agent"?o.agentId||"?":o.surface||"other",k=T.get(c)||{key:c,calls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheWriteTokens:0,usd:0,estimated:!1};for(const p of["calls","inputTokens","outputTokens","cacheReadTokens","cacheWriteTokens"])k[p]+=o[p]||0,i[p]+=o[p]||0;const{usd:r,estimated:f}=o.costUsd?{usd:o.costUsd,estimated:o.estimatedCalls>0}:h({model:o.model,inputTokens:o.inputTokens,outputTokens:o.outputTokens,cacheReadTokens:o.cacheReadTokens,cacheWriteTokens:o.cacheWriteTokens,estimated:o.estimatedCalls>0});r!=null&&(k.usd+=r,i.usd+=r),f&&(k.estimated=!0,i.estimated=!0),T.set(c,k)}return{groups:[...T.values()].sort((o,c)=>c.inputTokens+c.outputTokens-(o.inputTokens+o.outputTokens)),total:i}}export{_ as USAGE_RING_MAX,g as applyUsage,l as dayOf,R as rollupKey,x as summarizeUsage,I as usageFromEvent,W as usageFromOpenAi,m as usageRecord};
1
+ import{costFor as W,formatUsd as x}from"./usage-pricing.js";const C=500,B=Object.freeze(["plan","list","custom"]),n=s=>Number.isFinite(Number(s))?Number(s):0,h=s=>new Date(s).toISOString().slice(0,10);function m(s={}){const e={ts:s.ts||Date.now(),surface:s.surface||"other",sourceId:s.sourceId||null,agentId:s.agentId||null,provider:s.provider||null,model:s.model||null,inputTokens:n(s.inputTokens),outputTokens:n(s.outputTokens),cacheReadTokens:n(s.cacheReadTokens),cacheWriteTokens:n(s.cacheWriteTokens),estimated:!!s.estimated,costUsd:s.costUsd!=null?n(s.costUsd):null,costBasis:B.includes(s.costBasis)?s.costBasis:null};return!e.inputTokens&&!e.outputTokens&&!e.cacheReadTokens&&!e.cacheWriteTokens?null:e}function F(s,t={}){return!s||s.type!=="usage"?null:m({surface:t.surface||"other",sourceId:t.sourceId,agentId:t.agentId,provider:s.provider,model:s.model,inputTokens:s.inputTokens,outputTokens:s.outputTokens,cacheReadTokens:s.cacheReadTokens,cacheWriteTokens:s.cacheWriteTokens,costUsd:s.costUsd,costBasis:s.costBasis,estimated:s.estimated})}function O(s,t={}){if(!s||typeof s!="object")return null;const e=n(s.prompt_tokens_details?.cached_tokens??s.cache_read_input_tokens),a=s.prompt_tokens!=null?Math.max(0,n(s.prompt_tokens)-e):n(s.input_tokens);return m({...t,inputTokens:a,outputTokens:s.completion_tokens??s.output_tokens,cacheReadTokens:e,cacheWriteTokens:s.cache_creation_input_tokens,costUsd:s.cost_usd??s.costUsd??null,costBasis:s.cost_basis??s.costBasis??t.costBasis??null})}function b(s){return`${s.surface}|${s.agentId||"?"}|${s.model||"?"}|${h(s.ts)}`}function w({rollups:s={},log:t=[]}={},e,{ringMax:a=C}={}){if(!e)return{rollups:s,log:t};const i=b(e),c=s[i]||{surface:e.surface,agentId:e.agentId,model:e.model,day:h(e.ts),calls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheWriteTokens:0,estimatedCalls:0,planCalls:0,costUsd:0};return c.calls+=1,e.costBasis==="plan"&&(c.planCalls=(c.planCalls||0)+1),c.inputTokens+=e.inputTokens,c.outputTokens+=e.outputTokens,c.cacheReadTokens+=e.cacheReadTokens,c.cacheWriteTokens+=e.cacheWriteTokens,e.estimated&&(c.estimatedCalls+=1),e.costUsd!=null&&(c.costUsd+=e.costUsd),s[i]=c,t.push(e),t.length>a&&t.splice(0,t.length-a),{rollups:s,log:t}}function j(s={},{surface:t,sinceDays:e,groupBy:a="surface",now:i=Date.now()}={}){const c=e?h(i-e*864e5):null,u=new Map,d={calls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheWriteTokens:0,planCalls:0,usd:0,estimated:!1};for(const o of Object.values(s||{})){if(!o||t&&o.surface!==t||c&&o.day<c)continue;const p=a==="model"?o.model||"?":a==="agent"?o.agentId||"?":o.surface||"other",k=u.get(p)||{key:p,calls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheWriteTokens:0,planCalls:0,usd:0,estimated:!1};for(const r of["calls","inputTokens","outputTokens","cacheReadTokens","cacheWriteTokens","planCalls"])k[r]+=o[r]||0,d[r]+=o[r]||0;const R=(o.planCalls||0)>=o.calls,{usd:T,estimated:I}=R?{usd:null,estimated:!1}:o.costUsd?{usd:o.costUsd,estimated:o.estimatedCalls>0}:W({model:o.model,inputTokens:o.inputTokens,outputTokens:o.outputTokens,cacheReadTokens:o.cacheReadTokens,cacheWriteTokens:o.cacheWriteTokens,estimated:o.estimatedCalls>0});T!=null&&(k.usd+=T,d.usd+=T),I&&(k.estimated=!0,d.estimated=!0),u.set(p,k)}const f=o=>(o.plan=o.calls>0&&o.planCalls>=o.calls,o);return{groups:[...u.values()].map(f).sort((o,p)=>p.inputTokens+p.outputTokens-(o.inputTokens+o.outputTokens)),total:f(d)}}const l=s=>s>=1e3?`${(s/1e3).toFixed(1)}k`:String(Math.round(s)),$=(s,t)=>`${t?"≈":""}${s>0&&s<.01?`$${s.toFixed(4)}`:x(s)}`;function U(s){return n(s?.inputTokens)+n(s?.cacheReadTokens)+n(s?.cacheWriteTokens)}const _=s=>s.costBasis!=="plan"&&s.costUsd!=null&&Number.isFinite(s.costUsd);function N(s){if(!s)return"";const t=s.harnessInputTokens!=null?n(s.harnessInputTokens):null,e=U(s),a=n(s.outputTokens);if(!e&&!a&&!t)return"";const i=t??e,c=s.estimated||t!=null,u=_(s)?` · ${$(s.costUsd,!!s.estimated)}`:"";return`${c?"≈":""}${l(i)} in / ${l(a)} out${u}`}function g(s){if(!s)return"";const t=s.harnessInputTokens!=null?n(s.harnessInputTokens):null,e=U(s),a=n(s.outputTokens);if(!e&&!a&&!t)return"";const i=[];t!=null&&(i.push(`≈${l(t)} tokens sent by you (message, prompt, tools)`),e>t&&i.push(`${l(e-t)} the agent's own context (its prompt, tools, history)`));const c=n(s.requests),u=[`${l(e)} in total${c>1?` over ${c} requests`:""}`];return n(s.cacheReadTokens)>0&&u.push(`${l(n(s.cacheReadTokens))} cached`),n(s.cacheWriteTokens)>0&&u.push(`${l(n(s.cacheWriteTokens))} written to cache`),i.push(`${s.estimated?"≈":""}${u.join(" · ")} / ${l(a)} out`),s.costBasis==="plan"?i.push("Subscription plan — no per-token price"):_(s)&&i.push(`${$(s.costUsd,!!s.estimated)} at ${s.costBasis==="custom"?"your rates":"list price"}, on the full count`),i.join(`
2
+ `)}export{B as COST_BASES,C as USAGE_RING_MAX,w as applyUsage,h as dayOf,g as describeTurnUsage,N as formatTurnUsage,U as promptTokensOf,b as rollupKey,j as summarizeUsage,F as usageFromEvent,O as usageFromOpenAi,m as usageRecord};
package/web-search.js CHANGED
@@ -1 +1 @@
1
- const g=Object.freeze([Object.freeze({id:"startpage",name:"Startpage",url:"https://www.startpage.com/sp/search?query=%s",enabled:!0}),Object.freeze({id:"duckduckgo",name:"DuckDuckGo",url:"https://html.duckduckgo.com/html/?q=%s",enabled:!0}),Object.freeze({id:"google",name:"Google",url:"https://www.google.com/search?q=%s",enabled:!1}),Object.freeze({id:"bing",name:"Bing",url:"https://www.bing.com/search?q=%s",enabled:!1})]),h=5;function S(n,r,{assertFetchable:e}={}){const t=String(n||"").trim();if(!/^https:\/\//i.test(t))throw new Error("Search engine URL must start with https://");if(!t.includes("%s")&&!t.includes("{q}"))throw new Error("Search engine URL must contain a %s (or {q}) query placeholder");const s=t.replace(/%s|\{q\}/g,encodeURIComponent(String(r||"").trim()));return typeof e=="function"&&e(s),s}function m(n){try{const r=new URL(n,"https://duckduckgo.com"),e=r.searchParams.get("uddg");if(e)return decodeURIComponent(e);const t=r.searchParams.get("url")||r.searchParams.get("u");return t&&/^https?:/i.test(t)?t:r.href}catch{return String(n||"")}}const d=/(^|\.)(startpage\.com|startmail\.com|startpage\.dev|mojeek\.com|buttondown\.(com|email)|ecosia\.org|duckduckgo\.com|search\.brave\.com|qwant\.com)$/i;function w(n,r=""){const e=String(n||"").toLowerCase();if(!e||d.test(e))return!1;const t=String(r||"").split(".").slice(-2).join(".").toLowerCase();return!(t&&(e===t||e.endsWith(`.${t}`)))}function b(n,{engineHost:r="",limit:e=h,fallback:t=!1}={}){const s=[],c=new Map;for(const o of n||[]){const a=m(o?.href||"");let i;try{i=new URL(a)}catch{continue}if(i.protocol!=="https:"&&i.protocol!=="http:"||!w(i.hostname,r)||t&&o.chrome)continue;const u=String(o?.text||"").replace(/\s+/g," ").trim();if(u.length<6)continue;const p=i.origin+i.pathname;if(c.has(p)){const l=c.get(p);l&&!l.snippet&&u.length>l.title.length+10&&(l.snippet=u);continue}const f={url:a,title:u,snippet:String(o?.snippet||"").replace(/\s+/g," ").trim()};if(c.set(p,f),s.push(f),s.length>=e)break}return s}function k(n,r=8){const e=[],t=new Set;for(const s of n||[])for(const c of s||[]){let o;try{const a=new URL(c.url);o=a.origin+a.pathname}catch{o=c.url}if(!t.has(o)&&(t.add(o),e.push(c),e.length>=r))return e}return e}function R(n=g){const r=(Array.isArray(n)?n:[]).filter(e=>e&&e.url&&!e.retired);return[...r.filter(e=>e.enabled!==!1),...r.filter(e=>e.enabled===!1)]}export{h as RESULTS_PER_ENGINE,g as SEARCH_ENGINES,S as buildSearchUrl,R as engineOrder,w as isResultHost,k as mergeEngineResults,b as pickResults,m as unwrapRedirect};
1
+ import{GATEWAY_ENGINE as g}from"./search-engines.js";const h=Object.freeze([g,Object.freeze({id:"startpage",name:"Startpage",url:"https://www.startpage.com/sp/search?query=%s",enabled:!0}),Object.freeze({id:"duckduckgo",name:"DuckDuckGo",url:"https://html.duckduckgo.com/html/?q=%s",enabled:!0}),Object.freeze({id:"google",name:"Google",url:"https://www.google.com/search?q=%s",enabled:!1}),Object.freeze({id:"bing",name:"Bing",url:"https://www.bing.com/search?q=%s",enabled:!1})]),m=5;function k(n,r,{assertFetchable:t}={}){const e=String(n||"").trim();if(!/^https:\/\//i.test(e))throw new Error("Search engine URL must start with https://");if(!e.includes("%s")&&!e.includes("{q}"))throw new Error("Search engine URL must contain a %s (or {q}) query placeholder");const s=e.replace(/%s|\{q\}/g,encodeURIComponent(String(r||"").trim()));return typeof t=="function"&&t(s),s}function d(n){try{const r=new URL(n,"https://duckduckgo.com"),t=r.searchParams.get("uddg");if(t)return decodeURIComponent(t);const e=r.searchParams.get("url")||r.searchParams.get("u");return e&&/^https?:/i.test(e)?e:r.href}catch{return String(n||"")}}const w=/(^|\.)(startpage\.com|startmail\.com|startpage\.dev|mojeek\.com|buttondown\.(com|email)|ecosia\.org|duckduckgo\.com|search\.brave\.com|qwant\.com)$/i;function S(n,r=""){const t=String(n||"").toLowerCase();if(!t||w.test(t))return!1;const e=String(r||"").split(".").slice(-2).join(".").toLowerCase();return!(e&&(t===e||t.endsWith(`.${e}`)))}function R(n,{engineHost:r="",limit:t=m,fallback:e=!1}={}){const s=[],c=new Map;for(const o of n||[]){const a=d(o?.href||"");let i;try{i=new URL(a)}catch{continue}if(i.protocol!=="https:"&&i.protocol!=="http:"||!S(i.hostname,r)||e&&o.chrome)continue;const u=String(o?.text||"").replace(/\s+/g," ").trim();if(u.length<6)continue;const p=i.origin+i.pathname;if(c.has(p)){const l=c.get(p);l&&!l.snippet&&u.length>l.title.length+10&&(l.snippet=u);continue}const f={url:a,title:u,snippet:String(o?.snippet||"").replace(/\s+/g," ").trim()};if(c.set(p,f),s.push(f),s.length>=t)break}return s}function E(n,r=8){const t=[],e=new Set;for(const s of n||[])for(const c of s||[]){let o;try{const a=new URL(c.url);o=a.origin+a.pathname}catch{o=c.url}if(!e.has(o)&&(e.add(o),t.push(c),t.length>=r))return t}return t}function y(n=h){const r=(Array.isArray(n)?n:[]).filter(t=>t&&(t.url||t.kind==="gateway")&&!t.retired);return[...r.filter(t=>t.enabled!==!1),...r.filter(t=>t.enabled===!1)]}export{m as RESULTS_PER_ENGINE,h as SEARCH_ENGINES,k as buildSearchUrl,y as engineOrder,S as isResultHost,E as mergeEngineResults,R as pickResults,d as unwrapRedirect};