openmagic 0.33.0 → 0.33.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/dist/cli.js CHANGED
@@ -82,9 +82,12 @@ import {
82
82
  readdirSync,
83
83
  copyFileSync,
84
84
  mkdirSync as mkdirSync2,
85
- realpathSync
85
+ realpathSync,
86
+ rmSync
86
87
  } from "fs";
87
88
  import { join as join2, resolve, relative, dirname, extname } from "path";
89
+ import { tmpdir } from "os";
90
+ import { createHash } from "crypto";
88
91
  var IGNORED_DIRS = /* @__PURE__ */ new Set([
89
92
  "node_modules",
90
93
  ".git",
@@ -145,6 +148,25 @@ function readFileSafe(filePath, roots) {
145
148
  return { error: `Failed to read file: ${e.message}` };
146
149
  }
147
150
  }
151
+ var BACKUP_DIR = join2(tmpdir(), "openmagic-backups");
152
+ var backupMap = /* @__PURE__ */ new Map();
153
+ function getBackupPath(filePath) {
154
+ const hash = createHash("md5").update(resolve(filePath)).digest("hex").slice(0, 12);
155
+ const name = filePath.split(/[/\\]/).pop() || "file";
156
+ return join2(BACKUP_DIR, `${hash}_${name}`);
157
+ }
158
+ function getBackupForFile(filePath) {
159
+ return backupMap.get(resolve(filePath));
160
+ }
161
+ function cleanupBackups() {
162
+ try {
163
+ if (existsSync2(BACKUP_DIR)) {
164
+ rmSync(BACKUP_DIR, { recursive: true, force: true });
165
+ }
166
+ } catch {
167
+ }
168
+ backupMap.clear();
169
+ }
148
170
  function writeFileSafe(filePath, content, roots) {
149
171
  if (!isPathSafe(filePath, roots)) {
150
172
  return { ok: false, error: "Path is outside allowed roots" };
@@ -152,8 +174,10 @@ function writeFileSafe(filePath, content, roots) {
152
174
  try {
153
175
  let backupPath;
154
176
  if (existsSync2(filePath)) {
155
- backupPath = filePath + ".openmagic-backup";
177
+ if (!existsSync2(BACKUP_DIR)) mkdirSync2(BACKUP_DIR, { recursive: true });
178
+ backupPath = getBackupPath(filePath);
156
179
  copyFileSync(filePath, backupPath);
180
+ backupMap.set(resolve(filePath), backupPath);
157
181
  }
158
182
  const dir = dirname(filePath);
159
183
  if (!existsSync2(dir)) {
@@ -165,13 +189,14 @@ function writeFileSafe(filePath, content, roots) {
165
189
  return { ok: false, error: `Failed to write file: ${e.message}` };
166
190
  }
167
191
  }
192
+ var MAX_LIST_ENTRIES = 2e3;
168
193
  function listFiles(rootPath, roots, maxDepth = 4) {
169
194
  if (!isPathSafe(rootPath, roots)) {
170
195
  return [];
171
196
  }
172
197
  const entries = [];
173
198
  function walk(dir, depth) {
174
- if (depth > maxDepth) return;
199
+ if (depth > maxDepth || entries.length >= MAX_LIST_ENTRIES) return;
175
200
  let items;
176
201
  try {
177
202
  items = readdirSync(dir);
@@ -179,6 +204,7 @@ function listFiles(rootPath, roots, maxDepth = 4) {
179
204
  return;
180
205
  }
181
206
  for (const item of items) {
207
+ if (entries.length >= MAX_LIST_ENTRIES) return;
182
208
  if (IGNORED_DIRS.has(item)) continue;
183
209
  if (item.startsWith(".") && item !== ".env.example") continue;
184
210
  const fullPath = join2(dir, item);
@@ -227,12 +253,15 @@ var GREP_EXTENSIONS = /* @__PURE__ */ new Set([
227
253
  ".py",
228
254
  ".rb"
229
255
  ]);
256
+ var MAX_GREP_FILE_SIZE = 256 * 1024;
257
+ var MAX_GREP_FILES_SCANNED = 500;
230
258
  function grepFiles(pattern, searchRoot, roots, maxResults = 30) {
231
259
  if (!isPathSafe(searchRoot, roots)) return [];
232
260
  const results = [];
233
261
  const lowerPattern = pattern.toLowerCase();
262
+ let filesScanned = 0;
234
263
  function walk(dir, depth) {
235
- if (depth > 5 || results.length >= maxResults) return;
264
+ if (depth > 6 || results.length >= maxResults || filesScanned >= MAX_GREP_FILES_SCANNED) return;
236
265
  let items;
237
266
  try {
238
267
  items = readdirSync(dir);
@@ -240,7 +269,7 @@ function grepFiles(pattern, searchRoot, roots, maxResults = 30) {
240
269
  return;
241
270
  }
242
271
  for (const item of items) {
243
- if (results.length >= maxResults) return;
272
+ if (results.length >= maxResults || filesScanned >= MAX_GREP_FILES_SCANNED) return;
244
273
  if (IGNORED_DIRS.has(item) || item.startsWith(".") && item !== ".env.example") continue;
245
274
  const fullPath = join2(dir, item);
246
275
  let stat;
@@ -255,6 +284,8 @@ function grepFiles(pattern, searchRoot, roots, maxResults = 30) {
255
284
  } else if (stat.isFile()) {
256
285
  const ext = extname(item).toLowerCase();
257
286
  if (!GREP_EXTENSIONS.has(ext)) continue;
287
+ if (stat.size > MAX_GREP_FILE_SIZE) continue;
288
+ filesScanned++;
258
289
  try {
259
290
  const content = readFileSync2(fullPath, "utf-8");
260
291
  const lines = content.split("\n");
@@ -1706,6 +1737,30 @@ async function handleMessage(ws, msg, state, roots) {
1706
1737
  }
1707
1738
  break;
1708
1739
  }
1740
+ case "fs.undo": {
1741
+ const payload = msg.payload;
1742
+ if (!payload?.path) {
1743
+ sendError(ws, "invalid_payload", "Missing path", msg.id);
1744
+ break;
1745
+ }
1746
+ const backupPath = getBackupForFile(payload.path);
1747
+ if (!backupPath) {
1748
+ sendError(ws, "fs_error", "No backup found", msg.id);
1749
+ break;
1750
+ }
1751
+ try {
1752
+ const backupContent = readFileSync3(backupPath, "utf-8");
1753
+ const writeResult = writeFileSafe(payload.path, backupContent, roots);
1754
+ if (!writeResult.ok) {
1755
+ sendError(ws, "fs_error", writeResult.error || "Undo failed", msg.id);
1756
+ break;
1757
+ }
1758
+ send(ws, { id: msg.id, type: "fs.undone", payload: { path: payload.path, ok: true } });
1759
+ } catch (e) {
1760
+ sendError(ws, "fs_error", `Backup read failed: ${e.message}`, msg.id);
1761
+ }
1762
+ break;
1763
+ }
1709
1764
  case "fs.list": {
1710
1765
  const payload = msg.payload;
1711
1766
  const root = payload?.root || roots[0];
@@ -2392,6 +2447,7 @@ program.name("openmagic").description("AI-powered coding toolbar for any web app
2392
2447
  const shutdown = () => {
2393
2448
  console.log("");
2394
2449
  console.log(chalk.dim(" Shutting down OpenMagic..."));
2450
+ cleanupBackups();
2395
2451
  proxyServer.close();
2396
2452
  process.exit(0);
2397
2453
  };