567d7ac74e
Adds the structured-output and agent-help surface plus root-level signal
handling so AI agents (and humans working through pipes) get a stable
wire contract.
* --format text|json|ndjson flag, registered per-command on outputs that
need it; default text on TTY, json on pipe. --jq <expr> pairs with json
/ ndjson to filter or project. FormatOptions absorbs JQ; WantsJSON()
helper for the JSON dispatch.
* WriteNDJSON helper in internal/format/ (per ndjson.org: one JSON value
per line, arrays split element-per-line, empty slice → zero bytes).
* chat / agent invoke wire --format ndjson via SDK StreamResponse /
AgentStreamResponse 1:1 passthrough. Both commands detect ctx.Cancelled
in every stream + session-create path and emit a stable
"operation.cancelled" code on Ctrl-C / SIGTERM.
* main.go wires signal.NotifyContext(SIGINT, SIGTERM) into the root
context so long-running commands run their cancellation cleanup
(re-emit auto-created session id, etc); the process exits 130 when
the context was signal-cancelled, matching Unix convention.
* MCP chat / agent_invoke output schemas extended with thinking /
tool_calls / assistant_message_id (server-side accumulated; MCP
tools/call has no standard partial-response). doc_view and doc_download
now use doc_id (not knowledge_id) so agents see a single id naming
convention across all tools — matches the chunk_list / search_chunks
schemas and the CLI's <doc-id> positional.
* SetAgentHelp(cmd, AgentHelp{...}) — opt-in machine-friendly --help
payload activated by WEKNORA_AGENT_HELP=1. Applied to chat / kb list.
* kb create --storage-provider <local|minio|cos|tos|s3|oss|ks3> — sets
the new KB's storage_provider_config.provider at creation time (server
does not expose it on update). Required on self-hosted deployments
where the server-side default doesn't pre-populate a provider —
without it, subsequent doc upload returns a misleading "kb not found".
147 lines
4.1 KiB
Go
147 lines
4.1 KiB
Go
package cmdutil
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func TestCheckFormatFlags(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
args []string
|
|
wantMode string // "text" | "json" | "ndjson" | "" (unset)
|
|
wantErr bool
|
|
}{
|
|
// Unset → Mode is "" (caller resolves TTY default via ResolveDefault).
|
|
{"default", []string{}, "", false},
|
|
{"explicit text", []string{"--format", "text"}, "text", false},
|
|
{"json", []string{"--format", "json"}, "json", false},
|
|
{"ndjson", []string{"--format", "ndjson"}, "ndjson", false},
|
|
{"invalid value", []string{"--format", "yaml"}, "", true},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
cmd := &cobra.Command{}
|
|
AddFormatFlag(cmd)
|
|
cmd.SetArgs(tc.args)
|
|
cmd.RunE = func(c *cobra.Command, _ []string) error {
|
|
opts, err := CheckFormatFlag(c)
|
|
if (err != nil) != tc.wantErr {
|
|
t.Fatalf("err=%v wantErr=%v", err, tc.wantErr)
|
|
}
|
|
if err == nil && opts.Mode != tc.wantMode {
|
|
t.Errorf("mode=%q want %q", opts.Mode, tc.wantMode)
|
|
}
|
|
return nil
|
|
}
|
|
_ = cmd.Execute()
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFormatOptions_NDJSONSplitsList(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
fopts := &FormatOptions{Mode: "ndjson"}
|
|
arr := []map[string]string{{"id": "a"}, {"id": "b"}}
|
|
if err := fopts.Emit(&buf, arr); err != nil {
|
|
t.Fatalf("Emit: %v", err)
|
|
}
|
|
want := `{"id":"a"}` + "\n" + `{"id":"b"}` + "\n"
|
|
if buf.String() != want {
|
|
t.Errorf("got %q want %q", buf.String(), want)
|
|
}
|
|
}
|
|
|
|
func TestFormatOptions_JSONEmitsArray(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
fopts := &FormatOptions{Mode: "json"}
|
|
arr := []map[string]string{{"id": "a"}, {"id": "b"}}
|
|
if err := fopts.Emit(&buf, arr); err != nil {
|
|
t.Fatalf("Emit: %v", err)
|
|
}
|
|
// Expect a single JSON array, e.g. [{"id":"a"},{"id":"b"}]
|
|
var got []map[string]string
|
|
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
|
t.Fatalf("not valid JSON: %v\n%s", err, buf.String())
|
|
}
|
|
if len(got) != 2 {
|
|
t.Errorf("got %d items, want 2", len(got))
|
|
}
|
|
}
|
|
|
|
func TestFormatOptions_TextModeReturnsError(t *testing.T) {
|
|
fopts := &FormatOptions{Mode: "text"}
|
|
err := fopts.Emit(&bytes.Buffer{}, map[string]string{"a": "b"})
|
|
if err == nil {
|
|
t.Error("expected error for text mode, got nil")
|
|
}
|
|
}
|
|
|
|
func TestResolveDefault(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
mode string // pre-set Mode
|
|
jq string
|
|
isTTY bool
|
|
wantMode string
|
|
}{
|
|
{"empty isTTY", "", "", true, "text"},
|
|
{"empty no-tty", "", "", false, "json"},
|
|
{"already set keeps value tty", "ndjson", "", true, "ndjson"},
|
|
{"already set keeps value no-tty", "json", "", false, "json"},
|
|
// --jq with unset --format promotes to JSON regardless of TTY so the
|
|
// filter has somewhere to apply (silent text drop would surprise users).
|
|
{"jq forces json on TTY", "", ".[]", true, "json"},
|
|
{"jq with explicit ndjson preserved", "ndjson", ".[]", true, "ndjson"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
o := &FormatOptions{Mode: tc.mode, JQ: tc.jq}
|
|
o.ResolveDefault(tc.isTTY)
|
|
if o.Mode != tc.wantMode {
|
|
t.Errorf("mode=%q want %q", o.Mode, tc.wantMode)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCheckFormatFlag_InvalidExitTwo guards the contract that flag-value
|
|
// validation maps to exit 2 (FlagError class), not the unclassified bucket.
|
|
func TestCheckFormatFlag_InvalidExitTwo(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
args []string
|
|
}{
|
|
{"invalid format value", []string{"--format", "yaml"}},
|
|
{"jq with explicit text mode", []string{"--format", "text", "--jq", ".id"}},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
cmd := &cobra.Command{}
|
|
AddFormatFlag(cmd)
|
|
cmd.SetArgs(tc.args)
|
|
var got error
|
|
cmd.RunE = func(c *cobra.Command, _ []string) error {
|
|
_, err := CheckFormatFlag(c)
|
|
got = err
|
|
return err
|
|
}
|
|
_ = cmd.Execute()
|
|
if got == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
var fe *FlagError
|
|
if !errors.As(got, &fe) {
|
|
t.Fatalf("error %v is not a *FlagError; would map to exit 1 instead of 2", got)
|
|
}
|
|
if ExitCode(got) != 2 {
|
|
t.Errorf("ExitCode=%d, want 2", ExitCode(got))
|
|
}
|
|
})
|
|
}
|
|
}
|