UNPKG

@ai-sdk/vue

Version:

[Vue.js](https://vuejs.org/) UI components for the [AI SDK](https://ai-sdk.dev/docs):

187 lines (184 loc) 5.38 kB
"use strict"; 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); // src/index.ts var src_exports = {}; __export(src_exports, { Chat: () => Chat, useCompletion: () => useCompletion }); module.exports = __toCommonJS(src_exports); // src/use-completion.ts var import_ai = require("ai"); var import_swrv = __toESM(require("swrv")); var import_vue = require("vue"); var uniqueId = 0; var useSWRV = import_swrv.default.default || import_swrv.default; var store = {}; function useCompletion({ api = "/api/completion", id, initialCompletion = "", initialInput = "", credentials, headers, body, streamProtocol, onFinish, onError, fetch } = {}) { var _a; const completionId = id || `completion-${uniqueId++}`; const key = `${api}|${completionId}`; const { data, mutate: originalMutate } = useSWRV( key, () => store[key] || initialCompletion ); const { data: isLoading, mutate: mutateLoading } = useSWRV( `${completionId}-loading`, null ); (_a = isLoading.value) != null ? _a : isLoading.value = false; data.value || (data.value = initialCompletion); const mutate = (data2) => { store[key] = data2; return originalMutate(); }; const completion = data; const error = (0, import_vue.ref)(void 0); let abortController = null; async function triggerRequest(prompt, options) { return (0, import_ai.callCompletionApi)({ api, prompt, credentials, headers: { ...headers, ...options == null ? void 0 : options.headers }, body: { ...(0, import_vue.unref)(body), ...options == null ? void 0 : options.body }, streamProtocol, setCompletion: mutate, setLoading: (loading) => mutateLoading(() => loading), setError: (err) => { error.value = err; }, setAbortController: (controller) => { abortController = controller; }, onFinish, onError, fetch }); } const complete = async (prompt, options) => { return triggerRequest(prompt, options); }; const stop = () => { if (abortController) { abortController.abort(); abortController = null; } }; const setCompletion = (completion2) => { mutate(completion2); }; const input = (0, import_vue.ref)(initialInput); const handleSubmit = (event) => { var _a2; (_a2 = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a2.call(event); const inputValue = input.value; return inputValue ? complete(inputValue) : void 0; }; return { completion, complete, error, stop, setCompletion, input, handleSubmit, isLoading }; } // src/chat.vue.ts var import_ai2 = require("ai"); var import_vue2 = require("vue"); var VueChatState = class { constructor(messages) { this.statusRef = (0, import_vue2.ref)("ready"); this.errorRef = (0, import_vue2.ref)(void 0); this.pushMessage = (message) => { this.messagesRef.value = [...this.messagesRef.value, message]; }; this.popMessage = () => { this.messagesRef.value = this.messagesRef.value.slice(0, -1); }; this.replaceMessage = (index, message) => { this.messagesRef.value[index] = { ...message }; }; this.snapshot = (value) => value; this.messagesRef = (0, import_vue2.ref)(messages != null ? messages : []); } get messages() { return this.messagesRef.value; } set messages(messages) { this.messagesRef.value = messages; } get status() { return this.statusRef.value; } set status(status) { this.statusRef.value = status; } get error() { return this.errorRef.value; } set error(error) { this.errorRef.value = error; } }; var Chat = class extends import_ai2.AbstractChat { constructor({ messages, ...init }) { super({ ...init, state: new VueChatState(messages) }); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Chat, useCompletion }); //# sourceMappingURL=index.js.map