fix: Playground completion request cancellation. (#251)
parent
407251b3f5
commit
69084d4c1e
|
|
@ -63,16 +63,21 @@ class CompletionProvider {
|
||||||
this.latestTimestamp = currentTimestamp
|
this.latestTimestamp = currentTimestamp
|
||||||
|
|
||||||
await this.sleep(500)
|
await this.sleep(500)
|
||||||
if (this.pendingRequest) await this.pendingRequest
|
|
||||||
if (currentTimestamp < this.latestTimestamp) {
|
if (currentTimestamp < this.latestTimestamp) {
|
||||||
return emptyResponse
|
return emptyResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.pendingRequest) {
|
||||||
|
this.pendingRequest.cancelToken.cancel()
|
||||||
|
this.pendingRequest = null
|
||||||
|
}
|
||||||
let response
|
let response
|
||||||
try {
|
try {
|
||||||
response = await this.callTabbyApi(currentTimestamp, segments)
|
response = await this.requestCompletion(segments)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("error", err)
|
if (err.code !== "ERR_CANCELED") {
|
||||||
|
console.error("error", err)
|
||||||
|
}
|
||||||
return emptyResponse
|
return emptyResponse
|
||||||
}
|
}
|
||||||
const hasSuffixParen = this.hasSuffixParen(document, position)
|
const hasSuffixParen = this.hasSuffixParen(document, position)
|
||||||
|
|
@ -128,15 +133,22 @@ class CompletionProvider {
|
||||||
return new Promise((r) => setTimeout(r, milliseconds))
|
return new Promise((r) => setTimeout(r, milliseconds))
|
||||||
}
|
}
|
||||||
|
|
||||||
async callTabbyApi(timestamp, segments) {
|
async requestCompletion(segments) {
|
||||||
const request = (this.pendingRequest = axios.post(
|
const cancelToken = axios.CancelToken.source()
|
||||||
`${TabbyServerURL}/v1/completions`,
|
this.pendingRequest = {
|
||||||
{
|
promise: axios.post(
|
||||||
language: "python",
|
`${TabbyServerURL}/v1/completions`,
|
||||||
segments,
|
{
|
||||||
}
|
language: "python",
|
||||||
))
|
segments,
|
||||||
const response = await request
|
},
|
||||||
|
{
|
||||||
|
cancelToken: cancelToken.token,
|
||||||
|
}
|
||||||
|
),
|
||||||
|
cancelToken,
|
||||||
|
}
|
||||||
|
const response = await this.pendingRequest.promise
|
||||||
this.pendingRequest = null
|
this.pendingRequest = null
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue