@axhub/genie 0.1.1
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/LICENSE +675 -0
- package/README.md +2 -0
- package/dist/api-docs.html +922 -0
- package/dist/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
- package/dist/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
- package/dist/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
- package/dist/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
- package/dist/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
- package/dist/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
- package/dist/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
- package/dist/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
- package/dist/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
- package/dist/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
- package/dist/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
- package/dist/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
- package/dist/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
- package/dist/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
- package/dist/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
- package/dist/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
- package/dist/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
- package/dist/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
- package/dist/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
- package/dist/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
- package/dist/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
- package/dist/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
- package/dist/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
- package/dist/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
- package/dist/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
- package/dist/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
- package/dist/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
- package/dist/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
- package/dist/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
- package/dist/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
- package/dist/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
- package/dist/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
- package/dist/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
- package/dist/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
- package/dist/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
- package/dist/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
- package/dist/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
- package/dist/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
- package/dist/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
- package/dist/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
- package/dist/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
- package/dist/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
- package/dist/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
- package/dist/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
- package/dist/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
- package/dist/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
- package/dist/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
- package/dist/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
- package/dist/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
- package/dist/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
- package/dist/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
- package/dist/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
- package/dist/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
- package/dist/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
- package/dist/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
- package/dist/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
- package/dist/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
- package/dist/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
- package/dist/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
- package/dist/assets/index-C75hHmvH.js +1249 -0
- package/dist/assets/index-CtRxrKDm.css +32 -0
- package/dist/assets/vendor-codemirror-B88_OPWf.js +39 -0
- package/dist/assets/vendor-react-C3RJLQGO.js +59 -0
- package/dist/assets/vendor-xterm-DfaPXD3y.js +66 -0
- package/dist/clear-cache.html +85 -0
- package/dist/convert-icons.md +53 -0
- package/dist/favicon.png +0 -0
- package/dist/favicon.svg +9 -0
- package/dist/generate-icons.js +49 -0
- package/dist/icons/claude-ai-icon.svg +1 -0
- package/dist/icons/codex-white.svg +3 -0
- package/dist/icons/codex.svg +3 -0
- package/dist/icons/cursor-white.svg +12 -0
- package/dist/icons/cursor.svg +1 -0
- package/dist/icons/generate-icons.md +19 -0
- package/dist/icons/icon-128x128.png +0 -0
- package/dist/icons/icon-128x128.svg +12 -0
- package/dist/icons/icon-144x144.png +0 -0
- package/dist/icons/icon-144x144.svg +12 -0
- package/dist/icons/icon-152x152.png +0 -0
- package/dist/icons/icon-152x152.svg +12 -0
- package/dist/icons/icon-192x192.png +0 -0
- package/dist/icons/icon-192x192.svg +12 -0
- package/dist/icons/icon-384x384.png +0 -0
- package/dist/icons/icon-384x384.svg +12 -0
- package/dist/icons/icon-512x512.png +0 -0
- package/dist/icons/icon-512x512.svg +12 -0
- package/dist/icons/icon-72x72.png +0 -0
- package/dist/icons/icon-72x72.svg +12 -0
- package/dist/icons/icon-96x96.png +0 -0
- package/dist/icons/icon-96x96.svg +12 -0
- package/dist/icons/icon-template.svg +12 -0
- package/dist/index.html +52 -0
- package/dist/logo-128.png +0 -0
- package/dist/logo-256.png +0 -0
- package/dist/logo-32.png +0 -0
- package/dist/logo-512.png +0 -0
- package/dist/logo-64.png +0 -0
- package/dist/logo.svg +17 -0
- package/dist/manifest.json +61 -0
- package/dist/screenshots/cli-selection.png +0 -0
- package/dist/screenshots/desktop-main.png +0 -0
- package/dist/screenshots/mobile-chat.png +0 -0
- package/dist/screenshots/tools-modal.png +0 -0
- package/dist/sw.js +49 -0
- package/package.json +108 -0
- package/server/claude-sdk.js +724 -0
- package/server/cli.js +439 -0
- package/server/constants/config.js +5 -0
- package/server/cursor-cli.js +276 -0
- package/server/database/auth.db +0 -0
- package/server/database/db.js +361 -0
- package/server/database/init.sql +52 -0
- package/server/index.js +1944 -0
- package/server/load-env.js +24 -0
- package/server/middleware/auth.js +117 -0
- package/server/openai-codex.js +447 -0
- package/server/projects.js +1679 -0
- package/server/routes/agent.js +1327 -0
- package/server/routes/auth.js +135 -0
- package/server/routes/cli-auth.js +263 -0
- package/server/routes/codex.js +344 -0
- package/server/routes/commands.js +521 -0
- package/server/routes/cursor.js +795 -0
- package/server/routes/git.js +1128 -0
- package/server/routes/mcp-utils.js +48 -0
- package/server/routes/mcp.js +552 -0
- package/server/routes/projects.js +550 -0
- package/server/routes/settings.js +178 -0
- package/server/routes/taskmaster.js +1963 -0
- package/server/routes/user.js +106 -0
- package/server/utils/codexTokenUsage.js +87 -0
- package/server/utils/commandParser.js +303 -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/modelConstants.js +65 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { userDb } from '../database/db.js';
|
|
3
|
+
import { authenticateToken } from '../middleware/auth.js';
|
|
4
|
+
import { getSystemGitConfig } from '../utils/gitConfig.js';
|
|
5
|
+
import { exec } from 'child_process';
|
|
6
|
+
import { promisify } from 'util';
|
|
7
|
+
|
|
8
|
+
const execAsync = promisify(exec);
|
|
9
|
+
const router = express.Router();
|
|
10
|
+
|
|
11
|
+
router.get('/git-config', authenticateToken, async (req, res) => {
|
|
12
|
+
try {
|
|
13
|
+
const userId = req.user.id;
|
|
14
|
+
let gitConfig = userDb.getGitConfig(userId);
|
|
15
|
+
|
|
16
|
+
// If database is empty, try to get from system git config
|
|
17
|
+
if (!gitConfig || (!gitConfig.git_name && !gitConfig.git_email)) {
|
|
18
|
+
const systemConfig = await getSystemGitConfig();
|
|
19
|
+
|
|
20
|
+
// If system has values, save them to database for this user
|
|
21
|
+
if (systemConfig.git_name || systemConfig.git_email) {
|
|
22
|
+
userDb.updateGitConfig(userId, systemConfig.git_name, systemConfig.git_email);
|
|
23
|
+
gitConfig = systemConfig;
|
|
24
|
+
console.log(`Auto-populated git config from system for user ${userId}: ${systemConfig.git_name} <${systemConfig.git_email}>`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
res.json({
|
|
29
|
+
success: true,
|
|
30
|
+
gitName: gitConfig?.git_name || null,
|
|
31
|
+
gitEmail: gitConfig?.git_email || null
|
|
32
|
+
});
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.error('Error getting git config:', error);
|
|
35
|
+
res.status(500).json({ error: 'Failed to get git configuration' });
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Apply git config globally via git config --global
|
|
40
|
+
router.post('/git-config', authenticateToken, async (req, res) => {
|
|
41
|
+
try {
|
|
42
|
+
const userId = req.user.id;
|
|
43
|
+
const { gitName, gitEmail } = req.body;
|
|
44
|
+
|
|
45
|
+
if (!gitName || !gitEmail) {
|
|
46
|
+
return res.status(400).json({ error: 'Git name and email are required' });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Validate email format
|
|
50
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
51
|
+
if (!emailRegex.test(gitEmail)) {
|
|
52
|
+
return res.status(400).json({ error: 'Invalid email format' });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
userDb.updateGitConfig(userId, gitName, gitEmail);
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
await execAsync(`git config --global user.name "${gitName.replace(/"/g, '\\"')}"`);
|
|
59
|
+
await execAsync(`git config --global user.email "${gitEmail.replace(/"/g, '\\"')}"`);
|
|
60
|
+
console.log(`Applied git config globally: ${gitName} <${gitEmail}>`);
|
|
61
|
+
} catch (gitError) {
|
|
62
|
+
console.error('Error applying git config:', gitError);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
res.json({
|
|
66
|
+
success: true,
|
|
67
|
+
gitName,
|
|
68
|
+
gitEmail
|
|
69
|
+
});
|
|
70
|
+
} catch (error) {
|
|
71
|
+
console.error('Error updating git config:', error);
|
|
72
|
+
res.status(500).json({ error: 'Failed to update git configuration' });
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
router.post('/complete-onboarding', authenticateToken, async (req, res) => {
|
|
77
|
+
try {
|
|
78
|
+
const userId = req.user.id;
|
|
79
|
+
userDb.completeOnboarding(userId);
|
|
80
|
+
|
|
81
|
+
res.json({
|
|
82
|
+
success: true,
|
|
83
|
+
message: 'Onboarding completed successfully'
|
|
84
|
+
});
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.error('Error completing onboarding:', error);
|
|
87
|
+
res.status(500).json({ error: 'Failed to complete onboarding' });
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
router.get('/onboarding-status', authenticateToken, async (req, res) => {
|
|
92
|
+
try {
|
|
93
|
+
const userId = req.user.id;
|
|
94
|
+
const hasCompleted = userDb.hasCompletedOnboarding(userId);
|
|
95
|
+
|
|
96
|
+
res.json({
|
|
97
|
+
success: true,
|
|
98
|
+
hasCompletedOnboarding: hasCompleted
|
|
99
|
+
});
|
|
100
|
+
} catch (error) {
|
|
101
|
+
console.error('Error checking onboarding status:', error);
|
|
102
|
+
res.status(500).json({ error: 'Failed to check onboarding status' });
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
export default router;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const DEFAULT_CODEX_CONTEXT_WINDOW = 258400;
|
|
2
|
+
|
|
3
|
+
function toNumber(value) {
|
|
4
|
+
const parsed = Number(value);
|
|
5
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function toNonNegativeInteger(value) {
|
|
9
|
+
const parsed = toNumber(value);
|
|
10
|
+
if (parsed === null || parsed < 0) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return Math.round(parsed);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function toPositiveInteger(value) {
|
|
17
|
+
const parsed = toNonNegativeInteger(value);
|
|
18
|
+
if (parsed === null || parsed <= 0) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
return parsed;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getDefaultCodexContextWindow() {
|
|
25
|
+
const envValue = toPositiveInteger(process.env.CODEX_CONTEXT_WINDOW);
|
|
26
|
+
return envValue || DEFAULT_CODEX_CONTEXT_WINDOW;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function parseCodexTokenCountInfo(info) {
|
|
30
|
+
const defaultWindow = getDefaultCodexContextWindow();
|
|
31
|
+
|
|
32
|
+
if (!info || typeof info !== 'object') {
|
|
33
|
+
return {
|
|
34
|
+
used: 0,
|
|
35
|
+
total: defaultWindow,
|
|
36
|
+
source: 'fallback'
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const lastTurnTokens = toNonNegativeInteger(info.last_token_usage?.total_tokens);
|
|
41
|
+
const totalUsageTokens = toNonNegativeInteger(info.total_token_usage?.total_tokens);
|
|
42
|
+
const contextWindow = toPositiveInteger(info.model_context_window) || defaultWindow;
|
|
43
|
+
|
|
44
|
+
if (lastTurnTokens !== null) {
|
|
45
|
+
return {
|
|
46
|
+
used: lastTurnTokens,
|
|
47
|
+
total: contextWindow,
|
|
48
|
+
source: 'last_token_usage'
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (totalUsageTokens !== null) {
|
|
53
|
+
return {
|
|
54
|
+
used: totalUsageTokens,
|
|
55
|
+
total: contextWindow,
|
|
56
|
+
source: 'total_token_usage'
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
used: 0,
|
|
62
|
+
total: contextWindow,
|
|
63
|
+
source: 'fallback'
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function parseCodexTurnUsage(usage) {
|
|
68
|
+
const defaultWindow = getDefaultCodexContextWindow();
|
|
69
|
+
|
|
70
|
+
if (!usage || typeof usage !== 'object') {
|
|
71
|
+
return {
|
|
72
|
+
used: 0,
|
|
73
|
+
total: defaultWindow,
|
|
74
|
+
source: 'turn_usage'
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const inputTokens = toNonNegativeInteger(usage.input_tokens) || 0;
|
|
79
|
+
const cachedInputTokens = toNonNegativeInteger(usage.cached_input_tokens) || 0;
|
|
80
|
+
const outputTokens = toNonNegativeInteger(usage.output_tokens) || 0;
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
used: inputTokens + cachedInputTokens + outputTokens,
|
|
84
|
+
total: defaultWindow,
|
|
85
|
+
source: 'turn_usage'
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import matter from 'gray-matter';
|
|
2
|
+
import { promises as fs } from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { execFile } from 'child_process';
|
|
5
|
+
import { promisify } from 'util';
|
|
6
|
+
import { parse as parseShellCommand } from 'shell-quote';
|
|
7
|
+
|
|
8
|
+
const execFileAsync = promisify(execFile);
|
|
9
|
+
|
|
10
|
+
// Configuration
|
|
11
|
+
const MAX_INCLUDE_DEPTH = 3;
|
|
12
|
+
const BASH_TIMEOUT = 30000; // 30 seconds
|
|
13
|
+
const BASH_COMMAND_ALLOWLIST = [
|
|
14
|
+
'echo',
|
|
15
|
+
'ls',
|
|
16
|
+
'pwd',
|
|
17
|
+
'date',
|
|
18
|
+
'whoami',
|
|
19
|
+
'git',
|
|
20
|
+
'npm',
|
|
21
|
+
'node',
|
|
22
|
+
'cat',
|
|
23
|
+
'grep',
|
|
24
|
+
'find',
|
|
25
|
+
'task-master'
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Parse a markdown command file and extract frontmatter and content
|
|
30
|
+
* @param {string} content - Raw markdown content
|
|
31
|
+
* @returns {object} Parsed command with data (frontmatter) and content
|
|
32
|
+
*/
|
|
33
|
+
export function parseCommand(content) {
|
|
34
|
+
try {
|
|
35
|
+
const parsed = matter(content);
|
|
36
|
+
return {
|
|
37
|
+
data: parsed.data || {},
|
|
38
|
+
content: parsed.content || '',
|
|
39
|
+
raw: content
|
|
40
|
+
};
|
|
41
|
+
} catch (error) {
|
|
42
|
+
throw new Error(`Failed to parse command: ${error.message}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Replace argument placeholders in content
|
|
48
|
+
* @param {string} content - Content with placeholders
|
|
49
|
+
* @param {string|array} args - Arguments to replace (string or array)
|
|
50
|
+
* @returns {string} Content with replaced arguments
|
|
51
|
+
*/
|
|
52
|
+
export function replaceArguments(content, args) {
|
|
53
|
+
if (!content) return content;
|
|
54
|
+
|
|
55
|
+
let result = content;
|
|
56
|
+
|
|
57
|
+
// Convert args to array if it's a string
|
|
58
|
+
const argsArray = Array.isArray(args) ? args : (args ? [args] : []);
|
|
59
|
+
|
|
60
|
+
// Replace $ARGUMENTS with all arguments joined by space
|
|
61
|
+
const allArgs = argsArray.join(' ');
|
|
62
|
+
result = result.replace(/\$ARGUMENTS/g, allArgs);
|
|
63
|
+
|
|
64
|
+
// Replace positional arguments $1-$9
|
|
65
|
+
for (let i = 1; i <= 9; i++) {
|
|
66
|
+
const regex = new RegExp(`\\$${i}`, 'g');
|
|
67
|
+
const value = argsArray[i - 1] || '';
|
|
68
|
+
result = result.replace(regex, value);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Validate file path to prevent directory traversal
|
|
76
|
+
* @param {string} filePath - Path to validate
|
|
77
|
+
* @param {string} basePath - Base directory path
|
|
78
|
+
* @returns {boolean} True if path is safe
|
|
79
|
+
*/
|
|
80
|
+
export function isPathSafe(filePath, basePath) {
|
|
81
|
+
const resolvedPath = path.resolve(basePath, filePath);
|
|
82
|
+
const resolvedBase = path.resolve(basePath);
|
|
83
|
+
const relative = path.relative(resolvedBase, resolvedPath);
|
|
84
|
+
return (
|
|
85
|
+
relative !== '' &&
|
|
86
|
+
!relative.startsWith('..') &&
|
|
87
|
+
!path.isAbsolute(relative)
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Process file includes in content (@filename syntax)
|
|
93
|
+
* @param {string} content - Content with @filename includes
|
|
94
|
+
* @param {string} basePath - Base directory for resolving file paths
|
|
95
|
+
* @param {number} depth - Current recursion depth
|
|
96
|
+
* @returns {Promise<string>} Content with includes resolved
|
|
97
|
+
*/
|
|
98
|
+
export async function processFileIncludes(content, basePath, depth = 0) {
|
|
99
|
+
if (!content) return content;
|
|
100
|
+
|
|
101
|
+
// Prevent infinite recursion
|
|
102
|
+
if (depth >= MAX_INCLUDE_DEPTH) {
|
|
103
|
+
throw new Error(`Maximum include depth (${MAX_INCLUDE_DEPTH}) exceeded`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Match @filename patterns (at start of line or after whitespace)
|
|
107
|
+
const includePattern = /(?:^|\s)@([^\s]+)/gm;
|
|
108
|
+
const matches = [...content.matchAll(includePattern)];
|
|
109
|
+
|
|
110
|
+
if (matches.length === 0) {
|
|
111
|
+
return content;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
let result = content;
|
|
115
|
+
|
|
116
|
+
for (const match of matches) {
|
|
117
|
+
const fullMatch = match[0];
|
|
118
|
+
const filename = match[1];
|
|
119
|
+
|
|
120
|
+
// Security: prevent directory traversal
|
|
121
|
+
if (!isPathSafe(filename, basePath)) {
|
|
122
|
+
throw new Error(`Invalid file path (directory traversal detected): ${filename}`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
const filePath = path.resolve(basePath, filename);
|
|
127
|
+
const fileContent = await fs.readFile(filePath, 'utf-8');
|
|
128
|
+
|
|
129
|
+
// Recursively process includes in the included file
|
|
130
|
+
const processedContent = await processFileIncludes(fileContent, basePath, depth + 1);
|
|
131
|
+
|
|
132
|
+
// Replace the @filename with the file content
|
|
133
|
+
result = result.replace(fullMatch, fullMatch.startsWith(' ') ? ' ' + processedContent : processedContent);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (error.code === 'ENOENT') {
|
|
136
|
+
throw new Error(`File not found: ${filename}`);
|
|
137
|
+
}
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Validate that a command and its arguments are safe
|
|
147
|
+
* @param {string} commandString - Command string to validate
|
|
148
|
+
* @returns {{ allowed: boolean, command: string, args: string[], error?: string }} Validation result
|
|
149
|
+
*/
|
|
150
|
+
export function validateCommand(commandString) {
|
|
151
|
+
const trimmedCommand = commandString.trim();
|
|
152
|
+
if (!trimmedCommand) {
|
|
153
|
+
return { allowed: false, command: '', args: [], error: 'Empty command' };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Parse the command using shell-quote to handle quotes properly
|
|
157
|
+
const parsed = parseShellCommand(trimmedCommand);
|
|
158
|
+
|
|
159
|
+
// Check for shell operators or control structures
|
|
160
|
+
const hasOperators = parsed.some(token =>
|
|
161
|
+
typeof token === 'object' && token.op
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
if (hasOperators) {
|
|
165
|
+
return {
|
|
166
|
+
allowed: false,
|
|
167
|
+
command: '',
|
|
168
|
+
args: [],
|
|
169
|
+
error: 'Shell operators (&&, ||, |, ;, etc.) are not allowed'
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Extract command and args (all should be strings after validation)
|
|
174
|
+
const tokens = parsed.filter(token => typeof token === 'string');
|
|
175
|
+
|
|
176
|
+
if (tokens.length === 0) {
|
|
177
|
+
return { allowed: false, command: '', args: [], error: 'No valid command found' };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const [command, ...args] = tokens;
|
|
181
|
+
|
|
182
|
+
// Extract just the command name (remove path if present)
|
|
183
|
+
const commandName = path.basename(command);
|
|
184
|
+
|
|
185
|
+
// Check if command exactly matches allowlist (no prefix matching)
|
|
186
|
+
const isAllowed = BASH_COMMAND_ALLOWLIST.includes(commandName);
|
|
187
|
+
|
|
188
|
+
if (!isAllowed) {
|
|
189
|
+
return {
|
|
190
|
+
allowed: false,
|
|
191
|
+
command: commandName,
|
|
192
|
+
args,
|
|
193
|
+
error: `Command '${commandName}' is not in the allowlist`
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Validate arguments don't contain dangerous metacharacters
|
|
198
|
+
const dangerousPattern = /[;&|`$()<>{}[\]\\]/;
|
|
199
|
+
for (const arg of args) {
|
|
200
|
+
if (dangerousPattern.test(arg)) {
|
|
201
|
+
return {
|
|
202
|
+
allowed: false,
|
|
203
|
+
command: commandName,
|
|
204
|
+
args,
|
|
205
|
+
error: `Argument contains dangerous characters: ${arg}`
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return { allowed: true, command: commandName, args };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Backward compatibility: Check if command is allowed (deprecated)
|
|
215
|
+
* @deprecated Use validateCommand() instead for better security
|
|
216
|
+
* @param {string} command - Command to validate
|
|
217
|
+
* @returns {boolean} True if command is allowed
|
|
218
|
+
*/
|
|
219
|
+
export function isBashCommandAllowed(command) {
|
|
220
|
+
const result = validateCommand(command);
|
|
221
|
+
return result.allowed;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Sanitize bash command output
|
|
226
|
+
* @param {string} output - Raw command output
|
|
227
|
+
* @returns {string} Sanitized output
|
|
228
|
+
*/
|
|
229
|
+
export function sanitizeOutput(output) {
|
|
230
|
+
if (!output) return '';
|
|
231
|
+
|
|
232
|
+
// Remove control characters except \t, \n, \r
|
|
233
|
+
return [...output]
|
|
234
|
+
.filter(ch => {
|
|
235
|
+
const code = ch.charCodeAt(0);
|
|
236
|
+
return code === 9 // \t
|
|
237
|
+
|| code === 10 // \n
|
|
238
|
+
|| code === 13 // \r
|
|
239
|
+
|| (code >= 32 && code !== 127);
|
|
240
|
+
})
|
|
241
|
+
.join('');
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Process bash commands in content (!command syntax)
|
|
246
|
+
* @param {string} content - Content with !command syntax
|
|
247
|
+
* @param {object} options - Options for bash execution
|
|
248
|
+
* @returns {Promise<string>} Content with bash commands executed and replaced
|
|
249
|
+
*/
|
|
250
|
+
export async function processBashCommands(content, options = {}) {
|
|
251
|
+
if (!content) return content;
|
|
252
|
+
|
|
253
|
+
const { cwd = process.cwd(), timeout = BASH_TIMEOUT } = options;
|
|
254
|
+
|
|
255
|
+
// Match !command patterns (at start of line or after whitespace)
|
|
256
|
+
const commandPattern = /(?:^|\n)!(.+?)(?=\n|$)/g;
|
|
257
|
+
const matches = [...content.matchAll(commandPattern)];
|
|
258
|
+
|
|
259
|
+
if (matches.length === 0) {
|
|
260
|
+
return content;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let result = content;
|
|
264
|
+
|
|
265
|
+
for (const match of matches) {
|
|
266
|
+
const fullMatch = match[0];
|
|
267
|
+
const commandString = match[1].trim();
|
|
268
|
+
|
|
269
|
+
// Security: validate command and parse args
|
|
270
|
+
const validation = validateCommand(commandString);
|
|
271
|
+
|
|
272
|
+
if (!validation.allowed) {
|
|
273
|
+
throw new Error(`Command not allowed: ${commandString} - ${validation.error}`);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
try {
|
|
277
|
+
// Execute without shell using execFile with parsed args
|
|
278
|
+
const { stdout, stderr } = await execFileAsync(
|
|
279
|
+
validation.command,
|
|
280
|
+
validation.args,
|
|
281
|
+
{
|
|
282
|
+
cwd,
|
|
283
|
+
timeout,
|
|
284
|
+
maxBuffer: 1024 * 1024, // 1MB max output
|
|
285
|
+
shell: false, // IMPORTANT: No shell interpretation
|
|
286
|
+
env: { ...process.env, PATH: process.env.PATH } // Inherit PATH for finding commands
|
|
287
|
+
}
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
const output = sanitizeOutput(stdout || stderr || '');
|
|
291
|
+
|
|
292
|
+
// Replace the !command with the output
|
|
293
|
+
result = result.replace(fullMatch, fullMatch.startsWith('\n') ? '\n' + output : output);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
if (error.killed) {
|
|
296
|
+
throw new Error(`Command timeout: ${commandString}`);
|
|
297
|
+
}
|
|
298
|
+
throw new Error(`Command failed: ${commandString} - ${error.message}`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { exec } from 'child_process';
|
|
2
|
+
import { promisify } from 'util';
|
|
3
|
+
|
|
4
|
+
const execAsync = promisify(exec);
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Read git configuration from system's global git config
|
|
8
|
+
* @returns {Promise<{git_name: string|null, git_email: string|null}>}
|
|
9
|
+
*/
|
|
10
|
+
export async function getSystemGitConfig() {
|
|
11
|
+
try {
|
|
12
|
+
const [nameResult, emailResult] = await Promise.all([
|
|
13
|
+
execAsync('git config --global user.name').catch(() => ({ stdout: '' })),
|
|
14
|
+
execAsync('git config --global user.email').catch(() => ({ stdout: '' }))
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
git_name: nameResult.stdout.trim() || null,
|
|
19
|
+
git_email: emailResult.stdout.trim() || null
|
|
20
|
+
};
|
|
21
|
+
} catch (error) {
|
|
22
|
+
return { git_name: null, git_email: null };
|
|
23
|
+
}
|
|
24
|
+
}
|