From cff58df175e084a7baa99f51f8eb4f0c4bf9c4e6 Mon Sep 17 00:00:00 2001 From: Whitewater Date: Mon, 3 Nov 2025 16:47:17 +0800 Subject: [PATCH] feat: enhance test run functionality with stream resume (#4648) * feat(ai): enhance test run functionality with session management * fix: ensure session ID is returned from test run mutation * chore: improve success message for test run * fix(task): handle test run errors and improve user feedback Added error handling for test run failures, dismissing the loading toast and providing specific error messages. Introduced a delay before switching to chat to ensure backend readiness. * chore: update test start message for clarity in multiple languages --- .../modules/ai-task/components/task-item.tsx | 44 ++++++++++++------- locales/ai/en.json | 4 +- locales/ai/ja.json | 4 +- locales/ai/zh-CN.json | 4 +- locales/ai/zh-TW.json | 4 +- 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/ai-task/components/task-item.tsx b/apps/desktop/layer/renderer/src/modules/ai-task/components/task-item.tsx index 246b11fde..f4d420776 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-task/components/task-item.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-task/components/task-item.tsx @@ -1,4 +1,4 @@ -import { cn } from "@follow/utils/utils" +import { cn, sleep } from "@follow/utils/utils" import type { AITask, TaskSchedule } from "@follow-app/client-sdk" import dayjs from "dayjs" import type { i18n, TFunction } from "i18next" @@ -8,7 +8,8 @@ import { toast } from "sonner" import { setAIPanelVisibility } from "~/atoms/settings/ai" import { useDialog, useModalStack } from "~/components/ui/modal/stacked/hooks" -import { followApi } from "~/lib/api-client" +import { toastFetchError } from "~/lib/error-parser" +import { AIPersistService } from "~/modules/ai-chat/services" import { ChatSliceActions } from "~/modules/ai-chat/store/chat-core/chat-actions" import { AIChatSessionService } from "~/modules/ai-chat-session" import { useAIChatSessionListQuery } from "~/modules/ai-chat-session/query" @@ -180,24 +181,37 @@ export const TaskItem = memo(({ task }: { task: AITask }) => { if (testRunResult.data.error) { throw new Error(testRunResult.data.error) } + const { sessionId } = testRunResult.data + if (!sessionId) { + throw new Error("No session ID returned from test run") + } + + // Ensure the session exists in local DB + await AIPersistService.ensureSession(sessionId, { + title: task.name, + createdAt: new Date(), + updatedAt: new Date(), + }) + + // Switch to the chat + setAIPanelVisibility(true) + const chatActions = ChatSliceActions.getActiveInstance() + if (chatActions) { + await sleep(1500) // wait for backend stream to be ready + chatActions.switchToChat(sessionId) + } + toast.success(t("tasks.toast.test_success"), { id: loadingId, }) - // @ts-ignore -- Remove once SDK types are updated - const { sessionId } = testRunResult.data - if (sessionId) { - const session = ( - await followApi.aiChatSessions.get({ - chatId: sessionId, - }) - ).data - await AIChatSessionService.fetchAndPersistMessages(session) - } } catch (error) { console.error("Failed to run test:", error) - toast.error(t("tasks.toast.test_failed"), { - id: loadingId, - }) + toast.dismiss(loadingId) + if (error instanceof Error) { + toastFetchError(error) + return + } + toast.error(t("tasks.toast.test_failed")) } }, title: t("tasks.actions.test_run"), diff --git a/locales/ai/en.json b/locales/ai/en.json index bfa370b44..935beeb78 100644 --- a/locales/ai/en.json +++ b/locales/ai/en.json @@ -268,8 +268,8 @@ "tasks.toast.no_report": "No report session found for this task yet.", "tasks.toast.switch_to_chat": "Switch to the chat panel to view reports.", "tasks.toast.test_failed": "Failed to run test. Please try again.", - "tasks.toast.test_start": "Starting test run… This may take about 1 minute. You can view the result in the Chat panel.", - "tasks.toast.test_success": "Test run finished. You can view the report in the Chat panel.", + "tasks.toast.test_start": "Running...", + "tasks.toast.test_success": "The test ran successfully. You can switch to the chat panel to view the report.", "tasks.toast.update_error": "Failed to update AI task. Please try again.", "tasks.toast.update_failed": "Failed to update task. Please try again.", "tasks.toast.updated": "AI task updated successfully", diff --git a/locales/ai/ja.json b/locales/ai/ja.json index 92bcd6abc..4d32a63e1 100644 --- a/locales/ai/ja.json +++ b/locales/ai/ja.json @@ -233,8 +233,8 @@ "tasks.toast.no_report": "このタスクのレポートセッションはまだありません。", "tasks.toast.switch_to_chat": "レポートを見るにはチャットパネルに切り替えてください。", "tasks.toast.test_failed": "テスト実行に失敗しました。もう一度お試しください。", - "tasks.toast.test_start": "テスト実行を開始…約1分かかる場合があります。結果はチャットパネルで確認できます。", - "tasks.toast.test_success": "テスト実行が完了しました。レポートはチャットパネルで確認できます。", + "tasks.toast.test_start": "実行中...", + "tasks.toast.test_success": "テストは正常に実行されました。チャットパネルに切り替えてレポートを確認できます。", "tasks.toast.update_error": "AIタスクの更新に失敗しました。もう一度お試しください。", "tasks.toast.update_failed": "タスクの更新に失敗しました。もう一度お試しください。", "tasks.toast.updated": "AIタスクが正常に更新されました", diff --git a/locales/ai/zh-CN.json b/locales/ai/zh-CN.json index 4941c4820..a66dda725 100644 --- a/locales/ai/zh-CN.json +++ b/locales/ai/zh-CN.json @@ -261,8 +261,8 @@ "tasks.toast.no_report": "此任务尚无报告会话。", "tasks.toast.switch_to_chat": "切换到聊天面板查看报告。", "tasks.toast.test_failed": "测试运行失败,请重试。", - "tasks.toast.test_start": "开始测试运行…大约需要 1 分钟。您可以在聊天面板查看结果。", - "tasks.toast.test_success": "测试运行完成。您可以在聊天面板查看报告。", + "tasks.toast.test_start": "正在运行…", + "tasks.toast.test_success": "测试成功运行。您可以切换到聊天面板查看报告。", "tasks.toast.update_error": "更新 AI 任务失败,请重试。", "tasks.toast.update_failed": "更新任务失败,请重试。", "tasks.toast.updated": "AI 任务更新成功", diff --git a/locales/ai/zh-TW.json b/locales/ai/zh-TW.json index 83b2618bc..2bf4cd30e 100644 --- a/locales/ai/zh-TW.json +++ b/locales/ai/zh-TW.json @@ -252,8 +252,8 @@ "tasks.toast.no_report": "此任務尚無報告。", "tasks.toast.switch_to_chat": "切換到聊天面板檢視報告。", "tasks.toast.test_failed": "測試執行失敗,請重試。", - "tasks.toast.test_start": "開始測試執行...大約需要 1 分鐘。您可以在聊天面板檢視結果。", - "tasks.toast.test_success": "測試執行完成。您可以在聊天面板檢視報告。", + "tasks.toast.test_start": "開始執行...", + "tasks.toast.test_success": "報測試已成功執行。您可以切換到聊天面板查看告。", "tasks.toast.update_error": "更新 AI 任務失敗,請重試。", "tasks.toast.update_failed": "更新任務失敗,請重試。", "tasks.toast.updated": "AI 任務更新成功",