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