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
57 changes: 56 additions & 1 deletion apps/web/src/components/ChatView.logic.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId, TurnId } from "@t3tools/contracts";
import {
EnvironmentId,
MessageId,
ProjectId,
ProviderInstanceId,
ThreadId,
TurnId,
} from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";

import type { Thread } from "../types";
Expand Down Expand Up @@ -359,6 +366,7 @@ describe("hasServerAcknowledgedLocalDispatch", () => {
localDispatch,
phase: "ready",
latestTurn: completedTurn,
latestUserMessageId: localDispatch.latestUserMessageId,
session: readySession,
hasPendingApproval: false,
hasPendingUserInput: false,
Expand All @@ -384,6 +392,7 @@ describe("hasServerAcknowledgedLocalDispatch", () => {
localDispatch,
phase: "ready",
latestTurn: newerTurn,
latestUserMessageId: localDispatch.latestUserMessageId,
session: { ...readySession, updatedAt: newerTurn.completedAt },
hasPendingApproval: false,
hasPendingUserInput: false,
Expand All @@ -410,6 +419,7 @@ describe("hasServerAcknowledgedLocalDispatch", () => {
localDispatch,
phase: "running",
latestTurn: runningTurn,
latestUserMessageId: localDispatch.latestUserMessageId,
session: {
...readySession,
status: "running",
Expand All @@ -425,6 +435,7 @@ describe("hasServerAcknowledgedLocalDispatch", () => {
localDispatch,
phase: "running",
latestTurn: runningTurn,
latestUserMessageId: localDispatch.latestUserMessageId,
session: {
...readySession,
status: "running",
Expand All @@ -437,12 +448,56 @@ describe("hasServerAcknowledgedLocalDispatch", () => {
).toBe(true);
});

it("acknowledges a steering message projected onto the current running turn", () => {
const runningTurn = {
...completedTurn,
state: "running" as const,
completedAt: null,
};
const runningSession = {
...readySession,
status: "running" as const,
activeTurnId: runningTurn.turnId,
};
const localDispatch = createLocalDispatchSnapshot(
makeThread({
latestTurn: runningTurn,
session: runningSession,
messages: [
{
id: MessageId.make("message-before-steer"),
role: "user",
text: "Initial prompt",
turnId: runningTurn.turnId,
createdAt: runningTurn.requestedAt,
updatedAt: runningTurn.requestedAt,
streaming: false,
},
],
}),
);

expect(
hasServerAcknowledgedLocalDispatch({
localDispatch,
phase: "running",
latestTurn: runningTurn,
latestUserMessageId: MessageId.make("message-steer"),
session: runningSession,
hasPendingApproval: false,
hasPendingUserInput: false,
threadError: null,
}),
).toBe(true);
});

it("acknowledges pending user interaction and errors immediately", () => {
const localDispatch = createLocalDispatchSnapshot(makeThread());
const common = {
localDispatch,
phase: "ready" as const,
latestTurn: null,
latestUserMessageId: localDispatch.latestUserMessageId,
session: null,
hasPendingApproval: false,
hasPendingUserInput: false,
Expand Down
13 changes: 13 additions & 0 deletions apps/web/src/components/ChatView.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ export async function waitForStartedServerThread(
export interface LocalDispatchSnapshot {
startedAt: string;
preparingWorktree: boolean;
latestUserMessageId: ChatMessage["id"] | null;
latestTurnTurnId: TurnId | null;
latestTurnRequestedAt: string | null;
latestTurnStartedAt: string | null;
Expand All @@ -401,9 +402,11 @@ export function createLocalDispatchSnapshot(
): LocalDispatchSnapshot {
const latestTurn = activeThread?.latestTurn ?? null;
const session = activeThread?.session ?? null;
const latestUserMessage = activeThread?.messages.findLast((message) => message.role === "user");
return {
startedAt: new Date().toISOString(),
preparingWorktree: Boolean(options?.preparingWorktree),
latestUserMessageId: latestUserMessage?.id ?? null,
latestTurnTurnId: latestTurn?.turnId ?? null,
latestTurnRequestedAt: latestTurn?.requestedAt ?? null,
latestTurnStartedAt: latestTurn?.startedAt ?? null,
Expand All @@ -417,6 +420,7 @@ export function hasServerAcknowledgedLocalDispatch(input: {
localDispatch: LocalDispatchSnapshot | null;
phase: SessionPhase;
latestTurn: Thread["latestTurn"] | null;
latestUserMessageId: ChatMessage["id"] | null;
session: Thread["session"] | null;
hasPendingApproval: boolean;
hasPendingUserInput: boolean;
Expand All @@ -431,13 +435,22 @@ export function hasServerAcknowledgedLocalDispatch(input: {

const latestTurn = input.latestTurn ?? null;
const session = input.session ?? null;
const latestUserMessageChanged =
input.localDispatch.latestUserMessageId !== input.latestUserMessageId;
const latestTurnChanged =
input.localDispatch.latestTurnTurnId !== (latestTurn?.turnId ?? null) ||
input.localDispatch.latestTurnRequestedAt !== (latestTurn?.requestedAt ?? null) ||
input.localDispatch.latestTurnStartedAt !== (latestTurn?.startedAt ?? null) ||
input.localDispatch.latestTurnCompletedAt !== (latestTurn?.completedAt ?? null);

if (input.phase === "running") {
// Steering adds a user message to the current running turn without
// necessarily changing any of the turn timestamps. Treat that projected
// message as the server acknowledgment so the composer does not remain
// stuck in its local "Sending" state until the turn settles.
if (latestUserMessageChanged) {
return true;
}
if (!latestTurnChanged) {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions apps/web/src/components/ChatView.tsx
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ function useLocalDispatchState(input: {
threadError: string | null | undefined;
}) {
const [localDispatch, setLocalDispatch] = useState<LocalDispatchSnapshot | null>(null);
const latestUserMessageId =
input.activeThread?.messages.findLast((message) => message.role === "user")?.id ?? null;

const resetLocalDispatch = useCallback(() => {
setLocalDispatch(null);
Expand All @@ -379,6 +381,7 @@ function useLocalDispatchState(input: {
localDispatch,
phase: input.phase,
latestTurn: input.activeLatestTurn,
latestUserMessageId,
session: input.activeThread?.session ?? null,
hasPendingApproval: input.activePendingApproval !== null,
hasPendingUserInput: input.activePendingUserInput !== null,
Expand All @@ -391,6 +394,7 @@ function useLocalDispatchState(input: {
input.activeThread?.session,
input.phase,
input.threadError,
latestUserMessageId,
localDispatch,
],
);
Expand Down
Loading