File tree Expand file tree Collapse file tree
views/shared/actions/api/create/interactions Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { Logger } from '@shared/utils/logger'
22import { PROVIDERS } from '@/constants/providers'
33import axios from 'axios'
4+ import { build_request_headers } from '@/utils/build-request-headers'
45
56export 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import * as vscode from 'vscode'
22import axios , { AxiosResponse } from 'axios'
33import { Logger } from '@shared/utils/logger'
44import { t } from '@/i18n'
5+ import { build_request_headers } from '@/utils/build-request-headers'
56
67type StreamCallback = ( tokens_per_second : number , total_tokens : number ) => void
78type 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
Original file line number Diff line number Diff line change 11import * as vscode from 'vscode'
22import axios from 'axios'
33import { t } from '@/i18n'
4+ import { build_request_headers } from '@/utils/build-request-headers'
45
56export 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 }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import * as vscode from 'vscode'
22import { Provider } from '@/services/providers-manager'
33import { apply_reasoning_effort } from '@/utils/apply-reasoning-effort'
44import axios from 'axios'
5+ import { build_request_headers } from '@/utils/build-request-headers'
56
67export 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 }
You can’t perform that action at this time.
0 commit comments