package org.ruoyi.service.chat.impl.provider;


import dev.langchain4j.community.model.zhipu.ZhipuAiChatModel;
import dev.langchain4j.community.model.zhipu.ZhipuAiStreamingChatModel;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ruoyi.common.chat.domain.dto.request.ChatRequest;
import org.ruoyi.common.chat.domain.vo.chat.ChatModelVo;
import org.ruoyi.enums.ChatModeType;
import org.ruoyi.observability.MyChatModelListener;
import org.ruoyi.service.chat.AbstractChatService;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 智谱AI服务调用
 *
 * @author zengxb
 * @date 2026/02/26
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ZhiPuChatServiceImpl implements AbstractChatService {

    @Override
    public StreamingChatModel buildStreamingChatModel(ChatModelVo chatModelVo, ChatRequest chatRequest) {
        return ZhipuAiStreamingChatModel.builder()
            .apiKey(chatModelVo.getApiKey())
            .model(chatModelVo.getModelName())
            .listeners(List.of(new MyChatModelListener()))
            .build();
    }

    @Override
    public ChatModel buildChatModel(ChatModelVo chatModelVo) {
        return ZhipuAiChatModel.builder()
            .apiKey(chatModelVo.getApiKey())
            .model(chatModelVo.getModelName())
            .build();
    }

    @Override
    public String getProviderName() {
        return ChatModeType.ZHI_PU.getCode();
    }
}
