chore(api/models): apply ruff reformatting (#7600)
Co-authored-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
@@ -22,11 +22,12 @@ class CreatedByRole(Enum):
|
||||
"""
|
||||
Created By Role Enum
|
||||
"""
|
||||
ACCOUNT = 'account'
|
||||
END_USER = 'end_user'
|
||||
|
||||
ACCOUNT = "account"
|
||||
END_USER = "end_user"
|
||||
|
||||
@classmethod
|
||||
def value_of(cls, value: str) -> 'CreatedByRole':
|
||||
def value_of(cls, value: str) -> "CreatedByRole":
|
||||
"""
|
||||
Get value of given mode.
|
||||
|
||||
@@ -36,18 +37,19 @@ class CreatedByRole(Enum):
|
||||
for mode in cls:
|
||||
if mode.value == value:
|
||||
return mode
|
||||
raise ValueError(f'invalid created by role value {value}')
|
||||
raise ValueError(f"invalid created by role value {value}")
|
||||
|
||||
|
||||
class WorkflowType(Enum):
|
||||
"""
|
||||
Workflow Type Enum
|
||||
"""
|
||||
WORKFLOW = 'workflow'
|
||||
CHAT = 'chat'
|
||||
|
||||
WORKFLOW = "workflow"
|
||||
CHAT = "chat"
|
||||
|
||||
@classmethod
|
||||
def value_of(cls, value: str) -> 'WorkflowType':
|
||||
def value_of(cls, value: str) -> "WorkflowType":
|
||||
"""
|
||||
Get value of given mode.
|
||||
|
||||
@@ -57,10 +59,10 @@ class WorkflowType(Enum):
|
||||
for mode in cls:
|
||||
if mode.value == value:
|
||||
return mode
|
||||
raise ValueError(f'invalid workflow type value {value}')
|
||||
raise ValueError(f"invalid workflow type value {value}")
|
||||
|
||||
@classmethod
|
||||
def from_app_mode(cls, app_mode: Union[str, 'AppMode']) -> 'WorkflowType':
|
||||
def from_app_mode(cls, app_mode: Union[str, "AppMode"]) -> "WorkflowType":
|
||||
"""
|
||||
Get workflow type from app mode.
|
||||
|
||||
@@ -68,6 +70,7 @@ class WorkflowType(Enum):
|
||||
:return: workflow type
|
||||
"""
|
||||
from models.model import AppMode
|
||||
|
||||
app_mode = app_mode if isinstance(app_mode, AppMode) else AppMode.value_of(app_mode)
|
||||
return cls.WORKFLOW if app_mode == AppMode.WORKFLOW else cls.CHAT
|
||||
|
||||
@@ -105,13 +108,13 @@ class Workflow(db.Model):
|
||||
- updated_at (timestamp) `optional` Last update time
|
||||
"""
|
||||
|
||||
__tablename__ = 'workflows'
|
||||
__tablename__ = "workflows"
|
||||
__table_args__ = (
|
||||
db.PrimaryKeyConstraint('id', name='workflow_pkey'),
|
||||
db.Index('workflow_version_idx', 'tenant_id', 'app_id', 'version'),
|
||||
db.PrimaryKeyConstraint("id", name="workflow_pkey"),
|
||||
db.Index("workflow_version_idx", "tenant_id", "app_id", "version"),
|
||||
)
|
||||
|
||||
id: Mapped[str] = db.Column(StringUUID, server_default=db.text('uuid_generate_v4()'))
|
||||
id: Mapped[str] = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
||||
tenant_id: Mapped[str] = db.Column(StringUUID, nullable=False)
|
||||
app_id: Mapped[str] = db.Column(StringUUID, nullable=False)
|
||||
type: Mapped[str] = db.Column(db.String(255), nullable=False)
|
||||
@@ -119,15 +122,31 @@ class Workflow(db.Model):
|
||||
graph: Mapped[str] = db.Column(db.Text)
|
||||
features: Mapped[str] = db.Column(db.Text)
|
||||
created_by: Mapped[str] = db.Column(StringUUID, nullable=False)
|
||||
created_at: Mapped[datetime] = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
|
||||
created_at: Mapped[datetime] = db.Column(
|
||||
db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)")
|
||||
)
|
||||
updated_by: Mapped[str] = db.Column(StringUUID)
|
||||
updated_at: Mapped[datetime] = db.Column(db.DateTime)
|
||||
_environment_variables: Mapped[str] = db.Column('environment_variables', db.Text, nullable=False, server_default='{}')
|
||||
_conversation_variables: Mapped[str] = db.Column('conversation_variables', db.Text, nullable=False, server_default='{}')
|
||||
_environment_variables: Mapped[str] = db.Column(
|
||||
"environment_variables", db.Text, nullable=False, server_default="{}"
|
||||
)
|
||||
_conversation_variables: Mapped[str] = db.Column(
|
||||
"conversation_variables", db.Text, nullable=False, server_default="{}"
|
||||
)
|
||||
|
||||
def __init__(self, *, tenant_id: str, app_id: str, type: str, version: str, graph: str,
|
||||
features: str, created_by: str, environment_variables: Sequence[Variable],
|
||||
conversation_variables: Sequence[Variable]):
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
tenant_id: str,
|
||||
app_id: str,
|
||||
type: str,
|
||||
version: str,
|
||||
graph: str,
|
||||
features: str,
|
||||
created_by: str,
|
||||
environment_variables: Sequence[Variable],
|
||||
conversation_variables: Sequence[Variable],
|
||||
):
|
||||
self.tenant_id = tenant_id
|
||||
self.app_id = app_id
|
||||
self.type = type
|
||||
@@ -160,22 +179,20 @@ class Workflow(db.Model):
|
||||
return []
|
||||
|
||||
graph_dict = self.graph_dict
|
||||
if 'nodes' not in graph_dict:
|
||||
if "nodes" not in graph_dict:
|
||||
return []
|
||||
|
||||
start_node = next((node for node in graph_dict['nodes'] if node['data']['type'] == 'start'), None)
|
||||
start_node = next((node for node in graph_dict["nodes"] if node["data"]["type"] == "start"), None)
|
||||
if not start_node:
|
||||
return []
|
||||
|
||||
# get user_input_form from start node
|
||||
variables = start_node.get('data', {}).get('variables', [])
|
||||
variables = start_node.get("data", {}).get("variables", [])
|
||||
|
||||
if to_old_structure:
|
||||
old_structure_variables = []
|
||||
for variable in variables:
|
||||
old_structure_variables.append({
|
||||
variable['type']: variable
|
||||
})
|
||||
old_structure_variables.append({variable["type"]: variable})
|
||||
|
||||
return old_structure_variables
|
||||
|
||||
@@ -188,25 +205,24 @@ class Workflow(db.Model):
|
||||
|
||||
:return: hash
|
||||
"""
|
||||
entity = {
|
||||
'graph': self.graph_dict,
|
||||
'features': self.features_dict
|
||||
}
|
||||
entity = {"graph": self.graph_dict, "features": self.features_dict}
|
||||
|
||||
return helper.generate_text_hash(json.dumps(entity, sort_keys=True))
|
||||
|
||||
@property
|
||||
def tool_published(self) -> bool:
|
||||
from models.tools import WorkflowToolProvider
|
||||
return db.session.query(WorkflowToolProvider).filter(
|
||||
WorkflowToolProvider.app_id == self.app_id
|
||||
).first() is not None
|
||||
|
||||
return (
|
||||
db.session.query(WorkflowToolProvider).filter(WorkflowToolProvider.app_id == self.app_id).first()
|
||||
is not None
|
||||
)
|
||||
|
||||
@property
|
||||
def environment_variables(self) -> Sequence[Variable]:
|
||||
# TODO: find some way to init `self._environment_variables` when instance created.
|
||||
if self._environment_variables is None:
|
||||
self._environment_variables = '{}'
|
||||
self._environment_variables = "{}"
|
||||
|
||||
tenant_id = contexts.tenant_id.get()
|
||||
|
||||
@@ -215,9 +231,7 @@ class Workflow(db.Model):
|
||||
|
||||
# decrypt secret variables value
|
||||
decrypt_func = (
|
||||
lambda var: var.model_copy(
|
||||
update={'value': encrypter.decrypt_token(tenant_id=tenant_id, token=var.value)}
|
||||
)
|
||||
lambda var: var.model_copy(update={"value": encrypter.decrypt_token(tenant_id=tenant_id, token=var.value)})
|
||||
if isinstance(var, SecretVariable)
|
||||
else var
|
||||
)
|
||||
@@ -230,19 +244,17 @@ class Workflow(db.Model):
|
||||
|
||||
value = list(value)
|
||||
if any(var for var in value if not var.id):
|
||||
raise ValueError('environment variable require a unique id')
|
||||
raise ValueError("environment variable require a unique id")
|
||||
|
||||
# Compare inputs and origin variables, if the value is HIDDEN_VALUE, use the origin variable value (only update `name`).
|
||||
origin_variables_dictionary = {var.id: var for var in self.environment_variables}
|
||||
for i, variable in enumerate(value):
|
||||
if variable.id in origin_variables_dictionary and variable.value == HIDDEN_VALUE:
|
||||
value[i] = origin_variables_dictionary[variable.id].model_copy(update={'name': variable.name})
|
||||
value[i] = origin_variables_dictionary[variable.id].model_copy(update={"name": variable.name})
|
||||
|
||||
# encrypt secret variables value
|
||||
encrypt_func = (
|
||||
lambda var: var.model_copy(
|
||||
update={'value': encrypter.encrypt_token(tenant_id=tenant_id, token=var.value)}
|
||||
)
|
||||
lambda var: var.model_copy(update={"value": encrypter.encrypt_token(tenant_id=tenant_id, token=var.value)})
|
||||
if isinstance(var, SecretVariable)
|
||||
else var
|
||||
)
|
||||
@@ -256,15 +268,15 @@ class Workflow(db.Model):
|
||||
def to_dict(self, *, include_secret: bool = False) -> Mapping[str, Any]:
|
||||
environment_variables = list(self.environment_variables)
|
||||
environment_variables = [
|
||||
v if not isinstance(v, SecretVariable) or include_secret else v.model_copy(update={'value': ''})
|
||||
v if not isinstance(v, SecretVariable) or include_secret else v.model_copy(update={"value": ""})
|
||||
for v in environment_variables
|
||||
]
|
||||
|
||||
result = {
|
||||
'graph': self.graph_dict,
|
||||
'features': self.features_dict,
|
||||
'environment_variables': [var.model_dump(mode='json') for var in environment_variables],
|
||||
'conversation_variables': [var.model_dump(mode='json') for var in self.conversation_variables],
|
||||
"graph": self.graph_dict,
|
||||
"features": self.features_dict,
|
||||
"environment_variables": [var.model_dump(mode="json") for var in environment_variables],
|
||||
"conversation_variables": [var.model_dump(mode="json") for var in self.conversation_variables],
|
||||
}
|
||||
return result
|
||||
|
||||
@@ -272,7 +284,7 @@ class Workflow(db.Model):
|
||||
def conversation_variables(self) -> Sequence[Variable]:
|
||||
# TODO: find some way to init `self._conversation_variables` when instance created.
|
||||
if self._conversation_variables is None:
|
||||
self._conversation_variables = '{}'
|
||||
self._conversation_variables = "{}"
|
||||
|
||||
variables_dict: dict[str, Any] = json.loads(self._conversation_variables)
|
||||
results = [factory.build_variable_from_mapping(v) for v in variables_dict.values()]
|
||||
@@ -290,11 +302,12 @@ class WorkflowRunTriggeredFrom(Enum):
|
||||
"""
|
||||
Workflow Run Triggered From Enum
|
||||
"""
|
||||
DEBUGGING = 'debugging'
|
||||
APP_RUN = 'app-run'
|
||||
|
||||
DEBUGGING = "debugging"
|
||||
APP_RUN = "app-run"
|
||||
|
||||
@classmethod
|
||||
def value_of(cls, value: str) -> 'WorkflowRunTriggeredFrom':
|
||||
def value_of(cls, value: str) -> "WorkflowRunTriggeredFrom":
|
||||
"""
|
||||
Get value of given mode.
|
||||
|
||||
@@ -304,20 +317,21 @@ class WorkflowRunTriggeredFrom(Enum):
|
||||
for mode in cls:
|
||||
if mode.value == value:
|
||||
return mode
|
||||
raise ValueError(f'invalid workflow run triggered from value {value}')
|
||||
raise ValueError(f"invalid workflow run triggered from value {value}")
|
||||
|
||||
|
||||
class WorkflowRunStatus(Enum):
|
||||
"""
|
||||
Workflow Run Status Enum
|
||||
"""
|
||||
RUNNING = 'running'
|
||||
SUCCEEDED = 'succeeded'
|
||||
FAILED = 'failed'
|
||||
STOPPED = 'stopped'
|
||||
|
||||
RUNNING = "running"
|
||||
SUCCEEDED = "succeeded"
|
||||
FAILED = "failed"
|
||||
STOPPED = "stopped"
|
||||
|
||||
@classmethod
|
||||
def value_of(cls, value: str) -> 'WorkflowRunStatus':
|
||||
def value_of(cls, value: str) -> "WorkflowRunStatus":
|
||||
"""
|
||||
Get value of given mode.
|
||||
|
||||
@@ -327,7 +341,7 @@ class WorkflowRunStatus(Enum):
|
||||
for mode in cls:
|
||||
if mode.value == value:
|
||||
return mode
|
||||
raise ValueError(f'invalid workflow run status value {value}')
|
||||
raise ValueError(f"invalid workflow run status value {value}")
|
||||
|
||||
|
||||
class WorkflowRun(db.Model):
|
||||
@@ -368,14 +382,14 @@ class WorkflowRun(db.Model):
|
||||
- finished_at (timestamp) End time
|
||||
"""
|
||||
|
||||
__tablename__ = 'workflow_runs'
|
||||
__tablename__ = "workflow_runs"
|
||||
__table_args__ = (
|
||||
db.PrimaryKeyConstraint('id', name='workflow_run_pkey'),
|
||||
db.Index('workflow_run_triggerd_from_idx', 'tenant_id', 'app_id', 'triggered_from'),
|
||||
db.Index('workflow_run_tenant_app_sequence_idx', 'tenant_id', 'app_id', 'sequence_number'),
|
||||
db.PrimaryKeyConstraint("id", name="workflow_run_pkey"),
|
||||
db.Index("workflow_run_triggerd_from_idx", "tenant_id", "app_id", "triggered_from"),
|
||||
db.Index("workflow_run_tenant_app_sequence_idx", "tenant_id", "app_id", "sequence_number"),
|
||||
)
|
||||
|
||||
id = db.Column(StringUUID, server_default=db.text('uuid_generate_v4()'))
|
||||
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
||||
tenant_id = db.Column(StringUUID, nullable=False)
|
||||
app_id = db.Column(StringUUID, nullable=False)
|
||||
sequence_number = db.Column(db.Integer, nullable=False)
|
||||
@@ -388,26 +402,25 @@ class WorkflowRun(db.Model):
|
||||
status = db.Column(db.String(255), nullable=False)
|
||||
outputs = db.Column(db.Text)
|
||||
error = db.Column(db.Text)
|
||||
elapsed_time = db.Column(db.Float, nullable=False, server_default=db.text('0'))
|
||||
total_tokens = db.Column(db.Integer, nullable=False, server_default=db.text('0'))
|
||||
total_steps = db.Column(db.Integer, server_default=db.text('0'))
|
||||
elapsed_time = db.Column(db.Float, nullable=False, server_default=db.text("0"))
|
||||
total_tokens = db.Column(db.Integer, nullable=False, server_default=db.text("0"))
|
||||
total_steps = db.Column(db.Integer, server_default=db.text("0"))
|
||||
created_by_role = db.Column(db.String(255), nullable=False)
|
||||
created_by = db.Column(StringUUID, nullable=False)
|
||||
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
|
||||
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
||||
finished_at = db.Column(db.DateTime)
|
||||
|
||||
@property
|
||||
def created_by_account(self):
|
||||
created_by_role = CreatedByRole.value_of(self.created_by_role)
|
||||
return db.session.get(Account, self.created_by) \
|
||||
if created_by_role == CreatedByRole.ACCOUNT else None
|
||||
return db.session.get(Account, self.created_by) if created_by_role == CreatedByRole.ACCOUNT else None
|
||||
|
||||
@property
|
||||
def created_by_end_user(self):
|
||||
from models.model import EndUser
|
||||
|
||||
created_by_role = CreatedByRole.value_of(self.created_by_role)
|
||||
return db.session.get(EndUser, self.created_by) \
|
||||
if created_by_role == CreatedByRole.END_USER else None
|
||||
return db.session.get(EndUser, self.created_by) if created_by_role == CreatedByRole.END_USER else None
|
||||
|
||||
@property
|
||||
def graph_dict(self):
|
||||
@@ -422,12 +435,12 @@ class WorkflowRun(db.Model):
|
||||
return json.loads(self.outputs) if self.outputs else None
|
||||
|
||||
@property
|
||||
def message(self) -> Optional['Message']:
|
||||
def message(self) -> Optional["Message"]:
|
||||
from models.model import Message
|
||||
return db.session.query(Message).filter(
|
||||
Message.app_id == self.app_id,
|
||||
Message.workflow_run_id == self.id
|
||||
).first()
|
||||
|
||||
return (
|
||||
db.session.query(Message).filter(Message.app_id == self.app_id, Message.workflow_run_id == self.id).first()
|
||||
)
|
||||
|
||||
@property
|
||||
def workflow(self):
|
||||
@@ -435,51 +448,51 @@ class WorkflowRun(db.Model):
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'tenant_id': self.tenant_id,
|
||||
'app_id': self.app_id,
|
||||
'sequence_number': self.sequence_number,
|
||||
'workflow_id': self.workflow_id,
|
||||
'type': self.type,
|
||||
'triggered_from': self.triggered_from,
|
||||
'version': self.version,
|
||||
'graph': self.graph_dict,
|
||||
'inputs': self.inputs_dict,
|
||||
'status': self.status,
|
||||
'outputs': self.outputs_dict,
|
||||
'error': self.error,
|
||||
'elapsed_time': self.elapsed_time,
|
||||
'total_tokens': self.total_tokens,
|
||||
'total_steps': self.total_steps,
|
||||
'created_by_role': self.created_by_role,
|
||||
'created_by': self.created_by,
|
||||
'created_at': self.created_at,
|
||||
'finished_at': self.finished_at,
|
||||
"id": self.id,
|
||||
"tenant_id": self.tenant_id,
|
||||
"app_id": self.app_id,
|
||||
"sequence_number": self.sequence_number,
|
||||
"workflow_id": self.workflow_id,
|
||||
"type": self.type,
|
||||
"triggered_from": self.triggered_from,
|
||||
"version": self.version,
|
||||
"graph": self.graph_dict,
|
||||
"inputs": self.inputs_dict,
|
||||
"status": self.status,
|
||||
"outputs": self.outputs_dict,
|
||||
"error": self.error,
|
||||
"elapsed_time": self.elapsed_time,
|
||||
"total_tokens": self.total_tokens,
|
||||
"total_steps": self.total_steps,
|
||||
"created_by_role": self.created_by_role,
|
||||
"created_by": self.created_by,
|
||||
"created_at": self.created_at,
|
||||
"finished_at": self.finished_at,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict) -> 'WorkflowRun':
|
||||
def from_dict(cls, data: dict) -> "WorkflowRun":
|
||||
return cls(
|
||||
id=data.get('id'),
|
||||
tenant_id=data.get('tenant_id'),
|
||||
app_id=data.get('app_id'),
|
||||
sequence_number=data.get('sequence_number'),
|
||||
workflow_id=data.get('workflow_id'),
|
||||
type=data.get('type'),
|
||||
triggered_from=data.get('triggered_from'),
|
||||
version=data.get('version'),
|
||||
graph=json.dumps(data.get('graph')),
|
||||
inputs=json.dumps(data.get('inputs')),
|
||||
status=data.get('status'),
|
||||
outputs=json.dumps(data.get('outputs')),
|
||||
error=data.get('error'),
|
||||
elapsed_time=data.get('elapsed_time'),
|
||||
total_tokens=data.get('total_tokens'),
|
||||
total_steps=data.get('total_steps'),
|
||||
created_by_role=data.get('created_by_role'),
|
||||
created_by=data.get('created_by'),
|
||||
created_at=data.get('created_at'),
|
||||
finished_at=data.get('finished_at'),
|
||||
id=data.get("id"),
|
||||
tenant_id=data.get("tenant_id"),
|
||||
app_id=data.get("app_id"),
|
||||
sequence_number=data.get("sequence_number"),
|
||||
workflow_id=data.get("workflow_id"),
|
||||
type=data.get("type"),
|
||||
triggered_from=data.get("triggered_from"),
|
||||
version=data.get("version"),
|
||||
graph=json.dumps(data.get("graph")),
|
||||
inputs=json.dumps(data.get("inputs")),
|
||||
status=data.get("status"),
|
||||
outputs=json.dumps(data.get("outputs")),
|
||||
error=data.get("error"),
|
||||
elapsed_time=data.get("elapsed_time"),
|
||||
total_tokens=data.get("total_tokens"),
|
||||
total_steps=data.get("total_steps"),
|
||||
created_by_role=data.get("created_by_role"),
|
||||
created_by=data.get("created_by"),
|
||||
created_at=data.get("created_at"),
|
||||
finished_at=data.get("finished_at"),
|
||||
)
|
||||
|
||||
|
||||
@@ -487,11 +500,12 @@ class WorkflowNodeExecutionTriggeredFrom(Enum):
|
||||
"""
|
||||
Workflow Node Execution Triggered From Enum
|
||||
"""
|
||||
SINGLE_STEP = 'single-step'
|
||||
WORKFLOW_RUN = 'workflow-run'
|
||||
|
||||
SINGLE_STEP = "single-step"
|
||||
WORKFLOW_RUN = "workflow-run"
|
||||
|
||||
@classmethod
|
||||
def value_of(cls, value: str) -> 'WorkflowNodeExecutionTriggeredFrom':
|
||||
def value_of(cls, value: str) -> "WorkflowNodeExecutionTriggeredFrom":
|
||||
"""
|
||||
Get value of given mode.
|
||||
|
||||
@@ -501,19 +515,20 @@ class WorkflowNodeExecutionTriggeredFrom(Enum):
|
||||
for mode in cls:
|
||||
if mode.value == value:
|
||||
return mode
|
||||
raise ValueError(f'invalid workflow node execution triggered from value {value}')
|
||||
raise ValueError(f"invalid workflow node execution triggered from value {value}")
|
||||
|
||||
|
||||
class WorkflowNodeExecutionStatus(Enum):
|
||||
"""
|
||||
Workflow Node Execution Status Enum
|
||||
"""
|
||||
RUNNING = 'running'
|
||||
SUCCEEDED = 'succeeded'
|
||||
FAILED = 'failed'
|
||||
|
||||
RUNNING = "running"
|
||||
SUCCEEDED = "succeeded"
|
||||
FAILED = "failed"
|
||||
|
||||
@classmethod
|
||||
def value_of(cls, value: str) -> 'WorkflowNodeExecutionStatus':
|
||||
def value_of(cls, value: str) -> "WorkflowNodeExecutionStatus":
|
||||
"""
|
||||
Get value of given mode.
|
||||
|
||||
@@ -523,7 +538,7 @@ class WorkflowNodeExecutionStatus(Enum):
|
||||
for mode in cls:
|
||||
if mode.value == value:
|
||||
return mode
|
||||
raise ValueError(f'invalid workflow node execution status value {value}')
|
||||
raise ValueError(f"invalid workflow node execution status value {value}")
|
||||
|
||||
|
||||
class WorkflowNodeExecution(db.Model):
|
||||
@@ -574,18 +589,31 @@ class WorkflowNodeExecution(db.Model):
|
||||
- finished_at (timestamp) End time
|
||||
"""
|
||||
|
||||
__tablename__ = 'workflow_node_executions'
|
||||
__tablename__ = "workflow_node_executions"
|
||||
__table_args__ = (
|
||||
db.PrimaryKeyConstraint('id', name='workflow_node_execution_pkey'),
|
||||
db.Index('workflow_node_execution_workflow_run_idx', 'tenant_id', 'app_id', 'workflow_id',
|
||||
'triggered_from', 'workflow_run_id'),
|
||||
db.Index('workflow_node_execution_node_run_idx', 'tenant_id', 'app_id', 'workflow_id',
|
||||
'triggered_from', 'node_id'),
|
||||
db.Index('workflow_node_execution_id_idx', 'tenant_id', 'app_id', 'workflow_id',
|
||||
'triggered_from', 'node_execution_id'),
|
||||
db.PrimaryKeyConstraint("id", name="workflow_node_execution_pkey"),
|
||||
db.Index(
|
||||
"workflow_node_execution_workflow_run_idx",
|
||||
"tenant_id",
|
||||
"app_id",
|
||||
"workflow_id",
|
||||
"triggered_from",
|
||||
"workflow_run_id",
|
||||
),
|
||||
db.Index(
|
||||
"workflow_node_execution_node_run_idx", "tenant_id", "app_id", "workflow_id", "triggered_from", "node_id"
|
||||
),
|
||||
db.Index(
|
||||
"workflow_node_execution_id_idx",
|
||||
"tenant_id",
|
||||
"app_id",
|
||||
"workflow_id",
|
||||
"triggered_from",
|
||||
"node_execution_id",
|
||||
),
|
||||
)
|
||||
|
||||
id = db.Column(StringUUID, server_default=db.text('uuid_generate_v4()'))
|
||||
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
||||
tenant_id = db.Column(StringUUID, nullable=False)
|
||||
app_id = db.Column(StringUUID, nullable=False)
|
||||
workflow_id = db.Column(StringUUID, nullable=False)
|
||||
@@ -602,9 +630,9 @@ class WorkflowNodeExecution(db.Model):
|
||||
outputs = db.Column(db.Text)
|
||||
status = db.Column(db.String(255), nullable=False)
|
||||
error = db.Column(db.Text)
|
||||
elapsed_time = db.Column(db.Float, nullable=False, server_default=db.text('0'))
|
||||
elapsed_time = db.Column(db.Float, nullable=False, server_default=db.text("0"))
|
||||
execution_metadata = db.Column(db.Text)
|
||||
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
|
||||
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
||||
created_by_role = db.Column(db.String(255), nullable=False)
|
||||
created_by = db.Column(StringUUID, nullable=False)
|
||||
finished_at = db.Column(db.DateTime)
|
||||
@@ -612,15 +640,14 @@ class WorkflowNodeExecution(db.Model):
|
||||
@property
|
||||
def created_by_account(self):
|
||||
created_by_role = CreatedByRole.value_of(self.created_by_role)
|
||||
return db.session.get(Account, self.created_by) \
|
||||
if created_by_role == CreatedByRole.ACCOUNT else None
|
||||
return db.session.get(Account, self.created_by) if created_by_role == CreatedByRole.ACCOUNT else None
|
||||
|
||||
@property
|
||||
def created_by_end_user(self):
|
||||
from models.model import EndUser
|
||||
|
||||
created_by_role = CreatedByRole.value_of(self.created_by_role)
|
||||
return db.session.get(EndUser, self.created_by) \
|
||||
if created_by_role == CreatedByRole.END_USER else None
|
||||
return db.session.get(EndUser, self.created_by) if created_by_role == CreatedByRole.END_USER else None
|
||||
|
||||
@property
|
||||
def inputs_dict(self):
|
||||
@@ -641,15 +668,17 @@ class WorkflowNodeExecution(db.Model):
|
||||
@property
|
||||
def extras(self):
|
||||
from core.tools.tool_manager import ToolManager
|
||||
|
||||
extras = {}
|
||||
if self.execution_metadata_dict:
|
||||
from core.workflow.entities.node_entities import NodeType
|
||||
if self.node_type == NodeType.TOOL.value and 'tool_info' in self.execution_metadata_dict:
|
||||
tool_info = self.execution_metadata_dict['tool_info']
|
||||
extras['icon'] = ToolManager.get_tool_icon(
|
||||
|
||||
if self.node_type == NodeType.TOOL.value and "tool_info" in self.execution_metadata_dict:
|
||||
tool_info = self.execution_metadata_dict["tool_info"]
|
||||
extras["icon"] = ToolManager.get_tool_icon(
|
||||
tenant_id=self.tenant_id,
|
||||
provider_type=tool_info['provider_type'],
|
||||
provider_id=tool_info['provider_id']
|
||||
provider_type=tool_info["provider_type"],
|
||||
provider_id=tool_info["provider_id"],
|
||||
)
|
||||
|
||||
return extras
|
||||
@@ -659,12 +688,13 @@ class WorkflowAppLogCreatedFrom(Enum):
|
||||
"""
|
||||
Workflow App Log Created From Enum
|
||||
"""
|
||||
SERVICE_API = 'service-api'
|
||||
WEB_APP = 'web-app'
|
||||
INSTALLED_APP = 'installed-app'
|
||||
|
||||
SERVICE_API = "service-api"
|
||||
WEB_APP = "web-app"
|
||||
INSTALLED_APP = "installed-app"
|
||||
|
||||
@classmethod
|
||||
def value_of(cls, value: str) -> 'WorkflowAppLogCreatedFrom':
|
||||
def value_of(cls, value: str) -> "WorkflowAppLogCreatedFrom":
|
||||
"""
|
||||
Get value of given mode.
|
||||
|
||||
@@ -674,7 +704,7 @@ class WorkflowAppLogCreatedFrom(Enum):
|
||||
for mode in cls:
|
||||
if mode.value == value:
|
||||
return mode
|
||||
raise ValueError(f'invalid workflow app log created from value {value}')
|
||||
raise ValueError(f"invalid workflow app log created from value {value}")
|
||||
|
||||
|
||||
class WorkflowAppLog(db.Model):
|
||||
@@ -706,13 +736,13 @@ class WorkflowAppLog(db.Model):
|
||||
- created_at (timestamp) Creation time
|
||||
"""
|
||||
|
||||
__tablename__ = 'workflow_app_logs'
|
||||
__tablename__ = "workflow_app_logs"
|
||||
__table_args__ = (
|
||||
db.PrimaryKeyConstraint('id', name='workflow_app_log_pkey'),
|
||||
db.Index('workflow_app_log_app_idx', 'tenant_id', 'app_id'),
|
||||
db.PrimaryKeyConstraint("id", name="workflow_app_log_pkey"),
|
||||
db.Index("workflow_app_log_app_idx", "tenant_id", "app_id"),
|
||||
)
|
||||
|
||||
id = db.Column(StringUUID, server_default=db.text('uuid_generate_v4()'))
|
||||
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
||||
tenant_id = db.Column(StringUUID, nullable=False)
|
||||
app_id = db.Column(StringUUID, nullable=False)
|
||||
workflow_id = db.Column(StringUUID, nullable=False)
|
||||
@@ -720,7 +750,7 @@ class WorkflowAppLog(db.Model):
|
||||
created_from = db.Column(db.String(255), nullable=False)
|
||||
created_by_role = db.Column(db.String(255), nullable=False)
|
||||
created_by = db.Column(StringUUID, nullable=False)
|
||||
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
|
||||
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
||||
|
||||
@property
|
||||
def workflow_run(self):
|
||||
@@ -729,26 +759,27 @@ class WorkflowAppLog(db.Model):
|
||||
@property
|
||||
def created_by_account(self):
|
||||
created_by_role = CreatedByRole.value_of(self.created_by_role)
|
||||
return db.session.get(Account, self.created_by) \
|
||||
if created_by_role == CreatedByRole.ACCOUNT else None
|
||||
return db.session.get(Account, self.created_by) if created_by_role == CreatedByRole.ACCOUNT else None
|
||||
|
||||
@property
|
||||
def created_by_end_user(self):
|
||||
from models.model import EndUser
|
||||
|
||||
created_by_role = CreatedByRole.value_of(self.created_by_role)
|
||||
return db.session.get(EndUser, self.created_by) \
|
||||
if created_by_role == CreatedByRole.END_USER else None
|
||||
return db.session.get(EndUser, self.created_by) if created_by_role == CreatedByRole.END_USER else None
|
||||
|
||||
|
||||
class ConversationVariable(db.Model):
|
||||
__tablename__ = 'workflow_conversation_variables'
|
||||
__tablename__ = "workflow_conversation_variables"
|
||||
|
||||
id: Mapped[str] = db.Column(StringUUID, primary_key=True)
|
||||
conversation_id: Mapped[str] = db.Column(StringUUID, nullable=False, primary_key=True)
|
||||
app_id: Mapped[str] = db.Column(StringUUID, nullable=False, index=True)
|
||||
data = db.Column(db.Text, nullable=False)
|
||||
created_at = db.Column(db.DateTime, nullable=False, index=True, server_default=db.text('CURRENT_TIMESTAMP(0)'))
|
||||
updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp())
|
||||
created_at = db.Column(db.DateTime, nullable=False, index=True, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
||||
updated_at = db.Column(
|
||||
db.DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp()
|
||||
)
|
||||
|
||||
def __init__(self, *, id: str, app_id: str, conversation_id: str, data: str) -> None:
|
||||
self.id = id
|
||||
@@ -757,7 +788,7 @@ class ConversationVariable(db.Model):
|
||||
self.data = data
|
||||
|
||||
@classmethod
|
||||
def from_variable(cls, *, app_id: str, conversation_id: str, variable: Variable) -> 'ConversationVariable':
|
||||
def from_variable(cls, *, app_id: str, conversation_id: str, variable: Variable) -> "ConversationVariable":
|
||||
obj = cls(
|
||||
id=variable.id,
|
||||
app_id=app_id,
|
||||
|
Reference in New Issue
Block a user