Commit dca12699 authored by Christos Tranoris's avatar Christos Tranoris
Browse files

add support to respond via activeMQ agents queue

parent c450151d
Loading
Loading
Loading
Loading
+67 −16
Original line number Diff line number Diff line
@@ -25,8 +25,31 @@
	<properties>
		<java.version>17</java.version>
		<spring-ai.version>1.1.0</spring-ai.version>
		<camel.version>4.0.0-RC2</camel.version>
	</properties>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.ai</groupId>
				<artifactId>spring-ai-bom</artifactId>
				<version>${spring-ai.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
			<!-- Camel BOM -->
			<dependency>
				<groupId>org.apache.camel.springboot</groupId>
				<artifactId>camel-spring-boot-dependencies</artifactId>
				<version>${camel.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		
		</dependencies>
	</dependencyManagement>
	
	
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
@@ -58,23 +81,51 @@
        	<version>0.0.4</version>
	    </dependency>
        
        
	</dependencies>
	<dependencyManagement>
		<dependencies>
        <!-- activeMQ -->
		<dependency>
				<groupId>org.springframework.ai</groupId>
				<artifactId>spring-ai-bom</artifactId>
				<version>${spring-ai.version}</version>
				<type>pom</type>
				<scope>import</scope>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-amqp</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.apache.qpid</groupId>
					<artifactId>proton-j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.messaginghub</groupId>
			<artifactId>pooled-jms</artifactId>
		</dependency>

			

		
		<!-- Camel -->
		<dependency>
			<groupId>org.apache.camel.springboot</groupId>
			<artifactId>camel-spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-activemq</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.apache.activemq</groupId>
					<artifactId>activemq-broker</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.camel.springboot</groupId>
			<artifactId>camel-service-starter</artifactId>
		</dependency>
	</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
+12 −0
Original line number Diff line number Diff line
package org.etsi.osl.mcp.backend;

import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
@@ -37,6 +38,17 @@ public class ChatController {
        return new Answer(htmlResponse);
    }


    public String simpleAsk( Map<String, Object> headers, String question) {
        var response = chatClient.prompt()
                .user( question )
                .call()
                .content();
        
        log.debug("Response from AI: {}", response);
        return response;
    }

    private String formatResponse(Question question, String answer) {
      return answer;
//        var prompt = """
+41 −0
Original line number Diff line number Diff line
/*-
 * ========================LICENSE_START=================================
 * org.etsi.osl.bugzilla
 * %%
 * Copyright (C) 2019 openslice.io
 * %%
 * 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
 * 
 *      http://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.
 * =========================LICENSE_END==================================
 */
package org.etsi.osl.mcp.backend.configuration;

import org.apache.camel.component.activemq.ActiveMQComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import jakarta.jms.ConnectionFactory;

/**
 * @author ctranoris
 *
 */
@Configuration
public class ActiveMQComponentConfig {

    @Bean(name = "activemq")
    public ActiveMQComponent createComponent(ConnectionFactory factory) {
        ActiveMQComponent activeMQComponent = new ActiveMQComponent();
        activeMQComponent.setConnectionFactory(factory);
        return activeMQComponent;
    }
}
+1 −1
Original line number Diff line number Diff line
package org.etsi.osl.mcp.backend;
package org.etsi.osl.mcp.backend.configuration;

import io.modelcontextprotocol.client.transport.customizer.McpSyncHttpClientRequestCustomizer;
import org.springaicommunity.mcp.security.client.sync.AuthenticationMcpTransportContextProvider;
+53 −0
Original line number Diff line number Diff line
package org.etsi.osl.mcp.backend.configuration;

import java.io.IOException;
import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.etsi.osl.mcp.backend.ChatController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;


@Configuration
@Component
public class PartnerRouteBuillder extends RouteBuilder {

  @Value("${spring.application.name}")
  private String compname;
  
  @Autowired
  private ChatController chatController;

  @Override
  public void configure() throws Exception {


    String EVENT_CHAT = "jms:queue:agents/"+compname ;

    
    from(EVENT_CHAT)
        .log(LoggingLevel.INFO, log, EVENT_CHAT + " message received!")
        .to("log:DEBUG?showBody=true&showHeaders=true")
        .bean( chatController, "simpleAsk( ${headers}, ${body} )")
        .convertBodyTo( String.class );   
  }

  static <T> T toJsonObj(String content, Class<T> valueType) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return mapper.readValue(content, valueType);
  }

  static String toJsonString(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return mapper.writeValueAsString(object);
  }


}
Loading