Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cmd/foo/commands/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ import (
"hop.top/foo/internal/embed"
kitcli "hop.top/kit/go/console/cli"
"hop.top/kit/go/core/xdg"
"hop.top/kit/go/storage/sqlstore"
)

// embedSchemaVersion is the embeddings-store schema revision recorded in
// pre-migrate backup filenames. Bump when embed.NewStore's table layout
// changes so backups are labeled with the version they precede.
const embedSchemaVersion = 1

func embedRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "embed",
Expand Down Expand Up @@ -73,6 +79,7 @@ collection is "default"; override with --collection.`,
return fmt.Errorf("store: %w", err)
}

publishEvent(cmd.Context(), "foo.knowledge.embedding.created", map[string]any{"id": storedID, "collection": collection})
fmt.Fprintf(cmd.OutOrStdout(), "embedded %s into %q\n", storedID, collection)
return nil
},
Expand Down Expand Up @@ -140,6 +147,7 @@ same content produces new rows.`,
}
}

publishEvent(cmd.Context(), "foo.knowledge.embedding.created", map[string]any{"source": filePath, "collection": collection, "chunks": len(chunks)})
fmt.Fprintf(cmd.OutOrStdout(), "embedded %d chunks from %s into %q\n", len(chunks), filePath, collection)
return nil
},
Expand Down Expand Up @@ -254,6 +262,7 @@ delete collections; embeddings themselves are added with
if err := store.DeleteCollection(args[0]); err != nil {
return err
}
publishEvent(cmd.Context(), "foo.knowledge.collection.deleted", map[string]any{"name": args[0]})
fmt.Fprintf(cmd.OutOrStdout(), "collection %q deleted\n", args[0])
return nil
},
Expand All @@ -274,6 +283,16 @@ func openEmbedStore() (*embed.Store, error) {
}

dbPath := filepath.Join(stateDir, "embeddings.db")

// Back up the live DB into <stateDir>/.dbs/ before NewStore runs its
// CREATE TABLE migrations. BackupBeforeMigrate is a no-op on first
// run (no file yet) and writes a timestamped copy otherwise, so a
// schema change never destroys recoverable data. Backups live in a
// hidden .dbs sibling, never beside the live DB.
if _, err := sqlstore.BackupBeforeMigrate(dbPath, embedSchemaVersion, sqlstore.WithBackupDir(filepath.Join(stateDir, ".dbs"))); err != nil {
return nil, fmt.Errorf("backup embeddings db: %w", err)
}

db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return nil, fmt.Errorf("open db: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/foo/commands/fragment.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func fragmentCreateCmd() *cobra.Command {
}
}

publishEvent(ctx, "foo.fragment.created", map[string]any{"alias": alias})
publishEvent(ctx, "foo.knowledge.fragment.created", map[string]any{"alias": alias})
fmt.Fprintf(cmd.OutOrStdout(), "fragment %q saved\n", alias)
return nil
},
Expand Down Expand Up @@ -152,7 +152,7 @@ func fragmentDeleteCmd() *cobra.Command {
return err
}

publishEvent(ctx, "foo.fragment.deleted", map[string]any{"alias": args[0]})
publishEvent(ctx, "foo.knowledge.fragment.deleted", map[string]any{"alias": args[0]})
fmt.Fprintf(cmd.OutOrStdout(), "fragment %q deleted\n", args[0])
return nil
},
Expand Down
Loading
Loading