plugin.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from collections.abc import Generator, Sequence
  2. from typing import Any, Optional
  3. from core.agent.entities import AgentInvokeMessage
  4. from core.agent.plugin_entities import AgentStrategyEntity, AgentStrategyParameter
  5. from core.agent.strategy.base import BaseAgentStrategy
  6. from core.plugin.manager.agent import PluginAgentManager
  7. from core.plugin.utils.converter import convert_parameters_to_plugin_format
  8. class PluginAgentStrategy(BaseAgentStrategy):
  9. """
  10. Agent Strategy
  11. """
  12. tenant_id: str
  13. plugin_unique_identifier: str
  14. declaration: AgentStrategyEntity
  15. def __init__(self, tenant_id: str, plugin_unique_identifier: str, declaration: AgentStrategyEntity):
  16. self.tenant_id = tenant_id
  17. self.plugin_unique_identifier = plugin_unique_identifier
  18. self.declaration = declaration
  19. def get_parameters(self) -> Sequence[AgentStrategyParameter]:
  20. return self.declaration.parameters
  21. def initialize_parameters(self, params: dict[str, Any]) -> dict[str, Any]:
  22. """
  23. Initialize the parameters for the agent strategy.
  24. """
  25. for parameter in self.declaration.parameters:
  26. params[parameter.name] = parameter.init_frontend_parameter(params.get(parameter.name))
  27. return params
  28. def _invoke(
  29. self,
  30. params: dict[str, Any],
  31. user_id: str,
  32. conversation_id: Optional[str] = None,
  33. app_id: Optional[str] = None,
  34. message_id: Optional[str] = None,
  35. ) -> Generator[AgentInvokeMessage, None, None]:
  36. """
  37. Invoke the agent strategy.
  38. """
  39. manager = PluginAgentManager()
  40. initialized_params = self.initialize_parameters(params)
  41. params = convert_parameters_to_plugin_format(initialized_params)
  42. yield from manager.invoke(
  43. tenant_id=self.tenant_id,
  44. user_id=user_id,
  45. agent_provider=self.declaration.identity.provider,
  46. agent_strategy=self.declaration.identity.name,
  47. agent_params=params,
  48. conversation_id=conversation_id,
  49. app_id=app_id,
  50. message_id=message_id,
  51. )