Skip to content

Commit 7b8753a

Browse files
committed
fix(web): preserve multi-model selection when auto routing refreshes
1 parent 0fe6261 commit 7b8753a

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎apps/web/src/components/chat/useDraftEnvironmentRouting.browser.test.tsx‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection";
22
import * as Option from "effect/Option";
33
import type { EnvironmentPresentation } from "../../state/environments";
44
import type { ServerProvider } from "@t3tools/contracts";
5-
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
5+
import {
6+
scopedThreadKey,
7+
scopeProjectRef,
8+
scopeThreadRef,
9+
} from "@t3tools/client-runtime/environment";
10+
import { createModelSelection } from "@t3tools/shared/model";
611
import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
712
import {
813
EnvironmentId,
@@ -15,6 +20,8 @@ import { expect, it, vi } from "vite-plus/test";
1520
import { act, render } from "../../../test/render";
1621
import { DraftId, useComposerDraftStore } from "../../composerDraftStore";
1722
import { useDraftEnvironmentRouting } from "./useDraftEnvironmentRouting";
23+
import { appAtomRegistry } from "../../rpc/atomRegistry";
24+
import { draftFanoutSelectionsAtom } from "./chatViewSend";
1825

1926
const resources = vi.hoisted(() => ({
2027
refreshSelection: vi.fn<(ids: readonly EnvironmentId[]) => Promise<EnvironmentId | null>>(),
@@ -134,6 +141,18 @@ function Harness({
134141

135142
it("refreshes a stored automatic choice before sending an unsent draft", async () => {
136143
const { a, b, draftId, input, store } = setup();
144+
const selections = ["model-a", "model-b"].map((model) =>
145+
createModelSelection(ProviderInstanceId.make("codex"), model),
146+
);
147+
const threadId = input.draftThread!.threadId;
148+
const destinationSelections = draftFanoutSelectionsAtom(
149+
scopedThreadKey(scopeThreadRef(b, threadId)),
150+
);
151+
appAtomRegistry.set(destinationSelections, null);
152+
appAtomRegistry.set(
153+
draftFanoutSelectionsAtom(scopedThreadKey(scopeThreadRef(a, threadId))),
154+
selections,
155+
);
137156
store.setDraftThreadContext(draftId, {
138157
branch: "main",
139158
envMode: "worktree",
@@ -152,6 +171,7 @@ it("refreshes a stored automatic choice before sending an unsent draft", async (
152171
envMode: "worktree",
153172
});
154173
expect(store.getComposerDraft(draftId)?.prompt).toBe("Keep this prompt");
174+
expect(appAtomRegistry.get(destinationSelections)).toEqual(selections);
155175
});
156176

157177
it.each(["manual", "attachments", "promoted"] as const)(

‎apps/web/src/components/chat/useDraftEnvironmentRouting.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
resolveProjectProviderInstanceEnabled,
1010
type ThreadContextRecord,
1111
} from "@t3tools/contracts";
12-
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
12+
import {
13+
scopedThreadKey,
14+
scopeProjectRef,
15+
scopeThreadRef,
16+
} from "@t3tools/client-runtime/environment";
1317
import { useEffect, useLayoutEffect, useRef, useState } from "react";
1418
import { flushSync } from "react-dom";
1519
import { toastManager } from "../ui/toast";
@@ -22,6 +26,8 @@ import { type EnvironmentPresentation, useEnvironmentProviders } from "../../sta
2226
import { useProjects } from "../../state/entities";
2327
import type { DraftSessionState } from "../../composerDraftStore";
2428
import { loadBalancingSendWarning } from "./composerSend.logic";
29+
import { appAtomRegistry } from "../../rpc/atomRegistry";
30+
import { draftFanoutSelectionsAtom } from "./chatViewSend";
2531

2632
interface DraftEnvironmentRoutingInput {
2733
readonly draftId: DraftId | null;
@@ -156,6 +162,18 @@ export function useDraftEnvironmentRouting(input: DraftEnvironmentRoutingInput)
156162
return false;
157163
}
158164
const projectRef = scopeProjectRef(target.environmentId, target.projectId);
165+
if (originalDraft && target.environmentId !== originalDraft.environmentId) {
166+
appAtomRegistry.set(
167+
draftFanoutSelectionsAtom(
168+
scopedThreadKey(scopeThreadRef(target.environmentId, originalDraft.threadId)),
169+
),
170+
appAtomRegistry.get(
171+
draftFanoutSelectionsAtom(
172+
scopedThreadKey(scopeThreadRef(originalDraft.environmentId, originalDraft.threadId)),
173+
),
174+
),
175+
);
176+
}
159177
// The send must read the target project's composer and provider context after retargeting.
160178
flushSync(() =>
161179
setDraftThreadContext(draftId, {

0 commit comments

Comments
 (0)