refactor(cli): drop --dry-run + introduce bare-JSON output path

Two intertwined mainstream-alignment moves bundled because they share
the migration target (every command's --json path):

1. Drop --dry-run entirely. Survey of comparable API-wrapper CLIs
   (gh, aws, stripe, lark): none expose --dry-run. The mainstream that
   does (kubectl/git/helm/ansible) operates on declarative manifests
   or local state where the preview is materially different from the
   executed action. WeKnora's CLI just echoed the same parameters
   that would have gone on the wire — the preview added no real
   signal over `--help` + reading the call site. Removes:
   - root --dry-run persistent flag + cmdutil/dryrun.go
   - DryRun fields + EmitDryRun calls in 12 write commands
   - format.Envelope.DryRun field
   - 8 corresponding *_test.go cases
   - --dry-run mention from README.md and CHANGELOG.md
   - "dry_run":false from 16 golden envelopes

2. Migrate every --json output to bare data:
   - New format.WriteJSON / WriteJSONFiltered helpers
     (cli/internal/format/bare.go) share filterArrayItems /
     filterObjectKeys / writeJQ with the (still-live for now) envelope
     filter helpers.
   - Read commands (kb/doc/session list+view, search chunks/docs/
     sessions/kb, auth list/status, agent list/view, context list,
     doctor) emit bare arrays / objects on stdout.
   - Write commands (kb create/edit/delete/pin/empty, doc upload/
     upload_recursive/delete, session delete, auth login/logout/
     refresh/token, link/unlink, context add/use/remove, agent
     invoke, chat, api, version) emit bare result objects. Risk
     classification dropped — the resource + exit code already
     convey the action.

Per-command shape changes:
   list / search       → []T   (was {ok, data:{items:[…]}})
   view                → T     (was {ok, data:T, _meta:…})
   create / edit       → T
   delete / pin / etc. → {id, …action result…}
   doctor              → {summary, checks}
   api                 → {status, headers, body}

_meta dropped on the read path:
   pagination (page/page_size/total/has_more) — agents iterate with
   --all-pages or accept --limit (gh CLI parity);
   kb_id / context echo — caller already knows what it asked for.

Acceptance contract goldens regenerated for the new bare shape.
Error envelope on stdout (PrintErrorEnvelope) stays live for now —
the envelope-infra deletion lands in the next commit.
This commit is contained in:
nullkey
2026-05-14 23:28:14 +08:00
committed by lyingbug
parent bdc589e1c0
commit cc8254f862
95 changed files with 579 additions and 911 deletions
+1 -5
View File
@@ -241,11 +241,7 @@ func runChat(ctx context.Context, opts *Options, jopts *cmdutil.JSONOptions, svc
KBID: opts.KBID,
Query: opts.Query,
}
return format.WriteEnvelopeFiltered(
iostreams.IO.Out,
format.Success(data, &format.Meta{KBID: opts.KBID}),
jopts.Fields, jopts.JQ,
)
return format.WriteJSONFiltered(iostreams.IO.Out, data, jopts.Fields, jopts.JQ)
}
// Human / non-JSON paths: streaming mode already wrote the answer body
+23 -29
View File
@@ -122,42 +122,36 @@ func TestChat_JSONMode(t *testing.T) {
t.Errorf("expected empty stderr in JSON mode, got %q", errBuf.String())
}
var env struct {
OK bool `json:"ok"`
Data struct {
Answer string `json:"answer"`
SessionID string `json:"session_id"`
AssistantMessageID string `json:"assistant_message_id"`
KBID string `json:"kb_id"`
Query string `json:"query"`
References []struct {
KnowledgeID string `json:"knowledge_id"`
} `json:"references"`
} `json:"data"`
var got struct {
Answer string `json:"answer"`
SessionID string `json:"session_id"`
AssistantMessageID string `json:"assistant_message_id"`
KBID string `json:"kb_id"`
Query string `json:"query"`
References []struct {
KnowledgeID string `json:"knowledge_id"`
} `json:"references"`
}
if err := json.Unmarshal(out.Bytes(), &env); err != nil {
t.Fatalf("decode envelope: %v\n%s", err, out.String())
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
t.Fatalf("decode JSON: %v\n%s", err, out.String())
}
if !env.OK {
t.Errorf("expected ok:true, got %s", out.String())
if got.Answer != "answer body" {
t.Errorf("answer: got %q", got.Answer)
}
if env.Data.Answer != "answer body" {
t.Errorf("answer: got %q", env.Data.Answer)
if got.SessionID != "sess_auto" {
t.Errorf("session_id: got %q", got.SessionID)
}
if env.Data.SessionID != "sess_auto" {
t.Errorf("session_id: got %q", env.Data.SessionID)
if got.AssistantMessageID != "msg_99" {
t.Errorf("assistant_message_id: got %q", got.AssistantMessageID)
}
if env.Data.AssistantMessageID != "msg_99" {
t.Errorf("assistant_message_id: got %q", env.Data.AssistantMessageID)
if got.KBID != "kb_42" {
t.Errorf("kb_id: got %q", got.KBID)
}
if env.Data.KBID != "kb_42" {
t.Errorf("kb_id: got %q", env.Data.KBID)
if got.Query != "q" {
t.Errorf("query: got %q", got.Query)
}
if env.Data.Query != "q" {
t.Errorf("query: got %q", env.Data.Query)
}
if len(env.Data.References) != 1 || env.Data.References[0].KnowledgeID != "k1" {
t.Errorf("references payload missing: %+v", env.Data.References)
if len(got.References) != 1 || got.References[0].KnowledgeID != "k1" {
t.Errorf("references payload missing: %+v", got.References)
}
}