@urun-sh/openai 0.2.56 → 0.2.57
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-DrccxHTc.d.cts → ResponsesClient-BDl1OIRP.d.cts} +12 -0
- package/dist/{ResponsesClient-DrccxHTc.d.ts → ResponsesClient-BDl1OIRP.d.ts} +6 -0
- package/dist/{chunk-HQVV5UN5.js → chunk-62IL5R2N.js} +1 -1
- package/dist/chunk-BJUGGTQT.js +36 -0
- package/dist/chunk-VFHQZ4OM.js +1 -0
- package/dist/chunk-VXTNG2TP.js +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +5 -45
- package/dist/index.d.ts +5 -24
- package/dist/index.js +1 -1
- package/dist/media-DCHTX3Ez.d.cts +68 -0
- package/dist/media-DCHTX3Ez.d.ts +42 -0
- package/dist/pi-extension/index.cjs +7 -3
- package/dist/pi-extension/index.d.cts +46 -9
- package/dist/pi-extension/index.d.ts +15 -6
- package/dist/pi-extension/index.js +7 -3
- package/dist/pi-extension/standalone.cjs +54 -50
- package/dist/proxy/cli.cjs +38 -29
- package/dist/proxy/cli.js +16 -16
- package/dist/proxy/index.cjs +23 -14
- package/dist/proxy/index.d.cts +109 -10
- package/dist/proxy/index.d.ts +38 -10
- package/dist/proxy/index.js +1 -1
- package/package.json +6 -2
- package/dist/chunk-47ZLHRBM.js +0 -27
- package/dist/chunk-5CEMLB6H.js +0 -1
package/dist/proxy/index.cjs
CHANGED
|
@@ -1,27 +1,36 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
data: ${JSON.stringify(c)}
|
|
1
|
+
"use strict";var ye=Object.defineProperty;var Ze=Object.getOwnPropertyDescriptor;var et=Object.getOwnPropertyNames;var tt=Object.prototype.hasOwnProperty;var nt=(n,t)=>{for(var e in t)ye(n,e,{get:t[e],enumerable:!0})},ot=(n,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of et(t))!tt.call(n,s)&&s!==e&&ye(n,s,{get:()=>t[s],enumerable:!(o=Ze(t,s))||o.enumerable});return n};var st=n=>ot(ye({},"__esModule",{value:!0}),n);var Mt={};nt(Mt,{GEMINI_LIVE_PATH:()=>de,createOpenAIProxy:()=>Qe});module.exports=st(Mt);var Ye=require("http");var Je=require("crypto"),Ve=require("ws");var F=class extends Error{},ne=class extends Error{};var de="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",it=1007,Te=1008,J=1011,we=1008,i=class extends Error{constructor(e,o=it){super(e);this.closeCode=o}closeCode},x=n=>typeof n=="object"&&n!==null&&!Array.isArray(n)?n:null,le=["setup","clientContent","realtimeInput","toolResponse"];function Ce(n){let t;try{t=JSON.parse(n)}catch{throw new i("client message is not valid JSON")}let e=x(t);if(!e)throw new i("client message must be a JSON object");let o=le.filter(u=>e[u]!==void 0),s=Object.keys(e).filter(u=>!le.includes(u));if(s.length>0)throw new i(`unknown client message field(s) ${JSON.stringify(s)} \u2014 expected exactly one of ${le.join(", ")}`);if(o.length!==1)throw new i(`client message must contain exactly one of ${le.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 Pe(n,t){if(typeof n=="string")return n;let e=x(n),o=e?e.parts:void 0;if(!Array.isArray(o))throw new i(`${t}: 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(`${t}: 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(`${t}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new i(`${t}: unsupported part ${JSON.stringify(r)}`)}return s.join(`
|
|
2
|
+
`)}var at={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},ut={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},lt=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption"],Ee=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function ct(n){let t=x(n);if(!t)throw new i("setup.realtimeInputConfig must be an object");for(let s of Object.keys(t))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(t.automaticActivityDetection);if(t.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(t.activityHandling!==void 0){if(!Ee.includes(t.activityHandling))throw new i(`unsupported activityHandling ${JSON.stringify(t.activityHandling)} \u2014 expected one of ${Ee.join(", ")}`);o=t.activityHandling}return{manualActivity:!0,activityHandling:o}}var dt=["temperature","maxOutputTokens","responseModalities"];function $e(n){for(let[d,h]of Object.entries(ut))if(n[d]!==void 0)throw new i(h);for(let d of Object.keys(n))if(!lt.includes(d))throw new i(`unsupported setup field "${d}"`);if(typeof n.model!="string"||n.model.length===0)throw new i('setup.model is required (format "models/{model}")');let t=n.model.replace(/^models\//,""),e,o,s=!1,r=x(n.generationConfig);if(n.generationConfig!==void 0&&!r)throw new i("setup.generationConfig must be an object");if(r){for(let d of Object.keys(r))if(!dt.includes(d))throw new i(`unsupported generationConfig field "${d}"`);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 d=Array.isArray(r.responseModalities)?r.responseModalities:[r.responseModalities];for(let h of d){let p=String(h).toUpperCase();if(p==="AUDIO"){s=!0;continue}if(p!=="TEXT")throw new i(`responseModalities ${JSON.stringify(h)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge rt-audio-out lane)`)}}}let a=[];n.systemInstruction!==void 0&&a.push({role:"system",content:Pe(n.systemInstruction,"setup.systemInstruction")});let u;if(n.tools!==void 0){if(!Array.isArray(n.tools))throw new i("setup.tools must be an array");u=[];for(let d of n.tools){let h=x(d);if(!h)throw new i("setup.tools entries must be objects");for(let p of Object.keys(h))if(p!=="functionDeclarations")throw new i(`unsupported tool "${p}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(h.functionDeclarations))throw new i("tool.functionDeclarations must be an array");for(let p of h.functionDeclarations){let g=x(p);if(!g||typeof g.name!="string")throw new i("functionDeclarations entries require a string name");u.push({type:"function",name:g.name,description:g.description,parameters:g.parameters})}}u.length===0&&(u=void 0)}let m={enabled:!1,handle:null};if(n.sessionResumption!==void 0){let d=x(n.sessionResumption);if(!d)throw new i("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let h of Object.keys(d))if(h!=="handle")throw new i(`unsupported sessionResumption field "${h}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(d.handle!==void 0&&(typeof d.handle!="string"||d.handle.length===0))throw new i("sessionResumption.handle must be a non-empty string when present");m={enabled:!0,handle:typeof d.handle=="string"?d.handle:null}}let c=n.realtimeInputConfig===void 0?at:ct(n.realtimeInputConfig);return{model:t,systemItems:a,tools:u,temperature:e,maxOutputTokens:o,audioModality:s,activity:c,sessionResumption:m}}function Ne(n){return{sessionResumptionUpdate:{newHandle:n,resumable:!0}}}function Le(n){let t=Math.max(0,n??0)/1e3;return{goAway:{timeLeft:`${Math.round(t*1e3)/1e3}s`}}}function Me(n){for(let o of Object.keys(n))if(o!=="turns"&&o!=="turnComplete")throw new i(`unsupported clientContent field "${o}"`);let t=n.turns===void 0?[]:n.turns;if(!Array.isArray(t))throw new i("clientContent.turns must be an array");let e=[];for(let o of t){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:Pe(s,"clientContent.turns")})}return{items:e,turnComplete:n.turnComplete===!0}}var ce=16e3,Ue=24e3,pt=`audio/pcm;rate=${Ue}`,De=Buffer.alloc(960).toString("base64");function ft(n,t,e){if(t===e)return n;let o=Math.floor(n.length*e/t),s=new Int16Array(o),r=t/e,a=n.length-1;for(let u=0;u<o;u++){let m=u*r,c=Math.floor(m);if(c>=a){s[u]=n[a];continue}let d=m-c;s[u]=Math.round(n[c]+(n[c+1]-n[c])*d)}return s}function mt(n){let t=x(n);if(!t)throw new i("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof t.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(t.mimeType))throw new i(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(t.mimeType)}`);if(typeof t.data!="string"||t.data.length===0)throw new i("realtimeInput.video.data must be non-empty base64");let e=Buffer.from(t.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 ht(n){let t=x(n);if(!t)throw new i("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof t.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(t.mimeType))throw new i(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(t.mimeType)}`);let e=/;\s*rate=(\d+)/.exec(t.mimeType);if(e&&Number(e[1])!==ce)throw new i(`realtimeInput.audio.mimeType declares rate=${e[1]} \u2014 the Live spec input format is ${ce} Hz (audio/pcm;rate=${ce})`);if(typeof t.data!="string"||t.data.length===0)throw new i("realtimeInput.audio.data must be non-empty base64");let o=Buffer.from(t.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=ft(s,ce,Ue);return Buffer.from(r.buffer,r.byteOffset,r.byteLength).toString("base64")}function Be(n){if(n.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(n.activityStart!==void 0||n.activityEnd!==void 0){if(Object.keys(n).length!==1||n.activityStart!==void 0&&n.activityEnd!==void 0)throw new i("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let s=n.activityStart!==void 0?"activityStart":"activityEnd",r=x(n[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 t=["text","audio","audioStreamEnd","video"];for(let o of Object.keys(n))if(!t.includes(o))throw new i(`unsupported realtimeInput field "${o}"`);let e=t.filter(o=>n[o]!==void 0);if(e.length!==1)throw new i(`realtimeInput must carry exactly one of ${t.join(", ")} (got ${e.length})`);if(n.audio!==void 0)return{kind:"audio",base64Pcm24k:ht(n.audio)};if(n.video!==void 0)return{kind:"video",jpegFrame:mt(n.video)};if(n.audioStreamEnd!==void 0){if(n.audioStreamEnd!==!0)throw new i("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof n.text!="string"||n.text.length===0)throw new i("realtimeInput.text must be a non-empty string");return{kind:"text",text:n.text}}function He(n){for(let t of Object.keys(n))if(t!=="functionResponses")throw new i(`unsupported toolResponse field "${t}"`);if(!Array.isArray(n.functionResponses)||n.functionResponses.length===0)throw new i("toolResponse.functionResponses must be a non-empty array");return n.functionResponses.map(t=>{let e=x(t);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 je(n){return n.type!=="response.output_text.delta"||typeof n.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:n.delta}]}}}}function qe(n){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:pt,data:n}}]}}}}function Fe(n){let t=x(n)??{},e=Array.isArray(t.output)?t.output:[],o=[],s=[],r=typeof t.output_text=="string"?t.output_text:"";if(!r)for(let c of e){let d=x(c);if(!(d?.type!=="message"||!Array.isArray(d.content)))for(let h of d.content){let p=x(h);p?.type==="output_text"&&typeof p.text=="string"&&(r+=p.text)}}r&&s.push({role:"assistant",content:r});for(let c of e){let d=x(c);if(d?.type!=="function_call")continue;if(typeof d.call_id!="string"||typeof d.name!="string")throw new i(`upstream function_call item is missing call_id/name: ${JSON.stringify(c)}`,J);let h=typeof d.arguments=="string"&&d.arguments.length>0?d.arguments:"{}",p;try{p=JSON.parse(h)}catch{throw new i(`upstream function_call arguments are not valid JSON: ${h}`,J)}o.push({id:d.call_id,name:d.name,args:p}),s.push({type:"function_call",call_id:d.call_id,name:d.name,arguments:h})}let a=[];o.length>0&&a.push({toolCall:{functionCalls:o}}),a.push({serverContent:{generationComplete:!0}});let u=x(t.usage),m={serverContent:{turnComplete:!0}};if(u){let c={};typeof u.input_tokens=="number"&&(c.promptTokenCount=u.input_tokens),typeof u.output_tokens=="number"&&(c.responseTokenCount=u.output_tokens),typeof u.total_tokens=="number"&&(c.totalTokenCount=u.total_tokens),Object.keys(c).length>0&&(m.usageMetadata=c)}return a.push(m),{messages:a,assistantItems:s}}var gt=256,_e=class{snapshots=new Map;store(t,e){for(this.snapshots.set(t,e);this.snapshots.size>gt;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(t){return this.snapshots.get(t)}},se=n=>n.slice(0,120),ke=class{constructor(t,e,o){this.ws=t;this.clients=e;this.registry=o;t.on("message",s=>this.onMessage(s)),t.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(t){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(t))}fail(t){if(t instanceof F){this.ws.close(Te,se(t.message));return}if(t instanceof ne){this.ws.close(we,se(t.message));return}if(t instanceof i){this.ws.close(t.closeCode,se(t.message));return}this.ws.close(J,se(String(t instanceof Error?t.message:t)))}onMessage(t){try{let e=Ce(typeof t=="string"?t:Buffer.concat(Array.isArray(t)?t:[t]).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}=Me(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:De);return}this.transcript.push(...He(e.toolResponse)),this.enqueue(()=>this.runTurn())}catch(e){this.fail(e)}}async handleSetup(t){if(this.config)throw new i("setup may only be sent once, as the first client message");let e=$e(t),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",we);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 t=this.config;if(!t)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)",J);return this.audio=(async()=>{let o=await e.call(this.clients,t.model);return this.detachAudio=o.onOutputAudio(s=>this.send(qe(s))),o})(),this.audio.catch(o=>this.fail(o)),this.audio}appendAudio(t){this.ensureAudioLane().then(e=>e.appendInputAudio(t)).catch(e=>this.fail(e))}ensureVideoLane(){if(this.video)return this.video;let t=this.config;if(!t)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)",J);return this.video=Promise.resolve(e.call(this.clients,t.model)),this.video.catch(o=>this.fail(o)),this.video}appendVideoFrame(t){this.ensureVideoLane().then(e=>e.sendInputFrame(t)).catch(e=>this.fail(e))}onActivity(t,e){if(!e.activity.manualActivity)throw new i(`realtimeInput.${t} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(t==="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(t){this.queue=this.queue.then(t,()=>{}),this.queue=this.queue.catch(e=>this.fail(e))}async mintResumptionUpdate(){let t=this.config;if(!t?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let e=await this.clients.sessionHandle(t.model);this.proxyHandle=e;let o=(0,Je.randomUUID)();this.registry.store(o,{model:t.model,proxyHandle:e,transcript:[...this.transcript]}),this.send(Ne(o))}async handleUpstreamEnd(t){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(Le(t.timeLeftMs)),this.ws.close(J,se(`upstream session ended: ${t.reason}`))}async runTurn(){let t=this.config;if(!t||this.ws.readyState!==this.ws.OPEN)return;let e={interrupted:!1};this.activeTurn=e;try{let o=await this.clients.createResponse({model:t.model,input:[...this.transcript],stream:!0,tools:t.tools,temperature:t.temperature,max_output_tokens:t.maxOutputTokens}),s,r=!1;for await(let m of o){if(e.interrupted)break;let c=m;if(c.type==="error")throw new i(`upstream error: ${JSON.stringify(c)}`,J);if(c.type==="response.completed"){s=c.response,r=!0;continue}let d=je(c);d&&this.send(d)}if(e.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!r)throw new i("upstream produced no response.completed event",J);let{messages:a,assistantItems:u}=Fe(s);this.transcript.push(...u);for(let m of a)this.send(m);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},yt=(n,t)=>{let e=n.headers["x-goog-api-key"];if(typeof e=="string")return e;let o=n.headers.authorization;return typeof o=="string"&&o.startsWith("Bearer ")?o.slice(7):t.searchParams.get("key")??void 0};function Ge(n,t){let e=new Ve.WebSocketServer({noServer:!0}),o=new _e;n.on("upgrade",(s,r,a)=>{let u=new URL(s.url??"/","http://localhost");if(u.pathname!==de){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(t.apiKey&&yt(s,u)!==t.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,m=>{new ke(m,t.clients,o)})})}var Ae=class n{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(t,e,o){this.requests[t]=(this.requests[t]??0)+1,this.localInBytes+=e,this.naiveInBytes+=o}modelEntry(t){let e=this.models[t];if(e)return e;let o=Object.keys(this.models).length>=wt?"(other)":t;return this.models[o]??={requests:0,tokens_out:0}}model(t){this.modelEntry(t).requests+=1}track(t,e){let o=this,s=performance.now(),r=null,a=0;return(async function*(){for await(let m of t)m.type==="response.output_text.delta"&&typeof m.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+=m.delta.length,e!==void 0&&(o.modelEntry(e).tokens_out+=1)),yield m;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(t){if(!t.length)return null;let e=[...t].sort((o,s)=>o-s);return e[Math.floor(e.length/2)]}snapshot(){let t=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:n.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:n.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:t},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 Se(n){n.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function H(n,t,e){n.writeHead(t,{"content-type":"application/json"}),n.end(JSON.stringify(e))}function V(n,t,e,o="invalid_request_error"){H(n,t,{error:{message:e,type:o}})}function B(n,t,e,o){H(n,t,{type:"error",error:{type:e,message:o}})}function Re(n,t,e){if(t==="anthropic"){H(n,404,{type:"error",error:{type:"not_found_error",message:e.message}});return}H(n,404,{error:{message:e.message,type:"invalid_request_error",code:"model_not_found"}})}var wt=256,_t=128;function Ie(n){return typeof n.model!="string"||!n.model.trim()?"(default)":n.model.trim().slice(0,_t)}var ze=64*1024*1024,pe=class extends Error{};async function kt(n){let t=[],e=0;for await(let s of n){if(e+=s.length,e>ze)throw new pe(`request body exceeds ${ze} bytes`);t.push(s)}let o=Buffer.concat(t).toString("utf8");return o?JSON.parse(o):{}}function Y(n,t,e,o){return{id:n,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:t,choices:[{index:0,delta:e,finish_reason:o}]}}var vt=256;function xt(n){if(typeof n=="string")return[{role:"user",content:n}];if(Array.isArray(n))return n;throw new Error("responses input must be a string or an array of messages/items")}var be=class{conversations=new Map;thread(t,e){let o=xt(e);if(t==null)return o;let s=this.conversations.get(String(t));if(!s)throw new Error(`unknown previous_response_id ${String(t)} (proxy restarts drop stored responses)`);return[...s,...o]}remember(t,e,o){this.conversations.set(t,[...e,{role:"assistant",content:o}]);for(let s of this.conversations.keys()){if(this.conversations.size<=vt)break;this.conversations.delete(s)}}};function re(n){let t=n?.output;return Array.isArray(t)?t.filter(e=>e.type==="function_call"):[]}function z(n){return n.find(t=>t.type==="response.completed")?.response??null}function fe(n){let t=n?.usage;if(!t||typeof t!="object")return null;let e=a=>typeof a=="number"&&Number.isFinite(a)&&a>=0?a:0,o=e(t.input_tokens),s=e(t.output_tokens);if(o===0&&s===0)return null;let r=e(t.total_tokens)||o+s;return{input_tokens:o,output_tokens:s,total_tokens:r}}function Ke(n){let t=fe(n);return t?{prompt_tokens:t.input_tokens,completion_tokens:t.output_tokens,total_tokens:t.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function Xe(n){let t=n?.output;if(!Array.isArray(t))return"";let e="";for(let o of t)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 ie(n){return n.type!=="response.function_call_arguments.delta"||typeof n.tool_index!="number"?null:{tool_index:n.tool_index,call_id:typeof n.call_id=="string"?n.call_id:void 0,name:typeof n.name=="string"?n.name:void 0,delta:typeof n.delta=="string"?n.delta:""}}var K=class{calls=new Map;add(t){let e=this.calls.get(t.tool_index),o=!e;return e||(e={args:""},this.calls.set(t.tool_index,e)),t.call_id&&(e.id=t.call_id),t.name&&(e.name=t.name),e.args+=t.delta,{call:e,isNew:o}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([t,e])=>({type:"function_call",call_id:e.id??`call_${t}`,name:e.name??"",arguments:e.args}))}},At=["<tool_call>","<function="];function bt(n){return At.find(t=>n.includes(t))}function X(n,t){return`serving row emitted a prose tool call (literal ${JSON.stringify(t)} 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 "${n}" (see catalog parser_defaults)`}function Q(n,t,e){if(!(!n||t>0))return bt(e)}function St(n){if(Array.isArray(n))return n.map(t=>{let e=t.function;return!e||typeof e!="object"?t:{type:"function",name:e.name,description:e.description,parameters:e.parameters,strict:e.strict}})}var me=class extends Error{};function Rt(n,t){return Array.isArray(t)?t.map(e=>{let o=e;if(o.type==="text")return{type:n==="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 me(`unsupported chat content part type "${String(o.type)}" \u2014 the proxy translates text and image_url parts`)}):t}function It(n){let t=[];for(let e of n){if(e.role==="tool"){t.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&&t.push({role:"assistant",content:e.content});for(let o of e.tool_calls){let s=o.function??{};t.push({type:"function_call",call_id:o.id,name:s.name,arguments:s.arguments})}continue}t.push({...e,content:Rt(e.role,e.content)})}return t}function ve(n){return re(n).map((t,e)=>({index:e,id:t.call_id??t.id??`call_${e}`,type:"function",function:{name:t.name??"",arguments:t.arguments??"{}"}}))}function We(n,t){let e=t?.output_text;return typeof e=="string"?e:n.filter(o=>o.type==="response.output_text.delta"&&typeof o.delta=="string").map(o=>o.delta).join("")}async function Ot(n,t,e,o,s){let r=n.stream===!0,a;try{a=e.thread(n.previous_response_id,n.input)}catch(f){V(o,400,String(f instanceof Error?f.message:f));return}n.previous_response_id!=null&&(s.naiveInBytes+=Buffer.byteLength(JSON.stringify(a))-Buffer.byteLength(JSON.stringify(n.input)));let u=`resp_${Math.random().toString(36).slice(2,14)}`,m=n.store!==!1,c=Ie(n);s.model(c);let d=Array.isArray(n.tools)&&n.tools.length>0,h;try{h=await t.createResponse({model:n.model,input:a,stream:r,tools:n.tools,tool_choice:n.tool_choice,temperature:n.temperature,max_output_tokens:n.max_output_tokens})}catch(f){if(f instanceof F){Re(o,"openai",f);return}throw f}let p=[];if(r){Se(o);let f=y=>{o.write(`event: ${String(y.type??"message")}
|
|
9
|
+
data: ${JSON.stringify(y)}
|
|
3
10
|
|
|
4
|
-
`)}
|
|
11
|
+
`)},$=y=>y.response&&typeof y.response=="object"?{...y,response:{...y.response,id:u}}:y,S=`item_${u.slice(5)}`,N=`rs_${u.slice(5)}`,T=!1,L=!1,l=0,k=()=>{T||(T=!0,f({type:"response.created",response:{id:u,object:"response",status:"in_progress"}}))},U=!1,E=0,M="",D=()=>{U&&(U=!1,f({type:"response.reasoning_text.done",item_id:N,output_index:E,content_index:0,text:M}),f({type:"response.output_item.done",output_index:E,item:{id:N,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:M}],status:"completed"}}))},C=!1,_=0,b="",j=y=>{C&&(C=!1,f({type:"response.output_text.done",item_id:S,output_index:_,content_index:0,text:b}),f({type:"response.content_part.done",item_id:S,output_index:_,content_index:0,part:{type:"output_text",text:b}}),f({type:"response.output_item.done",output_index:_,item:{id:S,type:"message",role:"assistant",status:y,content:[{type:"output_text",text:b}]}}))},q=new K,v=null,W=-1,Z=y=>{if(!v)return;let A=v;v=null,f({type:"response.function_call_arguments.done",item_id:A.fcId,output_index:A.outputIndex,arguments:A.args}),f({type:"response.output_item.done",output_index:A.outputIndex,item:{id:A.fcId,type:"function_call",call_id:A.callId??A.fcId,name:A.name??"",arguments:A.args,status:y}})},ee=!1;for await(let y of s.track(h,c)){p.push(y);let A=String(y.type??"");if(A==="response.created"||A==="response.in_progress"){T=!0,f($(y));continue}if(A==="response.output_item.added"){L=!0,f($(y));continue}if(A==="response.reasoning_text.delta"&&!L){k(),U||(U=!0,E=l++,f({type:"response.output_item.added",output_index:E,item:{id:N,type:"reasoning",summary:[],content:[],status:"in_progress"}})),M+=String(y.delta??""),f({type:"response.reasoning_text.delta",item_id:N,output_index:E,content_index:0,delta:y.delta});continue}if(A==="response.output_text.delta"&&!L){k(),D(),C||(C=!0,_=l++,f({type:"response.output_item.added",output_index:_,item:{id:S,type:"message",role:"assistant",status:"in_progress",content:[]}}),f({type:"response.content_part.added",item_id:S,output_index:_,content_index:0,part:{type:"output_text",text:""}})),b+=String(y.delta??""),f({type:A,item_id:S,output_index:_,content_index:0,delta:y.delta});continue}let R=L?null:ie(y);if(R){if(k(),D(),j("completed"),q.add(R),R.tool_index!==W){Z("completed"),W=R.tool_index;let w=l++;v={fcId:`fc_${u.slice(5)}_${R.tool_index}`,outputIndex:w,callId:R.call_id,name:R.name,args:""},f({type:"response.output_item.added",output_index:w,item:{id:v.fcId,type:"function_call",call_id:v.callId??v.fcId,name:v.name??"",arguments:"",status:"in_progress"}})}v&&(R.call_id&&(v.callId=R.call_id),R.name&&(v.name=R.name),v.args+=R.delta,f({type:"response.function_call_arguments.delta",item_id:v.fcId,output_index:v.outputIndex,delta:R.delta}));continue}if(A==="response.completed"&&!L){let w=re(y.response),I=Q(d,q.size+w.length,b);if(I){ee=!0,f({type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:X(c,I)}});break}D(),j("completed"),Z("completed"),q.size===0&&w.forEach(G=>{let te=l++,ue=G.id??G.call_id??`fc_${te}`,ge=G.arguments??"",Oe={id:ue,type:"function_call",call_id:G.call_id??ue,name:G.name??""};f({type:"response.output_item.added",output_index:te,item:{...Oe,arguments:"",status:"in_progress"}}),f({type:"response.function_call_arguments.delta",item_id:ue,output_index:te,delta:ge}),f({type:"response.function_call_arguments.done",item_id:ue,output_index:te,arguments:ge}),f({type:"response.output_item.done",output_index:te,item:{...Oe,arguments:ge,status:"completed"}})}),f($(y));continue}f($(y))}D(),j("incomplete"),Z("incomplete"),m&&!ee&&e.remember(u,a,We(p,z(p))),o.write(`data: [DONE]
|
|
5
12
|
|
|
6
|
-
`),o.end();return}let
|
|
13
|
+
`),o.end();return}let g=null;for await(let f of s.track(h,c)){if(p.push(f),f.type==="error"){V(o,502,JSON.stringify(f),"upstream_error");return}f.type==="response.completed"&&(g=f.response)}if(g==null){V(o,502,"upstream produced no response.completed event","upstream_error");return}let O=We(p,g),P=Q(d,re(g).length,O);if(P){V(o,502,X(c,P),"upstream_error");return}m&&e.remember(u,a,O),H(o,200,{...g,id:u})}async function Et(n,t,e,o){let s=n.messages;if(!Array.isArray(s)){V(e,400,"chat/completions requires a messages array");return}let r=String(n.model??"urun"),a=`chatcmpl-${Math.random().toString(36).slice(2,14)}`,u;try{u=It(s)}catch(l){if(l instanceof me){V(e,400,l.message);return}throw l}let m=Ie(n);o.model(m);let c=Array.isArray(n.tools)&&n.tools.length>0,d;try{d=await t.createResponse({model:n.model,input:u,stream:!0,tools:St(n.tools),tool_choice:n.tool_choice,temperature:n.temperature,max_output_tokens:n.max_completion_tokens??n.max_tokens})}catch(l){if(l instanceof F){Re(e,"openai",l);return}throw l}if(n.stream===!0){Se(e);let l=[],k=new K,U="";e.write(`data: ${JSON.stringify(Y(a,r,{role:"assistant"},null))}
|
|
7
14
|
|
|
8
|
-
`);for await(let
|
|
15
|
+
`);for await(let _ of o.track(d,m)){l.push(_);let b=ie(_);if(_.type==="response.output_text.delta"&&typeof _.delta=="string")U+=_.delta,e.write(`data: ${JSON.stringify(Y(a,r,{content:_.delta},null))}
|
|
9
16
|
|
|
10
|
-
`);else if(
|
|
17
|
+
`);else if(_.type==="response.reasoning_text.delta"&&typeof _.delta=="string")e.write(`data: ${JSON.stringify(Y(a,r,{reasoning_content:_.delta},null))}
|
|
11
18
|
|
|
12
|
-
`);else if(
|
|
19
|
+
`);else if(b){let{call:j,isNew:q}=k.add(b),v=q?{index:b.tool_index,id:j.id??`call_${b.tool_index}`,type:"function",function:{name:j.name??"",arguments:b.delta}}:{index:b.tool_index,function:{arguments:b.delta}};e.write(`data: ${JSON.stringify(Y(a,r,{tool_calls:[v]},null))}
|
|
13
20
|
|
|
14
|
-
`)}else if(
|
|
21
|
+
`)}else if(_.type==="error"){e.write(`data: ${JSON.stringify({error:_})}
|
|
15
22
|
|
|
16
|
-
`),e.end();return}}let
|
|
23
|
+
`),e.end();return}}let E=ve(z(l)),M=Q(c,k.size+E.length,U);if(M){e.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:X(m,M)}})}
|
|
17
24
|
|
|
18
|
-
`),e.end();return}
|
|
25
|
+
`),e.end();return}k.size===0&&E.length>0&&e.write(`data: ${JSON.stringify(Y(a,r,{tool_calls:E},null))}
|
|
19
26
|
|
|
20
|
-
`);let
|
|
27
|
+
`);let D=k.size>0||E.length>0;e.write(`data: ${JSON.stringify(Y(a,r,{},D?"tool_calls":"stop"))}
|
|
28
|
+
|
|
29
|
+
`),fe(z(l))&&e.write(`data: ${JSON.stringify({id:a,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:r,choices:[],usage:Ke(z(l))})}
|
|
21
30
|
|
|
22
31
|
`),e.write(`data: [DONE]
|
|
23
32
|
|
|
24
|
-
`),e.end();return}let
|
|
25
|
-
data: ${JSON.stringify({type:
|
|
33
|
+
`),e.end();return}let h="",p="",g=[],O=new K;for await(let l of o.track(d,m)){g.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"&&(p+=l.delta);let k=ie(l);if(k&&O.add(k),l.type==="error"){V(e,502,JSON.stringify(l),"upstream_error");return}}let P=z(g),f=ve(P),$=f.length>0?f:ve({output:O.items()}),S=Q(c,$.length,h);if(S){V(e,502,X(m,S),"upstream_error");return}let N=$.map(({index:l,...k})=>k),T={role:"assistant",content:h||null},L=p||Xe(P);L&&(T.reasoning_content=L),N.length>0&&(T.tool_calls=N),H(e,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:r,choices:[{index:0,message:T,finish_reason:N.length>0?"tool_calls":"stop"}],usage:Ke(P)})}function Tt(n){return typeof n=="string"?n:Array.isArray(n)?n.filter(t=>t.type==="text").map(t=>String(t.text??"")).join(""):""}var ae=class extends Error{};function he(n){return new ae(`unsupported anthropic content block type "${n}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function Ct(n){if(Array.isArray(n))return n.map(t=>{let e=t;return{type:"function",name:e.name,description:e.description,parameters:e.input_schema}})}function Pt(n){if(n==null)return;let t=n.type;if(t==="auto")return"auto";if(t==="none")return"none";if(t==="any")return"required";if(t==="tool")return{type:"function",name:n.name};throw new ae(`unsupported anthropic tool_choice type ${JSON.stringify(String(t))} \u2014 the proxy maps auto, none, any and tool`)}function $t(n){if(typeof n=="string")return n;if(n==null)return"";if(!Array.isArray(n))throw he(typeof n);let t="";for(let e of n){let o=String(e.type??"");if(o!=="text")throw he(`tool_result > ${o}`);t+=String(e.text??"")}return t}function Nt(n){let{role:t,content:e}=n;if(typeof e=="string")return[{role:t,content:e}];if(e==null)return[];if(!Array.isArray(e))throw he(typeof e);let o=[],s="",r=()=>{s&&(o.push({role:t,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:$t(a.content)});else{if(u==="thinking"||u==="redacted_thinking")continue;throw he(u)}}return r(),o}function xe(n){return re(n).map((t,e)=>{let o;try{o=JSON.parse(t.arguments||"{}")}catch{throw new Error(`upstream function_call "${t.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:t.call_id??t.id??`toolu_${e}`,name:t.name??"",input:o}})}async function Lt(n,t,e,o){let s=n.messages;if(!Array.isArray(s)){B(e,400,"invalid_request_error","messages requires a messages array");return}if(typeof n.max_tokens!="number"||!Number.isInteger(n.max_tokens)||n.max_tokens<1){B(e,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(n.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(n.stop_sequences!==void 0&&(!Array.isArray(n.stop_sequences)||n.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(n.model??"urun"),a=`msg_${Math.random().toString(36).slice(2,14)}`,u=[],m;try{n.system&&u.push({role:"system",content:Tt(n.system)});for(let l of s)u.push(...Nt(l));m=Pt(n.tool_choice)}catch(l){if(l instanceof ae){B(e,400,"invalid_request_error",l.message);return}throw l}let c=Ie(n);o.model(c);let d=Array.isArray(n.tools)&&n.tools.length>0,h;try{h=await t.createResponse({model:n.model,input:u,stream:!0,tools:Ct(n.tools),tool_choice:m,temperature:n.temperature,max_output_tokens:n.max_tokens,top_p:n.top_p,stop:n.stop_sequences})}catch(l){if(l instanceof F){Re(e,"anthropic",l);return}throw l}let p=(l,k)=>{e.destroyed||e.write(`event: ${l}
|
|
34
|
+
data: ${JSON.stringify({type:l,...k})}
|
|
26
35
|
|
|
27
|
-
`)};if(
|
|
36
|
+
`)};if(n.stream===!0){let l=o.track(h,c);Se(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="",E=0,M="none",D=()=>M,C=-1,_=-1,b=new K,j=[],q=()=>{M!=="none"&&(M="none",p("content_block_stop",{index:C}))},v=(w,I)=>{q(),M=w,C=E++,p("content_block_start",{index:C,content_block:I})};for await(let w of l){if(e.destroyed)break;j.push(w);let I=ie(w);if(w.type==="response.reasoning_text.delta"&&typeof w.delta=="string")D()!=="thinking"&&v("thinking",{type:"thinking",thinking:""}),p("content_block_delta",{index:C,delta:{type:"thinking_delta",thinking:w.delta}});else if(w.type==="response.output_text.delta"&&typeof w.delta=="string")D()!=="text"&&v("text",{type:"text",text:""}),k+=1,U+=w.delta,p("content_block_delta",{index:C,delta:{type:"text_delta",text:w.delta}});else if(I){let{call:G}=b.add(I);(D()!=="tool"||I.tool_index!==_)&&(_=I.tool_index,v("tool",{type:"tool_use",id:G.id??`call_${I.tool_index}`,name:G.name??"",input:{}})),I.delta&&p("content_block_delta",{index:C,delta:{type:"input_json_delta",partial_json:I.delta}})}else if(w.type==="error"){p("error",{error:{type:"api_error",message:JSON.stringify(w)}}),e.end();return}}if(e.destroyed)return;let W=z(j),Z=re(W),ee=Q(d,b.size+Z.length,U);if(ee){p("error",{error:{type:"api_error",message:X(c,ee)}}),e.end();return}q();let y=[];if(b.size===0)try{y=xe(W)}catch(w){p("error",{error:{type:"api_error",message:String(w instanceof Error?w.message:w)}}),e.end();return}E===0&&y.length===0&&(v("text",{type:"text",text:""}),q()),y.forEach(w=>{let I=E++;p("content_block_start",{index:I,content_block:{...w,input:{}}}),p("content_block_delta",{index:I,delta:{type:"input_json_delta",partial_json:JSON.stringify(w.input??{})}}),p("content_block_stop",{index:I})});let A=b.size>0||y.length>0,R=fe(W);p("message_delta",{delta:{stop_reason:A?"tool_use":"end_turn"},usage:R?{input_tokens:R.input_tokens,output_tokens:R.output_tokens}:{output_tokens:k}}),p("message_stop",{}),e.end();return}let g="",O="",P=[],f=new K;for await(let l of o.track(h,c)){P.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"&&(O+=l.delta);let k=ie(l);if(k&&f.add(k),l.type==="error"){B(e,502,"api_error",JSON.stringify(l));return}if(e.destroyed)break}if(e.destroyed)return;let $=z(P),S;try{S=xe($),S.length===0&&f.size>0&&(S=xe({output:f.items()}))}catch(l){B(e,502,"api_error",String(l instanceof Error?l.message:l));return}let N=Q(d,S.length,g);if(N){B(e,502,"api_error",X(c,N));return}let T=[],L=O||Xe($);L&&T.push({type:"thinking",thinking:L}),(g||S.length===0)&&T.push({type:"text",text:g}),T.push(...S),H(e,200,{id:a,type:"message",role:"assistant",content:T,model:r,stop_reason:S.length>0?"tool_use":"end_turn",usage:fe($)??{input_tokens:0,output_tokens:0}})}function Qe(n){let{clients:t,apiKey:e,identity:o}=n,s=new be,r=new Ae,a=(0,Ye.createServer)((u,m)=>{let c=new URL(u.url??"/","http://localhost"),d=c.pathname==="/v1/messages"||c.pathname.startsWith("/v1/messages/"),h=(p,g,O,P)=>d?B(m,p,P,g):V(m,p,g,O);(async()=>{if(u.method==="GET"&&c.pathname==="/healthz"){H(m,200,{ok:!0});return}if(u.method==="GET"&&c.pathname==="/stats"){H(m,200,o?{identity:o,...r.snapshot()}:r.snapshot());return}if(e&&(u.headers.authorization??"")!==`Bearer ${e}`){h(401,"invalid local proxy api key","authentication_error","authentication_error");return}if(u.method==="GET"&&c.pathname==="/v1/models"){H(m,200,await t.listModels());return}if(u.method!=="POST"){h(404,`no route for ${u.method} ${c.pathname}`,"invalid_request_error","not_found_error");return}let p;try{p=await kt(u)}catch(O){if(O instanceof pe){h(413,O.message,"request_too_large","request_too_large");return}h(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let g=Buffer.byteLength(JSON.stringify(p));if(c.pathname==="/v1/responses"){r.request("responses",g,g),await Ot(p,t,s,m,r);return}if(c.pathname==="/v1/chat/completions"){r.request("chat_completions",g,g),await Et(p,t,m,r);return}if(c.pathname==="/v1/messages"){r.request("messages",g,g),await Lt(p,t,m,r);return}if(c.pathname==="/v1/messages/count_tokens"){r.request("count_tokens",g,g),B(m,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}h(404,`no route for POST ${c.pathname}`,"invalid_request_error","not_found_error")})().catch(p=>{m.headersSent?m.end():h(500,String(p),"proxy_error","api_error")})});return Ge(a,{clients:t,apiKey:e}),a}0&&(module.exports={GEMINI_LIVE_PATH,createOpenAIProxy});
|
package/dist/proxy/index.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Server } from 'node:http';
|
|
2
|
+
import { a as AudioBridge, V as VideoFrameLane } from '../media-DCHTX3Ez.cjs';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* WHAT a proxy serves — the `identity` block on `GET /stats`. Reuse of a
|
|
@@ -41,21 +42,92 @@ interface ProxyIdentity {
|
|
|
41
42
|
* the session's own).
|
|
42
43
|
*/
|
|
43
44
|
|
|
45
|
+
/** The request envelope every Responses-shaped upstream call carries. */
|
|
46
|
+
interface ResponsesCreateParams {
|
|
47
|
+
model?: string;
|
|
48
|
+
input: unknown;
|
|
49
|
+
stream?: boolean;
|
|
50
|
+
tools?: unknown;
|
|
51
|
+
tool_choice?: unknown;
|
|
52
|
+
temperature?: number;
|
|
53
|
+
max_output_tokens?: number;
|
|
54
|
+
top_p?: number;
|
|
55
|
+
stop?: string[];
|
|
56
|
+
}
|
|
57
|
+
/** How one pinned session ended (the native phase machinery's terminal step). */
|
|
58
|
+
interface SessionEndInfo {
|
|
59
|
+
/**
|
|
60
|
+
* Milliseconds until the session's native deadline (`endsAt`), or null when
|
|
61
|
+
* the app declared no maximum session length. NOTE (missing primitive,
|
|
62
|
+
* called out in the PR): core exposes no PRE-expiry notice event — this
|
|
63
|
+
* callback fires AT terminal loss, so timeLeftMs is ~0 on expiry.
|
|
64
|
+
*/
|
|
65
|
+
timeLeftMs: number | null;
|
|
66
|
+
/** The terminal reason (expired / ended / error), for the loud close. */
|
|
67
|
+
reason: string;
|
|
68
|
+
}
|
|
44
69
|
/** The upstream calls the proxy makes — injectable (tests; alt transports). */
|
|
45
70
|
interface ProxyClients {
|
|
46
71
|
/** `UrunResponses(session).responses.create` — an async iterable of Responses stream events. */
|
|
47
|
-
createResponse(params:
|
|
48
|
-
model?: string;
|
|
49
|
-
input: unknown;
|
|
50
|
-
stream?: boolean;
|
|
51
|
-
tools?: unknown;
|
|
52
|
-
tool_choice?: unknown;
|
|
53
|
-
temperature?: number;
|
|
54
|
-
max_output_tokens?: number;
|
|
55
|
-
}): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
72
|
+
createResponse(params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
56
73
|
/** `listModels(...)` result (an OpenAI model list object). */
|
|
57
74
|
listModels(): Promise<unknown>;
|
|
75
|
+
/**
|
|
76
|
+
* Open (or reuse) the NATIVE audio lanes on the pooled session `model` routes
|
|
77
|
+
* to — the same first-party machinery as RealtimeClient.enableAudio
|
|
78
|
+
* (transport/media.ts `enableSessionAudio`: AudioBridge over the session's
|
|
79
|
+
* `rt-audio-in`/`rt-audio-out` stream lanes at 24 kHz). One lane per pooled
|
|
80
|
+
* session; repeat calls return the same lane. Optional at the seam because
|
|
81
|
+
* text-only embeddings exist — but a surface that RECEIVES audio while the
|
|
82
|
+
* embedder wired no `openAudio` must fail LOUD, never drop chunks.
|
|
83
|
+
*/
|
|
84
|
+
openAudio?(model: string | undefined): Promise<ProxyAudioLane>;
|
|
85
|
+
/**
|
|
86
|
+
* Open (or reuse) the NATIVE video FRAME lane on the pooled session `model`
|
|
87
|
+
* routes to (transport/media.ts `enableSessionVideo`: discrete JPEG frames →
|
|
88
|
+
* `stream('rt-video-in').emit`, the §5 named-DATA image-bytes path — NOT the
|
|
89
|
+
* RTP media plane, which requires an already-H.264-encoded track). One lane
|
|
90
|
+
* per pooled session; repeat calls return the same lane. Optional at the seam
|
|
91
|
+
* because text-only embeddings exist — but a surface that RECEIVES video
|
|
92
|
+
* frames while the embedder wired no `openVideo` must fail LOUD, never drop
|
|
93
|
+
* frames.
|
|
94
|
+
*/
|
|
95
|
+
openVideo?(model: string | undefined): Promise<ProxyVideoLane>;
|
|
96
|
+
/**
|
|
97
|
+
* SESSION-IDENTITY SEAM (ModelRouter.handleFor): the opaque stable handle
|
|
98
|
+
* for the pooled session currently serving `model`'s turns. Derived from
|
|
99
|
+
* native identity (app slug + uRun session id) — the same identity the
|
|
100
|
+
* serve-side session-affinity tag rides (urun-python#1556/#1582).
|
|
101
|
+
*/
|
|
102
|
+
sessionHandle(model: string | undefined): Promise<string>;
|
|
103
|
+
/**
|
|
104
|
+
* createResponse PINNED to the exact session a handle names
|
|
105
|
+
* (ModelRouter.sessionForHandle). Throws SessionGoneError LOUDLY when that
|
|
106
|
+
* session is gone or was replaced — never silently opens a fresh session
|
|
107
|
+
* while claiming resume. Deliberately NO re-home on this path: re-homing
|
|
108
|
+
* would swap the pinned session out from under the caller.
|
|
109
|
+
*/
|
|
110
|
+
createResponseOn(handle: string, params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
111
|
+
/**
|
|
112
|
+
* Subscribe to the pinned session's terminal end via core's NATIVE phase
|
|
113
|
+
* machinery (Session.onPhase → terminal 'expired'/'ended'/'error'). Fires
|
|
114
|
+
* `cb` once. Throws SessionGoneError if the handle's session is already
|
|
115
|
+
* gone — which doubles as the loud reattach check at resume time. Returns
|
|
116
|
+
* the unsubscribe.
|
|
117
|
+
*/
|
|
118
|
+
onSessionEnd(handle: string, cb: (end: SessionEndInfo) => void): Promise<() => void>;
|
|
58
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* The audio lane handle `openAudio` returns — structurally the transport
|
|
122
|
+
* AudioBridge (media.ts): base64 PCM16 @24 kHz mono in both directions.
|
|
123
|
+
*/
|
|
124
|
+
type ProxyAudioLane = Pick<AudioBridge, 'appendInputAudio' | 'onOutputAudio'>;
|
|
125
|
+
/**
|
|
126
|
+
* The video frame-lane handle `openVideo` returns — structurally the transport
|
|
127
|
+
* VideoFrameLane (media.ts): one raw encoded JPEG frame per call, input-only
|
|
128
|
+
* (Live-style protocols have no video OUT modality).
|
|
129
|
+
*/
|
|
130
|
+
type ProxyVideoLane = Pick<VideoFrameLane, 'sendInputFrame'>;
|
|
59
131
|
interface OpenAIProxyOptions {
|
|
60
132
|
clients: ProxyClients;
|
|
61
133
|
/** Optional bearer the local agent must present (never forwarded upstream). */
|
|
@@ -73,4 +145,31 @@ interface OpenAIProxyOptions {
|
|
|
73
145
|
/** Build (not listen) the proxy server — the caller owns listen/close. */
|
|
74
146
|
declare function createOpenAIProxy(options: OpenAIProxyOptions): Server;
|
|
75
147
|
|
|
76
|
-
|
|
148
|
+
/**
|
|
149
|
+
* Gemini Live (BidiGenerateContent) ⇄ uRun Responses translation — PURE
|
|
150
|
+
* functions only, no I/O and no session state. The wire shapes mirror the
|
|
151
|
+
* official WebSocket reference (https://ai.google.dev/api/live): client
|
|
152
|
+
* messages are a union of exactly one of `setup` / `clientContent` /
|
|
153
|
+
* `realtimeInput` / `toolResponse`; server messages are a union of
|
|
154
|
+
* `setupComplete` / `serverContent` / `toolCall` / … with `usageMetadata` as
|
|
155
|
+
* a SIBLING of the union field on `BidiGenerateContentServerMessage`.
|
|
156
|
+
*
|
|
157
|
+
* Scope (this slice): setup + clientContent + realtimeInput TEXT + AUDIO +
|
|
158
|
+
* VIDEO + serverContent streaming + toolCall/toolResponse + sessionResumption
|
|
159
|
+
* (handles over the proxy's pooled-session identity seam) + goAway + MANUAL
|
|
160
|
+
* VAD/activity (setup.realtimeInputConfig.automaticActivityDetection.disabled
|
|
161
|
+
* + realtimeInput.activityStart/activityEnd + serverContent.interrupted
|
|
162
|
+
* barge-in). AUTOMATIC (server-side) activity detection is the DECLARED
|
|
163
|
+
* follow-up slice — requesting it fails loud naming the follow-up, never
|
|
164
|
+
* silently ignored (repo mandate: one canonical path, no silent fallbacks).
|
|
165
|
+
*
|
|
166
|
+
* AUTOMATIC activity detection is deliberately NOT emulated here: server-side
|
|
167
|
+
* VAD is a PLATFORM primitive (urun-python `urun.serve.turn_detection`
|
|
168
|
+
* `detect_turns` — `turn_start`/`turn_end` events on the session's §5 stream
|
|
169
|
+
* lane), never adapter-local DSP in the proxy. Until the Live lane's session
|
|
170
|
+
* shapes emit those events, requesting automatic detection fails loud.
|
|
171
|
+
*/
|
|
172
|
+
/** The Live WS endpoint path (v1beta), verbatim from the Google reference. */
|
|
173
|
+
declare const GEMINI_LIVE_PATH = "/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent";
|
|
174
|
+
|
|
175
|
+
export { GEMINI_LIVE_PATH, type OpenAIProxyOptions, type ProxyClients, type ProxyIdentity, createOpenAIProxy };
|
package/dist/proxy/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Server } from 'node:http';
|
|
2
|
+
import { a as AudioBridge, V as VideoFrameLane } from '../media-DCHTX3Ez.js';
|
|
2
3
|
|
|
3
4
|
interface ProxyIdentity {
|
|
4
5
|
app: string;
|
|
@@ -9,20 +10,45 @@ interface ProxyIdentity {
|
|
|
9
10
|
proxy_version: string;
|
|
10
11
|
}
|
|
11
12
|
|
|
13
|
+
interface ResponsesCreateParams {
|
|
14
|
+
model?: string;
|
|
15
|
+
input: unknown;
|
|
16
|
+
stream?: boolean;
|
|
17
|
+
tools?: unknown;
|
|
18
|
+
tool_choice?: unknown;
|
|
19
|
+
temperature?: number;
|
|
20
|
+
max_output_tokens?: number;
|
|
21
|
+
top_p?: number;
|
|
22
|
+
stop?: string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface SessionEndInfo {
|
|
26
|
+
|
|
27
|
+
timeLeftMs: number | null;
|
|
28
|
+
|
|
29
|
+
reason: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
12
32
|
interface ProxyClients {
|
|
13
33
|
|
|
14
|
-
createResponse(params:
|
|
15
|
-
model?: string;
|
|
16
|
-
input: unknown;
|
|
17
|
-
stream?: boolean;
|
|
18
|
-
tools?: unknown;
|
|
19
|
-
tool_choice?: unknown;
|
|
20
|
-
temperature?: number;
|
|
21
|
-
max_output_tokens?: number;
|
|
22
|
-
}): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
34
|
+
createResponse(params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
23
35
|
|
|
24
36
|
listModels(): Promise<unknown>;
|
|
37
|
+
|
|
38
|
+
openAudio?(model: string | undefined): Promise<ProxyAudioLane>;
|
|
39
|
+
|
|
40
|
+
openVideo?(model: string | undefined): Promise<ProxyVideoLane>;
|
|
41
|
+
|
|
42
|
+
sessionHandle(model: string | undefined): Promise<string>;
|
|
43
|
+
|
|
44
|
+
createResponseOn(handle: string, params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
45
|
+
|
|
46
|
+
onSessionEnd(handle: string, cb: (end: SessionEndInfo) => void): Promise<() => void>;
|
|
25
47
|
}
|
|
48
|
+
|
|
49
|
+
type ProxyAudioLane = Pick<AudioBridge, 'appendInputAudio' | 'onOutputAudio'>;
|
|
50
|
+
|
|
51
|
+
type ProxyVideoLane = Pick<VideoFrameLane, 'sendInputFrame'>;
|
|
26
52
|
interface OpenAIProxyOptions {
|
|
27
53
|
clients: ProxyClients;
|
|
28
54
|
|
|
@@ -33,4 +59,6 @@ interface OpenAIProxyOptions {
|
|
|
33
59
|
|
|
34
60
|
declare function createOpenAIProxy(options: OpenAIProxyOptions): Server;
|
|
35
61
|
|
|
36
|
-
|
|
62
|
+
declare const GEMINI_LIVE_PATH = "/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent";
|
|
63
|
+
|
|
64
|
+
export { GEMINI_LIVE_PATH, type OpenAIProxyOptions, type ProxyClients, type ProxyIdentity, createOpenAIProxy };
|
package/dist/proxy/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as e}from"../chunk-
|
|
1
|
+
import{a as r,b as e}from"../chunk-BJUGGTQT.js";import"../chunk-VFHQZ4OM.js";import"../chunk-VXTNG2TP.js";export{r as GEMINI_LIVE_PATH,e as createOpenAIProxy};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@urun-sh/openai",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.57",
|
|
4
4
|
"description": "OpenAI-compatible Realtime + Responses SDK over uRun session primitives (not websockets).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -53,7 +53,8 @@
|
|
|
53
53
|
"@urun-sh/core": "^0.2.53"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"openai": "4.104.0"
|
|
56
|
+
"openai": "4.104.0",
|
|
57
|
+
"ws": "^8.18.0"
|
|
57
58
|
},
|
|
58
59
|
"optionalDependencies": {
|
|
59
60
|
"@roamhq/wrtc": "^0.8.0",
|
|
@@ -61,6 +62,8 @@
|
|
|
61
62
|
"opusscript": "^0.1.1"
|
|
62
63
|
},
|
|
63
64
|
"devDependencies": {
|
|
65
|
+
"@earendil-works/pi-coding-agent": "0.84.1",
|
|
66
|
+
"esbuild": "^0.28.1",
|
|
64
67
|
"reflect-metadata": "^0.2.2",
|
|
65
68
|
"tsup": "^8.5.0",
|
|
66
69
|
"vitest": "^3.0.0",
|
|
@@ -69,6 +72,7 @@
|
|
|
69
72
|
"happy-dom": "^20.9.0",
|
|
70
73
|
"typescript": "^5.3.0",
|
|
71
74
|
"@types/node": "^24.0.0",
|
|
75
|
+
"@types/ws": "^8.18.1",
|
|
72
76
|
"@urun-sh/core": "workspace:*"
|
|
73
77
|
},
|
|
74
78
|
"publishConfig": {
|
package/dist/chunk-47ZLHRBM.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import{b as V}from"./chunk-5CEMLB6H.js";import{createServer as yt}from"http";var ut=class e{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(n,t,o){this.requests[n]=(this.requests[n]??0)+1,this.localInBytes+=t,this.naiveInBytes+=o}modelEntry(n){let t=this.models[n];if(t)return t;let o=Object.keys(this.models).length>=ht?"(other)":n;return this.models[o]??={requests:0,tokens_out:0}}model(n){this.modelEntry(n).requests+=1}track(n,t){let o=this,a=performance.now(),c=null,u=0;return(async function*(){for await(let d of n)d.type==="response.output_text.delta"&&typeof d.delta=="string"&&(c===null&&(c=performance.now(),o.ttftMs.push(c-a),o.ttftMs.length>512&&o.ttftMs.shift()),u+=1,o.tokensOut+=1,o.charsOut+=d.delta.length,t!==void 0&&(o.modelEntry(t).tokens_out+=1)),yield d;let i=(performance.now()-(c??a))/1e3;u>0&&i>0&&(o.tokRates.push(u/i),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(n){if(!n.length)return null;let t=[...n].sort((o,a)=>o-a);return t[Math.floor(t.length/2)]}snapshot(){let n=Object.values(this.requests).reduce((t,o)=>t+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:e.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:e.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 ct(e){e.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function J(e,n,t){e.writeHead(n,{"content-type":"application/json"}),e.end(JSON.stringify(t))}function w(e,n,t,o="invalid_request_error"){J(e,n,{error:{message:t,type:o}})}function pt(e,n,t){if(n==="anthropic"){J(e,404,{type:"error",error:{type:"not_found_error",message:t.message}});return}J(e,404,{error:{message:t.message,type:"invalid_request_error",code:"model_not_found"}})}var ht=256,kt=128;function dt(e){return typeof e.model!="string"||!e.model.trim()?"(default)":e.model.trim().slice(0,kt)}var mt=64*1024*1024,nt=class extends Error{};async function wt(e){let n=[],t=0;for await(let a of e){if(t+=a.length,t>mt)throw new nt(`request body exceeds ${mt} bytes`);n.push(a)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function F(e,n,t,o){return{id:e,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:n,choices:[{index:0,delta:t,finish_reason:o}]}}var xt=256;function Rt(e){if(typeof e=="string")return[{role:"user",content:e}];if(Array.isArray(e))return e;throw new Error("responses input must be a string or an array of messages/items")}var lt=class{conversations=new Map;thread(n,t){let o=Rt(t);if(n==null)return o;let a=this.conversations.get(String(n));if(!a)throw new Error(`unknown previous_response_id ${String(n)} (proxy restarts drop stored responses)`);return[...a,...o]}remember(n,t,o){this.conversations.set(n,[...t,{role:"assistant",content:o}]);for(let a of this.conversations.keys()){if(this.conversations.size<=xt)break;this.conversations.delete(a)}}};function Q(e){let n=e?.output;return Array.isArray(n)?n.filter(t=>t.type==="function_call"):[]}function W(e){return e.find(n=>n.type==="response.completed")?.response??null}function _t(e){let n=e?.output;if(!Array.isArray(n))return"";let t="";for(let o of n)if(!(o.type!=="reasoning"||!Array.isArray(o.content)))for(let a of o.content)a.type==="reasoning_text"&&(t+=String(a.text??""));return t}function Z(e){return e.type!=="response.function_call_arguments.delta"||typeof e.tool_index!="number"?null:{tool_index:e.tool_index,call_id:typeof e.call_id=="string"?e.call_id:void 0,name:typeof e.name=="string"?e.name:void 0,delta:typeof e.delta=="string"?e.delta:""}}var z=class{calls=new Map;add(n){let t=this.calls.get(n.tool_index),o=!t;return t||(t={args:""},this.calls.set(n.tool_index,t)),n.call_id&&(t.id=n.call_id),n.name&&(t.name=n.name),t.args+=n.delta,{call:t,isNew:o}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([n,t])=>({type:"function_call",call_id:t.id??`call_${n}`,name:t.name??"",arguments:t.args}))}},vt=["<tool_call>","<function="];function At(e){return vt.find(n=>e.includes(n))}function D(e,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 "${e}" (see catalog parser_defaults)`}function K(e,n,t){if(!(!e||n>0))return At(t)}function St(e){if(Array.isArray(e))return e.map(n=>{let t=n.function;return!t||typeof t!="object"?n:{type:"function",name:t.name,description:t.description,parameters:t.parameters,strict:t.strict}})}var ot=class extends Error{};function It(e,n){return Array.isArray(n)?n.map(t=>{let o=t;if(o.type==="text")return{type:e==="assistant"?"output_text":"input_text",text:o.text};if(o.type==="image_url"){let a=o.image_url??{};return{type:"input_image",image_url:a.url,detail:a.detail}}throw new ot(`unsupported chat content part type "${String(o.type)}" \u2014 the proxy translates text and image_url parts`)}):n}function Ot(e){let n=[];for(let t of e){if(t.role==="tool"){n.push({type:"function_call_output",call_id:t.tool_call_id,output:typeof t.content=="string"?t.content:JSON.stringify(t.content??"")});continue}if(t.role==="assistant"&&Array.isArray(t.tool_calls)){typeof t.content=="string"&&t.content&&n.push({role:"assistant",content:t.content});for(let o of t.tool_calls){let a=o.function??{};n.push({type:"function_call",call_id:o.id,name:a.name,arguments:a.arguments})}continue}n.push({...t,content:It(t.role,t.content)})}return n}function it(e){return Q(e).map((n,t)=>({index:t,id:n.call_id??n.id??`call_${t}`,type:"function",function:{name:n.name??"",arguments:n.arguments??"{}"}}))}function gt(e,n){let t=n?.output_text;return typeof t=="string"?t:e.filter(o=>o.type==="response.output_text.delta"&&typeof o.delta=="string").map(o=>o.delta).join("")}async function Et(e,n,t,o,a){let c=e.stream===!0,u;try{u=t.thread(e.previous_response_id,e.input)}catch(s){w(o,400,String(s instanceof Error?s.message:s));return}e.previous_response_id!=null&&(a.naiveInBytes+=Buffer.byteLength(JSON.stringify(u))-Buffer.byteLength(JSON.stringify(e.input)));let i=`resp_${Math.random().toString(36).slice(2,14)}`,d=e.store!==!1,_=dt(e);a.model(_);let v=Array.isArray(e.tools)&&e.tools.length>0,A;try{A=await n.createResponse({model:e.model,input:u,stream:c,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,max_output_tokens:e.max_output_tokens})}catch(s){if(s instanceof V){pt(o,"openai",s);return}throw s}let f=[];if(c){ct(o);let s=p=>{o.write(`event: ${String(p.type??"message")}
|
|
2
|
-
data: ${JSON.stringify(p)}
|
|
3
|
-
|
|
4
|
-
`)},C=p=>p.response&&typeof p.response=="object"?{...p,response:{...p.response,id:i}}:p,R=`item_${i.slice(5)}`,b=`rs_${i.slice(5)}`,E=!1,$=!1,r=0,g=()=>{E||(E=!0,s({type:"response.created",response:{id:i,object:"response",status:"in_progress"}}))},T=!1,S=0,M="",N=()=>{T&&(T=!1,s({type:"response.reasoning_text.done",item_id:b,output_index:S,content_index:0,text:M}),s({type:"response.output_item.done",output_index:S,item:{id:b,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:M}],status:"completed"}}))},m=!1,x=0,O="",j=p=>{m&&(m=!1,s({type:"response.output_text.done",item_id:R,output_index:x,content_index:0,text:O}),s({type:"response.content_part.done",item_id:R,output_index:x,content_index:0,part:{type:"output_text",text:O}}),s({type:"response.output_item.done",output_index:x,item:{id:R,type:"message",role:"assistant",status:p,content:[{type:"output_text",text:O}]}}))},B=new z,y=null,L=-1,H=p=>{if(!y)return;let h=y;y=null,s({type:"response.function_call_arguments.done",item_id:h.fcId,output_index:h.outputIndex,arguments:h.args}),s({type:"response.output_item.done",output_index:h.outputIndex,item:{id:h.fcId,type:"function_call",call_id:h.callId??h.fcId,name:h.name??"",arguments:h.args,status:p}})},U=!1;for await(let p of a.track(A,_)){f.push(p);let h=String(p.type??"");if(h==="response.created"||h==="response.in_progress"){E=!0,s(C(p));continue}if(h==="response.output_item.added"){$=!0,s(C(p));continue}if(h==="response.reasoning_text.delta"&&!$){g(),T||(T=!0,S=r++,s({type:"response.output_item.added",output_index:S,item:{id:b,type:"reasoning",summary:[],content:[],status:"in_progress"}})),M+=String(p.delta??""),s({type:"response.reasoning_text.delta",item_id:b,output_index:S,content_index:0,delta:p.delta});continue}if(h==="response.output_text.delta"&&!$){g(),N(),m||(m=!0,x=r++,s({type:"response.output_item.added",output_index:x,item:{id:R,type:"message",role:"assistant",status:"in_progress",content:[]}}),s({type:"response.content_part.added",item_id:R,output_index:x,content_index:0,part:{type:"output_text",text:""}})),O+=String(p.delta??""),s({type:h,item_id:R,output_index:x,content_index:0,delta:p.delta});continue}let l=$?null:Z(p);if(l){if(g(),N(),j("completed"),B.add(l),l.tool_index!==L){H("completed"),L=l.tool_index;let k=r++;y={fcId:`fc_${i.slice(5)}_${l.tool_index}`,outputIndex:k,callId:l.call_id,name:l.name,args:""},s({type:"response.output_item.added",output_index:k,item:{id:y.fcId,type:"function_call",call_id:y.callId??y.fcId,name:y.name??"",arguments:"",status:"in_progress"}})}y&&(l.call_id&&(y.callId=l.call_id),l.name&&(y.name=l.name),y.args+=l.delta,s({type:"response.function_call_arguments.delta",item_id:y.fcId,output_index:y.outputIndex,delta:l.delta}));continue}if(h==="response.completed"&&!$){let k=Q(p.response),X=K(v,B.size+k.length,O);if(X){U=!0,s({type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:D(_,X)}});break}N(),j("completed"),H("completed"),B.size===0&&k.forEach(Y=>{let G=r++,et=Y.id??Y.call_id??`fc_${G}`,st=Y.arguments??"",ft={id:et,type:"function_call",call_id:Y.call_id??et,name:Y.name??""};s({type:"response.output_item.added",output_index:G,item:{...ft,arguments:"",status:"in_progress"}}),s({type:"response.function_call_arguments.delta",item_id:et,output_index:G,delta:st}),s({type:"response.function_call_arguments.done",item_id:et,output_index:G,arguments:st}),s({type:"response.output_item.done",output_index:G,item:{...ft,arguments:st,status:"completed"}})}),s(C(p));continue}s(C(p))}N(),j("incomplete"),H("incomplete"),d&&!U&&t.remember(i,u,gt(f,W(f))),o.write(`data: [DONE]
|
|
5
|
-
|
|
6
|
-
`),o.end();return}let I=null;for await(let s of a.track(A,_)){if(f.push(s),s.type==="error"){w(o,502,JSON.stringify(s),"upstream_error");return}s.type==="response.completed"&&(I=s.response)}if(I==null){w(o,502,"upstream produced no response.completed event","upstream_error");return}let q=gt(f,I),P=K(v,Q(I).length,q);if(P){w(o,502,D(_,P),"upstream_error");return}d&&t.remember(i,u,q),J(o,200,{...I,id:i})}async function bt(e,n,t,o){let a=e.messages;if(!Array.isArray(a)){w(t,400,"chat/completions requires a messages array");return}let c=String(e.model??"urun"),u=`chatcmpl-${Math.random().toString(36).slice(2,14)}`,i;try{i=Ot(a)}catch(r){if(r instanceof ot){w(t,400,r.message);return}throw r}let d=dt(e);o.model(d);let _=Array.isArray(e.tools)&&e.tools.length>0,v;try{v=await n.createResponse({model:e.model,input:i,stream:!0,tools:St(e.tools),tool_choice:e.tool_choice,temperature:e.temperature,max_output_tokens:e.max_completion_tokens??e.max_tokens})}catch(r){if(r instanceof V){pt(t,"openai",r);return}throw r}if(e.stream===!0){ct(t);let r=[],g=new z,T="";t.write(`data: ${JSON.stringify(F(u,c,{role:"assistant"},null))}
|
|
7
|
-
|
|
8
|
-
`);for await(let m of o.track(v,d)){r.push(m);let x=Z(m);if(m.type==="response.output_text.delta"&&typeof m.delta=="string")T+=m.delta,t.write(`data: ${JSON.stringify(F(u,c,{content:m.delta},null))}
|
|
9
|
-
|
|
10
|
-
`);else if(m.type==="response.reasoning_text.delta"&&typeof m.delta=="string")t.write(`data: ${JSON.stringify(F(u,c,{reasoning_content:m.delta},null))}
|
|
11
|
-
|
|
12
|
-
`);else if(x){let{call:O,isNew:j}=g.add(x),B=j?{index:x.tool_index,id:O.id??`call_${x.tool_index}`,type:"function",function:{name:O.name??"",arguments:x.delta}}:{index:x.tool_index,function:{arguments:x.delta}};t.write(`data: ${JSON.stringify(F(u,c,{tool_calls:[B]},null))}
|
|
13
|
-
|
|
14
|
-
`)}else if(m.type==="error"){t.write(`data: ${JSON.stringify({error:m})}
|
|
15
|
-
|
|
16
|
-
`),t.end();return}}let S=it(W(r)),M=K(_,g.size+S.length,T);if(M){t.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:D(d,M)}})}
|
|
17
|
-
|
|
18
|
-
`),t.end();return}g.size===0&&S.length>0&&t.write(`data: ${JSON.stringify(F(u,c,{tool_calls:S},null))}
|
|
19
|
-
|
|
20
|
-
`);let N=g.size>0||S.length>0;t.write(`data: ${JSON.stringify(F(u,c,{},N?"tool_calls":"stop"))}
|
|
21
|
-
|
|
22
|
-
`),t.write(`data: [DONE]
|
|
23
|
-
|
|
24
|
-
`),t.end();return}let A="",f="",I=[],q=new z;for await(let r of o.track(v,d)){I.push(r),r.type==="response.output_text.delta"&&typeof r.delta=="string"&&(A+=r.delta),r.type==="response.reasoning_text.delta"&&typeof r.delta=="string"&&(f+=r.delta);let g=Z(r);if(g&&q.add(g),r.type==="error"){w(t,502,JSON.stringify(r),"upstream_error");return}}let P=W(I),s=it(P),C=s.length>0?s:it({output:q.items()}),R=K(_,C.length,A);if(R){w(t,502,D(d,R),"upstream_error");return}let b=C.map(({index:r,...g})=>g),E={role:"assistant",content:A||null},$=f||_t(P);$&&(E.reasoning_content=$),b.length>0&&(E.tool_calls=b),J(t,200,{id:u,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:c,choices:[{index:0,message:E,finish_reason:b.length>0?"tool_calls":"stop"}],usage:{prompt_tokens:0,completion_tokens:0,total_tokens:0}})}function $t(e){return typeof e=="string"?e:Array.isArray(e)?e.filter(n=>n.type==="text").map(n=>String(n.text??"")).join(""):""}var tt=class extends Error{};function rt(e){return new tt(`unsupported anthropic content block type "${e}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function Mt(e){if(Array.isArray(e))return e.map(n=>{let t=n;return{type:"function",name:t.name,description:t.description,parameters:t.input_schema}})}function Ct(e){if(e==null)return;let n=e.type;if(n==="auto")return"auto";if(n==="none")return"none";if(n==="any")return"required";if(n==="tool")return{type:"function",name:e.name};throw new tt(`unsupported anthropic tool_choice type ${JSON.stringify(String(n))} \u2014 the proxy maps auto, none, any and tool`)}function Tt(e){if(typeof e=="string")return e;if(e==null)return"";if(!Array.isArray(e))throw rt(typeof e);let n="";for(let t of e){let o=String(t.type??"");if(o!=="text")throw rt(`tool_result > ${o}`);n+=String(t.text??"")}return n}function Nt(e){let{role:n,content:t}=e;if(typeof t=="string")return[{role:n,content:t}];if(t==null)return[];if(!Array.isArray(t))throw rt(typeof t);let o=[],a="",c=()=>{a&&(o.push({role:n,content:a}),a="")};for(let u of t){let i=String(u.type??"");if(i==="text")a+=String(u.text??"");else if(i==="tool_use")c(),o.push({type:"function_call",call_id:u.id,name:u.name,arguments:JSON.stringify(u.input??{})});else if(i==="tool_result")c(),o.push({type:"function_call_output",call_id:u.tool_use_id,output:Tt(u.content)});else{if(i==="thinking"||i==="redacted_thinking")continue;throw rt(i)}}return c(),o}function at(e){return Q(e).map((n,t)=>{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_${t}`,name:n.name??"",input:o}})}async function Bt(e,n,t,o){let a=e.messages;if(!Array.isArray(a)){w(t,400,"messages requires a messages array");return}let c=String(e.model??"urun"),u=`msg_${Math.random().toString(36).slice(2,14)}`,i=[],d;try{e.system&&i.push({role:"system",content:$t(e.system)});for(let r of a)i.push(...Nt(r));d=Ct(e.tool_choice)}catch(r){if(r instanceof tt){w(t,400,r.message);return}throw r}let _=dt(e);o.model(_);let v=Array.isArray(e.tools)&&e.tools.length>0,A;try{A=await n.createResponse({model:e.model,input:i,stream:!0,tools:Mt(e.tools),tool_choice:d,temperature:e.temperature,max_output_tokens:e.max_tokens})}catch(r){if(r instanceof V){pt(t,"anthropic",r);return}throw r}let f=(r,g)=>{t.write(`event: ${r}
|
|
25
|
-
data: ${JSON.stringify({type:r,...g})}
|
|
26
|
-
|
|
27
|
-
`)};if(e.stream===!0){let r=o.track(A,_);ct(t),f("message_start",{message:{id:u,type:"message",role:"assistant",content:[],model:c,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let g=0,T="",S=0,M="none",N=()=>M,m=-1,x=-1,O=new z,j=[],B=()=>{M!=="none"&&(M="none",f("content_block_stop",{index:m}))},y=(l,k)=>{B(),M=l,m=S++,f("content_block_start",{index:m,content_block:k})};for await(let l of r){j.push(l);let k=Z(l);if(l.type==="response.reasoning_text.delta"&&typeof l.delta=="string")N()!=="thinking"&&y("thinking",{type:"thinking",thinking:""}),f("content_block_delta",{index:m,delta:{type:"thinking_delta",thinking:l.delta}});else if(l.type==="response.output_text.delta"&&typeof l.delta=="string")N()!=="text"&&y("text",{type:"text",text:""}),g+=1,T+=l.delta,f("content_block_delta",{index:m,delta:{type:"text_delta",text:l.delta}});else if(k){let{call:X}=O.add(k);(N()!=="tool"||k.tool_index!==x)&&(x=k.tool_index,y("tool",{type:"tool_use",id:X.id??`call_${k.tool_index}`,name:X.name??"",input:{}})),k.delta&&f("content_block_delta",{index:m,delta:{type:"input_json_delta",partial_json:k.delta}})}else if(l.type==="error"){f("error",{error:{type:"api_error",message:JSON.stringify(l)}}),t.end();return}}let L=W(j),H=Q(L),U=K(v,O.size+H.length,T);if(U){f("error",{error:{type:"api_error",message:D(_,U)}}),t.end();return}B();let p=[];if(O.size===0)try{p=at(L)}catch(l){f("error",{error:{type:"api_error",message:String(l instanceof Error?l.message:l)}}),t.end();return}S===0&&p.length===0&&(y("text",{type:"text",text:""}),B()),p.forEach(l=>{let k=S++;f("content_block_start",{index:k,content_block:{...l,input:{}}}),f("content_block_delta",{index:k,delta:{type:"input_json_delta",partial_json:JSON.stringify(l.input??{})}}),f("content_block_stop",{index:k})});let h=O.size>0||p.length>0;f("message_delta",{delta:{stop_reason:h?"tool_use":"end_turn"},usage:{output_tokens:g}}),f("message_stop",{}),t.end();return}let I="",q="",P=[],s=new z;for await(let r of o.track(A,_)){P.push(r),r.type==="response.output_text.delta"&&typeof r.delta=="string"&&(I+=r.delta),r.type==="response.reasoning_text.delta"&&typeof r.delta=="string"&&(q+=r.delta);let g=Z(r);if(g&&s.add(g),r.type==="error"){w(t,502,JSON.stringify(r),"upstream_error");return}}let C=W(P),R;try{R=at(C),R.length===0&&s.size>0&&(R=at({output:s.items()}))}catch(r){w(t,502,String(r instanceof Error?r.message:r),"upstream_error");return}let b=K(v,R.length,I);if(b){w(t,502,D(_,b),"upstream_error");return}let E=[],$=q||_t(C);$&&E.push({type:"thinking",thinking:$}),(I||R.length===0)&&E.push({type:"text",text:I}),E.push(...R),J(t,200,{id:u,type:"message",role:"assistant",content:E,model:c,stop_reason:R.length>0?"tool_use":"end_turn",usage:{input_tokens:0,output_tokens:0}})}function zt(e){let{clients:n,apiKey:t,identity:o}=e,a=new lt,c=new ut;return yt((u,i)=>{(async()=>{let d=new URL(u.url??"/","http://localhost");if(u.method==="GET"&&d.pathname==="/healthz"){J(i,200,{ok:!0});return}if(u.method==="GET"&&d.pathname==="/stats"){J(i,200,o?{identity:o,...c.snapshot()}:c.snapshot());return}if(t&&(u.headers.authorization??"")!==`Bearer ${t}`){w(i,401,"invalid local proxy api key","authentication_error");return}if(u.method==="GET"&&d.pathname==="/v1/models"){J(i,200,await n.listModels());return}if(u.method!=="POST"){w(i,404,`no route for ${u.method} ${d.pathname}`);return}let _;try{_=await wt(u)}catch(A){if(A instanceof nt){w(i,413,A.message,"request_too_large");return}w(i,400,"request body is not valid JSON");return}let v=Buffer.byteLength(JSON.stringify(_));if(d.pathname==="/v1/responses"){c.request("responses",v,v),await Et(_,n,a,i,c);return}if(d.pathname==="/v1/chat/completions"){c.request("chat_completions",v,v),await bt(_,n,i,c);return}if(d.pathname==="/v1/messages"){c.request("messages",v,v),await Bt(_,n,i,c);return}w(i,404,`no route for POST ${d.pathname}`)})().catch(d=>{i.headersSent?i.end():w(i,500,String(d),"proxy_error")})})}export{zt as a};
|
package/dist/chunk-5CEMLB6H.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var f="https://api.urun.sh/v1";function l(a){return a.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}var r=class extends Error{};async function m(a){let e=a.fetchImpl??fetch,t=`${a.apiUrl.replace(/\/+$/,"")}/apps`,s=await e(t,{headers:{Authorization:`Bearer ${a.apiKey}`,Accept:"application/json"}});if(!s.ok)throw new Error(`org apps listing failed: GET ${t} \u2192 ${s.status}`);let i=await s.json();if(!Array.isArray(i.apps))throw new Error(`org apps listing returned no "apps" array (GET ${t})`);if(i.truncated===!0)throw new Error(`org apps listing was truncated (GET ${t} returned ${i.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return i.apps}var u=6e4,c=class{constructor(e){this.opts=e}opts;pool=new Map;appsCache=null;seed(e,t){this.pool.set(e,Promise.resolve(t))}async deployedApps(){if(!this.opts.listApps)return[];let e=this.opts.appsTtlMs??u;if(this.appsCache&&Date.now()-this.appsCache.at<e)return this.appsCache.apps;let t=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:t},t}servable(e){return e.filter(t=>t.function_name===this.opts.fnName&&t.deployment_status==="active").map(t=>t.app_slug)}async availableIds(){let e=new Set([this.opts.defaultApp]);if(this.opts.listApps)for(let t of this.servable(await this.deployedApps()))e.add(t);for(let t of this.pool.keys())e.add(t);return[this.opts.defaultApp,...[...e].filter(t=>t!==this.opts.defaultApp).sort()]}async resolveApp(e){let t=(e??"").trim();if(!t||t==="urun")return this.opts.defaultApp;let s=l(t);if(s===this.opts.defaultApp)return this.opts.defaultApp;if(this.pool.has(s))return s;if(this.opts.listApps){let i=await this.deployedApps(),p=this.servable(i);if(p.includes(s))return s;let o=p.filter(n=>n.startsWith(`${s}-`));if(o.length===1)return o[0];if(o.length>1)throw new r(`model "${t}" is ambiguous across deployed apps ${o.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let d=i.find(n=>n.app_slug===s||n.app_slug.startsWith(`${s}-`));if(d)throw new r(`model "${t}" maps to app "${d.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${t}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.listCatalog){let p=(await this.opts.listCatalog()).find(o=>t===o.model_id||t===`${o.model_id}:${o.variant}`||s===l(o.model_id)||s===l(`${o.model_id}-${o.variant}`));if(p)throw new r(`model "${t}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${p.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return this.opts.defaultApp}async sessionFor(e){let t=await this.resolveApp(e),s=this.pool.get(t);return s||(s=Promise.resolve(this.opts.openSession(t)),this.pool.set(t,s),s.catch(()=>this.pool.delete(t))),{app:t,entry:await s}}async evict(e,t){let s=this.pool.get(e);!s||await s.then(p=>p,()=>null)!==t||this.pool.get(e)!==s||(this.pool.delete(e),await this.opts.closeSession(t).catch(()=>{}))}async modelList(){return{object:"list",data:(await this.availableIds()).map(t=>({id:t,object:"model",created:0,owned_by:"urun"}))}}async closeAll(){let e=[...this.pool.values()];this.pool.clear();let t=[];if(await Promise.all(e.map(async s=>{try{await this.opts.closeSession(await s)}catch(i){t.push(String(i instanceof Error?i.message:i))}})),t.length>0)throw new Error(`failed to close ${t.length} pooled session(s): ${t.join("; ")}`)}};export{f as a,r as b,m as c,c as d};
|