001/*
002 *  Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.agentsflex.core.model.chat.log;
017
018import com.agentsflex.core.model.chat.ChatConfig;
019
020import java.util.function.Consumer;
021
022public class DefaultChatMessageLogger implements IChatMessageLogger {
023
024    private final Consumer<String> logConsumer;
025
026    public DefaultChatMessageLogger() {
027        this(System.out::println);
028    }
029
030    public DefaultChatMessageLogger(Consumer<String> logConsumer) {
031        this.logConsumer = logConsumer != null ? logConsumer : System.out::println;
032    }
033
034    @Override
035    public void logRequest(ChatConfig config, String message) {
036        if (shouldLog(config)) {
037            String provider = getProviderName(config);
038            String model = getModelName(config);
039            logConsumer.accept(String.format("[%s/%s] >>>> request: %s", provider, model, message));
040        }
041    }
042
043    @Override
044    public void logResponse(ChatConfig config, String message) {
045        if (shouldLog(config)) {
046            String provider = getProviderName(config);
047            String model = getModelName(config);
048            logConsumer.accept(String.format("[%s/%s] <<<< response: %s", provider, model, message));
049        }
050    }
051
052    private boolean shouldLog(ChatConfig config) {
053        return config != null && config.isLogEnabled();
054    }
055
056    private String getProviderName(ChatConfig config) {
057        String provider = config.getProvider();
058        return provider != null ? provider : "unknow";
059    }
060
061    private String getModelName(ChatConfig config) {
062        String model = config.getModel();
063        return model != null ? model : "unknow";
064    }
065}