fix(cli): align stream error handling after output-mode merge
Follow up on #1740/#1741 by typing terminal SSE errors in the SDK, classifying them as server.error in the CLI/MCP, marking Projector done on error frames, attaching session_id to MCP stream failures, and migrating the e2e chat step to the bounded JSON envelope.
This commit is contained in:
@@ -45,6 +45,18 @@ func toolErrorResult(err error) *mcpsdk.CallToolResult {
|
||||
}
|
||||
}
|
||||
|
||||
func streamErrorDetail(sessionID, assistantMessageID string) map[string]any {
|
||||
detail := map[string]any{"session_id": sessionID}
|
||||
if assistantMessageID != "" {
|
||||
detail["assistant_message_id"] = assistantMessageID
|
||||
}
|
||||
return detail
|
||||
}
|
||||
|
||||
func toolStreamError(err *cmdutil.Error, sessionID, assistantMessageID string) *mcpsdk.CallToolResult {
|
||||
return toolErrorResult(err.WithDetail(streamErrorDetail(sessionID, assistantMessageID)))
|
||||
}
|
||||
|
||||
// successResult builds a CallToolResult with StructuredContent = payload.
|
||||
// Using Out=any in all handlers disables the SDK auto-marshal path (which
|
||||
// would overwrite our StructuredContent with a zero-struct when the handler
|
||||
@@ -484,10 +496,10 @@ func addChat(server *mcpsdk.Server, svc chatService) {
|
||||
return nil
|
||||
})
|
||||
if streamErr != nil {
|
||||
return toolErrorResult(cmdutil.WrapHTTP(streamErr, "knowledge qa stream")), nil, nil
|
||||
return toolStreamError(cmdutil.WrapStream(streamErr, "knowledge qa stream"), sessionID, projector.AssistantMessageID()), nil, nil
|
||||
}
|
||||
if !projector.Done() {
|
||||
return toolErrorResult(cmdutil.NewError(cmdutil.CodeSSEStreamAborted, "stream ended without a terminal event")), nil, nil
|
||||
return toolStreamError(cmdutil.NewError(cmdutil.CodeSSEStreamAborted, "stream ended without a terminal event"), sessionID, projector.AssistantMessageID()), nil, nil
|
||||
}
|
||||
sid := projector.SessionID()
|
||||
if sid == "" {
|
||||
@@ -594,10 +606,10 @@ func addSessionAsk(server *mcpsdk.Server, svc sessionAskService) {
|
||||
return nil
|
||||
})
|
||||
if streamErr != nil {
|
||||
return toolErrorResult(cmdutil.WrapHTTP(streamErr, "agent-chat stream")), nil, nil
|
||||
return toolStreamError(cmdutil.WrapStream(streamErr, "agent-chat stream"), sessionID, ""), nil, nil
|
||||
}
|
||||
if !projector.Done() {
|
||||
return toolErrorResult(cmdutil.NewError(cmdutil.CodeSSEStreamAborted, "stream ended without a terminal event")), nil, nil
|
||||
return toolStreamError(cmdutil.NewError(cmdutil.CodeSSEStreamAborted, "stream ended without a terminal event"), sessionID, ""), nil, nil
|
||||
}
|
||||
return successResult(sessionAskOutput{
|
||||
Events: events,
|
||||
|
||||
@@ -576,6 +576,43 @@ func TestTool_SessionAsk_StreamAbort(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTool_Chat_StreamErrorIncludesSessionDetail(t *testing.T) {
|
||||
svc := &fakeSvc{
|
||||
kbStreamEvents: []*sdk.StreamResponse{
|
||||
{ResponseType: sdk.ResponseTypeAnswer, Content: "partial"},
|
||||
{ResponseType: sdk.ResponseTypeError, Content: "boom", Done: true},
|
||||
},
|
||||
kbStreamErr: sdk.NewSSEStreamError("boom"),
|
||||
}
|
||||
c, _ := newTestServer(t, svc)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
res, err := c.CallTool(ctx, &mcpsdk.CallToolParams{Name: "chat", Arguments: map[string]any{"kb_id": "kb_x", "query": "q"}})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected transport error: %v", err)
|
||||
}
|
||||
if !res.IsError {
|
||||
t.Fatal("expected IsError=true on terminal stream error")
|
||||
}
|
||||
b, err := json.Marshal(res.StructuredContent)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal structured content: %v", err)
|
||||
}
|
||||
var detail struct {
|
||||
Type string `json:"type"`
|
||||
Detail map[string]any `json:"detail"`
|
||||
}
|
||||
if err := json.Unmarshal(b, &detail); err != nil {
|
||||
t.Fatalf("unmarshal error detail: %v", err)
|
||||
}
|
||||
if detail.Type != "server.error" {
|
||||
t.Errorf("type=%q, want server.error", detail.Type)
|
||||
}
|
||||
if detail.Detail["session_id"] != "sess_auto" {
|
||||
t.Errorf("detail=%v, want session_id sess_auto", detail.Detail)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTool_ChunkList_Happy(t *testing.T) {
|
||||
svc := &fakeSvc{
|
||||
chunks: []sdk.Chunk{{ID: "c1", ChunkIndex: 0, Content: "hello"}},
|
||||
|
||||
Reference in New Issue
Block a user