Skip to content

Commit 8ec162a

Browse files
committed
feat: add config
1 parent 9756b22 commit 8ec162a

File tree

11 files changed

+304
-183
lines changed

11 files changed

+304
-183
lines changed

src/containers/Tenant/Query/QueryEditor/QueryEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export default function QueryEditor(props: QueryEditorProps) {
222222
if (window.api.codeAssistant) {
223223
const provider = getCompletionProvider();
224224
if (provider) {
225-
registerCompletionCommands(monaco, provider);
225+
registerCompletionCommands(monaco, provider, editor);
226226
}
227227
}
228228

src/services/codeCompletion/CodeCompletionService.ts

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
22

33
import {getPromptFileContent} from './promptContent';
44
import type {
5+
CodeCompletionConfig,
56
DiscardReason,
67
EnrichedCompletion,
78
ICodeCompletionAPI,
@@ -10,27 +11,61 @@ import type {
1011
InternalSuggestion,
1112
} from './types';
1213

14+
const DEFAULT_CONFIG: Required<CodeCompletionConfig> = {
15+
debounceTime: 200,
16+
textLimits: {
17+
beforeCursor: 8000,
18+
afterCursor: 1000,
19+
},
20+
telemetry: {
21+
enabled: true,
22+
},
23+
suggestionCache: {
24+
enabled: true,
25+
},
26+
};
27+
1328
export class CodeCompletionService implements ICodeCompletionService {
1429
private prevSuggestions: InternalSuggestion[] = [];
1530
private timer: number | null = null;
1631
private readonly api: ICodeCompletionAPI;
1732
private readonly telemetry: ITelemetryService;
18-
private readonly editor?: monaco.editor.IStandaloneCodeEditor;
33+
private readonly config: Required<CodeCompletionConfig>;
1934

2035
constructor(
2136
api: ICodeCompletionAPI,
2237
telemetry: ITelemetryService,
23-
editor?: monaco.editor.IStandaloneCodeEditor,
38+
userConfig?: CodeCompletionConfig,
2439
) {
2540
this.api = api;
2641
this.telemetry = telemetry;
27-
this.editor = editor;
42+
// Merge user config with defaults, ensuring all properties exist
43+
this.config = {
44+
...DEFAULT_CONFIG,
45+
...userConfig,
46+
textLimits: {
47+
...DEFAULT_CONFIG.textLimits,
48+
...(userConfig?.textLimits || {}),
49+
},
50+
telemetry: {
51+
...DEFAULT_CONFIG.telemetry,
52+
...(userConfig?.telemetry || {}),
53+
},
54+
suggestionCache: {
55+
...DEFAULT_CONFIG.suggestionCache,
56+
...(userConfig?.suggestionCache || {}),
57+
},
58+
};
2859
}
2960

3061
handleItemDidShow(
3162
_completions: monaco.languages.InlineCompletions<EnrichedCompletion>,
3263
item: EnrichedCompletion,
3364
) {
65+
if (!this.config.suggestionCache.enabled) {
66+
return;
67+
}
68+
3469
for (const suggests of this.prevSuggestions) {
3570
for (const completion of suggests.items) {
3671
if (completion.pristine === item.pristine) {
@@ -47,10 +82,13 @@ export class CodeCompletionService implements ICodeCompletionService {
4782
_context: monaco.languages.InlineCompletionContext,
4883
_token: monaco.CancellationToken,
4984
) {
50-
const cachedCompletions = this.getCachedCompletion(model, position);
51-
if (cachedCompletions.length) {
52-
return {items: cachedCompletions};
85+
if (this.config.suggestionCache.enabled) {
86+
const cachedCompletions = this.getCachedCompletion(model, position);
87+
if (cachedCompletions.length) {
88+
return {items: cachedCompletions};
89+
}
5390
}
91+
5492
while (this.prevSuggestions.length > 0) {
5593
this.dismissCompletion(this.prevSuggestions.pop());
5694
}
@@ -89,13 +127,14 @@ export class CodeCompletionService implements ICodeCompletionService {
89127
this.telemetry.sendAcceptTelemetry(requestId, suggestionText);
90128
}
91129

92-
commandDiscard(reason: DiscardReason = 'OnCancel'): void {
130+
commandDiscard(
131+
reason: DiscardReason = 'OnCancel',
132+
editor: monaco.editor.IStandaloneCodeEditor,
133+
): void {
93134
while (this.prevSuggestions.length > 0) {
94135
this.discardCompletion(reason, this.prevSuggestions.pop());
95136
}
96-
if (this.editor) {
97-
this.editor.trigger(undefined, 'editor.action.inlineSuggest.hide', undefined);
98-
}
137+
editor.trigger(undefined, 'editor.action.inlineSuggest.hide', undefined);
99138
}
100139

101140
emptyCache() {
@@ -165,12 +204,15 @@ export class CodeCompletionService implements ICodeCompletionService {
165204
window.clearTimeout(this.timer);
166205
}
167206
await new Promise((r) => {
168-
this.timer = window.setTimeout(r, 200);
207+
this.timer = window.setTimeout(r, this.config.debounceTime);
169208
});
170209
let suggestions: EnrichedCompletion[] = [];
171210
let requestId = '';
172211
try {
173-
const data = getPromptFileContent(model, position);
212+
const data = getPromptFileContent(model, position, {
213+
beforeCursor: this.config.textLimits.beforeCursor,
214+
afterCursor: this.config.textLimits.afterCursor,
215+
});
174216
if (!data) {
175217
return {suggestions: []};
176218
}

0 commit comments

Comments
 (0)