/**
 * Tool LLM — internal LLM access for tool operations.
 *
 * Provides a lightweight interface for tool-internal LLM calls:
 * semantic diff, auto-heal, commit messages, prompt hooks, etc.
 *
 * Resolution order:
 *   1. tools.llmProvider + tools.llmModel from config (if both set)
 *   2. Currently selected provider/model from providerRegistry
 *
 * Design constraints:
 *   - Never throws from ToolLLM.chat() — returns empty string on any error
 *   - Logs failures via logger.debug (tool LLM failures are expected when no key)
 *   - Singleton pattern — import `toolLLM` for use
 */
import { logger } from '@pellux/goodvibes-sdk/platform/utils/logger';
import { summarizeError } from '@pellux/goodvibes-sdk/platform/utils/error-display';
/**
 * Resolve the LLM provider and model to use for tool-internal operations.
 *
 * Resolution order:
 *   1. If tools.llmProvider + tools.llmModel are both set in config, use those.
 *   2. Otherwise fall back to the currently selected provider/model.
 *
 * Returns null if resolution fails (e.g. unknown provider name).
 */
export function resolveToolLLM(deps) {
    try {
        const cfgProvider = deps.configManager.get('tools.llmProvider');
        const cfgModel = deps.configManager.get('tools.llmModel');
        if (cfgProvider && cfgModel) {
            // Explicit config: resolve by provider name
            const provider = deps.providerRegistry.get(cfgProvider);
            return { provider, modelId: cfgModel };
        }
        // Fallback: use currently selected provider/model
        const currentDef = deps.providerRegistry.getCurrentModel();
        const provider = deps.providerRegistry.getForModel(currentDef.id, currentDef.provider);
        return { provider, modelId: currentDef.id };
    }
    catch (err) {
        logger.debug('resolveToolLLM: failed to resolve provider/model', { error: summarizeError(err) });
        return null;
    }
}
/**
 * ToolLLM — lightweight LLM interface for internal tool operations.
 *
 * Usage:
 *   const result = await toolLLM.chat('Generate a commit message for: ...');
 *
 * Always returns a string — empty string on any failure.
 */
export class ToolLLM {
    deps;
    constructor(deps) {
        this.deps = deps;
    }
    /**
     * Send a single-turn prompt to the tool LLM.
     *
     * @param prompt   The user prompt to send.
     * @param options  Optional maxTokens and systemPrompt.
     * @returns        The assistant's text response, or empty string on failure.
     */
    async chat(prompt, options = {}) {
        try {
            const resolved = resolveToolLLM(this.deps);
            if (!resolved) {
                logger.debug('ToolLLM.chat: no provider resolved, returning empty string');
                return '';
            }
            const { provider, modelId } = resolved;
            const response = await provider.chat({
                model: modelId,
                messages: [{ role: 'user', content: prompt }],
                maxTokens: options.maxTokens ?? 1024,
                systemPrompt: options.systemPrompt,
            });
            return response.content ?? '';
        }
        catch (err) {
            logger.debug('ToolLLM.chat: request failed (non-fatal)', { error: summarizeError(err) });
            return '';
        }
    }
}
