Skip to content

Commit

Permalink
Pass custom headers to rest API client
Browse files Browse the repository at this point in the history
  • Loading branch information
vovakulikov committed Jul 26, 2024
1 parent 53d2453 commit 8a257e4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export {
} from './experimentation/FeatureFlagProvider'
export { GuardrailsPost, summariseAttribution } from './guardrails'
export type { Attribution, Guardrails } from './guardrails'
export { SourcegraphGuardrailsClient, GuardrailsClientConfig } from './guardrails/client'
export { SourcegraphGuardrailsClient } from './guardrails/client'
export type { GuardrailsClientConfig } from './guardrails/client'
export {
CompletionStopReason,
type CodeCompletionsClient,
Expand Down
7 changes: 5 additions & 2 deletions lib/shared/src/sourcegraph-api/rest/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ export class RestClient {
/**
* @param endpointUrl URL to the sourcegraph instance, e.g. "https://sourcegraph.acme.com".
* @param accessToken User access token to contact the sourcegraph instance.
* @param customHeaders Custom headers (primary is used by Cody Web case when Sourcegraph client
* providers set of custom headers to make sure that auth flow will work properly
*/
constructor(
private endpointUrl: string,
private accessToken?: string
private accessToken: string | undefined,
private customHeaders: Record<string, string>
) {}

// Make an authenticated HTTP request to the Sourcegraph instance.
// "name" is a developer-friendly term to label the request's trace span.
private getRequest<T>(name: string, urlSuffix: string): Promise<T | Error> {
const headers = new Headers()
const headers = new Headers(this.customHeaders as HeadersInit)
if (this.accessToken) {
headers.set('Authorization', `token ${this.accessToken}`)
}
Expand Down
7 changes: 6 additions & 1 deletion vscode/src/models/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import type { ServerModelConfiguration } from '@sourcegraph/cody-shared/src/models'
import { ModelTag } from '@sourcegraph/cody-shared/src/models/tags'
import * as vscode from 'vscode'
import { getConfiguration } from '../configuration'
import { logDebug } from '../log'
import { secretStorage } from '../services/SecretStorageProvider'
import { getEnterpriseContextWindow } from './utils'
Expand Down Expand Up @@ -40,6 +41,7 @@ export async function syncModels(authStatus: AuthStatus): Promise<void> {
// Fetch the LLM models and configuration server-side. See:
// https://linear.app/sourcegraph/project/server-side-cody-model-selection-cca47c48da6d
const clientConfig = await ClientConfigSingleton.getInstance().getConfig()

if (clientConfig?.modelsAPIEnabled) {
logDebug('ModelsService', 'new models API enabled')
const serverSideModels = await fetchServerSideModels(authStatus.endpoint || '')
Expand Down Expand Up @@ -147,9 +149,12 @@ async function fetchServerSideModels(endpoint: string): Promise<ServerModelConfi

// Get the user's access token, assumed to be already saved in the secret store.
const userAccessToken = await secretStorage.getToken(endpoint)
const customHeaders = getConfiguration().customHeaders ?? {}

console.log('customHeaders', customHeaders)

// Fetch the data via REST API.
// NOTE: We may end up exposing this data via GraphQL, it's still TBD.
const client = new RestClient(endpoint, userAccessToken)
const client = new RestClient(endpoint, userAccessToken, customHeaders)
return await client.getAvailableModels()
}
4 changes: 4 additions & 0 deletions web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.9
- Add support for custom headers in Rest API service
(fixes problem with fetching remote LLM models for Cody Web)

## 0.2.8
- Adds new prop to set custom client telemetry name (telemetryClientName)

Expand Down
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cody-web-experimental",
"version": "0.2.8",
"version": "0.2.9",
"description": "Cody standalone web app",
"license": "Apache-2.0",
"repository": {
Expand Down

0 comments on commit 8a257e4

Please sign in to comment.