content: /*
   * Copyright 2024-2025 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      https://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package com.alibaba.cloud.ai.autoconfigure.arms;
  
  import com.alibaba.cloud.ai.observation.client.prompt.PromptMetadataAwareChatClientObservationConvention;
  import com.alibaba.cloud.ai.observation.model.ChatModelInputObservationHandler;
  import com.alibaba.cloud.ai.observation.model.ChatModelOutputObservationHandler;
  import com.alibaba.cloud.ai.observation.model.PromptMetadataAwareChatModelObservationConvention;
  import com.alibaba.cloud.ai.tool.ObservableToolCallingManager;
  import io.micrometer.observation.ObservationRegistry;
  import org.springframework.ai.chat.client.observation.ChatClientObservationConvention;
  import org.springframework.ai.chat.model.ChatModel;
  import org.springframework.ai.chat.observation.ChatModelObservationConvention;
  import org.springframework.ai.model.tool.ToolCallingManager;
  import org.springframework.ai.tool.execution.ToolExecutionExceptionProcessor;
  import org.springframework.ai.tool.resolution.ToolCallbackResolver;
  import org.springframework.beans.factory.ObjectProvider;
  import org.springframework.boot.autoconfigure.AutoConfiguration;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  import org.springframework.boot.context.properties.EnableConfigurationProperties;
  import org.springframework.context.annotation.Bean;
  
  /**
   * @author Lumian
   */
  @AutoConfiguration
  @ConditionalOnClass(ChatModel.class)
  @ConditionalOnProperty(prefix = ArmsCommonProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true")
  @EnableConfigurationProperties(ArmsCommonProperties.class)
  public class ArmsAutoConfiguration {
  
  	@Bean
  	@ConditionalOnProperty(prefix = ArmsCommonProperties.CONFIG_PREFIX, name = "tool.enabled", havingValue = "true")
  	ToolCallingManager toolCallingManager(ToolCallbackResolver toolCallbackResolver,
  			ToolExecutionExceptionProcessor toolExecutionExceptionProcessor,
  			ObjectProvider<ObservationRegistry> observationRegistry) {
  		return ObservableToolCallingManager.builder()
  			.observationRegistry(observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP))
  			.toolCallbackResolver(toolCallbackResolver)
  			.toolExecutionExceptionProcessor(toolExecutionExceptionProcessor)
  			.build();
  	}
  
  	@Bean
  	ChatClientObservationConvention chatClientObservationConvention() {
  		return new PromptMetadataAwareChatClientObservationConvention();
  	}
  
  	@Bean
  	ChatModelObservationConvention chatModelObservationConvention() {
  		return new PromptMetadataAwareChatModelObservationConvention();
  	}
  
  	@Bean
  	@ConditionalOnMissingBean(value = { ChatModelInputObservationHandler.class },
  			name = { "chatModelInputObservationHandler" })
  	@ConditionalOnProperty(prefix = ArmsCommonProperties.CONFIG_PREFIX, name = "model.capture-input",
  			havingValue = "true")
  	ChatModelInputObservationHandler armsChatModelInputObservationHandler(ArmsCommonProperties properties) {
  		return new ChatModelInputObservationHandler(properties.getModel().getMessageMode());
  	}
  
  	@Bean
  	@ConditionalOnMissingBean(value = { ChatModelOutputObservationHandler.class },
  			name = { "chatModelOutputObservationHandler" })
  	@ConditionalOnProperty(prefix = ArmsCommonProperties.CONFIG_PREFIX, name = "model.capture-output",
  			havingValue = "true")
  	ChatModelOutputObservationHandler armsChatModelOutputObservationHandler(ArmsCommonProperties properties) {
  		return new ChatModelOutputObservationHandler(properties.getModel().getMessageMode());
  	}
  
  }
  