@urun-sh/openai 0.6.17 → 0.6.19
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-7TOD3ORT.js +62 -0
- package/dist/chunk-DR2ANRFC.js +7 -0
- package/dist/chunk-MLJ66LPI.js +1 -0
- package/dist/chunk-NYSZ3RGB.js +4 -0
- package/dist/hosted/bin.cjs +31 -31
- package/dist/hosted/bin.js +1 -1
- package/dist/hosted/index.cjs +32 -32
- package/dist/hosted/index.d.cts +1 -1
- package/dist/hosted/index.d.ts +1 -1
- package/dist/hosted/index.js +1 -1
- package/dist/pi-extension/index.cjs +7 -7
- package/dist/pi-extension/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +44 -44
- package/dist/proxy/cli.cjs +48 -48
- package/dist/proxy/cli.js +1 -1
- package/dist/proxy/index.cjs +25 -25
- package/dist/proxy/index.d.cts +89 -5
- package/dist/proxy/index.d.ts +18 -4
- package/dist/proxy/index.js +1 -1
- package/dist/{responses-turn-Dd-h46sQ.d.cts → responses-turn-B91w2YKn.d.cts} +125 -25
- package/dist/{responses-turn-B0zlsQGn.d.ts → responses-turn-FcEDaymF.d.ts} +18 -12
- package/package.json +2 -2
- package/dist/chunk-6SN4EJ7Q.js +0 -7
- package/dist/chunk-TMQJY56F.js +0 -4
- package/dist/chunk-U7EJSC2N.js +0 -62
- package/dist/chunk-VRCCLQ5P.js +0 -1
package/dist/hosted/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Server } from 'node:http';
|
|
2
2
|
import * as _prometheus_io_client from '@prometheus-io/client';
|
|
3
3
|
import { Registry } from '@prometheus-io/client';
|
|
4
|
-
import { c as LedgerOutcome, I as InferenceRecord, S as SessionGoneError, P as ProxyClients, M as ModelRouter, i as UsageRowReporter, b as InferenceUsageRecord, e as LedgerWrite, d as LedgerRecorded, L as LedgerFailure } from '../responses-turn-
|
|
4
|
+
import { c as LedgerOutcome, I as InferenceRecord, S as SessionGoneError, P as ProxyClients, M as ModelRouter, i as UsageRowReporter, b as InferenceUsageRecord, e as LedgerWrite, d as LedgerRecorded, L as LedgerFailure } from '../responses-turn-B91w2YKn.cjs';
|
|
5
5
|
import { createClientToken } from '@urun-sh/core';
|
|
6
6
|
import { U as UrunResponses } from '../ResponsesClient-_OZERUjH.cjs';
|
|
7
7
|
import { b as UrunSessionLike } from '../types-CHPtJx6d.cjs';
|
package/dist/hosted/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Server } from 'node:http';
|
|
2
2
|
import * as _prometheus_io_client from '@prometheus-io/client';
|
|
3
3
|
import { Registry } from '@prometheus-io/client';
|
|
4
|
-
import { c as LedgerOutcome, I as InferenceRecord, S as SessionGoneError, P as ProxyClients, M as ModelRouter, i as UsageRowReporter, b as InferenceUsageRecord, e as LedgerWrite, d as LedgerRecorded, L as LedgerFailure } from '../responses-turn-
|
|
4
|
+
import { c as LedgerOutcome, I as InferenceRecord, S as SessionGoneError, P as ProxyClients, M as ModelRouter, i as UsageRowReporter, b as InferenceUsageRecord, e as LedgerWrite, d as LedgerRecorded, L as LedgerFailure } from '../responses-turn-FcEDaymF.js';
|
|
5
5
|
import { createClientToken } from '@urun-sh/core';
|
|
6
6
|
import { U as UrunResponses } from '../ResponsesClient-CSSrOYD8.js';
|
|
7
7
|
import { b as UrunSessionLike } from '../types-CHPtJx6d.js';
|
package/dist/hosted/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{A as x,B as v,C as m,D as y,E as B,F as H,G as P,H as u,I as G,J as U,K as F,L as X,M as Y,N as b,O as h,P as k,Q as K,R as V,S as j,T as W,U as q,a as r,b as o,c as t,d as E,e as n,f as _,g as a,h as T,i as O,j as i,k as s,l as I,m as p,n as A,o as R,p as S,q as N,r as C,s as D,t as L,u as M,v as d,w as f,x as l,y as c,z as g}from"../chunk-
|
|
1
|
+
import{A as x,B as v,C as m,D as y,E as B,F as H,G as P,H as u,I as G,J as U,K as F,L as X,M as Y,N as b,O as h,P as k,Q as K,R as V,S as j,T as W,U as q,a as r,b as o,c as t,d as E,e as n,f as _,g as a,h as T,i as O,j as i,k as s,l as I,m as p,n as A,o as R,p as S,q as N,r as C,s as D,t as L,u as M,v as d,w as f,x as l,y as c,z as g}from"../chunk-DR2ANRFC.js";import"../chunk-MLJ66LPI.js";import"../chunk-K23AZPI4.js";import"../chunk-7TOD3ORT.js";import"../chunk-76BZLCGT.js";import"../chunk-NYSZ3RGB.js";import"../chunk-3JWYIKHM.js";import"../chunk-6M5JK4YC.js";import"../chunk-7V2JKPIG.js";import{c as e}from"../chunk-YSFSRI3D.js";e();export{N as ANONYMOUS_KEY_ID,y as BIND_HOST,R as BUCKET_DOMAIN,O as BoundedLabel,f as ControlPlaneUnavailableError,X as ConversationBackhauls,F as ConversationCapacityError,v as DEFAULT_PORT,I as EDGE_IDENTITY_PORT,A as EDGE_KEY_FINGERPRINT_HEADER,p as EDGE_KEY_ID_HEADER,i as InferenceMetrics,Y as LEDGER_FUNCTION_PATH,b as LEDGER_WRITE_TIMEOUT_MS,V as LedgerClient,h as LedgerDuplicateError,k as LedgerSurfaceError,g as MAX_CACHED_KEYS,G as MAX_CONVERSATIONS,U as MAX_CONVERSATION_HANDLES,n as MAX_MODEL_LABELS,_ as MAX_ORG_LABELS,H as MAX_TENANTS,o as METRICS_PATH,r as METRICS_PORT,E as NO_MODEL_LABEL,c as ORG_BINDING_TTL_MS,t as OTHER_LABEL,x as OrgResolver,d as ProxyAuthError,W as READINESS_TIMEOUT_MS,j as READINESS_TTL_MS,m as SERVE_FUNCTION,S as TIER_DOMAIN,u as TenantRegistry,l as bearerFrom,L as bearerTokenOf,M as createEdgeIdentityServer,q as createHostedProxy,s as createMetricsServer,C as edgeBucketId,D as edgeTierFingerprint,a as laneOf,K as parseLedgerRecorded,B as resolveHostedConfig,T as statusClassOf,P as tenantSubject};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var Oe=Object.create;var F=Object.defineProperty;var Ue=Object.getOwnPropertyDescriptor;var Ne=Object.getOwnPropertyNames;var Le=Object.getPrototypeOf,De=Object.prototype.hasOwnProperty;var Fe=(e,t)=>{for(var n in t)F(e,n,{get:t[n],enumerable:!0})},ie=(e,t,n,
|
|
2
|
-
`)}catch(
|
|
3
|
-
`)}catch{}}}}var
|
|
4
|
-
`)}}function xe(e){let t=Se(H());return async(n,o
|
|
1
|
+
"use strict";var Oe=Object.create;var F=Object.defineProperty;var Ue=Object.getOwnPropertyDescriptor;var Ne=Object.getOwnPropertyNames;var Le=Object.getPrototypeOf,De=Object.prototype.hasOwnProperty;var Fe=(e,t)=>{for(var n in t)F(e,n,{get:t[n],enumerable:!0})},ie=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Ne(t))!De.call(e,o)&&o!==n&&F(e,o,{get:()=>t[o],enumerable:!(r=Ue(t,o))||r.enumerable});return e};var ae=(e,t,n)=>(n=e!=null?Oe(Le(e)):{},ie(t||!e||!e.__esModule?F(n,"default",{value:e,enumerable:!0}):n,e)),je=e=>ie(F({},"__esModule",{value:!0}),e);var ht={};Fe(ht,{SessionPool:()=>G,URUN_API:()=>te,applyServeUsage:()=>Pe,coalesceSameRole:()=>Ae,createFileDiagnosticSink:()=>Se,createUrunExtension:()=>$e,default:()=>mt,makeSessionFactory:()=>xe,makeStreamSimple:()=>Ce,notify:()=>ee,piDiagnosticLogPath:()=>H,resolveSessionEnv:()=>ke,toResponsesInput:()=>Ee});module.exports=je(ht);var Ke=()=>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,g=Ke();var be=require("@earendil-works/pi-ai"),M=require("fs"),Re=require("path");function le(e,t,n,r){let o=e.response??{},l={request_id:n,consumer_id:r,stream:!0,kind:"chat",messages:t};return typeof o.instructions=="string"&&(l.instructions=o.instructions),Array.isArray(o.modalities)&&(l.modalities=o.modalities),typeof o.temperature=="number"&&(l.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(l.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(l.tools=o.tools),l}function ue(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),typeof e.instructions=="string"&&(r.instructions=e.instructions),e.chat_template_kwargs!==void 0&&(r.chat_template_kwargs=e.chat_template_kwargs),e.response_format!==void 0&&(r.response_format=e.response_format),typeof e.voice=="string"&&(r.voice=e.voice),typeof e.language=="string"&&(r.language=e.language),r}function ce(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??{},r=n.error,o=r?.message??n.message??`unreadable serve error body: ${JSON.stringify(t.body??null)}`;return{type:"error",error:{type:r?.type??"urun_error",code:t.code??r?.code??null,message:o}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var Je="llm-resp";async function*V(e,t){let n=`${Je}:${t}`,r=`resp_${t}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;try{for await(let l of e.stream(n).messages()){let i=l;if(i.t==="delta"){if(typeof i.delta=="string"&&(yield{type:"response.output_text.delta",item_id:r,delta:i.delta}),typeof i.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:r,delta:i.reasoning}),Array.isArray(i.tool_calls))for(let u of i.tool_calls){let m=typeof u.index=="number"?u.index:0,a=o.get(m)??{};u.id&&(a.id=u.id),u.function?.name&&(a.name=u.function.name),o.set(m,a),yield{type:"response.function_call_arguments.delta",item_id:`fc_${t}_${m}`,tool_index:m,call_id:a.id,name:a.name,delta:u.function?.arguments??""}}}else if(i.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...i.body}};return}else if(i.t==="error"){yield ce(i);return}}}finally{try{e.doc("llm").set({requests:{[t]:null}})}catch{}}}var K=class e{constructor(t,n=e.DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS){this.session=t;this.docSyncTimeoutMs=n;this.sessionTag=t.sessionId??globalThis.crypto.randomUUID()}session;docSyncTimeoutMs;sessionTag;static DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS=15e3;get consumerId(){return this.session.consumerId}async ensureDocSynced(){let t=this.session.doc("llm");if(t.synced===!0||typeof t.onSynced!="function")return;let n;await new Promise((o,l)=>{let i=t.onSynced(()=>{clearTimeout(n),o()});n=setTimeout(()=>{i(),l(new Error(`[urun] llm doc plane never synced within ${this.docSyncTimeoutMs}ms \u2014 the request envelope write is withheld rather than raced into a possibly-lost pre-sync write (resumed-attach first-write-lost, prod-usw2 2026-09-23)`))},this.docSyncTimeoutMs)})}write(t){t.session_tag=this.sessionTag,this.session.doc("llm").set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,n){return this.writeGated(n,()=>{let r=ue(t,n,this.consumerId);return this.write(r),V(this.session,n)})}sendResponseCreate(t,n,r){return this.writeGated(r,()=>{let o=le(t,n,r,this.consumerId);return this.write(o),V(this.session,r)})}async writeGated(t,n){try{await this.ensureDocSynced()}catch(r){throw new Error(`[urun] responses dial ${t} aborted before its envelope write: ${r instanceof Error?r.message:String(r)}`)}return n()}};var de=0;function qe(){return de+=1,`req_${Date.now().toString(36)}_${de.toString(36)}`}var J=class{transport;constructor(t){this.transport=new K(t)}responses={create:async t=>{let n=qe(),r=await this.transport.sendResponses(t,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};var q=require("fs"),ge=ae(require("os"),1),B=require("path"),fe=require("util");function H(e=ge.default.homedir()){return(0,B.join)(e,".urun","logs","pi-extension.log")}var pe=!1;function me(e,t){let n=H();try{(0,q.mkdirSync)((0,B.dirname)(n),{recursive:!0}),(0,q.appendFileSync)(n,`${new Date().toISOString()} [console.${e}] [urun] ${(0,fe.format)(...t)}
|
|
2
|
+
`)}catch(r){if(!pe){pe=!0;try{process.stderr.write(`[urun] pi-extension quiet console: cannot write ${n} (${String(r)}); further urun-side console output will be dropped
|
|
3
|
+
`)}catch{}}}}var f=e=>(...t)=>me(e,t),X={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:(e,...t)=>{e||me("assert",t.length>0?t:["Assertion failed"])}};function Q(e){try{process.stderr.write(e)}catch{}}var Z="https://api.urun.sh/v1";async function he(e){let t=e.fetchImpl??fetch,n=`${e.apiUrl.replace(/\/+$/,"")}/apps`,r=await t(n,{headers:{Authorization:`Bearer ${e.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new Error(`org apps listing failed: GET ${n} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.apps))throw new Error(`org apps listing returned no "apps" array (GET ${n})`);if(o.truncated===!0)throw new Error(`org apps listing was truncated (GET ${n} returned ${o.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return o.apps}async function ye(e){let t=await he(e),n=t.filter(r=>r.function_name===e.fnName&&r.deployment_status==="active").map(r=>r.app_slug);if(n.length===0)throw new Error(`urun pi extension: the org has no active deployed app exposing "${e.fnName}" (GET ${e.apiUrl}/apps returned ${t.length} app(s), none servable) \u2014 deploy one with \`urun serve <model>\` first`);return n.sort()}var te="urun-serve",ze="serve",We=131072,Ye=16384,we=3,Ve=42e4,Xe=6e4,_e=2,Qe=15e3;function Ze(e){let t=(e.URUN_API_KEY??"").trim();if(!t)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:t,apiUrl:(e.URUN_API_URL??"").trim()||Z,fnName:(e.URUN_FUNCTION??"").trim()||ze}}function ke(e){let t=(e.URUN_BASE_URL??"").trim(),n=(e.URUN_ORG_ID??"").trim();if(!t)throw new Error("urun pi extension: URUN_BASE_URL is required to open a session");if(/\/v1\/*$/.test(new URL(t).pathname))throw new Error(`urun pi extension: URUN_BASE_URL must be the session-gateway base (e.g. https://api.urun.sh), got ${t} \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 r=(e.URUN_JWT??"").trim();if(r)return{baseUrl:t,orgId:n,auth:{lane:"jwt",jwt:r}};let o=(e.URUN_API_KEY??"").trim();if(!o)throw new Error("urun pi extension: URUN_JWT or URUN_API_KEY is required to open a session");let l=(e.URUN_GATEWAY_URL??"").trim()||void 0;return{baseUrl:t,orgId:n,auth:{lane:"api-key",apiKey:o,gatewayUrl:l}}}function Se(e){let t=null;return n=>{t===null&&((0,M.mkdirSync)((0,Re.dirname)(e),{recursive:!0}),t=(0,M.openSync)(e,"a"));let r=n.detail?` ${JSON.stringify(n.detail)}`:"";(0,M.writeSync)(t,`${new Date().toISOString()} [${n.level}] [urun] ${n.message}${r}
|
|
4
|
+
`)}}function xe(e){let t=Se(H());return async(n,r,o)=>{let{baseUrl:l,orgId:i,auth:u}=ke(n),{App:m,createClientToken:a}=await e(),y=(u.lane==="jwt"?m(r,{baseUrl:l,orgId:i,jwt:u.jwt,diagnosticSink:t}):m(r,{baseUrl:l,orgId:i,diagnosticSink:t,getAccessToken:async()=>(await a(u.apiKey,{baseUrl:u.gatewayUrl,expiresIn:300,allowedFunctions:[`${r}/${o}`]})).token}))[o];if(typeof y!="function")throw new Error(`urun pi extension: app "${r}" has no function "${o}"`);let _=y();if(typeof _.end!="function")throw new Error(`urun pi extension: app "${r}" function "${o}" returned a session without end()`);let P=_.connect;return typeof P=="function"&&await P.call(_),_}}var et=xe(async()=>{let{createRequire:e}=await import("module"),t=e(g);if(typeof globalThis.RTCPeerConnection>"u")try{t.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 t("@urun-sh/core")});function tt(e,t){try{return Promise.resolve(e.end()).then(()=>{},n=>t(n))}catch(n){return t(n),Promise.resolve()}}function nt(e){if(e?.hasUI===!1)return!0;if(e?.hasUI===!0)return!1;let t=process.argv.slice(2);if(t.includes("-p")||t.includes("--print"))return!0;let n=t.indexOf("--mode");return n!==-1&&(t[n+1]==="json"||t[n+1]==="rpc")?!0:!process.stdin.isTTY||!process.stdout.isTTY}function Te(e,t){return`uRun: releasing the pooled "${e}" session failed \u2014 the backend session may stay allocated until the platform reclaims it: ${t instanceof Error?t.message:String(t)}`}var G=class{constructor(t,n,r,o=(l,i)=>X.warn(Te(l,i))){this.open=t;this.env=n;this.fnName=r;this.onReleaseError=o}open;env;fnName;onReleaseError;pool=new Map;releasing=new Set;acquire(t){let n=this.pool.get(t);if(!n){let r=Promise.resolve(this.open(this.env,t,this.fnName)).then(o=>({session:o,responses:new J(o)}));n=r,this.pool.set(t,r),r.catch(()=>{this.pool.get(t)===r&&this.pool.delete(t)})}return n}evict(t){let n=this.pool.get(t);if(!n)return Promise.resolve();this.pool.delete(t);let r=n.then(o=>tt(o.session,l=>this.onReleaseError(t,l)),()=>{});return this.releasing.add(r),r.then(()=>this.releasing.delete(r)),r}closeAll(){let t=[...this.pool.keys()].map(n=>this.evict(n));return Promise.all([...t,...this.releasing]).then(()=>{})}};function Ee(e){let t=[];e.systemPrompt&&t.push({role:"system",content:e.systemPrompt});for(let n of e.messages){if(n.role==="toolResult"){t.push({type:"function_call_output",call_id:n.toolCallId,output:ot(n.content)});continue}if(n.role==="assistant"&&Array.isArray(n.content)){let o="",l=()=>{o&&(t.push({role:"assistant",content:o}),o="")};for(let i of n.content)if(i.type==="text")o+=String(i.text??"");else if(i.type==="toolCall")l(),t.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 r=n.role==="assistant"?"assistant":"user";t.push({role:r,content:rt(n.content)})}return Ae(t)}function Ae(e){let t=[];for(let n of e){let r=t[t.length-1];r&&"role"in r&&"role"in n&&r.role===n.role?r.content=[r.content,n.content].filter(o=>o.length>0).join(`
|
|
5
5
|
|
|
6
|
-
`):t.push({...n})}return t}function
|
|
7
|
-
`);return}let n=e?.sendMessage;if(typeof n=="function")try{n.call(e,{customType:"urun-status",content:t,display:!0},{triggerTurn:!1})}catch{}}function Ce(e,t={},n){let
|
|
8
|
-
`):X.warn(y)});t.on("session_shutdown",()=>u.closeAll());let
|
|
6
|
+
`):t.push({...n})}return t}function rt(e){return typeof e=="string"?e:Array.isArray(e)?e.map(t=>t&&typeof t=="object"&&"text"in t?String(t.text):"").join(""):""}function ot(e){if(typeof e=="string")return e;if(!Array.isArray(e))return"";let t="";for(let n of e){let r=String(n.type??"");if(r!=="text")throw new Error(`urun pi extension: unsupported toolResult content part "${r}" \u2014 the serve lane carries text tool results only`);t+=String(n.text??"")}return t}function st(e){if(!(!Array.isArray(e)||e.length===0))return e.map(t=>({type:"function",name:t.name,description:t.description,parameters:t.parameters}))}function ve(e,t){if(!t.trim())return{};try{return JSON.parse(t)}catch{throw new Error(`uRun serve error: tool call "${e}" arguments are not valid JSON: ${t}`)}}function it(e){let t=e?.output;return Array.isArray(t)?t.filter(n=>n.type==="function_call"):[]}function Pe(e,t){let n=t?.usage;if(!n||typeof n!="object")return;let r=typeof n.input_tokens=="number"?n.input_tokens:void 0,o=typeof n.output_tokens=="number"?n.output_tokens:void 0;r===void 0||o===void 0||(e.input=r,e.output=o,e.totalTokens=typeof n.total_tokens=="number"?n.total_tokens:r+o)}function at(e){let t=e?.output;if(!Array.isArray(t))return"";let n="";for(let r of t)if(!(r.type!=="reasoning"||!Array.isArray(r.content)))for(let o of r.content)o.type==="reasoning_text"&&(n+=String(o.text??""));return n}var lt=["<tool_call>","<function="];function ut(e,t){let n=lt.find(r=>t.includes(r));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 "${e}" (see catalog parser_defaults)`)}var z=class extends Error{};function ct(e){let t=e instanceof Error?e.message:String(e),n=e?.code;return/\b529\b/.test(t)||/overloaded/i.test(t)||n===529||n==="529"}function dt(e){return new Promise(t=>setTimeout(t,e))}function pt(e,t,n,r){return new Promise((o,l)=>{let i=!1,u=s=>{i||(i=!0,clearTimeout(m),r?.removeEventListener("abort",a),s())},m=setTimeout(()=>u(()=>l(n())),t),a=()=>u(()=>l(new Error("aborted")));if(r?.aborted){a();return}r?.addEventListener("abort",a,{once:!0}),Promise.resolve(e).then(s=>u(()=>o(s)),s=>u(()=>l(s)))})}async function*gt(e,t){let n=e[Symbol.asyncIterator](),r,o=new Promise((l,i)=>{r=()=>i(new Error("aborted")),t.addEventListener("abort",r,{once:!0})});try{for(;;){if(t.aborted)throw new Error("aborted");let l=await Promise.race([n.next(),o]);if(l.done)return;yield l.value}}finally{r&&t.removeEventListener("abort",r),n.return?.(void 0)}}function ee(e,t){if(e?.hasUI===!1){Q(`${t}
|
|
7
|
+
`);return}let n=e?.sendMessage;if(typeof n=="function")try{n.call(e,{customType:"urun-status",content:t,display:!0},{triggerTurn:!1})}catch{}}function Ce(e,t={},n){let r=t.connectDeadlineMs??Ve,o=t.stallTimeoutMs??Xe,l=t.phaseHeartbeatMs??Qe;return(i,u,m)=>{let a=(0,be.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()},y=new AbortController,_=m?.signal,P=!1,L=()=>{P=!0,y.abort()};_&&(_.aborted?L():_.addEventListener("abort",L,{once:!0}));let O=!1,C,ne=()=>{C&&clearTimeout(C),C=setTimeout(()=>{O=!0,y.abort()},o)},D=()=>{C&&clearTimeout(C),C=void 0},S=null,x=null,v=null,re="",U=!1,W=()=>{if(!x)return;let p=x;x=null,a.push({type:"thinking_end",contentIndex:s.content.indexOf(p),content:p.thinking,partial:s})},oe=()=>{if(!S)return;let p=S;S=null,a.push({type:"text_end",contentIndex:s.content.indexOf(p),content:p.text,partial:s})},Y=()=>{if(!v)return;let{call:p,args:b,contentIndex:T}=v;v=null,p.arguments=ve(p.name,b),a.push({type:"toolcall_end",contentIndex:T,toolCall:p,partial:s})},Ie=p=>{let b={type:"toolCall",id:String(p.call_id??p.id??`call_${s.content.length}`),name:String(p.name??""),arguments:{}};s.content.push(b);let T=s.content.length-1;a.push({type:"toolcall_start",contentIndex:T,partial:s});let N=typeof p.arguments=="string"?p.arguments:"";N&&a.push({type:"toolcall_delta",contentIndex:T,delta:N,partial:s}),b.arguments=ve(b.name,N),a.push({type:"toolcall_end",contentIndex:T,toolCall:b,partial:s})};return(async()=>{a.push({type:"start",partial:s});let p=!1,b,T=()=>{b&&clearInterval(b),b=void 0},N=Date.now();b=setInterval(()=>{let R=Math.round((Date.now()-N)/1e3);ee(n,`uRun: still opening the "${i.id}" session\u2026 (${R}s \u2014 serve apps scale to zero, first turn can cold-start ~6min)`)},l);let Me=async R=>{let{responses:k}=await pt(e.acquire(i.id),r,()=>new Error(`uRun session for "${i.id}" did not connect within ${Math.round(r/1e3)}s \u2014 likely cold-starting (scaled to zero) or queued behind capacity. Retry shortly.`),y.signal);T();let $,E;for(let c=1;c<=we;c++)try{let w=await k.responses.create({model:i.id,input:R,stream:!0,tools:st(u.tools),max_output_tokens:i.maxTokens,...m?.reasoning!=null&&m.reasoning!=="off"&&i.reasoning!==!1?{}:{chat_template_kwargs:{enable_thinking:!1}}});ne();for await(let A of gt(w,y.signal)){ne();let h=A;if(h.type==="response.output_text.delta"){let d=typeof h.delta=="string"?h.delta:"";d&&(U=!0,W(),Y(),S||(S={type:"text",text:""},s.content.push(S),a.push({type:"text_start",contentIndex:s.content.length-1,partial:s})),S.text+=d,re+=d,a.push({type:"text_delta",contentIndex:s.content.indexOf(S),delta:d,partial:s}))}else if(h.type==="response.reasoning_text.delta"){let d=typeof h.delta=="string"?h.delta:"";d&&(U=!0,x||(x={type:"thinking",thinking:""},s.content.push(x),a.push({type:"thinking_start",contentIndex:s.content.length-1,partial:s})),x.thinking+=d,a.push({type:"thinking_delta",contentIndex:s.content.indexOf(x),delta:d,partial:s}))}else if(h.type==="response.function_call_arguments.delta"&&typeof h.tool_index=="number"){let d=h;if(U=!0,W(),oe(),!v||v.toolIndex!==d.tool_index){Y();let se={type:"toolCall",id:d.call_id??`call_${d.tool_index}`,name:d.name??"",arguments:{}};s.content.push(se),v={call:se,args:"",contentIndex:s.content.length-1,toolIndex:d.tool_index},a.push({type:"toolcall_start",contentIndex:v.contentIndex,partial:s})}d.call_id&&(v.call.id=d.call_id),d.name&&(v.call.name=d.name);let I=typeof d.delta=="string"?d.delta:"";I&&(v.args+=I,a.push({type:"toolcall_delta",contentIndex:v.contentIndex,delta:I,partial:s}))}else if(h.type==="response.completed"){$=h.response;break}else if(h.type==="error"){let d=h.error,I=new Error(`uRun serve error: ${d?.message??"unknown error"}`);throw I.code=d?.code??null,I}}if(!$)throw new z(`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(w){if(E=w,y.signal.aborted)throw w;if(c<we&&ct(w)&&!U){await dt(c*500);continue}throw w}if(E)throw E;return $};try{let R=Ee(u),k;for(let c=1;c<=_e;c++)try{k=await Me(R);break}catch(w){let A=O||w instanceof z;if(c>=_e||!A||U||P)throw w;D(),e.evict(i.id),O=!1,y=new AbortController,ee(n,`uRun: the "${i.id}" session ended mid-turn \u2014 re-homing to a fresh assignment and replaying the turn\u2026`)}if(D(),W(),oe(),Y(),!s.content.some(c=>c.type==="toolCall"))for(let c of it(k))Ie(c);if(!s.content.some(c=>c.type==="thinking")){let c=at(k);if(c){let w={type:"thinking",thinking:""};s.content.push(w);let A=s.content.length-1;a.push({type:"thinking_start",contentIndex:A,partial:s}),w.thinking=c,a.push({type:"thinking_delta",contentIndex:A,delta:c,partial:s}),a.push({type:"thinking_end",contentIndex:A,content:c,partial:s})}}let $=s.content.filter(c=>c.type==="toolCall").length;if((u.tools?.length??0)>0&&$===0){let c=ut(i.id,re);if(c)throw c}let E=$>0?"toolUse":k?.status==="incomplete"?"length":"stop";s.stopReason=E,Pe(s.usage,k),a.push({type:"done",reason:E,message:s}),a.end(s)}catch(R){if(p=!0,D(),P||_?.aborted&&!O)s.stopReason="aborted",s.errorMessage="aborted by user",a.push({type:"error",reason:"aborted",error:s});else{let k=O?`uRun stream stalled \u2014 no output for ${Math.round(o/1e3)}s; the model may be hung. Retry, or Ctrl-C to abort.`:R instanceof Error?R.message:String(R);s.stopReason="error",s.errorMessage=k,a.push({type:"error",reason:"error",error:s})}a.end(s)}finally{D(),T(),_&&_.removeEventListener("abort",L),p&&e.evict(i.id)}})(),a}}function ft(e){return{id:e,name:`uRun ${e}`,api:te,reasoning:!0,input:["text"],cost:{input:0,output:0,cacheRead:0,cacheWrite:0},contextWindow:We,maxTokens:Ye}}function $e(e={}){return async t=>{let n=e.env??process.env,{apiKey:r,apiUrl:o,fnName:l}=Ze(n),i=await ye({apiUrl:o,apiKey:r,fnName:l,fetchImpl:e.fetchImpl}),u=new G(e.openSession??et,n,l,(a,s)=>{let y=Te(a,s);nt(t)?Q(`${y}
|
|
8
|
+
`):X.warn(y)});t.on("session_shutdown",()=>u.closeAll());let m={name:"uRun",api:te,baseUrl:"urun://session",apiKey:"$URUN_API_KEY",models:i.map(ft),streamSimple:Ce(u,e.timing??{},t)};t.registerProvider("urun",m)}}var mt=$e();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-76BZLCGT.js";import{
|
|
1
|
+
import{a as z}from"../chunk-76BZLCGT.js";import{e as W,h as Q}from"../chunk-NYSZ3RGB.js";import"../chunk-7V2JKPIG.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(`
|