BxDolAiActivityObserver.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. * NeuronAI observer that turns every write tool call of an agent into a
  7. * BxDolAiActivity row. Attached in BxDolAiAgent alongside the debug LogObserver.
  8. *
  9. * @defgroup UnaCore UNA Core
  10. * @{
  11. */
  12. use NeuronAI\Observability\ObserverInterface;
  13. class BxDolAiActivityObserver implements ObserverInterface
  14. {
  15. public function __construct(protected array $aAgent, protected array $aParams = [])
  16. {
  17. }
  18. public function onEvent(string $event, object $source, mixed $data = null, ?string $branchId = null): void
  19. {
  20. if ($event !== 'tool-called' || !is_object($data))
  21. return;
  22. $oTool = $data->tool ?? null;
  23. if (!is_object($oTool) || !method_exists($oTool, 'getName'))
  24. return;
  25. try {
  26. $aInputs = method_exists($oTool, 'getInputs') ? (array)$oTool->getInputs() : [];
  27. $mixedResult = method_exists($oTool, 'getResult') ? $oTool->getResult() : null;
  28. BxDolAiActivity::record($this->aAgent, $this->aParams, (string)$oTool->getName(), $aInputs, $mixedResult);
  29. } catch (Throwable $o) {
  30. bx_log('sys_agents', 'activity observer: ' . $o->getMessage(), BX_LOG_WARN);
  31. }
  32. }
  33. }
  34. /** @} */