This commit is contained in:
@@ -41,7 +41,7 @@ func Load(ctx context.Context, dir, dataDir string, embedder Embedder, rag bool,
|
||||
}
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("read knowledge directory %q: %w", dir, err)
|
||||
}
|
||||
for _, e := range entries {
|
||||
if e.IsDir() || !strings.HasSuffix(strings.ToLower(e.Name()), ".json") {
|
||||
@@ -70,14 +70,27 @@ func Load(ctx context.Context, dir, dataDir string, embedder Embedder, rag bool,
|
||||
s.docs = append(s.docs, d)
|
||||
}
|
||||
if rag && len(s.docs) > 0 {
|
||||
if s.embedder == nil {
|
||||
return nil, fmt.Errorf("RAG is enabled but no embedding provider is configured")
|
||||
}
|
||||
if err := s.index(ctx); err != nil {
|
||||
return s, err
|
||||
}
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
func (s *Store) Count() int { s.mu.RLock(); defer s.mu.RUnlock(); return len(s.docs) }
|
||||
func (s *Store) Count() int {
|
||||
if s == nil {
|
||||
return 0
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return len(s.docs)
|
||||
}
|
||||
func (s *Store) ByID(id string) (model.KnowledgeDoc, bool) {
|
||||
if s == nil {
|
||||
return model.KnowledgeDoc{}, false
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
for _, d := range s.docs {
|
||||
@@ -88,6 +101,9 @@ func (s *Store) ByID(id string) (model.KnowledgeDoc, bool) {
|
||||
return model.KnowledgeDoc{}, false
|
||||
}
|
||||
func (s *Store) Search(ctx context.Context, text string, topK int) ([]model.KnowledgeHit, error) {
|
||||
if s == nil {
|
||||
return nil, fmt.Errorf("knowledge store is not initialized")
|
||||
}
|
||||
s.mu.RLock()
|
||||
docs := append([]model.KnowledgeDoc(nil), s.docs...)
|
||||
vecs := make(map[string][]float64, len(s.vectors))
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -50,3 +51,38 @@ func TestLoadRequiresSourceMetadata(t *testing.T) {
|
||||
t.Fatal("expected missing source to fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadMissingDirectoryReturnsHelpfulError(t *testing.T) {
|
||||
missing := filepath.Join(t.TempDir(), "does-not-exist")
|
||||
_, err := Load(context.Background(), missing, t.TempDir(), nil, false, []string{"internal-kb"})
|
||||
if err == nil {
|
||||
t.Fatal("expected missing knowledge directory to fail")
|
||||
}
|
||||
if got := err.Error(); !strings.Contains(got, "read knowledge directory") || !strings.Contains(got, missing) {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNilStoreHelpersDoNotPanic(t *testing.T) {
|
||||
var s *Store
|
||||
if got := s.Count(); got != 0 {
|
||||
t.Fatalf("Count()=%d, want 0", got)
|
||||
}
|
||||
if _, ok := s.ByID("KB1"); ok {
|
||||
t.Fatal("nil store unexpectedly returned a document")
|
||||
}
|
||||
if _, err := s.Search(context.Background(), "vpn", 1); err == nil {
|
||||
t.Fatal("expected Search on nil store to return an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRAGRequiresEmbedderWhenDocumentsExist(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
doc := `{"id":"I1","title":"VPN intern","text":"gateway vpn","answer":"x","source":"internal-kb","language":"de-DE","communication_style":"formal"}`
|
||||
if err := os.WriteFile(filepath.Join(dir, "internal.json"), []byte(doc), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := Load(context.Background(), dir, t.TempDir(), nil, true, []string{"internal-kb"}); err == nil {
|
||||
t.Fatal("expected RAG without embedder to fail")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user