20
20
import dev .langchain4j .agentic .cognisphere .CognisphereAccess ;
21
21
import dev .langchain4j .agentic .cognisphere .CognisphereKey ;
22
22
import dev .langchain4j .agentic .cognisphere .CognisphereRegistry ;
23
+ import dev .langchain4j .agentic .cognisphere .DefaultCognisphere ;
23
24
import dev .langchain4j .agentic .cognisphere .ResultWithCognisphere ;
24
- import dev .langchain4j .agentic .internal .AgentInstance ;
25
+ import dev .langchain4j .agentic .internal .AgentInvoker ;
25
26
import dev .langchain4j .agentic .internal .AgentSpecification ;
26
27
import dev .langchain4j .agentic .internal .CognisphereOwner ;
27
28
import dev .langchain4j .service .MemoryId ;
28
29
import io .serverlessworkflow .api .types .Workflow ;
29
30
import io .serverlessworkflow .impl .WorkflowApplication ;
30
31
import io .serverlessworkflow .impl .WorkflowModel ;
31
32
33
+ import java .lang .reflect .InvocationHandler ;
32
34
import java .lang .reflect .Method ;
33
35
import java .lang .reflect .Parameter ;
34
36
import java .util .HashMap ;
35
37
import java .util .Map ;
36
38
import java .util .concurrent .CompletableFuture ;
37
39
import java .util .concurrent .ExecutionException ;
40
+ import java .util .concurrent .atomic .AtomicReference ;
38
41
39
- public class WorkflowInvocationHandler implements CognisphereOwner {
42
+ public class WorkflowInvocationHandler implements InvocationHandler , CognisphereOwner {
40
43
41
44
private final Workflow workflow ;
42
45
private final WorkflowApplication .Builder workflowApplicationBuilder ;
43
- private Cognisphere cognisphere ;
46
+ private DefaultCognisphere cognisphere ;
47
+ private Class <?> agentServiceClass ;
48
+ private final AtomicReference <CognisphereRegistry > cognisphereRegistry = new AtomicReference <>();
44
49
45
50
WorkflowInvocationHandler (
46
- Workflow workflow , WorkflowApplication .Builder workflowApplicationBuilder ) {
51
+ Workflow workflow , WorkflowApplication .Builder workflowApplicationBuilder , Class <?> agentServiceClass ) {
47
52
this .workflow = workflow ;
48
53
this .workflowApplicationBuilder = workflowApplicationBuilder ;
54
+ this .agentServiceClass = agentServiceClass ;
49
55
}
50
56
51
57
@ SuppressWarnings ("unchecked" )
@@ -56,7 +62,7 @@ private static void writeCognisphereState(Cognisphere cognisphere, Method method
56
62
Parameter [] parameters = method .getParameters ();
57
63
for (int i = 0 ; i < parameters .length ; i ++) {
58
64
int index = i ;
59
- AgentSpecification .optionalParameterName (parameters [i ])
65
+ AgentInvoker .optionalParameterName (parameters [i ])
60
66
.ifPresent (argName -> cognisphere .writeState (argName , args [index ]));
61
67
}
62
68
}
@@ -70,7 +76,7 @@ private static void writeWorkflowInputState(final Map<String, Object> input, Met
70
76
Parameter [] parameters = method .getParameters ();
71
77
for (int i = 0 ; i < parameters .length ; i ++) {
72
78
int index = i ;
73
- AgentSpecification .optionalParameterName (parameters [i ])
79
+ AgentInvoker .optionalParameterName (parameters [i ])
74
80
.ifPresent (argName -> input .put (argName , args [index ]));
75
81
}
76
82
}
@@ -82,8 +88,9 @@ private String agentId() {
82
88
83
89
@ Override
84
90
public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
91
+ CognisphereRegistry registry = cognisphereRegistry ();
85
92
// outputName
86
- if (method .getDeclaringClass () == AgentInstance .class ) {
93
+ if (method .getDeclaringClass () == AgentSpecification .class ) {
87
94
return switch (method .getName ()) {
88
95
case "outputName" ->
89
96
this .workflow
@@ -101,7 +108,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
101
108
// Ingest the workflow input as a Cognisphere object
102
109
// Later, retrieve it and start the workflow with it as input.
103
110
return switch (method .getName ()) {
104
- case "withCognisphere" -> this .withCognisphere ((Cognisphere ) args [0 ]);
111
+ case "withCognisphere" -> this .withCognisphere ((DefaultCognisphere ) args [0 ]);
112
+ case "registry" -> registry ;
105
113
default ->
106
114
throw new UnsupportedOperationException (
107
115
"Unknown method on CognisphereOwner class : " + method .getName ());
@@ -111,10 +119,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
111
119
// evictCognisphere
112
120
if (method .getDeclaringClass () == CognisphereAccess .class ) {
113
121
return switch (method .getName ()) {
114
- case "getCognisphere" ->
115
- CognisphereRegistry .get (new CognisphereKey (this .agentId (), args [0 ]));
116
- case "evictCognisphere" ->
117
- CognisphereRegistry .evict (new CognisphereKey (this .agentId (), args [0 ]));
122
+ case "getCognisphere" -> registry ().get (args [0 ]);
123
+ case "evictCognisphere" -> registry ().evict (args [0 ]);
118
124
default ->
119
125
throw new UnsupportedOperationException (
120
126
"Unknown method on CognisphereAccess class : " + method .getName ());
@@ -126,10 +132,6 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
126
132
}
127
133
128
134
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
135
Object input ;
134
136
if (args == null || args .length == 0 ) {
135
137
input = new HashMap <>();
@@ -155,21 +157,20 @@ private Object executeWorkflow(Method method, Object[] args) {
155
157
}
156
158
}
157
159
158
- @ Override
159
- public CognisphereOwner withCognisphere (Cognisphere cognisphere ) {
160
- this .cognisphere = cognisphere ;
161
- return this ;
160
+ private CognisphereRegistry cognisphereRegistry () {
161
+ cognisphereRegistry .compareAndSet (null , new CognisphereRegistry (this .agentServiceClass .getName ()));
162
+ return cognisphereRegistry .get ();
162
163
}
163
164
164
- private Cognisphere currentCognisphere (Method method , Object [] args ) {
165
+ private Cognisphere currentCognisphere (CognisphereRegistry registry , Method method , Object [] args ) {
165
166
if (cognisphere != null ) {
166
167
return cognisphere ;
167
168
}
168
169
169
170
Object memoryId = memoryId (method , args );
170
171
return memoryId != null
171
- ? CognisphereRegistry .getOrCreate (new CognisphereKey (this .agentId (), memoryId ))
172
- : CognisphereRegistry .createEphemeralCognisphere ();
172
+ ? registry .getOrCreate (new CognisphereKey (this .agentId (), memoryId ))
173
+ : registry .createEphemeralCognisphere ();
173
174
}
174
175
175
176
private Object memoryId (Method method , Object [] args ) {
@@ -181,4 +182,15 @@ private Object memoryId(Method method, Object[] args) {
181
182
}
182
183
return null ;
183
184
}
185
+
186
+ @ Override
187
+ public CognisphereOwner withCognisphere (DefaultCognisphere cognisphere ) {
188
+ this .cognisphere = cognisphere ;
189
+ return this ;
190
+ }
191
+
192
+ @ Override
193
+ public CognisphereRegistry registry () {
194
+ return this .cognisphereRegistry ();
195
+ }
184
196
}
0 commit comments