feat(http): add SSE streaming support via Client.Stream#107
Open
sunfuze wants to merge 1 commit into
Open
Conversation
Add server-sent events (SSE) support to the http package: - Client.Stream sends a request with "Accept: text/event-stream" and returns a pull-based *Stream; request/connection errors and non-200 responses (*ApiError) are returned synchronously. - Stream follows the bufio.Scanner / sql.Rows style: Next / Current / Err / Close, plus an Events() iter.Seq[[]byte] adapter for range-over-func consumption. Mid-stream read failures, timeouts and cancellation are reported by Err. - Streaming uses a copy of the configured http.Client with Timeout cleared, since http.Client.Timeout caps the whole request and would abort a long-lived stream; the lifetime is bounded by ctx instead. - Extract request building/signing from Call into newRequest, shared by Call and Stream. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add server-sent events (SSE) support to the
httppackage.Client.Streamsends a request withAccept: text/event-streamand returns a pull-based*Stream. Request build errors, connection failures and non-200 responses (*ApiError) are returned synchronously from the call.Streamfollows thebufio.Scanner/sql.Rowsidiom (same shape as anthropic-sdk-go / openai-go SSE streams):Next()/Current()/Err()/Close()— no background goroutine, no locks, natural backpressure. Mid-stream read failures, timeouts and cancellation are reported byErr.Events()returns aniter.Seq[[]byte](range-over-func) adapter yielding each event'sdatapayload — multipledata:lines are joined with\n; comments/heartbeats and non-data fields (event:/id:/retry:) are skipped.http.ClientwithTimeoutcleared, sincehttp.Client.Timeoutcaps the entire request (headers + body read) and would kill a long-lived stream; the stream's lifetime is bounded by the caller'sctxinstead.CallintonewRequest, shared byCallandStream(no behavior change forCall).Usage
Test plan
http/stream_test.go(httptest-based): multi-linedataevents, CRLF lines, comment/heartbeat skipping, trailing event without blank line, iterator + earlybreak, non-200 →*ApiError, context timeout surfaced viaErr(), connect failure,Close()idempotencyGOWORK=off go test ./http/ -race— all greenGOWORK=off go vet ./http/— clean🤖 Generated with Claude Code