parseSearchArgs converts the LLM-supplied JSON envelope into typed search arguments. Supports both flat (`{"term":...}`) and nested @coder envelope (`{"cmd":"search","args":{...}}`) shapes for compatibility with the legacy invocation path.
(args []string)
| 144 | // @coder envelope (`{"cmd":"search","args":{...}}`) shapes for |
| 145 | // compatibility with the legacy invocation path. |
| 146 | func parseSearchArgs(args []string) (searchArgs, error) { |
| 147 | var out searchArgs |
| 148 | if len(args) == 0 { |
| 149 | return out, nil |
| 150 | } |
| 151 | first := strings.TrimSpace(args[0]) |
| 152 | if strings.HasPrefix(first, "{") { |
| 153 | var raw map[string]json.RawMessage |
| 154 | if err := json.Unmarshal([]byte(first), &raw); err != nil { |
| 155 | return out, fmt.Errorf("@search: malformed JSON args: %w", err) |
| 156 | } |
| 157 | if inner, ok := raw["args"]; ok { |
| 158 | var innerMap map[string]json.RawMessage |
| 159 | if jsonErr := json.Unmarshal(inner, &innerMap); jsonErr == nil { |
| 160 | raw = innerMap |
| 161 | } |
| 162 | } |
| 163 | out.Term = jsonString(raw, "term", "pattern", "query", "regex") |
| 164 | out.Dir = jsonString(raw, "dir", "path", "directory") |
| 165 | out.MaxResults = jsonInt(raw, "max_results", "maxResults", "limit") |
| 166 | out.Include = jsonString(raw, "include", "glob") |
| 167 | return out, nil |
| 168 | } |
| 169 | // Positional / flag form (legacy CLI path). |
| 170 | for i := 0; i < len(args); i++ { |
| 171 | switch args[i] { |
| 172 | case "--term", "--pattern": |
| 173 | if i+1 < len(args) { |
| 174 | out.Term = args[i+1] |
| 175 | i++ |
| 176 | } |
| 177 | case "--dir": |
| 178 | if i+1 < len(args) { |
| 179 | out.Dir = args[i+1] |
| 180 | i++ |
| 181 | } |
| 182 | case "--max_results", "--max-results": |
| 183 | if i+1 < len(args) { |
| 184 | out.MaxResults, _ = strconv.Atoi(args[i+1]) |
| 185 | i++ |
| 186 | } |
| 187 | case "--include": |
| 188 | if i+1 < len(args) { |
| 189 | out.Include = args[i+1] |
| 190 | i++ |
| 191 | } |
| 192 | default: |
| 193 | if out.Term == "" && !strings.HasPrefix(args[i], "-") { |
| 194 | out.Term = args[i] |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | return out, nil |
| 199 | } |
| 200 | |
| 201 | // buildSearchArgv reconstructs the engine.handleSearch argv from the |
| 202 | // typed args. Engine uses --glob for the include pattern, while we |