From 9e7da52b0cb4d3848677b0aadf481891d049a5d9 Mon Sep 17 00:00:00 2001 From: LynPtl Date: Sun, 29 Nov 2026 19:17:18 +0800 Subject: [PATCH 1/6] feat(canvas): notify agent and generation task outcomes - Announce user-started Agent turns when they finish, fail, or ask a question. - Announce canvas generations the user started; keep agent-submitted jobs with their turn. - Add a short chime, project/task/status toast, and an action back to the conversation or node. - Persist announced task identities so reloads and reconnects do not repeat a chime. - Cover the decision rule, origin filtering, and reload dedupe with tests. - Add zh-CN and en-US catalogs. Fixes #535 --- docs/development/i18n.md | 1 + src/backend/creationJobs.ts | 1 + src/canvas/AssistantPanel.tsx | 85 +++++++++++ src/canvas/CanvasNotifications.tsx | 86 ++++++++--- src/canvas/generationHistory.ts | 2 + src/canvas/taskNotifications.ts | 139 ++++++++++++++++++ src/canvas/taskSound.ts | 72 +++++++++ src/canvas/workspace.css | 33 ++++- src/canvas/workspace/WorkspaceView.tsx | 28 ++++ src/canvas/workspace/runtimeTypes.ts | 3 +- .../workspace/useWorkspaceGeneration.ts | 114 +++++++++++++- src/i18n/messages/en-US/index.ts | 2 + src/i18n/messages/en-US/taskNotifications.ts | 11 ++ src/i18n/messages/zh-CN/index.ts | 2 + src/i18n/messages/zh-CN/taskNotifications.ts | 11 ++ tests/creationJobs.test.ts | 9 ++ tests/taskNotifications.test.ts | 108 ++++++++++++++ 17 files changed, 680 insertions(+), 27 deletions(-) create mode 100644 src/canvas/taskNotifications.ts create mode 100644 src/canvas/taskSound.ts create mode 100644 src/i18n/messages/en-US/taskNotifications.ts create mode 100644 src/i18n/messages/zh-CN/taskNotifications.ts create mode 100644 tests/taskNotifications.test.ts diff --git a/docs/development/i18n.md b/docs/development/i18n.md index fc0837253..04723b98a 100644 --- a/docs/development/i18n.md +++ b/docs/development/i18n.md @@ -107,6 +107,7 @@ npm run check:i18n | 错误、状态与后端回执 | [errors.ts](../../src/i18n/messages/zh-CN/errors.ts) | [errors.ts](../../src/i18n/messages/en-US/errors.ts) | | 后端请求错误 | [api.ts](../../src/i18n/messages/zh-CN/api.ts) | [api.ts](../../src/i18n/messages/en-US/api.ts) | | 工具编辑器 | [tools.ts](../../src/i18n/messages/zh-CN/tools.ts) | [tools.ts](../../src/i18n/messages/en-US/tools.ts) | +| 任务通知 | [taskNotifications.ts](../../src/i18n/messages/zh-CN/taskNotifications.ts) | [taskNotifications.ts](../../src/i18n/messages/en-US/taskNotifications.ts) | 审校时重点看:英文长度是否导致按钮换行、专有名词是否统一、错误文案是否给出下一步动作、数量与占位符是否自然。 diff --git a/src/backend/creationJobs.ts b/src/backend/creationJobs.ts index 59e4619ca..0c0d89482 100644 --- a/src/backend/creationJobs.ts +++ b/src/backend/creationJobs.ts @@ -56,6 +56,7 @@ export function creationJobs( nodeId: job.originItemId ?? spec.id, title: spec.prompt.slice(0, 60) || tNow("errors.backend.generationTask"), taskId: job.id, + ...(job.agentRunId ? { agentRunId: job.agentRunId } : {}), status: job.status === "queued" || job.status === "running" ? "running" diff --git a/src/canvas/AssistantPanel.tsx b/src/canvas/AssistantPanel.tsx index f21454680..976f83522 100644 --- a/src/canvas/AssistantPanel.tsx +++ b/src/canvas/AssistantPanel.tsx @@ -42,6 +42,12 @@ import { useProjectMentionAssets } from "./useProjectMentionAssets"; import { useReferenceUploads } from "./useReferenceUploads.tsx"; import { useT } from "../i18n/I18nProvider.tsx"; import type { MessageKey } from "../i18n/types.ts"; +import { + agentTaskNotificationKey, + agentTaskOutcome, + TaskNotificationLedger, + type AgentTaskNotice, +} from "./taskNotifications.ts"; import { AssetMedia } from "./useAssetMedia.tsx"; function kindOf(asset: ChatAsset) { @@ -67,7 +73,9 @@ export function AssistantPanel({ layout, selectionRequest, onClose, + onOpen, onTurnEnd, + onTaskNotice, onModelSelections, onAgentModel, onOpenSettings, @@ -84,7 +92,9 @@ export function AssistantPanel({ layout: AssistantLayout; selectionRequest: number; onClose: () => void; + onOpen: () => void; onTurnEnd: () => void; + onTaskNotice: (notice: AgentTaskNotice) => void; /** Lets the canvas default new nodes to the model this dialog is using. */ onModelSelections?: (selections: ModelSelections) => void; onAgentModel?: (projectId: string, selection: AssistantModelSelection | null) => void; @@ -177,6 +187,81 @@ export function AssistantPanel({ } return changed; }; + const taskWatch = useRef({ + documentId, + openedAt: Date.now(), + ledger: new TaskNotificationLedger(), + observed: new Map(), + }); + if (taskWatch.current.documentId !== documentId) + taskWatch.current = { + documentId, + openedAt: Date.now(), + ledger: new TaskNotificationLedger(), + observed: new Map(), + }; + useEffect(() => { + if (!chat.loaded || chat.changing) return; + const watch = taskWatch.current; + for (const message of value.messages) { + if (message.role !== "assistant") continue; + const outcome = agentTaskOutcome(message); + const previous = watch.observed.get(message.id); + watch.observed.set(message.id, message.status); + if (!outcome) continue; + const finishedAt = message.finishedAt + ? Date.parse(message.finishedAt) + : undefined; + const fresh = + previous === "streaming" || + (previous === undefined && + finishedAt !== undefined && + finishedAt >= watch.openedAt); + if (!fresh) continue; + const conversationId = value.conversationId; + const key = agentTaskNotificationKey( + documentId, + conversationId, + message.id, + outcome, + ); + if (!watch.ledger.claim(key)) continue; + const index = value.messages.indexOf(message); + const userMessage = [...value.messages.slice(0, index)] + .reverse() + .find((item) => item.role === "user"); + const thread = chat.threads.find( + (item) => item.id === conversationId, + ); + const task = + thread?.title?.trim() || + userMessage?.text.trim().slice(0, 80) || + t("assistant.sessions.untitled"); + onTaskNotice({ + key, + outcome, + project: documentTitle, + task, + run: () => { + onOpen(); + if (latest.current.value.conversationId !== conversationId) + void chat.changeConversation(conversationId); + requestAnimationFrame(() => editor.current?.focus()); + }, + }); + } + }, [ + chat.loaded, + chat.changing, + chat.threads, + chat.value.conversationId, + documentId, + documentTitle, + onOpen, + onTaskNotice, + t, + value.messages, + ]); useEffect(() => { if (!open || chat.sending || !value.draftContent) return; const content = currentReferences( diff --git a/src/canvas/CanvasNotifications.tsx b/src/canvas/CanvasNotifications.tsx index b8bd65fea..76b21fbec 100644 --- a/src/canvas/CanvasNotifications.tsx +++ b/src/canvas/CanvasNotifications.tsx @@ -1,26 +1,51 @@ import { useCallback, useEffect, useRef, useState } from "react"; import { CanvasIcon } from "./CanvasIcon"; import { useT } from "../i18n/I18nProvider"; +import { installTaskSoundUnlock, playTaskSound } from "./taskSound.ts"; +import type { TaskOutcome } from "./taskNotifications.ts"; + +export interface CanvasNotificationAction { + label: string; + run: () => void; +} + +export interface CanvasNotifyOptions { + /** A failure, drawn in the danger colour and announced as an alert. */ + error?: boolean; + /** Project or task name shown above the message. */ + title?: string; + /** A task outcome that also plays its short chime. */ + sound?: TaskOutcome; + action?: CanvasNotificationAction; +} interface CanvasNotification { id: number; message: string; - /** A failure, drawn in the danger colour and announced as an alert. */ error?: boolean; + title?: string; + action?: CanvasNotificationAction; } export function useCanvasNotifications() { const [notifications, setNotifications] = useState([]); const sequence = useRef(0); - const notify = useCallback((message: string, options?: { error?: boolean }) => { - if (!message) return; - const notification = { - id: ++sequence.current, - message, - ...(options?.error ? { error: true } : {}), - }; - setNotifications((current) => [...current, notification]); - }, []); + useEffect(() => installTaskSoundUnlock(), []); + const notify = useCallback( + (message: string, options?: CanvasNotifyOptions) => { + if (!message) return; + const notification = { + id: ++sequence.current, + message, + ...(options?.error ? { error: true } : {}), + ...(options?.title ? { title: options.title } : {}), + ...(options?.action ? { action: options.action } : {}), + }; + if (options?.sound) playTaskSound(options.sound); + setNotifications((current) => [...current, notification]); + }, + [], + ); const dismiss = useCallback((id: number) => { setNotifications((current) => current.filter((item) => item.id !== id)); }, []); @@ -40,7 +65,14 @@ function Notification({ const [exiting, setExiting] = useState(false); const paused = hovered || focused; const remaining = useRef( - Math.max(3800, Math.min(8000, notification.message.length * 100)), + Math.max( + 3800, + Math.min( + notification.action ? 12000 : 8000, + (notification.title?.length ?? 0) + + notification.message.length * 100, + ), + ), ); useEffect(() => { @@ -76,12 +108,32 @@ function Notification({ setFocused(false); }} > - - {notification.message} - +
+ {notification.title && ( + {notification.title} + )} + + {notification.message} + + {notification.action && ( + + )} +
+

+ {notifications.sound ? t("common.enabled") : t("common.disabled")} +

+ + +
+
+ +

{t("settings.taskPopupHint")}

+
+
+ +

+ {notifications.popup ? t("common.enabled") : t("common.disabled")} +

+
+
+
+
+ +

{t("settings.taskVolumeHint")}

+
+
+ + setTaskNotificationPreference("volume", Number(event.target.value)) + } + /> + {notifications.volume}% + +
+