@ai-sdk/vue
Version:
[Vue.js](https://vuejs.org/) UI components for the [AI SDK](https://ai-sdk.dev/docs):
151 lines (150 loc) • 3.58 kB
JavaScript
// src/use-completion.ts
import { callCompletionApi } from "ai";
import swrv from "swrv";
import { ref, unref } from "vue";
var uniqueId = 0;
var useSWRV = swrv.default || swrv;
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 = ref(void 0);
let abortController = null;
async function triggerRequest(prompt, options) {
return callCompletionApi({
api,
prompt,
credentials,
headers: {
...headers,
...options == null ? void 0 : options.headers
},
body: {
...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 = 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
import {
AbstractChat
} from "ai";
import { ref as ref2 } from "vue";
var VueChatState = class {
constructor(messages) {
this.statusRef = ref2("ready");
this.errorRef = ref2(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 = ref2(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 AbstractChat {
constructor({ messages, ...init }) {
super({
...init,
state: new VueChatState(messages)
});
}
};
export {
Chat,
useCompletion
};
//# sourceMappingURL=index.mjs.map