Files
t3code/apps/mobile/src/lib/modelOptions.ts
T
Julius MarmingeandClaude Fable 5 d256570669 fix: repair rerere-damaged files and reconcile main's round-3 features with v2
Post-rebase reconciliation sweep:
- Sidebar: main's folded Sidebar.tsx/Sidebar.logic.ts adapted to v2 shells
  (latestRun/runtime naming, waiting status instead of monitoring), with
  subagent-thread filtering and main's pinned-reorder helpers re-exported
- Pinned drag reorder (#5581) ported into v2: thread.pin orderKey +
  thread.pin.reorder command, thread.pin-reordered event, Orchestrator fold,
  ProjectionStore/Maintenance, client-runtime commands and shell mapping
- Project favicon (#4849-era) and defaultThreadEnvMode flowed through v2
  contracts (OrchestrationProjectShell, application event payloads)
- ChatView: main's #5592 header props, pull-request right-panel surfaces,
  liveAgentCount badge (#5745) wired into the v2 panel layout
- enableAssistantStreaming -> enableLegacyTokenStreaming rename applied to
  v2 RunExecutionService and replay testkit
- Removed v1 zombies resurrected by the rebase (provider service/reaper/
  ingestion + v1 layer tests, server.test.ts, integration harness)
- routeTree: main's tree + branch's /settings/scheduled-tasks route
- Misc marker-sweep syntax repairs (rpc.ts, entities.ts, localApi.test.ts,
  rightPanelStore.test.ts, GitManager.test.ts, mobile model menu helpers)

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-14 11:23:51 +02:00

241 lines
7.4 KiB
TypeScript

import type { MenuAction } from "@react-native-menu/menu";
import type {
ModelCapabilities,
ModelSelection,
ServerConfig as T3ServerConfig,
} from "@t3tools/contracts";
import {
buildProviderOptionSelectionsFromDescriptors,
getProviderOptionDescriptors,
} from "@t3tools/shared/model";
export type ModelOption = {
readonly key: string;
readonly label: string;
readonly subtitle: string;
readonly providerKey: string;
readonly providerLabel: string;
readonly providerDriver: string;
readonly isDefault: boolean;
readonly isLegacy: boolean;
readonly capabilities: ModelCapabilities | null;
readonly selection: ModelSelection;
};
export type ProviderGroup = {
readonly providerKey: string;
readonly providerLabel: string;
readonly models: ReadonlyArray<ModelOption>;
};
function providerDisplayLabel(provider: {
readonly displayName?: string | undefined;
readonly driver: string;
readonly instanceId: string;
}): string {
if (provider.displayName) return provider.displayName;
if (provider.driver === "codex") return "Codex";
if (provider.driver === "claudeAgent") return "Claude";
return provider.instanceId;
}
function normalizeSelectionOptions(
selection: ModelSelection,
capabilities: ModelCapabilities | null,
): ModelSelection {
if (!capabilities) {
return selection;
}
const options = buildProviderOptionSelectionsFromDescriptors(
getProviderOptionDescriptors({
caps: capabilities,
selections: selection.options,
}),
);
return options
? { ...selection, options }
: {
instanceId: selection.instanceId,
model: selection.model,
};
}
/**
* A stored model selection is only usable when its provider instance is
* currently enabled, installed, and authenticated on the server. Returns the
* selection unchanged when usable, otherwise `null` so callers fall through to
* the server's default model. A missing config (environment offline) cannot be
* validated, so stored selections pass through untouched.
*/
export function resolveSelectableModelSelection(
config: T3ServerConfig | null | undefined,
selection: ModelSelection | null,
): ModelSelection | null {
if (!selection || !config) {
return selection;
}
const provider = config.providers.find(
(candidate) => candidate.instanceId === selection.instanceId,
);
return provider &&
provider.enabled &&
provider.installed &&
provider.auth.status !== "unauthenticated"
? selection
: null;
}
/**
* Like resolveSelectableModelSelection, but additionally rejects legacy
* models. Used for implicit defaults (stored draft, project last-used): a
* new thread should never quietly start on a legacy model, so those fall
* through to the provider's default instead. Explicit picks in the settings
* sheet are unaffected.
*/
export function resolveDefaultableModelSelection(
config: T3ServerConfig | null | undefined,
selection: ModelSelection | null,
): ModelSelection | null {
const usable = resolveSelectableModelSelection(config, selection);
if (!usable || !config) {
return usable;
}
const provider = config.providers.find((candidate) => candidate.instanceId === usable.instanceId);
const model = provider?.models.find((candidate) => candidate.slug === usable.model);
return model?.isLegacy === true ? null : usable;
}
export function buildModelOptions(
config: T3ServerConfig | null | undefined,
fallbackModelSelection: ModelSelection | null,
): ReadonlyArray<ModelOption> {
const options = new Map<string, ModelOption>();
for (const provider of config?.providers ?? []) {
if (!provider.enabled || !provider.installed || provider.auth.status === "unauthenticated") {
continue;
}
const providerLabel = providerDisplayLabel(provider);
for (const model of provider.models) {
const key = `${provider.instanceId}:${model.slug}`;
options.set(key, {
key,
label: model.name,
subtitle: providerLabel,
providerKey: provider.instanceId,
providerLabel,
providerDriver: provider.driver,
isDefault: model.isDefault === true,
isLegacy: model.isLegacy === true,
capabilities: model.capabilities,
selection: normalizeSelectionOptions(
{
instanceId: provider.instanceId,
model: model.slug,
},
model.capabilities,
),
});
}
}
if (fallbackModelSelection) {
const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`;
const existing = options.get(key);
if (existing) {
options.set(key, {
...existing,
selection: normalizeSelectionOptions(fallbackModelSelection, existing.capabilities),
});
} else {
const providerLabel = fallbackModelSelection.instanceId;
options.set(key, {
key,
label: fallbackModelSelection.model,
subtitle: providerLabel,
providerKey: fallbackModelSelection.instanceId,
providerLabel,
providerDriver: fallbackModelSelection.instanceId,
isDefault: false,
isLegacy: false,
capabilities: null,
selection: fallbackModelSelection,
});
}
}
return [...options.values()];
}
export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyArray<ProviderGroup> {
const groups = new Map<string, { providerLabel: string; models: ModelOption[] }>();
for (const option of options) {
const existing = groups.get(option.providerKey);
if (existing) {
existing.models.push(option);
} else {
groups.set(option.providerKey, {
providerLabel: option.providerLabel,
models: [option],
});
}
}
return [...groups.entries()].map(([providerKey, group]) => ({
providerKey,
providerLabel: group.providerLabel,
models: group.models,
}));
}
function modelMenuAction(option: ModelOption, selectedModel: ModelSelection | null): MenuAction {
return {
id: `model:${option.key}`,
title: option.label,
state:
option.selection.instanceId === selectedModel?.instanceId &&
option.selection.model === selectedModel.model
? "on"
: undefined,
};
}
export function buildModelMenuActions(
groups: ReadonlyArray<ProviderGroup>,
selectedModel: ModelSelection | null,
): MenuAction[] {
return groups.flatMap((group) => {
const currentModels = group.models.filter((model) => !model.isLegacy);
const legacyModels = group.models.filter((model) => model.isLegacy);
const selected = group.models.find(
(model) =>
model.selection.instanceId === selectedModel?.instanceId &&
model.selection.model === selectedModel.model,
);
return [
...(currentModels.length > 0
? [
{
id: `provider:${group.providerKey}`,
title: group.providerLabel,
subtitle: selected && !selected.isLegacy ? selected.label : undefined,
subactions: currentModels.map((option) => modelMenuAction(option, selectedModel)),
},
]
: []),
...(legacyModels.length > 0
? [
{
id: `legacy-models:${group.providerKey}`,
title: `${group.providerLabel} legacy models`,
subtitle: selected?.isLegacy ? selected.label : undefined,
subactions: legacyModels.map((option) => modelMenuAction(option, selectedModel)),
},
]
: []),
];
});
}