@@ -305,14 +305,17 @@ class FernScribe {
305305
306306 async queryTurbopuffer ( query , opts = { } ) {
307307 if ( ! query || query . trimStart ( ) . length === 0 ) {
308+ console . log ( 'π§ Empty query provided to Turbopuffer' ) ;
308309 return [ ] ;
309310 }
310311
311312 try {
313+ console . log ( 'π§ Querying Turbopuffer with options:' , JSON . stringify ( opts , null , 2 ) ) ;
314+
312315 // Create embedding for the query
313316 const embeddingResponse = await this . createEmbedding ( query ) ;
314317 if ( ! embeddingResponse ) {
315- console . error ( 'Failed to create embedding for query' ) ;
318+ console . error ( 'π§ Failed to create embedding for query' ) ;
316319 return [ ] ;
317320 }
318321
@@ -324,6 +327,11 @@ class FernScribe {
324327 ...( opts . urlsToIgnore && { urls_to_ignore : opts . urlsToIgnore } )
325328 } ;
326329
330+ console . log ( 'π§ Turbopuffer request body (without embedding):' , {
331+ ...requestBody ,
332+ query_embedding : `[${ embeddingResponse . length } dimensions]`
333+ } ) ;
334+
327335 const response = await fetch ( this . turbopufferEndpoint , {
328336 method : 'POST' ,
329337 headers : {
@@ -334,17 +342,54 @@ class FernScribe {
334342 } ) ;
335343
336344 if ( ! response . ok ) {
345+ const errorText = await response . text ( ) ;
346+ console . error ( 'π§ Turbopuffer API error details:' , errorText ) ;
337347 throw new Error ( `Turbopuffer API error: ${ response . status } ` ) ;
338348 }
339349
340350 const data = await response . json ( ) ;
351+ console . log ( 'π§ Turbopuffer response structure:' , Object . keys ( data ) ) ;
352+ console . log ( 'π§ Turbopuffer results count:' , data . results ?. length || 0 ) ;
353+
341354 return data . results || [ ] ;
342355 } catch ( error ) {
343- console . error ( 'Turbopuffer query failed:' , error ) ;
356+ console . error ( 'π§ Turbopuffer query failed:' , error ) ;
344357 return [ ] ;
345358 }
346359 }
347360
361+ async createEmbedding ( text ) {
362+ try {
363+ console . log ( 'π§ Creating embedding for text of length:' , text . length ) ;
364+
365+ // Using OpenAI's embedding model
366+ const response = await fetch ( 'https://api.openai.com/v1/embeddings' , {
367+ method : 'POST' ,
368+ headers : {
369+ 'Authorization' : `Bearer ${ process . env . OPENAI_API_KEY } ` ,
370+ 'Content-Type' : 'application/json'
371+ } ,
372+ body : JSON . stringify ( {
373+ model : 'text-embedding-3-small' ,
374+ input : text
375+ } )
376+ } ) ;
377+
378+ if ( ! response . ok ) {
379+ const errorText = await response . text ( ) ;
380+ console . error ( 'π§ Embedding API error details:' , errorText ) ;
381+ throw new Error ( `Embedding API error: ${ response . status } ` ) ;
382+ }
383+
384+ const data = await response . json ( ) ;
385+ console . log ( 'π§ Embedding created successfully, dimensions:' , data . data [ 0 ] ?. embedding ?. length ) ;
386+ return data . data [ 0 ] ?. embedding ;
387+ } catch ( error ) {
388+ console . error ( 'π§ Embedding creation failed:' , error ) ;
389+ return null ;
390+ }
391+ }
392+
348393 async createEmbedding ( text ) {
349394 try {
350395 // Using OpenAI's embedding model
@@ -603,6 +648,11 @@ ${context.additionalContext ? `**Additional Context:** ${context.additionalConte
603648 context . additionalContext ? `\n\nAdditional Context:\n${ context . additionalContext } ` : ''
604649 ] . filter ( Boolean ) . join ( '\n' ) ;
605650
651+ // Debug logging
652+ console . log ( 'π Enhanced query length:' , enhancedQuery . length ) ;
653+ console . log ( 'π Enhanced query preview:' , enhancedQuery . substring ( 0 , 500 ) + '...' ) ;
654+ console . log ( 'π Namespace:' , process . env . TURBOPUFFER_NAMESPACE || 'default' ) ;
655+
606656 // Query TurboBuffer for relevant files
607657 console . log ( 'π Querying TurboBuffer for relevant files...' ) ;
608658 const searchResultURLs = new Set ( ) ;
@@ -613,6 +663,11 @@ ${context.additionalContext ? `**Additional Context:** ${context.additionalConte
613663 topK : 3
614664 } ) ;
615665
666+ console . log ( 'π Turbopuffer results count:' , turbopufferResults . length ) ;
667+ if ( turbopufferResults . length > 0 ) {
668+ console . log ( 'π First result preview:' , JSON . stringify ( turbopufferResults [ 0 ] , null , 2 ) ) ;
669+ }
670+
616671 // Deduplicate results by URL (following the original logic)
617672 for ( const result of turbopufferResults ) {
618673 const url = result . attributes ?. url ||
0 commit comments