Skip to content
Open
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
41 changes: 27 additions & 14 deletions apps/mobile/src/features/threads/ThreadQueueControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
queuedRunEditBlockedReason,
} from "@t3tools/client-runtime/queued-run-edit";
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
import {
isAtomCommandInterrupted,
squashAtomCommandFailure,
} from "@t3tools/client-runtime/state/runtime";
import { resolveUserMessagePresentation } from "@t3tools/client-runtime/user-message";
import type { ChatAttachment, EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
import { Atom } from "effect/reactivity";
Expand All @@ -20,6 +24,7 @@ import { useUniwindTheme } from "../../lib/useUniwindTheme";
import { useSafeAreaInsets } from "react-native-safe-area-context";

import { Button } from "../../components/ui/Button";
import * as Dialog from "../../components/ui/Dialog";
import { SheetHeader } from "../../components/ui/SheetHeader";
import { AppText as Text } from "../../components/ui/AppText";
import { SymbolView } from "../../components/ui/AppSymbol";
Expand Down Expand Up @@ -178,22 +183,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
50 changes: 48 additions & 2 deletions apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
import {
CommandId,
EnvironmentId,
MessageId,
OrchestrationV2DispatchCommandError,
ThreadId,
} from "@t3tools/contracts";
import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
import * as Cause from "effect/Cause";
import * as DateTime from "effect/DateTime";
import { AsyncResult } from "effect/reactivity";
import type { ComponentProps } from "react";
import { expect, it, vi } from "vite-plus/test";
import { page } from "vite-plus/test/browser";

const state = vi.hoisted(() => ({
projection: null as unknown,
workflow: null as unknown,
commandResult: undefined as unknown,
}));

vi.mock(import("@t3tools/client-runtime/state/thread-workflows"), async (importOriginal) => ({
Expand All @@ -31,7 +40,7 @@ vi.mock(import("../../state/query"), 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 @@ -42,11 +51,13 @@ vi.mock(import("../../assets/assetUrls"), async (importOriginal) => ({

import "~/index.css";
import { render as renderUi } from "../../../test/render";
import { ToastProvider } from "../ui/toast";
import { QueuedRunsControl } from "./QueuedRunsControl";

function setup() {
state.projection = null;
state.workflow = null;
state.commandResult = undefined;
}

const baseProps = {
Expand Down Expand Up @@ -203,3 +214,38 @@ it("stays inside the timeline scroller instead of growing the chat column", asyn
const column = screen.getByTestId("column").element();
expect(column.scrollHeight).toBe(column.clientHeight);
});

it("tells the user why Send now was rejected", async () => {
setup();
const reason = "Target run run:active is stopping and cannot be steered.";
state.projection = { projection: { messages: [] } };
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 @@ -33,6 +37,7 @@ import { useAtomCommand } from "../../state/use-atom-command";
import { isImageAttachment, 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 @@ -176,19 +181,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