@kivox/client 0.1.0-beta.17 → 0.1.0-beta.18

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/index.d.ts CHANGED
@@ -338,6 +338,7 @@ type ServerTurnComplete = {
338
338
  */
339
339
  type ServerFlowComplete = {
340
340
  type: "flow_complete"
341
+ stats: UsageStats
341
342
  };
342
343
  /**
343
344
  * Represents a non-fatal execution or protocol error.
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- class Z{http;constructor(K){this.http=K}async list(K={}){let J={limit:(K.limit??10).toString(),page:(K.page??1).toString()};if(K.search)J.search=K.search;let Q=await this.http.get("/agents",J);return{...Q,items:Z.parseAgents(Q.items)}}async listLive(K={}){let J={limit:(K.limit??10).toString(),page:(K.page??1).toString()};if(K.search)J.search=K.search;let Q=await this.http.get("/agents/live",J);return{...Q,items:Z.parseAgents(Q.items)}}async listDraft(K={}){let J={limit:(K.limit??10).toString(),page:(K.page??1).toString()};if(K.search)J.search=K.search;let Q=await this.http.get("/agents/draft",J);return{...Q,items:Z.parseAgents(Q.items)}}async listArchived(K={}){let J={limit:(K.limit??10).toString(),page:(K.page??1).toString()};if(K.search)J.search=K.search;let Q=await this.http.get("/agents/archived",J);return{...Q,items:Z.parseAgents(Q.items)}}async get(K){let J=await this.http.get(`/agents/${K}`);return Z.parseAgent(J)}async create(K){let J=await this.http.post("/agents",K);return Z.parseAgent(J)}async update(K,J){let Q=await this.http.put(`/agents/${K}`,J);return Z.parseAgent(Q)}async markAsLive(K){let J=await this.http.patch(`/agents/${K}/live`);return Z.parseAgent(J)}async markAsDraft(K){let J=await this.http.patch(`/agents/${K}/draft`);return Z.parseAgent(J)}async markAsArchived(K){let J=await this.http.patch(`/agents/${K}/archived`);return Z.parseAgent(J)}async delete(K){let J=await this.http.delete(`/agents/${K}`);return Z.parseAgent(J)}async compile(K,J){return await this.http.post(`/agents/${K}/compile`,{ork_source:J})}static parseAgents(K){return K.map(Z.parseAgent)}static parseAgent(K){return{...K,config:JSON.parse(K.config),variables:JSON.parse(K.variables)}}}class O{_transport;_conversationId;_closed=!1;_onEvent;_pendingAudioMetadata=null;_remainingMs=0;_elapsedMs=0;get conversationId(){return this._conversationId}get remainingMs(){return this._remainingMs}get elapsedMs(){return this._elapsedMs}get closed(){return this._closed}constructor(K,J,Q){this._transport=K,this._conversationId=J,this._onEvent=Q.onEvent,this.#K()}sendText(K){if(this._closed){console.warn("Cannot send text: session is closed");return}this._transport.send({type:"input_text",text:K})}sendAudio(K){if(this._closed){console.warn("Cannot send audio: session is closed");return}this._transport.send({type:"input_audio"}),this._transport.sendBinary(K)}streamAudio(K){if(this._closed){console.warn("Cannot stream audio: session is closed");return}this._transport.send({type:"input_audio_stream"}),this._transport.sendBinary(K)}cancelRequest(){if(this._closed){console.warn("Cannot cancel request: session is closed");return}this._transport.send({type:"cancel"})}end(){if(this._closed){console.warn("Cannot end session: session is closed");return}this._transport.send({type:"end"})}close(){if(this._closed)return;this._closed=!0,this._transport.close()}#K(){this._transport.onMessage((K)=>{let J=K;if(J.type==="audio_delta"){this._pendingAudioMetadata={size:J.size};return}if(J.type==="tick")this._remainingMs=J.remaining_ms,this._elapsedMs=J.elapsed_ms;if(J.type==="session_closed")this._closed=!0;this._onEvent?.(J)}),this._transport.onBinary((K)=>{if(this._pendingAudioMetadata){let J={type:"audio_delta",size:this._pendingAudioMetadata.size,audio:K};this._pendingAudioMetadata=null,this._onEvent?.(J)}}),this._transport.onClose(()=>{if(this._closed)return;this._closed=!0,this._onEvent?.({type:"connection_lost",reason:"socket_closed"})}),this._transport.onError(()=>{if(this._closed)return;this._closed=!0,this._onEvent?.({type:"connection_lost",reason:"socket_error"})})}}class z extends Error{code;constructor(K,J="TRANSPORT_ERROR"){super(K);this.code=J;this.name="ConversationTransportError"}}class X{_ws;_closed=!1;_onMessage=null;_onBinary=null;_onError=null;_onClose=null;constructor(K){this._ws=K}async connect(){if(this._ws.readyState===WebSocket.OPEN)throw new z("Already connected","ALREADY_CONNECTED");if(this._ws.readyState===WebSocket.CLOSING||this._ws.readyState===WebSocket.CLOSED)throw new z("Socket is closing or closed","SOCKET_CLOSED");return new Promise((K,J)=>{let Q=()=>{this._ws.removeEventListener("open",Q),this._ws.removeEventListener("error",Y),this.#K(),K()},Y=()=>{this._ws.removeEventListener("open",Q),this._ws.removeEventListener("error",Y),J(new z("Connection failed","CONNECTION_FAILED"))};this._ws.addEventListener("open",Q),this._ws.addEventListener("error",Y)})}send(K){if(this._ws.readyState!==WebSocket.OPEN)throw new z("Not connected","NOT_CONNECTED");this._ws.send(JSON.stringify(K))}async sendBinary(K){if(this._ws.readyState!==WebSocket.OPEN)throw new z("Not connected","NOT_CONNECTED");let J=await K.arrayBuffer();this._ws.send(J)}onMessage(K){this._onMessage=K}onBinary(K){this._onBinary=K}onError(K){this._onError=K}onClose(K){this._onClose=K}close(){if(this._closed)return;if(this._closed=!0,this._ws.readyState===WebSocket.OPEN||this._ws.readyState===WebSocket.CONNECTING)this._ws.close();this._onClose?.()}#K(){this._ws.addEventListener("message",(K)=>{if(K.data instanceof Blob)this._onBinary?.(K.data);else if(K.data instanceof ArrayBuffer)this._onBinary?.(new Blob([K.data]));else if(typeof K.data==="string")try{let J=JSON.parse(K.data);this._onMessage?.(J)}catch(J){console.warn("Failed to parse JSON message:",K.data,J)}}),this._ws.addEventListener("error",()=>{if(this._closed)return;this._onError?.(new z("WebSocket error")),this.close()}),this._ws.addEventListener("close",()=>{if(this._closed)return;this.close()})}}class x{http;ws;constructor(K,J){this.http=K;this.ws=J}async list(K){let J={limit:(K.limit??20).toString(),page:(K.page??1).toString()};return this.http.get(`/agents/${K.agentId}/conversations`,J)}async get(K){return this.http.get(`/conversations/${K}`)}async connect(K){let J=this.ws.connect("/conversations/websocket"),Q=new X(J);return await Q.connect(),Q.send({type:"handshake",agent_id:K.agentId}),new Promise((Y,$)=>{let B=!1;Q.onMessage((N)=>{switch(N.type){case"handshake_ok":B=!0,K.onConnection?.(N),Y(new O(Q,N.conversation_id,K));break;case"handshake_error":B=!0,K.onConnection?.(N),Q.close(),$(Error(`Handshake rejected: ${N.reason}`));break;default:break}}),Q.onError((N)=>{if(!B)$(N)}),Q.onClose(()=>{if(!B)$(Error("WebSocket connection lost during handshake"))})})}}function M(K){let J=new URL(K),Q=J.protocol==="https:"?"wss:":"ws:";return{rest:`${J.origin}/v1`,ws:`${Q}//${J.host}/v1`}}class F extends Error{status;statusText;body;constructor(K,J,Q,Y){super(K);this.status=J;this.statusText=Q;this.body=Y;this.name="HttpTransportError"}}class G{config;constructor(K){this.config=K}async request(K,J){let Q=`${this.config.baseUrl}${K}`,Y=await fetch(Q,{...J,headers:{"Content-Type":"application/json",...this.config.headers,...J?.headers}});if(!Y.ok){let $=await Y.text();throw new F(`HTTP ${Y.status}: ${Y.statusText}`,Y.status,Y.statusText,$)}return Y.json()}async get(K,J){let Q=new URL(`${this.config.baseUrl}${K}`);if(J){let Y=Object.entries(J);for(let[$,B]of Y)Q.searchParams.set($,B)}return this.request(K+Q.search,{method:"GET"})}async post(K,J){return this.request(K,{method:"POST",body:J?JSON.stringify(J):void 0})}async put(K,J){return this.request(K,{method:"PUT",body:J?JSON.stringify(J):void 0})}async patch(K,J){return this.request(K,{method:"PATCH",body:J?JSON.stringify(J):void 0})}async delete(K){return this.request(K,{method:"DELETE"})}}class _{config;constructor(K){this.config=K}connect(K){let J=`${this.config.baseUrl}${K}`;return new WebSocket(J)}}class D{http;constructor(K){this.http=K}async list(K={}){let J={limit:(K.limit??50).toString(),page:(K.page??1).toString()};if(K.conversationId)J.conversation_id=K.conversationId;return this.http.get("/messages",J)}async get(K){return this.http.get(`/messages/${K}`)}}class H{http;constructor(K){this.http=K}async list(K={}){let J={limit:(K.limit??10).toString(),page:(K.page??1).toString()};return this.http.get("/templates",J)}async get(K){return this.http.get(`/templates/${K}`)}async create(K){return this.http.post("/templates",K)}async update(K,J){return this.http.put(`/templates/${K}`,J)}async delete(K){return this.http.delete(`/templates/${K}`)}}class W{agents;conversations;templates;messages;constructor(K){let J=M(K.baseUrl),Q=new G({baseUrl:J.rest,headers:K.headers}),Y=new _({baseUrl:J.ws,headers:K.headers});this.agents=new Z(Q),this.templates=new H(Q),this.conversations=new x(Q,Y),this.messages=new D(Q)}}var L=/\{\{([a-zA-Z_][a-zA-Z0-9_]*)\}\}/g;function j(K){let J=new Set,Q;while((Q=L.exec(K))!==null){let Y=Q[1];if(!Y)continue;J.add(Y)}return J}function U(K,J){if(typeof K==="string"){let Q=j(K);for(let Y of Q)J.add(Y)}else if(Array.isArray(K))for(let Q of K)U(Q,J);else if(typeof K==="object"&&K!==null){let Q=Object.values(K);for(let Y of Q)U(Y,J)}}function v(K){let J=new Set;return U(K,J),Array.from(J).toSorted()}function d(K,J){let Q=new Map(J.map((Y)=>[Y.key,Y.value]));return K.replace(L,(Y,$)=>{return Q.get($)??Y})}function m(K){return L.test(K)}export{d as interpolateVariables,m as hasVariables,v as extractAllVariables,L as REGEX_VARIABLE,W as KivoxClient,F as HttpTransportError,z as ConversationTransportError,O as ConversationSession};
1
+ class z{http;constructor(_){this.http=_}async list(_={}){let x={limit:(_.limit??10).toString(),page:(_.page??1).toString()};if(_.search)x.search=_.search;let D=await this.http.get("/agents",x);return{...D,items:z.parseAgents(D.items)}}async listLive(_={}){let x={limit:(_.limit??10).toString(),page:(_.page??1).toString()};if(_.search)x.search=_.search;let D=await this.http.get("/agents/live",x);return{...D,items:z.parseAgents(D.items)}}async listDraft(_={}){let x={limit:(_.limit??10).toString(),page:(_.page??1).toString()};if(_.search)x.search=_.search;let D=await this.http.get("/agents/draft",x);return{...D,items:z.parseAgents(D.items)}}async listArchived(_={}){let x={limit:(_.limit??10).toString(),page:(_.page??1).toString()};if(_.search)x.search=_.search;let D=await this.http.get("/agents/archived",x);return{...D,items:z.parseAgents(D.items)}}async get(_){let x=await this.http.get(`/agents/${_}`);return z.parseAgent(x)}async create(_){let x=await this.http.post("/agents",_);return z.parseAgent(x)}async update(_,x){let D=await this.http.put(`/agents/${_}`,x);return z.parseAgent(D)}async markAsLive(_){let x=await this.http.patch(`/agents/${_}/live`);return z.parseAgent(x)}async markAsDraft(_){let x=await this.http.patch(`/agents/${_}/draft`);return z.parseAgent(x)}async markAsArchived(_){let x=await this.http.patch(`/agents/${_}/archived`);return z.parseAgent(x)}async delete(_){let x=await this.http.delete(`/agents/${_}`);return z.parseAgent(x)}async compile(_,x){return await this.http.post(`/agents/${_}/compile`,{ork_source:x})}static parseAgents(_){return _.map(z.parseAgent)}static parseAgent(_){return{..._,config:JSON.parse(_.config),variables:JSON.parse(_.variables)}}}class ${_transport;_conversationId;_closed=!1;_onEvent;_pendingAudioMetadata=null;_remainingMs=0;_elapsedMs=0;get conversationId(){return this._conversationId}get remainingMs(){return this._remainingMs}get elapsedMs(){return this._elapsedMs}get closed(){return this._closed}constructor(_,x,D){this._transport=_,this._conversationId=x,this._onEvent=D.onEvent,this.#_()}sendText(_){if(this._closed){console.warn("Cannot send text: session is closed");return}this._transport.send({type:"input_text",text:_})}sendAudio(_){if(this._closed){console.warn("Cannot send audio: session is closed");return}this._transport.send({type:"input_audio"}),this._transport.sendBinary(_)}streamAudio(_){if(this._closed){console.warn("Cannot stream audio: session is closed");return}this._transport.send({type:"input_audio_stream"}),this._transport.sendBinary(_)}cancelRequest(){if(this._closed){console.warn("Cannot cancel request: session is closed");return}this._transport.send({type:"cancel"})}end(){if(this._closed){console.warn("Cannot end session: session is closed");return}this._transport.send({type:"end"})}close(){if(this._closed)return;this._closed=!0,this._transport.close()}#_(){this._transport.onMessage((_)=>{let x=_;if(x.type==="audio_delta"){this._pendingAudioMetadata={size:x.size};return}if(x.type==="tick")this._remainingMs=x.remaining_ms,this._elapsedMs=x.elapsed_ms;if(x.type==="session_closed")this._closed=!0;this._onEvent?.(x)}),this._transport.onBinary((_)=>{if(this._pendingAudioMetadata){let x={type:"audio_delta",size:this._pendingAudioMetadata.size,audio:_};this._pendingAudioMetadata=null,this._onEvent?.(x)}}),this._transport.onClose(()=>{if(this._closed)return;this._closed=!0,this._onEvent?.({type:"connection_lost",reason:"socket_closed"})}),this._transport.onError(()=>{if(this._closed)return;this._closed=!0,this._onEvent?.({type:"connection_lost",reason:"socket_error"})})}}class Q extends Error{code;constructor(_,x="TRANSPORT_ERROR"){super(_);this.code=x;this.name="ConversationTransportError"}}class N{_ws;_closed=!1;_onMessage=null;_onBinary=null;_onError=null;_onClose=null;constructor(_){this._ws=_}async connect(){if(this._ws.readyState===WebSocket.OPEN)throw new Q("Already connected","ALREADY_CONNECTED");if(this._ws.readyState===WebSocket.CLOSING||this._ws.readyState===WebSocket.CLOSED)throw new Q("Socket is closing or closed","SOCKET_CLOSED");return new Promise((_,x)=>{let D=()=>{this._ws.removeEventListener("open",D),this._ws.removeEventListener("error",K),this.#_(),_()},K=()=>{this._ws.removeEventListener("open",D),this._ws.removeEventListener("error",K),x(new Q("Connection failed","CONNECTION_FAILED"))};this._ws.addEventListener("open",D),this._ws.addEventListener("error",K)})}send(_){if(this._ws.readyState!==WebSocket.OPEN)throw new Q("Not connected","NOT_CONNECTED");this._ws.send(JSON.stringify(_))}async sendBinary(_){if(this._ws.readyState!==WebSocket.OPEN)throw new Q("Not connected","NOT_CONNECTED");let x=await _.arrayBuffer();this._ws.send(x)}onMessage(_){this._onMessage=_}onBinary(_){this._onBinary=_}onError(_){this._onError=_}onClose(_){this._onClose=_}close(){if(this._closed)return;if(this._closed=!0,this._ws.readyState===WebSocket.OPEN||this._ws.readyState===WebSocket.CONNECTING)this._ws.close();this._onClose?.()}#_(){this._ws.addEventListener("message",(_)=>{if(_.data instanceof Blob)this._onBinary?.(_.data);else if(_.data instanceof ArrayBuffer)this._onBinary?.(new Blob([_.data]));else if(typeof _.data==="string")try{let x=JSON.parse(_.data);this._onMessage?.(x)}catch(x){console.warn("Failed to parse JSON message:",_.data,x)}}),this._ws.addEventListener("error",()=>{if(this._closed)return;this._onError?.(new Q("WebSocket error")),this.close()}),this._ws.addEventListener("close",()=>{if(this._closed)return;this.close()})}}class B{http;ws;constructor(_,x){this.http=_;this.ws=x}async list(_){let x={limit:(_.limit??20).toString(),page:(_.page??1).toString()};return this.http.get(`/agents/${_.agentId}/conversations`,x)}async get(_){return this.http.get(`/conversations/${_}`)}async connect(_){let x=this.ws.connect("/conversations/websocket"),D=new N(x);return await D.connect(),D.send({type:"handshake",agent_id:_.agentId}),new Promise((K,J)=>{let Z=!1;D.onMessage((Y)=>{switch(Y.type){case"handshake_ok":Z=!0,_.onConnection?.(Y),K(new $(D,Y.conversation_id,_));break;case"handshake_error":Z=!0,_.onConnection?.(Y),D.close(),J(Error(`Handshake rejected: ${Y.reason}`));break;default:break}}),D.onError((Y)=>{if(!Z)J(Y)}),D.onClose(()=>{if(!Z)J(Error("WebSocket connection lost during handshake"))})})}}function L(_){let x=new URL(_),D=x.protocol==="https:"?"wss:":"ws:";return{rest:`${x.origin}/v1`,ws:`${D}//${x.host}/v1`}}class M extends Error{status;statusText;body;constructor(_,x,D,K){super(_);this.status=x;this.statusText=D;this.body=K;this.name="HttpTransportError"}}class O{config;constructor(_){this.config=_}async request(_,x){let D=`${this.config.baseUrl}${_}`,K=await fetch(D,{...x,headers:{"Content-Type":"application/json",...this.config.headers,...x?.headers}});if(!K.ok){let J=await K.text();throw new M(`HTTP ${K.status}: ${K.statusText}`,K.status,K.statusText,J)}return K.json()}async get(_,x){let D=new URL(`${this.config.baseUrl}${_}`);if(x){let K=Object.entries(x);for(let[J,Z]of K)D.searchParams.set(J,Z)}return this.request(_+D.search,{method:"GET"})}async post(_,x){return this.request(_,{method:"POST",body:x?JSON.stringify(x):void 0})}async put(_,x){return this.request(_,{method:"PUT",body:x?JSON.stringify(x):void 0})}async patch(_,x){return this.request(_,{method:"PATCH",body:x?JSON.stringify(x):void 0})}async delete(_){return this.request(_,{method:"DELETE"})}}class X{config;constructor(_){this.config=_}connect(_){let x=`${this.config.baseUrl}${_}`;return new WebSocket(x)}}class F{http;constructor(_){this.http=_}async list(_={}){let x={limit:(_.limit??50).toString(),page:(_.page??1).toString()};if(_.conversationId)x.conversation_id=_.conversationId;return this.http.get("/messages",x)}async get(_){return this.http.get(`/messages/${_}`)}}class G{http;constructor(_){this.http=_}async list(_={}){let x={limit:(_.limit??10).toString(),page:(_.page??1).toString()};return this.http.get("/templates",x)}async get(_){return this.http.get(`/templates/${_}`)}async create(_){return this.http.post("/templates",_)}async update(_,x){return this.http.put(`/templates/${_}`,x)}async delete(_){return this.http.delete(`/templates/${_}`)}}class P{agents;conversations;templates;messages;constructor(_){let x=L(_.baseUrl),D=new O({baseUrl:x.rest,headers:_.headers}),K=new X({baseUrl:x.ws,headers:_.headers});this.agents=new z(D),this.templates=new G(D),this.conversations=new B(D,K),this.messages=new F(D)}}var H=/\{\{([a-zA-Z_][a-zA-Z0-9_]*)\}\}/g;function W(_){let x=new Set,D;while((D=H.exec(_))!==null){let K=D[1];if(!K)continue;x.add(K)}return x}function U(_,x){if(typeof _==="string"){let D=W(_);for(let K of D)x.add(K)}else if(Array.isArray(_))for(let D of _)U(D,x);else if(typeof _==="object"&&_!==null){let D=Object.values(_);for(let K of D)U(K,x)}}function h(_){let x=new Set;return U(_,x),Array.from(x).toSorted()}function v(_,x){let D=new Map(x.map((K)=>[K.key,K.value]));return _.replace(H,(K,J)=>{return D.get(J)??K})}function m(_){return H.test(_)}export{v as interpolateVariables,m as hasVariables,h as extractAllVariables,H as REGEX_VARIABLE,P as KivoxClient,M as HttpTransportError,Q as ConversationTransportError,$ as ConversationSession};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kivox/client",
3
- "version": "0.1.0-beta.17",
3
+ "version": "0.1.0-beta.18",
4
4
  "description": "JavaScript/TypeScript client for Kivox",
5
5
  "homepage": "https://github.com/ekisa-team/kivox#readme",
6
6
  "bugs": {
@@ -15,7 +15,6 @@
15
15
  "email": "ju4n97@proton.me",
16
16
  "url": "https://github.com/ju4n97"
17
17
  },
18
- "sideEffects": false,
19
18
  "type": "module",
20
19
  "exports": {
21
20
  ".": {