Files
t3code/apps/mobile/src/lib/modelOptions.ts
T

142 lines
4.0 KiB
TypeScript

import type {
ModelCapabilities,
ModelSelection,
ServerConfig as T3ServerConfig,
} from "@t3tools/contracts";
import {
buildProviderOptionSelectionsFromDescriptors,
getProviderOptionDescriptors,
} from "@t3tools/shared/model";
export type ModelOption = {
readonly key: string;
readonly label: string;
readonly subtitle: string;
readonly providerKey: string;
readonly providerLabel: string;
readonly providerDriver: string;
readonly isDefault: boolean;
readonly capabilities: ModelCapabilities | null;
readonly selection: ModelSelection;
};
export type ProviderGroup = {
readonly providerKey: string;
readonly providerLabel: string;
readonly models: ReadonlyArray<ModelOption>;
};
function providerDisplayLabel(provider: {
readonly displayName?: string | undefined;
readonly driver: string;
readonly instanceId: string;
}): string {
if (provider.displayName) return provider.displayName;
if (provider.driver === "codex") return "Codex";
if (provider.driver === "claudeAgent") return "Claude";
return provider.instanceId;
}
function normalizeSelectionOptions(
selection: ModelSelection,
capabilities: ModelCapabilities | null,
): ModelSelection {
if (!capabilities) {
return selection;
}
const options = buildProviderOptionSelectionsFromDescriptors(
getProviderOptionDescriptors({
caps: capabilities,
selections: selection.options,
}),
);
return options
? { ...selection, options }
: {
instanceId: selection.instanceId,
model: selection.model,
};
}
export function buildModelOptions(
config: T3ServerConfig | null | undefined,
fallbackModelSelection: ModelSelection | null,
): ReadonlyArray<ModelOption> {
const options = new Map<string, ModelOption>();
for (const provider of config?.providers ?? []) {
if (!provider.enabled || !provider.installed || provider.auth.status === "unauthenticated") {
continue;
}
const providerLabel = providerDisplayLabel(provider);
for (const model of provider.models) {
const key = `${provider.instanceId}:${model.slug}`;
options.set(key, {
key,
label: model.name,
subtitle: providerLabel,
providerKey: provider.instanceId,
providerLabel,
providerDriver: provider.driver,
isDefault: model.isDefault === true,
capabilities: model.capabilities,
selection: normalizeSelectionOptions(
{
instanceId: provider.instanceId,
model: model.slug,
},
model.capabilities,
),
});
}
}
if (fallbackModelSelection) {
const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`;
const existing = options.get(key);
if (existing) {
options.set(key, {
...existing,
selection: normalizeSelectionOptions(fallbackModelSelection, existing.capabilities),
});
} else {
const providerLabel = fallbackModelSelection.instanceId;
options.set(key, {
key,
label: fallbackModelSelection.model,
subtitle: providerLabel,
providerKey: fallbackModelSelection.instanceId,
providerLabel,
providerDriver: fallbackModelSelection.instanceId,
isDefault: false,
capabilities: null,
selection: fallbackModelSelection,
});
}
}
return [...options.values()];
}
export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyArray<ProviderGroup> {
const groups = new Map<string, { providerLabel: string; models: ModelOption[] }>();
for (const option of options) {
const existing = groups.get(option.providerKey);
if (existing) {
existing.models.push(option);
} else {
groups.set(option.providerKey, {
providerLabel: option.providerLabel,
models: [option],
});
}
}
return [...groups.entries()].map(([providerKey, group]) => ({
providerKey,
providerLabel: group.providerLabel,
models: group.models,
}));
}