allowed_actions ?? []; if (! in_array($action, $allowedActions, true)) { return $this->recordSkipped($ticket, $article, $action, $parameters, 'Action is not allowed for the selected article.'); } if ($action !== DomainInfoTool::ACTION) { return $this->recordSkipped($ticket, $article, $action, $parameters, 'Unsupported tool action.'); } try { $parameters = $this->domainInfoTool->validateParameters($parameters); } catch (Throwable $e) { return $this->recordSkipped($ticket, $article, $action, $parameters, $e->getMessage()); } $credentials = $ticket->api_credentials; if (! is_array($credentials) || empty($credentials['apiuser']) || empty($credentials['apipassword'])) { return $this->recordSkipped($ticket, $article, $action, $parameters, 'API credentials are missing for this ticket.'); } $record = TicketToolCall::query()->create([ 'ticket_id' => $ticket->id, 'article_id' => $article->id, 'action' => $action, 'status' => 'running', 'parameters' => $parameters, ]); $this->logger->log($ticket, 'tool_call', 'info', 'Toolcall gestart.', [ 'tool_call_id' => $record->id, 'action' => $action, 'parameters' => $parameters, ]); try { $response = $this->domainInfoTool->execute($parameters, $credentials); $status = ($response['ok'] ?? false) === true ? 'success' : 'failed'; $record->update([ 'status' => $status, 'response' => $response, 'error' => $status === 'failed' ? (string) ($response['error'] ?? 'Tool returned an error.') : null, 'executed_at' => now(), ]); $this->logger->log($ticket, 'tool_call', $status, 'Toolcall afgerond.', [ 'tool_call_id' => $record->id, 'action' => $action, 'parameters' => $parameters, 'response' => $response, ]); } catch (Throwable $e) { $record->update([ 'status' => 'failed', 'error' => $e->getMessage(), 'executed_at' => now(), ]); $this->logger->log($ticket, 'tool_call', 'error', 'Toolcall gefaald.', [ 'tool_call_id' => $record->id, 'action' => $action, 'parameters' => $parameters, 'error' => $e->getMessage(), ]); } return $record->refresh(); } private function recordSkipped(Ticket $ticket, Article $article, string $action, array $parameters, string $reason): TicketToolCall { $record = TicketToolCall::query()->create([ 'ticket_id' => $ticket->id, 'article_id' => $article->id, 'action' => $action !== '' ? $action : 'unknown', 'status' => 'skipped', 'parameters' => $parameters, 'error' => $reason, 'executed_at' => now(), ]); $this->logger->log($ticket, 'tool_call', 'warning', 'Toolcall overgeslagen.', [ 'tool_call_id' => $record->id, 'action' => $record->action, 'parameters' => $parameters, 'reason' => $reason, ]); return $record; } }