Skip to content

Commit 440c44e

Browse files
committed
update supplemental context logic according to science feedback
1 parent 475c5dd commit 440c44e

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

packages/core/src/codewhisperer/nextEditPrediction/PredictionTracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as vscode from 'vscode'
77
import * as path from 'path'
88
import fs from '../../shared/fs/fs'
99
import { getLogger } from '../../shared/logger/logger'
10-
import * as diffGenerator from './diffGenerator'
10+
import * as diffGenerator from './diffContextGenerator'
1111
import * as codewhispererClient from '../client/codewhisperer'
1212

1313
export interface FileTrackerConfig {

packages/core/src/codewhisperer/nextEditPrediction/diffGenerator.ts renamed to packages/core/src/codewhisperer/nextEditPrediction/diffContextGenerator.ts

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as codewhispererClient from '../client/codewhisperer'
1919
* @param contextSize - Number of context lines to include (default: 3)
2020
* @returns Unified diff as a string
2121
*/
22-
export async function generateUnifiedDiffWithTimestamps(
22+
async function generateUnifiedDiffWithTimestamps(
2323
oldFilePath: string,
2424
newFilePath: string,
2525
oldContent: string,
@@ -33,8 +33,8 @@ export async function generateUnifiedDiffWithTimestamps(
3333
newFilePath,
3434
oldContent,
3535
newContent,
36-
`${oldTimestamp}`, // Old file label with timestamp
37-
`${newTimestamp}`, // New file label with timestamp
36+
String(oldTimestamp),
37+
String(newTimestamp),
3838
{ context: contextSize }
3939
)
4040

@@ -64,7 +64,10 @@ export interface SnapshotContent {
6464
* @param currentContent - Current content of the file
6565
* @param snapshotContents - List of snapshot contents sorted by timestamp (oldest first)
6666
* @param maxContexts - Maximum number of supplemental contexts to return
67-
* @returns Array of SupplementalContext objects
67+
* @returns Array of SupplementalContext objects, T_0 being the snapshot of current file content:
68+
* U0: udiff of T_0 and T_1
69+
* U1: udiff of T_0 and T_2
70+
* U2: udiff of T_0 and T_3
6871
*/
6972
export async function generateDiffContexts(
7073
filePath: string,
@@ -79,38 +82,28 @@ export async function generateDiffContexts(
7982
const supplementalContexts: codewhispererClient.SupplementalContext[] = []
8083
const currentTimestamp = Date.now()
8184

82-
// Treat current content as the last snapshot
83-
const allContents = [
84-
...snapshotContents,
85-
{
86-
filePath,
87-
content: currentContent,
88-
timestamp: currentTimestamp,
89-
},
90-
]
91-
92-
// Generate diffs between all adjacent snapshots (including current content)
93-
for (let i = 0; i < allContents.length - 1; i++) {
94-
const oldSnapshot = allContents[i]
95-
const newSnapshot = allContents[i + 1]
85+
// Create a copy of snapshots and reverse it so newest snapshots are processed first
86+
const sortedSnapshots = [...snapshotContents].reverse()
9687

88+
// Generate diffs between each snapshot and the current content
89+
for (const snapshot of sortedSnapshots) {
9790
try {
98-
const diff = await generateUnifiedDiffWithTimestamps(
99-
oldSnapshot.filePath,
100-
newSnapshot.filePath,
101-
oldSnapshot.content,
102-
newSnapshot.content,
103-
oldSnapshot.timestamp,
104-
newSnapshot.timestamp
91+
const unifiedDiff = await generateUnifiedDiffWithTimestamps(
92+
snapshot.filePath,
93+
filePath,
94+
snapshot.content,
95+
currentContent,
96+
snapshot.timestamp,
97+
currentTimestamp
10598
)
10699

107100
supplementalContexts.push({
108-
filePath: oldSnapshot.filePath,
109-
content: diff,
101+
filePath: snapshot.filePath,
102+
content: unifiedDiff,
110103
type: 'PreviousEditorState',
111104
metadata: {
112105
previousEditorStateMetadata: {
113-
timeOffset: currentTimestamp - oldSnapshot.timestamp,
106+
timeOffset: currentTimestamp - snapshot.timestamp,
114107
},
115108
},
116109
})
@@ -121,7 +114,7 @@ export async function generateDiffContexts(
121114

122115
// Limit the number of supplemental contexts based on config
123116
if (supplementalContexts.length > maxContexts) {
124-
return supplementalContexts.slice(-maxContexts)
117+
return supplementalContexts.slice(0, maxContexts)
125118
}
126119

127120
return supplementalContexts

0 commit comments

Comments
 (0)