66
77use Closure ;
88use DateInterval ;
9+ use Hypervel \Ai \Contracts \Gateway \ListsStoreFiles ;
910use Hypervel \Ai \Contracts \Gateway \StoreGateway ;
1011use Hypervel \Ai \Contracts \Providers \FileProvider ;
1112use Hypervel \Ai \Contracts \Providers \StoreProvider ;
13+ use Hypervel \Ai \Enums \StoreDocumentStatus ;
14+ use Hypervel \Ai \Responses \Data \StoreDocument ;
1215use Hypervel \Ai \Responses \Data \StoreFileCounts ;
1316use Hypervel \Ai \Store ;
1417use Hypervel \Ai \Stores ;
1518use Hypervel \Support \Attributes \TestsOnly ;
1619use Hypervel \Support \Collection ;
20+ use Hypervel \Support \LazyCollection ;
1721use 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}
0 commit comments