feat: ingest Kagi Assistant threads
build / test (push) Successful in 3m21s
build / image (push) Successful in 35s

Add a Kagi source that indexes Assistant conversations with the subscriber
session token, one entry per thread.

- Read the token from KAGI_SESSION_TOKEN, KAGI_SESSION_TOKEN_FILE, or the
  kagi-cli config ([auth] session_token); accept full session-link URLs.
- List conversations through /api/conversations, load each thread via
  /api/v2/conversations/{id}/init, and walk older turn pages through the
  branch turns endpoint.
- Render one markdown document per thread (stable fingerprint by conversation
  UUID, content hash for versioning), keeping the newest turns when a thread
  exceeds the body budget.
- Skip re-fetching threads whose updated_at has not moved during this process.
- Wire the 30-minute watcher ticker + sync status, the `knox kagi` command,
  the full ingest sweep, MCP scope filtering, thread anchoring, and web links.
This commit is contained in:
2026-11-27 13:43:10 -08:00
parent 65fdcbc789
commit d27c682c19
10 changed files with 977 additions and 10 deletions
+1
View File
@@ -41,6 +41,7 @@ var workSources = map[string]bool{
"obsidian": true,
"filesystem": true,
"karakeep": true,
"kagi": true,
}
// NewAutoThreader reads the tuning knobs from the environment. All have
+68 -4
View File
@@ -28,6 +28,7 @@ const (
// is not fully re-parsed every couple of seconds.
logDebounce = 30 * time.Second
karakeepInterval = 30 * time.Minute
kagiInterval = 30 * time.Minute
bookorbitInterval = 30 * time.Minute
// Brevier's archive changes only when a page is read, so an hour is plenty.
brevierInterval = 1 * time.Hour
@@ -233,6 +234,7 @@ func (w *Watcher) Start() error {
gitTicker := time.NewTicker(gitInterval)
opencodeTicker := time.NewTicker(opencodeInterval)
karakeepTicker := time.NewTicker(karakeepInterval)
kagiTicker := time.NewTicker(kagiInterval)
bookorbitTicker := time.NewTicker(bookorbitInterval)
brevierTicker := time.NewTicker(brevierInterval)
healthTicker := time.NewTicker(healthInterval)
@@ -249,16 +251,16 @@ func (w *Watcher) Start() error {
// Ingestion only runs where the sources exist. A node with no local
// sources (KNOX_LOCAL_SOURCES=none) leaves these nil: nil channels block
// forever, so the select never fires for them.
var browserC, giteaC, gitC, opencodeC, karakeepC, bookorbitC, brevierC, healthC <-chan time.Time
var browserC, giteaC, gitC, opencodeC, karakeepC, kagiC, bookorbitC, brevierC, healthC <-chan time.Time
if LocalSourcesEnabled() {
browserC, giteaC, gitC = browserTicker.C, giteaTicker.C, gitTicker.C
opencodeC, karakeepC, bookorbitC = opencodeTicker.C, karakeepTicker.C, bookorbitTicker.C
brevierC, healthC = brevierTicker.C, healthTicker.C
opencodeC, karakeepC, kagiC = opencodeTicker.C, karakeepTicker.C, kagiTicker.C
bookorbitC, brevierC, healthC = bookorbitTicker.C, brevierTicker.C, healthTicker.C
} else {
log.Printf("[knox] KNOX_LOCAL_SOURCES=none: local ingest disabled, derivation still active")
}
var browserRunning, giteaRunning, gitRunning, threadRunning, gossipRunning atomic.Bool
var opencodeRunning, karakeepRunning, bookorbitRunning, brevierRunning, healthRunning, snippetRunning atomic.Bool
var opencodeRunning, karakeepRunning, kagiRunning, bookorbitRunning, brevierRunning, healthRunning, snippetRunning atomic.Bool
for {
select {
@@ -344,6 +346,14 @@ func (w *Watcher) Start() error {
defer karakeepRunning.Store(false)
w.ingestKarakeep()
}()
case <-kagiC:
if !kagiRunning.CompareAndSwap(false, true) {
continue
}
go func() {
defer kagiRunning.Store(false)
w.ingestKagi()
}()
case <-bookorbitC:
if !bookorbitRunning.CompareAndSwap(false, true) {
continue
@@ -805,6 +815,60 @@ func (w *Watcher) ingestKarakeep() {
w.recordSync("karakeep", karakeepInterval, db.SyncOK, newCount, 0, skipped, len(results), nil)
}
// ingestKagi pulls Kagi Assistant conversations, one entry per thread, using a
// subscriber session token (KAGI_SESSION_TOKEN, or the kagi-cli config).
func (w *Watcher) ingestKagi() {
ing := ingest.NewKagiIngester()
if ing.Token == "" {
w.recordSync("kagi", kagiInterval, db.SyncUnconfigured, 0, 0, 0, 0, nil)
return
}
results, err := ing.IngestAll()
if err != nil {
log.Printf("[knox] kagi ingest error: %v", err)
w.recordSync("kagi", kagiInterval, db.SyncError, 0, 0, 0, 0, err)
return
}
if len(results) == 0 {
w.recordSync("kagi", kagiInterval, db.SyncOK, 0, 0, 0, 0, nil)
return
}
// The ingester already skips conversations whose updated_at has not moved
// since this process last read them; the content hash in RecordObservation
// still decides whether a re-read is a new version or a no-op.
var newCount, updated int
for _, result := range results {
_, isNew, err := w.knoxDB.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: "kagi_timer",
Provenance: ingest.ProvenanceJSON(result.Provenance),
ContentHash: result.ContentHash,
})
if err != nil {
continue
}
if isNew {
newCount++
} else {
updated++
}
}
if newCount > 0 || updated > 0 {
log.Printf("[knox] kagi: %d new, %d updated", newCount, updated)
}
w.recordSync("kagi", kagiInterval, db.SyncOK, newCount, updated, 0, len(results), nil)
}
// ingestBookOrbit pulls reading highlights (one entry per book chapter) from a
// self-hosted BookOrbit instance configured via BOOKORBIT_URL,
// BOOKORBIT_USERNAME and BOOKORBIT_PASSWORD.