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
11 changes: 4 additions & 7 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,9 @@ import {
} from "@t3tools/client-runtime/state/subagentRuntime";
import {
getUserQueuedThreadRuns,
threadSteersDuringCompaction,
threadCanSteer,
threadSupportsProviderHandoff,
} from "@t3tools/client-runtime/state/thread-workflows";
import { compactionBlocksSteering } from "@t3tools/client-runtime/state/composer-dispatch";
import { resolvePendingHandoff } from "@t3tools/client-runtime/handoff";
import { HandoffRow } from "./chat/V2LifecycleRow";
import {
Expand Down Expand Up @@ -1545,12 +1544,10 @@ export default function ChatView(props: ChatViewProps) {
const isCompacting =
(isSendBusy || phase === "connecting" || phase === "running") &&
(compactionRunning || compactCommandIsActive);
const compactionBlocksSteer = compactionBlocksSteering({
const canSteer = threadCanSteer(serverProjection, {
compactCommandIsActive,
compactionRunning,
steersDuringCompaction: threadSteersDuringCompaction(serverProjection),
run: serverProjection?.runs.find((run) => run.id === activeRuntime?.activeRunId),
messages: serverProjection?.messages,
activeRunId: activeRuntime?.activeRunId,
});
const isWorking = deriveTimelineWorking({
phase,
Expand Down Expand Up @@ -3822,7 +3819,7 @@ export default function ChatView(props: ChatViewProps) {
isLocalDraftThread && activeProject === null
}
phase={phase}
canSteer={!compactionBlocksSteer}
canSteer={canSteer}
canInterrupt={canInterruptRunningThread}
isSendBusy={
(isSendBusy && !sendQueuesBehindSetup) ||
Expand Down
34 changes: 33 additions & 1 deletion packages/client-runtime/src/state/threadWorkflows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {
deriveThreadQueueWorkflowState,
getUserQueuedThreadRuns,
resolveLatestMergeBackRun,
threadCanSteer,
threadSupportsProviderHandoff,
} from "./threadWorkflows.ts";

const capabilities = (input?: {
readonly queued?: boolean;
readonly steer?: boolean;
readonly restartSteer?: boolean;
readonly interrupt?: boolean;
readonly nativeFork?: boolean;
readonly portableFork?: boolean;
}) =>
Expand All @@ -21,6 +23,7 @@ const capabilities = (input?: {
supportsQueuedMessages: input?.queued ?? false,
supportsActiveSteering: input?.steer ?? false,
supportsSteeringByInterruptRestart: input?.restartSteer ?? false,
supportsInterrupt: input?.interrupt ?? false,
},
threads: {
canForkThread: input?.nativeFork ?? false,
Expand All @@ -31,6 +34,35 @@ const capabilities = (input?: {
}) as never;

describe("thread workflows", () => {
it.each([
["queue only", { queued: true }, false, false],
["session still attaching", undefined, false, true],
["native steering", { steer: true }, false, true],
["interrupt and restart", { interrupt: true, restartSteer: true }, false, true],
["restart without interrupt", { restartSteer: true }, false, false],
["explicit compaction", { steer: true }, true, false],
] as const)("gates composer steering for %s", (_, turns, compactCommandIsActive, expected) => {
const projection = {
thread: { id: "thread", activeProviderThreadId: "provider-thread" },
runs: [{ id: "active", status: "running", providerThreadId: "provider-thread" }],
providerThreads: [{ id: "provider-thread", providerSessionId: "session" }],
providerSessions: [
...(turns === undefined
? []
: [{ id: "session", status: "running", capabilities: capabilities(turns) }]),
{ id: "other", status: "ready", capabilities: capabilities({ steer: true }) },
],
messages: [],
} as unknown as OrchestrationV2ThreadProjection;
expect(
threadCanSteer(projection, {
compactCommandIsActive,
compactionRunning: false,
activeRunId: projection.runs[0]!.id,
}),
).toBe(expected);
});

it("skips message history classification when no runs are queued", () => {
let metadataReads = 0;
const message = {
Expand Down Expand Up @@ -139,7 +171,7 @@ describe("thread workflows", () => {
{
id: "provider-session",
status: "running",
capabilities: capabilities({ queued: true, restartSteer: true }),
capabilities: capabilities({ queued: true, interrupt: true, restartSteer: true }),
},
],
} as never);
Expand Down
37 changes: 31 additions & 6 deletions packages/client-runtime/src/state/threadWorkflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { copySorted } from "@t3tools/shared/Array";
import { mFallback, msg } from "gt-i18n";
import type { MFunctionType } from "gt-i18n/types";

import { compactionBlocksSteering } from "./composerDispatch.ts";
import type { ThreadStructure } from "./models.ts";

type Projection = ThreadStructure;
Expand Down Expand Up @@ -102,6 +103,33 @@ export function threadSteersDuringCompaction(projection: Projection | null | und
);
}

function supportsSteering(turns: OrchestrationV2ProviderCapabilities["turns"]): boolean {
return (
turns.supportsActiveSteering ||
(turns.supportsInterrupt && turns.supportsSteeringByInterruptRestart)
);
}

export function threadCanSteer(
projection: Projection | null,
input: {
readonly compactCommandIsActive: boolean;
readonly compactionRunning: boolean;
readonly activeRunId: Run["id"] | null | undefined;
},
): boolean {
const turns = projection && resolveThreadProviderSession(projection)?.capabilities.turns;
// A steer submitted before the session attaches can wait for its provider turn.
if (turns && !supportsSteering(turns)) return false;
return !compactionBlocksSteering({
compactCommandIsActive: input.compactCommandIsActive,
compactionRunning: input.compactionRunning,
steersDuringCompaction: turns?.activeSteeringDuringCompaction === true,
run: projection?.runs.find((run) => run.id === input.activeRunId),
messages: projection?.messages,
});
}

export function threadSupportsProviderHandoff(projection: Projection | null | undefined): boolean {
if (projection == null) return false;
const session = resolveThreadProviderSession(projection);
Expand Down Expand Up @@ -147,6 +175,7 @@ export function deriveThreadQueueWorkflowState(
const activeRun = resolveActiveThreadRun(projection);
const session = resolveThreadProviderSession(projection);
const capabilities = session?.capabilities.turns;
const canSteer = capabilities !== undefined && supportsSteering(capabilities);
const activeMessage = projection.messages.find(
(message) => message.id === activeRun?.userMessageId,
);
Expand Down Expand Up @@ -187,12 +216,8 @@ export function deriveThreadQueueWorkflowState(
canPromoteToSteer:
promoteSkipsCompaction ||
((activeRun?.status === "preparing" || activeRun?.status === "starting") &&
(capabilities === undefined ||
capabilities.supportsActiveSteering ||
capabilities.supportsSteeringByInterruptRestart)) ||
(hasSteerableProviderTurn &&
(capabilities?.supportsActiveSteering === true ||
capabilities?.supportsSteeringByInterruptRestart === true)),
(capabilities === undefined || canSteer)) ||
(hasSteerableProviderTurn && canSteer),
promoteSkipsCompaction,
};
}
Expand Down
Loading