Wire admin studio SSR and search infrastructure

This commit is contained in:
2026-05-01 11:46:06 +02:00
parent 257b0dbef6
commit 18cea8b0f0
329 changed files with 197465 additions and 2741 deletions

View File

@@ -7,8 +7,9 @@ namespace App\Services\Vision;
use App\Models\Artwork;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Http;
use RuntimeException;
use Throwable;
final class AiArtworkVectorSearchService
{
@@ -35,12 +36,7 @@ final class AiArtworkVectorSearchService
$ttl = max(60, (int) config('recommendations.ttl.similar_artworks', 30 * 60));
return Cache::remember($cacheKey, $ttl, function () use ($artwork, $safeLimit): array {
$url = $this->imageUrl->fromArtwork($artwork);
if ($url === null) {
return [];
}
$matches = $this->client->searchByUrl($url, $safeLimit + 1);
$matches = $this->searchMatchesForArtwork($artwork, $safeLimit + 1);
return $this->resolveMatches($matches, $safeLimit, $artwork->id);
});
@@ -52,27 +48,80 @@ final class AiArtworkVectorSearchService
public function searchByUploadedImage(UploadedFile $file, int $limit = 12): array
{
$safeLimit = max(1, min(self::MAX_SIMILAR_RESULTS, $limit));
$path = $file->store('ai-search/tmp', 'public');
$matches = $this->client->searchByUploadedFile($file, $safeLimit);
if (! is_string($path) || $path === '') {
throw new RuntimeException('Unable to persist uploaded image for vector search.');
return $this->resolveMatches($matches, $safeLimit);
}
/**
* @return array{contents: string, filename: string}|null
*/
private function downloadArtworkImage(Artwork $artwork, string $url): ?array
{
$response = Http::accept('*/*')
->connectTimeout(5)
->timeout(20)
->retry(1, 200, throw: false)
->get($url);
if (! $response->ok()) {
return null;
}
$publicBaseUrl = rtrim((string) config('filesystems.disks.public.url', ''), '/');
if ($publicBaseUrl === '') {
Storage::disk('public')->delete($path);
throw new RuntimeException('Public disk URL is not configured for vector search uploads.');
$contents = $response->body();
if ($contents === '') {
return null;
}
$url = $publicBaseUrl . '/' . ltrim($path, '/');
$ext = strtolower(ltrim((string) ($artwork->thumb_ext ?: 'webp'), '.'));
return [
'contents' => $contents,
'filename' => sprintf('artwork-%d.%s', (int) $artwork->id, $ext !== '' ? $ext : 'webp'),
];
}
/**
* @return list<array{id: int|string, score: float, metadata: array<string, mixed>}>
*/
private function searchMatchesForArtwork(Artwork $artwork, int $limit): array
{
$url = $this->imageUrl->fromArtwork($artwork);
if ($url === null || $url === '') {
return [];
}
$fileFailure = null;
try {
$matches = $this->client->searchByUrl($url, $safeLimit);
return $this->resolveMatches($matches, $safeLimit);
} finally {
Storage::disk('public')->delete($path);
$payload = $this->downloadArtworkImage($artwork, $url);
if ($payload !== null) {
return $this->client->searchByFileContents($payload['contents'], $payload['filename'], $limit);
}
} catch (Throwable $e) {
$fileFailure = $e;
}
try {
return $this->client->searchByUrl($url, $limit);
} catch (Throwable $e) {
throw $this->normalizeSearchFailure($fileFailure, $e);
}
}
private function normalizeSearchFailure(?Throwable $fileFailure, Throwable $fallbackFailure): RuntimeException
{
if ($fileFailure === null) {
return $fallbackFailure instanceof RuntimeException
? $fallbackFailure
: new RuntimeException($fallbackFailure->getMessage(), 0, $fallbackFailure);
}
return new RuntimeException(sprintf(
'Vector search failed via file endpoint (%s) and URL fallback (%s).',
$fileFailure->getMessage(),
$fallbackFailure->getMessage(),
), 0, $fallbackFailure);
}
/**
@@ -126,7 +175,7 @@ final class AiArtworkVectorSearchService
'id' => $artwork->id,
'title' => $artwork->title,
'slug' => $artwork->slug,
'thumb' => $artwork->thumbUrl('md'),
'thumb' => $artwork->thumbUrl('sm'),
'url' => '/art/' . $artwork->id . '/' . $artwork->slug,
'author' => $artwork->user?->name ?? 'Artist',
'author_avatar' => $artwork->user?->profile?->avatar_url,