|
| 1 | +// Package yaad provides the public Go SDK for embedding Yaad memory |
| 2 | +// into any Go application or coding agent. |
| 3 | +// |
| 4 | +// Usage: |
| 5 | +// |
| 6 | +// import "github.com/GrayCodeAI/yaad/sdk/go/yaad" |
| 7 | +// |
| 8 | +// mem, _ := yaad.Open(".yaad/yaad.db") |
| 9 | +// defer mem.Close() |
| 10 | +// |
| 11 | +// node, _ := mem.Remember("Use jose for JWT", yaad.Convention) |
| 12 | +// results, _ := mem.Recall("auth middleware") |
| 13 | +// context, _ := mem.Context("") |
| 14 | +// model, _ := mem.MentalModel("") |
| 15 | +package yaad |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/GrayCodeAI/yaad/internal/engine" |
| 19 | + "github.com/GrayCodeAI/yaad/internal/mental" |
| 20 | + "github.com/GrayCodeAI/yaad/internal/storage" |
| 21 | +) |
| 22 | + |
| 23 | +// Memory types. |
| 24 | +const ( |
| 25 | + Convention = "convention" |
| 26 | + Decision = "decision" |
| 27 | + Bug = "bug" |
| 28 | + Spec = "spec" |
| 29 | + Task = "task" |
| 30 | + Skill = "skill" |
| 31 | + Preference = "preference" |
| 32 | +) |
| 33 | + |
| 34 | +// Node is a memory node in the Yaad graph. |
| 35 | +type Node = storage.Node |
| 36 | + |
| 37 | +// Edge is a relationship between two nodes. |
| 38 | +type Edge = storage.Edge |
| 39 | + |
| 40 | +// RecallResult holds search results with nodes and edges. |
| 41 | +type RecallResult = engine.RecallResult |
| 42 | + |
| 43 | +// MentalModel is an auto-generated project summary. |
| 44 | +type MentalModel = mental.Model |
| 45 | + |
| 46 | +// Memory is the main Yaad SDK handle. |
| 47 | +type Memory struct { |
| 48 | + eng *engine.Engine |
| 49 | + store *storage.Store |
| 50 | +} |
| 51 | + |
| 52 | +// Open opens a Yaad database at the given path. |
| 53 | +// Creates the database and schema if it doesn't exist. |
| 54 | +func Open(dbPath string) (*Memory, error) { |
| 55 | + store, err := storage.NewStore(dbPath) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + return &Memory{eng: engine.New(store), store: store}, nil |
| 60 | +} |
| 61 | + |
| 62 | +// Close closes the database connection. |
| 63 | +func (m *Memory) Close() error { |
| 64 | + return m.store.Close() |
| 65 | +} |
| 66 | + |
| 67 | +// Remember stores a memory node with auto entity extraction, |
| 68 | +// dedup, temporal linking, and conflict resolution. |
| 69 | +func (m *Memory) Remember(content string, nodeType string, opts ...RememberOption) (*Node, error) { |
| 70 | + in := engine.RememberInput{ |
| 71 | + Content: content, |
| 72 | + Type: nodeType, |
| 73 | + Scope: "project", |
| 74 | + } |
| 75 | + for _, opt := range opts { |
| 76 | + opt(&in) |
| 77 | + } |
| 78 | + return m.eng.Remember(in) |
| 79 | +} |
| 80 | + |
| 81 | +// RememberOption configures a Remember call. |
| 82 | +type RememberOption func(*engine.RememberInput) |
| 83 | + |
| 84 | +// WithTags sets tags on the memory. |
| 85 | +func WithTags(tags string) RememberOption { |
| 86 | + return func(in *engine.RememberInput) { in.Tags = tags } |
| 87 | +} |
| 88 | + |
| 89 | +// WithProject sets the project scope. |
| 90 | +func WithProject(project string) RememberOption { |
| 91 | + return func(in *engine.RememberInput) { in.Project = project } |
| 92 | +} |
| 93 | + |
| 94 | +// WithSummary sets a short summary. |
| 95 | +func WithSummary(summary string) RememberOption { |
| 96 | + return func(in *engine.RememberInput) { in.Summary = summary } |
| 97 | +} |
| 98 | + |
| 99 | +// WithSession sets the session ID. |
| 100 | +func WithSession(session string) RememberOption { |
| 101 | + return func(in *engine.RememberInput) { in.Session = session } |
| 102 | +} |
| 103 | + |
| 104 | +// WithAgent sets the agent name. |
| 105 | +func WithAgent(agent string) RememberOption { |
| 106 | + return func(in *engine.RememberInput) { in.Agent = agent } |
| 107 | +} |
| 108 | + |
| 109 | +// Recall performs graph-aware search: BM25 + vector + graph + temporal. |
| 110 | +func (m *Memory) Recall(query string, opts ...RecallOption) (*RecallResult, error) { |
| 111 | + ro := engine.RecallOpts{Query: query, Depth: 2, Limit: 10} |
| 112 | + for _, opt := range opts { |
| 113 | + opt(&ro) |
| 114 | + } |
| 115 | + return m.eng.Recall(ro) |
| 116 | +} |
| 117 | + |
| 118 | +// RecallOption configures a Recall call. |
| 119 | +type RecallOption func(*engine.RecallOpts) |
| 120 | + |
| 121 | +// WithDepth sets the graph expansion depth. |
| 122 | +func WithDepth(depth int) RecallOption { |
| 123 | + return func(ro *engine.RecallOpts) { ro.Depth = depth } |
| 124 | +} |
| 125 | + |
| 126 | +// WithLimit sets the max results. |
| 127 | +func WithLimit(limit int) RecallOption { |
| 128 | + return func(ro *engine.RecallOpts) { ro.Limit = limit } |
| 129 | +} |
| 130 | + |
| 131 | +// WithType filters by node type. |
| 132 | +func WithType(nodeType string) RecallOption { |
| 133 | + return func(ro *engine.RecallOpts) { ro.Type = nodeType } |
| 134 | +} |
| 135 | + |
| 136 | +// Context returns the hot-tier subgraph for session start injection. |
| 137 | +func (m *Memory) Context(project string) (*RecallResult, error) { |
| 138 | + return m.eng.Context(project) |
| 139 | +} |
| 140 | + |
| 141 | +// Forget archives a memory node. |
| 142 | +func (m *Memory) Forget(id string) error { |
| 143 | + return m.eng.Forget(id) |
| 144 | +} |
| 145 | + |
| 146 | +// Compact merges low-confidence memories to keep the graph lean. |
| 147 | +func (m *Memory) Compact(project string) (int, error) { |
| 148 | + return m.eng.Compact(project) |
| 149 | +} |
| 150 | + |
| 151 | +// MentalModel generates an auto-evolving project summary. |
| 152 | +func (m *Memory) MentalModel(project string) (*MentalModel, error) { |
| 153 | + return m.eng.MentalModel(project) |
| 154 | +} |
| 155 | + |
| 156 | +// Feedback applies user feedback to a memory node. |
| 157 | +func (m *Memory) Approve(id string) error { |
| 158 | + return m.eng.Feedback(id, "approve", "") |
| 159 | +} |
| 160 | + |
| 161 | +// Edit updates a memory node's content. |
| 162 | +func (m *Memory) Edit(id string, newContent string) error { |
| 163 | + return m.eng.Feedback(id, "edit", newContent) |
| 164 | +} |
| 165 | + |
| 166 | +// Discard archives a memory node via feedback. |
| 167 | +func (m *Memory) Discard(id string) error { |
| 168 | + return m.eng.Feedback(id, "discard", "") |
| 169 | +} |
0 commit comments