ForcePilot/src/agents/common/toolkits/debug/tools.py

87 lines
2.7 KiB
Python
Raw Normal View History

import os
import uuid
from typing import Any
import requests
from langgraph.types import interrupt
from src import config
from src.agents.common.toolkits import tool
from src.storage.minio import aupload_file_to_minio
from src.utils import logger
@tool(category="debug", tags=["图片", "测试"], display_name="文生图测试")
async def text_to_img_demo(text: str) -> str:
"""【测试用】使用模型生成图片, 会返回图片的URL"""
url = "https://api.siliconflow.cn/v1/images/generations"
payload = {
"model": "Qwen/Qwen-Image",
"prompt": text,
}
headers = {"Authorization": f"Bearer {os.getenv('SILICONFLOW_API_KEY')}", "Content-Type": "application/json"}
try:
response = requests.post(url, json=payload, headers=headers)
response_json = response.json()
except Exception as e:
logger.error(f"Failed to generate image with: {e}")
raise ValueError(f"Image generation failed: {e}")
try:
image_url = response_json["images"][0]["url"]
except (KeyError, IndexError, TypeError) as e:
logger.error(f"Failed to parse image URL from response: {e}, {response_json=}")
raise ValueError(f"Image URL extraction failed: {e}")
# 2. Upload to MinIO (Simplified)
response = requests.get(image_url)
file_data = response.content
file_name = f"{uuid.uuid4()}.jpg"
image_url = await aupload_file_to_minio(
bucket_name="generated-images", file_name=file_name, data=file_data, file_extension="jpg"
)
logger.info(f"Image uploaded. URL: {image_url}")
return image_url
@tool(category="debug", tags=["内置", "审批"], display_name="人工审批")
def get_approved_user_goal(
operation_description: str,
) -> dict:
"""
请求人工审批在执行重要操作前获得人类确认
Args:
operation_description: 需要审批的操作描述例如 "调用知识库工具"
Returns:
dict: 包含审批结果的字典格式为 {"approved": bool, "message": str}
"""
# 构建详细的中断信息
interrupt_info = {
"question": "是否批准以下操作?",
"operation": operation_description,
}
# 触发人工审批
is_approved = interrupt(interrupt_info)
# 返回审批结果
if is_approved:
result = {
"approved": True,
"message": f"✅ 操作已批准:{operation_description}",
}
print(f"✅ 人工审批通过: {operation_description}")
else:
result = {
"approved": False,
"message": f"❌ 操作被拒绝:{operation_description}",
}
print(f"❌ 人工审批被拒绝: {operation_description}")
return result