Files
ai-agent/backend/runs/reply.py
gabrielvidalandClaude Opus 5.5 e0d0bf3b51 refactor(approvals): move Approvals to the live approvals plugin
Removes core backend/approvals/ (routes, service, store), its schemas and
tests, Settings → Approvals, the generated client bits and `notify approval`
with cli/skills/ask-approval: the ai-agent-plugins 'approvals' plugin (with
its ask-approval skill) replaces them.

- carryover.approvals(): /data/approvals.json → the plugin's hub-scoped
  approvals.json, once (history and standing rules kept)
- the run-exit watcher now dispatches conversation.finished to live plugins
- /settings/approvals redirects to /settings/plugin/approvals
- guests get the approval guidance line when the plugin is on for them

authz/matrix.golden.txt is left for the merge step (3 approvals routes gone).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 20:02:48 +01:00

82 lines
3.0 KiB
Python

"""App-initiated turns into an existing conversation, always preserving guest
scope and active-user checks.
Shared by child-conversation reports (``runs/children.py``), the new-project
flow, and the ``conversations.reply`` plugin hub capability.
"""
from __future__ import annotations
import logging
from users.caller import OWNER, OWNER_ID, OWNER_NAME, guest_from_record
log = logging.getLogger("ai-agent.runs.reply")
APP_TAG = '<ai-agent-from user="ai-agent" name="ai-agent" role="app"/>'
def requester_of(deps, sid: str) -> str:
"""Whose conversation ``sid`` is: its guest owner, else the owner."""
return (deps.meta_store.peek(sid) or {}).get("owner") or OWNER_ID
def caller_of(deps, uid: str):
if uid == OWNER_ID:
return OWNER
u = deps.users.active(uid)
return guest_from_record(u) if u else None
def display_name(deps, uid: str) -> str:
if uid == OWNER_ID:
return OWNER_NAME
return (deps.users.get(uid) or {}).get("name") or uid
def session_cwd(deps, sid: str) -> str | None:
"""Where a session last ran: the cwd of its newest transcript."""
from conversations import ids
from conversations.catalog import _conv_ids_by_session
conv = _conv_ids_by_session(deps).get(sid)
if not conv:
return None
summ = deps.store.get_summary(ids.path_of(conv)) or {}
return summ.get("cwd") or None
def say(deps, sid: str, text: str, tag: str = APP_TAG, *, harness: str | None = "claude",
inbox_only: bool = False) -> dict | None:
"""An app turn in the conversation, run where it already is — with its
guest's scope when a guest owns it.
``harness=None`` keeps the session's own (runs/children.py's report back);
``inbox_only`` only rides a live run (``_send_message``). Returns the
send's answer, or None when it was not sent (a guest no longer enabled)."""
from runs.service import _send_message
from users import runs as guest_runs
requester = requester_of(deps, sid)
scope = None
if requester != OWNER_ID:
caller = caller_of(deps, requester)
if caller is None:
# A guest disabled or deleted since: never their session on the
# owner's permissions (no scope = the owner's run).
log.warning("not resuming %s: its guest %s is not enabled", sid, requester)
return None
slug = ((deps.meta_store.peek(sid) or {}).get("projects") or [None])[0]
if slug and caller.can(slug, "chat"):
scope = guest_runs.scope_for(caller, slug, caller.level(slug),
guest_runs.shared_skills(deps))
else:
scope = {**guest_runs.scope_for(caller, slug or "-", None,
guest_runs.shared_skills(deps)),
"writeDirs": ["/tmp"]}
return _send_message(deps, f"{tag}\n{text}", session_id=sid, harness=harness,
cwd=session_cwd(deps, sid), scope=scope, inbox_only=inbox_only)
# Backwards-compatible aliases
_say = say
_caller = caller_of
_name = display_name