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:
@@ -183,11 +183,7 @@ func runInvoke(ctx context.Context, opts *InvokeOptions, jopts *cmdutil.JSONOpti
|
||||
AgentID: opts.AgentID,
|
||||
Query: opts.Query,
|
||||
}
|
||||
return format.WriteEnvelopeFiltered(
|
||||
iostreams.IO.Out,
|
||||
format.Success(data, nil),
|
||||
jopts.Fields, jopts.JQ,
|
||||
)
|
||||
return format.WriteJSONFiltered(iostreams.IO.Out, data, jopts.Fields, jopts.JQ)
|
||||
}
|
||||
|
||||
out := iostreams.IO.Out
|
||||
|
||||
@@ -76,30 +76,24 @@ func TestInvoke_AccumulateMode_EmitsJSONEnvelope(t *testing.T) {
|
||||
if err := runInvoke(context.Background(), opts, &cmdutil.JSONOptions{}, svc); err != nil {
|
||||
t.Fatalf("runInvoke: %v", err)
|
||||
}
|
||||
var env struct {
|
||||
OK bool `json:"ok"`
|
||||
Data invokeData `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(out.Bytes(), &env); err != nil {
|
||||
var got invokeData
|
||||
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
|
||||
t.Fatalf("parse: %v\n%s", err, out.String())
|
||||
}
|
||||
if !env.OK {
|
||||
t.Fatalf("ok=false: %s", out.String())
|
||||
if got.Answer != "Hello world." {
|
||||
t.Errorf("answer = %q, want %q", got.Answer, "Hello world.")
|
||||
}
|
||||
if env.Data.Answer != "Hello world." {
|
||||
t.Errorf("answer = %q, want %q", env.Data.Answer, "Hello world.")
|
||||
if got.AgentID != "ag_x" {
|
||||
t.Errorf("agent_id = %q, want ag_x", got.AgentID)
|
||||
}
|
||||
if env.Data.AgentID != "ag_x" {
|
||||
t.Errorf("agent_id = %q, want ag_x", env.Data.AgentID)
|
||||
if got.Query != "ping" {
|
||||
t.Errorf("query = %q, want ping", got.Query)
|
||||
}
|
||||
if env.Data.Query != "ping" {
|
||||
t.Errorf("query = %q, want ping", env.Data.Query)
|
||||
if got.SessionID != "sess_auto" {
|
||||
t.Errorf("session_id = %q, want sess_auto", got.SessionID)
|
||||
}
|
||||
if env.Data.SessionID != "sess_auto" {
|
||||
t.Errorf("session_id = %q, want sess_auto", env.Data.SessionID)
|
||||
}
|
||||
if len(env.Data.References) != 1 || env.Data.References[0].KnowledgeID != "k1" {
|
||||
t.Errorf("references missing: %+v", env.Data.References)
|
||||
if len(got.References) != 1 || got.References[0].KnowledgeID != "k1" {
|
||||
t.Errorf("references missing: %+v", got.References)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,17 +155,15 @@ func TestInvoke_ToolEventsCaptured(t *testing.T) {
|
||||
if err := runInvoke(context.Background(), opts, &cmdutil.JSONOptions{}, svc); err != nil {
|
||||
t.Fatalf("runInvoke: %v", err)
|
||||
}
|
||||
var env struct {
|
||||
Data invokeData `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(out.Bytes(), &env); err != nil {
|
||||
var got invokeData
|
||||
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
if len(env.Data.ToolEvents) != 1 {
|
||||
t.Fatalf("expected 1 tool call, got %d", len(env.Data.ToolEvents))
|
||||
if len(got.ToolEvents) != 1 {
|
||||
t.Fatalf("expected 1 tool call, got %d", len(got.ToolEvents))
|
||||
}
|
||||
if env.Data.ToolEvents[0].ID != "call_1" {
|
||||
t.Errorf("tool_calls[0].id = %q, want call_1", env.Data.ToolEvents[0].ID)
|
||||
if got.ToolEvents[0].ID != "call_1" {
|
||||
t.Errorf("tool_calls[0].id = %q, want call_1", got.ToolEvents[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-11
@@ -39,11 +39,6 @@ type ListOptions struct {
|
||||
Limit int
|
||||
}
|
||||
|
||||
// listResult is the typed payload emitted under data.items.
|
||||
type listResult struct {
|
||||
Items []sdk.Agent `json:"items"`
|
||||
}
|
||||
|
||||
// NewCmdList builds `weknora agent list`.
|
||||
func NewCmdList(f *cmdutil.Factory) *cobra.Command {
|
||||
opts := &ListOptions{}
|
||||
@@ -65,7 +60,7 @@ func NewCmdList(f *cmdutil.Factory) *cobra.Command {
|
||||
}
|
||||
cmd.Flags().IntVarP(&opts.Limit, "limit", "L", 30, "Maximum results to return (0 = no cap, 1..10000 = explicit)")
|
||||
cmdutil.AddJSONFlags(cmd, agentListFields)
|
||||
aiclient.SetAgentHelp(cmd, "Lists tenant-visible agents (built-in + custom). Returns data.items: [{id, name, description, ...}]; empty array when none. --limit caps the returned slice. Use --json id,name to project, --jq for arbitrary reshape.")
|
||||
aiclient.SetAgentHelp(cmd, "Lists tenant-visible agents (built-in + custom) as a bare JSON array of Agent objects (empty `[]` when none). --limit caps the returned slice. Use `--json id,name` to project fields, `--jq` for arbitrary reshape.")
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -97,11 +92,7 @@ func runList(ctx context.Context, opts *ListOptions, jopts *cmdutil.JSONOptions,
|
||||
}
|
||||
|
||||
if jopts.Enabled() {
|
||||
return format.WriteEnvelopeFiltered(
|
||||
iostreams.IO.Out,
|
||||
format.Success(listResult{Items: items}, nil),
|
||||
jopts.Fields, jopts.JQ,
|
||||
)
|
||||
return format.WriteJSONFiltered(iostreams.IO.Out, items, jopts.Fields, jopts.JQ)
|
||||
}
|
||||
|
||||
if len(items) == 0 {
|
||||
|
||||
+12
-20
@@ -38,8 +38,8 @@ func TestList_Empty_JSON(t *testing.T) {
|
||||
if err := runList(context.Background(), &ListOptions{}, &cmdutil.JSONOptions{}, &fakeListSvc{}); err != nil {
|
||||
t.Fatalf("runList: %v", err)
|
||||
}
|
||||
if !strings.Contains(out.String(), `"items":[]`) {
|
||||
t.Errorf("expected items:[], got %q", out.String())
|
||||
if got := strings.TrimSpace(out.String()); got != "[]" {
|
||||
t.Errorf("expected bare `[]`, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,21 +72,17 @@ func TestList_NonEmpty_JSON_SortsByUpdatedAtDesc(t *testing.T) {
|
||||
if err := runList(context.Background(), &ListOptions{}, &cmdutil.JSONOptions{}, &fakeListSvc{items: items}); err != nil {
|
||||
t.Fatalf("runList: %v", err)
|
||||
}
|
||||
var env struct {
|
||||
Data struct {
|
||||
Items []sdk.Agent `json:"items"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(out.Bytes(), &env); err != nil {
|
||||
var got []sdk.Agent
|
||||
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
if len(env.Data.Items) != 3 {
|
||||
t.Fatalf("len = %d, want 3", len(env.Data.Items))
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("len = %d, want 3", len(got))
|
||||
}
|
||||
wantOrder := []string{"ag_new", "ag_mid", "ag_old"}
|
||||
for i, w := range wantOrder {
|
||||
if env.Data.Items[i].ID != w {
|
||||
t.Errorf("position %d: got %s, want %s (updated_at desc)", i, env.Data.Items[i].ID, w)
|
||||
if got[i].ID != w {
|
||||
t.Errorf("position %d: got %s, want %s (updated_at desc)", i, got[i].ID, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,16 +96,12 @@ func TestList_JSON_FieldFilter(t *testing.T) {
|
||||
if err := runList(context.Background(), &ListOptions{}, jopts, &fakeListSvc{items: items}); err != nil {
|
||||
t.Fatalf("runList: %v", err)
|
||||
}
|
||||
var env struct {
|
||||
Data struct {
|
||||
Items []map[string]any `json:"items"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(out.Bytes(), &env); err != nil {
|
||||
var got []map[string]any
|
||||
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
if _, has := env.Data.Items[0]["description"]; has {
|
||||
t.Errorf("description should be filtered out: %+v", env.Data.Items[0])
|
||||
if _, has := got[0]["description"]; has {
|
||||
t.Errorf("description should be filtered out: %+v", got[0])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-10
@@ -15,9 +15,9 @@ import (
|
||||
)
|
||||
|
||||
// agentViewFields enumerates fields surfaced for `--json` discovery on
|
||||
// `agent view`. Single-resource shape: filter applies to data itself.
|
||||
// Config sub-fields are intentionally omitted — too granular for naked
|
||||
// projection; use `--jq '.data.config'` to reach them.
|
||||
// `agent view`. Filter applies to the bare Agent object. Config sub-fields
|
||||
// are intentionally omitted — too granular for naked projection; use
|
||||
// `--jq '.config'` to reach them.
|
||||
var agentViewFields = []string{
|
||||
"id", "name", "description", "avatar",
|
||||
"is_builtin", "tenant_id", "created_by",
|
||||
@@ -36,10 +36,10 @@ func NewCmdView(f *cmdutil.Factory) *cobra.Command {
|
||||
Short: "Show a custom agent's configuration",
|
||||
Long: `Renders the agent's metadata (id / name / description / created-by /
|
||||
timestamps) plus a compact config summary (mode, model, allowed tools, KB
|
||||
scope). Pass --json for the full envelope including the nested config
|
||||
struct — or --jq '.data.config' to extract just the config.`,
|
||||
scope). Pass --json for the full Agent object including the nested config
|
||||
struct — or --jq '.config' to extract just the config.`,
|
||||
Example: ` weknora agent view ag_abc
|
||||
weknora agent view ag_abc --json | jq '.data.config.allowed_tools'`,
|
||||
weknora agent view ag_abc --json | jq '.config.allowed_tools'`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(c *cobra.Command, args []string) error {
|
||||
jopts, err := cmdutil.CheckJSONFlags(c)
|
||||
@@ -54,7 +54,7 @@ struct — or --jq '.data.config' to extract just the config.`,
|
||||
},
|
||||
}
|
||||
cmdutil.AddJSONFlags(cmd, agentViewFields)
|
||||
aiclient.SetAgentHelp(cmd, "Fetches an agent by ID. Returns data: full sdk.Agent (with nested config). Errors: resource.not_found when the agent ID does not exist or is not visible to the active tenant.")
|
||||
aiclient.SetAgentHelp(cmd, "Fetches an agent by ID. Returns the full sdk.Agent (with nested config) as a bare JSON object. Errors: resource.not_found when the agent ID does not exist or is not visible to the active tenant.")
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -64,9 +64,7 @@ func runView(ctx context.Context, jopts *cmdutil.JSONOptions, svc ViewService, a
|
||||
return cmdutil.WrapHTTP(err, "fetch agent %s", agentID)
|
||||
}
|
||||
if jopts.Enabled() {
|
||||
return format.WriteEnvelopeFiltered(iostreams.IO.Out,
|
||||
format.Success(a, nil),
|
||||
jopts.Fields, jopts.JQ)
|
||||
return format.WriteJSONFiltered(iostreams.IO.Out, a, jopts.Fields, jopts.JQ)
|
||||
}
|
||||
renderAgent(iostreams.IO.Out, a)
|
||||
return nil
|
||||
|
||||
@@ -74,21 +74,21 @@ func TestView_Human_OmitsEmptyFields(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestView_JSON_Envelope(t *testing.T) {
|
||||
func TestView_JSON_BareObject(t *testing.T) {
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
svc := &fakeViewSvc{resp: &sdk.Agent{ID: "ag_json", Name: "JSONy"}}
|
||||
if err := runView(context.Background(), &cmdutil.JSONOptions{}, svc, "ag_json"); err != nil {
|
||||
t.Fatalf("runView: %v", err)
|
||||
}
|
||||
var env struct {
|
||||
OK bool `json:"ok"`
|
||||
Data sdk.Agent `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(out.Bytes(), &env); err != nil {
|
||||
var got sdk.Agent
|
||||
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
if !env.OK || env.Data.ID != "ag_json" {
|
||||
t.Errorf("envelope shape wrong: ok=%v id=%s", env.OK, env.Data.ID)
|
||||
if got.ID != "ag_json" || got.Name != "JSONy" {
|
||||
t.Errorf("bare object shape wrong: id=%s name=%s", got.ID, got.Name)
|
||||
}
|
||||
if strings.Contains(out.String(), `"ok":`) || strings.Contains(out.String(), `"data":`) {
|
||||
t.Errorf("bare output must not carry envelope keys, got %q", out.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user