Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when receing an SSE response that does not contain any SSE event #3479

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lib/shared/src/sourcegraph-api/completions/nodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class SourcegraphNodeCompletionsClient extends SourcegraphCompletionsClie
// Keep track if we have send any message to the completion callbacks
let didSendMessage = false
let didSendError = false
let didReceiveAnyEvent = false

// Call the error callback only once per request.
const onErrorOnce = (error: Error, statusCode?: number | undefined): void => {
Expand All @@ -64,6 +65,9 @@ export class SourcegraphNodeCompletionsClient extends SourcegraphCompletionsClie
}
}

// Text which has not been decoded as a server-sent event (SSE)
let bufferText = ''

const request = requestFn(
this.completionsEndpoint,
{
Expand Down Expand Up @@ -150,10 +154,8 @@ export class SourcegraphNodeCompletionsClient extends SourcegraphCompletionsClie
return
}

// By tes which have not been decoded as UTF-8 text
// Bytes which have not been decoded as UTF-8 text
let bufferBin = Buffer.of()
// Text which has not been decoded as a server-sent event (SSE)
let bufferText = ''

res.on('data', chunk => {
if (!(chunk instanceof Buffer)) {
Expand All @@ -176,6 +178,7 @@ export class SourcegraphNodeCompletionsClient extends SourcegraphCompletionsClie
}

didSendMessage = true
didReceiveAnyEvent = didReceiveAnyEvent || parseResult.events.length > 0
log?.onEvents(parseResult.events)
this.sendEvents(parseResult.events, cb, span)
bufferText = parseResult.remainingBuffer
Expand Down Expand Up @@ -203,6 +206,15 @@ export class SourcegraphNodeCompletionsClient extends SourcegraphCompletionsClie
//
// We still want to close the request.
request.on('close', () => {
if (!didReceiveAnyEvent) {
logError(
'SourcegraphNodeCompletionsClient',
"request.on('close')",
'Connection closed without receiving any events',
{ verbose: { bufferText } }
)
onErrorOnce(new Error('Connection closed without receiving any events'))
}
if (!didSendMessage) {
onErrorOnce(new Error('Connection unexpectedly closed'))
}
Expand Down
1 change: 1 addition & 0 deletions vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This is a log of all notable changes to Cody for VS Code. [Unreleased] changes a
- Autocomplete: Improve StarCoder2 Ollama support. [pull/3452](https://github.com/sourcegraph/cody/pull/3452)
- Command: Enhanced the context provided to the Test command to help the language model determine the appropriate testing framework to use. [pull/3344](https://github.com/sourcegraph/cody/pull/3344)
- Autocomplete: Upgrade tree-sitter grammars and add Dart support. [pull/3476](https://github.com/sourcegraph/cody/pull/3476)
- Properly throw an error when attempting to parse an incomplete SSE stream with the nodeClient. [pull/3479](https://github.com/sourcegraph/cody/pull/3479)

## [1.8.3]

Expand Down
Loading