feat(cli): --log-level + kb/agent status & check + cross-cutting refactor

Operability surface and the bulk of the jopts→fopts migration:

* --log-level error|warn|info|debug + WEKNORA_LOG_LEVEL env, wired to
  the SDK via client.SetDebugLevel. Invalid --log-level returns FlagError
  (exit 2).
* kb status <kb-id> / kb check <kb-id> verb split (1 HTTP vs 1+N for
  failed_count aggregation).
* agent status <agent-id> / agent check <agent-id> verb split
  (probes kb_scope_all_reachable via 1+N HTTP).
* kb create <name> positional (matches agent create).
* Positional id help strings namespaced (<kb-id> / <agent-id>).
* All auth / context / link / doctor / kb / agent CRUD commands migrated
  to the FormatOptions API.
* root.go Execute(ctx) takes a context so signal-cancellation propagates
  via cmd.Context() into long-running commands.
* Pagination termination uses len(accum) >= total (not page*pageSize)
  so server-capped page sizes do not truncate aggregations.
This commit is contained in:
nullkey
2026-05-18 01:38:16 +08:00
committed by lyingbug
parent 7eeb3bec5d
commit 0e081aec5c
64 changed files with 1581 additions and 393 deletions
+11 -10
View File
@@ -12,7 +12,7 @@ import (
sdk "github.com/Tencent/WeKnora/client"
)
// kbEmptyFields enumerates the fields surfaced for `--json` discovery on
// kbEmptyFields enumerates the fields surfaced for `--format json` discovery on
// `kb empty`. The result payload is {id, deleted_count}.
var kbEmptyFields = []string{"id", "deleted_count"}
@@ -37,7 +37,7 @@ type emptyResult struct {
func NewCmdEmpty(f *cmdutil.Factory) *cobra.Command {
opts := &EmptyOptions{}
cmd := &cobra.Command{
Use: "empty <id>",
Use: "empty <kb-id>",
Short: "Delete every document in a knowledge base (preserves the KB)",
Long: `Removes all documents and chunks from a knowledge base while keeping the
KB record (its name, description, and config) intact. The delete is async;
@@ -46,27 +46,28 @@ the server reports the count of items enqueued for removal.
Prompts for confirmation by default; pass -y/--yes to skip in agent / CI /
piped contexts. Without -y the CLI exits 10 in non-interactive mode.`,
Example: ` weknora kb empty kb_abc # interactive confirm
weknora kb empty kb_abc -y --json # agent-friendly`,
weknora kb empty kb_abc -y --format json # agent-friendly`,
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
jopts, err := cmdutil.CheckJSONFlags(c)
fopts, err := cmdutil.CheckFormatFlag(c)
if err != nil {
return err
}
fopts.ResolveDefault(iostreams.IO.IsStdoutTTY())
opts.Yes, _ = c.Flags().GetBool("yes")
cli, err := f.Client()
if err != nil {
return err
}
return runEmpty(c.Context(), opts, jopts, cli, f.Prompter(), args[0])
return runEmpty(c.Context(), opts, fopts, cli, f.Prompter(), args[0])
},
}
cmdutil.AddJSONFlags(cmd, kbEmptyFields)
cmdutil.AddFormatFlag(cmd, kbEmptyFields...)
return cmd
}
func runEmpty(ctx context.Context, opts *EmptyOptions, jopts *cmdutil.JSONOptions, svc EmptyService, p prompt.Prompter, id string) error {
if err := cmdutil.ConfirmDestructive(p, opts.Yes, jopts.Enabled(), "all contents of knowledge base", id); err != nil {
func runEmpty(ctx context.Context, opts *EmptyOptions, fopts *cmdutil.FormatOptions, svc EmptyService, p prompt.Prompter, id string) error {
if err := cmdutil.ConfirmDestructive(p, opts.Yes, fopts.WantsJSON(), "all contents of knowledge base", id); err != nil {
return err
}
@@ -79,8 +80,8 @@ func runEmpty(ctx context.Context, opts *EmptyOptions, jopts *cmdutil.JSONOption
deleted = resp.DeletedCount
}
if jopts.Enabled() {
return jopts.Emit(iostreams.IO.Out, emptyResult{ID: id, DeletedCount: deleted})
if fopts.WantsJSON() {
return fopts.Emit(iostreams.IO.Out, emptyResult{ID: id, DeletedCount: deleted})
}
fmt.Fprintf(iostreams.IO.Out, "✓ Emptied knowledge base %s (%d document(s) cleared)\n", id, deleted)
return nil