fix:lsome bug

This commit is contained in:
fdc
2025-07-31 10:28:43 +08:00
parent e4d951b174
commit 5560a4f52d
2 changed files with 38 additions and 29 deletions
@@ -46,6 +46,18 @@ export default function DebugDialog({
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}; };
const loadMessages = async (pipelineId: string) => {
try {
const response = await httpClient.getWebChatHistoryMessages(
pipelineId,
sessionType,
);
setMessages(response.messages);
} catch (error) {
console.error('Failed to load messages:', error);
}
};
useEffect(() => { useEffect(() => {
scrollToBottom(); scrollToBottom();
}, [messages]); }, [messages]);
@@ -61,7 +73,7 @@ export default function DebugDialog({
if (open) { if (open) {
loadMessages(selectedPipelineId); loadMessages(selectedPipelineId);
} }
}, [sessionType, selectedPipelineId]); }, [sessionType, selectedPipelineId, open, loadMessages]);
useEffect(() => { useEffect(() => {
const handleClickOutside = (event: MouseEvent) => { const handleClickOutside = (event: MouseEvent) => {
@@ -86,18 +98,6 @@ export default function DebugDialog({
} }
}, [showAtPopover]); }, [showAtPopover]);
const loadMessages = async (pipelineId: string) => {
try {
const response = await httpClient.getWebChatHistoryMessages(
pipelineId,
sessionType,
);
setMessages(response.messages);
} catch (error) {
console.error('Failed to load messages:', error);
}
};
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value; const value = e.target.value;
if (sessionType === 'group') { if (sessionType === 'group') {
@@ -179,12 +179,16 @@ export default function DebugDialog({
// 添加用户消息和初始bot消息到状态 // 添加用户消息和初始bot消息到状态
setMessages((prevMessages) => [...prevMessages, userMessage, botMessage]); setMessages((prevMessages) => [
...prevMessages,
userMessage,
botMessage,
]);
setInputValue(''); setInputValue('');
setHasAt(false); setHasAt(false);
try { try {
let botMessageId = botMessage.id; const botMessageId = botMessage.id;
let accumulatedContent = ''; let accumulatedContent = '';
await httpClient.sendStreamingWebChatMessage( await httpClient.sendStreamingWebChatMessage(
@@ -200,14 +204,17 @@ export default function DebugDialog({
setMessages((prevMessages) => { setMessages((prevMessages) => {
const updatedMessages = [...prevMessages]; const updatedMessages = [...prevMessages];
const botMessageIndex = updatedMessages.findIndex( const botMessageIndex = updatedMessages.findIndex(
(msg) => msg.id === botMessageId && msg.role === 'assistant' (msg) =>
msg.id === botMessageId && msg.role === 'assistant',
); );
if (botMessageIndex !== -1) { if (botMessageIndex !== -1) {
const updatedBotMessage = { const updatedBotMessage = {
...updatedMessages[botMessageIndex], ...updatedMessages[botMessageIndex],
content: accumulatedContent, content: accumulatedContent,
message_chain: [{ type: 'Plain', text: accumulatedContent }], message_chain: [
{ type: 'Plain', text: accumulatedContent },
]
}; };
updatedMessages[botMessageIndex] = updatedBotMessage; updatedMessages[botMessageIndex] = updatedBotMessage;
} }
@@ -226,7 +233,7 @@ export default function DebugDialog({
if (sessionType === 'person') { if (sessionType === 'person') {
toast.error(t('pipelines.debugDialog.sendFailed')); toast.error(t('pipelines.debugDialog.sendFailed'));
} }
} },
); );
} catch (error) { } catch (error) {
console.error('Failed to send streaming message:', error); console.error('Failed to send streaming message:', error);
@@ -378,7 +385,9 @@ export default function DebugDialog({
<div className="p-4 pb-0 bg-white flex gap-2"> <div className="p-4 pb-0 bg-white flex gap-2">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<span className="text-sm text-gray-600">{t('pipelines.debugDialog.streaming')}</span> <span className="text-sm text-gray-600">
{t('pipelines.debugDialog.streaming')}
</span>
<Switch <Switch
checked={isStreaming} checked={isStreaming}
onCheckedChange={setIsStreaming} onCheckedChange={setIsStreaming}
+10 -10
View File
@@ -34,7 +34,7 @@ import {
} from '@/app/infra/entities/api'; } from '@/app/infra/entities/api';
import { GetBotLogsRequest } from '@/app/infra/http/requestParam/bots/GetBotLogsRequest'; import { GetBotLogsRequest } from '@/app/infra/http/requestParam/bots/GetBotLogsRequest';
import { GetBotLogsResponse } from '@/app/infra/http/requestParam/bots/GetBotLogsResponse'; import { GetBotLogsResponse } from '@/app/infra/http/requestParam/bots/GetBotLogsResponse';
import {boolean} from "zod";
type JSONValue = string | number | boolean | JSONObject | JSONArray | null; type JSONValue = string | number | boolean | JSONObject | JSONArray | null;
interface JSONObject { interface JSONObject {
@@ -329,13 +329,13 @@ class HttpClient {
sessionType: string, sessionType: string,
messageChain: object[], messageChain: object[],
pipelineId: string, pipelineId: string,
onMessage: (data: any) => void, onMessage: (data: ApiRespWebChatMessage) => void,
onComplete: () => void, onComplete: () => void,
onError: (error: any) => void, onError: (error: Error) => void,
): Promise<void> { ): Promise<void> {
try { try {
const url = `${this.baseURL}/api/v1/pipelines/${pipelineId}/chat/send`; const url = `${this.baseURL}/api/v1/pipelines/${pipelineId}/chat/send`;
// 使用fetch发送流式请求,因为axios在浏览器环境中不直接支持流式响应 // 使用fetch发送流式请求,因为axios在浏览器环境中不直接支持流式响应
const response = await fetch(url, { const response = await fetch(url, {
method: 'POST', method: 'POST',
@@ -365,7 +365,7 @@ class HttpClient {
try { try {
while (true) { while (true) {
const { done, value } = await reader.read(); const { done, value } = await reader.read();
if (done) { if (done) {
onComplete(); onComplete();
break; break;
@@ -373,23 +373,23 @@ class HttpClient {
// 解码数据 // 解码数据
buffer += decoder.decode(value, { stream: true }); buffer += decoder.decode(value, { stream: true });
// 处理完整的JSON对象 // 处理完整的JSON对象
const lines = buffer.split('\n\n'); const lines = buffer.split('\n\n');
buffer = lines.pop() || ''; buffer = lines.pop() || '';
for (const line of lines) { for (const line of lines) {
if (line.startsWith('data: ')) { if (line.startsWith('data: ')) {
try { try {
const data = JSON.parse(line.slice(6)); const data = JSON.parse(line.slice(6));
if (data.type === 'end') { if (data.type === 'end') {
// 流传输结束 // 流传输结束
reader.cancel(); reader.cancel();
onComplete(); onComplete();
return; return;
} }
if (data.message) { if (data.message) {
// 处理消息数据 // 处理消息数据
onMessage(data); onMessage(data);
@@ -404,7 +404,7 @@ class HttpClient {
reader.releaseLock(); reader.releaseLock();
} }
} catch (error) { } catch (error) {
onError(error); onError(error as Error);
} }
} }