package cmd import ( "fmt" "log" "github.com/david/knox/internal/db" "github.com/david/knox/internal/ingest" "github.com/spf13/cobra" ) // NewBookOrbitCmd ingests reading highlights from a self-hosted BookOrbit // instance into the knowledge index. func NewBookOrbitCmd(kdb *db.KnoxDB) *cobra.Command { cmd := &cobra.Command{ Use: "bookorbit", Short: "Ingest BookOrbit highlights as per-chapter entries", Long: `Reads the annotation export from the BookOrbit REST API (BOOKORBIT_URL, BOOKORBIT_USERNAME, BOOKORBIT_PASSWORD) and records one entry per book chapter. Fingerprints by book and chapter, so added highlights update the chapter instead of creating a second copy of it.`, RunE: func(c *cobra.Command, args []string) error { ing := ingest.NewBookOrbitIngester() if ing.BaseURL == "" || ing.Username == "" || ing.Password == "" { return fmt.Errorf("bookorbit not configured: set BOOKORBIT_URL, BOOKORBIT_USERNAME and BOOKORBIT_PASSWORD") } log.Printf("[knox] ingesting BookOrbit highlights from %s...", ing.BaseURL) results, err := ing.IngestAll() if err != nil { return fmt.Errorf("bookorbit ingest: %w", err) } if len(results) == 0 { fmt.Println("No BookOrbit annotations found.") return nil } var newCount int for _, result := range results { _, isNew, err := kdb.RecordObservation(db.ObservationRecord{ Fingerprint: result.Fingerprint, SourceID: result.SourceID, SourcePath: result.SourcePath, Project: result.Project, ContentType: result.ContentType, Title: result.Title, Summary: result.Summary, Content: result.Content, CreatedAt: result.CreatedAt, Confidence: result.Confidence, IngesterVersion: result.IngesterVersion, Trigger: "bookorbit_ingest", Provenance: ingest.ProvenanceJSON(result.Provenance), ContentHash: result.ContentHash, }) if err != nil { log.Printf("[knox] db error: %v", err) continue } if isNew { newCount++ } } fmt.Printf("BookOrbit: %d chapter(s), %d new\n", len(results), newCount) return nil }, } return cmd }