|
1 | 1 | import { Page } from '@browserbasehq/stagehand'; |
2 | | -import { existsSync, cpSync, mkdirSync } from 'fs'; |
| 2 | +import { existsSync, cpSync, mkdirSync, readFileSync } from 'fs'; |
3 | 3 | import { platform } from 'os'; |
4 | 4 | import { join } from 'path'; |
| 5 | +import { execSync } from 'child_process'; |
| 6 | + |
| 7 | +// Retrieve Claude Code API key from system keychain |
| 8 | +export function getClaudeCodeApiKey(): string | null { |
| 9 | + try { |
| 10 | + if (platform() === 'darwin') { |
| 11 | + const result = execSync( |
| 12 | + 'security find-generic-password -s "Claude Code" -w 2>/dev/null', |
| 13 | + { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] } |
| 14 | + ).trim(); |
| 15 | + if (result && result.startsWith('sk-ant-')) { |
| 16 | + return result; |
| 17 | + } |
| 18 | + } else if (platform() === 'win32') { |
| 19 | + try { |
| 20 | + const psCommand = `$cred = Get-StoredCredential -Target "Claude Code" -ErrorAction SilentlyContinue; if ($cred) { $cred.GetNetworkCredential().Password }`; |
| 21 | + const result = execSync(`powershell -Command "${psCommand}"`, { |
| 22 | + encoding: 'utf-8', |
| 23 | + stdio: ['pipe', 'pipe', 'pipe'] |
| 24 | + }).trim(); |
| 25 | + if (result && result.startsWith('sk-ant-')) { |
| 26 | + return result; |
| 27 | + } |
| 28 | + } catch {} |
| 29 | + } else { |
| 30 | + // Linux |
| 31 | + const configPaths = [ |
| 32 | + join(process.env.HOME || '', '.claude', 'credentials'), |
| 33 | + join(process.env.HOME || '', '.config', 'claude-code', 'credentials'), |
| 34 | + join(process.env.XDG_CONFIG_HOME || join(process.env.HOME || '', '.config'), 'claude-code', 'credentials'), |
| 35 | + ]; |
| 36 | + for (const configPath of configPaths) { |
| 37 | + if (existsSync(configPath)) { |
| 38 | + try { |
| 39 | + const content = readFileSync(configPath, 'utf-8').trim(); |
| 40 | + if (content.startsWith('sk-ant-')) { |
| 41 | + return content; |
| 42 | + } |
| 43 | + const parsed = JSON.parse(content); |
| 44 | + if (parsed.apiKey && parsed.apiKey.startsWith('sk-ant-')) { |
| 45 | + return parsed.apiKey; |
| 46 | + } |
| 47 | + } catch {} |
| 48 | + } |
| 49 | + } |
| 50 | + try { |
| 51 | + const result = execSync( |
| 52 | + 'secret-tool lookup service "Claude Code" 2>/dev/null', |
| 53 | + { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] } |
| 54 | + ).trim(); |
| 55 | + if (result && result.startsWith('sk-ant-')) { |
| 56 | + return result; |
| 57 | + } |
| 58 | + } catch {} |
| 59 | + } |
| 60 | + } catch {} |
| 61 | + return null; |
| 62 | +} |
| 63 | + |
| 64 | +// Get API key from env or Claude Code keychain |
| 65 | +export function getAnthropicApiKey(): { apiKey: string; source: 'env' | 'claude-code' } | null { |
| 66 | + if (process.env.ANTHROPIC_API_KEY) { |
| 67 | + return { apiKey: process.env.ANTHROPIC_API_KEY, source: 'env' }; |
| 68 | + } |
| 69 | + const claudeCodeKey = getClaudeCodeApiKey(); |
| 70 | + if (claudeCodeKey) { |
| 71 | + return { apiKey: claudeCodeKey, source: 'claude-code' }; |
| 72 | + } |
| 73 | + return null; |
| 74 | +} |
5 | 75 |
|
6 | 76 | /** |
7 | 77 | * Finds the local Chrome installation path based on the operating system |
|
0 commit comments