gateway

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package gateway provides the WebSocket control plane for omniagent.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AgentProcessor

type AgentProcessor interface {
	Process(ctx context.Context, sessionID, content string) (string, error)
}

AgentProcessor processes messages through an AI agent.

type AuthMessage

type AuthMessage struct {
	Token    string `json:"token,omitempty"`
	DeviceID string `json:"device_id,omitempty"`
}

AuthMessage represents an authentication message.

type ChatMessage

type ChatMessage struct {
	SessionID string `json:"session_id,omitempty"`
	Content   string `json:"content"`
	Channel   string `json:"channel,omitempty"`
	ReplyTo   string `json:"reply_to,omitempty"`
}

ChatMessage represents a chat message.

type Client

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

Client represents a connected WebSocket client.

func (*Client) Close

func (c *Client) Close()

Close closes the client connection.

func (*Client) GetMetadata

func (c *Client) GetMetadata(key string) (interface{}, bool)

GetMetadata gets a metadata value.

func (*Client) Send

func (c *Client) Send(msg *Message)

Send queues a message to be sent to the client.

func (*Client) SetMetadata

func (c *Client) SetMetadata(key string, value interface{})

SetMetadata sets a metadata value.

type Config

type Config struct {
	Address         string
	ReadTimeout     time.Duration
	WriteTimeout    time.Duration
	PingInterval    time.Duration
	Logger          *slog.Logger
	Agent           AgentProcessor
	WebhookHandlers map[string]http.Handler // Path -> Handler for webhook endpoints
}

Config configures the gateway server.

type DefaultMessageHandler

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

DefaultMessageHandler provides a basic message handler implementation.

func NewDefaultMessageHandler

func NewDefaultMessageHandler(gw *Gateway) *DefaultMessageHandler

NewDefaultMessageHandler creates a new default message handler.

func (*DefaultMessageHandler) Handle

func (h *DefaultMessageHandler) Handle(ctx context.Context, client *Client, msg *Message) (*Message, error)

Handle processes incoming messages.

type EventMessage

type EventMessage struct {
	Event   string                 `json:"event"`
	Channel string                 `json:"channel,omitempty"`
	Data    map[string]interface{} `json:"data,omitempty"`
}

EventMessage represents an event notification.

type Gateway

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

Gateway is the WebSocket control plane server.

func New

func New(config Config) (*Gateway, error)

New creates a new Gateway.

func (*Gateway) Broadcast

func (g *Gateway) Broadcast(msg *Message)

Broadcast sends a message to all connected clients.

func (*Gateway) ClientCount

func (g *Gateway) ClientCount() int

ClientCount returns the number of connected clients.

func (*Gateway) GetClient

func (g *Gateway) GetClient(id string) *Client

GetClient returns a client by ID.

func (*Gateway) OnMessage

func (g *Gateway) OnMessage(handler MessageHandler)

OnMessage sets the message handler.

func (*Gateway) Run

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

Run starts the gateway server.

type Message

type Message struct {
	ID        string                 `json:"id,omitempty"`
	Type      MessageType            `json:"type"`
	Channel   string                 `json:"channel,omitempty"`
	Content   string                 `json:"content,omitempty"`
	Data      map[string]interface{} `json:"data,omitempty"`
	Error     string                 `json:"error,omitempty"`
	Timestamp time.Time              `json:"timestamp,omitempty"`
}

Message is the base message structure for gateway communication.

func NewChatResponse

func NewChatResponse(id, content string) *Message

NewChatResponse creates a chat response message.

func NewErrorMessage

func NewErrorMessage(id, errMsg string) *Message

NewErrorMessage creates an error message.

func NewEventMessage

func NewEventMessage(event, channel string, data map[string]interface{}) *Message

NewEventMessage creates an event message.

type MessageHandler

type MessageHandler func(ctx context.Context, client *Client, msg *Message) (*Message, error)

MessageHandler handles incoming messages from clients.

type MessageType

type MessageType string

MessageType represents the type of gateway message.

const (
	// Client -> Gateway
	MessageTypeChat      MessageType = "chat"
	MessageTypePing      MessageType = "ping"
	MessageTypeAuth      MessageType = "auth"
	MessageTypeSubscribe MessageType = "subscribe"

	// Gateway -> Client
	MessageTypeResponse MessageType = "response"
	MessageTypePong     MessageType = "pong"
	MessageTypeError    MessageType = "error"
	MessageTypeEvent    MessageType = "event"
)

type Observability added in v0.9.0

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

Observability provides gateway instrumentation.

func NewObservability added in v0.9.0

func NewObservability(config ObservabilityConfig) (*Observability, error)

NewObservability creates a new observability instance.

func (*Observability) CompleteWorkflow added in v0.9.0

func (o *Observability) CompleteWorkflow(ctx context.Context, workflowID string, output map[string]any) error

CompleteWorkflow completes an agentops workflow.

func (*Observability) EndTrace added in v0.9.0

func (o *Observability) EndTrace(tc *TraceContext, err error)

EndTrace ends a trace with optional error.

func (*Observability) FailWorkflow added in v0.9.0

func (o *Observability) FailWorkflow(ctx context.Context, workflowID string, err error) error

FailWorkflow marks an agentops workflow as failed.

func (*Observability) ForceFlush added in v0.9.0

func (o *Observability) ForceFlush(ctx context.Context) error

ForceFlush forces any buffered telemetry to be exported.

func (*Observability) Provider added in v0.9.0

func (o *Observability) Provider() observops.Provider

Provider returns the observops provider.

func (*Observability) RecordClientConnect added in v0.9.0

func (o *Observability) RecordClientConnect(ctx context.Context, clientID string)

RecordClientConnect records a client connection event.

func (*Observability) RecordClientDisconnect added in v0.9.0

func (o *Observability) RecordClientDisconnect(ctx context.Context, clientID string)

RecordClientDisconnect records a client disconnection event.

func (*Observability) RecordEvent added in v0.9.0

func (o *Observability) RecordEvent(ctx context.Context, eventType, category string, data map[string]any) error

RecordEvent records a generic event.

func (*Observability) RecordMessage added in v0.9.0

func (o *Observability) RecordMessage(ctx context.Context, clientID string, msgType MessageType, err error)

RecordMessage records a message processing event.

func (*Observability) RecordToolInvocation added in v0.9.0

func (o *Observability) RecordToolInvocation(ctx context.Context, toolName string, duration time.Duration, err error)

RecordToolInvocation records a tool invocation event.

func (*Observability) Shutdown added in v0.9.0

func (o *Observability) Shutdown(ctx context.Context) error

Shutdown shuts down observability, flushing any buffered data.

func (*Observability) StartTrace added in v0.9.0

func (o *Observability) StartTrace(ctx context.Context, operationName string, attrs ...observops.KeyValue) *TraceContext

StartTrace starts a new trace for a gateway operation.

func (*Observability) StartWorkflow added in v0.9.0

func (o *Observability) StartWorkflow(ctx context.Context, name string, input map[string]any) (*agentops.Workflow, error)

StartWorkflow starts a new agentops workflow for tracking.

func (*Observability) Store added in v0.9.0

func (o *Observability) Store() agentops.Store

Store returns the agentops store.

type ObservabilityConfig added in v0.9.0

type ObservabilityConfig struct {
	// ServiceName is the name of this gateway service.
	ServiceName string

	// ServiceVersion is the version of this gateway service.
	ServiceVersion string

	// ObservopsProvider is the observops provider for metrics/traces.
	ObservopsProvider observops.Provider

	// AgentopsStore is the agentops store for workflow/task tracking.
	AgentopsStore agentops.Store

	// Logger is the logger for observability events.
	Logger *slog.Logger
}

ObservabilityConfig configures gateway observability.

type ToolInfo added in v0.9.0

type ToolInfo struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Parameters  any    `json:"parameters,omitempty"`
}

ToolInfo describes a tool for listing.

type ToolInvokeRequest added in v0.9.0

type ToolInvokeRequest struct {
	// Tool is the name of the tool to invoke.
	Tool string `json:"tool"`

	// Arguments are the tool arguments as JSON.
	Arguments json.RawMessage `json:"arguments"`

	// WorkflowID is the optional workflow ID for tracking.
	WorkflowID string `json:"workflow_id,omitempty"`

	// TaskID is the optional task ID for tracking.
	TaskID string `json:"task_id,omitempty"`

	// AgentID is the agent invoking the tool.
	AgentID string `json:"agent_id,omitempty"`

	// TraceID is the optional trace ID for correlation.
	TraceID string `json:"trace_id,omitempty"`

	// Metadata contains optional request metadata.
	Metadata map[string]any `json:"metadata,omitempty"`
}

ToolInvokeRequest is the request format for tools.invoke RPC.

type ToolInvokeResponse added in v0.9.0

type ToolInvokeResponse struct {
	// Result is the tool execution result.
	Result string `json:"result,omitempty"`

	// Error is the error message if execution failed.
	Error string `json:"error,omitempty"`

	// ToolName is the name of the tool that was invoked.
	ToolName string `json:"tool_name"`

	// DurationMs is the execution duration in milliseconds.
	DurationMs int64 `json:"duration_ms"`

	// InvocationID is the unique ID for this invocation.
	InvocationID string `json:"invocation_id,omitempty"`

	// TraceID is the trace ID for correlation.
	TraceID string `json:"trace_id,omitempty"`
}

ToolInvokeResponse is the response format for tools.invoke RPC.

type ToolsListHandler added in v0.9.0

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

ToolsListHandler handles listing available tools.

func NewToolsListHandler added in v0.9.0

func NewToolsListHandler(registry *agent.ToolRegistry, logger *slog.Logger) *ToolsListHandler

NewToolsListHandler creates a new tools list handler.

func (*ToolsListHandler) ServeHTTP added in v0.9.0

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

ServeHTTP handles the HTTP request.

type ToolsListResponse added in v0.9.0

type ToolsListResponse struct {
	Tools []ToolInfo `json:"tools"`
}

ToolsListResponse is the response format for tools.list RPC.

type ToolsRPCConfig added in v0.9.0

type ToolsRPCConfig struct {
	// ToolRegistry is the registry of available tools.
	ToolRegistry *agent.ToolRegistry

	// Observability provides tracing and metrics.
	Observability *Observability

	// Logger is the logger for RPC events.
	Logger *slog.Logger

	// MaxRequestSize is the maximum request body size in bytes.
	// Default is 1MB.
	MaxRequestSize int64

	// Timeout is the maximum time for a tool invocation.
	// Default is 30 seconds.
	Timeout time.Duration
}

ToolsRPCConfig configures the tools RPC handler.

type ToolsRPCHandler added in v0.9.0

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

ToolsRPCHandler handles SDK-facing tools.invoke RPC requests.

func NewToolsRPCHandler added in v0.9.0

func NewToolsRPCHandler(config ToolsRPCConfig) *ToolsRPCHandler

NewToolsRPCHandler creates a new tools RPC handler.

func (*ToolsRPCHandler) ServeHTTP added in v0.9.0

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

ServeHTTP handles the HTTP request.

type TraceContext added in v0.9.0

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

TraceContext holds trace context for a request.

Jump to

Keyboard shortcuts

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