File size: 5,245 Bytes
64fb1e7 93117c2 64fb1e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | """
Workflow engine β transition table runner + tenant config helpers.
WorkflowRunner.advance(step, key) is the single entry point for all tool-outcome
routing. It looks up (step, key) in definitions.STEP_TRANSITIONS, records the step
in WorkflowProgress, and returns a StepOutcome with a directive message and metadata.
Wrappers in agent_tools.py call advance() and return outcome.message β no if/else.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Optional
# ββ Tenant config helpers (unchanged) ββββββββββββββββββββββββββββββββββββββββ
def get_workflow(name: str) -> dict | None:
"""Return the workflow config dict for the given workflow name, or None."""
try:
from config_loader import get_config
return get_config().workflow_configs.get(name)
except Exception:
return None
def get_allowed_tools(workflow_name: str) -> list[str]:
"""Return the list of tools allowed in a given workflow."""
wf = get_workflow(workflow_name)
return wf.get("allowed_tools", []) if wf else []
def should_auto_escalate(workflow_name: str, trigger: str) -> bool:
"""Return True if the given trigger should auto-escalate in this workflow."""
wf = get_workflow(workflow_name)
return trigger in wf.get("auto_escalate_on", []) if wf else False
def requires_verification(workflow_name: str) -> bool:
"""Return True if this workflow requires identity verification before tool use."""
wf = get_workflow(workflow_name)
return wf.get("requires_verification", False) if wf else False
# ββ WorkflowRunner ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@dataclass
class StepOutcome:
message: str
requires_consent: bool = False
is_terminal: bool = False
escalation_team: Optional[str] = None
@dataclass
class WorkflowProgress:
"""Audit trail of steps taken this call + pending-consent state."""
steps_taken: list[dict] = field(default_factory=list)
pending_consent_team: Optional[str] = None # set when requires_consent=True
pending_consent_step: Optional[str] = None # which step is waiting for answer
# ββ In-memory workflow accessors ββββββββββββββββββββββββββββββββββββββββββββββ
def load_workflow(workflow_id: str) -> Any:
"""Return WorkflowDefinition from the in-memory cache, or None."""
try:
from config_loader import get_active_workflows
for wf in get_active_workflows():
if wf.id == workflow_id:
return wf
except Exception:
pass
return None
def load_node(workflow_id: str, node_id: str) -> Any:
"""Return WorkflowNode from the in-memory cache, or None."""
wf = load_workflow(workflow_id)
if wf is None:
return None
return wf.nodes.get(node_id)
class WorkflowRunner:
"""
Stateless resolver that translates (step_name, result_key) pairs into
structured StepOutcome objects using the STEP_TRANSITIONS lookup table.
Usage in a wrapper:
runner = WorkflowRunner(session)
outcome = runner.advance("verify_otp", result.get("fast_response_key"))
return outcome.message
"""
def __init__(self, session: Any) -> None:
self._session = session
if session.workflow is None:
session.workflow = WorkflowProgress()
@property
def progress(self) -> WorkflowProgress:
return self._session.workflow # type: ignore[return-value]
def advance(
self,
step_name: str,
result_key: str,
extras: dict | None = None,
) -> StepOutcome:
"""
Resolve (step_name, result_key) β StepOutcome.
Records the step in WorkflowProgress.steps_taken for auditability.
Sets pending_consent_* fields when the outcome requires user consent.
"""
from workflow.definitions import STEP_TRANSITIONS
ts = datetime.now(timezone.utc).isoformat()
entry: dict = {"step": step_name, "key": result_key, "ts": ts}
if extras:
entry.update(extras)
self.progress.steps_taken.append(entry)
transition = STEP_TRANSITIONS.get(step_name, {}).get(result_key)
if transition is None:
return StepOutcome(
message=(
f"Unexpected result '{result_key}' from {step_name}. "
"Acknowledge the issue and offer to help in another way."
),
)
if transition.requires_consent:
self.progress.pending_consent_team = transition.escalation_team
self.progress.pending_consent_step = step_name
return StepOutcome(
message=transition.message,
requires_consent=transition.requires_consent,
is_terminal=transition.is_terminal,
escalation_team=transition.escalation_team,
)
|