Skip to content

Commit 90c4da9

Browse files
authored
Agents - Changes view pickers should work with multiple chats in a session (#309479)
* Agents - Changes view pickers should work with multiple chats in a session * Pull request feedback
1 parent 689454c commit 90c4da9

1 file changed

Lines changed: 41 additions & 3 deletions

File tree

src/vs/sessions/contrib/changes/browser/changesViewModel.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ function toIChatSessionFileChange2(changes: GitDiffChange[], originalRef: string
4040
} satisfies IChatSessionFileChange2));
4141
}
4242

43+
function sortDateDesc(dateA: Date | undefined, dateB: Date | undefined): number {
44+
const chatALastTurnEnd = dateA?.getTime();
45+
const chatBLastTurnEnd = dateB?.getTime();
46+
47+
if (!chatALastTurnEnd && !chatBLastTurnEnd) {
48+
return 0;
49+
}
50+
51+
if (!chatALastTurnEnd) {
52+
return 1;
53+
}
54+
55+
if (!chatBLastTurnEnd) {
56+
return -1;
57+
}
58+
59+
return chatBLastTurnEnd - chatALastTurnEnd;
60+
}
61+
4362
export interface ActiveSessionState {
4463
readonly isolationMode: IsolationMode;
4564
readonly hasGitRepository: boolean;
@@ -129,8 +148,27 @@ export class ChangesViewModel extends Disposable {
129148

130149
// Active session last checkpoint ref
131150
this.activeSessionLastCheckpointRefObs = derived(reader => {
132-
const metadata = this._activeSessionMetadataObs.read(reader);
133-
return metadata?.lastCheckpointRef as string | undefined;
151+
const activeSessionChats = this.sessionManagementService.activeSession.read(reader)?.chats.read(reader);
152+
if (!activeSessionChats || activeSessionChats.length === 0) {
153+
return undefined;
154+
}
155+
156+
// Session has only one chat
157+
if (activeSessionChats.length === 1) {
158+
const metadata = this._activeSessionMetadataObs.read(reader);
159+
return metadata?.lastCheckpointRef as string | undefined;
160+
}
161+
162+
// Session has multiple chats - find the last chat that completed
163+
const chatsSortedByLastTurnEnd = activeSessionChats.toSorted((chatA, chatB) => {
164+
const chatALastTurnEnd = chatA.lastTurnEnd.read(reader);
165+
const chatBLastTurnEnd = chatB.lastTurnEnd.read(reader);
166+
167+
return sortDateDesc(chatALastTurnEnd, chatBLastTurnEnd);
168+
});
169+
170+
const model = this.agentSessionsService.getSession(chatsSortedByLastTurnEnd[0].resource);
171+
return model?.metadata?.lastCheckpointRef as string | undefined;
134172
});
135173

136174
// Active session state
@@ -429,7 +467,7 @@ export class ChangesViewModel extends Disposable {
429467

430468
private async _getRepositoryChanges(repositoryPath: string, firstCheckpointRef: string, lastCheckpointRef: string): Promise<IChatSessionFileChange2[] | undefined> {
431469
const repository = await this.gitService.openRepository(URI.file(repositoryPath));
432-
const changes = await repository?.diffBetweenWithStats(firstCheckpointRef, lastCheckpointRef) ?? [];
470+
const changes = await repository?.diffBetweenWithStats2(`${firstCheckpointRef}..${lastCheckpointRef}`) ?? [];
433471
return toIChatSessionFileChange2(changes, firstCheckpointRef, lastCheckpointRef);
434472
}
435473

0 commit comments

Comments
 (0)