Skip to content

Commit a0c187a

Browse files
committed
Tentative to call the workflow
Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com>
1 parent e40c7a5 commit a0c187a

File tree

6 files changed

+419
-34
lines changed

6 files changed

+419
-34
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,8 @@ public abstract class AbstractAgentService<T, S> implements WorkflowDefinitionBu
3737
protected final Class<T> agentServiceClass;
3838

3939
protected AbstractAgentService(Class<T> agentServiceClass) {
40-
this("", agentServiceClass);
41-
}
42-
43-
protected AbstractAgentService(String name, Class<T> agentServiceClass) {
4440
this.workflowBuilder =
45-
AgentWorkflowBuilder.workflow(name)
41+
AgentWorkflowBuilder.workflow()
4642
.outputAs(DEFAULT_OUTPUT_FUNCTION)
4743
.input(i -> i.from(DEFAULT_INPUT_FUNCTION));
4844
this.agentServiceClass = agentServiceClass;
@@ -60,25 +56,27 @@ public T build() {
6056

6157
@SuppressWarnings("unchecked")
6258
public S beforeCall(Consumer<Cognisphere> beforeCall) {
63-
// TODO: Our runner must know that our input can be a cognisphere object and extract it from the
64-
// context, so that we can accept the consumer.
65-
// TODO: For now, we can add the consumer to inputFrom
66-
this.workflowBuilder.input(i -> i.from(beforeCall));
59+
this.workflowBuilder.inputFrom(
60+
cog -> {
61+
beforeCall.accept(cog);
62+
return cog;
63+
},
64+
Cognisphere.class);
6765
return (S) this;
6866
}
6967

7068
@SuppressWarnings("unchecked")
7169
public S outputName(String outputName) {
7270
Function<Cognisphere, Object> outputFunction = cog -> cog.readState(outputName);
73-
this.workflowBuilder.outputAs(outputFunction);
71+
this.workflowBuilder.outputAs(outputFunction, Cognisphere.class);
7472
this.workflowBuilder.document(
7573
d -> d.metadata(m -> m.metadata(META_KEY_OUTPUTNAME, outputName)));
7674
return (S) this;
7775
}
7876

7977
@SuppressWarnings("unchecked")
8078
public S output(Function<Cognisphere, Object> output) {
81-
this.workflowBuilder.outputAs(output);
79+
this.workflowBuilder.outputAs(output, Cognisphere.class);
8280
return (S) this;
8381
}
8482

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

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

18+
import dev.langchain4j.agentic.UntypedAgent;
1819
import dev.langchain4j.agentic.cognisphere.Cognisphere;
1920
import dev.langchain4j.agentic.cognisphere.CognisphereAccess;
2021
import dev.langchain4j.agentic.cognisphere.CognisphereKey;
2122
import dev.langchain4j.agentic.cognisphere.CognisphereRegistry;
23+
import dev.langchain4j.agentic.cognisphere.ResultWithCognisphere;
2224
import dev.langchain4j.agentic.internal.AgentInstance;
25+
import dev.langchain4j.agentic.internal.AgentSpecification;
2326
import dev.langchain4j.agentic.internal.CognisphereOwner;
27+
import dev.langchain4j.service.MemoryId;
2428
import io.serverlessworkflow.api.types.Workflow;
2529
import io.serverlessworkflow.impl.WorkflowApplication;
30+
import io.serverlessworkflow.impl.WorkflowModel;
31+
2632
import java.lang.reflect.Method;
33+
import java.lang.reflect.Parameter;
34+
import java.util.HashMap;
35+
import java.util.Map;
36+
import java.util.concurrent.CompletableFuture;
37+
import java.util.concurrent.ExecutionException;
2738

2839
public class WorkflowInvocationHandler implements CognisphereOwner {
2940

@@ -37,6 +48,38 @@ public class WorkflowInvocationHandler implements CognisphereOwner {
3748
this.workflowApplicationBuilder = workflowApplicationBuilder;
3849
}
3950

51+
@SuppressWarnings("unchecked")
52+
private static void writeCognisphereState(Cognisphere cognisphere, Method method, Object[] args) {
53+
if (method.getDeclaringClass() == UntypedAgent.class) {
54+
cognisphere.writeStates((Map<String, Object>) args[0]);
55+
} else {
56+
Parameter[] parameters = method.getParameters();
57+
for (int i = 0; i < parameters.length; i++) {
58+
int index = i;
59+
AgentSpecification.optionalParameterName(parameters[i])
60+
.ifPresent(argName -> cognisphere.writeState(argName, args[index]));
61+
}
62+
}
63+
}
64+
65+
@SuppressWarnings("unchecked")
66+
private static void writeWorkflowInputState(final Map<String, Object> input, Method method, Object[] args) {
67+
if (method.getDeclaringClass() == UntypedAgent.class) {
68+
input.putAll(((Map<String, Object>) args[0]));
69+
} else {
70+
Parameter[] parameters = method.getParameters();
71+
for (int i = 0; i < parameters.length; i++) {
72+
int index = i;
73+
AgentSpecification.optionalParameterName(parameters[i])
74+
.ifPresent(argName -> input.put(argName, args[index]));
75+
}
76+
}
77+
}
78+
79+
private String agentId() {
80+
return workflow.getDocument().getName();
81+
}
82+
4083
@Override
4184
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
4285
// outputName
@@ -69,24 +112,73 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
69112
if (method.getDeclaringClass() == CognisphereAccess.class) {
70113
return switch (method.getName()) {
71114
case "getCognisphere" ->
72-
CognisphereRegistry.get(
73-
new CognisphereKey(this.workflow.getDocument().getName(), args[0]));
115+
CognisphereRegistry.get(new CognisphereKey(this.agentId(), args[0]));
74116
case "evictCognisphere" ->
75-
CognisphereRegistry.evict(
76-
new CognisphereKey(this.workflow.getDocument().getName(), args[0]));
117+
CognisphereRegistry.evict(new CognisphereKey(this.agentId(), args[0]));
77118
default ->
78119
throw new UnsupportedOperationException(
79120
"Unknown method on CognisphereAccess class : " + method.getName());
80121
};
81122
}
82123

83124
// invoke
84-
return null;
125+
return executeWorkflow(method, args);
126+
}
127+
128+
private Object executeWorkflow(Method method, Object[] args) {
129+
// TODO: actually, we must own the Cognisphere object creation upon calling the workflow
130+
131+
//writeCognisphereState(cognisphere, method, args);
132+
133+
Object input;
134+
if (args == null || args.length == 0) {
135+
input = new HashMap<>();
136+
} else if (args.length == 1) {
137+
input = args[0];
138+
} else {
139+
Map<String, Object> inputMap = new HashMap<>();
140+
writeWorkflowInputState(inputMap, method, args);
141+
input = inputMap;
142+
}
143+
144+
try (WorkflowApplication app = workflowApplicationBuilder.build()) {
145+
CompletableFuture<WorkflowModel> workflowInstance = app.workflowDefinition(workflow).instance(input).start();
146+
147+
if (method.getReturnType().equals(ResultWithCognisphere.class)) {
148+
return workflowInstance.get().as(ResultWithCognisphere.class);
149+
} else {
150+
return workflowInstance.get().asJavaObject();
151+
}
152+
} catch (ExecutionException | InterruptedException e) {
153+
throw new RuntimeException(
154+
"Failed to execute workflow: " + agentId() + " - Cognisphere: " + cognisphere, e);
155+
}
85156
}
86157

87158
@Override
88159
public CognisphereOwner withCognisphere(Cognisphere cognisphere) {
89160
this.cognisphere = cognisphere;
90161
return this;
91162
}
163+
164+
private Cognisphere currentCognisphere(Method method, Object[] args) {
165+
if (cognisphere != null) {
166+
return cognisphere;
167+
}
168+
169+
Object memoryId = memoryId(method, args);
170+
return memoryId != null
171+
? CognisphereRegistry.getOrCreate(new CognisphereKey(this.agentId(), memoryId))
172+
: CognisphereRegistry.createEphemeralCognisphere();
173+
}
174+
175+
private Object memoryId(Method method, Object[] args) {
176+
Parameter[] parameters = method.getParameters();
177+
for (int i = 0; i < parameters.length; i++) {
178+
if (parameters[i].getAnnotation(MemoryId.class) != null) {
179+
return args[i];
180+
}
181+
}
182+
return null;
183+
}
92184
}

0 commit comments

Comments
 (0)