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

modelOptions.ts

141 lines4.0 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 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;
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 };
60​
61export function buildModelOptions(
62 config: T3ServerConfig | null | undefined,
63 fallbackModelSelection: ModelSelection | null,
64): ReadonlyArray<ModelOption> {
65 const options = new Map<string, ModelOption>();
66​
67 for (const provider of config?.providers ?? []) {
68 if (!provider.enabled || !provider.installed || provider.auth.status === "unauthenticated") {
69 continue;
70 }
71​
72 const providerLabel = providerDisplayLabel(provider);
73 for (const model of provider.models) {
74 const key = `${provider.instanceId}:${model.slug}`;
75 options.set(key, {
76 key,
77 label: model.name,
78 subtitle: providerLabel,
79 providerKey: provider.instanceId,
80 providerLabel,
81 providerDriver: provider.driver,
82 isDefault: model.isDefault === true,
83 capabilities: model.capabilities,
84 selection: normalizeSelectionOptions(
85 {
86 instanceId: provider.instanceId,
87 model: model.slug,
88 },
89 model.capabilities,
90 ),
91 });
92 }
93 }
94​
95 if (fallbackModelSelection) {
96 const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`;
97 const existing = options.get(key);
98 if (existing) {
99 options.set(key, {
100 ...existing,
101 selection: normalizeSelectionOptions(fallbackModelSelection, existing.capabilities),
102 });
103 } else {
104 const providerLabel = fallbackModelSelection.instanceId;
105 options.set(key, {
106 key,
107 label: fallbackModelSelection.model,
108 subtitle: providerLabel,
109 providerKey: fallbackModelSelection.instanceId,
110 providerLabel,
111 providerDriver: fallbackModelSelection.instanceId,
112 isDefault: false,
113 capabilities: null,
114 selection: fallbackModelSelection,
115 });
116 }
117 }
118​
119 return [...options.values()];
121​
122export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyArray<ProviderGroup> {
123 const groups = new Map<string, { providerLabel: string; models: ModelOption[] }>();
124 for (const option of options) {
125 const existing = groups.get(option.providerKey);
126 if (existing) {
127 existing.models.push(option);
128 } else {
129 groups.set(option.providerKey, {
130 providerLabel: option.providerLabel,
131 models: [option],
132 });
133 }
134 }
135​
136 return [...groups.entries()].map(([providerKey, group]) => ({
137 providerKey,
138 providerLabel: group.providerLabel,
139 models: group.models,
140 }));