@@ -5,6 +5,11 @@ import * as fs from 'node:fs/promises';
55import * as path from 'node:path' ;
66import { getLogger } from './logger' ;
77
8+ type PerLanguageData = {
9+ method ?: string ;
10+ example ?: string ;
11+ } ;
12+
813type MethodEntry = {
914 name : string ;
1015 endpoint : string ;
@@ -16,6 +21,7 @@ type MethodEntry = {
1621 params ?: string [ ] ;
1722 response ?: string ;
1823 markdown ?: string ;
24+ perLanguage ?: Record < string , PerLanguageData > ;
1925} ;
2026
2127type ProseChunk = {
@@ -318,6 +324,8 @@ const EMBEDDED_METHODS: MethodEntry[] = [
318324 } ,
319325] ;
320326
327+ const EMBEDDED_READMES : { language : string ; content : string } [ ] = [ ] ;
328+
321329const INDEX_OPTIONS = {
322330 fields : [
323331 'name' ,
@@ -332,13 +340,15 @@ const INDEX_OPTIONS = {
332340 storeFields : [ 'kind' , '_original' ] ,
333341 searchOptions : {
334342 prefix : true ,
335- fuzzy : 0.2 ,
343+ fuzzy : 0.1 ,
336344 boost : {
337- name : 3 ,
338- endpoint : 2 ,
345+ name : 5 ,
346+ stainlessPath : 3 ,
347+ endpoint : 3 ,
348+ qualified : 3 ,
339349 summary : 2 ,
340- qualified : 2 ,
341350 content : 1 ,
351+ description : 1 ,
342352 } as Record < string , number > ,
343353 } ,
344354} ;
@@ -360,30 +370,45 @@ export class LocalDocsSearch {
360370 static async create ( opts ?: { docsDir ?: string } ) : Promise < LocalDocsSearch > {
361371 const instance = new LocalDocsSearch ( ) ;
362372 instance . indexMethods ( EMBEDDED_METHODS ) ;
373+ for ( const readme of EMBEDDED_READMES ) {
374+ instance . indexProse ( readme . content , `readme:${ readme . language } ` ) ;
375+ }
363376 if ( opts ?. docsDir ) {
364377 await instance . loadDocsDirectory ( opts . docsDir ) ;
365378 }
366379 return instance ;
367380 }
368381
369- // Note: Language is accepted for interface consistency with remote search, but currently has no
370- // effect since this local search only supports TypeScript docs.
371382 search ( props : {
372383 query : string ;
373384 language ?: string ;
374385 detail ?: string ;
375386 maxResults ?: number ;
376387 maxLength ?: number ;
377388 } ) : SearchResult {
378- const { query, detail = 'default' , maxResults = 5 , maxLength = 100_000 } = props ;
389+ const { query, language = 'typescript' , detail = 'default' , maxResults = 5 , maxLength = 100_000 } = props ;
379390
380391 const useMarkdown = detail === 'verbose' || detail === 'high' ;
381392
382- // Search both indices and merge results by score
393+ // Search both indices and merge results by score.
394+ // Filter prose hits so language-tagged content (READMEs and docs with
395+ // frontmatter) only matches the requested language.
383396 const methodHits = this . methodIndex
384397 . search ( query )
385398 . map ( ( hit ) => ( { ...hit , _kind : 'http_method' as const } ) ) ;
386- const proseHits = this . proseIndex . search ( query ) . map ( ( hit ) => ( { ...hit , _kind : 'prose' as const } ) ) ;
399+ const proseHits = this . proseIndex
400+ . search ( query )
401+ . filter ( ( hit ) => {
402+ const source = ( ( hit as Record < string , unknown > ) [ '_original' ] as ProseChunk | undefined ) ?. source ;
403+ if ( ! source ) return true ;
404+ // Check for language-tagged sources: "readme:<lang>" or "lang:<lang>:<filename>"
405+ let taggedLang : string | undefined ;
406+ if ( source . startsWith ( 'readme:' ) ) taggedLang = source . slice ( 'readme:' . length ) ;
407+ else if ( source . startsWith ( 'lang:' ) ) taggedLang = source . split ( ':' ) [ 1 ] ;
408+ if ( ! taggedLang ) return true ;
409+ return taggedLang === language || ( language === 'javascript' && taggedLang === 'typescript' ) ;
410+ } )
411+ . map ( ( hit ) => ( { ...hit , _kind : 'prose' as const } ) ) ;
387412 const merged = [ ...methodHits , ...proseHits ] . sort ( ( a , b ) => b . score - a . score ) ;
388413 const top = merged . slice ( 0 , maxResults ) ;
389414
@@ -396,11 +421,16 @@ export class LocalDocsSearch {
396421 if ( useMarkdown && m . markdown ) {
397422 fullResults . push ( m . markdown ) ;
398423 } else {
424+ // Use per-language data when available, falling back to the
425+ // top-level fields (which are TypeScript-specific in the
426+ // legacy codepath).
427+ const langData = m . perLanguage ?. [ language ] ;
399428 fullResults . push ( {
400- method : m . qualified ,
429+ method : langData ?. method ?? m . qualified ,
401430 summary : m . summary ,
402431 description : m . description ,
403432 endpoint : `${ m . httpMethod . toUpperCase ( ) } ${ m . endpoint } ` ,
433+ ...( langData ?. example ? { example : langData . example } : { } ) ,
404434 ...( m . params ? { params : m . params } : { } ) ,
405435 ...( m . response ? { response : m . response } : { } ) ,
406436 } ) ;
@@ -471,7 +501,19 @@ export class LocalDocsSearch {
471501 this . indexProse ( texts . join ( '\n\n' ) , file . name ) ;
472502 }
473503 } else {
474- this . indexProse ( content , file . name ) ;
504+ // Parse optional YAML frontmatter for language tagging.
505+ // Files with a "language" field in frontmatter will only
506+ // surface in searches for that language.
507+ //
508+ // Example:
509+ // ---
510+ // language: python
511+ // ---
512+ // # Error handling in Python
513+ // ...
514+ const frontmatter = parseFrontmatter ( content ) ;
515+ const source = frontmatter . language ? `lang:${ frontmatter . language } :${ file . name } ` : file . name ;
516+ this . indexProse ( content , source ) ;
475517 }
476518 } catch ( err ) {
477519 getLogger ( ) . warn ( { err, file : file . name } , 'Failed to index docs file' ) ;
@@ -549,3 +591,12 @@ function extractTexts(data: unknown, depth = 0): string[] {
549591 }
550592 return [ ] ;
551593}
594+
595+ /** Parses YAML frontmatter from a markdown string, extracting the language field if present. */
596+ function parseFrontmatter ( markdown : string ) : { language ?: string } {
597+ const match = markdown . match ( / ^ - - - \n ( [ \s \S ] * ?) \n - - - / ) ;
598+ if ( ! match ) return { } ;
599+ const body = match [ 1 ] ?? '' ;
600+ const langMatch = body . match ( / ^ l a n g u a g e : \s * ( .+ ) $ / m) ;
601+ return langMatch ? { language : langMatch [ 1 ] ! . trim ( ) } : { } ;
602+ }
0 commit comments