2026-02-05 14:43:25 +05:30
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-02-06 00:44:03 +05:30
|
|
|
import asyncio
|
|
|
|
|
import json
|
|
|
|
|
import re
|
2026-02-06 16:12:04 +05:30
|
|
|
from collections.abc import AsyncIterator
|
2026-02-06 02:43:08 +05:30
|
|
|
from datetime import datetime, timezone
|
2026-02-06 16:12:04 +05:30
|
|
|
from uuid import UUID
|
2026-02-06 00:44:03 +05:30
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, Request, status
|
|
|
|
|
from sqlmodel import col, select
|
|
|
|
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
2026-02-06 00:44:03 +05:30
|
|
|
from sse_starlette.sse import EventSourceResponse
|
2026-02-05 14:43:25 +05:30
|
|
|
|
|
|
|
|
from app.api.deps import ActorContext, get_board_or_404, require_admin_or_agent
|
2026-02-06 00:44:03 +05:30
|
|
|
from app.core.config import settings
|
2026-02-06 16:12:04 +05:30
|
|
|
from app.core.time import utcnow
|
|
|
|
|
from app.db.session import async_session_maker, get_session
|
2026-02-06 02:43:08 +05:30
|
|
|
from app.integrations.openclaw_gateway import GatewayConfig as GatewayClientConfig
|
|
|
|
|
from app.integrations.openclaw_gateway import OpenClawGatewayError, ensure_session, send_message
|
2026-02-06 00:44:03 +05:30
|
|
|
from app.models.agents import Agent
|
2026-02-05 14:43:25 +05:30
|
|
|
from app.models.board_memory import BoardMemory
|
2026-02-06 16:12:04 +05:30
|
|
|
from app.models.boards import Board
|
2026-02-06 00:44:03 +05:30
|
|
|
from app.models.gateways import Gateway
|
2026-02-05 14:43:25 +05:30
|
|
|
from app.schemas.board_memory import BoardMemoryCreate, BoardMemoryRead
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/boards/{board_id}/memory", tags=["board-memory"])
|
|
|
|
|
|
2026-02-06 00:44:03 +05:30
|
|
|
MENTION_PATTERN = re.compile(r"@([A-Za-z][\w-]{0,31})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_since(value: str | None) -> datetime | None:
|
|
|
|
|
if not value:
|
|
|
|
|
return None
|
|
|
|
|
normalized = value.strip()
|
|
|
|
|
if not normalized:
|
|
|
|
|
return None
|
|
|
|
|
normalized = normalized.replace("Z", "+00:00")
|
|
|
|
|
try:
|
|
|
|
|
parsed = datetime.fromisoformat(normalized)
|
|
|
|
|
except ValueError:
|
|
|
|
|
return None
|
|
|
|
|
if parsed.tzinfo is not None:
|
|
|
|
|
return parsed.astimezone(timezone.utc).replace(tzinfo=None)
|
|
|
|
|
return parsed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _serialize_memory(memory: BoardMemory) -> dict[str, object]:
|
2026-02-06 02:43:08 +05:30
|
|
|
return BoardMemoryRead.model_validate(memory, from_attributes=True).model_dump(mode="json")
|
2026-02-06 00:44:03 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_mentions(message: str) -> set[str]:
|
|
|
|
|
return {match.group(1).lower() for match in MENTION_PATTERN.finditer(message)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _matches_mention(agent: Agent, mentions: set[str]) -> bool:
|
|
|
|
|
if not mentions:
|
|
|
|
|
return False
|
|
|
|
|
name = (agent.name or "").strip()
|
|
|
|
|
if not name:
|
|
|
|
|
return False
|
|
|
|
|
normalized = name.lower()
|
|
|
|
|
if normalized in mentions:
|
|
|
|
|
return True
|
|
|
|
|
first = normalized.split()[0]
|
|
|
|
|
return first in mentions
|
|
|
|
|
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
async def _gateway_config(session: AsyncSession, board: Board) -> GatewayClientConfig | None:
|
|
|
|
|
if board.gateway_id is None:
|
2026-02-06 00:44:03 +05:30
|
|
|
return None
|
2026-02-06 16:12:04 +05:30
|
|
|
gateway = await session.get(Gateway, board.gateway_id)
|
2026-02-06 00:44:03 +05:30
|
|
|
if gateway is None or not gateway.url:
|
|
|
|
|
return None
|
|
|
|
|
return GatewayClientConfig(url=gateway.url, token=gateway.token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _send_agent_message(
|
|
|
|
|
*,
|
|
|
|
|
session_key: str,
|
|
|
|
|
config: GatewayClientConfig,
|
|
|
|
|
agent_name: str,
|
|
|
|
|
message: str,
|
|
|
|
|
) -> None:
|
|
|
|
|
await ensure_session(session_key, config=config, label=agent_name)
|
|
|
|
|
await send_message(message, session_key=session_key, config=config, deliver=False)
|
|
|
|
|
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
async def _fetch_memory_events(
|
|
|
|
|
session: AsyncSession,
|
|
|
|
|
board_id: UUID,
|
2026-02-06 00:44:03 +05:30
|
|
|
since: datetime,
|
|
|
|
|
) -> list[BoardMemory]:
|
2026-02-06 16:12:04 +05:30
|
|
|
statement = (
|
|
|
|
|
select(BoardMemory)
|
|
|
|
|
.where(col(BoardMemory.board_id) == board_id)
|
|
|
|
|
.where(col(BoardMemory.created_at) >= since)
|
|
|
|
|
.order_by(col(BoardMemory.created_at))
|
|
|
|
|
)
|
|
|
|
|
return list(await session.exec(statement))
|
2026-02-06 00:44:03 +05:30
|
|
|
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
async def _notify_chat_targets(
|
2026-02-06 00:44:03 +05:30
|
|
|
*,
|
2026-02-06 16:12:04 +05:30
|
|
|
session: AsyncSession,
|
|
|
|
|
board: Board,
|
2026-02-06 00:44:03 +05:30
|
|
|
memory: BoardMemory,
|
|
|
|
|
actor: ActorContext,
|
|
|
|
|
) -> None:
|
|
|
|
|
if not memory.content:
|
|
|
|
|
return
|
2026-02-06 16:12:04 +05:30
|
|
|
config = await _gateway_config(session, board)
|
2026-02-06 00:44:03 +05:30
|
|
|
if config is None:
|
|
|
|
|
return
|
|
|
|
|
mentions = _extract_mentions(memory.content)
|
|
|
|
|
statement = select(Agent).where(col(Agent.board_id) == board.id)
|
|
|
|
|
targets: dict[str, Agent] = {}
|
2026-02-06 16:12:04 +05:30
|
|
|
for agent in await session.exec(statement):
|
2026-02-06 00:44:03 +05:30
|
|
|
if agent.is_board_lead:
|
|
|
|
|
targets[str(agent.id)] = agent
|
|
|
|
|
continue
|
|
|
|
|
if mentions and _matches_mention(agent, mentions):
|
|
|
|
|
targets[str(agent.id)] = agent
|
|
|
|
|
if actor.actor_type == "agent" and actor.agent:
|
|
|
|
|
targets.pop(str(actor.agent.id), None)
|
|
|
|
|
if not targets:
|
|
|
|
|
return
|
|
|
|
|
actor_name = "User"
|
|
|
|
|
if actor.actor_type == "agent" and actor.agent:
|
|
|
|
|
actor_name = actor.agent.name
|
|
|
|
|
elif actor.user:
|
|
|
|
|
actor_name = actor.user.preferred_name or actor.user.name or actor_name
|
|
|
|
|
snippet = memory.content.strip()
|
|
|
|
|
if len(snippet) > 800:
|
|
|
|
|
snippet = f"{snippet[:797]}..."
|
|
|
|
|
base_url = settings.base_url or "http://localhost:8000"
|
|
|
|
|
for agent in targets.values():
|
|
|
|
|
if not agent.openclaw_session_id:
|
|
|
|
|
continue
|
|
|
|
|
mentioned = _matches_mention(agent, mentions)
|
|
|
|
|
header = "BOARD CHAT MENTION" if mentioned else "BOARD CHAT"
|
|
|
|
|
message = (
|
|
|
|
|
f"{header}\n"
|
|
|
|
|
f"Board: {board.name}\n"
|
|
|
|
|
f"From: {actor_name}\n\n"
|
|
|
|
|
f"{snippet}\n\n"
|
|
|
|
|
"Reply via board chat:\n"
|
|
|
|
|
f"POST {base_url}/api/v1/agent/boards/{board.id}/memory\n"
|
|
|
|
|
'Body: {"content":"...","tags":["chat"]}'
|
|
|
|
|
)
|
|
|
|
|
try:
|
2026-02-06 16:12:04 +05:30
|
|
|
await _send_agent_message(
|
|
|
|
|
session_key=agent.openclaw_session_id,
|
|
|
|
|
config=config,
|
|
|
|
|
agent_name=agent.name,
|
|
|
|
|
message=message,
|
2026-02-06 00:44:03 +05:30
|
|
|
)
|
|
|
|
|
except OpenClawGatewayError:
|
|
|
|
|
continue
|
2026-02-05 14:43:25 +05:30
|
|
|
|
2026-02-06 02:43:08 +05:30
|
|
|
|
2026-02-05 14:43:25 +05:30
|
|
|
@router.get("", response_model=list[BoardMemoryRead])
|
2026-02-06 16:12:04 +05:30
|
|
|
async def list_board_memory(
|
2026-02-05 14:43:25 +05:30
|
|
|
limit: int = Query(default=50, ge=1, le=200),
|
|
|
|
|
offset: int = Query(default=0, ge=0),
|
2026-02-06 16:12:04 +05:30
|
|
|
board: Board = Depends(get_board_or_404),
|
|
|
|
|
session: AsyncSession = Depends(get_session),
|
2026-02-05 14:43:25 +05:30
|
|
|
actor: ActorContext = Depends(require_admin_or_agent),
|
|
|
|
|
) -> list[BoardMemory]:
|
|
|
|
|
if actor.actor_type == "agent" and actor.agent:
|
|
|
|
|
if actor.agent.board_id and actor.agent.board_id != board.id:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
|
|
|
|
statement = (
|
|
|
|
|
select(BoardMemory)
|
|
|
|
|
.where(col(BoardMemory.board_id) == board.id)
|
|
|
|
|
.order_by(col(BoardMemory.created_at).desc())
|
|
|
|
|
.offset(offset)
|
|
|
|
|
.limit(limit)
|
|
|
|
|
)
|
2026-02-06 16:12:04 +05:30
|
|
|
return list(await session.exec(statement))
|
2026-02-05 14:43:25 +05:30
|
|
|
|
|
|
|
|
|
2026-02-06 00:44:03 +05:30
|
|
|
@router.get("/stream")
|
|
|
|
|
async def stream_board_memory(
|
|
|
|
|
request: Request,
|
2026-02-06 16:12:04 +05:30
|
|
|
board: Board = Depends(get_board_or_404),
|
2026-02-06 00:44:03 +05:30
|
|
|
actor: ActorContext = Depends(require_admin_or_agent),
|
|
|
|
|
since: str | None = Query(default=None),
|
|
|
|
|
) -> EventSourceResponse:
|
|
|
|
|
if actor.actor_type == "agent" and actor.agent:
|
|
|
|
|
if actor.agent.board_id and actor.agent.board_id != board.id:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
2026-02-06 16:12:04 +05:30
|
|
|
since_dt = _parse_since(since) or utcnow()
|
2026-02-06 00:44:03 +05:30
|
|
|
last_seen = since_dt
|
|
|
|
|
|
2026-02-06 16:12:04 +05:30
|
|
|
async def event_generator() -> AsyncIterator[dict[str, str]]:
|
2026-02-06 00:44:03 +05:30
|
|
|
nonlocal last_seen
|
|
|
|
|
while True:
|
|
|
|
|
if await request.is_disconnected():
|
|
|
|
|
break
|
2026-02-06 16:12:04 +05:30
|
|
|
async with async_session_maker() as session:
|
|
|
|
|
memories = await _fetch_memory_events(session, board.id, last_seen)
|
2026-02-06 00:44:03 +05:30
|
|
|
for memory in memories:
|
|
|
|
|
if memory.created_at > last_seen:
|
|
|
|
|
last_seen = memory.created_at
|
|
|
|
|
payload = {"memory": _serialize_memory(memory)}
|
|
|
|
|
yield {"event": "memory", "data": json.dumps(payload)}
|
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
|
|
|
|
|
|
return EventSourceResponse(event_generator(), ping=15)
|
|
|
|
|
|
|
|
|
|
|
2026-02-05 14:43:25 +05:30
|
|
|
@router.post("", response_model=BoardMemoryRead)
|
2026-02-06 16:12:04 +05:30
|
|
|
async def create_board_memory(
|
2026-02-05 14:43:25 +05:30
|
|
|
payload: BoardMemoryCreate,
|
2026-02-06 16:12:04 +05:30
|
|
|
board: Board = Depends(get_board_or_404),
|
|
|
|
|
session: AsyncSession = Depends(get_session),
|
2026-02-05 14:43:25 +05:30
|
|
|
actor: ActorContext = Depends(require_admin_or_agent),
|
|
|
|
|
) -> BoardMemory:
|
|
|
|
|
if actor.actor_type == "agent" and actor.agent:
|
|
|
|
|
if actor.agent.board_id and actor.agent.board_id != board.id:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
2026-02-06 00:44:03 +05:30
|
|
|
is_chat = payload.tags is not None and "chat" in payload.tags
|
|
|
|
|
source = payload.source
|
|
|
|
|
if is_chat and not source:
|
|
|
|
|
if actor.actor_type == "agent" and actor.agent:
|
|
|
|
|
source = actor.agent.name
|
|
|
|
|
elif actor.user:
|
|
|
|
|
source = actor.user.preferred_name or actor.user.name or "User"
|
2026-02-05 14:43:25 +05:30
|
|
|
memory = BoardMemory(
|
|
|
|
|
board_id=board.id,
|
|
|
|
|
content=payload.content,
|
|
|
|
|
tags=payload.tags,
|
2026-02-06 00:44:03 +05:30
|
|
|
source=source,
|
2026-02-05 14:43:25 +05:30
|
|
|
)
|
|
|
|
|
session.add(memory)
|
2026-02-06 16:12:04 +05:30
|
|
|
await session.commit()
|
|
|
|
|
await session.refresh(memory)
|
2026-02-06 00:44:03 +05:30
|
|
|
if is_chat:
|
2026-02-06 16:12:04 +05:30
|
|
|
await _notify_chat_targets(session=session, board=board, memory=memory, actor=actor)
|
2026-02-05 14:43:25 +05:30
|
|
|
return memory
|