Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions apps/mobile/src/features/threads/ThreadQueueControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,30 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
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 = (
Expand Down
48 changes: 46 additions & 2 deletions apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -16,6 +23,7 @@ import { page } from "vite-plus/test/browser";
const state = vi.hoisted(() => ({
threadState: null as unknown as Atom.Writable<AsyncResult.AsyncResult<EnvironmentThreadState>>,
workflow: null as unknown,
commandResult: undefined as unknown,
}));

vi.mock(import("@t3tools/client-runtime/state/thread-workflows"), async (importOriginal) => ({
Expand Down Expand Up @@ -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) => ({
Expand All @@ -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 = {
Expand Down Expand Up @@ -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(
<ToastProvider timeout={0}>
<QueuedRunsControl {...baseProps} />
</ToastProvider>,
);

await page.getByRole("button", { name: "Send now" }).click();
await vi.waitFor(() => expect(page.getByText(reason).element()).toBeVisible());
});
35 changes: 24 additions & 11 deletions apps/web/src/components/chat/QueuedRunsControl.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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, () => ({
Expand Down
Loading