Skip to content

Commit 09b7cf5

Browse files
committed
Moving to cognisphere as input
Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com>
1 parent 88cf6e8 commit 09b7cf5

File tree

12 files changed

+57
-96
lines changed

12 files changed

+57
-96
lines changed

experimental/agentic/src/main/java/io/serverlessworkflow/impl/expressions/agentic/AgenticModel.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,15 @@
1616
package io.serverlessworkflow.impl.expressions.agentic;
1717

1818
import dev.langchain4j.agentic.cognisphere.Cognisphere;
19-
import dev.langchain4j.agentic.cognisphere.ResultWithCognisphere;
2019
import io.serverlessworkflow.impl.WorkflowModel;
2120
import io.serverlessworkflow.impl.expressions.func.JavaModel;
2221
import java.util.Collection;
23-
import java.util.Collections;
2422
import java.util.Optional;
2523

2624
class AgenticModel extends JavaModel {
2725

28-
private final Cognisphere cognisphere;
29-
30-
AgenticModel(Object object, Cognisphere cognisphere) {
31-
super(object);
32-
this.cognisphere = cognisphere;
26+
AgenticModel(Cognisphere cognisphere) {
27+
super(cognisphere);
3328
}
3429

3530
@Override
@@ -39,17 +34,13 @@ public void setObject(Object obj) {
3934

4035
@Override
4136
public Collection<WorkflowModel> asCollection() {
42-
return object instanceof Collection value
43-
? new AgenticModelCollection(value, cognisphere)
44-
: Collections.emptyList();
37+
throw new UnsupportedOperationException("Not supported yet.");
4538
}
4639

4740
@Override
4841
public <T> Optional<T> as(Class<T> clazz) {
4942
if (Cognisphere.class.isAssignableFrom(clazz)) {
50-
return Optional.of(clazz.cast(cognisphere));
51-
} else if (ResultWithCognisphere.class.isAssignableFrom(clazz)) {
52-
return Optional.of(clazz.cast(new ResultWithCognisphere<>(cognisphere, object)));
43+
return Optional.of(clazz.cast(object));
5344
} else {
5445
return super.as(clazz);
5546
}

experimental/agentic/src/main/java/io/serverlessworkflow/impl/expressions/agentic/AgenticModelCollection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class AgenticModelCollection extends JavaModelCollection {
3737

3838
@Override
3939
protected WorkflowModel nextItem(Object obj) {
40-
return new AgenticModel(obj, cognisphere);
40+
return new AgenticModel((Cognisphere) obj);
4141
}
4242

4343
@Override

experimental/agentic/src/main/java/io/serverlessworkflow/impl/expressions/agentic/AgenticModelFactory.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,17 @@
1616
package io.serverlessworkflow.impl.expressions.agentic;
1717

1818
import dev.langchain4j.agentic.cognisphere.Cognisphere;
19-
import dev.langchain4j.agentic.cognisphere.CognisphereRegistry;
2019
import io.cloudevents.CloudEvent;
2120
import io.cloudevents.CloudEventData;
2221
import io.serverlessworkflow.impl.WorkflowModel;
2322
import io.serverlessworkflow.impl.WorkflowModelCollection;
2423
import io.serverlessworkflow.impl.WorkflowModelFactory;
24+
import io.serverlessworkflow.impl.expressions.func.JavaModel;
2525
import java.time.OffsetDateTime;
2626
import java.util.Map;
2727

2828
class AgenticModelFactory implements WorkflowModelFactory {
2929

30-
private Cognisphere cognisphere = CognisphereRegistry.createEphemeralCognisphere();
31-
32-
private final AgenticModel TrueModel = new AgenticModel(Boolean.TRUE, cognisphere);
33-
private final AgenticModel FalseModel = new AgenticModel(Boolean.FALSE, cognisphere);
34-
private final AgenticModel NullModel = new AgenticModel(null, cognisphere);
35-
36-
public void setCognishere(Cognisphere cognisphere) {
37-
this.cognisphere = cognisphere;
38-
}
39-
4030
@Override
4131
public WorkflowModel fromAny(WorkflowModel prev, Object obj) {
4232
((AgenticModel) prev).setObject(obj);
@@ -45,57 +35,59 @@ public WorkflowModel fromAny(WorkflowModel prev, Object obj) {
4535

4636
@Override
4737
public WorkflowModel combine(Map<String, WorkflowModel> workflowVariables) {
48-
return new AgenticModel(workflowVariables, cognisphere);
38+
throw new UnsupportedOperationException();
4939
}
5040

5141
@Override
5242
public WorkflowModelCollection createCollection() {
53-
return new AgenticModelCollection(cognisphere);
43+
throw new UnsupportedOperationException();
5444
}
5545

5646
@Override
5747
public WorkflowModel from(boolean value) {
58-
return value ? TrueModel : FalseModel;
48+
return new JavaModel(value);
5949
}
6050

6151
@Override
6252
public WorkflowModel from(Number value) {
63-
return new AgenticModel(value, cognisphere);
53+
return new JavaModel(value);
6454
}
6555

6656
@Override
6757
public WorkflowModel from(String value) {
68-
return new AgenticModel(value, cognisphere);
58+
return new JavaModel(value);
6959
}
7060

7161
@Override
7262
public WorkflowModel from(CloudEvent ce) {
73-
return new AgenticModel(ce, cognisphere);
63+
return new JavaModel(ce);
7464
}
7565

7666
@Override
7767
public WorkflowModel from(CloudEventData ce) {
78-
return new AgenticModel(ce, cognisphere);
68+
return new JavaModel(ce);
7969
}
8070

8171
@Override
8272
public WorkflowModel from(OffsetDateTime value) {
83-
return new AgenticModel(value, cognisphere);
73+
return new JavaModel(value);
8474
}
8575

8676
@Override
8777
public WorkflowModel from(Map<String, Object> map) {
88-
cognisphere.writeStates(map);
89-
return new AgenticModel(map, cognisphere);
78+
return new JavaModel(map);
9079
}
9180

9281
@Override
9382
public WorkflowModel fromNull() {
94-
return NullModel;
83+
return new JavaModel(null);
9584
}
9685

9786
@Override
9887
public WorkflowModel fromOther(Object value) {
99-
return new AgenticModel(value, cognisphere);
88+
if (value instanceof Cognisphere) {
89+
return new AgenticModel((Cognisphere) value);
90+
}
91+
return new JavaModel(value);
10092
}
10193
}

experimental/lambda/src/main/java/io/serverlessworkflow/impl/expressions/func/JavaModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class JavaModel implements WorkflowModel {
2929

3030
protected Object object;
3131

32-
protected JavaModel(Object object) {
32+
public JavaModel(Object object) {
3333
this.object = asJavaObject(object);
3434
}
3535

fluent/agentic-langchain4j/src/main/java/io/serverlessworkflow/fluent/agentic/langchain4j/WorkflowInvocationHandler.java

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.lang.reflect.InvocationHandler;
3434
import java.lang.reflect.Method;
3535
import java.lang.reflect.Parameter;
36-
import java.util.HashMap;
3736
import java.util.Map;
3837
import java.util.concurrent.CompletableFuture;
3938
import java.util.concurrent.ExecutionException;
@@ -68,20 +67,6 @@ private static void writeCognisphereState(Cognisphere cognisphere, Method method
6867
}
6968
}
7069

71-
@SuppressWarnings("unchecked")
72-
private static void writeWorkflowInputState(final Map<String, Object> input, Method method, Object[] args) {
73-
if (method.getDeclaringClass() == UntypedAgent.class) {
74-
input.putAll(((Map<String, Object>) args[0]));
75-
} else {
76-
Parameter[] parameters = method.getParameters();
77-
for (int i = 0; i < parameters.length; i++) {
78-
int index = i;
79-
AgentInvoker.optionalParameterName(parameters[i])
80-
.ifPresent(argName -> input.put(argName, args[index]));
81-
}
82-
}
83-
}
84-
8570
private String agentId() {
8671
return workflow.getDocument().getName();
8772
}
@@ -128,23 +113,14 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
128113
}
129114

130115
// invoke
131-
return executeWorkflow(method, args);
116+
return executeWorkflow(currentCognisphere(cognisphereRegistry(), method, args), method, args);
132117
}
133118

134-
private Object executeWorkflow(Method method, Object[] args) {
135-
Object input;
136-
if (args == null || args.length == 0) {
137-
input = new HashMap<>();
138-
} else if (args.length == 1) {
139-
input = args[0];
140-
} else {
141-
Map<String, Object> inputMap = new HashMap<>();
142-
writeWorkflowInputState(inputMap, method, args);
143-
input = inputMap;
144-
}
119+
private Object executeWorkflow(DefaultCognisphere cognisphere, Method method, Object[] args) {
120+
writeCognisphereState(cognisphere, method, args);
145121

146122
try (WorkflowApplication app = workflowApplicationBuilder.build()) {
147-
CompletableFuture<WorkflowModel> workflowInstance = app.workflowDefinition(workflow).instance(input).start();
123+
CompletableFuture<WorkflowModel> workflowInstance = app.workflowDefinition(workflow).instance(cognisphere).start();
148124

149125
if (method.getReturnType().equals(ResultWithCognisphere.class)) {
150126
return workflowInstance.get().as(ResultWithCognisphere.class);
@@ -162,7 +138,7 @@ private CognisphereRegistry cognisphereRegistry() {
162138
return cognisphereRegistry.get();
163139
}
164140

165-
private Cognisphere currentCognisphere(CognisphereRegistry registry, Method method, Object[] args) {
141+
private DefaultCognisphere currentCognisphere(CognisphereRegistry registry, Method method, Object[] args) {
166142
if (cognisphere != null) {
167143
return cognisphere;
168144
}

fluent/agentic-langchain4j/src/test/java/io/serverlessworkflow/fluent/agentic/langchain4j/WorkflowAgentsIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import org.junit.jupiter.api.Test;
66

7-
import dev.langchain4j.agentic.AgentServices;
7+
import dev.langchain4j.agentic.AgenticServices;
88
import dev.langchain4j.agentic.UntypedAgent;
99
import dev.langchain4j.agentic.workflow.WorkflowAgentsBuilder;
1010

@@ -23,17 +23,17 @@ public class WorkflowAgentsIT {
2323
void sequential_agents_tests() {
2424
WorkflowAgentsBuilder builder = new LC4JWorkflowBuilder();
2525

26-
CreativeWriter creativeWriter = spy(AgentServices.agentBuilder(CreativeWriter.class)
26+
CreativeWriter creativeWriter = spy(AgenticServices.agentBuilder(CreativeWriter.class)
2727
.chatModel(BASE_MODEL)
2828
.outputName("story")
2929
.build());
3030

31-
AudienceEditor audienceEditor = spy(AgentServices.agentBuilder(AudienceEditor.class)
31+
AudienceEditor audienceEditor = spy(AgenticServices.agentBuilder(AudienceEditor.class)
3232
.chatModel(BASE_MODEL)
3333
.outputName("story")
3434
.build());
3535

36-
StyleEditor styleEditor = spy(AgentServices.agentBuilder(StyleEditor.class)
36+
StyleEditor styleEditor = spy(AgenticServices.agentBuilder(StyleEditor.class)
3737
.chatModel(BASE_MODEL)
3838
.outputName("story")
3939
.build());

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
import static dev.langchain4j.agentic.internal.AgentUtil.agentsToExecutors;
1919

2020
import dev.langchain4j.agentic.cognisphere.Cognisphere;
21+
import dev.langchain4j.agentic.cognisphere.DefaultCognisphere;
2122
import dev.langchain4j.agentic.internal.AgentExecutor;
22-
import dev.langchain4j.agentic.internal.AgentInstance;
23+
import dev.langchain4j.agentic.internal.AgentSpecification;
2324
import io.serverlessworkflow.impl.expressions.LoopPredicateIndex;
2425
import java.util.List;
2526
import java.util.function.Function;
@@ -31,11 +32,11 @@ public final class AgentAdapters {
3132
private AgentAdapters() {}
3233

3334
public static List<AgentExecutor> toExecutors(Object... agents) {
34-
return agentsToExecutors(Stream.of(agents).map(AgentInstance.class::cast).toArray());
35+
return agentsToExecutors(Stream.of(agents).map(AgentSpecification.class::cast).toArray());
3536
}
3637

37-
public static Function<Cognisphere, Object> toFunction(AgentExecutor exec) {
38-
return exec::invoke;
38+
public static Function<DefaultCognisphere, Object> toFunction(AgentExecutor exec) {
39+
return exec::execute;
3940
}
4041

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

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

Lines changed: 4 additions & 3 deletions
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.Cognisphere;
18+
import dev.langchain4j.agentic.cognisphere.DefaultCognisphere;
1919
import dev.langchain4j.agentic.internal.AgentExecutor;
2020
import io.serverlessworkflow.api.types.Task;
2121
import io.serverlessworkflow.api.types.TaskItem;
@@ -57,7 +57,8 @@ public AgentTaskItemListBuilder agent(String name, Object agent) {
5757
.forEach(
5858
exec ->
5959
this.delegate.callFn(
60-
name, fn -> fn.function(AgentAdapters.toFunction(exec), Cognisphere.class)));
60+
name,
61+
fn -> fn.function(AgentAdapters.toFunction(exec), DefaultCognisphere.class)));
6162
return self();
6263
}
6364

@@ -92,7 +93,7 @@ public AgentTaskItemListBuilder parallel(String name, Object... agents) {
9293
for (int i = 0; i < execs.size(); i++) {
9394
AgentExecutor ex = execs.get(i);
9495
fork.branch(
95-
"branch-" + i + "-" + name, AgentAdapters.toFunction(ex), Cognisphere.class);
96+
"branch-" + i + "-" + name, AgentAdapters.toFunction(ex), DefaultCognisphere.class);
9697
}
9798
});
9899
return self();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import static org.junit.jupiter.api.Assertions.assertNull;
2525
import static org.mockito.Mockito.spy;
2626

27-
import dev.langchain4j.agentic.AgentServices;
27+
import dev.langchain4j.agentic.AgenticServices;
2828
import dev.langchain4j.agentic.cognisphere.Cognisphere;
2929
import io.serverlessworkflow.api.types.ForkTask;
3030
import io.serverlessworkflow.api.types.Task;
@@ -43,7 +43,7 @@ class AgentWorkflowBuilderTest {
4343
public void verifyAgentCall() {
4444
Agents.MovieExpert movieExpert =
4545
spy(
46-
AgentServices.agentBuilder(Agents.MovieExpert.class)
46+
AgenticServices.agentBuilder(Agents.MovieExpert.class)
4747
.outputName("movies")
4848
.chatModel(BASE_MODEL)
4949
.build());

0 commit comments

Comments
 (0)