@urun-sh/openai 0.2.60 → 0.3.1
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-54VWZJU7.js +6 -0
- package/dist/chunk-5NXM4IO3.js +2 -0
- package/dist/chunk-CWJRDBDC.js +1 -0
- package/dist/chunk-F2TEK34X.js +1 -0
- package/dist/chunk-G3NMGP4N.js +1 -0
- package/dist/chunk-M6ICU4F5.js +40 -0
- package/dist/chunk-UVAY7Q7Z.js +1 -0
- package/dist/chunk-XHIIEA6Z.js +1 -0
- package/dist/chunk-YSFSRI3D.js +1 -0
- package/dist/chunk-ZW6ENXZY.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 +56 -0
- package/dist/hosted/bin.js +23 -0
- package/dist/hosted/index.cjs +34 -0
- package/dist/hosted/index.d.cts +621 -0
- package/dist/hosted/index.d.ts +226 -0
- package/dist/hosted/index.js +1 -0
- 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.cjs +4 -4
- package/dist/pi-extension/index.d.cts +2 -1
- package/dist/pi-extension/index.d.ts +2 -1
- package/dist/pi-extension/index.js +7 -7
- package/dist/pi-extension/standalone.cjs +54 -54
- package/dist/proxy/cli.cjs +39 -39
- package/dist/proxy/cli.js +14 -19
- package/dist/proxy/index.cjs +22 -22
- package/dist/proxy/index.d.cts +5 -182
- package/dist/proxy/index.d.ts +5 -67
- package/dist/proxy/index.js +1 -1
- package/dist/server-BfME37pQ.d.cts +200 -0
- package/dist/server-l1himPxc.d.ts +82 -0
- 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 +22 -2
- package/dist/chunk-4FUBGLFN.js +0 -41
- package/dist/chunk-OLE2YJO3.js +0 -1
- package/dist/chunk-SSZL77P5.js +0 -1
- package/dist/chunk-VFHQZ4OM.js +0 -1
- package/dist/chunk-VXTNG2TP.js +0 -1
- package/dist/media-DCHTX3Ez.d.cts +0 -68
- package/dist/media-DCHTX3Ez.d.ts +0 -42
package/dist/proxy/cli.js
CHANGED
|
@@ -1,20 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{a as j}from"../chunk-
|
|
3
|
-
`)
|
|
4
|
-
`)}
|
|
5
|
-
`)}
|
|
6
|
-
`)
|
|
7
|
-
`)
|
|
8
|
-
`)
|
|
9
|
-
`)
|
|
10
|
-
`)
|
|
11
|
-
`)
|
|
12
|
-
`),
|
|
13
|
-
`),
|
|
14
|
-
`)
|
|
15
|
-
`)
|
|
16
|
-
`)}),l.stdout.write(`urun openai proxy on http://127.0.0.1:${c}/v1 \u2014 proxy logs: ${f}
|
|
17
|
-
`),d=ne(y)),s==="codex"&&(o=Y(c,h(l.env).appId,o));let R=fe(r,o,{stdio:"inherit",env:H(l.env,c,i)}),he=f!==null?oe({logPath:f,appendSync:ue,restore:()=>{d?.(),d=null},writeStderr:p=>l.stderr.write(p),killChild:()=>R.kill("SIGTERM"),exit:p=>l.exit(p),proc:l}):null,$=await new Promise(p=>{R.once("exit",P=>p(P??1)),R.once("error",P=>{d?.(),d=null,E(l.stderr,`failed to launch ${r}: ${String(P)}
|
|
18
|
-
`),p(127)})});he?.();let m=null;try{u&&await u.close()}catch(p){m=p}finally{d?.(),d=null,b?.(),b=null,y?.end()}return m!==null&&E(l.stderr,`urun compat: proxy teardown failed after the harness exited (harness exit code ${$} preserved): ${String(m instanceof Error?m.message:m)}
|
|
19
|
-
`),$}}Je().then(e=>l.exit(e),e=>{l.stderr.write(`${String(e instanceof Error?e.message:e)}
|
|
20
|
-
`),l.exit(1)});export{Ke as buildClients,Be as buildRouter};
|
|
2
|
+
import{a as k,b as $,c as h,d as C,e as U,f as N,g as O,h as L,i as G,j as E,k as I,l as y,n as _,o as P,p as W,q as j,r as B,s as D,t as v,u as K,v as M,w as F}from"../chunk-54VWZJU7.js";import"../chunk-F2TEK34X.js";import{b as T}from"../chunk-M6ICU4F5.js";import"../chunk-G3NMGP4N.js";import"../chunk-XHIIEA6Z.js";import"../chunk-5NXM4IO3.js";import{g as R}from"../chunk-CWJRDBDC.js";import"../chunk-UVAY7Q7Z.js";import{c as g}from"../chunk-YSFSRI3D.js";g();import{spawn as V}from"child_process";import{appendFileSync as Y,createWriteStream as re,existsSync as ne,mkdirSync as oe}from"fs";import ae from"path";import t from"process";g();import{ensureNodeWebRTC as Z,nodeWebRTCUnavailableReason as ee}from"@urun-sh/core/internal";var te={ensureWebRTC:Z,webrtcUnavailableReason:ee,audioBackend:R};async function J(e=te){try{await e.ensureWebRTC()}catch(r){return{ok:!1,reason:`the WebRTC bootstrap threw: ${String(r)}`}}let i=e.webrtcUnavailableReason();if(i)return{ok:!1,reason:`${i} \u2014 this artifact would join every session doc/KV-only and every token stream would error. Either "werift" is missing from the deployment directory (npm lane), or it was marked --external in a single-file build, where nothing can ever resolve it (bunfs: "Cannot find package 'werift' from '/$bunfs/root/\u2026'").`};try{(await e.audioBackend()).createSource()}catch(r){return{ok:!1,reason:`the werift/opus audio backend could not be constructed: ${String(r)}`}}return{ok:!0,reason:null}}function ie(e,i){let r=(e.URUN_CATALOG_URL??"").trim(),l=r?_(r,"URUN_CATALOG_URL"):"",n=(e.URUN_CATALOG_ANON_KEY??"").trim();return!l||!n?(i(`urun compat: catalog oracle off (URUN_CATALOG_URL/URUN_CATALOG_ANON_KEY unset) \u2014 an undeployed uRun CATALOG model name falls through to the default app instead of a loud 404; deployed apps still resolve exactly, and /stats.models records every mapping
|
|
3
|
+
`),null):{catalogUrl:l,anonKey:n}}function se(e){return k({baseUrl:e.baseUrl,orgId:e.orgId,defaultApp:e.appId,fnName:e.fnName,apiUrl:e.apiUrl,auth:e.auth,catalog:ie(t.env,i=>t.stderr.write(i))})}async function le(e,i,r){let l=V(e,i,{stdio:"inherit",env:r});return new Promise(n=>{l.once("exit",o=>n(o??1)),l.once("error",o=>{t.stderr.write(`failed to launch ${e}: ${String(o)}
|
|
4
|
+
`),n(127)})})}function ue(){return L(t.env,import.meta.url,ne)}function q(e){for(let[i,r]of Object.entries(C(e)))t.stdout.write(`export ${i}=${r}
|
|
5
|
+
`)}async function H(e,i){let r=y(t.env),l=i?P(r):void 0,n=se(r),o=T({clients:$(n),identity:l});try{await new Promise((s,d)=>{o.once("error",d),o.listen(e,"127.0.0.1",s)})}catch(s){throw await n.closeAll().catch(()=>{}),s.code==="EADDRINUSE"?new Error(F(e,await W(e))):s}let u=o.address().port;return i&&(t.stdout.write(`urun openai proxy listening on http://127.0.0.1:${u}/v1
|
|
6
|
+
`),q(u)),{port:u,close:async()=>{let s=new Promise(d=>o.close(()=>d()));o.closeAllConnections(),await s,await n.closeAll()}}}async function ce(){let e=t.argv.slice(2),i=G(e),r=e.shift()??"proxy";if(r==="env")return q(I(i??E(e))),0;if(r==="selftest"){let{ok:n,reason:o}=await J();return n?(t.stdout.write(`urun-openai selftest: OK \u2014 werift WebRTC peer globals installed and the werift/opus audio backend constructed from this artifact
|
|
7
|
+
`),0):(t.stderr.write(`urun-openai selftest: FAIL \u2014 ${o}
|
|
8
|
+
`),1)}if(r==="proxy"){let n=i??E(e),o=await H(n,!0);return await new Promise(u=>{t.once("SIGINT",u),t.once("SIGTERM",u)}),await o.close(),0}let l=r==="run"?e.shift():r;{if(!l)throw new Error(`launch needs an agent (${Object.keys(h).join("|")}) or \`-- <cmd>\``);if(l==="pi"){let a=y(t.env);return le("pi",["-e",ue(),...O(a.appId,e)],t.env)}let n,o,u="openai";if(l==="--"){if(n=e.shift()??"",o=e,!n)throw new Error("`-- <cmd>` needs a command to launch")}else{let a=h[l];if(!a)throw new Error(`unknown command/agent ${l}; commands: proxy | env; agents: ${Object.keys(h).join(" | ")} \u2014 or \`-- <cmd>\``);n=a.bin,o=[...a.args??[],...e],u=a.env}let s=await j(i,P(y(t.env))),d=null,f=s.port;s.mode==="reuse"?t.stdout.write(`reusing the running urun openai proxy on http://127.0.0.1:${f} (app ${s.identity.app}, fn ${s.identity.fn}, v${s.identity.proxy_version})
|
|
9
|
+
`):s.reason&&t.stderr.write(`urun compat: ${s.reason}
|
|
10
|
+
`);let c=null,b=null,w=null,p=null;s.mode!=="reuse"&&(d=await H(s.port,!1),f=d.port,p=B(f),oe(ae.dirname(p),{recursive:!0}),w=re(p,{flags:"a"}),b=K({streams:t,note:a=>Y(p,a)}).uninstall,w.on("error",a=>{c?.(),c=null,v(t.stderr,`urun compat: proxy log ${p} failed: ${String(a)} \u2014 proxy output continues on the terminal
|
|
11
|
+
`)}),t.stdout.write(`urun openai proxy on http://127.0.0.1:${f}/v1 \u2014 proxy logs: ${p}
|
|
12
|
+
`),c=D(w)),l==="codex"&&(o=N(f,y(t.env).appId,o));let x=V(n,o,{stdio:"inherit",env:U(t.env,f,u)}),z=p!==null?M({logPath:p,appendSync:Y,restore:()=>{c?.(),c=null},writeStderr:a=>t.stderr.write(a),killChild:()=>x.kill("SIGTERM"),exit:a=>t.exit(a),proc:t}):null,A=await new Promise(a=>{x.once("exit",S=>a(S??1)),x.once("error",S=>{c?.(),c=null,v(t.stderr,`failed to launch ${n}: ${String(S)}
|
|
13
|
+
`),a(127)})});z?.();let m=null;try{d&&await d.close()}catch(a){m=a}finally{c?.(),c=null,b?.(),b=null,w?.end()}return m!==null&&v(t.stderr,`urun compat: proxy teardown failed after the harness exited (harness exit code ${A} preserved): ${String(m instanceof Error?m.message:m)}
|
|
14
|
+
`),A}}ce().then(e=>t.exit(e),e=>{t.stderr.write(`${String(e instanceof Error?e.message:e)}
|
|
15
|
+
`),t.exit(1)});
|
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=x(t);if(!n)throw new i("setup.realtimeInputConfig must be an object");for(let s of Object.keys(n))if(!(s==="automaticActivityDetection"||s==="activityHandling"))throw new i(s==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${s}"`);let e=x(n.automaticActivityDetection);if(n.automaticActivityDetection!==void 0&&!e)throw new i("realtimeInputConfig.automaticActivityDetection must be an object");for(let s of Object.keys(e??{}))if(s!=="disabled")throw new i(`automaticActivityDetection.${s} tunes AUTOMATIC (server-side) activity detection \u2014 the declared platform follow-up (serve turn_detection events over the session stream lane); only "disabled: true" (manual activity mode) is supported in this slice`);if(e?.disabled!==!0)throw new i("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let o="START_OF_ACTIVITY_INTERRUPTS";if(n.activityHandling!==void 0){if(!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[c,g]of Object.entries(ut))if(t[c]!==void 0)throw new i(g);for(let c of Object.keys(t))if(!lt.includes(c))throw new i(`unsupported setup field "${c}"`);if(typeof t.model!="string"||t.model.length===0)throw new i('setup.model is required (format "models/{model}")');let n=t.model.replace(/^models\//,""),e,o,s=!1,r=x(t.generationConfig);if(t.generationConfig!==void 0&&!r)throw new i("setup.generationConfig must be an object");if(r){for(let c of Object.keys(r))if(!dt.includes(c))throw new i(`unsupported generationConfig field "${c}"`);if(r.temperature!==void 0){if(typeof r.temperature!="number")throw new i("generationConfig.temperature must be a number");e=r.temperature}if(r.maxOutputTokens!==void 0){if(typeof r.maxOutputTokens!="number")throw new i("generationConfig.maxOutputTokens must be a number");o=r.maxOutputTokens}if(r.responseModalities!==void 0){let c=Array.isArray(r.responseModalities)?r.responseModalities:[r.responseModalities];for(let g of c){let p=String(g).toUpperCase();if(p==="AUDIO"){s=!0;continue}if(p!=="TEXT")throw new i(`responseModalities ${JSON.stringify(g)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge rt-audio-out lane)`)}}}let a=[];t.systemInstruction!==void 0&&a.push({role:"system",content:Pe(t.systemInstruction,"setup.systemInstruction")});let u;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new i("setup.tools must be an array");u=[];for(let c of t.tools){let g=x(c);if(!g)throw new i("setup.tools entries must be objects");for(let p of Object.keys(g))if(p!=="functionDeclarations")throw new i(`unsupported tool "${p}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(g.functionDeclarations))throw new i("tool.functionDeclarations must be an array");for(let p of g.functionDeclarations){let h=x(p);if(!h||typeof h.name!="string")throw new i("functionDeclarations entries require a string name");u.push({type:"function",name:h.name,description:h.description,parameters:h.parameters})}}u.length===0&&(u=void 0)}let f={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let c=x(t.sessionResumption);if(!c)throw new i("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let g of Object.keys(c))if(g!=="handle")throw new i(`unsupported sessionResumption field "${g}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(c.handle!==void 0&&(typeof c.handle!="string"||c.handle.length===0))throw new i("sessionResumption.handle must be a non-empty string when present");f={enabled:!0,handle:typeof c.handle=="string"?c.handle:null}}let d=t.realtimeInputConfig===void 0?at:ct(t.realtimeInputConfig);return{model:n,systemItems:a,tools:u,temperature:e,maxOutputTokens:o,audioModality:s,activity:d,sessionResumption:f}}function Ne(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function Le(t){let n=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(n*1e3)/1e3}s`}}}function Me(t){for(let o of Object.keys(t))if(o!=="turns"&&o!=="turnComplete")throw new i(`unsupported clientContent field "${o}"`);let n=t.turns===void 0?[]:t.turns;if(!Array.isArray(n))throw new i("clientContent.turns must be an array");let e=[];for(let o of n){let s=x(o);if(!s)throw new i("clientContent.turns entries must be Content objects");let r=s.role===void 0?"user":String(s.role).toLowerCase();if(r!=="user"&&r!=="model")throw new i(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(s.role)})`);e.push({role:r==="model"?"assistant":"user",content: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 u=0;u<o;u++){let f=u*r,d=Math.floor(f);if(d>=a){s[u]=t[a];continue}let c=f-d;s[u]=Math.round(t[d]+(t[d+1]-t[d])*c)}return s}function mt(t){let n=x(t);if(!n)throw new i("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof n.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(n.mimeType))throw new i(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(n.mimeType)}`);if(typeof n.data!="string"||n.data.length===0)throw new i("realtimeInput.video.data must be non-empty base64");let e=Buffer.from(n.data,"base64");if(e.length<3||e[0]!==255||e[1]!==216)throw new i("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(e)}function gt(t){let n=x(t);if(!n)throw new i("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof n.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(n.mimeType))throw new i(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(n.mimeType)}`);let e=/;\s*rate=(\d+)/.exec(n.mimeType);if(e&&Number(e[1])!==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=x(t[s]);if(!r||Object.keys(r).length!==0)throw new i(`realtimeInput.${s} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:s}}let n=["text","audio","audioStreamEnd","video"];for(let o of Object.keys(t))if(!n.includes(o))throw new i(`unsupported realtimeInput field "${o}"`);let e=n.filter(o=>t[o]!==void 0);if(e.length!==1)throw new i(`realtimeInput must carry exactly one of ${n.join(", ")} (got ${e.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k: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=x(n);if(!e||typeof e.id!="string"||e.id.length===0)throw new i("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:e.id,output:JSON.stringify(e.response??null)}})}function 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=x(t)??{},e=Array.isArray(n.output)?n.output:[],o=[],s=[],r=typeof n.output_text=="string"?n.output_text:"";if(!r)for(let d of e){let c=x(d);if(!(c?.type!=="message"||!Array.isArray(c.content)))for(let g of c.content){let p=x(g);p?.type==="output_text"&&typeof p.text=="string"&&(r+=p.text)}}r&&s.push({role:"assistant",content:r});for(let d of e){let c=x(d);if(c?.type!=="function_call")continue;if(typeof c.call_id!="string"||typeof c.name!="string")throw new i(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,J);let g=typeof c.arguments=="string"&&c.arguments.length>0?c.arguments:"{}",p;try{p=JSON.parse(g)}catch{throw new i(`upstream function_call arguments are not valid JSON: ${g}`,J)}o.push({id:c.call_id,name:c.name,args:p}),s.push({type:"function_call",call_id:c.call_id,name:c.name,arguments:g})}let a=[];o.length>0&&a.push({toolCall:{functionCalls:o}}),a.push({serverContent:{generationComplete:!0}});let u=x(n.usage),f={serverContent:{turnComplete:!0}};if(u){let d={};typeof u.input_tokens=="number"&&(d.promptTokenCount=u.input_tokens),typeof u.output_tokens=="number"&&(d.responseTokenCount=u.output_tokens),typeof u.total_tokens=="number"&&(d.totalTokenCount=u.total_tokens),Object.keys(d).length>0&&(f.usageMetadata=d)}return a.push(f),{messages:a,assistantItems:s}}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}=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(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(Le(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 f of o){if(e.interrupted)break;let d=f;if(d.type==="error")throw new i(`upstream error: ${JSON.stringify(d)}`,J);if(d.type==="response.completed"){s=d.response,r=!0;continue}let c=je(d);c&&this.send(c)}if(e.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!r)throw new i("upstream produced no response.completed event",J);let{messages:a,assistantItems:u}=Fe(s);this.transcript.push(...u);for(let f of a)this.send(f);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 u=new URL(s.url??"/","http://localhost");if(u.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
|
|
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
|
|
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(
|
|
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(
|
|
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
|
-
`),
|
|
34
|
-
data: ${JSON.stringify({type:
|
|
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
|
+
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});
|