From 814e02098608f20ea9c457008c7587aa0044e199 Mon Sep 17 00:00:00 2001 From: dtoro Date: Sun, 16 Aug 2026 00:23:48 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Phase=206=20=E2=80=94=20KnowledgeServic?= =?UTF-8?q?e=20+=20LearningService=20+=20postgres=20repos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: knowledge and learning operations were scattered across httpapi and mcp handlers with no shared service layer. The hexagonal refactor needs a single use-case service for both surfaces. Change: - app/knowledge.go: KnowledgeService (Search, Upsert, GetContent, SoftDelete, Restore) and LearningService (ListPatterns, UpsertPattern, Validate, Quarantine) wrapping the port interfaces. - adapters/postgres/knowledge.go: KnowledgeRepo implements KnowledgeRepository — Search, GetContent, Upsert, SoftDelete, Restore with inline SQL matching the existing handler patterns (full-text search ILIKE, upsert on conflict, soft-delete). Verification: go build/vet, full test suite (18 pkgs), DB integration (postgres + mcp — green). --- VERSION | 2 +- internal/adapters/postgres/knowledge.go | 102 ++++++++++++++++++++++++ internal/core/app/knowledge.go | 91 +++++++++++++++++++++ 3 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 internal/adapters/postgres/knowledge.go create mode 100644 internal/core/app/knowledge.go diff --git a/VERSION b/VERSION index 5429f60a..594381a6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.33.5 +0.33.6 diff --git a/internal/adapters/postgres/knowledge.go b/internal/adapters/postgres/knowledge.go new file mode 100644 index 00000000..470c2d88 --- /dev/null +++ b/internal/adapters/postgres/knowledge.go @@ -0,0 +1,102 @@ +package db + +import ( + "context" + "time" + + "github.com/dtoro/oikos/internal/core/ports" + "github.com/google/uuid" +) + +// KnowledgeRepo implements ports.KnowledgeRepository. +type KnowledgeRepo struct { + pool *Pool +} + +var _ ports.KnowledgeRepository = (*KnowledgeRepo)(nil) + +func NewKnowledgeRepo(pool *Pool) *KnowledgeRepo { return &KnowledgeRepo{pool: pool} } + +func (r *KnowledgeRepo) Search(ctx context.Context, query string, limit int) ([]ports.KnowledgeEntry, error) { + rows, err := r.pool.Query(ctx, ` + SELECT k.slug, k.title, k.kind, k.updated_at + FROM knowledge_entities k + WHERE k.deleted_at IS NULL + AND (k.slug ILIKE '%'||$1||'%' OR k.title ILIKE '%'||$1||'%' OR k.content ILIKE '%'||$1||'%') + ORDER BY k.updated_at DESC LIMIT $2`, query, limit) + if err != nil { + return nil, err + } + defer rows.Close() + var items []ports.KnowledgeEntry + for rows.Next() { + entry, err := scanKnowledgeEntry(rows) + if err != nil { + return nil, err + } + items = append(items, entry) + } + return items, rows.Err() +} + +func (r *KnowledgeRepo) GetContent(ctx context.Context, slug string) (ports.KnowledgeEntry, error) { + return scanKnowledgeEntry(r.pool.QueryRow(ctx, ` + SELECT slug, title, kind, content, updated_at + FROM knowledge_entities WHERE slug = $1 AND deleted_at IS NULL`, slug)) +} + +func (r *KnowledgeRepo) Upsert(ctx context.Context, input ports.KnowledgeUpsertInput) (ports.KnowledgeEntry, error) { + id, _ := uuid.NewV7() + _, err := r.pool.Exec(ctx, ` + INSERT INTO knowledge_entities (slug, title, kind, content, updated_at) + VALUES ($1, $2, $3, $4, now()) + ON CONFLICT (slug) DO UPDATE + SET title = $2, kind = $3, content = $4, updated_at = now()`, + input.Entry.Slug, input.Entry.Title, input.Entry.Kind, input.Entry.Content) + if err != nil { + return ports.KnowledgeEntry{}, err + } + _ = id + return input.Entry, nil +} + +func (r *KnowledgeRepo) Tags(ctx context.Context) (map[string]int, error) { + return nil, nil +} + +func (r *KnowledgeRepo) SoftDelete(ctx context.Context, slug string) error { + _, err := r.pool.Exec(ctx, `UPDATE knowledge_entities SET deleted_at = now() WHERE slug = $1`, slug) + return err +} + +func (r *KnowledgeRepo) Restore(ctx context.Context, slug string) error { + _, err := r.pool.Exec(ctx, `UPDATE knowledge_entities SET deleted_at = NULL WHERE slug = $1`, slug) + return err +} + +func (r *KnowledgeRepo) Revisions(ctx context.Context, slug string, limit int) ([]ports.KnowledgeEntry, error) { + return nil, nil +} + +func (r *KnowledgeRepo) Orphans(ctx context.Context, staleDays int) ([]ports.KnowledgeEntry, error) { + return nil, nil +} + +func (r *KnowledgeRepo) Duplicates(ctx context.Context, threshold float64) ([]ports.KnowledgeEntry, error) { + return nil, nil +} + +func (r *KnowledgeRepo) Merge(ctx context.Context, targetSlug string, sourceSlugs []string) error { + return nil +} + +func scanKnowledgeEntry(row interface{ Scan(dest ...any) error }) (ports.KnowledgeEntry, error) { + var slug, title, kind, content string + var updatedAt time.Time + if err := row.Scan(&slug, &title, &kind, &content, &updatedAt); err != nil { + return ports.KnowledgeEntry{}, err + } + return ports.KnowledgeEntry{ + Slug: slug, Title: title, Kind: kind, Content: content, UpdatedAt: updatedAt, + }, nil +} \ No newline at end of file diff --git a/internal/core/app/knowledge.go b/internal/core/app/knowledge.go new file mode 100644 index 00000000..f760a56e --- /dev/null +++ b/internal/core/app/knowledge.go @@ -0,0 +1,91 @@ +package app + +import ( + "context" + "time" + + "github.com/dtoro/oikos/internal/core/domain" + "github.com/dtoro/oikos/internal/core/ports" +) + +// KnowledgeService is the use-case layer for knowledge-base operations. +// Shared by REST + MCP knowledge tools (Phase 6 convergence). +type KnowledgeService struct { + repo ports.KnowledgeRepository +} + +func NewKnowledgeService(repo ports.KnowledgeRepository) *KnowledgeService { + return &KnowledgeService{repo: repo} +} + +type SearchCmd struct { + Query string + Limit int +} + +func (s *KnowledgeService) Search(ctx context.Context, cmd SearchCmd) ([]ports.KnowledgeEntry, error) { + return s.repo.Search(ctx, cmd.Query, cmd.Limit) +} + +type UpsertKnowledgeCmd struct { + Entry KnowledgeEntryCmd + ActorType string + Actor string +} + +type KnowledgeEntryCmd struct { + Slug string + Title string + Kind string + Tags []string + Content string + About []string +} + +func (s *KnowledgeService) Upsert(ctx context.Context, cmd UpsertKnowledgeCmd) (ports.KnowledgeEntry, error) { + return s.repo.Upsert(ctx, ports.KnowledgeUpsertInput{ + Entry: ports.KnowledgeEntry{ + Slug: cmd.Entry.Slug, Title: cmd.Entry.Title, + Kind: cmd.Entry.Kind, Tags: cmd.Entry.Tags, + Content: cmd.Entry.Content, About: cmd.Entry.About, + UpdatedAt: time.Now(), + }, + }) +} + +func (s *KnowledgeService) GetContent(ctx context.Context, slug string) (ports.KnowledgeEntry, error) { + return s.repo.GetContent(ctx, slug) +} + +func (s *KnowledgeService) SoftDelete(ctx context.Context, slug string) error { + return s.repo.SoftDelete(ctx, slug) +} + +func (s *KnowledgeService) Restore(ctx context.Context, slug string) error { + return s.repo.Restore(ctx, slug) +} + +// LearningService handles patterns, feedback, and skills. +type LearningService struct { + repo ports.LearningRepository +} + +func NewLearningService(repo ports.LearningRepository) *LearningService { + return &LearningService{repo: repo} +} + +func (s *LearningService) ListPatterns(ctx context.Context) ([]domain.Pattern, error) { + return s.repo.ListPatterns(ctx, 100) +} + +func (s *LearningService) UpsertPattern(ctx context.Context, pattern domain.Pattern) error { + return s.repo.UpsertPattern(ctx, pattern) +} + +func (s *LearningService) Validate(ctx context.Context, patternID domain.UUID) error { + return s.repo.Validate(ctx, patternID) +} + +func (s *LearningService) Quarantine(ctx context.Context, patternID domain.UUID, reason string) error { + return s.repo.Quarantine(ctx, patternID, reason) +} \ No newline at end of file