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

modelOptions.ts

189 lines5.7 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 buildModelOptions(
108 config: T3ServerConfig | null | undefined,
109 fallbackModelSelection: ModelSelection | null,
110): ReadonlyArray<ModelOption> {
111 const options = new Map<string, ModelOption>();
112​
113 for (const provider of config?.providers ?? []) {
114 if (!provider.enabled || !provider.installed || provider.auth.status === "unauthenticated") {
115 continue;
116 }
117​
118 const providerLabel = providerDisplayLabel(provider);
119 for (const model of provider.models) {
120 const key = `${provider.instanceId}:${model.slug}`;
121 options.set(key, {
122 key,
123 label: model.name,
124 subtitle: providerLabel,
125 providerKey: provider.instanceId,
126 providerLabel,
127 providerDriver: provider.driver,
128 isDefault: model.isDefault === true,
129 isLegacy: model.isLegacy === true,
130 capabilities: model.capabilities,
131 selection: normalizeSelectionOptions(
132 {
133 instanceId: provider.instanceId,
134 model: model.slug,
135 },
136 model.capabilities,
137 ),
138 });
139 }
140 }
141​
142 if (fallbackModelSelection) {
143 const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`;
144 const existing = options.get(key);
145 if (existing) {
146 options.set(key, {
147 ...existing,
148 selection: normalizeSelectionOptions(fallbackModelSelection, existing.capabilities),
149 });
150 } else {
151 const providerLabel = fallbackModelSelection.instanceId;
152 options.set(key, {
153 key,
154 label: fallbackModelSelection.model,
155 subtitle: providerLabel,
156 providerKey: fallbackModelSelection.instanceId,
157 providerLabel,
158 providerDriver: fallbackModelSelection.instanceId,
159 isDefault: false,
160 isLegacy: false,
161 capabilities: null,
162 selection: fallbackModelSelection,
163 });
164 }
165 }
166​
167 return [...options.values()];
169​
170export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyArray<ProviderGroup> {
171 const groups = new Map<string, { providerLabel: string; models: ModelOption[] }>();
172 for (const option of options) {
173 const existing = groups.get(option.providerKey);
174 if (existing) {
175 existing.models.push(option);
176 } else {
177 groups.set(option.providerKey, {
178 providerLabel: option.providerLabel,
179 models: [option],
180 });
181 }
182 }
183​
184 return [...groups.entries()].map(([providerKey, group]) => ({
185 providerKey,
186 providerLabel: group.providerLabel,
187 models: group.models,
188 }));