Open History to see the last commit
1import type {
2 ModelCapabilities,
3 ModelSelection,
4 ServerConfig as T3ServerConfig,
5} from "@t3tools/contracts";
6import {
7 buildProviderOptionSelectionsFromDescriptors,
8 getProviderOptionDescriptors,
9} from "@t3tools/shared/model";
10
11export type ModelOption = {
12 readonly key: string;
13 readonly label: string;
14 readonly subtitle: string;
15 readonly providerKey: string;
16 readonly providerLabel: string;
17 readonly providerDriver: string;
18 readonly isDefault: boolean;
19 readonly isLegacy: boolean;
20 readonly capabilities: ModelCapabilities | null;
21 readonly selection: ModelSelection;
22};
23
24export type ProviderGroup = {
25 readonly providerKey: string;
26 readonly providerLabel: string;
27 readonly models: ReadonlyArray<ModelOption>;
28};
29
30function providerDisplayLabel(provider: {
31 readonly displayName?: string | undefined;
32 readonly driver: string;
33 readonly instanceId: string;
34}): string {
35 if (provider.displayName) return provider.displayName;
36 if (provider.driver === "codex") return "Codex";
37 if (provider.driver === "claudeAgent") return "Claude";
38 if (provider.driver === "acpRegistry") return "ACP";
39 return provider.instanceId;
40}
41
42function normalizeSelectionOptions(
43 selection: ModelSelection,
44 capabilities: ModelCapabilities | null,
45): ModelSelection {
46 if (!capabilities) {
47 return selection;
48 }
49 const options = buildProviderOptionSelectionsFromDescriptors(
50 getProviderOptionDescriptors({
51 caps: capabilities,
52 selections: selection.options,
53 }),
54 );
55 return options
56 ? { ...selection, options }
57 : {
58 instanceId: selection.instanceId,
59 model: selection.model,
60 };
61}
62
63/**
64 * A stored model selection is only usable when its provider instance is
65 * currently enabled, installed, and authenticated on the server. Returns the
66 * selection unchanged when usable, otherwise `null` so callers fall through to
67 * the server's default model. A missing config (environment offline) cannot be
68 * validated, so stored selections pass through untouched.
69 */
70export function resolveSelectableModelSelection(
71 config: T3ServerConfig | null | undefined,
72 selection: ModelSelection | null,
73): ModelSelection | null {
74 if (!selection || !config) {
75 return selection;
76 }
77 const provider = config.providers.find(
78 (candidate) => candidate.instanceId === selection.instanceId,
79 );
80 return provider &&
81 provider.enabled &&
82 provider.installed &&
83 provider.auth.status !== "unauthenticated"
84 ? selection
85 : null;
86}
87
88/**
89 * Like resolveSelectableModelSelection, but additionally rejects legacy
90 * models. Used for implicit defaults (stored draft, project last-used): a
91 * new thread should never quietly start on a legacy model, so those fall
92 * through to the provider's default instead. Explicit picks in the settings
93 * sheet are unaffected.
94 */
95export function resolveDefaultableModelSelection(
96 config: T3ServerConfig | null | undefined,
97 selection: ModelSelection | null,
98): ModelSelection | null {
99 const usable = resolveSelectableModelSelection(config, selection);
100 if (!usable || !config) {
101 return usable;
102 }
103 const provider = config.providers.find((candidate) => candidate.instanceId === usable.instanceId);
104 const model = provider?.models.find((candidate) => candidate.slug === usable.model);
105 return model?.isLegacy === true ? null : usable;
106}
107
108export function buildModelOptions(
109 config: T3ServerConfig | null | undefined,
110 fallbackModelSelection: ModelSelection | null,
111): ReadonlyArray<ModelOption> {
112 const options = new Map<string, ModelOption>();
113
114 for (const provider of config?.providers ?? []) {
115 if (!provider.enabled || !provider.installed || provider.auth.status === "unauthenticated") {
116 continue;
117 }
118
119 const providerLabel = providerDisplayLabel(provider);
120 for (const model of provider.models) {
121 const key = `${provider.instanceId}:${model.slug}`;
122 options.set(key, {
123 key,
124 label: model.name,
125 subtitle: providerLabel,
126 providerKey: provider.instanceId,
127 providerLabel,
128 providerDriver: provider.driver,
129 isDefault: model.isDefault === true,
130 isLegacy: model.isLegacy === true,
131 capabilities: model.capabilities,
132 selection: normalizeSelectionOptions(
133 {
134 instanceId: provider.instanceId,
135 model: model.slug,
136 },
137 model.capabilities,
138 ),
139 });
140 }
141 }
142
143 if (fallbackModelSelection) {
144 const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`;
145 const existing = options.get(key);
146 if (existing) {
147 options.set(key, {
148 ...existing,
149 selection: normalizeSelectionOptions(fallbackModelSelection, existing.capabilities),
150 });
151 } else {
152 const providerLabel = fallbackModelSelection.instanceId;
153 options.set(key, {
154 key,
155 label: fallbackModelSelection.model,
156 subtitle: providerLabel,
157 providerKey: fallbackModelSelection.instanceId,
158 providerLabel,
159 providerDriver: fallbackModelSelection.instanceId,
160 isDefault: false,
161 isLegacy: false,
162 capabilities: null,
163 selection: fallbackModelSelection,
164 });
165 }
166 }
167
168 return [...options.values()];
169}
170
171export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyArray<ProviderGroup> {
172 const groups = new Map<string, { providerLabel: string; models: ModelOption[] }>();
173 for (const option of options) {
174 const existing = groups.get(option.providerKey);
175 if (existing) {
176 existing.models.push(option);
177 } else {
178 groups.set(option.providerKey, {
179 providerLabel: option.providerLabel,
180 models: [option],
181 });
182 }
183 }
184
185 return [...groups.entries()].map(([providerKey, group]) => ({
186 providerKey,
187 providerLabel: group.providerLabel,
188 models: group.models,
189 }));
190}