routes.tool_approvals_listener
Persistent listener for the datalayer-ai-agents tool-approvals WebSocket.
Whenever agent-runtimes has at least one in-flight approval forwarded to
ai-agents, we maintain a long-lived WS subscription (one per unique JWT
credential) that mirrors every tool_approval_* event into the local
store via :func:update_local_approval_status.
This guarantees that approvals/denials submitted by any participant —
including the SaaS UI (datalayer.ai/agents/tool-approvals) or other
runtimes — unblock the local waiter and update the local record, even
when the per-approval bridge in ToolApprovalManager is not active.
Lifecycle:
- :func:
ensure_listeneris invoked byregister_approval_credentialswhenever a new approval is created with a JWT. - A reference count per JWT tracks how many approvals are currently keeping the listener alive.
- :func:
0 is invoked byremove_approval_credentials`` once an approval is decided. When the refcount reaches zero the WS task is cancelled.
ensure_listener
async def ensure_listener(jwt: str) -> None
Increment refcount for jwt and start a listener task if absent.
release_listener
async def release_listener(jwt: str) -> None
Decrement refcount; cancel the listener when no approvals remain.