Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ func RunSelfCorrectingRequest(originalFields []Valuereplace, action Action, stat

systemMessage := fmt.Sprintf(`INTRODUCTION

Return all key:value pairs from the last user message, but with modified values to fix ALL the HTTP errors at once. Don't add any comments. Do not try the same thing twice, and use your existing knowledge of the API name and action to reformat the output until it works. All fields in "Required data" MUST be a part of the output if possible. Output MUST be valid JSON.
Return all key:value pairs from the last user message, but with modified values to fix ALL the HTTP errors at once. Don't add any comments. Do not try the same thing twice, and use your existing knowledge of the API name and action to reformat the output until it works. Output MUST be valid JSON.

END INTRODUCTION
---
Expand Down Expand Up @@ -781,6 +781,7 @@ OUTPUT FORMATTING
- Output as JSON for a Rest API
- Do NOT make the same output mistake twice.
- Headers should be separated by newline between each key:value pair
- Queries should be a single string (e.g. "q=foo&bar=baz")

END OUTPUT FORMATTING
---
Expand Down Expand Up @@ -993,6 +994,7 @@ Input JSON Payload (ensure VALID JSON):
urlValue := ""
pathValue := ""
queriesValue := ""
method := ""

// Collect current values from action parameters
for _, param := range action.Parameters {
Expand All @@ -1002,6 +1004,8 @@ Input JSON Payload (ensure VALID JSON):
pathValue = param.Value
} else if param.Name == "queries" {
queriesValue = param.Value
} else if param.Name == "method" {
method = param.Value
}
}

Expand All @@ -1024,6 +1028,11 @@ Input JSON Payload (ensure VALID JSON):
action.Parameters[i].Value = pathValue
case "queries":
action.Parameters[i].Value = queriesValue
case "body":
// If method does not allow body, empty it.
if method == "GET" {
action.Parameters[i].Value = ""
}
}
}

Expand All @@ -1035,33 +1044,59 @@ Input JSON Payload (ensure VALID JSON):
}

func normalize(urlValue, pathValue, queriesValue string) (string, string, string) {
parsed, err := url.Parse(urlValue)
if err != nil {
return urlValue, pathValue, queriesValue
path := strings.TrimSpace(pathValue)
queries := strings.TrimSpace(queriesValue)
baseURL := ""

var parsed *url.URL
if urlValue != "" {
u, err := url.Parse(urlValue)
if err == nil {
parsed = u
if u.Scheme != "" && u.Host != "" {
baseURL = u.Scheme + "://" + u.Host
}
}
}

// If BOTH path and queries are empty, keep the full URL as-is
// This means LLM didn't fill them, so we shouldn't split
if pathValue == "" && queriesValue == "" {
return urlValue, pathValue, queriesValue
// 1. Normalize queries if LLM returned JSON
if strings.HasPrefix(queries, "{") {
var m map[string]interface{}
if json.Unmarshal([]byte(queries), &m) == nil {
parts := []string{}
for k, v := range m {
parts = append(parts, fmt.Sprintf("%s=%v", k, v))
}
queries = strings.Join(parts, "&")
}
}

baseURL := ""
if parsed.Scheme != "" && parsed.Host != "" {
baseURL = parsed.Scheme + "://" + parsed.Host
// 2. If path contains queries and queries field is empty, extract them
if queries == "" && strings.Contains(path, "?") {
parts := strings.SplitN(path, "?", 2)
path = parts[0]
if len(parts) == 2 {
queries = parts[1]
}
}

// 3. If path is empty, try URL
if path == "" && parsed != nil {
path = parsed.Path
}

// Extract path from URL if path is still empty
if pathValue == "" {
pathValue = parsed.Path
// 4. If queries still empty, try URL
if queries == "" && parsed != nil {
queries = parsed.RawQuery
}

// Extract queries from URL if queries is still empty
if queriesValue == "" {
queriesValue = parsed.RawQuery
// 5. Final safety: path must never contain '?'
if strings.Contains(path, "?") {
parts := strings.SplitN(path, "?", 2)
path = parts[0]
}

return baseURL, pathValue, queriesValue
return baseURL, path, queries
}

func getBadOutputString(action Action, appname, inputdata, outputBody string, status int) string {
Expand Down
Loading