Skip to content

Commit 6924267

Browse files
committed
improve logging for controller
1 parent d939e5d commit 6924267

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

src/vs/workbench/contrib/interactiveEditor/browser/interactiveEditorController.ts

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,26 @@ export class InteractiveEditorController implements IEditorContribution {
130130
return;
131131
}
132132

133-
this._logService.trace('[IE] session RESUMING');
133+
this._log('session RESUMING', e);
134134
await this._nextState(State.CREATE_SESSION, { existingSession });
135-
this._logService.trace('[IE] session done or paused');
135+
this._log('session done or paused');
136136
}));
137+
this._log('NEW controller');
137138
}
138139

139140
dispose(): void {
140141
this._stashedSession.clear();
141142
this._finishExistingSession();
142143
this._store.dispose();
144+
this._log('controller disposed');
145+
}
146+
147+
private _log(message: string | Error, ...more: any[]): void {
148+
if (message instanceof Error) {
149+
this._logService.error(message, ...more);
150+
} else {
151+
this._logService.trace(`[IE] (editor:${this._editor.getId()})${message}`, ...more);
152+
}
143153
}
144154

145155
getId(): string {
@@ -161,21 +171,21 @@ export class InteractiveEditorController implements IEditorContribution {
161171
}
162172

163173
async run(options: InteractiveEditorRunOptions | undefined): Promise<void> {
164-
this._logService.trace('[IE] session starting');
174+
this._log('session starting');
165175
await this._finishExistingSession();
166176
this._stashedSession.clear();
167177

168178
await this._nextState(State.CREATE_SESSION, options);
169-
this._logService.trace('[IE] session done or paused');
179+
this._log('session done or paused');
170180
}
171181

172182
private async _finishExistingSession(): Promise<void> {
173183
if (this._activeSession) {
174184
if (this._activeSession.editMode === EditMode.Preview) {
175-
this._logService.trace('[IE] finishing existing session, using CANCEL', this._activeSession.editMode);
185+
this._log('finishing existing session, using CANCEL', this._activeSession.editMode);
176186
await this.cancelSession();
177187
} else {
178-
this._logService.trace('[IE] finishing existing session, using APPLY', this._activeSession.editMode);
188+
this._log('finishing existing session, using APPLY', this._activeSession.editMode);
179189
await this.applyChanges();
180190
}
181191
}
@@ -184,7 +194,7 @@ export class InteractiveEditorController implements IEditorContribution {
184194
// ---- state machine
185195

186196
protected async _nextState(state: State, options: InteractiveEditorRunOptions | undefined): Promise<void> {
187-
this._logService.trace('[IE] setState to ', state);
197+
this._log('setState to ', state);
188198
const nextState = await this[state](options);
189199
if (nextState) {
190200
await this._nextState(nextState, options);
@@ -200,7 +210,7 @@ export class InteractiveEditorController implements IEditorContribution {
200210
if (!session) {
201211
const createSessionCts = new CancellationTokenSource();
202212
const msgListener = Event.once(this._messages.event)(m => {
203-
this._logService.trace('[IE](state=_createSession) message received', m);
213+
this._log('state=_createSession) message received', m);
204214
createSessionCts.cancel();
205215
});
206216

@@ -261,11 +271,12 @@ export class InteractiveEditorController implements IEditorContribution {
261271
this._zone.widget.updateInfo(this._activeSession.session.message ?? localize('welcome.1', "AI-generated code may be incorrect"));
262272
this._zone.show(this._activeSession.wholeRange.getEndPosition());
263273

264-
this._sessionStore.add(this._editor.onDidChangeModel(() => {
265-
this._messages.fire(this._activeSession?.lastExchange
274+
this._sessionStore.add(this._editor.onDidChangeModel((e) => {
275+
const msg = this._activeSession?.lastExchange
266276
? Message.PAUSE_SESSION // pause when switching models/tabs and when having a previous exchange
267-
: Message.CANCEL_SESSION
268-
);
277+
: Message.CANCEL_SESSION;
278+
this._log('model changed, pause or cancel session', msg, e);
279+
this._messages.fire(msg);
269280
}));
270281

271282
this._sessionStore.add(this._editor.onDidChangeModelContent(e => {
@@ -282,7 +293,7 @@ export class InteractiveEditorController implements IEditorContribution {
282293
this._activeSession!.recordExternalEditOccurred(editIsOutsideOfWholeRange);
283294

284295
if (editIsOutsideOfWholeRange) {
285-
this._logService.info('[IE] text changed outside of whole range, FINISH session');
296+
this._log('text changed outside of whole range, FINISH session');
286297
this._finishExistingSession();
287298
}
288299
}));
@@ -363,7 +374,7 @@ export class InteractiveEditorController implements IEditorContribution {
363374
} else {
364375
const barrier = new Barrier();
365376
const msgListener = Event.once(this._messages.event)(m => {
366-
this._logService.trace('[IE](state=_waitForInput) message received', m);
377+
this._log('state=_waitForInput) message received', m);
367378
message = m;
368379
barrier.open();
369380
});
@@ -397,7 +408,7 @@ export class InteractiveEditorController implements IEditorContribution {
397408

398409
const refer = this._activeSession.session.slashCommands?.some(value => value.refer && input!.startsWith(`/${value.command}`));
399410
if (refer) {
400-
this._logService.info('[IE] seeing refer command, continuing outside editor', this._activeSession.provider.debugName);
411+
this._log('[IE] seeing refer command, continuing outside editor', this._activeSession.provider.debugName);
401412
this._editor.setSelection(this._activeSession.wholeRange);
402413
this._instaService.invokeFunction(sendRequest, input);
403414

@@ -421,7 +432,7 @@ export class InteractiveEditorController implements IEditorContribution {
421432

422433
let message = Message.NONE;
423434
const msgListener = Event.once(this._messages.event)(m => {
424-
this._logService.trace('[IE](state=_makeRequest) message received', m);
435+
this._log('state=_makeRequest) message received', m);
425436
message = m;
426437
requestCts.cancel();
427438
});
@@ -438,7 +449,7 @@ export class InteractiveEditorController implements IEditorContribution {
438449
attempt: 0,
439450
};
440451
const task = this._activeSession.provider.provideResponse(this._activeSession.session, request, requestCts.token);
441-
this._logService.trace('[IE] request started', this._activeSession.provider.debugName, this._activeSession.session, request);
452+
this._log('request started', this._activeSession.provider.debugName, this._activeSession.session, request);
442453

443454
let response: EditResponse | MarkdownResponse | ErrorResponse | EmptyResponse;
444455
let reply: IInteractiveEditorResponse | null | undefined;
@@ -463,7 +474,7 @@ export class InteractiveEditorController implements IEditorContribution {
463474
this._ctxHasActiveRequest.set(false);
464475
this._zone.widget.updateProgress(false);
465476
this._zone.widget.updateInfo('');
466-
this._logService.trace('[IE] request took', sw.elapsed(), this._activeSession.provider.debugName);
477+
this._log('request took', sw.elapsed(), this._activeSession.provider.debugName);
467478

468479
}
469480

@@ -497,7 +508,7 @@ export class InteractiveEditorController implements IEditorContribution {
497508
}
498509
const moreMinimalEdits = (await this._editorWorkerService.computeHumanReadableDiff(this._activeSession.textModelN.uri, response.localEdits));
499510
const editOperations = (moreMinimalEdits ?? response.localEdits).map(edit => EditOperation.replace(Range.lift(edit.range), edit.text));
500-
this._logService.trace('[IE] edits from PROVIDER and after making them MORE MINIMAL', this._activeSession.provider.debugName, response.localEdits, moreMinimalEdits);
511+
this._log('edits from PROVIDER and after making them MORE MINIMAL', this._activeSession.provider.debugName, response.localEdits, moreMinimalEdits);
501512

502513
const textModelNplus1 = this._modelService.createModel(createTextBufferFactoryFromSnapshot(this._activeSession.textModelN.createSnapshot()), null, undefined, true);
503514
textModelNplus1.applyEdits(editOperations);
@@ -682,8 +693,8 @@ export class InteractiveEditorController implements IEditorContribution {
682693
await strategy?.apply();
683694
} catch (err) {
684695
this._dialogService.error(localize('err.apply', "Failed to apply changes.", toErrorMessage(err)));
685-
this._logService.error('[IE] FAILED to apply changes');
686-
this._logService.error(err);
696+
this._log('FAILED to apply changes');
697+
this._log(err);
687698
}
688699
strategy?.dispose();
689700
this._messages.fire(Message.ACCEPT_SESSION);
@@ -702,8 +713,8 @@ export class InteractiveEditorController implements IEditorContribution {
702713
await strategy?.cancel();
703714
} catch (err) {
704715
this._dialogService.error(localize('err.discard', "Failed to discard changes.", toErrorMessage(err)));
705-
this._logService.error('[IE] FAILED to discard changes');
706-
this._logService.error(err);
716+
this._log('FAILED to discard changes');
717+
this._log(err);
707718
}
708719
strategy?.dispose();
709720
this._messages.fire(Message.CANCEL_SESSION);

src/vs/workbench/contrib/interactiveEditor/browser/interactiveEditorSession.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ export class InteractiveEditorSessionService implements IInteractiveEditorSessio
386386
data.session.session.dispose?.();
387387

388388
map.delete(textModelN.uri);
389+
this._logService.trace(`[IE] did RELEASE session for ${editor.getId()}, ${session.provider.debugName}`);
390+
389391
}
390392
if (map.size === 0) {
391393
this._sessions.delete(editor);

0 commit comments

Comments
 (0)