MCPcopy Create free account
hub / github.com/diillson/chatcli / parseContextInvocation

Function parseContextInvocation

cli/plugins/builtin_context.go:415–448  ·  view source on GitHub ↗

parseContextInvocation accepts the {cmd, args} envelope, a flat object whose shape implies the subcommand, or ` --flag value` argv.

(args []string)

Source from the content-addressed store, hash-verified

413// parseContextInvocation accepts the {cmd, args} envelope, a flat object whose
414// shape implies the subcommand, or `<sub> --flag value` argv.
415func parseContextInvocation(args []string) (string, map[string]json.RawMessage, error) {
416 payload := strings.TrimSpace(strings.Join(args, " "))
417 if strings.HasPrefix(payload, "{") {
418 var top map[string]json.RawMessage
419 if err := json.Unmarshal([]byte(payload), &top); err != nil {
420 return "", nil, fmt.Errorf("malformed JSON args: %w", err)
421 }
422 cmd := canonicalContextCmd(rawJSONString(top, "cmd", "command", "action"))
423 inner := top
424 if rargs, ok := top["args"]; ok && len(rargs) > 0 {
425 var im map[string]json.RawMessage
426 if err := json.Unmarshal(rargs, &im); err == nil {
427 inner = im
428 }
429 }
430 if cmd == "" {
431 cmd = inferContextCmd(inner)
432 }
433 if cmd == "" {
434 return "", nil, fmt.Errorf("missing or unknown cmd (valid: create|update|attach|detach|list|show|inspect|merge|status|export|import|delete|metrics)")
435 }
436 return cmd, inner, nil
437 }
438
439 // argv form: first token is the subcommand.
440 if len(args) == 0 {
441 return "", nil, fmt.Errorf("no subcommand")
442 }
443 cmd := canonicalContextCmd(strings.TrimSpace(args[0]))
444 if cmd == "" {
445 return "", nil, fmt.Errorf("unknown subcommand %q", args[0])
446 }
447 return cmd, contextArgvToMap(args[1:]), nil
448}
449
450// canonicalContextCmd normalizes subcommand aliases.
451func canonicalContextCmd(s string) string {

Callers 5

TestContextRagTopKFunction · 0.85
TestContextStringSliceFunction · 0.85
TestContextArgvFormFunction · 0.85
contextSubcommandFunction · 0.85
ExecuteWithStreamMethod · 0.85

Calls 5

canonicalContextCmdFunction · 0.85
rawJSONStringFunction · 0.85
inferContextCmdFunction · 0.85
contextArgvToMapFunction · 0.85
ErrorfMethod · 0.80

Tested by 3

TestContextRagTopKFunction · 0.68
TestContextStringSliceFunction · 0.68
TestContextArgvFormFunction · 0.68