feat: Phase 6 — KnowledgeService + LearningService + postgres repos
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).
This commit is contained in:
102
internal/adapters/postgres/knowledge.go
Normal file
102
internal/adapters/postgres/knowledge.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
91
internal/core/app/knowledge.go
Normal file
91
internal/core/app/knowledge.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user