@agent-relay/cloud 0.1.0
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.
- package/dist/api/admin.d.ts +8 -0
- package/dist/api/admin.d.ts.map +1 -0
- package/dist/api/admin.js +225 -0
- package/dist/api/admin.js.map +1 -0
- package/dist/api/auth.d.ts +20 -0
- package/dist/api/auth.d.ts.map +1 -0
- package/dist/api/auth.js +136 -0
- package/dist/api/auth.js.map +1 -0
- package/dist/api/billing.d.ts +7 -0
- package/dist/api/billing.d.ts.map +1 -0
- package/dist/api/billing.js +564 -0
- package/dist/api/billing.js.map +1 -0
- package/dist/api/cli-pty-runner.d.ts +53 -0
- package/dist/api/cli-pty-runner.d.ts.map +1 -0
- package/dist/api/cli-pty-runner.js +193 -0
- package/dist/api/cli-pty-runner.js.map +1 -0
- package/dist/api/codex-auth-helper.d.ts +21 -0
- package/dist/api/codex-auth-helper.d.ts.map +1 -0
- package/dist/api/codex-auth-helper.js +327 -0
- package/dist/api/codex-auth-helper.js.map +1 -0
- package/dist/api/consensus.d.ts +13 -0
- package/dist/api/consensus.d.ts.map +1 -0
- package/dist/api/consensus.js +261 -0
- package/dist/api/consensus.js.map +1 -0
- package/dist/api/coordinators.d.ts +8 -0
- package/dist/api/coordinators.d.ts.map +1 -0
- package/dist/api/coordinators.js +750 -0
- package/dist/api/coordinators.js.map +1 -0
- package/dist/api/daemons.d.ts +12 -0
- package/dist/api/daemons.d.ts.map +1 -0
- package/dist/api/daemons.js +535 -0
- package/dist/api/daemons.js.map +1 -0
- package/dist/api/generic-webhooks.d.ts +8 -0
- package/dist/api/generic-webhooks.d.ts.map +1 -0
- package/dist/api/generic-webhooks.js +129 -0
- package/dist/api/generic-webhooks.js.map +1 -0
- package/dist/api/git.d.ts +8 -0
- package/dist/api/git.d.ts.map +1 -0
- package/dist/api/git.js +269 -0
- package/dist/api/git.js.map +1 -0
- package/dist/api/github-app.d.ts +11 -0
- package/dist/api/github-app.d.ts.map +1 -0
- package/dist/api/github-app.js +223 -0
- package/dist/api/github-app.js.map +1 -0
- package/dist/api/middleware/planLimits.d.ts +43 -0
- package/dist/api/middleware/planLimits.d.ts.map +1 -0
- package/dist/api/middleware/planLimits.js +202 -0
- package/dist/api/middleware/planLimits.js.map +1 -0
- package/dist/api/monitoring.d.ts +11 -0
- package/dist/api/monitoring.d.ts.map +1 -0
- package/dist/api/monitoring.js +578 -0
- package/dist/api/monitoring.js.map +1 -0
- package/dist/api/nango-auth.d.ts +9 -0
- package/dist/api/nango-auth.d.ts.map +1 -0
- package/dist/api/nango-auth.js +674 -0
- package/dist/api/nango-auth.js.map +1 -0
- package/dist/api/onboarding.d.ts +15 -0
- package/dist/api/onboarding.d.ts.map +1 -0
- package/dist/api/onboarding.js +679 -0
- package/dist/api/onboarding.js.map +1 -0
- package/dist/api/policy.d.ts +8 -0
- package/dist/api/policy.d.ts.map +1 -0
- package/dist/api/policy.js +229 -0
- package/dist/api/policy.js.map +1 -0
- package/dist/api/provider-env.d.ts +14 -0
- package/dist/api/provider-env.d.ts.map +1 -0
- package/dist/api/provider-env.js +75 -0
- package/dist/api/provider-env.js.map +1 -0
- package/dist/api/providers.d.ts +7 -0
- package/dist/api/providers.d.ts.map +1 -0
- package/dist/api/providers.js +564 -0
- package/dist/api/providers.js.map +1 -0
- package/dist/api/repos.d.ts +8 -0
- package/dist/api/repos.d.ts.map +1 -0
- package/dist/api/repos.js +577 -0
- package/dist/api/repos.js.map +1 -0
- package/dist/api/sessions.d.ts +11 -0
- package/dist/api/sessions.d.ts.map +1 -0
- package/dist/api/sessions.js +302 -0
- package/dist/api/sessions.js.map +1 -0
- package/dist/api/teams.d.ts +7 -0
- package/dist/api/teams.d.ts.map +1 -0
- package/dist/api/teams.js +281 -0
- package/dist/api/teams.js.map +1 -0
- package/dist/api/test-helpers.d.ts +10 -0
- package/dist/api/test-helpers.d.ts.map +1 -0
- package/dist/api/test-helpers.js +745 -0
- package/dist/api/test-helpers.js.map +1 -0
- package/dist/api/usage.d.ts +7 -0
- package/dist/api/usage.d.ts.map +1 -0
- package/dist/api/usage.js +111 -0
- package/dist/api/usage.js.map +1 -0
- package/dist/api/webhooks.d.ts +8 -0
- package/dist/api/webhooks.d.ts.map +1 -0
- package/dist/api/webhooks.js +645 -0
- package/dist/api/webhooks.js.map +1 -0
- package/dist/api/workspaces.d.ts +25 -0
- package/dist/api/workspaces.d.ts.map +1 -0
- package/dist/api/workspaces.js +1799 -0
- package/dist/api/workspaces.js.map +1 -0
- package/dist/billing/index.d.ts +9 -0
- package/dist/billing/index.d.ts.map +1 -0
- package/dist/billing/index.js +9 -0
- package/dist/billing/index.js.map +1 -0
- package/dist/billing/plans.d.ts +39 -0
- package/dist/billing/plans.d.ts.map +1 -0
- package/dist/billing/plans.js +245 -0
- package/dist/billing/plans.js.map +1 -0
- package/dist/billing/service.d.ts +80 -0
- package/dist/billing/service.d.ts.map +1 -0
- package/dist/billing/service.js +388 -0
- package/dist/billing/service.js.map +1 -0
- package/dist/billing/types.d.ts +141 -0
- package/dist/billing/types.d.ts.map +1 -0
- package/dist/billing/types.js +7 -0
- package/dist/billing/types.js.map +1 -0
- package/dist/config.d.ts +5 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +5 -0
- package/dist/config.js.map +1 -0
- package/dist/db/bulk-ingest.d.ts +89 -0
- package/dist/db/bulk-ingest.d.ts.map +1 -0
- package/dist/db/bulk-ingest.js +268 -0
- package/dist/db/bulk-ingest.js.map +1 -0
- package/dist/db/drizzle.d.ts +256 -0
- package/dist/db/drizzle.d.ts.map +1 -0
- package/dist/db/drizzle.js +1286 -0
- package/dist/db/drizzle.js.map +1 -0
- package/dist/db/index.d.ts +55 -0
- package/dist/db/index.d.ts.map +1 -0
- package/dist/db/index.js +68 -0
- package/dist/db/index.js.map +1 -0
- package/dist/db/schema.d.ts +4873 -0
- package/dist/db/schema.d.ts.map +1 -0
- package/dist/db/schema.js +620 -0
- package/dist/db/schema.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/provisioner/index.d.ts +207 -0
- package/dist/provisioner/index.d.ts.map +1 -0
- package/dist/provisioner/index.js +2114 -0
- package/dist/provisioner/index.js.map +1 -0
- package/dist/server.d.ts +17 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +1924 -0
- package/dist/server.js.map +1 -0
- package/dist/services/auto-scaler.d.ts +152 -0
- package/dist/services/auto-scaler.d.ts.map +1 -0
- package/dist/services/auto-scaler.js +439 -0
- package/dist/services/auto-scaler.js.map +1 -0
- package/dist/services/capacity-manager.d.ts +148 -0
- package/dist/services/capacity-manager.d.ts.map +1 -0
- package/dist/services/capacity-manager.js +449 -0
- package/dist/services/capacity-manager.js.map +1 -0
- package/dist/services/ci-agent-spawner.d.ts +49 -0
- package/dist/services/ci-agent-spawner.d.ts.map +1 -0
- package/dist/services/ci-agent-spawner.js +373 -0
- package/dist/services/ci-agent-spawner.js.map +1 -0
- package/dist/services/cloud-message-bus.d.ts +28 -0
- package/dist/services/cloud-message-bus.d.ts.map +1 -0
- package/dist/services/cloud-message-bus.js +19 -0
- package/dist/services/cloud-message-bus.js.map +1 -0
- package/dist/services/compute-enforcement.d.ts +57 -0
- package/dist/services/compute-enforcement.d.ts.map +1 -0
- package/dist/services/compute-enforcement.js +175 -0
- package/dist/services/compute-enforcement.js.map +1 -0
- package/dist/services/coordinator.d.ts +62 -0
- package/dist/services/coordinator.d.ts.map +1 -0
- package/dist/services/coordinator.js +389 -0
- package/dist/services/coordinator.js.map +1 -0
- package/dist/services/index.d.ts +17 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +25 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/intro-expiration.d.ts +60 -0
- package/dist/services/intro-expiration.d.ts.map +1 -0
- package/dist/services/intro-expiration.js +252 -0
- package/dist/services/intro-expiration.js.map +1 -0
- package/dist/services/mention-handler.d.ts +65 -0
- package/dist/services/mention-handler.d.ts.map +1 -0
- package/dist/services/mention-handler.js +405 -0
- package/dist/services/mention-handler.js.map +1 -0
- package/dist/services/nango.d.ts +201 -0
- package/dist/services/nango.d.ts.map +1 -0
- package/dist/services/nango.js +392 -0
- package/dist/services/nango.js.map +1 -0
- package/dist/services/persistence.d.ts +131 -0
- package/dist/services/persistence.d.ts.map +1 -0
- package/dist/services/persistence.js +200 -0
- package/dist/services/persistence.js.map +1 -0
- package/dist/services/planLimits.d.ts +147 -0
- package/dist/services/planLimits.d.ts.map +1 -0
- package/dist/services/planLimits.js +335 -0
- package/dist/services/planLimits.js.map +1 -0
- package/dist/services/presence-registry.d.ts +56 -0
- package/dist/services/presence-registry.d.ts.map +1 -0
- package/dist/services/presence-registry.js +91 -0
- package/dist/services/presence-registry.js.map +1 -0
- package/dist/services/scaling-orchestrator.d.ts +159 -0
- package/dist/services/scaling-orchestrator.d.ts.map +1 -0
- package/dist/services/scaling-orchestrator.js +502 -0
- package/dist/services/scaling-orchestrator.js.map +1 -0
- package/dist/services/scaling-policy.d.ts +121 -0
- package/dist/services/scaling-policy.d.ts.map +1 -0
- package/dist/services/scaling-policy.js +415 -0
- package/dist/services/scaling-policy.js.map +1 -0
- package/dist/services/ssh-security.d.ts +31 -0
- package/dist/services/ssh-security.d.ts.map +1 -0
- package/dist/services/ssh-security.js +63 -0
- package/dist/services/ssh-security.js.map +1 -0
- package/dist/services/workspace-keepalive.d.ts +76 -0
- package/dist/services/workspace-keepalive.d.ts.map +1 -0
- package/dist/services/workspace-keepalive.js +234 -0
- package/dist/services/workspace-keepalive.js.map +1 -0
- package/dist/shims/consensus.d.ts +23 -0
- package/dist/shims/consensus.d.ts.map +1 -0
- package/dist/shims/consensus.js +5 -0
- package/dist/shims/consensus.js.map +1 -0
- package/dist/webhooks/index.d.ts +24 -0
- package/dist/webhooks/index.d.ts.map +1 -0
- package/dist/webhooks/index.js +29 -0
- package/dist/webhooks/index.js.map +1 -0
- package/dist/webhooks/parsers/github.d.ts +8 -0
- package/dist/webhooks/parsers/github.d.ts.map +1 -0
- package/dist/webhooks/parsers/github.js +234 -0
- package/dist/webhooks/parsers/github.js.map +1 -0
- package/dist/webhooks/parsers/index.d.ts +23 -0
- package/dist/webhooks/parsers/index.d.ts.map +1 -0
- package/dist/webhooks/parsers/index.js +30 -0
- package/dist/webhooks/parsers/index.js.map +1 -0
- package/dist/webhooks/parsers/linear.d.ts +9 -0
- package/dist/webhooks/parsers/linear.d.ts.map +1 -0
- package/dist/webhooks/parsers/linear.js +258 -0
- package/dist/webhooks/parsers/linear.js.map +1 -0
- package/dist/webhooks/parsers/slack.d.ts +9 -0
- package/dist/webhooks/parsers/slack.d.ts.map +1 -0
- package/dist/webhooks/parsers/slack.js +214 -0
- package/dist/webhooks/parsers/slack.js.map +1 -0
- package/dist/webhooks/responders/github.d.ts +8 -0
- package/dist/webhooks/responders/github.d.ts.map +1 -0
- package/dist/webhooks/responders/github.js +73 -0
- package/dist/webhooks/responders/github.js.map +1 -0
- package/dist/webhooks/responders/index.d.ts +23 -0
- package/dist/webhooks/responders/index.d.ts.map +1 -0
- package/dist/webhooks/responders/index.js +30 -0
- package/dist/webhooks/responders/index.js.map +1 -0
- package/dist/webhooks/responders/linear.d.ts +9 -0
- package/dist/webhooks/responders/linear.d.ts.map +1 -0
- package/dist/webhooks/responders/linear.js +149 -0
- package/dist/webhooks/responders/linear.js.map +1 -0
- package/dist/webhooks/responders/slack.d.ts +20 -0
- package/dist/webhooks/responders/slack.d.ts.map +1 -0
- package/dist/webhooks/responders/slack.js +178 -0
- package/dist/webhooks/responders/slack.js.map +1 -0
- package/dist/webhooks/router.d.ts +25 -0
- package/dist/webhooks/router.d.ts.map +1 -0
- package/dist/webhooks/router.js +504 -0
- package/dist/webhooks/router.js.map +1 -0
- package/dist/webhooks/rules-engine.d.ts +24 -0
- package/dist/webhooks/rules-engine.d.ts.map +1 -0
- package/dist/webhooks/rules-engine.js +287 -0
- package/dist/webhooks/rules-engine.js.map +1 -0
- package/dist/webhooks/types.d.ts +186 -0
- package/dist/webhooks/types.d.ts.map +1 -0
- package/dist/webhooks/types.js +8 -0
- package/dist/webhooks/types.js.map +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Onboarding API Routes
|
|
3
|
+
*
|
|
4
|
+
* Handles CLI proxy authentication for Claude Code and other providers.
|
|
5
|
+
* Spawns CLI tools via PTY to get auth URLs, captures tokens.
|
|
6
|
+
*
|
|
7
|
+
* Uses relay-pty binary for PTY emulation, which provides:
|
|
8
|
+
* 1. TTY detection for CLIs that behave differently in non-TTY
|
|
9
|
+
* 2. Proper interactive OAuth flow handling
|
|
10
|
+
* 3. Better Node.js version compatibility (no native compilation)
|
|
11
|
+
*/
|
|
12
|
+
import { Router } from 'express';
|
|
13
|
+
import * as crypto from 'crypto';
|
|
14
|
+
import { requireAuth } from './auth.js';
|
|
15
|
+
import { db } from '../db/index.js';
|
|
16
|
+
import { setProviderApiKeyEnv } from './provider-env.js';
|
|
17
|
+
// Import for local use
|
|
18
|
+
import { CLI_AUTH_CONFIG, runCLIAuthViaPTY, stripAnsiCodes, matchesSuccessPattern, findMatchingPrompt, validateProviderConfig, validateAllProviderConfigs, getSupportedProviders, } from './cli-pty-runner.js';
|
|
19
|
+
// Re-export from shared module for backward compatibility
|
|
20
|
+
export { CLI_AUTH_CONFIG, runCLIAuthViaPTY, stripAnsiCodes, matchesSuccessPattern, findMatchingPrompt, validateProviderConfig, validateAllProviderConfigs, getSupportedProviders, };
|
|
21
|
+
export const onboardingRouter = Router();
|
|
22
|
+
// Debug: log all requests to this router
|
|
23
|
+
onboardingRouter.use((req, res, next) => {
|
|
24
|
+
console.log(`[onboarding] ${req.method} ${req.path} - body:`, JSON.stringify(req.body));
|
|
25
|
+
next();
|
|
26
|
+
});
|
|
27
|
+
// All routes require authentication
|
|
28
|
+
onboardingRouter.use(requireAuth);
|
|
29
|
+
const activeSessions = new Map();
|
|
30
|
+
// Clean up old sessions periodically
|
|
31
|
+
setInterval(() => {
|
|
32
|
+
const now = Date.now();
|
|
33
|
+
activeSessions.forEach((session, id) => {
|
|
34
|
+
// Remove sessions older than 10 minutes
|
|
35
|
+
if (now - session.createdAt.getTime() > 10 * 60 * 1000) {
|
|
36
|
+
if (session.process) {
|
|
37
|
+
try {
|
|
38
|
+
session.process.kill();
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Process may already be dead
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
activeSessions.delete(id);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}, 60000);
|
|
48
|
+
/**
|
|
49
|
+
* POST /api/onboarding/cli/:provider/start
|
|
50
|
+
* Start CLI-based auth - forwards to workspace daemon if available
|
|
51
|
+
*
|
|
52
|
+
* CLI auth requires a running workspace since CLI tools are installed there.
|
|
53
|
+
* For onboarding without a workspace, users should use the API key flow.
|
|
54
|
+
*/
|
|
55
|
+
onboardingRouter.post('/cli/:provider/start', async (req, res) => {
|
|
56
|
+
console.log('[onboarding] Route handler entered! provider:', req.params.provider);
|
|
57
|
+
const provider = req.params.provider;
|
|
58
|
+
const userId = req.session.userId;
|
|
59
|
+
const { workspaceId, useDeviceFlow: requestedDeviceFlow } = req.body; // Optional: specific workspace, device flow option
|
|
60
|
+
// Device flow is only used if explicitly requested by the client
|
|
61
|
+
// Standard flow: user runs `codex-auth` CLI locally to capture OAuth callback and forward to cloud
|
|
62
|
+
const config = CLI_AUTH_CONFIG[provider];
|
|
63
|
+
const useDeviceFlow = requestedDeviceFlow ?? false;
|
|
64
|
+
console.log('[onboarding] userId:', userId, 'workspaceId:', workspaceId, 'useDeviceFlow:', useDeviceFlow);
|
|
65
|
+
if (!config) {
|
|
66
|
+
return res.status(400).json({
|
|
67
|
+
error: 'Provider not supported for CLI auth',
|
|
68
|
+
supportedProviders: Object.keys(CLI_AUTH_CONFIG),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
// Find a running workspace to use for CLI auth
|
|
73
|
+
let workspace;
|
|
74
|
+
if (workspaceId) {
|
|
75
|
+
workspace = await db.workspaces.findById(workspaceId);
|
|
76
|
+
if (!workspace) {
|
|
77
|
+
console.log(`[onboarding] Workspace ${workspaceId} not found in database`);
|
|
78
|
+
return res.status(404).json({ error: 'Workspace not found' });
|
|
79
|
+
}
|
|
80
|
+
if (workspace.userId !== userId) {
|
|
81
|
+
console.log(`[onboarding] Workspace ${workspaceId} belongs to ${workspace.userId}, not ${userId}`);
|
|
82
|
+
return res.status(404).json({ error: 'Workspace not found' });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
// Find any running workspace for this user
|
|
87
|
+
const workspaces = await db.workspaces.findByUserId(userId);
|
|
88
|
+
workspace = workspaces.find(w => w.status === 'running' && w.publicUrl);
|
|
89
|
+
}
|
|
90
|
+
if (!workspace || workspace.status !== 'running' || !workspace.publicUrl) {
|
|
91
|
+
return res.status(400).json({
|
|
92
|
+
error: 'CLI auth requires a running workspace',
|
|
93
|
+
code: 'NO_RUNNING_WORKSPACE',
|
|
94
|
+
message: 'Please start a workspace first, or use the API key input to connect your provider.',
|
|
95
|
+
hint: 'You can create a workspace without providers and connect them afterward using CLI auth.',
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
// Forward auth request to workspace daemon
|
|
99
|
+
// When running in Docker, localhost refers to the container, not the host
|
|
100
|
+
// Use host.docker.internal on Mac/Windows to reach the host machine
|
|
101
|
+
// When running on Fly.io, use internal networking (.internal) instead of public DNS
|
|
102
|
+
let workspaceUrl = workspace.publicUrl.replace(/\/$/, '');
|
|
103
|
+
// Detect Fly.io by checking FLY_APP_NAME env var
|
|
104
|
+
const isOnFly = !!process.env.FLY_APP_NAME;
|
|
105
|
+
// Detect Docker by checking for /.dockerenv file or RUNNING_IN_DOCKER env var
|
|
106
|
+
const isInDocker = process.env.RUNNING_IN_DOCKER === 'true' ||
|
|
107
|
+
await import('fs').then(fs => fs.existsSync('/.dockerenv')).catch(() => false);
|
|
108
|
+
console.log('[onboarding] isOnFly:', isOnFly, 'isInDocker:', isInDocker);
|
|
109
|
+
if (isOnFly && workspaceUrl.includes('.fly.dev')) {
|
|
110
|
+
// Use Fly.io internal networking for server-to-server communication
|
|
111
|
+
// ar-583f273b.fly.dev -> http://ar-583f273b.internal:3888
|
|
112
|
+
// .internal uses IPv6 and works by default for apps in the same org
|
|
113
|
+
const appName = workspaceUrl.match(/https?:\/\/([^.]+)\.fly\.dev/)?.[1];
|
|
114
|
+
if (appName) {
|
|
115
|
+
workspaceUrl = `http://${appName}.internal:3888`;
|
|
116
|
+
console.log('[onboarding] Using Fly internal network:', workspaceUrl);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else if (isInDocker && workspaceUrl.includes('localhost')) {
|
|
120
|
+
workspaceUrl = workspaceUrl.replace('localhost', 'host.docker.internal');
|
|
121
|
+
console.log('[onboarding] Translated localhost to host.docker.internal');
|
|
122
|
+
}
|
|
123
|
+
const targetUrl = `${workspaceUrl}/auth/cli/${provider}/start`;
|
|
124
|
+
console.log('[onboarding] Forwarding to workspace daemon:', targetUrl);
|
|
125
|
+
// Pass userId to enable per-user credential storage in multi-user workspaces
|
|
126
|
+
const authResponse = await fetch(targetUrl, {
|
|
127
|
+
method: 'POST',
|
|
128
|
+
headers: { 'Content-Type': 'application/json' },
|
|
129
|
+
body: JSON.stringify({ useDeviceFlow, userId }),
|
|
130
|
+
});
|
|
131
|
+
console.log('[onboarding] Workspace daemon response:', authResponse.status);
|
|
132
|
+
if (!authResponse.ok) {
|
|
133
|
+
const errorData = await authResponse.json().catch(() => ({}));
|
|
134
|
+
console.log('[onboarding] Workspace daemon error:', errorData);
|
|
135
|
+
return res.status(authResponse.status).json({
|
|
136
|
+
error: errorData.error || 'Failed to start CLI auth in workspace',
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const workspaceSession = await authResponse.json();
|
|
140
|
+
// Create cloud session to track this
|
|
141
|
+
const sessionId = crypto.randomUUID();
|
|
142
|
+
const session = {
|
|
143
|
+
userId,
|
|
144
|
+
provider,
|
|
145
|
+
status: workspaceSession.status || 'starting',
|
|
146
|
+
authUrl: workspaceSession.authUrl,
|
|
147
|
+
createdAt: new Date(),
|
|
148
|
+
output: '',
|
|
149
|
+
// Store workspace info for status polling and auth code forwarding
|
|
150
|
+
workspaceId: workspace.id,
|
|
151
|
+
workspaceUrl,
|
|
152
|
+
workspaceSessionId: workspaceSession.sessionId,
|
|
153
|
+
};
|
|
154
|
+
activeSessions.set(sessionId, session);
|
|
155
|
+
console.log('[onboarding] Session created:', { sessionId, workspaceUrl, workspaceSessionId: workspaceSession.sessionId });
|
|
156
|
+
res.json({
|
|
157
|
+
sessionId,
|
|
158
|
+
status: session.status,
|
|
159
|
+
authUrl: session.authUrl,
|
|
160
|
+
workspaceId: workspace.id,
|
|
161
|
+
useDeviceFlow, // Tell dashboard whether device flow is being used (no CLI helper needed)
|
|
162
|
+
message: session.authUrl ? 'Open the auth URL to complete login' : 'Auth session starting, poll for status',
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
console.error(`Error starting CLI auth for ${provider}:`, error);
|
|
167
|
+
res.status(500).json({ error: 'Failed to start CLI authentication' });
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
/**
|
|
171
|
+
* GET /api/onboarding/cli/:provider/status/:sessionId
|
|
172
|
+
* Check status of CLI auth session - forwards to workspace daemon
|
|
173
|
+
*/
|
|
174
|
+
onboardingRouter.get('/cli/:provider/status/:sessionId', async (req, res) => {
|
|
175
|
+
const provider = req.params.provider;
|
|
176
|
+
const sessionId = req.params.sessionId;
|
|
177
|
+
const userId = req.session.userId;
|
|
178
|
+
const session = activeSessions.get(sessionId);
|
|
179
|
+
if (!session) {
|
|
180
|
+
return res.status(404).json({ error: 'Session not found or expired' });
|
|
181
|
+
}
|
|
182
|
+
if (session.userId !== userId) {
|
|
183
|
+
return res.status(403).json({ error: 'Unauthorized' });
|
|
184
|
+
}
|
|
185
|
+
// If we have workspace info, poll the workspace for status
|
|
186
|
+
if (session.workspaceUrl && session.workspaceSessionId) {
|
|
187
|
+
try {
|
|
188
|
+
const statusResponse = await fetch(`${session.workspaceUrl}/auth/cli/${provider}/status/${session.workspaceSessionId}`);
|
|
189
|
+
if (statusResponse.ok) {
|
|
190
|
+
const workspaceStatus = await statusResponse.json();
|
|
191
|
+
// Update local session with workspace status
|
|
192
|
+
session.status = workspaceStatus.status || session.status;
|
|
193
|
+
session.authUrl = workspaceStatus.authUrl || session.authUrl;
|
|
194
|
+
session.error = workspaceStatus.error;
|
|
195
|
+
session.errorHint = workspaceStatus.errorHint;
|
|
196
|
+
session.recoverable = workspaceStatus.recoverable;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
catch (err) {
|
|
200
|
+
console.error('[onboarding] Failed to poll workspace status:', err);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
res.json({
|
|
204
|
+
status: session.status,
|
|
205
|
+
authUrl: session.authUrl,
|
|
206
|
+
error: session.error,
|
|
207
|
+
errorHint: session.errorHint,
|
|
208
|
+
recoverable: session.recoverable,
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
/**
|
|
212
|
+
* POST /api/onboarding/cli/:provider/complete/:sessionId
|
|
213
|
+
* Mark CLI auth as complete and store credentials
|
|
214
|
+
*
|
|
215
|
+
* Handles two modes:
|
|
216
|
+
* 1. Workspace delegation: Forwards to workspace daemon to complete auth, then fetches credentials
|
|
217
|
+
* 2. Direct: Uses token from body or session
|
|
218
|
+
*/
|
|
219
|
+
onboardingRouter.post('/cli/:provider/complete/:sessionId', async (req, res) => {
|
|
220
|
+
const provider = req.params.provider;
|
|
221
|
+
const sessionId = req.params.sessionId;
|
|
222
|
+
const userId = req.session.userId;
|
|
223
|
+
const { token, authCode } = req.body; // token for direct mode, authCode for Codex redirect
|
|
224
|
+
console.log(`[onboarding] POST /cli/${provider}/complete/${sessionId} - token: ${token ? 'provided' : 'none'}, authCode: ${authCode ? 'provided' : 'none'}`);
|
|
225
|
+
const session = activeSessions.get(sessionId);
|
|
226
|
+
if (!session) {
|
|
227
|
+
return res.status(404).json({ error: 'Session not found or expired' });
|
|
228
|
+
}
|
|
229
|
+
if (session.userId !== userId) {
|
|
230
|
+
return res.status(403).json({ error: 'Unauthorized' });
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
let accessToken = token || session.token;
|
|
234
|
+
let refreshToken = session.refreshToken;
|
|
235
|
+
let _tokenExpiresAt = session.tokenExpiresAt;
|
|
236
|
+
// If using workspace delegation, forward complete request first
|
|
237
|
+
if (session.workspaceUrl && session.workspaceSessionId) {
|
|
238
|
+
// Forward authCode to workspace if provided (for Codex-style redirects)
|
|
239
|
+
if (authCode) {
|
|
240
|
+
const backendProviderId = provider === 'anthropic' ? 'anthropic' : provider;
|
|
241
|
+
const targetUrl = `${session.workspaceUrl}/auth/cli/${backendProviderId}/complete/${session.workspaceSessionId}`;
|
|
242
|
+
console.log('[onboarding] Forwarding complete request to workspace:', targetUrl);
|
|
243
|
+
const completeResponse = await fetch(targetUrl, {
|
|
244
|
+
method: 'POST',
|
|
245
|
+
headers: { 'Content-Type': 'application/json' },
|
|
246
|
+
body: JSON.stringify({ authCode }),
|
|
247
|
+
});
|
|
248
|
+
if (!completeResponse.ok) {
|
|
249
|
+
const errorData = await completeResponse.json().catch(() => ({}));
|
|
250
|
+
return res.status(completeResponse.status).json({
|
|
251
|
+
error: errorData.error || 'Failed to complete authentication in workspace',
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
session.status = 'success';
|
|
255
|
+
}
|
|
256
|
+
// Fetch credentials from workspace with retry
|
|
257
|
+
// Credentials may not be immediately available after OAuth completes
|
|
258
|
+
if (!accessToken) {
|
|
259
|
+
const MAX_CREDS_RETRIES = 5;
|
|
260
|
+
const CREDS_RETRY_DELAY = 1000; // 1 second between retries
|
|
261
|
+
for (let attempt = 1; attempt <= MAX_CREDS_RETRIES; attempt++) {
|
|
262
|
+
try {
|
|
263
|
+
console.log(`[onboarding] Fetching credentials from workspace (attempt ${attempt}/${MAX_CREDS_RETRIES})`);
|
|
264
|
+
const credsResponse = await fetch(`${session.workspaceUrl}/auth/cli/${provider}/creds/${session.workspaceSessionId}`);
|
|
265
|
+
if (credsResponse.ok) {
|
|
266
|
+
const creds = await credsResponse.json();
|
|
267
|
+
accessToken = creds.token;
|
|
268
|
+
refreshToken = creds.refreshToken;
|
|
269
|
+
if (creds.tokenExpiresAt) {
|
|
270
|
+
_tokenExpiresAt = new Date(creds.tokenExpiresAt);
|
|
271
|
+
}
|
|
272
|
+
console.log('[onboarding] Fetched credentials from workspace:', {
|
|
273
|
+
hasToken: !!accessToken,
|
|
274
|
+
hasRefreshToken: !!refreshToken,
|
|
275
|
+
attempt,
|
|
276
|
+
});
|
|
277
|
+
break; // Success, exit retry loop
|
|
278
|
+
}
|
|
279
|
+
// Check if it's an error state (not just "not ready yet")
|
|
280
|
+
const errorBody = await credsResponse.json().catch(() => ({}));
|
|
281
|
+
if (errorBody.status === 'error') {
|
|
282
|
+
// Auth failed, don't retry
|
|
283
|
+
console.error('[onboarding] Auth failed in workspace:', errorBody);
|
|
284
|
+
return res.status(400).json({
|
|
285
|
+
error: errorBody.error || 'Authentication failed',
|
|
286
|
+
errorHint: errorBody.errorHint,
|
|
287
|
+
recoverable: errorBody.recoverable,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
// If not ready yet and we have more retries, wait and try again
|
|
291
|
+
if (attempt < MAX_CREDS_RETRIES) {
|
|
292
|
+
console.log(`[onboarding] Credentials not ready yet, retrying in ${CREDS_RETRY_DELAY}ms...`);
|
|
293
|
+
await new Promise(resolve => setTimeout(resolve, CREDS_RETRY_DELAY));
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
catch (err) {
|
|
297
|
+
console.error(`[onboarding] Failed to get credentials from workspace (attempt ${attempt}):`, err);
|
|
298
|
+
if (attempt < MAX_CREDS_RETRIES) {
|
|
299
|
+
await new Promise(resolve => setTimeout(resolve, CREDS_RETRY_DELAY));
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (!accessToken) {
|
|
306
|
+
return res.status(400).json({
|
|
307
|
+
error: 'No token found. Please complete authentication or paste your token.',
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
// Mark provider as connected (tokens are not stored centrally - CLI tools
|
|
311
|
+
// authenticate directly on workspace instances)
|
|
312
|
+
await db.credentials.upsert({
|
|
313
|
+
userId,
|
|
314
|
+
workspaceId: session.workspaceId,
|
|
315
|
+
provider,
|
|
316
|
+
scopes: getProviderScopes(provider),
|
|
317
|
+
});
|
|
318
|
+
// Clean up session
|
|
319
|
+
activeSessions.delete(sessionId);
|
|
320
|
+
res.json({
|
|
321
|
+
success: true,
|
|
322
|
+
message: `${provider} connected successfully`,
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
catch (error) {
|
|
326
|
+
console.error(`Error completing CLI auth for ${provider}:`, error);
|
|
327
|
+
res.status(500).json({ error: 'Failed to complete authentication' });
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
/**
|
|
331
|
+
* POST /api/onboarding/cli/:provider/code/:sessionId
|
|
332
|
+
* Submit auth code to the CLI PTY session
|
|
333
|
+
* Used when OAuth returns a code that must be pasted into the CLI
|
|
334
|
+
*/
|
|
335
|
+
onboardingRouter.post('/cli/:provider/code/:sessionId', async (req, res) => {
|
|
336
|
+
const provider = req.params.provider;
|
|
337
|
+
const sessionId = req.params.sessionId;
|
|
338
|
+
const userId = req.session.userId;
|
|
339
|
+
const { code, state } = req.body; // state is optional, used for Codex OAuth
|
|
340
|
+
console.log('[onboarding] Auth code submission request:', { provider, sessionId, codeLength: code?.length, hasState: !!state });
|
|
341
|
+
if (!code || typeof code !== 'string') {
|
|
342
|
+
return res.status(400).json({ error: 'Auth code is required' });
|
|
343
|
+
}
|
|
344
|
+
const session = activeSessions.get(sessionId);
|
|
345
|
+
if (!session) {
|
|
346
|
+
console.log('[onboarding] Session not found:', { sessionId, activeSessions: Array.from(activeSessions.keys()) });
|
|
347
|
+
return res.status(404).json({ error: 'Session not found or expired. Please try connecting again.' });
|
|
348
|
+
}
|
|
349
|
+
if (session.userId !== userId) {
|
|
350
|
+
return res.status(403).json({ error: 'Unauthorized' });
|
|
351
|
+
}
|
|
352
|
+
console.log('[onboarding] Session found:', {
|
|
353
|
+
sessionId,
|
|
354
|
+
workspaceUrl: session.workspaceUrl,
|
|
355
|
+
workspaceSessionId: session.workspaceSessionId,
|
|
356
|
+
status: session.status,
|
|
357
|
+
});
|
|
358
|
+
// Forward to workspace daemon
|
|
359
|
+
if (session.workspaceUrl && session.workspaceSessionId) {
|
|
360
|
+
try {
|
|
361
|
+
const targetUrl = `${session.workspaceUrl}/auth/cli/${provider}/code/${session.workspaceSessionId}`;
|
|
362
|
+
console.log('[onboarding] Forwarding auth code to workspace:', targetUrl);
|
|
363
|
+
const codeResponse = await fetch(targetUrl, {
|
|
364
|
+
method: 'POST',
|
|
365
|
+
headers: { 'Content-Type': 'application/json' },
|
|
366
|
+
body: JSON.stringify({ code, state }), // Forward state for Codex CSRF validation
|
|
367
|
+
});
|
|
368
|
+
console.log('[onboarding] Workspace response:', { status: codeResponse.status });
|
|
369
|
+
if (codeResponse.ok) {
|
|
370
|
+
return res.json({ success: true, message: 'Auth code submitted' });
|
|
371
|
+
}
|
|
372
|
+
const errorData = await codeResponse.json().catch(() => ({}));
|
|
373
|
+
console.log('[onboarding] Workspace error:', errorData);
|
|
374
|
+
// Provide more helpful error message
|
|
375
|
+
const needsRestart = errorData.needsRestart;
|
|
376
|
+
if (codeResponse.status === 404 || codeResponse.status === 400) {
|
|
377
|
+
return res.status(400).json({
|
|
378
|
+
error: errorData.error || 'Auth session expired in workspace. The CLI process may have timed out. Please try connecting again.',
|
|
379
|
+
needsRestart: needsRestart ?? true,
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
return res.status(codeResponse.status).json({
|
|
383
|
+
error: errorData.error || 'Failed to submit auth code to workspace',
|
|
384
|
+
needsRestart,
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
catch (err) {
|
|
388
|
+
console.error('[onboarding] Failed to submit auth code to workspace:', err);
|
|
389
|
+
return res.status(500).json({
|
|
390
|
+
error: 'Failed to reach workspace. Please ensure your workspace is running and try again.',
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
console.log('[onboarding] No workspace session info available');
|
|
395
|
+
return res.status(400).json({
|
|
396
|
+
error: 'No workspace session available. This can happen if the workspace was restarted. Please try connecting again.',
|
|
397
|
+
});
|
|
398
|
+
});
|
|
399
|
+
// Note: POST /cli/:provider/complete/:sessionId handler is defined above (lines 269-368)
|
|
400
|
+
// It handles both direct token storage and workspace delegation with authCode forwarding
|
|
401
|
+
/**
|
|
402
|
+
* POST /api/onboarding/cli/:provider/cancel/:sessionId
|
|
403
|
+
* Cancel a CLI auth session
|
|
404
|
+
*/
|
|
405
|
+
onboardingRouter.post('/cli/:provider/cancel/:sessionId', async (req, res) => {
|
|
406
|
+
const provider = req.params.provider;
|
|
407
|
+
const sessionId = req.params.sessionId;
|
|
408
|
+
const userId = req.session.userId;
|
|
409
|
+
const session = activeSessions.get(sessionId);
|
|
410
|
+
if (session?.userId === userId) {
|
|
411
|
+
// Cancel on workspace side if applicable
|
|
412
|
+
if (session.workspaceUrl && session.workspaceSessionId) {
|
|
413
|
+
try {
|
|
414
|
+
await fetch(`${session.workspaceUrl}/auth/cli/${provider}/cancel/${session.workspaceSessionId}`, { method: 'POST' });
|
|
415
|
+
}
|
|
416
|
+
catch {
|
|
417
|
+
// Ignore cancel errors
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
activeSessions.delete(sessionId);
|
|
421
|
+
}
|
|
422
|
+
res.json({ success: true });
|
|
423
|
+
});
|
|
424
|
+
/**
|
|
425
|
+
* POST /api/onboarding/mark-connected/:provider
|
|
426
|
+
* Mark a provider as connected without storing a token.
|
|
427
|
+
* Used by terminal-based setup where the CLI stores credentials locally.
|
|
428
|
+
*/
|
|
429
|
+
onboardingRouter.post('/mark-connected/:provider', async (req, res) => {
|
|
430
|
+
const provider = req.params.provider;
|
|
431
|
+
const userId = req.session.userId;
|
|
432
|
+
const { workspaceId } = req.body;
|
|
433
|
+
// Validate provider
|
|
434
|
+
const validProviders = ['anthropic', 'openai', 'google', 'github', 'opencode', 'factory', 'cursor'];
|
|
435
|
+
if (!validProviders.includes(provider)) {
|
|
436
|
+
return res.status(400).json({ error: 'Invalid provider' });
|
|
437
|
+
}
|
|
438
|
+
try {
|
|
439
|
+
// Mark provider as connected (tokens are stored by CLI on workspace)
|
|
440
|
+
await db.credentials.upsert({
|
|
441
|
+
userId,
|
|
442
|
+
workspaceId,
|
|
443
|
+
provider,
|
|
444
|
+
scopes: getProviderScopes(provider),
|
|
445
|
+
});
|
|
446
|
+
console.log(`[onboarding] Marked ${provider} as connected for user ${userId} workspace ${workspaceId}`);
|
|
447
|
+
res.json({
|
|
448
|
+
success: true,
|
|
449
|
+
message: `${provider} connected successfully`,
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
catch (error) {
|
|
453
|
+
console.error(`Error marking ${provider} as connected:`, error);
|
|
454
|
+
res.status(500).json({ error: 'Failed to mark provider as connected' });
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
/**
|
|
458
|
+
* POST /api/onboarding/token/:provider
|
|
459
|
+
* Directly store a token (for manual paste flow)
|
|
460
|
+
*/
|
|
461
|
+
onboardingRouter.post('/token/:provider', async (req, res) => {
|
|
462
|
+
const provider = req.params.provider;
|
|
463
|
+
const userId = req.session.userId;
|
|
464
|
+
const { token, email, workspaceId } = req.body;
|
|
465
|
+
if (!token) {
|
|
466
|
+
return res.status(400).json({ error: 'Token is required' });
|
|
467
|
+
}
|
|
468
|
+
try {
|
|
469
|
+
// Validate token by making a test API call
|
|
470
|
+
const isValid = await validateProviderToken(provider, token);
|
|
471
|
+
if (!isValid) {
|
|
472
|
+
return res.status(400).json({ error: 'Invalid token' });
|
|
473
|
+
}
|
|
474
|
+
// Mark provider as connected (tokens are not stored centrally - CLI tools
|
|
475
|
+
// authenticate directly on workspace instances)
|
|
476
|
+
await db.credentials.upsert({
|
|
477
|
+
userId,
|
|
478
|
+
provider,
|
|
479
|
+
scopes: getProviderScopes(provider),
|
|
480
|
+
providerAccountEmail: email,
|
|
481
|
+
workspaceId,
|
|
482
|
+
});
|
|
483
|
+
// Set env var and write credential file to workspace
|
|
484
|
+
await setProviderApiKeyEnv(userId, provider, token, workspaceId);
|
|
485
|
+
res.json({
|
|
486
|
+
success: true,
|
|
487
|
+
message: `${provider} connected successfully`,
|
|
488
|
+
note: 'Token validated. Configure this on your workspace for usage.',
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
catch (error) {
|
|
492
|
+
console.error(`Error storing provider connection for ${provider}:`, error);
|
|
493
|
+
res.status(500).json({ error: 'Failed to store provider connection' });
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
/**
|
|
497
|
+
* GET /api/onboarding/status
|
|
498
|
+
* Get overall onboarding status
|
|
499
|
+
*/
|
|
500
|
+
onboardingRouter.get('/status', async (req, res) => {
|
|
501
|
+
const userId = req.session.userId;
|
|
502
|
+
try {
|
|
503
|
+
const [user, credentials, repositories] = await Promise.all([
|
|
504
|
+
db.users.findById(userId),
|
|
505
|
+
db.credentials.findByUserId(userId),
|
|
506
|
+
db.repositories.findByUserId(userId),
|
|
507
|
+
]);
|
|
508
|
+
const connectedProviders = credentials.map(c => c.provider);
|
|
509
|
+
const hasAIProvider = connectedProviders.some(p => ['anthropic', 'openai', 'google', 'cursor'].includes(p));
|
|
510
|
+
res.json({
|
|
511
|
+
steps: {
|
|
512
|
+
github: { complete: connectedProviders.includes('github') },
|
|
513
|
+
aiProvider: {
|
|
514
|
+
complete: hasAIProvider,
|
|
515
|
+
connected: connectedProviders.filter(p => p !== 'github'),
|
|
516
|
+
},
|
|
517
|
+
repository: {
|
|
518
|
+
complete: repositories.length > 0,
|
|
519
|
+
count: repositories.length,
|
|
520
|
+
},
|
|
521
|
+
},
|
|
522
|
+
onboardingComplete: user?.onboardingCompletedAt != null,
|
|
523
|
+
canCreateWorkspace: hasAIProvider && repositories.length > 0,
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
catch (error) {
|
|
527
|
+
console.error('Error getting onboarding status:', error);
|
|
528
|
+
res.status(500).json({ error: 'Failed to get status' });
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
/**
|
|
532
|
+
* POST /api/onboarding/complete
|
|
533
|
+
* Mark onboarding as complete
|
|
534
|
+
*/
|
|
535
|
+
onboardingRouter.post('/complete', async (req, res) => {
|
|
536
|
+
const userId = req.session.userId;
|
|
537
|
+
try {
|
|
538
|
+
await db.users.completeOnboarding(userId);
|
|
539
|
+
res.json({ success: true });
|
|
540
|
+
}
|
|
541
|
+
catch (error) {
|
|
542
|
+
console.error('Error completing onboarding:', error);
|
|
543
|
+
res.status(500).json({ error: 'Failed to complete onboarding' });
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
/**
|
|
547
|
+
* Helper: Extract credentials from CLI credential file
|
|
548
|
+
* @deprecated Currently unused - kept for potential future use
|
|
549
|
+
*/
|
|
550
|
+
async function _extractCredentials(session, config) {
|
|
551
|
+
if (!config.credentialPath)
|
|
552
|
+
return;
|
|
553
|
+
try {
|
|
554
|
+
const fs = await import('fs/promises');
|
|
555
|
+
const os = await import('os');
|
|
556
|
+
const credPath = config.credentialPath.replace('~', os.homedir());
|
|
557
|
+
const content = await fs.readFile(credPath, 'utf8');
|
|
558
|
+
const creds = JSON.parse(content);
|
|
559
|
+
// Extract token based on provider structure
|
|
560
|
+
if (session.provider === 'anthropic') {
|
|
561
|
+
// Claude stores OAuth in: { claudeAiOauth: { accessToken: "...", refreshToken: "...", expiresAt: ... } }
|
|
562
|
+
if (creds.claudeAiOauth?.accessToken) {
|
|
563
|
+
session.token = creds.claudeAiOauth.accessToken;
|
|
564
|
+
session.refreshToken = creds.claudeAiOauth.refreshToken;
|
|
565
|
+
if (creds.claudeAiOauth.expiresAt) {
|
|
566
|
+
session.tokenExpiresAt = new Date(creds.claudeAiOauth.expiresAt);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
else {
|
|
570
|
+
// Fallback to legacy formats
|
|
571
|
+
session.token = creds.oauth_token || creds.access_token || creds.api_key;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
else if (session.provider === 'openai') {
|
|
575
|
+
// Codex stores OAuth in: { tokens: { access_token: "...", refresh_token: "...", ... } }
|
|
576
|
+
if (creds.tokens?.access_token) {
|
|
577
|
+
session.token = creds.tokens.access_token;
|
|
578
|
+
session.refreshToken = creds.tokens.refresh_token;
|
|
579
|
+
// Codex doesn't store expiry in the file, but JWTs have exp claim
|
|
580
|
+
// We could decode it, but for now just skip
|
|
581
|
+
}
|
|
582
|
+
else {
|
|
583
|
+
// Fallback: API key or legacy formats
|
|
584
|
+
session.token = creds.OPENAI_API_KEY || creds.token || creds.access_token || creds.api_key;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
catch (error) {
|
|
589
|
+
// Credentials file doesn't exist or isn't readable yet
|
|
590
|
+
console.log(`Could not read credentials file: ${error}`);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Helper: Get default scopes for a provider
|
|
595
|
+
*/
|
|
596
|
+
function getProviderScopes(provider) {
|
|
597
|
+
const scopes = {
|
|
598
|
+
anthropic: ['claude-code:execute', 'user:read'],
|
|
599
|
+
openai: ['codex:execute', 'chat:write'],
|
|
600
|
+
google: ['generative-language'],
|
|
601
|
+
github: ['read:user', 'user:email', 'repo'],
|
|
602
|
+
opencode: ['code:execute'],
|
|
603
|
+
factory: ['droid:execute'],
|
|
604
|
+
cursor: ['cursor:execute', 'code:write'],
|
|
605
|
+
};
|
|
606
|
+
return scopes[provider] || [];
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Helper: Validate a provider token by making a test API call
|
|
610
|
+
*
|
|
611
|
+
* Note: OAuth tokens from CLI flows (like `claude` CLI) are different from API keys.
|
|
612
|
+
* - API keys: sk-ant-api03-... (can be validated via API)
|
|
613
|
+
* - OAuth tokens: Session tokens from OAuth flow (can't be validated the same way)
|
|
614
|
+
*
|
|
615
|
+
* For OAuth tokens, we accept them if they look valid (non-empty, reasonable length).
|
|
616
|
+
* The CLI already validated the OAuth flow, so we trust those tokens.
|
|
617
|
+
*/
|
|
618
|
+
async function validateProviderToken(provider, token) {
|
|
619
|
+
// Basic sanity check
|
|
620
|
+
if (!token || token.length < 10) {
|
|
621
|
+
return false;
|
|
622
|
+
}
|
|
623
|
+
try {
|
|
624
|
+
// Check if this looks like an API key vs OAuth token
|
|
625
|
+
const isAnthropicApiKey = token.startsWith('sk-ant-');
|
|
626
|
+
const isOpenAIApiKey = token.startsWith('sk-');
|
|
627
|
+
// For OAuth tokens (not API keys), accept them without API validation
|
|
628
|
+
// The OAuth flow already authenticated the user
|
|
629
|
+
if (provider === 'anthropic' && !isAnthropicApiKey) {
|
|
630
|
+
console.log('[onboarding] Accepting OAuth token for anthropic (not an API key)');
|
|
631
|
+
return true;
|
|
632
|
+
}
|
|
633
|
+
if (provider === 'openai' && !isOpenAIApiKey) {
|
|
634
|
+
console.log('[onboarding] Accepting OAuth token for openai (not an API key)');
|
|
635
|
+
return true;
|
|
636
|
+
}
|
|
637
|
+
// For API keys, validate via API call
|
|
638
|
+
const endpoints = {
|
|
639
|
+
anthropic: {
|
|
640
|
+
url: 'https://api.anthropic.com/v1/messages',
|
|
641
|
+
headers: {
|
|
642
|
+
'x-api-key': token,
|
|
643
|
+
'anthropic-version': '2023-06-01',
|
|
644
|
+
},
|
|
645
|
+
},
|
|
646
|
+
openai: {
|
|
647
|
+
url: 'https://api.openai.com/v1/models',
|
|
648
|
+
headers: {
|
|
649
|
+
Authorization: `Bearer ${token}`,
|
|
650
|
+
},
|
|
651
|
+
},
|
|
652
|
+
google: {
|
|
653
|
+
url: `https://generativelanguage.googleapis.com/v1/models?key=${encodeURIComponent(token)}`,
|
|
654
|
+
headers: {},
|
|
655
|
+
},
|
|
656
|
+
};
|
|
657
|
+
const config = endpoints[provider];
|
|
658
|
+
if (!config)
|
|
659
|
+
return true; // Unknown provider, assume valid
|
|
660
|
+
const response = await fetch(config.url, {
|
|
661
|
+
method: provider === 'anthropic' ? 'POST' : 'GET',
|
|
662
|
+
headers: config.headers,
|
|
663
|
+
...(provider === 'anthropic' && {
|
|
664
|
+
body: JSON.stringify({
|
|
665
|
+
model: 'claude-3-haiku-20240307',
|
|
666
|
+
max_tokens: 1,
|
|
667
|
+
messages: [{ role: 'user', content: 'test' }],
|
|
668
|
+
}),
|
|
669
|
+
}),
|
|
670
|
+
});
|
|
671
|
+
// 401/403 means invalid token, anything else (including rate limits) means valid
|
|
672
|
+
return response.status !== 401 && response.status !== 403;
|
|
673
|
+
}
|
|
674
|
+
catch (error) {
|
|
675
|
+
console.error(`Error validating ${provider} token:`, error);
|
|
676
|
+
return false;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
//# sourceMappingURL=onboarding.js.map
|