Skip to content

Commit 2d7b7a1

Browse files
committed
feat: update AI Search type definitions for V5 namespace binding spec
Aligns ai-search.d.ts with the V5 binding spec (path-based namespaces). Key changes: - Rename AiSearchAccountService -> AiSearchNamespace (namespace-scoped, not account-scoped) - Rename AiSearchInstanceService -> AiSearchInstance - Add delete(name) to AiSearchNamespace (namespace-level deletion) - Remove delete() from AiSearchInstance (moved to namespace level) - Add update(), stats(), items, jobs to AiSearchInstance - Add sub-service types: AiSearchItems, AiSearchItem, AiSearchJobs, AiSearchJob - Add item/job metadata types and request/response types - Remove AiSearchNameNotSetError (V4 concept, namespace always present in V5) - Update Ai.aiSearch() return type and JSDoc - Update AutoRAG @see references
1 parent d62d62a commit 2d7b7a1

7 files changed

Lines changed: 1357 additions & 565 deletions

File tree

types/defines/ai-search.d.ts

Lines changed: 261 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
// AI Search V2 API Error Interfaces
1+
// ============ AI Search Error Interfaces ============
2+
23
export interface AiSearchInternalError extends Error {}
34
export interface AiSearchNotFoundError extends Error {}
4-
export interface AiSearchNameNotSetError extends Error {}
55

6-
// AI Search V2 Request Types
6+
// ============ AI Search Request Types ============
7+
78
export type AiSearchSearchRequest = {
89
messages: Array<{
910
role: 'system' | 'developer' | 'user' | 'assistant' | 'tool';
@@ -28,7 +29,6 @@ export type AiSearchSearchRequest = {
2829
[key: string]: unknown;
2930
};
3031
reranking?: {
31-
/** Enable reranking (default false) */
3232
enabled?: boolean;
3333
model?: '@cf/baai/bge-reranker-base' | '';
3434
/** Match threshold (0-1, default 0.4) */
@@ -72,7 +72,8 @@ export type AiSearchChatCompletionsRequest = {
7272
[key: string]: unknown;
7373
};
7474

75-
// AI Search V2 Response Types
75+
// ============ AI Search Response Types ============
76+
7677
export type AiSearchSearchResponse = {
7778
search_query: string;
7879
chunks: Array<{
@@ -107,6 +108,8 @@ export type AiSearchListResponse = Array<{
107108
[key: string]: unknown;
108109
}>;
109110

111+
// ============ AI Search Config Types ============
112+
110113
export type AiSearchConfig = {
111114
/** Instance ID (1-32 chars, pattern: ^[a-z0-9_]+(?:-[a-z0-9_]+)*$) */
112115
id: string;
@@ -124,57 +127,285 @@ export type AiSearchConfig = {
124127
ai_search_model?: string;
125128
};
126129

127-
export type AiSearchInstance = {
130+
// ============ AI Search Item Types ============
131+
132+
export type AiSearchItemInfo = {
128133
id: string;
129-
enable?: boolean;
130-
type?: 'r2' | 'web-crawler';
131-
source?: string;
134+
key: string;
135+
status: 'completed' | 'error' | 'skipped' | 'queued' | 'processing';
136+
metadata?: Record<string, unknown>;
132137
[key: string]: unknown;
133138
};
134139

135-
// AI Search Instance Service - Instance-level operations
136-
export declare abstract class AiSearchInstanceService {
140+
export type AiSearchItemContentResult = {
141+
body: ReadableStream;
142+
contentType: string;
143+
filename: string;
144+
size: number;
145+
};
146+
147+
export type AiSearchUploadItemOptions = {
148+
metadata?: Record<string, unknown>;
149+
};
150+
151+
export type AiSearchListItemsParams = {
152+
page?: number;
153+
per_page?: number;
154+
};
155+
156+
export type AiSearchListItemsResponse = {
157+
result: AiSearchItemInfo[];
158+
result_info?: {
159+
count: number;
160+
page: number;
161+
per_page: number;
162+
total_count: number;
163+
};
164+
};
165+
166+
// ============ AI Search Job Types ============
167+
168+
export type AiSearchJobInfo = {
169+
id: string;
170+
status: 'completed' | 'error' | 'queued' | 'processing';
171+
[key: string]: unknown;
172+
};
173+
174+
export type AiSearchListJobsParams = {
175+
page?: number;
176+
per_page?: number;
177+
};
178+
179+
export type AiSearchListJobsResponse = {
180+
result: AiSearchJobInfo[];
181+
result_info?: {
182+
count: number;
183+
page: number;
184+
per_page: number;
185+
total_count: number;
186+
};
187+
};
188+
189+
export type AiSearchJobLogsParams = {
190+
page?: number;
191+
per_page?: number;
192+
};
193+
194+
// ============ AI Search Sub-Service Classes ============
195+
196+
/**
197+
* Single item service for an AI Search instance.
198+
* Provides info, delete, and download operations on a specific item.
199+
*/
200+
export declare abstract class AiSearchItem {
201+
/** Get metadata about this item. */
202+
info(): Promise<AiSearchItemInfo>;
203+
204+
/** Delete this item from the instance. */
205+
delete(): Promise<void>;
206+
207+
/**
208+
* Download the item's content.
209+
* @returns Object with body stream, content type, filename, and size.
210+
*/
211+
download(): Promise<AiSearchItemContentResult>;
212+
}
213+
214+
/**
215+
* Items collection service for an AI Search instance.
216+
* Provides list, upload, and access to individual items.
217+
*/
218+
export declare abstract class AiSearchItems {
219+
/** List items in this instance. */
220+
list(params?: AiSearchListItemsParams): Promise<AiSearchListItemsResponse>;
221+
222+
/**
223+
* Upload a file as an item.
224+
* @param name Filename for the uploaded item.
225+
* @param content File content as a ReadableStream, ArrayBuffer, or string.
226+
* @param options Optional metadata to attach to the item.
227+
* @returns The created item info.
228+
*/
229+
upload(
230+
name: string,
231+
content: ReadableStream | ArrayBuffer | string,
232+
options?: AiSearchUploadItemOptions
233+
): Promise<AiSearchItemInfo>;
234+
235+
/**
236+
* Upload a file and poll until processing completes.
237+
* @param name Filename for the uploaded item.
238+
* @param content File content as a ReadableStream, ArrayBuffer, or string.
239+
* @param options Optional metadata and polling configuration.
240+
* @returns The item info after processing completes (or timeout).
241+
*/
242+
uploadAndPoll(
243+
name: string,
244+
content: ReadableStream | ArrayBuffer | string,
245+
options?: AiSearchUploadItemOptions & {
246+
pollIntervalMs?: number;
247+
timeoutMs?: number;
248+
}
249+
): Promise<AiSearchItemInfo>;
250+
251+
/**
252+
* Get an item by ID.
253+
* @param itemId The item identifier.
254+
* @returns Item service for info, delete, and download operations.
255+
*/
256+
get(itemId: string): AiSearchItem;
257+
}
258+
259+
/**
260+
* Single job service for an AI Search instance.
261+
* Provides info and logs for a specific job.
262+
*/
263+
export declare abstract class AiSearchJob {
264+
/** Get metadata about this job. */
265+
info(): Promise<AiSearchJobInfo>;
266+
267+
/** Get logs for this job. */
268+
logs(params?: AiSearchJobLogsParams): Promise<object>;
269+
}
270+
271+
/**
272+
* Jobs collection service for an AI Search instance.
273+
* Provides list, create, and access to individual jobs.
274+
*/
275+
export declare abstract class AiSearchJobs {
276+
/** List jobs for this instance. */
277+
list(params?: AiSearchListJobsParams): Promise<AiSearchListJobsResponse>;
278+
279+
/**
280+
* Create a new indexing job.
281+
* @param params Optional job parameters.
282+
* @returns The created job info.
283+
*/
284+
create(params?: Record<string, unknown>): Promise<AiSearchJobInfo>;
285+
286+
/**
287+
* Get a job by ID.
288+
* @param jobId The job identifier.
289+
* @returns Job service for info and logs operations.
290+
*/
291+
get(jobId: string): AiSearchJob;
292+
}
293+
294+
// ============ AI Search Binding Classes ============
295+
296+
/**
297+
* Instance-level AI Search service.
298+
*
299+
* Used as:
300+
* - The return type of `AiSearchNamespace.get(name)` (namespace binding)
301+
* - The type of `env.BLOG_SEARCH` (single instance binding via `ai_search`)
302+
*
303+
* Provides search, chat, update, stats, items, and jobs operations.
304+
* No delete() — deletion is done via `AiSearchNamespace.delete(name)`.
305+
*
306+
* @example
307+
* ```ts
308+
* // Via namespace binding
309+
* const instance = env.AI_SEARCH.get("blog");
310+
* const results = await instance.search({
311+
* messages: [{ role: "user", content: "How does caching work?" }],
312+
* });
313+
*
314+
* // Via single instance binding
315+
* const results = await env.BLOG_SEARCH.search({
316+
* messages: [{ role: "user", content: "How does caching work?" }],
317+
* });
318+
* ```
319+
*/
320+
export declare abstract class AiSearchInstance {
137321
/**
138322
* Search the AI Search instance for relevant chunks.
139-
* @param params Search request with messages and AI search options
140-
* @returns Search response with matching chunks
323+
* @param params Search request with messages and optional AI search options.
324+
* @returns Search response with matching chunks and search query.
141325
*/
142326
search(params: AiSearchSearchRequest): Promise<AiSearchSearchResponse>;
143327

144328
/**
145329
* Generate chat completions with AI Search context.
146-
* @param params Chat completions request with optional streaming
147-
* @returns Response object (if streaming) or chat completion result
330+
* @param params Chat completions request with optional streaming.
331+
* @returns ReadableStream if streaming, otherwise a chat completion object.
148332
*/
149333
chatCompletions(
150334
params: AiSearchChatCompletionsRequest
151-
): Promise<Response | object>;
335+
): Promise<ReadableStream | object>;
152336

153337
/**
154-
* Delete this AI Search instance.
338+
* Update the instance configuration.
339+
* @param config Partial configuration to update.
340+
* @returns Updated instance data.
155341
*/
156-
delete(): Promise<void>;
342+
update(config: Partial<AiSearchConfig>): Promise<object>;
343+
344+
/**
345+
* Get instance statistics (item count, indexing status, etc.).
346+
* @returns Statistics object.
347+
*/
348+
stats(): Promise<object>;
349+
350+
/** Items collection — list, upload, and manage items in this instance. */
351+
get items(): AiSearchItems;
352+
353+
/** Jobs collection — list, create, and inspect indexing jobs. */
354+
get jobs(): AiSearchJobs;
157355
}
158356

159-
// AI Search Account Service - Account-level operations
160-
export declare abstract class AiSearchAccountService {
357+
/**
358+
* Namespace-level AI Search service.
359+
*
360+
* Used as the type of `env.AI_SEARCH` (namespace binding via `ai_search_namespaces`).
361+
* Scoped to a single namespace. Provides dynamic instance access, creation, and deletion.
362+
*
363+
* @example
364+
* ```ts
365+
* // Access an instance within the namespace
366+
* const blog = env.AI_SEARCH.get("blog");
367+
* const results = await blog.search({
368+
* messages: [{ role: "user", content: "How does caching work?" }],
369+
* });
370+
*
371+
* // List all instances in the namespace
372+
* const instances = await env.AI_SEARCH.list();
373+
*
374+
* // Create a new instance
375+
* const tenant = await env.AI_SEARCH.create({
376+
* id: "tenant-123",
377+
* type: "web-crawler",
378+
* source: "https://example.com",
379+
* });
380+
*
381+
* // Delete an instance
382+
* await env.AI_SEARCH.delete("tenant-123");
383+
* ```
384+
*/
385+
export declare abstract class AiSearchNamespace {
386+
/**
387+
* Get an instance by name within the bound namespace.
388+
* @param name Instance name.
389+
* @returns Instance service for search, chat, update, stats, items, and jobs.
390+
*/
391+
get(name: string): AiSearchInstance;
392+
161393
/**
162-
* List all AI Search instances in the account.
163-
* @returns Array of AI Search instances
394+
* List all instances in the bound namespace.
395+
* @returns Array of instance metadata.
164396
*/
165397
list(): Promise<AiSearchListResponse>;
166398

167399
/**
168-
* Get an AI Search instance by ID.
169-
* @param name Instance ID
170-
* @returns Instance service for performing operations
400+
* Create a new instance within the bound namespace.
401+
* @param config Instance configuration including id, type, and source.
402+
* @returns Instance service for the newly created instance.
171403
*/
172-
get(name: string): AiSearchInstanceService;
404+
create(config: AiSearchConfig): Promise<AiSearchInstance>;
173405

174406
/**
175-
* Create a new AI Search instance.
176-
* @param config Instance configuration
177-
* @returns Instance service for performing operations
407+
* Delete an instance from the bound namespace.
408+
* @param name Instance name to delete.
178409
*/
179-
create(config: AiSearchConfig): Promise<AiSearchInstanceService>;
410+
delete(name: string): Promise<void>;
180411
}

0 commit comments

Comments
 (0)