feat(cli)!: add agent-first chat and session ask output modes
This commit is contained in:
+34
-34
@@ -431,22 +431,22 @@ type chatInput struct {
|
||||
KBID string `json:"kb_id" jsonschema:"knowledge base ID to chat against"`
|
||||
Query string `json:"query" jsonschema:"user query"`
|
||||
SessionID string `json:"session_id,omitempty" jsonschema:"existing session to continue; auto-created when empty"`
|
||||
Reference bool `json:"reference,omitempty" jsonschema:"include indexed references"`
|
||||
Verbose bool `json:"verbose,omitempty" jsonschema:"include reasoning, tools, and lifecycle events"`
|
||||
}
|
||||
|
||||
type chatOutput struct {
|
||||
Answer string `json:"answer"`
|
||||
References []*sdk.SearchResult `json:"references"`
|
||||
Thinking string `json:"thinking,omitempty"` // reasoning text from response_type=thinking; empty for non-reasoning models
|
||||
SessionID string `json:"session_id"`
|
||||
AssistantMessageID string `json:"assistant_message_id,omitempty"`
|
||||
KBID string `json:"kb_id"`
|
||||
Query string `json:"query"`
|
||||
Events []sse.ProjectedEvent `json:"events"`
|
||||
SessionID string `json:"session_id"`
|
||||
AssistantMessageID string `json:"assistant_message_id,omitempty"`
|
||||
KBID string `json:"kb_id"`
|
||||
Query string `json:"query"`
|
||||
}
|
||||
|
||||
func addChat(server *mcpsdk.Server, svc chatService) {
|
||||
mcpsdk.AddTool(server, &mcpsdk.Tool{
|
||||
Name: "chat",
|
||||
Description: "Stream a RAG answer from the LLM, grounded in the given knowledge base. The SSE stream is accumulated server-side (MCP tools/call has no standard partial-response, so this is NOT streaming); the tool returns the full accumulated response once the stream completes. Pass session_id to continue a multi-turn conversation; otherwise a fresh session is auto-created.",
|
||||
Description: "Run a RAG answer against a knowledge base. Returns a bounded answer-event projection by default; reference=true adds indexed citations and verbose=true adds reasoning, tool, and lifecycle events. MCP tools/call is buffered rather than streaming. Pass session_id to continue a conversation.",
|
||||
Annotations: &mcpsdk.ToolAnnotations{
|
||||
Title: "Chat with KB (Streaming RAG)",
|
||||
DestructiveHint: bptr(false),
|
||||
@@ -475,27 +475,28 @@ func addChat(server *mcpsdk.Server, svc chatService) {
|
||||
AgentEnabled: false,
|
||||
Channel: "api",
|
||||
}
|
||||
acc := &sse.Accumulator{}
|
||||
projector := sse.NewProjector(in.Verbose, in.Reference, in.KBID)
|
||||
events := make([]sse.ProjectedEvent, 0)
|
||||
streamErr := svc.KnowledgeQAStream(ctx, sessionID, req, func(r *sdk.StreamResponse) error {
|
||||
acc.Append(r)
|
||||
if event, include := projector.Chat(r); include {
|
||||
events = append(events, event)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if streamErr != nil {
|
||||
return toolErrorResult(cmdutil.WrapHTTP(streamErr, "knowledge qa stream")), nil, nil
|
||||
}
|
||||
if !acc.Done() {
|
||||
if !projector.Done() {
|
||||
return toolErrorResult(cmdutil.NewError(cmdutil.CodeSSEStreamAborted, "stream ended without a terminal event")), nil, nil
|
||||
}
|
||||
sid := acc.SessionID
|
||||
sid := projector.SessionID()
|
||||
if sid == "" {
|
||||
sid = sessionID
|
||||
}
|
||||
return successResult(chatOutput{
|
||||
Answer: acc.Result(),
|
||||
References: acc.References,
|
||||
Thinking: acc.Thinking(),
|
||||
Events: events,
|
||||
SessionID: sid,
|
||||
AssistantMessageID: acc.AssistantMessageID,
|
||||
AssistantMessageID: projector.AssistantMessageID(),
|
||||
KBID: in.KBID,
|
||||
Query: in.Query,
|
||||
}), nil, nil
|
||||
@@ -539,22 +540,21 @@ type sessionAskInput struct {
|
||||
AgentID string `json:"agent_id" jsonschema:"custom agent ID"`
|
||||
Query string `json:"query" jsonschema:"user query"`
|
||||
SessionID string `json:"session_id,omitempty" jsonschema:"existing session to continue; auto-created when empty"`
|
||||
Reference bool `json:"reference,omitempty" jsonschema:"include indexed references"`
|
||||
Verbose bool `json:"verbose,omitempty" jsonschema:"include reasoning, tools, and lifecycle events"`
|
||||
}
|
||||
|
||||
type sessionAskOutput struct {
|
||||
Answer string `json:"answer"`
|
||||
References []*sdk.SearchResult `json:"references"`
|
||||
ToolEvents []sse.AgentToolEvent `json:"tool_events,omitempty"`
|
||||
Thinking string `json:"thinking,omitempty"`
|
||||
SessionID string `json:"session_id"`
|
||||
AgentID string `json:"agent_id"`
|
||||
Query string `json:"query"`
|
||||
Events []sse.ProjectedEvent `json:"events"`
|
||||
SessionID string `json:"session_id"`
|
||||
AgentID string `json:"agent_id"`
|
||||
Query string `json:"query"`
|
||||
}
|
||||
|
||||
func addSessionAsk(server *mcpsdk.Server, svc sessionAskService) {
|
||||
mcpsdk.AddTool(server, &mcpsdk.Tool{
|
||||
Name: "session_ask",
|
||||
Description: "Run a query through a custom agent via `session ask --agent` (system prompt + tool allow-list + KB scope). The agent's SSE stream is accumulated server-side (MCP tools/call has no standard partial-response, so this is NOT streaming); the tool returns the final accumulated response once the stream completes.",
|
||||
Description: "Run a query through a custom agent. Returns a bounded answer-event projection by default; reference=true adds indexed citations and verbose=true adds reasoning, tool, and lifecycle events. MCP tools/call is buffered rather than streaming.",
|
||||
Annotations: &mcpsdk.ToolAnnotations{
|
||||
Title: "Ask a Custom Agent (session ask --agent)",
|
||||
DestructiveHint: bptr(false),
|
||||
@@ -569,7 +569,8 @@ func addSessionAsk(server *mcpsdk.Server, svc sessionAskService) {
|
||||
if strings.TrimSpace(in.Query) == "" {
|
||||
return toolErrorResult(cmdutil.NewError(cmdutil.CodeInputMissingFlag, "query cannot be empty")), nil, nil
|
||||
}
|
||||
acc := &sse.AgentAccumulator{}
|
||||
projector := sse.NewProjector(in.Verbose, in.Reference, "")
|
||||
events := make([]sse.ProjectedEvent, 0)
|
||||
req := &sdk.AgentQARequest{
|
||||
Query: in.Query,
|
||||
AgentEnabled: true,
|
||||
@@ -587,23 +588,22 @@ func addSessionAsk(server *mcpsdk.Server, svc sessionAskService) {
|
||||
sessionID = sess.ID
|
||||
}
|
||||
streamErr := svc.AgentQAStreamWithRequest(ctx, sessionID, req, func(r *sdk.AgentStreamResponse) error {
|
||||
acc.Append(r)
|
||||
if event, include := projector.Agent(r); include {
|
||||
events = append(events, event)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if streamErr != nil {
|
||||
return toolErrorResult(cmdutil.WrapHTTP(streamErr, "agent-chat stream")), nil, nil
|
||||
}
|
||||
if !acc.Done() {
|
||||
if !projector.Done() {
|
||||
return toolErrorResult(cmdutil.NewError(cmdutil.CodeSSEStreamAborted, "stream ended without a terminal event")), nil, nil
|
||||
}
|
||||
return successResult(sessionAskOutput{
|
||||
Answer: acc.Answer(),
|
||||
References: acc.References,
|
||||
ToolEvents: acc.ToolEvents,
|
||||
Thinking: acc.Thinking(),
|
||||
SessionID: sessionID,
|
||||
AgentID: in.AgentID,
|
||||
Query: in.Query,
|
||||
Events: events,
|
||||
SessionID: sessionID,
|
||||
AgentID: in.AgentID,
|
||||
Query: in.Query,
|
||||
}), nil, nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -423,30 +423,37 @@ func TestTool_SearchChunks_PassesMatchCountFromLimit(t *testing.T) {
|
||||
assert.Equal(t, 50, svc.calls.hybridParams.MatchCount, "MCP search_chunks must thread limit into SearchParams.MatchCount")
|
||||
}
|
||||
|
||||
func TestTool_Chat_AccumulateAnswerAndReferences(t *testing.T) {
|
||||
func TestTool_Chat_DefaultReturnsAnswerEventsOnly(t *testing.T) {
|
||||
svc := &fakeSvc{
|
||||
kbStreamEvents: []*sdk.StreamResponse{
|
||||
{Content: "Hello "},
|
||||
{Content: "world."},
|
||||
{KnowledgeReferences: []*sdk.SearchResult{{KnowledgeID: "k1"}}},
|
||||
{KnowledgeReferences: []*sdk.SearchResult{{
|
||||
ID: "c1",
|
||||
KnowledgeID: "k1",
|
||||
ParentChunkID: "p1",
|
||||
Content: "bulky passage",
|
||||
}}},
|
||||
{ResponseType: sdk.ResponseTypeComplete},
|
||||
},
|
||||
}
|
||||
c, _ := newTestServer(t, svc)
|
||||
var out chatOutput
|
||||
callTool(t, c, "chat", map[string]any{"kb_id": "kb_x", "query": "ping"}, &out)
|
||||
if out.Answer != "Hello world." {
|
||||
t.Errorf("answer = %q", out.Answer)
|
||||
if len(out.Events) != 2 || out.Events[0].Content != "Hello " || out.Events[1].Content != "world." {
|
||||
t.Errorf("answer events=%+v", out.Events)
|
||||
}
|
||||
if len(out.References) != 1 || out.References[0].KnowledgeID != "k1" {
|
||||
t.Errorf("references missing: %+v", out.References)
|
||||
for _, event := range out.Events {
|
||||
if event.ResponseType != "answer" || len(event.KnowledgeReferences) != 0 {
|
||||
t.Errorf("default output leaked non-answer data: %+v", event)
|
||||
}
|
||||
}
|
||||
if out.SessionID != "sess_auto" {
|
||||
t.Errorf("session_id = %q, want sess_auto", out.SessionID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMCP_ChatToolReturnsThinking(t *testing.T) {
|
||||
func TestMCP_ChatVerbosePreservesThinking(t *testing.T) {
|
||||
svc := &fakeSvc{
|
||||
kbStreamEvents: []*sdk.StreamResponse{
|
||||
{ResponseType: sdk.ResponseTypeThinking, Content: "let me reason..."},
|
||||
@@ -456,12 +463,15 @@ func TestMCP_ChatToolReturnsThinking(t *testing.T) {
|
||||
}
|
||||
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...")
|
||||
callTool(t, c, "chat", map[string]any{"kb_id": "kb_x", "query": "deep question", "verbose": true}, &out)
|
||||
want := []string{"thinking", "answer", "complete"}
|
||||
if len(out.Events) != len(want) {
|
||||
t.Fatalf("events=%+v", out.Events)
|
||||
}
|
||||
if out.Answer != "final answer" {
|
||||
t.Errorf("answer = %q, want %q", out.Answer, "final answer")
|
||||
for i, responseType := range want {
|
||||
if out.Events[i].ResponseType != responseType {
|
||||
t.Errorf("events[%d]=%q, want %q", i, out.Events[i].ResponseType, responseType)
|
||||
}
|
||||
}
|
||||
if out.KBID != "kb_x" {
|
||||
t.Errorf("kb_id = %q, want %q", out.KBID, "kb_x")
|
||||
@@ -471,23 +481,35 @@ func TestMCP_ChatToolReturnsThinking(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMCP_SessionAskToolReturnsToolCalls(t *testing.T) {
|
||||
func TestMCP_SessionAskVerboseAndReferenceReturnsBothDetailClasses(t *testing.T) {
|
||||
svc := &fakeSvc{
|
||||
agentEvents: []*sdk.AgentStreamResponse{
|
||||
{ResponseType: sdk.AgentResponseTypeThinking, Content: "agent thinks"},
|
||||
{ResponseType: sdk.AgentResponseTypeToolCall, ID: "tc1", Content: "knowledge_search"},
|
||||
{ResponseType: sdk.AgentResponseTypeReferences, KnowledgeReferences: []*sdk.SearchResult{{
|
||||
ID: "c1",
|
||||
ParentChunkID: "p1",
|
||||
Content: "bulky passage",
|
||||
}}},
|
||||
{ResponseType: sdk.AgentResponseTypeAnswer, Content: "agent answer"},
|
||||
{ResponseType: sdk.AgentResponseTypeComplete, Done: true},
|
||||
},
|
||||
}
|
||||
c, _ := newTestServer(t, svc)
|
||||
var out sessionAskOutput
|
||||
callTool(t, c, "session_ask", 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")
|
||||
callTool(t, c, "session_ask", map[string]any{"agent_id": "ag1", "query": "tool question", "verbose": true, "reference": true}, &out)
|
||||
want := []string{"thinking", "tool_call", "references", "answer", "complete"}
|
||||
if len(out.Events) != len(want) {
|
||||
t.Fatalf("events=%+v", out.Events)
|
||||
}
|
||||
if len(out.ToolEvents) != 1 || out.ToolEvents[0].ID != "tc1" {
|
||||
t.Errorf("tool_events = %+v, want 1 event with id tc1", out.ToolEvents)
|
||||
for i, responseType := range want {
|
||||
if out.Events[i].ResponseType != responseType {
|
||||
t.Errorf("events[%d]=%q, want %q", i, out.Events[i].ResponseType, responseType)
|
||||
}
|
||||
}
|
||||
refs := out.Events[2].KnowledgeReferences
|
||||
if len(refs) != 1 || refs[0].ChunkID != "c1" || refs[0].ParentChunkID != "p1" {
|
||||
t.Errorf("reference indexes=%+v", refs)
|
||||
}
|
||||
if out.Query != "tool question" {
|
||||
t.Errorf("query = %q, want %q", out.Query, "tool question")
|
||||
@@ -529,11 +551,8 @@ func TestTool_SessionAsk(t *testing.T) {
|
||||
c, _ := newTestServer(t, svc)
|
||||
var out sessionAskOutput
|
||||
callTool(t, c, "session_ask", map[string]any{"agent_id": "ag1", "query": "x"}, &out)
|
||||
if out.Answer != "result" {
|
||||
t.Errorf("answer = %q", out.Answer)
|
||||
}
|
||||
if len(out.ToolEvents) != 1 {
|
||||
t.Errorf("tool_calls len = %d, want 1", len(out.ToolEvents))
|
||||
if len(out.Events) != 1 || out.Events[0].ResponseType != "answer" || out.Events[0].Content != "result" {
|
||||
t.Errorf("default events=%+v", out.Events)
|
||||
}
|
||||
if out.AgentID != "ag1" {
|
||||
t.Errorf("agent_id = %q", out.AgentID)
|
||||
|
||||
Reference in New Issue
Block a user