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

Function parseMemoryInvocation

cli/plugins/builtin_memory.go:294–337  ·  view source on GitHub ↗

parseMemoryInvocation accepts the JSON envelope {"cmd":..,"args":{..}}, flat JSON {"cmd":..,"content":..}, and the flattened argv form the agent tool sanitizer may produce. Returns the canonical (cmd, innerJSON).

(args []string)

Source from the content-addressed store, hash-verified

292// flat JSON {"cmd":..,"content":..}, and the flattened argv form the agent
293// tool sanitizer may produce. Returns the canonical (cmd, innerJSON).
294func parseMemoryInvocation(args []string) (string, string, error) {
295 payload := strings.TrimSpace(strings.Join(args, " "))
296
297 if strings.HasPrefix(payload, "{") {
298 var raw map[string]json.RawMessage
299 if err := json.Unmarshal([]byte(payload), &raw); err != nil {
300 return "", "", fmt.Errorf(
301 `parse envelope: %w. Expected {"cmd":"remember","args":{"content":"..."}}`, err,
302 )
303 }
304 var cmdStr string
305 if rc, ok := raw["cmd"]; ok {
306 _ = json.Unmarshal(rc, &cmdStr)
307 }
308 canon := canonicalMemoryCmd(cmdStr)
309 if canon == "" {
310 return "", "", fmt.Errorf(
311 "missing or unknown cmd %q (valid: remember|profile|forget|recall|neighbors|map)", cmdStr,
312 )
313 }
314 var inner string
315 if rargs, ok := raw["args"]; ok && len(rargs) > 0 {
316 inner = string(rargs)
317 } else {
318 delete(raw, "cmd")
319 b, _ := json.Marshal(raw)
320 inner = string(b)
321 }
322 return canon, inner, nil
323 }
324
325 canon := canonicalMemoryCmd(args[0])
326 if canon == "" {
327 return "", "", fmt.Errorf(
328 `expected JSON envelope or subcommand; got %q. Example: {"cmd":"remember","args":{"content":"..."}}`,
329 args[0],
330 )
331 }
332 inner, err := memoryFlagsToJSON(args[1:])
333 if err != nil {
334 return "", "", err
335 }
336 return canon, inner, nil
337}
338
339// canonicalMemoryCmd folds aliases into the four canonical names.
340func canonicalMemoryCmd(s string) string {

Calls 3

canonicalMemoryCmdFunction · 0.85
memoryFlagsToJSONFunction · 0.85
ErrorfMethod · 0.80