Skip to content

Commit c0f9d42

Browse files
authored
Merge pull request #6102 from G4brym/add-ai-search-rpc-types
Add AI Search RPC Types
2 parents acdca96 + 6159082 commit c0f9d42

File tree

7 files changed

+1511
-45
lines changed

7 files changed

+1511
-45
lines changed

types/defines/ai-search.d.ts

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
// AI Search V2 API Error Interfaces
2+
export interface AiSearchInternalError extends Error {}
3+
export interface AiSearchNotFoundError extends Error {}
4+
export interface AiSearchNameNotSetError extends Error {}
5+
6+
// Filter types (shared with AutoRAG for compatibility)
7+
export type ComparisonFilter = {
8+
key: string;
9+
type: 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte';
10+
value: string | number | boolean;
11+
};
12+
13+
export type CompoundFilter = {
14+
type: 'and' | 'or';
15+
filters: ComparisonFilter[];
16+
};
17+
18+
// AI Search V2 Request Types
19+
export type AiSearchSearchRequest = {
20+
messages: Array<{
21+
role: 'system' | 'developer' | 'user' | 'assistant' | 'tool';
22+
content: string | null;
23+
}>;
24+
ai_search_options?: {
25+
retrieval?: {
26+
retrieval_type?: 'vector' | 'keyword' | 'hybrid';
27+
/** Match threshold (0-1, default 0.4) */
28+
match_threshold?: number;
29+
/** Maximum number of results (1-50, default 10) */
30+
max_num_results?: number;
31+
filters?: CompoundFilter | ComparisonFilter;
32+
/** Context expansion (0-3, default 0) */
33+
context_expansion?: number;
34+
[key: string]: unknown;
35+
};
36+
query_rewrite?: {
37+
enabled?: boolean;
38+
model?: string;
39+
rewrite_prompt?: string;
40+
[key: string]: unknown;
41+
};
42+
reranking?: {
43+
/** Enable reranking (default false) */
44+
enabled?: boolean;
45+
model?: '@cf/baai/bge-reranker-base' | '';
46+
/** Match threshold (0-1, default 0.4) */
47+
match_threshold?: number;
48+
[key: string]: unknown;
49+
};
50+
[key: string]: unknown;
51+
};
52+
};
53+
54+
export type AiSearchChatCompletionsRequest = {
55+
messages: Array<{
56+
role: 'system' | 'developer' | 'user' | 'assistant' | 'tool';
57+
content: string | null;
58+
}>;
59+
model?: string;
60+
stream?: boolean;
61+
ai_search_options?: {
62+
retrieval?: {
63+
retrieval_type?: 'vector' | 'keyword' | 'hybrid';
64+
match_threshold?: number;
65+
max_num_results?: number;
66+
filters?: CompoundFilter | ComparisonFilter;
67+
context_expansion?: number;
68+
[key: string]: unknown;
69+
};
70+
query_rewrite?: {
71+
enabled?: boolean;
72+
model?: string;
73+
rewrite_prompt?: string;
74+
[key: string]: unknown;
75+
};
76+
reranking?: {
77+
enabled?: boolean;
78+
model?: '@cf/baai/bge-reranker-base' | '';
79+
match_threshold?: number;
80+
[key: string]: unknown;
81+
};
82+
[key: string]: unknown;
83+
};
84+
[key: string]: unknown;
85+
};
86+
87+
// AI Search V2 Response Types
88+
export type AiSearchSearchResponse = {
89+
search_query: string;
90+
chunks: Array<{
91+
id: string;
92+
type: string;
93+
/** Match score (0-1) */
94+
score: number;
95+
text: string;
96+
item: {
97+
timestamp?: number;
98+
key: string;
99+
metadata?: Record<string, unknown>;
100+
};
101+
scoring_details?: {
102+
/** Keyword match score (0-1) */
103+
keyword_score?: number;
104+
/** Vector similarity score (0-1) */
105+
vector_score?: number;
106+
};
107+
}>;
108+
};
109+
110+
export type AiSearchListResponse = Array<{
111+
id: string;
112+
internal_id?: string;
113+
account_id?: string;
114+
account_tag?: string;
115+
/** Whether the instance is enabled (default true) */
116+
enable?: boolean;
117+
type?: 'r2' | 'web-crawler';
118+
source?: string;
119+
[key: string]: unknown;
120+
}>;
121+
122+
export type AiSearchConfig = {
123+
/** Instance ID (1-32 chars, pattern: ^[a-z0-9_]+(?:-[a-z0-9_]+)*$) */
124+
id: string;
125+
type: 'r2' | 'web-crawler';
126+
source: string;
127+
source_params?: object;
128+
/** Token ID (UUID format) */
129+
token_id?: string;
130+
ai_gateway_id?: string;
131+
/** Enable query rewriting (default false) */
132+
rewrite_query?: boolean;
133+
/** Enable reranking (default false) */
134+
reranking?: boolean;
135+
embedding_model?: string;
136+
ai_search_model?: string;
137+
};
138+
139+
export type AiSearchInstance = {
140+
id: string;
141+
enable?: boolean;
142+
type?: 'r2' | 'web-crawler';
143+
source?: string;
144+
[key: string]: unknown;
145+
};
146+
147+
// AI Search Instance Service - Instance-level operations
148+
export declare abstract class AiSearchInstanceService {
149+
/**
150+
* Search the AI Search instance for relevant chunks.
151+
* @param params Search request with messages and AI search options
152+
* @returns Search response with matching chunks
153+
*/
154+
search(params: AiSearchSearchRequest): Promise<AiSearchSearchResponse>;
155+
156+
/**
157+
* Generate chat completions with AI Search context.
158+
* @param params Chat completions request with optional streaming
159+
* @returns Response object (if streaming) or chat completion result
160+
*/
161+
chatCompletions(
162+
params: AiSearchChatCompletionsRequest
163+
): Promise<Response | object>;
164+
165+
/**
166+
* Delete this AI Search instance.
167+
*/
168+
delete(): Promise<void>;
169+
}
170+
171+
// AI Search Account Service - Account-level operations
172+
export declare abstract class AiSearchAccountService {
173+
/**
174+
* List all AI Search instances in the account.
175+
* @returns Array of AI Search instances
176+
*/
177+
list(): Promise<AiSearchListResponse>;
178+
179+
/**
180+
* Get an AI Search instance by ID.
181+
* @param name Instance ID
182+
* @returns Instance service for performing operations
183+
*/
184+
get(name: string): AiSearchInstanceService;
185+
186+
/**
187+
* Create a new AI Search instance.
188+
* @param config Instance configuration
189+
* @returns Instance service for performing operations
190+
*/
191+
create(config: AiSearchConfig): Promise<AiSearchInstanceService>;
192+
}

types/defines/ai.d.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5452,6 +5452,50 @@ export type AiModelListType = Record<string, any>;
54525452
export declare abstract class Ai<AiModelList extends AiModelListType = AiModels> {
54535453
aiGatewayLogId: string | null;
54545454
gateway(gatewayId: string): AiGateway;
5455+
5456+
/**
5457+
* Access the AI Search API for managing AI-powered search instances.
5458+
*
5459+
* This is the new API that replaces AutoRAG with better namespace separation:
5460+
* - Account-level operations: `list()`, `create()`
5461+
* - Instance-level operations: `get(id).search()`, `get(id).chatCompletions()`, `get(id).delete()`
5462+
*
5463+
* @example
5464+
* ```typescript
5465+
* // List all AI Search instances
5466+
* const instances = await env.AI.aiSearch.list();
5467+
*
5468+
* // Search an instance
5469+
* const results = await env.AI.aiSearch.get('my-search').search({
5470+
* messages: [{ role: 'user', content: 'What is the policy?' }],
5471+
* ai_search_options: {
5472+
* retrieval: { max_num_results: 10 }
5473+
* }
5474+
* });
5475+
*
5476+
* // Generate chat completions with AI Search context
5477+
* const response = await env.AI.aiSearch.get('my-search').chatCompletions({
5478+
* messages: [{ role: 'user', content: 'What is the policy?' }],
5479+
* model: '@cf/meta/llama-3.3-70b-instruct-fp8-fast'
5480+
* });
5481+
* ```
5482+
*/
5483+
aiSearch: AiSearchAccountService;
5484+
5485+
/**
5486+
* @deprecated AutoRAG has been replaced by AI Search.
5487+
* Use `env.AI.aiSearch` instead for better API design and new features.
5488+
*
5489+
* Migration guide:
5490+
* - `env.AI.autorag().list()` → `env.AI.aiSearch.list()`
5491+
* - `env.AI.autorag('id').search({ query: '...' })` → `env.AI.aiSearch.get('id').search({ messages: [{ role: 'user', content: '...' }] })`
5492+
* - `env.AI.autorag('id').aiSearch(...)` → `env.AI.aiSearch.get('id').chatCompletions(...)`
5493+
*
5494+
* Note: The old API continues to work for backwards compatibility, but new projects should use AI Search.
5495+
*
5496+
* @see AiSearchAccountService
5497+
* @param autoragId Optional instance ID (omit for account-level operations)
5498+
*/
54555499
autorag(autoragId: string): AutoRAG;
54565500
run<Name extends keyof AiModelList, Options extends AiOptions, InputOptions extends AiModelList[Name]["inputs"]>(
54575501
model: Name,

types/defines/autorag.d.ts

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1+
/**
2+
* @deprecated AutoRAG has been replaced by AI Search. Use AiSearchInternalError instead.
3+
* @see AiSearchInternalError
4+
*/
15
export interface AutoRAGInternalError extends Error {}
6+
7+
/**
8+
* @deprecated AutoRAG has been replaced by AI Search. Use AiSearchNotFoundError instead.
9+
* @see AiSearchNotFoundError
10+
*/
211
export interface AutoRAGNotFoundError extends Error {}
12+
13+
/**
14+
* @deprecated This error type is no longer used in the AI Search API.
15+
*/
316
export interface AutoRAGUnauthorizedError extends Error {}
17+
18+
/**
19+
* @deprecated AutoRAG has been replaced by AI Search. Use AiSearchNameNotSetError instead.
20+
* @see AiSearchNameNotSetError
21+
*/
422
export interface AutoRAGNameNotSetError extends Error {}
523

6-
export type ComparisonFilter = {
7-
key: string;
8-
type: 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte';
9-
value: string | number | boolean;
10-
};
11-
export type CompoundFilter = {
12-
type: 'and' | 'or';
13-
filters: ComparisonFilter[];
14-
};
24+
/**
25+
* @deprecated AutoRAG has been replaced by AI Search.
26+
* Use AiSearchSearchRequest with the new API instead.
27+
* @see AiSearchSearchRequest
28+
*/
1529
export type AutoRagSearchRequest = {
1630
query: string;
1731
filters?: CompoundFilter | ComparisonFilter;
@@ -26,16 +40,34 @@ export type AutoRagSearchRequest = {
2640
};
2741
rewrite_query?: boolean;
2842
};
43+
44+
/**
45+
* @deprecated AutoRAG has been replaced by AI Search.
46+
* Use AiSearchChatCompletionsRequest with the new API instead.
47+
* @see AiSearchChatCompletionsRequest
48+
*/
2949
export type AutoRagAiSearchRequest = AutoRagSearchRequest & {
3050
stream?: boolean;
3151
system_prompt?: string;
3252
};
53+
54+
/**
55+
* @deprecated AutoRAG has been replaced by AI Search.
56+
* Use AiSearchChatCompletionsRequest with stream: true instead.
57+
* @see AiSearchChatCompletionsRequest
58+
*/
3359
export type AutoRagAiSearchRequestStreaming = Omit<
3460
AutoRagAiSearchRequest,
3561
'stream'
3662
> & {
3763
stream: true;
3864
};
65+
66+
/**
67+
* @deprecated AutoRAG has been replaced by AI Search.
68+
* Use AiSearchSearchResponse with the new API instead.
69+
* @see AiSearchSearchResponse
70+
*/
3971
export type AutoRagSearchResponse = {
4072
object: 'vector_store.search_results.page';
4173
search_query: string;
@@ -53,6 +85,11 @@ export type AutoRagSearchResponse = {
5385
next_page: string | null;
5486
};
5587

88+
/**
89+
* @deprecated AutoRAG has been replaced by AI Search.
90+
* Use AiSearchListResponse with the new API instead.
91+
* @see AiSearchListResponse
92+
*/
5693
export type AutoRagListResponse = {
5794
id: string;
5895
enable: boolean;
@@ -63,15 +100,56 @@ export type AutoRagListResponse = {
63100
status: string;
64101
}[];
65102

103+
/**
104+
* @deprecated AutoRAG has been replaced by AI Search.
105+
* The new API returns different response formats for chat completions.
106+
*/
66107
export type AutoRagAiSearchResponse = AutoRagSearchResponse & {
67108
response: string;
68109
};
69110

111+
/**
112+
* @deprecated AutoRAG has been replaced by AI Search.
113+
* Use the new AI Search API instead: `env.AI.aiSearch`
114+
*
115+
* Migration guide:
116+
* - `env.AI.autorag().list()` → `env.AI.aiSearch.list()`
117+
* - `env.AI.autorag('id').search(...)` → `env.AI.aiSearch.get('id').search(...)`
118+
* - `env.AI.autorag('id').aiSearch(...)` → `env.AI.aiSearch.get('id').chatCompletions(...)`
119+
*
120+
* @see AiSearchAccountService
121+
* @see AiSearchInstanceService
122+
*/
70123
export declare abstract class AutoRAG {
124+
/**
125+
* @deprecated Use `env.AI.aiSearch.list()` instead.
126+
* @see AiSearchAccountService.list
127+
*/
71128
list(): Promise<AutoRagListResponse>;
129+
130+
/**
131+
* @deprecated Use `env.AI.aiSearch.get(id).search(...)` instead.
132+
* Note: The new API uses a messages array instead of a query string.
133+
* @see AiSearchInstanceService.search
134+
*/
72135
search(params: AutoRagSearchRequest): Promise<AutoRagSearchResponse>;
136+
137+
/**
138+
* @deprecated Use `env.AI.aiSearch.get(id).chatCompletions(...)` instead.
139+
* @see AiSearchInstanceService.chatCompletions
140+
*/
73141
aiSearch(params: AutoRagAiSearchRequestStreaming): Promise<Response>;
142+
143+
/**
144+
* @deprecated Use `env.AI.aiSearch.get(id).chatCompletions(...)` instead.
145+
* @see AiSearchInstanceService.chatCompletions
146+
*/
74147
aiSearch(params: AutoRagAiSearchRequest): Promise<AutoRagAiSearchResponse>;
148+
149+
/**
150+
* @deprecated Use `env.AI.aiSearch.get(id).chatCompletions(...)` instead.
151+
* @see AiSearchInstanceService.chatCompletions
152+
*/
75153
aiSearch(
76154
params: AutoRagAiSearchRequest
77155
): Promise<AutoRagAiSearchResponse | Response>;

0 commit comments

Comments
 (0)