refactor: replace SQLModel with QueryModel in various models and update query methods
This commit is contained in:
@@ -102,7 +102,7 @@ def _guard_board_access(agent_ctx: AgentAuthContext, board: Board) -> None:
|
||||
async def _gateway_config(session: AsyncSession, board: Board) -> GatewayClientConfig:
|
||||
if not board.gateway_id:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
gateway = await session.get(Gateway, board.gateway_id)
|
||||
gateway = await Gateway.objects.by_id(board.gateway_id).first(session)
|
||||
if gateway is None or not gateway.url:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
return GatewayClientConfig(url=gateway.url, token=gateway.token)
|
||||
@@ -117,9 +117,7 @@ async def _require_gateway_main(
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Agent missing session key"
|
||||
)
|
||||
gateway = (
|
||||
await session.exec(select(Gateway).where(col(Gateway.main_session_key) == session_key))
|
||||
).first()
|
||||
gateway = await Gateway.objects.filter_by(main_session_key=session_key).first(session)
|
||||
if gateway is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
@@ -139,7 +137,7 @@ async def _require_gateway_board(
|
||||
gateway: Gateway,
|
||||
board_id: UUID | str,
|
||||
) -> Board:
|
||||
board = await session.get(Board, board_id)
|
||||
board = await Board.objects.by_id(board_id).first(session)
|
||||
if board is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Board not found")
|
||||
if board.gateway_id != gateway.id:
|
||||
@@ -254,7 +252,7 @@ async def create_task(
|
||||
},
|
||||
)
|
||||
if task.assigned_agent_id:
|
||||
agent = await session.get(Agent, task.assigned_agent_id)
|
||||
agent = await Agent.objects.by_id(task.assigned_agent_id).first(session)
|
||||
if agent is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if agent.is_board_lead:
|
||||
@@ -286,7 +284,7 @@ async def create_task(
|
||||
)
|
||||
await session.commit()
|
||||
if task.assigned_agent_id:
|
||||
assigned_agent = await session.get(Agent, task.assigned_agent_id)
|
||||
assigned_agent = await Agent.objects.by_id(task.assigned_agent_id).first(session)
|
||||
if assigned_agent:
|
||||
await tasks_api._notify_agent_on_task_assign(
|
||||
session=session,
|
||||
@@ -466,7 +464,7 @@ async def nudge_agent(
|
||||
_guard_board_access(agent_ctx, board)
|
||||
if not agent_ctx.agent.is_board_lead:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
target = await session.get(Agent, agent_id)
|
||||
target = await Agent.objects.by_id(agent_id).first(session)
|
||||
if target is None or (target.board_id and target.board_id != board.id):
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if not target.openclaw_session_id:
|
||||
@@ -528,7 +526,7 @@ async def get_agent_soul(
|
||||
_guard_board_access(agent_ctx, board)
|
||||
if not agent_ctx.agent.is_board_lead and str(agent_ctx.agent.id) != agent_id:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
target = await session.get(Agent, agent_id)
|
||||
target = await Agent.objects.by_id(agent_id).first(session)
|
||||
if target is None or (target.board_id and target.board_id != board.id):
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
config = await _gateway_config(session, board)
|
||||
@@ -566,7 +564,7 @@ async def update_agent_soul(
|
||||
_guard_board_access(agent_ctx, board)
|
||||
if not agent_ctx.agent.is_board_lead:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
target = await session.get(Agent, agent_id)
|
||||
target = await Agent.objects.by_id(agent_id).first(session)
|
||||
if target is None or (target.board_id and target.board_id != board.id):
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
config = await _gateway_config(session, board)
|
||||
@@ -629,7 +627,7 @@ async def ask_user_via_gateway_main(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="Board is not attached to a gateway",
|
||||
)
|
||||
gateway = await session.get(Gateway, board.gateway_id)
|
||||
gateway = await Gateway.objects.by_id(board.gateway_id).first(session)
|
||||
if gateway is None or not gateway.url:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
@@ -689,9 +687,7 @@ async def ask_user_via_gateway_main(
|
||||
agent_id=agent_ctx.agent.id,
|
||||
)
|
||||
|
||||
main_agent = (
|
||||
await session.exec(select(Agent).where(col(Agent.openclaw_session_id) == main_session_key))
|
||||
).first()
|
||||
main_agent = await Agent.objects.filter_by(openclaw_session_id=main_session_key).first(session)
|
||||
|
||||
await session.commit()
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ async def _require_board(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="board_id is required",
|
||||
)
|
||||
board = await session.get(Board, board_id)
|
||||
board = await Board.objects.by_id(board_id).first(session)
|
||||
if board is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Board not found")
|
||||
if user is not None:
|
||||
@@ -125,7 +125,7 @@ async def _require_gateway(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="Board gateway_id is required",
|
||||
)
|
||||
gateway = await session.get(Gateway, board.gateway_id)
|
||||
gateway = await Gateway.objects.by_id(board.gateway_id).first(session)
|
||||
if gateway is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
@@ -182,9 +182,7 @@ async def _find_gateway_for_main_session(
|
||||
) -> Gateway | None:
|
||||
if not session_key:
|
||||
return None
|
||||
return (
|
||||
await session.exec(select(Gateway).where(Gateway.main_session_key == session_key))
|
||||
).first()
|
||||
return await Gateway.objects.filter_by(main_session_key=session_key).first(session)
|
||||
|
||||
|
||||
async def _ensure_gateway_session(
|
||||
@@ -237,7 +235,7 @@ async def _require_user_context(session: AsyncSession, user: User | None) -> Org
|
||||
member = await get_active_membership(session, user)
|
||||
if member is None:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
organization = await session.get(Organization, member.organization_id)
|
||||
organization = await Organization.objects.by_id(member.organization_id).first(session)
|
||||
if organization is None:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
return OrganizationContext(organization=organization, member=member)
|
||||
@@ -258,7 +256,7 @@ async def _require_agent_access(
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
return
|
||||
|
||||
board = await session.get(Board, agent.board_id)
|
||||
board = await Board.objects.by_id(agent.board_id).first(session)
|
||||
if board is None or board.organization_id != ctx.organization.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if not await has_board_access(session, member=ctx.member, board=board, write=write):
|
||||
@@ -323,7 +321,7 @@ async def list_agents(
|
||||
if board_id is not None:
|
||||
statement = statement.where(col(Agent.board_id) == board_id)
|
||||
if gateway_id is not None:
|
||||
gateway = await session.get(Gateway, gateway_id)
|
||||
gateway = await Gateway.objects.by_id(gateway_id).first(session)
|
||||
if gateway is None or gateway.organization_id != ctx.organization.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
statement = statement.join(Board, col(Agent.board_id) == col(Board.id)).where(
|
||||
@@ -532,7 +530,7 @@ async def get_agent(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
ctx: OrganizationContext = Depends(require_org_admin),
|
||||
) -> AgentRead:
|
||||
agent = await session.get(Agent, agent_id)
|
||||
agent = await Agent.objects.by_id(agent_id).first(session)
|
||||
if agent is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
await _require_agent_access(session, agent=agent, ctx=ctx, write=False)
|
||||
@@ -549,7 +547,7 @@ async def update_agent(
|
||||
auth: AuthContext = Depends(get_auth_context),
|
||||
ctx: OrganizationContext = Depends(require_org_admin),
|
||||
) -> AgentRead:
|
||||
agent = await session.get(Agent, agent_id)
|
||||
agent = await Agent.objects.by_id(agent_id).first(session)
|
||||
if agent is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
await _require_agent_access(session, agent=agent, ctx=ctx, write=True)
|
||||
@@ -728,7 +726,7 @@ async def heartbeat_agent(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
actor: ActorContext = Depends(require_admin_or_agent),
|
||||
) -> AgentRead:
|
||||
agent = await session.get(Agent, agent_id)
|
||||
agent = await Agent.objects.by_id(agent_id).first(session)
|
||||
if agent is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if actor.actor_type == "agent" and actor.agent and actor.agent.id != agent.id:
|
||||
@@ -767,7 +765,7 @@ async def heartbeat_or_create_agent(
|
||||
actor=actor,
|
||||
)
|
||||
|
||||
statement = select(Agent).where(Agent.name == payload.name)
|
||||
statement = Agent.objects.filter_by(name=payload.name).statement
|
||||
if payload.board_id is not None:
|
||||
statement = statement.where(Agent.board_id == payload.board_id)
|
||||
agent = (await session.exec(statement)).first()
|
||||
@@ -943,7 +941,7 @@ async def delete_agent(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
ctx: OrganizationContext = Depends(require_org_admin),
|
||||
) -> OkResponse:
|
||||
agent = await session.get(Agent, agent_id)
|
||||
agent = await Agent.objects.by_id(agent_id).first(session)
|
||||
if agent is None:
|
||||
return OkResponse()
|
||||
await _require_agent_access(session, agent=agent, ctx=ctx, write=True)
|
||||
|
||||
@@ -77,9 +77,8 @@ async def _fetch_approval_events(
|
||||
since: datetime,
|
||||
) -> list[Approval]:
|
||||
statement = (
|
||||
select(Approval)
|
||||
.where(col(Approval.board_id) == board_id)
|
||||
.where(
|
||||
Approval.objects.filter_by(board_id=board_id)
|
||||
.filter(
|
||||
or_(
|
||||
col(Approval.created_at) >= since,
|
||||
col(Approval.resolved_at) >= since,
|
||||
@@ -87,7 +86,7 @@ async def _fetch_approval_events(
|
||||
)
|
||||
.order_by(asc(col(Approval.created_at)))
|
||||
)
|
||||
return list(await session.exec(statement))
|
||||
return await statement.all(session)
|
||||
|
||||
|
||||
@router.get("", response_model=DefaultLimitOffsetPage[ApprovalRead])
|
||||
@@ -97,11 +96,11 @@ async def list_approvals(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
actor: ActorContext = Depends(require_admin_or_agent),
|
||||
) -> DefaultLimitOffsetPage[ApprovalRead]:
|
||||
statement = select(Approval).where(col(Approval.board_id) == board.id)
|
||||
statement = Approval.objects.filter_by(board_id=board.id)
|
||||
if status_filter:
|
||||
statement = statement.where(col(Approval.status) == status_filter)
|
||||
statement = statement.filter(col(Approval.status) == status_filter)
|
||||
statement = statement.order_by(col(Approval.created_at).desc())
|
||||
return await paginate(session, statement)
|
||||
return await paginate(session, statement.statement)
|
||||
|
||||
|
||||
@router.get("/stream")
|
||||
@@ -207,7 +206,7 @@ async def update_approval(
|
||||
board: Board = Depends(get_board_for_user_write),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> Approval:
|
||||
approval = await session.get(Approval, approval_id)
|
||||
approval = await Approval.objects.by_id(approval_id).first(session)
|
||||
if approval is None or approval.board_id != board.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
updates = payload.model_dump(exclude_unset=True)
|
||||
|
||||
@@ -8,7 +8,7 @@ from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request, status
|
||||
from sqlalchemy import func
|
||||
from sqlmodel import col, select
|
||||
from sqlmodel import col
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
from sse_starlette.sse import EventSourceResponse
|
||||
|
||||
@@ -71,7 +71,7 @@ def _serialize_memory(memory: BoardGroupMemory) -> dict[str, object]:
|
||||
async def _gateway_config(session: AsyncSession, board: Board) -> GatewayClientConfig | None:
|
||||
if board.gateway_id is None:
|
||||
return None
|
||||
gateway = await session.get(Gateway, board.gateway_id)
|
||||
gateway = await Gateway.objects.by_id(board.gateway_id).first(session)
|
||||
if gateway is None or not gateway.url:
|
||||
return None
|
||||
return GatewayClientConfig(url=gateway.url, token=gateway.token)
|
||||
@@ -96,17 +96,17 @@ async def _fetch_memory_events(
|
||||
is_chat: bool | None = None,
|
||||
) -> list[BoardGroupMemory]:
|
||||
statement = (
|
||||
select(BoardGroupMemory).where(col(BoardGroupMemory.board_group_id) == board_group_id)
|
||||
BoardGroupMemory.objects.filter_by(board_group_id=board_group_id)
|
||||
# Old/invalid rows (empty/whitespace-only content) can exist; exclude them to
|
||||
# satisfy the NonEmptyStr response schema.
|
||||
.where(func.length(func.trim(col(BoardGroupMemory.content))) > 0)
|
||||
.filter(func.length(func.trim(col(BoardGroupMemory.content))) > 0)
|
||||
)
|
||||
if is_chat is not None:
|
||||
statement = statement.where(col(BoardGroupMemory.is_chat) == is_chat)
|
||||
statement = statement.where(col(BoardGroupMemory.created_at) >= since).order_by(
|
||||
statement = statement.filter(col(BoardGroupMemory.is_chat) == is_chat)
|
||||
statement = statement.filter(col(BoardGroupMemory.created_at) >= since).order_by(
|
||||
col(BoardGroupMemory.created_at)
|
||||
)
|
||||
return list(await session.exec(statement))
|
||||
return await statement.all(session)
|
||||
|
||||
|
||||
async def _require_group_access(
|
||||
@@ -116,7 +116,7 @@ async def _require_group_access(
|
||||
ctx: OrganizationContext,
|
||||
write: bool,
|
||||
) -> BoardGroup:
|
||||
group = await session.get(BoardGroup, group_id)
|
||||
group = await BoardGroup.objects.by_id(group_id).first(session)
|
||||
if group is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if group.organization_id != ctx.member.organization_id:
|
||||
@@ -127,9 +127,9 @@ async def _require_group_access(
|
||||
if not write and member_all_boards_read(ctx.member):
|
||||
return group
|
||||
|
||||
board_ids = list(
|
||||
await session.exec(select(Board.id).where(col(Board.board_group_id) == group_id))
|
||||
)
|
||||
board_ids = [
|
||||
board.id for board in await Board.objects.filter_by(board_group_id=group_id).all(session)
|
||||
]
|
||||
if not board_ids:
|
||||
if is_org_admin(ctx.member):
|
||||
return group
|
||||
@@ -156,12 +156,12 @@ async def _notify_group_memory_targets(
|
||||
is_broadcast = "broadcast" in tags or "all" in mentions
|
||||
|
||||
# Fetch group boards + agents.
|
||||
boards = list(await session.exec(select(Board).where(col(Board.board_group_id) == group.id)))
|
||||
boards = await Board.objects.filter_by(board_group_id=group.id).all(session)
|
||||
if not boards:
|
||||
return
|
||||
board_by_id = {board.id: board for board in boards}
|
||||
board_ids = list(board_by_id.keys())
|
||||
agents = list(await session.exec(select(Agent).where(col(Agent.board_id).in_(board_ids))))
|
||||
agents = await Agent.objects.by_field_in("board_id", board_ids).all(session)
|
||||
|
||||
targets: dict[str, Agent] = {}
|
||||
for agent in agents:
|
||||
@@ -242,15 +242,15 @@ async def list_board_group_memory(
|
||||
) -> DefaultLimitOffsetPage[BoardGroupMemoryRead]:
|
||||
await _require_group_access(session, group_id=group_id, ctx=ctx, write=False)
|
||||
statement = (
|
||||
select(BoardGroupMemory).where(col(BoardGroupMemory.board_group_id) == group_id)
|
||||
BoardGroupMemory.objects.filter_by(board_group_id=group_id)
|
||||
# Old/invalid rows (empty/whitespace-only content) can exist; exclude them to
|
||||
# satisfy the NonEmptyStr response schema.
|
||||
.where(func.length(func.trim(col(BoardGroupMemory.content))) > 0)
|
||||
.filter(func.length(func.trim(col(BoardGroupMemory.content))) > 0)
|
||||
)
|
||||
if is_chat is not None:
|
||||
statement = statement.where(col(BoardGroupMemory.is_chat) == is_chat)
|
||||
statement = statement.filter(col(BoardGroupMemory.is_chat) == is_chat)
|
||||
statement = statement.order_by(col(BoardGroupMemory.created_at).desc())
|
||||
return await paginate(session, statement)
|
||||
return await paginate(session, statement.statement)
|
||||
|
||||
|
||||
@group_router.get("/stream")
|
||||
@@ -297,7 +297,7 @@ async def create_board_group_memory(
|
||||
) -> BoardGroupMemory:
|
||||
group = await _require_group_access(session, group_id=group_id, ctx=ctx, write=True)
|
||||
|
||||
user = await session.get(User, ctx.member.user_id)
|
||||
user = await User.objects.by_id(ctx.member.user_id).first(session)
|
||||
actor = ActorContext(actor_type="user", user=user)
|
||||
tags = set(payload.tags or [])
|
||||
is_chat = "chat" in tags
|
||||
@@ -332,19 +332,18 @@ async def list_board_group_memory_for_board(
|
||||
) -> DefaultLimitOffsetPage[BoardGroupMemoryRead]:
|
||||
group_id = board.board_group_id
|
||||
if group_id is None:
|
||||
statement = select(BoardGroupMemory).where(col(BoardGroupMemory.id).is_(None))
|
||||
return await paginate(session, statement)
|
||||
return await paginate(session, BoardGroupMemory.objects.by_ids([]).statement)
|
||||
|
||||
statement = (
|
||||
select(BoardGroupMemory).where(col(BoardGroupMemory.board_group_id) == group_id)
|
||||
queryset = (
|
||||
BoardGroupMemory.objects.filter_by(board_group_id=group_id)
|
||||
# Old/invalid rows (empty/whitespace-only content) can exist; exclude them to
|
||||
# satisfy the NonEmptyStr response schema.
|
||||
.where(func.length(func.trim(col(BoardGroupMemory.content))) > 0)
|
||||
.filter(func.length(func.trim(col(BoardGroupMemory.content))) > 0)
|
||||
)
|
||||
if is_chat is not None:
|
||||
statement = statement.where(col(BoardGroupMemory.is_chat) == is_chat)
|
||||
statement = statement.order_by(col(BoardGroupMemory.created_at).desc())
|
||||
return await paginate(session, statement)
|
||||
queryset = queryset.filter(col(BoardGroupMemory.is_chat) == is_chat)
|
||||
queryset = queryset.order_by(col(BoardGroupMemory.created_at).desc())
|
||||
return await paginate(session, queryset.statement)
|
||||
|
||||
|
||||
@board_router.get("/stream")
|
||||
@@ -396,7 +395,7 @@ async def create_board_group_memory_for_board(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="Board is not in a board group",
|
||||
)
|
||||
group = await session.get(BoardGroup, group_id)
|
||||
group = await BoardGroup.objects.by_id(group_id).first(session)
|
||||
if group is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ async def _require_group_access(
|
||||
member: OrganizationMember,
|
||||
write: bool,
|
||||
) -> BoardGroup:
|
||||
group = await session.get(BoardGroup, group_id)
|
||||
group = await BoardGroup.objects.by_id(group_id).first(session)
|
||||
if group is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if group.organization_id != member.organization_id:
|
||||
@@ -67,9 +67,9 @@ async def _require_group_access(
|
||||
if not write and member_all_boards_read(member):
|
||||
return group
|
||||
|
||||
board_ids = list(
|
||||
await session.exec(select(Board.id).where(col(Board.board_group_id) == group_id))
|
||||
)
|
||||
board_ids = [
|
||||
board.id for board in await Board.objects.filter_by(board_group_id=group_id).all(session)
|
||||
]
|
||||
if not board_ids:
|
||||
if is_org_admin(member):
|
||||
return group
|
||||
@@ -153,7 +153,7 @@ async def apply_board_group_heartbeat(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
actor: ActorContext = Depends(require_admin_or_agent),
|
||||
) -> BoardGroupHeartbeatApplyResult:
|
||||
group = await session.get(BoardGroup, group_id)
|
||||
group = await BoardGroup.objects.by_id(group_id).first(session)
|
||||
if group is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
@@ -181,11 +181,11 @@ async def apply_board_group_heartbeat(
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
if not agent.is_board_lead:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
board = await session.get(Board, agent.board_id)
|
||||
board = await Board.objects.by_id(agent.board_id).first(session)
|
||||
if board is None or board.board_group_id != group_id:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
boards = list(await session.exec(select(Board).where(col(Board.board_group_id) == group_id)))
|
||||
boards = await Board.objects.filter_by(board_group_id=group_id).all(session)
|
||||
board_by_id = {board.id: board for board in boards}
|
||||
board_ids = list(board_by_id.keys())
|
||||
if not board_ids:
|
||||
@@ -196,7 +196,7 @@ async def apply_board_group_heartbeat(
|
||||
failed_agent_ids=[],
|
||||
)
|
||||
|
||||
agents = list(await session.exec(select(Agent).where(col(Agent.board_id).in_(board_ids))))
|
||||
agents = await Agent.objects.by_field_in("board_id", board_ids).all(session)
|
||||
if not payload.include_board_leads:
|
||||
agents = [agent for agent in agents if not agent.is_board_lead]
|
||||
|
||||
@@ -232,7 +232,7 @@ async def apply_board_group_heartbeat(
|
||||
|
||||
failed_agent_ids: list[UUID] = []
|
||||
gateway_ids = list(agents_by_gateway_id.keys())
|
||||
gateways = list(await session.exec(select(Gateway).where(col(Gateway.id).in_(gateway_ids))))
|
||||
gateways = await Gateway.objects.by_ids(gateway_ids).all(session)
|
||||
gateway_by_id = {gateway.id: gateway for gateway in gateways}
|
||||
for gateway_id, gateway_agents in agents_by_gateway_id.items():
|
||||
gateway = gateway_by_id.get(gateway_id)
|
||||
|
||||
@@ -8,7 +8,7 @@ from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, Query, Request
|
||||
from sqlalchemy import func
|
||||
from sqlmodel import col, select
|
||||
from sqlmodel import col
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
from sse_starlette.sse import EventSourceResponse
|
||||
|
||||
@@ -58,7 +58,7 @@ def _serialize_memory(memory: BoardMemory) -> dict[str, object]:
|
||||
async def _gateway_config(session: AsyncSession, board: Board) -> GatewayClientConfig | None:
|
||||
if board.gateway_id is None:
|
||||
return None
|
||||
gateway = await session.get(Gateway, board.gateway_id)
|
||||
gateway = await Gateway.objects.by_id(board.gateway_id).first(session)
|
||||
if gateway is None or not gateway.url:
|
||||
return None
|
||||
return GatewayClientConfig(url=gateway.url, token=gateway.token)
|
||||
@@ -83,17 +83,17 @@ async def _fetch_memory_events(
|
||||
is_chat: bool | None = None,
|
||||
) -> list[BoardMemory]:
|
||||
statement = (
|
||||
select(BoardMemory).where(col(BoardMemory.board_id) == board_id)
|
||||
BoardMemory.objects.filter_by(board_id=board_id)
|
||||
# Old/invalid rows (empty/whitespace-only content) can exist; exclude them to
|
||||
# satisfy the NonEmptyStr response schema.
|
||||
.where(func.length(func.trim(col(BoardMemory.content))) > 0)
|
||||
.filter(func.length(func.trim(col(BoardMemory.content))) > 0)
|
||||
)
|
||||
if is_chat is not None:
|
||||
statement = statement.where(col(BoardMemory.is_chat) == is_chat)
|
||||
statement = statement.where(col(BoardMemory.created_at) >= since).order_by(
|
||||
statement = statement.filter(col(BoardMemory.is_chat) == is_chat)
|
||||
statement = statement.filter(col(BoardMemory.created_at) >= since).order_by(
|
||||
col(BoardMemory.created_at)
|
||||
)
|
||||
return list(await session.exec(statement))
|
||||
return await statement.all(session)
|
||||
|
||||
|
||||
async def _notify_chat_targets(
|
||||
@@ -114,8 +114,7 @@ async def _notify_chat_targets(
|
||||
# Special-case control commands to reach all board agents.
|
||||
# These are intended to be parsed verbatim by agent runtimes.
|
||||
if command in {"/pause", "/resume"}:
|
||||
statement = select(Agent).where(col(Agent.board_id) == board.id)
|
||||
pause_targets: list[Agent] = list(await session.exec(statement))
|
||||
pause_targets: list[Agent] = await Agent.objects.filter_by(board_id=board.id).all(session)
|
||||
for agent in pause_targets:
|
||||
if actor.actor_type == "agent" and actor.agent and agent.id == actor.agent.id:
|
||||
continue
|
||||
@@ -134,9 +133,8 @@ async def _notify_chat_targets(
|
||||
return
|
||||
|
||||
mentions = extract_mentions(memory.content)
|
||||
statement = select(Agent).where(col(Agent.board_id) == board.id)
|
||||
targets: dict[str, Agent] = {}
|
||||
for agent in await session.exec(statement):
|
||||
for agent in await Agent.objects.filter_by(board_id=board.id).all(session):
|
||||
if agent.is_board_lead:
|
||||
targets[str(agent.id)] = agent
|
||||
continue
|
||||
@@ -188,15 +186,15 @@ async def list_board_memory(
|
||||
actor: ActorContext = Depends(require_admin_or_agent),
|
||||
) -> DefaultLimitOffsetPage[BoardMemoryRead]:
|
||||
statement = (
|
||||
select(BoardMemory).where(col(BoardMemory.board_id) == board.id)
|
||||
BoardMemory.objects.filter_by(board_id=board.id)
|
||||
# Old/invalid rows (empty/whitespace-only content) can exist; exclude them to
|
||||
# satisfy the NonEmptyStr response schema.
|
||||
.where(func.length(func.trim(col(BoardMemory.content))) > 0)
|
||||
.filter(func.length(func.trim(col(BoardMemory.content))) > 0)
|
||||
)
|
||||
if is_chat is not None:
|
||||
statement = statement.where(col(BoardMemory.is_chat) == is_chat)
|
||||
statement = statement.filter(col(BoardMemory.is_chat) == is_chat)
|
||||
statement = statement.order_by(col(BoardMemory.created_at).desc())
|
||||
return await paginate(session, statement)
|
||||
return await paginate(session, statement.statement)
|
||||
|
||||
|
||||
@router.get("/stream")
|
||||
|
||||
@@ -6,7 +6,7 @@ from uuid import uuid4
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from pydantic import ValidationError
|
||||
from sqlmodel import col, select
|
||||
from sqlmodel import col
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from app.api.deps import (
|
||||
@@ -50,7 +50,7 @@ async def _gateway_config(
|
||||
) -> tuple[Gateway, GatewayClientConfig]:
|
||||
if not board.gateway_id:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
gateway = await session.get(Gateway, board.gateway_id)
|
||||
gateway = await Gateway.objects.by_id(board.gateway_id).first(session)
|
||||
if gateway is None or not gateway.url or not gateway.main_session_key:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
return gateway, GatewayClientConfig(url=gateway.url, token=gateway.token)
|
||||
@@ -80,12 +80,10 @@ async def _ensure_lead_agent(
|
||||
identity_profile: dict[str, str] | None = None,
|
||||
) -> Agent:
|
||||
existing = (
|
||||
await session.exec(
|
||||
select(Agent)
|
||||
.where(Agent.board_id == board.id)
|
||||
.where(col(Agent.is_board_lead).is_(True))
|
||||
)
|
||||
).first()
|
||||
await Agent.objects.filter_by(board_id=board.id)
|
||||
.filter(col(Agent.is_board_lead).is_(True))
|
||||
.first(session)
|
||||
)
|
||||
if existing:
|
||||
desired_name = agent_name or _lead_agent_name(board)
|
||||
if existing.name != desired_name:
|
||||
@@ -147,12 +145,10 @@ async def get_onboarding(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> BoardOnboardingSession:
|
||||
onboarding = (
|
||||
await session.exec(
|
||||
select(BoardOnboardingSession)
|
||||
.where(BoardOnboardingSession.board_id == board.id)
|
||||
.order_by(col(BoardOnboardingSession.created_at).desc())
|
||||
)
|
||||
).first()
|
||||
await BoardOnboardingSession.objects.filter_by(board_id=board.id)
|
||||
.order_by(col(BoardOnboardingSession.updated_at).desc())
|
||||
.first(session)
|
||||
)
|
||||
if onboarding is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
return onboarding
|
||||
@@ -165,12 +161,10 @@ async def start_onboarding(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> BoardOnboardingSession:
|
||||
onboarding = (
|
||||
await session.exec(
|
||||
select(BoardOnboardingSession)
|
||||
.where(BoardOnboardingSession.board_id == board.id)
|
||||
.where(BoardOnboardingSession.status == "active")
|
||||
)
|
||||
).first()
|
||||
await BoardOnboardingSession.objects.filter_by(board_id=board.id)
|
||||
.filter(col(BoardOnboardingSession.status) == "active")
|
||||
.first(session)
|
||||
)
|
||||
if onboarding:
|
||||
return onboarding
|
||||
|
||||
@@ -248,12 +242,10 @@ async def answer_onboarding(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> BoardOnboardingSession:
|
||||
onboarding = (
|
||||
await session.exec(
|
||||
select(BoardOnboardingSession)
|
||||
.where(BoardOnboardingSession.board_id == board.id)
|
||||
.order_by(col(BoardOnboardingSession.created_at).desc())
|
||||
)
|
||||
).first()
|
||||
await BoardOnboardingSession.objects.filter_by(board_id=board.id)
|
||||
.order_by(col(BoardOnboardingSession.updated_at).desc())
|
||||
.first(session)
|
||||
)
|
||||
if onboarding is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
@@ -295,18 +287,16 @@ async def agent_onboarding_update(
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
if board.gateway_id:
|
||||
gateway = await session.get(Gateway, board.gateway_id)
|
||||
gateway = await Gateway.objects.by_id(board.gateway_id).first(session)
|
||||
if gateway and gateway.main_session_key and agent.openclaw_session_id:
|
||||
if agent.openclaw_session_id != gateway.main_session_key:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
onboarding = (
|
||||
await session.exec(
|
||||
select(BoardOnboardingSession)
|
||||
.where(BoardOnboardingSession.board_id == board.id)
|
||||
.order_by(col(BoardOnboardingSession.created_at).desc())
|
||||
)
|
||||
).first()
|
||||
await BoardOnboardingSession.objects.filter_by(board_id=board.id)
|
||||
.order_by(col(BoardOnboardingSession.updated_at).desc())
|
||||
.first(session)
|
||||
)
|
||||
if onboarding is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if onboarding.status == "confirmed":
|
||||
@@ -351,12 +341,10 @@ async def confirm_onboarding(
|
||||
auth: AuthContext = Depends(require_admin_auth),
|
||||
) -> Board:
|
||||
onboarding = (
|
||||
await session.exec(
|
||||
select(BoardOnboardingSession)
|
||||
.where(BoardOnboardingSession.board_id == board.id)
|
||||
.order_by(col(BoardOnboardingSession.created_at).desc())
|
||||
)
|
||||
).first()
|
||||
await BoardOnboardingSession.objects.filter_by(board_id=board.id)
|
||||
.order_by(col(BoardOnboardingSession.updated_at).desc())
|
||||
.first(session)
|
||||
)
|
||||
if onboarding is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
@@ -163,7 +163,7 @@ async def _board_gateway(
|
||||
) -> tuple[Gateway | None, GatewayClientConfig | None]:
|
||||
if not board.gateway_id:
|
||||
return None, None
|
||||
config = await session.get(Gateway, board.gateway_id)
|
||||
config = await Gateway.objects.by_id(board.gateway_id).first(session)
|
||||
if config is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
@@ -292,7 +292,7 @@ async def delete_board(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
board: Board = Depends(get_board_for_user_write),
|
||||
) -> OkResponse:
|
||||
agents = list(await session.exec(select(Agent).where(Agent.board_id == board.id)))
|
||||
agents = await Agent.objects.filter_by(board_id=board.id).all(session)
|
||||
task_ids = list(await session.exec(select(Task.id).where(Task.board_id == board.id)))
|
||||
|
||||
config, client_config = await _board_gateway(session, board)
|
||||
|
||||
@@ -59,7 +59,7 @@ async def require_org_member(
|
||||
member = await ensure_member_for_user(session, auth.user)
|
||||
if member is None:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
organization = await session.get(Organization, member.organization_id)
|
||||
organization = await Organization.objects.by_id(member.organization_id).first(session)
|
||||
if organization is None:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
return OrganizationContext(organization=organization, member=member)
|
||||
@@ -77,7 +77,7 @@ async def get_board_or_404(
|
||||
board_id: str,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> Board:
|
||||
board = await session.get(Board, board_id)
|
||||
board = await Board.objects.by_id(board_id).first(session)
|
||||
if board is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
return board
|
||||
@@ -88,7 +88,7 @@ async def get_board_for_actor_read(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
actor: ActorContext = Depends(require_admin_or_agent),
|
||||
) -> Board:
|
||||
board = await session.get(Board, board_id)
|
||||
board = await Board.objects.by_id(board_id).first(session)
|
||||
if board is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if actor.actor_type == "agent":
|
||||
@@ -106,7 +106,7 @@ async def get_board_for_actor_write(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
actor: ActorContext = Depends(require_admin_or_agent),
|
||||
) -> Board:
|
||||
board = await session.get(Board, board_id)
|
||||
board = await Board.objects.by_id(board_id).first(session)
|
||||
if board is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if actor.actor_type == "agent":
|
||||
@@ -124,7 +124,7 @@ async def get_board_for_user_read(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
auth: AuthContext = Depends(get_auth_context),
|
||||
) -> Board:
|
||||
board = await session.get(Board, board_id)
|
||||
board = await Board.objects.by_id(board_id).first(session)
|
||||
if board is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if auth.user is None:
|
||||
@@ -138,7 +138,7 @@ async def get_board_for_user_write(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
auth: AuthContext = Depends(get_auth_context),
|
||||
) -> Board:
|
||||
board = await session.get(Board, board_id)
|
||||
board = await Board.objects.by_id(board_id).first(session)
|
||||
if board is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if auth.user is None:
|
||||
@@ -152,7 +152,7 @@ async def get_task_or_404(
|
||||
board: Board = Depends(get_board_for_actor_read),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> Task:
|
||||
task = await session.get(Task, task_id)
|
||||
task = await Task.objects.by_id(task_id).first(session)
|
||||
if task is None or task.board_id != board.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
return task
|
||||
|
||||
@@ -56,7 +56,7 @@ async def _resolve_gateway(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="board_id or gateway_url is required",
|
||||
)
|
||||
board = await session.get(Board, board_id)
|
||||
board = await Board.objects.by_id(board_id).first(session)
|
||||
if board is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Board not found")
|
||||
if isinstance(user, object) and user is not None:
|
||||
@@ -66,7 +66,7 @@ async def _resolve_gateway(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="Board gateway_id is required",
|
||||
)
|
||||
gateway = await session.get(Gateway, board.gateway_id)
|
||||
gateway = await Gateway.objects.by_id(board.gateway_id).first(session)
|
||||
if gateway is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
@@ -216,7 +216,7 @@ async def get_gateway_session(
|
||||
sessions_list = list(sessions.get("sessions") or [])
|
||||
else:
|
||||
sessions_list = list(sessions or [])
|
||||
if main_session and not any(session.get("key") == main_session for session in sessions_list):
|
||||
if main_session and not any(item.get("key") == main_session for item in sessions_list):
|
||||
try:
|
||||
await ensure_session(main_session, config=config, label="Main Agent")
|
||||
refreshed = await openclaw_call("sessions.list", config=config)
|
||||
|
||||
@@ -2,12 +2,11 @@ from __future__ import annotations
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlmodel import col, select
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from sqlmodel import col
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from app.api.deps import require_org_admin
|
||||
from app.api.queryset import api_qs
|
||||
from app.core.agent_tokens import generate_agent_token, hash_agent_token
|
||||
from app.core.auth import AuthContext, get_auth_context
|
||||
from app.core.time import utcnow
|
||||
@@ -43,14 +42,14 @@ async def _require_gateway(
|
||||
gateway_id: UUID,
|
||||
organization_id: UUID,
|
||||
) -> Gateway:
|
||||
return await (
|
||||
api_qs(Gateway)
|
||||
.filter(
|
||||
col(Gateway.id) == gateway_id,
|
||||
col(Gateway.organization_id) == organization_id,
|
||||
)
|
||||
.first_or_404(session, detail="Gateway not found")
|
||||
gateway = (
|
||||
await Gateway.objects.by_id(gateway_id)
|
||||
.filter(col(Gateway.organization_id) == organization_id)
|
||||
.first(session)
|
||||
)
|
||||
if gateway is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Gateway not found")
|
||||
return gateway
|
||||
|
||||
|
||||
async def _find_main_agent(
|
||||
@@ -60,26 +59,22 @@ async def _find_main_agent(
|
||||
previous_session_key: str | None = None,
|
||||
) -> Agent | None:
|
||||
if gateway.main_session_key:
|
||||
agent = (
|
||||
await session.exec(
|
||||
select(Agent).where(Agent.openclaw_session_id == gateway.main_session_key)
|
||||
)
|
||||
).first()
|
||||
agent = await Agent.objects.filter_by(openclaw_session_id=gateway.main_session_key).first(
|
||||
session
|
||||
)
|
||||
if agent:
|
||||
return agent
|
||||
if previous_session_key:
|
||||
agent = (
|
||||
await session.exec(
|
||||
select(Agent).where(Agent.openclaw_session_id == previous_session_key)
|
||||
)
|
||||
).first()
|
||||
agent = await Agent.objects.filter_by(openclaw_session_id=previous_session_key).first(
|
||||
session
|
||||
)
|
||||
if agent:
|
||||
return agent
|
||||
names = {_main_agent_name(gateway)}
|
||||
if previous_name:
|
||||
names.add(f"{previous_name} Main")
|
||||
for name in names:
|
||||
agent = (await session.exec(select(Agent).where(Agent.name == name))).first()
|
||||
agent = await Agent.objects.filter_by(name=name).first(session)
|
||||
if agent:
|
||||
return agent
|
||||
return None
|
||||
@@ -153,8 +148,7 @@ async def list_gateways(
|
||||
ctx: OrganizationContext = Depends(require_org_admin),
|
||||
) -> DefaultLimitOffsetPage[GatewayRead]:
|
||||
statement = (
|
||||
api_qs(Gateway)
|
||||
.filter(col(Gateway.organization_id) == ctx.organization.id)
|
||||
Gateway.objects.filter_by(organization_id=ctx.organization.id)
|
||||
.order_by(col(Gateway.created_at).desc())
|
||||
.statement
|
||||
)
|
||||
|
||||
@@ -10,7 +10,6 @@ from sqlmodel import col, select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from app.api.deps import require_org_admin, require_org_member
|
||||
from app.api.queryset import api_qs
|
||||
from app.core.auth import AuthContext, get_auth_context
|
||||
from app.core.time import utcnow
|
||||
from app.db import crud
|
||||
@@ -81,14 +80,10 @@ async def _require_org_member(
|
||||
organization_id: UUID,
|
||||
member_id: UUID,
|
||||
) -> OrganizationMember:
|
||||
return await (
|
||||
api_qs(OrganizationMember)
|
||||
.filter(
|
||||
col(OrganizationMember.id) == member_id,
|
||||
col(OrganizationMember.organization_id) == organization_id,
|
||||
)
|
||||
.first_or_404(session)
|
||||
)
|
||||
member = await OrganizationMember.objects.by_id(member_id).first(session)
|
||||
if member is None or member.organization_id != organization_id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
return member
|
||||
|
||||
|
||||
async def _require_org_invite(
|
||||
@@ -97,14 +92,10 @@ async def _require_org_invite(
|
||||
organization_id: UUID,
|
||||
invite_id: UUID,
|
||||
) -> OrganizationInvite:
|
||||
return await (
|
||||
api_qs(OrganizationInvite)
|
||||
.filter(
|
||||
col(OrganizationInvite.id) == invite_id,
|
||||
col(OrganizationInvite.organization_id) == organization_id,
|
||||
)
|
||||
.first_or_404(session)
|
||||
)
|
||||
invite = await OrganizationInvite.objects.by_id(invite_id).first(session)
|
||||
if invite is None or invite.organization_id != organization_id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
return invite
|
||||
|
||||
|
||||
@router.post("", response_model=OrganizationRead)
|
||||
@@ -157,7 +148,7 @@ async def list_my_organizations(
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
await get_active_membership(session, auth.user)
|
||||
db_user = await session.get(User, auth.user.id)
|
||||
db_user = await User.objects.by_id(auth.user.id).first(session)
|
||||
active_id = db_user.active_organization_id if db_user else auth.user.active_organization_id
|
||||
|
||||
statement = (
|
||||
@@ -189,7 +180,7 @@ async def set_active_org(
|
||||
member = await set_active_organization(
|
||||
session, user=auth.user, organization_id=payload.organization_id
|
||||
)
|
||||
organization = await session.get(Organization, member.organization_id)
|
||||
organization = await Organization.objects.by_id(member.organization_id).first(session)
|
||||
if organization is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
return OrganizationRead.model_validate(organization, from_attributes=True)
|
||||
@@ -293,14 +284,10 @@ async def get_my_membership(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
ctx: OrganizationContext = Depends(require_org_member),
|
||||
) -> OrganizationMemberRead:
|
||||
user = await session.get(User, ctx.member.user_id)
|
||||
access_rows = list(
|
||||
await session.exec(
|
||||
select(OrganizationBoardAccess).where(
|
||||
col(OrganizationBoardAccess.organization_member_id) == ctx.member.id
|
||||
)
|
||||
)
|
||||
)
|
||||
user = await User.objects.by_id(ctx.member.user_id).first(session)
|
||||
access_rows = await OrganizationBoardAccess.objects.filter_by(
|
||||
organization_member_id=ctx.member.id
|
||||
).all(session)
|
||||
model = _member_to_read(ctx.member, user)
|
||||
model.board_access = [
|
||||
OrganizationBoardAccessRead.model_validate(row, from_attributes=True) for row in access_rows
|
||||
@@ -342,14 +329,10 @@ async def get_org_member(
|
||||
)
|
||||
if not is_org_admin(ctx.member) and member.user_id != ctx.member.user_id:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
user = await session.get(User, member.user_id)
|
||||
access_rows = list(
|
||||
await session.exec(
|
||||
select(OrganizationBoardAccess).where(
|
||||
col(OrganizationBoardAccess.organization_member_id) == member.id
|
||||
)
|
||||
)
|
||||
)
|
||||
user = await User.objects.by_id(member.user_id).first(session)
|
||||
access_rows = await OrganizationBoardAccess.objects.filter_by(
|
||||
organization_member_id=member.id
|
||||
).all(session)
|
||||
model = _member_to_read(member, user)
|
||||
model.board_access = [
|
||||
OrganizationBoardAccessRead.model_validate(row, from_attributes=True) for row in access_rows
|
||||
@@ -374,7 +357,7 @@ async def update_org_member(
|
||||
updates["role"] = normalize_role(updates["role"])
|
||||
updates["updated_at"] = utcnow()
|
||||
member = await crud.patch(session, member, updates)
|
||||
user = await session.get(User, member.user_id)
|
||||
user = await User.objects.by_id(member.user_id).first(session)
|
||||
return _member_to_read(member, user)
|
||||
|
||||
|
||||
@@ -393,20 +376,19 @@ async def update_member_access(
|
||||
|
||||
board_ids = {entry.board_id for entry in payload.board_access}
|
||||
if board_ids:
|
||||
valid_board_ids = set(
|
||||
await session.exec(
|
||||
select(Board.id)
|
||||
.where(col(Board.id).in_(board_ids))
|
||||
.where(col(Board.organization_id) == ctx.organization.id)
|
||||
)
|
||||
)
|
||||
valid_board_ids = {
|
||||
board.id
|
||||
for board in await Board.objects.filter_by(organization_id=ctx.organization.id)
|
||||
.filter(col(Board.id).in_(board_ids))
|
||||
.all(session)
|
||||
}
|
||||
if valid_board_ids != board_ids:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
|
||||
await apply_member_access_update(session, member=member, update=payload)
|
||||
await session.commit()
|
||||
await session.refresh(member)
|
||||
user = await session.get(User, member.user_id)
|
||||
user = await User.objects.by_id(member.user_id).first(session)
|
||||
return _member_to_read(member, user)
|
||||
|
||||
|
||||
@@ -416,9 +398,11 @@ async def remove_org_member(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
ctx: OrganizationContext = Depends(require_org_admin),
|
||||
) -> OkResponse:
|
||||
member = await session.get(OrganizationMember, member_id)
|
||||
if member is None or member.organization_id != ctx.organization.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
member = await _require_org_member(
|
||||
session,
|
||||
organization_id=ctx.organization.id,
|
||||
member_id=member_id,
|
||||
)
|
||||
if member.user_id == ctx.member.user_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
@@ -430,15 +414,12 @@ async def remove_org_member(
|
||||
detail="Only owners can remove owners",
|
||||
)
|
||||
if member.role == "owner":
|
||||
owner_ids = list(
|
||||
await session.exec(
|
||||
select(OrganizationMember.id).where(
|
||||
col(OrganizationMember.organization_id) == ctx.organization.id,
|
||||
col(OrganizationMember.role) == "owner",
|
||||
)
|
||||
)
|
||||
owners = (
|
||||
await OrganizationMember.objects.filter_by(organization_id=ctx.organization.id)
|
||||
.filter(col(OrganizationMember.role) == "owner")
|
||||
.all(session)
|
||||
)
|
||||
if len(owner_ids) <= 1:
|
||||
if len(owners) <= 1:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
|
||||
detail="Organization must have at least one owner",
|
||||
@@ -451,17 +432,22 @@ async def remove_org_member(
|
||||
),
|
||||
)
|
||||
|
||||
user = await session.get(User, member.user_id)
|
||||
user = await User.objects.by_id(member.user_id).first(session)
|
||||
if user is not None and user.active_organization_id == ctx.organization.id:
|
||||
fallback_org_id = (
|
||||
await session.exec(
|
||||
select(OrganizationMember.organization_id)
|
||||
.where(col(OrganizationMember.user_id) == user.id)
|
||||
.where(col(OrganizationMember.organization_id) != ctx.organization.id)
|
||||
.order_by(col(OrganizationMember.created_at).asc())
|
||||
fallback_membership = (
|
||||
await OrganizationMember.objects.filter(
|
||||
col(OrganizationMember.user_id) == user.id,
|
||||
col(OrganizationMember.organization_id) != ctx.organization.id,
|
||||
)
|
||||
.order_by(col(OrganizationMember.created_at).asc())
|
||||
.first(session)
|
||||
)
|
||||
if isinstance(fallback_membership, UUID):
|
||||
user.active_organization_id = fallback_membership
|
||||
else:
|
||||
user.active_organization_id = (
|
||||
fallback_membership.organization_id if fallback_membership is not None else None
|
||||
)
|
||||
).first()
|
||||
user.active_organization_id = fallback_org_id
|
||||
session.add(user)
|
||||
|
||||
await crud.delete(session, member)
|
||||
@@ -474,8 +460,7 @@ async def list_org_invites(
|
||||
ctx: OrganizationContext = Depends(require_org_admin),
|
||||
) -> DefaultLimitOffsetPage[OrganizationInviteRead]:
|
||||
statement = (
|
||||
api_qs(OrganizationInvite)
|
||||
.filter(col(OrganizationInvite.organization_id) == ctx.organization.id)
|
||||
OrganizationInvite.objects.filter_by(organization_id=ctx.organization.id)
|
||||
.filter(col(OrganizationInvite.accepted_at).is_(None))
|
||||
.order_by(col(OrganizationInvite.created_at).desc())
|
||||
.statement
|
||||
@@ -522,13 +507,12 @@ async def create_org_invite(
|
||||
|
||||
board_ids = {entry.board_id for entry in payload.board_access}
|
||||
if board_ids:
|
||||
valid_board_ids = set(
|
||||
await session.exec(
|
||||
select(Board.id)
|
||||
.where(col(Board.id).in_(board_ids))
|
||||
.where(col(Board.organization_id) == ctx.organization.id)
|
||||
)
|
||||
)
|
||||
valid_board_ids = {
|
||||
board.id
|
||||
for board in await Board.objects.filter_by(organization_id=ctx.organization.id)
|
||||
.filter(col(Board.id).in_(board_ids))
|
||||
.all(session)
|
||||
}
|
||||
if valid_board_ids != board_ids:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
await apply_invite_board_access(session, invite=invite, entries=payload.board_access)
|
||||
@@ -566,13 +550,10 @@ async def accept_org_invite(
|
||||
) -> OrganizationMemberRead:
|
||||
if auth.user is None:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
||||
invite = (
|
||||
await session.exec(
|
||||
select(OrganizationInvite)
|
||||
.where(col(OrganizationInvite.token) == payload.token)
|
||||
.where(col(OrganizationInvite.accepted_at).is_(None))
|
||||
)
|
||||
).first()
|
||||
invite = await OrganizationInvite.objects.filter(
|
||||
col(OrganizationInvite.token) == payload.token,
|
||||
col(OrganizationInvite.accepted_at).is_(None),
|
||||
).first(session)
|
||||
if invite is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if invite.invited_email and auth.user.email:
|
||||
@@ -597,5 +578,5 @@ async def accept_org_invite(
|
||||
await session.commit()
|
||||
member = existing
|
||||
|
||||
user = await session.get(User, member.user_id)
|
||||
user = await User.objects.by_id(member.user_id).first(session)
|
||||
return _member_to_read(member, user)
|
||||
|
||||
@@ -243,7 +243,7 @@ def _serialize_comment(event: ActivityEvent) -> dict[str, object]:
|
||||
async def _gateway_config(session: AsyncSession, board: Board) -> GatewayClientConfig | None:
|
||||
if not board.gateway_id:
|
||||
return None
|
||||
gateway = await session.get(Gateway, board.gateway_id)
|
||||
gateway = await Gateway.objects.by_id(board.gateway_id).first(session)
|
||||
if gateway is None or not gateway.url:
|
||||
return None
|
||||
return GatewayClientConfig(url=gateway.url, token=gateway.token)
|
||||
@@ -331,12 +331,10 @@ async def _notify_lead_on_task_create(
|
||||
task: Task,
|
||||
) -> None:
|
||||
lead = (
|
||||
await session.exec(
|
||||
select(Agent)
|
||||
.where(Agent.board_id == board.id)
|
||||
.where(col(Agent.is_board_lead).is_(True))
|
||||
)
|
||||
).first()
|
||||
await Agent.objects.filter_by(board_id=board.id)
|
||||
.filter(col(Agent.is_board_lead).is_(True))
|
||||
.first(session)
|
||||
)
|
||||
if lead is None or not lead.openclaw_session_id:
|
||||
return
|
||||
config = await _gateway_config(session, board)
|
||||
@@ -390,12 +388,10 @@ async def _notify_lead_on_task_unassigned(
|
||||
task: Task,
|
||||
) -> None:
|
||||
lead = (
|
||||
await session.exec(
|
||||
select(Agent)
|
||||
.where(Agent.board_id == board.id)
|
||||
.where(col(Agent.is_board_lead).is_(True))
|
||||
)
|
||||
).first()
|
||||
await Agent.objects.filter_by(board_id=board.id)
|
||||
.filter(col(Agent.is_board_lead).is_(True))
|
||||
.first(session)
|
||||
)
|
||||
if lead is None or not lead.openclaw_session_id:
|
||||
return
|
||||
config = await _gateway_config(session, board)
|
||||
@@ -635,7 +631,7 @@ async def create_task(
|
||||
await session.commit()
|
||||
await _notify_lead_on_task_create(session=session, board=board, task=task)
|
||||
if task.assigned_agent_id:
|
||||
assigned_agent = await session.get(Agent, task.assigned_agent_id)
|
||||
assigned_agent = await Agent.objects.by_id(task.assigned_agent_id).first(session)
|
||||
if assigned_agent:
|
||||
await _notify_agent_on_task_assign(
|
||||
session=session,
|
||||
@@ -670,7 +666,7 @@ async def update_task(
|
||||
)
|
||||
board_id = task.board_id
|
||||
if actor.actor_type == "user" and actor.user is not None:
|
||||
board = await session.get(Board, board_id)
|
||||
board = await Board.objects.by_id(board_id).first(session)
|
||||
if board is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
await require_board_access(session, user=actor.user, board=board, write=True)
|
||||
@@ -740,7 +736,7 @@ async def update_task(
|
||||
if "assigned_agent_id" in updates:
|
||||
assigned_id = updates["assigned_agent_id"]
|
||||
if assigned_id:
|
||||
agent = await session.get(Agent, assigned_id)
|
||||
agent = await Agent.objects.by_id(assigned_id).first(session)
|
||||
if agent is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if agent.is_board_lead:
|
||||
@@ -796,9 +792,13 @@ async def update_task(
|
||||
await session.refresh(task)
|
||||
|
||||
if task.assigned_agent_id and task.assigned_agent_id != previous_assigned:
|
||||
assigned_agent = await session.get(Agent, task.assigned_agent_id)
|
||||
assigned_agent = await Agent.objects.by_id(task.assigned_agent_id).first(session)
|
||||
if assigned_agent:
|
||||
board = await session.get(Board, task.board_id) if task.board_id else None
|
||||
board = (
|
||||
await Board.objects.by_id(task.board_id).first(session)
|
||||
if task.board_id
|
||||
else None
|
||||
)
|
||||
if board:
|
||||
await _notify_agent_on_task_assign(
|
||||
session=session,
|
||||
@@ -879,7 +879,7 @@ async def update_task(
|
||||
task.in_progress_at = utcnow()
|
||||
|
||||
if "assigned_agent_id" in updates and updates["assigned_agent_id"]:
|
||||
agent = await session.get(Agent, updates["assigned_agent_id"])
|
||||
agent = await Agent.objects.by_id(updates["assigned_agent_id"]).first(session)
|
||||
if agent is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if agent.board_id and task.board_id and agent.board_id != task.board_id:
|
||||
@@ -941,7 +941,9 @@ async def update_task(
|
||||
|
||||
if task.status == "inbox" and task.assigned_agent_id is None:
|
||||
if previous_status != "inbox" or previous_assigned is not None:
|
||||
board = await session.get(Board, task.board_id) if task.board_id else None
|
||||
board = (
|
||||
await Board.objects.by_id(task.board_id).first(session) if task.board_id else None
|
||||
)
|
||||
if board:
|
||||
await _notify_lead_on_task_unassigned(
|
||||
session=session,
|
||||
@@ -953,9 +955,13 @@ async def update_task(
|
||||
# Don't notify the actor about their own assignment.
|
||||
pass
|
||||
else:
|
||||
assigned_agent = await session.get(Agent, task.assigned_agent_id)
|
||||
assigned_agent = await Agent.objects.by_id(task.assigned_agent_id).first(session)
|
||||
if assigned_agent:
|
||||
board = await session.get(Board, task.board_id) if task.board_id else None
|
||||
board = (
|
||||
await Board.objects.by_id(task.board_id).first(session)
|
||||
if task.board_id
|
||||
else None
|
||||
)
|
||||
if board:
|
||||
await _notify_agent_on_task_assign(
|
||||
session=session,
|
||||
@@ -985,7 +991,7 @@ async def delete_task(
|
||||
) -> OkResponse:
|
||||
if task.board_id is None:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
board = await session.get(Board, task.board_id)
|
||||
board = await Board.objects.by_id(task.board_id).first(session)
|
||||
if board is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if auth.user is None:
|
||||
@@ -1032,7 +1038,7 @@ async def create_task_comment(
|
||||
if task.board_id is None:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
if actor.actor_type == "user" and actor.user is not None:
|
||||
board = await session.get(Board, task.board_id)
|
||||
board = await Board.objects.by_id(task.board_id).first(session)
|
||||
if board is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
await require_board_access(session, user=actor.user, board=board, write=True)
|
||||
@@ -1059,18 +1065,17 @@ async def create_task_comment(
|
||||
mention_names = extract_mentions(payload.message)
|
||||
targets: dict[UUID, Agent] = {}
|
||||
if mention_names and task.board_id:
|
||||
statement = select(Agent).where(col(Agent.board_id) == task.board_id)
|
||||
for agent in await session.exec(statement):
|
||||
for agent in await Agent.objects.filter_by(board_id=task.board_id).all(session):
|
||||
if matches_agent_mention(agent, mention_names):
|
||||
targets[agent.id] = agent
|
||||
if not mention_names and task.assigned_agent_id:
|
||||
assigned_agent = await session.get(Agent, task.assigned_agent_id)
|
||||
assigned_agent = await Agent.objects.by_id(task.assigned_agent_id).first(session)
|
||||
if assigned_agent:
|
||||
targets[assigned_agent.id] = assigned_agent
|
||||
if actor.actor_type == "agent" and actor.agent:
|
||||
targets.pop(actor.agent.id, None)
|
||||
if targets:
|
||||
board = await session.get(Board, task.board_id) if task.board_id else None
|
||||
board = await Board.objects.by_id(task.board_id).first(session) if task.board_id else None
|
||||
config = await _gateway_config(session, board) if board else None
|
||||
if board and config:
|
||||
snippet = payload.message.strip()
|
||||
|
||||
Reference in New Issue
Block a user