@@ -22,7 +22,13 @@ import { CancellationTokenSource } from "vscode";
2222// Note that the model may return fewer than this number of candidates.
2323const candidateBatchSize = 20 ;
2424
25+ /**
26+ * The auto-modeler holds state around auto-modeling jobs and allows
27+ * starting and stopping them.
28+ */
2529export class AutoModeler {
30+ // Keep track of auto-modeling jobs that are in progress
31+ // so that we can stop them.
2632 private readonly jobs : Map < string , CancellationTokenSource > ;
2733
2834 constructor (
@@ -42,6 +48,14 @@ export class AutoModeler {
4248 this . jobs = new Map < string , CancellationTokenSource > ( ) ;
4349 }
4450
51+ /**
52+ * Models the given package's external API usages, except
53+ * the ones that are already modeled.
54+ * @param packageName The name of the package to model.
55+ * @param externalApiUsages The external API usages.
56+ * @param modeledMethods The currently modeled methods.
57+ * @param mode The mode we are modeling in.
58+ */
4559 public async startModeling (
4660 packageName : string ,
4761 externalApiUsages : ExternalApiUsage [ ] ,
@@ -68,6 +82,10 @@ export class AutoModeler {
6882 }
6983 }
7084
85+ /**
86+ * Stops modeling the given package.
87+ * @param packageName The name of the package to stop modeling.
88+ */
7189 public async stopModeling ( packageName : string ) : Promise < void > {
7290 void extLogger . log ( `Stopping modeling for package ${ packageName } ` ) ;
7391 const cancellationTokenSource = this . jobs . get ( packageName ) ;
@@ -76,6 +94,9 @@ export class AutoModeler {
7694 }
7795 }
7896
97+ /**
98+ * Stops all in-progress modeling jobs.
99+ */
79100 public async stopAllModeling ( ) : Promise < void > {
80101 for ( const cancellationTokenSource of this . jobs . values ( ) ) {
81102 cancellationTokenSource . cancel ( ) ;
@@ -91,8 +112,6 @@ export class AutoModeler {
91112 ) : Promise < void > {
92113 void extLogger . log ( `Modeling package ${ packageName } ` ) ;
93114 await withProgress ( async ( progress ) => {
94- const maxStep = 3000 ;
95-
96115 // Fetch the candidates to send to the model
97116 const allCandidateMethods = getCandidates (
98117 mode ,
@@ -112,6 +131,7 @@ export class AutoModeler {
112131 ) ;
113132 try {
114133 for ( let i = 0 ; i < batchNumber ; i ++ ) {
134+ // Check if we should stop
115135 if ( cancellationTokenSource . token . isCancellationRequested ) {
116136 break ;
117137 }
@@ -120,24 +140,17 @@ export class AutoModeler {
120140 const end = start + candidateBatchSize ;
121141 const candidatesToProcess = allCandidateMethods . slice ( start , end ) ;
122142
143+ // Let the UI know which candidates we are modeling
123144 await this . setInProgressMethods (
124145 packageName ,
125146 candidatesToProcess . map ( ( c ) => c . signature ) ,
126147 ) ;
127148
128- progress ( {
129- step : 1800 + i * 100 ,
130- maxStep,
131- message : `Automodeling candidates, batch ${
132- i + 1
133- } of ${ batchNumber } `,
134- } ) ;
135-
149+ // Kick off the process to model the slice of candidates
136150 await this . modelCandidates (
137151 candidatesToProcess ,
138152 mode ,
139153 progress ,
140- maxStep ,
141154 cancellationTokenSource ,
142155 ) ;
143156 }
@@ -152,48 +165,33 @@ export class AutoModeler {
152165 candidateMethods : MethodSignature [ ] ,
153166 mode : Mode ,
154167 progress : ProgressCallback ,
155- maxStep : number ,
156168 cancellationTokenSource : CancellationTokenSource ,
157169 ) : Promise < void > {
170+ void extLogger . log ( "Executing auto-model queries" ) ;
171+
158172 const usages = await runAutoModelQueries ( {
159173 mode,
160174 candidateMethods,
161175 cliServer : this . cliServer ,
162176 queryRunner : this . queryRunner ,
163177 queryStorageDir : this . queryStorageDir ,
164178 databaseItem : this . databaseItem ,
165- progress : ( update ) => progress ( { ...update , maxStep } ) ,
179+ progress : ( update ) => progress ( { ...update } ) ,
166180 cancellationTokenSource,
167181 } ) ;
168182 if ( ! usages ) {
169183 return ;
170184 }
171185
172- progress ( {
173- step : 1800 ,
174- maxStep,
175- message : "Creating request" ,
176- } ) ;
177-
178186 const request = await createAutoModelV2Request ( mode , usages ) ;
179187
180- progress ( {
181- step : 2000 ,
182- maxStep,
183- message : "Sending request" ,
184- } ) ;
188+ void extLogger . log ( "Calling auto-model API" ) ;
185189
186190 const response = await this . callAutoModelApi ( request ) ;
187191 if ( ! response ) {
188192 return ;
189193 }
190194
191- progress ( {
192- step : 2500 ,
193- maxStep,
194- message : "Parsing response" ,
195- } ) ;
196-
197195 const models = loadYaml ( response . models , {
198196 filename : "auto-model.yml" ,
199197 } ) ;
@@ -224,12 +222,6 @@ export class AutoModeler {
224222 }
225223 }
226224
227- progress ( {
228- step : 2800 ,
229- maxStep,
230- message : "Applying results" ,
231- } ) ;
232-
233225 await this . addModeledMethods ( loadedMethods ) ;
234226 }
235227
0 commit comments