mirror of
https://github.com/ccmjga/zhilu-admin
synced 2026-03-23 11:23:42 +08:00
fix command type
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user