Skip to content

Commit 8734faf

Browse files
author
Eric Wheeler
committed
perf: optimize environment details by omitting identical records
Added prevEnvDetails property to Task class to store previous environment state. Modified getEnvironmentDetails to recursively compare current and previous environment details, returning only the differences. This optimization reduces the size of environment details in responses by omitting any records that are identical to the previous state. Fixes: #5844 Signed-off-by: Eric Wheeler <[email protected]>
1 parent 4266140 commit 8734faf

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

src/core/environment/context/metadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function getMetadataContext(cline: Task) {
3030
.padStart(2, "0")}:${offsetMinutes.toString().padStart(2, "0")}`
3131
const isoDateWithOffset = now.toISOString().replace(/Z$/, offsetString)
3232
const time = {
33-
"@v": isoDateWithOffset,
33+
"@I": isoDateWithOffset,
3434
}
3535

3636
const { totalCost } = getApiMetrics(cline.clineMessages)

src/core/environment/getEnvironmentDetails.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function getEnvironmentDetails(task: Task, includeFileDetails: bool
1919
getTodoContext(task),
2020
])
2121

22-
const envDetails = {
22+
const currentEnvDetails = {
2323
...vscodeContext,
2424
...terminalContext,
2525
...fileContext,
@@ -28,6 +28,10 @@ export async function getEnvironmentDetails(task: Task, includeFileDetails: bool
2828
...todoContext,
2929
}
3030

31+
const diffEnvDetails = _envDiff(currentEnvDetails, task.prevEnvDetails)
32+
33+
task.prevEnvDetails = currentEnvDetails
34+
3135
const builder = new XMLBuilder({
3236
format: true, // Enable pretty printing
3337
indentBy: " ", // Use two spaces for indentation
@@ -38,5 +42,54 @@ export async function getEnvironmentDetails(task: Task, includeFileDetails: bool
3842
textNodeName: "#text",
3943
})
4044

41-
return builder.build({ environment_details: envDetails })
45+
return builder.build({ environment_details: diffEnvDetails })
46+
}
47+
48+
function _envDiff(current: any, previous: any): any {
49+
if (!previous) return current
50+
51+
return Object.keys(current).reduce((acc, key) => {
52+
const currentValue = current[key]
53+
const previousValue = previous ? previous[key] : undefined
54+
55+
if (_objIsEqual(currentValue, previousValue)) {
56+
return acc
57+
}
58+
59+
// Check for nested objects (but not arrays)
60+
if (
61+
typeof currentValue === "object" &&
62+
currentValue !== null &&
63+
!Array.isArray(currentValue) &&
64+
typeof previousValue === "object" &&
65+
previousValue !== null &&
66+
!Array.isArray(previousValue)
67+
) {
68+
const nestedDiff = _envDiff(currentValue, previousValue)
69+
if (Object.keys(nestedDiff).length > 0) {
70+
acc[key] = nestedDiff
71+
}
72+
} else {
73+
// For primitives, arrays, or if previous was not an object
74+
acc[key] = currentValue
75+
}
76+
77+
return acc
78+
}, {} as any)
79+
}
80+
81+
function _objIsEqual(a: any, b: any): boolean {
82+
if (a === b) return true
83+
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") return false
84+
85+
const keysA = Object.keys(a)
86+
const keysB = Object.keys(b)
87+
88+
if (keysA.length !== keysB.length) return false
89+
90+
for (const key of keysA) {
91+
if (!keysB.includes(key) || !_objIsEqual(a[key], b[key])) return false
92+
}
93+
94+
return true
4295
}

src/core/task/Task.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export class Task extends EventEmitter<ClineEvents> {
180180
// LLM Messages & Chat Messages
181181
apiConversationHistory: ApiMessage[] = []
182182
clineMessages: ClineMessage[] = []
183+
prevEnvDetails?: Record<string, any>
183184

184185
// Ask
185186
private askResponse?: ClineAskResponse

0 commit comments

Comments
 (0)