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

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