Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

joh/inline progress #187705

Merged
merged 4 commits into from Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/vs/editor/common/languages.ts
Expand Up @@ -13,7 +13,7 @@ import { IMarkdownString } from 'vs/base/common/htmlContent';
import { IDisposable } from 'vs/base/common/lifecycle';
import { ThemeIcon } from 'vs/base/common/themables';
import { URI, UriComponents } from 'vs/base/common/uri';
import { ISingleEditOperation } from 'vs/editor/common/core/editOperation';
import { EditOperation, ISingleEditOperation } from 'vs/editor/common/core/editOperation';
import { IPosition, Position } from 'vs/editor/common/core/position';
import { IRange, Range } from 'vs/editor/common/core/range';
import { Selection } from 'vs/editor/common/core/selection';
Expand Down Expand Up @@ -1222,6 +1222,13 @@ export interface TextEdit {
eol?: model.EndOfLineSequence;
}

/** @internal */
export abstract class TextEdit {
static asEditOperation(edit: TextEdit): ISingleEditOperation {
return EditOperation.replace(Range.lift(edit.range), edit.text);
}
}

/**
* Interface used to format a model
*/
Expand Down
20 changes: 18 additions & 2 deletions src/vs/platform/progress/common/progress.ts
Expand Up @@ -111,15 +111,31 @@ export class Progress<T> implements IProgress<T> {

static readonly None = Object.freeze<IProgress<unknown>>({ report() { } });

report: (item: T) => void;

private _value?: T;
get value(): T | undefined { return this._value; }

constructor(private callback: (data: T) => void) { }
private _lastTask?: Promise<unknown>;

constructor(private callback: (data: T) => unknown, opts?: { async?: boolean }) {
this.report = opts?.async
? this._reportAsync.bind(this)
: this._reportSync.bind(this);
}

report(item: T) {
private _reportSync(item: T) {
this._value = item;
this.callback(this._value);
}

private _reportAsync(item: T) {
Promise.resolve(this._lastTask).finally(() => {
this._value = item;
const r = this.callback(this._value);
this._lastTask = Promise.resolve(r).finally(() => this._lastTask = undefined);
});
}
}

/**
Expand Down
23 changes: 18 additions & 5 deletions src/vs/workbench/api/browser/mainThreadInlineChat.ts
Expand Up @@ -9,13 +9,17 @@ import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity'
import { reviveWorkspaceEditDto } from 'vs/workbench/api/browser/mainThreadBulkEdits';
import { ExtHostContext, ExtHostInlineChatShape, MainContext, MainThreadInlineChatShape as MainThreadInlineChatShape } from 'vs/workbench/api/common/extHost.protocol';
import { IExtHostContext, extHostNamedCustomer } from 'vs/workbench/services/extensions/common/extHostCustomers';
import { TextEdit } from 'vs/editor/common/languages';
import { IProgress } from 'vs/platform/progress/common/progress';

@extHostNamedCustomer(MainContext.MainThreadInlineChat)
export class MainThreadInlineChat implements MainThreadInlineChatShape {

private readonly _registrations = new DisposableMap<number>();
private readonly _proxy: ExtHostInlineChatShape;

private readonly _progresses = new Map<string, IProgress<any>>();

constructor(
extHostContext: IExtHostContext,
@IInlineChatService private readonly _inlineChatService: IInlineChatService,
Expand Down Expand Up @@ -43,12 +47,17 @@ export class MainThreadInlineChat implements MainThreadInlineChatShape {
}
};
},
provideResponse: async (item, request, token) => {
const result = await this._proxy.$provideResponse(handle, item, request, token);
if (result?.type === 'bulkEdit') {
(<IInlineChatBulkEditResponse>result).edits = reviveWorkspaceEditDto(result.edits, this._uriIdentService);
provideResponse: async (item, request, progress, token) => {
this._progresses.set(request.requestId, progress);
try {
const result = await this._proxy.$provideResponse(handle, item, request, token);
if (result?.type === 'bulkEdit') {
(<IInlineChatBulkEditResponse>result).edits = reviveWorkspaceEditDto(result.edits, this._uriIdentService);
}
return <IInlineChatResponse | undefined>result;
} finally {
this._progresses.delete(request.requestId);
}
return <IInlineChatResponse | undefined>result;
},
handleInlineChatResponseFeedback: !supportsFeedback ? undefined : async (session, response, kind) => {
this._proxy.$handleFeedback(handle, session.id, response.id, kind);
Expand All @@ -58,6 +67,10 @@ export class MainThreadInlineChat implements MainThreadInlineChatShape {
this._registrations.set(handle, unreg);
}

async $handleProgressChunk(requestId: string, chunk: { message?: string | undefined; edits?: TextEdit[] | undefined }): Promise<void> {
this._progresses.get(requestId)?.report(chunk);
}

async $unregisterInteractiveEditorProvider(handle: number): Promise<void> {
this._registrations.deleteAndDispose(handle);
}
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHost.protocol.ts
Expand Up @@ -1121,6 +1121,7 @@ export interface MainThreadInteractiveShape extends IDisposable {

export interface MainThreadInlineChatShape extends IDisposable {
$registerInteractiveEditorProvider(handle: number, debugName: string, supportsFeedback: boolean): Promise<void>;
$handleProgressChunk(requestId: string, chunk: { message?: string; edits?: languages.TextEdit[] }): Promise<void>;
$unregisterInteractiveEditorProvider(handle: number): Promise<void>;
}

Expand Down
33 changes: 31 additions & 2 deletions src/vs/workbench/api/common/extHostInlineChat.ts
Expand Up @@ -139,13 +139,42 @@ export class ExtHostInteractiveEditor implements ExtHostInlineChatShape {
return;
}

const res = await entry.provider.provideInteractiveEditorResponse({
const apiRequest: vscode.InteractiveEditorRequest = {
session: sessionData.session,
prompt: request.prompt,
selection: typeConvert.Selection.to(request.selection),
wholeRange: typeConvert.Range.to(request.wholeRange),
attempt: request.attempt,
}, token);
live: request.live,
};


let done = false;
const progress: vscode.Progress<{ message?: string; edits?: vscode.TextEdit[] }> = {
report: value => {
if (!request.live) {
throw new Error('Progress reporting is only supported for live sessions');
}
if (done || token.isCancellationRequested) {
return;
}
if (!value.message && !value.edits) {
return;
}
this._proxy.$handleProgressChunk(request.requestId, {
message: value.message,
edits: value.edits?.map(typeConvert.TextEdit.from)
});
}
};

const task = typeof entry.provider.provideInteractiveEditorResponse2 === 'function'
? entry.provider.provideInteractiveEditorResponse2(apiRequest, progress, token)
: entry.provider.provideInteractiveEditorResponse(apiRequest, token);

Promise.resolve(task).finally(() => done = true);

const res = await task;

if (res) {

Expand Down
76 changes: 57 additions & 19 deletions src/vs/workbench/contrib/inlineChat/browser/inlineChatController.ts
Expand Up @@ -13,7 +13,6 @@ import { DisposableStore, IDisposable, MutableDisposable, toDisposable } from 'v
import { StopWatch } from 'vs/base/common/stopwatch';
import { assertType } from 'vs/base/common/types';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditOperation } from 'vs/editor/common/core/editOperation';
import { IPosition, Position } from 'vs/editor/common/core/position';
import { IRange, Range } from 'vs/editor/common/core/range';
import { IEditorContribution, ScrollType } from 'vs/editor/common/editorCommon';
Expand All @@ -31,11 +30,14 @@ import { ILogService } from 'vs/platform/log/common/log';
import { EditResponse, EmptyResponse, ErrorResponse, ExpansionState, IInlineChatSessionService, MarkdownResponse, Session, SessionExchange, SessionPrompt } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSession';
import { EditModeStrategy, LivePreviewStrategy, LiveStrategy, PreviewStrategy } from 'vs/workbench/contrib/inlineChat/browser/inlineChatStrategies';
import { InlineChatZoneWidget } from 'vs/workbench/contrib/inlineChat/browser/inlineChatWidget';
import { CTX_INLINE_CHAT_HAS_ACTIVE_REQUEST, CTX_INLINE_CHAT_LAST_FEEDBACK, IInlineChatRequest, IInlineChatResponse, INLINE_CHAT_ID, EditMode, InlineChatResponseFeedbackKind, CTX_INLINE_CHAT_LAST_RESPONSE_TYPE, InlineChatResponseType, CTX_INLINE_CHAT_DID_EDIT, CTX_INLINE_CHAT_HAS_STASHED_SESSION, InlineChateResponseTypes, CTX_INLINE_CHAT_RESPONSE_TYPES, CTX_INLINE_CHAT_USER_DID_EDIT } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
import { CTX_INLINE_CHAT_HAS_ACTIVE_REQUEST, CTX_INLINE_CHAT_LAST_FEEDBACK, IInlineChatRequest, IInlineChatResponse, INLINE_CHAT_ID, EditMode, InlineChatResponseFeedbackKind, CTX_INLINE_CHAT_LAST_RESPONSE_TYPE, InlineChatResponseType, CTX_INLINE_CHAT_DID_EDIT, CTX_INLINE_CHAT_HAS_STASHED_SESSION, InlineChateResponseTypes, CTX_INLINE_CHAT_RESPONSE_TYPES, CTX_INLINE_CHAT_USER_DID_EDIT, IInlineChatProgressItem } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
import { IChatAccessibilityService, IChatWidgetService } from 'vs/workbench/contrib/chat/browser/chat';
import { IChatService } from 'vs/workbench/contrib/chat/common/chatService';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { Lazy } from 'vs/base/common/lazy';
import { Progress } from 'vs/platform/progress/common/progress';
import { generateUuid } from 'vs/base/common/uuid';
import { TextEdit } from 'vs/editor/common/languages';

export const enum State {
CREATE_SESSION = 'CREATE_SESSION',
Expand Down Expand Up @@ -465,13 +467,31 @@ export class InlineChatController implements IEditorContribution {

const sw = StopWatch.create();
const request: IInlineChatRequest = {
requestId: generateUuid(),
prompt: this._activeSession.lastInput.value,
attempt: this._activeSession.lastInput.attempt,
selection: this._editor.getSelection(),
wholeRange: this._activeSession.wholeRange.value,
live: this._activeSession.editMode !== EditMode.Preview // TODO@jrieken let extension know what document is used for previewing
};
this._chatAccessibilityService.acceptRequest();
const task = this._activeSession.provider.provideResponse(this._activeSession.session, request, requestCts.token);

const progressEdits: TextEdit[][] = [];
const progress = new Progress<IInlineChatProgressItem>(async data => {
this._log('received chunk', data, request);
if (!request.live) {
throw new Error('Progress in NOT supported in non-live mode');
}
if (data.message) {
this._zone.value.widget.updateToolbar(false);
this._zone.value.widget.updateInfo(data.message);
}
if (data.edits) {
progressEdits.push(data.edits);
await this._makeChanges(progressEdits);
}
}, { async: true });
const task = this._activeSession.provider.provideResponse(this._activeSession.session, request, progress, requestCts.token);
this._log('request started', this._activeSession.provider.debugName, this._activeSession.session, request);

let response: EditResponse | MarkdownResponse | ErrorResponse | EmptyResponse;
Expand All @@ -482,10 +502,14 @@ export class InlineChatController implements IEditorContribution {
this._ctxHasActiveRequest.set(true);
reply = await raceCancellationError(Promise.resolve(task), requestCts.token);

if (reply?.type === 'message') {
if (reply?.type === InlineChatResponseType.Message) {
response = new MarkdownResponse(this._activeSession.textModelN.uri, reply);
} else if (reply) {
response = new EditResponse(this._activeSession.textModelN.uri, this._activeSession.textModelN.getAlternativeVersionId(), reply);
const editResponse = new EditResponse(this._activeSession.textModelN.uri, this._activeSession.textModelN.getAlternativeVersionId(), reply, progressEdits);
if (editResponse.allLocalEdits.length > progressEdits.length) {
await this._makeChanges(editResponse.allLocalEdits);
}
response = editResponse;
} else {
response = new EmptyResponse();
}
Expand Down Expand Up @@ -531,27 +555,41 @@ export class InlineChatController implements IEditorContribution {
if (!canContinue) {
return State.ACCEPT;
}
const moreMinimalEdits = (await this._editorWorkerService.computeHumanReadableDiff(this._activeSession.textModelN.uri, response.localEdits));
const editOperations = (moreMinimalEdits ?? response.localEdits).map(edit => EditOperation.replace(Range.lift(edit.range), edit.text));
this._log('edits from PROVIDER and after making them MORE MINIMAL', this._activeSession.provider.debugName, response.localEdits, moreMinimalEdits);
}
return State.SHOW_RESPONSE;
}

private async _makeChanges(allEdits: TextEdit[][]) {
assertType(this._activeSession);
assertType(this._strategy);

if (allEdits.length === 0) {
return;
}

// diff-changes from model0 -> modelN+1
for (const edits of allEdits) {
const textModelNplus1 = this._modelService.createModel(createTextBufferFactoryFromSnapshot(this._activeSession.textModelN.createSnapshot()), null, undefined, true);
textModelNplus1.applyEdits(editOperations);
textModelNplus1.applyEdits(edits.map(TextEdit.asEditOperation));
const diff = await this._editorWorkerService.computeDiff(this._activeSession.textModel0.uri, textModelNplus1.uri, { ignoreTrimWhitespace: false, maxComputationTimeMs: 5000, computeMoves: false }, 'advanced');
this._activeSession.lastTextModelChanges = diff?.changes ?? [];
textModelNplus1.dispose();

try {
this._ignoreModelContentChanged = true;
this._activeSession.wholeRange.trackEdits(editOperations);
await this._strategy.makeChanges(editOperations);
this._ctxDidEdit.set(this._activeSession.hasChangedText);
} finally {
this._ignoreModelContentChanged = false;
}
}

return State.SHOW_RESPONSE;
// make changes from modelN -> modelN+1
const lastEdits = allEdits[allEdits.length - 1];
const moreMinimalEdits = await this._editorWorkerService.computeHumanReadableDiff(this._activeSession.textModelN.uri, lastEdits);
const editOperations = (moreMinimalEdits ?? lastEdits).map(TextEdit.asEditOperation);
this._log('edits from PROVIDER and after making them MORE MINIMAL', this._activeSession.provider.debugName, lastEdits, moreMinimalEdits);

try {
this._ignoreModelContentChanged = true;
this._activeSession.wholeRange.trackEdits(editOperations);
await this._strategy.makeChanges(editOperations);
this._ctxDidEdit.set(this._activeSession.hasChangedText);
} finally {
this._ignoreModelContentChanged = false;
}
}

private async [State.SHOW_RESPONSE](): Promise<State.WAIT_FOR_INPUT | State.ACCEPT> {
Expand Down
17 changes: 12 additions & 5 deletions src/vs/workbench/contrib/inlineChat/browser/inlineChatSession.ts
Expand Up @@ -296,19 +296,23 @@ export class MarkdownResponse {

export class EditResponse {

readonly localEdits: TextEdit[] = [];
readonly allLocalEdits: TextEdit[][] = [];
readonly singleCreateFileEdit: { uri: URI; edits: Promise<TextEdit>[] } | undefined;
readonly workspaceEdits: ResourceEdit[] | undefined;
readonly workspaceEditsIncludeLocalEdits: boolean = false;

constructor(
localUri: URI,
readonly modelAltVersionId: number,
readonly raw: IInlineChatBulkEditResponse | IInlineChatEditResponse
readonly raw: IInlineChatBulkEditResponse | IInlineChatEditResponse,
progressEdits: TextEdit[][],
) {

this.allLocalEdits.push(...progressEdits);

if (raw.type === 'editorEdit') {
//
this.localEdits = raw.edits;
this.allLocalEdits.push(raw.edits);
this.singleCreateFileEdit = undefined;
this.workspaceEdits = undefined;

Expand All @@ -318,6 +322,7 @@ export class EditResponse {
this.workspaceEdits = edits;

let isComplexEdit = false;
const localEdits: TextEdit[] = [];

for (const edit of edits) {
if (edit instanceof ResourceFileEdit) {
Expand All @@ -336,7 +341,7 @@ export class EditResponse {
} else if (edit instanceof ResourceTextEdit) {
//
if (isEqual(edit.resource, localUri)) {
this.localEdits.push(edit.textEdit);
localEdits.push(edit.textEdit);
this.workspaceEditsIncludeLocalEdits = true;

} else if (isEqual(this.singleCreateFileEdit?.uri, edit.resource)) {
Expand All @@ -346,7 +351,9 @@ export class EditResponse {
}
}
}

if (localEdits.length > 0) {
this.allLocalEdits.push(localEdits);
}
if (isComplexEdit) {
this.singleCreateFileEdit = undefined;
}
Expand Down