Files
WeKnora/cli/cmd/model/delete.go
T
nullkey 6f8b825f5f feat(cli): agent-first WeKnora CLI
The `weknora` command-line client for the WeKnora RAG server. Full command
surface: kb / doc / chunk / session / message / agent / model / search / chat /
auth / profile / link / api / mcp / skills / config / doctor / schema /
exit-codes.

Agent-first wire contract: JSON envelope by default, typed error codes mapped to
stable exit codes, error.retry_argv / retryable / partial-failure status,
global --dry-run preview, the exit-10 confirmation protocol for writes, and
control/ANSI/Bidi input hygiene. Stateless env-credential auth
(WEKNORA_API_KEY / WEKNORA_TOKEN + WEKNORA_HOST) for headless/CI/agent use.
A curated read-only MCP tool surface (`mcp serve`) and embedded Agent Skills
(`skills install`). Extensive unit + contract-golden tests and CI.
2026-07-06 22:53:01 +08:00

88 lines
2.7 KiB
Go

package modelcmd
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
"github.com/Tencent/WeKnora/cli/internal/iostreams"
sdk "github.com/Tencent/WeKnora/client"
)
// DeleteService is the narrow SDK surface this command depends on.
type DeleteService interface {
DeleteModel(ctx context.Context, id string) error
}
type deleteOptions struct {
Yes bool
DryRun bool
}
// NewCmdDelete builds `weknora model delete <model-id>`.
func NewCmdDelete(f *cmdutil.Factory) *cobra.Command {
opts := &deleteOptions{}
cmd := &cobra.Command{
Use: "delete <model-id>",
Short: "Delete a model",
Long: `Delete a model by id. High-risk write: without -y/--yes in a non-TTY / JSON
context it exits 10 (input.confirmation_required) without deleting. A KB or
agent still referencing the model will fail until repointed.`,
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
fopts, err := cmdutil.CheckFormatFlag(c)
if err != nil {
return err
}
fopts.ResolveDefault(iostreams.IO.IsStdoutTTY())
opts.Yes, _ = c.Flags().GetBool("yes")
id := args[0]
if handled, err := cmdutil.HandleDryRun(c, opts.DryRun, cmdutil.DryRunPlan{
Action: "model.delete",
Args: map[string]any{"model": id},
}); handled {
return err
}
cli, err := f.Client()
if err != nil {
return err
}
if err := cmdutil.ConfirmDestructive(f.Prompter(), opts.Yes, fopts.WantsJSON(),
"delete", "model", id, "model.delete", []string{"weknora", "model", "delete", id, "-y"}); err != nil {
return err
}
return runDelete(c.Context(), fopts, cli, id)
},
}
cmdutil.AddFormatFlag(cmd, "id", "deleted")
cmdutil.AddDryRunFlag(cmd, &opts.DryRun)
cmdutil.SetRisk(cmd, "model.delete")
cmdutil.SetAgentHelp(cmd, cmdutil.AgentHelp{
UsedFor: "delete a model by id",
RequiredFlags: []string{"<model-id> (positional)"},
Examples: []string{"weknora model delete model_abc -y"},
Output: "envelope.data is {id, deleted:true}",
Warnings: []string{
"Requires explicit user approval (exit 10 / input.confirmation_required); never auto-add -y.",
"A knowledge base or agent still referencing this model will fail until repointed.",
},
})
return cmd
}
func runDelete(ctx context.Context, fopts *cmdutil.FormatOptions, svc DeleteService, id string) error {
if err := svc.DeleteModel(ctx, id); err != nil {
return cmdutil.WrapHTTP(err, "delete model %q", id)
}
if fopts.WantsJSON() {
return fopts.Emit(iostreams.IO.Out, map[string]any{"id": id, "deleted": true}, nil)
}
fmt.Fprintf(iostreams.IO.Out, "✓ Deleted model %s\n", id)
return nil
}
// compile-time check: the production SDK client implements DeleteService.
var _ DeleteService = (*sdk.Client)(nil)