Skip to content

Commit beb987d

Browse files
committed
Improved handling of WorkflowModel and Cognisphere
Signed-off-by: Dmitrii Tikhomirov <chani.liet@gmail.com>
1 parent b11505b commit beb987d

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

fluent/agentic/src/main/java/io/serverlessworkflow/fluent/agentic/AgentAdapters.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,45 @@
1515
*/
1616
package io.serverlessworkflow.fluent.agentic;
1717

18-
import static dev.langchain4j.agentic.internal.AgentExecutor.agentsToExecutors;
18+
import static dev.langchain4j.agentic.internal.AgentUtil.agentsToExecutors;
1919

20-
import dev.langchain4j.agentic.Cognisphere;
20+
import dev.langchain4j.agentic.cognisphere.Cognisphere;
2121
import dev.langchain4j.agentic.internal.AgentExecutor;
2222
import dev.langchain4j.agentic.internal.AgentInstance;
2323
import io.serverlessworkflow.impl.expressions.LoopPredicateIndex;
2424
import java.util.List;
25+
import java.util.Map;
2526
import java.util.function.Function;
2627
import java.util.function.Predicate;
2728
import java.util.stream.Stream;
2829

2930
public final class AgentAdapters {
31+
32+
private static final Cognisphere cognisphere =
33+
Cognisphere.registry().createEphemeralCognisphere();
34+
3035
private AgentAdapters() {}
3136

3237
public static List<AgentExecutor> toExecutors(Object... agents) {
33-
return agentsToExecutors(Stream.of(agents).map(AgentInstance.class::cast).toList());
38+
return agentsToExecutors(Stream.of(agents).map(AgentInstance.class::cast).toArray());
3439
}
3540

36-
public static Function<Cognisphere, Object> toFunction(AgentExecutor exec) {
37-
return exec::invoke;
41+
public static Function<Object, Object> toFunction(AgentExecutor exec) {
42+
return o -> {
43+
if (!(o instanceof Map)) {
44+
throw new IllegalArgumentException(
45+
"Expected input to be a Map, but got: " + o.getClass().getCanonicalName());
46+
}
47+
48+
Map<String, Object> input = (Map<String, Object>) o;
49+
String outputName = exec.agentSpecification().outputName();
50+
cognisphere.writeStates(input);
51+
52+
Object result = exec.invoke(cognisphere);
53+
input.put(outputName, result);
54+
55+
return input;
56+
};
3857
}
3958

4059
public static LoopPredicateIndex<Object, Object> toWhile(Predicate<Cognisphere> exit) {

fluent/agentic/src/main/java/io/serverlessworkflow/fluent/agentic/LoopAgentsBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package io.serverlessworkflow.fluent.agentic;
1717

18-
import dev.langchain4j.agentic.Cognisphere;
18+
import dev.langchain4j.agentic.cognisphere.Cognisphere;
1919
import dev.langchain4j.agentic.internal.AgentExecutor;
2020
import io.serverlessworkflow.api.types.ForTaskConfiguration;
2121
import io.serverlessworkflow.api.types.func.ForTaskFunction;

fluent/agentic/src/test/java/io/serverlessworkflow/fluent/agentic/AgentWorkflowBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import static org.mockito.Mockito.spy;
2525

2626
import dev.langchain4j.agentic.AgentServices;
27-
import dev.langchain4j.agentic.Cognisphere;
27+
import dev.langchain4j.agentic.cognisphere.Cognisphere;
2828
import io.serverlessworkflow.api.types.ForkTask;
2929
import io.serverlessworkflow.api.types.Task;
3030
import io.serverlessworkflow.api.types.TaskItem;

0 commit comments

Comments
 (0)