@whoz-oss/coday-server 0.44.0 → 0.46.0

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/package.json +1 -1
  2. package/server.js +3 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whoz-oss/coday-server",
3
- "version": "0.44.0",
3
+ "version": "0.46.0",
4
4
  "repository": "https://github.com/whoz-oss/coday",
5
5
  "type": "module",
6
6
  "main": "server/server.js",
package/server.js CHANGED
@@ -825,7 +825,7 @@ you can set the \`dangerouslyAllowBrowser\` option to \`true\`, e.g.,
825
825
  new OpenAI({ apiKey, dangerouslyAllowBrowser: true });
826
826
 
827
827
  https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety
828
- `);this.baseURL=o.baseURL,this.timeout=o.timeout??rO.DEFAULT_TIMEOUT,this.logger=o.logger??console;let c="warn";this.logLevel=c,this.logLevel=OP(o.logLevel,"ClientOptions.logLevel",this)??OP(Ql("OPENAI_LOG"),"process.env['OPENAI_LOG']",this)??c,this.fetchOptions=o.fetchOptions,this.maxRetries=o.maxRetries??2,this.fetch=o.fetch??qee(),at(this,Cy,Bee,"f"),this._options=o,this.apiKey=typeof t=="string"?t:"Missing Key",this.organization=n,this.project=i,this.webhookSecret=a}withOptions(e){return new this.constructor({...this._options,baseURL:this.baseURL,maxRetries:this.maxRetries,timeout:this.timeout,logger:this.logger,logLevel:this.logLevel,fetch:this.fetch,fetchOptions:this.fetchOptions,apiKey:this.apiKey,organization:this.organization,project:this.project,webhookSecret:this.webhookSecret,...e})}defaultQuery(){return this._options.defaultQuery}validateHeaders({values:e,nulls:t}){}async authHeaders(e){return Re([{Authorization:`Bearer ${this.apiKey}`}])}stringifyQuery(e){return DP(e,{arrayFormat:"brackets"})}getUserAgent(){return`${this.constructor.name}/JS ${Gl}`}defaultIdempotencyKey(){return`stainless-node-retry-${yP()}`}makeStatusError(e,t,n,i){return Sn.generate(e,t,n,i)}async _callApiKey(){let e=this._options.apiKey;if(typeof e!="function")return!1;let t;try{t=await e()}catch(n){throw n instanceof We?n:new We(`Failed to get token from 'apiKey' function: ${n.message}`,{cause:n})}if(typeof t!="string"||!t)throw new We(`Expected 'apiKey' function argument to return a string but it returned ${t}`);return this.apiKey=t,!0}buildURL(e,t,n){let i=!me(this,tO,"m",Dte).call(this)&&n||this.baseURL,a=Tee(e)?new URL(e):new URL(i+(i.endsWith("/")&&e.startsWith("/")?e.slice(1):e)),s=this.defaultQuery();return Dee(s)||(t={...s,...t}),typeof t=="object"&&t&&!Array.isArray(t)&&(a.search=this.stringifyQuery(t)),a.toString()}async prepareOptions(e){await this._callApiKey()}async prepareRequest(e,{url:t,options:n}){}get(e,t){return this.methodRequest("get",e,t)}post(e,t){return this.methodRequest("post",e,t)}patch(e,t){return this.methodRequest("patch",e,t)}put(e,t){return this.methodRequest("put",e,t)}delete(e,t){return this.methodRequest("delete",e,t)}methodRequest(e,t,n){return this.request(Promise.resolve(n).then(i=>({method:e,path:t,...i})))}request(e,t=null){return new Lu(this,this.makeRequest(e,t,void 0))}async makeRequest(e,t,n){let i=await e,a=i.maxRetries??this.maxRetries;t==null&&(t=a),await this.prepareOptions(i);let{req:s,url:o,timeout:c}=await this.buildRequest(i,{retryCount:a-t});await this.prepareRequest(s,{url:o,options:i});let l="log_"+(Math.random()*(1<<24)|0).toString(16).padStart(6,"0"),u=n===void 0?"":`, retryOf: ${n}`,f=Date.now();if(an(this).debug(`[${l}] sending request`,fc({retryOfRequestLogID:n,method:i.method,url:o,options:i,headers:s.headers})),i.signal?.aborted)throw new mn;let d=new AbortController,p=await this.fetchWithTimeout(o,s,c,d).catch($4),m=Date.now();if(p instanceof globalThis.Error){let v=`retrying, ${t} attempts remaining`;if(i.signal?.aborted)throw new mn;let b=B4(p)||/timed? ?out/i.test(String(p)+("cause"in p?String(p.cause):""));if(t)return an(this).info(`[${l}] connection ${b?"timed out":"failed"} - ${v}`),an(this).debug(`[${l}] connection ${b?"timed out":"failed"} (${v})`,fc({retryOfRequestLogID:n,url:o,durationMs:m-f,message:p.message})),this.retryRequest(i,t,n??l);throw an(this).info(`[${l}] connection ${b?"timed out":"failed"} - error; no more retries left`),an(this).debug(`[${l}] connection ${b?"timed out":"failed"} (error; no more retries left)`,fc({retryOfRequestLogID:n,url:o,durationMs:m-f,message:p.message})),b?new Wl:new zl({cause:p})}let h=[...p.headers.entries()].filter(([v])=>v==="x-request-id").map(([v,b])=>", "+v+": "+JSON.stringify(b)).join(""),g=`[${l}${u}${h}] ${s.method} ${o} ${p.ok?"succeeded":"failed"} with status ${p.status} in ${m-f}ms`;if(!p.ok){let v=await this.shouldRetry(p);if(t&&v){let R=`retrying, ${t} attempts remaining`;return await Lee(p.body),an(this).info(`${g} - ${R}`),an(this).debug(`[${l}] response error (${R})`,fc({retryOfRequestLogID:n,url:p.url,status:p.status,headers:p.headers,durationMs:m-f})),this.retryRequest(i,t,n??l,p.headers)}let b=v?"error; no more retries left":"error; not retryable";an(this).info(`${g} - ${b}`);let w=await p.text().catch(R=>$4(R).message),C=Iee(w),A=C?void 0:w;throw an(this).debug(`[${l}] response error (${b})`,fc({retryOfRequestLogID:n,url:p.url,status:p.status,headers:p.headers,message:A,durationMs:Date.now()-f})),this.makeStatusError(p.status,C,A,p.headers)}return an(this).info(g),an(this).debug(`[${l}] response start`,fc({retryOfRequestLogID:n,url:p.url,status:p.status,headers:p.headers,durationMs:m-f})),{response:p,options:i,controller:d,requestLogID:l,retryOfRequestLogID:n,startTime:f}}getAPIList(e,t,n){return this.requestAPIList(t,{method:"get",path:e,...n})}requestAPIList(e,t){let n=this.makeRequest(t,null,void 0);return new V4(this,n,e)}async fetchWithTimeout(e,t,n,i){let{signal:a,method:s,...o}=t||{};a&&a.addEventListener("abort",()=>i.abort());let c=setTimeout(()=>i.abort(),n),l=globalThis.ReadableStream&&o.body instanceof globalThis.ReadableStream||typeof o.body=="object"&&o.body!==null&&Symbol.asyncIterator in o.body,u={signal:i.signal,...l?{duplex:"half"}:{},method:"GET",...o};s&&(u.method=s.toUpperCase());try{return await this.fetch.call(void 0,e,u)}finally{clearTimeout(c)}}async shouldRetry(e){let t=e.headers.get("x-should-retry");return t==="true"?!0:t==="false"?!1:e.status===408||e.status===409||e.status===429||e.status>=500}async retryRequest(e,t,n,i){let a,s=i?.get("retry-after-ms");if(s){let c=parseFloat(s);Number.isNaN(c)||(a=c)}let o=i?.get("retry-after");if(o&&!a){let c=parseFloat(o);Number.isNaN(c)?a=Date.parse(o)-Date.now():a=c*1e3}if(!(a&&0<=a&&a<60*1e3)){let c=e.maxRetries??this.maxRetries;a=this.calculateDefaultRetryTimeoutMillis(t,c)}return await yo(a),this.makeRequest(e,t-1,n)}calculateDefaultRetryTimeoutMillis(e,t){let a=t-e,s=Math.min(.5*Math.pow(2,a),8),o=1-Math.random()*.25;return s*o*1e3}async buildRequest(e,{retryCount:t=0}={}){let n={...e},{method:i,path:a,query:s,defaultBaseURL:o}=n,c=this.buildURL(a,s,o);"timeout"in n&&Oee("timeout",n.timeout),n.timeout=n.timeout??this.timeout;let{bodyHeaders:l,body:u}=this.buildBody({options:n}),f=await this.buildHeaders({options:e,method:i,bodyHeaders:l,retryCount:t});return{req:{method:i,headers:f,...n.signal&&{signal:n.signal},...globalThis.ReadableStream&&u instanceof globalThis.ReadableStream&&{duplex:"half"},...u&&{body:u},...this.fetchOptions??{},...n.fetchOptions??{}},url:c,timeout:n.timeout}}async buildHeaders({options:e,method:t,bodyHeaders:n,retryCount:i}){let a={};this.idempotencyHeader&&t!=="get"&&(e.idempotencyKey||(e.idempotencyKey=this.defaultIdempotencyKey()),a[this.idempotencyHeader]=e.idempotencyKey);let s=Re([a,{Accept:"application/json","User-Agent":this.getUserAgent(),"X-Stainless-Retry-Count":String(i),...e.timeout?{"X-Stainless-Timeout":String(Math.trunc(e.timeout/1e3))}:{},...Nee(),"OpenAI-Organization":this.organization,"OpenAI-Project":this.project},await this.authHeaders(e),this._options.defaultHeaders,n,e.headers]);return this.validateHeaders(s),s.values}buildBody({options:{body:e,headers:t}}){if(!e)return{bodyHeaders:void 0,body:void 0};let n=Re([t]);return ArrayBuffer.isView(e)||e instanceof ArrayBuffer||e instanceof DataView||typeof e=="string"&&n.values.has("content-type")||globalThis.Blob&&e instanceof globalThis.Blob||e instanceof FormData||e instanceof URLSearchParams||globalThis.ReadableStream&&e instanceof globalThis.ReadableStream?{bodyHeaders:void 0,body:e}:typeof e=="object"&&(Symbol.asyncIterator in e||Symbol.iterator in e&&"next"in e&&typeof e.next=="function")?{bodyHeaders:void 0,body:Xb(e)}:me(this,Cy,"f").call(this,{body:e,headers:n})}};rO=Ct,Cy=new WeakMap,tO=new WeakSet,Dte=function(){return this.baseURL!=="https://api.openai.com/v1"};Ct.OpenAI=rO;Ct.DEFAULT_TIMEOUT=6e5;Ct.OpenAIError=We;Ct.APIError=Sn;Ct.APIConnectionError=zl;Ct.APIConnectionTimeoutError=Wl;Ct.APIUserAbortError=mn;Ct.NotFoundError=yp;Ct.ConflictError=xp;Ct.RateLimitError=_p;Ct.BadRequestError=gp;Ct.AuthenticationError=vp;Ct.InternalServerError=Ep;Ct.PermissionDeniedError=bp;Ct.UnprocessableEntityError=wp;Ct.InvalidWebhookSignatureError=bo;Ct.toFile=sy;Ct.Completions=Ku;Ct.Chat=Jl;Ct.Embeddings=Yu;Ct.Files=Qu;Ct.Images=r3;Ct.Audio=Eo;Ct.Moderations=i3;Ct.Models=n3;Ct.FineTuning=Ns;Ct.Graders=n2;Ct.VectorStores=bc;Ct.Webhooks=s3;Ct.Beta=Ms;Ct.Batches=Hu;Ct.Uploads=s2;Ct.Responses=a2;Ct.Realtime=vc;Ct.Conversations=t2;Ct.Evals=r2;Ct.Containers=e2;Ct.Videos=a3;var nO=Tr(As(),1),aHe=[{name:"gpt-4.1-2025-04-14",contextWindow:1e6,alias:"BIG",price:{inputMTokens:2,cacheRead:.5,outputMTokens:8}},{name:"gpt-4o-mini",alias:"SMALL",contextWindow:128e3,price:{inputMTokens:.15,cacheRead:.075,outputMTokens:.6}}],ad=class r extends qf{constructor(t,n,i){super(n,i);this.interactor=t;this.mergeModels(aHe),n.name.toLowerCase()!=="openai"&&(this.models=n.models??[]),this.name=n.name}name;static MAX_TOOLS=128;truncateToolsIfNeeded(t){return t.length<=r.MAX_TOOLS?t:(this.interactor.warn(`\u26A0\uFE0F OpenAI limits tools to ${r.MAX_TOOLS} maximum. Your agent has ${t.length} tools. Truncating to first ${r.MAX_TOOLS} tools. Consider reducing your integrations or using a shorter tool list for better performance.`),t.slice(0,r.MAX_TOOLS))}async run(t,n){n.resetUsageForRun();let i=this.isOpenaiReady();if(!i)return this.returnError("Client not ready");let a=this.getModel(t);if(!a)return this.returnError(`Model not found for agent ${t.name}`);if(t.definition.openaiAssistantId)return this.runAssistant(t,a,n);let s=new nO.Subject,o=this.startThinkingInterval();return this.processThread(i,t,a,n,s).catch(c=>{s.next(new Wi({error:c}))}).finally(()=>{this.stopThinkingInterval(o),this.showAgentAndUsage(t,this.aiProviderConfig.name,a.name,n);let c=n.usage?.price||0;this.logAgentUsage(t,a.name,c),s.complete()}),s}async runAssistant(t,n,i){let a=this.isOpenaiReady();if(!a)return this.returnError("Client not ready");let s=new nO.Subject;i.data.openai={price:i.data?.openai?.price??0,runPrice:0,assistantThreadData:i.data?.openai?.assistantThreadData??{}};let o=n.contextWindow*this.charsPerToken-t.systemInstructions.length-t.tools.charLength,c=await this.getMessages(i,o,n.name);c.compacted&&(i.data.openai.assistantThreadData={});let l=i.data.openai.assistantThreadData,u=this.startThinkingInterval();if(!l.threadId){let m=await a.beta.threads.create();this.interactor.displayText("Assistant thread created"),l.threadId=m.id}let f=c.messages,d=l.lastTimestamp?f.findIndex(m=>m.timestamp>=l.lastTimestamp):-1,p=f.slice(d+1);return this.updateAssistantThread(a,i,p).then(async()=>await this.processAssistantThread(a,t,n,i,s)).finally(()=>{this.stopThinkingInterval(u),this.showAgentAndUsage(t,this.aiProviderConfig.name,n.name,i);let m=i.usage?.price||0;this.logAgentUsage(t,n.name,m),s.complete()}),s}async processAssistantThread(t,n,i,a,s){let o=t.beta.threads.runs.stream(a.data.openai.assistantThreadData.threadId,{assistant_id:n.definition.openaiAssistantId,tools:this.truncateToolsIfNeeded([...n.tools.getTools(),{type:"file_search"}]),tool_choice:"auto",max_completion_tokens:12e4,max_prompt_tokens:12e4,parallel_tool_calls:!1});await this.processAssistantStream(o,n,i,t,a,s)}async processThread(t,n,i,a,s){try{let o=n.systemInstructions.length+n.tools.charLength+20,c=i.contextWindow*this.charsPerToken-o,l=await this.getMessages(a,c,i.name),u=await t.chat.completions.create({model:i.name,messages:this.toOpenAiMessage(n,l.messages),tools:this.truncateToolsIfNeeded(n.tools.getTools()),max_completion_tokens:void 0,temperature:n.definition.temperature??.8});this.updateUsage(u.usage,n,i,a);let f=u.choices[0];if(f.finish_reason==="length")throw new Error("Max tokens reached for Openai \u{1F62C}");let d=f.message.content?.trim();this.handleText(a,d,n,s);let p=f.message?.tool_calls?.filter(m=>m.type==="function").map(m=>new fn({toolRequestId:m.id,name:m.function.name,args:m.function.arguments}));await this.shouldProcessAgainAfterResponse(d,p,n,a)&&await this.processThread(t,n,i,a,s)}catch(o){this.handleError(o,s,this.aiProviderConfig.name)}}updateUsage(t,n,i,a){let s=t?.prompt_tokens_details?.cached_tokens??0,o=(t?.prompt_tokens??0)-s,c=o*(i?.price?.inputMTokens??0),l=t?.completion_tokens??0,u=l*(i?.price?.outputMTokens??0),f=s*(i?.price?.cacheRead??0),d=(c+u+f)/1e6;a.addUsage({input:o,output:l,cache_read:s,cache_write:0,price:d})}isOpenaiReady(){if(!this.aiProviderConfig.apiKey){this.interactor.warn(`${this.aiProviderConfig.name}_API_KEY not set, skipping AI command. Please configure your API key.`);return}try{return new Ct({apiKey:this.aiProviderConfig.apiKey,baseURL:this.aiProviderConfig.url})}catch(t){let n=t instanceof Error?t.message:"Unknown error";this.interactor.warn(`Failed to initialize ${this.aiProviderConfig.name} client: ${n}`),console.error(`${this.aiProviderConfig.name} client initialization error:`,t);return}}toOpenAiMessage(t,n){let i={content:t.systemInstructions,role:"system"},a=n.flatMap((s,o)=>{if(s instanceof Gi)return[{role:"user",content:s.summary}];if(s instanceof lr){let c=s.role==="user"&&o===n.length-1,u=this.enhanceWithCurrentDateTime(s.content,c).map(f=>{if(f.type==="text")return{type:"text",text:f.content};if(f.type==="image"){let d={type:"image_url",image_url:{url:`data:${f.mimeType};base64,${f.content}`,detail:"auto"}};return console.log("got an image in message event"),d}throw new Error(`Unknown content type: ${f.type}`)});return s.role==="assistant"?[{role:"assistant",content:typeof u=="string"?u:u.map(f=>f.type==="text"?f.text:"[Image]").join(" "),name:t.name}]:[{role:"user",content:u,name:s.name}]}if(s instanceof fn)return[{role:"assistant",name:t.name,tool_calls:[{type:"function",id:s.toolRequestId,function:{name:s.name,arguments:s.args}}]}];if(s instanceof pn){if(typeof s.output=="string")return[{role:"tool",content:s.output,tool_call_id:s.toolRequestId}];{let c=s.output;if(c.type==="image")return[{role:"tool",content:"Image retrieved successfully. See following message.",tool_call_id:s.toolRequestId},{role:"user",content:[{type:"image_url",image_url:{url:`data:${c.mimeType};base64,${c.content}`,detail:"auto"}}],name:"system"}];if(c.type==="text")return[{role:"tool",content:c.content,tool_call_id:s.toolRequestId}];throw new Error(`Unknown content type: ${c.type}`)}}return[]});return[i,...a]}async updateAssistantThread(t,n,i){try{this.interactor.displayText(`${i.length} messages to upload to assistant thread...`);for(let a of i){if(!this.shouldProceed(n))throw new Error("Assistant thread update interrupted");let s=this.toAssistantMessage(a);await t.beta.threads.messages.create(n.data.openai.assistantThreadData.threadId,s),n.data.openai.assistantThreadData.lastTimestamp=a.timestamp}this.interactor.displayText("Messages uploaded.")}catch(a){console.error("Error updating assistant thread:",a);let s=a instanceof Error?a.message:"Unknown error";throw this.interactor.displayText(`\u26A0\uFE0F Error updating assistant thread: ${s}`),a}}toAssistantMessage(t){if(t instanceof Gi)return{role:"user",content:t.summary};if(t instanceof lr){let n=t.content.filter(i=>i.type==="text").map(i=>i.content).join(`
828
+ `);this.baseURL=o.baseURL,this.timeout=o.timeout??rO.DEFAULT_TIMEOUT,this.logger=o.logger??console;let c="warn";this.logLevel=c,this.logLevel=OP(o.logLevel,"ClientOptions.logLevel",this)??OP(Ql("OPENAI_LOG"),"process.env['OPENAI_LOG']",this)??c,this.fetchOptions=o.fetchOptions,this.maxRetries=o.maxRetries??2,this.fetch=o.fetch??qee(),at(this,Cy,Bee,"f"),this._options=o,this.apiKey=typeof t=="string"?t:"Missing Key",this.organization=n,this.project=i,this.webhookSecret=a}withOptions(e){return new this.constructor({...this._options,baseURL:this.baseURL,maxRetries:this.maxRetries,timeout:this.timeout,logger:this.logger,logLevel:this.logLevel,fetch:this.fetch,fetchOptions:this.fetchOptions,apiKey:this.apiKey,organization:this.organization,project:this.project,webhookSecret:this.webhookSecret,...e})}defaultQuery(){return this._options.defaultQuery}validateHeaders({values:e,nulls:t}){}async authHeaders(e){return Re([{Authorization:`Bearer ${this.apiKey}`}])}stringifyQuery(e){return DP(e,{arrayFormat:"brackets"})}getUserAgent(){return`${this.constructor.name}/JS ${Gl}`}defaultIdempotencyKey(){return`stainless-node-retry-${yP()}`}makeStatusError(e,t,n,i){return Sn.generate(e,t,n,i)}async _callApiKey(){let e=this._options.apiKey;if(typeof e!="function")return!1;let t;try{t=await e()}catch(n){throw n instanceof We?n:new We(`Failed to get token from 'apiKey' function: ${n.message}`,{cause:n})}if(typeof t!="string"||!t)throw new We(`Expected 'apiKey' function argument to return a string but it returned ${t}`);return this.apiKey=t,!0}buildURL(e,t,n){let i=!me(this,tO,"m",Dte).call(this)&&n||this.baseURL,a=Tee(e)?new URL(e):new URL(i+(i.endsWith("/")&&e.startsWith("/")?e.slice(1):e)),s=this.defaultQuery();return Dee(s)||(t={...s,...t}),typeof t=="object"&&t&&!Array.isArray(t)&&(a.search=this.stringifyQuery(t)),a.toString()}async prepareOptions(e){await this._callApiKey()}async prepareRequest(e,{url:t,options:n}){}get(e,t){return this.methodRequest("get",e,t)}post(e,t){return this.methodRequest("post",e,t)}patch(e,t){return this.methodRequest("patch",e,t)}put(e,t){return this.methodRequest("put",e,t)}delete(e,t){return this.methodRequest("delete",e,t)}methodRequest(e,t,n){return this.request(Promise.resolve(n).then(i=>({method:e,path:t,...i})))}request(e,t=null){return new Lu(this,this.makeRequest(e,t,void 0))}async makeRequest(e,t,n){let i=await e,a=i.maxRetries??this.maxRetries;t==null&&(t=a),await this.prepareOptions(i);let{req:s,url:o,timeout:c}=await this.buildRequest(i,{retryCount:a-t});await this.prepareRequest(s,{url:o,options:i});let l="log_"+(Math.random()*(1<<24)|0).toString(16).padStart(6,"0"),u=n===void 0?"":`, retryOf: ${n}`,f=Date.now();if(an(this).debug(`[${l}] sending request`,fc({retryOfRequestLogID:n,method:i.method,url:o,options:i,headers:s.headers})),i.signal?.aborted)throw new mn;let d=new AbortController,p=await this.fetchWithTimeout(o,s,c,d).catch($4),m=Date.now();if(p instanceof globalThis.Error){let v=`retrying, ${t} attempts remaining`;if(i.signal?.aborted)throw new mn;let b=B4(p)||/timed? ?out/i.test(String(p)+("cause"in p?String(p.cause):""));if(t)return an(this).info(`[${l}] connection ${b?"timed out":"failed"} - ${v}`),an(this).debug(`[${l}] connection ${b?"timed out":"failed"} (${v})`,fc({retryOfRequestLogID:n,url:o,durationMs:m-f,message:p.message})),this.retryRequest(i,t,n??l);throw an(this).info(`[${l}] connection ${b?"timed out":"failed"} - error; no more retries left`),an(this).debug(`[${l}] connection ${b?"timed out":"failed"} (error; no more retries left)`,fc({retryOfRequestLogID:n,url:o,durationMs:m-f,message:p.message})),b?new Wl:new zl({cause:p})}let h=[...p.headers.entries()].filter(([v])=>v==="x-request-id").map(([v,b])=>", "+v+": "+JSON.stringify(b)).join(""),g=`[${l}${u}${h}] ${s.method} ${o} ${p.ok?"succeeded":"failed"} with status ${p.status} in ${m-f}ms`;if(!p.ok){let v=await this.shouldRetry(p);if(t&&v){let R=`retrying, ${t} attempts remaining`;return await Lee(p.body),an(this).info(`${g} - ${R}`),an(this).debug(`[${l}] response error (${R})`,fc({retryOfRequestLogID:n,url:p.url,status:p.status,headers:p.headers,durationMs:m-f})),this.retryRequest(i,t,n??l,p.headers)}let b=v?"error; no more retries left":"error; not retryable";an(this).info(`${g} - ${b}`);let w=await p.text().catch(R=>$4(R).message),C=Iee(w),A=C?void 0:w;throw an(this).debug(`[${l}] response error (${b})`,fc({retryOfRequestLogID:n,url:p.url,status:p.status,headers:p.headers,message:A,durationMs:Date.now()-f})),this.makeStatusError(p.status,C,A,p.headers)}return an(this).info(g),an(this).debug(`[${l}] response start`,fc({retryOfRequestLogID:n,url:p.url,status:p.status,headers:p.headers,durationMs:m-f})),{response:p,options:i,controller:d,requestLogID:l,retryOfRequestLogID:n,startTime:f}}getAPIList(e,t,n){return this.requestAPIList(t,{method:"get",path:e,...n})}requestAPIList(e,t){let n=this.makeRequest(t,null,void 0);return new V4(this,n,e)}async fetchWithTimeout(e,t,n,i){let{signal:a,method:s,...o}=t||{};a&&a.addEventListener("abort",()=>i.abort());let c=setTimeout(()=>i.abort(),n),l=globalThis.ReadableStream&&o.body instanceof globalThis.ReadableStream||typeof o.body=="object"&&o.body!==null&&Symbol.asyncIterator in o.body,u={signal:i.signal,...l?{duplex:"half"}:{},method:"GET",...o};s&&(u.method=s.toUpperCase());try{return await this.fetch.call(void 0,e,u)}finally{clearTimeout(c)}}async shouldRetry(e){let t=e.headers.get("x-should-retry");return t==="true"?!0:t==="false"?!1:e.status===408||e.status===409||e.status===429||e.status>=500}async retryRequest(e,t,n,i){let a,s=i?.get("retry-after-ms");if(s){let c=parseFloat(s);Number.isNaN(c)||(a=c)}let o=i?.get("retry-after");if(o&&!a){let c=parseFloat(o);Number.isNaN(c)?a=Date.parse(o)-Date.now():a=c*1e3}if(!(a&&0<=a&&a<60*1e3)){let c=e.maxRetries??this.maxRetries;a=this.calculateDefaultRetryTimeoutMillis(t,c)}return await yo(a),this.makeRequest(e,t-1,n)}calculateDefaultRetryTimeoutMillis(e,t){let a=t-e,s=Math.min(.5*Math.pow(2,a),8),o=1-Math.random()*.25;return s*o*1e3}async buildRequest(e,{retryCount:t=0}={}){let n={...e},{method:i,path:a,query:s,defaultBaseURL:o}=n,c=this.buildURL(a,s,o);"timeout"in n&&Oee("timeout",n.timeout),n.timeout=n.timeout??this.timeout;let{bodyHeaders:l,body:u}=this.buildBody({options:n}),f=await this.buildHeaders({options:e,method:i,bodyHeaders:l,retryCount:t});return{req:{method:i,headers:f,...n.signal&&{signal:n.signal},...globalThis.ReadableStream&&u instanceof globalThis.ReadableStream&&{duplex:"half"},...u&&{body:u},...this.fetchOptions??{},...n.fetchOptions??{}},url:c,timeout:n.timeout}}async buildHeaders({options:e,method:t,bodyHeaders:n,retryCount:i}){let a={};this.idempotencyHeader&&t!=="get"&&(e.idempotencyKey||(e.idempotencyKey=this.defaultIdempotencyKey()),a[this.idempotencyHeader]=e.idempotencyKey);let s=Re([a,{Accept:"application/json","User-Agent":this.getUserAgent(),"X-Stainless-Retry-Count":String(i),...e.timeout?{"X-Stainless-Timeout":String(Math.trunc(e.timeout/1e3))}:{},...Nee(),"OpenAI-Organization":this.organization,"OpenAI-Project":this.project},await this.authHeaders(e),this._options.defaultHeaders,n,e.headers]);return this.validateHeaders(s),s.values}buildBody({options:{body:e,headers:t}}){if(!e)return{bodyHeaders:void 0,body:void 0};let n=Re([t]);return ArrayBuffer.isView(e)||e instanceof ArrayBuffer||e instanceof DataView||typeof e=="string"&&n.values.has("content-type")||globalThis.Blob&&e instanceof globalThis.Blob||e instanceof FormData||e instanceof URLSearchParams||globalThis.ReadableStream&&e instanceof globalThis.ReadableStream?{bodyHeaders:void 0,body:e}:typeof e=="object"&&(Symbol.asyncIterator in e||Symbol.iterator in e&&"next"in e&&typeof e.next=="function")?{bodyHeaders:void 0,body:Xb(e)}:me(this,Cy,"f").call(this,{body:e,headers:n})}};rO=Ct,Cy=new WeakMap,tO=new WeakSet,Dte=function(){return this.baseURL!=="https://api.openai.com/v1"};Ct.OpenAI=rO;Ct.DEFAULT_TIMEOUT=6e5;Ct.OpenAIError=We;Ct.APIError=Sn;Ct.APIConnectionError=zl;Ct.APIConnectionTimeoutError=Wl;Ct.APIUserAbortError=mn;Ct.NotFoundError=yp;Ct.ConflictError=xp;Ct.RateLimitError=_p;Ct.BadRequestError=gp;Ct.AuthenticationError=vp;Ct.InternalServerError=Ep;Ct.PermissionDeniedError=bp;Ct.UnprocessableEntityError=wp;Ct.InvalidWebhookSignatureError=bo;Ct.toFile=sy;Ct.Completions=Ku;Ct.Chat=Jl;Ct.Embeddings=Yu;Ct.Files=Qu;Ct.Images=r3;Ct.Audio=Eo;Ct.Moderations=i3;Ct.Models=n3;Ct.FineTuning=Ns;Ct.Graders=n2;Ct.VectorStores=bc;Ct.Webhooks=s3;Ct.Beta=Ms;Ct.Batches=Hu;Ct.Uploads=s2;Ct.Responses=a2;Ct.Realtime=vc;Ct.Conversations=t2;Ct.Evals=r2;Ct.Containers=e2;Ct.Videos=a3;var nO=Tr(As(),1),aHe=[{name:"gpt-4.1-2025-04-14",contextWindow:1e6,alias:"BIG",temperature:.8,maxOutputTokens:12e4,price:{inputMTokens:2,cacheRead:.5,outputMTokens:8}},{name:"gpt-5-mini",alias:"SMALL",contextWindow:4e5,temperature:1,maxOutputTokens:128e3,price:{inputMTokens:.25,cacheRead:.025,outputMTokens:2}}],ad=class r extends qf{constructor(t,n,i){super(n,i);this.interactor=t;this.mergeModels(aHe),n.name.toLowerCase()!=="openai"&&(this.models=n.models??[]),this.name=n.name}name;static MAX_TOOLS=128;truncateToolsIfNeeded(t){return t.length<=r.MAX_TOOLS?t:(this.interactor.warn(`\u26A0\uFE0F OpenAI limits tools to ${r.MAX_TOOLS} maximum. Your agent has ${t.length} tools. Truncating to first ${r.MAX_TOOLS} tools. Consider reducing your integrations or using a shorter tool list for better performance.`),t.slice(0,r.MAX_TOOLS))}async run(t,n){n.resetUsageForRun();let i=this.isOpenaiReady();if(!i)return this.returnError("Client not ready");let a=this.getModel(t);if(!a)return this.returnError(`Model not found for agent ${t.name}`);if(t.definition.openaiAssistantId)return this.runAssistant(t,a,n);let s=new nO.Subject,o=this.startThinkingInterval();return this.processThread(i,t,a,n,s).catch(c=>{s.next(new Wi({error:c}))}).finally(()=>{this.stopThinkingInterval(o),this.showAgentAndUsage(t,this.aiProviderConfig.name,a.name,n);let c=n.usage?.price||0;this.logAgentUsage(t,a.name,c),s.complete()}),s}async runAssistant(t,n,i){let a=this.isOpenaiReady();if(!a)return this.returnError("Client not ready");let s=new nO.Subject;i.data.openai={price:i.data?.openai?.price??0,runPrice:0,assistantThreadData:i.data?.openai?.assistantThreadData??{}};let o=n.contextWindow*this.charsPerToken-t.systemInstructions.length-t.tools.charLength,c=await this.getMessages(i,o,n.name);c.compacted&&(i.data.openai.assistantThreadData={});let l=i.data.openai.assistantThreadData,u=this.startThinkingInterval();if(!l.threadId){let m=await a.beta.threads.create();this.interactor.displayText("Assistant thread created"),l.threadId=m.id}let f=c.messages,d=l.lastTimestamp?f.findIndex(m=>m.timestamp>=l.lastTimestamp):-1,p=f.slice(d+1);return this.updateAssistantThread(a,i,p).then(async()=>await this.processAssistantThread(a,t,n,i,s)).finally(()=>{this.stopThinkingInterval(u),this.showAgentAndUsage(t,this.aiProviderConfig.name,n.name,i);let m=i.usage?.price||0;this.logAgentUsage(t,n.name,m),s.complete()}),s}async processAssistantThread(t,n,i,a,s){let o=t.beta.threads.runs.stream(a.data.openai.assistantThreadData.threadId,{assistant_id:n.definition.openaiAssistantId,tools:this.truncateToolsIfNeeded([...n.tools.getTools(),{type:"file_search"}]),tool_choice:"auto",max_completion_tokens:12e4,max_prompt_tokens:12e4,parallel_tool_calls:!1});await this.processAssistantStream(o,n,i,t,a,s)}async processThread(t,n,i,a,s){try{let o=n.systemInstructions.length+n.tools.charLength+20,c=i.contextWindow*this.charsPerToken-o,l=await this.getMessages(a,c,i.name),u=await t.chat.completions.create({model:i.name,messages:this.toOpenAiMessage(n,l.messages),tools:this.truncateToolsIfNeeded(n.tools.getTools()),max_completion_tokens:n.definition.maxOutputTokens??i.maxOutputTokens??void 0,temperature:n.definition.temperature??i.temperature??.8});this.updateUsage(u.usage,n,i,a);let f=u.choices[0];if(f.finish_reason==="length")throw new Error("Max tokens reached for Openai \u{1F62C}");let d=f.message.content?.trim();this.handleText(a,d,n,s);let p=f.message?.tool_calls?.filter(m=>m.type==="function").map(m=>new fn({toolRequestId:m.id,name:m.function.name,args:m.function.arguments}));await this.shouldProcessAgainAfterResponse(d,p,n,a)&&await this.processThread(t,n,i,a,s)}catch(o){this.handleError(o,s,this.aiProviderConfig.name)}}updateUsage(t,n,i,a){let s=t?.prompt_tokens_details?.cached_tokens??0,o=(t?.prompt_tokens??0)-s,c=o*(i?.price?.inputMTokens??0),l=t?.completion_tokens??0,u=l*(i?.price?.outputMTokens??0),f=s*(i?.price?.cacheRead??0),d=(c+u+f)/1e6;a.addUsage({input:o,output:l,cache_read:s,cache_write:0,price:d})}isOpenaiReady(){if(!this.aiProviderConfig.apiKey){this.interactor.warn(`${this.aiProviderConfig.name}_API_KEY not set, skipping AI command. Please configure your API key.`);return}try{return new Ct({apiKey:this.aiProviderConfig.apiKey,baseURL:this.aiProviderConfig.url})}catch(t){let n=t instanceof Error?t.message:"Unknown error";this.interactor.warn(`Failed to initialize ${this.aiProviderConfig.name} client: ${n}`),console.error(`${this.aiProviderConfig.name} client initialization error:`,t);return}}toOpenAiMessage(t,n){let i={content:t.systemInstructions,role:"system"},a=n.flatMap((s,o)=>{if(s instanceof Gi)return[{role:"user",content:s.summary}];if(s instanceof lr){let c=s.role==="user"&&o===n.length-1,u=this.enhanceWithCurrentDateTime(s.content,c).map(f=>{if(f.type==="text")return{type:"text",text:f.content};if(f.type==="image"){let d={type:"image_url",image_url:{url:`data:${f.mimeType};base64,${f.content}`,detail:"auto"}};return console.log("got an image in message event"),d}throw new Error(`Unknown content type: ${f.type}`)});return s.role==="assistant"?[{role:"assistant",content:typeof u=="string"?u:u.map(f=>f.type==="text"?f.text:"[Image]").join(" "),name:t.name}]:[{role:"user",content:u,name:s.name}]}if(s instanceof fn)return[{role:"assistant",name:t.name,tool_calls:[{type:"function",id:s.toolRequestId,function:{name:s.name,arguments:s.args}}]}];if(s instanceof pn){if(typeof s.output=="string")return[{role:"tool",content:s.output,tool_call_id:s.toolRequestId}];{let c=s.output;if(c.type==="image")return[{role:"tool",content:"Image retrieved successfully. See following message.",tool_call_id:s.toolRequestId},{role:"user",content:[{type:"image_url",image_url:{url:`data:${c.mimeType};base64,${c.content}`,detail:"auto"}}],name:"system"}];if(c.type==="text")return[{role:"tool",content:c.content,tool_call_id:s.toolRequestId}];throw new Error(`Unknown content type: ${c.type}`)}}return[]});return[i,...a]}async updateAssistantThread(t,n,i){try{this.interactor.displayText(`${i.length} messages to upload to assistant thread...`);for(let a of i){if(!this.shouldProceed(n))throw new Error("Assistant thread update interrupted");let s=this.toAssistantMessage(a);await t.beta.threads.messages.create(n.data.openai.assistantThreadData.threadId,s),n.data.openai.assistantThreadData.lastTimestamp=a.timestamp}this.interactor.displayText("Messages uploaded.")}catch(a){console.error("Error updating assistant thread:",a);let s=a instanceof Error?a.message:"Unknown error";throw this.interactor.displayText(`\u26A0\uFE0F Error updating assistant thread: ${s}`),a}}toAssistantMessage(t){if(t instanceof Gi)return{role:"user",content:t.summary};if(t instanceof lr){let n=t.content.filter(i=>i.type==="text").map(i=>i.content).join(`
829
829
  `);return{role:t.role,content:n}}return t instanceof pn?{role:"user",content:`Here is the result of :
830
830
  <toolRequestId>${t.toolRequestId}</toolRequestId>
831
831
  <output>${t.output}</output>`}:{role:"assistant",content:`${t.name}: Can you provide me the result of this :
@@ -848,8 +848,8 @@ If you understand the risks and have appropriate mitigations in place,
848
848
  you can set the \`dangerouslyAllowBrowser\` option to \`true\`, e.g.,
849
849
 
850
850
  new Anthropic({ apiKey, dangerouslyAllowBrowser: true });
851
- `);this.baseURL=a.baseURL,this.timeout=a.timeout??DO.DEFAULT_TIMEOUT,this.logger=a.logger??console;let s="warn";this.logLevel=s,this.logLevel=uO(a.logLevel,"ClientOptions.logLevel",this)??uO(T5("ANTHROPIC_LOG"),"process.env['ANTHROPIC_LOG']",this)??s,this.fetchOptions=a.fetchOptions,this.maxRetries=a.maxRetries??2,this.fetch=a.fetch??Bte(),Ke(this,Xy,Ute,"f"),this._options=a,this.apiKey=t,this.authToken=n}withOptions(e){return new this.constructor({...this._options,baseURL:this.baseURL,maxRetries:this.maxRetries,timeout:this.timeout,logger:this.logger,logLevel:this.logLevel,fetch:this.fetch,fetchOptions:this.fetchOptions,apiKey:this.apiKey,authToken:this.authToken,...e})}defaultQuery(){return this._options.defaultQuery}validateHeaders({values:e,nulls:t}){if(!(this.apiKey&&e.get("x-api-key"))&&!t.has("x-api-key")&&!(this.authToken&&e.get("authorization"))&&!t.has("authorization"))throw new Error('Could not resolve authentication method. Expected either apiKey or authToken to be set. Or for one of the "X-Api-Key" or "Authorization" headers to be explicitly omitted')}async authHeaders(e){return er([await this.apiKeyAuth(e),await this.bearerAuth(e)])}async apiKeyAuth(e){if(this.apiKey!=null)return er([{"X-Api-Key":this.apiKey}])}async bearerAuth(e){if(this.authToken!=null)return er([{Authorization:`Bearer ${this.authToken}`}])}stringifyQuery(e){return Object.entries(e).filter(([t,n])=>typeof n<"u").map(([t,n])=>{if(typeof n=="string"||typeof n=="number"||typeof n=="boolean")return`${encodeURIComponent(t)}=${encodeURIComponent(n)}`;if(n===null)return`${encodeURIComponent(t)}=`;throw new lt(`Cannot stringify type ${typeof n}; Expected string, number, boolean, or null. If you need to pass nested query parameters, you can manually encode them, e.g. { query: { 'foo[key1]': value1, 'foo[key2]': value2 } }, and please open a GitHub issue requesting better support for your use case.`)}).join("&")}getUserAgent(){return`${this.constructor.name}/JS ${c2}`}defaultIdempotencyKey(){return`stainless-node-retry-${iO()}`}makeStatusError(e,t,n,i){return jn.generate(e,t,n,i)}buildURL(e,t,n){let i=!ye(this,TO,"m",fre).call(this)&&n||this.baseURL,a=Pte(e)?new URL(e):new URL(i+(i.endsWith("/")&&e.startsWith("/")?e.slice(1):e)),s=this.defaultQuery();return Ote(s)||(t={...s,...t}),typeof t=="object"&&t&&!Array.isArray(t)&&(a.search=this.stringifyQuery(t)),a.toString()}_calculateNonstreamingTimeout(e){if(3600*e/128e3>600)throw new lt("Streaming is required for operations that may take longer than 10 minutes. See https://github.com/anthropics/anthropic-sdk-typescript#streaming-responses for more details");return 600*1e3}async prepareOptions(e){}async prepareRequest(e,{url:t,options:n}){}get(e,t){return this.methodRequest("get",e,t)}post(e,t){return this.methodRequest("post",e,t)}patch(e,t){return this.methodRequest("patch",e,t)}put(e,t){return this.methodRequest("put",e,t)}delete(e,t){return this.methodRequest("delete",e,t)}methodRequest(e,t,n){return this.request(Promise.resolve(n).then(i=>({method:e,path:t,...i})))}request(e,t=null){return new o3(this,this.makeRequest(e,t,void 0))}async makeRequest(e,t,n){let i=await e,a=i.maxRetries??this.maxRetries;t==null&&(t=a),await this.prepareOptions(i);let{req:s,url:o,timeout:c}=await this.buildRequest(i,{retryCount:a-t});await this.prepareRequest(s,{url:o,options:i});let l="log_"+(Math.random()*(1<<24)|0).toString(16).padStart(6,"0"),u=n===void 0?"":`, retryOf: ${n}`,f=Date.now();if(Mn(this).debug(`[${l}] sending request`,wc({retryOfRequestLogID:n,method:i.method,url:o,options:i,headers:s.headers})),i.signal?.aborted)throw new ri;let d=new AbortController,p=await this.fetchWithTimeout(o,s,c,d).catch(c5),m=Date.now();if(p instanceof globalThis.Error){let v=`retrying, ${t} attempts remaining`;if(i.signal?.aborted)throw new ri;let b=yc(p)||/timed? ?out/i.test(String(p)+("cause"in p?String(p.cause):""));if(t)return Mn(this).info(`[${l}] connection ${b?"timed out":"failed"} - ${v}`),Mn(this).debug(`[${l}] connection ${b?"timed out":"failed"} (${v})`,wc({retryOfRequestLogID:n,url:o,durationMs:m-f,message:p.message})),this.retryRequest(i,t,n??l);throw Mn(this).info(`[${l}] connection ${b?"timed out":"failed"} - error; no more retries left`),Mn(this).debug(`[${l}] connection ${b?"timed out":"failed"} (error; no more retries left)`,wc({retryOfRequestLogID:n,url:o,durationMs:m-f,message:p.message})),b?new sd:new o2({cause:p})}let h=[...p.headers.entries()].filter(([v])=>v==="request-id").map(([v,b])=>", "+v+": "+JSON.stringify(b)).join(""),g=`[${l}${u}${h}] ${s.method} ${o} ${p.ok?"succeeded":"failed"} with status ${p.status} in ${m-f}ms`;if(!p.ok){let v=await this.shouldRetry(p);if(t&&v){let R=`retrying, ${t} attempts remaining`;return await $te(p.body),Mn(this).info(`${g} - ${R}`),Mn(this).debug(`[${l}] response error (${R})`,wc({retryOfRequestLogID:n,url:p.url,status:p.status,headers:p.headers,durationMs:m-f})),this.retryRequest(i,t,n??l,p.headers)}let b=v?"error; no more retries left":"error; not retryable";Mn(this).info(`${g} - ${b}`);let w=await p.text().catch(R=>c5(R).message),C=Ay(w),A=C?void 0:w;throw Mn(this).debug(`[${l}] response error (${b})`,wc({retryOfRequestLogID:n,url:p.url,status:p.status,headers:p.headers,message:A,durationMs:Date.now()-f})),this.makeStatusError(p.status,C,A,p.headers)}return Mn(this).info(g),Mn(this).debug(`[${l}] response start`,wc({retryOfRequestLogID:n,url:p.url,status:p.status,headers:p.headers,durationMs:m-f})),{response:p,options:i,controller:d,requestLogID:l,retryOfRequestLogID:n,startTime:f}}getAPIList(e,t,n){return this.requestAPIList(t,{method:"get",path:e,...n})}requestAPIList(e,t){let n=this.makeRequest(t,null,void 0);return new h5(this,n,e)}async fetchWithTimeout(e,t,n,i){let{signal:a,method:s,...o}=t||{};a&&a.addEventListener("abort",()=>i.abort());let c=setTimeout(()=>i.abort(),n),l=globalThis.ReadableStream&&o.body instanceof globalThis.ReadableStream||typeof o.body=="object"&&o.body!==null&&Symbol.asyncIterator in o.body,u={signal:i.signal,...l?{duplex:"half"}:{},method:"GET",...o};s&&(u.method=s.toUpperCase());try{return await this.fetch.call(void 0,e,u)}finally{clearTimeout(c)}}async shouldRetry(e){let t=e.headers.get("x-should-retry");return t==="true"?!0:t==="false"?!1:e.status===408||e.status===409||e.status===429||e.status>=500}async retryRequest(e,t,n,i){let a,s=i?.get("retry-after-ms");if(s){let c=parseFloat(s);Number.isNaN(c)||(a=c)}let o=i?.get("retry-after");if(o&&!a){let c=parseFloat(o);Number.isNaN(c)?a=Date.parse(o)-Date.now():a=c*1e3}if(!(a&&0<=a&&a<60*1e3)){let c=e.maxRetries??this.maxRetries;a=this.calculateDefaultRetryTimeoutMillis(t,c)}return await Fte(a),this.makeRequest(e,t-1,n)}calculateDefaultRetryTimeoutMillis(e,t){let a=t-e,s=Math.min(.5*Math.pow(2,a),8),o=1-Math.random()*.25;return s*o*1e3}calculateNonstreamingTimeout(e,t){if(36e5*e/128e3>6e5||t!=null&&e>t)throw new lt("Streaming is required for operations that may take longer than 10 minutes. See https://github.com/anthropics/anthropic-sdk-typescript#long-requests for more details");return 6e5}async buildRequest(e,{retryCount:t=0}={}){let n={...e},{method:i,path:a,query:s,defaultBaseURL:o}=n,c=this.buildURL(a,s,o);"timeout"in n&&Rte("timeout",n.timeout),n.timeout=n.timeout??this.timeout;let{bodyHeaders:l,body:u}=this.buildBody({options:n}),f=await this.buildHeaders({options:e,method:i,bodyHeaders:l,retryCount:t});return{req:{method:i,headers:f,...n.signal&&{signal:n.signal},...globalThis.ReadableStream&&u instanceof globalThis.ReadableStream&&{duplex:"half"},...u&&{body:u},...this.fetchOptions??{},...n.fetchOptions??{}},url:c,timeout:n.timeout}}async buildHeaders({options:e,method:t,bodyHeaders:n,retryCount:i}){let a={};this.idempotencyHeader&&t!=="get"&&(e.idempotencyKey||(e.idempotencyKey=this.defaultIdempotencyKey()),a[this.idempotencyHeader]=e.idempotencyKey);let s=er([a,{Accept:"application/json","User-Agent":this.getUserAgent(),"X-Stainless-Retry-Count":String(i),...e.timeout?{"X-Stainless-Timeout":String(Math.trunc(e.timeout/1e3))}:{},...Lte(),...this._options.dangerouslyAllowBrowser?{"anthropic-dangerous-direct-browser-access":"true"}:void 0,"anthropic-version":"2023-06-01"},await this.authHeaders(e),this._options.defaultHeaders,n,e.headers]);return this.validateHeaders(s),s.values}buildBody({options:{body:e,headers:t}}){if(!e)return{bodyHeaders:void 0,body:void 0};let n=er([t]);return ArrayBuffer.isView(e)||e instanceof ArrayBuffer||e instanceof DataView||typeof e=="string"&&n.values.has("content-type")||globalThis.Blob&&e instanceof globalThis.Blob||e instanceof FormData||e instanceof URLSearchParams||globalThis.ReadableStream&&e instanceof globalThis.ReadableStream?{bodyHeaders:void 0,body:e}:typeof e=="object"&&(Symbol.asyncIterator in e||Symbol.iterator in e&&"next"in e&&typeof e.next=="function")?{bodyHeaders:void 0,body:ky(e)}:ye(this,Xy,"f").call(this,{body:e,headers:n})}};DO=Ur,Xy=new WeakMap,TO=new WeakSet,fre=function(){return this.baseURL!=="https://api.anthropic.com"};Ur.Anthropic=DO;Ur.HUMAN_PROMPT=pre;Ur.AI_PROMPT=dre;Ur.DEFAULT_TIMEOUT=6e5;Ur.AnthropicError=lt;Ur.APIError=jn;Ur.APIConnectionError=o2;Ur.APIConnectionTimeoutError=sd;Ur.APIUserAbortError=ri;Ur.NotFoundError=ud;Ur.ConflictError=fd;Ur.RateLimitError=dd;Ur.BadRequestError=od;Ur.AuthenticationError=cd;Ur.InternalServerError=hd;Ur.PermissionDeniedError=ld;Ur.UnprocessableEntityError=pd;Ur.toFile=Iy;var To=class extends Ur{constructor(){super(...arguments),this.completions=new u3(this),this.messages=new d2(this),this.models=new f3(this),this.beta=new ko(this)}};To.Completions=u3;To.Messages=d2;To.Models=f3;To.Beta=ko;var Zy=Tr(As(),1),qHe=[{name:"claude-sonnet-4-5-20250929",alias:"BIG",contextWindow:2e5,price:{inputMTokens:3,cacheWrite:3.75,cacheRead:.3,outputMTokens:15}},{name:"claude-3-5-haiku-latest",alias:"SMALL",contextWindow:2e5,price:{inputMTokens:.8,cacheWrite:1,cacheRead:.08,outputMTokens:4}}],LHe=60,PO=.4,OO=.9,hre=.5,BHe=5,Qy=class extends qf{constructor(t,n,i){super(n,i);this.interactor=t;this.name="Anthropic",this.mergeModels(qHe)}name;rateLimitInfo=null;async run(t,n){let i=this.isAnthropicReady(),a=this.getModel(t);if(!i||!a)return(0,Zy.of)();n.resetUsageForRun();let s=new Zy.Subject,o=this.startThinkingInterval();return this.processThread(i,t,a,n,s).catch(c=>{s.next(new Wi({error:c}))}).finally(()=>{this.stopThinkingInterval(o),this.showAgentAndUsage(t,"Anthropic",a.name,n);let c=n.usage?.price||0;this.logAgentUsage(t,a.name,c),s.complete()}),s}async processThread(t,n,i,a,s){await this.applyThrottlingDelay();let o=n.systemInstructions.length+n.tools.charLength+20,c=Math.max(i.contextWindow*this.charsPerToken-o,1e4),l,u;try{let p=await this.streamApiCall(t,i,n,a,c,s);l=p.data,u=p.response}catch(p){if(p.status===400&&p.error?.error?.message?.includes("prompt is too long")){this.interactor.displayText("\u26A0\uFE0F Context window exceeded. Forcing aggressive compaction...");let m=p.error.error.message.match(/(\d+) tokens > (\d+) maximum/);if(m){let h=parseInt(m[1]),g=parseInt(m[2]),v=h-g,b=Math.ceil(v*this.charsPerToken*1.2);c=Math.max(c-b,5e3),this.interactor.debug(`Token overshoot: ${v} tokens (${b} chars). Reduced budget to ${c} chars.`)}else c=Math.floor(c*.7),this.interactor.debug(`Reduced budget by 30% to ${c} chars.`);try{let h=await this.streamApiCall(t,i,n,a,c,s,!0);l=h.data,u=h.response,this.interactor.displayText("\u2705 Successfully recovered with compacted context")}catch(h){this.handleError(h,s,this.name);return}}else if(p.status===429&&p.headers){let m=parseInt(p.headers["retry-after"]??"60");this.interactor.displayText(`\u23F3 Rate limit hit. Waiting ${m} seconds before retry...`),this.updateRateLimitsFromHeaders(p.headers),this.displayRateLimitStatus(p.headers);try{await this.delay(m*1e3,"rate limit retry")}catch{this.handleError(new Error("Rate limit retry interrupted by shutdown"),s,this.name);return}this.interactor.displayText("\u{1F504} Retrying request...");try{let h=await this.streamApiCall(t,i,n,a,c,s);l=h.data,u=h.response}catch(h){this.handleError(h,s,this.name);return}}else{this.handleError(p,s,this.name);return}}if(u&&this.updateRateLimitsFromHeaders(u.headers),this.updateUsage(l?.usage,n,a),l.stop_reason==="max_tokens")throw new Error("Max tokens reached for Anthropic \u{1F62C}");let f=l.content.filter(p=>p.type==="text").map(p=>p.text.trim()).filter(p=>!!p).join(`
852
- `);this.handleText(a,f,n,s);let d=l.content.filter(p=>p.type==="tool_use").map(p=>new fn({toolRequestId:p.id,name:p.name,args:JSON.stringify(p.input)}));await this.shouldProcessAgainAfterResponse(f,d,n,a)&&await this.processThread(t,n,i,a,s)}updateUsage(t,n,i){let a=this.getModel(n),s=(t?.input_tokens??0)*(a?.price?.inputMTokens??0),o=(t?.output_tokens??0)*(a?.price?.outputMTokens??0),c=(t?.cache_creation_input_tokens??0)*(a?.price?.cacheWrite??0),l=(t?.cache_read_input_tokens??0)*(a?.price?.cacheRead??0),u=(s+o+c+l)/1e6;i.addUsage({input:t?.input_tokens??0,output:t?.output_tokens??0,cache_read:t?.cache_read_input_tokens??0,cache_write:t?.cache_creation_input_tokens??0,price:u})}isAnthropicReady(){if(!this.apiKey){this.interactor.warn("ANTHROPIC_API_KEY not set, skipping AI command. Please configure your API key.");return}try{return new To({apiKey:this.apiKey,defaultHeaders:{"anthropic-beta":"prompt-caching-2024-07-31"}})}catch(t){let n=t instanceof Error?t.message:"Unknown error";this.interactor.warn(`Failed to initialize Anthropic client: ${n}`),console.error("Anthropic client initialization error:",t);return}}toClaudeMessage(t,n,i=!1){let a=this.getOrUpdateCacheMarker(n,t,i);return t.map((s,o)=>{let c,l=a&&s.timestamp===a;if(s instanceof Gi)c={role:"user",content:[{type:"text",text:s.summary,...l&&{cache_control:{type:"ephemeral"}}}]};else if(s instanceof lr){let u=s.role==="user"&&o===t.length-1,f=this.enhanceWithCurrentDateTime(s.content,u),d=f.map((p,m)=>{let h;return p.type==="text"&&(h={type:"text",text:p.content}),p.type==="image"&&(h={type:"image",source:{type:"base64",media_type:p.mimeType,data:p.content}}),h?{...h,...l&&m===f.length-1&&{cache_control:{type:"ephemeral"}}}:(this.interactor.warn(`Unknown content type: ${p.type}`),null)}).filter(Boolean);c={role:s.role,content:d}}if(s instanceof fn&&(c={role:"assistant",content:[{type:"tool_use",id:s.toolRequestId,name:s.name,input:JSON.parse(s.args),...l&&{cache_control:{type:"ephemeral"}}}]}),s instanceof pn){let u;if(typeof s.output=="string")u=s.output;else{let f=s.output;if(f.type==="text")u=[{type:"text",text:f.content}];else if(f.type==="image")u=[{type:"image",source:{type:"base64",media_type:f.mimeType,data:f.content}}];else throw new Error(`Unknown content type: ${f.type}`)}c={role:"user",content:[{type:"tool_result",tool_use_id:s.toolRequestId,content:u,...l&&{cache_control:{type:"ephemeral"}}}]}}return c}).filter(s=>!!s)}getOrUpdateCacheMarker(t,n,i){let a=n.length;if(a<BHe)return this.interactor.debug(`\u{1F4CB} Cache: Conversation too short (${a} messages), no marker`),null;t.data.anthropic||(t.data.anthropic={});let s=t.data.anthropic.cacheMarkerMessageId;if(s&&!i){let u=n.findIndex(f=>f.timestamp===s);if(u!==-1){let f=u/a,d=Math.round(f*100),p=Math.round(hre*100),m=Math.round(OO*100);if(f>=hre&&f<=OO)return this.interactor.debug(`\u2705 Cache: Keeping marker at message ${u+1}/${a} (${d}%)`),s;this.interactor.debug(`\u{1F504} Cache: Marker at ${d}% is out of range (${p}-${m}%), repositioning...`)}else this.interactor.debug("\u26A0\uFE0F Cache: Marker message not found, creating new marker")}else this.interactor.debug("\u{1F195} Cache: No existing marker, creating first marker");let o=Math.floor(a*OO),c=n[o]?.timestamp,l=Math.round(o/a*100);return c?(t.data.anthropic.cacheMarkerMessageId=c,this.interactor.debug(`\u{1F4CD} Cache: Placed marker at message ${o+1}/${a} (${l}%) - ID: ${c}`),c):(this.interactor.debug(`\u274C Cache: Failed to place marker at position ${o}`),null)}getClaudeTools(t){let n=t.getTools().map(i=>({name:i.function.name,description:i.function.description,input_schema:i.function.parameters}));return n.length>0&&(n[n.length-1].cache_control={type:"ephemeral"}),n}async streamApiCall(t,n,i,a,s,o,c=!1){let l=await this.getMessages(a,s,n.name),u=this.toClaudeMessage(l.messages,a,c),f=t.messages.stream({model:n.name,messages:u,system:[{text:i.systemInstructions,type:"text",cache_control:{type:"ephemeral"}}],tools:this.getClaudeTools(i.tools),temperature:i.definition.temperature??.8,max_tokens:8192});return f.on("text",p=>{o.next(new Rf({chunk:p}))}),{data:await f.finalMessage(),response:void 0}}getHeader=(t,n)=>t?t instanceof Headers?t.get(n):t[n]||null:null;updateRateLimitsFromHeaders(t){if(!t){this.rateLimitInfo=null;return}let n=this.getHeader(t,"anthropic-ratelimit-input-tokens-remaining"),i=this.getHeader(t,"anthropic-ratelimit-output-tokens-remaining"),a=this.getHeader(t,"anthropic-ratelimit-requests-remaining"),s=this.getHeader(t,"anthropic-ratelimit-input-tokens-limit"),o=this.getHeader(t,"anthropic-ratelimit-output-tokens-limit"),c=this.getHeader(t,"anthropic-ratelimit-requests-limit");n&&!isNaN(parseInt(n))||i&&!isNaN(parseInt(i))||a&&!isNaN(parseInt(a))?this.rateLimitInfo={inputTokensRemaining:n?parseInt(n):999999,outputTokensRemaining:i?parseInt(i):999999,requestsRemaining:a?parseInt(a):999999,inputTokensLimit:s?parseInt(s):2e5,outputTokensLimit:o?parseInt(o):8e4,requestsLimit:c?parseInt(c):4e3}:this.rateLimitInfo=null}async applyThrottlingDelay(){if(!this.rateLimitInfo)return;let t=this.rateLimitInfo.inputTokensRemaining/Math.max(this.rateLimitInfo.inputTokensLimit,1),n=this.rateLimitInfo.outputTokensRemaining/Math.max(this.rateLimitInfo.outputTokensLimit,1),i=this.rateLimitInfo.requestsRemaining/Math.max(this.rateLimitInfo.requestsLimit,1);this.interactor.debug(`Rate limit ratios: ${JSON.stringify({inputTokensRatio:t,outputTokensRatio:n,requestsRatio:i,rateLimitInfo:this.rateLimitInfo})}`);let a=Math.min(t,n,i);if(a<PO){let s=(PO-a)/PO,o=Math.max(1,Math.round(s*LHe));this.interactor.displayText(`\u23F3 Rate limit approaching (${Math.round(a*100)}% remaining), waiting ${o} seconds...`);try{await this.delay(o*1e3,"throttling")}catch{throw new Error("Throttling delay interrupted by shutdown")}}}displayRateLimitStatus(t){let n=parseInt(t["anthropic-ratelimit-input-tokens-remaining"]??"0"),i=parseInt(t["anthropic-ratelimit-output-tokens-remaining"]??"0"),a=parseInt(t["anthropic-ratelimit-requests-remaining"]??"0"),s="unknown";n===0?s="input tokens":i===0?s="output tokens":a===0&&(s="requests"),this.interactor.debug(`\u{1F6A8} Rate limit exceeded: ${s} limit reached`)}handleError(t,n,i){super.handleError(t,n,i)}async complete(t,n){let i=this.isAnthropicReady();if(!i)throw new Error("Anthropic client not ready");let a=n?.model??this.models.find(s=>s.alias==="SMALL")?.name??"claude-3-5-haiku-latest";try{return(await i.messages.create({model:a,messages:[{role:"user",content:t}],max_tokens:n?.maxTokens??100,temperature:n?.temperature??.5,stop_sequences:n?.stopSequences})).content.filter(o=>o.type==="text").map(o=>o.text.trim()).join(" ")}catch(s){throw console.error("Anthropic completion error:",s),new Error(`Anthropic completion failed: ${s.message}`)}}};var ex=class extends ad{models=[{name:"gemini-2.5-pro",contextWindow:1e6,alias:"BIG",price:{inputMTokens:.175,cacheRead:.0875,outputMTokens:.525}},{name:"gemini-2.5-flash",alias:"SMALL",contextWindow:1e6,price:{inputMTokens:.1,cacheRead:.025,outputMTokens:.4}}];constructor(e,t,n){let i={...t,url:t.url||"https://generativelanguage.googleapis.com/v1beta/openai/"};super(e,i,n)}};var mre={anthropic:"ANTHROPIC_API_KEY",openai:"OPENAI_API_KEY",google:"GEMINI_API_KEY"},Cd=class{constructor(e,t,n,i){this.interactor=e;this.userService=t;this.projectService=n;this.logger=i}aiClients=[];aiProviderConfigs;init(e){if(this.aiProviderConfigs)return;let t=e.project.ai||[],n=this.projectService.selectedProject?.config?.ai||[],i=this.userService.config.ai||[],a=this.detectProvidersFromEnvironment(),s=new Set([...t.map(u=>u.name.toLowerCase()),...n.map(u=>u.name.toLowerCase()),...i.map(u=>u.name.toLowerCase())]);this.aiProviderConfigs=[];for(let u of[...a,...t,...n,...i]){let f=this.aiProviderConfigs.findIndex(d=>d.name===u.name);if(f===-1)this.aiProviderConfigs.push(u);else{let d=this.aiProviderConfigs[f],p=d.models?[...d.models]:[];(u.models??[]).forEach(h=>{let g=p.findIndex(v=>v.alias===h.alias||v.name===h.name);if(g===-1)p.push(h);else{let v=p[g];p[g]={...v,...h,price:{...v.price,...h.price}}}}),this.aiProviderConfigs[f]={...d,...u,models:p}}}this.aiClients=this.aiProviderConfigs.map(u=>this.createClient(u)).filter(u=>!!u);let o=a.filter(u=>!s.has(u.name.toLowerCase())).map(u=>u.name.toLowerCase()),c=new Set(o),l=`AI providers (models listed as: name (alias)):
851
+ `);this.baseURL=a.baseURL,this.timeout=a.timeout??DO.DEFAULT_TIMEOUT,this.logger=a.logger??console;let s="warn";this.logLevel=s,this.logLevel=uO(a.logLevel,"ClientOptions.logLevel",this)??uO(T5("ANTHROPIC_LOG"),"process.env['ANTHROPIC_LOG']",this)??s,this.fetchOptions=a.fetchOptions,this.maxRetries=a.maxRetries??2,this.fetch=a.fetch??Bte(),Ke(this,Xy,Ute,"f"),this._options=a,this.apiKey=t,this.authToken=n}withOptions(e){return new this.constructor({...this._options,baseURL:this.baseURL,maxRetries:this.maxRetries,timeout:this.timeout,logger:this.logger,logLevel:this.logLevel,fetch:this.fetch,fetchOptions:this.fetchOptions,apiKey:this.apiKey,authToken:this.authToken,...e})}defaultQuery(){return this._options.defaultQuery}validateHeaders({values:e,nulls:t}){if(!(this.apiKey&&e.get("x-api-key"))&&!t.has("x-api-key")&&!(this.authToken&&e.get("authorization"))&&!t.has("authorization"))throw new Error('Could not resolve authentication method. Expected either apiKey or authToken to be set. Or for one of the "X-Api-Key" or "Authorization" headers to be explicitly omitted')}async authHeaders(e){return er([await this.apiKeyAuth(e),await this.bearerAuth(e)])}async apiKeyAuth(e){if(this.apiKey!=null)return er([{"X-Api-Key":this.apiKey}])}async bearerAuth(e){if(this.authToken!=null)return er([{Authorization:`Bearer ${this.authToken}`}])}stringifyQuery(e){return Object.entries(e).filter(([t,n])=>typeof n<"u").map(([t,n])=>{if(typeof n=="string"||typeof n=="number"||typeof n=="boolean")return`${encodeURIComponent(t)}=${encodeURIComponent(n)}`;if(n===null)return`${encodeURIComponent(t)}=`;throw new lt(`Cannot stringify type ${typeof n}; Expected string, number, boolean, or null. If you need to pass nested query parameters, you can manually encode them, e.g. { query: { 'foo[key1]': value1, 'foo[key2]': value2 } }, and please open a GitHub issue requesting better support for your use case.`)}).join("&")}getUserAgent(){return`${this.constructor.name}/JS ${c2}`}defaultIdempotencyKey(){return`stainless-node-retry-${iO()}`}makeStatusError(e,t,n,i){return jn.generate(e,t,n,i)}buildURL(e,t,n){let i=!ye(this,TO,"m",fre).call(this)&&n||this.baseURL,a=Pte(e)?new URL(e):new URL(i+(i.endsWith("/")&&e.startsWith("/")?e.slice(1):e)),s=this.defaultQuery();return Ote(s)||(t={...s,...t}),typeof t=="object"&&t&&!Array.isArray(t)&&(a.search=this.stringifyQuery(t)),a.toString()}_calculateNonstreamingTimeout(e){if(3600*e/128e3>600)throw new lt("Streaming is required for operations that may take longer than 10 minutes. See https://github.com/anthropics/anthropic-sdk-typescript#streaming-responses for more details");return 600*1e3}async prepareOptions(e){}async prepareRequest(e,{url:t,options:n}){}get(e,t){return this.methodRequest("get",e,t)}post(e,t){return this.methodRequest("post",e,t)}patch(e,t){return this.methodRequest("patch",e,t)}put(e,t){return this.methodRequest("put",e,t)}delete(e,t){return this.methodRequest("delete",e,t)}methodRequest(e,t,n){return this.request(Promise.resolve(n).then(i=>({method:e,path:t,...i})))}request(e,t=null){return new o3(this,this.makeRequest(e,t,void 0))}async makeRequest(e,t,n){let i=await e,a=i.maxRetries??this.maxRetries;t==null&&(t=a),await this.prepareOptions(i);let{req:s,url:o,timeout:c}=await this.buildRequest(i,{retryCount:a-t});await this.prepareRequest(s,{url:o,options:i});let l="log_"+(Math.random()*(1<<24)|0).toString(16).padStart(6,"0"),u=n===void 0?"":`, retryOf: ${n}`,f=Date.now();if(Mn(this).debug(`[${l}] sending request`,wc({retryOfRequestLogID:n,method:i.method,url:o,options:i,headers:s.headers})),i.signal?.aborted)throw new ri;let d=new AbortController,p=await this.fetchWithTimeout(o,s,c,d).catch(c5),m=Date.now();if(p instanceof globalThis.Error){let v=`retrying, ${t} attempts remaining`;if(i.signal?.aborted)throw new ri;let b=yc(p)||/timed? ?out/i.test(String(p)+("cause"in p?String(p.cause):""));if(t)return Mn(this).info(`[${l}] connection ${b?"timed out":"failed"} - ${v}`),Mn(this).debug(`[${l}] connection ${b?"timed out":"failed"} (${v})`,wc({retryOfRequestLogID:n,url:o,durationMs:m-f,message:p.message})),this.retryRequest(i,t,n??l);throw Mn(this).info(`[${l}] connection ${b?"timed out":"failed"} - error; no more retries left`),Mn(this).debug(`[${l}] connection ${b?"timed out":"failed"} (error; no more retries left)`,wc({retryOfRequestLogID:n,url:o,durationMs:m-f,message:p.message})),b?new sd:new o2({cause:p})}let h=[...p.headers.entries()].filter(([v])=>v==="request-id").map(([v,b])=>", "+v+": "+JSON.stringify(b)).join(""),g=`[${l}${u}${h}] ${s.method} ${o} ${p.ok?"succeeded":"failed"} with status ${p.status} in ${m-f}ms`;if(!p.ok){let v=await this.shouldRetry(p);if(t&&v){let R=`retrying, ${t} attempts remaining`;return await $te(p.body),Mn(this).info(`${g} - ${R}`),Mn(this).debug(`[${l}] response error (${R})`,wc({retryOfRequestLogID:n,url:p.url,status:p.status,headers:p.headers,durationMs:m-f})),this.retryRequest(i,t,n??l,p.headers)}let b=v?"error; no more retries left":"error; not retryable";Mn(this).info(`${g} - ${b}`);let w=await p.text().catch(R=>c5(R).message),C=Ay(w),A=C?void 0:w;throw Mn(this).debug(`[${l}] response error (${b})`,wc({retryOfRequestLogID:n,url:p.url,status:p.status,headers:p.headers,message:A,durationMs:Date.now()-f})),this.makeStatusError(p.status,C,A,p.headers)}return Mn(this).info(g),Mn(this).debug(`[${l}] response start`,wc({retryOfRequestLogID:n,url:p.url,status:p.status,headers:p.headers,durationMs:m-f})),{response:p,options:i,controller:d,requestLogID:l,retryOfRequestLogID:n,startTime:f}}getAPIList(e,t,n){return this.requestAPIList(t,{method:"get",path:e,...n})}requestAPIList(e,t){let n=this.makeRequest(t,null,void 0);return new h5(this,n,e)}async fetchWithTimeout(e,t,n,i){let{signal:a,method:s,...o}=t||{};a&&a.addEventListener("abort",()=>i.abort());let c=setTimeout(()=>i.abort(),n),l=globalThis.ReadableStream&&o.body instanceof globalThis.ReadableStream||typeof o.body=="object"&&o.body!==null&&Symbol.asyncIterator in o.body,u={signal:i.signal,...l?{duplex:"half"}:{},method:"GET",...o};s&&(u.method=s.toUpperCase());try{return await this.fetch.call(void 0,e,u)}finally{clearTimeout(c)}}async shouldRetry(e){let t=e.headers.get("x-should-retry");return t==="true"?!0:t==="false"?!1:e.status===408||e.status===409||e.status===429||e.status>=500}async retryRequest(e,t,n,i){let a,s=i?.get("retry-after-ms");if(s){let c=parseFloat(s);Number.isNaN(c)||(a=c)}let o=i?.get("retry-after");if(o&&!a){let c=parseFloat(o);Number.isNaN(c)?a=Date.parse(o)-Date.now():a=c*1e3}if(!(a&&0<=a&&a<60*1e3)){let c=e.maxRetries??this.maxRetries;a=this.calculateDefaultRetryTimeoutMillis(t,c)}return await Fte(a),this.makeRequest(e,t-1,n)}calculateDefaultRetryTimeoutMillis(e,t){let a=t-e,s=Math.min(.5*Math.pow(2,a),8),o=1-Math.random()*.25;return s*o*1e3}calculateNonstreamingTimeout(e,t){if(36e5*e/128e3>6e5||t!=null&&e>t)throw new lt("Streaming is required for operations that may take longer than 10 minutes. See https://github.com/anthropics/anthropic-sdk-typescript#long-requests for more details");return 6e5}async buildRequest(e,{retryCount:t=0}={}){let n={...e},{method:i,path:a,query:s,defaultBaseURL:o}=n,c=this.buildURL(a,s,o);"timeout"in n&&Rte("timeout",n.timeout),n.timeout=n.timeout??this.timeout;let{bodyHeaders:l,body:u}=this.buildBody({options:n}),f=await this.buildHeaders({options:e,method:i,bodyHeaders:l,retryCount:t});return{req:{method:i,headers:f,...n.signal&&{signal:n.signal},...globalThis.ReadableStream&&u instanceof globalThis.ReadableStream&&{duplex:"half"},...u&&{body:u},...this.fetchOptions??{},...n.fetchOptions??{}},url:c,timeout:n.timeout}}async buildHeaders({options:e,method:t,bodyHeaders:n,retryCount:i}){let a={};this.idempotencyHeader&&t!=="get"&&(e.idempotencyKey||(e.idempotencyKey=this.defaultIdempotencyKey()),a[this.idempotencyHeader]=e.idempotencyKey);let s=er([a,{Accept:"application/json","User-Agent":this.getUserAgent(),"X-Stainless-Retry-Count":String(i),...e.timeout?{"X-Stainless-Timeout":String(Math.trunc(e.timeout/1e3))}:{},...Lte(),...this._options.dangerouslyAllowBrowser?{"anthropic-dangerous-direct-browser-access":"true"}:void 0,"anthropic-version":"2023-06-01"},await this.authHeaders(e),this._options.defaultHeaders,n,e.headers]);return this.validateHeaders(s),s.values}buildBody({options:{body:e,headers:t}}){if(!e)return{bodyHeaders:void 0,body:void 0};let n=er([t]);return ArrayBuffer.isView(e)||e instanceof ArrayBuffer||e instanceof DataView||typeof e=="string"&&n.values.has("content-type")||globalThis.Blob&&e instanceof globalThis.Blob||e instanceof FormData||e instanceof URLSearchParams||globalThis.ReadableStream&&e instanceof globalThis.ReadableStream?{bodyHeaders:void 0,body:e}:typeof e=="object"&&(Symbol.asyncIterator in e||Symbol.iterator in e&&"next"in e&&typeof e.next=="function")?{bodyHeaders:void 0,body:ky(e)}:ye(this,Xy,"f").call(this,{body:e,headers:n})}};DO=Ur,Xy=new WeakMap,TO=new WeakSet,fre=function(){return this.baseURL!=="https://api.anthropic.com"};Ur.Anthropic=DO;Ur.HUMAN_PROMPT=pre;Ur.AI_PROMPT=dre;Ur.DEFAULT_TIMEOUT=6e5;Ur.AnthropicError=lt;Ur.APIError=jn;Ur.APIConnectionError=o2;Ur.APIConnectionTimeoutError=sd;Ur.APIUserAbortError=ri;Ur.NotFoundError=ud;Ur.ConflictError=fd;Ur.RateLimitError=dd;Ur.BadRequestError=od;Ur.AuthenticationError=cd;Ur.InternalServerError=hd;Ur.PermissionDeniedError=ld;Ur.UnprocessableEntityError=pd;Ur.toFile=Iy;var To=class extends Ur{constructor(){super(...arguments),this.completions=new u3(this),this.messages=new d2(this),this.models=new f3(this),this.beta=new ko(this)}};To.Completions=u3;To.Messages=d2;To.Models=f3;To.Beta=ko;var Zy=Tr(As(),1),qHe=[{name:"claude-sonnet-4-5",alias:"BIG",contextWindow:2e5,temperature:.8,maxOutputTokens:64e3,price:{inputMTokens:3,cacheWrite:3.75,cacheRead:.3,outputMTokens:15}},{name:"claude-haiku-4-5",alias:"SMALL",contextWindow:2e5,temperature:.8,maxOutputTokens:64e3,price:{inputMTokens:1,cacheWrite:1.25,cacheRead:.1,outputMTokens:5}}],LHe=60,PO=.4,OO=.9,hre=.5,BHe=5,Qy=class extends qf{constructor(t,n,i){super(n,i);this.interactor=t;this.name="Anthropic",this.mergeModels(qHe)}name;rateLimitInfo=null;async run(t,n){let i=this.isAnthropicReady(),a=this.getModel(t);if(!i||!a)return(0,Zy.of)();n.resetUsageForRun();let s=new Zy.Subject,o=this.startThinkingInterval();return this.processThread(i,t,a,n,s).catch(c=>{s.next(new Wi({error:c}))}).finally(()=>{this.stopThinkingInterval(o),this.showAgentAndUsage(t,"Anthropic",a.name,n);let c=n.usage?.price||0;this.logAgentUsage(t,a.name,c),s.complete()}),s}async processThread(t,n,i,a,s){await this.applyThrottlingDelay();let o=n.systemInstructions.length+n.tools.charLength+20,c=Math.max(i.contextWindow*this.charsPerToken-o,1e4),l,u;try{let p=await this.streamApiCall(t,i,n,a,c,s);l=p.data,u=p.response}catch(p){if(p.status===400&&p.error?.error?.message?.includes("prompt is too long")){this.interactor.displayText("\u26A0\uFE0F Context window exceeded. Forcing aggressive compaction...");let m=p.error.error.message.match(/(\d+) tokens > (\d+) maximum/);if(m){let h=parseInt(m[1]),g=parseInt(m[2]),v=h-g,b=Math.ceil(v*this.charsPerToken*1.2);c=Math.max(c-b,5e3),this.interactor.debug(`Token overshoot: ${v} tokens (${b} chars). Reduced budget to ${c} chars.`)}else c=Math.floor(c*.7),this.interactor.debug(`Reduced budget by 30% to ${c} chars.`);try{let h=await this.streamApiCall(t,i,n,a,c,s,!0);l=h.data,u=h.response,this.interactor.displayText("\u2705 Successfully recovered with compacted context")}catch(h){this.handleError(h,s,this.name);return}}else if(p.status===429&&p.headers){let m=parseInt(p.headers["retry-after"]??"60");this.interactor.displayText(`\u23F3 Rate limit hit. Waiting ${m} seconds before retry...`),this.updateRateLimitsFromHeaders(p.headers),this.displayRateLimitStatus(p.headers);try{await this.delay(m*1e3,"rate limit retry")}catch{this.handleError(new Error("Rate limit retry interrupted by shutdown"),s,this.name);return}this.interactor.displayText("\u{1F504} Retrying request...");try{let h=await this.streamApiCall(t,i,n,a,c,s);l=h.data,u=h.response}catch(h){this.handleError(h,s,this.name);return}}else{this.handleError(p,s,this.name);return}}if(u&&this.updateRateLimitsFromHeaders(u.headers),this.updateUsage(l?.usage,n,a),l.stop_reason==="max_tokens")throw new Error("Max tokens reached for Anthropic \u{1F62C}");let f=l.content.filter(p=>p.type==="text").map(p=>p.text.trim()).filter(p=>!!p).join(`
852
+ `);this.handleText(a,f,n,s);let d=l.content.filter(p=>p.type==="tool_use").map(p=>new fn({toolRequestId:p.id,name:p.name,args:JSON.stringify(p.input)}));await this.shouldProcessAgainAfterResponse(f,d,n,a)&&await this.processThread(t,n,i,a,s)}updateUsage(t,n,i){let a=this.getModel(n),s=(t?.input_tokens??0)*(a?.price?.inputMTokens??0),o=(t?.output_tokens??0)*(a?.price?.outputMTokens??0),c=(t?.cache_creation_input_tokens??0)*(a?.price?.cacheWrite??0),l=(t?.cache_read_input_tokens??0)*(a?.price?.cacheRead??0),u=(s+o+c+l)/1e6;i.addUsage({input:t?.input_tokens??0,output:t?.output_tokens??0,cache_read:t?.cache_read_input_tokens??0,cache_write:t?.cache_creation_input_tokens??0,price:u})}isAnthropicReady(){if(!this.apiKey){this.interactor.warn("ANTHROPIC_API_KEY not set, skipping AI command. Please configure your API key.");return}try{return new To({apiKey:this.apiKey,defaultHeaders:{"anthropic-beta":"prompt-caching-2024-07-31"}})}catch(t){let n=t instanceof Error?t.message:"Unknown error";this.interactor.warn(`Failed to initialize Anthropic client: ${n}`),console.error("Anthropic client initialization error:",t);return}}toClaudeMessage(t,n,i=!1){let a=this.getOrUpdateCacheMarker(n,t,i);return t.map((s,o)=>{let c,l=a&&s.timestamp===a;if(s instanceof Gi)c={role:"user",content:[{type:"text",text:s.summary,...l&&{cache_control:{type:"ephemeral"}}}]};else if(s instanceof lr){let u=s.role==="user"&&o===t.length-1,f=this.enhanceWithCurrentDateTime(s.content,u),d=f.map((p,m)=>{let h;return p.type==="text"&&(h={type:"text",text:p.content}),p.type==="image"&&(h={type:"image",source:{type:"base64",media_type:p.mimeType,data:p.content}}),h?{...h,...l&&m===f.length-1&&{cache_control:{type:"ephemeral"}}}:(this.interactor.warn(`Unknown content type: ${p.type}`),null)}).filter(Boolean);c={role:s.role,content:d}}if(s instanceof fn&&(c={role:"assistant",content:[{type:"tool_use",id:s.toolRequestId,name:s.name,input:JSON.parse(s.args),...l&&{cache_control:{type:"ephemeral"}}}]}),s instanceof pn){let u;if(typeof s.output=="string")u=s.output;else{let f=s.output;if(f.type==="text")u=[{type:"text",text:f.content}];else if(f.type==="image")u=[{type:"image",source:{type:"base64",media_type:f.mimeType,data:f.content}}];else throw new Error(`Unknown content type: ${f.type}`)}c={role:"user",content:[{type:"tool_result",tool_use_id:s.toolRequestId,content:u,...l&&{cache_control:{type:"ephemeral"}}}]}}return c}).filter(s=>!!s)}getOrUpdateCacheMarker(t,n,i){let a=n.length;if(a<BHe)return this.interactor.debug(`\u{1F4CB} Cache: Conversation too short (${a} messages), no marker`),null;t.data.anthropic||(t.data.anthropic={});let s=t.data.anthropic.cacheMarkerMessageId;if(s&&!i){let u=n.findIndex(f=>f.timestamp===s);if(u!==-1){let f=u/a,d=Math.round(f*100),p=Math.round(hre*100),m=Math.round(OO*100);if(f>=hre&&f<=OO)return this.interactor.debug(`\u2705 Cache: Keeping marker at message ${u+1}/${a} (${d}%)`),s;this.interactor.debug(`\u{1F504} Cache: Marker at ${d}% is out of range (${p}-${m}%), repositioning...`)}else this.interactor.debug("\u26A0\uFE0F Cache: Marker message not found, creating new marker")}else this.interactor.debug("\u{1F195} Cache: No existing marker, creating first marker");let o=Math.floor(a*OO),c=n[o]?.timestamp,l=Math.round(o/a*100);return c?(t.data.anthropic.cacheMarkerMessageId=c,this.interactor.debug(`\u{1F4CD} Cache: Placed marker at message ${o+1}/${a} (${l}%) - ID: ${c}`),c):(this.interactor.debug(`\u274C Cache: Failed to place marker at position ${o}`),null)}getClaudeTools(t){let n=t.getTools().map(i=>({name:i.function.name,description:i.function.description,input_schema:i.function.parameters}));return n.length>0&&(n[n.length-1].cache_control={type:"ephemeral"}),n}async streamApiCall(t,n,i,a,s,o,c=!1){let l=await this.getMessages(a,s,n.name),u=this.toClaudeMessage(l.messages,a,c),f=t.messages.stream({model:n.name,messages:u,system:[{text:i.systemInstructions,type:"text",cache_control:{type:"ephemeral"}}],tools:this.getClaudeTools(i.tools),temperature:i.definition.temperature??n.temperature??.8,max_tokens:i.definition.maxOutputTokens??n.maxOutputTokens??8192});return f.on("text",p=>{o.next(new Rf({chunk:p}))}),{data:await f.finalMessage(),response:void 0}}getHeader=(t,n)=>t?t instanceof Headers?t.get(n):t[n]||null:null;updateRateLimitsFromHeaders(t){if(!t){this.rateLimitInfo=null;return}let n=this.getHeader(t,"anthropic-ratelimit-input-tokens-remaining"),i=this.getHeader(t,"anthropic-ratelimit-output-tokens-remaining"),a=this.getHeader(t,"anthropic-ratelimit-requests-remaining"),s=this.getHeader(t,"anthropic-ratelimit-input-tokens-limit"),o=this.getHeader(t,"anthropic-ratelimit-output-tokens-limit"),c=this.getHeader(t,"anthropic-ratelimit-requests-limit");n&&!isNaN(parseInt(n))||i&&!isNaN(parseInt(i))||a&&!isNaN(parseInt(a))?this.rateLimitInfo={inputTokensRemaining:n?parseInt(n):999999,outputTokensRemaining:i?parseInt(i):999999,requestsRemaining:a?parseInt(a):999999,inputTokensLimit:s?parseInt(s):2e5,outputTokensLimit:o?parseInt(o):8e4,requestsLimit:c?parseInt(c):4e3}:this.rateLimitInfo=null}async applyThrottlingDelay(){if(!this.rateLimitInfo)return;let t=this.rateLimitInfo.inputTokensRemaining/Math.max(this.rateLimitInfo.inputTokensLimit,1),n=this.rateLimitInfo.outputTokensRemaining/Math.max(this.rateLimitInfo.outputTokensLimit,1),i=this.rateLimitInfo.requestsRemaining/Math.max(this.rateLimitInfo.requestsLimit,1);this.interactor.debug(`Rate limit ratios: ${JSON.stringify({inputTokensRatio:t,outputTokensRatio:n,requestsRatio:i,rateLimitInfo:this.rateLimitInfo})}`);let a=Math.min(t,n,i);if(a<PO){let s=(PO-a)/PO,o=Math.max(1,Math.round(s*LHe));this.interactor.displayText(`\u23F3 Rate limit approaching (${Math.round(a*100)}% remaining), waiting ${o} seconds...`);try{await this.delay(o*1e3,"throttling")}catch{throw new Error("Throttling delay interrupted by shutdown")}}}displayRateLimitStatus(t){let n=parseInt(t["anthropic-ratelimit-input-tokens-remaining"]??"0"),i=parseInt(t["anthropic-ratelimit-output-tokens-remaining"]??"0"),a=parseInt(t["anthropic-ratelimit-requests-remaining"]??"0"),s="unknown";n===0?s="input tokens":i===0?s="output tokens":a===0&&(s="requests"),this.interactor.debug(`\u{1F6A8} Rate limit exceeded: ${s} limit reached`)}handleError(t,n,i){super.handleError(t,n,i)}async complete(t,n){let i=this.isAnthropicReady();if(!i)throw new Error("Anthropic client not ready");let a=n?.model??this.models.find(s=>s.alias==="SMALL")?.name??"claude-3-5-haiku-latest";try{return(await i.messages.create({model:a,messages:[{role:"user",content:t}],max_tokens:n?.maxTokens??100,temperature:n?.temperature??.5,stop_sequences:n?.stopSequences})).content.filter(o=>o.type==="text").map(o=>o.text.trim()).join(" ")}catch(s){throw console.error("Anthropic completion error:",s),new Error(`Anthropic completion failed: ${s.message}`)}}};var ex=class extends ad{models=[{name:"gemini-2.5-pro",contextWindow:1e6,alias:"BIG",temperature:.8,maxOutputTokens:65e3,price:{inputMTokens:.175,cacheRead:.0875,outputMTokens:.525}},{name:"gemini-2.5-flash",alias:"SMALL",contextWindow:1e6,temperature:.8,maxOutputTokens:65e3,price:{inputMTokens:.1,cacheRead:.025,outputMTokens:.4}}];constructor(e,t,n){let i={...t,url:t.url||"https://generativelanguage.googleapis.com/v1beta/openai/"};super(e,i,n)}};var mre={anthropic:"ANTHROPIC_API_KEY",openai:"OPENAI_API_KEY",google:"GEMINI_API_KEY"},Cd=class{constructor(e,t,n,i){this.interactor=e;this.userService=t;this.projectService=n;this.logger=i}aiClients=[];aiProviderConfigs;init(e){if(this.aiProviderConfigs)return;let t=e.project.ai||[],n=this.projectService.selectedProject?.config?.ai||[],i=this.userService.config.ai||[],a=this.detectProvidersFromEnvironment(),s=new Set([...t.map(u=>u.name.toLowerCase()),...n.map(u=>u.name.toLowerCase()),...i.map(u=>u.name.toLowerCase())]);this.aiProviderConfigs=[];for(let u of[...a,...t,...n,...i]){let f=this.aiProviderConfigs.findIndex(d=>d.name===u.name);if(f===-1)this.aiProviderConfigs.push(u);else{let d=this.aiProviderConfigs[f],p=d.models?[...d.models]:[];(u.models??[]).forEach(h=>{let g=p.findIndex(v=>v.alias===h.alias||v.name===h.name);if(g===-1)p.push(h);else{let v=p[g];p[g]={...v,...h,price:{...v.price,...h.price}}}}),this.aiProviderConfigs[f]={...d,...u,models:p}}}this.aiClients=this.aiProviderConfigs.map(u=>this.createClient(u)).filter(u=>!!u);let o=a.filter(u=>!s.has(u.name.toLowerCase())).map(u=>u.name.toLowerCase()),c=new Set(o),l=`AI providers (models listed as: name (alias)):
853
853
  `+this.aiProviderConfigs.map(u=>{let f=this.aiClients.find(v=>v.name.toLowerCase()===u.name.toLowerCase()),d=!!f,p=d?"\u2705":"\u274C",h=c.has(u.name.toLowerCase())?" (auto-detected)":"",g=` - ${p} ${u.name}${h}`;if(d&&Array.isArray(f.models)&&f.models.length>0){let v=f.models.map(b=>b.alias&&b.alias!==b.name?`${b.name} (${b.alias})`:b.name).join(", ");g+=`, models: ${v}`}return g}).join(`
854
854
  `);this.interactor.displayText(l)}getClient(e,t){let n=this.aiClients.filter(i=>{let a=!e||i.name.toLowerCase()===e.toLowerCase(),s=!t||i.supportsModel(t.toLowerCase());return a&&s});return n.length?n[0]:void 0}cleanup(){this.aiClients.forEach(e=>e.kill()),this.aiClients=[]}kill(){this.cleanup(),this.aiProviderConfigs=void 0}getApiKey(e){let t=mre[e.name];return(t?process.env[t]??process.env[`${e.name.toUpperCase()}_API_KEY`]:void 0)??e.apiKey}detectProvidersFromEnvironment(){let e=[];for(let[t,n]of Object.entries(mre)){let i=process.env[n];if(i){let a={name:t,apiKey:i};e.push(a),this.interactor.debug(`\u{1F50D} Auto-detected ${t} provider from ${n} environment variable`)}}return e}createClient(e){let t=this.getApiKey(e);if(!t){this.interactor.displayText(`\u2139\uFE0F no api key for AI provider '${e.name}'`);return}let n={...e,apiKey:t};switch(e.name.toLowerCase()){case"anthropic":return new Qy(this.interactor,n,this.logger);case"google":return new ex(this.interactor,n,this.logger);default:return new ad(this.interactor,n,this.logger)}}};var Pue=Tr(Nl(),1);import*as Zw from"fs/promises";import*as K3 from"path";var Kr=class{constructor(e){this.interactor=e}tools=[];async kill(){}async getTools(e,t,n){return this.tools=await this.buildTools(e,n),this.tools.filter(i=>!t||!t.length||t.includes(i.function.name))}};function gre(r){let{context:e}=r;return({query:n,agentName:i})=>{try{let a=`@${i} ${n}`;return e.addCommands(a),`Query successfully redirected to agent '${i}'. The agent will process the query with full context after this run completes.`}catch(a){return`Error during redirection: ${a.message}`}}}var tx=class extends Kr{constructor(t,n){super(t);this.agentService=n}name="AI";async buildTools(t,n){let i=[];if(!t.oneshot){let s={type:"function",function:{name:"queryUser",description:`Allows to ask the user a question.
855
855
  IMPORTANT: Use this tool only when necessary, as it is intrusive for the user.