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

Ensure proper sequencing of notebook cell edits #241350

Merged
merged 4 commits into from
Feb 20, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import { IEditorService } from '../../../../services/editor/common/editorService
import { IExtensionService } from '../../../../services/extensions/common/extensions.js';
import { ILifecycleService } from '../../../../services/lifecycle/common/lifecycle.js';
import { IMultiDiffSourceResolver, IMultiDiffSourceResolverService, IResolvedMultiDiffSource, MultiDiffEditorItem } from '../../../multiDiffEditor/browser/multiDiffSourceResolverService.js';
import { CellUri, ICellEditOperation } from '../../../notebook/common/notebookCommon.js';
import { CellUri } from '../../../notebook/common/notebookCommon.js';
import { ChatAgentLocation, IChatAgentService } from '../../common/chatAgents.js';
import { CHAT_EDITING_MULTI_DIFF_SOURCE_RESOLVER_SCHEME, chatEditingAgentSupportsReadonlyReferencesContextKey, chatEditingResourceContextKey, ChatEditingSessionState, chatEditingSnapshotScheme, IChatEditingService, IChatEditingSession, IChatRelatedFile, IChatRelatedFilesProvider, IModifiedFileEntry, inChatEditingSessionContextKey, IStreamingEdits, WorkingSetEntryState } from '../../common/chatEditingService.js';
import { IChatResponseModel } from '../../common/chatModel.js';
import { IChatResponseModel, isCellTextEditOperation } from '../../common/chatModel.js';
import { IChatService } from '../../common/chatService.js';
import { AbstractChatEditingModifiedFileEntry } from './chatEditingModifiedFileEntry.js';
import { ChatEditingSession } from './chatEditingSession.js';
Expand Down Expand Up @@ -314,7 +314,7 @@ export class ChatEditingService extends Disposable implements IChatEditingServic
entry = { seen: 0, streaming: codeBlockUrisSeen[codeBlockIndex]!.streaming! };
codeBlockUrisSeen[codeBlockIndex]!.streaming = undefined;
} else {
entry = { seen: 0, streaming: session.startStreamingEdits(part.uri, responseModel, undoStop) };
entry = { seen: 0, streaming: session.startStreamingEdits(CellUri.parse(part.uri)?.notebook ?? part.uri, responseModel, undoStop) };
}

editsSeen[i] = entry;
Expand All @@ -326,7 +326,16 @@ export class ChatEditingService extends Disposable implements IChatEditingServic

if (newEdits.length > 0 || isFirst) {
if (part.kind === 'notebookEditGroup') {
entry.streaming.pushNotebook(newEdits as ICellEditOperation[]);
newEdits.forEach(edit => {
if (TextEdit.isTextEdit(edit)) {
// Not possible, as Notebooks would have a different type.
return;
} else if (isCellTextEditOperation(edit)) {
entry.streaming.pushNotebookCellText(edit.uri, [edit.edit]);
} else {
entry.streaming.pushNotebook([edit]);
}
});
} else if (part.kind === 'textEditGroup') {
entry.streaming.pushText(newEdits as TextEdit[]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,13 +708,20 @@ export class ChatEditingSession extends Disposable implements IChatEditingSessio
let didComplete = false;

return {
pushText: edits => {
pushText: (edits) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@connor4312 @DonJayamanne Can we align these and just have pushEdits(uri, edits) because all these call _acceptEdits

sequencer.queue(async () => {
if (!this.isDisposed) {
await this._acceptEdits(resource, edits, false, responseModel);
}
});
},
pushNotebookCellText: (cell, edits) => {
sequencer.queue(async () => {
if (!this.isDisposed) {
await this._acceptEdits(cell, edits, false, responseModel);
}
});
},
pushNotebook: edits => {
sequencer.queue(async () => {
if (!this.isDisposed) {
Expand Down
20 changes: 14 additions & 6 deletions src/vs/workbench/contrib/chat/browser/chatQuick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { editorBackground, inputBackground, quickInputBackground, quickInputFore
import { IQuickChatOpenOptions, IQuickChatService, showChatView } from './chat.js';
import { ChatWidget } from './chatWidget.js';
import { ChatAgentLocation } from '../common/chatAgents.js';
import { ChatModel } from '../common/chatModel.js';
import { ChatModel, isCellTextEditOperation } from '../common/chatModel.js';
import { IParsedChatRequest } from '../common/chatParserTypes.js';
import { IChatProgress, IChatService } from '../common/chatService.js';
import { IViewsService } from '../../../services/views/common/viewsService.js';
Expand Down Expand Up @@ -300,11 +300,19 @@ class QuickChat extends Disposable {
}
} else if (item.kind === 'notebookEditGroup') {
for (const group of item.edits) {
message.push({
kind: 'notebookEdit',
edits: group,
uri: item.uri
});
if (isCellTextEditOperation(group)) {
message.push({
kind: 'textEdit',
edits: [group.edit],
uri: group.uri
});
} else {
message.push({
kind: 'notebookEdit',
edits: [group],
uri: item.uri
});
}
}
} else {
message.push(item);
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/contrib/chat/common/chatEditingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface WorkingSetDisplayMetadata {

export interface IStreamingEdits {
pushText(edits: TextEdit[]): void;
pushNotebookCellText(cell: URI, edits: TextEdit[]): void;
pushNotebook(edits: ICellEditOperation[]): void;
/** Marks edits as done, idempotent */
complete(): void;
Expand Down
27 changes: 20 additions & 7 deletions src/vs/workbench/contrib/chat/common/chatModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Location, SymbolKind, TextEdit } from '../../../../editor/common/langua
import { localize } from '../../../../nls.js';
import { ILogService } from '../../../../platform/log/common/log.js';
import { MarkerSeverity } from '../../../../platform/markers/common/markers.js';
import { ICellEditOperation } from '../../notebook/common/notebookCommon.js';
import { CellUri, ICellEditOperation } from '../../notebook/common/notebookCommon.js';
import { ChatAgentLocation, IChatAgentCommand, IChatAgentData, IChatAgentResult, IChatAgentService, IChatWelcomeMessageContent, reviveSerializedAgent } from './chatAgents.js';
import { ChatRequestTextPart, IParsedChatRequest, reviveParsedChatRequest } from './chatParserTypes.js';
import { ChatAgentVoteDirection, ChatAgentVoteDownReason, IChatAgentMarkdownContentWithVulnerability, IChatCodeCitation, IChatCommandButton, IChatConfirmation, IChatContentInlineReference, IChatContentReference, IChatFollowup, IChatLocationData, IChatMarkdownContent, IChatNotebookEdit, IChatProgress, IChatProgressMessage, IChatResponseCodeblockUriPart, IChatResponseProgressFileTreeData, IChatTask, IChatTextEdit, IChatToolInvocation, IChatToolInvocationSerialized, IChatTreeData, IChatUndoStop, IChatUsedContext, IChatWarningMessage, isIUsedContext } from './chatService.js';
Expand Down Expand Up @@ -180,9 +180,19 @@ export interface IChatTextEditGroup {
done: boolean | undefined;
}

export function isCellTextEditOperation(value: unknown): value is ICellTextEditOperation {
const candidate = value as ICellTextEditOperation;
return !!candidate && !!candidate.edit && !!candidate.uri && URI.isUri(candidate.uri);
}

export interface ICellTextEditOperation {
edit: TextEdit;
uri: URI;
}

export interface IChatNotebookEditGroup {
uri: URI;
edits: ICellEditOperation[][];
edits: (ICellTextEditOperation | ICellEditOperation)[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Text edits belonging to a notebook must belong to notebookGroup
This way the order is preserved and its clear these text edits belong to notebooks.

state?: IChatTextEditGroupState;
kind: 'notebookEditGroup';
done: boolean | undefined;
Expand Down Expand Up @@ -522,21 +532,24 @@ export class Response extends AbstractResponse implements IDisposable {
this._updateRepr(quiet);
} else if (progress.kind === 'textEdit' || progress.kind === 'notebookEdit') {
// merge edits for the same file no matter when they come in
const groupKind = progress.kind === 'textEdit' ? 'textEditGroup' : 'notebookEditGroup';
const notebookUri = CellUri.parse(progress.uri)?.notebook;
const uri = notebookUri ?? progress.uri;
let found = false;
const groupKind = progress.kind === 'textEdit' && !notebookUri ? 'textEditGroup' : 'notebookEditGroup';
const edits: any = groupKind === 'textEditGroup' ? progress.edits : progress.edits.map(edit => TextEdit.isTextEdit(edit) ? { uri: progress.uri, edit } : edit);
for (let i = 0; !found && i < this._responseParts.length; i++) {
const candidate = this._responseParts[i];
if (candidate.kind === groupKind && !candidate.done && isEqual(candidate.uri, progress.uri)) {
candidate.edits.push(progress.edits as any);
if (candidate.kind === groupKind && !candidate.done && isEqual(candidate.uri, uri)) {
candidate.edits.push(edits);
candidate.done = progress.done;
found = true;
}
}
if (!found) {
this._responseParts.push({
kind: groupKind,
uri: progress.uri,
edits: [progress.edits as any],
uri,
edits: groupKind === 'textEditGroup' ? [edits] : edits,
done: progress.done
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { CancelablePromise, createCancelablePromise, DeferredPromise } from '../
import { IStorageService, StorageScope, StorageTarget } from '../../../../../platform/storage/common/storage.js';
import { IViewsService } from '../../../../services/views/common/viewsService.js';
import { IChatAcceptInputOptions, showChatView } from '../../../chat/browser/chat.js';
import { ChatModel, IChatResponseModel } from '../../../chat/common/chatModel.js';
import { ChatModel, IChatResponseModel, isCellTextEditOperation } from '../../../chat/common/chatModel.js';
import { IChatService, IChatProgress } from '../../../chat/common/chatService.js';
import { CancellationTokenSource } from '../../../../../base/common/cancellation.js';
import { MenuId } from '../../../../../platform/actions/common/actions.js';
Expand Down Expand Up @@ -448,11 +448,19 @@ export class TerminalChatWidget extends Disposable {
}
} else if (item.kind === 'notebookEditGroup') {
for (const group of item.edits) {
message.push({
kind: 'notebookEdit',
edits: group,
uri: item.uri
});
if (isCellTextEditOperation(group)) {
message.push({
kind: 'textEdit',
edits: [group.edit],
uri: group.uri
});
} else {
message.push({
kind: 'notebookEdit',
edits: [group],
uri: item.uri
});
}
}
} else {
message.push(item);
Expand Down
Loading