BxDolAIToolContentStructure.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. use NeuronAI\Tools\PropertyType;
  10. use NeuronAI\Tools\Tool;
  11. use NeuronAI\Tools\ToolProperty;
  12. class BxDolAIToolContentStructure extends Tool
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct(
  17. 'content_structure',
  18. 'Retrieve the structure of content fields for a given module.',
  19. );
  20. }
  21. protected function properties(): array
  22. {
  23. return [
  24. new ToolProperty(
  25. name: 'module',
  26. type: PropertyType::STRING,
  27. description: 'The module name to retrieve content fields from. Use "system" for site accounts. Use "all" to get fields for all content modules.',
  28. required: true
  29. )
  30. ];
  31. }
  32. public function __invoke(string $module): string
  33. {
  34. echoDbgLog("content_structure tool called with module: {$module}");
  35. $aContentModules = bx_srv('system', 'modules_list', [true], 'TemplServiceContent');
  36. $s = $this->formatModulesOutputForLLM($aContentModules);
  37. $aStructure = bx_srv('system', 'content_modules_fields', [$module], 'TemplServiceContent');
  38. if (isset($aStructure['code']))
  39. $s .= "\n\nError: module was not found";
  40. else
  41. $s .= "\n" . $this->formatStructureOutputForLLM($aStructure);
  42. return $s;
  43. }
  44. protected function formatModulesOutputForLLM(array $aContentModules): string
  45. {
  46. $s = "# All content modules\n\n";
  47. foreach ($aContentModules as $sModule => $a) {
  48. $s .= "- **" . $a['title'] . "** (" . $sModule . ")";
  49. if ($sModule == 'system') {
  50. $s .= " - system module with site accounts";
  51. }
  52. $s .= "\n";
  53. }
  54. return $s;
  55. }
  56. protected function formatStructureOutputForLLM(array $aStructure): string
  57. {
  58. $s = "# Content modules fields structure\n\n";
  59. foreach ($aStructure as $sModule => $a) {
  60. $s .= "## " . $sModule . "\n\n";
  61. $s .= $a['db_table'] ? "DB table: " . $a['db_table'] . "\n" : "";
  62. $s .= $a['db_table_fields'] ? "DB table fields: " . implode(', ', $a['db_table_fields']) . "\n" : "";
  63. $s .= "\n";
  64. $aMap = ['fields_add' => 'Fields for adding content', 'fields_edit' => 'Fields for editing content'];
  65. foreach ($aMap as $sKey => $sTitle) {
  66. if (isset($a[$sKey])) {
  67. $s .= "### " . $sTitle . "\n\n";
  68. foreach ($a[$sKey] as $sField => $r) {
  69. if ($r['type'] == 'custom' || $r['type'] == 'input_set')
  70. continue;
  71. $s .= "- **" . $r['name'] . "** " . (!empty($r['caption']) ? "(" . $r['caption'] . ")" : "") . (isset($r['required']) && $r['required'] ? " required" : "") . "\n";
  72. $s .= "\t- type: " . $r['type'] . "\n";
  73. if (isset($r['values']) && is_array($r['values']) && $r['type'] != 'files') {
  74. $s .= "\t- values (id:value): " . $this->formatValues($r['values']) . "\n";
  75. }
  76. }
  77. }
  78. $s .= "\n";
  79. }
  80. }
  81. return $s;
  82. }
  83. protected function formatValues(array $aValues): string
  84. {
  85. $s = "";
  86. foreach ($aValues as $sKey => $mixedValue) {
  87. $sValue = $mixedValue;
  88. if (is_array($mixedValue) && isset($mixedValue['value'])) {
  89. $sValue = $mixedValue['value'];
  90. $sKey = $mixedValue['key'];
  91. }
  92. $s .= (empty($sKey) ? 'EMPTY' : $sKey) . ":" . $sValue . ",";
  93. }
  94. return trim($s, ",");
  95. }
  96. }