build: 内置 norma 模块,构建不再依赖上游仓库

把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到
third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或
改写 tag 都不再影响本仓库构建。

- 已验证 GOPROXY=off 下可完整编译并打出发行包
- go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希)
- 升级方法与授权注意事项见 third_party/README.md
This commit is contained in:
wangdefa committed 2026-10-20 21:10:41 -07:00
1 parent a819fa3230
commit f782b612ec
213 files changed
+37401 -2

No files matched your search

+91
View File
@@ -0,0 +1,91 @@
package memory
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/Autumn-27/norma/permission"
"github.com/Autumn-27/norma/tool"
)
// Tools returns the RecallMemory and RecordMemory tools bound to a store.
func Tools(s *Store) []tool.CoreTool {
return []tool.CoreTool{RecallTool(s), RecordTool(s)}
}
// RecallTool lets the agent read memories relevant to a query.
func RecallTool(s *Store) tool.CoreTool {
return tool.Build(tool.Spec{
Name: "RecallMemory",
Description: "Searches your long-term memory for facts relevant to a query and returns their full content. Use it when prior context (user preferences, project state, past decisions) would help.",
Schema: map[string]any{
"type": "object",
"properties": map[string]any{"query": map[string]any{"type": "string", "description": "What to recall."}},
"required": []any{"query"},
},
ReadOnly: func(json.RawMessage) bool { return true },
Concurrent: func(json.RawMessage) bool { return true },
Permissions: func(context.Context, json.RawMessage, permission.Context) permission.Decision {
return permission.Allowed()
},
Run: func(_ context.Context, input json.RawMessage, _ *tool.ToolContext) (tool.Result, error) {
var in struct {
Query string `json:"query"`
}
if err := json.Unmarshal(input, &in); err != nil {
return tool.Result{}, err
}
mems := s.Relevant(in.Query, 5)
if len(mems) == 0 {
return tool.Text("No relevant memories found."), nil
}
return tool.Text(RenderForInjection(mems, time.Now())), nil
},
})
}
// RecordTool lets the agent persist a durable fact to memory.
func RecordTool(s *Store) tool.CoreTool {
return tool.Build(tool.Spec{
Name: "RecordMemory",
Description: "Saves a durable fact to long-term memory so it persists across sessions. Record stable facts (user preferences, project constraints, decisions) — not transient conversation detail.",
Prompt: "type is one of: user (about the user), feedback (how you should work), project (ongoing work/constraints), reference (external resources). Give a short name and a one-line description.",
Schema: map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{"type": "string", "description": "Short title for the memory."},
"description": map[string]any{"type": "string", "description": "One-line summary."},
"type": map[string]any{"type": "string", "description": "user | feedback | project | reference"},
"content": map[string]any{"type": "string", "description": "The fact to remember (markdown)."},
},
"required": []any{"name", "content"},
},
ReadOnly: func(json.RawMessage) bool { return false },
Concurrent: func(json.RawMessage) bool { return false },
Permissions: func(context.Context, json.RawMessage, permission.Context) permission.Decision {
return permission.Allowed()
},
Run: func(_ context.Context, input json.RawMessage, _ *tool.ToolContext) (tool.Result, error) {
var in struct {
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Content string `json:"content"`
}
if err := json.Unmarshal(input, &in); err != nil {
return tool.Result{}, err
}
if strings.TrimSpace(in.Name) == "" || strings.TrimSpace(in.Content) == "" {
return tool.Errorf("Error: name and content are required"), nil
}
m := Memory{Name: in.Name, Description: in.Description, Type: Type(in.Type), Content: in.Content}
if err := s.Save(m); err != nil {
return tool.Errorf("Error saving memory: " + err.Error()), nil
}
return tool.Text(fmt.Sprintf("Saved memory %q.", in.Name)), nil
},
})
}