@@ -27,18 +27,44 @@ interface AIResponse {
2727 tables : AIGeneratedTableSettings [ ] ;
2828}
2929
30+ const AI_BATCH_SIZE = 10 ;
31+
3032@Injectable ( )
3133export class AiService {
3234 constructor ( protected readonly aiCoreService : AICoreService ) { }
3335
3436 public async generateNewTableSettingsWithAI (
3537 tablesInformation : Array < TableInformation > ,
3638 ) : Promise < Array < TableSettingsEntity > > {
39+ const allSettings : Array < TableSettingsEntity > = [ ] ;
40+
41+ for ( let i = 0 ; i < tablesInformation . length ; i += AI_BATCH_SIZE ) {
42+ const batch = tablesInformation . slice ( i , i + AI_BATCH_SIZE ) ;
43+ try {
44+ const batchSettings = await this . processTablesBatch ( batch ) ;
45+ allSettings . push ( ...batchSettings ) ;
46+ } catch ( error ) {
47+ console . warn ( `Batch processing failed, falling back to individual table processing: ${ error . message } ` ) ;
48+ for ( const tableInfo of batch ) {
49+ try {
50+ const singleTableSettings = await this . processTablesBatch ( [ tableInfo ] ) ;
51+ allSettings . push ( ...singleTableSettings ) ;
52+ } catch ( singleError ) {
53+ console . error ( `Error processing AI for table "${ tableInfo . table_name } ": ${ singleError . message } ` ) ;
54+ }
55+ }
56+ }
57+ }
58+
59+ return allSettings ;
60+ }
61+
62+ private async processTablesBatch ( tablesInformation : Array < TableInformation > ) : Promise < Array < TableSettingsEntity > > {
3763 const prompt = this . buildPrompt ( tablesInformation ) ;
3864 const aiResponse = await this . aiCoreService . completeWithProvider ( AIProviderType . BEDROCK , prompt , {
3965 temperature : 0.3 ,
4066 } ) ;
41- const parsedResponse = this . parseAIResponse ( aiResponse ) ;
67+ const parsedResponse = this . parseAIResponse ( aiResponse , tablesInformation ) ;
4268 return this . buildTableSettingsEntities ( parsedResponse , tablesInformation ) ;
4369 }
4470
@@ -131,13 +157,14 @@ Respond ONLY with valid JSON in this exact format (no markdown, no explanations)
131157}` ;
132158 }
133159
134- private parseAIResponse ( aiResponse : string ) : AIResponse {
160+ private parseAIResponse ( aiResponse : string , tablesInformation : Array < TableInformation > ) : AIResponse {
135161 const cleanedResponse = cleanAIJsonResponse ( aiResponse ) ;
162+ const tableNames = tablesInformation . map ( ( t ) => t . table_name ) ;
136163
137164 try {
138165 return JSON . parse ( cleanedResponse ) as AIResponse ;
139166 } catch ( error ) {
140- throw new Error ( `Failed to parse AI response: ${ error . message } ` ) ;
167+ throw new Error ( `Failed to parse AI response for tables [ ${ tableNames . join ( ', ' ) } ] : ${ error . message } ` ) ;
141168 }
142169 }
143170
@@ -156,7 +183,9 @@ Respond ONLY with valid JSON in this exact format (no markdown, no explanations)
156183 settings . readonly_fields = this . filterValidColumns ( tableSettings . readonly_fields , validColumnNames ) ;
157184 settings . columns_view = this . filterValidColumns ( tableSettings . columns_view , validColumnNames ) ;
158185 settings . ordering = this . mapOrdering ( tableSettings . ordering ) ;
159- settings . ordering_field = validColumnNames . includes ( tableSettings . ordering_field ) ? tableSettings . ordering_field : null ;
186+ settings . ordering_field = validColumnNames . includes ( tableSettings . ordering_field )
187+ ? tableSettings . ordering_field
188+ : null ;
160189 settings . table_widgets = tableSettings . widgets
161190 . filter ( ( w ) => validColumnNames . includes ( w . field_name ) )
162191 . map ( ( widgetData ) => {
0 commit comments