Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

modelOptions.ts

205 lines6.2 KB

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 return provider.instanceId;
40​
41function normalizeSelectionOptions(
42 selection: ModelSelection,
43 capabilities: ModelCapabilities | null,
44): ModelSelection {
45 if (!capabilities) {
46 return selection;
47 }
48 const options = buildProviderOptionSelectionsFromDescriptors(
49 getProviderOptionDescriptors({
50 caps: capabilities,
51 selections: selection.options,
52 }),
53 );
54 return options
55 ? { ...selection, options }
56 : {
57 instanceId: selection.instanceId,
58 model: selection.model,
59 };
61​
62/**
63 * A stored model selection is only usable when its provider instance is
64 * currently enabled, installed, and authenticated on the server. Returns the
65 * selection unchanged when usable, otherwise `null` so callers fall through to
66 * the server's default model. A missing config (environment offline) cannot be
67 * validated, so stored selections pass through untouched.
68 */
69export function resolveSelectableModelSelection(
70 config: T3ServerConfig | null | undefined,
71 selection: ModelSelection | null,
72): ModelSelection | null {
73 if (!selection || !config) {
74 return selection;
75 }
76 const provider = config.providers.find(
77 (candidate) => candidate.instanceId === selection.instanceId,
78 );
79 return provider &&
80 provider.enabled &&
81 provider.installed &&
82 provider.auth.status !== "unauthenticated"
83 ? selection
84 : null;
86​
87/**
88 * Like resolveSelectableModelSelection, but additionally rejects legacy
89 * models. Used for implicit defaults (stored draft, project last-used): a
90 * new thread should never quietly start on a legacy model, so those fall
91 * through to the provider's default instead. Explicit picks in the settings
92 * sheet are unaffected.
93 */
94export function resolveDefaultableModelSelection(
95 config: T3ServerConfig | null | undefined,
96 selection: ModelSelection | null,
97): ModelSelection | null {
98 const usable = resolveSelectableModelSelection(config, selection);
99 if (!usable || !config) {
100 return usable;
101 }
102 const provider = config.providers.find((candidate) => candidate.instanceId === usable.instanceId);
103 const model = provider?.models.find((candidate) => candidate.slug === usable.model);
104 return model?.isLegacy === true ? null : usable;
106​
107export function resolveNewTaskModelSelection(input: {
108 readonly draftSelection: ModelSelection | null;
109 readonly projectDefaultSelection: ModelSelection | null;
110 readonly stickySelection: ModelSelection | null;
111 readonly modelOptions: ReadonlyArray<ModelOption>;
112}): ModelSelection | null {
113 return (
114 input.draftSelection ??
115 input.projectDefaultSelection ??
116 input.stickySelection ??
117 input.modelOptions.find((option) => option.isDefault)?.selection ??
118 input.modelOptions[0]?.selection ??
119 null
120 );
122​
123export function buildModelOptions(
124 config: T3ServerConfig | null | undefined,
125 fallbackModelSelection: ModelSelection | null,
126): ReadonlyArray<ModelOption> {
127 const options = new Map<string, ModelOption>();
128​
129 for (const provider of config?.providers ?? []) {
130 if (!provider.enabled || !provider.installed || provider.auth.status === "unauthenticated") {
131 continue;
132 }
133​
134 const providerLabel = providerDisplayLabel(provider);
135 for (const model of provider.models) {
136 const key = `${provider.instanceId}:${model.slug}`;
137 options.set(key, {
138 key,
139 label: model.name,
140 subtitle: model.subProvider ?? "",
141 providerKey: provider.instanceId,
142 providerLabel,
143 providerDriver: provider.driver,
144 isDefault: model.isDefault === true,
145 isLegacy: model.isLegacy === true,
146 capabilities: model.capabilities,
147 selection: normalizeSelectionOptions(
148 {
149 instanceId: provider.instanceId,
150 model: model.slug,
151 },
152 model.capabilities,
153 ),
154 });
155 }
156 }
157​
158 if (fallbackModelSelection) {
159 const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`;
160 const existing = options.get(key);
161 if (existing) {
162 options.set(key, {
163 ...existing,
164 selection: normalizeSelectionOptions(fallbackModelSelection, existing.capabilities),
165 });
166 } else {
167 const providerLabel = fallbackModelSelection.instanceId;
168 options.set(key, {
169 key,
170 label: fallbackModelSelection.model,
171 subtitle: "",
172 providerKey: fallbackModelSelection.instanceId,
173 providerLabel,
174 providerDriver: fallbackModelSelection.instanceId,
175 isDefault: false,
176 isLegacy: false,
177 capabilities: null,
178 selection: fallbackModelSelection,
179 });
180 }
181 }
182​
183 return [...options.values()];
185​
186export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyArray<ProviderGroup> {
187 const groups = new Map<string, { providerLabel: string; models: ModelOption[] }>();
188 for (const option of options) {
189 const existing = groups.get(option.providerKey);
190 if (existing) {
191 existing.models.push(option);
192 } else {
193 groups.set(option.providerKey, {
194 providerLabel: option.providerLabel,
195 models: [option],
196 });
197 }
198 }
199​
200 return [...groups.entries()].map(([providerKey, group]) => ({
201 providerKey,
202 providerLabel: group.providerLabel,
203 models: group.models,
204 }));