2020import dev .langchain4j .agentic .cognisphere .CognisphereAccess ;
2121import dev .langchain4j .agentic .cognisphere .CognisphereKey ;
2222import dev .langchain4j .agentic .cognisphere .CognisphereRegistry ;
23+ import dev .langchain4j .agentic .cognisphere .DefaultCognisphere ;
2324import dev .langchain4j .agentic .cognisphere .ResultWithCognisphere ;
24- import dev .langchain4j .agentic .internal .AgentInstance ;
25+ import dev .langchain4j .agentic .internal .AgentInvoker ;
2526import dev .langchain4j .agentic .internal .AgentSpecification ;
2627import dev .langchain4j .agentic .internal .CognisphereOwner ;
2728import dev .langchain4j .service .MemoryId ;
2829import io .serverlessworkflow .api .types .Workflow ;
2930import io .serverlessworkflow .impl .WorkflowApplication ;
3031import io .serverlessworkflow .impl .WorkflowModel ;
3132
33+ import java .lang .reflect .InvocationHandler ;
3234import java .lang .reflect .Method ;
3335import java .lang .reflect .Parameter ;
3436import java .util .HashMap ;
3537import java .util .Map ;
3638import java .util .concurrent .CompletableFuture ;
3739import java .util .concurrent .ExecutionException ;
40+ import java .util .concurrent .atomic .AtomicReference ;
3841
39- public class WorkflowInvocationHandler implements CognisphereOwner {
42+ public class WorkflowInvocationHandler implements InvocationHandler , CognisphereOwner {
4043
4144 private final Workflow workflow ;
4245 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 <>();
4449
4550 WorkflowInvocationHandler (
46- Workflow workflow , WorkflowApplication .Builder workflowApplicationBuilder ) {
51+ Workflow workflow , WorkflowApplication .Builder workflowApplicationBuilder , Class <?> agentServiceClass ) {
4752 this .workflow = workflow ;
4853 this .workflowApplicationBuilder = workflowApplicationBuilder ;
54+ this .agentServiceClass = agentServiceClass ;
4955 }
5056
5157 @ SuppressWarnings ("unchecked" )
@@ -56,7 +62,7 @@ private static void writeCognisphereState(Cognisphere cognisphere, Method method
5662 Parameter [] parameters = method .getParameters ();
5763 for (int i = 0 ; i < parameters .length ; i ++) {
5864 int index = i ;
59- AgentSpecification .optionalParameterName (parameters [i ])
65+ AgentInvoker .optionalParameterName (parameters [i ])
6066 .ifPresent (argName -> cognisphere .writeState (argName , args [index ]));
6167 }
6268 }
@@ -70,7 +76,7 @@ private static void writeWorkflowInputState(final Map<String, Object> input, Met
7076 Parameter [] parameters = method .getParameters ();
7177 for (int i = 0 ; i < parameters .length ; i ++) {
7278 int index = i ;
73- AgentSpecification .optionalParameterName (parameters [i ])
79+ AgentInvoker .optionalParameterName (parameters [i ])
7480 .ifPresent (argName -> input .put (argName , args [index ]));
7581 }
7682 }
@@ -82,8 +88,9 @@ private String agentId() {
8288
8389 @ Override
8490 public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
91+ CognisphereRegistry registry = cognisphereRegistry ();
8592 // outputName
86- if (method .getDeclaringClass () == AgentInstance .class ) {
93+ if (method .getDeclaringClass () == AgentSpecification .class ) {
8794 return switch (method .getName ()) {
8895 case "outputName" ->
8996 this .workflow
@@ -101,7 +108,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
101108 // Ingest the workflow input as a Cognisphere object
102109 // Later, retrieve it and start the workflow with it as input.
103110 return switch (method .getName ()) {
104- case "withCognisphere" -> this .withCognisphere ((Cognisphere ) args [0 ]);
111+ case "withCognisphere" -> this .withCognisphere ((DefaultCognisphere ) args [0 ]);
112+ case "registry" -> registry ;
105113 default ->
106114 throw new UnsupportedOperationException (
107115 "Unknown method on CognisphereOwner class : " + method .getName ());
@@ -111,10 +119,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
111119 // evictCognisphere
112120 if (method .getDeclaringClass () == CognisphereAccess .class ) {
113121 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 ]);
118124 default ->
119125 throw new UnsupportedOperationException (
120126 "Unknown method on CognisphereAccess class : " + method .getName ());
@@ -126,10 +132,6 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
126132 }
127133
128134 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-
133135 Object input ;
134136 if (args == null || args .length == 0 ) {
135137 input = new HashMap <>();
@@ -155,21 +157,20 @@ private Object executeWorkflow(Method method, Object[] args) {
155157 }
156158 }
157159
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 ();
162163 }
163164
164- private Cognisphere currentCognisphere (Method method , Object [] args ) {
165+ private Cognisphere currentCognisphere (CognisphereRegistry registry , Method method , Object [] args ) {
165166 if (cognisphere != null ) {
166167 return cognisphere ;
167168 }
168169
169170 Object memoryId = memoryId (method , args );
170171 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 ();
173174 }
174175
175176 private Object memoryId (Method method , Object [] args ) {
@@ -181,4 +182,15 @@ private Object memoryId(Method method, Object[] args) {
181182 }
182183 return null ;
183184 }
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+ }
184196}
0 commit comments