Skip to content

Let's use AI to Earn!

fix: fix agent ui (#480)

Gao2e92a1883308
diff --git a/project/web/src/app/[lng]/chat/[taskId]/utils/convertMessages.ts b/project/web/src/app/[lng]/chat/[taskId]/utils/convertMessages.ts
index f78d5924..17fd02fe 100644
--- a/project/web/src/app/[lng]/chat/[taskId]/utils/convertMessages.ts
+++ b/project/web/src/app/[lng]/chat/[taskId]/utils/convertMessages.ts
@@ -37,6 +37,10 @@ export function convertMessages(messages: TaskMessage[]): IDisplayMessage[] {
// 用于追踪工具调用的 Map
const toolCallMap = new Map<string, string>()
+ // 用于追踪当前消息轮次(通过 message.id 判断)
+ // 当 message.id 变化时,表示新的一轮消息开始,需要分割步骤
+ let currentMessageId = ''
+
/** 保存当前步骤到步骤列表 */
const saveCurrentStep = () => {
if (currentStepContent.trim() || currentStepWorkflow.length > 0) {
@@ -163,6 +167,25 @@ export function convertMessages(messages: TaskMessage[]): IDisplayMessage[] {
}
else if (msg.type === 'assistant') {
// AI 回复消息处理
+ // 检测消息轮次变化:通过 message.id 判断是否是新的一轮
+ // 在详情数据中,同一轮的多条 assistant 消息会有相同的 message.id
+ const messageData = (msg as any).message as any
+ const messageId = messageData?.id || ''
+
+ // 检查是否包含 text 内容(非空文本)
+ const hasText = messageData?.content?.some((item: any) => item.type === 'text' && item.text?.trim())
+
+ // 只在遇到包含 text 的新 message.id 时开始新步骤
+ // 纯 tool_use 消息不触发新步骤,其工作流会合并到当前步骤
+ if (hasText && messageId && currentMessageId && messageId !== currentMessageId) {
+ // 新的消息轮次开始(且有文本内容),保存当前步骤
+ saveCurrentStep()
+ }
+ // 更新当前消息 ID
+ if (messageId) {
+ currentMessageId = messageId
+ }
+
const result = processAssistantMessage(msg, index, displayMessages, currentStepWorkflow, toolCallMap)
if (result.contentToAdd) {
currentStepContent += (currentStepContent ? '\n\n' : '') + result.contentToAdd
diff --git a/project/web/src/store/agent/handlers/sse.handlers.ts b/project/web/src/store/agent/handlers/sse.handlers.ts
index 3e05c30b..70bd894b 100644
--- a/project/web/src/store/agent/handlers/sse.handlers.ts
+++ b/project/web/src/store/agent/handlers/sse.handlers.ts
@@ -163,20 +163,27 @@ export const textDeltaHandler: ISSEHandler = {
const steps = m.steps || []
const updatedSteps = [...steps]
+ const currentStepId = `step-${ctx.refs.currentStepIndex.value}-live`
const currentStepData: IMessageStep = {
- id: `step-${ctx.refs.currentStepIndex.value}-live`,
+ id: currentStepId,
content: ctx.refs.streamingText.value,
workflowSteps: [...ctx.refs.currentStepWorkflow.value],
isActive: true,
timestamp: Date.now(),
}
- if (ctx.refs.currentStepIndex.value >= 0 && ctx.refs.currentStepIndex.value < updatedSteps.length) {
- updatedSteps[ctx.refs.currentStepIndex.value] = currentStepData
+ // 通过 ID 查找已存在的步骤,而不是使用数组索引
+ // 这样可以正确处理 stepIndex 跳过某些值的情况
+ const existingStepIndex = updatedSteps.findIndex(
+ (s: IMessageStep) => s.id === currentStepId || s.id === `step-${ctx.refs.currentStepIndex.value}-saved`,
+ )
+
+ if (existingStepIndex >= 0) {
+ // 更新已存在的步骤
+ updatedSteps[existingStepIndex] = currentStepData
}
- else if (ctx.refs.currentStepIndex.value >= updatedSteps.length) {
- // 当 stepIndex >= steps.length 时,添加新步骤
- // 这处理了 stepIndex > steps.length 的情况(中间可能有跳过的空步骤)
+ else {
+ // 添加新步骤
updatedSteps.push(currentStepData)
}