knowledge

package
v0.0.0-...-d709ff6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package knowledge provides system knowledge indexing functionality. This file implements SystemKnowledgeIndexer for managing built-in knowledge about Agent capabilities, Golang best practices, and architecture principles.

Package knowledge provides document storage and vector-based semantic search.

Store manages documents with embedding vectors using PostgreSQL + pgvector. It supports metadata filtering and similarity search for conversation history and document retrieval.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Document

type Document struct {
	ID       string            // Unique identifier
	Content  string            // Document text content
	Metadata map[string]string // Optional metadata (source, type, etc.)
	CreateAt time.Time         // Creation timestamp
}

Document represents a knowledge document. It contains the textual content and optional metadata. Metadata must be map[string]string to comply with chromem-go requirements.

type KnowledgeQuerier

type KnowledgeQuerier interface {
	// UpsertDocument inserts or updates a document
	UpsertDocument(ctx context.Context, arg sqlc.UpsertDocumentParams) error

	// SearchDocuments performs filtered vector search
	SearchDocuments(ctx context.Context, arg sqlc.SearchDocumentsParams) ([]sqlc.SearchDocumentsRow, error)

	// SearchDocumentsAll performs unfiltered vector search
	SearchDocumentsAll(ctx context.Context, arg sqlc.SearchDocumentsAllParams) ([]sqlc.SearchDocumentsAllRow, error)

	// CountDocuments counts documents matching filter
	CountDocuments(ctx context.Context, filterMetadata []byte) (int64, error)

	// CountDocumentsAll counts all documents
	CountDocumentsAll(ctx context.Context) (int64, error)

	// DeleteDocument deletes a document by ID
	DeleteDocument(ctx context.Context, id string) error

	// ListDocumentsBySourceType lists documents by source type
	ListDocumentsBySourceType(ctx context.Context, arg sqlc.ListDocumentsBySourceTypeParams) ([]sqlc.ListDocumentsBySourceTypeRow, error)
}

KnowledgeQuerier defines the interface for database operations on knowledge documents. Following Go best practices: interfaces are defined by the consumer, not the provider (similar to http.RoundTripper, sql.Driver, io.Reader).

This interface allows Store to depend on abstraction rather than concrete implementation, improving testability and flexibility.

type Result

type Result struct {
	Document   Document
	Similarity float64 // Cosine similarity score (0-1)
}

Result represents a single search result with similarity score.

type SearchOption

type SearchOption func(*searchConfig)

SearchOption configures search behavior using the functional options pattern. This follows Go best practices as seen in context.WithTimeout, grpc.Dial, etc.

func WithFilter

func WithFilter(key, value string) SearchOption

WithFilter adds a metadata filter to restrict search results. Multiple calls to WithFilter add additional filters (AND logic). Example: WithFilter("source_type", "conversation")

func WithTopK

func WithTopK(k int32) SearchOption

WithTopK sets the maximum number of results to return. Default is 5 if not specified. If k < 1, it will be clamped to 1 to ensure valid search configuration.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store manages knowledge documents with vector search capabilities. It handles embedding generation and vector similarity search using PostgreSQL + pgvector.

Store is safe for concurrent use by multiple goroutines.

func New

func New(dbPool *pgxpool.Pool, embedder ai.Embedder, logger *slog.Logger) *Store

New creates a new Store instance

Parameters:

  • dbPool: PostgreSQL connection pool (pgxpool)
  • embedder: AI embedder for generating vector embeddings
  • logger: Logger for debugging (nil = use default)

Example:

store := knowledge.New(dbPool, embedder, slog.Default())

Design: Accepts dbPool and converts to KnowledgeQuerier interface internally. For testing, use NewWithQuerier to inject mock querier directly.

func NewWithQuerier

func NewWithQuerier(querier KnowledgeQuerier, embedder ai.Embedder, logger *slog.Logger) *Store

NewWithQuerier creates a new Store instance with custom querier (useful for testing).

Parameters:

  • querier: Database querier implementing KnowledgeQuerier interface
  • embedder: AI embedder for generating vector embeddings
  • logger: Logger for debugging (nil = use default)

Design: Accepts KnowledgeQuerier interface following "Accept interfaces, return structs" principle for better testability.

func (*Store) Add

func (s *Store) Add(ctx context.Context, doc Document) error

Add adds a document to the knowledge store. The document's content is automatically embedded using the configured embedder. Uses UPSERT (ON CONFLICT DO UPDATE) to handle both inserts and updates.

func (*Store) Close

func (s *Store) Close() error

Close closes the Store (no-op, database connection managed externally)

func (*Store) Count

func (s *Store) Count(ctx context.Context, filter map[string]string) (int, error)

Count returns the number of documents matching the given filter. If filter is nil or empty, it returns the total count of all documents.

Parameters:

  • ctx: Context for the operation
  • filter: Metadata filter (e.g., map[string]string{"source_type": "conversation"})

Returns:

  • int: Number of documents matching the filter
  • error: If count fails

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, docID string) error

Delete removes a document from the knowledge store.

Parameters:

  • ctx: Context for the operation
  • docID: Document ID to delete

Returns:

  • error: If deletion fails

func (*Store) ListBySourceType

func (s *Store) ListBySourceType(ctx context.Context, sourceType string, limit int32) ([]Document, error)

ListBySourceType lists all documents by source type without similarity calculation. This is useful for listing indexed files without needing embeddings.

Parameters:

  • ctx: Context for the operation
  • sourceType: Source type to filter by (e.g., "file", "conversation")
  • limit: Maximum number of documents to return

Returns:

  • []Document: List of documents ordered by creation time (newest first)
  • error: If listing fails

func (*Store) Search

func (s *Store) Search(ctx context.Context, query string, opts ...SearchOption) ([]Result, error)

Search performs semantic search on the knowledge store using functional options. It returns the most similar documents to the query, ordered by similarity score. Automatically applies 10-second timeout for vector search queries to prevent blocking.

Example usage:

results, err := store.Search(ctx, "AI safety",
    knowledge.WithTopK(10),
    knowledge.WithFilter("source_type", "conversation"))

type SystemKnowledgeIndexer

type SystemKnowledgeIndexer struct {
	// contains filtered or unexported fields
}

SystemKnowledgeIndexer manages indexing of system knowledge documents. It provides pre-defined knowledge about Agent capabilities, coding standards, and architectural principles.

Thread-safe: safe for concurrent use (protected by mu).

func NewSystemKnowledgeIndexer

func NewSystemKnowledgeIndexer(store *Store, logger *slog.Logger) *SystemKnowledgeIndexer

NewSystemKnowledgeIndexer creates a new system knowledge indexer.

func (*SystemKnowledgeIndexer) ClearAll

func (s *SystemKnowledgeIndexer) ClearAll(ctx context.Context) error

ClearAll removes all system knowledge documents. Useful for testing and manual reindexing. Thread-safe (uses mutex).

func (*SystemKnowledgeIndexer) IndexAll

func (s *SystemKnowledgeIndexer) IndexAll(ctx context.Context) (int, error)

IndexAll indexes all default system knowledge documents. This method is called during application startup.

Features:

  • Uses fixed document IDs (e.g., "system:golang-errors")
  • UPSERT behavior (updates if already exists)
  • Returns count of successfully indexed documents
  • Thread-safe (uses mutex)

Returns: (indexedCount, error) Error: returns error if ALL documents failed to index