Skip to content

Let's use AI to Earn!

fix(chat): optimeze agent (#433)

yikaeab7cbcd3ce1
diff --git a/project/web/src/api/agent.ts b/project/web/src/api/agent.ts
index 0c1f1188..f292ed7f 100644
--- a/project/web/src/api/agent.ts
+++ b/project/web/src/api/agent.ts
@@ -185,6 +185,10 @@ export const agentApi = {
let sessionId: string | undefined
let abortController = new AbortController()
+ // 用于消息去重,防止重复处理
+ const processedMessageIds = new Set<string>()
+ // 标记是否已经完成,防止重复调用 onDone
+ let isCompleted = false
// 返回 abort 函数
const abort = () => {
@@ -224,7 +228,7 @@ export const agentApi = {
throw new Error(`HTTP ${response.status}: ${errorText}`)
}
else {
- // 服务器错误或其他问题,会自动重试
+ // 服务器错误或其他问题,不自动重试,直接抛出错误
console.error('[SSE] Server error:', response.status)
throw new Error(`HTTP ${response.status}`)
}
@@ -232,6 +236,12 @@ export const agentApi = {
// 当收到消息时
onmessage(event) {
+ // 如果已完成,忽略后续消息
+ if (isCompleted) {
+ console.log('[SSE] Already completed, ignoring message')
+ return
+ }
+
console.log('[SSE] Received message:', event)
// 如果没有数据,跳过
@@ -244,22 +254,31 @@ export const agentApi = {
const data = JSON.parse(event.data)
console.log('[SSE] Parsed data:', data)
+ // 消息去重:基于 uuid 或生成唯一标识
+ const messageId = data.uuid || data.message?.uuid || `${data.type}-${JSON.stringify(data).slice(0, 100)}`
+ if (processedMessageIds.has(messageId)) {
+ console.log('[SSE] Duplicate message, skipping:', messageId)
+ return
+ }
+ processedMessageIds.add(messageId)
+
// 保存 sessionId
if (data.sessionId) {
sessionId = data.sessionId
console.log('[SSE] Got sessionId:', sessionId)
- }
+ }
- // 调用消息回调
- onMessage(data)
+ // 调用消息回调
+ onMessage(data)
- // 如果收到结束信号,关闭连接
- if (data.type === 'done' || data.type === 'error') {
- console.log('[SSE] Received end signal:', data.type)
- abortController.abort()
+ // 如果收到结束信号,关闭连接
+ if (data.type === 'done' || data.type === 'error') {
+ console.log('[SSE] Received end signal:', data.type)
+ isCompleted = true
+ abortController.abort()
+ }
}
- }
- catch (error) {
+ catch (error) {
console.error('[SSE] Failed to parse message:', event.data, error)
}
},
@@ -267,7 +286,10 @@ export const agentApi = {
// 当连接关闭时
onclose() {
console.log('[SSE] Connection closed')
- onDone(sessionId)
+ if (!isCompleted) {
+ isCompleted = true
+ onDone(sessionId)
+ }
},
// 当发生错误时
@@ -280,8 +302,12 @@ export const agentApi = {
return
}
- // 其他错误,调用错误回调
- onError(error instanceof Error ? error : new Error(String(error)))
+ // 标记为已完成,防止重复处理
+ if (!isCompleted) {
+ isCompleted = true
+ // 其他错误,调用错误回调
+ onError(error instanceof Error ? error : new Error(String(error)))
+ }
// 抛出错误以停止重试
throw error
@@ -291,8 +317,9 @@ export const agentApi = {
catch (error) {
console.error('[SSE] fetchEventSource failed:', error)
- // 如果不是手动中止的错误,调用错误回调
- if (!abortController.signal.aborted) {
+ // 如果不是手动中止的错误且未完成,调用错误回调
+ if (!abortController.signal.aborted && !isCompleted) {
+ isCompleted = true
onError(error instanceof Error ? error : new Error(String(error)))
}
}
diff --git a/project/web/src/components/Chat/ChatInput/index.tsx b/project/web/src/components/Chat/ChatInput/index.tsx
index 88d02ecd..70732e12 100644
--- a/project/web/src/components/Chat/ChatInput/index.tsx
+++ b/project/web/src/components/Chat/ChatInput/index.tsx
@@ -90,9 +90,11 @@ export function ChatInput({
/** 处理发送/停止按钮点击 */
const handleButtonClick = () => {
- if (isGenerating) {
- onStop?.()
- } else if (!disabled && !isUploading && value.trim()) {
+ // 中断功能暂时禁用
+ // if (isGenerating) {
+ // onStop?.()
+ // } else
+ if (!disabled && !isUploading && value.trim() && !isGenerating) {
onSend()
}
}
@@ -165,15 +167,14 @@ export function ChatInput({
{/* 右侧:发送/停止按钮 */}
<button
onClick={handleButtonClick}
- disabled={!isGenerating && !canSend}
+ disabled={isGenerating || !canSend}
className={cn(
'shrink-0 flex items-center justify-center rounded-full transition-all',
mode === 'large' ? 'w-10 h-10' : 'w-8 h-8',
- isGenerating
- ? 'bg-destructive hover:bg-destructive/90 text-destructive-foreground'
- : canSend
- ? 'bg-primary hover:bg-primary/90 text-primary-foreground'
- : 'bg-muted text-muted-foreground cursor-not-allowed',
+ // 生成中或无法发送时都显示灰色
+ (isGenerating || !canSend)
+ ? 'bg-muted text-muted-foreground cursor-not-allowed'
+ : 'bg-primary hover:bg-primary/90 text-primary-foreground',
)}
>
{isGenerating ? (
diff --git a/project/web/src/components/Chat/ChatMessage/index.tsx b/project/web/src/components/Chat/ChatMessage/index.tsx
index 9f559d23..da184eb3 100644
--- a/project/web/src/components/Chat/ChatMessage/index.tsx
+++ b/project/web/src/components/Chat/ChatMessage/index.tsx
@@ -499,8 +499,9 @@ export function ChatMessage({
<div
className={cn(
'px-4 py-3 rounded-2xl text-sm leading-relaxed',
- 'bg-card border border-border text-foreground rounded-br-md whitespace-pre-wrap',
+ 'bg-card border border-border text-foreground rounded-br-md whitespace-pre-wrap break-words',
)}
+ style={{ wordBreak: 'break-word', overflowWrap: 'anywhere' }}
>
{content}
</div>