added timer 2 sec to restrict completion request

pull/4607/head
Stéphane Tetsing 9 months ago
parent 63e547d25b
commit 60a5b7d658
  1. 31
      libs/remix-ui/editor/src/lib/providers/completionTimer.ts
  2. 18
      libs/remix-ui/editor/src/lib/providers/inlineCompletionProvider.ts
  3. 2
      libs/remix-ui/settings/src/lib/remix-ui-settings.tsx

@ -0,0 +1,31 @@
export class CompletionTimer {
private duration: number;
private timerId: NodeJS.Timeout | null = null;
private callback: () => void;
constructor(duration: number, callback: () => void) {
this.duration = duration;
this.callback = callback;
}
start() {
if (this.timerId) {
console.error("Timer is already running.");
return;
}
this.timerId = setTimeout(() => {
this.callback();
this.timerId = null;
}, this.duration);
}
stop() {
if (this.timerId) {
clearTimeout(this.timerId);
this.timerId = null;
} else {
console.error("Timer is not running.");
}
}
}

@ -1,5 +1,6 @@
/* eslint-disable no-control-regex */
import { EditorUIProps, monacoTypes } from '@remix-ui/editor';
import { CompletionTimer } from './completionTimer';
import axios, {AxiosResponse} from 'axios'
import { slice } from 'lodash';
@ -12,11 +13,14 @@ const result: string = ''
export class RemixInLineCompletionProvider implements monacoTypes.languages.InlineCompletionsProvider {
props: EditorUIProps
monaco: any
completionEnabled: boolean
constructor(props: any, monaco: any) {
this.props = props
this.monaco = monaco
this.completionEnabled = true
}
async provideInlineCompletions(model: monacoTypes.editor.ITextModel, position: monacoTypes.Position, context: monacoTypes.languages.InlineCompletionContext, token: monacoTypes.CancellationToken): Promise<monacoTypes.languages.InlineCompletions<monacoTypes.languages.InlineCompletion>> {
if (context.selectedSuggestionInfo) {
return;
@ -82,6 +86,11 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
return
}
// abort if the completion is not enabled
if (!this.completionEnabled) {
return
}
let result
try {
const output = await this.props.plugin.call('solcoder', 'code_completion', word)
@ -92,12 +101,17 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
clean = generatedText.replace('@custom:dev-run-script', '@custom:dev-run-script ')
}
clean = clean.replace(word, '').trimStart()
console.log('clean', clean)
clean = this.process_completion(clean)
const item: monacoTypes.languages.InlineCompletion = {
insertText: clean
};
// handle the completion timer by locking suggestions request for 2 seconds
this.completionEnabled = false
const handleCompletionTimer = new CompletionTimer(2000, () => { this.completionEnabled = true });
handleCompletionTimer.start()
return {
items: [item],
enableForwardStability: true
@ -115,7 +129,7 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
return ""
}
// remove comment inline
clean = clean.split('//')[0]
clean = clean.split('//')[0].trimEnd()
return clean
}

@ -131,7 +131,6 @@ export const RemixUiSettings = (props: RemixUiSettingsProps) => {
}
const onchangeCopilotActivate = () => {
console.log("onchangeCopilotActivate ", props.useCopilot)
if (!props.useCopilot) {
copilotActivate(props.config, props.useCopilot, dispatch)
props.plugin.call('terminal', 'log', {type: 'typewriterlog', value: `Solidity copilot deactivated!` })
@ -426,7 +425,6 @@ export const RemixUiSettings = (props: RemixUiSettingsProps) => {
copilotTemperatureValue = 0.9
}
//if (isCopilotActivated) props.plugin.call('copilot-suggestion', 'init')
const copilotSettings = () => (
<div className="border-top">
<div className="card-body pt-3 pb-2">

Loading…
Cancel
Save