Browse Source

Merge branch 'master' of https://github.com/unacms/UNA

AntonLV 5 months ago
parent
commit
0dcb6f3f6b

+ 1 - 1
inc/classes/BxDolAI.php

@@ -379,7 +379,7 @@ class BxDolAI extends BxDolFactory implements iBxDolSingleton
         $sParams = is_string($mixedParams) ? $mixedParams : json_encode($mixedParams);
 
         // update sample data
-        $a = ['alert' => 'alert_sample', 'webhook' => 'webhook_sample'];
+        $a = ['webhook' => 'webhook_sample'];
         if (isset($a[$sType]) && empty($aAgent[$a[$sType]])) {
             $this->_oDb->updateAgentField($aAgent['id'], $a[$sType], $sParams);
         }

+ 12 - 7
inc/classes/BxDolAIQuery.php

@@ -942,11 +942,12 @@ class BxDolAIQuery extends BxDolDb
         return $this->query($sQuery, ['val' => $aAgent['trigger'] . ':' . $aAgent['id'] . ':%']);
     }
 
-    public function getAlert($s) {
+    static public function getAlert($s) {
         if (!$s)
-            return false;
+            return false;        
         $a = explode(':', $s);
-        return $this->getRow("SELECT * FROM `sys_alerts_log` WHERE `unit` = :unit AND `action` = :action", [
+        $oDb = BxDolDb::getInstance();
+        return $oDb->getRow("SELECT * FROM `sys_alerts_log` WHERE `unit` = :unit AND `action` = :action", [
             'unit' => $a[0] ?? '',
             'action' => $a[1] ?? '',
         ]);
@@ -955,7 +956,7 @@ class BxDolAIQuery extends BxDolDb
     public function getAlerts()
     {
         $aValues = [];
-        $aAlerts = $this->getAll("SELECT `unit`, `action` FROM `sys_alerts_log` ORDER BY `unit`, `action`");
+        $aAlerts = $this->getAll("SELECT `unit`, `action`, `counter_24h` FROM `sys_alerts_log` ORDER BY `unit`, `action`");
         foreach ($aAlerts as $a) {
             $sKey = $a['unit'] . ':' . $a['action'];
             $aValues[$sKey] = [
@@ -964,20 +965,24 @@ class BxDolAIQuery extends BxDolDb
                 'action' => $a['action'],
                 'name' => $a['unit'] . ' - ' . $a['action'],
                 'desc' => $this->getAlertDesc($a['unit'] . ':' . $a['action']),
+                'counter_24h' => $a['counter_24h'],
             ];
         }
         return $aValues;
     }
 
-    public function getAlertDesc($sAlert) 
+    static public function getAlertDesc($sAlert) 
     {
+        if (!$sAlert)
+            return '';
+        $oDb = BxDolDb::getInstance();
         [$sUnit, $sAction] = explode(':', $sAlert);
-        $sDesc = $this->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` = :unit AND `action` = :action LIMIT 1", [
+        $sDesc = $oDb->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` = :unit AND `action` = :action LIMIT 1", [
             'unit' => $sUnit,
             'action' => $sAction,
         ]);
         if (!$sDesc) {
-            $this->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` LIKE '{%}' AND `action` = :action LIMIT 1", [
+            $oDb->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` LIKE '{%}' AND `action` = :action LIMIT 1", [
                 'action' => $sAction,
             ]);
         }

+ 0 - 1
inc/classes/BxDolAIToolContentSearch.php

@@ -56,7 +56,6 @@ class BxDolAIToolContentSearch extends BxDolAITool
 
     public function __invoke(string $keyword, array $sections, ?int $limit = 5): array
     {
-echoDbgLog("Invoking content search tool with keyword: {$keyword}, sections: " . implode(',', $sections) . ", limit: {$limit}");
         $a = bx_srv('system', 'search', [$keyword, $sections, $limit], 'TemplServiceContent');
         if (!$a)
             return ["error" => "No content items found"];

+ 35 - 6
inc/classes/BxDolAiAgent.php

@@ -38,14 +38,27 @@ class BxDolAiAgent extends RAG
 
     protected function instructions(): string
     {
-        $aToolsUsage = !empty($this->aAgent['prompt_tools']) ? [$this->aAgent['prompt_tools']] : [];
-        $aToolsUsage[] = "Always use agent profile id = {$this->aAgent['profile_id']} as author ('profile_id' tool parameter), unless explicitly specified in the user instructions, no other variants strictly.";
+        $aPromptSystem = !empty($this->aAgent['prompt_system']) ? [$this->aAgent['prompt_system']] : [];
+        $aPromptSteps = !empty($this->aAgent['prompt_steps']) ? [$this->aAgent['prompt_steps']] : [];
+        $aPromptTools = !empty($this->aAgent['prompt_tools']) ? [$this->aAgent['prompt_tools']] : [];
+        $aPromptOutput = !empty($this->aAgent['prompt_output']) ? [$this->aAgent['prompt_output']] : [];
+
+        $aPromptTools[] = "Always use agent profile id = {$this->aAgent['profile_id']} as author ('profile_id' tool parameter), unless explicitly specified in the user instructions, no other variants strictly.";
+
+        if ('alert' == $this->aAgent['trigger']) {
+            $aPromptTools[] = "Return array only, modified version of 'extra' array. Modifyable keys: " . $this->getAlertTriggerModifyableKeys() . ".";             
+
+            $sDesc = trim(BxDolAiQuery::getAlertDesc($this->aAgent['alert']));
+            if ('.' != mb_substr($sDesc, -1))
+                $sDesc .= '.';
+            $aPromptSystem[] = $sDesc;
+        }
 
         $oPrompt = new NeuronAI\Agent\SystemPrompt(
-            background: [$this->aAgent['prompt_system']],
-            steps: !empty($this->aAgent['prompt_steps']) ? [$this->aAgent['prompt_steps']] : [],
-            output: !empty($this->aAgent['prompt_output']) ? [$this->aAgent['prompt_output']] : [],
-            toolsUsage: $aToolsUsage
+            background: $aPromptSystem,
+            steps: $aPromptSteps,
+            output: $aPromptOutput,
+            toolsUsage: $aPromptTools
         );
         return (string) $oPrompt;
     }
@@ -101,4 +114,20 @@ class BxDolAiAgent extends RAG
             $s .=  ':' . $this->aParams['chat_history_subindex'];
         return $s;
     }
+
+    protected function getAlertTriggerModifyableKeys(): string
+    {
+        if ('alert' != $this->aAgent['trigger'])
+            return 'none';
+
+        $aAlert = BxDolAiQuery::getAlert($this->aAgent['alert']);
+        if (!$aAlert)
+            return 'none';
+
+        $aKeys = json_decode($aAlert['extra_refs']);
+        if (!$aKeys)
+            return 'none';
+
+        return implode(',', $aKeys);
+    }
 }

+ 20 - 12
inc/classes/BxDolAlerts.php

@@ -152,14 +152,25 @@ class BxDolAlerts extends BxDol
         $oAi = BxDolAI::getInstance();
         if($aAgents = $oAi->getAgentsByAlertUnitAndAction($this->sUnit, $this->sAction)) {
             foreach($aAgents as $a) {
-                $oAi->callAgent('alert', $a, [
+                $sExtraJSON = $oAi->callAgent('alert', $a, [
                     'trigger' => 'alert',
-                    'object' => $this->iObject,
-                    'sender' => $this->iSender,
+                    'object_id' => $this->iObject,
+                    'sender_profile_id' => $this->iSender,
                     'unit' => $this->sUnit, 
                     'action' => $this->sAction,
-                    'extra' => $this->aExtras
+                    'extra' => $this->aExtras,
                 ]);
+
+                $aExtra = [];
+                if ($sExtraJSON && 0 === bx_mb_strpos($sExtraJSON, '{'))
+                    $aExtra = json_decode($sExtraJSON, true);
+                
+                if ($aExtra && is_array($aExtra)) {
+                    foreach ($aExtra as $k => $v) {
+                        if (isset($this->aExtras[$k]))
+                            $this->aExtras[$k] = $v;
+                    }
+                }
             }
         }
 
@@ -230,20 +241,17 @@ class BxDolAlerts extends BxDol
         $oDb->query("
             INSERT INTO sys_alerts_log
             SET
-                unit  = :unit,
+                unit = :unit,
                 action = :action,
                 object = :object,
                 sender = :sender,
-                extra  = :extra,
-                extra_refs  = :extra_refs,
-                ts     = UNIX_TIMESTAMP(),
+                extra = :extra,
+                extra_refs = :extra_refs,
+                ts = UNIX_TIMESTAMP(),
                 counter_total = 1,
                 counter_24h = 1
             ON DUPLICATE KEY UPDATE
-                object = :object,
-                sender = :sender,
-                extra  = :extra,
-                ts     = UNIX_TIMESTAMP(),
+                ts = UNIX_TIMESTAMP(),
                 counter_total = counter_total + 1,
                 counter_24h = counter_24h + 1
         ", $aBind);

+ 1 - 0
modules/boonex/english/data/langs/system/en.xml

@@ -2283,6 +2283,7 @@
     <string name="_sys_agents_automators_btn_tune"><![CDATA[Tune]]></string>
     <string name="_sys_agents_automators_btn_approve"><![CDATA[Approve]]></string>
     <string name="_sys_agents_automators_popup_add"><![CDATA[Add automator]]></string>
+    <string name="_sys_agents_count_today"><![CDATA[{0} calls today]]></string>
     <string name="_sys_agents_popup_edit"><![CDATA[Edit automator]]></string>
     <string name="_sys_agents_field_name"><![CDATA[Name]]></string>
     <string name="_sys_agents_field_name_info"><![CDATA[Uniq agent name]]></string>

+ 1 - 0
modules/boonex/russian/data/langs/system/ru.xml

@@ -2259,6 +2259,7 @@
         <string name="_sys_agents_automators_btn_tune"><![CDATA[Настройка]]></string>
         <string name="_sys_agents_automators_btn_approve"><![CDATA[Одобрить]]></string>
         <string name="_sys_agents_automators_popup_add"><![CDATA[Добавить автоматизатор]]></string>
+        <string name="_sys_agents_count_today"><![CDATA[{0} вызовов сегодня]]></string>
         <string name="_sys_agents_popup_edit"><![CDATA[Редактировать автоматизатор]]></string>
         <string name="_sys_agents_field_name"><![CDATA[Название]]></string>
         <string name="_sys_agents_field_name_info"><![CDATA[Уникальное название агента.]]></string>

+ 2 - 2
package.json

@@ -5,7 +5,7 @@
     "@claviska/jquery-minicolors": "^2.3.6",
     "@egjs/hammerjs": "^2.0.17",
     "@fullcalendar/core": "^6.1.20",
-    "@stripe/connect-js": "^3.3.35",
+    "@stripe/connect-js": "^3.3.38",
     "at.js": "^1.5.4",
     "autosize": "^6.0.1",
     "chart.js": "^4.5.1",
@@ -56,7 +56,7 @@
     "marka": "github:fians/marka",
     "masonry-layout": "^4.2.2",
     "moment": "^2.30.1",
-    "moment-timezone": "^0.6.1",
+    "moment-timezone": "^0.6.2",
     "npm": "^11.13.0",
     "ovenplayer": "^0.10.49",
     "pdfjs-dist": "^5.6.205",

+ 5 - 1
studio/template/agents_agents_alerts_select.html

@@ -1,7 +1,7 @@
 <div id="bx-agents-agents-alert-cont" style="overflow:visible;">
 <select id="bx-agents-agents-alert" name="alert" class="bx-def-font-inputs bx-form-input-select">
     <bx_repeat:alerts>
-        <option value="__value__" <bx_if:sel>__sel__</bx_if:sel> data-desc="__desc__">__name__</option>
+        <option value="__value__" <bx_if:sel>__sel__</bx_if:sel> data-desc="__desc__" data-count="__count__">__name__</option>
     </bx_repeat:alerts>
 </select>
 </div>
@@ -10,11 +10,15 @@
         if (!state.id) 
             return state.text;
         const desc = $(state.element).data('desc');
+        const count = $(state.element).data('count');
         return $(`
+        <div class="bx-alert-wrap">
             <div>
                 <div class="bx-alert-text">${state.text}</div>
                 <div class="bx-alert-desc">${desc || ''}</div>
             </div>
+            <div class="bx-alert-counter">${count}</div>
+        </div>
         `);
     }
     $('#bx-agents-agents-alert').select2({

+ 12 - 0
studio/template/css/agents.css

@@ -129,6 +129,12 @@
     height: 30px;
 }
 
+.select2-results__option .bx-alert-wrap {
+    display:flex;
+    justify-content:space-between;
+    gap:10px;
+    width:100%;
+}
 .select2-results__option .bx-alert-text {
     font-weight:600;
 }
@@ -136,3 +142,9 @@
     font-size:12px;
     color:#888;
 }
+.select2-results__option .bx-alert-counter {
+    padding:2px 8px;
+    border-radius:10px;
+    font-size:12px;
+    height:fit-content;
+}

+ 2 - 1
studio/template/scripts/BxBaseStudioAgentsAgents.php

@@ -375,7 +375,7 @@ class BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
                         'text' => _t('_sys_agents_waiting_for_sample_data'),
 
                         'payload-visibility' => !empty($aAgent['alert']) ? 'block' : 'none',
-                        'desc' => $this->_oDb->getAlertDesc($aAgent['alert']),
+                        'desc' => $aAgent ? $this->_oDb->getAlertDesc($aAgent['alert']) : '',
                         'payload' => !empty($aAgent['alert']) ? json_encode($this->_getAlertPayload($aAgent['alert']), JSON_PRETTY_PRINT) : '',
                     ]),
                 ],
@@ -585,6 +585,7 @@ class BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
                 'name' => $r['name'],
                 'value' => $r['key'],
                 'desc' => bx_html_attribute($r['desc']),
+                'count' => bx_html_attribute(_t('_sys_agents_count_today', $r['counter_24h'])),
                 'bx_if:sel' => [
                     'condition' => $sValue == $r['key'],
                     'content' => [

+ 11 - 11
yarn.lock

@@ -564,10 +564,10 @@
     "@sigstore/core" "^3.0.0"
     "@sigstore/protobuf-specs" "^0.5.0"
 
-"@stripe/connect-js@^3.3.35":
-  version "3.3.35"
-  resolved "https://registry.yarnpkg.com/@stripe/connect-js/-/connect-js-3.3.35.tgz#5d9943f670acf893715ab89a11844561012fdaea"
-  integrity sha512-9qGDe+G5MWPHdFK05lBRX+wjHIafZ72hCNrXzXnM4Uf7X/rbRe04/EOgaur0/aza5UlDcmEWJ+LSNmeoxytX6w==
+"@stripe/connect-js@^3.3.38":
+  version "3.3.38"
+  resolved "https://registry.yarnpkg.com/@stripe/connect-js/-/connect-js-3.3.38.tgz#e0945c7c375c62ae2cd980d83c7277e644772011"
+  integrity sha512-CnhJ8xzzUZD6/eHFDio/8Nea8U0tFicCkfWfP4x0VGq2JY8uS8t5O500NaHSajvQcPxQCrbG+/6xnM/8G9FLrA==
 
 "@svta/cml-608@1.0.1":
   version "1.0.1"
@@ -2521,10 +2521,10 @@ minizlib@^3.0.1, minizlib@^3.1.0:
   dependencies:
     minipass "^7.1.2"
 
-moment-timezone@^0.6.1:
-  version "0.6.1"
-  resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.6.1.tgz#c324a71a8285583eb679ced4ba3dcfa53fdce500"
-  integrity sha512-1B9lmAhB9D9/sHaPC1N7wLFEVUoFldxOpOO96lOD1PvJ43vCd0ozDPbu0FEL3++VvawOlDkq8YD373tJmP5JHw==
+moment-timezone@^0.6.2:
+  version "0.6.2"
+  resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.6.2.tgz#a704cca03ee502f0df6edd42efcdcc61ee03190e"
+  integrity sha512-lDsQv8FoGdBUdf0+TjGsq2orxKuXdwFlQ6Zw6TX3xIcTwTfEpCLyKqvEauvCHJ8iu3KBV8+uPhlv70YsNGdUBQ==
   dependencies:
     moment "^2.29.4"
 
@@ -3039,9 +3039,9 @@ postcss-value-parser@^4.0.0:
   integrity sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==
 
 postcss@^8.4.47:
-  version "8.5.6"
-  resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.5.6.tgz#2825006615a619b4f62a9e7426cc120b349a8f3c"
-  integrity sha512-3Ybi1tAuwAP9s0r1UQ2J4n5Y0G05bJkpUIO0/bI9MhwmD70S5aTWbXGBwxHrelT+XM1k6dM0pk+SwNkpTRN7Pg==
+  version "8.5.12"
+  resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.5.12.tgz#cd0c0f667f7cb0521e2313234ea6e707a9ec1ddb"
+  integrity sha512-W62t/Se6rA0Az3DfCL0AqJwXuKwBeYg6nOaIgzP+xZ7N5BFCI7DYi1qs6ygUYT6rvfi6t9k65UMLJC+PHZpDAA==
   dependencies:
     nanoid "^3.3.11"
     picocolors "^1.1.1"