11package io .sentrius .agent .analysis .agents .agents ;
22
33import com .fasterxml .jackson .core .JsonProcessingException ;
4+ import com .fasterxml .jackson .databind .JsonNode ;
5+ import com .fasterxml .jackson .databind .node .ObjectNode ;
46import io .github .classgraph .ClassGraph ;
57import io .github .classgraph .ScanResult ;
8+ import io .sentrius .sso .core .dto .agents .AgentExecutionContextDTO ;
69import io .sentrius .sso .core .dto .capabilities .EndpointDescriptor ;
710import io .sentrius .agent .discovery .AgentEndpointDiscoveryService ;
8- import io .sentrius .sso .core .dto .ztat .AgentExecution ;
11+ import io .sentrius .sso .core .dto .agents .AgentExecution ;
912import io .sentrius .sso .core .dto .ztat .ZtatRequestDTO ;
1013import io .sentrius .sso .core .exceptions .ZtatException ;
11- import io .sentrius .sso .core .model .verbs .OutputInterpreterIfc ;
1214import io .sentrius .sso .core .model .verbs .Verb ;
1315import io .sentrius .sso .core .model .verbs .VerbResponse ;
1416import io .sentrius .sso .core .services .agents .AgentClientService ;
1517import io .sentrius .sso .core .services .agents .ZeroTrustClientService ;
1618import io .sentrius .sso .core .services .capabilities .EndpointScanningService ;
19+ import io .sentrius .sso .core .utils .JsonUtil ;
1720import lombok .RequiredArgsConstructor ;
1821import lombok .extern .slf4j .Slf4j ;
1922import org .springframework .context .ApplicationContext ;
@@ -93,11 +96,11 @@ public void scanClasspath() {
9396 .name (name )
9497 .description (annotation .description ())
9598 .method (method )
99+ .argName (annotation .argName ())
100+ .returnName (annotation .returnName ())
96101 .exampleJson (annotation .exampleJson ())
97102 .requiresTokenManagement (annotation .requiresTokenManagement ())
98103 .returnType (annotation .returnType ())
99- .outputInterpreter (annotation .outputInterpreter ())
100- .inputInterpreter (annotation .inputInterpreter ())
101104 .build ();
102105 verbs .put (name , verb );
103106 instances .put (name , instance );
@@ -119,46 +122,75 @@ public boolean isVerbRegistered(String verb) {
119122 }
120123 }
121124
122- public VerbResponse execute (AgentExecution agentExecution , VerbResponse priorResponse , String verb ,
125+ public VerbResponse execute (AgentExecution agentExecution ,
126+ AgentExecutionContextDTO contextDTO , VerbResponse priorResponse ,
127+ String verb ,
123128 Map <String , Object > args )
124129 throws Exception {
130+
131+ log .info ("Executing {}" , contextDTO );
125132 synchronized (this ) {
126- Map <String , Object > newArgs = new HashMap <>();
133+ var agentVerb = verbs .get (verb );
134+ if (null == agentVerb ) {
135+ throw new IllegalArgumentException ("Unknown verb: " + verb );
136+ }
137+
127138 if (null != args ) {
128- newArgs .putAll (args );
139+
140+ for (Map .Entry <String , Object > entry : args .entrySet ()) {
141+ contextDTO .getExecutionArgs ().put (entry .getKey (), entry .getValue ().toString ());
142+ }
143+
144+ if (agentVerb .getArgName () != null && !agentVerb .getArgName ().isEmpty () && !agentVerb .getArgName ().equals ("arg1" )) {
145+ ObjectNode object = JsonUtil .MAPPER .createObjectNode ();
146+ for (Map .Entry <String , Object > entry : args .entrySet ()) {
147+ var node = JsonUtil .MAPPER .valueToTree (entry .getValue ());
148+ object .put (entry .getKey (), node );
149+ log .info ("Interpreting input " +
150+ "for AgentExecutionContextDTO: {}" , entry .getValue ());
151+ }
152+
153+
154+ contextDTO .getExecutionArgs ().put (agentVerb .getArgName (), object );
155+
156+ }
129157 }
158+
130159 log .info ("Executing verb: {}" , verb );
131- var returnType = verbs . get ( verb ) .getReturnType ();
160+ var returnType = agentVerb .getReturnType ();
132161 if (null != priorResponse ) {
133- Class <? extends OutputInterpreterIfc > interpreter = priorResponse .getOutputInterpreter ();
134162 log .info ("Interpreting prior response for verb: {}" , verb );
135- log .info ("Interpreting prior response with: {}" , interpreter .getName ());
136- log .info ("Interpreting prior response: {}" , priorResponse .getReturnType ());
163+ log .info ("Interpreting prior response: {}" , priorResponse .getReturnType ());
137164 log .info ("New response return type: {}" , returnType );
138- newArgs .putAll (interpreter .getConstructor ().newInstance ().interpret (priorResponse ));
139165 }
140- Method method = verbs .get (verb ).getMethod ();
141- var inputInterpreter = verbs .get (verb ).getInputInterpreter ();
166+ Method method = agentVerb .getMethod ();
142167 Object instance = instances .get (verb );
143168 if (method == null ) {
144169 throw new IllegalArgumentException ("Unknown verb: " + verb );
145170 }
146- var interpreterInstance = inputInterpreter .getConstructor ().newInstance ();
147171 try {
148172
149173
150- log .info ("Interpreting input with: {}" , interpreterInstance .getClass ().getName ());
151- log .info ("Interpreting input: {}" , newArgs .getClass ().getName ());
152- log .info ("Interpreting input: {}" , newArgs );
153- var interpretedInput = interpreterInstance .interpret (newArgs );
174+
175+ var thisVerb = agentVerb ;
176+ var exec = thisVerb .isRequiresTokenManagement () ?
177+ method .invoke (instance , agentExecution , contextDTO ) :
178+ method .invoke (instance , contextDTO );
179+
180+ JsonNode execNode = JsonUtil .MAPPER .valueToTree (exec );
181+ log .info ("Interpreting output for AgentExecutionContextDTO: {}" , execNode );
182+ // add the output
183+ if (null != thisVerb .getReturnName () && !thisVerb .getReturnName ().isEmpty ()) {
184+ contextDTO .addToMemory (thisVerb .getReturnName (), execNode );
185+ } else {
186+ contextDTO .addToMemory (verb , execNode );
187+ }
188+
154189
155190 return VerbResponse .builder ()
156- .response (verbs .get (verb ).isRequiresTokenManagement () ?
157- method .invoke (instance ,agentExecution , interpretedInput ) :
158- method .invoke (instance , interpretedInput ))
159191 .returnType (returnType )
160- .outputInterpreter (verbs .get (verb ).getOutputInterpreter ())
161192 .build ();
193+
162194 } catch (InvocationTargetException e ) {
163195 Throwable targetException = e .getTargetException ();
164196 if (targetException instanceof ZtatException ztatEx ) {
@@ -179,22 +211,31 @@ public VerbResponse execute(AgentExecution agentExecution, VerbResponse priorRes
179211
180212 log .info ("Re-attempting verb execution after Ztat token acquisition: {}" , verb );
181213
182- var interpretedInput = interpreterInstance .interpret (newArgs );
183-
184- log .info ("Re-attempting verb execution after Ztat token acquisition: {}" , verb );
214+ var thisVerb = agentVerb ;
215+ var exec = thisVerb .isRequiresTokenManagement () ?
216+ method .invoke (instance , agentExecution , contextDTO ) :
217+ method .invoke (instance , contextDTO );
218+ JsonNode execNode = JsonUtil .MAPPER .valueToTree (exec );
219+ // add the output
220+ if (null != thisVerb .getReturnName () && !thisVerb .getReturnName ().isEmpty ()) {
221+ contextDTO .addToMemory (thisVerb .getReturnName (), execNode );
222+ } else {
223+ contextDTO .addToMemory (verb , execNode );
224+ }
185225
186226 return VerbResponse .builder ()
187- .response (verbs .get (verb ).isRequiresTokenManagement () ?
188- method .invoke (instance ,agentExecution , interpretedInput ) :
189- method .invoke (instance , interpretedInput ))
190227 .returnType (returnType )
191- .outputInterpreter (verbs .get (verb ).getOutputInterpreter ())
192228 .build ();
229+
230+
231+
193232 // re-attempt
194233 } else {
195234 throw new RuntimeException (targetException );
196235 }
197236 } catch (Exception e ) {
237+ log .info (method .getName () + " failed" , e );
238+ e .printStackTrace ();
198239 throw new RuntimeException ("Failed to execute verb: " + verb , e );
199240 }
200241 }
0 commit comments