Skip to content

Commit f7e3470

Browse files
committed
Console: limit number of interactions that we retain
Only retain and display the 50 most recent interactions; this prevents the stored items from growing without bound if the user is using the vscode instance to edit for a long time.
1 parent 6fcaf90 commit f7e3470

File tree

2 files changed

+93
-17
lines changed

2 files changed

+93
-17
lines changed

Diff for: extensions/vscode/src/ContinueConsoleWebviewViewProvider.ts

+65-16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ interface FromConsoleView {
1010
uuid: string;
1111
}
1212

13+
// Maximum interactions we retain; when we exceed this, we drop the
14+
// oldest and also send a message to the view to do the same.
15+
const MAX_INTERACTIONS = 50;
16+
1317
export class ContinueConsoleWebviewViewProvider
1418
implements vscode.WebviewViewProvider
1519
{
@@ -36,7 +40,7 @@ export class ContinueConsoleWebviewViewProvider
3640
this._webview?.postMessage({
3741
type: "init",
3842
uuid: this._currentUuid,
39-
items: this._items,
43+
items: this.getAllItems(),
4044
});
4145
}
4246
});
@@ -50,7 +54,8 @@ export class ContinueConsoleWebviewViewProvider
5054
private _webview?: vscode.Webview;
5155
private _webviewView?: vscode.WebviewView;
5256
private _currentUuid?: string;
53-
private _items: LLMInteractionItem[] = [];
57+
private _currentInteractions = new Map<string, LLMInteractionItem[]>();
58+
private _completedInteractions: LLMInteractionItem[][] = [];
5459
private _saveLog;
5560

5661
constructor(
@@ -71,24 +76,68 @@ export class ContinueConsoleWebviewViewProvider
7176
}
7277
});
7378

74-
llmLogger.onLogItem((item) => {
75-
if (!this._saveLog) {
76-
return;
77-
}
79+
llmLogger.onLogItem((item) => this.addItem(item));
80+
}
7881

79-
this._items.push(item);
80-
if (this._currentUuid) {
81-
this._webview?.postMessage({
82-
type: "item",
83-
uuid: this._currentUuid,
84-
item,
85-
});
86-
}
87-
});
82+
private addItem(item: LLMInteractionItem) {
83+
if (!this._saveLog) {
84+
return;
85+
}
86+
87+
let iteractionItems = this._currentInteractions.get(item.interactionId);
88+
if (iteractionItems === undefined) {
89+
iteractionItems = [];
90+
this._currentInteractions.set(item.interactionId, iteractionItems);
91+
}
92+
93+
iteractionItems.push(item);
94+
switch (item.kind) {
95+
case "success":
96+
case "cancel":
97+
case "error":
98+
this._completedInteractions.push(iteractionItems);
99+
this._currentInteractions.delete(item.interactionId);
100+
break;
101+
default:
102+
break;
103+
}
104+
105+
if (this._currentUuid) {
106+
this._webview?.postMessage({
107+
type: "item",
108+
uuid: this._currentUuid,
109+
item,
110+
});
111+
}
112+
113+
while (
114+
this._completedInteractions.length > 0 &&
115+
this._completedInteractions.length + this._currentInteractions.size >
116+
MAX_INTERACTIONS
117+
) {
118+
let toRemove = this._completedInteractions.shift();
119+
this._webview?.postMessage({
120+
type: "remove",
121+
uuid: this._currentUuid,
122+
interactionId: toRemove![0].interactionId,
123+
});
124+
}
125+
}
126+
127+
private getAllItems() {
128+
let items = this._completedInteractions.flat();
129+
130+
for (const interactionItems of this._currentInteractions.values()) {
131+
items.push(...interactionItems);
132+
}
133+
134+
return items;
88135
}
89136

90137
clearLog() {
91-
this._items = [];
138+
this._completedInteractions = [];
139+
this._currentInteractions = new Map();
140+
92141
if (this._currentUuid) {
93142
this._webview?.postMessage({
94143
type: "clear",

Diff for: gui/src/hooks/useLLMLog.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,22 @@ interface ToConsoleViewItem {
2929
item: LLMInteractionItem;
3030
}
3131

32+
interface ToConsoleViewRemove {
33+
type: "remove";
34+
uuid: string;
35+
interactionId: string;
36+
}
37+
3238
interface ToConsoleViewClear {
3339
type: "clear";
3440
uuid: string;
3541
}
3642

37-
type ToConsoleView = ToConsoleViewInit | ToConsoleViewItem | ToConsoleViewClear;
43+
type ToConsoleView =
44+
| ToConsoleViewInit
45+
| ToConsoleViewItem
46+
| ToConsoleViewRemove
47+
| ToConsoleViewClear;
3848

3949
/**
4050
* Represents a linear list of LLMInteractionItem, transformed into
@@ -168,6 +178,21 @@ function appendItemsToLLMLog(
168178
};
169179
}
170180

181+
function removeInteractionFromLLMLog(
182+
llmLog: LLMLog,
183+
interactionId: string,
184+
): LLMLog {
185+
const newInteractions = new Map(llmLog.interactions);
186+
newInteractions.delete(interactionId);
187+
const newOrder = llmLog.order.filter((id) => id !== interactionId);
188+
189+
return {
190+
loading: false,
191+
interactions: newInteractions,
192+
order: newOrder,
193+
};
194+
}
195+
171196
/**
172197
* Hook to accumulate log data structures based on messages passed
173198
* from the core. Note that each call site will create an independent
@@ -183,6 +208,8 @@ export default function useLLMLog() {
183208
return appendItemsToLLMLog(llmLog, message.items);
184209
case "item":
185210
return appendItemsToLLMLog(llmLog, [message.item]);
211+
case "remove":
212+
return removeInteractionFromLLMLog(llmLog, message.interactionId);
186213
case "clear":
187214
return {
188215
loading: false,

0 commit comments

Comments
 (0)