feat: ingest Kagi Assistant threads
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:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user