websocket

package
v0.1.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package streamhub provides task-scoped stream buffers for worker-push and client subscribe (SSE).

Package wsconn defines the typed event protocol for WebSocket communication between the portal frontend and the server. All messages are JSON envelopes with a "type" field and a typed "payload".

Index

Constants

View Source
const (
	TypeConversationCreate  = "conversation.create"
	TypeConversationMessage = "conversation.message"
	TypeSubscribeTask       = "subscribe.task"
	TypeUnsubscribeTask     = "unsubscribe.task"
)
View Source
const (
	TypeConversationCreated = "conversation.created"
	TypeMessageDelta        = "conversation.message.delta"
	TypeMessageCompleted    = "conversation.message.completed"
	TypeConversationError   = "conversation.error"
	TypeTaskStatusChanged   = "task.status.changed"
	TypeTaskStreamDelta     = "task.stream.delta"
	TypeTaskStreamDone      = "task.stream.done"
	TypeSystemError         = "system.error"
)
View Source
const StreamEventDone = "[[DONE]]"

StreamEventDone is sent on the subscription channel when the run finishes (SUCCEEDED/FAILED).

Variables

This section is empty.

Functions

func DecodePayload

func DecodePayload[T any](env Envelope) (T, error)

DecodePayload unmarshals the envelope's raw payload into the target type T.

func Encode

func Encode(eventType string, payload any) ([]byte, error)

Encode marshals an event type and payload into JSON bytes suitable for WebSocket write.

Types

type ConversationCreate

type ConversationCreate struct {
	Channel string `json:"channel,omitempty"`
	Message string `json:"message"`
}

ConversationCreate is the payload for TypeConversationCreate.

type ConversationCreated

type ConversationCreated struct {
	ConversationID string `json:"conversation_id"`
}

ConversationCreated is the payload for TypeConversationCreated.

type ConversationError

type ConversationError struct {
	ConversationID string `json:"conversation_id,omitempty"`
	Error          string `json:"error"`
}

ConversationError is the payload for TypeConversationError.

type ConversationMessage

type ConversationMessage struct {
	ConversationID string `json:"conversation_id"`
	Content        string `json:"content"`
}

ConversationMessage is the payload for TypeConversationMessage.

type Envelope

type Envelope struct {
	Type    string          `json:"type"`
	Payload json.RawMessage `json:"payload"`
}

Envelope is the wire format for every WebSocket message (both directions).

func Decode

func Decode(data []byte) (Envelope, error)

Decode unmarshals raw bytes into an Envelope.

type MessageCompleted

type MessageCompleted struct {
	ConversationID string `json:"conversation_id"`
}

MessageCompleted is the payload for TypeMessageCompleted.

type MessageDelta

type MessageDelta struct {
	ConversationID string `json:"conversation_id"`
	Delta          string `json:"delta"`
}

MessageDelta is the payload for TypeMessageDelta.

type StreamHub

type StreamHub interface {
	// Append adds a delta to the buffer and broadcasts to all subscribers for that task.
	Append(taskID, delta string)
	// Buffer returns the current buffered content for the task. Empty string if task has no buffer or was Done.
	Buffer(taskID string) string
	// Done marks the task's current run as finished; sends StreamEventDone to subscribers and clears state.
	Done(taskID string)
	// Subscribe returns a channel of deltas (and finally StreamEventDone) for the task. unsub must be called when done.
	Subscribe(taskID string) (events <-chan string, unsub func())
}

StreamHub is the interface for task-scoped stream buffers. Keys are task_id (one active run per task). Implementations may be in-memory (single instance) or backed by Redis (multi-instance).

func NewStreamHub

func NewStreamHub() StreamHub

NewStreamHub returns an in-memory StreamHub. Multi-instance scaling requires a Redis-backed impl.

type SubscribeTask

type SubscribeTask struct {
	TaskID string `json:"task_id"`
}

SubscribeTask is the payload for TypeSubscribeTask.

type SystemError

type SystemError struct {
	Error string `json:"error"`
}

SystemError is the payload for TypeSystemError.

type TaskStatusChanged

type TaskStatusChanged struct {
	TaskID string `json:"task_id"`
	Status string `json:"status"`
	Title  string `json:"title,omitempty"`
}

TaskStatusChanged is the payload for TypeTaskStatusChanged.

type TaskStreamDelta

type TaskStreamDelta struct {
	TaskID string `json:"task_id"`
	Delta  string `json:"delta"`
}

TaskStreamDelta is the payload for TypeTaskStreamDelta.

type TaskStreamDone

type TaskStreamDone struct {
	TaskID string `json:"task_id"`
}

TaskStreamDone is the payload for TypeTaskStreamDone.

type UnsubscribeTask

type UnsubscribeTask struct {
	TaskID string `json:"task_id"`
}

UnsubscribeTask is the payload for TypeUnsubscribeTask.

Jump to

Keyboard shortcuts

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