3030
3131import java .io .Closeable ;
3232import java .io .IOException ;
33+ import java .util .Collections ;
3334import java .util .List ;
3435import java .util .Map ;
3536import java .util .Objects ;
@@ -361,6 +362,9 @@ public CompletableFuture<ClientResponse> executeQueryAsync(ClientRequest request
361362 /**
362363 * Generates a multi-agent plan for a complex task.
363364 *
365+ * <p>This method uses the Agent API with request_type "multi-agent-plan"
366+ * to generate and execute plans through the governance layer.
367+ *
364368 * @param request the plan request
365369 * @return the generated plan
366370 * @throws PlanExecutionException if plan generation fails
@@ -369,13 +373,83 @@ public PlanResponse generatePlan(PlanRequest request) {
369373 Objects .requireNonNull (request , "request cannot be null" );
370374
371375 return retryExecutor .execute (() -> {
372- Request httpRequest = buildRequest ("POST" , "/api/v1/orchestrator/plan" , request );
376+ // Build agent request format - use HashMap to allow null-safe values
377+ String userToken = request .getUserToken ();
378+ if (userToken == null ) {
379+ userToken = config .getClientId () != null ? config .getClientId () : "default" ;
380+ }
381+ String clientId = config .getClientId () != null ? config .getClientId () : "default" ;
382+ String domain = request .getDomain () != null ? request .getDomain () : "generic" ;
383+
384+ Map <String , Object > agentRequest = new java .util .HashMap <>();
385+ agentRequest .put ("query" , request .getObjective ());
386+ agentRequest .put ("user_token" , userToken );
387+ agentRequest .put ("client_id" , clientId );
388+ agentRequest .put ("request_type" , "multi-agent-plan" );
389+ agentRequest .put ("context" , Map .of ("domain" , domain ));
390+
391+ Request httpRequest = buildRequest ("POST" , "/api/request" , agentRequest );
373392 try (Response response = httpClient .newCall (httpRequest ).execute ()) {
374- return parseResponse (response , PlanResponse . class );
393+ return parsePlanResponse (response , request . getDomain () );
375394 }
376395 }, "generatePlan" );
377396 }
378397
398+ /**
399+ * Parses the Agent API response format into PlanResponse.
400+ * The Agent API returns: {success, plan_id, data: {steps, domain, ...}, metadata, result}
401+ */
402+ @ SuppressWarnings ("unchecked" )
403+ private PlanResponse parsePlanResponse (Response response , String requestDomain ) throws IOException {
404+ handleErrorResponse (response );
405+
406+ ResponseBody body = response .body ();
407+ if (body == null ) {
408+ throw new AxonFlowException ("Empty response body" , response .code (), null );
409+ }
410+
411+ String json = body .string ();
412+ Map <String , Object > agentResponse = objectMapper .readValue (json ,
413+ new TypeReference <Map <String , Object >>() {});
414+
415+ // Check for errors
416+ Boolean success = (Boolean ) agentResponse .get ("success" );
417+ if (success == null || !success ) {
418+ String error = (String ) agentResponse .get ("error" );
419+ throw new PlanExecutionException (error != null ? error : "Plan generation failed" );
420+ }
421+
422+ // Extract fields from Agent API response format
423+ String planId = (String ) agentResponse .get ("plan_id" );
424+ Map <String , Object > data = (Map <String , Object >) agentResponse .get ("data" );
425+ Map <String , Object > metadata = (Map <String , Object >) agentResponse .get ("metadata" );
426+ String result = (String ) agentResponse .get ("result" );
427+
428+ // Extract nested fields from data
429+ List <PlanStep > steps = Collections .emptyList ();
430+ String domain = requestDomain != null ? requestDomain : "generic" ;
431+ Integer complexity = null ;
432+ Boolean parallel = null ;
433+ String estimatedDuration = null ;
434+
435+ if (data != null ) {
436+ // Parse steps if present
437+ List <Map <String , Object >> rawSteps = (List <Map <String , Object >>) data .get ("steps" );
438+ if (rawSteps != null ) {
439+ steps = rawSteps .stream ()
440+ .map (stepMap -> objectMapper .convertValue (stepMap , PlanStep .class ))
441+ .toList ();
442+ }
443+ domain = data .get ("domain" ) != null ? (String ) data .get ("domain" ) : domain ;
444+ complexity = data .get ("complexity" ) != null ? ((Number ) data .get ("complexity" )).intValue () : null ;
445+ parallel = (Boolean ) data .get ("parallel" );
446+ estimatedDuration = (String ) data .get ("estimated_duration" );
447+ }
448+
449+ return new PlanResponse (planId , steps , domain , complexity , parallel ,
450+ estimatedDuration , metadata , null , result );
451+ }
452+
379453 /**
380454 * Asynchronously generates a multi-agent plan.
381455 *
0 commit comments