Files
goose/ui/desktop/src/components/settings/models/modelInterface.ts
T
Douwe Osinga f583255088 Show provider failures (#6744)
Co-authored-by: Douwe Osinga <douwe@squareup.com>
2026-01-27 14:53:06 -05:00

74 lines
2.1 KiB
TypeScript

import { ProviderDetails, getProviderModels } from '../../../api';
export default interface Model {
id?: number; // Make `id` optional to allow user-defined models
name: string;
provider: string;
lastUsed?: string;
alias?: string; // optional model display name
subtext?: string; // goes below model name if not the provider
context_limit?: number; // optional context limit override
request_params?: Record<string, unknown>; // provider-specific request parameters
}
export function createModelStruct(
modelName: string,
provider: string,
id?: number, // Make `id` optional to allow user-defined models
lastUsed?: string,
alias?: string, // optional model display name
subtext?: string
): Model {
// use the metadata to create a Model
return {
name: modelName,
provider: provider,
alias: alias,
id: id,
lastUsed: lastUsed,
subtext: subtext,
};
}
export async function getProviderMetadata(
providerName: string,
getProvidersFunc: (b: boolean) => Promise<ProviderDetails[]>
) {
const providers = await getProvidersFunc(false);
const matches = providers.find((providerMatch) => providerMatch.name === providerName);
if (!matches) {
throw Error(`No match for provider: ${providerName}`);
}
return matches.metadata;
}
export interface ProviderModelsResult {
provider: ProviderDetails;
models: string[] | null;
error: string | null;
}
export async function fetchModelsForProviders(
activeProviders: ProviderDetails[]
): Promise<ProviderModelsResult[]> {
const modelPromises = activeProviders.map(async (p) => {
try {
const response = await getProviderModels({
path: { name: p.name },
throwOnError: true,
});
const models = response.data || [];
return { provider: p, models, error: null };
} catch (e: unknown) {
const errorMessage = `Failed to fetch models for ${p.name}${e instanceof Error ? `: ${e.message}` : ''}`;
return {
provider: p,
models: null,
error: errorMessage,
};
}
});
return await Promise.all(modelPromises);
}