WeKnora/cli/cmd/doc/delete.go

94 lines
3.3 KiB
Go
Raw Normal View History

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"
)
refactor(cli): align command surface with gh CLI conventions (ADR-3) Audited the v0.0~v0.2 21-command surface against gh / kubectl / cargo / npm / git / docker / flyctl / vercel / supabase / brew. WeKnora was cherry-picking from multiple heritages, producing an inconsistent feel: the kb subtree mixed gh verbs (create / delete / list) with a kubectl verb (get); confirmation flag duplicated --force (docker/kubectl) with global -y/--yes (gh/vercel/npm); the --agent flag stretched Stripe's telemetry-tag pattern into a behavior-mode switch that no mainstream CLI does. ADR-3 picks gh as the primary north star. Documented deviations remain for project-link (vercel/cargo), chat (openai-cli), context (kubectl- light), and doctor (brew/flutter). The decision and its deviations are documented self-contained in cli/AGENTS.md. Surface changes: - kb get → kb view (gh repo view convention); "get" kept as cobra Alias for v0.0/v0.1 callers — see https://cli.github.com/manual/gh_repo_view. - kb delete --force / doc delete --force removed in favor of the global -y/--yes persistent flag (gh repo delete --yes convention). One mechanism skips destructive prompts; ConfirmDestructive's parameter renamed `force` → `yes` to match. - --agent omnibus mode-switch removed. Stripe's DetectAIAgent (the cited inspiration) only tags User-Agent for telemetry, never flips behavior; gh / kubectl / aws / docker / flyctl all decline this kind of flag. The 7-env auto-detect list is reduced to the two entries Stripe also recognizes (CLAUDECODE, CURSOR_AGENT) — the other five had no agent-documented source. ApplyAgentSugar / ShouldUseAgentMode and the dead --no-interactive / --no-progress globals are deleted entirely. - DetectAIAgent and SetAgentHelp annotations are kept: env detection now only triggers AGENT-targeted help text rendering (no behavior change), matching Stripe's narrower scope. Tests: 27 cli packages green (acceptance/contract still pins kb get golden; the alias keeps it valid).
2026-05-10 18:44:20 +00:00
// 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 {
refactor(cli): align command surface with gh CLI conventions (ADR-3) Audited the v0.0~v0.2 21-command surface against gh / kubectl / cargo / npm / git / docker / flyctl / vercel / supabase / brew. WeKnora was cherry-picking from multiple heritages, producing an inconsistent feel: the kb subtree mixed gh verbs (create / delete / list) with a kubectl verb (get); confirmation flag duplicated --force (docker/kubectl) with global -y/--yes (gh/vercel/npm); the --agent flag stretched Stripe's telemetry-tag pattern into a behavior-mode switch that no mainstream CLI does. ADR-3 picks gh as the primary north star. Documented deviations remain for project-link (vercel/cargo), chat (openai-cli), context (kubectl- light), and doctor (brew/flutter). The decision and its deviations are documented self-contained in cli/AGENTS.md. Surface changes: - kb get → kb view (gh repo view convention); "get" kept as cobra Alias for v0.0/v0.1 callers — see https://cli.github.com/manual/gh_repo_view. - kb delete --force / doc delete --force removed in favor of the global -y/--yes persistent flag (gh repo delete --yes convention). One mechanism skips destructive prompts; ConfirmDestructive's parameter renamed `force` → `yes` to match. - --agent omnibus mode-switch removed. Stripe's DetectAIAgent (the cited inspiration) only tags User-Agent for telemetry, never flips behavior; gh / kubectl / aws / docker / flyctl all decline this kind of flag. The 7-env auto-detect list is reduced to the two entries Stripe also recognizes (CLAUDECODE, CURSOR_AGENT) — the other five had no agent-documented source. ApplyAgentSugar / ShouldUseAgentMode and the dead --no-interactive / --no-progress globals are deleted entirely. - DetectAIAgent and SetAgentHelp annotations are kept: env detection now only triggers AGENT-targeted help text rendering (no behavior change), matching Stripe's narrower scope. Tests: 27 cli packages green (acceptance/contract still pins kb get golden; the alias keeps it valid).
2026-05-10 18:44:20 +00:00
Yes bool
JSONOut bool
feat(cli): add agent-first affordance — envelope, exit-10, --dry-run 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.
2026-05-10 18:44:34 +00:00
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"`
}
refactor(cli): align command surface with gh CLI conventions (ADR-3) Audited the v0.0~v0.2 21-command surface against gh / kubectl / cargo / npm / git / docker / flyctl / vercel / supabase / brew. WeKnora was cherry-picking from multiple heritages, producing an inconsistent feel: the kb subtree mixed gh verbs (create / delete / list) with a kubectl verb (get); confirmation flag duplicated --force (docker/kubectl) with global -y/--yes (gh/vercel/npm); the --agent flag stretched Stripe's telemetry-tag pattern into a behavior-mode switch that no mainstream CLI does. ADR-3 picks gh as the primary north star. Documented deviations remain for project-link (vercel/cargo), chat (openai-cli), context (kubectl- light), and doctor (brew/flutter). The decision and its deviations are documented self-contained in cli/AGENTS.md. Surface changes: - kb get → kb view (gh repo view convention); "get" kept as cobra Alias for v0.0/v0.1 callers — see https://cli.github.com/manual/gh_repo_view. - kb delete --force / doc delete --force removed in favor of the global -y/--yes persistent flag (gh repo delete --yes convention). One mechanism skips destructive prompts; ConfirmDestructive's parameter renamed `force` → `yes` to match. - --agent omnibus mode-switch removed. Stripe's DetectAIAgent (the cited inspiration) only tags User-Agent for telemetry, never flips behavior; gh / kubectl / aws / docker / flyctl all decline this kind of flag. The 7-env auto-detect list is reduced to the two entries Stripe also recognizes (CLAUDECODE, CURSOR_AGENT) — the other five had no agent-documented source. ApplyAgentSugar / ShouldUseAgentMode and the dead --no-interactive / --no-progress globals are deleted entirely. - DetectAIAgent and SetAgentHelp annotations are kept: env detection now only triggers AGENT-targeted help text rendering (no behavior change), matching Stripe's narrower scope. Tests: 27 cli packages green (acceptance/contract still pins kb get golden; the alias keeps it valid).
2026-05-10 18:44:20 +00:00
// 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
refactor(cli): align command surface with gh CLI conventions (ADR-3) Audited the v0.0~v0.2 21-command surface against gh / kubectl / cargo / npm / git / docker / flyctl / vercel / supabase / brew. WeKnora was cherry-picking from multiple heritages, producing an inconsistent feel: the kb subtree mixed gh verbs (create / delete / list) with a kubectl verb (get); confirmation flag duplicated --force (docker/kubectl) with global -y/--yes (gh/vercel/npm); the --agent flag stretched Stripe's telemetry-tag pattern into a behavior-mode switch that no mainstream CLI does. ADR-3 picks gh as the primary north star. Documented deviations remain for project-link (vercel/cargo), chat (openai-cli), context (kubectl- light), and doctor (brew/flutter). The decision and its deviations are documented self-contained in cli/AGENTS.md. Surface changes: - kb get → kb view (gh repo view convention); "get" kept as cobra Alias for v0.0/v0.1 callers — see https://cli.github.com/manual/gh_repo_view. - kb delete --force / doc delete --force removed in favor of the global -y/--yes persistent flag (gh repo delete --yes convention). One mechanism skips destructive prompts; ConfirmDestructive's parameter renamed `force` → `yes` to match. - --agent omnibus mode-switch removed. Stripe's DetectAIAgent (the cited inspiration) only tags User-Agent for telemetry, never flips behavior; gh / kubectl / aws / docker / flyctl all decline this kind of flag. The 7-env auto-detect list is reduced to the two entries Stripe also recognizes (CLAUDECODE, CURSOR_AGENT) — the other five had no agent-documented source. ApplyAgentSugar / ShouldUseAgentMode and the dead --no-interactive / --no-progress globals are deleted entirely. - DetectAIAgent and SetAgentHelp annotations are kept: env detection now only triggers AGENT-targeted help text rendering (no behavior change), matching Stripe's narrower scope. Tests: 27 cli packages green (acceptance/contract still pins kb get golden; the alias keeps it valid).
2026-05-10 18:44:20 +00:00
when stdout is a TTY and --json is not set; pass -y/--yes (global flag) to skip
feat(cli): add agent-first affordance — envelope, exit-10, --dry-run 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.
2026-05-10 18:44:34 +00:00
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.`,
refactor(cli): align command surface with gh CLI conventions (ADR-3) Audited the v0.0~v0.2 21-command surface against gh / kubectl / cargo / npm / git / docker / flyctl / vercel / supabase / brew. WeKnora was cherry-picking from multiple heritages, producing an inconsistent feel: the kb subtree mixed gh verbs (create / delete / list) with a kubectl verb (get); confirmation flag duplicated --force (docker/kubectl) with global -y/--yes (gh/vercel/npm); the --agent flag stretched Stripe's telemetry-tag pattern into a behavior-mode switch that no mainstream CLI does. ADR-3 picks gh as the primary north star. Documented deviations remain for project-link (vercel/cargo), chat (openai-cli), context (kubectl- light), and doctor (brew/flutter). The decision and its deviations are documented self-contained in cli/AGENTS.md. Surface changes: - kb get → kb view (gh repo view convention); "get" kept as cobra Alias for v0.0/v0.1 callers — see https://cli.github.com/manual/gh_repo_view. - kb delete --force / doc delete --force removed in favor of the global -y/--yes persistent flag (gh repo delete --yes convention). One mechanism skips destructive prompts; ConfirmDestructive's parameter renamed `force` → `yes` to match. - --agent omnibus mode-switch removed. Stripe's DetectAIAgent (the cited inspiration) only tags User-Agent for telemetry, never flips behavior; gh / kubectl / aws / docker / flyctl all decline this kind of flag. The 7-env auto-detect list is reduced to the two entries Stripe also recognizes (CLAUDECODE, CURSOR_AGENT) — the other five had no agent-documented source. ApplyAgentSugar / ShouldUseAgentMode and the dead --no-interactive / --no-progress globals are deleted entirely. - DetectAIAgent and SetAgentHelp annotations are kept: env detection now only triggers AGENT-targeted help text rendering (no behavior change), matching Stripe's narrower scope. Tests: 27 cli packages green (acceptance/contract still pins kb get golden; the alias keeps it valid).
2026-05-10 18:44:20 +00:00
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 {
refactor(cli): align command surface with gh CLI conventions (ADR-3) Audited the v0.0~v0.2 21-command surface against gh / kubectl / cargo / npm / git / docker / flyctl / vercel / supabase / brew. WeKnora was cherry-picking from multiple heritages, producing an inconsistent feel: the kb subtree mixed gh verbs (create / delete / list) with a kubectl verb (get); confirmation flag duplicated --force (docker/kubectl) with global -y/--yes (gh/vercel/npm); the --agent flag stretched Stripe's telemetry-tag pattern into a behavior-mode switch that no mainstream CLI does. ADR-3 picks gh as the primary north star. Documented deviations remain for project-link (vercel/cargo), chat (openai-cli), context (kubectl- light), and doctor (brew/flutter). The decision and its deviations are documented self-contained in cli/AGENTS.md. Surface changes: - kb get → kb view (gh repo view convention); "get" kept as cobra Alias for v0.0/v0.1 callers — see https://cli.github.com/manual/gh_repo_view. - kb delete --force / doc delete --force removed in favor of the global -y/--yes persistent flag (gh repo delete --yes convention). One mechanism skips destructive prompts; ConfirmDestructive's parameter renamed `force` → `yes` to match. - --agent omnibus mode-switch removed. Stripe's DetectAIAgent (the cited inspiration) only tags User-Agent for telemetry, never flips behavior; gh / kubectl / aws / docker / flyctl all decline this kind of flag. The 7-env auto-detect list is reduced to the two entries Stripe also recognizes (CLAUDECODE, CURSOR_AGENT) — the other five had no agent-documented source. ApplyAgentSugar / ShouldUseAgentMode and the dead --no-interactive / --no-progress globals are deleted entirely. - DetectAIAgent and SetAgentHelp annotations are kept: env detection now only triggers AGENT-targeted help text rendering (no behavior change), matching Stripe's narrower scope. Tests: 27 cli packages green (acceptance/contract still pins kb get golden; the alias keeps it valid).
2026-05-10 18:44:20 +00:00
opts.Yes, _ = c.Flags().GetBool("yes")
feat(cli): add agent-first affordance — envelope, exit-10, --dry-run 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.
2026-05-10 18:44:34 +00:00
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")
refactor(cli): align command surface with gh CLI conventions (ADR-3) Audited the v0.0~v0.2 21-command surface against gh / kubectl / cargo / npm / git / docker / flyctl / vercel / supabase / brew. WeKnora was cherry-picking from multiple heritages, producing an inconsistent feel: the kb subtree mixed gh verbs (create / delete / list) with a kubectl verb (get); confirmation flag duplicated --force (docker/kubectl) with global -y/--yes (gh/vercel/npm); the --agent flag stretched Stripe's telemetry-tag pattern into a behavior-mode switch that no mainstream CLI does. ADR-3 picks gh as the primary north star. Documented deviations remain for project-link (vercel/cargo), chat (openai-cli), context (kubectl- light), and doctor (brew/flutter). The decision and its deviations are documented self-contained in cli/AGENTS.md. Surface changes: - kb get → kb view (gh repo view convention); "get" kept as cobra Alias for v0.0/v0.1 callers — see https://cli.github.com/manual/gh_repo_view. - kb delete --force / doc delete --force removed in favor of the global -y/--yes persistent flag (gh repo delete --yes convention). One mechanism skips destructive prompts; ConfirmDestructive's parameter renamed `force` → `yes` to match. - --agent omnibus mode-switch removed. Stripe's DetectAIAgent (the cited inspiration) only tags User-Agent for telemetry, never flips behavior; gh / kubectl / aws / docker / flyctl all decline this kind of flag. The 7-env auto-detect list is reduced to the two entries Stripe also recognizes (CLAUDECODE, CURSOR_AGENT) — the other five had no agent-documented source. ApplyAgentSugar / ShouldUseAgentMode and the dead --no-interactive / --no-progress globals are deleted entirely. - DetectAIAgent and SetAgentHelp annotations are kept: env detection now only triggers AGENT-targeted help text rendering (no behavior change), matching Stripe's narrower scope. Tests: 27 cli packages green (acceptance/contract still pins kb get golden; the alias keeps it valid).
2026-05-10 18:44:20 +00:00
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 {
feat(cli): add agent-first affordance — envelope, exit-10, --dry-run 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.
2026-05-10 18:44:34 +00:00
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)})
}
refactor(cli): align command surface with gh CLI conventions (ADR-3) Audited the v0.0~v0.2 21-command surface against gh / kubectl / cargo / npm / git / docker / flyctl / vercel / supabase / brew. WeKnora was cherry-picking from multiple heritages, producing an inconsistent feel: the kb subtree mixed gh verbs (create / delete / list) with a kubectl verb (get); confirmation flag duplicated --force (docker/kubectl) with global -y/--yes (gh/vercel/npm); the --agent flag stretched Stripe's telemetry-tag pattern into a behavior-mode switch that no mainstream CLI does. ADR-3 picks gh as the primary north star. Documented deviations remain for project-link (vercel/cargo), chat (openai-cli), context (kubectl- light), and doctor (brew/flutter). The decision and its deviations are documented self-contained in cli/AGENTS.md. Surface changes: - kb get → kb view (gh repo view convention); "get" kept as cobra Alias for v0.0/v0.1 callers — see https://cli.github.com/manual/gh_repo_view. - kb delete --force / doc delete --force removed in favor of the global -y/--yes persistent flag (gh repo delete --yes convention). One mechanism skips destructive prompts; ConfirmDestructive's parameter renamed `force` → `yes` to match. - --agent omnibus mode-switch removed. Stripe's DetectAIAgent (the cited inspiration) only tags User-Agent for telemetry, never flips behavior; gh / kubectl / aws / docker / flyctl all decline this kind of flag. The 7-env auto-detect list is reduced to the two entries Stripe also recognizes (CLAUDECODE, CURSOR_AGENT) — the other five had no agent-documented source. ApplyAgentSugar / ShouldUseAgentMode and the dead --no-interactive / --no-progress globals are deleted entirely. - DetectAIAgent and SetAgentHelp annotations are kept: env detection now only triggers AGENT-targeted help text rendering (no behavior change), matching Stripe's narrower scope. Tests: 27 cli packages green (acceptance/contract still pins kb get golden; the alias keeps it valid).
2026-05-10 18:44:20 +00:00
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 {
feat(cli): add agent-first affordance — envelope, exit-10, --dry-run 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.
2026-05-10 18:44:34 +00:00
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
}