cody-gateway: handle missing Google response (#63895)

- Logs a warning instead of returning an error when the Google response
is missing
- This prevents the API from returning an error when the Google response
is empty, which can happen when Google is not happy with the question
due to safety issues. We will only log a decoder error as error.

<!-- PR description tips:
https://www.notion.so/sourcegraph/Write-a-good-pull-request-description-610a7fd3e613496eb76f450db5a49b6e
-->

## Test plan

<!-- REQUIRED; info at
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles
-->

only the log level was updated for empty responses. the function
behavior was not changed.

## Changelog

<!-- OPTIONAL; info at
https://www.notion.so/sourcegraph/Writing-a-changelog-entry-dd997f411d524caabf0d8d38a24a878c
-->

cody-gateway: log missing Google response as warning
This commit is contained in:
Beatrix 2024-07-24 10:58:19 -07:00 committed by GitHub
parent 56817a7a9e
commit 468a01a3ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 3 deletions

View File

@ -168,5 +168,6 @@ func parseGoogleTokenUsage(r io.Reader, logger log.Logger) (promptTokens int, co
return res.UsageMetadata.PromptTokenCount, res.UsageMetadata.CompletionTokenCount, nil
}
return -1, -1, errors.New("no Google response found")
logger.Warn("no Google response found", log.Error(err))
return -1, -1, nil
}

View File

@ -128,13 +128,13 @@ func TestParseGoogleTokenUsage(t *testing.T) {
name: "no prefix",
input: `{"usageMetadata": {"promptTokenCount": 10, "candidatesTokenCount": 20}}`,
want: nil,
wantErr: true,
wantErr: false,
},
{
name: "empty input",
input: ``,
want: nil,
wantErr: true,
wantErr: false,
},
{
name: "multiple lines with one valid",