Files
dify/api/dify_app.py
T
-LAN-andautofix-ci[bot] 823f40e71f refactor(workflow): adopt Graphon 0.8 execution orchestration (#40277)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-10-19 13:03:56 +00:00

47 lines
1.4 KiB
Python

from __future__ import annotations
from contextlib import AbstractContextManager, nullcontext
from typing import TYPE_CHECKING, Any, override
from flask import Flask
from flask.ctx import AppContext
from graphon.file.runtime import use_workflow_file_runtime
if TYPE_CHECKING:
from extensions.ext_login import DifyLoginManager
class DifyApp(Flask):
"""Flask application type with Dify-specific extension attributes."""
login_manager: DifyLoginManager
@override
def app_context(self) -> AppContext:
return DifyAppContext(self)
class DifyAppContext(AppContext):
"""Keep the file adapter available through Flask teardown, including failures."""
def __init__(self, app: DifyApp) -> None:
super().__init__(app)
self._file_scopes: list[AbstractContextManager[None]] = []
@override
def push(self) -> None:
super().push()
runtime = self.app.extensions.get("workflow_file_runtime")
scope = use_workflow_file_runtime(runtime) if runtime is not None else nullcontext()
scope.__enter__()
self._file_scopes.append(scope)
@override
def pop(self, *args: Any, **kwargs: Any) -> None:
# Forward omitted arguments unchanged so Flask can infer the active exception.
try:
super().pop(*args, **kwargs)
finally:
self._file_scopes.pop().__exit__(None, None, None)