refactor(cli): v0.3 cross-cutting cleanup
Cross-cutting findings surfaced by the branch-completion review. Perf bug: - Factory.Client closure was not memoized. Factory.ResolveKB internally calls f.Client() to resolve --kb name → id, then the command's RunE calls f.Client() again. Two SDK clients, two keyring reads, two AuthRetryTransport allocations per name-resolved invocation, with *independent* token state (a refresh in one was invisible to the other). Switched to sync.Once like Secrets already does. Silent bug bait: - cmdutil.NormalizeHost docstring claimed CodeInputMissingFlag for the empty case; code returned CodeInputInvalidArgument. Aligned doc to code (present-but-empty is a bad value, not a missing flag). Agent contract gaps: - Five user-facing subcommands lacked SetAgentHelp: auth login / logout / list / status and chat. Added concise strings with error- code call-outs so agents can branch without parsing human strings. Helper extraction (≥3 callers): - text.KnowledgeDisplayName(fileName, title, id) — byte-identical formatter that was in both cmd/doc/list.go and cmd/search/docs.go. Takes fields directly so internal/text stays SDK-free. - cmdutil.WrapHTTP(cause, fmt, args...) *Error — replaces the `Wrapf(ClassifyHTTPError(err), err, ...)` pattern across 24 SDK call sites. Sed-driven migration; off-pattern shapes in chat.go (used streamErr) and cmdutil/kb.go (in-package) hand-edited. Contract test gains a comment update: post-migration the dominant pattern is WrapHTTP which the AST scanner skips entirely (only NewError/Wrapf selectors inspected); ClassifyHTTPErrorOutputs() bridge still covers the dynamic codes those paths can yield. UX consistency: - cmd/doc/list.go --page-size help now reads "Items per page (1..1000)" matching cmd/session/list.go. The bounds validation already enforced 1..1000; the help text was the last drift. Comment-discipline sweep: - Deleted the WHAT-only "*Options captures `weknora ...` flag state" docstring across 23 files (context, kb, auth, doc, session, search, chat, doctor, link). Where the line carried a real WHY clause (kb/delete, doc/delete, session/delete, kb/edit), kept the WHY and dropped only the leading WHAT phrase. - Stripped third-party project-name attribution from inline comments and one user-visible flag-help string across ~40 files in cli/cmd and cli/internal (plus 4 test-file comments). Removed phrases like "Mirrors `gh X`", "borrowed from lark-cli", "kubectl-style", "gcloud `--project`", "Stripe pattern", and the embedded GitHub URLs pointing at those projects. Behavioral descriptions and the WHY behind each comment are preserved; only the upstream-name attribution is gone. Inspiration / north-star references belong in cli/AGENTS.md (the design doc) and commit messages, not scattered through every file. Triggered by an audit round that surfaced several false / fragile parity claims (e.g. "Mirrors `gh repo edit`" — gh repo edit has no --name flag; "matches gcloud `--project` id-or-name" — gcloud's --project accepts ID only). Rather than fix them one by one, the whole category of in-comment external-project references was stripped uniformly.
This commit is contained in:
@@ -14,7 +14,6 @@ import (
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// CreateOptions captures `weknora kb create` flags.
|
||||
type CreateOptions struct {
|
||||
Name string
|
||||
Description string
|
||||
@@ -78,7 +77,7 @@ func runCreate(ctx context.Context, opts *CreateOptions, svc CreateService) erro
|
||||
|
||||
created, err := svc.CreateKnowledgeBase(ctx, req)
|
||||
if err != nil {
|
||||
return cmdutil.Wrapf(cmdutil.ClassifyHTTPError(err), err, "create knowledge base")
|
||||
return cmdutil.WrapHTTP(err, "create knowledge base")
|
||||
}
|
||||
|
||||
if opts.JSONOut {
|
||||
|
||||
@@ -13,10 +13,8 @@ import (
|
||||
"github.com/Tencent/WeKnora/cli/internal/prompt"
|
||||
)
|
||||
|
||||
// DeleteOptions captures `weknora kb delete` flags. Yes is sourced from the
|
||||
// global -y/--yes persistent flag (gh-style; see cli/cmd/root.go addGlobalFlags).
|
||||
type DeleteOptions struct {
|
||||
Yes bool
|
||||
Yes bool // sourced from the global -y/--yes persistent flag (see cli/cmd/root.go addGlobalFlags)
|
||||
JSONOut bool
|
||||
DryRun bool
|
||||
}
|
||||
@@ -33,10 +31,9 @@ type deleteResult struct {
|
||||
Deleted bool `json:"deleted"`
|
||||
}
|
||||
|
||||
// NewCmdDelete builds `weknora kb delete`. Mirrors `gh repo delete --yes`
|
||||
// (https://cli.github.com/manual/gh_repo_delete) on the confirmation
|
||||
// pattern — the global -y/--yes persistent flag is the single skip-prompt
|
||||
// switch.
|
||||
// NewCmdDelete builds `weknora kb delete`. The global -y/--yes persistent
|
||||
// flag is the single skip-prompt switch for the destructive-write
|
||||
// confirmation pattern.
|
||||
func NewCmdDelete(f *cmdutil.Factory) *cobra.Command {
|
||||
opts := &DeleteOptions{}
|
||||
cmd := &cobra.Command{
|
||||
@@ -85,7 +82,7 @@ func runDelete(ctx context.Context, opts *DeleteOptions, svc DeleteService, p pr
|
||||
}
|
||||
|
||||
if err := svc.DeleteKnowledgeBase(ctx, id); err != nil {
|
||||
return cmdutil.Wrapf(cmdutil.ClassifyHTTPError(err), err, "delete knowledge base %s", id)
|
||||
return cmdutil.WrapHTTP(err, "delete knowledge base %s", id)
|
||||
}
|
||||
|
||||
if opts.JSONOut {
|
||||
|
||||
@@ -69,8 +69,8 @@ func TestDelete_NotFound(t *testing.T) {
|
||||
|
||||
func TestDelete_NonTTY_NoYes_RequiresConfirmation(t *testing.T) {
|
||||
// SetForTest uses bytes.Buffer for Out — IsStdoutTTY() = false. Without
|
||||
// -y/--yes, exit-10 protocol fires (lark-cli skill protocol; AGENTS.md):
|
||||
// the CLI must NOT silently proceed in scripted contexts.
|
||||
// -y/--yes, exit-10 protocol fires (see AGENTS.md): the CLI must NOT
|
||||
// silently proceed in scripted contexts.
|
||||
iostreams.SetForTest(t)
|
||||
svc := &fakeDeleteSvc{}
|
||||
p := &confirmPrompter{}
|
||||
@@ -82,7 +82,7 @@ func TestDelete_NonTTY_NoYes_RequiresConfirmation(t *testing.T) {
|
||||
assert.Equal(t, cmdutil.CodeInputConfirmationRequired, typed.Code)
|
||||
assert.False(t, svc.called, "non-TTY without -y must not call DeleteKnowledgeBase")
|
||||
assert.False(t, p.asked, "non-TTY ⇒ Confirm is never invoked")
|
||||
assert.Equal(t, 10, cmdutil.ExitCode(err), "exit code 10 per lark-cli skill protocol")
|
||||
assert.Equal(t, 10, cmdutil.ExitCode(err), "exit code 10 per destructive-write protocol")
|
||||
}
|
||||
|
||||
func TestDelete_JSONOutput(t *testing.T) {
|
||||
|
||||
+8
-10
@@ -13,26 +13,24 @@ import (
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// EditOptions captures `weknora kb edit` flags. Name/Description are *string
|
||||
// so we can distinguish "unset" from "set to empty". An unset field is
|
||||
// omitted from the SDK request — only fields the user passed are sent. The
|
||||
// server PUT semantics are "replace everything in the request"; if we
|
||||
// always sent both, an `--name` invocation would silently clear the
|
||||
// description.
|
||||
type EditOptions struct {
|
||||
// Name/Description are *string so we can distinguish "unset" from "set to
|
||||
// empty". An unset field is omitted from the SDK request — only fields the
|
||||
// user passed are sent. Server PUT semantics are "replace everything in the
|
||||
// request"; if we always sent both, an `--name` invocation would silently
|
||||
// clear the description.
|
||||
Name *string
|
||||
Description *string
|
||||
JSONOut bool
|
||||
DryRun bool
|
||||
}
|
||||
|
||||
// EditService is the narrow SDK surface this command depends on.
|
||||
type EditService interface {
|
||||
UpdateKnowledgeBase(ctx context.Context, id string, req *sdk.UpdateKnowledgeBaseRequest) (*sdk.KnowledgeBase, error)
|
||||
}
|
||||
|
||||
// NewCmdEdit builds `weknora kb edit <id>`. Mirrors `gh repo edit`. At
|
||||
// least one of --name / --description must be provided.
|
||||
// NewCmdEdit builds `weknora kb edit <id>`. At least one of --name /
|
||||
// --description must be provided.
|
||||
func NewCmdEdit(f *cmdutil.Factory) *cobra.Command {
|
||||
opts := &EditOptions{}
|
||||
var name, desc string
|
||||
@@ -89,7 +87,7 @@ func runEdit(ctx context.Context, opts *EditOptions, svc EditService, id string)
|
||||
|
||||
updated, err := svc.UpdateKnowledgeBase(ctx, id, req)
|
||||
if err != nil {
|
||||
return cmdutil.Wrapf(cmdutil.ClassifyHTTPError(err), err, "edit knowledge base %s", id)
|
||||
return cmdutil.WrapHTTP(err, "edit knowledge base %s", id)
|
||||
}
|
||||
if opts.JSONOut {
|
||||
return format.WriteEnvelope(iostreams.IO.Out, format.SuccessWithRisk(updated, &format.Meta{KBID: id}, risk))
|
||||
|
||||
+6
-8
@@ -14,14 +14,12 @@ import (
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// EmptyOptions captures `weknora kb empty` flag state.
|
||||
type EmptyOptions struct {
|
||||
Yes bool
|
||||
JSONOut bool
|
||||
DryRun bool
|
||||
}
|
||||
|
||||
// EmptyService is the narrow SDK surface this command depends on.
|
||||
type EmptyService interface {
|
||||
ClearKnowledgeBaseContents(ctx context.Context, id string) (*sdk.ClearKnowledgeBaseContentsResponse, error)
|
||||
}
|
||||
@@ -32,10 +30,10 @@ type emptyResult struct {
|
||||
DeletedCount int `json:"deleted_count"`
|
||||
}
|
||||
|
||||
// NewCmdEmpty builds `weknora kb empty <id>`. Wipes every
|
||||
// document inside the knowledge base; the KB itself is preserved. The
|
||||
// server runs the delete asynchronously and reports the count of documents
|
||||
// that were enqueued for removal.
|
||||
// NewCmdEmpty builds `weknora kb empty <id>`. Wipes every document inside
|
||||
// the knowledge base; the KB itself is preserved. The server runs the
|
||||
// delete asynchronously and reports the count of documents that were
|
||||
// enqueued for removal.
|
||||
func NewCmdEmpty(f *cmdutil.Factory) *cobra.Command {
|
||||
opts := &EmptyOptions{}
|
||||
cmd := &cobra.Command{
|
||||
@@ -46,7 +44,7 @@ KB record (its name, description, and config) intact. The delete is async;
|
||||
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 (lark-cli skill protocol).`,
|
||||
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`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
@@ -81,7 +79,7 @@ func runEmpty(ctx context.Context, opts *EmptyOptions, svc EmptyService, p promp
|
||||
|
||||
resp, err := svc.ClearKnowledgeBaseContents(ctx, id)
|
||||
if err != nil {
|
||||
return cmdutil.Wrapf(cmdutil.ClassifyHTTPError(err), err, "empty knowledge base %s", id)
|
||||
return cmdutil.WrapHTTP(err, "empty knowledge base %s", id)
|
||||
}
|
||||
deleted := 0
|
||||
if resp != nil {
|
||||
|
||||
+2
-3
@@ -1,7 +1,6 @@
|
||||
// Package kb holds the `weknora kb` command tree: list / view / create /
|
||||
// edit / delete / pin / unpin / clear-contents. `view` is the primary read
|
||||
// verb (gh repo view convention); `get` survives as a cobra alias on the
|
||||
// view subcommand for v0.0/v0.1 callers.
|
||||
// edit / delete / pin / unpin / empty. Verb set follows common CRUD
|
||||
// vocabulary (list/view/create/edit/delete) plus pin/unpin and empty.
|
||||
package kb
|
||||
|
||||
import (
|
||||
|
||||
+1
-2
@@ -17,7 +17,6 @@ import (
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// ListOptions captures `weknora kb list` flags.
|
||||
type ListOptions struct {
|
||||
JSONOut bool
|
||||
}
|
||||
@@ -55,7 +54,7 @@ func NewCmdList(f *cmdutil.Factory) *cobra.Command {
|
||||
func runList(ctx context.Context, opts *ListOptions, svc ListService) error {
|
||||
items, err := svc.ListKnowledgeBases(ctx)
|
||||
if err != nil {
|
||||
return cmdutil.Wrapf(cmdutil.ClassifyHTTPError(err), err, "list knowledge bases")
|
||||
return cmdutil.WrapHTTP(err, "list knowledge bases")
|
||||
}
|
||||
if items == nil {
|
||||
items = []sdk.KnowledgeBase{} // ensure JSON [] not null
|
||||
|
||||
+2
-3
@@ -13,7 +13,6 @@ import (
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// PinOptions captures `weknora kb pin / unpin` flag state.
|
||||
type PinOptions struct {
|
||||
JSONOut bool
|
||||
DryRun bool
|
||||
@@ -79,7 +78,7 @@ func runPin(ctx context.Context, opts *PinOptions, svc PinService, id string, wa
|
||||
|
||||
current, err := svc.GetKnowledgeBase(ctx, id)
|
||||
if err != nil {
|
||||
return cmdutil.Wrapf(cmdutil.ClassifyHTTPError(err), err, "get knowledge base %s", id)
|
||||
return cmdutil.WrapHTTP(err, "get knowledge base %s", id)
|
||||
}
|
||||
if current.IsPinned == want {
|
||||
state := "pinned"
|
||||
@@ -99,7 +98,7 @@ func runPin(ctx context.Context, opts *PinOptions, svc PinService, id string, wa
|
||||
|
||||
updated, err := svc.TogglePinKnowledgeBase(ctx, id)
|
||||
if err != nil {
|
||||
return cmdutil.Wrapf(cmdutil.ClassifyHTTPError(err), err, "%s knowledge base %s", verb, id)
|
||||
return cmdutil.WrapHTTP(err, "%s knowledge base %s", verb, id)
|
||||
}
|
||||
if opts.JSONOut {
|
||||
return format.WriteEnvelope(iostreams.IO.Out, format.SuccessWithRisk(updated, &format.Meta{KBID: id}, risk))
|
||||
|
||||
+2
-5
@@ -14,7 +14,6 @@ import (
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// ViewOptions captures `weknora kb view` flags.
|
||||
type ViewOptions struct {
|
||||
JSONOut bool
|
||||
}
|
||||
@@ -24,9 +23,7 @@ type ViewService interface {
|
||||
GetKnowledgeBase(ctx context.Context, id string) (*sdk.KnowledgeBase, error)
|
||||
}
|
||||
|
||||
// NewCmdView builds `weknora kb view <id>`. Mirrors `gh repo view`
|
||||
// (https://cli.github.com/manual/gh_repo_view) — the established mainstream
|
||||
// verb for single-resource reads.
|
||||
// NewCmdView builds `weknora kb view <id>`.
|
||||
func NewCmdView(f *cmdutil.Factory) *cobra.Command {
|
||||
opts := &ViewOptions{}
|
||||
cmd := &cobra.Command{
|
||||
@@ -49,7 +46,7 @@ func NewCmdView(f *cmdutil.Factory) *cobra.Command {
|
||||
func runView(ctx context.Context, opts *ViewOptions, svc ViewService, id string) error {
|
||||
kb, err := svc.GetKnowledgeBase(ctx, id)
|
||||
if err != nil {
|
||||
return cmdutil.Wrapf(cmdutil.ClassifyHTTPError(err), err, "get knowledge base %q", id)
|
||||
return cmdutil.WrapHTTP(err, "get knowledge base %q", id)
|
||||
}
|
||||
if opts.JSONOut {
|
||||
return format.WriteEnvelope(iostreams.IO.Out, format.Success(kb, nil))
|
||||
|
||||
Reference in New Issue
Block a user