1
0

BxDolAIToolChatButtons.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. use NeuronAI\Tools\ArrayProperty;
  12. use NeuronAI\Tools\ObjectProperty;
  13. class BxDolAIToolChatButtons extends BxDolAITool
  14. {
  15. public function __construct()
  16. {
  17. $sYes = _t('_Yes');
  18. $sNo = _t('_No');
  19. parent::__construct(
  20. 'chat_buttons',
  21. '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.',
  22. );
  23. }
  24. protected function properties(): array
  25. {
  26. $sYes = _t('_Yes');
  27. $sNo = _t('_No');
  28. return [
  29. new ArrayProperty(
  30. name: 'buttons',
  31. description: 'Buttons under this reply. reply = visitor tap sends that label as the next message; link = an https URL (site pages, help, registration). Yes/no labels are exactly "' . $sYes . '" and "' . $sNo . '" (site language). Example: [{"type":"reply","label":"' . $sYes . '"},{"type":"reply","label":"' . $sNo . '"}]',
  32. required: true,
  33. items: new ObjectProperty(
  34. name: 'button',
  35. properties: [
  36. new ToolProperty('type', PropertyType::STRING, 'reply or link', true),
  37. new ToolProperty('label', PropertyType::STRING, 'Button text shown to the visitor', true),
  38. new ToolProperty('url', PropertyType::STRING, 'Required for link. https URL.', false),
  39. ]
  40. ),
  41. minItems: 1,
  42. maxItems: 8
  43. ),
  44. ];
  45. }
  46. public function __invoke($buttons): string
  47. {
  48. $oChat = BxDolAiChat::getInstance();
  49. if (!$oChat->hasTurnText())
  50. 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.';
  51. $i = $oChat->setPendingChatActions(is_array($buttons) ? $buttons : []);
  52. return $i > 0
  53. ? 'Buttons are already under your last sentence. Do not send another assistant message this turn. Wait for the user to tap a button.'
  54. : 'ignored';
  55. }
  56. }