BxDolAIToolChatButtons.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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), with check_url and message from a tool result the chat waits for it and sends message by itself; form = opens a site form in a popup over the chat, form_url comes from a tool result only (never made up), after the form is saved the "message" text is sent as the visitor next message. 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, link or form', 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. new ToolProperty('form_url', PropertyType::STRING, 'Required for form. Exactly the form_url from a tool result, like "module/method/&params[]=1".', false),
  40. new ToolProperty('check_url', PropertyType::STRING, 'Optional for link, together with message. Exactly the check_url from a tool result, like "module/method/&params[]=1": the chat polls it after the link is opened and sends message once it is done.', false),
  41. new ToolProperty('message', PropertyType::STRING, 'For form, and for link with check_url. Sent as the visitor message after the form is saved or the check_url reports done, in the site language, e.g. "DigitalOcean is connected".', false),
  42. ]
  43. ),
  44. minItems: 1,
  45. maxItems: 8
  46. ),
  47. ];
  48. }
  49. public function __invoke($buttons): string
  50. {
  51. $oChat = BxDolAiChat::getInstance();
  52. if (!$oChat->hasTurnText())
  53. 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.';
  54. $i = $oChat->setPendingChatActions(is_array($buttons) ? $buttons : []);
  55. return $i > 0
  56. ? 'Buttons are already under your last sentence. Do not send another assistant message this turn. Wait for the user to tap a button.'
  57. : 'ignored';
  58. }
  59. }