-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
187 lines (155 loc) · 5.1 KB
/
index.js
File metadata and controls
187 lines (155 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env node
const { spawn } = require('child_process');
const { program } = require('commander');
const keytar = require('keytar');
const readline = require('readline');
const { start: startProxyServer } = require('./proxy.js');
const SERVICE_NAME = 'grok-cli';
const ACCOUNT_NAME = 'grok-api-key';
program
.name('grok')
.description('Start anthropic-proxy with Grok model and run claude-code')
.option('-k, --api-key <key>', 'Grok API key (will be stored in keychain)')
.option('-p, --port <port>', 'Port for the proxy server', '3000')
.option('--base-url <url>', 'Base URL for the API endpoint', 'https://api.x.ai')
.option('--reasoning-model <model>', 'Reasoning model to use', 'grok-4')
.option('--completion-model <model>', 'Completion model to use', 'grok-3')
.option('--debug', 'Enable debug logging')
.option('--reset-key', 'Reset the stored API key')
.parse();
const options = program.opts();
let claudeProcess = null;
// Function to cleanup processes
function cleanup() {
console.log('\nShutting down...');
if (claudeProcess) {
claudeProcess.kill('SIGTERM');
}
process.exit(0);
}
// Handle process termination
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
// Function to prompt for API key
function promptForApiKey() {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter your Grok API key: ', (answer) => {
rl.close();
resolve(answer.trim());
});
});
}
// Function to get API key from keychain or prompt
async function getApiKey() {
// If reset-key flag is used, delete the stored key
if (options.resetKey) {
try {
await keytar.deletePassword(SERVICE_NAME, ACCOUNT_NAME);
console.log('Stored API key has been reset.');
} catch (error) {
// Ignore error if key doesn't exist
}
}
// Check if API key is provided via command line
if (options.apiKey) {
try {
await keytar.setPassword(SERVICE_NAME, ACCOUNT_NAME, options.apiKey);
console.log('API key stored in keychain.');
return options.apiKey;
} catch (error) {
console.error('Failed to store API key in keychain:', error.message);
return options.apiKey;
}
}
// Try to get API key from keychain
try {
const storedKey = await keytar.getPassword(SERVICE_NAME, ACCOUNT_NAME);
if (storedKey) {
console.log('Using stored API key from keychain.');
return storedKey;
}
} catch (error) {
console.error('Failed to retrieve API key from keychain:', error.message);
}
// Prompt for API key if not found
console.log('No API key found. Please enter your Grok API key.');
console.log('You can get one from: https://console.x.ai/');
const newKey = await promptForApiKey();
if (!newKey) {
console.error('API key is required to continue.');
process.exit(1);
}
// Store the new key in keychain
try {
await keytar.setPassword(SERVICE_NAME, ACCOUNT_NAME, newKey);
console.log('API key stored in keychain for future use.');
} catch (error) {
console.error('Failed to store API key in keychain:', error.message);
console.log('Continuing with session-only API key...');
}
return newKey;
}
async function startProxy(apiKey) {
console.log('Starting proxy server with Grok model...');
const proxyOptions = {
key: apiKey,
baseUrl: options.baseUrl,
debug: options.debug
};
try {
// Start the proxy server directly
await startProxyServer(options.port, proxyOptions);
} catch (error) {
console.error('Failed to start proxy:', error);
throw error;
}
}
async function startClaude() {
console.log('Starting claude-code...');
const claudeEnv = {
...process.env,
ANTHROPIC_BASE_URL: `http://localhost:${options.port}`,
ANTHROPIC_API_KEY: 'sk-ant-api03-demo',
ANTHROPIC_MODEL: options.reasoningModel,
ANTHROPIC_SMALL_FAST_MODEL: options.completionModel,
CLAUDE_CODE_DISABLE_TERMINAL_TITLE: 1,
DISABLE_ERROR_REPORTING: 1,
DISABLE_NON_ESSENTIAL_MODEL_CALLS: 1,
DISABLE_TELEMETRY: 1,
};
claudeProcess = spawn('claude', [], {
env: claudeEnv,
stdio: 'inherit'
});
claudeProcess.on('error', (error) => {
console.error('Failed to start claude-code:', error);
console.error('Make sure claude-code is installed and available in your PATH');
cleanup();
});
claudeProcess.on('exit', (code) => {
console.log(`Claude-code exited with code ${code}`);
cleanup();
});
}
async function main() {
try {
console.log('🚀 Grok CLI');
console.log('================');
const apiKey = await getApiKey();
await startProxy(apiKey);
console.log(`✅ Proxy started on port ${options.port}`);
console.log(`🤖 Using model: ${options.reasoningModel}`);
console.log(`🔗 Base URL: ${options.baseUrl}`);
// Wait a moment for the proxy to be fully ready
await new Promise(resolve => setTimeout(resolve, 1000));
await startClaude();
} catch (error) {
console.error('❌ Error:', error.message);
cleanup();
}
}
main();