feat(stage-ui): Include OpenRouter attribution headers (#1436)
This commit is contained in:
@@ -4,6 +4,11 @@ import { z } from 'zod'
|
||||
import { createOpenAICompatibleValidators } from '../../validators/openai-compatible'
|
||||
import { defineProvider } from '../registry'
|
||||
|
||||
export const OPENROUTER_ATTRIBUTION_HEADERS: Record<string, string> = {
|
||||
'HTTP-Referer': 'https://airi.moeru.ai/',
|
||||
'X-OpenRouter-Title': 'Project AIRI',
|
||||
}
|
||||
|
||||
const openRouterConfigSchema = z.object({
|
||||
apiKey: z
|
||||
.string('API Key'),
|
||||
@@ -39,7 +44,19 @@ export const providerOpenRouterAI = defineProvider<OpenRouterConfig>({
|
||||
}),
|
||||
}),
|
||||
createProvider(config) {
|
||||
return createOpenRouter(config.apiKey, config.baseUrl)
|
||||
const base = createOpenRouter(config.apiKey, config.baseUrl)
|
||||
return {
|
||||
...base,
|
||||
chat: (model: string) => ({
|
||||
...base.chat(model),
|
||||
fetch: (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const headers = new Headers(init?.headers)
|
||||
for (const [k, v] of Object.entries(OPENROUTER_ATTRIBUTION_HEADERS))
|
||||
headers.set(k, v)
|
||||
return globalThis.fetch(input, { ...init, headers })
|
||||
},
|
||||
}),
|
||||
}
|
||||
},
|
||||
|
||||
validationRequiredWhen(config) {
|
||||
@@ -48,6 +65,7 @@ export const providerOpenRouterAI = defineProvider<OpenRouterConfig>({
|
||||
validators: {
|
||||
...createOpenAICompatibleValidators({
|
||||
checks: ['connectivity', 'model_list'],
|
||||
additionalHeaders: OPENROUTER_ATTRIBUTION_HEADERS,
|
||||
}),
|
||||
},
|
||||
})
|
||||
|
||||
@@ -2,6 +2,8 @@ import type { SpeechProvider } from '@xsai-ext/providers/utils'
|
||||
|
||||
import type { ModelInfo, ProviderMetadata, VoiceInfo } from '../../providers'
|
||||
|
||||
import { OPENROUTER_ATTRIBUTION_HEADERS } from '../../../libs/providers/providers/openrouter-ai'
|
||||
|
||||
const DEFAULT_BASE_URL = 'https://openrouter.ai/api/v1/'
|
||||
const DEFAULT_MODEL = 'openai/gpt-audio-mini'
|
||||
const PROVIDER_ID = 'openrouter-audio-speech'
|
||||
@@ -145,6 +147,7 @@ function createAudioFetch(apiKey: string, baseUrl: string, model: string) {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
...OPENROUTER_ATTRIBUTION_HEADERS,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model,
|
||||
@@ -187,7 +190,9 @@ function createSpeechProvider(apiKey: string, baseUrl: string): SpeechProvider {
|
||||
}
|
||||
|
||||
async function listModels(baseUrl: string): Promise<ModelInfo[]> {
|
||||
const res = await fetch(`${baseUrl}models?output_modality=audio`)
|
||||
const res = await fetch(`${baseUrl}models?output_modality=audio`, {
|
||||
headers: OPENROUTER_ATTRIBUTION_HEADERS,
|
||||
})
|
||||
if (!res.ok)
|
||||
return []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user