返回 DeepSeek-Reasonix
forget.go
根目录 / internal / memory / forget.go
1 package memory
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "strings"
8
9 "reasonix/internal/tool"
10 )
11
12 // forgetTool deletes a saved memory the model judges wrong or stale. Like
13 // rememberTool it is stateful (bound to one project's Store), so boot constructs
14 // it and adds it to the registry.
15 type forgetTool struct{ store Store }
16
17 // NewForgetTool returns the `forget` tool bound to store.
18 func NewForgetTool(store Store) tool.Tool { return forgetTool{store: store} }
19
20 func (forgetTool) Name() string { return "forget" }
21
22 func (forgetTool) Description() string {
23 return "Delete a saved memory by name when it is wrong, stale, or superseded, so it stops loading into future sessions. " +
24 "Use the stable project/<name>.md or global/<name>.md reference returned by memory search/read/list. " +
25 "Prefer updating a memory with `remember` (reuse its name) over forget-then-recreate; reach for forget only when the fact should no longer exist at all."
26 }
27
28 func (forgetTool) Schema() json.RawMessage {
29 return json.RawMessage(`{
30 "type": "object",
31 "properties": {
32 "name": {"type": "string", "description": "Stable memory id, project/<name>.md or global/<name>.md reference, or legacy slug of the memory to archive."}
33 },
34 "required": ["name"]
35 }`)
36 }
37
38 func (t forgetTool) Execute(ctx context.Context, args json.RawMessage) (string, error) {
39 var in struct {
40 Name string `json:"name"`
41 }
42 if err := json.Unmarshal(args, &in); err != nil {
43 return "", fmt.Errorf("invalid arguments: %w", err)
44 }
45 if in.Name == "" {
46 return "", fmt.Errorf("name is required")
47 }
48 memory, found := t.store.Read(in.Name)
49 archive, err := t.store.Archive(in.Name)
50 if err != nil {
51 return "", err
52 }
53 if q, ok := QueueFromContext(ctx); ok {
54 name := slug(strings.TrimSuffix(in.Name, ".md"))
55 if found {
56 name = memory.Name
57 }
58 q.QueueMemory("Forgot memory \"" + name + "\" — disregard its loaded guidance and background-index entry for the rest of this session.")
59 }
60 if archive != "" {
61 if found {
62 return fmt.Sprintf("Forgot memory %q (it no longer applies and will not load in future sessions; archived from %s).", in.Name, providerMemoryReference(memory)), nil
63 }
64 return fmt.Sprintf("Forgot memory %q (it no longer applies and will not load in future sessions; archived).", in.Name), nil
65 }
66 return fmt.Sprintf("Forgot memory %q (it no longer applies and will not load in future sessions).", in.Name), nil
67 }
68
69 func (forgetTool) ReadOnly() bool { return false }
70
70 lines GO