upfynai-code 2.9.1 → 2.9.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.
- package/README.md +91 -66
- package/client/dist/api-docs.html +838 -0
- package/client/dist/assets/AppContent-BXZDeSIC.js +545 -0
- package/client/dist/assets/CanvasFullScreen-mnpCnLZ9.js +1 -0
- package/client/dist/assets/CanvasWorkspace-4CqmjAVQ.js +163 -0
- package/client/dist/assets/DashboardPanel-zFIFlw56.js +1 -0
- package/client/dist/assets/FileTree-B0c_GaB3.js +1 -0
- package/client/dist/assets/GitPanel-DUP4zVU4.js +2 -0
- package/client/dist/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
- package/client/dist/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
- package/client/dist/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
- package/client/dist/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
- package/client/dist/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
- package/client/dist/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
- package/client/dist/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
- package/client/dist/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
- package/client/dist/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
- package/client/dist/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
- package/client/dist/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
- package/client/dist/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
- package/client/dist/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
- package/client/dist/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
- package/client/dist/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
- package/client/dist/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
- package/client/dist/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
- package/client/dist/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
- package/client/dist/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
- package/client/dist/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
- package/client/dist/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
- package/client/dist/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
- package/client/dist/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
- package/client/dist/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
- package/client/dist/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
- package/client/dist/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
- package/client/dist/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
- package/client/dist/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
- package/client/dist/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
- package/client/dist/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
- package/client/dist/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
- package/client/dist/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
- package/client/dist/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
- package/client/dist/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
- package/client/dist/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
- package/client/dist/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
- package/client/dist/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
- package/client/dist/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
- package/client/dist/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
- package/client/dist/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
- package/client/dist/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
- package/client/dist/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
- package/client/dist/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
- package/client/dist/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
- package/client/dist/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
- package/client/dist/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
- package/client/dist/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
- package/client/dist/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
- package/client/dist/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
- package/client/dist/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
- package/client/dist/assets/LoginModal-BRycfsyD.js +13 -0
- package/client/dist/assets/MarkdownPreview-DHmk3qzu.js +1 -0
- package/client/dist/assets/MermaidBlock-BuBc_G-F.js +2 -0
- package/client/dist/assets/Onboarding-BcnaZZ0o.js +1 -0
- package/client/dist/assets/PreviewPanel-CqCa92Tf.js +32 -0
- package/client/dist/assets/SetupForm-S0g6u5yT.js +1 -0
- package/client/dist/assets/WorkflowsPanel-CouH9JDO.js +1 -0
- package/client/dist/assets/index-BFuqS0tY.css +1 -0
- package/client/dist/assets/index-CNDcVl2g.js +68 -0
- package/client/dist/assets/pdf-CE_K4jFx.js +12 -0
- package/client/dist/assets/vendor-canvas-BZV40eAE.css +1 -0
- package/client/dist/assets/vendor-canvas-D39yWul6.js +49 -0
- package/client/dist/assets/vendor-codemirror-CbtmxxaB.js +35 -0
- package/client/dist/assets/vendor-diff-DNQpbhrT.js +69 -0
- package/client/dist/assets/vendor-i18n-DCFGyhQR.js +1 -0
- package/client/dist/assets/vendor-icons-BaD0x9SL.js +711 -0
- package/client/dist/assets/vendor-markdown-CimbIo6Y.js +296 -0
- package/client/dist/assets/vendor-mermaid-CH7SGc99.js +2556 -0
- package/client/dist/assets/vendor-react-96lCPsRK.js +67 -0
- package/client/dist/assets/vendor-syntax-DuHI9Ok6.js +16 -0
- package/client/dist/assets/vendor-xterm-CZq1hqo1.js +66 -0
- package/client/dist/assets/vendor-xterm-qxJ8_QYu.css +32 -0
- package/client/dist/clear-cache.html +85 -0
- package/client/dist/convert-icons.md +53 -0
- package/client/dist/favicon.png +0 -0
- package/client/dist/favicon.svg +5 -0
- package/client/dist/generate-icons.js +49 -0
- package/client/dist/icons/claude-ai-icon.svg +1 -0
- package/client/dist/icons/codex-white.svg +3 -0
- package/client/dist/icons/codex.svg +3 -0
- package/client/dist/icons/cursor-white.svg +12 -0
- package/client/dist/icons/cursor.svg +1 -0
- package/client/dist/icons/icon-128x128.png +0 -0
- package/client/dist/icons/icon-128x128.svg +5 -0
- package/client/dist/icons/icon-144x144.png +0 -0
- package/client/dist/icons/icon-144x144.svg +5 -0
- package/client/dist/icons/icon-152x152.png +0 -0
- package/client/dist/icons/icon-152x152.svg +5 -0
- package/client/dist/icons/icon-192x192.png +0 -0
- package/client/dist/icons/icon-192x192.svg +5 -0
- package/client/dist/icons/icon-384x384.png +0 -0
- package/client/dist/icons/icon-384x384.svg +5 -0
- package/client/dist/icons/icon-512x512.png +0 -0
- package/client/dist/icons/icon-512x512.svg +5 -0
- package/client/dist/icons/icon-72x72.png +0 -0
- package/client/dist/icons/icon-72x72.svg +5 -0
- package/client/dist/icons/icon-96x96.png +0 -0
- package/client/dist/icons/icon-96x96.svg +5 -0
- package/client/dist/icons/icon-template.svg +5 -0
- package/client/dist/index.html +119 -0
- package/client/dist/logo-128.png +0 -0
- package/client/dist/logo-256.png +0 -0
- package/client/dist/logo-32.png +0 -0
- package/client/dist/logo-512.png +0 -0
- package/client/dist/logo-64.png +0 -0
- package/client/dist/logo.svg +14 -0
- package/client/dist/manifest.json +61 -0
- package/client/dist/mcp-docs.html +108 -0
- package/client/dist/offline.html +84 -0
- package/client/dist/screenshots/cli-selection.png +0 -0
- package/client/dist/screenshots/desktop-main.png +0 -0
- package/client/dist/screenshots/mobile-chat.png +0 -0
- package/client/dist/screenshots/tools-modal.png +0 -0
- package/client/dist/sw.js +82 -0
- package/commands/upfynai-connect.md +59 -0
- package/commands/upfynai-disconnect.md +31 -0
- package/commands/upfynai-doctor.md +99 -0
- package/commands/upfynai-export.md +49 -0
- package/commands/upfynai-local.md +82 -0
- package/commands/upfynai-status.md +75 -0
- package/commands/upfynai-stop.md +49 -0
- package/commands/upfynai-uninstall.md +58 -0
- package/commands/upfynai.md +69 -0
- package/package.json +143 -82
- package/scripts/build-client.js +17 -0
- package/scripts/fix-node-pty.js +67 -0
- package/scripts/install-commands.js +78 -0
- package/server/agent-loop.js +242 -0
- package/server/auto-compact.js +99 -0
- package/server/claude-sdk.js +797 -0
- package/server/cli-ui.js +785 -0
- package/server/cli.js +596 -0
- package/server/constants/config.js +31 -0
- package/server/cursor-cli.js +270 -0
- package/server/database/auth.db +0 -0
- package/server/database/db.js +1391 -0
- package/server/database/init.sql +70 -0
- package/server/index.js +3799 -0
- package/server/load-env.js +26 -0
- package/server/mcp-server.js +621 -0
- package/server/middleware/auth.js +176 -0
- package/server/middleware/relayHelpers.js +44 -0
- package/server/middleware/sandboxRouter.js +174 -0
- package/server/openai-codex.js +403 -0
- package/server/openrouter.js +137 -0
- package/server/projects.js +1807 -0
- package/server/provider-factory.js +174 -0
- package/server/relay-client.js +379 -0
- package/server/routes/agent.js +1226 -0
- package/server/routes/auth.js +554 -0
- package/server/routes/canvas.js +53 -0
- package/server/routes/cli-auth.js +263 -0
- package/server/routes/codex.js +396 -0
- package/server/routes/commands.js +707 -0
- package/server/routes/composio.js +176 -0
- package/server/routes/cursor.js +770 -0
- package/server/routes/dashboard.js +295 -0
- package/server/routes/git.js +1208 -0
- package/server/routes/keys.js +34 -0
- package/server/routes/mcp-utils.js +48 -0
- package/server/routes/mcp.js +661 -0
- package/server/routes/payments.js +227 -0
- package/server/routes/projects.js +655 -0
- package/server/routes/sessions.js +146 -0
- package/server/routes/settings.js +261 -0
- package/server/routes/taskmaster.js +1928 -0
- package/server/routes/user.js +106 -0
- package/server/routes/vapi-chat.js +624 -0
- package/server/routes/voice.js +235 -0
- package/server/routes/webhooks.js +166 -0
- package/server/routes/workflows.js +312 -0
- package/server/sandbox.js +120 -0
- package/server/services/composio.js +204 -0
- package/server/services/sessionRegistry.js +139 -0
- package/server/services/whisperService.js +84 -0
- package/server/services/workflowScheduler.js +206 -0
- package/server/tests/relay-flow.test.js +570 -0
- package/server/tests/sessions.test.js +259 -0
- package/server/utils/commandParser.js +303 -0
- package/server/utils/email.js +61 -0
- package/server/utils/gitConfig.js +24 -0
- package/server/utils/mcp-detector.js +198 -0
- package/server/utils/taskmaster-websocket.js +129 -0
- package/shared/integrationCatalog.d.ts +12 -0
- package/shared/integrationCatalog.js +172 -0
- package/shared/modelConstants.js +96 -0
- package/bin/cli.js +0 -97
- package/dist/agents/claude.js +0 -229
- package/dist/agents/codex.js +0 -48
- package/dist/agents/cursor.js +0 -48
- package/dist/agents/detect.js +0 -51
- package/dist/agents/exec.js +0 -31
- package/dist/agents/files.js +0 -105
- package/dist/agents/git.js +0 -18
- package/dist/agents/gitagent.js +0 -67
- package/dist/agents/index.js +0 -88
- package/dist/agents/shell.js +0 -38
- package/dist/agents/utils.js +0 -136
- package/scripts/postinstall.js +0 -9
- package/scripts/prepublish.js +0 -58
- package/src/animation.js +0 -228
- package/src/auth.js +0 -122
- package/src/config.js +0 -40
- package/src/connect.js +0 -416
- package/src/launch.js +0 -78
- package/src/mcp.js +0 -57
- package/src/permissions.js +0 -140
- package/src/persistent-shell.js +0 -261
- package/src/server.js +0 -54
- /package/{dist → shared}/gitagent/index.js +0 -0
- /package/{dist → shared}/gitagent/parser.js +0 -0
- /package/{dist → shared}/gitagent/prompt-builder.js +0 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Management REST API
|
|
3
|
+
* Provides unified session listing, abort, delete, and stats endpoints.
|
|
4
|
+
* All endpoints filter by userId for user isolation.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Router } from 'express';
|
|
8
|
+
import {
|
|
9
|
+
getAllActiveSessions,
|
|
10
|
+
checkSessionStatus,
|
|
11
|
+
abortSession,
|
|
12
|
+
abortAllSessions,
|
|
13
|
+
getSessionStats,
|
|
14
|
+
} from '../services/sessionRegistry.js';
|
|
15
|
+
|
|
16
|
+
const router = Router();
|
|
17
|
+
|
|
18
|
+
// Helper: get sessions owned by the current user
|
|
19
|
+
function getUserSessions(req) {
|
|
20
|
+
const userId = req.user?.id || req.user?.userId;
|
|
21
|
+
const sessionOwners = req.sessionOwners;
|
|
22
|
+
const all = getAllActiveSessions();
|
|
23
|
+
if (!userId || !sessionOwners) return all; // local mode, no filtering
|
|
24
|
+
return all.filter(s => {
|
|
25
|
+
const owner = sessionOwners.get(s.sessionId);
|
|
26
|
+
return !owner || owner === userId; // include if no owner (legacy) or owned by user
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* GET /api/sessions — List active sessions for this user
|
|
32
|
+
*/
|
|
33
|
+
router.get('/', (req, res) => {
|
|
34
|
+
try {
|
|
35
|
+
const sessions = getUserSessions(req);
|
|
36
|
+
res.json({ sessions });
|
|
37
|
+
} catch (error) {
|
|
38
|
+
res.status(500).json({ error: 'Failed to fetch sessions' });
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* GET /api/sessions/stats — Session counts for this user
|
|
44
|
+
*/
|
|
45
|
+
router.get('/stats', (req, res) => {
|
|
46
|
+
try {
|
|
47
|
+
const sessions = getUserSessions(req);
|
|
48
|
+
const byProvider = { claude: 0, cursor: 0, codex: 0 };
|
|
49
|
+
for (const s of sessions) {
|
|
50
|
+
byProvider[s.provider] = (byProvider[s.provider] || 0) + 1;
|
|
51
|
+
}
|
|
52
|
+
res.json({
|
|
53
|
+
total: sessions.length,
|
|
54
|
+
active: sessions.filter(s => s.status === 'active').length,
|
|
55
|
+
byProvider,
|
|
56
|
+
});
|
|
57
|
+
} catch (error) {
|
|
58
|
+
res.status(500).json({ error: 'Failed to fetch session stats' });
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* GET /api/sessions/:sessionId — Get session details (only if owned)
|
|
64
|
+
*/
|
|
65
|
+
router.get('/:sessionId', (req, res) => {
|
|
66
|
+
try {
|
|
67
|
+
const { sessionId } = req.params;
|
|
68
|
+
const userId = req.user?.id || req.user?.userId;
|
|
69
|
+
const sessionOwners = req.sessionOwners;
|
|
70
|
+
|
|
71
|
+
// Check ownership
|
|
72
|
+
if (userId && sessionOwners) {
|
|
73
|
+
const owner = sessionOwners.get(sessionId);
|
|
74
|
+
if (owner && owner !== userId) {
|
|
75
|
+
return res.status(403).json({ error: 'Access denied' });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const status = checkSessionStatus(sessionId);
|
|
80
|
+
if (!status.active) {
|
|
81
|
+
return res.status(404).json({ error: 'Session not found or inactive' });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
res.json({
|
|
85
|
+
sessionId,
|
|
86
|
+
provider: status.provider,
|
|
87
|
+
active: status.active,
|
|
88
|
+
});
|
|
89
|
+
} catch (error) {
|
|
90
|
+
res.status(500).json({ error: 'Failed to fetch session details' });
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* POST /api/sessions/:sessionId/abort — Abort an active session (only if owned)
|
|
96
|
+
*/
|
|
97
|
+
router.post('/:sessionId/abort', async (req, res) => {
|
|
98
|
+
try {
|
|
99
|
+
const { sessionId } = req.params;
|
|
100
|
+
const { provider } = req.body || {};
|
|
101
|
+
const userId = req.user?.id || req.user?.userId;
|
|
102
|
+
const sessionOwners = req.sessionOwners;
|
|
103
|
+
|
|
104
|
+
// Check ownership before aborting
|
|
105
|
+
if (userId && sessionOwners) {
|
|
106
|
+
const owner = sessionOwners.get(sessionId);
|
|
107
|
+
if (owner && owner !== userId) {
|
|
108
|
+
return res.status(403).json({ error: 'Access denied — cannot abort another user\'s session' });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const result = await abortSession(sessionId, provider);
|
|
113
|
+
if (!result.aborted) {
|
|
114
|
+
return res.status(404).json({ error: 'Session not found or already terminated' });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
res.json({ success: true, sessionId, provider: result.provider });
|
|
118
|
+
} catch (error) {
|
|
119
|
+
res.status(500).json({ error: 'Failed to abort session' });
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* POST /api/sessions/abort-all — Abort all active sessions owned by this user
|
|
125
|
+
*/
|
|
126
|
+
router.post('/abort-all', async (req, res) => {
|
|
127
|
+
try {
|
|
128
|
+
const userSessions = getUserSessions(req);
|
|
129
|
+
const results = [];
|
|
130
|
+
|
|
131
|
+
for (const s of userSessions) {
|
|
132
|
+
const { aborted } = await abortSession(s.sessionId, s.provider);
|
|
133
|
+
results.push({ sessionId: s.sessionId, provider: s.provider, aborted });
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
res.json({
|
|
137
|
+
success: true,
|
|
138
|
+
aborted: results.filter(r => r.aborted).length,
|
|
139
|
+
results,
|
|
140
|
+
});
|
|
141
|
+
} catch (error) {
|
|
142
|
+
res.status(500).json({ error: 'Failed to abort sessions' });
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
export default router;
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { apiKeysDb, credentialsDb } from '../database/db.js';
|
|
3
|
+
|
|
4
|
+
const router = express.Router();
|
|
5
|
+
|
|
6
|
+
// ===============================
|
|
7
|
+
// API Keys Management
|
|
8
|
+
// ===============================
|
|
9
|
+
|
|
10
|
+
// Get all API keys for the authenticated user
|
|
11
|
+
router.get('/api-keys', async (req, res) => {
|
|
12
|
+
try {
|
|
13
|
+
const apiKeys = await apiKeysDb.getApiKeys(req.user.id);
|
|
14
|
+
// Don't send the full API key in the list for security
|
|
15
|
+
const sanitizedKeys = apiKeys.map(key => ({
|
|
16
|
+
...key,
|
|
17
|
+
api_key: key.api_key.substring(0, 10) + '...'
|
|
18
|
+
}));
|
|
19
|
+
res.json({ apiKeys: sanitizedKeys });
|
|
20
|
+
} catch (error) {
|
|
21
|
+
res.status(500).json({ error: 'Failed to fetch API keys' });
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Create a new API key
|
|
26
|
+
router.post('/api-keys', async (req, res) => {
|
|
27
|
+
try {
|
|
28
|
+
const { keyName } = req.body;
|
|
29
|
+
|
|
30
|
+
if (!keyName || !keyName.trim()) {
|
|
31
|
+
return res.status(400).json({ error: 'Key name is required' });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const result = await apiKeysDb.createApiKey(req.user.id, keyName.trim());
|
|
35
|
+
res.json({
|
|
36
|
+
success: true,
|
|
37
|
+
apiKey: result
|
|
38
|
+
});
|
|
39
|
+
} catch (error) {
|
|
40
|
+
res.status(500).json({ error: 'Failed to create API key' });
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Delete an API key
|
|
45
|
+
router.delete('/api-keys/:keyId', async (req, res) => {
|
|
46
|
+
try {
|
|
47
|
+
const { keyId } = req.params;
|
|
48
|
+
const success = await apiKeysDb.deleteApiKey(req.user.id, parseInt(keyId));
|
|
49
|
+
|
|
50
|
+
if (success) {
|
|
51
|
+
res.json({ success: true });
|
|
52
|
+
} else {
|
|
53
|
+
res.status(404).json({ error: 'API key not found' });
|
|
54
|
+
}
|
|
55
|
+
} catch (error) {
|
|
56
|
+
res.status(500).json({ error: 'Failed to delete API key' });
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Toggle API key active status
|
|
61
|
+
router.patch('/api-keys/:keyId/toggle', async (req, res) => {
|
|
62
|
+
try {
|
|
63
|
+
const { keyId } = req.params;
|
|
64
|
+
const { isActive } = req.body;
|
|
65
|
+
|
|
66
|
+
if (typeof isActive !== 'boolean') {
|
|
67
|
+
return res.status(400).json({ error: 'isActive must be a boolean' });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const success = await apiKeysDb.toggleApiKey(req.user.id, parseInt(keyId), isActive);
|
|
71
|
+
|
|
72
|
+
if (success) {
|
|
73
|
+
res.json({ success: true });
|
|
74
|
+
} else {
|
|
75
|
+
res.status(404).json({ error: 'API key not found' });
|
|
76
|
+
}
|
|
77
|
+
} catch (error) {
|
|
78
|
+
res.status(500).json({ error: 'Failed to toggle API key' });
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// ===============================
|
|
83
|
+
// Generic Credentials Management
|
|
84
|
+
// ===============================
|
|
85
|
+
|
|
86
|
+
// Get all credentials for the authenticated user (optionally filtered by type)
|
|
87
|
+
router.get('/credentials', async (req, res) => {
|
|
88
|
+
try {
|
|
89
|
+
const { type } = req.query;
|
|
90
|
+
const credentials = await credentialsDb.getCredentials(req.user.id, type || null);
|
|
91
|
+
// Don't send the actual credential values for security
|
|
92
|
+
res.json({ credentials });
|
|
93
|
+
} catch (error) {
|
|
94
|
+
res.status(500).json({ error: 'Failed to fetch credentials' });
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Create a new credential
|
|
99
|
+
router.post('/credentials', async (req, res) => {
|
|
100
|
+
try {
|
|
101
|
+
const { credentialName, credentialType, credentialValue, description } = req.body;
|
|
102
|
+
|
|
103
|
+
if (!credentialName || !credentialName.trim()) {
|
|
104
|
+
return res.status(400).json({ error: 'Credential name is required' });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!credentialType || !credentialType.trim()) {
|
|
108
|
+
return res.status(400).json({ error: 'Credential type is required' });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (!credentialValue || !credentialValue.trim()) {
|
|
112
|
+
return res.status(400).json({ error: 'Credential value is required' });
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const result = await credentialsDb.createCredential(
|
|
116
|
+
req.user.id,
|
|
117
|
+
credentialName.trim(),
|
|
118
|
+
credentialType.trim(),
|
|
119
|
+
credentialValue.trim(),
|
|
120
|
+
description?.trim() || null
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
res.json({
|
|
124
|
+
success: true,
|
|
125
|
+
credential: result
|
|
126
|
+
});
|
|
127
|
+
} catch (error) {
|
|
128
|
+
res.status(500).json({ error: 'Failed to create credential' });
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Delete a credential
|
|
133
|
+
router.delete('/credentials/:credentialId', async (req, res) => {
|
|
134
|
+
try {
|
|
135
|
+
const { credentialId } = req.params;
|
|
136
|
+
const success = await credentialsDb.deleteCredential(req.user.id, parseInt(credentialId));
|
|
137
|
+
|
|
138
|
+
if (success) {
|
|
139
|
+
res.json({ success: true });
|
|
140
|
+
} else {
|
|
141
|
+
res.status(404).json({ error: 'Credential not found' });
|
|
142
|
+
}
|
|
143
|
+
} catch (error) {
|
|
144
|
+
res.status(500).json({ error: 'Failed to delete credential' });
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Toggle credential active status
|
|
149
|
+
router.patch('/credentials/:credentialId/toggle', async (req, res) => {
|
|
150
|
+
try {
|
|
151
|
+
const { credentialId } = req.params;
|
|
152
|
+
const { isActive } = req.body;
|
|
153
|
+
|
|
154
|
+
if (typeof isActive !== 'boolean') {
|
|
155
|
+
return res.status(400).json({ error: 'isActive must be a boolean' });
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const success = await credentialsDb.toggleCredential(req.user.id, parseInt(credentialId), isActive);
|
|
159
|
+
|
|
160
|
+
if (success) {
|
|
161
|
+
res.json({ success: true });
|
|
162
|
+
} else {
|
|
163
|
+
res.status(404).json({ error: 'Credential not found' });
|
|
164
|
+
}
|
|
165
|
+
} catch (error) {
|
|
166
|
+
res.status(500).json({ error: 'Failed to toggle credential' });
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// ===============================
|
|
171
|
+
// AI Provider Keys (BYOK)
|
|
172
|
+
// ===============================
|
|
173
|
+
|
|
174
|
+
const AI_PROVIDER_TYPES = [
|
|
175
|
+
'anthropic_key',
|
|
176
|
+
'openai_key',
|
|
177
|
+
'openrouter_key',
|
|
178
|
+
'google_key',
|
|
179
|
+
];
|
|
180
|
+
|
|
181
|
+
// Get all AI provider keys for the authenticated user (masked values)
|
|
182
|
+
router.get('/ai-providers', async (req, res) => {
|
|
183
|
+
try {
|
|
184
|
+
const allCreds = [];
|
|
185
|
+
for (const type of AI_PROVIDER_TYPES) {
|
|
186
|
+
const creds = await credentialsDb.getCredentials(req.user.id, type);
|
|
187
|
+
allCreds.push(...creds.map(c => ({
|
|
188
|
+
id: c.id,
|
|
189
|
+
credential_name: c.credential_name,
|
|
190
|
+
credential_type: c.credential_type,
|
|
191
|
+
description: c.description,
|
|
192
|
+
is_active: c.is_active,
|
|
193
|
+
created_at: c.created_at,
|
|
194
|
+
// Mask the key — show first 8 and last 4 chars
|
|
195
|
+
masked_value: c.credential_value
|
|
196
|
+
? c.credential_value.slice(0, 8) + '...' + c.credential_value.slice(-4)
|
|
197
|
+
: '***',
|
|
198
|
+
})));
|
|
199
|
+
}
|
|
200
|
+
res.json({ providers: allCreds });
|
|
201
|
+
} catch (error) {
|
|
202
|
+
res.status(500).json({ error: 'Failed to fetch AI provider keys' });
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// Save an AI provider key
|
|
207
|
+
router.post('/ai-providers', async (req, res) => {
|
|
208
|
+
try {
|
|
209
|
+
const { providerType, apiKey, name } = req.body;
|
|
210
|
+
|
|
211
|
+
if (!providerType || !AI_PROVIDER_TYPES.includes(providerType)) {
|
|
212
|
+
return res.status(400).json({ error: `Invalid provider type. Supported: ${AI_PROVIDER_TYPES.join(', ')}` });
|
|
213
|
+
}
|
|
214
|
+
if (!apiKey || !apiKey.trim()) {
|
|
215
|
+
return res.status(400).json({ error: 'API key is required' });
|
|
216
|
+
}
|
|
217
|
+
if (apiKey.trim().length < 10 || apiKey.trim().length > 256) {
|
|
218
|
+
return res.status(400).json({ error: 'Invalid API key length' });
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const label = providerType.replace('_key', '').replace('_', ' ');
|
|
222
|
+
const credName = name?.trim() || `${label} API key`;
|
|
223
|
+
|
|
224
|
+
// Deactivate existing keys of same type (user should only have one active per provider)
|
|
225
|
+
const existing = await credentialsDb.getCredentials(req.user.id, providerType);
|
|
226
|
+
for (const cred of existing) {
|
|
227
|
+
if (cred.is_active) {
|
|
228
|
+
await credentialsDb.toggleCredential(req.user.id, cred.id, false);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const result = await credentialsDb.createCredential(
|
|
233
|
+
req.user.id,
|
|
234
|
+
credName,
|
|
235
|
+
providerType,
|
|
236
|
+
apiKey.trim(),
|
|
237
|
+
`User-provided ${label} API key`
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
res.json({ success: true, credential: { id: result.id, credential_type: providerType, credential_name: credName } });
|
|
241
|
+
} catch (error) {
|
|
242
|
+
res.status(500).json({ error: 'Failed to save AI provider key' });
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// Delete an AI provider key
|
|
247
|
+
router.delete('/ai-providers/:credentialId', async (req, res) => {
|
|
248
|
+
try {
|
|
249
|
+
const { credentialId } = req.params;
|
|
250
|
+
const success = await credentialsDb.deleteCredential(req.user.id, parseInt(credentialId));
|
|
251
|
+
if (success) {
|
|
252
|
+
res.json({ success: true });
|
|
253
|
+
} else {
|
|
254
|
+
res.status(404).json({ error: 'Provider key not found' });
|
|
255
|
+
}
|
|
256
|
+
} catch (error) {
|
|
257
|
+
res.status(500).json({ error: 'Failed to delete provider key' });
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
export default router;
|