extractStringArg looks for a string value at any of the given JSON keys (in the first positional arg if it parses as JSON) and falls back to --flag style scanning across the positional args. Used by builtins to surface a single contextual value in DescribeCall without depending on the heavier per-pl
(args []string, keys ...string)
| 20 | // |
| 21 | // Keys are tried in order. Empty values are skipped. |
| 22 | func extractStringArg(args []string, keys ...string) string { |
| 23 | if len(args) == 0 { |
| 24 | return "" |
| 25 | } |
| 26 | // 1. JSON in the first arg (the common case for native tool calls). |
| 27 | if v := stringFromJSONArg(args[0], keys); v != "" { |
| 28 | return v |
| 29 | } |
| 30 | // 2. --flag value across all args. |
| 31 | if v := stringFromFlagArgs(args, keys); v != "" { |
| 32 | return v |
| 33 | } |
| 34 | // 3. Whole-arg fallback for simple plugins: the first non-flag arg. |
| 35 | for _, a := range args { |
| 36 | a = strings.TrimSpace(a) |
| 37 | if a == "" || strings.HasPrefix(a, "-") || strings.HasPrefix(a, "{") || strings.HasPrefix(a, "[") { |
| 38 | continue |
| 39 | } |
| 40 | return a |
| 41 | } |
| 42 | return "" |
| 43 | } |
| 44 | |
| 45 | // stringFromJSONArg pulls a string value out of a single JSON argument |
| 46 | // blob, handling both flat formats (`{"url":"…"}`) and the nested @coder |