Skip to content

Commit e3e7c47

Browse files
committed
Centralize request header construction with a shared utility function
- requests to anthropic doesn't work, could be wrong way we pass api key. create build-request-headers util ├── apps │ ├── browser │ │ ├── package.json │ │ └── tsconfig.json │ ├── editor │ │ ├── package.json │ │ ├── src │ │ │ ├── constants │ │ │ │ ├── providers.ts │ │ │ │ └── secret-storage-keys.ts │ │ │ ├── services │ │ │ │ ├── model-fetcher.ts │ │ │ │ ├── prompt-view-api-calls-manager.ts │ │ │ │ └── providers-manager.ts │ │ │ ├── utils │ │ │ │ ├── build-request-headers.ts │ │ │ │ ├── build-user-content.ts │ │ │ │ └── send-llm-message.ts │ │ │ └── views │ │ │ ├── prompt │ │ │ │ └── backend │ │ │ │ └── message-handlers │ │ │ │ └── handle-make-api-call.ts │ │ │ ├── settings │ │ │ │ ├── backend │ │ │ │ │ └── message-handlers │ │ │ │ │ ├── add-provider │ │ │ │ │ │ └── handle-add-provider.ts │ │ │ │ │ ├── handle-get-providers.ts │ │ │ │ │ └── handle-update-provider.ts │ │ │ │ ├── frontend │ │ │ │ │ └── forms │ │ │ │ │ └── EditProviderForm │ │ │ │ │ └── EditProviderForm.tsx │ │ │ │ └── types │ │ │ │ └── messages.ts │ │ │ └── shared │ │ │ └── actions │ │ │ └── api │ │ │ └── create │ │ │ └── interactions │ │ │ ├── initial-select-model.ts │ │ │ ├── verify-model.ts │ │ │ └── verify-reasoning-effort.ts │ │ └── tsconfig.json │ └── web │ ├── package.json │ └── tsconfig.json ├── package.json ├── packages │ ├── shared │ │ ├── package.json │ │ └── tsconfig.json │ └── ui │ ├── package.json │ └── tsconfig.json └── tsconfig.json
1 parent 7334150 commit e3e7c47

5 files changed

Lines changed: 43 additions & 28 deletions

File tree

‎apps/editor/src/services/model-fetcher.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Logger } from '@shared/utils/logger'
22
import { PROVIDERS } from '@/constants/providers'
33
import axios from 'axios'
4+
import { build_request_headers } from '@/utils/build-request-headers'
45

56
export const MODELS_ROUTE_NOT_FOUND_ERROR = '/models route not found'
67

@@ -37,10 +38,7 @@ export class ModelFetcher {
3738
const response = await axios.get<ApiResponse>(
3839
`${params.base_url}/models`,
3940
{
40-
headers: {
41-
['Content-Type']: 'application/json',
42-
Authorization: params.api_key ? `Bearer ${params.api_key}` : ''
43-
}
41+
headers: build_request_headers(params)
4442
}
4543
)
4644

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { PROVIDERS } from '@/constants/providers'
2+
3+
export const build_request_headers = (params: {
4+
base_url: string
5+
api_key?: string
6+
}): Record<string, string> => {
7+
const headers: Record<string, string> = {
8+
'Content-Type': 'application/json'
9+
}
10+
11+
if (params.api_key) {
12+
if (params.base_url === PROVIDERS.Anthropic.base_url) {
13+
headers['x-api-key'] = params.api_key
14+
headers['anthropic-version'] = '2023-06-01'
15+
} else {
16+
headers['Authorization'] = `Bearer ${params.api_key}`
17+
}
18+
}
19+
20+
if (params.base_url === PROVIDERS.OpenRouter.base_url) {
21+
headers['HTTP-Referer'] = 'https://codeweb.chat/'
22+
headers['X-Title'] = 'Code Web Chat'
23+
}
24+
25+
return headers
26+
}

‎apps/editor/src/utils/send-llm-message.ts‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from 'vscode'
22
import axios, { AxiosResponse } from 'axios'
33
import { Logger } from '@shared/utils/logger'
44
import { t } from '@/i18n'
5+
import { build_request_headers } from '@/utils/build-request-headers'
56

67
type StreamCallback = (tokens_per_second: number, total_tokens: number) => void
78
type ThinkingStreamCallback = (text: string) => void
@@ -257,18 +258,10 @@ export const send_llm_message = async (params: {
257258
}
258259
}
259260

260-
const headers: Record<string, string> = {
261-
...(params.api_key
262-
? { ['Authorization']: `Bearer ${params.api_key}` }
263-
: {}),
264-
['Content-Type']: 'application/json',
265-
...(params.base_url == 'https://openrouter.ai/api/v1'
266-
? {
267-
'HTTP-Referer': 'https://codeweb.chat/',
268-
'X-Title': 'Code Web Chat'
269-
}
270-
: {})
271-
}
261+
const headers = build_request_headers({
262+
base_url: params.base_url,
263+
api_key: params.api_key
264+
})
272265

273266
let response: AxiosResponse<NodeJS.ReadableStream>
274267
let attempt = 0

‎apps/editor/src/views/shared/actions/api/create/interactions/verify-model.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode'
22
import axios from 'axios'
33
import { t } from '@/i18n'
4+
import { build_request_headers } from '@/utils/build-request-headers'
45

56
export const verify_model = async (params: {
67
model: string
@@ -68,12 +69,10 @@ export const verify_model = async (params: {
6869
stream: true
6970
},
7071
{
71-
headers: {
72-
'Content-Type': 'application/json',
73-
...(params.api_key
74-
? { Authorization: `Bearer ${params.api_key}` }
75-
: {})
76-
},
72+
headers: build_request_headers({
73+
base_url: params.base_url,
74+
api_key: params.api_key
75+
}),
7776
signal: abort_controller.signal,
7877
responseType: 'stream'
7978
}

‎apps/editor/src/views/shared/actions/api/create/interactions/verify-reasoning-effort.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from 'vscode'
22
import { Provider } from '@/services/providers-manager'
33
import { apply_reasoning_effort } from '@/utils/apply-reasoning-effort'
44
import axios from 'axios'
5+
import { build_request_headers } from '@/utils/build-request-headers'
56

67
export const verify_reasoning_effort = async (params: {
78
base_url: string
@@ -39,12 +40,10 @@ export const verify_reasoning_effort = async (params: {
3940
params.base_url + '/chat/completions',
4041
body,
4142
{
42-
headers: {
43-
...(params.api_key
44-
? { ['Authorization']: `Bearer ${params.api_key}` }
45-
: {}),
46-
['Content-Type']: 'application/json'
47-
},
43+
headers: build_request_headers({
44+
base_url: params.base_url,
45+
api_key: params.api_key
46+
}),
4847
signal: abort_controller.signal,
4948
responseType: 'stream'
5049
}

0 commit comments

Comments
 (0)