stream

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSourceRequired = errors.New("message source is required")
	ErrSourceClosed   = errors.New("message source is closed")
)
View Source
var SseHeaders = map[string]string{
	fiber.HeaderContentType:         "text/event-stream",
	fiber.HeaderCacheControl:        "no-cache",
	fiber.HeaderConnection:          "keep-alive",
	fiber.HeaderTransferEncoding:    "chunked",
	"X-Vercel-AI-UI-Message-Stream": "v1",
	"X-Accel-Buffering":             "no",
}

SseHeaders contains the standard headers for AI SDK UI Message Stream.

Functions

This section is empty.

Types

type Builder

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

Builder provides a fluent interface for building UI message streams.

func FromAiMessageStream

func FromAiMessageStream(stream ai.MessageStream) *Builder

FromAiMessageStream creates a Builder with an ai.MessageStream as the source.

func FromCallback

func FromCallback(execute func(writer CallbackWriter) error) *Builder

FromCallback creates a Builder with a callback-based source.

func FromChannel

func FromChannel(ch <-chan Message) *Builder

FromChannel creates a Builder with a channel as the source.

func New

func New() *Builder

New creates a new stream builder with default options.

func (*Builder) OnError

func (b *Builder) OnError(handler func(err error) string) *Builder

func (*Builder) OnFinish

func (b *Builder) OnFinish(handler func(content string)) *Builder

func (*Builder) Stream

func (b *Builder) Stream(ctx fiber.Ctx) error

Stream executes the stream and writes to a Fiber context.

func (*Builder) StreamToWriter

func (b *Builder) StreamToWriter(w *bufio.Writer)

StreamToWriter streams messages to a bufio.Writer.

func (*Builder) WithFinish

func (b *Builder) WithFinish(enabled bool) *Builder

func (*Builder) WithHeader

func (b *Builder) WithHeader(key, value string) *Builder

func (*Builder) WithIDGenerator

func (b *Builder) WithIDGenerator(gen func(prefix string) string) *Builder

func (*Builder) WithMessageID

func (b *Builder) WithMessageID(id string) *Builder

func (*Builder) WithReasoning

func (b *Builder) WithReasoning(enabled bool) *Builder

func (*Builder) WithSource

func (b *Builder) WithSource(source MessageSource) *Builder

func (*Builder) WithSources

func (b *Builder) WithSources(enabled bool) *Builder

func (*Builder) WithStart

func (b *Builder) WithStart(enabled bool) *Builder

type CallbackWriter

type CallbackWriter interface {
	// WriteText sends a text content message as the assistant role.
	WriteText(content string)
	// WriteToolCall sends a tool call message with the given id, function name, and JSON arguments.
	WriteToolCall(id, name, arguments string)
	// WriteToolResult sends a tool result message for the specified tool call.
	WriteToolResult(toolCallID, content string)
	// WriteReasoning sends a reasoning/thinking message as the assistant role.
	WriteReasoning(reasoning string)
	// WriteData sends a custom data message with the specified type and payload.
	WriteData(dataType string, data any)
	// WriteMessage sends an arbitrary pre-built message to the stream.
	WriteMessage(msg Message)
}

CallbackWriter provides methods to push messages in callback-based sources.

type Chunk

type Chunk map[string]any

Chunk represents a single chunk in the UI message stream.

func NewDataChunk

func NewDataChunk(dataType string, data any) Chunk

NewDataChunk creates a custom data chunk. Type will be "data-{dataType}".

func NewErrorChunk

func NewErrorChunk(errorText string) Chunk

func NewFileChunk

func NewFileChunk(fileID, mediaType, url string) Chunk

func NewFinishChunk

func NewFinishChunk() Chunk

func NewFinishStepChunk

func NewFinishStepChunk() Chunk

func NewReasoningDeltaChunk

func NewReasoningDeltaChunk(id, delta string) Chunk

func NewReasoningEndChunk

func NewReasoningEndChunk(id string) Chunk

func NewReasoningStartChunk

func NewReasoningStartChunk(id string) Chunk

func NewSourceDocumentChunk

func NewSourceDocumentChunk(sourceID, mediaType, title string) Chunk

func NewSourceURLChunk

func NewSourceURLChunk(sourceID, url, title string) Chunk

func NewStartChunk

func NewStartChunk(messageID string) Chunk

func NewStartStepChunk

func NewStartStepChunk() Chunk

func NewTextDeltaChunk

func NewTextDeltaChunk(id, delta string) Chunk

func NewTextEndChunk

func NewTextEndChunk(id string) Chunk

func NewTextStartChunk

func NewTextStartChunk(id string) Chunk

func NewToolInputAvailableChunk

func NewToolInputAvailableChunk(toolCallID, toolName string, input any) Chunk

func NewToolInputDeltaChunk

func NewToolInputDeltaChunk(toolCallID, delta string) Chunk

func NewToolInputStartChunk

func NewToolInputStartChunk(toolCallID, toolName string) Chunk

func NewToolOutputAvailableChunk

func NewToolOutputAvailableChunk(toolCallID string, output any) Chunk

type ChunkType

type ChunkType string

ChunkType represents the type of a UI message stream chunk.

const (
	ChunkTypeStart      ChunkType = "start"
	ChunkTypeFinish     ChunkType = "finish"
	ChunkTypeStartStep  ChunkType = "start-step"
	ChunkTypeFinishStep ChunkType = "finish-step"
	ChunkTypeError      ChunkType = "error"

	ChunkTypeTextStart ChunkType = "text-start"
	ChunkTypeTextDelta ChunkType = "text-delta"
	ChunkTypeTextEnd   ChunkType = "text-end"

	ChunkTypeReasoningStart ChunkType = "reasoning-start"
	ChunkTypeReasoningDelta ChunkType = "reasoning-delta"
	ChunkTypeReasoningEnd   ChunkType = "reasoning-end"

	ChunkTypeToolInputStart      ChunkType = "tool-input-start"
	ChunkTypeToolInputDelta      ChunkType = "tool-input-delta"
	ChunkTypeToolInputAvailable  ChunkType = "tool-input-available"
	ChunkTypeToolOutputAvailable ChunkType = "tool-output-available"

	ChunkTypeSourceURL      ChunkType = "source-url"
	ChunkTypeSourceDocument ChunkType = "source-document"

	ChunkTypeFile ChunkType = "file"
)

Chunk types as defined by AI SDK UI Message Stream Protocol.

type Message

type Message struct {
	Role       Role
	Content    string
	ToolCalls  []ToolCall
	ToolCallID string
	Reasoning  string
	Data       map[string]any
}

Message represents a single message in the stream.

type MessageSource

type MessageSource interface {
	// Recv receives the next message from the stream. Returns io.EOF when no more messages are available.
	Recv() (Message, error)
	// Close releases resources associated with this message source.
	Close() error
}

MessageSource produces streaming messages. Returns io.EOF when complete.

func NewAiMessageStreamSource

func NewAiMessageStreamSource(stream ai.MessageStream) MessageSource

NewAiMessageStreamSource creates an adapter for ai.MessageStream.

func NewCallbackSource

func NewCallbackSource(execute func(writer CallbackWriter) error) MessageSource

NewCallbackSource creates a callback-based message source.

func NewChannelSource

func NewChannelSource(ch <-chan Message) MessageSource

NewChannelSource creates an adapter for a Go channel.

type Options

type Options struct {
	SendReasoning bool
	SendSources   bool
	SendStart     bool
	SendFinish    bool
	OnError       func(err error) string
	OnFinish      func(content string)
	GenerateID    func(prefix string) string
}

Options configures the stream behavior.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns the default stream options.

type ResponseWriter

type ResponseWriter interface {
	io.Writer
}

ResponseWriter is compatible with fiber.Ctx.SendStreamWriter.

type Role

type Role string

Role represents the role of a message sender.

const (
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleTool      Role = "tool"
	RoleSystem    Role = "system"
)

type Source

type Source struct {
	Type      string
	ID        string
	URL       string
	Title     string
	MediaType string
}

Source represents a reference source (URL or document).

type StreamWriter

type StreamWriter interface {
	// WriteChunk writes a single chunk to the stream output.
	WriteChunk(chunk Chunk) error
	// Flush flushes any buffered data to the underlying writer.
	Flush() error
}

StreamWriter writes UI message stream chunks.

type ToolCall

type ToolCall struct {
	ID        string
	Name      string
	Arguments string
}

ToolCall represents a tool invocation by the AI.

Jump to

Keyboard shortcuts

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