ruff format

This commit is contained in:
hjlarry 2025-09-10 14:25:37 +08:00
parent 21fee59b22
commit c8acc48976
6 changed files with 52 additions and 71 deletions

View File

@ -88,7 +88,7 @@ def handle_disconnect(sid):
if mapping:
data = json.loads(mapping)
workflow_id = data["workflow_id"]
# Remove this specific session
redis_client.hdel(f"workflow_online_users:{workflow_id}", sid)
redis_client.delete(f"ws_sid_map:{sid}")
@ -177,18 +177,20 @@ def broadcast_online_users(workflow_id):
"""
sessions_json = redis_client.hgetall(f"workflow_online_users:{workflow_id}")
users = []
for sid, session_info_json in sessions_json.items():
try:
session_info = json.loads(session_info_json)
# Each session appears as a separate "user" in the UI
users.append({
"user_id": session_info["user_id"],
"username": session_info["username"],
"avatar": session_info.get("avatar"),
"sid": session_info["sid"],
"connected_at": session_info.get("connected_at"),
})
users.append(
{
"user_id": session_info["user_id"],
"username": session_info["username"],
"avatar": session_info.get("avatar"),
"sid": session_info["sid"],
"connected_at": session_info.get("connected_at"),
}
)
except Exception:
continue

View File

@ -164,10 +164,10 @@ class WorkflowCommentReplyApi(Resource):
args = parser.parse_args()
result = WorkflowCommentService.create_reply(
comment_id=comment_id,
content=args.content,
comment_id=comment_id,
content=args.content,
created_by=current_user.id,
mentioned_user_ids=args.mentioned_user_ids
mentioned_user_ids=args.mentioned_user_ids,
)
return result, 201
@ -194,10 +194,7 @@ class WorkflowCommentReplyDetailApi(Resource):
args = parser.parse_args()
reply = WorkflowCommentService.update_reply(
reply_id=reply_id,
user_id=current_user.id,
content=args.content,
mentioned_user_ids=args.mentioned_user_ids
reply_id=reply_id, user_id=current_user.id, content=args.content, mentioned_user_ids=args.mentioned_user_ids
)
return reply
@ -227,7 +224,7 @@ class WorkflowCommentMentionUsersApi(Resource):
@get_app_model
@marshal_with({"users": fields.List(fields.Nested(account_with_role_fields))})
def get(self, app_model: App):
"""Get all users in current tenant for mentions."""
"""Get all users in current tenant for mentions."""
members = TenantService.get_tenant_members(current_user.current_tenant)
return {"users": members}

View File

@ -370,7 +370,7 @@ class ConversationVariableCollectionApi(Resource):
draft_var_srv.prefill_conversation_variable_default_values(draft_workflow)
db.session.commit()
return _get_variable_list(app_model, CONVERSATION_VARIABLE_NODE_ID)
@setup_required
@login_required
@account_initialization_required
@ -379,7 +379,7 @@ class ConversationVariableCollectionApi(Resource):
# The role of the current user in the ta table must be admin, owner, or editor
if not current_user.is_editor:
raise Forbidden()
parser = reqparse.RequestParser()
parser.add_argument("conversation_variables", type=list, required=True, location="json")
args = parser.parse_args()
@ -397,7 +397,7 @@ class ConversationVariableCollectionApi(Resource):
conversation_variables=conversation_variables,
)
return { "result": "success" }
return {"result": "success"}
class SystemVariableCollectionApi(Resource):
@ -439,7 +439,7 @@ class EnvironmentVariableCollectionApi(Resource):
)
return {"items": env_vars_list}
@setup_required
@login_required
@account_initialization_required
@ -448,7 +448,7 @@ class EnvironmentVariableCollectionApi(Resource):
# The role of the current user in the ta table must be admin, owner, or editor
if not current_user.is_editor:
raise Forbidden()
parser = reqparse.RequestParser()
parser.add_argument("environment_variables", type=list, required=True, location="json")
args = parser.parse_args()
@ -466,7 +466,7 @@ class EnvironmentVariableCollectionApi(Resource):
environment_variables=environment_variables,
)
return { "result": "success" }
return {"result": "success"}
api.add_resource(

View File

@ -9,7 +9,7 @@ comment_account_fields = {"id": fields.String, "name": fields.String, "email": f
workflow_comment_mention_fields = {
"mentioned_user_id": fields.String,
"mentioned_user_account": fields.Nested(comment_account_fields, allow_null=True),
"reply_id": fields.String
"reply_id": fields.String,
}
# Comment reply fields

View File

@ -42,6 +42,7 @@ class WorkflowCommentService:
@staticmethod
def get_comment(tenant_id: str, app_id: str, comment_id: str, session: Session = None) -> WorkflowComment:
"""Get a specific comment."""
def _get_comment(session: Session) -> WorkflowComment:
stmt = (
select(WorkflowComment)
@ -96,9 +97,9 @@ class WorkflowCommentService:
for user_id in mentioned_user_ids:
if isinstance(user_id, str) and uuid_value(user_id):
mention = WorkflowCommentMention(
comment_id=comment.id,
comment_id=comment.id,
reply_id=None, # This is a comment mention, not reply mention
mentioned_user_id=user_id
mentioned_user_id=user_id,
)
session.add(mention)
@ -123,16 +124,13 @@ class WorkflowCommentService:
with Session(db.engine, expire_on_commit=False) as session:
# Get comment with validation
stmt = (
select(WorkflowComment)
.where(
WorkflowComment.id == comment_id,
WorkflowComment.tenant_id == tenant_id,
WorkflowComment.app_id == app_id,
)
stmt = select(WorkflowComment).where(
WorkflowComment.id == comment_id,
WorkflowComment.tenant_id == tenant_id,
WorkflowComment.app_id == app_id,
)
comment = session.scalar(stmt)
if not comment:
raise NotFound("Comment not found")
@ -151,7 +149,7 @@ class WorkflowCommentService:
existing_mentions = session.scalars(
select(WorkflowCommentMention).where(
WorkflowCommentMention.comment_id == comment.id,
WorkflowCommentMention.reply_id.is_(None) # Only comment mentions, not reply mentions
WorkflowCommentMention.reply_id.is_(None), # Only comment mentions, not reply mentions
)
).all()
for mention in existing_mentions:
@ -162,18 +160,15 @@ class WorkflowCommentService:
for user_id_str in mentioned_user_ids:
if isinstance(user_id_str, str) and uuid_value(user_id_str):
mention = WorkflowCommentMention(
comment_id=comment.id,
comment_id=comment.id,
reply_id=None, # This is a comment mention
mentioned_user_id=user_id_str
mentioned_user_id=user_id_str,
)
session.add(mention)
session.commit()
return {
"id": comment.id,
"updated_at": comment.updated_at
}
return {"id": comment.id, "updated_at": comment.updated_at}
@staticmethod
def delete_comment(tenant_id: str, app_id: str, comment_id: str, user_id: str) -> None:
@ -219,10 +214,7 @@ class WorkflowCommentService:
@staticmethod
def create_reply(
comment_id: str,
content: str,
created_by: str,
mentioned_user_ids: Optional[list[str]] = None
comment_id: str, content: str, created_by: str, mentioned_user_ids: Optional[list[str]] = None
) -> dict:
"""Add a reply to a workflow comment."""
WorkflowCommentService._validate_content(content)
@ -244,29 +236,21 @@ class WorkflowCommentService:
if isinstance(user_id, str) and uuid_value(user_id):
# Create mention linking to specific reply
mention = WorkflowCommentMention(
comment_id=comment_id,
reply_id=reply.id,
mentioned_user_id=user_id
comment_id=comment_id, reply_id=reply.id, mentioned_user_id=user_id
)
session.add(mention)
session.commit()
return {
"id": reply.id,
"created_at": reply.created_at
}
return {"id": reply.id, "created_at": reply.created_at}
@staticmethod
def update_reply(
reply_id: str,
user_id: str,
content: str,
mentioned_user_ids: Optional[list[str]] = None
reply_id: str, user_id: str, content: str, mentioned_user_ids: Optional[list[str]] = None
) -> WorkflowCommentReply:
"""Update a comment reply."""
WorkflowCommentService._validate_content(content)
with Session(db.engine, expire_on_commit=False) as session:
reply = session.get(WorkflowCommentReply, reply_id)
if not reply:
@ -290,19 +274,14 @@ class WorkflowCommentService:
for user_id_str in mentioned_user_ids:
if isinstance(user_id_str, str) and uuid_value(user_id_str):
mention = WorkflowCommentMention(
comment_id=reply.comment_id,
reply_id=reply.id,
mentioned_user_id=user_id_str
comment_id=reply.comment_id, reply_id=reply.id, mentioned_user_id=user_id_str
)
session.add(mention)
session.commit()
session.refresh(reply) # Refresh to get updated timestamp
return {
"id": reply.id,
"updated_at": reply.updated_at
}
return {"id": reply.id, "updated_at": reply.updated_at}
@staticmethod
def delete_reply(reply_id: str, user_id: str) -> None:

View File

@ -244,9 +244,10 @@ class WorkflowService:
# return draft workflow
return workflow
def update_draft_workflow_environment_variables(
self, *,
self,
*,
app_model: App,
environment_variables: Sequence[Variable],
account: Account,
@ -268,7 +269,8 @@ class WorkflowService:
db.session.commit()
def update_draft_workflow_conversation_variables(
self, *,
self,
*,
app_model: App,
conversation_variables: Sequence[Variable],
account: Account,
@ -290,7 +292,8 @@ class WorkflowService:
db.session.commit()
def update_draft_workflow_features(
self, *,
self,
*,
app_model: App,
features: dict,
account: Account,
@ -303,7 +306,7 @@ class WorkflowService:
if not workflow:
raise ValueError("No draft workflow found.")
# validate features structure
self.validate_features_structure(app_model=app_model, features=features)