Skip to content

Commit 591b54b

Browse files
refactor(web): the composer builds model options without a "use memo" hook
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e2e9b4d commit 591b54b

1 file changed

Lines changed: 21 additions & 31 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,9 @@ function useComposerProviderSelection(
17771777
readonly activeProjectId: ProjectId | null;
17781778
},
17791779
) {
1780-
// Calls no hooks, so the compiler needs the directive to memoize it.
1780+
// Kept: `providerInstanceEntries` is read by plain functions on both sides of
1781+
// `useEffectiveComposerModelState` and `useProviderWorkspaceSnapshotRefresh` (the
1782+
// `hasProviderSetup` lookup before, the model picker's `.find` after), so inline it loses its cache.
17811783
"use memo";
17821784
const { providerStatuses, settings, activeProjectId, ...selection } = input;
17831785
const providerInstanceEntries: ReadonlyArray<ProviderInstanceEntry> = sortProviderInstanceEntries(
@@ -1793,36 +1795,16 @@ function useComposerProviderSelection(
17931795
};
17941796
}
17951797

1796-
function useModelOptionsByInstance(
1797-
settings: UnifiedSettings,
1798-
entries: ReadonlyArray<ProviderInstanceEntry>,
1799-
selectedInstanceId: ProviderInstanceId,
1800-
selectedModel: string | null,
1801-
): ReadonlyMap<ProviderInstanceId, ReadonlyArray<AppModelOption>> {
1802-
// Calls no hooks, so the compiler needs the directive to memoize it.
1803-
"use memo";
1804-
const out = new Map<ProviderInstanceId, ReadonlyArray<AppModelOption>>();
1805-
for (const entry of entries) {
1806-
out.set(
1807-
entry.instanceId,
1808-
getAppModelOptionsForInstance(
1809-
settings,
1810-
entry,
1811-
entry.instanceId === selectedInstanceId ? selectedModel : null,
1812-
),
1813-
);
1814-
}
1815-
return out;
1816-
}
1817-
18181798
/** Splits the draft's images into pending SnapShots and standalone images. */
18191799
function useComposerImageGroups(
18201800
pendingSnapShotAnimations: ReturnType<typeof getPendingSnapShotAnimations>,
18211801
composerDraftTarget: ScopedThreadRef | DraftId,
18221802
composerImages: ReadonlyArray<ComposerImageAttachment>,
18231803
composerPreviewAnnotations: ReadonlyArray<{ readonly id: string }>,
18241804
) {
1825-
// Calls no hooks, so the compiler needs the directive to memoize it.
1805+
// Kept: these arrays go through plain functions here and again ~2,700 lines later
1806+
// (`.some`, `.filter`, `.slice` in the resting layout), past dozens of hook calls. Inline, the
1807+
// compiler gives them no cache scope, and moving every later use above those hooks is not possible.
18261808
"use memo";
18271809
const pendingSnapShotIds = pendingSnapShotAnimationIdsForTarget(
18281810
pendingSnapShotAnimations,
@@ -1845,7 +1827,9 @@ const NO_PROVIDER_SKILLS: ServerProvider["skills"] = [];
18451827
const NO_PROVIDER_SLASH_COMMANDS: ServerProvider["slashCommands"] = [];
18461828

18471829
function useComposerProviderState(input: Parameters<typeof getComposerProviderState>[0]) {
1848-
// Calls no hooks, so the compiler needs the directive to memoize it.
1830+
// Kept: inline, `composerProviderState`, `selectedModelSelection` and `providerInputInputs` lose
1831+
// their cache scope. The inputs come from `useEffectiveComposerModelState`, and the results are
1832+
// read past later hooks such as `useProviderWorkspaceSnapshotRefresh`.
18491833
"use memo";
18501834
return getComposerProviderState(input);
18511835
}
@@ -2345,12 +2329,18 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
23452329
selectedModelOptionsForDispatch,
23462330
);
23472331
const selectedModelForPicker = selectedModel;
2348-
const modelOptionsByInstance = useModelOptionsByInstance(
2349-
settings,
2350-
providerInstanceEntries,
2351-
selectedInstanceId,
2352-
selectedModelForPicker,
2353-
);
2332+
const modelOptionsByInstance = new Map<ProviderInstanceId, ReadonlyArray<AppModelOption>>();
2333+
for (const entry of providerInstanceEntries) {
2334+
modelOptionsByInstance.set(
2335+
entry.instanceId,
2336+
getAppModelOptionsForInstance(
2337+
settings,
2338+
entry,
2339+
entry.instanceId === selectedInstanceId ? selectedModelForPicker : null,
2340+
),
2341+
);
2342+
}
2343+
23542344
const selectedModelForPickerWithCustomFallback = (() => {
23552345
const currentOptions = modelOptionsByInstance.get(selectedInstanceId) ?? [];
23562346
return currentOptions.some((option) => option.slug === selectedModelForPicker)

0 commit comments

Comments
 (0)