llmasaservice-client 0.9.1 → 0.9.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/dist/index.d.mts CHANGED
@@ -22,13 +22,21 @@ interface UserProviderProps {
22
22
  }
23
23
  declare const LLMServiceProvider: React.FC<UserProviderProps>;
24
24
 
25
+ interface Message {
26
+ role: string;
27
+ content: string;
28
+ }
29
+ interface DataItem {
30
+ key: string;
31
+ data: string;
32
+ }
25
33
  interface UseLLMReturnType {
26
- send: Function;
27
- stop: Function;
34
+ send: (prompt: string, messages?: Message[], data?: DataItem[], stream?: boolean, allowCaching?: boolean, service?: string | null, conversation?: string | null, abortController?: AbortController, onComplete?: (result: string) => void, onError?: (error: string) => void) => Promise<ReadableStreamDefaultReader<any> | string | undefined>;
35
+ stop: (controller: AbortController | null) => void;
28
36
  response: string;
29
37
  idle: boolean;
30
38
  error: string;
31
- setResponse: Function;
39
+ setResponse: (response: string) => void;
32
40
  lastCallId: string;
33
41
  }
34
42
  declare const useLLM: (options?: LLMServiceType) => UseLLMReturnType;
package/dist/index.d.ts CHANGED
@@ -22,13 +22,21 @@ interface UserProviderProps {
22
22
  }
23
23
  declare const LLMServiceProvider: React.FC<UserProviderProps>;
24
24
 
25
+ interface Message {
26
+ role: string;
27
+ content: string;
28
+ }
29
+ interface DataItem {
30
+ key: string;
31
+ data: string;
32
+ }
25
33
  interface UseLLMReturnType {
26
- send: Function;
27
- stop: Function;
34
+ send: (prompt: string, messages?: Message[], data?: DataItem[], stream?: boolean, allowCaching?: boolean, service?: string | null, conversation?: string | null, abortController?: AbortController, onComplete?: (result: string) => void, onError?: (error: string) => void) => Promise<ReadableStreamDefaultReader<any> | string | undefined>;
35
+ stop: (controller: AbortController | null) => void;
28
36
  response: string;
29
37
  idle: boolean;
30
38
  error: string;
31
- setResponse: Function;
39
+ setResponse: (response: string) => void;
32
40
  lastCallId: string;
33
41
  }
34
42
  declare const useLLM: (options?: LLMServiceType) => UseLLMReturnType;
package/dist/index.js CHANGED
@@ -219,3 +219,4 @@ var useLLM = (options) => {
219
219
  LLMServiceProvider,
220
220
  useLLM
221
221
  });
222
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../index.ts","../src/useLLM.ts","../src/LLMAsAService.tsx"],"sourcesContent":["\r\nexport { useLLM, UseLLMReturnType } from \"./src/useLLM\";\r\nexport * from './src/LLMAsAService';","import { useContext, useState } from \"react\";\r\nimport { LLMService, LLMServiceType } from \"./LLMAsAService\";\r\n\r\nexport interface Message {\r\n role: string;\r\n content: string;\r\n}\r\n\r\nexport interface DataItem {\r\n key: string;\r\n data: string;\r\n}\r\n\r\nexport interface UseLLMReturnType {\r\n send: (\r\n prompt: string,\r\n messages?: Message[],\r\n data?: DataItem[],\r\n stream?: boolean,\r\n allowCaching?: boolean,\r\n service?: string | null,\r\n conversation?: string | null,\r\n abortController?: AbortController,\r\n onComplete?: (result: string) => void,\r\n onError?: (error: string) => void\r\n ) => Promise<ReadableStreamDefaultReader<any> | string | undefined>;\r\n stop: (controller: AbortController | null) => void;\r\n response: string;\r\n idle: boolean;\r\n error: string;\r\n setResponse: (response: string) => void;\r\n lastCallId: string;\r\n}\r\n\r\nexport const useLLM = (options?: LLMServiceType): UseLLMReturnType => {\r\n const [response, setResponse] = useState<string>(\"\");\r\n const [idle, setIdle] = useState<boolean>(true);\r\n const [error, setError] = useState<string>(\"\");\r\n const [lastCallId, setLastCallId] = useState<string>(\"\");\r\n\r\n let context = useContext(LLMService);\r\n if (!context) {\r\n context = options;\r\n }\r\n\r\n if (!context) {\r\n throw new Error(\r\n \"useLLM must be used within a LLMServiceProvider or constructed with options in your useLLM() call.\"\r\n );\r\n }\r\n\r\n /**\r\n * Stops the fetch request and returns the hook to an idle state. Use this to add abort functionality to your UI.\r\n *\r\n * @param controller An AbortController object to stop the fetch request and return this hook to an idle state, the controller should be the same one passed to the send function.\r\n */\r\n const stop = (controller: AbortController | null) => {\r\n if (controller) controller.abort();\r\n setIdle(true);\r\n };\r\n\r\n /**\r\n * Calls the LLM as a service with the given prompt and messages. The response is returned in the response property of the hook.\r\n *\r\n * @param {string} prompt - The prompt to send to the LLM service.\r\n * @param {Message[]} messages - The history and context messages to send to the LLM service, as an array of {role: string, content: string} objects. For example, [{ role: \"system\", content: \"You are a useful assistant.\" }]\r\n * @param {DataItem[]} data - The data to send to the LLM service, as an array of {key: string, data: string} objects. For example, [{ key: \"name\", value: \"John\" }]\r\n * @param {boolean} stream - Determines whether to stream results back in the response property as they return from the service or batch them up and return them all at once in the response property as a string.\r\n * @param {boolean} allowCaching - Determines whether the service can use cached results or not.\r\n * @param {string | null} service - The service to use for the request. If null, load balancing will be applied. This is typically only used for testing.\r\n * @param {string | null} conversation - The conversation of this request. If null, this is a one off call with no conversation history\r\n * @param {AbortController} abortController - The AbortController used to abort this request once it's started. This allows you to add a stop button to your UI.\r\n * @param {(result: string) => void} onComplete - The callback function to be called once the stream completes, with the final result string.\r\n * @param {(error: string) => void} onError - The callback function to be called if an error occurs, with the error string.\r\n * @returns {Promise<ReadableStreamDefaultReader<any> | string | undefined>} - A StreamReader object if stream is true, otherwise a string of the response. Typically this isn't used when streaming, the stream is exposed in the response property.\r\n */\r\n async function send(\r\n prompt: string,\r\n messages: Message[] = [],\r\n data: DataItem[] = [],\r\n stream: boolean = true,\r\n allowCaching: boolean = true,\r\n service: string | null = null, // null means use the default service and apply services load balancing\r\n conversation: string | null = null,\r\n abortController: AbortController = new AbortController(),\r\n onComplete?: (result: string) => void,\r\n onError?: (error: string) => void\r\n ): Promise<ReadableStreamDefaultReader<any> | string | undefined> {\r\n setResponse(\"\");\r\n setIdle(false);\r\n\r\n let errorInFetch = \"\";\r\n\r\n const responseBody = JSON.stringify({\r\n projectId: context?.project_id ?? \"\",\r\n serviceId: service,\r\n agentId: context?.agent,\r\n prompt: prompt,\r\n messages: messages,\r\n data: data,\r\n customer: context?.customer ?? {}, // if no customer, use the projectId as the customer_id\r\n allowCaching: allowCaching,\r\n conversationId: conversation,\r\n });\r\n\r\n // trying to get cloudfront oac going. posts need to be signed, but when i add this the call fails...\r\n const options = {\r\n method: \"POST\",\r\n signal: abortController.signal,\r\n mode: \"cors\" as RequestMode,\r\n headers: {\r\n \"Content-Type\": \"text/plain\",\r\n },\r\n body: responseBody,\r\n };\r\n\r\n try {\r\n const url = context?.url ?? \"https://chat.llmasaservice.io/\";\r\n const response = await fetch(url, options);\r\n if (!response.ok) {\r\n errorInFetch = `Error: Network error for service. (${response.status} ${response.statusText})`;\r\n } else {\r\n setLastCallId(response.headers.get(\"x-callId\") ?? \"\");\r\n const reader =\r\n response?.body?.getReader() as ReadableStreamDefaultReader;\r\n const decoder = new TextDecoder(\"utf-8\");\r\n setIdle(false);\r\n\r\n if (!stream) {\r\n return await readStream(\r\n reader,\r\n decoder,\r\n stream,\r\n {\r\n signal: options.signal,\r\n },\r\n onComplete,\r\n onError\r\n );\r\n } else {\r\n readStream(\r\n reader,\r\n decoder,\r\n stream,\r\n {\r\n signal: options.signal,\r\n },\r\n onComplete,\r\n onError\r\n );\r\n\r\n return reader;\r\n }\r\n }\r\n } catch (errorObject: any) {\r\n errorInFetch = `Error: Having trouble connecting to chat service. (${errorObject.message})`;\r\n }\r\n\r\n if (errorInFetch !== \"\") {\r\n setError(errorInFetch);\r\n if (onError) {\r\n onError(errorInFetch);\r\n }\r\n console.error(`Error: Error in fetch. (${errorInFetch})`);\r\n }\r\n }\r\n\r\n async function readStream(\r\n reader: ReadableStreamDefaultReader,\r\n decoder: TextDecoder,\r\n stream: Boolean = true,\r\n { signal: signal }: { signal: AbortSignal },\r\n onComplete?: (result: string) => void,\r\n onError?: (error: string) => void\r\n ): Promise<string> {\r\n let errorInRead = \"\";\r\n let result = \"\";\r\n\r\n while (true) {\r\n try {\r\n // Check if the stream has been aborted\r\n if (signal.aborted) {\r\n reader.cancel();\r\n setIdle(true);\r\n break;\r\n }\r\n\r\n // Read a chunk of data from the stream\r\n const { value, done } = await reader.read();\r\n\r\n if (decoder.decode(value).startsWith(\"Error:\")) {\r\n errorInRead = decoder.decode(value).substring(6);\r\n break;\r\n }\r\n\r\n // If the stream has been read to the end, exit the loop\r\n if (done) {\r\n setIdle(true);\r\n break;\r\n }\r\n\r\n // Process the chunk of data\r\n result += decoder.decode(value);\r\n if (stream) setResponse((prevState: any) => result);\r\n } catch (error: any) {\r\n if (error.name === \"AbortError\") {\r\n break;\r\n }\r\n\r\n errorInRead = `Reading error ${error.message}`;\r\n break;\r\n } finally {\r\n if (signal.aborted) {\r\n reader.releaseLock();\r\n }\r\n }\r\n }\r\n\r\n if (errorInRead !== \"\") {\r\n setError(errorInRead);\r\n reader.cancel();\r\n if (onError) onError(errorInRead);\r\n setIdle(true);\r\n }\r\n\r\n if (onComplete) {\r\n onComplete(result);\r\n }\r\n\r\n return result;\r\n }\r\n\r\n return { response, send, stop, idle, error, setResponse, lastCallId };\r\n};\r\n\r\nexport default useLLM;\r\n","import React, { createContext, ReactNode } from \"react\";\r\n\r\nexport type LLMAsAServiceCustomer = {\r\n customer_id: string;\r\n customer_name?: string;\r\n customer_user_id?: string;\r\n customer_user_email?: string;\r\n};\r\n\r\nexport interface LLMServiceType {\r\n project_id: string | undefined;\r\n customer?: LLMAsAServiceCustomer;\r\n url?: string | null;\r\n agent?: string | null;\r\n}\r\n\r\nexport const LLMService = createContext<LLMServiceType | undefined>(undefined);\r\n\r\ninterface UserProviderProps {\r\n children: ReactNode;\r\n project_id: string | undefined;\r\n customer?: LLMAsAServiceCustomer;\r\n url?: string | null;\r\n agent?: string | null;\r\n}\r\n\r\nexport const LLMServiceProvider: React.FC<UserProviderProps> = ({\r\n children,\r\n project_id,\r\n customer,\r\n url = \"https://chat.llmasaservice.io/\",\r\n agent = null,\r\n}) => {\r\n return (\r\n <LLMService.Provider value={{ project_id, customer, url, agent }}>\r\n {children}\r\n </LLMService.Provider>\r\n );\r\n};\r\n\r\nexport default LLMServiceProvider;"],"mappings":";;;;;;;;;;;;;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e,WACb,IACA,IAKiB;AAAA,+CANjB,QACA,SACA,SAAkB,MAClB,EAAE,OAAe,GACjB,YACA,SACiB;AACjB,UAAI,cAAc;AAClB,UAAI,SAAS;AAEb,aAAO,MAAM;AACX,YAAI;AAEF,cAAI,OAAO,SAAS;AAClB,mBAAO,OAAO;AACd,oBAAQ,IAAI;AACZ;AAAA,UACF;AAGA,gBAAM,EAAE,OAAO,KAAK,IAAI,MAAM,OAAO,KAAK;AAE1C,cAAI,QAAQ,OAAO,KAAK,EAAE,WAAW,QAAQ,GAAG;AAC9C,0BAAc,QAAQ,OAAO,KAAK,EAAE,UAAU,CAAC;AAC/C;AAAA,UACF;AAGA,cAAI,MAAM;AACR,oBAAQ,IAAI;AACZ;AAAA,UACF;AAGA,oBAAU,QAAQ,OAAO,KAAK;AAC9B,cAAI,OAAQ,aAAY,CAAC,cAAmB,MAAM;AAAA,QACpD,SAASE,QAAY;AACnB,cAAIA,OAAM,SAAS,cAAc;AAC/B;AAAA,UACF;AAEA,wBAAc,kBAAkBA,OAAM,OAAO;AAC7C;AAAA,QACF,UAAE;AACA,cAAI,OAAO,SAAS;AAClB,mBAAO,YAAY;AAAA,UACrB;AAAA,QACF;AAAA,MACF;AAEA,UAAI,gBAAgB,IAAI;AACtB,iBAAS,WAAW;AACpB,eAAO,OAAO;AACd,YAAI,QAAS,SAAQ,WAAW;AAChC,gBAAQ,IAAI;AAAA,MACd;AAEA,UAAI,YAAY;AACd,mBAAW,MAAM;AAAA,MACnB;AAEA,aAAO;AAAA,IACT;AAAA;AAEA,SAAO,EAAE,UAAU,MAAM,MAAM,MAAM,OAAO,aAAa,WAAW;AACtE;","names":["import_react","React","options","response","error"]}
package/dist/index.mjs CHANGED
@@ -181,3 +181,4 @@ export {
181
181
  LLMServiceProvider,
182
182
  useLLM
183
183
  };
184
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/useLLM.ts","../src/LLMAsAService.tsx"],"sourcesContent":["import { useContext, useState } from \"react\";\r\nimport { LLMService, LLMServiceType } from \"./LLMAsAService\";\r\n\r\nexport interface Message {\r\n role: string;\r\n content: string;\r\n}\r\n\r\nexport interface DataItem {\r\n key: string;\r\n data: string;\r\n}\r\n\r\nexport interface UseLLMReturnType {\r\n send: (\r\n prompt: string,\r\n messages?: Message[],\r\n data?: DataItem[],\r\n stream?: boolean,\r\n allowCaching?: boolean,\r\n service?: string | null,\r\n conversation?: string | null,\r\n abortController?: AbortController,\r\n onComplete?: (result: string) => void,\r\n onError?: (error: string) => void\r\n ) => Promise<ReadableStreamDefaultReader<any> | string | undefined>;\r\n stop: (controller: AbortController | null) => void;\r\n response: string;\r\n idle: boolean;\r\n error: string;\r\n setResponse: (response: string) => void;\r\n lastCallId: string;\r\n}\r\n\r\nexport const useLLM = (options?: LLMServiceType): UseLLMReturnType => {\r\n const [response, setResponse] = useState<string>(\"\");\r\n const [idle, setIdle] = useState<boolean>(true);\r\n const [error, setError] = useState<string>(\"\");\r\n const [lastCallId, setLastCallId] = useState<string>(\"\");\r\n\r\n let context = useContext(LLMService);\r\n if (!context) {\r\n context = options;\r\n }\r\n\r\n if (!context) {\r\n throw new Error(\r\n \"useLLM must be used within a LLMServiceProvider or constructed with options in your useLLM() call.\"\r\n );\r\n }\r\n\r\n /**\r\n * Stops the fetch request and returns the hook to an idle state. Use this to add abort functionality to your UI.\r\n *\r\n * @param controller An AbortController object to stop the fetch request and return this hook to an idle state, the controller should be the same one passed to the send function.\r\n */\r\n const stop = (controller: AbortController | null) => {\r\n if (controller) controller.abort();\r\n setIdle(true);\r\n };\r\n\r\n /**\r\n * Calls the LLM as a service with the given prompt and messages. The response is returned in the response property of the hook.\r\n *\r\n * @param {string} prompt - The prompt to send to the LLM service.\r\n * @param {Message[]} messages - The history and context messages to send to the LLM service, as an array of {role: string, content: string} objects. For example, [{ role: \"system\", content: \"You are a useful assistant.\" }]\r\n * @param {DataItem[]} data - The data to send to the LLM service, as an array of {key: string, data: string} objects. For example, [{ key: \"name\", value: \"John\" }]\r\n * @param {boolean} stream - Determines whether to stream results back in the response property as they return from the service or batch them up and return them all at once in the response property as a string.\r\n * @param {boolean} allowCaching - Determines whether the service can use cached results or not.\r\n * @param {string | null} service - The service to use for the request. If null, load balancing will be applied. This is typically only used for testing.\r\n * @param {string | null} conversation - The conversation of this request. If null, this is a one off call with no conversation history\r\n * @param {AbortController} abortController - The AbortController used to abort this request once it's started. This allows you to add a stop button to your UI.\r\n * @param {(result: string) => void} onComplete - The callback function to be called once the stream completes, with the final result string.\r\n * @param {(error: string) => void} onError - The callback function to be called if an error occurs, with the error string.\r\n * @returns {Promise<ReadableStreamDefaultReader<any> | string | undefined>} - A StreamReader object if stream is true, otherwise a string of the response. Typically this isn't used when streaming, the stream is exposed in the response property.\r\n */\r\n async function send(\r\n prompt: string,\r\n messages: Message[] = [],\r\n data: DataItem[] = [],\r\n stream: boolean = true,\r\n allowCaching: boolean = true,\r\n service: string | null = null, // null means use the default service and apply services load balancing\r\n conversation: string | null = null,\r\n abortController: AbortController = new AbortController(),\r\n onComplete?: (result: string) => void,\r\n onError?: (error: string) => void\r\n ): Promise<ReadableStreamDefaultReader<any> | string | undefined> {\r\n setResponse(\"\");\r\n setIdle(false);\r\n\r\n let errorInFetch = \"\";\r\n\r\n const responseBody = JSON.stringify({\r\n projectId: context?.project_id ?? \"\",\r\n serviceId: service,\r\n agentId: context?.agent,\r\n prompt: prompt,\r\n messages: messages,\r\n data: data,\r\n customer: context?.customer ?? {}, // if no customer, use the projectId as the customer_id\r\n allowCaching: allowCaching,\r\n conversationId: conversation,\r\n });\r\n\r\n // trying to get cloudfront oac going. posts need to be signed, but when i add this the call fails...\r\n const options = {\r\n method: \"POST\",\r\n signal: abortController.signal,\r\n mode: \"cors\" as RequestMode,\r\n headers: {\r\n \"Content-Type\": \"text/plain\",\r\n },\r\n body: responseBody,\r\n };\r\n\r\n try {\r\n const url = context?.url ?? \"https://chat.llmasaservice.io/\";\r\n const response = await fetch(url, options);\r\n if (!response.ok) {\r\n errorInFetch = `Error: Network error for service. (${response.status} ${response.statusText})`;\r\n } else {\r\n setLastCallId(response.headers.get(\"x-callId\") ?? \"\");\r\n const reader =\r\n response?.body?.getReader() as ReadableStreamDefaultReader;\r\n const decoder = new TextDecoder(\"utf-8\");\r\n setIdle(false);\r\n\r\n if (!stream) {\r\n return await readStream(\r\n reader,\r\n decoder,\r\n stream,\r\n {\r\n signal: options.signal,\r\n },\r\n onComplete,\r\n onError\r\n );\r\n } else {\r\n readStream(\r\n reader,\r\n decoder,\r\n stream,\r\n {\r\n signal: options.signal,\r\n },\r\n onComplete,\r\n onError\r\n );\r\n\r\n return reader;\r\n }\r\n }\r\n } catch (errorObject: any) {\r\n errorInFetch = `Error: Having trouble connecting to chat service. (${errorObject.message})`;\r\n }\r\n\r\n if (errorInFetch !== \"\") {\r\n setError(errorInFetch);\r\n if (onError) {\r\n onError(errorInFetch);\r\n }\r\n console.error(`Error: Error in fetch. (${errorInFetch})`);\r\n }\r\n }\r\n\r\n async function readStream(\r\n reader: ReadableStreamDefaultReader,\r\n decoder: TextDecoder,\r\n stream: Boolean = true,\r\n { signal: signal }: { signal: AbortSignal },\r\n onComplete?: (result: string) => void,\r\n onError?: (error: string) => void\r\n ): Promise<string> {\r\n let errorInRead = \"\";\r\n let result = \"\";\r\n\r\n while (true) {\r\n try {\r\n // Check if the stream has been aborted\r\n if (signal.aborted) {\r\n reader.cancel();\r\n setIdle(true);\r\n break;\r\n }\r\n\r\n // Read a chunk of data from the stream\r\n const { value, done } = await reader.read();\r\n\r\n if (decoder.decode(value).startsWith(\"Error:\")) {\r\n errorInRead = decoder.decode(value).substring(6);\r\n break;\r\n }\r\n\r\n // If the stream has been read to the end, exit the loop\r\n if (done) {\r\n setIdle(true);\r\n break;\r\n }\r\n\r\n // Process the chunk of data\r\n result += decoder.decode(value);\r\n if (stream) setResponse((prevState: any) => result);\r\n } catch (error: any) {\r\n if (error.name === \"AbortError\") {\r\n break;\r\n }\r\n\r\n errorInRead = `Reading error ${error.message}`;\r\n break;\r\n } finally {\r\n if (signal.aborted) {\r\n reader.releaseLock();\r\n }\r\n }\r\n }\r\n\r\n if (errorInRead !== \"\") {\r\n setError(errorInRead);\r\n reader.cancel();\r\n if (onError) onError(errorInRead);\r\n setIdle(true);\r\n }\r\n\r\n if (onComplete) {\r\n onComplete(result);\r\n }\r\n\r\n return result;\r\n }\r\n\r\n return { response, send, stop, idle, error, setResponse, lastCallId };\r\n};\r\n\r\nexport default useLLM;\r\n","import React, { createContext, ReactNode } from \"react\";\r\n\r\nexport type LLMAsAServiceCustomer = {\r\n customer_id: string;\r\n customer_name?: string;\r\n customer_user_id?: string;\r\n customer_user_email?: string;\r\n};\r\n\r\nexport interface LLMServiceType {\r\n project_id: string | undefined;\r\n customer?: LLMAsAServiceCustomer;\r\n url?: string | null;\r\n agent?: string | null;\r\n}\r\n\r\nexport const LLMService = createContext<LLMServiceType | undefined>(undefined);\r\n\r\ninterface UserProviderProps {\r\n children: ReactNode;\r\n project_id: string | undefined;\r\n customer?: LLMAsAServiceCustomer;\r\n url?: string | null;\r\n agent?: string | null;\r\n}\r\n\r\nexport const LLMServiceProvider: React.FC<UserProviderProps> = ({\r\n children,\r\n project_id,\r\n customer,\r\n url = \"https://chat.llmasaservice.io/\",\r\n agent = null,\r\n}) => {\r\n return (\r\n <LLMService.Provider value={{ project_id, customer, url, agent }}>\r\n {children}\r\n </LLMService.Provider>\r\n );\r\n};\r\n\r\nexport default LLMServiceProvider;"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,YAAY,gBAAgB;;;ACArC,OAAO,SAAS,qBAAgC;AAgBzC,IAAM,aAAa,cAA0C,MAAS;AAUtE,IAAM,qBAAkD,CAAC;AAAA,EAC9D;AAAA,EACA;AAAA,EACA;AAAA,EACA,MAAM;AAAA,EACN,QAAQ;AACV,MAAM;AACJ,SACE,oCAAC,WAAW,UAAX,EAAoB,OAAO,EAAE,YAAY,UAAU,KAAK,MAA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,OAAO,KAAK,EAAE,UAAU,CAAC;AAC/C;AAAA,UACF;AAGA,cAAI,MAAM;AACR,oBAAQ,IAAI;AACZ;AAAA,UACF;AAGA,oBAAU,QAAQ,OAAO,KAAK;AAC9B,cAAI,OAAQ,aAAY,CAAC,cAAmB,MAAM;AAAA,QACpD,SAASE,QAAY;AACnB,cAAIA,OAAM,SAAS,cAAc;AAC/B;AAAA,UACF;AAEA,wBAAc,kBAAkBA,OAAM,OAAO;AAC7C;AAAA,QACF,UAAE;AACA,cAAI,OAAO,SAAS;AAClB,mBAAO,YAAY;AAAA,UACrB;AAAA,QACF;AAAA,MACF;AAEA,UAAI,gBAAgB,IAAI;AACtB,iBAAS,WAAW;AACpB,eAAO,OAAO;AACd,YAAI,QAAS,SAAQ,WAAW;AAChC,gBAAQ,IAAI;AAAA,MACd;AAEA,UAAI,YAAY;AACd,mBAAW,MAAM;AAAA,MACnB;AAEA,aAAO;AAAA,IACT;AAAA;AAEA,SAAO,EAAE,UAAU,MAAM,MAAM,MAAM,OAAO,aAAa,WAAW;AACtE;","names":["options","response","error"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "llmasaservice-client",
3
3
  "license": "MIT",
4
- "version": "0.9.1",
4
+ "version": "0.9.2",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
@@ -9,7 +9,7 @@
9
9
  "dist"
10
10
  ],
11
11
  "scripts": {
12
- "build": "tsup index.ts --format cjs,esm --dts",
12
+ "build": "tsup index.ts --format cjs,esm --dts --sourcemap",
13
13
  "lint": "tsc"
14
14
  },
15
15
  "peerDependencies": {