@wonderwhy-er/desktop-commander 0.1.38 → 0.2.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.
Files changed (50) hide show
  1. package/README.md +89 -6
  2. package/dist/REPLSessionManager.d.ts +109 -0
  3. package/dist/REPLSessionManager.js +364 -0
  4. package/dist/REPLSessionManager.test.d.ts +1 -0
  5. package/dist/REPLSessionManager.test.js +75 -0
  6. package/dist/client/replClient.d.ts +63 -0
  7. package/dist/client/replClient.js +217 -0
  8. package/dist/client/sshClient.d.ts +82 -0
  9. package/dist/client/sshClient.js +200 -0
  10. package/dist/config-manager.d.ts +2 -0
  11. package/dist/config-manager.js +3 -1
  12. package/dist/config.d.ts +1 -0
  13. package/dist/config.js +1 -0
  14. package/dist/handlers/filesystem-handlers.js +37 -3
  15. package/dist/handlers/fuzzy-search-log-handlers.d.ts +13 -0
  16. package/dist/handlers/fuzzy-search-log-handlers.js +179 -0
  17. package/dist/handlers/repl-handlers.d.ts +21 -0
  18. package/dist/handlers/repl-handlers.js +37 -0
  19. package/dist/handlers/replCommandHandler.d.ts +125 -0
  20. package/dist/handlers/replCommandHandler.js +255 -0
  21. package/dist/handlers/replCommandHandler.test.d.ts +1 -0
  22. package/dist/handlers/replCommandHandler.test.js +103 -0
  23. package/dist/repl-manager.d.ts +73 -0
  24. package/dist/repl-manager.js +407 -0
  25. package/dist/replIntegration.d.ts +14 -0
  26. package/dist/replIntegration.js +27 -0
  27. package/dist/server.js +37 -19
  28. package/dist/setup-claude-server.js +0 -20
  29. package/dist/tools/edit.js +129 -29
  30. package/dist/tools/enhanced-read-output.js +69 -0
  31. package/dist/tools/enhanced-send-input.js +111 -0
  32. package/dist/tools/filesystem.d.ts +7 -5
  33. package/dist/tools/filesystem.js +56 -27
  34. package/dist/tools/repl.d.ts +21 -0
  35. package/dist/tools/repl.js +217 -0
  36. package/dist/tools/schemas.d.ts +9 -0
  37. package/dist/tools/schemas.js +3 -0
  38. package/dist/tools/send-input.d.ts +2 -0
  39. package/dist/tools/send-input.js +45 -0
  40. package/dist/utils/fuzzySearchLogger.d.ts +30 -0
  41. package/dist/utils/fuzzySearchLogger.js +126 -0
  42. package/dist/utils/lineEndingHandler.d.ts +21 -0
  43. package/dist/utils/lineEndingHandler.js +77 -0
  44. package/dist/utils/lineEndingHandler_optimized.d.ts +21 -0
  45. package/dist/utils/lineEndingHandler_optimized.js +77 -0
  46. package/dist/utils/trackTools.d.ts +6 -0
  47. package/dist/utils/trackTools.js +54 -0
  48. package/dist/version.d.ts +1 -1
  49. package/dist/version.js +1 -1
  50. package/package.json +7 -2
package/dist/server.js CHANGED
@@ -3,8 +3,10 @@ import { CallToolRequestSchema, ListToolsRequestSchema, ListResourcesRequestSche
3
3
  import { zodToJsonSchema } from "zod-to-json-schema";
4
4
  // Shared constants for tool descriptions
5
5
  const PATH_GUIDANCE = `IMPORTANT: Always use absolute paths (starting with '/' or drive letter like 'C:\\') for reliability. Relative paths may fail as they depend on the current working directory. Tilde paths (~/...) might not work in all contexts. Unless the user explicitly asks for relative paths, use absolute paths.`;
6
+ const CMD_PREFIX_DESCRIPTION = `This command can be referenced as "DC: ..." or "use Desktop Commander to ..." in your instructions.`;
6
7
  import { ExecuteCommandArgsSchema, ReadOutputArgsSchema, ForceTerminateArgsSchema, ListSessionsArgsSchema, KillProcessArgsSchema, ReadFileArgsSchema, ReadMultipleFilesArgsSchema, WriteFileArgsSchema, CreateDirectoryArgsSchema, ListDirectoryArgsSchema, MoveFileArgsSchema, SearchFilesArgsSchema, GetFileInfoArgsSchema, SearchCodeArgsSchema, GetConfigArgsSchema, SetConfigValueArgsSchema, ListProcessesArgsSchema, EditBlockArgsSchema, } from './tools/schemas.js';
7
8
  import { getConfig, setConfigValue } from './tools/config.js';
9
+ import { trackToolCall } from './utils/trackTools.js';
8
10
  import { VERSION } from './version.js';
9
11
  import { capture } from "./utils/capture.js";
10
12
  console.error("Loading server.ts");
@@ -41,45 +43,58 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
41
43
  // Configuration tools
42
44
  {
43
45
  name: "get_config",
44
- description: "Get the complete server configuration as JSON. Config includes fields for: blockedCommands (array of blocked shell commands), defaultShell (shell to use for commands), allowedDirectories (paths the server can access).",
46
+ description: `Get the complete server configuration as JSON. Config includes fields for: blockedCommands (array of blocked shell commands), defaultShell (shell to use for commands), allowedDirectories (paths the server can access), fileReadLineLimit (max lines for read_file, default 1000), fileWriteLineLimit (max lines per write_file call, default 50), telemetryEnabled (boolean for telemetry opt-in/out). ${CMD_PREFIX_DESCRIPTION}`,
45
47
  inputSchema: zodToJsonSchema(GetConfigArgsSchema),
46
48
  },
47
49
  {
48
50
  name: "set_config_value",
49
- description: "Set a specific configuration value by key. WARNING: Should be used in a separate chat from file operations and command execution to prevent security issues. Config keys include: blockedCommands (array), defaultShell (string), allowedDirectories (array of paths). IMPORTANT: Setting allowedDirectories to an empty array ([]) allows full access to the entire file system, regardless of the operating system.",
51
+ description: `Set a specific configuration value by key. WARNING: Should be used in a separate chat from file operations and command execution to prevent security issues. Config keys include: blockedCommands (array), defaultShell (string), allowedDirectories (array of paths), fileReadLineLimit (number, max lines for read_file), fileWriteLineLimit (number, max lines per write_file call), telemetryEnabled (boolean). IMPORTANT: Setting allowedDirectories to an empty array ([]) allows full access to the entire file system, regardless of the operating system. ${CMD_PREFIX_DESCRIPTION}`,
50
52
  inputSchema: zodToJsonSchema(SetConfigValueArgsSchema),
51
53
  },
52
54
  // Filesystem tools
53
55
  {
54
56
  name: "read_file",
55
- description: `Read the complete contents of a file from the file system or a URL. Prefer this over 'execute_command' with cat/type for viewing files. When reading from the file system, only works within allowed directories. Can fetch content from URLs when isUrl parameter is set to true. Handles text files normally and image files are returned as viewable images. Recognized image types: PNG, JPEG, GIF, WebP. ${PATH_GUIDANCE}`,
57
+ description: `Read the contents of a file from the file system or a URL with optional offset and length parameters. Prefer this over 'execute_command' with cat/type for viewing files. Supports partial file reading with 'offset' (start line, default: 0) and 'length' (max lines to read, default: configurable via 'fileReadLineLimit' setting, initially 1000). When reading from the file system, only works within allowed directories. Can fetch content from URLs when isUrl parameter is set to true (URLs are always read in full regardless of offset/length). Handles text files normally and image files are returned as viewable images. Recognized image types: PNG, JPEG, GIF, WebP. ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`,
56
58
  inputSchema: zodToJsonSchema(ReadFileArgsSchema),
57
59
  },
58
60
  {
59
61
  name: "read_multiple_files",
60
- description: `Read the contents of multiple files simultaneously. Each file's content is returned with its path as a reference. Handles text files normally and renders images as viewable content. Recognized image types: PNG, JPEG, GIF, WebP. Failed reads for individual files won't stop the entire operation. Only works within allowed directories. ${PATH_GUIDANCE}`,
62
+ description: `Read the contents of multiple files simultaneously. Each file's content is returned with its path as a reference. Handles text files normally and renders images as viewable content. Recognized image types: PNG, JPEG, GIF, WebP. Failed reads for individual files won't stop the entire operation. Only works within allowed directories. ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`,
61
63
  inputSchema: zodToJsonSchema(ReadMultipleFilesArgsSchema),
62
64
  },
63
65
  {
64
66
  name: "write_file",
65
- description: `Completely replace file contents. Best for large changes (>20% of file) or when edit_block fails. Use with caution as it will overwrite existing files. Only works within allowed directories. ${PATH_GUIDANCE}`,
67
+ description: `Write or append to file contents with a configurable line limit per call (default: 50 lines). THIS IS A STRICT REQUIREMENT. ANY file with more than the configured limit MUST BE written in chunks or IT WILL FAIL.
68
+
69
+ NEVER attempt to write more than the configured line limit at once.
70
+
71
+ REQUIRED PROCESS FOR LARGE FILES:
72
+ 1. FIRST → write_file(filePath, firstChunk, {mode: 'rewrite'})
73
+ 2. THEN → write_file(filePath, secondChunk, {mode: 'append'})
74
+ 3. THEN → write_file(filePath, thirdChunk, {mode: 'append'})
75
+ ... and so on for each chunk
76
+
77
+ If asked to continue writing do not restart from beginning, read end of file to see where you stopped and continue from there
78
+
79
+ Files over the line limit (configurable via 'fileWriteLineLimit' setting) WILL BE REJECTED if not broken into chunks as described above.
80
+ Only works within allowed directories. ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`,
66
81
  inputSchema: zodToJsonSchema(WriteFileArgsSchema),
67
82
  },
68
83
  {
69
84
  name: "create_directory",
70
- description: `Create a new directory or ensure a directory exists. Can create multiple nested directories in one operation. Only works within allowed directories. ${PATH_GUIDANCE}`,
85
+ description: `Create a new directory or ensure a directory exists. Can create multiple nested directories in one operation. Only works within allowed directories. ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`,
71
86
  inputSchema: zodToJsonSchema(CreateDirectoryArgsSchema),
72
87
  },
73
88
  {
74
89
  name: "list_directory",
75
- description: `Get a detailed listing of all files and directories in a specified path. Use this instead of 'execute_command' with ls/dir commands. Results distinguish between files and directories with [FILE] and [DIR] prefixes. Only works within allowed directories. ${PATH_GUIDANCE}`,
90
+ description: `Get a detailed listing of all files and directories in a specified path. Use this instead of 'execute_command' with ls/dir commands. Results distinguish between files and directories with [FILE] and [DIR] prefixes. Only works within allowed directories. ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`,
76
91
  inputSchema: zodToJsonSchema(ListDirectoryArgsSchema),
77
92
  },
78
93
  {
79
94
  name: "move_file",
80
95
  description: `Move or rename files and directories.
81
96
  Can move files between directories and rename them in a single operation.
82
- Both source and destination must be within allowed directories. ${PATH_GUIDANCE}`,
97
+ Both source and destination must be within allowed directories. ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`,
83
98
  inputSchema: zodToJsonSchema(MoveFileArgsSchema),
84
99
  },
85
100
  {
@@ -88,7 +103,7 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
88
103
  Use this instead of 'execute_command' with find/dir/ls for locating files.
89
104
  Searches through all subdirectories from the starting path.
90
105
  Has a default timeout of 30 seconds which can be customized using the timeoutMs parameter.
91
- Only searches within allowed directories. ${PATH_GUIDANCE}`,
106
+ Only searches within allowed directories. ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`,
92
107
  inputSchema: zodToJsonSchema(SearchFilesArgsSchema),
93
108
  },
94
109
  {
@@ -99,14 +114,14 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
99
114
  Supports regular expressions, file pattern filtering, and context lines.
100
115
  Has a default timeout of 30 seconds which can be customized.
101
116
  Only searches within allowed directories.
102
- ${PATH_GUIDANCE}`,
117
+ ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`,
103
118
  inputSchema: zodToJsonSchema(SearchCodeArgsSchema),
104
119
  },
105
120
  {
106
121
  name: "get_file_info",
107
122
  description: `Retrieve detailed metadata about a file or directory including size, creation time, last modified time,
108
123
  permissions, and type.
109
- Only works within allowed directories. ${PATH_GUIDANCE}`,
124
+ Only works within allowed directories. ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`,
110
125
  inputSchema: zodToJsonSchema(GetFileInfoArgsSchema),
111
126
  },
112
127
  // Note: list_allowed_directories removed - use get_config to check allowedDirectories
@@ -121,8 +136,9 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
121
136
  To replace multiple occurrences, provide the expected_replacements parameter with the exact number of matches expected.
122
137
  UNIQUENESS REQUIREMENT: When expected_replacements=1 (default), include the minimal amount of context necessary (typically 1-3 lines) before and after the change point, with exact whitespace and indentation.
123
138
  When editing multiple sections, make separate edit_block calls for each distinct change rather than one large replacement.
124
- When a close but non-exact match is found, a character-level diff is shown in the format: common_prefix{-removed-}{+added+}common_suffix to help you identify what's different.
125
- ${PATH_GUIDANCE}`,
139
+ When a close but non-exact match is found, a character-level diff is shown in the format: common_prefix{-removed-}{+added+}common_suffix to help you identify what's different.
140
+ Similar to write_file, there is a configurable line limit (fileWriteLineLimit) that warns if the edited file exceeds this limit. If this happens, consider breaking your edits into smaller, more focused changes.
141
+ ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`,
126
142
  inputSchema: zodToJsonSchema(EditBlockArgsSchema),
127
143
  },
128
144
  // Terminal tools
@@ -131,32 +147,32 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
131
147
  description: `Execute a terminal command with timeout.
132
148
  Command will continue running in background if it doesn't complete within timeout.
133
149
  NOTE: For file operations, prefer specialized tools like read_file, search_code, list_directory instead of cat, grep, or ls commands.
134
- ${PATH_GUIDANCE}`,
150
+ ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`,
135
151
  inputSchema: zodToJsonSchema(ExecuteCommandArgsSchema),
136
152
  },
137
153
  {
138
154
  name: "read_output",
139
- description: "Read new output from a running terminal session.",
155
+ description: `Read new output from a running terminal session. ${CMD_PREFIX_DESCRIPTION}`,
140
156
  inputSchema: zodToJsonSchema(ReadOutputArgsSchema),
141
157
  },
142
158
  {
143
159
  name: "force_terminate",
144
- description: "Force terminate a running terminal session.",
160
+ description: `Force terminate a running terminal session. ${CMD_PREFIX_DESCRIPTION}`,
145
161
  inputSchema: zodToJsonSchema(ForceTerminateArgsSchema),
146
162
  },
147
163
  {
148
164
  name: "list_sessions",
149
- description: "List all active terminal sessions.",
165
+ description: `List all active terminal sessions. ${CMD_PREFIX_DESCRIPTION}`,
150
166
  inputSchema: zodToJsonSchema(ListSessionsArgsSchema),
151
167
  },
152
168
  {
153
169
  name: "list_processes",
154
- description: "List all running processes. Returns process information including PID, command name, CPU usage, and memory usage.",
170
+ description: `List all running processes. Returns process information including PID, command name, CPU usage, and memory usage. ${CMD_PREFIX_DESCRIPTION}`,
155
171
  inputSchema: zodToJsonSchema(ListProcessesArgsSchema),
156
172
  },
157
173
  {
158
174
  name: "kill_process",
159
- description: "Terminate a running process by PID. Use with caution as this will forcefully terminate the specified process.",
175
+ description: `Terminate a running process by PID. Use with caution as this will forcefully terminate the specified process. ${CMD_PREFIX_DESCRIPTION}`,
160
176
  inputSchema: zodToJsonSchema(KillProcessArgsSchema),
161
177
  },
162
178
  ],
@@ -174,6 +190,8 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
174
190
  capture('server_call_tool', {
175
191
  name
176
192
  });
193
+ // Track tool call
194
+ trackToolCall(name, args);
177
195
  // Using a more structured approach with dedicated handlers
178
196
  switch (name) {
179
197
  // Config tools
@@ -385,26 +385,6 @@ function updateSetupStep(index, status, error = null) {
385
385
  }
386
386
  }
387
387
 
388
- try {
389
- // Only dependency is node-machine-id
390
- const machineIdInitStep = addSetupStep('initialize_machine_id');
391
- try {
392
- const machineIdModule = await import('node-machine-id');
393
- // Get a unique user ID
394
- uniqueUserId = machineIdModule.machineIdSync();
395
- updateSetupStep(machineIdInitStep, 'completed');
396
- } catch (error) {
397
- // Fall back to a semi-unique identifier if machine-id is not available
398
- uniqueUserId = `${platform()}-${process.env.USER || process.env.USERNAME || 'unknown'}-${Date.now()}`;
399
- updateSetupStep(machineIdInitStep, 'fallback', error);
400
- }
401
- } catch (error) {
402
- addSetupStep('initialize_machine_id', 'failed', error);
403
- }
404
-
405
-
406
-
407
-
408
388
  async function execAsync(command) {
409
389
  const execStep = addSetupStep(`exec_${command.substring(0, 20)}...`);
410
390
  return new Promise((resolve, reject) => {
@@ -3,11 +3,66 @@ import { recursiveFuzzyIndexOf, getSimilarityRatio } from './fuzzySearch.js';
3
3
  import { capture } from '../utils/capture.js';
4
4
  import { EditBlockArgsSchema } from "./schemas.js";
5
5
  import path from 'path';
6
+ import { detectLineEnding, normalizeLineEndings } from '../utils/lineEndingHandler.js';
7
+ import { configManager } from '../config-manager.js';
8
+ import { fuzzySearchLogger } from '../utils/fuzzySearchLogger.js';
6
9
  /**
7
10
  * Threshold for fuzzy matching - similarity must be at least this value to be considered
8
11
  * (0-1 scale where 1 is perfect match and 0 is completely different)
9
12
  */
10
13
  const FUZZY_THRESHOLD = 0.7;
14
+ /**
15
+ * Extract character code data from diff
16
+ * @param expected The string that was searched for
17
+ * @param actual The string that was found
18
+ * @returns Character code statistics
19
+ */
20
+ function getCharacterCodeData(expected, actual) {
21
+ // Find common prefix and suffix
22
+ let prefixLength = 0;
23
+ const minLength = Math.min(expected.length, actual.length);
24
+ // Determine common prefix length
25
+ while (prefixLength < minLength &&
26
+ expected[prefixLength] === actual[prefixLength]) {
27
+ prefixLength++;
28
+ }
29
+ // Determine common suffix length
30
+ let suffixLength = 0;
31
+ while (suffixLength < minLength - prefixLength &&
32
+ expected[expected.length - 1 - suffixLength] === actual[actual.length - 1 - suffixLength]) {
33
+ suffixLength++;
34
+ }
35
+ // Extract the different parts
36
+ const expectedDiff = expected.substring(prefixLength, expected.length - suffixLength);
37
+ const actualDiff = actual.substring(prefixLength, actual.length - suffixLength);
38
+ // Count unique character codes in the diff
39
+ const characterCodes = new Map();
40
+ const fullDiff = expectedDiff + actualDiff;
41
+ for (let i = 0; i < fullDiff.length; i++) {
42
+ const charCode = fullDiff.charCodeAt(i);
43
+ characterCodes.set(charCode, (characterCodes.get(charCode) || 0) + 1);
44
+ }
45
+ // Create character codes string report
46
+ const charCodeReport = [];
47
+ characterCodes.forEach((count, code) => {
48
+ // Include character representation for better readability
49
+ const char = String.fromCharCode(code);
50
+ // Make special characters more readable
51
+ const charDisplay = code < 32 || code > 126 ? `\\x${code.toString(16).padStart(2, '0')}` : char;
52
+ charCodeReport.push(`${code}:${count}[${charDisplay}]`);
53
+ });
54
+ // Sort by character code for consistency
55
+ charCodeReport.sort((a, b) => {
56
+ const codeA = parseInt(a.split(':')[0]);
57
+ const codeB = parseInt(b.split(':')[0]);
58
+ return codeA - codeB;
59
+ });
60
+ return {
61
+ report: charCodeReport.join(','),
62
+ uniqueCount: characterCodes.size,
63
+ diffLength: fullDiff.length
64
+ };
65
+ }
11
66
  export async function performSearchReplace(filePath, block, expectedReplacements = 1) {
12
67
  // Check for empty search string to prevent infinite loops
13
68
  if (block.search === "") {
@@ -23,18 +78,25 @@ export async function performSearchReplace(filePath, block, expectedReplacements
23
78
  // Capture file extension in telemetry without capturing the file path
24
79
  capture('server_edit_block', { fileExtension: fileExtension });
25
80
  // Read file as plain string
26
- const { content } = await readFile(filePath);
81
+ const { content } = await readFile(filePath, false, 0, Number.MAX_SAFE_INTEGER);
27
82
  // Make sure content is a string
28
83
  if (typeof content !== 'string') {
29
84
  throw new Error('Wrong content for file ' + filePath);
30
85
  }
86
+ // Get the line limit from configuration
87
+ const config = await configManager.getConfig();
88
+ const MAX_LINES = config.fileWriteLineLimit ?? 50; // Default to 50 if not set
89
+ // Detect file's line ending style
90
+ const fileLineEnding = detectLineEnding(content);
91
+ // Normalize search string to match file's line endings
92
+ const normalizedSearch = normalizeLineEndings(block.search, fileLineEnding);
31
93
  // First try exact match
32
94
  let tempContent = content;
33
95
  let count = 0;
34
- let pos = tempContent.indexOf(block.search);
96
+ let pos = tempContent.indexOf(normalizedSearch);
35
97
  while (pos !== -1) {
36
98
  count++;
37
- pos = tempContent.indexOf(block.search, pos + 1);
99
+ pos = tempContent.indexOf(normalizedSearch, pos + 1);
38
100
  }
39
101
  // If exact match found and count matches expected replacements, proceed with exact replacement
40
102
  if (count > 0 && count === expectedReplacements) {
@@ -42,21 +104,32 @@ export async function performSearchReplace(filePath, block, expectedReplacements
42
104
  let newContent = content;
43
105
  // If we're only replacing one occurrence, replace it directly
44
106
  if (expectedReplacements === 1) {
45
- const searchIndex = newContent.indexOf(block.search);
107
+ const searchIndex = newContent.indexOf(normalizedSearch);
46
108
  newContent =
47
109
  newContent.substring(0, searchIndex) +
48
- block.replace +
49
- newContent.substring(searchIndex + block.search.length);
110
+ normalizeLineEndings(block.replace, fileLineEnding) +
111
+ newContent.substring(searchIndex + normalizedSearch.length);
50
112
  }
51
113
  else {
52
114
  // Replace all occurrences using split and join for multiple replacements
53
- newContent = newContent.split(block.search).join(block.replace);
115
+ newContent = newContent.split(normalizedSearch).join(normalizeLineEndings(block.replace, fileLineEnding));
116
+ }
117
+ // Check if search or replace text has too many lines
118
+ const searchLines = block.search.split('\n').length;
119
+ const replaceLines = block.replace.split('\n').length;
120
+ const maxLines = Math.max(searchLines, replaceLines);
121
+ let warningMessage = "";
122
+ if (maxLines > MAX_LINES) {
123
+ const problemText = searchLines > replaceLines ? 'search text' : 'replacement text';
124
+ warningMessage = `\n\nWARNING: The ${problemText} has ${maxLines} lines (maximum: ${MAX_LINES}).
125
+
126
+ RECOMMENDATION: For large search/replace operations, consider breaking them into smaller chunks with fewer lines.`;
54
127
  }
55
128
  await writeFile(filePath, newContent);
56
129
  return {
57
130
  content: [{
58
131
  type: "text",
59
- text: `Successfully applied ${expectedReplacements} edit${expectedReplacements > 1 ? 's' : ''} to ${filePath}`
132
+ text: `Successfully applied ${expectedReplacements} edit${expectedReplacements > 1 ? 's' : ''} to ${filePath}${warningMessage}`
60
133
  }],
61
134
  };
62
135
  }
@@ -81,19 +154,47 @@ export async function performSearchReplace(filePath, block, expectedReplacements
81
154
  const similarity = getSimilarityRatio(block.search, fuzzyResult.value);
82
155
  // Calculate execution time in milliseconds
83
156
  const executionTime = performance.now() - startTime;
157
+ // Generate diff and gather character code data
158
+ const diff = highlightDifferences(block.search, fuzzyResult.value);
159
+ // Count character codes in diff
160
+ const characterCodeData = getCharacterCodeData(block.search, fuzzyResult.value);
161
+ // Create comprehensive log entry
162
+ const logEntry = {
163
+ timestamp: new Date(),
164
+ searchText: block.search,
165
+ foundText: fuzzyResult.value,
166
+ similarity: similarity,
167
+ executionTime: executionTime,
168
+ exactMatchCount: count,
169
+ expectedReplacements: expectedReplacements,
170
+ fuzzyThreshold: FUZZY_THRESHOLD,
171
+ belowThreshold: similarity < FUZZY_THRESHOLD,
172
+ diff: diff,
173
+ searchLength: block.search.length,
174
+ foundLength: fuzzyResult.value.length,
175
+ fileExtension: fileExtension,
176
+ characterCodes: characterCodeData.report,
177
+ uniqueCharacterCount: characterCodeData.uniqueCount,
178
+ diffLength: characterCodeData.diffLength
179
+ };
180
+ // Log to file
181
+ await fuzzySearchLogger.log(logEntry);
182
+ // Combine all fuzzy search data for single capture
183
+ const fuzzySearchData = {
184
+ similarity: similarity,
185
+ execution_time_ms: executionTime,
186
+ search_length: block.search.length,
187
+ file_size: content.length,
188
+ threshold: FUZZY_THRESHOLD,
189
+ found_text_length: fuzzyResult.value.length,
190
+ character_codes: characterCodeData.report,
191
+ unique_character_count: characterCodeData.uniqueCount,
192
+ total_diff_length: characterCodeData.diffLength
193
+ };
84
194
  // Check if the fuzzy match is "close enough"
85
195
  if (similarity >= FUZZY_THRESHOLD) {
86
- // Format differences for clearer output
87
- const diff = highlightDifferences(block.search, fuzzyResult.value);
88
- // Capture the fuzzy search event
89
- capture('server_fuzzy_search_performed', {
90
- similarity: similarity,
91
- execution_time_ms: executionTime,
92
- search_length: block.search.length,
93
- file_size: content.length,
94
- threshold: FUZZY_THRESHOLD,
95
- found_text_length: fuzzyResult.value.length
96
- });
196
+ // Capture the fuzzy search event with all data
197
+ capture('server_fuzzy_search_performed', fuzzySearchData);
97
198
  // If we allow fuzzy matches, we would make the replacement here
98
199
  // For now, we'll return a detailed message about the fuzzy match
99
200
  return {
@@ -101,20 +202,17 @@ export async function performSearchReplace(filePath, block, expectedReplacements
101
202
  type: "text",
102
203
  text: `Exact match not found, but found a similar text with ${Math.round(similarity * 100)}% similarity (found in ${executionTime.toFixed(2)}ms):\n\n` +
103
204
  `Differences:\n${diff}\n\n` +
104
- `To replace this text, use the exact text found in the file.`
105
- }],
205
+ `To replace this text, use the exact text found in the file.\n\n` +
206
+ `Log entry saved for analysis. Use the following command to check the log:\n` +
207
+ `Check log: ${await fuzzySearchLogger.getLogPath()}`
208
+ }], // TODO
106
209
  };
107
210
  }
108
211
  else {
109
212
  // If the fuzzy match isn't close enough
110
- // Still capture the fuzzy search event even for unsuccessful matches
213
+ // Still capture the fuzzy search event with all data
111
214
  capture('server_fuzzy_search_performed', {
112
- similarity: similarity,
113
- execution_time_ms: executionTime,
114
- search_length: block.search.length,
115
- file_size: content.length,
116
- threshold: FUZZY_THRESHOLD,
117
- found_text_length: fuzzyResult.value.length,
215
+ ...fuzzySearchData,
118
216
  below_threshold: true
119
217
  });
120
218
  return {
@@ -122,7 +220,9 @@ export async function performSearchReplace(filePath, block, expectedReplacements
122
220
  type: "text",
123
221
  text: `Search content not found in ${filePath}. The closest match was "${fuzzyResult.value}" ` +
124
222
  `with only ${Math.round(similarity * 100)}% similarity, which is below the ${Math.round(FUZZY_THRESHOLD * 100)}% threshold. ` +
125
- `(Fuzzy search completed in ${executionTime.toFixed(2)}ms)`
223
+ `(Fuzzy search completed in ${executionTime.toFixed(2)}ms)\n\n` +
224
+ `Log entry saved for analysis. Use the following command to check the log:\n` +
225
+ `Check log: ${await fuzzySearchLogger.getLogPath()}`
126
226
  }],
127
227
  };
128
228
  }
@@ -0,0 +1,69 @@
1
+ import { terminalManager } from '../terminal-manager.js';
2
+ import { ReadOutputArgsSchema } from './schemas.js';
3
+
4
+ export async function readOutput(args) {
5
+ const parsed = ReadOutputArgsSchema.safeParse(args);
6
+ if (!parsed.success) {
7
+ return {
8
+ content: [{ type: "text", text: `Error: Invalid arguments for read_output: ${parsed.error}` }],
9
+ isError: true,
10
+ };
11
+ }
12
+
13
+ const { pid, timeout_ms = 5000 } = parsed.data;
14
+
15
+ // Check if the process exists
16
+ const session = terminalManager.getSession(pid);
17
+ if (!session) {
18
+ return {
19
+ content: [{ type: "text", text: `No session found for PID ${pid}` }],
20
+ isError: true,
21
+ };
22
+ }
23
+
24
+ // Wait for output with timeout
25
+ let output = "";
26
+ let timeoutReached = false;
27
+
28
+ try {
29
+ // Create a promise that resolves when new output is available or when timeout is reached
30
+ const outputPromise = new Promise((resolve) => {
31
+ // Check for initial output
32
+ const initialOutput = terminalManager.getNewOutput(pid);
33
+ if (initialOutput && initialOutput.length > 0) {
34
+ resolve(initialOutput);
35
+ return;
36
+ }
37
+
38
+ // Setup an interval to poll for output
39
+ const interval = setInterval(() => {
40
+ const newOutput = terminalManager.getNewOutput(pid);
41
+ if (newOutput && newOutput.length > 0) {
42
+ clearInterval(interval);
43
+ resolve(newOutput);
44
+ }
45
+ }, 100); // Check every 100ms
46
+
47
+ // Set a timeout to stop waiting
48
+ setTimeout(() => {
49
+ clearInterval(interval);
50
+ timeoutReached = true;
51
+ resolve(terminalManager.getNewOutput(pid) || "");
52
+ }, timeout_ms);
53
+ });
54
+
55
+ output = await outputPromise;
56
+ } catch (error) {
57
+ return {
58
+ content: [{ type: "text", text: `Error reading output: ${error}` }],
59
+ isError: true,
60
+ };
61
+ }
62
+
63
+ return {
64
+ content: [{
65
+ type: "text",
66
+ text: output || 'No new output available' + (timeoutReached ? ' (timeout reached)' : '')
67
+ }],
68
+ };
69
+ }
@@ -0,0 +1,111 @@
1
+ import { terminalManager } from '../terminal-manager.js';
2
+ import { SendInputArgsSchema } from './schemas.js';
3
+ import { capture } from "../utils/capture.js";
4
+
5
+ export async function sendInput(args) {
6
+ const parsed = SendInputArgsSchema.safeParse(args);
7
+ if (!parsed.success) {
8
+ capture('server_send_input_failed', {
9
+ error: 'Invalid arguments'
10
+ });
11
+ return {
12
+ content: [{ type: "text", text: `Error: Invalid arguments for send_input: ${parsed.error}` }],
13
+ isError: true,
14
+ };
15
+ }
16
+
17
+ const { pid, input, timeout_ms = 5000, wait_for_prompt = false } = parsed.data;
18
+
19
+ try {
20
+ capture('server_send_input', {
21
+ pid: pid,
22
+ inputLength: input.length
23
+ });
24
+
25
+ // Try to send input to the process
26
+ const success = terminalManager.sendInputToProcess(pid, input);
27
+
28
+ if (!success) {
29
+ return {
30
+ content: [{ type: "text", text: `Error: Failed to send input to process ${pid}. The process may have exited or doesn't accept input.` }],
31
+ isError: true,
32
+ };
33
+ }
34
+
35
+ // If we don't need to wait for output, return immediately
36
+ if (!wait_for_prompt) {
37
+ return {
38
+ content: [{
39
+ type: "text",
40
+ text: `Successfully sent input to process ${pid}. Use read_output to get the process response.`
41
+ }],
42
+ };
43
+ }
44
+
45
+ // Wait for output with timeout
46
+ let output = "";
47
+ let timeoutReached = false;
48
+
49
+ try {
50
+ // Create a promise that resolves when new output is available or when timeout is reached
51
+ const outputPromise = new Promise((resolve) => {
52
+ // Setup an interval to poll for output
53
+ const interval = setInterval(() => {
54
+ const newOutput = terminalManager.getNewOutput(pid);
55
+
56
+ if (newOutput && newOutput.length > 0) {
57
+ output += newOutput;
58
+
59
+ // Check if output contains a prompt pattern (indicating the REPL is ready for more input)
60
+ const promptPatterns = [/^>\s*$/, /^>>>\s*$/, /^\.{3}\s*$/]; // Common REPL prompts
61
+ const lines = output.split('\n');
62
+ const lastLine = lines[lines.length - 1];
63
+ const hasPrompt = promptPatterns.some(pattern => pattern.test(lastLine.trim()));
64
+
65
+ if (hasPrompt) {
66
+ clearInterval(interval);
67
+ resolve(output);
68
+ }
69
+ }
70
+ }, 100); // Check every 100ms
71
+
72
+ // Set a timeout to stop waiting
73
+ setTimeout(() => {
74
+ clearInterval(interval);
75
+ timeoutReached = true;
76
+
77
+ // Get any final output
78
+ const finalOutput = terminalManager.getNewOutput(pid);
79
+ if (finalOutput) {
80
+ output += finalOutput;
81
+ }
82
+
83
+ resolve(output);
84
+ }, timeout_ms);
85
+ });
86
+
87
+ await outputPromise;
88
+ } catch (error) {
89
+ return {
90
+ content: [{ type: "text", text: `Error reading output after sending input: ${error}` }],
91
+ isError: true,
92
+ };
93
+ }
94
+
95
+ return {
96
+ content: [{
97
+ type: "text",
98
+ text: `Input sent to process ${pid}.\n\nOutput received:\n${output || '(No output)'}${timeoutReached ? ' (timeout reached)' : ''}`
99
+ }],
100
+ };
101
+ } catch (error) {
102
+ const errorMessage = error instanceof Error ? error.message : String(error);
103
+ capture('server_send_input_error', {
104
+ error: errorMessage
105
+ });
106
+ return {
107
+ content: [{ type: "text", text: `Error sending input: ${errorMessage}` }],
108
+ isError: true,
109
+ };
110
+ }
111
+ }
@@ -22,19 +22,21 @@ export declare function readFileFromUrl(url: string): Promise<FileResult>;
22
22
  /**
23
23
  * Read file content from the local filesystem
24
24
  * @param filePath Path to the file
25
- * @param returnMetadata Whether to return metadata with the content
25
+ * @param offset Starting line number to read from (default: 0)
26
+ * @param length Maximum number of lines to read (default: from config or 1000)
26
27
  * @returns File content or file result with metadata
27
28
  */
28
- export declare function readFileFromDisk(filePath: string): Promise<FileResult>;
29
+ export declare function readFileFromDisk(filePath: string, offset?: number, length?: number): Promise<FileResult>;
29
30
  /**
30
31
  * Read a file from either the local filesystem or a URL
31
32
  * @param filePath Path to the file or URL
32
- * @param returnMetadata Whether to return metadata with the content
33
33
  * @param isUrl Whether the path is a URL
34
+ * @param offset Starting line number to read from (default: 0)
35
+ * @param length Maximum number of lines to read (default: from config or 1000)
34
36
  * @returns File content or file result with metadata
35
37
  */
36
- export declare function readFile(filePath: string, isUrl?: boolean): Promise<FileResult>;
37
- export declare function writeFile(filePath: string, content: string): Promise<void>;
38
+ export declare function readFile(filePath: string, isUrl?: boolean, offset?: number, length?: number): Promise<FileResult>;
39
+ export declare function writeFile(filePath: string, content: string, mode?: 'rewrite' | 'append'): Promise<void>;
38
40
  export interface MultiFileResult {
39
41
  path: string;
40
42
  content?: string;