Save workspace changes

This commit is contained in:
2026-04-18 17:02:56 +02:00
parent f02ea9a711
commit 87d60af5a9
4220 changed files with 1388603 additions and 1554 deletions

View File

@@ -0,0 +1,143 @@
<?php
declare(strict_types=1);
namespace App\Services\Vision;
use App\Models\Artwork;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use RuntimeException;
final class AiArtworkVectorSearchService
{
public function __construct(
private readonly VectorGatewayClient $client,
private readonly ArtworkVisionImageUrl $imageUrl,
) {
}
public function isConfigured(): bool
{
return $this->client->isConfigured();
}
/**
* @return list<array<string, mixed>>
*/
public function similarToArtwork(Artwork $artwork, int $limit = 12): array
{
$safeLimit = max(1, min(24, $limit));
$cacheKey = sprintf('rec:artwork:%d:similar-ai:%d', $artwork->id, $safeLimit);
$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);
return $this->resolveMatches($matches, $safeLimit, $artwork->id);
});
}
/**
* @return list<array<string, mixed>>
*/
public function searchByUploadedImage(UploadedFile $file, int $limit = 12): array
{
$safeLimit = max(1, min(24, $limit));
$path = $file->store('ai-search/tmp', 'public');
if (! is_string($path) || $path === '') {
throw new RuntimeException('Unable to persist uploaded image for vector search.');
}
$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.');
}
$url = $publicBaseUrl . '/' . ltrim($path, '/');
try {
$matches = $this->client->searchByUrl($url, $safeLimit);
return $this->resolveMatches($matches, $safeLimit);
} finally {
Storage::disk('public')->delete($path);
}
}
/**
* @param list<array{id: int|string, score: float, metadata: array<string, mixed>}> $matches
* @return list<array<string, mixed>>
*/
private function resolveMatches(array $matches, int $limit, ?int $excludeArtworkId = null): array
{
$orderedIds = [];
$scores = [];
foreach ($matches as $match) {
$artworkId = (int) ($match['id'] ?? 0);
if ($artworkId <= 0) {
continue;
}
if ($excludeArtworkId !== null && $artworkId === $excludeArtworkId) {
continue;
}
if (isset($scores[$artworkId])) {
continue;
}
$orderedIds[] = $artworkId;
$scores[$artworkId] = (float) ($match['score'] ?? 0.0);
}
if ($orderedIds === []) {
return [];
}
$artworks = Artwork::query()
->whereIn('id', $orderedIds)
->public()
->published()
->with(['user:id,name', 'user.profile:user_id,avatar_hash'])
->get()
->keyBy('id');
$items = [];
foreach ($orderedIds as $artworkId) {
/** @var Artwork|null $artwork */
$artwork = $artworks->get($artworkId);
if ($artwork === null) {
continue;
}
$items[] = [
'id' => $artwork->id,
'title' => $artwork->title,
'slug' => $artwork->slug,
'thumb' => $artwork->thumbUrl('md'),
'url' => '/art/' . $artwork->id . '/' . $artwork->slug,
'author' => $artwork->user?->name ?? 'Artist',
'author_avatar' => $artwork->user?->profile?->avatar_url,
'author_id' => $artwork->user_id,
'score' => round((float) ($scores[$artworkId] ?? 0.0), 5),
'source' => 'vector_gateway',
];
if (count($items) >= $limit) {
break;
}
}
return $items;
}
}

View File

@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
namespace App\Services\Vision;
use Illuminate\Support\Facades\Http;
final class ArtworkEmbeddingClient
{
/**
* @return array<int, float>
*/
public function embed(string $imageUrl, int $artworkId, string $sourceHash): array
{
$base = trim((string) config('vision.clip.base_url', ''));
if ($base === '') {
return [];
}
$endpoint = (string) config('recommendations.embedding.endpoint', '/embed');
$url = rtrim($base, '/') . '/' . ltrim($endpoint, '/');
$timeout = (int) config('recommendations.embedding.timeout_seconds', 8);
$connectTimeout = (int) config('recommendations.embedding.connect_timeout_seconds', 2);
$retries = (int) config('recommendations.embedding.retries', 1);
$delay = (int) config('recommendations.embedding.retry_delay_ms', 200);
$response = Http::acceptJson()
->connectTimeout(max(1, $connectTimeout))
->timeout(max(1, $timeout))
->retry(max(0, $retries), max(0, $delay), throw: false)
->post($url, [
'image_url' => $imageUrl,
'artwork_id' => $artworkId,
'hash' => $sourceHash,
]);
if (! $response->ok()) {
return [];
}
return $this->extractEmbedding($response->json());
}
/**
* @param mixed $json
* @return array<int, float>
*/
private function extractEmbedding(mixed $json): array
{
$candidate = null;
if (is_array($json) && $this->isNumericVector($json)) {
$candidate = $json;
} elseif (is_array($json) && isset($json['embedding']) && is_array($json['embedding'])) {
$candidate = $json['embedding'];
} elseif (is_array($json) && isset($json['data']['embedding']) && is_array($json['data']['embedding'])) {
$candidate = $json['data']['embedding'];
}
if (! is_array($candidate) || ! $this->isNumericVector($candidate)) {
return [];
}
$vector = array_map(static fn ($value): float => (float) $value, $candidate);
$dim = count($vector);
$minDim = (int) config('recommendations.embedding.min_dim', 64);
$maxDim = (int) config('recommendations.embedding.max_dim', 4096);
if ($dim < $minDim || $dim > $maxDim) {
return [];
}
return $vector;
}
/**
* @param array<mixed> $arr
*/
private function isNumericVector(array $arr): bool
{
if ($arr === []) {
return false;
}
foreach ($arr as $value) {
if (! is_numeric($value)) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Services\Vision;
use App\Models\Artwork;
use RuntimeException;
final class ArtworkVectorIndexService
{
public function __construct(
private readonly VectorGatewayClient $client,
private readonly ArtworkVisionImageUrl $imageUrl,
private readonly ArtworkVectorMetadataService $metadata,
) {
}
public function isConfigured(): bool
{
return $this->client->isConfigured();
}
/**
* @return array{url: string, metadata: array<string, mixed>}
*/
public function payloadForArtwork(Artwork $artwork): array
{
$url = $this->imageUrl->fromArtwork($artwork);
if ($url === null || $url === '') {
throw new RuntimeException('No vision image URL could be generated for artwork ' . (int) $artwork->id . '.');
}
return [
'url' => $url,
'metadata' => $this->metadata->forArtwork($artwork),
];
}
/**
* @return array{url: string, metadata: array<string, mixed>}
*/
public function upsertArtwork(Artwork $artwork): array
{
$payload = $this->payloadForArtwork($artwork);
$this->client->upsertByUrl($payload['url'], (int) $artwork->id, $payload['metadata']);
$artwork->forceFill([
'last_vector_indexed_at' => now(),
])->save();
return $payload;
}
}

View File

@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace App\Services\Vision;
use App\Models\Artwork;
use App\Models\Category;
final class ArtworkVectorMetadataService
{
/**
* @return array{content_type: string, category: string, user_id: string, tags: list<string>, is_public: bool, is_deleted: bool, is_nsfw: bool, category_id: int, content_type_id: int, status: mixed}
*/
public function forArtwork(Artwork $artwork): array
{
$artwork->loadMissing([
'categories' => fn ($categories) => $categories->with('contentType')->orderBy('sort_order')->orderBy('name'),
'tags:id,slug',
]);
$category = $this->primaryCategory($artwork);
return [
'content_type' => (string) ($category?->contentType?->name ?? ''),
'category' => (string) ($category?->name ?? ''),
'user_id' => (string) ($artwork->user_id ?? ''),
'is_public' => (bool) $artwork->is_public,
'is_deleted' => $artwork->trashed(),
'is_nsfw' => (bool) $artwork->is_mature,
'category_id' => (int) ($category?->id ?? 0),
'content_type_id' => (int) ($category?->contentType?->id ?? 0),
'status' => $artwork->artwork_status,
'tags' => $artwork->tags
->pluck('slug')
->map(static fn (mixed $slug): string => trim((string) $slug))
->filter(static fn (string $slug): bool => $slug !== '')
->unique()
->values()
->all(),
];
}
private function primaryCategory(Artwork $artwork): ?Category
{
/** @var Category|null $category */
$category = $artwork->categories->sortBy('sort_order')->first();
return $category;
}
}

View File

@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace App\Services\Vision;
use App\Models\Artwork;
use App\Services\ThumbnailService;
final class ArtworkVisionImageUrl
{
public function fromArtwork(Artwork $artwork): ?string
{
return $this->fromHash(
(string) ($artwork->hash ?? ''),
(string) ($artwork->thumb_ext ?: 'webp')
);
}
public function fromHash(?string $hash, ?string $ext = 'webp', string $size = 'md'): ?string
{
$clean = strtolower((string) preg_replace('/[^a-z0-9]/', '', (string) $hash));
if ($clean === '') {
return null;
}
return ThumbnailService::fromHash($clean, $ext, $size);
}
}

View File

@@ -0,0 +1,213 @@
<?php
declare(strict_types=1);
namespace App\Services\Vision;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use RuntimeException;
final class VectorGatewayClient
{
public function isConfigured(): bool
{
return (bool) config('vision.vector_gateway.enabled', true)
&& $this->baseUrl() !== ''
&& $this->apiKey() !== '';
}
public function upsertByUrl(string $imageUrl, int|string $id, array $metadata = []): array
{
$response = $this->postJson(
$this->url((string) config('vision.vector_gateway.upsert_endpoint', '/vectors/upsert')),
[
'url' => $imageUrl,
'id' => (string) $id,
'metadata' => $metadata,
]
);
if ($response->failed()) {
throw new RuntimeException($this->failureMessage('Vector upsert', $response));
}
$json = $response->json();
return is_array($json) ? $json : [];
}
/**
* @return list<array{id: int|string, score: float, metadata: array<string, mixed>}>
*/
public function searchByUrl(string $imageUrl, int $limit = 5): array
{
$response = $this->postJson(
$this->url((string) config('vision.vector_gateway.search_endpoint', '/vectors/search')),
[
'url' => $imageUrl,
'limit' => max(1, $limit),
]
);
if ($response->failed()) {
throw new RuntimeException($this->failureMessage('Vector search', $response));
}
return $this->extractMatches($response->json());
}
public function deleteByIds(array $ids): array
{
$response = $this->postJson(
$this->url((string) config('vision.vector_gateway.delete_endpoint', '/vectors/delete')),
[
'ids' => array_values(array_map(static fn (int|string $id): string => (string) $id, $ids)),
]
);
if ($response->failed()) {
throw new RuntimeException($this->failureMessage('Vector delete', $response));
}
$json = $response->json();
return is_array($json) ? $json : [];
}
private function request(): PendingRequest
{
if (! $this->isConfigured()) {
throw new RuntimeException('Vision vector gateway is not configured. Set VISION_VECTOR_GATEWAY_URL and VISION_VECTOR_GATEWAY_API_KEY.');
}
return Http::acceptJson()
->withHeaders([
'X-API-Key' => $this->apiKey(),
])
->connectTimeout(max(1, (int) config('vision.vector_gateway.connect_timeout_seconds', 5)))
->timeout(max(1, (int) config('vision.vector_gateway.timeout_seconds', 20)))
->retry(
max(0, (int) config('vision.vector_gateway.retries', 1)),
max(0, (int) config('vision.vector_gateway.retry_delay_ms', 250)),
throw: false,
);
}
/**
* @param array<string, mixed> $payload
*/
private function postJson(string $url, array $payload): Response
{
$response = $this->request()->post($url, $payload);
if (! $response instanceof Response) {
throw new RuntimeException('Vector gateway request did not return an HTTP response.');
}
return $response;
}
private function baseUrl(): string
{
return rtrim((string) config('vision.vector_gateway.base_url', ''), '/');
}
private function apiKey(): string
{
return trim((string) config('vision.vector_gateway.api_key', ''));
}
private function url(string $path): string
{
return $this->baseUrl() . '/' . ltrim($path, '/');
}
private function failureMessage(string $operation, Response $response): string
{
$body = trim($response->body());
if ($body === '') {
return $operation . ' failed with HTTP ' . $response->status() . '.';
}
return $operation . ' failed with HTTP ' . $response->status() . ': ' . $body;
}
/**
* @param mixed $json
* @return list<array{id: int|string, score: float, metadata: array<string, mixed>}>
*/
private function extractMatches(mixed $json): array
{
$candidates = [];
if (is_array($json)) {
$candidates = $this->extractCandidateRows($json);
}
$results = [];
foreach ($candidates as $candidate) {
if (! is_array($candidate)) {
continue;
}
$id = $candidate['id']
?? $candidate['point_id']
?? $candidate['payload']['id']
?? $candidate['metadata']['id']
?? null;
if (! is_int($id) && ! is_string($id)) {
continue;
}
$score = $candidate['score']
?? $candidate['similarity']
?? $candidate['distance']
?? 0.0;
$metadata = $candidate['metadata'] ?? $candidate['payload'] ?? [];
if (! is_array($metadata)) {
$metadata = [];
}
$results[] = [
'id' => $id,
'score' => (float) $score,
'metadata' => $metadata,
];
}
return $results;
}
/**
* @param array<mixed> $json
* @return array<int, mixed>
*/
private function extractCandidateRows(array $json): array
{
$keys = ['results', 'matches', 'points', 'data'];
foreach ($keys as $key) {
if (! isset($json[$key]) || ! is_array($json[$key])) {
continue;
}
$value = $json[$key];
if (array_is_list($value)) {
return $value;
}
foreach (['results', 'matches', 'points', 'items'] as $nestedKey) {
if (isset($value[$nestedKey]) && is_array($value[$nestedKey]) && array_is_list($value[$nestedKey])) {
return $value[$nestedKey];
}
}
}
return array_is_list($json) ? $json : [];
}
}

View File

@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace App\Services\Vision;
use App\Models\Artwork;
use Illuminate\Http\UploadedFile;
final class VectorService
{
public function __construct(
private readonly AiArtworkVectorSearchService $searchService,
private readonly ArtworkVectorIndexService $indexService,
) {
}
public function isConfigured(): bool
{
return $this->searchService->isConfigured();
}
/**
* @return list<array<string, mixed>>
*/
public function similarToArtwork(Artwork $artwork, int $limit = 12): array
{
return $this->searchService->similarToArtwork($artwork, $limit);
}
/**
* @return list<array<string, mixed>>
*/
public function searchByUploadedImage(UploadedFile $file, int $limit = 12): array
{
return $this->searchService->searchByUploadedImage($file, $limit);
}
/**
* @return array{url: string, metadata: array<string, mixed>}
*/
public function payloadForArtwork(Artwork $artwork): array
{
return $this->indexService->payloadForArtwork($artwork);
}
/**
* @return array{url: string, metadata: array<string, mixed>}
*/
public function upsertArtwork(Artwork $artwork): array
{
return $this->indexService->upsertArtwork($artwork);
}
}

File diff suppressed because it is too large Load Diff