copilot-api-node20 0.21.1 → 0.21.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/main.mjs +1 -1
  2. package/package.json +2 -2
package/dist/main.mjs CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import{A as e,B as t,C as n,D as o,E as r,F as a,G as i,H as s,I as l,J as u,K as c,L as p,M as d,N as m,O as f,P as _,R as g,S as h,T as y,U as k,V as b,W as w,X as x,Y as v,Z as T,_ as A,a as $,b as S,c as O,d as I,f as q,g as N,h as j,i as E,j as P,k as C,l as R,m as B,n as D,o as M,p as H,q as F,r as J,s as U,t as L,u as W,v as K,w as G,x as z,y as Y,z as V}from"./get-user-CEqqH34r.mjs";import{defineCommand as Q,runMain as X}from"citty";import Z from"consola";import ee from"node:fs/promises";import te from"node:os";import{SpanKind as ne,SpanStatusCode as oe,context as re,trace as ae}from"@opentelemetry/api";import{Client as ie}from"undici";import se from"clipboardy";import le from"node:process";import{serve as ue}from"srvx";import ce from"tiny-invariant";import{execSync as pe}from"node:child_process";import{Hono as de}from"hono";import{cors as me}from"hono/cors";import{streamSSE as fe}from"hono/streaming";import{events as _e}from"fetch-event-stream";import{Tokenizer as ge,models as he}from"ai-tokenizer";import*as ye from"ai-tokenizer/encoding/claude";const ke=async()=>{let e=`${E}/copilot_internal/v2/token`,t=v.timeoutMs,n=new AbortController,o=setTimeout(()=>n.abort(),t);try{let{response:t,span:o}=await k({spanName:"github GET /copilot_internal/v2/token",url:e,target:"copilot_token",headers:W(v),signal:n.signal});if(!t.ok)throw o.end(),new D("Failed to get Service token",t);let r=await t.json();return o.end(),r}finally{clearTimeout(o)}};const be=e=>new Promise(t=>{setTimeout(t,e)}),we=e=>null==e;async function xe(){v.models=await(async()=>{let e=`${O(v)}/models`,t=R(v),n=v.timeoutMs,o=new AbortController,r=setTimeout(()=>o.abort(),n);try{let{response:n,span:r}=await k({spanName:"copilot GET /models",url:e,target:"models",headers:t,signal:o.signal});if(!n.ok)throw r.end(),new D("Failed to get models",n);let a=await n.json();return r.setAttribute("copilot_api.models_count",a.data.length),r.end(),a}finally{clearTimeout(r)}})()}let ve,Te,Ae,$e=!1;const Se=async()=>{let{token:e,refresh_in:n}=await ke();v._t1=e,Z.debug("Service token fetched successfully!"),v.showToken&&Z.info("Service token:",e);let o=async(e=5,n=1e3,o="scheduled")=>{if(!$e)return $e=!0,i.startActiveSpan("copilot_api.token.refresh",async r=>{let a=performance.now();r.setAttribute("copilot_api.token_refresh.reason",o),r.setAttribute("copilot_api.token_refresh.max_retries",e);try{for(let i=1;i<=e;i++)try{r.setAttribute("copilot_api.token_refresh.attempt",i),Z.debug(`Refreshing Service token (${o}, attempt ${i}/${e})`);let{token:n}=await ke();return v._t1=n,Z.debug("Service token refreshed successfully"),v.showToken&&Z.info("Refreshed Service token:",n),r.setAttribute("copilot_api.token_refresh.success",!0),t.add(1,{reason:o,success:"true"}),b.record(performance.now()-a,{reason:o}),void z(j.INFO,"INFO",`Token refreshed successfully (${o})`,{"copilot_api.token_refresh.reason":o,"copilot_api.token_refresh.attempt":i})}catch(l){let u=i===e;if(Z.error(`Failed to refresh Service token (attempt ${i}/${e}):`,l),t.add(1,{reason:o,success:"false"}),u){Z.error("All refresh attempts failed. May be unavailable until next scheduled refresh.");let t=l instanceof Error?l:Error(String(l));return r.setAttribute("copilot_api.token_refresh.success",!1),r.setStatus({code:oe.ERROR,message:t.message}),r.recordException(t),s.add(1,{reason:o}),b.record(performance.now()-a,{reason:o}),void z(j.ERROR,"ERROR",`Token refresh failed after ${e} attempts (${o})`,{"copilot_api.token_refresh.reason":o,"copilot_api.token_refresh.attempts":e})}let c=n*2**(i-1);Z.debug(`Retrying token refresh in ${c}ms...`),await new Promise(e=>setTimeout(e,c))}}finally{r.end(),$e=!1}});Z.debug("Refresh already in progress, skipping")};ve=setInterval(()=>{o(5,1e3,"scheduled").catch(e=>{Z.error("Unexpected error in scheduled token refresh:",e)})},1e3*(n-60)),H.start(),Te=()=>{Z.debug("Network connectivity restored, attempting immediate token refresh"),o(3,500,"network-restored").catch(e=>{Z.error("Unexpected error in network-restored token refresh:",e)})},Ae=()=>{Z.debug("Network connectivity lost")},H.on("online",Te),H.on("offline",Ae)};async function Oe(e){try{let t=await ee.readFile(x.GITHUB_TOKEN_PATH,"utf8");if(t&&!e?.force)return v._t2=t,v.showToken&&Z.info("Auth token:",t),void await Ie();Z.info("Authentication required");let n=await async function(){let e=new AbortController,t=v.timeoutMs,n=setTimeout(()=>{e.abort()},t);try{let t=await fetch(`${M}/login/device/code`,{method:"POST",headers:q(),body:JSON.stringify({client_id:U,scope:$}),signal:e.signal});if(!t.ok)throw new D("Failed to get device code",t);return await t.json()}finally{clearTimeout(n)}}();Z.debug("Device code response:",n),Z.info(`Please enter the code "${n.user_code}" in ${n.verification_uri}`);let o=await async function(e){let t=1e3*(e.interval+1);for(Z.debug(`Polling access token with interval of ${t}ms`);;){let n=new AbortController,o=v.timeoutMs,r=setTimeout(()=>{n.abort()},o);try{let o=await fetch(`${M}/login/oauth/access_token`,{method:"POST",headers:q(),body:JSON.stringify({client_id:U,device_code:e.device_code,grant_type:"urn:ietf:params:oauth:grant-type:device_code"}),signal:n.signal});if(!o.ok){await be(t),Z.error("Failed to poll access token:",await o.text());continue}let r=await o.json();Z.debug("Polling access token response:",r);let{access_token:a}=r;if(a)return a;await be(t)}catch(e){if(e instanceof Error&&"AbortError"===e.name){Z.error("Access token polling timed out"),await be(t);continue}throw e}finally{clearTimeout(r)}}}(n);await(e=>ee.writeFile(x.GITHUB_TOKEN_PATH,e))(o),v._t2=o,v.showToken&&Z.info("Auth token:",o),await Ie()}catch(e){throw e instanceof D?(Z.error("Failed to get Auth token:",await e.response.json()),e):(Z.error("Failed to get Auth token:",e),e)}}async function Ie(){let e=await L();Z.debug(`Authenticated as ${e.login}`)}const qe=Q({meta:{name:"auth",description:"Run authentication flow"},args:{verbose:{alias:"v",type:"boolean",default:!1,description:"Enable verbose logging"},"show-token":{type:"boolean",default:!1,description:"Show token on auth"}},run:({args:e})=>async function(e){e.verbose&&(Z.level=5,Z.info("Verbose logging enabled")),v.showToken=e.showToken,await T(),await Oe({force:!0}),Z.success("Auth token written to",x.GITHUB_TOKEN_PATH)}({verbose:e.verbose,showToken:e["show-token"]})}),Ne=async()=>{let e=`${E}/copilot_internal/user`,t=v.timeoutMs,n=new AbortController,o=setTimeout(()=>n.abort(),t);try{let{response:t,span:o}=await k({spanName:"github GET /copilot_internal/user",url:e,target:"usage",headers:W(v),signal:n.signal});if(!t.ok)throw o.end(),new D("Failed to get usage",t);let r=await t.json();return o.end(),r}finally{clearTimeout(o)}},je=Q({meta:{name:"check-usage",description:"Show current usage/quota information"},async run(){await T(),await Oe();try{let e=await Ne(),t=e.quota_snapshots.premium_interactions,n=t.entitlement,o=n-t.remaining,r=n>0?o/n*100:0,a=t.percent_remaining;function i(e,t){if(!t)return`${e}: N/A`;let n=t.entitlement,o=n-t.remaining,r=n>0?o/n*100:0,a=t.percent_remaining;return`${e}: ${o}/${n} used (${r.toFixed(1)}% used, ${a.toFixed(1)}% remaining)`}let s=`Premium: ${o}/${n} used (${r.toFixed(1)}% used, ${a.toFixed(1)}% remaining)`,l=i("Chat",e.quota_snapshots.chat),u=i("Completions",e.quota_snapshots.completions);Z.box(`Usage (plan: ${e.copilot_plan})\nQuota resets: ${e.quota_reset_date}\n\nQuotas:\n ${s}\n ${l}\n ${u}`)}catch(c){Z.error("Failed to fetch usage:",c),process.exit(1)}}});async function Ee(){try{let e=new URL("../package.json",import.meta.url).pathname,t=await ee.readFile(e);return JSON.parse(t.toString()).version}catch{return"unknown"}}function Pe(){let e=typeof Bun<"u";return{name:e?"bun":"node",version:e?Bun.version:process.version.slice(1),platform:te.platform(),arch:te.arch()}}async function Ce(){try{return!!(await ee.stat(x.GITHUB_TOKEN_PATH)).isFile()&&(await ee.readFile(x.GITHUB_TOKEN_PATH,"utf8")).trim().length>0}catch{return!1}}async function Re(e){let t=await async function(){let[e,t]=await Promise.all([Ee(),Ce()]);return{version:e,runtime:Pe(),paths:{APP_DIR:x.APP_DIR,GITHUB_TOKEN_PATH:x.GITHUB_TOKEN_PATH},tokenExists:t}}();e.json?function(e){console.log(JSON.stringify(e,null,2))}(t):function(e){Z.info(`service debug\n\nVersion: ${e.version}\nRuntime: ${e.runtime.name} ${e.runtime.version} (${e.runtime.platform} ${e.runtime.arch})\n\nPaths:\n- APP_DIR: ${e.paths.APP_DIR}\n- GITHUB_TOKEN_PATH: ${e.paths.GITHUB_TOKEN_PATH}\n\nToken exists: ${e.tokenExists?"Yes":"No"}`)}(t)}const Be=Q({meta:{name:"debug",description:"Print debug information about the application"},args:{json:{type:"boolean",default:!1,description:"Output debug information as JSON"}},run:({args:e})=>Re({json:e.json})}),De="copilot-api-node20",Me=`https://registry.npmjs.org/${De}/latest`,He=e=>`${e}`,Fe=e=>`${e}`;async function Je(){let e=new AbortController,t=setTimeout(()=>e.abort(),5e3);try{let t=await fetch(Me,{signal:e.signal});return t.ok?(await t.json()).version??null:null}catch{return null}finally{clearTimeout(t)}}function Ue(e,t){let n=Math.max(0,t-function(e){return e.replaceAll(/\x1b\[[0-9;]*m/g,"").length}(e));return e+" ".repeat(n)}const Le=new Map,We=new Map,Ke=new Set;let Ge,ze,Ye;function Ve(e){return!e.closed&&!e.destroyed}function Qe(e){Z.debug(`Creating HTTP/2 client for ${e}`);let t=new ie(e,{allowH2:!0,pipelining:0,keepAliveTimeout:6e4,initialWindowSize:4194304,connectionWindowSize:16777216});return Le.set(t,Date.now()),G.add(1,{event:"created",origin:e}),z(j.INFO,"INFO",`HTTP/2 client created for ${e}`,{"copilot_api.h2.event":"created","copilot_api.h2.origin":e}),t.on("connect",()=>{Z.debug(`HTTP/2 client connected to ${e}`)}),t.on("error",t=>{Z.debug(`HTTP/2 client error for ${e}:`,t.message)}),t.on("disconnect",(n,o,r)=>{Z.debug(`HTTP/2 client disconnected from ${e}:`,r.message),G.add(1,{event:"goaway",origin:e}),z(j.WARN,"WARN",`HTTP/2 client disconnected from ${e}: ${r.message}`,{"copilot_api.h2.event":"goaway","copilot_api.h2.origin":e,"copilot_api.h2.error":r.message}),We.delete(t),Ke.delete(t),Le.delete(t),Ve(t)&&t.close().catch(e=>{Z.debug("Error closing GOAWAY'd HTTP/2 client:",e)})}),t}function Xe(e){if(!v.connectivity.h2)return;Ge&&Ge!==e&&function(){for(let e of[...We.keys(),...Ke])Ve(e)&&e.close().catch(e=>{Z.debug("Error closing HTTP/2 pool entry:",e)});We.clear(),Ke.clear(),Le.clear()}(),Ge=e;let t=Date.now();for(let[n]of We)if(t-(Le.get(n)??0)>=6e5)We.delete(n),Le.delete(n),Ve(n)&&n.close().catch(e=>{Z.debug("Error closing rotated HTTP/2 client:",e)}),G.add(1,{event:"rotated",origin:e}),z(j.INFO,"INFO",`HTTP/2 client rotated for ${e} (max lifetime reached)`,{"copilot_api.h2.event":"rotated","copilot_api.h2.origin":e});else{if(Ve(n))return We.delete(n),Ke.add(n),n;We.delete(n),Le.delete(n)}let n=Qe(e);return Ke.add(n),n}function Ze(e){e&&Ke.delete(e)&&(Ve(e)?We.set(e,Date.now()):Le.delete(e))}function et(){Ye||(Ye=setInterval(()=>{!function(){let e=Date.now(),t=v.connectivity.h2Keepalive,n=We.size;for(let[o,r]of We)e-r<6e4||t&&n<=1||(We.delete(o),Le.delete(o),n--,Ve(o)&&o.close().catch(e=>{Z.debug("Error closing idle HTTP/2 client:",e)}),G.add(1,{event:"idle_reaped",origin:Ge??""}),z(j.INFO,"INFO",`HTTP/2 idle connection reaped for ${Ge}`,{"copilot_api.h2.event":"idle_reaped","copilot_api.h2.origin":Ge??""}))}()},3e4),Ye.unref())}function tt(){ze||(Z.debug("Starting HTTP/2 keepalive (every 45s)"),ze=setInterval(()=>{(async function(){if(0===We.size&&0===Ke.size){if(Ge){let e=Qe(Ge);We.set(e,Date.now())}return}let e=[...We.keys()];await Promise.allSettled(e.map(async e=>{if(!Ve(e))return We.delete(e),void Le.delete(e);try{let{statusCode:t,body:n}=await e.request({path:"/models",method:"GET",headers:R(v)});await n.dump(),y.add(1,{result:"success"}),Z.debug(`HTTP/2 keepalive: GET /models → ${t}`),We.set(e,Date.now())}catch(t){y.add(1,{result:"error"}),Z.warn("HTTP/2 keepalive failed:",t instanceof Error?t.message:t),G.add(1,{event:"error",origin:Ge??""}),We.delete(e),Le.delete(e),Ve(e)&&e.close().catch(e=>{Z.debug("Error closing broken HTTP/2 client:",e)})}}))})().catch(e=>{Z.debug("Unexpected error in HTTP/2 keepalive:",e)})},45e3),ze.unref(),et())}function nt(e,t=""){let n,o=function(){let{platform:e,ppid:t,env:n}=le;if("win32"===e){try{if(pe(`wmic process get ParentProcessId,Name | findstr "${t}"`,{stdio:"pipe"}).toString().toLowerCase().includes("powershell.exe"))return"powershell"}catch{return"cmd"}return"cmd"}{let e=n.SHELL;if(e){if(e.endsWith("zsh"))return"zsh";if(e.endsWith("fish"))return"fish";if(e.endsWith("bash"))return"bash"}return"sh"}}(),r=Object.entries(e).filter(([,e])=>void 0!==e);switch(o){case"powershell":n=r.map(([e,t])=>`$env:${e} = ${t}`).join("; ");break;case"cmd":n=r.map(([e,t])=>`set ${e}=${t}`).join(" & ");break;case"fish":n=r.map(([e,t])=>`set -gx ${e} ${t}`).join("; ");break;default:{let e=r.map(([e,t])=>`${e}=${t}`).join(" ");n=r.length>0?`export ${e}`:"";break}}return n&&t?`${n}${"cmd"===o?" & ":" && "}${t}`:n||t}const ot=e=>`${e}`,rt=e=>`${e}`,at=e=>`${e}`,it=e=>`${e}`,st=e=>`${e}`,lt=e=>`${e}`,ut=(e,t)=>`]8;;${e}\\${t}]8;;\\`,ct="?utm_source=copilot-cli&utm_medium=terminal&utm_campaign=startup_banner",pt=rt(" ─────────────────────────────────────────────────────────────");function dt(e){return void 0===e?"":e>=1e6?`${(e/1e6).toFixed(0)}M`:`${(e/1e3).toFixed(0)}K`}const mt=[{title:"The 5-Hour Quota, Boris's Tweet, and What the Source Code Actually Reveals",teaser:"Claude Code's caching story — decoded from the source.",url:"https://barazany.dev/blog/claude-code-token-caching"},{title:"Claude Code's Compaction Engine: What the Source Code Actually Reveals",teaser:"What's actually running beneath every long session.",url:"https://barazany.dev/blog/claude-codes-compaction-engine"},{title:"We Were About to Buy an Automation Platform. We Already Had One.",teaser:"Governance spirals over n8n. The answer was already deployed.",url:"https://barazany.dev/blog/we-already-had-the-automation-platform"}];function ft(e){if(0===e.length)return[];let t=["",pt,` ${ot("BARAZANY.DEV")} ${rt("— Recent Posts")}`,pt,""];for(let[n,o]of e.entries()){let e=ut(`${o.url}${ct}`,ot(lt(`[${n+1}]`)));t.push(` ${e} ${at(o.title)}`,` ${rt("→")} ${rt(o.teaser)}`,"")}return t.push(pt,` ${ut(`https://barazany.dev/blog${ct}`,"barazany.dev/blog")}`,pt),t}function _t(e){let t=[];v.connectivity.h2Keepalive?t.push("HTTP/2 keepalive"):v.connectivity.h2&&t.push("HTTP/2"),t.push(`timeout ${e.timeout/1e3}s`);let n=e.webSearch?`Tavily ${it("✓")}`:rt("off"),o=["",` ${ot("COPILOT-API")} ${rt(`v${e.version}`)}`,pt,` ${rt("Runtime")} ${e.runtime.name} ${e.runtime.version} (${e.runtime.platform}-${e.runtime.arch})`,` ${rt("User")} ${e.user} ${rt("·")} ${e.accountType}`];return v._v1&&o.push(` ${rt("Client")} VSCode ${v._v1}`),o.push(` ${rt("Network")} ${t.join(rt(" · "))}`,` ${rt("Search")} ${n}`),e.clipboardCopied&&o.push(` ${rt("Clipboard")} Claude Code command ${it("✓")}`),o.push(pt),o}function gt(e){if(!e.model&&!e.smallModel)return[];let t=["",` ${ot("Active")}`];if(e.model){let n=v.models?.data.find(t=>t.id===e.model),o=dt(n?.capabilities.limits?.max_context_window_tokens);t.push(` ${at(e.model.padEnd(32))} ${rt("ctx")} ${ot(o.padStart(5))} ${st("← main")}`)}if(e.smallModel){let n=v.models?.data.find(t=>t.id===e.smallModel),o=dt(n?.capabilities.limits?.max_context_window_tokens);t.push(` ${e.smallModel.padEnd(32)} ${rt("ctx")} ${o.padStart(5)} ${st("← small")}`)}return t}function ht(e){let t=[];for(let[n,o]of function(){let e=[],t=[],n=[],o=[];for(let r of v.models?.data??[]){let a=r.capabilities.limits?.max_context_window_tokens,i={id:r.id,ctx:a};r.id.startsWith("text-embedding")?o.push(i):"Anthropic"===r.vendor?e.push(i):"Google"===r.vendor?n.push(i):t.push(i)}return[["Claude",e],["OpenAI",t],["Gemini",n],["Embedding",o]]}())if(0!==o.length){t.push("",` ${ot(n)}`);for(let n of o){let o=n.id===e.model||n.id===e.smallModel,r=o?at("●"):rt("·"),a=o?at(n.id.padEnd(34)):n.id.padEnd(34),i=n.ctx?dt(n.ctx).padStart(5):"";t.push(` ${r} ${a} ${rt(i)}`)}}return t}function yt(e){let t=Math.floor(e/1e3),n=Math.floor(t/3600),o=Math.floor(t%3600/60);return n>0?`${n}h ${o}m`:o>0?`${o}m`:`${t}s`}function kt(e){(function(e){let{c:t,start:n,span:o}=e,r=t.get("requestData"),a=t.res.status,i=r?.model??"unknown",s=r?.streaming??!1,l=Date.now()-n;if(o.setAttribute("http.status_code",a),o.setAttribute("copilot_api.model",i),o.setAttribute("copilot_api.streaming",s),o.setAttribute("copilot_api.duration_ms",l),r?._d1&&o.setAttribute("copilot_api.copilot_duration_ms",r._d1),r?.tokenUsage){let e=r.tokenUsage;o.setAttribute("copilot_api.input_tokens",e.inputTokens),o.setAttribute("copilot_api.output_tokens",e.outputTokens),o.setAttribute("copilot_api.total_tokens",e.totalTokens),e.cachedTokens&&o.setAttribute("copilot_api.cached_tokens",e.cachedTokens),e.estimatedCost&&o.setAttribute("copilot_api.estimated_cost",e.estimatedCost)}t.res.headers.forEach((e,t)=>{let n=t.toLowerCase();B.has(n)||o.setAttribute(`http.response.header.${n}`,e)}),a>=400&&o.setStatus({code:oe.ERROR,message:`HTTP ${a}`}),o.end()})(e),function(e){let{c:t,start:n,method:r,path:i,endpointType:s,requestId:u}=e,c=Date.now()-n,p=t.get("requestData"),m=t.res.status,_=p?.model??"unknown",g=p?.streaming??!1,y={method:r,path:i,status_code:String(m),model:_,streaming:String(g),endpoint_type:s};if(l.add(1,y),a.record(c,y),m>=400&&h.add(1,{method:r,path:i,status_code:String(m),error_type:m>=500?"server_error":"client_error"}),p?.tokenUsage){let e=p.tokenUsage,t={model:_,endpoint_type:s};e.inputTokens&&(o.add(e.inputTokens,t),f.record(e.inputTokens,t)),e.outputTokens&&(P.add(e.outputTokens,t),d.record(e.outputTokens,t)),e.cachedTokens&&K.add(e.cachedTokens,{model:_})}!g&&p?.tokenUsage&&F.executeCompletion(u)}(e)}const bt=async()=>i.startActiveSpan("copilot_api.approval.await",async e=>{let t=performance.now();try{let n=await Z.prompt("Accept incoming request?",{type:"confirm"}),o=performance.now()-t;if(e.setAttribute("copilot_api.approval.wait_duration_ms",o),A.record(o),!n)throw e.setAttribute("copilot_api.approval.result","rejected"),N.add(1,{result:"rejected"}),new D("Request rejected",Response.json({message:"Request rejected"},{status:403}));e.setAttribute("copilot_api.approval.result","approved"),N.add(1,{result:"approved"})}finally{e.end()}}),wt=3e-6,xt=15e-6;function vt(e){let t=e.prompt_tokens||0,n=e.completion_tokens||0;return{inputTokens:t,outputTokens:n,totalTokens:e.total_tokens||t+n,estimatedCost:t*wt+n*xt,cachedTokens:e.prompt_tokens_details?.cached_tokens,acceptedPredictionTokens:e.completion_tokens_details?.accepted_prediction_tokens,rejectedPredictionTokens:e.completion_tokens_details?.rejected_prediction_tokens}}function Tt(e,t){void 0!==t.prompt_tokens&&e.setAttribute("copilot_api.input_tokens",t.prompt_tokens),void 0!==t.completion_tokens&&e.setAttribute("copilot_api.output_tokens",t.completion_tokens),void 0!==t.total_tokens&&e.setAttribute("copilot_api.total_tokens",t.total_tokens)}const At=[{pattern:/haiku/i,family:"haiku"},{pattern:/sonnet[.-]?4[.-]6/i,family:"sonnet-4.6"},{pattern:/(sonnet-4[.-]5|[.-]?3[.-]5[.-]?sonnet|[.-]?3[.-]7[.-]?sonnet)/i,family:"sonnet-4.5"},{pattern:/sonnet-4(?![.-]5|[.-]6|[.-]1)/i,family:"sonnet-4"},{pattern:/opus[.-]?4[.-]7/i,family:"opus-4.7"},{pattern:/opus[.-]?4[.-]6/i,family:"opus-4.6"},{pattern:/opus[.-]?4[.-]5/i,family:"opus-4.5"},{pattern:/opus/i,family:"opus"}],$t={haiku:"claude-haiku-4.5","sonnet-4.6":"claude-sonnet-4.6","sonnet-4.5":"claude-sonnet-4.5","sonnet-4":"claude-sonnet-4","opus-4.7":"claude-opus-4.7","opus-4.6":"claude-opus-4.6","opus-4.5":"claude-opus-4.5",opus:"claude-opus-4.5"};function St(e,t){let n=function(e){return e.startsWith("anthropic/")?e.slice(10):e}(e),[o,r]=function(e){let t=e.lastIndexOf("["),n=e.lastIndexOf("]");return-1!==t&&n===e.length-1&&n>t?[e.slice(0,t),e.slice(t+1,n)]:[e,null]}(n);if(t.includes(n))return n;if(r){let e=`${o}-${r}`.replaceAll(/(\d)-(\d)\b/g,"$1.$2");if(t.includes(e))return e}let a=function(e){for(let t of At)if(t.pattern.test(e))return t.family;return null}(o);if(!a)return null;let i=o.replace(/-\d{8}(?::.*)?$/,""),s=r?`-${r}`:"";for(let e of t){let t=e.replace(/-\d{8}(?::.*)?$/,""),n=`${i}${s}`.toLowerCase().replaceAll(/(\d)-(\d)\b/g,"$1.$2");if(t.toLowerCase().replaceAll(/(\d)-(\d)\b/g,"$1.$2")===n)return e}return function(e,t,n=null){let o=$t[e];if(!o)return null;if(n){let e=`${o}-${n}`;if(t.includes(e))return e}if(t.includes(o))return o;if("sonnet-4.5"===e)for(let e of["claude-3.5-sonnet"]){let o=n?`${e}-${n}`:e;if(t.includes(o))return o}return null}(a,t,r)}var Ot=class extends Error{statusCode;constructor(e,t=400){super(e),this.name="ModelValidationError",this.statusCode=t}};function It(t){if(!t||"string"!=typeof t)throw new Ot("Model name is required and must be a string");if(!v.models?.data)throw new Ot("Models not available. Please try again later.",503);let n=v.models.data.map(e=>e.id);if(n.includes(t))return t;let o=St(t,n);if(!o)throw e.add(1,{requested_model:t}),new Ot(`Invalid model: '${t}'. Available services: ${n.join(", ")}`);return o}async function qt(e){if(void 0===e.rateLimitSeconds)return;let t=e.rateLimitSeconds;return i.startActiveSpan("copilot_api.rate_limit.check",async n=>{try{let o=Date.now();if(n.setAttribute("copilot_api.rate_limit.configured_seconds",t),!e.lastRequestTimestamp)return e.lastRequestTimestamp=o,void n.setAttribute("copilot_api.rate_limit.action","allowed");let r=(o-e.lastRequestTimestamp)/1e3;if(n.setAttribute("copilot_api.rate_limit.elapsed_seconds",r),r>t)return e.lastRequestTimestamp=o,void n.setAttribute("copilot_api.rate_limit.action","allowed");let a=Math.ceil(t-r);if(n.setAttribute("copilot_api.rate_limit.wait_seconds",a),!e.rateLimitWait)throw n.setAttribute("copilot_api.rate_limit.action","rejected"),m.add(1,{action:"rejected",source:"local"}),Z.warn(`Rate limit exceeded. Need to wait ${a} more seconds.`),new D("Rate limit exceeded",Response.json({message:"Rate limit exceeded"},{status:429}));let i=1e3*a;n.setAttribute("copilot_api.rate_limit.action","waited"),m.add(1,{action:"waited",source:"local"}),_.record(i,{source:"local"}),Z.warn(`Rate limit reached. Waiting ${a} seconds before proceeding...`),await be(i),e.lastRequestTimestamp=o,Z.info("Rate limit wait completed, proceeding with request")}finally{n.end()}})}function Nt(e){if("number"==typeof e){if(!Number.isFinite(e))return null;let t=Math.floor(e);return t>0?t:null}if("string"==typeof e){let t=e.trim();if(!t)return null;let n=Number(t);if(!Number.isFinite(n))return null;let o=Math.floor(n);return o>0?o:null}return null}function jt(e,t,n){return Math.min(Math.max(e,t),n)}function Et(e,t=0){if(t>5||!e||"object"!=typeof e)return null;let n=e.error;if(!n||"object"!=typeof n)return null;let o=n.code;if("string"==typeof o)return o;let r=n.message;if("string"!=typeof r)return null;try{return Et(JSON.parse(r),t+1)}catch{return null}}function Pt(e,t){let n=e?.data.find(e=>e.id===t);if(!n)return null;let o=Nt(n.capabilities.supports.min_thinking_budget),r=Nt(n.capabilities.supports.max_thinking_budget);return!o||!r||r<=o?null:{bounds:{min:o,max:r-1},maxOutputTokens:Nt(n.capabilities.limits?.max_output_tokens)??void 0}}function Ct(e){let{config:t}=e,n=t.bounds.min,o=t.bounds.max,r=jt(Nt(e.requestedBudget)??n,n,o),a=Nt(e.maxTokens);if(a||=r+1,t.maxOutputTokens&&a>t.maxOutputTokens&&(a=t.maxOutputTokens),a<=r){let e=Math.min(r,a-1);if(e>=n)r=e;else{let e=n+1;if(t.maxOutputTokens&&e>t.maxOutputTokens)return{thinkingBudget:null,maxTokens:a};a=e,r=n}}return a<=r?{thinkingBudget:null,maxTokens:a}:{thinkingBudget:r,maxTokens:a}}function Rt(e,t,n){if(we(e.thinking_budget))return e;if(!1===n?.enabled)return{...e,thinking_budget:void 0};let o=Pt(t,e.model);if(!o)return{...e,thinking_budget:void 0};let r=Ct({requestedBudget:e.thinking_budget,maxTokens:e.max_tokens,config:o});return null===r.thinkingBudget?{...e,max_tokens:r.maxTokens,thinking_budget:void 0}:{...e,max_tokens:r.maxTokens,thinking_budget:r.thinkingBudget}}function Bt(e){return"invalid_thinking_budget"===Et(e)}async function Dt(e){let{payload:t,error:n,models:o,errorBodyText:r}=e;if(we(t.thinking_budget)||!(n instanceof Error))return null;let a,i,s=n;if(!(s.response instanceof Response)||400!==s.response.status)return null;if(void 0!==r)a=r;else try{a=await s.response.clone().text()}catch{return null}try{i=JSON.parse(a)}catch{return null}if(!Bt(i))return null;let l=Pt(o,t.model);return l?function(e,t){let n=Ct({requestedBudget:e.thinking_budget,maxTokens:e.max_tokens,config:t});if(null===n.thinkingBudget)return{...e,max_tokens:n.maxTokens,thinking_budget:void 0};let o=n.thinkingBudget,r=Nt(e.thinking_budget);if(r&&r===n.thinkingBudget){let e=o+1;e<=t.bounds.max&&(!n.maxTokens||e<n.maxTokens)&&(o=e)}let a=n.maxTokens;if((!a||a<=o)&&(a=o+1),t.maxOutputTokens&&a>t.maxOutputTokens){if(!(t.maxOutputTokens>o))return{...e,max_tokens:t.maxOutputTokens,thinking_budget:void 0};a=t.maxOutputTokens}return{...e,max_tokens:a,thinking_budget:o}}(t,l):{...t,thinking_budget:void 0}}function Mt(e){if(!(e instanceof Error))return!1;let t=e.code;return(!t||"UND_ERR_SOCKET"===t)&&/GOAWAY/i.test(e.message)}async function Ht(){return new Promise(e=>setTimeout(e,100))}function Ft(e){if("string"==typeof e)return function(e){let t=e,n=String.fromCodePoint(27),o=t.split(n);if(o.length>1){t=o[0];for(let e=1;e<o.length;e++){let r=o[e],a=r.match(/^\[[0-9;]*[a-z]/i);t+=a?r.slice(a[0].length):n+r}}return t.replaceAll(/[\u200B-\u200D\uFEFF]/g,"").replaceAll(/[\u2060-\u2064]/g,"").replaceAll(/[\u206A-\u206F]/g,"").replaceAll(/[\u180E\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]/g,"").replaceAll(/[\uFFF0-\uFFFF]/g,"").replaceAll(/[\x00-\x08\v\f\x0E-\x1F]/g,"")}(e);if(Array.isArray(e))return e.map(e=>Ft(e));if(e&&"object"==typeof e){let t={};for(let[n,o]of Object.entries(e))t[n]=Ft(o);return"object"===t.type&&!("properties"in t)&&(t.properties={}),t}return e}const Jt=async e=>{let t=Ft(e);try{return await Ut(t)}catch(e){let n;if(e instanceof D)try{n=await e.response.clone().text()}catch{n=void 0}let o=await Dt({payload:t,error:e,models:v.models,errorBodyText:n});if(o)return e instanceof D&&void 0!==n&&Z.warn(`Upstream rejected thinking_budget (status ${e.response.status}): ${n.slice(0,500)}`),Z.warn("Adjusting thinking_budget payload and retrying once..."),await Ut(Ft(o));throw e}},Ut=async e=>{if(!v._t1)throw Error("Service token not found");let t=e.messages.some(e=>"string"!=typeof e.content&&e.content?.some(e=>"image_url"===e.type)),n=e.messages.some(e=>["assistant","tool"].includes(e.role)),o={...R(v,t),"X-Initiator":n?"agent":"user"},r=O(v),a=`${r}/chat/completions`,i=v.timeoutMs,s=JSON.stringify(e),l={"copilot_api.model":e.model,"copilot_api.streaming":!!e.stream,"copilot_api.timeout_ms":i,"copilot_api.has_tools":!!e.tools?.length,"copilot_api.tool_count":e.tools?.length??0,"copilot_api.message_count":e.messages.length,"copilot_api.max_tokens":e.max_tokens??0,"copilot_api.vision_enabled":t,"copilot_api.initiator":n?"agent":"user"};for(let t=0;t<=1;t++){let n=Xe(r),u=new AbortController,c=setTimeout(()=>u.abort(),i);try{let{statusCode:r,headers:c,body:p,span:d}=await w({spanName:"copilot POST /chat/completions",url:a,target:"chat_completions",headers:o,body:s,signal:u.signal,headersTimeout:i,bodyTimeout:3*i,extraAttributes:{...l,"copilot_api.h2.retry_attempt":t},dispatcher:n}),m=new Response(p,{status:r,headers:c});if(!m.ok)throw d.end(),new D("Failed to create chat completions",m);if(e.stream)return{events:_e(m),span:d,release:()=>Ze(n)};let f=await m.json();return f.id&&d.setAttribute("copilot_api.response.id",f.id),f.usage&&Tt(d,f.usage),d.end(),Ze(n),f}catch(e){if(Ze(n),Mt(e)&&t<1){Z.warn(`HTTP/2 GOAWAY during chat completions, retrying (attempt ${t+1})`),await Ht();continue}throw e}finally{clearTimeout(c)}}throw Error("Unexpected: exhausted GOAWAY retries without result")};function Lt(e){return i.startActiveSpan("copilot_api.model.validate",t=>{try{t.setAttribute("copilot_api.model.requested",e.model);let n=It(e.model);return t.setAttribute("copilot_api.model.normalized",n),n===e.model?(t.setAttribute("copilot_api.model.was_mapped",!1),e):(t.setAttribute("copilot_api.model.was_mapped",!0),C.add(1,{from_model:e.model,to_model:n}),Z.debug(`Normalized model from '${e.model}' to '${n}'`),{...e,model:n})}finally{t.end()}})}function Wt(e,t){if(!e.data)return e;try{let n=JSON.parse(e.data);if(n.usage&&t(n.usage),void 0!==n.usage?.prompt_tokens_details?.cached_tokens)return{...e,data:JSON.stringify({...n,usage:{...n.usage,prompt_tokens_details:void 0}})}}catch{}return e}const Kt=e=>Object.hasOwn(e,"choices"),Gt=new de;Gt.post("/",async e=>{try{return await async function(e){await qt(v);let t=await e.req.json();Z.debug("Request payload:",JSON.stringify(t).slice(-400));try{t=Lt(t)}catch(t){if(t instanceof Ot)return e.json({error:{message:t.message,type:"invalid_request_error",code:"invalid_model"}},t.statusCode);throw t}if(e.set("requestData",{model:t.model,streaming:!!t.stream}),v.manualApprove&&await bt(),we(t.max_tokens)){let e=v.models?.data.find(e=>e.id===t.model);t={...t,max_tokens:e?.capabilities.limits?.max_output_tokens},Z.debug("Set max_tokens to:",JSON.stringify(t.max_tokens))}t=Rt(t,v.models,{enabled:v.thinkingEnabled}),we(t.thinking_budget)||Z.debug("Normalized thinking_budget to:",JSON.stringify(t.thinking_budget));let n=performance.now(),o=await Jt(t),r=performance.now()-n;if(S.record(r,{model:t.model,target:"chat_completions",streaming:String(!!t.stream)}),Kt(o)){let t=e.get("requestData")||{};return o.usage&&(t.tokenUsage=vt(o.usage)),t._d1=r,e.set("requestData",t),Z.debug("Non-streaming response:",JSON.stringify(o)),e.json(o)}let{events:a,span:i,release:s}=o;return fe(e,async n=>{let o={value:null},l=0,u=performance.now();try{try{for await(let i of a){let a=i;i.data&&"[DONE]"!==i.data&&(l++,g.add(1,{model:t.model,endpoint_type:"openai"}),a=Wt(i,t=>{o.value=t;let n=e.get("requestData")||{};n.tokenUsage=vt(t),n._d1=r,e.set("requestData",n)})),await n.writeSSE(a)}let i=performance.now()-u,s={model:t.model,endpoint_type:"openai"};if(V.record(i,s),Y.record(l,s),o.value){let t=e.get("requestData")||{};t.tokenUsage||(t.tokenUsage=vt(o.value),t._d1=r,e.set("requestData",t))}}finally{o.value&&Tt(i,o.value),i.end(),s()}let c=e.get("requestId");c&&F.executeCompletion(c)}finally{e.get("resolveStreamComplete")?.()}})}(e)}catch(t){return await J(e,t)}});const zt=new de;zt.post("/",async e=>{try{let t=await e.req.json();try{It(t.model)}catch(t){if(t instanceof Ot)return e.json({error:{message:t.message,type:"invalid_request_error",code:"invalid_model"}},t.statusCode);throw t}e.set("requestData",{model:t.model});let n=performance.now(),o=await(async e=>{if(!v._t1)throw Error("Service token not found");let t=`${O(v)}/embeddings`,n=R(v),o=v.timeoutMs,r=new AbortController,a=setTimeout(()=>r.abort(),o);try{let{response:o,span:a}=await k({spanName:"copilot POST /embeddings",url:t,target:"embeddings",method:"POST",headers:n,body:JSON.stringify(e),signal:r.signal,extraAttributes:{"copilot_api.model":e.model,"copilot_api.input_count":Array.isArray(e.input)?e.input.length:1}});if(!o.ok)throw a.end(),new D("Failed to create embeddings",o);let i=await o.json();return a.end(),i}finally{clearTimeout(a)}})(t),r=performance.now()-n;S.record(r,{model:t.model,target:"embeddings",streaming:"false"});let a=e.get("requestData")||{};return a.tokenUsage=vt(o.usage),a._d1=r,e.set("requestData",a),e.json(o)}catch(t){return await J(e,t)}});const Yt=new ge(ye),Vt={...he["anthropic/claude-sonnet-4"].tokens,baseOverhead:4,perTool:27};function Qt(e){return Yt.encode(e).length}function Xt(e){let t=0;for(let n of e)switch(n.type){case"text":t+=Qt(n.text)*Vt.contentMultiplier;break;case"tool_use":t+=Qt(n.name+JSON.stringify(n.input))*Vt.contentMultiplier;break;case"tool_result":n.content&&(t+=Qt("string"==typeof n.content?n.content:JSON.stringify(n.content))*Vt.contentMultiplier);break;case"thinking":t+=Qt(n.thinking)*Vt.contentMultiplier;break;case"image":t+=1600}return t}function Zt(e){let t=0,n=e.properties;if(!n)return t;let o=Object.entries(n);for(let[e,[n,r]]of o.entries()){if(t+=0===e?Vt.perFirstProp:Vt.perAdditionalProp,t+=Qt(n),r.description&&(t+=Vt.perPropDesc+Qt(r.description)),r.enum&&Array.isArray(r.enum)){t+=Vt.perEnum;for(let e of r.enum)t+=Qt(String(e))}if("object"===r.type&&r.properties&&(t+=Vt.perNestedObject,t+=Zt(r)),"array"===r.type&&r.items){let e=r.items;"object"===e.type&&e.properties&&(t+=Vt.perArrayOfObjects,t+=Zt(e))}}return t}const en=async e=>{if(!v._t1)throw Error("Service token not found");let t=Ft(e),n={...R(v),"X-Initiator":"agent"},o=O(v),r=`${o}/responses`,a=v.timeoutMs,i=JSON.stringify(t),s={"copilot_api.model":t.model,"copilot_api.streaming":!!t.stream,"copilot_api.timeout_ms":a,"copilot_api.has_tools":!!t.tools?.length,"copilot_api.tool_count":t.tools?.length??0,"copilot_api.max_output_tokens":t.max_output_tokens??0,"copilot_api.api_type":"responses"};for(let e=0;e<=1;e++){let l=Xe(o),u=new AbortController,c=setTimeout(()=>u.abort(),a);try{let{statusCode:o,headers:c,body:p,span:d}=await w({spanName:"copilot POST /responses",url:r,target:"responses",headers:n,body:i,signal:u.signal,headersTimeout:a,bodyTimeout:3*a,extraAttributes:{...s,"copilot_api.h2.retry_attempt":e},dispatcher:l}),m=new Response(p,{status:o,headers:c});if(!m.ok)throw d.end(),new D("Failed to create responses",m);if(t.stream)return{events:_e(m),span:d,release:()=>Ze(l)};let f=await m.json();return f.id&&d.setAttribute("copilot_api.response.id",f.id),f.usage&&Tt(d,{prompt_tokens:f.usage.input_tokens,completion_tokens:f.usage.output_tokens,total_tokens:f.usage.total_tokens}),d.end(),Ze(l),f}catch(t){if(Ze(l),Mt(t)&&e<1){Z.warn(`HTTP/2 GOAWAY during responses, retrying (attempt ${e+1})`),await Ht();continue}throw t}finally{clearTimeout(c)}}throw Error("Unexpected: exhausted GOAWAY retries without result")};function tn(e){return null===e?null:{stop:"end_turn",length:"max_tokens",tool_calls:"tool_use",content_filter:"end_turn"}[e]}function nn(e,t){let n={model:e.model,messages:on(e.messages,e.system),max_tokens:e.max_tokens,stop:e.stop_sequences,stream:e.stream,temperature:e.temperature,top_p:e.top_p,user:e.metadata?.user_id,tools:un(e.tools),tool_choice:cn(e.tool_choice)},o=e.thinking;if("enabled"===o?.type||"adaptive"===o?.type){let r=Pt(t,n.model);if(r){let a="enabled"===o.type&&"number"==typeof o.budget_tokens?o.budget_tokens:function(e,t,n){let o=function(e,t){return"max"===e?t:"high"===e?16e3:"medium"===e?8e3:1024}(e,n.max),r=Nt(t),a=r&&r>1?r-1:n.min;return jt(Math.min(o,a),n.min,n.max)}(e.output_config?.effort??"high",n.max_tokens,r.bounds);n=Rt({...n,thinking_budget:a},t)}}return n}function on(e,t){let n=function(e){return e?"string"==typeof e?[{role:"system",content:rn(e)}]:[{role:"system",content:rn(e.map(e=>e.text).join("\n\n"))}]:[]}(t),o=e.flatMap(e=>"user"===e.role?function(e){let t=[];if(Array.isArray(e.content)){let n=e.content.filter(e=>"tool_result"===e.type),o=e.content.filter(e=>"tool_result"!==e.type);for(let e of n)t.push({role:"tool",tool_call_id:e.tool_use_id,content:ln(e.content)});o.length>0&&t.push({role:"user",content:ln(o)})}else t.push({role:"user",content:ln(e.content)});return t}(e):function(e){let t=function(e){return{reasoning_text:sn(e.reasoning_text),reasoning_opaque:sn(e.reasoning_opaque)}}(e);if(!Array.isArray(e.content))return[{role:"assistant",content:ln(e.content),...t}];let n=e.content.filter(e=>"tool_use"===e.type),o=e.content.filter(e=>"text"===e.type),r=e.content.filter(e=>"thinking"===e.type),a=[...o.map(e=>e.text),...r.map(e=>e.thinking)].join("\n\n");return n.length>0?[{role:"assistant",content:a||null,...t,tool_calls:n.map(e=>({id:e.id,type:"function",function:{name:e.name,arguments:JSON.stringify(e.input)}}))}]:[{role:"assistant",content:ln(e.content),...t}]}(e));return[...n,...o]}function rn(e){return e.startsWith("x-anthropic-billing-header")?e.replace(/^x-_an1-billing-header:[^\n]*\n+/,""):e}const an=32768;function sn(e){if("string"==typeof e)return e.length<=an?e:e.slice(0,an)}function ln(e){if("string"==typeof e)return e;if(!Array.isArray(e))return null;if(!e.some(e=>"image"===e.type))return e.filter(e=>"text"===e.type||"thinking"===e.type).map(e=>"text"===e.type?e.text:e.thinking).join("\n\n");let t=[];for(let n of e)switch(n.type){case"text":t.push({type:"text",text:n.text});break;case"thinking":t.push({type:"text",text:n.thinking});break;case"image":t.push({type:"image_url",image_url:{url:`data:${n.source.media_type};base64,${n.source.data}`}})}return t}function un(e){if(e)return e.map(e=>({type:"function",function:{name:e.name,description:e.description,parameters:e.input_schema}}))}function cn(e){if(e)switch(e.type){case"auto":return"auto";case"any":return"required";case"tool":return e.name?{type:"function",function:{name:e.name}}:void 0;case"none":return"none";default:return}}function pn(e){return"string"==typeof e?[{type:"text",text:e}]:Array.isArray(e)?e.filter(e=>"text"===e.type).map(e=>({type:"text",text:e.text})):[]}function dn(e){return e.reasoning_text?[{type:"thinking",thinking:e.reasoning_text}]:[]}function mn(e){return e?e.map(e=>({type:"tool_use",id:e.id,name:e.function.name,input:JSON.parse(e.function.arguments)})):[]}function fn(e){let t={model:e.model,input:gn(e.messages),max_output_tokens:Math.max(e.max_tokens,16),stream:e.stream,temperature:e.temperature,top_p:e.top_p,store:!1,user:e.metadata?.user_id?.slice(0,64)},n=function(e){return e?function(e){return e.startsWith("x-anthropic-billing-header")?e.replace(/^x-_an1-billing-header:[^\n]*\n+/,""):e}("string"==typeof e?e:e.map(e=>e.text).join("\n\n")):null}(e.system);return n&&(t.instructions=n),e.tools?.length&&(t.tools=e.tools.map(e=>({type:"function",name:e.name,description:e.description,parameters:e.input_schema}))),e.tool_choice&&(t.tool_choice=function(e){switch(e.type){case"auto":return"auto";case"any":return"required";case"none":return"none";case"tool":return e.name?{type:"function",name:e.name}:void 0;default:return}}(e.tool_choice)),"adaptive"===e.thinking?.type?t.reasoning={effort:_n(e.output_config?.effort??"high")}:"enabled"===e.thinking?.type&&e.thinking.budget_tokens&&(t.reasoning=function(e){return e<5e3?{effort:"low"}:e<2e4?{effort:"medium"}:e<32e3?{effort:"high"}:{effort:"xhigh"}}(e.thinking.budget_tokens)),t}function _n(e){switch(e){case"low":return"low";case"medium":return"medium";case"max":return"xhigh";default:return"high"}}function gn(e){let t=[];for(let n of e)"user"===n.role?t.push(...hn(n)):t.push(...kn(n));return t}function hn(e){if("string"==typeof e.content)return[{type:"message",role:"user",content:e.content}];let t=[],n=e.content.filter(e=>"tool_result"===e.type),o=e.content.filter(e=>"tool_result"!==e.type);for(let e of n)t.push(yn(e));if(o.length>0)if(o.some(e=>"image"===e.type)){let e=[];for(let t of o)"text"===t.type?e.push({type:"input_text",text:t.text}):e.push({type:"input_image",image_url:`data:${t.source.media_type};base64,${t.source.data}`});t.push({type:"message",role:"user",content:e})}else{let e=o.filter(e=>"text"===e.type).map(e=>e.text).join("\n\n");e&&t.push({type:"message",role:"user",content:e})}return t}function yn(e){let t;return t="string"==typeof e.content?e.content:Array.isArray(e.content)?e.content.filter(e=>"text"===e.type).map(e=>e.text).join("\n"):"",e.is_error&&(t=`[ERROR] ${t}`),{type:"function_call_output",call_id:e.tool_use_id,output:t}}function kn(e){if("string"==typeof e.content)return[{type:"message",role:"assistant",content:e.content}];let t=[],n=e.content.filter(e=>"text"===e.type),o=e.content.filter(e=>"thinking"===e.type),r=e.content.filter(e=>"tool_use"===e.type),a=[...n.map(e=>e.text),...o.map(e=>e.thinking)].join("\n\n");a&&t.push({type:"message",role:"assistant",content:a});for(let e of r)t.push({type:"function_call",call_id:e.id,name:e.name,arguments:JSON.stringify(e.input)});return t}function bn(e,t){if(t)return"tool_use";switch(e){case"completed":case"cancelled":case"failed":return"end_turn";case"incomplete":return"max_tokens";default:return null}}function wn(e,t){e.contentBlockOpen&&=(t.push({type:"content_block_stop",index:e.contentBlockIndex}),e.contentBlockIndex++,!1)}function xn(e,t,n){e.messageStartSent||=(e.responseId=n?.responseId??e.responseId,e.model=n?.model??e.model,t.push({type:"message_start",message:{id:e.responseId,type:"message",role:"assistant",content:[],model:e.model,stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0}}}),!0)}function vn(e,t){let n=[];switch(e.type){case"response.created":xn(t,n,{responseId:e.response.id,model:e.response.model});break;case"response.in_progress":case"response.output_text.done":case"response.function_call_arguments.done":case"response.refusal.done":case"response.reasoning_summary_text.delta":case"response.reasoning_summary_text.done":default:break;case"response.output_item.added":if("function_call"===e.item.type){xn(t,n),wn(t,n);let o=e.item;t.toolCalls[e.output_index]={call_id:o.call_id,name:o.name,_bi1:t.contentBlockIndex},n.push({type:"content_block_start",index:t.contentBlockIndex,content_block:{type:"tool_use",id:o.call_id,name:o.name,input:{}}}),t.contentBlockOpen=!0}break;case"response.content_part.added":xn(t,n),wn(t,n),n.push({type:"content_block_start",index:t.contentBlockIndex,content_block:{type:"text",text:""}}),t.contentBlockOpen=!0;break;case"response.output_text.delta":n.push({type:"content_block_delta",index:t.contentBlockIndex,delta:{type:"text_delta",text:e.delta}});break;case"response.content_part.done":wn(t,n);break;case"response.function_call_arguments.delta":{let o=t.toolCalls[e.output_index];o&&n.push({type:"content_block_delta",index:o._bi1,delta:{type:"input_json_delta",partial_json:e.delta}});break}case"response.output_item.done":if("function_call"===e.item.type){let o=t.toolCalls[e.output_index];o&&t.contentBlockOpen&&(n.push({type:"content_block_stop",index:o._bi1}),t.contentBlockIndex++,t.contentBlockOpen=!1)}break;case"response.completed":{wn(t,n);let o=e.response.output.some(e=>"function_call"===e.type),r=e.response.usage;n.push({type:"message_delta",delta:{stop_reason:bn(e.response.status,o),stop_sequence:null},usage:{input_tokens:r?.input_tokens??0,output_tokens:r?.output_tokens??0}},{type:"message_stop"});break}case"response.incomplete":{xn(t,n),wn(t,n);let o=e.response.usage;n.push({type:"message_delta",delta:{stop_reason:"max_tokens",stop_sequence:null},usage:{input_tokens:o?.input_tokens??0,output_tokens:o?.output_tokens??0}},{type:"message_stop"});break}case"response.failed":xn(t,n),wn(t,n),n.push({type:"error",error:{type:e.response.error?.code??"api_error",message:e.response.error?.message??"Response generation failed"}});break;case"response.cancelled":xn(t,n),wn(t,n),n.push({type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{input_tokens:0,output_tokens:0}},{type:"message_stop"});break;case"response.refusal.delta":xn(t,n),t.contentBlockOpen||=(n.push({type:"content_block_start",index:t.contentBlockIndex,content_block:{type:"text",text:""}}),!0),n.push({type:"content_block_delta",index:t.contentBlockIndex,delta:{type:"text_delta",text:e.delta}})}return n}function Tn(e,t){e.thinkingBlockOpen||(e.contentBlockOpen&&=(t.push({type:"content_block_stop",index:e.contentBlockIndex}),e.contentBlockIndex++,!1),t.push({type:"content_block_start",index:e.contentBlockIndex,content_block:{type:"thinking",thinking:""}}),e.thinkingBlockOpen=!0,e.contentBlockOpen=!0)}function An(e,t){e.thinkingBlockOpen&&(t.push({type:"content_block_stop",index:e.contentBlockIndex}),e.contentBlockIndex++,e.thinkingBlockOpen=!1,e.contentBlockOpen=!1)}function $n(e,t){let n=[];if(0===e.choices.length)return n;let o=e.choices[0],{delta:r}=o;if(t.messageStartSent||=(n.push({type:"message_start",message:{id:e.id,type:"message",role:"assistant",content:[],model:e.model,stop_reason:null,stop_sequence:null,usage:{input_tokens:e.usage?.prompt_tokens??0,output_tokens:0}}}),!0),"string"==typeof r.reasoning_text&&""!==r.reasoning_text&&(Tn(t,n),n.push({type:"content_block_delta",index:t.contentBlockIndex,delta:{type:"thinking_delta",thinking:r.reasoning_text}})),"string"==typeof r.reasoning_opaque&&""!==r.reasoning_opaque&&(Tn(t,n),n.push({type:"content_block_delta",index:t.contentBlockIndex,delta:{type:"signature_delta",signature:r.reasoning_opaque}}),An(t,n)),r.content&&(An(t,n),function(e){return!!e.contentBlockOpen&&Object.values(e.toolCalls).some(t=>t._bi1===e.contentBlockIndex)}(t)&&(n.push({type:"content_block_stop",index:t.contentBlockIndex}),t.contentBlockIndex++,t.contentBlockOpen=!1),t.contentBlockOpen||=(n.push({type:"content_block_start",index:t.contentBlockIndex,content_block:{type:"text",text:""}}),!0),n.push({type:"content_block_delta",index:t.contentBlockIndex,delta:{type:"text_delta",text:r.content}})),r.tool_calls){An(t,n);for(let e of r.tool_calls){if(e.id&&e.function?.name){t.contentBlockOpen&&=(n.push({type:"content_block_stop",index:t.contentBlockIndex}),t.contentBlockIndex++,!1);let o=t.contentBlockIndex;t.toolCalls[e.index]={id:e.id,name:e.function.name,_bi1:o},n.push({type:"content_block_start",index:o,content_block:{type:"tool_use",id:e.id,name:e.function.name,input:{}}}),t.contentBlockOpen=!0}if(e.function?.arguments){let o=t.toolCalls[e.index];o&&n.push({type:"content_block_delta",index:o._bi1,delta:{type:"input_json_delta",partial_json:e.function.arguments}})}}}return o.finish_reason&&(An(t,n),t.contentBlockOpen&&=(n.push({type:"content_block_stop",index:t.contentBlockIndex}),!1),n.push({type:"message_delta",delta:{stop_reason:tn(o.finish_reason),stop_sequence:null},usage:{input_tokens:e.usage?.prompt_tokens??0,output_tokens:e.usage?.completion_tokens??0}},{type:"message_stop"})),n}async function Sn(e,t,n){let o={query:t,max_results:n?.maxResults??5};n?.includeDomains?.length&&(o.include_domains=n.includeDomains),n?.excludeDomains?.length&&(o.exclude_domains=n.excludeDomains),Z.debug("Tavily search request:",JSON.stringify(o));let r=await fetch("https://api.tavily.com/search",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(o)});if(!r.ok){let e=await r.text();throw Z.error(`Tavily API error (${r.status}):`,e),Error(`Tavily API error: ${r.status} ${e}`)}let a=await r.json();return Z.debug(`Tavily returned ${a.results.length} results`),a.results}function On(){let e="";for(let t=0;t<24;t++)e+="abcdefghijklmnopqrstuvwxyz0123456789"[Math.floor(36*Math.random())];return e}async function In(e,t){let n=function(e){let t,n=e.messages[0];if(!n||"user"!==n.role)return null;if("string"==typeof n.content)t=n.content;else{if(!Array.isArray(n.content))return null;{let e=n.content.find(e=>"text"===e.type);if(!e)return null;t=e.text}}return t.startsWith("Perform a web search for the query: ")?t.slice(36).trim():t.trim()||null}(t);if(!n)return e.json({type:"error",error:{type:"invalid_request_error",message:"Could not extract search query from web search request"}},400);let o=function(e){let t=e.tools?.[0];return{maxResults:"number"==typeof t?.max_uses?t.max_uses:5,allowedDomains:t?.allowed_domains,blockedDomains:t?.blocked_domains}}(t);Z.info(`Web search intercepted: "${n}"`);let r=[];try{let e=v.tavilyApiKey;if(!e)throw Error("No Tavily API key configured");r=(await Sn(e,n,{maxResults:Math.min(o.maxResults,10),includeDomains:o.allowedDomains,excludeDomains:o.blockedDomains})).map(e=>({type:"web_search_result",url:e.url,title:e.title,encrypted_content:Buffer.from(e.content).toString("base64"),page_age:null}))}catch(e){Z.error("Tavily search failed:",e)}let a={query:n,results:r,model:t.model};if(!t.stream)return e.json(function(e){let{query:t,results:n,model:o}=e,r=`srvtoolu_${On()}`,a=[{type:"text",text:"I'll search for that."},{type:"server_tool_use",id:r,name:"web_search",input:{query:t}},{type:"web_search_tool_result",tool_use_id:r,content:n}];return{id:`msg_tavily_${On()}`,type:"message",role:"assistant",model:o,content:a,stop_reason:"end_turn",stop_sequence:null,usage:{input_tokens:0,output_tokens:0}}}(a));let i=function(e){let{query:t,results:n,model:o}=e,r=`srvtoolu_${On()}`,a=`msg_tavily_${On()}`,i=[];return i.push({type:"message_start",message:{id:a,type:"message",role:"assistant",content:[],model:o,stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0}}},{type:"content_block_start",index:0,content_block:{type:"text",text:""}},{type:"content_block_delta",index:0,delta:{type:"text_delta",text:"I'll search for that."}},{type:"content_block_stop",index:0},{type:"content_block_start",index:1,content_block:{type:"server_tool_use",id:r,name:"web_search",input:{}}},{type:"content_block_delta",index:1,delta:{type:"input_json_delta",partial_json:JSON.stringify({query:t})}},{type:"content_block_stop",index:1},{type:"content_block_start",index:2,content_block:{type:"web_search_tool_result",tool_use_id:r,content:n}},{type:"content_block_stop",index:2},{type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{output_tokens:0}},{type:"message_stop"}),i}(a);return fe(e,async e=>{for(let t of i)await e.writeSSE({event:t.type,data:JSON.stringify(t)})})}function qn(e){return function(e){let t=e.thinking;if(null==t)return null;if("object"!=typeof t)return"thinking must be an object";let n=t,o=new Set(["enabled","adaptive","disabled"]);return"string"==typeof n.type&&o.has(n.type)?"enabled"!==n.type||void 0===n.budget_tokens||"number"==typeof n.budget_tokens&&Number.isFinite(n.budget_tokens)&&!(n.budget_tokens<=0)&&Number.isInteger(n.budget_tokens)?null:"thinking.budget_tokens must be a positive integer":"thinking.type must be one of: enabled, adaptive, disabled"}(e)??function(e){let t=e.output_config;if(null==t)return null;if("object"!=typeof t)return"output_config must be an object";let n=t;if(void 0===n.effort)return null;let o=new Set(["low","medium","high","max"]);return"string"==typeof n.effort&&o.has(n.effort)?null:"output_config.effort must be one of: low, medium, high, max"}(e)}async function Nn(e){let t=await e.req.json();if(function(e){let t=e.tools;if(!t||1!==t.length)return!1;let n=t[0].type;return"string"==typeof n&&n.startsWith("web_search")}(t))return In(e,t);await qt(v),Z.debug("Anthropic request payload:",JSON.stringify(t));let n=e.req.header("anthropic-beta")??"",o=/context-1m/i.test(n);if(o&&/claude/i.test(t.model)){let e=t.model;t={...t,model:`${t.model}-1m`},Z.debug(`Detected context-1m beta flag, rewriting model: '${e}' → '${t.model}'`)}let r=function(e,t){return i.startActiveSpan("copilot_api.model.validate",n=>{try{n.setAttribute("copilot_api.model.requested",t.model);let o=It(t.model);n.setAttribute("copilot_api.model.normalized",o);let r=qn(t);return r?{ok:!1,response:e.json({type:"error",error:{type:"invalid_request_error",message:r}},400)}:o===t.model?(n.setAttribute("copilot_api.model.was_mapped",!1),{ok:!0,payload:t}):(n.setAttribute("copilot_api.model.was_mapped",!0),C.add(1,{from_model:t.model,to_model:o}),Z.debug(`Model mapping: '${t.model}' → '${o}'`),{ok:!0,payload:{...t,model:o}})}catch(t){if(t instanceof Ot)return{ok:!1,response:e.json({type:"error",error:{type:"invalid_request_error",message:t.message}},t.statusCode)};throw t}finally{n.end()}})}(e,t);if(!r.ok)return r.response;if(t=r.payload,function(e){return/gpt-[\d.]+-codex/i.test(e)}(t.model))return async function(e,t){let n=performance.now(),o=i.startActiveSpan("copilot_api.translate.anthropic_to_responses",e=>{try{return e.setAttribute("copilot_api.translation.direction","anthropic_to_responses"),e.setAttribute("copilot_api.translation.message_count",t.messages.length),fn(t)}finally{e.end()}});c.record(performance.now()-n,{direction:"anthropic_to_responses"}),Z.debug("Translated Responses API payload:",JSON.stringify(o)),e.set("requestData",{model:o.model,streaming:!!o.stream}),v.manualApprove&&await bt();let r=performance.now(),a=await en(o),s=performance.now()-r;if(S.record(s,{model:o.model,target:"responses",streaming:String(!!o.stream)}),Pn(a)){let t=e.get("requestData")||{};if(a.usage&&(t.tokenUsage=vt({prompt_tokens:a.usage.input_tokens,completion_tokens:a.usage.output_tokens,total_tokens:a.usage.total_tokens})),t._d1=s,e.set("requestData",t),Z.debug("Non-streaming Responses API response:",JSON.stringify(a).slice(-400)),"failed"===a.status)return e.json({type:"error",error:{type:a.error?.code??"api_error",message:a.error?.message??"Response generation failed"}},500);let n=performance.now(),o=i.startActiveSpan("copilot_api.translate.responses_to_anthropic",e=>{try{return e.setAttribute("copilot_api.translation.direction","responses_to_anthropic"),function(e){let t=[],n=!1;for(let o of e.output)if("message"===o.type)for(let e of o.content)e.text&&t.push({type:"text",text:e.text});else if("function_call"===o.type){n=!0;let e={};try{e=JSON.parse(o.arguments)}catch{}t.push({type:"tool_use",id:o.call_id,name:o.name,input:e})}return{id:e.id,type:"message",role:"assistant",model:e.model,content:t,stop_reason:bn(e.status,n),stop_sequence:null,usage:{input_tokens:e.usage?.input_tokens??0,output_tokens:e.usage?.output_tokens??0}}}(a)}finally{e.end()}});return c.record(performance.now()-n,{direction:"responses_to_anthropic"}),e.json(o)}Z.debug("Streaming Responses API response");let{events:l,span:u,release:p}=a;return fe(e,async t=>{let n=null,r=0,a=performance.now(),i={messageStartSent:!1,contentBlockIndex:0,contentBlockOpen:!1,responseId:"",model:"",toolCalls:{}};try{try{for await(let a of l){if(Z.debug("Responses API raw stream event:",JSON.stringify(a)),!a.data)continue;let l;try{l=JSON.parse(a.data)}catch{continue}r++,g.add(1,{model:o.model,endpoint_type:"responses"});let u=jn(l);if(u){n=u;let t=e.get("requestData")||{};t.tokenUsage=vt(n),t._d1=s,e.set("requestData",t)}let c=vn(l,i);for(let e of c)Z.debug("Translated Anthropic event:",JSON.stringify(e)),await t.writeSSE({event:e.type,data:JSON.stringify(e)})}let u=performance.now()-a,c={model:o.model,endpoint_type:"responses"};V.record(u,c),Y.record(r,c)}finally{n&&Tt(u,n),u.end(),p()}let c=e.get("requestId");c&&F.executeCompletion(c)}finally{e.get("resolveStreamComplete")?.()}})}(e,t);let a=performance.now(),s=i.startActiveSpan("copilot_api.translate.anthropic_to_openai",e=>{try{e.setAttribute("copilot_api.translation.direction","anthropic_to_openai"),e.setAttribute("copilot_api.translation.message_count",t.messages.length),e.setAttribute("copilot_api.translation.has_tools",!!t.tools?.length),e.setAttribute("copilot_api.translation.has_system",!!t.system),e.setAttribute("copilot_api.translation.context_1m_beta",o);let n=t.messages.some(e=>Array.isArray(e.content)&&e.content.some(e=>"image"===e.type)),r=t.messages.some(e=>Array.isArray(e.content)&&e.content.some(e=>"thinking"===e.type));return e.setAttribute("copilot_api.translation.has_images",n),e.setAttribute("copilot_api.translation.has_thinking",r),nn(t,v.models)}finally{e.end()}});c.record(performance.now()-a,{direction:"anthropic_to_openai"}),s=Rt(s,v.models,{enabled:v.thinkingEnabled}),Z.debug("Translated OpenAI request payload:",JSON.stringify(s)),e.set("requestData",{model:s.model,streaming:!!s.stream});let l=performance.now();v.manualApprove&&await bt();let u=await Jt(s),p=performance.now()-l;if(S.record(p,{model:s.model,target:"chat_completions",streaming:String(!!s.stream)}),En(u)){let t=e.get("requestData")||{};u.usage&&(t.tokenUsage=vt(u.usage)),t._d1=p,e.set("requestData",t),Z.debug("Non-streaming response:",JSON.stringify(u).slice(-400));let n=performance.now(),o=i.startActiveSpan("copilot_api.translate.openai_to_anthropic",e=>{try{e.setAttribute("copilot_api.translation.direction","openai_to_anthropic");let t=function(e){let t=[],n=[],o=[],r=null;r=e.choices[0]?.finish_reason??r;for(let a of e.choices){let e=dn(a.message),i=pn(a.message.content),s=mn(a.message.tool_calls);t.push(...e),n.push(...i),o.push(...s),("tool_calls"===a.finish_reason||"stop"===r)&&(r=a.finish_reason)}return{id:e.id,type:"message",role:"assistant",model:e.model,content:[...t,...n,...o],stop_reason:tn(r),stop_sequence:null,usage:{input_tokens:e.usage?.prompt_tokens??0,output_tokens:e.usage?.completion_tokens??0}}}(u);return e.setAttribute("copilot_api.translation.content_blocks",t.content.length),e.setAttribute("copilot_api.translation.stop_reason",t.stop_reason??"null"),e.setAttribute("copilot_api.translation.has_tool_use",t.content.some(e=>"tool_use"===e.type)),t}finally{e.end()}});return c.record(performance.now()-n,{direction:"openai_to_anthropic"}),Z.debug("Translated Anthropic response:",JSON.stringify(o)),e.json(o)}Z.debug("Streaming response from service");let{events:d,span:m,release:f}=u;return fe(e,async t=>{let n=null,o=0,r=performance.now(),a={messageStartSent:!1,contentBlockIndex:0,contentBlockOpen:!1,thinkingBlockOpen:!1,toolCalls:{}};try{try{for await(let r of d){if(Z.debug("Raw stream event:",JSON.stringify(r)),"[DONE]"===r.data)break;if(!r.data)continue;let i=JSON.parse(r.data);if(o++,g.add(1,{model:s.model,endpoint_type:"anthropic"}),i.usage){n=i.usage;let t=e.get("requestData")||{};t.tokenUsage=vt(n),t._d1=p,e.set("requestData",t)}let l=$n(i,a);for(let e of l)Z.debug("Translated Anthropic event:",JSON.stringify(e)),await t.writeSSE({event:e.type,data:JSON.stringify(e)})}let i=performance.now()-r,l={model:s.model,endpoint_type:"anthropic"};if(V.record(i,l),Y.record(o,l),n){let t=e.get("requestData")||{};t.tokenUsage||(t.tokenUsage=vt(n),t._d1=p,e.set("requestData",t))}}finally{n&&Tt(m,n),m.end(),f()}let i=e.get("requestId");i&&F.executeCompletion(i)}finally{e.get("resolveStreamComplete")?.()}})}function jn(e){if("response.completed"!==e.type&&"response.incomplete"!==e.type)return null;let t=e.response.usage;return t?{prompt_tokens:t.input_tokens,completion_tokens:t.output_tokens,total_tokens:t.total_tokens}:null}const En=e=>Object.hasOwn(e,"choices"),Pn=e=>Object.hasOwn(e,"output");const Cn=new de;Cn.post("/",async e=>{try{return await Nn(e)}catch(t){return await J(e,t)}});const Rn=new de;function Bn(e){return i.startActiveSpan("copilot_api.model.validate",t=>{try{t.setAttribute("copilot_api.model.requested",e.model);let n=It(e.model);return t.setAttribute("copilot_api.model.normalized",n),n===e.model?(t.setAttribute("copilot_api.model.was_mapped",!1),e):(t.setAttribute("copilot_api.model.was_mapped",!0),C.add(1,{from_model:e.model,to_model:n}),Z.debug(`Normalized model from '${e.model}' to '${n}'`),{...e,model:n})}finally{t.end()}})}function Dn(e,t){let n=function(e){try{let t=JSON.parse(e);if(("response.completed"===t.type||"response.incomplete"===t.type)&&t.response?.usage){let e=t.response.usage;return{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}}}catch{}return null}(e);if(!n)return;t.usageRef.value=n;let o=t.c.get("requestData")||{};o.tokenUsage=vt(n),o._d1=t._d1,t.c.set("requestData",o)}Rn.get("/",async e=>{try{v.models||await xe();let t=v.models?.data.map(e=>({id:e.id,object:"model",type:"model",created:0,created_at:new Date(0).toISOString(),owned_by:e.vendor,display_name:e.name,context_length:e.capabilities.limits?.max_context_window_tokens}));return e.json({object:"list",data:t,has_more:!1})}catch(t){return await J(e,t)}});const Mn=new Set(["message","function_call","function_call_output"]),Hn=new Set(["type","name","description","parameters"]);async function Fn(e){if(!e)return!1;let t=e.sources;if(t&&t.length>0)return!1;let n=e.query??e.queries?.[0];return!(!n||!v.tavilyApiKey)&&(Z.info(`Enriching web search: "${n}"`),e.sources=(await Sn(v.tavilyApiKey,n,{maxResults:5})).map(e=>({type:"url",url:e.url,title:e.title,content:e.content})),!0)}async function Jn(e){if(!v.tavilyApiKey)return e;try{let t=JSON.parse(e);if("response.output_item.done"===t.type){let e=t.item;if("web_search_call"===e?.type&&await Fn(e.action))return JSON.stringify(t)}}catch(e){Z.error("Failed to enrich web search:",e)}return e}function Un(e,t){if("function_call"!==e.type||!t.has(e.name))return!1;if(e.type="custom_tool_call","string"==typeof e.arguments){try{e.input=JSON.parse(e.arguments).input??e.arguments}catch{e.input=e.arguments}delete e.arguments}return!0}function Ln(e,t){if(0===t.size)return e;try{let n=JSON.parse(e),o=n.type;if("response.output_item.added"===o||"response.output_item.done"===o){let e=n.item;if(e&&Un(e,t))return JSON.stringify(n)}if("response.completed"===o){let o=n.response?.output;if(!o)return e;let r=!1;for(let e of o)Un(e,t)&&(r=!0);if(r)return JSON.stringify(n)}}catch{}return e}function Wn(e){return null!=e}function Kn(e){let t=e.input,n="string"==typeof t?t:t.map(e=>function(e){switch(e.type){case"custom_tool_call":{let t=e.input;return{type:"function_call",call_id:e.call_id,name:e.name,arguments:JSON.stringify("string"==typeof t?{input:t}:t)}}case"custom_tool_call_output":return{type:"function_call_output",call_id:e.call_id,output:e.output};default:return Mn.has(e.type)||"web_search_call"===e.type?e:null}}(e)).filter(e=>null!==e),o={model:e.model,input:n};for(let t of["instructions","max_output_tokens","temperature","top_p","stream","store","tool_choice","parallel_tool_calls","user"])Wn(e[t])&&(o[t]=e[t]);let r=new Set;if(Wn(e.tools)){let t=function(e){let t=new Set;return{tools:e.filter(e=>{let t=e.type;return"function"===t||"custom"===t||"web_search"===t||t.startsWith("web_search_")}).map(e=>{let n=e.type;if("web_search"===n||n.startsWith("web_search_"))return{type:e.type};let o={};for(let t of Object.keys(e))Hn.has(t)&&(o[t]=e[t]);return"custom"===o.type&&(o.type="function",t.add(o.name),o.parameters||={type:"object",properties:{input:{type:"string",description:"The freeform tool input content"}},required:["input"],additionalProperties:!1}),o}),customToolNames:t}}(e.tools);o.tools=t.tools,r=t.customToolNames}if(Wn(e.reasoning)&&"object"==typeof e.reasoning){let t=e.reasoning;o.reasoning={},Wn(t.effort)&&(o.reasoning.effort=t.effort),Wn(t.summary)&&(o.reasoning.summary=t.summary)}if(Wn(e.text)&&"object"==typeof e.text){let t=e.text;Wn(t.format)&&(o.text={format:t.format})}return{payload:o,customToolNames:r}}async function Gn(e,t,n){await async function(e){if(v.tavilyApiKey)try{for(let t of e.output){let e=t;"web_search_call"===e.type&&await Fn(e.action)}}catch(e){Z.error("Failed to enrich non-streaming web search:",e)}}(t);let o=e.get("requestData")||{};return t.usage&&(o.tokenUsage=vt({prompt_tokens:t.usage.input_tokens,completion_tokens:t.usage.output_tokens,total_tokens:t.usage.total_tokens})),o._d1=n,e.set("requestData",o),e.json(t)}const zn=e=>Object.hasOwn(e,"output"),Yn=new de;Yn.post("/",async e=>{try{return await async function(e){await qt(v);let{payload:t,customToolNames:n}=Kn(await e.req.json()),o=t;Z.debug(`Responses API [${Array.isArray(o.input)?o.input.length:1} items]`),n.size>0&&Z.debug(`Custom tools detected: ${[...n].join(", ")}`);try{o=Bn(o)}catch(t){if(t instanceof Ot)return e.json({error:{message:t.message,type:"invalid_request_error",code:"invalid_model"}},t.statusCode);throw t}e.set("requestData",{model:o.model,streaming:!!o.stream}),v.manualApprove&&await bt();let r=performance.now(),a=await en(o),i=performance.now()-r;if(S.record(i,{model:o.model,target:"responses",streaming:String(!!o.stream)}),zn(a))return Gn(e,a,i);let{events:s,span:l,release:u}=a;return fe(e,async t=>{let r={value:null},a=0,c=performance.now();try{try{for await(let l of s)if(l.data&&"[DONE]"!==l.data){a++;let s=Ln(l.data,n);s=await Jn(s);let u=s===l.data?l:{...l,data:s};g.add(1,{model:o.model,endpoint_type:"responses"}),Dn(s,{usageRef:r,c:e,_d1:i}),await t.writeSSE(u)}else await t.writeSSE(l);let l=performance.now()-c,u={model:o.model,endpoint_type:"responses"};V.record(l,u),Y.record(a,u)}finally{r.value&&Tt(l,r.value),l.end(),u()}let p=e.get("requestId");p&&F.executeCompletion(p)}finally{e.get("resolveStreamComplete")?.()}})}(e)}catch(t){return await J(e,t)}});const Vn=new de;Vn.get("/",e=>{try{return e.json({token:v._t1})}catch(t){return Z.error("Error fetching token:",t),e.json({error:"Failed to fetch token",token:null},500)}});const Qn=new de;Qn.get("/",async e=>{try{let t=await Ne();return e.json(t)}catch(t){return Z.error("Error fetching usage:",t),e.json({error:"Failed to fetch usage"},500)}});const Xn=new de;Xn.use(async(e,t)=>{let n=Date.now(),o=`req_${Date.now()}_${Math.random().toString(36).slice(2,11)}`,r=e.req.method,a=e.req.path,s=function(e){return e.includes("/messages")?"anthropic":e.includes("/chat/completions")?"openai":e.includes("/embeddings")?"embeddings":e.includes("/models")?"models":e.includes("/usage")?"usage":e.includes("/token")?"token":"unknown"}(a);e.set("requestId",o),e.header("x-request-id",o);let l=i.startSpan("copilot_api.handle_request",{kind:ne.SERVER,attributes:{"http.method":r,"http.url":e.req.url,"http.route":a,"copilot_api.request_id":o,"copilot_api.endpoint_type":s}});for(let[t,n]of Object.entries(e.req.header())){let e=t.toLowerCase();B.has(e)||l.setAttribute(`http.request.header.${e}`,n)}let u,c=new Promise(e=>{u=e});e.set("streamComplete",c),e.set("resolveStreamComplete",u);let p=ae.setSpan(re.active(),l);await re.with(p,async()=>{F.registerCompletion(o,e,n);try{await t()}catch(e){let t=e instanceof Error?e:Error(String(e));throw l.setStatus({code:oe.ERROR,message:t.message}),l.recordException(t),e}});let d=e.get("requestData"),m={c:e,start:n,method:r,path:a,endpointType:s,requestId:o,span:l};d?.streaming?c.then(()=>kt(m)).catch(e=>{Z.debug("Error finalizing streaming request:",e),l.end()}):kt(m)}),Xn.use(me()),Xn.get("/",e=>e.text("Server running")),Xn.route("/chat/completions",Gt),Xn.route("/models",Rn),Xn.route("/embeddings",zt),Xn.route("/usage",Qn),Xn.route("/token",Vn),Xn.route("/v1/chat/completions",Gt),Xn.route("/v1/models",Rn),Xn.route("/v1/embeddings",zt),Xn.route("/v1/messages",Cn),Xn.post("/v1/messages/count_tokens",async function(e){let t;try{t=await e.req.json()}catch{return e.json({error:{type:"invalid_request_error",message:"Invalid JSON"}},400)}if(!Array.isArray(t.messages))return e.json({error:{type:"invalid_request_error",message:"messages is required"}},400);let n=Vt.baseOverhead;if(t.system){let e="string"==typeof t.system?t.system:t.system.map(e=>e.text).join("\n\n");n+=Vt.perMessage+Qt(e)*Vt.contentMultiplier}for(let e of t.messages)n+=Vt.perMessage,n+=Qt(e.role),"string"==typeof e.content?n+=Qt(e.content)*Vt.contentMultiplier:Array.isArray(e.content)&&(n+=Xt(e.content));return t.tools&&t.tools.length>0&&(n+=function(e){let t=Vt.toolsExist;for(let[n,o]of e.entries())n>0&&(t+=Vt.perTool),t+=Qt(o.name),o.description&&(t+=Vt.perDesc+Qt(o.description)),t+=Zt(o.input_schema);return t}(t.tools)),e.json({input_tokens:Math.round(n)})}),Xn.route("/v1/responses",Yn),Xn.route("/responses",Yn);const Zn=[],eo=Date.now();let to="";function no(){let e=async(e,t=0)=>{let n=i.startSpan("copilot_api.server.shutdown",{kind:ne.INTERNAL,attributes:{"copilot_api.shutdown.reason":e,"copilot_api.shutdown.exit_code":t,"copilot_api.uptime_seconds":(Date.now()-eo)/1e3}});for(let t of Zn)try{await t()}catch(e){Z.error("Error during cleanup:",e)}n.end(),function(e,t){let n=ut("https://barazany.dev?utm_source=copilot-cli&utm_medium=terminal&utm_campaign=goodbye_banner","barazany.dev"),o=["",pt,` ${ot("COPILOT-API")} ${rt(`v${e}`)} ${rt("·")} ${yt(t)} uptime`,pt,""," Check in from time to time on",` ${at(n)}`,"",pt,""];console.log(o.join("\n"))}(to,Date.now()-eo),le.exit(t)};le.on("SIGINT",()=>e("SIGINT")),le.on("SIGTERM",()=>e("SIGTERM")),le.on("uncaughtException",t=>{Z.error("Uncaught exception:",t),e("uncaughtException",1)}),le.on("unhandledRejection",(t,n)=>{Z.error("Unhandled promise rejection at:",n,"reason:",t),e("unhandledRejection",1)})}function oo(e){let{options:t,version:o,runtime:r,_u1:a}=e,s=i.startSpan("copilot_api.server.startup",{kind:ne.INTERNAL,attributes:n({config:{version:o,port:t.port,accountType:t.accountType,verbose:t.verbose,manualApprove:t.manual,rateLimitSeconds:t.rateLimit??0,rateLimitWait:t.rateLimitWait,timeoutMs:t.timeout,showToken:t.showToken,_cc1:t._cc1,model:t.model,smallModel:t.smallModel,_t3:t._t2?"cli_arg":"stored",enableThinking:t.enableThinking},runtime:r,identity:{_u1:a.login,machineId:v.machineId,sessionId:v.sessionId,_v1:v._v1},connectivity:v.connectivity,modelsCount:v.models?.data.length??0,startupDurationMs:Date.now()-eo},"copilot_api")});if(v.models?.data)for(let e of v.models.data)s.addEvent("copilot_api.model.available",n(e,"model"));s.end(),z(j.INFO,"INFO","Server started",n({version:o,port:t.port,accountType:t.accountType,_u1:a.login,modelsCount:v.models?.data.length??0,startupDurationMs:Date.now()-eo},"copilot_api"))}async function ro(e){no(),Zn.push(()=>{Z.debug("Cleaning up connectivity monitor"),H.stop()},()=>{Z.debug("Cleaning up token management"),Z.debug("Cleaning up token management"),ve&&=void clearInterval(ve),Te&&=void H.off("online",Te),Ae&&=void H.off("offline",Ae)},async()=>{Z.debug("Cleaning up HTTP/2 client"),await async function(){Z.debug("Stopping HTTP/2 pool"),ze&&=void clearInterval(ze),Ye&&=void clearInterval(Ye);let e=[...We.keys(),...Ke];We.clear(),Ke.clear(),Le.clear(),Ge=void 0;for(let t of e)Ve(t)&&(G.add(1,{event:"closed"}),await t.close())}()},async()=>{Z.debug("Shutting down OTel SDK"),await p()}),e.verbose&&(Z.level=5,Z.info("Verbose logging enabled")),e._cc1&&e.codex&&(Z.error("Cannot use --claude-code and --codex together"),le.exit(1)),v.accountType=e.accountType,v.manualApprove=e.manual,v.rateLimitSeconds=e.rateLimit,v.rateLimitWait=e.rateLimitWait,v.showToken=e.showToken,v.timeoutMs=e.timeout,v.thinkingEnabled=e.enableThinking,e.tavilyApiKey&&(v.tavilyApiKey=e.tavilyApiKey),function(){let e="1"===le.env.COPILOT_H2_KEEPALIVE||"true"===le.env.COPILOT_H2_KEEPALIVE,t=e||"1"===le.env.COPILOT_H2||"true"===le.env.COPILOT_H2;v.connectivity.h2=t,v.connectivity.h2Keepalive=e}(),await T(),v._v1="1.116.0-insider";let t=await Ee();to=t;let n,o=Pe();r(t,eo),e._t2?(v._t2=e._t2,Z.info("Using provided Auth token")):await Oe();try{let{_s4:e}=await import("./get-user-O49GxhnU.mjs");n=await e(),await I(v,n.login)}catch(e){Z.error("Failed to get user info for machine ID generation:",e),Z.error("Cannot proceed without user information"),le.exit(1)}await Se(),await xe();let a=function(e=18e5){let t=setInterval(()=>{xe().then(()=>Z.debug("Model list refreshed successfully")).catch(e=>{Z.warn("Failed to refresh model list; keeping existing cache:",e)})},e);return t.unref(),()=>{clearInterval(t)}}();Zn.push(()=>{Z.debug("Cleaning up model refresh interval"),a()}),v.connectivity.h2&&(Ze(Xe(O(v))),v.connectivity.h2Keepalive&&tt()),oo({options:e,version:t,runtime:o,_u1:n});let i=`http://localhost:${e.port}`,s=await async function(e,t){if(!e._cc1)return!1;let n,o;if(ce(v.models,"Models should be loaded by now"),e.model&&e.smallModel){let t=v.models.data.map(e=>e.id);t.includes(e.model)||(Z.error(`Invalid model: ${e.model}`),Z.info(`Available services: \n${t.join("\n")}`),le.exit(1)),t.includes(e.smallModel)||(Z.error(`Invalid small model: ${e.smallModel}`),Z.info(`Available services: \n${t.join("\n")}`),le.exit(1)),n=e.model,o=e.smallModel}else e.model||e.smallModel?(Z.error("Both --model and --small-model must be specified for model selection"),le.exit(1)):(n=await Z.prompt("Select a model to use",{type:"select",options:v.models.data.map(e=>e.id)}),o=await Z.prompt("Select a small model to use",{type:"select",options:v.models.data.map(e=>e.id)}));let r=nt({ANTHROPIC_BASE_URL:t,ANTHROPIC_AUTH_TOKEN:"dummy",ANTHROPIC_MODEL:n,ANTHROPIC_SMALL_FAST_MODEL:o},"claude");try{return se.writeSync(r),!0}catch{return Z.warn("Failed to copy to clipboard. Here is the command:"),Z.log(r),!1}}(e,i)||await async function(e,t){if(!e.codex)return!1;let n;if(ce(v.models,"Models should be loaded by now"),e.model){let t=v.models.data.map(e=>e.id);t.includes(e.model)||(Z.error(`Invalid model: ${e.model}`),Z.info(`Available services: \n${t.join("\n")}`),le.exit(1)),n=e.model}else n=await Z.prompt("Select a model to use with Codex CLI",{type:"select",options:v.models.data.map(e=>e.id)});let o=nt({OPENAI_BASE_URL:t,OPENAI_API_KEY:"dummy"},`codex --model ${n}`);try{return se.writeSync(o),!0}catch{return Z.warn("Failed to copy to clipboard. Here is the Codex CLI command:"),Z.log(o),!1}}(e,i);(function(e){let t=[..._t(e),...gt(e),...ht(e),"",...ft(mt),""];console.log(t.join("\n"))})({version:t,runtime:o,user:n.login,accountType:e.accountType,port:e.port,timeout:e.timeout,webSearch:!!e.tavilyApiKey,model:e.model,smallModel:e.smallModel,clipboardCopied:s}),ue({fetch:Xn.fetch,port:e.port,bun:{idleTimeout:255}})}const ao=Q({meta:{name:"start",description:"Start the API server"},args:{port:{alias:"p",type:"string",default:"4141",description:"Port to listen on"},verbose:{alias:"v",type:"boolean",default:!1,description:"Enable verbose logging"},"account-type":{alias:"a",type:"string",default:"individual",description:"Account type (individual, business, enterprise)"},manual:{type:"boolean",default:!1,description:"Enable manual request approval"},"rate-limit":{alias:"r",type:"string",description:"Rate limit in seconds between requests"},wait:{alias:"w",type:"boolean",default:!1,description:"Wait instead of error when rate limit is hit. Has no effect if rate limit is not set"},"github-token":{alias:"g",type:"string",description:"Provide auth token directly (must be generated using the `auth` subcommand)"},"claude-code":{alias:"c",type:"boolean",default:!1,description:"Generate a command to launch with API config"},codex:{type:"boolean",default:!1,description:"Generate a command to launch Codex CLI with Copilot API config"},model:{alias:"m",type:"string",description:"Model to use (with --claude-code or --codex)"},"small-model":{alias:"s",type:"string",description:"Small/fast model to use (requires --claude-code)"},"show-token":{type:"boolean",default:!1,description:"Show tokens on fetch and refresh"},timeout:{alias:"t",type:"string",description:"API timeout in milliseconds (default: 600000)"},"tavily-api-key":{type:"string",description:"Tavily API key for web search support (or set TAVILY_API_KEY env var)"},"enable-thinking":{type:"boolean",default:!1,description:"Enable forwarding of thinking_budget / Anthropic thinking config to upstream. Off by default to avoid accidentally triggering reasoning behaviour."}},run({args:e}){let t=e["rate-limit"],n=void 0===t?void 0:Number.parseInt(t,10),o=e.timeout,r=void 0===o?u:Number.parseInt(o,10);return ro({port:Number.parseInt(e.port,10),verbose:e.verbose,accountType:e["account-type"],manual:e.manual,rateLimit:n,rateLimitWait:e.wait,_t2:e["github-token"],_cc1:e["claude-code"],codex:e.codex,model:e.model,smallModel:e["small-model"],showToken:e["show-token"],timeout:r,tavilyApiKey:e["tavily-api-key"]||le.env.TAVILY_API_KEY,enableThinking:e["enable-thinking"]})}});await async function(){try{if("1"===process.env.COPILOT_API_SKIP_UPDATE_CHECK)return;let[e,t]=await Promise.all([Ee(),Je()]);if("unknown"===e||!t||!function(e,t){let n=e.split(".").map(Number),o=t.split(".").map(Number),r=Math.max(n.length,o.length);for(let e=0;e<r;e++){let t=n[e]??0,r=o[e]??0;if(t<r)return!0;if(t>r)return!1}return!1}(e,t))return;!function(e,t){let n=`npx ${De}@latest`,o=Fe("│"),r=` ${He("Update available!")} ${(e=>`${e}`)(e)} → ${(e=>`${e}`)(t)}`,a=` Run ${He(n)} to update`,i=" ".repeat(54),s=Fe(`╭${"─".repeat(54)}╮`),l=Fe(`╰${"─".repeat(54)}╯`),u=["",s,`${o}${i}${o}`,`${o}${Ue(r,54)}${o}`,`${o}${i}${o}`,`${o}${Ue(a,54)}${o}`,`${o}${i}${o}`,l,""];console.log(u.join("\n"))}(e,t)}catch{}}(),await X(Q({meta:{name:"copilot-api",description:"API proxy server for tool integration."},subCommands:{auth:qe,start:ao,"check-usage":je,debug:Be}}));export{};
2
+ import{A as e,B as t,C as n,D as o,E as r,F as a,G as i,H as s,I as l,J as u,K as c,L as p,M as d,N as m,O as f,P as _,R as g,S as h,T as y,U as k,V as b,W as w,X as x,Y as v,Z as A,_ as T,a as $,b as S,c as O,d as I,f as q,g as N,h as j,i as P,j as E,k as C,l as R,m as B,n as D,o as M,p as F,q as H,r as J,s as U,t as L,u as W,v as z,w as K,x as G,y as Y,z as V}from"./get-user-CEqqH34r.mjs";import{defineCommand as Q,runMain as X}from"citty";import Z from"consola";import ee from"node:fs/promises";import te from"node:os";import{SpanKind as ne,SpanStatusCode as oe,context as re,trace as ae}from"@opentelemetry/api";import ie from"clipboardy";import{execFile as se,execSync as le}from"node:child_process";import ue from"node:process";import{Client as ce}from"undici";import{serve as pe}from"srvx";import de from"tiny-invariant";import{Hono as me}from"hono";import{cors as fe}from"hono/cors";import{streamSSE as _e}from"hono/streaming";import{events as ge}from"fetch-event-stream";import{Tokenizer as he,models as ye}from"ai-tokenizer";import*as ke from"ai-tokenizer/encoding/claude";const be=async()=>{let e=`${P}/copilot_internal/v2/token`,t=v.timeoutMs,n=new AbortController,o=setTimeout(()=>n.abort(),t);try{let{response:t,span:o}=await k({spanName:"github GET /copilot_internal/v2/token",url:e,target:"copilot_token",headers:W(v),signal:n.signal});if(!t.ok)throw o.end(),new D("Failed to get Service token",t);let r=await t.json();return o.end(),r}finally{clearTimeout(o)}};const we=e=>new Promise(t=>{setTimeout(t,e)}),xe=e=>null==e;async function ve(){v.models=await(async()=>{let e=`${O(v)}/models`,t=R(v),n=v.timeoutMs,o=new AbortController,r=setTimeout(()=>o.abort(),n);try{let{response:n,span:r}=await k({spanName:"copilot GET /models",url:e,target:"models",headers:t,signal:o.signal});if(!n.ok)throw r.end(),new D("Failed to get models",n);let a=await n.json();return r.setAttribute("copilot_api.models_count",a.data.length),r.end(),a}finally{clearTimeout(r)}})()}var Ae=class extends Error{constructor(e){super(e),this.name="AuthorizationError"}};let Te,$e,Se,Oe=!1;const Ie=async()=>{let{token:e,refresh_in:n}=await be();v._t1=e,Z.debug("Service token fetched successfully!"),v.showToken&&Z.info("Service token:",e);let o=async(e=5,n=1e3,o="scheduled")=>{if(!Oe)return Oe=!0,i.startActiveSpan("copilot_api.token.refresh",async r=>{let a=performance.now();r.setAttribute("copilot_api.token_refresh.reason",o),r.setAttribute("copilot_api.token_refresh.max_retries",e);try{for(let i=1;i<=e;i++)try{r.setAttribute("copilot_api.token_refresh.attempt",i),Z.debug(`Refreshing Service token (${o}, attempt ${i}/${e})`);let{token:n}=await be();return v._t1=n,Z.debug("Service token refreshed successfully"),v.showToken&&Z.info("Refreshed Service token:",n),r.setAttribute("copilot_api.token_refresh.success",!0),t.add(1,{reason:o,success:"true"}),b.record(performance.now()-a,{reason:o}),void G(j.INFO,"INFO",`Token refreshed successfully (${o})`,{"copilot_api.token_refresh.reason":o,"copilot_api.token_refresh.attempt":i})}catch(l){let u=i===e;if(Z.error(`Failed to refresh Service token (attempt ${i}/${e}):`,l),t.add(1,{reason:o,success:"false"}),u){Z.error("All refresh attempts failed. May be unavailable until next scheduled refresh.");let t=l instanceof Error?l:Error(String(l));return r.setAttribute("copilot_api.token_refresh.success",!1),r.setStatus({code:oe.ERROR,message:t.message}),r.recordException(t),s.add(1,{reason:o}),b.record(performance.now()-a,{reason:o}),void G(j.ERROR,"ERROR",`Token refresh failed after ${e} attempts (${o})`,{"copilot_api.token_refresh.reason":o,"copilot_api.token_refresh.attempts":e})}let c=n*2**(i-1);Z.debug(`Retrying token refresh in ${c}ms...`),await new Promise(e=>setTimeout(e,c))}}finally{r.end(),Oe=!1}});Z.debug("Refresh already in progress, skipping")};Te=setInterval(()=>{o(5,1e3,"scheduled").catch(e=>{Z.error("Unexpected error in scheduled token refresh:",e)})},1e3*(n-60)),F.start(),$e=()=>{Z.debug("Network connectivity restored, attempting immediate token refresh"),o(3,500,"network-restored").catch(e=>{Z.error("Unexpected error in network-restored token refresh:",e)})},Se=()=>{Z.debug("Network connectivity lost")},F.on("online",$e),F.on("offline",Se)};async function qe(e){try{let t=await ee.readFile(x.GITHUB_TOKEN_PATH,"utf8");if(t&&!e?.force)return v._t2=t,v.showToken&&Z.info("Auth token:",t),void await Ne();Z.info("Authentication required");let n=await async function(){let e=new AbortController,t=v.timeoutMs,n=setTimeout(()=>{e.abort()},t);try{let t=await fetch(`${M}/login/device/code`,{method:"POST",headers:q(),body:JSON.stringify({client_id:U,scope:$}),signal:e.signal});if(!t.ok)throw new D("Failed to get device code",t);return await t.json()}finally{clearTimeout(n)}}();Z.debug("Device code response:",n),Z.info(`Please enter the code "${n.user_code}" at ${n.verification_uri}`);try{ie.writeSync(n.user_code),Z.success("Code copied to clipboard — opening browser...")}catch{Z.info("Open the link above and enter the code to authenticate")}!function(e){try{let t,n=ue.platform;t="darwin"===n?"open":"win32"===n?"start":"xdg-open",se(t,[e],()=>{})}catch{}}(n.verification_uri);let o=await async function(e){let t=1e3*(e.interval+1);Z.debug(`Polling access token with interval of ${t}ms`),Z.start("Waiting for authorization...");let n=Date.now(),o=1e3*e.expires_in;for(;;){if(Date.now()-n>=o)throw new Ae("Authorization timed out — the code expired. Please run the command again.");let r=new AbortController,a=v.timeoutMs,i=setTimeout(()=>{r.abort()},a);try{let n=await fetch(`${M}/login/oauth/access_token`,{method:"POST",headers:q(),body:JSON.stringify({client_id:U,device_code:e.device_code,grant_type:"urn:ietf:params:oauth:grant-type:device_code"}),signal:r.signal});if(!n.ok){await we(t),Z.error("Failed to poll access token:",await n.text());continue}let o=await n.json();if(Z.debug("Polling access token response:",o),o.error){if("expired_token"===o.error)throw new Ae("Authorization timed out — the code expired. Please run the command again.");if("access_denied"===o.error)throw new Ae("Authorization was denied. Please run the command again and approve the request.");"authorization_pending"!==o.error&&"slow_down"!==o.error&&Z.warn(`Unexpected auth error: ${o.error}`),await we(t);continue}if(o.access_token)return o.access_token;await we(t)}catch(e){if(e instanceof Ae)throw e;if(e instanceof Error&&"AbortError"===e.name){Z.error("Access token polling timed out"),await we(t);continue}throw e}finally{clearTimeout(i)}}}(n);await(e=>ee.writeFile(x.GITHUB_TOKEN_PATH,e))(o),v._t2=o,v.showToken&&Z.info("Auth token:",o),await Ne()}catch(e){throw e instanceof D?(Z.error("Failed to get Auth token:",await e.response.json()),e):(Z.error("Failed to get Auth token:",e),e)}}async function Ne(){let e=await L();Z.success(`Authenticated as ${e.login}`)}async function je(e){e.verbose&&(Z.level=5,Z.info("Verbose logging enabled")),v.showToken=e.showToken,await A(),await qe({force:!0}),console.log([""," Get started:",""," $ npx copilot-api-node20@latest start --claude-code"," $ npx copilot-api-node20@latest start --codex",""," Full docs: https://www.npmjs.com/package/copilot-api-node20",""].join("\n")),process.exit(0)}const Pe=Q({meta:{name:"auth",description:"Run authentication flow"},args:{verbose:{alias:"v",type:"boolean",default:!1,description:"Enable verbose logging"},"show-token":{type:"boolean",default:!1,description:"Show token on auth"}},run:({args:e})=>je({verbose:e.verbose,showToken:e["show-token"]})}),Ee=async()=>{let e=`${P}/copilot_internal/user`,t=v.timeoutMs,n=new AbortController,o=setTimeout(()=>n.abort(),t);try{let{response:t,span:o}=await k({spanName:"github GET /copilot_internal/user",url:e,target:"usage",headers:W(v),signal:n.signal});if(!t.ok)throw o.end(),new D("Failed to get usage",t);let r=await t.json();return o.end(),r}finally{clearTimeout(o)}},Ce=Q({meta:{name:"check-usage",description:"Show current usage/quota information"},async run(){await A(),await qe();try{let e=await Ee(),t=e.quota_snapshots.premium_interactions,n=t.entitlement,o=n-t.remaining,r=n>0?o/n*100:0,a=t.percent_remaining;function i(e,t){if(!t)return`${e}: N/A`;let n=t.entitlement,o=n-t.remaining,r=n>0?o/n*100:0,a=t.percent_remaining;return`${e}: ${o}/${n} used (${r.toFixed(1)}% used, ${a.toFixed(1)}% remaining)`}let s=`Premium: ${o}/${n} used (${r.toFixed(1)}% used, ${a.toFixed(1)}% remaining)`,l=i("Chat",e.quota_snapshots.chat),u=i("Completions",e.quota_snapshots.completions);Z.box(`Usage (plan: ${e.copilot_plan})\nQuota resets: ${e.quota_reset_date}\n\nQuotas:\n ${s}\n ${l}\n ${u}`)}catch(c){Z.error("Failed to fetch usage:",c),process.exit(1)}}});async function Re(){try{let e=new URL("../package.json",import.meta.url).pathname,t=await ee.readFile(e);return JSON.parse(t.toString()).version}catch{return"unknown"}}function Be(){let e=typeof Bun<"u";return{name:e?"bun":"node",version:e?Bun.version:process.version.slice(1),platform:te.platform(),arch:te.arch()}}async function De(){try{return!!(await ee.stat(x.GITHUB_TOKEN_PATH)).isFile()&&(await ee.readFile(x.GITHUB_TOKEN_PATH,"utf8")).trim().length>0}catch{return!1}}async function Me(e){let t=await async function(){let[e,t]=await Promise.all([Re(),De()]);return{version:e,runtime:Be(),paths:{APP_DIR:x.APP_DIR,GITHUB_TOKEN_PATH:x.GITHUB_TOKEN_PATH},tokenExists:t}}();e.json?function(e){console.log(JSON.stringify(e,null,2))}(t):function(e){Z.info(`service debug\n\nVersion: ${e.version}\nRuntime: ${e.runtime.name} ${e.runtime.version} (${e.runtime.platform} ${e.runtime.arch})\n\nPaths:\n- APP_DIR: ${e.paths.APP_DIR}\n- GITHUB_TOKEN_PATH: ${e.paths.GITHUB_TOKEN_PATH}\n\nToken exists: ${e.tokenExists?"Yes":"No"}`)}(t)}const Fe=Q({meta:{name:"debug",description:"Print debug information about the application"},args:{json:{type:"boolean",default:!1,description:"Output debug information as JSON"}},run:({args:e})=>Me({json:e.json})}),He="copilot-api-node20",Je=`https://registry.npmjs.org/${He}/latest`,Ue=e=>`${e}`,Le=e=>`${e}`;async function We(){let e=new AbortController,t=setTimeout(()=>e.abort(),5e3);try{let t=await fetch(Je,{signal:e.signal});return t.ok?(await t.json()).version??null:null}catch{return null}finally{clearTimeout(t)}}function ze(e,t){let n=Math.max(0,t-function(e){return e.replaceAll(/\x1b\[[0-9;]*m/g,"").length}(e));return e+" ".repeat(n)}const Ke=new Map,Ge=new Map,Ye=new Set;let Ve,Qe,Xe;function Ze(e){return!e.closed&&!e.destroyed}function et(e){Z.debug(`Creating HTTP/2 client for ${e}`);let t=new ce(e,{allowH2:!0,pipelining:0,keepAliveTimeout:6e4,initialWindowSize:4194304,connectionWindowSize:16777216});return Ke.set(t,Date.now()),K.add(1,{event:"created",origin:e}),G(j.INFO,"INFO",`HTTP/2 client created for ${e}`,{"copilot_api.h2.event":"created","copilot_api.h2.origin":e}),t.on("connect",()=>{Z.debug(`HTTP/2 client connected to ${e}`)}),t.on("error",t=>{Z.debug(`HTTP/2 client error for ${e}:`,t.message)}),t.on("disconnect",(n,o,r)=>{Z.debug(`HTTP/2 client disconnected from ${e}:`,r.message),K.add(1,{event:"goaway",origin:e}),G(j.WARN,"WARN",`HTTP/2 client disconnected from ${e}: ${r.message}`,{"copilot_api.h2.event":"goaway","copilot_api.h2.origin":e,"copilot_api.h2.error":r.message}),Ge.delete(t),Ye.delete(t),Ke.delete(t),Ze(t)&&t.close().catch(e=>{Z.debug("Error closing GOAWAY'd HTTP/2 client:",e)})}),t}function tt(e){if(!v.connectivity.h2)return;Ve&&Ve!==e&&function(){for(let e of[...Ge.keys(),...Ye])Ze(e)&&e.close().catch(e=>{Z.debug("Error closing HTTP/2 pool entry:",e)});Ge.clear(),Ye.clear(),Ke.clear()}(),Ve=e;let t=Date.now();for(let[n]of Ge)if(t-(Ke.get(n)??0)>=6e5)Ge.delete(n),Ke.delete(n),Ze(n)&&n.close().catch(e=>{Z.debug("Error closing rotated HTTP/2 client:",e)}),K.add(1,{event:"rotated",origin:e}),G(j.INFO,"INFO",`HTTP/2 client rotated for ${e} (max lifetime reached)`,{"copilot_api.h2.event":"rotated","copilot_api.h2.origin":e});else{if(Ze(n))return Ge.delete(n),Ye.add(n),n;Ge.delete(n),Ke.delete(n)}let n=et(e);return Ye.add(n),n}function nt(e){e&&Ye.delete(e)&&(Ze(e)?Ge.set(e,Date.now()):Ke.delete(e))}function ot(){Xe||(Xe=setInterval(()=>{!function(){let e=Date.now(),t=v.connectivity.h2Keepalive,n=Ge.size;for(let[o,r]of Ge)e-r<6e4||t&&n<=1||(Ge.delete(o),Ke.delete(o),n--,Ze(o)&&o.close().catch(e=>{Z.debug("Error closing idle HTTP/2 client:",e)}),K.add(1,{event:"idle_reaped",origin:Ve??""}),G(j.INFO,"INFO",`HTTP/2 idle connection reaped for ${Ve}`,{"copilot_api.h2.event":"idle_reaped","copilot_api.h2.origin":Ve??""}))}()},3e4),Xe.unref())}function rt(){Qe||(Z.debug("Starting HTTP/2 keepalive (every 45s)"),Qe=setInterval(()=>{(async function(){if(0===Ge.size&&0===Ye.size){if(Ve){let e=et(Ve);Ge.set(e,Date.now())}return}let e=[...Ge.keys()];await Promise.allSettled(e.map(async e=>{if(!Ze(e))return Ge.delete(e),void Ke.delete(e);try{let{statusCode:t,body:n}=await e.request({path:"/models",method:"GET",headers:R(v)});await n.dump(),y.add(1,{result:"success"}),Z.debug(`HTTP/2 keepalive: GET /models → ${t}`),Ge.set(e,Date.now())}catch(t){y.add(1,{result:"error"}),Z.warn("HTTP/2 keepalive failed:",t instanceof Error?t.message:t),K.add(1,{event:"error",origin:Ve??""}),Ge.delete(e),Ke.delete(e),Ze(e)&&e.close().catch(e=>{Z.debug("Error closing broken HTTP/2 client:",e)})}}))})().catch(e=>{Z.debug("Unexpected error in HTTP/2 keepalive:",e)})},45e3),Qe.unref(),ot())}function at(e,t=""){let n,o=function(){let{platform:e,ppid:t,env:n}=ue;if("win32"===e){try{if(le(`wmic process get ParentProcessId,Name | findstr "${t}"`,{stdio:"pipe"}).toString().toLowerCase().includes("powershell.exe"))return"powershell"}catch{return"cmd"}return"cmd"}{let e=n.SHELL;if(e){if(e.endsWith("zsh"))return"zsh";if(e.endsWith("fish"))return"fish";if(e.endsWith("bash"))return"bash"}return"sh"}}(),r=Object.entries(e).filter(([,e])=>void 0!==e);switch(o){case"powershell":n=r.map(([e,t])=>`$env:${e} = ${t}`).join("; ");break;case"cmd":n=r.map(([e,t])=>`set ${e}=${t}`).join(" & ");break;case"fish":n=r.map(([e,t])=>`set -gx ${e} ${t}`).join("; ");break;default:{let e=r.map(([e,t])=>`${e}=${t}`).join(" ");n=r.length>0?`export ${e}`:"";break}}return n&&t?`${n}${"cmd"===o?" & ":" && "}${t}`:n||t}const it=e=>`${e}`,st=e=>`${e}`,lt=e=>`${e}`,ut=e=>`${e}`,ct=e=>`${e}`,pt=e=>`${e}`,dt=(e,t)=>`]8;;${e}\\${t}]8;;\\`,mt="?utm_source=copilot-cli&utm_medium=terminal&utm_campaign=startup_banner",ft=st(" ─────────────────────────────────────────────────────────────");function _t(e){return void 0===e?"":e>=1e6?`${(e/1e6).toFixed(0)}M`:`${(e/1e3).toFixed(0)}K`}const gt=[{title:"The 5-Hour Quota, Boris's Tweet, and What the Source Code Actually Reveals",teaser:"Claude Code's caching story — decoded from the source.",url:"https://barazany.dev/blog/claude-code-token-caching"},{title:"Claude Code's Compaction Engine: What the Source Code Actually Reveals",teaser:"What's actually running beneath every long session.",url:"https://barazany.dev/blog/claude-codes-compaction-engine"},{title:"We Were About to Buy an Automation Platform. We Already Had One.",teaser:"Governance spirals over n8n. The answer was already deployed.",url:"https://barazany.dev/blog/we-already-had-the-automation-platform"}];function ht(e){if(0===e.length)return[];let t=["",ft,` ${it("BARAZANY.DEV")} ${st("— Recent Posts")}`,ft,""];for(let[n,o]of e.entries()){let e=dt(`${o.url}${mt}`,it(pt(`[${n+1}]`)));t.push(` ${e} ${lt(o.title)}`,` ${st("→")} ${st(o.teaser)}`,"")}return t.push(ft,` ${dt(`https://barazany.dev/blog${mt}`,"barazany.dev/blog")}`,ft),t}function yt(e){let t=[];v.connectivity.h2Keepalive?t.push("HTTP/2 keepalive"):v.connectivity.h2&&t.push("HTTP/2"),t.push(`timeout ${e.timeout/1e3}s`);let n=e.webSearch?`Tavily ${ut("✓")}`:st("off"),o=["",` ${it("COPILOT-API")} ${st(`v${e.version}`)}`,ft,` ${st("Runtime")} ${e.runtime.name} ${e.runtime.version} (${e.runtime.platform}-${e.runtime.arch})`,` ${st("User")} ${e.user} ${st("·")} ${e.accountType}`];return v._v1&&o.push(` ${st("Client")} VSCode ${v._v1}`),o.push(` ${st("Network")} ${t.join(st(" · "))}`,` ${st("Search")} ${n}`),e.clipboardCopied&&o.push(` ${st("Clipboard")} Claude Code command ${ut("✓")}`),o.push(ft),o}function kt(e){if(!e.model&&!e.smallModel)return[];let t=["",` ${it("Active")}`];if(e.model){let n=v.models?.data.find(t=>t.id===e.model),o=_t(n?.capabilities.limits?.max_context_window_tokens);t.push(` ${lt(e.model.padEnd(32))} ${st("ctx")} ${it(o.padStart(5))} ${ct("← main")}`)}if(e.smallModel){let n=v.models?.data.find(t=>t.id===e.smallModel),o=_t(n?.capabilities.limits?.max_context_window_tokens);t.push(` ${e.smallModel.padEnd(32)} ${st("ctx")} ${o.padStart(5)} ${ct("← small")}`)}return t}function bt(e){let t=[];for(let[n,o]of function(){let e=[],t=[],n=[],o=[];for(let r of v.models?.data??[]){let a=r.capabilities.limits?.max_context_window_tokens,i={id:r.id,ctx:a};r.id.startsWith("text-embedding")?o.push(i):"Anthropic"===r.vendor?e.push(i):"Google"===r.vendor?n.push(i):t.push(i)}return[["Claude",e],["OpenAI",t],["Gemini",n],["Embedding",o]]}())if(0!==o.length){t.push("",` ${it(n)}`);for(let n of o){let o=n.id===e.model||n.id===e.smallModel,r=o?lt("●"):st("·"),a=o?lt(n.id.padEnd(34)):n.id.padEnd(34),i=n.ctx?_t(n.ctx).padStart(5):"";t.push(` ${r} ${a} ${st(i)}`)}}return t}function wt(e){let t=Math.floor(e/1e3),n=Math.floor(t/3600),o=Math.floor(t%3600/60);return n>0?`${n}h ${o}m`:o>0?`${o}m`:`${t}s`}function xt(e){(function(e){let{c:t,start:n,span:o}=e,r=t.get("requestData"),a=t.res.status,i=r?.model??"unknown",s=r?.streaming??!1,l=Date.now()-n;if(o.setAttribute("http.status_code",a),o.setAttribute("copilot_api.model",i),o.setAttribute("copilot_api.streaming",s),o.setAttribute("copilot_api.duration_ms",l),r?._d1&&o.setAttribute("copilot_api.copilot_duration_ms",r._d1),r?.tokenUsage){let e=r.tokenUsage;o.setAttribute("copilot_api.input_tokens",e.inputTokens),o.setAttribute("copilot_api.output_tokens",e.outputTokens),o.setAttribute("copilot_api.total_tokens",e.totalTokens),e.cachedTokens&&o.setAttribute("copilot_api.cached_tokens",e.cachedTokens),e.estimatedCost&&o.setAttribute("copilot_api.estimated_cost",e.estimatedCost)}t.res.headers.forEach((e,t)=>{let n=t.toLowerCase();B.has(n)||o.setAttribute(`http.response.header.${n}`,e)}),a>=400&&o.setStatus({code:oe.ERROR,message:`HTTP ${a}`}),o.end()})(e),function(e){let{c:t,start:n,method:r,path:i,endpointType:s,requestId:u}=e,c=Date.now()-n,p=t.get("requestData"),m=t.res.status,_=p?.model??"unknown",g=p?.streaming??!1,y={method:r,path:i,status_code:String(m),model:_,streaming:String(g),endpoint_type:s};if(l.add(1,y),a.record(c,y),m>=400&&h.add(1,{method:r,path:i,status_code:String(m),error_type:m>=500?"server_error":"client_error"}),p?.tokenUsage){let e=p.tokenUsage,t={model:_,endpoint_type:s};e.inputTokens&&(o.add(e.inputTokens,t),f.record(e.inputTokens,t)),e.outputTokens&&(E.add(e.outputTokens,t),d.record(e.outputTokens,t)),e.cachedTokens&&z.add(e.cachedTokens,{model:_})}!g&&p?.tokenUsage&&H.executeCompletion(u)}(e)}const vt=async()=>i.startActiveSpan("copilot_api.approval.await",async e=>{let t=performance.now();try{let n=await Z.prompt("Accept incoming request?",{type:"confirm"}),o=performance.now()-t;if(e.setAttribute("copilot_api.approval.wait_duration_ms",o),T.record(o),!n)throw e.setAttribute("copilot_api.approval.result","rejected"),N.add(1,{result:"rejected"}),new D("Request rejected",Response.json({message:"Request rejected"},{status:403}));e.setAttribute("copilot_api.approval.result","approved"),N.add(1,{result:"approved"})}finally{e.end()}}),At=3e-6,Tt=15e-6;function $t(e){let t=e.prompt_tokens||0,n=e.completion_tokens||0;return{inputTokens:t,outputTokens:n,totalTokens:e.total_tokens||t+n,estimatedCost:t*At+n*Tt,cachedTokens:e.prompt_tokens_details?.cached_tokens,acceptedPredictionTokens:e.completion_tokens_details?.accepted_prediction_tokens,rejectedPredictionTokens:e.completion_tokens_details?.rejected_prediction_tokens}}function St(e,t){void 0!==t.prompt_tokens&&e.setAttribute("copilot_api.input_tokens",t.prompt_tokens),void 0!==t.completion_tokens&&e.setAttribute("copilot_api.output_tokens",t.completion_tokens),void 0!==t.total_tokens&&e.setAttribute("copilot_api.total_tokens",t.total_tokens)}const Ot=[{pattern:/haiku/i,family:"haiku"},{pattern:/sonnet[.-]?4[.-]6/i,family:"sonnet-4.6"},{pattern:/(sonnet-4[.-]5|[.-]?3[.-]5[.-]?sonnet|[.-]?3[.-]7[.-]?sonnet)/i,family:"sonnet-4.5"},{pattern:/sonnet-4(?![.-]5|[.-]6|[.-]1)/i,family:"sonnet-4"},{pattern:/opus[.-]?4[.-]7/i,family:"opus-4.7"},{pattern:/opus[.-]?4[.-]6/i,family:"opus-4.6"},{pattern:/opus[.-]?4[.-]5/i,family:"opus-4.5"},{pattern:/opus/i,family:"opus"}],It={haiku:"claude-haiku-4.5","sonnet-4.6":"claude-sonnet-4.6","sonnet-4.5":"claude-sonnet-4.5","sonnet-4":"claude-sonnet-4","opus-4.7":"claude-opus-4.7","opus-4.6":"claude-opus-4.6","opus-4.5":"claude-opus-4.5",opus:"claude-opus-4.5"};function qt(e,t){let n=function(e){return e.startsWith("anthropic/")?e.slice(10):e}(e),[o,r]=function(e){let t=e.lastIndexOf("["),n=e.lastIndexOf("]");return-1!==t&&n===e.length-1&&n>t?[e.slice(0,t),e.slice(t+1,n)]:[e,null]}(n);if(t.includes(n))return n;if(r){let e=`${o}-${r}`.replaceAll(/(\d)-(\d)\b/g,"$1.$2");if(t.includes(e))return e}let a=function(e){for(let t of Ot)if(t.pattern.test(e))return t.family;return null}(o);if(!a)return null;let i=o.replace(/-\d{8}(?::.*)?$/,""),s=r?`-${r}`:"";for(let e of t){let t=e.replace(/-\d{8}(?::.*)?$/,""),n=`${i}${s}`.toLowerCase().replaceAll(/(\d)-(\d)\b/g,"$1.$2");if(t.toLowerCase().replaceAll(/(\d)-(\d)\b/g,"$1.$2")===n)return e}return function(e,t,n=null){let o=It[e];if(!o)return null;if(n){let e=`${o}-${n}`;if(t.includes(e))return e}if(t.includes(o))return o;if("sonnet-4.5"===e)for(let e of["claude-3.5-sonnet"]){let o=n?`${e}-${n}`:e;if(t.includes(o))return o}return null}(a,t,r)}var Nt=class extends Error{statusCode;constructor(e,t=400){super(e),this.name="ModelValidationError",this.statusCode=t}};function jt(t){if(!t||"string"!=typeof t)throw new Nt("Model name is required and must be a string");if(!v.models?.data)throw new Nt("Models not available. Please try again later.",503);let n=v.models.data.map(e=>e.id);if(n.includes(t))return t;let o=qt(t,n);if(!o)throw e.add(1,{requested_model:t}),new Nt(`Invalid model: '${t}'. Available services: ${n.join(", ")}`);return o}async function Pt(e){if(void 0===e.rateLimitSeconds)return;let t=e.rateLimitSeconds;return i.startActiveSpan("copilot_api.rate_limit.check",async n=>{try{let o=Date.now();if(n.setAttribute("copilot_api.rate_limit.configured_seconds",t),!e.lastRequestTimestamp)return e.lastRequestTimestamp=o,void n.setAttribute("copilot_api.rate_limit.action","allowed");let r=(o-e.lastRequestTimestamp)/1e3;if(n.setAttribute("copilot_api.rate_limit.elapsed_seconds",r),r>t)return e.lastRequestTimestamp=o,void n.setAttribute("copilot_api.rate_limit.action","allowed");let a=Math.ceil(t-r);if(n.setAttribute("copilot_api.rate_limit.wait_seconds",a),!e.rateLimitWait)throw n.setAttribute("copilot_api.rate_limit.action","rejected"),m.add(1,{action:"rejected",source:"local"}),Z.warn(`Rate limit exceeded. Need to wait ${a} more seconds.`),new D("Rate limit exceeded",Response.json({message:"Rate limit exceeded"},{status:429}));let i=1e3*a;n.setAttribute("copilot_api.rate_limit.action","waited"),m.add(1,{action:"waited",source:"local"}),_.record(i,{source:"local"}),Z.warn(`Rate limit reached. Waiting ${a} seconds before proceeding...`),await we(i),e.lastRequestTimestamp=o,Z.info("Rate limit wait completed, proceeding with request")}finally{n.end()}})}function Et(e){if("number"==typeof e){if(!Number.isFinite(e))return null;let t=Math.floor(e);return t>0?t:null}if("string"==typeof e){let t=e.trim();if(!t)return null;let n=Number(t);if(!Number.isFinite(n))return null;let o=Math.floor(n);return o>0?o:null}return null}function Ct(e,t,n){return Math.min(Math.max(e,t),n)}function Rt(e,t=0){if(t>5||!e||"object"!=typeof e)return null;let n=e.error;if(!n||"object"!=typeof n)return null;let o=n.code;if("string"==typeof o)return o;let r=n.message;if("string"!=typeof r)return null;try{return Rt(JSON.parse(r),t+1)}catch{return null}}function Bt(e,t){let n=e?.data.find(e=>e.id===t);if(!n)return null;let o=Et(n.capabilities.supports.min_thinking_budget),r=Et(n.capabilities.supports.max_thinking_budget);return!o||!r||r<=o?null:{bounds:{min:o,max:r-1},maxOutputTokens:Et(n.capabilities.limits?.max_output_tokens)??void 0}}function Dt(e){let{config:t}=e,n=t.bounds.min,o=t.bounds.max,r=Ct(Et(e.requestedBudget)??n,n,o),a=Et(e.maxTokens);if(a||=r+1,t.maxOutputTokens&&a>t.maxOutputTokens&&(a=t.maxOutputTokens),a<=r){let e=Math.min(r,a-1);if(e>=n)r=e;else{let e=n+1;if(t.maxOutputTokens&&e>t.maxOutputTokens)return{thinkingBudget:null,maxTokens:a};a=e,r=n}}return a<=r?{thinkingBudget:null,maxTokens:a}:{thinkingBudget:r,maxTokens:a}}function Mt(e,t,n){if(xe(e.thinking_budget))return e;if(!1===n?.enabled)return{...e,thinking_budget:void 0};let o=Bt(t,e.model);if(!o)return{...e,thinking_budget:void 0};let r=Dt({requestedBudget:e.thinking_budget,maxTokens:e.max_tokens,config:o});return null===r.thinkingBudget?{...e,max_tokens:r.maxTokens,thinking_budget:void 0}:{...e,max_tokens:r.maxTokens,thinking_budget:r.thinkingBudget}}function Ft(e){return"invalid_thinking_budget"===Rt(e)}async function Ht(e){let{payload:t,error:n,models:o,errorBodyText:r}=e;if(xe(t.thinking_budget)||!(n instanceof Error))return null;let a,i,s=n;if(!(s.response instanceof Response)||400!==s.response.status)return null;if(void 0!==r)a=r;else try{a=await s.response.clone().text()}catch{return null}try{i=JSON.parse(a)}catch{return null}if(!Ft(i))return null;let l=Bt(o,t.model);return l?function(e,t){let n=Dt({requestedBudget:e.thinking_budget,maxTokens:e.max_tokens,config:t});if(null===n.thinkingBudget)return{...e,max_tokens:n.maxTokens,thinking_budget:void 0};let o=n.thinkingBudget,r=Et(e.thinking_budget);if(r&&r===n.thinkingBudget){let e=o+1;e<=t.bounds.max&&(!n.maxTokens||e<n.maxTokens)&&(o=e)}let a=n.maxTokens;if((!a||a<=o)&&(a=o+1),t.maxOutputTokens&&a>t.maxOutputTokens){if(!(t.maxOutputTokens>o))return{...e,max_tokens:t.maxOutputTokens,thinking_budget:void 0};a=t.maxOutputTokens}return{...e,max_tokens:a,thinking_budget:o}}(t,l):{...t,thinking_budget:void 0}}function Jt(e){if(!(e instanceof Error))return!1;let t=e.code;return(!t||"UND_ERR_SOCKET"===t)&&/GOAWAY/i.test(e.message)}async function Ut(){return new Promise(e=>setTimeout(e,100))}function Lt(e){if("string"==typeof e)return function(e){let t=e,n=String.fromCodePoint(27),o=t.split(n);if(o.length>1){t=o[0];for(let e=1;e<o.length;e++){let r=o[e],a=r.match(/^\[[0-9;]*[a-z]/i);t+=a?r.slice(a[0].length):n+r}}return t.replaceAll(/[\u200B-\u200D\uFEFF]/g,"").replaceAll(/[\u2060-\u2064]/g,"").replaceAll(/[\u206A-\u206F]/g,"").replaceAll(/[\u180E\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]/g,"").replaceAll(/[\uFFF0-\uFFFF]/g,"").replaceAll(/[\x00-\x08\v\f\x0E-\x1F]/g,"")}(e);if(Array.isArray(e))return e.map(e=>Lt(e));if(e&&"object"==typeof e){let t={};for(let[n,o]of Object.entries(e))t[n]=Lt(o);return"object"===t.type&&!("properties"in t)&&(t.properties={}),t}return e}const Wt=async e=>{let t=Lt(e);try{return await zt(t)}catch(e){let n;if(e instanceof D)try{n=await e.response.clone().text()}catch{n=void 0}let o=await Ht({payload:t,error:e,models:v.models,errorBodyText:n});if(o)return e instanceof D&&void 0!==n&&Z.warn(`Upstream rejected thinking_budget (status ${e.response.status}): ${n.slice(0,500)}`),Z.warn("Adjusting thinking_budget payload and retrying once..."),await zt(Lt(o));throw e}},zt=async e=>{if(!v._t1)throw Error("Service token not found");let t=e.messages.some(e=>"string"!=typeof e.content&&e.content?.some(e=>"image_url"===e.type)),n=e.messages.some(e=>["assistant","tool"].includes(e.role)),o={...R(v,t),"X-Initiator":n?"agent":"user"},r=O(v),a=`${r}/chat/completions`,i=v.timeoutMs,s=JSON.stringify(e),l={"copilot_api.model":e.model,"copilot_api.streaming":!!e.stream,"copilot_api.timeout_ms":i,"copilot_api.has_tools":!!e.tools?.length,"copilot_api.tool_count":e.tools?.length??0,"copilot_api.message_count":e.messages.length,"copilot_api.max_tokens":e.max_tokens??0,"copilot_api.vision_enabled":t,"copilot_api.initiator":n?"agent":"user"};for(let t=0;t<=1;t++){let n=tt(r),u=new AbortController,c=setTimeout(()=>u.abort(),i);try{let{statusCode:r,headers:c,body:p,span:d}=await w({spanName:"copilot POST /chat/completions",url:a,target:"chat_completions",headers:o,body:s,signal:u.signal,headersTimeout:i,bodyTimeout:3*i,extraAttributes:{...l,"copilot_api.h2.retry_attempt":t},dispatcher:n}),m=new Response(p,{status:r,headers:c});if(!m.ok)throw d.end(),new D("Failed to create chat completions",m);if(e.stream)return{events:ge(m),span:d,release:()=>nt(n)};let f=await m.json();return f.id&&d.setAttribute("copilot_api.response.id",f.id),f.usage&&St(d,f.usage),d.end(),nt(n),f}catch(e){if(nt(n),Jt(e)&&t<1){Z.warn(`HTTP/2 GOAWAY during chat completions, retrying (attempt ${t+1})`),await Ut();continue}throw e}finally{clearTimeout(c)}}throw Error("Unexpected: exhausted GOAWAY retries without result")};function Kt(e){return i.startActiveSpan("copilot_api.model.validate",t=>{try{t.setAttribute("copilot_api.model.requested",e.model);let n=jt(e.model);return t.setAttribute("copilot_api.model.normalized",n),n===e.model?(t.setAttribute("copilot_api.model.was_mapped",!1),e):(t.setAttribute("copilot_api.model.was_mapped",!0),C.add(1,{from_model:e.model,to_model:n}),Z.debug(`Normalized model from '${e.model}' to '${n}'`),{...e,model:n})}finally{t.end()}})}function Gt(e,t){if(!e.data)return e;try{let n=JSON.parse(e.data);if(n.usage&&t(n.usage),void 0!==n.usage?.prompt_tokens_details?.cached_tokens)return{...e,data:JSON.stringify({...n,usage:{...n.usage,prompt_tokens_details:void 0}})}}catch(t){Z.warn("Failed to parse streaming chunk:",e.data.slice(0,200),t)}return e}const Yt=e=>Object.hasOwn(e,"choices"),Vt=new me;Vt.post("/",async e=>{try{return await async function(e){await Pt(v);let t=await e.req.json();Z.debug("Request payload:",JSON.stringify(t).slice(-400));try{t=Kt(t)}catch(t){if(t instanceof Nt)return e.json({error:{message:t.message,type:"invalid_request_error",code:"invalid_model"}},t.statusCode);throw t}if(e.set("requestData",{model:t.model,streaming:!!t.stream}),v.manualApprove&&await vt(),xe(t.max_tokens)){let e=v.models?.data.find(e=>e.id===t.model);t={...t,max_tokens:e?.capabilities.limits?.max_output_tokens},Z.debug("Set max_tokens to:",JSON.stringify(t.max_tokens))}t=Mt(t,v.models,{enabled:v.thinkingEnabled}),xe(t.thinking_budget)||Z.debug("Normalized thinking_budget to:",JSON.stringify(t.thinking_budget));let n=performance.now(),o=await Wt(t),r=performance.now()-n;if(S.record(r,{model:t.model,target:"chat_completions",streaming:String(!!t.stream)}),Yt(o)){let t=e.get("requestData")||{};return o.usage&&(t.tokenUsage=$t(o.usage)),t._d1=r,e.set("requestData",t),Z.debug("Non-streaming response:",JSON.stringify(o)),e.json(o)}let{events:a,span:i,release:s}=o;return _e(e,async n=>{let o={value:null},l=0,u=performance.now();try{try{for await(let i of a){let a=i;i.data&&"[DONE]"!==i.data&&(l++,g.add(1,{model:t.model,endpoint_type:"openai"}),a=Gt(i,t=>{o.value=t;let n=e.get("requestData")||{};n.tokenUsage=$t(t),n._d1=r,e.set("requestData",n)})),await n.writeSSE(a)}let i=performance.now()-u,s={model:t.model,endpoint_type:"openai"};if(V.record(i,s),Y.record(l,s),o.value){let t=e.get("requestData")||{};t.tokenUsage||(t.tokenUsage=$t(o.value),t._d1=r,e.set("requestData",t))}}finally{o.value&&St(i,o.value),i.end(),s()}let c=e.get("requestId");c&&H.executeCompletion(c)}finally{e.get("resolveStreamComplete")?.()}})}(e)}catch(t){return await J(e,t)}});const Qt=new me;Qt.post("/",async e=>{try{let t=await e.req.json();try{jt(t.model)}catch(t){if(t instanceof Nt)return e.json({error:{message:t.message,type:"invalid_request_error",code:"invalid_model"}},t.statusCode);throw t}e.set("requestData",{model:t.model});let n=performance.now(),o=await(async e=>{if(!v._t1)throw Error("Service token not found");let t=`${O(v)}/embeddings`,n=R(v),o=v.timeoutMs,r=new AbortController,a=setTimeout(()=>r.abort(),o);try{let{response:o,span:a}=await k({spanName:"copilot POST /embeddings",url:t,target:"embeddings",method:"POST",headers:n,body:JSON.stringify(e),signal:r.signal,extraAttributes:{"copilot_api.model":e.model,"copilot_api.input_count":Array.isArray(e.input)?e.input.length:1}});if(!o.ok)throw a.end(),new D("Failed to create embeddings",o);let i=await o.json();return a.end(),i}finally{clearTimeout(a)}})(t),r=performance.now()-n;S.record(r,{model:t.model,target:"embeddings",streaming:"false"});let a=e.get("requestData")||{};return a.tokenUsage=$t(o.usage),a._d1=r,e.set("requestData",a),e.json(o)}catch(t){return await J(e,t)}});const Xt=new he(ke),Zt={...ye["anthropic/claude-sonnet-4"].tokens,baseOverhead:4,perTool:27};function en(e){return Xt.encode(e).length}function tn(e){let t=0;for(let n of e)switch(n.type){case"text":t+=en(n.text)*Zt.contentMultiplier;break;case"tool_use":t+=en(n.name+JSON.stringify(n.input))*Zt.contentMultiplier;break;case"tool_result":n.content&&(t+=en("string"==typeof n.content?n.content:JSON.stringify(n.content))*Zt.contentMultiplier);break;case"thinking":t+=en(n.thinking)*Zt.contentMultiplier;break;case"image":t+=1600}return t}function nn(e){let t=0,n=e.properties;if(!n)return t;let o=Object.entries(n);for(let[e,[n,r]]of o.entries()){if(t+=0===e?Zt.perFirstProp:Zt.perAdditionalProp,t+=en(n),r.description&&(t+=Zt.perPropDesc+en(r.description)),r.enum&&Array.isArray(r.enum)){t+=Zt.perEnum;for(let e of r.enum)t+=en(String(e))}if("object"===r.type&&r.properties&&(t+=Zt.perNestedObject,t+=nn(r)),"array"===r.type&&r.items){let e=r.items;"object"===e.type&&e.properties&&(t+=Zt.perArrayOfObjects,t+=nn(e))}}return t}const on=async e=>{if(!v._t1)throw Error("Service token not found");let t=Lt(e),n={...R(v),"X-Initiator":"agent"},o=O(v),r=`${o}/responses`,a=v.timeoutMs,i=JSON.stringify(t),s={"copilot_api.model":t.model,"copilot_api.streaming":!!t.stream,"copilot_api.timeout_ms":a,"copilot_api.has_tools":!!t.tools?.length,"copilot_api.tool_count":t.tools?.length??0,"copilot_api.max_output_tokens":t.max_output_tokens??0,"copilot_api.api_type":"responses"};for(let e=0;e<=1;e++){let l=tt(o),u=new AbortController,c=setTimeout(()=>u.abort(),a);try{let{statusCode:o,headers:c,body:p,span:d}=await w({spanName:"copilot POST /responses",url:r,target:"responses",headers:n,body:i,signal:u.signal,headersTimeout:a,bodyTimeout:3*a,extraAttributes:{...s,"copilot_api.h2.retry_attempt":e},dispatcher:l}),m=new Response(p,{status:o,headers:c});if(!m.ok)throw d.end(),new D("Failed to create responses",m);if(t.stream)return{events:ge(m),span:d,release:()=>nt(l)};let f=await m.json();return f.id&&d.setAttribute("copilot_api.response.id",f.id),f.usage&&St(d,{prompt_tokens:f.usage.input_tokens,completion_tokens:f.usage.output_tokens,total_tokens:f.usage.total_tokens}),d.end(),nt(l),f}catch(t){if(nt(l),Jt(t)&&e<1){Z.warn(`HTTP/2 GOAWAY during responses, retrying (attempt ${e+1})`),await Ut();continue}throw t}finally{clearTimeout(c)}}throw Error("Unexpected: exhausted GOAWAY retries without result")};function rn(e){return null===e?null:{stop:"end_turn",length:"max_tokens",tool_calls:"tool_use",content_filter:"end_turn"}[e]}function an(e,t){let n={model:e.model,messages:sn(e.messages,e.system),max_tokens:e.max_tokens,stop:e.stop_sequences,stream:e.stream,temperature:e.temperature,top_p:e.top_p,user:e.metadata?.user_id,tools:dn(e.tools),tool_choice:mn(e.tool_choice)},o=e.thinking;if("enabled"===o?.type||"adaptive"===o?.type){let r=Bt(t,n.model);if(r){let a="enabled"===o.type&&"number"==typeof o.budget_tokens?o.budget_tokens:function(e,t,n){let o=function(e,t){return"max"===e?t:"high"===e?16e3:"medium"===e?8e3:1024}(e,n.max),r=Et(t),a=r&&r>1?r-1:n.min;return Ct(Math.min(o,a),n.min,n.max)}(e.output_config?.effort??"high",n.max_tokens,r.bounds);n=Mt({...n,thinking_budget:a},t)}}return n}function sn(e,t){let n=function(e){return e?"string"==typeof e?[{role:"system",content:ln(e)}]:[{role:"system",content:ln(e.map(e=>e.text).join("\n\n"))}]:[]}(t),o=e.flatMap(e=>"user"===e.role?function(e){let t=[];if(Array.isArray(e.content)){let n=e.content.filter(e=>"tool_result"===e.type),o=e.content.filter(e=>"tool_result"!==e.type);for(let e of n)t.push({role:"tool",tool_call_id:e.tool_use_id,content:pn(e.content)});o.length>0&&t.push({role:"user",content:pn(o)})}else t.push({role:"user",content:pn(e.content)});return t}(e):function(e){let t=function(e){return{reasoning_text:cn(e.reasoning_text),reasoning_opaque:cn(e.reasoning_opaque)}}(e);if(!Array.isArray(e.content))return[{role:"assistant",content:pn(e.content),...t}];let n=e.content.filter(e=>"tool_use"===e.type),o=e.content.filter(e=>"text"===e.type),r=e.content.filter(e=>"thinking"===e.type),a=[...o.map(e=>e.text),...r.map(e=>e.thinking)].join("\n\n");return n.length>0?[{role:"assistant",content:a||null,...t,tool_calls:n.map(e=>({id:e.id,type:"function",function:{name:e.name,arguments:JSON.stringify(e.input)}}))}]:[{role:"assistant",content:pn(e.content),...t}]}(e));return[...n,...o]}function ln(e){return e.startsWith("x-anthropic-billing-header")?e.replace(/^x-_an1-billing-header:[^\n]*\n+/,""):e}const un=32768;function cn(e){if("string"==typeof e)return e.length<=un?e:e.slice(0,un)}function pn(e){if("string"==typeof e)return e;if(!Array.isArray(e))return null;if(!e.some(e=>"image"===e.type))return e.filter(e=>"text"===e.type||"thinking"===e.type).map(e=>"text"===e.type?e.text:e.thinking).join("\n\n");let t=[];for(let n of e)switch(n.type){case"text":t.push({type:"text",text:n.text});break;case"thinking":t.push({type:"text",text:n.thinking});break;case"image":t.push({type:"image_url",image_url:{url:`data:${n.source.media_type};base64,${n.source.data}`}})}return t}function dn(e){if(e)return e.map(e=>({type:"function",function:{name:e.name,description:e.description,parameters:e.input_schema}}))}function mn(e){if(e)switch(e.type){case"auto":return"auto";case"any":return"required";case"tool":return e.name?{type:"function",function:{name:e.name}}:void 0;case"none":return"none";default:return}}function fn(e){return"string"==typeof e?[{type:"text",text:e}]:Array.isArray(e)?e.filter(e=>"text"===e.type).map(e=>({type:"text",text:e.text})):[]}function _n(e){return e.reasoning_text?[{type:"thinking",thinking:e.reasoning_text}]:[]}function gn(e){return e?e.map(e=>({type:"tool_use",id:e.id,name:e.function.name,input:JSON.parse(e.function.arguments)})):[]}function hn(e){let t={model:e.model,input:kn(e.messages),max_output_tokens:Math.max(e.max_tokens,16),stream:e.stream,temperature:e.temperature,top_p:e.top_p,store:!1,user:e.metadata?.user_id?.slice(0,64)},n=function(e){return e?function(e){return e.startsWith("x-anthropic-billing-header")?e.replace(/^x-_an1-billing-header:[^\n]*\n+/,""):e}("string"==typeof e?e:e.map(e=>e.text).join("\n\n")):null}(e.system);return n&&(t.instructions=n),e.tools?.length&&(t.tools=e.tools.map(e=>({type:"function",name:e.name,description:e.description,parameters:e.input_schema}))),e.tool_choice&&(t.tool_choice=function(e){switch(e.type){case"auto":return"auto";case"any":return"required";case"none":return"none";case"tool":return e.name?{type:"function",name:e.name}:void 0;default:return}}(e.tool_choice)),"adaptive"===e.thinking?.type?t.reasoning={effort:yn(e.output_config?.effort??"high")}:"enabled"===e.thinking?.type&&e.thinking.budget_tokens&&(t.reasoning=function(e){return e<5e3?{effort:"low"}:e<2e4?{effort:"medium"}:e<32e3?{effort:"high"}:{effort:"xhigh"}}(e.thinking.budget_tokens)),t}function yn(e){switch(e){case"low":return"low";case"medium":return"medium";case"max":return"xhigh";default:return"high"}}function kn(e){let t=[];for(let n of e)"user"===n.role?t.push(...bn(n)):t.push(...xn(n));return t}function bn(e){if("string"==typeof e.content)return[{type:"message",role:"user",content:e.content}];let t=[],n=e.content.filter(e=>"tool_result"===e.type),o=e.content.filter(e=>"tool_result"!==e.type);for(let e of n)t.push(wn(e));if(o.length>0)if(o.some(e=>"image"===e.type)){let e=[];for(let t of o)"text"===t.type?e.push({type:"input_text",text:t.text}):e.push({type:"input_image",image_url:`data:${t.source.media_type};base64,${t.source.data}`});t.push({type:"message",role:"user",content:e})}else{let e=o.filter(e=>"text"===e.type).map(e=>e.text).join("\n\n");e&&t.push({type:"message",role:"user",content:e})}return t}function wn(e){let t;return t="string"==typeof e.content?e.content:Array.isArray(e.content)?e.content.filter(e=>"text"===e.type).map(e=>e.text).join("\n"):"",e.is_error&&(t=`[ERROR] ${t}`),{type:"function_call_output",call_id:e.tool_use_id,output:t}}function xn(e){if("string"==typeof e.content)return[{type:"message",role:"assistant",content:e.content}];let t=[],n=e.content.filter(e=>"text"===e.type),o=e.content.filter(e=>"thinking"===e.type),r=e.content.filter(e=>"tool_use"===e.type),a=[...n.map(e=>e.text),...o.map(e=>e.thinking)].join("\n\n");a&&t.push({type:"message",role:"assistant",content:a});for(let e of r)t.push({type:"function_call",call_id:e.id,name:e.name,arguments:JSON.stringify(e.input)});return t}function vn(e,t){if(t)return"tool_use";switch(e){case"completed":case"cancelled":case"failed":return"end_turn";case"incomplete":return"max_tokens";default:return null}}function An(e,t){e.contentBlockOpen&&=(t.push({type:"content_block_stop",index:e.contentBlockIndex}),e.contentBlockIndex++,!1)}function Tn(e,t,n){e.messageStartSent||=(e.responseId=n?.responseId??e.responseId,e.model=n?.model??e.model,t.push({type:"message_start",message:{id:e.responseId,type:"message",role:"assistant",content:[],model:e.model,stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0}}}),!0)}function $n(e,t){let n=[];switch(e.type){case"response.created":Tn(t,n,{responseId:e.response.id,model:e.response.model});break;case"response.in_progress":case"response.output_text.done":case"response.function_call_arguments.done":case"response.refusal.done":case"response.reasoning_summary_text.delta":case"response.reasoning_summary_text.done":default:break;case"response.output_item.added":if("function_call"===e.item.type){Tn(t,n),An(t,n);let o=e.item;t.toolCalls[e.output_index]={call_id:o.call_id,name:o.name,_bi1:t.contentBlockIndex},n.push({type:"content_block_start",index:t.contentBlockIndex,content_block:{type:"tool_use",id:o.call_id,name:o.name,input:{}}}),t.contentBlockOpen=!0}break;case"response.content_part.added":Tn(t,n),An(t,n),n.push({type:"content_block_start",index:t.contentBlockIndex,content_block:{type:"text",text:""}}),t.contentBlockOpen=!0;break;case"response.output_text.delta":n.push({type:"content_block_delta",index:t.contentBlockIndex,delta:{type:"text_delta",text:e.delta}});break;case"response.content_part.done":An(t,n);break;case"response.function_call_arguments.delta":{let o=t.toolCalls[e.output_index];o&&n.push({type:"content_block_delta",index:o._bi1,delta:{type:"input_json_delta",partial_json:e.delta}});break}case"response.output_item.done":if("function_call"===e.item.type){let o=t.toolCalls[e.output_index];o&&t.contentBlockOpen&&(n.push({type:"content_block_stop",index:o._bi1}),t.contentBlockIndex++,t.contentBlockOpen=!1)}break;case"response.completed":{An(t,n);let o=e.response.output.some(e=>"function_call"===e.type),r=e.response.usage;n.push({type:"message_delta",delta:{stop_reason:vn(e.response.status,o),stop_sequence:null},usage:{input_tokens:r?.input_tokens??0,output_tokens:r?.output_tokens??0}},{type:"message_stop"});break}case"response.incomplete":{Tn(t,n),An(t,n);let o=e.response.usage;n.push({type:"message_delta",delta:{stop_reason:"max_tokens",stop_sequence:null},usage:{input_tokens:o?.input_tokens??0,output_tokens:o?.output_tokens??0}},{type:"message_stop"});break}case"response.failed":Tn(t,n),An(t,n),n.push({type:"error",error:{type:e.response.error?.code??"api_error",message:e.response.error?.message??"Response generation failed"}});break;case"response.cancelled":Tn(t,n),An(t,n),n.push({type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{input_tokens:0,output_tokens:0}},{type:"message_stop"});break;case"response.refusal.delta":Tn(t,n),t.contentBlockOpen||=(n.push({type:"content_block_start",index:t.contentBlockIndex,content_block:{type:"text",text:""}}),!0),n.push({type:"content_block_delta",index:t.contentBlockIndex,delta:{type:"text_delta",text:e.delta}})}return n}function Sn(e,t){e.thinkingBlockOpen||(e.contentBlockOpen&&=(t.push({type:"content_block_stop",index:e.contentBlockIndex}),e.contentBlockIndex++,!1),t.push({type:"content_block_start",index:e.contentBlockIndex,content_block:{type:"thinking",thinking:""}}),e.thinkingBlockOpen=!0,e.contentBlockOpen=!0)}function On(e,t){e.thinkingBlockOpen&&(t.push({type:"content_block_stop",index:e.contentBlockIndex}),e.contentBlockIndex++,e.thinkingBlockOpen=!1,e.contentBlockOpen=!1)}function In(e,t){let n=[];if(0===e.choices.length)return n;let o=e.choices[0],{delta:r}=o;if(t.messageStartSent||=(n.push({type:"message_start",message:{id:e.id,type:"message",role:"assistant",content:[],model:e.model,stop_reason:null,stop_sequence:null,usage:{input_tokens:e.usage?.prompt_tokens??0,output_tokens:0}}}),!0),"string"==typeof r.reasoning_text&&""!==r.reasoning_text&&(Sn(t,n),n.push({type:"content_block_delta",index:t.contentBlockIndex,delta:{type:"thinking_delta",thinking:r.reasoning_text}})),"string"==typeof r.reasoning_opaque&&""!==r.reasoning_opaque&&(Sn(t,n),n.push({type:"content_block_delta",index:t.contentBlockIndex,delta:{type:"signature_delta",signature:r.reasoning_opaque}}),On(t,n)),r.content&&(On(t,n),function(e){return!!e.contentBlockOpen&&Object.values(e.toolCalls).some(t=>t._bi1===e.contentBlockIndex)}(t)&&(n.push({type:"content_block_stop",index:t.contentBlockIndex}),t.contentBlockIndex++,t.contentBlockOpen=!1),t.contentBlockOpen||=(n.push({type:"content_block_start",index:t.contentBlockIndex,content_block:{type:"text",text:""}}),!0),n.push({type:"content_block_delta",index:t.contentBlockIndex,delta:{type:"text_delta",text:r.content}})),r.tool_calls){On(t,n);for(let e of r.tool_calls){if(e.id&&e.function?.name){t.contentBlockOpen&&=(n.push({type:"content_block_stop",index:t.contentBlockIndex}),t.contentBlockIndex++,!1);let o=t.contentBlockIndex;t.toolCalls[e.index]={id:e.id,name:e.function.name,_bi1:o},n.push({type:"content_block_start",index:o,content_block:{type:"tool_use",id:e.id,name:e.function.name,input:{}}}),t.contentBlockOpen=!0}if(e.function?.arguments){let o=t.toolCalls[e.index];o&&n.push({type:"content_block_delta",index:o._bi1,delta:{type:"input_json_delta",partial_json:e.function.arguments}})}}}return o.finish_reason&&(On(t,n),t.contentBlockOpen&&=(n.push({type:"content_block_stop",index:t.contentBlockIndex}),!1),n.push({type:"message_delta",delta:{stop_reason:rn(o.finish_reason),stop_sequence:null},usage:{input_tokens:e.usage?.prompt_tokens??0,output_tokens:e.usage?.completion_tokens??0}},{type:"message_stop"})),n}async function qn(e,t,n){let o={query:t,max_results:n?.maxResults??5};n?.includeDomains?.length&&(o.include_domains=n.includeDomains),n?.excludeDomains?.length&&(o.exclude_domains=n.excludeDomains),Z.debug("Tavily search request:",JSON.stringify(o));let r=await fetch("https://api.tavily.com/search",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(o)});if(!r.ok){let e=await r.text();throw Z.error(`Tavily API error (${r.status}):`,e),Error(`Tavily API error: ${r.status} ${e}`)}let a=await r.json();return Z.debug(`Tavily returned ${a.results.length} results`),a.results}function Nn(){let e="";for(let t=0;t<24;t++)e+="abcdefghijklmnopqrstuvwxyz0123456789"[Math.floor(36*Math.random())];return e}async function jn(e,t){let n=function(e){let t,n=e.messages[0];if(!n||"user"!==n.role)return null;if("string"==typeof n.content)t=n.content;else{if(!Array.isArray(n.content))return null;{let e=n.content.find(e=>"text"===e.type);if(!e)return null;t=e.text}}return t.startsWith("Perform a web search for the query: ")?t.slice(36).trim():t.trim()||null}(t);if(!n)return e.json({type:"error",error:{type:"invalid_request_error",message:"Could not extract search query from web search request"}},400);let o=function(e){let t=e.tools?.[0];return{maxResults:"number"==typeof t?.max_uses?t.max_uses:5,allowedDomains:t?.allowed_domains,blockedDomains:t?.blocked_domains}}(t);Z.info(`Web search intercepted: "${n}"`);let r=[];try{let e=v.tavilyApiKey;if(!e)throw Error("No Tavily API key configured");r=(await qn(e,n,{maxResults:Math.min(o.maxResults,10),includeDomains:o.allowedDomains,excludeDomains:o.blockedDomains})).map(e=>({type:"web_search_result",url:e.url,title:e.title,encrypted_content:Buffer.from(e.content).toString("base64"),page_age:null}))}catch(e){Z.error("Tavily search failed:",e)}let a={query:n,results:r,model:t.model};if(!t.stream)return e.json(function(e){let{query:t,results:n,model:o}=e,r=`srvtoolu_${Nn()}`,a=[{type:"text",text:"I'll search for that."},{type:"server_tool_use",id:r,name:"web_search",input:{query:t}},{type:"web_search_tool_result",tool_use_id:r,content:n}];return{id:`msg_tavily_${Nn()}`,type:"message",role:"assistant",model:o,content:a,stop_reason:"end_turn",stop_sequence:null,usage:{input_tokens:0,output_tokens:0}}}(a));let i=function(e){let{query:t,results:n,model:o}=e,r=`srvtoolu_${Nn()}`,a=`msg_tavily_${Nn()}`,i=[];return i.push({type:"message_start",message:{id:a,type:"message",role:"assistant",content:[],model:o,stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0}}},{type:"content_block_start",index:0,content_block:{type:"text",text:""}},{type:"content_block_delta",index:0,delta:{type:"text_delta",text:"I'll search for that."}},{type:"content_block_stop",index:0},{type:"content_block_start",index:1,content_block:{type:"server_tool_use",id:r,name:"web_search",input:{}}},{type:"content_block_delta",index:1,delta:{type:"input_json_delta",partial_json:JSON.stringify({query:t})}},{type:"content_block_stop",index:1},{type:"content_block_start",index:2,content_block:{type:"web_search_tool_result",tool_use_id:r,content:n}},{type:"content_block_stop",index:2},{type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{output_tokens:0}},{type:"message_stop"}),i}(a);return _e(e,async e=>{for(let t of i)await e.writeSSE({event:t.type,data:JSON.stringify(t)})})}function Pn(e){return function(e){let t=e.thinking;if(null==t)return null;if("object"!=typeof t)return"thinking must be an object";let n=t,o=new Set(["enabled","adaptive","disabled"]);return"string"==typeof n.type&&o.has(n.type)?"enabled"!==n.type||void 0===n.budget_tokens||"number"==typeof n.budget_tokens&&Number.isFinite(n.budget_tokens)&&!(n.budget_tokens<=0)&&Number.isInteger(n.budget_tokens)?null:"thinking.budget_tokens must be a positive integer":"thinking.type must be one of: enabled, adaptive, disabled"}(e)??function(e){let t=e.output_config;if(null==t)return null;if("object"!=typeof t)return"output_config must be an object";let n=t;if(void 0===n.effort)return null;let o=new Set(["low","medium","high","max"]);return"string"==typeof n.effort&&o.has(n.effort)?null:"output_config.effort must be one of: low, medium, high, max"}(e)}async function En(e){let t=await e.req.json();if(function(e){let t=e.tools;if(!t||1!==t.length)return!1;let n=t[0].type;return"string"==typeof n&&n.startsWith("web_search")}(t))return jn(e,t);await Pt(v),Z.debug("Anthropic request payload:",JSON.stringify(t));let n=e.req.header("anthropic-beta")??"",o=/context-1m/i.test(n);if(o&&/claude/i.test(t.model)){let e=t.model;t={...t,model:`${t.model}-1m`},Z.debug(`Detected context-1m beta flag, rewriting model: '${e}' → '${t.model}'`)}let r=function(e,t){return i.startActiveSpan("copilot_api.model.validate",n=>{try{n.setAttribute("copilot_api.model.requested",t.model);let o=jt(t.model);n.setAttribute("copilot_api.model.normalized",o);let r=Pn(t);return r?{ok:!1,response:e.json({type:"error",error:{type:"invalid_request_error",message:r}},400)}:o===t.model?(n.setAttribute("copilot_api.model.was_mapped",!1),{ok:!0,payload:t}):(n.setAttribute("copilot_api.model.was_mapped",!0),C.add(1,{from_model:t.model,to_model:o}),Z.debug(`Model mapping: '${t.model}' → '${o}'`),{ok:!0,payload:{...t,model:o}})}catch(t){if(t instanceof Nt)return{ok:!1,response:e.json({type:"error",error:{type:"invalid_request_error",message:t.message}},t.statusCode)};throw t}finally{n.end()}})}(e,t);if(!r.ok)return r.response;if(t=r.payload,function(e){return/gpt-[\d.]+-codex/i.test(e)}(t.model))return async function(e,t){let n=performance.now(),o=i.startActiveSpan("copilot_api.translate.anthropic_to_responses",e=>{try{return e.setAttribute("copilot_api.translation.direction","anthropic_to_responses"),e.setAttribute("copilot_api.translation.message_count",t.messages.length),hn(t)}finally{e.end()}});c.record(performance.now()-n,{direction:"anthropic_to_responses"}),Z.debug("Translated Responses API payload:",JSON.stringify(o)),e.set("requestData",{model:o.model,streaming:!!o.stream}),v.manualApprove&&await vt();let r=performance.now(),a=await on(o),s=performance.now()-r;if(S.record(s,{model:o.model,target:"responses",streaming:String(!!o.stream)}),Bn(a)){let t=e.get("requestData")||{};if(a.usage&&(t.tokenUsage=$t({prompt_tokens:a.usage.input_tokens,completion_tokens:a.usage.output_tokens,total_tokens:a.usage.total_tokens})),t._d1=s,e.set("requestData",t),Z.debug("Non-streaming Responses API response:",JSON.stringify(a).slice(-400)),"failed"===a.status)return e.json({type:"error",error:{type:a.error?.code??"api_error",message:a.error?.message??"Response generation failed"}},500);let n=performance.now(),o=i.startActiveSpan("copilot_api.translate.responses_to_anthropic",e=>{try{return e.setAttribute("copilot_api.translation.direction","responses_to_anthropic"),function(e){let t=[],n=!1;for(let o of e.output)if("message"===o.type)for(let e of o.content)e.text&&t.push({type:"text",text:e.text});else if("function_call"===o.type){n=!0;let e={};try{e=JSON.parse(o.arguments)}catch{}t.push({type:"tool_use",id:o.call_id,name:o.name,input:e})}return{id:e.id,type:"message",role:"assistant",model:e.model,content:t,stop_reason:vn(e.status,n),stop_sequence:null,usage:{input_tokens:e.usage?.input_tokens??0,output_tokens:e.usage?.output_tokens??0}}}(a)}finally{e.end()}});return c.record(performance.now()-n,{direction:"responses_to_anthropic"}),e.json(o)}Z.debug("Streaming Responses API response");let{events:l,span:u,release:p}=a;return _e(e,async t=>{let n=null,r=0,a=performance.now(),i={messageStartSent:!1,contentBlockIndex:0,contentBlockOpen:!1,responseId:"",model:"",toolCalls:{}};try{try{for await(let a of l){if(Z.debug("Responses API raw stream event:",JSON.stringify(a)),!a.data)continue;let l;try{l=JSON.parse(a.data)}catch(e){Z.warn("Failed to parse streaming chunk:",a.data.slice(0,200),e);continue}r++,g.add(1,{model:o.model,endpoint_type:"responses"});let u=Cn(l);if(u){n=u;let t=e.get("requestData")||{};t.tokenUsage=$t(n),t._d1=s,e.set("requestData",t)}let c=$n(l,i);for(let e of c)Z.debug("Translated Anthropic event:",JSON.stringify(e)),await t.writeSSE({event:e.type,data:JSON.stringify(e)})}let u=performance.now()-a,c={model:o.model,endpoint_type:"responses"};V.record(u,c),Y.record(r,c)}finally{n&&St(u,n),u.end(),p()}let c=e.get("requestId");c&&H.executeCompletion(c)}finally{e.get("resolveStreamComplete")?.()}})}(e,t);let a=performance.now(),s=i.startActiveSpan("copilot_api.translate.anthropic_to_openai",e=>{try{e.setAttribute("copilot_api.translation.direction","anthropic_to_openai"),e.setAttribute("copilot_api.translation.message_count",t.messages.length),e.setAttribute("copilot_api.translation.has_tools",!!t.tools?.length),e.setAttribute("copilot_api.translation.has_system",!!t.system),e.setAttribute("copilot_api.translation.context_1m_beta",o);let n=t.messages.some(e=>Array.isArray(e.content)&&e.content.some(e=>"image"===e.type)),r=t.messages.some(e=>Array.isArray(e.content)&&e.content.some(e=>"thinking"===e.type));return e.setAttribute("copilot_api.translation.has_images",n),e.setAttribute("copilot_api.translation.has_thinking",r),an(t,v.models)}finally{e.end()}});c.record(performance.now()-a,{direction:"anthropic_to_openai"}),s=Mt(s,v.models,{enabled:v.thinkingEnabled}),Z.debug("Translated OpenAI request payload:",JSON.stringify(s)),e.set("requestData",{model:s.model,streaming:!!s.stream});let l=performance.now();v.manualApprove&&await vt();let u=await Wt(s),p=performance.now()-l;if(S.record(p,{model:s.model,target:"chat_completions",streaming:String(!!s.stream)}),Rn(u)){let t=e.get("requestData")||{};u.usage&&(t.tokenUsage=$t(u.usage)),t._d1=p,e.set("requestData",t),Z.debug("Non-streaming response:",JSON.stringify(u).slice(-400));let n=performance.now(),o=i.startActiveSpan("copilot_api.translate.openai_to_anthropic",e=>{try{e.setAttribute("copilot_api.translation.direction","openai_to_anthropic");let t=function(e){let t=[],n=[],o=[],r=null;r=e.choices[0]?.finish_reason??r;for(let a of e.choices){let e=_n(a.message),i=fn(a.message.content),s=gn(a.message.tool_calls);t.push(...e),n.push(...i),o.push(...s),("tool_calls"===a.finish_reason||"stop"===r)&&(r=a.finish_reason)}return{id:e.id,type:"message",role:"assistant",model:e.model,content:[...t,...n,...o],stop_reason:rn(r),stop_sequence:null,usage:{input_tokens:e.usage?.prompt_tokens??0,output_tokens:e.usage?.completion_tokens??0}}}(u);return e.setAttribute("copilot_api.translation.content_blocks",t.content.length),e.setAttribute("copilot_api.translation.stop_reason",t.stop_reason??"null"),e.setAttribute("copilot_api.translation.has_tool_use",t.content.some(e=>"tool_use"===e.type)),t}finally{e.end()}});return c.record(performance.now()-n,{direction:"openai_to_anthropic"}),Z.debug("Translated Anthropic response:",JSON.stringify(o)),e.json(o)}Z.debug("Streaming response from service");let{events:d,span:m,release:f}=u;return _e(e,async t=>{let n=null,o=0,r=performance.now(),a={messageStartSent:!1,contentBlockIndex:0,contentBlockOpen:!1,thinkingBlockOpen:!1,toolCalls:{}};try{try{for await(let r of d){if(Z.debug("Raw stream event:",JSON.stringify(r)),"[DONE]"===r.data)break;if(!r.data)continue;let i;try{i=JSON.parse(r.data)}catch(e){Z.warn("Failed to parse streaming chunk:",r.data.slice(0,200),e);continue}if(o++,g.add(1,{model:s.model,endpoint_type:"anthropic"}),i.usage){n=i.usage;let t=e.get("requestData")||{};t.tokenUsage=$t(n),t._d1=p,e.set("requestData",t)}let l=In(i,a);for(let e of l)Z.debug("Translated Anthropic event:",JSON.stringify(e)),await t.writeSSE({event:e.type,data:JSON.stringify(e)})}let i=performance.now()-r,l={model:s.model,endpoint_type:"anthropic"};if(V.record(i,l),Y.record(o,l),n){let t=e.get("requestData")||{};t.tokenUsage||(t.tokenUsage=$t(n),t._d1=p,e.set("requestData",t))}}finally{n&&St(m,n),m.end(),f()}let i=e.get("requestId");i&&H.executeCompletion(i)}finally{e.get("resolveStreamComplete")?.()}})}function Cn(e){if("response.completed"!==e.type&&"response.incomplete"!==e.type)return null;let t=e.response.usage;return t?{prompt_tokens:t.input_tokens,completion_tokens:t.output_tokens,total_tokens:t.total_tokens}:null}const Rn=e=>Object.hasOwn(e,"choices"),Bn=e=>Object.hasOwn(e,"output");const Dn=new me;Dn.post("/",async e=>{try{return await En(e)}catch(t){return await J(e,t)}});const Mn=new me;function Fn(e){return i.startActiveSpan("copilot_api.model.validate",t=>{try{t.setAttribute("copilot_api.model.requested",e.model);let n=jt(e.model);return t.setAttribute("copilot_api.model.normalized",n),n===e.model?(t.setAttribute("copilot_api.model.was_mapped",!1),e):(t.setAttribute("copilot_api.model.was_mapped",!0),C.add(1,{from_model:e.model,to_model:n}),Z.debug(`Normalized model from '${e.model}' to '${n}'`),{...e,model:n})}finally{t.end()}})}function Hn(e,t){let n=function(e){try{let t=JSON.parse(e);if(("response.completed"===t.type||"response.incomplete"===t.type)&&t.response?.usage){let e=t.response.usage;return{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}}}catch{}return null}(e);if(!n)return;t.usageRef.value=n;let o=t.c.get("requestData")||{};o.tokenUsage=$t(n),o._d1=t._d1,t.c.set("requestData",o)}Mn.get("/",async e=>{try{v.models||await ve();let t=v.models?.data.map(e=>({id:e.id,object:"model",type:"model",created:0,created_at:new Date(0).toISOString(),owned_by:e.vendor,display_name:e.name,context_length:e.capabilities.limits?.max_context_window_tokens}));return e.json({object:"list",data:t,has_more:!1})}catch(t){return await J(e,t)}});const Jn=new Set(["message","function_call","function_call_output"]),Un=new Set(["type","name","description","parameters"]);async function Ln(e){if(!e)return!1;let t=e.sources;if(t&&t.length>0)return!1;let n=e.query??e.queries?.[0];return!(!n||!v.tavilyApiKey)&&(Z.info(`Enriching web search: "${n}"`),e.sources=(await qn(v.tavilyApiKey,n,{maxResults:5})).map(e=>({type:"url",url:e.url,title:e.title,content:e.content})),!0)}async function Wn(e){if(!v.tavilyApiKey)return e;try{let t=JSON.parse(e);if("response.output_item.done"===t.type){let e=t.item;if("web_search_call"===e?.type&&await Ln(e.action))return JSON.stringify(t)}}catch(e){Z.error("Failed to enrich web search:",e)}return e}function zn(e,t){if("function_call"!==e.type||!t.has(e.name))return!1;if(e.type="custom_tool_call","string"==typeof e.arguments){try{e.input=JSON.parse(e.arguments).input??e.arguments}catch{e.input=e.arguments}delete e.arguments}return!0}function Kn(e,t){if(0===t.size)return e;try{let n=JSON.parse(e),o=n.type;if("response.output_item.added"===o||"response.output_item.done"===o){let e=n.item;if(e&&zn(e,t))return JSON.stringify(n)}if("response.completed"===o){let o=n.response?.output;if(!o)return e;let r=!1;for(let e of o)zn(e,t)&&(r=!0);if(r)return JSON.stringify(n)}}catch{}return e}function Gn(e){return null!=e}function Yn(e){let t=e.input,n="string"==typeof t?t:t.map(e=>function(e){switch(e.type){case"custom_tool_call":{let t=e.input;return{type:"function_call",call_id:e.call_id,name:e.name,arguments:JSON.stringify("string"==typeof t?{input:t}:t)}}case"custom_tool_call_output":return{type:"function_call_output",call_id:e.call_id,output:e.output};default:return Jn.has(e.type)||"web_search_call"===e.type?e:null}}(e)).filter(e=>null!==e),o={model:e.model,input:n};for(let t of["instructions","max_output_tokens","temperature","top_p","stream","store","tool_choice","parallel_tool_calls","user"])Gn(e[t])&&(o[t]=e[t]);let r=new Set;if(Gn(e.tools)){let t=function(e){let t=new Set;return{tools:e.filter(e=>{let t=e.type;return"function"===t||"custom"===t||"web_search"===t||t.startsWith("web_search_")}).map(e=>{let n=e.type;if("web_search"===n||n.startsWith("web_search_"))return{type:e.type};let o={};for(let t of Object.keys(e))Un.has(t)&&(o[t]=e[t]);return"custom"===o.type&&(o.type="function",t.add(o.name),o.parameters||={type:"object",properties:{input:{type:"string",description:"The freeform tool input content"}},required:["input"],additionalProperties:!1}),o}),customToolNames:t}}(e.tools);o.tools=t.tools,r=t.customToolNames}if(Gn(e.reasoning)&&"object"==typeof e.reasoning){let t=e.reasoning;o.reasoning={},Gn(t.effort)&&(o.reasoning.effort=t.effort),Gn(t.summary)&&(o.reasoning.summary=t.summary)}if(Gn(e.text)&&"object"==typeof e.text){let t=e.text;Gn(t.format)&&(o.text={format:t.format})}return{payload:o,customToolNames:r}}async function Vn(e,t,n){await async function(e){if(v.tavilyApiKey)try{for(let t of e.output){let e=t;"web_search_call"===e.type&&await Ln(e.action)}}catch(e){Z.error("Failed to enrich non-streaming web search:",e)}}(t);let o=e.get("requestData")||{};return t.usage&&(o.tokenUsage=$t({prompt_tokens:t.usage.input_tokens,completion_tokens:t.usage.output_tokens,total_tokens:t.usage.total_tokens})),o._d1=n,e.set("requestData",o),e.json(t)}const Qn=e=>Object.hasOwn(e,"output"),Xn=new me;Xn.post("/",async e=>{try{return await async function(e){await Pt(v);let{payload:t,customToolNames:n}=Yn(await e.req.json()),o=t;Z.debug(`Responses API [${Array.isArray(o.input)?o.input.length:1} items]`),n.size>0&&Z.debug(`Custom tools detected: ${[...n].join(", ")}`);try{o=Fn(o)}catch(t){if(t instanceof Nt)return e.json({error:{message:t.message,type:"invalid_request_error",code:"invalid_model"}},t.statusCode);throw t}e.set("requestData",{model:o.model,streaming:!!o.stream}),v.manualApprove&&await vt();let r=performance.now(),a=await on(o),i=performance.now()-r;if(S.record(i,{model:o.model,target:"responses",streaming:String(!!o.stream)}),Qn(a))return Vn(e,a,i);let{events:s,span:l,release:u}=a;return _e(e,async t=>{let r={value:null},a=0,c=performance.now();try{try{for await(let l of s)if(l.data&&"[DONE]"!==l.data){a++;let s=Kn(l.data,n);s=await Wn(s);let u=s===l.data?l:{...l,data:s};g.add(1,{model:o.model,endpoint_type:"responses"}),Hn(s,{usageRef:r,c:e,_d1:i}),await t.writeSSE(u)}else await t.writeSSE(l);let l=performance.now()-c,u={model:o.model,endpoint_type:"responses"};V.record(l,u),Y.record(a,u)}finally{r.value&&St(l,r.value),l.end(),u()}let p=e.get("requestId");p&&H.executeCompletion(p)}finally{e.get("resolveStreamComplete")?.()}})}(e)}catch(t){return await J(e,t)}});const Zn=new me;Zn.get("/",e=>{try{return e.json({token:v._t1})}catch(t){return Z.error("Error fetching token:",t),e.json({error:"Failed to fetch token",token:null},500)}});const eo=new me;eo.get("/",async e=>{try{let t=await Ee();return e.json(t)}catch(t){return Z.error("Error fetching usage:",t),e.json({error:"Failed to fetch usage"},500)}});const to=new me;to.use(async(e,t)=>{let n=Date.now(),o=`req_${Date.now()}_${Math.random().toString(36).slice(2,11)}`,r=e.req.method,a=e.req.path,s=function(e){return e.includes("/messages")?"anthropic":e.includes("/chat/completions")?"openai":e.includes("/embeddings")?"embeddings":e.includes("/models")?"models":e.includes("/usage")?"usage":e.includes("/token")?"token":"unknown"}(a);e.set("requestId",o),e.header("x-request-id",o);let l=i.startSpan("copilot_api.handle_request",{kind:ne.SERVER,attributes:{"http.method":r,"http.url":e.req.url,"http.route":a,"copilot_api.request_id":o,"copilot_api.endpoint_type":s}});for(let[t,n]of Object.entries(e.req.header())){let e=t.toLowerCase();B.has(e)||l.setAttribute(`http.request.header.${e}`,n)}let u,c=new Promise(e=>{u=e});e.set("streamComplete",c),e.set("resolveStreamComplete",u);let p=ae.setSpan(re.active(),l);await re.with(p,async()=>{H.registerCompletion(o,e,n);try{await t()}catch(e){let t=e instanceof Error?e:Error(String(e));throw l.setStatus({code:oe.ERROR,message:t.message}),l.recordException(t),e}});let d=e.get("requestData"),m={c:e,start:n,method:r,path:a,endpointType:s,requestId:o,span:l};d?.streaming?c.then(()=>xt(m)).catch(e=>{Z.debug("Error finalizing streaming request:",e),l.end()}):xt(m)}),to.use(fe()),to.get("/",e=>e.text("Server running")),to.route("/chat/completions",Vt),to.route("/models",Mn),to.route("/embeddings",Qt),to.route("/usage",eo),to.route("/token",Zn),to.route("/v1/chat/completions",Vt),to.route("/v1/models",Mn),to.route("/v1/embeddings",Qt),to.route("/v1/messages",Dn),to.post("/v1/messages/count_tokens",async function(e){let t;try{t=await e.req.json()}catch{return e.json({error:{type:"invalid_request_error",message:"Invalid JSON"}},400)}if(!Array.isArray(t.messages))return e.json({error:{type:"invalid_request_error",message:"messages is required"}},400);let n=Zt.baseOverhead;if(t.system){let e="string"==typeof t.system?t.system:t.system.map(e=>e.text).join("\n\n");n+=Zt.perMessage+en(e)*Zt.contentMultiplier}for(let e of t.messages)n+=Zt.perMessage,n+=en(e.role),"string"==typeof e.content?n+=en(e.content)*Zt.contentMultiplier:Array.isArray(e.content)&&(n+=tn(e.content));return t.tools&&t.tools.length>0&&(n+=function(e){let t=Zt.toolsExist;for(let[n,o]of e.entries())n>0&&(t+=Zt.perTool),t+=en(o.name),o.description&&(t+=Zt.perDesc+en(o.description)),t+=nn(o.input_schema);return t}(t.tools)),e.json({input_tokens:Math.round(n)})}),to.route("/v1/responses",Xn),to.route("/responses",Xn);const no=[],oo=Date.now();let ro="";function ao(){let e=async(e,t=0)=>{let n=i.startSpan("copilot_api.server.shutdown",{kind:ne.INTERNAL,attributes:{"copilot_api.shutdown.reason":e,"copilot_api.shutdown.exit_code":t,"copilot_api.uptime_seconds":(Date.now()-oo)/1e3}});for(let t of no)try{await t()}catch(e){Z.error("Error during cleanup:",e)}n.end(),function(e,t){let n=dt("https://barazany.dev?utm_source=copilot-cli&utm_medium=terminal&utm_campaign=goodbye_banner","barazany.dev"),o=["",ft,` ${it("COPILOT-API")} ${st(`v${e}`)} ${st("·")} ${wt(t)} uptime`,ft,""," Check in from time to time on",` ${lt(n)}`,"",ft,""];console.log(o.join("\n"))}(ro,Date.now()-oo),ue.exit(t)};ue.on("SIGINT",()=>e("SIGINT")),ue.on("SIGTERM",()=>e("SIGTERM")),ue.on("uncaughtException",t=>{Z.error("Uncaught exception:",t),e("uncaughtException",1)}),ue.on("unhandledRejection",(t,n)=>{Z.error("Unhandled promise rejection at:",n,"reason:",t),e("unhandledRejection",1)})}function io(e){let{options:t,version:o,runtime:r,_u1:a}=e,s=i.startSpan("copilot_api.server.startup",{kind:ne.INTERNAL,attributes:n({config:{version:o,port:t.port,accountType:t.accountType,verbose:t.verbose,manualApprove:t.manual,rateLimitSeconds:t.rateLimit??0,rateLimitWait:t.rateLimitWait,timeoutMs:t.timeout,showToken:t.showToken,_cc1:t._cc1,model:t.model,smallModel:t.smallModel,_t3:t._t2?"cli_arg":"stored",enableThinking:t.enableThinking},runtime:r,identity:{_u1:a.login,machineId:v.machineId,sessionId:v.sessionId,_v1:v._v1},connectivity:v.connectivity,modelsCount:v.models?.data.length??0,startupDurationMs:Date.now()-oo},"copilot_api")});if(v.models?.data)for(let e of v.models.data)s.addEvent("copilot_api.model.available",n(e,"model"));s.end(),G(j.INFO,"INFO","Server started",n({version:o,port:t.port,accountType:t.accountType,_u1:a.login,modelsCount:v.models?.data.length??0,startupDurationMs:Date.now()-oo},"copilot_api"))}async function so(e){ao(),no.push(()=>{Z.debug("Cleaning up connectivity monitor"),F.stop()},()=>{Z.debug("Cleaning up token management"),Z.debug("Cleaning up token management"),Te&&=void clearInterval(Te),$e&&=void F.off("online",$e),Se&&=void F.off("offline",Se)},async()=>{Z.debug("Cleaning up HTTP/2 client"),await async function(){Z.debug("Stopping HTTP/2 pool"),Qe&&=void clearInterval(Qe),Xe&&=void clearInterval(Xe);let e=[...Ge.keys(),...Ye];Ge.clear(),Ye.clear(),Ke.clear(),Ve=void 0;for(let t of e)Ze(t)&&(K.add(1,{event:"closed"}),await t.close())}()},async()=>{Z.debug("Shutting down OTel SDK"),await p()}),e.verbose&&(Z.level=5,Z.info("Verbose logging enabled")),e._cc1&&e.codex&&(Z.error("Cannot use --claude-code and --codex together"),ue.exit(1)),v.accountType=e.accountType,v.manualApprove=e.manual,v.rateLimitSeconds=e.rateLimit,v.rateLimitWait=e.rateLimitWait,v.showToken=e.showToken,v.timeoutMs=e.timeout,v.thinkingEnabled=e.enableThinking,e.tavilyApiKey&&(v.tavilyApiKey=e.tavilyApiKey),function(){let e="1"===ue.env.COPILOT_H2_KEEPALIVE||"true"===ue.env.COPILOT_H2_KEEPALIVE,t=e||"1"===ue.env.COPILOT_H2||"true"===ue.env.COPILOT_H2;v.connectivity.h2=t,v.connectivity.h2Keepalive=e}(),await A(),v._v1="1.116.0-insider";let t=await Re();ro=t;let n,o=Be();r(t,oo),e._t2?(v._t2=e._t2,Z.info("Using provided Auth token")):await qe();try{let{_s4:e}=await import("./get-user-O49GxhnU.mjs");n=await e(),await I(v,n.login)}catch(e){Z.error("Failed to get user info for machine ID generation:",e),Z.error("Cannot proceed without user information"),ue.exit(1)}await Ie(),await ve();let a=function(e=18e5){let t=setInterval(()=>{ve().then(()=>Z.debug("Model list refreshed successfully")).catch(e=>{Z.warn("Failed to refresh model list; keeping existing cache:",e)})},e);return t.unref(),()=>{clearInterval(t)}}();no.push(()=>{Z.debug("Cleaning up model refresh interval"),a()}),v.connectivity.h2&&(nt(tt(O(v))),v.connectivity.h2Keepalive&&rt()),io({options:e,version:t,runtime:o,_u1:n});let i=`http://localhost:${e.port}`,s=await async function(e,t){if(!e._cc1)return!1;let n,o;if(de(v.models,"Models should be loaded by now"),e.model&&e.smallModel){let t=v.models.data.map(e=>e.id);t.includes(e.model)||(Z.error(`Invalid model: ${e.model}`),Z.info(`Available services: \n${t.join("\n")}`),ue.exit(1)),t.includes(e.smallModel)||(Z.error(`Invalid small model: ${e.smallModel}`),Z.info(`Available services: \n${t.join("\n")}`),ue.exit(1)),n=e.model,o=e.smallModel}else e.model||e.smallModel?(Z.error("Both --model and --small-model must be specified for model selection"),ue.exit(1)):(n=await Z.prompt("Select a model to use",{type:"select",options:v.models.data.map(e=>e.id)}),o=await Z.prompt("Select a small model to use",{type:"select",options:v.models.data.map(e=>e.id)}));let r=at({ANTHROPIC_BASE_URL:t,ANTHROPIC_AUTH_TOKEN:"dummy",ANTHROPIC_MODEL:n,ANTHROPIC_SMALL_FAST_MODEL:o},"claude");try{return ie.writeSync(r),!0}catch{return Z.warn("Failed to copy to clipboard. Here is the command:"),Z.log(r),!1}}(e,i)||await async function(e,t){if(!e.codex)return!1;let n;if(de(v.models,"Models should be loaded by now"),e.model){let t=v.models.data.map(e=>e.id);t.includes(e.model)||(Z.error(`Invalid model: ${e.model}`),Z.info(`Available services: \n${t.join("\n")}`),ue.exit(1)),n=e.model}else n=await Z.prompt("Select a model to use with Codex CLI",{type:"select",options:v.models.data.map(e=>e.id)});let o=at({OPENAI_BASE_URL:t,OPENAI_API_KEY:"dummy"},`codex --model ${n}`);try{return ie.writeSync(o),!0}catch{return Z.warn("Failed to copy to clipboard. Here is the Codex CLI command:"),Z.log(o),!1}}(e,i);(function(e){let t=[...yt(e),...kt(e),...bt(e),"",...ht(gt),""];console.log(t.join("\n"))})({version:t,runtime:o,user:n.login,accountType:e.accountType,port:e.port,timeout:e.timeout,webSearch:!!e.tavilyApiKey,model:e.model,smallModel:e.smallModel,clipboardCopied:s}),pe({fetch:to.fetch,port:e.port,bun:{idleTimeout:255}})}const lo=Q({meta:{name:"start",description:"Start the API server"},args:{port:{alias:"p",type:"string",default:"4141",description:"Port to listen on"},verbose:{alias:"v",type:"boolean",default:!1,description:"Enable verbose logging"},"account-type":{alias:"a",type:"string",default:"individual",description:"Account type (individual, business, enterprise)"},manual:{type:"boolean",default:!1,description:"Enable manual request approval"},"rate-limit":{alias:"r",type:"string",description:"Rate limit in seconds between requests"},wait:{alias:"w",type:"boolean",default:!1,description:"Wait instead of error when rate limit is hit. Has no effect if rate limit is not set"},"github-token":{alias:"g",type:"string",description:"Provide auth token directly (must be generated using the `auth` subcommand)"},"claude-code":{alias:"c",type:"boolean",default:!1,description:"Generate a command to launch with API config"},codex:{type:"boolean",default:!1,description:"Generate a command to launch Codex CLI with Copilot API config"},model:{alias:"m",type:"string",description:"Model to use (with --claude-code or --codex)"},"small-model":{alias:"s",type:"string",description:"Small/fast model to use (requires --claude-code)"},"show-token":{type:"boolean",default:!1,description:"Show tokens on fetch and refresh"},timeout:{alias:"t",type:"string",description:"API timeout in milliseconds (default: 600000)"},"tavily-api-key":{type:"string",description:"Tavily API key for web search support (or set TAVILY_API_KEY env var)"},"enable-thinking":{type:"boolean",default:!1,description:"Enable forwarding of thinking_budget / Anthropic thinking config to upstream. Off by default to avoid accidentally triggering reasoning behaviour."}},run({args:e}){let t=e["rate-limit"],n=void 0===t?void 0:Number.parseInt(t,10),o=e.timeout,r=void 0===o?u:Number.parseInt(o,10);return so({port:Number.parseInt(e.port,10),verbose:e.verbose,accountType:e["account-type"],manual:e.manual,rateLimit:n,rateLimitWait:e.wait,_t2:e["github-token"],_cc1:e["claude-code"],codex:e.codex,model:e.model,smallModel:e["small-model"],showToken:e["show-token"],timeout:r,tavilyApiKey:e["tavily-api-key"]||ue.env.TAVILY_API_KEY,enableThinking:e["enable-thinking"]})}});await async function(){try{if("1"===process.env.COPILOT_API_SKIP_UPDATE_CHECK)return;let[e,t]=await Promise.all([Re(),We()]);if("unknown"===e||!t||!function(e,t){let n=e.split(".").map(Number),o=t.split(".").map(Number),r=Math.max(n.length,o.length);for(let e=0;e<r;e++){let t=n[e]??0,r=o[e]??0;if(t<r)return!0;if(t>r)return!1}return!1}(e,t))return;!function(e,t){let n=`npx ${He}@latest`,o=Le("│"),r=` ${Ue("Update available!")} ${(e=>`${e}`)(e)} → ${(e=>`${e}`)(t)}`,a=` Run ${Ue(n)} to update`,i=" ".repeat(54),s=Le(`╭${"─".repeat(54)}╮`),l=Le(`╰${"─".repeat(54)}╯`),u=["",s,`${o}${i}${o}`,`${o}${ze(r,54)}${o}`,`${o}${i}${o}`,`${o}${ze(a,54)}${o}`,`${o}${i}${o}`,l,""];console.log(u.join("\n"))}(e,t)}catch{}}(),await X(Q({meta:{name:"copilot-api",description:"API proxy server for tool integration."},subCommands:{auth:Pe,start:lo,"check-usage":Ce,debug:Fe}}));export{};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "copilot-api-node20",
3
- "version": "0.21.1",
3
+ "version": "0.21.3",
4
4
  "description": "Turn GitHub Copilot into OpenAI/Anthropic API compatible server. Usable with Claude Code! (Node v20+ fork)",
5
5
  "keywords": [
6
6
  "proxy",
@@ -35,7 +35,7 @@
35
35
  "start": "NODE_ENV=production bun run ./src/main.ts",
36
36
  "test": "bun test",
37
37
  "test:coverage": "bun test --coverage",
38
- "test:e2e": "E2E=1 bun test tests/e2e.test.ts",
38
+ "test:e2e": "E2E=1 bun test tests/e2e.test.ts tests/e2e-thinking.test.ts",
39
39
  "test:watch": "bun test --watch",
40
40
  "typecheck": "tsc"
41
41
  },