@wonderwhy-er/desktop-commander 0.2.48 → 0.2.51

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.
@@ -38,6 +38,20 @@ export class TextFileHandler {
38
38
  // The factory routes binary files to BinaryFileHandler before reaching here
39
39
  return true;
40
40
  }
41
+ async detectTextEncoding(filePath) {
42
+ const fd = await fs.open(filePath, 'r');
43
+ try {
44
+ const bom = Buffer.alloc(2);
45
+ const { bytesRead } = await fd.read(bom, 0, bom.length, 0);
46
+ if (bytesRead === 2 && bom[0] === 0xff && bom[1] === 0xfe) {
47
+ return 'utf16le';
48
+ }
49
+ return 'utf8';
50
+ }
51
+ finally {
52
+ await fd.close();
53
+ }
54
+ }
41
55
  async read(filePath, options) {
42
56
  const offset = options?.offset ?? 0;
43
57
  const length = options?.length ?? 1000; // Default from config
@@ -69,7 +83,11 @@ export class TextFileHandler {
69
83
  // For text files that aren't too large, count lines
70
84
  if (stats.isFile() && stats.size < FILE_SIZE_LIMITS.LINE_COUNT_LIMIT) {
71
85
  try {
72
- const content = await fs.readFile(path, 'utf8');
86
+ const encoding = await this.detectTextEncoding(path);
87
+ let content = await fs.readFile(path, encoding);
88
+ if (encoding === 'utf16le' && content.charCodeAt(0) === 0xfeff) {
89
+ content = content.slice(1);
90
+ }
73
91
  const lineCount = TextFileHandler.countLines(content);
74
92
  info.metadata.lineCount = lineCount;
75
93
  }
@@ -100,11 +118,14 @@ export class TextFileHandler {
100
118
  /**
101
119
  * Get file line count (for files under size limit)
102
120
  */
103
- async getFileLineCount(filePath, signal) {
121
+ async getFileLineCount(filePath, signal, encoding = 'utf8') {
104
122
  try {
105
123
  const stats = await fs.stat(filePath);
106
124
  if (stats.size < FILE_SIZE_LIMITS.LINE_COUNT_LIMIT) {
107
- const content = await fs.readFile(filePath, { encoding: 'utf8', signal });
125
+ let content = await fs.readFile(filePath, { encoding, signal });
126
+ if (encoding === 'utf16le' && content.charCodeAt(0) === 0xfeff) {
127
+ content = content.slice(1);
128
+ }
108
129
  return TextFileHandler.countLines(content);
109
130
  }
110
131
  }
@@ -182,7 +203,16 @@ export class TextFileHandler {
182
203
  async readFileWithSmartPositioning(filePath, offset, length, mimeType, includeStatusMessage = true, signal) {
183
204
  const stats = await fs.stat(filePath);
184
205
  const fileSize = stats.size;
185
- const totalLines = await this.getFileLineCount(filePath, signal);
206
+ const encoding = await this.detectTextEncoding(filePath);
207
+ const totalLines = await this.getFileLineCount(filePath, signal, encoding);
208
+ // UTF-16LE needs character-aware streaming. Skip the two-byte BOM and keep
209
+ // the same line pagination/status semantics as ordinary text reads.
210
+ if (encoding === 'utf16le') {
211
+ if (offset < 0) {
212
+ return await this.readFromEndWithReadline(filePath, Math.abs(offset), mimeType, includeStatusMessage, totalLines, signal, 'utf16le', 2);
213
+ }
214
+ return await this.readFromStartWithReadline(filePath, offset, length, mimeType, includeStatusMessage, totalLines, signal, 'utf16le', 2);
215
+ }
186
216
  // For negative offsets (tail behavior), use reverse reading
187
217
  if (offset < 0) {
188
218
  const requestedLines = Math.abs(offset);
@@ -252,9 +282,9 @@ export class TextFileHandler {
252
282
  /**
253
283
  * Read from end using readline with circular buffer
254
284
  */
255
- async readFromEndWithReadline(filePath, requestedLines, mimeType, includeStatusMessage = true, fileTotalLines, signal) {
285
+ async readFromEndWithReadline(filePath, requestedLines, mimeType, includeStatusMessage = true, fileTotalLines, signal, encoding, start) {
256
286
  const rl = createInterface({
257
- input: createReadStream(filePath, { signal }),
287
+ input: createReadStream(filePath, { signal, encoding, start }),
258
288
  crlfDelay: Infinity
259
289
  });
260
290
  const buffer = new Array(requestedLines);
@@ -284,9 +314,9 @@ export class TextFileHandler {
284
314
  /**
285
315
  * Read from start/middle using readline
286
316
  */
287
- async readFromStartWithReadline(filePath, offset, length, mimeType, includeStatusMessage = true, fileTotalLines, signal) {
317
+ async readFromStartWithReadline(filePath, offset, length, mimeType, includeStatusMessage = true, fileTotalLines, signal, encoding, start) {
288
318
  const rl = createInterface({
289
- input: createReadStream(filePath, { signal }),
319
+ input: createReadStream(filePath, { signal, encoding, start }),
290
320
  crlfDelay: Infinity
291
321
  });
292
322
  const result = [];
@@ -37,14 +37,6 @@ declare class UsageTracker {
37
37
  * Get current usage stats from config
38
38
  */
39
39
  getStats(): Promise<ToolUsageStats>;
40
- /**
41
- * Save usage stats to config.
42
- * Non-blocking: the tool-call return path must not wait on a disk write. The
43
- * in-memory stats are already updated by the caller (getStats returns the live
44
- * config object), so persistence is coalesced in the background. This is what
45
- * keeps a saturated libuv threadpool from gating tool responses on every call.
46
- */
47
- private saveStats;
48
40
  /**
49
41
  * Determine which category a tool belongs to
50
42
  */
@@ -44,16 +44,6 @@ class UsageTracker {
44
44
  const stats = await configManager.getValue('usageStats');
45
45
  return stats || this.getDefaultStats();
46
46
  }
47
- /**
48
- * Save usage stats to config.
49
- * Non-blocking: the tool-call return path must not wait on a disk write. The
50
- * in-memory stats are already updated by the caller (getStats returns the live
51
- * config object), so persistence is coalesced in the background. This is what
52
- * keeps a saturated libuv threadpool from gating tool responses on every call.
53
- */
54
- async saveStats(stats) {
55
- await configManager.setValueNonBlocking('usageStats', stats);
56
- }
57
47
  /**
58
48
  * Determine which category a tool belongs to
59
49
  */
@@ -104,51 +94,53 @@ class UsageTracker {
104
94
  * Track a successful tool call
105
95
  */
106
96
  async trackSuccess(toolName) {
107
- const stats = await this.getStats();
108
- // Update session
109
97
  this.updateSession();
110
- // Update counters
111
- stats.totalToolCalls++;
112
- stats.successfulCalls++;
113
- stats.lastUsed = Date.now();
114
- // Update tool-specific counter
115
- stats.toolCounts[toolName] = (stats.toolCounts[toolName] || 0) + 1;
116
- // Update category counter
98
+ const now = Date.now();
117
99
  const category = this.getToolCategory(toolName);
118
- if (category) {
119
- stats[category]++;
120
- }
121
- // Update session count if this is a new session
122
- if (this.currentSession?.commandsInSession === 1) {
123
- stats.totalSessions++;
124
- }
125
- await this.saveStats(stats);
126
- return stats;
100
+ const newSession = this.currentSession?.commandsInSession === 1;
101
+ return await configManager.updateValueNonBlocking('usageStats', (stored) => {
102
+ const defaults = { ...this.getDefaultStats(), firstUsed: now, lastUsed: now };
103
+ const stats = {
104
+ ...defaults,
105
+ ...(stored || {}),
106
+ toolCounts: { ...(stored?.toolCounts || {}) },
107
+ };
108
+ stats.totalToolCalls++;
109
+ stats.successfulCalls++;
110
+ stats.lastUsed = Math.max(stats.lastUsed || 0, now);
111
+ stats.toolCounts[toolName] = (stats.toolCounts[toolName] || 0) + 1;
112
+ if (category)
113
+ stats[category]++;
114
+ if (newSession)
115
+ stats.totalSessions++;
116
+ return stats;
117
+ });
127
118
  }
128
119
  /**
129
120
  * Track a failed tool call
130
121
  */
131
122
  async trackFailure(toolName) {
132
- const stats = await this.getStats();
133
- // Update session
134
123
  this.updateSession();
135
- // Update counters
136
- stats.totalToolCalls++;
137
- stats.failedCalls++;
138
- stats.lastUsed = Date.now();
139
- // Update tool-specific counter (we count failures too)
140
- stats.toolCounts[toolName] = (stats.toolCounts[toolName] || 0) + 1;
141
- // Update category counter
124
+ const now = Date.now();
142
125
  const category = this.getToolCategory(toolName);
143
- if (category) {
144
- stats[category]++;
145
- }
146
- // Update session count if this is a new session
147
- if (this.currentSession?.commandsInSession === 1) {
148
- stats.totalSessions++;
149
- }
150
- await this.saveStats(stats);
151
- return stats;
126
+ const newSession = this.currentSession?.commandsInSession === 1;
127
+ return await configManager.updateValueNonBlocking('usageStats', (stored) => {
128
+ const defaults = { ...this.getDefaultStats(), firstUsed: now, lastUsed: now };
129
+ const stats = {
130
+ ...defaults,
131
+ ...(stored || {}),
132
+ toolCounts: { ...(stored?.toolCounts || {}) },
133
+ };
134
+ stats.totalToolCalls++;
135
+ stats.failedCalls++;
136
+ stats.lastUsed = Math.max(stats.lastUsed || 0, now);
137
+ stats.toolCounts[toolName] = (stats.toolCounts[toolName] || 0) + 1;
138
+ if (category)
139
+ stats[category]++;
140
+ if (newSession)
141
+ stats.totalSessions++;
142
+ return stats;
143
+ });
152
144
  }
153
145
  /**
154
146
  * Check if user should be prompted for feedback based on usage patterns
@@ -258,16 +250,23 @@ class UsageTracker {
258
250
  * Mark that user was prompted for feedback
259
251
  */
260
252
  async markFeedbackPrompted() {
261
- const stats = await this.getStats();
262
- const today = new Date().toISOString().split('T')[0]; // '2025-10-20'
263
- stats.lastFeedbackPrompt = Date.now();
264
- stats.lastFeedbackPromptDate = today;
265
- stats.feedbackAttempts = (stats.feedbackAttempts || 0) + 1;
266
- // Mark session so we don't prompt again this session
253
+ const now = Date.now();
254
+ const today = new Date(now).toISOString().split('T')[0];
267
255
  if (this.currentSession) {
268
256
  this.currentSession.promptedThisSession = true;
269
257
  }
270
- await this.saveStats(stats);
258
+ await configManager.updateValueNonBlocking('usageStats', (stored) => {
259
+ const defaults = { ...this.getDefaultStats(), firstUsed: now, lastUsed: now };
260
+ const stats = {
261
+ ...defaults,
262
+ ...(stored || {}),
263
+ toolCounts: { ...(stored?.toolCounts || {}) },
264
+ };
265
+ stats.lastFeedbackPrompt = Math.max(stats.lastFeedbackPrompt || 0, now);
266
+ stats.lastFeedbackPromptDate = today;
267
+ stats.feedbackAttempts = (stats.feedbackAttempts || 0) + 1;
268
+ return stats;
269
+ });
271
270
  }
272
271
  /**
273
272
  * Mark that user has given feedback
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.2.48";
1
+ export declare const VERSION = "0.2.51";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '0.2.48';
1
+ export const VERSION = '0.2.51';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wonderwhy-er/desktop-commander",
3
- "version": "0.2.48",
3
+ "version": "0.2.51",
4
4
  "description": "MCP server for terminal operations and file editing",
5
5
  "mcpName": "io.github.wonderwhy-er/desktop-commander",
6
6
  "license": "MIT",
@@ -112,6 +112,7 @@
112
112
  "open": "^10.2.0",
113
113
  "pdf-lib": "^1.17.1",
114
114
  "pizzip": "^3.2.0",
115
+ "proper-lockfile": "^4.1.2",
115
116
  "remark": "^15.0.1",
116
117
  "remark-gfm": "^4.0.1",
117
118
  "remark-parse": "^11.0.0",
@@ -129,6 +130,7 @@
129
130
  "@anthropic-ai/mcpb": "^1.2.0",
130
131
  "@modelcontextprotocol/ext-apps": "^1.0.1",
131
132
  "@types/node": "^20.17.24",
133
+ "@types/proper-lockfile": "^4.1.4",
132
134
  "commander": "^13.1.0",
133
135
  "esbuild": "^0.27.2",
134
136
  "js-tiktoken": "^1.0.21",