Files
knox/internal/cmd/bookorbit.go
T
david fc9454832f
build / image (push) Successful in 40s
build / test (push) Successful in 3m42s
feat: ingest BookOrbit highlights as chapter entries (#47)
The index had no trace of anything I have read: BookOrbit holds 234
annotations across 511 books and none of it reached knox, so recall about books
was empty.

Entries are chapters, not highlights. A book produces a handful of dense,
readable entries instead of hundreds of fragments, which is what a reader
actually asks for ("what did I highlight in this chapter").

Uses the annotations export endpoint rather than the Postgres schema: it is the
contract BookOrbit maintains, and it already carries book, chapter, note, style
and origin. Login is one POST per sweep (the endpoint is throttled at 3/min).

Fingerprint is (book, chapter); the content hash is the rendered body, so a new
highlight updates the entry and appends a log version rather than creating a
second copy of the chapter. The watcher deliberately has no mtime-style skip:
a highlight edited in place keeps the same latest timestamp, and skipping on
that would hide the edit.

Wired as 'knox bookorbit', into the full 'knox ingest' sweep, and a 30-minute
ticker inside LocalSourcesEnabled (the server node replicates only).
2026-11-08 11:55:41 -08:00

71 lines
2.2 KiB
Go

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
}