refactor: simplify code formatting and improve readability across multiple files
This commit is contained in:
@@ -91,7 +91,8 @@ async def _resolve_gateway(
|
||||
board = await Board.objects.by_id(params.board_id).first(session)
|
||||
if board is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Board not found",
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Board not found",
|
||||
)
|
||||
if user is not None:
|
||||
await require_board_access(session, user=user, board=board, write=False)
|
||||
@@ -119,7 +120,10 @@ async def _resolve_gateway(
|
||||
|
||||
|
||||
async def _require_gateway(
|
||||
session: AsyncSession, board_id: str | None, *, user: User | None = None,
|
||||
session: AsyncSession,
|
||||
board_id: str | None,
|
||||
*,
|
||||
user: User | None = None,
|
||||
) -> tuple[Board, GatewayClientConfig, str | None]:
|
||||
params = GatewayResolveQuery(board_id=board_id)
|
||||
board, config, main_session = await _resolve_gateway(
|
||||
@@ -161,7 +165,9 @@ async def gateways_status(
|
||||
if main_session:
|
||||
try:
|
||||
ensured = await ensure_session(
|
||||
main_session, config=config, label="Main Agent",
|
||||
main_session,
|
||||
config=config,
|
||||
label="Main Agent",
|
||||
)
|
||||
if isinstance(ensured, dict):
|
||||
main_session_entry = ensured.get("entry") or ensured
|
||||
@@ -178,7 +184,9 @@ async def gateways_status(
|
||||
)
|
||||
except OpenClawGatewayError as exc:
|
||||
return GatewaysStatusResponse(
|
||||
connected=False, gateway_url=config.url, error=str(exc),
|
||||
connected=False,
|
||||
gateway_url=config.url,
|
||||
error=str(exc),
|
||||
)
|
||||
|
||||
|
||||
@@ -202,7 +210,8 @@ async def list_gateway_sessions(
|
||||
sessions = await openclaw_call("sessions.list", config=config)
|
||||
except OpenClawGatewayError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_502_BAD_GATEWAY, detail=str(exc),
|
||||
status_code=status.HTTP_502_BAD_GATEWAY,
|
||||
detail=str(exc),
|
||||
) from exc
|
||||
if isinstance(sessions, dict):
|
||||
sessions_list = _as_object_list(sessions.get("sessions"))
|
||||
@@ -213,7 +222,9 @@ async def list_gateway_sessions(
|
||||
if main_session:
|
||||
try:
|
||||
ensured = await ensure_session(
|
||||
main_session, config=config, label="Main Agent",
|
||||
main_session,
|
||||
config=config,
|
||||
label="Main Agent",
|
||||
)
|
||||
if isinstance(ensured, dict):
|
||||
main_session_entry = ensured.get("entry") or ensured
|
||||
@@ -233,11 +244,7 @@ async def _list_sessions(config: GatewayClientConfig) -> list[dict[str, object]]
|
||||
raw_items = _as_object_list(sessions.get("sessions"))
|
||||
else:
|
||||
raw_items = _as_object_list(sessions)
|
||||
return [
|
||||
item
|
||||
for item in raw_items
|
||||
if isinstance(item, dict)
|
||||
]
|
||||
return [item for item in raw_items if isinstance(item, dict)]
|
||||
|
||||
|
||||
async def _with_main_session(
|
||||
@@ -246,9 +253,7 @@ async def _with_main_session(
|
||||
config: GatewayClientConfig,
|
||||
main_session: str | None,
|
||||
) -> list[dict[str, object]]:
|
||||
if not main_session or any(
|
||||
item.get("key") == main_session for item in sessions_list
|
||||
):
|
||||
if not main_session or any(item.get("key") == main_session for item in sessions_list):
|
||||
return sessions_list
|
||||
try:
|
||||
await ensure_session(main_session, config=config, label="Main Agent")
|
||||
@@ -278,7 +283,8 @@ async def get_gateway_session(
|
||||
sessions_list = await _list_sessions(config)
|
||||
except OpenClawGatewayError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_502_BAD_GATEWAY, detail=str(exc),
|
||||
status_code=status.HTTP_502_BAD_GATEWAY,
|
||||
detail=str(exc),
|
||||
) from exc
|
||||
sessions_list = await _with_main_session(
|
||||
sessions_list,
|
||||
@@ -286,12 +292,15 @@ async def get_gateway_session(
|
||||
main_session=main_session,
|
||||
)
|
||||
session_entry = next(
|
||||
(item for item in sessions_list if item.get("key") == session_id), None,
|
||||
(item for item in sessions_list if item.get("key") == session_id),
|
||||
None,
|
||||
)
|
||||
if session_entry is None and main_session and session_id == main_session:
|
||||
try:
|
||||
ensured = await ensure_session(
|
||||
main_session, config=config, label="Main Agent",
|
||||
main_session,
|
||||
config=config,
|
||||
label="Main Agent",
|
||||
)
|
||||
if isinstance(ensured, dict):
|
||||
session_entry = ensured.get("entry") or ensured
|
||||
@@ -299,13 +308,15 @@ async def get_gateway_session(
|
||||
session_entry = None
|
||||
if session_entry is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Session not found",
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Session not found",
|
||||
)
|
||||
return GatewaySessionResponse(session=session_entry)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/sessions/{session_id}/history", response_model=GatewaySessionHistoryResponse,
|
||||
"/sessions/{session_id}/history",
|
||||
response_model=GatewaySessionHistoryResponse,
|
||||
)
|
||||
async def get_session_history(
|
||||
session_id: str,
|
||||
@@ -322,7 +333,8 @@ async def get_session_history(
|
||||
history = await get_chat_history(session_id, config=config)
|
||||
except OpenClawGatewayError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_502_BAD_GATEWAY, detail=str(exc),
|
||||
status_code=status.HTTP_502_BAD_GATEWAY,
|
||||
detail=str(exc),
|
||||
) from exc
|
||||
if isinstance(history, dict) and isinstance(history.get("messages"), list):
|
||||
return GatewaySessionHistoryResponse(history=history["messages"])
|
||||
@@ -339,7 +351,9 @@ async def send_gateway_session_message(
|
||||
) -> OkResponse:
|
||||
"""Send a message into a specific gateway session."""
|
||||
board, config, main_session = await _require_gateway(
|
||||
session, board_id, user=auth.user,
|
||||
session,
|
||||
board_id,
|
||||
user=auth.user,
|
||||
)
|
||||
if auth.user is None:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
||||
@@ -350,7 +364,8 @@ async def send_gateway_session_message(
|
||||
await send_message(payload.content, session_key=session_id, config=config)
|
||||
except OpenClawGatewayError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_502_BAD_GATEWAY, detail=str(exc),
|
||||
status_code=status.HTTP_502_BAD_GATEWAY,
|
||||
detail=str(exc),
|
||||
) from exc
|
||||
return OkResponse()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user