2026-02-02 16:05:18 +05:30
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-02-02 20:51:08 +05:30
|
|
|
import logging
|
2026-02-02 16:05:18 +05:30
|
|
|
from dataclasses import dataclass
|
|
|
|
|
from typing import Iterable
|
|
|
|
|
|
|
|
|
|
from sqlmodel import Session, select
|
|
|
|
|
|
2026-02-02 21:53:47 +05:30
|
|
|
from app.db.session import engine
|
2026-02-02 16:05:18 +05:30
|
|
|
from app.integrations.openclaw import OpenClawClient
|
|
|
|
|
from app.models.org import Employee
|
|
|
|
|
from app.models.projects import ProjectMember
|
|
|
|
|
from app.models.work import Task, TaskComment
|
|
|
|
|
|
2026-02-02 20:51:08 +05:30
|
|
|
logger = logging.getLogger("app.notify")
|
|
|
|
|
|
2026-02-02 16:05:18 +05:30
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
|
class NotifyContext:
|
2026-02-02 21:53:47 +05:30
|
|
|
"""Notification context.
|
|
|
|
|
|
|
|
|
|
IMPORTANT: this is passed into FastAPI BackgroundTasks.
|
|
|
|
|
Do not store live SQLAlchemy/SQLModel objects here; only ids/primitive data.
|
|
|
|
|
"""
|
|
|
|
|
|
2026-02-02 16:05:18 +05:30
|
|
|
event: str # task.created | task.updated | task.assigned | comment.created | status.changed
|
|
|
|
|
actor_employee_id: int
|
2026-02-02 21:53:47 +05:30
|
|
|
task_id: int
|
|
|
|
|
comment_id: int | None = None
|
2026-02-02 16:05:18 +05:30
|
|
|
changed_fields: dict | None = None
|
|
|
|
|
|
|
|
|
|
|
2026-02-02 20:32:38 +05:30
|
|
|
def _employees_with_session_keys(session: Session, employee_ids: Iterable[int]) -> list[Employee]:
|
2026-02-02 16:05:18 +05:30
|
|
|
ids = sorted({i for i in employee_ids if i is not None})
|
|
|
|
|
if not ids:
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
emps = session.exec(select(Employee).where(Employee.id.in_(ids))).all()
|
2026-02-02 20:32:38 +05:30
|
|
|
out: list[Employee] = []
|
2026-02-02 16:05:18 +05:30
|
|
|
for e in emps:
|
|
|
|
|
if not getattr(e, "notify_enabled", True):
|
|
|
|
|
continue
|
2026-02-02 20:32:38 +05:30
|
|
|
if getattr(e, "openclaw_session_key", None):
|
|
|
|
|
out.append(e)
|
|
|
|
|
return out
|
2026-02-02 16:05:18 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
def _project_pm_employee_ids(session: Session, project_id: int) -> set[int]:
|
|
|
|
|
pms = session.exec(select(ProjectMember).where(ProjectMember.project_id == project_id)).all()
|
|
|
|
|
pm_ids: set[int] = set()
|
|
|
|
|
for m in pms:
|
|
|
|
|
role = (m.role or "").lower()
|
|
|
|
|
if role in {"pm", "product", "product_manager", "manager"}:
|
|
|
|
|
pm_ids.add(m.employee_id)
|
|
|
|
|
return pm_ids
|
|
|
|
|
|
|
|
|
|
|
2026-02-02 21:53:47 +05:30
|
|
|
def resolve_recipients(
|
|
|
|
|
session: Session, ctx: NotifyContext, task: Task, comment: TaskComment | None
|
|
|
|
|
) -> set[int]:
|
2026-02-02 16:05:18 +05:30
|
|
|
recipients: set[int] = set()
|
|
|
|
|
|
|
|
|
|
if ctx.event == "task.created":
|
2026-02-02 21:53:47 +05:30
|
|
|
if task.assignee_employee_id:
|
|
|
|
|
recipients.add(task.assignee_employee_id)
|
|
|
|
|
recipients |= _project_pm_employee_ids(session, task.project_id)
|
2026-02-02 16:05:18 +05:30
|
|
|
|
|
|
|
|
elif ctx.event == "task.assigned":
|
2026-02-02 21:53:47 +05:30
|
|
|
if task.assignee_employee_id:
|
|
|
|
|
recipients.add(task.assignee_employee_id)
|
|
|
|
|
recipients |= _project_pm_employee_ids(session, task.project_id)
|
2026-02-02 16:05:18 +05:30
|
|
|
|
|
|
|
|
elif ctx.event == "comment.created":
|
2026-02-02 21:53:47 +05:30
|
|
|
if task.assignee_employee_id:
|
|
|
|
|
recipients.add(task.assignee_employee_id)
|
|
|
|
|
if task.reviewer_employee_id:
|
|
|
|
|
recipients.add(task.reviewer_employee_id)
|
|
|
|
|
recipients |= _project_pm_employee_ids(session, task.project_id)
|
|
|
|
|
if comment and comment.author_employee_id:
|
|
|
|
|
recipients.discard(comment.author_employee_id)
|
2026-02-02 16:05:18 +05:30
|
|
|
|
|
|
|
|
elif ctx.event == "status.changed":
|
2026-02-02 21:53:47 +05:30
|
|
|
new_status = (getattr(task, "status", None) or "").lower()
|
|
|
|
|
if new_status in {"review", "ready_for_review"} and task.reviewer_employee_id:
|
|
|
|
|
recipients.add(task.reviewer_employee_id)
|
|
|
|
|
recipients |= _project_pm_employee_ids(session, task.project_id)
|
2026-02-02 16:05:18 +05:30
|
|
|
|
|
|
|
|
elif ctx.event == "task.updated":
|
2026-02-02 21:53:47 +05:30
|
|
|
recipients |= _project_pm_employee_ids(session, task.project_id)
|
2026-02-02 16:05:18 +05:30
|
|
|
|
|
|
|
|
recipients.discard(ctx.actor_employee_id)
|
|
|
|
|
return recipients
|
|
|
|
|
|
|
|
|
|
|
2026-02-02 21:53:47 +05:30
|
|
|
def ensure_employee_provisioned(session: Session, employee_id: int) -> None:
|
|
|
|
|
"""Best-effort provisioning of a reviewer/manager so notifications can be delivered."""
|
|
|
|
|
|
|
|
|
|
emp = session.get(Employee, employee_id)
|
|
|
|
|
if emp is None:
|
|
|
|
|
return
|
|
|
|
|
if not getattr(emp, "notify_enabled", True):
|
|
|
|
|
return
|
|
|
|
|
if getattr(emp, "openclaw_session_key", None):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
client = OpenClawClient.from_env()
|
|
|
|
|
if client is None:
|
|
|
|
|
logger.warning(
|
|
|
|
|
"ensure_employee_provisioned: missing OpenClaw env", extra={"employee_id": employee_id}
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
prompt = (
|
|
|
|
|
f"You are {emp.name} (employee_id={emp.id}).\n"
|
2026-02-02 17:37:07 +00:00
|
|
|
"You are a reviewer/manager in Mission Control.\n\n"
|
|
|
|
|
"Your job is to REVIEW work within the bounds of what the task requester asked for, using only the task + comments + current system state.\n"
|
|
|
|
|
"Do NOT wait for the requester to provide more info by default.\n\n"
|
|
|
|
|
"When a task is in review you must:\n"
|
|
|
|
|
"1) Read the task title/description and all comments\n"
|
|
|
|
|
"2) Verify the requested changes were actually made (check via Mission Control API if needed)\n"
|
|
|
|
|
"3) Decide: approve or request changes\n"
|
|
|
|
|
"4) Leave an audit comment explaining your decision (required)\n\n"
|
|
|
|
|
"If something is ambiguous or missing, request changes with a clear checklist. Only ask the human if it's truly impossible to decide.\n"
|
2026-02-02 21:53:47 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
res = client.tools_invoke(
|
|
|
|
|
"sessions_spawn",
|
|
|
|
|
{"task": prompt, "label": f"employee:{emp.id}:{emp.name}"},
|
|
|
|
|
timeout_s=20.0,
|
|
|
|
|
)
|
|
|
|
|
details = (res.get("result") or {}).get("details") or {}
|
|
|
|
|
sk = details.get("childSessionKey") or details.get("sessionKey")
|
|
|
|
|
if sk:
|
|
|
|
|
emp.openclaw_session_key = sk
|
|
|
|
|
session.add(emp)
|
|
|
|
|
session.commit()
|
|
|
|
|
logger.info(
|
|
|
|
|
"ensure_employee_provisioned: provisioned",
|
|
|
|
|
extra={"employee_id": emp.id, "session_key": sk},
|
|
|
|
|
)
|
|
|
|
|
except Exception:
|
|
|
|
|
session.rollback()
|
|
|
|
|
logger.exception("ensure_employee_provisioned: failed", extra={"employee_id": employee_id})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_message(
|
|
|
|
|
*,
|
|
|
|
|
ctx: NotifyContext,
|
|
|
|
|
task: Task,
|
|
|
|
|
comment: TaskComment | None,
|
|
|
|
|
recipient: Employee,
|
|
|
|
|
base_url: str,
|
|
|
|
|
) -> str:
|
|
|
|
|
base = f"Task #{task.id}: {task.title}" if task.id is not None else f"Task: {task.title}"
|
2026-02-02 16:05:18 +05:30
|
|
|
|
2026-02-02 20:32:38 +05:30
|
|
|
if ctx.event in {"task.created", "task.assigned"} and recipient.employee_type == "agent":
|
2026-02-02 21:53:47 +05:30
|
|
|
desc = (task.description or "").strip()
|
2026-02-02 20:32:38 +05:30
|
|
|
if len(desc) > 500:
|
|
|
|
|
desc = desc[:497] + "..."
|
|
|
|
|
desc_block = f"\n\nDescription:\n{desc}" if desc else ""
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
f"{base}\n\n"
|
2026-02-02 21:19:50 +05:30
|
|
|
f"Set BASE={base_url}\n\n"
|
|
|
|
|
"You are the assignee. Start NOW (use the exec tool to run these curl commands):\n"
|
2026-02-02 21:53:47 +05:30
|
|
|
f"1) curl -sS -X PATCH $BASE/tasks/{task.id} -H 'X-Actor-Employee-Id: {recipient.id}' -H 'Content-Type: application/json' -d '{{\"status\":\"in_progress\"}}'\n"
|
|
|
|
|
f"2) curl -sS -X POST $BASE/task-comments -H 'X-Actor-Employee-Id: {recipient.id}' -H 'Content-Type: application/json' -d '{{\"task_id\":{task.id},\"body\":\"Plan: ... Next: ...\"}}'\n"
|
2026-02-02 20:32:38 +05:30
|
|
|
"3) Do the work\n"
|
2026-02-02 21:53:47 +05:30
|
|
|
"4) Post progress updates via POST $BASE/task-comments (same headers)\n"
|
|
|
|
|
f"5) When complete: set status=review (assignee cannot set done) and wait for manager approval\n"
|
2026-02-02 20:32:38 +05:30
|
|
|
f"{desc_block}"
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-02 16:05:18 +05:30
|
|
|
if ctx.event == "comment.created":
|
|
|
|
|
snippet = ""
|
2026-02-02 21:53:47 +05:30
|
|
|
if comment and comment.body:
|
|
|
|
|
snippet = comment.body.strip().replace("\n", " ")
|
2026-02-02 16:05:18 +05:30
|
|
|
if len(snippet) > 180:
|
|
|
|
|
snippet = snippet[:177] + "..."
|
|
|
|
|
snippet = f"\nComment: {snippet}"
|
2026-02-02 21:53:47 +05:30
|
|
|
return f"New comment on {base}.{snippet}\nPlease review and respond in Mission Control."
|
2026-02-02 16:05:18 +05:30
|
|
|
|
|
|
|
|
if ctx.event == "status.changed":
|
2026-02-02 21:55:45 +05:30
|
|
|
new_status = (getattr(task, "status", None) or "").lower()
|
|
|
|
|
if new_status in {"review", "ready_for_review"}:
|
|
|
|
|
return (
|
|
|
|
|
f"Review requested for {base}.\n"
|
|
|
|
|
"As the reviewer/manager, you must:\n"
|
|
|
|
|
"1) Read the task + latest assignee comments\n"
|
|
|
|
|
"2) Decide: approve or request changes\n"
|
|
|
|
|
"3) Leave an audit comment explaining your decision (required)\n"
|
|
|
|
|
f"4) Submit decision via POST /tasks/{task.id}/review (decision=approve|changes)\n"
|
|
|
|
|
"Approve → task becomes done. Changes → task returns to in_progress and assignee is notified."
|
|
|
|
|
)
|
|
|
|
|
return (
|
|
|
|
|
f"Status changed on {base} → {task.status}.\n"
|
|
|
|
|
"Please review and respond in Mission Control."
|
|
|
|
|
)
|
2026-02-02 16:05:18 +05:30
|
|
|
|
|
|
|
|
if ctx.event == "task.created":
|
2026-02-02 21:53:47 +05:30
|
|
|
return f"New task created: {base}.\nPlease review and respond in Mission Control."
|
|
|
|
|
|
|
|
|
|
if ctx.event == "task.assigned":
|
|
|
|
|
return f"Assigned: {base}.\nPlease review and respond in Mission Control."
|
|
|
|
|
|
|
|
|
|
return f"Update on {base}.\nPlease review and respond in Mission Control."
|
2026-02-02 16:05:18 +05:30
|
|
|
|
|
|
|
|
|
2026-02-02 21:53:47 +05:30
|
|
|
def notify_openclaw(ctx: NotifyContext) -> None:
|
|
|
|
|
"""Send OpenClaw notifications.
|
|
|
|
|
|
|
|
|
|
Runs in BackgroundTasks; opens its own DB session for safety.
|
|
|
|
|
"""
|
2026-02-02 16:05:18 +05:30
|
|
|
|
|
|
|
|
client = OpenClawClient.from_env()
|
2026-02-02 20:51:08 +05:30
|
|
|
logger.info(
|
|
|
|
|
"notify_openclaw: start",
|
2026-02-02 21:53:47 +05:30
|
|
|
extra={"event": ctx.event, "task_id": ctx.task_id, "actor": ctx.actor_employee_id},
|
2026-02-02 20:51:08 +05:30
|
|
|
)
|
2026-02-02 16:05:18 +05:30
|
|
|
if client is None:
|
2026-02-02 20:51:08 +05:30
|
|
|
logger.warning("notify_openclaw: skipped (missing OpenClaw env)")
|
2026-02-02 16:05:18 +05:30
|
|
|
return
|
|
|
|
|
|
2026-02-02 21:53:47 +05:30
|
|
|
with Session(engine) as session:
|
|
|
|
|
task = session.get(Task, ctx.task_id)
|
|
|
|
|
if task is None:
|
|
|
|
|
logger.warning("notify_openclaw: task not found", extra={"task_id": ctx.task_id})
|
|
|
|
|
return
|
2026-02-02 21:43:38 +05:30
|
|
|
|
2026-02-02 21:53:47 +05:30
|
|
|
comment = session.get(TaskComment, ctx.comment_id) if ctx.comment_id else None
|
|
|
|
|
|
|
|
|
|
if ctx.event == "status.changed":
|
|
|
|
|
new_status = (getattr(task, "status", None) or "").lower()
|
|
|
|
|
if new_status in {"review", "ready_for_review"} and task.reviewer_employee_id:
|
|
|
|
|
ensure_employee_provisioned(session, int(task.reviewer_employee_id))
|
2026-02-02 16:05:18 +05:30
|
|
|
|
2026-02-02 21:53:47 +05:30
|
|
|
recipient_ids = resolve_recipients(session, ctx, task, comment)
|
2026-02-02 20:51:08 +05:30
|
|
|
logger.info(
|
2026-02-02 21:53:47 +05:30
|
|
|
"notify_openclaw: recipients resolved", extra={"recipient_ids": sorted(recipient_ids)}
|
2026-02-02 20:51:08 +05:30
|
|
|
)
|
2026-02-02 21:53:47 +05:30
|
|
|
recipients = _employees_with_session_keys(session, recipient_ids)
|
|
|
|
|
if not recipients:
|
|
|
|
|
logger.info("notify_openclaw: no recipients with session keys")
|
|
|
|
|
return
|
2026-02-02 16:05:18 +05:30
|
|
|
|
2026-02-02 21:53:47 +05:30
|
|
|
# base URL used in agent messages
|
|
|
|
|
base_url = __import__(
|
|
|
|
|
"app.core.urls", fromlist=["public_api_base_url"]
|
|
|
|
|
).public_api_base_url()
|
|
|
|
|
|
|
|
|
|
for e in recipients:
|
|
|
|
|
sk = getattr(e, "openclaw_session_key", None)
|
|
|
|
|
if not sk:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
message = build_message(
|
|
|
|
|
ctx=ctx,
|
|
|
|
|
task=task,
|
|
|
|
|
comment=comment,
|
|
|
|
|
recipient=e,
|
|
|
|
|
base_url=base_url,
|
2026-02-02 16:05:18 +05:30
|
|
|
)
|
2026-02-02 21:53:47 +05:30
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
client.tools_invoke(
|
|
|
|
|
"sessions_send",
|
|
|
|
|
{"sessionKey": sk, "message": message},
|
2026-02-02 21:58:59 +05:30
|
|
|
timeout_s=30.0,
|
2026-02-02 21:53:47 +05:30
|
|
|
)
|
|
|
|
|
except Exception:
|
|
|
|
|
# keep the log, but avoid giant stack spam unless debugging
|
|
|
|
|
logger.warning(
|
|
|
|
|
"notify_openclaw: sessions_send failed",
|
|
|
|
|
extra={
|
|
|
|
|
"event": ctx.event,
|
|
|
|
|
"task_id": ctx.task_id,
|
|
|
|
|
"to_employee_id": getattr(e, "id", None),
|
|
|
|
|
"session_key": sk,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
continue
|