diff --git a/apps/mobile/src/features/threads/ThreadQueueControl.tsx b/apps/mobile/src/features/threads/ThreadQueueControl.tsx index c251283ef8839..16ae5b6cf8093 100644 --- a/apps/mobile/src/features/threads/ThreadQueueControl.tsx +++ b/apps/mobile/src/features/threads/ThreadQueueControl.tsx @@ -191,22 +191,30 @@ export function ThreadQueueSheet({ route }: StaticScreenProps) { busyRef.current = true; setBusyRunId(runId); void Haptics.selectionAsync(); - if (action === "remove") { - await cancel(buildCancelQueuedRunCommand({ ...target, runId })); - } else if (workflow?.canSendNow) { - await resume({ ...target, input: { threadId: target.threadId, runId } }); - } else if (workflow?.activeRun && workflow.canPromoteToSteer) { - await promote({ - ...target, - input: { - threadId: target.threadId, - queuedRunId: runId, - targetRunId: workflow.activeRun.id, - }, - }); - } + const result = + action === "remove" + ? await cancel(buildCancelQueuedRunCommand({ ...target, runId })) + : workflow?.canSendNow + ? await resume({ ...target, input: { threadId: target.threadId, runId } }) + : workflow?.activeRun && workflow.canPromoteToSteer + ? await promote({ + ...target, + input: { + threadId: target.threadId, + queuedRunId: runId, + targetRunId: workflow.activeRun.id, + }, + }) + : null; busyRef.current = false; setBusyRunId(null); + if (action === "steer" && result?._tag === "Failure" && !isAtomCommandInterrupted(result)) { + const error = squashAtomCommandFailure(result); + Dialog.alert( + "Could not send queued message", + error instanceof Error ? error.message : "Try again.", + ); + } }; const content = ( diff --git a/apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx b/apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx index 8781327b5e8ad..4f4daf7c798b4 100644 --- a/apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx +++ b/apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx @@ -1,11 +1,18 @@ import { RegistryContext } from "@effect/atom-react"; import type { EnvironmentThreadState } from "@t3tools/client-runtime/state/threads"; -import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts"; +import { + CommandId, + EnvironmentId, + MessageId, + OrchestrationV2DispatchCommandError, + ThreadId, +} from "@t3tools/contracts"; import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations"; import { formatComposerContextReference, toComposerContextId, } from "@t3tools/shared/composerContextReferences"; +import * as Cause from "effect/Cause"; import * as DateTime from "effect/DateTime"; import * as Option from "effect/Option"; import { AsyncResult, type Atom, AtomRegistry } from "effect/reactivity"; @@ -16,6 +23,7 @@ import { page } from "vite-plus/test/browser"; const state = vi.hoisted(() => ({ threadState: null as unknown as Atom.Writable>, workflow: null as unknown, + commandResult: undefined as unknown, })); vi.mock(import("@t3tools/client-runtime/state/thread-workflows"), async (importOriginal) => ({ @@ -66,7 +74,7 @@ vi.mock(import("../../state/threads"), async (importOriginal) => { vi.mock(import("../../state/use-atom-command"), async (importOriginal) => ({ ...(await importOriginal()), - useAtomCommand: (() => async () => undefined) as never, + useAtomCommand: (() => async () => state.commandResult) as never, })); vi.mock(import("../../assets/assetUrls"), async (importOriginal) => ({ @@ -80,12 +88,14 @@ import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/th import { act, render as renderUi } from "../../../test/render"; import { makeStreamingTimelineFixture, makeThreadProjectionFixture } from "../../test-fixtures"; +import { ToastProvider } from "../ui/toast"; import { QueuedRunsControl } from "./QueuedRunsControl"; import { ExpandedImageDialog } from "./ExpandedImageDialog"; import type { ExpandedImagePreview } from "./ExpandedImagePreview"; function setup() { state.workflow = null; + state.commandResult = undefined; } const baseProps = { @@ -399,3 +409,37 @@ it("does not re-render while assistant text streams into the thread", async () = await act(() => registry.set(state.threadState, thread("Hello there", true))); expect(renders).toBeGreaterThan(settled); }); + +it("tells the user why Send now was rejected", async () => { + setup(); + const reason = "Target run run:active is stopping and cannot be steered."; + state.workflow = { + activeRun: { id: "run:active" }, + canPromoteToSteer: true, + queuedRuns: [ + { + run: { id: "run:queued", userMessageId: "message:queued" }, + text: "Follow up", + attachments: [], + }, + ], + }; + state.commandResult = AsyncResult.failure( + Cause.fail( + new OrchestrationV2DispatchCommandError({ + commandId: CommandId.make("command:promote"), + commandType: "queued-message.promote-to-steer", + message: reason, + detail: reason, + }), + ), + ); + await using _screen = await renderUi( + + + , + ); + + await page.getByRole("button", { name: "Send now" }).click(); + await vi.waitFor(() => expect(page.getByText(reason).element()).toBeVisible()); +}); diff --git a/apps/web/src/components/chat/QueuedRunsControl.tsx b/apps/web/src/components/chat/QueuedRunsControl.tsx index 8972bf468448b..49cbc8f050eea 100644 --- a/apps/web/src/components/chat/QueuedRunsControl.tsx +++ b/apps/web/src/components/chat/QueuedRunsControl.tsx @@ -1,4 +1,8 @@ import { scopeThreadRef } from "@t3tools/client-runtime/environment"; +import { + isAtomCommandInterrupted, + squashAtomCommandFailure, +} from "@t3tools/client-runtime/state/runtime"; import { deriveThreadQueueWorkflowState } from "@t3tools/client-runtime/state/thread-workflows"; import { assistantCitationsToPlainText } from "@t3tools/shared/assistantCitations"; import { @@ -34,6 +38,7 @@ import { useAtomCommand } from "../../state/use-atom-command"; import { isImageAttachment, type ChatImageAttachment, type ChatMessage } from "../../types"; import { cn } from "~/lib/utils"; import { Button, InlineButton } from "../ui/button"; +import { toastManager } from "../ui/toast"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import type { TimestampFormat } from "@t3tools/contracts/settings"; import { GuestMessageName } from "./guestMessageStyle"; @@ -168,19 +173,27 @@ export function QueuedRunsControl({ if (!canSendRun(freshContext) || steerInFlightRef.current) return; steerInFlightRef.current = true; setBusyRunId(queuedRunId); - if (workflow?.canSendNow) { - await resume({ - environmentId: props.environmentId, - input: { threadId: props.threadId, runId: queuedRunId }, - }); - } else if (activeRun !== null) { - await promote({ - environmentId: props.environmentId, - input: { threadId: props.threadId, queuedRunId, targetRunId: activeRun.id }, - }); - } + const result = workflow?.canSendNow + ? await resume({ + environmentId: props.environmentId, + input: { threadId: props.threadId, runId: queuedRunId }, + }) + : activeRun !== null + ? await promote({ + environmentId: props.environmentId, + input: { threadId: props.threadId, queuedRunId, targetRunId: activeRun.id }, + }) + : null; steerInFlightRef.current = false; setBusyRunId(null); + if (result?._tag === "Failure" && !isAtomCommandInterrupted(result)) { + const error = squashAtomCommandFailure(result); + toastManager.add({ + type: "error", + title: "Could not send queued message", + description: error instanceof Error ? error.message : "An error occurred.", + }); + } }; useImperativeHandle(ref, () => ({