BxDolAIToolChatArtifactSave.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. class BxDolAIToolChatArtifactSave extends BxDolAITool
  12. {
  13. public function __construct()
  14. {
  15. parent::__construct(
  16. 'chat_artifact_save',
  17. 'Save one collected field from this conversation (for example name, email, group_name). Call as soon as the visitor gives a value. Do not print saved values to the visitor.',
  18. );
  19. }
  20. protected function properties(): array
  21. {
  22. return [
  23. new ToolProperty(
  24. name: 'field_name',
  25. type: PropertyType::STRING,
  26. description: 'Field key, e.g. name, email, group_name.',
  27. required: true
  28. ),
  29. new ToolProperty(
  30. name: 'field_value',
  31. type: PropertyType::STRING,
  32. description: 'Field value as given by the visitor.',
  33. required: true
  34. ),
  35. ];
  36. }
  37. public function __invoke(string $sFieldName, string $sFieldValue): string
  38. {
  39. $sName = strtolower(trim((string)$sFieldName));
  40. $sName = preg_replace('/[^a-z0-9_]/', '', $sName);
  41. $sValue = trim((string)$sFieldValue);
  42. if ($sName === '' || $sValue === '')
  43. return 'ignored';
  44. $iHistoryId = (int)BxDolAiChat::getInstance()->getCurrentChatHistoryId();
  45. if ($iHistoryId <= 0)
  46. return 'no_history';
  47. BxDolDb::getInstance()->query("
  48. INSERT INTO `sys_agents_chat_artifacts`
  49. (`history_id`, `field_name`, `field_value`, `updated_at`)
  50. VALUES
  51. (:h, :n, :v, :t)
  52. ON DUPLICATE KEY UPDATE
  53. `field_value` = VALUES(`field_value`),
  54. `updated_at` = VALUES(`updated_at`)
  55. ", [
  56. 'h' => $iHistoryId,
  57. 'n' => $sName,
  58. 'v' => $sValue,
  59. 't' => time(),
  60. ]);
  61. return 'ok';
  62. }
  63. }