feat(server): add brand kit management and project binding

Add a brand kit domain with memory and postgres stores, a BrandKitService
for CRUD and resolution, and REST endpoints to list, upsert, and delete
brand kits. Projects can bind a brand kit whose resolved context and
reference images feed into the agent's long-term memory and design prompts.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-10 20:58:55 +08:00
parent 3653474355
commit 789f51b29e
38 changed files with 2165 additions and 135 deletions
@@ -0,0 +1,105 @@
package memory
import (
"context"
"sort"
"sync"
"img_infinite_canvas/internal/domain/brandkit"
)
type BrandKitStore struct {
mu sync.RWMutex
kits map[string]brandkit.Kit
}
func NewBrandKitStore() *BrandKitStore {
return &BrandKitStore{kits: make(map[string]brandkit.Kit)}
}
func (s *BrandKitStore) List(ctx context.Context, userID string) ([]brandkit.Kit, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
s.mu.RLock()
defer s.mu.RUnlock()
items := make([]brandkit.Kit, 0)
for _, kit := range s.kits {
if kit.UserID == userID {
items = append(items, brandkit.Clone(kit))
}
}
sort.SliceStable(items, func(i, j int) bool {
if items[i].UpdatedAt.Equal(items[j].UpdatedAt) {
return items[i].ID < items[j].ID
}
return items[i].UpdatedAt.After(items[j].UpdatedAt)
})
return items, nil
}
func (s *BrandKitStore) Get(ctx context.Context, userID string, id string) (brandkit.Kit, error) {
if err := ctx.Err(); err != nil {
return brandkit.Kit{}, err
}
s.mu.RLock()
defer s.mu.RUnlock()
kit, ok := s.kits[id]
if !ok || kit.UserID != userID {
return brandkit.Kit{}, brandkit.ErrNotFound
}
return brandkit.Clone(kit), nil
}
func (s *BrandKitStore) GetDefault(ctx context.Context, userID string) (brandkit.Kit, error) {
if err := ctx.Err(); err != nil {
return brandkit.Kit{}, err
}
s.mu.RLock()
defer s.mu.RUnlock()
for _, kit := range s.kits {
if kit.UserID == userID && kit.IsDefault {
return brandkit.Clone(kit), nil
}
}
return brandkit.Kit{}, brandkit.ErrNotFound
}
func (s *BrandKitStore) Upsert(ctx context.Context, kit brandkit.Kit) (brandkit.Kit, error) {
if err := ctx.Err(); err != nil {
return brandkit.Kit{}, err
}
s.mu.Lock()
defer s.mu.Unlock()
if existing, ok := s.kits[kit.ID]; ok && existing.UserID != kit.UserID {
return brandkit.Kit{}, brandkit.ErrConflict
}
if kit.IsDefault {
for id, candidate := range s.kits {
if candidate.UserID == kit.UserID && candidate.IsDefault && id != kit.ID {
candidate.IsDefault = false
s.kits[id] = candidate
}
}
}
s.kits[kit.ID] = brandkit.Clone(kit)
return brandkit.Clone(kit), nil
}
func (s *BrandKitStore) Delete(ctx context.Context, userID string, id string) error {
if err := ctx.Err(); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
kit, ok := s.kits[id]
if !ok || kit.UserID != userID {
return brandkit.ErrNotFound
}
delete(s.kits, id)
return nil
}
func (s *BrandKitStore) Close() error {
return nil
}
@@ -38,13 +38,14 @@ func (r *ProjectRepository) List(ctx context.Context) ([]design.ProjectSummary,
continue
}
items = append(items, design.ProjectSummary{
ID: project.ID,
UserID: project.UserID,
Title: project.Title,
Brief: project.Brief,
Status: project.Status,
Thumbnail: project.Thumbnail,
UpdatedAt: project.UpdatedAt,
ID: project.ID,
UserID: project.UserID,
Title: project.Title,
Brief: project.Brief,
Status: project.Status,
Thumbnail: project.Thumbnail,
BrandKitID: project.BrandKitID,
UpdatedAt: project.UpdatedAt,
})
}
return items, nil
@@ -207,6 +208,7 @@ func repositoryUserID(ctx context.Context, userID string) string {
func cloneProject(project design.Project) design.Project {
project.UserID = repositoryUserID(context.Background(), project.UserID)
project.BrandImages = append([]string(nil), project.BrandImages...)
project.Nodes = append([]design.Node(nil), project.Nodes...)
project.Connections = append([]design.Connection(nil), project.Connections...)
project.Messages = append([]design.Message(nil), project.Messages...)