package com.seer.teach.ai.client.model.local;

import com.seer.teach.ai.client.model.AbstractAiModelClient;
import com.seer.teach.ai.client.model.config.ModelConfig;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.ai.ollama.api.OllamaChatOptions;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

/**
 * 本地模型管理器
 * 专门负责Ollama等本地AI模型管理
 *
 * @author Captain
 * @since 2025-12-26
 */
@Component
public class LocalModelClient extends AbstractAiModelClient {
    
    @Override
    public String getPlatformName() {
        return "local";
    }
    
    @Override
    public Optional<ChatModel> createChatModel(ModelConfig modelConfig) {
        OllamaChatOptions chatOptions = OllamaChatOptions.builder()
                .model(modelConfig.getModel())
                .build();

        OllamaApi ollamaApi = OllamaApi.builder()
                .baseUrl(modelConfig.getUrl())
                .restClientBuilder(restClientBuilder)
                .webClientBuilder(webClientBuilder)
                .responseErrorHandler(responseErrorHandler)
                .build();

        return Optional.of(OllamaChatModel.builder()
                .ollamaApi(ollamaApi)
                .defaultOptions(chatOptions)
                .retryTemplate(retryTemplate)
                .toolCallingManager(toolCallingManager)
                .observationRegistry(observationRegistry)
                .toolExecutionEligibilityPredicate(toolExecutionEligibilityPredicate)
                .build());
    }

    @Override
    public Optional<ChatOptions> createChatOptions(Map<String, Object> options) {
        return Optional.empty();
    }
}