Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

workflow.py 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. import json
  2. import logging
  3. from collections.abc import Mapping, Sequence
  4. from datetime import datetime
  5. from enum import Enum, StrEnum
  6. from typing import TYPE_CHECKING, Any, Optional, Union, cast
  7. from uuid import uuid4
  8. import sqlalchemy as sa
  9. from sqlalchemy import DateTime, exists, orm, select
  10. from core.file.constants import maybe_file_object
  11. from core.file.models import File
  12. from core.variables import utils as variable_utils
  13. from core.variables.variables import FloatVariable, IntegerVariable, StringVariable
  14. from core.workflow.constants import CONVERSATION_VARIABLE_NODE_ID, SYSTEM_VARIABLE_NODE_ID
  15. from core.workflow.enums import NodeType
  16. from factories.variable_factory import TypeMismatchError, build_segment_with_type
  17. from libs.datetime_utils import naive_utc_now
  18. from ._workflow_exc import NodeNotFoundError, WorkflowDataError
  19. if TYPE_CHECKING:
  20. from models.model import AppMode
  21. from sqlalchemy import Index, PrimaryKeyConstraint, String, UniqueConstraint, func
  22. from sqlalchemy.orm import Mapped, declared_attr, mapped_column
  23. from constants import DEFAULT_FILE_NUMBER_LIMITS, HIDDEN_VALUE
  24. from core.helper import encrypter
  25. from core.variables import SecretVariable, Segment, SegmentType, Variable
  26. from factories import variable_factory
  27. from libs import helper
  28. from .account import Account
  29. from .base import Base
  30. from .engine import db
  31. from .enums import CreatorUserRole, DraftVariableType
  32. from .types import EnumText, StringUUID
  33. logger = logging.getLogger(__name__)
  34. class WorkflowType(Enum):
  35. """
  36. Workflow Type Enum
  37. """
  38. WORKFLOW = "workflow"
  39. CHAT = "chat"
  40. @classmethod
  41. def value_of(cls, value: str) -> "WorkflowType":
  42. """
  43. Get value of given mode.
  44. :param value: mode value
  45. :return: mode
  46. """
  47. for mode in cls:
  48. if mode.value == value:
  49. return mode
  50. raise ValueError(f"invalid workflow type value {value}")
  51. @classmethod
  52. def from_app_mode(cls, app_mode: Union[str, "AppMode"]) -> "WorkflowType":
  53. """
  54. Get workflow type from app mode.
  55. :param app_mode: app mode
  56. :return: workflow type
  57. """
  58. from models.model import AppMode
  59. app_mode = app_mode if isinstance(app_mode, AppMode) else AppMode.value_of(app_mode)
  60. return cls.WORKFLOW if app_mode == AppMode.WORKFLOW else cls.CHAT
  61. class _InvalidGraphDefinitionError(Exception):
  62. pass
  63. class Workflow(Base):
  64. """
  65. Workflow, for `Workflow App` and `Chat App workflow mode`.
  66. Attributes:
  67. - id (uuid) Workflow ID, pk
  68. - tenant_id (uuid) Workspace ID
  69. - app_id (uuid) App ID
  70. - type (string) Workflow type
  71. `workflow` for `Workflow App`
  72. `chat` for `Chat App workflow mode`
  73. - version (string) Version
  74. `draft` for draft version (only one for each app), other for version number (redundant)
  75. - graph (text) Workflow canvas configuration (JSON)
  76. The entire canvas configuration JSON, including Node, Edge, and other configurations
  77. - nodes (array[object]) Node list, see Node Schema
  78. - edges (array[object]) Edge list, see Edge Schema
  79. - created_by (uuid) Creator ID
  80. - created_at (timestamp) Creation time
  81. - updated_by (uuid) `optional` Last updater ID
  82. - updated_at (timestamp) `optional` Last update time
  83. """
  84. __tablename__ = "workflows"
  85. __table_args__ = (
  86. sa.PrimaryKeyConstraint("id", name="workflow_pkey"),
  87. sa.Index("workflow_version_idx", "tenant_id", "app_id", "version"),
  88. )
  89. id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"))
  90. tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
  91. app_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
  92. type: Mapped[str] = mapped_column(String(255), nullable=False)
  93. version: Mapped[str] = mapped_column(String(255), nullable=False)
  94. marked_name: Mapped[str] = mapped_column(default="", server_default="")
  95. marked_comment: Mapped[str] = mapped_column(default="", server_default="")
  96. graph: Mapped[str] = mapped_column(sa.Text)
  97. _features: Mapped[str] = mapped_column("features", sa.TEXT)
  98. created_by: Mapped[str] = mapped_column(StringUUID, nullable=False)
  99. created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
  100. updated_by: Mapped[Optional[str]] = mapped_column(StringUUID)
  101. updated_at: Mapped[datetime] = mapped_column(
  102. DateTime,
  103. nullable=False,
  104. default=naive_utc_now(),
  105. server_onupdate=func.current_timestamp(),
  106. )
  107. _environment_variables: Mapped[str] = mapped_column(
  108. "environment_variables", sa.Text, nullable=False, server_default="{}"
  109. )
  110. _conversation_variables: Mapped[str] = mapped_column(
  111. "conversation_variables", sa.Text, nullable=False, server_default="{}"
  112. )
  113. VERSION_DRAFT = "draft"
  114. @classmethod
  115. def new(
  116. cls,
  117. *,
  118. tenant_id: str,
  119. app_id: str,
  120. type: str,
  121. version: str,
  122. graph: str,
  123. features: str,
  124. created_by: str,
  125. environment_variables: Sequence[Variable],
  126. conversation_variables: Sequence[Variable],
  127. marked_name: str = "",
  128. marked_comment: str = "",
  129. ) -> "Workflow":
  130. workflow = Workflow()
  131. workflow.id = str(uuid4())
  132. workflow.tenant_id = tenant_id
  133. workflow.app_id = app_id
  134. workflow.type = type
  135. workflow.version = version
  136. workflow.graph = graph
  137. workflow.features = features
  138. workflow.created_by = created_by
  139. workflow.environment_variables = environment_variables or []
  140. workflow.conversation_variables = conversation_variables or []
  141. workflow.marked_name = marked_name
  142. workflow.marked_comment = marked_comment
  143. workflow.created_at = naive_utc_now()
  144. workflow.updated_at = workflow.created_at
  145. return workflow
  146. @property
  147. def created_by_account(self):
  148. return db.session.get(Account, self.created_by)
  149. @property
  150. def updated_by_account(self):
  151. return db.session.get(Account, self.updated_by) if self.updated_by else None
  152. @property
  153. def graph_dict(self) -> Mapping[str, Any]:
  154. # TODO(QuantumGhost): Consider caching `graph_dict` to avoid repeated JSON decoding.
  155. #
  156. # Using `functools.cached_property` could help, but some code in the codebase may
  157. # modify the returned dict, which can cause issues elsewhere.
  158. #
  159. # For example, changing this property to a cached property led to errors like the
  160. # following when single stepping an `Iteration` node:
  161. #
  162. # Root node id 1748401971780start not found in the graph
  163. #
  164. # There is currently no standard way to make a dict deeply immutable in Python,
  165. # and tracking modifications to the returned dict is difficult. For now, we leave
  166. # the code as-is to avoid these issues.
  167. #
  168. # Currently, the following functions / methods would mutate the returned dict:
  169. #
  170. # - `_get_graph_and_variable_pool_of_single_iteration`.
  171. # - `_get_graph_and_variable_pool_of_single_loop`.
  172. return json.loads(self.graph) if self.graph else {}
  173. def get_node_config_by_id(self, node_id: str) -> Mapping[str, Any]:
  174. """Extract a node configuration from the workflow graph by node ID.
  175. A node configuration is a dictionary containing the node's properties, including
  176. the node's id, title, and its data as a dict.
  177. """
  178. workflow_graph = self.graph_dict
  179. if not workflow_graph:
  180. raise WorkflowDataError(f"workflow graph not found, workflow_id={self.id}")
  181. nodes = workflow_graph.get("nodes")
  182. if not nodes:
  183. raise WorkflowDataError("nodes not found in workflow graph")
  184. try:
  185. node_config: dict[str, Any] = next(filter(lambda node: node["id"] == node_id, nodes))
  186. except StopIteration:
  187. raise NodeNotFoundError(node_id)
  188. assert isinstance(node_config, dict)
  189. return node_config
  190. @staticmethod
  191. def get_node_type_from_node_config(node_config: Mapping[str, Any]) -> NodeType:
  192. """Extract type of a node from the node configuration returned by `get_node_config_by_id`."""
  193. node_config_data = node_config.get("data", {})
  194. # Get node class
  195. node_type = NodeType(node_config_data.get("type"))
  196. return node_type
  197. @staticmethod
  198. def get_enclosing_node_type_and_id(node_config: Mapping[str, Any]) -> tuple[NodeType, str] | None:
  199. in_loop = node_config.get("isInLoop", False)
  200. in_iteration = node_config.get("isInIteration", False)
  201. if in_loop:
  202. loop_id = node_config.get("loop_id")
  203. if loop_id is None:
  204. raise _InvalidGraphDefinitionError("invalid graph")
  205. return NodeType.LOOP, loop_id
  206. elif in_iteration:
  207. iteration_id = node_config.get("iteration_id")
  208. if iteration_id is None:
  209. raise _InvalidGraphDefinitionError("invalid graph")
  210. return NodeType.ITERATION, iteration_id
  211. else:
  212. return None
  213. @property
  214. def features(self) -> str:
  215. """
  216. Convert old features structure to new features structure.
  217. """
  218. if not self._features:
  219. return self._features
  220. features = json.loads(self._features)
  221. if features.get("file_upload", {}).get("image", {}).get("enabled", False):
  222. image_enabled = True
  223. image_number_limits = int(features["file_upload"]["image"].get("number_limits", DEFAULT_FILE_NUMBER_LIMITS))
  224. image_transfer_methods = features["file_upload"]["image"].get(
  225. "transfer_methods", ["remote_url", "local_file"]
  226. )
  227. features["file_upload"]["enabled"] = image_enabled
  228. features["file_upload"]["number_limits"] = image_number_limits
  229. features["file_upload"]["allowed_file_upload_methods"] = image_transfer_methods
  230. features["file_upload"]["allowed_file_types"] = features["file_upload"].get("allowed_file_types", ["image"])
  231. features["file_upload"]["allowed_file_extensions"] = features["file_upload"].get(
  232. "allowed_file_extensions", []
  233. )
  234. del features["file_upload"]["image"]
  235. self._features = json.dumps(features)
  236. return self._features
  237. @features.setter
  238. def features(self, value: str):
  239. self._features = value
  240. @property
  241. def features_dict(self) -> dict[str, Any]:
  242. return json.loads(self.features) if self.features else {}
  243. def user_input_form(self, to_old_structure: bool = False) -> list[Any]:
  244. # get start node from graph
  245. if not self.graph:
  246. return []
  247. graph_dict = self.graph_dict
  248. if "nodes" not in graph_dict:
  249. return []
  250. start_node = next((node for node in graph_dict["nodes"] if node["data"]["type"] == "start"), None)
  251. if not start_node:
  252. return []
  253. # get user_input_form from start node
  254. variables: list[Any] = start_node.get("data", {}).get("variables", [])
  255. if to_old_structure:
  256. old_structure_variables: list[dict[str, Any]] = []
  257. for variable in variables:
  258. old_structure_variables.append({variable["type"]: variable})
  259. return old_structure_variables
  260. return variables
  261. @property
  262. def unique_hash(self) -> str:
  263. """
  264. Get hash of workflow.
  265. :return: hash
  266. """
  267. entity = {"graph": self.graph_dict, "features": self.features_dict}
  268. return helper.generate_text_hash(json.dumps(entity, sort_keys=True))
  269. @property
  270. def tool_published(self) -> bool:
  271. """
  272. DEPRECATED: This property is not accurate for determining if a workflow is published as a tool.
  273. It only checks if there's a WorkflowToolProvider for the app, not if this specific workflow version
  274. is the one being used by the tool.
  275. For accurate checking, use a direct query with tenant_id, app_id, and version.
  276. """
  277. from models.tools import WorkflowToolProvider
  278. stmt = select(
  279. exists().where(
  280. WorkflowToolProvider.tenant_id == self.tenant_id,
  281. WorkflowToolProvider.app_id == self.app_id,
  282. )
  283. )
  284. return db.session.execute(stmt).scalar_one()
  285. @property
  286. def environment_variables(self) -> Sequence[StringVariable | IntegerVariable | FloatVariable | SecretVariable]:
  287. # _environment_variables is guaranteed to be non-None due to server_default="{}"
  288. # Use workflow.tenant_id to avoid relying on request user in background threads
  289. tenant_id = self.tenant_id
  290. if not tenant_id:
  291. return []
  292. environment_variables_dict: dict[str, Any] = json.loads(self._environment_variables)
  293. results = [
  294. variable_factory.build_environment_variable_from_mapping(v) for v in environment_variables_dict.values()
  295. ]
  296. # decrypt secret variables value
  297. def decrypt_func(var: Variable) -> StringVariable | IntegerVariable | FloatVariable | SecretVariable:
  298. if isinstance(var, SecretVariable):
  299. return var.model_copy(update={"value": encrypter.decrypt_token(tenant_id=tenant_id, token=var.value)})
  300. elif isinstance(var, (StringVariable, IntegerVariable, FloatVariable)):
  301. return var
  302. else:
  303. # Other variable types are not supported for environment variables
  304. raise AssertionError(f"Unexpected variable type for environment variable: {type(var)}")
  305. decrypted_results: list[SecretVariable | StringVariable | IntegerVariable | FloatVariable] = [
  306. decrypt_func(var) for var in results
  307. ]
  308. return decrypted_results
  309. @environment_variables.setter
  310. def environment_variables(self, value: Sequence[Variable]):
  311. if not value:
  312. self._environment_variables = "{}"
  313. return
  314. # Use workflow.tenant_id to avoid relying on request user in background threads
  315. tenant_id = self.tenant_id
  316. if not tenant_id:
  317. self._environment_variables = "{}"
  318. return
  319. value = list(value)
  320. if any(var for var in value if not var.id):
  321. raise ValueError("environment variable require a unique id")
  322. # Compare inputs and origin variables,
  323. # if the value is HIDDEN_VALUE, use the origin variable value (only update `name`).
  324. origin_variables_dictionary = {var.id: var for var in self.environment_variables}
  325. for i, variable in enumerate(value):
  326. if variable.id in origin_variables_dictionary and variable.value == HIDDEN_VALUE:
  327. value[i] = origin_variables_dictionary[variable.id].model_copy(update={"name": variable.name})
  328. # encrypt secret variables value
  329. def encrypt_func(var: Variable) -> Variable:
  330. if isinstance(var, SecretVariable):
  331. return var.model_copy(update={"value": encrypter.encrypt_token(tenant_id=tenant_id, token=var.value)})
  332. else:
  333. return var
  334. encrypted_vars = list(map(encrypt_func, value))
  335. environment_variables_json = json.dumps(
  336. {var.name: var.model_dump() for var in encrypted_vars},
  337. ensure_ascii=False,
  338. )
  339. self._environment_variables = environment_variables_json
  340. def to_dict(self, *, include_secret: bool = False) -> Mapping[str, Any]:
  341. environment_variables = list(self.environment_variables)
  342. environment_variables = [
  343. v if not isinstance(v, SecretVariable) or include_secret else v.model_copy(update={"value": ""})
  344. for v in environment_variables
  345. ]
  346. result = {
  347. "graph": self.graph_dict,
  348. "features": self.features_dict,
  349. "environment_variables": [var.model_dump(mode="json") for var in environment_variables],
  350. "conversation_variables": [var.model_dump(mode="json") for var in self.conversation_variables],
  351. }
  352. return result
  353. @property
  354. def conversation_variables(self) -> Sequence[Variable]:
  355. # _conversation_variables is guaranteed to be non-None due to server_default="{}"
  356. variables_dict: dict[str, Any] = json.loads(self._conversation_variables)
  357. results = [variable_factory.build_conversation_variable_from_mapping(v) for v in variables_dict.values()]
  358. return results
  359. @conversation_variables.setter
  360. def conversation_variables(self, value: Sequence[Variable]):
  361. self._conversation_variables = json.dumps(
  362. {var.name: var.model_dump() for var in value},
  363. ensure_ascii=False,
  364. )
  365. @staticmethod
  366. def version_from_datetime(d: datetime) -> str:
  367. return str(d)
  368. class WorkflowRun(Base):
  369. """
  370. Workflow Run
  371. Attributes:
  372. - id (uuid) Run ID
  373. - tenant_id (uuid) Workspace ID
  374. - app_id (uuid) App ID
  375. - workflow_id (uuid) Workflow ID
  376. - type (string) Workflow type
  377. - triggered_from (string) Trigger source
  378. `debugging` for canvas debugging
  379. `app-run` for (published) app execution
  380. - version (string) Version
  381. - graph (text) Workflow canvas configuration (JSON)
  382. - inputs (text) Input parameters
  383. - status (string) Execution status, `running` / `succeeded` / `failed` / `stopped`
  384. - outputs (text) `optional` Output content
  385. - error (string) `optional` Error reason
  386. - elapsed_time (float) `optional` Time consumption (s)
  387. - total_tokens (int) `optional` Total tokens used
  388. - total_steps (int) Total steps (redundant), default 0
  389. - created_by_role (string) Creator role
  390. - `account` Console account
  391. - `end_user` End user
  392. - created_by (uuid) Runner ID
  393. - created_at (timestamp) Run time
  394. - finished_at (timestamp) End time
  395. """
  396. __tablename__ = "workflow_runs"
  397. __table_args__ = (
  398. sa.PrimaryKeyConstraint("id", name="workflow_run_pkey"),
  399. sa.Index("workflow_run_triggerd_from_idx", "tenant_id", "app_id", "triggered_from"),
  400. )
  401. id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"))
  402. tenant_id: Mapped[str] = mapped_column(StringUUID)
  403. app_id: Mapped[str] = mapped_column(StringUUID)
  404. workflow_id: Mapped[str] = mapped_column(StringUUID)
  405. type: Mapped[str] = mapped_column(String(255))
  406. triggered_from: Mapped[str] = mapped_column(String(255))
  407. version: Mapped[str] = mapped_column(String(255))
  408. graph: Mapped[Optional[str]] = mapped_column(sa.Text)
  409. inputs: Mapped[Optional[str]] = mapped_column(sa.Text)
  410. status: Mapped[str] = mapped_column(String(255)) # running, succeeded, failed, stopped, partial-succeeded
  411. outputs: Mapped[Optional[str]] = mapped_column(sa.Text, default="{}")
  412. error: Mapped[Optional[str]] = mapped_column(sa.Text)
  413. elapsed_time: Mapped[float] = mapped_column(sa.Float, nullable=False, server_default=sa.text("0"))
  414. total_tokens: Mapped[int] = mapped_column(sa.BigInteger, server_default=sa.text("0"))
  415. total_steps: Mapped[int] = mapped_column(sa.Integer, server_default=sa.text("0"), nullable=True)
  416. created_by_role: Mapped[str] = mapped_column(String(255)) # account, end_user
  417. created_by: Mapped[str] = mapped_column(StringUUID, nullable=False)
  418. created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
  419. finished_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
  420. exceptions_count: Mapped[int] = mapped_column(sa.Integer, server_default=sa.text("0"), nullable=True)
  421. @property
  422. def created_by_account(self):
  423. created_by_role = CreatorUserRole(self.created_by_role)
  424. return db.session.get(Account, self.created_by) if created_by_role == CreatorUserRole.ACCOUNT else None
  425. @property
  426. def created_by_end_user(self):
  427. from models.model import EndUser
  428. created_by_role = CreatorUserRole(self.created_by_role)
  429. return db.session.get(EndUser, self.created_by) if created_by_role == CreatorUserRole.END_USER else None
  430. @property
  431. def graph_dict(self) -> Mapping[str, Any]:
  432. return json.loads(self.graph) if self.graph else {}
  433. @property
  434. def inputs_dict(self) -> Mapping[str, Any]:
  435. return json.loads(self.inputs) if self.inputs else {}
  436. @property
  437. def outputs_dict(self) -> Mapping[str, Any]:
  438. return json.loads(self.outputs) if self.outputs else {}
  439. @property
  440. def message(self):
  441. from models.model import Message
  442. return (
  443. db.session.query(Message).where(Message.app_id == self.app_id, Message.workflow_run_id == self.id).first()
  444. )
  445. @property
  446. def workflow(self):
  447. return db.session.query(Workflow).where(Workflow.id == self.workflow_id).first()
  448. def to_dict(self):
  449. return {
  450. "id": self.id,
  451. "tenant_id": self.tenant_id,
  452. "app_id": self.app_id,
  453. "workflow_id": self.workflow_id,
  454. "type": self.type,
  455. "triggered_from": self.triggered_from,
  456. "version": self.version,
  457. "graph": self.graph_dict,
  458. "inputs": self.inputs_dict,
  459. "status": self.status,
  460. "outputs": self.outputs_dict,
  461. "error": self.error,
  462. "elapsed_time": self.elapsed_time,
  463. "total_tokens": self.total_tokens,
  464. "total_steps": self.total_steps,
  465. "created_by_role": self.created_by_role,
  466. "created_by": self.created_by,
  467. "created_at": self.created_at,
  468. "finished_at": self.finished_at,
  469. "exceptions_count": self.exceptions_count,
  470. }
  471. @classmethod
  472. def from_dict(cls, data: dict[str, Any]) -> "WorkflowRun":
  473. return cls(
  474. id=data.get("id"),
  475. tenant_id=data.get("tenant_id"),
  476. app_id=data.get("app_id"),
  477. workflow_id=data.get("workflow_id"),
  478. type=data.get("type"),
  479. triggered_from=data.get("triggered_from"),
  480. version=data.get("version"),
  481. graph=json.dumps(data.get("graph")),
  482. inputs=json.dumps(data.get("inputs")),
  483. status=data.get("status"),
  484. outputs=json.dumps(data.get("outputs")),
  485. error=data.get("error"),
  486. elapsed_time=data.get("elapsed_time"),
  487. total_tokens=data.get("total_tokens"),
  488. total_steps=data.get("total_steps"),
  489. created_by_role=data.get("created_by_role"),
  490. created_by=data.get("created_by"),
  491. created_at=data.get("created_at"),
  492. finished_at=data.get("finished_at"),
  493. exceptions_count=data.get("exceptions_count"),
  494. )
  495. class WorkflowNodeExecutionTriggeredFrom(StrEnum):
  496. """
  497. Workflow Node Execution Triggered From Enum
  498. """
  499. SINGLE_STEP = "single-step"
  500. WORKFLOW_RUN = "workflow-run"
  501. class WorkflowNodeExecutionModel(Base):
  502. """
  503. Workflow Node Execution
  504. - id (uuid) Execution ID
  505. - tenant_id (uuid) Workspace ID
  506. - app_id (uuid) App ID
  507. - workflow_id (uuid) Workflow ID
  508. - triggered_from (string) Trigger source
  509. `single-step` for single-step debugging
  510. `workflow-run` for workflow execution (debugging / user execution)
  511. - workflow_run_id (uuid) `optional` Workflow run ID
  512. Null for single-step debugging.
  513. - index (int) Execution sequence number, used for displaying Tracing Node order
  514. - predecessor_node_id (string) `optional` Predecessor node ID, used for displaying execution path
  515. - node_id (string) Node ID
  516. - node_type (string) Node type, such as `start`
  517. - title (string) Node title
  518. - inputs (json) All predecessor node variable content used in the node
  519. - process_data (json) Node process data
  520. - outputs (json) `optional` Node output variables
  521. - status (string) Execution status, `running` / `succeeded` / `failed`
  522. - error (string) `optional` Error reason
  523. - elapsed_time (float) `optional` Time consumption (s)
  524. - execution_metadata (text) Metadata
  525. - total_tokens (int) `optional` Total tokens used
  526. - total_price (decimal) `optional` Total cost
  527. - currency (string) `optional` Currency, such as USD / RMB
  528. - created_at (timestamp) Run time
  529. - created_by_role (string) Creator role
  530. - `account` Console account
  531. - `end_user` End user
  532. - created_by (uuid) Runner ID
  533. - finished_at (timestamp) End time
  534. """
  535. __tablename__ = "workflow_node_executions"
  536. @declared_attr
  537. @classmethod
  538. def __table_args__(cls) -> Any:
  539. return (
  540. PrimaryKeyConstraint("id", name="workflow_node_execution_pkey"),
  541. Index(
  542. "workflow_node_execution_workflow_run_idx",
  543. "tenant_id",
  544. "app_id",
  545. "workflow_id",
  546. "triggered_from",
  547. "workflow_run_id",
  548. ),
  549. Index(
  550. "workflow_node_execution_node_run_idx",
  551. "tenant_id",
  552. "app_id",
  553. "workflow_id",
  554. "triggered_from",
  555. "node_id",
  556. ),
  557. Index(
  558. "workflow_node_execution_id_idx",
  559. "tenant_id",
  560. "app_id",
  561. "workflow_id",
  562. "triggered_from",
  563. "node_execution_id",
  564. ),
  565. Index(
  566. # The first argument is the index name,
  567. # which we leave as `None`` to allow auto-generation by the ORM.
  568. None,
  569. cls.tenant_id,
  570. cls.workflow_id,
  571. cls.node_id,
  572. # MyPy may flag the following line because it doesn't recognize that
  573. # the `declared_attr` decorator passes the receiving class as the first
  574. # argument to this method, allowing us to reference class attributes.
  575. cls.created_at.desc(),
  576. ),
  577. )
  578. id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"))
  579. tenant_id: Mapped[str] = mapped_column(StringUUID)
  580. app_id: Mapped[str] = mapped_column(StringUUID)
  581. workflow_id: Mapped[str] = mapped_column(StringUUID)
  582. triggered_from: Mapped[str] = mapped_column(String(255))
  583. workflow_run_id: Mapped[Optional[str]] = mapped_column(StringUUID)
  584. index: Mapped[int] = mapped_column(sa.Integer)
  585. predecessor_node_id: Mapped[Optional[str]] = mapped_column(String(255))
  586. node_execution_id: Mapped[Optional[str]] = mapped_column(String(255))
  587. node_id: Mapped[str] = mapped_column(String(255))
  588. node_type: Mapped[str] = mapped_column(String(255))
  589. title: Mapped[str] = mapped_column(String(255))
  590. inputs: Mapped[Optional[str]] = mapped_column(sa.Text)
  591. process_data: Mapped[Optional[str]] = mapped_column(sa.Text)
  592. outputs: Mapped[Optional[str]] = mapped_column(sa.Text)
  593. status: Mapped[str] = mapped_column(String(255))
  594. error: Mapped[Optional[str]] = mapped_column(sa.Text)
  595. elapsed_time: Mapped[float] = mapped_column(sa.Float, server_default=sa.text("0"))
  596. execution_metadata: Mapped[Optional[str]] = mapped_column(sa.Text)
  597. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.current_timestamp())
  598. created_by_role: Mapped[str] = mapped_column(String(255))
  599. created_by: Mapped[str] = mapped_column(StringUUID)
  600. finished_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
  601. @property
  602. def created_by_account(self):
  603. created_by_role = CreatorUserRole(self.created_by_role)
  604. # TODO(-LAN-): Avoid using db.session.get() here.
  605. return db.session.get(Account, self.created_by) if created_by_role == CreatorUserRole.ACCOUNT else None
  606. @property
  607. def created_by_end_user(self):
  608. from models.model import EndUser
  609. created_by_role = CreatorUserRole(self.created_by_role)
  610. # TODO(-LAN-): Avoid using db.session.get() here.
  611. return db.session.get(EndUser, self.created_by) if created_by_role == CreatorUserRole.END_USER else None
  612. @property
  613. def inputs_dict(self):
  614. return json.loads(self.inputs) if self.inputs else None
  615. @property
  616. def outputs_dict(self) -> dict[str, Any] | None:
  617. return json.loads(self.outputs) if self.outputs else None
  618. @property
  619. def process_data_dict(self):
  620. return json.loads(self.process_data) if self.process_data else None
  621. @property
  622. def execution_metadata_dict(self) -> dict[str, Any]:
  623. # When the metadata is unset, we return an empty dictionary instead of `None`.
  624. # This approach streamlines the logic for the caller, making it easier to handle
  625. # cases where metadata is absent.
  626. return json.loads(self.execution_metadata) if self.execution_metadata else {}
  627. @property
  628. def extras(self) -> dict[str, Any]:
  629. from core.tools.tool_manager import ToolManager
  630. extras: dict[str, Any] = {}
  631. if self.execution_metadata_dict:
  632. from core.workflow.nodes import NodeType
  633. if self.node_type == NodeType.TOOL.value and "tool_info" in self.execution_metadata_dict:
  634. tool_info: dict[str, Any] = self.execution_metadata_dict["tool_info"]
  635. extras["icon"] = ToolManager.get_tool_icon(
  636. tenant_id=self.tenant_id,
  637. provider_type=tool_info["provider_type"],
  638. provider_id=tool_info["provider_id"],
  639. )
  640. return extras
  641. class WorkflowAppLogCreatedFrom(Enum):
  642. """
  643. Workflow App Log Created From Enum
  644. """
  645. SERVICE_API = "service-api"
  646. WEB_APP = "web-app"
  647. INSTALLED_APP = "installed-app"
  648. @classmethod
  649. def value_of(cls, value: str) -> "WorkflowAppLogCreatedFrom":
  650. """
  651. Get value of given mode.
  652. :param value: mode value
  653. :return: mode
  654. """
  655. for mode in cls:
  656. if mode.value == value:
  657. return mode
  658. raise ValueError(f"invalid workflow app log created from value {value}")
  659. class WorkflowAppLog(Base):
  660. """
  661. Workflow App execution log, excluding workflow debugging records.
  662. Attributes:
  663. - id (uuid) run ID
  664. - tenant_id (uuid) Workspace ID
  665. - app_id (uuid) App ID
  666. - workflow_id (uuid) Associated Workflow ID
  667. - workflow_run_id (uuid) Associated Workflow Run ID
  668. - created_from (string) Creation source
  669. `service-api` App Execution OpenAPI
  670. `web-app` WebApp
  671. `installed-app` Installed App
  672. - created_by_role (string) Creator role
  673. - `account` Console account
  674. - `end_user` End user
  675. - created_by (uuid) Creator ID, depends on the user table according to created_by_role
  676. - created_at (timestamp) Creation time
  677. """
  678. __tablename__ = "workflow_app_logs"
  679. __table_args__ = (
  680. sa.PrimaryKeyConstraint("id", name="workflow_app_log_pkey"),
  681. sa.Index("workflow_app_log_app_idx", "tenant_id", "app_id"),
  682. sa.Index("workflow_app_log_workflow_run_id_idx", "workflow_run_id"),
  683. )
  684. id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"))
  685. tenant_id: Mapped[str] = mapped_column(StringUUID)
  686. app_id: Mapped[str] = mapped_column(StringUUID)
  687. workflow_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
  688. workflow_run_id: Mapped[str] = mapped_column(StringUUID)
  689. created_from: Mapped[str] = mapped_column(String(255), nullable=False)
  690. created_by_role: Mapped[str] = mapped_column(String(255), nullable=False)
  691. created_by: Mapped[str] = mapped_column(StringUUID, nullable=False)
  692. created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
  693. @property
  694. def workflow_run(self):
  695. return db.session.get(WorkflowRun, self.workflow_run_id)
  696. @property
  697. def created_by_account(self):
  698. created_by_role = CreatorUserRole(self.created_by_role)
  699. return db.session.get(Account, self.created_by) if created_by_role == CreatorUserRole.ACCOUNT else None
  700. @property
  701. def created_by_end_user(self):
  702. from models.model import EndUser
  703. created_by_role = CreatorUserRole(self.created_by_role)
  704. return db.session.get(EndUser, self.created_by) if created_by_role == CreatorUserRole.END_USER else None
  705. def to_dict(self):
  706. return {
  707. "id": self.id,
  708. "tenant_id": self.tenant_id,
  709. "app_id": self.app_id,
  710. "workflow_id": self.workflow_id,
  711. "workflow_run_id": self.workflow_run_id,
  712. "created_from": self.created_from,
  713. "created_by_role": self.created_by_role,
  714. "created_by": self.created_by,
  715. "created_at": self.created_at,
  716. }
  717. class ConversationVariable(Base):
  718. __tablename__ = "workflow_conversation_variables"
  719. id: Mapped[str] = mapped_column(StringUUID, primary_key=True)
  720. conversation_id: Mapped[str] = mapped_column(StringUUID, nullable=False, primary_key=True, index=True)
  721. app_id: Mapped[str] = mapped_column(StringUUID, nullable=False, index=True)
  722. data: Mapped[str] = mapped_column(sa.Text, nullable=False)
  723. created_at: Mapped[datetime] = mapped_column(
  724. DateTime, nullable=False, server_default=func.current_timestamp(), index=True
  725. )
  726. updated_at: Mapped[datetime] = mapped_column(
  727. DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp()
  728. )
  729. def __init__(self, *, id: str, app_id: str, conversation_id: str, data: str):
  730. self.id = id
  731. self.app_id = app_id
  732. self.conversation_id = conversation_id
  733. self.data = data
  734. @classmethod
  735. def from_variable(cls, *, app_id: str, conversation_id: str, variable: Variable) -> "ConversationVariable":
  736. obj = cls(
  737. id=variable.id,
  738. app_id=app_id,
  739. conversation_id=conversation_id,
  740. data=variable.model_dump_json(),
  741. )
  742. return obj
  743. def to_variable(self) -> Variable:
  744. mapping = json.loads(self.data)
  745. return variable_factory.build_conversation_variable_from_mapping(mapping)
  746. # Only `sys.query` and `sys.files` could be modified.
  747. _EDITABLE_SYSTEM_VARIABLE = frozenset(["query", "files"])
  748. def _naive_utc_datetime():
  749. return naive_utc_now()
  750. class WorkflowDraftVariable(Base):
  751. """`WorkflowDraftVariable` record variables and outputs generated during
  752. debugging workflow or chatflow.
  753. IMPORTANT: This model maintains multiple invariant rules that must be preserved.
  754. Do not instantiate this class directly with the constructor.
  755. Instead, use the factory methods (`new_conversation_variable`, `new_sys_variable`,
  756. `new_node_variable`) defined below to ensure all invariants are properly maintained.
  757. """
  758. @staticmethod
  759. def unique_app_id_node_id_name() -> list[str]:
  760. return [
  761. "app_id",
  762. "node_id",
  763. "name",
  764. ]
  765. __tablename__ = "workflow_draft_variables"
  766. __table_args__ = (UniqueConstraint(*unique_app_id_node_id_name()),)
  767. # Required for instance variable annotation.
  768. __allow_unmapped__ = True
  769. # id is the unique identifier of a draft variable.
  770. id: Mapped[str] = mapped_column(StringUUID, primary_key=True, server_default=sa.text("uuid_generate_v4()"))
  771. created_at: Mapped[datetime] = mapped_column(
  772. DateTime,
  773. nullable=False,
  774. default=_naive_utc_datetime,
  775. server_default=func.current_timestamp(),
  776. )
  777. updated_at: Mapped[datetime] = mapped_column(
  778. DateTime,
  779. nullable=False,
  780. default=_naive_utc_datetime,
  781. server_default=func.current_timestamp(),
  782. onupdate=func.current_timestamp(),
  783. )
  784. # "`app_id` maps to the `id` field in the `model.App` model."
  785. app_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
  786. # `last_edited_at` records when the value of a given draft variable
  787. # is edited.
  788. #
  789. # If it's not edited after creation, its value is `None`.
  790. last_edited_at: Mapped[datetime | None] = mapped_column(
  791. DateTime,
  792. nullable=True,
  793. default=None,
  794. )
  795. # The `node_id` field is special.
  796. #
  797. # If the variable is a conversation variable or a system variable, then the value of `node_id`
  798. # is `conversation` or `sys`, respective.
  799. #
  800. # Otherwise, if the variable is a variable belonging to a specific node, the value of `_node_id` is
  801. # the identity of correspond node in graph definition. An example of node id is `"1745769620734"`.
  802. #
  803. # However, there's one caveat. The id of the first "Answer" node in chatflow is "answer". (Other
  804. # "Answer" node conform the rules above.)
  805. node_id: Mapped[str] = mapped_column(sa.String(255), nullable=False, name="node_id")
  806. # From `VARIABLE_PATTERN`, we may conclude that the length of a top level variable is less than
  807. # 80 chars.
  808. #
  809. # ref: api/core/workflow/entities/variable_pool.py:18
  810. name: Mapped[str] = mapped_column(sa.String(255), nullable=False)
  811. description: Mapped[str] = mapped_column(
  812. sa.String(255),
  813. default="",
  814. nullable=False,
  815. )
  816. selector: Mapped[str] = mapped_column(sa.String(255), nullable=False, name="selector")
  817. # The data type of this variable's value
  818. value_type: Mapped[SegmentType] = mapped_column(EnumText(SegmentType, length=20))
  819. # The variable's value serialized as a JSON string
  820. value: Mapped[str] = mapped_column(sa.Text, nullable=False, name="value")
  821. # Controls whether the variable should be displayed in the variable inspection panel
  822. visible: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, default=True)
  823. # Determines whether this variable can be modified by users
  824. editable: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, default=False)
  825. # The `node_execution_id` field identifies the workflow node execution that created this variable.
  826. # It corresponds to the `id` field in the `WorkflowNodeExecutionModel` model.
  827. #
  828. # This field is not `None` for system variables and node variables, and is `None`
  829. # for conversation variables.
  830. node_execution_id: Mapped[str | None] = mapped_column(
  831. StringUUID,
  832. nullable=True,
  833. default=None,
  834. )
  835. # Cache for deserialized value
  836. #
  837. # NOTE(QuantumGhost): This field serves two purposes:
  838. #
  839. # 1. Caches deserialized values to reduce repeated parsing costs
  840. # 2. Allows modification of the deserialized value after retrieval,
  841. # particularly important for `File`` variables which require database
  842. # lookups to obtain storage_key and other metadata
  843. #
  844. # Use double underscore prefix for better encapsulation,
  845. # making this attribute harder to access from outside the class.
  846. __value: Segment | None
  847. def __init__(self, *args: Any, **kwargs: Any) -> None:
  848. """
  849. The constructor of `WorkflowDraftVariable` is not intended for
  850. direct use outside this file. Its solo purpose is setup private state
  851. used by the model instance.
  852. Please use the factory methods
  853. (`new_conversation_variable`, `new_sys_variable`, `new_node_variable`)
  854. defined below to create instances of this class.
  855. """
  856. super().__init__(*args, **kwargs)
  857. self.__value = None
  858. @orm.reconstructor
  859. def _init_on_load(self):
  860. self.__value = None
  861. def get_selector(self) -> list[str]:
  862. selector: Any = json.loads(self.selector)
  863. if not isinstance(selector, list):
  864. logger.error(
  865. "invalid selector loaded from database, type=%s, value=%s",
  866. type(selector).__name__,
  867. self.selector,
  868. )
  869. raise ValueError("invalid selector.")
  870. return cast(list[str], selector)
  871. def _set_selector(self, value: list[str]):
  872. self.selector = json.dumps(value)
  873. def _loads_value(self) -> Segment:
  874. value = json.loads(self.value)
  875. return self.build_segment_with_type(self.value_type, value)
  876. @staticmethod
  877. def rebuild_file_types(value: Any):
  878. # NOTE(QuantumGhost): Temporary workaround for structured data handling.
  879. # By this point, `output` has been converted to dict by
  880. # `WorkflowEntry.handle_special_values`, so we need to
  881. # reconstruct File objects from their serialized form
  882. # to maintain proper variable saving behavior.
  883. #
  884. # Ideally, we should work with structured data objects directly
  885. # rather than their serialized forms.
  886. # However, multiple components in the codebase depend on
  887. # `WorkflowEntry.handle_special_values`, making a comprehensive migration challenging.
  888. if isinstance(value, dict):
  889. if not maybe_file_object(value):
  890. return cast(Any, value)
  891. return File.model_validate(value)
  892. elif isinstance(value, list) and value:
  893. value_list = cast(list[Any], value)
  894. first: Any = value_list[0]
  895. if not maybe_file_object(first):
  896. return cast(Any, value)
  897. file_list: list[File] = [File.model_validate(cast(dict[str, Any], i)) for i in value_list]
  898. return cast(Any, file_list)
  899. else:
  900. return cast(Any, value)
  901. @classmethod
  902. def build_segment_with_type(cls, segment_type: SegmentType, value: Any) -> Segment:
  903. # Extends `variable_factory.build_segment_with_type` functionality by
  904. # reconstructing `FileSegment`` or `ArrayFileSegment`` objects from
  905. # their serialized dictionary or list representations, respectively.
  906. if segment_type == SegmentType.FILE:
  907. if isinstance(value, File):
  908. return build_segment_with_type(segment_type, value)
  909. elif isinstance(value, dict):
  910. file = cls.rebuild_file_types(value)
  911. return build_segment_with_type(segment_type, file)
  912. else:
  913. raise TypeMismatchError(f"expected dict or File for FileSegment, got {type(value)}")
  914. if segment_type == SegmentType.ARRAY_FILE:
  915. if not isinstance(value, list):
  916. raise TypeMismatchError(f"expected list for ArrayFileSegment, got {type(value)}")
  917. file_list = cls.rebuild_file_types(value)
  918. return build_segment_with_type(segment_type=segment_type, value=file_list)
  919. return build_segment_with_type(segment_type=segment_type, value=value)
  920. def get_value(self) -> Segment:
  921. """Decode the serialized value into its corresponding `Segment` object.
  922. This method caches the result, so repeated calls will return the same
  923. object instance without re-parsing the serialized data.
  924. If you need to modify the returned `Segment`, use `value.model_copy()`
  925. to create a copy first to avoid affecting the cached instance.
  926. For more information about the caching mechanism, see the documentation
  927. of the `__value` field.
  928. Returns:
  929. Segment: The deserialized value as a Segment object.
  930. """
  931. if self.__value is not None:
  932. return self.__value
  933. value = self._loads_value()
  934. self.__value = value
  935. return value
  936. def set_name(self, name: str):
  937. self.name = name
  938. self._set_selector([self.node_id, name])
  939. def set_value(self, value: Segment):
  940. """Updates the `value` and corresponding `value_type` fields in the database model.
  941. This method also stores the provided Segment object in the deserialized cache
  942. without creating a copy, allowing for efficient value access.
  943. Args:
  944. value: The Segment object to store as the variable's value.
  945. """
  946. self.__value = value
  947. self.value = variable_utils.dumps_with_segments(value)
  948. self.value_type = value.value_type
  949. def get_node_id(self) -> str | None:
  950. if self.get_variable_type() == DraftVariableType.NODE:
  951. return self.node_id
  952. else:
  953. return None
  954. def get_variable_type(self) -> DraftVariableType:
  955. match self.node_id:
  956. case DraftVariableType.CONVERSATION:
  957. return DraftVariableType.CONVERSATION
  958. case DraftVariableType.SYS:
  959. return DraftVariableType.SYS
  960. case _:
  961. return DraftVariableType.NODE
  962. @classmethod
  963. def _new(
  964. cls,
  965. *,
  966. app_id: str,
  967. node_id: str,
  968. name: str,
  969. value: Segment,
  970. node_execution_id: str | None,
  971. description: str = "",
  972. ) -> "WorkflowDraftVariable":
  973. variable = WorkflowDraftVariable()
  974. variable.created_at = _naive_utc_datetime()
  975. variable.updated_at = _naive_utc_datetime()
  976. variable.description = description
  977. variable.app_id = app_id
  978. variable.node_id = node_id
  979. variable.name = name
  980. variable.set_value(value)
  981. variable._set_selector(list(variable_utils.to_selector(node_id, name)))
  982. variable.node_execution_id = node_execution_id
  983. return variable
  984. @classmethod
  985. def new_conversation_variable(
  986. cls,
  987. *,
  988. app_id: str,
  989. name: str,
  990. value: Segment,
  991. description: str = "",
  992. ) -> "WorkflowDraftVariable":
  993. variable = cls._new(
  994. app_id=app_id,
  995. node_id=CONVERSATION_VARIABLE_NODE_ID,
  996. name=name,
  997. value=value,
  998. description=description,
  999. node_execution_id=None,
  1000. )
  1001. variable.editable = True
  1002. return variable
  1003. @classmethod
  1004. def new_sys_variable(
  1005. cls,
  1006. *,
  1007. app_id: str,
  1008. name: str,
  1009. value: Segment,
  1010. node_execution_id: str,
  1011. editable: bool = False,
  1012. ) -> "WorkflowDraftVariable":
  1013. variable = cls._new(
  1014. app_id=app_id,
  1015. node_id=SYSTEM_VARIABLE_NODE_ID,
  1016. name=name,
  1017. node_execution_id=node_execution_id,
  1018. value=value,
  1019. )
  1020. variable.editable = editable
  1021. return variable
  1022. @classmethod
  1023. def new_node_variable(
  1024. cls,
  1025. *,
  1026. app_id: str,
  1027. node_id: str,
  1028. name: str,
  1029. value: Segment,
  1030. node_execution_id: str,
  1031. visible: bool = True,
  1032. editable: bool = True,
  1033. ) -> "WorkflowDraftVariable":
  1034. variable = cls._new(
  1035. app_id=app_id,
  1036. node_id=node_id,
  1037. name=name,
  1038. node_execution_id=node_execution_id,
  1039. value=value,
  1040. )
  1041. variable.visible = visible
  1042. variable.editable = editable
  1043. return variable
  1044. @property
  1045. def edited(self):
  1046. return self.last_edited_at is not None
  1047. def is_system_variable_editable(name: str) -> bool:
  1048. return name in _EDITABLE_SYSTEM_VARIABLE