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 authored and maria-rcks committed 2026-08-03 11:45:07 -04:00
1 parent 14d20c8665
commit 638b9fc854
3 files changed
+35 -8

No files matched your search

@@ -304,18 +304,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
@@ -403,6 +403,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;
@@ -420,6 +421,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,
@@ -1460,6 +1461,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 =
@@ -1912,6 +1919,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
@@ -1952,7 +1960,8 @@ function ChatViewContent(props: ChatViewProps) {
providerStatuses,
selectedProviderByThreadId ?? threadProvider,
);
const selectedProvider: ProviderDriverKind = lockedProvider ?? unlockedSelectedProvider;
const selectedProvider: ProviderDriverKind =
modelPickerLockedProvider ?? unlockedSelectedProvider;
const phase = derivePhase(activeRuntime);
const pendingRequests = useMemo(
() =>
@@ -5451,13 +5460,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(
@@ -5469,6 +5479,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
@@ -5476,7 +5487,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,
);
@@ -5506,6 +5521,7 @@ function ChatViewContent(props: ChatViewProps) {
const modelChangeBlockReason = getStartedThreadModelChangeBlockReason({
providers: providerStatuses,
hasStartedSession: activeRuntime !== null,
supportsProviderSwitchingViaHandoff,
currentModelSelection: activeThread.modelSelection,
currentProviderInstanceId: activeRuntime?.providerInstanceId ?? null,
nextModelSelection,
@@ -5530,6 +5546,7 @@ function ChatViewContent(props: ChatViewProps) {
activeThread,
activeRuntime,
lockedProvider,
supportsProviderSwitchingViaHandoff,
scheduleComposerFocus,
setComposerDraftModelSelection,
setStickyComposerModelSelection,
@@ -5974,7 +5991,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}