extractJSON strips optional markdown code fences (```json or ```) that LLMs sometimes wrap around JSON output, returning only the inner JSON string. If the response starts with JSON, it returns the first JSON value so trailing commentary or dangling fences do not break parsing.
(s string)
| 103 | // string. If the response starts with JSON, it returns the first JSON value so |
| 104 | // trailing commentary or dangling fences do not break parsing. |
| 105 | func extractJSON(s string) string { |
| 106 | s = strings.TrimSpace(s) |
| 107 | if matches := markdownCodeFenceRE.FindStringSubmatch(s); matches != nil { |
| 108 | s = strings.TrimSpace(matches[1]) |
| 109 | } |
| 110 | |
| 111 | var raw json.RawMessage |
| 112 | if err := json.NewDecoder(strings.NewReader(s)).Decode(&raw); err == nil { |
| 113 | return string(raw) |
| 114 | } |
| 115 | |
| 116 | return s |
| 117 | } |
| 118 | |
| 119 | type TaskName struct { |
| 120 | Name string `json:"task_name"` |
no outgoing calls