This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathcli.ts
More file actions
286 lines (251 loc) · 8.36 KB
/
cli.ts
File metadata and controls
286 lines (251 loc) · 8.36 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env bun
import { createInterface } from 'node:readline';
import chalk from 'chalk';
import { Command } from 'commander';
import { ensureProjectStarConsent } from './services/install-consent.js';
import {
callAnthropicApi,
resolveOAuthToken,
validateOAuthToken,
verifyOAuthToken,
} from './services/oauth-client.js';
import { OAuthService } from './services/oauth-flow.js';
import {
clearStoredTokens,
getTokenFilePath,
saveOAuthTokenOnly,
saveStoredTokens,
} from './services/token-store.js';
function startManualCodeCapture(oauthService: OAuthService): () => void {
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
rl.setPrompt('Paste code here if prompted > ');
rl.prompt();
rl.on('line', (line) => {
const value = line.trim();
if (!value.includes('#')) {
rl.prompt();
return;
}
const [authorizationCode, state] = value.split('#');
if (!authorizationCode || !state) {
rl.prompt();
return;
}
if (!oauthService.handleManualAuthCodeInput({ authorizationCode, state })) {
console.log(chalk.red('Invalid authorization code or state.'));
rl.prompt();
}
});
return () => {
rl.close();
};
}
async function runOAuthLogin(options: {
loginWithClaudeAi: boolean;
setupToken: boolean;
loginHint?: string;
loginMethod?: string;
}): Promise<void> {
const oauthService = new OAuthService();
const stopManualCapture = startManualCodeCapture(oauthService);
try {
const tokens = await oauthService.startOAuthFlow(
async (manualUrl) => {
console.log(chalk.blue('Opening browser to sign in…'));
console.log(chalk.gray(`If the browser does not open, visit: ${manualUrl}`));
},
{
loginWithClaudeAi: options.loginWithClaudeAi,
loginHint: options.loginHint,
loginMethod: options.loginMethod,
inferenceOnly: options.setupToken,
expiresIn: options.setupToken ? 365 * 24 * 60 * 60 : undefined,
}
);
if (options.setupToken) {
await saveOAuthTokenOnly(tokens.accessToken);
} else {
await saveStoredTokens(tokens);
}
console.log(chalk.green('✅ OAuth login completed'));
console.log(chalk.gray(`Stored tokens at: ${getTokenFilePath()}`));
if (options.setupToken) {
console.log(chalk.gray('Created long-lived token using the setup-token flow.'));
console.log(chalk.yellow(tokens.accessToken));
}
} finally {
stopManualCapture();
oauthService.cleanup();
}
}
const program = new Command();
program
.name('not-claude-code-emulator')
.description('CLI for managing not-claude-code-emulator')
.version('1.0.0');
program
.command('install')
.description('Run the first-time installer and OAuth setup')
.action(async () => {
await ensureProjectStarConsent({
interactive: Boolean(process.stdin.isTTY && process.stdout.isTTY),
allowRetry: true,
});
console.log(chalk.blue('🔧 Running installer...'));
await runOAuthLogin({
loginWithClaudeAi: true,
setupToken: false,
});
console.log(chalk.green('✅ Installation completed'));
console.log(chalk.gray('Next step: bun run dev'));
});
program
.command('login')
.description('Sign in with the Claude OAuth flow and persist tokens')
.option('--console', 'Use Console login instead of Claude subscription login')
.option('--claudeai', 'Use Claude subscription login')
.option('--email <email>', 'Prefill the login email')
.option('--sso', 'Request SSO login')
.action(async (options) => {
if (options.console && options.claudeai) {
console.error('Error: --console and --claudeai cannot be used together.');
process.exit(1);
}
await runOAuthLogin({
loginWithClaudeAi: options.console ? false : true,
setupToken: false,
loginHint: options.email,
loginMethod: options.sso ? 'sso' : undefined,
});
});
program
.command('setup-token')
.description('Create a long-lived OAuth token and persist it')
.action(async () => {
await runOAuthLogin({
loginWithClaudeAi: true,
setupToken: true,
});
});
program
.command('logout')
.description('Clear persisted OAuth tokens')
.action(async () => {
await clearStoredTokens();
console.log(chalk.green('✅ Cleared persisted OAuth tokens'));
});
program
.command('verify-token')
.description('Verify an OAuth token')
.option('-t, --token <token>', 'OAuth token to verify')
.action(async (options) => {
const token = options.token ?? (await resolveOAuthToken());
if (!token) {
console.error(chalk.red('❌ Error: No token provided'));
console.log(chalk.yellow('Usage: cli verify-token --token <token>'));
console.log(chalk.yellow('Or set ANTHROPIC_OAUTH_TOKEN environment variable'));
process.exit(1);
}
console.log(chalk.blue('🔍 Verifying OAuth token...'));
console.log(chalk.gray(`Token: ${token.slice(0, 30)}...`));
const isValidFormat = validateOAuthToken(token);
console.log(chalk.gray(`Format valid: ${isValidFormat ? '✅' : '❌'}`));
if (!isValidFormat) {
console.error(chalk.red('❌ Token format is invalid'));
console.log(chalk.yellow('Expected format: sk-ant-oat01-...'));
process.exit(1);
}
console.log(chalk.blue('🌐 Testing token against Anthropic API...'));
const result = await verifyOAuthToken(token);
if (result.isValid) {
console.log(chalk.green('✅ Token is valid and working!'));
} else {
console.error(chalk.red('❌ Token verification failed'));
process.exit(1);
}
});
program
.command('test-request')
.description('Send a test request to verify the token works')
.option('-t, --token <token>', 'OAuth token to use')
.action(async (options) => {
const token = options.token ?? (await resolveOAuthToken());
if (!token) {
console.error(chalk.red('❌ Error: No token provided'));
process.exit(1);
}
console.log(chalk.blue('📤 Sending test request...'));
try {
const response = await callAnthropicApi(
'/v1/messages?beta=true',
{
model: 'claude-sonnet-4-5-20250929',
max_tokens: 100,
messages: [{ role: 'user', content: 'Say hello' }],
},
token
);
if (response.ok) {
const data = await response.json();
console.log(chalk.green('✅ Request successful!'));
console.log(chalk.gray('Response:'));
console.log(JSON.stringify(data, null, 2));
} else {
console.error(chalk.red(`❌ Request failed: ${response.status}`));
const error = await response.text();
console.error(chalk.red(error));
process.exit(1);
}
} catch (error) {
console.error(chalk.red('❌ Error sending request:'));
console.error(error instanceof Error ? error.message : String(error));
process.exit(1);
}
});
program
.command('start')
.description('Start the server')
.option('-p, --port <port>', 'Port to run on', '3000')
.option('-h, --host <host>', 'Host to bind to', 'localhost')
.action(async (options) => {
process.env.PORT = options.port;
process.env.HOST = options.host;
console.log(chalk.blue('🚀 Starting server...'));
await import('./index.js');
});
program
.command('health')
.description('Check server health')
.option('-u, --url <url>', 'Server URL', 'http://localhost:3000')
.action(async (options) => {
try {
const response = await fetch(`${options.url}/health`);
if (response.ok) {
const data = await response.json();
console.log(chalk.green('✅ Server is healthy'));
console.log(chalk.gray(JSON.stringify(data, null, 2)));
} else {
console.error(chalk.red(`❌ Server unhealthy: ${response.status}`));
process.exit(1);
}
} catch (error) {
console.error(chalk.red('❌ Cannot reach server:'));
console.error(error instanceof Error ? error.message : String(error));
process.exit(1);
}
});
program.hook('preAction', async (_thisCommand, actionCommand) => {
if (actionCommand.name() === 'install') {
return;
}
await ensureProjectStarConsent({
interactive: Boolean(process.stdin.isTTY && process.stdout.isTTY),
});
});
await program.parseAsync().catch((error) => {
console.error(chalk.red(`❌ ${error instanceof Error ? error.message : String(error)}`));
process.exit(1);
});