Allow provider switching via handoff in chat threads

- Let started threads switch providers when handoff is supported
- Keep model picker and block reasons aligned with thread capabilities
- Update logic tests for the new provider-switching rule
This commit is contained in:
Julius Marminge committed 2026-06-30 17:38:12 -07:00
1 parent 6d33dd25ca
commit 4ca3ff5191
3 files changed
+35 -8

No files matched your search

@@ -197,18 +197,18 @@ describe("getStartedThreadModelChangeBlockReason", () => {
).toBeNull();
});
it("blocks started-session model changes when either provider requires a new thread", () => {
it("blocks started-session model changes for providers that require a new thread", () => {
expect(
getStartedThreadModelChangeBlockReason({
providers,
hasStartedSession: true,
currentModelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5.4",
instanceId: ProviderInstanceId.make("grok"),
model: "grok-build",
},
nextModelSelection: {
instanceId: ProviderInstanceId.make("grok"),
model: "grok-build",
model: "grok-other",
},
}),
).toEqual({
+10
View File
@@ -303,6 +303,7 @@ export function deriveLockedProvider(input: {
export function getStartedThreadModelChangeBlockReason(input: {
providers: ReadonlyArray<Pick<ServerProvider, "instanceId" | "requiresNewThreadForModelChange">>;
hasStartedSession: boolean;
supportsProviderSwitchingViaHandoff?: boolean;
currentModelSelection: ModelSelection;
currentProviderInstanceId?: ModelSelection["instanceId"] | null | undefined;
nextModelSelection: ModelSelection;
@@ -320,6 +321,15 @@ export function getStartedThreadModelChangeBlockReason(input: {
) {
return null;
}
if (currentModelSelection.instanceId !== input.nextModelSelection.instanceId) {
if (input.supportsProviderSwitchingViaHandoff === true) {
return null;
}
return {
title: "Start a new chat to switch providers",
description: "This thread does not support switching providers after it has started.",
};
}
const currentProvider = input.providers.find(
(snapshot) => snapshot.instanceId === currentModelSelection.instanceId,
);
+21 -4
View File
@@ -26,6 +26,7 @@ import {
deriveLatestThreadRun,
deriveThreadRuntime,
} from "@t3tools/client-runtime/state/thread-execution";
import { resolveThreadProviderSession } from "@t3tools/client-runtime/state/thread-workflows";
import { derivePendingThreadRequests } from "@t3tools/client-runtime/state/thread-requests";
import {
parseScopedThreadKey,
@@ -1282,6 +1283,12 @@ function ChatViewContent(props: ChatViewProps) {
() => (serverProjection === null ? null : deriveThreadRuntime(serverProjection)),
[serverProjection],
);
const activeProviderSession = useMemo(
() => (serverProjection === null ? null : resolveThreadProviderSession(serverProjection)),
[serverProjection],
);
const supportsProviderSwitchingViaHandoff =
activeProviderSession?.capabilities.sessions.supportsProviderSwitchingViaHandoff === true;
const activeLatestRun = isServerThread ? serverLatestRun : (activeThread?.latestRun ?? null);
const activeRuntime = isServerThread ? serverRuntime : (activeThread?.runtime ?? null);
const parentSubagentThreadId =
@@ -1706,6 +1713,7 @@ function ChatViewContent(props: ChatViewProps) {
selectedProvider: selectedProviderByThreadId,
threadProvider,
});
const modelPickerLockedProvider = supportsProviderSwitchingViaHandoff ? null : lockedProvider;
// Once a thread selects an environment, never substitute the primary
// environment's config while the selected environment is still loading.
const serverConfig = activeThread
@@ -1746,7 +1754,8 @@ function ChatViewContent(props: ChatViewProps) {
providerStatuses,
selectedProviderByThreadId ?? threadProvider ?? ProviderDriverKind.make("codex"),
);
const selectedProvider: ProviderDriverKind = lockedProvider ?? unlockedSelectedProvider;
const selectedProvider: ProviderDriverKind =
modelPickerLockedProvider ?? unlockedSelectedProvider;
const phase = derivePhase(activeRuntime);
const pendingRequests = useMemo(
() =>
@@ -4882,13 +4891,14 @@ function ChatViewContent(props: ChatViewProps) {
const reason = getStartedThreadModelChangeBlockReason({
providers: providerStatuses,
hasStartedSession: activeRuntime !== null,
supportsProviderSwitchingViaHandoff,
currentModelSelection: activeThread.modelSelection,
currentProviderInstanceId: activeRuntime?.providerInstanceId ?? null,
nextModelSelection: { instanceId, model },
});
return reason ? `${reason.description} Start a new thread to use this model.` : null;
},
[activeRuntime, activeThread, providerStatuses],
[activeRuntime, activeThread, providerStatuses, supportsProviderSwitchingViaHandoff],
);
const onProviderModelSelect = useCallback(
@@ -4900,6 +4910,7 @@ function ChatViewContent(props: ChatViewProps) {
const entry = providerStatuses.find((snapshot) => snapshot.instanceId === instanceId);
const resolvedDriverKind = entry?.driver ?? null;
if (
!supportsProviderSwitchingViaHandoff &&
lockedProvider !== null &&
resolvedDriverKind !== null &&
resolvedDriverKind !== lockedProvider
@@ -4907,7 +4918,11 @@ function ChatViewContent(props: ChatViewProps) {
scheduleComposerFocus();
return;
}
if (lockedProvider !== null && activeRuntime?.providerInstanceId) {
if (
!supportsProviderSwitchingViaHandoff &&
lockedProvider !== null &&
activeRuntime?.providerInstanceId
) {
const currentEntry = providerStatuses.find(
(snapshot) => snapshot.instanceId === activeRuntime.providerInstanceId,
);
@@ -4937,6 +4952,7 @@ function ChatViewContent(props: ChatViewProps) {
const modelChangeBlockReason = getStartedThreadModelChangeBlockReason({
providers: providerStatuses,
hasStartedSession: activeRuntime !== null,
supportsProviderSwitchingViaHandoff,
currentModelSelection: activeThread.modelSelection,
currentProviderInstanceId: activeRuntime?.providerInstanceId ?? null,
nextModelSelection,
@@ -4961,6 +4977,7 @@ function ChatViewContent(props: ChatViewProps) {
activeThread,
activeRuntime,
lockedProvider,
supportsProviderSwitchingViaHandoff,
scheduleComposerFocus,
setComposerDraftModelSelection,
setStickyComposerModelSelection,
@@ -5386,7 +5403,7 @@ function ChatViewContent(props: ChatViewProps) {
planSidebarOpen={planSidebarOpen}
runtimeMode={runtimeMode}
interactionMode={interactionMode}
lockedProvider={lockedProvider}
lockedProvider={modelPickerLockedProvider}
providerStatuses={providerStatuses as ServerProvider[]}
activeProjectDefaultModelSelection={activeProject?.defaultModelSelection}
activeThreadModelSelection={activeThread?.modelSelection}