1515 */
1616package io .serverlessworkflow .fluent .agentic .langchain4j ;
1717
18+ import dev .langchain4j .agentic .UntypedAgent ;
1819import dev .langchain4j .agentic .cognisphere .Cognisphere ;
1920import dev .langchain4j .agentic .cognisphere .CognisphereAccess ;
2021import dev .langchain4j .agentic .cognisphere .CognisphereKey ;
2122import dev .langchain4j .agentic .cognisphere .CognisphereRegistry ;
23+ import dev .langchain4j .agentic .cognisphere .ResultWithCognisphere ;
2224import dev .langchain4j .agentic .internal .AgentInstance ;
25+ import dev .langchain4j .agentic .internal .AgentSpecification ;
2326import dev .langchain4j .agentic .internal .CognisphereOwner ;
27+ import dev .langchain4j .service .MemoryId ;
2428import io .serverlessworkflow .api .types .Workflow ;
2529import io .serverlessworkflow .impl .WorkflowApplication ;
30+ import io .serverlessworkflow .impl .WorkflowModel ;
31+
2632import 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
2839public 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