export async function executeLocalModel(config, messages) {
    const baseUrl = config.baseUrl || getDefaultBaseUrl(config.provider);
    if (config.provider === 'ollama') {
        return executeOllama(baseUrl, config, messages);
    }
    else if (config.provider === 'lm-studio') {
        return executeLMStudio(baseUrl, config, messages);
    }
    else if (config.provider === 'custom') {
        return executeCustom(baseUrl, config, messages);
    }
    else {
        throw new Error(`Unsupported local model provider: ${config.provider}`);
    }
}
function getDefaultBaseUrl(provider) {
    switch (provider) {
        case 'ollama':
            return 'http://localhost:11434';
        case 'lm-studio':
            return 'http://localhost:1234';
        case 'custom':
            return 'http://localhost:8000';
        default:
            return 'http://localhost:11434';
    }
}
async function executeOllama(baseUrl, config, messages) {
    const prompt = formatMessagesForOllama(messages);
    const response = await fetch(`${baseUrl}/api/generate`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            model: config.model,
            prompt: prompt,
            stream: false,
            options: {
                temperature: config.temperature ?? 0.7,
                top_p: config.topP,
                num_predict: config.maxTokens,
            },
        }),
    });
    if (!response.ok) {
        const error = await response.text();
        throw new Error(`Ollama API error: ${response.status} - ${error}`);
    }
    const data = await response.json();
    return {
        result: data.response || 'No response',
        model: config.model,
        usage: {
            totalTokens: data.eval_count ? (data.prompt_eval_count || 0) + data.eval_count : undefined,
            promptTokens: data.prompt_eval_count,
            completionTokens: data.eval_count,
        },
        cost: 0,
    };
}
async function executeLMStudio(baseUrl, config, messages) {
    const response = await fetch(`${baseUrl}/v1/chat/completions`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            model: config.model,
            messages: messages,
            temperature: config.temperature ?? 0.7,
            max_tokens: config.maxTokens,
            top_p: config.topP,
            frequency_penalty: config.frequencyPenalty,
            presence_penalty: config.presencePenalty,
        }),
    });
    if (!response.ok) {
        const error = await response.text();
        throw new Error(`LM Studio API error: ${response.status} - ${error}`);
    }
    const data = await response.json();
    const usage = data.usage || {};
    return {
        result: data.choices?.[0]?.message?.content || 'No response',
        model: data.model || config.model,
        usage: {
            totalTokens: usage.total_tokens,
            promptTokens: usage.prompt_tokens,
            completionTokens: usage.completion_tokens,
        },
        cost: 0,
    };
}
async function executeCustom(baseUrl, config, messages) {
    const response = await fetch(`${baseUrl}/v1/chat/completions`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            model: config.model,
            messages: messages,
            temperature: config.temperature ?? 0.7,
            max_tokens: config.maxTokens,
            top_p: config.topP,
            frequency_penalty: config.frequencyPenalty,
            presence_penalty: config.presencePenalty,
        }),
    });
    if (!response.ok) {
        const error = await response.text();
        throw new Error(`Custom model API error: ${response.status} - ${error}`);
    }
    const data = await response.json();
    const usage = data.usage || {};
    return {
        result: data.choices?.[0]?.message?.content || 'No response',
        model: data.model || config.model,
        usage: {
            totalTokens: usage.total_tokens,
            promptTokens: usage.prompt_tokens,
            completionTokens: usage.completion_tokens,
        },
        cost: 0,
    };
}
function formatMessagesForOllama(messages) {
    let prompt = '';
    for (const msg of messages) {
        if (msg.role === 'system') {
            prompt += `System: ${msg.content}\n\n`;
        }
        else if (msg.role === 'user') {
            prompt += `User: ${msg.content}\n\n`;
        }
        else if (msg.role === 'assistant') {
            prompt += `Assistant: ${msg.content}\n\n`;
        }
    }
    prompt += 'Assistant:';
    return prompt;
}
//# sourceMappingURL=local-model-provider.js.map