1
0
forked from Leaf/amber-ui

改进 当没有对话的时候则创建对话

This commit is contained in:
ivamp 2024-09-13 00:43:23 +08:00
parent dd119f2da7
commit bbcfd3bfdc

View File

@ -128,6 +128,7 @@ import { EntityChatMessage, SchemaChatMessageAddRequestRoleEnum } from "@/api";
import getApi from "@/plugins/api"; import getApi from "@/plugins/api";
import MessageList from "./MessageList.vue"; import MessageList from "./MessageList.vue";
import { useChatStore } from "@/stores/chat"; import { useChatStore } from "@/stores/chat";
import router from "@/router";
// chatId // chatId
const chatId: Ref<string | number | undefined | null> = ref(null); const chatId: Ref<string | number | undefined | null> = ref(null);
@ -293,17 +294,22 @@ async function sendMessage(
return; return;
} }
let redirect = false;
if (!chatId.value) { if (!chatId.value) {
getApi() await getApi()
.Chat.apiV1ChatsPost({ .Chat.apiV1ChatsPost({
name: text.slice(0, 10), name: text.slice(0, 10),
}) })
.then(async (res) => { .then(async (res) => {
chatId.value = res.data.data?.id; chatId.value = res.data.data?.id;
if (chatId.value) {
chatStore.currentChatId = chatId.value;
redirect = true;
}
await getChatMessages(); await getChatMessages();
}); });
return;
} }
toolError.value = false; toolError.value = false;
@ -330,7 +336,7 @@ async function sendMessage(
if (streamId) { if (streamId) {
await getChatMessages(); await getChatMessages();
streamChat(streamId); streamChat(streamId, redirect);
} }
}) })
.catch(async (err) => { .catch(async (err) => {
@ -340,7 +346,7 @@ async function sendMessage(
if (streamId) { if (streamId) {
await getChatMessages(); await getChatMessages();
streamChat(streamId); streamChat(streamId, redirect);
} }
} }
}); });
@ -382,7 +388,7 @@ async function getChatMessages() {
return false; return false;
} }
function streamChat(streamId: String) { function streamChat(streamId: String, redirect = false) {
const url = getApi().conf.basePath + "/api/v1/stream/" + streamId; const url = getApi().conf.basePath + "/api/v1/stream/" + streamId;
const evtSource = new EventSource(url); const evtSource = new EventSource(url);
@ -397,6 +403,16 @@ function streamChat(streamId: String) {
evtSource.close(); evtSource.close();
processing.value = false; processing.value = false;
if (redirect && chatId.value != undefined) {
// URL
router.replace({
name: "/chat/[id]/",
params: {
id: chatId.value,
},
});
}
return; return;
} }