romanlesnikov 2 недель назад
Родитель
Сommit
0d3549497a

+ 6 - 2
inc/classes/BxDolAIToolChatButtons.php

@@ -21,7 +21,7 @@ class BxDolAIToolChatButtons extends BxDolAITool
 
         parent::__construct(
             'chat_buttons',
-            'Show clickable buttons under THIS assistant reply: ' . $sYes . '/' . $sNo . ', suggested names, register/support links. Call once per turn, after the sentence the user should confirm. Write every label in the site language. A yes/no choice uses exactly these labels: "' . $sYes . '" and "' . $sNo . '". Do not write another assistant message after this tool. Do not say that buttons are ready.',
+            'Show clickable buttons under THIS assistant reply: ' . $sYes . '/' . $sNo . ', suggested names, register/support links. Call once per turn, only AFTER you wrote the visible sentence the user should answer (never with an empty reply). Write every label in the site language. A yes/no choice uses exactly these labels: "' . $sYes . '" and "' . $sNo . '". Do not write another assistant message after this tool. Do not say that buttons are ready.',
         );
     }
 
@@ -51,7 +51,11 @@ class BxDolAIToolChatButtons extends BxDolAITool
 
     public function __invoke($buttons): string
     {
-        $i = BxDolAiChat::getInstance()->setPendingChatActions(is_array($buttons) ? $buttons : []);
+        $oChat = BxDolAiChat::getInstance();
+        if (!$oChat->hasTurnText())
+            return 'Not shown: buttons need a visible sentence above them. First write the reply text (question or offer) as a normal assistant message, then call chat_buttons again.';
+
+        $i = $oChat->setPendingChatActions(is_array($buttons) ? $buttons : []);
 
         return $i > 0
             ? 'Buttons are already under your last sentence. Do not send another assistant message this turn. Wait for the user to tap a button.'

+ 18 - 0
inc/classes/BxDolAiChat.php

@@ -19,6 +19,12 @@ class BxDolAiChat
      */
     protected $_aPendingChatActions = [];
 
+    /**
+     * The assistant already wrote visible text this turn: `chat_buttons` refuses
+     * to hang buttons on an empty reply.
+     */
+    protected $_bTurnHasText = false;
+
     public static function getInstance()
     {
         if (!isset($GLOBALS['bxDolClasses'][__CLASS__]))
@@ -259,6 +265,18 @@ class BxDolAiChat
     public function resetPendingChatActions()
     {
         $this->_aPendingChatActions = [];
+        $this->_bTurnHasText = false;
+    }
+
+    public function markTurnText($sDelta)
+    {
+        if (trim((string)$sDelta) !== '')
+            $this->_bTurnHasText = true;
+    }
+
+    public function hasTurnText()
+    {
+        return $this->_bTurnHasText;
     }
 
     protected function normalizeChatButtonsInput($mixed)

+ 1 - 0
inc/classes/BxDolAiTriggerChat.php

@@ -584,6 +584,7 @@ class BxDolAiTriggerChat extends BxDolAiTrigger
         if ($sType === 'TEXT_MESSAGE_CONTENT') {
             $sDelta = (string)($aPayload['delta'] ?? '');
             $aState['buf'] .= $sDelta;
+            BxDolAiChat::getInstance()->markTurnText($sDelta);
             if ($aState['mode'] === 'text') {
                 $fEmit($sEvent);
                 return;