import { ProviderCredentialRepository, } from '../../core/auth/index.js';
import { BuiltinLlmProviderRegistry } from './registry/index.js';
/**
 * Main LLM application service. It is the public entry point for creating
 * provider adapters and resolving provider/model ownership.
 */
export class LlmAdapterService {
    static registry = BuiltinLlmProviderRegistry.create();
    static create(input = {}) {
        const provider = LlmAdapterService.resolveProvider(input);
        return LlmAdapterService.registry.createAdapter(LlmAdapterService.withStoredOAuthCredential(input, provider));
    }
    static resolveProvider(input = {}) {
        return LlmAdapterService.registry.resolveProvider(input).provider;
    }
    static inferProvider(model) {
        return LlmAdapterService.registry.inferProvider(model);
    }
    static withStoredOAuthCredential(input, provider) {
        const credentials = input.credentials;
        if (credentials?.credential || LlmAdapterService.hasApiKey(credentials?.apiKey)) {
            return input;
        }
        const credential = LlmAdapterService.resolveStoredOAuthCredential(provider, credentials?.credentialStorePath);
        if (!credential) {
            return input;
        }
        return {
            ...input,
            credentials: {
                ...(credentials ?? {}),
                credential,
            },
        };
    }
    static resolveStoredOAuthCredential(provider, storePath) {
        const credential = new ProviderCredentialRepository({ storePath }).get(provider);
        return credential?.type === 'oauth' ? credential : undefined;
    }
    static hasApiKey(value) {
        return typeof value === 'string' && value.trim().length > 0;
    }
}
//# sourceMappingURL=service.js.map