- Created `quick-replies.blade.php` for managing quick replies. - Added `settings.blade.php` for admin settings management. - Implemented `ticket-show.blade.php` to display ticket details. - Introduced `timeline-card.blade.php` component for displaying timeline information. Enhance quick reply management functionality - Developed `quick-reply-manager.blade.php` for creating and editing quick replies. - Integrated Livewire for dynamic interaction and validation. Implement settings page for AI configuration - Created `settings-page.blade.php` for managing AI settings, including prompts and provider instances. - Added functionality for managing models and embeddings. Add ticket show functionality with real-time updates - Implemented ticket details view with processing status and tool call logs. - Added support for displaying article suggestions and error messages. Create unit tests for AI classifier and domain info tool - Added `AIClassifierServiceTest.php` to validate AI classifier functionality. - Implemented `DomainInfoToolTest.php` for domain parameter validation. - Created `OxxaClientTest.php` to test API interactions and password hashing.
118 lines
4.1 KiB
PHP
118 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Tools;
|
|
|
|
use App\Models\Article;
|
|
use App\Models\Ticket;
|
|
use App\Models\TicketToolCall;
|
|
use App\Services\TicketProcessingLoggerService;
|
|
use Throwable;
|
|
|
|
class TicketToolCallService
|
|
{
|
|
public function __construct(
|
|
private readonly DomainInfoTool $domainInfoTool,
|
|
private readonly TicketProcessingLoggerService $logger,
|
|
) {}
|
|
|
|
public function executeRequestedTool(Ticket $ticket, ?Article $article, ?array $toolCall): ?TicketToolCall
|
|
{
|
|
if ($toolCall === null || $article === null) {
|
|
return null;
|
|
}
|
|
|
|
$action = (string) ($toolCall['action'] ?? '');
|
|
$parameters = is_array($toolCall['parameters'] ?? null) ? $toolCall['parameters'] : [];
|
|
$allowedActions = $article->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;
|
|
}
|
|
}
|