Documentation
¶
Index ¶
- Variables
- type Builder
- func (b *Builder) OnError(handler func(err error) string) *Builder
- func (b *Builder) OnFinish(handler func(content string)) *Builder
- func (b *Builder) Stream(ctx fiber.Ctx) error
- func (b *Builder) StreamToWriter(w *bufio.Writer)
- func (b *Builder) WithFinish(enabled bool) *Builder
- func (b *Builder) WithHeader(key, value string) *Builder
- func (b *Builder) WithIDGenerator(gen func(prefix string) string) *Builder
- func (b *Builder) WithMessageID(id string) *Builder
- func (b *Builder) WithReasoning(enabled bool) *Builder
- func (b *Builder) WithSource(source MessageSource) *Builder
- func (b *Builder) WithSources(enabled bool) *Builder
- func (b *Builder) WithStart(enabled bool) *Builder
- type CallbackWriter
- type Chunk
- func NewDataChunk(dataType string, data any) Chunk
- func NewErrorChunk(errorText string) Chunk
- func NewFileChunk(fileID, mediaType, url string) Chunk
- func NewFinishChunk() Chunk
- func NewFinishStepChunk() Chunk
- func NewReasoningDeltaChunk(id, delta string) Chunk
- func NewReasoningEndChunk(id string) Chunk
- func NewReasoningStartChunk(id string) Chunk
- func NewSourceDocumentChunk(sourceID, mediaType, title string) Chunk
- func NewSourceURLChunk(sourceID, url, title string) Chunk
- func NewStartChunk(messageID string) Chunk
- func NewStartStepChunk() Chunk
- func NewTextDeltaChunk(id, delta string) Chunk
- func NewTextEndChunk(id string) Chunk
- func NewTextStartChunk(id string) Chunk
- func NewToolInputAvailableChunk(toolCallID, toolName string, input any) Chunk
- func NewToolInputDeltaChunk(toolCallID, delta string) Chunk
- func NewToolInputStartChunk(toolCallID, toolName string) Chunk
- func NewToolOutputAvailableChunk(toolCallID string, output any) Chunk
- type ChunkType
- type Message
- type MessageSource
- type Options
- type ResponseWriter
- type Role
- type Source
- type StreamWriter
- type ToolCall
Constants ¶
This section is empty.
Variables ¶
var ( ErrSourceRequired = errors.New("message source is required") ErrSourceClosed = errors.New("message source is closed") )
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 ¶
FromChannel creates a Builder with a channel as the source.
func (*Builder) StreamToWriter ¶
StreamToWriter streams messages to a bufio.Writer.
func (*Builder) WithFinish ¶
func (*Builder) WithHeader ¶
func (*Builder) WithIDGenerator ¶
func (*Builder) WithMessageID ¶
func (*Builder) WithReasoning ¶
func (*Builder) WithSource ¶
func (b *Builder) WithSource(source MessageSource) *Builder
func (*Builder) WithSources ¶
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 ¶
Chunk represents a single chunk in the UI message stream.
func NewDataChunk ¶
NewDataChunk creates a custom data chunk. Type will be "data-{dataType}".
func NewErrorChunk ¶
func NewFileChunk ¶
func NewFinishChunk ¶
func NewFinishChunk() Chunk
func NewFinishStepChunk ¶
func NewFinishStepChunk() Chunk
func NewReasoningDeltaChunk ¶
func NewReasoningEndChunk ¶
func NewReasoningStartChunk ¶
func NewSourceDocumentChunk ¶
func NewSourceURLChunk ¶
func NewStartChunk ¶
func NewStartStepChunk ¶
func NewStartStepChunk() Chunk
func NewTextDeltaChunk ¶
func NewTextEndChunk ¶
func NewTextStartChunk ¶
func NewToolInputDeltaChunk ¶
func NewToolInputStartChunk ¶
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 ¶
ResponseWriter is compatible with fiber.Ctx.SendStreamWriter.
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.