websocket

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BroadcastMessage

type BroadcastMessage struct {
	TicketID types.TicketID
	Message  []byte
}

BroadcastMessage represents a message to be broadcast to all clients of a ticket

type Client

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

Client is a middleman between the websocket connection and the hub

func (*Client) AttachSession added in v0.16.0

func (c *Client) AttachSession(id types.SessionID)

AttachSession binds a resolved Session to the client. Called once by the handler after EnsureWebSession succeeds.

func (*Client) GetClientID

func (c *Client) GetClientID() string

GetClientID returns the client ID for a client

func (*Client) SessionID added in v0.16.0

func (c *Client) SessionID() types.SessionID

SessionID returns the Session ID attached to the client, or the zero value when the handler has not resolved one yet.

type Envelope added in v0.16.0

type Envelope struct {
	Event     EventKind          `json:"event"`
	Timestamp time.Time          `json:"timestamp"`
	SessionID types.SessionID    `json:"session_id,omitempty"`
	TurnID    *types.TurnID      `json:"turn_id,omitempty"`
	Message   *MessageView       `json:"message,omitempty"`
	Session   *SessionView       `json:"session,omitempty"`
	Turn      *TurnView          `json:"turn,omitempty"`
	Status    session.TurnStatus `json:"status,omitempty"`
	Intent    string             `json:"intent,omitempty"`
	HITL      *HITLView          `json:"hitl,omitempty"`
}

Envelope is the common wire shape for redesign events. Only the fields relevant to the event kind are populated; all others are omitted to keep payloads compact.

func NewHITLRequestPendingEvent added in v0.16.0

func NewHITLRequestPendingEvent(sessionID types.SessionID, view *HITLView) Envelope

NewHITLRequestPendingEvent builds the envelope emitted when a Web-facing HITL request enters pending state. messageID optionally binds the prompt to an existing progress message so the UI can render approval UI in-place.

func NewHITLRequestResolvedEvent added in v0.16.0

func NewHITLRequestResolvedEvent(sessionID types.SessionID, view *HITLView) Envelope

NewHITLRequestResolvedEvent builds the envelope emitted when a HITL request transitions out of pending state. The Web UI uses this to clear the approval/question prompt.

func NewSessionCreatedEvent added in v0.16.0

func NewSessionCreatedEvent(s *session.Session) Envelope

NewSessionCreatedEvent builds the envelope for a newly created Session.

func NewSessionMessageAddedEvent added in v0.16.0

func NewSessionMessageAddedEvent(m *session.Message) Envelope

NewSessionMessageAddedEvent builds the envelope for a newly persisted Message. created_at / timestamp default to m.CreatedAt if zero.

func NewSessionMessageUpdatedEvent added in v0.16.0

func NewSessionMessageUpdatedEvent(m *session.Message) Envelope

NewSessionMessageUpdatedEvent builds the envelope for a Message whose content was replaced in-place (updatable trace / HITL target). The MessageView carries the *current* content; prior revisions live on the persisted Message and are not shipped over the wire to keep events small.

func NewTurnEndedEvent added in v0.16.0

func NewTurnEndedEvent(t *session.Turn) Envelope

NewTurnEndedEvent builds the envelope for a Turn that has entered a terminal state (completed / aborted).

func NewTurnStartedEvent added in v0.16.0

func NewTurnStartedEvent(t *session.Turn) Envelope

NewTurnStartedEvent builds the envelope for a Turn that has just entered running state.

func (Envelope) Marshal added in v0.16.0

func (e Envelope) Marshal() ([]byte, error)

Marshal returns the JSON bytes for an envelope. Separate method so callers do not need to import encoding/json directly and to allow future transport instrumentation.

type EventKind added in v0.16.0

type EventKind string

EventKind enumerates the event types the server emits.

const (
	EventKindSessionMessageAdded   EventKind = "session_message_added"
	EventKindSessionMessageUpdated EventKind = "session_message_updated"
	EventKindSessionCreated        EventKind = "session_created"
	EventKindTurnStarted           EventKind = "turn_started"
	EventKindTurnEnded             EventKind = "turn_ended"
	EventKindHITLRequestPending    EventKind = "hitl_request_pending"
	EventKindHITLRequestResolved   EventKind = "hitl_request_resolved"
)

type HITLView added in v0.16.0

type HITLView struct {
	ID        string         `json:"id"`
	SessionID string         `json:"session_id"`
	Type      string         `json:"type"`
	Status    string         `json:"status"`
	UserID    string         `json:"user_id,omitempty"`
	Payload   map[string]any `json:"payload,omitempty"`
	Response  map[string]any `json:"response,omitempty"`
	// MessageID optionally binds the HITL prompt to an existing Web
	// progress message so the UI can render approval UI in-place
	// instead of floating it above the timeline.
	MessageID string `json:"message_id,omitempty"`
}

HITLView is the wire shape for an in-flight HITL request emitted by hitl_request_pending / hitl_request_resolved events. The Web UI uses this to render approval/question UI and to clear it once the request resolves.

type Handler

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

Handler handles WebSocket connections for chat functionality

func NewHandler

func NewHandler(hub *Hub, repository interfaces.Repository, useCases *usecase.UseCases) *Handler

NewHandler creates a new WebSocket handler

func NewTestHandler

func NewTestHandler(hub *Hub, repository interfaces.Repository, useCases *usecase.UseCases) (*Handler, *httptest.Server)

NewTestHandler creates a WebSocket handler configured for testing It automatically sets the frontend URL to match the test server

func (*Handler) HandleTicketChat

func (h *Handler) HandleTicketChat(w http.ResponseWriter, r *http.Request)

HandleTicketChat handles WebSocket connections for ticket chat

func (*Handler) WithAllowedOrigins

func (h *Handler) WithAllowedOrigins(origins []string) *Handler

WithAllowedOrigins sets additional allowed origins (useful for development)

func (*Handler) WithFrontendURL

func (h *Handler) WithFrontendURL(url string) *Handler

WithFrontendURL sets the frontend URL for origin checking

type Hub

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

Hub maintains the set of active clients and broadcasts messages to the clients

func NewHub

func NewHub(ctx context.Context) *Hub

NewHub creates a new Hub

func (*Hub) BroadcastToTicket

func (h *Hub) BroadcastToTicket(ticketID types.TicketID, message []byte)

BroadcastToTicket sends a message to all clients of a specific ticket

func (*Hub) Close

func (h *Hub) Close() error

Close gracefully shuts down the hub

func (*Hub) GetActiveTickets

func (h *Hub) GetActiveTickets() []types.TicketID

GetActiveTickets returns a list of ticket IDs that have active clients

func (*Hub) GetClientCount

func (h *Hub) GetClientCount(ticketID types.TicketID) int

GetClientCount returns the number of clients connected to a specific ticket

func (*Hub) GetTotalClientCount

func (h *Hub) GetTotalClientCount() int

GetTotalClientCount returns the total number of connected clients across all tickets

func (*Hub) NewClient

func (h *Hub) NewClient(conn *websocket.Conn, ticketID types.TicketID, userID string) *Client

NewClient creates a new client (for backward compatibility)

func (*Hub) NewClientWithTabID

func (h *Hub) NewClientWithTabID(conn *websocket.Conn, ticketID types.TicketID, userID string, tabID string) *Client

NewClientWithTabID creates a new client with a specific tab ID

func (*Hub) Register

func (h *Hub) Register(client *Client)

Register registers a client with the hub

func (*Hub) Run

func (h *Hub) Run()

Run starts the hub's main loop

func (*Hub) SendErrorToTicket

func (h *Hub) SendErrorToTicket(ticketID types.TicketID, content string) error

SendErrorToTicket sends an error message to all clients of a ticket

func (*Hub) SendMessageToClient

func (h *Hub) SendMessageToClient(clientID string, content string, user *websocket_model.User) error

SendMessageToClient sends a chat message to a specific client

func (*Hub) SendMessageToTicket

func (h *Hub) SendMessageToTicket(ticketID types.TicketID, content string, user *websocket_model.User) error

SendMessageToTicket sends a chat message to all clients of a ticket

func (*Hub) SendStatusToTicket

func (h *Hub) SendStatusToTicket(ticketID types.TicketID, content string) error

SendStatusToTicket sends a status message to all clients of a ticket

func (*Hub) SendToClient

func (h *Hub) SendToClient(clientID string, response *websocket_model.ChatResponse) error

SendToClient sends a message to a specific client by client ID

func (*Hub) SendToTicket

func (h *Hub) SendToTicket(ticketID types.TicketID, response *websocket_model.ChatResponse) error

SendToTicket is a convenience method to send a WebSocket message to a ticket

func (*Hub) SendTraceToClient

func (h *Hub) SendTraceToClient(clientID string, content string, user *websocket_model.User) error

SendTraceToClient sends a trace message to a specific client

func (*Hub) SendWarningToClient added in v0.8.0

func (h *Hub) SendWarningToClient(clientID string, content string, user *websocket_model.User) error

SendWarningToClient sends a warning message to a specific client

func (*Hub) Unregister

func (h *Hub) Unregister(client *Client)

Unregister removes a client from the hub

type MessageView added in v0.16.0

type MessageView struct {
	ID        string          `json:"id"`
	SessionID string          `json:"session_id"`
	TurnID    *string         `json:"turn_id,omitempty"`
	Type      string          `json:"type"`
	Author    *session.Author `json:"author,omitempty"`
	Content   string          `json:"content"`
	CreatedAt time.Time       `json:"created_at"`
}

MessageView is the wire shape for SessionMessage. Uses strings for IDs so the TypeScript client can treat everything as opaque.

type SessionView added in v0.16.0

type SessionView struct {
	ID           string    `json:"id"`
	TicketID     *string   `json:"ticket_id,omitempty"`
	Source       string    `json:"source"`
	UserID       string    `json:"user_id"`
	CreatedAt    time.Time `json:"created_at"`
	LastActiveAt time.Time `json:"last_active_at"`
}

SessionView is the wire shape for Session metadata emitted alongside session_created. Lock / TicketIDPtr are intentionally omitted; the frontend does not need to see internal state.

type TurnView added in v0.16.0

type TurnView struct {
	ID        string     `json:"id"`
	SessionID string     `json:"session_id"`
	Status    string     `json:"status"`
	Intent    string     `json:"intent,omitempty"`
	StartedAt time.Time  `json:"started_at"`
	EndedAt   *time.Time `json:"ended_at,omitempty"`
}

TurnView is the wire shape for Turn metadata emitted alongside turn_started / turn_ended.

Jump to

Keyboard shortcuts

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