您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from collections.abc import Generator
  2. from typing import Any, Optional
  3. from core.agent.entities import AgentInvokeMessage
  4. from core.plugin.entities.plugin import GenericProviderID
  5. from core.plugin.entities.plugin_daemon import (
  6. PluginAgentProviderEntity,
  7. )
  8. from core.plugin.entities.request import PluginInvokeContext
  9. from core.plugin.impl.base import BasePluginClient
  10. class PluginAgentClient(BasePluginClient):
  11. def fetch_agent_strategy_providers(self, tenant_id: str) -> list[PluginAgentProviderEntity]:
  12. """
  13. Fetch agent providers for the given tenant.
  14. """
  15. def transformer(json_response: dict[str, Any]) -> dict:
  16. for provider in json_response.get("data", []):
  17. declaration = provider.get("declaration", {}) or {}
  18. provider_name = declaration.get("identity", {}).get("name")
  19. for strategy in declaration.get("strategies", []):
  20. strategy["identity"]["provider"] = provider_name
  21. return json_response
  22. response = self._request_with_plugin_daemon_response(
  23. "GET",
  24. f"plugin/{tenant_id}/management/agent_strategies",
  25. list[PluginAgentProviderEntity],
  26. params={"page": 1, "page_size": 256},
  27. transformer=transformer,
  28. )
  29. for provider in response:
  30. provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
  31. # override the provider name for each tool to plugin_id/provider_name
  32. for strategy in provider.declaration.strategies:
  33. strategy.identity.provider = provider.declaration.identity.name
  34. return response
  35. def fetch_agent_strategy_provider(self, tenant_id: str, provider: str) -> PluginAgentProviderEntity:
  36. """
  37. Fetch tool provider for the given tenant and plugin.
  38. """
  39. agent_provider_id = GenericProviderID(provider)
  40. def transformer(json_response: dict[str, Any]) -> dict:
  41. # skip if error occurs
  42. if json_response.get("data") is None or json_response.get("data", {}).get("declaration") is None:
  43. return json_response
  44. for strategy in json_response.get("data", {}).get("declaration", {}).get("strategies", []):
  45. strategy["identity"]["provider"] = agent_provider_id.provider_name
  46. return json_response
  47. response = self._request_with_plugin_daemon_response(
  48. "GET",
  49. f"plugin/{tenant_id}/management/agent_strategy",
  50. PluginAgentProviderEntity,
  51. params={"provider": agent_provider_id.provider_name, "plugin_id": agent_provider_id.plugin_id},
  52. transformer=transformer,
  53. )
  54. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  55. # override the provider name for each tool to plugin_id/provider_name
  56. for strategy in response.declaration.strategies:
  57. strategy.identity.provider = response.declaration.identity.name
  58. return response
  59. def invoke(
  60. self,
  61. tenant_id: str,
  62. user_id: str,
  63. agent_provider: str,
  64. agent_strategy: str,
  65. agent_params: dict[str, Any],
  66. conversation_id: Optional[str] = None,
  67. app_id: Optional[str] = None,
  68. message_id: Optional[str] = None,
  69. context: Optional[PluginInvokeContext] = None,
  70. ) -> Generator[AgentInvokeMessage, None, None]:
  71. """
  72. Invoke the agent with the given tenant, user, plugin, provider, name and parameters.
  73. """
  74. agent_provider_id = GenericProviderID(agent_provider)
  75. response = self._request_with_plugin_daemon_response_stream(
  76. "POST",
  77. f"plugin/{tenant_id}/dispatch/agent_strategy/invoke",
  78. AgentInvokeMessage,
  79. data={
  80. "user_id": user_id,
  81. "conversation_id": conversation_id,
  82. "app_id": app_id,
  83. "message_id": message_id,
  84. "context": context.model_dump() if context else {},
  85. "data": {
  86. "agent_strategy_provider": agent_provider_id.provider_name,
  87. "agent_strategy": agent_strategy,
  88. "agent_strategy_params": agent_params,
  89. },
  90. },
  91. headers={
  92. "X-Plugin-ID": agent_provider_id.plugin_id,
  93. "Content-Type": "application/json",
  94. },
  95. )
  96. return response