import type { ServerWebSocket } from "bun"; /** * Message types for gateway communication */ export type MessageType = "connect" | "res" | "req:agent" | "req:agent:cancel" | "event:agent" | "req:node" | "event:node" | "session_subscribe" | "session_unsubscribe" | "register" | "registered" | "unregister" | "join_group" | "leave_group" | "broadcast" | "direct" | "ping" | "pong" | "error" | "ack" | "upgrade"; /** * Gateway message structure */ export interface GatewayMessage { type: MessageType; id?: string; client?: GatewayClientInfo; auth?: GatewayAuthPayload; ok?: boolean; clientId?: string; nodeId?: string; groupId?: string; roomId?: string; targetNodeId?: string; payload?: unknown; timestamp: number; messageId?: string; } export interface GatewayClientInfo { instanceId: string; clientType: string; version?: string; } export interface GatewayAuthPayload { token?: string; password?: string; deviceId?: string; } export interface ImageAttachment { kind?: "image"; dataUrl: string; mimeType?: string; name?: string; size?: number; } export interface AudioAttachment { kind: "audio"; dataUrl: string; mimeType?: string; name?: string; size?: number; } export interface FileAttachment { kind: "file"; dataUrl: string; textContent: string; mimeType?: string; name?: string; size?: number; } export type MediaAttachment = ImageAttachment | AudioAttachment | FileAttachment; export interface AgentRequestPayload { agentId?: string; content?: string; attachments?: MediaAttachment[]; execution?: { workspace?: string; configDir?: string; }; routing?: RoutingInfo; sessionKey?: string; queueIfBusy?: boolean; } export interface AgentCancelPayload { requestId: string; } export interface RoutingPeer { kind: "dm" | "group" | "channel"; id: string; } export interface RoutingInfo { channel: string; accountId?: string; guildId?: string; teamId?: string; peer?: RoutingPeer; threadId?: string; } /** * Node metadata */ export interface NodeMetadata { id: string; name: string; clientId?: string; capabilities?: string[]; groups: Set; connectedAt: number; lastPing?: number; messageCount?: number; lastMessageTime?: number; sessionId?: string; agentName?: string; } /** * Node with WebSocket connection */ export interface Node extends NodeMetadata { ws: ServerWebSocket<{ nodeId: string; clientId?: string; clientType?: string; authenticated?: boolean; tailscaleUser?: string; }>; } /** * Broadcast processing strategy */ export type BroadcastStrategy = "parallel" | "sequential"; /** * Broadcast group */ export interface BroadcastGroup { id: string; name: string; description?: string; createdAt: number; createdBy: string; members: Set; strategy: BroadcastStrategy; metadata?: Record; } /** * Gateway configuration */ export interface GatewayConfig { port: number; host: string; authToken?: string; requireAuth?: boolean; auth?: GatewayAuthConfig; stateDir?: string; workspace?: string; configDir?: string; fsRoots?: string[]; maxNodes?: number; pingInterval?: number; pingTimeout?: number; logLevel?: "debug" | "info" | "warn" | "error" | "silent"; discovery?: { enabled: boolean; method: "mdns" | "tailscale"; name: string; }; mcpProxy?: { enabled: boolean; command?: string; baseArgs?: string[]; projectName?: string; pushExplorer?: boolean; apiKey?: string; apiUrl?: string; }; } export interface GatewayAuthConfig { mode: "token" | "password" | "none"; token?: string; password?: string; allowTailscale?: boolean; } /** * Gateway statistics */ export interface GatewayStats { uptime: number; totalNodes: number; totalGroups: number; messagesProcessed: number; startedAt: number; activeSessions?: number; sessionNodes?: Array<{ sessionId: string; agentName?: string; nodeIds: string[]; }>; } /** * Registration request payload */ export interface RegisterPayload { name: string; capabilities?: string[]; token?: string; sessionId?: string; agentName?: string; metadata?: Record; } export interface SessionSubscriptionPayload { sessionId: string; } /** * Join group request payload */ export interface JoinGroupPayload { groupId?: string; groupName?: string; createIfNotExists?: boolean; description?: string; } /** * Broadcast message payload */ export interface BroadcastPayload { groupId: string; message: unknown; } /** * Direct message payload */ export interface DirectPayload { targetNodeId: string; message: unknown; } /** * Error payload */ export interface ErrorPayload { code: string; message: string; details?: unknown; } /** * Health check response */ export interface HealthResponse { status: "healthy" | "degraded" | "unhealthy"; version: string; stats: GatewayStats; timestamp: number; } /** * Daemon status */ export interface DaemonStatus { running: boolean; pid?: number; uptime?: number; config?: GatewayConfig; }