feat(ai): add headers option to StreamOptions for custom HTTP headers

- Added headers field to base StreamOptions interface
- Updated all providers to merge options.headers with defaults
- Forward headers and onPayload through streamSimple/completeSimple
- Bedrock not supported (uses AWS SDK auth)
This commit is contained in:
Mario Zechner
2026-01-20 01:08:24 +01:00
parent 20c7b5fed4
commit d2be6486a4
10 changed files with 96 additions and 28 deletions

View File

@@ -99,7 +99,7 @@ export const streamOpenAICompletions: StreamFunction<"openai-completions"> = (
try {
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
const client = createClient(model, context, apiKey);
const client = createClient(model, context, apiKey, options?.headers);
const params = buildParams(model, context, options);
options?.onPayload?.(params);
const openaiStream = await client.chat.completions.create(params, { signal: options?.signal });
@@ -318,7 +318,12 @@ export const streamOpenAICompletions: StreamFunction<"openai-completions"> = (
return stream;
};
function createClient(model: Model<"openai-completions">, context: Context, apiKey?: string) {
function createClient(
model: Model<"openai-completions">,
context: Context,
apiKey?: string,
optionsHeaders?: Record<string, string>,
) {
if (!apiKey) {
if (!process.env.OPENAI_API_KEY) {
throw new Error(
@@ -354,6 +359,11 @@ function createClient(model: Model<"openai-completions">, context: Context, apiK
}
}
// Merge options headers last so they can override defaults
if (optionsHeaders) {
Object.assign(headers, optionsHeaders);
}
return new OpenAI({
apiKey,
baseURL: model.baseUrl,