Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.9 KB
Raw Blame
"""Workflow execution values shared across runtime and storage boundaries."""

from __future__ import annotations

from dataclasses import dataclass, field
from threading import Lock
from time import perf_counter
from typing import TYPE_CHECKING, override

from pydantic import Field

from graphon.entities import WorkflowExecution as GraphonWorkflowExecution

if TYPE_CHECKING:
    from core.app.entities.app_invoke_entities import UserFrom


@dataclass(frozen=True)
class WorkflowExecutionIdentity:
    """Resolved caller values crossing execution boundaries without ORM objects."""

    user_id: str
    user_from: UserFrom


@dataclass
class WorkflowExecutionTiming:
    """Settle one execution segment once, excluding time spent paused."""

    elapsed_time: float = 0.0
    _started_at: float | None = field(default=None, init=False, repr=False, compare=False)
    _finished: bool = field(default=False, init=False, repr=False, compare=False)
    _lock: Lock = field(default_factory=Lock, init=False, repr=False, compare=False)

    def start(self) -> None:
        with self._lock:
            if self._started_at is None:
                self._started_at = perf_counter()

    def finish(self) -> float:
        with self._lock:
            if self._started_at is not None and not self._finished:
                self.elapsed_time += max(perf_counter() - self._started_at, 0.0)
                self._finished = True
            return self.elapsed_time


class WorkflowExecution(GraphonWorkflowExecution):
    """Carry settled active duration independently of wall-clock timestamps."""

    accumulated_elapsed_time: float | None = Field(default=None, ge=0, allow_inf_nan=False)
    """None preserves timestamp-based duration for older serialized executions."""

    @property
    @override
    def elapsed_time(self) -> float:
        if self.accumulated_elapsed_time is not None:
            return self.accumulated_elapsed_time
        return super().elapsed_time