fix(anthropic): surface mid-stream error events

PC
3d ago
+196-15

Description

Anthropic can send event: error (for example overloaded_error) inside a 200 stream. The shared SSE line consumer decoded it, but neither converter had a case for it, so it was dropped: the chat stream ended with a normal data: [DONE], and the Responses stream ended with response.incomplete (reason: interrupted). The upstream message was lost in both cases.

Now:

  • Chat Completions: the stream relays what it already received, then an OpenAI-style {"error": ...} chunk with the upstream message and type as code, and no [DONE].
  • Responses: the stream ends with response.failed carrying the same code and message, plus the usage captured so far and the partial output (open items closed as incomplete). The usage and request log observers read response.failed.

Tests cover both converters with an error event after partial text; both fail without the change. The internal/providers/..., internal/anthropicapi and internal/testconventions packages pass; the only failures in internal/server are the dashboard-asset tests, which need make frontend and fail the same way on main.

AI Generated (optional)

This change was prepared with AI assistance.

Summary by CodeRabbit

  • Bug Fixes
    • Anthropic stream errors are now reported as failures instead of appearing as successful completions. Content received before an error is preserved, and upstream error details are included when available. Failed streams no longer include a success-ending event.
    • Failed responses retain available token usage information, improving usage reporting for streams that end with an error. Their output includes failure status and preserves response details, including any content received before the error.