parseKnowledgeInvocation accepts the JSON envelope {"cmd":..,"args":{..}}, flat JSON, and the flattened argv form. Returns the canonical (cmd, innerJSON).
(args []string)
| 208 | // parseKnowledgeInvocation accepts the JSON envelope {"cmd":..,"args":{..}}, |
| 209 | // flat JSON, and the flattened argv form. Returns the canonical (cmd, innerJSON). |
| 210 | func parseKnowledgeInvocation(args []string) (string, string, error) { |
| 211 | payload := strings.TrimSpace(strings.Join(args, " ")) |
| 212 | |
| 213 | if strings.HasPrefix(payload, "{") { |
| 214 | var raw map[string]json.RawMessage |
| 215 | if err := json.Unmarshal([]byte(payload), &raw); err != nil { |
| 216 | return "", "", fmt.Errorf( |
| 217 | `parse envelope: %w. Expected {"cmd":"search","args":{"query":"..."}}`, err, |
| 218 | ) |
| 219 | } |
| 220 | var cmdStr string |
| 221 | if rc, ok := raw["cmd"]; ok { |
| 222 | _ = json.Unmarshal(rc, &cmdStr) |
| 223 | } |
| 224 | canon := canonicalKnowledgeCmd(cmdStr) |
| 225 | if canon == "" { |
| 226 | return "", "", fmt.Errorf("missing or unknown cmd %q (valid: search|get|toc|list)", cmdStr) |
| 227 | } |
| 228 | var inner string |
| 229 | if rargs, ok := raw["args"]; ok && len(rargs) > 0 { |
| 230 | inner = string(rargs) |
| 231 | } else { |
| 232 | delete(raw, "cmd") |
| 233 | b, _ := json.Marshal(raw) |
| 234 | inner = string(b) |
| 235 | } |
| 236 | return canon, inner, nil |
| 237 | } |
| 238 | |
| 239 | canon := canonicalKnowledgeCmd(args[0]) |
| 240 | if canon == "" { |
| 241 | return "", "", fmt.Errorf( |
| 242 | `expected JSON envelope or subcommand; got %q. Example: {"cmd":"search","args":{"query":"..."}}`, |
| 243 | args[0], |
| 244 | ) |
| 245 | } |
| 246 | // The agent flattener delivers the {cmd,args} envelope as "--flag value" |
| 247 | // pairs; numeric fields (top_k, offset) must come back as numbers, and a |
| 248 | // bare positional maps onto the subcommand's primary field. |
| 249 | primary := map[string]string{"search": "query", "get": "source", "toc": "prefix"}[canon] |
| 250 | return canon, argvInner(args[1:], primary, nil, map[string]bool{"top_k": true, "offset": true}), nil |
| 251 | } |
| 252 | |
| 253 | // canonicalKnowledgeCmd folds aliases into the four canonical names. |
| 254 | func canonicalKnowledgeCmd(s string) string { |
no test coverage detected