2026-02-04 02:28:51 +05:30
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
|
|
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
2026-02-04 02:28:51 +05:30
|
|
|
|
2026-02-08 21:16:26 +05:30
|
|
|
from app.api.deps import require_org_admin
|
2026-02-04 23:07:22 +05:30
|
|
|
from app.core.auth import AuthContext, get_auth_context
|
2026-02-05 00:21:33 +05:30
|
|
|
from app.db.session import get_session
|
|
|
|
|
from app.integrations.openclaw_gateway import GatewayConfig as GatewayClientConfig
|
2026-02-04 02:28:51 +05:30
|
|
|
from app.integrations.openclaw_gateway import (
|
|
|
|
|
OpenClawGatewayError,
|
2026-02-04 14:58:14 +05:30
|
|
|
ensure_session,
|
2026-02-04 02:28:51 +05:30
|
|
|
get_chat_history,
|
|
|
|
|
openclaw_call,
|
|
|
|
|
send_message,
|
|
|
|
|
)
|
2026-02-04 17:05:58 +05:30
|
|
|
from app.integrations.openclaw_gateway_protocol import (
|
|
|
|
|
GATEWAY_EVENTS,
|
|
|
|
|
GATEWAY_METHODS,
|
|
|
|
|
PROTOCOL_VERSION,
|
|
|
|
|
)
|
2026-02-04 16:04:52 +05:30
|
|
|
from app.models.boards import Board
|
2026-02-04 23:07:22 +05:30
|
|
|
from app.models.gateways import Gateway
|
2026-02-06 16:12:04 +05:30
|
|
|
from app.schemas.common import OkResponse
|
|
|
|
|
from app.schemas.gateway_api import (
|
|
|
|
|
GatewayCommandsResponse,
|
|
|
|
|
GatewayResolveQuery,
|
|
|
|
|
GatewaySessionHistoryResponse,
|
|
|
|
|
GatewaySessionMessageRequest,
|
|
|
|
|
GatewaySessionResponse,
|
|
|
|
|
GatewaySessionsResponse,
|
|
|
|
|
GatewaysStatusResponse,
|
|
|
|
|
)
|
2026-02-08 21:17:26 +05:30
|
|
|
from app.services.organizations import OrganizationContext, require_board_access
|
2026-02-04 02:28:51 +05:30
|
|
|
|
2026-02-04 23:07:22 +05:30
|
|
|
router = APIRouter(prefix="/gateways", tags=["gateways"])
|
2026-02-04 02:28:51 +05:30
|
|
|
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
async def _resolve_gateway(
|
|
|
|
|
session: AsyncSession,
|
2026-02-04 21:44:21 +05:30
|
|
|
board_id: str | None,
|
|
|
|
|
gateway_url: str | None,
|
|
|
|
|
gateway_token: str | None,
|
|
|
|
|
gateway_main_session_key: str | None,
|
2026-02-08 21:16:26 +05:30
|
|
|
*,
|
|
|
|
|
user: object | None = None,
|
2026-02-04 23:07:22 +05:30
|
|
|
) -> tuple[Board | None, GatewayClientConfig, str | None]:
|
2026-02-04 21:44:21 +05:30
|
|
|
if gateway_url:
|
2026-02-04 23:07:22 +05:30
|
|
|
return (
|
|
|
|
|
None,
|
|
|
|
|
GatewayClientConfig(url=gateway_url, token=gateway_token),
|
|
|
|
|
gateway_main_session_key,
|
|
|
|
|
)
|
2026-02-04 16:04:52 +05:30
|
|
|
if not board_id:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
2026-02-04 21:44:21 +05:30
|
|
|
detail="board_id or gateway_url is required",
|
2026-02-04 16:04:52 +05:30
|
|
|
)
|
2026-02-06 16:12:04 +05:30
|
|
|
board = await session.get(Board, board_id)
|
2026-02-04 16:04:52 +05:30
|
|
|
if board is None:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Board not found")
|
2026-02-08 21:16:26 +05:30
|
|
|
if isinstance(user, object) and user is not None:
|
|
|
|
|
await require_board_access(session, user=user, board=board, write=False) # type: ignore[arg-type]
|
2026-02-04 23:07:22 +05:30
|
|
|
if not board.gateway_id:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
|
|
|
detail="Board gateway_id is required",
|
|
|
|
|
)
|
2026-02-06 16:12:04 +05:30
|
|
|
gateway = await session.get(Gateway, board.gateway_id)
|
2026-02-04 23:07:22 +05:30
|
|
|
if gateway is None:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
|
|
|
detail="Board gateway_id is invalid",
|
|
|
|
|
)
|
|
|
|
|
if not gateway.url:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
|
|
|
detail="Gateway url is required",
|
|
|
|
|
)
|
|
|
|
|
return (
|
|
|
|
|
board,
|
|
|
|
|
GatewayClientConfig(url=gateway.url, token=gateway.token),
|
|
|
|
|
gateway.main_session_key,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
async def _require_gateway(
|
2026-02-08 21:16:26 +05:30
|
|
|
session: AsyncSession, board_id: str | None, *, user: object | None = None
|
2026-02-04 23:07:22 +05:30
|
|
|
) -> tuple[Board, GatewayClientConfig, str | None]:
|
2026-02-08 21:16:26 +05:30
|
|
|
board, config, main_session = await _resolve_gateway(
|
|
|
|
|
session,
|
|
|
|
|
board_id,
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
user=user,
|
|
|
|
|
)
|
2026-02-04 23:07:22 +05:30
|
|
|
if board is None:
|
2026-02-04 16:04:52 +05:30
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
2026-02-04 23:07:22 +05:30
|
|
|
detail="board_id is required",
|
2026-02-04 16:04:52 +05:30
|
|
|
)
|
2026-02-04 23:07:22 +05:30
|
|
|
return board, config, main_session
|
2026-02-04 16:04:52 +05:30
|
|
|
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
@router.get("/status", response_model=GatewaysStatusResponse)
|
2026-02-04 23:07:22 +05:30
|
|
|
async def gateways_status(
|
2026-02-06 16:12:04 +05:30
|
|
|
params: GatewayResolveQuery = Depends(),
|
|
|
|
|
session: AsyncSession = Depends(get_session),
|
2026-02-04 23:07:22 +05:30
|
|
|
auth: AuthContext = Depends(get_auth_context),
|
2026-02-08 21:16:26 +05:30
|
|
|
ctx: OrganizationContext = Depends(require_org_admin),
|
2026-02-06 16:12:04 +05:30
|
|
|
) -> GatewaysStatusResponse:
|
|
|
|
|
board, config, main_session = await _resolve_gateway(
|
2026-02-04 21:44:21 +05:30
|
|
|
session,
|
2026-02-06 16:12:04 +05:30
|
|
|
params.board_id,
|
|
|
|
|
params.gateway_url,
|
|
|
|
|
params.gateway_token,
|
|
|
|
|
params.gateway_main_session_key,
|
2026-02-08 21:16:26 +05:30
|
|
|
user=auth.user,
|
2026-02-04 21:44:21 +05:30
|
|
|
)
|
2026-02-08 21:16:26 +05:30
|
|
|
if board is not None and board.organization_id != ctx.organization.id:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
2026-02-04 02:28:51 +05:30
|
|
|
try:
|
2026-02-04 16:04:52 +05:30
|
|
|
sessions = await openclaw_call("sessions.list", config=config)
|
2026-02-04 02:28:51 +05:30
|
|
|
if isinstance(sessions, dict):
|
|
|
|
|
sessions_list = list(sessions.get("sessions") or [])
|
|
|
|
|
else:
|
|
|
|
|
sessions_list = list(sessions or [])
|
2026-02-04 14:58:14 +05:30
|
|
|
main_session_entry: object | None = None
|
|
|
|
|
main_session_error: str | None = None
|
|
|
|
|
if main_session:
|
|
|
|
|
try:
|
2026-02-05 00:21:33 +05:30
|
|
|
ensured = await ensure_session(main_session, config=config, label="Main Agent")
|
2026-02-04 14:58:14 +05:30
|
|
|
if isinstance(ensured, dict):
|
|
|
|
|
main_session_entry = ensured.get("entry") or ensured
|
|
|
|
|
except OpenClawGatewayError as exc:
|
|
|
|
|
main_session_error = str(exc)
|
2026-02-06 16:12:04 +05:30
|
|
|
return GatewaysStatusResponse(
|
|
|
|
|
connected=True,
|
|
|
|
|
gateway_url=config.url,
|
|
|
|
|
sessions_count=len(sessions_list),
|
|
|
|
|
sessions=sessions_list,
|
|
|
|
|
main_session_key=main_session,
|
|
|
|
|
main_session=main_session_entry,
|
|
|
|
|
main_session_error=main_session_error,
|
|
|
|
|
)
|
2026-02-04 02:28:51 +05:30
|
|
|
except OpenClawGatewayError as exc:
|
2026-02-06 16:12:04 +05:30
|
|
|
return GatewaysStatusResponse(connected=False, gateway_url=config.url, error=str(exc))
|
2026-02-04 02:28:51 +05:30
|
|
|
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
@router.get("/sessions", response_model=GatewaySessionsResponse)
|
2026-02-04 23:07:22 +05:30
|
|
|
async def list_gateway_sessions(
|
2026-02-04 16:04:52 +05:30
|
|
|
board_id: str | None = Query(default=None),
|
2026-02-06 16:12:04 +05:30
|
|
|
session: AsyncSession = Depends(get_session),
|
2026-02-04 23:07:22 +05:30
|
|
|
auth: AuthContext = Depends(get_auth_context),
|
2026-02-08 21:16:26 +05:30
|
|
|
ctx: OrganizationContext = Depends(require_org_admin),
|
2026-02-06 16:12:04 +05:30
|
|
|
) -> GatewaySessionsResponse:
|
|
|
|
|
board, config, main_session = await _resolve_gateway(
|
2026-02-04 21:44:21 +05:30
|
|
|
session,
|
|
|
|
|
board_id,
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
None,
|
2026-02-08 21:16:26 +05:30
|
|
|
user=auth.user,
|
2026-02-04 21:44:21 +05:30
|
|
|
)
|
2026-02-08 21:16:26 +05:30
|
|
|
if board is not None and board.organization_id != ctx.organization.id:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
2026-02-04 02:28:51 +05:30
|
|
|
try:
|
2026-02-04 16:04:52 +05:30
|
|
|
sessions = await openclaw_call("sessions.list", config=config)
|
2026-02-04 02:28:51 +05:30
|
|
|
except OpenClawGatewayError as exc:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail=str(exc)) from exc
|
|
|
|
|
if isinstance(sessions, dict):
|
2026-02-04 14:58:14 +05:30
|
|
|
sessions_list = list(sessions.get("sessions") or [])
|
|
|
|
|
else:
|
|
|
|
|
sessions_list = list(sessions or [])
|
|
|
|
|
|
|
|
|
|
main_session_entry: object | None = None
|
|
|
|
|
if main_session:
|
|
|
|
|
try:
|
2026-02-05 00:21:33 +05:30
|
|
|
ensured = await ensure_session(main_session, config=config, label="Main Agent")
|
2026-02-04 14:58:14 +05:30
|
|
|
if isinstance(ensured, dict):
|
|
|
|
|
main_session_entry = ensured.get("entry") or ensured
|
|
|
|
|
except OpenClawGatewayError:
|
|
|
|
|
main_session_entry = None
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
return GatewaySessionsResponse(
|
|
|
|
|
sessions=sessions_list,
|
|
|
|
|
main_session_key=main_session,
|
|
|
|
|
main_session=main_session_entry,
|
|
|
|
|
)
|
2026-02-04 02:28:51 +05:30
|
|
|
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
@router.get("/sessions/{session_id}", response_model=GatewaySessionResponse)
|
2026-02-04 16:04:52 +05:30
|
|
|
async def get_gateway_session(
|
|
|
|
|
session_id: str,
|
|
|
|
|
board_id: str | None = Query(default=None),
|
2026-02-06 16:12:04 +05:30
|
|
|
session: AsyncSession = Depends(get_session),
|
2026-02-04 23:07:22 +05:30
|
|
|
auth: AuthContext = Depends(get_auth_context),
|
2026-02-08 21:16:26 +05:30
|
|
|
ctx: OrganizationContext = Depends(require_org_admin),
|
2026-02-06 16:12:04 +05:30
|
|
|
) -> GatewaySessionResponse:
|
|
|
|
|
board, config, main_session = await _resolve_gateway(
|
2026-02-04 21:44:21 +05:30
|
|
|
session,
|
|
|
|
|
board_id,
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
None,
|
2026-02-08 21:16:26 +05:30
|
|
|
user=auth.user,
|
2026-02-04 21:44:21 +05:30
|
|
|
)
|
2026-02-08 21:16:26 +05:30
|
|
|
if board is not None and board.organization_id != ctx.organization.id:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
2026-02-04 02:28:51 +05:30
|
|
|
try:
|
2026-02-04 16:04:52 +05:30
|
|
|
sessions = await openclaw_call("sessions.list", config=config)
|
2026-02-04 02:28:51 +05:30
|
|
|
except OpenClawGatewayError as exc:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail=str(exc)) from exc
|
|
|
|
|
if isinstance(sessions, dict):
|
|
|
|
|
sessions_list = list(sessions.get("sessions") or [])
|
|
|
|
|
else:
|
|
|
|
|
sessions_list = list(sessions or [])
|
2026-02-05 00:21:33 +05:30
|
|
|
if main_session and not any(session.get("key") == main_session for session in sessions_list):
|
2026-02-04 14:58:14 +05:30
|
|
|
try:
|
2026-02-04 16:04:52 +05:30
|
|
|
await ensure_session(main_session, config=config, label="Main Agent")
|
|
|
|
|
refreshed = await openclaw_call("sessions.list", config=config)
|
2026-02-04 14:58:14 +05:30
|
|
|
if isinstance(refreshed, dict):
|
|
|
|
|
sessions_list = list(refreshed.get("sessions") or [])
|
|
|
|
|
else:
|
|
|
|
|
sessions_list = list(refreshed or [])
|
|
|
|
|
except OpenClawGatewayError:
|
|
|
|
|
pass
|
2026-02-05 00:21:33 +05:30
|
|
|
session_entry = next((item for item in sessions_list if item.get("key") == session_id), None)
|
2026-02-04 16:04:52 +05:30
|
|
|
if session_entry is None and main_session and session_id == main_session:
|
2026-02-04 14:58:14 +05:30
|
|
|
try:
|
2026-02-04 16:04:52 +05:30
|
|
|
ensured = await ensure_session(main_session, config=config, label="Main Agent")
|
2026-02-04 14:58:14 +05:30
|
|
|
if isinstance(ensured, dict):
|
2026-02-04 16:04:52 +05:30
|
|
|
session_entry = ensured.get("entry") or ensured
|
2026-02-04 14:58:14 +05:30
|
|
|
except OpenClawGatewayError:
|
2026-02-04 16:04:52 +05:30
|
|
|
session_entry = None
|
|
|
|
|
if session_entry is None:
|
2026-02-04 02:28:51 +05:30
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Session not found")
|
2026-02-06 16:12:04 +05:30
|
|
|
return GatewaySessionResponse(session=session_entry)
|
2026-02-04 02:28:51 +05:30
|
|
|
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
@router.get("/sessions/{session_id}/history", response_model=GatewaySessionHistoryResponse)
|
2026-02-04 03:57:19 +05:30
|
|
|
async def get_session_history(
|
2026-02-04 16:04:52 +05:30
|
|
|
session_id: str,
|
|
|
|
|
board_id: str | None = Query(default=None),
|
2026-02-06 16:12:04 +05:30
|
|
|
session: AsyncSession = Depends(get_session),
|
2026-02-04 23:07:22 +05:30
|
|
|
auth: AuthContext = Depends(get_auth_context),
|
2026-02-08 21:16:26 +05:30
|
|
|
ctx: OrganizationContext = Depends(require_org_admin),
|
2026-02-06 16:12:04 +05:30
|
|
|
) -> GatewaySessionHistoryResponse:
|
2026-02-08 21:16:26 +05:30
|
|
|
board, config, _ = await _require_gateway(session, board_id, user=auth.user)
|
|
|
|
|
if board.organization_id != ctx.organization.id:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
2026-02-04 02:28:51 +05:30
|
|
|
try:
|
2026-02-04 16:04:52 +05:30
|
|
|
history = await get_chat_history(session_id, config=config)
|
2026-02-04 02:28:51 +05:30
|
|
|
except OpenClawGatewayError as exc:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail=str(exc)) from exc
|
|
|
|
|
if isinstance(history, dict) and isinstance(history.get("messages"), list):
|
2026-02-06 16:12:04 +05:30
|
|
|
return GatewaySessionHistoryResponse(history=history["messages"])
|
|
|
|
|
return GatewaySessionHistoryResponse(history=list(history or []))
|
2026-02-04 02:28:51 +05:30
|
|
|
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
@router.post("/sessions/{session_id}/message", response_model=OkResponse)
|
2026-02-04 23:07:22 +05:30
|
|
|
async def send_gateway_session_message(
|
2026-02-04 02:28:51 +05:30
|
|
|
session_id: str,
|
2026-02-06 16:12:04 +05:30
|
|
|
payload: GatewaySessionMessageRequest,
|
2026-02-04 16:04:52 +05:30
|
|
|
board_id: str | None = Query(default=None),
|
2026-02-06 16:12:04 +05:30
|
|
|
session: AsyncSession = Depends(get_session),
|
2026-02-04 23:07:22 +05:30
|
|
|
auth: AuthContext = Depends(get_auth_context),
|
2026-02-08 21:16:26 +05:30
|
|
|
ctx: OrganizationContext = Depends(require_org_admin),
|
2026-02-06 16:12:04 +05:30
|
|
|
) -> OkResponse:
|
2026-02-08 21:16:26 +05:30
|
|
|
board, config, main_session = await _require_gateway(session, board_id, user=auth.user)
|
|
|
|
|
if board.organization_id != ctx.organization.id:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
|
|
|
|
if auth.user is None:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
|
|
|
|
await require_board_access(session, user=auth.user, board=board, write=True)
|
2026-02-04 02:28:51 +05:30
|
|
|
try:
|
2026-02-04 14:58:14 +05:30
|
|
|
if main_session and session_id == main_session:
|
2026-02-04 16:04:52 +05:30
|
|
|
await ensure_session(main_session, config=config, label="Main Agent")
|
2026-02-06 16:12:04 +05:30
|
|
|
await send_message(payload.content, session_key=session_id, config=config)
|
2026-02-04 02:28:51 +05:30
|
|
|
except OpenClawGatewayError as exc:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail=str(exc)) from exc
|
2026-02-06 16:12:04 +05:30
|
|
|
return OkResponse()
|
2026-02-04 17:05:58 +05:30
|
|
|
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
@router.get("/commands", response_model=GatewayCommandsResponse)
|
2026-02-04 17:05:58 +05:30
|
|
|
async def gateway_commands(
|
2026-02-04 23:07:22 +05:30
|
|
|
auth: AuthContext = Depends(get_auth_context),
|
2026-02-08 21:16:26 +05:30
|
|
|
_ctx: OrganizationContext = Depends(require_org_admin),
|
2026-02-06 16:12:04 +05:30
|
|
|
) -> GatewayCommandsResponse:
|
|
|
|
|
return GatewayCommandsResponse(
|
|
|
|
|
protocol_version=PROTOCOL_VERSION,
|
|
|
|
|
methods=GATEWAY_METHODS,
|
|
|
|
|
events=GATEWAY_EVENTS,
|
|
|
|
|
)
|