gateway

package
v1.18.3 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package gateway – channel_health_monitor.go monitors the health of active channels (WebSocket connections, long-running integrations) and restarts stale ones with cooldown and rate limiting.

Package gateway provides an HTTP API gateway for DevClaw.

Package gateway provides an HTTP API gateway for DevClaw.

Package gateway – websocket.go implements a bidirectional JSON-RPC WebSocket endpoint for real-time communication with the DevClaw agent.

Protocol:

Client → Server (requests):
  {"type":"req","id":"1","method":"chat.send","params":{"sessionId":"...","content":"..."}}
  {"type":"req","id":"2","method":"chat.abort","params":{"sessionId":"..."}}
  {"type":"req","id":"3","method":"chat.history","params":{"sessionId":"..."}}

Server → Client (responses):
  {"type":"res","id":"1","ok":true,"payload":{"runId":"..."}}

Server → Client (events — unsolicited):
  {"type":"event","event":"delta","payload":{"content":"..."}}
  {"type":"event","event":"tool_use","payload":{"tool":"...","input":{...}}}
  {"type":"event","event":"done","payload":{"usage":{...}}}

Index

Constants

This section is empty.

Variables

View Source
var ValidWebhookEvents = []string{
	"message.received",
	"message.sent",
	"agent.started",
	"agent.completed",
	"agent.error",
	"tool.called",
	"tool.completed",
	"session.created",
	"session.deleted",
}

ValidWebhookEvents lists all supported webhook event types.

Functions

This section is empty.

Types

type ChannelHealth added in v1.16.0

type ChannelHealth struct {
	Name         string    `json:"name"`
	LastActivity time.Time `json:"last_activity"`
	Restarts     int       `json:"restarts"`
	Healthy      bool      `json:"healthy"`
}

ChannelHealth represents the health state of a single channel.

type ChannelHealthMonitor added in v1.16.0

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

ChannelHealthMonitor checks registered channels periodically and restarts stale connections with rate limiting.

func NewChannelHealthMonitor added in v1.16.0

func NewChannelHealthMonitor(cfg ChannelHealthMonitorConfig, logger *slog.Logger) *ChannelHealthMonitor

NewChannelHealthMonitor creates a new monitor.

func (*ChannelHealthMonitor) Health added in v1.16.0

func (m *ChannelHealthMonitor) Health() []ChannelHealth

Health returns the current health status of all monitored channels. Does not hold the mutex during channel interface calls.

func (*ChannelHealthMonitor) Register added in v1.16.0

func (m *ChannelHealthMonitor) Register(ch MonitoredChannel)

Register adds a channel to the monitor. Nil channels and duplicates (by ChannelName) are silently ignored.

func (*ChannelHealthMonitor) Start added in v1.16.0

func (m *ChannelHealthMonitor) Start(ctx context.Context)

Start begins the periodic health check loop. Blocks until ctx is cancelled.

type ChannelHealthMonitorConfig added in v1.16.0

type ChannelHealthMonitorConfig struct {
	Enabled            bool          `yaml:"enabled"`
	CheckInterval      time.Duration `yaml:"check_interval"`        // default 60s
	StaleThreshold     time.Duration `yaml:"stale_threshold"`       // default 5m
	RestartCooldown    time.Duration `yaml:"restart_cooldown"`      // default 2m
	MaxRestartsPerHour int           `yaml:"max_restarts_per_hour"` // default 5
	RestartTimeout     time.Duration `yaml:"restart_timeout"`       // default 30s
}

ChannelHealthMonitorConfig configures the health monitor.

func DefaultChannelHealthMonitorConfig added in v1.16.0

func DefaultChannelHealthMonitorConfig() ChannelHealthMonitorConfig

DefaultChannelHealthMonitorConfig returns sensible defaults.

type Gateway

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

Gateway is the HTTP API gateway.

func New

func New(assistant *copilot.Assistant, cfg copilot.GatewayConfig, logger *slog.Logger) *Gateway

New creates a new Gateway.

func (*Gateway) AddWebhook

func (g *Gateway) AddWebhook(webhookURL string, events []string) (WebhookEntry, error)

AddWebhook registers a new webhook and returns the entry or an error. Returns an error if the URL targets a private/loopback address (SSRF guard).

func (*Gateway) DeleteWebhook

func (g *Gateway) DeleteWebhook(id string) bool

DeleteWebhook removes a webhook by ID.

func (*Gateway) ListWebhooks

func (g *Gateway) ListWebhooks() []WebhookEntry

ListWebhooks returns all registered webhooks.

func (*Gateway) Start

func (g *Gateway) Start(ctx context.Context) error

Start starts the HTTP server.

func (*Gateway) Stop

func (g *Gateway) Stop(ctx context.Context) error

Stop gracefully shuts down the HTTP server.

func (*Gateway) ToggleWebhook

func (g *Gateway) ToggleWebhook(id string, active bool) bool

ToggleWebhook enables or disables a webhook by ID.

type MonitoredChannel added in v1.16.0

type MonitoredChannel interface {
	// ChannelName returns the channel identifier (e.g. "whatsapp", "telegram").
	ChannelName() string
	// LastActivityTime returns the time of the last received or sent message.
	LastActivityTime() time.Time
	// Restart attempts to reconnect/restart the channel. Returns error if failed.
	Restart(ctx context.Context) error
}

MonitoredChannel is the interface that channels must implement to be monitored.

type WebSocketHandler

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

WebSocketHandler upgrades HTTP connections to WebSocket and handles bidirectional JSON-RPC communication.

func NewWebSocketHandler

func NewWebSocketHandler(api webui.AssistantAPI, logger *slog.Logger) *WebSocketHandler

NewWebSocketHandler creates a new WebSocket handler. The handshake timeout defaults to 10s and can be overridden via DEVCLAW_WS_HANDSHAKE_TIMEOUT_MS environment variable.

func (*WebSocketHandler) ServeHTTP

func (h *WebSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP upgrades the connection and starts the message loop.

type WebhookEntry

type WebhookEntry struct {
	ID        string    `json:"id"`
	URL       string    `json:"url"`
	Events    []string  `json:"events"`
	CreatedAt time.Time `json:"created_at"`
	Active    bool      `json:"active"`
}

WebhookEntry represents a registered outgoing webhook.

Jump to

Keyboard shortcuts

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