@@ -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 */
6972export 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