From 42cdb7f84510f95f6488555fdfa158e7d9f02308 Mon Sep 17 00:00:00 2001 From: Ben Jones Date: Wed, 5 Mar 2025 16:37:55 +0000 Subject: [PATCH 001/378] Enhancements to autocomplete to allow for modification of debounce delay and model timeout in configuration GUI, fix to debounce delay handling --- .../StreamTransformPipeline.ts | 6 +-- .../util/AutocompleteDebouncer.ts | 48 +++++++++---------- core/config/sharedConfig.ts | 10 ++++ core/index.d.ts | 1 + core/util/parameters.ts | 1 + gui/src/components/gui/NumberInput.tsx | 36 ++++++++++++-- gui/src/pages/config/index.tsx | 31 +++++++++++- 7 files changed, 98 insertions(+), 35 deletions(-) diff --git a/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts b/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts index 1bd90c503..695c9afeb 100644 --- a/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts +++ b/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts @@ -1,5 +1,4 @@ import { streamLines } from "../../../diff/util"; -import { PosthogFeatureFlag, Telemetry } from "../../../util/posthog"; import { HelperVars } from "../../util/HelperVars"; import { stopAtStartOf, stopAtStopTokens } from "./charStream"; @@ -73,10 +72,7 @@ export class StreamTransformPipeline { fullStop, ); - const timeoutValue = await Telemetry.getValueForFeatureFlag( - PosthogFeatureFlag.AutocompleteTimeout, - ); - + const timeoutValue = helper.options.modelTimeout; lineGenerator = showWhateverWeHaveAtXMs(lineGenerator, timeoutValue!); const finalGenerator = streamWithNewLines(lineGenerator); diff --git a/core/autocomplete/util/AutocompleteDebouncer.ts b/core/autocomplete/util/AutocompleteDebouncer.ts index f8bcc474f..2616acaef 100644 --- a/core/autocomplete/util/AutocompleteDebouncer.ts +++ b/core/autocomplete/util/AutocompleteDebouncer.ts @@ -1,32 +1,32 @@ import { v4 as uuidv4 } from "uuid"; + export class AutocompleteDebouncer { private debounceTimeout: NodeJS.Timeout | undefined = undefined; - private debouncing = false; - private lastUUID: string | undefined = undefined; + private currentRequestId: string | undefined = undefined; async delayAndShouldDebounce(debounceDelay: number): Promise { - // Debounce - const uuid = uuidv4(); - this.lastUUID = uuid; - - // Debounce - if (this.debouncing) { - this.debounceTimeout?.refresh(); - const lastUUID = await new Promise((resolve) => - setTimeout(() => { - resolve(this.lastUUID); - }, debounceDelay), - ); - if (uuid !== lastUUID) { - return true; - } - } else { - this.debouncing = true; - this.debounceTimeout = setTimeout(async () => { - this.debouncing = false; - }, debounceDelay); + // Generate a unique ID for this request + const requestId = uuidv4(); + this.currentRequestId = requestId; + + // Clear any existing timeout + if (this.debounceTimeout) { + clearTimeout(this.debounceTimeout); } - - return false; + + // Create a new promise that resolves after the debounce delay + return new Promise((resolve) => { + this.debounceTimeout = setTimeout(() => { + // When the timeout completes, check if this is still the most recent request + const shouldDebounce = this.currentRequestId !== requestId; + + // If this is the most recent request, it shouldn't be debounced + if (!shouldDebounce) { + this.currentRequestId = undefined; + } + + resolve(shouldDebounce); + }, debounceDelay); + }); } } diff --git a/core/config/sharedConfig.ts b/core/config/sharedConfig.ts index ce16ea24f..b8ac20584 100644 --- a/core/config/sharedConfig.ts +++ b/core/config/sharedConfig.ts @@ -31,6 +31,8 @@ export const sharedConfigSchema = z useAutocompleteCache: z.boolean(), useAutocompleteMultilineCompletions: z.enum(["always", "never", "auto"]), disableAutocompleteInFiles: z.array(z.string()), + modelTimeout: z.number(), + debounceDelay: z.number(), }) .partial(); @@ -103,6 +105,14 @@ export function modifyAnyConfigWithSharedConfig< configCopy.tabAutocompleteOptions.disableInFiles = sharedConfig.disableAutocompleteInFiles; } + if (sharedConfig.modelTimeout !== undefined) { + configCopy.tabAutocompleteOptions.modelTimeout = + sharedConfig.modelTimeout; + } + if (sharedConfig.debounceDelay !== undefined) { + configCopy.tabAutocompleteOptions.debounceDelay = + sharedConfig.debounceDelay; + } configCopy.ui = { ...configCopy.ui, diff --git a/core/index.d.ts b/core/index.d.ts index 7cbf0e25b..c2a11dad8 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -983,6 +983,7 @@ export interface TabAutocompleteOptions { disable: boolean; maxPromptTokens: number; debounceDelay: number; + modelTimeout: number; maxSuffixPercentage: number; prefixPercentage: number; transform?: boolean; diff --git a/core/util/parameters.ts b/core/util/parameters.ts index 05ab61e06..aa50de122 100644 --- a/core/util/parameters.ts +++ b/core/util/parameters.ts @@ -6,6 +6,7 @@ export const DEFAULT_AUTOCOMPLETE_OPTS: TabAutocompleteOptions = { prefixPercentage: 0.3, maxSuffixPercentage: 0.2, debounceDelay: 350, + modelTimeout: 150, multilineCompletions: "auto", // @deprecated TO BE REMOVED slidingWindowPrefixPercentage: 0.75, diff --git a/gui/src/components/gui/NumberInput.tsx b/gui/src/components/gui/NumberInput.tsx index 5ea817b84..c2f8c3311 100644 --- a/gui/src/components/gui/NumberInput.tsx +++ b/gui/src/components/gui/NumberInput.tsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useState } from "react"; interface NumberInputProps { value: number; @@ -13,24 +13,50 @@ const NumberInput: React.FC = ({ max, min, }) => { + const [inputValue, setInputValue] = useState(value.toString()); + const handleIncrement = () => { if (value < max) { - onChange(value + 1); + const newValue = value + 1; + onChange(newValue); + setInputValue(newValue.toString()); } }; const handleDecrement = () => { if (value > min) { - onChange(value - 1); + const newValue = value - 1; + onChange(newValue); + setInputValue(newValue.toString()); } }; + const handleInputChange = (e: React.ChangeEvent) => { + const newInputValue = e.target.value; + setInputValue(newInputValue); + + // Only update the actual value if it's a valid number + const numValue = parseInt(newInputValue, 10); + if (!isNaN(numValue)) { + // Apply min/max constraints + const constrainedValue = Math.min(Math.max(numValue, min), max); + onChange(constrainedValue); + } + }; + + const handleBlur = () => { + // When input loses focus, ensure the displayed value matches the actual value + // This handles cases where the user entered an invalid value + setInputValue(value.toString()); + }; + return (
Never - + +