|
| 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 | +} |
0 commit comments