@urun-sh/openai 0.5.3 → 0.5.4
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-4MSBS7M6.js +1 -0
- package/dist/{chunk-G3NMGP4N.js → chunk-GBBY3PCZ.js} +1 -1
- package/dist/chunk-K23AZPI4.js +1 -0
- package/dist/chunk-OI2OY32M.js +1 -0
- package/dist/chunk-RQBD4OAA.js +6 -0
- package/dist/{chunk-FVD4NMXJ.js → chunk-STFRT5OY.js} +1 -1
- package/dist/{chunk-M6ICU4F5.js → chunk-VNM4M4P3.js} +12 -12
- package/dist/gemini-live.cjs +2 -2
- package/dist/gemini-live.d.cts +6 -0
- package/dist/gemini-live.d.ts +6 -0
- package/dist/gemini-live.js +1 -1
- package/dist/hosted/bin.cjs +16 -16
- package/dist/hosted/bin.js +1 -1
- package/dist/hosted/index.cjs +16 -16
- package/dist/hosted/index.d.cts +94 -13
- package/dist/hosted/index.d.ts +44 -11
- package/dist/hosted/index.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -18
- package/dist/index.d.ts +2 -18
- package/dist/index.js +1 -1
- package/dist/models-NYMZrklp.d.cts +38 -0
- package/dist/models-NYMZrklp.d.ts +38 -0
- package/dist/pi-extension/index.cjs +6 -6
- package/dist/pi-extension/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +49 -49
- package/dist/proxy/cli.cjs +31 -31
- package/dist/proxy/cli.js +1 -1
- package/dist/proxy/index.cjs +13 -13
- package/dist/proxy/index.d.cts +1 -1
- package/dist/proxy/index.d.ts +1 -1
- package/dist/proxy/index.js +1 -1
- package/dist/{server-l1himPxc.d.ts → server-DuH_5OD9.d.ts} +2 -0
- package/dist/{server-BfME37pQ.d.cts → server-wmUnMewW.d.cts} +6 -0
- package/package.json +3 -3
- package/dist/chunk-F2TEK34X.js +0 -1
- package/dist/chunk-ODHCPZHI.js +0 -6
- package/dist/chunk-UVAY7Q7Z.js +0 -1
- package/dist/chunk-XHIIEA6Z.js +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var Q=Object.create;var h=Object.defineProperty;var K=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var X=Object.getPrototypeOf,Z=Object.prototype.hasOwnProperty;var ee=(e,t)=>{for(var r in t)h(e,r,{get:t[r],enumerable:!0})},x=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of z(t))!Z.call(e,o)&&o!==r&&h(e,o,{get:()=>t[o],enumerable:!(n=K(t,o))||n.enumerable});return e};var b=(e,t,r)=>(r=e!=null?Q(X(e)):{},x(t||!e||!e.__esModule?h(r,"default",{value:e,enumerable:!0}):r,e)),te=e=>x(h({},"__esModule",{value:!0}),e);var le={};ee(le,{RT_VIDEO_OUT_STREAM:()=>w,SUPPORTED_REALTIME_CLIENT_EVENTS:()=>A,SUPPORTED_RESPONSES_EVENTS:()=>T,UrunRealtime:()=>k,UrunResponses:()=>_,VIDEO_OUT_DEFAULT_CODEC:()=>E,VIDEO_OUT_FRAMING:()=>R,VIDEO_OUT_WS_FRAMING:()=>W,consumeVideoOut:()=>q,decodeVideoOutRecord:()=>v,encodeVideoOutRecord:()=>$,isSupportedClientEvent:()=>F,isSupportedResponsesEvent:()=>j,listModels:()=>B,negotiateSessionVideoOutQuic:()=>N,nodeAudioBackend:()=>P,openSessionVideoOutLane:()=>H,toErrorEvent:()=>f,weriftAudioBackend:()=>U});module.exports=te(le);function I(e,t,r,n){let o=e.response??{},i={request_id:r,consumer_id:n,stream:!0,kind:"chat",messages:t};return typeof o.instructions=="string"&&(i.instructions=o.instructions),Array.isArray(o.modalities)&&(i.modalities=o.modalities),typeof o.temperature=="number"&&(i.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(i.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(i.tools=o.tools),i}function L(e,t,r){let n={request_id:t,consumer_id:r,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(n.model=e.model),e.tools!==void 0&&(n.tools=e.tools),e.tool_choice!==void 0&&(n.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(n.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(n.max_output_tokens=e.max_output_tokens),typeof e.reasoning_effort=="string"&&(n.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(n.chat_template_kwargs=e.chat_template_kwargs),n}function f(e){if(e instanceof Error)return{type:"error",error:{type:"urun_error",code:e.name||null,message:e.message}};if(e&&typeof e=="object"&&e.t==="error"){let t=e,r=t.body??{};return{type:"error",error:{type:"urun_error",code:t.code??null,message:r.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var re="llm-resp";async function*S(e,t){let r=`${re}:${t}`,n=`resp_${t}`;yield{type:"response.created",response:{id:n,status:"in_progress"}};let o=new Map;for await(let i of e.stream(r).messages()){let d=i;if(d.t==="delta"){if(typeof d.delta=="string"&&(yield{type:"response.output_text.delta",item_id:n,delta:d.delta}),typeof d.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:n,delta:d.reasoning}),Array.isArray(d.tool_calls))for(let c of d.tool_calls){let s=typeof c.index=="number"?c.index:0,a=o.get(s)??{};c.id&&(a.id=c.id),c.function?.name&&(a.name=c.function.name),o.set(s,a),yield{type:"response.function_call_arguments.delta",item_id:`fc_${t}_${s}`,tool_index:s,call_id:a.id,name:a.name,delta:c.function?.arguments??""}}}else if(d.t==="response"){yield{type:"response.completed",response:{id:n,status:"completed",...d.body}};return}else if(d.t==="error"){yield f(d);return}}}var ne="llm",g=class{constructor(t){this.session=t;this.sessionTag=t.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(t){t.session_tag=this.sessionTag,this.session.doc(ne).set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,r){let n=L(t,r,this.consumerId);return this.write(n),S(this.session,r)}sendResponseCreate(t,r,n){let o=I(t,r,n,this.consumerId);return this.write(o),S(this.session,n)}};var O=class{constructor(t,r){this.backend=t;this.opts=r}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(t){if(!this.source)throw new Error("startOutbound() not called");let r=Buffer.from(t,"base64"),n=Math.floor(r.byteLength/2),o=new Int16Array(n);for(let i=0;i<n;i++)o[i]=r.readInt16LE(i*2);this.source.onData(o)}async startInbound(t){if(t==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("rt-audio-out").track is null)');this.sink=this.backend.createSink(t),this.sink.onframe=r=>{let n=Buffer.from(r.samples.buffer,r.samples.byteOffset,r.samples.byteLength).toString("base64");for(let o of this.outHandlers)o(n)}}onOutputAudio(t){return this.outHandlers.add(t),()=>this.outHandlers.delete(t)}};async function V(e,t,r=1e4){let n=new O(t,{sampleRate:24e3}),o=await n.startOutbound();await e.stream("rt-audio-in").attach(o);let i=await oe(e,r);return await n.startInbound(i),n}function oe(e,t){let r=e.stream("rt-audio-out");return r.track!=null?Promise.resolve(r.track):new Promise((n,o)=>{let i,d=setTimeout(()=>{i?.(),o(new Error(`enableSessionAudio: no downstream audio track within ${t}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},t),c=s=>{s!=null&&(clearTimeout(d),i?.(),n(s))};i=r.on?.("track",c),r.track!=null&&c(r.track)})}var se=24e3,y=480,ie=111;async function ae(){let e=await import("opusscript"),t=e.default??e;return new t(se,1,2048)}async function U(){let e=await import("werift"),t=await ae();return{createSource:()=>{let r=new e.MediaStreamTrack({kind:"audio"}),n=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,i=Math.random()*4294967295>>>0,d=new Int16Array(0);return{track:r,onData:c=>{let s=new Int16Array(d.length+c.length);s.set(d,0),s.set(c,d.length);let a=0;for(;s.length-a>=y;){let u=s.subarray(a,a+y);a+=y;let l=Buffer.from(u.buffer,u.byteOffset,u.byteLength),Y=t.encode(l,y);o=o+1&65535,i=i+y>>>0;let G=new e.RtpHeader({version:2,payloadType:ie,sequenceNumber:o,timestamp:i,ssrc:n,marker:!1}),J=new e.RtpPacket(G,Y);r.writeRtp(J)}d=s.slice(a)}}},createSink:r=>{let n={onframe:null};return r.onReceiveRtp.subscribe(i=>{let d;try{d=t.decode(i.payload)}catch{return}let c=new Int16Array(d.length/2);for(let s=0;s<c.length;s++)c[s]=d.readInt16LE(s*2);n.onframe?.({samples:c})}),n}}}async function P(){let e=await import("@roamhq/wrtc");return{createSource:()=>{let t=new e.nonstandard.RTCAudioSource;return{track:t.createTrack(),onData:n=>t.onData({samples:n,sampleRate:24e3})}},createSink:t=>{let r={onframe:null},n=new e.nonstandard.RTCAudioSink(t);return n.ondata=o=>{r.onframe?.(o)},r}}}var D=0;function de(){return D+=1,`rt_${Date.now().toString(36)}_${D.toString(36)}`}var k=class{constructor(t,r={}){this.session=t;this.opts=r;this.transport=new g(t)}session;opts;transport;handlers=new Set;conversation=[];active=null;audio=null;currentRequestId=null;async enableAudio(){if(!this.opts.audioBackend)throw new Error("audioBackend required for audio");this.audio=await V(this.session,this.opts.audioBackend),this.audio.onOutputAudio(t=>this.emit({type:"response.audio.delta",delta:t}))}on(t,r){return this.handlers.add(r),()=>this.handlers.delete(r)}emit(t){for(let r of this.handlers)r(t)}send(t){switch(t.type){case"conversation.item.create":{let r=(t.item?.content??[]).map(n=>n.text??"").join("");this.conversation.push({role:t.item?.role??"user",content:r});return}case"response.create":{let r=de();this.currentRequestId=r;let n=this.transport.sendResponseCreate({type:"response.create",response:t.response},this.conversation,r);this.active=(async()=>{try{for await(let o of n)this.emit(o)}catch(o){this.emit(f(o))}})();return}case"input_audio_buffer.append":this.audio?.appendInputAudio(t.audio);return;case"input_audio_buffer.commit":case"input_audio_buffer.clear":return;case"response.cancel":return;default:return}}async drain(){this.active&&await this.active}};var C=0;function ce(){return C+=1,`req_${Date.now().toString(36)}_${C.toString(36)}`}var _=class{transport;constructor(t){this.transport=new g(t)}responses={create:async t=>{let r=ce(),n=this.transport.sendResponses(t,r);return Object.assign((async function*(){yield*n})(),{requestId:r})}}};async function ue(e){let r=await(e.fetchImpl??fetch)(`${e.catalogUrl}/model_catalog?select=model_id,variant`,{headers:{apikey:e.anonKey,Authorization:`Bearer ${e.anonKey}`}});if(!r.ok)throw new Error(`model_catalog fetch failed: ${r.status}`);return await r.json()}async function B(e){return{object:"list",data:(await ue(e)).map(r=>({id:`${r.model_id}:${r.variant}`,object:"model",created:0,owned_by:"urun"}))}}var m=require("@urun-sh/core"),w="rt-video-out",R="urun.videoOut.v1",E="h264";function M(e,t){if(!Number.isInteger(e.seq)||e.seq<0)throw new Error(`${t}: header seq must be a non-negative integer (got ${e.seq})`);if(!Number.isInteger(e.ptsUs))throw new Error(`${t}: header ptsUs must be an integer microsecond timestamp (got ${e.ptsUs})`);if(typeof e.codec!="string"||e.codec.length===0)throw new Error(`${t}: header codec must be a non-empty string`);if(typeof e.key!="boolean")throw new Error(`${t}: header key must be a boolean`)}function $(e,t){if(M(e,"encodeVideoOutRecord"),t.byteLength===0)throw new Error("encodeVideoOutRecord: refusing to encode an empty access unit");let r=new TextEncoder().encode(JSON.stringify({v:1,seq:e.seq,ptsUs:e.ptsUs,codec:e.codec,key:e.key}));if(r.byteLength>65535)throw new Error(`encodeVideoOutRecord: header JSON is ${r.byteLength} bytes \u2014 over the u16 length prefix`);let n=new Uint8Array(2+r.byteLength+t.byteLength);return n[0]=r.byteLength&255,n[1]=r.byteLength>>8,n.set(r,2),n.set(t,2+r.byteLength),n}function v(e){if(e.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${e.byteLength} byte(s), no header length)`);let t=e[0]|e[1]<<8;if(e.byteLength<2+t)throw new Error(`decodeVideoOutRecord: truncated record (header says ${t} bytes, ${e.byteLength-2} present)`);let r;try{r=JSON.parse(new TextDecoder().decode(e.subarray(2,2+t)))}catch(d){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(d)})`)}let n=r;if(n===null||typeof n!="object"||n.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(n?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let o=n;M(o,"decodeVideoOutRecord");let i=e.subarray(2+t);if(i.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:o,au:i.slice()}}async function N(e){let t=e.wtDataEndpoint;if(typeof t!="function")throw new Error("negotiateSessionVideoOutQuic: the core session exposes no wtDataEndpoint() \u2014 the \xA75-over-QUIC dial seam (urun-infra#1608 TransportOfferResponse.webtransport, offered with wtData: true) is required for the video OUT lane; there is no WS fallback");let r=await t.call(e),n=r.sessionId??e.sessionId;if(typeof n!="string"||n.length===0)throw new Error("negotiateSessionVideoOutQuic: no session id \u2014 the WT hello requires one and neither the negotiated endpoint nor the core session carries it");return{transport:"webtransport",url:r.url,certHashes:r.certHashes,token:r.token,sessionId:n,stream:w,codec:E,framing:R}}var W="urun.videoOut.v2-ws";function H(e){let t=e.stream(w);if(typeof t.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${w}') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the video OUT lane`);let r=new Set,n=new Set,o=new Set,i=s=>{for(let a of n)a(s)},d=!1,c=async()=>{for await(let s of t.messages()){if(!(s instanceof Uint8Array)){i(new Error(`openSessionVideoOutLane: "${w}" delivered a non-BINARY \xA75 payload (${typeof s}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let a;try{a=v(s)}catch(u){i(u instanceof Error?u:new Error(String(u)));continue}for(let u of r)u(a)}for(let s of o)s()};return{codec:E,onOutputFrame:s=>(r.add(s),d||(d=!0,c().catch(a=>i(a instanceof Error?a:new Error(String(a))))),()=>r.delete(s)),onLaneError:s=>(n.add(s),()=>n.delete(s)),onEnd:s=>(o.add(s),()=>o.delete(s))}}function pe(e){if(e.length%2!==0||/[^0-9a-fA-F]/.test(e))throw new Error(`consumeVideoOut: certHashes entry is not hex sha-256 (${JSON.stringify(e)})`);let t=new Uint8Array(e.length/2);for(let r=0;r<t.length;r++)t[r]=parseInt(e.slice(r*2,r*2+2),16);return t}async function q(e,t,r){let n=r??(typeof globalThis.WebTransport=="function"?(a,u)=>new globalThis.WebTransport(a,u):void 0);if(!n)throw new Error("consumeVideoOut: no WebTransport implementation \u2014 this platform has no globalThis.WebTransport; inject a connect factory (the third argument)");let o=n(e.url,{serverCertificateHashes:e.certHashes.map(a=>({algorithm:"sha-256",value:pe(a)}))});await o.ready;let i=await o.createBidirectionalStream();await i.writable.getWriter().write((0,m.encodeWtControlFrame)({t:"hello",token:e.token,sessionId:e.sessionId}));let d=i.readable.getReader(),c=new m.WtControlDecoder,s=!1;for(;!s;){let{value:a,done:u}=await d.read();if(u)throw new Error("consumeVideoOut: gateway closed the control stream before hello-ack");if(a)for(let l of c.push(a)){if(l.t==="hello-ack"){s=!0;break}throw new Error(`consumeVideoOut: gateway rejected hello (got ${JSON.stringify(l.t)} before hello-ack)`)}}await(0,m.consumeWtDataStreams)(o.incomingUnidirectionalStreams,{onData:(a,u)=>{if(a===e.stream){if(u===m.STREAM_END){t.onEnd?.();return}if(!(u instanceof Uint8Array)){t.onLaneError(new Error(`consumeVideoOut: "${e.stream}" delivered a non-BINARY \xA75 payload (${typeof u}) \u2014 spec v1 records ride BINARY envelopes only`));return}try{t.onFrame(v(u))}catch(l){t.onLaneError(l instanceof Error?l:new Error(String(l)))}}},onLaneError:a=>t.onLaneError(a)})}var A=["session.update","conversation.item.create","response.create","response.cancel","input_audio_buffer.append","input_audio_buffer.commit","input_audio_buffer.clear"],T=["response.created","response.output_text.delta","response.output_item.added","response.function_call_arguments.delta","response.image_generation_call.partial_image","response.completed","error"];function F(e){return A.includes(e)}function j(e){return T.includes(e)}0&&(module.exports={RT_VIDEO_OUT_STREAM,SUPPORTED_REALTIME_CLIENT_EVENTS,SUPPORTED_RESPONSES_EVENTS,UrunRealtime,UrunResponses,VIDEO_OUT_DEFAULT_CODEC,VIDEO_OUT_FRAMING,VIDEO_OUT_WS_FRAMING,consumeVideoOut,decodeVideoOutRecord,encodeVideoOutRecord,isSupportedClientEvent,isSupportedResponsesEvent,listModels,negotiateSessionVideoOutQuic,nodeAudioBackend,openSessionVideoOutLane,toErrorEvent,weriftAudioBackend});
|
|
1
|
+
"use strict";var z=Object.create;var h=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames;var X=Object.getPrototypeOf,Z=Object.prototype.hasOwnProperty;var ee=(e,t)=>{for(var n in t)h(e,n,{get:t[n],enumerable:!0})},x=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of K(t))!Z.call(e,o)&&o!==n&&h(e,o,{get:()=>t[o],enumerable:!(r=Q(t,o))||r.enumerable});return e};var v=(e,t,n)=>(n=e!=null?z(X(e)):{},x(t||!e||!e.__esModule?h(n,"default",{value:e,enumerable:!0}):n,e)),te=e=>x(h({},"__esModule",{value:!0}),e);var le={};ee(le,{RT_VIDEO_OUT_STREAM:()=>w,SUPPORTED_REALTIME_CLIENT_EVENTS:()=>A,SUPPORTED_RESPONSES_EVENTS:()=>T,UrunRealtime:()=>k,UrunResponses:()=>_,VIDEO_OUT_DEFAULT_CODEC:()=>b,VIDEO_OUT_FRAMING:()=>R,VIDEO_OUT_WS_FRAMING:()=>W,consumeVideoOut:()=>q,decodeVideoOutRecord:()=>E,encodeVideoOutRecord:()=>$,isSupportedClientEvent:()=>F,isSupportedResponsesEvent:()=>j,listModels:()=>B,negotiateSessionVideoOutQuic:()=>N,nodeAudioBackend:()=>P,openSessionVideoOutLane:()=>H,toErrorEvent:()=>m,weriftAudioBackend:()=>U});module.exports=te(le);function I(e,t,n,r){let o=e.response??{},i={request_id:n,consumer_id:r,stream:!0,kind:"chat",messages:t};return typeof o.instructions=="string"&&(i.instructions=o.instructions),Array.isArray(o.modalities)&&(i.modalities=o.modalities),typeof o.temperature=="number"&&(i.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(i.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(i.tools=o.tools),i}function L(e,t,n){let r={request_id:t,consumer_id:n,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(r.model=e.model),e.tools!==void 0&&(r.tools=e.tools),e.tool_choice!==void 0&&(r.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(r.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(r.max_output_tokens=e.max_output_tokens),typeof e.top_p=="number"&&(r.top_p=e.top_p),Array.isArray(e.stop)&&e.stop.length>0&&(r.stop=e.stop),typeof e.reasoning_effort=="string"&&(r.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(r.chat_template_kwargs=e.chat_template_kwargs),r}function m(e){if(e instanceof Error)return{type:"error",error:{type:"urun_error",code:e.name||null,message:e.message}};if(e&&typeof e=="object"&&e.t==="error"){let t=e,n=t.body??{};return{type:"error",error:{type:"urun_error",code:t.code??null,message:n.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var ne="llm-resp";async function*S(e,t){let n=`${ne}:${t}`,r=`resp_${t}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;for await(let i of e.stream(n).messages()){let d=i;if(d.t==="delta"){if(typeof d.delta=="string"&&(yield{type:"response.output_text.delta",item_id:r,delta:d.delta}),typeof d.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:r,delta:d.reasoning}),Array.isArray(d.tool_calls))for(let u of d.tool_calls){let s=typeof u.index=="number"?u.index:0,a=o.get(s)??{};u.id&&(a.id=u.id),u.function?.name&&(a.name=u.function.name),o.set(s,a),yield{type:"response.function_call_arguments.delta",item_id:`fc_${t}_${s}`,tool_index:s,call_id:a.id,name:a.name,delta:u.function?.arguments??""}}}else if(d.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...d.body}};return}else if(d.t==="error"){yield m(d);return}}}var re="llm",g=class{constructor(t){this.session=t;this.sessionTag=t.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(t){t.session_tag=this.sessionTag,this.session.doc(re).set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,n){let r=L(t,n,this.consumerId);return this.write(r),S(this.session,n)}sendResponseCreate(t,n,r){let o=I(t,n,r,this.consumerId);return this.write(o),S(this.session,r)}};var O=class{constructor(t,n){this.backend=t;this.opts=n}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(t){if(!this.source)throw new Error("startOutbound() not called");let n=Buffer.from(t,"base64"),r=Math.floor(n.byteLength/2),o=new Int16Array(r);for(let i=0;i<r;i++)o[i]=n.readInt16LE(i*2);this.source.onData(o)}async startInbound(t){if(t==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("rt-audio-out").track is null)');this.sink=this.backend.createSink(t),this.sink.onframe=n=>{let r=Buffer.from(n.samples.buffer,n.samples.byteOffset,n.samples.byteLength).toString("base64");for(let o of this.outHandlers)o(r)}}onOutputAudio(t){return this.outHandlers.add(t),()=>this.outHandlers.delete(t)}};async function V(e,t,n=1e4){let r=new O(t,{sampleRate:24e3}),o=await r.startOutbound();await e.stream("rt-audio-in").attach(o);let i=await oe(e,n);return await r.startInbound(i),r}function oe(e,t){let n=e.stream("rt-audio-out");return n.track!=null?Promise.resolve(n.track):new Promise((r,o)=>{let i,d=setTimeout(()=>{i?.(),o(new Error(`enableSessionAudio: no downstream audio track within ${t}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},t),u=s=>{s!=null&&(clearTimeout(d),i?.(),r(s))};i=n.on?.("track",u),n.track!=null&&u(n.track)})}var se=24e3,y=480,ie=111;async function ae(){let e=await import("opusscript"),t=e.default??e;return new t(se,1,2048)}async function U(){let e=await import("werift"),t=await ae();return{createSource:()=>{let n=new e.MediaStreamTrack({kind:"audio"}),r=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,i=Math.random()*4294967295>>>0,d=new Int16Array(0);return{track:n,onData:u=>{let s=new Int16Array(d.length+u.length);s.set(d,0),s.set(u,d.length);let a=0;for(;s.length-a>=y;){let c=s.subarray(a,a+y);a+=y;let l=Buffer.from(c.buffer,c.byteOffset,c.byteLength),Y=t.encode(l,y);o=o+1&65535,i=i+y>>>0;let G=new e.RtpHeader({version:2,payloadType:ie,sequenceNumber:o,timestamp:i,ssrc:r,marker:!1}),J=new e.RtpPacket(G,Y);n.writeRtp(J)}d=s.slice(a)}}},createSink:n=>{let r={onframe:null};return n.onReceiveRtp.subscribe(i=>{let d;try{d=t.decode(i.payload)}catch{return}let u=new Int16Array(d.length/2);for(let s=0;s<u.length;s++)u[s]=d.readInt16LE(s*2);r.onframe?.({samples:u})}),r}}}async function P(){let e=await import("@roamhq/wrtc");return{createSource:()=>{let t=new e.nonstandard.RTCAudioSource;return{track:t.createTrack(),onData:r=>t.onData({samples:r,sampleRate:24e3})}},createSink:t=>{let n={onframe:null},r=new e.nonstandard.RTCAudioSink(t);return r.ondata=o=>{n.onframe?.(o)},n}}}var D=0;function de(){return D+=1,`rt_${Date.now().toString(36)}_${D.toString(36)}`}var k=class{constructor(t,n={}){this.session=t;this.opts=n;this.transport=new g(t)}session;opts;transport;handlers=new Set;conversation=[];active=null;audio=null;currentRequestId=null;async enableAudio(){if(!this.opts.audioBackend)throw new Error("audioBackend required for audio");this.audio=await V(this.session,this.opts.audioBackend),this.audio.onOutputAudio(t=>this.emit({type:"response.audio.delta",delta:t}))}on(t,n){return this.handlers.add(n),()=>this.handlers.delete(n)}emit(t){for(let n of this.handlers)n(t)}send(t){switch(t.type){case"conversation.item.create":{let n=(t.item?.content??[]).map(r=>r.text??"").join("");this.conversation.push({role:t.item?.role??"user",content:n});return}case"response.create":{let n=de();this.currentRequestId=n;let r=this.transport.sendResponseCreate({type:"response.create",response:t.response},this.conversation,n);this.active=(async()=>{try{for await(let o of r)this.emit(o)}catch(o){this.emit(m(o))}})();return}case"input_audio_buffer.append":this.audio?.appendInputAudio(t.audio);return;case"input_audio_buffer.commit":case"input_audio_buffer.clear":return;case"response.cancel":return;default:return}}async drain(){this.active&&await this.active}};var C=0;function ue(){return C+=1,`req_${Date.now().toString(36)}_${C.toString(36)}`}var _=class{transport;constructor(t){this.transport=new g(t)}responses={create:async t=>{let n=ue(),r=this.transport.sendResponses(t,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};async function ce(e){let n=await(e.fetchImpl??fetch)(`${e.catalogUrl}/model_catalog?select=model_id,variant,gpu_spec,engine,task,description,size_gb,hf_repo,engine_args,updated_at`,{headers:{apikey:e.anonKey,Authorization:`Bearer ${e.anonKey}`}});if(!n.ok)throw new Error(`model_catalog fetch failed: ${n.status}`);return await n.json()}async function B(e){return{object:"list",data:(await ce(e)).map(n=>({id:`${n.model_id}:${n.variant}`,object:"model",created:0,owned_by:"urun"}))}}var f=require("@urun-sh/core"),w="rt-video-out",R="urun.videoOut.v1",b="h264";function M(e,t){if(!Number.isInteger(e.seq)||e.seq<0)throw new Error(`${t}: header seq must be a non-negative integer (got ${e.seq})`);if(!Number.isInteger(e.ptsUs))throw new Error(`${t}: header ptsUs must be an integer microsecond timestamp (got ${e.ptsUs})`);if(typeof e.codec!="string"||e.codec.length===0)throw new Error(`${t}: header codec must be a non-empty string`);if(typeof e.key!="boolean")throw new Error(`${t}: header key must be a boolean`)}function $(e,t){if(M(e,"encodeVideoOutRecord"),t.byteLength===0)throw new Error("encodeVideoOutRecord: refusing to encode an empty access unit");let n=new TextEncoder().encode(JSON.stringify({v:1,seq:e.seq,ptsUs:e.ptsUs,codec:e.codec,key:e.key}));if(n.byteLength>65535)throw new Error(`encodeVideoOutRecord: header JSON is ${n.byteLength} bytes \u2014 over the u16 length prefix`);let r=new Uint8Array(2+n.byteLength+t.byteLength);return r[0]=n.byteLength&255,r[1]=n.byteLength>>8,r.set(n,2),r.set(t,2+n.byteLength),r}function E(e){if(e.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${e.byteLength} byte(s), no header length)`);let t=e[0]|e[1]<<8;if(e.byteLength<2+t)throw new Error(`decodeVideoOutRecord: truncated record (header says ${t} bytes, ${e.byteLength-2} present)`);let n;try{n=JSON.parse(new TextDecoder().decode(e.subarray(2,2+t)))}catch(d){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(d)})`)}let r=n;if(r===null||typeof r!="object"||r.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(r?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let o=r;M(o,"decodeVideoOutRecord");let i=e.subarray(2+t);if(i.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:o,au:i.slice()}}async function N(e){let t=e.wtDataEndpoint;if(typeof t!="function")throw new Error("negotiateSessionVideoOutQuic: the core session exposes no wtDataEndpoint() \u2014 the \xA75-over-QUIC dial seam (urun-infra#1608 TransportOfferResponse.webtransport, offered with wtData: true) is required for the video OUT lane; there is no WS fallback");let n=await t.call(e),r=n.sessionId??e.sessionId;if(typeof r!="string"||r.length===0)throw new Error("negotiateSessionVideoOutQuic: no session id \u2014 the WT hello requires one and neither the negotiated endpoint nor the core session carries it");return{transport:"webtransport",url:n.url,certHashes:n.certHashes,token:n.token,sessionId:r,stream:w,codec:b,framing:R}}var W="urun.videoOut.v2-ws";function H(e){let t=e.stream(w);if(typeof t.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${w}') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the video OUT lane`);let n=new Set,r=new Set,o=new Set,i=s=>{for(let a of r)a(s)},d=!1,u=async()=>{for await(let s of t.messages()){if(!(s instanceof Uint8Array)){i(new Error(`openSessionVideoOutLane: "${w}" delivered a non-BINARY \xA75 payload (${typeof s}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let a;try{a=E(s)}catch(c){i(c instanceof Error?c:new Error(String(c)));continue}for(let c of n)c(a)}for(let s of o)s()};return{codec:b,onOutputFrame:s=>(n.add(s),d||(d=!0,u().catch(a=>i(a instanceof Error?a:new Error(String(a))))),()=>n.delete(s)),onLaneError:s=>(r.add(s),()=>r.delete(s)),onEnd:s=>(o.add(s),()=>o.delete(s))}}function pe(e){if(e.length%2!==0||/[^0-9a-fA-F]/.test(e))throw new Error(`consumeVideoOut: certHashes entry is not hex sha-256 (${JSON.stringify(e)})`);let t=new Uint8Array(e.length/2);for(let n=0;n<t.length;n++)t[n]=parseInt(e.slice(n*2,n*2+2),16);return t}async function q(e,t,n){let r=n??(typeof globalThis.WebTransport=="function"?(a,c)=>new globalThis.WebTransport(a,c):void 0);if(!r)throw new Error("consumeVideoOut: no WebTransport implementation \u2014 this platform has no globalThis.WebTransport; inject a connect factory (the third argument)");let o=r(e.url,{serverCertificateHashes:e.certHashes.map(a=>({algorithm:"sha-256",value:pe(a)}))});await o.ready;let i=await o.createBidirectionalStream();await i.writable.getWriter().write((0,f.encodeWtControlFrame)({t:"hello",token:e.token,sessionId:e.sessionId}));let d=i.readable.getReader(),u=new f.WtControlDecoder,s=!1;for(;!s;){let{value:a,done:c}=await d.read();if(c)throw new Error("consumeVideoOut: gateway closed the control stream before hello-ack");if(a)for(let l of u.push(a)){if(l.t==="hello-ack"){s=!0;break}throw new Error(`consumeVideoOut: gateway rejected hello (got ${JSON.stringify(l.t)} before hello-ack)`)}}await(0,f.consumeWtDataStreams)(o.incomingUnidirectionalStreams,{onData:(a,c)=>{if(a===e.stream){if(c===f.STREAM_END){t.onEnd?.();return}if(!(c instanceof Uint8Array)){t.onLaneError(new Error(`consumeVideoOut: "${e.stream}" delivered a non-BINARY \xA75 payload (${typeof c}) \u2014 spec v1 records ride BINARY envelopes only`));return}try{t.onFrame(E(c))}catch(l){t.onLaneError(l instanceof Error?l:new Error(String(l)))}}},onLaneError:a=>t.onLaneError(a)})}var A=["session.update","conversation.item.create","response.create","response.cancel","input_audio_buffer.append","input_audio_buffer.commit","input_audio_buffer.clear"],T=["response.created","response.output_text.delta","response.output_item.added","response.function_call_arguments.delta","response.image_generation_call.partial_image","response.completed","error"];function F(e){return A.includes(e)}function j(e){return T.includes(e)}0&&(module.exports={RT_VIDEO_OUT_STREAM,SUPPORTED_REALTIME_CLIENT_EVENTS,SUPPORTED_RESPONSES_EVENTS,UrunRealtime,UrunResponses,VIDEO_OUT_DEFAULT_CODEC,VIDEO_OUT_FRAMING,VIDEO_OUT_WS_FRAMING,consumeVideoOut,decodeVideoOutRecord,encodeVideoOutRecord,isSupportedClientEvent,isSupportedResponsesEvent,listModels,negotiateSessionVideoOutQuic,nodeAudioBackend,openSessionVideoOutLane,toErrorEvent,weriftAudioBackend});
|
package/dist/index.d.cts
CHANGED
|
@@ -2,6 +2,7 @@ import { b as UrunSessionLike, L as LaneMessage } from './types-lsVTbNcH.cjs';
|
|
|
2
2
|
import { A as AudioBackend } from './video-out-D20UuJ8G.cjs';
|
|
3
3
|
export { b as AudioSink, c as AudioSource, d as RT_VIDEO_OUT_STREAM, e as VIDEO_OUT_DEFAULT_CODEC, f as VIDEO_OUT_FRAMING, g as VIDEO_OUT_WS_FRAMING, i as VideoOutEndpoint, j as VideoOutLane, k as VideoOutRecord, l as VideoOutRecordHeader, m as VideoOutSink, n as VideoOutWtConnect, o as VideoOutWtSession, p as consumeVideoOut, q as decodeVideoOutRecord, t as encodeVideoOutRecord, u as negotiateSessionVideoOutQuic, v as nodeAudioBackend, w as openSessionVideoOutLane, x as weriftAudioBackend } from './video-out-D20UuJ8G.cjs';
|
|
4
4
|
export { U as UrunResponses } from './ResponsesClient-Dft3bg3b.cjs';
|
|
5
|
+
export { M as ModelList, a as ModelObject, l as listModels } from './models-NYMZrklp.cjs';
|
|
5
6
|
export { RealtimeClientEvent, RealtimeServerEvent } from 'openai/resources/beta/realtime/realtime';
|
|
6
7
|
export { ResponseStreamEvent } from 'openai/resources/responses/responses';
|
|
7
8
|
|
|
@@ -38,23 +39,6 @@ declare class UrunRealtime {
|
|
|
38
39
|
drain(): Promise<void>;
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
interface ModelObject {
|
|
42
|
-
id: string;
|
|
43
|
-
object: 'model';
|
|
44
|
-
created: number;
|
|
45
|
-
owned_by: string;
|
|
46
|
-
}
|
|
47
|
-
interface ModelList {
|
|
48
|
-
object: 'list';
|
|
49
|
-
data: ModelObject[];
|
|
50
|
-
}
|
|
51
|
-
interface CatalogOptions {
|
|
52
|
-
catalogUrl: string;
|
|
53
|
-
anonKey: string;
|
|
54
|
-
fetchImpl?: typeof fetch;
|
|
55
|
-
}
|
|
56
|
-
declare function listModels(opts: CatalogOptions): Promise<ModelList>;
|
|
57
|
-
|
|
58
42
|
interface OpenAIErrorEvent {
|
|
59
43
|
type: 'error';
|
|
60
44
|
error: {
|
|
@@ -73,4 +57,4 @@ declare const SUPPORTED_RESPONSES_EVENTS: readonly ["response.created", "respons
|
|
|
73
57
|
declare function isSupportedClientEvent(type: string): boolean;
|
|
74
58
|
declare function isSupportedResponsesEvent(type: string): boolean;
|
|
75
59
|
|
|
76
|
-
export { AudioBackend, LaneMessage, type
|
|
60
|
+
export { AudioBackend, LaneMessage, type OpenAIErrorEvent, SUPPORTED_REALTIME_CLIENT_EVENTS, SUPPORTED_RESPONSES_EVENTS, UrunRealtime, UrunSessionLike, isSupportedClientEvent, isSupportedResponsesEvent, toErrorEvent };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { b as UrunSessionLike, L as LaneMessage } from './types-lsVTbNcH.js';
|
|
|
2
2
|
import { A as AudioBackend } from './video-out-CWesbk12.js';
|
|
3
3
|
export { b as AudioSink, c as AudioSource, d as RT_VIDEO_OUT_STREAM, e as VIDEO_OUT_DEFAULT_CODEC, f as VIDEO_OUT_FRAMING, g as VIDEO_OUT_WS_FRAMING, i as VideoOutEndpoint, j as VideoOutLane, k as VideoOutRecord, l as VideoOutRecordHeader, m as VideoOutSink, n as VideoOutWtConnect, o as VideoOutWtSession, p as consumeVideoOut, q as decodeVideoOutRecord, t as encodeVideoOutRecord, u as negotiateSessionVideoOutQuic, v as nodeAudioBackend, w as openSessionVideoOutLane, x as weriftAudioBackend } from './video-out-CWesbk12.js';
|
|
4
4
|
export { U as UrunResponses } from './ResponsesClient-BYx3YLGo.js';
|
|
5
|
+
export { M as ModelList, a as ModelObject, l as listModels } from './models-NYMZrklp.js';
|
|
5
6
|
export { RealtimeClientEvent, RealtimeServerEvent } from 'openai/resources/beta/realtime/realtime';
|
|
6
7
|
export { ResponseStreamEvent } from 'openai/resources/responses/responses';
|
|
7
8
|
|
|
@@ -38,23 +39,6 @@ declare class UrunRealtime {
|
|
|
38
39
|
drain(): Promise<void>;
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
interface ModelObject {
|
|
42
|
-
id: string;
|
|
43
|
-
object: 'model';
|
|
44
|
-
created: number;
|
|
45
|
-
owned_by: string;
|
|
46
|
-
}
|
|
47
|
-
interface ModelList {
|
|
48
|
-
object: 'list';
|
|
49
|
-
data: ModelObject[];
|
|
50
|
-
}
|
|
51
|
-
interface CatalogOptions {
|
|
52
|
-
catalogUrl: string;
|
|
53
|
-
anonKey: string;
|
|
54
|
-
fetchImpl?: typeof fetch;
|
|
55
|
-
}
|
|
56
|
-
declare function listModels(opts: CatalogOptions): Promise<ModelList>;
|
|
57
|
-
|
|
58
42
|
interface OpenAIErrorEvent {
|
|
59
43
|
type: 'error';
|
|
60
44
|
error: {
|
|
@@ -72,4 +56,4 @@ declare const SUPPORTED_RESPONSES_EVENTS: readonly ["response.created", "respons
|
|
|
72
56
|
declare function isSupportedClientEvent(type: string): boolean;
|
|
73
57
|
declare function isSupportedResponsesEvent(type: string): boolean;
|
|
74
58
|
|
|
75
|
-
export { AudioBackend, LaneMessage, type
|
|
59
|
+
export { AudioBackend, LaneMessage, type OpenAIErrorEvent, SUPPORTED_REALTIME_CLIENT_EVENTS, SUPPORTED_RESPONSES_EVENTS, UrunRealtime, UrunSessionLike, isSupportedClientEvent, isSupportedResponsesEvent, toErrorEvent };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{b as S}from"./chunk-
|
|
1
|
+
import{b as S}from"./chunk-K23AZPI4.js";import{a as _}from"./chunk-GBBY3PCZ.js";import{c as p,g as v,h as f,i as R,j as y,k as O,l as h,m as x,n as g,o as k,p as A,q as T}from"./chunk-CWJRDBDC.js";import{c as s,e as d}from"./chunk-OI2OY32M.js";import{c as o}from"./chunk-YSFSRI3D.js";o();o();var u=0;function V(){return u+=1,`rt_${Date.now().toString(36)}_${u.toString(36)}`}var a=class{constructor(e,t={}){this.session=e;this.opts=t;this.transport=new d(e)}session;opts;transport;handlers=new Set;conversation=[];active=null;audio=null;currentRequestId=null;async enableAudio(){if(!this.opts.audioBackend)throw new Error("audioBackend required for audio");this.audio=await p(this.session,this.opts.audioBackend),this.audio.onOutputAudio(e=>this.emit({type:"response.audio.delta",delta:e}))}on(e,t){return this.handlers.add(t),()=>this.handlers.delete(t)}emit(e){for(let t of this.handlers)t(e)}send(e){switch(e.type){case"conversation.item.create":{let t=(e.item?.content??[]).map(n=>n.text??"").join("");this.conversation.push({role:e.item?.role??"user",content:t});return}case"response.create":{let t=V();this.currentRequestId=t;let n=this.transport.sendResponseCreate({type:"response.create",response:e.response},this.conversation,t);this.active=(async()=>{try{for await(let i of n)this.emit(i)}catch(i){this.emit(s(i))}})();return}case"input_audio_buffer.append":this.audio?.appendInputAudio(e.audio);return;case"input_audio_buffer.commit":case"input_audio_buffer.clear":return;case"response.cancel":return;default:return}}async drain(){this.active&&await this.active}};o();var m=["session.update","conversation.item.create","response.create","response.cancel","input_audio_buffer.append","input_audio_buffer.commit","input_audio_buffer.clear"],E=["response.created","response.output_text.delta","response.output_item.added","response.function_call_arguments.delta","response.image_generation_call.partial_image","response.completed","error"];function w(r){return m.includes(r)}function I(r){return E.includes(r)}export{R as RT_VIDEO_OUT_STREAM,m as SUPPORTED_REALTIME_CLIENT_EVENTS,E as SUPPORTED_RESPONSES_EVENTS,a as UrunRealtime,_ as UrunResponses,O as VIDEO_OUT_DEFAULT_CODEC,y as VIDEO_OUT_FRAMING,k as VIDEO_OUT_WS_FRAMING,T as consumeVideoOut,x as decodeVideoOutRecord,h as encodeVideoOutRecord,w as isSupportedClientEvent,I as isSupportedResponsesEvent,S as listModels,g as negotiateSessionVideoOutQuic,f as nodeAudioBackend,A as openSessionVideoOutLane,s as toErrorEvent,v as weriftAudioBackend};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
interface ModelObject {
|
|
2
|
+
id: string;
|
|
3
|
+
object: 'model';
|
|
4
|
+
created: number;
|
|
5
|
+
owned_by: string;
|
|
6
|
+
}
|
|
7
|
+
interface ModelList {
|
|
8
|
+
object: 'list';
|
|
9
|
+
data: ModelObject[];
|
|
10
|
+
}
|
|
11
|
+
interface CatalogRow {
|
|
12
|
+
model_id: string;
|
|
13
|
+
variant: string;
|
|
14
|
+
/** PLACEMENT-level GPU spec, e.g. 'rtx6000:1' (the serving lane this row is). */
|
|
15
|
+
gpu_spec?: string | null;
|
|
16
|
+
/** Serving engine — the CHECK on model_catalog enumerates vllm/sglang/llamacpp/... */
|
|
17
|
+
engine?: string | null;
|
|
18
|
+
/** Catalog modality: chat | code | agent | vl | audio | image | stt | tts | ... */
|
|
19
|
+
task?: string | null;
|
|
20
|
+
/** HF checkpoint (optionally `repo@revision`); the doc builder strips the rev. */
|
|
21
|
+
hf_repo?: string | null;
|
|
22
|
+
/** User-facing description (console Endpoints surface copy). */
|
|
23
|
+
description?: string | null;
|
|
24
|
+
/** Weights size in GB. */
|
|
25
|
+
size_gb?: number | null;
|
|
26
|
+
/** Engine args jsonb — context_length (chat rows) lives here. */
|
|
27
|
+
engine_args?: Record<string, unknown> | null;
|
|
28
|
+
/** Last catalog reseed that touched this row (ISO 8601). */
|
|
29
|
+
updated_at?: string | null;
|
|
30
|
+
}
|
|
31
|
+
interface CatalogOptions {
|
|
32
|
+
catalogUrl: string;
|
|
33
|
+
anonKey: string;
|
|
34
|
+
fetchImpl?: typeof fetch;
|
|
35
|
+
}
|
|
36
|
+
declare function listModels(opts: CatalogOptions): Promise<ModelList>;
|
|
37
|
+
|
|
38
|
+
export { type CatalogRow as C, type ModelList as M, type ModelObject as a, listModels as l };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
interface ModelObject {
|
|
2
|
+
id: string;
|
|
3
|
+
object: 'model';
|
|
4
|
+
created: number;
|
|
5
|
+
owned_by: string;
|
|
6
|
+
}
|
|
7
|
+
interface ModelList {
|
|
8
|
+
object: 'list';
|
|
9
|
+
data: ModelObject[];
|
|
10
|
+
}
|
|
11
|
+
interface CatalogRow {
|
|
12
|
+
model_id: string;
|
|
13
|
+
variant: string;
|
|
14
|
+
|
|
15
|
+
gpu_spec?: string | null;
|
|
16
|
+
|
|
17
|
+
engine?: string | null;
|
|
18
|
+
|
|
19
|
+
task?: string | null;
|
|
20
|
+
|
|
21
|
+
hf_repo?: string | null;
|
|
22
|
+
|
|
23
|
+
description?: string | null;
|
|
24
|
+
|
|
25
|
+
size_gb?: number | null;
|
|
26
|
+
|
|
27
|
+
engine_args?: Record<string, unknown> | null;
|
|
28
|
+
|
|
29
|
+
updated_at?: string | null;
|
|
30
|
+
}
|
|
31
|
+
interface CatalogOptions {
|
|
32
|
+
catalogUrl: string;
|
|
33
|
+
anonKey: string;
|
|
34
|
+
fetchImpl?: typeof fetch;
|
|
35
|
+
}
|
|
36
|
+
declare function listModels(opts: CatalogOptions): Promise<ModelList>;
|
|
37
|
+
|
|
38
|
+
export { type CatalogRow as C, type ModelList as M, type ModelObject as a, listModels as l };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var $e=Object.create;var F=Object.defineProperty;var Me=Object.getOwnPropertyDescriptor;var Le=Object.getOwnPropertyNames;var Oe=Object.getPrototypeOf,Ne=Object.prototype.hasOwnProperty;var De=(t,e)=>{for(var n in e)F(t,n,{get:e[n],enumerable:!0})},se=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Le(e))!Ne.call(t,r)&&r!==n&&F(t,r,{get:()=>e[r],enumerable:!(o=Me(e,r))||o.enumerable});return t};var ie=(t,e,n)=>(n=t!=null?$e(Oe(t)):{},se(e||!t||!t.__esModule?F(n,"default",{value:t,enumerable:!0}):n,t)),Fe=t=>se(F({},"__esModule",{value:!0}),t);var ft={};De(ft,{SessionPool:()=>J,URUN_API:()=>ee,applyServeUsage:()=>Te,coalesceSameRole:()=>Ee,createFileDiagnosticSink:()=>be,createUrunExtension:()=>Pe,default:()=>gt,makeSessionFactory:()=>Re,makeStreamSimple:()=>Ie,notify:()=>V,piDiagnosticLogPath:()=>H,resolveSessionEnv:()=>xe,toResponsesInput:()=>Ae});module.exports=Fe(ft);var je=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.tagName.toUpperCase()==="SCRIPT"?document.currentScript.src:new URL("main.js",document.baseURI).href,w=je();var ve=require("@earendil-works/pi-ai"),$=require("fs"),ke=require("path");function ae(t,e,n,o){let r=t.response??{},l={request_id:n,consumer_id:o,stream:!0,kind:"chat",messages:e};return typeof r.instructions=="string"&&(l.instructions=r.instructions),Array.isArray(r.modalities)&&(l.modalities=r.modalities),typeof r.temperature=="number"&&(l.temperature=r.temperature),typeof r.max_output_tokens=="number"&&(l.max_output_tokens=r.max_output_tokens),r.tools!==void 0&&(l.tools=r.tools),l}function le(t,e,n){let o={request_id:e,consumer_id:n,stream:!!t.stream,kind:"responses",input:t.input};return t.model&&(o.model=t.model),t.tools!==void 0&&(o.tools=t.tools),t.tool_choice!==void 0&&(o.tool_choice=t.tool_choice),typeof t.temperature=="number"&&(o.temperature=t.temperature),typeof t.max_output_tokens=="number"&&(o.max_output_tokens=t.max_output_tokens),typeof t.top_p=="number"&&(o.top_p=t.top_p),Array.isArray(t.stop)&&t.stop.length>0&&(o.stop=t.stop),typeof t.reasoning_effort=="string"&&(o.reasoning_effort=t.reasoning_effort),t.chat_template_kwargs!==void 0&&(o.chat_template_kwargs=t.chat_template_kwargs),o}function ue(t){if(t instanceof Error)return{type:"error",error:{type:"urun_error",code:t.name||null,message:t.message}};if(t&&typeof t=="object"&&t.t==="error"){let e=t,n=e.body??{};return{type:"error",error:{type:"urun_error",code:e.code??null,message:n.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(t)}}}var Ke="llm-resp";async function*Y(t,e){let n=`${Ke}:${e}`,o=`resp_${e}`;yield{type:"response.created",response:{id:o,status:"in_progress"}};let r=new Map;for await(let l of t.stream(n).messages()){let i=l;if(i.t==="delta"){if(typeof i.delta=="string"&&(yield{type:"response.output_text.delta",item_id:o,delta:i.delta}),typeof i.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:o,delta:i.reasoning}),Array.isArray(i.tool_calls))for(let u of i.tool_calls){let f=typeof u.index=="number"?u.index:0,a=r.get(f)??{};u.id&&(a.id=u.id),u.function?.name&&(a.name=u.function.name),r.set(f,a),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${f}`,tool_index:f,call_id:a.id,name:a.name,delta:u.function?.arguments??""}}}else if(i.t==="response"){yield{type:"response.completed",response:{id:o,status:"completed",...i.body}};return}else if(i.t==="error"){yield ue(i);return}}}var qe="llm",j=class{constructor(e){this.session=e;this.sessionTag=e.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(e){e.session_tag=this.sessionTag,this.session.doc(qe).set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,n){let o=le(e,n,this.consumerId);return this.write(o),Y(this.session,n)}sendResponseCreate(e,n,o){let r=ae(e,n,o,this.consumerId);return this.write(r),Y(this.session,o)}};var pe=0;function We(){return pe+=1,`req_${Date.now().toString(36)}_${pe.toString(36)}`}var K=class{transport;constructor(e){this.transport=new j(e)}responses={create:async e=>{let n=We(),o=this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*o})(),{requestId:n})}}};var q=require("fs"),de=ie(require("os"),1),W=require("path"),ge=require("util");function H(t=de.default.homedir()){return(0,W.join)(t,".urun","logs","pi-extension.log")}var ce=!1;function fe(t,e){let n=H();try{(0,q.mkdirSync)((0,W.dirname)(n),{recursive:!0}),(0,q.appendFileSync)(n,`${new Date().toISOString()} [console.${t}] [urun] ${(0,ge.format)(...e)}
|
|
2
2
|
`)}catch(o){if(!ce){ce=!0;try{process.stderr.write(`[urun] pi-extension quiet console: cannot write ${n} (${String(o)}); further urun-side console output will be dropped
|
|
3
|
-
`)}catch{}}}}var
|
|
4
|
-
`)}}function
|
|
3
|
+
`)}catch{}}}}var g=t=>(...e)=>fe(t,e),X={log:g("log"),info:g("info"),warn:g("warn"),error:g("error"),debug:g("debug"),trace:g("trace"),dir:g("dir"),table:g("table"),group:g("group"),groupCollapsed:g("groupCollapsed"),groupEnd:g("groupEnd"),count:g("count"),countReset:g("countReset"),time:g("time"),timeLog:g("timeLog"),timeEnd:g("timeEnd"),assert:(t,...e)=>{t||fe("assert",e.length>0?e:["Assertion failed"])}};function Q(t){try{process.stderr.write(t)}catch{}}var Z="https://api.urun.sh/v1";async function me(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/apps`,o=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!o.ok)throw new Error(`org apps listing failed: GET ${n} \u2192 ${o.status}`);let r=await o.json();if(!Array.isArray(r.apps))throw new Error(`org apps listing returned no "apps" array (GET ${n})`);if(r.truncated===!0)throw new Error(`org apps listing was truncated (GET ${n} returned ${r.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return r.apps}async function he(t){let e=await me(t),n=e.filter(o=>o.function_name===t.fnName&&o.deployment_status==="active").map(o=>o.app_slug);if(n.length===0)throw new Error(`urun pi extension: the org has no active deployed app exposing "${t.fnName}" (GET ${t.apiUrl}/apps returned ${e.length} app(s), none servable) \u2014 deploy one with \`urun serve <model>\` first`);return n.sort()}var ee="urun-serve",Je="serve",Ge=131072,ze=16384,ye=3,Be=42e4,Ye=6e4,we=2,Xe=15e3;function Qe(t){let e=(t.URUN_API_KEY??"").trim();if(!e)throw new Error("urun pi extension: URUN_API_KEY is required \u2014 model discovery lists the org's deployed serve apps via the org API (URUN_JWT alone cannot list apps)");return{apiKey:e,apiUrl:(t.URUN_API_URL??"").trim()||Z,fnName:(t.URUN_FUNCTION??"").trim()||Je}}function xe(t){let e=(t.URUN_BASE_URL??"").trim(),n=(t.URUN_ORG_ID??"").trim();if(!e)throw new Error("urun pi extension: URUN_BASE_URL is required to open a session");if(/\/v1\/*$/.test(new URL(e).pathname))throw new Error(`urun pi extension: URUN_BASE_URL must be the session-gateway base (e.g. https://api.urun.sh), got ${e} \u2014 a trailing /v1 is the org API form (URUN_API_URL); session allocation 404s against it. Drop the /v1.`);if(!n)throw new Error("urun pi extension: URUN_ORG_ID is required to open a session");let o=(t.URUN_JWT??"").trim();if(o)return{baseUrl:e,orgId:n,auth:{lane:"jwt",jwt:o}};let r=(t.URUN_API_KEY??"").trim();if(!r)throw new Error("urun pi extension: URUN_JWT or URUN_API_KEY is required to open a session");let l=(t.URUN_GATEWAY_URL??"").trim()||void 0;return{baseUrl:e,orgId:n,auth:{lane:"api-key",apiKey:r,gatewayUrl:l}}}function be(t){let e=null;return n=>{e===null&&((0,$.mkdirSync)((0,ke.dirname)(t),{recursive:!0}),e=(0,$.openSync)(t,"a"));let o=n.detail?` ${JSON.stringify(n.detail)}`:"";(0,$.writeSync)(e,`${new Date().toISOString()} [${n.level}] [urun] ${n.message}${o}
|
|
4
|
+
`)}}function Re(t){let e=be(H());return async(n,o,r)=>{let{baseUrl:l,orgId:i,auth:u}=xe(n),{App:f,createClientToken:a}=await t(),h=(u.lane==="jwt"?f(o,{baseUrl:l,orgId:i,jwt:u.jwt,diagnosticSink:e}):f(o,{baseUrl:l,orgId:i,diagnosticSink:e,getAccessToken:async()=>(await a(u.apiKey,{baseUrl:u.gatewayUrl,expiresIn:300,allowedFunctions:[`${o}/${r}`]})).token}))[r];if(typeof h!="function")throw new Error(`urun pi extension: app "${o}" has no function "${r}"`);let _=h();if(typeof _.end!="function")throw new Error(`urun pi extension: app "${o}" function "${r}" returned a session without end()`);let I=_.connect;return typeof I=="function"&&await I.call(_),_}}var Ze=Re(async()=>{let{createRequire:t}=await import("module"),e=t(w);if(typeof globalThis.RTCPeerConnection>"u")try{e.resolve("werift")}catch{throw new Error('urun pi extension: token streams require a WebRTC data channel, and this Node runtime has no RTCPeerConnection and no "werift" backend installed (@urun-sh/core\'s optional Node WebRTC backend \u2014 it was probably stripped by a --no-optional install). Install werift (`npm i werift`) and retry.')}return e("@urun-sh/core")});function Ve(t,e){try{return Promise.resolve(t.end()).then(()=>{},n=>e(n))}catch(n){return e(n),Promise.resolve()}}function et(t){if(t?.hasUI===!1)return!0;if(t?.hasUI===!0)return!1;let e=process.argv.slice(2);if(e.includes("-p")||e.includes("--print"))return!0;let n=e.indexOf("--mode");return n!==-1&&(e[n+1]==="json"||e[n+1]==="rpc")?!0:!process.stdin.isTTY||!process.stdout.isTTY}function Se(t,e){return`uRun: releasing the pooled "${t}" session failed \u2014 the backend session may stay allocated until the platform reclaims it: ${e instanceof Error?e.message:String(e)}`}var J=class{constructor(e,n,o,r=(l,i)=>X.warn(Se(l,i))){this.open=e;this.env=n;this.fnName=o;this.onReleaseError=r}open;env;fnName;onReleaseError;pool=new Map;releasing=new Set;acquire(e){let n=this.pool.get(e);if(!n){let o=Promise.resolve(this.open(this.env,e,this.fnName)).then(r=>({session:r,responses:new K(r)}));n=o,this.pool.set(e,o),o.catch(()=>{this.pool.get(e)===o&&this.pool.delete(e)})}return n}evict(e){let n=this.pool.get(e);if(!n)return Promise.resolve();this.pool.delete(e);let o=n.then(r=>Ve(r.session,l=>this.onReleaseError(e,l)),()=>{});return this.releasing.add(o),o.then(()=>this.releasing.delete(o)),o}closeAll(){let e=[...this.pool.keys()].map(n=>this.evict(n));return Promise.all([...e,...this.releasing]).then(()=>{})}};function Ae(t){let e=[];t.systemPrompt&&e.push({role:"system",content:t.systemPrompt});for(let n of t.messages){if(n.role==="toolResult"){e.push({type:"function_call_output",call_id:n.toolCallId,output:nt(n.content)});continue}if(n.role==="assistant"&&Array.isArray(n.content)){let r="",l=()=>{r&&(e.push({role:"assistant",content:r}),r="")};for(let i of n.content)if(i.type==="text")r+=String(i.text??"");else if(i.type==="toolCall")l(),e.push({type:"function_call",call_id:String(i.id??""),name:String(i.name??""),arguments:JSON.stringify(i.arguments??{})});else{if(i.type==="thinking")continue;throw new Error(`urun pi extension: unsupported assistant content part "${String(i.type)}"`)}l();continue}let o=n.role==="assistant"?"assistant":"user";e.push({role:o,content:tt(n.content)})}return Ee(e)}function Ee(t){let e=[];for(let n of t){let o=e[e.length-1];o&&"role"in o&&"role"in n&&o.role===n.role?o.content=[o.content,n.content].filter(r=>r.length>0).join(`
|
|
5
5
|
|
|
6
|
-
`):e.push({...n})}return e}function
|
|
7
|
-
`);return}let n=t?.sendMessage;if(typeof n=="function")try{n.call(t,{customType:"urun-status",content:e,display:!0},{triggerTurn:!1})}catch{}}function Ie(t,e={},n){let o=e.connectDeadlineMs??Be,r=e.stallTimeoutMs??Ye,l=e.phaseHeartbeatMs??Xe;return(i,u,
|
|
8
|
-
`):
|
|
6
|
+
`):e.push({...n})}return e}function tt(t){return typeof t=="string"?t:Array.isArray(t)?t.map(e=>e&&typeof e=="object"&&"text"in e?String(e.text):"").join(""):""}function nt(t){if(typeof t=="string")return t;if(!Array.isArray(t))return"";let e="";for(let n of t){let o=String(n.type??"");if(o!=="text")throw new Error(`urun pi extension: unsupported toolResult content part "${o}" \u2014 the serve lane carries text tool results only`);e+=String(n.text??"")}return e}function ot(t){if(!(!Array.isArray(t)||t.length===0))return t.map(e=>({type:"function",name:e.name,description:e.description,parameters:e.parameters}))}function _e(t,e){if(!e.trim())return{};try{return JSON.parse(e)}catch{throw new Error(`uRun serve error: tool call "${t}" arguments are not valid JSON: ${e}`)}}function rt(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function Te(t,e){let n=e?.usage;if(!n||typeof n!="object")return;let o=typeof n.input_tokens=="number"?n.input_tokens:void 0,r=typeof n.output_tokens=="number"?n.output_tokens:void 0;o===void 0||r===void 0||(t.input=o,t.output=r,t.totalTokens=typeof n.total_tokens=="number"?n.total_tokens:o+r)}function st(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let o of e)if(!(o.type!=="reasoning"||!Array.isArray(o.content)))for(let r of o.content)r.type==="reasoning_text"&&(n+=String(r.text??""));return n}var it=["<tool_call>","<function="];function at(t,e){let n=it.find(o=>e.includes(o));if(n)return new Error(`serving row emitted a prose tool call (literal ${JSON.stringify(n)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${t}" (see catalog parser_defaults)`)}var G=class extends Error{};function lt(t){let e=t instanceof Error?t.message:String(t),n=t?.code;return/\b529\b/.test(e)||/overloaded/i.test(e)||n===529||n==="529"}function ut(t){return new Promise(e=>setTimeout(e,t))}function pt(t,e,n,o){return new Promise((r,l)=>{let i=!1,u=s=>{i||(i=!0,clearTimeout(f),o?.removeEventListener("abort",a),s())},f=setTimeout(()=>u(()=>l(n())),e),a=()=>u(()=>l(new Error("aborted")));if(o?.aborted){a();return}o?.addEventListener("abort",a,{once:!0}),Promise.resolve(t).then(s=>u(()=>r(s)),s=>u(()=>l(s)))})}async function*ct(t,e){let n=t[Symbol.asyncIterator](),o,r=new Promise((l,i)=>{o=()=>i(new Error("aborted")),e.addEventListener("abort",o,{once:!0})});try{for(;;){if(e.aborted)throw new Error("aborted");let l=await Promise.race([n.next(),r]);if(l.done)return;yield l.value}}finally{o&&e.removeEventListener("abort",o),n.return?.(void 0)}}function V(t,e){if(t?.hasUI===!1){Q(`${e}
|
|
7
|
+
`);return}let n=t?.sendMessage;if(typeof n=="function")try{n.call(t,{customType:"urun-status",content:e,display:!0},{triggerTurn:!1})}catch{}}function Ie(t,e={},n){let o=e.connectDeadlineMs??Be,r=e.stallTimeoutMs??Ye,l=e.phaseHeartbeatMs??Xe;return(i,u,f)=>{let a=(0,ve.createAssistantMessageEventStream)(),s={role:"assistant",content:[],api:i.api,provider:i.provider,model:i.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()},h=new AbortController,_=f?.signal,I=!1,N=()=>{I=!0,h.abort()};_&&(_.aborted?N():_.addEventListener("abort",N,{once:!0}));let M=!1,P,te=()=>{P&&clearTimeout(P),P=setTimeout(()=>{M=!0,h.abort()},r)},D=()=>{P&&clearTimeout(P),P=void 0},R=null,S=null,v=null,ne="",L=!1,z=()=>{if(!S)return;let d=S;S=null,a.push({type:"thinking_end",contentIndex:s.content.indexOf(d),content:d.thinking,partial:s})},oe=()=>{if(!R)return;let d=R;R=null,a.push({type:"text_end",contentIndex:s.content.indexOf(d),content:d.text,partial:s})},B=()=>{if(!v)return;let{call:d,args:k,contentIndex:A}=v;v=null,d.arguments=_e(d.name,k),a.push({type:"toolcall_end",contentIndex:A,toolCall:d,partial:s})},Ce=d=>{let k={type:"toolCall",id:String(d.call_id??d.id??`call_${s.content.length}`),name:String(d.name??""),arguments:{}};s.content.push(k);let A=s.content.length-1;a.push({type:"toolcall_start",contentIndex:A,partial:s});let O=typeof d.arguments=="string"?d.arguments:"";O&&a.push({type:"toolcall_delta",contentIndex:A,delta:O,partial:s}),k.arguments=_e(k.name,O),a.push({type:"toolcall_end",contentIndex:A,toolCall:k,partial:s})};return(async()=>{a.push({type:"start",partial:s});let d=!1,k,A=()=>{k&&clearInterval(k),k=void 0},O=Date.now();k=setInterval(()=>{let x=Math.round((Date.now()-O)/1e3);V(n,`uRun: still opening the "${i.id}" session\u2026 (${x}s \u2014 serve apps scale to zero, first turn can cold-start ~6min)`)},l);let Ue=async x=>{let{responses:b}=await pt(t.acquire(i.id),o,()=>new Error(`uRun session for "${i.id}" did not connect within ${Math.round(o/1e3)}s \u2014 likely cold-starting (scaled to zero) or queued behind capacity. Retry shortly.`),h.signal);A();let C,E;for(let p=1;p<=ye;p++)try{let y=await b.responses.create({model:i.id,input:x,stream:!0,tools:ot(u.tools),max_output_tokens:i.maxTokens,...f?.reasoning!=null&&f.reasoning!=="off"&&i.reasoning!==!1?{}:{chat_template_kwargs:{enable_thinking:!1}}});te();for await(let T of ct(y,h.signal)){te();let m=T;if(m.type==="response.output_text.delta"){let c=typeof m.delta=="string"?m.delta:"";c&&(L=!0,z(),B(),R||(R={type:"text",text:""},s.content.push(R),a.push({type:"text_start",contentIndex:s.content.length-1,partial:s})),R.text+=c,ne+=c,a.push({type:"text_delta",contentIndex:s.content.indexOf(R),delta:c,partial:s}))}else if(m.type==="response.reasoning_text.delta"){let c=typeof m.delta=="string"?m.delta:"";c&&(L=!0,S||(S={type:"thinking",thinking:""},s.content.push(S),a.push({type:"thinking_start",contentIndex:s.content.length-1,partial:s})),S.thinking+=c,a.push({type:"thinking_delta",contentIndex:s.content.indexOf(S),delta:c,partial:s}))}else if(m.type==="response.function_call_arguments.delta"&&typeof m.tool_index=="number"){let c=m;if(L=!0,z(),oe(),!v||v.toolIndex!==c.tool_index){B();let re={type:"toolCall",id:c.call_id??`call_${c.tool_index}`,name:c.name??"",arguments:{}};s.content.push(re),v={call:re,args:"",contentIndex:s.content.length-1,toolIndex:c.tool_index},a.push({type:"toolcall_start",contentIndex:v.contentIndex,partial:s})}c.call_id&&(v.call.id=c.call_id),c.name&&(v.call.name=c.name);let U=typeof c.delta=="string"?c.delta:"";U&&(v.args+=U,a.push({type:"toolcall_delta",contentIndex:v.contentIndex,delta:U,partial:s}))}else if(m.type==="response.completed"){C=m.response;break}else if(m.type==="error"){let c=m.error,U=new Error(`uRun serve error: ${c?.message??"unknown error"}`);throw U.code=c?.code??null,U}}if(!C)throw new G(`uRun backhaul for "${i.id}" closed before the turn completed \u2014 the serving session ended mid-turn (its pod was restarted, drained or deleted).`);E=void 0;break}catch(y){if(E=y,h.signal.aborted)throw y;if(p<ye&<(y)&&!L){await ut(p*500);continue}throw y}if(E)throw E;return C};try{let x=Ae(u),b;for(let p=1;p<=we;p++)try{b=await Ue(x);break}catch(y){let T=M||y instanceof G;if(p>=we||!T||L||I)throw y;D(),t.evict(i.id),M=!1,h=new AbortController,V(n,`uRun: the "${i.id}" session ended mid-turn \u2014 re-homing to a fresh assignment and replaying the turn\u2026`)}if(D(),z(),oe(),B(),!s.content.some(p=>p.type==="toolCall"))for(let p of rt(b))Ce(p);if(!s.content.some(p=>p.type==="thinking")){let p=st(b);if(p){let y={type:"thinking",thinking:""};s.content.push(y);let T=s.content.length-1;a.push({type:"thinking_start",contentIndex:T,partial:s}),y.thinking=p,a.push({type:"thinking_delta",contentIndex:T,delta:p,partial:s}),a.push({type:"thinking_end",contentIndex:T,content:p,partial:s})}}let C=s.content.filter(p=>p.type==="toolCall").length;if((u.tools?.length??0)>0&&C===0){let p=at(i.id,ne);if(p)throw p}let E=C>0?"toolUse":b?.status==="incomplete"?"length":"stop";s.stopReason=E,Te(s.usage,b),a.push({type:"done",reason:E,message:s}),a.end(s)}catch(x){if(d=!0,D(),I||_?.aborted&&!M)s.stopReason="aborted",s.errorMessage="aborted by user",a.push({type:"error",reason:"aborted",error:s});else{let b=M?`uRun stream stalled \u2014 no output for ${Math.round(r/1e3)}s; the model may be hung. Retry, or Ctrl-C to abort.`:x instanceof Error?x.message:String(x);s.stopReason="error",s.errorMessage=b,a.push({type:"error",reason:"error",error:s})}a.end(s)}finally{D(),A(),_&&_.removeEventListener("abort",N),d&&t.evict(i.id)}})(),a}}function dt(t){return{id:t,name:`uRun ${t}`,api:ee,reasoning:!0,input:["text"],cost:{input:0,output:0,cacheRead:0,cacheWrite:0},contextWindow:Ge,maxTokens:ze}}function Pe(t={}){return async e=>{let n=t.env??process.env,{apiKey:o,apiUrl:r,fnName:l}=Qe(n),i=await he({apiUrl:r,apiKey:o,fnName:l,fetchImpl:t.fetchImpl}),u=new J(t.openSession??Ze,n,l,(a,s)=>{let h=Se(a,s);et(e)?Q(`${h}
|
|
8
|
+
`):X.warn(h)});e.on("session_shutdown",()=>u.closeAll());let f={name:"uRun",api:ee,baseUrl:"urun://session",apiKey:"$URUN_API_KEY",models:i.map(dt),streamSimple:Ie(u,t.timing??{},e)};e.registerProvider("urun",f)}}var gt=Pe();0&&(module.exports={SessionPool,URUN_API,applyServeUsage,coalesceSameRole,createFileDiagnosticSink,createUrunExtension,makeSessionFactory,makeStreamSimple,notify,piDiagnosticLogPath,resolveSessionEnv,toResponsesInput});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{a as z}from"../chunk-
|
|
1
|
+
import{a as z}from"../chunk-GBBY3PCZ.js";import{a as W,d as Q}from"../chunk-4MSBS7M6.js";import"../chunk-OI2OY32M.js";import{c as C}from"../chunk-YSFSRI3D.js";C();import{createAssistantMessageEventStream as he}from"@earendil-works/pi-ai";import{mkdirSync as we,openSync as _e,writeSync as ke}from"fs";import{dirname as ve}from"path";C();import{appendFileSync as de,mkdirSync as pe}from"fs";import fe from"os";import{dirname as ge,join as me}from"path";import{format as ye}from"util";function J(t=fe.homedir()){return me(t,".urun","logs","pi-extension.log")}var V=!1;function te(t,e){let n=J();try{pe(ge(n),{recursive:!0}),de(n,`${new Date().toISOString()} [console.${t}] [urun] ${ye(...e)}
|
|
2
2
|
`)}catch(o){if(!V){V=!0;try{process.stderr.write(`[urun] pi-extension quiet console: cannot write ${n} (${String(o)}); further urun-side console output will be dropped
|
|
3
3
|
`)}catch{}}}}var f=t=>(...e)=>te(t,e),j={log:f("log"),info:f("info"),warn:f("warn"),error:f("error"),debug:f("debug"),trace:f("trace"),dir:f("dir"),table:f("table"),group:f("group"),groupCollapsed:f("groupCollapsed"),groupEnd:f("groupEnd"),count:f("count"),countReset:f("countReset"),time:f("time"),timeLog:f("timeLog"),timeEnd:f("timeEnd"),assert:(t,...e)=>{t||te("assert",e.length>0?e:["Assertion failed"])}};function q(t){try{process.stderr.write(t)}catch{}}C();async function ne(t){let e=await Q(t),n=e.filter(o=>o.function_name===t.fnName&&o.deployment_status==="active").map(o=>o.app_slug);if(n.length===0)throw new Error(`urun pi extension: the org has no active deployed app exposing "${t.fnName}" (GET ${t.apiUrl}/apps returned ${e.length} app(s), none servable) \u2014 deploy one with \`urun serve <model>\` first`);return n.sort()}var ae="urun-serve",xe="serve",Re=131072,be=16384,oe=3,Te=42e4,Ee=6e4,re=2,Se=15e3;function Ae(t){let e=(t.URUN_API_KEY??"").trim();if(!e)throw new Error("urun pi extension: URUN_API_KEY is required \u2014 model discovery lists the org's deployed serve apps via the org API (URUN_JWT alone cannot list apps)");return{apiKey:e,apiUrl:(t.URUN_API_URL??"").trim()||W,fnName:(t.URUN_FUNCTION??"").trim()||xe}}function Ue(t){let e=(t.URUN_BASE_URL??"").trim(),n=(t.URUN_ORG_ID??"").trim();if(!e)throw new Error("urun pi extension: URUN_BASE_URL is required to open a session");if(/\/v1\/*$/.test(new URL(e).pathname))throw new Error(`urun pi extension: URUN_BASE_URL must be the session-gateway base (e.g. https://api.urun.sh), got ${e} \u2014 a trailing /v1 is the org API form (URUN_API_URL); session allocation 404s against it. Drop the /v1.`);if(!n)throw new Error("urun pi extension: URUN_ORG_ID is required to open a session");let o=(t.URUN_JWT??"").trim();if(o)return{baseUrl:e,orgId:n,auth:{lane:"jwt",jwt:o}};let s=(t.URUN_API_KEY??"").trim();if(!s)throw new Error("urun pi extension: URUN_JWT or URUN_API_KEY is required to open a session");let l=(t.URUN_GATEWAY_URL??"").trim()||void 0;return{baseUrl:e,orgId:n,auth:{lane:"api-key",apiKey:s,gatewayUrl:l}}}function Ie(t){let e=null;return n=>{e===null&&(we(ve(t),{recursive:!0}),e=_e(t,"a"));let o=n.detail?` ${JSON.stringify(n.detail)}`:"";ke(e,`${new Date().toISOString()} [${n.level}] [urun] ${n.message}${o}
|
|
4
4
|
`)}}function Pe(t){let e=Ie(J());return async(n,o,s)=>{let{baseUrl:l,orgId:i,auth:p}=Ue(n),{App:k,createClientToken:a}=await t(),m=(p.lane==="jwt"?k(o,{baseUrl:l,orgId:i,jwt:p.jwt,diagnosticSink:e}):k(o,{baseUrl:l,orgId:i,diagnosticSink:e,getAccessToken:async()=>(await a(p.apiKey,{baseUrl:p.gatewayUrl,expiresIn:300,allowedFunctions:[`${o}/${s}`]})).token}))[s];if(typeof m!="function")throw new Error(`urun pi extension: app "${o}" has no function "${s}"`);let h=m();if(typeof h.end!="function")throw new Error(`urun pi extension: app "${o}" function "${s}" returned a session without end()`);let A=h.connect;return typeof A=="function"&&await A.call(h),h}}var Ce=Pe(async()=>{let{createRequire:t}=await import("module"),e=t(import.meta.url);if(typeof globalThis.RTCPeerConnection>"u")try{e.resolve("werift")}catch{throw new Error('urun pi extension: token streams require a WebRTC data channel, and this Node runtime has no RTCPeerConnection and no "werift" backend installed (@urun-sh/core\'s optional Node WebRTC backend \u2014 it was probably stripped by a --no-optional install). Install werift (`npm i werift`) and retry.')}return e("@urun-sh/core")});function Ne(t,e){try{return Promise.resolve(t.end()).then(()=>{},n=>e(n))}catch(n){return e(n),Promise.resolve()}}function $e(t){if(t?.hasUI===!1)return!0;if(t?.hasUI===!0)return!1;let e=process.argv.slice(2);if(e.includes("-p")||e.includes("--print"))return!0;let n=e.indexOf("--mode");return n!==-1&&(e[n+1]==="json"||e[n+1]==="rpc")?!0:!process.stdin.isTTY||!process.stdout.isTTY}function le(t,e){return`uRun: releasing the pooled "${t}" session failed \u2014 the backend session may stay allocated until the platform reclaims it: ${e instanceof Error?e.message:String(e)}`}var H=class{constructor(e,n,o,s=(l,i)=>j.warn(le(l,i))){this.open=e;this.env=n;this.fnName=o;this.onReleaseError=s}open;env;fnName;onReleaseError;pool=new Map;releasing=new Set;acquire(e){let n=this.pool.get(e);if(!n){let o=Promise.resolve(this.open(this.env,e,this.fnName)).then(s=>({session:s,responses:new z(s)}));n=o,this.pool.set(e,o),o.catch(()=>{this.pool.get(e)===o&&this.pool.delete(e)})}return n}evict(e){let n=this.pool.get(e);if(!n)return Promise.resolve();this.pool.delete(e);let o=n.then(s=>Ne(s.session,l=>this.onReleaseError(e,l)),()=>{});return this.releasing.add(o),o.then(()=>this.releasing.delete(o)),o}closeAll(){let e=[...this.pool.keys()].map(n=>this.evict(n));return Promise.all([...e,...this.releasing]).then(()=>{})}};function Le(t){let e=[];t.systemPrompt&&e.push({role:"system",content:t.systemPrompt});for(let n of t.messages){if(n.role==="toolResult"){e.push({type:"function_call_output",call_id:n.toolCallId,output:De(n.content)});continue}if(n.role==="assistant"&&Array.isArray(n.content)){let s="",l=()=>{s&&(e.push({role:"assistant",content:s}),s="")};for(let i of n.content)if(i.type==="text")s+=String(i.text??"");else if(i.type==="toolCall")l(),e.push({type:"function_call",call_id:String(i.id??""),name:String(i.name??""),arguments:JSON.stringify(i.arguments??{})});else{if(i.type==="thinking")continue;throw new Error(`urun pi extension: unsupported assistant content part "${String(i.type)}"`)}l();continue}let o=n.role==="assistant"?"assistant":"user";e.push({role:o,content:Me(n.content)})}return Oe(e)}function Oe(t){let e=[];for(let n of t){let o=e[e.length-1];o&&"role"in o&&"role"in n&&o.role===n.role?o.content=[o.content,n.content].filter(s=>s.length>0).join(`
|