Bridges

Bridges connect your nodes to the event system.

What is a Bridge?

Bridges are event transport that sit between the Nodes and the Bus. Messages are received and sent on the Bridge.

You can subscribe to specific event types on a Bridge and execute actions when those events occur. When subscribing to an event type, you create a Route, which runs when an event of that type is received on the bridge.

Basic Example

1from line import Bridge
2from line.events import UserStoppedSpeaking
3
4# Create bridge for your node
5bridge = Bridge(chat_node)
6
7# Define route: when user stops speaking, generate response.
8bridge.on(UserStoppedSpeaking).map(chat_node.generate)

Key Concepts

Event Patterns: Patterns that match events (e.g., UserTranscriptionReceived, ToolCall, "*" for all events).

Actions: What to do when an event matches (e.g., call a node method, transform data, broadcast new events).

Filtering: Additional criteria for when routes should trigger (source, event properties, custom functions).

Event Patterns

1# Match specific event type
2bridge.on(UserTranscriptionReceived).map(handle_user_input)
3
4# Match all events with wildcard
5bridge.on("*").map(log_all_events)
6
7# Filter by source
8bridge.on(AgentResponse, source="main_agent").map(send_to_user)
9
10# Custom filtering
11bridge.on(ToolCall, filter_fn=lambda msg: msg.event.tool_name == "calculator").map(handle_calc)