Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

3.7 KB
Raw Blame
"""Read correlated replies from the durable board, including very fast jobs."""

from __future__ import annotations

from typing import Any

from dodaios.core.delegated_work import DelegatedWork, register_delegated_work

from .communication import reply_policy
from .display_name import display_name
from .events import MsgType, SocietyEnvelope
from .roster import conversation_session_id


async def result_for(store: Any, request: SocietyEnvelope) -> dict[str, Any] | None:
    events = await store.events_for_trace(request.trace_id)
    related = [row for row in events if row.parent_event_id == request.event_id]
    claims = [
        row
        for row in related
        if row.msg_type is MsgType.CLAIM and row.from_agent == request.to_agent
    ]
    for claim in claims:
        run_id = str(claim.payload.get("run_id") or "")
        if run_id and not run_id.startswith("turn:"):
            related.extend(
                row
                for row in await store.events_for_trace(f"mission:{run_id}")
                if row.msg_type is MsgType.RESULT
                and row.from_agent == request.to_agent
                and row.payload.get("run_id") == run_id
            )
    for row in reversed(related):
        if row.msg_type is MsgType.VETO and row.from_agent == "scheduler":
            return {"status": "blocked", "report": row.text}
        if row.from_agent != request.to_agent or row.to_agent not in (request.from_agent, None):
            continue
        if row.msg_type is MsgType.RESULT:
            return {
                "status": str(row.payload.get("status") or "unknown"),
                "report": str(row.payload.get("done") or row.text),
                "evidence": row.payload.get("evidence", []),
                "open": row.payload.get("open", []),
            }
        if row.msg_type in (MsgType.QUERY, MsgType.PROPOSE):
            return {"status": "needs_input", "report": row.text}
        if row.msg_type is MsgType.ANSWER and (
            request.msg_type is not MsgType.ASSIGN or row.payload.get("reply_status") == "blocked"
        ):
            return {"status": str(row.payload.get("reply_status") or "done"), "report": row.text}
    return None


async def track_request(runtime: Any, request: SocietyEnvelope) -> None:
    if request.msg_type not in (MsgType.ASSIGN, MsgType.QUERY, MsgType.SAY, MsgType.PROPOSE):
        return
    if reply_policy(request) != "always":
        return
    target = await runtime.roster.get(request.to_agent)

    async def probe() -> dict[str, Any] | None:
        result = await result_for(runtime.store, request)
        if result is not None:
            return result
        service = runtime.chat_service()
        if service is None or request.to_agent is None:
            return None
        claims = await runtime.store.events_for_trace(request.trace_id)
        turn_ids = {
            str(row.payload.get("run_id", ""))[5:]
            for row in claims
            if row.msg_type is MsgType.CLAIM
            and row.parent_event_id == request.event_id
            and row.from_agent == request.to_agent
            and str(row.payload.get("run_id", "")).startswith("turn:")
        }
        session_id = conversation_session_id(request.to_agent, request.from_agent)
        progress = [
            row
            for row in service.store.list_events(session_id, tail=64)
            if row.get("payload", {}).get("turn_id") in turn_ids
        ]
        return {"status": "running", "progress": str(progress[-1]["seq"])} if progress else None

    register_delegated_work(
        DelegatedWork(
            key="society:" + request.event_id,
            name=display_name(target) if target else str(request.to_agent),
            probe=probe,
        )
    )