llmasaservice-client 0.9.4 → 0.9.6

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.js CHANGED
@@ -177,15 +177,19 @@ var useLLM = (options) => {
177
177
  break;
178
178
  }
179
179
  const { value, done } = yield reader.read();
180
- if (decoder.decode(value).startsWith("Error:")) {
181
- errorInRead = decoder.decode(value).substring(6);
180
+ const decoded = decoder.decode(value);
181
+ result += decoded;
182
+ if (stream) {
183
+ console.log("streaming set response", result);
184
+ setResponse(result);
185
+ }
186
+ if (decoded.startsWith("Error:")) {
187
+ errorInRead = decoded.substring(6);
182
188
  break;
183
189
  }
184
190
  if (done) {
185
191
  break;
186
192
  }
187
- result += decoder.decode(value);
188
- if (stream) setResponse(result);
189
193
  } catch (error2) {
190
194
  if (error2.name !== "AbortError") {
191
195
  errorInRead = `Reading error ${error2.message}`;
package/dist/index.js.map CHANGED
@@ -1 +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 tools: context?.tools ?? [],\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 async function readStream(\r\n reader: ReadableStreamDefaultReader,\r\n decoder: TextDecoder,\r\n stream: Boolean = true,\r\n { 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 if (signal.aborted) {\r\n reader.cancel();\r\n break;\r\n }\r\n\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 (done) {\r\n break;\r\n }\r\n\r\n result += decoder.decode(value);\r\n if (stream) setResponse(result);\r\n } catch (error: any) {\r\n if (error.name !== \"AbortError\") {\r\n errorInRead = `Reading error ${error.message}`;\r\n }\r\n break;\r\n } finally {\r\n if (signal.aborted) reader.releaseLock();\r\n }\r\n }\r\n\r\n // once the loop is fully done, flip idle = true exactly once\r\n setIdle(true);\r\n\r\n if (errorInRead) {\r\n setError(errorInRead);\r\n reader.cancel();\r\n if (onError) onError(errorInRead);\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 tools?: [] | 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 tools?: [] | 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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,gBAAqC;;;ACArC,mBAAgD;AAiBzC,IAAM,iBAAa,4BAA0C,MAAS;AAWtE,IAAM,qBAAkD,CAAC;AAAA,EAC9D;AAAA,EACA;AAAA,EACA;AAAA,EACA,MAAM;AAAA,EACN,QAAQ;AACV,MAAM;AACJ,SACE,6BAAAC,QAAA,cAAC,WAAW,UAAX,EAAoB,OAAO,EAAE,YAAY,UAAU,KAAK,MAAM,KAC5D,QACH;AAEJ;;;ADNO,IAAM,SAAS,CAAC,YAA+C;AACpE,QAAM,CAAC,UAAU,WAAW,QAAI,wBAAiB,EAAE;AACnD,QAAM,CAAC,MAAM,OAAO,QAAI,wBAAkB,IAAI;AAC9C,QAAM,CAAC,OAAO,QAAQ,QAAI,wBAAiB,EAAE;AAC7C,QAAM,CAAC,YAAY,aAAa,QAAI,wBAAiB,EAAE;AAEvD,MAAI,cAAU,0BAAW,UAAU;AACnC,MAAI,CAAC,SAAS;AACZ,cAAU;AAAA,EACZ;AAEA,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAOA,QAAM,OAAO,CAAC,eAAuC;AACnD,QAAI,WAAY,YAAW,MAAM;AACjC,YAAQ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AAY;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"]}
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 tools: context?.tools ?? [],\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 async function readStream(\r\n reader: ReadableStreamDefaultReader,\r\n decoder: TextDecoder,\r\n stream: Boolean = true,\r\n { 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 if (signal.aborted) {\r\n reader.cancel();\r\n break;\r\n }\r\n\r\n const { value, done } = await reader.read();\r\n\r\n const decoded = decoder.decode(value);\r\n result += decoded;\r\n\r\n if (stream) {\r\n console.log(\"streaming set response\", result);\r\n setResponse(result);\r\n }\r\n\r\n if (decoded.startsWith(\"Error:\")) {\r\n errorInRead = decoded.substring(6);\r\n break;\r\n }\r\n\r\n if (done) {\r\n break;\r\n }\r\n } catch (error: any) {\r\n if (error.name !== \"AbortError\") {\r\n errorInRead = `Reading error ${error.message}`;\r\n }\r\n break;\r\n } finally {\r\n if (signal.aborted) reader.releaseLock();\r\n }\r\n }\r\n\r\n // once the loop is fully done, flip idle = true exactly once\r\n setIdle(true);\r\n\r\n if (errorInRead) {\r\n setError(errorInRead);\r\n reader.cancel();\r\n if (onError) onError(errorInRead);\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 tools?: [] | 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 tools?: [] | 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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1D;AAAA,IACF;AAAA;AACA,WAAe,WACb,IACA,IAKiB;AAAA,+CANjB,QACA,SACA,SAAkB,MAClB,EAAE,OAAO,GACT,YACA,SACiB;AACjB,UAAI,cAAc;AAClB,UAAI,SAAS;AAEb,aAAO,MAAM;AACX,YAAI;AACF,cAAI,OAAO,SAAS;AAClB,mBAAO,OAAO;AACd;AAAA,UACF;AAEA,gBAAM,EAAE,OAAO,KAAK,IAAI,MAAM,OAAO,KAAK;AAE1C,gBAAM,UAAU,QAAQ,OAAO,KAAK;AACpC,oBAAU;AAEV,cAAI,QAAQ;AACV,oBAAQ,IAAI,0BAA0B,MAAM;AAC5C,wBAAY,MAAM;AAAA,UACpB;AAEA,cAAI,QAAQ,WAAW,QAAQ,GAAG;AAChC,0BAAc,QAAQ,UAAU,CAAC;AACjC;AAAA,UACF;AAEA,cAAI,MAAM;AACR;AAAA,UACF;AAAA,QACF,SAASE,QAAY;AACnB,cAAIA,OAAM,SAAS,cAAc;AAC/B,0BAAc,kBAAkBA,OAAM,OAAO;AAAA,UAC/C;AACA;AAAA,QACF,UAAE;AACA,cAAI,OAAO,QAAS,QAAO,YAAY;AAAA,QACzC;AAAA,MACF;AAGA,cAAQ,IAAI;AAEZ,UAAI,aAAa;AACf,iBAAS,WAAW;AACpB,eAAO,OAAO;AACd,YAAI,QAAS,SAAQ,WAAW;AAAA,MAClC;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
@@ -140,15 +140,19 @@ var useLLM = (options) => {
140
140
  break;
141
141
  }
142
142
  const { value, done } = yield reader.read();
143
- if (decoder.decode(value).startsWith("Error:")) {
144
- errorInRead = decoder.decode(value).substring(6);
143
+ const decoded = decoder.decode(value);
144
+ result += decoded;
145
+ if (stream) {
146
+ console.log("streaming set response", result);
147
+ setResponse(result);
148
+ }
149
+ if (decoded.startsWith("Error:")) {
150
+ errorInRead = decoded.substring(6);
145
151
  break;
146
152
  }
147
153
  if (done) {
148
154
  break;
149
155
  }
150
- result += decoder.decode(value);
151
- if (stream) setResponse(result);
152
156
  } catch (error2) {
153
157
  if (error2.name !== "AbortError") {
154
158
  errorInRead = `Reading error ${error2.message}`;
@@ -1 +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 tools: context?.tools ?? [],\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 async function readStream(\r\n reader: ReadableStreamDefaultReader,\r\n decoder: TextDecoder,\r\n stream: Boolean = true,\r\n { 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 if (signal.aborted) {\r\n reader.cancel();\r\n break;\r\n }\r\n\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 (done) {\r\n break;\r\n }\r\n\r\n result += decoder.decode(value);\r\n if (stream) setResponse(result);\r\n } catch (error: any) {\r\n if (error.name !== \"AbortError\") {\r\n errorInRead = `Reading error ${error.message}`;\r\n }\r\n break;\r\n } finally {\r\n if (signal.aborted) reader.releaseLock();\r\n }\r\n }\r\n\r\n // once the loop is fully done, flip idle = true exactly once\r\n setIdle(true);\r\n\r\n if (errorInRead) {\r\n setError(errorInRead);\r\n reader.cancel();\r\n if (onError) onError(errorInRead);\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 tools?: [] | 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 tools?: [] | 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,g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ACT,YACA,SACiB;AACjB,UAAI,cAAc;AAClB,UAAI,SAAS;AAEb,aAAO,MAAM;AACX,YAAI;AACF,cAAI,OAAO,SAAS;AAClB,mBAAO,OAAO;AACd;AAAA,UACF;AAEA,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;AAEA,cAAI,MAAM;AACR;AAAA,UACF;AAEA,oBAAU,QAAQ,OAAO,KAAK;AAC9B,cAAI,OAAQ,aAAY,MAAM;AAAA,QAChC,SAASE,QAAY;AACnB,cAAIA,OAAM,SAAS,cAAc;AAC/B,0BAAc,kBAAkBA,OAAM,OAAO;AAAA,UAC/C;AACA;AAAA,QACF,UAAE;AACA,cAAI,OAAO,QAAS,QAAO,YAAY;AAAA,QACzC;AAAA,MACF;AAGA,cAAQ,IAAI;AAEZ,UAAI,aAAa;AACf,iBAAS,WAAW;AACpB,eAAO,OAAO;AACd,YAAI,QAAS,SAAQ,WAAW;AAAA,MAClC;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"]}
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 tools: context?.tools ?? [],\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 async function readStream(\r\n reader: ReadableStreamDefaultReader,\r\n decoder: TextDecoder,\r\n stream: Boolean = true,\r\n { 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 if (signal.aborted) {\r\n reader.cancel();\r\n break;\r\n }\r\n\r\n const { value, done } = await reader.read();\r\n\r\n const decoded = decoder.decode(value);\r\n result += decoded;\r\n\r\n if (stream) {\r\n console.log(\"streaming set response\", result);\r\n setResponse(result);\r\n }\r\n\r\n if (decoded.startsWith(\"Error:\")) {\r\n errorInRead = decoded.substring(6);\r\n break;\r\n }\r\n\r\n if (done) {\r\n break;\r\n }\r\n } catch (error: any) {\r\n if (error.name !== \"AbortError\") {\r\n errorInRead = `Reading error ${error.message}`;\r\n }\r\n break;\r\n } finally {\r\n if (signal.aborted) reader.releaseLock();\r\n }\r\n }\r\n\r\n // once the loop is fully done, flip idle = true exactly once\r\n setIdle(true);\r\n\r\n if (errorInRead) {\r\n setError(errorInRead);\r\n reader.cancel();\r\n if (onError) onError(errorInRead);\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 tools?: [] | 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 tools?: [] | 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;AAiBzC,IAAM,aAAa,cAA0C,MAAS;AAWtE,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,MAAM,KAC5D,QACH;AAEJ;;;ADNO,IAAM,SAAS,CAAC,YAA+C;AACpE,QAAM,CAAC,UAAU,WAAW,IAAI,SAAiB,EAAE;AACnD,QAAM,CAAC,MAAM,OAAO,IAAI,SAAkB,IAAI;AAC9C,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAiB,EAAE;AAC7C,QAAM,CAAC,YAAY,aAAa,IAAI,SAAiB,EAAE;AAEvD,MAAI,UAAU,WAAW,UAAU;AACnC,MAAI,CAAC,SAAS;AACZ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A,QACF,UAAE;AACA,cAAI,OAAO,QAAS,QAAO,YAAY;AAAA,QACzC;AAAA,MACF;AAGA,cAAQ,IAAI;AAEZ,UAAI,aAAa;AACf,iBAAS,WAAW;AACpB,eAAO,OAAO;AACd,YAAI,QAAS,SAAQ,WAAW;AAAA,MAClC;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.4",
4
+ "version": "0.9.6",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",