@@ -17987,7 +17987,7 @@ func PrepareSingleAction(ctx context.Context, user User, fileId string, body []b
1798717987 newParams = append(newParams, param)
1798817988 }
1798917989
17990- log.Printf("URL %#v", originalUrl)
17990+ // log.Printf("URL %#v", originalUrl)
1799117991
1799217992 action.Parameters = newParams
1799317993
@@ -18042,6 +18042,13 @@ func PrepareSingleAction(ctx context.Context, user User, fileId string, body []b
1804218042
1804318043 workflowExecution, _, errString, err := PrepareWorkflowExecution(ctx, workflow, badRequest, 10)
1804418044 if err != nil || len(errString) > 0 {
18045+
18046+ // FIXME: Handle other error returns as well?
18047+ if strings.Contains(errString, "App Auth ID") {
18048+ log.Printf("[DEBUG] Bad auth ID provided for single action: %s", errString)
18049+ return workflowExecution, errors.New("The authentication ID provided is invalid. Please try another.")
18050+ }
18051+
1804518052 log.Printf("[ERROR] Failed preparing single execution (%s): %s", workflowExecution.ExecutionId, err)
1804618053 }
1804718054
@@ -25235,8 +25242,8 @@ func RunCategoryAction(resp http.ResponseWriter, request *http.Request) {
2523525242 categories := GetAllAppCategories()
2523625243
2523725244 value.Category = strings.ToLower(value.Category)
25238- value.Label = strings.ReplaceAll(strings.ToLower(value.Label), " ", "_")
25239- value.AppName = strings.ReplaceAll(strings.ToLower(value.AppName), " ", "_")
25245+ value.Label = strings.ReplaceAll(strings.ToLower(strings.TrimSpace( value.Label) ), " ", "_")
25246+ value.AppName = strings.ReplaceAll(strings.ToLower(strings.TrimSpace( value.AppName) ), " ", "_")
2524025247
2524125248 if value.AppName == "email" {
2524225249 value.Category = "email"
@@ -25351,14 +25358,17 @@ func RunCategoryAction(resp http.ResponseWriter, request *http.Request) {
2535125358
2535225359 //RunAiQuery(systemMessage, userMessage)
2535325360
25361+ partialMatch := true
2535425362 availableLabels := []string{}
25363+
25364+ matchName := strings.ReplaceAll(strings.ToLower(strings.TrimSpace(value.AppName)), " ", "_")
2535525365 for _, app := range newapps {
2535625366 if app.Name == "" || len(app.Categories) == 0 {
2535725367 continue
2535825368 }
2535925369
2536025370 // If we HAVE an app as a category already
25361- if len(value.AppName ) == 0 {
25371+ if len(matchName ) == 0 {
2536225372 availableLabels = []string{}
2536325373 if len(app.Categories) == 0 {
2536425374 continue
@@ -25403,22 +25413,33 @@ func RunCategoryAction(resp http.ResponseWriter, request *http.Request) {
2540325413 }
2540425414
2540525415 } else {
25416+ appName := strings.TrimSpace(strings.ReplaceAll(strings.ToLower(app.Name), " ", "_"))
25417+
2540625418 // If we DONT have a category app already
25407- if app.ID == value.AppName || strings.ReplaceAll(strings.ToLower(app.Name), " ", "_") == value.AppName {
25408- //log.Printf("[DEBUG] Found app - checking label: %s vs %s (%s)", app.Name, value.AppName, app.ID)
25409- selectedAction, selectedCategory, availableLabels = GetActionFromLabel(ctx, selectedApp, value.Label, true)
25419+ if app.ID == matchName || appName == matchName {
25420+ selectedApp = app
25421+ log.Printf("[DEBUG] Found app - checking label: %s vs %s (%s)", app.Name, value.AppName, app.ID)
25422+ //selectedAction, selectedCategory, availableLabels = GetActionFromLabel(ctx, selectedApp, value.Label, true)
25423+ selectedAction, selectedCategory, availableLabels = GetActionFromLabel(ctx, app, value.Label, true)
25424+ partialMatch = false
2541025425
2541125426 break
2541225427
25413- } else if selectedApp.ID == "" && len(value.AppName) > 0 && (strings.Contains(strings.ToLower(app.Name), strings.ToLower(value.AppName)) || strings.Contains(strings.ToLower(value.AppName), strings.ToLower(app.Name))) {
25428+ // Finds a random match, but doesn't break in case it finds exact
25429+ } else if selectedApp.ID == "" && len(matchName) > 0 && (strings.Contains(appName, matchName) || strings.Contains(matchName, appName)) {
2541425430 selectedApp = app
2541525431
25416- log.Printf("[WARNING] Set selected app to partial match %s (%s) for input %s", selectedApp.Name, selectedApp.ID, value.AppName)
25417- selectedAction, selectedCategory, availableLabels = GetActionFromLabel(ctx, selectedApp, value.Label, true)
25432+ log.Printf("[WARNING] Set selected app to PARTIAL match %s (%s) for input %s", selectedApp.Name, selectedApp.ID, value.AppName)
25433+ selectedAction, selectedCategory, availableLabels = GetActionFromLabel(ctx, app, value.Label, true)
25434+
25435+ partialMatch = true
2541825436 }
2541925437 }
2542025438 }
2542125439
25440+ // In case we wanna use this to get good matches
25441+ _ = partialMatch
25442+
2542225443 if len(selectedApp.ID) == 0 {
2542325444 log.Printf("[WARNING] Couldn't find app with ID or name '%s' active in org %s (%s)", value.AppName, user.ActiveOrg.Name, user.ActiveOrg.Id)
2542425445 failed := true
@@ -25501,6 +25522,7 @@ func RunCategoryAction(resp http.ResponseWriter, request *http.Request) {
2550125522 }
2550225523
2550325524 if strings.Contains(strings.ToLower(strings.Join(selectedApp.ReferenceInfo.Triggers, ",")), "webhook") {
25525+ log.Printf("[INFO] App %s (%s) has a webhook trigger as default. Setting available labels to Webhook", selectedApp.Name, selectedApp.ID)
2550425526 availableLabels = append(availableLabels, "Webhook")
2550525527
2550625528 if len(selectedAction.Name) == 0 {
@@ -26048,6 +26070,7 @@ func RunCategoryAction(resp http.ResponseWriter, request *http.Request) {
2604826070
2604926071 client := GetExternalClient(baseUrl)
2605026072
26073+
2605126074 selectedAction.AppName = selectedApp.Name
2605226075 selectedAction.AppID = selectedApp.ID
2605326076 selectedAction.AppVersion = selectedApp.AppVersion
@@ -26221,6 +26244,7 @@ func RunCategoryAction(resp http.ResponseWriter, request *http.Request) {
2622126244
2622226245 //streamUrl = "http://localhost:5002"
2622326246 conversationUrl := fmt.Sprintf("%s/api/v1/conversation", baseUrl)
26247+ log.Printf("[DEBUG][AI] Sending single conversation execution to %s", conversationUrl)
2622426248
2622526249 // Check if "execution_id" & "authorization" queries exist
2622626250 if len(request.Header.Get("Authorization")) == 0 && len(request.URL.Query().Get("execution_id")) > 0 && len(request.URL.Query().Get("authorization")) > 0 {
@@ -26360,6 +26384,7 @@ func RunCategoryAction(resp http.ResponseWriter, request *http.Request) {
2636026384 // The app run url to use. Default delete is false
2636126385 shouldDelete := "false"
2636226386 apprunUrl := fmt.Sprintf("%s/api/v1/apps/%s/run?delete=%s", baseUrl, secondAction.AppID, shouldDelete)
26387+
2636326388 if len(request.Header.Get("Authorization")) == 0 && len(request.URL.Query().Get("execution_id")) > 0 && len(request.URL.Query().Get("authorization")) > 0 {
2636426389 apprunUrl = fmt.Sprintf("%s&execution_id=%s&authorization=%s", apprunUrl, request.URL.Query().Get("execution_id"), request.URL.Query().Get("authorization"))
2636526390
@@ -26381,6 +26406,8 @@ func RunCategoryAction(resp http.ResponseWriter, request *http.Request) {
2638126406
2638226407 // Runs attempts up to X times
2638326408 maxAttempts := 7
26409+
26410+ log.Printf("[DEBUG][AI] Sending single API run execution to %s", apprunUrl)
2638426411 for i := 0; i < maxAttempts; i++ {
2638526412
2638626413 // Sends back how many translations happened
@@ -26422,6 +26449,7 @@ func RunCategoryAction(resp http.ResponseWriter, request *http.Request) {
2642226449 return
2642326450 }
2642426451
26452+
2642526453 // Ensures frontend has something to debug if things go wrong
2642626454 for key, value := range newresp.Header {
2642726455 if strings.HasSuffix(strings.ToLower(key), "-url") {
@@ -26444,14 +26472,27 @@ func RunCategoryAction(resp http.ResponseWriter, request *http.Request) {
2644426472 return
2644526473 }
2644626474
26475+ // Parse success struct
26476+ successStruct := ResultChecker{}
26477+ json.Unmarshal(apprunBody, &successStruct)
26478+
26479+ httpOutput, marshalledBody, httpParseErr := FindHttpBody(apprunBody)
26480+ //log.Printf("\n\nGOT RESPONSE (%d): %s. STATUS: %d\n\n", newresp.StatusCode, string(apprunBody), httpOutput.Status)
26481+ if successStruct.Success == false && len(successStruct.Reason) > 0 && httpOutput.Status == 0 && strings.Contains(strings.ReplaceAll(string(apprunBody), " ", ""), `"success":false`){
26482+ log.Printf("[WARNING][AI] Failed running app %s (%s). Contact support. Reason: %s", selectedAction.Name, selectedAction.AppID, successStruct.Reason)
26483+
26484+ resp.WriteHeader(400)
26485+ resp.Write(apprunBody)
26486+ return
26487+ }
26488+
2644726489 // Input value to get raw output instead of translated
2644826490 if value.SkipOutputTranslation {
2644926491 resp.WriteHeader(202)
2645026492 resp.Write(apprunBody)
2645126493 return
2645226494 }
2645326495
26454- httpOutput, marshalledBody, httpParseErr := FindHttpBody(apprunBody)
2645526496 parsedTranslation := SchemalessOutput{
2645626497 Success: false,
2645726498 Action: value.Label,
@@ -27029,6 +27070,7 @@ func GetActionFromLabel(ctx context.Context, app WorkflowApp, label string, fixL
2702927070
2703027071 categories := GetAllAppCategories()
2703127072 lowercaseLabel := strings.ReplaceAll(strings.ToLower(label), " ", "_")
27073+ exactMatch := false
2703227074 for _, action := range app.Actions {
2703327075 if len(action.CategoryLabel) == 0 {
2703427076 //log.Printf("%s: %#v\n", action.Name, action.CategoryLabel)
@@ -27037,15 +27079,18 @@ func GetActionFromLabel(ctx context.Context, app WorkflowApp, label string, fixL
2703727079
2703827080 //log.Printf("FOUND LABELS: %s -> %#v\n", action.Name, action.CategoryLabel)
2703927081
27040- for labelIndex, _ := range action.CategoryLabel {
27041- if strings.ReplaceAll(strings.ToLower(action.CategoryLabel[labelIndex]), " ", "_") == "no_label" {
27082+ for _, label := range action.CategoryLabel {
27083+ newLabel := strings.ReplaceAll(strings.ToLower(label), " ", "_")
27084+ if newLabel == "no_label" {
2704227085 continue
2704327086 }
2704427087
27045- availableLabels = append(availableLabels, action.CategoryLabel[labelIndex])
27046- actionCategory := strings.ReplaceAll(strings.ToLower(action.CategoryLabel[labelIndex]), " ", "_")
27088+ // To ensure we have both normal + parsed label
27089+ availableLabels = append(availableLabels, newLabel)
27090+ availableLabels = append(availableLabels, label)
2704727091
27048- if actionCategory == lowercaseLabel || strings.HasPrefix(actionCategory, lowercaseLabel) {
27092+ if newLabel == lowercaseLabel || strings.HasPrefix(newLabel, lowercaseLabel) {
27093+ //log.Printf("[DEBUG] Found action for label '%s' in app %s (%s): %s", label, app.Name, app.ID, action.Name)
2704927094 selectedAction = action
2705027095
2705127096 for _, category := range categories {
@@ -27054,13 +27099,20 @@ func GetActionFromLabel(ctx context.Context, app WorkflowApp, label string, fixL
2705427099 break
2705527100 }
2705627101 }
27102+
27103+ if newLabel == lowercaseLabel {
27104+ exactMatch = true
27105+ break
27106+ }
2705727107 }
2705827108 }
27059- }
2706027109
27061- //log.Printf("\n\n[DEBUG] SELECTED: %#v\n\n", selectedAction)
27110+ if len(selectedAction.ID) > 0 && exactMatch {
27111+ break
27112+ }
27113+ }
2706227114
27063- // FIXME: If selectedAction isn't chosen, then we need to try to discover it in the app
27115+ // Decides if we are to autocomplete the app if labels are not found
2706427116 if len(selectedAction.ID) == 0 {
2706527117 if fixLabels == true {
2706627118 //log.Printf("\n\n[DEBUG] Action not found in app %s (%s) for label '%s'. Autodiscovering and updating the app!!!\n\n", app.Name, app.ID, label)
0 commit comments