@@ -51,6 +51,7 @@ def __init__(
5151 use_ssl : bool = False ,
5252 verify_ssl : bool = True ,
5353 session : aiohttp .ClientSession | None = None ,
54+ agent_id : str = "main" ,
5455 ) -> None :
5556 """Initialize the API client.
5657
@@ -61,13 +62,15 @@ def __init__(
6162 use_ssl: Use HTTPS instead of HTTP.
6263 verify_ssl: Verify SSL certificates (set False for self-signed certs).
6364 session: Optional aiohttp session (reused from HA).
65+ agent_id: Target OpenClaw agent ID (default: "main").
6466 """
6567 self ._host = host
6668 self ._port = port
6769 self ._token = token
6870 self ._use_ssl = use_ssl
6971 self ._verify_ssl = verify_ssl
7072 self ._session = session
73+ self ._agent_id = agent_id
7174 self ._base_url = f"{ 'https' if use_ssl else 'http' } ://{ host } :{ port } "
7275 # ssl=False disables cert verification for self-signed certs;
7376 # ssl=None uses default verification.
@@ -82,11 +85,13 @@ def update_token(self, token: str) -> None:
8285 """Update the authentication token (e.g., after addon restart)."""
8386 self ._token = token
8487
85- def _headers (self ) -> dict [str , str ]:
86- """Build request headers with auth token."""
88+ def _headers (self , agent_id : str | None = None ) -> dict [str , str ]:
89+ """Build request headers with auth token and agent ID."""
90+ effective_agent = agent_id or self ._agent_id or "main"
8791 return {
8892 "Authorization" : f"Bearer { self ._token } " ,
8993 "Content-Type" : "application/json" ,
94+ "x-openclaw-agent-id" : effective_agent ,
9095 }
9196
9297 async def _get_session (self ) -> aiohttp .ClientSession :
@@ -182,6 +187,7 @@ async def async_send_message(
182187 model : str | None = None ,
183188 system_prompt : str | None = None ,
184189 stream : bool = False ,
190+ agent_id : str | None = None ,
185191 ) -> dict [str , Any ]:
186192 """Send a chat message (non-streaming).
187193
@@ -190,6 +196,7 @@ async def async_send_message(
190196 session_id: Optional session/conversation ID.
191197 model: Optional model override.
192198 stream: If True, raises ValueError (use async_stream_message).
199+ agent_id: Optional per-call agent ID override.
193200
194201 Returns:
195202 Complete chat completion response.
@@ -213,7 +220,7 @@ async def async_send_message(
213220 payload ["model" ] = model
214221
215222 # Pass session_id as a custom header or param if supported by gateway
216- headers = self ._headers ()
223+ headers = self ._headers (agent_id = agent_id )
217224 if session_id :
218225 headers ["X-Session-Id" ] = session_id
219226 headers ["x-openclaw-session-key" ] = session_id
@@ -247,6 +254,7 @@ async def async_stream_message(
247254 session_id : str | None = None ,
248255 model : str | None = None ,
249256 system_prompt : str | None = None ,
257+ agent_id : str | None = None ,
250258 ) -> AsyncIterator [str ]:
251259 """Send a chat message and stream the response via SSE.
252260
@@ -256,6 +264,7 @@ async def async_stream_message(
256264 message: The user message text.
257265 session_id: Optional session/conversation ID.
258266 model: Optional model override.
267+ agent_id: Optional per-call agent ID override.
259268
260269 Yields:
261270 Content delta strings from the streaming response.
@@ -275,7 +284,7 @@ async def async_stream_message(
275284 if model :
276285 payload ["model" ] = model
277286
278- headers = self ._headers ()
287+ headers = self ._headers (agent_id = agent_id )
279288 if session_id :
280289 headers ["X-Session-Id" ] = session_id
281290 headers ["x-openclaw-session-key" ] = session_id
0 commit comments