agent.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. for strategy in json_response.get("data", {}).get("declaration", {}).get("strategies", []):
  41. strategy["identity"]["provider"] = agent_provider_id.provider_name
  42. return json_response
  43. response = self._request_with_plugin_daemon_response(
  44. "GET",
  45. f"plugin/{tenant_id}/management/agent_strategy",
  46. PluginAgentProviderEntity,
  47. params={"provider": agent_provider_id.provider_name, "plugin_id": agent_provider_id.plugin_id},
  48. transformer=transformer,
  49. )
  50. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  51. # override the provider name for each tool to plugin_id/provider_name
  52. for strategy in response.declaration.strategies:
  53. strategy.identity.provider = response.declaration.identity.name
  54. return response
  55. def invoke(
  56. self,
  57. tenant_id: str,
  58. user_id: str,
  59. agent_provider: str,
  60. agent_strategy: str,
  61. agent_params: dict[str, Any],
  62. conversation_id: Optional[str] = None,
  63. app_id: Optional[str] = None,
  64. message_id: Optional[str] = None,
  65. ) -> Generator[AgentInvokeMessage, None, None]:
  66. """
  67. Invoke the agent with the given tenant, user, plugin, provider, name and parameters.
  68. """
  69. agent_provider_id = GenericProviderID(agent_provider)
  70. response = self._request_with_plugin_daemon_response_stream(
  71. "POST",
  72. f"plugin/{tenant_id}/dispatch/agent/invoke",
  73. AgentInvokeMessage,
  74. data={
  75. "user_id": user_id,
  76. "conversation_id": conversation_id,
  77. "app_id": app_id,
  78. "message_id": message_id,
  79. "data": {
  80. "agent_strategy_provider": agent_provider_id.provider_name,
  81. "agent_strategy": agent_strategy,
  82. "agent_strategy_params": agent_params,
  83. },
  84. },
  85. headers={
  86. "X-Plugin-ID": agent_provider_id.plugin_id,
  87. "Content-Type": "application/json",
  88. },
  89. )
  90. return response