@siteboon/claude-code-ui 1.12.0 → 1.13.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.
@@ -0,0 +1,263 @@
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: 'credentials_file'
18
+ });
19
+ }
20
+
21
+ return res.json({
22
+ authenticated: false,
23
+ email: null,
24
+ error: credentialsResult.error || 'Not authenticated'
25
+ });
26
+
27
+ } catch (error) {
28
+ console.error('Error checking Claude auth status:', error);
29
+ res.status(500).json({
30
+ authenticated: false,
31
+ email: null,
32
+ error: error.message
33
+ });
34
+ }
35
+ });
36
+
37
+ router.get('/cursor/status', async (req, res) => {
38
+ try {
39
+ const result = await checkCursorStatus();
40
+
41
+ res.json({
42
+ authenticated: result.authenticated,
43
+ email: result.email,
44
+ error: result.error
45
+ });
46
+
47
+ } catch (error) {
48
+ console.error('Error checking Cursor auth status:', error);
49
+ res.status(500).json({
50
+ authenticated: false,
51
+ email: null,
52
+ error: error.message
53
+ });
54
+ }
55
+ });
56
+
57
+ router.get('/codex/status', async (req, res) => {
58
+ try {
59
+ const result = await checkCodexCredentials();
60
+
61
+ res.json({
62
+ authenticated: result.authenticated,
63
+ email: result.email,
64
+ error: result.error
65
+ });
66
+
67
+ } catch (error) {
68
+ console.error('Error checking Codex auth status:', error);
69
+ res.status(500).json({
70
+ authenticated: false,
71
+ email: null,
72
+ error: error.message
73
+ });
74
+ }
75
+ });
76
+
77
+ async function checkClaudeCredentials() {
78
+ try {
79
+ const credPath = path.join(os.homedir(), '.claude', '.credentials.json');
80
+ const content = await fs.readFile(credPath, 'utf8');
81
+ const creds = JSON.parse(content);
82
+
83
+ const oauth = creds.claudeAiOauth;
84
+ if (oauth && oauth.accessToken) {
85
+ const isExpired = oauth.expiresAt && Date.now() >= oauth.expiresAt;
86
+
87
+ if (!isExpired) {
88
+ return {
89
+ authenticated: true,
90
+ email: creds.email || creds.user || null
91
+ };
92
+ }
93
+ }
94
+
95
+ return {
96
+ authenticated: false,
97
+ email: null
98
+ };
99
+ } catch (error) {
100
+ return {
101
+ authenticated: false,
102
+ email: null
103
+ };
104
+ }
105
+ }
106
+
107
+ function checkCursorStatus() {
108
+ return new Promise((resolve) => {
109
+ let processCompleted = false;
110
+
111
+ const timeout = setTimeout(() => {
112
+ if (!processCompleted) {
113
+ processCompleted = true;
114
+ if (childProcess) {
115
+ childProcess.kill();
116
+ }
117
+ resolve({
118
+ authenticated: false,
119
+ email: null,
120
+ error: 'Command timeout'
121
+ });
122
+ }
123
+ }, 5000);
124
+
125
+ let childProcess;
126
+ try {
127
+ childProcess = spawn('cursor-agent', ['status']);
128
+ } catch (err) {
129
+ clearTimeout(timeout);
130
+ processCompleted = true;
131
+ resolve({
132
+ authenticated: false,
133
+ email: null,
134
+ error: 'Cursor CLI not found or not installed'
135
+ });
136
+ return;
137
+ }
138
+
139
+ let stdout = '';
140
+ let stderr = '';
141
+
142
+ childProcess.stdout.on('data', (data) => {
143
+ stdout += data.toString();
144
+ });
145
+
146
+ childProcess.stderr.on('data', (data) => {
147
+ stderr += data.toString();
148
+ });
149
+
150
+ childProcess.on('close', (code) => {
151
+ if (processCompleted) return;
152
+ processCompleted = true;
153
+ clearTimeout(timeout);
154
+
155
+ if (code === 0) {
156
+ const emailMatch = stdout.match(/Logged in as ([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/i);
157
+
158
+ if (emailMatch) {
159
+ resolve({
160
+ authenticated: true,
161
+ email: emailMatch[1],
162
+ output: stdout
163
+ });
164
+ } else if (stdout.includes('Logged in')) {
165
+ resolve({
166
+ authenticated: true,
167
+ email: 'Logged in',
168
+ output: stdout
169
+ });
170
+ } else {
171
+ resolve({
172
+ authenticated: false,
173
+ email: null,
174
+ error: 'Not logged in'
175
+ });
176
+ }
177
+ } else {
178
+ resolve({
179
+ authenticated: false,
180
+ email: null,
181
+ error: stderr || 'Not logged in'
182
+ });
183
+ }
184
+ });
185
+
186
+ childProcess.on('error', (err) => {
187
+ if (processCompleted) return;
188
+ processCompleted = true;
189
+ clearTimeout(timeout);
190
+
191
+ resolve({
192
+ authenticated: false,
193
+ email: null,
194
+ error: 'Cursor CLI not found or not installed'
195
+ });
196
+ });
197
+ });
198
+ }
199
+
200
+ async function checkCodexCredentials() {
201
+ try {
202
+ const authPath = path.join(os.homedir(), '.codex', 'auth.json');
203
+ const content = await fs.readFile(authPath, 'utf8');
204
+ const auth = JSON.parse(content);
205
+
206
+ // Tokens are nested under 'tokens' key
207
+ const tokens = auth.tokens || {};
208
+
209
+ // Check for valid tokens (id_token or access_token)
210
+ if (tokens.id_token || tokens.access_token) {
211
+ // Try to extract email from id_token JWT payload
212
+ let email = 'Authenticated';
213
+ if (tokens.id_token) {
214
+ try {
215
+ // JWT is base64url encoded: header.payload.signature
216
+ const parts = tokens.id_token.split('.');
217
+ if (parts.length >= 2) {
218
+ // Decode the payload (second part)
219
+ const payload = JSON.parse(Buffer.from(parts[1], 'base64url').toString('utf8'));
220
+ email = payload.email || payload.user || 'Authenticated';
221
+ }
222
+ } catch {
223
+ // If JWT decoding fails, use fallback
224
+ email = 'Authenticated';
225
+ }
226
+ }
227
+
228
+ return {
229
+ authenticated: true,
230
+ email
231
+ };
232
+ }
233
+
234
+ // Also check for OPENAI_API_KEY as fallback auth method
235
+ if (auth.OPENAI_API_KEY) {
236
+ return {
237
+ authenticated: true,
238
+ email: 'API Key Auth'
239
+ };
240
+ }
241
+
242
+ return {
243
+ authenticated: false,
244
+ email: null,
245
+ error: 'No valid tokens found'
246
+ };
247
+ } catch (error) {
248
+ if (error.code === 'ENOENT') {
249
+ return {
250
+ authenticated: false,
251
+ email: null,
252
+ error: 'Codex not configured'
253
+ };
254
+ }
255
+ return {
256
+ authenticated: false,
257
+ email: null,
258
+ error: error.message
259
+ };
260
+ }
261
+ }
262
+
263
+ export default router;
@@ -0,0 +1,310 @@
1
+ import express from 'express';
2
+ import { spawn } from 'child_process';
3
+ import { promises as fs } from 'fs';
4
+ import path from 'path';
5
+ import os from 'os';
6
+ import TOML from '@iarna/toml';
7
+ import { getCodexSessions, getCodexSessionMessages, deleteCodexSession } from '../projects.js';
8
+
9
+ const router = express.Router();
10
+
11
+ router.get('/config', async (req, res) => {
12
+ try {
13
+ const configPath = path.join(os.homedir(), '.codex', 'config.toml');
14
+ const content = await fs.readFile(configPath, 'utf8');
15
+ const config = TOML.parse(content);
16
+
17
+ res.json({
18
+ success: true,
19
+ config: {
20
+ model: config.model || null,
21
+ mcpServers: config.mcp_servers || {},
22
+ approvalMode: config.approval_mode || 'suggest'
23
+ }
24
+ });
25
+ } catch (error) {
26
+ if (error.code === 'ENOENT') {
27
+ res.json({
28
+ success: true,
29
+ config: {
30
+ model: null,
31
+ mcpServers: {},
32
+ approvalMode: 'suggest'
33
+ }
34
+ });
35
+ } else {
36
+ console.error('Error reading Codex config:', error);
37
+ res.status(500).json({ success: false, error: error.message });
38
+ }
39
+ }
40
+ });
41
+
42
+ router.get('/sessions', async (req, res) => {
43
+ try {
44
+ const { projectPath } = req.query;
45
+
46
+ if (!projectPath) {
47
+ return res.status(400).json({ success: false, error: 'projectPath query parameter required' });
48
+ }
49
+
50
+ const sessions = await getCodexSessions(projectPath);
51
+ res.json({ success: true, sessions });
52
+ } catch (error) {
53
+ console.error('Error fetching Codex sessions:', error);
54
+ res.status(500).json({ success: false, error: error.message });
55
+ }
56
+ });
57
+
58
+ router.get('/sessions/:sessionId/messages', async (req, res) => {
59
+ try {
60
+ const { sessionId } = req.params;
61
+ const { limit, offset } = req.query;
62
+
63
+ const result = await getCodexSessionMessages(
64
+ sessionId,
65
+ limit ? parseInt(limit, 10) : null,
66
+ offset ? parseInt(offset, 10) : 0
67
+ );
68
+
69
+ res.json({ success: true, ...result });
70
+ } catch (error) {
71
+ console.error('Error fetching Codex session messages:', error);
72
+ res.status(500).json({ success: false, error: error.message });
73
+ }
74
+ });
75
+
76
+ router.delete('/sessions/:sessionId', async (req, res) => {
77
+ try {
78
+ const { sessionId } = req.params;
79
+ await deleteCodexSession(sessionId);
80
+ res.json({ success: true });
81
+ } catch (error) {
82
+ console.error(`Error deleting Codex session ${req.params.sessionId}:`, error);
83
+ res.status(500).json({ success: false, error: error.message });
84
+ }
85
+ });
86
+
87
+ // MCP Server Management Routes
88
+
89
+ router.get('/mcp/cli/list', async (req, res) => {
90
+ try {
91
+ const proc = spawn('codex', ['mcp', 'list'], { stdio: ['pipe', 'pipe', 'pipe'] });
92
+
93
+ let stdout = '';
94
+ let stderr = '';
95
+
96
+ proc.stdout.on('data', (data) => { stdout += data.toString(); });
97
+ proc.stderr.on('data', (data) => { stderr += data.toString(); });
98
+
99
+ proc.on('close', (code) => {
100
+ if (code === 0) {
101
+ res.json({ success: true, output: stdout, servers: parseCodexListOutput(stdout) });
102
+ } else {
103
+ res.status(500).json({ error: 'Codex CLI command failed', details: stderr });
104
+ }
105
+ });
106
+
107
+ proc.on('error', (error) => {
108
+ res.status(500).json({ error: 'Failed to run Codex CLI', details: error.message });
109
+ });
110
+ } catch (error) {
111
+ res.status(500).json({ error: 'Failed to list MCP servers', details: error.message });
112
+ }
113
+ });
114
+
115
+ router.post('/mcp/cli/add', async (req, res) => {
116
+ try {
117
+ const { name, command, args = [], env = {} } = req.body;
118
+
119
+ if (!name || !command) {
120
+ return res.status(400).json({ error: 'name and command are required' });
121
+ }
122
+
123
+ // Build: codex mcp add <name> [-e KEY=VAL]... -- <command> [args...]
124
+ let cliArgs = ['mcp', 'add', name];
125
+
126
+ Object.entries(env).forEach(([key, value]) => {
127
+ cliArgs.push('-e', `${key}=${value}`);
128
+ });
129
+
130
+ cliArgs.push('--', command);
131
+
132
+ if (args && args.length > 0) {
133
+ cliArgs.push(...args);
134
+ }
135
+
136
+ const proc = spawn('codex', cliArgs, { stdio: ['pipe', 'pipe', 'pipe'] });
137
+
138
+ let stdout = '';
139
+ let stderr = '';
140
+
141
+ proc.stdout.on('data', (data) => { stdout += data.toString(); });
142
+ proc.stderr.on('data', (data) => { stderr += data.toString(); });
143
+
144
+ proc.on('close', (code) => {
145
+ if (code === 0) {
146
+ res.json({ success: true, output: stdout, message: `MCP server "${name}" added successfully` });
147
+ } else {
148
+ res.status(400).json({ error: 'Codex CLI command failed', details: stderr });
149
+ }
150
+ });
151
+
152
+ proc.on('error', (error) => {
153
+ res.status(500).json({ error: 'Failed to run Codex CLI', details: error.message });
154
+ });
155
+ } catch (error) {
156
+ res.status(500).json({ error: 'Failed to add MCP server', details: error.message });
157
+ }
158
+ });
159
+
160
+ router.delete('/mcp/cli/remove/:name', async (req, res) => {
161
+ try {
162
+ const { name } = req.params;
163
+
164
+ const proc = spawn('codex', ['mcp', 'remove', name], { stdio: ['pipe', 'pipe', 'pipe'] });
165
+
166
+ let stdout = '';
167
+ let stderr = '';
168
+
169
+ proc.stdout.on('data', (data) => { stdout += data.toString(); });
170
+ proc.stderr.on('data', (data) => { stderr += data.toString(); });
171
+
172
+ proc.on('close', (code) => {
173
+ if (code === 0) {
174
+ res.json({ success: true, output: stdout, message: `MCP server "${name}" removed successfully` });
175
+ } else {
176
+ res.status(400).json({ error: 'Codex CLI command failed', details: stderr });
177
+ }
178
+ });
179
+
180
+ proc.on('error', (error) => {
181
+ res.status(500).json({ error: 'Failed to run Codex CLI', details: error.message });
182
+ });
183
+ } catch (error) {
184
+ res.status(500).json({ error: 'Failed to remove MCP server', details: error.message });
185
+ }
186
+ });
187
+
188
+ router.get('/mcp/cli/get/:name', async (req, res) => {
189
+ try {
190
+ const { name } = req.params;
191
+
192
+ const proc = spawn('codex', ['mcp', 'get', name], { stdio: ['pipe', 'pipe', 'pipe'] });
193
+
194
+ let stdout = '';
195
+ let stderr = '';
196
+
197
+ proc.stdout.on('data', (data) => { stdout += data.toString(); });
198
+ proc.stderr.on('data', (data) => { stderr += data.toString(); });
199
+
200
+ proc.on('close', (code) => {
201
+ if (code === 0) {
202
+ res.json({ success: true, output: stdout, server: parseCodexGetOutput(stdout) });
203
+ } else {
204
+ res.status(404).json({ error: 'Codex CLI command failed', details: stderr });
205
+ }
206
+ });
207
+
208
+ proc.on('error', (error) => {
209
+ res.status(500).json({ error: 'Failed to run Codex CLI', details: error.message });
210
+ });
211
+ } catch (error) {
212
+ res.status(500).json({ error: 'Failed to get MCP server details', details: error.message });
213
+ }
214
+ });
215
+
216
+ router.get('/mcp/config/read', async (req, res) => {
217
+ try {
218
+ const configPath = path.join(os.homedir(), '.codex', 'config.toml');
219
+
220
+ let configData = null;
221
+
222
+ try {
223
+ const fileContent = await fs.readFile(configPath, 'utf8');
224
+ configData = TOML.parse(fileContent);
225
+ } catch (error) {
226
+ // Config file doesn't exist
227
+ }
228
+
229
+ if (!configData) {
230
+ return res.json({ success: false, message: 'No Codex configuration file found', servers: [] });
231
+ }
232
+
233
+ const servers = [];
234
+
235
+ if (configData.mcp_servers && typeof configData.mcp_servers === 'object') {
236
+ for (const [name, config] of Object.entries(configData.mcp_servers)) {
237
+ servers.push({
238
+ id: name,
239
+ name: name,
240
+ type: 'stdio',
241
+ scope: 'user',
242
+ config: {
243
+ command: config.command || '',
244
+ args: config.args || [],
245
+ env: config.env || {}
246
+ },
247
+ raw: config
248
+ });
249
+ }
250
+ }
251
+
252
+ res.json({ success: true, configPath, servers });
253
+ } catch (error) {
254
+ res.status(500).json({ error: 'Failed to read Codex configuration', details: error.message });
255
+ }
256
+ });
257
+
258
+ function parseCodexListOutput(output) {
259
+ const servers = [];
260
+ const lines = output.split('\n').filter(line => line.trim());
261
+
262
+ for (const line of lines) {
263
+ if (line.includes(':')) {
264
+ const colonIndex = line.indexOf(':');
265
+ const name = line.substring(0, colonIndex).trim();
266
+
267
+ if (!name) continue;
268
+
269
+ const rest = line.substring(colonIndex + 1).trim();
270
+ let description = rest;
271
+ let status = 'unknown';
272
+
273
+ if (rest.includes('✓') || rest.includes('✗')) {
274
+ const statusMatch = rest.match(/(.*?)\s*-\s*([✓✗].*)$/);
275
+ if (statusMatch) {
276
+ description = statusMatch[1].trim();
277
+ status = statusMatch[2].includes('✓') ? 'connected' : 'failed';
278
+ }
279
+ }
280
+
281
+ servers.push({ name, type: 'stdio', status, description });
282
+ }
283
+ }
284
+
285
+ return servers;
286
+ }
287
+
288
+ function parseCodexGetOutput(output) {
289
+ try {
290
+ const jsonMatch = output.match(/\{[\s\S]*\}/);
291
+ if (jsonMatch) {
292
+ return JSON.parse(jsonMatch[0]);
293
+ }
294
+
295
+ const server = { raw_output: output };
296
+ const lines = output.split('\n');
297
+
298
+ for (const line of lines) {
299
+ if (line.includes('Name:')) server.name = line.split(':')[1]?.trim();
300
+ else if (line.includes('Type:')) server.type = line.split(':')[1]?.trim();
301
+ else if (line.includes('Command:')) server.command = line.split(':')[1]?.trim();
302
+ }
303
+
304
+ return server;
305
+ } catch (error) {
306
+ return { raw_output: output, parse_error: error.message };
307
+ }
308
+ }
309
+
310
+ export default router;