tools.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. """
  2. Gateway 通用工具函数
  3. 这些函数不依赖任何特定的 Agent 框架,可以被任何 Agent 直接使用。
  4. 各框架可以将这些函数包装成自己的工具格式。
  5. """
  6. import uuid
  7. from typing import Any, Dict, List, Optional
  8. from .client import GatewayClient, AgentCard
  9. async def send_message(
  10. gateway_url: str,
  11. from_agent_id: str,
  12. to_agent_id: str,
  13. message: str,
  14. conversation_id: Optional[str] = None,
  15. metadata: Optional[Dict[str, Any]] = None
  16. ) -> Dict[str, Any]:
  17. """
  18. 发送消息到其他 Agent(通用函数)
  19. Args:
  20. gateway_url: Gateway 地址
  21. from_agent_id: 发送方 Agent ID
  22. to_agent_id: 接收方 Agent ID
  23. message: 消息内容(文本)
  24. conversation_id: 对话 ID(可选,不提供则新建)
  25. metadata: 元数据
  26. Returns:
  27. 发送结果,包含 conversation_id, message_id 等
  28. """
  29. # 生成对话 ID
  30. conv_id = conversation_id or f"conv-{uuid.uuid4()}"
  31. # 创建临时客户端
  32. agent_card = AgentCard(
  33. agent_id=from_agent_id,
  34. agent_name=from_agent_id
  35. )
  36. async with GatewayClient(gateway_url=gateway_url, agent_card=agent_card) as client:
  37. result = await client.send_message(
  38. to_agent_id=to_agent_id,
  39. content=[{"type": "text", "text": message}],
  40. conversation_id=conv_id,
  41. metadata=metadata
  42. )
  43. return {
  44. "conversation_id": conv_id,
  45. "message_id": result.get("message_id"),
  46. "status": result.get("status", "sent"),
  47. "to_agent": to_agent_id
  48. }
  49. async def send_multimodal_message(
  50. gateway_url: str,
  51. from_agent_id: str,
  52. to_agent_id: str,
  53. content: List[Dict[str, Any]],
  54. conversation_id: Optional[str] = None,
  55. metadata: Optional[Dict[str, Any]] = None
  56. ) -> Dict[str, Any]:
  57. """
  58. 发送多模态消息到其他 Agent(通用函数)
  59. Args:
  60. gateway_url: Gateway 地址
  61. from_agent_id: 发送方 Agent ID
  62. to_agent_id: 接收方 Agent ID
  63. content: MAMP 格式的多模态内容
  64. conversation_id: 对话 ID(可选)
  65. metadata: 元数据
  66. Returns:
  67. 发送结果
  68. """
  69. conv_id = conversation_id or f"conv-{uuid.uuid4()}"
  70. agent_card = AgentCard(
  71. agent_id=from_agent_id,
  72. agent_name=from_agent_id
  73. )
  74. async with GatewayClient(gateway_url=gateway_url, agent_card=agent_card) as client:
  75. result = await client.send_message(
  76. to_agent_id=to_agent_id,
  77. content=content,
  78. conversation_id=conv_id,
  79. metadata=metadata
  80. )
  81. return {
  82. "conversation_id": conv_id,
  83. "message_id": result.get("message_id"),
  84. "status": result.get("status", "sent"),
  85. "to_agent": to_agent_id
  86. }
  87. async def list_online_agents(
  88. gateway_url: str,
  89. agent_type: Optional[str] = None
  90. ) -> List[Dict[str, Any]]:
  91. """
  92. 查询在线 Agent 列表(通用函数)
  93. Args:
  94. gateway_url: Gateway 地址
  95. agent_type: 过滤 Agent 类型(可选)
  96. Returns:
  97. 在线 Agent 列表
  98. """
  99. # 创建临时客户端(用于查询)
  100. agent_card = AgentCard(
  101. agent_id="query-client",
  102. agent_name="Query Client"
  103. )
  104. async with GatewayClient(gateway_url=gateway_url, agent_card=agent_card) as client:
  105. agents = await client.list_agents(online_only=True, agent_type=agent_type)
  106. return agents
  107. async def get_agent_status(
  108. gateway_url: str,
  109. agent_id: str
  110. ) -> Dict[str, Any]:
  111. """
  112. 查询指定 Agent 的在线状态(通用函数)
  113. Args:
  114. gateway_url: Gateway 地址
  115. agent_id: Agent ID
  116. Returns:
  117. Agent 状态信息
  118. """
  119. agent_card = AgentCard(
  120. agent_id="query-client",
  121. agent_name="Query Client"
  122. )
  123. async with GatewayClient(gateway_url=gateway_url, agent_card=agent_card) as client:
  124. status = await client.get_agent_status(agent_id)
  125. return status
  126. async def register_agent(
  127. gateway_url: str,
  128. agent_id: str,
  129. agent_name: str,
  130. agent_type: Optional[str] = None,
  131. capabilities: Optional[List[str]] = None,
  132. description: Optional[str] = None,
  133. metadata: Optional[Dict[str, Any]] = None
  134. ) -> GatewayClient:
  135. """
  136. 注册 Agent 到 Gateway(通用函数)
  137. Args:
  138. gateway_url: Gateway 地址
  139. agent_id: Agent ID
  140. agent_name: Agent 名称
  141. agent_type: Agent 类型
  142. capabilities: Agent 能力列表
  143. description: Agent 描述
  144. metadata: 元数据
  145. Returns:
  146. 已连接的 GatewayClient 实例(需要调用方管理生命周期)
  147. """
  148. agent_card = AgentCard(
  149. agent_id=agent_id,
  150. agent_name=agent_name,
  151. agent_type=agent_type,
  152. capabilities=capabilities or [],
  153. description=description,
  154. metadata=metadata or {}
  155. )
  156. client = GatewayClient(gateway_url=gateway_url, agent_card=agent_card)
  157. await client.connect()
  158. return client