feat(cli): --format / NDJSON / chat & agent invoke / MCP / SetAgentHelp / signal-aware ctx / kb create --storage-provider
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".
This commit is contained in:
@@ -326,7 +326,7 @@ func TestTool_DocView(t *testing.T) {
|
||||
svc := &fakeSvc{getDoc: &sdk.Knowledge{ID: "k1", FileName: "a.pdf"}}
|
||||
c, _ := newTestServer(t, svc)
|
||||
var out sdk.Knowledge
|
||||
callTool(t, c, "doc_view", map[string]any{"knowledge_id": "k1"}, &out)
|
||||
callTool(t, c, "doc_view", map[string]any{"doc_id": "k1"}, &out)
|
||||
if out.ID != "k1" {
|
||||
t.Errorf("got %+v", out)
|
||||
}
|
||||
@@ -339,7 +339,7 @@ func TestTool_DocDownload_Text(t *testing.T) {
|
||||
}
|
||||
c, _ := newTestServer(t, svc)
|
||||
var out docDownloadOutput
|
||||
callTool(t, c, "doc_download", map[string]any{"knowledge_id": "k1"}, &out)
|
||||
callTool(t, c, "doc_download", map[string]any{"doc_id": "k1"}, &out)
|
||||
if out.Content != "hello world" {
|
||||
t.Errorf("content = %q", out.Content)
|
||||
}
|
||||
@@ -357,7 +357,7 @@ func TestTool_DocDownload_BinaryBase64(t *testing.T) {
|
||||
}
|
||||
c, _ := newTestServer(t, svc)
|
||||
var out docDownloadOutput
|
||||
callTool(t, c, "doc_download", map[string]any{"knowledge_id": "k1"}, &out)
|
||||
callTool(t, c, "doc_download", map[string]any{"doc_id": "k1"}, &out)
|
||||
if !out.IsBase64 {
|
||||
t.Errorf("binary should be base64; got is_base64=%v content=%q", out.IsBase64, out.Content)
|
||||
}
|
||||
@@ -423,6 +423,54 @@ func TestTool_Chat_AccumulateAnswerAndReferences(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMCP_ChatToolReturnsThinking(t *testing.T) {
|
||||
svc := &fakeSvc{
|
||||
kbStreamEvents: []*sdk.StreamResponse{
|
||||
{ResponseType: sdk.ResponseTypeThinking, Content: "let me reason..."},
|
||||
{ResponseType: sdk.ResponseTypeAnswer, Content: "final answer"},
|
||||
{ResponseType: sdk.ResponseTypeComplete},
|
||||
},
|
||||
}
|
||||
c, _ := newTestServer(t, svc)
|
||||
var out chatOutput
|
||||
callTool(t, c, "chat", map[string]any{"kb_id": "kb_x", "query": "deep question"}, &out)
|
||||
if out.Thinking != "let me reason..." {
|
||||
t.Errorf("thinking = %q, want %q", out.Thinking, "let me reason...")
|
||||
}
|
||||
if out.Answer != "final answer" {
|
||||
t.Errorf("answer = %q, want %q", out.Answer, "final answer")
|
||||
}
|
||||
if out.KBID != "kb_x" {
|
||||
t.Errorf("kb_id = %q, want %q", out.KBID, "kb_x")
|
||||
}
|
||||
if out.Query != "deep question" {
|
||||
t.Errorf("query = %q, want %q", out.Query, "deep question")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMCP_AgentInvokeToolReturnsToolCalls(t *testing.T) {
|
||||
svc := &fakeSvc{
|
||||
agentEvents: []*sdk.AgentStreamResponse{
|
||||
{ResponseType: sdk.AgentResponseTypeThinking, Content: "agent thinks"},
|
||||
{ResponseType: sdk.AgentResponseTypeToolCall, ID: "tc1", Content: "knowledge_search"},
|
||||
{ResponseType: sdk.AgentResponseTypeAnswer, Content: "agent answer"},
|
||||
{Done: true},
|
||||
},
|
||||
}
|
||||
c, _ := newTestServer(t, svc)
|
||||
var out agentInvokeOutput
|
||||
callTool(t, c, "agent_invoke", map[string]any{"agent_id": "ag1", "query": "tool question"}, &out)
|
||||
if out.Thinking != "agent thinks" {
|
||||
t.Errorf("thinking = %q, want %q", out.Thinking, "agent thinks")
|
||||
}
|
||||
if len(out.ToolEvents) != 1 || out.ToolEvents[0].ID != "tc1" {
|
||||
t.Errorf("tool_events = %+v, want 1 event with id tc1", out.ToolEvents)
|
||||
}
|
||||
if out.Query != "tool question" {
|
||||
t.Errorf("query = %q, want %q", out.Query, "tool question")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTool_Chat_ExistingSessionSkipsCreate(t *testing.T) {
|
||||
svc := &fakeSvc{
|
||||
kbStreamEvents: []*sdk.StreamResponse{{ResponseType: sdk.ResponseTypeComplete}},
|
||||
|
||||
Reference in New Issue
Block a user