ws

package
v0.4.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ServeWs

func ServeWs(hub *Hub, handler MessageHandler, w http.ResponseWriter, r *http.Request)

ServeWs handles WebSocket requests from clients

Types

type ActionPayload

type ActionPayload struct {
	ActionType string      `json:"action_type"`
	Success    bool        `json:"success"`
	Data       interface{} `json:"data,omitempty"`
	Error      string      `json:"error,omitempty"`
}

ActionPayload reports action execution results

type Client

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

Client represents a WebSocket client connection

func NewClient

func NewClient(hub *Hub, conn *websocket.Conn, handler MessageHandler) *Client

NewClient creates a new WebSocket client

func (*Client) AddToHistory

func (c *Client) AddToHistory(role, content string)

AddToHistory adds a message to the conversation history

func (*Client) GetFormat

func (c *Client) GetFormat() ResponseFormat

GetFormat returns the client's preferred response format

func (*Client) GetHistory

func (c *Client) GetHistory() []ConversationMessage

GetHistory returns the conversation history

func (*Client) Send

func (c *Client) Send(message []byte)

Send sends a message to this client

func (*Client) SendMessage

func (c *Client) SendMessage(msg *Message) error

SendMessage sends a Message struct to this client

type CommandPayload

type CommandPayload struct {
	Text       string  `json:"text"`       // The transcribed text
	WakeWord   bool    `json:"wake_word"`  // Whether wake word was detected
	Confidence float64 `json:"confidence"` // Speech recognition confidence
}

CommandPayload is sent when user issues a voice command

type ConversationMessage

type ConversationMessage struct {
	Role    string // "user" or "assistant"
	Content string
}

ConversationMessage represents a message in the conversation history

type ErrorPayload

type ErrorPayload struct {
	Code    string `json:"code"`
	Message string `json:"message"`
	Details string `json:"details,omitempty"`
}

ErrorPayload for error messages

type Hub

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

Hub maintains the set of active clients and broadcasts messages

func NewHub

func NewHub() *Hub

NewHub creates a new Hub instance

func (*Hub) Broadcast

func (h *Hub) Broadcast(message []byte)

Broadcast sends a message to all connected clients

func (*Hub) BroadcastMessage

func (h *Hub) BroadcastMessage(msg *Message) error

BroadcastMessage sends a Message to all connected clients

func (*Hub) ClientCount

func (h *Hub) ClientCount() int

ClientCount returns the number of connected clients

func (*Hub) Register

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

Register adds a client to the hub

func (*Hub) Run

func (h *Hub) Run()

Run starts the hub's event loop

func (*Hub) SendToClient

func (h *Hub) SendToClient(client *Client, message []byte)

SendToClient sends a message to a specific client

func (*Hub) Unregister

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

Unregister removes a client from the hub

type Message

type Message struct {
	Type      MessageType     `json:"type"`
	Payload   json.RawMessage `json:"payload"`
	RequestID string          `json:"request_id"`
	Format    ResponseFormat  `json:"format,omitempty"`
	Timestamp time.Time       `json:"timestamp"`
}

Message is the base WebSocket message structure

func NewError

func NewError(code, message, details string) (*Message, error)

NewError creates an error message

func NewMessage

func NewMessage(msgType MessageType, payload interface{}) (*Message, error)

NewMessage creates a new message with a generated request ID

func NewResponse

func NewResponse(text, emotion string) (*Message, error)

NewResponse creates a response message

func NewStatus

func NewStatus(status string, connected bool, aiStatus string) (*Message, error)

NewStatus creates a status message

func NewStreamChunk

func NewStreamChunk(chunk string, done bool, fullText string) (*Message, error)

NewStreamChunk creates a streaming chunk message

func NewTrigger

func NewTrigger(triggerType, title, message string, data interface{}) (*Message, error)

NewTrigger creates a trigger message

func (*Message) MarshalJSON

func (m *Message) MarshalJSON() ([]byte, error)

MarshalJSON marshals a message to JSON bytes

func (*Message) ParseCommand

func (m *Message) ParseCommand() (*CommandPayload, error)

ParseCommand extracts CommandPayload from a message

type MessageHandler

type MessageHandler func(client *Client, msg *Message)

MessageHandler processes incoming messages

type MessageType

type MessageType string

MessageType represents the type of WebSocket message

const (
	MessageTypeCommand  MessageType = "command"  // User voice command (client -> server)
	MessageTypeResponse MessageType = "response" // AI response (server -> client)
	MessageTypeStream   MessageType = "stream"   // Streaming AI response chunk (server -> client)
	MessageTypeAction   MessageType = "action"   // Action execution result (server -> client)
	MessageTypeStatus   MessageType = "status"   // System status updates (bidirectional)
	MessageTypeTrigger  MessageType = "trigger"  // PIKA-initiated interaction (server -> client)
	MessageTypeError    MessageType = "error"    // Error message (server -> client)
)

type ResponseFormat

type ResponseFormat string

ResponseFormat represents how the client wants responses

const (
	FormatHTMX ResponseFormat = "htmx" // Return HTML partials for HTMX
	FormatJSON ResponseFormat = "json" // Return raw JSON
	FormatPush ResponseFormat = "push" // Return push notification format
)

type ResponsePayload

type ResponsePayload struct {
	Text    string `json:"text"`
	Emotion string `json:"emotion,omitempty"` // helpful, curious, alert, etc.
	HTML    string `json:"html,omitempty"`    // Pre-rendered HTML for HTMX
}

ResponsePayload is sent as AI response to user

type StatusPayload

type StatusPayload struct {
	Status    string `json:"status"`    // listening, processing, speaking, idle
	Connected bool   `json:"connected"` // WebSocket connection status
	AIStatus  string `json:"ai_status"` // ready, busy, error
}

StatusPayload for system status updates

type StreamPayload

type StreamPayload struct {
	Chunk    string `json:"chunk"`               // The text chunk
	Done     bool   `json:"done"`                // Whether streaming is complete
	FullText string `json:"full_text,omitempty"` // Full text when done
}

StreamPayload is sent for streaming AI responses

type TriggerPayload

type TriggerPayload struct {
	TriggerType string      `json:"trigger_type"` // reminder, suggestion, alert
	Title       string      `json:"title"`
	Message     string      `json:"message"`
	Data        interface{} `json:"data,omitempty"`
	Priority    string      `json:"priority,omitempty"` // low, normal, high
}

TriggerPayload for PIKA-initiated interactions

Jump to

Keyboard shortcuts

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