|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.impl.executors.a2a; |
| 17 | + |
| 18 | +import io.serverlessworkflow.api.types.A2AArguments; |
| 19 | +import io.serverlessworkflow.api.types.CallA2A; |
| 20 | +import io.serverlessworkflow.api.types.TaskBase; |
| 21 | +import io.serverlessworkflow.impl.TaskContext; |
| 22 | +import io.serverlessworkflow.impl.WorkflowContext; |
| 23 | +import io.serverlessworkflow.impl.WorkflowDefinition; |
| 24 | +import io.serverlessworkflow.impl.WorkflowMutablePosition; |
| 25 | +import io.serverlessworkflow.impl.WorkflowUtils; |
| 26 | +import io.serverlessworkflow.impl.WorkflowValueResolver; |
| 27 | +import io.serverlessworkflow.impl.executors.CallableTaskBuilder; |
| 28 | +import io.serverlessworkflow.impl.executors.CallableTaskFactory; |
| 29 | +import java.net.URI; |
| 30 | + |
| 31 | +public class A2AExecutorBuilder implements CallableTaskBuilder<CallA2A> { |
| 32 | + |
| 33 | + @Override |
| 34 | + public boolean accept(Class<? extends TaskBase> clazz) { |
| 35 | + return CallA2A.class.equals(clazz); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public CallableTaskFactory init( |
| 40 | + CallA2A task, WorkflowDefinition definition, WorkflowMutablePosition position) { |
| 41 | + A2AArguments args = task.getWith(); |
| 42 | + |
| 43 | + WorkflowValueResolver<URI> uriSupplier; |
| 44 | + if (args.getServer() != null) { |
| 45 | + uriSupplier = definition.resourceLoader().uriSupplier(args.getServer()); |
| 46 | + } else if (args.getAgentCard() != null) { |
| 47 | + uriSupplier = definition.resourceLoader().uriSupplier(args.getAgentCard().getEndpoint()); |
| 48 | + } else { |
| 49 | + throw new IllegalArgumentException("Neither server or agent card is set for task" + task); |
| 50 | + } |
| 51 | + |
| 52 | + A2ARequestDispatcher dispatcher = |
| 53 | + switch (args.getMethod()) { |
| 54 | + case MESSAGE_SEND -> |
| 55 | + new MessageDispatcher( |
| 56 | + new MessageConsumerFactory() { |
| 57 | + @Override |
| 58 | + protected MessageConsumer buildConsumer( |
| 59 | + WorkflowContext workflowContext, TaskContext taskContext) { |
| 60 | + return new MessageSendConsumer(workflowContext.definition()); |
| 61 | + } |
| 62 | + }); |
| 63 | + case MESSAGE_STREAM -> |
| 64 | + new MessageDispatcher( |
| 65 | + new MessageConsumerFactory() { |
| 66 | + |
| 67 | + @Override |
| 68 | + protected MessageConsumer buildConsumer( |
| 69 | + WorkflowContext workflowContext, TaskContext taskContext) { |
| 70 | + return new MessageStreamConsumer( |
| 71 | + workflowContext.definition(), taskContext.position()); |
| 72 | + } |
| 73 | + }); |
| 74 | + case TASKS_LIST -> new ListTaskParamsDispatcher(); |
| 75 | + case TASKS_GET -> new GetTaskParamsDispatcher(); |
| 76 | + case TASKS_CANCEL -> new CancelTaskParamsDispatcher(); |
| 77 | + // TODO handle missing cases |
| 78 | + case AGENT_GET_AUTHENTICATED_EXTENDED_CARD, |
| 79 | + TASKS_PUSH_NOTIFICATION_CONFIG_DELETE, |
| 80 | + TASKS_PUSH_NOTIFICATION_CONFIG_GET, |
| 81 | + TASKS_PUSH_NOTIFICATION_CONFIG_LIST, |
| 82 | + TASKS_PUSH_NOTIFICATION_CONFIG_SET, |
| 83 | + TASKS_RESUBSCRIBE -> |
| 84 | + throw new UnsupportedOperationException("Unimplemented case: " + args.getMethod()); |
| 85 | + }; |
| 86 | + |
| 87 | + return () -> |
| 88 | + new A2AExecutor( |
| 89 | + uriSupplier, |
| 90 | + dispatcher, |
| 91 | + WorkflowUtils.buildMapResolver( |
| 92 | + definition.application(), |
| 93 | + args.getParameters().getString(), |
| 94 | + args.getParameters().getWithA2AParameters().getAdditionalProperties())); |
| 95 | + } |
| 96 | +} |
0 commit comments