WIP: remove setApiKey, resolveApiKey

This commit is contained in:
Mario Zechner 2025-12-24 23:34:23 +01:00
parent 385e7aff62
commit d93cbf8c32
6 changed files with 21 additions and 61 deletions

View file

@ -5,7 +5,7 @@ import type {
MessageParam,
} from "@anthropic-ai/sdk/resources/messages.js";
import { calculateCost } from "../models.js";
import { getApiKey } from "../stream.js";
import { getApiKeyFromEnv } from "../stream.js";
import type {
Api,
AssistantMessage,
@ -114,7 +114,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
};
try {
const apiKey = options?.apiKey ?? getApiKey(model.provider) ?? "";
const apiKey = options?.apiKey ?? getApiKeyFromEnv(model.provider) ?? "";
const { client, isOAuthToken } = createClient(model, apiKey, options?.interleavedThinking ?? true);
const params = buildParams(model, context, isOAuthToken, options);
const anthropicStream = client.messages.stream({ ...params, stream: true }, { signal: options?.signal });

View file

@ -6,6 +6,7 @@ import {
type ThinkingLevel,
} from "@google/genai";
import { calculateCost } from "../models.js";
import { getApiKeyFromEnv } from "../stream.js";
import type {
Api,
AssistantMessage,
@ -60,7 +61,8 @@ export const streamGoogle: StreamFunction<"google-generative-ai"> = (
};
try {
const client = createClient(model, options?.apiKey);
const apiKey = options?.apiKey || getApiKeyFromEnv(model.provider) || "";
const client = createClient(model, apiKey);
const params = buildParams(model, context, options);
const googleStream = await client.models.generateContentStream(params);
@ -248,15 +250,6 @@ export const streamGoogle: StreamFunction<"google-generative-ai"> = (
};
function createClient(model: Model<"google-generative-ai">, apiKey?: string): GoogleGenAI {
if (!apiKey) {
if (!process.env.GEMINI_API_KEY) {
throw new Error(
"Gemini API key is required. Set GEMINI_API_KEY environment variable or pass it as an argument.",
);
}
apiKey = process.env.GEMINI_API_KEY;
}
const httpOptions: { baseUrl?: string; apiVersion?: string; headers?: Record<string, string> } = {};
if (model.baseUrl) {
httpOptions.baseUrl = model.baseUrl;

View file

@ -9,6 +9,7 @@ import type {
ChatCompletionToolMessageParam,
} from "openai/resources/chat/completions.js";
import { calculateCost } from "../models.js";
import { getApiKeyFromEnv } from "../stream.js";
import type {
AssistantMessage,
Context,
@ -98,7 +99,8 @@ export const streamOpenAICompletions: StreamFunction<"openai-completions"> = (
};
try {
const client = createClient(model, context, options?.apiKey);
const apiKey = options?.apiKey || getApiKeyFromEnv(model.provider) || "";
const client = createClient(model, context, apiKey);
const params = buildParams(model, context, options);
const openaiStream = await client.chat.completions.create(params, { signal: options?.signal });
stream.push({ type: "start", partial: output });

View file

@ -11,6 +11,7 @@ import type {
ResponseReasoningItem,
} from "openai/resources/responses/responses.js";
import { calculateCost } from "../models.js";
import { getApiKeyFromEnv } from "../stream.js";
import type {
Api,
AssistantMessage,
@ -27,7 +28,6 @@ import type {
import { AssistantMessageEventStream } from "../utils/event-stream.js";
import { parseStreamingJson } from "../utils/json-parse.js";
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
import { transformMessages } from "./transorm-messages.js";
/** Fast deterministic hash to shorten long strings */
@ -82,7 +82,8 @@ export const streamOpenAIResponses: StreamFunction<"openai-responses"> = (
try {
// Create OpenAI client
const client = createClient(model, context, options?.apiKey);
const apiKey = options?.apiKey || getApiKeyFromEnv(model.provider) || "";
const client = createClient(model, context, apiKey);
const params = buildParams(model, context, options);
const openaiStream = await client.responses.create(params, { signal: options?.signal });
stream.push({ type: "start", partial: output });