# create_agents.py
import requests
import urllib3
from uuid import uuid4

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

BASE_URL = "http://192.168.0.46:22000"
AGENT_API = f"{BASE_URL}/api/v1/agents/"
HEADERS = {
    "Accept": "application/json",
    "Content-Type": "application/json",
}

TIMEOUT = 10
OUTPUT_FILE = "agent_ids.txt"

AGENT_META = {
    str(uuid4()): {
        "icon_index": 1,
        "name": "修理工程管理",
        "description": "对舰船修理工程实施全过程管理，包括任务分解、进度控制、工序协调和资源调配。",
        "prompt": "助理员您好，我是修理工程管理助手，我已经绑定了技术资料、通用知识、故障知识、法规标准知识库。我可以为您提供舰船设备的标准修理内容范围、作业工序清单以及安全施工要求，请直接告诉我您需要查询的设备名称。",
        "default_query": [
            {"title": "查询设备标准  →", "content": "查询设备标准"},
            {"title": "查看作业工序  →", "content": "查看作业工序"}
        ],
    },
    str(uuid4()): {
        "icon_index": 2,
        "name": "器材统计与分析",
        "description": "对舰船维修保障过程中涉及的器材进行统计分析与趋势研判。",
        "prompt": "助理员您好，我是器材统计与分析助手。我已经绑定了技术资料、通用知识、故障知识、法规标准知识库。为您提供多维度的器材消耗统计与库存分析服务。请告诉我您需要统计的具体业务场景。",
        "default_query": [
            {"title": "统计第一季度器材消耗  →", "content": "统计第一季度器材消耗"},
            {"title": "分析当前库存周转率  →", "content": "分析当前库存周转率"},
            {"title": "查看特定型号历史记录  →", "content": "查看特定型号历史记录"}
        ],
    },
    str(uuid4()): {
        "icon_index": 3,
        "name": "修理法规知识查询",
        "description": "提供舰船修理相关法规、标准和制度的查询与解读。",
        "prompt": "您好！我是修理法规知识查询助手。我已经绑定了技术资料、通用知识、故障知识、法规标准知识库。请告诉我需要查询的主题。",
         "default_query": [
            {"title": "查询焊接工艺规程  →", "content": "查询焊接工艺规程"},
            {"title": "查看设备拆装安全规程  →", "content": "查看设备拆装安全规程"},
            {"title": "搜索环保作业规定  →", "content": "搜索环保作业规定"}
        ],
    },
    str(uuid4()): {
        "icon_index": 4,
        "name": "船舰知识查询",
        "description": "提供舰船结构、系统组成与功能原理等知识。",
        "prompt": "您好！我是舰船知识查询助手。我已经绑定了技术资料、通用知识、故障知识、法规标准知识库。请告诉我需要了解的主题。",
        "default_query": [
            {"title": "了解舰船电力系统组成  →", "content": "了解舰船电力系统组成"},
            {"title": "查询舰船推进装置类型  →", "content": "查询舰船推进装置类型"},
            {"title": "搜索舰船雷达系统原理  →", "content": "搜索舰船雷达系统原理"}
        ],
    },
    str(uuid4()): {
        "icon_index": 5,
        "name": "故障排查与修理",
        "description": "辅助进行舰船设备故障分析、原因定位与修理方案制定。",
        "prompt": "您好！我是智能故障排查与修理助手。我已经绑定了技术资料、通用知识、故障知识、法规标准知识库。提供舷号、设备名称和故障现象，我可以协助您进行复杂故障的研判诊断。",
        "default_query": [
            {"title": "多模态输出与校验  →", "content": "多模态输出与校验"},
            {"title": "诊断研判与可能性排序  →", "content": "诊断研判与可能性排序"},
            {"title": "GT-25000燃气轮机的日常维护保养方案  →", "content": "GT-25000燃气轮机的日常维护保养方案"}
        ],
    },
    str(uuid4()): {
        "icon_index": 6,
        "name": "舰船机电知识",
        "description": "提供舰船动力、电气、液压等机电系统相关知识。",
        "prompt": "机电长您好，我是舰船机电知识查询助手。我已经绑定了技术资料、通用知识、故障知识、法规标准知识库。请告诉我需了解的主题。",
        "default_query": [
            {"title": "了解主推进系统构成  →", "content": "了解主推进系统构成"},
            {"title": "查看舰艇液压机参数  →", "content": "查看舰艇液压机参数"},
            {"title": "查看舰机液压系统参数  →", "content": "查看舰机液压系统参数"}
        ],
    },
    str(uuid4()): {
        "icon_index": 7,
        "name": "现场维修作业向导",
        "description": "为一线维修人员提供现场操作流程与安全指导。",
        "prompt": "您好！我是现场维修作业向导。我已经绑定了技术资料、通用知识、故障知识、法规标准知识库。您可以询问具体的维修步骤、拆装工艺或查询安全操作规程。",
        "default_query": [
            {"title": "离心泵汽蚀的典型特征  →", "content": "离心泵汽蚀的典型特征"},
            {"title": "海水泵汽蚀的维修步骤  →", "content": "海水泵汽蚀的维修步骤"},
            {"title": "GT-25000燃气轮机的日常维护步骤  →", "content": "GT-25000燃气轮机的日常维护步骤"}
        ],
    },
    str(uuid4()): {
        "icon_index": 8,
        "name": "舰船技术知识百科",
        "description": "综合性舰船技术知识库。",
        "prompt": "您好，我是舰船技术知识百科助手。我已经绑定了技术资料、通用知识、故障知识、法规标准知识库。请告诉我您需要查询的技术主题。",
        "default_query": [
            {"title": "了解船体高强钢性能  →", "content": "了解船体高强钢性能"},
            {"title": "查询焊接工艺评定标准  →", "content": "查询焊接工艺评定标准"},
            {"title": "查看防腐涂装技术规范  →", "content": "查看防腐涂装技术规范"}
        ],
    },
}


def build_agent_json(agent_id: str) -> dict:
    meta = AGENT_META[agent_id]
    return {
        "id": agent_id,
        "name": meta["name"],
        "description": meta["description"],
        "prompt": meta["prompt"],
        "background": "string",
        "access_control": {},
        "default_query": meta["default_query"],
    }


def create_agent(agent_id: str) -> bool:
    resp = requests.post(
        AGENT_API,
        headers=HEADERS,
        json=build_agent_json(agent_id),
        verify=False,
        timeout=TIMEOUT,
    )
    print(resp.json())
    print(f"[CREATE] {agent_id} -> {resp.status_code}")

    return resp.status_code in (200, 201, 409)


def main():
    created_ids = []

    for agent_id in AGENT_META.keys():
        if create_agent(agent_id):
            created_ids.append(agent_id)

    with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
        for aid in created_ids:
            f.write(aid + "\n")

    print(f"\n✅ 已写入 {len(created_ids)} 个 Agent UUID 到 {OUTPUT_FILE}")


if __name__ == "__main__":
    main()




# import requests
# import urllib3
# from uuid import uuid4
# from typing import List, Tuple

# # =========================
# # 0. 关闭 HTTPS 警告（开发环境）
# # =========================
# urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# # =========================
# # 1. 基础配置
# # =========================

# BASE_URL = "https://devb.zdht.hjkl01.cn:65432"

# AGENT_API = f"{BASE_URL}/api/v1/agents/"
# ROLE_BIND_API = f"{BASE_URL}/api/v1/agent-roles/association"

# HEADERS = {
#     "Accept": "application/json",
#     "Content-Type": "application/json",
# }

# TIMEOUT = 10

# # =========================
# # 2. 角色 ID（数据库真实存在）
# # =========================

# AGENT_ROLE_1 = "2"  # 助理员助手 (1-4)
# AGENT_ROLE_2 = "3"  # 机电长助手 (5-6)
# AGENT_ROLE_3 = "4"  # 维修员助手 (7-8)

# ROLE_NAME_MAP = {
#     AGENT_ROLE_1: "助理员助手",
#     AGENT_ROLE_2: "机电长助手",
#     AGENT_ROLE_3: "维修员助手",
# }

# # =========================
# # 3. Agent 元数据
# # =========================

# AGENT_META = {
#     str(uuid4()): {
#         "icon_index": 1,
#         "name": "修理工程管理",
#         "description": "对舰船修理工程实施全过程管理，包括任务分解、进度控制、工序协调和资源调配。",
#         "prompt": "您好！我是一名舰船修理工程管理专家。",
#     },
#     str(uuid4()): {
#         "icon_index": 2,
#         "name": "器材统计与分析",
#         "description": "对舰船维修保障过程中涉及的器材进行统计分析与趋势研判。",
#         "prompt": "您好！我是一名舰船器材统计与分析专家。",
#     },
#     str(uuid4()): {
#         "icon_index": 3,
#         "name": "修理法规知识查询",
#         "description": "提供舰船修理相关法规、标准和制度的查询与解读。",
#         "prompt": "您好！我是一名舰船修理法规专家。",
#     },
#     str(uuid4()): {
#         "icon_index": 4,
#         "name": "船舰知识查询",
#         "description": "提供舰船结构、系统组成与功能原理等知识。",
#         "prompt": "您好！我是一名舰船专业知识顾问。",
#     },
#     str(uuid4()): {
#         "icon_index": 5,
#         "name": "故障排查与修理",
#         "description": "辅助进行舰船设备故障分析、原因定位与修理方案制定。",
#         "prompt": "您好！我是一名舰船设备故障诊断专家。",
#     },
#     str(uuid4()): {
#         "icon_index": 6,
#         "name": "舰船机电知识",
#         "description": "提供舰船动力、电气、液压等机电系统相关知识。",
#         "prompt": "您好！我是一名舰船机电系统专家。",
#     },
#     str(uuid4()): {
#         "icon_index": 7,
#         "name": "现场维修作业向导",
#         "description": "为一线维修人员提供现场操作流程与安全指导。",
#         "prompt": "您好！我是一名现场维修作业向导。",
#     },
#     str(uuid4()): {
#         "icon_index": 8,
#         "name": "舰船技术知识百科",
#         "description": "综合性舰船技术知识库。",
#         "prompt": "您好！我是一部舰船技术知识百科。",
#     },
# }

# # =========================
# # 4. 构建 Agent JSON
# # =========================

# def build_agent_json(agent_id: str) -> dict:
#     meta = AGENT_META[agent_id]
#     return {
#         "id": agent_id,
#         "name": meta["name"],
#         "description": meta["description"],
#         "prompt": meta["prompt"],
#         "background": "string",
#         "access_control": {},
#     }

# # =========================
# # 5. 创建 Agent
# # =========================

# def create_agent(agent_id: str) -> bool:
#     resp = requests.post(
#         AGENT_API,
#         headers=HEADERS,
#         json=build_agent_json(agent_id),
#         verify=False,
#         timeout=TIMEOUT,
#     )

#     print(f"[CREATE] Agent {agent_id} -> {resp.status_code}")

#     if resp.status_code in (200, 201, 409):
#         return True

#     print("  ↳ 返回值:", resp.text)
#     return False

# # =========================
# # 6. 绑定 Agent 到角色
# # =========================

# def bind_agent_to_role(agent_id: str, role_id: str) -> bool:
#     payload = [
#         {
#             "agent_id": agent_id,
#             "agent_role_id": role_id,
#             "is_active": True,
#         }
#     ]

#     resp = requests.post(
#         ROLE_BIND_API,
#         headers=HEADERS,
#         json=payload,
#         verify=False,
#         timeout=TIMEOUT,
#     )

#     print(
#         f"[BIND] Agent {agent_id} -> Role {role_id} "
#         f"({ROLE_NAME_MAP.get(role_id)}) => {resp.status_code}"
#     )

#     if resp.status_code in (200, 201):
#         return True

#     print("  ↳ 返回值:", resp.text)
#     return False

# # =========================
# # 7. 主流程（先创建 → 再统一绑定）
# # =========================

# def main():
#     pending_bindings: List[Tuple[str, str]] = []

#     print("=== 开始创建 Agent ===")

#     for agent_id, meta in AGENT_META.items():
#         try:
#             if not create_agent(agent_id):
#                 continue

#             idx = meta["icon_index"]

#             if 1 <= idx <= 4:
#                 role_id = AGENT_ROLE_1
#             elif 5 <= idx <= 6:
#                 role_id = AGENT_ROLE_2
#             elif 7 <= idx <= 8:
#                 role_id = AGENT_ROLE_3
#             else:
#                 continue

#             pending_bindings.append((agent_id, role_id))

#         except Exception as e:
#             print(f"[ERROR] Agent {agent_id} -> {e}")

#     print("\n=== 开始统一绑定角色 ===")

#     for agent_id, role_id in pending_bindings:
#         bind_agent_to_role(agent_id, role_id)

#     print("\n=== 全部完成 ===")

# # =========================
# # 8. 入口
# # =========================

# if __name__ == "__main__":
#     main()
