1818import dev .langchain4j .agentic .UntypedAgent ;
1919import dev .langchain4j .agentic .cognisphere .Cognisphere ;
2020import dev .langchain4j .agentic .cognisphere .CognisphereAccess ;
21- import dev .langchain4j .agentic .cognisphere .CognisphereKey ;
2221import dev .langchain4j .agentic .cognisphere .CognisphereRegistry ;
2322import dev .langchain4j .agentic .cognisphere .DefaultCognisphere ;
2423import dev .langchain4j .agentic .cognisphere .ResultWithCognisphere ;
2827import dev .langchain4j .service .MemoryId ;
2928import io .serverlessworkflow .api .types .Workflow ;
3029import io .serverlessworkflow .impl .WorkflowApplication ;
31- import io .serverlessworkflow .impl .WorkflowModel ;
32-
30+ import io .serverlessworkflow .impl .expressions .agentic .langchain4j .CognisphereRegistryAssessor ;
3331import java .lang .reflect .InvocationHandler ;
3432import java .lang .reflect .Method ;
3533import java .lang .reflect .Parameter ;
3634import java .util .Map ;
37- import java .util .concurrent .CompletableFuture ;
3835import java .util .concurrent .ExecutionException ;
39- import java .util .concurrent .atomic .AtomicReference ;
4036
4137public class WorkflowInvocationHandler implements InvocationHandler , CognisphereOwner {
4238
4339 private final Workflow workflow ;
4440 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 ;
4842
4943 WorkflowInvocationHandler (
50- Workflow workflow , WorkflowApplication .Builder workflowApplicationBuilder , Class <?> agentServiceClass ) {
44+ Workflow workflow ,
45+ WorkflowApplication .Builder workflowApplicationBuilder ,
46+ Class <?> agentServiceClass ) {
5147 this .workflow = workflow ;
5248 this .workflowApplicationBuilder = workflowApplicationBuilder ;
53- this .agentServiceClass = agentServiceClass ;
49+ this .cognisphereRegistryAssessor = new CognisphereRegistryAssessor ( agentServiceClass . getName ()) ;
5450 }
5551
5652 @ SuppressWarnings ("unchecked" )
@@ -67,22 +63,26 @@ private static void writeCognisphereState(Cognisphere cognisphere, Method method
6763 }
6864 }
6965
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 ;
7277 }
7378
7479 @ Override
7580 public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
76- CognisphereRegistry registry = cognisphereRegistry ();
81+ CognisphereRegistry registry = registry ();
7782 // outputName
7883 if (method .getDeclaringClass () == AgentSpecification .class ) {
7984 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 ();
8686 default ->
8787 throw new UnsupportedOperationException (
8888 "Unknown method on AgentInstance class : " + method .getName ());
@@ -113,39 +113,44 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
113113 }
114114
115115 // invoke
116- return executeWorkflow (currentCognisphere (cognisphereRegistry (), method , args ), method , args );
116+ return executeWorkflow (currentCognisphere (method , args ), method , args );
117117 }
118118
119119 private Object executeWorkflow (DefaultCognisphere cognisphere , Method method , Object [] args ) {
120120 writeCognisphereState (cognisphere , method , args );
121121
122122 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 ;
128139
129140 } catch (ExecutionException | InterruptedException e ) {
130141 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 );
132147 }
133148 }
134149
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 ) {
145151 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 ();
149154 }
150155
151156 private Object memoryId (Method method , Object [] args ) {
@@ -160,12 +165,12 @@ private Object memoryId(Method method, Object[] args) {
160165
161166 @ Override
162167 public CognisphereOwner withCognisphere (DefaultCognisphere cognisphere ) {
163- this .cognisphere = cognisphere ;
168+ this .cognisphereRegistryAssessor . withCognisphere ( cognisphere ) ;
164169 return this ;
165170 }
166171
167172 @ Override
168173 public CognisphereRegistry registry () {
169- return this .cognisphereRegistry ();
174+ return this .cognisphereRegistryAssessor . registry ();
170175 }
171176}
0 commit comments