Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AIScientists Dev WorldSeed ConnectionBridge

From Leeroopedia
Knowledge Sources
Domains Gateway, Networking
Last Updated 2026-10-26 18:00 GMT

Overview

Concrete per-account WebSocket connection bridge that manages request/response correlation for tool calls through a shared gateway connection, provided by the WorldSeed library.

Description

The ConnectionBridge class owns the WebSocket lifecycle reference and a pending-requests map keyed by `request_id`. `setConnection(ws)` sets or clears the active WebSocket, rejecting all pending requests on disconnect. `sendRequest(msg, timeoutMs)` assigns a unique request ID, sets a timeout, sends the JSON message, and returns a Promise that resolves when `handleResponse` matches the request ID. `handleResponse(msg)` correlates by request ID, rejecting on `type: "error"` and resolving otherwise. `sendRaw(msg)` fires-and-forgets without a request ID or response expectation. Each account gets its own bridge instance, making it safe for multi-agent scenarios.

Usage

Import this class from the gateway adapter to send perceive/act tool requests through the WebSocket connection and await correlated responses. The gateway owns the WebSocket; the bridge borrows it for request/response cycles.

Code Reference

Source Location

  • Repository: WorldSeed
  • File: openclaw-plugin/src/connection.ts

Signature

export class ConnectionBridge {
  setConnection(ws: WebSocket | null): void;
  handleResponse(msg: any): boolean;
  sendRaw(msg: Record<string, unknown>): void;
  sendRequest(msg: Record<string, unknown>, timeoutMs?: number): Promise<any>;
}

Import

import { ConnectionBridge } from "./connection.js";

I/O Contract

Inputs

Name Type Required Description
ws null Yes (setConnection) Active WebSocket or null to disconnect
msg Record<string, unknown> Yes Message to send (will get request_id appended)
timeoutMs number No Request timeout (default 30000ms)

Outputs

Name Type Description
sendRequest returns Promise<any> │ Resolves with response, rejects on error/timeout
handleResponse returns boolean │ True if message matched a pending request

Usage Examples

import { ConnectionBridge } from "./connection.js";

const bridge = new ConnectionBridge();

// On WebSocket open
ws.onopen = () => bridge.setConnection(ws);

// Send a perceive request and await response
const response = await bridge.sendRequest({
  type: "perceive",
  agent_id: "old_chen",
});

// Fire-and-forget (no response expected)
bridge.sendRaw({ type: "heartbeat" });

// On WebSocket close
ws.onclose = () => bridge.setConnection(null);

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment