Elffuss BraidHub

kikocis/continue public

6c9c9d0b91ee · 4.6 KiBRawPermalink
1import { DataLogger } from "../../data/log";
2import { COUNT_COMPLETION_REJECTED_AFTER } from "../../util/parameters";
3import { Telemetry } from "../../util/posthog";
4import { getUriFileExtension } from "../../util/uri";
5
6import { AutocompleteOutcome } from "./types";
7
8export class AutocompleteLoggingService {
9 // Key is completionId
10 private _abortControllers = new Map<string, AbortController>();
11 private _logRejectionTimeouts = new Map<string, NodeJS.Timeout>();
12 private _outcomes = new Map<string, AutocompleteOutcome>();
13 _lastDisplayedCompletion: { id: string; displayedAt: number } | undefined =
14 undefined;
15
16 public createAbortController(completionId: string): AbortController {
17 const abortController = new AbortController();
18 this._abortControllers.set(completionId, abortController);
19 return abortController;
20 }
21
22 public deleteAbortController(completionId: string) {
23 this._abortControllers.delete(completionId);
24 }
25
26 public cancel() {
27 this._abortControllers.forEach((abortController, id) => {
28 abortController.abort();
29 });
30 this._abortControllers.clear();
31 }
32
33 public accept(completionId: string): AutocompleteOutcome | undefined {
34 if (this._logRejectionTimeouts.has(completionId)) {
35 clearTimeout(this._logRejectionTimeouts.get(completionId));
36 this._logRejectionTimeouts.delete(completionId);
37 }
38
39 if (this._outcomes.has(completionId)) {
40 const outcome = this._outcomes.get(completionId)!;
41 outcome.accepted = true;
42 this.logAutocompleteOutcome(outcome);
43 this._outcomes.delete(completionId);
44 return outcome;
45 }
46 }
47
48 public cancelRejectionTimeout(completionId: string) {
49 if (this._logRejectionTimeouts.has(completionId)) {
50 clearTimeout(this._logRejectionTimeouts.get(completionId)!);
51 this._logRejectionTimeouts.delete(completionId);
52 }
53
54 if (this._outcomes.has(completionId)) {
55 this._outcomes.delete(completionId);
56 }
57 }
58
59 public markDisplayed(completionId: string, outcome: AutocompleteOutcome) {
60 const logRejectionTimeout = setTimeout(() => {
61 // Wait 10 seconds, then assume it wasn't accepted
62 outcome.accepted = false;
63 this.logAutocompleteOutcome(outcome);
64 this._logRejectionTimeouts.delete(completionId);
65 }, COUNT_COMPLETION_REJECTED_AFTER);
66 this._outcomes.set(completionId, outcome);
67 this._logRejectionTimeouts.set(completionId, logRejectionTimeout);
68
69 // If the previously displayed completion is still waiting for rejection,
70 // and this one is a continuation of that (the outcome.completion is the same modulo prefix)
71 // then we should cancel the rejection timeout
72 const previous = this._lastDisplayedCompletion;
73 const now = Date.now();
74 if (previous && this._logRejectionTimeouts.has(previous.id)) {
75 const previousOutcome = this._outcomes.get(previous.id);
76 const c1 = previousOutcome?.completion.split("\n")[0] ?? "";
77 const c2 = outcome.completion.split("\n")[0];
78 if (
79 previousOutcome &&
80 (c1.endsWith(c2) ||
81 c2.endsWith(c1) ||
82 c1.startsWith(c2) ||
83 c2.startsWith(c1))
84 ) {
85 this.cancelRejectionTimeout(previous.id);
86 } else if (now - previous.displayedAt < 500) {
87 // If a completion isn't shown for more than
88 this.cancelRejectionTimeout(previous.id);
89 }
90 }
91
92 this._lastDisplayedCompletion = {
93 id: completionId,
94 displayedAt: now,
95 };
96 }
97
98 private logAutocompleteOutcome(outcome: AutocompleteOutcome) {
99 void DataLogger.getInstance().logDevData({
100 name: "autocomplete",
101 data: {
102 ...outcome,
103 useFileSuffix: true, // from outdated schema
104 },
105 });
106
107 const { prompt, completion, prefix, suffix, ...restOfOutcome } = outcome;
108 const toLog = {
109 accepted: restOfOutcome.accepted,
110 cacheHit: restOfOutcome.cacheHit,
111 completionId: restOfOutcome.completionId,
112 completionOptions: restOfOutcome.completionOptions,
113 debounceDelay: restOfOutcome.debounceDelay,
114 fileExtension: getUriFileExtension(restOfOutcome.filepath),
115 maxPromptTokens: restOfOutcome.maxPromptTokens,
116 modelName: restOfOutcome.modelName,
117 modelProvider: restOfOutcome.modelProvider,
118 multilineCompletions: restOfOutcome.multilineCompletions,
119 time: restOfOutcome.time,
120 useRecentlyEdited: restOfOutcome.useRecentlyEdited,
121 numLines: restOfOutcome.numLines,
122 };
123
124 outcome.enabledStaticContextualization
125 ? void Telemetry.capture("autocomplete", {
126 ...toLog,
127 enabledStaticContextualization: true,
128 })
129 : void Telemetry.capture("autocomplete", toLog);
130 }
131}