Skip to content

Let's use AI to Earn!

fix(web): action card persistence and status (#446)

yika7d98530bd924
diff --git a/project/web/src/api/agent.ts b/project/web/src/api/agent.ts
index 5f7a12ac..455d5190 100644
--- a/project/web/src/api/agent.ts
+++ b/project/web/src/api/agent.ts
@@ -13,7 +13,9 @@ export enum TaskStatus {
GeneratingImage = 'GENERATING_IMAGE', // 图片生成中
GeneratingVideo = 'GENERATING_VIDEO', // 视频生成中
GeneratingText = 'GENERATING_TEXT', // 文本生成中
+ Running = 'running', // 运行中
Completed = 'COMPLETED', // 完成
+ RequiresAction = 'requires_action', // 需要用户操作(如绑定频道等)
Failed = 'FAILED', // 失败
Cancelled = 'CANCELLED', // 取消
}
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 d1890c12..fc044bc4 100644
--- a/project/web/src/app/[lng]/chat/[taskId]/utils/convertMessages.ts
+++ b/project/web/src/app/[lng]/chat/[taskId]/utils/convertMessages.ts
@@ -13,7 +13,9 @@ import { parseUserMessageContent } from './parseMessageContent'
* - user: { type: 'user', content: [{ type: 'text', text: '...' }] }
* - assistant: { type: 'assistant', uuid: '...', message: { content: [{ type: 'text', text: '...' }] } }
* - stream_event: 流式事件,用于提取工具调用信息
- * - result: { type: 'result', message: '...' } - message 字段直接包含文本内容
+ * - result: { type: 'result', message: '...', result: { action: '...', platform: '...', ... } }
+ * - message 字段包含文本内容
+ * - result 字段(根级别)包含 action 数据(如 createChannel、updateChannel 等)
*
* 改进:解析多步骤和工作流步骤
* - message_start 事件标识新步骤开始
@@ -335,7 +337,8 @@ function processResultMessage(
index: number,
displayMessages: IDisplayMessage[],
): { contentToAdd?: string; newAssistantMsgIndex?: number; actions?: any[] } {
- const msgData = (msg as any).message
+ const msgAny = msg as any
+ const msgData = msgAny.message
let content = ''
let actions: any[] = []
@@ -350,24 +353,28 @@ function processResultMessage(
if (msgData.message && typeof msgData.message === 'string') {
content = msgData.message
}
+ }
+
+ // 解析 result 数据中的 action(支持数组和单个对象)
+ // 注意:后端保存的结构是 msg.result(根级别),不是 msg.message.result
+ // 同时兼容两种格式以防万一
+ const resultData = msgAny.result || (msgData && typeof msgData === 'object' ? msgData.result : null)
+
+ if (resultData) {
+ // 统一转换为数组处理
+ const resultArray = Array.isArray(resultData) ? resultData : [resultData]
- // 解析 result 数据中的 action(支持数组和单个对象)
- if (msgData.result) {
- // 统一转换为数组处理
- const resultArray = Array.isArray(msgData.result) ? msgData.result : [msgData.result]
-
- actions = resultArray
- .filter((item: any) => item && item.action) // 只处理有 action 的项
- .map((item: any) => ({
- type: item.action, // 映射 action -> type
- platform: item.platform,
- accountId: item.accountId,
- title: item.title,
- description: item.description,
- medias: item.medias,
- tags: item.tags,
- }))
- }
+ actions = resultArray
+ .filter((item: any) => item && item.action) // 只处理有 action 的项
+ .map((item: any) => ({
+ type: item.action, // 映射 action -> type
+ platform: item.platform,
+ accountId: item.accountId,
+ title: item.title,
+ description: item.description,
+ medias: item.medias,
+ tags: item.tags,
+ }))
}
if (content || actions.length > 0) {
@@ -380,7 +387,7 @@ function processResultMessage(
return { contentToAdd: content || undefined }
} else {
displayMessages.push({
- id: (msg as any).uuid || `result-${index}`,
+ id: msgAny.uuid || `result-${index}`,
role: 'assistant',
content: content || '',
status: 'done',
diff --git a/project/web/src/app/[lng]/chat/[taskId]/utils/taskStatus.ts b/project/web/src/app/[lng]/chat/[taskId]/utils/taskStatus.ts
index 2c2c020e..d5ada2ac 100644
--- a/project/web/src/app/[lng]/chat/[taskId]/utils/taskStatus.ts
+++ b/project/web/src/app/[lng]/chat/[taskId]/utils/taskStatus.ts
@@ -9,10 +9,16 @@ import type { TaskDetail, TaskMessage } from '@/api/agent'
import { TaskStatus } from '@/api/agent'
// 认为这些状态表示任务已结束(不再需要轮询)
-const COMPLETED_STATUSES: TaskStatus[] = [
- TaskStatus.Completed,
- TaskStatus.Failed,
- TaskStatus.Cancelled,
+// 同时支持大写和小写格式(兼容后端返回的格式)
+const COMPLETED_STATUS_VALUES = [
+ 'completed',
+ 'COMPLETED',
+ 'failed',
+ 'FAILED',
+ 'cancelled',
+ 'CANCELLED',
+ 'requires_action', // 需要用户操作也算完成(停止轮询)
+ 'error',
]
/**
@@ -20,7 +26,9 @@ const COMPLETED_STATUSES: TaskStatus[] = [
*/
export function isTaskCompletedByStatus(task: Pick<TaskDetail, 'status'> | null | undefined): boolean {
if (!task) return false
- return COMPLETED_STATUSES.includes(task.status)
+ const statusValue = task.status as string
+ return COMPLETED_STATUS_VALUES.includes(statusValue?.toLowerCase()) ||
+ COMPLETED_STATUS_VALUES.includes(statusValue)
}
/**
diff --git a/project/web/src/app/i18n/locales/en/chat.json b/project/web/src/app/i18n/locales/en/chat.json
index 06161ee4..30002706 100644
--- a/project/web/src/app/i18n/locales/en/chat.json
+++ b/project/web/src/app/i18n/locales/en/chat.json
@@ -23,7 +23,13 @@
"delete": "Delete",
"deleteConfirm": "Are you sure you want to delete this chat?",
"deleteSuccess": "Deleted successfully",
- "deleteFailed": "Delete failed"
+ "deleteFailed": "Delete failed",
+ "status": {
+ "running": "Running",
+ "completed": "Completed",
+ "requiresAction": "Action Required",
+ "failed": "Failed"
+ }
},
"home": {
"title": "How can I help you?",
diff --git a/project/web/src/app/i18n/locales/zh-CN/chat.json b/project/web/src/app/i18n/locales/zh-CN/chat.json
index 8a623126..9682f45e 100644
--- a/project/web/src/app/i18n/locales/zh-CN/chat.json
+++ b/project/web/src/app/i18n/locales/zh-CN/chat.json
@@ -23,7 +23,13 @@
"delete": "删除",
"deleteConfirm": "确定要删除这个对话吗?",
"deleteSuccess": "删除成功",
- "deleteFailed": "删除失败"
+ "deleteFailed": "删除失败",
+ "status": {
+ "running": "运行中",
+ "completed": "已完成",
+ "requiresAction": "需要操作",
+ "failed": "失败"
+ }
},
"home": {
"title": "有什么可以帮你的?",
diff --git a/project/web/src/components/Chat/TaskCard/index.tsx b/project/web/src/components/Chat/TaskCard/index.tsx
index 41bc69b2..cb28c9e5 100644
--- a/project/web/src/components/Chat/TaskCard/index.tsx
+++ b/project/web/src/components/Chat/TaskCard/index.tsx
@@ -8,9 +8,10 @@
import type React from 'react'
import { useState } from 'react'
import { useRouter } from 'next/navigation'
-import { Loader2, MessageSquare, MoreHorizontal, Trash2 } from 'lucide-react'
+import { Loader2, MessageSquare, MoreHorizontal, Trash2, AlertCircle, CheckCircle2 } from 'lucide-react'
import { cn, formatRelativeTime } from '@/lib/utils'
import { useGetClientLng } from '@/hooks/useSystem'
+import { useTransClient } from '@/app/i18n/client'
import {
DropdownMenu,
DropdownMenuContent,
@@ -35,6 +36,45 @@ export interface ITaskCardProps {
className?: string
}
+/** 获取状态显示配置 */
+function getStatusConfig(status: string | undefined, t: (key: string) => string) {
+ const normalizedStatus = status?.toLowerCase()
+
+ switch (normalizedStatus) {
+ case 'requires_action':
+ return {
+ label: t('task.status.requiresAction') || 'Requires Action',
+ className: 'bg-amber-100 dark:bg-amber-900/30 text-amber-700 dark:text-amber-400 border-amber-200 dark:border-amber-800',
+ icon: AlertCircle,
+ }
+ case 'completed':
+ return {
+ label: t('task.status.completed') || 'Completed',
+ className: 'bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-400 border-green-200 dark:border-green-800',
+ icon: CheckCircle2,
+ }
+ case 'running':
+ return {
+ label: t('task.status.running') || 'Running',
+ className: 'bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-400 border-blue-200 dark:border-blue-800',
+ icon: Loader2,
+ }
+ case 'error':
+ case 'failed':
+ return {
+ label: t('task.status.failed') || 'Failed',
+ className: 'bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-400 border-red-200 dark:border-red-800',
+ icon: AlertCircle,
+ }
+ default:
+ return {
+ label: status || '',
+ className: 'bg-muted text-muted-foreground border-border',
+ icon: null,
+ }
+ }
+}
+
/**
* TaskCard - 任务卡片组件
*/
@@ -49,7 +89,10 @@ export function TaskCard({
}: ITaskCardProps) {
const router = useRouter()
const lng = useGetClientLng()
+ const { t } = useTransClient('chat')
const [isDeleting, setIsDeleting] = useState(false)
+
+ const statusConfig = getStatusConfig(status, t as (key: string) => string)
/** 跳转到对话详情页 */
const handleClick = () => {
@@ -95,9 +138,18 @@ export function TaskCard({
<span className="text-xs text-muted-foreground">
{formatRelativeTime(new Date(updatedAt || createdAt))}
</span>
- {status && (
- <span className="inline-flex items-center rounded-full border border-border px-2 py-0.5 text-[11px] font-medium text-muted-foreground">
- {status}
+ {status && statusConfig.label && (
+ <span className={cn(
+ 'inline-flex items-center gap-1 rounded-full border px-2 py-0.5 text-[11px] font-medium',
+ statusConfig.className,
+ )}>
+ {statusConfig.icon && (
+ <statusConfig.icon className={cn(
+ 'w-3 h-3',
+ status?.toLowerCase() === 'running' && 'animate-spin',
+ )} />
+ )}
+ {statusConfig.label}
</span>
)}
</div>
diff --git a/project/web/src/store/agent/handlers/sse.handlers.ts b/project/web/src/store/agent/handlers/sse.handlers.ts
index 779a2bdf..15b07c7e 100644
--- a/project/web/src/store/agent/handlers/sse.handlers.ts
+++ b/project/web/src/store/agent/handlers/sse.handlers.ts
@@ -143,10 +143,6 @@ export const textDeltaHandler: ISSEHandler = {
messages: state.messages.map((m: any) => {
if (m.id === ctx.refs.currentAssistantMessageId.value) {
const steps = m.steps || []
- const allContent = steps.map((s: IMessageStep) => s.content).join('\n\n')
- const totalContent = allContent
- ? allContent + '\n\n' + ctx.refs.streamingText.value
- : ctx.refs.streamingText.value
let updatedSteps = [...steps]
const currentStepData: IMessageStep = {
@@ -163,6 +159,9 @@ export const textDeltaHandler: ISSEHandler = {
updatedSteps.push(currentStepData)
}
+ // 从更新后的 steps 计算 content,避免重复
+ const totalContent = updatedSteps.map((s: IMessageStep) => s.content).join('\n\n')
+
return {
...m,
content: totalContent,
diff --git a/project/web/src/store/agent/utils/message.ts b/project/web/src/store/agent/utils/message.ts
index d4737250..01cc6cd2 100644
--- a/project/web/src/store/agent/utils/message.ts
+++ b/project/web/src/store/agent/utils/message.ts
@@ -84,12 +84,12 @@ export function createMessageUtils(ctx: IMessageContext) {
},
/**
- * 更新当前 assistant 消息的 actions
+ * 更新当前 assistant 消息的 actions(同时标记为完成)
*/
updateMessageActions(actions: IActionCard[]) {
set((state: any) => ({
messages: state.messages.map((m: any) =>
- m.id === refs.currentAssistantMessageId.value ? { ...m, actions } : m,
+ m.id === refs.currentAssistantMessageId.value ? { ...m, actions, status: 'done' } : m,
),
}))
},