Files
gsfdc/demo-dify/dify/api/dify_app.py
T
wuzilongandCursor 6519682885 chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-18 13:59:25 +08: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)