1- import { EmbedContentResponse , GenerativeModel , GoogleGenerativeAI } from "@google/generative-ai" ;
1+ import {
2+ EmbedContentResponse ,
3+ GenerativeModel ,
4+ GoogleGenerativeAI ,
5+ } from "@google/generative-ai" ;
26import { EmbeddingsConfig } from "../application/constant" ;
37import { IFunctionData } from "../application/interfaces" ;
48import { Logger , LogLevel } from "../infrastructure/logger/logger" ;
@@ -24,7 +28,8 @@ interface BatchProcessResult {
2428 * @class EmbeddingService
2529 */
2630export class EmbeddingService {
27- private static readonly DEFAULT_OPTIONS : Required < EmbeddingServiceOptions > = EmbeddingsConfig ;
31+ private static readonly DEFAULT_OPTIONS : Required < EmbeddingServiceOptions > =
32+ EmbeddingsConfig ;
2833
2934 private readonly options : Required < EmbeddingServiceOptions > ;
3035 private readonly requestInterval : number ;
@@ -56,7 +61,10 @@ export class EmbeddingService {
5661 try {
5762 const vscode = require ( "vscode" ) ;
5863 const config = vscode . workspace ?. getConfiguration ?.( ) ;
59- return ( config ?. get ( "codebuddy.embeddingModel" ) as string ) || "gemini-2.0-flash" ;
64+ return (
65+ ( config ?. get ( "codebuddy.embeddingModel" ) as string ) ||
66+ "gemini-2.0-flash"
67+ ) ;
6068 } catch {
6169 // Fallback if vscode module is not available (e.g., in tests)
6270 return "gemini-2.0-flash" ;
@@ -171,7 +179,9 @@ export class EmbeddingService {
171179 * @returns {Promise<IFunctionData> } The function data with the generated embeddings.
172180 * @memberof EmbeddingService
173181 */
174- private async generateFunctionEmbeddings ( item : IFunctionData ) : Promise < IFunctionData > {
182+ private async generateFunctionEmbeddings (
183+ item : IFunctionData ,
184+ ) : Promise < IFunctionData > {
175185 try {
176186 const embedding = await this . generateEmbedding ( item . compositeText ) ;
177187 return {
@@ -202,7 +212,7 @@ export class EmbeddingService {
202212 private async processBatchWithRetry (
203213 batch : IFunctionData [ ] ,
204214 lastRequestTime : number ,
205- forEmbedding : boolean
215+ forEmbedding : boolean ,
206216 ) : Promise < BatchProcessResult > {
207217 let retries = 0 ;
208218 const generateComments : IFunctionData [ ] = [ ] ;
@@ -216,17 +226,23 @@ export class EmbeddingService {
216226 }
217227
218228 if ( forEmbedding ) {
219- const embeddings = await Promise . all ( batch . map ( ( item ) => this . generateFunctionEmbeddings ( item ) ) ) ;
229+ const embeddings = await Promise . all (
230+ batch . map ( ( item ) => this . generateFunctionEmbeddings ( item ) ) ,
231+ ) ;
220232 generateEmbeddings . push ( ...embeddings . filter ( Boolean ) ) ;
221233 } else {
222234 const comments = await Promise . all (
223235 batch . map ( ( item ) => {
224236 if ( ! item . description ) {
225237 return item . content ? this . generateText ( item ) : null ;
226238 }
227- } )
239+ } ) ,
240+ ) ;
241+ generateComments . push (
242+ ...comments . filter (
243+ ( comment ) : comment is IFunctionData => comment !== null ,
244+ ) ,
228245 ) ;
229- generateComments . push ( ...comments . filter ( ( comment ) : comment is IFunctionData => comment !== null ) ) ;
230246 }
231247
232248 return {
@@ -262,7 +278,10 @@ export class EmbeddingService {
262278 * @returns {Promise<IFunctionData[]> } The processed function data, including any generated embeddings or text.
263279 * @memberof EmbeddingService
264280 */
265- public async processFunctions ( data : IFunctionData [ ] , forEmbedding = false ) : Promise < IFunctionData [ ] > {
281+ public async processFunctions (
282+ data : IFunctionData [ ] ,
283+ forEmbedding = false ,
284+ ) : Promise < IFunctionData [ ] > {
266285 try {
267286 const result = await this . processWithRateLimit ( data , forEmbedding ) ;
268287
@@ -290,7 +309,7 @@ export class EmbeddingService {
290309 */
291310 private async processWithRateLimit (
292311 data : IFunctionData [ ] ,
293- forEmbedding : boolean
312+ forEmbedding : boolean ,
294313 ) : Promise < {
295314 successful : IFunctionData [ ] ;
296315 failed : IFunctionData [ ] ;
@@ -304,9 +323,17 @@ export class EmbeddingService {
304323
305324 try {
306325 await this . delay ( 60000 ) ;
307- const result = await this . processBatchWithRetry ( batch , lastRequestTime , forEmbedding ) ;
326+ const result = await this . processBatchWithRetry (
327+ batch ,
328+ lastRequestTime ,
329+ forEmbedding ,
330+ ) ;
308331
309- successful . push ( ...( forEmbedding ? result . generateEmbeddings : result . generateComments ) ) ;
332+ successful . push (
333+ ...( forEmbedding
334+ ? result . generateEmbeddings
335+ : result . generateComments ) ,
336+ ) ;
310337 lastRequestTime = Date . now ( ) ;
311338
312339 this . logger . info ( `Batch processed` , { startIndex : i } ) ;
@@ -315,7 +342,9 @@ export class EmbeddingService {
315342 startIndex : i ,
316343 error,
317344 } ) ;
318- failed . push ( ...batch . map ( ( item ) => ( { ...item , error : error as Error } ) ) ) ;
345+ failed . push (
346+ ...batch . map ( ( item ) => ( { ...item , error : error as Error } ) ) ,
347+ ) ;
319348 }
320349 }
321350
0 commit comments