@wonderwhy-er/desktop-commander 0.2.10 → 0.2.12

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,467 @@
1
+ import { spawn } from 'child_process';
2
+ import { rgPath } from '@vscode/ripgrep';
3
+ import path from 'path';
4
+ import { validatePath } from './tools/filesystem.js';
5
+ import { capture } from './utils/capture.js';
6
+ /**
7
+ * Search Session Manager - handles ripgrep processes like terminal sessions
8
+ * Supports both file search and content search with progressive results
9
+ */ export class SearchManager {
10
+ constructor() {
11
+ this.sessions = new Map();
12
+ this.sessionCounter = 0;
13
+ }
14
+ /**
15
+ * Start a new search session (like start_process)
16
+ * Returns immediately with initial state and results
17
+ */
18
+ async startSearch(options) {
19
+ const sessionId = `search_${++this.sessionCounter}_${Date.now()}`;
20
+ // Validate path first
21
+ const validPath = await validatePath(options.rootPath);
22
+ // Build ripgrep arguments
23
+ const args = this.buildRipgrepArgs({ ...options, rootPath: validPath });
24
+ // Start ripgrep process
25
+ const rgProcess = spawn(rgPath, args);
26
+ if (!rgProcess.pid) {
27
+ throw new Error('Failed to start ripgrep process');
28
+ }
29
+ // Create session
30
+ const session = {
31
+ id: sessionId,
32
+ process: rgProcess,
33
+ results: [],
34
+ isComplete: false,
35
+ isError: false,
36
+ startTime: Date.now(),
37
+ lastReadTime: Date.now(),
38
+ options,
39
+ buffer: '',
40
+ totalMatches: 0,
41
+ totalContextLines: 0
42
+ };
43
+ this.sessions.set(sessionId, session);
44
+ // Set up process event handlers
45
+ this.setupProcessHandlers(session);
46
+ // Start cleanup interval now that we have a session
47
+ startCleanupIfNeeded();
48
+ // Set up timeout if specified and auto-terminate
49
+ // For exact filename searches, use a shorter default timeout
50
+ const timeoutMs = options.timeout ?? (this.isExactFilename(options.pattern) ? 1500 : undefined);
51
+ let killTimer = null;
52
+ if (timeoutMs) {
53
+ killTimer = setTimeout(() => {
54
+ if (!session.isComplete && !session.process.killed) {
55
+ session.process.kill('SIGTERM');
56
+ }
57
+ }, timeoutMs);
58
+ }
59
+ // Clear timer on process completion
60
+ session.process.once('close', () => {
61
+ if (killTimer) {
62
+ clearTimeout(killTimer);
63
+ killTimer = null;
64
+ }
65
+ });
66
+ session.process.once('error', () => {
67
+ if (killTimer) {
68
+ clearTimeout(killTimer);
69
+ killTimer = null;
70
+ }
71
+ });
72
+ capture('search_session_started', {
73
+ sessionId,
74
+ searchType: options.searchType,
75
+ hasTimeout: !!timeoutMs,
76
+ timeoutMs,
77
+ requestedPath: options.rootPath,
78
+ validatedPath: validPath
79
+ });
80
+ // Wait for first chunk of data or early completion instead of fixed delay
81
+ const firstChunk = new Promise(resolve => {
82
+ const onData = () => {
83
+ session.process.stdout?.off('data', onData);
84
+ resolve();
85
+ };
86
+ session.process.stdout?.once('data', onData);
87
+ setTimeout(resolve, 40); // cap at 40ms instead of 50-100ms
88
+ });
89
+ await firstChunk;
90
+ return {
91
+ sessionId,
92
+ isComplete: session.isComplete,
93
+ isError: session.isError,
94
+ results: [...session.results],
95
+ totalResults: session.totalMatches,
96
+ runtime: Date.now() - session.startTime
97
+ };
98
+ }
99
+ /**
100
+ * Read search results with offset-based pagination (like read_file)
101
+ * Supports both range reading and tail behavior
102
+ */
103
+ readSearchResults(sessionId, offset = 0, length = 100) {
104
+ const session = this.sessions.get(sessionId);
105
+ if (!session) {
106
+ throw new Error(`Search session ${sessionId} not found`);
107
+ }
108
+ // Get all results (excluding internal markers)
109
+ const allResults = session.results.filter(r => r.file !== '__LAST_READ_MARKER__');
110
+ // Handle negative offsets (tail behavior) - like file reading
111
+ if (offset < 0) {
112
+ const tailCount = Math.abs(offset);
113
+ const tailResults = allResults.slice(-tailCount);
114
+ return {
115
+ results: tailResults,
116
+ returnedCount: tailResults.length,
117
+ totalResults: session.totalMatches + session.totalContextLines,
118
+ totalMatches: session.totalMatches, // Actual matches only
119
+ isComplete: session.isComplete,
120
+ isError: session.isError && !!session.error?.trim(), // Only error if we have actual errors
121
+ error: session.error?.trim() || undefined,
122
+ hasMoreResults: false, // Tail always returns what's available
123
+ runtime: Date.now() - session.startTime
124
+ };
125
+ }
126
+ // Handle positive offsets (range behavior) - like file reading
127
+ const slicedResults = allResults.slice(offset, offset + length);
128
+ const hasMoreResults = offset + length < allResults.length || !session.isComplete;
129
+ session.lastReadTime = Date.now();
130
+ return {
131
+ results: slicedResults,
132
+ returnedCount: slicedResults.length,
133
+ totalResults: session.totalMatches + session.totalContextLines,
134
+ totalMatches: session.totalMatches, // Actual matches only
135
+ isComplete: session.isComplete,
136
+ isError: session.isError && !!session.error?.trim(), // Only error if we have actual errors
137
+ error: session.error?.trim() || undefined,
138
+ hasMoreResults,
139
+ runtime: Date.now() - session.startTime
140
+ };
141
+ }
142
+ /**
143
+ * Terminate a search session (like force_terminate)
144
+ */
145
+ terminateSearch(sessionId) {
146
+ const session = this.sessions.get(sessionId);
147
+ if (!session) {
148
+ return false;
149
+ }
150
+ if (!session.process.killed) {
151
+ session.process.kill('SIGTERM');
152
+ capture('search_session_terminated', { sessionId });
153
+ }
154
+ // Don't delete session immediately - let user read final results
155
+ // It will be cleaned up by cleanup process
156
+ return true;
157
+ }
158
+ /**
159
+ * Get list of active search sessions (like list_sessions)
160
+ */
161
+ listSearchSessions() {
162
+ return Array.from(this.sessions.values()).map(session => ({
163
+ id: session.id,
164
+ searchType: session.options.searchType,
165
+ pattern: session.options.pattern,
166
+ isComplete: session.isComplete,
167
+ isError: session.isError,
168
+ runtime: Date.now() - session.startTime,
169
+ totalResults: session.totalMatches + session.totalContextLines
170
+ }));
171
+ }
172
+ /**
173
+ * Clean up completed sessions older than specified time
174
+ * Called automatically by cleanup interval
175
+ */
176
+ cleanupSessions(maxAge = 5 * 60 * 1000) {
177
+ const cutoffTime = Date.now() - maxAge;
178
+ for (const [sessionId, session] of this.sessions) {
179
+ if (session.isComplete && session.lastReadTime < cutoffTime) {
180
+ this.sessions.delete(sessionId);
181
+ capture('search_session_cleaned_up', { sessionId });
182
+ }
183
+ }
184
+ }
185
+ /**
186
+ * Get total number of active sessions (excluding completed ones)
187
+ */
188
+ getActiveSessionCount() {
189
+ return Array.from(this.sessions.values()).filter(session => !session.isComplete).length;
190
+ }
191
+ /**
192
+ * Detect if pattern looks like an exact filename
193
+ * (has file extension and no glob wildcards)
194
+ */
195
+ isExactFilename(pattern) {
196
+ return /\.[a-zA-Z0-9]+$/.test(pattern) &&
197
+ !this.isGlobPattern(pattern);
198
+ }
199
+ /**
200
+ * Detect if pattern contains glob wildcards
201
+ */
202
+ isGlobPattern(pattern) {
203
+ return pattern.includes('*') ||
204
+ pattern.includes('?') ||
205
+ pattern.includes('[') ||
206
+ pattern.includes('{') ||
207
+ pattern.includes(']') ||
208
+ pattern.includes('}');
209
+ }
210
+ buildRipgrepArgs(options) {
211
+ const args = [];
212
+ if (options.searchType === 'content') {
213
+ // Content search mode
214
+ args.push('--json', '--line-number');
215
+ if (options.contextLines && options.contextLines > 0) {
216
+ args.push('-C', options.contextLines.toString());
217
+ }
218
+ }
219
+ else {
220
+ // File search mode
221
+ args.push('--files');
222
+ }
223
+ // Case-insensitive: content searches use -i flag, file searches use --iglob
224
+ if (options.searchType === 'content' && options.ignoreCase !== false) {
225
+ args.push('-i');
226
+ }
227
+ if (options.includeHidden) {
228
+ args.push('--hidden');
229
+ }
230
+ if (options.maxResults && options.maxResults > 0) {
231
+ args.push('-m', options.maxResults.toString());
232
+ }
233
+ // File pattern filtering (for file type restrictions like *.js, *.d.ts)
234
+ if (options.filePattern) {
235
+ const patterns = options.filePattern
236
+ .split('|')
237
+ .map(p => p.trim())
238
+ .filter(Boolean);
239
+ for (const p of patterns) {
240
+ if (options.searchType === 'content') {
241
+ args.push('-g', p);
242
+ }
243
+ else {
244
+ // For file search: use --iglob for case-insensitive or --glob for case-sensitive
245
+ if (options.ignoreCase !== false) {
246
+ args.push('--iglob', p);
247
+ }
248
+ else {
249
+ args.push('--glob', p);
250
+ }
251
+ }
252
+ }
253
+ }
254
+ // Handle the main search pattern
255
+ if (options.searchType === 'files') {
256
+ // For file search: determine how to treat the pattern
257
+ const globFlag = options.ignoreCase !== false ? '--iglob' : '--glob';
258
+ if (this.isExactFilename(options.pattern)) {
259
+ // Exact filename: use appropriate glob flag with the exact pattern
260
+ args.push(globFlag, options.pattern);
261
+ }
262
+ else if (this.isGlobPattern(options.pattern)) {
263
+ // Already a glob pattern: use appropriate glob flag as-is
264
+ args.push(globFlag, options.pattern);
265
+ }
266
+ else {
267
+ // Substring/fuzzy search: wrap with wildcards
268
+ args.push(globFlag, `*${options.pattern}*`);
269
+ }
270
+ // Add the root path for file mode
271
+ args.push(options.rootPath);
272
+ }
273
+ else {
274
+ // Content search: terminate options before the pattern to prevent
275
+ // patterns starting with '-' being interpreted as flags
276
+ args.push('--', options.pattern, options.rootPath);
277
+ }
278
+ return args;
279
+ }
280
+ setupProcessHandlers(session) {
281
+ const { process } = session;
282
+ process.stdout?.on('data', (data) => {
283
+ session.buffer += data.toString();
284
+ this.processBufferedOutput(session);
285
+ });
286
+ process.stderr?.on('data', (data) => {
287
+ const errorText = data.toString();
288
+ // Filter meaningful errors
289
+ const filteredErrors = errorText
290
+ .split('\n')
291
+ .filter(line => {
292
+ const trimmed = line.trim();
293
+ // Skip empty lines and lines with just symbols/numbers/colons
294
+ if (!trimmed || trimmed.match(/^[\)\(\s\d:]*$/))
295
+ return false;
296
+ // Skip all ripgrep system errors that start with "rg:"
297
+ if (trimmed.startsWith('rg:'))
298
+ return false;
299
+ return true;
300
+ });
301
+ // Only add to session.error if there are actual meaningful errors after filtering
302
+ if (filteredErrors.length > 0) {
303
+ const meaningfulErrors = filteredErrors.join('\n').trim();
304
+ if (meaningfulErrors) {
305
+ session.error = (session.error || '') + meaningfulErrors + '\n';
306
+ capture('search_session_error', {
307
+ sessionId: session.id,
308
+ error: meaningfulErrors.substring(0, 200)
309
+ });
310
+ }
311
+ }
312
+ });
313
+ process.on('close', (code) => {
314
+ // Process any remaining buffer content
315
+ if (session.buffer.trim()) {
316
+ this.processBufferedOutput(session, true);
317
+ }
318
+ session.isComplete = true;
319
+ // Only treat as error if:
320
+ // 1. Unexpected exit code (not 0, 1, or 2) AND
321
+ // 2. We have meaningful errors after filtering AND
322
+ // 3. We found no results at all
323
+ if (code !== 0 && code !== 1 && code !== 2) {
324
+ // Codes 0=success, 1=no matches, 2=some files couldn't be searched
325
+ if (session.error?.trim() && session.totalMatches === 0) {
326
+ session.isError = true;
327
+ session.error = session.error || `ripgrep exited with code ${code}`;
328
+ }
329
+ }
330
+ // If we have results, don't mark as error even if there were permission issues
331
+ if (session.totalMatches > 0) {
332
+ session.isError = false;
333
+ }
334
+ capture('search_session_completed', {
335
+ sessionId: session.id,
336
+ exitCode: code,
337
+ totalResults: session.totalMatches + session.totalContextLines,
338
+ totalMatches: session.totalMatches,
339
+ runtime: Date.now() - session.startTime
340
+ });
341
+ // Rely on cleanupSessions(maxAge) only; no per-session timer
342
+ });
343
+ process.on('error', (error) => {
344
+ session.isComplete = true;
345
+ session.isError = true;
346
+ session.error = `Process error: ${error.message}`;
347
+ capture('search_session_process_error', {
348
+ sessionId: session.id,
349
+ error: error.message
350
+ });
351
+ // Rely on cleanupSessions(maxAge) only; no per-session timer
352
+ });
353
+ }
354
+ processBufferedOutput(session, isFinal = false) {
355
+ const lines = session.buffer.split('\n');
356
+ // Keep the last incomplete line in the buffer unless this is final processing
357
+ if (!isFinal) {
358
+ session.buffer = lines.pop() || '';
359
+ }
360
+ else {
361
+ session.buffer = '';
362
+ }
363
+ for (const line of lines) {
364
+ if (!line.trim())
365
+ continue;
366
+ const result = this.parseLine(line, session.options.searchType);
367
+ if (result) {
368
+ session.results.push(result);
369
+ // Separate counting of matches vs context lines
370
+ if (result.type === 'content' && line.includes('"type":"context"')) {
371
+ session.totalContextLines++;
372
+ }
373
+ else {
374
+ session.totalMatches++;
375
+ }
376
+ // Early termination for exact filename matches (if enabled)
377
+ if (session.options.earlyTermination !== false && // Default to true
378
+ session.options.searchType === 'files' &&
379
+ this.isExactFilename(session.options.pattern)) {
380
+ const pat = path.normalize(session.options.pattern);
381
+ const filePath = path.normalize(result.file);
382
+ const ignoreCase = session.options.ignoreCase !== false;
383
+ const ends = ignoreCase
384
+ ? filePath.toLowerCase().endsWith(pat.toLowerCase())
385
+ : filePath.endsWith(pat);
386
+ if (ends) {
387
+ // Found exact match, terminate search early
388
+ setTimeout(() => {
389
+ if (!session.process.killed) {
390
+ session.process.kill('SIGTERM');
391
+ }
392
+ }, 100); // Small delay to allow any remaining results
393
+ break;
394
+ }
395
+ }
396
+ }
397
+ }
398
+ }
399
+ parseLine(line, searchType) {
400
+ if (searchType === 'content') {
401
+ // Parse JSON output from content search
402
+ try {
403
+ const parsed = JSON.parse(line);
404
+ if (parsed.type === 'match') {
405
+ // Handle multiple submatches per line - return first submatch
406
+ const submatch = parsed.data?.submatches?.[0];
407
+ return {
408
+ file: parsed.data.path.text,
409
+ line: parsed.data.line_number,
410
+ match: submatch?.match?.text || parsed.data.lines.text,
411
+ type: 'content'
412
+ };
413
+ }
414
+ if (parsed.type === 'context') {
415
+ return {
416
+ file: parsed.data.path.text,
417
+ line: parsed.data.line_number,
418
+ match: parsed.data.lines.text.trim(),
419
+ type: 'content'
420
+ };
421
+ }
422
+ // Handle summary to reconcile totals
423
+ if (parsed.type === 'summary') {
424
+ // Optional: could reconcile totalMatches with parsed.data.stats?.matchedLines
425
+ return null;
426
+ }
427
+ return null;
428
+ }
429
+ catch (error) {
430
+ // Skip invalid JSON lines
431
+ return null;
432
+ }
433
+ }
434
+ else {
435
+ // File search - each line is a file path
436
+ return {
437
+ file: line.trim(),
438
+ type: 'file'
439
+ };
440
+ }
441
+ }
442
+ }
443
+ // Global search manager instance
444
+ export const searchManager = new SearchManager();
445
+ // Cleanup management - run on fixed schedule
446
+ let cleanupInterval = null;
447
+ /**
448
+ * Start cleanup interval - now runs on fixed schedule
449
+ */
450
+ function startCleanupIfNeeded() {
451
+ if (!cleanupInterval) {
452
+ cleanupInterval = setInterval(() => {
453
+ searchManager.cleanupSessions();
454
+ }, 5 * 60 * 1000);
455
+ // Also check immediately after a short delay (let search process finish)
456
+ setTimeout(() => {
457
+ searchManager.cleanupSessions();
458
+ }, 1000);
459
+ }
460
+ }
461
+ // Export cleanup function for graceful shutdown
462
+ export function stopSearchManagerCleanup() {
463
+ if (cleanupInterval) {
464
+ clearInterval(cleanupInterval);
465
+ cleanupInterval = null;
466
+ }
467
+ }