|
| 1 | +// Package profile implements auto-maintained user/project profiles. |
| 2 | +// Inspired by SuperMemory's profile system: static facts + dynamic recent context. |
| 3 | +// |
| 4 | +// A profile is automatically built from the memory graph: |
| 5 | +// - Static: long-term facts (conventions, decisions, preferences) — stable over weeks |
| 6 | +// - Dynamic: recent activity (active tasks, recent bugs, last session) — changes daily |
| 7 | +// |
| 8 | +// One call, ~50ms. Inject into system prompt and the agent instantly knows the project. |
| 9 | +package profile |
| 10 | + |
| 11 | +import ( |
| 12 | + "fmt" |
| 13 | + "sort" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/GrayCodeAI/yaad/internal/storage" |
| 18 | +) |
| 19 | + |
| 20 | +// Profile is an auto-maintained project/user profile. |
| 21 | +type Profile struct { |
| 22 | + Project string `json:"project"` |
| 23 | + Static []string `json:"static"` // long-term facts (conventions, decisions, preferences) |
| 24 | + Dynamic []string `json:"dynamic"` // recent activity (tasks, bugs, last session) |
| 25 | + Stack []string `json:"stack"` // detected tech stack |
| 26 | + Summary string `json:"summary"` // one-line project summary |
| 27 | +} |
| 28 | + |
| 29 | +// Build generates a profile from the memory graph. No LLM needed. |
| 30 | +func Build(store *storage.Store, project string) (*Profile, error) { |
| 31 | + p := &Profile{Project: project} |
| 32 | + |
| 33 | + // Static: high-confidence conventions, decisions, preferences |
| 34 | + for _, typ := range []string{"convention", "decision", "preference"} { |
| 35 | + nodes, _ := store.ListNodes(storage.NodeFilter{ |
| 36 | + Type: typ, Project: project, MinConfidence: 0.5, |
| 37 | + }) |
| 38 | + for _, n := range nodes { |
| 39 | + p.Static = append(p.Static, n.Content) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Dynamic: recent tasks, bugs, sessions (last 7 days) |
| 44 | + cutoff := time.Now().AddDate(0, 0, -7) |
| 45 | + for _, typ := range []string{"task", "bug", "session"} { |
| 46 | + nodes, _ := store.ListNodes(storage.NodeFilter{ |
| 47 | + Type: typ, Project: project, MinConfidence: 0.1, |
| 48 | + }) |
| 49 | + for _, n := range nodes { |
| 50 | + if n.CreatedAt.After(cutoff) || n.UpdatedAt.After(cutoff) { |
| 51 | + p.Dynamic = append(p.Dynamic, fmt.Sprintf("[%s] %s", n.Type, n.Content)) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Sort dynamic by recency (most recent first) |
| 57 | + // Already in insertion order which is roughly chronological |
| 58 | + |
| 59 | + // Stack: extract from entity nodes |
| 60 | + entities, _ := store.ListNodes(storage.NodeFilter{ |
| 61 | + Type: "entity", Project: project, |
| 62 | + }) |
| 63 | + for _, n := range entities { |
| 64 | + if isTech(n.Content) { |
| 65 | + p.Stack = append(p.Stack, n.Content) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Deduplicate stack |
| 70 | + p.Stack = dedup(p.Stack) |
| 71 | + |
| 72 | + // Summary |
| 73 | + parts := []string{} |
| 74 | + if len(p.Stack) > 0 { |
| 75 | + parts = append(parts, "Stack: "+strings.Join(p.Stack[:min(len(p.Stack), 5)], ", ")) |
| 76 | + } |
| 77 | + parts = append(parts, fmt.Sprintf("%d facts", len(p.Static))) |
| 78 | + if len(p.Dynamic) > 0 { |
| 79 | + parts = append(parts, fmt.Sprintf("%d recent items", len(p.Dynamic))) |
| 80 | + } |
| 81 | + p.Summary = strings.Join(parts, " · ") |
| 82 | + |
| 83 | + return p, nil |
| 84 | +} |
| 85 | + |
| 86 | +// Format returns the profile as markdown for agent injection. |
| 87 | +func (p *Profile) Format() string { |
| 88 | + var sb strings.Builder |
| 89 | + sb.WriteString("## User Profile\n\n") |
| 90 | + |
| 91 | + if p.Summary != "" { |
| 92 | + sb.WriteString("**" + p.Summary + "**\n\n") |
| 93 | + } |
| 94 | + |
| 95 | + if len(p.Static) > 0 { |
| 96 | + sb.WriteString("### What I Know (stable)\n") |
| 97 | + for _, s := range p.Static[:min(len(p.Static), 10)] { |
| 98 | + sb.WriteString("- " + s + "\n") |
| 99 | + } |
| 100 | + sb.WriteString("\n") |
| 101 | + } |
| 102 | + |
| 103 | + if len(p.Dynamic) > 0 { |
| 104 | + sb.WriteString("### What's Happening (recent)\n") |
| 105 | + for _, d := range p.Dynamic[:min(len(p.Dynamic), 5)] { |
| 106 | + sb.WriteString("- " + d + "\n") |
| 107 | + } |
| 108 | + sb.WriteString("\n") |
| 109 | + } |
| 110 | + |
| 111 | + return sb.String() |
| 112 | +} |
| 113 | + |
| 114 | +// Merge combines two profiles (e.g., project + global). |
| 115 | +func Merge(a, b *Profile) *Profile { |
| 116 | + return &Profile{ |
| 117 | + Project: a.Project, |
| 118 | + Static: dedup(append(a.Static, b.Static...)), |
| 119 | + Dynamic: append(a.Dynamic, b.Dynamic...), |
| 120 | + Stack: dedup(append(a.Stack, b.Stack...)), |
| 121 | + Summary: a.Summary, |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func isTech(name string) bool { |
| 126 | + techs := map[string]bool{ |
| 127 | + "typescript": true, "javascript": true, "python": true, "go": true, "rust": true, |
| 128 | + "react": true, "vue": true, "next": true, "node": true, "deno": true, "bun": true, |
| 129 | + "postgresql": true, "mysql": true, "sqlite": true, "redis": true, "nats": true, |
| 130 | + "docker": true, "kubernetes": true, "aws": true, "gcp": true, "azure": true, |
| 131 | + "jose": true, "express": true, "fastify": true, "gin": true, "fiber": true, |
| 132 | + "tailwind": true, "prisma": true, "drizzle": true, "trpc": true, "graphql": true, |
| 133 | + } |
| 134 | + return techs[strings.ToLower(name)] |
| 135 | +} |
| 136 | + |
| 137 | +func dedup(items []string) []string { |
| 138 | + seen := map[string]bool{} |
| 139 | + var out []string |
| 140 | + for _, item := range items { |
| 141 | + lower := strings.ToLower(item) |
| 142 | + if !seen[lower] { |
| 143 | + seen[lower] = true |
| 144 | + out = append(out, item) |
| 145 | + } |
| 146 | + } |
| 147 | + return out |
| 148 | +} |
| 149 | + |
| 150 | +func min(a, b int) int { |
| 151 | + if a < b { |
| 152 | + return a |
| 153 | + } |
| 154 | + return b |
| 155 | +} |
| 156 | + |
| 157 | +// ensure sort is used |
| 158 | +var _ = sort.Strings |
0 commit comments