mirror of
https://github.com/carlrobertoh/ProxyAI.git
synced 2026-05-10 20:30:24 +00:00
feat: cancel completions early on newline (#461)
* Stream completion results and cancel early on newline * Rename 'suggestion, needCancel' to 'message, cancel' * Replace cancelCurrentCall() with eventSource.cancel() for simplicity * remove isStreaming variable and onComplete() method * fix: do not trigger completed callbacks during streaming --------- Co-authored-by: lichuang <lichuanglai8@163.com> Co-authored-by: Carl-Robert Linnupuu <carlrobertoh@gmail.com>
This commit is contained in:
parent
b2d9442eba
commit
63f139dd74
1 changed files with 16 additions and 18 deletions
|
|
@ -43,11 +43,12 @@ class CodeGPTInlineCompletionProvider : InlineCompletionProvider {
|
|||
currentCall.set(
|
||||
CompletionRequestService.getInstance().getCodeCompletionAsync(
|
||||
infillRequest,
|
||||
CodeCompletionEventListener(infillRequest) {
|
||||
request.editor.putUserData(CodeGPTKeys.PREVIOUS_INLAY_TEXT, it)
|
||||
CodeCompletionEventListener {
|
||||
val inlineText = it.takeWhile { message -> message != '\n' }.toString()
|
||||
request.editor.putUserData(CodeGPTKeys.PREVIOUS_INLAY_TEXT, inlineText)
|
||||
launch {
|
||||
try {
|
||||
trySend(InlineCompletionGrayTextElement(it))
|
||||
trySend(InlineCompletionGrayTextElement(inlineText))
|
||||
} catch (e: Exception) {
|
||||
LOG.error("Failed to send inline completion suggestion", e)
|
||||
}
|
||||
|
|
@ -74,24 +75,21 @@ class CodeGPTInlineCompletionProvider : InlineCompletionProvider {
|
|||
}
|
||||
|
||||
class CodeCompletionEventListener(
|
||||
private val requestDetails: InfillRequestDetails,
|
||||
private val completed: (String) -> Unit
|
||||
private val completed: (StringBuilder) -> Unit
|
||||
) : CompletionEventListener<String> {
|
||||
|
||||
override fun onMessage(message: String?, eventSource: EventSource?) {
|
||||
if (message != null && message.contains('\n')) {
|
||||
eventSource?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onComplete(messageBuilder: StringBuilder) {
|
||||
// TODO: https://youtrack.jetbrains.com/issue/CPP-38312/CLion-crashes-around-every-10-minutes-of-work
|
||||
/*val processedOutput = CodeCompletionParserFactory
|
||||
.getParserForFileExtension(requestDetails.fileExtension)
|
||||
.parse(
|
||||
requestDetails.prefix,
|
||||
requestDetails.suffix,
|
||||
messageBuilder.toString()
|
||||
)*/
|
||||
val output =
|
||||
if (messageBuilder.contains("\n"))
|
||||
messageBuilder.substring(0, messageBuilder.indexOf("\n"))
|
||||
else messageBuilder.toString()
|
||||
completed(output)
|
||||
completed(messageBuilder)
|
||||
}
|
||||
|
||||
override fun onCancelled(messageBuilder: StringBuilder) {
|
||||
completed(messageBuilder)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue