llmasaservice-client
Version:
HOC and hook to use the LLMAsAService.io LLM load balancer and firewall
185 lines (182 loc) • 5.67 kB
JavaScript
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("");
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) {
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 };
};
export {
LLMService,
LLMServiceProvider,
useLLM
};
//# sourceMappingURL=index.mjs.map