Elffuss BraidHub

kikocis/continue public

3a1e5cc69df2 · 8.1 KiBRawPermalink
1import { COUNT_COMPLETION_REJECTED_AFTER } from "../util/parameters";
2
3import { DataLogger } from "../data/log";
4import { Telemetry } from "../util/posthog";
5import { NextEditOutcome } from "./types";
6
7export class NextEditLoggingService {
8 private static instance: NextEditLoggingService;
9
10 // Key is completionId
11 private _abortControllers = new Map<string, AbortController>();
12 private _logRejectionTimeouts = new Map<string, NodeJS.Timeout>();
13 private _outcomes = new Map<string, NextEditOutcome>();
14 // Track minimal data for completions that get aborted before we have full outcome.
15 private _pendingCompletions = new Map<
16 string,
17 {
18 startTime: number;
19 modelName?: string;
20 modelProvider?: string;
21 filepath?: string;
22 }
23 >();
24 _lastDisplayedCompletion: { id: string; displayedAt: number } | undefined =
25 undefined;
26
27 private constructor() {}
28
29 public static getInstance(): NextEditLoggingService {
30 if (!NextEditLoggingService.instance) {
31 NextEditLoggingService.instance = new NextEditLoggingService();
32 }
33 return NextEditLoggingService.instance;
34 }
35
36 public createAbortController(completionId: string): AbortController {
37 const abortController = new AbortController();
38 this._abortControllers.set(completionId, abortController);
39 this.trackPendingCompletion(completionId);
40 return abortController;
41 }
42
43 public deleteAbortController(completionId: string) {
44 this._abortControllers.delete(completionId);
45 this._pendingCompletions.delete(completionId);
46 }
47
48 // Keep track of a new completion request.
49 public trackPendingCompletion(completionId: string) {
50 this._pendingCompletions.set(completionId, {
51 startTime: Date.now(),
52 });
53 }
54
55 // Update pending completion info as it becomes available.
56 public updatePendingCompletion(
57 completionId: string,
58 data: {
59 modelName?: string;
60 modelProvider?: string;
61 filepath?: string;
62 },
63 ) {
64 const pending = this._pendingCompletions.get(completionId);
65 if (pending) {
66 this._pendingCompletions.set(completionId, { ...pending, ...data });
67 } else {
68 // If we haven't tracked it yet, create new entry with provided data.
69 this._pendingCompletions.set(completionId, {
70 startTime: Date.now(),
71 ...data,
72 });
73 }
74 }
75
76 public cancel() {
77 this._abortControllers.forEach((abortController, completionId) => {
78 this.handleAbort(completionId);
79 abortController.abort();
80 });
81 this._abortControllers.clear();
82 }
83
84 public accept(completionId: string): NextEditOutcome | undefined {
85 this._pendingCompletions.delete(completionId);
86
87 if (this._logRejectionTimeouts.has(completionId)) {
88 clearTimeout(this._logRejectionTimeouts.get(completionId));
89 this._logRejectionTimeouts.delete(completionId);
90 }
91 if (this._outcomes.has(completionId)) {
92 const outcome = this._outcomes.get(completionId)!;
93 outcome.accepted = true;
94 outcome.aborted = false;
95 this.logNextEditOutcome(outcome);
96 this._outcomes.delete(completionId);
97 return outcome;
98 }
99 }
100
101 public reject(completionId: string): NextEditOutcome | undefined {
102 this._pendingCompletions.delete(completionId);
103
104 if (this._logRejectionTimeouts.has(completionId)) {
105 clearTimeout(this._logRejectionTimeouts.get(completionId));
106 this._logRejectionTimeouts.delete(completionId);
107 }
108
109 if (this._outcomes.has(completionId)) {
110 const outcome = this._outcomes.get(completionId)!;
111 outcome.accepted = false;
112 outcome.aborted = false;
113 this.logNextEditOutcome(outcome);
114 this._outcomes.delete(completionId);
115 return outcome;
116 }
117 }
118
119 public cancelRejectionTimeout(completionId: string) {
120 if (this._logRejectionTimeouts.has(completionId)) {
121 clearTimeout(this._logRejectionTimeouts.get(completionId)!);
122 this._logRejectionTimeouts.delete(completionId);
123 }
124 if (this._outcomes.has(completionId)) {
125 this._outcomes.delete(completionId);
126 }
127 }
128
129 public cancelRejectionTimeoutButKeepCompletionId(completionId: string) {
130 if (this._logRejectionTimeouts.has(completionId)) {
131 clearTimeout(this._logRejectionTimeouts.get(completionId)!);
132 }
133 }
134
135 public markDisplayed(completionId: string, outcome: NextEditOutcome) {
136 // Remove from pending since we now have full data.
137 this._pendingCompletions.delete(completionId);
138 outcome.aborted = false;
139
140 const logRejectionTimeout = setTimeout(() => {
141 // Wait 10 seconds, then assume it wasn't accepted
142 outcome.accepted = false;
143 outcome.aborted = false;
144 this.logNextEditOutcome(outcome);
145 this._logRejectionTimeouts.delete(completionId);
146 this._outcomes.delete(completionId);
147 }, COUNT_COMPLETION_REJECTED_AFTER);
148
149 this._outcomes.set(completionId, outcome);
150 this._logRejectionTimeouts.set(completionId, logRejectionTimeout);
151
152 // If the previously displayed completion is still waiting for rejection,
153 // and this one is a continuation of that (the outcome.completion is the same modulo prefix)
154 // then we should cancel the rejection timeout
155 const previous = this._lastDisplayedCompletion;
156 const now = Date.now();
157 if (previous && this._logRejectionTimeouts.has(previous.id)) {
158 const previousOutcome = this._outcomes.get(previous.id);
159 const c1 = previousOutcome?.completion.split("\n")[0] ?? "";
160 const c2 = outcome.completion.split("\n")[0];
161 if (
162 previousOutcome &&
163 (c1.endsWith(c2) ||
164 c2.endsWith(c1) ||
165 c1.startsWith(c2) ||
166 c2.startsWith(c1))
167 ) {
168 this.cancelRejectionTimeout(previous.id);
169 } else if (now - previous.displayedAt < 500) {
170 // If a completion isn't shown for more than
171 this.cancelRejectionTimeout(previous.id);
172 }
173 }
174
175 this._lastDisplayedCompletion = {
176 id: completionId,
177 displayedAt: now,
178 };
179 }
180
181 public handleAbort(completionId: string) {
182 // Clear any pending rejection timeout.
183 if (this._logRejectionTimeouts.has(completionId)) {
184 clearTimeout(this._logRejectionTimeouts.get(completionId));
185 this._logRejectionTimeouts.delete(completionId);
186 }
187
188 // If we have the full outcome, log it as aborted.
189 if (this._outcomes.has(completionId)) {
190 const outcome = this._outcomes.get(completionId)!;
191 outcome.accepted = false;
192 outcome.aborted = true;
193 this.logNextEditOutcome(outcome);
194 this._outcomes.delete(completionId);
195 } else {
196 // Log minimal abort event for requests that never got displayed.
197 const pendingData = this._pendingCompletions.get(completionId);
198
199 const minimalAbortOutcome: Partial<NextEditOutcome> = {
200 completionId,
201 accepted: false,
202 aborted: true,
203 timestamp: Date.now(),
204 uniqueId: completionId,
205 elapsed: pendingData ? Date.now() - pendingData.startTime : 0,
206 modelName: pendingData?.modelName || "unknown",
207 modelProvider: pendingData?.modelProvider || "unknown",
208 fileUri: pendingData?.filepath || "unknown",
209
210 // Empty/default values for fields we don't have.
211 completion: "",
212 prompt: "",
213 userEdits: "",
214 userExcerpts: "",
215 originalEditableRange: "",
216 workspaceDirUri: "",
217 cursorPosition: { line: -1, character: -1 },
218 finalCursorPosition: { line: -1, character: -1 },
219 editableRegionStartLine: -1,
220 editableRegionEndLine: -1,
221 diffLines: [],
222 completionOptions: {},
223 };
224
225 this.logNextEditOutcome(minimalAbortOutcome as NextEditOutcome);
226 }
227
228 // Clean up.
229 this._pendingCompletions.delete(completionId);
230 }
231
232 private logNextEditOutcome(outcome: NextEditOutcome) {
233 if (outcome.aborted === undefined) {
234 outcome.aborted = false;
235 }
236
237 void DataLogger.getInstance().logDevData({
238 name: "nextEditOutcome",
239 data: outcome,
240 // data: {
241 // ...outcome, // TODO: this is somehow getting messed up with autocomplete schema.
242 // },
243 });
244
245 // const { prompt, completion, prefix, suffix, ...restOfOutcome } = outcome;
246 void Telemetry.capture("nextEditOutcome", outcome, true);
247 }
248}