更新yaml文件

This commit is contained in:
zhanggangyong
2026-01-15 07:43:09 +00:00
parent 114ddd4803
commit 9ceb72abf1
5 changed files with 643 additions and 38 deletions
@@ -320,7 +320,7 @@ async def litellm_callback(
# 🔍 调试:打印完整的回调内容(使用print确保输出)
print(f"📥 收到LiteLLM回调 - 类型: {'数组' if isinstance(body, list) else '对象'}")
print(f"📥 完整回调内容:\n{json.dumps(body, indent=2, ensure_ascii=False)}")
#print(f"📥 完整回调内容:\n{json.dumps(body, indent=2, ensure_ascii=False)}")
logger.info(f"📥 收到LiteLLM回调 - 类型: {'数组' if isinstance(body, list) else '对象'}")
# 判断是数组还是单个对象
@@ -371,9 +371,16 @@ async def agent_manager_callback(
- Pod 运行时间(必填)
- 使用的工具列表(可选)
此接口用于记录 Agent 的实际运行时长并计费
此接口用于更新或创建 Agent 的计费记录:
- 如果存在运行中的计费记录(end_time=None),则更新该记录
- 如果不存在运行中的记录,则创建新记录(异常情况的兜底)
计费逻辑:
- 周期计费任务会对运行中的 Agent 进行增量扣款
- 此回调负责结算最终费用,只扣除增量部分,避免重复扣款
"""
from models import AgentBillingRecord
from sqlalchemy import and_
from app.billing import calculate_eu, calculate_platform_agent_cost, deduct_balance
try:
@@ -418,51 +425,104 @@ async def agent_manager_callback(
# 假设是平台Agent(可以根据agent_name前缀判断)
is_platform_agent = True
agent_type = "platform" # 可以从agent名称中提取
cost = calculate_platform_agent_cost(agent_type, duration_seconds)
new_cost = calculate_platform_agent_cost(agent_type, duration_seconds)
# 创建计费记录
billing_record = AgentBillingRecord(
user_id=callback_data.userId,
channel_id=user.channel_id if user.channel_id else None,
agent_name=callback_data.agentName,
agent_type=agent_type,
is_platform_agent=is_platform_agent,
duration_seconds=duration_seconds,
eu_consumed=eu_consumed,
cost=cost,
start_time=start_time,
end_time=end_time,
period_start=start_time or datetime.utcnow(),
period_end=end_time or datetime.utcnow(),
tools_used=callback_data.toolsUsed,
request_id=callback_data.requestId,
# ✅ 先查找现有的运行中计费记录(end_time == None)
existing_result = await db.execute(
select(AgentBillingRecord).where(
and_(
AgentBillingRecord.agent_name == callback_data.agentName,
AgentBillingRecord.user_id == callback_data.userId,
AgentBillingRecord.end_time == None # 运行中的记录
)
)
)
existing_record = existing_result.scalar_one_or_none()
db.add(billing_record)
# 扣除用户余额
success, message = await deduct_balance(
callback_data.userId,
cost,
db,
f"Agent 运行: {callback_data.agentName}"
)
if not success:
logger.warning(f"余额扣除失败: {message}")
if existing_record:
# ✅ 更新现有记录(避免重复创建)
previous_cost = Decimal(str(existing_record.cost or 0))
existing_record.end_time = end_time or datetime.utcnow()
existing_record.duration_seconds = duration_seconds
existing_record.eu_consumed = eu_consumed
existing_record.cost = float(new_cost)
existing_record.period_end = end_time or datetime.utcnow()
existing_record.tools_used = callback_data.toolsUsed
existing_record.request_id = callback_data.requestId
billing_record = existing_record
# 计算增量成本(新成本 - 已扣成本)
cost_increment = new_cost - previous_cost
logger.info(
f"📝 更新现有计费记录: agent={callback_data.agentName}, "
f"之前成本={previous_cost}, 最终成本={new_cost}, 增量={cost_increment}"
)
# 只扣除增量部分(避免与周期计费重复扣款)
if cost_increment > 0:
success, message = await deduct_balance(
callback_data.userId,
cost_increment,
db,
f"Agent 结算(增量): {callback_data.agentName}"
)
if not success:
logger.warning(f"增量余额扣除失败: {message}")
else:
logger.info(f"无需扣款(增量={cost_increment})")
else:
# ⚠️ 没有现有记录,创建新记录(异常情况的兜底)
logger.warning(
f"⚠️ 未找到运行中的计费记录,将创建新记录: "
f"agent={callback_data.agentName}, user={callback_data.userId}"
)
billing_record = AgentBillingRecord(
user_id=callback_data.userId,
channel_id=user.channel_id if user.channel_id else None,
agent_name=callback_data.agentName,
agent_type=agent_type,
is_platform_agent=is_platform_agent,
duration_seconds=duration_seconds,
eu_consumed=eu_consumed,
cost=float(new_cost),
start_time=start_time or datetime.utcnow(),
end_time=end_time or datetime.utcnow(),
period_start=start_time or datetime.utcnow(),
period_end=end_time or datetime.utcnow(),
tools_used=callback_data.toolsUsed,
request_id=callback_data.requestId,
)
db.add(billing_record)
# 新记录需要全额扣款
success, message = await deduct_balance(
callback_data.userId,
new_cost,
db,
f"Agent 运行: {callback_data.agentName}"
)
if not success:
logger.warning(f"余额扣除失败: {message}")
await db.commit()
await db.refresh(billing_record)
logger.info(
f"✅ Agent 计费记录创建成功: agent={callback_data.agentName}, "
f"duration={duration_seconds}秒, EU={eu_consumed}, cost={cost}, "
f"tools={callback_data.toolsUsed}"
f"✅ Agent 计费记录{'更新' if existing_record else '创建'}成功: "
f"agent={callback_data.agentName}, duration={duration_seconds}秒, "
f"EU={eu_consumed}, cost={new_cost}, tools={callback_data.toolsUsed}"
)
return AgentManagerCallbackResponse(
success=True,
message="Agent 计费记录创建成功",
message=f"Agent 计费记录{'更新' if existing_record else '创建'}成功",
recordId=str(billing_record.id)
)