|
| 1 | +package org.a2aproject.sdk.sut; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import jakarta.enterprise.context.ApplicationScoped; |
| 7 | +import jakarta.enterprise.inject.Produces; |
| 8 | + |
| 9 | +import org.a2aproject.sdk.A2A; |
| 10 | +import org.a2aproject.sdk.server.agentexecution.AgentExecutor; |
| 11 | +import org.a2aproject.sdk.server.agentexecution.RequestContext; |
| 12 | +import org.a2aproject.sdk.server.tasks.AgentEmitter; |
| 13 | +import org.a2aproject.sdk.spec.A2AError; |
| 14 | +import org.a2aproject.sdk.spec.DataPart; |
| 15 | +import org.a2aproject.sdk.spec.FileWithBytes; |
| 16 | +import org.a2aproject.sdk.spec.FileWithUri; |
| 17 | +import org.a2aproject.sdk.spec.FilePart; |
| 18 | +import org.a2aproject.sdk.spec.TaskNotCancelableError; |
| 19 | +import org.a2aproject.sdk.spec.TextPart; |
| 20 | + |
| 21 | +/** |
| 22 | + * CDI producer for the TCK agent executor. |
| 23 | + * |
| 24 | + * <p>Generated from Gherkin scenarios — do not edit by hand. |
| 25 | + */ |
| 26 | +@ApplicationScoped |
| 27 | +public class TckAgentExecutorProducer { |
| 28 | + |
| 29 | + @Produces |
| 30 | + public AgentExecutor agentExecutor() { |
| 31 | + return new AgentExecutor() { |
| 32 | + @Override |
| 33 | + public void execute(RequestContext context, AgentEmitter emitter) throws A2AError { |
| 34 | + String messageId = context.getMessage().messageId(); |
| 35 | + |
| 36 | + if (messageId.startsWith("tck-stream-artifact-chunked")) { |
| 37 | + emitter.startWork(); |
| 38 | + emitter.addArtifact(List.of(new TextPart("chunk-1 ")), null, null, null, true, false); |
| 39 | + emitter.addArtifact(List.of(new TextPart("chunk-2")), null, null, null, true, true); |
| 40 | + emitter.complete(); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (messageId.startsWith("test-resubscribe-message-id")) { |
| 45 | + emitter.startWork(); |
| 46 | + try { Thread.sleep(4000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } |
| 47 | + emitter.complete(); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (messageId.startsWith("tck-stream-artifact-text")) { |
| 52 | + emitter.startWork(); |
| 53 | + emitter.addArtifact(List.of(new TextPart("Streamed text content")), null, null, null); |
| 54 | + emitter.complete(); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (messageId.startsWith("tck-stream-artifact-file")) { |
| 59 | + emitter.startWork(); |
| 60 | + emitter.addArtifact(List.of(new FilePart(new FileWithBytes("text/plain", "output.txt", "dGNr"))), null, null, null); |
| 61 | + emitter.complete(); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (messageId.startsWith("tck-stream-ordering-001")) { |
| 66 | + emitter.startWork(); |
| 67 | + emitter.addArtifact(List.of(new TextPart("Ordered output")), null, null, null); |
| 68 | + emitter.complete(); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + if (messageId.startsWith("tck-artifact-file-url")) { |
| 73 | + emitter.addArtifact(List.of(new FilePart(new FileWithUri("text/plain", "output.txt", "https://example.com/output.txt"))), null, null, null); |
| 74 | + emitter.complete(); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (messageId.startsWith("tck-message-response")) { |
| 79 | + emitter.sendMessage(List.of(new TextPart("Direct message response"))); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (messageId.startsWith("tck-input-required")) { |
| 84 | + emitter.requiresInput(); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if (messageId.startsWith("tck-complete-task")) { |
| 89 | + emitter.complete(A2A.toAgentMessage("Hello from TCK")); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + if (messageId.startsWith("tck-artifact-text")) { |
| 94 | + emitter.addArtifact(List.of(new TextPart("Generated text content")), null, null, null); |
| 95 | + emitter.complete(); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if (messageId.startsWith("tck-artifact-file")) { |
| 100 | + emitter.addArtifact(List.of(new FilePart(new FileWithBytes("text/plain", "output.txt", "dGNr"))), null, null, null); |
| 101 | + emitter.complete(); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + if (messageId.startsWith("tck-artifact-data")) { |
| 106 | + emitter.addArtifact(List.of(DataPart.fromJson("{\"key\": \"value\", \"count\": 42}")), null, null, null); |
| 107 | + emitter.complete(); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if (messageId.startsWith("tck-reject-task")) { |
| 112 | + throw new A2AError(-1, "rejected", null); |
| 113 | + } |
| 114 | + |
| 115 | + if (messageId.startsWith("tck-stream-001")) { |
| 116 | + emitter.startWork(); |
| 117 | + emitter.addArtifact(List.of(new TextPart("Stream hello from TCK")), null, null, null); |
| 118 | + emitter.complete(); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + if (messageId.startsWith("tck-stream-002")) { |
| 123 | + emitter.complete(); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + if (messageId.startsWith("tck-stream-003")) { |
| 128 | + emitter.startWork(); |
| 129 | + emitter.addArtifact(List.of(new TextPart("Stream task lifecycle")), null, null, null); |
| 130 | + emitter.complete(); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + // Default: complete the task with an echo response |
| 135 | + emitter.complete(A2A.toAgentMessage("Unhandled messageId prefix: " + messageId)); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void cancel(RequestContext context, AgentEmitter emitter) throws A2AError { |
| 140 | + emitter.cancel(); |
| 141 | + } |
| 142 | + }; |
| 143 | + } |
| 144 | +} |
0 commit comments