upfynai-code 2.7.5 → 2.8.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,136 @@
1
+ /**
2
+ * Shared utilities for agent command handlers.
3
+ * Used by both CLI relay clients and backend server.
4
+ */
5
+ import { spawn } from 'child_process';
6
+ import { promises as fsPromises } from 'fs';
7
+ import os from 'os';
8
+ import path from 'path';
9
+
10
+ /**
11
+ * Execute a shell command and return stdout.
12
+ * @param {string} cmd - Command to run
13
+ * @param {string[]} args - Command arguments
14
+ * @param {object} options - { cwd, env, timeout }
15
+ * @returns {Promise<string>} stdout
16
+ */
17
+ export function execCommand(cmd, args, options = {}) {
18
+ return new Promise((resolve, reject) => {
19
+ const proc = spawn(cmd, args, {
20
+ shell: true,
21
+ cwd: options.cwd || os.homedir(),
22
+ env: { ...process.env, ...options.env },
23
+ stdio: ['pipe', 'pipe', 'pipe'],
24
+ });
25
+
26
+ let stdout = '';
27
+ let stderr = '';
28
+ proc.stdout.on('data', (d) => { stdout += d; });
29
+ proc.stderr.on('data', (d) => { stderr += d; });
30
+
31
+ const timeout = setTimeout(() => {
32
+ proc.kill();
33
+ reject(new Error('Command timed out'));
34
+ }, options.timeout || 30000);
35
+
36
+ proc.on('close', (code) => {
37
+ clearTimeout(timeout);
38
+ if (code === 0) resolve(stdout);
39
+ else reject(new Error(stderr || `Exit code ${code}`));
40
+ });
41
+
42
+ proc.on('error', (err) => {
43
+ clearTimeout(timeout);
44
+ reject(err);
45
+ });
46
+ });
47
+ }
48
+
49
+ /**
50
+ * Build a file tree for a directory (recursive).
51
+ * @param {string} dirPath - Directory to scan
52
+ * @param {number} maxDepth - Maximum recursion depth
53
+ * @param {number} currentDepth - Current depth (internal)
54
+ * @param {object} options - { maxEntries, includeStats, skipDotfilesAtRoot }
55
+ * @returns {Promise<Array>} File tree items
56
+ */
57
+ export async function buildFileTree(dirPath, maxDepth, currentDepth = 0, options = {}) {
58
+ const { maxEntries = 200, includeStats = false, skipDotfilesAtRoot = false } = options;
59
+ if (currentDepth >= maxDepth) return [];
60
+
61
+ const SKIP_DIRS = new Set(['node_modules', '.git', 'dist', 'build', '.svn', '.hg']);
62
+
63
+ try {
64
+ const entries = await fsPromises.readdir(dirPath, { withFileTypes: true });
65
+ const items = [];
66
+ for (const entry of entries.slice(0, maxEntries)) {
67
+ if (SKIP_DIRS.has(entry.name)) continue;
68
+ if (entry.name.startsWith('.') && skipDotfilesAtRoot && currentDepth === 0) continue;
69
+
70
+ const itemPath = path.join(dirPath, entry.name);
71
+ const item = {
72
+ name: entry.name,
73
+ path: itemPath,
74
+ type: entry.isDirectory() ? 'directory' : 'file',
75
+ };
76
+
77
+ if (includeStats) {
78
+ try {
79
+ const stats = await fsPromises.stat(itemPath);
80
+ item.size = stats.size;
81
+ item.modified = stats.mtime.toISOString();
82
+ } catch { /* ignore stat errors */ }
83
+ }
84
+
85
+ if (entry.isDirectory() && currentDepth < maxDepth - 1) {
86
+ item.children = await buildFileTree(itemPath, maxDepth, currentDepth + 1, options);
87
+ }
88
+ items.push(item);
89
+ }
90
+ return items;
91
+ } catch {
92
+ return [];
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Resolve a path that may start with ~ to an absolute path.
98
+ * @param {string} inputPath - Path to resolve
99
+ * @returns {string} Resolved absolute path
100
+ */
101
+ export function resolveTildePath(inputPath) {
102
+ if (!inputPath) return os.homedir();
103
+ if (inputPath === '~') return os.homedir();
104
+ if (inputPath.startsWith('~/') || inputPath.startsWith('~\\')) {
105
+ return path.join(os.homedir(), inputPath.slice(2));
106
+ }
107
+ return path.resolve(inputPath);
108
+ }
109
+
110
+ /** Blocked paths for file reads (security) */
111
+ export const BLOCKED_READ_PATTERNS = ['/etc/shadow', '/etc/passwd', '.ssh/id_rsa', '.ssh/id_ed25519', '/.env'];
112
+
113
+ /** Blocked paths for file writes (security) */
114
+ export const BLOCKED_WRITE_PATTERNS = [
115
+ '/etc/', '/usr/bin/', '/usr/sbin/',
116
+ '/windows/system32', '/windows/syswow64', '/program files',
117
+ '.ssh/', '/.env',
118
+ ];
119
+
120
+ /** Dangerous shell patterns to block */
121
+ export const DANGEROUS_SHELL_PATTERNS = [
122
+ 'rm -rf /', 'mkfs', 'dd if=', ':(){', 'fork bomb', '> /dev/sd',
123
+ 'format c:', 'format d:', 'format e:', 'del /s /q c:\\',
124
+ 'rd /s /q c:\\', 'reg delete', 'bcdedit',
125
+ ];
126
+
127
+ /**
128
+ * Check if a normalized path matches any blocked patterns.
129
+ * @param {string} normalizedPath - Absolute path (resolved)
130
+ * @param {string[]} patterns - Blocked patterns to check
131
+ * @returns {boolean}
132
+ */
133
+ export function isBlockedPath(normalizedPath, patterns) {
134
+ const lower = normalizedPath.toLowerCase().replace(/\\/g, '/');
135
+ return patterns.some(b => lower.includes(b));
136
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "upfynai-code",
3
- "version": "2.7.5",
3
+ "version": "2.8.0",
4
4
  "description": "Unified AI coding interface — access AI chat, terminal, file explorer, git, and visual canvas from any browser. Connect your local machine and code from anywhere.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -11,11 +11,13 @@
11
11
  "bin/",
12
12
  "src/",
13
13
  "dist/",
14
+ "scripts/",
14
15
  "README.md",
15
16
  "LICENSE"
16
17
  ],
17
18
  "scripts": {
18
- "start": "node bin/cli.js"
19
+ "start": "node bin/cli.js",
20
+ "prepublishOnly": "node scripts/prepublish.js"
19
21
  },
20
22
  "keywords": [
21
23
  "upfyn",
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Prepublish script — copies shared agents into dist/ for npm distribution.
4
+ *
5
+ * In the monorepo, connect.js imports from ../../shared/agents/.
6
+ * For npm, we copy those files into dist/agents/ and connect.js
7
+ * resolves them at runtime.
8
+ */
9
+ import { cpSync, mkdirSync, rmSync, existsSync } from 'fs';
10
+ import { join, dirname } from 'path';
11
+ import { fileURLToPath } from 'url';
12
+
13
+ const __dirname = dirname(fileURLToPath(import.meta.url));
14
+ const cliRoot = join(__dirname, '..');
15
+ const distAgents = join(cliRoot, 'dist', 'agents');
16
+ const sharedAgents = join(cliRoot, '..', 'shared', 'agents');
17
+
18
+ // Clean
19
+ if (existsSync(distAgents)) rmSync(distAgents, { recursive: true });
20
+ mkdirSync(distAgents, { recursive: true });
21
+
22
+ // Copy all agent files
23
+ const files = [
24
+ 'index.js', 'utils.js',
25
+ 'claude.js', 'codex.js', 'cursor.js',
26
+ 'shell.js', 'files.js', 'git.js', 'exec.js', 'detect.js',
27
+ ];
28
+
29
+ let copied = 0;
30
+ for (const file of files) {
31
+ const src = join(sharedAgents, file);
32
+ const dest = join(distAgents, file);
33
+ if (existsSync(src)) {
34
+ cpSync(src, dest);
35
+ copied++;
36
+ } else {
37
+ console.warn(` [WARN] Missing: ${file}`);
38
+ }
39
+ }
40
+
41
+ console.log(` [prepublish] Copied ${copied} agent files to dist/agents/`);