@urun-sh/openai 0.3.0 → 0.3.2
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-BYx3YLGo.d.ts +26 -0
- package/dist/ResponsesClient-Dft3bg3b.d.cts +26 -0
- package/dist/chunk-5NXM4IO3.js +2 -0
- package/dist/{chunk-3ED25N7E.js → chunk-AJYWEXQZ.js} +1 -1
- package/dist/chunk-CWJRDBDC.js +1 -0
- package/dist/chunk-FVFF5EWK.js +6 -0
- package/dist/chunk-G3NMGP4N.js +1 -0
- package/dist/chunk-M6ICU4F5.js +40 -0
- package/dist/chunk-UVAY7Q7Z.js +1 -0
- package/dist/gemini-live.cjs +2 -0
- package/dist/gemini-live.d.cts +120 -0
- package/dist/gemini-live.d.ts +79 -0
- package/dist/gemini-live.js +1 -0
- package/dist/hosted/bin.cjs +18 -18
- package/dist/hosted/bin.js +1 -1
- package/dist/hosted/index.cjs +17 -17
- package/dist/hosted/index.d.cts +4 -3
- package/dist/hosted/index.d.ts +4 -3
- package/dist/hosted/index.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -1
- package/dist/pi-extension/index.d.cts +2 -1
- package/dist/pi-extension/index.d.ts +2 -1
- package/dist/pi-extension/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +53 -53
- package/dist/proxy/cli.cjs +37 -37
- package/dist/proxy/cli.js +1 -1
- package/dist/proxy/index.cjs +21 -21
- package/dist/proxy/index.d.cts +4 -31
- package/dist/proxy/index.d.ts +4 -6
- package/dist/proxy/index.js +1 -1
- package/dist/{server-DiQUIDDg.d.cts → server-BfME37pQ.d.cts} +20 -2
- package/dist/{server-BBKNv7hV.d.ts → server-l1himPxc.d.ts} +6 -2
- package/dist/translator-C9uPKypK.d.ts +130 -0
- package/dist/translator-CcDBEfvm.d.cts +227 -0
- package/dist/{ResponsesClient-y8g6OfNN.d.cts → types-lsVTbNcH.d.cts} +16 -24
- package/dist/{ResponsesClient-y8g6OfNN.d.ts → types-lsVTbNcH.d.ts} +8 -24
- package/dist/video-out-CWesbk12.d.ts +155 -0
- package/dist/video-out-D20UuJ8G.d.cts +298 -0
- package/package.json +12 -2
- package/dist/chunk-35LB5OHI.js +0 -1
- package/dist/chunk-EFBD3CGZ.js +0 -1
- package/dist/chunk-VG3X2LVG.js +0 -41
- package/dist/chunk-XHRPJIEK.js +0 -6
- package/dist/media-DCHTX3Ez.d.cts +0 -68
- package/dist/media-DCHTX3Ez.d.ts +0 -42
package/dist/proxy/index.cjs
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
"use strict";var
|
|
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(t){let n=b(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=b(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(!Ee.includes(n.activityHandling))throw new i(`unsupported activityHandling ${JSON.stringify(n.activityHandling)} \u2014 expected one of ${Ee.join(", ")}`);o=n.activityHandling}return{manualActivity:!0,activityHandling:o}}var dt=["temperature","maxOutputTokens","responseModalities"];function $e(t){for(let[u,f]of Object.entries(ut))if(t[u]!==void 0)throw new i(f);for(let u of Object.keys(t))if(!lt.includes(u))throw new i(`unsupported setup field "${u}"`);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=b(t.generationConfig);if(t.generationConfig!==void 0&&!r)throw new i("setup.generationConfig must be an object");if(r){for(let u of Object.keys(r))if(!dt.includes(u))throw new i(`unsupported generationConfig field "${u}"`);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 u=Array.isArray(r.responseModalities)?r.responseModalities:[r.responseModalities];for(let f of u){let g=String(f).toUpperCase();if(g==="AUDIO"){s=!0;continue}if(g!=="TEXT")throw new i(`responseModalities ${JSON.stringify(f)} 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:Pe(t.systemInstruction,"setup.systemInstruction")});let d;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new i("setup.tools must be an array");d=[];for(let u of t.tools){let f=b(u);if(!f)throw new i("setup.tools entries must be objects");for(let g of Object.keys(f))if(g!=="functionDeclarations")throw new i(`unsupported tool "${g}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(f.functionDeclarations))throw new i("tool.functionDeclarations must be an array");for(let g of f.functionDeclarations){let y=b(g);if(!y||typeof y.name!="string")throw new i("functionDeclarations entries require a string name");d.push({type:"function",name:y.name,description:y.description,parameters:y.parameters})}}d.length===0&&(d=void 0)}let m={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let u=b(t.sessionResumption);if(!u)throw new i("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let f of Object.keys(u))if(f!=="handle")throw new i(`unsupported sessionResumption field "${f}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(u.handle!==void 0&&(typeof u.handle!="string"||u.handle.length===0))throw new i("sessionResumption.handle must be a non-empty string when present");m={enabled:!0,handle:typeof u.handle=="string"?u.handle:null}}let p=t.realtimeInputConfig===void 0?at:ct(t.realtimeInputConfig);return{model:n,systemItems:a,tools:d,temperature:e,maxOutputTokens:o,audioModality:s,activity:p,sessionResumption:m}}function Ne(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function Me(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=b(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:t.turnComplete===!0}}var ce=16e3,Ue=24e3,pt=`audio/pcm;rate=${Ue}`,De=Buffer.alloc(960).toString("base64");function ft(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 d=0;d<o;d++){let m=d*r,p=Math.floor(m);if(p>=a){s[d]=t[a];continue}let u=m-p;s[d]=Math.round(t[p]+(t[p+1]-t[p])*u)}return s}function mt(t){let n=b(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 gt(t){let n=b(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])!==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 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=ft(s,ce,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=b(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:gt(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:mt(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 He(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=b(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 je(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function qe(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:pt,data:t}}]}}}}function Fe(t){let n=b(t)??{},e=Array.isArray(n.output)?n.output:[],o=[],s=[],r=typeof n.output_text=="string"?n.output_text:"";if(!r)for(let p of e){let u=b(p);if(!(u?.type!=="message"||!Array.isArray(u.content)))for(let f of u.content){let g=b(f);g?.type==="output_text"&&typeof g.text=="string"&&(r+=g.text)}}r&&s.push({role:"assistant",content:r});for(let p of e){let u=b(p);if(u?.type!=="function_call")continue;if(typeof u.call_id!="string"||typeof u.name!="string")throw new i(`upstream function_call item is missing call_id/name: ${JSON.stringify(p)}`,J);let f=typeof u.arguments=="string"&&u.arguments.length>0?u.arguments:"{}",g;try{g=JSON.parse(f)}catch{throw new i(`upstream function_call arguments are not valid JSON: ${f}`,J)}o.push({id:u.call_id,name:u.name,args:g}),s.push({type:"function_call",call_id:u.call_id,name:u.name,arguments:f})}let a=[];o.length>0&&a.push({toolCall:{functionCalls:o}}),a.push({serverContent:{generationComplete:!0}});let d=b(n.usage),m={serverContent:{turnComplete:!0}};if(d){let p={};typeof d.input_tokens=="number"&&(p.promptTokenCount=d.input_tokens),typeof d.output_tokens=="number"&&(p.responseTokenCount=d.output_tokens),typeof d.total_tokens=="number"&&(p.totalTokenCount=d.total_tokens),Object.keys(p).length>0&&(m.usageMetadata=p)}return a.push(m),{messages:a,assistantItems:s}}var ht=256,_e=class{snapshots=new Map;store(n,e){for(this.snapshots.set(n,e);this.snapshots.size>ht;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(n){return this.snapshots.get(n)}},se=t=>t.slice(0,120),ke=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 F){this.ws.close(Te,se(n.message));return}if(n instanceof ne){this.ws.close(we,se(n.message));return}if(n instanceof i){this.ws.close(n.closeCode,se(n.message));return}this.ws.close(J,se(String(n instanceof Error?n.message:n)))}onMessage(n){try{let e=Ce(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:De);return}this.transcript.push(...He(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=$e(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",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 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)",J);return this.audio=(async()=>{let o=await e.call(this.clients,n.model);return this.detachAudio=o.onOutputAudio(s=>this.send(qe(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)",J);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=(0,Je.randomUUID)();this.registry.store(o,{model:n.model,proxyHandle:e,transcript:[...this.transcript]}),this.send(Ne(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(Me(n.timeLeftMs)),this.ws.close(J,se(`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 m of o){if(e.interrupted)break;let p=m;if(p.type==="error")throw new i(`upstream error: ${JSON.stringify(p)}`,J);if(p.type==="response.completed"){s=p.response,r=!0;continue}let u=je(p);u&&this.send(u)}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:d}=Fe(s);this.transcript.push(...d);for(let m of a)this.send(m);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},yt=(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 Ge(t,n){let e=new Ve.WebSocketServer({noServer:!0}),o=new _e;t.on("upgrade",(s,r,a)=>{let d=new URL(s.url??"/","http://localhost");if(d.pathname!==de){r.write(`HTTP/1.1 404 Not Found\r
|
|
1
|
+
"use strict";var _e=Object.defineProperty;var rt=Object.getOwnPropertyDescriptor;var st=Object.getOwnPropertyNames;var it=Object.prototype.hasOwnProperty;var at=(e,n)=>{for(var t in n)_e(e,t,{get:n[t],enumerable:!0})},ut=(e,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of st(n))!it.call(e,r)&&r!==t&&_e(e,r,{get:()=>n[r],enumerable:!(o=rt(n,r))||o.enumerable});return e};var ct=e=>ut(_e({},"__esModule",{value:!0}),e);var Ft={};at(Ft,{GEMINI_LIVE_PATH:()=>fe,VIDEO_OUT_SETUP_KEY:()=>j,createOpenAIProxy:()=>ot});module.exports=ct(Ft);var tt=require("http");var ze=require("crypto"),Ye=require("ws");var q=class extends Error{},re=class extends Error{};var le=require("@urun-sh/core");var Ce="urun.videoOut.v2-ws";var fe="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",dt=1007,Le=1008,J=1011,ke=1008,a=class extends Error{constructor(t,o=dt){super(t);this.closeCode=o}closeCode},A=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)?e:null,de=["setup","clientContent","realtimeInput","toolResponse"];function $e(e){let n;try{n=JSON.parse(e)}catch{throw new a("client message is not valid JSON")}let t=A(n);if(!t)throw new a("client message must be a JSON object");let o=de.filter(l=>t[l]!==void 0),r=Object.keys(t).filter(l=>!de.includes(l));if(r.length>0)throw new a(`unknown client message field(s) ${JSON.stringify(r)} \u2014 expected exactly one of ${de.join(", ")}`);if(o.length!==1)throw new a(`client message must contain exactly one of ${de.join(", ")} (got ${o.length})`);let s=o[0],i=A(t[s]);if(!i)throw new a(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function Ne(e,n){if(typeof e=="string")return e;let t=A(e),o=t?t.parts:void 0;if(!Array.isArray(o))throw new a(`${n}: expected a Content with a parts array`);let r=[];for(let s of o){let i=A(s);if(i&&typeof i.text=="string"){r.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new a(`${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`):i&&(i.functionCall!==void 0||i.functionResponse!==void 0)?new a(`${n}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new a(`${n}: unsupported part ${JSON.stringify(s)}`)}return r.join(`
|
|
2
|
+
`)}var pt={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},j="urun.videoOut";function Ue(e){return{framing:Ce,codec:e}}function Me(e){let{seq:n,ptsUs:t,codec:o,key:r}=e.header;return{[j]:{v:1,seq:n,ptsUs:t,codec:o,key:r,data:Buffer.from(e.au).toString("base64")}}}var ft={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},mt=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",j],Pe=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function ht(e){let n=A(e);if(!n)throw new a("setup.realtimeInputConfig must be an object");for(let r of Object.keys(n))if(!(r==="automaticActivityDetection"||r==="activityHandling"))throw new a(r==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${r}"`);let t=A(n.automaticActivityDetection);if(n.automaticActivityDetection!==void 0&&!t)throw new a("realtimeInputConfig.automaticActivityDetection must be an object");for(let r of Object.keys(t??{}))if(r!=="disabled")throw new a(`automaticActivityDetection.${r} 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(t?.disabled!==!0)throw new a("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(!Pe.includes(n.activityHandling))throw new a(`unsupported activityHandling ${JSON.stringify(n.activityHandling)} \u2014 expected one of ${Pe.join(", ")}`);o=n.activityHandling}return{manualActivity:!0,activityHandling:o}}var gt=["temperature","maxOutputTokens","responseModalities"];function Ve(e){for(let[f,m]of Object.entries(ft))if(e[f]!==void 0)throw new a(m);for(let f of Object.keys(e))if(!mt.includes(f))throw new a(`unsupported setup field "${f}"`);if(typeof e.model!="string"||e.model.length===0)throw new a('setup.model is required (format "models/{model}")');let n=e.model.replace(/^models\//,""),t,o,r=!1,s=A(e.generationConfig);if(e.generationConfig!==void 0&&!s)throw new a("setup.generationConfig must be an object");if(s){for(let f of Object.keys(s))if(!gt.includes(f))throw new a(`unsupported generationConfig field "${f}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new a("generationConfig.temperature must be a number");t=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new a("generationConfig.maxOutputTokens must be a number");o=s.maxOutputTokens}if(s.responseModalities!==void 0){let f=Array.isArray(s.responseModalities)?s.responseModalities:[s.responseModalities];for(let m of f){let y=String(m).toUpperCase();if(y==="AUDIO"){r=!0;continue}if(y!=="TEXT")throw new a(`responseModalities ${JSON.stringify(m)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge rt-audio-out lane)`)}}}let i=[];e.systemInstruction!==void 0&&i.push({role:"system",content:Ne(e.systemInstruction,"setup.systemInstruction")});let l;if(e.tools!==void 0){if(!Array.isArray(e.tools))throw new a("setup.tools must be an array");l=[];for(let f of e.tools){let m=A(f);if(!m)throw new a("setup.tools entries must be objects");for(let y of Object.keys(m))if(y!=="functionDeclarations")throw new a(`unsupported tool "${y}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(m.functionDeclarations))throw new a("tool.functionDeclarations must be an array");for(let y of m.functionDeclarations){let w=A(y);if(!w||typeof w.name!="string")throw new a("functionDeclarations entries require a string name");l.push({type:"function",name:w.name,description:w.description,parameters:w.parameters})}}l.length===0&&(l=void 0)}let h={enabled:!1,handle:null};if(e.sessionResumption!==void 0){let f=A(e.sessionResumption);if(!f)throw new a("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let m of Object.keys(f))if(m!=="handle")throw new a(`unsupported sessionResumption field "${m}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(f.handle!==void 0&&(typeof f.handle!="string"||f.handle.length===0))throw new a("sessionResumption.handle must be a non-empty string when present");h={enabled:!0,handle:typeof f.handle=="string"?f.handle:null}}let d=e.realtimeInputConfig===void 0?pt:ht(e.realtimeInputConfig),p=!1;if(e[j]!==void 0){if(!A(e[j]))throw new a(`setup["${j}"] must be an object (the v1 opt-in is {})`);p=!0}return{model:n,systemItems:i,tools:l,temperature:t,maxOutputTokens:o,audioModality:r,activity:d,sessionResumption:h,videoOut:p}}function De(e){return{sessionResumptionUpdate:{newHandle:e,resumable:!0}}}function Be(e){let n=Math.max(0,e??0)/1e3;return{goAway:{timeLeft:`${Math.round(n*1e3)/1e3}s`}}}function He(e){for(let o of Object.keys(e))if(o!=="turns"&&o!=="turnComplete")throw new a(`unsupported clientContent field "${o}"`);let n=e.turns===void 0?[]:e.turns;if(!Array.isArray(n))throw new a("clientContent.turns must be an array");let t=[];for(let o of n){let r=A(o);if(!r)throw new a("clientContent.turns entries must be Content objects");let s=r.role===void 0?"user":String(r.role).toLowerCase();if(s!=="user"&&s!=="model")throw new a(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(r.role)})`);t.push({role:s==="model"?"assistant":"user",content:Ne(r,"clientContent.turns")})}return{items:t,turnComplete:e.turnComplete===!0}}var pe=16e3,Fe=24e3,yt=`audio/pcm;rate=${Fe}`,qe=Buffer.alloc(960).toString("base64");function wt(e,n,t){if(n===t)return e;let o=Math.floor(e.length*t/n),r=new Int16Array(o),s=n/t,i=e.length-1;for(let l=0;l<o;l++){let h=l*s,d=Math.floor(h);if(d>=i){r[l]=e[i];continue}let p=h-d;r[l]=Math.round(e[d]+(e[d+1]-e[d])*p)}return r}function _t(e){let n=A(e);if(!n)throw new a("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof n.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(n.mimeType))throw new a(`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 a("realtimeInput.video.data must be non-empty base64");let t=Buffer.from(n.data,"base64");if(t.length<3||t[0]!==255||t[1]!==216)throw new a("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(t)}function kt(e){let n=A(e);if(!n)throw new a("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof n.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(n.mimeType))throw new a(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(n.mimeType)}`);let t=/;\s*rate=(\d+)/.exec(n.mimeType);if(t&&Number(t[1])!==pe)throw new a(`realtimeInput.audio.mimeType declares rate=${t[1]} \u2014 the Live spec input format is ${pe} Hz (audio/pcm;rate=${pe})`);if(typeof n.data!="string"||n.data.length===0)throw new a("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 a(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${o.length} bytes \u2014 not even)`);let r=new Int16Array(o.length/2);for(let i=0;i<r.length;i++)r[i]=o.readInt16LE(i*2);let s=wt(r,pe,Fe);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function je(e){if(e.mediaChunks!==void 0)throw new a('realtimeInput.mediaChunks is deprecated by the Live API \u2014 send the audio stream via realtimeInput.audio ({mimeType:"audio/pcm;rate=16000", data}) instead');if(e.activityStart!==void 0||e.activityEnd!==void 0){if(Object.keys(e).length!==1||e.activityStart!==void 0&&e.activityEnd!==void 0)throw new a("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let r=e.activityStart!==void 0?"activityStart":"activityEnd",s=A(e[r]);if(!s||Object.keys(s).length!==0)throw new a(`realtimeInput.${r} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:r}}let n=["text","audio","audioStreamEnd","video"];for(let o of Object.keys(e))if(!n.includes(o))throw new a(`unsupported realtimeInput field "${o}"`);let t=n.filter(o=>e[o]!==void 0);if(t.length!==1)throw new a(`realtimeInput must carry exactly one of ${n.join(", ")} (got ${t.length})`);if(e.audio!==void 0)return{kind:"audio",base64Pcm24k:kt(e.audio)};if(e.video!==void 0)return{kind:"video",jpegFrame:_t(e.video)};if(e.audioStreamEnd!==void 0){if(e.audioStreamEnd!==!0)throw new a("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof e.text!="string"||e.text.length===0)throw new a("realtimeInput.text must be a non-empty string");return{kind:"text",text:e.text}}function Je(e){for(let n of Object.keys(e))if(n!=="functionResponses")throw new a(`unsupported toolResponse field "${n}"`);if(!Array.isArray(e.functionResponses)||e.functionResponses.length===0)throw new a("toolResponse.functionResponses must be a non-empty array");return e.functionResponses.map(n=>{let t=A(n);if(!t||typeof t.id!="string"||t.id.length===0)throw new a("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:t.id,output:JSON.stringify(t.response??null)}})}function We(e){return e.type!=="response.output_text.delta"||typeof e.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:e.delta}]}}}}function Ge(e){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:yt,data:e}}]}}}}function Ke(e){let n=A(e)??{},t=Array.isArray(n.output)?n.output:[],o=[],r=[],s=typeof n.output_text=="string"?n.output_text:"";if(!s)for(let d of t){let p=A(d);if(!(p?.type!=="message"||!Array.isArray(p.content)))for(let f of p.content){let m=A(f);m?.type==="output_text"&&typeof m.text=="string"&&(s+=m.text)}}s&&r.push({role:"assistant",content:s});for(let d of t){let p=A(d);if(p?.type!=="function_call")continue;if(typeof p.call_id!="string"||typeof p.name!="string")throw new a(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,J);let f=typeof p.arguments=="string"&&p.arguments.length>0?p.arguments:"{}",m;try{m=JSON.parse(f)}catch{throw new a(`upstream function_call arguments are not valid JSON: ${f}`,J)}o.push({id:p.call_id,name:p.name,args:m}),r.push({type:"function_call",call_id:p.call_id,name:p.name,arguments:f})}let i=[];o.length>0&&i.push({toolCall:{functionCalls:o}}),i.push({serverContent:{generationComplete:!0}});let l=A(n.usage),h={serverContent:{turnComplete:!0}};if(l){let d={};typeof l.input_tokens=="number"&&(d.promptTokenCount=l.input_tokens),typeof l.output_tokens=="number"&&(d.responseTokenCount=l.output_tokens),typeof l.total_tokens=="number"&&(d.totalTokenCount=l.total_tokens),Object.keys(d).length>0&&(h.usageMetadata=d)}return i.push(h),{messages:i,assistantItems:r}}var vt=256,ve=class{snapshots=new Map;store(n,t){for(this.snapshots.set(n,t);this.snapshots.size>vt;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(n){return this.snapshots.get(n)}},se=e=>e.slice(0,120),xe=class{constructor(n,t,o){this.ws=n;this.clients=t;this.registry=o;n.on("message",r=>this.onMessage(r)),n.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.(),this.detachVideoOut?.()})}ws;clients;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;detachVideoOut=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 q){this.ws.close(Le,se(n.message));return}if(n instanceof re){this.ws.close(ke,se(n.message));return}if(n instanceof a){this.ws.close(n.closeCode,se(n.message));return}this.ws.close(J,se(String(n instanceof Error?n.message:n)))}onMessage(n){try{let t=$e(typeof n=="string"?n:Buffer.concat(Array.isArray(n)?n:[n]).toString("utf8"));if(t.kind==="setup"){this.handleSetup(t.setup).catch(r=>this.fail(r));return}if(!this.config)throw new a(`"${t.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let o=this.config;if(t.kind==="clientContent"){let{items:r,turnComplete:s}=He(t.clientContent);this.transcript.push(...r),s&&this.enqueue(()=>this.runTurn());return}if(t.kind==="realtimeInput"){let r=je(t.realtimeInput);if(r.kind==="activityStart"||r.kind==="activityEnd"){this.onActivity(r.kind,o);return}if(r.kind==="text"){this.transcript.push({role:"user",content:r.text}),this.enqueue(()=>this.runTurn());return}if(r.kind==="video"){this.appendVideoFrame(r.jpegFrame);return}this.appendAudio(r.kind==="audio"?r.base64Pcm24k:qe);return}this.transcript.push(...Je(t.toolResponse)),this.enqueue(()=>this.runTurn())}catch(t){this.fail(t)}}async handleSetup(n){if(this.config)throw new a("setup may only be sent once, as the first client message");let t=Ve(n),o=t.sessionResumption.handle;if(o!==null){let s=this.registry.get(o);if(!s)throw new a("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",ke);if(s.model!==t.model)throw new a(`sessionResumption.handle was issued for model "${s.model}" \u2014 the model cannot change on resume (got "${t.model}", per the Live API session-resumption contract)`);if(t.systemItems.length>0)throw new a("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,i=>this.enqueue(()=>this.handleUpstreamEnd(i)))}else this.proxyHandle=await this.clients.sessionHandle(t.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.transcript.push(...t.systemItems);this.config=t;let r={};if(t.videoOut){let s=this.clients.openVideoOut;if(s){let i=await s.call(this.clients,t.model),l=i.onOutputFrame(d=>this.send(Me(d))),h=i.onLaneError(d=>this.fail(d));this.detachVideoOut=()=>{l(),h()},r[j]=Ue(i.codec)}}this.send({setupComplete:r}),t.audioModality&&this.ensureAudioLane(),t.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let n=this.config;if(!n)throw new a("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let t=this.clients.openAudio;if(!t)throw new a("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 t.call(this.clients,n.model);return this.detachAudio=o.onOutputAudio(r=>this.send(Ge(r))),o})(),this.audio.catch(o=>this.fail(o)),this.audio}appendAudio(n){this.ensureAudioLane().then(t=>t.appendInputAudio(n)).catch(t=>this.fail(t))}ensureVideoLane(){if(this.video)return this.video;let n=this.config;if(!n)throw new a("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let t=this.clients.openVideo;if(!t)throw new a("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(t.call(this.clients,n.model)),this.video.catch(o=>this.fail(o)),this.video}appendVideoFrame(n){this.ensureVideoLane().then(t=>t.sendInputFrame(n)).catch(t=>this.fail(t))}onActivity(n,t){if(!t.activity.manualActivity)throw new a(`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 a("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,t.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new a("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(t=>this.fail(t))}async mintResumptionUpdate(){let n=this.config;if(!n?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let t=await this.clients.sessionHandle(n.model);this.proxyHandle=t;let o=(0,ze.randomUUID)();this.registry.store(o,{model:n.model,proxyHandle:t,transcript:[...this.transcript]}),this.send(De(o))}async handleUpstreamEnd(n){if(this.ws.readyState!==this.ws.OPEN||!this.config)return;this.unsubscribeEnd?.(),this.unsubscribeEnd=null;let t=null;try{t=await this.clients.sessionHandle(this.config.model)}catch{}if(t!==null&&t!==this.proxyHandle){this.proxyHandle=t,this.unsubscribeEnd=await this.clients.onSessionEnd(t,o=>this.enqueue(()=>this.handleUpstreamEnd(o))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(Be(n.timeLeftMs)),this.ws.close(J,se(`upstream session ended: ${n.reason}`))}async runTurn(){let n=this.config;if(!n||this.ws.readyState!==this.ws.OPEN)return;let t={interrupted:!1};this.activeTurn=t;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}),r,s=!1;for await(let h of o){if(t.interrupted)break;let d=h;if(d.type==="error")throw new a(`upstream error: ${JSON.stringify(d)}`,J);if(d.type==="response.completed"){r=d.response,s=!0;continue}let p=We(d);p&&this.send(p)}if(t.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new a("upstream produced no response.completed event",J);let{messages:i,assistantItems:l}=Ke(r);this.transcript.push(...l);for(let h of i)this.send(h);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},xt=(e,n)=>{let t=e.headers["x-goog-api-key"];if(typeof t=="string")return t;let o=e.headers.authorization;return typeof o=="string"&&o.startsWith("Bearer ")?o.slice(7):n.searchParams.get("key")??void 0};function Xe(e,n){let t=new Ye.WebSocketServer({noServer:!0}),o=new ve;e.on("upgrade",(r,s,i)=>{let l=new URL(r.url??"/","http://localhost");if(l.pathname!==fe){s.write(`HTTP/1.1 404 Not Found\r
|
|
3
3
|
Connection: close\r
|
|
4
4
|
\r
|
|
5
|
-
no WS route for ${
|
|
5
|
+
no WS route for ${l.pathname}`),s.destroy();return}if(n.apiKey&&xt(r,l)!==n.apiKey){s.write(`HTTP/1.1 401 Unauthorized\r
|
|
6
6
|
Connection: close\r
|
|
7
7
|
\r
|
|
8
|
-
invalid local proxy api key`),
|
|
9
|
-
data: ${JSON.stringify(
|
|
8
|
+
invalid local proxy api key`),s.destroy();return}t.handleUpgrade(r,s,i,h=>{new xe(h,n.clients,o)})})}var Se=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>=bt?"(other)":n;return this.models[o]??={requests:0,tokens_out:0}}model(n){this.modelEntry(n).requests+=1}track(n,t){let o=this,r=performance.now(),s=null,i=0;return(async function*(){for await(let h of n)h.type==="response.output_text.delta"&&typeof h.delta=="string"&&(s===null&&(s=performance.now(),o.ttftMs.push(s-r),o.ttftMs.length>512&&o.ttftMs.shift()),i+=1,o.tokensOut+=1,o.charsOut+=h.delta.length,t!==void 0&&(o.modelEntry(t).tokens_out+=1)),yield h;let l=(performance.now()-(s??r))/1e3;i>0&&l>0&&(o.tokRates.push(i/l),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(n){if(!n.length)return null;let t=[...n].sort((o,r)=>o-r);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 Re(e){e.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function B(e,n,t){e.writeHead(n,{"content-type":"application/json"}),e.end(JSON.stringify(t))}function W(e,n,t,o="invalid_request_error"){B(e,n,{error:{message:t,type:o}})}function D(e,n,t,o){B(e,n,{type:"error",error:{type:t,message:o}})}function Ie(e,n,t){if(n==="anthropic"){B(e,404,{type:"error",error:{type:"not_found_error",message:t.message}});return}B(e,404,{error:{message:t.message,type:"invalid_request_error",code:"model_not_found"}})}var bt=256,At=128;function Ee(e){return typeof e.model!="string"||!e.model.trim()?"(default)":e.model.trim().slice(0,At)}var Qe=64*1024*1024,me=class extends Error{};async function St(e){let n=[],t=0;for await(let r of e){if(t+=r.length,t>Qe)throw new me(`request body exceeds ${Qe} bytes`);n.push(r)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function Q(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 Ot=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 Oe=class{conversations=new Map;thread(n,t){let o=Rt(t);if(n==null)return o;let r=this.conversations.get(String(n));if(!r)throw new Error(`unknown previous_response_id ${String(n)} (proxy restarts drop stored responses)`);return[...r,...o]}remember(n,t,o){this.conversations.set(n,[...t,{role:"assistant",content:o}]);for(let r of this.conversations.keys()){if(this.conversations.size<=Ot)break;this.conversations.delete(r)}}};function ie(e){let n=e?.output;return Array.isArray(n)?n.filter(t=>t.type==="function_call"):[]}function K(e){return e.find(n=>n.type==="response.completed")?.response??null}function he(e){let n=e?.usage;if(!n||typeof n!="object")return null;let t=i=>typeof i=="number"&&Number.isFinite(i)&&i>=0?i:0,o=t(n.input_tokens),r=t(n.output_tokens);if(o===0&&r===0)return null;let s=t(n.total_tokens)||o+r;return{input_tokens:o,output_tokens:r,total_tokens:s}}function Ze(e){let n=he(e);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 nt(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 r of o.content)r.type==="reasoning_text"&&(t+=String(r.text??""));return t}function ae(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}))}},It=["<tool_call>","<function="];function Et(e){return It.find(n=>e.includes(n))}function Z(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 ee(e,n,t){if(!(!e||n>0))return Et(t)}function Tt(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 ge=class extends Error{};function Ct(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 r=o.image_url??{};return{type:"input_image",image_url:r.url,detail:r.detail}}throw new ge(`unsupported chat content part type "${String(o.type)}" \u2014 the proxy translates text and image_url parts`)}):n}function Pt(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 r=o.function??{};n.push({type:"function_call",call_id:o.id,name:r.name,arguments:r.arguments})}continue}n.push({...t,content:Ct(t.role,t.content)})}return n}function be(e){return ie(e).map((n,t)=>({index:t,id:n.call_id??n.id??`call_${t}`,type:"function",function:{name:n.name??"",arguments:n.arguments??"{}"}}))}function et(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 Lt(e,n,t,o,r){let s=e.stream===!0,i;try{i=t.thread(e.previous_response_id,e.input)}catch(u){W(o,400,String(u instanceof Error?u.message:u));return}e.previous_response_id!=null&&(r.naiveInBytes+=Buffer.byteLength(JSON.stringify(i))-Buffer.byteLength(JSON.stringify(e.input)));let l=`resp_${Math.random().toString(36).slice(2,14)}`,h=e.store!==!1,d=Ee(e);r.model(d);let p=Array.isArray(e.tools)&&e.tools.length>0,f;try{f=await n.createResponse({model:e.model,input:i,stream:s,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,max_output_tokens:e.max_output_tokens,...e.reasoning_effort!==void 0?{reasoning_effort:e.reasoning_effort}:{},...e.chat_template_kwargs!==void 0?{chat_template_kwargs:e.chat_template_kwargs}:{}})}catch(u){if(u instanceof q){Ie(o,"openai",u);return}throw u}let m=[];if(s){Re(o);let u=g=>{o.write(`event: ${String(g.type??"message")}
|
|
9
|
+
data: ${JSON.stringify(g)}
|
|
10
10
|
|
|
11
|
-
`)},
|
|
11
|
+
`)},R=g=>g.response&&typeof g.response=="object"?{...g,response:{...g.response,id:l}}:g,I=`item_${l.slice(5)}`,$=`rs_${l.slice(5)}`,P=!1,N=!1,c=0,x=()=>{P||(P=!0,u({type:"response.created",response:{id:l,object:"response",status:"in_progress"}}))},M=!1,C=0,U="",V=()=>{M&&(M=!1,u({type:"response.reasoning_text.done",item_id:$,output_index:C,content_index:0,text:U}),u({type:"response.output_item.done",output_index:C,item:{id:$,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:U}],status:"completed"}}))},L=!1,v=0,O="",H=g=>{L&&(L=!1,u({type:"response.output_text.done",item_id:I,output_index:v,content_index:0,text:O}),u({type:"response.content_part.done",item_id:I,output_index:v,content_index:0,part:{type:"output_text",text:O}}),u({type:"response.output_item.done",output_index:v,item:{id:I,type:"message",role:"assistant",status:g,content:[{type:"output_text",text:O}]}}))},F=new z,b=null,Y=-1,te=g=>{if(!b)return;let S=b;b=null,u({type:"response.function_call_arguments.done",item_id:S.fcId,output_index:S.outputIndex,arguments:S.args}),u({type:"response.output_item.done",output_index:S.outputIndex,item:{id:S.fcId,type:"function_call",call_id:S.callId??S.fcId,name:S.name??"",arguments:S.args,status:g}})},ne=!1;for await(let g of r.track(f,d)){m.push(g);let S=String(g.type??"");if(S==="response.created"||S==="response.in_progress"){P=!0,u(R(g));continue}if(S==="response.output_item.added"){N=!0,u(R(g));continue}if(S==="response.reasoning_text.delta"&&!N){x(),M||(M=!0,C=c++,u({type:"response.output_item.added",output_index:C,item:{id:$,type:"reasoning",summary:[],content:[],status:"in_progress"}})),U+=String(g.delta??""),u({type:"response.reasoning_text.delta",item_id:$,output_index:C,content_index:0,delta:g.delta});continue}if(S==="response.output_text.delta"&&!N){x(),V(),L||(L=!0,v=c++,u({type:"response.output_item.added",output_index:v,item:{id:I,type:"message",role:"assistant",status:"in_progress",content:[]}}),u({type:"response.content_part.added",item_id:I,output_index:v,content_index:0,part:{type:"output_text",text:""}})),O+=String(g.delta??""),u({type:S,item_id:I,output_index:v,content_index:0,delta:g.delta});continue}let E=N?null:ae(g);if(E){if(x(),V(),H("completed"),F.add(E),E.tool_index!==Y){te("completed"),Y=E.tool_index;let _=c++;b={fcId:`fc_${l.slice(5)}_${E.tool_index}`,outputIndex:_,callId:E.call_id,name:E.name,args:""},u({type:"response.output_item.added",output_index:_,item:{id:b.fcId,type:"function_call",call_id:b.callId??b.fcId,name:b.name??"",arguments:"",status:"in_progress"}})}b&&(E.call_id&&(b.callId=E.call_id),E.name&&(b.name=E.name),b.args+=E.delta,u({type:"response.function_call_arguments.delta",item_id:b.fcId,output_index:b.outputIndex,delta:E.delta}));continue}if(S==="response.completed"&&!N){let _=ie(g.response),T=ee(p,F.size+_.length,O);if(T){ne=!0,u({type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:Z(d,T)}});break}V(),H("completed"),te("completed"),F.size===0&&_.forEach(G=>{let oe=c++,ce=G.id??G.call_id??`fc_${oe}`,we=G.arguments??"",Te={id:ce,type:"function_call",call_id:G.call_id??ce,name:G.name??""};u({type:"response.output_item.added",output_index:oe,item:{...Te,arguments:"",status:"in_progress"}}),u({type:"response.function_call_arguments.delta",item_id:ce,output_index:oe,delta:we}),u({type:"response.function_call_arguments.done",item_id:ce,output_index:oe,arguments:we}),u({type:"response.output_item.done",output_index:oe,item:{...Te,arguments:we,status:"completed"}})}),u(R(g));continue}u(R(g))}V(),H("incomplete"),te("incomplete"),h&&!ne&&t.remember(l,i,et(m,K(m))),o.write(`data: [DONE]
|
|
12
12
|
|
|
13
|
-
`),o.end();return}let y=null;for await(let
|
|
13
|
+
`),o.end();return}let y=null;for await(let u of r.track(f,d)){if(m.push(u),u.type==="error"){W(o,502,JSON.stringify(u),"upstream_error");return}u.type==="response.completed"&&(y=u.response)}if(y==null){W(o,502,"upstream produced no response.completed event","upstream_error");return}let w=et(m,y),k=ee(p,ie(y).length,w);if(k){W(o,502,Z(d,k),"upstream_error");return}h&&t.remember(l,i,w),B(o,200,{...y,id:l})}async function $t(e,n,t,o){let r=e.messages;if(!Array.isArray(r)){W(t,400,"chat/completions requires a messages array");return}let s=String(e.model??"urun"),i=`chatcmpl-${Math.random().toString(36).slice(2,14)}`,l;try{l=Pt(r)}catch(c){if(c instanceof ge){W(t,400,c.message);return}throw c}let h=Ee(e);o.model(h);let d=Array.isArray(e.tools)&&e.tools.length>0,p;try{p=await n.createResponse({model:e.model,input:l,stream:!0,tools:Tt(e.tools),tool_choice:e.tool_choice,temperature:e.temperature,max_output_tokens:e.max_completion_tokens??e.max_tokens,...e.reasoning_effort!==void 0?{reasoning_effort:e.reasoning_effort}:{},...e.chat_template_kwargs!==void 0?{chat_template_kwargs:e.chat_template_kwargs}:{}})}catch(c){if(c instanceof q){Ie(t,"openai",c);return}throw c}if(e.stream===!0){Re(t);let c=[],x=new z,M="";t.write(`data: ${JSON.stringify(Q(i,s,{role:"assistant"},null))}
|
|
14
14
|
|
|
15
|
-
`);for await(let v of o.track(
|
|
15
|
+
`);for await(let v of o.track(p,h)){c.push(v);let O=ae(v);if(v.type==="response.output_text.delta"&&typeof v.delta=="string")M+=v.delta,t.write(`data: ${JSON.stringify(Q(i,s,{content:v.delta},null))}
|
|
16
16
|
|
|
17
|
-
`);else if(v.type==="response.reasoning_text.delta"&&typeof v.delta=="string")
|
|
17
|
+
`);else if(v.type==="response.reasoning_text.delta"&&typeof v.delta=="string")t.write(`data: ${JSON.stringify(Q(i,s,{reasoning_content:v.delta},null))}
|
|
18
18
|
|
|
19
|
-
`);else if(
|
|
19
|
+
`);else if(O){let{call:H,isNew:F}=x.add(O),b=F?{index:O.tool_index,id:H.id??`call_${O.tool_index}`,type:"function",function:{name:H.name??"",arguments:O.delta}}:{index:O.tool_index,function:{arguments:O.delta}};t.write(`data: ${JSON.stringify(Q(i,s,{tool_calls:[b]},null))}
|
|
20
20
|
|
|
21
|
-
`)}else if(v.type==="error"){
|
|
21
|
+
`)}else if(v.type==="error"){t.write(`data: ${JSON.stringify({error:v})}
|
|
22
22
|
|
|
23
|
-
`),
|
|
23
|
+
`),t.end();return}}let C=be(K(c)),U=ee(d,x.size+C.length,M);if(U){t.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:Z(h,U)}})}
|
|
24
24
|
|
|
25
|
-
`),
|
|
25
|
+
`),t.end();return}x.size===0&&C.length>0&&t.write(`data: ${JSON.stringify(Q(i,s,{tool_calls:C},null))}
|
|
26
26
|
|
|
27
|
-
`);let
|
|
27
|
+
`);let V=x.size>0||C.length>0;t.write(`data: ${JSON.stringify(Q(i,s,{},V?"tool_calls":"stop"))}
|
|
28
28
|
|
|
29
|
-
`),
|
|
29
|
+
`),he(K(c))&&t.write(`data: ${JSON.stringify({id:i,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:s,choices:[],usage:Ze(K(c))})}
|
|
30
30
|
|
|
31
|
-
`),
|
|
31
|
+
`),t.write(`data: [DONE]
|
|
32
32
|
|
|
33
|
-
`),
|
|
33
|
+
`),t.end();return}let f="",m="",y=[],w=new z;for await(let c of o.track(p,h)){y.push(c),c.type==="response.output_text.delta"&&typeof c.delta=="string"&&(f+=c.delta),c.type==="response.reasoning_text.delta"&&typeof c.delta=="string"&&(m+=c.delta);let x=ae(c);if(x&&w.add(x),c.type==="error"){W(t,502,JSON.stringify(c),"upstream_error");return}}let k=K(y),u=be(k),R=u.length>0?u:be({output:w.items()}),I=ee(d,R.length,f);if(I){W(t,502,Z(h,I),"upstream_error");return}let $=R.map(({index:c,...x})=>x),P={role:"assistant",content:f||null},N=m||nt(k);N&&(P.reasoning_content=N),$.length>0&&(P.tool_calls=$),B(t,200,{id:i,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:s,choices:[{index:0,message:P,finish_reason:$.length>0?"tool_calls":"stop"}],usage:Ze(k)})}function Nt(e){return typeof e=="string"?e:Array.isArray(e)?e.filter(n=>n.type==="text").map(n=>String(n.text??"")).join(""):""}var ue=class extends Error{};function ye(e){return new ue(`unsupported anthropic content block type "${e}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function Ut(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 Mt(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 ue(`unsupported anthropic tool_choice type ${JSON.stringify(String(n))} \u2014 the proxy maps auto, none, any and tool`)}function Vt(e){if(typeof e=="string")return e;if(e==null)return"";if(!Array.isArray(e))throw ye(typeof e);let n="";for(let t of e){let o=String(t.type??"");if(o!=="text")throw ye(`tool_result > ${o}`);n+=String(t.text??"")}return n}function Dt(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 ye(typeof t);let o=[],r="",s=()=>{r&&(o.push({role:n,content:r}),r="")};for(let i of t){let l=String(i.type??"");if(l==="text")r+=String(i.text??"");else if(l==="tool_use")s(),o.push({type:"function_call",call_id:i.id,name:i.name,arguments:JSON.stringify(i.input??{})});else if(l==="tool_result")s(),o.push({type:"function_call_output",call_id:i.tool_use_id,output:Vt(i.content)});else{if(l==="thinking"||l==="redacted_thinking")continue;throw ye(l)}}return s(),o}function Ae(e){return ie(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 r=e.messages;if(!Array.isArray(r)){D(t,400,"invalid_request_error","messages requires a messages array");return}if(typeof e.max_tokens!="number"||!Number.isInteger(e.max_tokens)||e.max_tokens<1){D(t,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(e.top_k!==void 0){D(t,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(e.stop_sequences!==void 0&&(!Array.isArray(e.stop_sequences)||e.stop_sequences.some(c=>typeof c!="string"))){D(t,400,"invalid_request_error","stop_sequences must be an array of strings");return}let s=String(e.model??"urun"),i=`msg_${Math.random().toString(36).slice(2,14)}`,l=[],h;try{e.system&&l.push({role:"system",content:Nt(e.system)});for(let c of r)l.push(...Dt(c));h=Mt(e.tool_choice)}catch(c){if(c instanceof ue){D(t,400,"invalid_request_error",c.message);return}throw c}let d=Ee(e);o.model(d);let p=Array.isArray(e.tools)&&e.tools.length>0,f;try{f=await n.createResponse({model:e.model,input:l,stream:!0,tools:Ut(e.tools),tool_choice:h,temperature:e.temperature,max_output_tokens:e.max_tokens,top_p:e.top_p,stop:e.stop_sequences})}catch(c){if(c instanceof q){Ie(t,"anthropic",c);return}throw c}let m=(c,x)=>{t.destroyed||t.write(`event: ${c}
|
|
34
34
|
data: ${JSON.stringify({type:c,...x})}
|
|
35
35
|
|
|
36
|
-
`)};if(
|
|
37
|
-
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:
|
|
36
|
+
`)};if(e.stream===!0){let c=o.track(f,d);Re(t),m("message_start",{message:{id:i,type:"message",role:"assistant",content:[],model:s,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let x=0,M="",C=0,U="none",V=()=>U,L=-1,v=-1,O=new z,H=[],F=()=>{U!=="none"&&(U="none",m("content_block_stop",{index:L}))},b=(_,T)=>{F(),U=_,L=C++,m("content_block_start",{index:L,content_block:T})};for await(let _ of c){if(t.destroyed)break;H.push(_);let T=ae(_);if(_.type==="response.reasoning_text.delta"&&typeof _.delta=="string")V()!=="thinking"&&b("thinking",{type:"thinking",thinking:""}),m("content_block_delta",{index:L,delta:{type:"thinking_delta",thinking:_.delta}});else if(_.type==="response.output_text.delta"&&typeof _.delta=="string")V()!=="text"&&b("text",{type:"text",text:""}),x+=1,M+=_.delta,m("content_block_delta",{index:L,delta:{type:"text_delta",text:_.delta}});else if(T){let{call:G}=O.add(T);(V()!=="tool"||T.tool_index!==v)&&(v=T.tool_index,b("tool",{type:"tool_use",id:G.id??`call_${T.tool_index}`,name:G.name??"",input:{}})),T.delta&&m("content_block_delta",{index:L,delta:{type:"input_json_delta",partial_json:T.delta}})}else if(_.type==="error"){m("error",{error:{type:"api_error",message:JSON.stringify(_)}}),t.end();return}}if(t.destroyed)return;let Y=K(H),te=ie(Y),ne=ee(p,O.size+te.length,M);if(ne){m("error",{error:{type:"api_error",message:Z(d,ne)}}),t.end();return}F();let g=[];if(O.size===0)try{g=Ae(Y)}catch(_){m("error",{error:{type:"api_error",message:String(_ instanceof Error?_.message:_)}}),t.end();return}C===0&&g.length===0&&(b("text",{type:"text",text:""}),F()),g.forEach(_=>{let T=C++;m("content_block_start",{index:T,content_block:{..._,input:{}}}),m("content_block_delta",{index:T,delta:{type:"input_json_delta",partial_json:JSON.stringify(_.input??{})}}),m("content_block_stop",{index:T})});let S=O.size>0||g.length>0,E=he(Y);m("message_delta",{delta:{stop_reason:S?"tool_use":"end_turn"},usage:E?{input_tokens:E.input_tokens,output_tokens:E.output_tokens}:{output_tokens:x}}),m("message_stop",{}),t.end();return}let y="",w="",k=[],u=new z;for await(let c of o.track(f,d)){k.push(c),c.type==="response.output_text.delta"&&typeof c.delta=="string"&&(y+=c.delta),c.type==="response.reasoning_text.delta"&&typeof c.delta=="string"&&(w+=c.delta);let x=ae(c);if(x&&u.add(x),c.type==="error"){D(t,502,"api_error",JSON.stringify(c));return}if(t.destroyed)break}if(t.destroyed)return;let R=K(k),I;try{I=Ae(R),I.length===0&&u.size>0&&(I=Ae({output:u.items()}))}catch(c){D(t,502,"api_error",String(c instanceof Error?c.message:c));return}let $=ee(p,I.length,y);if($){D(t,502,"api_error",Z(d,$));return}let P=[],N=w||nt(R);N&&P.push({type:"thinking",thinking:N}),(y||I.length===0)&&P.push({type:"text",text:y}),P.push(...I),B(t,200,{id:i,type:"message",role:"assistant",content:P,model:s,stop_reason:I.length>0?"tool_use":"end_turn",usage:he(R)??{input_tokens:0,output_tokens:0}})}function Ht(e){let{clients:n,apiKey:t,identity:o,statusOf:r}=e,s=typeof n=="function"?n:()=>n,i=new WeakMap,l=d=>{let p=i.get(d);return p||(p=new Oe,i.set(d,p)),p},h=new Se;return(d,p)=>{let f=new URL(d.url??"/","http://localhost"),m=f.pathname==="/v1/messages"||f.pathname.startsWith("/v1/messages/"),y=(w,k,u,R)=>m?D(p,w,R,k):W(p,w,k,u);(async()=>{if(d.method==="GET"&&f.pathname==="/healthz"){B(p,200,{ok:!0});return}if(d.method==="GET"&&f.pathname==="/stats"){B(p,200,o?{identity:o,...h.snapshot()}:h.snapshot());return}if(t&&(d.headers.authorization??"")!==`Bearer ${t}`){y(401,"invalid local proxy api key","authentication_error","authentication_error");return}let w=await s(d);if(d.method==="GET"&&f.pathname==="/v1/models"){B(p,200,await w.listModels());return}if(d.method!=="POST"){y(404,`no route for ${d.method} ${f.pathname}`,"invalid_request_error","not_found_error");return}let k;try{k=await St(d)}catch(R){if(R instanceof me){y(413,R.message,"request_too_large","request_too_large");return}y(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let u=Buffer.byteLength(JSON.stringify(k));if(f.pathname==="/v1/responses"){h.request("responses",u,u),await Lt(k,w,l(w),p,h);return}if(f.pathname==="/v1/chat/completions"){h.request("chat_completions",u,u),await $t(k,w,p,h);return}if(f.pathname==="/v1/messages"){h.request("messages",u,u),await Bt(k,w,p,h);return}if(f.pathname==="/v1/messages/count_tokens"){h.request("count_tokens",u,u),D(p,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}y(404,`no route for POST ${f.pathname}`,"invalid_request_error","not_found_error")})().catch(w=>{let k=String(w instanceof Error?w.message:w);if(!p.headersSent){let u=r?.(w)??null;if(u){y(u.status,k,u.openaiType,u.anthropicType);return}y(500,k,"proxy_error","api_error");return}p.destroyed||(m?p.write(`event: error
|
|
37
|
+
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:k}})}
|
|
38
38
|
|
|
39
|
-
`):
|
|
39
|
+
`):p.write(`data: ${JSON.stringify({error:{message:k,type:"upstream_error"}})}
|
|
40
40
|
|
|
41
|
-
`)),
|
|
41
|
+
`)),p.end()})}}function ot(e){let{clients:n,apiKey:t}=e,o=(0,tt.createServer)(Ht(e));return Xe(o,{clients:n,apiKey:t}),o}0&&(module.exports={GEMINI_LIVE_PATH,VIDEO_OUT_SETUP_KEY,createOpenAIProxy});
|
package/dist/proxy/index.d.cts
CHANGED
|
@@ -1,32 +1,5 @@
|
|
|
1
|
-
export { O as OpenAIProxyOptions, P as ProxyClients, a as ProxyIdentity, c as createOpenAIProxy } from '../server-
|
|
1
|
+
export { O as OpenAIProxyOptions, P as ProxyClients, a as ProxyIdentity, b as ProxyVideoOutLane, c as createOpenAIProxy } from '../server-BfME37pQ.cjs';
|
|
2
|
+
export { h as GEMINI_LIVE_PATH, V as VIDEO_OUT_SETUP_KEY } from '../translator-CcDBEfvm.cjs';
|
|
2
3
|
import 'node:http';
|
|
3
|
-
import '../
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Gemini Live (BidiGenerateContent) ⇄ uRun Responses translation — PURE
|
|
7
|
-
* functions only, no I/O and no session state. The wire shapes mirror the
|
|
8
|
-
* official WebSocket reference (https://ai.google.dev/api/live): client
|
|
9
|
-
* messages are a union of exactly one of `setup` / `clientContent` /
|
|
10
|
-
* `realtimeInput` / `toolResponse`; server messages are a union of
|
|
11
|
-
* `setupComplete` / `serverContent` / `toolCall` / … with `usageMetadata` as
|
|
12
|
-
* a SIBLING of the union field on `BidiGenerateContentServerMessage`.
|
|
13
|
-
*
|
|
14
|
-
* Scope (this slice): setup + clientContent + realtimeInput TEXT + AUDIO +
|
|
15
|
-
* VIDEO + serverContent streaming + toolCall/toolResponse + sessionResumption
|
|
16
|
-
* (handles over the proxy's pooled-session identity seam) + goAway + MANUAL
|
|
17
|
-
* VAD/activity (setup.realtimeInputConfig.automaticActivityDetection.disabled
|
|
18
|
-
* + realtimeInput.activityStart/activityEnd + serverContent.interrupted
|
|
19
|
-
* barge-in). AUTOMATIC (server-side) activity detection is the DECLARED
|
|
20
|
-
* follow-up slice — requesting it fails loud naming the follow-up, never
|
|
21
|
-
* silently ignored (repo mandate: one canonical path, no silent fallbacks).
|
|
22
|
-
*
|
|
23
|
-
* AUTOMATIC activity detection is deliberately NOT emulated here: server-side
|
|
24
|
-
* VAD is a PLATFORM primitive (urun-python `urun.serve.turn_detection`
|
|
25
|
-
* `detect_turns` — `turn_start`/`turn_end` events on the session's §5 stream
|
|
26
|
-
* lane), never adapter-local DSP in the proxy. Until the Live lane's session
|
|
27
|
-
* shapes emit those events, requesting automatic detection fails loud.
|
|
28
|
-
*/
|
|
29
|
-
/** The Live WS endpoint path (v1beta), verbatim from the Google reference. */
|
|
30
|
-
declare const GEMINI_LIVE_PATH = "/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent";
|
|
31
|
-
|
|
32
|
-
export { GEMINI_LIVE_PATH };
|
|
4
|
+
import '../video-out-D20UuJ8G.cjs';
|
|
5
|
+
import '../types-lsVTbNcH.cjs';
|
package/dist/proxy/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
export { O as OpenAIProxyOptions, P as ProxyClients, a as ProxyIdentity, c as createOpenAIProxy } from '../server-
|
|
1
|
+
export { O as OpenAIProxyOptions, P as ProxyClients, a as ProxyIdentity, b as ProxyVideoOutLane, c as createOpenAIProxy } from '../server-l1himPxc.js';
|
|
2
|
+
export { h as GEMINI_LIVE_PATH, V as VIDEO_OUT_SETUP_KEY } from '../translator-C9uPKypK.js';
|
|
2
3
|
import 'node:http';
|
|
3
|
-
import '../
|
|
4
|
-
|
|
5
|
-
declare const GEMINI_LIVE_PATH = "/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent";
|
|
6
|
-
|
|
7
|
-
export { GEMINI_LIVE_PATH };
|
|
4
|
+
import '../video-out-CWesbk12.js';
|
|
5
|
+
import '../types-lsVTbNcH.js';
|
package/dist/proxy/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{b as t}from"../chunk-M6ICU4F5.js";import"../chunk-XHIIEA6Z.js";import{a as r,h as o}from"../chunk-5NXM4IO3.js";import"../chunk-CWJRDBDC.js";import{c as e}from"../chunk-YSFSRI3D.js";e();export{r as GEMINI_LIVE_PATH,o as VIDEO_OUT_SETUP_KEY,t as createOpenAIProxy};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IncomingMessage, Server } from 'node:http';
|
|
2
|
-
import { a as AudioBridge,
|
|
2
|
+
import { a as AudioBridge, h as VideoFrameLane, j as VideoOutLane } from './video-out-D20UuJ8G.cjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* WHAT a proxy serves — the `identity` block on `GET /stats`. Reuse of a
|
|
@@ -100,6 +100,18 @@ interface ProxyClients {
|
|
|
100
100
|
* frames.
|
|
101
101
|
*/
|
|
102
102
|
openVideo?(model: string | undefined): Promise<ProxyVideoLane>;
|
|
103
|
+
/**
|
|
104
|
+
* Open (or reuse) the NATIVE video OUTPUT lane on the pooled session
|
|
105
|
+
* `model` routes to (transport/video-out.ts `openSessionVideoOutLane`):
|
|
106
|
+
* decoded spec-v1 records consumed from the session's named §5
|
|
107
|
+
* `rt-video-out` downstream. The Live adapter fans each record onto the
|
|
108
|
+
* SAME Bidi WS as an extension server message (spec v2, WS-inline) — no
|
|
109
|
+
* side-channel transport. Optional at the seam; an ABSENT seam means the
|
|
110
|
+
* `urun.videoOut` capability is refused by omission in setupComplete
|
|
111
|
+
* (vanilla parity), while a wired seam that cannot open the lane must
|
|
112
|
+
* throw LOUD — never a quiet downgrade.
|
|
113
|
+
*/
|
|
114
|
+
openVideoOut?(model: string | undefined): Promise<ProxyVideoOutLane>;
|
|
103
115
|
/**
|
|
104
116
|
* SESSION-IDENTITY SEAM (ModelRouter.handleFor): the opaque stable handle
|
|
105
117
|
* for the pooled session currently serving `model`'s turns. Derived from
|
|
@@ -135,6 +147,12 @@ type ProxyAudioLane = Pick<AudioBridge, 'appendInputAudio' | 'onOutputAudio'>;
|
|
|
135
147
|
* (Live-style protocols have no video OUT modality).
|
|
136
148
|
*/
|
|
137
149
|
type ProxyVideoLane = Pick<VideoFrameLane, 'sendInputFrame'>;
|
|
150
|
+
/**
|
|
151
|
+
* The video-OUT lane handle `openVideoOut` returns — structurally the
|
|
152
|
+
* transport VideoOutLane (video-out.ts): the codec plus frame/error fan-out
|
|
153
|
+
* the WS-inline delivery subscribes.
|
|
154
|
+
*/
|
|
155
|
+
type ProxyVideoOutLane = Pick<VideoOutLane, 'codec' | 'onOutputFrame' | 'onLaneError'>;
|
|
138
156
|
/**
|
|
139
157
|
* Per-request `ProxyClients` resolution — the seam the HOSTED multi-tenant
|
|
140
158
|
* server (`src/hosted/`) plugs into so ONE handler implementation serves
|
|
@@ -179,4 +197,4 @@ interface OpenAIProxyOptions extends ProxyHandlerOptions {
|
|
|
179
197
|
/** Build (not listen) the proxy server — the caller owns listen/close. */
|
|
180
198
|
declare function createOpenAIProxy(options: OpenAIProxyOptions): Server;
|
|
181
199
|
|
|
182
|
-
export { type OpenAIProxyOptions as O, type ProxyClients as P, type ProxyIdentity as a, createOpenAIProxy as c };
|
|
200
|
+
export { type OpenAIProxyOptions as O, type ProxyClients as P, type ProxyIdentity as a, type ProxyVideoOutLane as b, createOpenAIProxy as c };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IncomingMessage, Server } from 'node:http';
|
|
2
|
-
import { a as AudioBridge,
|
|
2
|
+
import { a as AudioBridge, h as VideoFrameLane, j as VideoOutLane } from './video-out-CWesbk12.js';
|
|
3
3
|
|
|
4
4
|
interface ProxyIdentity {
|
|
5
5
|
app: string;
|
|
@@ -42,6 +42,8 @@ interface ProxyClients {
|
|
|
42
42
|
|
|
43
43
|
openVideo?(model: string | undefined): Promise<ProxyVideoLane>;
|
|
44
44
|
|
|
45
|
+
openVideoOut?(model: string | undefined): Promise<ProxyVideoOutLane>;
|
|
46
|
+
|
|
45
47
|
sessionHandle(model: string | undefined): Promise<string>;
|
|
46
48
|
|
|
47
49
|
createResponseOn(handle: string, params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
@@ -53,6 +55,8 @@ type ProxyAudioLane = Pick<AudioBridge, 'appendInputAudio' | 'onOutputAudio'>;
|
|
|
53
55
|
|
|
54
56
|
type ProxyVideoLane = Pick<VideoFrameLane, 'sendInputFrame'>;
|
|
55
57
|
|
|
58
|
+
type ProxyVideoOutLane = Pick<VideoOutLane, 'codec' | 'onOutputFrame' | 'onLaneError'>;
|
|
59
|
+
|
|
56
60
|
type ProxyClientsFor = (req: IncomingMessage) => ProxyClients | Promise<ProxyClients>;
|
|
57
61
|
interface ProxyHandlerOptions {
|
|
58
62
|
|
|
@@ -75,4 +79,4 @@ interface OpenAIProxyOptions extends ProxyHandlerOptions {
|
|
|
75
79
|
|
|
76
80
|
declare function createOpenAIProxy(options: OpenAIProxyOptions): Server;
|
|
77
81
|
|
|
78
|
-
export { type OpenAIProxyOptions as O, type ProxyClients as P, type ProxyIdentity as a, createOpenAIProxy as c };
|
|
82
|
+
export { type OpenAIProxyOptions as O, type ProxyClients as P, type ProxyIdentity as a, type ProxyVideoOutLane as b, createOpenAIProxy as c };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { k as VideoOutRecord } from './video-out-CWesbk12.js';
|
|
2
|
+
|
|
3
|
+
declare const GEMINI_LIVE_PATH = "/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent";
|
|
4
|
+
|
|
5
|
+
declare const CLOSE_PROTOCOL = 1007;
|
|
6
|
+
|
|
7
|
+
declare const CLOSE_UNKNOWN_MODEL = 1008;
|
|
8
|
+
|
|
9
|
+
declare const CLOSE_UPSTREAM = 1011;
|
|
10
|
+
|
|
11
|
+
declare const CLOSE_SESSION_GONE = 1008;
|
|
12
|
+
|
|
13
|
+
declare class GeminiLiveError extends Error {
|
|
14
|
+
readonly closeCode: number;
|
|
15
|
+
constructor(message: string, closeCode?: number);
|
|
16
|
+
}
|
|
17
|
+
type Rec = Record<string, unknown>;
|
|
18
|
+
|
|
19
|
+
type ClientMessage = {
|
|
20
|
+
kind: 'setup';
|
|
21
|
+
setup: Rec;
|
|
22
|
+
} | {
|
|
23
|
+
kind: 'clientContent';
|
|
24
|
+
clientContent: Rec;
|
|
25
|
+
} | {
|
|
26
|
+
kind: 'realtimeInput';
|
|
27
|
+
realtimeInput: Rec;
|
|
28
|
+
} | {
|
|
29
|
+
kind: 'toolResponse';
|
|
30
|
+
toolResponse: Rec;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
declare function parseClientMessage(raw: string): ClientMessage;
|
|
34
|
+
|
|
35
|
+
type ActivityHandling = 'START_OF_ACTIVITY_INTERRUPTS' | 'NO_INTERRUPTION';
|
|
36
|
+
|
|
37
|
+
interface ActivityConfig {
|
|
38
|
+
|
|
39
|
+
manualActivity: boolean;
|
|
40
|
+
|
|
41
|
+
activityHandling: ActivityHandling;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface LiveSessionConfig {
|
|
45
|
+
|
|
46
|
+
model: string;
|
|
47
|
+
|
|
48
|
+
systemItems: Rec[];
|
|
49
|
+
|
|
50
|
+
tools: Rec[] | undefined;
|
|
51
|
+
temperature: number | undefined;
|
|
52
|
+
maxOutputTokens: number | undefined;
|
|
53
|
+
|
|
54
|
+
audioModality: boolean;
|
|
55
|
+
|
|
56
|
+
activity: ActivityConfig;
|
|
57
|
+
|
|
58
|
+
sessionResumption: {
|
|
59
|
+
enabled: boolean;
|
|
60
|
+
handle: string | null;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
videoOut: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
declare const VIDEO_OUT_SETUP_KEY = "urun.videoOut";
|
|
67
|
+
|
|
68
|
+
declare function videoOutAck(codec: string): Rec;
|
|
69
|
+
|
|
70
|
+
declare function videoOutChunkMsg(record: VideoOutRecord): Rec;
|
|
71
|
+
|
|
72
|
+
declare function translateSetup(setup: Rec): LiveSessionConfig;
|
|
73
|
+
|
|
74
|
+
declare function sessionResumptionUpdateMsg(newHandle: string): Rec;
|
|
75
|
+
|
|
76
|
+
declare function goAwayMsg(timeLeftMs: number | null): Rec;
|
|
77
|
+
|
|
78
|
+
declare function translateClientContent(cc: Rec): {
|
|
79
|
+
items: Rec[];
|
|
80
|
+
turnComplete: boolean;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
declare const GEMINI_AUDIO_IN_RATE = 16000;
|
|
84
|
+
|
|
85
|
+
declare const GEMINI_AUDIO_OUT_RATE = 24000;
|
|
86
|
+
|
|
87
|
+
declare const GEMINI_AUDIO_OUT_MIME = "audio/pcm;rate=24000";
|
|
88
|
+
|
|
89
|
+
declare const OUTBOUND_AUDIO_FLUSH_B64: string;
|
|
90
|
+
|
|
91
|
+
declare function resamplePcm16(samples: Int16Array, from: number, to: number): Int16Array;
|
|
92
|
+
|
|
93
|
+
type RealtimeInputAction = {
|
|
94
|
+
kind: 'text';
|
|
95
|
+
text: string;
|
|
96
|
+
} | {
|
|
97
|
+
kind: 'audio';
|
|
98
|
+
base64Pcm24k: string;
|
|
99
|
+
} | {
|
|
100
|
+
kind: 'audioStreamEnd';
|
|
101
|
+
} | {
|
|
102
|
+
kind: 'activityStart';
|
|
103
|
+
} | {
|
|
104
|
+
kind: 'activityEnd';
|
|
105
|
+
} | {
|
|
106
|
+
kind: 'video';
|
|
107
|
+
jpegFrame: Uint8Array;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
declare function translateRealtimeInput(ri: Rec): RealtimeInputAction;
|
|
111
|
+
|
|
112
|
+
declare function translateToolResponse(tr: Rec): Rec[];
|
|
113
|
+
|
|
114
|
+
declare function serverContentOfDelta(ev: {
|
|
115
|
+
type?: unknown;
|
|
116
|
+
delta?: unknown;
|
|
117
|
+
}): Rec | null;
|
|
118
|
+
|
|
119
|
+
declare function serverContentOfAudio(base64Pcm24k: string): Rec;
|
|
120
|
+
|
|
121
|
+
interface TurnEnd {
|
|
122
|
+
|
|
123
|
+
messages: Rec[];
|
|
124
|
+
|
|
125
|
+
assistantItems: Rec[];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
declare function turnEndOfCompleted(completed: unknown): TurnEnd;
|
|
129
|
+
|
|
130
|
+
export { type ActivityConfig as A, CLOSE_PROTOCOL as C, GEMINI_AUDIO_IN_RATE as G, type LiveSessionConfig as L, OUTBOUND_AUDIO_FLUSH_B64 as O, type RealtimeInputAction as R, type TurnEnd as T, VIDEO_OUT_SETUP_KEY as V, type ActivityHandling as a, CLOSE_SESSION_GONE as b, CLOSE_UNKNOWN_MODEL as c, CLOSE_UPSTREAM as d, type ClientMessage as e, GEMINI_AUDIO_OUT_MIME as f, GEMINI_AUDIO_OUT_RATE as g, GEMINI_LIVE_PATH as h, GeminiLiveError as i, goAwayMsg as j, serverContentOfDelta as k, sessionResumptionUpdateMsg as l, translateRealtimeInput as m, translateSetup as n, translateToolResponse as o, parseClientMessage as p, turnEndOfCompleted as q, resamplePcm16 as r, serverContentOfAudio as s, translateClientContent as t, videoOutChunkMsg as u, videoOutAck as v };
|