@urun-sh/openai 0.5.5 → 0.6.0

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.
Files changed (56) hide show
  1. package/dist/{ResponsesClient-BYx3YLGo.d.ts → ResponsesClient-CSSrOYD8.d.ts} +7 -1
  2. package/dist/{ResponsesClient-Dft3bg3b.d.cts → ResponsesClient-_OZERUjH.d.cts} +11 -1
  3. package/dist/chunk-3JWYIKHM.js +2 -0
  4. package/dist/chunk-45FJ5GTP.js +7 -0
  5. package/dist/chunk-6M5JK4YC.js +1 -0
  6. package/dist/chunk-QTVTCMJU.js +1 -0
  7. package/dist/chunk-TDRZZGUK.js +1 -0
  8. package/dist/chunk-TPH77ZOW.js +58 -0
  9. package/dist/chunk-WCMX5VOF.js +4 -0
  10. package/dist/chunk-YXK42SKC.js +1 -0
  11. package/dist/gemini-live.cjs +2 -2
  12. package/dist/gemini-live.d.cts +79 -7
  13. package/dist/gemini-live.d.ts +32 -7
  14. package/dist/gemini-live.js +1 -1
  15. package/dist/hosted/bin.cjs +43 -27
  16. package/dist/hosted/bin.js +4 -4
  17. package/dist/hosted/index.cjs +41 -26
  18. package/dist/hosted/index.d.cts +710 -17
  19. package/dist/hosted/index.d.ts +171 -6
  20. package/dist/hosted/index.js +1 -1
  21. package/dist/index.cjs +1 -1
  22. package/dist/index.d.cts +5 -5
  23. package/dist/index.d.ts +5 -5
  24. package/dist/index.js +1 -1
  25. package/dist/{models-NYMZrklp.d.cts → models-DUdx_Y6X.d.cts} +1 -1
  26. package/dist/pi-extension/index.cjs +7 -7
  27. package/dist/pi-extension/index.d.cts +2 -2
  28. package/dist/pi-extension/index.d.ts +2 -2
  29. package/dist/pi-extension/index.js +1 -1
  30. package/dist/pi-extension/standalone.cjs +53 -53
  31. package/dist/proxy/cli.cjs +55 -43
  32. package/dist/proxy/cli.js +14 -13
  33. package/dist/proxy/index.cjs +31 -28
  34. package/dist/proxy/index.d.cts +132 -18
  35. package/dist/proxy/index.d.ts +28 -11
  36. package/dist/proxy/index.js +1 -6
  37. package/dist/responses-turn-BfdBbey8.d.cts +2394 -0
  38. package/dist/responses-turn-CdhIre_a.d.ts +652 -0
  39. package/dist/{translator-CcDBEfvm.d.cts → translator-CO8W_hmJ.d.cts} +3 -3
  40. package/dist/{translator-C9uPKypK.d.ts → translator-Ddd65sXR.d.ts} +1 -1
  41. package/dist/{types-lsVTbNcH.d.cts → types-CHPtJx6d.d.cts} +13 -0
  42. package/dist/{types-lsVTbNcH.d.ts → types-CHPtJx6d.d.ts} +4 -0
  43. package/dist/{video-out-D20UuJ8G.d.cts → video-out-BsuLqlID.d.cts} +6 -6
  44. package/dist/{video-out-CWesbk12.d.ts → video-out-Cawtm7SF.d.ts} +1 -1
  45. package/package.json +14 -11
  46. package/dist/chunk-5BZCM3RS.js +0 -4
  47. package/dist/chunk-5NXM4IO3.js +0 -2
  48. package/dist/chunk-CWJRDBDC.js +0 -1
  49. package/dist/chunk-DMD5UENK.js +0 -1
  50. package/dist/chunk-GBBY3PCZ.js +0 -1
  51. package/dist/chunk-I2Q3B3OG.js +0 -6
  52. package/dist/chunk-OI2OY32M.js +0 -1
  53. package/dist/chunk-QVF7NF7G.js +0 -44
  54. package/dist/responses-turn-KOAoIqZ-.d.ts +0 -200
  55. package/dist/responses-turn-OrO4euEN.d.cts +0 -513
  56. /package/dist/{models-NYMZrklp.d.ts → models-DUdx_Y6X.d.ts} +0 -0
@@ -1,4 +1,4 @@
1
- import { b as UrunSessionLike } from './types-lsVTbNcH.cjs';
1
+ import { b as UrunSessionLike } from './types-CHPtJx6d.cjs';
2
2
 
3
3
  /**
4
4
  * The ONE native audio sample rate of the session transport: the SFU Opus leg
@@ -44,7 +44,7 @@ declare class AudioBridge {
44
44
  /**
45
45
  * Wire the runtime's downstream audio track into the output-frame fan-out.
46
46
  * `remoteTrack` is the REAL Opus `MediaStreamTrack` the SFU produces for this
47
- * session (`session.stream('rt-audio-out').track`). The backend attaches a
47
+ * session (`session.stream('audio').track`). The backend attaches a
48
48
  * real `RTCAudioSink` to it; each decoded PCM16 frame is re-emitted as base64
49
49
  * to every `onOutputAudio` handler (the OpenAI `response.audio.delta` source).
50
50
  *
@@ -61,9 +61,9 @@ declare class AudioBridge {
61
61
  * The ONE canonical session-audio wiring — every protocol surface that carries
62
62
  * audio over a uRun session (OpenAI Realtime `enableAudio`, the Gemini Live
63
63
  * proxy lane) rides THIS function; none re-implements the dance:
64
- *
65
- * 1. `bridge.startOutbound()` → attach the local track to `rt-audio-in`
66
- * 2. await the runtime's downstream track on `rt-audio-out` (consumed
64
+ * 1. `bridge.startOutbound()` → attach the local track to `audio` (the ONE
65
+ * canonical session-audio lane, {@link AUDIO_STREAM})
66
+ * 2. await the runtime's downstream track on `audio` (consumed
67
67
  * ASYNCHRONOUSLY after the WebRTC handshake — may not exist yet)
68
68
  * 3. `bridge.startInbound(track)` → decoded PCM16 fans out via `onOutputAudio`
69
69
  *
@@ -73,7 +73,7 @@ declare class AudioBridge {
73
73
  declare function enableSessionAudio(session: UrunSessionLike, backend: AudioBackend, timeoutMs?: number): Promise<AudioBridge>;
74
74
  /**
75
75
  * The named-DATA stream video frames ride browser→runtime (the video-frame twin
76
- * of the `rt-audio-in` media lane). Apps consume it as
76
+ * of the `audio` media lane, {@link AUDIO_STREAM}). Apps consume it as
77
77
  * `ctx.stream('rt-video-in', kind='data')` — each segment is one encoded JPEG
78
78
  * frame (raw bytes, no envelope beyond the §5 stream framing).
79
79
  */
@@ -1,4 +1,4 @@
1
- import { b as UrunSessionLike } from './types-lsVTbNcH.js';
1
+ import { b as UrunSessionLike } from './types-CHPtJx6d.js';
2
2
 
3
3
  declare const NATIVE_AUDIO_SAMPLE_RATE = 24000;
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urun-sh/openai",
3
- "version": "0.5.5",
3
+ "version": "0.6.0",
4
4
  "description": "OpenAI-compatible Realtime + Responses SDK over uRun session primitives (not websockets).",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -70,30 +70,33 @@
70
70
  "build:bin": "bun build --compile --external @roamhq/wrtc src/proxy/bin.ts --outfile dist/bin/urun-openai && node scripts/assert-bin-transport.mjs"
71
71
  },
72
72
  "peerDependencies": {
73
- "@urun-sh/core": "^0.5.5"
73
+ "@urun-sh/core": "^0.6.0"
74
74
  },
75
75
  "dependencies": {
76
+ "@prometheus-io/client": "^0.16.1",
76
77
  "openai": "4.104.0",
77
78
  "ws": "^8.18.0"
78
79
  },
79
80
  "optionalDependencies": {
80
81
  "@roamhq/wrtc": "^0.8.0",
81
- "werift": "^0.23.0",
82
- "opusscript": "^0.1.1"
82
+ "opusscript": "^0.1.1",
83
+ "werift": "^0.23.0"
83
84
  },
84
85
  "devDependencies": {
85
86
  "@earendil-works/pi-coding-agent": "0.84.1",
87
+ "@types/node": "^24.0.0",
88
+ "@types/ws": "^8.18.1",
89
+ "@urun-sh/core": "workspace:*",
90
+ "ajv": "^8.17.1",
91
+ "ajv-formats": "^3.0.1",
86
92
  "esbuild": "^0.28.1",
93
+ "happy-dom": "^20.9.0",
94
+ "opusscript": "^0.1.1",
87
95
  "reflect-metadata": "^0.2.2",
88
96
  "tsup": "^8.5.0",
89
- "vitest": "^4.1.11",
90
- "werift": "^0.23.0",
91
- "opusscript": "^0.1.1",
92
- "happy-dom": "^20.9.0",
93
97
  "typescript": "^5.3.0",
94
- "@types/node": "^24.0.0",
95
- "@types/ws": "^8.18.1",
96
- "@urun-sh/core": "workspace:*"
98
+ "vitest": "^4.1.11",
99
+ "werift": "^0.23.0"
97
100
  },
98
101
  "publishConfig": {
99
102
  "access": "public"
@@ -1,4 +0,0 @@
1
- import{a as P,b as x,n as H,o as v}from"./chunk-I2Q3B3OG.js";import{b as _,d as T,e as O,f as N}from"./chunk-QVF7NF7G.js";import{c as I}from"./chunk-DMD5UENK.js";import{a as S}from"./chunk-5NXM4IO3.js";import{c as h}from"./chunk-YSFSRI3D.js";h();import{createClientToken as F,ClientTokenError as z}from"@urun-sh/core";var p=class extends Error{constructor(e,n=401){super(e);this.status=n;this.name="ProxyAuthError"}status},g=class extends Error{constructor(t){super(t),this.name="ControlPlaneUnavailableError"}};function $(o){let t=(o??"").trim();if(t==="")throw new p("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let e=/^Bearer[ \t]+(\S.*)$/i.exec(t);if(!e)throw new p("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return e[1].trim()}var G=60,j=6e4,V=4096;function D(o){return o===401||o===403}var m=class{constructor(t){this.opts=t;this.mint=t.mint??F,this.now=t.now??Date.now}opts;cache=new Map;mint;now;async resolve(t){if(t==="")throw new p("empty Authorization bearer token");let e=this.cache.get(t);if(e&&this.now()-e.at<j)return{apiKey:t,orgId:e.orgId};let n;try{n=(await this.mint(t,{baseUrl:this.opts.gatewayUrl,expiresIn:G})).orgId}catch(r){throw r instanceof z&&D(r.status)?(this.cache.delete(t),new p("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new g(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${r instanceof Error?r.message:String(r)}`)}if(!n)throw new g("the control plane verified the API key but returned no org_id \u2014 refusing to route a request with no tenancy (control-plane contract violation)");if(this.cache.size>=V){let r=this.cache.keys().next();r.done||this.cache.delete(r.value)}return this.cache.set(t,{orgId:n,at:this.now()}),{apiKey:t,orgId:n}}clear(){this.cache.clear()}};h();var W=8080,A="serve",de="0.0.0.0";function J(o,t,e){let n=(o[t]??"").trim();if(!n)throw new Error(`${t} is not set \u2014 ${e}`);return n}function ue(o=process.env){let t=(o.PORT??"").trim(),e=t===""?W:Number(t);if(!Number.isInteger(e)||e<0||e>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(t)}`);let n=H(v(J(o,"URUN_BASE_URL","the hosted proxy needs the uRun session-gateway base it opens sessions and verifies API keys against (e.g. https://api.urun.sh)"),"URUN_BASE_URL")),r=(o.URUN_API_URL??"").trim(),i=r?v(r,"URUN_API_URL"):`${n.replace(/\/+$/,"")}/v1`,a=(o.URUN_CATALOG_URL??"").trim(),s=(o.URUN_CATALOG_ANON_KEY??"").trim();if(!!a!=!!s)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let u=a?{catalogUrl:v(a,"URUN_CATALOG_URL"),anonKey:s}:null;return{port:e,baseUrl:n,apiUrl:i,catalog:u}}h();import{createHash as X,randomUUID as Y}from"crypto";var q=512;function U(o){return`urun-inference-proxy-${X("sha256").update(o).digest("hex").slice(0,32)}`}var b=class{constructor(t){this.opts=t}opts;tenants=new Map;build(t){if(this.opts.build)return this.opts.build(t);let e=P({baseUrl:this.opts.baseUrl,orgId:t.orgId,defaultApp:null,fnName:A,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:t.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:U(t.apiKey),release:"detach"});return{router:e,clients:x(e)}}clientsFor(t){let e=this.tenants.get(t.apiKey);if(e)return this.tenants.delete(t.apiKey),this.tenants.set(t.apiKey,e),e.clients;for(;this.tenants.size>=q;){let r=this.tenants.entries().next();if(r.done)break;let[i,a]=r.value;this.tenants.delete(i),a.router.closeAll().catch(()=>{})}let n=this.build(t);return this.tenants.set(t.apiKey,n),n.clients}get size(){return this.tenants.size}async closeAll(){let t=[...this.tenants.values()].map(n=>n.router);this.tenants.clear();let e=[];if(await Promise.all(t.map(async n=>{try{await n.closeAll()}catch(r){e.push(String(r instanceof Error?r.message:r))}})),e.length>0)throw new Error(`failed to close ${e.length} tenant backhaul(s): ${e.join("; ")}`)}},M=512,Q=1024,w=class extends Error{constructor(){super(`conversation capacity reached (${M} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function Z(o,t,e,n){let r=P({baseUrl:o.baseUrl,orgId:t.orgId,defaultApp:null,fnName:A,apiUrl:o.apiUrl,auth:{lane:"api-key",apiKey:t.apiKey,gatewayUrl:o.baseUrl},catalog:o.catalog,subject:e.subject,release:"detach",sessionStartOptions:i=>{let a=n.get(i);return a===void 0?void 0:{sessionId:a}},onSessionOpen:(i,a)=>{n.set(i,a)}});return{clients:x(r),release:async()=>{await r.closeAll()}}}var C=class{constructor(t){this.opts=t}opts;byCaller=new Map;byHandle=new Map;build(t,e){let n={id:e,subject:`${U(t.apiKey)}:live:${e}`},r=new Map,i=this.opts.build?this.opts.build(t,n):Z(this.opts,t,n,r);return{id:e,backhaul:i,attachments:0,nativeRefs:r}}attachEntry(t,e){let n=this.byCaller.get(t.apiKey);n===void 0&&(n=new Map,this.byCaller.set(t.apiKey,n));let r=n.get(e);if(r!==void 0)return n.delete(e),n.set(e,r),r.attachments+=1,r;this.evictOverflow();let i=this.build(t,e);return i.attachments=1,n.set(e,i),i}get conversationCount(){let t=0;for(let e of this.byCaller.values())t+=e.size;return t}evictOverflow(){for(;this.conversationCount>=M;){let t=this.byCaller.keys().next();if(t.done)throw new w;let e=t.value,n=this.byCaller.get(e),r=!1;for(let[i,a]of n)if(!(a.attachments>0)){n.delete(i),this.dropHandleIndexFor(e,i),a.backhaul.release().catch(()=>{}),r=!0;break}if(!r)throw n.size===0&&this.byCaller.delete(e),new w;n.size===0&&this.byCaller.delete(e)}}dropHandleIndexFor(t,e){for(let[n,r]of this.byHandle)r.apiKey===t&&r.conversationId===e&&this.byHandle.delete(n)}connectionFor(t){let e=null,n=!1,r=()=>(e===null&&(e=this.attachEntry(t,Y())),e),i={createResponse:a=>r().backhaul.clients.createResponse(a),listModels:()=>r().backhaul.clients.listModels(),sessionHandle:a=>r().backhaul.clients.sessionHandle(a),createResponseOn:(a,s)=>r().backhaul.clients.createResponseOn(a,s),onSessionEnd:(a,s)=>r().backhaul.clients.onSessionEnd(a,s)};return i.openAudio=async a=>{let s=r().backhaul.clients.openAudio;if(s===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return s(a)},i.openVideo=async a=>{let s=r().backhaul.clients.openVideo;if(s===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return s(a)},i.openVideoOut=async a=>{let s=r().backhaul.clients.openVideoOut;if(s===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return s(a)},{clients:i,bindResumeHandle:a=>{let s=e;if(s===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=Q;){let u=this.byHandle.keys().next();if(u.done)break;this.byHandle.delete(u.value)}this.byHandle.set(a,{apiKey:t.apiKey,conversationId:s.id})},clientsForResume:async a=>{let s=this.byHandle.get(a),u=s===void 0?void 0:this.byCaller.get(s.apiKey),y=s===void 0||u===void 0?void 0:u.get(s.conversationId);if(s===void 0||y===void 0||s.apiKey!==t.apiKey)throw new I("the conversation behind this resume handle is gone (evicted at capacity, or the replica restarted) or the handle is not yours \u2014 reconnect without a handle to start a new conversation");return e=y,y.attachments+=1,y.backhaul.clients},release:async()=>{if(n)return;n=!0;let a=e;e=null,a!==null&&(a.attachments-=1,!(a.attachments>0)&&await a.backhaul.release().catch(()=>{}))}}}async detachAll(){let t=[];for(let n of this.byCaller.values())for(let r of n.values())t.push(r.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let e=[];if(await Promise.all(t.map(async n=>{try{await n}catch(r){e.push(String(r instanceof Error?r.message:r))}})),e.length>0)throw new Error(`failed to detach ${e.length} conversation backhaul(s): ${e.join("; ")}`)}};h();import{createServer as ee}from"http";var te=5e3,ne=3e3,re="/api/client-tokens";function k(o,t,e){o.writeHead(t,{"content-type":"application/json"}),o.end(JSON.stringify(e))}function L(o,t,e,n){k(o,t,{error:{message:e,type:n}})}async function oe(o,t=fetch){let e=`${o.replace(/\/+$/,"")}${re}`;try{let n=await t(e,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(ne)});return n.status===404?{ready:!1,detail:`${e} answered 404 \u2014 this is not a uRun control plane`}:n.status>=500?{ready:!1,detail:`${e} answered HTTP ${n.status}`}:{ready:!0,detail:`${e} answered HTTP ${n.status}`}}catch(n){return{ready:!1,detail:`${e} unreachable: ${n instanceof Error?n.message:String(n)}`}}}function Ae(o){let t=o.resolver??new m({gatewayUrl:o.baseUrl}),e=o.registry??new b({baseUrl:o.baseUrl,apiUrl:o.apiUrl,catalog:o.catalog}),n=o.probeControlPlane??(()=>oe(o.baseUrl)),r=async l=>t.resolve($(l.headers.authorization)),i=l=>l instanceof p?{status:l.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:l instanceof g?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,a=N({clients:async l=>e.clientsFor(await r(l)),statusOf:i}),s=null,u=ee((l,c)=>{let d=new URL(l.url??"/","http://localhost");(async()=>{if(l.method==="GET"&&d.pathname==="/healthz"){k(c,200,{ok:!0});return}if(l.method==="GET"&&d.pathname==="/readyz"){let f=Date.now();if(!s||f-s.at>=te){let B=await n();s={at:f,...B}}k(c,s.ready?200:503,{ok:s.ready,control_plane:s.detail});return}if(d.pathname!=="/v1"&&!d.pathname.startsWith("/v1/")){L(c,404,`no route for ${l.method} ${d.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the Gemini Live WS upgrade path (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent), plus /healthz and /readyz`,"invalid_request_error");return}a(l,c)})().catch(f=>{if(!c.headersSent){L(c,500,String(f instanceof Error?f.message:f),"proxy_error");return}c.end()})}),y=o.conversations??new C({baseUrl:o.baseUrl,apiUrl:o.apiUrl,catalog:o.catalog}),K=[_(u,{authenticate:async l=>{let c=await t.resolve(l),d=y.connectionFor(c);return{clients:d.clients,scope:U(c.apiKey),onBindResumeHandle:d.bindResumeHandle,clientsForResume:d.clientsForResume,release:d.release}}}),O(u,{clients:async l=>e.clientsFor(await r(l)),statusOf:i}).close];return u.on("upgrade",(l,c)=>{let d=new URL(l.url??"/","http://localhost");d.pathname===S||d.pathname===T||c.destroyed||(c.write(`HTTP/1.1 404 Not Found\r
2
- Connection: close\r
3
- \r
4
- no WS route for ${d.pathname} \u2014 this endpoint serves only the Gemini Live and /v1/responses upgrade paths`),c.destroy())}),{server:u,closeAll:()=>e.closeAll(),closeWebSockets:async()=>{for(let l of K)await l();await y.detachAll()}}}async function ke(o,t,e){e&&await e();let n=new Promise(r=>o.close(()=>r()));o.closeAllConnections(),await n,await t()}export{p as a,g as b,$ as c,j as d,V as e,m as f,W as g,A as h,de as i,ue as j,q as k,U as l,b as m,M as n,Q as o,w as p,C as q,te as r,ne as s,Ae as t,ke as u};
@@ -1,2 +0,0 @@
1
- import{a as I,o as A}from"./chunk-CWJRDBDC.js";import{c as v}from"./chunk-YSFSRI3D.js";v();var J="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",k=1007,F=1008,R=1011,V=1008,n=class extends Error{constructor(o,i=k){super(o);this.closeCode=i}closeCode},d=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)?e:null,w=["setup","clientContent","realtimeInput","toolResponse"];function B(e){let t;try{t=JSON.parse(e)}catch{throw new n("client message is not valid JSON")}let o=d(t);if(!o)throw new n("client message must be a JSON object");let i=w.filter(c=>o[c]!==void 0),s=Object.keys(o).filter(c=>!w.includes(c));if(s.length>0)throw new n(`unknown client message field(s) ${JSON.stringify(s)} \u2014 expected exactly one of ${w.join(", ")}`);if(i.length!==1)throw new n(`client message must contain exactly one of ${w.join(", ")} (got ${i.length})`);let r=i[0],a=d(o[r]);if(!a)throw new n(`"${r}" must be a JSON object`);return{kind:r,[r]:a}}function C(e,t){if(typeof e=="string")return e;let o=d(e),i=o?o.parts:void 0;if(!Array.isArray(i))throw new n(`${t}: expected a Content with a parts array`);let s=[];for(let r of i){let a=d(r);if(a&&typeof a.text=="string"){s.push(a.text);continue}throw a&&(a.inlineData!==void 0||a.fileData!==void 0)?new n(`${t}: media parts (inlineData/fileData) are a declared follow-up slice (audio/video lanes over the proxy transport AudioBridge) \u2014 only text parts are supported in this slice`):a&&(a.functionCall!==void 0||a.functionResponse!==void 0)?new n(`${t}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new n(`${t}: unsupported part ${JSON.stringify(r)}`)}return s.join(`
2
- `)}var _={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},h="urun.videoOut";function G(e){return{framing:A,codec:e}}function Y(e){let{seq:t,ptsUs:o,codec:i,key:s}=e.header;return{[h]:{v:1,seq:t,ptsUs:o,codec:i,key:s,data:Buffer.from(e.au).toString("base64")}}}var x={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},S=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",h],T=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function E(e){let t=d(e);if(!t)throw new n("setup.realtimeInputConfig must be an object");for(let s of Object.keys(t))if(!(s==="automaticActivityDetection"||s==="activityHandling"))throw new n(s==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${s}"`);let o=d(t.automaticActivityDetection);if(t.automaticActivityDetection!==void 0&&!o)throw new n("realtimeInputConfig.automaticActivityDetection must be an object");for(let s of Object.keys(o??{}))if(s!=="disabled")throw new n(`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(o?.disabled!==!0)throw new n("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 i="START_OF_ACTIVITY_INTERRUPTS";if(t.activityHandling!==void 0){if(!T.includes(t.activityHandling))throw new n(`unsupported activityHandling ${JSON.stringify(t.activityHandling)} \u2014 expected one of ${T.join(", ")}`);i=t.activityHandling}return{manualActivity:!0,activityHandling:i}}var N=["temperature","maxOutputTokens","responseModalities"];function K(e){for(let[u,f]of Object.entries(x))if(e[u]!==void 0)throw new n(f);for(let u of Object.keys(e))if(!S.includes(u))throw new n(`unsupported setup field "${u}"`);if(typeof e.model!="string"||e.model.length===0)throw new n('setup.model is required (format "models/{model}")');let t=e.model.replace(/^models\//,""),o,i,s=!1,r=d(e.generationConfig);if(e.generationConfig!==void 0&&!r)throw new n("setup.generationConfig must be an object");if(r){for(let u of Object.keys(r))if(!N.includes(u))throw new n(`unsupported generationConfig field "${u}"`);if(r.temperature!==void 0){if(typeof r.temperature!="number")throw new n("generationConfig.temperature must be a number");o=r.temperature}if(r.maxOutputTokens!==void 0){if(typeof r.maxOutputTokens!="number")throw new n("generationConfig.maxOutputTokens must be a number");i=r.maxOutputTokens}if(r.responseModalities!==void 0){let u=Array.isArray(r.responseModalities)?r.responseModalities:[r.responseModalities];for(let f of u){let y=String(f).toUpperCase();if(y==="AUDIO"){s=!0;continue}if(y!=="TEXT")throw new n(`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=[];e.systemInstruction!==void 0&&a.push({role:"system",content:C(e.systemInstruction,"setup.systemInstruction")});let c;if(e.tools!==void 0){if(!Array.isArray(e.tools))throw new n("setup.tools must be an array");c=[];for(let u of e.tools){let f=d(u);if(!f)throw new n("setup.tools entries must be objects");for(let y of Object.keys(f))if(y!=="functionDeclarations")throw new n(`unsupported tool "${y}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(f.functionDeclarations))throw new n("tool.functionDeclarations must be an array");for(let y of f.functionDeclarations){let g=d(y);if(!g||typeof g.name!="string")throw new n("functionDeclarations entries require a string name");c.push({type:"function",name:g.name,description:g.description,parameters:g.parameters})}}c.length===0&&(c=void 0)}let m={enabled:!1,handle:null};if(e.sessionResumption!==void 0){let u=d(e.sessionResumption);if(!u)throw new n("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let f of Object.keys(u))if(f!=="handle")throw new n(`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 n("sessionResumption.handle must be a non-empty string when present");m={enabled:!0,handle:typeof u.handle=="string"?u.handle:null}}let p=e.realtimeInputConfig===void 0?_:E(e.realtimeInputConfig),l=!1;if(e[h]!==void 0){if(!d(e[h]))throw new n(`setup["${h}"] must be an object (the v1 opt-in is {})`);l=!0}return{model:t,systemItems:a,tools:c,temperature:o,maxOutputTokens:i,audioModality:s,activity:p,sessionResumption:m,videoOut:l}}function q(e){return{sessionResumptionUpdate:{newHandle:e,resumable:!0}}}function W(e){let t=Math.max(0,e??0)/1e3;return{goAway:{timeLeft:`${Math.round(t*1e3)/1e3}s`}}}function X(e){for(let i of Object.keys(e))if(i!=="turns"&&i!=="turnComplete")throw new n(`unsupported clientContent field "${i}"`);let t=e.turns===void 0?[]:e.turns;if(!Array.isArray(t))throw new n("clientContent.turns must be an array");let o=[];for(let i of t){let s=d(i);if(!s)throw new n("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 n(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(s.role)})`);o.push({role:r==="model"?"assistant":"user",content:C(s,"clientContent.turns")})}return{items:o,turnComplete:e.turnComplete===!0}}var b=16e3,O=24e3,$=`audio/pcm;rate=${O}`,z=Buffer.alloc(960).toString("base64");function j(e,t,o){if(t===o)return e;let i=Math.floor(e.length*o/t),s=new Int16Array(i),r=t/o,a=e.length-1;for(let c=0;c<i;c++){let m=c*r,p=Math.floor(m);if(p>=a){s[c]=e[a];continue}let l=m-p;s[c]=Math.round(e[p]+(e[p+1]-e[p])*l)}return s}function U(e){let t=d(e);if(!t)throw new n("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof t.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(t.mimeType))throw new n(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(t.mimeType)}`);if(typeof t.data!="string"||t.data.length===0)throw new n("realtimeInput.video.data must be non-empty base64");let o=Buffer.from(t.data,"base64");if(o.length<3||o[0]!==255||o[1]!==216)throw new n("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(o)}function D(e){let t=d(e);if(!t)throw new n("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof t.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(t.mimeType))throw new n(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(t.mimeType)}`);let o=/;\s*rate=(\d+)/.exec(t.mimeType);if(o&&Number(o[1])!==b)throw new n(`realtimeInput.audio.mimeType declares rate=${o[1]} \u2014 the Live spec input format is ${b} Hz (audio/pcm;rate=${b})`);if(typeof t.data!="string"||t.data.length===0)throw new n("realtimeInput.audio.data must be non-empty base64");let i=Buffer.from(t.data,"base64");if(i.length===0||i.length%2!==0)throw new n(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${i.length} bytes \u2014 not even)`);let s=new Int16Array(i.length/2);for(let a=0;a<s.length;a++)s[a]=i.readInt16LE(a*2);let r=j(s,b,O);return Buffer.from(r.buffer,r.byteOffset,r.byteLength).toString("base64")}function Q(e){if(e.mediaChunks!==void 0)throw new n('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 n("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let s=e.activityStart!==void 0?"activityStart":"activityEnd",r=d(e[s]);if(!r||Object.keys(r).length!==0)throw new n(`realtimeInput.${s} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:s}}let t=["text","audio","audioStreamEnd","video"];for(let i of Object.keys(e))if(!t.includes(i))throw new n(`unsupported realtimeInput field "${i}"`);let o=t.filter(i=>e[i]!==void 0);if(o.length!==1)throw new n(`realtimeInput must carry exactly one of ${t.join(", ")} (got ${o.length})`);if(e.audio!==void 0)return{kind:"audio",base64Pcm24k:D(e.audio)};if(e.video!==void 0)return{kind:"video",jpegFrame:U(e.video)};if(e.audioStreamEnd!==void 0){if(e.audioStreamEnd!==!0)throw new n("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof e.text!="string"||e.text.length===0)throw new n("realtimeInput.text must be a non-empty string");return{kind:"text",text:e.text}}function Z(e){for(let t of Object.keys(e))if(t!=="functionResponses")throw new n(`unsupported toolResponse field "${t}"`);if(!Array.isArray(e.functionResponses)||e.functionResponses.length===0)throw new n("toolResponse.functionResponses must be a non-empty array");return e.functionResponses.map(t=>{let o=d(t);if(!o||typeof o.id!="string"||o.id.length===0)throw new n("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:o.id,output:JSON.stringify(o.response??null)}})}function ee(e){return e.type!=="response.output_text.delta"||typeof e.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:e.delta}]}}}}function te(e){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:$,data:e}}]}}}}function ne(e){let t=d(e)??{},o=Array.isArray(t.output)?t.output:[],i=[],s=[],r=typeof t.output_text=="string"?t.output_text:"";if(!r)for(let p of o){let l=d(p);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let u of l.content){let f=d(u);f?.type==="output_text"&&typeof f.text=="string"&&(r+=f.text)}}r&&s.push({role:"assistant",content:r});for(let p of o){let l=d(p);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new n(`upstream function_call item is missing call_id/name: ${JSON.stringify(p)}`,R);let u=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",f;try{f=JSON.parse(u)}catch{throw new n(`upstream function_call arguments are not valid JSON: ${u}`,R)}i.push({id:l.call_id,name:l.name,args:f}),s.push({type:"function_call",call_id:l.call_id,name:l.name,arguments:u})}let a=[];i.length>0&&a.push({toolCall:{functionCalls:i}}),a.push({serverContent:{generationComplete:!0}});let c=d(t.usage),m={serverContent:{turnComplete:!0}};if(c){let p={};typeof c.input_tokens=="number"&&(p.promptTokenCount=c.input_tokens),typeof c.output_tokens=="number"&&(p.responseTokenCount=c.output_tokens),typeof c.total_tokens=="number"&&(p.totalTokenCount=c.total_tokens),Object.keys(p).length>0&&(m.usageMetadata=p)}return a.push(m),{messages:a,assistantItems:s}}export{J as a,k as b,F as c,R as d,V as e,n as f,B as g,h,G as i,Y as j,K as k,q as l,W as m,X as n,b as o,O as p,$ as q,z as r,j as s,Q as t,Z as u,ee as v,te as w,ne as x};
@@ -1 +0,0 @@
1
- import{c as m}from"./chunk-YSFSRI3D.js";m();var B=24e3,w=class{constructor(r,t){this.backend=r;this.opts=t}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(r){if(!this.source)throw new Error("startOutbound() not called");let t=Buffer.from(r,"base64"),n=Math.floor(t.byteLength/2),a=new Int16Array(n);for(let s=0;s<n;s++)a[s]=t.readInt16LE(s*2);this.source.onData(a)}async startInbound(r){if(r==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("rt-audio-out").track is null)');this.sink=this.backend.createSink(r),this.sink.onframe=t=>{let n=Buffer.from(t.samples.buffer,t.samples.byteOffset,t.samples.byteLength).toString("base64");for(let a of this.outHandlers)a(n)}}onOutputAudio(r){return this.outHandlers.add(r),()=>this.outHandlers.delete(r)}};async function W(e,r,t=1e4){let n=new w(r,{sampleRate:24e3}),a=await n.startOutbound();await e.stream("rt-audio-in").attach(a);let s=await R(e,t);return await n.startInbound(s),n}function R(e,r){let t=e.stream("rt-audio-out");return t.track!=null?Promise.resolve(t.track):new Promise((n,a)=>{let s,c=setTimeout(()=>{s?.(),a(new Error(`enableSessionAudio: no downstream audio track within ${r}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},r),u=o=>{o!=null&&(clearTimeout(c),s?.(),n(o))};s=t.on?.("track",u),t.track!=null&&u(t.track)})}var h="rt-video-in",y=256e3;function H(e){let r=e.stream(h),t=r.emit;if(typeof t!="function")throw new Error(`enableSessionVideo: session.stream('${h}') has no emit() \u2014 the named-DATA stream produce seam (@urun-sh/core SessionStream.emit, contract \xA75) is required for the video frame lane`);return{sendInputFrame:async n=>{if(n.byteLength===0)throw new Error("enableSessionVideo: refusing to send an empty video frame");if(n.byteLength>y)throw new Error(`enableSessionVideo: frame is ${n.byteLength} bytes \u2014 over the ${y}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await t.call(r,n)}}}var V=24e3,l=480,T=111;async function I(){let e=await import("opusscript"),r=e.default??e;return new r(V,1,2048)}async function N(){let e=await import("werift"),r=await I();return{createSource:()=>{let t=new e.MediaStreamTrack({kind:"audio"}),n=Math.random()*4294967295>>>0,a=Math.random()*65535&65535,s=Math.random()*4294967295>>>0,c=new Int16Array(0);return{track:t,onData:u=>{let o=new Int16Array(c.length+u.length);o.set(c,0),o.set(u,c.length);let i=0;for(;o.length-i>=l;){let d=o.subarray(i,i+l);i+=l;let f=Buffer.from(d.buffer,d.byteOffset,d.byteLength),O=r.encode(f,l);a=a+1&65535,s=s+l>>>0;let S=new e.RtpHeader({version:2,payloadType:T,sequenceNumber:a,timestamp:s,ssrc:n,marker:!1}),E=new e.RtpPacket(S,O);t.writeRtp(E)}c=o.slice(i)}}},createSink:t=>{let n={onframe:null};return t.onReceiveRtp.subscribe(s=>{let c;try{c=r.decode(s.payload)}catch{return}let u=new Int16Array(c.length/2);for(let o=0;o<u.length;o++)u[o]=c.readInt16LE(o*2);n.onframe?.({samples:u})}),n}}}async function $(){let e=await import("@roamhq/wrtc");return{createSource:()=>{let r=new e.nonstandard.RTCAudioSource;return{track:r.createTrack(),onData:n=>r.onData({samples:n,sampleRate:24e3})}},createSink:r=>{let t={onframe:null},n=new e.nonstandard.RTCAudioSink(r);return n.ondata=a=>{t.onframe?.(a)},t}}}m();import{WtControlDecoder as L,consumeWtDataStreams as U,encodeWtControlFrame as _,STREAM_END as x}from"@urun-sh/core";var p="rt-video-out",P="urun.videoOut.v1",b="h264";function g(e,r){if(!Number.isInteger(e.seq)||e.seq<0)throw new Error(`${r}: header seq must be a non-negative integer (got ${e.seq})`);if(!Number.isInteger(e.ptsUs))throw new Error(`${r}: header ptsUs must be an integer microsecond timestamp (got ${e.ptsUs})`);if(typeof e.codec!="string"||e.codec.length===0)throw new Error(`${r}: header codec must be a non-empty string`);if(typeof e.key!="boolean")throw new Error(`${r}: header key must be a boolean`)}function q(e,r){if(g(e,"encodeVideoOutRecord"),r.byteLength===0)throw new Error("encodeVideoOutRecord: refusing to encode an empty access unit");let t=new TextEncoder().encode(JSON.stringify({v:1,seq:e.seq,ptsUs:e.ptsUs,codec:e.codec,key:e.key}));if(t.byteLength>65535)throw new Error(`encodeVideoOutRecord: header JSON is ${t.byteLength} bytes \u2014 over the u16 length prefix`);let n=new Uint8Array(2+t.byteLength+r.byteLength);return n[0]=t.byteLength&255,n[1]=t.byteLength>>8,n.set(t,2),n.set(r,2+t.byteLength),n}function k(e){if(e.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${e.byteLength} byte(s), no header length)`);let r=e[0]|e[1]<<8;if(e.byteLength<2+r)throw new Error(`decodeVideoOutRecord: truncated record (header says ${r} bytes, ${e.byteLength-2} present)`);let t;try{t=JSON.parse(new TextDecoder().decode(e.subarray(2,2+r)))}catch(c){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(c)})`)}let n=t;if(n===null||typeof n!="object"||n.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(n?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let a=n;g(a,"decodeVideoOutRecord");let s=e.subarray(2+r);if(s.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:a,au:s.slice()}}async function J(e){let r=e.wtDataEndpoint;if(typeof r!="function")throw new Error("negotiateSessionVideoOutQuic: the core session exposes no wtDataEndpoint() \u2014 the \xA75-over-QUIC dial seam (urun-infra#1608 TransportOfferResponse.webtransport, offered with wtData: true) is required for the video OUT lane; there is no WS fallback");let t=await r.call(e),n=t.sessionId??e.sessionId;if(typeof n!="string"||n.length===0)throw new Error("negotiateSessionVideoOutQuic: no session id \u2014 the WT hello requires one and neither the negotiated endpoint nor the core session carries it");return{transport:"webtransport",url:t.url,certHashes:t.certHashes,token:t.token,sessionId:n,stream:p,codec:b,framing:P}}var Y="urun.videoOut.v2-ws";function G(e){let r=e.stream(p);if(typeof r.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${p}') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the video OUT lane`);let t=new Set,n=new Set,a=new Set,s=o=>{for(let i of n)i(o)},c=!1,u=async()=>{for await(let o of r.messages()){if(!(o instanceof Uint8Array)){s(new Error(`openSessionVideoOutLane: "${p}" delivered a non-BINARY \xA75 payload (${typeof o}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let i;try{i=k(o)}catch(d){s(d instanceof Error?d:new Error(String(d)));continue}for(let d of t)d(i)}for(let o of a)o()};return{codec:b,onOutputFrame:o=>(t.add(o),c||(c=!0,u().catch(i=>s(i instanceof Error?i:new Error(String(i))))),()=>t.delete(o)),onLaneError:o=>(n.add(o),()=>n.delete(o)),onEnd:o=>(a.add(o),()=>a.delete(o))}}function D(e){if(e.length%2!==0||/[^0-9a-fA-F]/.test(e))throw new Error(`consumeVideoOut: certHashes entry is not hex sha-256 (${JSON.stringify(e)})`);let r=new Uint8Array(e.length/2);for(let t=0;t<r.length;t++)r[t]=parseInt(e.slice(t*2,t*2+2),16);return r}async function Q(e,r,t){let n=t??(typeof globalThis.WebTransport=="function"?(i,d)=>new globalThis.WebTransport(i,d):void 0);if(!n)throw new Error("consumeVideoOut: no WebTransport implementation \u2014 this platform has no globalThis.WebTransport; inject a connect factory (the third argument)");let a=n(e.url,{serverCertificateHashes:e.certHashes.map(i=>({algorithm:"sha-256",value:D(i)}))});await a.ready;let s=await a.createBidirectionalStream();await s.writable.getWriter().write(_({t:"hello",token:e.token,sessionId:e.sessionId}));let c=s.readable.getReader(),u=new L,o=!1;for(;!o;){let{value:i,done:d}=await c.read();if(d)throw new Error("consumeVideoOut: gateway closed the control stream before hello-ack");if(i)for(let f of u.push(i)){if(f.t==="hello-ack"){o=!0;break}throw new Error(`consumeVideoOut: gateway rejected hello (got ${JSON.stringify(f.t)} before hello-ack)`)}}await U(a.incomingUnidirectionalStreams,{onData:(i,d)=>{if(i===e.stream){if(d===x){r.onEnd?.();return}if(!(d instanceof Uint8Array)){r.onLaneError(new Error(`consumeVideoOut: "${e.stream}" delivered a non-BINARY \xA75 payload (${typeof d}) \u2014 spec v1 records ride BINARY envelopes only`));return}try{r.onFrame(k(d))}catch(f){r.onLaneError(f instanceof Error?f:new Error(String(f)))}}},onLaneError:i=>r.onLaneError(i)})}export{B as a,w as b,W as c,h as d,y as e,H as f,N as g,$ as h,p as i,P as j,b as k,q as l,k as m,J as n,Y as o,G as p,Q as q};
@@ -1 +0,0 @@
1
- import{c as p}from"./chunk-YSFSRI3D.js";p();p();function l(s){return s.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}p();var A={chat:!0,code:!0,agent:!0,vl:!0},y={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function S(s){let e=s.toLowerCase();return Object.hasOwn(y,e)?y[e]:null}function g(s){let e=s.engine_args?.context_length;return typeof e=="number"&&Number.isFinite(e)&&e>0?e:null}function _(s,e){let t=e.filter(i=>l(`${i.model_id}-${i.variant}`)===s);if(t.length===1)return t[0];if(t.length>1)return v(t);let n=e.filter(i=>l(i.model_id)===s),o=new Set(n.map(i=>i.variant));return n.length>0&&o.size===1?v(n):null}function v(s){let e=s[0],t=r=>s.every(a=>(a[r]??void 0)===(e[r]??void 0))?e[r]:void 0,n=s.map(g),o=n[0],i=o!=null&&n.every(r=>r===o)?o:null;return{model_id:e.model_id,variant:e.variant,gpu_spec:t("gpu_spec"),engine:t("engine"),task:t("task"),description:t("description"),size_gb:t("size_gb"),hf_repo:t("hf_repo"),engine_args:i!==null?{context_length:i}:{},updated_at:t("updated_at")}}function b(s){let e=s.updated_at?Date.parse(s.updated_at):NaN;return Number.isFinite(e)?Math.floor(e/1e3):0}function R(s,e){let t=_(s,e);if(!t)return{id:s,object:"model",created:0,owned_by:"urun"};let n=g(t);return{id:s,object:"model",created:b(t),owned_by:"urun",name:`${t.model_id}:${t.variant}`,...t.description?{description:t.description}:{},...t.task?{task:t.task}:{},...t.engine?{engine:t.engine}:{},...t.gpu_spec?{gpu_spec:t.gpu_spec}:{},...n!==null?{context_length:n}:{}}}function x(s,e){let t=[];for(let n of s){let o=_(n,e);if(!o||!o.task||A[o.task]!==!0)continue;let i=g(o),r={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};i!==null&&(r.max_tokens={type:"integer",min:1,max:i,unit:"token"}),t.push({schema_version:"2.4",id:n,name:`${o.model_id}:${o.variant}`,created:b(o),quantization:S(o.variant),description:o.description??"",hugging_face_id:o.hf_repo?o.hf_repo.split("@")[0]??o.hf_repo:"",input_modalities:[{type:"text",...i!==null?{supported_inputs:{max_context_length:{value:i,unit:"token"}}}:{}},...o.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:r}]})}return{data:t}}var K="https://api.urun.sh/v1",d=class extends Error{},u=class extends Error{},f="urs1.";function k(s,e){return f+Buffer.from(JSON.stringify([s,e]),"utf8").toString("base64url")}function P(s){if(!s.startsWith(f))return null;try{let e=JSON.parse(Buffer.from(s.slice(f.length),"base64url").toString("utf8"));if(Array.isArray(e)&&typeof e[0]=="string"&&typeof e[1]=="string")return{app:e[0],key:e[1]}}catch{}return null}async function z(s){let e=s.fetchImpl??fetch,t=`${s.apiUrl.replace(/\/+$/,"")}/apps`,n=await e(t,{headers:{Authorization:`Bearer ${s.apiKey}`,Accept:"application/json"}});if(!n.ok)throw new Error(`org apps listing failed: GET ${t} \u2192 ${n.status}`);let o=await n.json();if(!Array.isArray(o.apps))throw new Error(`org apps listing returned no "apps" array (GET ${t})`);if(o.truncated===!0)throw new Error(`org apps listing was truncated (GET ${t} returned ${o.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return o.apps}function M(s,e){let t=e.filter(i=>l(`${i.model_id}-${i.variant}`)===s);if(t.length>0)return t;let n=e.filter(i=>l(i.model_id)===s);return new Set(n.map(i=>i.variant)).size===1?n:[]}function D(s){let e=s.engine_args?.expects_image;return e==null?"edit":e===!1?"generation":e===!0?"edit":null}function O(s){let e=s.map(D),t=e[0];return t!=null&&e.every(n=>n===t)?t:null}var C=6e4,$=class{constructor(e){this.opts=e}opts;pool=new Map;appsCache=null;rowsCache=null;seed(e,t){this.pool.set(e,Promise.resolve(t))}async deployedApps(){if(!this.opts.listApps)return[];let e=this.opts.appsTtlMs??C;if(this.appsCache&&Date.now()-this.appsCache.at<e)return this.appsCache.apps;let t=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:t},t}async catalogRows(){if(!this.opts.listCatalog)return[];let e=this.opts.appsTtlMs??C;if(this.rowsCache&&Date.now()-this.rowsCache.at<e)return this.rowsCache.rows;let t=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:t},t}servable(e){return e.filter(t=>t.function_name===this.opts.fnName&&t.deployment_status==="active").map(t=>t.app_slug)}async availableIds(){let e=this.opts.defaultApp,t=new Set(e===null?[]:[e]);if(this.opts.listApps)for(let o of this.servable(await this.deployedApps()))t.add(o);for(let o of this.pool.keys())t.add(o);let n=[...t].filter(o=>o!==e).sort();return e===null?n:[e,...n]}async noDefaultApp(e){let t=await this.availableIds(),n=t.length>0?t.join(", "):"(none deployed)";throw new d(e===""?`no "model" was given and this endpoint has no default model \u2014 name one of your org's deployed "${this.opts.fnName}" apps; available models: ${n}`:`model "${e}" does not name a deployed "${this.opts.fnName}" app in your org, and this endpoint has no default model to fall back to; available models: ${n}`)}async resolveApp(e){let t=(e??"").trim(),n=this.opts.defaultApp;if(!t||t==="urun")return n===null?this.noDefaultApp(t):n;let o=l(t);if(o===n)return n;if(this.pool.has(o))return o;if(this.opts.listApps){let i=await this.deployedApps(),r=this.servable(i);if(r.includes(o))return o;let a=r.filter(c=>c.startsWith(`${o}-`));if(a.length===1)return a[0];if(a.length>1)throw new d(`model "${t}" is ambiguous across deployed apps ${a.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let h=i.find(c=>c.app_slug===o||c.app_slug.startsWith(`${o}-`));if(h)throw new d(`model "${t}" maps to app "${h.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${t}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.listCatalog){let r=(await this.opts.listCatalog()).find(a=>t===a.model_id||t===`${a.model_id}:${a.variant}`||o===l(a.model_id)||o===l(`${a.model_id}-${a.variant}`));if(r)throw new d(`model "${t}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${r.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return n===null?this.noDefaultApp(t):n}async sessionFor(e){let t=await this.resolveApp(e),n=this.pool.get(t);return n||(n=Promise.resolve(this.opts.openSession(t)),this.pool.set(t,n),n.catch(()=>this.pool.delete(t))),{app:t,entry:await n}}keyOf(e){let t=this.opts.sessionKey;if(!t)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let n=t(e);if(typeof n!="string"||n.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return n}async handleFor(e){let{app:t,entry:n}=await this.sessionFor(e);return{app:t,handle:k(t,this.keyOf(n))}}async sessionForHandle(e){let t=P(e);if(!t)throw new u(`malformed session handle ${JSON.stringify(e)} \u2014 not issued by this proxy`);let{app:n,key:o}=t,i=this.pool.get(n),r=i?await i.then(a=>a,()=>null):null;if(r===null)throw new u(`the session behind this handle (app "${n}") is gone \u2014 closed, evicted after its pod died, or the proxy restarted; it cannot be reattached. Reconnect without a handle to start a new session.`);if(this.keyOf(r)!==o)throw new u(`the session behind this handle (app "${n}") was replaced \u2014 the original backhaul died and a new session serves this app now; the handle's session cannot be reattached. Reconnect without a handle to start a new session.`);return{app:n,entry:r}}async evict(e,t){let n=this.pool.get(e);!n||await n.then(i=>i,()=>null)!==t||this.pool.get(e)!==n||(this.pool.delete(e),await this.opts.closeSession(t).catch(()=>{}))}async modelList(){let e=await this.availableIds(),t=await this.catalogRows().catch(()=>[]);return{object:"list",data:e.map(n=>R(n,t))}}async openRouterModels(){let e=await this.availableIds(),t=await this.catalogRows();return x(e,t)}async supportsImages(e,t){let n=await this.resolveApp(e),o=await this.catalogRows(),i=M(n,o);return i.length===0||!i.every(r=>(r.task??void 0)==="image")?!1:O(i)===t}async closeAll(){let e=[...this.pool.values()];this.pool.clear();let t=[];if(await Promise.all(e.map(async n=>{try{await this.opts.closeSession(await n)}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to close ${t.length} pooled session(s): ${t.join("; ")}`)}};export{K as a,d as b,u as c,z as d,$ as e};
@@ -1 +0,0 @@
1
- import{e as r}from"./chunk-OI2OY32M.js";import{c as e}from"./chunk-YSFSRI3D.js";e();var o=0;function i(){return o+=1,`req_${Date.now().toString(36)}_${o.toString(36)}`}var s=class{transport;constructor(n){this.transport=new r(n)}responses={create:async n=>{let t=i(),a=this.transport.sendResponses(n,t);return Object.assign((async function*(){yield*a})(),{requestId:t})}}};export{s as a};
@@ -1,6 +0,0 @@
1
- import{a as U}from"./chunk-K23AZPI4.js";import{a as b}from"./chunk-GBBY3PCZ.js";import{a as P,c as f,d as _,e as k}from"./chunk-DMD5UENK.js";import{c as x,f as v,g as E,p as R}from"./chunk-CWJRDBDC.js";import{c as p}from"./chunk-YSFSRI3D.js";p();import{App as I,createClientToken as H}from"@urun-sh/core";p();function D(e){let n=e?.type;return n==="response.created"||n==="response.in_progress"}function M(e){let n=e?.type;return n==="response.completed"||n==="error"}function A(e){return async function(t){return(async function*(){for(let a=1;a<=2;a++){let{app:s,entry:r}=await e.sessionFor(t.model),i=!1,u=!1;try{let c=await e.createStream(r,t);for await(let d of c){if(M(d)&&(i=!0),D(d)){if(a>1)continue}else u=!0;yield d}}catch(c){if(a<2&&!u){await e.evict(s,r);continue}throw c}if(i)return;if(a<2&&!u){await e.evict(s,r);continue}return}})()}}p();var F=3,W="llm",B=new Set(["paused","error"]);function O(e,n){let t=e.doc(W),o=typeof t.onConnectionState=="function",a=typeof e.onPhase=="function";if(!o&&!a)throw new Error(`session for app "${n}" exposes neither a doc connection-state surface nor onPhase \u2014 session-gone detection requires @urun-sh/core Session (SessionDocument.onConnectionState / Session.onPhase); pooling a session with no native lifecycle signal would silently swallow requests dispatched after the platform closes it`);let s=null,r=new Set,i=[],u=c=>{if(!s){s=c;for(let d of[...r])d(c);r.clear()}};return o&&i.push(t.onConnectionState(c=>{c.consecutiveFailures<F||u(new f(`the pooled session for app "${n}" is gone: its doc backhaul failed ${c.consecutiveFailures} consecutive times (last close ${c.lastCloseCode===null?"by provider liveness reap":`code ${c.lastCloseCode}`}${c.lastCloseReason?`, reason ${JSON.stringify(c.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`))})),a&&i.push(e.onPhase(c=>{B.has(c.name)&&u(new f(`the pooled session for app "${n}" lost its compute binding (phase "${c.name}"). The entry is evicted; retry to get a fresh session. The named session itself is not over \u2014 it stays attachable by name.`))})),{gone:()=>s,onGone(c){return s?(c(s),()=>{}):(r.add(c),()=>r.delete(c))},dispose(){r.clear();for(let c of i.splice(0))c()}}}async function*y(e,n){let t=n.gone();if(t)throw t;let o=e[Symbol.asyncIterator](),a,s=new Promise((r,i)=>{a=n.onGone(u=>i(u))});s.catch(()=>{});try{for(;;){let r=await Promise.race([o.next(),s]);if(r.done)return;yield r.value}}finally{a?.(),o.return?.(void 0)}}function N(e,n,t,o){let a=e[t];if(typeof a!="function")throw new Error(`app "${n}" has no function "${t}" (set URUN_FUNCTION)`);let s=o?a(void 0,o):a();if(typeof s.end!="function")throw new Error(`app "${n}" function "${t}" returned a session without end()`);return s}function K(e,n){let{baseUrl:t,orgId:o,fnName:a,auth:s,subject:r}=e,i=e.sessionStartOptions?.(n),u=l=>({session:l,responses:new b(l),gone:O(l,n)});if(s.lane==="jwt"){let l=I(n,{baseUrl:t,orgId:o,jwt:s.jwt});return u(N(l,n,a,i))}let{apiKey:c,gatewayUrl:d}=s,h=I(n,{baseUrl:t,orgId:o,getAccessToken:async()=>(await H(c,{baseUrl:d,expiresIn:300,allowedFunctions:[`${n}/${a}`],...r===void 0?{}:{subject:r}})).token});return u(N(h,n,a,i))}function q(e){let n=e.session.id;if(typeof n!="string"||n.length===0)throw new Error("pooled session exposes no stable session id \u2014 the session-identity seam requires @urun-sh/core Session (session.id)");return n}var Y=new Set(["error","paused"]);function xe(e){let{auth:n,catalog:t}=e,o=null,a=r=>{let i=K(e,r);return e.onSessionOpen&&typeof i.session.id=="string"&&i.session.id!==""&&e.onSessionOpen(r,i.session.id),i.gone.onGone(()=>{o?.evict(r,i)}),i},s=new k({defaultApp:e.defaultApp,fnName:e.fnName,openSession:a,closeSession:async r=>{if(r.gone.dispose(),e.release==="detach"){if(typeof r.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');r.session.detach();return}await r.session.end()},listApps:n.lane==="api-key"?()=>_({apiUrl:e.apiUrl,apiKey:n.apiKey}):null,listCatalog:t?()=>U(t):null,sessionKey:q});return o=s,e.defaultApp!==null&&s.seed(e.defaultApp,a(e.defaultApp)),s}function ve(e,n=E){let t=new WeakMap,o=new WeakMap,a=new WeakMap;return{createResponse:A({sessionFor:s=>e.sessionFor(s),evict:(s,r)=>e.evict(s,r),createStream:async(s,r)=>y(await s.responses.responses.create(r),s.gone)}),listModels:()=>e.modelList(),openRouterModels:()=>e.openRouterModels(),openAudio:async s=>{let{entry:r}=await e.sessionFor(s),i=t.get(r);return i||(i=(async()=>x(r.session,await n()))(),t.set(r,i),i.catch(()=>t.delete(r))),i},openVideo:async s=>{let{entry:r}=await e.sessionFor(s),i=o.get(r);return i||(i=v(r.session),o.set(r,i)),i},openVideoOut:async s=>{let{entry:r}=await e.sessionFor(s),i=a.get(r);return i||(i=R(r.session),a.set(r,i)),i},sessionHandle:async s=>(await e.handleFor(s)).handle,createResponseOn:async(s,r)=>{let{entry:i}=await e.sessionForHandle(s);return y(await i.responses.responses.create(r),i.gone)},onSessionEnd:async(s,r)=>{let{entry:i}=await e.sessionForHandle(s),u=i.session;if(typeof u.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let c=!1;return u.onPhase(d=>{if(c||!Y.has(d.name))return;c=!0;let h=u.endsAt??null;r({timeLeftMs:h?Math.max(0,h.getTime()-Date.now()):null,reason:d.reason??`session ${d.name}`})})}}}p();import J from"os";import X from"path";import{fileURLToPath as z}from"url";import{format as Q}from"util";var $="0.5.5";var T=4141,Z=$,Ie={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},omp:{bin:"omp",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function ee(e,n="openai"){if(n==="anthropic")return{ANTHROPIC_BASE_URL:`http://127.0.0.1:${e}`,ANTHROPIC_API_KEY:"urun-local"};let t=`http://127.0.0.1:${e}/v1`;return{OPENAI_BASE_URL:t,OPENAI_API_BASE:t,OPENAI_API_KEY:"urun-local"}}function Ne(e,n,t){let o={...e,...ee(n,t)};return delete o.URUN_API_KEY,delete o.URUN_JWT,o}function $e(e,n,t){let o=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${e}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],a=t.indexOf("--"),s=a===-1?t:t.slice(0,a),r=s.some(d=>d==="-m"||d==="--model"||d.startsWith("--model=")),i=t[0]==="exec",u=s.includes("--skip-git-repo-check"),c=i&&!u?["exec","--skip-git-repo-check",...t.slice(1)]:t;return[...o,...r?[]:["-m",n],...c]}function Ce(e,n){return n.some(o=>o==="--model"||o==="--provider"||o.startsWith("--model=")||o.startsWith("--provider="))?n:["--provider","urun","--model",e,...n]}function Te(e,n){return n.some(o=>o==="--model"||o==="--provider"||o.startsWith("--model=")||o.startsWith("--provider="))?n:["--model",`urun/${e}`,...n]}function Ge(e,n,t){let o=(e.URUN_PI_EXTENSION_ENTRY??"").trim();if(o){if(!t(o))throw new Error(`URUN_PI_EXTENSION_ENTRY points at ${o}, which does not exist \u2014 the launcher that injected it (the vendored urun CLI shim) must materialize the pi extension bundle before spawning pi`);return o}let a=z(new URL("../pi-extension/standalone.cjs",n));if(!t(a))throw new Error(`pi extension entry not found at ${a} \u2014 the pi lane loads the self-contained @urun-sh/openai dist/pi-extension/standalone.cjs from the installed package (run \`urun compat pi\` via the npm-installed CLI, e.g. \`npx @urun-sh/openai\`), or pass an entry to pi yourself: \`pi -e <path-to-@urun-sh/openai>/dist/pi-extension/standalone.cjs\``);return a}function G(e){let n=Number(e);if(!Number.isInteger(n)||n<0)throw new Error(`--port needs a non-negative integer (0 = OS-assigned ephemeral), got ${e}`);return n}function Le(e){if(e[0]!=="--port")return null;let n=G(e[1]);return e.splice(0,2),n}function je(e){let n=e.indexOf("--port");if(n===-1)return T;let t=G(e[n+1]);return e.splice(n,2),t}function De(e){if(e===0)throw new Error("`env` cannot use --port 0: it starts no proxy, so the OS never assigns the ephemeral port \u2014 pass the actual port of the running proxy");return e}function w(e,n){let t=(e[n]??"").trim();if(!t)throw new Error(`${n} is not set. The proxy needs URUN_BASE_URL, URUN_ORG_ID, URUN_APP and either URUN_API_KEY (the org key; the proxy vends short-lived function-scoped client tokens from it via the SDK) or URUN_JWT (an explicit pre-vended token override) \u2014 plus optional URUN_FUNCTION, default "serve" \u2014 to open the backhaul session.`);return t}function Me(e){let n=ne(w(e,"URUN_BASE_URL")),t=w(e,"URUN_ORG_ID"),o=w(e,"URUN_APP"),a=(e.URUN_FUNCTION??"serve").trim()||"serve",s=(e.URUN_API_URL??"").trim(),r=s?C(s,"URUN_API_URL"):P,i=(e.URUN_JWT??"").trim();if(i)return{baseUrl:n,orgId:t,appId:o,fnName:a,apiUrl:r,auth:{lane:"jwt",jwt:i}};let u=(e.URUN_API_KEY??"").trim();if(u){let c=(e.URUN_GATEWAY_URL??"").trim(),d=c?C(c):void 0;return{baseUrl:n,orgId:t,appId:o,fnName:a,apiUrl:r,auth:{lane:"api-key",apiKey:u,gatewayUrl:d}}}throw new Error("neither URUN_API_KEY nor URUN_JWT is set. Set URUN_API_KEY (the org API key \u2014 the proxy vends short-lived, function-scoped client tokens from it via the SDK, so the key never reaches the agent process) or URUN_JWT (an explicit pre-vended token; when set it always wins). URUN_BASE_URL, URUN_ORG_ID and URUN_APP are also required.")}function ne(e,n="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(e).pathname))throw new Error(`${n} must be the session-gateway base (e.g. https://api.urun.sh), got ${e} \u2014 a trailing /v1 is the org control-plane API form (that belongs in URUN_API_URL); session allocation 404s against it. Drop the /v1.`);return e}function C(e,n="URUN_GATEWAY_URL"){let t=new URL(e),o=t.hostname==="localhost"||t.hostname==="127.0.0.1"||t.hostname==="[::1]";if(t.protocol!=="https:"&&!o)throw new Error(`${n} must be https (got ${e}) \u2014 the org API key rides the request's Authorization header`);return e}function Fe(e){return{app:e.appId,org:e.orgId,fn:e.fnName,base_url:e.baseUrl,proxy_version:Z}}function te(e,n){return e.app===n.app&&e.org===n.org&&e.fn===n.fn&&e.base_url===n.base_url&&e.proxy_version===n.proxy_version}var oe=250;async function re(e,n=oe){try{let t=await fetch(`http://127.0.0.1:${e}/stats`,{signal:AbortSignal.timeout(n)});if(!t.ok)return null;let o=(await t.json()).identity;if(o==null||typeof o!="object")return null;let{app:a,org:s,fn:r,base_url:i,proxy_version:u}=o;return typeof a!="string"||typeof s!="string"||typeof r!="string"||typeof i!="string"||typeof u!="string"?null:{app:a,org:s,fn:r,base_url:i,proxy_version:u}}catch{return null}}var L=2e3;async function se(e,n=L){try{let t=await fetch(`http://127.0.0.1:${e}/v1/models`,{signal:AbortSignal.timeout(n)});return t.ok?null:`GET /v1/models answered HTTP ${t.status}`}catch(t){return`GET /v1/models failed (${String(t instanceof Error?t.message:t)})`}}async function We(e,n,t=T,o=L){if(e!=null)return{mode:"spawn",port:e};let a=await re(t);if(a!==null&&te(a,n)){let s=await se(t,o);return s===null?{mode:"reuse",port:t,identity:a}:{mode:"spawn",port:0,reason:`a urun-openai proxy on port ${t} matches this invocation's identity but failed the serving-surface liveness check (${s}) \u2014 starting a fresh proxy on an ephemeral port instead of pointing the harness at it`}}return{mode:"spawn",port:0}}function Be(e,n=J.homedir()){return X.join(n,".urun","logs",`compat-proxy-${e}.log`)}var S=["log","info","warn","error","debug","trace"];function He(e,n=process,t=console){let o=n.stdout.write,a=n.stderr.write,s=(i,...u)=>e.write(i,...u);n.stdout.write=s,n.stderr.write=s;let r=Object.fromEntries(S.map(i=>[i,t[i]]));for(let i of S)t[i]=(...u)=>{e.write(`${Q(...u)}
2
- `)};return()=>{n.stdout.write=o,n.stderr.write=a;for(let i of S)t[i]=r[i]}}var ie=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function j(e){let n=e?.code;return typeof n=="string"&&ie.has(n)}function Ke(e,n){try{return e.write(n),!0}catch(t){if(j(t))return!1;throw t}}function qe(e){let n={stdout:!1,stderr:!1},t=[];for(let o of["stdout","stderr"]){let a=e.streams[o],s=r=>{j(r)&&(n[o]=!0);let i=r?.code;try{e.note(`${new Date().toISOString()} urun compat: process.${o} died (${String(i??(r instanceof Error?r.message:r))}) \u2014 the terminal is gone; launcher output continues in this log only
3
- `)}catch{}};a.on("error",s),t.push([a,s])}return{isDead:o=>n[o],uninstall:()=>{for(let[o,a]of t)o.off("error",a)}}}function Ye(e){let n=t=>{let o=t instanceof Error?`${t.stack??`${t.name}: ${t.message}`}`:String(t);try{e.appendSync(e.logPath,`${new Date().toISOString()} launcher crashed (in-process proxy): ${o}
4
- `)}catch{}e.restore();try{e.writeStderr(`urun compat: the in-process proxy crashed (${t instanceof Error?t.message:String(t)}) \u2014 terminating the harness (its backend is gone); details: ${e.logPath}
5
- `)}catch(a){try{e.appendSync(e.logPath,`${new Date().toISOString()} urun compat: the crash line could not reach stderr (${String(a?.code??a)}) \u2014 the terminal is gone
6
- `)}catch{}}e.killChild(),e.exit(1)};return e.proc.on("uncaughtException",n),e.proc.on("unhandledRejection",n),()=>{e.proc.off("uncaughtException",n),e.proc.off("unhandledRejection",n)}}function Ve(e,n){let t="pass --port 0 for an OS-assigned ephemeral port";return n?`port ${e} is held by another urun-openai proxy serving app ${n.app} (org ${n.org}, fn ${n.fn}, version ${n.proxy_version}) \u2014 reuse it by pointing your agent at http://127.0.0.1:${e}, or ${t}`:`port ${e} is already in use and does not answer the urun-openai /stats identity probe \u2014 ${t}`}export{xe as a,ve as b,Ie as c,ee as d,Ne as e,$e as f,Ce as g,Te as h,Ge as i,Le as j,je as k,De as l,Me as m,ne as n,C as o,Fe as p,re as q,We as r,Be as s,He as t,Ke as u,qe as v,Ye as w,Ve as x};
@@ -1 +0,0 @@
1
- import{c as p}from"./chunk-YSFSRI3D.js";p();function g(e,n,r,t){let o=e.response??{},i={request_id:r,consumer_id:t,stream:!0,kind:"chat",messages:n};return typeof o.instructions=="string"&&(i.instructions=o.instructions),Array.isArray(o.modalities)&&(i.modalities=o.modalities),typeof o.temperature=="number"&&(i.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(i.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(i.tools=o.tools),i}function _(e,n,r){let t={request_id:n,consumer_id:r,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(t.model=e.model),e.tools!==void 0&&(t.tools=e.tools),e.tool_choice!==void 0&&(t.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(t.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(t.max_output_tokens=e.max_output_tokens),typeof e.top_p=="number"&&(t.top_p=e.top_p),Array.isArray(e.stop)&&e.stop.length>0&&(t.stop=e.stop),typeof e.reasoning_effort=="string"&&(t.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(t.chat_template_kwargs=e.chat_template_kwargs),t}p();function f(e){if(e instanceof Error)return{type:"error",error:{type:"urun_error",code:e.name||null,message:e.message}};if(e&&typeof e=="object"&&e.t==="error"){let n=e,r=n.body??{};return{type:"error",error:{type:"urun_error",code:n.code??null,message:r.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}p();var k="llm-resp";async function*m(e,n){let r=`${k}:${n}`,t=`resp_${n}`;yield{type:"response.created",response:{id:t,status:"in_progress"}};let o=new Map;for await(let i of e.stream(r).messages()){let s=i;if(s.t==="delta"){if(typeof s.delta=="string"&&(yield{type:"response.output_text.delta",item_id:t,delta:s.delta}),typeof s.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:t,delta:s.reasoning}),Array.isArray(s.tool_calls))for(let u of s.tool_calls){let c=typeof u.index=="number"?u.index:0,a=o.get(c)??{};u.id&&(a.id=u.id),u.function?.name&&(a.name=u.function.name),o.set(c,a),yield{type:"response.function_call_arguments.delta",item_id:`fc_${n}_${c}`,tool_index:c,call_id:a.id,name:a.name,delta:u.function?.arguments??""}}}else if(s.t==="response"){yield{type:"response.completed",response:{id:t,status:"completed",...s.body}};return}else if(s.t==="error"){yield f(s);return}}}p();var w="llm",y=class{constructor(n){this.session=n;this.sessionTag=n.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(n){n.session_tag=this.sessionTag,this.session.doc(w).set({requests:{[n.request_id]:{payload:n,consumer_id:n.consumer_id,stream:n.stream}}})}sendResponses(n,r){let t=_(n,r,this.consumerId);return this.write(t),m(this.session,r)}sendResponseCreate(n,r,t){let o=g(n,r,t,this.consumerId);return this.write(o),m(this.session,t)}};export{g as a,_ as b,f as c,m as d,y as e};
@@ -1,44 +0,0 @@
1
- import{b as U,c as le}from"./chunk-DMD5UENK.js";import{a as $e,c as Fe,d as X,e as ye,f as A,g as Be,h as He,i as Je,j as Ue,k as Ge,l as De,m as Ve,n as je,r as We,t as ze,u as Ke,v as Xe,w as Ye,x as Ze}from"./chunk-5NXM4IO3.js";import{c as j}from"./chunk-YSFSRI3D.js";j();import{randomUUID as at}from"crypto";import{WebSocketServer as ut}from"ws";var lt=256,ge=class{snapshots=new Map;store(e,t,s){for(this.snapshots.set(`${e}\0${t}`,s);this.snapshots.size>lt;){let r=this.snapshots.keys().next().value;this.snapshots.delete(r)}}get(e,t){return this.snapshots.get(`${e}\0${t}`)}},ct="local",ce=n=>n.slice(0,120),_e=class{constructor(e,t,s){this.ws=e;this.call=t;this.registry=s;this.clients=t.clients,this.scope=t.scope,e.on("message",r=>this.onMessage(r)),e.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.(),this.detachVideoOut?.(),this.call.release?.().catch(()=>{})})}ws;call;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;detachVideoOut=null;video=null;activityOpen=!1;activeTurn=null;scope;clients;send(e){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(e))}fail(e){if(e instanceof U){this.ws.close(Fe,ce(e.message));return}if(e instanceof le){this.ws.close(ye,ce(e.message));return}if(e instanceof A){this.ws.close(e.closeCode,ce(e.message));return}this.ws.close(X,ce(String(e instanceof Error?e.message:e)))}onMessage(e){try{let t=Be(typeof e=="string"?e:Buffer.concat(Array.isArray(e)?e:[e]).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 s=this.config;if(t.kind==="clientContent"){let{items:r,turnComplete:i}=je(t.clientContent);this.transcript.push(...r),i&&this.enqueue(()=>this.runTurn());return}if(t.kind==="realtimeInput"){let r=ze(t.realtimeInput);if(r.kind==="activityStart"||r.kind==="activityEnd"){this.onActivity(r.kind,s);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:We);return}this.transcript.push(...Ke(t.toolResponse)),this.enqueue(()=>this.runTurn())}catch(t){this.fail(t)}}async handleSetup(e){if(this.config)throw new A("setup may only be sent once, as the first client message");let t=Ge(e),s=t.sessionResumption.handle;if(s!==null){let i=this.registry.get(this.scope,s);if(!i)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",ye);if(i.model!==t.model)throw new A(`sessionResumption.handle was issued for model "${i.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");if(this.call.clientsForResume!==void 0&&(this.clients=await this.call.clientsForResume(s)),this.proxyHandle=await this.clients.sessionHandle(t.model),this.proxyHandle!==i.proxyHandle)throw new A(`sessionResumption.handle named pooled session "${i.proxyHandle}" but the re-attach landed on "${this.proxyHandle}" \u2014 the conversation's native session changed and the snapshot cannot be adopted; reconnect without a handle to start a new session`,ye);this.unsubscribeEnd=await this.clients.onSessionEnd(i.proxyHandle,o=>this.enqueue(()=>this.handleUpstreamEnd(o))),this.transcript=[...i.transcript]}else this.proxyHandle=await this.clients.sessionHandle(t.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,i=>this.enqueue(()=>this.handleUpstreamEnd(i))),this.transcript.push(...t.systemItems);this.config=t;let r={};if(t.videoOut){let i=this.clients.openVideoOut;if(i){let o=await i.call(this.clients,t.model),l=o.onOutputFrame(p=>this.send(Ue(p))),a=o.onLaneError(p=>this.fail(p));this.detachVideoOut=()=>{l(),a()},r[He]=Je(o.codec)}}this.send({setupComplete:r}),t.audioModality&&this.ensureAudioLane(),t.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let e=this.config;if(!e)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)",X);return this.audio=(async()=>{let s=await t.call(this.clients,e.model);return this.detachAudio=s.onOutputAudio(r=>this.send(Ye(r))),s})(),this.audio.catch(s=>this.fail(s)),this.audio}appendAudio(e){this.ensureAudioLane().then(t=>t.appendInputAudio(e)).catch(t=>this.fail(t))}ensureVideoLane(){if(this.video)return this.video;let e=this.config;if(!e)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)",X);return this.video=Promise.resolve(t.call(this.clients,e.model)),this.video.catch(s=>this.fail(s)),this.video}appendVideoFrame(e){this.ensureVideoLane().then(t=>t.sendInputFrame(e)).catch(t=>this.fail(t))}onActivity(e,t){if(!t.activity.manualActivity)throw new A(`realtimeInput.${e} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(e==="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(e){this.queue=this.queue.then(e,()=>{}),this.queue=this.queue.catch(t=>this.fail(t))}async mintResumptionUpdate(){let e=this.config;if(!e?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let t=await this.clients.sessionHandle(e.model);this.proxyHandle=t;let s=at();this.registry.store(this.scope,s,{model:e.model,proxyHandle:t,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(s),this.send(De(s))}async handleUpstreamEnd(e){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,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(Ve(e.timeLeftMs)),this.ws.close(X,ce(`upstream session ended: ${e.reason}`))}async runTurn(){let e=this.config;if(!e||this.ws.readyState!==this.ws.OPEN)return;let t={interrupted:!1};this.activeTurn=t;try{let s=await this.clients.createResponse({model:e.model,input:[...this.transcript],stream:!0,tools:e.tools,temperature:e.temperature,max_output_tokens:e.maxOutputTokens}),r,i=!1;for await(let a of s){if(t.interrupted)break;let p=a;if(p.type==="error")throw new A(`upstream error: ${JSON.stringify(p)}`,X);if(p.type==="response.completed"){r=p.response,i=!0;continue}let m=Xe(p);m&&this.send(m)}if(t.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!i)throw new A("upstream produced no response.completed event",X);let{messages:o,assistantItems:l}=Ze(r);this.transcript.push(...l);for(let a of o)this.send(a);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},Y=class extends Error{constructor(t,s){super(s);this.status=t;this.name="UpgradeAuthError"}status};function dt(n,e,t){let s=new Set;(a=>{if(typeof a=="string")a!==""&&s.add(a);else if(Array.isArray(a))for(let p of a)p!==""&&s.add(p)})(n.headers["x-goog-api-key"]);let i=n.headers.authorization;typeof i=="string"&&i.startsWith("Bearer ")&&i.length>7&&s.add(i.slice(7));let o=e.searchParams.get("key")??void 0;if(o!==void 0&&o!==""&&s.add(o),s.size>1)throw new Y(401,`contradictory credentials: ${s.size} distinct keys presented across the x-goog-api-key/Authorization headers and the ?key= query parameter \u2014 present exactly one`);if(t&&o!==void 0&&o!=="")throw new Y(401,"the ?key= query parameter is not accepted on the hosted endpoint \u2014 send the org API key in the x-goog-api-key header or as Authorization: Bearer");let l;for(let a of s)l=a;if(l===void 0&&t)throw new Y(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return l??""}function Qe(n,e){let t=e.authenticate!==void 0;if(t&&(e.clients!==void 0||e.apiKey!==void 0))throw new TypeError("attachGeminiLive: authenticate (hosted per-request resolution) is mutually exclusive with a fixed clients/apiKey lane");if(!t&&e.clients===void 0)throw new TypeError("attachGeminiLive: pass fixed `clients` (local lane) or `authenticate` (hosted lane)");let s=new ut({noServer:!0}),r=new ge;n.on("upgrade",(o,l,a)=>{let p=new URL(o.url??"/","http://localhost");p.pathname===$e&&(l.on("error",()=>{}),(async()=>{let m=dt(o,p,t),c;if(e.authenticate)c=await e.authenticate(m,o);else{if(e.apiKey!==void 0&&m!==e.apiKey)throw new Y(401,"invalid local proxy api key");c={clients:e.clients,scope:ct}}s.handleUpgrade(o,l,a,d=>{new _e(d,c,r)})})().catch(m=>{let c=502;(m instanceof Y||typeof m=="object"&&m!==null&&"status"in m&&typeof m.status=="number")&&(c=m.status);let d=c===401?"Unauthorized":c===403?"Forbidden":c===502?"Bad Gateway":"Internal Server Error",f=m instanceof Error?m.message:String(m);l.destroyed||(l.write(`HTTP/1.1 ${c} ${d}\r
2
- Connection: close\r
3
- \r
4
- ${f}\r
5
- `),l.destroy())}))});let i=null;return()=>(i??=(async()=>{for(let o of s.clients)o.close(1001,"server closing");await new Promise((o,l)=>s.close(a=>a===void 0?o():l(a)))})(),i)}function Dt(n,e,t=new ge){new _e(n,e,t)}j();import{createServer as At}from"http";j();import{WebSocketServer as _t,WebSocket as fe}from"ws";j();function re(n){return typeof n.model!="string"||!n.model.trim()?"(default)":n.model.trim().slice(0,128)}var Z=class n{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(e,t,s){this.requests[e]=(this.requests[e]??0)+1,this.localInBytes+=t,this.naiveInBytes+=s}modelEntry(e){let t=this.models[e];if(t)return t;let s=Object.keys(this.models).length>=256?"(other)":e;return this.models[s]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,t){let s=this,r=performance.now(),i=null,o=0;return(async function*(){for await(let a of e)a.type==="response.output_text.delta"&&typeof a.delta=="string"&&(i===null&&(i=performance.now(),s.ttftMs.push(i-r),s.ttftMs.length>512&&s.ttftMs.shift()),o+=1,s.tokensOut+=1,s.charsOut+=a.delta.length,t!==void 0&&(s.modelEntry(t).tokens_out+=1)),yield a;let l=(performance.now()-(i??r))/1e3;o>0&&l>0&&(s.tokRates.push(o/l),s.tokRates.length>512&&s.tokRates.shift())})()}static p50(e){if(!e.length)return null;let t=[...e].sort((s,r)=>s-r);return t[Math.floor(t.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((t,s)=>t+s,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:n.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:n.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:e},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}}}},pt=256;function ft(n){if(typeof n=="string")return[{role:"user",content:n}];if(Array.isArray(n))return n;throw new Error("responses input must be a string or an array of messages/items")}var Q=class extends Error{},se=class{conversations=new Map;thread(e,t){let s=ft(t);if(e==null)return s;let r=this.conversations.get(String(e));if(!r)throw new Q(`unknown previous_response_id ${String(e)} (proxy restarts drop stored responses)`);return[...r,...s]}remember(e,t,s,r=!0){this.conversations.set(e,[...t,...s]);for(let i of this.conversations.keys()){if(this.conversations.size<=pt)break;this.conversations.delete(i)}}forget(e){this.conversations.delete(e)}},et=new WeakMap;function de(n){let e=et.get(n);return e||(e=new se,et.set(n,e)),e}function oe(n){let e=n?.output;return Array.isArray(e)?e.filter(t=>t.type==="function_call"):[]}function z(n){return n.find(e=>e.type==="response.completed")?.response??null}function ie(n){return n.type!=="response.function_call_arguments.delta"||typeof n.tool_index!="number"?null:{tool_index:n.tool_index,call_id:typeof n.call_id=="string"?n.call_id:void 0,name:typeof n.name=="string"?n.name:void 0,delta:typeof n.delta=="string"?n.delta:""}}var W=class{calls=new Map;add(e){let t=this.calls.get(e.tool_index),s=!t;return t||(t={args:""},this.calls.set(e.tool_index,t)),e.call_id&&(t.id=e.call_id),e.name&&(t.name=e.name),t.args+=e.delta,{call:t,isNew:s}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([e,t])=>({type:"function_call",call_id:t.id??`call_${e}`,name:t.name??"",arguments:t.args}))}},mt=["<tool_call>","<function="];function ht(n){return mt.find(e=>n.includes(e))}function ee(n,e){return`serving row emitted a prose tool call (literal ${JSON.stringify(e)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${n}" (see catalog parser_defaults)`}function te(n,e,t){if(!(!n||e>0))return ht(t)}function be(n,e){let t=e?.output_text;return typeof t=="string"?t:n.filter(s=>s.type==="response.output_text.delta"&&typeof s.delta=="string").map(s=>s.delta).join("")}function yt(n){let e=n?.output;if(!Array.isArray(e))return[];let t=[];for(let s of e){if(s===null||typeof s!="object")continue;let r=s;r.type==="message"?t.push({role:"assistant",content:r.content??""}):r.type==="function_call"&&t.push({type:"function_call",call_id:r.call_id,name:r.name,arguments:r.arguments})}return t}function Ee(n,e){let t=yt(n);return t.length>0?t:e?[{role:"assistant",content:e}]:[]}var M=class extends Error{constructor(t,s,r=400,i){super(t);this.code=s;this.status=r;this.param=i}code;status;param};function ae(n,e){try{return n.thread(e.previous_response_id,e.input)}catch(t){throw t instanceof Q?new M(t.message,"previous_response_not_found",400,"previous_response_id"):new M(String(t instanceof Error?t.message:t),"invalid_input")}}async function Pe(n,e,t,s){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new M("instructions must be a string","invalid_instructions");let r=typeof e.instructions=="string"?e.instructions:void 0;return n.createResponse({model:e.model,input:t,stream:s,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,top_p:e.top_p,stop:e.stop,...r!==void 0?{instructions:r}:{},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}:{}})}async function pe(n){let{body:e,clients:t,store:s,stats:r}=n,i=ae(s,e);e.previous_response_id!=null&&(r.naiveInBytes+=Buffer.byteLength(JSON.stringify(i))-Buffer.byteLength(JSON.stringify(e.input)));let o=`resp_${Math.random().toString(36).slice(2,14)}`,l=n.remember??e.store!==!1,a=e.store!==!1,p=re(e);r.model(p);let m=Array.isArray(e.tools)&&e.tools.length>0,c=await Pe(t,e,i,!0);return{responseId:o,events:gt({events:c,conversation:i,responseId:o,modelKey:p,requestHadTools:m,remember:l,persist:a,store:s,stats:r})}}async function*gt(n){let{events:e,conversation:t,responseId:s,modelKey:r,requestHadTools:i,remember:o,persist:l,store:a,stats:p}=n,m=[],c=g=>g.response&&typeof g.response=="object"?{...g,response:{...g.response,id:s}}:g,d=`item_${s.slice(5)}`,f=`rs_${s.slice(5)}`,y=!1,_=!1,h=0;function*x(){y||(y=!0,yield{type:"response.created",response:{id:s,object:"response",status:"in_progress"}})}let b=!1,C=0,T="";function*B(){b&&(b=!1,yield{type:"response.reasoning_text.done",item_id:f,output_index:C,content_index:0,text:T},yield{type:"response.output_item.done",output_index:C,item:{id:f,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:T}],status:"completed"}})}let u=!1,w=0,N="";function*L(g){u&&(u=!1,yield{type:"response.output_text.done",item_id:d,output_index:w,content_index:0,text:N},yield{type:"response.content_part.done",item_id:d,output_index:w,content_index:0,part:{type:"output_text",text:N}},yield{type:"response.output_item.done",output_index:w,item:{id:d,type:"message",role:"assistant",status:g,content:[{type:"output_text",text:N}]}})}let q=new W,R=null,H=-1;function*O(g){if(!R)return;let v=R;R=null,yield{type:"response.function_call_arguments.done",item_id:v.fcId,output_index:v.outputIndex,arguments:v.args},yield{type:"response.output_item.done",output_index:v.outputIndex,item:{id:v.fcId,type:"function_call",call_id:v.callId??v.fcId,name:v.name??"",arguments:v.args,status:g}}}let E=!1;for await(let g of p.track(e,r)){m.push(g);let v=String(g.type??"");if(v==="response.created"||v==="response.in_progress"){y=!0,yield c(g);continue}if(v==="response.output_item.added"){_=!0,yield c(g);continue}if(v==="response.reasoning_text.delta"&&!_){yield*x(),b||(b=!0,C=h++,yield{type:"response.output_item.added",output_index:C,item:{id:f,type:"reasoning",summary:[],content:[],status:"in_progress"}}),T+=String(g.delta??""),yield{type:"response.reasoning_text.delta",item_id:f,output_index:C,content_index:0,delta:g.delta};continue}if(v==="response.output_text.delta"&&!_){yield*x(),yield*B(),u||(u=!0,w=h++,yield{type:"response.output_item.added",output_index:w,item:{id:d,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:d,output_index:w,content_index:0,part:{type:"output_text",text:""}}),N+=String(g.delta??""),yield{type:v,item_id:d,output_index:w,content_index:0,delta:g.delta};continue}let S=_?null:ie(g);if(S){if(yield*x(),yield*B(),yield*L("completed"),q.add(S),S.tool_index!==H){yield*O("completed"),H=S.tool_index;let D=h++;R={fcId:`fc_${s.slice(5)}_${S.tool_index}`,outputIndex:D,callId:S.call_id,name:S.name,args:""},yield{type:"response.output_item.added",output_index:D,item:{id:R.fcId,type:"function_call",call_id:R.callId??R.fcId,name:R.name??"",arguments:"",status:"in_progress"}}}R&&(S.call_id&&(R.callId=S.call_id),S.name&&(R.name=S.name),R.args+=S.delta,yield{type:"response.function_call_arguments.delta",item_id:R.fcId,output_index:R.outputIndex,delta:S.delta});continue}if(v==="response.completed"&&!_){let D=oe(g.response),he=te(i,q.size+D.length,N);if(he){E=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:ee(r,he)}};break}if(yield*B(),yield*L("completed"),yield*O("completed"),q.size===0)for(let V of D){let J=h++,ne=V.id??V.call_id??`fc_${J}`,K=V.arguments??"",k={id:ne,type:"function_call",call_id:V.call_id??ne,name:V.name??""};yield{type:"response.output_item.added",output_index:J,item:{...k,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:ne,output_index:J,delta:K},yield{type:"response.function_call_arguments.done",item_id:ne,output_index:J,arguments:K},yield{type:"response.output_item.done",output_index:J,item:{...k,arguments:K,status:"completed"}}}yield c(g);continue}yield c(g)}if(yield*B(),yield*L("incomplete"),yield*O("incomplete"),o&&!E){let g=z(m);a.remember(s,t,Ee(g,be(m,g)),l)}}var wt="/v1/responses",vt=64*1024*1024,kt=8*1024*1024,xt=16,tt=32,nt=3600*1e3,Rt=/^[A-Za-z0-9._-]{1,256}$/;function P(n,e,t){return{type:"error",status:n,...t!==void 0?{stream_id:t}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var St=P(400,{type:"invalid_request_error",code:"invalid_stream_id",message:"The 'stream_id' field must be a non-empty string with at most 256 characters and may only contain letters, numbers, underscores, hyphens, and periods.",param:"stream_id"});function Te(){let n,e;return{promise:new Promise((s,r)=>{n=s,e=r}),resolve:n,reject:e}}async function Ot(n){for(;n.bufferedAmount>kt&&n.readyState===fe.OPEN;){let{promise:e,resolve:t}=Te();setTimeout(t,5),await e}}var Ie=class extends se{constructor(t){super();this.shared=t}shared;thread(t,s){if(t==null)return super.thread(t,s);try{return super.thread(t,s)}catch(r){if(!(r instanceof Q))throw r;return this.shared.thread(t,s)}}remember(t,s,r,i=!0){i?this.shared.remember(t,s,r):super.remember(t,s,r)}forget(t){super.forget(t),this.shared.forget(t)}},Ce=class{constructor(e,t,s,r){this.ws=e;this.clients=t;this.stats=s;this.statusOf=r;this.store=new Ie(de(t)),e.on("message",(o,l)=>this.onMessage(o,l)),e.on("close",()=>this.dispose()),e.on("error",()=>this.dispose()),setTimeout(()=>{this.closed||(this.sendFrame(P(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${nt/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},nt).unref?.()}ws;clients;stats;statusOf;store;lanes=new Map;laneOfResponse=new Map;activeTurns=new Set;active=0;waiters=[];closed=!1;onMessage(e,t){if(this.closed)return;if(t){this.sendFrame(P(400,{type:"invalid_request_error",code:"binary_frame",message:"Responses WebSocket frames must be JSON text"})),this.ws.close(1003,"binary frames are not supported");return}let s;try{s=JSON.parse(e.toString())}catch{this.sendFrame(P(400,{type:"invalid_request_error",code:"invalid_json",message:"frame is not valid JSON"}));return}if(s===null||typeof s!="object"||Array.isArray(s)){this.sendFrame(P(400,{type:"invalid_request_error",code:"invalid_event",message:"frame must be a JSON object"}));return}let r=s;if(r.type!=="response.create"){this.sendFrame(P(400,{type:"invalid_request_error",code:"unknown_event",message:`unknown client event type ${JSON.stringify(r.type??null)}; only "response.create" is supported`}));return}if("stream"in r){this.sendFrame(P(400,{type:"invalid_request_error",code:"stream_is_implicit",message:"'stream' is implicit over the Responses WebSocket and must not be sent",param:"stream"}));return}if("background"in r){this.sendFrame(P(400,{type:"invalid_request_error",code:"background_not_supported",message:"'background' is not supported over the Responses WebSocket",param:"background"}));return}let i;if(r.stream_id!==void 0){if(typeof r.stream_id!="string"||!Rt.test(r.stream_id)){this.sendFrame(St);return}if(i=r.stream_id,!this.lanes.has(i)&&this.namedLaneCount()>=tt){this.sendFrame(P(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${tt}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},i));return}}let o=r.generate===!1?()=>this.runWarmup(r,i):()=>this.runTurn(r,i),l=i??"",a=this.lanes.get(l)??Promise.resolve();this.lanes.set(l,a.then(o).catch(()=>{}))}namedLaneCount(){return this.lanes.has("")?this.lanes.size-1:this.lanes.size}async runTurn(e,t){if(!this.closed){await this.acquire();try{let s=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",s,s);let r;try{r=await pe({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0})}catch(o){this.failTurn(o,t,e);return}this.laneOfResponse.set(r.responseId,t??null);let i=r.events;this.activeTurns.add(i);try{for await(let o of i){if(this.closed||this.ws.readyState!==fe.OPEN)break;this.send(o,t),await Ot(this.ws)}}finally{this.activeTurns.delete(i)}}catch(s){this.failTurn(s,t,e)}finally{this.release()}}}runWarmup(e,t){if(this.closed)return;let s=`resp_${Math.random().toString(36).slice(2,14)}`;try{let r=ae(this.store,{...e,input:e.input??(e.previous_response_id!=null?[]:void 0)});this.store.remember(s,r,[],e.store!==!1),this.laneOfResponse.set(s,t??null),this.send({type:"response.created",response:{id:s,object:"response",status:"in_progress"}},t)}catch(r){this.failTurn(r,t,e)}}failTurn(e,t,s){this.sendFrame(this.errorFrameOf(e,t));let r=s.previous_response_id;typeof r=="string"&&this.laneOfResponse.get(r)===(t??null)&&(this.store.forget(r),this.laneOfResponse.delete(r))}errorFrameOf(e,t){let s=String(e instanceof Error?e.message:e);if(e instanceof M)return P(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},t);if(e instanceof U)return P(404,{type:"invalid_request_error",code:"model_not_found",message:s},t);if(e instanceof le)return P(503,{type:"upstream_error",code:"session_gone",message:s},t);let r=this.statusOf?.(e)??null;return r?P(r.status,{type:r.openaiType,message:s},t):P(500,{type:"upstream_error",message:s},t)}async acquire(){for(;this.active>=xt;){let{promise:e,resolve:t}=Te();this.waiters.push(t),await e}this.active++}release(){this.active--,this.waiters.shift()?.()}dispose(){if(!this.closed){this.closed=!0;for(let e of[...this.activeTurns])e.return(void 0);this.activeTurns.clear(),this.lanes.clear(),this.laneOfResponse.clear();for(let e of this.waiters.splice(0))e()}}send(e,t){if(this.closed||this.ws.readyState!==fe.OPEN)return;let s=t===void 0?e:{...e,stream_id:t};this.ws.send(JSON.stringify(s))}sendFrame(e){this.closed||this.ws.readyState!==fe.OPEN||this.ws.send(JSON.stringify(e))}};function we(n,e,t){let s=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",r=`${t}\r
6
- `;n.end(`HTTP/1.1 ${e} ${s}\r
7
- Connection: close\r
8
- Content-Type: text/plain; charset=utf-8\r
9
- Content-Length: ${Buffer.byteLength(r)}\r
10
- \r
11
- ${r}`)}function st(n,e){let t=new _t({noServer:!0,maxPayload:vt}),s=new Z,r=new Set,i=new Set,o=new Set,l=typeof e.clients=="function"?e.clients:()=>e.clients,a=!1,p=(m,c,d)=>{let f;try{f=new URL(m.url??"/","http://localhost")}catch{we(c,400,"malformed upgrade URL");return}if(c.on("error",()=>c.destroy()),c.on("close",()=>o.delete(c)),f.pathname!==wt){o.add(c);return}i.add(c);let y=!1;(async()=>{if(e.apiKey&&(m.headers.authorization??"")!==`Bearer ${e.apiKey}`){we(c,401,"invalid local proxy api key");return}let _;try{_=await l(m)}catch(h){let x=e.statusOf?.(h)??null;we(c,x?.status??500,"upstream resolver rejected the connection");return}a||c.destroyed||(y=!0,t.handleUpgrade(m,c,d,h=>{i.delete(c),r.add(h),h.on("close",()=>r.delete(h));try{new Ce(h,_,s,e.statusOf)}catch{r.delete(h),h.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!y&&!c.destroyed&&we(c,500,"responses websocket upgrade failed")}).finally(()=>i.delete(c))};return n.on("upgrade",p),{stats:s,close:()=>{let{promise:m,resolve:c}=Te();if(a)return c(),m;a=!0,n.off("upgrade",p);for(let f of i)f.destroy();i.clear();for(let f of o)f.destroy();o.clear();for(let f of r)f.close(1001,"server shutting down");let d=setTimeout(()=>{for(let f of r)f.readyState!==fe.CLOSED&&f.terminate()},2e3);return d.unref?.(),t.close(()=>{clearTimeout(d),c()}),m}}}function Ne(n){n.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function F(n,e,t){n.writeHead(e,{"content-type":"application/json"}),n.end(JSON.stringify(t))}function $(n,e,t,s="invalid_request_error"){F(n,e,{error:{message:t,type:s}})}function G(n,e,t,s){F(n,e,{type:"error",error:{type:t,message:s}})}function ve(n,e,t){if(e==="anthropic"){F(n,404,{type:"error",error:{type:"not_found_error",message:t.message}});return}F(n,404,{error:{message:t.message,type:"invalid_request_error",code:"model_not_found"}})}var rt=64*1024*1024,ke=class extends Error{};async function bt(n){let e=[],t=0;for await(let r of n){if(t+=r.length,t>rt)throw new ke(`request body exceeds ${rt} bytes`);e.push(r)}let s=Buffer.concat(e).toString("utf8");return s?JSON.parse(s):{}}function ue(n,e,t,s){return{id:n,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:t,finish_reason:s}]}}function xe(n){let e=n?.usage;if(!e||typeof e!="object")return null;let t=o=>typeof o=="number"&&Number.isFinite(o)&&o>=0?o:0,s=t(e.input_tokens),r=t(e.output_tokens);if(s===0&&r===0)return null;let i=t(e.total_tokens)||s+r;return{input_tokens:s,output_tokens:r,total_tokens:i}}function ot(n){let e=xe(n);return e?{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function it(n){let e=n?.output;if(!Array.isArray(e))return"";let t="";for(let s of e)if(!(s.type!=="reasoning"||!Array.isArray(s.content)))for(let r of s.content)r.type==="reasoning_text"&&(t+=String(r.text??""));return t}function Et(n){if(Array.isArray(n))return n.map(e=>{let t=e.function;return!t||typeof t!="object"?e:{type:"function",name:t.name,description:t.description,parameters:t.parameters,strict:t.strict}})}var Re=class extends Error{};function Pt(n,e){return Array.isArray(e)?e.map(t=>{let s=t;if(s.type==="text")return{type:n==="assistant"?"output_text":"input_text",text:s.text};if(s.type==="image_url"){let r=s.image_url??{};return{type:"input_image",image_url:r.url,detail:r.detail}}throw new Re(`unsupported chat content part type "${String(s.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function It(n){let e=[];for(let t of n){if(t.role==="tool"){e.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&&e.push({role:"assistant",content:t.content});for(let s of t.tool_calls){let r=s.function??{};e.push({type:"function_call",call_id:s.id,name:r.name,arguments:r.arguments})}continue}e.push({...t,content:Pt(t.role,t.content)})}return e}function Le(n){return oe(n).map((e,t)=>({index:t,id:e.call_id??e.id??`call_${t}`,type:"function",function:{name:e.name??"",arguments:e.arguments??"{}"}}))}async function Ct(n,e,t,s,r){if(n.stream===!0){let h;try{h=await pe({body:n,clients:e,store:t,stats:r})}catch(x){if(x instanceof U){ve(s,"openai",x);return}if(x instanceof M){$(s,x.status,x.message);return}throw x}Ne(s);for await(let x of h.events)s.write(`event: ${String(x.type??"message")}
12
- data: ${JSON.stringify(x)}
13
-
14
- `);s.write(`data: [DONE]
15
-
16
- `),s.end();return}let o;try{o=ae(t,n)}catch(h){if(h instanceof M){$(s,h.status,h.message);return}throw h}n.previous_response_id!=null&&(r.naiveInBytes+=Buffer.byteLength(JSON.stringify(o))-Buffer.byteLength(JSON.stringify(n.input)));let l=`resp_${Math.random().toString(36).slice(2,14)}`,a=n.store!==!1,p=re(n);r.model(p);let m=Array.isArray(n.tools)&&n.tools.length>0,c;try{c=await Pe(e,n,o,!1)}catch(h){if(h instanceof U){ve(s,"openai",h);return}if(h instanceof M){$(s,h.status,h.message);return}throw h}let d=[],f=null;for await(let h of r.track(c,p)){if(d.push(h),h.type==="error"){$(s,502,JSON.stringify(h),"upstream_error");return}h.type==="response.completed"&&(f=h.response)}if(f==null){$(s,502,"upstream produced no response.completed event","upstream_error");return}let y=be(d,f),_=te(m,oe(f).length,y);if(_){$(s,502,ee(p,_),"upstream_error");return}a&&t.remember(l,o,Ee(f,y),a),F(s,200,{...f,id:l})}async function Tt(n,e,t,s){let r=n.messages;if(!Array.isArray(r)){$(t,400,"chat/completions requires a messages array");return}let i=String(n.model??"urun"),o=`chatcmpl-${Math.random().toString(36).slice(2,14)}`,l;try{l=It(r)}catch(u){if(u instanceof Re){$(t,400,u.message);return}throw u}let a=re(n);s.model(a);let p=Array.isArray(n.tools)&&n.tools.length>0,m;try{m=await e.createResponse({model:n.model,input:l,stream:!0,tools:Et(n.tools),tool_choice:n.tool_choice,temperature:n.temperature,top_p:n.top_p,stop:n.stop,max_output_tokens:n.max_completion_tokens??n.max_tokens,...n.reasoning_effort!==void 0?{reasoning_effort:n.reasoning_effort}:{},...n.chat_template_kwargs!==void 0?{chat_template_kwargs:n.chat_template_kwargs}:{}})}catch(u){if(u instanceof U){ve(t,"openai",u);return}throw u}if(n.stream===!0){Ne(t);let u=[],w=new W,N="";t.write(`data: ${JSON.stringify(ue(o,i,{role:"assistant"},null))}
17
-
18
- `);for await(let O of s.track(m,a)){u.push(O);let E=ie(O);if(O.type==="response.output_text.delta"&&typeof O.delta=="string")N+=O.delta,t.write(`data: ${JSON.stringify(ue(o,i,{content:O.delta},null))}
19
-
20
- `);else if(O.type==="response.reasoning_text.delta"&&typeof O.delta=="string")t.write(`data: ${JSON.stringify(ue(o,i,{reasoning_content:O.delta},null))}
21
-
22
- `);else if(E){let{call:g,isNew:v}=w.add(E),S=v?{index:E.tool_index,id:g.id??`call_${E.tool_index}`,type:"function",function:{name:g.name??"",arguments:E.delta}}:{index:E.tool_index,function:{arguments:E.delta}};t.write(`data: ${JSON.stringify(ue(o,i,{tool_calls:[S]},null))}
23
-
24
- `)}else if(O.type==="error"){t.write(`data: ${JSON.stringify({error:O})}
25
-
26
- `),t.end();return}}let L=Le(z(u)),q=te(p,w.size+L.length,N);if(q){t.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:ee(a,q)}})}
27
-
28
- `),t.end();return}w.size===0&&L.length>0&&t.write(`data: ${JSON.stringify(ue(o,i,{tool_calls:L},null))}
29
-
30
- `);let R=w.size>0||L.length>0;t.write(`data: ${JSON.stringify(ue(o,i,{},R?"tool_calls":"stop"))}
31
-
32
- `),xe(z(u))&&t.write(`data: ${JSON.stringify({id:o,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:i,choices:[],usage:ot(z(u))})}
33
-
34
- `),t.write(`data: [DONE]
35
-
36
- `),t.end();return}let c="",d="",f=[],y=new W;for await(let u of s.track(m,a)){f.push(u),u.type==="response.output_text.delta"&&typeof u.delta=="string"&&(c+=u.delta),u.type==="response.reasoning_text.delta"&&typeof u.delta=="string"&&(d+=u.delta);let w=ie(u);if(w&&y.add(w),u.type==="error"){$(t,502,JSON.stringify(u),"upstream_error");return}}let _=z(f),h=Le(_),x=h.length>0?h:Le({output:y.items()}),b=te(p,x.length,c);if(b){$(t,502,ee(a,b),"upstream_error");return}let C=x.map(({index:u,...w})=>w),T={role:"assistant",content:c||null},B=d||it(_);B&&(T.reasoning_content=B),C.length>0&&(T.tool_calls=C),F(t,200,{id:o,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:i,choices:[{index:0,message:T,finish_reason:C.length>0?"tool_calls":"stop"}],usage:ot(_)})}function Lt(n){return typeof n=="string"?n:Array.isArray(n)?n.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var me=class extends Error{};function Se(n){return new me(`unsupported anthropic content block type "${n}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function Mt(n){if(Array.isArray(n))return n.map(e=>{let t=e;return{type:"function",name:t.name,description:t.description,parameters:t.input_schema}})}function Nt(n){if(n==null)return;let e=n.type;if(e==="auto")return"auto";if(e==="none")return"none";if(e==="any")return"required";if(e==="tool")return{type:"function",name:n.name};throw new me(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function qt(n){if(typeof n=="string")return n;if(n==null)return"";if(!Array.isArray(n))throw Se(typeof n);let e="";for(let t of n){let s=String(t.type??"");if(s!=="text")throw Se(`tool_result > ${s}`);e+=String(t.text??"")}return e}function $t(n){let{role:e,content:t}=n;if(typeof t=="string")return[{role:e,content:t}];if(t==null)return[];if(!Array.isArray(t))throw Se(typeof t);let s=[],r="",i=()=>{r&&(s.push({role:e,content:r}),r="")};for(let o of t){let l=String(o.type??"");if(l==="text")r+=String(o.text??"");else if(l==="tool_use")i(),s.push({type:"function_call",call_id:o.id,name:o.name,arguments:JSON.stringify(o.input??{})});else if(l==="tool_result")i(),s.push({type:"function_call_output",call_id:o.tool_use_id,output:qt(o.content)});else{if(l==="thinking"||l==="redacted_thinking")continue;throw Se(l)}}return i(),s}function Me(n){return oe(n).map((e,t)=>{let s;try{s=JSON.parse(e.arguments||"{}")}catch{throw new Error(`upstream function_call "${e.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:e.call_id??e.id??`toolu_${t}`,name:e.name??"",input:s}})}async function Ft(n,e,t,s){let r=n.messages;if(!Array.isArray(r)){G(t,400,"invalid_request_error","messages requires a messages array");return}if(typeof n.max_tokens!="number"||!Number.isInteger(n.max_tokens)||n.max_tokens<1){G(t,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(n.top_k!==void 0){G(t,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(n.stop_sequences!==void 0&&(!Array.isArray(n.stop_sequences)||n.stop_sequences.some(u=>typeof u!="string"))){G(t,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(n.model??"urun"),o=`msg_${Math.random().toString(36).slice(2,14)}`,l=[],a;try{n.system&&l.push({role:"system",content:Lt(n.system)});for(let u of r)l.push(...$t(u));a=Nt(n.tool_choice)}catch(u){if(u instanceof me){G(t,400,"invalid_request_error",u.message);return}throw u}let p=re(n);s.model(p);let m=Array.isArray(n.tools)&&n.tools.length>0,c;try{c=await e.createResponse({model:n.model,input:l,stream:!0,tools:Mt(n.tools),tool_choice:a,temperature:n.temperature,max_output_tokens:n.max_tokens,top_p:n.top_p,stop:n.stop_sequences})}catch(u){if(u instanceof U){ve(t,"anthropic",u);return}throw u}let d=(u,w)=>{t.destroyed||t.write(`event: ${u}
37
- data: ${JSON.stringify({type:u,...w})}
38
-
39
- `)};if(n.stream===!0){let u=s.track(c,p);Ne(t),d("message_start",{message:{id:o,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let w=0,N="",L=0,q="none",R=()=>q,H=-1,O=-1,E=new W,g=[],v=()=>{q!=="none"&&(q="none",d("content_block_stop",{index:H}))},S=(k,I)=>{v(),q=k,H=L++,d("content_block_start",{index:H,content_block:I})};for await(let k of u){if(t.destroyed)break;g.push(k);let I=ie(k);if(k.type==="response.reasoning_text.delta"&&typeof k.delta=="string")R()!=="thinking"&&S("thinking",{type:"thinking",thinking:""}),d("content_block_delta",{index:H,delta:{type:"thinking_delta",thinking:k.delta}});else if(k.type==="response.output_text.delta"&&typeof k.delta=="string")R()!=="text"&&S("text",{type:"text",text:""}),w+=1,N+=k.delta,d("content_block_delta",{index:H,delta:{type:"text_delta",text:k.delta}});else if(I){let{call:qe}=E.add(I);(R()!=="tool"||I.tool_index!==O)&&(O=I.tool_index,S("tool",{type:"tool_use",id:qe.id??`call_${I.tool_index}`,name:qe.name??"",input:{}})),I.delta&&d("content_block_delta",{index:H,delta:{type:"input_json_delta",partial_json:I.delta}})}else if(k.type==="error"){d("error",{error:{type:"api_error",message:JSON.stringify(k)}}),t.end();return}}if(t.destroyed)return;let D=z(g),he=oe(D),V=te(m,E.size+he.length,N);if(V){d("error",{error:{type:"api_error",message:ee(p,V)}}),t.end();return}v();let J=[];if(E.size===0)try{J=Me(D)}catch(k){d("error",{error:{type:"api_error",message:String(k instanceof Error?k.message:k)}}),t.end();return}L===0&&J.length===0&&(S("text",{type:"text",text:""}),v()),J.forEach(k=>{let I=L++;d("content_block_start",{index:I,content_block:{...k,input:{}}}),d("content_block_delta",{index:I,delta:{type:"input_json_delta",partial_json:JSON.stringify(k.input??{})}}),d("content_block_stop",{index:I})});let ne=E.size>0||J.length>0,K=xe(D);d("message_delta",{delta:{stop_reason:ne?"tool_use":"end_turn"},usage:K?{input_tokens:K.input_tokens,output_tokens:K.output_tokens}:{output_tokens:w}}),d("message_stop",{}),t.end();return}let f="",y="",_=[],h=new W;for await(let u of s.track(c,p)){_.push(u),u.type==="response.output_text.delta"&&typeof u.delta=="string"&&(f+=u.delta),u.type==="response.reasoning_text.delta"&&typeof u.delta=="string"&&(y+=u.delta);let w=ie(u);if(w&&h.add(w),u.type==="error"){G(t,502,"api_error",JSON.stringify(u));return}if(t.destroyed)break}if(t.destroyed)return;let x=z(_),b;try{b=Me(x),b.length===0&&h.size>0&&(b=Me({output:h.items()}))}catch(u){G(t,502,"api_error",String(u instanceof Error?u.message:u));return}let C=te(m,b.length,f);if(C){G(t,502,"api_error",ee(p,C));return}let T=[],B=y||it(x);B&&T.push({type:"thinking",thinking:B}),(f||b.length===0)&&T.push({type:"text",text:f}),T.push(...b),F(t,200,{id:o,type:"message",role:"assistant",content:T,model:i,stop_reason:b.length>0?"tool_use":"end_turn",usage:xe(x)??{input_tokens:0,output_tokens:0}})}function Bt(n){let{clients:e,apiKey:t,identity:s,statusOf:r}=n,i=typeof e=="function"?e:()=>e,o=new Z;return(l,a)=>{let p=new URL(l.url??"/","http://localhost"),m=p.pathname==="/v1/messages"||p.pathname.startsWith("/v1/messages/"),c=(d,f,y,_)=>m?G(a,d,_,f):$(a,d,f,y);(async()=>{if(l.method==="GET"&&p.pathname==="/healthz"){F(a,200,{ok:!0});return}if(l.method==="GET"&&p.pathname==="/stats"){F(a,200,s?{identity:s,...o.snapshot()}:o.snapshot());return}if(t&&(l.headers.authorization??"")!==`Bearer ${t}`){c(401,"invalid local proxy api key","authentication_error","authentication_error");return}let d=await i(l);if(l.method==="GET"&&p.pathname==="/v1/models"){if(p.searchParams.get("format")==="openrouter"){if(!d.openRouterModels){c(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}F(a,200,await d.openRouterModels());return}F(a,200,await d.listModels());return}if(l.method!=="POST"){c(404,`no route for ${l.method} ${p.pathname}`,"invalid_request_error","not_found_error");return}let f;try{f=await bt(l)}catch(_){if(_ instanceof ke){c(413,_.message,"request_too_large","request_too_large");return}c(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let y=Buffer.byteLength(JSON.stringify(f));if(p.pathname==="/v1/responses"){o.request("responses",y,y),await Ct(f,d,de(d),a,o);return}if(p.pathname==="/v1/chat/completions"){o.request("chat_completions",y,y),await Tt(f,d,a,o);return}if(p.pathname==="/v1/messages"){o.request("messages",y,y),await Ft(f,d,a,o);return}if(p.pathname==="/v1/messages/count_tokens"){o.request("count_tokens",y,y),G(a,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}c(404,`no route for POST ${p.pathname}`,"invalid_request_error","not_found_error")})().catch(d=>{let f=String(d instanceof Error?d.message:d);if(!a.headersSent){let y=r?.(d)??null;if(y){c(y.status,f,y.openaiType,y.anthropicType);return}c(500,f,"proxy_error","api_error");return}a.destroyed||(m?a.write(`event: error
40
- data: ${JSON.stringify({type:"error",error:{type:"api_error",message:f}})}
41
-
42
- `):a.write(`data: ${JSON.stringify({error:{message:f,type:"upstream_error"}})}
43
-
44
- `)),a.end()})}}function an(n){let{clients:e,apiKey:t}=n,s=At(Bt(n)),r=st(s,{clients:e,apiKey:t,statusOf:n.statusOf}),i=s.close.bind(s);return s.close=(o=>(r.close(),i(o))),Qe(s,{clients:e,apiKey:t}),s}export{ge as a,Qe as b,Dt as c,wt as d,st as e,Bt as f,an as g};
@@ -1,200 +0,0 @@
1
- import { C as CatalogRow } from './models-NYMZrklp.js';
2
- import { IncomingMessage } from 'node:http';
3
- import { a as AudioBridge, h as VideoFrameLane, j as VideoOutLane } from './video-out-CWesbk12.js';
4
-
5
- interface ProxyIdentity {
6
- app: string;
7
- org: string;
8
- fn: string;
9
-
10
- base_url: string;
11
- proxy_version: string;
12
- }
13
-
14
- type OpenRouterProviderDoc = {
15
- data: OpenRouterProviderModel[];
16
- };
17
- interface OpenRouterProviderModel {
18
- schema_version: '2.4';
19
-
20
- id: string;
21
- name: string;
22
- created: number;
23
-
24
- quantization: string | null;
25
- description: string;
26
- hugging_face_id: string;
27
- input_modalities: Array<Record<string, unknown>>;
28
- output_modalities: Array<Record<string, unknown>>;
29
- }
30
-
31
- interface DeployedApp {
32
- app_slug: string;
33
- function_name?: string | null;
34
- deployment_status?: string | null;
35
- [k: string]: unknown;
36
- }
37
-
38
- declare class UnknownModelError extends Error {
39
- }
40
-
41
- declare class SessionGoneError extends Error {
42
- }
43
-
44
- interface RouterModelEntry {
45
- id: string;
46
- object: 'model';
47
- created: number;
48
- owned_by: string;
49
-
50
- name?: string;
51
-
52
- description?: string;
53
-
54
- task?: string;
55
-
56
- engine?: string;
57
-
58
- gpu_spec?: string;
59
-
60
- context_length?: number;
61
- }
62
- interface RouterModelList {
63
- object: 'list';
64
- data: RouterModelEntry[];
65
- }
66
- interface ModelRouterOptions<S> {
67
-
68
- defaultApp: string | null;
69
-
70
- fnName: string;
71
-
72
- openSession: (appSlug: string) => S | Promise<S>;
73
-
74
- closeSession: (entry: S) => Promise<void>;
75
-
76
- listApps: (() => Promise<DeployedApp[]>) | null;
77
-
78
- listCatalog: (() => Promise<CatalogRow[]>) | null;
79
-
80
- appsTtlMs?: number;
81
-
82
- sessionKey?: (entry: S) => string;
83
- }
84
-
85
- type ImageCapabilityMode = 'generation' | 'edit';
86
-
87
- declare class ModelRouter<S> {
88
- private readonly opts;
89
- private readonly pool;
90
- private appsCache;
91
- private rowsCache;
92
- constructor(opts: ModelRouterOptions<S>);
93
-
94
- seed(appSlug: string, entry: S): void;
95
- private deployedApps;
96
-
97
- private catalogRows;
98
-
99
- private servable;
100
- private availableIds;
101
-
102
- private noDefaultApp;
103
-
104
- resolveApp(model: string | undefined): Promise<string>;
105
-
106
- sessionFor(model: string | undefined): Promise<{
107
- app: string;
108
- entry: S;
109
- }>;
110
- private keyOf;
111
-
112
- handleFor(model: string | undefined): Promise<{
113
- app: string;
114
- handle: string;
115
- }>;
116
-
117
- sessionForHandle(handle: string): Promise<{
118
- app: string;
119
- entry: S;
120
- }>;
121
-
122
- evict(app: string, entry: S): Promise<void>;
123
-
124
- modelList(): Promise<RouterModelList>;
125
-
126
- openRouterModels(): Promise<OpenRouterProviderDoc>;
127
-
128
- supportsImages(model: string | undefined, mode: ImageCapabilityMode): Promise<boolean>;
129
-
130
- closeAll(): Promise<void>;
131
- }
132
-
133
- interface ResponsesCreateParams {
134
- model?: string;
135
- input: unknown;
136
- stream?: boolean;
137
- tools?: unknown;
138
- tool_choice?: unknown;
139
- temperature?: number;
140
- max_output_tokens?: number;
141
-
142
- instructions?: string;
143
- top_p?: number;
144
- stop?: string[];
145
-
146
- reasoning_effort?: string;
147
- chat_template_kwargs?: Record<string, unknown>;
148
- }
149
-
150
- interface SessionEndInfo {
151
-
152
- timeLeftMs: number | null;
153
-
154
- reason: string;
155
- }
156
-
157
- interface ProxyClients {
158
-
159
- createResponse(params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
160
-
161
- listModels(): Promise<unknown>;
162
-
163
- openRouterModels?(): Promise<unknown>;
164
-
165
- openAudio?(model: string | undefined): Promise<ProxyAudioLane>;
166
-
167
- openVideo?(model: string | undefined): Promise<ProxyVideoLane>;
168
-
169
- openVideoOut?(model: string | undefined): Promise<ProxyVideoOutLane>;
170
-
171
- sessionHandle(model: string | undefined): Promise<string>;
172
-
173
- createResponseOn(handle: string, params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
174
-
175
- onSessionEnd(handle: string, cb: (end: SessionEndInfo) => void): Promise<() => void>;
176
- }
177
-
178
- type ProxyAudioLane = Pick<AudioBridge, 'appendInputAudio' | 'onOutputAudio'>;
179
-
180
- type ProxyVideoLane = Pick<VideoFrameLane, 'sendInputFrame'>;
181
-
182
- type ProxyVideoOutLane = Pick<VideoOutLane, 'codec' | 'onOutputFrame' | 'onLaneError'>;
183
-
184
- type ProxyClientsFor = (req: IncomingMessage) => ProxyClients | Promise<ProxyClients>;
185
- interface ProxyHandlerOptions {
186
-
187
- clients: ProxyClients | ProxyClientsFor;
188
-
189
- apiKey?: string;
190
-
191
- identity?: ProxyIdentity;
192
-
193
- statusOf?: (err: unknown) => {
194
- status: number;
195
- openaiType: string;
196
- anthropicType: string;
197
- } | null;
198
- }
199
-
200
- export { ModelRouter as M, type ProxyClients as P, SessionGoneError as S, UnknownModelError as U, type ProxyHandlerOptions as a, type ProxyIdentity as b, type ProxyVideoOutLane as c };