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 capabilities: ModelCapabilities | null;
20 readonly selection: ModelSelection;
21};
22
23export type ProviderGroup = {
24 readonly providerKey: string;
25 readonly providerLabel: string;
26 readonly models: ReadonlyArray<ModelOption>;
27};
28
29function providerDisplayLabel(provider: {
30 readonly displayName?: string | undefined;
31 readonly driver: string;
32 readonly instanceId: string;
33}): string {
34 if (provider.displayName) return provider.displayName;
35 if (provider.driver === "codex") return "Codex";
36 if (provider.driver === "claudeAgent") return "Claude";
37 return provider.instanceId;
38}
39
40function normalizeSelectionOptions(
41 selection: ModelSelection,
42 capabilities: ModelCapabilities | null,
43): ModelSelection {
44 if (!capabilities) {
45 return selection;
46 }
47 const options = buildProviderOptionSelectionsFromDescriptors(
48 getProviderOptionDescriptors({
49 caps: capabilities,
50 selections: selection.options,
51 }),
52 );
53 return options
54 ? { ...selection, options }
55 : {
56 instanceId: selection.instanceId,
57 model: selection.model,
58 };
59}
60
61/**
62 * A stored model selection is only usable when its provider instance is
63 * currently enabled, installed, and authenticated on the server. Returns the
64 * selection unchanged when usable, otherwise `null` so callers fall through to
65 * the server's default model. A missing config (environment offline) cannot be
66 * validated, so stored selections pass through untouched.
67 */
68export function resolveSelectableModelSelection(
69 config: T3ServerConfig | null | undefined,
70 selection: ModelSelection | null,
71): ModelSelection | null {
72 if (!selection || !config) {
73 return selection;
74 }
75 const provider = config.providers.find(
76 (candidate) => candidate.instanceId === selection.instanceId,
77 );
78 return provider &&
79 provider.enabled &&
80 provider.installed &&
81 provider.auth.status !== "unauthenticated"
82 ? selection
83 : null;
84}
85
86export function buildModelOptions(
87 config: T3ServerConfig | null | undefined,
88 fallbackModelSelection: ModelSelection | null,
89): ReadonlyArray<ModelOption> {
90 const options = new Map<string, ModelOption>();
91
92 for (const provider of config?.providers ?? []) {
93 if (!provider.enabled || !provider.installed || provider.auth.status === "unauthenticated") {
94 continue;
95 }
96
97 const providerLabel = providerDisplayLabel(provider);
98 for (const model of provider.models) {
99 const key = `${provider.instanceId}:${model.slug}`;
100 options.set(key, {
101 key,
102 label: model.name,
103 subtitle: providerLabel,
104 providerKey: provider.instanceId,
105 providerLabel,
106 providerDriver: provider.driver,
107 isDefault: model.isDefault === true,
108 capabilities: model.capabilities,
109 selection: normalizeSelectionOptions(
110 {
111 instanceId: provider.instanceId,
112 model: model.slug,
113 },
114 model.capabilities,
115 ),
116 });
117 }
118 }
119
120 if (fallbackModelSelection) {
121 const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`;
122 const existing = options.get(key);
123 if (existing) {
124 options.set(key, {
125 ...existing,
126 selection: normalizeSelectionOptions(fallbackModelSelection, existing.capabilities),
127 });
128 } else {
129 const providerLabel = fallbackModelSelection.instanceId;
130 options.set(key, {
131 key,
132 label: fallbackModelSelection.model,
133 subtitle: providerLabel,
134 providerKey: fallbackModelSelection.instanceId,
135 providerLabel,
136 providerDriver: fallbackModelSelection.instanceId,
137 isDefault: false,
138 capabilities: null,
139 selection: fallbackModelSelection,
140 });
141 }
142 }
143
144 return [...options.values()];
145}
146
147export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyArray<ProviderGroup> {
148 const groups = new Map<string, { providerLabel: string; models: ModelOption[] }>();
149 for (const option of options) {
150 const existing = groups.get(option.providerKey);
151 if (existing) {
152 existing.models.push(option);
153 } else {
154 groups.set(option.providerKey, {
155 providerLabel: option.providerLabel,
156 models: [option],
157 });
158 }
159 }
160
161 return [...groups.entries()].map(([providerKey, group]) => ({
162 providerKey,
163 providerLabel: group.providerLabel,
164 models: group.models,
165 }));
166}