@urun-sh/openai 0.5.4 → 0.5.6

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 (55) hide show
  1. package/dist/{ResponsesClient-BYx3YLGo.d.ts → ResponsesClient-BE3-hx3m.d.ts} +2 -0
  2. package/dist/{ResponsesClient-Dft3bg3b.d.cts → ResponsesClient-DjZWjlFH.d.cts} +2 -0
  3. package/dist/chunk-2T2YYBVX.js +1 -0
  4. package/dist/chunk-3WAJD62J.js +4 -0
  5. package/dist/{chunk-K23AZPI4.js → chunk-5HKWNK3O.js} +1 -1
  6. package/dist/chunk-7M6LY6DR.js +2 -0
  7. package/dist/chunk-BSHT6RZZ.js +1 -0
  8. package/dist/{chunk-GBBY3PCZ.js → chunk-FP4RSAIE.js} +1 -1
  9. package/dist/chunk-HR5H6S7L.js +1 -0
  10. package/dist/chunk-NY23USZF.js +57 -0
  11. package/dist/chunk-VLRMJRLS.js +6 -0
  12. package/dist/gemini-live.cjs +2 -2
  13. package/dist/gemini-live.d.cts +17 -2
  14. package/dist/gemini-live.d.ts +7 -2
  15. package/dist/gemini-live.js +1 -1
  16. package/dist/hosted/bin.cjs +65 -37
  17. package/dist/hosted/bin.js +3 -3
  18. package/dist/hosted/index.cjs +46 -19
  19. package/dist/hosted/index.d.cts +760 -272
  20. package/dist/hosted/index.d.ts +203 -107
  21. package/dist/hosted/index.js +1 -1
  22. package/dist/index.cjs +1 -1
  23. package/dist/index.d.cts +4 -4
  24. package/dist/index.d.ts +4 -4
  25. package/dist/index.js +1 -1
  26. package/dist/{models-NYMZrklp.d.cts → models-DUdx_Y6X.d.cts} +1 -1
  27. package/dist/pi-extension/index.cjs +7 -7
  28. package/dist/pi-extension/index.d.cts +1 -1
  29. package/dist/pi-extension/index.d.ts +1 -1
  30. package/dist/pi-extension/index.js +1 -1
  31. package/dist/pi-extension/standalone.cjs +48 -48
  32. package/dist/proxy/cli.cjs +54 -40
  33. package/dist/proxy/cli.js +14 -14
  34. package/dist/proxy/index.cjs +33 -22
  35. package/dist/proxy/index.d.cts +546 -4
  36. package/dist/proxy/index.d.ts +185 -4
  37. package/dist/proxy/index.js +1 -1
  38. package/dist/responses-turn-Dr37N3kH.d.ts +486 -0
  39. package/dist/responses-turn-tNUHq5b0.d.cts +1807 -0
  40. package/dist/{translator-C9uPKypK.d.ts → translator-BCoRFaTs.d.ts} +1 -1
  41. package/dist/{translator-CcDBEfvm.d.cts → translator-Bh0Bp_Ie.d.cts} +3 -3
  42. package/dist/{video-out-D20UuJ8G.d.cts → video-out-CCksIcj8.d.cts} +5 -5
  43. package/package.json +12 -11
  44. package/dist/chunk-4MSBS7M6.js +0 -1
  45. package/dist/chunk-5NXM4IO3.js +0 -2
  46. package/dist/chunk-CWJRDBDC.js +0 -1
  47. package/dist/chunk-OI2OY32M.js +0 -1
  48. package/dist/chunk-RQBD4OAA.js +0 -6
  49. package/dist/chunk-STFRT5OY.js +0 -1
  50. package/dist/chunk-VNM4M4P3.js +0 -40
  51. package/dist/server-DuH_5OD9.d.ts +0 -84
  52. package/dist/server-wmUnMewW.d.cts +0 -206
  53. /package/dist/{chunk-YSFSRI3D.js → chunk-5CCJUNH7.js} +0 -0
  54. /package/dist/{models-NYMZrklp.d.ts → models-DUdx_Y6X.d.ts} +0 -0
  55. /package/dist/{video-out-CWesbk12.d.ts → video-out-IGQ4YQ-c.d.ts} +0 -0
@@ -0,0 +1,6 @@
1
+ import{a as D,b as B}from"./chunk-HR5H6S7L.js";import{H as oe,I as C,J as se,K as ie,L as ae,M as le,N as ce,O as ue,P as de,b as Y,d as Q,e as X,g as Z,h as ee,i as te,j as re,w as ne,x}from"./chunk-NY23USZF.js";import{e as q}from"./chunk-3WAJD62J.js";import{a as V}from"./chunk-7M6LY6DR.js";import{c as p}from"./chunk-5CCJUNH7.js";p();import{Counter as R,Gauge as he,Histogram as pe,Registry as ke,collectDefaultMetrics as Oe}from"@prometheus-io/client";import{createServer as Ie}from"http";var xt=9464,ge="/metrics",S="(other)",Ne="(none)",Te=64,Pe=128,$e=["/v1/responses","/v1/chat/completions","/v1/messages","/v1/messages/count_tokens","/v1/models","/v1/images/models","/v1/images/generations","/v1/images/edits",oe,ie],Ue=new Set($e);function Le(t){return Ue.has(t)?t:S}function He(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var A=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,r=!0){return e===null||e===""?Ne:this.admitted.has(e)?e:r?this.admitted.size>=this.max?(this.overflows+=1,S):(this.admitted.add(e),e):S}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},k=class{registry=new ke;modelLabel=new A(Te);orgLabel=new A(Pe);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new R({name:"urun_inference_requests_total",help:"Completed /v1 requests, by lane, method, HTTP status class, streaming, model and org.",labelNames:["lane","method","status_class","stream","model","org"],registers:[this.registry]}),this.duration=new pe({name:"urun_inference_request_duration_seconds",help:"Wall time from request receipt to response close. For a stream this is the WHOLE generation, not the time to the head.",labelNames:["lane","stream","model","org"],buckets:[.1,.25,.5,1,2.5,5,10,30,60,120,300,600],registers:[this.registry]}),this.ttft=new pe({name:"urun_inference_ttft_seconds",help:"Time to first output-text delta on a streamed response. The metric Hugging Face gates provider listing on (<5s for conversational models).",labelNames:["lane","model","org"],buckets:[.1,.25,.5,1,2,3,5,8,15,30,60],registers:[this.registry]}),this.streamErrors=new R({name:"urun_inference_stream_errors_total",help:"Responses that emitted an error frame AFTER the head flushed. These are HTTP 200 with a complete body, so no status-based metric can see them.",labelNames:["lane","model"],registers:[this.registry]}),this.undelivered=new R({name:"urun_inference_undelivered_total",help:"Responses whose socket did not finish flushing \u2014 the client vanished mid-answer.",labelNames:["lane","model"],registers:[this.registry]}),this.ledgerWrites=new R({name:"urun_inference_ledger_writes_total",help:"Terminal fate of the per-request ledger row for each billable /v1 request. outcome=written is the row landing (reason=priced|unpriced); outcome=skipped and outcome=failed are BOTH revenue that went unrecorded. failed/duplicate is the ENG-410 fabricated-row signal.",labelNames:["outcome","reason"],registers:[this.registry]}),this.ledgerQueue=new he({name:"urun_inference_ledger_queue",help:'The ledger write queue right now. measure="depth" is rows waiting for a worker; measure="oldest_age_seconds" is how long the head has waited \u2014 approaching the dwell bound means rows are about to be given up on, which is revenue lost.',labelNames:["measure"],registers:[this.registry],collect:()=>{this.ledgerQueue.set({measure:"depth"},ae()),this.ledgerQueue.set({measure:"oldest_age_seconds"},le()/1e3)}}),this.labelCardinality=new he({name:"urun_inference_label_cardinality",help:"Distinct values admitted for a bounded metric label. At its ceiling, further values report as (other).",labelNames:["label"],registers:[this.registry],collect:()=>{this.labelCardinality.set({label:"model"},this.modelLabel.size),this.labelCardinality.set({label:"org"},this.orgLabel.size)}}),this.labelOverflows=new R({name:"urun_inference_label_overflows_total",help:"Observations that would have taken a new label value but found the budget spent. Non-zero means a metric dimension is now partly blind.",labelNames:["label"],registers:[this.registry]}),e.defaultMetrics!==!1&&Oe({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let r=Le(e.path),n=e.status!==null&&!(e.status>=400&&e.status<500),o=this.modelLabel.overflowCount+this.orgLabel.overflowCount,a=this.modelLabel.of(e.model,n),s=this.orgLabel.of(e.org,n);this.modelLabel.overflowCount+this.orgLabel.overflowCount-o>0&&(this.modelLabel.overflowCount>o?this.labelOverflows.inc({label:"model"}):this.labelOverflows.inc({label:"org"}));let c=e.stream===!0?"true":"false";this.requests.inc({lane:r,method:e.method===""?S:e.method,status_class:He(e.status),stream:c,model:a,org:s}),this.duration.observe({lane:r,stream:c,model:a,org:s},e.duration_ms/1e3),e.ttft_ms!==null&&this.ttft.observe({lane:r,model:a,org:s},e.ttft_ms/1e3),e.stream_error&&this.streamErrors.inc({lane:r,model:a}),e.delivered||this.undelivered.inc({lane:r,model:a})}scrape(){return this.registry.metrics()}get contentType(){return this.registry.contentType}};function fe(t){return Ie((e,r)=>{(async()=>{let n=(e.url??"/").split("?")[0];if(e.method!=="GET"||n!==ge){r.writeHead(404,{"content-type":"text/plain"}),r.end(`no route for ${e.method} ${n} \u2014 this port serves only GET ${ge}
2
+ `);return}let o=await t.scrape();r.writeHead(200,{"content-type":t.contentType}),r.end(o)})().catch(n=>{if(!r.headersSent){r.writeHead(500,{"content-type":"text/plain"}),r.end(`${String(n instanceof Error?n.message:n)}
3
+ `);return}r.end()})})}p();import{createHash as ye}from"crypto";import{createServer as Me}from"http";var Ot=9465,Fe="x-urun-key-id",De="x-urun-key-fingerprint",Be="urun-ratelimit-bucket:",Ge="urun-ratelimit-tier:",me="anonymous",ze=/^urun_sk_[A-Za-z0-9_-]{43}$/;function Ke(t){return ye("sha256").update(`${Be}${t}`).digest("hex")}function je(t){return ye("sha256").update(`${Ge}${t}`).digest("hex")}function We(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let r=e[1].trim();return ze.test(r)?r:null}function we(){return Me((t,e)=>{let r=We(t.headers.authorization);e.setHeader(Fe,r===null?me:Ke(r)),e.setHeader(De,r===null?me:je(r)),e.writeHead(200),e.end()})}p();import{createClientToken as Je,ClientTokenError as qe}from"@urun-sh/core";var m=class extends Error{constructor(r,n=401){super(r);this.status=n;this.name="ProxyAuthError"}status},v=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function E(t){let e=(t??"").trim();if(e==="")throw new m("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let r=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!r)throw new m("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return r[1].trim()}var Ve=60,Ye=6e4,Qe=4096;function Xe(t){return t===401||t===403}var O=class{constructor(e){this.opts=e;this.mint=e.mint??Je,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new m("empty Authorization bearer token");let r=this.cache.get(e);if(r&&this.now()-r.at<Ye)return{apiKey:e,orgId:r.orgId};let n;try{n=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:Ve})).orgId}catch(o){throw o instanceof qe&&Xe(o.status)?(this.cache.delete(e),new m("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new v(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${o instanceof Error?o.message:String(o)}`)}if(!n)throw new v("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>=Qe){let o=this.cache.keys().next();o.done||this.cache.delete(o.value)}return this.cache.set(e,{orgId:n,at:this.now()}),{apiKey:e,orgId:n}}clear(){this.cache.clear()}};p();var Ze=8080,G="serve",Lt="0.0.0.0";function et(t,e,r){let n=(t[e]??"").trim();if(!n)throw new Error(`${e} is not set \u2014 ${r}`);return n}function Ht(t=process.env){let e=(t.PORT??"").trim(),r=e===""?Ze:Number(e);if(!Number.isInteger(r)||r<0||r>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let n=ne(x(et(t,"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")),o=(t.URUN_API_URL??"").trim(),a=o?x(o,"URUN_API_URL"):`${n.replace(/\/+$/,"")}/v1`,s=(t.URUN_CATALOG_URL??"").trim(),i=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!s!=!!i)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 c=s?{catalogUrl:x(s,"URUN_CATALOG_URL"),anonKey:i}:null;return{port:r,baseUrl:n,apiUrl:a,catalog:c}}p();import{createHash as tt,randomUUID as rt}from"crypto";var nt=512;function P(t){return`urun-inference-proxy-${tt("sha256").update(t).digest("hex").slice(0,32)}`}var I=class{constructor(e){this.opts=e}opts;tenants=new Map;label(e,r){return Object.assign(Object.create(e),{tenant:r.orgId})}build(e){if(this.opts.build){let n=this.opts.build(e);return{router:n.router,clients:this.label(n.clients,e)}}let r=D({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:G,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:P(e.apiKey),release:"detach"});return{router:r,clients:this.label(B(r),e)}}clientsFor(e){let r=this.tenants.get(e.apiKey);if(r)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,r),r.clients;for(;this.tenants.size>=nt;){let o=this.tenants.entries().next();if(o.done)break;let[a,s]=o.value;this.tenants.delete(a),s.router.closeAll().catch(()=>{})}let n=this.build(e);return this.tenants.set(e.apiKey,n),n.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(n=>n.router);this.tenants.clear();let r=[];if(await Promise.all(e.map(async n=>{try{await n.closeAll()}catch(o){r.push(String(o instanceof Error?o.message:o))}})),r.length>0)throw new Error(`failed to close ${r.length} tenant backhaul(s): ${r.join("; ")}`)}},be=512,ot=1024,N=class extends Error{constructor(){super(`conversation capacity reached (${be} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function st(t,e,r,n){let o=D({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:G,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:r.subject,release:"detach",sessionStartOptions:a=>{let s=n.get(a);return s===void 0?void 0:{sessionId:s}},onSessionOpen:(a,s)=>{n.set(a,s)}});return{clients:B(o),release:async()=>{await o.closeAll()}}}var T=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,r){let n={id:r,subject:`${P(e.apiKey)}:live:${r}`},o=new Map,a=this.opts.build?this.opts.build(e,n):st(this.opts,e,n,o);return{id:r,backhaul:a,attachments:0,nativeRefs:o}}attachEntry(e,r){let n=this.byCaller.get(e.apiKey);n===void 0&&(n=new Map,this.byCaller.set(e.apiKey,n));let o=n.get(r);if(o!==void 0)return n.delete(r),n.set(r,o),o.attachments+=1,o;this.evictOverflow();let a=this.build(e,r);return a.attachments=1,n.set(r,a),a}get conversationCount(){let e=0;for(let r of this.byCaller.values())e+=r.size;return e}evictOverflow(){for(;this.conversationCount>=be;){let e=this.byCaller.keys().next();if(e.done)throw new N;let r=e.value,n=this.byCaller.get(r),o=!1;for(let[a,s]of n)if(!(s.attachments>0)){n.delete(a),this.dropHandleIndexFor(r,a),s.backhaul.release().catch(()=>{}),o=!0;break}if(!o)throw n.size===0&&this.byCaller.delete(r),new N;n.size===0&&this.byCaller.delete(r)}}dropHandleIndexFor(e,r){for(let[n,o]of this.byHandle)o.apiKey===e&&o.conversationId===r&&this.byHandle.delete(n)}connectionFor(e){let r=null,n=!1,o=()=>(r===null&&(r=this.attachEntry(e,rt())),r),a={createResponse:(s,i)=>o().backhaul.clients.createResponse(s,i),catalogIdentity:s=>o().backhaul.clients.catalogIdentity(s),servingAppSlug:s=>{let i=o().backhaul.clients.servingAppSlug;if(i===void 0)throw new Error("the conversation backhaul wires no servingAppSlug seam \u2014 the caller-org app a request resolves to cannot be known");return i(s)},listModels:()=>o().backhaul.clients.listModels(),sessionHandle:s=>o().backhaul.clients.sessionHandle(s),createResponseOn:(s,i)=>o().backhaul.clients.createResponseOn(s,i),onSessionEnd:(s,i)=>o().backhaul.clients.onSessionEnd(s,i)};return a.openAudio=async s=>{let i=o().backhaul.clients.openAudio;if(i===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return i(s)},a.openVideo=async s=>{let i=o().backhaul.clients.openVideo;if(i===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return i(s)},a.openVideoOut=async s=>{let i=o().backhaul.clients.openVideoOut;if(i===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return i(s)},a.supportsAudio=async s=>{let i=o().backhaul.clients.supportsAudio;if(i===void 0)throw new Error("the conversation backhaul wires no supportsAudio seam \u2014 the realtime lane cannot gate audio modality on this deployment");return i(s)},{clients:a,bindResumeHandle:s=>{let i=r;if(i===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=ot;){let c=this.byHandle.keys().next();if(c.done)break;this.byHandle.delete(c.value)}this.byHandle.set(s,{apiKey:e.apiKey,conversationId:i.id})},clientsForResume:async s=>{let i=this.byHandle.get(s),c=i===void 0?void 0:this.byCaller.get(i.apiKey),u=i===void 0||c===void 0?void 0:c.get(i.conversationId);if(i===void 0||u===void 0||i.apiKey!==e.apiKey)throw new q("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 r=u,u.attachments+=1,u.backhaul.clients},release:async()=>{if(n)return;n=!0;let s=r;r=null,s!==null&&(s.attachments-=1,!(s.attachments>0)&&await s.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let n of this.byCaller.values())for(let o of n.values())e.push(o.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let r=[];if(await Promise.all(e.map(async n=>{try{await n}catch(o){r.push(String(o instanceof Error?o.message:o))}})),r.length>0)throw new Error(`failed to detach ${r.length} conversation backhaul(s): ${r.join("; ")}`)}};p();var it="/inference-ledger",at=1e4,$=class extends Error{ledgerFailureReason;constructor(e,r="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=r}},K=class extends Error{constructor(r,n){super(n);this.ledgerAlreadyWritten=r;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},g=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},j=class extends m{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},lt=[["inference_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["model_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["variant",t=>t===null||typeof t=="string"],["task",t=>typeof t=="string"&&/[^\s]/.test(t)],["http_status",t=>typeof t=="number"&&Number.isInteger(t)],["started_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["completed_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["prompt_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["completion_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["output_units",ve],["cost_nano_usd",ve],["price_version",t=>t===null||typeof t=="string"]];function ct(t){for(let[e,r]of lt){if(!(e in t))return`${e} (absent)`;if(!r(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function ut(t){let e;try{e=JSON.parse(t)}catch{return{status:"unavailable"}}if(e===null||typeof e!="object"||Array.isArray(e))return{status:"unavailable"};let r=e.existing_status;if(r==="not_in_this_org")return{status:"not_in_this_org"};if(r!=="found")return{status:"unavailable"};let n=e.existing;return n===null||typeof n!="object"||Array.isArray(n)?{status:"unavailable"}:ct(n)!==null?{status:"unavailable"}:{status:"found",row:n}}function ve(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function z(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function _e(t,e){if(typeof e!="string")return!1;let r=Date.parse(t),n=Date.parse(e);return Number.isFinite(r)&&Number.isFinite(n)&&r===n}function dt(t,e){let r=e;return r.inference_id===t.inference_id&&r.model_id===t.model_id&&(r.variant??null)===t.variant&&r.task===t.task&&r.http_status===t.http_status&&_e(t.started_at,r.started_at)&&_e(t.completed_at,r.completed_at)&&z(t.prompt_tokens,r.prompt_tokens)&&z(t.completion_tokens,r.completion_tokens)&&z(t.output_units,r.output_units)}function ht(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new g(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let r=t,n=r.inference_id;if(typeof n!="string"||n==="")throw new g(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in r))throw new g(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let o=r.cost_nano_usd;if(o!=null&&!(typeof o=="number"&&Number.isInteger(o)&&o>=0))throw new g(`${e} answered cost_nano_usd ${JSON.stringify(o)}, which is neither a non-negative integer nor null`);let a=u=>{if(!(u in r))throw new g(`${e} answered a row with no ${u} field: ${JSON.stringify(t)}`);let f=r[u];if(f===null)return null;if(typeof f!="string")throw new g(`${e} answered a non-string ${u}: ${JSON.stringify(f)}`);if(!/[^\s]/.test(f))throw new g(`${e} answered a BLANK ${u} (${JSON.stringify(f)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return f},s=a("price_version"),i=a("unpriced_reason"),c=o!=null;if(c&&(s===null||i!==null))throw new g(`${e} answered a PRICED row (cost_nano_usd ${String(o)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}`);if(!c&&(s!==null||i===null))throw new g(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}. NULL means NOT YET PRICED, and the reason is how an operator learns that traffic is being served which nobody can bill.`);return{inference_id:n,cost_nano_usd:o===void 0?null:o,price_version:s,unpriced_reason:i}}var U=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,r){let n=`${this.opts.apiUrl.replace(/\/+$/,"")}${it}`,o;try{o=await this.fetchImpl(n,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(r),signal:AbortSignal.timeout(at)})}catch(c){throw new g(`the uRun ledger surface at ${n} could not be reached: ${c instanceof Error?c.message:String(c)}`)}if(o.status===409){let c=await o.text().catch(()=>""),u=ut(c);throw u.status==="found"&&dt(r,u.row)?new K(u.row,`the uRun control plane already holds THIS PROXY'S OWN row for inference_id ${r.inference_id} \u2014 every billable field matches the row being re-sent, so an earlier attempt landed and this retry is a no-op. Nothing was written and nothing was overwritten.`):u.status==="not_in_this_org"?new $(`inference_id ${r.inference_id} is ALREADY TAKEN by a ledger row this org cannot read. The id space is a global primary key, so this is not a retry and cannot be this proxy's own write \u2014 either two orgs collided on a uuid, or a row was written for this id by somebody else.`,"duplicate_foreign"):new $(`the uRun control plane already holds a ledger row for inference_id ${r.inference_id} that is NOT the one being written${u.status==="unavailable"?" (and it could not be read back to compare)":""} \u2014 nothing was written and nothing was overwritten. Either this proxy wrote twice, or a row was fabricated ahead of it (this write is authenticated by the CALLER'S OWN org key). ${c.slice(0,400)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new j("the uRun org API key was rejected by the uRun control plane when writing the inference ledger \u2014 the request was served but produced no billable row");if(!o.ok){let c=await o.text().catch(u=>`<body unreadable: ${u instanceof Error?u.message:String(u)}>`);throw new g(`the uRun ledger surface at ${n} answered HTTP ${o.status}: ${c.slice(0,400)}`)}let a;try{a=await o.json()}catch(c){throw new g(`the uRun ledger surface at ${n} answered a body that is not JSON: ${c instanceof Error?c.message:String(c)}`)}let s=a?.recorded;if(s===void 0)throw new g(`the uRun ledger surface at ${n} answered a body with no "recorded" field`);let i=ht(s,`the uRun ledger surface at ${n}`);if(i.inference_id!==r.inference_id)throw new g(`the uRun ledger surface at ${n} answered for inference_id ${i.inference_id} when asked about ${r.inference_id}`);return i}};p();import{createServer as gt}from"http";p();var L="/inference-usage",Re=25e3;var pt=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function Ee(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=pt.has(t.name)?t.name:"Error",r=t.cause?.code;return typeof r=="string"&&/^[A-Z][A-Z0-9_]{0,31}$/.test(r)?`${e} (${r})`:e}var H=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,r){let n=`${this.opts.apiUrl.replace(/\/+$/,"")}${L}`,o;try{o=await this.fetchImpl(n,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:r}),signal:AbortSignal.timeout(Re)})}catch(s){throw new C(`the uRun usage surface (POST ${L}) could not be reached within ${Re}ms: ${Ee(s)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new m("the uRun org API key was rejected by the uRun control plane \u2014 check the key and that it is not revoked");if(!o.ok){let s=await o.text().catch(()=>null),i=s===null?"body unreadable":`${s.length}-character body`;throw new C(`the uRun usage surface (POST ${L}) answered HTTP ${o.status} (${i})`)}let a;try{a=await o.json()}catch(s){throw new C(`the uRun usage surface (POST ${L}) answered HTTP ${o.status} with a body that is not JSON: ${Ee(s)}`)}return se(a,this.opts.onRejectedRow)}};var ft=5e3,mt=3e3,yt="/api/client-tokens";function W(t,e,r){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(r))}function xe(t,e,r,n){W(t,e,{error:{message:r,type:n}})}async function wt(t,e=fetch){let r=`${t.replace(/\/+$/,"")}${yt}`;try{let n=await e(r,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(mt)});return n.status===404?{ready:!1,detail:`${r} answered 404 \u2014 this is not a uRun control plane`}:n.status>=500?{ready:!1,detail:`${r} answered HTTP ${n.status}`}:{ready:!0,detail:`${r} answered HTTP ${n.status}`}}catch(n){return{ready:!1,detail:`${r} unreachable: ${n instanceof Error?n.message:String(n)}`}}}function pr(t){let e=t.resolver??new O({gatewayUrl:t.baseUrl}),r=t.registry??new I({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),n=t.probeControlPlane??(()=>wt(t.baseUrl)),o=t.usageClient??new H({apiUrl:t.apiUrl}),a=t.ledgerClient??new U({apiUrl:t.apiUrl}),s=new k(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),i=async l=>e.resolve(E(l.headers.authorization)),c=l=>l instanceof m?{status:l.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:l instanceof v?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,u=de({clients:async l=>r.clientsFor(await i(l)),usage:{authorize:l=>E(l.headers.authorization),lookup:(l,d)=>o.lookup(l,d)},statusOf:c,...t.requestLog?{requestLog:t.requestLog}:{},observe:l=>s.observe(l),ledger:{write:(l,d)=>a.write(E(l.headers.authorization),d),observe:l=>s.observeLedger(l),...t.ledgerLog?{report:t.ledgerLog}:{}}}),f=null,_=gt((l,d)=>{let h=new URL(l.url??"/","http://localhost");(async()=>{if(l.method==="GET"&&h.pathname==="/healthz"){W(d,200,{ok:!0});return}if(l.method==="GET"&&h.pathname==="/readyz"){let J=Date.now();if(!f||J-f.at>=ft){let Ae=await n();f={at:J,...Ae}}W(d,f.ready?200:503,{ok:f.ready,control_plane:f.detail});return}let y=h.pathname==="/v1"||h.pathname.startsWith("/v1/"),F=h.pathname.startsWith("/partners/");if(!y&&!F){xe(d,404,`no route for ${l.method} ${h.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the partner adapter surface (/partners/...), the WS upgrade paths (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent and /v1/realtime), plus /healthz and /readyz`,"invalid_request_error");return}u(l,d)})().catch(y=>{if(!d.headersSent){xe(d,500,String(y instanceof Error?y.message:y),"proxy_error");return}d.end()})}),M=t.conversations??new T({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),Ce=[Y(_,{authenticate:async l=>{let d=await e.resolve(l),h=M.connectionFor(d);return{clients:h.clients,scope:P(d.apiKey),onBindResumeHandle:h.bindResumeHandle,clientsForResume:h.clientsForResume,release:h.release}}}),X(_,{clients:async l=>r.clientsFor(await i(l)),statusOf:c}).close,ee(_,{resolver:{resolve:async l=>{let d=await e.resolve(E(l.headers.authorization)),h=M.connectionFor(d),y;try{y=await re(h.clients,te(l))}catch(F){throw h.release().catch(()=>{}),F}return{...y,release:h.release}}}})];_.on("upgrade",(l,d)=>{let h=new URL(l.url??"/","http://localhost");h.pathname===V||h.pathname===Q||h.pathname===Z||d.destroyed||(d.write(`HTTP/1.1 404 Not Found\r
4
+ Connection: close\r
5
+ \r
6
+ no WS route for ${h.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),d.destroy())});let Se=async()=>{for(let l of Ce)await l();await M.detachAll()};return{server:_,metricsServer:fe(s),metrics:s,edgeIdentityServer:we(),closeAll:()=>r.closeAll(),closeWebSockets:Se}}async function gr(t,e,r){r&&await r();let n=new Promise(o=>t.close(()=>o()));t.closeAllConnections(),await n,await ce()||ue(),await e()}export{xt as a,ge as b,S as c,Ne as d,Te as e,Pe as f,Le as g,He as h,A as i,k as j,fe as k,Ot as l,Fe as m,De as n,Be as o,Ge as p,me as q,Ke as r,je as s,We as t,we as u,m as v,v as w,E as x,Ye as y,Qe as z,O as A,Ze as B,G as C,Lt as D,Ht as E,nt as F,P as G,I as H,be as I,ot as J,N as K,T as L,it as M,at as N,$ as O,g as P,ht as Q,U as R,ft as S,mt as T,pr as U,gr as V};
@@ -1,2 +1,2 @@
1
- "use strict";var he=Object.create;var E=Object.defineProperty;var be=Object.getOwnPropertyDescriptor;var ke=Object.getOwnPropertyNames;var _e=Object.getPrototypeOf,Oe=Object.prototype.hasOwnProperty;var Ae=(e,t)=>{for(var n in t)E(e,n,{get:t[n],enumerable:!0})},F=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of ke(t))!Oe.call(e,r)&&r!==n&&E(e,r,{get:()=>t[r],enumerable:!(o=be(t,r))||o.enumerable});return e};var $=(e,t,n)=>(n=e!=null?he(_e(e)):{},F(t||!e||!e.__esModule?E(n,"default",{value:e,enumerable:!0}):n,e)),ve=e=>F(E({},"__esModule",{value:!0}),e);var Pe={};Ae(Pe,{AudioBridge:()=>A,CLOSE_PROTOCOL:()=>B,CLOSE_SESSION_GONE:()=>ne,CLOSE_UNKNOWN_MODEL:()=>te,CLOSE_UPSTREAM:()=>U,GEMINI_AUDIO_IN_RATE:()=>_,GEMINI_AUDIO_OUT_MIME:()=>H,GEMINI_AUDIO_OUT_RATE:()=>V,GEMINI_LIVE_PATH:()=>ee,GeminiLiveError:()=>s,NATIVE_AUDIO_SAMPLE_RATE:()=>I,OUTBOUND_AUDIO_FLUSH_B64:()=>le,RT_VIDEO_IN_STREAM:()=>S,RT_VIDEO_OUT_STREAM:()=>k,SdkTransport:()=>M,VIDEO_FRAME_MESSAGE_BUDGET:()=>R,VIDEO_OUT_DEFAULT_CODEC:()=>T,VIDEO_OUT_FRAMING:()=>P,VIDEO_OUT_SETUP_KEY:()=>b,VIDEO_OUT_WS_FRAMING:()=>C,consumeVideoOut:()=>X,decodeResponsesLane:()=>v,decodeVideoOutRecord:()=>x,enableSessionAudio:()=>W,enableSessionVideo:()=>G,encodeVideoOutRecord:()=>K,goAwayMsg:()=>de,negotiateSessionVideoOutQuic:()=>Q,nodeAudioBackend:()=>q,openSessionVideoOutLane:()=>z,parseClientMessage:()=>oe,resamplePcm16:()=>j,responseCreateToEnvelope:()=>D,responsesCreateToEnvelope:()=>N,serverContentOfAudio:()=>ge,serverContentOfDelta:()=>me,sessionResumptionUpdateMsg:()=>ue,translateClientContent:()=>ce,translateRealtimeInput:()=>fe,translateSetup:()=>ae,translateToolResponse:()=>pe,turnEndOfCompleted:()=>ye,videoOutAck:()=>ie,videoOutChunkMsg:()=>se,weriftAudioBackend:()=>J});module.exports=ve(Pe);var I=24e3,A=class{constructor(t,n){this.backend=t;this.opts=n}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(t){if(!this.source)throw new Error("startOutbound() not called");let n=Buffer.from(t,"base64"),o=Math.floor(n.byteLength/2),r=new Int16Array(o);for(let i=0;i<o;i++)r[i]=n.readInt16LE(i*2);this.source.onData(r)}async startInbound(t){if(t==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(t),this.sink.onframe=n=>{let o=Buffer.from(n.samples.buffer,n.samples.byteOffset,n.samples.byteLength).toString("base64");for(let r of this.outHandlers)r(o)}}onOutputAudio(t){return this.outHandlers.add(t),()=>this.outHandlers.delete(t)}};async function W(e,t,n=1e4){let o=new A(t,{sampleRate:24e3}),r=await o.startOutbound();await e.stream("rt-audio-in").attach(r);let i=await Ee(e,n);return await o.startInbound(i),o}function Ee(e,t){let n=e.stream("rt-audio-out");return n.track!=null?Promise.resolve(n.track):new Promise((o,r)=>{let i,a=setTimeout(()=>{i?.(),r(new Error(`enableSessionAudio: no downstream audio track within ${t}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},t),c=d=>{d!=null&&(clearTimeout(a),i?.(),o(d))};i=n.on?.("track",c),n.track!=null&&c(n.track)})}var S="rt-video-in",R=256e3;function G(e){let t=e.stream(S),n=t.emit;if(typeof n!="function")throw new Error(`enableSessionVideo: session.stream('${S}') 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 o=>{if(o.byteLength===0)throw new Error("enableSessionVideo: refusing to send an empty video frame");if(o.byteLength>R)throw new Error(`enableSessionVideo: frame is ${o.byteLength} bytes \u2014 over the ${R}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await n.call(t,o)}}}var Se=24e3,O=480,Re=111;async function Ie(){let e=await import("opusscript"),t=e.default??e;return new t(Se,1,2048)}async function J(){let e=await import("werift"),t=await Ie();return{createSource:()=>{let n=new e.MediaStreamTrack({kind:"audio"}),o=Math.random()*4294967295>>>0,r=Math.random()*65535&65535,i=Math.random()*4294967295>>>0,a=new Int16Array(0);return{track:n,onData:c=>{let d=new Int16Array(a.length+c.length);d.set(a,0),d.set(c,a.length);let u=0;for(;d.length-u>=O;){let l=d.subarray(u,u+O);u+=O;let f=Buffer.from(l.buffer,l.byteOffset,l.byteLength),p=t.encode(f,O);r=r+1&65535,i=i+O>>>0;let g=new e.RtpHeader({version:2,payloadType:Re,sequenceNumber:r,timestamp:i,ssrc:o,marker:!1}),w=new e.RtpPacket(g,p);n.writeRtp(w)}a=d.slice(u)}}},createSink:n=>{let o={onframe:null};return n.onReceiveRtp.subscribe(i=>{let a;try{a=t.decode(i.payload)}catch{return}let c=new Int16Array(a.length/2);for(let d=0;d<c.length;d++)c[d]=a.readInt16LE(d*2);o.onframe?.({samples:c})}),o}}}async function q(){let e=await import("@roamhq/wrtc");return{createSource:()=>{let t=new e.nonstandard.RTCAudioSource;return{track:t.createTrack(),onData:o=>t.onData({samples:o,sampleRate:24e3})}},createSink:t=>{let n={onframe:null},o=new e.nonstandard.RTCAudioSink(t);return o.ondata=r=>{n.onframe?.(r)},n}}}var y=require("@urun-sh/core"),k="rt-video-out",P="urun.videoOut.v1",T="h264";function Y(e,t){if(!Number.isInteger(e.seq)||e.seq<0)throw new Error(`${t}: header seq must be a non-negative integer (got ${e.seq})`);if(!Number.isInteger(e.ptsUs))throw new Error(`${t}: header ptsUs must be an integer microsecond timestamp (got ${e.ptsUs})`);if(typeof e.codec!="string"||e.codec.length===0)throw new Error(`${t}: header codec must be a non-empty string`);if(typeof e.key!="boolean")throw new Error(`${t}: header key must be a boolean`)}function K(e,t){if(Y(e,"encodeVideoOutRecord"),t.byteLength===0)throw new Error("encodeVideoOutRecord: refusing to encode an empty access unit");let n=new TextEncoder().encode(JSON.stringify({v:1,seq:e.seq,ptsUs:e.ptsUs,codec:e.codec,key:e.key}));if(n.byteLength>65535)throw new Error(`encodeVideoOutRecord: header JSON is ${n.byteLength} bytes \u2014 over the u16 length prefix`);let o=new Uint8Array(2+n.byteLength+t.byteLength);return o[0]=n.byteLength&255,o[1]=n.byteLength>>8,o.set(n,2),o.set(t,2+n.byteLength),o}function x(e){if(e.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${e.byteLength} byte(s), no header length)`);let t=e[0]|e[1]<<8;if(e.byteLength<2+t)throw new Error(`decodeVideoOutRecord: truncated record (header says ${t} bytes, ${e.byteLength-2} present)`);let n;try{n=JSON.parse(new TextDecoder().decode(e.subarray(2,2+t)))}catch(a){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(a)})`)}let o=n;if(o===null||typeof o!="object"||o.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(o?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let r=o;Y(r,"decodeVideoOutRecord");let i=e.subarray(2+t);if(i.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:r,au:i.slice()}}async function Q(e){let t=e.wtDataEndpoint;if(typeof t!="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 n=await t.call(e),o=n.sessionId??e.sessionId;if(typeof o!="string"||o.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:n.url,certHashes:n.certHashes,token:n.token,sessionId:o,stream:k,codec:T,framing:P}}var C="urun.videoOut.v2-ws";function z(e){let t=e.stream(k);if(typeof t.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${k}') 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 n=new Set,o=new Set,r=new Set,i=d=>{for(let u of o)u(d)},a=!1,c=async()=>{for await(let d of t.messages()){if(!(d instanceof Uint8Array)){i(new Error(`openSessionVideoOutLane: "${k}" delivered a non-BINARY \xA75 payload (${typeof d}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let u;try{u=x(d)}catch(l){i(l instanceof Error?l:new Error(String(l)));continue}for(let l of n)l(u)}for(let d of r)d()};return{codec:T,onOutputFrame:d=>(n.add(d),a||(a=!0,c().catch(u=>i(u instanceof Error?u:new Error(String(u))))),()=>n.delete(d)),onLaneError:d=>(o.add(d),()=>o.delete(d)),onEnd:d=>(r.add(d),()=>r.delete(d))}}function Te(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 t=new Uint8Array(e.length/2);for(let n=0;n<t.length;n++)t[n]=parseInt(e.slice(n*2,n*2+2),16);return t}async function X(e,t,n){let o=n??(typeof globalThis.WebTransport=="function"?(u,l)=>new globalThis.WebTransport(u,l):void 0);if(!o)throw new Error("consumeVideoOut: no WebTransport implementation \u2014 this platform has no globalThis.WebTransport; inject a connect factory (the third argument)");let r=o(e.url,{serverCertificateHashes:e.certHashes.map(u=>({algorithm:"sha-256",value:Te(u)}))});await r.ready;let i=await r.createBidirectionalStream();await i.writable.getWriter().write((0,y.encodeWtControlFrame)({t:"hello",token:e.token,sessionId:e.sessionId}));let a=i.readable.getReader(),c=new y.WtControlDecoder,d=!1;for(;!d;){let{value:u,done:l}=await a.read();if(l)throw new Error("consumeVideoOut: gateway closed the control stream before hello-ack");if(u)for(let f of c.push(u)){if(f.t==="hello-ack"){d=!0;break}throw new Error(`consumeVideoOut: gateway rejected hello (got ${JSON.stringify(f.t)} before hello-ack)`)}}await(0,y.consumeWtDataStreams)(r.incomingUnidirectionalStreams,{onData:(u,l)=>{if(u===e.stream){if(l===y.STREAM_END){t.onEnd?.();return}if(!(l instanceof Uint8Array)){t.onLaneError(new Error(`consumeVideoOut: "${e.stream}" delivered a non-BINARY \xA75 payload (${typeof l}) \u2014 spec v1 records ride BINARY envelopes only`));return}try{t.onFrame(x(l))}catch(f){t.onLaneError(f instanceof Error?f:new Error(String(f)))}}},onLaneError:u=>t.onLaneError(u)})}var ee="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",B=1007,te=1008,U=1011,ne=1008,s=class extends Error{constructor(n,o=B){super(n);this.closeCode=o}closeCode},m=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)?e:null,L=["setup","clientContent","realtimeInput","toolResponse"];function oe(e){let t;try{t=JSON.parse(e)}catch{throw new s("client message is not valid JSON")}let n=m(t);if(!n)throw new s("client message must be a JSON object");let o=L.filter(c=>n[c]!==void 0),r=Object.keys(n).filter(c=>!L.includes(c));if(r.length>0)throw new s(`unknown client message field(s) ${JSON.stringify(r)} \u2014 expected exactly one of ${L.join(", ")}`);if(o.length!==1)throw new s(`client message must contain exactly one of ${L.join(", ")} (got ${o.length})`);let i=o[0],a=m(n[i]);if(!a)throw new s(`"${i}" must be a JSON object`);return{kind:i,[i]:a}}function re(e,t){if(typeof e=="string")return e;let n=m(e),o=n?n.parts:void 0;if(!Array.isArray(o))throw new s(`${t}: expected a Content with a parts array`);let r=[];for(let i of o){let a=m(i);if(a&&typeof a.text=="string"){r.push(a.text);continue}throw a&&(a.inlineData!==void 0||a.fileData!==void 0)?new s(`${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 s(`${t}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new s(`${t}: unsupported part ${JSON.stringify(i)}`)}return r.join(`
2
- `)}var xe={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},b="urun.videoOut";function ie(e){return{framing:C,codec:e}}function se(e){let{seq:t,ptsUs:n,codec:o,key:r}=e.header;return{[b]:{v:1,seq:t,ptsUs:n,codec:o,key:r,data:Buffer.from(e.au).toString("base64")}}}var Ce={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Le=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",b],Z=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function Ue(e){let t=m(e);if(!t)throw new s("setup.realtimeInputConfig must be an object");for(let r of Object.keys(t))if(!(r==="automaticActivityDetection"||r==="activityHandling"))throw new s(r==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${r}"`);let n=m(t.automaticActivityDetection);if(t.automaticActivityDetection!==void 0&&!n)throw new s("realtimeInputConfig.automaticActivityDetection must be an object");for(let r of Object.keys(n??{}))if(r!=="disabled")throw new s(`automaticActivityDetection.${r} tunes AUTOMATIC (server-side) activity detection \u2014 the declared platform follow-up (serve turn_detection events over the session stream lane); only "disabled: true" (manual activity mode) is supported in this slice`);if(n?.disabled!==!0)throw new s("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let o="START_OF_ACTIVITY_INTERRUPTS";if(t.activityHandling!==void 0){if(!Z.includes(t.activityHandling))throw new s(`unsupported activityHandling ${JSON.stringify(t.activityHandling)} \u2014 expected one of ${Z.join(", ")}`);o=t.activityHandling}return{manualActivity:!0,activityHandling:o}}var Ve=["temperature","maxOutputTokens","responseModalities"];function ae(e){for(let[f,p]of Object.entries(Ce))if(e[f]!==void 0)throw new s(p);for(let f of Object.keys(e))if(!Le.includes(f))throw new s(`unsupported setup field "${f}"`);if(typeof e.model!="string"||e.model.length===0)throw new s('setup.model is required (format "models/{model}")');let t=e.model.replace(/^models\//,""),n,o,r=!1,i=m(e.generationConfig);if(e.generationConfig!==void 0&&!i)throw new s("setup.generationConfig must be an object");if(i){for(let f of Object.keys(i))if(!Ve.includes(f))throw new s(`unsupported generationConfig field "${f}"`);if(i.temperature!==void 0){if(typeof i.temperature!="number")throw new s("generationConfig.temperature must be a number");n=i.temperature}if(i.maxOutputTokens!==void 0){if(typeof i.maxOutputTokens!="number")throw new s("generationConfig.maxOutputTokens must be a number");o=i.maxOutputTokens}if(i.responseModalities!==void 0){let f=Array.isArray(i.responseModalities)?i.responseModalities:[i.responseModalities];for(let p of f){let g=String(p).toUpperCase();if(g==="AUDIO"){r=!0;continue}if(g!=="TEXT")throw new s(`responseModalities ${JSON.stringify(p)} 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:re(e.systemInstruction,"setup.systemInstruction")});let c;if(e.tools!==void 0){if(!Array.isArray(e.tools))throw new s("setup.tools must be an array");c=[];for(let f of e.tools){let p=m(f);if(!p)throw new s("setup.tools entries must be objects");for(let g of Object.keys(p))if(g!=="functionDeclarations")throw new s(`unsupported tool "${g}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(p.functionDeclarations))throw new s("tool.functionDeclarations must be an array");for(let g of p.functionDeclarations){let w=m(g);if(!w||typeof w.name!="string")throw new s("functionDeclarations entries require a string name");c.push({type:"function",name:w.name,description:w.description,parameters:w.parameters})}}c.length===0&&(c=void 0)}let d={enabled:!1,handle:null};if(e.sessionResumption!==void 0){let f=m(e.sessionResumption);if(!f)throw new s("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let p of Object.keys(f))if(p!=="handle")throw new s(`unsupported sessionResumption field "${p}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(f.handle!==void 0&&(typeof f.handle!="string"||f.handle.length===0))throw new s("sessionResumption.handle must be a non-empty string when present");d={enabled:!0,handle:typeof f.handle=="string"?f.handle:null}}let u=e.realtimeInputConfig===void 0?xe:Ue(e.realtimeInputConfig),l=!1;if(e[b]!==void 0){if(!m(e[b]))throw new s(`setup["${b}"] must be an object (the v1 opt-in is {})`);l=!0}return{model:t,systemItems:a,tools:c,temperature:n,maxOutputTokens:o,audioModality:r,activity:u,sessionResumption:d,videoOut:l}}function ue(e){return{sessionResumptionUpdate:{newHandle:e,resumable:!0}}}function de(e){let t=Math.max(0,e??0)/1e3;return{goAway:{timeLeft:`${Math.round(t*1e3)/1e3}s`}}}function ce(e){for(let o of Object.keys(e))if(o!=="turns"&&o!=="turnComplete")throw new s(`unsupported clientContent field "${o}"`);let t=e.turns===void 0?[]:e.turns;if(!Array.isArray(t))throw new s("clientContent.turns must be an array");let n=[];for(let o of t){let r=m(o);if(!r)throw new s("clientContent.turns entries must be Content objects");let i=r.role===void 0?"user":String(r.role).toLowerCase();if(i!=="user"&&i!=="model")throw new s(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(r.role)})`);n.push({role:i==="model"?"assistant":"user",content:re(r,"clientContent.turns")})}return{items:n,turnComplete:e.turnComplete===!0}}var _=16e3,V=24e3,H=`audio/pcm;rate=${V}`,le=Buffer.alloc(960).toString("base64");function j(e,t,n){if(t===n)return e;let o=Math.floor(e.length*n/t),r=new Int16Array(o),i=t/n,a=e.length-1;for(let c=0;c<o;c++){let d=c*i,u=Math.floor(d);if(u>=a){r[c]=e[a];continue}let l=d-u;r[c]=Math.round(e[u]+(e[u+1]-e[u])*l)}return r}function De(e){let t=m(e);if(!t)throw new s("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof t.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(t.mimeType))throw new s(`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 s("realtimeInput.video.data must be non-empty base64");let n=Buffer.from(t.data,"base64");if(n.length<3||n[0]!==255||n[1]!==216)throw new s("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function Ne(e){let t=m(e);if(!t)throw new s("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof t.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(t.mimeType))throw new s(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(t.mimeType)}`);let n=/;\s*rate=(\d+)/.exec(t.mimeType);if(n&&Number(n[1])!==_)throw new s(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${_} Hz (audio/pcm;rate=${_})`);if(typeof t.data!="string"||t.data.length===0)throw new s("realtimeInput.audio.data must be non-empty base64");let o=Buffer.from(t.data,"base64");if(o.length===0||o.length%2!==0)throw new s(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${o.length} bytes \u2014 not even)`);let r=new Int16Array(o.length/2);for(let a=0;a<r.length;a++)r[a]=o.readInt16LE(a*2);let i=j(r,_,V);return Buffer.from(i.buffer,i.byteOffset,i.byteLength).toString("base64")}function fe(e){if(e.mediaChunks!==void 0)throw new s('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 s("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let r=e.activityStart!==void 0?"activityStart":"activityEnd",i=m(e[r]);if(!i||Object.keys(i).length!==0)throw new s(`realtimeInput.${r} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:r}}let t=["text","audio","audioStreamEnd","video"];for(let o of Object.keys(e))if(!t.includes(o))throw new s(`unsupported realtimeInput field "${o}"`);let n=t.filter(o=>e[o]!==void 0);if(n.length!==1)throw new s(`realtimeInput must carry exactly one of ${t.join(", ")} (got ${n.length})`);if(e.audio!==void 0)return{kind:"audio",base64Pcm24k:Ne(e.audio)};if(e.video!==void 0)return{kind:"video",jpegFrame:De(e.video)};if(e.audioStreamEnd!==void 0){if(e.audioStreamEnd!==!0)throw new s("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof e.text!="string"||e.text.length===0)throw new s("realtimeInput.text must be a non-empty string");return{kind:"text",text:e.text}}function pe(e){for(let t of Object.keys(e))if(t!=="functionResponses")throw new s(`unsupported toolResponse field "${t}"`);if(!Array.isArray(e.functionResponses)||e.functionResponses.length===0)throw new s("toolResponse.functionResponses must be a non-empty array");return e.functionResponses.map(t=>{let n=m(t);if(!n||typeof n.id!="string"||n.id.length===0)throw new s("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:n.id,output:JSON.stringify(n.response??null)}})}function me(e){return e.type!=="response.output_text.delta"||typeof e.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:e.delta}]}}}}function ge(e){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:H,data:e}}]}}}}function ye(e){let t=m(e)??{},n=Array.isArray(t.output)?t.output:[],o=[],r=[],i=typeof t.output_text=="string"?t.output_text:"";if(!i)for(let u of n){let l=m(u);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let f of l.content){let p=m(f);p?.type==="output_text"&&typeof p.text=="string"&&(i+=p.text)}}i&&r.push({role:"assistant",content:i});for(let u of n){let l=m(u);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new s(`upstream function_call item is missing call_id/name: ${JSON.stringify(u)}`,U);let f=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",p;try{p=JSON.parse(f)}catch{throw new s(`upstream function_call arguments are not valid JSON: ${f}`,U)}o.push({id:l.call_id,name:l.name,args:p}),r.push({type:"function_call",call_id:l.call_id,name:l.name,arguments:f})}let a=[];o.length>0&&a.push({toolCall:{functionCalls:o}}),a.push({serverContent:{generationComplete:!0}});let c=m(t.usage),d={serverContent:{turnComplete:!0}};if(c){let u={};typeof c.input_tokens=="number"&&(u.promptTokenCount=c.input_tokens),typeof c.output_tokens=="number"&&(u.responseTokenCount=c.output_tokens),typeof c.total_tokens=="number"&&(u.totalTokenCount=c.total_tokens),Object.keys(u).length>0&&(d.usageMetadata=u)}return a.push(d),{messages:a,assistantItems:r}}function D(e,t,n,o){let r=e.response??{},i={request_id:n,consumer_id:o,stream:!0,kind:"chat",messages:t};return typeof r.instructions=="string"&&(i.instructions=r.instructions),Array.isArray(r.modalities)&&(i.modalities=r.modalities),typeof r.temperature=="number"&&(i.temperature=r.temperature),typeof r.max_output_tokens=="number"&&(i.max_output_tokens=r.max_output_tokens),r.tools!==void 0&&(i.tools=r.tools),i}function N(e,t,n){let o={request_id:t,consumer_id:n,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(o.model=e.model),e.tools!==void 0&&(o.tools=e.tools),e.tool_choice!==void 0&&(o.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(o.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(o.max_output_tokens=e.max_output_tokens),typeof e.top_p=="number"&&(o.top_p=e.top_p),Array.isArray(e.stop)&&e.stop.length>0&&(o.stop=e.stop),typeof e.reasoning_effort=="string"&&(o.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(o.chat_template_kwargs=e.chat_template_kwargs),o}function we(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 t=e,n=t.body??{};return{type:"error",error:{type:"urun_error",code:t.code??null,message:n.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var Me="llm-resp";async function*v(e,t){let n=`${Me}:${t}`,o=`resp_${t}`;yield{type:"response.created",response:{id:o,status:"in_progress"}};let r=new Map;for await(let i of e.stream(n).messages()){let a=i;if(a.t==="delta"){if(typeof a.delta=="string"&&(yield{type:"response.output_text.delta",item_id:o,delta:a.delta}),typeof a.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:o,delta:a.reasoning}),Array.isArray(a.tool_calls))for(let c of a.tool_calls){let d=typeof c.index=="number"?c.index:0,u=r.get(d)??{};c.id&&(u.id=c.id),c.function?.name&&(u.name=c.function.name),r.set(d,u),yield{type:"response.function_call_arguments.delta",item_id:`fc_${t}_${d}`,tool_index:d,call_id:u.id,name:u.name,delta:c.function?.arguments??""}}}else if(a.t==="response"){yield{type:"response.completed",response:{id:o,status:"completed",...a.body}};return}else if(a.t==="error"){yield we(a);return}}}var $e="llm",M=class{constructor(t){this.session=t;this.sessionTag=t.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(t){t.session_tag=this.sessionTag,this.session.doc($e).set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,n){let o=N(t,n,this.consumerId);return this.write(o),v(this.session,n)}sendResponseCreate(t,n,o){let r=D(t,n,o,this.consumerId);return this.write(r),v(this.session,o)}};0&&(module.exports={AudioBridge,CLOSE_PROTOCOL,CLOSE_SESSION_GONE,CLOSE_UNKNOWN_MODEL,CLOSE_UPSTREAM,GEMINI_AUDIO_IN_RATE,GEMINI_AUDIO_OUT_MIME,GEMINI_AUDIO_OUT_RATE,GEMINI_LIVE_PATH,GeminiLiveError,NATIVE_AUDIO_SAMPLE_RATE,OUTBOUND_AUDIO_FLUSH_B64,RT_VIDEO_IN_STREAM,RT_VIDEO_OUT_STREAM,SdkTransport,VIDEO_FRAME_MESSAGE_BUDGET,VIDEO_OUT_DEFAULT_CODEC,VIDEO_OUT_FRAMING,VIDEO_OUT_SETUP_KEY,VIDEO_OUT_WS_FRAMING,consumeVideoOut,decodeResponsesLane,decodeVideoOutRecord,enableSessionAudio,enableSessionVideo,encodeVideoOutRecord,goAwayMsg,negotiateSessionVideoOutQuic,nodeAudioBackend,openSessionVideoOutLane,parseClientMessage,resamplePcm16,responseCreateToEnvelope,responsesCreateToEnvelope,serverContentOfAudio,serverContentOfDelta,sessionResumptionUpdateMsg,translateClientContent,translateRealtimeInput,translateSetup,translateToolResponse,turnEndOfCompleted,videoOutAck,videoOutChunkMsg,weriftAudioBackend});
1
+ "use strict";var be=Object.create;var E=Object.defineProperty;var ke=Object.getOwnPropertyDescriptor;var _e=Object.getOwnPropertyNames;var Oe=Object.getPrototypeOf,Ae=Object.prototype.hasOwnProperty;var ve=(e,t)=>{for(var n in t)E(e,n,{get:t[n],enumerable:!0})},F=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of _e(t))!Ae.call(e,r)&&r!==n&&E(e,r,{get:()=>t[r],enumerable:!(o=ke(t,r))||o.enumerable});return e};var $=(e,t,n)=>(n=e!=null?be(Oe(e)):{},F(t||!e||!e.__esModule?E(n,"default",{value:e,enumerable:!0}):n,e)),Ee=e=>F(E({},"__esModule",{value:!0}),e);var Be={};ve(Be,{AudioBridge:()=>A,CLOSE_PROTOCOL:()=>B,CLOSE_SESSION_GONE:()=>oe,CLOSE_UNKNOWN_MODEL:()=>ne,CLOSE_UPSTREAM:()=>L,GEMINI_AUDIO_IN_RATE:()=>_,GEMINI_AUDIO_OUT_MIME:()=>H,GEMINI_AUDIO_OUT_RATE:()=>V,GEMINI_LIVE_PATH:()=>te,GeminiLiveError:()=>s,NATIVE_AUDIO_SAMPLE_RATE:()=>I,OUTBOUND_AUDIO_FLUSH_B64:()=>fe,RT_VIDEO_IN_STREAM:()=>S,RT_VIDEO_OUT_STREAM:()=>k,SdkTransport:()=>M,VIDEO_FRAME_MESSAGE_BUDGET:()=>R,VIDEO_OUT_DEFAULT_CODEC:()=>T,VIDEO_OUT_FRAMING:()=>P,VIDEO_OUT_SETUP_KEY:()=>b,VIDEO_OUT_WS_FRAMING:()=>C,consumeVideoOut:()=>Z,decodeResponsesLane:()=>v,decodeVideoOutRecord:()=>x,enableSessionAudio:()=>G,enableSessionVideo:()=>J,encodeVideoOutRecord:()=>Q,goAwayMsg:()=>ce,negotiateSessionVideoOutQuic:()=>z,nodeAudioBackend:()=>Y,openSessionVideoOutLane:()=>X,parseClientMessage:()=>re,resamplePcm16:()=>j,responseCreateToEnvelope:()=>D,responsesCreateToEnvelope:()=>N,serverContentOfAudio:()=>ye,serverContentOfDelta:()=>ge,sessionResumptionUpdateMsg:()=>de,translateClientContent:()=>le,translateRealtimeInput:()=>pe,translateSetup:()=>ue,translateToolResponse:()=>me,turnEndOfCompleted:()=>we,videoOutAck:()=>se,videoOutChunkMsg:()=>ae,weriftAudioBackend:()=>q});module.exports=Ee(Be);var I=24e3,W="audio",A=class{constructor(t,n){this.backend=t;this.opts=n}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(t){if(!this.source)throw new Error("startOutbound() not called");let n=Buffer.from(t,"base64"),o=Math.floor(n.byteLength/2),r=new Int16Array(o);for(let i=0;i<o;i++)r[i]=n.readInt16LE(i*2);this.source.onData(r)}async startInbound(t){if(t==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("audio").track is null)');this.sink=this.backend.createSink(t),this.sink.onframe=n=>{let o=Buffer.from(n.samples.buffer,n.samples.byteOffset,n.samples.byteLength).toString("base64");for(let r of this.outHandlers)r(o)}}onOutputAudio(t){return this.outHandlers.add(t),()=>this.outHandlers.delete(t)}};async function G(e,t,n=1e4){let o=new A(t,{sampleRate:24e3}),r=await o.startOutbound();await e.stream(W).attach(r);let i=await Se(e,n);return await o.startInbound(i),o}function Se(e,t){let n=e.stream(W);return n.track!=null?Promise.resolve(n.track):new Promise((o,r)=>{let i,a=setTimeout(()=>{i?.(),r(new Error(`enableSessionAudio: no downstream audio track within ${t}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},t),c=d=>{d!=null&&(clearTimeout(a),i?.(),o(d))};i=n.on?.("track",c),n.track!=null&&c(n.track)})}var S="rt-video-in",R=256e3;function J(e){let t=e.stream(S),n=t.emit;if(typeof n!="function")throw new Error(`enableSessionVideo: session.stream('${S}') 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 o=>{if(o.byteLength===0)throw new Error("enableSessionVideo: refusing to send an empty video frame");if(o.byteLength>R)throw new Error(`enableSessionVideo: frame is ${o.byteLength} bytes \u2014 over the ${R}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await n.call(t,o)}}}var Re=24e3,O=480,Ie=111;async function Te(){let e=await import("opusscript"),t=e.default??e;return new t(Re,1,2048)}async function q(){let e=await import("werift"),t=await Te();return{createSource:()=>{let n=new e.MediaStreamTrack({kind:"audio"}),o=Math.random()*4294967295>>>0,r=Math.random()*65535&65535,i=Math.random()*4294967295>>>0,a=new Int16Array(0);return{track:n,onData:c=>{let d=new Int16Array(a.length+c.length);d.set(a,0),d.set(c,a.length);let u=0;for(;d.length-u>=O;){let l=d.subarray(u,u+O);u+=O;let f=Buffer.from(l.buffer,l.byteOffset,l.byteLength),p=t.encode(f,O);r=r+1&65535,i=i+O>>>0;let g=new e.RtpHeader({version:2,payloadType:Ie,sequenceNumber:r,timestamp:i,ssrc:o,marker:!1}),w=new e.RtpPacket(g,p);n.writeRtp(w)}a=d.slice(u)}}},createSink:n=>{let o={onframe:null};return n.onReceiveRtp.subscribe(i=>{let a;try{a=t.decode(i.payload)}catch{return}let c=new Int16Array(a.length/2);for(let d=0;d<c.length;d++)c[d]=a.readInt16LE(d*2);o.onframe?.({samples:c})}),o}}}async function Y(){let e=await import("@roamhq/wrtc");return{createSource:()=>{let t=new e.nonstandard.RTCAudioSource;return{track:t.createTrack(),onData:o=>t.onData({samples:o,sampleRate:24e3})}},createSink:t=>{let n={onframe:null},o=new e.nonstandard.RTCAudioSink(t);return o.ondata=r=>{n.onframe?.(r)},n}}}var y=require("@urun-sh/core"),k="rt-video-out",P="urun.videoOut.v1",T="h264";function K(e,t){if(!Number.isInteger(e.seq)||e.seq<0)throw new Error(`${t}: header seq must be a non-negative integer (got ${e.seq})`);if(!Number.isInteger(e.ptsUs))throw new Error(`${t}: header ptsUs must be an integer microsecond timestamp (got ${e.ptsUs})`);if(typeof e.codec!="string"||e.codec.length===0)throw new Error(`${t}: header codec must be a non-empty string`);if(typeof e.key!="boolean")throw new Error(`${t}: header key must be a boolean`)}function Q(e,t){if(K(e,"encodeVideoOutRecord"),t.byteLength===0)throw new Error("encodeVideoOutRecord: refusing to encode an empty access unit");let n=new TextEncoder().encode(JSON.stringify({v:1,seq:e.seq,ptsUs:e.ptsUs,codec:e.codec,key:e.key}));if(n.byteLength>65535)throw new Error(`encodeVideoOutRecord: header JSON is ${n.byteLength} bytes \u2014 over the u16 length prefix`);let o=new Uint8Array(2+n.byteLength+t.byteLength);return o[0]=n.byteLength&255,o[1]=n.byteLength>>8,o.set(n,2),o.set(t,2+n.byteLength),o}function x(e){if(e.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${e.byteLength} byte(s), no header length)`);let t=e[0]|e[1]<<8;if(e.byteLength<2+t)throw new Error(`decodeVideoOutRecord: truncated record (header says ${t} bytes, ${e.byteLength-2} present)`);let n;try{n=JSON.parse(new TextDecoder().decode(e.subarray(2,2+t)))}catch(a){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(a)})`)}let o=n;if(o===null||typeof o!="object"||o.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(o?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let r=o;K(r,"decodeVideoOutRecord");let i=e.subarray(2+t);if(i.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:r,au:i.slice()}}async function z(e){let t=e.wtDataEndpoint;if(typeof t!="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 n=await t.call(e),o=n.sessionId??e.sessionId;if(typeof o!="string"||o.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:n.url,certHashes:n.certHashes,token:n.token,sessionId:o,stream:k,codec:T,framing:P}}var C="urun.videoOut.v2-ws";function X(e){let t=e.stream(k);if(typeof t.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${k}') 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 n=new Set,o=new Set,r=new Set,i=d=>{for(let u of o)u(d)},a=!1,c=async()=>{for await(let d of t.messages()){if(!(d instanceof Uint8Array)){i(new Error(`openSessionVideoOutLane: "${k}" delivered a non-BINARY \xA75 payload (${typeof d}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let u;try{u=x(d)}catch(l){i(l instanceof Error?l:new Error(String(l)));continue}for(let l of n)l(u)}for(let d of r)d()};return{codec:T,onOutputFrame:d=>(n.add(d),a||(a=!0,c().catch(u=>i(u instanceof Error?u:new Error(String(u))))),()=>n.delete(d)),onLaneError:d=>(o.add(d),()=>o.delete(d)),onEnd:d=>(r.add(d),()=>r.delete(d))}}function xe(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 t=new Uint8Array(e.length/2);for(let n=0;n<t.length;n++)t[n]=parseInt(e.slice(n*2,n*2+2),16);return t}async function Z(e,t,n){let o=n??(typeof globalThis.WebTransport=="function"?(u,l)=>new globalThis.WebTransport(u,l):void 0);if(!o)throw new Error("consumeVideoOut: no WebTransport implementation \u2014 this platform has no globalThis.WebTransport; inject a connect factory (the third argument)");let r=o(e.url,{serverCertificateHashes:e.certHashes.map(u=>({algorithm:"sha-256",value:xe(u)}))});await r.ready;let i=await r.createBidirectionalStream();await i.writable.getWriter().write((0,y.encodeWtControlFrame)({t:"hello",token:e.token,sessionId:e.sessionId}));let a=i.readable.getReader(),c=new y.WtControlDecoder,d=!1;for(;!d;){let{value:u,done:l}=await a.read();if(l)throw new Error("consumeVideoOut: gateway closed the control stream before hello-ack");if(u)for(let f of c.push(u)){if(f.t==="hello-ack"){d=!0;break}throw new Error(`consumeVideoOut: gateway rejected hello (got ${JSON.stringify(f.t)} before hello-ack)`)}}await(0,y.consumeWtDataStreams)(r.incomingUnidirectionalStreams,{onData:(u,l)=>{if(u===e.stream){if(l===y.STREAM_END){t.onEnd?.();return}if(!(l instanceof Uint8Array)){t.onLaneError(new Error(`consumeVideoOut: "${e.stream}" delivered a non-BINARY \xA75 payload (${typeof l}) \u2014 spec v1 records ride BINARY envelopes only`));return}try{t.onFrame(x(l))}catch(f){t.onLaneError(f instanceof Error?f:new Error(String(f)))}}},onLaneError:u=>t.onLaneError(u)})}var te="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",B=1007,ne=1008,L=1011,oe=1008,s=class extends Error{constructor(n,o=B){super(n);this.closeCode=o}closeCode},m=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)?e:null,U=["setup","clientContent","realtimeInput","toolResponse"];function re(e){let t;try{t=JSON.parse(e)}catch{throw new s("client message is not valid JSON")}let n=m(t);if(!n)throw new s("client message must be a JSON object");let o=U.filter(c=>n[c]!==void 0),r=Object.keys(n).filter(c=>!U.includes(c));if(r.length>0)throw new s(`unknown client message field(s) ${JSON.stringify(r)} \u2014 expected exactly one of ${U.join(", ")}`);if(o.length!==1)throw new s(`client message must contain exactly one of ${U.join(", ")} (got ${o.length})`);let i=o[0],a=m(n[i]);if(!a)throw new s(`"${i}" must be a JSON object`);return{kind:i,[i]:a}}function ie(e,t){if(typeof e=="string")return e;let n=m(e),o=n?n.parts:void 0;if(!Array.isArray(o))throw new s(`${t}: expected a Content with a parts array`);let r=[];for(let i of o){let a=m(i);if(a&&typeof a.text=="string"){r.push(a.text);continue}throw a&&(a.inlineData!==void 0||a.fileData!==void 0)?new s(`${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 s(`${t}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new s(`${t}: unsupported part ${JSON.stringify(i)}`)}return r.join(`
2
+ `)}var Ce={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},b="urun.videoOut";function se(e){return{framing:C,codec:e}}function ae(e){let{seq:t,ptsUs:n,codec:o,key:r}=e.header;return{[b]:{v:1,seq:t,ptsUs:n,codec:o,key:r,data:Buffer.from(e.au).toString("base64")}}}var Ue={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Le=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",b],ee=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function Ve(e){let t=m(e);if(!t)throw new s("setup.realtimeInputConfig must be an object");for(let r of Object.keys(t))if(!(r==="automaticActivityDetection"||r==="activityHandling"))throw new s(r==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${r}"`);let n=m(t.automaticActivityDetection);if(t.automaticActivityDetection!==void 0&&!n)throw new s("realtimeInputConfig.automaticActivityDetection must be an object");for(let r of Object.keys(n??{}))if(r!=="disabled")throw new s(`automaticActivityDetection.${r} tunes AUTOMATIC (server-side) activity detection \u2014 the declared platform follow-up (serve turn_detection events over the session stream lane); only "disabled: true" (manual activity mode) is supported in this slice`);if(n?.disabled!==!0)throw new s("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let o="START_OF_ACTIVITY_INTERRUPTS";if(t.activityHandling!==void 0){if(!ee.includes(t.activityHandling))throw new s(`unsupported activityHandling ${JSON.stringify(t.activityHandling)} \u2014 expected one of ${ee.join(", ")}`);o=t.activityHandling}return{manualActivity:!0,activityHandling:o}}var De=["temperature","maxOutputTokens","responseModalities"];function ue(e){for(let[f,p]of Object.entries(Ue))if(e[f]!==void 0)throw new s(p);for(let f of Object.keys(e))if(!Le.includes(f))throw new s(`unsupported setup field "${f}"`);if(typeof e.model!="string"||e.model.length===0)throw new s('setup.model is required (format "models/{model}")');let t=e.model.replace(/^models\//,""),n,o,r=!1,i=m(e.generationConfig);if(e.generationConfig!==void 0&&!i)throw new s("setup.generationConfig must be an object");if(i){for(let f of Object.keys(i))if(!De.includes(f))throw new s(`unsupported generationConfig field "${f}"`);if(i.temperature!==void 0){if(typeof i.temperature!="number")throw new s("generationConfig.temperature must be a number");n=i.temperature}if(i.maxOutputTokens!==void 0){if(typeof i.maxOutputTokens!="number")throw new s("generationConfig.maxOutputTokens must be a number");o=i.maxOutputTokens}if(i.responseModalities!==void 0){let f=Array.isArray(i.responseModalities)?i.responseModalities:[i.responseModalities];for(let p of f){let g=String(p).toUpperCase();if(g==="AUDIO"){r=!0;continue}if(g!=="TEXT")throw new s(`responseModalities ${JSON.stringify(p)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge 'audio' lane)`)}}}let a=[];e.systemInstruction!==void 0&&a.push({role:"system",content:ie(e.systemInstruction,"setup.systemInstruction")});let c;if(e.tools!==void 0){if(!Array.isArray(e.tools))throw new s("setup.tools must be an array");c=[];for(let f of e.tools){let p=m(f);if(!p)throw new s("setup.tools entries must be objects");for(let g of Object.keys(p))if(g!=="functionDeclarations")throw new s(`unsupported tool "${g}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(p.functionDeclarations))throw new s("tool.functionDeclarations must be an array");for(let g of p.functionDeclarations){let w=m(g);if(!w||typeof w.name!="string")throw new s("functionDeclarations entries require a string name");c.push({type:"function",name:w.name,description:w.description,parameters:w.parameters})}}c.length===0&&(c=void 0)}let d={enabled:!1,handle:null};if(e.sessionResumption!==void 0){let f=m(e.sessionResumption);if(!f)throw new s("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let p of Object.keys(f))if(p!=="handle")throw new s(`unsupported sessionResumption field "${p}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(f.handle!==void 0&&(typeof f.handle!="string"||f.handle.length===0))throw new s("sessionResumption.handle must be a non-empty string when present");d={enabled:!0,handle:typeof f.handle=="string"?f.handle:null}}let u=e.realtimeInputConfig===void 0?Ce:Ve(e.realtimeInputConfig),l=!1;if(e[b]!==void 0){if(!m(e[b]))throw new s(`setup["${b}"] must be an object (the v1 opt-in is {})`);l=!0}return{model:t,systemItems:a,tools:c,temperature:n,maxOutputTokens:o,audioModality:r,activity:u,sessionResumption:d,videoOut:l}}function de(e){return{sessionResumptionUpdate:{newHandle:e,resumable:!0}}}function ce(e){let t=Math.max(0,e??0)/1e3;return{goAway:{timeLeft:`${Math.round(t*1e3)/1e3}s`}}}function le(e){for(let o of Object.keys(e))if(o!=="turns"&&o!=="turnComplete")throw new s(`unsupported clientContent field "${o}"`);let t=e.turns===void 0?[]:e.turns;if(!Array.isArray(t))throw new s("clientContent.turns must be an array");let n=[];for(let o of t){let r=m(o);if(!r)throw new s("clientContent.turns entries must be Content objects");let i=r.role===void 0?"user":String(r.role).toLowerCase();if(i!=="user"&&i!=="model")throw new s(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(r.role)})`);n.push({role:i==="model"?"assistant":"user",content:ie(r,"clientContent.turns")})}return{items:n,turnComplete:e.turnComplete===!0}}var _=16e3,V=24e3,H=`audio/pcm;rate=${V}`,fe=Buffer.alloc(960).toString("base64");function j(e,t,n){if(t===n)return e;let o=Math.floor(e.length*n/t),r=new Int16Array(o),i=t/n,a=e.length-1;for(let c=0;c<o;c++){let d=c*i,u=Math.floor(d);if(u>=a){r[c]=e[a];continue}let l=d-u;r[c]=Math.round(e[u]+(e[u+1]-e[u])*l)}return r}function Ne(e){let t=m(e);if(!t)throw new s("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof t.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(t.mimeType))throw new s(`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 s("realtimeInput.video.data must be non-empty base64");let n=Buffer.from(t.data,"base64");if(n.length<3||n[0]!==255||n[1]!==216)throw new s("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function Me(e){let t=m(e);if(!t)throw new s("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof t.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(t.mimeType))throw new s(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(t.mimeType)}`);let n=/;\s*rate=(\d+)/.exec(t.mimeType);if(n&&Number(n[1])!==_)throw new s(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${_} Hz (audio/pcm;rate=${_})`);if(typeof t.data!="string"||t.data.length===0)throw new s("realtimeInput.audio.data must be non-empty base64");let o=Buffer.from(t.data,"base64");if(o.length===0||o.length%2!==0)throw new s(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${o.length} bytes \u2014 not even)`);let r=new Int16Array(o.length/2);for(let a=0;a<r.length;a++)r[a]=o.readInt16LE(a*2);let i=j(r,_,V);return Buffer.from(i.buffer,i.byteOffset,i.byteLength).toString("base64")}function pe(e){if(e.mediaChunks!==void 0)throw new s('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 s("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let r=e.activityStart!==void 0?"activityStart":"activityEnd",i=m(e[r]);if(!i||Object.keys(i).length!==0)throw new s(`realtimeInput.${r} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:r}}let t=["text","audio","audioStreamEnd","video"];for(let o of Object.keys(e))if(!t.includes(o))throw new s(`unsupported realtimeInput field "${o}"`);let n=t.filter(o=>e[o]!==void 0);if(n.length!==1)throw new s(`realtimeInput must carry exactly one of ${t.join(", ")} (got ${n.length})`);if(e.audio!==void 0)return{kind:"audio",base64Pcm24k:Me(e.audio)};if(e.video!==void 0)return{kind:"video",jpegFrame:Ne(e.video)};if(e.audioStreamEnd!==void 0){if(e.audioStreamEnd!==!0)throw new s("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof e.text!="string"||e.text.length===0)throw new s("realtimeInput.text must be a non-empty string");return{kind:"text",text:e.text}}function me(e){for(let t of Object.keys(e))if(t!=="functionResponses")throw new s(`unsupported toolResponse field "${t}"`);if(!Array.isArray(e.functionResponses)||e.functionResponses.length===0)throw new s("toolResponse.functionResponses must be a non-empty array");return e.functionResponses.map(t=>{let n=m(t);if(!n||typeof n.id!="string"||n.id.length===0)throw new s("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:n.id,output:JSON.stringify(n.response??null)}})}function ge(e){return e.type!=="response.output_text.delta"||typeof e.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:e.delta}]}}}}function ye(e){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:H,data:e}}]}}}}function we(e){let t=m(e)??{},n=Array.isArray(t.output)?t.output:[],o=[],r=[],i=typeof t.output_text=="string"?t.output_text:"";if(!i)for(let u of n){let l=m(u);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let f of l.content){let p=m(f);p?.type==="output_text"&&typeof p.text=="string"&&(i+=p.text)}}i&&r.push({role:"assistant",content:i});for(let u of n){let l=m(u);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new s(`upstream function_call item is missing call_id/name: ${JSON.stringify(u)}`,L);let f=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",p;try{p=JSON.parse(f)}catch{throw new s(`upstream function_call arguments are not valid JSON: ${f}`,L)}o.push({id:l.call_id,name:l.name,args:p}),r.push({type:"function_call",call_id:l.call_id,name:l.name,arguments:f})}let a=[];o.length>0&&a.push({toolCall:{functionCalls:o}}),a.push({serverContent:{generationComplete:!0}});let c=m(t.usage),d={serverContent:{turnComplete:!0}};if(c){let u={};typeof c.input_tokens=="number"&&(u.promptTokenCount=c.input_tokens),typeof c.output_tokens=="number"&&(u.responseTokenCount=c.output_tokens),typeof c.total_tokens=="number"&&(u.totalTokenCount=c.total_tokens),Object.keys(u).length>0&&(d.usageMetadata=u)}return a.push(d),{messages:a,assistantItems:r}}function D(e,t,n,o){let r=e.response??{},i={request_id:n,consumer_id:o,stream:!0,kind:"chat",messages:t};return typeof r.instructions=="string"&&(i.instructions=r.instructions),Array.isArray(r.modalities)&&(i.modalities=r.modalities),typeof r.temperature=="number"&&(i.temperature=r.temperature),typeof r.max_output_tokens=="number"&&(i.max_output_tokens=r.max_output_tokens),r.tools!==void 0&&(i.tools=r.tools),i}function N(e,t,n){let o={request_id:t,consumer_id:n,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(o.model=e.model),e.tools!==void 0&&(o.tools=e.tools),e.tool_choice!==void 0&&(o.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(o.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(o.max_output_tokens=e.max_output_tokens),typeof e.top_p=="number"&&(o.top_p=e.top_p),Array.isArray(e.stop)&&e.stop.length>0&&(o.stop=e.stop),typeof e.reasoning_effort=="string"&&(o.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(o.chat_template_kwargs=e.chat_template_kwargs),e.response_format!==void 0&&(o.response_format=e.response_format),o}function he(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 t=e,n=t.body??{},o=n.error,r=o?.message??n.message??`unreadable serve error body: ${JSON.stringify(t.body??null)}`;return{type:"error",error:{type:o?.type??"urun_error",code:t.code??o?.code??null,message:r}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var $e="llm-resp";async function*v(e,t){let n=`${$e}:${t}`,o=`resp_${t}`;yield{type:"response.created",response:{id:o,status:"in_progress"}};let r=new Map;for await(let i of e.stream(n).messages()){let a=i;if(a.t==="delta"){if(typeof a.delta=="string"&&(yield{type:"response.output_text.delta",item_id:o,delta:a.delta}),typeof a.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:o,delta:a.reasoning}),Array.isArray(a.tool_calls))for(let c of a.tool_calls){let d=typeof c.index=="number"?c.index:0,u=r.get(d)??{};c.id&&(u.id=c.id),c.function?.name&&(u.name=c.function.name),r.set(d,u),yield{type:"response.function_call_arguments.delta",item_id:`fc_${t}_${d}`,tool_index:d,call_id:u.id,name:u.name,delta:c.function?.arguments??""}}}else if(a.t==="response"){yield{type:"response.completed",response:{id:o,status:"completed",...a.body}};return}else if(a.t==="error"){yield he(a);return}}}var Pe="llm",M=class{constructor(t){this.session=t;this.sessionTag=t.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(t){t.session_tag=this.sessionTag,this.session.doc(Pe).set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,n){let o=N(t,n,this.consumerId);return this.write(o),v(this.session,n)}sendResponseCreate(t,n,o){let r=D(t,n,o,this.consumerId);return this.write(r),v(this.session,o)}};0&&(module.exports={AudioBridge,CLOSE_PROTOCOL,CLOSE_SESSION_GONE,CLOSE_UNKNOWN_MODEL,CLOSE_UPSTREAM,GEMINI_AUDIO_IN_RATE,GEMINI_AUDIO_OUT_MIME,GEMINI_AUDIO_OUT_RATE,GEMINI_LIVE_PATH,GeminiLiveError,NATIVE_AUDIO_SAMPLE_RATE,OUTBOUND_AUDIO_FLUSH_B64,RT_VIDEO_IN_STREAM,RT_VIDEO_OUT_STREAM,SdkTransport,VIDEO_FRAME_MESSAGE_BUDGET,VIDEO_OUT_DEFAULT_CODEC,VIDEO_OUT_FRAMING,VIDEO_OUT_SETUP_KEY,VIDEO_OUT_WS_FRAMING,consumeVideoOut,decodeResponsesLane,decodeVideoOutRecord,enableSessionAudio,enableSessionVideo,encodeVideoOutRecord,goAwayMsg,negotiateSessionVideoOutQuic,nodeAudioBackend,openSessionVideoOutLane,parseClientMessage,resamplePcm16,responseCreateToEnvelope,responsesCreateToEnvelope,serverContentOfAudio,serverContentOfDelta,sessionResumptionUpdateMsg,translateClientContent,translateRealtimeInput,translateSetup,translateToolResponse,turnEndOfCompleted,videoOutAck,videoOutChunkMsg,weriftAudioBackend});
@@ -1,7 +1,7 @@
1
- export { A as ActivityConfig, a as ActivityHandling, C as CLOSE_PROTOCOL, b as CLOSE_SESSION_GONE, c as CLOSE_UNKNOWN_MODEL, d as CLOSE_UPSTREAM, e as ClientMessage, G as GEMINI_AUDIO_IN_RATE, f as GEMINI_AUDIO_OUT_MIME, g as GEMINI_AUDIO_OUT_RATE, h as GEMINI_LIVE_PATH, i as GeminiLiveError, L as LiveSessionConfig, O as OUTBOUND_AUDIO_FLUSH_B64, R as RealtimeInputAction, T as TurnEnd, V as VIDEO_OUT_SETUP_KEY, j as goAwayMsg, p as parseClientMessage, r as resamplePcm16, s as serverContentOfAudio, k as serverContentOfDelta, l as sessionResumptionUpdateMsg, t as translateClientContent, m as translateRealtimeInput, n as translateSetup, o as translateToolResponse, q as turnEndOfCompleted, v as videoOutAck, u as videoOutChunkMsg } from './translator-CcDBEfvm.cjs';
1
+ export { A as ActivityConfig, a as ActivityHandling, C as CLOSE_PROTOCOL, b as CLOSE_SESSION_GONE, c as CLOSE_UNKNOWN_MODEL, d as CLOSE_UPSTREAM, e as ClientMessage, G as GEMINI_AUDIO_IN_RATE, f as GEMINI_AUDIO_OUT_MIME, g as GEMINI_AUDIO_OUT_RATE, h as GEMINI_LIVE_PATH, i as GeminiLiveError, L as LiveSessionConfig, O as OUTBOUND_AUDIO_FLUSH_B64, R as RealtimeInputAction, T as TurnEnd, V as VIDEO_OUT_SETUP_KEY, j as goAwayMsg, p as parseClientMessage, r as resamplePcm16, s as serverContentOfAudio, k as serverContentOfDelta, l as sessionResumptionUpdateMsg, t as translateClientContent, m as translateRealtimeInput, n as translateSetup, o as translateToolResponse, q as turnEndOfCompleted, v as videoOutAck, u as videoOutChunkMsg } from './translator-Bh0Bp_Ie.cjs';
2
2
  import { b as UrunSessionLike } from './types-lsVTbNcH.cjs';
3
3
  export { L as LaneMessage, a as LaneToolCallDelta, U as UrunDocLike, c as UrunStreamLike } from './types-lsVTbNcH.cjs';
4
- export { A as AudioBackend, a as AudioBridge, b as AudioSink, c as AudioSource, N as NATIVE_AUDIO_SAMPLE_RATE, R as RT_VIDEO_IN_STREAM, d as RT_VIDEO_OUT_STREAM, V as VIDEO_FRAME_MESSAGE_BUDGET, e as VIDEO_OUT_DEFAULT_CODEC, f as VIDEO_OUT_FRAMING, g as VIDEO_OUT_WS_FRAMING, h as VideoFrameLane, i as VideoOutEndpoint, j as VideoOutLane, k as VideoOutRecord, l as VideoOutRecordHeader, m as VideoOutTransportSink, n as VideoOutWtConnect, o as VideoOutWtSession, p as consumeVideoOut, q as decodeVideoOutRecord, r as enableSessionAudio, s as enableSessionVideo, t as encodeVideoOutRecord, u as negotiateSessionVideoOutQuic, v as nodeAudioBackend, w as openSessionVideoOutLane, x as weriftAudioBackend } from './video-out-D20UuJ8G.cjs';
4
+ export { A as AudioBackend, a as AudioBridge, b as AudioSink, c as AudioSource, N as NATIVE_AUDIO_SAMPLE_RATE, R as RT_VIDEO_IN_STREAM, d as RT_VIDEO_OUT_STREAM, V as VIDEO_FRAME_MESSAGE_BUDGET, e as VIDEO_OUT_DEFAULT_CODEC, f as VIDEO_OUT_FRAMING, g as VIDEO_OUT_WS_FRAMING, h as VideoFrameLane, i as VideoOutEndpoint, j as VideoOutLane, k as VideoOutRecord, l as VideoOutRecordHeader, m as VideoOutTransportSink, n as VideoOutWtConnect, o as VideoOutWtSession, p as consumeVideoOut, q as decodeVideoOutRecord, r as enableSessionAudio, s as enableSessionVideo, t as encodeVideoOutRecord, u as negotiateSessionVideoOutQuic, v as nodeAudioBackend, w as openSessionVideoOutLane, x as weriftAudioBackend } from './video-out-CCksIcj8.cjs';
5
5
 
6
6
  declare class SdkTransport {
7
7
  private readonly session;
@@ -29,6 +29,8 @@ declare class SdkTransport {
29
29
  max_output_tokens?: number;
30
30
  reasoning_effort?: string;
31
31
  chat_template_kwargs?: Record<string, unknown>;
32
+ /** ENG-310 structured output: forwarded verbatim; the serve runtime validates. */
33
+ response_format?: unknown;
32
34
  }, requestId: string): AsyncIterable<unknown>;
33
35
  sendResponseCreate(ev: {
34
36
  type: 'response.create';
@@ -90,6 +92,18 @@ interface ServeEnvelope {
90
92
  */
91
93
  reasoning_effort?: string;
92
94
  chat_template_kwargs?: Record<string, unknown>;
95
+ /**
96
+ * OpenAI `response_format` — STRUCTURED OUTPUT (ENG-310). Forwarded VERBATIM
97
+ * to the serve envelope, where it becomes engine-native CONSTRAINED DECODING
98
+ * (vLLM `structured_outputs`, SGLang `json_schema`) — never a prompt-level
99
+ * "answer in JSON" hint. `unknown` on purpose: the serve runtime's
100
+ * `parse_request` is the ONE validator (it accepts both the Chat-Completions
101
+ * nested `json_schema` spelling and the Responses flat one, and rejects
102
+ * everything else with a typed 400), so the proxy passes the caller's value
103
+ * through instead of growing a second, drifting copy of those rules.
104
+ * Absent -> key absent: today's unconstrained request, byte for byte.
105
+ */
106
+ response_format?: unknown;
93
107
  /**
94
108
  * Session-affinity tag (urun-python #1556, additive like `tools` in the
95
109
  * #1557 lane pair): the stable identity of THIS persistent uRun session.
@@ -121,6 +135,7 @@ declare function responsesCreateToEnvelope(params: {
121
135
  max_output_tokens?: number;
122
136
  reasoning_effort?: string;
123
137
  chat_template_kwargs?: Record<string, unknown>;
138
+ response_format?: unknown;
124
139
  }, requestId: string, consumerId: string): ServeEnvelope;
125
140
 
126
141
  export { SdkTransport, type ServeEnvelope, UrunSessionLike, decodeResponsesLane, responseCreateToEnvelope, responsesCreateToEnvelope };
@@ -1,7 +1,7 @@
1
- export { A as ActivityConfig, a as ActivityHandling, C as CLOSE_PROTOCOL, b as CLOSE_SESSION_GONE, c as CLOSE_UNKNOWN_MODEL, d as CLOSE_UPSTREAM, e as ClientMessage, G as GEMINI_AUDIO_IN_RATE, f as GEMINI_AUDIO_OUT_MIME, g as GEMINI_AUDIO_OUT_RATE, h as GEMINI_LIVE_PATH, i as GeminiLiveError, L as LiveSessionConfig, O as OUTBOUND_AUDIO_FLUSH_B64, R as RealtimeInputAction, T as TurnEnd, V as VIDEO_OUT_SETUP_KEY, j as goAwayMsg, p as parseClientMessage, r as resamplePcm16, s as serverContentOfAudio, k as serverContentOfDelta, l as sessionResumptionUpdateMsg, t as translateClientContent, m as translateRealtimeInput, n as translateSetup, o as translateToolResponse, q as turnEndOfCompleted, v as videoOutAck, u as videoOutChunkMsg } from './translator-C9uPKypK.js';
1
+ export { A as ActivityConfig, a as ActivityHandling, C as CLOSE_PROTOCOL, b as CLOSE_SESSION_GONE, c as CLOSE_UNKNOWN_MODEL, d as CLOSE_UPSTREAM, e as ClientMessage, G as GEMINI_AUDIO_IN_RATE, f as GEMINI_AUDIO_OUT_MIME, g as GEMINI_AUDIO_OUT_RATE, h as GEMINI_LIVE_PATH, i as GeminiLiveError, L as LiveSessionConfig, O as OUTBOUND_AUDIO_FLUSH_B64, R as RealtimeInputAction, T as TurnEnd, V as VIDEO_OUT_SETUP_KEY, j as goAwayMsg, p as parseClientMessage, r as resamplePcm16, s as serverContentOfAudio, k as serverContentOfDelta, l as sessionResumptionUpdateMsg, t as translateClientContent, m as translateRealtimeInput, n as translateSetup, o as translateToolResponse, q as turnEndOfCompleted, v as videoOutAck, u as videoOutChunkMsg } from './translator-BCoRFaTs.js';
2
2
  import { b as UrunSessionLike } from './types-lsVTbNcH.js';
3
3
  export { L as LaneMessage, a as LaneToolCallDelta, U as UrunDocLike, c as UrunStreamLike } from './types-lsVTbNcH.js';
4
- export { A as AudioBackend, a as AudioBridge, b as AudioSink, c as AudioSource, N as NATIVE_AUDIO_SAMPLE_RATE, R as RT_VIDEO_IN_STREAM, d as RT_VIDEO_OUT_STREAM, V as VIDEO_FRAME_MESSAGE_BUDGET, e as VIDEO_OUT_DEFAULT_CODEC, f as VIDEO_OUT_FRAMING, g as VIDEO_OUT_WS_FRAMING, h as VideoFrameLane, i as VideoOutEndpoint, j as VideoOutLane, k as VideoOutRecord, l as VideoOutRecordHeader, m as VideoOutTransportSink, n as VideoOutWtConnect, o as VideoOutWtSession, p as consumeVideoOut, q as decodeVideoOutRecord, r as enableSessionAudio, s as enableSessionVideo, t as encodeVideoOutRecord, u as negotiateSessionVideoOutQuic, v as nodeAudioBackend, w as openSessionVideoOutLane, x as weriftAudioBackend } from './video-out-CWesbk12.js';
4
+ export { A as AudioBackend, a as AudioBridge, b as AudioSink, c as AudioSource, N as NATIVE_AUDIO_SAMPLE_RATE, R as RT_VIDEO_IN_STREAM, d as RT_VIDEO_OUT_STREAM, V as VIDEO_FRAME_MESSAGE_BUDGET, e as VIDEO_OUT_DEFAULT_CODEC, f as VIDEO_OUT_FRAMING, g as VIDEO_OUT_WS_FRAMING, h as VideoFrameLane, i as VideoOutEndpoint, j as VideoOutLane, k as VideoOutRecord, l as VideoOutRecordHeader, m as VideoOutTransportSink, n as VideoOutWtConnect, o as VideoOutWtSession, p as consumeVideoOut, q as decodeVideoOutRecord, r as enableSessionAudio, s as enableSessionVideo, t as encodeVideoOutRecord, u as negotiateSessionVideoOutQuic, v as nodeAudioBackend, w as openSessionVideoOutLane, x as weriftAudioBackend } from './video-out-IGQ4YQ-c.js';
5
5
 
6
6
  declare class SdkTransport {
7
7
  private readonly session;
@@ -22,6 +22,8 @@ declare class SdkTransport {
22
22
  max_output_tokens?: number;
23
23
  reasoning_effort?: string;
24
24
  chat_template_kwargs?: Record<string, unknown>;
25
+
26
+ response_format?: unknown;
25
27
  }, requestId: string): AsyncIterable<unknown>;
26
28
  sendResponseCreate(ev: {
27
29
  type: 'response.create';
@@ -57,6 +59,8 @@ interface ServeEnvelope {
57
59
  reasoning_effort?: string;
58
60
  chat_template_kwargs?: Record<string, unknown>;
59
61
 
62
+ response_format?: unknown;
63
+
60
64
  session_tag?: string;
61
65
  }
62
66
 
@@ -80,6 +84,7 @@ declare function responsesCreateToEnvelope(params: {
80
84
  max_output_tokens?: number;
81
85
  reasoning_effort?: string;
82
86
  chat_template_kwargs?: Record<string, unknown>;
87
+ response_format?: unknown;
83
88
  }, requestId: string, consumerId: string): ServeEnvelope;
84
89
 
85
90
  export { SdkTransport, type ServeEnvelope, UrunSessionLike, decodeResponsesLane, responseCreateToEnvelope, responsesCreateToEnvelope };
@@ -1 +1 @@
1
- import{a as c,b as C,c as L,d as M,e as R,f as D,g as f,h as N,i as v,j as g,k as x,l as k,m as y,n as G,o as B,p as F,q as P,r as H,s as W,t as b,u as w,v as K,w as h,x as Q}from"./chunk-5NXM4IO3.js";import{a as i,b as s,c as O,d,e as a,f as E,g as _,h as p,i as u,j as A,k as S,l as I,m as T,n as l,o as m,p as U,q as V}from"./chunk-CWJRDBDC.js";import{a as o,b as t,d as n,e as r}from"./chunk-OI2OY32M.js";import{c as e}from"./chunk-YSFSRI3D.js";e();export{s as AudioBridge,C as CLOSE_PROTOCOL,R as CLOSE_SESSION_GONE,L as CLOSE_UNKNOWN_MODEL,M as CLOSE_UPSTREAM,B as GEMINI_AUDIO_IN_RATE,P as GEMINI_AUDIO_OUT_MIME,F as GEMINI_AUDIO_OUT_RATE,c as GEMINI_LIVE_PATH,D as GeminiLiveError,i as NATIVE_AUDIO_SAMPLE_RATE,H as OUTBOUND_AUDIO_FLUSH_B64,d as RT_VIDEO_IN_STREAM,u as RT_VIDEO_OUT_STREAM,r as SdkTransport,a as VIDEO_FRAME_MESSAGE_BUDGET,S as VIDEO_OUT_DEFAULT_CODEC,A as VIDEO_OUT_FRAMING,N as VIDEO_OUT_SETUP_KEY,m as VIDEO_OUT_WS_FRAMING,V as consumeVideoOut,n as decodeResponsesLane,T as decodeVideoOutRecord,O as enableSessionAudio,E as enableSessionVideo,I as encodeVideoOutRecord,y as goAwayMsg,l as negotiateSessionVideoOutQuic,p as nodeAudioBackend,U as openSessionVideoOutLane,f as parseClientMessage,W as resamplePcm16,o as responseCreateToEnvelope,t as responsesCreateToEnvelope,h as serverContentOfAudio,K as serverContentOfDelta,k as sessionResumptionUpdateMsg,G as translateClientContent,b as translateRealtimeInput,x as translateSetup,w as translateToolResponse,Q as turnEndOfCompleted,v as videoOutAck,g as videoOutChunkMsg,_ as weriftAudioBackend};
1
+ import{a as c,b as C,c as L,d as M,e as R,f as D,g as f,h as N,i as v,j as g,k as x,l as k,m as y,n as G,o as B,p as F,q as P,r as H,s as W,t as b,u as w,v as K,w as h,x as Q}from"./chunk-7M6LY6DR.js";import{a as i,b as s,c as O,d,e as a,f as E,g as _,h as p,i as u,j as A,k as S,l as I,m as T,n as l,o as m,p as U,q as V}from"./chunk-BSHT6RZZ.js";import{a as o,b as t,d as n,e as r}from"./chunk-2T2YYBVX.js";import{c as e}from"./chunk-5CCJUNH7.js";e();export{s as AudioBridge,C as CLOSE_PROTOCOL,R as CLOSE_SESSION_GONE,L as CLOSE_UNKNOWN_MODEL,M as CLOSE_UPSTREAM,B as GEMINI_AUDIO_IN_RATE,P as GEMINI_AUDIO_OUT_MIME,F as GEMINI_AUDIO_OUT_RATE,c as GEMINI_LIVE_PATH,D as GeminiLiveError,i as NATIVE_AUDIO_SAMPLE_RATE,H as OUTBOUND_AUDIO_FLUSH_B64,d as RT_VIDEO_IN_STREAM,u as RT_VIDEO_OUT_STREAM,r as SdkTransport,a as VIDEO_FRAME_MESSAGE_BUDGET,S as VIDEO_OUT_DEFAULT_CODEC,A as VIDEO_OUT_FRAMING,N as VIDEO_OUT_SETUP_KEY,m as VIDEO_OUT_WS_FRAMING,V as consumeVideoOut,n as decodeResponsesLane,T as decodeVideoOutRecord,O as enableSessionAudio,E as enableSessionVideo,I as encodeVideoOutRecord,y as goAwayMsg,l as negotiateSessionVideoOutQuic,p as nodeAudioBackend,U as openSessionVideoOutLane,f as parseClientMessage,W as resamplePcm16,o as responseCreateToEnvelope,t as responsesCreateToEnvelope,h as serverContentOfAudio,K as serverContentOfDelta,k as sessionResumptionUpdateMsg,G as translateClientContent,b as translateRealtimeInput,x as translateSetup,w as translateToolResponse,Q as turnEndOfCompleted,v as videoOutAck,g as videoOutChunkMsg,_ as weriftAudioBackend};