cortexweaver
Version:
CortexWeaver is a command-line interface (CLI) tool that orchestrates a swarm of specialized AI agents, powered by Claude Code and Gemini CLI, to assist in software development. It transforms a high-level project plan (plan.md) into a series of coordinate
381 lines • 14.5 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthStrategies = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const index_1 = require("./index");
/**
* AuthStrategies handles the various authentication strategies and validation logic
*/
class AuthStrategies {
constructor() {
this.homeDir = process.env.HOME || os.homedir();
}
/**
* Discover Claude authentication methods in order of priority:
* 1. Claude Code environment authentication
* 2. Claude Code session tokens
* 3. Claude Code config files
* 4. ANTHROPIC_API_KEY environment variable
* 5. CLAUDE_API_KEY environment variable (legacy)
*/
async discoverClaudeAuthentication() {
// 1. Check for Claude Code environment authentication (highest priority)
const envAuth = await this.checkClaudeCodeEnvironment();
if (envAuth && envAuth.method === index_1.AuthMethod.CLAUDE_CODE_SESSION) {
return envAuth;
}
// 2. Check for Claude Code session tokens
const sessionAuth = await this.checkClaudeCodeSession();
if (sessionAuth && sessionAuth.method === index_1.AuthMethod.CLAUDE_CODE_SESSION) {
return sessionAuth;
}
// 2. Check for Claude Code config files
const configAuth = await this.checkClaudeCodeConfig();
if (configAuth && configAuth.method === index_1.AuthMethod.CLAUDE_CODE_CONFIG) {
return configAuth;
}
// 3. Check for ANTHROPIC_API_KEY environment variable
const anthropicApiKey = process.env.ANTHROPIC_API_KEY;
if (anthropicApiKey) {
return {
method: index_1.AuthMethod.API_KEY,
details: { api_key: anthropicApiKey }
};
}
// 4. Check for CLAUDE_API_KEY environment variable (legacy)
const claudeApiKey = process.env.CLAUDE_API_KEY;
if (claudeApiKey) {
return {
method: index_1.AuthMethod.API_KEY,
details: { api_key: claudeApiKey }
};
}
// If we found an expired session, report that specifically
if (sessionAuth && sessionAuth.error) {
return sessionAuth;
}
// No authentication found
return {
method: index_1.AuthMethod.NONE,
error: 'No Claude authentication found'
};
}
/**
* Check for Claude Code environment authentication
* This detects when we're running inside Claude Code CLI
*/
async checkClaudeCodeEnvironment() {
try {
// Check if we're running in Claude Code environment
const claudeCodeEnv = process.env.CLAUDECODE;
if (claudeCodeEnv === '1') {
// We're running in Claude Code environment, authentication should be inherited
return {
method: index_1.AuthMethod.CLAUDE_CODE_SESSION,
details: {
session_token: 'claude-code-inherited',
environment: 'claude-code-cli'
}
};
}
return null;
}
catch (error) {
return null;
}
}
/**
* Check for Claude Code session authentication
*/
async checkClaudeCodeSession() {
try {
const sessionPath = path.join(this.homeDir, '.config', 'claude', 'session.json');
if (!fs.existsSync(sessionPath)) {
return null;
}
const sessionContent = fs.readFileSync(sessionPath, 'utf-8');
const sessionData = JSON.parse(sessionContent);
// Check if session is expired
if (sessionData.expires_at) {
const expiresAt = new Date(sessionData.expires_at);
if (expiresAt <= new Date()) {
return {
method: index_1.AuthMethod.NONE,
error: 'Claude Code session token has expired'
};
}
}
return {
method: index_1.AuthMethod.CLAUDE_CODE_SESSION,
details: {
session_token: sessionData.session_token,
expires_at: sessionData.expires_at,
user_id: sessionData.user_id
}
};
}
catch (error) {
return {
method: index_1.AuthMethod.NONE,
error: `Failed to read Claude Code session configuration: ${error.message}`
};
}
}
/**
* Check for Claude Code config file authentication
*/
async checkClaudeCodeConfig() {
try {
const configPath = path.join(this.homeDir, '.config', 'claude', 'config.json');
if (!fs.existsSync(configPath)) {
return null;
}
const configContent = fs.readFileSync(configPath, 'utf-8');
const configData = JSON.parse(configContent);
if (!configData.api_key) {
return null;
}
return {
method: index_1.AuthMethod.CLAUDE_CODE_CONFIG,
details: {
api_key: configData.api_key
}
};
}
catch (error) {
return {
method: index_1.AuthMethod.NONE,
error: `Failed to read Claude Code configuration: ${error.message}`
};
}
}
/**
* Discover Gemini authentication methods in order of priority:
* 1. Gemini CLI configuration
* 2. GOOGLE_API_KEY environment variable
* 3. GEMINI_API_KEY environment variable (legacy)
*/
async discoverGeminiAuthentication() {
// 1. Check for Gemini CLI configuration
const geminiCliAuth = await this.checkGeminiCli();
if (geminiCliAuth && geminiCliAuth.method === index_1.AuthMethod.GEMINI_CLI) {
return geminiCliAuth;
}
// 2. Check for GOOGLE_API_KEY environment variable
const googleApiKey = process.env.GOOGLE_API_KEY;
if (googleApiKey) {
return {
method: index_1.AuthMethod.API_KEY,
details: { api_key: googleApiKey }
};
}
// 3. Check for GEMINI_API_KEY environment variable (legacy)
const geminiApiKey = process.env.GEMINI_API_KEY;
if (geminiApiKey) {
return {
method: index_1.AuthMethod.API_KEY,
details: { api_key: geminiApiKey }
};
}
// No authentication found
return {
method: index_1.AuthMethod.NONE,
error: 'No Gemini authentication found'
};
}
/**
* Check for Gemini CLI authentication
*/
async checkGeminiCli() {
try {
// Check for Google Cloud SDK credentials
const gcloudDir = path.join(this.homeDir, '.config', 'gcloud');
const credentialsPath = path.join(gcloudDir, 'application_default_credentials.json');
const geminiConfigPath = path.join(gcloudDir, 'gemini-config.json');
// Check if either credentials exist
if (!fs.existsSync(credentialsPath) && !fs.existsSync(geminiConfigPath)) {
return null;
}
let authDetails = {};
// Read Google Cloud credentials if available
if (fs.existsSync(credentialsPath)) {
const credentialsContent = fs.readFileSync(credentialsPath, 'utf-8');
const credentialsData = JSON.parse(credentialsContent);
authDetails.client_id = credentialsData.client_id;
authDetails.client_secret = credentialsData.client_secret;
authDetails.refresh_token = credentialsData.refresh_token;
}
// Read Gemini CLI config if available
if (fs.existsSync(geminiConfigPath)) {
const geminiContent = fs.readFileSync(geminiConfigPath, 'utf-8');
const geminiData = JSON.parse(geminiContent);
authDetails.api_key = geminiData.api_key;
authDetails.project = geminiData.project;
}
return {
method: index_1.AuthMethod.GEMINI_CLI,
details: authDetails
};
}
catch (error) {
return {
method: index_1.AuthMethod.NONE,
error: `Failed to read Gemini CLI configuration: ${error.message}`
};
}
}
/**
* Check if authentication is valid and not expired
*/
async isAuthenticationValid(auth) {
if (auth.method === index_1.AuthMethod.NONE) {
return false;
}
// Check for authentication errors
if (auth.error) {
return false;
}
// Check for expiration
if (await this.isAuthExpired(auth)) {
return false;
}
// Validate based on method
if (auth.method === index_1.AuthMethod.CLAUDE_CODE_SESSION ||
auth.method === index_1.AuthMethod.CLAUDE_CODE_CONFIG) {
return await this.validateClaudeAuth(auth);
}
if (auth.method === index_1.AuthMethod.GEMINI_CLI) {
return await this.validateGeminiAuth(auth);
}
if (auth.method === index_1.AuthMethod.API_KEY) {
return await this.validateApiKeyAuth(auth);
}
return false;
}
/**
* Check if authentication is expired
*/
async isAuthExpired(auth) {
if (auth.details?.expires_at) {
const expiresAt = new Date(auth.details.expires_at);
return expiresAt <= new Date();
}
return false;
}
/**
* Validate Claude authentication
*/
async validateClaudeAuth(auth) {
if (auth.method === index_1.AuthMethod.CLAUDE_CODE_SESSION) {
return !!(auth.details?.session_token);
}
if (auth.method === index_1.AuthMethod.CLAUDE_CODE_CONFIG || auth.method === index_1.AuthMethod.API_KEY) {
const apiKey = auth.details?.api_key;
return !!(apiKey && (apiKey.startsWith('sk-ant-') || apiKey.startsWith('sk-claude-')));
}
return false;
}
/**
* Validate Gemini authentication
*/
async validateGeminiAuth(auth) {
if (auth.method === index_1.AuthMethod.GEMINI_CLI) {
return !!(auth.details?.api_key || auth.details?.refresh_token);
}
if (auth.method === index_1.AuthMethod.API_KEY) {
return !!(auth.details?.api_key);
}
return false;
}
/**
* Validate API key authentication
*/
async validateApiKeyAuth(auth) {
const apiKey = auth.details?.api_key;
if (!apiKey)
return false;
// Basic format validation
return apiKey.length > 10; // Very basic validation
}
/**
* Generate authentication recommendations
*/
generateRecommendations(claudeAuth, geminiAuth) {
const recommendations = [];
// Claude recommendations
if (claudeAuth.method === index_1.AuthMethod.NONE) {
recommendations.push('No Claude authentication found. Install Claude Code CLI or set ANTHROPIC_API_KEY environment variable.');
}
else if (claudeAuth.error) {
recommendations.push(`Claude authentication issue: ${claudeAuth.error}`);
}
// Gemini recommendations
if (geminiAuth.method === index_1.AuthMethod.NONE) {
recommendations.push('No Gemini authentication found. Install Gemini CLI or set GOOGLE_API_KEY environment variable.');
}
else if (geminiAuth.error) {
recommendations.push(`Gemini authentication issue: ${geminiAuth.error}`);
}
// Priority recommendations
if (claudeAuth.method === index_1.AuthMethod.API_KEY && !claudeAuth.details?.api_key?.startsWith('sk-ant-')) {
recommendations.push('Consider using Claude Code CLI for better authentication management and Max Plan features.');
}
if (recommendations.length === 0) {
recommendations.push('All authentication methods are properly configured.');
}
return recommendations;
}
/**
* Attempt to refresh Claude authentication
*/
async refreshClaudeAuth() {
// For now, we don't implement automatic refresh
// This would require calling Claude Code CLI or handling OAuth flows
return false;
}
/**
* Attempt to refresh Gemini authentication
*/
async refreshGeminiAuth() {
// For now, we don't implement automatic refresh
// This would require calling gcloud CLI or handling OAuth flows
return false;
}
}
exports.AuthStrategies = AuthStrategies;
//# sourceMappingURL=auth-strategies.js.map