|
|
|
|
|
|
|
|
from datetime import UTC, datetime |
|
|
from datetime import UTC, datetime |
|
|
from typing import Any, Optional, Union, cast |
|
|
from typing import Any, Optional, Union, cast |
|
|
|
|
|
|
|
|
from sqlalchemy import select |
|
|
|
|
|
from sqlalchemy.orm import Session |
|
|
from sqlalchemy.orm import Session |
|
|
|
|
|
|
|
|
from core.app.entities.app_invoke_entities import AdvancedChatAppGenerateEntity, WorkflowAppGenerateEntity |
|
|
from core.app.entities.app_invoke_entities import AdvancedChatAppGenerateEntity, WorkflowAppGenerateEntity |
|
|
|
|
|
|
|
|
from libs.datetime_utils import naive_utc_now |
|
|
from libs.datetime_utils import naive_utc_now |
|
|
from models import ( |
|
|
from models import ( |
|
|
Account, |
|
|
Account, |
|
|
CreatorUserRole, |
|
|
|
|
|
EndUser, |
|
|
EndUser, |
|
|
WorkflowRun, |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self, |
|
|
self, |
|
|
*, |
|
|
*, |
|
|
application_generate_entity: Union[AdvancedChatAppGenerateEntity, WorkflowAppGenerateEntity], |
|
|
application_generate_entity: Union[AdvancedChatAppGenerateEntity, WorkflowAppGenerateEntity], |
|
|
|
|
|
user: Union[Account, EndUser], |
|
|
) -> None: |
|
|
) -> None: |
|
|
self._application_generate_entity = application_generate_entity |
|
|
self._application_generate_entity = application_generate_entity |
|
|
|
|
|
self._user = user |
|
|
|
|
|
|
|
|
def workflow_start_to_stream_response( |
|
|
def workflow_start_to_stream_response( |
|
|
self, |
|
|
self, |
|
|
|
|
|
|
|
|
workflow_execution: WorkflowExecution, |
|
|
workflow_execution: WorkflowExecution, |
|
|
) -> WorkflowFinishStreamResponse: |
|
|
) -> WorkflowFinishStreamResponse: |
|
|
created_by = None |
|
|
created_by = None |
|
|
workflow_run = session.scalar(select(WorkflowRun).where(WorkflowRun.id == workflow_execution.id_)) |
|
|
|
|
|
assert workflow_run is not None |
|
|
|
|
|
if workflow_run.created_by_role == CreatorUserRole.ACCOUNT: |
|
|
|
|
|
stmt = select(Account).where(Account.id == workflow_run.created_by) |
|
|
|
|
|
account = session.scalar(stmt) |
|
|
|
|
|
if account: |
|
|
|
|
|
created_by = { |
|
|
|
|
|
"id": account.id, |
|
|
|
|
|
"name": account.name, |
|
|
|
|
|
"email": account.email, |
|
|
|
|
|
} |
|
|
|
|
|
elif workflow_run.created_by_role == CreatorUserRole.END_USER: |
|
|
|
|
|
stmt = select(EndUser).where(EndUser.id == workflow_run.created_by) |
|
|
|
|
|
end_user = session.scalar(stmt) |
|
|
|
|
|
if end_user: |
|
|
|
|
|
created_by = { |
|
|
|
|
|
"id": end_user.id, |
|
|
|
|
|
"user": end_user.session_id, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user = self._user |
|
|
|
|
|
if isinstance(user, Account): |
|
|
|
|
|
created_by = { |
|
|
|
|
|
"id": user.id, |
|
|
|
|
|
"name": user.name, |
|
|
|
|
|
"email": user.email, |
|
|
|
|
|
} |
|
|
|
|
|
elif isinstance(user, EndUser): |
|
|
|
|
|
created_by = { |
|
|
|
|
|
"id": user.id, |
|
|
|
|
|
"user": user.session_id, |
|
|
|
|
|
} |
|
|
else: |
|
|
else: |
|
|
raise NotImplementedError(f"unknown created_by_role: {workflow_run.created_by_role}") |
|
|
|
|
|
|
|
|
raise NotImplementedError(f"User type not supported: {type(user)}") |
|
|
|
|
|
|
|
|
# Handle the case where finished_at is None by using current time as default |
|
|
# Handle the case where finished_at is None by using current time as default |
|
|
finished_at_timestamp = ( |
|
|
finished_at_timestamp = ( |