websocket_platform#

class baf.platforms.websocket.websocket_platform.WebSocketPlatform(agent, use_ui=True, authenticate_users=False)[source]#

Bases: Platform

The WebSocket Platform allows an agent to communicate with the users using the WebSocket bidirectional communications protocol.

This platform implements the WebSocket server, and it can establish connection with a client, allowing the bidirectional communication between server and client (i.e. sending and receiving messages).

Note

We provide different interfaces implementing a WebSocket client to communicate with the agent, though you can use or create your own UI as long as it has a WebSocket client that connects to the agent’s WebSocket server.

Parameters:
  • agent (Agent) – the agent the platform belongs to

  • use_ui (bool) – whether to use the built-in UI or not

  • authenticate_users (bool) – whether to authenticate users when they connect to the agent and load their chat history

_agent#

The agent the platform belongs to

Type:

Agent

_host#

The WebSocket host address (e.g. localhost)

Type:

str

_port#

The WebSocket port (e.g. 8765)

Type:

int

_use_ui#

Whether to use the built-in UI or not

Type:

bool

_connections#

The list of active connections (i.e. users connected to the agent)

Type:

dict[str, ServerConnection]

_websocket_server#

The WebSocket server instance

Type:

WebSocketServer or None

_message_handler#

The function that handles the user connections (sessions) and incoming messages

Type:

Callable[[ServerConnection], None]

_abc_impl = <_abc._abc_data object>#
_send(session_id, payload)[source]#

Send a payload message to a specific user.

Parameters:
  • session_id (str) – the user to send the response to

  • payload (Payload) – the payload message to send to the user

initialize()[source]#

Initialize the platform. This function is called right before starting the platform.

reply(session, message)[source]#

Send an agent reply, i.e. a text message, to a specific user.

Parameters:
  • session (Session) – the user session

  • message (str) – the message to send to the user

reply_dataframe(session, df)[source]#

Send a DataFrame agent reply, i.e. a table, to a specific user.

Parameters:
reply_file(session, file)[source]#

Send a file reply to a specific user

Parameters:
  • session (Session) – the user session

  • file (File) – the file to send

reply_html(session, message)[source]#

Send an agent reply to a specific user, containing text in HTML format.

Parameters:
  • session (Session) – the user session

  • message (str) – the message in HTML format to send to the user

reply_image(session, img)[source]#

Send an image reply to a specific user.

Before being sent, the image is encoded as jpg and then as a base64 string. This must be known before dedocing the image on the client side.

Parameters:
  • session (Session) – the user session

  • img (np.ndarray) – the image to send

reply_location(session, latitude, longitude)[source]#

Send a location reply to a specific user.

Parameters:
  • session (Session) – the user session

  • latitude (str) – the latitude of the location

  • longitude (str) – the longitude of the location

reply_markdown(session, message)[source]#

Send an agent reply to a specific user, containing text in Markdown format.

Parameters:
  • session (Session) – the user session

  • message (str) – the message in Markdown format to send to the user

reply_options(session, options)[source]#

Send a list of options as a reply. They can be used to let the user choose one of them

Parameters:
  • session (Session) – the user session

  • options (list[str]) – the list of options to send to the user

reply_plotly(session, plot)[source]#

Send a Plotly figure as an agent reply, to a specific user.

Parameters:
  • session (Session) – the user session

  • plot (plotly.graph_objs.Figure) – the message to send to the user

reply_rag(session, rag_message)[source]#

Send a rag reply to a specific user.

Parameters:
  • session (Session) – the user session

  • rag_message (RAGMessage) – the rag message to send to the user

reply_reasoning_step(session, step)[source]#

Send an intermediate reasoning-state step to a specific user.

A reasoning step is any observable event the predefined reasoning state body produces while it is working: the LLM emitting tool calls or text, a tool returning a result, the task list being mutated (planned / completed / skipped), the orchestrator pushing back a premature final answer, or the step budget being exhausted. Sending these to the client lets the UI show a live “thinking” trace before the final reply arrives.

Persisted in the dedicated reasoning_step table (kind= reasoning_step) — not in the chat table — so it does not pollute chat history retrieval.

Parameters:
  • session (Session) – the user session.

  • step (dict) – the step payload with the shape {"kind": str, "step": int, "summary": str, "details": dict}. kind is one of llm_text, llm_tool_calls, tool_result, task_added, task_completed, task_skipped, pushback or max_steps. summary is a short human-readable description suitable for direct rendering; details is a kind-specific structured object for richer client-side rendering.

reply_speech(session, message, audio_speed=None)[source]#

Send an audio reply to a specific user.

The text message is converted to speech and sent to the user. Before being sent, the audio is encoded as a Base64 string. This must be taken into account when decoding the audio on the client side.

Parameters:
  • session (Session) – the user session

  • message (str) – the text message to be converted to speech and sent to the user.

  • audio_speed (float, optional) – The speed of the audio. If not provided, the speed is retrieved from the

  • session

  • speed (2.0 is double) –

  • speed

  • None. (etc. Defaults to) –

reply_task_list_update(session, tasks)[source]#

Send a snapshot of the reasoning state’s task list to a specific user.

Emitted alongside (not in place of) the existing task_added / task_completed / task_skipped reasoning step events, so a client can render a live “task panel” without having to reconstruct the list from the step stream. The reasoning_started / reasoning_finished reasoning steps bracket the lifetime of this panel.

Persisted in the dedicated reasoning_step table (kind= task_list_update) — not in the chat table — so it does not pollute chat history retrieval.

Parameters:
  • session (Session) – the user session.

  • tasks (list) – the current task list as a list of dicts with shape {"id": int, "description": str, "status": str, "result": str}. Each call carries the full current snapshot — the client replaces its state rather than applying a diff.

reply_ui(session, ui)[source]#

Send a GUI model reply to a specific user.

The GUI model is serialized to JSON before being sent. It can be used to dynamically render a user interface on the client side.

Parameters:
  • session (Session) – the user session

  • ui (GUIModel) – the GUI model to send to the user

start()[source]#

Start the platform.

stop()[source]#

Terminate the platform execution.

baf.platforms.websocket.websocket_platform._extract_parameter_from_request(parameter, request)[source]#