da9faa9e07
Borrows the lark-cli agent-affordance model (https://github.com/larksuite/cli/blob/main/AGENTS.md + skills/lark-shared/SKILL.md) so weknora is designed to be agent-friendly: error messages, output format, and flag design follow conventions agents can rely on. cli/AGENTS.md (operational reference for LLM agents invoking weknora): Public document covering envelope schema, exit-code protocol (0/1/2/10/130), stdout/stderr separation, and behavioral rules. Sensitive commands (\`context use\`, \`kb delete\`, \`doc delete\`, \`init\`) gain "AI agents:" paragraphs in their cobra Long descriptions so guidance shows in --help. format.Envelope schema additions: Risk per-operation classification (read / write / high-risk-write + action description), populated by write commands on both success and failure paths. Notice system advisories (CLI update available, server-CLI version skew); type defined, emit sites land in v0.3. DryRun marker for envelopes returned from --dry-run preview paths. RiskLevel constants realigned to lark's taxonomy: read / write / high-risk-write (was: read / mutating / destructive — not yet wired by any command). cmdutil.Error gains OperationRisk; PrintErrorEnvelope auto-attaches it to envelope.Risk so destructive failure paths surface uniformly. Exit-10 confirmation protocol: New ErrorCode \`input.confirmation_required\` mapped to exit code 10 in cmdutil.ExitCode. ConfirmDestructive now returns this code (with OperationRisk attached) when stdout is non-TTY or --json was set, with -y/--yes absent. Previous behavior — silent proceed in non-TTY — was unsafe: scripts and agents could delete resources with no explicit approval. Three test cases re-pinned around the new contract. This is a wire-contract change for any caller who relied on silent proceed; v0.0/v0.1 had no destructive commands, so the blast radius is contained to v0.2 itself. --dry-run global flag: cmd write paths (kb create/delete, doc upload/delete, api POST/PUT/PATCH/ DELETE) check cmdutil.IsDryRun(cmd) and skip the SDK call, emitting an envelope with dry_run=true plus a Risk classification. Read commands ignore --dry-run by design (no side effect to preview). Human-mode prints \`[dry-run] would <action>\` to stdout. Command discovery: agents introspect via the existing \`--help\` surface (consistent with gh / kubectl / aws / gcloud / terraform — none of them ship a CLI-tree self-description command). An earlier draft added a \`weknora schema\` reflection command; dropped after a mainstream survey found it has no stable analog (lark-cli's schema describes Lark API methods, not its own CLI tree). Tests: 27 cli packages pass at this commit. Added two new tests covering envelope.risk and envelope._notice serialization.
94 lines
3.3 KiB
Go
94 lines
3.3 KiB
Go
package doc
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/Tencent/WeKnora/cli/internal/agent"
|
|
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
|
"github.com/Tencent/WeKnora/cli/internal/format"
|
|
"github.com/Tencent/WeKnora/cli/internal/iostreams"
|
|
"github.com/Tencent/WeKnora/cli/internal/prompt"
|
|
)
|
|
|
|
// DeleteOptions captures `weknora doc delete` flags. Yes is sourced from the
|
|
// global -y/--yes persistent flag (gh-style; see cli/cmd/root.go).
|
|
type DeleteOptions struct {
|
|
Yes bool
|
|
JSONOut bool
|
|
DryRun bool
|
|
}
|
|
|
|
// DeleteService is the narrow SDK surface this command depends on.
|
|
// *sdk.Client satisfies it.
|
|
type DeleteService interface {
|
|
DeleteKnowledge(ctx context.Context, id string) error
|
|
}
|
|
|
|
// deleteResult is the typed payload emitted under data on success.
|
|
type deleteResult struct {
|
|
ID string `json:"id"`
|
|
Deleted bool `json:"deleted"`
|
|
}
|
|
|
|
// NewCmdDelete builds `weknora doc delete`. Mirrors `gh repo delete --yes`
|
|
// confirmation pattern via the global -y/--yes persistent flag.
|
|
func NewCmdDelete(f *cmdutil.Factory) *cobra.Command {
|
|
opts := &DeleteOptions{}
|
|
cmd := &cobra.Command{
|
|
Use: "delete <id>",
|
|
Short: "Delete a document from a knowledge base",
|
|
Long: `Permanently deletes one document. Prompts for confirmation by default
|
|
when stdout is a TTY and --json is not set; pass -y/--yes (global flag) to skip
|
|
the prompt (required in agent / CI / piped contexts).
|
|
|
|
AI agents: This is a high-risk write. Without -y/--yes the CLI exits 10 and
|
|
returns an envelope describing the missing confirmation. NEVER auto-pass -y
|
|
without the user's explicit go-ahead.`,
|
|
Example: ` weknora doc delete doc_abc # interactive confirm
|
|
weknora doc delete doc_abc -y # no prompt
|
|
weknora doc delete doc_abc -y --json # envelope output`,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
opts.Yes, _ = c.Flags().GetBool("yes")
|
|
opts.DryRun = cmdutil.IsDryRun(c)
|
|
if opts.DryRun {
|
|
return runDelete(c.Context(), opts, nil, f.Prompter(), args[0])
|
|
}
|
|
cli, err := f.Client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return runDelete(c.Context(), opts, cli, f.Prompter(), args[0])
|
|
},
|
|
}
|
|
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
|
|
agent.SetAgentHelp(cmd, "Destructively deletes one document by id. ALWAYS pass -y/--yes in agent mode (no TTY ⇒ confirm prompt fails). Returns data: {id, deleted:true}.")
|
|
return cmd
|
|
}
|
|
|
|
func runDelete(ctx context.Context, opts *DeleteOptions, svc DeleteService, p prompt.Prompter, id string) error {
|
|
if opts.DryRun {
|
|
return cmdutil.EmitDryRun(opts.JSONOut,
|
|
deleteResult{ID: id, Deleted: false}, nil,
|
|
&format.Risk{Level: format.RiskHighRiskWrite, Action: fmt.Sprintf("delete document %s", id)})
|
|
}
|
|
|
|
if err := cmdutil.ConfirmDestructive(p, opts.Yes, opts.JSONOut, "document", id); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := svc.DeleteKnowledge(ctx, id); err != nil {
|
|
return cmdutil.Wrapf(cmdutil.ClassifyHTTPError(err), err, "delete document %s", id)
|
|
}
|
|
|
|
if opts.JSONOut {
|
|
risk := &format.Risk{Level: format.RiskHighRiskWrite, Action: fmt.Sprintf("deleted document %s", id)}
|
|
return format.WriteEnvelope(iostreams.IO.Out, format.SuccessWithRisk(deleteResult{ID: id, Deleted: true}, nil, risk))
|
|
}
|
|
fmt.Fprintf(iostreams.IO.Out, "✓ Deleted document %s\n", id)
|
|
return nil
|
|
}
|