Add admin views for quick replies, settings, and ticket details

- 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.
This commit is contained in:
SitiWeb
2026-04-30 01:50:21 +02:00
parent 01aa115a49
commit f939133fe0
103 changed files with 4721 additions and 245 deletions

View File

@@ -0,0 +1,47 @@
<?php
namespace App\Services\Tools;
use InvalidArgumentException;
class DomainInfoTool
{
public const ACTION = 'domain_inf';
public function __construct(private readonly OxxaClient $client) {}
public function execute(array $parameters, array $credentials): array
{
$parameters = $this->validateParameters($parameters);
return $this->client->request(self::ACTION, [
'apiuser' => (string) ($credentials['apiuser'] ?? ''),
'apipassword' => (string) ($credentials['apipassword'] ?? ''),
'sld' => $parameters['sld'],
'tld' => $parameters['tld'],
]);
}
public function validateParameters(array $parameters): array
{
$sld = mb_strtolower(trim((string) ($parameters['sld'] ?? '')));
$tld = mb_strtolower(trim((string) ($parameters['tld'] ?? '')));
if ($sld === '' || $tld === '') {
throw new InvalidArgumentException('domain_inf requires both sld and tld parameters.');
}
if (preg_match('/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/', $sld) !== 1) {
throw new InvalidArgumentException('domain_inf parameter sld is invalid.');
}
if (preg_match('/^[a-z0-9-]{2,63}(?:\.[a-z0-9-]{2,63})*$/', $tld) !== 1) {
throw new InvalidArgumentException('domain_inf parameter tld is invalid.');
}
return [
'sld' => $sld,
'tld' => $tld,
];
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace App\Services\Tools;
use Illuminate\Support\Facades\Http;
use InvalidArgumentException;
use RuntimeException;
use SimpleXMLElement;
class OxxaClient
{
public function request(string $command, array $data = []): array|SimpleXMLElement
{
$endpoint = rtrim((string) config('services.oxxa.endpoint'), '?&');
if ($endpoint === '') {
throw new InvalidArgumentException('OXXA_API_ENDPOINT is not configured.');
}
if (! isset($data['apiuser'], $data['apipassword'])) {
throw new InvalidArgumentException('Oxxa credentials are required.');
}
$payload = $data;
$payload['apipassword'] = 'MD5'.md5((string) $payload['apipassword']);
$response = Http::timeout((int) config('services.oxxa.timeout', 60))
->get($endpoint, ['command' => $command] + $payload)
->throw();
$xml = simplexml_load_string((string) $response->body(), SimpleXMLElement::class, LIBXML_NOCDATA);
if (! $xml instanceof SimpleXMLElement) {
throw new RuntimeException('Oxxa returned invalid XML.');
}
$responseData = $this->xmlToArray($xml);
$statusCode = trim((string) data_get($responseData, 'order.status_code', ''));
$statusDescription = trim((string) data_get($responseData, 'order.status_description', ''));
if ($statusCode === 'XMLERR 1') {
return [
'ok' => false,
'error' => 'Credentials are incorrect. Please change the credentials in the registrar module.',
'status_code' => $statusCode,
'status_description' => $statusDescription,
];
}
if (str_contains($statusCode, 'XMLOK')) {
return [
'ok' => true,
'status_code' => $statusCode,
'status_description' => $statusDescription,
'data' => $responseData,
];
}
return [
'ok' => false,
'error' => trim($statusCode.' '.$statusDescription),
'status_code' => $statusCode,
'status_description' => $statusDescription,
'data' => $responseData,
];
}
private function xmlToArray(SimpleXMLElement $xml): array
{
return json_decode(json_encode($xml, JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR);
}
}

View File

@@ -0,0 +1,117 @@
<?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;
}
}