mirror of
https://github.com/ccmjga/zhilu-admin
synced 2026-04-07 22:17:34 +00:00
fix command chat
This commit is contained in:
3366
frontend/src/api/types/schema.d.ts
vendored
3366
frontend/src/api/types/schema.d.ts
vendored
File diff suppressed because it is too large
Load Diff
@@ -1,11 +0,0 @@
|
|||||||
import client from "../../api/client";
|
|
||||||
|
|
||||||
const useAiAction = () => {
|
|
||||||
const actionChat = (message: string) => {
|
|
||||||
return client.POST("/ai/action/chat", {
|
|
||||||
body: message,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return { actionChat };
|
|
||||||
};
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { fetchEventSource } from "@microsoft/fetch-event-source";
|
import { fetchEventSource } from "@microsoft/fetch-event-source";
|
||||||
import { ref } from "vue";
|
import { ref } from "vue";
|
||||||
import useAuthStore from "../store/useAuthStore";
|
import useAuthStore from "../store/useAuthStore";
|
||||||
import useAlertStore from "../store/useAlertStore";
|
import client from "../../api/client";
|
||||||
|
|
||||||
const authStore = useAuthStore();
|
const authStore = useAuthStore();
|
||||||
|
|
||||||
@@ -43,6 +43,21 @@ export const useAiChat = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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] += "接收到指令,请您执行。";
|
||||||
|
return data;
|
||||||
|
} finally {
|
||||||
|
isLoading.value = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const cancel = () => {
|
const cancel = () => {
|
||||||
if (currentController) {
|
if (currentController) {
|
||||||
currentController.abort();
|
currentController.abort();
|
||||||
@@ -50,5 +65,5 @@ export const useAiChat = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return { messages, chat, isLoading, cancel };
|
return { messages, chat, isLoading, cancel, actionChat };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ const makeErrorHandler =
|
|||||||
}) => void,
|
}) => void,
|
||||||
) =>
|
) =>
|
||||||
(err: unknown, instance: ComponentPublicInstance | null, info: string) => {
|
(err: unknown, instance: ComponentPublicInstance | null, info: string) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
if (err instanceof UnAuthError) {
|
if (err instanceof UnAuthError) {
|
||||||
signOut();
|
signOut();
|
||||||
router.push(RoutePath.LOGIN);
|
router.push(RoutePath.LOGIN);
|
||||||
@@ -44,6 +44,11 @@ const makeErrorHandler =
|
|||||||
level: "error",
|
level: "error",
|
||||||
content: err.detail ?? err.message,
|
content: err.detail ?? err.message,
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
showAlert({
|
||||||
|
level: "error",
|
||||||
|
content: "发生异常,请稍候再试",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -19,8 +19,23 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form class="sticky bottom-4 mt-14">
|
<form class="sticky bottom-4 mt-14">
|
||||||
<div class="w-full border border-gray-200 rounded-lg bg-gray-50 ">
|
<button @click.prevent="toggleCommandMode"
|
||||||
<div class="px-4 py-2 bg-white rounded-t-lg ">
|
class="relative inline-flex items-center justify-center p-0.5 mb-2 me-2 overflow-hidden text-sm font-medium text-gray-900 rounded-lg group focus:ring-4 focus:outline-none focus:ring-lime-200"
|
||||||
|
:class="[
|
||||||
|
isCommandMode
|
||||||
|
? 'bg-gradient-to-br from-teal-300 to-lime-300 '
|
||||||
|
: 'bg-gradient-to-br from-gray-300 to-gray-300 group-hover:from-teal-300 group-hover:to-lime-300'
|
||||||
|
]">
|
||||||
|
<span class="relative px-3 py-2 transition-all ease-in duration-75 rounded-md" :class="[
|
||||||
|
isCommandMode
|
||||||
|
? 'bg-transparent'
|
||||||
|
: 'bg-white group-hover:bg-transparent'
|
||||||
|
]">
|
||||||
|
命令模式
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
<div class="w-full border border-gray-200 rounded-lg bg-gray-50">
|
||||||
|
<div class="px-4 py-2 bg-white rounded-t-lg">
|
||||||
<label for="comment" class="sr-only"></label>
|
<label for="comment" class="sr-only"></label>
|
||||||
<textarea id="comment" rows="3" v-model="inputMessage"
|
<textarea id="comment" rows="3" v-model="inputMessage"
|
||||||
class="w-full px-0 text-gray-900 bg-white border-0 focus:ring-0 " placeholder="发送消息" required></textarea>
|
class="w-full px-0 text-gray-900 bg-white border-0 focus:ring-0 " placeholder="发送消息" required></textarea>
|
||||||
@@ -65,11 +80,16 @@ import Button from "../components/Button.vue";
|
|||||||
import { useAiChat } from "../composables/ai/useAiChat";
|
import { useAiChat } from "../composables/ai/useAiChat";
|
||||||
import useUserStore from "../composables/store/useUserStore";
|
import useUserStore from "../composables/store/useUserStore";
|
||||||
|
|
||||||
const { messages, chat, isLoading, cancel } = useAiChat();
|
const { messages, chat, isLoading, cancel, actionChat } = useAiChat();
|
||||||
const { user } = useUserStore();
|
const { user } = useUserStore();
|
||||||
const inputMessage = ref("");
|
const inputMessage = ref("");
|
||||||
const chatContainer = ref<HTMLElement | null>(null);
|
const chatContainer = ref<HTMLElement | null>(null);
|
||||||
const alertStore = useAlertStore();
|
const alertStore = useAlertStore();
|
||||||
|
const isCommandMode = ref(false);
|
||||||
|
|
||||||
|
const toggleCommandMode = () => {
|
||||||
|
isCommandMode.value = !isCommandMode.value;
|
||||||
|
};
|
||||||
|
|
||||||
marked.setOptions({
|
marked.setOptions({
|
||||||
gfm: true,
|
gfm: true,
|
||||||
@@ -125,7 +145,19 @@ const abortChat = () => {
|
|||||||
cancel();
|
cancel();
|
||||||
};
|
};
|
||||||
|
|
||||||
const sendMessage = async () => {
|
const chatByMode = async (message: string) => {
|
||||||
|
if (isCommandMode.value) {
|
||||||
|
await actionChat(message);
|
||||||
|
} else {
|
||||||
|
if (isLoading.value) {
|
||||||
|
abortChat();
|
||||||
|
} else {
|
||||||
|
await chat(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSendClick = async () => {
|
||||||
try {
|
try {
|
||||||
const validInputMessage = z
|
const validInputMessage = z
|
||||||
.string({ message: "消息不能为空" })
|
.string({ message: "消息不能为空" })
|
||||||
@@ -133,7 +165,7 @@ const sendMessage = async () => {
|
|||||||
.parse(inputMessage.value);
|
.parse(inputMessage.value);
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
inputMessage.value = "";
|
inputMessage.value = "";
|
||||||
await chat(validInputMessage);
|
await chatByMode(validInputMessage);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof z.ZodError) {
|
if (error instanceof z.ZodError) {
|
||||||
alertStore.showAlert({
|
alertStore.showAlert({
|
||||||
@@ -146,14 +178,6 @@ const sendMessage = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSendClick = async () => {
|
|
||||||
if (isLoading.value) {
|
|
||||||
abortChat();
|
|
||||||
} else {
|
|
||||||
sendMessage();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
cancel();
|
cancel();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user