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
2 changes: 2 additions & 0 deletions apps/web/src/components/AddProjectToMachineDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
isDefaultCopiedFile,
} from "../lib/addProjectToMachine";
import { formatBytes } from "../lib/formatBytes";
import { seedDraftModelSelection } from "../lib/seedDraftModelSelection";
import { newProjectId } from "../lib/utils";
import { readProject, subscribeProject, useProject } from "../state/entities";
import { useEnvironments } from "../state/environments";
Expand Down Expand Up @@ -201,6 +202,7 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe
environmentSelection: "manual",
loadBalancedEnvironmentId: null,
});
seedDraftModelSelection(draftId, targetRef);
return;
}
void handleNewThread(targetRef);
Expand Down
62 changes: 7 additions & 55 deletions apps/web/src/components/chat/DraftHeroHeadline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useProjectPin } from "~/hooks/useProjectGroupUpdate";
import { useScratchProject } from "~/hooks/useScratchProject";
import { isTriagePrompt } from "@t3tools/shared/triagePrompt";
import { useClientSettings } from "~/hooks/useSettings";
import { hasExplicitComposerModelSelection } from "~/lib/chatThreadActions";
import { seedDraftModelSelection } from "~/lib/seedDraftModelSelection";
import {
deriveLogicalProjectKeyFromSettings,
pickableProjectGroups,
Expand All @@ -27,11 +27,7 @@ import {
projectGroupsSpanEnvironments,
} from "~/sidebarProjectGrouping";
import { useProjects } from "~/state/entities";
import {
useEnvironments,
usePrimaryEnvironmentId,
useEnvironmentProviders,
} from "~/state/environments";
import { useEnvironments, usePrimaryEnvironmentId } from "~/state/environments";
import { ProjectEnvironmentBadge } from "../ProjectEnvironmentBadge";
import { ProjectFavicon } from "../ProjectFavicon";
import { isProjectPinShortcut, ProjectPinButton } from "../ProjectPinButton";
Expand All @@ -51,9 +47,6 @@ import { InlineButton } from "../ui/button";
import { DraftProjectName } from "./DraftProjectName";
import { DraftHeroProjectHeading } from "./DraftHeroProjectHeading";
import { TriageDraftHero } from "./TriageDraftHero";
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
import { resolveProjectDefaultProviderModelSelection } from "../../providerInstances";
import { resolveNewThreadModelSelection } from "@t3tools/shared/usageLimits";

const ADD_PROJECT_VALUE = "add-project";

Expand All @@ -75,7 +68,6 @@ export function DraftHeroHeadline({
}: DraftHeroHeadlineProps) {
const projects = useProjects();
const { environments } = useEnvironments();
const providersByEnvironment = useEnvironmentProviders();
const primaryEnvironmentId = usePrimaryEnvironmentId();
const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);
const setLogicalProjectDraftThreadId = useComposerDraftStore(
Expand All @@ -86,8 +78,6 @@ export function DraftHeroHeadline({
const isTriageDraftPrompt = useComposerDraftStore((store) =>
draftId ? isTriagePrompt(store.getComposerDraft(draftId)?.prompt ?? "") : false,
);
const applyStickyState = useComposerDraftStore((store) => store.applyStickyState);
const setModelSelection = useComposerDraftStore((store) => store.setModelSelection);
const openAddProject = () => openCommandPalette({ open: "add-project" });
const { scratchEnvironmentId, scratchWorkspaceRootFor, openScratchProject } = useScratchProject();
const openProjectDraft = useNewThreadHandler();
Expand Down Expand Up @@ -186,51 +176,13 @@ export function DraftHeroHeadline({
return;
}
pendingScratchRef.current = null;
const currentDraft = getComposerDraft(draftId);
if (!composerDraftHasUserContent(currentDraft)) {
void openProjectDraft(scopeProjectRef(project.environmentId, project.id));
const projectRef = scopeProjectRef(project.environmentId, project.id);
if (!composerDraftHasUserContent(getComposerDraft(draftId))) {
void openProjectDraft(projectRef);
return;
}
setLogicalProjectDraftThreadId(
logicalProjectKey,
scopeProjectRef(project.environmentId, project.id),
draftId,
);
if (!hasExplicitComposerModelSelection(currentDraft)) {
applyStickyState(draftId);
const environmentConfig = environments.find(
(environment) => environment.environmentId === project.environmentId,
)?.serverConfig;
const environmentSettings = environmentConfig?.settings;
const defaultSelection = environmentSettings
? resolveProjectSettings(environmentSettings, project.id, project).settings
.defaultModelSelection
: project.defaultModelSelection;
const fallbackResolution = resolveNewThreadModelSelection({
defaultSelection:
defaultSelection ??
(useComposerDraftStore.getState().stickyActiveProvider !== null || !environmentSettings
? null
: resolveProjectDefaultProviderModelSelection(
providersByEnvironment.get(project.environmentId) ?? [],
environmentSettings,
project.id,
)),
fallbackSelection: environmentSettings?.fallbackModelSelection ?? null,
providers: providersByEnvironment.get(project.environmentId) ?? [],
...(environmentSettings ? { settings: environmentSettings } : {}),
projectId: project.id,
now: Date.now(),
});
const defaultModelSelection = fallbackResolution.fallback
? fallbackResolution.selection
: defaultSelection;
if (defaultModelSelection) {
setModelSelection(draftId, defaultModelSelection, {
replaceOptions: true,
});
}
}
setLogicalProjectDraftThreadId(logicalProjectKey, projectRef, draftId);
seedDraftModelSelection(draftId, projectRef);
};
const startScratch = async (): Promise<boolean> => {
if (scratchTargetEnvironmentId === null || isScratchDraft) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ vi.mock("../../hooks/useLoadBalancedEnvironment", () => ({
refreshSelection: resources.refreshSelection,
}),
}));
vi.mock("../../state/entities", () => ({ useProjects: () => resources.projects }));
vi.mock("../../state/entities", () => ({
useProjects: () => resources.projects,
readProject: (ref: { environmentId: EnvironmentId; projectId: ProjectId }) =>
resources.projects.find(
(project) => project.environmentId === ref.environmentId && project.id === ref.projectId,
) ?? null,
}));
vi.mock("../../hooks/useScratchProject", () => ({ useScratchProject: () => ({}) }));

function setup() {
Expand Down Expand Up @@ -314,3 +320,35 @@ it("keeps the selected models when automatic routing picks another machine", asy
await expect.element(screen.getByText(b)).toBeInTheDocument();
await expect.element(screen.getByText("gpt-6.1-sol, gpt-6.1-luna")).toBeInTheDocument();
});

it.each([
{ name: "takes the destination default", explicit: false, remoteDefault: true, model: "luna" },
{ name: "keeps a model the user picked", explicit: true, remoteDefault: true, model: "sol" },
{
name: "keeps the last-used model without a default",
explicit: false,
remoteDefault: false,
model: "sol",
},
])("$name when the draft switches machine by hand", async ({ explicit, remoteDefault, model }) => {
const { b, draftId, input, store } = fanoutSetup();
const codex = ProviderInstanceId.make("codex");
resources.projects = resources.projects.map((project) =>
project.environmentId === b && remoteDefault
? { ...project, defaultModelSelection: createModelSelection(codex, "luna") }
: project,
);
store.setStickyModelSelection(createModelSelection(codex, "sol"));
store.setModelSelection(draftId, createModelSelection(codex, "sol"), { explicit });
await using screen = await render(
<Harness
input={{ ...input, automaticEnvironment: false, draftThread: store.getDraftSession(draftId) }}
/>,
);
await screen.getByRole("button", { name: `Switch to ${b}` }).click();
await expect.element(screen.getByText(b)).toBeInTheDocument();
const draft = store.getComposerDraft(draftId);
expect(draft?.activeProvider).toBe(codex);
expect(draft?.modelSelectionByProvider[codex]?.model).toBe(model);
expect(draft?.modelSelectionExplicit === true).toBe(explicit);
});
2 changes: 2 additions & 0 deletions apps/web/src/components/chat/useDraftEnvironmentRouting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { useAtomCommand } from "../../state/use-atom-command";
import type { DraftSessionState } from "../../composerDraftStore";
import { loadBalancingSendWarning } from "./composerSend.logic";
import { appAtomRegistry } from "../../rpc/atomRegistry";
import { seedDraftModelSelection } from "../../lib/seedDraftModelSelection";
import { draftFanoutSelectionsAtom } from "./chatViewSend";

interface DraftEnvironmentRoutingInput {
Expand Down Expand Up @@ -337,6 +338,7 @@ export function useDraftEnvironmentRouting(input: DraftEnvironmentRoutingInput)
loadBalancedEnvironmentId: null,
});
}
seedDraftModelSelection(draftId, projectRef);
};
const finish = () => {
if (environmentChangeRef.current === request) environmentChangeRef.current = null;
Expand Down
37 changes: 37 additions & 0 deletions apps/web/src/lib/seedDraftModelSelection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { ScopedProjectRef } from "@t3tools/contracts";
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
import { resolveNewThreadModelSelection } from "@t3tools/shared/usageLimits";

import { type DraftId, useComposerDraftStore } from "../composerDraftStore";
import { resolveProjectDefaultProviderModelSelection } from "../providerInstances";
import { readProject } from "../state/entities";
import { readEnvironment, readEnvironmentProviders } from "../state/environments";
import { hasExplicitComposerModelSelection } from "./chatThreadActions";

/** Gives a draft that moved project or machine the model a new thread there would start with. */
export function seedDraftModelSelection(draftId: DraftId, projectRef: ScopedProjectRef): void {
const store = useComposerDraftStore.getState();
if (hasExplicitComposerModelSelection(store.getComposerDraft(draftId))) return;
store.applyStickyState(draftId);
const project = readProject(projectRef);
const settings = readEnvironment(projectRef.environmentId)?.serverConfig?.settings;
const providers = readEnvironmentProviders(projectRef.environmentId) ?? [];
const defaultSelection = settings
? resolveProjectSettings(settings, projectRef.projectId, project ?? undefined).settings
.defaultModelSelection
: (project?.defaultModelSelection ?? null);
const resolution = resolveNewThreadModelSelection({
defaultSelection:
defaultSelection ??
(useComposerDraftStore.getState().stickyActiveProvider !== null || !settings
? null
: resolveProjectDefaultProviderModelSelection(providers, settings, projectRef.projectId)),
fallbackSelection: settings?.fallbackModelSelection ?? null,
providers,
...(settings ? { settings } : {}),
projectId: projectRef.projectId,
now: Date.now(),
});
const selection = resolution.fallback ? resolution.selection : defaultSelection;
if (selection) store.setModelSelection(draftId, selection, { replaceOptions: true });
}
6 changes: 6 additions & 0 deletions apps/web/src/state/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ export function useEnvironmentProviders() {
return useAtomValue(environmentPresentations.providersByEnvironmentAtom);
}

export function readEnvironmentProviders(environmentId: EnvironmentId) {
return appAtomRegistry
.get(environmentPresentations.providersByEnvironmentAtom)
.get(environmentId);
}

export function usePrimaryEnvironmentId(): EnvironmentId | null {
return useAtomValue(primaryEnvironmentIdAtom);
}
Expand Down
Loading