fix command type

This commit is contained in:
Chuck1sn
2025-05-26 11:27:00 +08:00
parent dde5fecd62
commit 3f4a5f2e8b
8 changed files with 106 additions and 44 deletions

View File

@@ -1,23 +1,34 @@
import { fetchEventSource } from "@microsoft/fetch-event-source";
import { ref } from "vue";
import useAuthStore from "../store/useAuthStore";
import client from "../../api/client";
import useAuthStore from "../store/useAuthStore";
const authStore = useAuthStore();
export const useAiChat = () => {
const messages = ref<string[]>([]);
const messages = ref<
{
content: string;
type: "chat" | "action";
isUser: boolean;
username: string;
command?: string;
}[]
>([]);
const isLoading = ref(false);
let currentController: AbortController | null = null;
const chat = async (message: string) => {
isLoading.value = true;
messages.value.push(message);
messages.value.push("");
const ctrl = new AbortController();
currentController = ctrl;
messages.value.push({
content: "",
type: "chat",
isUser: false,
username: "知路智能体",
});
try {
const baseUrl = `${import.meta.env.VITE_BASE_URL}`;
await fetchEventSource(`${baseUrl}/ai/chat`, {
@@ -29,7 +40,7 @@ export const useAiChat = () => {
body: message,
signal: ctrl.signal,
onmessage(ev) {
messages.value[messages.value.length - 1] += ev.data;
messages.value[messages.value.length - 1].content += ev.data;
},
onclose() {
console.log("onclose");
@@ -38,20 +49,26 @@ export const useAiChat = () => {
throw err;
},
});
} catch (error) {
messages.value.pop();
} finally {
isLoading.value = false;
}
};
const actionChat = async (message: string) => {
messages.value.push(message);
messages.value.push("");
isLoading.value = true;
try {
const { data } = await client.POST("/ai/action/chat", {
body: message,
});
messages.value[messages.value.length - 1] += "接收到指令,请您执行。";
messages.value.push({
content: "接收到指令,请您执行。",
type: "action",
isUser: false,
username: "知路智能体",
command: data?.action,
});
return data;
} finally {
isLoading.value = false;