agent.py 4.3 KB

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