use timespan instead of array && abort

pull/5370/head
yann300 1 year ago
parent 4042f5b91e
commit a2a8e02820
  1. 21
      apps/remix-ide/src/app/plugins/copilot/suggestion-service/suggestion-service.ts

@ -4,14 +4,16 @@ export type SuggestOptions = { max_new_tokens: number, temperature: number, top_
export class SuggestionService { export class SuggestionService {
worker: Worker worker: Worker
responses: Array<any> responses: { [key: number]: Function }
events: EventEmitter events: EventEmitter
current: number
constructor() { constructor() {
this.worker = new Worker(new URL('./worker.js', import.meta.url), { this.worker = new Worker(new URL('./worker.js', import.meta.url), {
type: 'module' type: 'module'
}); });
this.events = new EventEmitter() this.events = new EventEmitter()
this.responses = [] this.responses = {}
this.current
} }
async init() { async init() {
@ -50,7 +52,13 @@ export class SuggestionService {
case 'complete': case 'complete':
console.log(e.data) console.log(e.data)
if (this.responses[e.data.id]) { if (this.responses[e.data.id]) {
if (this.current === e.data.id) {
this.responses[e.data.id](null, e.data) this.responses[e.data.id](null, e.data)
} else {
this.responses[e.data.id]('aborted')
}
delete this.responses[e.data.id]
this.current = null
} else { } else {
console.log('no callback for', e.data) console.log('no callback for', e.data)
} }
@ -71,8 +79,11 @@ export class SuggestionService {
suggest (content: string, options: SuggestOptions) { suggest (content: string, options: SuggestOptions) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.current) return reject(new Error('already running'))
const timespan = Date.now()
this.current = timespan
this.worker.postMessage({ this.worker.postMessage({
id: this.responses.length, id: timespan,
cmd: 'suggest', cmd: 'suggest',
text: content, text: content,
max_new_tokens: options.max_new_tokens, max_new_tokens: options.max_new_tokens,
@ -80,10 +91,10 @@ export class SuggestionService {
top_k: options.top_k, top_k: options.top_k,
do_sample: options.do_sample do_sample: options.do_sample
}) })
this.responses.push((error, result) => { this.responses[timespan] = (error, result) => {
if (error) return reject(error) if (error) return reject(error)
resolve(result) resolve(result)
}) }
}) })
} }
} }
Loading…
Cancel
Save