llmasaservice-client
Version:
HOC and hook to use the LLMAsAService.io LLM load balancer and firewall
223 lines (219 loc) • 7.49 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
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());
});
};
// index.ts
var useLLM_exports = {};
__export(useLLM_exports, {
LLMService: () => LLMService,
LLMServiceProvider: () => LLMServiceProvider,
useLLM: () => useLLM
});
module.exports = __toCommonJS(useLLM_exports);
// src/useLLM.ts
var import_react2 = require("react");
// src/LLMAsAService.tsx
var import_react = __toESM(require("react"));
var LLMService = (0, import_react.createContext)(void 0);
var LLMServiceProvider = ({
children,
project_id,
customer,
url = "https://chat.llmasaservice.io/",
agent = null
}) => {
return /* @__PURE__ */ import_react.default.createElement(LLMService.Provider, { value: { project_id, customer, url, agent } }, children);
};
// src/useLLM.ts
var useLLM = (options) => {
const [response, setResponse] = (0, import_react2.useState)("");
const [idle, setIdle] = (0, import_react2.useState)(true);
const [error, setError] = (0, import_react2.useState)("");
const [lastCallId, setLastCallId] = (0, import_react2.useState)("");
let context = (0, import_react2.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) {
var _a, _b, _c, _d, _e, _f;
setResponse("");
setIdle(false);
let errorInFetch = "";
const responseBody = JSON.stringify({
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 = context == null ? void 0 : context.customer) != null ? _b : {},
// if no customer, use the projectId as the customer_id
allowCaching,
conversationId: conversation,
tools: (_c = context == null ? void 0 : context.tools) != null ? _c : []
});
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();
setIdle(true);
break;
}
const { value, done } = yield reader.read();
if (decoder.decode(value).startsWith("Error:")) {
errorInRead = decoder.decode(value).substring(6);
break;
}
if (done) {
setIdle(true);
break;
}
result += decoder.decode(value);
if (stream) setResponse((prevState) => result);
} catch (error2) {
if (error2.name === "AbortError") {
break;
}
errorInRead = `Reading error ${error2.message}`;
break;
} finally {
if (signal.aborted) {
reader.releaseLock();
}
}
}
if (errorInRead !== "") {
setError(errorInRead);
reader.cancel();
if (onError) onError(errorInRead);
setIdle(true);
}
if (onComplete) {
onComplete(result);
}
return result;
});
}
return { response, send, stop, idle, error, setResponse, lastCallId };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
LLMService,
LLMServiceProvider,
useLLM
});
//# sourceMappingURL=index.js.map