-
Notifications
You must be signed in to change notification settings - Fork 57
[Fix #1087] A2A implementation #1469
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
Open
fjtirado
wants to merge
1
commit into
serverlessworkflow:main
Choose a base branch
from
fjtirado:Fix_#1087
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,220
−6
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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,30 @@ | ||
| <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>io.serverlessworkflow</groupId> | ||
| <artifactId>serverlessworkflow-impl</artifactId> | ||
| <version>8.0.0-SNAPSHOT</version> | ||
| </parent> | ||
| <artifactId>serverlessworkflow-impl-a2a</artifactId> | ||
| <name>Serverless Workflow :: Impl :: A2A</name> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.serverlessworkflow</groupId> | ||
| <artifactId>serverlessworkflow-impl-core</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.a2aproject.sdk</groupId> | ||
| <artifactId>a2a-java-sdk-client</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-params</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
37 changes: 37 additions & 0 deletions
37
impl/a2a/src/main/java/io/serverlessworkflow/impl/executors/a2a/A2AExceptionHandler.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,37 @@ | ||
| /* | ||
| * Copyright 2020-Present The Serverless Workflow Specification 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 | ||
| * | ||
| * 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. | ||
| */ | ||
| package io.serverlessworkflow.impl.executors.a2a; | ||
|
|
||
| import io.serverlessworkflow.impl.WorkflowModel; | ||
| import io.serverlessworkflow.impl.WorkflowPosition; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.function.Consumer; | ||
|
|
||
| class A2AExceptionHandler implements Consumer<Throwable> { | ||
|
|
||
| private final CompletableFuture<WorkflowModel> future; | ||
| private final WorkflowPosition position; | ||
|
|
||
| A2AExceptionHandler(CompletableFuture<WorkflowModel> future, WorkflowPosition position) { | ||
| this.future = future; | ||
| this.position = position; | ||
| } | ||
|
|
||
| @Override | ||
| public void accept(Throwable ex) { | ||
| future.completeExceptionally(A2AUtils.workflowException(position, ex)); | ||
| } | ||
| } |
91 changes: 91 additions & 0 deletions
91
impl/a2a/src/main/java/io/serverlessworkflow/impl/executors/a2a/A2AExecutor.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,91 @@ | ||
| /* | ||
| * Copyright 2020-Present The Serverless Workflow Specification 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 | ||
| * | ||
| * 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. | ||
| */ | ||
| package io.serverlessworkflow.impl.executors.a2a; | ||
|
|
||
| import io.serverlessworkflow.impl.TaskContext; | ||
| import io.serverlessworkflow.impl.WorkflowContext; | ||
| import io.serverlessworkflow.impl.WorkflowModel; | ||
| import io.serverlessworkflow.impl.WorkflowValueResolver; | ||
| import io.serverlessworkflow.impl.executors.CallableTask; | ||
| import java.net.URI; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import org.a2aproject.sdk.client.Client; | ||
| import org.a2aproject.sdk.client.config.ClientConfig; | ||
| import org.a2aproject.sdk.client.http.A2ACardResolver; | ||
| import org.a2aproject.sdk.client.transport.jsonrpc.JSONRPCTransport; | ||
| import org.a2aproject.sdk.client.transport.jsonrpc.JSONRPCTransportConfig; | ||
| import org.a2aproject.sdk.spec.A2AClientException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| class A2AExecutor implements CallableTask { | ||
|
|
||
| private final WorkflowValueResolver<URI> uriSupplier; | ||
| private final A2ARequestDispatcher dispatcher; | ||
| private final Optional<WorkflowValueResolver<Map<String, Object>>> mapResolver; | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(A2AExecutor.class); | ||
|
|
||
| public A2AExecutor( | ||
| WorkflowValueResolver<URI> uriSupplier, | ||
| A2ARequestDispatcher dispatcher, | ||
| Optional<WorkflowValueResolver<Map<String, Object>>> mapResolver) { | ||
| this.uriSupplier = uriSupplier; | ||
| this.dispatcher = dispatcher; | ||
| this.mapResolver = mapResolver; | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<WorkflowModel> apply( | ||
| WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) { | ||
| URI uri = uriSupplier.apply(workflowContext, taskContext, input); | ||
|
|
||
| return CompletableFuture.supplyAsync( | ||
| () -> { | ||
| try { | ||
| return A2ACardResolver.builder() | ||
| .baseUrl(uri.resolve("/").toString()) | ||
| .agentCardPath(uri.getPath()) | ||
| .build() | ||
| .getAgentCard(); | ||
| } catch (A2AClientException ex) { | ||
| throw A2AUtils.workflowException(taskContext.position(), ex); | ||
| } | ||
| }, | ||
| workflowContext.definition().application().executorService()) | ||
| .thenCompose( | ||
| agentCard -> { | ||
| logger.debug("Agent card is {}", agentCard); | ||
| try { | ||
| return dispatcher.apply( | ||
| agentCard, | ||
| Client.builder(agentCard) | ||
| .clientConfig(new ClientConfig.Builder().build()) | ||
| .withTransport(JSONRPCTransport.class, new JSONRPCTransportConfig()) | ||
| .build(), | ||
| mapResolver | ||
| .map(m -> m.apply(workflowContext, taskContext, input)) | ||
| .orElse(Map.of()), | ||
| workflowContext, | ||
| taskContext); | ||
| } catch (A2AClientException ex) { | ||
| throw A2AUtils.workflowException(taskContext.position(), ex); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
93 changes: 93 additions & 0 deletions
93
impl/a2a/src/main/java/io/serverlessworkflow/impl/executors/a2a/A2AExecutorBuilder.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,93 @@ | ||
| /* | ||
| * Copyright 2020-Present The Serverless Workflow Specification 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 | ||
| * | ||
| * 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. | ||
| */ | ||
| package io.serverlessworkflow.impl.executors.a2a; | ||
|
|
||
| import io.serverlessworkflow.api.types.A2AArguments; | ||
| import io.serverlessworkflow.api.types.CallA2A; | ||
| import io.serverlessworkflow.api.types.Parameters; | ||
| import io.serverlessworkflow.api.types.TaskBase; | ||
| import io.serverlessworkflow.api.types.WithA2AParameters; | ||
| import io.serverlessworkflow.impl.WorkflowDefinition; | ||
| import io.serverlessworkflow.impl.WorkflowMutablePosition; | ||
| import io.serverlessworkflow.impl.WorkflowUtils; | ||
| import io.serverlessworkflow.impl.WorkflowValueResolver; | ||
| import io.serverlessworkflow.impl.executors.CallableTaskBuilder; | ||
| import io.serverlessworkflow.impl.executors.CallableTaskFactory; | ||
| import java.net.URI; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| public class A2AExecutorBuilder implements CallableTaskBuilder<CallA2A> { | ||
|
|
||
| @Override | ||
| public boolean accept(Class<? extends TaskBase> clazz) { | ||
| return CallA2A.class.equals(clazz); | ||
| } | ||
|
|
||
| @Override | ||
| public CallableTaskFactory init( | ||
| CallA2A task, WorkflowDefinition definition, WorkflowMutablePosition position) { | ||
| A2AArguments args = task.getWith(); | ||
|
|
||
| WorkflowValueResolver<URI> uriSupplier; | ||
| if (args.getServer() != null) { | ||
| uriSupplier = definition.resourceLoader().uriSupplier(args.getServer()); | ||
| } else if (args.getAgentCard() != null) { | ||
| uriSupplier = definition.resourceLoader().uriSupplier(args.getAgentCard().getEndpoint()); | ||
| } else { | ||
| throw new IllegalArgumentException("Neither server nor agent card is set for task: " + task); | ||
| } | ||
|
|
||
| A2ARequestDispatcher dispatcher = | ||
| switch (args.getMethod()) { | ||
| case MESSAGE_SEND -> | ||
| new MessageDispatcher( | ||
| (workflowContext, taskContext, completableFuture) -> | ||
| new MessageSendConsumer(workflowContext.definition(), completableFuture)); | ||
| case MESSAGE_STREAM -> | ||
| new MessageDispatcher( | ||
| (workflowContext, taskContext, completableFuture) -> | ||
| new MessageStreamConsumer( | ||
| workflowContext.definition(), completableFuture, taskContext.position())); | ||
| case TASKS_LIST -> new ListTaskDispatcher(); | ||
| case TASKS_GET -> new GetTaskDispatcher(); | ||
| case TASKS_CANCEL -> new CancelTaskDispatcher(); | ||
| // TODO handle missing cases | ||
| case AGENT_GET_AUTHENTICATED_EXTENDED_CARD, | ||
| TASKS_PUSH_NOTIFICATION_CONFIG_DELETE, | ||
| TASKS_PUSH_NOTIFICATION_CONFIG_GET, | ||
| TASKS_PUSH_NOTIFICATION_CONFIG_LIST, | ||
| TASKS_PUSH_NOTIFICATION_CONFIG_SET, | ||
| TASKS_RESUBSCRIBE -> | ||
|
Comment on lines
+69
to
+74
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since these operations are not really frequent, they will be adressed with a separate issue #1485 |
||
| throw new UnsupportedOperationException("Unimplemented case: " + args.getMethod()); | ||
| }; | ||
|
|
||
| Parameters parameters = args.getParameters(); | ||
| Optional<WorkflowValueResolver<Map<String, Object>>> mapResolver; | ||
| if (parameters == null) { | ||
| mapResolver = Optional.empty(); | ||
| } else { | ||
| WithA2AParameters a2aParameters = parameters.getWithA2AParameters(); | ||
| mapResolver = | ||
| Optional.of( | ||
| WorkflowUtils.buildMapResolver( | ||
| definition.application(), | ||
| parameters.getString(), | ||
| a2aParameters != null ? a2aParameters.getAdditionalProperties() : null)); | ||
| } | ||
| return () -> new A2AExecutor(uriSupplier, dispatcher, mapResolver); | ||
| } | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
impl/a2a/src/main/java/io/serverlessworkflow/impl/executors/a2a/A2ARequestDispatcher.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,34 @@ | ||
| /* | ||
| * Copyright 2020-Present The Serverless Workflow Specification 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 | ||
| * | ||
| * 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. | ||
| */ | ||
| package io.serverlessworkflow.impl.executors.a2a; | ||
|
|
||
| import io.serverlessworkflow.impl.TaskContext; | ||
| import io.serverlessworkflow.impl.WorkflowContext; | ||
| import io.serverlessworkflow.impl.WorkflowModel; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import org.a2aproject.sdk.client.Client; | ||
| import org.a2aproject.sdk.spec.AgentCard; | ||
|
|
||
| @FunctionalInterface | ||
| interface A2ARequestDispatcher { | ||
| CompletableFuture<WorkflowModel> apply( | ||
| AgentCard agentCard, | ||
| Client client, | ||
| Map<String, Object> parameters, | ||
| WorkflowContext workflowContext, | ||
| TaskContext taskContext); | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do we replace this client with a framework's infra?