rayhpeng 9d0a42c1fb refactor(runtime): restructure runs module with new execution architecture
Major refactoring of deerflow/runtime/:
- runs/callbacks/ - new callback system (builder, events, title, tokens)
- runs/internal/ - execution internals (executor, supervisor, stream_logic, registry)
- runs/internal/execution/ - execution artifacts and events handling
- runs/facade.py - high-level run facade
- runs/observer.py - run observation protocol
- runs/types.py - type definitions
- runs/store/ - simplified store interfaces (create, delete, query, event)

Refactor stream_bridge/:
- Replace old providers with contract.py and exceptions.py
- Remove async_provider.py, base.py, memory.py

Add documentation:
- README.md and README_zh.md for runtime module

Remove deprecated:
- manager.py moved to internal/
- worker.py, schemas.py
- user_context.py

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-04-22 11:28:01 +08:00

65 lines
1.8 KiB
Python

"""Execution preparation helpers for a single run."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any
from langchain_core.callbacks import BaseCallbackHandler
from langchain_core.runnables import RunnableConfig
from langgraph.runtime import Runtime
from deerflow.runtime.stream_bridge import StreamBridge
@dataclass
class RunBuildArtifacts:
"""Assembled agent runtime pieces for a single run."""
agent: Any
runnable_config: dict[str, Any]
reference_store: Any | None = None
def build_run_artifacts(
*,
thread_id: str,
run_id: str,
checkpointer: Any | None,
store: Any | None,
agent_factory: Any,
config: dict[str, Any],
bridge: StreamBridge,
interrupt_before: list[str] | None = None,
interrupt_after: list[str] | None = None,
callbacks: list[BaseCallbackHandler] | None = None,
) -> RunBuildArtifacts:
"""Assemble all components needed for agent execution."""
runtime = Runtime(context={"thread_id": thread_id}, store=store)
if "context" in config and isinstance(config["context"], dict):
config["context"].setdefault("thread_id", thread_id)
config.setdefault("configurable", {})["__pregel_runtime"] = runtime
config_callbacks = config.setdefault("callbacks", [])
if callbacks:
config_callbacks.extend(callbacks)
runnable_config = RunnableConfig(**config)
agent = agent_factory(config=runnable_config)
if checkpointer is not None:
agent.checkpointer = checkpointer
if store is not None:
agent.store = store
if interrupt_before:
agent.interrupt_before_nodes = interrupt_before
if interrupt_after:
agent.interrupt_after_nodes = interrupt_after
return RunBuildArtifacts(
agent=agent,
runnable_config=dict(runnable_config),
reference_store=store,
)