BxDolAIToolChatButtons.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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); 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('message', PropertyType::STRING, 'For form. Sent as the visitor message after the form is saved, in the site language, e.g. "DigitalOcean is connected".', false),
  41. ]
  42. ),
  43. minItems: 1,
  44. maxItems: 8
  45. ),
  46. ];
  47. }
  48. public function __invoke($buttons): string
  49. {
  50. $oChat = BxDolAiChat::getInstance();
  51. if (!$oChat->hasTurnText())
  52. 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.';
  53. $i = $oChat->setPendingChatActions(is_array($buttons) ? $buttons : []);
  54. return $i > 0
  55. ? 'Buttons are already under your last sentence. Do not send another assistant message this turn. Wait for the user to tap a button.'
  56. : 'ignored';
  57. }
  58. }