Move HA user agent routing to options
This commit is contained in:
@@ -50,9 +50,11 @@ from .const import (
|
||||
ATTR_HA_USER_ID,
|
||||
CONF_ADDON_CONFIG_PATH,
|
||||
CONF_AGENT_ID,
|
||||
CONF_FALLBACK_AGENT_ID,
|
||||
CONF_GATEWAY_HOST,
|
||||
CONF_GATEWAY_PORT,
|
||||
CONF_GATEWAY_TOKEN,
|
||||
CONF_USER_AGENT_MAP,
|
||||
CONF_USE_SSL,
|
||||
CONF_VERIFY_SSL,
|
||||
CONF_CONTEXT_MAX_CHARS,
|
||||
@@ -90,6 +92,7 @@ from .coordinator import OpenClawCoordinator
|
||||
from .exposure import apply_context_policy, build_exposed_entities_context
|
||||
from .helpers import extract_text_recursive
|
||||
from .routing import (
|
||||
DEFAULT_CHAT_AGENT_ID,
|
||||
build_scoped_session_id,
|
||||
build_scoped_session_prefix,
|
||||
normalize_ha_user_id,
|
||||
@@ -427,7 +430,11 @@ async def _async_send_openclaw_chat(
|
||||
client: OpenClawApiClient = entry_data["client"]
|
||||
coordinator: OpenClawCoordinator = entry_data["coordinator"]
|
||||
options = _get_entry_options(hass, entry_data)
|
||||
resolved_agent_id = resolve_agent_id(ha_user_id, call_agent_id)
|
||||
resolved_agent_id = _resolve_agent_id_from_options(
|
||||
options,
|
||||
ha_user_id,
|
||||
call_agent_id,
|
||||
)
|
||||
scoped_session_id = build_scoped_session_id(
|
||||
raw_session_id,
|
||||
ha_user_id,
|
||||
@@ -524,7 +531,13 @@ def _async_register_services(hass: HomeAssistant) -> None:
|
||||
raw_session_id: str = call.data.get(ATTR_SESSION_ID) or "default"
|
||||
ha_user_id = normalize_ha_user_id(call.context.user_id)
|
||||
call_agent_id = normalize_optional_text(call.data.get(ATTR_AGENT_ID))
|
||||
resolved_agent_id = resolve_agent_id(ha_user_id, call_agent_id)
|
||||
entry_data = _get_first_entry_data(hass)
|
||||
options = _get_entry_options(hass, entry_data) if entry_data else {}
|
||||
resolved_agent_id = _resolve_agent_id_from_options(
|
||||
options,
|
||||
ha_user_id,
|
||||
call_agent_id,
|
||||
)
|
||||
scoped_session_id = build_scoped_session_id(
|
||||
raw_session_id,
|
||||
ha_user_id,
|
||||
@@ -568,7 +581,13 @@ def _async_register_services(hass: HomeAssistant) -> None:
|
||||
raw_session_id: str | None = call.data.get(ATTR_SESSION_ID)
|
||||
ha_user_id = normalize_ha_user_id(call.context.user_id)
|
||||
requested_agent_id = normalize_optional_text(call.data.get(ATTR_AGENT_ID))
|
||||
agent_id = resolve_agent_id(ha_user_id, requested_agent_id)
|
||||
entry_data = _get_first_entry_data(hass)
|
||||
options = _get_entry_options(hass, entry_data) if entry_data else {}
|
||||
agent_id = _resolve_agent_id_from_options(
|
||||
options,
|
||||
ha_user_id,
|
||||
requested_agent_id,
|
||||
)
|
||||
_LOGGER.info(
|
||||
"Clear history requested (user=%s, agent=%s, session=%s)",
|
||||
ha_user_id,
|
||||
@@ -707,6 +726,20 @@ def _get_entry_options(hass: HomeAssistant, entry_data: dict[str, Any]) -> dict[
|
||||
return latest_entry.options if latest_entry else {}
|
||||
|
||||
|
||||
def _resolve_agent_id_from_options(
|
||||
options: dict[str, Any],
|
||||
ha_user_id: str | None,
|
||||
explicit_agent_id: str | None = None,
|
||||
) -> str:
|
||||
"""Resolve the OpenClaw agent from config entry options."""
|
||||
return resolve_agent_id(
|
||||
ha_user_id,
|
||||
explicit_agent_id,
|
||||
options.get(CONF_USER_AGENT_MAP),
|
||||
options.get(CONF_FALLBACK_AGENT_ID, DEFAULT_CHAT_AGENT_ID),
|
||||
)
|
||||
|
||||
|
||||
def _summarize_tool_result(value: Any, max_len: int = 240) -> str | None:
|
||||
"""Return compact string preview of tool result payload."""
|
||||
if value is None:
|
||||
@@ -864,7 +897,9 @@ def _async_register_websocket_api(hass: HomeAssistant) -> None:
|
||||
raw_session_id = msg.get("session_id") or "default"
|
||||
connection_user = getattr(connection, "user", None)
|
||||
ha_user_id = normalize_ha_user_id(getattr(connection_user, "id", None))
|
||||
agent_id = resolve_agent_id(ha_user_id)
|
||||
entry_data = _get_first_entry_data(hass)
|
||||
options = _get_entry_options(hass, entry_data) if entry_data else {}
|
||||
agent_id = _resolve_agent_id_from_options(options, ha_user_id)
|
||||
scoped_session_id = build_scoped_session_id(
|
||||
raw_session_id,
|
||||
ha_user_id,
|
||||
@@ -939,7 +974,7 @@ def _async_register_websocket_api(hass: HomeAssistant) -> None:
|
||||
options = _get_entry_options(hass, entry_data) if entry_data else {}
|
||||
connection_user = getattr(connection, "user", None)
|
||||
ha_user_id = normalize_ha_user_id(getattr(connection_user, "id", None))
|
||||
agent_id = resolve_agent_id(ha_user_id)
|
||||
agent_id = _resolve_agent_id_from_options(options, ha_user_id)
|
||||
connection.send_result(
|
||||
msg["id"],
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user