@urun-sh/openai 0.2.59 → 0.2.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{ResponsesClient-BDl1OIRP.d.cts → ResponsesClient-y8g6OfNN.d.cts} +2 -0
- package/dist/{ResponsesClient-BDl1OIRP.d.ts → ResponsesClient-y8g6OfNN.d.ts} +2 -0
- package/dist/chunk-4FUBGLFN.js +41 -0
- package/dist/chunk-SSZL77P5.js +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/pi-extension/index.cjs +5 -5
- package/dist/pi-extension/index.d.cts +1 -1
- package/dist/pi-extension/index.d.ts +1 -1
- package/dist/pi-extension/index.js +6 -6
- package/dist/pi-extension/standalone.cjs +2 -2
- package/dist/proxy/cli.cjs +15 -15
- package/dist/proxy/cli.js +1 -1
- package/dist/proxy/index.cjs +13 -13
- package/dist/proxy/index.d.cts +7 -0
- package/dist/proxy/index.d.ts +3 -0
- package/dist/proxy/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-62IL5R2N.js +0 -1
- package/dist/chunk-TEZ4WLY6.js +0 -41
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import{b as V,c as me}from"./chunk-VXTNG2TP.js";var Ce="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",We=1007,Ee=1008,H=1011,ge=1008,i=class extends Error{constructor(e,o=We){super(e);this.closeCode=o}closeCode},x=t=>typeof t=="object"&&t!==null&&!Array.isArray(t)?t:null,ae=["setup","clientContent","realtimeInput","toolResponse"];function Te(t){let n;try{n=JSON.parse(t)}catch{throw new i("client message is not valid JSON")}let e=x(n);if(!e)throw new i("client message must be a JSON object");let o=ae.filter(u=>e[u]!==void 0),s=Object.keys(e).filter(u=>!ae.includes(u));if(s.length>0)throw new i(`unknown client message field(s) ${JSON.stringify(s)} \u2014 expected exactly one of ${ae.join(", ")}`);if(o.length!==1)throw new i(`client message must contain exactly one of ${ae.join(", ")} (got ${o.length})`);let r=o[0],a=x(e[r]);if(!a)throw new i(`"${r}" must be a JSON object`);return{kind:r,[r]:a}}function Ne(t,n){if(typeof t=="string")return t;let e=x(t),o=e?e.parts:void 0;if(!Array.isArray(o))throw new i(`${n}: expected a Content with a parts array`);let s=[];for(let r of o){let a=x(r);if(a&&typeof a.text=="string"){s.push(a.text);continue}throw a&&(a.inlineData!==void 0||a.fileData!==void 0)?new i(`${n}: media parts (inlineData/fileData) are a declared follow-up slice (audio/video lanes over the proxy transport AudioBridge) \u2014 only text parts are supported in this slice`):a&&(a.functionCall!==void 0||a.functionResponse!==void 0)?new i(`${n}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new i(`${n}: unsupported part ${JSON.stringify(r)}`)}return s.join(`
|
|
2
|
+
`)}var Xe={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},Qe={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Ze=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption"],Ie=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function et(t){let n=x(t);if(!n)throw new i("setup.realtimeInputConfig must be an object");for(let s of Object.keys(n))if(!(s==="automaticActivityDetection"||s==="activityHandling"))throw new i(s==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${s}"`);let e=x(n.automaticActivityDetection);if(n.automaticActivityDetection!==void 0&&!e)throw new i("realtimeInputConfig.automaticActivityDetection must be an object");for(let s of Object.keys(e??{}))if(s!=="disabled")throw new i(`automaticActivityDetection.${s} tunes AUTOMATIC (server-side) activity detection \u2014 the declared platform follow-up (serve turn_detection events over the session stream lane); only "disabled: true" (manual activity mode) is supported in this slice`);if(e?.disabled!==!0)throw new i("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let o="START_OF_ACTIVITY_INTERRUPTS";if(n.activityHandling!==void 0){if(!Ie.includes(n.activityHandling))throw new i(`unsupported activityHandling ${JSON.stringify(n.activityHandling)} \u2014 expected one of ${Ie.join(", ")}`);o=n.activityHandling}return{manualActivity:!0,activityHandling:o}}var tt=["temperature","maxOutputTokens","responseModalities"];function Pe(t){for(let[c,g]of Object.entries(Qe))if(t[c]!==void 0)throw new i(g);for(let c of Object.keys(t))if(!Ze.includes(c))throw new i(`unsupported setup field "${c}"`);if(typeof t.model!="string"||t.model.length===0)throw new i('setup.model is required (format "models/{model}")');let n=t.model.replace(/^models\//,""),e,o,s=!1,r=x(t.generationConfig);if(t.generationConfig!==void 0&&!r)throw new i("setup.generationConfig must be an object");if(r){for(let c of Object.keys(r))if(!tt.includes(c))throw new i(`unsupported generationConfig field "${c}"`);if(r.temperature!==void 0){if(typeof r.temperature!="number")throw new i("generationConfig.temperature must be a number");e=r.temperature}if(r.maxOutputTokens!==void 0){if(typeof r.maxOutputTokens!="number")throw new i("generationConfig.maxOutputTokens must be a number");o=r.maxOutputTokens}if(r.responseModalities!==void 0){let c=Array.isArray(r.responseModalities)?r.responseModalities:[r.responseModalities];for(let g of c){let p=String(g).toUpperCase();if(p==="AUDIO"){s=!0;continue}if(p!=="TEXT")throw new i(`responseModalities ${JSON.stringify(g)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge rt-audio-out lane)`)}}}let a=[];t.systemInstruction!==void 0&&a.push({role:"system",content:Ne(t.systemInstruction,"setup.systemInstruction")});let u;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new i("setup.tools must be an array");u=[];for(let c of t.tools){let g=x(c);if(!g)throw new i("setup.tools entries must be objects");for(let p of Object.keys(g))if(p!=="functionDeclarations")throw new i(`unsupported tool "${p}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(g.functionDeclarations))throw new i("tool.functionDeclarations must be an array");for(let p of g.functionDeclarations){let h=x(p);if(!h||typeof h.name!="string")throw new i("functionDeclarations entries require a string name");u.push({type:"function",name:h.name,description:h.description,parameters:h.parameters})}}u.length===0&&(u=void 0)}let f={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let c=x(t.sessionResumption);if(!c)throw new i("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let g of Object.keys(c))if(g!=="handle")throw new i(`unsupported sessionResumption field "${g}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(c.handle!==void 0&&(typeof c.handle!="string"||c.handle.length===0))throw new i("sessionResumption.handle must be a non-empty string when present");f={enabled:!0,handle:typeof c.handle=="string"?c.handle:null}}let d=t.realtimeInputConfig===void 0?Xe:et(t.realtimeInputConfig);return{model:n,systemItems:a,tools:u,temperature:e,maxOutputTokens:o,audioModality:s,activity:d,sessionResumption:f}}function Me(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function $e(t){let n=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(n*1e3)/1e3}s`}}}function Le(t){for(let o of Object.keys(t))if(o!=="turns"&&o!=="turnComplete")throw new i(`unsupported clientContent field "${o}"`);let n=t.turns===void 0?[]:t.turns;if(!Array.isArray(n))throw new i("clientContent.turns must be an array");let e=[];for(let o of n){let s=x(o);if(!s)throw new i("clientContent.turns entries must be Content objects");let r=s.role===void 0?"user":String(s.role).toLowerCase();if(r!=="user"&&r!=="model")throw new i(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(s.role)})`);e.push({role:r==="model"?"assistant":"user",content:Ne(s,"clientContent.turns")})}return{items:e,turnComplete:t.turnComplete===!0}}var ue=16e3,Ue=24e3,nt=`audio/pcm;rate=${Ue}`,je=Buffer.alloc(960).toString("base64");function ot(t,n,e){if(n===e)return t;let o=Math.floor(t.length*e/n),s=new Int16Array(o),r=n/e,a=t.length-1;for(let u=0;u<o;u++){let f=u*r,d=Math.floor(f);if(d>=a){s[u]=t[a];continue}let c=f-d;s[u]=Math.round(t[d]+(t[d+1]-t[d])*c)}return s}function st(t){let n=x(t);if(!n)throw new i("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof n.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(n.mimeType))throw new i(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(n.mimeType)}`);if(typeof n.data!="string"||n.data.length===0)throw new i("realtimeInput.video.data must be non-empty base64");let e=Buffer.from(n.data,"base64");if(e.length<3||e[0]!==255||e[1]!==216)throw new i("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(e)}function rt(t){let n=x(t);if(!n)throw new i("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof n.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(n.mimeType))throw new i(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(n.mimeType)}`);let e=/;\s*rate=(\d+)/.exec(n.mimeType);if(e&&Number(e[1])!==ue)throw new i(`realtimeInput.audio.mimeType declares rate=${e[1]} \u2014 the Live spec input format is ${ue} Hz (audio/pcm;rate=${ue})`);if(typeof n.data!="string"||n.data.length===0)throw new i("realtimeInput.audio.data must be non-empty base64");let o=Buffer.from(n.data,"base64");if(o.length===0||o.length%2!==0)throw new i(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${o.length} bytes \u2014 not even)`);let s=new Int16Array(o.length/2);for(let a=0;a<s.length;a++)s[a]=o.readInt16LE(a*2);let r=ot(s,ue,Ue);return Buffer.from(r.buffer,r.byteOffset,r.byteLength).toString("base64")}function Be(t){if(t.mediaChunks!==void 0)throw new i('realtimeInput.mediaChunks is deprecated by the Live API \u2014 send the audio stream via realtimeInput.audio ({mimeType:"audio/pcm;rate=16000", data}) instead');if(t.activityStart!==void 0||t.activityEnd!==void 0){if(Object.keys(t).length!==1||t.activityStart!==void 0&&t.activityEnd!==void 0)throw new i("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let s=t.activityStart!==void 0?"activityStart":"activityEnd",r=x(t[s]);if(!r||Object.keys(r).length!==0)throw new i(`realtimeInput.${s} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:s}}let n=["text","audio","audioStreamEnd","video"];for(let o of Object.keys(t))if(!n.includes(o))throw new i(`unsupported realtimeInput field "${o}"`);let e=n.filter(o=>t[o]!==void 0);if(e.length!==1)throw new i(`realtimeInput must carry exactly one of ${n.join(", ")} (got ${e.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:rt(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:st(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new i("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new i("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function qe(t){for(let n of Object.keys(t))if(n!=="functionResponses")throw new i(`unsupported toolResponse field "${n}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new i("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(n=>{let e=x(n);if(!e||typeof e.id!="string"||e.id.length===0)throw new i("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:e.id,output:JSON.stringify(e.response??null)}})}function De(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function Je(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:nt,data:t}}]}}}}function He(t){let n=x(t)??{},e=Array.isArray(n.output)?n.output:[],o=[],s=[],r=typeof n.output_text=="string"?n.output_text:"";if(!r)for(let d of e){let c=x(d);if(!(c?.type!=="message"||!Array.isArray(c.content)))for(let g of c.content){let p=x(g);p?.type==="output_text"&&typeof p.text=="string"&&(r+=p.text)}}r&&s.push({role:"assistant",content:r});for(let d of e){let c=x(d);if(c?.type!=="function_call")continue;if(typeof c.call_id!="string"||typeof c.name!="string")throw new i(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,H);let g=typeof c.arguments=="string"&&c.arguments.length>0?c.arguments:"{}",p;try{p=JSON.parse(g)}catch{throw new i(`upstream function_call arguments are not valid JSON: ${g}`,H)}o.push({id:c.call_id,name:c.name,args:p}),s.push({type:"function_call",call_id:c.call_id,name:c.name,arguments:g})}let a=[];o.length>0&&a.push({toolCall:{functionCalls:o}}),a.push({serverContent:{generationComplete:!0}});let u=x(n.usage),f={serverContent:{turnComplete:!0}};if(u){let d={};typeof u.input_tokens=="number"&&(d.promptTokenCount=u.input_tokens),typeof u.output_tokens=="number"&&(d.responseTokenCount=u.output_tokens),typeof u.total_tokens=="number"&&(d.totalTokenCount=u.total_tokens),Object.keys(d).length>0&&(f.usageMetadata=d)}return a.push(f),{messages:a,assistantItems:s}}import{createServer as ct}from"http";import{randomUUID as it}from"crypto";import{WebSocketServer as at}from"ws";var ut=256,he=class{snapshots=new Map;store(n,e){for(this.snapshots.set(n,e);this.snapshots.size>ut;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(n){return this.snapshots.get(n)}},ne=t=>t.slice(0,120),ye=class{constructor(n,e,o){this.ws=n;this.clients=e;this.registry=o;n.on("message",s=>this.onMessage(s)),n.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.()})}ws;clients;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;video=null;activityOpen=!1;activeTurn=null;send(n){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(n))}fail(n){if(n instanceof V){this.ws.close(Ee,ne(n.message));return}if(n instanceof me){this.ws.close(ge,ne(n.message));return}if(n instanceof i){this.ws.close(n.closeCode,ne(n.message));return}this.ws.close(H,ne(String(n instanceof Error?n.message:n)))}onMessage(n){try{let e=Te(typeof n=="string"?n:Buffer.concat(Array.isArray(n)?n:[n]).toString("utf8"));if(e.kind==="setup"){this.handleSetup(e.setup).catch(s=>this.fail(s));return}if(!this.config)throw new i(`"${e.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let o=this.config;if(e.kind==="clientContent"){let{items:s,turnComplete:r}=Le(e.clientContent);this.transcript.push(...s),r&&this.enqueue(()=>this.runTurn());return}if(e.kind==="realtimeInput"){let s=Be(e.realtimeInput);if(s.kind==="activityStart"||s.kind==="activityEnd"){this.onActivity(s.kind,o);return}if(s.kind==="text"){this.transcript.push({role:"user",content:s.text}),this.enqueue(()=>this.runTurn());return}if(s.kind==="video"){this.appendVideoFrame(s.jpegFrame);return}this.appendAudio(s.kind==="audio"?s.base64Pcm24k:je);return}this.transcript.push(...qe(e.toolResponse)),this.enqueue(()=>this.runTurn())}catch(e){this.fail(e)}}async handleSetup(n){if(this.config)throw new i("setup may only be sent once, as the first client message");let e=Pe(n),o=e.sessionResumption.handle;if(o!==null){let s=this.registry.get(o);if(!s)throw new i("unknown sessionResumption.handle \u2014 the Live session it names is gone (proxy restarted, handle evicted, or never issued) and cannot be resumed; reconnect without a handle to start a new session",ge);if(s.model!==e.model)throw new i(`sessionResumption.handle was issued for model "${s.model}" \u2014 the model cannot change on resume (got "${e.model}", per the Live API session-resumption contract)`);if(e.systemItems.length>0)throw new i("setup.systemInstruction cannot be changed on resume \u2014 it is part of the resumed conversation state");this.transcript=[...s.transcript],this.proxyHandle=s.proxyHandle,this.unsubscribeEnd=await this.clients.onSessionEnd(s.proxyHandle,r=>this.enqueue(()=>this.handleUpstreamEnd(r)))}else this.proxyHandle=await this.clients.sessionHandle(e.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.transcript.push(...e.systemItems);this.config=e,this.send({setupComplete:{}}),e.audioModality&&this.ensureAudioLane(),e.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let n=this.config;if(!n)throw new i("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let e=this.clients.openAudio;if(!e)throw new i("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native rt-audio-in/rt-audio-out AudioBridge seam)",H);return this.audio=(async()=>{let o=await e.call(this.clients,n.model);return this.detachAudio=o.onOutputAudio(s=>this.send(Je(s))),o})(),this.audio.catch(o=>this.fail(o)),this.audio}appendAudio(n){this.ensureAudioLane().then(e=>e.appendInputAudio(n)).catch(e=>this.fail(e))}ensureVideoLane(){if(this.video)return this.video;let n=this.config;if(!n)throw new i("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let e=this.clients.openVideo;if(!e)throw new i("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",H);return this.video=Promise.resolve(e.call(this.clients,n.model)),this.video.catch(o=>this.fail(o)),this.video}appendVideoFrame(n){this.ensureVideoLane().then(e=>e.sendInputFrame(n)).catch(e=>this.fail(e))}onActivity(n,e){if(!e.activity.manualActivity)throw new i(`realtimeInput.${n} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(n==="activityStart"){if(this.activityOpen)throw new i("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,e.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new i("activityEnd without an open activity window (send activityStart first)");this.activityOpen=!1}enqueue(n){this.queue=this.queue.then(n,()=>{}),this.queue=this.queue.catch(e=>this.fail(e))}async mintResumptionUpdate(){let n=this.config;if(!n?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let e=await this.clients.sessionHandle(n.model);this.proxyHandle=e;let o=it();this.registry.store(o,{model:n.model,proxyHandle:e,transcript:[...this.transcript]}),this.send(Me(o))}async handleUpstreamEnd(n){if(this.ws.readyState!==this.ws.OPEN||!this.config)return;this.unsubscribeEnd?.(),this.unsubscribeEnd=null;let e=null;try{e=await this.clients.sessionHandle(this.config.model)}catch{}if(e!==null&&e!==this.proxyHandle){this.proxyHandle=e,this.unsubscribeEnd=await this.clients.onSessionEnd(e,o=>this.enqueue(()=>this.handleUpstreamEnd(o))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send($e(n.timeLeftMs)),this.ws.close(H,ne(`upstream session ended: ${n.reason}`))}async runTurn(){let n=this.config;if(!n||this.ws.readyState!==this.ws.OPEN)return;let e={interrupted:!1};this.activeTurn=e;try{let o=await this.clients.createResponse({model:n.model,input:[...this.transcript],stream:!0,tools:n.tools,temperature:n.temperature,max_output_tokens:n.maxOutputTokens}),s,r=!1;for await(let f of o){if(e.interrupted)break;let d=f;if(d.type==="error")throw new i(`upstream error: ${JSON.stringify(d)}`,H);if(d.type==="response.completed"){s=d.response,r=!0;continue}let c=De(d);c&&this.send(c)}if(e.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!r)throw new i("upstream produced no response.completed event",H);let{messages:a,assistantItems:u}=He(s);this.transcript.push(...u);for(let f of a)this.send(f);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},lt=(t,n)=>{let e=t.headers["x-goog-api-key"];if(typeof e=="string")return e;let o=t.headers.authorization;return typeof o=="string"&&o.startsWith("Bearer ")?o.slice(7):n.searchParams.get("key")??void 0};function Fe(t,n){let e=new at({noServer:!0}),o=new he;t.on("upgrade",(s,r,a)=>{let u=new URL(s.url??"/","http://localhost");if(u.pathname!==Ce){r.write(`HTTP/1.1 404 Not Found\r
|
|
3
|
+
Connection: close\r
|
|
4
|
+
\r
|
|
5
|
+
no WS route for ${u.pathname}`),r.destroy();return}if(n.apiKey&<(s,u)!==n.apiKey){r.write(`HTTP/1.1 401 Unauthorized\r
|
|
6
|
+
Connection: close\r
|
|
7
|
+
\r
|
|
8
|
+
invalid local proxy api key`),r.destroy();return}e.handleUpgrade(s,r,a,f=>{new ye(f,n.clients,o)})})}var ke=class t{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(n,e,o){this.requests[n]=(this.requests[n]??0)+1,this.localInBytes+=e,this.naiveInBytes+=o}modelEntry(n){let e=this.models[n];if(e)return e;let o=Object.keys(this.models).length>=dt?"(other)":n;return this.models[o]??={requests:0,tokens_out:0}}model(n){this.modelEntry(n).requests+=1}track(n,e){let o=this,s=performance.now(),r=null,a=0;return(async function*(){for await(let f of n)f.type==="response.output_text.delta"&&typeof f.delta=="string"&&(r===null&&(r=performance.now(),o.ttftMs.push(r-s),o.ttftMs.length>512&&o.ttftMs.shift()),a+=1,o.tokensOut+=1,o.charsOut+=f.delta.length,e!==void 0&&(o.modelEntry(e).tokens_out+=1)),yield f;let u=(performance.now()-(r??s))/1e3;a>0&&u>0&&(o.tokRates.push(a/u),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(n){if(!n.length)return null;let e=[...n].sort((o,s)=>o-s);return e[Math.floor(e.length/2)]}snapshot(){let n=Object.values(this.requests).reduce((e,o)=>e+o,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:t.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:t.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:n},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}};function xe(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function q(t,n,e){t.writeHead(n,{"content-type":"application/json"}),t.end(JSON.stringify(e))}function F(t,n,e,o="invalid_request_error"){q(t,n,{error:{message:e,type:o}})}function B(t,n,e,o){q(t,n,{type:"error",error:{type:e,message:o}})}function Re(t,n,e){if(n==="anthropic"){q(t,404,{type:"error",error:{type:"not_found_error",message:e.message}});return}q(t,404,{error:{message:e.message,type:"invalid_request_error",code:"model_not_found"}})}var dt=256,pt=128;function Ae(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,pt)}var Ge=64*1024*1024,le=class extends Error{};async function ft(t){let n=[],e=0;for await(let s of t){if(e+=s.length,e>Ge)throw new le(`request body exceeds ${Ge} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function W(t,n,e,o){return{id:t,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:n,choices:[{index:0,delta:e,finish_reason:o}]}}var mt=256;function gt(t){if(typeof t=="string")return[{role:"user",content:t}];if(Array.isArray(t))return t;throw new Error("responses input must be a string or an array of messages/items")}var ve=class{conversations=new Map;thread(n,e){let o=gt(e);if(n==null)return o;let s=this.conversations.get(String(n));if(!s)throw new Error(`unknown previous_response_id ${String(n)} (proxy restarts drop stored responses)`);return[...s,...o]}remember(n,e,o){this.conversations.set(n,[...e,{role:"assistant",content:o}]);for(let s of this.conversations.keys()){if(this.conversations.size<=mt)break;this.conversations.delete(s)}}};function oe(t){let n=t?.output;return Array.isArray(n)?n.filter(e=>e.type==="function_call"):[]}function z(t){return t.find(n=>n.type==="response.completed")?.response??null}function ce(t){let n=t?.usage;if(!n||typeof n!="object")return null;let e=a=>typeof a=="number"&&Number.isFinite(a)&&a>=0?a:0,o=e(n.input_tokens),s=e(n.output_tokens);if(o===0&&s===0)return null;let r=e(n.total_tokens)||o+s;return{input_tokens:o,output_tokens:s,total_tokens:r}}function Ve(t){let n=ce(t);return n?{prompt_tokens:n.input_tokens,completion_tokens:n.output_tokens,total_tokens:n.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function Ke(t){let n=t?.output;if(!Array.isArray(n))return"";let e="";for(let o of n)if(!(o.type!=="reasoning"||!Array.isArray(o.content)))for(let s of o.content)s.type==="reasoning_text"&&(e+=String(s.text??""));return e}function se(t){return t.type!=="response.function_call_arguments.delta"||typeof t.tool_index!="number"?null:{tool_index:t.tool_index,call_id:typeof t.call_id=="string"?t.call_id:void 0,name:typeof t.name=="string"?t.name:void 0,delta:typeof t.delta=="string"?t.delta:""}}var K=class{calls=new Map;add(n){let e=this.calls.get(n.tool_index),o=!e;return e||(e={args:""},this.calls.set(n.tool_index,e)),n.call_id&&(e.id=n.call_id),n.name&&(e.name=n.name),e.args+=n.delta,{call:e,isNew:o}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([n,e])=>({type:"function_call",call_id:e.id??`call_${n}`,name:e.name??"",arguments:e.args}))}},ht=["<tool_call>","<function="];function yt(t){return ht.find(n=>t.includes(n))}function X(t,n){return`serving row emitted a prose tool call (literal ${JSON.stringify(n)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${t}" (see catalog parser_defaults)`}function Q(t,n,e){if(!(!t||n>0))return yt(e)}function _t(t){if(Array.isArray(t))return t.map(n=>{let e=n.function;return!e||typeof e!="object"?n:{type:"function",name:e.name,description:e.description,parameters:e.parameters,strict:e.strict}})}var de=class extends Error{};function wt(t,n){return Array.isArray(n)?n.map(e=>{let o=e;if(o.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:o.text};if(o.type==="image_url"){let s=o.image_url??{};return{type:"input_image",image_url:s.url,detail:s.detail}}throw new de(`unsupported chat content part type "${String(o.type)}" \u2014 the proxy translates text and image_url parts`)}):n}function kt(t){let n=[];for(let e of t){if(e.role==="tool"){n.push({type:"function_call_output",call_id:e.tool_call_id,output:typeof e.content=="string"?e.content:JSON.stringify(e.content??"")});continue}if(e.role==="assistant"&&Array.isArray(e.tool_calls)){typeof e.content=="string"&&e.content&&n.push({role:"assistant",content:e.content});for(let o of e.tool_calls){let s=o.function??{};n.push({type:"function_call",call_id:o.id,name:s.name,arguments:s.arguments})}continue}n.push({...e,content:wt(e.role,e.content)})}return n}function _e(t){return oe(t).map((n,e)=>({index:e,id:n.call_id??n.id??`call_${e}`,type:"function",function:{name:n.name??"",arguments:n.arguments??"{}"}}))}function ze(t,n){let e=n?.output_text;return typeof e=="string"?e:t.filter(o=>o.type==="response.output_text.delta"&&typeof o.delta=="string").map(o=>o.delta).join("")}async function vt(t,n,e,o,s){let r=t.stream===!0,a;try{a=e.thread(t.previous_response_id,t.input)}catch(m){F(o,400,String(m instanceof Error?m.message:m));return}t.previous_response_id!=null&&(s.naiveInBytes+=Buffer.byteLength(JSON.stringify(a))-Buffer.byteLength(JSON.stringify(t.input)));let u=`resp_${Math.random().toString(36).slice(2,14)}`,f=t.store!==!1,d=Ae(t);s.model(d);let c=Array.isArray(t.tools)&&t.tools.length>0,g;try{g=await n.createResponse({model:t.model,input:a,stream:r,tools:t.tools,tool_choice:t.tool_choice,temperature:t.temperature,max_output_tokens:t.max_output_tokens,...t.reasoning_effort!==void 0?{reasoning_effort:t.reasoning_effort}:{},...t.chat_template_kwargs!==void 0?{chat_template_kwargs:t.chat_template_kwargs}:{}})}catch(m){if(m instanceof V){Re(o,"openai",m);return}throw m}let p=[];if(r){xe(o);let m=y=>{o.write(`event: ${String(y.type??"message")}
|
|
9
|
+
data: ${JSON.stringify(y)}
|
|
10
|
+
|
|
11
|
+
`)},P=y=>y.response&&typeof y.response=="object"?{...y,response:{...y.response,id:u}}:y,S=`item_${u.slice(5)}`,M=`rs_${u.slice(5)}`,E=!1,$=!1,l=0,k=()=>{E||(E=!0,m({type:"response.created",response:{id:u,object:"response",status:"in_progress"}}))},U=!1,C=0,L="",j=()=>{U&&(U=!1,m({type:"response.reasoning_text.done",item_id:M,output_index:C,content_index:0,text:L}),m({type:"response.output_item.done",output_index:C,item:{id:M,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:L}],status:"completed"}}))},T=!1,w=0,A="",D=y=>{T&&(T=!1,m({type:"response.output_text.done",item_id:S,output_index:w,content_index:0,text:A}),m({type:"response.content_part.done",item_id:S,output_index:w,content_index:0,part:{type:"output_text",text:A}}),m({type:"response.output_item.done",output_index:w,item:{id:S,type:"message",role:"assistant",status:y,content:[{type:"output_text",text:A}]}}))},J=new K,v=null,Y=-1,Z=y=>{if(!v)return;let R=v;v=null,m({type:"response.function_call_arguments.done",item_id:R.fcId,output_index:R.outputIndex,arguments:R.args}),m({type:"response.output_item.done",output_index:R.outputIndex,item:{id:R.fcId,type:"function_call",call_id:R.callId??R.fcId,name:R.name??"",arguments:R.args,status:y}})},ee=!1;for await(let y of s.track(g,d)){p.push(y);let R=String(y.type??"");if(R==="response.created"||R==="response.in_progress"){E=!0,m(P(y));continue}if(R==="response.output_item.added"){$=!0,m(P(y));continue}if(R==="response.reasoning_text.delta"&&!$){k(),U||(U=!0,C=l++,m({type:"response.output_item.added",output_index:C,item:{id:M,type:"reasoning",summary:[],content:[],status:"in_progress"}})),L+=String(y.delta??""),m({type:"response.reasoning_text.delta",item_id:M,output_index:C,content_index:0,delta:y.delta});continue}if(R==="response.output_text.delta"&&!$){k(),j(),T||(T=!0,w=l++,m({type:"response.output_item.added",output_index:w,item:{id:S,type:"message",role:"assistant",status:"in_progress",content:[]}}),m({type:"response.content_part.added",item_id:S,output_index:w,content_index:0,part:{type:"output_text",text:""}})),A+=String(y.delta??""),m({type:R,item_id:S,output_index:w,content_index:0,delta:y.delta});continue}let I=$?null:se(y);if(I){if(k(),j(),D("completed"),J.add(I),I.tool_index!==Y){Z("completed"),Y=I.tool_index;let _=l++;v={fcId:`fc_${u.slice(5)}_${I.tool_index}`,outputIndex:_,callId:I.call_id,name:I.name,args:""},m({type:"response.output_item.added",output_index:_,item:{id:v.fcId,type:"function_call",call_id:v.callId??v.fcId,name:v.name??"",arguments:"",status:"in_progress"}})}v&&(I.call_id&&(v.callId=I.call_id),I.name&&(v.name=I.name),v.args+=I.delta,m({type:"response.function_call_arguments.delta",item_id:v.fcId,output_index:v.outputIndex,delta:I.delta}));continue}if(R==="response.completed"&&!$){let _=oe(y.response),b=Q(c,J.size+_.length,A);if(b){ee=!0,m({type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:X(d,b)}});break}j(),D("completed"),Z("completed"),J.size===0&&_.forEach(G=>{let te=l++,ie=G.id??G.call_id??`fc_${te}`,fe=G.arguments??"",Se={id:ie,type:"function_call",call_id:G.call_id??ie,name:G.name??""};m({type:"response.output_item.added",output_index:te,item:{...Se,arguments:"",status:"in_progress"}}),m({type:"response.function_call_arguments.delta",item_id:ie,output_index:te,delta:fe}),m({type:"response.function_call_arguments.done",item_id:ie,output_index:te,arguments:fe}),m({type:"response.output_item.done",output_index:te,item:{...Se,arguments:fe,status:"completed"}})}),m(P(y));continue}m(P(y))}j(),D("incomplete"),Z("incomplete"),f&&!ee&&e.remember(u,a,ze(p,z(p))),o.write(`data: [DONE]
|
|
12
|
+
|
|
13
|
+
`),o.end();return}let h=null;for await(let m of s.track(g,d)){if(p.push(m),m.type==="error"){F(o,502,JSON.stringify(m),"upstream_error");return}m.type==="response.completed"&&(h=m.response)}if(h==null){F(o,502,"upstream produced no response.completed event","upstream_error");return}let O=ze(p,h),N=Q(c,oe(h).length,O);if(N){F(o,502,X(d,N),"upstream_error");return}f&&e.remember(u,a,O),q(o,200,{...h,id:u})}async function xt(t,n,e,o){let s=t.messages;if(!Array.isArray(s)){F(e,400,"chat/completions requires a messages array");return}let r=String(t.model??"urun"),a=`chatcmpl-${Math.random().toString(36).slice(2,14)}`,u;try{u=kt(s)}catch(l){if(l instanceof de){F(e,400,l.message);return}throw l}let f=Ae(t);o.model(f);let d=Array.isArray(t.tools)&&t.tools.length>0,c;try{c=await n.createResponse({model:t.model,input:u,stream:!0,tools:_t(t.tools),tool_choice:t.tool_choice,temperature:t.temperature,max_output_tokens:t.max_completion_tokens??t.max_tokens,...t.reasoning_effort!==void 0?{reasoning_effort:t.reasoning_effort}:{},...t.chat_template_kwargs!==void 0?{chat_template_kwargs:t.chat_template_kwargs}:{}})}catch(l){if(l instanceof V){Re(e,"openai",l);return}throw l}if(t.stream===!0){xe(e);let l=[],k=new K,U="";e.write(`data: ${JSON.stringify(W(a,r,{role:"assistant"},null))}
|
|
14
|
+
|
|
15
|
+
`);for await(let w of o.track(c,f)){l.push(w);let A=se(w);if(w.type==="response.output_text.delta"&&typeof w.delta=="string")U+=w.delta,e.write(`data: ${JSON.stringify(W(a,r,{content:w.delta},null))}
|
|
16
|
+
|
|
17
|
+
`);else if(w.type==="response.reasoning_text.delta"&&typeof w.delta=="string")e.write(`data: ${JSON.stringify(W(a,r,{reasoning_content:w.delta},null))}
|
|
18
|
+
|
|
19
|
+
`);else if(A){let{call:D,isNew:J}=k.add(A),v=J?{index:A.tool_index,id:D.id??`call_${A.tool_index}`,type:"function",function:{name:D.name??"",arguments:A.delta}}:{index:A.tool_index,function:{arguments:A.delta}};e.write(`data: ${JSON.stringify(W(a,r,{tool_calls:[v]},null))}
|
|
20
|
+
|
|
21
|
+
`)}else if(w.type==="error"){e.write(`data: ${JSON.stringify({error:w})}
|
|
22
|
+
|
|
23
|
+
`),e.end();return}}let C=_e(z(l)),L=Q(d,k.size+C.length,U);if(L){e.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:X(f,L)}})}
|
|
24
|
+
|
|
25
|
+
`),e.end();return}k.size===0&&C.length>0&&e.write(`data: ${JSON.stringify(W(a,r,{tool_calls:C},null))}
|
|
26
|
+
|
|
27
|
+
`);let j=k.size>0||C.length>0;e.write(`data: ${JSON.stringify(W(a,r,{},j?"tool_calls":"stop"))}
|
|
28
|
+
|
|
29
|
+
`),ce(z(l))&&e.write(`data: ${JSON.stringify({id:a,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:r,choices:[],usage:Ve(z(l))})}
|
|
30
|
+
|
|
31
|
+
`),e.write(`data: [DONE]
|
|
32
|
+
|
|
33
|
+
`),e.end();return}let g="",p="",h=[],O=new K;for await(let l of o.track(c,f)){h.push(l),l.type==="response.output_text.delta"&&typeof l.delta=="string"&&(g+=l.delta),l.type==="response.reasoning_text.delta"&&typeof l.delta=="string"&&(p+=l.delta);let k=se(l);if(k&&O.add(k),l.type==="error"){F(e,502,JSON.stringify(l),"upstream_error");return}}let N=z(h),m=_e(N),P=m.length>0?m:_e({output:O.items()}),S=Q(d,P.length,g);if(S){F(e,502,X(f,S),"upstream_error");return}let M=P.map(({index:l,...k})=>k),E={role:"assistant",content:g||null},$=p||Ke(N);$&&(E.reasoning_content=$),M.length>0&&(E.tool_calls=M),q(e,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:r,choices:[{index:0,message:E,finish_reason:M.length>0?"tool_calls":"stop"}],usage:Ve(N)})}function Rt(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(n=>n.type==="text").map(n=>String(n.text??"")).join(""):""}var re=class extends Error{};function pe(t){return new re(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function At(t){if(Array.isArray(t))return t.map(n=>{let e=n;return{type:"function",name:e.name,description:e.description,parameters:e.input_schema}})}function St(t){if(t==null)return;let n=t.type;if(n==="auto")return"auto";if(n==="none")return"none";if(n==="any")return"required";if(n==="tool")return{type:"function",name:t.name};throw new re(`unsupported anthropic tool_choice type ${JSON.stringify(String(n))} \u2014 the proxy maps auto, none, any and tool`)}function It(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw pe(typeof t);let n="";for(let e of t){let o=String(e.type??"");if(o!=="text")throw pe(`tool_result > ${o}`);n+=String(e.text??"")}return n}function bt(t){let{role:n,content:e}=t;if(typeof e=="string")return[{role:n,content:e}];if(e==null)return[];if(!Array.isArray(e))throw pe(typeof e);let o=[],s="",r=()=>{s&&(o.push({role:n,content:s}),s="")};for(let a of e){let u=String(a.type??"");if(u==="text")s+=String(a.text??"");else if(u==="tool_use")r(),o.push({type:"function_call",call_id:a.id,name:a.name,arguments:JSON.stringify(a.input??{})});else if(u==="tool_result")r(),o.push({type:"function_call_output",call_id:a.tool_use_id,output:It(a.content)});else{if(u==="thinking"||u==="redacted_thinking")continue;throw pe(u)}}return r(),o}function we(t){return oe(t).map((n,e)=>{let o;try{o=JSON.parse(n.arguments||"{}")}catch{throw new Error(`upstream function_call "${n.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:n.call_id??n.id??`toolu_${e}`,name:n.name??"",input:o}})}async function Ot(t,n,e,o){let s=t.messages;if(!Array.isArray(s)){B(e,400,"invalid_request_error","messages requires a messages array");return}if(typeof t.max_tokens!="number"||!Number.isInteger(t.max_tokens)||t.max_tokens<1){B(e,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){B(e,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(t.stop_sequences!==void 0&&(!Array.isArray(t.stop_sequences)||t.stop_sequences.some(l=>typeof l!="string"))){B(e,400,"invalid_request_error","stop_sequences must be an array of strings");return}let r=String(t.model??"urun"),a=`msg_${Math.random().toString(36).slice(2,14)}`,u=[],f;try{t.system&&u.push({role:"system",content:Rt(t.system)});for(let l of s)u.push(...bt(l));f=St(t.tool_choice)}catch(l){if(l instanceof re){B(e,400,"invalid_request_error",l.message);return}throw l}let d=Ae(t);o.model(d);let c=Array.isArray(t.tools)&&t.tools.length>0,g;try{g=await n.createResponse({model:t.model,input:u,stream:!0,tools:At(t.tools),tool_choice:f,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences})}catch(l){if(l instanceof V){Re(e,"anthropic",l);return}throw l}let p=(l,k)=>{e.destroyed||e.write(`event: ${l}
|
|
34
|
+
data: ${JSON.stringify({type:l,...k})}
|
|
35
|
+
|
|
36
|
+
`)};if(t.stream===!0){let l=o.track(g,d);xe(e),p("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:r,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let k=0,U="",C=0,L="none",j=()=>L,T=-1,w=-1,A=new K,D=[],J=()=>{L!=="none"&&(L="none",p("content_block_stop",{index:T}))},v=(_,b)=>{J(),L=_,T=C++,p("content_block_start",{index:T,content_block:b})};for await(let _ of l){if(e.destroyed)break;D.push(_);let b=se(_);if(_.type==="response.reasoning_text.delta"&&typeof _.delta=="string")j()!=="thinking"&&v("thinking",{type:"thinking",thinking:""}),p("content_block_delta",{index:T,delta:{type:"thinking_delta",thinking:_.delta}});else if(_.type==="response.output_text.delta"&&typeof _.delta=="string")j()!=="text"&&v("text",{type:"text",text:""}),k+=1,U+=_.delta,p("content_block_delta",{index:T,delta:{type:"text_delta",text:_.delta}});else if(b){let{call:G}=A.add(b);(j()!=="tool"||b.tool_index!==w)&&(w=b.tool_index,v("tool",{type:"tool_use",id:G.id??`call_${b.tool_index}`,name:G.name??"",input:{}})),b.delta&&p("content_block_delta",{index:T,delta:{type:"input_json_delta",partial_json:b.delta}})}else if(_.type==="error"){p("error",{error:{type:"api_error",message:JSON.stringify(_)}}),e.end();return}}if(e.destroyed)return;let Y=z(D),Z=oe(Y),ee=Q(c,A.size+Z.length,U);if(ee){p("error",{error:{type:"api_error",message:X(d,ee)}}),e.end();return}J();let y=[];if(A.size===0)try{y=we(Y)}catch(_){p("error",{error:{type:"api_error",message:String(_ instanceof Error?_.message:_)}}),e.end();return}C===0&&y.length===0&&(v("text",{type:"text",text:""}),J()),y.forEach(_=>{let b=C++;p("content_block_start",{index:b,content_block:{..._,input:{}}}),p("content_block_delta",{index:b,delta:{type:"input_json_delta",partial_json:JSON.stringify(_.input??{})}}),p("content_block_stop",{index:b})});let R=A.size>0||y.length>0,I=ce(Y);p("message_delta",{delta:{stop_reason:R?"tool_use":"end_turn"},usage:I?{input_tokens:I.input_tokens,output_tokens:I.output_tokens}:{output_tokens:k}}),p("message_stop",{}),e.end();return}let h="",O="",N=[],m=new K;for await(let l of o.track(g,d)){N.push(l),l.type==="response.output_text.delta"&&typeof l.delta=="string"&&(h+=l.delta),l.type==="response.reasoning_text.delta"&&typeof l.delta=="string"&&(O+=l.delta);let k=se(l);if(k&&m.add(k),l.type==="error"){B(e,502,"api_error",JSON.stringify(l));return}if(e.destroyed)break}if(e.destroyed)return;let P=z(N),S;try{S=we(P),S.length===0&&m.size>0&&(S=we({output:m.items()}))}catch(l){B(e,502,"api_error",String(l instanceof Error?l.message:l));return}let M=Q(c,S.length,h);if(M){B(e,502,"api_error",X(d,M));return}let E=[],$=O||Ke(P);$&&E.push({type:"thinking",thinking:$}),(h||S.length===0)&&E.push({type:"text",text:h}),E.push(...S),q(e,200,{id:a,type:"message",role:"assistant",content:E,model:r,stop_reason:S.length>0?"tool_use":"end_turn",usage:ce(P)??{input_tokens:0,output_tokens:0}})}function Dt(t){let{clients:n,apiKey:e,identity:o}=t,s=new ve,r=new ke,a=ct((u,f)=>{let d=new URL(u.url??"/","http://localhost"),c=d.pathname==="/v1/messages"||d.pathname.startsWith("/v1/messages/"),g=(p,h,O,N)=>c?B(f,p,N,h):F(f,p,h,O);(async()=>{if(u.method==="GET"&&d.pathname==="/healthz"){q(f,200,{ok:!0});return}if(u.method==="GET"&&d.pathname==="/stats"){q(f,200,o?{identity:o,...r.snapshot()}:r.snapshot());return}if(e&&(u.headers.authorization??"")!==`Bearer ${e}`){g(401,"invalid local proxy api key","authentication_error","authentication_error");return}if(u.method==="GET"&&d.pathname==="/v1/models"){q(f,200,await n.listModels());return}if(u.method!=="POST"){g(404,`no route for ${u.method} ${d.pathname}`,"invalid_request_error","not_found_error");return}let p;try{p=await ft(u)}catch(O){if(O instanceof le){g(413,O.message,"request_too_large","request_too_large");return}g(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let h=Buffer.byteLength(JSON.stringify(p));if(d.pathname==="/v1/responses"){r.request("responses",h,h),await vt(p,n,s,f,r);return}if(d.pathname==="/v1/chat/completions"){r.request("chat_completions",h,h),await xt(p,n,f,r);return}if(d.pathname==="/v1/messages"){r.request("messages",h,h),await Ot(p,n,f,r);return}if(d.pathname==="/v1/messages/count_tokens"){r.request("count_tokens",h,h),B(f,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}g(404,`no route for POST ${d.pathname}`,"invalid_request_error","not_found_error")})().catch(p=>{let h=String(p instanceof Error?p.message:p);if(!f.headersSent){g(500,h,"proxy_error","api_error");return}f.destroyed||(c?f.write(`event: error
|
|
37
|
+
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:h}})}
|
|
38
|
+
|
|
39
|
+
`):f.write(`data: ${JSON.stringify({error:{message:h,type:"upstream_error"}})}
|
|
40
|
+
|
|
41
|
+
`)),f.end()})});return Fe(a,{clients:n,apiKey:e}),a}export{Ce as a,Dt as b};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function g(e){if(e instanceof Error)return{type:"error",error:{type:"urun_error",code:e.name||null,message:e.message}};if(e&&typeof e=="object"&&e.t==="error"){let n=e,r=n.body??{};return{type:"error",error:{type:"urun_error",code:n.code??null,message:r.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}function _(e,n,r,t){let o=e.response??{},i={request_id:r,consumer_id:t,stream:!0,kind:"chat",messages:n};return typeof o.instructions=="string"&&(i.instructions=o.instructions),Array.isArray(o.modalities)&&(i.modalities=o.modalities),typeof o.temperature=="number"&&(i.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(i.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(i.tools=o.tools),i}function f(e,n,r){let t={request_id:n,consumer_id:r,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(t.model=e.model),e.tools!==void 0&&(t.tools=e.tools),e.tool_choice!==void 0&&(t.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(t.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(t.max_output_tokens=e.max_output_tokens),typeof e.reasoning_effort=="string"&&(t.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(t.chat_template_kwargs=e.chat_template_kwargs),t}var w="llm-resp";async function*d(e,n){let r=`${w}:${n}`,t=`resp_${n}`;yield{type:"response.created",response:{id:t,status:"in_progress"}};let o=new Map;for await(let i of e.stream(r).messages()){let s=i;if(s.t==="delta"){if(typeof s.delta=="string"&&(yield{type:"response.output_text.delta",item_id:t,delta:s.delta}),typeof s.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:t,delta:s.reasoning}),Array.isArray(s.tool_calls))for(let a of s.tool_calls){let p=typeof a.index=="number"?a.index:0,u=o.get(p)??{};a.id&&(u.id=a.id),a.function?.name&&(u.name=a.function.name),o.set(p,u),yield{type:"response.function_call_arguments.delta",item_id:`fc_${n}_${p}`,tool_index:p,call_id:u.id,name:u.name,delta:a.function?.arguments??""}}}else if(s.t==="response"){yield{type:"response.completed",response:{id:t,status:"completed",...s.body}};return}else if(s.t==="error"){yield g(s);return}}}var b="llm",m=class{constructor(n){this.session=n;this.sessionTag=n.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(n){n.session_tag=this.sessionTag,this.session.doc(b).set({requests:{[n.request_id]:{payload:n,consumer_id:n.consumer_id,stream:n.stream}}})}sendResponses(n,r){let t=f(n,r,this.consumerId);return this.write(t),d(this.session,r)}sendResponseCreate(n,r,t){let o=_(n,r,t,this.consumerId);return this.write(o),d(this.session,t)}};var y=0;function h(){return y+=1,`req_${Date.now().toString(36)}_${y.toString(36)}`}var k=class{transport;constructor(n){this.transport=new m(n)}responses={create:async n=>{let r=h(),t=this.transport.sendResponses(n,r);return Object.assign((async function*(){yield*t})(),{requestId:r})}}};export{g as a,m as b,k as c};
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var $=Object.create;var f=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,H=Object.prototype.hasOwnProperty;var j=(t,e)=>{for(var n in e)f(t,n,{get:e[n],enumerable:!0})},b=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of V(e))!H.call(t,o)&&o!==n&&f(t,o,{get:()=>e[o],enumerable:!(r=N(e,o))||r.enumerable});return t};var
|
|
1
|
+
"use strict";var $=Object.create;var f=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,H=Object.prototype.hasOwnProperty;var j=(t,e)=>{for(var n in e)f(t,n,{get:e[n],enumerable:!0})},b=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of V(e))!H.call(t,o)&&o!==n&&f(t,o,{get:()=>e[o],enumerable:!(r=N(e,o))||r.enumerable});return t};var _=(t,e,n)=>(n=t!=null?$(q(t)):{},b(e||!t||!t.__esModule?f(n,"default",{value:t,enumerable:!0}):n,t)),F=t=>b(f({},"__esModule",{value:!0}),t);var ee={};j(ee,{SUPPORTED_REALTIME_CLIENT_EVENTS:()=>v,SUPPORTED_RESPONSES_EVENTS:()=>S,UrunRealtime:()=>g,UrunResponses:()=>k,isSupportedClientEvent:()=>O,isSupportedResponsesEvent:()=>M,listModels:()=>P,nodeAudioBackend:()=>I,toErrorEvent:()=>p,weriftAudioBackend:()=>x});module.exports=F(ee);function E(t,e,n,r){let o=t.response??{},s={request_id:n,consumer_id:r,stream:!0,kind:"chat",messages:e};return typeof o.instructions=="string"&&(s.instructions=o.instructions),Array.isArray(o.modalities)&&(s.modalities=o.modalities),typeof o.temperature=="number"&&(s.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(s.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(s.tools=o.tools),s}function A(t,e,n){let r={request_id:e,consumer_id:n,stream:!!t.stream,kind:"responses",input:t.input};return t.model&&(r.model=t.model),t.tools!==void 0&&(r.tools=t.tools),t.tool_choice!==void 0&&(r.tool_choice=t.tool_choice),typeof t.temperature=="number"&&(r.temperature=t.temperature),typeof t.max_output_tokens=="number"&&(r.max_output_tokens=t.max_output_tokens),typeof t.reasoning_effort=="string"&&(r.reasoning_effort=t.reasoning_effort),t.chat_template_kwargs!==void 0&&(r.chat_template_kwargs=t.chat_template_kwargs),r}function p(t){if(t instanceof Error)return{type:"error",error:{type:"urun_error",code:t.name||null,message:t.message}};if(t&&typeof t=="object"&&t.t==="error"){let e=t,n=e.body??{};return{type:"error",error:{type:"urun_error",code:e.code??null,message:n.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(t)}}}var W="llm-resp";async function*w(t,e){let n=`${W}:${e}`,r=`resp_${e}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;for await(let s of t.stream(n).messages()){let i=s;if(i.t==="delta"){if(typeof i.delta=="string"&&(yield{type:"response.output_text.delta",item_id:r,delta:i.delta}),typeof i.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:r,delta:i.reasoning}),Array.isArray(i.tool_calls))for(let u of i.tool_calls){let a=typeof u.index=="number"?u.index:0,c=o.get(a)??{};u.id&&(c.id=u.id),u.function?.name&&(c.name=u.function.name),o.set(a,c),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${a}`,tool_index:a,call_id:c.id,name:c.name,delta:u.function?.arguments??""}}}else if(i.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...i.body}};return}else if(i.t==="error"){yield p(i);return}}}var K="llm",l=class{constructor(e){this.session=e;this.sessionTag=e.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(e){e.session_tag=this.sessionTag,this.session.doc(K).set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,n){let r=A(e,n,this.consumerId);return this.write(r),w(this.session,n)}sendResponseCreate(e,n,r){let o=E(e,n,r,this.consumerId);return this.write(o),w(this.session,r)}};var h=class{constructor(e,n){this.backend=e;this.opts=n}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(e){if(!this.source)throw new Error("startOutbound() not called");let n=Buffer.from(e,"base64"),r=Math.floor(n.byteLength/2),o=new Int16Array(r);for(let s=0;s<r;s++)o[s]=n.readInt16LE(s*2);this.source.onData(o)}async startInbound(e){if(e==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("rt-audio-out").track is null)');this.sink=this.backend.createSink(e),this.sink.onframe=n=>{let r=Buffer.from(n.samples.buffer,n.samples.byteOffset,n.samples.byteLength).toString("base64");for(let o of this.outHandlers)o(r)}}onOutputAudio(e){return this.outHandlers.add(e),()=>this.outHandlers.delete(e)}};async function R(t,e,n=1e4){let r=new h(e,{sampleRate:24e3}),o=await r.startOutbound();await t.stream("rt-audio-in").attach(o);let s=await Y(t,n);return await r.startInbound(s),r}function Y(t,e){let n=t.stream("rt-audio-out");return n.track!=null?Promise.resolve(n.track):new Promise((r,o)=>{let s,i=setTimeout(()=>{s?.(),o(new Error(`enableSessionAudio: no downstream audio track within ${e}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},e),u=a=>{a!=null&&(clearTimeout(i),s?.(),r(a))};s=n.on?.("track",u),n.track!=null&&u(n.track)})}var z=24e3,m=480,G=111;async function X(){let t=await import("opusscript"),e=t.default??t;return new e(z,1,2048)}async function x(){let t=await import("werift"),e=await X();return{createSource:()=>{let n=new t.MediaStreamTrack({kind:"audio"}),r=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,s=Math.random()*4294967295>>>0,i=new Int16Array(0);return{track:n,onData:u=>{let a=new Int16Array(i.length+u.length);a.set(i,0),a.set(u,i.length);let c=0;for(;a.length-c>=m;){let y=a.subarray(c,c+m);c+=m;let B=Buffer.from(y.buffer,y.byteOffset,y.byteLength),U=e.encode(B,m);o=o+1&65535,s=s+m>>>0;let C=new t.RtpHeader({version:2,payloadType:G,sequenceNumber:o,timestamp:s,ssrc:r,marker:!1}),D=new t.RtpPacket(C,U);n.writeRtp(D)}i=a.slice(c)}}},createSink:n=>{let r={onframe:null};return n.onReceiveRtp.subscribe(s=>{let i;try{i=e.decode(s.payload)}catch{return}let u=new Int16Array(i.length/2);for(let a=0;a<u.length;a++)u[a]=i.readInt16LE(a*2);r.onframe?.({samples:u})}),r}}}async function I(){let t=await import("@roamhq/wrtc");return{createSource:()=>{let e=new t.nonstandard.RTCAudioSource;return{track:e.createTrack(),onData:r=>e.onData({samples:r,sampleRate:24e3})}},createSink:e=>{let n={onframe:null},r=new t.nonstandard.RTCAudioSink(e);return r.ondata=o=>{n.onframe?.(o)},n}}}var T=0;function J(){return T+=1,`rt_${Date.now().toString(36)}_${T.toString(36)}`}var g=class{constructor(e,n={}){this.session=e;this.opts=n;this.transport=new l(e)}session;opts;transport;handlers=new Set;conversation=[];active=null;audio=null;currentRequestId=null;async enableAudio(){if(!this.opts.audioBackend)throw new Error("audioBackend required for audio");this.audio=await R(this.session,this.opts.audioBackend),this.audio.onOutputAudio(e=>this.emit({type:"response.audio.delta",delta:e}))}on(e,n){return this.handlers.add(n),()=>this.handlers.delete(n)}emit(e){for(let n of this.handlers)n(e)}send(e){switch(e.type){case"conversation.item.create":{let n=(e.item?.content??[]).map(r=>r.text??"").join("");this.conversation.push({role:e.item?.role??"user",content:n});return}case"response.create":{let n=J();this.currentRequestId=n;let r=this.transport.sendResponseCreate({type:"response.create",response:e.response},this.conversation,n);this.active=(async()=>{try{for await(let o of r)this.emit(o)}catch(o){this.emit(p(o))}})();return}case"input_audio_buffer.append":this.audio?.appendInputAudio(e.audio);return;case"input_audio_buffer.commit":case"input_audio_buffer.clear":return;case"response.cancel":return;default:return}}async drain(){this.active&&await this.active}};var L=0;function Q(){return L+=1,`req_${Date.now().toString(36)}_${L.toString(36)}`}var k=class{transport;constructor(e){this.transport=new l(e)}responses={create:async e=>{let n=Q(),r=this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};async function Z(t){let n=await(t.fetchImpl??fetch)(`${t.catalogUrl}/model_catalog?select=model_id,variant`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`}});if(!n.ok)throw new Error(`model_catalog fetch failed: ${n.status}`);return await n.json()}async function P(t){return{object:"list",data:(await Z(t)).map(n=>({id:`${n.model_id}:${n.variant}`,object:"model",created:0,owned_by:"urun"}))}}var v=["session.update","conversation.item.create","response.create","response.cancel","input_audio_buffer.append","input_audio_buffer.commit","input_audio_buffer.clear"],S=["response.created","response.output_text.delta","response.output_item.added","response.function_call_arguments.delta","response.image_generation_call.partial_image","response.completed","error"];function O(t){return v.includes(t)}function M(t){return S.includes(t)}0&&(module.exports={SUPPORTED_REALTIME_CLIENT_EVENTS,SUPPORTED_RESPONSES_EVENTS,UrunRealtime,UrunResponses,isSupportedClientEvent,isSupportedResponsesEvent,listModels,nodeAudioBackend,toErrorEvent,weriftAudioBackend});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { a as UrunSessionLike, L as LaneMessage } from './ResponsesClient-
|
|
2
|
-
export { U as UrunResponses } from './ResponsesClient-
|
|
1
|
+
import { a as UrunSessionLike, L as LaneMessage } from './ResponsesClient-y8g6OfNN.cjs';
|
|
2
|
+
export { U as UrunResponses } from './ResponsesClient-y8g6OfNN.cjs';
|
|
3
3
|
import { A as AudioBackend } from './media-DCHTX3Ez.cjs';
|
|
4
4
|
export { b as AudioSink, c as AudioSource, n as nodeAudioBackend, w as weriftAudioBackend } from './media-DCHTX3Ez.cjs';
|
|
5
5
|
export { RealtimeClientEvent, RealtimeServerEvent } from 'openai/resources/beta/realtime/realtime';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { a as UrunSessionLike, L as LaneMessage } from './ResponsesClient-
|
|
2
|
-
export { U as UrunResponses } from './ResponsesClient-
|
|
1
|
+
import { a as UrunSessionLike, L as LaneMessage } from './ResponsesClient-y8g6OfNN.js';
|
|
2
|
+
export { U as UrunResponses } from './ResponsesClient-y8g6OfNN.js';
|
|
3
3
|
import { A as AudioBackend } from './media-DCHTX3Ez.js';
|
|
4
4
|
export { b as AudioSink, c as AudioSource, n as nodeAudioBackend, w as weriftAudioBackend } from './media-DCHTX3Ez.js';
|
|
5
5
|
export { RealtimeClientEvent, RealtimeServerEvent } from 'openai/resources/beta/realtime/realtime';
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{b as S}from"./chunk-OLE2YJO3.js";import{a as p,c as v,d as E}from"./chunk-VFHQZ4OM.js";import{a as s,b as a,c as f}from"./chunk-
|
|
1
|
+
import{b as S}from"./chunk-OLE2YJO3.js";import{a as p,c as v,d as E}from"./chunk-VFHQZ4OM.js";import{a as s,b as a,c as f}from"./chunk-SSZL77P5.js";var u=0;function _(){return u+=1,`rt_${Date.now().toString(36)}_${u.toString(36)}`}var i=class{constructor(e,t={}){this.session=e;this.opts=t;this.transport=new a(e)}session;opts;transport;handlers=new Set;conversation=[];active=null;audio=null;currentRequestId=null;async enableAudio(){if(!this.opts.audioBackend)throw new Error("audioBackend required for audio");this.audio=await p(this.session,this.opts.audioBackend),this.audio.onOutputAudio(e=>this.emit({type:"response.audio.delta",delta:e}))}on(e,t){return this.handlers.add(t),()=>this.handlers.delete(t)}emit(e){for(let t of this.handlers)t(e)}send(e){switch(e.type){case"conversation.item.create":{let t=(e.item?.content??[]).map(n=>n.text??"").join("");this.conversation.push({role:e.item?.role??"user",content:t});return}case"response.create":{let t=_();this.currentRequestId=t;let n=this.transport.sendResponseCreate({type:"response.create",response:e.response},this.conversation,t);this.active=(async()=>{try{for await(let o of n)this.emit(o)}catch(o){this.emit(s(o))}})();return}case"input_audio_buffer.append":this.audio?.appendInputAudio(e.audio);return;case"input_audio_buffer.commit":case"input_audio_buffer.clear":return;case"response.cancel":return;default:return}}async drain(){this.active&&await this.active}};var l=["session.update","conversation.item.create","response.create","response.cancel","input_audio_buffer.append","input_audio_buffer.commit","input_audio_buffer.clear"],m=["response.created","response.output_text.delta","response.output_item.added","response.function_call_arguments.delta","response.image_generation_call.partial_image","response.completed","error"];function y(r){return l.includes(r)}function h(r){return m.includes(r)}export{l as SUPPORTED_REALTIME_CLIENT_EVENTS,m as SUPPORTED_RESPONSES_EVENTS,i as UrunRealtime,f as UrunResponses,y as isSupportedClientEvent,h as isSupportedResponsesEvent,S as listModels,E as nodeAudioBackend,s as toErrorEvent,v as weriftAudioBackend};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var Ce=Object.create;var F=Object.defineProperty;var Le=Object.getOwnPropertyDescriptor;var Me=Object.getOwnPropertyNames;var Oe=Object.getPrototypeOf,Ne=Object.prototype.hasOwnProperty;var De=(t,e)=>{for(var n in e)F(t,n,{get:e[n],enumerable:!0})},se=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Me(e))!Ne.call(t,r)&&r!==n&&F(t,r,{get:()=>e[r],enumerable:!(o=Le(e,r))||o.enumerable});return t};var ie=(t,e,n)=>(n=t!=null?Ce(Oe(t)):{},se(e||!t||!t.__esModule?F(n,"default",{value:t,enumerable:!0}):n,t)),Fe=t=>se(F({},"__esModule",{value:!0}),t);var ft={};De(ft,{SessionPool:()=>H,URUN_API:()=>ee,applyServeUsage:()=>Te,coalesceSameRole:()=>Ee,createFileDiagnosticSink:()=>be,createUrunExtension:()=>Pe,default:()=>dt,makeSessionFactory:()=>Se,makeStreamSimple:()=>Ie,notify:()=>Z,piDiagnosticLogPath:()=>J,resolveSessionEnv:()=>xe,toResponsesInput:()=>Re});module.exports=Fe(ft);var je=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.tagName.toUpperCase()==="SCRIPT"?document.currentScript.src:new URL("main.js",document.baseURI).href,
|
|
1
|
+
"use strict";var Ce=Object.create;var F=Object.defineProperty;var Le=Object.getOwnPropertyDescriptor;var Me=Object.getOwnPropertyNames;var Oe=Object.getPrototypeOf,Ne=Object.prototype.hasOwnProperty;var De=(t,e)=>{for(var n in e)F(t,n,{get:e[n],enumerable:!0})},se=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Me(e))!Ne.call(t,r)&&r!==n&&F(t,r,{get:()=>e[r],enumerable:!(o=Le(e,r))||o.enumerable});return t};var ie=(t,e,n)=>(n=t!=null?Ce(Oe(t)):{},se(e||!t||!t.__esModule?F(n,"default",{value:t,enumerable:!0}):n,t)),Fe=t=>se(F({},"__esModule",{value:!0}),t);var ft={};De(ft,{SessionPool:()=>H,URUN_API:()=>ee,applyServeUsage:()=>Te,coalesceSameRole:()=>Ee,createFileDiagnosticSink:()=>be,createUrunExtension:()=>Pe,default:()=>dt,makeSessionFactory:()=>Se,makeStreamSimple:()=>Ie,notify:()=>Z,piDiagnosticLogPath:()=>J,resolveSessionEnv:()=>xe,toResponsesInput:()=>Re});module.exports=Fe(ft);var je=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.tagName.toUpperCase()==="SCRIPT"?document.currentScript.src:new URL("main.js",document.baseURI).href,v=je();var ke=require("@earendil-works/pi-ai"),C=require("fs"),ve=require("path");function ae(t,e,n,o){let r=t.response??{},l={request_id:n,consumer_id:o,stream:!0,kind:"chat",messages:e};return typeof r.instructions=="string"&&(l.instructions=r.instructions),Array.isArray(r.modalities)&&(l.modalities=r.modalities),typeof r.temperature=="number"&&(l.temperature=r.temperature),typeof r.max_output_tokens=="number"&&(l.max_output_tokens=r.max_output_tokens),r.tools!==void 0&&(l.tools=r.tools),l}function le(t,e,n){let o={request_id:e,consumer_id:n,stream:!!t.stream,kind:"responses",input:t.input};return t.model&&(o.model=t.model),t.tools!==void 0&&(o.tools=t.tools),t.tool_choice!==void 0&&(o.tool_choice=t.tool_choice),typeof t.temperature=="number"&&(o.temperature=t.temperature),typeof t.max_output_tokens=="number"&&(o.max_output_tokens=t.max_output_tokens),typeof t.reasoning_effort=="string"&&(o.reasoning_effort=t.reasoning_effort),t.chat_template_kwargs!==void 0&&(o.chat_template_kwargs=t.chat_template_kwargs),o}function ue(t){if(t instanceof Error)return{type:"error",error:{type:"urun_error",code:t.name||null,message:t.message}};if(t&&typeof t=="object"&&t.t==="error"){let e=t,n=e.body??{};return{type:"error",error:{type:"urun_error",code:e.code??null,message:n.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(t)}}}var Ke="llm-resp";async function*X(t,e){let n=`${Ke}:${e}`,o=`resp_${e}`;yield{type:"response.created",response:{id:o,status:"in_progress"}};let r=new Map;for await(let l of t.stream(n).messages()){let i=l;if(i.t==="delta"){if(typeof i.delta=="string"&&(yield{type:"response.output_text.delta",item_id:o,delta:i.delta}),typeof i.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:o,delta:i.reasoning}),Array.isArray(i.tool_calls))for(let u of i.tool_calls){let g=typeof u.index=="number"?u.index:0,a=r.get(g)??{};u.id&&(a.id=u.id),u.function?.name&&(a.name=u.function.name),r.set(g,a),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${g}`,tool_index:g,call_id:a.id,name:a.name,delta:u.function?.arguments??""}}}else if(i.t==="response"){yield{type:"response.completed",response:{id:o,status:"completed",...i.body}};return}else if(i.t==="error"){yield ue(i);return}}}var qe="llm",j=class{constructor(e){this.session=e;this.sessionTag=e.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(e){e.session_tag=this.sessionTag,this.session.doc(qe).set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,n){let o=le(e,n,this.consumerId);return this.write(o),X(this.session,n)}sendResponseCreate(e,n,o){let r=ae(e,n,o,this.consumerId);return this.write(r),X(this.session,o)}};var pe=0;function We(){return pe+=1,`req_${Date.now().toString(36)}_${pe.toString(36)}`}var K=class{transport;constructor(e){this.transport=new j(e)}responses={create:async e=>{let n=We(),o=this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*o})(),{requestId:n})}}};var q=require("fs"),de=ie(require("os"),1),W=require("path"),fe=require("util");function J(t=de.default.homedir()){return(0,W.join)(t,".urun","logs","pi-extension.log")}var ce=!1;function ge(t,e){let n=J();try{(0,q.mkdirSync)((0,W.dirname)(n),{recursive:!0}),(0,q.appendFileSync)(n,`${new Date().toISOString()} [console.${t}] [urun] ${(0,fe.format)(...e)}
|
|
2
2
|
`)}catch(o){if(!ce){ce=!0;try{process.stderr.write(`[urun] pi-extension quiet console: cannot write ${n} (${String(o)}); further urun-side console output will be dropped
|
|
3
|
-
`)}catch{}}}}var f=t=>(...e)=>ge(t,e),z={log:f("log"),info:f("info"),warn:f("warn"),error:f("error"),debug:f("debug"),trace:f("trace"),dir:f("dir"),table:f("table"),group:f("group"),groupCollapsed:f("groupCollapsed"),groupEnd:f("groupEnd"),count:f("count"),countReset:f("countReset"),time:f("time"),timeLog:f("timeLog"),timeEnd:f("timeEnd"),assert:(t,...e)=>{t||ge("assert",e.length>0?e:["Assertion failed"])}};function Q(t){try{process.stderr.write(t)}catch{}}var V="https://api.urun.sh/v1";async function me(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/apps`,o=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!o.ok)throw new Error(`org apps listing failed: GET ${n} \u2192 ${o.status}`);let r=await o.json();if(!Array.isArray(r.apps))throw new Error(`org apps listing returned no "apps" array (GET ${n})`);if(r.truncated===!0)throw new Error(`org apps listing was truncated (GET ${n} returned ${r.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return r.apps}async function
|
|
4
|
-
`)}}function Se(t){let e=be(J());return async(n,o,r)=>{let{baseUrl:l,orgId:i,auth:u}=xe(n),{App:g,createClientToken:a}=await t(),
|
|
3
|
+
`)}catch{}}}}var f=t=>(...e)=>ge(t,e),z={log:f("log"),info:f("info"),warn:f("warn"),error:f("error"),debug:f("debug"),trace:f("trace"),dir:f("dir"),table:f("table"),group:f("group"),groupCollapsed:f("groupCollapsed"),groupEnd:f("groupEnd"),count:f("count"),countReset:f("countReset"),time:f("time"),timeLog:f("timeLog"),timeEnd:f("timeEnd"),assert:(t,...e)=>{t||ge("assert",e.length>0?e:["Assertion failed"])}};function Q(t){try{process.stderr.write(t)}catch{}}var V="https://api.urun.sh/v1";async function me(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/apps`,o=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!o.ok)throw new Error(`org apps listing failed: GET ${n} \u2192 ${o.status}`);let r=await o.json();if(!Array.isArray(r.apps))throw new Error(`org apps listing returned no "apps" array (GET ${n})`);if(r.truncated===!0)throw new Error(`org apps listing was truncated (GET ${n} returned ${r.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return r.apps}async function he(t){let e=await me(t),n=e.filter(o=>o.function_name===t.fnName&&o.deployment_status==="active").map(o=>o.app_slug);if(n.length===0)throw new Error(`urun pi extension: the org has no active deployed app exposing "${t.fnName}" (GET ${t.apiUrl}/apps returned ${e.length} app(s), none servable) \u2014 deploy one with \`urun serve <model>\` first`);return n.sort()}var ee="urun-serve",Je="serve",He=131072,Ge=16384,ye=3,Be=42e4,Ye=6e4,we=2,Xe=15e3;function ze(t){let e=(t.URUN_API_KEY??"").trim();if(!e)throw new Error("urun pi extension: URUN_API_KEY is required \u2014 model discovery lists the org's deployed serve apps via the org API (URUN_JWT alone cannot list apps)");return{apiKey:e,apiUrl:(t.URUN_API_URL??"").trim()||V,fnName:(t.URUN_FUNCTION??"").trim()||Je}}function xe(t){let e=(t.URUN_BASE_URL??"").trim(),n=(t.URUN_ORG_ID??"").trim();if(!e)throw new Error("urun pi extension: URUN_BASE_URL is required to open a session");if(/\/v1\/*$/.test(new URL(e).pathname))throw new Error(`urun pi extension: URUN_BASE_URL must be the session-gateway base (e.g. https://api.urun.sh), got ${e} \u2014 a trailing /v1 is the org API form (URUN_API_URL); session allocation 404s against it. Drop the /v1.`);if(!n)throw new Error("urun pi extension: URUN_ORG_ID is required to open a session");let o=(t.URUN_JWT??"").trim();if(o)return{baseUrl:e,orgId:n,auth:{lane:"jwt",jwt:o}};let r=(t.URUN_API_KEY??"").trim();if(!r)throw new Error("urun pi extension: URUN_JWT or URUN_API_KEY is required to open a session");let l=(t.URUN_GATEWAY_URL??"").trim()||void 0;return{baseUrl:e,orgId:n,auth:{lane:"api-key",apiKey:r,gatewayUrl:l}}}function be(t){let e=null;return n=>{e===null&&((0,C.mkdirSync)((0,ve.dirname)(t),{recursive:!0}),e=(0,C.openSync)(t,"a"));let o=n.detail?` ${JSON.stringify(n.detail)}`:"";(0,C.writeSync)(e,`${new Date().toISOString()} [${n.level}] [urun] ${n.message}${o}
|
|
4
|
+
`)}}function Se(t){let e=be(J());return async(n,o,r)=>{let{baseUrl:l,orgId:i,auth:u}=xe(n),{App:g,createClientToken:a}=await t(),h=(u.lane==="jwt"?g(o,{baseUrl:l,orgId:i,jwt:u.jwt,diagnosticSink:e}):g(o,{baseUrl:l,orgId:i,diagnosticSink:e,getAccessToken:async()=>(await a(u.apiKey,{baseUrl:u.gatewayUrl,expiresIn:300,allowedFunctions:[`${o}/${r}`]})).token}))[r];if(typeof h!="function")throw new Error(`urun pi extension: app "${o}" has no function "${r}"`);let w=h();if(typeof w.end!="function")throw new Error(`urun pi extension: app "${o}" function "${r}" returned a session without end()`);let I=w.connect;return typeof I=="function"&&await I.call(w),w}}var Qe=Se(async()=>{let{createRequire:t}=await import("module"),e=t(v);if(typeof globalThis.RTCPeerConnection>"u")try{e.resolve("werift")}catch{throw new Error('urun pi extension: token streams require a WebRTC data channel, and this Node runtime has no RTCPeerConnection and no "werift" backend installed (@urun-sh/core\'s optional Node WebRTC backend \u2014 it was probably stripped by a --no-optional install). Install werift (`npm i werift`) and retry.')}return e("@urun-sh/core")});function Ve(t,e){try{return Promise.resolve(t.end()).then(()=>{},n=>e(n))}catch(n){return e(n),Promise.resolve()}}function Ze(t){if(t?.hasUI===!1)return!0;if(t?.hasUI===!0)return!1;let e=process.argv.slice(2);if(e.includes("-p")||e.includes("--print"))return!0;let n=e.indexOf("--mode");return n!==-1&&(e[n+1]==="json"||e[n+1]==="rpc")?!0:!process.stdin.isTTY||!process.stdout.isTTY}function Ae(t,e){return`uRun: releasing the pooled "${t}" session failed \u2014 the backend session may stay allocated until the platform reclaims it: ${e instanceof Error?e.message:String(e)}`}var H=class{constructor(e,n,o,r=(l,i)=>z.warn(Ae(l,i))){this.open=e;this.env=n;this.fnName=o;this.onReleaseError=r}open;env;fnName;onReleaseError;pool=new Map;releasing=new Set;acquire(e){let n=this.pool.get(e);if(!n){let o=Promise.resolve(this.open(this.env,e,this.fnName)).then(r=>({session:r,responses:new K(r)}));n=o,this.pool.set(e,o),o.catch(()=>{this.pool.get(e)===o&&this.pool.delete(e)})}return n}evict(e){let n=this.pool.get(e);if(!n)return Promise.resolve();this.pool.delete(e);let o=n.then(r=>Ve(r.session,l=>this.onReleaseError(e,l)),()=>{});return this.releasing.add(o),o.then(()=>this.releasing.delete(o)),o}closeAll(){let e=[...this.pool.keys()].map(n=>this.evict(n));return Promise.all([...e,...this.releasing]).then(()=>{})}};function Re(t){let e=[];t.systemPrompt&&e.push({role:"system",content:t.systemPrompt});for(let n of t.messages){if(n.role==="toolResult"){e.push({type:"function_call_output",call_id:n.toolCallId,output:tt(n.content)});continue}if(n.role==="assistant"&&Array.isArray(n.content)){let r="",l=()=>{r&&(e.push({role:"assistant",content:r}),r="")};for(let i of n.content)if(i.type==="text")r+=String(i.text??"");else if(i.type==="toolCall")l(),e.push({type:"function_call",call_id:String(i.id??""),name:String(i.name??""),arguments:JSON.stringify(i.arguments??{})});else{if(i.type==="thinking")continue;throw new Error(`urun pi extension: unsupported assistant content part "${String(i.type)}"`)}l();continue}let o=n.role==="assistant"?"assistant":"user";e.push({role:o,content:et(n.content)})}return Ee(e)}function Ee(t){let e=[];for(let n of t){let o=e[e.length-1];o&&"role"in o&&"role"in n&&o.role===n.role?o.content=[o.content,n.content].filter(r=>r.length>0).join(`
|
|
5
5
|
|
|
6
6
|
`):e.push({...n})}return e}function et(t){return typeof t=="string"?t:Array.isArray(t)?t.map(e=>e&&typeof e=="object"&&"text"in e?String(e.text):"").join(""):""}function tt(t){if(typeof t=="string")return t;if(!Array.isArray(t))return"";let e="";for(let n of t){let o=String(n.type??"");if(o!=="text")throw new Error(`urun pi extension: unsupported toolResult content part "${o}" \u2014 the serve lane carries text tool results only`);e+=String(n.text??"")}return e}function nt(t){if(!(!Array.isArray(t)||t.length===0))return t.map(e=>({type:"function",name:e.name,description:e.description,parameters:e.parameters}))}function _e(t,e){if(!e.trim())return{};try{return JSON.parse(e)}catch{throw new Error(`uRun serve error: tool call "${t}" arguments are not valid JSON: ${e}`)}}function ot(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function Te(t,e){let n=e?.usage;if(!n||typeof n!="object")return;let o=typeof n.input_tokens=="number"?n.input_tokens:void 0,r=typeof n.output_tokens=="number"?n.output_tokens:void 0;o===void 0||r===void 0||(t.input=o,t.output=r,t.totalTokens=typeof n.total_tokens=="number"?n.total_tokens:o+r)}function rt(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let o of e)if(!(o.type!=="reasoning"||!Array.isArray(o.content)))for(let r of o.content)r.type==="reasoning_text"&&(n+=String(r.text??""));return n}var st=["<tool_call>","<function="];function it(t,e){let n=st.find(o=>e.includes(o));if(n)return new Error(`serving row emitted a prose tool call (literal ${JSON.stringify(n)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${t}" (see catalog parser_defaults)`)}var G=class extends Error{};function at(t){let e=t instanceof Error?t.message:String(t),n=t?.code;return/\b529\b/.test(e)||/overloaded/i.test(e)||n===529||n==="529"}function lt(t){return new Promise(e=>setTimeout(e,t))}function ut(t,e,n,o){return new Promise((r,l)=>{let i=!1,u=s=>{i||(i=!0,clearTimeout(g),o?.removeEventListener("abort",a),s())},g=setTimeout(()=>u(()=>l(n())),e),a=()=>u(()=>l(new Error("aborted")));if(o?.aborted){a();return}o?.addEventListener("abort",a,{once:!0}),Promise.resolve(t).then(s=>u(()=>r(s)),s=>u(()=>l(s)))})}async function*pt(t,e){let n=t[Symbol.asyncIterator](),o,r=new Promise((l,i)=>{o=()=>i(new Error("aborted")),e.addEventListener("abort",o,{once:!0})});try{for(;;){if(e.aborted)throw new Error("aborted");let l=await Promise.race([n.next(),r]);if(l.done)return;yield l.value}}finally{o&&e.removeEventListener("abort",o),n.return?.(void 0)}}function Z(t,e){if(t?.hasUI===!1){Q(`${e}
|
|
7
|
-
`);return}let n=t?.sendMessage;if(typeof n=="function")try{n.call(t,{customType:"urun-status",content:e,display:!0},{triggerTurn:!1})}catch{}}function Ie(t,e={},n){let o=e.connectDeadlineMs??Be,r=e.stallTimeoutMs??Ye,l=e.phaseHeartbeatMs??Xe;return(i,u,g)=>{let a=(0,
|
|
8
|
-
`):z.warn(
|
|
7
|
+
`);return}let n=t?.sendMessage;if(typeof n=="function")try{n.call(t,{customType:"urun-status",content:e,display:!0},{triggerTurn:!1})}catch{}}function Ie(t,e={},n){let o=e.connectDeadlineMs??Be,r=e.stallTimeoutMs??Ye,l=e.phaseHeartbeatMs??Xe;return(i,u,g)=>{let a=(0,ke.createAssistantMessageEventStream)(),s={role:"assistant",content:[],api:i.api,provider:i.provider,model:i.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()},h=new AbortController,w=g?.signal,I=!1,N=()=>{I=!0,h.abort()};w&&(w.aborted?N():w.addEventListener("abort",N,{once:!0}));let L=!1,P,te=()=>{P&&clearTimeout(P),P=setTimeout(()=>{L=!0,h.abort()},r)},D=()=>{P&&clearTimeout(P),P=void 0},S=null,A=null,_=null,ne="",M=!1,B=()=>{if(!A)return;let d=A;A=null,a.push({type:"thinking_end",contentIndex:s.content.indexOf(d),content:d.thinking,partial:s})},oe=()=>{if(!S)return;let d=S;S=null,a.push({type:"text_end",contentIndex:s.content.indexOf(d),content:d.text,partial:s})},Y=()=>{if(!_)return;let{call:d,args:k,contentIndex:R}=_;_=null,d.arguments=_e(d.name,k),a.push({type:"toolcall_end",contentIndex:R,toolCall:d,partial:s})},Ue=d=>{let k={type:"toolCall",id:String(d.call_id??d.id??`call_${s.content.length}`),name:String(d.name??""),arguments:{}};s.content.push(k);let R=s.content.length-1;a.push({type:"toolcall_start",contentIndex:R,partial:s});let O=typeof d.arguments=="string"?d.arguments:"";O&&a.push({type:"toolcall_delta",contentIndex:R,delta:O,partial:s}),k.arguments=_e(k.name,O),a.push({type:"toolcall_end",contentIndex:R,toolCall:k,partial:s})};return(async()=>{a.push({type:"start",partial:s});let d=!1,k,R=()=>{k&&clearInterval(k),k=void 0},O=Date.now();k=setInterval(()=>{let x=Math.round((Date.now()-O)/1e3);Z(n,`uRun: still opening the "${i.id}" session\u2026 (${x}s \u2014 serve apps scale to zero, first turn can cold-start ~6min)`)},l);let $e=async x=>{let{responses:b}=await ut(t.acquire(i.id),o,()=>new Error(`uRun session for "${i.id}" did not connect within ${Math.round(o/1e3)}s \u2014 likely cold-starting (scaled to zero) or queued behind capacity. Retry shortly.`),h.signal);R();let U,E;for(let p=1;p<=ye;p++)try{let y=await b.responses.create({model:i.id,input:x,stream:!0,tools:nt(u.tools),max_output_tokens:i.maxTokens,...g?.reasoning!=null&&g.reasoning!=="off"&&i.reasoning!==!1?{}:{chat_template_kwargs:{enable_thinking:!1}}});te();for await(let T of pt(y,h.signal)){te();let m=T;if(m.type==="response.output_text.delta"){let c=typeof m.delta=="string"?m.delta:"";c&&(M=!0,B(),Y(),S||(S={type:"text",text:""},s.content.push(S),a.push({type:"text_start",contentIndex:s.content.length-1,partial:s})),S.text+=c,ne+=c,a.push({type:"text_delta",contentIndex:s.content.indexOf(S),delta:c,partial:s}))}else if(m.type==="response.reasoning_text.delta"){let c=typeof m.delta=="string"?m.delta:"";c&&(M=!0,A||(A={type:"thinking",thinking:""},s.content.push(A),a.push({type:"thinking_start",contentIndex:s.content.length-1,partial:s})),A.thinking+=c,a.push({type:"thinking_delta",contentIndex:s.content.indexOf(A),delta:c,partial:s}))}else if(m.type==="response.function_call_arguments.delta"&&typeof m.tool_index=="number"){let c=m;if(M=!0,B(),oe(),!_||_.toolIndex!==c.tool_index){Y();let re={type:"toolCall",id:c.call_id??`call_${c.tool_index}`,name:c.name??"",arguments:{}};s.content.push(re),_={call:re,args:"",contentIndex:s.content.length-1,toolIndex:c.tool_index},a.push({type:"toolcall_start",contentIndex:_.contentIndex,partial:s})}c.call_id&&(_.call.id=c.call_id),c.name&&(_.call.name=c.name);let $=typeof c.delta=="string"?c.delta:"";$&&(_.args+=$,a.push({type:"toolcall_delta",contentIndex:_.contentIndex,delta:$,partial:s}))}else if(m.type==="response.completed"){U=m.response;break}else if(m.type==="error"){let c=m.error,$=new Error(`uRun serve error: ${c?.message??"unknown error"}`);throw $.code=c?.code??null,$}}if(!U)throw new G(`uRun backhaul for "${i.id}" closed before the turn completed \u2014 the serving session ended mid-turn (its pod was restarted, drained or deleted).`);E=void 0;break}catch(y){if(E=y,h.signal.aborted)throw y;if(p<ye&&at(y)&&!M){await lt(p*500);continue}throw y}if(E)throw E;return U};try{let x=Re(u),b;for(let p=1;p<=we;p++)try{b=await $e(x);break}catch(y){let T=L||y instanceof G;if(p>=we||!T||M||I)throw y;D(),t.evict(i.id),L=!1,h=new AbortController,Z(n,`uRun: the "${i.id}" session ended mid-turn \u2014 re-homing to a fresh assignment and replaying the turn\u2026`)}if(D(),B(),oe(),Y(),!s.content.some(p=>p.type==="toolCall"))for(let p of ot(b))Ue(p);if(!s.content.some(p=>p.type==="thinking")){let p=rt(b);if(p){let y={type:"thinking",thinking:""};s.content.push(y);let T=s.content.length-1;a.push({type:"thinking_start",contentIndex:T,partial:s}),y.thinking=p,a.push({type:"thinking_delta",contentIndex:T,delta:p,partial:s}),a.push({type:"thinking_end",contentIndex:T,content:p,partial:s})}}let U=s.content.filter(p=>p.type==="toolCall").length;if((u.tools?.length??0)>0&&U===0){let p=it(i.id,ne);if(p)throw p}let E=U>0?"toolUse":b?.status==="incomplete"?"length":"stop";s.stopReason=E,Te(s.usage,b),a.push({type:"done",reason:E,message:s}),a.end(s)}catch(x){if(d=!0,D(),I||w?.aborted&&!L)s.stopReason="aborted",s.errorMessage="aborted by user",a.push({type:"error",reason:"aborted",error:s});else{let b=L?`uRun stream stalled \u2014 no output for ${Math.round(r/1e3)}s; the model may be hung. Retry, or Ctrl-C to abort.`:x instanceof Error?x.message:String(x);s.stopReason="error",s.errorMessage=b,a.push({type:"error",reason:"error",error:s})}a.end(s)}finally{D(),R(),w&&w.removeEventListener("abort",N),d&&t.evict(i.id)}})(),a}}function ct(t){return{id:t,name:`uRun ${t}`,api:ee,reasoning:!0,input:["text"],cost:{input:0,output:0,cacheRead:0,cacheWrite:0},contextWindow:He,maxTokens:Ge}}function Pe(t={}){return async e=>{let n=t.env??process.env,{apiKey:o,apiUrl:r,fnName:l}=ze(n),i=await he({apiUrl:r,apiKey:o,fnName:l,fetchImpl:t.fetchImpl}),u=new H(t.openSession??Qe,n,l,(a,s)=>{let h=Ae(a,s);Ze(e)?Q(`${h}
|
|
8
|
+
`):z.warn(h)});e.on("session_shutdown",()=>u.closeAll());let g={name:"uRun",api:ee,baseUrl:"urun://session",apiKey:"$URUN_API_KEY",models:i.map(ct),streamSimple:Ie(u,t.timing??{},e)};e.registerProvider("urun",g)}}var dt=Pe();0&&(module.exports={SessionPool,URUN_API,applyServeUsage,coalesceSameRole,createFileDiagnosticSink,createUrunExtension,makeSessionFactory,makeStreamSimple,notify,piDiagnosticLogPath,resolveSessionEnv,toResponsesInput});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ExtensionAPI } from '@earendil-works/pi-coding-agent';
|
|
2
2
|
import { Api, AssistantMessage, Model, Context, SimpleStreamOptions, AssistantMessageEventStream } from '@earendil-works/pi-ai';
|
|
3
|
-
import { a as UrunSessionLike, U as UrunResponses } from '../ResponsesClient-
|
|
3
|
+
import { a as UrunSessionLike, U as UrunResponses } from '../ResponsesClient-y8g6OfNN.cjs';
|
|
4
4
|
|
|
5
5
|
/** Where the pi lane's quiet diagnostics land (the compat log convention). */
|
|
6
6
|
declare function piDiagnosticLogPath(home?: string): string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ExtensionAPI } from '@earendil-works/pi-coding-agent';
|
|
2
2
|
import { Api, AssistantMessage, Model, Context, SimpleStreamOptions, AssistantMessageEventStream } from '@earendil-works/pi-ai';
|
|
3
|
-
import { a as UrunSessionLike, U as UrunResponses } from '../ResponsesClient-
|
|
3
|
+
import { a as UrunSessionLike, U as UrunResponses } from '../ResponsesClient-y8g6OfNN.js';
|
|
4
4
|
|
|
5
5
|
declare function piDiagnosticLogPath(home?: string): string;
|
|
6
6
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import{c as X}from"../chunk-
|
|
1
|
+
import{c as X}from"../chunk-SSZL77P5.js";import{a as K,d as z}from"../chunk-VXTNG2TP.js";import{createAssistantMessageEventStream as ye}from"@earendil-works/pi-ai";import{mkdirSync as he,openSync as we,writeSync as _e}from"fs";import{dirname as ke}from"path";import{appendFileSync as ce,mkdirSync as de}from"fs";import pe from"os";import{dirname as fe,join as ge}from"path";import{format as me}from"util";function W(t=pe.homedir()){return ge(t,".urun","logs","pi-extension.log")}var Q=!1;function ee(t,e){let n=W();try{de(fe(n),{recursive:!0}),ce(n,`${new Date().toISOString()} [console.${t}] [urun] ${me(...e)}
|
|
2
2
|
`)}catch(o){if(!Q){Q=!0;try{process.stderr.write(`[urun] pi-extension quiet console: cannot write ${n} (${String(o)}); further urun-side console output will be dropped
|
|
3
|
-
`)}catch{}}}}var f=t=>(...e)=>ee(t,e),J={log:f("log"),info:f("info"),warn:f("warn"),error:f("error"),debug:f("debug"),trace:f("trace"),dir:f("dir"),table:f("table"),group:f("group"),groupCollapsed:f("groupCollapsed"),groupEnd:f("groupEnd"),count:f("count"),countReset:f("countReset"),time:f("time"),timeLog:f("timeLog"),timeEnd:f("timeEnd"),assert:(t,...e)=>{t||ee("assert",e.length>0?e:["Assertion failed"])}};function j(t){try{process.stderr.write(t)}catch{}}async function te(t){let e=await z(t),n=e.filter(o=>o.function_name===t.fnName&&o.deployment_status==="active").map(o=>o.app_slug);if(n.length===0)throw new Error(`urun pi extension: the org has no active deployed app exposing "${t.fnName}" (GET ${t.apiUrl}/apps returned ${e.length} app(s), none servable) \u2014 deploy one with \`urun serve <model>\` first`);return n.sort()}var ie="urun-serve",
|
|
4
|
-
`)}}function Ie(t){let e=Ue(W());return async(n,o,s)=>{let{baseUrl:l,orgId:i,auth:p}=Ae(n),{App:
|
|
3
|
+
`)}catch{}}}}var f=t=>(...e)=>ee(t,e),J={log:f("log"),info:f("info"),warn:f("warn"),error:f("error"),debug:f("debug"),trace:f("trace"),dir:f("dir"),table:f("table"),group:f("group"),groupCollapsed:f("groupCollapsed"),groupEnd:f("groupEnd"),count:f("count"),countReset:f("countReset"),time:f("time"),timeLog:f("timeLog"),timeEnd:f("timeEnd"),assert:(t,...e)=>{t||ee("assert",e.length>0?e:["Assertion failed"])}};function j(t){try{process.stderr.write(t)}catch{}}async function te(t){let e=await z(t),n=e.filter(o=>o.function_name===t.fnName&&o.deployment_status==="active").map(o=>o.app_slug);if(n.length===0)throw new Error(`urun pi extension: the org has no active deployed app exposing "${t.fnName}" (GET ${t.apiUrl}/apps returned ${e.length} app(s), none servable) \u2014 deploy one with \`urun serve <model>\` first`);return n.sort()}var ie="urun-serve",ve="serve",xe=131072,Re=16384,ne=3,be=42e4,Te=6e4,oe=2,Ee=15e3;function Se(t){let e=(t.URUN_API_KEY??"").trim();if(!e)throw new Error("urun pi extension: URUN_API_KEY is required \u2014 model discovery lists the org's deployed serve apps via the org API (URUN_JWT alone cannot list apps)");return{apiKey:e,apiUrl:(t.URUN_API_URL??"").trim()||K,fnName:(t.URUN_FUNCTION??"").trim()||ve}}function Ae(t){let e=(t.URUN_BASE_URL??"").trim(),n=(t.URUN_ORG_ID??"").trim();if(!e)throw new Error("urun pi extension: URUN_BASE_URL is required to open a session");if(/\/v1\/*$/.test(new URL(e).pathname))throw new Error(`urun pi extension: URUN_BASE_URL must be the session-gateway base (e.g. https://api.urun.sh), got ${e} \u2014 a trailing /v1 is the org API form (URUN_API_URL); session allocation 404s against it. Drop the /v1.`);if(!n)throw new Error("urun pi extension: URUN_ORG_ID is required to open a session");let o=(t.URUN_JWT??"").trim();if(o)return{baseUrl:e,orgId:n,auth:{lane:"jwt",jwt:o}};let s=(t.URUN_API_KEY??"").trim();if(!s)throw new Error("urun pi extension: URUN_JWT or URUN_API_KEY is required to open a session");let l=(t.URUN_GATEWAY_URL??"").trim()||void 0;return{baseUrl:e,orgId:n,auth:{lane:"api-key",apiKey:s,gatewayUrl:l}}}function Ue(t){let e=null;return n=>{e===null&&(he(ke(t),{recursive:!0}),e=we(t,"a"));let o=n.detail?` ${JSON.stringify(n.detail)}`:"";_e(e,`${new Date().toISOString()} [${n.level}] [urun] ${n.message}${o}
|
|
4
|
+
`)}}function Ie(t){let e=Ue(W());return async(n,o,s)=>{let{baseUrl:l,orgId:i,auth:p}=Ae(n),{App:k,createClientToken:a}=await t(),m=(p.lane==="jwt"?k(o,{baseUrl:l,orgId:i,jwt:p.jwt,diagnosticSink:e}):k(o,{baseUrl:l,orgId:i,diagnosticSink:e,getAccessToken:async()=>(await a(p.apiKey,{baseUrl:p.gatewayUrl,expiresIn:300,allowedFunctions:[`${o}/${s}`]})).token}))[s];if(typeof m!="function")throw new Error(`urun pi extension: app "${o}" has no function "${s}"`);let h=m();if(typeof h.end!="function")throw new Error(`urun pi extension: app "${o}" function "${s}" returned a session without end()`);let A=h.connect;return typeof A=="function"&&await A.call(h),h}}var Pe=Ie(async()=>{let{createRequire:t}=await import("module"),e=t(import.meta.url);if(typeof globalThis.RTCPeerConnection>"u")try{e.resolve("werift")}catch{throw new Error('urun pi extension: token streams require a WebRTC data channel, and this Node runtime has no RTCPeerConnection and no "werift" backend installed (@urun-sh/core\'s optional Node WebRTC backend \u2014 it was probably stripped by a --no-optional install). Install werift (`npm i werift`) and retry.')}return e("@urun-sh/core")});function Ce(t,e){try{return Promise.resolve(t.end()).then(()=>{},n=>e(n))}catch(n){return e(n),Promise.resolve()}}function Ne(t){if(t?.hasUI===!1)return!0;if(t?.hasUI===!0)return!1;let e=process.argv.slice(2);if(e.includes("-p")||e.includes("--print"))return!0;let n=e.indexOf("--mode");return n!==-1&&(e[n+1]==="json"||e[n+1]==="rpc")?!0:!process.stdin.isTTY||!process.stdout.isTTY}function ae(t,e){return`uRun: releasing the pooled "${t}" session failed \u2014 the backend session may stay allocated until the platform reclaims it: ${e instanceof Error?e.message:String(e)}`}var q=class{constructor(e,n,o,s=(l,i)=>J.warn(ae(l,i))){this.open=e;this.env=n;this.fnName=o;this.onReleaseError=s}open;env;fnName;onReleaseError;pool=new Map;releasing=new Set;acquire(e){let n=this.pool.get(e);if(!n){let o=Promise.resolve(this.open(this.env,e,this.fnName)).then(s=>({session:s,responses:new X(s)}));n=o,this.pool.set(e,o),o.catch(()=>{this.pool.get(e)===o&&this.pool.delete(e)})}return n}evict(e){let n=this.pool.get(e);if(!n)return Promise.resolve();this.pool.delete(e);let o=n.then(s=>Ce(s.session,l=>this.onReleaseError(e,l)),()=>{});return this.releasing.add(o),o.then(()=>this.releasing.delete(o)),o}closeAll(){let e=[...this.pool.keys()].map(n=>this.evict(n));return Promise.all([...e,...this.releasing]).then(()=>{})}};function $e(t){let e=[];t.systemPrompt&&e.push({role:"system",content:t.systemPrompt});for(let n of t.messages){if(n.role==="toolResult"){e.push({type:"function_call_output",call_id:n.toolCallId,output:Me(n.content)});continue}if(n.role==="assistant"&&Array.isArray(n.content)){let s="",l=()=>{s&&(e.push({role:"assistant",content:s}),s="")};for(let i of n.content)if(i.type==="text")s+=String(i.text??"");else if(i.type==="toolCall")l(),e.push({type:"function_call",call_id:String(i.id??""),name:String(i.name??""),arguments:JSON.stringify(i.arguments??{})});else{if(i.type==="thinking")continue;throw new Error(`urun pi extension: unsupported assistant content part "${String(i.type)}"`)}l();continue}let o=n.role==="assistant"?"assistant":"user";e.push({role:o,content:Oe(n.content)})}return Le(e)}function Le(t){let e=[];for(let n of t){let o=e[e.length-1];o&&"role"in o&&"role"in n&&o.role===n.role?o.content=[o.content,n.content].filter(s=>s.length>0).join(`
|
|
5
5
|
|
|
6
|
-
`):e.push({...n})}return e}function Oe(t){return typeof t=="string"?t:Array.isArray(t)?t.map(e=>e&&typeof e=="object"&&"text"in e?String(e.text):"").join(""):""}function Me(t){if(typeof t=="string")return t;if(!Array.isArray(t))return"";let e="";for(let n of t){let o=String(n.type??"");if(o!=="text")throw new Error(`urun pi extension: unsupported toolResult content part "${o}" \u2014 the serve lane carries text tool results only`);e+=String(n.text??"")}return e}function De(t){if(!(!Array.isArray(t)||t.length===0))return t.map(e=>({type:"function",name:e.name,description:e.description,parameters:e.parameters}))}function re(t,e){if(!e.trim())return{};try{return JSON.parse(e)}catch{throw new Error(`uRun serve error: tool call "${t}" arguments are not valid JSON: ${e}`)}}function Fe(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function Ke(t,e){let n=e?.usage;if(!n||typeof n!="object")return;let o=typeof n.input_tokens=="number"?n.input_tokens:void 0,s=typeof n.output_tokens=="number"?n.output_tokens:void 0;o===void 0||s===void 0||(t.input=o,t.output=s,t.totalTokens=typeof n.total_tokens=="number"?n.total_tokens:o+s)}function We(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let o of e)if(!(o.type!=="reasoning"||!Array.isArray(o.content)))for(let s of o.content)s.type==="reasoning_text"&&(n+=String(s.text??""));return n}var Je=["<tool_call>","<function="];function je(t,e){let n=Je.find(o=>e.includes(o));if(n)return new Error(`serving row emitted a prose tool call (literal ${JSON.stringify(n)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${t}" (see catalog parser_defaults)`)}var M=class extends Error{};function qe(t){let e=t instanceof Error?t.message:String(t),n=t?.code;return/\b529\b/.test(e)||/overloaded/i.test(e)||n===529||n==="529"}function He(t){return new Promise(e=>setTimeout(e,t))}function Ye(t,e,n,o){return new Promise((s,l)=>{let i=!1,p=r=>{i||(i=!0,clearTimeout(
|
|
7
|
-
`);return}let n=t?.sendMessage;if(typeof n=="function")try{n.call(t,{customType:"urun-status",content:e,display:!0},{triggerTurn:!1})}catch{}}function Be(t,e={},n){let o=e.connectDeadlineMs??be,s=e.stallTimeoutMs??Te,l=e.phaseHeartbeatMs??Ee;return(i,p,
|
|
8
|
-
`):J.warn(m)});e.on("session_shutdown",()=>p.closeAll());let
|
|
6
|
+
`):e.push({...n})}return e}function Oe(t){return typeof t=="string"?t:Array.isArray(t)?t.map(e=>e&&typeof e=="object"&&"text"in e?String(e.text):"").join(""):""}function Me(t){if(typeof t=="string")return t;if(!Array.isArray(t))return"";let e="";for(let n of t){let o=String(n.type??"");if(o!=="text")throw new Error(`urun pi extension: unsupported toolResult content part "${o}" \u2014 the serve lane carries text tool results only`);e+=String(n.text??"")}return e}function De(t){if(!(!Array.isArray(t)||t.length===0))return t.map(e=>({type:"function",name:e.name,description:e.description,parameters:e.parameters}))}function re(t,e){if(!e.trim())return{};try{return JSON.parse(e)}catch{throw new Error(`uRun serve error: tool call "${t}" arguments are not valid JSON: ${e}`)}}function Fe(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function Ke(t,e){let n=e?.usage;if(!n||typeof n!="object")return;let o=typeof n.input_tokens=="number"?n.input_tokens:void 0,s=typeof n.output_tokens=="number"?n.output_tokens:void 0;o===void 0||s===void 0||(t.input=o,t.output=s,t.totalTokens=typeof n.total_tokens=="number"?n.total_tokens:o+s)}function We(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let o of e)if(!(o.type!=="reasoning"||!Array.isArray(o.content)))for(let s of o.content)s.type==="reasoning_text"&&(n+=String(s.text??""));return n}var Je=["<tool_call>","<function="];function je(t,e){let n=Je.find(o=>e.includes(o));if(n)return new Error(`serving row emitted a prose tool call (literal ${JSON.stringify(n)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${t}" (see catalog parser_defaults)`)}var M=class extends Error{};function qe(t){let e=t instanceof Error?t.message:String(t),n=t?.code;return/\b529\b/.test(e)||/overloaded/i.test(e)||n===529||n==="529"}function He(t){return new Promise(e=>setTimeout(e,t))}function Ye(t,e,n,o){return new Promise((s,l)=>{let i=!1,p=r=>{i||(i=!0,clearTimeout(k),o?.removeEventListener("abort",a),r())},k=setTimeout(()=>p(()=>l(n())),e),a=()=>p(()=>l(new Error("aborted")));if(o?.aborted){a();return}o?.addEventListener("abort",a,{once:!0}),Promise.resolve(t).then(r=>p(()=>s(r)),r=>p(()=>l(r)))})}async function*Ge(t,e){let n=t[Symbol.asyncIterator](),o,s=new Promise((l,i)=>{o=()=>i(new Error("aborted")),e.addEventListener("abort",o,{once:!0})});try{for(;;){if(e.aborted)throw new Error("aborted");let l=await Promise.race([n.next(),s]);if(l.done)return;yield l.value}}finally{o&&e.removeEventListener("abort",o),n.return?.(void 0)}}function se(t,e){if(t?.hasUI===!1){j(`${e}
|
|
7
|
+
`);return}let n=t?.sendMessage;if(typeof n=="function")try{n.call(t,{customType:"urun-status",content:e,display:!0},{triggerTurn:!1})}catch{}}function Be(t,e={},n){let o=e.connectDeadlineMs??be,s=e.stallTimeoutMs??Te,l=e.phaseHeartbeatMs??Ee;return(i,p,k)=>{let a=ye(),r={role:"assistant",content:[],api:i.api,provider:i.provider,model:i.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()},m=new AbortController,h=k?.signal,A=!1,L=()=>{A=!0,m.abort()};h&&(h.aborted?L():h.addEventListener("abort",L,{once:!0}));let C=!1,U,H=()=>{U&&clearTimeout(U),U=setTimeout(()=>{C=!0,m.abort()},s)},O=()=>{U&&clearTimeout(U),U=void 0},R=null,b=null,w=null,Y="",N=!1,D=()=>{if(!b)return;let d=b;b=null,a.push({type:"thinking_end",contentIndex:r.content.indexOf(d),content:d.thinking,partial:r})},G=()=>{if(!R)return;let d=R;R=null,a.push({type:"text_end",contentIndex:r.content.indexOf(d),content:d.text,partial:r})},F=()=>{if(!w)return;let{call:d,args:_,contentIndex:T}=w;w=null,d.arguments=re(d.name,_),a.push({type:"toolcall_end",contentIndex:T,toolCall:d,partial:r})},le=d=>{let _={type:"toolCall",id:String(d.call_id??d.id??`call_${r.content.length}`),name:String(d.name??""),arguments:{}};r.content.push(_);let T=r.content.length-1;a.push({type:"toolcall_start",contentIndex:T,partial:r});let $=typeof d.arguments=="string"?d.arguments:"";$&&a.push({type:"toolcall_delta",contentIndex:T,delta:$,partial:r}),_.arguments=re(_.name,$),a.push({type:"toolcall_end",contentIndex:T,toolCall:_,partial:r})};return(async()=>{a.push({type:"start",partial:r});let d=!1,_,T=()=>{_&&clearInterval(_),_=void 0},$=Date.now();_=setInterval(()=>{let v=Math.round((Date.now()-$)/1e3);se(n,`uRun: still opening the "${i.id}" session\u2026 (${v}s \u2014 serve apps scale to zero, first turn can cold-start ~6min)`)},l);let ue=async v=>{let{responses:x}=await Ye(t.acquire(i.id),o,()=>new Error(`uRun session for "${i.id}" did not connect within ${Math.round(o/1e3)}s \u2014 likely cold-starting (scaled to zero) or queued behind capacity. Retry shortly.`),m.signal);T();let I,E;for(let u=1;u<=ne;u++)try{let y=await x.responses.create({model:i.id,input:v,stream:!0,tools:De(p.tools),max_output_tokens:i.maxTokens,...k?.reasoning!=null&&k.reasoning!=="off"&&i.reasoning!==!1?{}:{chat_template_kwargs:{enable_thinking:!1}}});H();for await(let S of Ge(y,m.signal)){H();let g=S;if(g.type==="response.output_text.delta"){let c=typeof g.delta=="string"?g.delta:"";c&&(N=!0,D(),F(),R||(R={type:"text",text:""},r.content.push(R),a.push({type:"text_start",contentIndex:r.content.length-1,partial:r})),R.text+=c,Y+=c,a.push({type:"text_delta",contentIndex:r.content.indexOf(R),delta:c,partial:r}))}else if(g.type==="response.reasoning_text.delta"){let c=typeof g.delta=="string"?g.delta:"";c&&(N=!0,b||(b={type:"thinking",thinking:""},r.content.push(b),a.push({type:"thinking_start",contentIndex:r.content.length-1,partial:r})),b.thinking+=c,a.push({type:"thinking_delta",contentIndex:r.content.indexOf(b),delta:c,partial:r}))}else if(g.type==="response.function_call_arguments.delta"&&typeof g.tool_index=="number"){let c=g;if(N=!0,D(),G(),!w||w.toolIndex!==c.tool_index){F();let B={type:"toolCall",id:c.call_id??`call_${c.tool_index}`,name:c.name??"",arguments:{}};r.content.push(B),w={call:B,args:"",contentIndex:r.content.length-1,toolIndex:c.tool_index},a.push({type:"toolcall_start",contentIndex:w.contentIndex,partial:r})}c.call_id&&(w.call.id=c.call_id),c.name&&(w.call.name=c.name);let P=typeof c.delta=="string"?c.delta:"";P&&(w.args+=P,a.push({type:"toolcall_delta",contentIndex:w.contentIndex,delta:P,partial:r}))}else if(g.type==="response.completed"){I=g.response;break}else if(g.type==="error"){let c=g.error,P=new Error(`uRun serve error: ${c?.message??"unknown error"}`);throw P.code=c?.code??null,P}}if(!I)throw new M(`uRun backhaul for "${i.id}" closed before the turn completed \u2014 the serving session ended mid-turn (its pod was restarted, drained or deleted).`);E=void 0;break}catch(y){if(E=y,m.signal.aborted)throw y;if(u<ne&&qe(y)&&!N){await He(u*500);continue}throw y}if(E)throw E;return I};try{let v=$e(p),x;for(let u=1;u<=oe;u++)try{x=await ue(v);break}catch(y){let S=C||y instanceof M;if(u>=oe||!S||N||A)throw y;O(),t.evict(i.id),C=!1,m=new AbortController,se(n,`uRun: the "${i.id}" session ended mid-turn \u2014 re-homing to a fresh assignment and replaying the turn\u2026`)}if(O(),D(),G(),F(),!r.content.some(u=>u.type==="toolCall"))for(let u of Fe(x))le(u);if(!r.content.some(u=>u.type==="thinking")){let u=We(x);if(u){let y={type:"thinking",thinking:""};r.content.push(y);let S=r.content.length-1;a.push({type:"thinking_start",contentIndex:S,partial:r}),y.thinking=u,a.push({type:"thinking_delta",contentIndex:S,delta:u,partial:r}),a.push({type:"thinking_end",contentIndex:S,content:u,partial:r})}}let I=r.content.filter(u=>u.type==="toolCall").length;if((p.tools?.length??0)>0&&I===0){let u=je(i.id,Y);if(u)throw u}let E=I>0?"toolUse":x?.status==="incomplete"?"length":"stop";r.stopReason=E,Ke(r.usage,x),a.push({type:"done",reason:E,message:r}),a.end(r)}catch(v){if(d=!0,O(),A||h?.aborted&&!C)r.stopReason="aborted",r.errorMessage="aborted by user",a.push({type:"error",reason:"aborted",error:r});else{let x=C?`uRun stream stalled \u2014 no output for ${Math.round(s/1e3)}s; the model may be hung. Retry, or Ctrl-C to abort.`:v instanceof Error?v.message:String(v);r.stopReason="error",r.errorMessage=x,a.push({type:"error",reason:"error",error:r})}a.end(r)}finally{O(),T(),h&&h.removeEventListener("abort",L),d&&t.evict(i.id)}})(),a}}function Xe(t){return{id:t,name:`uRun ${t}`,api:ie,reasoning:!0,input:["text"],cost:{input:0,output:0,cacheRead:0,cacheWrite:0},contextWindow:xe,maxTokens:Re}}function ze(t={}){return async e=>{let n=t.env??process.env,{apiKey:o,apiUrl:s,fnName:l}=Se(n),i=await te({apiUrl:s,apiKey:o,fnName:l,fetchImpl:t.fetchImpl}),p=new q(t.openSession??Pe,n,l,(a,r)=>{let m=ae(a,r);Ne(e)?j(`${m}
|
|
8
|
+
`):J.warn(m)});e.on("session_shutdown",()=>p.closeAll());let k={name:"uRun",api:ie,baseUrl:"urun://session",apiKey:"$URUN_API_KEY",models:i.map(Xe),streamSimple:Be(p,t.timing??{},e)};e.registerProvider("urun",k)}}var ft=ze();export{q as SessionPool,ie as URUN_API,Ke as applyServeUsage,Le as coalesceSameRole,Ue as createFileDiagnosticSink,ze as createUrunExtension,ft as default,Ie as makeSessionFactory,Be as makeStreamSimple,se as notify,W as piDiagnosticLogPath,Ae as resolveSessionEnv,$e as toResponsesInput};
|