feat(cli): --json field-select, --jq, auth token, doc --from-url

Output ergonomics:
- `--json` accepts a comma-separated field list (gh-parity); selects
  named keys from the per-command payload. Bare `--json` keeps the
  full shape.
- `--jq <expr>` evaluates a gojq expression over the JSON; pairs with
  `--json field-list` so projection runs before jq.
- `--version` is a global cobra flag in addition to the `version`
  subcommand; both render the same line.
- Per-command `--help` now renders the available JSON field list under
  "JSON fields available via `--json id,name,...`" (field-discovery
  parity with gh / kubectl `-o jsonpath`).

New commands:
- `auth token` — print the active context's credential to stdout for
  shell command substitution (`WEKNORA_TOKEN=$(weknora auth token)`).
  Default: raw secret, no trailing newline. `--json` emits
  `{token, mode, context}`.
- `doc upload --from-url <URL>` — ingest a remote URL via the SDK
  `CreateKnowledgeFromURL`. `--name` forwarded as `FileName` so the
  server's known-extension heuristic upgrades crawl-mode to
  file-download-mode where appropriate.

Includes the simplify post-review polish pass (field-filter unit
tests, --json/--jq compose check, agent_help copy fixes).
This commit is contained in:
nullkey
2026-05-14 23:28:14 +08:00
committed by lyingbug
parent 4fb089d4d7
commit 1b20b06f5e
81 changed files with 2351 additions and 642 deletions
+11 -11
View File
@@ -25,7 +25,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, svc UploadService, kbID, dir string) error {
func runUploadRecursive(ctx context.Context, opts *UploadOptions, jopts *cmdutil.JSONOptions, svc UploadService, kbID, dir string) error {
if opts.Name != "" {
return &cmdutil.Error{
Code: cmdutil.CodeInputInvalidArgument,
@@ -62,9 +62,9 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, svc UploadServ
return cmdutil.Wrapf(cmdutil.CodeLocalFileIO, err, "walk %s", dir)
}
if len(matches) == 0 {
if opts.JSONOut {
return format.WriteEnvelope(iostreams.IO.Out, format.Success(
recursiveResult{KBID: kbID}, &format.Meta{KBID: kbID}))
if jopts.Enabled() {
return format.WriteEnvelopeFiltered(iostreams.IO.Out, format.Success(
recursiveResult{KBID: kbID}, &format.Meta{KBID: kbID}), jopts.Fields, jopts.JQ)
}
fmt.Fprintf(iostreams.IO.Out, "(no files matched %q under %s)\n", opts.Glob, dir)
return nil
@@ -75,7 +75,7 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, svc UploadServ
for _, m := range matches {
previews = append(previews, uploadOutcome{Path: m})
}
return cmdutil.EmitDryRun(opts.JSONOut,
return cmdutil.EmitDryRun(jopts.Enabled(),
recursiveResult{KBID: kbID, Uploaded: previews},
&format.Meta{KBID: kbID},
&format.Risk{Level: format.RiskWrite, Action: fmt.Sprintf("upload %d file(s) to kb %s", len(matches), kbID)})
@@ -93,7 +93,7 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, svc UploadServ
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 envelope on stdout.
if !opts.JSONOut {
if !jopts.Enabled() {
fmt.Fprintf(iostreams.IO.Out, "FAIL %s: %v\n", filepath.Base(p), err)
}
continue
@@ -103,16 +103,16 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, svc UploadServ
id = k.ID
}
uploaded = append(uploaded, uploadOutcome{Path: p, ID: id})
if !opts.JSONOut {
if !jopts.Enabled() {
fmt.Fprintf(iostreams.IO.Out, "OK %s (id: %s)\n", filepath.Base(p), id)
}
}
if opts.JSONOut {
if jopts.Enabled() {
result := recursiveResult{KBID: kbID, Uploaded: uploaded, Failed: failed}
risk := &format.Risk{Level: format.RiskWrite, Action: fmt.Sprintf("upload %d file(s) to kb %s", len(matches), kbID)}
if err := format.WriteEnvelope(iostreams.IO.Out,
format.SuccessWithRisk(result, &format.Meta{KBID: kbID}, risk)); err != nil {
if err := format.WriteEnvelopeFiltered(iostreams.IO.Out,
format.SuccessWithRisk(result, &format.Meta{KBID: kbID}, risk), jopts.Fields, jopts.JQ); err != nil {
return err
}
} else {
@@ -128,7 +128,7 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, svc UploadServ
return &cmdutil.Error{
Code: firstFailCode,
Message: fmt.Sprintf("%d of %d uploads failed", len(failed), len(matches)),
Silent: opts.JSONOut,
Silent: jopts.Enabled(),
}
}
return nil