getApiKeyFromEnv -> getEnvApiKey

This commit is contained in:
Mario Zechner 2025-12-25 02:38:00 +01:00
parent bf022d2581
commit 9f97f0c8da
8 changed files with 16 additions and 53 deletions

View file

@ -4,7 +4,7 @@
### Breaking Changes
- **setApiKey, resolveApiKey**: Removed. Callers must manage their own API key storage/resolution.
- **getApiKey**: Renamed to `getApiKeyFromEnv`. Only checks environment variables for known providers.
- **getApiKey**: Renamed to `getEnvApiKey`. Only checks environment variables for known providers.
- **OAuth storage removed**: All storage functions (`loadOAuthCredentials`, `saveOAuthCredentials`, `setOAuthStorage`, etc.) removed. Callers are responsible for storing credentials.
- **OAuth login functions**: `loginAnthropic`, `loginGitHubCopilot`, `loginGeminiCli`, `loginAntigravity` now return `OAuthCredentials` instead of saving to disk.
- **refreshOAuthToken**: Now takes `(provider, credentials)` and returns new `OAuthCredentials` instead of saving.

View file

@ -1103,10 +1103,10 @@ const response = await complete(model, context, {
### Checking Environment Variables
```typescript
import { getApiKeyFromEnv } from '@mariozechner/pi-ai';
import { getEnvApiKey } from '@mariozechner/pi-ai';
// Check if an API key is set in environment variables
const key = getApiKeyFromEnv('openai'); // checks OPENAI_API_KEY
const key = getEnvApiKey('openai'); // checks OPENAI_API_KEY
```
## OAuth Providers

View file

@ -5,7 +5,7 @@ import type {
MessageParam,
} from "@anthropic-ai/sdk/resources/messages.js";
import { calculateCost } from "../models.js";
import { getApiKeyFromEnv } from "../stream.js";
import { getEnvApiKey } from "../stream.js";
import type {
Api,
AssistantMessage,
@ -114,7 +114,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
};
try {
const apiKey = options?.apiKey ?? getApiKeyFromEnv(model.provider) ?? "";
const apiKey = options?.apiKey ?? getEnvApiKey(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,7 +6,7 @@ import {
type ThinkingLevel,
} from "@google/genai";
import { calculateCost } from "../models.js";
import { getApiKeyFromEnv } from "../stream.js";
import { getEnvApiKey } from "../stream.js";
import type {
Api,
AssistantMessage,
@ -61,7 +61,7 @@ export const streamGoogle: StreamFunction<"google-generative-ai"> = (
};
try {
const apiKey = options?.apiKey || getApiKeyFromEnv(model.provider) || "";
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
const client = createClient(model, apiKey);
const params = buildParams(model, context, options);
const googleStream = await client.models.generateContentStream(params);

View file

@ -9,7 +9,7 @@ import type {
ChatCompletionToolMessageParam,
} from "openai/resources/chat/completions.js";
import { calculateCost } from "../models.js";
import { getApiKeyFromEnv } from "../stream.js";
import { getEnvApiKey } from "../stream.js";
import type {
AssistantMessage,
Context,
@ -99,7 +99,7 @@ export const streamOpenAICompletions: StreamFunction<"openai-completions"> = (
};
try {
const apiKey = options?.apiKey || getApiKeyFromEnv(model.provider) || "";
const apiKey = options?.apiKey || getEnvApiKey(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 });

View file

@ -11,7 +11,7 @@ import type {
ResponseReasoningItem,
} from "openai/resources/responses/responses.js";
import { calculateCost } from "../models.js";
import { getApiKeyFromEnv } from "../stream.js";
import { getEnvApiKey } from "../stream.js";
import type {
Api,
AssistantMessage,
@ -82,7 +82,7 @@ export const streamOpenAIResponses: StreamFunction<"openai-responses"> = (
try {
// Create OpenAI client
const apiKey = options?.apiKey || getApiKeyFromEnv(model.provider) || "";
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
const client = createClient(model, context, apiKey);
const params = buildParams(model, context, options);
const openaiStream = await client.responses.create(params, { signal: options?.signal });

View file

@ -22,9 +22,9 @@ import type {
*
* Will not return API keys for providers that require OAuth tokens.
*/
export function getApiKeyFromEnv(provider: KnownProvider): string | undefined;
export function getApiKeyFromEnv(provider: string): string | undefined;
export function getApiKeyFromEnv(provider: any): string | undefined {
export function getEnvApiKey(provider: KnownProvider): string | undefined;
export function getEnvApiKey(provider: string): string | undefined;
export function getEnvApiKey(provider: any): string | undefined {
// Fall back to environment variables
if (provider === "github-copilot") {
return process.env.COPILOT_GITHUB_TOKEN || process.env.GH_TOKEN || process.env.GITHUB_TOKEN;
@ -51,7 +51,7 @@ export function stream<TApi extends Api>(
context: Context,
options?: OptionsForApi<TApi>,
): AssistantMessageEventStream {
const apiKey = options?.apiKey || getApiKeyFromEnv(model.provider);
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
if (!apiKey) {
throw new Error(`No API key for provider: ${model.provider}`);
}
@ -100,7 +100,7 @@ export function streamSimple<TApi extends Api>(
context: Context,
options?: SimpleStreamOptions,
): AssistantMessageEventStream {
const apiKey = options?.apiKey || getApiKeyFromEnv(model.provider);
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
if (!apiKey) {
throw new Error(`No API key for provider: ${model.provider}`);
}

View file

@ -1,37 +0,0 @@
#!/bin/bash
# Unset all provider API keys
unset OPENAI_API_KEY
unset OPENAI_KEY
unset ANTHROPIC_API_KEY
unset ANTHROPIC_OAUTH_TOKEN
unset GROQ_API_KEY
unset OPENROUTER_API_KEY
unset GEMINI_API_KEY
unset CEREBRAS_API_KEY
unset HF_TOKEN
unset XAI_API_KEY
unset ZAI_API_KEY
unset EXA_API_KEY
unset MINIMAX_API_KEY
unset MISTRAL_API_KEY
unset ELEVENLABS_API_KEY
unset BRAVE_API_KEY
echo "Unset the following provider API keys:"
echo " OPENAI_API_KEY"
echo " OPENAI_KEY"
echo " ANTHROPIC_API_KEY"
echo " ANTHROPIC_OAUTH_TOKEN"
echo " GROQ_API_KEY"
echo " OPENROUTER_API_KEY"
echo " GEMINI_API_KEY"
echo " CEREBRAS_API_KEY"
echo " HF_TOKEN"
echo " XAI_API_KEY"
echo " ZAI_API_KEY"
echo " EXA_API_KEY"
echo " MINIMAX_API_KEY"
echo " MISTRAL_API_KEY"
echo " ELEVENLABS_API_KEY"
echo " BRAVE_API_KEY"