@jazzmine-ui/sdk 0.1.0 → 0.1.2

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/README.md CHANGED
@@ -5,7 +5,7 @@ Framework-agnostic TypeScript client for Jazzmine agentic backends.
5
5
  ## Install
6
6
 
7
7
  ```bash
8
- npm install @jazzmine/client
8
+ npm install @jazzmine-ui/sdk
9
9
  ```
10
10
 
11
11
  ## Quick Start
@@ -13,7 +13,7 @@ npm install @jazzmine/client
13
13
  ### 1) Basic chat
14
14
 
15
15
  ```ts
16
- import JazzmineClient from "@jazzmine/client";
16
+ import JazzmineClient from "@jazzmine-ui/sdk";
17
17
 
18
18
  const client = new JazzmineClient("https://your-jazzmine-api.example.com", {
19
19
  apiKey: "your-api-key",
@@ -26,7 +26,7 @@ console.log(reply.response);
26
26
  ### 2) Streaming chat
27
27
 
28
28
  ```ts
29
- import JazzmineClient from "@jazzmine/client";
29
+ import JazzmineClient from "@jazzmine-ui/sdk";
30
30
 
31
31
  const client = new JazzmineClient("https://your-jazzmine-api.example.com");
32
32
 
@@ -51,7 +51,7 @@ console.log(final.conversation_id);
51
51
  ### 3) Convenience sendMessage flow
52
52
 
53
53
  ```ts
54
- import JazzmineClient from "@jazzmine/client";
54
+ import JazzmineClient from "@jazzmine-ui/sdk";
55
55
 
56
56
  const client = new JazzmineClient("https://your-jazzmine-api.example.com", {
57
57
  defaultUserId: "alice",
@@ -128,7 +128,7 @@ class JazzmineClientError extends Error {
128
128
 
129
129
  ## Framework Compatibility
130
130
 
131
- `@jazzmine/client` is framework-agnostic and can be used from React, Vue, Svelte, Next.js, and vanilla JavaScript/TypeScript apps.
131
+ `@jazzmine-ui/sdk` is framework-agnostic and can be used from React, Vue, Svelte, Next.js, and vanilla JavaScript/TypeScript apps.
132
132
 
133
133
  ## Node.js Note
134
134
 
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const x={chat:"/chat",stream:"/chat/stream",conversations:"/conversations",health:"/health",info:"/info"};async function I(r){if(r.endpointsResolved||!r.autoDiscoverEndpoints)return r.endpointsResolved;try{return await r.resolveServerEndpoints(r.requestOptions),!0}catch{return!0}}function A(r,t){const e={},n=r==null?void 0:r.endpoints;if(!n||typeof n!="object")return e;const s=m(n.chat,"POST",t),i=m(n.stream,"POST",t),a=m(n.conversations_create,"POST",t),h=m(n.health,"GET",t),d=m(n.info,"GET",t);return s&&(e.chat=s),i?e.stream=i:s&&(e.stream=`${s.replace(/\/+$/,"")}/stream`),a&&(e.conversations=a),h&&(e.health=h),d&&(e.info=d),e}function m(r,t,e){if(typeof r!="string")return null;const n=r.match(/^([A-Z]+)\s+(.+)$/i);if(!n)return null;const s=n[1].toUpperCase(),i=n[2].trim();if(s!==t.toUpperCase())return null;try{const a=new URL(i,e);return`${a.pathname}${a.search}`}catch{return null}}class o extends Error{constructor(t,e){super(t),this.name="JazzmineClientError",this.status=e==null?void 0:e.status,this.code=e==null?void 0:e.code,this.details=e==null?void 0:e.details}}function b(r,t){if(!r&&!t)return;if(!r)return t;if(!t)return r;const e=AbortSignal.any;if(typeof e=="function")return e([r,t]);const n=new AbortController,s=()=>{n.abort()};return r.aborted||t.aborted?(n.abort(),n.signal):(r.addEventListener("abort",s,{once:!0}),t.addEventListener("abort",s,{once:!0}),n.signal)}function M(r){return r instanceof DOMException&&(r.name==="AbortError"||r.name==="TimeoutError")}function U(r){const t=r.trim().replace(/\/+$/,"");let e;try{e=new URL(t)}catch{throw new o("Invalid baseEndpoint. Provide a full URL like https://api.example.com or http://127.0.0.1:8010.")}if(!e.protocol.startsWith("http"))throw new o("baseEndpoint must use http or https.");return`${e.protocol}//${e.host}${e.pathname.replace(/\/+$/,"")}`}function k(r,t){const e=t.startsWith("/")?t:`/${t}`;return`${r}${e}`}function E(r){const t=r==null?void 0:r.message;if(typeof t!="string"||!t.trim())throw new o("chat payload requires a non-empty message string.");if(r.explicit_context&&!Array.isArray(r.explicit_context))throw new o("explicit_context must be an array of strings.");if(Array.isArray(r.explicit_context)&&r.explicit_context.some(n=>typeof n!="string"))throw new o("explicit_context must contain only strings.");if(r.metadata!==void 0&&(r.metadata===null||typeof r.metadata!="object"))throw new o("metadata must be an object when provided.")}function J(r,t,e){if(r===void 0)return t;if(!Number.isFinite(r)||r<=0)throw new o(`${e} must be a positive number.`);return Math.round(r)}function g(r,t,e,n,s){if(r===void 0)return t;if(!Number.isInteger(r)||r<n||r>s)throw new o(`${e} must be an integer between ${n} and ${s}.`);return r}async function N(r,t){const e=r*(t+1);e<=0||await new Promise(n=>setTimeout(n,e))}async function P(r){let t;for(let e=0;e<=r.retries;e+=1)try{return await r.runAttempt(e)}catch(n){if(t=n,r.allowRetry&&e<r.retries&&r.shouldRetry(n)){await N(r.retryBackoffMs,e);continue}throw n}throw t}class w extends Error{constructor(t){const e=`Request timed out after ${t} ms.`;super(e),this.name="RetryableTimeoutError",this.finalError=new o(e)}}class p extends Error{constructor(t){super(t),this.name="RetryableNetworkError",this.finalError=new o(t)}}async function z(r,t){try{return await P({retries:t.retries,allowRetry:t.allowRetry,retryBackoffMs:r.retryBackoffMs,runAttempt:async()=>{const e=new AbortController,n=setTimeout(()=>{e.abort()},t.timeoutMs),s=b(t.signal,e.signal);try{const i=await r.fetchFn(T(r.baseUrl,t.path),{method:t.method,headers:_(r.apiKey,{accept:"application/json",contentType:t.body!==void 0?"application/json":void 0}),body:t.body!==void 0?JSON.stringify(t.body):void 0,signal:s});if(!i.ok)throw await R(i,t.path);const a=await i.text();if(!a.trim())return{};try{return JSON.parse(a)}catch{throw new o(`Expected JSON response from ${t.path}, but received invalid JSON.`)}}catch(i){throw M(i)?new w(t.timeoutMs):i instanceof o?i:i instanceof TypeError?new p(y(i,"Network request failed.")):new o(y(i,"Network request failed."))}finally{clearTimeout(n)}},shouldRetry:e=>{if(e instanceof w||e instanceof p)return!0;if(e instanceof o){const n=e.status??0;return n===429||n>=500}return!1}})}catch(e){throw e instanceof w||e instanceof p?e.finalError:e instanceof o?e:new o(y(e,"Network request failed."))}}async function R(r,t){const e=await r.text();let n;if(e.trim())try{n=JSON.parse(e)}catch{n=e}const s=D(n),i=`HTTP ${r.status} on ${t}${s?`: ${s}`:""}`;return new o(i,{status:r.status,details:n})}function _(r,t){const e={};return t.accept&&(e.Accept=t.accept),t.contentType&&(e["Content-Type"]=t.contentType),r&&(e.Authorization=`Bearer ${r}`),e}function T(r,t){return k(r,t)}function D(r){if(!r)return"";if(typeof r=="string")return r.slice(0,300);if(typeof r=="object"){const t=r,e=t.detail,n=t.error;if(typeof e=="string")return e.slice(0,300);if(typeof n=="string")return n.slice(0,300)}return""}function y(r,t){return r instanceof Error&&r.message?r.message:t}function j(r){const t=r.split(/\r?\n/).map(i=>i.trimEnd()).filter(i=>i.length>0&&!i.startsWith(":"));if(t.length===0)return null;let e="message";const n=[];for(const i of t){if(i.startsWith("event:")){e=i.slice(6).trim()||"message";continue}i.startsWith("data:")&&n.push(i.slice(5).trim())}const s=n.join(`
2
- `);if(!s)return{event:e,data:null};try{return{event:e,data:JSON.parse(s)}}catch{return{event:e,data:s}}}async function F(r){var a,h,d,c;const t=r.handlers??{},e=r.body.getReader(),n=new TextDecoder("utf-8");let s="",i=null;for(;;){const{done:l,value:C}=await e.read();if(l)break;s+=n.decode(C,{stream:!0});const v=s.split(/\n\n/);s=v.pop()??"";for(const $ of v){const u=j($);if(u){if((a=t.onRawEvent)==null||a.call(t,u.event,u.data),u.event==="intermediate"){(h=t.onIntermediate)==null||h.call(t,u.data);continue}if(u.event==="done"){i=r.expectChatResponse(u.data),(d=t.onDone)==null||d.call(t,i);continue}if(u.event==="error"){const f=r.toStreamError(u.data);throw(c=t.onErrorEvent)==null||c.call(t,f),new o(f.detail||f.error||"Stream error event received.",{code:f.error,details:f})}}}}if(!i)throw new o("Stream ended without a final done event containing ChatResponse.");return i}const B=2e4,L=2,W=350;class S{constructor(t,e={}){if(this.endpointsResolved=!1,!t||!t.trim())throw new o("baseEndpoint is required.");if(typeof fetch!="function"&&!e.fetchImpl)throw new o("fetch is not available in this runtime. Provide options.fetchImpl.");this.baseUrl=U(t),this.apiKey=e.apiKey,this.timeoutMs=J(e.timeoutMs,B,"timeoutMs"),this.retries=g(e.retries,L,"retries",0,10),this.retryBackoffMs=g(e.retryBackoffMs,W,"retryBackoffMs",0,6e4),this.autoDiscoverEndpoints=e.autoDiscoverEndpoints!==!1,this.defaultUserId=(e.defaultUserId??"user").trim()||"user",this.fetchFn=e.fetchImpl??fetch,this.endpoints={...x}}async getHealth(t={}){return await this.ensureEndpoints(t),this.requestJson({method:"GET",path:this.endpoints.health,timeoutMs:t.timeoutMs??this.timeoutMs,retries:t.retries??this.retries,signal:t.signal,allowRetry:!0})}async getInfo(t={}){return await this.ensureEndpoints(t),this.requestJson({method:"GET",path:this.endpoints.info,timeoutMs:t.timeoutMs??this.timeoutMs,retries:t.retries??this.retries,signal:t.signal,allowRetry:!0})}async createConversation(t={},e={}){return await this.ensureEndpoints(e),this.requestJson({method:"POST",path:this.endpoints.conversations,body:{conversation_id:t.conversation_id??"",user_id:t.user_id??this.defaultUserId,title:t.title??"New conversation"},timeoutMs:e.timeoutMs??this.timeoutMs,retries:e.retries??0,signal:e.signal,allowRetry:!1})}async deleteConversation(t,e={}){const n=t==null?void 0:t.trim();if(!n)throw new o("conversationId is required.");return await this.ensureEndpoints(e),this.requestJson({method:"DELETE",path:`${this.endpoints.conversations}/${encodeURIComponent(n)}`,timeoutMs:e.timeoutMs??this.timeoutMs,retries:e.retries??this.retries,signal:e.signal,allowRetry:!0})}async chat(t,e={}){return E(t),await this.ensureEndpoints(e),this.requestJson({method:"POST",path:this.endpoints.chat,body:{message:t.message,conversation_id:t.conversation_id??"",user_id:t.user_id??this.defaultUserId,explicit_context:t.explicit_context,metadata:t.metadata??{}},timeoutMs:e.timeoutMs??this.timeoutMs,retries:e.retries??0,signal:e.signal,allowRetry:!1})}async chatStream(t,e={},n={}){E(t),await this.ensureEndpoints(n);const s=n.timeoutMs??this.timeoutMs,i=n.signal,a=new AbortController,h=setTimeout(()=>{a.abort()},s),d=b(i,a.signal);try{const c=await this.fetchFn(T(this.baseUrl,this.endpoints.stream),{method:"POST",headers:_(this.apiKey,{accept:"text/event-stream",contentType:"application/json"}),body:JSON.stringify({message:t.message,conversation_id:t.conversation_id??"",user_id:t.user_id??this.defaultUserId,explicit_context:t.explicit_context,metadata:t.metadata??{}}),signal:d});if(!c.ok)throw await R(c,this.endpoints.stream);if(!c.body)throw new o("Streaming response body is missing.");return await F({body:c.body,handlers:e,expectChatResponse:l=>this.expectChatResponse(l),toStreamError:l=>this.toStreamError(l)})}catch(c){throw M(c)?new o(`Streaming request timed out after ${s} ms.`):c instanceof o?c:new o(this.errorMessage(c,"Streaming request failed."))}finally{clearTimeout(h)}}async sendMessage(t,e={}){var a;const n={message:t,conversation_id:e.conversationId,user_id:e.userId,explicit_context:e.explicitContext,metadata:e.metadata};let s=(a=e.conversationId)==null?void 0:a.trim();!s&&e.autoCreateConversation&&(s=(await this.createConversation({user_id:e.userId??this.defaultUserId,title:e.conversationTitle??"New conversation"},e.requestOptions)).conversation_id,n.conversation_id=s);const i=await this.chat(n,e.requestOptions);return{response:i,conversationId:i.conversation_id||s||""}}async resolveServerEndpoints(t={}){const e=await this.requestJson({method:"GET",path:this.endpoints.info,timeoutMs:t.timeoutMs??this.timeoutMs,retries:t.retries??this.retries,signal:t.signal,allowRetry:!0}),n=A(e,this.baseUrl);return this.endpoints={...this.endpoints,...n},this.endpointsResolved=!0,{...this.endpoints}}async ensureEndpoints(t={}){this.endpointsResolved=await I({endpointsResolved:this.endpointsResolved,autoDiscoverEndpoints:this.autoDiscoverEndpoints,requestOptions:t,resolveServerEndpoints:e=>this.resolveServerEndpoints(e)})}requestJson(t){return z({baseUrl:this.baseUrl,apiKey:this.apiKey,fetchFn:this.fetchFn,retryBackoffMs:this.retryBackoffMs},t)}expectChatResponse(t){if(!t||typeof t!="object")throw new o("Invalid stream done payload: expected ChatResponse object.");const e=t;if(typeof e.response!="string"||typeof e.conversation_id!="string")throw new o("Invalid ChatResponse payload: missing required fields response/conversation_id.");return e}toStreamError(t){if(!t||typeof t!="object")return{error:"stream_error",detail:"Unknown stream error payload."};const e=t;return{error:typeof e.error=="string"?e.error:"stream_error",detail:typeof e.detail=="string"?e.detail:void 0}}errorMessage(t,e){return t instanceof Error&&t.message?t.message:e}}exports.JazzmineClient=S;exports.JazzmineClientError=o;exports.default=S;
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const x={chat:"/chat",stream:"/chat/stream",conversations:"/conversations",health:"/health",info:"/info"};async function I(r){if(r.endpointsResolved||!r.autoDiscoverEndpoints)return r.endpointsResolved;try{return await r.resolveServerEndpoints(r.requestOptions),!0}catch{return!0}}function A(r,t){const e={},n=r==null?void 0:r.endpoints;if(!n||typeof n!="object")return e;const s=m(n.chat,"POST",t),i=m(n.stream,"POST",t),a=m(n.conversations_create,"POST",t),h=m(n.health,"GET",t),d=m(n.info,"GET",t);return s&&(e.chat=s),i?e.stream=i:s&&(e.stream=`${s.replace(/\/+$/,"")}/stream`),a&&(e.conversations=a),h&&(e.health=h),d&&(e.info=d),e}function m(r,t,e){if(typeof r!="string")return null;const n=r.match(/^([A-Z]+)\s+(.+)$/i);if(!n)return null;const s=n[1].toUpperCase(),i=n[2].trim();if(s!==t.toUpperCase())return null;try{const a=new URL(i,e);return`${a.pathname}${a.search}`}catch{return null}}class o extends Error{constructor(t,e){super(t),this.name="JazzmineClientError",this.status=e==null?void 0:e.status,this.code=e==null?void 0:e.code,this.details=e==null?void 0:e.details}}function b(r,t){if(!r&&!t)return;if(!r)return t;if(!t)return r;const e=AbortSignal.any;if(typeof e=="function")return e([r,t]);const n=new AbortController,s=()=>{n.abort()};return r.aborted||t.aborted?(n.abort(),n.signal):(r.addEventListener("abort",s,{once:!0}),t.addEventListener("abort",s,{once:!0}),n.signal)}function M(r){return r instanceof DOMException&&(r.name==="AbortError"||r.name==="TimeoutError")}function U(r){const t=r.trim().replace(/\/+$/,"");let e;try{e=new URL(t)}catch{throw new o("Invalid baseEndpoint. Provide a full URL like https://api.example.com or http://127.0.0.1:8010.")}if(!e.protocol.startsWith("http"))throw new o("baseEndpoint must use http or https.");return`${e.protocol}//${e.host}${e.pathname.replace(/\/+$/,"")}`}function k(r,t){const e=t.startsWith("/")?t:`/${t}`;return`${r}${e}`}function E(r){const t=r==null?void 0:r.message;if(typeof t!="string"||!t.trim())throw new o("chat payload requires a non-empty message string.");if(r.explicit_context&&!Array.isArray(r.explicit_context))throw new o("explicit_context must be an array of strings.");if(Array.isArray(r.explicit_context)&&r.explicit_context.some(n=>typeof n!="string"))throw new o("explicit_context must contain only strings.");if(r.metadata!==void 0&&(r.metadata===null||typeof r.metadata!="object"))throw new o("metadata must be an object when provided.")}function J(r,t,e){if(r===void 0)return t;if(!Number.isFinite(r)||r<=0)throw new o(`${e} must be a positive number.`);return Math.round(r)}function g(r,t,e,n,s){if(r===void 0)return t;if(!Number.isInteger(r)||r<n||r>s)throw new o(`${e} must be an integer between ${n} and ${s}.`);return r}async function N(r,t){const e=r*(t+1);e<=0||await new Promise(n=>setTimeout(n,e))}async function P(r){let t;for(let e=0;e<=r.retries;e+=1)try{return await r.runAttempt(e)}catch(n){if(t=n,r.allowRetry&&e<r.retries&&r.shouldRetry(n)){await N(r.retryBackoffMs,e);continue}throw n}throw t}class w extends Error{constructor(t){const e=`Request timed out after ${t} ms.`;super(e),this.name="RetryableTimeoutError",this.finalError=new o(e)}}class p extends Error{constructor(t){super(t),this.name="RetryableNetworkError",this.finalError=new o(t)}}async function z(r,t){try{return await P({retries:t.retries,allowRetry:t.allowRetry,retryBackoffMs:r.retryBackoffMs,runAttempt:async()=>{const e=new AbortController,n=setTimeout(()=>{e.abort()},t.timeoutMs),s=b(t.signal,e.signal);try{const i=await r.fetchFn(_(r.baseUrl,t.path),{method:t.method,headers:T(r.apiKey,{accept:"application/json",contentType:t.body!==void 0?"application/json":void 0}),body:t.body!==void 0?JSON.stringify(t.body):void 0,signal:s});if(!i.ok)throw await R(i,t.path);const a=await i.text();if(!a.trim())return{};try{return JSON.parse(a)}catch{throw new o(`Expected JSON response from ${t.path}, but received invalid JSON.`)}}catch(i){throw M(i)?new w(t.timeoutMs):i instanceof o?i:i instanceof TypeError?new p(y(i,"Network request failed.")):new o(y(i,"Network request failed."))}finally{clearTimeout(n)}},shouldRetry:e=>{if(e instanceof w||e instanceof p)return!0;if(e instanceof o){const n=e.status??0;return n===429||n>=500}return!1}})}catch(e){throw e instanceof w||e instanceof p?e.finalError:e instanceof o?e:new o(y(e,"Network request failed."))}}async function R(r,t){const e=await r.text();let n;if(e.trim())try{n=JSON.parse(e)}catch{n=e}const s=D(n),i=`HTTP ${r.status} on ${t}${s?`: ${s}`:""}`;return new o(i,{status:r.status,details:n})}function T(r,t){const e={};return t.accept&&(e.Accept=t.accept),t.contentType&&(e["Content-Type"]=t.contentType),r&&(e.Authorization=`Bearer ${r}`),e}function _(r,t){return k(r,t)}function D(r){if(!r)return"";if(typeof r=="string")return r.slice(0,300);if(typeof r=="object"){const t=r,e=t.detail,n=t.error;if(typeof e=="string")return e.slice(0,300);if(typeof n=="string")return n.slice(0,300)}return""}function y(r,t){return r instanceof Error&&r.message?r.message:t}function j(r){const t=r.split(/\r?\n/).map(i=>i.trimEnd()).filter(i=>i.length>0&&!i.startsWith(":"));if(t.length===0)return null;let e="message";const n=[];for(const i of t){if(i.startsWith("event:")){e=i.slice(6).trim()||"message";continue}i.startsWith("data:")&&n.push(i.slice(5).trim())}const s=n.join(`
2
+ `);if(!s)return{event:e,data:null};try{return{event:e,data:JSON.parse(s)}}catch{return{event:e,data:s}}}async function F(r){var a,h,d,c;const t=r.handlers??{},e=r.body.getReader(),n=new TextDecoder("utf-8");let s="",i=null;for(;;){const{done:l,value:C}=await e.read();if(l)break;s+=n.decode(C,{stream:!0});const v=s.split(/\n\n/);s=v.pop()??"";for(const $ of v){const u=j($);if(u){if((a=t.onRawEvent)==null||a.call(t,u.event,u.data),u.event==="intermediate"){(h=t.onIntermediate)==null||h.call(t,u.data);continue}if(u.event==="done"){i=r.expectChatResponse(u.data),(d=t.onDone)==null||d.call(t,i);continue}if(u.event==="error"){const f=r.toStreamError(u.data);throw(c=t.onErrorEvent)==null||c.call(t,f),new o(f.detail||f.error||"Stream error event received.",{code:f.error,details:f})}}}}if(!i)throw new o("Stream ended without a final done event containing ChatResponse.");return i}const B=2e4,L=2,W=350;class S{constructor(t,e={}){if(this.endpointsResolved=!1,!t||!t.trim())throw new o("baseEndpoint is required.");if(typeof fetch!="function"&&!e.fetchImpl)throw new o("fetch is not available in this runtime. Provide options.fetchImpl.");this.baseUrl=U(t),this.apiKey=e.apiKey,this.timeoutMs=J(e.timeoutMs,B,"timeoutMs"),this.retries=g(e.retries,L,"retries",0,10),this.retryBackoffMs=g(e.retryBackoffMs,W,"retryBackoffMs",0,6e4),this.autoDiscoverEndpoints=e.autoDiscoverEndpoints!==!1,this.defaultUserId=(e.defaultUserId??"user").trim()||"user",this.fetchFn=e.fetchImpl??fetch.bind(globalThis),this.endpoints={...x}}async getHealth(t={}){return await this.ensureEndpoints(t),this.requestJson({method:"GET",path:this.endpoints.health,timeoutMs:t.timeoutMs??this.timeoutMs,retries:t.retries??this.retries,signal:t.signal,allowRetry:!0})}async getInfo(t={}){return await this.ensureEndpoints(t),this.requestJson({method:"GET",path:this.endpoints.info,timeoutMs:t.timeoutMs??this.timeoutMs,retries:t.retries??this.retries,signal:t.signal,allowRetry:!0})}async createConversation(t={},e={}){return await this.ensureEndpoints(e),this.requestJson({method:"POST",path:this.endpoints.conversations,body:{conversation_id:t.conversation_id??"",user_id:t.user_id??this.defaultUserId,title:t.title??"New conversation"},timeoutMs:e.timeoutMs??this.timeoutMs,retries:e.retries??0,signal:e.signal,allowRetry:!1})}async deleteConversation(t,e={}){const n=t==null?void 0:t.trim();if(!n)throw new o("conversationId is required.");return await this.ensureEndpoints(e),this.requestJson({method:"DELETE",path:`${this.endpoints.conversations}/${encodeURIComponent(n)}`,timeoutMs:e.timeoutMs??this.timeoutMs,retries:e.retries??this.retries,signal:e.signal,allowRetry:!0})}async chat(t,e={}){return E(t),await this.ensureEndpoints(e),this.requestJson({method:"POST",path:this.endpoints.chat,body:{message:t.message,conversation_id:t.conversation_id??"",user_id:t.user_id??this.defaultUserId,explicit_context:t.explicit_context,metadata:t.metadata??{}},timeoutMs:e.timeoutMs??this.timeoutMs,retries:e.retries??0,signal:e.signal,allowRetry:!1})}async chatStream(t,e={},n={}){E(t),await this.ensureEndpoints(n);const s=n.timeoutMs??this.timeoutMs,i=n.signal,a=new AbortController,h=setTimeout(()=>{a.abort()},s),d=b(i,a.signal);try{const c=await this.fetchFn(_(this.baseUrl,this.endpoints.stream),{method:"POST",headers:T(this.apiKey,{accept:"text/event-stream",contentType:"application/json"}),body:JSON.stringify({message:t.message,conversation_id:t.conversation_id??"",user_id:t.user_id??this.defaultUserId,explicit_context:t.explicit_context,metadata:t.metadata??{}}),signal:d});if(!c.ok)throw await R(c,this.endpoints.stream);if(!c.body)throw new o("Streaming response body is missing.");return await F({body:c.body,handlers:e,expectChatResponse:l=>this.expectChatResponse(l),toStreamError:l=>this.toStreamError(l)})}catch(c){throw M(c)?new o(`Streaming request timed out after ${s} ms.`):c instanceof o?c:new o(this.errorMessage(c,"Streaming request failed."))}finally{clearTimeout(h)}}async sendMessage(t,e={}){var a;const n={message:t,conversation_id:e.conversationId,user_id:e.userId,explicit_context:e.explicitContext,metadata:e.metadata};let s=(a=e.conversationId)==null?void 0:a.trim();!s&&e.autoCreateConversation&&(s=(await this.createConversation({user_id:e.userId??this.defaultUserId,title:e.conversationTitle??"New conversation"},e.requestOptions)).conversation_id,n.conversation_id=s);const i=await this.chat(n,e.requestOptions);return{response:i,conversationId:i.conversation_id||s||""}}async resolveServerEndpoints(t={}){const e=await this.requestJson({method:"GET",path:this.endpoints.info,timeoutMs:t.timeoutMs??this.timeoutMs,retries:t.retries??this.retries,signal:t.signal,allowRetry:!0}),n=A(e,this.baseUrl);return this.endpoints={...this.endpoints,...n},this.endpointsResolved=!0,{...this.endpoints}}async ensureEndpoints(t={}){this.endpointsResolved=await I({endpointsResolved:this.endpointsResolved,autoDiscoverEndpoints:this.autoDiscoverEndpoints,requestOptions:t,resolveServerEndpoints:e=>this.resolveServerEndpoints(e)})}requestJson(t){return z({baseUrl:this.baseUrl,apiKey:this.apiKey,fetchFn:this.fetchFn,retryBackoffMs:this.retryBackoffMs},t)}expectChatResponse(t){if(!t||typeof t!="object")throw new o("Invalid stream done payload: expected ChatResponse object.");const e=t;if(typeof e.response!="string"||typeof e.conversation_id!="string")throw new o("Invalid ChatResponse payload: missing required fields response/conversation_id.");return e}toStreamError(t){if(!t||typeof t!="object")return{error:"stream_error",detail:"Unknown stream error payload."};const e=t;return{error:typeof e.error=="string"?e.error:"stream_error",detail:typeof e.detail=="string"?e.detail:void 0}}errorMessage(t,e){return t instanceof Error&&t.message?t.message:e}}exports.JazzmineClient=S;exports.JazzmineClientError=o;exports.default=S;
package/dist/index.js CHANGED
@@ -299,7 +299,7 @@ class W {
299
299
  "retryBackoffMs",
300
300
  0,
301
301
  6e4
302
- ), this.autoDiscoverEndpoints = e.autoDiscoverEndpoints !== !1, this.defaultUserId = (e.defaultUserId ?? "user").trim() || "user", this.fetchFn = e.fetchImpl ?? fetch, this.endpoints = { ...C };
302
+ ), this.autoDiscoverEndpoints = e.autoDiscoverEndpoints !== !1, this.defaultUserId = (e.defaultUserId ?? "user").trim() || "user", this.fetchFn = e.fetchImpl ?? fetch.bind(globalThis), this.endpoints = { ...C };
303
303
  }
304
304
  async getHealth(t = {}) {
305
305
  return await this.ensureEndpoints(t), this.requestJson({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jazzmine-ui/sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "description": "Framework-agnostic TypeScript client for Jazzmine agentic backends",
6
6
  "main": "./dist/index.cjs",