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:
wizardchen
2026-07-03 00:27:48 +08:00
committed by lyingbug
parent 9437e1ac3c
commit 1109e85a57
19 changed files with 260 additions and 35 deletions
+1 -1
View File
@@ -130,7 +130,7 @@ func (c *Client) processAgentSSEStream(reader io.Reader, callback AgentEventCall
// follows it with `complete` or EOF. Deliver it to the callback
// first, then terminate the SDK call with an error.
if streamResponse.ResponseType == AgentResponseTypeError && streamResponse.Done {
return fmt.Errorf("SSE stream error: %s", streamResponse.Content)
return NewSSEStreamError(streamResponse.Content)
}
dataBuffer = ""
}
+2 -2
View File
@@ -321,7 +321,7 @@ func (c *Client) KnowledgeQAStream(
return err
}
if streamResponse.ResponseType == ResponseTypeError && streamResponse.Done {
return fmt.Errorf("SSE stream error: %s", streamResponse.Content)
return NewSSEStreamError(streamResponse.Content)
}
dataBuffer = ""
eventType = ""
@@ -396,7 +396,7 @@ func (c *Client) ContinueStream(
return err
}
if streamResponse.ResponseType == ResponseTypeError && streamResponse.Done {
return fmt.Errorf("SSE stream error: %s", streamResponse.Content)
return NewSSEStreamError(streamResponse.Content)
}
dataBuffer = ""
eventType = ""
+49
View File
@@ -0,0 +1,49 @@
package client
import (
"errors"
"fmt"
"strings"
)
// ErrSSEStreamTerminal marks a terminal response_type=error, done=true frame
// from an SSE stream. The stream delivered the frame to the callback before
// returning the error.
var ErrSSEStreamTerminal = errors.New("SSE stream terminal error")
// SSEStreamError is returned when the server emits a terminal error frame on
// an SSE stream.
type SSEStreamError struct {
Content string
}
func (e *SSEStreamError) Error() string {
return fmt.Sprintf("SSE stream error: %s", e.Content)
}
func (e *SSEStreamError) Unwrap() error {
return ErrSSEStreamTerminal
}
// NewSSEStreamError builds the terminal SSE stream error returned by the
// streaming readers after delivering the error frame to the callback.
func NewSSEStreamError(content string) error {
return &SSEStreamError{Content: content}
}
// IsSSEStreamError reports whether err is a terminal SSE stream error from the
// SDK readers, including wrapped *SSEStreamError values and legacy
// fmt.Errorf("SSE stream error: ...") chains.
func IsSSEStreamError(err error) bool {
var sse *SSEStreamError
if errors.As(err, &sse) {
return true
}
for err != nil {
if strings.HasPrefix(err.Error(), "SSE stream error: ") {
return true
}
err = errors.Unwrap(err)
}
return false
}
+35
View File
@@ -0,0 +1,35 @@
package client
import (
"errors"
"fmt"
"testing"
)
func TestIsSSEStreamError(t *testing.T) {
cases := []struct {
name string
err error
want bool
}{
{"typed", NewSSEStreamError("boom"), true},
{"wrapped", fmt.Errorf("request failed: %w", NewSSEStreamError("boom")), true},
{"legacy_string", fmt.Errorf("SSE stream error: boom"), true},
{"http", fmt.Errorf("HTTP error 500: internal"), false},
{"nil", nil, false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := IsSSEStreamError(tc.err); got != tc.want {
t.Errorf("IsSSEStreamError(%v) = %v, want %v", tc.err, got, tc.want)
}
})
}
}
func TestSSEStreamError_Unwrap(t *testing.T) {
err := NewSSEStreamError("boom")
if !errors.Is(err, ErrSSEStreamTerminal) {
t.Fatal("expected ErrSSEStreamTerminal in chain")
}
}