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-08-07 13:24:29 +02:00
1 parent 9acdef5551
commit c0f30e2e08
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
@@ -27,6 +27,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,
@@ -1490,6 +1491,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 =
@@ -1926,6 +1933,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
@@ -1966,7 +1974,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(
() =>
@@ -5790,13 +5799,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(
@@ -5808,6 +5818,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
@@ -5815,7 +5826,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,
);
@@ -5845,6 +5860,7 @@ function ChatViewContent(props: ChatViewProps) {
const modelChangeBlockReason = getStartedThreadModelChangeBlockReason({
providers: providerStatuses,
hasStartedSession: activeRuntime !== null,
supportsProviderSwitchingViaHandoff,
currentModelSelection: activeThread.modelSelection,
currentProviderInstanceId: activeRuntime?.providerInstanceId ?? null,
nextModelSelection,
@@ -5869,6 +5885,7 @@ function ChatViewContent(props: ChatViewProps) {
activeThread,
activeRuntime,
lockedProvider,
supportsProviderSwitchingViaHandoff,
scheduleComposerFocus,
setComposerDraftModelSelection,
setStickyComposerModelSelection,
@@ -6313,7 +6330,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}