package com.rj.config;

import com.rj.aiservice.CstAIService;
import com.rj.repository.RedisChatMemoryStore;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Author: 李中华  wx: spllzh  email(qq): 28668817@qq.com
 * Date: 2025/8/4 10:24
 **/
@Configuration
public class CommonConfig {

    @Autowired
    private OpenAiChatModel openAiChatModel;

   // @Bean
    public CstAIService cstAIService() {

        CstAIService cstAIService = AiServices.builder(CstAIService.class)
                .chatModel(openAiChatModel)
                .build();

        return cstAIService;
    }
    @Bean
    public ChatMemory chatMemory() {

        MessageWindowChatMemory build = MessageWindowChatMemory.builder()
                .maxMessages(10)
                .build();
        return  build;
    }


    @Autowired
    RedisChatMemoryStore redisChatMemoryStore;



    @Bean
    public ChatMemoryProvider chatMemoryProvider() {

       ChatMemoryProvider chatMemoryProvider = new ChatMemoryProvider() {
           @Override
           public ChatMemory get(Object memoryId) {

               MessageWindowChatMemory chatMemory =  MessageWindowChatMemory.builder()
                       .id(memoryId)
                       .maxMessages(10)
                       .chatMemoryStore(redisChatMemoryStore)
                       .build();
               return  chatMemory;
           }
       };

       return chatMemoryProvider;
    }


}
