1- import { log } from 'console' ;
1+ import { log , warn } from 'console' ;
22import sqlite3 from 'better-sqlite3' ;
33
44import { ContextV2 , applyContext } from '@appland/navie' ;
55import { SearchRpc } from '@appland/rpc' ;
6- import { FileIndex , FileSearchResult } from '@appland/search' ;
6+ import { FileIndex , FileSearchResult , SnippetSearchResult } from '@appland/search' ;
77
88import { SearchResponse as AppMapSearchResponse } from '../../fulltext/appmap-match' ;
99import { DEFAULT_MAX_DIAGRAMS } from '../search/search' ;
10- import EventCollector from './EventCollector' ;
1110import indexFiles from './index-files' ;
1211import indexSnippets from './index-snippets' ;
13- import collectSnippets from './collect-snippets' ;
1412import buildIndex from './buildIndex' ;
1513import { buildAppMapIndex , search } from '../../fulltext/appmap-index' ;
14+ import indexEvents from './index-events' ;
15+
16+ type ContextCandidate = {
17+ results : SearchRpc . SearchResult [ ] ;
18+ context : ContextV2 . ContextResponse ;
19+ contextSize : number ;
20+ } ;
1621
1722export default class SearchContextCollector {
1823 public excludePatterns : RegExp [ ] | undefined ;
@@ -98,41 +103,80 @@ export default class SearchContextCollector {
98103
99104 const snippetIndex = await buildIndex ( 'snippets' , async ( indexFile ) => {
100105 const db = new sqlite3 ( indexFile ) ;
101- return await indexSnippets ( db , fileSearchResults ) ;
106+ const snippetIndex = await indexSnippets ( db , fileSearchResults ) ;
107+ await indexEvents ( snippetIndex , appmapSearchResponse . results ) ;
108+ return snippetIndex ;
102109 } ) ;
103110
104- let contextCandidate : {
105- results : SearchRpc . SearchResult [ ] ;
106- context : ContextV2 . ContextResponse ;
107- contextSize : number ;
108- } ;
111+ let contextCandidate : ContextCandidate ;
109112 try {
110- const eventsCollector = new EventCollector ( this . vectorTerms . join ( ' ' ) , appmapSearchResponse ) ;
111-
112113 let charCount = 0 ;
113- let maxEventsPerDiagram = 5 ;
114+ let maxSnippets = 50 ;
114115 log ( `[search-context] Requested char limit: ${ this . charLimit } ` ) ;
115116 for ( ; ; ) {
116- log ( `[search-context] Collecting context with ${ maxEventsPerDiagram } events per diagram.` ) ;
117-
118- contextCandidate = await eventsCollector . collectEvents (
119- maxEventsPerDiagram ,
120- this . excludePatterns ,
121- this . includePatterns ,
122- this . includeTypes
117+ log ( `[search-context] Collecting context with ${ maxSnippets } events per diagram.` ) ;
118+
119+ // Collect all code objects from AppMaps and use them to build the sequence diagram
120+ // const codeSnippets = new Array<SnippetSearchResult>();
121+ // TODO: Apply this.includeTypes
122+
123+ const snippetContextItem = (
124+ snippet : SnippetSearchResult
125+ ) : ContextV2 . ContextItem | ContextV2 . FileContextItem | undefined => {
126+ const { snippetId, directory, score, content } = snippet ;
127+
128+ const { type : snippetIdType , id : snippetIdValue } = snippetId ;
129+
130+ let location : string | undefined ;
131+ if ( snippetIdType === 'code-snippet' ) location = snippetIdValue ;
132+
133+ switch ( snippetId . type ) {
134+ case 'query' :
135+ case 'route' :
136+ case 'external-route' :
137+ return {
138+ type : ContextV2 . ContextItemType . DataRequest ,
139+ content,
140+ directory,
141+ score,
142+ } ;
143+ case 'code-snippet' :
144+ return {
145+ type : ContextV2 . ContextItemType . CodeSnippet ,
146+ content,
147+ directory,
148+ score,
149+ location,
150+ } ;
151+ default :
152+ warn ( `[search-context] Unknown snippet type: ${ snippetId . type } ` ) ;
153+
154+ // TODO: Collect all matching events, then build a sequence diagram
155+ // case 'event':
156+ // return await buildSequenceDiagram(snippet);
157+ // default:
158+ // codeSnippets.push(snippet);
159+ }
160+ } ;
161+
162+ const snippetSearchResults = snippetIndex . index . searchSnippets (
163+ this . vectorTerms . join ( ' OR ' ) ,
164+ maxSnippets
123165 ) ;
166+ const context : ContextV2 . ContextItem [ ] = [ ] ;
167+ for ( const result of snippetSearchResults ) {
168+ const contextItem = snippetContextItem ( result ) ;
169+ if ( contextItem ) context . push ( contextItem ) ;
170+ }
124171
125- const codeSnippetCount = contextCandidate . context . filter (
126- ( item ) => item . type === ContextV2 . ContextItemType . CodeSnippet
127- ) . length ;
172+ // TODO: Build sequence diagrams
128173
129- const charLimit = codeSnippetCount === 0 ? this . charLimit : this . charLimit / 4 ;
130- const sourceContext = collectSnippets (
131- snippetIndex . index ,
132- this . vectorTerms . join ( ' OR ' ) ,
133- charLimit
134- ) ;
135- contextCandidate . context = contextCandidate . context . concat ( sourceContext ) ;
174+ contextCandidate = {
175+ // TODO: Fixme remove hard coded cast
176+ results : appmapSearchResponse . results as SearchRpc . SearchResult [ ] ,
177+ context,
178+ contextSize : snippetSearchResults . reduce ( ( acc , result ) => acc + result . content . length , 0 ) ,
179+ } ;
136180
137181 const appliedContext = applyContext ( contextCandidate . context , this . charLimit ) ;
138182 const appliedContextSize = appliedContext . reduce (
@@ -147,8 +191,8 @@ export default class SearchContextCollector {
147191 break ;
148192 }
149193 charCount = appliedContextSize ;
150- maxEventsPerDiagram = Math . ceil ( maxEventsPerDiagram * 1.5 ) ;
151- log ( `[search-context] Increasing max events per diagram to ${ maxEventsPerDiagram } .` ) ;
194+ maxSnippets = Math . ceil ( maxSnippets * 1.5 ) ;
195+ log ( `[search-context] Increasing max events per diagram to ${ maxSnippets } .` ) ;
152196 }
153197 } finally {
154198 snippetIndex . close ( ) ;
0 commit comments