Skip to content

Commit f625f41

Browse files
authored
Merge pull request #787 from hypervel/upstream-sync-2026-11-27-1
Sync Laravel updates for console testing, route binding and the AI SDK
2 parents a826dc2 + b82f6b4 commit f625f41

103 files changed

Lines changed: 3381 additions & 996 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Ai\Contracts\Gateway;
6+
7+
use Hypervel\Ai\Contracts\Providers\StoreProvider;
8+
use Hypervel\Ai\Responses\Data\StoreDocument;
9+
use Hypervel\Support\LazyCollection;
10+
11+
interface ListsStoreFiles
12+
{
13+
/**
14+
* List the documents in a vector store.
15+
*
16+
* @return LazyCollection<int, StoreDocument>
17+
*/
18+
public function listFiles(StoreProvider $provider, string $storeId): LazyCollection;
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Ai\Contracts\Providers;
6+
7+
use Hypervel\Ai\Responses\Data\StoreDocument;
8+
use Hypervel\Support\LazyCollection;
9+
10+
interface SupportsStoreFileListing
11+
{
12+
/**
13+
* List the documents in a vector store.
14+
*
15+
* @return LazyCollection<int, StoreDocument>
16+
*/
17+
public function listStoreFiles(string $storeId): LazyCollection;
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Ai\Enums;
6+
7+
enum StoreDocumentStatus: string
8+
{
9+
case Completed = 'completed';
10+
case Pending = 'pending';
11+
case Failed = 'failed';
12+
}

‎src/ai/src/Gateway/AzureOpenAi/AzureOpenAiGateway.php‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Hypervel\Ai\Gateway\AzureOpenAi;
66

7-
use Hypervel\Ai\Contracts\Files\StorableFile;
87
use Hypervel\Ai\Contracts\Gateway\EmbeddingGateway;
98
use Hypervel\Ai\Contracts\Gateway\ImageGateway;
109
use Hypervel\Ai\Contracts\Gateway\StepTextGateway;
@@ -33,8 +32,8 @@
3332
use Hypervel\Ai\Tools\ToolNameResolver;
3433
use Hypervel\Contracts\Events\Dispatcher;
3534
use Hypervel\Http\Client\Response;
36-
use Hypervel\Http\UploadedFile;
3735
use Hypervel\JsonSchema\JsonSchemaTypeFactory;
36+
use Hypervel\Support\Arr;
3837
use InvalidArgumentException;
3938

4039
class AzureOpenAiGateway implements EmbeddingGateway, ImageGateway, StepTextGateway
@@ -106,10 +105,16 @@ public function generateImage(
106105
?int $timeout = null,
107106
array $providerOptions = [],
108107
): ImageResponse {
108+
$hasAttachments = filled($attachments);
109+
110+
if (! $hasAttachments && isset($providerOptions['mask'])) {
111+
throw new InvalidArgumentException('An image mask can only be used when editing an attached image.');
112+
}
113+
109114
/** @var ImageProvider&Provider $provider */
110115
$response = $this->withErrorHandling(
111116
$provider->name(),
112-
fn () => filled($attachments)
117+
fn () => $hasAttachments
113118
? $this->sendImageEditRequest($provider, $model, $prompt, $attachments, $size, $quality, $timeout, $providerOptions)
114119
: $this->client($provider, $timeout ?? 120)->post('images/generations', [
115120
...$providerOptions,
@@ -151,13 +156,11 @@ protected function sendImageEditRequest(
151156
$request = $this->client($provider, $timeout ?? 120);
152157

153158
foreach ($attachments as $attachment) {
154-
$content = match (true) {
155-
$attachment instanceof Image && $attachment instanceof StorableFile => $attachment->content(),
156-
$attachment instanceof UploadedFile => $attachment->get(),
157-
default => throw new InvalidArgumentException('Unsupported image attachment type [' . get_debug_type($attachment) . ']'),
158-
};
159+
$request = $request->attach('image[]', $this->imageEditContent($attachment), 'image.png');
160+
}
159161

160-
$request->attach('image[]', $content, 'image.png');
162+
if (($mask = Arr::pull($providerOptions, 'mask')) !== null) {
163+
$request = $request->attach('mask', $this->imageEditContent($mask), 'mask.png');
161164
}
162165

163166
return $request->post('images/edits', [

‎src/ai/src/Gateway/FakeStoreGateway.php‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,30 @@
66

77
use Closure;
88
use DateInterval;
9+
use Hypervel\Ai\Contracts\Gateway\ListsStoreFiles;
910
use Hypervel\Ai\Contracts\Gateway\StoreGateway;
1011
use Hypervel\Ai\Contracts\Providers\FileProvider;
1112
use Hypervel\Ai\Contracts\Providers\StoreProvider;
13+
use Hypervel\Ai\Enums\StoreDocumentStatus;
14+
use Hypervel\Ai\Responses\Data\StoreDocument;
1215
use Hypervel\Ai\Responses\Data\StoreFileCounts;
1316
use Hypervel\Ai\Store;
1417
use Hypervel\Ai\Stores;
1518
use Hypervel\Support\Attributes\TestsOnly;
1619
use Hypervel\Support\Collection;
20+
use Hypervel\Support\LazyCollection;
1721
use RuntimeException;
1822

19-
class FakeStoreGateway implements StoreGateway
23+
class FakeStoreGateway implements StoreGateway, ListsStoreFiles
2024
{
25+
/**
26+
* The documents in each fake vector store, keyed by store ID and then document ID.
27+
* All providers share this map, and creating the same name reuses its fake ID.
28+
*
29+
* @var array<string, array<string, StoreDocument>>
30+
*/
31+
protected array $documents = [];
32+
2133
protected int $currentResponseIndex = 0;
2234

2335
protected bool $preventStrayOperations = false;
@@ -105,10 +117,14 @@ public function createStore(
105117
?Collection $fileIds = null,
106118
?DateInterval $expiresWhenIdleFor = null,
107119
): Store {
120+
$storeId = Stores::fakeId($name);
121+
$this->documents[$storeId] = [];
122+
$this->withDocuments($storeId, $fileIds?->values()->all() ?? []);
123+
108124
/** @var FileProvider&StoreProvider $provider */
109125
return new Store(
110126
provider: $provider,
111-
id: Stores::fakeId($name),
127+
id: $storeId,
112128
name: $name,
113129
fileCounts: new StoreFileCounts(0, 0, 0),
114130
ready: true,
@@ -120,14 +136,28 @@ public function createStore(
120136
*/
121137
public function addFile(StoreProvider $provider, string $storeId, string $fileId, array $metadata = []): string
122138
{
139+
$this->documents[$storeId][$fileId] = new StoreDocument($fileId, $fileId, StoreDocumentStatus::Completed, $metadata);
140+
123141
return $fileId;
124142
}
125143

144+
/**
145+
* List the documents in a vector store.
146+
*
147+
* @return LazyCollection<int, StoreDocument>
148+
*/
149+
public function listFiles(StoreProvider $provider, string $storeId): LazyCollection
150+
{
151+
return LazyCollection::make(fn () => array_values($this->documents[$storeId] ?? []));
152+
}
153+
126154
/**
127155
* Remove a file from a vector store.
128156
*/
129157
public function removeFile(StoreProvider $provider, string $storeId, string $documentId): bool
130158
{
159+
unset($this->documents[$storeId][$documentId]);
160+
131161
return true;
132162
}
133163

@@ -136,6 +166,8 @@ public function removeFile(StoreProvider $provider, string $storeId, string $doc
136166
*/
137167
public function deleteStore(StoreProvider $provider, string $storeId): bool
138168
{
169+
unset($this->documents[$storeId]);
170+
139171
return true;
140172
}
141173

@@ -152,4 +184,21 @@ public function preventStrayOperations(bool $prevent = true): self
152184

153185
return $this;
154186
}
187+
188+
/**
189+
* Seed a fake vector store with the given documents or file IDs.
190+
*
191+
* @param array<int, StoreDocument|string> $documents
192+
*/
193+
#[TestsOnly(whenShared: true)]
194+
public function withDocuments(string $storeId, array $documents): self
195+
{
196+
foreach ($documents as $document) {
197+
$document = is_string($document) ? new StoreDocument($document, $document, StoreDocumentStatus::Completed) : $document;
198+
199+
$this->documents[$storeId][$document->id] = $document;
200+
}
201+
202+
return $this;
203+
}
155204
}

‎src/ai/src/Gateway/Gemini/GeminiStoreGateway.php‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,28 @@
55
namespace Hypervel\Ai\Gateway\Gemini;
66

77
use DateInterval;
8+
use Hypervel\Ai\Contracts\Gateway\ListsStoreFiles;
89
use Hypervel\Ai\Contracts\Gateway\StoreGateway;
910
use Hypervel\Ai\Contracts\Providers\FileProvider;
1011
use Hypervel\Ai\Contracts\Providers\StoreProvider;
12+
use Hypervel\Ai\Enums\StoreDocumentStatus;
1113
use Hypervel\Ai\Exceptions\AiException;
1214
use Hypervel\Ai\Gateway\Concerns\CreatesClient;
1315
use Hypervel\Ai\Gateway\Concerns\EncodesResourceIds;
1416
use Hypervel\Ai\Providers\Provider;
17+
use Hypervel\Ai\Responses\Data\StoreDocument;
1518
use Hypervel\Ai\Responses\Data\StoreFileCounts;
1619
use Hypervel\Ai\Store;
1720
use Hypervel\Database\ConnectionResolver;
1821
use Hypervel\Http\Client\PendingRequest;
1922
use Hypervel\Http\Client\Response;
23+
use Hypervel\Support\Arr;
2024
use Hypervel\Support\Collection;
25+
use Hypervel\Support\LazyCollection;
2126
use Hypervel\Support\Sleep;
2227
use Hypervel\Support\Str;
2328

24-
class GeminiStoreGateway implements StoreGateway
29+
class GeminiStoreGateway implements StoreGateway, ListsStoreFiles
2530
{
2631
use Concerns\HandlesGeminiErrors;
2732
use CreatesClient;
@@ -152,6 +157,65 @@ protected function formatMetadata(array $metadata): array
152157
})->values()->all();
153158
}
154159

160+
/**
161+
* List the documents in a vector store, without file IDs, which Gemini does not return.
162+
*
163+
* @return LazyCollection<int, StoreDocument>
164+
*/
165+
public function listFiles(StoreProvider $provider, string $storeId): LazyCollection
166+
{
167+
/** @var Provider&StoreProvider $provider */
168+
return LazyCollection::make(function () use ($provider, $storeId) {
169+
$storeId = $this->normalizeStoreId($storeId);
170+
$pageToken = null;
171+
172+
do {
173+
$response = $this->withErrorHandling($provider->name(), fn () => $this->client($provider)
174+
->get($this->baseUrl($provider) . "/{$storeId}/documents", Arr::whereNotNull([
175+
'pageSize' => 20,
176+
'pageToken' => $pageToken,
177+
]))->throw());
178+
179+
foreach ($response->json('documents') ?? [] as $document) {
180+
yield new StoreDocument(
181+
id: basename($document['name']),
182+
status: $this->documentStatus($document['state'] ?? null),
183+
metadata: $this->parseMetadata($document['customMetadata'] ?? []),
184+
);
185+
}
186+
187+
$pageToken = $response->json('nextPageToken');
188+
} while (filled($pageToken));
189+
});
190+
}
191+
192+
/**
193+
* Map a Gemini document state to a document status.
194+
*/
195+
protected function documentStatus(?string $state): ?StoreDocumentStatus
196+
{
197+
return match ($state) {
198+
'STATE_ACTIVE' => StoreDocumentStatus::Completed,
199+
'STATE_PENDING' => StoreDocumentStatus::Pending,
200+
'STATE_FAILED' => StoreDocumentStatus::Failed,
201+
default => null,
202+
};
203+
}
204+
205+
/**
206+
* Parse metadata from Gemini's custom_metadata format.
207+
*
208+
* @return array<string, mixed>
209+
*/
210+
protected function parseMetadata(array $metadata): array
211+
{
212+
return (new Collection($metadata))->mapWithKeys(fn (array $entry): array => [$entry['key'] => match (true) {
213+
isset($entry['numericValue']) => $entry['numericValue'],
214+
isset($entry['stringListValue']) => $entry['stringListValue']['values'] ?? [],
215+
default => $entry['stringValue'] ?? null,
216+
}])->all();
217+
}
218+
155219
/**
156220
* Remove a file from a vector store.
157221
*/

‎src/ai/src/Gateway/OpenAi/Concerns/MapsAttachments.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
namespace Hypervel\Ai\Gateway\OpenAi\Concerns;
66

7+
use Hypervel\Ai\Contracts\Files\StorableFile;
78
use Hypervel\Ai\Contracts\HasProviderOptions;
89
use Hypervel\Ai\Enums\Lab;
910
use Hypervel\Ai\Files\Base64Document;
1011
use Hypervel\Ai\Files\Base64Image;
1112
use Hypervel\Ai\Files\File;
13+
use Hypervel\Ai\Files\Image;
1214
use Hypervel\Ai\Files\LocalDocument;
1315
use Hypervel\Ai\Files\LocalImage;
1416
use Hypervel\Ai\Files\ProviderDocument;
@@ -109,6 +111,18 @@ protected function mapAttachments(Collection $attachments, Provider $provider):
109111
})->all();
110112
}
111113

114+
/**
115+
* Get the contents of an image sent to the edits endpoint.
116+
*/
117+
protected function imageEditContent(mixed $image): string
118+
{
119+
return match (true) {
120+
$image instanceof Image && $image instanceof StorableFile => $image->content(),
121+
$image instanceof UploadedFile => $image->get(),
122+
default => throw new InvalidArgumentException('Unsupported image attachment type [' . get_debug_type($image) . ']'),
123+
};
124+
}
125+
112126
/**
113127
* Determine if the given uploaded file is an image.
114128
*/

0 commit comments

Comments
 (0)