diff --git a/functions/onprem/worker/worker.go b/functions/onprem/worker/worker.go index eb3db030..707ca7b6 100644 --- a/functions/onprem/worker/worker.go +++ b/functions/onprem/worker/worker.go @@ -1498,11 +1498,77 @@ func handleExecutionResult(workflowExecution shuffle.WorkflowExecution) { action, _ = singul.HandleSingulStartnode(workflowExecution, action, []string{}) parsedAppname := strings.Replace(strings.ToLower(action.AppName), " ", "-", -1) - //if strings.ToLower(parsedAppname) == "singul" { - // parsedAppname = "shuffle-ai" - // appversion = "1.0.0" - // appname = "shuffle-ai" - //} + // if strings.ToLower(parsedAppname) == "singul" { + // parsedAppname = "shuffle-ai" + // appversion = "1.0.0" + // appname = "shuffle-ai" + // } + + if parsedAppname == "ai-agent" { + log.Printf("[INFO][%s] Running AI Agent action %s via backend API", workflowExecution.ExecutionId, action.ID) + + fullUrl := fmt.Sprintf("%s/api/v1/agent?execution_id=%s&authorization=%s&action_id=%s", + baseUrl, workflowExecution.ExecutionId, workflowExecution.Authorization, action.ID) + + serverUrl := os.Getenv("SHUFFLE_BACKEND_URL") + if len(serverUrl) > 0 { + fullUrl = fmt.Sprintf("%s/api/v1/agent?execution_id=%s&authorization=%s&action_id=%s", + serverUrl, workflowExecution.ExecutionId, workflowExecution.Authorization, action.ID) + } + + inputParamValue := "" + for _, param := range action.Parameters { + if strings.ToLower(param.Name) == "input" { + inputParamValue = param.Value + break + } + } + + requestBody := map[string]interface{}{ + "id": action.ID, + "params": map[string]interface{}{ + "tool_name": action.AppName, + "tool_id": action.AppID, + "environment": action.Environment, + "input": map[string]interface{}{ + "text": inputParamValue, + }, + }, + } + + requestBodyBytes, err := json.Marshal(requestBody) + if err != nil { + log.Printf("[ERROR][%s] Failed marshalling request body: %s", workflowExecution.ExecutionId, err) + continue + } + + req, err := http.NewRequest("POST", fullUrl, bytes.NewBuffer(requestBodyBytes)) + if err != nil { + log.Printf("[ERROR][%s] Failed creating AI Agent request: %s", workflowExecution.ExecutionId, err) + continue + } + + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{Timeout: 10 * time.Second} + resp, err := client.Do(req) + if err != nil { + log.Printf("[ERROR][%s] Failed triggering AI Agent (timeout/error): %s", workflowExecution.ExecutionId, err) + log.Printf("[INFO][%s] Exiting execution handler - backend will requeue when agent completes", workflowExecution.ExecutionId) + return + } + + defer resp.Body.Close() + // body, err := ioutil.ReadAll(resp.Body) + // if err != nil { + // log.Printf("[ERROR][%s] Failed reading AI Agent response: %s", workflowExecution.ExecutionId, err) + // } else { + // log.Printf("[INFO][%s] AI Agent triggered: %s", workflowExecution.ExecutionId, string(body)) + // } + + log.Printf("[INFO][%s] AI Agent triggered successfully - exiting execution handler, backend will requeue when agent completes", workflowExecution.ExecutionId) + return + } imageName := fmt.Sprintf("%s:%s_%s", baseimagename, parsedAppname, action.AppVersion) if strings.Contains(imageName, " ") {