BxDolAIToolFactory.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolAIToolFactory extends BxDolFactory
  10. {
  11. public static function getToolInstance(int $iId): NeuronAI\Tools\ToolInterface
  12. {
  13. if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '_AiAgentTool_' . $iId]))
  14. return $GLOBALS['bxDolClasses'][__CLASS__ . '_AiAgentTool_' . $iId];
  15. $a = BxDolAIQuery::getToolObject($iId);
  16. if (!$a) {
  17. bx_log('sys_agents', "Tool with id {$iId} not found", BX_LOG_ERR);
  18. throw new Exception("Tool with id {$iId} not found");
  19. }
  20. $aParametersSystem = !empty($a['params']) ? json_decode($a['params'], true) : [];
  21. $aParametersUser = !empty($a['params_user']) ? json_decode($a['params_user'], true) : [];
  22. $aParameters = array_merge($aParametersSystem, $aParametersUser);
  23. switch($a['type']) {
  24. case 'mysql_schema':
  25. $o = NeuronAI\Tools\Toolkits\MySQL\MySQLSchemaTool::make(
  26. pdo: BxDolDb::getInstance()->getLink(),
  27. tables: $aParameters['tables'] ?? null
  28. );
  29. break;
  30. case 'mysql_select':
  31. $o = NeuronAI\Tools\Toolkits\MySQL\MySQLSelectTool::make(
  32. pdo: BxDolDb::getInstance()->getLink(),
  33. );
  34. break;
  35. case 'mysql_write':
  36. $o = NeuronAI\Tools\Toolkits\MySQL\MySQLWriteTool::make(
  37. pdo: BxDolDb::getInstance()->getLink(),
  38. );
  39. break;
  40. default:
  41. if (!empty($a['class_name'])) {
  42. if (!empty($aObject['class_file']))
  43. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['class_file']);
  44. $o = $a['class_name']::make();
  45. }
  46. else {
  47. bx_log('sys_agents', "Tool type {$a['type']} is not supported", BX_LOG_ERR);
  48. throw new Exception("Tool type {$a['type']} is not supported");
  49. }
  50. }
  51. $GLOBALS['bxDolClasses'][__CLASS__ . '_AiAgentTool_' . $iId] = $o;
  52. return $o;
  53. }
  54. }