From b6492c234e9ad24e20b8a062c5cc1019e51b5d12 Mon Sep 17 00:00:00 2001 From: weareoutman Date: Mon, 19 Aug 2024 10:00:05 +0800 Subject: [PATCH] fix(): handle done immediately --- bricks/ai/src/chat-view/ChatService.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/bricks/ai/src/chat-view/ChatService.ts b/bricks/ai/src/chat-view/ChatService.ts index b7be14628..4754f8994 100644 --- a/bricks/ai/src/chat-view/ChatService.ts +++ b/bricks/ai/src/chat-view/ChatService.ts @@ -385,6 +385,7 @@ export class ChatService { async chat(msg: string | ChatBody): Promise { this.#ctrl = new AbortController(); let hadMatchMessage = false; + let hadUsefulMessage = false; this.#chatting = true; await fetchEventSource( `${getBasePath()}api/gateway/easyops.api.aiops_chat.manage.LLMChatProxy@1.0.0/api/aiops_chat/v1/chat/completions`, @@ -423,9 +424,24 @@ export class ChatService { const { data } = msg; hadMatchMessage = true; if (data === "[DONE]") { + if (!hadUsefulMessage) { + this.enqueue({ + topic: "add", + message: { + created: moment().format("YYYY-MM-DD HH:mm:ss"), + delta: { + role: "assistant", + content: "", + }, + agentId: this.#agentId, + robotId: this.#robotId, + }, + }); + } this.#ctrl!.abort(); return; } + hadUsefulMessage = true; let result = {} as SSEMessageItem; try { result = JSON.parse(data); @@ -538,7 +554,7 @@ export class ChatService { stop() { clearTimeout(this.#emitTimer); - this.#ctrl && this.#ctrl?.abort(); + this.#ctrl?.abort(); this.#chatting = false; this.#emitTimer = undefined; this.#isStartEmitEvent = false;