@urun-sh/openai 0.6.22 → 0.6.25

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.
@@ -0,0 +1,2 @@
1
+ import{a as M}from"./chunk-K23AZPI4.js";import{I as j,J as B,N as _,O as K,P as q}from"./chunk-SOAZAPCO.js";import{a as F}from"./chunk-76BZLCGT.js";import{a as x,b as G,d as $,g as A,h as W,j as E,k as H,l as V}from"./chunk-CUDFHFNX.js";import{c as R,f as D,g as N,p as U}from"./chunk-6M5JK4YC.js";import{c as v}from"./chunk-YSFSRI3D.js";v();import{App as z,createClientToken as de,COLD_BOOT_WHEN_LIVE_TIMEOUT_MS as ue,DEFAULT_WHEN_LIVE_TIMEOUT_MS as Z,isWakingPhase as pe}from"@urun-sh/core";v();import{SessionFailedError as te}from"@urun-sh/core";var L=2;function se(e){let s=e?.type;return s==="response.created"||s==="response.in_progress"}function ie(e){let s=e?.type;return s==="response.completed"||s==="error"}function Q(e){return async function(a,d){return(async function*(){for(let l=1;l<=L;l++){let{app:p,entry:f,identity:t}=await e.sessionFor(a.model);d?.({sessionId:e.sessionIdOf?.(f)??null,identity:t});let i=!1,c=!1;try{let m=await e.createStream(f,a);for await(let g of m){if(ie(g)&&(i=!0),se(g)){if(l>1)continue}else c=!0;yield g}}catch(m){if(m instanceof te&&(m.kind==="never-live"||m.kind==="create-failed")){try{e.onAdmissionVerdict?.(p,f,m)}catch{}throw m}if(l<L&&!c){await e.evict(p,f);continue}throw m}if(i)return;if(l<L&&!c){await e.evict(p,f);continue}return}})()}}import{randomUUID as fe}from"crypto";v();var re=3,ae="llm",le=new Set(["paused","error"]);function Y(e,s){let a=e.doc(ae),d=typeof a.onConnectionState=="function",u=typeof e.onPhase=="function";if(!d&&!u)throw new Error(`session for app "${s}" 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 l=null,p=!1,f=new Set,t=[],i=c=>{if(!l){l=c;for(let m of[...f])m(c);f.clear()}};return d&&t.push(a.onConnectionState(c=>{p&&(c.consecutiveFailures<re||i(new A(`the pooled session for app "${s}" is gone: its doc backhaul failed ${c.consecutiveFailures} consecutive times (last close ${c.lastCloseCode===null?"by provider liveness reap":`code ${c.lastCloseCode}`}${c.lastCloseReason?`, reason ${JSON.stringify(c.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),u&&t.push(e.onPhase(c=>{if(c.name==="live"){p=!0;return}p&&le.has(c.name)&&i(new A(`the pooled session for app "${s}" lost its compute binding (phase "${c.name}"). The entry is evicted; retry to get a fresh session. The named session itself is not over \u2014 it stays attachable by name.`))})),{gone:()=>l,onGone(c){return l?(c(l),()=>{}):(f.add(c),()=>f.delete(c))},dispose(){f.clear();for(let c of t.splice(0))c()}}}async function*C(e,s){let a=s.gone();if(a)throw a;let d=e[Symbol.asyncIterator](),u,l=new Promise((p,f)=>{u=s.onGone(t=>f(t))});l.catch(()=>{});try{for(;;){let p=await Promise.race([d.next(),l]);if(p.done)return;yield p.value}}finally{u?.(),d.return?.(void 0)}}var me=1,ge=4,he=4;function He(e,s){let a=(p,f)=>{let t=(e[p]??"").trim();if(!t)return f;let i=Number.parseInt(t,10);if(!Number.isInteger(i)||i<1)throw new Error(`${p} must be a positive integer (got ${JSON.stringify(e[p])})`);return i},d=(e.URUN_DECISION_POOL_MIN??"").trim();if(d==="0")return null;let u=d?Number.parseInt(d,10):me;if(!Number.isInteger(u)||u<0)throw new Error(`URUN_DECISION_POOL_MIN must be a non-negative integer (got ${JSON.stringify(e.URUN_DECISION_POOL_MIN)})`);let l=a("URUN_DECISION_POOL_MAX",Math.max(u,ge));return{min:u,max:l,idleGraceMs:3e4,inFlightCap:a("URUN_DECISION_POOL_INFLIGHT_CAP",he),replicaId:(e.URUN_REPLICA_ID??"").trim()||s||"proxy"}}function Ve(e){let s=(e.URUN_DECISION_STREAM_IDLE_TIMEOUT_S??"").trim();if(!s)return _;let a=Number.parseInt(s,10);if(!Number.isInteger(a)||a<=0)throw new Error(`URUN_DECISION_STREAM_IDLE_TIMEOUT_S must be a positive integer (got ${JSON.stringify(e.URUN_DECISION_STREAM_IDLE_TIMEOUT_S)})`);return a*1e3}function ee(e,s,a,d){let u=e[a];if(typeof u!="function")throw new Error(`app "${s}" has no function "${a}" (set URUN_FUNCTION)`);let l=d?u(void 0,d):u();if(typeof l.end!="function")throw new Error(`app "${s}" function "${a}" returned a session without end()`);return l}function oe(e,s,a,d){let{baseUrl:u,orgId:l,auth:p}=e,f=s.fnName??e.fnName,t=e.sessionStartOptions?.(a),i=d||t||s.sharedApp?{...s.sharedApp?{sharedApp:!0}:{},...t,...d}:void 0,c=o=>({session:o,responses:new F(o),gone:Y(o,a)});if(p.lane==="jwt"){let o=z(s.appSlug,{baseUrl:u,orgId:l,jwt:p.jwt});return c(ee(o,s.appSlug,f,i))}let{apiKey:m,gatewayUrl:g}=p,w=(typeof e.subject=="function"?e.subject():e.subject)??fe(),n=z(s.appSlug,{baseUrl:u,orgId:l,getAccessToken:async()=>(await de(m,{baseUrl:g,expiresIn:300,allowedFunctions:[`${s.appSlug}/${f}`],subject:w,...s.sharedApp?{sharedApp:!0}:{}})).token});return c(ee(n,s.appSlug,f,i))}function we(e){let s=e.session.id;if(typeof s!="string"||s.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 s}function Se(e){if(e.kind==="never-live")return!0;if(e.kind!=="create-failed")return!1;let s=e.status.error?.httpStatus,a=e.status.error?.code;return s===410||s===409&&(a==="cancelled"||a==="expired")?!0:a==="client_ended"}var ye=new Set(["error","paused"]);function je(e){let{auth:s,catalog:a}=e,d=null,u=(p,f)=>{let t=oe(e,p,f);return e.onSessionOpen&&typeof t.session.id=="string"&&t.session.id!==""&&e.onSessionOpen(f,t.session.id),t.gone.onGone(()=>{d?.evict(f,t)}),t},l=new V({sharedOnlyListing:e.sharedOnlyListing===!0,defaultApp:e.defaultApp,fnName:e.fnName,openSession:u,closeSession:async p=>{if(p.gone.dispose(),e.release==="detach"){if(typeof p.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');p.session.detach();return}await p.session.end()},listApps:s.lane==="api-key"?()=>W({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,listCatalog:a?()=>M(a):null,listPrices:a?()=>G(a):null,shared:s.lane==="api-key"?()=>x({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,sessionKey:we});return d=l,e.defaultApp!==null&&l.seed(e.defaultApp,u({appSlug:e.defaultApp},E({appSlug:e.defaultApp}))),l}function Be(e,s){let a=e.decisionPool;if(!a)throw new Error("wireDecisionPool called without BackhaulConfig.decisionPool");let d=a.replicaId??"proxy",u=new Map,l=new Map,p=t=>{process.stderr.write(`[decision-pool] ${t}
2
+ `)},f=(t,i)=>{let c=u.get(i);if(c)return c;let m=new B({min:a.min,max:a.max,idleGraceMs:a.idleGraceMs,inFlightCap:a.inFlightCap,replicaId:d,openSlot:async g=>{let w=oe(e,t,i,{concurrencyKey:g.concurrencyKey});return w.gone.onGone(()=>m.evict(w)),await b(w.session),p(`slot attached pool=${i} slot=${g.concurrencyKey} session=${w.session.id??"(no id)"}`),w},closeSlot:async g=>{g.gone.dispose(),await g.session.end()},onDetach:(g,w)=>p(`slot detached pool=${i} slot=${g.concurrencyKey} reason=${w}`)});return u.set(i,m),m};return{async boot(){if(e.defaultApp===null)return;let t={appSlug:e.defaultApp};await f(t,E(t)).boot()},async sessionFor(t){let i=await s.resolveTargetFor(t),c=E(i),m=f(i,c).tryAcquireReady();if(!m)throw new j(f(i,c).stats().lastMissAgeMs);let g=l.get(m.entry)??[];return g.push(m),l.set(m.entry,g),{app:c,entry:m.entry,identity:H(i)}},async warmFor(t){let i=await s.resolveTargetFor(t);await f(i,E(i)).boot()},end(t){let i=l.get(t)?.shift();i&&(l.get(t)?.length===0&&l.delete(t),i.release())},evict(t){for(let i of u.values())i.evict(t)},reap(){for(let t of u.values())t.reap()},async pingReady(t){for(let i of u.values())if(await i.pingReady(t))return!0;return!1},stats(){return[...u.entries()].map(([t,i])=>({poolKey:t,...i.stats()}))},async drain(t){await Promise.all([...u.values()].map(i=>i.drain({timeoutMs:t})))},async close(){await Promise.all([...u.values()].map(t=>t.close()))}}}function ve(e){return pe(e?.name)?e?.runtime?.wake?.expectedWakePath==="portable-golden-restore"?Z:ue:Z}async function b(e,s){let a=e.whenLive;if(typeof a!="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 a.call(e,{timeout:ve(e.phase),signal:s})}async function ne(e){if(typeof e.cancel=="function")try{await e.cancel();return}catch{}if(typeof e.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");e.detach()}function O(e){return e.reason instanceof Error?e.reason:new Error("the client socket closed")}function Ke(e,s=N,a,d,u=_){let l=new WeakMap,p=new WeakMap,f=new WeakMap,t=new WeakMap,i=new WeakMap,c=n=>{let o=n.session.id;return typeof o=="string"&&o.length>0?o:null},m=n=>{let o=l.get(n);if(!o){let r={waiting:0,gone:new AbortController};i.set(n,r),o=(async()=>{if(await b(n.session,r.gone.signal),r.gone.signal.aborted)throw O(r.gone.signal);i.get(n)===r&&i.delete(n);let h=await R(n.session,await s());return Object.assign(h,{sessionId:n.session.id})})(),l.set(n,o),o.catch(()=>{l.get(n)===o&&l.delete(n)}),o.then(()=>{i.get(n)===r&&i.delete(n)},()=>{i.get(n)===r&&i.delete(n)})}return o},g=n=>{let o=t.get(n);return o||(o=(async()=>{let r=await m(n);return{appendInputAudio:h=>r.appendInputAudio(h),onOutputAudio:h=>r.onOutputAudio(h),sessionId:c(n)}})(),t.set(n,o),o.catch(()=>{t.get(n)===o&&t.delete(n)})),o},w=async(n,o,r)=>{if(r?.aborted){let S=i.get(n);throw(l.get(n)===void 0||S!==void 0&&S.waiting===0)&&await ne(n.session),O(r)}let h=o(n),y=i.get(n);return y!==void 0&&y.waiting++,r===void 0?h:new Promise((S,P)=>{let T=()=>{y!==void 0&&--y.waiting===0&&i.get(n)===y&&(y.gone.abort(),i.delete(n),l.delete(n),t.delete(n),ne(n.session).catch(()=>{})),P(O(r))};r.addEventListener("abort",T,{once:!0});let k=()=>r.removeEventListener("abort",T);h.then(I=>{k(),S(I)},I=>{k(),P(I)})})};return{createResponse:Q({sessionFor:d?n=>d.sessionFor(n):n=>e.sessionFor(n),sessionIdOf:n=>c(n),evict:(n,o)=>{d?d.evict(o):e.evict(n,o)},onAdmissionVerdict:(n,o,r)=>{Se(r)&&(d?d.evict(o):e.evict(n,o),a?.(n,r.kind==="never-live"?"never-live":"create-failed"))},createStream:async(n,o)=>{try{await b(n.session);let r=K(C(await n.responses.responses.create(o),n.gone),u);if(!d)return r;let h=n;return(async function*(){try{yield*r}catch(S){throw q(S)&&d.evict(h),S}finally{d.end(h)}})()}catch(r){throw d&&d.end(n),r}}}),listModels:()=>e.modelList(),openRouterModels:()=>e.openRouterModels(),supportsImages:(n,o)=>e.supportsImages(n,o),listImageModels:()=>e.imageModelList(),catalogIdentity:n=>e.catalogIdentityFor(n),servingAppSlug:async n=>await e.servingAppSlugFor(n),supportsAudio:n=>e.supportsAudio(n),supportsSpeech:n=>e.supportsSpeech(n),speechVoices:n=>e.speechVoices(n),supportsTranscription:n=>e.supportsTranscription(n),transcriptStreamFor:n=>e.transcriptStreamFor(n),listSpeechToTextModels:()=>e.speechToTextModelList(),openAudio:async(n,o)=>{let{entry:r}=await e.sessionFor(n);return await w(r,m,o)},openAudioOn:async(n,o)=>{let{entry:r}=await e.sessionForHandle(n);return await w(r,g,o)},openTurnsOn:async function*(n,o){if(o?.aborted)throw O(o);let{entry:r}=await e.sessionForHandle(n);yield*$(r.session,o)},openVideo:async n=>{let{entry:o}=await e.sessionFor(n),r=p.get(o);return r||(r=D(o.session),p.set(o,r)),r},openVideoOut:async n=>{let{entry:o}=await e.sessionFor(n),r=f.get(o);return r||(r=U(o.session),f.set(o,r)),r},sessionHandle:async n=>(await e.handleFor(n)).handle,speechSessionHandle:async n=>(await e.speechHandleFor(n)).handle,evictSpeechSession:async n=>{let{app:o,entry:r}=await e.sessionForHandle(n);await e.evict(o,r)},createResponseOn:async(n,o)=>{let{entry:r}=await e.sessionForHandle(n);return await b(r.session),C(await r.responses.responses.create(o),r.gone)},onSessionEnd:async(n,o)=>{let{entry:r}=await e.sessionForHandle(n),h=r.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 y=!1;return h.onPhase(S=>{if(y||!ye.has(S.name))return;y=!0;let P=h.endsAt??null;o({timeLeftMs:P?Math.max(0,P.getTime()-Date.now()):null,reason:S.reason??`session ${S.name}`})})}}}export{He as a,Ve as b,je as c,Be as d,Ke as e};