| 
 | 1 | +# SPDX-License-Identifier: MIT  | 
 | 2 | +# Copyright (c) 2025 LlamaIndex Inc.  | 
 | 3 | + | 
 | 4 | +from __future__ import annotations  | 
 | 5 | + | 
 | 6 | +import time  | 
 | 7 | +from typing import AsyncGenerator  | 
 | 8 | + | 
 | 9 | +from dbos import DBOS, SetWorkflowID  # required extra, import must succeed  | 
 | 10 | + | 
 | 11 | +from workflows.events import Event, StopEvent  | 
 | 12 | +from workflows.runtime.types.plugin import (  | 
 | 13 | +    ControlLoopFunction,  | 
 | 14 | +    Plugin,  | 
 | 15 | +    WorkflowRuntime,  | 
 | 16 | +    RegisteredWorkflow,  | 
 | 17 | +)  | 
 | 18 | +from workflows.runtime.types.internal_state import BrokerState  | 
 | 19 | +from workflows.runtime.types.step_function import StepWorkerFunction  | 
 | 20 | +from workflows.runtime.types.ticks import WorkflowTick  | 
 | 21 | + | 
 | 22 | +from workflows.workflow import Workflow  | 
 | 23 | + | 
 | 24 | + | 
 | 25 | +@DBOS.step()  | 
 | 26 | +async def _durable_time() -> float:  | 
 | 27 | +    return time.time()  | 
 | 28 | + | 
 | 29 | + | 
 | 30 | +class DBOSRuntime:  | 
 | 31 | +    def register(  | 
 | 32 | +        self,  | 
 | 33 | +        workflow: Workflow,  | 
 | 34 | +        workflow_function: ControlLoopFunction,  | 
 | 35 | +        steps: dict[str, StepWorkerFunction],  | 
 | 36 | +    ) -> RegisteredWorkflow | None:  | 
 | 37 | +        """  | 
 | 38 | +        Wrap the workflow control loop in a DBOS workflow so ticks are received via DBOS.recv  | 
 | 39 | +        and sent via DBOS.send, enabling durable orchestration.  | 
 | 40 | +        """  | 
 | 41 | + | 
 | 42 | +        @DBOS.workflow()  | 
 | 43 | +        async def _dbos_control_loop(  | 
 | 44 | +            start_event: Event | None,  | 
 | 45 | +            init_state: BrokerState | None,  | 
 | 46 | +            run_id: str,  | 
 | 47 | +        ) -> StopEvent:  | 
 | 48 | +            with SetWorkflowID(run_id):  | 
 | 49 | +                return await workflow_function(start_event, init_state, run_id)  | 
 | 50 | + | 
 | 51 | +        wrapped_steps = {name: DBOS.step()(step) for name, step in steps.items()}  | 
 | 52 | + | 
 | 53 | +        return RegisteredWorkflow(  | 
 | 54 | +            workflow_function=_dbos_control_loop, steps=wrapped_steps  | 
 | 55 | +        )  | 
 | 56 | + | 
 | 57 | +    def new_runtime(self, run_id: str) -> WorkflowRuntime:  | 
 | 58 | +        runtime: WorkflowRuntime = DBOSWorkflowRuntime(run_id)  | 
 | 59 | +        return runtime  | 
 | 60 | + | 
 | 61 | + | 
 | 62 | +dbos_runtime: Plugin = DBOSRuntime()  | 
 | 63 | + | 
 | 64 | + | 
 | 65 | +class DBOSWorkflowRuntime:  | 
 | 66 | +    """  | 
 | 67 | +    Workflow runtime backed by asyncio mailboxes, with durable timing via DBOS when available.  | 
 | 68 | +
  | 
 | 69 | +    - send_event/wait_receive implement the tick mailbox used by the control loop  | 
 | 70 | +    - write_to_event_stream/stream_published_events expose published events to callers  | 
 | 71 | +    - get_now returns a stable value on first call within a run (durable if DBOS is installed)  | 
 | 72 | +    - sleep uses DBOS durable sleep when available, otherwise asyncio.sleep  | 
 | 73 | +    - on_tick/replay provide a lightweight snapshot for debug/replay via the broker  | 
 | 74 | +    """  | 
 | 75 | + | 
 | 76 | +    def __init__(  | 
 | 77 | +        self,  | 
 | 78 | +        run_id: str,  | 
 | 79 | +    ) -> None:  | 
 | 80 | +        self.run_id = run_id  | 
 | 81 | + | 
 | 82 | +    # Mailbox used by control loop and broker  | 
 | 83 | +    async def wait_receive(self) -> WorkflowTick:  | 
 | 84 | +        # Receive next tick via DBOS durable notification  | 
 | 85 | +        tick = await DBOS.recv_async()  | 
 | 86 | +        return tick  # type: ignore[return-value]  | 
 | 87 | + | 
 | 88 | +    async def send_event(self, tick: WorkflowTick) -> None:  | 
 | 89 | +        await DBOS.send_async(self.run_id, tick)  | 
 | 90 | + | 
 | 91 | +    # Event stream used by handlers/observers  | 
 | 92 | +    async def write_to_event_stream(self, event: Event) -> None:  | 
 | 93 | +        await DBOS.write_stream_async("published_events", event)  | 
 | 94 | + | 
 | 95 | +    async def stream_published_events(self) -> AsyncGenerator[Event, None]:  | 
 | 96 | +        async for event in DBOS.read_stream_async(self.run_id, "published_events"):  | 
 | 97 | +            yield event  | 
 | 98 | + | 
 | 99 | +    # Timing utilities  | 
 | 100 | +    async def get_now(self) -> float:  | 
 | 101 | +        return await _durable_time()  | 
 | 102 | + | 
 | 103 | +    async def sleep(self, seconds: float) -> None:  | 
 | 104 | +        await DBOS.sleep_async(seconds)  | 
 | 105 | + | 
 | 106 | +    async def close(self) -> None:  | 
 | 107 | +        pass  | 
0 commit comments