import { OpenAiCompatibleModelDiscoveryService, } from '../../../core/llm/adapters/openai-compatible/index.js';
import { LlmProviderInference } from '../registry/provider-inference.js';
import { BUILT_IN_MODEL_GROUPS, } from './model-catalog.js';
import { ModelPolicyService } from './model-policy-service.js';
/**
 * Resolves the shared model picker contract for control-plane clients. Static
 * provider shortlists and local provider discovery meet here so TUI, web, task
 * forms, and slash-command pickers render the same provider-aware model list.
 */
export class ModelOptionsService {
    static async resolve(context = {}) {
        const credentialModes = context.credentialModes ?? {};
        const builtInGroups = BUILT_IN_MODEL_GROUPS.map((group) => ModelOptionsService.toBuiltInModelOptionGroup(group, credentialModes));
        const discoveredGroups = await ModelOptionsService.resolveDiscoveredModelOptionGroups(context);
        return {
            groups: [
                ...builtInGroups,
                ...discoveredGroups,
            ],
        };
    }
    static toBuiltInModelOptionGroup(group, credentialModes) {
        const options = group.models.map((model) => {
            const provider = LlmProviderInference.inferBuiltin(model);
            return ModelPolicyService.buildCredentialAwareModelOption({
                model,
                provider,
                credentialMode: credentialModes[provider],
            });
        });
        return {
            label: group.label,
            models: group.models,
            options,
            source: 'built-in',
        };
    }
    static async resolveDiscoveredModelOptionGroups(context) {
        const sources = context.openAiCompatibleSources ?? [];
        const groups = await Promise.all(sources.map((source) => ModelOptionsService.resolveOpenAiCompatibleModelOptionGroup(source, context)));
        return groups.flatMap((group) => group ?? []);
    }
    static async resolveOpenAiCompatibleModelOptionGroup(source, context) {
        try {
            const models = await OpenAiCompatibleModelDiscoveryService.listModels({
                ...source,
                fetchImpl: context.fetchImpl,
                signal: context.signal,
            });
            if (models.length === 0) {
                return undefined;
            }
            return {
                label: source.profile.modelDiscovery.label,
                models: models.map((model) => model.id),
                options: models.map((model) => ModelPolicyService.buildCredentialAwareModelOption({
                    model: model.id,
                    provider: source.profile.id,
                    label: model.name,
                    credentialMode: 'api-key',
                })),
                source: source.profile.modelDiscovery.source,
            };
        }
        catch {
            return undefined;
        }
    }
}
//# sourceMappingURL=model-options-service.js.map