feat(cli): doc delete multi-id + api --paginate + paginate fixes + batch deletes

Batch-write surface and pagination consistency:

* weknora doc delete <doc-id> [<doc-id>...] — positional multi-id, default
  keep-going on failure. Single -y confirms entire batch.
* weknora session delete <session-id> [<session-id>...] — same shape.
* weknora chunk delete <chunk-id> [<chunk-id>...] --doc <doc-id> — multi-id
  with shared --doc parent.
* Multi-id partial-failure rolls up as operation.failed (exit 1), not
  server.error (exit 7) — failures are operation outcomes, not transient
  transport issues, and the retry-with-backoff hint for server.* would
  mislead callers.
* weknora api <path> --paginate — auto-walks offset pagination and merges
  pages into a single {data, total} JSON response.
* Paginate truncation fix across 6 list/follower call sites.
* All doc / search / chunk / session / kb list commands migrated to
  FormatOptions API.

Multi-id RunE only emits the {ok, failed} envelope when the operation
actually ran — pre-flight failures (e.g. confirmation_required) leave
stdout empty per the wire contract.

doc upload's missing-positional-or-flag check is wrapped as FlagError so
the exit code (2) matches the convention used by other commands that
require a positional argument directly.
This commit is contained in:
nullkey
2026-05-18 01:38:30 +08:00
committed by lyingbug
parent 0e081aec5c
commit 34bb0b5096
33 changed files with 1259 additions and 406 deletions
+11 -11
View File
@@ -24,7 +24,7 @@ type uploadOutcome struct {
// in one run. Exit semantics: nil error on full success, a typed *cmdutil.Error
// when ≥1 file failed (the typed code mirrors the first failure's
// classification so callers can still branch).
func runUploadRecursive(ctx context.Context, opts *UploadOptions, jopts *cmdutil.JSONOptions, svc UploadService, kbID, dir string) error {
func runUploadRecursive(ctx context.Context, opts *UploadOptions, fopts *cmdutil.FormatOptions, svc UploadService, kbID, dir string) error {
if opts.Name != "" {
return &cmdutil.Error{
Code: cmdutil.CodeInputInvalidArgument,
@@ -74,8 +74,8 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, jopts *cmdutil
return cmdutil.Wrapf(cmdutil.CodeLocalFileIO, err, "walk %s", dir)
}
if len(matches) == 0 {
if jopts.Enabled() {
return jopts.Emit(iostreams.IO.Out, recursiveResult{KBID: kbID})
if fopts.WantsJSON() {
return fopts.Emit(iostreams.IO.Out, recursiveResult{KBID: kbID})
}
fmt.Fprintf(iostreams.IO.Out, "(no files matched %q under %s)\n", opts.Glob, dir)
return nil
@@ -93,8 +93,8 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, jopts *cmdutil
}
failed = append(failed, uploadOutcome{Path: p, Error: err.Error()})
// Per-file progress lines are human progress signal; suppress
// under --json so they don't precede the JSON object on stdout.
if !jopts.Enabled() {
// under --format json so they don't precede the JSON object on stdout.
if !fopts.WantsJSON() {
fmt.Fprintf(iostreams.IO.Out, "FAIL %s: %v\n", filepath.Base(p), err)
}
continue
@@ -104,14 +104,14 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, jopts *cmdutil
id = k.ID
}
uploaded = append(uploaded, uploadOutcome{Path: p, ID: id})
if !jopts.Enabled() {
if !fopts.WantsJSON() {
fmt.Fprintf(iostreams.IO.Out, "OK %s (id: %s)\n", filepath.Base(p), id)
}
}
if jopts.Enabled() {
if fopts.WantsJSON() {
result := recursiveResult{KBID: kbID, Uploaded: uploaded, Failed: failed}
if err := jopts.Emit(iostreams.IO.Out, result); err != nil {
if err := fopts.Emit(iostreams.IO.Out, result); err != nil {
return err
}
} else {
@@ -119,21 +119,21 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, jopts *cmdutil
}
if len(failed) > 0 {
// Silent on the --json path: the success object above already
// Silent on the --format json path: the success object above already
// carries per-file uploaded[]/failed[] detail; without Silent the
// root error handler would print to stderr in addition. ExitCode
// still walks Code so the typed exit-code-by-class contract holds.
return &cmdutil.Error{
Code: firstFailCode,
Message: fmt.Sprintf("%d of %d uploads failed", len(failed), len(matches)),
Silent: jopts.Enabled(),
Silent: fopts.WantsJSON(),
}
}
return nil
}
// recursiveResult is the JSON shape emitted under data when --recursive is
// combined with --json. Mirrors the human-mode per-file output: a list of
// combined with --format json. Mirrors the human-mode per-file output: a list of
// successes (Uploaded) and a list of failures (Failed), each with the
// originating path so agents can re-try only the failed entries.
type recursiveResult struct {