@schmitech/chatbot-api 0.5.2 → 0.5.3
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/api.cjs +2 -2
- package/dist/api.cjs.map +1 -1
- package/dist/api.d.ts +7 -0
- package/dist/api.mjs +91 -64
- package/dist/api.mjs.map +1 -1
- package/package.json +2 -1
package/dist/api.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";var S=Object.create;var p=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var
|
|
2
|
-
`,
|
|
1
|
+
"use strict";var S=Object.create;var p=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var T=Object.getPrototypeOf,O=Object.prototype.hasOwnProperty;var K=(t,e,r)=>e in t?p(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var D=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of P(e))!O.call(t,n)&&n!==r&&p(t,n,{get:()=>e[n],enumerable:!(s=x(e,n))||s.enumerable});return t};var I=(t,e,r)=>(r=t!=null?S(T(t)):{},D(e||!t||!t.__esModule?p(r,"default",{value:t,enumerable:!0}):r,t));var f=(t,e,r)=>K(t,typeof e!="symbol"?e+"":e,r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});let m=null,v=null;typeof window>"u"&&Promise.all([import("http").catch(()=>null),import("https").catch(()=>null)]).then(([t,e])=>{var r,s;(r=t==null?void 0:t.default)!=null&&r.Agent?m=new t.default.Agent({keepAlive:!0}):t!=null&&t.Agent&&(m=new t.Agent({keepAlive:!0})),(s=e==null?void 0:e.default)!=null&&s.Agent?v=new e.default.Agent({keepAlive:!0}):e!=null&&e.Agent&&(v=new e.Agent({keepAlive:!0}))}).catch(t=>{console.warn("Failed to initialize HTTP agents:",t.message)});class E{constructor(e){f(this,"apiUrl");f(this,"apiKey");f(this,"sessionId");if(!e.apiUrl||typeof e.apiUrl!="string")throw new Error("API URL must be a valid string");if(e.apiKey!==void 0&&e.apiKey!==null&&typeof e.apiKey!="string")throw new Error("API key must be a valid string or null");if(e.sessionId!==void 0&&e.sessionId!==null&&typeof e.sessionId!="string")throw new Error("Session ID must be a valid string or null");this.apiUrl=e.apiUrl,this.apiKey=e.apiKey??null,this.sessionId=e.sessionId??null}setSessionId(e){if(e!==null&&typeof e!="string")throw new Error("Session ID must be a valid string or null");this.sessionId=e}getSessionId(){return this.sessionId}getFetchOptions(e={}){const r={};if(typeof window>"u"){const c=this.apiUrl.startsWith("https:")?v:m;c&&(r.agent=c)}else r.headers={Connection:"keep-alive"};const s={"X-Request-ID":Date.now().toString(36)+Math.random().toString(36).substring(2)};return r.headers&&Object.assign(s,r.headers),e.headers&&Object.assign(s,e.headers),this.apiKey&&(s["X-API-Key"]=this.apiKey),this.sessionId&&(s["X-Session-ID"]=this.sessionId),{...e,...r,headers:s}}createChatRequest(e,r=!0){return{messages:[{role:"user",content:e}],stream:r}}async*streamChat(e,r=!0){var s;try{const n=new AbortController,c=setTimeout(()=>n.abort(),6e4),a=await fetch(`${this.apiUrl}/v1/chat`,{...this.getFetchOptions({method:"POST",headers:{"Content-Type":"application/json",Accept:r?"text/event-stream":"application/json"},body:JSON.stringify(this.createChatRequest(e,r))}),signal:n.signal});if(clearTimeout(c),!a.ok){const l=await a.text();throw new Error(`Network response was not ok: ${a.status} ${l}`)}if(!r){const l=await a.json();l.response&&(yield{text:l.response,done:!0});return}const w=(s=a.body)==null?void 0:s.getReader();if(!w)throw new Error("No reader available");const b=new TextDecoder;let i="",y=!1;try{for(;;){const{done:l,value:k}=await w.read();if(l)break;const C=b.decode(k,{stream:!0});i+=C;let u=0,g;for(;(g=i.indexOf(`
|
|
2
|
+
`,u))!==-1;){const d=i.slice(u,g).trim();if(u=g+1,d&&d.startsWith("data: ")){const h=d.slice(6).trim();if(!h||h==="[DONE]"){yield{text:"",done:!0};return}try{const o=JSON.parse(h);if(o.error)throw new Error(`Server Error: ${o.error.message}`);if(o.response&&(y=!0,yield{text:o.response,done:o.done||!1}),o.done){yield{text:"",done:!0};return}}catch(o){console.warn("Error parsing JSON chunk:",o,"Chunk:",h)}}else d&&(y=!0,yield{text:d,done:!1})}i=i.slice(u),i.length>1e6&&(console.warn("Buffer too large, truncating..."),i=i.slice(-5e5))}y&&(yield{text:"",done:!0})}finally{w.releaseLock()}}catch(n){throw n.name==="AbortError"?new Error("Connection timed out. Please check if the server is running."):n.name==="TypeError"&&n.message.includes("Failed to fetch")?new Error("Could not connect to the server. Please check if the server is running."):n}}async clearConversationHistory(e){const r=e||this.sessionId;if(!r)throw new Error("No session ID provided and no current session available");if(!this.apiKey)throw new Error("API key is required for clearing conversation history");const s={"Content-Type":"application/json","X-Session-ID":r,"X-API-Key":this.apiKey};try{const n=await fetch(`${this.apiUrl}/admin/chat-history/${r}`,{...this.getFetchOptions({method:"DELETE",headers:s})});if(!n.ok){const a=await n.text();throw new Error(`Failed to clear conversation history: ${n.status} ${a}`)}return await n.json()}catch(n){throw n.name==="TypeError"&&n.message.includes("Failed to fetch")?new Error("Could not connect to the server. Please check if the server is running."):n}}}let A=null;const U=(t,e=null,r=null)=>{A=new E({apiUrl:t,apiKey:e,sessionId:r})};async function*j(t,e=!0){if(!A)throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");yield*A.streamChat(t,e)}exports.ApiClient=E;exports.configureApi=U;exports.streamChat=j;
|
|
3
3
|
//# sourceMappingURL=api.cjs.map
|
package/dist/api.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.cjs","sources":["../api.ts"],"sourcesContent":["// For Node.js environments, we can use http.Agent for connection pooling\nlet httpAgent: any = null;\nlet httpsAgent: any = null;\n\n// Initialize agents for connection pooling in Node.js environments\nif (typeof window === 'undefined') {\n // Lazy load to avoid including 'http' in browser bundles\n Promise.all([\n import('http').catch(() => null),\n import('https').catch(() => null)\n ]).then(([http, https]) => {\n if (http?.default?.Agent) {\n httpAgent = new http.default.Agent({ keepAlive: true });\n } else if (http?.Agent) {\n httpAgent = new http.Agent({ keepAlive: true });\n }\n \n if (https?.default?.Agent) {\n httpsAgent = new https.default.Agent({ keepAlive: true });\n } else if (https?.Agent) {\n httpsAgent = new https.Agent({ keepAlive: true });\n }\n }).catch(err => {\n // Silently fail - connection pooling is optional\n console.warn('Failed to initialize HTTP agents:', err.message);\n });\n}\n\n// Define the StreamResponse interface\nexport interface StreamResponse {\n text: string;\n done: boolean;\n}\n\n// The server now returns this directly for non-streaming chat\nexport interface ChatResponse {\n response: string;\n sources?: any[];\n}\n\n// The request body for the /v1/chat endpoint\ninterface ChatRequest {\n messages: Array<{ role: string; content: string; }>;\n stream: boolean;\n}\n\nexport class ApiClient {\n private readonly apiUrl: string;\n private readonly apiKey: string | null;\n private sessionId: string | null; // Session ID can be mutable\n\n constructor(config: { apiUrl: string; apiKey?: string | null; sessionId?: string | null }) {\n if (!config.apiUrl || typeof config.apiUrl !== 'string') {\n throw new Error('API URL must be a valid string');\n }\n if (config.apiKey !== undefined && config.apiKey !== null && typeof config.apiKey !== 'string') {\n throw new Error('API key must be a valid string or null');\n }\n if (config.sessionId !== undefined && config.sessionId !== null && typeof config.sessionId !== 'string') {\n throw new Error('Session ID must be a valid string or null');\n }\n \n this.apiUrl = config.apiUrl;\n this.apiKey = config.apiKey ?? null;\n this.sessionId = config.sessionId ?? null;\n }\n\n public setSessionId(sessionId: string | null): void {\n if (sessionId !== null && typeof sessionId !== 'string') {\n throw new Error('Session ID must be a valid string or null');\n }\n this.sessionId = sessionId;\n }\n\n public getSessionId(): string | null {\n return this.sessionId;\n }\n\n // Helper to get fetch options with connection pooling if available\n private getFetchOptions(options: RequestInit = {}): RequestInit {\n const baseOptions: RequestInit = {};\n \n // Environment-specific options\n if (typeof window === 'undefined') {\n // Node.js: Use connection pooling agent\n const isHttps = this.apiUrl.startsWith('https:');\n const agent = isHttps ? httpsAgent : httpAgent;\n if (agent) {\n (baseOptions as any).agent = agent;\n }\n } else {\n // Browser: Use keep-alive header\n baseOptions.headers = { 'Connection': 'keep-alive' };\n }\n\n // Common headers\n const headers: Record<string, string> = {\n 'X-Request-ID': Date.now().toString(36) + Math.random().toString(36).substring(2),\n };\n\n // Merge base options headers (for browser keep-alive)\n if (baseOptions.headers) {\n Object.assign(headers, baseOptions.headers);\n }\n\n // Merge original request headers\n if (options.headers) {\n Object.assign(headers, options.headers);\n }\n\n if (this.apiKey) {\n headers['X-API-Key'] = this.apiKey;\n }\n\n if (this.sessionId) {\n headers['X-Session-ID'] = this.sessionId;\n }\n\n return {\n ...options,\n ...baseOptions,\n headers,\n };\n }\n\n // Create Chat request\n private createChatRequest(message: string, stream: boolean = true): ChatRequest {\n return {\n messages: [\n { role: \"user\", content: message }\n ],\n stream\n };\n }\n\n public async *streamChat(\n message: string,\n stream: boolean = true\n ): AsyncGenerator<StreamResponse> {\n try {\n // Add timeout to the fetch request\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), 60000); // 60 second timeout\n\n const response = await fetch(`${this.apiUrl}/v1/chat`, {\n ...this.getFetchOptions({\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Accept': stream ? 'text/event-stream' : 'application/json'\n },\n body: JSON.stringify(this.createChatRequest(message, stream)),\n }),\n signal: controller.signal\n });\n\n clearTimeout(timeoutId);\n\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(`Network response was not ok: ${response.status} ${errorText}`);\n }\n\n if (!stream) {\n // Handle non-streaming response\n const data = await response.json() as ChatResponse;\n if (data.response) {\n yield {\n text: data.response,\n done: true\n };\n }\n return;\n }\n \n const reader = response.body?.getReader();\n if (!reader) throw new Error('No reader available');\n\n const decoder = new TextDecoder();\n let buffer = '';\n let hasReceivedContent = false;\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n\n const chunk = decoder.decode(value, { stream: true });\n buffer += chunk;\n \n // Process complete lines immediately as they arrive\n let lineStartIndex = 0;\n let newlineIndex;\n \n while ((newlineIndex = buffer.indexOf('\\n', lineStartIndex)) !== -1) {\n const line = buffer.slice(lineStartIndex, newlineIndex).trim();\n lineStartIndex = newlineIndex + 1;\n \n if (line && line.startsWith('data: ')) {\n const jsonText = line.slice(6).trim();\n \n if (!jsonText || jsonText === '[DONE]') {\n yield { text: '', done: true };\n return;\n }\n\n try {\n const data = JSON.parse(jsonText);\n \n if (data.error) {\n throw new Error(`Server Error: ${data.error.message}`);\n }\n \n if (data.response) {\n hasReceivedContent = true;\n yield { text: data.response, done: data.done || false };\n }\n\n if (data.done) {\n yield { text: '', done: true };\n return;\n }\n \n } catch (parseError) {\n console.warn('Error parsing JSON chunk:', parseError, 'Chunk:', jsonText);\n }\n } else if (line) {\n // Handle raw text chunks that are not in SSE format\n hasReceivedContent = true;\n yield { text: line, done: false };\n }\n }\n \n buffer = buffer.slice(lineStartIndex);\n \n if (buffer.length > 1000000) { // 1MB limit\n console.warn('Buffer too large, truncating...');\n buffer = buffer.slice(-500000); // Keep last 500KB\n }\n }\n \n if (hasReceivedContent) {\n yield { text: '', done: true };\n }\n \n } finally {\n reader.releaseLock();\n }\n \n } catch (error: any) {\n if (error.name === 'AbortError') {\n throw new Error('Connection timed out. Please check if the server is running.');\n } else if (error.name === 'TypeError' && error.message.includes('Failed to fetch')) {\n throw new Error('Could not connect to the server. Please check if the server is running.');\n } else {\n throw error;\n }\n }\n }\n}\n\n// Legacy compatibility functions - these create a default client instance\n// These are kept for backward compatibility but should be deprecated in favor of the class-based approach\n\nlet defaultClient: ApiClient | null = null;\n\n// Configure the API with a custom URL, API key (optional), and session ID (optional)\nexport const configureApi = (apiUrl: string, apiKey: string | null = null, sessionId: string | null = null): void => {\n defaultClient = new ApiClient({ apiUrl, apiKey, sessionId });\n}\n\n// Legacy streamChat function that uses the default client\nexport async function* streamChat(\n message: string,\n stream: boolean = true\n): AsyncGenerator<StreamResponse> {\n if (!defaultClient) {\n throw new Error('API not configured. Please call configureApi() with your server URL before using any API functions.');\n }\n \n yield* defaultClient.streamChat(message, stream);\n}\n"],"names":["httpAgent","httpsAgent","http","https","_a","_b","err","ApiClient","config","__publicField","sessionId","options","baseOptions","agent","headers","message","stream","controller","timeoutId","response","errorText","data","reader","decoder","buffer","hasReceivedContent","done","value","chunk","lineStartIndex","newlineIndex","line","jsonText","parseError","error","defaultClient","configureApi","apiUrl","apiKey","streamChat"],"mappings":"mqBACA,IAAIA,EAAiB,KACjBC,EAAkB,KAGlB,OAAO,OAAW,KAEpB,QAAQ,IAAI,CACV,OAAO,MAAM,EAAE,MAAM,IAAM,IAAI,EAC/B,OAAO,OAAO,EAAE,MAAM,IAAM,IAAI,CAAA,CACjC,EAAE,KAAK,CAAC,CAACC,EAAMC,CAAK,IAAM,UACrBC,EAAAF,GAAA,YAAAA,EAAM,UAAN,MAAAE,EAAe,MACjBJ,EAAY,IAAIE,EAAK,QAAQ,MAAM,CAAE,UAAW,GAAM,EAC7CA,GAAA,MAAAA,EAAM,QACfF,EAAY,IAAIE,EAAK,MAAM,CAAE,UAAW,GAAM,IAG5CG,EAAAF,GAAA,YAAAA,EAAO,UAAP,MAAAE,EAAgB,MAClBJ,EAAa,IAAIE,EAAM,QAAQ,MAAM,CAAE,UAAW,GAAM,EAC/CA,GAAA,MAAAA,EAAO,QAChBF,EAAa,IAAIE,EAAM,MAAM,CAAE,UAAW,GAAM,EAEpD,CAAC,EAAE,MAAMG,GAAO,CAEd,QAAQ,KAAK,oCAAqCA,EAAI,OAAO,CAC/D,CAAC,EAqBI,MAAMC,CAAU,CAKrB,YAAYC,EAA+E,CAJ1EC,EAAA,eACAA,EAAA,eACTA,EAAA,kBAGN,GAAI,CAACD,EAAO,QAAU,OAAOA,EAAO,QAAW,SAC7C,MAAM,IAAI,MAAM,gCAAgC,EAElD,GAAIA,EAAO,SAAW,QAAaA,EAAO,SAAW,MAAQ,OAAOA,EAAO,QAAW,SACpF,MAAM,IAAI,MAAM,wCAAwC,EAE1D,GAAIA,EAAO,YAAc,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EAAO,KAAK,MAAMW,CAAQ,EAEhC,GAAIX,EAAK,MACP,MAAM,IAAI,MAAM,iBAAiBA,EAAK,MAAM,OAAO,EAAE,EAQvD,GALIA,EAAK,WACPI,EAAqB,GACrB,KAAM,CAAE,KAAMJ,EAAK,SAAU,KAAMA,EAAK,MAAQ,EAAA,GAG9CA,EAAK,KAAM,CACX,KAAM,CAAE,KAAM,GAAI,KAAM,EAAA,EACxB,MACJ,CAEF,OAASY,EAAY,CACnB,QAAQ,KAAK,4BAA6BA,EAAY,SAAUD,CAAQ,CAC1E,CACF,MAAWD,IAEPN,EAAqB,GACrB,KAAM,CAAE,KAAMM,EAAM,KAAM,EAAA,EAEhC,CAEAP,EAASA,EAAO,MAAMK,CAAc,EAEhCL,EAAO,OAAS,MAClB,QAAQ,KAAK,iCAAiC,EAC9CA,EAASA,EAAO,MAAM,IAAO,EAEjC,CAEIC,IACF,KAAM,CAAE,KAAM,GAAI,KAAM,EAAA,EAG5B,QAAA,CACEH,EAAO,YAAA,CACT,CAEF,OAASY,EAAY,CACnB,MAAIA,EAAM,OAAS,aACX,IAAI,MAAM,8DAA8D,EACrEA,EAAM,OAAS,aAAeA,EAAM,QAAQ,SAAS,iBAAiB,EACzE,IAAI,MAAM,yEAAyE,EAEnFA,CAEV,CACF,CACF,CAKA,IAAIC,EAAkC,KAG/B,MAAMC,EAAe,CAACC,EAAgBC,EAAwB,KAAM5B,EAA2B,OAAe,CACnHyB,EAAgB,IAAI5B,EAAU,CAAE,OAAA8B,EAAQ,OAAAC,EAAQ,UAAA5B,EAAW,CAC7D,EAGA,eAAuB6B,EACrBxB,EACAC,EAAkB,GACc,CAChC,GAAI,CAACmB,EACH,MAAM,IAAI,MAAM,qGAAqG,EAGvH,MAAOA,EAAc,WAAWpB,EAASC,CAAM,CACjD"}
|
|
1
|
+
{"version":3,"file":"api.cjs","sources":["../api.ts"],"sourcesContent":["// For Node.js environments, we can use http.Agent for connection pooling\nlet httpAgent: any = null;\nlet httpsAgent: any = null;\n\n// Initialize agents for connection pooling in Node.js environments\nif (typeof window === 'undefined') {\n // Lazy load to avoid including 'http' in browser bundles\n Promise.all([\n import('http').catch(() => null),\n import('https').catch(() => null)\n ]).then(([http, https]) => {\n if (http?.default?.Agent) {\n httpAgent = new http.default.Agent({ keepAlive: true });\n } else if (http?.Agent) {\n httpAgent = new http.Agent({ keepAlive: true });\n }\n \n if (https?.default?.Agent) {\n httpsAgent = new https.default.Agent({ keepAlive: true });\n } else if (https?.Agent) {\n httpsAgent = new https.Agent({ keepAlive: true });\n }\n }).catch(err => {\n // Silently fail - connection pooling is optional\n console.warn('Failed to initialize HTTP agents:', err.message);\n });\n}\n\n// Define the StreamResponse interface\nexport interface StreamResponse {\n text: string;\n done: boolean;\n}\n\n// The server now returns this directly for non-streaming chat\nexport interface ChatResponse {\n response: string;\n sources?: any[];\n}\n\n// The request body for the /v1/chat endpoint\ninterface ChatRequest {\n messages: Array<{ role: string; content: string; }>;\n stream: boolean;\n}\n\nexport class ApiClient {\n private readonly apiUrl: string;\n private readonly apiKey: string | null;\n private sessionId: string | null; // Session ID can be mutable\n\n constructor(config: { apiUrl: string; apiKey?: string | null; sessionId?: string | null }) {\n if (!config.apiUrl || typeof config.apiUrl !== 'string') {\n throw new Error('API URL must be a valid string');\n }\n if (config.apiKey !== undefined && config.apiKey !== null && typeof config.apiKey !== 'string') {\n throw new Error('API key must be a valid string or null');\n }\n if (config.sessionId !== undefined && config.sessionId !== null && typeof config.sessionId !== 'string') {\n throw new Error('Session ID must be a valid string or null');\n }\n \n this.apiUrl = config.apiUrl;\n this.apiKey = config.apiKey ?? null;\n this.sessionId = config.sessionId ?? null;\n }\n\n public setSessionId(sessionId: string | null): void {\n if (sessionId !== null && typeof sessionId !== 'string') {\n throw new Error('Session ID must be a valid string or null');\n }\n this.sessionId = sessionId;\n }\n\n public getSessionId(): string | null {\n return this.sessionId;\n }\n\n // Helper to get fetch options with connection pooling if available\n private getFetchOptions(options: RequestInit = {}): RequestInit {\n const baseOptions: RequestInit = {};\n \n // Environment-specific options\n if (typeof window === 'undefined') {\n // Node.js: Use connection pooling agent\n const isHttps = this.apiUrl.startsWith('https:');\n const agent = isHttps ? httpsAgent : httpAgent;\n if (agent) {\n (baseOptions as any).agent = agent;\n }\n } else {\n // Browser: Use keep-alive header\n baseOptions.headers = { 'Connection': 'keep-alive' };\n }\n\n // Common headers\n const headers: Record<string, string> = {\n 'X-Request-ID': Date.now().toString(36) + Math.random().toString(36).substring(2),\n };\n\n // Merge base options headers (for browser keep-alive)\n if (baseOptions.headers) {\n Object.assign(headers, baseOptions.headers);\n }\n\n // Merge original request headers\n if (options.headers) {\n Object.assign(headers, options.headers);\n }\n\n if (this.apiKey) {\n headers['X-API-Key'] = this.apiKey;\n }\n\n if (this.sessionId) {\n headers['X-Session-ID'] = this.sessionId;\n }\n\n return {\n ...options,\n ...baseOptions,\n headers,\n };\n }\n\n // Create Chat request\n private createChatRequest(message: string, stream: boolean = true): ChatRequest {\n return {\n messages: [\n { role: \"user\", content: message }\n ],\n stream\n };\n }\n\n public async *streamChat(\n message: string,\n stream: boolean = true\n ): AsyncGenerator<StreamResponse> {\n try {\n // Add timeout to the fetch request\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), 60000); // 60 second timeout\n\n const response = await fetch(`${this.apiUrl}/v1/chat`, {\n ...this.getFetchOptions({\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Accept': stream ? 'text/event-stream' : 'application/json'\n },\n body: JSON.stringify(this.createChatRequest(message, stream)),\n }),\n signal: controller.signal\n });\n\n clearTimeout(timeoutId);\n\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(`Network response was not ok: ${response.status} ${errorText}`);\n }\n\n if (!stream) {\n // Handle non-streaming response\n const data = await response.json() as ChatResponse;\n if (data.response) {\n yield {\n text: data.response,\n done: true\n };\n }\n return;\n }\n \n const reader = response.body?.getReader();\n if (!reader) throw new Error('No reader available');\n\n const decoder = new TextDecoder();\n let buffer = '';\n let hasReceivedContent = false;\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n\n const chunk = decoder.decode(value, { stream: true });\n buffer += chunk;\n \n // Process complete lines immediately as they arrive\n let lineStartIndex = 0;\n let newlineIndex;\n \n while ((newlineIndex = buffer.indexOf('\\n', lineStartIndex)) !== -1) {\n const line = buffer.slice(lineStartIndex, newlineIndex).trim();\n lineStartIndex = newlineIndex + 1;\n \n if (line && line.startsWith('data: ')) {\n const jsonText = line.slice(6).trim();\n \n if (!jsonText || jsonText === '[DONE]') {\n yield { text: '', done: true };\n return;\n }\n\n try {\n const data = JSON.parse(jsonText);\n \n if (data.error) {\n throw new Error(`Server Error: ${data.error.message}`);\n }\n \n if (data.response) {\n hasReceivedContent = true;\n yield { text: data.response, done: data.done || false };\n }\n\n if (data.done) {\n yield { text: '', done: true };\n return;\n }\n \n } catch (parseError) {\n console.warn('Error parsing JSON chunk:', parseError, 'Chunk:', jsonText);\n }\n } else if (line) {\n // Handle raw text chunks that are not in SSE format\n hasReceivedContent = true;\n yield { text: line, done: false };\n }\n }\n \n buffer = buffer.slice(lineStartIndex);\n \n if (buffer.length > 1000000) { // 1MB limit\n console.warn('Buffer too large, truncating...');\n buffer = buffer.slice(-500000); // Keep last 500KB\n }\n }\n \n if (hasReceivedContent) {\n yield { text: '', done: true };\n }\n \n } finally {\n reader.releaseLock();\n }\n \n } catch (error: any) {\n if (error.name === 'AbortError') {\n throw new Error('Connection timed out. Please check if the server is running.');\n } else if (error.name === 'TypeError' && error.message.includes('Failed to fetch')) {\n throw new Error('Could not connect to the server. Please check if the server is running.');\n } else {\n throw error;\n }\n }\n }\n\n public async clearConversationHistory(sessionId?: string): Promise<{\n status: string;\n message: string;\n session_id: string;\n deleted_count: number;\n timestamp: string;\n }> {\n /**\n * Clear conversation history for a session.\n * \n * @param sessionId - Optional session ID to clear. If not provided, uses current session.\n * @returns Promise resolving to operation result\n * @throws Error if the operation fails\n */\n const targetSessionId = sessionId || this.sessionId;\n \n if (!targetSessionId) {\n throw new Error('No session ID provided and no current session available');\n }\n \n if (!this.apiKey) {\n throw new Error('API key is required for clearing conversation history');\n }\n \n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n 'X-Session-ID': targetSessionId,\n 'X-API-Key': this.apiKey\n };\n \n try {\n const response = await fetch(`${this.apiUrl}/admin/chat-history/${targetSessionId}`, {\n ...this.getFetchOptions({\n method: 'DELETE',\n headers\n })\n });\n \n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(`Failed to clear conversation history: ${response.status} ${errorText}`);\n }\n \n const result = await response.json();\n return result;\n \n } catch (error: any) {\n if (error.name === 'TypeError' && error.message.includes('Failed to fetch')) {\n throw new Error('Could not connect to the server. Please check if the server is running.');\n } else {\n throw error;\n }\n }\n }\n}\n\n// Legacy compatibility functions - these create a default client instance\n// These are kept for backward compatibility but should be deprecated in favor of the class-based approach\n\nlet defaultClient: ApiClient | null = null;\n\n// Configure the API with a custom URL, API key (optional), and session ID (optional)\nexport const configureApi = (apiUrl: string, apiKey: string | null = null, sessionId: string | null = null): void => {\n defaultClient = new ApiClient({ apiUrl, apiKey, sessionId });\n}\n\n// Legacy streamChat function that uses the default client\nexport async function* streamChat(\n message: string,\n stream: boolean = true\n): AsyncGenerator<StreamResponse> {\n if (!defaultClient) {\n throw new Error('API not configured. Please call configureApi() with your server URL before using any API functions.');\n }\n \n yield* defaultClient.streamChat(message, stream);\n}\n\n"],"names":["httpAgent","httpsAgent","http","https","_a","_b","err","ApiClient","config","__publicField","sessionId","options","baseOptions","agent","headers","message","stream","controller","timeoutId","response","errorText","data","reader","decoder","buffer","hasReceivedContent","done","value","chunk","lineStartIndex","newlineIndex","line","jsonText","parseError","error","targetSessionId","defaultClient","configureApi","apiUrl","apiKey","streamChat"],"mappings":"mqBACA,IAAIA,EAAiB,KACjBC,EAAkB,KAGlB,OAAO,OAAW,KAEpB,QAAQ,IAAI,CACV,OAAO,MAAM,EAAE,MAAM,IAAM,IAAI,E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AgB,IAAI7B,EAAU,CAAE,OAAA+B,EAAQ,OAAAC,EAAQ,UAAA7B,EAAW,CAC7D,EAGA,eAAuB8B,EACrBzB,EACAC,EAAkB,GACc,CAChC,GAAI,CAACoB,EACH,MAAM,IAAI,MAAM,qGAAqG,EAGvH,MAAOA,EAAc,WAAWrB,EAASC,CAAM,CACjD"}
|
package/dist/api.d.ts
CHANGED
|
@@ -20,6 +20,13 @@ export declare class ApiClient {
|
|
|
20
20
|
private getFetchOptions;
|
|
21
21
|
private createChatRequest;
|
|
22
22
|
streamChat(message: string, stream?: boolean): AsyncGenerator<StreamResponse>;
|
|
23
|
+
clearConversationHistory(sessionId?: string): Promise<{
|
|
24
|
+
status: string;
|
|
25
|
+
message: string;
|
|
26
|
+
session_id: string;
|
|
27
|
+
deleted_count: number;
|
|
28
|
+
timestamp: string;
|
|
29
|
+
}>;
|
|
23
30
|
}
|
|
24
31
|
export declare const configureApi: (apiUrl: string, apiKey?: string | null, sessionId?: string | null) => void;
|
|
25
32
|
export declare function streamChat(message: string, stream?: boolean): AsyncGenerator<StreamResponse>;
|
package/dist/api.mjs
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
var k = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
let p = null,
|
|
2
|
+
var b = (r, e, t) => e in r ? k(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
|
|
3
|
+
var f = (r, e, t) => b(r, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
let p = null, m = null;
|
|
5
5
|
typeof window > "u" && Promise.all([
|
|
6
6
|
import("http").catch(() => null),
|
|
7
7
|
import("https").catch(() => null)
|
|
8
|
-
]).then(([
|
|
9
|
-
var
|
|
10
|
-
(
|
|
11
|
-
}).catch((
|
|
12
|
-
console.warn("Failed to initialize HTTP agents:",
|
|
8
|
+
]).then(([r, e]) => {
|
|
9
|
+
var t, s;
|
|
10
|
+
(t = r == null ? void 0 : r.default) != null && t.Agent ? p = new r.default.Agent({ keepAlive: !0 }) : r != null && r.Agent && (p = new r.Agent({ keepAlive: !0 })), (s = e == null ? void 0 : e.default) != null && s.Agent ? m = new e.default.Agent({ keepAlive: !0 }) : e != null && e.Agent && (m = new e.Agent({ keepAlive: !0 }));
|
|
11
|
+
}).catch((r) => {
|
|
12
|
+
console.warn("Failed to initialize HTTP agents:", r.message);
|
|
13
13
|
});
|
|
14
14
|
class x {
|
|
15
15
|
// Session ID can be mutable
|
|
16
16
|
constructor(e) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
f(this, "apiUrl");
|
|
18
|
+
f(this, "apiKey");
|
|
19
|
+
f(this, "sessionId");
|
|
20
20
|
if (!e.apiUrl || typeof e.apiUrl != "string")
|
|
21
21
|
throw new Error("API URL must be a valid string");
|
|
22
22
|
if (e.apiKey !== void 0 && e.apiKey !== null && typeof e.apiKey != "string")
|
|
@@ -35,113 +35,140 @@ class x {
|
|
|
35
35
|
}
|
|
36
36
|
// Helper to get fetch options with connection pooling if available
|
|
37
37
|
getFetchOptions(e = {}) {
|
|
38
|
-
const
|
|
38
|
+
const t = {};
|
|
39
39
|
if (typeof window > "u") {
|
|
40
|
-
const
|
|
41
|
-
|
|
40
|
+
const c = this.apiUrl.startsWith("https:") ? m : p;
|
|
41
|
+
c && (t.agent = c);
|
|
42
42
|
} else
|
|
43
|
-
|
|
44
|
-
const
|
|
43
|
+
t.headers = { Connection: "keep-alive" };
|
|
44
|
+
const s = {
|
|
45
45
|
"X-Request-ID": Date.now().toString(36) + Math.random().toString(36).substring(2)
|
|
46
46
|
};
|
|
47
|
-
return
|
|
47
|
+
return t.headers && Object.assign(s, t.headers), e.headers && Object.assign(s, e.headers), this.apiKey && (s["X-API-Key"] = this.apiKey), this.sessionId && (s["X-Session-ID"] = this.sessionId), {
|
|
48
48
|
...e,
|
|
49
|
-
...
|
|
50
|
-
headers:
|
|
49
|
+
...t,
|
|
50
|
+
headers: s
|
|
51
51
|
};
|
|
52
52
|
}
|
|
53
53
|
// Create Chat request
|
|
54
|
-
createChatRequest(e,
|
|
54
|
+
createChatRequest(e, t = !0) {
|
|
55
55
|
return {
|
|
56
56
|
messages: [
|
|
57
57
|
{ role: "user", content: e }
|
|
58
58
|
],
|
|
59
|
-
stream:
|
|
59
|
+
stream: t
|
|
60
60
|
};
|
|
61
61
|
}
|
|
62
|
-
async *streamChat(e,
|
|
63
|
-
var
|
|
62
|
+
async *streamChat(e, t = !0) {
|
|
63
|
+
var s;
|
|
64
64
|
try {
|
|
65
|
-
const
|
|
65
|
+
const n = new AbortController(), c = setTimeout(() => n.abort(), 6e4), a = await fetch(`${this.apiUrl}/v1/chat`, {
|
|
66
66
|
...this.getFetchOptions({
|
|
67
67
|
method: "POST",
|
|
68
68
|
headers: {
|
|
69
69
|
"Content-Type": "application/json",
|
|
70
|
-
Accept:
|
|
70
|
+
Accept: t ? "text/event-stream" : "application/json"
|
|
71
71
|
},
|
|
72
|
-
body: JSON.stringify(this.createChatRequest(e,
|
|
72
|
+
body: JSON.stringify(this.createChatRequest(e, t))
|
|
73
73
|
}),
|
|
74
|
-
signal:
|
|
74
|
+
signal: n.signal
|
|
75
75
|
});
|
|
76
|
-
if (clearTimeout(
|
|
77
|
-
const
|
|
78
|
-
throw new Error(`Network response was not ok: ${
|
|
76
|
+
if (clearTimeout(c), !a.ok) {
|
|
77
|
+
const l = await a.text();
|
|
78
|
+
throw new Error(`Network response was not ok: ${a.status} ${l}`);
|
|
79
79
|
}
|
|
80
|
-
if (!
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
text:
|
|
80
|
+
if (!t) {
|
|
81
|
+
const l = await a.json();
|
|
82
|
+
l.response && (yield {
|
|
83
|
+
text: l.response,
|
|
84
84
|
done: !0
|
|
85
85
|
});
|
|
86
86
|
return;
|
|
87
87
|
}
|
|
88
|
-
const
|
|
89
|
-
if (!
|
|
88
|
+
const w = (s = a.body) == null ? void 0 : s.getReader();
|
|
89
|
+
if (!w) throw new Error("No reader available");
|
|
90
90
|
const I = new TextDecoder();
|
|
91
|
-
let i = "",
|
|
91
|
+
let i = "", y = !1;
|
|
92
92
|
try {
|
|
93
93
|
for (; ; ) {
|
|
94
|
-
const { done:
|
|
95
|
-
if (
|
|
94
|
+
const { done: l, value: A } = await w.read();
|
|
95
|
+
if (l)
|
|
96
96
|
break;
|
|
97
|
-
const
|
|
98
|
-
i +=
|
|
99
|
-
let
|
|
100
|
-
for (; (
|
|
101
|
-
`,
|
|
102
|
-
const d = i.slice(
|
|
103
|
-
if (
|
|
104
|
-
const
|
|
105
|
-
if (!
|
|
97
|
+
const E = I.decode(A, { stream: !0 });
|
|
98
|
+
i += E;
|
|
99
|
+
let u = 0, g;
|
|
100
|
+
for (; (g = i.indexOf(`
|
|
101
|
+
`, u)) !== -1; ) {
|
|
102
|
+
const d = i.slice(u, g).trim();
|
|
103
|
+
if (u = g + 1, d && d.startsWith("data: ")) {
|
|
104
|
+
const h = d.slice(6).trim();
|
|
105
|
+
if (!h || h === "[DONE]") {
|
|
106
106
|
yield { text: "", done: !0 };
|
|
107
107
|
return;
|
|
108
108
|
}
|
|
109
109
|
try {
|
|
110
|
-
const o = JSON.parse(
|
|
110
|
+
const o = JSON.parse(h);
|
|
111
111
|
if (o.error)
|
|
112
112
|
throw new Error(`Server Error: ${o.error.message}`);
|
|
113
|
-
if (o.response && (
|
|
113
|
+
if (o.response && (y = !0, yield { text: o.response, done: o.done || !1 }), o.done) {
|
|
114
114
|
yield { text: "", done: !0 };
|
|
115
115
|
return;
|
|
116
116
|
}
|
|
117
117
|
} catch (o) {
|
|
118
|
-
console.warn("Error parsing JSON chunk:", o, "Chunk:",
|
|
118
|
+
console.warn("Error parsing JSON chunk:", o, "Chunk:", h);
|
|
119
119
|
}
|
|
120
|
-
} else d && (
|
|
120
|
+
} else d && (y = !0, yield { text: d, done: !1 });
|
|
121
121
|
}
|
|
122
|
-
i = i.slice(
|
|
122
|
+
i = i.slice(u), i.length > 1e6 && (console.warn("Buffer too large, truncating..."), i = i.slice(-5e5));
|
|
123
123
|
}
|
|
124
|
-
|
|
124
|
+
y && (yield { text: "", done: !0 });
|
|
125
125
|
} finally {
|
|
126
|
-
|
|
126
|
+
w.releaseLock();
|
|
127
127
|
}
|
|
128
|
-
} catch (
|
|
129
|
-
throw
|
|
128
|
+
} catch (n) {
|
|
129
|
+
throw n.name === "AbortError" ? new Error("Connection timed out. Please check if the server is running.") : n.name === "TypeError" && n.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : n;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async clearConversationHistory(e) {
|
|
133
|
+
const t = e || this.sessionId;
|
|
134
|
+
if (!t)
|
|
135
|
+
throw new Error("No session ID provided and no current session available");
|
|
136
|
+
if (!this.apiKey)
|
|
137
|
+
throw new Error("API key is required for clearing conversation history");
|
|
138
|
+
const s = {
|
|
139
|
+
"Content-Type": "application/json",
|
|
140
|
+
"X-Session-ID": t,
|
|
141
|
+
"X-API-Key": this.apiKey
|
|
142
|
+
};
|
|
143
|
+
try {
|
|
144
|
+
const n = await fetch(`${this.apiUrl}/admin/chat-history/${t}`, {
|
|
145
|
+
...this.getFetchOptions({
|
|
146
|
+
method: "DELETE",
|
|
147
|
+
headers: s
|
|
148
|
+
})
|
|
149
|
+
});
|
|
150
|
+
if (!n.ok) {
|
|
151
|
+
const a = await n.text();
|
|
152
|
+
throw new Error(`Failed to clear conversation history: ${n.status} ${a}`);
|
|
153
|
+
}
|
|
154
|
+
return await n.json();
|
|
155
|
+
} catch (n) {
|
|
156
|
+
throw n.name === "TypeError" && n.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : n;
|
|
130
157
|
}
|
|
131
158
|
}
|
|
132
159
|
}
|
|
133
|
-
let
|
|
134
|
-
const S = (
|
|
135
|
-
|
|
160
|
+
let v = null;
|
|
161
|
+
const S = (r, e = null, t = null) => {
|
|
162
|
+
v = new x({ apiUrl: r, apiKey: e, sessionId: t });
|
|
136
163
|
};
|
|
137
|
-
async function*
|
|
138
|
-
if (!
|
|
164
|
+
async function* P(r, e = !0) {
|
|
165
|
+
if (!v)
|
|
139
166
|
throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");
|
|
140
|
-
yield*
|
|
167
|
+
yield* v.streamChat(r, e);
|
|
141
168
|
}
|
|
142
169
|
export {
|
|
143
170
|
x as ApiClient,
|
|
144
171
|
S as configureApi,
|
|
145
|
-
|
|
172
|
+
P as streamChat
|
|
146
173
|
};
|
|
147
174
|
//# sourceMappingURL=api.mjs.map
|
package/dist/api.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.mjs","sources":["../api.ts"],"sourcesContent":["// For Node.js environments, we can use http.Agent for connection pooling\nlet httpAgent: any = null;\nlet httpsAgent: any = null;\n\n// Initialize agents for connection pooling in Node.js environments\nif (typeof window === 'undefined') {\n // Lazy load to avoid including 'http' in browser bundles\n Promise.all([\n import('http').catch(() => null),\n import('https').catch(() => null)\n ]).then(([http, https]) => {\n if (http?.default?.Agent) {\n httpAgent = new http.default.Agent({ keepAlive: true });\n } else if (http?.Agent) {\n httpAgent = new http.Agent({ keepAlive: true });\n }\n \n if (https?.default?.Agent) {\n httpsAgent = new https.default.Agent({ keepAlive: true });\n } else if (https?.Agent) {\n httpsAgent = new https.Agent({ keepAlive: true });\n }\n }).catch(err => {\n // Silently fail - connection pooling is optional\n console.warn('Failed to initialize HTTP agents:', err.message);\n });\n}\n\n// Define the StreamResponse interface\nexport interface StreamResponse {\n text: string;\n done: boolean;\n}\n\n// The server now returns this directly for non-streaming chat\nexport interface ChatResponse {\n response: string;\n sources?: any[];\n}\n\n// The request body for the /v1/chat endpoint\ninterface ChatRequest {\n messages: Array<{ role: string; content: string; }>;\n stream: boolean;\n}\n\nexport class ApiClient {\n private readonly apiUrl: string;\n private readonly apiKey: string | null;\n private sessionId: string | null; // Session ID can be mutable\n\n constructor(config: { apiUrl: string; apiKey?: string | null; sessionId?: string | null }) {\n if (!config.apiUrl || typeof config.apiUrl !== 'string') {\n throw new Error('API URL must be a valid string');\n }\n if (config.apiKey !== undefined && config.apiKey !== null && typeof config.apiKey !== 'string') {\n throw new Error('API key must be a valid string or null');\n }\n if (config.sessionId !== undefined && config.sessionId !== null && typeof config.sessionId !== 'string') {\n throw new Error('Session ID must be a valid string or null');\n }\n \n this.apiUrl = config.apiUrl;\n this.apiKey = config.apiKey ?? null;\n this.sessionId = config.sessionId ?? null;\n }\n\n public setSessionId(sessionId: string | null): void {\n if (sessionId !== null && typeof sessionId !== 'string') {\n throw new Error('Session ID must be a valid string or null');\n }\n this.sessionId = sessionId;\n }\n\n public getSessionId(): string | null {\n return this.sessionId;\n }\n\n // Helper to get fetch options with connection pooling if available\n private getFetchOptions(options: RequestInit = {}): RequestInit {\n const baseOptions: RequestInit = {};\n \n // Environment-specific options\n if (typeof window === 'undefined') {\n // Node.js: Use connection pooling agent\n const isHttps = this.apiUrl.startsWith('https:');\n const agent = isHttps ? httpsAgent : httpAgent;\n if (agent) {\n (baseOptions as any).agent = agent;\n }\n } else {\n // Browser: Use keep-alive header\n baseOptions.headers = { 'Connection': 'keep-alive' };\n }\n\n // Common headers\n const headers: Record<string, string> = {\n 'X-Request-ID': Date.now().toString(36) + Math.random().toString(36).substring(2),\n };\n\n // Merge base options headers (for browser keep-alive)\n if (baseOptions.headers) {\n Object.assign(headers, baseOptions.headers);\n }\n\n // Merge original request headers\n if (options.headers) {\n Object.assign(headers, options.headers);\n }\n\n if (this.apiKey) {\n headers['X-API-Key'] = this.apiKey;\n }\n\n if (this.sessionId) {\n headers['X-Session-ID'] = this.sessionId;\n }\n\n return {\n ...options,\n ...baseOptions,\n headers,\n };\n }\n\n // Create Chat request\n private createChatRequest(message: string, stream: boolean = true): ChatRequest {\n return {\n messages: [\n { role: \"user\", content: message }\n ],\n stream\n };\n }\n\n public async *streamChat(\n message: string,\n stream: boolean = true\n ): AsyncGenerator<StreamResponse> {\n try {\n // Add timeout to the fetch request\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), 60000); // 60 second timeout\n\n const response = await fetch(`${this.apiUrl}/v1/chat`, {\n ...this.getFetchOptions({\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Accept': stream ? 'text/event-stream' : 'application/json'\n },\n body: JSON.stringify(this.createChatRequest(message, stream)),\n }),\n signal: controller.signal\n });\n\n clearTimeout(timeoutId);\n\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(`Network response was not ok: ${response.status} ${errorText}`);\n }\n\n if (!stream) {\n // Handle non-streaming response\n const data = await response.json() as ChatResponse;\n if (data.response) {\n yield {\n text: data.response,\n done: true\n };\n }\n return;\n }\n \n const reader = response.body?.getReader();\n if (!reader) throw new Error('No reader available');\n\n const decoder = new TextDecoder();\n let buffer = '';\n let hasReceivedContent = false;\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n\n const chunk = decoder.decode(value, { stream: true });\n buffer += chunk;\n \n // Process complete lines immediately as they arrive\n let lineStartIndex = 0;\n let newlineIndex;\n \n while ((newlineIndex = buffer.indexOf('\\n', lineStartIndex)) !== -1) {\n const line = buffer.slice(lineStartIndex, newlineIndex).trim();\n lineStartIndex = newlineIndex + 1;\n \n if (line && line.startsWith('data: ')) {\n const jsonText = line.slice(6).trim();\n \n if (!jsonText || jsonText === '[DONE]') {\n yield { text: '', done: true };\n return;\n }\n\n try {\n const data = JSON.parse(jsonText);\n \n if (data.error) {\n throw new Error(`Server Error: ${data.error.message}`);\n }\n \n if (data.response) {\n hasReceivedContent = true;\n yield { text: data.response, done: data.done || false };\n }\n\n if (data.done) {\n yield { text: '', done: true };\n return;\n }\n \n } catch (parseError) {\n console.warn('Error parsing JSON chunk:', parseError, 'Chunk:', jsonText);\n }\n } else if (line) {\n // Handle raw text chunks that are not in SSE format\n hasReceivedContent = true;\n yield { text: line, done: false };\n }\n }\n \n buffer = buffer.slice(lineStartIndex);\n \n if (buffer.length > 1000000) { // 1MB limit\n console.warn('Buffer too large, truncating...');\n buffer = buffer.slice(-500000); // Keep last 500KB\n }\n }\n \n if (hasReceivedContent) {\n yield { text: '', done: true };\n }\n \n } finally {\n reader.releaseLock();\n }\n \n } catch (error: any) {\n if (error.name === 'AbortError') {\n throw new Error('Connection timed out. Please check if the server is running.');\n } else if (error.name === 'TypeError' && error.message.includes('Failed to fetch')) {\n throw new Error('Could not connect to the server. Please check if the server is running.');\n } else {\n throw error;\n }\n }\n }\n}\n\n// Legacy compatibility functions - these create a default client instance\n// These are kept for backward compatibility but should be deprecated in favor of the class-based approach\n\nlet defaultClient: ApiClient | null = null;\n\n// Configure the API with a custom URL, API key (optional), and session ID (optional)\nexport const configureApi = (apiUrl: string, apiKey: string | null = null, sessionId: string | null = null): void => {\n defaultClient = new ApiClient({ apiUrl, apiKey, sessionId });\n}\n\n// Legacy streamChat function that uses the default client\nexport async function* streamChat(\n message: string,\n stream: boolean = true\n): AsyncGenerator<StreamResponse> {\n if (!defaultClient) {\n throw new Error('API not configured. Please call configureApi() with your server URL before using any API functions.');\n }\n \n yield* defaultClient.streamChat(message, stream);\n}\n"],"names":["httpAgent","httpsAgent","http","https","_a","_b","err","ApiClient","config","__publicField","sessionId","options","baseOptions","agent","headers","message","stream","controller","timeoutId","response","errorText","data","reader","decoder","buffer","hasReceivedContent","done","value","chunk","lineStartIndex","newlineIndex","line","jsonText","parseError","error","defaultClient","configureApi","apiUrl","apiKey","streamChat"],"mappings":";;;AACA,IAAIA,IAAiB,MACjBC,IAAkB;AAGlB,OAAO,SAAW,OAEpB,QAAQ,IAAI;AAAA,EACV,OAAO,MAAM,EAAE,MAAM,MAAM,IAAI;AAAA,EAC/B,OAAO,OAAO,EAAE,MAAM,MAAM,IAAI;AAAA,CACjC,EAAE,KAAK,CAAC,CAACC,GAAMC,CAAK,MAAM;AAT7B,MAAAC,GAAAC;AAUI,GAAID,IAAAF,KAAA,gBAAAA,EAAM,YAAN,QAAAE,EAAe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AAU,MAAMA,EAAK,QAAQ,GAAA,IAG9CA,EAAK,MAAM;AACX,wBAAM,EAAE,MAAM,IAAI,MAAM,GAAA;AACxB;AAAA,gBACJ;AAAA,cAEF,SAASY,GAAY;AACnB,wBAAQ,KAAK,6BAA6BA,GAAY,UAAUD,CAAQ;AAAA,cAC1E;AAAA,YACF,OAAWD,MAEPN,IAAqB,IACrB,MAAM,EAAE,MAAMM,GAAM,MAAM,GAAA;AAAA,UAEhC;AAEA,UAAAP,IAASA,EAAO,MAAMK,CAAc,GAEhCL,EAAO,SAAS,QAClB,QAAQ,KAAK,iCAAiC,GAC9CA,IAASA,EAAO,MAAM,IAAO;AAAA,QAEjC;AAEA,QAAIC,MACF,MAAM,EAAE,MAAM,IAAI,MAAM,GAAA;AAAA,MAG5B,UAAA;AACE,QAAAH,EAAO,YAAA;AAAA,MACT;AAAA,IAEF,SAASY,GAAY;AACnB,YAAIA,EAAM,SAAS,eACX,IAAI,MAAM,8DAA8D,IACrEA,EAAM,SAAS,eAAeA,EAAM,QAAQ,SAAS,iBAAiB,IACzE,IAAI,MAAM,yEAAyE,IAEnFA;AAAA,IAEV;AAAA,EACF;AACF;AAKA,IAAIC,IAAkC;AAG/B,MAAMC,IAAe,CAACC,GAAgBC,IAAwB,MAAM5B,IAA2B,SAAe;AACnH,EAAAyB,IAAgB,IAAI5B,EAAU,EAAE,QAAA8B,GAAQ,QAAAC,GAAQ,WAAA5B,GAAW;AAC7D;AAGA,gBAAuB6B,EACrBxB,GACAC,IAAkB,IACc;AAChC,MAAI,CAACmB;AACH,UAAM,IAAI,MAAM,qGAAqG;AAGvH,SAAOA,EAAc,WAAWpB,GAASC,CAAM;AACjD;"}
|
|
1
|
+
{"version":3,"file":"api.mjs","sources":["../api.ts"],"sourcesContent":["// For Node.js environments, we can use http.Agent for connection pooling\nlet httpAgent: any = null;\nlet httpsAgent: any = null;\n\n// Initialize agents for connection pooling in Node.js environments\nif (typeof window === 'undefined') {\n // Lazy load to avoid including 'http' in browser bundles\n Promise.all([\n import('http').catch(() => null),\n import('https').catch(() => null)\n ]).then(([http, https]) => {\n if (http?.default?.Agent) {\n httpAgent = new http.default.Agent({ keepAlive: true });\n } else if (http?.Agent) {\n httpAgent = new http.Agent({ keepAlive: true });\n }\n \n if (https?.default?.Agent) {\n httpsAgent = new https.default.Agent({ keepAlive: true });\n } else if (https?.Agent) {\n httpsAgent = new https.Agent({ keepAlive: true });\n }\n }).catch(err => {\n // Silently fail - connection pooling is optional\n console.warn('Failed to initialize HTTP agents:', err.message);\n });\n}\n\n// Define the StreamResponse interface\nexport interface StreamResponse {\n text: string;\n done: boolean;\n}\n\n// The server now returns this directly for non-streaming chat\nexport interface ChatResponse {\n response: string;\n sources?: any[];\n}\n\n// The request body for the /v1/chat endpoint\ninterface ChatRequest {\n messages: Array<{ role: string; content: string; }>;\n stream: boolean;\n}\n\nexport class ApiClient {\n private readonly apiUrl: string;\n private readonly apiKey: string | null;\n private sessionId: string | null; // Session ID can be mutable\n\n constructor(config: { apiUrl: string; apiKey?: string | null; sessionId?: string | null }) {\n if (!config.apiUrl || typeof config.apiUrl !== 'string') {\n throw new Error('API URL must be a valid string');\n }\n if (config.apiKey !== undefined && config.apiKey !== null && typeof config.apiKey !== 'string') {\n throw new Error('API key must be a valid string or null');\n }\n if (config.sessionId !== undefined && config.sessionId !== null && typeof config.sessionId !== 'string') {\n throw new Error('Session ID must be a valid string or null');\n }\n \n this.apiUrl = config.apiUrl;\n this.apiKey = config.apiKey ?? null;\n this.sessionId = config.sessionId ?? null;\n }\n\n public setSessionId(sessionId: string | null): void {\n if (sessionId !== null && typeof sessionId !== 'string') {\n throw new Error('Session ID must be a valid string or null');\n }\n this.sessionId = sessionId;\n }\n\n public getSessionId(): string | null {\n return this.sessionId;\n }\n\n // Helper to get fetch options with connection pooling if available\n private getFetchOptions(options: RequestInit = {}): RequestInit {\n const baseOptions: RequestInit = {};\n \n // Environment-specific options\n if (typeof window === 'undefined') {\n // Node.js: Use connection pooling agent\n const isHttps = this.apiUrl.startsWith('https:');\n const agent = isHttps ? httpsAgent : httpAgent;\n if (agent) {\n (baseOptions as any).agent = agent;\n }\n } else {\n // Browser: Use keep-alive header\n baseOptions.headers = { 'Connection': 'keep-alive' };\n }\n\n // Common headers\n const headers: Record<string, string> = {\n 'X-Request-ID': Date.now().toString(36) + Math.random().toString(36).substring(2),\n };\n\n // Merge base options headers (for browser keep-alive)\n if (baseOptions.headers) {\n Object.assign(headers, baseOptions.headers);\n }\n\n // Merge original request headers\n if (options.headers) {\n Object.assign(headers, options.headers);\n }\n\n if (this.apiKey) {\n headers['X-API-Key'] = this.apiKey;\n }\n\n if (this.sessionId) {\n headers['X-Session-ID'] = this.sessionId;\n }\n\n return {\n ...options,\n ...baseOptions,\n headers,\n };\n }\n\n // Create Chat request\n private createChatRequest(message: string, stream: boolean = true): ChatRequest {\n return {\n messages: [\n { role: \"user\", content: message }\n ],\n stream\n };\n }\n\n public async *streamChat(\n message: string,\n stream: boolean = true\n ): AsyncGenerator<StreamResponse> {\n try {\n // Add timeout to the fetch request\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), 60000); // 60 second timeout\n\n const response = await fetch(`${this.apiUrl}/v1/chat`, {\n ...this.getFetchOptions({\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Accept': stream ? 'text/event-stream' : 'application/json'\n },\n body: JSON.stringify(this.createChatRequest(message, stream)),\n }),\n signal: controller.signal\n });\n\n clearTimeout(timeoutId);\n\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(`Network response was not ok: ${response.status} ${errorText}`);\n }\n\n if (!stream) {\n // Handle non-streaming response\n const data = await response.json() as ChatResponse;\n if (data.response) {\n yield {\n text: data.response,\n done: true\n };\n }\n return;\n }\n \n const reader = response.body?.getReader();\n if (!reader) throw new Error('No reader available');\n\n const decoder = new TextDecoder();\n let buffer = '';\n let hasReceivedContent = false;\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n\n const chunk = decoder.decode(value, { stream: true });\n buffer += chunk;\n \n // Process complete lines immediately as they arrive\n let lineStartIndex = 0;\n let newlineIndex;\n \n while ((newlineIndex = buffer.indexOf('\\n', lineStartIndex)) !== -1) {\n const line = buffer.slice(lineStartIndex, newlineIndex).trim();\n lineStartIndex = newlineIndex + 1;\n \n if (line && line.startsWith('data: ')) {\n const jsonText = line.slice(6).trim();\n \n if (!jsonText || jsonText === '[DONE]') {\n yield { text: '', done: true };\n return;\n }\n\n try {\n const data = JSON.parse(jsonText);\n \n if (data.error) {\n throw new Error(`Server Error: ${data.error.message}`);\n }\n \n if (data.response) {\n hasReceivedContent = true;\n yield { text: data.response, done: data.done || false };\n }\n\n if (data.done) {\n yield { text: '', done: true };\n return;\n }\n \n } catch (parseError) {\n console.warn('Error parsing JSON chunk:', parseError, 'Chunk:', jsonText);\n }\n } else if (line) {\n // Handle raw text chunks that are not in SSE format\n hasReceivedContent = true;\n yield { text: line, done: false };\n }\n }\n \n buffer = buffer.slice(lineStartIndex);\n \n if (buffer.length > 1000000) { // 1MB limit\n console.warn('Buffer too large, truncating...');\n buffer = buffer.slice(-500000); // Keep last 500KB\n }\n }\n \n if (hasReceivedContent) {\n yield { text: '', done: true };\n }\n \n } finally {\n reader.releaseLock();\n }\n \n } catch (error: any) {\n if (error.name === 'AbortError') {\n throw new Error('Connection timed out. Please check if the server is running.');\n } else if (error.name === 'TypeError' && error.message.includes('Failed to fetch')) {\n throw new Error('Could not connect to the server. Please check if the server is running.');\n } else {\n throw error;\n }\n }\n }\n\n public async clearConversationHistory(sessionId?: string): Promise<{\n status: string;\n message: string;\n session_id: string;\n deleted_count: number;\n timestamp: string;\n }> {\n /**\n * Clear conversation history for a session.\n * \n * @param sessionId - Optional session ID to clear. If not provided, uses current session.\n * @returns Promise resolving to operation result\n * @throws Error if the operation fails\n */\n const targetSessionId = sessionId || this.sessionId;\n \n if (!targetSessionId) {\n throw new Error('No session ID provided and no current session available');\n }\n \n if (!this.apiKey) {\n throw new Error('API key is required for clearing conversation history');\n }\n \n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n 'X-Session-ID': targetSessionId,\n 'X-API-Key': this.apiKey\n };\n \n try {\n const response = await fetch(`${this.apiUrl}/admin/chat-history/${targetSessionId}`, {\n ...this.getFetchOptions({\n method: 'DELETE',\n headers\n })\n });\n \n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(`Failed to clear conversation history: ${response.status} ${errorText}`);\n }\n \n const result = await response.json();\n return result;\n \n } catch (error: any) {\n if (error.name === 'TypeError' && error.message.includes('Failed to fetch')) {\n throw new Error('Could not connect to the server. Please check if the server is running.');\n } else {\n throw error;\n }\n }\n }\n}\n\n// Legacy compatibility functions - these create a default client instance\n// These are kept for backward compatibility but should be deprecated in favor of the class-based approach\n\nlet defaultClient: ApiClient | null = null;\n\n// Configure the API with a custom URL, API key (optional), and session ID (optional)\nexport const configureApi = (apiUrl: string, apiKey: string | null = null, sessionId: string | null = null): void => {\n defaultClient = new ApiClient({ apiUrl, apiKey, sessionId });\n}\n\n// Legacy streamChat function that uses the default client\nexport async function* streamChat(\n message: string,\n stream: boolean = true\n): AsyncGenerator<StreamResponse> {\n if (!defaultClient) {\n throw new Error('API not configured. Please call configureApi() with your server URL before using any API functions.');\n }\n \n yield* defaultClient.streamChat(message, stream);\n}\n\n"],"names":["httpAgent","httpsAgent","http","https","_a","_b","err","ApiClient","config","__publicField","sessionId","options","baseOptions","agent","headers","message","stream","controller","timeoutId","response","errorText","data","reader","decoder","buffer","hasReceivedContent","done","value","chunk","lineStartIndex","newlineIndex","line","jsonText","parseError","error","targetSessionId","defaultClient","configureApi","apiUrl","apiKey","streamChat"],"mappings":";;;AACA,IAAIA,IAAiB,MACjBC,IAAkB;AAGlB,OAAO,SAAW,OAEpB,QAAQ,IAAI;AAAA,EACV,OAAO,MAAM,EAAE,MAAM,MAAM,IAAI;AAAA,EAC/B,OAAO,OAAO,EAAE,MAAM,MAAM,IAAI;AAAA,CACjC,EAAE,K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;AAAA,UACtB,QAAQ;AAAA,UACR,SAAArB;AAAA,QAAA,CACD;AAAA,MAAA,CACF;AAED,UAAI,CAACK,EAAS,IAAI;AAChB,cAAMC,IAAY,MAAMD,EAAS,KAAA;AACjC,cAAM,IAAI,MAAM,yCAAyCA,EAAS,MAAM,IAAIC,CAAS,EAAE;AAAA,MACzF;AAGA,aADe,MAAMD,EAAS,KAAA;AAAA,IAGhC,SAASe,GAAY;AACnB,YAAIA,EAAM,SAAS,eAAeA,EAAM,QAAQ,SAAS,iBAAiB,IAClE,IAAI,MAAM,yEAAyE,IAEnFA;AAAA,IAEV;AAAA,EACF;AACF;AAKA,IAAIE,IAAkC;AAG/B,MAAMC,IAAe,CAACC,GAAgBC,IAAwB,MAAM7B,IAA2B,SAAe;AACnH,EAAA0B,IAAgB,IAAI7B,EAAU,EAAE,QAAA+B,GAAQ,QAAAC,GAAQ,WAAA7B,GAAW;AAC7D;AAGA,gBAAuB8B,EACrBzB,GACAC,IAAkB,IACc;AAChC,MAAI,CAACoB;AACH,UAAM,IAAI,MAAM,qGAAqG;AAGvH,SAAOA,EAAc,WAAWrB,GAASC,CAAM;AACjD;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schmitech/chatbot-api",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.5.
|
|
4
|
+
"version": "0.5.3",
|
|
5
5
|
"description": "API client for the ORBIT MCP server",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/api.cjs",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"preview": "vite preview",
|
|
30
30
|
"test": "vitest",
|
|
31
31
|
"test:watch": "vitest",
|
|
32
|
+
"test:npm": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' ./test/test-npm-package.ts",
|
|
32
33
|
"test-query": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' ./test/run-query.js",
|
|
33
34
|
"test-query-from-pairs": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' ./test/run-query-from-pairs.js"
|
|
34
35
|
},
|