@cortexkit/opencode-anthropic-auth 1.9.4 → 1.10.1
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/dist/cli.js +1 -1
- package/dist/index-3ynbghx3.js +1 -0
- package/dist/index-jb14nj5c.js +2 -0
- package/dist/{index-agh52sak.js → index-p8j7mmfc.js} +1 -1
- package/dist/index.js +11 -9
- package/dist/rpc/notifications.d.ts +5 -0
- package/dist/rpc/port-file.d.ts +12 -0
- package/dist/rpc/port-file.js +1 -0
- package/dist/rpc/protocol.d.ts +20 -0
- package/dist/rpc/protocol.js +0 -0
- package/dist/rpc/rpc-client.d.ts +6 -0
- package/dist/rpc/rpc-client.js +1 -0
- package/dist/rpc/rpc-dir.d.ts +3 -0
- package/dist/rpc/rpc-dir.js +1 -0
- package/dist/rpc/rpc-server.d.ts +13 -0
- package/dist/sidebar-state.js +1 -1
- package/dist/tui/command-dialogs.d.ts +8 -0
- package/dist/tui-preferences.js +1 -1
- package/package.json +5 -3
- package/src/rpc/notifications.ts +54 -0
- package/src/rpc/port-file.ts +64 -0
- package/src/rpc/protocol.ts +31 -0
- package/src/rpc/rpc-client.ts +55 -0
- package/src/rpc/rpc-dir.ts +19 -0
- package/src/rpc/rpc-server.ts +121 -0
- package/src/tui/command-dialogs.tsx +229 -0
- package/src/tui.tsx +105 -1
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{Oa as I,Qa as $,m as w,n as k,p as A,r as R,t as d,u as l,v as p}from"./index-fvzta6xw.js";import"./index-
|
|
2
|
+
import{Oa as I,Qa as $,m as w,n as k,p as A,r as R,t as d,u as l,v as p}from"./index-fvzta6xw.js";import"./index-p8j7mmfc.js";import{stdin as E,stdout as U}from"node:process";import{createInterface as _}from"node:readline/promises";function g(){return{version:1,main:{type:"opencode",provider:"anthropic"},fallbackOn:[401,403,429],refresh:{enabled:!0,intervalMinutes:10,refreshBeforeExpiryMinutes:240},quota:{enabled:!0,checkIntervalMinutes:5,minimumRemaining:{five_hour:10,seven_day:20},failClosedOnUnknownQuota:!0},accounts:[]}}function y(){console.log(`Usage:
|
|
3
3
|
opencode-anthropic-auth login [label]
|
|
4
4
|
opencode-anthropic-auth api add [label]
|
|
5
5
|
opencode-anthropic-auth list
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createHash as z}from"node:crypto";import{homedir as F,tmpdir as L}from"node:os";import{join as k}from"node:path";var G="OPENCODE_ANTHROPIC_AUTH_RPC_DIR";function Q(q){let g=process.env[G]?.trim();if(g)return g;let w=z("sha256").update(q).digest("hex").slice(0,16),x=process.env.XDG_STATE_HOME||k(F(),".local","state");return k(x,"cortexkit","anthropic-auth","rpc",w)}export{L as fb,Q as gb};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{mkdir as s,readdir as a,readFile as u,rename as c,unlink as l,writeFile as p}from"node:fs/promises";import{join as o}from"node:path";function m(r){try{return process.kill(r,0),!0}catch(n){return n.code==="EPERM"}}async function g(r,n){await s(r,{recursive:!0});let i={...n,startedAt:Date.now()},t=o(r,`port-${n.pid}.json`),e=`${t}.${process.pid}.tmp`;return await p(e,JSON.stringify(i),{encoding:"utf8",mode:384}),await c(e,t),t}async function y(r){let n;try{n=await a(r)}catch{return null}let i=[];for(let t of n){if(!t.startsWith("port-")||!t.endsWith(".json"))continue;try{let e=JSON.parse(await u(o(r,t),"utf8"));if(Number.isFinite(e.port))if(m(e.pid))i.push(e);else l(o(r,t)).catch(()=>{})}catch{}}if(i.length===0)return null;return i.sort((t,e)=>e.startedAt-t.startedAt)[0]??null}
|
|
2
|
+
export{g as db,y as eb};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import{createRequire as q}from"node:module";var j=Object.create;var{getPrototypeOf:k,defineProperty:i,getOwnPropertyNames:l}=Object;var m=Object.prototype.hasOwnProperty;function n(a){return this[a]}var o,p,r=(a,b,d)=>{var f=a!=null&&typeof a==="object";if(f){var g=b?o??=new WeakMap:p??=new WeakMap,h=g.get(a);if(h)return h}d=a!=null?j(k(a)):{};let c=b||!a||!a.__esModule?i(d,"default",{value:a,enumerable:!0}):d;for(let e of l(a))if(!m.call(c,e))i(c,e,{get:n.bind(a,e),enumerable:!0});if(f)g.set(a,c);return c};var s=(a,b)=>()=>(b||a((b={exports:{}}).exports,b),b.exports);var u=q(import.meta.url);
|
|
2
|
-
export{r as
|
|
2
|
+
export{r as hb,s as ib,u as jb};
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import{$ as
|
|
1
|
+
import{$ as eW,A as mW,Aa as N4,B as pW,Ba as FW,C as $1,Ca as n0,D as iW,Da as V4,E as O1,Ea as X4,F as lW,Fa as Z4,G as dW,Ga as K4,H as nW,Ha as CW,I as q1,Ia as HW,J as G1,Ja as Y4,K as uW,Ka as p1,L as x0,La as _4,M as R1,Ma as D4,N as cW,Na as i1,O as oW,P as p0,Pa as I4,Q as i0,R as N1,Ra as l1,S as rW,Sa as Y1,T as T1,Ta as z4,U as tW,Ua as w4,V as sW,Va as d1,W as aW,X as E1,Y as q0,Z as R0,_ as l0,a as YW,aa as V1,b as _W,ba as QW,c as v0,ca as LW,d as A0,da as S1,e as DW,ea as MW,f as f0,fa as UW,g as kW,ga as BW,h as IW,ha as v1,i as zW,ia as W4,j as wW,ja as j4,k as C1,ka as A1,l as yW,la as J4,m as H1,ma as k1,n as f1,na as P4,o as E,oa as $4,p as W0,pa as y1,q as h0,qa as X1,r as x1,ra as O4,s as hW,sa as h1,t as bW,ta as d0,u as A,ua as b1,va as Z1,w as b0,wa as K1,x as gW,xa as G4,y as g0,ya as g1,z as m0,za as m1}from"./index-fvzta6xw.js";import{Za as L4}from"./index-9yjbmhf1.js";import{db as WW}from"./index-jb14nj5c.js";import{gb as Q4}from"./index-3ynbghx3.js";import"./index-p8j7mmfc.js";import{randomUUID as zj}from"node:crypto";function H0(W){return typeof W==="object"&&W!==null}function H4(W){if(Array.isArray(W))return W;if(H0(W)&&Array.isArray(W.data))return W.data;return[]}function f4(W){if(!H0(W)||!H0(W.info))return;return typeof W.info.role==="string"?W.info.role:void 0}function u1(W){if(!H0(W)||!H0(W.info))return null;let J=W.info,j=H0(J.model)?J.model:void 0,$=typeof J.agent==="string"?J.agent:void 0,P=typeof j?.providerID==="string"?j.providerID:typeof J.providerID==="string"?J.providerID:void 0,N=typeof j?.modelID==="string"?j.modelID:typeof J.modelID==="string"?J.modelID:void 0,I=typeof j?.variant==="string"?j.variant:typeof J.variant==="string"?J.variant:void 0;if(!$&&(!P||!N)&&!I)return null;let H={};if($)H.agent=$;if(P&&N)H.model={providerID:P,modelID:N};if(I)H.variant=I;return H}function c1(W,J){return{agent:W.agent??J.agent,model:W.model??J.model,variant:W.variant??J.variant}}function o1(W){return Boolean(W.agent&&W.model&&W.variant)}async function r1(W,J){if(!W||!J)return null;let j=W;if(typeof j.session?.messages!=="function")return null;let $=[];try{$=H4(await Promise.resolve(j.session.messages({path:{id:J},query:{limit:100}})))}catch{return null}if($.length===0)return null;let P={};for(let N=$.length-1;N>=0;N--){if(f4($[N])!=="assistant")continue;let I=u1($[N]);if(!I)continue;if(P=c1(P,I),o1(P))return P}for(let N=$.length-1;N>=0;N--){let I=u1($[N]);if(!I)continue;if(P=c1(P,I),o1(P))return P}if(!P.agent&&!P.model&&!P.variant)return null;return P}var K0=[],x4=1,w1=0,t1=new Map;function s1(W,J){if(K0.push({id:x4++,type:"open-dialog",payload:W,sessionId:J}),K0.length>100)K0=K0.slice(K0.length-100)}function a1(W=0,J){let j=Date.now();if(w1=j,J!==void 0)t1.set(J,j);let $=(P)=>J===void 0||P.sessionId===void 0||P.sessionId===J;if(W>0)K0=K0.filter((P)=>{if(P.id>W)return!0;if(J===void 0)return!1;return P.sessionId!==J});return K0.filter((P)=>P.id>W&&$(P))}function e1(W){let J=Date.now();if(W!==void 0){let j=t1.get(W)??0;return j>0&&J-j<3000}return w1>0&&J-w1<3000}import{randomBytes as q4,timingSafeEqual as R4}from"node:crypto";import{unlink as T4}from"node:fs/promises";import{createServer as E4}from"node:http";import{join as S4}from"node:path";function v4(W){return new Promise((J,j)=>{let $=[],P=0;W.on("data",(N)=>{if(P+=N.length,P>1e6){W.destroy(),j(Error("body too large"));return}$.push(N)}),W.on("end",()=>J(Buffer.concat($).toString("utf8"))),W.on("error",j)})}function A4(W,J){if(!W?.startsWith("Bearer "))return!1;let j=Buffer.from(W.slice(7)),$=Buffer.from(J);return j.length===$.length&&R4(j,$)}async function jW(W){let J=q4(32).toString("hex"),j=E4((N,I)=>{$(N,I)});async function $(N,I){let H=(k,b)=>{I.writeHead(k,{"content-type":"application/json"}),I.end(JSON.stringify(b))};try{let k=N.url??"";if(N.method==="GET"&&k==="/health")return H(200,{ok:!0});if(N.method!=="POST"||!k.startsWith("/rpc/"))return H(404,{error:"not found"});if(!A4(N.headers.authorization,J))return H(401,{error:"unauthorized"});let b=k.slice(5),m=JSON.parse(await v4(N)||"{}");if(b==="pending-notifications"){let S=W.drain(Number(m.lastReceivedId??0),typeof m.sessionId==="string"?m.sessionId:void 0);return H(200,{messages:S})}if(b==="apply"){let S=await W.apply(m);return H(200,S)}return H(404,{error:"unknown method"})}catch(k){H(500,{error:k instanceof Error?k.message:String(k)})}}let P=await new Promise((N,I)=>{j.on("error",I),j.listen(0,"127.0.0.1",()=>{let H=j.address();if(H&&typeof H==="object")N(H.port);else I(Error("no port"))})});j.unref();try{await WW(W.dir,{port:P,token:J,pid:process.pid})}catch(N){throw await new Promise((I)=>j.close(()=>I())),N}return{port:P,token:J,async stop(){await new Promise((N)=>j.close(()=>N())),await T4(S4(W.dir,`port-${process.pid}.json`)).catch(()=>{})}}}function JW(W,J){let j=new Map,$=0,P=0,N=0,I=0,H=0,k=(S)=>{let r=performance.now(),l=W(S);return H+=performance.now()-r,N++,l};return{call:(S)=>{if(!J.enabled())return k(S);let r=j.get(S);if(r!==void 0)return P++,j.delete(S),j.set(S,r),r;let l=k(S),O0=S.length+l.length;if(O0>J.maxBytes)return l;j.set(S,l),$+=O0;while($>J.maxBytes&&j.size>1){let d=j.keys().next().value,j0=j.get(d)??"";j.delete(d),$-=d.length+j0.length,I++}return l},stats:()=>({hits:P,misses:N,evictions:I,entries:j.size,cacheBytes:$,computeMsTotal:H})}}function PW(W){return`${YW}${W.charAt(0).toUpperCase()}${W.slice(1)}`}function fW(W){if(W==="StructuredOutput")return W;return`${W.charAt(0).toLowerCase()}${W.slice(1)}`}var k4=new Set(["callgraph","conflicts","delete","import","inspect","move","navigate","outline","refactor","safety","search","transform","zoom"]);function $W(W){let J=fW(W);if(J.startsWith("aft_"))return J;if(k4.has(J))return`aft_${J}`;return null}function M1(W,J){let j=new Headers;if(W instanceof Request)W.headers.forEach((P,N)=>{j.set(N,P)});let $=J?.headers;if($){if($ instanceof Headers)$.forEach((P,N)=>{j.set(N,P)});else if(Array.isArray($))for(let P of $){let[N,I]=P;if(typeof I<"u")j.set(N,String(I))}else for(let[P,N]of Object.entries($))if(typeof N<"u")j.set(P,String(N))}return j}function W1(W){return W.set("anthropic-beta",v0(W.get("anthropic-beta"),[_W])),W}function k0(W,J,j={}){return UW(W,J,j)}function y4(W){if(W.tools&&Array.isArray(W.tools))W.tools=W.tools.map((J)=>({...J,name:J.name?PW(J.name):J.name}));if(W.messages&&Array.isArray(W.messages))W.messages=W.messages.map((J)=>{if(J.content&&Array.isArray(J.content))J.content=J.content.map((j)=>{if(j.type==="tool_use"&&j.name)return{...j,name:PW(j.name)};return j});return J});return JSON.stringify(BW(W))}function Q1(W){return W.replace(/"name"\s*:\s*"(mcp_)?([^"]+)"/g,(J,j,$)=>{if(j)return`"name": "${$W($)??fW($)}"`;let P=$W($);return P?`"name": "${P}"`:J})}function OW(W,J=!1){if(J)return{ready:Q1(W),pending:""};let j=W.length,$='"name"',P=Math.max(0,W.length-$.length+1);for(let I=P;I<W.length;I++)if($.startsWith(W.slice(I))){j=Math.min(j,I);break}let N=W.lastIndexOf($);if(N!==-1){let I=W.slice(N);if(/^"name"\s*(?::\s*(?:"[^"]*)?)?$/.test(I))j=Math.min(j,N)}if(j<W.length)return{ready:Q1(W.slice(0,j)),pending:W.slice(j)};return{ready:Q1(W),pending:""}}function U1(){if(!process.env.ANTHROPIC_BASE_URL?.trim())return!1;let W=process.env.ANTHROPIC_INSECURE?.trim();return W==="1"||W==="true"}function xW(W){let J=W?.trim();if(!J)return null;try{let j=new URL(J);if(j.protocol!=="http:"&&j.protocol!=="https:"||j.username||j.password)return null;return j}catch{return null}}function h4(){return xW(process.env.ANTHROPIC_BASE_URL)}function B1(W,J={}){let j=null;try{if(typeof W==="string"||W instanceof URL)j=new URL(W.toString());else if(W instanceof Request)j=new URL(W.url)}catch{j=null}if(!j)return{input:W,url:null};let $=j.href,P=J.baseURL?xW(J.baseURL):h4();if(P){if(j.protocol=P.protocol,j.host=P.host,J.baseURL)j.pathname=`${P.pathname.replace(/\/$/,"")}${j.pathname}`}if(j.pathname.endsWith("/v1/messages")&&!j.searchParams.has("beta"))j.searchParams.set("beta","true");if(j.href===$)return{input:W,url:j};return{input:W instanceof Request?new Request(j.toString(),W):j,url:j}}function b4(W){let $=W.split(/\n\n+/).filter((P)=>{if(P.includes(DW))return!1;for(let N of zW)if(P.includes(N))return!1;return!0}).join(`
|
|
2
2
|
|
|
3
|
-
`);for(let
|
|
4
|
-
`),
|
|
3
|
+
`);for(let P of wW)$=$.replace(P.match,P.replacement);return $.trim()}var g4=8388608;function m4(){return process.env.OPENCODE_ANTHROPIC_AUTH_MEMO!=="0"}var p4=JW(b4,{maxBytes:g4,enabled:m4});function t0(W){return p4.call(W)}function i(W){return W!=null&&typeof W==="object"&&!Array.isArray(W)}function i4(W){let J={type:"text",text:f0};if(W==null)return[J];if(typeof W==="string"){let $=t0(W);if($===f0)return[J];return[J,{type:"text",text:$}]}if(i(W)){let $=typeof W.type==="string"?W.type:"text",P=typeof W.text==="string"?W.text:"";return[J,{...W,type:$,text:t0(P)}]}if(!Array.isArray(W))return[J];let j=W.map(($)=>{if(typeof $==="string")return{type:"text",text:t0($)};if(i($)&&$.type==="text"&&typeof $.text==="string")return{...$,type:"text",text:t0($.text)};return{type:"text",text:String($)}});if(j[0]?.text===f0)return j;return[J,...j]}var qW={type:"ephemeral",ttl:"1h"},l4=20;function d4(W){if(i(W.cache_control))return W.cache_control;if(i(W.cacheControl))return W.cacheControl;return null}function S0(W,J){if(!i(W))return!1;return delete W.cacheControl,W.cache_control=J?{...qW}:{type:"ephemeral"},!0}function GW(W){if(!i(W))return;delete W.cache_control,delete W.cacheControl}function n4(W){if(Array.isArray(W))return W;if(typeof W==="string")return[{type:"text",text:W}];return null}function s0(W,J){if(!i(W))return;let j=d4(W);if(j?.type!=="ephemeral")return;if(J)j.ttl="1h";else delete j.ttl}function NW(W,J){if(Array.isArray(W.system))for(let j of W.system)s0(j,J);else s0(W.system,J);if(!Array.isArray(W.messages))return;for(let j of W.messages)if(s0(j,J),i(j)&&Array.isArray(j.content))for(let $ of j.content)s0($,J)}function u4(W,J){if(Array.isArray(W.system))for(let j of W.system)J(j);else J(W.system);if(!Array.isArray(W.messages))return;for(let j of W.messages)if(J(j),i(j)&&Array.isArray(j.content))for(let $ of j.content)J($)}function RW(W){GW(W),u4(W,GW)}function c4(W){RW(W),W.cache_control={...qW}}function o4(W){return i(W)&&W.type!=="thinking"&&W.type!=="redacted_thinking"}function j1(W){let J=n4(W.content);if(!J?.length)return;let j=J.filter(o4);if(!j.length)return;return{content:J,cacheableBlocks:j}}function r4(W){if(!i(W))return!1;let J=j1(W);if(!J)return!1;return W.content=J.content,S0(J.cacheableBlocks[0],!0)}function t4(W){if(!i(W))return!1;let J=j1(W);if(!J||J.cacheableBlocks.length<2)return!1;return W.content=J.content,S0(J.cacheableBlocks[1],!0)}function a0(W){if(!i(W))return!1;let J=j1(W);if(!J)return!1;let j=J.cacheableBlocks[J.cacheableBlocks.length-1];return W.content=J.content,S0(j,!0)}function s4(W){if(!i(W))return!1;return(j1(W)?.cacheableBlocks.length??0)>1}function a4(W){if(!i(W))return 0;let J=W.content;if(Array.isArray(J))return J.length;if(J==null)return 0;return 1}function e4(W){let J=[],j=0;return W.forEach(($,P)=>{let N=a4($);if(i($)&&$.role==="user"&&N>0)J.push({index:P,blockPosition:j+N-1});j+=N}),J}function Wj(W){let J=e4(W),j=[...J].reverse().find((I)=>I.index>1);if(!j)return{latest:void 0,bridge:void 0};let $=[...J].reverse().find((I)=>I.index<j.index),P=$?j.blockPosition-$.blockPosition+1:0,N=$&&$.index>1&&P>l4?$:void 0;return{latest:j,bridge:N}}function L1(W){return i(W)&&typeof W.text==="string"?W.text:""}function jj(W){if(!Array.isArray(W.system))return;let J=W.system,j=0;if(L1(J[j]).startsWith("x-anthropic-billing-header:"))j++;if(L1(J[j])===f0)j++;let $=j+1;if($>=J.length-1)return;let P=J[$];if(!i(P))return;let N=J.slice($).map(L1).join(`
|
|
4
|
+
`);J.splice($,J.length-$,{...P,type:"text",text:N})}function VW(W){if(Array.isArray(W.system)){let J=W.system.findIndex((P)=>i(P)&&P.text===f0),j=W.system.slice(J>=0?J+1:0).filter(i),$=j[j.length-1];S0($,!0)}else S0(W.system,!0)}function Jj(W){if(RW(W),jj(W),!Array.isArray(W.messages)){VW(W);return}let{latest:J,bridge:j}=Wj(W.messages);if(!j)VW(W);if(s4(W.messages[0]))r4(W.messages[0]),t4(W.messages[0]);else a0(W.messages[0]),a0(W.messages[1]);if(j)a0(W.messages[j.index]);if(J)a0(W.messages[J.index])}function Pj(W){if(!Array.isArray(W.messages))return 0;let J=0;return W.messages=W.messages.filter((j)=>{if(!i(j)||!Array.isArray(j.content))return!0;if(j.role!=="assistant")return!0;let $=j.content.filter((P)=>{if(!i(P)||P.type!=="thinking")return!0;if(!HW(P.signature))return!0;return J+=1,!1});if($.length===j.content.length)return!0;return j.content=$,$.length>0}),J}function $j(W){if(!CW(W.model))return null;let J=Object.hasOwn(W,"thinking");return W.thinking={...FW},{replacedExisting:J}}function Oj(W,J){if(!J.enabled){NW(W,!1),delete W.cache_control,delete W.cacheControl;return}if(J.mode==="automatic"){c4(W);return}if(J.mode==="hybrid"){Jj(W);return}NW(W,!0),delete W.cacheControl}function Gj(W){if(!Array.isArray(W.messages))return;while(W.messages.length){let J=W.messages[W.messages.length-1];if(!i(J)||J.role!=="assistant")break;W.messages.pop()}}function v(){return performance.now()}function P0(W){return Math.round(W*10)/10}function XW(W){let J=0,j=0,$=0,P=Array.isArray(W.messages)?W.messages:[];J=P.length;for(let N of P){if(!N||typeof N!=="object")continue;let I=N.content;if(!Array.isArray(I))continue;j+=I.length;for(let H of I)if(H&&typeof H==="object"&&H.type==="tool_use")$++}return{messageCount:J,contentBlockCount:j,toolDefinitionCount:Array.isArray(W.tools)?W.tools.length:0,toolUseCount:$,systemBlockCount:Array.isArray(W.system)?W.system.length:0}}async function F1(W,J={}){try{let j=v(),$=JSON.parse(W);J.perf?.("parse",{ms:P0(v()-j),inputBytes:W.length,...XW($)});let P=v(),N=Array.isArray($.messages)?$.messages.length:void 0;Gj($);let I=Array.isArray($.messages)?$.messages.length:void 0;J.perf?.("strip_trailing_assistant",{ms:P0(v()-P),removedMessages:typeof N==="number"&&typeof I==="number"?N-I:void 0});let H=v(),k=Pj($),b=$j($);J.perf?.("model_normalize",{ms:P0(v()-H),model:typeof $.model==="string"?$.model:void 0,fableMythosThinkingDisplay:b?"summarized":void 0,replacedFableMythosThinking:b?.replacedExisting??!1,removedNonAnthropicThinking:k,hasOutputConfig:Object.hasOwn($,"output_config")});let m=v(),S=Array.isArray($.messages)&&$.messages.some((J0)=>J0.role==="user")?LW($.messages,void 0,IW):null;J.perf?.("billing_header",{ms:P0(v()-m),hasBillingHeader:Boolean(S)});let r=v();if($.system=i4($.system),J.perf?.("system_identity",{ms:P0(v()-r),systemBlockCount:Array.isArray($.system)?$.system.length:0}),S&&Array.isArray($.system))$.system.unshift({type:"text",text:S});let l=v();if(Oj($,{enabled:J.cache1hEnabled??!1,mode:J.cache1hMode??"explicit"}),J.perf?.("cache_strategy",{ms:P0(v()-l),enabled:J.cache1hEnabled??!1,mode:J.cache1hMode??"explicit"}),J.fastModeEnabled&&A0($.model))$.speed="fast";else if($.speed==="fast")delete $.speed;let O0=v();if(J.identity)MW($,J.identity);J.perf?.("metadata",{ms:P0(v()-O0),hasAccountUuid:Boolean(J.identity?.accountUuid)});let d=v(),j0=y4($);J.perf?.("prefix_tools_stringify",{ms:P0(v()-d),outputBytesBeforeSign:j0.length,...XW($)});let N0=v(),z0=await QW(j0);return J.perf?.("cch_sign",{ms:P0(v()-N0),outputBytes:z0.length}),z0}catch{return W}}var Nj=new TextEncoder;function Vj(){return{pending:"",events:0,parseErrors:0,eventCounts:{},typeCounts:{},deltaTypeCounts:{},contentBlockTypeCounts:{},textDeltaBytes:0,thinkingDeltaBytes:0,inputJsonDeltaBytes:0,signatureDeltaBytes:0,redactedThinkingBytes:0}}function e0(W,J){if(!J)return;W[J]=(W[J]??0)+1}function I0(W){return Nj.encode(W).byteLength}function Y0(W){return W&&typeof W==="object"&&!Array.isArray(W)?W:void 0}function s(W,J){let j=W?.[J];return typeof j==="string"?j:void 0}function Xj(W,J){let j=W?.[J];return typeof j==="number"?j:void 0}function Zj(W){if(!W.trim())return null;let J,j=[];for(let S of W.split(/\r?\n/)){if(S.startsWith("event:")){J=S.slice(6).trim();continue}if(S.startsWith("data:")){let r=S.slice(5);j.push(r.startsWith(" ")?r.slice(1):r)}}let $=j.join(`
|
|
5
|
+
`),P={event:J,rawBytes:I0(W),dataBytes:I0($)};if(!$||$==="[DONE]")return P.type=$==="[DONE]"?"done":void 0,P.event??=P.type,P;let N;try{N=JSON.parse($)}catch{return P}let I=Y0(N),H=Y0(I?.delta),k=Y0(I?.content_block),b=Y0(I?.message),m=Y0(I?.usage)??Y0(b?.usage);if(P.type=s(I,"type"),P.event??=P.type,P.index=Xj(I,"index"),P.contentBlockType=s(k,"type"),P.deltaType=s(H,"type"),P.stopReason=s(I,"stop_reason"),P.deltaType==="text_delta")P.textDeltaBytes=I0(s(H,"text")??"");if(P.deltaType==="thinking_delta")P.thinkingDeltaBytes=I0(s(H,"thinking")??"");if(P.deltaType==="input_json_delta")P.inputJsonDeltaBytes=I0(s(H,"partial_json")??"");if(P.deltaType==="signature_delta")P.signatureDeltaBytes=I0(s(H,"signature")??"");if(P.contentBlockType==="redacted_thinking")P.redactedThinkingBytes=I0(s(k,"data")??"");if(m)P.stopReason??=s(b,"stop_reason");return P}function TW(W){let J=W.indexOf(`
|
|
5
6
|
|
|
6
|
-
`)
|
|
7
|
+
`),j=W.indexOf(`\r
|
|
7
8
|
\r
|
|
8
|
-
`);if(J===-1)return $===-1?null:{index:$,length:4};if($===-1||J<$)return{index:J,length:2};return{index:$,length:4}}function t1(j,J){if(!J)return;j.pending+=J;while(!0){let $=dJ(j.pending);if(!$)break;let W=j.pending.slice(0,$.index);j.pending=j.pending.slice($.index+$.length);let V=lJ(W);if(!V)continue;if(j.events++,a0(j.eventCounts,V.event??"unknown"),a0(j.typeCounts,V.type),a0(j.deltaTypeCounts,V.deltaType),a0(j.contentBlockTypeCounts,V.contentBlockType),j.textDeltaBytes+=V.textDeltaBytes??0,j.thinkingDeltaBytes+=V.thinkingDeltaBytes??0,j.inputJsonDeltaBytes+=V.inputJsonDeltaBytes??0,j.signatureDeltaBytes+=V.signatureDeltaBytes??0,j.redactedThinkingBytes+=V.redactedThinkingBytes??0,j.last=V,V.dataBytes>0&&!V.type&&!V.event)j.parseErrors++}}function iJ(j){return{sseEvents:j.events,ssePendingChars:j.pending.length,sseParseErrors:j.parseErrors,sseEventCounts:{...j.eventCounts},sseTypeCounts:{...j.typeCounts},sseDeltaTypeCounts:{...j.deltaTypeCounts},sseContentBlockTypeCounts:{...j.contentBlockTypeCounts},sseTextDeltaBytes:j.textDeltaBytes,sseThinkingDeltaBytes:j.thinkingDeltaBytes,sseInputJsonDeltaBytes:j.inputJsonDeltaBytes,sseSignatureDeltaBytes:j.signatureDeltaBytes,sseRedactedThinkingBytes:j.redactedThinkingBytes,sseLastEvent:j.last?{...j.last}:void 0}}function f0(j,J={}){if(!j.body)return j;let $=j.body.getReader(),W=new TextDecoder,V=new TextEncoder,z="",M=0,S=0,r=0,u=0,j0=0,y=!1,f=!1,n=v(),a=v(),d=J.perf?mJ():void 0,W0=()=>{if(f)return;f=!0,$.releaseLock()},C0=(s={})=>({chunks:M,pulls:S,inputBytes:r,outputBytes:u,pendingChars:z.length,rewriteMs:J0(j0),totalMs:J0(v()-a),...d?iJ(d):{},...s}),$0=(s,x0={})=>{J.perf?.(s,C0(x0))};J.perf?.("stream_tool_prefix_wrapper_created",{status:j.status,hasBody:!0});let H0=new ReadableStream({async pull(s){if(S++,!y)y=!0,$0("stream_tool_prefix_first_pull");let x0=v();try{let{done:K0,value:T0}=await $.read(),l0=J0(v()-x0);if(K0){let G=W.decode();if(d)t1(d,G);let D=v(),w=n1(`${z}${G}`,!0);if(j0+=v()-D,w.ready){let P=V.encode(w.ready);u+=P.byteLength,s.enqueue(P)}$0("stream_tool_prefix_rewrite",{readMs:l0}),W0(),s.close();return}M++,r+=T0.byteLength;let d0=W.decode(T0,{stream:!0});if(d)t1(d,d0);let N0=z+d0,i0=v(),q0=n1(N0);if(j0+=v()-i0,z=q0.pending,q0.ready){let G=V.encode(q0.ready);u+=G.byteLength,s.enqueue(G)}let X=v();if(M===1||M%25===0||X-n>5000)n=X,$0("stream_tool_prefix_progress",{readMs:l0,lastChunkBytes:T0.byteLength})}catch(K0){throw $0("stream_tool_prefix_error",{error:K0 instanceof Error?K0.message:String(K0)}),W0(),K0}},async cancel(s){$0("stream_tool_prefix_cancel",{reason:s instanceof Error?s.message:String(s)});try{await $.cancel(s)}finally{W0()}}});return new Response(H0,{status:j.status,statusText:j.statusText,headers:j.headers})}var cJ="__OPENCODE_ANTHROPIC_AUTH_COMMAND_HANDLED__",nJ=60000,oJ=60000,rJ=5000,Lj=200,ZJ=240,sJ=ZJ,aJ=1,Qj=!1;function L1(){return process.env.OPENCODE_ANTHROPIC_AUTH_PERF==="1"}function F(){return performance.now()}function A(j){return Math.round(j*10)/10}function Mj(j){return Math.floor(Math.random()*Math.max(0,j))}function j1(j){if(typeof j==="string")return j;if(j instanceof URL)return j.toString();return j.url}function J1(j,J){return J?.method??(j instanceof Request?j.method:void 0)}function wj(j){return j instanceof Error?j.message:String(j)}function tJ(){if(Qj||!1||!L1())return;Qj=!0;let j=100,J=250,$=F()+j;setInterval(()=>{let W=F(),V=W-$;if($=W+j,V<J)return;q("[perf] opencode event_loop_lag",{lagMs:A(V),thresholdMs:J})},j).unref?.()}function eJ(j){let J=F(),$={requestId:String(aJ++),start:J,last:J,mark(W,V){let z=F();if(L1())q("[perf] opencode request stage",{requestId:$.requestId,stage:W,deltaMs:A(z-$.last),totalMs:A(z-$.start),...V});$.last=z},done(W,V){let z=F();if(L1())q("[perf] opencode request done",{requestId:$.requestId,stage:W,deltaMs:A(z-$.last),totalMs:A(z-$.start),...V});$.last=z}};if(L1())q("[perf] opencode request start",{requestId:$.requestId,...j});return $}async function D0(j,J,$){let W=j.client.session,V=await d1(j.client,J),z={path:{id:J},body:{noReply:!0,parts:[{type:"text",text:$,ignored:!0}]}};if(V?.agent)z.body.agent=V.agent;if(V?.model)z.body.model=V.model;if(V?.variant)z.body.variant=V.variant;if(typeof W?.promptAsync==="function"){await W.promptAsync(z);return}if(typeof W?.prompt==="function"){await Promise.resolve(W.prompt(z));return}throw Error("OpenCode session prompt API is unavailable for ignored replies.")}function I0(){throw Error(cJ)}function j3(j){if(j.sessionID==null)return!1;let J=j.model;return J?.providerID==="anthropic"||J?.api?.npm==="@ai-sdk/anthropic"||J?.api?.npm==="@ai-sdk/google-vertex/anthropic"}function J3(j){if(j.some((J)=>J.includes("<use_parallel_tool_calls>")))return!1;return j.push(Uj),!0}var $3={input:0,output:0,cache:{read:0,write:0}};function V3(j){let J=j["claude-opus-4-8"]??j["claude-opus-4-5"]??Object.values(j)[0];if(!J)return j;return{...j,...Object.fromEntries(Object.values(ej).map(($)=>[$.id,{...J,id:$.id,name:$.name,api:J.api?{...J.api,id:$.id}:void 0,cost:{input:p0.input,output:p0.output,cache:{read:p0.cacheRead,write:p0.cacheWrite5m}},limit:{...J.limit??{},context:sj,output:aj},capabilities:{...J.capabilities??{},reasoning:!0,attachment:!0,toolcall:!0},release_date:tj}]))}}function W3(j){return Object.fromEntries(Object.entries(j).map(([J,$])=>[J,{...$,cost:$3}]))}var _3=async(j)=>{tJ();let{client:J}=j,$=Cj(),W=await h($),V=new jJ({storage:W,onMainQuotaFetched:async(X,G,D,w)=>{try{let P=await h($)??{version:1,accounts:[]},E=fj(P);if(E&&E.checkedAt>=w&&hj(E.quota,P,E.checkedAt)>Date.now()){V.seedMainFromStorage(P);return}P.quota=P.quota??{},P.quota.mainQuota=X,P.quota.mainQuotaCheckedAt=G,P.quota.mainQuotaToken=D,P.quota.mainLastQuotaApiError=void 0,await y0(P,$,{mainQuota:!0})}catch(P){q("[quota] failed to persist main quota",{error:P instanceof Error?P.message:String(P)})}},onApiError:async(X)=>{try{let G=await h($)??{version:1,accounts:[]};G.quota=G.quota??{},G.quota.mainLastQuotaApiError=X,await y0(G,$,{mainQuota:!0})}catch(G){q("[quota] failed to persist backoff state",{error:G instanceof Error?G.message:String(G)})}}}),z=new gj({quotaManager:V,onFallbackStorageChanged:()=>{j0()}});z.startBackgroundRefresh();let M=null,S=new cj({loadStorage:()=>h($),prepareHeaders:async(X,G)=>{if(!f)return X;let D=await f();if(D.type!=="oauth")return X;if(!D.access||D.expires&&D.expires<Date.now()){if(!M)return X;D.access=await M()}if(!D.access)return X;try{let w=JSON.parse(G.bodyText),P=await S1(D.access,typeof w.model==="string"?w.model:void 0);if(X.delete("anthropic-beta"),A0(X,D.access,{body:w,identity:P}),X.set("anthropic-beta",v0(X.get("anthropic-beta"),[pj])),w.speed==="fast")t0(X)}catch{A0(X,D.access)}return X},log:q});g0({enabled:$1(W),mode:V1(W)}),P1(q1(W)),D1(E1(W));let r={activeId:"main",route:"main"};function u(X,G){r={activeId:G.activeId,route:G.route},V.updateStorage(X),V.seedMainFromStorage(X,G.mainAccessToken),V.seedFallbacksFromAccounts((X?.accounts??[]).filter(e));let D=V.getMain(G.mainAccessToken),w=V.getLastApiError(),P=X?.refresh?.mainLastRefreshError,E={main:{quota:D?.quota??null,quotaBackedOff:V.isBackedOff(),quotaBackoffUntil:w?.nextRetryAt,refreshBackedOff:P?U0(P,G.mainRefreshToken,Date.now()):!1,refreshBackoffUntil:P?.nextRetryAt},fallbacks:(X?.accounts??[]).filter((R)=>R.enabled!==!1&&e(R)).map((R)=>({id:R.id,label:R.label,quota:R.access?V.getFallback(R.id,R.access)?.quota??null:null,enabled:R.enabled!==!1})),activeId:G.activeId,route:G.route,relay:(()=>{let R=y1(X);return R?{enabled:!0,transport:R.transport??"http"}:null})(),fastMode:z1(),cacheKeep:{enabled:A1(X),window:X?.cacheKeep?.startHour!=null&&X?.cacheKeep?.endHour!=null?`${X.cacheKeep.startHour}-${X.cacheKeep.endHour}`:void 0,trackedSessions:S.trackedCount()},lastUpdated:Date.now()};GJ(E).catch((R)=>q("[sidebar] state write failed",{error:R instanceof Error?R.message:String(R)}))}async function j0(){let X=await h($),G,D;if(f)try{let w=await f();G=w.access,D=w.refresh}catch{}u(X,{activeId:r.activeId,route:r.route,mainAccessToken:G,mainRefreshToken:D})}function y(X,G){if(!G)return;if(X?.quota?.enabled!==!0)return;if(V.getMain(G))return;if(n)return;n=!0,V.refreshMain(G).then(()=>j0()).catch(()=>{}).finally(()=>{n=!1})}let f=null,n=!1,a=null,d=0;function W0(X){let G=X?.refresh?.refreshBeforeExpiryMinutes??sJ;return Math.max(ZJ,G)*60000}function C0(X){return X?.refresh?.enabled!==!1}async function $0(X){if(!X)return;let G=await h($),D=G?.refresh?.mainLastRefreshError;if(!G?.refresh||!D?.tokenHash)return;let w=W1(X);if(D.tokenHash===w)return;if(D.nextRetryAt&&D.nextRetryAt>Date.now()){q("[refresh] opencode main oauth keeping backoff despite token rotation",{nextRetryAt:D.nextRetryAt,retryCount:D.retryCount,remainingMs:D.nextRetryAt-Date.now()});return}G.refresh.mainLastRefreshError=void 0,await y0(G,$,{mainRefresh:!0}),q("[refresh] opencode main oauth cleared stale backoff after token rotation",{previousCheckedAt:D.checkedAt,previousNextRetryAt:D.nextRetryAt,previousRetryCount:D.retryCount})}async function H0(){let X=[];if(f)try{let P=await f();if(P.type==="oauth"&&P.access){let E=await V.refreshMain(P.access);X.push({name:"OpenCode anthropic",role:"main",quota:E})}else if(P.type==="oauth")X.push({name:"OpenCode anthropic",role:"main",error:"missing access token; send a request first or reconnect auth"})}catch(P){let E=P instanceof Error?P.message:String(P);X.push({name:"OpenCode anthropic",role:"main",error:E.includes("429")?"Usage API rate limited — try again in a moment":E})}let{storage:G,errors:D}=await z.refreshQuotaForAllAccounts({force:!0}),w=new Map(D.map((P)=>[P.accountId,P.message]));if(X.push(...JJ(G,w)),!f)X.unshift({name:"OpenCode anthropic",role:"main",error:"auth loader has not run yet; send a request first"});return $J({accounts:X,refreshedAt:Date.now()})}async function s(X){let G=mj(X);if(G.type==="enable"||G.type==="disable"){let E=G.type==="enable",R=await xj(E),o=V1(R);return g0({enabled:E,mode:o}),Z1({argumentsText:X,enabled:E,mode:o})}if(G.type==="mode"){let E=await Tj(G.mode),R=$1(E);return g0({enabled:R,mode:G.mode}),Z1({argumentsText:X,enabled:R,mode:G.mode})}let D=await h($),w=$1(D),P=V1(D);return g0({enabled:w,mode:P}),Z1({argumentsText:X,enabled:w,mode:P})}async function x0(X){let G=lj(X),D=await h($);if(G.type==="window")D=await Rj(G.startHour,G.endHour);else if(G.type==="disable")D=await vj(!1);let w=dj(D),P=S.stats(w);return uj({argumentsText:X,enabled:ij(D),window:w,hybridActive:A1(D),trackedSessions:P.trackedSessions,nextPrewarmAt:P.nextPrewarmAt})}async function K0(X){let G=nj(X);if(G.type==="enable"||G.type==="disable"){let P=G.type==="enable";return await qj(P),P1(P),f1({argumentsText:X,enabled:P})}let D=await h($),w=q1(D);return P1(w),f1({argumentsText:X,enabled:w})}async function T0(X){let G=oj(X);if(G.type==="enable"||G.type==="disable"){let P=G.type==="enable";return await Ej(P),D1(P),k1({argumentsText:X,enabled:P})}let D=await h($),w=E1(D);return D1(w),k1({argumentsText:X,enabled:w})}async function l0(X){let G=XJ(X);if(G.type==="mode")return await WJ(G.mode),h1({argumentsText:X,mode:G.mode});let D=await h();return h1({argumentsText:X,mode:b1(D)})}function d0(X,G=10){let D=Math.max(0,Math.min(Math.round(X/100*G),G));return"█".repeat(D)+"░".repeat(G-D)}function N0(X,G){return`${X} ${d0(G)} ${String(Math.round(G)).padStart(3)}%`}function i0(X){if(!X)return"";let G=new Date(X).getTime();if(Number.isNaN(G))return"";let D=G-Date.now();if(D<=0)return"resets now";let w=Math.floor(D/60000);if(w<1)return"resets <1m";if(w<60)return`resets ${w}m`;let P=Math.floor(w/60),E=w%60;return E>0?`resets ${P}h${E}m`:`resets ${P}h`}function q0(X,G,D){let w=[],P=0;if(X){let{five_hour:o,seven_day:V0}=X;if(o||V0){let Z0=D==="main"?"active":"idle",P0=i0(o?.resetsAt),_0=[`main · ${Z0}${P0?` (${P0})`:""}`];if(o)_0.push(N0("5h",o.usedPercent)),P=Math.max(P,o.usedPercent);if(V0)_0.push(N0("7d",V0.usedPercent)),P=Math.max(P,V0.usedPercent);w.push(_0.join(`
|
|
9
|
-
`))}}if(G?.length)for(let o of G){let V0=o.quota;if(!V0)continue;let{five_hour:Y0,seven_day:Z0}=V0;if(!Y0&&!Z0)continue;let P0=o.label||"alt",u0=D===o.id?"active":"idle",L0=i0(Y0?.resetsAt),O0=[`${P0} · ${u0}${L0?` (${L0})`:""}`];if(Y0)O0.push(N0("5h",Y0.usedPercent)),P=Math.max(P,Y0.usedPercent);if(Z0)O0.push(N0("7d",Z0.usedPercent)),P=Math.max(P,Z0.usedPercent);w.push(O0.join(`
|
|
10
|
-
`))}if(
|
|
9
|
+
`);if(J===-1)return j===-1?null:{index:j,length:4};if(j===-1||J<j)return{index:J,length:2};return{index:j,length:4}}function ZW(W,J){if(!J)return;W.pending+=J;while(!0){let j=TW(W.pending);if(!j)break;let $=W.pending.slice(0,j.index);W.pending=W.pending.slice(j.index+j.length);let P=Zj($);if(!P)continue;if(W.events++,e0(W.eventCounts,P.event??"unknown"),e0(W.typeCounts,P.type),e0(W.deltaTypeCounts,P.deltaType),e0(W.contentBlockTypeCounts,P.contentBlockType),W.textDeltaBytes+=P.textDeltaBytes??0,W.thinkingDeltaBytes+=P.thinkingDeltaBytes??0,W.inputJsonDeltaBytes+=P.inputJsonDeltaBytes??0,W.signatureDeltaBytes+=P.signatureDeltaBytes??0,W.redactedThinkingBytes+=P.redactedThinkingBytes??0,W.last=P,P.dataBytes>0&&!P.type&&!P.event)W.parseErrors++}}function Kj(){return{pending:""}}function Yj(W,J){let j=W?.toLowerCase(),$=J.toLowerCase();return j==="api_error"||j==="overloaded_error"||j==="server_error"||j==="internal_server_error"||$.includes("internal server error")||$.includes("server overloaded")}function _j(W,J){let j=W?`${W}: ${J}`:J,$=Error(`Anthropic stream error: ${j}`);if($.code="ECONNRESET",$.syscall="anthropic-sse",W)$.providerErrorType=W;return $}function Dj(W){if(!W.includes("error"))return null;let J,j=[];for(let b of W.split(/\r?\n/)){if(b.startsWith("event:")){J=b.slice(6).trim();continue}if(b.startsWith("data:")){let m=b.slice(5);j.push(m.startsWith(" ")?m.slice(1):m)}}let $=j.join(`
|
|
10
|
+
`);if(!$||$==="[DONE]")return null;let P;try{P=JSON.parse($)}catch{return null}let N=Y0(P);if(J!=="error"&&s(N,"type")!=="error")return null;let I=Y0(N?.error),H=s(I,"type")??s(I,"code")??void 0,k=s(I,"message")??s(N,"message")??H??"Anthropic stream error";if(!Yj(H,k))return null;return _j(H,k)}function KW(W,J){if(!J)return null;W.pending+=J;while(!0){let j=TW(W.pending);if(!j)break;let $=W.pending.slice(0,j.index);W.pending=W.pending.slice(j.index+j.length);let P=Dj($);if(P)return P}return null}function Ij(W){return{sseEvents:W.events,ssePendingChars:W.pending.length,sseParseErrors:W.parseErrors,sseEventCounts:{...W.eventCounts},sseTypeCounts:{...W.typeCounts},sseDeltaTypeCounts:{...W.deltaTypeCounts},sseContentBlockTypeCounts:{...W.contentBlockTypeCounts},sseTextDeltaBytes:W.textDeltaBytes,sseThinkingDeltaBytes:W.thinkingDeltaBytes,sseInputJsonDeltaBytes:W.inputJsonDeltaBytes,sseSignatureDeltaBytes:W.signatureDeltaBytes,sseRedactedThinkingBytes:W.redactedThinkingBytes,sseLastEvent:W.last?{...W.last}:void 0}}function y0(W,J={}){if(!W.body)return W;let j=W.body.getReader(),$=new TextDecoder,P=new TextEncoder,N="",I=0,H=0,k=0,b=0,m=0,S=!1,r=!1,l=v(),O0=v(),d=J.perf?Vj():void 0,j0=Kj(),N0=()=>{if(r)return;r=!0,j.releaseLock()},z0=(a={})=>({chunks:I,pulls:H,inputBytes:k,outputBytes:b,pendingChars:N.length,rewriteMs:P0(m),totalMs:P0(v()-O0),...d?Ij(d):{},...a}),J0=(a,T0={})=>{J.perf?.(a,z0(T0))};J.perf?.("stream_tool_prefix_wrapper_created",{status:W.status,hasBody:!0});let D1=new ReadableStream({async pull(a){if(H++,!S)S=!0,J0("stream_tool_prefix_first_pull");let T0=v();try{let{done:X0,value:w0}=await j.read(),E0=P0(v()-T0);if(X0){let O=$.decode();if(d)ZW(d,O);let G=KW(j0,O);if(G)throw J0("stream_tool_prefix_retryable_error",{error:G.message,providerErrorType:G.providerErrorType}),N0(),G;let Y=v(),L=OW(`${N}${O}`,!0);if(m+=v()-Y,L.ready){let K=P.encode(L.ready);b+=K.byteLength,a.enqueue(K)}J0("stream_tool_prefix_rewrite",{readMs:E0}),N0(),a.close();return}I++,k+=w0.byteLength;let Q0=$.decode(w0,{stream:!0});if(d)ZW(d,Q0);let L0=KW(j0,Q0);if(L0)throw J0("stream_tool_prefix_retryable_error",{error:L0.message,providerErrorType:L0.providerErrorType,readMs:E0,lastChunkBytes:w0.byteLength}),N0(),L0;let I1=N+Q0,M0=v(),U0=OW(I1);if(m+=v()-M0,N=U0.pending,U0.ready){let O=P.encode(U0.ready);b+=O.byteLength,a.enqueue(O)}let u0=v();if(I===1||I%25===0||u0-l>5000)l=u0,J0("stream_tool_prefix_progress",{readMs:E0,lastChunkBytes:w0.byteLength})}catch(X0){throw J0("stream_tool_prefix_error",{error:X0 instanceof Error?X0.message:String(X0)}),N0(),X0}},async cancel(a){J0("stream_tool_prefix_cancel",{reason:a instanceof Error?a.message:String(a)});try{await j.cancel(a)}finally{N0()}}});return new Response(D1,{status:W.status,statusText:W.statusText,headers:W.headers})}var wj="__OPENCODE_ANTHROPIC_AUTH_COMMAND_HANDLED__",Qj=60000,Lj=60000,Mj=5000,EW=200,M4=240,Uj=M4,Bj=1,SW=!1;function _1(){return process.env.OPENCODE_ANTHROPIC_AUTH_PERF==="1"}function F(){return performance.now()}function h(W){return Math.round(W*10)/10}function vW(W){return Math.floor(Math.random()*Math.max(0,W))}function J1(W){if(typeof W==="string")return W;if(W instanceof URL)return W.toString();return W.url}function P1(W,J){return J?.method??(W instanceof Request?W.method:void 0)}function AW(W){return W instanceof Error?W.message:String(W)}function Fj(){if(SW||!1||!_1())return;SW=!0;let W=100,J=250,j=F()+W;setInterval(()=>{let $=F(),P=$-j;if(j=$+W,P<J)return;E("[perf] opencode event_loop_lag",{lagMs:h(P),thresholdMs:J})},W).unref?.()}function Cj(W){let J=F(),j={requestId:String(Bj++),start:J,last:J,mark($,P){let N=F();if(_1())E("[perf] opencode request stage",{requestId:j.requestId,stage:$,deltaMs:h(N-j.last),totalMs:h(N-j.start),...P});j.last=N},done($,P){let N=F();if(_1())E("[perf] opencode request done",{requestId:j.requestId,stage:$,deltaMs:h(N-j.last),totalMs:h(N-j.start),...P});j.last=N}};if(_1())E("[perf] opencode request start",{requestId:j.requestId,...W});return j}async function Hj(W,J,j){let $=W.client.session,P=await r1(W.client,J),N={path:{id:J},body:{noReply:!0,parts:[{type:"text",text:j,ignored:!0}]}};if(P?.agent)N.body.agent=P.agent;if(P?.model)N.body.model=P.model;if(P?.variant)N.body.variant=P.variant;if(typeof $?.promptAsync==="function"){await $.promptAsync(N);return}if(typeof $?.prompt==="function"){await Promise.resolve($.prompt(N));return}throw Error("OpenCode session prompt API is unavailable for ignored replies.")}function fj(){throw Error(wj)}function xj(W){if(W.sessionID==null)return!1;let J=W.model;return J?.providerID==="anthropic"||J?.api?.npm==="@ai-sdk/anthropic"||J?.api?.npm==="@ai-sdk/google-vertex/anthropic"}function qj(W){if(W.some((J)=>J.includes("<use_parallel_tool_calls>")))return!1;return W.push(kW),!0}var Rj={input:0,output:0,cache:{read:0,write:0}};function Tj(W){let J=W["claude-opus-4-8"]??W["claude-opus-4-5"]??Object.values(W)[0];if(!J)return W;return{...W,...Object.fromEntries(Object.values(K4).map((j)=>[j.id,{...J,id:j.id,name:j.name,api:J.api?{...J.api,id:j.id}:void 0,cost:{input:n0.input,output:n0.output,cache:{read:n0.cacheRead,write:n0.cacheWrite5m}},limit:{...J.limit??{},context:V4,output:X4},capabilities:{...J.capabilities??{},reasoning:!0,attachment:!0,toolcall:!0},release_date:Z4}]))}}function Ej(W){return Object.fromEntries(Object.entries(W).map(([J,j])=>[J,{...j,cost:Rj}]))}var ej=async(W)=>{Fj();let{client:J}=W,j=bW(),$=await A(j),P=new Y4({storage:$,onMainQuotaFetched:async(O,G,Y,L)=>{try{let K=await A(j)??{version:1,accounts:[]},T=oW(K);if(T&&T.checkedAt>=L&&sW(T.quota,K,T.checkedAt)>Date.now()){P.seedMainFromStorage(K);return}K.quota=K.quota??{},K.quota.mainQuota=O,K.quota.mainQuotaCheckedAt=G,K.quota.mainQuotaToken=Y,K.quota.mainLastQuotaApiError=void 0,await b0(K,j,{mainQuota:!0})}catch(K){E("[quota] failed to persist main quota",{error:K instanceof Error?K.message:String(K)})}},onApiError:async(O)=>{try{let G=await A(j)??{version:1,accounts:[]};G.quota=G.quota??{},G.quota.mainLastQuotaApiError=O,await b0(G,j,{mainQuota:!0})}catch(G){E("[quota] failed to persist backoff state",{error:G instanceof Error?G.message:String(G)})}}}),N=new aW({quotaManager:P,onFallbackStorageChanged:()=>{S()}});N.startBackgroundRefresh();let I=null,H=new $4({loadStorage:()=>A(j),prepareHeaders:async(O,G)=>{if(!l)return O;let Y=await l();if(Y.type!=="oauth")return O;if(!Y.access||Y.expires&&Y.expires<Date.now()){if(!I)return O;Y.access=await I()}if(!Y.access)return O;try{let L=JSON.parse(G.bodyText),K=await S1(Y.access,typeof L.model==="string"?L.model:void 0);if(O.delete("anthropic-beta"),k0(O,Y.access,{body:L,identity:K}),O.set("anthropic-beta",v0(O.get("anthropic-beta"),[W4])),L.speed==="fast")W1(O)}catch{k0(O,Y.access)}return O},log:E});l0({enabled:g0($),mode:m0($)}),X1($1($)),K1(O1($));let k=null;if(W.directory){let O=globalThis;if(O.__anthropicAuthRpcServer)await O.__anthropicAuthRpcServer.stop().catch(()=>{}),O.__anthropicAuthRpcServer=void 0;try{k=await jW({dir:Q4(W.directory),drain:a1,apply:L0}),O.__anthropicAuthRpcServer=k}catch(G){E("[rpc] failed to start",{error:G instanceof Error?G.message:String(G)})}}let b={activeId:"main",route:"main"};function m(O,G){b={activeId:G.activeId,route:G.route},P.updateStorage(O),P.seedMainFromStorage(O,G.mainAccessToken),P.seedFallbacksFromAccounts((O?.accounts??[]).filter(W0));let Y=P.getMain(G.mainAccessToken),L=P.getLastApiError(),K=O?.refresh?.mainLastRefreshError,T={main:{quota:Y?.quota??null,quotaBackedOff:P.isBackedOff(),quotaBackoffUntil:L?.nextRetryAt,refreshBackedOff:K?x0(K,G.mainRefreshToken,Date.now()):!1,refreshBackoffUntil:K?.nextRetryAt},fallbacks:(O?.accounts??[]).filter((f)=>f.enabled!==!1&&W0(f)).map((f)=>({id:f.id,label:f.label,quota:f.access?P.getFallback(f.id,f.access)?.quota??null:null,enabled:f.enabled!==!1})),activeId:G.activeId,route:G.route,relay:(()=>{let f=i1(O);return f?{enabled:!0,transport:f.transport??"http"}:null})(),fastMode:Z1(),cacheKeep:{enabled:k1(O),window:O?.cacheKeep?.startHour!=null&&O?.cacheKeep?.endHour!=null?`${O.cacheKeep.startHour}-${O.cacheKeep.endHour}`:void 0,trackedSessions:H.trackedCount()},lastUpdated:Date.now()};L4(T).catch((f)=>E("[sidebar] state write failed",{error:f instanceof Error?f.message:String(f)}))}async function S(){let O=await A(j),G,Y;if(l)try{let L=await l();G=L.access,Y=L.refresh}catch{}m(O,{activeId:b.activeId,route:b.route,mainAccessToken:G,mainRefreshToken:Y})}function r(O,G){if(!G)return;if(O?.quota?.enabled!==!0)return;if(P.getMain(G))return;if(O0)return;O0=!0,P.refreshMain(G).then(()=>S()).catch(()=>{}).finally(()=>{O0=!1})}let l=null,O0=!1,d=null,j0=0;function N0(O){let G=O?.refresh?.refreshBeforeExpiryMinutes??Uj;return Math.max(M4,G)*60000}function z0(O){return O?.refresh?.enabled!==!1}async function J0(O){if(!O)return;let G=await A(j),Y=G?.refresh?.mainLastRefreshError;if(!G?.refresh||!Y?.tokenHash)return;let L=G1(O);if(Y.tokenHash===L)return;if(Y.nextRetryAt&&Y.nextRetryAt>Date.now()){E("[refresh] opencode main oauth keeping backoff despite token rotation",{nextRetryAt:Y.nextRetryAt,retryCount:Y.retryCount,remainingMs:Y.nextRetryAt-Date.now()});return}G.refresh.mainLastRefreshError=void 0,await b0(G,j,{mainRefresh:!0}),E("[refresh] opencode main oauth cleared stale backoff after token rotation",{previousCheckedAt:Y.checkedAt,previousNextRetryAt:Y.nextRetryAt,previousRetryCount:Y.retryCount})}async function D1(){let O=[];if(l)try{let K=await l();if(K.type==="oauth"&&K.access){let T=await P.refreshMain(K.access);O.push({name:"OpenCode anthropic",role:"main",quota:T})}else if(K.type==="oauth")O.push({name:"OpenCode anthropic",role:"main",error:"missing access token; send a request first or reconnect auth"})}catch(K){let T=K instanceof Error?K.message:String(K);O.push({name:"OpenCode anthropic",role:"main",error:T.includes("429")?"Usage API rate limited — try again in a moment":T})}let{storage:G,errors:Y}=await N.refreshQuotaForAllAccounts({force:!0}),L=new Map(Y.map((K)=>[K.accountId,K.message]));if(O.push(..._4(G,L)),!l)O.unshift({name:"OpenCode anthropic",role:"main",error:"auth loader has not run yet; send a request first"});return D4({accounts:O,refreshedAt:Date.now()})}async function a(O){let G=eW(O);if(G.type==="enable"||G.type==="disable"){let T=G.type==="enable",f=await mW(T),y=m0(f);return l0({enabled:T,mode:y}),V1({argumentsText:O,enabled:T,mode:y})}if(G.type==="mode"){let T=await pW(G.mode),f=g0(T);return l0({enabled:f,mode:G.mode}),V1({argumentsText:O,enabled:f,mode:G.mode})}let Y=await A(j),L=g0(Y),K=m0(Y);return l0({enabled:L,mode:K}),V1({argumentsText:O,enabled:L,mode:K})}async function T0(O){let G=j4(O),Y=await A(j);if(G.type==="window")Y=await dW(G.startHour,G.endHour);else if(G.type==="disable")Y=await nW(!1);let L=A1(Y),K=H.stats(L);return P4({argumentsText:O,enabled:J4(Y),window:L,hybridActive:k1(Y),trackedSessions:K.trackedSessions,nextPrewarmAt:K.nextPrewarmAt})}async function X0(O){let G=O4(O);if(G.type==="enable"||G.type==="disable"){let K=G.type==="enable";return await iW(K),X1(K),h1({argumentsText:O,enabled:K})}let Y=await A(j),L=$1(Y);return X1(L),h1({argumentsText:O,enabled:L})}async function w0(O){let G=G4(O);if(G.type==="enable"||G.type==="disable"){let K=G.type==="enable";return await lW(K),K1(K),g1({argumentsText:O,enabled:K})}let Y=await A(j),L=O1(Y);return K1(L),g1({argumentsText:O,enabled:L})}async function E0(O){let G=w4(O);if(G.type==="mode")return await z4(G.mode),d1({argumentsText:O,mode:G.mode});let Y=await A();return d1({argumentsText:O,mode:Y1(Y)})}async function Q0(O,G){if(O==="claude-quota")return{command:O,text:await D1(),knobs:{}};if(O==="claude-routing"){let f=await E0(G),y=await A(j);return{command:O,text:f,knobs:{mode:Y1(y)}}}if(O==="claude-fast"){let f=await w0(G),y=await A(j);return{command:O,text:f,knobs:{enabled:O1(y)}}}if(O==="claude-dump"){let f=await X0(G),y=await A(j);return{command:O,text:f,knobs:{enabled:$1(y)}}}if(O==="claude-cache"){let f=await a(G),y=await A(j);return{command:O,text:f,knobs:{enabled:g0(y),mode:m0(y)}}}if(O==="claude-cachekeep"){let f=await T0(G),y=await A(j);return{command:O,text:f,knobs:{window:A1(y)}}}let Y=await A(),L=T1(Y),K=(Y?.accounts??[]).filter((f)=>f.enabled!==!1).map((f)=>f.id),T=N4({argumentsText:G,config:L,accountIds:K});if(T.updatedConfig)await tW(T.updatedConfig);return{command:O,text:T.text,knobs:{config:T1(await A()),accountIds:K}}}async function L0(O){let G=await Q0(O.command,O.arguments);return{text:G.text,knobs:G.knobs}}function I1(O,G=10){let Y=Math.max(0,Math.min(Math.round(O/100*G),G));return"█".repeat(Y)+"░".repeat(G-Y)}function M0(O,G){return`${O} ${I1(G)} ${String(Math.round(G)).padStart(3)}%`}function U0(O){if(!O)return"";let G=new Date(O).getTime();if(Number.isNaN(G))return"";let Y=G-Date.now();if(Y<=0)return"resets now";let L=Math.floor(Y/60000);if(L<1)return"resets <1m";if(L<60)return`resets ${L}m`;let K=Math.floor(L/60),T=L%60;return T>0?`resets ${K}h${T}m`:`resets ${K}h`}function u0(O,G,Y){let L=[],K=0;if(O){let{five_hour:y,seven_day:$0}=O;if(y||$0){let V0=Y==="main"?"active":"idle",_0=U0(y?.resetsAt),B0=[`main · ${V0}${_0?` (${_0})`:""}`];if(y)B0.push(M0("5h",y.usedPercent)),K=Math.max(K,y.usedPercent);if($0)B0.push(M0("7d",$0.usedPercent)),K=Math.max(K,$0.usedPercent);L.push(B0.join(`
|
|
11
|
+
`))}}if(G?.length)for(let y of G){let $0=y.quota;if(!$0)continue;let{five_hour:Z0,seven_day:V0}=$0;if(!Z0&&!V0)continue;let _0=y.label||"alt",c0=Y===y.id?"active":"idle",F0=U0(Z0?.resetsAt),D0=[`${_0} · ${c0}${F0?` (${F0})`:""}`];if(Z0)D0.push(M0("5h",Z0.usedPercent)),K=Math.max(K,Z0.usedPercent);if(V0)D0.push(M0("7d",V0.usedPercent)),K=Math.max(K,V0.usedPercent);L.push(D0.join(`
|
|
12
|
+
`))}if(!L.length)return;let T=L.join(`
|
|
11
13
|
|
|
12
|
-
`),
|
|
14
|
+
`),f=K>=90?"error":K>=70?"warning":"info";J.tui?.showToast?.({body:{title:"Claude Quota",message:T,variant:f,duration:f==="error"?8000:5000}})?.catch?.(()=>{})}return{config:async(O)=>{O.command={...O.command??{},[E1]:{template:E1,description:"Show or toggle 1-hour Anthropic ephemeral prompt cache TTL."},[v1]:{template:v1,description:"Keep hybrid Claude cache warm for recently used sessions during a local time window."},[p1]:{template:p1,description:"Show current Claude OAuth quota usage for all accounts."},[y1]:{template:y1,description:"Show or toggle Anthropic request dump capture for debugging."},[b1]:{template:b1,description:"Show or toggle Anthropic fast mode for supported Opus models."},[l1]:{template:l1,description:"Show or change Claude account routing between main-first and fallback-first."},[m1]:{template:m1,description:"Manage killswitch — hard-block requests when quota drops below per-account thresholds."}}},"experimental.chat.system.transform":async(O,G)=>{if(!xj(O))return;qj(G.system)},provider:{id:"anthropic",async models(O,G){let Y=Tj(O.models);if(G.auth?.type!=="oauth"||!hW($??{}))return Y;return Ej(Y)}},"command.execute.before":async(O)=>{if(!["claude-cache","claude-cachekeep","claude-quota","claude-dump","claude-fast","claude-routing","claude-killswitch"].includes(O.command))return;let Y=O.command,L=await Q0(Y,O.arguments);if(Y==="claude-quota"){let K=await A(),T=l?await l().catch(()=>{return}):void 0;m(K,{activeId:"main",route:"main",mainAccessToken:T?.access,mainRefreshToken:T?.refresh})}if(e1(O.sessionID))s1(L,O.sessionID);else await Hj(W,O.sessionID,L.text);fj()},auth:{provider:"anthropic",async loader(O,G){l=O;let Y=await O();if(Y.type==="oauth"){let T=function(){if(d)clearInterval(d),d=null;let Z=async()=>{try{let D=await A(j);if(!z0(D))return;let q=await O();if(q.type!=="oauth")return;if(await J0(q.refresh),!q.expires)return;let z=q.expires-Date.now(),x=N0(D);if(z>x)return;if(E("[refresh] opencode main oauth background due",{expiresInMs:z,refreshBeforeMs:x}),q.refresh&&x0(D?.refresh?.mainLastRefreshError,q.refresh,Date.now())){E("[refresh] opencode main oauth background skipped backoff",{nextRetryAt:D?.refresh?.mainLastRefreshError?.nextRetryAt,retryCount:D?.refresh?.mainLastRefreshError?.retryCount,expiresInMs:z});return}if(q.refresh&&D?.refresh?.mainRefreshLeaseUntil&&D.refresh.mainRefreshLeaseUntil>Date.now()&&D.refresh.mainRefreshLeaseTokenHash===G1(q.refresh))return;await K();let X=await O();E("[refresh] opencode main oauth refreshed in background",{newExpiresInMs:X.expires?X.expires-Date.now():void 0})}catch(D){E("[refresh] opencode main oauth refresh failed",{message:D instanceof Error?D.message:String(D)})}};if(d=setInterval(()=>{Z()},Qj+vW(Lj)),"unref"in d)d.unref()},f=function(Z,D){if(Z instanceof Request&&Z.body)return!1;return D==null||typeof D==="string"},y=function(Z){return Z.has("x-parent-session-id")},$0=function(Z){return Z.includes("rate_limit_error")||/exceed your account'?s rate limit/i.test(Z)},Z0=function(Z){return Z?.quota?.enabled===!0},_0=function(Z,D){if(Z.delete("authorization"),Z.delete("x-api-key"),D.authHeader==="x-api-key")Z.set("x-api-key",D.apiKey??"");else Z.set("Authorization",`Bearer ${D.apiKey??""}`);Z.set("Content-Type","application/json")},F0=function(Z){return P.getFallback(Z.id,Z.access)?.quota??Z.quota},D0=function(Z){return Object.values(Z??{}).some((D)=>D&&D.remainingPercent<=0)},n1=function(Z,D){return Z.status===429||D},z1=function(Z){if(!Z)return!1;let D=P.getMain(Z);return Boolean(D&&D.refreshAfter>Date.now()&&D0(D.quota))},L=null;async function K(){if(!L)L=(async()=>{let q=null,z=null,x=null;async function X(_){let R=await A(j)??{version:1,main:{type:"opencode",provider:"anthropic"},accounts:[]};R.refresh=R.refresh??{},_(R),await b0(R,j,{mainRefresh:!0})}async function p(_){let R=Date.now()+Mj;while(Date.now()<R){await new Promise((B)=>setTimeout(B,EW+vW(EW)));let V=await O();if(V.type!=="oauth"||!V.access)continue;if((V.access!==_.access||V.refresh!==_.refresh||(V.expires??0)>(_.expires??0)+60000)&&(!V.expires||V.expires>Date.now()))return E("[refresh] opencode main oauth joined concurrent refresh",{expiresInMs:V.expires?V.expires-Date.now():void 0}),V.access}return null}for(let _=0;_<=2;_++){let R=null;try{if(_>0){let C=500*2**(_-1);await new Promise((Q)=>setTimeout(Q,C))}if(R=await O(),!R.refresh)throw Error("Token refresh failed: missing refresh token");let V=await A(j),w=G1(R.refresh),B=V?.refresh?.mainLastRefreshError;if(E("[refresh] opencode main oauth refresh check",{attempt:_,expiresInMs:R.expires?R.expires-Date.now():void 0,hasBackoff:Boolean(B),backoffActive:B?x0(B,R.refresh,Date.now()):!1,retryCount:B?.retryCount,nextRetryAt:B?.nextRetryAt}),B&&x0(B,R.refresh,Date.now()))throw E("[refresh] opencode main oauth refresh skipped backoff",{nextRetryAt:B.nextRetryAt,retryCount:B.retryCount}),Error(R1(B,Date.now()));if(V?.refresh?.mainRefreshLeaseUntil&&V.refresh.mainRefreshLeaseUntil>Date.now()&&V.refresh.mainRefreshLeaseTokenHash===w){E("[refresh] opencode main oauth refresh skipped lease",{leaseUntil:V.refresh.mainRefreshLeaseUntil});let C=await p(R);if(C)return C;throw Error("Claude OAuth refresh is already in progress")}let M=await gW({name:"opencode-main-oauth-refresh",ttlMs:120000,renew:!0});if(!M){E("[refresh] opencode main oauth refresh skipped file lock");let C=await p(R);if(C)return C;throw Error("Claude OAuth refresh is already in progress")}x=M.release,q=zj(),z=w,await X((C)=>{C.refresh=C.refresh??{},C.refresh.mainRefreshLeaseId=q??void 0,C.refresh.mainRefreshLeaseUntil=Date.now()+120000,C.refresh.mainRefreshLeaseTokenHash=w});let c=await A(j);if(E("[refresh] opencode main oauth refresh lease acquired",{attempt:_,leaseUntil:Date.now()+120000}),c?.refresh?.mainRefreshLeaseId!==q||c.refresh.mainRefreshLeaseTokenHash!==w)throw Error("Claude OAuth refresh is already in progress");E("[refresh] opencode main oauth refresh request start",{attempt:_});let n=await yW({refreshToken:R.refresh,maxRetries:0});return await J.auth.set({path:{id:"anthropic"},body:{type:"oauth",refresh:n.refresh,access:n.access,expires:n.expires}}),await X((C)=>{if(!C?.refresh)return;if(C.refresh.mainLastRefreshError=void 0,C.refresh.mainRefreshLeaseId===q)C.refresh.mainRefreshLeaseId=void 0,C.refresh.mainRefreshLeaseUntil=void 0,C.refresh.mainRefreshLeaseTokenHash=void 0}),E("[refresh] opencode main oauth refresh succeeded",{attempt:_,expiresInMs:n.expires-Date.now()}),n.access}catch(V){let w=V instanceof Error&&(V.message.includes("fetch failed")||("code"in V)&&(V.code==="ECONNRESET"||V.code==="ECONNREFUSED"||V.code==="ETIMEDOUT"||V.code==="UND_ERR_CONNECT_TIMEOUT"));if(_<2&&(w||V instanceof C1&&V.status>=500))continue;E("[refresh] opencode main oauth refresh attempt failed",{attempt:_,error:V instanceof Error?V.message:String(V),transient:w});let B=R?.refresh;if(B&&V instanceof C1)await X((M)=>{M.refresh=M.refresh??{},M.refresh.mainLastRefreshError=uW({error:V,now:Date.now(),refreshToken:B,previous:M.refresh.mainLastRefreshError})});throw V}finally{if(q)await X((V)=>{if(!V?.refresh)return;if(V.refresh.mainRefreshLeaseId===q&&V.refresh.mainRefreshLeaseTokenHash===z)V.refresh.mainRefreshLeaseId=void 0,V.refresh.mainRefreshLeaseUntil=void 0,V.refresh.mainRefreshLeaseTokenHash=void 0}).catch(()=>{});await x?.().catch(()=>{})}}throw Error("Token refresh exhausted all retries")})().finally(()=>{L=null});return L}I=K,T(),P.seedFallbacksFromAccounts(($?.accounts??[]).filter(W0)),m($,{activeId:"main",route:"main",mainAccessToken:Y.access,mainRefreshToken:Y.refresh});async function V0(Z,D){if(!Z.body||Z.status!==200)return D?.mark("inspect_stream_skip",{status:Z.status}),{response:Z,rateLimited:!1};if(Z.headers.get("x-cortexkit-relay-optimistic")==="true")return D?.mark("inspect_stream_skip",{status:Z.status,reason:"optimistic_relay"}),{response:Z,rateLimited:!1};let q=F(),z=Z.body.getReader(),x=[],X=new TextDecoder,p="",_=0;while(!p.includes(`
|
|
13
15
|
|
|
14
|
-
`)&&b.length<65536){let{done:Z,value:_}=await N.read();if(Z)break;if(H.push(_),O+=_.byteLength,b+=K.decode(_,{stream:!0}),V0(b))break}if(V0(b))return await N.cancel().catch(()=>{}),I?.mark("inspect_stream_first_event",{ms:A(F()-x),bytes:O,rateLimited:!0}),{response:Y,rateLimited:!0};let T=new ReadableStream({start(Z){for(let _ of H)Z.enqueue(_)},async pull(Z){let{done:_,value:B}=await N.read();if(_){Z.close();return}Z.enqueue(B)},cancel(Z){return N.cancel(Z)}});return I?.mark("inspect_stream_first_event",{ms:A(F()-x),bytes:O,rateLimited:!1}),{response:new Response(T,{status:Y.status,statusText:Y.statusText,headers:Y.headers}),rateLimited:!1}}async function _0(Y,I,x,N,H="api_fallback",K){let b=F(),O=w1(Y,I),T=O.get("x-session-affinity")||O.get("x-opencode-session"),Z=o(O);O.delete("x-parent-session-id"),O.delete("x-session-affinity"),O.delete("x-opencode-session");let _=I?.body,B=typeof _==="string"?_.length:void 0;if(_&&typeof _==="string"){let C=F(),L=(()=>{if(!z1())return!1;try{return S0(JSON.parse(_).model)}catch{return!1}})();if(_=await F1(_,{cache1hEnabled:!Z&&B0(),cache1hMode:F0(),fastModeEnabled:L,perf:(U,k)=>N?.mark(`rewrite_body_${U}`,{route:H,...k})}),P0(O,x),O.set("anthropic-beta",v0(O.get("anthropic-beta"),[])),L)t0(O);N?.mark("rewrite_body",{route:H,ms:A(F()-C),originalBytes:B,rewrittenBytes:_.length,cacheEnabled:!Z&&B0(),cacheMode:F0(),fastModeEnabled:L,subagent:Z})}else P0(O,x);let Q=B1(Y,{baseURL:x.baseURL}),l=F(),m;try{m=await fetch(Q.input,{...I,body:_,headers:O,...U1()&&{tls:{rejectUnauthorized:!1}}})}catch(C){if(typeof _==="string")await m0({affinity:T,route:H,error:wj(C),bodyText:_,url:Q.url?.toString()??j1(Q.input),method:J1(Y,I),headers:O});throw C}if(typeof _==="string")await m0({affinity:T,route:H,status:m.status,bodyText:_,url:Q.url?.toString()??j1(Q.input),method:J1(Y,I),headers:O});return N?.mark("send_headers_received",{route:H,ms:A(F()-l),status:m.status,relayConfigured:!1,totalSendWithAccessMs:A(F()-b),baseURL:x.baseURL}),m}async function u0(Y,I,x,N,H="unknown",K){let b=F(),O=K,T=async()=>{return O??=await h($),O},Z=w1(Y,I),_=Z.get("x-session-affinity")||Z.get("x-opencode-session"),B=o(Z);Z.delete("x-parent-session-id"),Z.delete("x-session-affinity"),Z.delete("x-opencode-session");let Q=I?.body,l;if(Q&&typeof Q==="string"){let i=F();try{let X0=JSON.parse(Q);if(typeof X0.model==="string")l=X0.model}catch{}N?.mark("model_parse_for_identity",{route:H,ms:A(F()-i),bodyBytes:Q.length})}let m=F(),C=await S1(x,l);N?.mark("resolve_claude_code_identity",{route:H,ms:A(F()-m),hasAccountUuid:Boolean(C.accountUuid)});let L=typeof Q==="string"?Q.length:void 0;if(Q&&typeof Q==="string"){let i=F(),X0=(()=>{if(!z1())return!1;try{return S0(JSON.parse(Q).model)}catch{return!1}})();Q=await F1(Q,{cache1hEnabled:!B&&B0(),cache1hMode:F0(),fastModeEnabled:X0,identity:C,perf:(PJ,OJ)=>N?.mark(`rewrite_body_${PJ}`,{route:H,...OJ})});let Q0=F();try{A0(Z,x,{body:JSON.parse(Q),identity:C}),N?.mark("set_oauth_headers_body_parse",{route:H,ms:A(F()-Q0),bodyBytes:Q.length,parsed:!0})}catch{A0(Z,x,{identity:C}),N?.mark("set_oauth_headers_body_parse",{route:H,ms:A(F()-Q0),bodyBytes:Q.length,parsed:!1})}if(X0)t0(Z);N?.mark("rewrite_body",{route:H,ms:A(F()-i),originalBytes:L,rewrittenBytes:Q.length,cacheEnabled:!B&&B0(),cacheMode:F0(),fastModeEnabled:X0,subagent:B})}let U=B1(Y);if(H==="main"&&typeof Q==="string"&&!B&&B0()&&F0()==="hybrid"){let i=await T(),X0=F(),Q0=S.track({sessionId:_,url:U.url?.toString()??U.input.toString(),headers:Z,bodyText:Q,storage:i,cacheMode:"hybrid"});N?.mark("cachekeep_track",{session:_,ms:A(F()-X0),tracked:Q0.tracked,reason:Q0.tracked?void 0:Q0.reason,bodyBytes:Q.length})}let k=async()=>{try{let i=await fetch(U.input,{...I,body:Q,headers:Z,...U1()&&{tls:{rejectUnauthorized:!1}}});if(typeof Q==="string")await m0({affinity:_,route:H,status:i.status,bodyText:Q,url:U.url?.toString()??j1(U.input),method:J1(Y,I),headers:Z});return i}catch(i){if(typeof Q==="string")await m0({affinity:_,route:H,error:wj(i),bodyText:Q,url:U.url?.toString()??j1(U.input),method:J1(Y,I),headers:Z});throw i}},t=y1(await T()),p=F(),c=await VJ({config:t,input:U.input,init:I,headers:Z,body:Q,fallback:k,affinity:_,optimisticResponse:t?.transport==="websocket"});return N?.mark("send_headers_received",{route:H,ms:A(F()-p),status:c.status,relayConfigured:t!=null,totalSendWithAccessMs:A(F()-b)}),c}async function KJ(Y){if(!Y)return!1;try{return await V.refreshMain(Y),Q1(Y)}catch{return!1}}async function c0(Y,I={}){let x=await z.getUsableFallbackAccounts(Y),N=new Map(x.map((K)=>[K.id,K])),H=[];for(let K of Y?.accounts??[]){if(e(K)){let b=N.get(K.id);if(b)H.push(b);continue}if(I.includeApiRoutes===!0&&k0(K)&&K.enabled!==!1&&K.apiKey&&T1(K.baseURL))H.push(K)}if(!h0(Y))return H;return H.filter((K)=>e(K)?X1(L0(K),Y,K.id):!0)}async function n0(Y,I,x,N,H,K,b){if(!x.length)return H??null;let O=b?.returnLastOnExhausted??!0;await H?.body?.cancel().catch(()=>{});let T=H??null;for(let[Z,_]of x.entries()){let B;if(k0(_)){if(!_.apiKey)continue;B=await _0(Y,I,_,K,`api_fallback_${Z}`,N)}else{let l=_.access;if(!l)continue;B=await u0(Y,I,l,K,`fallback_${Z}`,N)}T=B;let Q=R1(B.status,N);if(!Q){let l=await Z0(B,K);B=l.response,T=B,Q=l.rateLimited}if(!Q){if(await z.markUsed(_),b?.onSuccess?.(_),e(_)&&_.access&&V.shouldRefreshOnRequestCount(d))V.refreshFallback(_.id,_.access).then(()=>b?.onSuccess?.(_)).catch(()=>{});return B}if(Z<x.length-1||!O)await B.body?.cancel().catch(()=>{})}return O?T:null}async function YJ(Y,I,x,N,H,K,b,O){if(!R(Y,I?.body))return x;let T=F(),Z=K??await h($);if(H?.mark("fallback_load_storage",{ms:A(F()-T),cached:!!K}),!(Z?.accounts??[]).some((L)=>L.enabled!==!1&&(e(L)||k0(L)&&Boolean(L.apiKey)&&T1(L.baseURL))))return x;let B=x,Q=R1(B.status,Z),l=g1(B,!1);if(!Q){let L=await Z0(B,H);B=L.response,Q=L.rateLimited,l=g1(B,L.rateLimited)}if(!Q)return B;let m=!1;if(N)m=N.some(k0);else if(l)m=await KJ(b);let C=N;if(!C){let L=F();C=await c0(Z,{includeApiRoutes:m}),H?.mark("fallback_get_accounts",{ms:A(F()-L),accounts:C.length})}if(h0(Z)){let L=C.length;if(C=C.filter((U)=>e(U)?X1(L0(U),Z,U.id):!0),C.length<L)q("[killswitch] filtered fallbacks",{before:L,after:C.length})}return await n0(Y,I,C,Z,B,H,{onSuccess:O})??B}return{apiKey:"",async fetch(Y,I){let x=I?.body,N=eJ({bodyBytes:typeof x==="string"?x.length:void 0}),H=F(),K=await X();if(N.mark("get_auth",{ms:A(F()-H),authType:K.type,hasAccess:Boolean(K.access)}),K.type!=="oauth"){let L=await fetch(Y,I);return N.done("non_oauth_passthrough",{status:L.status}),L}await $0(K.refresh);let b=F(),O=await h();N.mark("load_storage",{ms:A(F()-b)}),V.updateStorage(O),V.seedMainFromStorage(O,K.access),V.seedFallbacksFromAccounts((O?.accounts??[]).filter(e));let T=R(Y,I?.body);if(T)d++;if(T&&K.access&&(!K.expires||K.expires>Date.now()))y(O,K.access);let Z=(L,U)=>u(O,{activeId:L,route:U,mainAccessToken:K.access,mainRefreshToken:K.refresh}),_;if(T&&b1(O)==="fallback-first")try{let L=F();_=await c0(O,{includeApiRoutes:Q1(K.access)}),N.mark("fallback_first_get_accounts",{ms:A(F()-L),accounts:_.length});let U=await n0(Y,I,_,O,void 0,N,{returnLastOnExhausted:!1,onSuccess:(k)=>Z(k.id,"fallback-first")});if(U)return N.done("return_fallback_first",{status:U.status}),f0(U,{perf:(k,t)=>N.mark(k,t)});_=void 0}catch(L){N.mark("fallback_first_error",{error:L instanceof Error?L.message:String(L)})}if(!K.access||!K.expires||K.expires<Date.now()){let U=(await h())?.refresh?.mainLastRefreshError;if(K.refresh&&U&&U0(U,K.refresh,Date.now()))throw q("[refresh] opencode main oauth request skipped backoff",{nextRetryAt:U.nextRetryAt,retryCount:U.retryCount,expiresInMs:K.expires?K.expires-Date.now():void 0}),Error(v1(U,Date.now()));q("[refresh] opencode main oauth refresh required for request",{hasAccess:Boolean(K.access),expiresInMs:K.expires?K.expires-Date.now():void 0,expiredAgoMs:K.expires&&K.expires<Date.now()?Date.now()-K.expires:void 0});let k=F();K.access=await P(),N.mark("refresh_main_access",{ms:A(F()-k)})}if(!K.access)throw N.done("missing_access_error"),Error("OAuth access token is missing after refresh");function B(){if(O?.quota?.showToasts!==!0)return;let L=V.getMain();if(!L)return;let U=(O?.accounts??[]).filter((p)=>p.enabled!==!1&&e(p)).map((p)=>({...p,quota:V.getFallback(p.id,p.access)?.quota??p.quota})),k=b0(L.quota,O),t;if(k)t="main";else t=U.find((p)=>b0(p.quota,O))?.id??"main";q0(L.quota,U,t)}if(T&&Y0(O))try{let L=F(),U=V.getMain(K.access),k=U?.quota;if(!k)k=await V.refreshMain(K.access),U=V.getMain(K.access),B();else if(V.needsRefresh(d))if(O0(k))k=await V.refreshMain(K.access),U=V.getMain(K.access);else V.refreshMain(K.access).then(()=>{j0(),B()}).catch(()=>{});if(u(O,{activeId:"main",route:"main",mainAccessToken:K.access,mainRefreshToken:K.refresh}),N.mark("main_quota_for_routing",{ms:A(F()-L),passes:b0(k,O)}),!b0(k,O)){let t=F();_=await c0(O,{includeApiRoutes:Boolean(U&&U.refreshAfter>Date.now()&&O0(U.quota))}),N.mark("preselect_fallback_accounts",{ms:A(F()-t),accounts:_.length});let p=await n0(Y,I,_,O,void 0,N,{onSuccess:(c)=>Z(c.id,"fallback")});if(p)return N.done("return_preselected_fallback",{status:p.status}),f0(p,{perf:(c,i)=>N.mark(c,i)})}}catch(L){N.mark("main_quota_for_routing_error",{error:L instanceof Error?L.message:String(L)})}let Q=V.getMain(K.access)?.quota;if(O?.quota?.failClosedOnUnknownQuota&&!Q&&V.isBackedOff()){let L=V.getLastApiError(),U=L?Aj(L,Date.now()):"Quota API unavailable";return q("[quota] blocked: quota API backed off (failClosed)",{nextRetryAt:L?.nextRetryAt,retryCount:L?.retryCount}),new Response(JSON.stringify({type:"error",error:{type:"rate_limit_error",message:U}}),{status:429,headers:{"content-type":"application/json","retry-after":String(L?.nextRetryAt?Math.max(1,Math.ceil((L.nextRetryAt-Date.now())/1000)):60)}})}if(h0(O)){if(V.needsRefresh(d))try{let U=(O?.accounts??[]).filter((k)=>k.enabled!==!1&&e(k)&&Boolean(k.access));await Promise.all([V.refreshMain(K.access),V.refreshAllFallbacks(U)])}catch(U){q("[quota] killswitch refresh failed",{error:U instanceof Error?U.message:String(U),backedOff:V.isBackedOff()})}Q=V.getMain(K.access)?.quota}if(h0(O)&&!X1(Q,O)){let U=R(Y,I?.body)?await c0(O,{includeApiRoutes:Q1(K.access)}):[];if(U.length>0){q("[route] skipping main (killswitch), trying fallbacks");let c=await n0(Y,I,U,O,void 0,N,{onSuccess:(i)=>Z(i.id,"fallback")});if(c)return N.done("return_killswitch_fallback",{status:c.status}),f0(c,{perf:(i,X0)=>N.mark(i,X0)})}let k=Date.now(),t=(O?.accounts??[]).filter((c)=>c.enabled!==!1&&e(c)).map((c)=>({...c,quota:L0(c)})),p=kj(Q,t,k);return new Response(JSON.stringify({type:"error",error:{type:"rate_limit_error",message:`Killswitch: no routable accounts. Retry in ${Math.floor(p/60)}m ${p%60}s.`}}),{status:429,headers:{"content-type":"application/json","retry-after":String(p)}})}let l=await u0(Y,I,K.access,N,"main",O),m=!1,C=await YJ(Y,I,l,_,N,O,K.access,(L)=>{m=!0,Z(L.id,"fallback")});if(!m)Z("main","main");return N.done("return_response",{status:C.status}),f0(C,{perf:(L,U)=>N.mark(L,U)})}}}return{}},methods:[{label:"Claude Pro/Max",type:"oauth",authorize:async()=>{let X=await H1("max");return{url:X.url,instructions:"Paste the authorization code here:",method:"code",callback:async(G)=>{return x1(G,X.verifier,X.redirectUri,X.state)}}}},{label:"Create an API Key",type:"oauth",authorize:async()=>{let X=await H1("console");return{url:X.url,instructions:"Paste the authorization code here:",method:"code",callback:async(G)=>{let D=await x1(G,X.verifier,X.redirectUri,X.state);if(D.type==="failed")return D;return{type:"success",key:(await fetch("https://api.anthropic.com/api/oauth/claude_cli/create_api_key",{method:"POST",headers:{"Content-Type":"application/json",authorization:`Bearer ${D.access}`}}).then((P)=>P.json())).raw_key}}}}},{provider:"anthropic",label:"Manually enter API Key",type:"api"}]}}};export{_3 as AnthropicAuthPlugin};
|
|
16
|
+
`)&&p.length<65536){let{done:V,value:w}=await z.read();if(V)break;if(x.push(w),_+=w.byteLength,p+=X.decode(w,{stream:!0}),$0(p))break}if($0(p))return await z.cancel().catch(()=>{}),D?.mark("inspect_stream_first_event",{ms:h(F()-q),bytes:_,rateLimited:!0}),{response:Z,rateLimited:!0};let R=new ReadableStream({start(V){for(let w of x)V.enqueue(w)},async pull(V){let{done:w,value:B}=await z.read();if(w){V.close();return}V.enqueue(B)},cancel(V){return z.cancel(V)}});return D?.mark("inspect_stream_first_event",{ms:h(F()-q),bytes:_,rateLimited:!1}),{response:new Response(R,{status:Z.status,statusText:Z.statusText,headers:Z.headers}),rateLimited:!1}}async function B0(Z,D,q,z,x="api_fallback",X){let p=F(),_=M1(Z,D),R=_.get("x-session-affinity")||_.get("x-opencode-session"),V=y(_);_.delete("x-parent-session-id"),_.delete("x-session-affinity"),_.delete("x-opencode-session");let w=D?.body,B=typeof w==="string"?w.length:void 0;if(w&&typeof w==="string"){let C=F(),Q=(()=>{if(!Z1())return!1;try{return A0(JSON.parse(w).model)}catch{return!1}})();if(w=await F1(w,{cache1hEnabled:!V&&q0(),cache1hMode:R0(),fastModeEnabled:Q,perf:(U,g)=>z?.mark(`rewrite_body_${U}`,{route:x,...g})}),_0(_,q),_.set("anthropic-beta",v0(_.get("anthropic-beta"),[])),Q)W1(_);z?.mark("rewrite_body",{route:x,ms:h(F()-C),originalBytes:B,rewrittenBytes:w.length,cacheEnabled:!V&&q0(),cacheMode:R0(),fastModeEnabled:Q,subagent:V})}else _0(_,q);let M=B1(Z,{baseURL:q.baseURL}),c=F(),n;try{n=await fetch(M.input,{...D,body:w,headers:_,...U1()&&{tls:{rejectUnauthorized:!1}}})}catch(C){if(typeof w==="string")await d0({affinity:R,route:x,error:AW(C),bodyText:w,url:M.url?.toString()??J1(M.input),method:P1(Z,D),headers:_});throw C}if(typeof w==="string")await d0({affinity:R,route:x,status:n.status,bodyText:w,url:M.url?.toString()??J1(M.input),method:P1(Z,D),headers:_});return z?.mark("send_headers_received",{route:x,ms:h(F()-c),status:n.status,relayConfigured:!1,totalSendWithAccessMs:h(F()-p),baseURL:q.baseURL}),n}async function c0(Z,D,q,z,x="unknown",X){let p=F(),_=X,R=async()=>{return _??=await A(j),_},V=M1(Z,D),w=V.get("x-session-affinity")||V.get("x-opencode-session"),B=y(V);V.delete("x-parent-session-id"),V.delete("x-session-affinity"),V.delete("x-opencode-session");let M=D?.body,c;if(M&&typeof M==="string"){let o=F();try{let G0=JSON.parse(M);if(typeof G0.model==="string")c=G0.model}catch{}z?.mark("model_parse_for_identity",{route:x,ms:h(F()-o),bodyBytes:M.length})}let n=F(),C=await S1(q,c);z?.mark("resolve_claude_code_identity",{route:x,ms:h(F()-n),hasAccountUuid:Boolean(C.accountUuid)});let Q=typeof M==="string"?M.length:void 0;if(M&&typeof M==="string"){let o=F(),G0=(()=>{if(!Z1())return!1;try{return A0(JSON.parse(M).model)}catch{return!1}})();M=await F1(M,{cache1hEnabled:!B&&q0(),cache1hMode:R0(),fastModeEnabled:G0,identity:C,perf:(F4,C4)=>z?.mark(`rewrite_body_${F4}`,{route:x,...C4})});let C0=F();try{k0(V,q,{body:JSON.parse(M),identity:C}),z?.mark("set_oauth_headers_body_parse",{route:x,ms:h(F()-C0),bodyBytes:M.length,parsed:!0})}catch{k0(V,q,{identity:C}),z?.mark("set_oauth_headers_body_parse",{route:x,ms:h(F()-C0),bodyBytes:M.length,parsed:!1})}if(G0)W1(V);z?.mark("rewrite_body",{route:x,ms:h(F()-o),originalBytes:Q,rewrittenBytes:M.length,cacheEnabled:!B&&q0(),cacheMode:R0(),fastModeEnabled:G0,subagent:B})}let U=B1(Z);if(x==="main"&&typeof M==="string"&&!B&&q0()&&R0()==="hybrid"){let o=await R(),G0=F(),C0=H.track({sessionId:w,url:U.url?.toString()??U.input.toString(),headers:V,bodyText:M,storage:o,cacheMode:"hybrid"});z?.mark("cachekeep_track",{session:w,ms:h(F()-G0),tracked:C0.tracked,reason:C0.tracked?void 0:C0.reason,bodyBytes:M.length})}let g=async()=>{try{let o=await fetch(U.input,{...D,body:M,headers:V,...U1()&&{tls:{rejectUnauthorized:!1}}});if(typeof M==="string")await d0({affinity:w,route:x,status:o.status,bodyText:M,url:U.url?.toString()??J1(U.input),method:P1(Z,D),headers:V});return o}catch(o){if(typeof M==="string")await d0({affinity:w,route:x,error:AW(o),bodyText:M,url:U.url?.toString()??J1(U.input),method:P1(Z,D),headers:V});throw o}},e=i1(await R()),u=F(),t=await I4({config:e,input:U.input,init:D,headers:V,body:M,fallback:g,affinity:w,optimisticResponse:e?.transport==="websocket"});return z?.mark("send_headers_received",{route:x,ms:h(F()-u),status:t.status,relayConfigured:e!=null,totalSendWithAccessMs:h(F()-p)}),t}async function U4(Z){if(!Z)return!1;try{return await P.refreshMain(Z),z1(Z)}catch{return!1}}async function o0(Z,D={}){let q=await N.getUsableFallbackAccounts(Z),z=new Map(q.map((X)=>[X.id,X])),x=[];for(let X of Z?.accounts??[]){if(W0(X)){let p=z.get(X.id);if(p)x.push(p);continue}if(D.includeApiRoutes===!0&&h0(X)&&X.enabled!==!1&&X.apiKey&&x1(X.baseURL))x.push(X)}if(!i0(Z))return x;return x.filter((X)=>W0(X)?N1(F0(X),Z,X.id):!0)}async function r0(Z,D,q,z,x,X,p){if(!q.length)return x??null;let _=p?.returnLastOnExhausted??!0;await x?.body?.cancel().catch(()=>{});let R=x??null;for(let[V,w]of q.entries()){let B;if(h0(w)){if(!w.apiKey)continue;B=await B0(Z,D,w,X,`api_fallback_${V}`,z)}else{let c=w.access;if(!c)continue;B=await c0(Z,D,c,X,`fallback_${V}`,z)}R=B;let M=q1(B.status,z);if(!M){let c=await V0(B,X);B=c.response,R=B,M=c.rateLimited}if(!M){if(await N.markUsed(w),p?.onSuccess?.(w),W0(w)&&w.access&&P.shouldRefreshOnRequestCount(j0))P.refreshFallback(w.id,w.access).then(()=>p?.onSuccess?.(w)).catch(()=>{});return B}if(V<q.length-1||!_)await B.body?.cancel().catch(()=>{})}return _?R:null}async function B4(Z,D,q,z,x,X,p,_){if(!f(Z,D?.body))return q;let R=F(),V=X??await A(j);if(x?.mark("fallback_load_storage",{ms:h(F()-R),cached:!!X}),!(V?.accounts??[]).some((Q)=>Q.enabled!==!1&&(W0(Q)||h0(Q)&&Boolean(Q.apiKey)&&x1(Q.baseURL))))return q;let B=q,M=q1(B.status,V),c=n1(B,!1);if(!M){let Q=await V0(B,x);B=Q.response,M=Q.rateLimited,c=n1(B,Q.rateLimited)}if(!M)return B;let n=!1;if(z)n=z.some(h0);else if(c)n=await U4(p);let C=z;if(!C){let Q=F();C=await o0(V,{includeApiRoutes:n}),x?.mark("fallback_get_accounts",{ms:h(F()-Q),accounts:C.length})}if(i0(V)){let Q=C.length;if(C=C.filter((U)=>W0(U)?N1(F0(U),V,U.id):!0),C.length<Q)E("[killswitch] filtered fallbacks",{before:Q,after:C.length})}return await r0(Z,D,C,V,B,x,{onSuccess:_})??B}return{apiKey:"",async fetch(Z,D){let q=D?.body,z=Cj({bodyBytes:typeof q==="string"?q.length:void 0}),x=F(),X=await O();if(z.mark("get_auth",{ms:h(F()-x),authType:X.type,hasAccess:Boolean(X.access)}),X.type!=="oauth"){let Q=await fetch(Z,D);return z.done("non_oauth_passthrough",{status:Q.status}),Q}await J0(X.refresh);let p=F(),_=await A();z.mark("load_storage",{ms:h(F()-p)}),P.updateStorage(_),P.seedMainFromStorage(_,X.access),P.seedFallbacksFromAccounts((_?.accounts??[]).filter(W0));let R=f(Z,D?.body);if(R)j0++;if(R&&X.access&&(!X.expires||X.expires>Date.now()))r(_,X.access);let V=(Q,U)=>m(_,{activeId:Q,route:U,mainAccessToken:X.access,mainRefreshToken:X.refresh}),w;if(R&&Y1(_)==="fallback-first")try{let Q=F();w=await o0(_,{includeApiRoutes:z1(X.access)}),z.mark("fallback_first_get_accounts",{ms:h(F()-Q),accounts:w.length});let U=await r0(Z,D,w,_,void 0,z,{returnLastOnExhausted:!1,onSuccess:(g)=>V(g.id,"fallback-first")});if(U)return z.done("return_fallback_first",{status:U.status}),y0(U,{perf:(g,e)=>z.mark(g,e)});w=void 0}catch(Q){z.mark("fallback_first_error",{error:Q instanceof Error?Q.message:String(Q)})}if(!X.access||!X.expires||X.expires<Date.now()){let U=(await A())?.refresh?.mainLastRefreshError;if(X.refresh&&U&&x0(U,X.refresh,Date.now()))throw E("[refresh] opencode main oauth request skipped backoff",{nextRetryAt:U.nextRetryAt,retryCount:U.retryCount,expiresInMs:X.expires?X.expires-Date.now():void 0}),Error(R1(U,Date.now()));E("[refresh] opencode main oauth refresh required for request",{hasAccess:Boolean(X.access),expiresInMs:X.expires?X.expires-Date.now():void 0,expiredAgoMs:X.expires&&X.expires<Date.now()?Date.now()-X.expires:void 0});let g=F();X.access=await K(),z.mark("refresh_main_access",{ms:h(F()-g)})}if(!X.access)throw z.done("missing_access_error"),Error("OAuth access token is missing after refresh");function B(){if(_?.quota?.showToasts!==!0)return;let Q=P.getMain();if(!Q)return;let U=(_?.accounts??[]).filter((u)=>u.enabled!==!1&&W0(u)).map((u)=>({...u,quota:P.getFallback(u.id,u.access)?.quota??u.quota})),g=p0(Q.quota,_),e;if(g)e="main";else e=U.find((u)=>p0(u.quota,_))?.id??"main";u0(Q.quota,U,e)}if(R&&Z0(_))try{let Q=F(),U=P.getMain(X.access),g=U?.quota;if(!g)g=await P.refreshMain(X.access),U=P.getMain(X.access),B();else if(P.needsRefresh(j0))if(D0(g))g=await P.refreshMain(X.access),U=P.getMain(X.access);else P.refreshMain(X.access).then(()=>{S(),B()}).catch(()=>{});if(m(_,{activeId:"main",route:"main",mainAccessToken:X.access,mainRefreshToken:X.refresh}),z.mark("main_quota_for_routing",{ms:h(F()-Q),passes:p0(g,_)}),!p0(g,_)){let e=F();w=await o0(_,{includeApiRoutes:Boolean(U&&U.refreshAfter>Date.now()&&D0(U.quota))}),z.mark("preselect_fallback_accounts",{ms:h(F()-e),accounts:w.length});let u=await r0(Z,D,w,_,void 0,z,{onSuccess:(t)=>V(t.id,"fallback")});if(u)return z.done("return_preselected_fallback",{status:u.status}),y0(u,{perf:(t,o)=>z.mark(t,o)})}}catch(Q){z.mark("main_quota_for_routing_error",{error:Q instanceof Error?Q.message:String(Q)})}let M=P.getMain(X.access)?.quota;if(_?.quota?.failClosedOnUnknownQuota&&!M&&P.isBackedOff()){let Q=P.getLastApiError(),U=Q?cW(Q,Date.now()):"Quota API unavailable";return E("[quota] blocked: quota API backed off (failClosed)",{nextRetryAt:Q?.nextRetryAt,retryCount:Q?.retryCount}),new Response(JSON.stringify({type:"error",error:{type:"rate_limit_error",message:U}}),{status:429,headers:{"content-type":"application/json","retry-after":String(Q?.nextRetryAt?Math.max(1,Math.ceil((Q.nextRetryAt-Date.now())/1000)):60)}})}if(i0(_)){if(P.needsRefresh(j0))try{let U=(_?.accounts??[]).filter((g)=>g.enabled!==!1&&W0(g)&&Boolean(g.access));await Promise.all([P.refreshMain(X.access),P.refreshAllFallbacks(U)])}catch(U){E("[quota] killswitch refresh failed",{error:U instanceof Error?U.message:String(U),backedOff:P.isBackedOff()})}M=P.getMain(X.access)?.quota}if(i0(_)&&!N1(M,_)){let U=f(Z,D?.body)?await o0(_,{includeApiRoutes:z1(X.access)}):[];if(U.length>0){E("[route] skipping main (killswitch), trying fallbacks");let t=await r0(Z,D,U,_,void 0,z,{onSuccess:(o)=>V(o.id,"fallback")});if(t)return z.done("return_killswitch_fallback",{status:t.status}),y0(t,{perf:(o,G0)=>z.mark(o,G0)})}let g=Date.now(),e=(_?.accounts??[]).filter((t)=>t.enabled!==!1&&W0(t)).map((t)=>({...t,quota:F0(t)})),u=rW(M,e,g);return new Response(JSON.stringify({type:"error",error:{type:"rate_limit_error",message:`Killswitch: no routable accounts. Retry in ${Math.floor(u/60)}m ${u%60}s.`}}),{status:429,headers:{"content-type":"application/json","retry-after":String(u)}})}let c=await c0(Z,D,X.access,z,"main",_),n=!1,C=await B4(Z,D,c,w,z,_,X.access,(Q)=>{n=!0,V(Q.id,"fallback")});if(!n)V("main","main");return z.done("return_response",{status:C.status}),y0(C,{perf:(Q,U)=>z.mark(Q,U)})}}}return{}},methods:[{label:"Claude Pro/Max",type:"oauth",authorize:async()=>{let O=await H1("max");return{url:O.url,instructions:"Paste the authorization code here:",method:"code",callback:async(G)=>{return f1(G,O.verifier,O.redirectUri,O.state)}}}},{label:"Create an API Key",type:"oauth",authorize:async()=>{let O=await H1("console");return{url:O.url,instructions:"Paste the authorization code here:",method:"code",callback:async(G)=>{let Y=await f1(G,O.verifier,O.redirectUri,O.state);if(Y.type==="failed")return Y;return{type:"success",key:(await fetch("https://api.anthropic.com/api/oauth/claude_cli/create_api_key",{method:"POST",headers:{"Content-Type":"application/json",authorization:`Bearer ${Y.access}`}}).then((K)=>K.json())).raw_key}}}}},{provider:"anthropic",label:"Manually enter API Key",type:"api"}]}}};export{ej as AnthropicAuthPlugin};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { OpenDialogPayload, RpcNotification } from './protocol';
|
|
2
|
+
export declare function pushNotification(payload: OpenDialogPayload, sessionId?: string): void;
|
|
3
|
+
export declare function drainNotifications(lastReceivedId?: number, sessionId?: string): RpcNotification[];
|
|
4
|
+
export declare function isTuiConnected(sessionId?: string): boolean;
|
|
5
|
+
export declare function resetNotificationsForTest(): void;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface PortFileEntry {
|
|
2
|
+
port: number;
|
|
3
|
+
token: string;
|
|
4
|
+
pid: number;
|
|
5
|
+
startedAt: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function writePortFile(dir: string, entry: {
|
|
8
|
+
port: number;
|
|
9
|
+
token: string;
|
|
10
|
+
pid: number;
|
|
11
|
+
}): Promise<string>;
|
|
12
|
+
export declare function discoverPortFile(dir: string): Promise<PortFileEntry | null>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{db as a,eb as b}from"../index-jb14nj5c.js";import"../index-p8j7mmfc.js";export{a as writePortFile,b as discoverPortFile};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type CommandModalName = 'claude-cache' | 'claude-cachekeep' | 'claude-quota' | 'claude-dump' | 'claude-fast' | 'claude-routing' | 'claude-killswitch';
|
|
2
|
+
export interface OpenDialogPayload {
|
|
3
|
+
command: CommandModalName;
|
|
4
|
+
text: string;
|
|
5
|
+
knobs: Record<string, unknown>;
|
|
6
|
+
}
|
|
7
|
+
export interface RpcNotification {
|
|
8
|
+
id: number;
|
|
9
|
+
type: 'open-dialog';
|
|
10
|
+
payload: OpenDialogPayload;
|
|
11
|
+
sessionId?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface ApplyRequest {
|
|
14
|
+
command: CommandModalName;
|
|
15
|
+
arguments: string;
|
|
16
|
+
}
|
|
17
|
+
export interface ApplyResult {
|
|
18
|
+
text: string;
|
|
19
|
+
knobs: Record<string, unknown>;
|
|
20
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ApplyRequest, ApplyResult, RpcNotification } from './protocol';
|
|
2
|
+
export interface RpcClient {
|
|
3
|
+
pending: (lastReceivedId: number, sessionId?: string) => Promise<RpcNotification[]>;
|
|
4
|
+
apply: (request: ApplyRequest) => Promise<ApplyResult>;
|
|
5
|
+
}
|
|
6
|
+
export declare function createRpcClient(dir: string): RpcClient;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{eb as a}from"../index-jb14nj5c.js";import"../index-p8j7mmfc.js";async function s(e,t,n){let o=await a(e);if(!o)return null;let i=new AbortController,l=setTimeout(()=>i.abort(),2000);try{let r=await fetch(`http://127.0.0.1:${o.port}/rpc/${t}`,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${o.token}`},body:JSON.stringify(n),signal:i.signal});if(!r.ok)return null;return await r.json()}catch{return null}finally{clearTimeout(l)}}function c(e){return{async pending(t,n){return(await s(e,"pending-notifications",{lastReceivedId:t,sessionId:n}))?.messages??[]},async apply(t){return await s(e,"apply",{...t})??{text:"apply failed",knobs:{}}}}}export{c as createRpcClient};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{fb as a,gb as b}from"../index-3ynbghx3.js";import"../index-p8j7mmfc.js";export{a as tmpdir,b as getRpcDir};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { drainNotifications } from './notifications';
|
|
2
|
+
import type { ApplyRequest, ApplyResult } from './protocol';
|
|
3
|
+
export interface RpcServerHandle {
|
|
4
|
+
port: number;
|
|
5
|
+
token: string;
|
|
6
|
+
stop: () => Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
export interface RpcServerOptions {
|
|
9
|
+
dir: string;
|
|
10
|
+
drain: typeof drainNotifications;
|
|
11
|
+
apply: (request: ApplyRequest) => Promise<ApplyResult>;
|
|
12
|
+
}
|
|
13
|
+
export declare function startRpcServer(options: RpcServerOptions): Promise<RpcServerHandle>;
|
package/dist/sidebar-state.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{$a as f,Wa as a,Xa as b,Ya as c,Za as d,_a as e,ab as g,bb as h,cb as i}from"./index-9yjbmhf1.js";import"./index-
|
|
1
|
+
import{$a as f,Wa as a,Xa as b,Ya as c,Za as d,_a as e,ab as g,bb as h,cb as i}from"./index-9yjbmhf1.js";import"./index-p8j7mmfc.js";export{d as setSidebarState,e as resolveActiveAccount,a as getSidebarStateFile,c as getSidebarState,f as getCollapsedQuotaSummary,i as computeQuotaPacing,h as SEVEN_DAY_MS,g as FIVE_HOUR_MS,b as DEFAULT_SIDEBAR_STATE};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** @jsxImportSource @opentui/solid */
|
|
2
|
+
import type { TuiPluginApi } from '@opencode-ai/plugin/tui';
|
|
3
|
+
import type { OpenDialogPayload } from '../rpc/protocol.js';
|
|
4
|
+
type ApplyFn = (command: OpenDialogPayload['command'], args: string) => Promise<{
|
|
5
|
+
text: string;
|
|
6
|
+
}>;
|
|
7
|
+
export declare function openCommandDialog(api: TuiPluginApi, payload: OpenDialogPayload, apply: ApplyFn): void;
|
|
8
|
+
export {};
|
package/dist/tui-preferences.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{hb as P,ib as C,jb as _}from"./index-p8j7mmfc.js";var g=C((T,L)=>{(function(Q){if(typeof L==="object"&&typeof L.exports==="object"){var J=Q(_,T);if(J!==void 0)L.exports=J}else if(typeof define==="function"&&define.amd)define(["require","exports","./impl/format","./impl/edit","./impl/scanner","./impl/parser"],Q)})(function(Q,J){Object.defineProperty(J,"__esModule",{value:!0}),J.applyEdits=J.modify=J.format=J.printParseErrorCode=J.ParseErrorCode=J.stripComments=J.visit=J.getNodeValue=J.getNodePath=J.findNodeAtOffset=J.findNodeAtLocation=J.parseTree=J.parse=J.getLocation=J.SyntaxKind=J.ScanError=J.createScanner=void 0;let X=Q("./impl/format"),Z=Q("./impl/edit"),G=Q("./impl/scanner"),$=Q("./impl/parser");J.createScanner=G.createScanner;var W;(function(z){z[z.None=0]="None",z[z.UnexpectedEndOfComment=1]="UnexpectedEndOfComment",z[z.UnexpectedEndOfString=2]="UnexpectedEndOfString",z[z.UnexpectedEndOfNumber=3]="UnexpectedEndOfNumber",z[z.InvalidUnicode=4]="InvalidUnicode",z[z.InvalidEscapeCharacter=5]="InvalidEscapeCharacter",z[z.InvalidCharacter=6]="InvalidCharacter"})(W||(J.ScanError=W={}));var H;(function(z){z[z.OpenBraceToken=1]="OpenBraceToken",z[z.CloseBraceToken=2]="CloseBraceToken",z[z.OpenBracketToken=3]="OpenBracketToken",z[z.CloseBracketToken=4]="CloseBracketToken",z[z.CommaToken=5]="CommaToken",z[z.ColonToken=6]="ColonToken",z[z.NullKeyword=7]="NullKeyword",z[z.TrueKeyword=8]="TrueKeyword",z[z.FalseKeyword=9]="FalseKeyword",z[z.StringLiteral=10]="StringLiteral",z[z.NumericLiteral=11]="NumericLiteral",z[z.LineCommentTrivia=12]="LineCommentTrivia",z[z.BlockCommentTrivia=13]="BlockCommentTrivia",z[z.LineBreakTrivia=14]="LineBreakTrivia",z[z.Trivia=15]="Trivia",z[z.Unknown=16]="Unknown",z[z.EOF=17]="EOF"})(H||(J.SyntaxKind=H={})),J.getLocation=$.getLocation,J.parse=$.parse,J.parseTree=$.parseTree,J.findNodeAtLocation=$.findNodeAtLocation,J.findNodeAtOffset=$.findNodeAtOffset,J.getNodePath=$.getNodePath,J.getNodeValue=$.getNodeValue,J.visit=$.visit,J.stripComments=$.stripComments;var q;(function(z){z[z.InvalidSymbol=1]="InvalidSymbol",z[z.InvalidNumberFormat=2]="InvalidNumberFormat",z[z.PropertyNameExpected=3]="PropertyNameExpected",z[z.ValueExpected=4]="ValueExpected",z[z.ColonExpected=5]="ColonExpected",z[z.CommaExpected=6]="CommaExpected",z[z.CloseBraceExpected=7]="CloseBraceExpected",z[z.CloseBracketExpected=8]="CloseBracketExpected",z[z.EndOfFileExpected=9]="EndOfFileExpected",z[z.InvalidCommentToken=10]="InvalidCommentToken",z[z.UnexpectedEndOfComment=11]="UnexpectedEndOfComment",z[z.UnexpectedEndOfString=12]="UnexpectedEndOfString",z[z.UnexpectedEndOfNumber=13]="UnexpectedEndOfNumber",z[z.InvalidUnicode=14]="InvalidUnicode",z[z.InvalidEscapeCharacter=15]="InvalidEscapeCharacter",z[z.InvalidCharacter=16]="InvalidCharacter"})(q||(J.ParseErrorCode=q={}));function D(z){switch(z){case 1:return"InvalidSymbol";case 2:return"InvalidNumberFormat";case 3:return"PropertyNameExpected";case 4:return"ValueExpected";case 5:return"ColonExpected";case 6:return"CommaExpected";case 7:return"CloseBraceExpected";case 8:return"CloseBracketExpected";case 9:return"EndOfFileExpected";case 10:return"InvalidCommentToken";case 11:return"UnexpectedEndOfComment";case 12:return"UnexpectedEndOfString";case 13:return"UnexpectedEndOfNumber";case 14:return"InvalidUnicode";case 15:return"InvalidEscapeCharacter";case 16:return"InvalidCharacter"}return"<unknown ParseErrorCode>"}J.printParseErrorCode=D;function m(z,U,j){return X.format(z,U,j)}J.format=m;function f(z,U,j,A){return Z.setProperty(z,U,j,A)}J.modify=f;function u(z,U){let j=U.slice(0).sort((I,Y)=>{let F=I.offset-Y.offset;if(F===0)return I.length-Y.length;return F}),A=z.length;for(let I=j.length-1;I>=0;I--){let Y=j[I];if(Y.offset+Y.length<=A)z=Z.applyEdit(z,Y);else throw Error("Overlapping edit");A=Y.offset}return z}J.applyEdits=u})});var M=P(g(),1);import{watch as K}from"node:fs";import{mkdir as c,readFile as O,rename as y,writeFile as S}from"node:fs/promises";import{homedir as E}from"node:os";import{basename as l,dirname as v,join as k}from"node:path";var p="OPENCODE_TUI_PREFERENCES_FILE",x="tui-preferences.jsonc";function w(){let Q=process.env[p];if(Q)return Q;let J=process.env.OPENCODE_CONFIG_DIR||k(process.env.XDG_CONFIG_HOME||k(E(),".config"),"opencode");return k(J,x)}function N(Q){return typeof Q==="object"&&Q!==null&&!Array.isArray(Q)}async function Xz(){try{let Q=await O(w(),"utf8"),J=[],X=M.parse(Q,J,{allowTrailingComma:!0});if(J.length>0)return{};return N(X)?X:{}}catch{return{}}}var i="anthropic-auth",s=160,b={forceToTop:!1,order:s,startCollapsed:!1,rememberCollapsed:!0,collapsed:null,pollMs:1500,refreshDebounceMs:200,header:{label:"CLAUDE",showVersion:!0},sections:{quota:!0,fallbackAccounts:!0,routing:!0,cache:!0,health:!0,pacing:!0},appearance:{barWidth:10,barFilledChar:"█",barEmptyChar:"░",warnThreshold:50,errorThreshold:80}};function V(Q,J){return typeof Q==="boolean"?Q:J}function B(Q,J,X,Z){if(typeof Q!=="number"||!Number.isFinite(Q))return J;return Math.min(Math.max(Math.round(Q),X),Z)}function d(Q,J,X){if(typeof Q!=="string"||Q.length===0)return J;return Q.slice(0,X)}function h(Q,J){if(typeof Q!=="string")return J;return[...Q][0]??J}function Zz(Q){let J=Q[i];if(!N(J))return structuredClone(b);let X=b,Z=N(J.header)?J.header:{},G=N(J.sections)?J.sections:{},$=N(J.appearance)?J.appearance:{},W=B($.warnThreshold,X.appearance.warnThreshold,0,99),H=Math.max(B($.errorThreshold,X.appearance.errorThreshold,0,100),Math.min(W+1,100));return{forceToTop:V(J.forceToTop,X.forceToTop),order:B(J.order,X.order,-1e4,1e4),startCollapsed:V(J.startCollapsed,X.startCollapsed),rememberCollapsed:V(J.rememberCollapsed,X.rememberCollapsed),collapsed:typeof J.collapsed==="boolean"?J.collapsed:null,pollMs:B(J.pollMs,X.pollMs,500,30000),refreshDebounceMs:B(J.refreshDebounceMs,X.refreshDebounceMs,50,5000),header:{label:d(Z.label,X.header.label,20),showVersion:V(Z.showVersion,X.header.showVersion)},sections:{quota:V(G.quota,X.sections.quota),fallbackAccounts:V(G.fallbackAccounts,X.sections.fallbackAccounts),routing:V(G.routing,X.sections.routing),cache:V(G.cache,X.sections.cache),health:V(G.health,X.sections.health),pacing:V(G.pacing,X.sections.pacing)},appearance:{barWidth:B($.barWidth,X.appearance.barWidth,4,40),barFilledChar:h($.barFilledChar,X.appearance.barFilledChar),barEmptyChar:h($.barEmptyChar,X.appearance.barEmptyChar),warnThreshold:W,errorThreshold:H}}}var n=-1e5;function $z(Q,J,X){let Z=Q[J];if(!N(Z))return X;if(Z.forceToTop===!0)return n+Object.keys(Q).indexOf(J);return B(Z.order,X,-1e4,1e4)}var t=`// Shared preferences for opencode TUI plugins.
|
|
2
2
|
// One top-level key per plugin (short name). See each plugin's README for
|
|
3
3
|
// its supported settings. This file is safe to hand-edit; plugins update
|
|
4
4
|
// individual keys in place and preserve comments.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cortexkit/opencode-anthropic-auth",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -36,13 +36,15 @@
|
|
|
36
36
|
"files": [
|
|
37
37
|
"dist",
|
|
38
38
|
"src/tui.tsx",
|
|
39
|
+
"src/tui/command-dialogs.tsx",
|
|
39
40
|
"src/sidebar-state.ts",
|
|
40
41
|
"src/tui-preferences.ts",
|
|
42
|
+
"src/rpc",
|
|
41
43
|
"README.md",
|
|
42
44
|
"LICENSE"
|
|
43
45
|
],
|
|
44
46
|
"scripts": {
|
|
45
|
-
"build": "rm -rf dist && bun build src/index.ts src/cli.ts src/sidebar-state.ts src/tui-preferences.ts --outdir dist --target node --format esm --splitting --external @opencode-ai/plugin --minify && tsc -p tsconfig.build.json --emitDeclarationOnly",
|
|
47
|
+
"build": "rm -rf dist && bun build src/index.ts src/cli.ts src/sidebar-state.ts src/tui-preferences.ts src/rpc/rpc-client.ts src/rpc/port-file.ts src/rpc/protocol.ts src/rpc/rpc-dir.ts --outdir dist --target node --format esm --splitting --external @opencode-ai/plugin --minify && tsc -p tsconfig.build.json --emitDeclarationOnly",
|
|
46
48
|
"build:dev": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
47
49
|
"dev": "bun ../../scripts/dev.ts",
|
|
48
50
|
"dev:clean": "bun ../../scripts/dev-clean.ts",
|
|
@@ -56,7 +58,7 @@
|
|
|
56
58
|
"@opencode-ai/plugin": "*"
|
|
57
59
|
},
|
|
58
60
|
"dependencies": {
|
|
59
|
-
"@cortexkit/anthropic-auth-core": "1.
|
|
61
|
+
"@cortexkit/anthropic-auth-core": "1.10.1",
|
|
60
62
|
"@opentui/core": ">=0.4.0",
|
|
61
63
|
"@opentui/solid": ">=0.4.0",
|
|
62
64
|
"jsonc-parser": "^3.3.1",
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { OpenDialogPayload, RpcNotification } from './protocol'
|
|
2
|
+
|
|
3
|
+
const QUEUE_CAP = 100
|
|
4
|
+
const TUI_CONNECTED_WINDOW_MS = 3_000
|
|
5
|
+
|
|
6
|
+
let queue: RpcNotification[] = []
|
|
7
|
+
let nextId = 1
|
|
8
|
+
let lastDrainAtAny = 0
|
|
9
|
+
const lastDrainAtBySession = new Map<string, number>()
|
|
10
|
+
|
|
11
|
+
export function pushNotification(
|
|
12
|
+
payload: OpenDialogPayload,
|
|
13
|
+
sessionId?: string,
|
|
14
|
+
): void {
|
|
15
|
+
queue.push({ id: nextId++, type: 'open-dialog', payload, sessionId })
|
|
16
|
+
if (queue.length > QUEUE_CAP) queue = queue.slice(queue.length - QUEUE_CAP)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function drainNotifications(
|
|
20
|
+
lastReceivedId = 0,
|
|
21
|
+
sessionId?: string,
|
|
22
|
+
): RpcNotification[] {
|
|
23
|
+
const now = Date.now()
|
|
24
|
+
lastDrainAtAny = now
|
|
25
|
+
if (sessionId !== undefined) lastDrainAtBySession.set(sessionId, now)
|
|
26
|
+
const matches = (n: RpcNotification) =>
|
|
27
|
+
sessionId === undefined ||
|
|
28
|
+
n.sessionId === undefined ||
|
|
29
|
+
n.sessionId === sessionId
|
|
30
|
+
if (lastReceivedId > 0) {
|
|
31
|
+
queue = queue.filter((n) => {
|
|
32
|
+
if (n.id > lastReceivedId) return true
|
|
33
|
+
if (sessionId === undefined) return false
|
|
34
|
+
return n.sessionId !== sessionId
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
return queue.filter((n) => n.id > lastReceivedId && matches(n))
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function isTuiConnected(sessionId?: string): boolean {
|
|
41
|
+
const now = Date.now()
|
|
42
|
+
if (sessionId !== undefined) {
|
|
43
|
+
const at = lastDrainAtBySession.get(sessionId) ?? 0
|
|
44
|
+
return at > 0 && now - at < TUI_CONNECTED_WINDOW_MS
|
|
45
|
+
}
|
|
46
|
+
return lastDrainAtAny > 0 && now - lastDrainAtAny < TUI_CONNECTED_WINDOW_MS
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function resetNotificationsForTest(): void {
|
|
50
|
+
queue = []
|
|
51
|
+
nextId = 1
|
|
52
|
+
lastDrainAtAny = 0
|
|
53
|
+
lastDrainAtBySession.clear()
|
|
54
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {
|
|
2
|
+
mkdir,
|
|
3
|
+
readdir,
|
|
4
|
+
readFile,
|
|
5
|
+
rename,
|
|
6
|
+
unlink,
|
|
7
|
+
writeFile,
|
|
8
|
+
} from 'node:fs/promises'
|
|
9
|
+
import { join } from 'node:path'
|
|
10
|
+
|
|
11
|
+
export interface PortFileEntry {
|
|
12
|
+
port: number
|
|
13
|
+
token: string
|
|
14
|
+
pid: number
|
|
15
|
+
startedAt: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function pidAlive(pid: number): boolean {
|
|
19
|
+
try {
|
|
20
|
+
process.kill(pid, 0)
|
|
21
|
+
return true
|
|
22
|
+
} catch (error) {
|
|
23
|
+
return (error as NodeJS.ErrnoException).code === 'EPERM'
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function writePortFile(
|
|
28
|
+
dir: string,
|
|
29
|
+
entry: { port: number; token: string; pid: number },
|
|
30
|
+
): Promise<string> {
|
|
31
|
+
await mkdir(dir, { recursive: true })
|
|
32
|
+
const full: PortFileEntry = { ...entry, startedAt: Date.now() }
|
|
33
|
+
const target = join(dir, `port-${entry.pid}.json`)
|
|
34
|
+
const tmp = `${target}.${process.pid}.tmp`
|
|
35
|
+
await writeFile(tmp, JSON.stringify(full), { encoding: 'utf8', mode: 0o600 })
|
|
36
|
+
await rename(tmp, target)
|
|
37
|
+
return target
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function discoverPortFile(
|
|
41
|
+
dir: string,
|
|
42
|
+
): Promise<PortFileEntry | null> {
|
|
43
|
+
let names: string[]
|
|
44
|
+
try {
|
|
45
|
+
names = await readdir(dir)
|
|
46
|
+
} catch {
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
const live: PortFileEntry[] = []
|
|
50
|
+
for (const name of names) {
|
|
51
|
+
if (!name.startsWith('port-') || !name.endsWith('.json')) continue
|
|
52
|
+
try {
|
|
53
|
+
const parsed = JSON.parse(
|
|
54
|
+
await readFile(join(dir, name), 'utf8'),
|
|
55
|
+
) as PortFileEntry
|
|
56
|
+
if (Number.isFinite(parsed.port)) {
|
|
57
|
+
if (pidAlive(parsed.pid)) live.push(parsed)
|
|
58
|
+
else unlink(join(dir, name)).catch(() => {})
|
|
59
|
+
}
|
|
60
|
+
} catch {}
|
|
61
|
+
}
|
|
62
|
+
if (live.length === 0) return null
|
|
63
|
+
return live.sort((a, b) => b.startedAt - a.startedAt)[0] ?? null
|
|
64
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type CommandModalName =
|
|
2
|
+
| 'claude-cache'
|
|
3
|
+
| 'claude-cachekeep'
|
|
4
|
+
| 'claude-quota'
|
|
5
|
+
| 'claude-dump'
|
|
6
|
+
| 'claude-fast'
|
|
7
|
+
| 'claude-routing'
|
|
8
|
+
| 'claude-killswitch'
|
|
9
|
+
|
|
10
|
+
export interface OpenDialogPayload {
|
|
11
|
+
command: CommandModalName
|
|
12
|
+
text: string
|
|
13
|
+
knobs: Record<string, unknown>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface RpcNotification {
|
|
17
|
+
id: number
|
|
18
|
+
type: 'open-dialog'
|
|
19
|
+
payload: OpenDialogPayload
|
|
20
|
+
sessionId?: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ApplyRequest {
|
|
24
|
+
command: CommandModalName
|
|
25
|
+
arguments: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ApplyResult {
|
|
29
|
+
text: string
|
|
30
|
+
knobs: Record<string, unknown>
|
|
31
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { discoverPortFile } from './port-file'
|
|
2
|
+
import type { ApplyRequest, ApplyResult, RpcNotification } from './protocol'
|
|
3
|
+
|
|
4
|
+
export interface RpcClient {
|
|
5
|
+
pending: (
|
|
6
|
+
lastReceivedId: number,
|
|
7
|
+
sessionId?: string,
|
|
8
|
+
) => Promise<RpcNotification[]>
|
|
9
|
+
apply: (request: ApplyRequest) => Promise<ApplyResult>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function call<T>(
|
|
13
|
+
dir: string,
|
|
14
|
+
method: string,
|
|
15
|
+
params: Record<string, unknown>,
|
|
16
|
+
): Promise<T | null> {
|
|
17
|
+
const entry = await discoverPortFile(dir)
|
|
18
|
+
if (!entry) return null
|
|
19
|
+
const controller = new AbortController()
|
|
20
|
+
const timer = setTimeout(() => controller.abort(), 2_000)
|
|
21
|
+
try {
|
|
22
|
+
const res = await fetch(`http://127.0.0.1:${entry.port}/rpc/${method}`, {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers: {
|
|
25
|
+
'content-type': 'application/json',
|
|
26
|
+
authorization: `Bearer ${entry.token}`,
|
|
27
|
+
},
|
|
28
|
+
body: JSON.stringify(params),
|
|
29
|
+
signal: controller.signal,
|
|
30
|
+
})
|
|
31
|
+
if (!res.ok) return null
|
|
32
|
+
return (await res.json()) as T
|
|
33
|
+
} catch {
|
|
34
|
+
return null
|
|
35
|
+
} finally {
|
|
36
|
+
clearTimeout(timer)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function createRpcClient(dir: string): RpcClient {
|
|
41
|
+
return {
|
|
42
|
+
async pending(lastReceivedId, sessionId) {
|
|
43
|
+
const out = await call<{ messages: RpcNotification[] }>(
|
|
44
|
+
dir,
|
|
45
|
+
'pending-notifications',
|
|
46
|
+
{ lastReceivedId, sessionId },
|
|
47
|
+
)
|
|
48
|
+
return out?.messages ?? []
|
|
49
|
+
},
|
|
50
|
+
async apply(request) {
|
|
51
|
+
const out = await call<ApplyResult>(dir, 'apply', { ...request })
|
|
52
|
+
return out ?? { text: 'apply failed', knobs: {} }
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { homedir, tmpdir } from 'node:os'
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
|
|
5
|
+
const RPC_DIR_ENV = 'OPENCODE_ANTHROPIC_AUTH_RPC_DIR'
|
|
6
|
+
|
|
7
|
+
// Both processes must resolve the SAME dir from the SAME project directory.
|
|
8
|
+
export function getRpcDir(projectDirectory: string): string {
|
|
9
|
+
const override = process.env[RPC_DIR_ENV]?.trim()
|
|
10
|
+
if (override) return override
|
|
11
|
+
const hash = createHash('sha256')
|
|
12
|
+
.update(projectDirectory)
|
|
13
|
+
.digest('hex')
|
|
14
|
+
.slice(0, 16)
|
|
15
|
+
const base = process.env.XDG_STATE_HOME || join(homedir(), '.local', 'state')
|
|
16
|
+
return join(base, 'cortexkit', 'anthropic-auth', 'rpc', hash)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { tmpdir }
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto'
|
|
2
|
+
import { unlink } from 'node:fs/promises'
|
|
3
|
+
import {
|
|
4
|
+
createServer,
|
|
5
|
+
type IncomingMessage,
|
|
6
|
+
type ServerResponse,
|
|
7
|
+
} from 'node:http'
|
|
8
|
+
import { join } from 'node:path'
|
|
9
|
+
import type { drainNotifications } from './notifications'
|
|
10
|
+
import { writePortFile } from './port-file'
|
|
11
|
+
import type { ApplyRequest, ApplyResult } from './protocol'
|
|
12
|
+
|
|
13
|
+
export interface RpcServerHandle {
|
|
14
|
+
port: number
|
|
15
|
+
token: string
|
|
16
|
+
stop: () => Promise<void>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface RpcServerOptions {
|
|
20
|
+
dir: string
|
|
21
|
+
drain: typeof drainNotifications
|
|
22
|
+
apply: (request: ApplyRequest) => Promise<ApplyResult>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function readBody(req: IncomingMessage): Promise<string> {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
const chunks: Buffer[] = []
|
|
28
|
+
let size = 0
|
|
29
|
+
req.on('data', (chunk: Buffer) => {
|
|
30
|
+
size += chunk.length
|
|
31
|
+
if (size > 1_000_000) {
|
|
32
|
+
req.destroy()
|
|
33
|
+
reject(new Error('body too large'))
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
chunks.push(chunk)
|
|
37
|
+
})
|
|
38
|
+
req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
|
|
39
|
+
req.on('error', reject)
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function tokenOk(header: string | undefined, token: string): boolean {
|
|
44
|
+
if (!header?.startsWith('Bearer ')) return false
|
|
45
|
+
const got = Buffer.from(header.slice(7))
|
|
46
|
+
const want = Buffer.from(token)
|
|
47
|
+
return got.length === want.length && timingSafeEqual(got, want)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function startRpcServer(
|
|
51
|
+
options: RpcServerOptions,
|
|
52
|
+
): Promise<RpcServerHandle> {
|
|
53
|
+
const token = randomBytes(32).toString('hex')
|
|
54
|
+
const server = createServer((req, res) => {
|
|
55
|
+
void dispatch(req, res)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
async function dispatch(req: IncomingMessage, res: ServerResponse) {
|
|
59
|
+
const json = (status: number, value: unknown) => {
|
|
60
|
+
res.writeHead(status, { 'content-type': 'application/json' })
|
|
61
|
+
res.end(JSON.stringify(value))
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
const url = req.url ?? ''
|
|
65
|
+
if (req.method === 'GET' && url === '/health')
|
|
66
|
+
return json(200, { ok: true })
|
|
67
|
+
if (req.method !== 'POST' || !url.startsWith('/rpc/'))
|
|
68
|
+
return json(404, { error: 'not found' })
|
|
69
|
+
if (!tokenOk(req.headers.authorization, token))
|
|
70
|
+
return json(401, { error: 'unauthorized' })
|
|
71
|
+
const method = url.slice('/rpc/'.length)
|
|
72
|
+
const params = JSON.parse((await readBody(req)) || '{}') as Record<
|
|
73
|
+
string,
|
|
74
|
+
unknown
|
|
75
|
+
>
|
|
76
|
+
if (method === 'pending-notifications') {
|
|
77
|
+
const messages = options.drain(
|
|
78
|
+
Number(params.lastReceivedId ?? 0),
|
|
79
|
+
typeof params.sessionId === 'string' ? params.sessionId : undefined,
|
|
80
|
+
)
|
|
81
|
+
return json(200, { messages })
|
|
82
|
+
}
|
|
83
|
+
if (method === 'apply') {
|
|
84
|
+
const result = await options.apply(params as unknown as ApplyRequest)
|
|
85
|
+
return json(200, result)
|
|
86
|
+
}
|
|
87
|
+
return json(404, { error: 'unknown method' })
|
|
88
|
+
} catch (error) {
|
|
89
|
+
json(500, {
|
|
90
|
+
error: error instanceof Error ? error.message : String(error),
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const port = await new Promise<number>((resolve, reject) => {
|
|
96
|
+
server.on('error', reject)
|
|
97
|
+
server.listen(0, '127.0.0.1', () => {
|
|
98
|
+
const addr = server.address()
|
|
99
|
+
if (addr && typeof addr === 'object') resolve(addr.port)
|
|
100
|
+
else reject(new Error('no port'))
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
server.unref()
|
|
104
|
+
try {
|
|
105
|
+
await writePortFile(options.dir, { port, token, pid: process.pid })
|
|
106
|
+
} catch (error) {
|
|
107
|
+
await new Promise<void>((resolve) => server.close(() => resolve()))
|
|
108
|
+
throw error
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
port,
|
|
113
|
+
token,
|
|
114
|
+
async stop() {
|
|
115
|
+
await new Promise<void>((resolve) => server.close(() => resolve()))
|
|
116
|
+
await unlink(join(options.dir, `port-${process.pid}.json`)).catch(
|
|
117
|
+
() => {},
|
|
118
|
+
)
|
|
119
|
+
},
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/** @jsxImportSource @opentui/solid */
|
|
2
|
+
import type { TuiPluginApi } from '@opencode-ai/plugin/tui'
|
|
3
|
+
import type { OpenDialogPayload } from '../rpc/protocol.js'
|
|
4
|
+
|
|
5
|
+
type ApplyFn = (
|
|
6
|
+
command: OpenDialogPayload['command'],
|
|
7
|
+
args: string,
|
|
8
|
+
) => Promise<{ text: string }>
|
|
9
|
+
|
|
10
|
+
function showText(api: TuiPluginApi, text: string) {
|
|
11
|
+
api.ui.dialog.setSize('xlarge')
|
|
12
|
+
api.ui.dialog.replace(() => (
|
|
13
|
+
<box flexDirection='column' padding={1} width='100%'>
|
|
14
|
+
<text>{text}</text>
|
|
15
|
+
</box>
|
|
16
|
+
))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function openCommandDialog(
|
|
20
|
+
api: TuiPluginApi,
|
|
21
|
+
payload: OpenDialogPayload,
|
|
22
|
+
apply: ApplyFn,
|
|
23
|
+
) {
|
|
24
|
+
if (payload.command === 'claude-routing') {
|
|
25
|
+
const current = (payload.knobs.mode as string) ?? 'main-first'
|
|
26
|
+
const DialogSelect = api.ui.DialogSelect<string>
|
|
27
|
+
api.ui.dialog.setSize('xlarge')
|
|
28
|
+
api.ui.dialog.replace(() => (
|
|
29
|
+
<DialogSelect
|
|
30
|
+
title='Claude routing'
|
|
31
|
+
current={current}
|
|
32
|
+
options={[
|
|
33
|
+
{
|
|
34
|
+
title: 'Main first',
|
|
35
|
+
value: 'main-first',
|
|
36
|
+
description: 'Use the main account until exhausted',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
title: 'Fallback first',
|
|
40
|
+
value: 'fallback-first',
|
|
41
|
+
description: 'Prefer fallback accounts, preserve main',
|
|
42
|
+
},
|
|
43
|
+
]}
|
|
44
|
+
onSelect={(option) => {
|
|
45
|
+
void apply('claude-routing', String(option.value)).then((r) => {
|
|
46
|
+
api.ui.toast({ message: r.text })
|
|
47
|
+
api.ui.dialog.clear()
|
|
48
|
+
})
|
|
49
|
+
}}
|
|
50
|
+
/>
|
|
51
|
+
))
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (payload.command === 'claude-fast' || payload.command === 'claude-dump') {
|
|
56
|
+
const enabled = payload.knobs.enabled === true
|
|
57
|
+
const label =
|
|
58
|
+
payload.command === 'claude-fast' ? 'fast mode' : 'request dump'
|
|
59
|
+
const DialogConfirm = api.ui.DialogConfirm
|
|
60
|
+
api.ui.dialog.setSize('xlarge')
|
|
61
|
+
api.ui.dialog.replace(() => (
|
|
62
|
+
<DialogConfirm
|
|
63
|
+
title={`Claude ${label}`}
|
|
64
|
+
message={`${payload.text}\n\n${enabled ? 'Disable' : 'Enable'} ${label}?`}
|
|
65
|
+
onConfirm={() => {
|
|
66
|
+
void apply(payload.command, enabled ? 'off' : 'on').then((r) => {
|
|
67
|
+
api.ui.toast({ message: r.text })
|
|
68
|
+
api.ui.dialog.clear()
|
|
69
|
+
})
|
|
70
|
+
}}
|
|
71
|
+
onCancel={() => api.ui.dialog.clear()}
|
|
72
|
+
/>
|
|
73
|
+
))
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (payload.command === 'claude-cache') {
|
|
78
|
+
const enabled = payload.knobs.enabled === true
|
|
79
|
+
const mode = (payload.knobs.mode as string) ?? 'hybrid'
|
|
80
|
+
const currentValue = enabled ? mode : 'off'
|
|
81
|
+
const DialogSelect = api.ui.DialogSelect<string>
|
|
82
|
+
api.ui.dialog.setSize('xlarge')
|
|
83
|
+
api.ui.dialog.replace(() => (
|
|
84
|
+
<DialogSelect
|
|
85
|
+
title='Claude 1h cache'
|
|
86
|
+
current={currentValue}
|
|
87
|
+
options={[
|
|
88
|
+
{ title: 'Off', value: 'off', description: 'Disable 1h cache' },
|
|
89
|
+
{
|
|
90
|
+
title: 'Explicit',
|
|
91
|
+
value: 'explicit',
|
|
92
|
+
description: 'Existing OpenCode breakpoints',
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
title: 'Automatic',
|
|
96
|
+
value: 'automatic',
|
|
97
|
+
description: 'Top-level cache_control only',
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
title: 'Hybrid',
|
|
101
|
+
value: 'hybrid',
|
|
102
|
+
description: 'system + messages[0] + top-level',
|
|
103
|
+
},
|
|
104
|
+
]}
|
|
105
|
+
onSelect={(option) => {
|
|
106
|
+
if (option.value === 'off') {
|
|
107
|
+
void apply('claude-cache', 'off').then((r) => {
|
|
108
|
+
api.ui.toast({ message: r.text })
|
|
109
|
+
api.ui.dialog.clear()
|
|
110
|
+
})
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
void apply('claude-cache', `mode ${option.value}`)
|
|
114
|
+
.then(() => apply('claude-cache', 'on'))
|
|
115
|
+
.then((r) => {
|
|
116
|
+
api.ui.toast({ message: r.text })
|
|
117
|
+
api.ui.dialog.clear()
|
|
118
|
+
})
|
|
119
|
+
}}
|
|
120
|
+
/>
|
|
121
|
+
))
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (payload.command === 'claude-cachekeep') {
|
|
126
|
+
const window = payload.knobs.window as
|
|
127
|
+
| { startHour: number; endHour: number }
|
|
128
|
+
| undefined
|
|
129
|
+
const seed = window
|
|
130
|
+
? `${String(window.startHour).padStart(2, '0')}-${String(window.endHour).padStart(2, '0')}`
|
|
131
|
+
: ''
|
|
132
|
+
const DialogPrompt = api.ui.DialogPrompt
|
|
133
|
+
api.ui.dialog.setSize('xlarge')
|
|
134
|
+
api.ui.dialog.replace(() => (
|
|
135
|
+
<DialogPrompt
|
|
136
|
+
title='Claude cachekeep window'
|
|
137
|
+
description={() => <text>{payload.text}</text>}
|
|
138
|
+
placeholder="HH-HH (e.g. 08-20) or 'off'"
|
|
139
|
+
value={seed}
|
|
140
|
+
onConfirm={(value: string) => {
|
|
141
|
+
void apply('claude-cachekeep', value.trim()).then((r) => {
|
|
142
|
+
api.ui.toast({ message: r.text })
|
|
143
|
+
api.ui.dialog.clear()
|
|
144
|
+
})
|
|
145
|
+
}}
|
|
146
|
+
onCancel={() => api.ui.dialog.clear()}
|
|
147
|
+
/>
|
|
148
|
+
))
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (payload.command === 'claude-killswitch') {
|
|
153
|
+
const config = (payload.knobs.config ?? {}) as {
|
|
154
|
+
enabled?: boolean
|
|
155
|
+
main?: Record<string, number>
|
|
156
|
+
accounts?: Record<string, Record<string, number>>
|
|
157
|
+
}
|
|
158
|
+
const accountIds = (payload.knobs.accountIds as string[]) ?? []
|
|
159
|
+
const enabled = config.enabled === true
|
|
160
|
+
const readT = (t: Record<string, number> | undefined) => {
|
|
161
|
+
const fh = t?.five_hour ?? t?.['5h'] ?? 5
|
|
162
|
+
const sd = t?.seven_day ?? t?.['1w'] ?? 10
|
|
163
|
+
return { fh, sd }
|
|
164
|
+
}
|
|
165
|
+
const mainT = readT(config.main)
|
|
166
|
+
const seedParts = [`main:${mainT.fh},${mainT.sd}`]
|
|
167
|
+
for (const id of accountIds) {
|
|
168
|
+
const t = readT(config.accounts?.[id] ?? config.main)
|
|
169
|
+
seedParts.push(`${id}:${t.fh},${t.sd}`)
|
|
170
|
+
}
|
|
171
|
+
const seed = seedParts.join(' ')
|
|
172
|
+
|
|
173
|
+
const openEdit = () => {
|
|
174
|
+
const DialogPrompt = api.ui.DialogPrompt
|
|
175
|
+
api.ui.dialog.setSize('xlarge')
|
|
176
|
+
api.ui.dialog.replace(() => (
|
|
177
|
+
<DialogPrompt
|
|
178
|
+
title='Killswitch thresholds'
|
|
179
|
+
description={() => <text>{payload.text}</text>}
|
|
180
|
+
placeholder='main:5,10 work-alt:5,10'
|
|
181
|
+
value={seed}
|
|
182
|
+
onConfirm={(value: string) => {
|
|
183
|
+
void apply('claude-killswitch', `set ${value.trim()}`).then((r) => {
|
|
184
|
+
api.ui.toast({ message: r.text })
|
|
185
|
+
api.ui.dialog.clear()
|
|
186
|
+
})
|
|
187
|
+
}}
|
|
188
|
+
onCancel={() => api.ui.dialog.clear()}
|
|
189
|
+
/>
|
|
190
|
+
))
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const DialogSelect = api.ui.DialogSelect<string>
|
|
194
|
+
api.ui.dialog.setSize('xlarge')
|
|
195
|
+
api.ui.dialog.replace(() => (
|
|
196
|
+
<DialogSelect
|
|
197
|
+
title='Claude killswitch'
|
|
198
|
+
options={[
|
|
199
|
+
{
|
|
200
|
+
title: enabled ? 'Disable killswitch' : 'Enable killswitch',
|
|
201
|
+
value: enabled ? 'off' : 'on',
|
|
202
|
+
description: enabled
|
|
203
|
+
? 'Stop hard-blocking on low quota'
|
|
204
|
+
: 'Hard-block requests when quota drops below thresholds',
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
title: 'Edit thresholds…',
|
|
208
|
+
value: 'edit',
|
|
209
|
+
description: 'Set per-account 5h,1w cutoffs',
|
|
210
|
+
},
|
|
211
|
+
]}
|
|
212
|
+
onSelect={(option) => {
|
|
213
|
+
if (option.value === 'edit') {
|
|
214
|
+
openEdit()
|
|
215
|
+
return
|
|
216
|
+
}
|
|
217
|
+
void apply('claude-killswitch', String(option.value)).then((r) => {
|
|
218
|
+
api.ui.toast({ message: r.text })
|
|
219
|
+
api.ui.dialog.clear()
|
|
220
|
+
})
|
|
221
|
+
}}
|
|
222
|
+
/>
|
|
223
|
+
))
|
|
224
|
+
return
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// fallback for quota (display-only)
|
|
228
|
+
showText(api, payload.text)
|
|
229
|
+
}
|
package/src/tui.tsx
CHANGED
|
@@ -16,7 +16,8 @@ import {
|
|
|
16
16
|
onCleanup,
|
|
17
17
|
Show,
|
|
18
18
|
} from 'solid-js'
|
|
19
|
-
|
|
19
|
+
import { createRpcClient } from './rpc/rpc-client.js'
|
|
20
|
+
import { getRpcDir } from './rpc/rpc-dir.js'
|
|
20
21
|
import {
|
|
21
22
|
type AccountQuota,
|
|
22
23
|
computeQuotaPacing,
|
|
@@ -29,6 +30,7 @@ import {
|
|
|
29
30
|
SEVEN_DAY_MS,
|
|
30
31
|
type SidebarState,
|
|
31
32
|
} from './sidebar-state.js'
|
|
33
|
+
import { openCommandDialog } from './tui/command-dialogs.js'
|
|
32
34
|
import {
|
|
33
35
|
type AnthropicAuthTuiPrefs,
|
|
34
36
|
type AppearancePrefs,
|
|
@@ -41,6 +43,9 @@ import {
|
|
|
41
43
|
watchTuiPreferences,
|
|
42
44
|
} from './tui-preferences.js'
|
|
43
45
|
|
|
46
|
+
const RPC_POLL_MS = 500
|
|
47
|
+
let rpcPollStarted = false
|
|
48
|
+
|
|
44
49
|
const ID = 'cortexkit.anthropic-auth'
|
|
45
50
|
|
|
46
51
|
// Plugin version for the header (mirrors the Magic Context / AFT convention).
|
|
@@ -345,6 +350,67 @@ function AccountBlock(props: {
|
|
|
345
350
|
)
|
|
346
351
|
}
|
|
347
352
|
|
|
353
|
+
// --- Quota dialog content ---------------------------------------------------
|
|
354
|
+
|
|
355
|
+
// Renders the same rich visualization as the sidebar (account blocks with
|
|
356
|
+
// quota bars + pacing) so the modal matches the sidebar look.
|
|
357
|
+
function QuotaDialogContent(props: {
|
|
358
|
+
api: TuiPluginApi
|
|
359
|
+
controller: SidebarController
|
|
360
|
+
}) {
|
|
361
|
+
const prefs = props.controller.prefs
|
|
362
|
+
const [state, setState] = createSignal<SidebarState>(DEFAULT_SIDEBAR_STATE)
|
|
363
|
+
let lastUpdated = 0
|
|
364
|
+
async function refresh() {
|
|
365
|
+
const next = await readStateFromFile()
|
|
366
|
+
if (next.lastUpdated !== lastUpdated) {
|
|
367
|
+
lastUpdated = next.lastUpdated
|
|
368
|
+
setState(next)
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
createEffect(() => {
|
|
372
|
+
const timer = setInterval(refresh, prefs().pollMs)
|
|
373
|
+
onCleanup(() => clearInterval(timer))
|
|
374
|
+
})
|
|
375
|
+
setTimeout(refresh, 0)
|
|
376
|
+
const theme = () => props.api.theme.current
|
|
377
|
+
const enabledFallbacks = () => state().fallbacks.filter((f) => f.enabled)
|
|
378
|
+
return (
|
|
379
|
+
<box flexDirection='column' padding={2} width='100%' alignItems='center'>
|
|
380
|
+
<box flexDirection='column' width={58}>
|
|
381
|
+
<box width='100%' justifyContent='center' marginBottom={1}>
|
|
382
|
+
<text fg={theme().text}>
|
|
383
|
+
<b>Quota</b>
|
|
384
|
+
</text>
|
|
385
|
+
</box>
|
|
386
|
+
<AccountBlock
|
|
387
|
+
theme={theme()}
|
|
388
|
+
appearance={prefs().appearance}
|
|
389
|
+
name='main'
|
|
390
|
+
quota={state().main.quota}
|
|
391
|
+
active={state().activeId === 'main'}
|
|
392
|
+
pacingEnabled={prefs().sections.pacing}
|
|
393
|
+
/>
|
|
394
|
+
<Show when={prefs().sections.fallbackAccounts}>
|
|
395
|
+
<For each={enabledFallbacks()}>
|
|
396
|
+
{(fb) => (
|
|
397
|
+
<AccountBlock
|
|
398
|
+
theme={theme()}
|
|
399
|
+
appearance={prefs().appearance}
|
|
400
|
+
name={fb.label ?? fb.id}
|
|
401
|
+
quota={fb.quota}
|
|
402
|
+
active={state().activeId === fb.id}
|
|
403
|
+
pacingEnabled={prefs().sections.pacing}
|
|
404
|
+
marginTop={1}
|
|
405
|
+
/>
|
|
406
|
+
)}
|
|
407
|
+
</For>
|
|
408
|
+
</Show>
|
|
409
|
+
</box>
|
|
410
|
+
</box>
|
|
411
|
+
)
|
|
412
|
+
}
|
|
413
|
+
|
|
348
414
|
// --- State plumbing ---------------------------------------------------------
|
|
349
415
|
|
|
350
416
|
async function readStateFromFile(): Promise<SidebarState> {
|
|
@@ -701,6 +767,44 @@ const tui: TuiPlugin = async (api) => {
|
|
|
701
767
|
},
|
|
702
768
|
},
|
|
703
769
|
})
|
|
770
|
+
|
|
771
|
+
if (!rpcPollStarted) {
|
|
772
|
+
rpcPollStarted = true
|
|
773
|
+
const rpcClient = createRpcClient(getRpcDir(api.state.path.directory ?? ''))
|
|
774
|
+
let lastNotificationId = 0
|
|
775
|
+
let rpcInFlight = false
|
|
776
|
+
setInterval(() => {
|
|
777
|
+
if (rpcInFlight) return
|
|
778
|
+
const current = (api.route as { current?: unknown }).current
|
|
779
|
+
const resolved =
|
|
780
|
+
typeof current === 'function' ? (current as () => unknown)() : current
|
|
781
|
+
const sessionId = (
|
|
782
|
+
resolved as { params?: { sessionID?: string } } | undefined
|
|
783
|
+
)?.params?.sessionID
|
|
784
|
+
rpcInFlight = true
|
|
785
|
+
void rpcClient
|
|
786
|
+
.pending(lastNotificationId, sessionId)
|
|
787
|
+
.then((messages) => {
|
|
788
|
+
for (const message of [...messages].sort((a, b) => a.id - b.id)) {
|
|
789
|
+
lastNotificationId = Math.max(lastNotificationId, message.id)
|
|
790
|
+
if (message.payload.command === 'claude-quota') {
|
|
791
|
+
api.ui.dialog.setSize('xlarge')
|
|
792
|
+
api.ui.dialog.replace(() => (
|
|
793
|
+
<QuotaDialogContent api={api} controller={controller} />
|
|
794
|
+
))
|
|
795
|
+
continue
|
|
796
|
+
}
|
|
797
|
+
openCommandDialog(api, message.payload, (command, args) =>
|
|
798
|
+
rpcClient.apply({ command, arguments: args }),
|
|
799
|
+
)
|
|
800
|
+
}
|
|
801
|
+
})
|
|
802
|
+
.catch(() => {})
|
|
803
|
+
.finally(() => {
|
|
804
|
+
rpcInFlight = false
|
|
805
|
+
})
|
|
806
|
+
}, RPC_POLL_MS)
|
|
807
|
+
}
|
|
704
808
|
}
|
|
705
809
|
|
|
706
810
|
const plugin: TuiPluginModule & { id: string } = {
|