Files
Netcatty/infrastructure/ai/composerModelPrefs.ts
T
bincxz 4acf254f72 feat(ai): unify sidebar provider, model, and thinking pickers
Give Catty a two-column provider/model menu in the composer, add search
plus recent/pinned models, and move thinking effort onto its own chip
so Claude, Codex, Grok, and Catty share the same compact controls.

Closes #2137
Closes #2637
Closes #1884
2026-08-19 17:23:06 +08:00

74 lines
2.1 KiB
TypeScript

import { localStorageAdapter } from '../persistence/localStorageAdapter';
import { STORAGE_KEY_AI_COMPOSER_MODEL_PREFS } from '../config/storageKeys';
import {
COMPOSER_PINNED_MODEL_LIMIT,
COMPOSER_RECENT_MODEL_LIMIT,
parseComposerModelPrefs,
toggleComposerPinnedPref,
upsertComposerPrefFront,
type ComposerModelPrefEntry,
type ComposerModelPrefs,
} from './composerPicker';
type PrefsByScope = Record<string, ComposerModelPrefs>;
function emptyPrefs(): ComposerModelPrefs {
return { recent: [], pinned: [] };
}
function readAllPrefs(): PrefsByScope {
try {
const raw = localStorageAdapter.read<PrefsByScope>(STORAGE_KEY_AI_COMPOSER_MODEL_PREFS);
if (!raw || typeof raw !== 'object') return {};
const next: PrefsByScope = {};
for (const [scope, value] of Object.entries(raw)) {
next[scope] = parseComposerModelPrefs(value);
}
return next;
} catch {
return {};
}
}
function writeAllPrefs(prefs: PrefsByScope): void {
try {
localStorageAdapter.write(STORAGE_KEY_AI_COMPOSER_MODEL_PREFS, prefs);
} catch {
// Tests and SSR have no storage. Recent/pinned stay in-memory only.
}
}
export function readComposerModelPrefs(scope: string): ComposerModelPrefs {
return parseComposerModelPrefs(readAllPrefs()[scope] ?? emptyPrefs());
}
export function rememberComposerRecentModel(
scope: string,
entry: ComposerModelPrefEntry,
): ComposerModelPrefs {
const all = readAllPrefs();
const current = parseComposerModelPrefs(all[scope]);
const next: ComposerModelPrefs = {
...current,
recent: upsertComposerPrefFront(current.recent, entry, COMPOSER_RECENT_MODEL_LIMIT),
};
all[scope] = next;
writeAllPrefs(all);
return next;
}
export function toggleComposerPinnedModel(
scope: string,
entry: ComposerModelPrefEntry,
): ComposerModelPrefs {
const all = readAllPrefs();
const current = parseComposerModelPrefs(all[scope]);
const next: ComposerModelPrefs = {
...current,
pinned: toggleComposerPinnedPref(current.pinned, entry, COMPOSER_PINNED_MODEL_LIMIT),
};
all[scope] = next;
writeAllPrefs(all);
return next;
}