Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import dev.langchain4j.agentic.scope.DefaultAgenticScope;
import io.serverlessworkflow.api.types.func.LoopPredicateIndex;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

Expand All @@ -34,8 +36,21 @@ public static List<AgentExecutor> toExecutors(Object... agents) {
return agentsToExecutors(agents);
}

public static Function<DefaultAgenticScope, Object> toFunction(AgentExecutor exec) {
return exec::execute;
public static Function<DefaultAgenticScope, Object> toFunction(
AgentExecutor exec,
AtomicReference<Consumer<AgenticScope>> beforeAgentInvocation,
AtomicReference<Consumer<AgenticScope>> afterAgentInvocation) {
return defaultAgenticScope -> {
if (beforeAgentInvocation.get() != null) {
beforeAgentInvocation.get().accept(defaultAgenticScope);
}
Object result = exec.execute(defaultAgenticScope);
if (afterAgentInvocation.get() != null) {
defaultAgenticScope.writeState("input", result);
afterAgentInvocation.get().accept(defaultAgenticScope);
}
return result;
};
}

public static LoopPredicateIndex<AgenticScope, Object> toWhile(Predicate<AgenticScope> exit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.serverlessworkflow.fluent.agentic;

import dev.langchain4j.agentic.scope.AgenticScope;
import io.serverlessworkflow.fluent.agentic.spi.AgentDoFluent;
import io.serverlessworkflow.fluent.func.FuncCallTaskBuilder;
import io.serverlessworkflow.fluent.func.FuncEmitTaskBuilder;
Expand Down Expand Up @@ -117,4 +118,14 @@ public AgentDoTaskBuilder switchCase(
this.listBuilder().switchCase(name, itemsConfigurer);
return self();
}

public AgentDoTaskBuilder inputFrom(Consumer<AgenticScope> inputFrom) {
this.listBuilder().inputFrom(inputFrom);
return self();
}

public AgentDoTaskBuilder outputAs(Consumer<AgenticScope> outputAs) {
this.listBuilder().outputAs(outputAs);
return self();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.serverlessworkflow.fluent.agentic;

import dev.langchain4j.agentic.internal.AgentExecutor;
import dev.langchain4j.agentic.scope.AgenticScope;
import dev.langchain4j.agentic.scope.DefaultAgenticScope;
import io.serverlessworkflow.api.types.Task;
import io.serverlessworkflow.api.types.TaskItem;
Expand All @@ -29,12 +30,17 @@
import io.serverlessworkflow.fluent.func.FuncTaskItemListBuilder;
import io.serverlessworkflow.fluent.spec.BaseTaskItemListBuilder;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

public class AgentTaskItemListBuilder extends BaseTaskItemListBuilder<AgentTaskItemListBuilder>
implements AgentDoFluent<AgentTaskItemListBuilder> {

private final FuncTaskItemListBuilder delegate;
private final AtomicReference<Consumer<AgenticScope>> beforeAgentInvocation =
new AtomicReference<>();
private final AtomicReference<Consumer<AgenticScope>> afterAgentInvocation =
new AtomicReference<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why atomic? Is there a concurrency for changing these builders somewhere?


public AgentTaskItemListBuilder() {
super();
Expand All @@ -58,7 +64,11 @@ public AgentTaskItemListBuilder agent(String name, Object agent) {
exec ->
this.delegate.function(
name,
fn -> fn.function(AgentAdapters.toFunction(exec), DefaultAgenticScope.class)));
fn ->
fn.function(
AgentAdapters.toFunction(
exec, beforeAgentInvocation, afterAgentInvocation),
DefaultAgenticScope.class)));
return self();
}

Expand Down Expand Up @@ -96,7 +106,10 @@ public AgentTaskItemListBuilder parallel(String name, Object... agents) {
ex.agentInvoker().name() != null
? ex.agentInvoker().name()
: "branch-" + i + "-" + name;
fork.branch(agentName, AgentAdapters.toFunction(ex), DefaultAgenticScope.class);
fork.branch(
agentName,
AgentAdapters.toFunction(ex, beforeAgentInvocation, afterAgentInvocation),
DefaultAgenticScope.class);
}
});
return self();
Expand Down Expand Up @@ -154,4 +167,14 @@ public AgentTaskItemListBuilder switchCase(
this.delegate.switchCase(name, itemsConfigurer);
return self();
}

public AgentTaskItemListBuilder inputFrom(Consumer<AgenticScope> beforeAgentInvocation) {
this.beforeAgentInvocation.set(beforeAgentInvocation);
return self();
}

public AgentTaskItemListBuilder outputAs(Consumer<AgenticScope> afterAgentInvocationConsumer) {
this.afterAgentInvocation.set(afterAgentInvocationConsumer);
return self();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import io.serverlessworkflow.fluent.func.FuncTaskItemListBuilder;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.ObjIntConsumer;
import java.util.function.Predicate;
import java.util.stream.IntStream;
Expand All @@ -31,6 +33,10 @@ public class LoopAgentsBuilder {

private final FuncTaskItemListBuilder funcDelegate;
private final ForTaskFunction forTask;
private final AtomicReference<Consumer<AgenticScope>> beforeAgentInvocation =
new AtomicReference<>();
private final AtomicReference<Consumer<AgenticScope>> afterAgentInvocation =
new AtomicReference<>();

private int maxIterations = 1024;

Expand All @@ -50,7 +56,11 @@ public LoopAgentsBuilder subAgents(String baseName, Object... agents) {
execs,
(exec, idx) ->
funcDelegate.function(
baseName + "-" + idx, fn -> fn.function(AgentAdapters.toFunction(exec))));
baseName + "-" + idx,
fn ->
fn.function(
AgentAdapters.toFunction(
exec, beforeAgentInvocation, afterAgentInvocation))));
return this;
}

Expand All @@ -73,6 +83,16 @@ public LoopAgentsBuilder exitCondition(BiPredicate<AgenticScope, Integer> exitCo
return this;
}

public LoopAgentsBuilder inputFrom(Consumer<AgenticScope> beforeAgentInvocationConsumer) {
this.beforeAgentInvocation.set(beforeAgentInvocationConsumer);
return this;
}

public LoopAgentsBuilder outputAs(Consumer<AgenticScope> afterAgentInvocationConsumer) {
this.afterAgentInvocation.set(afterAgentInvocationConsumer);
return this;
}

public ForTaskFunction build() {
this.forTask.setDo(this.funcDelegate.build());
this.forTask.withCollection(ignored -> IntStream.range(0, maxIterations).boxed().toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
* 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.fluent.agentic;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.impl.WorkflowApplication;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;

public class AgenticBeforeAfterInvokeTest {

@Test
void testAgentBeforeAfter() throws ExecutionException, InterruptedException {
Agents.ChatBot chatBot = mock(Agents.ChatBot.class);
when(chatBot.chat(eq("A Great Story"))).thenReturn("Once upon a time...");

Workflow wf =
AgentWorkflowBuilder.workflow()
.tasks(
t ->
t.agent(chatBot)
.inputFrom(
agenticScope -> {
assertTrue(agenticScope.state().containsKey("userInput"));
assertEquals("A Great Story", agenticScope.readState("userInput"));
agenticScope.writeState("testData", "someValue");
})
.outputAs(
agenticScope -> {
assertTrue(agenticScope.state().containsKey("userInput"));
assertEquals("A Great Story", agenticScope.readState("userInput"));
assertTrue(agenticScope.state().containsKey("testData"));
assertEquals("someValue", agenticScope.readState("testData"));
assertEquals("Once upon a time...", agenticScope.readState("input"));
agenticScope.writeState("outputData", "outputValue");
}))
.build();

Map<String, Object> in = new HashMap<>();
in.put("userInput", "A Great Story");

Map<String, Object> out;
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
out = app.workflowDefinition(wf).instance(in).start().get().asMap().orElseThrow();
}

assertTrue(out.containsKey("outputData"));
assertEquals("outputValue", out.get("outputData"));
assertTrue(out.containsKey("userInput"));
assertEquals("A Great Story", out.get("userInput"));
assertTrue(out.containsKey("testData"));
assertEquals("someValue", out.get("testData"));
assertTrue(out.containsKey("input"));
assertEquals("Once upon a time...", out.get("input"));
}

@Test
void testSeqBeforeAfter() throws ExecutionException, InterruptedException {
Agents.CreativeWriter creativeWriter = mock(Agents.CreativeWriter.class);
when(creativeWriter.generateStory((eq("dragons and wizards"))))
.thenReturn("A fantasy story...");

Agents.StyleEditor styleEditor = mock(Agents.StyleEditor.class);
when(styleEditor.editStory(eq("A fantasy story..."), eq("young adults")))
.thenReturn("An engaging fantasy story for young adults...");

Agents.StyleScorer styleScorer = mock(Agents.StyleScorer.class);
when(styleScorer.scoreStyle(eq("An engaging fantasy story for young adults..."), eq("funny")))
.thenReturn(8.0);

Workflow wf =
AgentWorkflowBuilder.workflow()
.tasks(
t ->
t.sequence(creativeWriter, styleEditor, styleScorer)
.inputFrom(
agenticScope -> {
if (agenticScope.readState("step") == null) {
agenticScope.writeState("topic", "dragons and wizards");
agenticScope.writeState("step", "out_1");
} else if (agenticScope.readState("step").equals("in_2")) {
agenticScope.writeState("story", agenticScope.readState("input"));
agenticScope.writeState("style", "young adults");
agenticScope.writeState("step", "out_2");
} else if (agenticScope.readState("step").equals("in_3")) {
agenticScope.writeState("story", agenticScope.readState("input"));
agenticScope.writeState("style", "funny");
agenticScope.writeState("step", "out_3");
} else {
throw new RuntimeException("We should not reach here");
}
})
.outputAs(
agenticScope -> {
if (agenticScope.readState("step").equals("out_1")) {
assertEquals("A fantasy story...", agenticScope.readState("input"));
agenticScope.writeState("step", "in_2");
} else if (agenticScope.readState("step").equals("out_2")) {
assertEquals(
"An engaging fantasy story for young adults...",
agenticScope.readState("input"));
agenticScope.writeState("step", "in_3");
} else if (agenticScope.readState("step").equals("out_3")) {
assertEquals(8.0, agenticScope.readState("input"));
} else {
throw new RuntimeException("We should not reach here");
}
}))
.build();

Map<String, Object> out;
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
out = app.workflowDefinition(wf).instance(Map.of()).start().get().asMap().orElseThrow();
}

assertEquals(8.0, out.get("input"));
assertEquals("An engaging fantasy story for young adults...", out.get("story"));
assertEquals("funny", out.get("style"));
}

@Test
void testParallelBeforeAfter() throws ExecutionException, InterruptedException {
Agents.ChatBot chatBot = mock(Agents.ChatBot.class);
when(chatBot.chat(eq("A Great Story"))).thenReturn("Once upon a time...");

Agents.MovieExpert movieExpert = mock(Agents.MovieExpert.class);
when(movieExpert.findMovie(eq("si-fi"))).thenReturn(List.of("Movie A", "Movie B"));

Workflow wf =
AgentWorkflowBuilder.workflow()
.tasks(
t ->
t.parallel(chatBot, movieExpert)
.inputFrom(
agenticScope -> {
agenticScope.writeState("userInput", "A Great Story");
agenticScope.writeState("mood", "si-fi");
}))
.build();

try (WorkflowApplication app = WorkflowApplication.builder().build()) {
app.workflowDefinition(wf).instance(Map.of()).start().get().asJavaObject();
}
}

@Test
void testLoopBeforeAfter() throws ExecutionException, InterruptedException {
Agents.MovieExpert expert = mock(Agents.MovieExpert.class);
when(expert.findMovie(eq("si-fi"))).thenReturn(List.of("Movie A", "Movie B"));
AtomicInteger atomicInteger = new AtomicInteger(0);

Workflow wf =
AgentWorkflowBuilder.workflow()
.tasks(
d ->
d.loop(
l ->
l.subAgents(expert)
.inputFrom(a -> atomicInteger.incrementAndGet())
.outputAs(
agenticScope ->
agenticScope.writeState("count", atomicInteger.get()))
.exitCondition(exit -> ((Integer) exit.readState("count")) == 10)))
.build();

Map<String, Object> result;
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
result =
app.workflowDefinition(wf)
.instance(Map.of("mood", "si-fi", "count", 0))
.start()
.get()
.asMap()
.orElseThrow();
}

assertEquals(10, result.get("count"));
}
}