Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@cortexkit/opencode-anthropic-auth

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cortexkit/opencode-anthropic-auth - npm Package Compare versions

Comparing version
1.2.3
to
1.2.4
dist/index-v5pddj3r.js

Sorry, the diff of this file is too big to display

+1
-1
#!/usr/bin/env node
import{l as y,m as w,o as l,p as u,q as m,ra as b,ta as k}from"./index-gjnk6q4x.js";import{stdin as A,stdout as $}from"node:process";import{createInterface as R}from"node:readline/promises";function v(){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 h(){console.log(`Usage:
import{l as y,m as w,o as l,p as u,q as m,ra as b,ta as k}from"./index-v5pddj3r.js";import{stdin as A,stdout as $}from"node:process";import{createInterface as R}from"node:readline/promises";function v(){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 h(){console.log(`Usage:
opencode-anthropic-auth login [label]

@@ -4,0 +4,0 @@ opencode-anthropic-auth list

@@ -1,2 +0,2 @@

import{$ as S1,A as _1,B as F1,C as H0,D as Z0,E as B1,F as p,G as q0,H as M1,I as S0,J as v1,K as A0,L as U1,M as $0,N as G0,O as D0,P as c,Q as x1,R as V0,S as e0,T as j1,U as E0,V as J1,W as K1,X as Z1,Y as P0,Z as H1,_ as q1,a as c0,aa as A1,b as o0,ba as E1,c as e,ca as C1,d as j0,da as R1,e as t0,ea as X0,f as d,fa as W0,g as a0,ga as T1,h as r0,ha as C0,i as s0,ia as Y0,j as F0,ja as f1,k as z1,ka as w0,l as B0,la as y1,m as M0,ma as R0,n as U,na as z0,oa as k1,p as A,pa as g1,q as v0,qa as b1,r as I1,s as J0,sa as m1,t as K0,u as O1,v as Q1,w as U0,x as L1,y as x0,z as N1}from"./index-gjnk6q4x.js";import{randomUUID as Wj}from"node:crypto";function m(j){return typeof j==="object"&&j!==null}function s1(j){if(Array.isArray(j))return j;if(m(j)&&Array.isArray(j.data))return j.data;return[]}function e1(j){if(!m(j)||!m(j.info))return;return typeof j.info.role==="string"?j.info.role:void 0}function m0(j){if(!m(j)||!m(j.info))return null;let J=j.info,K=m(J.model)?J.model:void 0,$=typeof J.agent==="string"?J.agent:void 0,D=typeof K?.providerID==="string"?K.providerID:typeof J.providerID==="string"?J.providerID:void 0,V=typeof K?.modelID==="string"?K.modelID:typeof J.modelID==="string"?J.modelID:void 0,B=typeof K?.variant==="string"?K.variant:typeof J.variant==="string"?J.variant:void 0;if(!$&&(!D||!V)&&!B)return null;let E={};if($)E.agent=$;if(D&&V)E.model={providerID:D,modelID:V};if(B)E.variant=B;return E}function h0(j,J){return{agent:j.agent??J.agent,model:j.model??J.model,variant:j.variant??J.variant}}function u0(j){return Boolean(j.agent&&j.model&&j.variant)}async function d0(j,J){if(!j||!J)return null;let K=j;if(typeof K.session?.messages!=="function")return null;let $=[];try{$=s1(await Promise.resolve(K.session.messages({path:{id:J},query:{limit:100}})))}catch{return null}if($.length===0)return null;let D={};for(let V=$.length-1;V>=0;V--){if(e1($[V])!=="assistant")continue;let B=m0($[V]);if(!B)continue;if(D=h0(D,B),u0(D))return D}for(let V=$.length-1;V>=0;V--){let B=m0($[V]);if(!B)continue;if(D=h0(D,B),u0(D))return D}if(!D.agent&&!D.model&&!D.variant)return null;return D}function l0(j){return`${c0}${j.charAt(0).toUpperCase()}${j.slice(1)}`}function jj(j){if(j==="StructuredOutput")return j;return`${j.charAt(0).toLowerCase()}${j.slice(1)}`}function $1(j,J){let K=new Headers;if(j instanceof Request)j.headers.forEach((D,V)=>{K.set(V,D)});let $=J?.headers;if($){if($ instanceof Headers)$.forEach((D,V)=>{K.set(V,D)});else if(Array.isArray($))for(let D of $){let[V,B]=D;if(typeof B<"u")K.set(V,String(B))}else for(let[D,V]of Object.entries($))if(typeof V<"u")K.set(D,String(V))}return K}function N0(j){return j.set("anthropic-beta",e(j.get("anthropic-beta"),[o0])),j}function l(j,J,K={}){return K1(j,J,K)}function Jj(j){if(j.tools&&Array.isArray(j.tools))j.tools=j.tools.map((J)=>({...J,name:J.name?l0(J.name):J.name}));if(j.messages&&Array.isArray(j.messages))j.messages=j.messages.map((J)=>{if(J.content&&Array.isArray(J.content))J.content=J.content.map((K)=>{if(K.type==="tool_use"&&K.name)return{...K,name:l0(K.name)};return K});return J});return JSON.stringify(Z1(j))}function Q0(j){return j.replace(/"name"\s*:\s*"mcp_([^"]+)"/g,(J,K)=>`"name": "${jj(K)}"`)}function i0(j,J=!1){if(J)return{ready:Q0(j),pending:""};let K=j.length,$='"name"',D=Math.max(0,j.length-$.length+1);for(let B=D;B<j.length;B++)if($.startsWith(j.slice(B))){K=Math.min(K,B);break}let V=j.lastIndexOf($);if(V!==-1){let B=j.slice(V);if(/^"name"\s*(?::\s*(?:"[^"]*)?)?$/.test(B))K=Math.min(K,V)}if(K<j.length)return{ready:Q0(j.slice(0,K)),pending:j.slice(K)};return{ready:Q0(j),pending:""}}function G1(){if(!process.env.ANTHROPIC_BASE_URL?.trim())return!1;let j=process.env.ANTHROPIC_INSECURE?.trim();return j==="1"||j==="true"}function Kj(){let j=process.env.ANTHROPIC_BASE_URL?.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 D1(j){let J=null;try{if(typeof j==="string"||j instanceof URL)J=new URL(j.toString());else if(j instanceof Request)J=new URL(j.url)}catch{J=null}if(!J)return{input:j,url:null};let K=J.href,$=Kj();if($)J.protocol=$.protocol,J.host=$.host;if(J.pathname==="/v1/messages"&&!J.searchParams.has("beta"))J.searchParams.set("beta","true");if(J.href===K)return{input:j,url:J};return{input:j instanceof Request?new Request(J.toString(),j):J,url:J}}function a(j){let $=j.split(/\n\n+/).filter((D)=>{if(D.includes(t0))return!1;for(let V of r0)if(D.includes(V))return!1;return!0}).join(`
import{$ as S1,A as _1,B as F1,C as H0,D as Z0,E as B1,F as p,G as q0,H as M1,I as S0,J as v1,K as A0,L as U1,M as $0,N as G0,O as D0,P as c,Q as x1,R as V0,S as e0,T as j1,U as E0,V as J1,W as K1,X as Z1,Y as P0,Z as H1,_ as q1,a as c0,aa as A1,b as o0,ba as E1,c as e,ca as C1,d as j0,da as R1,e as t0,ea as X0,f as d,fa as W0,g as a0,ga as T1,h as r0,ha as C0,i as s0,ia as Y0,j as F0,ja as f1,k as z1,ka as w0,l as B0,la as y1,m as M0,ma as R0,n as U,na as z0,oa as k1,p as A,pa as g1,q as v0,qa as b1,r as I1,s as J0,sa as m1,t as K0,u as O1,v as Q1,w as U0,x as L1,y as x0,z as N1}from"./index-v5pddj3r.js";import{randomUUID as Wj}from"node:crypto";function m(j){return typeof j==="object"&&j!==null}function s1(j){if(Array.isArray(j))return j;if(m(j)&&Array.isArray(j.data))return j.data;return[]}function e1(j){if(!m(j)||!m(j.info))return;return typeof j.info.role==="string"?j.info.role:void 0}function m0(j){if(!m(j)||!m(j.info))return null;let J=j.info,K=m(J.model)?J.model:void 0,$=typeof J.agent==="string"?J.agent:void 0,D=typeof K?.providerID==="string"?K.providerID:typeof J.providerID==="string"?J.providerID:void 0,V=typeof K?.modelID==="string"?K.modelID:typeof J.modelID==="string"?J.modelID:void 0,B=typeof K?.variant==="string"?K.variant:typeof J.variant==="string"?J.variant:void 0;if(!$&&(!D||!V)&&!B)return null;let E={};if($)E.agent=$;if(D&&V)E.model={providerID:D,modelID:V};if(B)E.variant=B;return E}function h0(j,J){return{agent:j.agent??J.agent,model:j.model??J.model,variant:j.variant??J.variant}}function u0(j){return Boolean(j.agent&&j.model&&j.variant)}async function d0(j,J){if(!j||!J)return null;let K=j;if(typeof K.session?.messages!=="function")return null;let $=[];try{$=s1(await Promise.resolve(K.session.messages({path:{id:J},query:{limit:100}})))}catch{return null}if($.length===0)return null;let D={};for(let V=$.length-1;V>=0;V--){if(e1($[V])!=="assistant")continue;let B=m0($[V]);if(!B)continue;if(D=h0(D,B),u0(D))return D}for(let V=$.length-1;V>=0;V--){let B=m0($[V]);if(!B)continue;if(D=h0(D,B),u0(D))return D}if(!D.agent&&!D.model&&!D.variant)return null;return D}function l0(j){return`${c0}${j.charAt(0).toUpperCase()}${j.slice(1)}`}function jj(j){if(j==="StructuredOutput")return j;return`${j.charAt(0).toLowerCase()}${j.slice(1)}`}function $1(j,J){let K=new Headers;if(j instanceof Request)j.headers.forEach((D,V)=>{K.set(V,D)});let $=J?.headers;if($){if($ instanceof Headers)$.forEach((D,V)=>{K.set(V,D)});else if(Array.isArray($))for(let D of $){let[V,B]=D;if(typeof B<"u")K.set(V,String(B))}else for(let[D,V]of Object.entries($))if(typeof V<"u")K.set(D,String(V))}return K}function N0(j){return j.set("anthropic-beta",e(j.get("anthropic-beta"),[o0])),j}function l(j,J,K={}){return K1(j,J,K)}function Jj(j){if(j.tools&&Array.isArray(j.tools))j.tools=j.tools.map((J)=>({...J,name:J.name?l0(J.name):J.name}));if(j.messages&&Array.isArray(j.messages))j.messages=j.messages.map((J)=>{if(J.content&&Array.isArray(J.content))J.content=J.content.map((K)=>{if(K.type==="tool_use"&&K.name)return{...K,name:l0(K.name)};return K});return J});return JSON.stringify(Z1(j))}function Q0(j){return j.replace(/"name"\s*:\s*"mcp_([^"]+)"/g,(J,K)=>`"name": "${jj(K)}"`)}function i0(j,J=!1){if(J)return{ready:Q0(j),pending:""};let K=j.length,$='"name"',D=Math.max(0,j.length-$.length+1);for(let B=D;B<j.length;B++)if($.startsWith(j.slice(B))){K=Math.min(K,B);break}let V=j.lastIndexOf($);if(V!==-1){let B=j.slice(V);if(/^"name"\s*(?::\s*(?:"[^"]*)?)?$/.test(B))K=Math.min(K,V)}if(K<j.length)return{ready:Q0(j.slice(0,K)),pending:j.slice(K)};return{ready:Q0(j),pending:""}}function G1(){if(!process.env.ANTHROPIC_BASE_URL?.trim())return!1;let j=process.env.ANTHROPIC_INSECURE?.trim();return j==="1"||j==="true"}function Kj(){let j=process.env.ANTHROPIC_BASE_URL?.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 D1(j){let J=null;try{if(typeof j==="string"||j instanceof URL)J=new URL(j.toString());else if(j instanceof Request)J=new URL(j.url)}catch{J=null}if(!J)return{input:j,url:null};let K=J.href,$=Kj();if($)J.protocol=$.protocol,J.host=$.host;if(J.pathname==="/v1/messages"&&!J.searchParams.has("beta"))J.searchParams.set("beta","true");if(J.href===K)return{input:j,url:J};return{input:j instanceof Request?new Request(J.toString(),j):J,url:J}}function a(j){let $=j.split(/\n\n+/).filter((D)=>{if(D.includes(t0))return!1;for(let V of r0)if(D.includes(V))return!1;return!0}).join(`

@@ -3,0 +3,0 @@ `);for(let D of s0)$=$.replace(D.match,D.replacement);return $.trim()}function C(j){return j!=null&&typeof j==="object"&&!Array.isArray(j)}function Zj(j){let J={type:"text",text:d};if(j==null)return[J];if(typeof j==="string"){let $=a(j);if($===d)return[J];return[J,{type:"text",text:$}]}if(C(j)){let $=typeof j.type==="string"?j.type:"text",D=typeof j.text==="string"?j.text:"";return[J,{...j,type:$,text:a(D)}]}if(!Array.isArray(j))return[J];let K=j.map(($)=>{if(typeof $==="string")return{type:"text",text:a($)};if(C($)&&$.type==="text"&&typeof $.text==="string")return{...$,type:"text",text:a($.text)};return{type:"text",text:String($)}});if(K[0]?.text===d)return K;return[J,...K]}var V1={type:"ephemeral",ttl:"1h"};function $j(j){if(C(j.cache_control))return j.cache_control;if(C(j.cacheControl))return j.cacheControl;return null}function s(j,J){if(!C(j))return!1;return delete j.cacheControl,j.cache_control=J?{...V1}:{type:"ephemeral"},!0}function n0(j){if(!C(j))return;delete j.cache_control,delete j.cacheControl}function Gj(j){if(Array.isArray(j))return j;if(typeof j==="string")return[{type:"text",text:j}];return null}function r(j,J){if(!C(j))return;let K=$j(j);if(!K||K.type!=="ephemeral")return;if(J)K.ttl="1h";else delete K.ttl}function p0(j,J){if(Array.isArray(j.system))for(let K of j.system)r(K,J);else r(j.system,J);if(!Array.isArray(j.messages))return;for(let K of j.messages)if(r(K,J),C(K)&&Array.isArray(K.content))for(let $ of K.content)r($,J)}function Dj(j,J){if(Array.isArray(j.system))for(let K of j.system)J(K);else J(j.system);if(!Array.isArray(j.messages))return;for(let K of j.messages)if(J(K),C(K)&&Array.isArray(K.content))for(let $ of K.content)J($)}function P1(j){n0(j),Dj(j,n0)}function Vj(j){P1(j),j.cache_control={...V1}}function L0(j){if(!C(j))return!1;let J=Gj(j.content);if(!J?.length)return s(j,!0);j.content=J;let K=[...J].reverse().find(($)=>C($)&&$.type!=="thinking");return s(K??j,!0)}function Pj(j){if(P1(j),Array.isArray(j.system)){let K=j.system.findIndex((V)=>C(V)&&V.text===d),$=j.system.slice(K>=0?K+1:0).filter(C),D=$[$.length-1];s(D,!0)}else s(j.system,!0);if(!Array.isArray(j.messages))return;L0(j.messages[0]),L0(j.messages[1]);let J=j.messages.length-2;if(J>1)L0(j.messages[J])}function Xj(j,J){if(!J.enabled){p0(j,!1),delete j.cache_control,delete j.cacheControl;return}if(J.mode==="automatic"){Vj(j);return}if(J.mode==="hybrid"){Pj(j);return}p0(j,!0),delete j.cacheControl}async function X1(j,J={}){try{let K=JSON.parse(j),$=Array.isArray(K.messages)&&K.messages.some((D)=>D.role==="user")?j1(K.messages,void 0,a0):null;if(K.system=Zj(K.system),$&&Array.isArray(K.system))K.system.unshift({type:"text",text:$});if(Xj(K,{enabled:J.cache1hEnabled??!1,mode:J.cache1hMode??"explicit"}),J.fastModeEnabled&&j0(K.model))K.speed="fast";else if(K.speed==="fast")delete K.speed;if(J.identity)J1(K,J.identity);return await e0(Jj(K))}catch{return j}}function _0(j){if(!j.body)return j;let J=j.body.getReader(),K=new TextDecoder,$=new TextEncoder,D="",V=new ReadableStream({async pull(B){let{done:E,value:y}=await J.read();if(E){let u=i0(`${D}${K.decode()}`,!0);if(u.ready)B.enqueue($.encode(u.ready));B.close();return}let O0=D+K.decode(y,{stream:!0}),h=i0(O0);if(D=h.pending,h.ready)B.enqueue($.encode(h.ready))}});return new Response(V,{status:j.status,statusText:j.statusText,headers:j.headers})}var Yj="__OPENCODE_ANTHROPIC_AUTH_COMMAND_HANDLED__",wj=60000,zj=60000,Ij=5000,W1=200,h1=240,Oj=h1,Qj=1,Y1=!1;function I0(){return process.env.OPENCODE_ANTHROPIC_AUTH_PERF==="1"}function M(){return performance.now()}function S(j){return Math.round(j*10)/10}function w1(j){return Math.floor(Math.random()*Math.max(0,j))}function Lj(){if(Y1||!1||!I0())return;Y1=!0;let j=100,J=250,K=M()+j;setInterval(()=>{let $=M(),D=$-K;if(K=$+j,D<J)return;U("[perf] opencode event_loop_lag",{lagMs:S(D),thresholdMs:J})},j).unref?.()}function Nj(j){let J=M(),K={requestId:String(Qj++),start:J,last:J,mark($,D){let V=M();if(I0())U("[perf] opencode request stage",{requestId:K.requestId,stage:$,deltaMs:S(V-K.last),totalMs:S(V-K.start),...D});K.last=V},done($,D){let V=M();if(I0())U("[perf] opencode request done",{requestId:K.requestId,stage:$,deltaMs:S(V-K.last),totalMs:S(V-K.start),...D});K.last=V}};if(I0())U("[perf] opencode request start",{requestId:K.requestId,...j});return K}async function i(j,J,K){let $=j.client.session,D=await d0(j.client,J),V={path:{id:J},body:{noReply:!0,parts:[{type:"text",text:K,ignored:!0}]}};if(D?.agent)V.body.agent=D.agent;if(D?.model)V.body.model=D.model;if(D?.variant)V.body.variant=D.variant;if(typeof $?.promptAsync==="function"){await $.promptAsync(V);return}if(typeof $?.prompt==="function"){await Promise.resolve($.prompt(V));return}throw Error("OpenCode session prompt API is unavailable for ignored replies.")}function n(){throw Error(Yj)}var qj=async(j)=>{Lj();let{client:J}=j,K=new U1;K.startBackgroundRefresh();let $=null,D=new R1({loadStorage:()=>A(),prepareHeaders:async(Z,Q)=>{if(!E)return Z;let W=await E();if(W.type!=="oauth")return Z;if(!W.access||W.expires&&W.expires<Date.now()){if(!$)return Z;W.access=await $()}if(!W.access)return Z;try{let N=JSON.parse(Q.bodyText),x=await E0(W.access,typeof N.model==="string"?N.model:void 0);if(Z.delete("anthropic-beta"),l(Z,W.access,{body:N,identity:x}),Z.set("anthropic-beta",e(Z.get("anthropic-beta"),[H1])),N.speed==="fast")N0(Z)}catch{l(Z,W.access)}return Z},log:U}),V=await A(),B=b1(V);c({enabled:J0(V),mode:K0(V)}),W0(U0(V)),w0(x0(V));let E=null,y=null;function O0(Z){let Q=Z?.refresh?.refreshBeforeExpiryMinutes??Oj;return Math.max(h1,Q)*60000}function h(Z){return Z?.refresh?.enabled!==!1}async function u(Z){if(!Z)return;let Q=await A(),W=Q?.refresh?.mainLastRefreshError;if(!Q?.refresh||!W?.tokenHash)return;let N=Z0(Z);if(W.tokenHash===N)return;if(W.nextRetryAt&&W.nextRetryAt>Date.now()){U("[refresh] opencode main oauth keeping backoff despite token rotation",{nextRetryAt:W.nextRetryAt,retryCount:W.retryCount,remainingMs:W.nextRetryAt-Date.now()});return}Q.refresh.mainLastRefreshError=void 0,await v0(Q),U("[refresh] opencode main oauth cleared stale backoff after token rotation",{previousCheckedAt:W.checkedAt,previousNextRetryAt:W.nextRetryAt,previousRetryCount:W.retryCount})}async function u1(){let Z=[];if(E)try{let N=await E();if(N.type==="oauth"&&N.access)Z.push({name:"OpenCode anthropic",role:"main",quota:await A0({accessToken:N.access})});else if(N.type==="oauth")Z.push({name:"OpenCode anthropic",role:"main",error:"missing access token; send a request first or reconnect auth"})}catch(N){Z.push({name:"OpenCode anthropic",role:"main",error:N instanceof Error?N.message:String(N)})}let{storage:Q,errors:W}=await K.refreshQuotaForAllAccounts();if(Z.push(...k1(Q,new Map(W.map((N)=>[N.accountId,N.message])))),!E)Z.unshift({name:"OpenCode anthropic",role:"main",error:"auth loader has not run yet; send a request first"});return g1({accounts:Z,refreshedAt:Date.now()})}async function d1(Z){let Q=x1(Z);if(Q.type==="enable"||Q.type==="disable"){let f=Q.type==="enable",k=await O1(f),g=K0(k);return c({enabled:f,mode:g}),V0({argumentsText:Z,enabled:f,mode:g})}if(Q.type==="mode"){let f=await Q1(Q.mode),k=J0(f);return c({enabled:k,mode:Q.mode}),V0({argumentsText:Z,enabled:k,mode:Q.mode})}let W=await A(),N=J0(W),x=K0(W);return c({enabled:N,mode:x}),V0({argumentsText:Z,enabled:N,mode:x})}async function l1(Z){let Q=q1(Z),W=await A();if(Q.type==="window")W=await _1(Q.startHour,Q.endHour);else if(Q.type==="disable")W=await F1(!1);let N=S1(W),x=D.stats(N);return C1({argumentsText:Z,enabled:A1(W),window:N,hybridActive:E1(W),trackedSessions:x.trackedSessions,nextPrewarmAt:x.nextPrewarmAt})}async function i1(Z){let Q=T1(Z);if(Q.type==="enable"||Q.type==="disable"){let x=Q.type==="enable";return await L1(x),W0(x),C0({argumentsText:Z,enabled:x})}let W=await A(),N=U0(W);return W0(N),C0({argumentsText:Z,enabled:N})}async function n1(Z){let Q=y1(Z);if(Q.type==="enable"||Q.type==="disable"){let x=Q.type==="enable";return await N1(x),w0(x),R0({argumentsText:Z,enabled:x})}let W=await A(),N=x0(W);return w0(N),R0({argumentsText:Z,enabled:N})}return{config:async(Z)=>{Z.command={...Z.command??{},[$0]:{template:$0,description:"Show or toggle 1-hour Anthropic ephemeral prompt cache TTL."},[P0]:{template:P0,description:"Keep hybrid Claude cache warm for recently used sessions during a local time window."},[z0]:{template:z0,description:"Show current Claude OAuth quota usage for all accounts."},[X0]:{template:X0,description:"Show or toggle Anthropic request dump capture for debugging."},[Y0]:{template:Y0,description:"Show or toggle Anthropic fast mode for supported Opus models."}}},"command.execute.before":async(Z)=>{if(Z.command===$0)await i(j,Z.sessionID,await d1(Z.arguments)),n();if(Z.command===P0)await i(j,Z.sessionID,await l1(Z.arguments)),n();if(Z.command===z0)await i(j,Z.sessionID,await u1()),n();if(Z.command===X0)await i(j,Z.sessionID,await i1(Z.arguments)),n();if(Z.command===Y0)await i(j,Z.sessionID,await n1(Z.arguments)),n()},auth:{provider:"anthropic",async loader(Z,Q){if(E=Z,(await Z()).type==="oauth"){let p1=function(){if(y)clearInterval(y),y=null;let P=async()=>{try{let X=await A();if(!h(X))return;let z=await Z();if(z.type!=="oauth")return;if(await u(z.refresh),!z.expires)return;let I=z.expires-Date.now(),v=O0(X);if(I>v)return;if(U("[refresh] opencode main oauth background due",{expiresInMs:I,refreshBeforeMs:v}),z.refresh&&p(X?.refresh?.mainLastRefreshError,z.refresh,Date.now())){U("[refresh] opencode main oauth background skipped backoff",{nextRetryAt:X?.refresh?.mainLastRefreshError?.nextRetryAt,retryCount:X?.refresh?.mainLastRefreshError?.retryCount,expiresInMs:I});return}if(z.refresh&&X?.refresh?.mainRefreshLeaseUntil&&X.refresh.mainRefreshLeaseUntil>Date.now()&&X.refresh.mainRefreshLeaseTokenHash===Z0(z.refresh))return;await g();let O=await Z();U("[refresh] opencode main oauth refreshed in background",{newExpiresInMs:O.expires?O.expires-Date.now():void 0})}catch(X){U("[refresh] opencode main oauth refresh failed",{message:X instanceof Error?X.message:String(X)})}};if(y=setInterval(()=>{P()},wj+w1(zj)),"unref"in y)y.unref()},T0=function(P,X){if(P instanceof Request&&P.body)return!1;return X==null||typeof X==="string"},c1=function(P){return P.has("x-parent-session-id")},f0=function(P){return P.includes("rate_limit_error")||/exceed your account'?s rate limit/i.test(P)},o1=function(P){return P?.quota?.enabled===!0},t1=function(P,X){let z=Date.now();if(f||z<k)return;f=g0(P,X).catch((I)=>{throw k=z+M1(X),I}).finally(()=>{f=null}),f.catch(()=>{})};for(let P of Object.values(Q.models))P.cost={input:0,output:0,cache:{read:0,write:0}};let N=null,x=null,f=null,k=0;async function g(){if(!N)N=(async()=>{let z=null,I=null,v=null;async function O(L){let Y=await A()??{version:1,main:{type:"opencode",provider:"anthropic"},accounts:[]};Y.refresh=Y.refresh??{},L(Y),await v0(Y)}async function F(L){let Y=Date.now()+Ij;while(Date.now()<Y){await new Promise((w)=>setTimeout(w,W1+w1(W1)));let G=await Z();if(G.type!=="oauth"||!G.access)continue;if((G.access!==L.access||G.refresh!==L.refresh||(G.expires??0)>(L.expires??0)+60000)&&(!G.expires||G.expires>Date.now()))return U("[refresh] opencode main oauth joined concurrent refresh",{expiresInMs:G.expires?G.expires-Date.now():void 0}),G.access}return null}for(let L=0;L<=2;L++){let Y=null;try{if(L>0){let H=500*2**(L-1);await new Promise((o)=>setTimeout(o,H))}if(Y=await Z(),!Y.refresh)throw Error("Token refresh failed: missing refresh token");let G=await A(),_=Z0(Y.refresh),w=G?.refresh?.mainLastRefreshError;if(U("[refresh] opencode main oauth refresh check",{attempt:L,expiresInMs:Y.expires?Y.expires-Date.now():void 0,hasBackoff:Boolean(w),backoffActive:w?p(w,Y.refresh,Date.now()):!1,retryCount:w?.retryCount,nextRetryAt:w?.nextRetryAt}),w&&p(w,Y.refresh,Date.now()))throw U("[refresh] opencode main oauth refresh skipped backoff",{nextRetryAt:w.nextRetryAt,retryCount:w.retryCount}),Error(q0(w,Date.now()));if(G?.refresh?.mainRefreshLeaseUntil&&G.refresh.mainRefreshLeaseUntil>Date.now()&&G.refresh.mainRefreshLeaseTokenHash===_){U("[refresh] opencode main oauth refresh skipped lease",{leaseUntil:G.refresh.mainRefreshLeaseUntil});let H=await F(Y);if(H)return H;throw Error("Claude OAuth refresh is already in progress")}let q=await I1({name:"opencode-main-oauth-refresh",ttlMs:120000});if(!q){U("[refresh] opencode main oauth refresh skipped file lock");let H=await F(Y);if(H)return H;throw Error("Claude OAuth refresh is already in progress")}v=q.release,z=Wj(),I=_,await O((H)=>{H.refresh=H.refresh??{},H.refresh.mainRefreshLeaseId=z??void 0,H.refresh.mainRefreshLeaseUntil=Date.now()+120000,H.refresh.mainRefreshLeaseTokenHash=_});let R=await A();if(U("[refresh] opencode main oauth refresh lease acquired",{attempt:L,leaseUntil:Date.now()+120000}),R?.refresh?.mainRefreshLeaseId!==z||R.refresh.mainRefreshLeaseTokenHash!==_)throw Error("Claude OAuth refresh is already in progress");U("[refresh] opencode main oauth refresh request start",{attempt:L});let T=await z1({refreshToken:Y.refresh,maxRetries:0});return await J.auth.set({path:{id:"anthropic"},body:{type:"oauth",refresh:T.refresh,access:T.access,expires:T.expires}}),await O((H)=>{if(!H?.refresh)return;if(H.refresh.mainLastRefreshError=void 0,H.refresh.mainRefreshLeaseId===z)H.refresh.mainRefreshLeaseId=void 0,H.refresh.mainRefreshLeaseUntil=void 0,H.refresh.mainRefreshLeaseTokenHash=void 0}),U("[refresh] opencode main oauth refresh succeeded",{attempt:L,expiresInMs:T.expires-Date.now()}),T.access}catch(G){let _=G instanceof Error&&(G.message.includes("fetch failed")||("code"in G)&&(G.code==="ECONNRESET"||G.code==="ECONNREFUSED"||G.code==="ETIMEDOUT"||G.code==="UND_ERR_CONNECT_TIMEOUT"));if(L<2&&(_||G instanceof F0&&G.status>=500))continue;U("[refresh] opencode main oauth refresh attempt failed",{attempt:L,error:G instanceof Error?G.message:String(G),transient:_});let w=Y?.refresh;if(w&&G instanceof F0)await O((q)=>{q.refresh=q.refresh??{},q.refresh.mainLastRefreshError=B1({error:G,now:Date.now(),refreshToken:w,previous:q.refresh.mainLastRefreshError})});throw G}finally{if(z)await O((G)=>{if(!G?.refresh)return;if(G.refresh.mainRefreshLeaseId===z&&G.refresh.mainRefreshLeaseTokenHash===I)G.refresh.mainRefreshLeaseId=void 0,G.refresh.mainRefreshLeaseUntil=void 0,G.refresh.mainRefreshLeaseTokenHash=void 0}).catch(()=>{});await v?.().catch(()=>{})}}throw Error("Token refresh exhausted all retries")})().finally(()=>{N=null});return N}$=g,p1();async function y0(P,X){if(!P.body||P.status!==200)return X?.mark("inspect_stream_skip",{status:P.status}),{response:P,rateLimited:!1};if(P.headers.get("x-cortexkit-relay-optimistic")==="true")return X?.mark("inspect_stream_skip",{status:P.status,reason:"optimistic_relay"}),{response:P,rateLimited:!1};let z=M(),I=P.body.getReader(),v=[],O=new TextDecoder,F="",L=0;while(!F.includes(`

{
"name": "@cortexkit/opencode-anthropic-auth",
"version": "1.2.3",
"version": "1.2.4",
"type": "module",

@@ -44,4 +44,4 @@ "repository": {

"dependencies": {
"@cortexkit/anthropic-auth-core": "1.2.3"
"@cortexkit/anthropic-auth-core": "1.2.4"
}
}

Sorry, the diff of this file is too big to display