|
| 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.fluent.agentic; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | +import static org.mockito.ArgumentMatchers.eq; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import io.serverlessworkflow.api.types.Workflow; |
| 25 | +import io.serverlessworkflow.impl.WorkflowApplication; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.concurrent.ExecutionException; |
| 30 | +import java.util.concurrent.atomic.AtomicInteger; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +public class AgenticBeforeAfterInvokeTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + void testAgentBeforeAfter() throws ExecutionException, InterruptedException { |
| 37 | + Agents.ChatBot chatBot = mock(Agents.ChatBot.class); |
| 38 | + when(chatBot.chat(eq("A Great Story"))).thenReturn("Once upon a time..."); |
| 39 | + |
| 40 | + Workflow wf = |
| 41 | + AgentWorkflowBuilder.workflow() |
| 42 | + .tasks( |
| 43 | + t -> |
| 44 | + t.agent(chatBot) |
| 45 | + .inputFrom( |
| 46 | + agenticScope -> { |
| 47 | + assertTrue(agenticScope.state().containsKey("userInput")); |
| 48 | + assertEquals("A Great Story", agenticScope.readState("userInput")); |
| 49 | + agenticScope.writeState("testData", "someValue"); |
| 50 | + }) |
| 51 | + .outputAs( |
| 52 | + agenticScope -> { |
| 53 | + assertTrue(agenticScope.state().containsKey("userInput")); |
| 54 | + assertEquals("A Great Story", agenticScope.readState("userInput")); |
| 55 | + assertTrue(agenticScope.state().containsKey("testData")); |
| 56 | + assertEquals("someValue", agenticScope.readState("testData")); |
| 57 | + assertEquals("Once upon a time...", agenticScope.readState("input")); |
| 58 | + agenticScope.writeState("outputData", "outputValue"); |
| 59 | + })) |
| 60 | + .build(); |
| 61 | + |
| 62 | + Map<String, Object> in = new HashMap<>(); |
| 63 | + in.put("userInput", "A Great Story"); |
| 64 | + |
| 65 | + Map<String, Object> out; |
| 66 | + try (WorkflowApplication app = WorkflowApplication.builder().build()) { |
| 67 | + out = app.workflowDefinition(wf).instance(in).start().get().asMap().orElseThrow(); |
| 68 | + } |
| 69 | + |
| 70 | + assertTrue(out.containsKey("outputData")); |
| 71 | + assertEquals("outputValue", out.get("outputData")); |
| 72 | + assertTrue(out.containsKey("userInput")); |
| 73 | + assertEquals("A Great Story", out.get("userInput")); |
| 74 | + assertTrue(out.containsKey("testData")); |
| 75 | + assertEquals("someValue", out.get("testData")); |
| 76 | + assertTrue(out.containsKey("input")); |
| 77 | + assertEquals("Once upon a time...", out.get("input")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testSeqBeforeAfter() throws ExecutionException, InterruptedException { |
| 82 | + Agents.CreativeWriter creativeWriter = mock(Agents.CreativeWriter.class); |
| 83 | + when(creativeWriter.generateStory((eq("dragons and wizards")))) |
| 84 | + .thenReturn("A fantasy story..."); |
| 85 | + |
| 86 | + Agents.StyleEditor styleEditor = mock(Agents.StyleEditor.class); |
| 87 | + when(styleEditor.editStory(eq("A fantasy story..."), eq("young adults"))) |
| 88 | + .thenReturn("An engaging fantasy story for young adults..."); |
| 89 | + |
| 90 | + Agents.StyleScorer styleScorer = mock(Agents.StyleScorer.class); |
| 91 | + when(styleScorer.scoreStyle(eq("An engaging fantasy story for young adults..."), eq("funny"))) |
| 92 | + .thenReturn(8.0); |
| 93 | + |
| 94 | + Workflow wf = |
| 95 | + AgentWorkflowBuilder.workflow() |
| 96 | + .tasks( |
| 97 | + t -> |
| 98 | + t.sequence(creativeWriter, styleEditor, styleScorer) |
| 99 | + .inputFrom( |
| 100 | + agenticScope -> { |
| 101 | + if (agenticScope.readState("step") == null) { |
| 102 | + agenticScope.writeState("topic", "dragons and wizards"); |
| 103 | + agenticScope.writeState("step", "out_1"); |
| 104 | + } else if (agenticScope.readState("step").equals("in_2")) { |
| 105 | + agenticScope.writeState("story", agenticScope.readState("input")); |
| 106 | + agenticScope.writeState("style", "young adults"); |
| 107 | + agenticScope.writeState("step", "out_2"); |
| 108 | + } else if (agenticScope.readState("step").equals("in_3")) { |
| 109 | + agenticScope.writeState("story", agenticScope.readState("input")); |
| 110 | + agenticScope.writeState("style", "funny"); |
| 111 | + agenticScope.writeState("step", "out_3"); |
| 112 | + } else { |
| 113 | + throw new RuntimeException("We should not reach here"); |
| 114 | + } |
| 115 | + }) |
| 116 | + .outputAs( |
| 117 | + agenticScope -> { |
| 118 | + if (agenticScope.readState("step").equals("out_1")) { |
| 119 | + assertEquals("A fantasy story...", agenticScope.readState("input")); |
| 120 | + agenticScope.writeState("step", "in_2"); |
| 121 | + } else if (agenticScope.readState("step").equals("out_2")) { |
| 122 | + assertEquals( |
| 123 | + "An engaging fantasy story for young adults...", |
| 124 | + agenticScope.readState("input")); |
| 125 | + agenticScope.writeState("step", "in_3"); |
| 126 | + } else if (agenticScope.readState("step").equals("out_3")) { |
| 127 | + assertEquals(8.0, agenticScope.readState("input")); |
| 128 | + } else { |
| 129 | + throw new RuntimeException("We should not reach here"); |
| 130 | + } |
| 131 | + })) |
| 132 | + .build(); |
| 133 | + |
| 134 | + Map<String, Object> out; |
| 135 | + try (WorkflowApplication app = WorkflowApplication.builder().build()) { |
| 136 | + out = app.workflowDefinition(wf).instance(Map.of()).start().get().asMap().orElseThrow(); |
| 137 | + } |
| 138 | + |
| 139 | + assertEquals(8.0, out.get("input")); |
| 140 | + assertEquals("An engaging fantasy story for young adults...", out.get("story")); |
| 141 | + assertEquals("funny", out.get("style")); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void testParallelBeforeAfter() throws ExecutionException, InterruptedException { |
| 146 | + Agents.ChatBot chatBot = mock(Agents.ChatBot.class); |
| 147 | + when(chatBot.chat(eq("A Great Story"))).thenReturn("Once upon a time..."); |
| 148 | + |
| 149 | + Agents.MovieExpert movieExpert = mock(Agents.MovieExpert.class); |
| 150 | + when(movieExpert.findMovie(eq("si-fi"))).thenReturn(List.of("Movie A", "Movie B")); |
| 151 | + |
| 152 | + Workflow wf = |
| 153 | + AgentWorkflowBuilder.workflow() |
| 154 | + .tasks( |
| 155 | + t -> |
| 156 | + t.parallel(chatBot, movieExpert) |
| 157 | + .inputFrom( |
| 158 | + agenticScope -> { |
| 159 | + agenticScope.writeState("userInput", "A Great Story"); |
| 160 | + agenticScope.writeState("mood", "si-fi"); |
| 161 | + })) |
| 162 | + .build(); |
| 163 | + |
| 164 | + try (WorkflowApplication app = WorkflowApplication.builder().build()) { |
| 165 | + app.workflowDefinition(wf).instance(Map.of()).start().get().asJavaObject(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + void testLoopBeforeAfter() throws ExecutionException, InterruptedException { |
| 171 | + Agents.MovieExpert expert = mock(Agents.MovieExpert.class); |
| 172 | + when(expert.findMovie(eq("si-fi"))).thenReturn(List.of("Movie A", "Movie B")); |
| 173 | + AtomicInteger atomicInteger = new AtomicInteger(0); |
| 174 | + |
| 175 | + Workflow wf = |
| 176 | + AgentWorkflowBuilder.workflow() |
| 177 | + .tasks( |
| 178 | + d -> |
| 179 | + d.loop( |
| 180 | + l -> |
| 181 | + l.subAgents(expert) |
| 182 | + .inputFrom(a -> atomicInteger.incrementAndGet()) |
| 183 | + .outputAs( |
| 184 | + agenticScope -> |
| 185 | + agenticScope.writeState("count", atomicInteger.get())) |
| 186 | + .exitCondition(exit -> ((Integer) exit.readState("count")) == 10))) |
| 187 | + .build(); |
| 188 | + |
| 189 | + Map<String, Object> result; |
| 190 | + try (WorkflowApplication app = WorkflowApplication.builder().build()) { |
| 191 | + result = |
| 192 | + app.workflowDefinition(wf) |
| 193 | + .instance(Map.of("mood", "si-fi", "count", 0)) |
| 194 | + .start() |
| 195 | + .get() |
| 196 | + .asMap() |
| 197 | + .orElseThrow(); |
| 198 | + } |
| 199 | + |
| 200 | + assertEquals(10, result.get("count")); |
| 201 | + } |
| 202 | +} |
0 commit comments