feat(cli): add whoami / doctor / kb / context commands (PR-7)

5 new leaf commands wired into the root tree:
  whoami      — simplified `auth status` (user_id + tenant_id only)
  doctor      — 4-item self-check (base_url / auth / server_version / cred_storage)
                with --offline / --no-cache flags + skip cascade + summary.all_passed
                防 agent 看到 envelope.ok=true 误判命令整体 success
  kb list     — list KBs (default updated_at desc; 0 KB → "(no knowledge bases)")
                tabwriter 4-col (ID/NAME/DOCS/UPDATED), display-width truncation
  kb get      — show single KB details (KEY: VALUE, suppress empty fields)
  context use — switch default context (writes config.current_context),
                带 levenshtein distance ≤ 2 的 did-you-mean hint

Each command uses the v0.0 narrow Service interface pattern (testable via
fakes), agent.SetAgentHelp for AI-friendly hints, and ClassifyHTTPError
for stable error code mapping.

cmdutil/errors.go: 新增 CodeLocalContextNotFound for `context use`,加入 AllCodes() 注册集.

cli/cmd/root.go: NewRootCmd 改为 exported (acceptance/contract 测试需要),
                 注册 4 个新命令 + 1 parent group; root_test.go 跟随更新.
This commit is contained in:
nullkey
2026-05-08 16:13:58 +08:00
committed by lyingbug
parent 92d78dea72
commit cf84bf2a38
15 changed files with 1201 additions and 15 deletions
+15 -6
View File
@@ -1,7 +1,7 @@
// Package cmd holds the cobra command tree. main.go calls Execute().
//
// Foundation PR registers only the root command and `version`; resource
// commands (auth, kb, doc, ...) land in PR-4 and later.
// v0.0 shipped: version / auth / search.
// v0.1 adds: whoami / doctor / kb (list + get) / context (use).
package cmd
import (
@@ -12,7 +12,11 @@ import (
"github.com/spf13/cobra"
"github.com/Tencent/WeKnora/cli/cmd/auth"
contextcmd "github.com/Tencent/WeKnora/cli/cmd/context"
"github.com/Tencent/WeKnora/cli/cmd/doctor"
"github.com/Tencent/WeKnora/cli/cmd/kb"
"github.com/Tencent/WeKnora/cli/cmd/search"
"github.com/Tencent/WeKnora/cli/cmd/whoami"
"github.com/Tencent/WeKnora/cli/internal/agent"
"github.com/Tencent/WeKnora/cli/internal/build"
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
@@ -22,7 +26,7 @@ import (
// Execute is the entry point invoked by main(). Returns the process exit code.
func Execute() int {
root := newRootCmd(cmdutil.New())
root := NewRootCmd(cmdutil.New())
// ExecuteC returns the actually-invoked leaf (or root when invocation
// failed before dispatch); we use it to honor the leaf's --json and
// inherited --format without walking the tree ourselves.
@@ -116,9 +120,10 @@ var cobraFlagErrorPrefixes = []string{
"invalid argument", // pflag type-coercion failure (e.g. --top-k=foo)
}
// newRootCmd builds the cobra tree. Splitting it from Execute() lets tests
// drive the tree directly with their own factory.
func newRootCmd(f *cmdutil.Factory) *cobra.Command {
// NewRootCmd builds the cobra tree. Splitting it from Execute() lets tests
// drive the tree directly with their own factory. Exported (PR-7) so the
// acceptance/contract suite can construct the tree in-process.
func NewRootCmd(f *cmdutil.Factory) *cobra.Command {
v, commit, date := build.Info()
cmd := &cobra.Command{
Use: "weknora",
@@ -148,6 +153,10 @@ func newRootCmd(f *cmdutil.Factory) *cobra.Command {
cmd.AddCommand(newVersionCmd(f))
cmd.AddCommand(auth.NewCmdAuth(f))
cmd.AddCommand(search.NewCmdSearch(f))
cmd.AddCommand(whoami.NewCmd(f))
cmd.AddCommand(doctor.NewCmd(f))
cmd.AddCommand(kb.NewCmd(f))
cmd.AddCommand(contextcmd.NewCmd(f))
return cmd
}