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