From 67cb3b876d89c274e111936dc9dcd9a658d57354 Mon Sep 17 00:00:00 2001 From: npc0-hue Date: Sat, 18 Oct 2025 18:20:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E7=94=A8=E6=88=B7=E5=92=8C=E5=B7=A5=E4=BD=9C?= =?UTF-8?q?=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/services/account_service_extend.py | 7 +++--- ..._workflow_node_execution_created_extend.py | 23 +++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/api/services/account_service_extend.py b/api/services/account_service_extend.py index 28e24b65c..288741059 100644 --- a/api/services/account_service_extend.py +++ b/api/services/account_service_extend.py @@ -1,5 +1,6 @@ from sqlalchemy import or_ +from extensions.ext_database import db from models.account import * from models.account import TenantAccountJoin from models.provider import Provider, ProviderModel @@ -10,7 +11,7 @@ class TenantExtendService: @staticmethod def create_default_tenant_member_if_not_exist(tenant_id: str, account_id: str, role: str = "normal") -> bool: available_ta = ( - TenantAccountJoin.query.filter_by(account_id=account_id, tenant_id=tenant_id) + db.session.query(TenantAccountJoin).filter_by(account_id=account_id, tenant_id=tenant_id) .order_by(TenantAccountJoin.id.asc()) .first() ) @@ -31,7 +32,7 @@ class TenantExtendService: @staticmethod def create_model_sync_config_if_not_exist(model_id: str, is_all: bool = True) -> bool: available_ta = ( - ModelSyncConfigExtend.query.filter_by(model_id=model_id).order_by(ModelSyncConfigExtend.id.asc()).first() + db.session.query(ModelSyncConfigExtend).filter_by(model_id=model_id).order_by(ModelSyncConfigExtend.id.asc()).first() ) if available_ta: @@ -91,7 +92,7 @@ class TenantExtendService: @staticmethod def create_provider_sync_config_if_not_exist(provider_id: str, is_all: bool = True) -> bool: available_ta = ( - ModelSyncConfigExtend.query.filter_by(model_id=provider_id).order_by(ModelSyncConfigExtend.id.asc()).first() + db.session.query(ModelSyncConfigExtend).filter_by(model_id=provider_id).order_by(ModelSyncConfigExtend.id.asc()).first() ) if available_ta: diff --git a/api/tasks/extend/update_account_money_when_workflow_node_execution_created_extend.py b/api/tasks/extend/update_account_money_when_workflow_node_execution_created_extend.py index 5af24616a..13ced3a6e 100644 --- a/api/tasks/extend/update_account_money_when_workflow_node_execution_created_extend.py +++ b/api/tasks/extend/update_account_money_when_workflow_node_execution_created_extend.py @@ -31,15 +31,23 @@ def update_account_money_when_workflow_node_execution_created_extend( # 非大模型则跳过 if workflow_node_execution_dict.get("node_type") != NodeType.LLM.value: return - + node_id = workflow_node_execution_dict.get("id") logging.info(click.style("工作流节点ID: {}".format(node_id), fg="cyan")) - # 拿到费用 - outputs_str = workflow_node_execution_dict.get("outputs") - outputs = json.loads(outputs_str) if outputs_str else {} - total_price = Decimal(outputs.get("usage", {}).get("total_price", 0)) - currency = outputs.get("usage", {}).get("currency", "USD") + # 拿到费用 - 从 outputs 字段获取费用信息(参考原始代码) + outputs = workflow_node_execution_dict.get("outputs", {}) + + # 如果 outputs 是字符串,则解析 JSON;如果已经是字典,则直接使用 + if isinstance(outputs, str): + outputs = json.loads(outputs) if outputs else {} + elif not isinstance(outputs, dict): + outputs = {} + + usage = outputs.get("usage", {}) + total_price = Decimal(usage.get("total_price", 0)) + currency = usage.get("currency", "USD") + if total_price == 0: return price = float(total_price) if currency == "USD" else ( @@ -116,6 +124,3 @@ def update_account_money_when_workflow_node_execution_created_extend( f"{format(price)} 异常报错,60秒后进行重试,", fg="red") ) raise self.retry(exc=e, countdown=60) - - -