-
Notifications
You must be signed in to change notification settings - Fork 105
feat: support springboot integrated and example #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
0e23274
feature: support sdk spring integrate
tomsun28 eafe84d
Merge branch 'refs/heads/main' into spring-inte
tomsun28 5c30569
update
tomsun28 967a755
update
tomsun28 767f9dd
update
tomsun28 9375c15
update
tomsun28 ed2e61c
update
tomsun28 0467e70
update
tomsun28 0f84556
update
tomsun28 f208f81
Merge branch 'main' into spring-inte
tomsun28 c611582
update
tomsun28 93701cb
update
tomsun28 59401cb
update
tomsun28 ff4e68f
update
tomsun28 271ce4c
update
tomsun28 23aa356
Merge branch 'main' into spring-inte
tomsun28 286db5d
Merge branch 'main' into spring-inte
tomsun28 1bc3228
Merge branch 'main' into spring-inte
tomsun28 ac6c127
Merge branch 'main' into spring-inte
tomsun28 b8b5fd2
Merge branch 'main' into spring-inte
tomsun28 c686087
update
tomsun28 a4061fa
update
tomsun28 77737c9
update
tomsun28 c7067ca
update
tomsun28 e4ad3bf
Merge branch 'main' into spring-inte
tomsun28 fc6ee78
Merge branch 'main' into spring-inte
tomsun28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>io.github.a2asdk</groupId> | ||
| <artifactId>a2a-java-sdk-parent</artifactId> | ||
| <version>0.2.3.Beta2-SNAPSHOT</version> | ||
| <relativePath>../../pom.xml</relativePath> | ||
| </parent> | ||
|
|
||
| <artifactId>spring-helloworld</artifactId> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <spring-boot.version>3.4.5</spring-boot.version> | ||
| </properties> | ||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-dependencies</artifactId> | ||
| <version>${spring-boot.version}</version> | ||
| <type>pom</type> | ||
| <scope>import</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.github.a2asdk</groupId> | ||
| <artifactId>a2a-java-sdk-server-spring</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-web</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-webflux</artifactId> | ||
| </dependency> | ||
|
|
||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-actuator</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-test</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-maven-plugin</artifactId> | ||
| <version>${spring-boot.version}</version> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> | ||
15 changes: 15 additions & 0 deletions
15
examples/spring-helloworld/src/main/java/io/a2a/sdk/apps/spring/demo/SpringHelloworld.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package io.a2a.sdk.apps.spring.demo; | ||
|
|
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| @SpringBootApplication | ||
| public class SpringHelloworld { | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication.run(SpringHelloworld.class, args); | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
...s/spring-helloworld/src/main/java/io/a2a/sdk/apps/spring/demo/config/A2AServerConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package io.a2a.sdk.apps.spring.demo.config; | ||
|
|
||
| import io.a2a.A2A; | ||
| import io.a2a.server.agentexecution.AgentExecutor; | ||
| import io.a2a.server.agentexecution.RequestContext; | ||
| import io.a2a.server.events.EventQueue; | ||
| import io.a2a.server.tasks.TaskUpdater; | ||
| import io.a2a.spec.JSONRPCError; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| /** | ||
| * A2A server config | ||
| */ | ||
| @Configuration | ||
| public class A2AServerConfig { | ||
|
|
||
| @Bean | ||
| public AgentExecutor agentExecutor() { | ||
| return new AgentExecutor() { | ||
| @Override | ||
| public void execute(RequestContext context, EventQueue eventQueue) throws JSONRPCError { | ||
| eventQueue.enqueueEvent(context.getMessage() != null ? context.getMessage() : context.getTask()); | ||
| eventQueue.enqueueEvent(A2A.toAgentMessage("Hello World")); | ||
| } | ||
|
|
||
| @Override | ||
| public void cancel(RequestContext context, EventQueue eventQueue) throws JSONRPCError { | ||
| TaskUpdater taskUpdater = new TaskUpdater(context, eventQueue); | ||
| taskUpdater.cancel(); | ||
| }; | ||
tomsun28 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
| } | ||
222 changes: 222 additions & 0 deletions
222
...ng-helloworld/src/main/java/io/a2a/sdk/apps/spring/demo/endpoint/A2AServerController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| package io.a2a.sdk.apps.spring.demo.endpoint; | ||
|
|
||
| import jakarta.annotation.Resource; | ||
| import java.util.concurrent.Flow; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.http.codec.ServerSentEvent; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParseException; | ||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
|
|
||
| import io.a2a.server.ExtendedAgentCard; | ||
| import io.a2a.server.requesthandlers.JSONRPCHandler; | ||
| import io.a2a.spec.AgentCard; | ||
| import io.a2a.spec.CancelTaskRequest; | ||
| import io.a2a.spec.GetTaskPushNotificationConfigRequest; | ||
| import io.a2a.spec.GetTaskRequest; | ||
| import io.a2a.spec.IdJsonMappingException; | ||
| import io.a2a.spec.InvalidParamsError; | ||
| import io.a2a.spec.InvalidParamsJsonMappingException; | ||
| import io.a2a.spec.InvalidRequestError; | ||
| import io.a2a.spec.JSONErrorResponse; | ||
| import io.a2a.spec.JSONParseError; | ||
| import io.a2a.spec.JSONRPCError; | ||
| import io.a2a.spec.JSONRPCErrorResponse; | ||
| import io.a2a.spec.JSONRPCRequest; | ||
| import io.a2a.spec.JSONRPCResponse; | ||
| import io.a2a.spec.MethodNotFoundError; | ||
| import io.a2a.spec.MethodNotFoundJsonMappingException; | ||
| import io.a2a.spec.NonStreamingJSONRPCRequest; | ||
| import io.a2a.spec.SendMessageRequest; | ||
| import io.a2a.spec.SendStreamingMessageRequest; | ||
| import io.a2a.spec.SetTaskPushNotificationConfigRequest; | ||
| import io.a2a.spec.StreamingJSONRPCRequest; | ||
| import io.a2a.spec.TaskResubscriptionRequest; | ||
| import io.a2a.spec.UnsupportedOperationError; | ||
| import io.a2a.util.Utils; | ||
| import reactor.core.publisher.Flux; | ||
|
|
||
| /** | ||
| * Spring Boot REST controller for A2A (Agent2Agent) protocol endpoints. | ||
| * Provides endpoints for JSON-RPC communication and agent card retrieval. | ||
| */ | ||
| @RestController | ||
| @RequestMapping("/") | ||
| public class A2AServerController { | ||
|
|
||
| @Resource | ||
| private JSONRPCHandler jsonRpcHandler; | ||
|
|
||
| @Autowired(required = false) | ||
| @ExtendedAgentCard | ||
| private AgentCard extendedAgentCard; | ||
|
|
||
| /** | ||
| * Handles incoming POST requests to the main A2A endpoint. | ||
| * Dispatches the request to the appropriate JSON-RPC handler method. | ||
| * | ||
| * @param requestBody the JSON-RPC request body as string | ||
| * @return the JSON-RPC response | ||
| */ | ||
| @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) | ||
| public ResponseEntity<JSONRPCResponse<?>> handleNonStreamingRequests(@RequestBody String requestBody) { | ||
| try { | ||
| NonStreamingJSONRPCRequest<?> request = Utils.OBJECT_MAPPER.readValue(requestBody, NonStreamingJSONRPCRequest.class); | ||
| JSONRPCResponse<?> response = processNonStreamingRequest(request); | ||
| return ResponseEntity.ok(response); | ||
| } catch (JsonProcessingException e) { | ||
| JSONRPCErrorResponse error = handleError(e); | ||
| return ResponseEntity.ok(error); | ||
| } catch (Throwable t) { | ||
| JSONRPCErrorResponse error = new JSONRPCErrorResponse(new io.a2a.spec.InternalError(t.getMessage())); | ||
| return ResponseEntity.ok(error); | ||
| } | ||
tomsun28 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Handles incoming POST requests for streaming operations using Server-Sent Events (SSE). | ||
| * Dispatches the request to the appropriate JSON-RPC handler method. | ||
| * | ||
| * @param requestBody the JSON-RPC request body as string | ||
| * @return a Flux of ServerSentEvent containing the streaming responses | ||
| */ | ||
| @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.TEXT_EVENT_STREAM_VALUE) | ||
| public Flux<ServerSentEvent<JSONRPCResponse<?>>> handleStreamingRequests(@RequestBody String requestBody) { | ||
| try { | ||
| StreamingJSONRPCRequest<?> request = Utils.OBJECT_MAPPER.readValue(requestBody, StreamingJSONRPCRequest.class); | ||
| return processStreamingRequest(request); | ||
| } catch (JsonProcessingException e) { | ||
| JSONRPCErrorResponse error = handleError(e); | ||
| return Flux.just(ServerSentEvent.<JSONRPCResponse<?>>builder() | ||
| .data(error) | ||
| .build()); | ||
| } catch (Throwable t) { | ||
| JSONRPCErrorResponse error = new JSONRPCErrorResponse(new io.a2a.spec.InternalError(t.getMessage())); | ||
| return Flux.just(ServerSentEvent.<JSONRPCResponse<?>>builder() | ||
| .data(error) | ||
| .build()); | ||
| } | ||
tomsun28 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Handles incoming GET requests to the agent card endpoint. | ||
| * Returns the agent card in JSON format. | ||
| * | ||
| * @return the agent card | ||
| */ | ||
| @GetMapping(path = "/.well-known/agent.json", produces = MediaType.APPLICATION_JSON_VALUE) | ||
| public AgentCard getAgentCard() { | ||
| return jsonRpcHandler.getAgentCard(); | ||
| } | ||
|
|
||
| /** | ||
| * Handles incoming GET requests to the authenticated extended agent card endpoint. | ||
| * Returns the extended agent card in JSON format. | ||
| * | ||
| * @return the authenticated extended agent card | ||
| */ | ||
| @GetMapping(path = "/agent/authenticatedExtendedCard", produces = MediaType.APPLICATION_JSON_VALUE) | ||
| public ResponseEntity<?> getAuthenticatedExtendedAgentCard() { | ||
| // TODO: Add authentication for this endpoint | ||
| // https://github.com/a2aproject/a2a-java/issues/77 | ||
| if (!jsonRpcHandler.getAgentCard().supportsAuthenticatedExtendedCard()) { | ||
| JSONErrorResponse errorResponse = new JSONErrorResponse("Extended agent card not supported or not enabled."); | ||
| return ResponseEntity.status(404).body(errorResponse); | ||
| } | ||
| if (extendedAgentCard == null) { | ||
| JSONErrorResponse errorResponse = new JSONErrorResponse("Authenticated extended agent card is supported but not configured on the server."); | ||
| return ResponseEntity.status(404).body(errorResponse); | ||
| } | ||
| return ResponseEntity.ok(extendedAgentCard); | ||
| } | ||
|
|
||
| private JSONRPCResponse<?> processNonStreamingRequest(NonStreamingJSONRPCRequest<?> request) { | ||
| if (request instanceof GetTaskRequest) { | ||
| return jsonRpcHandler.onGetTask((GetTaskRequest) request); | ||
| } else if (request instanceof CancelTaskRequest) { | ||
| return jsonRpcHandler.onCancelTask((CancelTaskRequest) request); | ||
| } else if (request instanceof SetTaskPushNotificationConfigRequest) { | ||
| return jsonRpcHandler.setPushNotification((SetTaskPushNotificationConfigRequest) request); | ||
| } else if (request instanceof GetTaskPushNotificationConfigRequest) { | ||
| return jsonRpcHandler.getPushNotification((GetTaskPushNotificationConfigRequest) request); | ||
| } else if (request instanceof SendMessageRequest) { | ||
| return jsonRpcHandler.onMessageSend((SendMessageRequest) request); | ||
| } else { | ||
| return generateErrorResponse(request, new UnsupportedOperationError()); | ||
| } | ||
| } | ||
tomsun28 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private Flux<ServerSentEvent<JSONRPCResponse<?>>> processStreamingRequest(StreamingJSONRPCRequest<?> request) { | ||
| Flow.Publisher<? extends JSONRPCResponse<?>> publisher; | ||
| if (request instanceof SendStreamingMessageRequest) { | ||
| publisher = jsonRpcHandler.onMessageSendStream((SendStreamingMessageRequest) request); | ||
| } else if (request instanceof TaskResubscriptionRequest) { | ||
| publisher = jsonRpcHandler.onResubscribeToTask((TaskResubscriptionRequest) request); | ||
| } else { | ||
| return Flux.just(ServerSentEvent.<JSONRPCResponse<?>>builder() | ||
| .data(generateErrorResponse(request, new UnsupportedOperationError())) | ||
| .build()); | ||
| } | ||
|
|
||
| return Flux.create(sink -> { | ||
| publisher.subscribe(new Flow.Subscriber<JSONRPCResponse<?>>() { | ||
| @Override | ||
| public void onSubscribe(Flow.Subscription subscription) { | ||
| subscription.request(Long.MAX_VALUE); | ||
| } | ||
|
|
||
| @Override | ||
| public void onNext(JSONRPCResponse<?> item) { | ||
| sink.next(ServerSentEvent.<JSONRPCResponse<?>>builder() | ||
| .data(item) | ||
| .build()); | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(Throwable throwable) { | ||
| sink.error(throwable); | ||
| } | ||
|
|
||
| @Override | ||
| public void onComplete() { | ||
| sink.complete(); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| private JSONRPCErrorResponse handleError(JsonProcessingException exception) { | ||
| Object id = null; | ||
| JSONRPCError jsonRpcError = null; | ||
| if (exception.getCause() instanceof JsonParseException) { | ||
| jsonRpcError = new JSONParseError(); | ||
| } else if (exception instanceof com.fasterxml.jackson.core.io.JsonEOFException) { | ||
| jsonRpcError = new JSONParseError(exception.getMessage()); | ||
| } else if (exception instanceof MethodNotFoundJsonMappingException err) { | ||
| id = err.getId(); | ||
| jsonRpcError = new MethodNotFoundError(); | ||
| } else if (exception instanceof InvalidParamsJsonMappingException err) { | ||
| id = err.getId(); | ||
| jsonRpcError = new InvalidParamsError(); | ||
| } else if (exception instanceof IdJsonMappingException err) { | ||
| id = err.getId(); | ||
| jsonRpcError = new InvalidRequestError(); | ||
| } else { | ||
| jsonRpcError = new InvalidRequestError(); | ||
| } | ||
| return new JSONRPCErrorResponse(id, jsonRpcError); | ||
| } | ||
|
|
||
| private JSONRPCResponse<?> generateErrorResponse(JSONRPCRequest<?> request, JSONRPCError error) { | ||
| return new JSONRPCErrorResponse(request.getId(), error); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.