import{n as e}from"./chunk-DR8-3Aex.js";import{ai as t,ci as n}from"./paths-Ci1iwL5Y.js";import{l as r,p as i}from"./debug-otxXY3j-.js";import{B_ as a,G_ as o,K_ as s,Qo as c,Tm as l,Uo as u,V_ as d,W_ as f,Xo as p,dD as m,km as h,mo as g,q_ as _,st as v,uD as y,ut as b}from"./loadAgentsDir-C_wFhytF.js";import{F as x,I as S,J as C,L as w,P as T,R as E}from"./rcDebugLog-C42A0pX1.js";import{n as D,t as O}from"./openaiShared-C-AuktRP.js";import{randomUUID as k}from"crypto";async function*A(e,n,r,u,d){try{let h=C(d.model),g=c(e,r),_=await Promise.all(r.map(e=>b(e,{getToolPermissionContext:d.getToolPermissionContext,tools:r,agents:d.agents,allowedAgentTypes:d.allowedAgentTypes,model:d.model}))),v=_.filter(e=>{let t=e;return t.type!==`advisor_20260301`&&t.type!==`computer_20250124`}),y=E(g,n),T=w(v),O=S(d.toolChoice),A=a({maxRetries:0,fetchOverride:d.fetchOverride,source:d.querySource});i(`[Grok] Calling model=${h}, messages=${y.length}, tools=${T.length}`);let j=x(await A.chat.completions.create({model:h,messages:y,...T.length>0&&{tools:T,...O&&{tool_choice:O}},stream:!0,stream_options:{include_usage:!0},...d.temperatureOverride!==void 0&&{temperature:d.temperatureOverride}},{signal:u}),h),M={},N=[],P=null,F={input_tokens:0,output_tokens:0,cache_creation_input_tokens:0,cache_read_input_tokens:0},I=0,L=Date.now();for await(let e of j){switch(e.type){case`message_start`:P=e.message,I=Date.now()-L,e.message.usage&&(F=D(F,e.message.usage));break;case`content_block_start`:{let t=e.index,n=e.content_block;n.type===`tool_use`?M[t]={...n,input:``}:n.type===`text`?M[t]={...n,text:``}:n.type===`thinking`?M[t]={...n,thinking:``,signature:``}:M[t]={...n};break}case`content_block_delta`:{let t=e.index,n=e.delta,r=M[t];if(!r)break;n.type===`text_delta`?r.text=(r.text||``)+n.text:n.type===`input_json_delta`?r.input=(r.input||``)+n.partial_json:n.type===`thinking_delta`?r.thinking=(r.thinking||``)+n.thinking:n.type===`signature_delta`&&(r.signature=n.signature);break}case`content_block_stop`:{let t=M[e.index];if(!t||!P)break;let n={message:{...P,content:p([t],r,d.agentId)},requestId:void 0,type:`assistant`,uuid:k(),timestamp:new Date().toISOString()};N.push(n),yield n;break}case`message_delta`:{let t=e.usage;t&&(F=D(F,t));break}case`message_stop`:break}e.type===`message_stop`&&F.input_tokens+F.output_tokens>0&&l(t(h,F),F,d.model),yield{type:`stream_event`,event:e,...e.type===`message_start`?{ttftMs:I}:void 0}}m(d.langfuseTrace??null,{model:h,provider:`grok`,input:f(g,n),output:o(N),usage:{input_tokens:F.input_tokens,output_tokens:F.output_tokens,cache_creation_input_tokens:F.cache_creation_input_tokens,cache_read_input_tokens:F.cache_read_input_tokens},startTime:new Date(L),endTime:new Date,completionStartTime:I>0?new Date(L+I):void 0,tools:s(_)})}catch(e){let t=e instanceof Error?e.message:String(e);i(`[Grok] Error: ${t}`,{level:`error`}),yield g({content:`API Error: ${t}`,apiError:`api_error`,error:e instanceof Error?e:Error(String(e))})}}e((()=>{d(),O(),T(),u(),v(),r(),h(),n(),y(),_()}))();export{A as queryModelGrok};