notioncode 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +10 -4
  2. package/agent-runtime-server/package-lock.json +4377 -0
  3. package/agent-runtime-server/package.json +36 -0
  4. package/agent-runtime-server/scripts/fix-node-pty.js +67 -0
  5. package/agent-runtime-server/server/agent-session-service.js +816 -0
  6. package/agent-runtime-server/server/claude-sdk.js +836 -0
  7. package/agent-runtime-server/server/cli.js +330 -0
  8. package/agent-runtime-server/server/constants/config.js +5 -0
  9. package/agent-runtime-server/server/cursor-cli.js +335 -0
  10. package/agent-runtime-server/server/database/db.js +653 -0
  11. package/agent-runtime-server/server/database/init.sql +99 -0
  12. package/agent-runtime-server/server/gemini-cli.js +460 -0
  13. package/agent-runtime-server/server/gemini-response-handler.js +79 -0
  14. package/agent-runtime-server/server/index.js +2569 -0
  15. package/agent-runtime-server/server/load-env.js +32 -0
  16. package/agent-runtime-server/server/middleware/auth.js +132 -0
  17. package/agent-runtime-server/server/openai-codex.js +512 -0
  18. package/agent-runtime-server/server/projects.js +2594 -0
  19. package/agent-runtime-server/server/providers/claude/adapter.js +278 -0
  20. package/agent-runtime-server/server/providers/codex/adapter.js +248 -0
  21. package/agent-runtime-server/server/providers/cursor/adapter.js +353 -0
  22. package/agent-runtime-server/server/providers/gemini/adapter.js +186 -0
  23. package/agent-runtime-server/server/providers/registry.js +44 -0
  24. package/agent-runtime-server/server/providers/types.js +119 -0
  25. package/agent-runtime-server/server/providers/utils.js +29 -0
  26. package/agent-runtime-server/server/routes/agent-sessions.js +238 -0
  27. package/agent-runtime-server/server/routes/agent.js +1244 -0
  28. package/agent-runtime-server/server/routes/auth.js +144 -0
  29. package/agent-runtime-server/server/routes/cli-auth.js +478 -0
  30. package/agent-runtime-server/server/routes/codex.js +329 -0
  31. package/agent-runtime-server/server/routes/commands.js +596 -0
  32. package/agent-runtime-server/server/routes/cursor.js +798 -0
  33. package/agent-runtime-server/server/routes/gemini.js +24 -0
  34. package/agent-runtime-server/server/routes/git.js +1508 -0
  35. package/agent-runtime-server/server/routes/mcp-utils.js +48 -0
  36. package/agent-runtime-server/server/routes/mcp.js +552 -0
  37. package/agent-runtime-server/server/routes/messages.js +61 -0
  38. package/agent-runtime-server/server/routes/plugins.js +307 -0
  39. package/agent-runtime-server/server/routes/projects.js +548 -0
  40. package/agent-runtime-server/server/routes/settings.js +276 -0
  41. package/agent-runtime-server/server/routes/taskmaster.js +1963 -0
  42. package/agent-runtime-server/server/routes/user.js +123 -0
  43. package/agent-runtime-server/server/services/notification-orchestrator.js +227 -0
  44. package/agent-runtime-server/server/services/vapid-keys.js +35 -0
  45. package/agent-runtime-server/server/sessionManager.js +226 -0
  46. package/agent-runtime-server/server/utils/commandParser.js +303 -0
  47. package/agent-runtime-server/server/utils/frontmatter.js +18 -0
  48. package/agent-runtime-server/server/utils/gitConfig.js +34 -0
  49. package/agent-runtime-server/server/utils/mcp-detector.js +198 -0
  50. package/agent-runtime-server/server/utils/plugin-loader.js +457 -0
  51. package/agent-runtime-server/server/utils/plugin-process-manager.js +184 -0
  52. package/agent-runtime-server/server/utils/taskmaster-websocket.js +129 -0
  53. package/agent-runtime-server/shared/modelConstants.js +12 -0
  54. package/agent-runtime-server/shared/modelConstants.test.js +34 -0
  55. package/agent-runtime-server/shared/networkHosts.js +22 -0
  56. package/agent-runtime-server/test_sdk.mjs +16 -0
  57. package/bin/bridges/darwin-x64/nocode-bridge +0 -0
  58. package/bin/{nocode-local.js → notioncode.js} +0 -0
  59. package/dist/assets/icon-CQtd7WEB.png +0 -0
  60. package/dist/assets/index-D_1ZrHDe.js +1 -0
  61. package/dist/assets/index-DhCWie1Z.css +1 -0
  62. package/dist/assets/index-DkGqIiwF.js +689 -0
  63. package/dist/index.html +46 -0
  64. package/dist/onboarding/step1_create.png +0 -0
  65. package/dist/onboarding/step2_capabilities.png +0 -0
  66. package/dist/onboarding/step2b_content_access.png +0 -0
  67. package/dist/onboarding/step2c_page_access.png +0 -0
  68. package/dist/onboarding/step3_token.png +0 -0
  69. package/dist/onboarding/step4_webhook.png +0 -0
  70. package/dist/onboarding/step6a_verify.png +0 -0
  71. package/dist/onboarding/step6b_copy_verify_token.png +0 -0
  72. package/dist/tinyfish-fish-only.png +0 -0
  73. package/lib/install.js +33 -2
  74. package/lib/start.js +157 -25
  75. package/package.json +7 -4
  76. package/src/shared/modelRegistry.d.ts +24 -0
  77. package/src/shared/modelRegistry.js +163 -0
@@ -0,0 +1,144 @@
1
+ import express from 'express';
2
+ import bcrypt from 'bcrypt';
3
+ import { userDb, db } from '../database/db.js';
4
+ import { generateToken, authenticateToken } from '../middleware/auth.js';
5
+ import { IS_PLATFORM } from '../constants/config.js';
6
+
7
+ const router = express.Router();
8
+
9
+ // Check auth status and setup requirements
10
+ router.get('/status', async (req, res) => {
11
+ try {
12
+ if (IS_PLATFORM) {
13
+ const user = userDb.ensurePlatformUser();
14
+ return res.json({
15
+ needsSetup: !user,
16
+ isAuthenticated: Boolean(user)
17
+ });
18
+ }
19
+
20
+ const hasUsers = await userDb.hasUsers();
21
+ res.json({
22
+ needsSetup: !hasUsers,
23
+ isAuthenticated: false // Will be overridden by frontend if token exists
24
+ });
25
+ } catch (error) {
26
+ console.error('Auth status error:', error);
27
+ res.status(500).json({ error: 'Internal server error' });
28
+ }
29
+ });
30
+
31
+ // User registration (setup) - only allowed if no users exist
32
+ router.post('/register', async (req, res) => {
33
+ try {
34
+ const { username, password } = req.body;
35
+
36
+ // Validate input
37
+ if (!username || !password) {
38
+ return res.status(400).json({ error: 'Username and password are required' });
39
+ }
40
+
41
+ if (username.length < 3 || password.length < 6) {
42
+ return res.status(400).json({ error: 'Username must be at least 3 characters, password at least 6 characters' });
43
+ }
44
+
45
+ // Use a transaction to prevent race conditions
46
+ db.prepare('BEGIN').run();
47
+ try {
48
+ // Check if users already exist (only allow one user)
49
+ const hasUsers = userDb.hasUsers();
50
+ if (hasUsers) {
51
+ db.prepare('ROLLBACK').run();
52
+ return res.status(403).json({ error: 'User already exists. This is a single-user system.' });
53
+ }
54
+
55
+ // Hash password
56
+ const saltRounds = 12;
57
+ const passwordHash = await bcrypt.hash(password, saltRounds);
58
+
59
+ // Create user
60
+ const user = userDb.createUser(username, passwordHash);
61
+
62
+ // Generate token
63
+ const token = generateToken(user);
64
+
65
+ db.prepare('COMMIT').run();
66
+
67
+ // Update last login (non-fatal, outside transaction)
68
+ userDb.updateLastLogin(user.id);
69
+
70
+ res.json({
71
+ success: true,
72
+ user: { id: user.id, username: user.username },
73
+ token
74
+ });
75
+ } catch (error) {
76
+ db.prepare('ROLLBACK').run();
77
+ throw error;
78
+ }
79
+
80
+ } catch (error) {
81
+ console.error('Registration error:', error);
82
+ if (error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
83
+ res.status(409).json({ error: 'Username already exists' });
84
+ } else {
85
+ res.status(500).json({ error: 'Internal server error' });
86
+ }
87
+ }
88
+ });
89
+
90
+ // User login
91
+ router.post('/login', async (req, res) => {
92
+ try {
93
+ const { username, password } = req.body;
94
+
95
+ // Validate input
96
+ if (!username || !password) {
97
+ return res.status(400).json({ error: 'Username and password are required' });
98
+ }
99
+
100
+ // Get user from database
101
+ const user = userDb.getUserByUsername(username);
102
+ if (!user) {
103
+ return res.status(401).json({ error: 'Invalid username or password' });
104
+ }
105
+
106
+ // Verify password
107
+ const isValidPassword = await bcrypt.compare(password, user.password_hash);
108
+ if (!isValidPassword) {
109
+ return res.status(401).json({ error: 'Invalid username or password' });
110
+ }
111
+
112
+ // Generate token
113
+ const token = generateToken(user);
114
+
115
+ // Update last login
116
+ userDb.updateLastLogin(user.id);
117
+
118
+ res.json({
119
+ success: true,
120
+ user: { id: user.id, username: user.username },
121
+ token
122
+ });
123
+
124
+ } catch (error) {
125
+ console.error('Login error:', error);
126
+ res.status(500).json({ error: 'Internal server error' });
127
+ }
128
+ });
129
+
130
+ // Get current user (protected route)
131
+ router.get('/user', authenticateToken, (req, res) => {
132
+ res.json({
133
+ user: req.user
134
+ });
135
+ });
136
+
137
+ // Logout (client-side token removal, but this endpoint can be used for logging)
138
+ router.post('/logout', authenticateToken, (req, res) => {
139
+ // In a simple JWT system, logout is mainly client-side
140
+ // This endpoint exists for consistency and potential future logging
141
+ res.json({ success: true, message: 'Logged out successfully' });
142
+ });
143
+
144
+ export default router;
@@ -0,0 +1,478 @@
1
+ import express from 'express';
2
+ import { spawn } from 'child_process';
3
+ import fs from 'fs/promises';
4
+ import path from 'path';
5
+ import os from 'os';
6
+
7
+ const router = express.Router();
8
+
9
+ router.get('/claude/status', async (req, res) => {
10
+ try {
11
+ const credentialsResult = await checkClaudeCredentials();
12
+
13
+ if (credentialsResult.authenticated) {
14
+ return res.json({
15
+ authenticated: true,
16
+ email: credentialsResult.email || 'Authenticated',
17
+ method: credentialsResult.method // 'api_key' or 'credentials_file'
18
+ });
19
+ }
20
+
21
+ return res.json({
22
+ authenticated: false,
23
+ email: null,
24
+ method: null,
25
+ error: credentialsResult.error || 'Not authenticated'
26
+ });
27
+
28
+ } catch (error) {
29
+ console.error('Error checking Claude auth status:', error);
30
+ res.status(500).json({
31
+ authenticated: false,
32
+ email: null,
33
+ method: null,
34
+ error: error.message
35
+ });
36
+ }
37
+ });
38
+
39
+ router.get('/cursor/status', async (req, res) => {
40
+ try {
41
+ const result = await checkCursorStatus();
42
+
43
+ res.json({
44
+ authenticated: result.authenticated,
45
+ email: result.email,
46
+ error: result.error
47
+ });
48
+
49
+ } catch (error) {
50
+ console.error('Error checking Cursor auth status:', error);
51
+ res.status(500).json({
52
+ authenticated: false,
53
+ email: null,
54
+ error: error.message
55
+ });
56
+ }
57
+ });
58
+
59
+ router.get('/codex/status', async (req, res) => {
60
+ try {
61
+ const result = await checkCodexCredentials();
62
+
63
+ res.json({
64
+ authenticated: result.authenticated,
65
+ email: result.email,
66
+ error: result.error
67
+ });
68
+
69
+ } catch (error) {
70
+ console.error('Error checking Codex auth status:', error);
71
+ res.status(500).json({
72
+ authenticated: false,
73
+ email: null,
74
+ error: error.message
75
+ });
76
+ }
77
+ });
78
+
79
+ router.get('/gemini/status', async (req, res) => {
80
+ try {
81
+ const result = await checkGeminiCredentials();
82
+
83
+ res.json({
84
+ authenticated: result.authenticated,
85
+ email: result.email,
86
+ error: result.error
87
+ });
88
+
89
+ } catch (error) {
90
+ console.error('Error checking Gemini auth status:', error);
91
+ res.status(500).json({
92
+ authenticated: false,
93
+ email: null,
94
+ error: error.message
95
+ });
96
+ }
97
+ });
98
+
99
+ async function loadClaudeSettingsEnv() {
100
+ try {
101
+ const settingsPath = path.join(os.homedir(), '.claude', 'settings.json');
102
+ const content = await fs.readFile(settingsPath, 'utf8');
103
+ const settings = JSON.parse(content);
104
+
105
+ if (settings?.env && typeof settings.env === 'object') {
106
+ return settings.env;
107
+ }
108
+ } catch (error) {
109
+ // Ignore missing or malformed settings and fall back to other auth sources.
110
+ }
111
+
112
+ return {};
113
+ }
114
+
115
+ function readOptionalString(value) {
116
+ return typeof value === 'string' && value.trim().length > 0 ? value.trim() : undefined;
117
+ }
118
+
119
+ async function checkGuiConfigForAnthropicKey(env = process.env) {
120
+ try {
121
+ const dataDir = readOptionalString(env.ABV_DATA_DIR) || path.join(os.homedir(), '.antigravity_tools');
122
+ const configPath = path.join(dataDir, 'gui_config.json');
123
+ const content = await fs.readFile(configPath, 'utf8');
124
+ const raw = JSON.parse(content);
125
+ const notionAutomation = raw?.notion_automation ?? raw?.notionAutomation ?? {};
126
+ const providers = notionAutomation?.agent_providers ?? notionAutomation?.agentProviders ?? [];
127
+
128
+ if (!Array.isArray(providers)) {
129
+ return { authenticated: false };
130
+ }
131
+
132
+ const anthropicProvider = providers.find((provider) => {
133
+ const id = readOptionalString(provider?.provider)?.toLowerCase();
134
+ const apiKey = readOptionalString(provider?.api_key ?? provider?.apiKey);
135
+ return (id === 'anthropic' || id === 'claude') && !!apiKey;
136
+ });
137
+
138
+ if (anthropicProvider) {
139
+ return {
140
+ authenticated: true,
141
+ email: 'API Key Auth (GUI Config)',
142
+ method: 'api_key'
143
+ };
144
+ }
145
+ } catch {
146
+ // Missing or malformed GUI config should not fail auth status checks.
147
+ }
148
+
149
+ return { authenticated: false };
150
+ }
151
+
152
+ /**
153
+ * Checks Claude authentication credentials using two methods with priority order:
154
+ *
155
+ * Priority 1: ANTHROPIC_API_KEY environment variable
156
+ * Priority 1b: ~/.claude/settings.json env values
157
+ * Priority 1c: GUI config (gui_config.json) Anthropic provider
158
+ * Priority 2: ~/.claude/.credentials.json OAuth tokens
159
+ *
160
+ * The Claude Agent SDK prioritizes environment variables over authenticated subscriptions.
161
+ * This matching behavior ensures consistency with how the SDK authenticates.
162
+ *
163
+ * References:
164
+ * - https://support.claude.com/en/articles/12304248-managing-api-key-environment-variables-in-claude-code
165
+ * "Claude Code prioritizes environment variable API keys over authenticated subscriptions"
166
+ * - https://platform.claude.com/docs/en/agent-sdk/overview
167
+ * SDK authentication documentation
168
+ *
169
+ * @returns {Promise<Object>} Authentication status with { authenticated, email, method }
170
+ * - authenticated: boolean indicating if valid credentials exist
171
+ * - email: user email or auth method identifier
172
+ * - method: 'api_key' for env var, 'credentials_file' for OAuth tokens
173
+ */
174
+ async function checkClaudeCredentials() {
175
+ // Priority 1: Check for ANTHROPIC_API_KEY environment variable
176
+ // The SDK checks this first and uses it if present, even if OAuth tokens exist.
177
+ // When set, API calls are charged via pay-as-you-go rates instead of subscription.
178
+ if (process.env.ANTHROPIC_API_KEY && process.env.ANTHROPIC_API_KEY.trim()) {
179
+ return {
180
+ authenticated: true,
181
+ email: 'API Key Auth',
182
+ method: 'api_key'
183
+ };
184
+ }
185
+
186
+ // Priority 1b: Check ~/.claude/settings.json env values.
187
+ // Claude Code can read proxy/auth values from settings.json even when the
188
+ // The agent runtime server process itself was not started with those env vars exported.
189
+ const settingsEnv = await loadClaudeSettingsEnv();
190
+
191
+ if (typeof settingsEnv.ANTHROPIC_API_KEY === 'string' && settingsEnv.ANTHROPIC_API_KEY.trim()) {
192
+ return {
193
+ authenticated: true,
194
+ email: 'API Key Auth',
195
+ method: 'api_key'
196
+ };
197
+ }
198
+
199
+ if (typeof settingsEnv.ANTHROPIC_AUTH_TOKEN === 'string' && settingsEnv.ANTHROPIC_AUTH_TOKEN.trim()) {
200
+ return {
201
+ authenticated: true,
202
+ email: 'Configured via settings.json',
203
+ method: 'api_key'
204
+ };
205
+ }
206
+
207
+ // Priority 1c: Check GUI-configured agent providers for an Anthropic API key.
208
+ const guiConfigResult = await checkGuiConfigForAnthropicKey();
209
+ if (guiConfigResult.authenticated) {
210
+ return guiConfigResult;
211
+ }
212
+
213
+ // Priority 2: Check ~/.claude/.credentials.json for OAuth tokens
214
+ // This is the standard authentication method used by Claude CLI after running
215
+ // 'claude /login' or 'claude setup-token' commands.
216
+ try {
217
+ const credPath = path.join(os.homedir(), '.claude', '.credentials.json');
218
+ const content = await fs.readFile(credPath, 'utf8');
219
+ const creds = JSON.parse(content);
220
+
221
+ const oauth = creds.claudeAiOauth;
222
+ if (oauth && oauth.accessToken) {
223
+ const isExpired = oauth.expiresAt && Date.now() >= oauth.expiresAt;
224
+
225
+ if (!isExpired) {
226
+ return {
227
+ authenticated: true,
228
+ email: creds.email || creds.user || null,
229
+ method: 'credentials_file'
230
+ };
231
+ }
232
+ }
233
+
234
+ return {
235
+ authenticated: false,
236
+ email: null,
237
+ method: null
238
+ };
239
+ } catch (error) {
240
+ return {
241
+ authenticated: false,
242
+ email: null,
243
+ method: null
244
+ };
245
+ }
246
+ }
247
+
248
+ function checkCursorStatus() {
249
+ return new Promise((resolve) => {
250
+ let processCompleted = false;
251
+
252
+ const timeout = setTimeout(() => {
253
+ if (!processCompleted) {
254
+ processCompleted = true;
255
+ if (childProcess) {
256
+ childProcess.kill();
257
+ }
258
+ resolve({
259
+ authenticated: false,
260
+ email: null,
261
+ error: 'Command timeout'
262
+ });
263
+ }
264
+ }, 5000);
265
+
266
+ let childProcess;
267
+ try {
268
+ childProcess = spawn('cursor-agent', ['status']);
269
+ } catch (err) {
270
+ clearTimeout(timeout);
271
+ processCompleted = true;
272
+ resolve({
273
+ authenticated: false,
274
+ email: null,
275
+ error: 'Cursor CLI not found or not installed'
276
+ });
277
+ return;
278
+ }
279
+
280
+ let stdout = '';
281
+ let stderr = '';
282
+
283
+ childProcess.stdout.on('data', (data) => {
284
+ stdout += data.toString();
285
+ });
286
+
287
+ childProcess.stderr.on('data', (data) => {
288
+ stderr += data.toString();
289
+ });
290
+
291
+ childProcess.on('close', (code) => {
292
+ if (processCompleted) return;
293
+ processCompleted = true;
294
+ clearTimeout(timeout);
295
+
296
+ if (code === 0) {
297
+ const emailMatch = stdout.match(/Logged in as ([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/i);
298
+
299
+ if (emailMatch) {
300
+ resolve({
301
+ authenticated: true,
302
+ email: emailMatch[1],
303
+ output: stdout
304
+ });
305
+ } else if (stdout.includes('Logged in')) {
306
+ resolve({
307
+ authenticated: true,
308
+ email: 'Logged in',
309
+ output: stdout
310
+ });
311
+ } else {
312
+ resolve({
313
+ authenticated: false,
314
+ email: null,
315
+ error: 'Not logged in'
316
+ });
317
+ }
318
+ } else {
319
+ resolve({
320
+ authenticated: false,
321
+ email: null,
322
+ error: stderr || 'Not logged in'
323
+ });
324
+ }
325
+ });
326
+
327
+ childProcess.on('error', (err) => {
328
+ if (processCompleted) return;
329
+ processCompleted = true;
330
+ clearTimeout(timeout);
331
+
332
+ resolve({
333
+ authenticated: false,
334
+ email: null,
335
+ error: 'Cursor CLI not found or not installed'
336
+ });
337
+ });
338
+ });
339
+ }
340
+
341
+ async function checkCodexCredentials() {
342
+ try {
343
+ const authPath = path.join(os.homedir(), '.codex', 'auth.json');
344
+ const content = await fs.readFile(authPath, 'utf8');
345
+ const auth = JSON.parse(content);
346
+
347
+ // Tokens are nested under 'tokens' key
348
+ const tokens = auth.tokens || {};
349
+
350
+ // Check for valid tokens (id_token or access_token)
351
+ if (tokens.id_token || tokens.access_token) {
352
+ // Try to extract email from id_token JWT payload
353
+ let email = 'Authenticated';
354
+ if (tokens.id_token) {
355
+ try {
356
+ // JWT is base64url encoded: header.payload.signature
357
+ const parts = tokens.id_token.split('.');
358
+ if (parts.length >= 2) {
359
+ // Decode the payload (second part)
360
+ const payload = JSON.parse(Buffer.from(parts[1], 'base64url').toString('utf8'));
361
+ email = payload.email || payload.user || 'Authenticated';
362
+ }
363
+ } catch {
364
+ // If JWT decoding fails, use fallback
365
+ email = 'Authenticated';
366
+ }
367
+ }
368
+
369
+ return {
370
+ authenticated: true,
371
+ email
372
+ };
373
+ }
374
+
375
+ // Also check for OPENAI_API_KEY as fallback auth method
376
+ if (auth.OPENAI_API_KEY) {
377
+ return {
378
+ authenticated: true,
379
+ email: 'API Key Auth'
380
+ };
381
+ }
382
+
383
+ return {
384
+ authenticated: false,
385
+ email: null,
386
+ error: 'No valid tokens found'
387
+ };
388
+ } catch (error) {
389
+ if (error.code === 'ENOENT') {
390
+ return {
391
+ authenticated: false,
392
+ email: null,
393
+ error: 'Codex not configured'
394
+ };
395
+ }
396
+ return {
397
+ authenticated: false,
398
+ email: null,
399
+ error: error.message
400
+ };
401
+ }
402
+ }
403
+
404
+ async function checkGeminiCredentials() {
405
+ if (process.env.GEMINI_API_KEY && process.env.GEMINI_API_KEY.trim()) {
406
+ return {
407
+ authenticated: true,
408
+ email: 'API Key Auth'
409
+ };
410
+ }
411
+
412
+ try {
413
+ const credsPath = path.join(os.homedir(), '.gemini', 'oauth_creds.json');
414
+ const content = await fs.readFile(credsPath, 'utf8');
415
+ const creds = JSON.parse(content);
416
+
417
+ if (creds.access_token) {
418
+ let email = 'OAuth Session';
419
+
420
+ try {
421
+ // Validate token against Google API
422
+ const tokenRes = await fetch(`https://oauth2.googleapis.com/tokeninfo?access_token=${creds.access_token}`);
423
+ if (tokenRes.ok) {
424
+ const tokenInfo = await tokenRes.json();
425
+ if (tokenInfo.email) {
426
+ email = tokenInfo.email;
427
+ }
428
+ } else if (!creds.refresh_token) {
429
+ // Token invalid and no refresh token available
430
+ return {
431
+ authenticated: false,
432
+ email: null,
433
+ error: 'Access token invalid and no refresh token found'
434
+ };
435
+ } else {
436
+ // Token might be expired but we have a refresh token, so CLI will refresh it
437
+ try {
438
+ const accPath = path.join(os.homedir(), '.gemini', 'google_accounts.json');
439
+ const accContent = await fs.readFile(accPath, 'utf8');
440
+ const accounts = JSON.parse(accContent);
441
+ if (accounts.active) {
442
+ email = accounts.active;
443
+ }
444
+ } catch (e) { }
445
+ }
446
+ } catch (e) {
447
+ // Network error, fallback to checking local accounts file
448
+ try {
449
+ const accPath = path.join(os.homedir(), '.gemini', 'google_accounts.json');
450
+ const accContent = await fs.readFile(accPath, 'utf8');
451
+ const accounts = JSON.parse(accContent);
452
+ if (accounts.active) {
453
+ email = accounts.active;
454
+ }
455
+ } catch (err) { }
456
+ }
457
+
458
+ return {
459
+ authenticated: true,
460
+ email: email
461
+ };
462
+ }
463
+
464
+ return {
465
+ authenticated: false,
466
+ email: null,
467
+ error: 'No valid tokens found in oauth_creds'
468
+ };
469
+ } catch (error) {
470
+ return {
471
+ authenticated: false,
472
+ email: null,
473
+ error: 'Gemini CLI not configured'
474
+ };
475
+ }
476
+ }
477
+
478
+ export default router;