Merge pull request #4509 from alfasoftware/autocomplete-fixes

Enhancements and fixes to autocomplete behaviour
This commit is contained in:
Tomasz Stefaniak 2025-05-12 15:09:00 -07:00 • committed by GitHub
commit 063e769a75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 121 additions and 54 deletions

View file

@ -72,7 +72,8 @@ export class StreamTransformPipeline {
fullStop,
);
const timeoutValue = helper.options.showWhateverWeHaveAtXMs;
const timeoutValue = helper.options.modelTimeout;
lineGenerator = showWhateverWeHaveAtXMs(lineGenerator, timeoutValue!);

View file

@ -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<boolean> {
// 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<boolean>((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);
});
}
}

View file

@ -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,

1
core/index.d.ts vendored
View file

@ -1090,6 +1090,7 @@ export interface TabAutocompleteOptions {
disable: boolean;
maxPromptTokens: number;
debounceDelay: number;
modelTimeout: number;
maxSuffixPercentage: number;
prefixPercentage: number;
transform?: boolean;

View file

@ -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,

View file

@ -1,4 +1,4 @@
import React from "react";
import React, { useState } from "react";
interface NumberInputProps {
value: number;
@ -13,25 +13,51 @@ const NumberInput: React.FC<NumberInputProps> = ({
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<HTMLInputElement>) => {
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 (
<div className="border-vsc-input-border bg-vsc-input-background flex flex-row overflow-hidden rounded-md border border-solid">
<input
type="text"
value={value}
readOnly
className="text-vsc-foreground max-w-7 border-none bg-inherit pr-1.5 text-right outline-none ring-0 focus:border-none focus:outline-none focus:ring-0"
value={inputValue}
onChange={handleInputChange}
onBlur={handleBlur}
className="text-vsc-foreground max-w-9 border-none bg-inherit pr-1.5 text-right outline-none ring-0 focus:border-none focus:outline-none focus:ring-0"
style={{
appearance: "none",
WebkitAppearance: "none",

View file

@ -77,6 +77,10 @@ export function UserSettingsForm() {
// const codeBlockToolbarPosition = config.ui?.codeBlockToolbarPosition ?? "top";
const useAutocompleteMultilineCompletions =
config.tabAutocompleteOptions?.multilineCompletions ?? "auto";
const modelTimeout =
config.tabAutocompleteOptions?.modelTimeout ?? 150;
const debounceDelay =
config.tabAutocompleteOptions?.debounceDelay ?? 250;
const fontSize = getFontSize();
const cancelChangeDisableAutocomplete = () => {
@ -218,7 +222,7 @@ export function UserSettingsForm() {
}
text="Use Autocomplete Cache"
/> */}
<label className="flex items-center justify-between gap-3">
<span className="text-left">Font Size</span>
<NumberInput
@ -232,28 +236,52 @@ export function UserSettingsForm() {
max={50}
/>
</label>
<label className="flex items-center justify-between gap-3">
<span className="line-clamp-1 text-left">
Multiline Autocompletions
</span>
<Select
value={useAutocompleteMultilineCompletions}
onChange={(e) =>
handleUpdate({
useAutocompleteMultilineCompletions: e.target.value as
| "auto"
| "always"
| "never",
})
}
>
<option value="auto">Auto</option>
<option value="always">Always</option>
<option value="never">Never</option>
</Select>
</label>
<label className="flex items-center justify-between gap-3">
<span className="lines lines-1 text-left">
Multiline Autocompletions
</span>
<Select
value={useAutocompleteMultilineCompletions}
onChange={(e) =>
handleUpdate({
useAutocompleteMultilineCompletions: e.target.value as
| "auto"
| "always"
| "never",
})
}
>
<option value="auto">Auto</option>
<option value="always">Always</option>
<option value="never">Never</option>
</Select>
</label>
<label className="flex items-center justify-between gap-3">
<span className="text-left">Model Timeout (ms)</span>
<NumberInput
value={modelTimeout}
onChange={(val) =>
handleUpdate({
modelTimeout: val,
})
}
min={100}
max={5000}
/>
</label>
<label className="flex items-center justify-between gap-3">
<span className="text-left">Model Debounce (ms)</span>
<NumberInput
value={debounceDelay}
onChange={(val) =>
handleUpdate({
debounceDelay: val,
})
}
min={0}
max={2500}
/>
</label>
<form
className="flex flex-col gap-1"
onSubmit={(e) => {