streaming

package
v1.0.64 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package streaming provides utilities for streaming responses from AI providers.

Package streaming provides error types and utilities for streaming responses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteTrackingStream

type ByteTrackingStream struct {
	// contains filtered or unexported fields
}

ByteTrackingStream wraps a ChatCompletionStream and tracks bytes read vs bytes written. It helps detect data corruption or loss during streaming by comparing the raw bytes read from the stream with the bytes written to the client.

func NewByteTrackingStream

func NewByteTrackingStream(stream types.ChatCompletionStream) *ByteTrackingStream

NewByteTrackingStream creates a new ByteTrackingStream that wraps the given stream.

func (*ByteTrackingStream) BytesRead

func (b *ByteTrackingStream) BytesRead() int64

BytesRead returns the number of bytes read from the stream.

func (*ByteTrackingStream) BytesWritten

func (b *ByteTrackingStream) BytesWritten() int64

BytesWritten returns the number of bytes written (set externally).

func (*ByteTrackingStream) Close

func (b *ByteTrackingStream) Close() error

Close closes the underlying stream.

func (*ByteTrackingStream) HasMismatch

func (b *ByteTrackingStream) HasMismatch() bool

HasMismatch returns true if there is a mismatch between bytes read and written. Allows 1-byte tolerance for trailing newline.

func (*ByteTrackingStream) MarkBytesWritten

func (b *ByteTrackingStream) MarkBytesWritten(n int64)

MarkBytesWritten sets the number of bytes written.

func (*ByteTrackingStream) Next

Next returns the next chunk from the stream and tracks bytes.

type ErrorType

type ErrorType string

ErrorType represents the type of streaming error

const (
	ErrorTypeStreamInterrupted ErrorType = "stream_interrupted"
	ErrorTypeAPIError          ErrorType = "api_error"
	ErrorTypeRateLimit         ErrorType = "rate_limit"
	ErrorTypeNetwork           ErrorType = "network"
	ErrorTypeTimeout           ErrorType = "timeout"
	ErrorTypeContextCanceled   ErrorType = "context_canceled"
)

type StreamError

type StreamError struct {
	Type    ErrorType `json:"type"`
	Message string    `json:"message"`
}

StreamError represents an error during streaming

func MakeStreamError

func MakeStreamError(err error) StreamError

MakeStreamError creates a StreamError from an error

func (StreamError) ToSSEEvent

func (se StreamError) ToSSEEvent() string

ToSSEEvent converts the StreamError to an SSE event string Format: event: error\ndata: {"type":"...","message":"..."}\n\n

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL