@urun-sh/openai 0.5.6 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/{ResponsesClient-BE3-hx3m.d.ts → ResponsesClient-CSSrOYD8.d.ts} +5 -1
  2. package/dist/{ResponsesClient-DjZWjlFH.d.cts → ResponsesClient-_OZERUjH.d.cts} +9 -1
  3. package/dist/{chunk-7M6LY6DR.js → chunk-3JWYIKHM.js} +1 -1
  4. package/dist/{chunk-BSHT6RZZ.js → chunk-6M5JK4YC.js} +1 -1
  5. package/dist/chunk-76BZLCGT.js +1 -0
  6. package/dist/chunk-7V2JKPIG.js +1 -0
  7. package/dist/{chunk-5HKWNK3O.js → chunk-K23AZPI4.js} +1 -1
  8. package/dist/chunk-T2LNHDPS.js +7 -0
  9. package/dist/chunk-TMQJY56F.js +4 -0
  10. package/dist/chunk-WEBOBE7Z.js +58 -0
  11. package/dist/chunk-X3GLH2HQ.js +1 -0
  12. package/dist/gemini-live.cjs +2 -2
  13. package/dist/gemini-live.d.cts +75 -7
  14. package/dist/gemini-live.d.ts +30 -7
  15. package/dist/gemini-live.js +1 -1
  16. package/dist/hosted/bin.cjs +37 -35
  17. package/dist/hosted/bin.js +1 -1
  18. package/dist/hosted/index.cjs +33 -31
  19. package/dist/hosted/index.d.cts +70 -4
  20. package/dist/hosted/index.d.ts +21 -4
  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/pi-extension/index.cjs +7 -7
  27. package/dist/pi-extension/index.d.cts +2 -2
  28. package/dist/pi-extension/index.d.ts +2 -2
  29. package/dist/pi-extension/index.js +1 -1
  30. package/dist/pi-extension/standalone.cjs +53 -53
  31. package/dist/proxy/cli.cjs +48 -46
  32. package/dist/proxy/cli.js +15 -14
  33. package/dist/proxy/index.cjs +25 -24
  34. package/dist/proxy/index.d.cts +142 -25
  35. package/dist/proxy/index.d.ts +22 -11
  36. package/dist/proxy/index.js +1 -1
  37. package/dist/{responses-turn-Dr37N3kH.d.ts → responses-turn-EwxfRJJg.d.ts} +187 -7
  38. package/dist/{responses-turn-tNUHq5b0.d.cts → responses-turn-vMefRdgJ.d.cts} +707 -23
  39. package/dist/{translator-Bh0Bp_Ie.d.cts → translator-CO8W_hmJ.d.cts} +1 -1
  40. package/dist/{translator-BCoRFaTs.d.ts → translator-Ddd65sXR.d.ts} +1 -1
  41. package/dist/{types-lsVTbNcH.d.cts → types-CHPtJx6d.d.cts} +13 -0
  42. package/dist/{types-lsVTbNcH.d.ts → types-CHPtJx6d.d.ts} +4 -0
  43. package/dist/{video-out-CCksIcj8.d.cts → video-out-BsuLqlID.d.cts} +1 -1
  44. package/dist/{video-out-IGQ4YQ-c.d.ts → video-out-Cawtm7SF.d.ts} +1 -1
  45. package/package.json +4 -2
  46. package/dist/chunk-2T2YYBVX.js +0 -1
  47. package/dist/chunk-3WAJD62J.js +0 -4
  48. package/dist/chunk-FP4RSAIE.js +0 -1
  49. package/dist/chunk-HR5H6S7L.js +0 -1
  50. package/dist/chunk-NY23USZF.js +0 -57
  51. package/dist/chunk-VLRMJRLS.js +0 -6
  52. /package/dist/{chunk-5CCJUNH7.js → chunk-YSFSRI3D.js} +0 -0
@@ -1,6 +0,0 @@
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};
File without changes