18
18
import dev .langchain4j .agentic .UntypedAgent ;
19
19
import dev .langchain4j .agentic .cognisphere .Cognisphere ;
20
20
import dev .langchain4j .agentic .cognisphere .CognisphereAccess ;
21
- import dev .langchain4j .agentic .cognisphere .CognisphereKey ;
22
21
import dev .langchain4j .agentic .cognisphere .CognisphereRegistry ;
23
22
import dev .langchain4j .agentic .cognisphere .DefaultCognisphere ;
24
23
import dev .langchain4j .agentic .cognisphere .ResultWithCognisphere ;
28
27
import dev .langchain4j .service .MemoryId ;
29
28
import io .serverlessworkflow .api .types .Workflow ;
30
29
import io .serverlessworkflow .impl .WorkflowApplication ;
31
- import io .serverlessworkflow .impl .WorkflowModel ;
32
-
30
+ import io .serverlessworkflow .impl .expressions .agentic .langchain4j .CognisphereRegistryAssessor ;
33
31
import java .lang .reflect .InvocationHandler ;
34
32
import java .lang .reflect .Method ;
35
33
import java .lang .reflect .Parameter ;
36
34
import java .util .Map ;
37
- import java .util .concurrent .CompletableFuture ;
38
35
import java .util .concurrent .ExecutionException ;
39
- import java .util .concurrent .atomic .AtomicReference ;
40
36
41
37
public class WorkflowInvocationHandler implements InvocationHandler , CognisphereOwner {
42
38
43
39
private final Workflow workflow ;
44
40
private final WorkflowApplication .Builder workflowApplicationBuilder ;
45
- private DefaultCognisphere cognisphere ;
46
- private Class <?> agentServiceClass ;
47
- private final AtomicReference <CognisphereRegistry > cognisphereRegistry = new AtomicReference <>();
41
+ private final CognisphereRegistryAssessor cognisphereRegistryAssessor ;
48
42
49
43
WorkflowInvocationHandler (
50
- Workflow workflow , WorkflowApplication .Builder workflowApplicationBuilder , Class <?> agentServiceClass ) {
44
+ Workflow workflow ,
45
+ WorkflowApplication .Builder workflowApplicationBuilder ,
46
+ Class <?> agentServiceClass ) {
51
47
this .workflow = workflow ;
52
48
this .workflowApplicationBuilder = workflowApplicationBuilder ;
53
- this .agentServiceClass = agentServiceClass ;
49
+ this .cognisphereRegistryAssessor = new CognisphereRegistryAssessor ( agentServiceClass . getName ()) ;
54
50
}
55
51
56
52
@ SuppressWarnings ("unchecked" )
@@ -67,22 +63,26 @@ private static void writeCognisphereState(Cognisphere cognisphere, Method method
67
63
}
68
64
}
69
65
70
- private String agentId () {
71
- return workflow .getDocument ().getName ();
66
+ private String outputName () {
67
+ Object outputName =
68
+ this .workflow
69
+ .getDocument ()
70
+ .getMetadata ()
71
+ .getAdditionalProperties ()
72
+ .get (WorkflowDefinitionBuilder .META_KEY_OUTPUTNAME );
73
+ if (outputName != null ) {
74
+ return outputName .toString ();
75
+ }
76
+ return null ;
72
77
}
73
78
74
79
@ Override
75
80
public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
76
- CognisphereRegistry registry = cognisphereRegistry ();
81
+ CognisphereRegistry registry = registry ();
77
82
// outputName
78
83
if (method .getDeclaringClass () == AgentSpecification .class ) {
79
84
return switch (method .getName ()) {
80
- case "outputName" ->
81
- this .workflow
82
- .getDocument ()
83
- .getMetadata ()
84
- .getAdditionalProperties ()
85
- .get (WorkflowDefinitionBuilder .META_KEY_OUTPUTNAME );
85
+ case "outputName" -> outputName ();
86
86
default ->
87
87
throw new UnsupportedOperationException (
88
88
"Unknown method on AgentInstance class : " + method .getName ());
@@ -113,39 +113,44 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
113
113
}
114
114
115
115
// invoke
116
- return executeWorkflow (currentCognisphere (cognisphereRegistry (), method , args ), method , args );
116
+ return executeWorkflow (currentCognisphere (method , args ), method , args );
117
117
}
118
118
119
119
private Object executeWorkflow (DefaultCognisphere cognisphere , Method method , Object [] args ) {
120
120
writeCognisphereState (cognisphere , method , args );
121
121
122
122
try (WorkflowApplication app = workflowApplicationBuilder .build ()) {
123
- CompletableFuture <WorkflowModel > workflowInstance = app .workflowDefinition (workflow ).instance (cognisphere ).start ();
124
- Object result = workflowInstance .get ().asJavaObject ();
125
- return method .getReturnType ().equals (ResultWithCognisphere .class ) ?
126
- new ResultWithCognisphere <>(cognisphere , result ) :
127
- result ;
123
+ // TODO improve result handling
124
+ DefaultCognisphere output =
125
+ app .workflowDefinition (workflow )
126
+ .instance (cognisphere )
127
+ .start ()
128
+ .get ()
129
+ .as (DefaultCognisphere .class )
130
+ .orElseThrow (
131
+ () ->
132
+ new IllegalArgumentException (
133
+ "Workflow hasn't returned a Cognisphere object." ));
134
+ Object result = output .readState (outputName ());
135
+
136
+ return method .getReturnType ().equals (ResultWithCognisphere .class )
137
+ ? new ResultWithCognisphere <>(output , result )
138
+ : result ;
128
139
129
140
} catch (ExecutionException | InterruptedException e ) {
130
141
throw new RuntimeException (
131
- "Failed to execute workflow: " + agentId () + " - Cognisphere: " + cognisphere , e );
142
+ "Failed to execute workflow: "
143
+ + workflow .getDocument ().getName ()
144
+ + " - Cognisphere: "
145
+ + cognisphere ,
146
+ e );
132
147
}
133
148
}
134
149
135
- private CognisphereRegistry cognisphereRegistry () {
136
- cognisphereRegistry .compareAndSet (null , new CognisphereRegistry (this .agentServiceClass .getName ()));
137
- return cognisphereRegistry .get ();
138
- }
139
-
140
- private DefaultCognisphere currentCognisphere (CognisphereRegistry registry , Method method , Object [] args ) {
141
- if (cognisphere != null ) {
142
- return cognisphere ;
143
- }
144
-
150
+ private DefaultCognisphere currentCognisphere (Method method , Object [] args ) {
145
151
Object memoryId = memoryId (method , args );
146
- return memoryId != null
147
- ? registry .getOrCreate (new CognisphereKey (this .agentId (), memoryId ))
148
- : registry .createEphemeralCognisphere ();
152
+ this .cognisphereRegistryAssessor .setMemoryId (memoryId );
153
+ return this .cognisphereRegistryAssessor .getCognisphere ();
149
154
}
150
155
151
156
private Object memoryId (Method method , Object [] args ) {
@@ -160,12 +165,12 @@ private Object memoryId(Method method, Object[] args) {
160
165
161
166
@ Override
162
167
public CognisphereOwner withCognisphere (DefaultCognisphere cognisphere ) {
163
- this .cognisphere = cognisphere ;
168
+ this .cognisphereRegistryAssessor . withCognisphere ( cognisphere ) ;
164
169
return this ;
165
170
}
166
171
167
172
@ Override
168
173
public CognisphereRegistry registry () {
169
- return this .cognisphereRegistry ();
174
+ return this .cognisphereRegistryAssessor . registry ();
170
175
}
171
176
}
0 commit comments