buroventures-harald-code
Version:
Harald Code - AI-powered coding assistant CLI
67 lines • 2.83 kB
JavaScript
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { AuthType } from 'buroventures-harald-code-core';
import { loadEnvironment } from './settings.js';
import { persistCerebrasConfig } from '../utils/envPersistence.js';
export const validateAuthMethod = (authMethod) => {
loadEnvironment();
if (authMethod === AuthType.LOGIN_WITH_GOOGLE ||
authMethod === AuthType.CLOUD_SHELL) {
return null;
}
if (authMethod === AuthType.USE_GEMINI) {
if (!process.env.GEMINI_API_KEY) {
return 'GEMINI_API_KEY environment variable not found. Add that to your environment and try again (no reload needed if using .env)!';
}
return null;
}
if (authMethod === AuthType.USE_VERTEX_AI) {
const hasVertexProjectLocationConfig = !!process.env.GOOGLE_CLOUD_PROJECT && !!process.env.GOOGLE_CLOUD_LOCATION;
const hasGoogleApiKey = !!process.env.GOOGLE_API_KEY;
if (!hasVertexProjectLocationConfig && !hasGoogleApiKey) {
return ('When using Vertex AI, you must specify either:\n' +
'• GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION environment variables.\n' +
'• GOOGLE_API_KEY environment variable (if using express mode).\n' +
'Update your environment and try again (no reload needed if using .env)!');
}
return null;
}
if (authMethod === AuthType.USE_OPENAI) {
if (!process.env.CEREBRAS_API_KEY && !process.env.OPENAI_API_KEY) {
return 'CEREBRAS_API_KEY or OPENAI_API_KEY environment variable not found. You can enter it interactively, add it to your .env file, or configure multiple API keys in settings.json for automatic rotation.';
}
return null;
}
return 'Invalid auth method selected.';
};
export const setOpenAIApiKey = (apiKey) => {
process.env.OPENAI_API_KEY = apiKey;
};
export const setOpenAIBaseUrl = (baseUrl) => {
process.env.OPENAI_BASE_URL = baseUrl;
};
export const setOpenAIModel = (model) => {
process.env.OPENAI_MODEL = model;
};
/**
* Sets and persists Cerebras/OpenAI configuration for future CLI sessions
*/
export const setAndPersistCerebrasConfig = async (apiKey, baseUrl, model) => {
// Set for current session
if (apiKey.startsWith('csk-') || baseUrl.includes('cerebras')) {
// This is a Cerebras key, use CEREBRAS_API_KEY
process.env.CEREBRAS_API_KEY = apiKey;
}
else {
// This might be an OpenAI key, use OPENAI_API_KEY
process.env.OPENAI_API_KEY = apiKey;
}
process.env.OPENAI_BASE_URL = baseUrl;
process.env.OPENAI_MODEL = model;
// Persist to shell profile for future sessions
await persistCerebrasConfig(apiKey, baseUrl, model);
};
//# sourceMappingURL=auth.js.map