@urun-sh/openai 0.6.20 → 0.6.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-4W3ZPIR7.js +62 -0
- package/dist/{chunk-BAKPG5S3.js → chunk-CUDFHFNX.js} +3 -3
- package/dist/chunk-HCAKF2SQ.js +7 -0
- package/dist/chunk-KSC5Q6PZ.js +2 -0
- package/dist/hosted/bin.cjs +40 -38
- package/dist/hosted/bin.js +1 -1
- package/dist/hosted/index.cjs +36 -34
- package/dist/hosted/index.d.cts +105 -3
- package/dist/hosted/index.d.ts +59 -2
- package/dist/hosted/index.js +1 -1
- package/dist/pi-extension/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +40 -40
- package/dist/proxy/cli.cjs +51 -49
- package/dist/proxy/cli.js +15 -14
- package/dist/proxy/index.cjs +26 -26
- package/dist/proxy/index.d.cts +2 -2
- package/dist/proxy/index.d.ts +2 -2
- package/dist/proxy/index.js +1 -1
- package/dist/{responses-turn-DmzWIpmO.d.ts → responses-turn-2UJImii3.d.ts} +12 -1
- package/dist/{responses-turn-Cb1qQBA7.d.cts → responses-turn-C0PBaF29.d.cts} +41 -1
- package/package.json +1 -1
- package/dist/chunk-64WZRHYB.js +0 -6
- package/dist/chunk-7ZH2UHK6.js +0 -1
- package/dist/chunk-TF2IMK3T.js +0 -62
package/dist/proxy/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Server, IncomingMessage } from 'node:http';
|
|
2
|
-
import { f as ProxyHandlerOptions, P as ProxyClients } from '../responses-turn-
|
|
3
|
-
export { D as DialObserver, a as DialReport, g as ProxyIdentity, h as ProxyVideoOutLane, S as SessionGoneError, U as UnknownModelError } from '../responses-turn-
|
|
2
|
+
import { f as ProxyHandlerOptions, P as ProxyClients } from '../responses-turn-C0PBaF29.cjs';
|
|
3
|
+
export { D as DialObserver, a as DialReport, g as ProxyIdentity, h as ProxyVideoOutLane, S as SessionGoneError, U as UnknownModelError } from '../responses-turn-C0PBaF29.cjs';
|
|
4
4
|
export { h as GEMINI_LIVE_PATH, V as VIDEO_OUT_SETUP_KEY } from '../translator-CO8W_hmJ.cjs';
|
|
5
5
|
import { WebSocket } from 'ws';
|
|
6
6
|
import '../models-DUdx_Y6X.cjs';
|
package/dist/proxy/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Server, IncomingMessage } from 'node:http';
|
|
2
|
-
import { f as ProxyHandlerOptions, P as ProxyClients } from '../responses-turn-
|
|
3
|
-
export { D as DialObserver, a as DialReport, g as ProxyIdentity, h as ProxyVideoOutLane, S as SessionGoneError, U as UnknownModelError } from '../responses-turn-
|
|
2
|
+
import { f as ProxyHandlerOptions, P as ProxyClients } from '../responses-turn-2UJImii3.js';
|
|
3
|
+
export { D as DialObserver, a as DialReport, g as ProxyIdentity, h as ProxyVideoOutLane, S as SessionGoneError, U as UnknownModelError } from '../responses-turn-2UJImii3.js';
|
|
4
4
|
export { h as GEMINI_LIVE_PATH, V as VIDEO_OUT_SETUP_KEY } from '../translator-Ddd65sXR.js';
|
|
5
5
|
import { WebSocket } from 'ws';
|
|
6
6
|
import '../models-DUdx_Y6X.js';
|
package/dist/proxy/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{B as l,C as a,D as x,E as s,G as O,H as f,
|
|
1
|
+
import{B as l,C as a,D as x,E as s,G as O,H as f,Y as I,a as n,b as p,c as m}from"../chunk-4W3ZPIR7.js";import{f as r,g as i}from"../chunk-CUDFHFNX.js";import{a as o,h as t}from"../chunk-3JWYIKHM.js";import"../chunk-6M5JK4YC.js";import{c as e}from"../chunk-YSFSRI3D.js";e();export{o as GEMINI_LIVE_PATH,a as OPENAI_REALTIME_PATH,l as OpenAIRealtimeConnection,n as ResumptionRegistry,i as SessionGoneError,r as UnknownModelError,t as VIDEO_OUT_SETUP_KEY,p as attachGeminiLive,x as attachOpenAIRealtime,I as createOpenAIProxy,f as fixedClientsResolver,O as realtimeBindingFor,s as realtimeModelOf,m as serveGeminiLiveSocket};
|
|
@@ -60,6 +60,13 @@ interface InferenceRecord {
|
|
|
60
60
|
readonly decode_ms: number | null;
|
|
61
61
|
readonly total_ms: number | null;
|
|
62
62
|
|
|
63
|
+
readonly decision_resolve_ms?: number | null;
|
|
64
|
+
readonly decision_dial_ms?: number | null;
|
|
65
|
+
readonly decision_first_event_ms?: number | null;
|
|
66
|
+
readonly decision_stream_ms?: number | null;
|
|
67
|
+
readonly decision_question_fwd_ms?: number | null;
|
|
68
|
+
readonly decision_head_ms?: number | null;
|
|
69
|
+
|
|
63
70
|
readonly catalog: TurnCatalogIdentity;
|
|
64
71
|
}
|
|
65
72
|
|
|
@@ -223,6 +230,8 @@ interface RouteTarget {
|
|
|
223
230
|
|
|
224
231
|
sharedOrgId?: string;
|
|
225
232
|
}
|
|
233
|
+
|
|
234
|
+
declare function billingIdentityOf(target: RouteTarget): TurnCatalogIdentity;
|
|
226
235
|
interface ModelRouterOptions<S> {
|
|
227
236
|
|
|
228
237
|
defaultApp: string | null;
|
|
@@ -298,6 +307,8 @@ declare class ModelRouter<S> {
|
|
|
298
307
|
identity: TurnCatalogIdentity;
|
|
299
308
|
}>;
|
|
300
309
|
|
|
310
|
+
resolveTargetFor(model: string | undefined): Promise<RouteTarget>;
|
|
311
|
+
|
|
301
312
|
speechSessionFor(model: string | undefined): Promise<{
|
|
302
313
|
app: string;
|
|
303
314
|
entry: S;
|
|
@@ -604,4 +615,4 @@ interface ProxyHandlerOptions {
|
|
|
604
615
|
observe?: (record: InferenceRecord) => void;
|
|
605
616
|
}
|
|
606
617
|
|
|
607
|
-
export { type DialObserver as D, type InferenceRecord as I, type LedgerFailure as L, ModelRouter as M, type ProxyClients as P, SessionGoneError as S, UnknownModelError as U, type DialReport as a, type InferenceUsageRecord as b, type LedgerOutcome as c, type LedgerRecorded as d, type LedgerWrite as e, type ProxyHandlerOptions as f, type ProxyIdentity as g, type ProxyVideoOutLane as h, type UsageRowReporter as i };
|
|
618
|
+
export { type DialObserver as D, type InferenceRecord as I, type LedgerFailure as L, ModelRouter as M, type ProxyClients as P, SessionGoneError as S, UnknownModelError as U, type DialReport as a, type InferenceUsageRecord as b, type LedgerOutcome as c, type LedgerRecorded as d, type LedgerWrite as e, type ProxyHandlerOptions as f, type ProxyIdentity as g, type ProxyVideoOutLane as h, type UsageRowReporter as i, billingIdentityOf as j };
|
|
@@ -170,6 +170,18 @@ interface InferenceRecord {
|
|
|
170
170
|
readonly prefill_ms: number | null;
|
|
171
171
|
readonly decode_ms: number | null;
|
|
172
172
|
readonly total_ms: number | null;
|
|
173
|
+
/**
|
|
174
|
+
* THE DECISION LANE'S HOP SPANS — see {@link InferenceTurn.decision}.
|
|
175
|
+
* Present only on `/v1/systemone` requests (absent keys on every other
|
|
176
|
+
* lane); `null` means the lane reached the decision but that hop never
|
|
177
|
+
* produced a measurement.
|
|
178
|
+
*/
|
|
179
|
+
readonly decision_resolve_ms?: number | null;
|
|
180
|
+
readonly decision_dial_ms?: number | null;
|
|
181
|
+
readonly decision_first_event_ms?: number | null;
|
|
182
|
+
readonly decision_stream_ms?: number | null;
|
|
183
|
+
readonly decision_question_fwd_ms?: number | null;
|
|
184
|
+
readonly decision_head_ms?: number | null;
|
|
173
185
|
/**
|
|
174
186
|
* The identity that served this request, reported by the dial that served it
|
|
175
187
|
* — see {@link TurnCatalogIdentity} and {@link DialReport}. The ledger writer
|
|
@@ -719,6 +731,26 @@ interface RouteTarget {
|
|
|
719
731
|
*/
|
|
720
732
|
sharedOrgId?: string;
|
|
721
733
|
}
|
|
734
|
+
/**
|
|
735
|
+
* THE BILLING IDENTITY one resolved target carries, in the LEDGER's vocabulary
|
|
736
|
+
* ({@link TurnCatalogIdentity}) rather than the router's.
|
|
737
|
+
*
|
|
738
|
+
* ONE DERIVATION, THREE VIEWS. {@link ModelRouter.catalogIdentityFor} is this
|
|
739
|
+
* function's catalog half, {@link ModelRouter.servingAppSlugFor} is its
|
|
740
|
+
* app half (`appSlug` is exactly what a non-catalog identity records), and
|
|
741
|
+
* {@link ModelRouter.sessionFor} hands the whole thing to the dial that is
|
|
742
|
+
* about to run. Two rules over the same `RouteTarget` would be two rules free
|
|
743
|
+
* to drift, and they drift on a money column: the catalog pair prices the row,
|
|
744
|
+
* and since ENG-412 the app slug lands in `model_id` too.
|
|
745
|
+
*
|
|
746
|
+
* `unresolved` is NOT reachable from here, and that is a fact about the input
|
|
747
|
+
* rather than an omission: a `RouteTarget` EXISTS only because
|
|
748
|
+
* `ModelRouter.resolveTarget` answered. A model it cannot resolve throws
|
|
749
|
+
* (`UnknownModelError`), and recording the reason for that is the lane's
|
|
750
|
+
* pre-dial pin (`server.ts` `pinCatalogIdentity`), which is the only place
|
|
751
|
+
* that has a failure to describe.
|
|
752
|
+
*/
|
|
753
|
+
declare function billingIdentityOf(target: RouteTarget): TurnCatalogIdentity;
|
|
722
754
|
interface ModelRouterOptions<S> {
|
|
723
755
|
/**
|
|
724
756
|
* The startup app slug (URUN_APP) — the DEFAULT model — or `null` for the
|
|
@@ -1008,6 +1040,14 @@ declare class ModelRouter<S> {
|
|
|
1008
1040
|
entry: S;
|
|
1009
1041
|
identity: TurnCatalogIdentity;
|
|
1010
1042
|
}>;
|
|
1043
|
+
/**
|
|
1044
|
+
* The resolved dial target one model names — the DECISION POOL's open seam
|
|
1045
|
+
* (backhaul.ts `wireDecisionPool`): the pool needs the target to open slot
|
|
1046
|
+
* sessions with per-slot `concurrency_key`s, the same resolution
|
|
1047
|
+
* {@link sessionFor} uses so the two never disagree. Pure resolution —
|
|
1048
|
+
* opens NOTHING.
|
|
1049
|
+
*/
|
|
1050
|
+
resolveTargetFor(model: string | undefined): Promise<RouteTarget>;
|
|
1011
1051
|
/**
|
|
1012
1052
|
* The SPEECH lane's pooled session for a model — the SAME resolution and
|
|
1013
1053
|
* acquisition as {@link sessionFor}, stored under a LANE-SCOPED pool key
|
|
@@ -2478,4 +2518,4 @@ interface ProxyHandlerOptions {
|
|
|
2478
2518
|
observe?: (record: InferenceRecord) => void;
|
|
2479
2519
|
}
|
|
2480
2520
|
|
|
2481
|
-
export { type DialObserver as D, type InferenceRecord as I, type LedgerFailure as L, ModelRouter as M, type ProxyClients as P, SessionGoneError as S, UnknownModelError as U, type DialReport as a, type InferenceUsageRecord as b, type LedgerOutcome as c, type LedgerRecorded as d, type LedgerWrite as e, type ProxyHandlerOptions as f, type ProxyIdentity as g, type ProxyVideoOutLane as h, type UsageRowReporter as i };
|
|
2521
|
+
export { type DialObserver as D, type InferenceRecord as I, type LedgerFailure as L, ModelRouter as M, type ProxyClients as P, SessionGoneError as S, UnknownModelError as U, type DialReport as a, type InferenceUsageRecord as b, type LedgerOutcome as c, type LedgerRecorded as d, type LedgerWrite as e, type ProxyHandlerOptions as f, type ProxyIdentity as g, type ProxyVideoOutLane as h, type UsageRowReporter as i, billingIdentityOf as j };
|
package/package.json
CHANGED
package/dist/chunk-64WZRHYB.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import{a as G,b as B}from"./chunk-7ZH2UHK6.js";import{C as re,D as oe,E as se,F as ie,G as ae,I as le,J as k,K as ce,L as ue,M as de,N as he,O as pe,P as fe,Q as ge,R as me,b as Z,d as ee,e as te,q as ne,r as C}from"./chunk-TF2IMK3T.js";import{c as Y,g as Q,i as D}from"./chunk-BAKPG5S3.js";import{a as X}from"./chunk-3JWYIKHM.js";import{c as p}from"./chunk-YSFSRI3D.js";p();import{Counter as E,Gauge as ye,Histogram as we,Registry as Ne,collectDefaultMetrics as Pe}from"@prometheus-io/client";import{createServer as Ue}from"http";var It=9464,be="/metrics",O="(other)",$e="(none)",Le=64,He=128,Me=["/v1/responses","/v1/chat/completions","/v1/systemone","/v1/messages","/v1/messages/count_tokens","/v1/models","/v1/images/models","/v1/images/generations","/v1/images/edits","/v1/audio/speech","/v1/audio/models","/v1/audio/transcriptions",le,ue,Y],Fe=new Set(Me);function De(t){return Fe.has(t)?t:O}function Ge(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var T=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,n=!0){return e===null||e===""?$e:this.admitted.has(e)?e:n?this.admitted.size>=this.max?(this.overflows+=1,O):(this.admitted.add(e),e):O}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},A=class{registry=new Ne;modelLabel=new T(Le);orgLabel=new T(He);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new E({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 we({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 we({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 E({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 E({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 E({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]});for(let n of de)this.ledgerWrites.inc({outcome:n.outcome,reason:n.reason},0);this.ledgerQueue=new ye({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"},he()),this.ledgerQueue.set({measure:"oldest_age_seconds"},pe()/1e3)}}),this.labelCardinality=new ye({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 E({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&&Pe({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let n=De(e.path),o=e.status!==null&&!(e.status>=400&&e.status<500),r=this.modelLabel.overflowCount+this.orgLabel.overflowCount,a=this.modelLabel.of(e.model,o),s=this.orgLabel.of(e.org,o);this.modelLabel.overflowCount+this.orgLabel.overflowCount-r>0&&(this.modelLabel.overflowCount>r?this.labelOverflows.inc({label:"model"}):this.labelOverflows.inc({label:"org"}));let l=e.stream===!0?"true":"false";this.requests.inc({lane:n,method:e.method===""?O:e.method,status_class:Ge(e.status),stream:l,model:a,org:s}),this.duration.observe({lane:n,stream:l,model:a,org:s},e.duration_ms/1e3),e.ttft_ms!==null&&this.ttft.observe({lane:n,model:a,org:s},e.ttft_ms/1e3),e.stream_error&&this.streamErrors.inc({lane:n,model:a}),e.delivered||this.undelivered.inc({lane:n,model:a})}scrape(){return this.registry.metrics()}get contentType(){return this.registry.contentType}};function ve(t){return Ue((e,n)=>{(async()=>{let o=(e.url??"/").split("?")[0];if(e.method!=="GET"||o!==be){n.writeHead(404,{"content-type":"text/plain"}),n.end(`no route for ${e.method} ${o} \u2014 this port serves only GET ${be}
|
|
2
|
-
`);return}let r=await t.scrape();n.writeHead(200,{"content-type":t.contentType}),n.end(r)})().catch(o=>{if(!n.headersSent){n.writeHead(500,{"content-type":"text/plain"}),n.end(`${String(o instanceof Error?o.message:o)}
|
|
3
|
-
`);return}n.end()})})}p();import{createHash as Ee}from"crypto";import{createServer as Be}from"http";var Lt=9465,ze="x-urun-key-id",Ke="x-urun-key-fingerprint",je="urun-ratelimit-bucket:",We="urun-ratelimit-tier:",_e="anonymous",Je=/^urun_sk_[A-Za-z0-9_-]{43}$/;function Ve(t){return Ee("sha256").update(`${je}${t}`).digest("hex")}function qe(t){return Ee("sha256").update(`${We}${t}`).digest("hex")}function Ye(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let n=e[1].trim();return Je.test(n)?n:null}function Se(){return Be((t,e)=>{let n=Ye(t.headers.authorization);e.setHeader(ze,n===null?_e:Ve(n)),e.setHeader(Ke,n===null?_e:qe(n)),e.writeHead(200),e.end()})}p();import{createClientToken as Qe,ClientTokenError as Xe}from"@urun-sh/core";var m=class extends Error{constructor(n,o=401){super(n);this.status=o;this.name="ProxyAuthError"}status},v=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function S(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 n=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!n)throw new m("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return n[1].trim()}var Ze=60,et=6e4,tt=4096;function nt(t){return t===401||t===403}var I=class{constructor(e){this.opts=e;this.mint=e.mint??Qe,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 n=this.cache.get(e);if(n&&this.now()-n.at<et)return{apiKey:e,orgId:n.orgId};let o;try{o=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:Ze})).orgId}catch(r){throw r instanceof Xe&&nt(r.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}: ${r instanceof Error?r.message:String(r)}`)}if(!o)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>=tt){let r=this.cache.keys().next();r.done||this.cache.delete(r.value)}return this.cache.set(e,{orgId:o,at:this.now()}),{apiKey:e,orgId:o}}clear(){this.cache.clear()}};p();var rt=8080,z="serve",zt="0.0.0.0";function ot(t,e,n){let o=(t[e]??"").trim();if(!o)throw new Error(`${e} is not set \u2014 ${n}`);return o}function Kt(t=process.env){let e=(t.PORT??"").trim(),n=e===""?rt:Number(e);if(!Number.isInteger(n)||n<0||n>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let o=ne(C(ot(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")),r=(t.URUN_API_URL??"").trim(),a=r?C(r,"URUN_API_URL"):`${o.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 l=s?{catalogUrl:C(s,"URUN_CATALOG_URL"),anonKey:i}:null;return{port:n,baseUrl:o,apiUrl:a,catalog:l}}p();import{createHash as st,randomUUID as it}from"crypto";var at=512;function K(t){return`urun-inference-proxy-${st("sha256").update(t).digest("hex").slice(0,32)}`}var N=class{constructor(e){this.opts=e}opts;tenants=new Map;label(e,n){return Object.assign(Object.create(e),{tenant:n.orgId})}build(e){if(this.opts.build){let o=this.opts.build(e);return{router:o.router,clients:this.label(o.clients,e)}}let n=G({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:z,sharedOnlyListing:!0,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,release:"end"});return{router:n,clients:this.label(B(n),e)}}clientsFor(e){let n=this.tenants.get(e.apiKey);if(n)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,n),n.clients;for(;this.tenants.size>=at;){let r=this.tenants.entries().next();if(r.done)break;let[a,s]=r.value;this.tenants.delete(a),s.router.closeAll().catch(()=>{})}let o=this.build(e);return this.tenants.set(e.apiKey,o),o.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(o=>o.router);this.tenants.clear();let n=[];if(await Promise.all(e.map(async o=>{try{await o.closeAll()}catch(r){n.push(String(r instanceof Error?r.message:r))}})),n.length>0)throw new Error(`failed to close ${n.length} tenant backhaul(s): ${n.join("; ")}`)}},Re=512,lt=1024,P=class extends Error{constructor(){super(`conversation capacity reached (${Re} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function ct(t){return{sessionStartOptions:e=>{if(e.startsWith(D))return;let n=t.get(e);return n===void 0?void 0:{sessionId:n}},onSessionOpen:(e,n)=>{e.startsWith(D)||t.set(e,n)}}}function ut(t,e,n,o){let r=G({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:z,sharedOnlyListing:!0,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",...ct(o)});return{clients:B(r),release:async()=>{await r.closeAll()}}}var U=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let o={id:n,subject:`${K(e.apiKey)}:live:${n}`},r=new Map,a=this.opts.build?this.opts.build(e,o):ut(this.opts,e,o,r);return{id:n,backhaul:a,attachments:0,nativeRefs:r}}attachEntry(e,n){let o=this.byCaller.get(e.apiKey);o===void 0&&(o=new Map,this.byCaller.set(e.apiKey,o));let r=o.get(n);if(r!==void 0)return o.delete(n),o.set(n,r),r.attachments+=1,r;this.evictOverflow();let a=this.build(e,n);return a.attachments=1,o.set(n,a),a}get conversationCount(){let e=0;for(let n of this.byCaller.values())e+=n.size;return e}evictOverflow(){for(;this.conversationCount>=Re;){let e=this.byCaller.keys().next();if(e.done)throw new P;let n=e.value,o=this.byCaller.get(n),r=!1;for(let[a,s]of o)if(!(s.attachments>0)){o.delete(a),this.dropHandleIndexFor(n,a),s.backhaul.release().catch(()=>{}),r=!0;break}if(!r)throw o.size===0&&this.byCaller.delete(n),new P;o.size===0&&this.byCaller.delete(n)}}dropHandleIndexFor(e,n){for(let[o,r]of this.byHandle)r.apiKey===e&&r.conversationId===n&&this.byHandle.delete(o)}connectionFor(e){let n=null,o=!1,r=()=>(n===null&&(n=this.attachEntry(e,it())),n),a={createResponse:(s,i)=>r().backhaul.clients.createResponse(s,i),catalogIdentity:s=>r().backhaul.clients.catalogIdentity(s),servingAppSlug:s=>{let i=r().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:()=>r().backhaul.clients.listModels(),sessionHandle:s=>r().backhaul.clients.sessionHandle(s),createResponseOn:(s,i)=>r().backhaul.clients.createResponseOn(s,i),onSessionEnd:(s,i)=>r().backhaul.clients.onSessionEnd(s,i)};return a.openAudio=async(s,i)=>{let l=r().backhaul.clients.openAudio;if(l===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return l(s,i)},a.openVideo=async s=>{let i=r().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=r().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=r().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)},a.supportsSpeech=async s=>{let i=r().backhaul.clients.supportsSpeech;if(i===void 0)throw new Error("the conversation backhaul wires no supportsSpeech seam \u2014 the speech lane cannot gate model capability on this deployment");return i(s)},a.speechVoices=async s=>{let i=r().backhaul.clients.speechVoices;if(i===void 0)throw new Error("the conversation backhaul wires no speechVoices seam \u2014 the speech lane cannot resolve the voice set /v1/models publishes on this deployment");return i(s)},a.speechSessionHandle=async s=>{let i=r().backhaul.clients.speechSessionHandle;if(i===void 0)throw new Error("the conversation backhaul wires no speechSessionHandle seam \u2014 the speech lane cannot resolve its own pooled session on this deployment");return i(s)},a.openAudioOn=async(s,i)=>{let l=r().backhaul.clients.openAudioOn;if(l===void 0)throw new Error("the conversation backhaul wires no openAudioOn seam \u2014 the speech and realtime lanes cannot open their pinned native session audio on this deployment");return l(s,i)},a.openTurnsOn=(s,i)=>{let l=r().backhaul.clients.openTurnsOn;if(l===void 0)throw new Error("the conversation backhaul wires no openTurnsOn seam \u2014 the realtime transcription lane cannot open the backend turn/transcript lane on this deployment");return l(s,i)},a.evictSpeechSession=async s=>{let i=r().backhaul.clients.evictSpeechSession;if(i===void 0)throw new Error("the conversation backhaul wires no evictSpeechSession seam \u2014 the speech lane cannot retire its pinned speech session after a failed turn on this deployment");return i(s)},a.supportsTranscription=async s=>{let i=r().backhaul.clients.supportsTranscription;if(i===void 0)throw new Error("the conversation backhaul wires no supportsTranscription seam \u2014 the transcription lane cannot gate model capability on this deployment");return i(s)},a.transcriptStreamFor=async s=>{let i=r().backhaul.clients.transcriptStreamFor;if(i===void 0)throw new Error("the conversation backhaul wires no transcriptStreamFor seam \u2014 the realtime transcription lane cannot verify the model's engine_args.transcript_stream on this deployment");return i(s)},a.listSpeechToTextModels=async()=>{let s=r().backhaul.clients.listSpeechToTextModels;if(s===void 0)throw new Error("the conversation backhaul wires no listSpeechToTextModels seam \u2014 the transcription lane cannot list the stt models on this deployment");return s()},{clients:a,bindResumeHandle:s=>{let i=n;if(i===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=lt;){let l=this.byHandle.keys().next();if(l.done)break;this.byHandle.delete(l.value)}this.byHandle.set(s,{apiKey:e.apiKey,conversationId:i.id})},clientsForResume:async s=>{let i=this.byHandle.get(s),l=i===void 0?void 0:this.byCaller.get(i.apiKey),u=i===void 0||l===void 0?void 0:l.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 n=u,u.attachments+=1,u.backhaul.clients},release:async()=>{if(o)return;o=!0;let s=n;n=null,s!==null&&(s.attachments-=1,!(s.attachments>0)&&await s.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let o of this.byCaller.values())for(let r of o.values())e.push(r.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let n=[];if(await Promise.all(e.map(async o=>{try{await o}catch(r){n.push(String(r instanceof Error?r.message:r))}})),n.length>0)throw new Error(`failed to detach ${n.length} conversation backhaul(s): ${n.join("; ")}`)}};p();var dt="/inference-ledger",ht=1e4,$=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},J=class extends Error{constructor(n,o){super(o);this.ledgerAlreadyWritten=n;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},f=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},V=class extends m{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},pt=[["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",j],["output_minutes",j],["cost_nano_usd",j],["price_version",t=>t===null||typeof t=="string"]];function ft(t){for(let[e,n]of pt){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function gt(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 n=e.existing_status;if(n==="not_in_this_org")return{status:"not_in_this_org"};if(n!=="found")return{status:"unavailable"};let o=e.existing;return o===null||typeof o!="object"||Array.isArray(o)?{status:"unavailable"}:ft(o)!==null?{status:"unavailable"}:{status:"found",row:o}}function j(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function W(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function mt(t,e){return t===null?e===null:e===null?!1:Number(t.toFixed(9))===Number(e)}function xe(t,e){if(typeof e!="string")return!1;let n=Date.parse(t),o=Date.parse(e);return Number.isFinite(n)&&Number.isFinite(o)&&n===o}function yt(t,e){let n=e;return n.inference_id===t.inference_id&&n.model_id===t.model_id&&(n.variant??null)===t.variant&&n.task===t.task&&n.http_status===t.http_status&&xe(t.started_at,n.started_at)&&xe(t.completed_at,n.completed_at)&&W(t.prompt_tokens,n.prompt_tokens)&&W(t.completion_tokens,n.completion_tokens)&&W(t.output_units,n.output_units)&&mt(t.output_minutes,n.output_minutes)}function wt(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new f(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let n=t,o=n.inference_id;if(typeof o!="string"||o==="")throw new f(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in n))throw new f(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let r=n.cost_nano_usd;if(r!=null&&!(typeof r=="number"&&Number.isInteger(r)&&r>=0))throw new f(`${e} answered cost_nano_usd ${JSON.stringify(r)}, which is neither a non-negative integer nor null`);let a=u=>{if(!(u in n))throw new f(`${e} answered a row with no ${u} field: ${JSON.stringify(t)}`);let g=n[u];if(g===null)return null;if(typeof g!="string")throw new f(`${e} answered a non-string ${u}: ${JSON.stringify(g)}`);if(!/[^\s]/.test(g))throw new f(`${e} answered a BLANK ${u} (${JSON.stringify(g)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return g},s=a("price_version"),i=a("unpriced_reason"),l=r!=null;if(l&&(s===null||i!==null))throw new f(`${e} answered a PRICED row (cost_nano_usd ${String(r)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}`);if(!l&&(s!==null||i===null))throw new f(`${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:o,cost_nano_usd:r===void 0?null:r,price_version:s,unpriced_reason:i}}var L=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let o=`${this.opts.apiUrl.replace(/\/+$/,"")}${dt}`,r;try{r=await this.fetchImpl(o,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(ht)})}catch(l){throw new f(`the uRun ledger surface at ${o} could not be reached: ${l instanceof Error?l.message:String(l)}`)}if(r.status===409){let l=await r.text().catch(()=>""),u=gt(l);throw u.status==="found"&&yt(n,u.row)?new J(u.row,`the uRun control plane already holds THIS PROXY'S OWN row for inference_id ${n.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 ${n.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 ${n.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). ${l.slice(0,400)}`)}if(r.status===401||r.status===403)throw await r.text().catch(()=>{}),new V("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(!r.ok){let l=await r.text().catch(u=>`<body unreadable: ${u instanceof Error?u.message:String(u)}>`);throw new f(`the uRun ledger surface at ${o} answered HTTP ${r.status}: ${l.slice(0,400)}`)}let a;try{a=await r.json()}catch(l){throw new f(`the uRun ledger surface at ${o} answered a body that is not JSON: ${l instanceof Error?l.message:String(l)}`)}let s=a?.recorded;if(s===void 0)throw new f(`the uRun ledger surface at ${o} answered a body with no "recorded" field`);let i=wt(s,`the uRun ledger surface at ${o}`);if(i.inference_id!==n.inference_id)throw new f(`the uRun ledger surface at ${o} answered for inference_id ${i.inference_id} when asked about ${n.inference_id}`);return i}};p();import{createServer as vt}from"http";p();var H="/inference-usage",Ce=25e3;var bt=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function ke(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=bt.has(t.name)?t.name:"Error",n=t.cause?.code;return typeof n=="string"&&/^[A-Z][A-Z0-9_]{0,31}$/.test(n)?`${e} (${n})`:e}var M=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let o=`${this.opts.apiUrl.replace(/\/+$/,"")}${H}`,r;try{r=await this.fetchImpl(o,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:n}),signal:AbortSignal.timeout(Ce)})}catch(s){throw new k(`the uRun usage surface (POST ${H}) could not be reached within ${Ce}ms: ${ke(s)}`)}if(r.status===401||r.status===403)throw await r.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(!r.ok){let s=await r.text().catch(()=>null),i=s===null?"body unreadable":`${s.length}-character body`;throw new k(`the uRun usage surface (POST ${H}) answered HTTP ${r.status} (${i})`)}let a;try{a=await r.json()}catch(s){throw new k(`the uRun usage surface (POST ${H}) answered HTTP ${r.status} with a body that is not JSON: ${ke(s)}`)}return ce(a,this.opts.onRejectedRow)}};var _t=5e3,Et=3e3,St="/api/client-tokens";function q(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function Oe(t,e,n,o){q(t,e,{error:{message:n,type:o}})}async function Rt(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${St}`;try{let o=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(Et)});return o.status===404?{ready:!1,detail:`${n} answered 404 \u2014 this is not a uRun control plane`}:o.status>=500?{ready:!1,detail:`${n} answered HTTP ${o.status}`}:{ready:!0,detail:`${n} answered HTTP ${o.status}`}}catch(o){return{ready:!1,detail:`${n} unreachable: ${o instanceof Error?o.message:String(o)}`}}}function _n(t){let e=t.resolver??new I({gatewayUrl:t.baseUrl}),n=t.registry??new N({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),o=t.probeControlPlane??(()=>Rt(t.baseUrl)),r=t.usageClient??new M({apiUrl:t.apiUrl}),a=t.ledgerClient??new L({apiUrl:t.apiUrl}),s=new A(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),i=async c=>e.resolve(S(c.headers.authorization)),l=c=>c instanceof m?{status:c.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:c instanceof v?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,u=me({clients:async c=>n.clientsFor(await i(c)),usage:{authorize:c=>S(c.headers.authorization),lookup:(c,d)=>r.lookup(c,d)},statusOf:l,...t.requestLog?{requestLog:t.requestLog}:{},observe:c=>s.observe(c),ledger:{write:(c,d)=>a.write(S(c.headers.authorization),d),observe:c=>s.observeLedger(c),...t.ledgerLog?{report:t.ledgerLog}:{}}}),g=null,_=vt((c,d)=>{let h=new URL(c.url??"/","http://localhost");(async()=>{if(c.method==="GET"&&h.pathname==="/healthz"){q(d,200,{ok:!0});return}if(c.method==="GET"&&h.pathname==="/readyz"){let x=Date.now();if(!g||x-g.at>=_t){let Ie=await o();g={at:x,...Ie}}q(d,g.ready?200:503,{ok:g.ready,control_plane:g.detail});return}let y=h.pathname==="/v1"||h.pathname.startsWith("/v1/"),R=h.pathname.startsWith("/partners/");if(!y&&!R){Oe(d,404,`no route for ${c.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(c,d)})().catch(y=>{if(!d.headersSent){Oe(d,500,String(y instanceof Error?y.message:y),"proxy_error");return}d.end()})}),F=t.conversations??new U({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),Te=[Z(_,{authenticate:async c=>{let d=await e.resolve(c),h=F.connectionFor(d);return{clients:h.clients,scope:K(d.apiKey),onBindResumeHandle:h.bindResumeHandle,clientsForResume:h.clientsForResume,release:h.release}}}),te(_,{clients:async c=>n.clientsFor(await i(c)),statusOf:l}).close,oe(_,{resolver:{resolve:async(c,d)=>{let h=await e.resolve(S(c.headers.authorization)),y=F.connectionFor(h),R;try{R=await ae(y.clients,se(c),d,ie(c))}catch(x){throw y.release().catch(()=>{}),x}return{...R,release:y.release}}}})];_.on("upgrade",(c,d)=>{let h=new URL(c.url??"/","http://localhost");h.pathname===X||h.pathname===ee||h.pathname===re||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 Ae=async()=>{for(let c of Te)await c();await F.detachAll()};return{server:_,metricsServer:ve(s),metrics:s,edgeIdentityServer:Se(),registry:n,closeAll:()=>n.closeAll(),closeWebSockets:Ae}}async function En(t,e,n){n&&await n();let o=new Promise(r=>t.close(()=>r()));t.closeAllConnections(),await o,await fe()||ge(),await e()}export{It as a,be as b,O as c,$e as d,Le as e,He as f,De as g,Ge as h,T as i,A as j,ve as k,Lt as l,ze as m,Ke as n,je as o,We as p,_e as q,Ve as r,qe as s,Ye as t,Se as u,m as v,v as w,S as x,et as y,tt as z,I as A,rt as B,z as C,zt as D,Kt as E,at as F,K as G,N as H,Re as I,lt as J,P as K,U as L,dt as M,ht as N,$ as O,f as P,wt as Q,L as R,_t as S,Et as T,_n as U,En as V};
|
package/dist/chunk-7ZH2UHK6.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{a as T}from"./chunk-K23AZPI4.js";import{a as x}from"./chunk-76BZLCGT.js";import{a as G,b as M,d as U,g as b,h as _,j as D,k as H}from"./chunk-BAKPG5S3.js";import{c as R,f as C,g as F,p as I}from"./chunk-6M5JK4YC.js";import{c as S}from"./chunk-YSFSRI3D.js";S();import{App as B,createClientToken as Z,DEFAULT_WHEN_LIVE_TIMEOUT_MS as ee}from"@urun-sh/core";S();import{SessionFailedError as K}from"@urun-sh/core";var P=2;function J(n){let t=n?.type;return t==="response.created"||t==="response.in_progress"}function Q(n){let t=n?.type;return t==="response.completed"||t==="error"}function N(n){return async function(r,c){return(async function*(){for(let a=1;a<=P;a++){let{app:d,entry:i,identity:u}=await n.sessionFor(r.model);c?.({sessionId:n.sessionIdOf?.(i)??null,identity:u});let m=!1,l=!1;try{let f=await n.createStream(i,r);for await(let e of f){if(Q(e)&&(m=!0),J(e)){if(a>1)continue}else l=!0;yield e}}catch(f){if(f instanceof K&&(f.kind==="never-live"||f.kind==="create-failed")){try{n.onAdmissionVerdict?.(d,i,f)}catch{}throw f}if(a<P&&!l){await n.evict(d,i);continue}throw f}if(m)return;if(a<P&&!l){await n.evict(d,i);continue}return}})()}}S();var Y=3,z="llm",X=new Set(["paused","error"]);function W(n,t){let r=n.doc(z),c=typeof r.onConnectionState=="function",p=typeof n.onPhase=="function";if(!c&&!p)throw new Error(`session for app "${t}" exposes neither a doc connection-state surface nor onPhase \u2014 session-gone detection requires @urun-sh/core Session (SessionDocument.onConnectionState / Session.onPhase); pooling a session with no native lifecycle signal would silently swallow requests dispatched after the platform closes it`);let a=null,d=!1,i=new Set,u=[],m=l=>{if(!a){a=l;for(let f of[...i])f(l);i.clear()}};return c&&u.push(r.onConnectionState(l=>{d&&(l.consecutiveFailures<Y||m(new b(`the pooled session for app "${t}" is gone: its doc backhaul failed ${l.consecutiveFailures} consecutive times (last close ${l.lastCloseCode===null?"by provider liveness reap":`code ${l.lastCloseCode}`}${l.lastCloseReason?`, reason ${JSON.stringify(l.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),p&&u.push(n.onPhase(l=>{if(l.name==="live"){d=!0;return}d&&X.has(l.name)&&m(new b(`the pooled session for app "${t}" lost its compute binding (phase "${l.name}"). The entry is evicted; retry to get a fresh session. The named session itself is not over \u2014 it stays attachable by name.`))})),{gone:()=>a,onGone(l){return a?(l(a),()=>{}):(i.add(l),()=>i.delete(l))},dispose(){i.clear();for(let l of u.splice(0))l()}}}async function*O(n,t){let r=t.gone();if(r)throw r;let c=n[Symbol.asyncIterator](),p,a=new Promise((d,i)=>{p=t.onGone(u=>i(u))});a.catch(()=>{});try{for(;;){let d=await Promise.race([c.next(),a]);if(d.done)return;yield d.value}}finally{p?.(),c.return?.(void 0)}}function j(n,t,r,c){let p=n[r];if(typeof p!="function")throw new Error(`app "${t}" has no function "${r}" (set URUN_FUNCTION)`);let a=c?p(void 0,c):p();if(typeof a.end!="function")throw new Error(`app "${t}" function "${r}" returned a session without end()`);return a}function ne(n,t,r){let{baseUrl:c,orgId:p,auth:a}=n,d=t.fnName??n.fnName,i=n.sessionStartOptions?.(r),u=t.sharedApp?{sharedApp:!0,...i}:i,m=o=>({session:o,responses:new x(o),gone:W(o,r)});if(a.lane==="jwt"){let o=B(t.appSlug,{baseUrl:c,orgId:p,jwt:a.jwt});return m(j(o,t.appSlug,d,u))}let{apiKey:l,gatewayUrl:f}=a,e=B(t.appSlug,{baseUrl:c,orgId:p,getAccessToken:async()=>(await Z(l,{baseUrl:f,expiresIn:300,allowedFunctions:[`${t.appSlug}/${d}`],...t.sharedApp?{sharedApp:!0}:{}})).token});return m(j(e,t.appSlug,d,u))}function oe(n){let t=n.session.id;if(typeof t!="string"||t.length===0)throw new Error("pooled session exposes no stable session id \u2014 the session-identity seam requires @urun-sh/core Session (session.id)");return t}function se(n){if(n.kind==="never-live")return!0;if(n.kind!=="create-failed")return!1;let t=n.status.error?.httpStatus,r=n.status.error?.code;return t===410||t===409&&(r==="cancelled"||r==="expired")?!0:r==="client_ended"}var te=new Set(["error","paused"]);function Pe(n){let{auth:t,catalog:r}=n,c=null,p=(d,i)=>{let u=ne(n,d,i);return n.onSessionOpen&&typeof u.session.id=="string"&&u.session.id!==""&&n.onSessionOpen(i,u.session.id),u.gone.onGone(()=>{c?.evict(i,u)}),u},a=new H({sharedOnlyListing:n.sharedOnlyListing===!0,defaultApp:n.defaultApp,fnName:n.fnName,openSession:p,closeSession:async d=>{if(d.gone.dispose(),n.release==="detach"){if(typeof d.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');d.session.detach();return}await d.session.end()},listApps:t.lane==="api-key"?()=>_({apiUrl:n.apiUrl,apiKey:t.apiKey}):null,listCatalog:r?()=>T(r):null,listPrices:r?()=>M(r):null,shared:t.lane==="api-key"?()=>G({apiUrl:n.apiUrl,apiKey:t.apiKey}):null,sessionKey:oe});return c=a,n.defaultApp!==null&&a.seed(n.defaultApp,p({appSlug:n.defaultApp},D({appSlug:n.defaultApp}))),a}async function E(n,t){let r=n.whenLive;if(typeof r!="function")throw new Error("pooled session exposes no native whenLive() \u2014 the audio lane must wait for the session's own admission (cold wake: queued \u2192 demand \u2192 promote) before wiring media; a session without the core Session.whenLive surface cannot be served audio");await r.call(n,{timeout:ee,signal:t})}async function q(n){if(typeof n.cancel=="function")try{await n.cancel();return}catch{}if(typeof n.detach!="function")throw new Error("pooled session exposes neither cancel() nor detach() \u2014 the client-gone abandon path requires the @urun-sh/core Session native lifecycle surface");n.detach()}function v(n){return n.reason instanceof Error?n.reason:new Error("the client socket closed")}function Oe(n,t=F,r){let c=new WeakMap,p=new WeakMap,a=new WeakMap,d=new WeakMap,i=new WeakMap,u=e=>{let o=e.session.id;return typeof o=="string"&&o.length>0?o:null},m=e=>{let o=c.get(e);if(!o){let s={waiting:0,gone:new AbortController};i.set(e,s),o=(async()=>{if(await E(e.session,s.gone.signal),s.gone.signal.aborted)throw v(s.gone.signal);i.get(e)===s&&i.delete(e);let h=await R(e.session,await t());return Object.assign(h,{sessionId:e.session.id})})(),c.set(e,o),o.catch(()=>{c.get(e)===o&&c.delete(e)}),o.then(()=>{i.get(e)===s&&i.delete(e)},()=>{i.get(e)===s&&i.delete(e)})}return o},l=e=>{let o=d.get(e);return o||(o=(async()=>{let s=await m(e);return{appendInputAudio:h=>s.appendInputAudio(h),onOutputAudio:h=>s.onOutputAudio(h),sessionId:u(e)}})(),d.set(e,o),o.catch(()=>{d.get(e)===o&&d.delete(e)})),o},f=async(e,o,s)=>{if(s?.aborted){let g=i.get(e);throw(c.get(e)===void 0||g!==void 0&&g.waiting===0)&&await q(e.session),v(s)}let h=o(e),w=i.get(e);return w!==void 0&&w.waiting++,s===void 0?h:new Promise((g,y)=>{let k=()=>{w!==void 0&&--w.waiting===0&&i.get(e)===w&&(w.gone.abort(),i.delete(e),c.delete(e),d.delete(e),q(e.session).catch(()=>{})),y(v(s))};s.addEventListener("abort",k,{once:!0});let L=()=>s.removeEventListener("abort",k);h.then(A=>{L(),g(A)},A=>{L(),y(A)})})};return{createResponse:N({sessionFor:e=>n.sessionFor(e),sessionIdOf:e=>u(e),evict:(e,o)=>n.evict(e,o),onAdmissionVerdict:(e,o,s)=>{se(s)&&(n.evict(e,o),r?.(e,s.kind==="never-live"?"never-live":"create-failed"))},createStream:async(e,o)=>(await E(e.session),O(await e.responses.responses.create(o),e.gone))}),listModels:()=>n.modelList(),openRouterModels:()=>n.openRouterModels(),supportsImages:(e,o)=>n.supportsImages(e,o),listImageModels:()=>n.imageModelList(),catalogIdentity:e=>n.catalogIdentityFor(e),servingAppSlug:async e=>await n.servingAppSlugFor(e),supportsAudio:e=>n.supportsAudio(e),supportsSpeech:e=>n.supportsSpeech(e),speechVoices:e=>n.speechVoices(e),supportsTranscription:e=>n.supportsTranscription(e),transcriptStreamFor:e=>n.transcriptStreamFor(e),listSpeechToTextModels:()=>n.speechToTextModelList(),openAudio:async(e,o)=>{let{entry:s}=await n.sessionFor(e);return await f(s,m,o)},openAudioOn:async(e,o)=>{let{entry:s}=await n.sessionForHandle(e);return await f(s,l,o)},openTurnsOn:async function*(e,o){if(o?.aborted)throw v(o);let{entry:s}=await n.sessionForHandle(e);yield*U(s.session,o)},openVideo:async e=>{let{entry:o}=await n.sessionFor(e),s=p.get(o);return s||(s=C(o.session),p.set(o,s)),s},openVideoOut:async e=>{let{entry:o}=await n.sessionFor(e),s=a.get(o);return s||(s=I(o.session),a.set(o,s)),s},sessionHandle:async e=>(await n.handleFor(e)).handle,speechSessionHandle:async e=>(await n.speechHandleFor(e)).handle,evictSpeechSession:async e=>{let{app:o,entry:s}=await n.sessionForHandle(e);await n.evict(o,s)},createResponseOn:async(e,o)=>{let{entry:s}=await n.sessionForHandle(e);return await E(s.session),O(await s.responses.responses.create(o),s.gone)},onSessionEnd:async(e,o)=>{let{entry:s}=await n.sessionForHandle(e),h=s.session;if(typeof h.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let w=!1;return h.onPhase(g=>{if(w||!te.has(g.name))return;w=!0;let y=h.endsAt??null;o({timeLeftMs:y?Math.max(0,y.getTime()-Date.now()):null,reason:g.reason??`session ${g.name}`})})}}}export{Pe as a,Oe as b};
|