@@ -10,25 +10,30 @@ interface RequiredEnv {
1010const AiSearchResponseSchema = z . object ( {
1111 object : z . string ( ) ,
1212 search_query : z . string ( ) ,
13- data : z . array ( z . object ( {
14- file_id : z . string ( ) ,
15- filename : z . string ( ) ,
16- score : z . number ( ) ,
17- attributes : z . object ( {
18- modified_date : z . number ( ) . optional ( ) ,
19- folder : z . string ( ) . optional ( ) ,
20- } ) . catchall ( z . any ( ) ) ,
21- content : z . array ( z . object ( {
22- id : z . string ( ) ,
23- type : z . string ( ) ,
24- text : z . string ( ) ,
25- } ) ) ,
26- } ) ) ,
13+ data : z . array (
14+ z . object ( {
15+ file_id : z . string ( ) ,
16+ filename : z . string ( ) ,
17+ score : z . number ( ) ,
18+ attributes : z
19+ . object ( {
20+ modified_date : z . number ( ) . optional ( ) ,
21+ folder : z . string ( ) . optional ( ) ,
22+ } )
23+ . catchall ( z . any ( ) ) ,
24+ content : z . array (
25+ z . object ( {
26+ id : z . string ( ) ,
27+ type : z . string ( ) ,
28+ text : z . string ( ) ,
29+ } )
30+ ) ,
31+ } )
32+ ) ,
2733 has_more : z . boolean ( ) ,
2834 next_page : z . string ( ) . nullable ( ) ,
2935} )
3036
31-
3237/**
3338 * Registers the docs search tool with the MCP server using AI Search
3439 * @param server The MCP server instance
@@ -114,7 +119,7 @@ ${result.text}
114119
115120async function queryAiSearch ( ai : Ai , query : string ) {
116121 const rawResponse = await doWithRetries ( ( ) =>
117- ai . autorag ( " docs-mcp-rag" ) . search ( {
122+ ai . autorag ( ' docs-mcp-rag' ) . search ( {
118123 query,
119124 } )
120125 )
@@ -127,7 +132,7 @@ async function queryAiSearch(ai: Ai, query: string) {
127132 id : item . file_id ,
128133 url : sourceToUrl ( item . filename ) ,
129134 title : extractTitle ( item . filename ) ,
130- text : item . content . map ( c => c . text ) . join ( '\n' ) ,
135+ text : item . content . map ( ( c ) => c . text ) . join ( '\n' ) ,
131136 } ) )
132137}
133138
@@ -136,9 +141,7 @@ function sourceToUrl(filename: string): string {
136141 // Example: "workers/configuration/index.md" -> "https://developers.cloudflare.com/workers/configuration/"
137142 return (
138143 'https://developers.cloudflare.com/' +
139- filename
140- . replace ( / i n d e x \. m d x ? $ / , '' )
141- . replace ( / \. m d x ? $ / , '' )
144+ filename . replace ( / i n d e x \. m d x ? $ / , '' ) . replace ( / \. m d x ? $ / , '' )
142145 )
143146}
144147
@@ -147,16 +150,14 @@ function extractTitle(filename: string): string {
147150 // Example: "workers/configuration/index.md" -> "Configuration"
148151 const parts = filename . replace ( / \. m d x ? $ / , '' ) . split ( '/' )
149152 const lastPart = parts [ parts . length - 1 ]
150-
153+
151154 if ( lastPart === 'index' ) {
152155 // Use the parent directory name if filename is index
153156 return parts [ parts . length - 2 ] || 'Documentation'
154157 }
155-
158+
156159 // Convert kebab-case or snake_case to title case
157- return lastPart
158- . replace ( / [ - _ ] / g, ' ' )
159- . replace ( / \b \w / g, l => l . toUpperCase ( ) )
160+ return lastPart . replace ( / [ - _ ] / g, ' ' ) . replace ( / \b \w / g, ( l ) => l . toUpperCase ( ) )
160161}
161162
162163/**
@@ -167,20 +168,20 @@ function extractTitle(filename: string): string {
167168async function doWithRetries < T > ( action : ( ) => Promise < T > ) {
168169 const NUM_RETRIES = 5
169170 const INIT_RETRY_MS = 100
170-
171+
171172 for ( let i = 0 ; i <= NUM_RETRIES ; i ++ ) {
172173 try {
173174 return await action ( )
174175 } catch ( e ) {
175176 // Check if error is retryable (system errors, not user errors)
176177 const isRetryable = isRetryableError ( e )
177-
178+
178179 console . error ( `AI Search attempt ${ i + 1 } failed:` , e )
179-
180+
180181 if ( ! isRetryable || i === NUM_RETRIES ) {
181182 throw e
182183 }
183-
184+
184185 // Exponential backoff with jitter
185186 const delay = Math . random ( ) * INIT_RETRY_MS * Math . pow ( 2 , i )
186187 await scheduler . wait ( delay )
@@ -200,7 +201,7 @@ function isRetryableError(error: unknown): boolean {
200201 // Retry server errors (5xx) and rate limits (429), not client errors (4xx)
201202 return status >= 500 || status === 429
202203 }
203-
204+
204205 // Handle network errors, timeouts, etc.
205206 if ( error instanceof Error ) {
206207 const errorMessage = error . message . toLowerCase ( )
@@ -211,7 +212,7 @@ function isRetryableError(error: unknown): boolean {
211212 errorMessage . includes ( 'fetch' )
212213 )
213214 }
214-
215+
215216 // Default to retryable for unknown errors (conservative approach)
216217 return true
217- }
218+ }
0 commit comments