package cmd import ( "bufio" "fmt" "os" "strings" "github.com/kb-search/kb/internal/api" "github.com/kb-search/kb/internal/output" "github.com/spf13/cobra" ) var reindexCmd = &cobra.Command{ Use: "reindex", Short: "Re-embed all chunks with the current engine model", Args: cobra.NoArgs, RunE: runReindex, } func init() { reindexCmd.Flags().BoolP("yes", "y", false, "skip confirmation prompt") rootCmd.AddCommand(reindexCmd) } func runReindex(cmd *cobra.Command, args []string) error { yes, _ := cmd.Flags().GetBool("yes") client := api.NewClient() if !yes { // Fetch model name from engine status modelName := "current" statusResp, err := client.Get("/api/v1/status") if err == nil && api.CheckError(statusResp) == nil { var status struct { ModelName string `json:"model_name"` } if api.DecodeJSON(statusResp, &status) == nil && status.ModelName != "" { modelName = status.ModelName } } fmt.Printf("Reindex all chunks? This will re-embed everything with the %s model. [y/N] ", modelName) reader := bufio.NewReader(os.Stdin) answer, _ := reader.ReadString('\n') answer = strings.TrimSpace(strings.ToLower(answer)) if answer != "y" && answer != "yes" { fmt.Println("Cancelled.") return nil } } resp, err := client.Post("/api/v1/reindex", nil) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } if err := api.CheckError(resp); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } var result struct { ChunksReindexed int `json:"chunks_reindexed"` Model string `json:"model"` } if output.IsJSON() { var raw interface{} if err := api.DecodeJSON(resp, &raw); err != nil { fmt.Fprintln(os.Stderr, "Failed to parse response:", err) os.Exit(1) } output.PrintJSON(raw) } else { if err := api.DecodeJSON(resp, &result); err != nil { fmt.Fprintln(os.Stderr, "Failed to parse response:", err) os.Exit(1) } fmt.Printf("Reindexed %d chunks (model: %s)\n", result.ChunksReindexed, result.Model) } return nil }