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
+8
View File
@@ -125,6 +125,14 @@ func NewIngestCmd(kdb *db.KnoxDB) *cobra.Command {
record(r)
}
}
log.Printf("[knox] ingesting kagi...")
if results, err := ingest.NewKagiIngester().IngestAll(); err == nil {
for _, r := range results {
record(r)
}
} else {
log.Printf("[knox] kagi ingest failed: %v", err)
}
log.Printf("[knox] ingesting bookorbit...")
if results, err := ingest.NewBookOrbitIngester().IngestAll(); err == nil {
for _, r := range results {
+72
View File
@@ -0,0 +1,72 @@
package cmd
import (
"fmt"
"log"
"github.com/david/knox/internal/db"
"github.com/david/knox/internal/ingest"
"github.com/spf13/cobra"
)
// NewKagiCmd ingests Kagi Assistant conversations (one entry per thread) into
// the knowledge index.
func NewKagiCmd(kdb *db.KnoxDB) *cobra.Command {
cmd := &cobra.Command{
Use: "kagi",
Short: "Ingest Kagi Assistant conversations into the index",
Long: `Reads Kagi Assistant threads with the subscriber session token
(KAGI_SESSION_TOKEN, KAGI_SESSION_TOKEN_FILE, or ~/.config/kagi-cli/config.toml)
and records one entry per conversation. Fingerprints by conversation ID so
edits version the existing entry instead of duplicating the thread.`,
RunE: func(c *cobra.Command, args []string) error {
ing := ingest.NewKagiIngester()
if ing.Token == "" {
return fmt.Errorf("kagi not configured: set KAGI_SESSION_TOKEN (or KAGI_SESSION_TOKEN_FILE, or run `kagi auth login`)")
}
log.Printf("[knox] ingesting Kagi Assistant threads from %s...", ing.BaseURL)
results, err := ing.IngestAll()
if err != nil {
return fmt.Errorf("kagi ingest: %w", err)
}
if len(results) == 0 {
fmt.Println("No new or changed Kagi threads.")
return nil
}
var newCount, updated 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: "kagi_ingest",
Provenance: ingest.ProvenanceJSON(result.Provenance),
ContentHash: result.ContentHash,
})
if err != nil {
log.Printf("[knox] db error: %v", err)
continue
}
if isNew {
newCount++
} else {
updated++
}
}
fmt.Printf("Kagi: %d threads (%d new, %d updated)\n", len(results), newCount, updated)
return nil
},
}
return cmd
}
+4 -2
View File
@@ -44,7 +44,7 @@ func NewMCPServer(kdb *db.KnoxDB) *mcp.Server {
Query string `json:"query,omitempty" jsonschema:"Search query"`
Limit int `json:"limit,omitempty" jsonschema:"Max results (default 10)"`
Detail string `json:"detail,omitempty" jsonschema:"Zoom level: brief, normal, or full (default normal)"`
Scope string `json:"scope,omitempty" jsonschema:"Source scope: all, skills, sessions, logs, snippets, code, notes, browser, karakeep, gitea, repos (default all)"`
Scope string `json:"scope,omitempty" jsonschema:"Source scope: all, skills, sessions, logs, snippets, code, notes, browser, karakeep, kagi, gitea, repos (default all)"`
}
mcp.AddTool(s, &mcp.Tool{Name: "knox_search", Description: "Search the knowledge index. Supports zoom levels: brief (lean), normal (default), full (verbose). Scope filters: all, skills, sessions, logs, snippets, code. A scope of all also appends the best-matching code snippets/techniques (separate index); scope 'snippets' returns only those."},
@@ -98,7 +98,7 @@ func NewMCPServer(kdb *db.KnoxDB) *mcp.Server {
type recentInput struct {
Limit int `json:"limit,omitempty" jsonschema:"Max results (default 10)"`
Detail string `json:"detail,omitempty" jsonschema:"Zoom level: brief, normal, or full (default normal)"`
Scope string `json:"scope,omitempty" jsonschema:"Source scope: all, skills, sessions, logs, notes, browser, karakeep, gitea, repos (default all)"`
Scope string `json:"scope,omitempty" jsonschema:"Source scope: all, skills, sessions, logs, notes, browser, karakeep, kagi, gitea, repos (default all)"`
}
mcp.AddTool(s, &mcp.Tool{Name: "knox_recent", Description: "Show recent knowledge entries with zoom levels: brief, normal (default), full."},
@@ -1266,6 +1266,8 @@ func filterByScope(entries []db.Entry, scope string) []db.Entry {
return filterSource(entries, "obsidian")
case "karakeep", "bookmarks":
return filterSource(entries, "karakeep")
case "kagi", "assistant":
return filterSource(entries, "kagi")
case "gitea":
return filterSource(entries, "gitea")
case "repos", "git":