BxDolAIToolFactory.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. case 'content_structure':
  41. $o = BxDolAIToolContentStructure::make();
  42. break;
  43. case 'content_get':
  44. $o = BxDolAIToolContentGet::make();
  45. break;
  46. case 'content_change':
  47. $o = BxDolAIToolContentChange::make();
  48. break;
  49. default:
  50. bx_log('sys_agents', "Tool type {$a['type']} is not supported", BX_LOG_ERR);
  51. throw new Exception("Tool type {$a['type']} is not supported");
  52. }
  53. $GLOBALS['bxDolClasses'][__CLASS__ . '_AiAgentTool_' . $iId] = $o;
  54. return $o;
  55. }
  56. }