UNPKG

llmasaservice-client

Version:

HOC and hook to use the LLMAsAService.io LLM load balancer and firewall

212 lines (209 loc) 6.75 kB
var __defProp = Object.defineProperty; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/useLLM.ts import { useContext, useState } from "react"; // src/LLMAsAService.tsx import React, { createContext } from "react"; var LLMService = createContext(void 0); var LLMServiceProvider = ({ children, project_id, customer, url = "https://chat.llmasaservice.io/", agent = null }) => { return /* @__PURE__ */ React.createElement(LLMService.Provider, { value: { project_id, customer, url, agent } }, children); }; // src/useLLM.ts var useLLM = (options) => { const [response, setResponse] = useState(""); const [idle, setIdle] = useState(true); const [error, setError] = useState(""); const [lastCallId, setLastCallId] = useState(""); const [customerOverride, setCustomerOverride] = useState(null); let context = useContext(LLMService); if (!context) { context = options; } if (!context) { throw new Error( "useLLM must be used within a LLMServiceProvider or constructed with options in your useLLM() call." ); } const stop = (controller) => { if (controller) controller.abort(); setIdle(true); }; function send(_0) { return __async(this, arguments, function* (prompt, messages = [], data = [], stream = true, allowCaching = true, service = null, conversation = null, abortController = new AbortController(), onComplete, onError, requestOptions) { var _a, _b, _c, _d, _e, _f; setResponse(""); setIdle(false); let errorInFetch = ""; const responseBody = JSON.stringify(__spreadValues(__spreadValues({ projectId: (_a = context == null ? void 0 : context.project_id) != null ? _a : "", serviceId: service, agentId: context == null ? void 0 : context.agent, prompt, messages, data, customer: (_b = customerOverride != null ? customerOverride : context == null ? void 0 : context.customer) != null ? _b : {}, // override customer if set allowCaching, conversationId: conversation, tools: (_c = context == null ? void 0 : context.tools) != null ? _c : [] }, (requestOptions == null ? void 0 : requestOptions.batch) != null && { batch: requestOptions.batch }), (requestOptions == null ? void 0 : requestOptions.callbackUrl) != null && { callbackUrl: requestOptions.callbackUrl })); const options2 = { method: "POST", signal: abortController.signal, mode: "cors", headers: { "Content-Type": "text/plain" }, body: responseBody }; try { const url = (_d = context == null ? void 0 : context.url) != null ? _d : "https://chat.llmasaservice.io/"; const response2 = yield fetch(url, options2); if (!response2.ok) { errorInFetch = `Error: Network error for service. (${response2.status} ${response2.statusText})`; } else { setLastCallId((_e = response2.headers.get("x-callId")) != null ? _e : ""); const reader = (_f = response2 == null ? void 0 : response2.body) == null ? void 0 : _f.getReader(); const decoder = new TextDecoder("utf-8"); setIdle(false); if (!stream) { return yield readStream( reader, decoder, stream, { signal: options2.signal }, onComplete, onError ); } else { readStream( reader, decoder, stream, { signal: options2.signal }, onComplete, onError ); return reader; } } } catch (errorObject) { errorInFetch = `Error: Having trouble connecting to chat service. (${errorObject.message})`; } if (errorInFetch !== "") { setError(errorInFetch); if (onError) { onError(errorInFetch); } console.error(`Error: Error in fetch. (${errorInFetch})`); } }); } function readStream(_0, _1) { return __async(this, arguments, function* (reader, decoder, stream = true, { signal }, onComplete, onError) { let errorInRead = ""; let result = ""; while (true) { try { if (signal.aborted) { reader.cancel(); break; } const { value, done } = yield reader.read(); const decoded = decoder.decode(value); result += decoded; if (stream) { setResponse(result); } if (decoded.startsWith("Error:")) { errorInRead = decoded.substring(6); break; } if (done) { break; } } catch (error2) { if (error2.name !== "AbortError") { errorInRead = `Read error: ${error2.message}`; } break; } finally { if (signal.aborted) reader.releaseLock(); } } setIdle(true); if (errorInRead) { setError(errorInRead); reader.cancel(); if (onError) onError(errorInRead); } if (onComplete) { onComplete(result); } return result; }); } const setCustomer = (customer) => { setCustomerOverride(customer); }; return { response, send, stop, idle, error, setResponse, lastCallId, setCustomer }; }; export { LLMService, LLMServiceProvider, useLLM }; //# sourceMappingURL=index.mjs.map