UNPKG

react-chat-ai-widget

Version:

Enhance user experience with a smart widget that combines AI assistants and live chat. Automate support, stay available 24/7, and boost customer engagement effortlessly.

1,411 lines 434 kB
var Tn = Object.defineProperty; var En = (e, t, n) => t in e ? Tn(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n; var p = (e, t, n) => En(e, typeof t != "symbol" ? t + "" : t, n); import { jsxs as D, jsx as h, Fragment as Cn } from "react/jsx-runtime"; import we, { useState as Ee, useEffect as ue, useRef as Ce, useCallback as Oe, useMemo as re } from "react"; import { u as ee, g as Rn } from "./main-D7UtAX1E.js"; const Me = { User: "user", Assistant: "assistant" }, Kt = "Hi, I'm an AI assistant. How can I help you today? ", Ge = (e, t, n, a = null) => ({ id: crypto.randomUUID(), role: e, content: t, error: a ?? null, isLoading: n ?? !1 }), Pn = () => Ge(Me.Assistant, Kt), $n = (e) => typeof e == "string" && e.trim() !== "", Un = () => { const [e, t] = Ee([Pn()]); ue(() => { localStorage.setItem("chat-messages", JSON.stringify(e)); }, [e]); const n = we.useCallback( async (i) => { if (!$n(i)) return; const r = Ge(Me.User, i); return t((o) => [...o, r]), r; }, [] ), a = we.useCallback( (i = "", r) => { const o = (r == null ? void 0 : r.isLoading) ?? !1, s = (r == null ? void 0 : r.error) ?? null, c = (r == null ? void 0 : r.errorType) ?? "general"; t((l) => { const g = l[l.length - 1]; return g.content === Kt ? [...l] : !g || g.role !== Me.Assistant ? [ ...l, Ge(Me.Assistant, i, o, s) ] : [ ...l.slice(0, -1), { ...g, content: g.content + i, isLoading: o, error: s, errorType: c } ]; }); }, [] ); return we.useMemo( () => ({ messages: e, // Return a copy of the messages array addMessageUser: n, addMessageAssistant: a }), [e, n, a] ); }; class Wt { constructor(t, n, a, i, r, o) { p(this, "apiKey"); p(this, "model"); p(this, "frecuency_penalty"); p(this, "temperature"); p(this, "max_tokens"); p(this, "timeout"); this.apiKey = t, this.model = n, this.frecuency_penalty = 0.5, this.temperature = i, this.max_tokens = r, this.timeout = o, this.frecuency_penalty = a; } static builder() { return new Nn(); } } class Nn { constructor() { p(this, "apiKey", ""); p(this, "model", ""); p(this, "temperature", 0.7); p(this, "max_tokens", 20); p(this, "frequency_penalty", 0.5); p(this, "timeout", 3e4); } setApiKey(t) { return this.apiKey = t, this; } setFrequencyPenalty(t) { return this.frequency_penalty = t, this; } setModel(t) { return this.model = t, this; } setTemperature(t) { return this.temperature = t, this; } setMaxTokens(t) { return this.max_tokens = t, this; } setTimeout(t) { return this.timeout = t, this; } build() { if (!this.apiKey) throw new Error("API key is required"); return new Wt( this.apiKey, this.model, this.frequency_penalty, this.temperature, this.max_tokens, this.timeout ); } } function jn(e) { return typeof (e == null ? void 0 : e.model) == "string" && typeof (e == null ? void 0 : e.temperature) == "number" && typeof (e == null ? void 0 : e.max_tokens) == "number" && typeof (e == null ? void 0 : e.timeout) == "number" && typeof (e == null ? void 0 : e.frequency_penalty) == "number"; } const at = "https://huggingface.co", zt = "https://router.huggingface.co", Dn = "X-HF-Bill-To", _t = { /** * "HF model ID" => "Model ID on Inference Provider's side" * * Example: * "Qwen/Qwen2.5-Coder-32B-Instruct": "Qwen2.5-Coder-32B-Instruct", */ "black-forest-labs": {}, cerebras: {}, cohere: {}, "fal-ai": {}, "featherless-ai": {}, "fireworks-ai": {}, groq: {}, "hf-inference": {}, hyperbolic: {}, nebius: {}, novita: {}, nscale: {}, openai: {}, ovhcloud: {}, replicate: {}, sambanova: {}, together: {} }; class it extends Error { constructor(t) { super(t), this.name = "InferenceClientError"; } } class F extends it { constructor(t) { super(t), this.name = "InputError"; } } class Qt extends it { constructor(n, a, i) { super(n); p(this, "httpRequest"); p(this, "httpResponse"); this.httpRequest = { ...a, ...a.headers ? { headers: { ...a.headers, ..."Authorization" in a.headers ? { Authorization: "Bearer [redacted]" } : void 0 /// redact authentication in the request headers } } : void 0 }, this.httpResponse = i; } } class K extends Qt { constructor(t, n, a) { super(t, n, a), this.name = "ProviderApiError"; } } class Se extends Qt { constructor(t, n, a) { super(t, n, a), this.name = "HubApiError"; } } class w extends it { constructor(t) { super(t), this.name = "ProviderOutputError"; } } function Xt(e) { return Array.isArray(e) ? e : [e]; } class X { constructor(t, n, a = !1) { p(this, "provider"); p(this, "baseUrl"); p(this, "clientSideRoutingOnly"); this.provider = t, this.baseUrl = n, this.clientSideRoutingOnly = a; } /** * Prepare the base URL for the request */ makeBaseUrl(t) { return t.authMethod !== "provider-key" ? `${zt}/${this.provider}` : this.baseUrl; } /** * Prepare the body for the request */ makeBody(t) { return "data" in t.args && t.args.data ? t.args.data : JSON.stringify(this.preparePayload(t)); } /** * Prepare the URL for the request */ makeUrl(t) { const n = this.makeBaseUrl(t), a = this.makeRoute(t).replace(/^\/+/, ""); return `${n}/${a}`; } /** * Prepare the headers for the request */ prepareHeaders(t, n) { const a = {}; return t.authMethod !== "none" && (a.Authorization = `Bearer ${t.accessToken}`), n || (a["Content-Type"] = "application/json"), a; } } class J extends X { constructor(t, n, a = !1) { super(t, n, a); } makeRoute() { return "v1/chat/completions"; } preparePayload(t) { return { ...t.args, model: t.model }; } async getResponse(t) { if (typeof t == "object" && Array.isArray(t == null ? void 0 : t.choices) && typeof (t == null ? void 0 : t.created) == "number" && typeof (t == null ? void 0 : t.id) == "string" && typeof (t == null ? void 0 : t.model) == "string" && /// Together.ai and Nebius do not output a system_fingerprint (t.system_fingerprint === void 0 || t.system_fingerprint === null || typeof t.system_fingerprint == "string") && typeof (t == null ? void 0 : t.usage) == "object") return t; throw new w("Expected ChatCompletionOutput"); } } class me extends X { constructor(t, n, a = !1) { super(t, n, a); } preparePayload(t) { return { ...t.args, model: t.model }; } makeRoute() { return "v1/completions"; } async getResponse(t) { const n = Xt(t); if (Array.isArray(n) && n.length > 0 && n.every((a) => typeof a == "object" && !!a && "generated_text" in a && typeof a.generated_text == "string")) return n[0]; throw new w("Expected Array<{generated_text: string}>"); } } function Z(e) { if (globalThis.Buffer) return globalThis.Buffer.from(e).toString("base64"); { const t = []; return e.forEach((n) => { t.push(String.fromCharCode(n)); }), globalThis.btoa(t.join("")); } } function Mn(e, t) { return Object.assign({}, ...t.map((n) => { if (e[n] !== void 0) return { [n]: e[n] }; })); } function et(e, t) { return e.includes(t); } function P(e, t) { const n = Array.isArray(t) ? t : [t], a = Object.keys(e).filter((i) => !et(n, i)); return Mn(e, a); } const xt = ["feature-extraction", "sentence-similarity"]; class $ extends X { constructor() { super("hf-inference", `${zt}/hf-inference`); } preparePayload(t) { return t.args; } makeUrl(t) { return t.model.startsWith("http://") || t.model.startsWith("https://") ? t.model : super.makeUrl(t); } makeRoute(t) { return t.task && ["feature-extraction", "sentence-similarity"].includes(t.task) ? `models/${t.model}/pipeline/${t.task}` : `models/${t.model}`; } async getResponse(t) { return t; } } class Ln extends $ { async getResponse(t, n, a, i) { if (!t) throw new w("Received malformed response from HF-Inference text-to-image API: response is undefined"); if (typeof t == "object") { if (i === "json") return { ...t }; if ("data" in t && Array.isArray(t.data) && t.data[0].b64_json) { const r = t.data[0].b64_json; return i === "url" ? `data:image/jpeg;base64,${r}` : await (await fetch(`data:image/jpeg;base64,${r}`)).blob(); } if ("output" in t && Array.isArray(t.output)) return i === "url" ? t.output[0] : await (await fetch(t.output[0])).blob(); } if (t instanceof Blob) { if (i === "url" || i === "json") { const r = await t.arrayBuffer().then((o) => Buffer.from(o).toString("base64")); return i === "url" ? `data:image/jpeg;base64,${r}` : { output: `data:image/jpeg;base64,${r}` }; } return t; } throw new w("Received malformed response from HF-Inference text-to-image API: expected a Blob"); } } class On extends $ { makeUrl(t) { let n; return t.model.startsWith("http://") || t.model.startsWith("https://") ? n = t.model.trim() : n = `${this.makeBaseUrl(t)}/models/${t.model}`, n = n.replace(/\/+$/, ""), n.endsWith("/v1") ? n += "/chat/completions" : n.endsWith("/chat/completions") || (n += "/v1/chat/completions"), n; } preparePayload(t) { return { ...t.args, model: t.model }; } async getResponse(t) { return t; } } class qn extends $ { async getResponse(t) { const n = Xt(t); if (Array.isArray(n) && n.every((a) => "generated_text" in a && typeof (a == null ? void 0 : a.generated_text) == "string")) return n == null ? void 0 : n[0]; throw new w("Received malformed response from HF-Inference text generation API: expected Array<{generated_text: string}>"); } } class Bn extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof n == "object" && n !== null && typeof n.label == "string" && typeof n.score == "number")) return t; throw new w("Received malformed response from HF-Inference audio-classification API: expected Array<{label: string, score: number}> but received different format"); } } class Fn extends $ { async getResponse(t) { return t; } async preparePayloadAsync(t) { return "data" in t ? t : { ...P(t, "inputs"), data: t.inputs }; } } class Hn extends $ { async getResponse(t) { if (!Array.isArray(t)) throw new w("Received malformed response from HF-Inference audio-to-audio API: expected Array"); if (!t.every((n) => typeof n == "object" && n && "label" in n && typeof n.label == "string" && "content-type" in n && typeof n["content-type"] == "string" && "blob" in n && typeof n.blob == "string")) throw new w("Received malformed response from HF-Inference audio-to-audio API: expected Array<{label: string, audio: Blob}>"); return t; } } class Vn extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof n == "object" && !!n && typeof (n == null ? void 0 : n.answer) == "string" && (typeof n.end == "number" || typeof n.end > "u") && (typeof n.score == "number" || typeof n.score > "u") && (typeof n.start == "number" || typeof n.start > "u"))) return t[0]; throw new w("Received malformed response from HF-Inference document-question-answering API: expected Array<{answer: string, end: number, score: number, start: number}>"); } } class Kn extends $ { async getResponse(t) { const n = (a, i, r = 0) => r > i ? !1 : a.every((o) => Array.isArray(o)) ? a.every((o) => n(o, i, r + 1)) : a.every((o) => typeof o == "number"); if (Array.isArray(t) && n(t, 3, 0)) return t; throw new w("Received malformed response from HF-Inference feature-extraction API: expected Array<number[][][] | number[][] | number[] | number>"); } } class Wn extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof n.label == "string" && typeof n.score == "number")) return t; throw new w("Received malformed response from HF-Inference image-classification API: expected Array<{label: string, score: number}>"); } } class zn extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof n.label == "string" && typeof n.mask == "string" && (n.score === void 0 || typeof n.score == "number"))) return t; throw new w("Received malformed response from HF-Inference image-segmentation API: expected Array<{label: string, mask: string, score: number}>"); } async preparePayloadAsync(t) { return { ...t, inputs: Z(new Uint8Array(t.inputs instanceof ArrayBuffer ? t.inputs : await t.inputs.arrayBuffer())) }; } } class Qn extends $ { async getResponse(t) { if (typeof (t == null ? void 0 : t.generated_text) != "string") throw new w("Received malformed response from HF-Inference image-to-text API: expected {generated_text: string}"); return t; } } class Xn extends $ { async preparePayloadAsync(t) { return t.parameters ? { ...t, inputs: Z(new Uint8Array(t.inputs instanceof ArrayBuffer ? t.inputs : await t.inputs.arrayBuffer())) } : { ...t, model: t.model, data: t.inputs }; } async getResponse(t) { if (t instanceof Blob) return t; throw new w("Received malformed response from HF-Inference image-to-image API: expected Blob"); } } class Jn extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof n.label == "string" && typeof n.score == "number" && typeof n.box.xmin == "number" && typeof n.box.ymin == "number" && typeof n.box.xmax == "number" && typeof n.box.ymax == "number")) return t; throw new w("Received malformed response from HF-Inference object-detection API: expected Array<{label: string, score: number, box: {xmin: number, ymin: number, xmax: number, ymax: number}}>"); } } class Yn extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof n.label == "string" && typeof n.score == "number")) return t; throw new w("Received malformed response from HF-Inference zero-shot-image-classification API: expected Array<{label: string, score: number}>"); } } class Zn extends $ { async getResponse(t) { const n = t == null ? void 0 : t[0]; if (Array.isArray(n) && n.every((a) => typeof (a == null ? void 0 : a.label) == "string" && typeof a.score == "number")) return n; throw new w("Received malformed response from HF-Inference text-classification API: expected Array<{label: string, score: number}>"); } } class Gn extends $ { async getResponse(t) { if (Array.isArray(t) ? t.every((n) => typeof n == "object" && !!n && typeof n.answer == "string" && typeof n.end == "number" && typeof n.score == "number" && typeof n.start == "number") : typeof t == "object" && t && typeof t.answer == "string" && typeof t.end == "number" && typeof t.score == "number" && typeof t.start == "number") return Array.isArray(t) ? t[0] : t; throw new w("Received malformed response from HF-Inference question-answering API: expected Array<{answer: string, end: number, score: number, start: number}>"); } } class ea extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof n.score == "number" && typeof n.sequence == "string" && typeof n.token == "number" && typeof n.token_str == "string")) return t; throw new w("Received malformed response from HF-Inference fill-mask API: expected Array<{score: number, sequence: string, token: number, token_str: string}>"); } } class rt extends $ { async getResponse(t) { if (typeof t == "object" && t !== null && "labels" in t && "scores" in t && Array.isArray(t.labels) && Array.isArray(t.scores) && t.labels.length === t.scores.length && t.labels.every((n) => typeof n == "string") && t.scores.every((n) => typeof n == "number")) { const n = t.scores; return t.labels.map((a, i) => ({ label: a, score: n[i] })); } if (Array.isArray(t) && t.every(rt.validateOutputElement)) return t; throw new w("Received malformed response from HF-Inference zero-shot-classification API: expected Array<{label: string, score: number}>"); } static validateOutputElement(t) { return typeof t == "object" && !!t && "label" in t && "score" in t && typeof t.label == "string" && typeof t.score == "number"; } } class ta extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof n == "number")) return t; throw new w("Received malformed response from HF-Inference sentence-similarity API: expected Array<number>"); } } class qe extends $ { static validate(t) { return typeof t == "object" && !!t && "aggregator" in t && typeof t.aggregator == "string" && "answer" in t && typeof t.answer == "string" && "cells" in t && Array.isArray(t.cells) && t.cells.every((n) => typeof n == "string") && "coordinates" in t && Array.isArray(t.coordinates) && t.coordinates.every((n) => Array.isArray(n) && n.every((a) => typeof a == "number")); } async getResponse(t) { if (Array.isArray(t) && Array.isArray(t) ? t.every((n) => qe.validate(n)) : qe.validate(t)) return Array.isArray(t) ? t[0] : t; throw new w("Received malformed response from HF-Inference table-question-answering API: expected {aggregator: string, answer: string, cells: string[], coordinates: number[][]}"); } } class na extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof n.end == "number" && typeof n.entity_group == "string" && typeof n.score == "number" && typeof n.start == "number" && typeof n.word == "string")) return t; throw new w("Received malformed response from HF-Inference token-classification API: expected Array<{end: number, entity_group: string, score: number, start: number, word: string}>"); } } class aa extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof (n == null ? void 0 : n.translation_text) == "string")) return (t == null ? void 0 : t.length) === 1 ? t == null ? void 0 : t[0] : t; throw new w("Received malformed response from HF-Inference translation API: expected Array<{translation_text: string}>"); } } class ia extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof (n == null ? void 0 : n.summary_text) == "string")) return t == null ? void 0 : t[0]; throw new w("Received malformed response from HF-Inference summarization API: expected Array<{summary_text: string}>"); } } class ra extends $ { async getResponse(t) { return t; } } class oa extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof n == "number")) return t; throw new w("Received malformed response from HF-Inference tabular-classification API: expected Array<number>"); } } class sa extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof n == "object" && !!n && typeof (n == null ? void 0 : n.answer) == "string" && typeof n.score == "number")) return t[0]; throw new w("Received malformed response from HF-Inference visual-question-answering API: expected Array<{answer: string, score: number}>"); } } class la extends $ { async getResponse(t) { if (Array.isArray(t) && t.every((n) => typeof n == "number")) return t; throw new w("Received malformed response from HF-Inference tabular-regression API: expected Array<number>"); } } class ca extends $ { async getResponse(t) { return t; } } let da = console; function Re() { return da; } const Xe = /* @__PURE__ */ new Map(); function pa(e, t) { return t ? Array.isArray(t) ? t : Object.entries(t).map(([n, a]) => ({ provider: n, hfModelId: e, providerId: a.providerId, status: a.status, task: a.task, adapter: a.adapter, adapterWeightsPath: a.adapterWeightsPath })) : []; } async function Jt(e, t, n) { var i; let a; if (Xe.has(e)) a = Xe.get(e); else { const r = `${at}/api/models/${e}?expand[]=inferenceProviderMapping`, o = await ((n == null ? void 0 : n.fetch) ?? fetch)(r, { headers: t != null && t.startsWith("hf_") ? { Authorization: `Bearer ${t}` } : {} }); if (!o.ok) if ((i = o.headers.get("Content-Type")) != null && i.startsWith("application/json")) { const c = await o.json(); if ("error" in c && typeof c.error == "string") throw new Se(`Failed to fetch inference provider mapping for model ${e}: ${c.error}`, { url: r, method: "GET" }, { requestId: o.headers.get("x-request-id") ?? "", status: o.status, body: c }); } else throw new Se(`Failed to fetch inference provider mapping for model ${e}`, { url: r, method: "GET" }, { requestId: o.headers.get("x-request-id") ?? "", status: o.status, body: await o.text() }); let s = null; try { s = await o.json(); } catch { throw new Se(`Failed to fetch inference provider mapping for model ${e}: malformed API response, invalid JSON`, { url: r, method: "GET" }, { requestId: o.headers.get("x-request-id") ?? "", status: o.status, body: await o.text() }); } if (!(s != null && s.inferenceProviderMapping)) throw new Se(`We have not been able to find inference provider information for model ${e}.`, { url: r, method: "GET" }, { requestId: o.headers.get("x-request-id") ?? "", status: o.status, body: await o.text() }); a = pa(e, s.inferenceProviderMapping), Xe.set(e, a); } return a; } async function ua(e, t) { const n = Re(); if (_t[e.provider][e.modelId]) return _t[e.provider][e.modelId]; const i = (await Jt(e.modelId, e.accessToken, t)).find((r) => r.provider === e.provider); if (i) { const r = e.provider === "hf-inference" && et(xt, e.task) ? xt : [e.task]; if (!et(r, i.task)) throw new F(`Model ${e.modelId} is not supported for task ${e.task} and provider ${e.provider}. Supported task: ${i.task}.`); return i.status === "staging" && n.warn(`Model ${e.modelId} is in staging mode for provider ${e.provider}. Meant for test purposes only.`), i; } return null; } async function T(e, t, n) { var i; const a = Re(); if (n) { if (e) throw new F("Specifying both endpointUrl and provider is not supported."); return "hf-inference"; } if (e || (a.log("Defaulting to 'auto' which will select the first provider available for the model, sorted by the user's order in https://hf.co/settings/inference-providers."), e = "auto"), e === "auto") { if (!t) throw new F("Specifying a model is required when provider is 'auto'"); e = (i = (await Jt(t))[0]) == null ? void 0 : i.provider, a.log("Auto selected provider:", e); } if (!e) throw new F(`No Inference Provider available for model ${t}.`); return e; } function ot(e) { return new Promise((t) => { setTimeout(() => t(), e); }); } const ma = "https://api.us1.bfl.ai"; class fa extends X { constructor() { super("black-forest-labs", ma); } preparePayload(t) { return { ...P(t.args, ["inputs", "parameters"]), ...t.args.parameters, prompt: t.args.inputs }; } prepareHeaders(t, n) { const a = { Authorization: t.authMethod !== "provider-key" ? `Bearer ${t.accessToken}` : `X-Key ${t.accessToken}` }; return n || (a["Content-Type"] = "application/json"), a; } makeRoute(t) { if (!t) throw new F("Params are required"); return `/v1/${t.model}`; } async getResponse(t, n, a, i) { const r = Re(), o = new URL(t.polling_url); for (let s = 0; s < 5; s++) { await ot(1e3), r.debug(`Polling Black Forest Labs API for the result... ${s + 1}/5`), o.searchParams.set("attempt", s.toString(10)); const c = await fetch(o, { headers: { "Content-Type": "application/json" } }); if (!c.ok) throw new K("Failed to fetch result from black forest labs API", { url: o.toString(), method: "GET", headers: { "Content-Type": "application/json" } }, { requestId: c.headers.get("x-request-id") ?? "", status: c.status, body: await c.text() }); const l = await c.json(); if (typeof l == "object" && l && "status" in l && typeof l.status == "string" && l.status === "Ready" && "result" in l && typeof l.result == "object" && l.result && "sample" in l.result && typeof l.result.sample == "string") return i === "json" ? l.result : i === "url" ? l.result.sample : await (await fetch(l.result.sample)).blob(); } throw new w("Timed out while waiting for the result from black forest labs API - aborting after 5 attempts"); } } class ha extends J { constructor() { super("cerebras", "https://api.cerebras.ai"); } } class ga extends J { constructor() { super("cohere", "https://api.cohere.com"); } makeRoute() { return "/compatibility/v1/chat/completions"; } } function fe(e) { return /^http(s?):/.test(e) || e.startsWith("/"); } const kt = ["audio/mpeg", "audio/mp4", "audio/wav", "audio/x-wav"]; class Be extends X { constructor(t) { super("fal-ai", t || "https://fal.run"); } preparePayload(t) { return t.args; } makeRoute(t) { return `/${t.model}`; } prepareHeaders(t, n) { const a = { Authorization: t.authMethod !== "provider-key" ? `Bearer ${t.accessToken}` : `Key ${t.accessToken}` }; return n || (a["Content-Type"] = "application/json"), a; } } class Fe extends Be { async getResponseFromQueueApi(t, n, a) { if (!n || !a) throw new F(`URL and headers are required for ${this.task} task`); if (!t.request_id) throw new w(`Received malformed response from Fal.ai ${this.task} API: no request ID found in the response`); let r = t.status; const o = new URL(n), s = `${o.protocol}//${o.host}${o.host === "router.huggingface.co" ? "/fal-ai" : ""}`, c = new URL(t.response_url).pathname, l = o.search, g = `${s}${c}/status${l}`, u = `${s}${c}${l}`; for (; r !== "COMPLETED"; ) { await ot(500); const j = await fetch(g, { headers: a }); if (!j.ok) throw new K("Failed to fetch response status from fal-ai API", { url: g, method: "GET" }, { requestId: j.headers.get("x-request-id") ?? "", status: j.status, body: await j.text() }); try { r = (await j.json()).status; } catch { throw new w("Failed to parse status response from fal-ai API: received malformed response"); } } const y = await fetch(u, { headers: a }); let A; try { A = await y.json(); } catch { throw new w("Failed to parse result response from fal-ai API: received malformed response"); } return A; } } function Yt(e, t) { return `${at}/${e}/resolve/main/${t}`; } class ya extends Be { preparePayload(t) { var a; const n = { ...P(t.args, ["inputs", "parameters"]), ...t.args.parameters, sync_mode: !0, prompt: t.args.inputs }; return ((a = t.mapping) == null ? void 0 : a.adapter) === "lora" && t.mapping.adapterWeightsPath && (n.loras = [ { path: Yt(t.mapping.hfModelId, t.mapping.adapterWeightsPath), scale: 1 } ], t.mapping.providerId === "fal-ai/lora" && (n.model_name = "stabilityai/stable-diffusion-xl-base-1.0")), n; } async getResponse(t, n, a, i) { if (typeof t == "object" && "images" in t && Array.isArray(t.images) && t.images.length > 0 && "url" in t.images[0] && typeof t.images[0].url == "string") return i === "json" ? { ...t } : i === "url" ? t.images[0].url : await (await fetch(t.images[0].url)).blob(); throw new w("Received malformed response from Fal.ai text-to-image API"); } } class ba extends Fe { constructor() { super("https://queue.fal.run"); p(this, "task"); this.task = "image-to-image"; } makeRoute(n) { return n.authMethod !== "provider-key" ? `/${n.model}?_subdomain=queue` : `/${n.model}`; } preparePayload(n) { var i; const a = n.args; return ((i = n.mapping) == null ? void 0 : i.adapter) === "lora" && n.mapping.adapterWeightsPath && (a.loras = [ { path: Yt(n.mapping.hfModelId, n.mapping.adapterWeightsPath), scale: 1 } ]), a; } async preparePayloadAsync(n) { const a = n.inputs instanceof Blob ? n.inputs.type : "image/png"; return { ...P(n, ["inputs", "parameters"]), image_url: `data:${a};base64,${Z(new Uint8Array(n.inputs instanceof ArrayBuffer ? n.inputs : await n.inputs.arrayBuffer()))}`, ...n.parameters, ...n }; } async getResponse(n, a, i) { const r = await this.getResponseFromQueueApi(n, a, i); if (typeof r == "object" && r && "images" in r && Array.isArray(r.images) && r.images.length > 0 && typeof r.images[0] == "object" && r.images[0] && "url" in r.images[0] && typeof r.images[0].url == "string" && fe(r.images[0].url)) return await (await fetch(r.images[0].url)).blob(); throw new w(`Received malformed response from Fal.ai image-to-image API: expected { images: Array<{ url: string }> } result format, got instead: ${JSON.stringify(r)}`); } } class wa extends Fe { constructor() { super("https://queue.fal.run"); p(this, "task"); this.task = "text-to-video"; } makeRoute(n) { return n.authMethod !== "provider-key" ? `/${n.model}?_subdomain=queue` : `/${n.model}`; } preparePayload(n) { return { ...P(n.args, ["inputs", "parameters"]), ...n.args.parameters, prompt: n.args.inputs }; } async getResponse(n, a, i) { const r = await this.getResponseFromQueueApi(n, a, i); if (typeof r == "object" && r && "video" in r && typeof r.video == "object" && r.video && "url" in r.video && typeof r.video.url == "string" && fe(r.video.url)) return await (await fetch(r.video.url)).blob(); throw new w(`Received malformed response from Fal.ai text-to-video API: expected { video: { url: string } } result format, got instead: ${JSON.stringify(r)}`); } } class va extends Fe { constructor() { super("https://queue.fal.run"); p(this, "task"); this.task = "image-to-video"; } /** Same queue routing rule as the other Fal queue tasks */ makeRoute(n) { return n.authMethod !== "provider-key" ? `/${n.model}?_subdomain=queue` : `/${n.model}`; } /** Synchronous case – caller already gave us base64 or a URL */ preparePayload(n) { return { ...P(n.args, ["inputs", "parameters"]), ...n.args.parameters, // args.inputs is expected to be a base64 data URI or an URL image_url: n.args.image_url }; } /** Asynchronous helper – caller gave us a Blob */ async preparePayloadAsync(n) { const a = n.inputs instanceof Blob ? n.inputs.type : "image/png"; return { ...P(n, ["inputs", "parameters"]), image_url: `data:${a};base64,${Z(new Uint8Array(n.inputs instanceof ArrayBuffer ? n.inputs : await n.inputs.arrayBuffer()))}`, ...n.parameters, ...n }; } /** Queue polling + final download – mirrors Text‑to‑Video */ async getResponse(n, a, i) { const r = await this.getResponseFromQueueApi(n, a, i); if (typeof r == "object" && r !== null && "video" in r && typeof r.video == "object" && r.video !== null && "url" in r.video && typeof r.video.url == "string" && "url" in r.video && fe(r.video.url)) return await (await fetch(r.video.url)).blob(); throw new w(`Received malformed response from Fal.ai image‑to‑video API: expected { video: { url: string } }, got: ${JSON.stringify(r)}`); } } class _a extends Be { prepareHeaders(t, n) { const a = super.prepareHeaders(t, n); return a["Content-Type"] = "application/json", a; } async getResponse(t) { const n = t; if (typeof (n == null ? void 0 : n.text) != "string") throw new w(`Received malformed response from Fal.ai Automatic Speech Recognition API: expected { text: string } format, got instead: ${JSON.stringify(t)}`); return { text: n.text }; } async preparePayloadAsync(t) { const n = "data" in t && t.data instanceof Blob ? t.data : "inputs" in t ? t.inputs : void 0, a = n == null ? void 0 : n.type; if (!a) throw new F("Unable to determine the input's content-type. Make sure your are passing a Blob when using provider fal-ai."); if (!kt.includes(a)) throw new F(`Provider fal-ai does not support blob type ${a} - supported content types are: ${kt.join(", ")}`); const i = Z(new Uint8Array(await n.arrayBuffer())); return { ..."data" in t ? P(t, "data") : P(t, "inputs"), audio_url: `data:${a};base64,${i}` }; } } class xa extends Be { preparePayload(t) { return { ...P(t.args, ["inputs", "parameters"]), ...t.args.parameters, text: t.args.inputs }; } async getResponse(t) { var i; const n = t; if (typeof ((i = n == null ? void 0 : n.audio) == null ? void 0 : i.url) != "string") throw new w(`Received malformed response from Fal.ai Text-to-Speech API: expected { audio: { url: string } } format, got instead: ${JSON.stringify(t)}`); const a = await fetch(n.audio.url); if (!a.ok) throw new K(`Failed to fetch audio from ${n.audio.url}: ${a.statusText}`, { url: n.audio.url, method: "GET", headers: { "Content-Type": "application/json" } }, { requestId: a.headers.get("x-request-id") ?? "", status: a.status, body: await a.text() }); try { return await a.blob(); } catch (r) { throw new K(`Failed to fetch audio from ${n.audio.url}: ${r instanceof Error ? r.message : String(r)}`, { url: n.audio.url, method: "GET", headers: { "Content-Type": "application/json" } }, { requestId: a.headers.get("x-request-id") ?? "", status: a.status, body: await a.text() }); } } } class ka extends Fe { constructor() { super("https://queue.fal.run"); p(this, "task"); this.task = "image-segmentation"; } makeRoute(n) { return n.authMethod !== "provider-key" ? `/${n.model}?_subdomain=queue` : `/${n.model}`; } preparePayload(n) { return { ...P(n.args, ["inputs", "parameters"]), ...n.args.parameters, sync_mode: !0 }; } async preparePayloadAsync(n) { const a = "data" in n && n.data instanceof Blob ? n.data : "inputs" in n ? n.inputs : void 0, i = a instanceof Blob ? a.type : "image/png", r = Z(new Uint8Array(a instanceof ArrayBuffer ? a : await a.arrayBuffer())); return { ...P(n, ["inputs", "parameters", "data"]), ...n.parameters, ...n, image_url: `data:${i};base64,${r}`, sync_mode: !0 }; } async getResponse(n, a, i) { const r = await this.getResponseFromQueueApi(n, a, i); if (typeof r == "object" && r !== null && "image" in r && typeof r.image == "object" && r.image !== null && "url" in r.image && typeof r.image.url == "string") { const o = await fetch(r.image.url); if (!o.ok) throw new K(`Failed to fetch segmentation mask from ${r.image.url}`, { url: r.image.url, method: "GET" }, { requestId: o.headers.get("x-request-id") ?? "", status: o.status, body: await o.text() }); const c = await (await o.blob()).arrayBuffer(); return [ { label: "mask", // placeholder label, as Fal does not provide labels in the response(?) score: 1, // placeholder score, as Fal does not provide scores in the response(?) mask: Z(new Uint8Array(c)) } ]; } throw new w(`Received malformed response from Fal.ai image-segmentation API: expected { image: { url: string } } format, got instead: ${JSON.stringify(n)}`); } } const Zt = "https://api.featherless.ai"; class Aa extends J { constructor() { super("featherless-ai", Zt); } } class Ia extends me { constructor() { super("featherless-ai", Zt); } preparePayload(t) { return { model: t.model, ...P(t.args, ["inputs", "parameters"]), ...t.args.parameters ? { max_tokens: t.args.parameters.max_new_tokens, ...P(t.args.parameters, "max_new_tokens") } : void 0, prompt: t.args.inputs }; } async getResponse(t) { if (typeof t == "object" && "choices" in t && Array.isArray(t == null ? void 0 : t.choices) && typeof (t == null ? void 0 : t.model) == "string") return { generated_text: t.choices[0].text }; throw new w("Received malformed response from Featherless AI text generation API"); } } class Sa extends J { constructor() { super("fireworks-ai", "https://api.fireworks.ai"); } makeRoute() { return "/inference/v1/chat/completions"; } } const Gt = "https://api.groq.com"; class Ta extends me { constructor() { super("groq", Gt); } makeRoute() { return "/openai/v1/chat/completions"; } } class Ea extends J { constructor() { super("groq", Gt); } makeRoute() { return "/openai/v1/chat/completions"; } } const st = "https://api.hyperbolic.xyz"; class Ca extends J { constructor() { super("hyperbolic", st); } } class Ra extends me { constructor() { super("hyperbolic", st); } makeRoute() { return "v1/chat/completions"; } preparePayload(t) { return { messages: [{ content: t.args.inputs, role: "user" }], ...t.args.parameters ? { max_tokens: t.args.parameters.max_new_tokens, ...P(t.args.parameters, "max_new_tokens") } : void 0, ...P(t.args, ["inputs", "parameters"]), model: t.model }; } async getResponse(t) { if (typeof t == "object" && "choices" in t && Array.isArray(t == null ? void 0 : t.choices) && typeof (t == null ? void 0 : t.model) == "string") return { generated_text: t.choices[0].message.content }; throw new w("Received malformed response from Hyperbolic text generation API"); } } class Pa extends X { constructor() { super("hyperbolic", st); } makeRoute(t) { return "/v1/images/generations"; } preparePayload(t) { return { ...P(t.args, ["inputs", "parameters"]), ...t.args.parameters, prompt: t.args.inputs, model_name: t.model }; } async getResponse(t, n, a, i) { if (typeof t == "object" && "images" in t && Array.isArray(t.images) && t.images[0] && typeof t.images[0].image == "string") return i === "json" ? { ...t } : i === "url" ? `data:image/jpeg;base64,${t.images[0].image}` : fetch(`data:image/jpeg;base64,${t.images[0].image}`).then((r) => r.blob()); throw new w("Received malformed response from Hyperbolic text-to-image API"); } } const He = "https://api.studio.nebius.ai"; class $a extends J { constructor() { super("nebius", He); } preparePayload(t) { var i; const n = super.preparePayload(t), a = t.args.response_format; return (a == null ? void 0 : a.type) === "json_schema" && ((i = a.json_schema) != null && i.schema) && (n.guided_json = a.json_schema.schema), n; } } class Ua extends me { constructor() { super("nebius", He); } preparePayload(t) { return { ...t.args, model: t.model, prompt: t.args.inputs }; } async getResponse(t) { var n; if (typeof t == "object" && "choices" in t && Array.isArray(t == null ? void 0 : t.choices) && t.choices.length > 0 && typeof ((n = t.choices[0]) == null ? void 0 : n.text) == "string") return { generated_text: t.choices[0].text }; throw new w("Received malformed response from Nebius text generation API"); } } class Na extends X { constructor() { super("nebius", He); } preparePayload(t) { return { ...P(t.args, ["inputs", "parameters"]), ...t.args.parameters, response_format: "b64_json", prompt: t.args.inputs, model: t.model }; } makeRoute() { return "v1/images/generations"; } async getResponse(t, n, a, i) { if (typeof t == "object" && "data" in t && Array.isArray(t.data) && t.data.length > 0 && "b64_json" in t.data[0] && typeof t.data[0].b64_json == "string") { if (i === "json") return { ...t }; const r = t.data[0].b64_json; return i === "url" ? `data:image/jpeg;base64,${r}` : fetch(`data:image/jpeg;base64,${r}`).then((o) => o.blob()); } throw new w("Received malformed response from Nebius text-to-image API"); } } class ja extends X { constructor() { super("nebius", He); } preparePayload(t) { return { input: t.args.inputs, model: t.model }; } makeRoute() { return "v1/embeddings"; } async getResponse(t) { return t.data.map((n) => n.embedding); } } const lt = "https://api.novita.ai"; class Da extends me { constructor() { super("novita", lt); } makeRoute() { return "/v3/openai/chat/completions"; } } class Ma extends J { constructor() { super("novita", lt); } makeRoute() { return "/v3/openai/chat/completions"; } } class La extends X { constructor() { super("novita", lt); } makeRoute(t) { return `/v3/async/${t.model}`; } preparePayload(t) { const { num_inference_steps: n, ...a } = t.args.parameters ?? {}; return { ...P(t.args, ["inputs", "parameters"]), ...a, steps: n, prompt: t.args.inputs }; } async getResponse(t, n, a) { if (!n || !a) throw new F("URL and headers are required for text-to-video task"); const i = t.task_id; if (!i) throw new w("Received malformed response from Novita text-to-video API: no task ID found in the response"); const r = new URL(n), s = `${`${r.protocol}//${r.host}${r.host === "router.huggingface.co" ? "/novita" : ""}`}/v3/async/task-result?task_id=${i}`; let c = "", l; for (; c !== "TASK_STATUS_SUCCEED" && c !== "TASK_STATUS_FAILED"; ) { await ot(500); const g = await fetch(s, { headers: a }); if (!g.ok) throw new K("Failed to fetch task result", { url: s, method: "GET", headers: a }, { requestId: g.headers.get("x-request-id") ?? "", status: g.status, body: await g.text() }); try { if (l = await g.json(), l && typeof l == "object" && "task" in l && l.task && typeof l.task == "object" && "status" in l.task && typeof l.task.status == "string") c = l.task.status; else throw new w("Received malformed response from Novita text-to-video API: failed to get task status"); } catch { throw new w("Received malformed response from Novita text-to-video API: failed to parse task result"); } } if (c === "TASK_STATUS_FAILED") throw new w("Novita text-to-video task failed"); if (typeof l == "object" && l && "videos" in l && typeof l.videos == "object" && l.videos && Array.isArray(l.videos) && l.videos.length > 0 && "video_url" in l.videos[0] && typeof l.videos[0].video_url == "string" && fe(l.videos[0].video_url)) return await (await fetch(l.videos[0].video_url)).blob(); throw new w(`Received malformed response from Novita text-to-video API: expected { videos: [{ video_url: string }] } format, got instead: ${JSON.stringify(l)}`); } } const en = "https://inference.api.nscale.com"; class Oa extends J { constructor() { super("nscale", en); } } class qa extends X { constructor() { super("nscale", en); } preparePayload(t) { return { ...P(t.args, ["inputs", "parameters"]), ...t.args.parameters, response_format: "b64_json", prompt: t.args.inputs, model: t.model }; } makeRoute() { return "v1/images/generations"; } async getResponse(t, n, a, i) { if (typeof t == "object" && "data" in t && Array.isArray(t.data) && t.data.length > 0 && "b64_json" in t.data[0] && typeof t.data[0].b64_json == "string") { if (i === "json") return { ...t }; const r = t.data[0].b64_json; return i === "url" ? `data:image/jpeg;base64,${r}` : fetch(`data:image/jpeg;base64,${r}`).then((o) => o.blob()); } throw new w("Received malformed response from Nscale text-to-image API"); } } const Ba = "https://api.openai.com"; class Fa extends J { constructor() { super("openai", Ba, !0); } } const tn = "https://oai.endpoints.kepler.ai.cloud.ovh.net"; class Ha extends J { constructor() { super("ovhcloud", tn); } } class Va extends me { constructor() { super("ovhcloud", tn); } preparePayload(t) { return { model: t.model, ...P(t.args, ["inputs", "parameters"]), ...t.args.parameters ? { max_tokens: t.args.parameters.max_new_tokens, ...P(t.args.parameters, "max_new_tokens") } : void 0, prompt: t.args.inputs }; } async getResponse(t) { if (typeof t == "object" && "choices" in t && Array.isArray(t == null ? void 0 : t.choices) && typeof (t == null ? void 0 : t.model) == "string") return { generated_text: t.choices[0].text }; throw new w("Received malformed response from OVHcloud text generation API"); } } class Ve extends X { constructor(t) { super("replicate", t || "https://api.replicate.com"); } makeRoute(t) { return t.model.includes(":") ? "v1/predictions" : `v1/models/${t.model}/predictions`; } preparePayload(t) { return { input: { ...P(t.args, ["inputs", "parameters"]), ...t.args.parameters, prompt: t.args.inputs }, version: t.model.includes(":") ? t.model.split(":")[1] : void 0 }; } prepareHeaders(t, n) { const a = { Authorization: `Bearer ${t.accessToken}`, Prefer: "wait" }; return n || (a["Content-Type"] = "application/json"), a; } makeUrl(t) { const n = this.makeBaseUrl(t); return t.model.includes(":") ? `${n}/v1/predictions` : `${n}/v1/models/${t.model}/predictions`; } } class Ka extends Ve { preparePayload(t) { var n; return { input: { ...P(t.args, ["inputs", "parameters"]), ...t.args.parameters, prompt: t.args.inputs, lora_weights: ((n = t.mapping) == null ? void 0 : n.adapter) === "lora" && t.mapping.adapterWeightsPath ? `https://huggingface.co/${t.mapping.hfModelId}` : void 0 }, version: t.model.includes(":") ? t.model.split(":")[1] : void 0 }; } async getResponse(t, n, a, i) { if (typeof t == "object" && "output" in t && Array.isArray(t.output) && t.output.length > 0 && typeof t.output[0] == "string") return i === "json" ? { ...t } : i === "url" ? t.output[0] : await (await fetch(t.output[0])).blob(); throw new w("Received malformed response from Replicate text-to-image API"); } } class Wa extends Ve { preparePayload(t) { const n = super.preparePayload(t), a = n.input; if (typeof a == "object" && a !== null && "prompt" in a) { const i = a; i.text = i.prompt, delete i.prompt; } return n; } async getResponse(t) { if (t instanceof Blob) return t; if (t && typeof t == "object" && "output" in t) { if (typeof t.output == "string") return await (await fetch(t.output)).blob(); if (Array.isArray(t.output)) return await (await fetch(t.output[0])).blob(); } throw new w("Received malformed response from Replicate text-to-speech API"); } } class za extends Ve { async getResponse(t) { if (typeof t == "object" && t && "output" in t && typeof t.output == "string" && fe(t.output)) return await (await fetch(t.output)).blob(); throw new w("Received malformed response from Replicate text-to-video API"); } } class Qa extends Ve { preparePayload(t) { var n; return { input: { ...P(t.args, ["inputs", "parameters"]), ...t.args.parameters, input_image: t.args.inputs, // This will be processed in preparePayloadAsync lora_weights: ((n = t.mapping) == null ? void 0 : n.adapter) === "lora" && t.mapping.adapterWeightsPath ? `https://huggingface.co/${t.mapping.hfModelId}` : void 0 }, version: t.model.includes(":") ? t.model.split(":")[1] : void 0 }; } async preparePayloadAsync(t) { const { inputs: n, ...a } = t, i = new Uint8Array(await n.arrayBuffer()), r = Z(i), o = `data:${n.type || "image/jpeg"};base64,${r}`; return { ...a, inputs: o }; } async getResponse(t) { if (typeof t == "object" && t && "output" in t && Array.isArray(t.output) && t.output.length > 0 && typeof t.output[0] == "string") return await (await fetch(t.output[0])).blob(); if (typeof t == "object" && t && "output" in t && typeof t.output == "string" && fe(t.output)) return await (await fetch(t.output)).blob(); throw new w("Received malformed response from Replicate image-to-image API"); } } class Xa extends J { constructor() { super("sambanova", "https://api.sambanova.ai"); } preparePayload(t) { const n = t.args.response_format; return (n == null ? void 0 : n.type) === "json_schema" && n.json_schema && (n.json_schema.strict ?? !0) && (n.json_schema.strict = !1), super.preparePayload(t); } } class Ja extends X { constructor() { super("sambanova", "https://api.sambanova.ai"); } makeRoute() { return "/v1/embeddings"; } async getResponse(t) { if (typeof t == "object" && "data" in t && Array.isArray(t.data)) return t.data.map((n) => n.embedding); throw new w("Received malformed response from Sambanova feature-extraction (embeddings) API"); } preparePayload(t) { return { model: t.model, input: t.args.inputs, ...t.args }; } } const ct = "https://api.together.xyz"; cla