This commit is contained in:
Chuck1sn
2025-05-21 11:49:15 +08:00
parent d12862ea39
commit 3bc65d2df4
9 changed files with 209 additions and 36 deletions

View File

@@ -0,0 +1,31 @@
import { ref } from "vue";
import client from "../../api/client";
export const useAiChat = () => {
const messages = ref<string[]>([]);
const isLoading = ref(false);
const chat = async (message: string) => {
isLoading.value = true;
try {
const { response } = await client.POST("/ai/chat", {
body: message,
parseAs: "stream",
});
const reader = response.body?.getReader();
if (reader) {
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
messages.value.push(decoder.decode(value, { stream: true }));
console.log(decoder.decode(value));
}
return;
}
} finally {
isLoading.value = false;
}
};
return { messages, chat, isLoading };
};