@wonderwhy-er/desktop-commander 0.2.51 → 0.3.1-alpha.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.
- package/README.md +14 -24
- package/dist/command-manager.d.ts +7 -2
- package/dist/command-manager.js +16 -11
- package/dist/config-manager.d.ts +49 -3
- package/dist/config-manager.js +329 -87
- package/dist/config-recovery.d.ts +28 -0
- package/dist/config-recovery.js +81 -0
- package/dist/config.d.ts +1 -0
- package/dist/config.js +9 -0
- package/dist/handlers/filesystem-handlers.d.ts +1 -1
- package/dist/handlers/filesystem-handlers.js +26 -16
- package/dist/handlers/search-answers.d.ts +58 -0
- package/dist/handlers/search-answers.js +172 -0
- package/dist/handlers/search-handlers.js +4 -92
- package/dist/index.js +14 -7
- package/dist/npm-scripts/remote.js +32 -4
- package/dist/remote-device/broadcast-analytics.d.ts +28 -0
- package/dist/remote-device/broadcast-analytics.js +41 -0
- package/dist/remote-device/desktop-commander-integration.d.ts +20 -0
- package/dist/remote-device/desktop-commander-integration.js +121 -40
- package/dist/remote-device/device-authenticator.d.ts +13 -1
- package/dist/remote-device/device-authenticator.js +87 -47
- package/dist/remote-device/device.d.ts +14 -0
- package/dist/remote-device/device.js +244 -51
- package/dist/remote-device/mqtt-enrollment.d.ts +16 -0
- package/dist/remote-device/mqtt-enrollment.js +162 -0
- package/dist/remote-device/mqtt-transport.d.ts +44 -0
- package/dist/remote-device/mqtt-transport.js +197 -0
- package/dist/remote-device/remote-channel.d.ts +64 -3
- package/dist/remote-device/remote-channel.js +317 -48
- package/dist/remote-device/scripts/blocking-offline-update.js +106 -69
- package/dist/remote-device/transport-analytics.d.ts +29 -0
- package/dist/remote-device/transport-analytics.js +44 -0
- package/dist/remote-device/transport-telemetry.d.ts +32 -0
- package/dist/remote-device/transport-telemetry.js +48 -0
- package/dist/search-manager.d.ts +239 -27
- package/dist/search-manager.js +746 -312
- package/dist/server.js +77 -92
- package/dist/terminal-manager.d.ts +138 -21
- package/dist/terminal-manager.js +300 -215
- package/dist/tools/config.d.ts +1 -1
- package/dist/tools/config.js +21 -70
- package/dist/tools/edit.js +12 -4
- package/dist/tools/filesystem.d.ts +30 -6
- package/dist/tools/filesystem.js +246 -140
- package/dist/tools/improved-process-tools.d.ts +9 -3
- package/dist/tools/improved-process-tools.js +171 -68
- package/dist/tools/pdf/extract-images.d.ts +2 -0
- package/dist/tools/pdf/extract-images.js +4 -1
- package/dist/tools/pdf/index.d.ts +5 -3
- package/dist/tools/pdf/index.js +4 -3
- package/dist/tools/pdf/lib/pdf2md.d.ts +8 -1
- package/dist/tools/pdf/lib/pdf2md.js +12 -5
- package/dist/tools/pdf/manipulations.d.ts +12 -1
- package/dist/tools/pdf/manipulations.js +17 -4
- package/dist/tools/pdf/markdown.d.ts +96 -0
- package/dist/tools/pdf/markdown.js +485 -20
- package/dist/tools/pdf/utils.d.ts +2 -2
- package/dist/tools/pdf/utils.js +4 -4
- package/dist/tools/process.js +8 -5
- package/dist/tools/schemas.d.ts +1 -1
- package/dist/tools/schemas.js +15 -6
- package/dist/tools/search.d.ts +32 -0
- package/dist/tools/search.js +202 -0
- package/dist/types.d.ts +1 -1
- package/dist/ui/config-editor/config-editor-runtime.js +6 -6
- package/dist/ui/config-editor/src/app.js +2 -15
- package/dist/utils/ab-test.js +27 -9
- package/dist/utils/atomic-write.d.ts +14 -0
- package/dist/utils/atomic-write.js +80 -0
- package/dist/utils/capture.d.ts +17 -1
- package/dist/utils/capture.js +177 -68
- package/dist/utils/exit-process.d.ts +24 -0
- package/dist/utils/exit-process.js +31 -0
- package/dist/utils/feature-flags.js +2 -1
- package/dist/utils/files/base.d.ts +3 -1
- package/dist/utils/files/docx.d.ts +3 -0
- package/dist/utils/files/docx.js +24 -9
- package/dist/utils/files/excel.d.ts +10 -0
- package/dist/utils/files/excel.js +93 -33
- package/dist/utils/files/factory.d.ts +22 -1
- package/dist/utils/files/factory.js +59 -4
- package/dist/utils/files/image.d.ts +12 -4
- package/dist/utils/files/image.js +19 -8
- package/dist/utils/files/index.d.ts +5 -3
- package/dist/utils/files/index.js +4 -3
- package/dist/utils/files/pdf.js +18 -36
- package/dist/utils/files/text.d.ts +21 -5
- package/dist/utils/files/text.js +90 -86
- package/dist/utils/internal-facts.d.ts +3 -0
- package/dist/utils/internal-facts.js +23 -0
- package/dist/utils/lazy-package.d.ts +23 -0
- package/dist/utils/lazy-package.js +42 -0
- package/dist/utils/open-browser.js +2 -1
- package/dist/utils/process-detection.d.ts +24 -5
- package/dist/utils/process-detection.js +199 -89
- package/dist/utils/process-tree.d.ts +14 -0
- package/dist/utils/process-tree.js +223 -0
- package/dist/utils/rename.d.ts +9 -0
- package/dist/utils/rename.js +74 -0
- package/dist/utils/ripgrep-resolver.js +9 -14
- package/dist/utils/shell.d.ts +42 -0
- package/dist/utils/shell.js +187 -0
- package/dist/utils/system-info.js +9 -7
- package/dist/utils/trackTools.js +2 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/node_modules/@desktop-commander/telemetry-contract/README.md +56 -0
- package/node_modules/@desktop-commander/telemetry-contract/package.json +32 -0
- package/node_modules/@desktop-commander/telemetry-contract/transport.d.ts +38 -0
- package/node_modules/@desktop-commander/telemetry-contract/transport.js +164 -0
- package/package.json +8 -2
- package/vendor/telemetry-contract/README.md +56 -0
- package/vendor/telemetry-contract/package.json +32 -0
- package/vendor/telemetry-contract/transport.d.ts +38 -0
- package/vendor/telemetry-contract/transport.js +164 -0
- package/vendor/telemetry-contract/transport.test.js +39 -0
package/dist/utils/files/pdf.js
CHANGED
|
@@ -26,27 +26,9 @@ export class PdfFileHandler {
|
|
|
26
26
|
const { offset = 0, length } = options ?? {};
|
|
27
27
|
try {
|
|
28
28
|
// Use existing PDF parser
|
|
29
|
-
//
|
|
30
|
-
// If length is undefined, we assume "rest of file" which requires careful handling.
|
|
31
|
-
// If length is defined, we pass { offset, length }.
|
|
32
|
-
// If neither, we pass empty array (all pages).
|
|
29
|
+
// With no length, read from offset to the last page (all pages from offset 0)
|
|
33
30
|
// Note: offset defaults to 0 if undefined.
|
|
34
|
-
|
|
35
|
-
if (length !== undefined) {
|
|
36
|
-
range = { offset, length };
|
|
37
|
-
}
|
|
38
|
-
else if (offset > 0) {
|
|
39
|
-
// If offset provided but no length, try to read reasonable amount or all?
|
|
40
|
-
// PageRange requires length. Let's assume 0 means "all" or use a large number?
|
|
41
|
-
// Looking at pdf2md implementation, it uses generatePageNumbers(offset, length, total).
|
|
42
|
-
// We'll pass 0 for length to imply "rest" if supported, or just undefined length if valid.
|
|
43
|
-
// But typescript requires length.
|
|
44
|
-
range = { offset, length: 0 };
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
range = [];
|
|
48
|
-
}
|
|
49
|
-
const pdfResult = await parsePdfToMarkdown(path, range);
|
|
31
|
+
const pdfResult = await parsePdfToMarkdown(path, { offset, length: length ?? Infinity });
|
|
50
32
|
return {
|
|
51
33
|
content: '', // Main content is in metadata.pages
|
|
52
34
|
mimeType: 'application/pdf',
|
|
@@ -75,9 +57,14 @@ export class PdfFileHandler {
|
|
|
75
57
|
* Write PDF - creates from markdown or operations
|
|
76
58
|
*/
|
|
77
59
|
async write(path, content, mode) {
|
|
60
|
+
// A PDF can't take text at its end: rendering the content would replace the file
|
|
61
|
+
if (mode === 'append') {
|
|
62
|
+
throw new Error('PDF append not supported. Use write_pdf to modify existing PDF files.');
|
|
63
|
+
}
|
|
78
64
|
// If content is string, treat as markdown to convert
|
|
79
65
|
if (typeof content === 'string') {
|
|
80
|
-
await parseMarkdownToPdf(content
|
|
66
|
+
const pdfBuffer = await parseMarkdownToPdf(content);
|
|
67
|
+
await fs.writeFile(path, pdfBuffer);
|
|
81
68
|
}
|
|
82
69
|
else if (Array.isArray(content)) {
|
|
83
70
|
// Array of operations - use editPdf
|
|
@@ -92,21 +79,16 @@ export class PdfFileHandler {
|
|
|
92
79
|
* Edit PDF by range/operations
|
|
93
80
|
*/
|
|
94
81
|
async editRange(path, range, content, options) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
success: false,
|
|
106
|
-
editsApplied: 0,
|
|
107
|
-
errors: [{ location: range, error: errorMessage }]
|
|
108
|
-
};
|
|
109
|
-
}
|
|
82
|
+
// For PDF, range editing isn't directly supported
|
|
83
|
+
// Could interpret range as page numbers in future
|
|
84
|
+
// The output path and inserted PDFs are checked as write_pdf checks them.
|
|
85
|
+
// Errors reach edit_block (as the Excel handler's do), which answers with them.
|
|
86
|
+
// Imported when used: tools/filesystem.js loads this handler through the file handler factory
|
|
87
|
+
const { validatePdfOperationPaths } = await import('../../tools/filesystem.js');
|
|
88
|
+
const targetPath = await validatePdfOperationPaths(path, content, options?.outputPath);
|
|
89
|
+
const resultBuffer = await editPdf(path, content);
|
|
90
|
+
await fs.writeFile(targetPath, resultBuffer);
|
|
91
|
+
return { success: true, editsApplied: 1 };
|
|
110
92
|
}
|
|
111
93
|
/**
|
|
112
94
|
* Get PDF file information
|
|
@@ -24,7 +24,8 @@ export declare class TextFileHandler implements FileHandler {
|
|
|
24
24
|
write(path: string, content: string, mode?: 'rewrite' | 'append'): Promise<void>;
|
|
25
25
|
getInfo(path: string): Promise<FileInfo>;
|
|
26
26
|
/**
|
|
27
|
-
* Count lines in text content
|
|
27
|
+
* Count lines in text content the way read_file reads them (lineBreakBetween).
|
|
28
|
+
* A line break at the very end ends the last line; it doesn't start another.
|
|
28
29
|
* Made static and public for use by other modules (e.g., writeFile telemetry in filesystem.ts)
|
|
29
30
|
*/
|
|
30
31
|
static countLines(content: string): number;
|
|
@@ -46,19 +47,34 @@ export declare class TextFileHandler implements FileHandler {
|
|
|
46
47
|
*/
|
|
47
48
|
private readFileWithSmartPositioning;
|
|
48
49
|
/**
|
|
49
|
-
* Read last N lines efficiently by reading file
|
|
50
|
+
* Read last N lines efficiently: find where they start by reading the file
|
|
51
|
+
* backwards, then read them from there as readline does for smaller files
|
|
50
52
|
*/
|
|
51
53
|
private readLastNLinesReverse;
|
|
54
|
+
/**
|
|
55
|
+
* Byte position where the last `n` lines start, found by reading 8 KB chunks
|
|
56
|
+
* backwards and finding line breaks by lineBreakBetween. A line break at the
|
|
57
|
+
* very end ends the last line instead of starting another.
|
|
58
|
+
*/
|
|
59
|
+
private findLastLinesStart;
|
|
52
60
|
/**
|
|
53
61
|
* Read from end using readline with circular buffer
|
|
54
62
|
*/
|
|
55
63
|
private readFromEndWithReadline;
|
|
56
64
|
/**
|
|
57
|
-
* Read from start/middle using readline
|
|
65
|
+
* Read from start/middle using readline. With `start`, reading begins at
|
|
66
|
+
* that byte, where line `startLine` begins.
|
|
58
67
|
*/
|
|
59
68
|
private readFromStartWithReadline;
|
|
60
69
|
/**
|
|
61
|
-
* Read from
|
|
70
|
+
* Read from a deep offset in a very large file: find the byte where line
|
|
71
|
+
* `offset` starts, then read from there with readline
|
|
72
|
+
*/
|
|
73
|
+
private readFromDeepOffset;
|
|
74
|
+
/**
|
|
75
|
+
* Byte position where line `line` (0-based) starts, or the file size when
|
|
76
|
+
* the file has fewer lines. Counts line breaks by lineBreakBetween in raw
|
|
77
|
+
* bytes, which is much faster than reading lines.
|
|
62
78
|
*/
|
|
63
|
-
private
|
|
79
|
+
private findLineStart;
|
|
64
80
|
}
|
package/dist/utils/files/text.js
CHANGED
|
@@ -24,10 +24,28 @@ const FILE_SIZE_LIMITS = {
|
|
|
24
24
|
};
|
|
25
25
|
const READ_PERFORMANCE_THRESHOLDS = {
|
|
26
26
|
SMALL_READ_THRESHOLD: 100, // For very small reads
|
|
27
|
-
DEEP_OFFSET_THRESHOLD: 1000, //
|
|
28
|
-
SAMPLE_SIZE: 10000, // Sample size for estimation
|
|
27
|
+
DEEP_OFFSET_THRESHOLD: 1000, // Past this line, find where it starts first
|
|
29
28
|
CHUNK_SIZE: 8192, // 8KB chunks for reverse reading
|
|
29
|
+
SCAN_CHUNK_SIZE: 64 * 1024, // 64KB chunks for counting line breaks forward
|
|
30
30
|
};
|
|
31
|
+
const LINE_FEED = 0x0a;
|
|
32
|
+
const CARRIAGE_RETURN = 0x0d;
|
|
33
|
+
/**
|
|
34
|
+
* The line breaks read_file reads by (readline's): LF, CRLF as one break, and
|
|
35
|
+
* a lone CR. True when a line ends between two adjacent code units: the bytes
|
|
36
|
+
* of UTF-8 text (an LF or CR byte is never part of another character) or a
|
|
37
|
+
* string's characters. `after` is -1 at the end of the text.
|
|
38
|
+
*/
|
|
39
|
+
function lineBreakBetween(before, after) {
|
|
40
|
+
return before === LINE_FEED || (before === CARRIAGE_RETURN && after !== LINE_FEED);
|
|
41
|
+
}
|
|
42
|
+
function throwIfAborted(signal) {
|
|
43
|
+
if (signal?.aborted) {
|
|
44
|
+
const err = new Error('Read aborted');
|
|
45
|
+
err.code = 'ABORT_ERR';
|
|
46
|
+
throw err;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
31
49
|
/**
|
|
32
50
|
* Text file handler implementation
|
|
33
51
|
* Binary detection is done at the factory level - this handler assumes file is text
|
|
@@ -101,19 +119,19 @@ export class TextFileHandler {
|
|
|
101
119
|
// Private Helper Methods (extracted from filesystem.ts)
|
|
102
120
|
// ========================================================================
|
|
103
121
|
/**
|
|
104
|
-
* Count lines in text content
|
|
122
|
+
* Count lines in text content the way read_file reads them (lineBreakBetween).
|
|
123
|
+
* A line break at the very end ends the last line; it doesn't start another.
|
|
105
124
|
* Made static and public for use by other modules (e.g., writeFile telemetry in filesystem.ts)
|
|
106
125
|
*/
|
|
107
126
|
static countLines(content) {
|
|
108
127
|
if (content === '')
|
|
109
128
|
return 0;
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return lines.length - 1;
|
|
129
|
+
let lines = 1;
|
|
130
|
+
for (let i = 1; i < content.length; i++) {
|
|
131
|
+
if (lineBreakBetween(content.charCodeAt(i - 1), content.charCodeAt(i)))
|
|
132
|
+
lines++;
|
|
115
133
|
}
|
|
116
|
-
return lines
|
|
134
|
+
return lines;
|
|
117
135
|
}
|
|
118
136
|
/**
|
|
119
137
|
* Get file line count (for files under size limit)
|
|
@@ -231,7 +249,7 @@ export class TextFileHandler {
|
|
|
231
249
|
}
|
|
232
250
|
else {
|
|
233
251
|
if (offset > READ_PERFORMANCE_THRESHOLDS.DEEP_OFFSET_THRESHOLD) {
|
|
234
|
-
return await this.
|
|
252
|
+
return await this.readFromDeepOffset(filePath, offset, length, mimeType, includeStatusMessage, totalLines, signal);
|
|
235
253
|
}
|
|
236
254
|
else {
|
|
237
255
|
return await this.readFromStartWithReadline(filePath, offset, length, mimeType, includeStatusMessage, totalLines, signal);
|
|
@@ -240,40 +258,40 @@ export class TextFileHandler {
|
|
|
240
258
|
}
|
|
241
259
|
}
|
|
242
260
|
/**
|
|
243
|
-
* Read last N lines efficiently by reading file
|
|
261
|
+
* Read last N lines efficiently: find where they start by reading the file
|
|
262
|
+
* backwards, then read them from there as readline does for smaller files
|
|
244
263
|
*/
|
|
245
264
|
async readLastNLinesReverse(filePath, n, mimeType, includeStatusMessage = true, fileTotalLines, signal) {
|
|
265
|
+
const start = await this.findLastLinesStart(filePath, n, signal);
|
|
266
|
+
return this.readFromEndWithReadline(filePath, n, mimeType, includeStatusMessage, fileTotalLines, signal, undefined, start);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Byte position where the last `n` lines start, found by reading 8 KB chunks
|
|
270
|
+
* backwards and finding line breaks by lineBreakBetween. A line break at the
|
|
271
|
+
* very end ends the last line instead of starting another.
|
|
272
|
+
*/
|
|
273
|
+
async findLastLinesStart(filePath, n, signal) {
|
|
246
274
|
const fd = await fs.open(filePath, 'r');
|
|
247
275
|
try {
|
|
248
|
-
const
|
|
249
|
-
const
|
|
250
|
-
let position =
|
|
251
|
-
let
|
|
252
|
-
let
|
|
253
|
-
while (position > 0
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
err.code = 'ABORT_ERR';
|
|
257
|
-
throw err;
|
|
258
|
-
}
|
|
259
|
-
const readSize = Math.min(READ_PERFORMANCE_THRESHOLDS.CHUNK_SIZE, position);
|
|
276
|
+
const { size } = await fd.stat();
|
|
277
|
+
const buffer = Buffer.alloc(READ_PERFORMANCE_THRESHOLDS.CHUNK_SIZE);
|
|
278
|
+
let position = size;
|
|
279
|
+
let breaks = 0;
|
|
280
|
+
let after = -1; // the byte after buffer[i]; -1 at the end of the file
|
|
281
|
+
while (position > 0) {
|
|
282
|
+
throwIfAborted(signal);
|
|
283
|
+
const readSize = Math.min(buffer.length, position);
|
|
260
284
|
position -= readSize;
|
|
261
|
-
const buffer = Buffer.alloc(readSize);
|
|
262
285
|
await fd.read(buffer, 0, readSize, position);
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
lines.unshift(partialLine);
|
|
286
|
+
for (let i = readSize - 1; i >= 0; i--) {
|
|
287
|
+
const byte = buffer[i];
|
|
288
|
+
if (after !== -1 && lineBreakBetween(byte, after) && ++breaks === n) {
|
|
289
|
+
return position + i + 1;
|
|
290
|
+
}
|
|
291
|
+
after = byte;
|
|
292
|
+
}
|
|
271
293
|
}
|
|
272
|
-
|
|
273
|
-
const content = includeStatusMessage
|
|
274
|
-
? `${this.generateEnhancedStatusMessage(result.length, -n, fileTotalLines, true)}\n\n${result.join('\n')}`
|
|
275
|
-
: result.join('\n');
|
|
276
|
-
return { content, mimeType, metadata: {} };
|
|
294
|
+
return 0;
|
|
277
295
|
}
|
|
278
296
|
finally {
|
|
279
297
|
await fd.close();
|
|
@@ -312,15 +330,16 @@ export class TextFileHandler {
|
|
|
312
330
|
return { content, mimeType, metadata: {} };
|
|
313
331
|
}
|
|
314
332
|
/**
|
|
315
|
-
* Read from start/middle using readline
|
|
333
|
+
* Read from start/middle using readline. With `start`, reading begins at
|
|
334
|
+
* that byte, where line `startLine` begins.
|
|
316
335
|
*/
|
|
317
|
-
async readFromStartWithReadline(filePath, offset, length, mimeType, includeStatusMessage = true, fileTotalLines, signal, encoding, start) {
|
|
336
|
+
async readFromStartWithReadline(filePath, offset, length, mimeType, includeStatusMessage = true, fileTotalLines, signal, encoding, start, startLine = 0) {
|
|
318
337
|
const rl = createInterface({
|
|
319
338
|
input: createReadStream(filePath, { signal, encoding, start }),
|
|
320
339
|
crlfDelay: Infinity
|
|
321
340
|
});
|
|
322
341
|
const result = [];
|
|
323
|
-
let lineNumber =
|
|
342
|
+
let lineNumber = startLine;
|
|
324
343
|
for await (const line of rl) {
|
|
325
344
|
if (lineNumber >= offset && result.length < length) {
|
|
326
345
|
result.push(line);
|
|
@@ -341,57 +360,42 @@ export class TextFileHandler {
|
|
|
341
360
|
}
|
|
342
361
|
}
|
|
343
362
|
/**
|
|
344
|
-
* Read from
|
|
363
|
+
* Read from a deep offset in a very large file: find the byte where line
|
|
364
|
+
* `offset` starts, then read from there with readline
|
|
345
365
|
*/
|
|
346
|
-
async
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
break;
|
|
359
|
-
}
|
|
360
|
-
rl.close();
|
|
361
|
-
if (sampleLines === 0) {
|
|
362
|
-
return await this.readFromStartWithReadline(filePath, offset, length, mimeType, includeStatusMessage, fileTotalLines, signal);
|
|
363
|
-
}
|
|
364
|
-
// Estimate position
|
|
365
|
-
const avgLineLength = bytesRead / sampleLines;
|
|
366
|
-
const estimatedBytePosition = Math.floor(offset * avgLineLength);
|
|
366
|
+
async readFromDeepOffset(filePath, offset, length, mimeType, includeStatusMessage = true, fileTotalLines, signal) {
|
|
367
|
+
const start = await this.findLineStart(filePath, offset, signal);
|
|
368
|
+
return this.readFromStartWithReadline(filePath, offset, length, mimeType, includeStatusMessage, fileTotalLines, signal, undefined, start, offset);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Byte position where line `line` (0-based) starts, or the file size when
|
|
372
|
+
* the file has fewer lines. Counts line breaks by lineBreakBetween in raw
|
|
373
|
+
* bytes, which is much faster than reading lines.
|
|
374
|
+
*/
|
|
375
|
+
async findLineStart(filePath, line, signal) {
|
|
376
|
+
if (line <= 0)
|
|
377
|
+
return 0;
|
|
367
378
|
const fd = await fs.open(filePath, 'r');
|
|
368
379
|
try {
|
|
369
|
-
const
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
386
|
-
else {
|
|
387
|
-
break;
|
|
380
|
+
const buffer = Buffer.alloc(READ_PERFORMANCE_THRESHOLDS.SCAN_CHUNK_SIZE);
|
|
381
|
+
let position = 0;
|
|
382
|
+
let breaks = 0;
|
|
383
|
+
let previous = -1;
|
|
384
|
+
for (;;) {
|
|
385
|
+
throwIfAborted(signal);
|
|
386
|
+
const { bytesRead } = await fd.read(buffer, 0, buffer.length, position);
|
|
387
|
+
if (bytesRead === 0)
|
|
388
|
+
return position;
|
|
389
|
+
for (let i = 0; i < bytesRead; i++) {
|
|
390
|
+
const byte = buffer[i];
|
|
391
|
+
// A line break before this byte: the next line starts here
|
|
392
|
+
if (lineBreakBetween(previous, byte) && ++breaks === line) {
|
|
393
|
+
return position + i;
|
|
394
|
+
}
|
|
395
|
+
previous = byte;
|
|
388
396
|
}
|
|
397
|
+
position += bytesRead;
|
|
389
398
|
}
|
|
390
|
-
rl2.close();
|
|
391
|
-
const content = includeStatusMessage
|
|
392
|
-
? `${this.generateEnhancedStatusMessage(result.length, offset, fileTotalLines, false)}\n\n${result.join('\n')}`
|
|
393
|
-
: result.join('\n');
|
|
394
|
-
return { content, mimeType, metadata: {} };
|
|
395
399
|
}
|
|
396
400
|
finally {
|
|
397
401
|
await fd.close();
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tools whose structuredContent is for Desktop Commander's own code and tests
|
|
3
|
+
* only: fixes added it next to the text, and a client must keep receiving what
|
|
4
|
+
* it received before (no new information in tool results). It is dropped here,
|
|
5
|
+
* before the result is recorded in the tool-call history or sent. Tools that
|
|
6
|
+
* already sent structuredContent (the read_file/edit widgets, get_config) are
|
|
7
|
+
* not listed and keep it.
|
|
8
|
+
*/
|
|
9
|
+
const INTERNAL_STRUCTURED_CONTENT_TOOLS = new Set([
|
|
10
|
+
'write_pdf',
|
|
11
|
+
'start_process',
|
|
12
|
+
'interact_with_process',
|
|
13
|
+
'list_sessions',
|
|
14
|
+
'start_search',
|
|
15
|
+
'get_more_search_results',
|
|
16
|
+
]);
|
|
17
|
+
/** The result as the client receives it: without structuredContent kept internal */
|
|
18
|
+
export function withoutInternalFacts(toolName, result) {
|
|
19
|
+
if (!INTERNAL_STRUCTURED_CONTENT_TOOLS.has(toolName) || !('structuredContent' in result))
|
|
20
|
+
return result;
|
|
21
|
+
const { structuredContent: _internal, ...sent } = result;
|
|
22
|
+
return sent;
|
|
23
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A package only some files need, declared next to the code that uses it,
|
|
3
|
+
* whose load() loads it on first use (#715). Loading them before initialize
|
|
4
|
+
* took 25-90 s on one reporter's machine and timed the client out, and loading
|
|
5
|
+
* one inside the tool call that first needs it can take longer than a client
|
|
6
|
+
* waits. So the server preloads them right after initialize, one at a time
|
|
7
|
+
* (preloadFileSupport() in utils/files/factory.ts), and until one is loaded the
|
|
8
|
+
* calls that need it answer at once that it is still loading. preload() loads
|
|
9
|
+
* it on a later turn of the event loop, so a call that starts it answers
|
|
10
|
+
* first. `error` says why the last preload failed. Node keeps a failed
|
|
11
|
+
* import() failed, so a package loaded with import() then `needsRestart`.
|
|
12
|
+
*/
|
|
13
|
+
export declare class LazyPackage<T> {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly support: string;
|
|
16
|
+
readonly load: () => T;
|
|
17
|
+
loaded: boolean;
|
|
18
|
+
error?: string;
|
|
19
|
+
needsRestart: boolean;
|
|
20
|
+
private running?;
|
|
21
|
+
constructor(name: string, support: string, load: () => T);
|
|
22
|
+
preload(): Promise<void>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { logger } from './logger.js';
|
|
2
|
+
/**
|
|
3
|
+
* A package only some files need, declared next to the code that uses it,
|
|
4
|
+
* whose load() loads it on first use (#715). Loading them before initialize
|
|
5
|
+
* took 25-90 s on one reporter's machine and timed the client out, and loading
|
|
6
|
+
* one inside the tool call that first needs it can take longer than a client
|
|
7
|
+
* waits. So the server preloads them right after initialize, one at a time
|
|
8
|
+
* (preloadFileSupport() in utils/files/factory.ts), and until one is loaded the
|
|
9
|
+
* calls that need it answer at once that it is still loading. preload() loads
|
|
10
|
+
* it on a later turn of the event loop, so a call that starts it answers
|
|
11
|
+
* first. `error` says why the last preload failed. Node keeps a failed
|
|
12
|
+
* import() failed, so a package loaded with import() then `needsRestart`.
|
|
13
|
+
*/
|
|
14
|
+
export class LazyPackage {
|
|
15
|
+
constructor(name, support, load) {
|
|
16
|
+
this.name = name;
|
|
17
|
+
this.support = support;
|
|
18
|
+
this.load = load;
|
|
19
|
+
this.loaded = false;
|
|
20
|
+
this.needsRestart = false;
|
|
21
|
+
}
|
|
22
|
+
preload() {
|
|
23
|
+
this.running ?? (this.running = new Promise((resolve) => setImmediate(resolve)).then(async () => {
|
|
24
|
+
let loading;
|
|
25
|
+
try {
|
|
26
|
+
loading = this.load();
|
|
27
|
+
await loading;
|
|
28
|
+
this.loaded = true;
|
|
29
|
+
this.error = undefined;
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
this.error = error instanceof Error ? error.message : String(error);
|
|
33
|
+
this.needsRestart = loading instanceof Promise;
|
|
34
|
+
logger.error(`Loading ${this.name} failed: ${this.error}`);
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
this.running = undefined;
|
|
38
|
+
}
|
|
39
|
+
}));
|
|
40
|
+
return this.running;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { execFile, spawn } from 'child_process';
|
|
2
2
|
import os from 'os';
|
|
3
3
|
import { logToStderr } from './logger.js';
|
|
4
|
+
import { resolveProgramPath } from './shell.js';
|
|
4
5
|
/**
|
|
5
6
|
* Open a URL in the default browser (cross-platform)
|
|
6
7
|
* Uses execFile/spawn with args array to avoid shell injection
|
|
@@ -24,7 +25,7 @@ export async function openBrowser(url) {
|
|
|
24
25
|
break;
|
|
25
26
|
case 'win32':
|
|
26
27
|
// Windows 'start' is a shell builtin, use spawn with shell but pass URL as separate arg
|
|
27
|
-
spawn('cmd', ['/c', 'start', '', url], { shell: false, windowsHide: true }).on('close', (code) => {
|
|
28
|
+
spawn(resolveProgramPath('cmd'), ['/c', 'start', '', url], { shell: false, windowsHide: true }).on('close', (code) => {
|
|
28
29
|
code === 0 ? resolve() : reject(new Error(`Exit code ${code}`));
|
|
29
30
|
});
|
|
30
31
|
break;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* REPL and Process State Detection Utilities
|
|
3
|
-
* Detects
|
|
3
|
+
* Detects from its output whether a running process is waiting for input.
|
|
4
|
+
* Whether it has finished is known from its exit, never from its output
|
|
5
|
+
* (see TerminalManager.getProcessState).
|
|
4
6
|
*/
|
|
5
7
|
export interface ProcessState {
|
|
6
8
|
isWaitingForInput: boolean;
|
|
@@ -10,13 +12,30 @@ export interface ProcessState {
|
|
|
10
12
|
lastOutput: string;
|
|
11
13
|
}
|
|
12
14
|
/**
|
|
13
|
-
*
|
|
15
|
+
* How much of the end of the output state detection examines. A prompt is
|
|
16
|
+
* the last thing a process writes before it waits for input, so the end of
|
|
17
|
+
* the output decides the state; examining only this much keeps detection's
|
|
18
|
+
* cost the same however much output there is.
|
|
14
19
|
*/
|
|
15
|
-
export declare
|
|
20
|
+
export declare const STATE_DETECTION_TAIL_CHARS = 4096;
|
|
16
21
|
/**
|
|
17
|
-
*
|
|
22
|
+
* Analyze a running process's output to determine whether it is waiting for
|
|
23
|
+
* input. Output text never makes a process finished: a line such as
|
|
24
|
+
* "Error: retrying" doesn't end it.
|
|
25
|
+
*
|
|
26
|
+
* `output` is what the process wrote most recently. For a process writing to
|
|
27
|
+
* both stdout and stderr it can be the recent output of each stream that may
|
|
28
|
+
* have written last (see TerminalManager.getProcessState): their relative
|
|
29
|
+
* order is unknown, so the process is waiting for input if any of them ends
|
|
30
|
+
* in a prompt. Only the last STATE_DETECTION_TAIL_CHARS of each are examined.
|
|
18
31
|
*/
|
|
19
|
-
export declare function
|
|
32
|
+
export declare function analyzeProcessState(output: string | readonly string[], pid?: number): ProcessState;
|
|
33
|
+
/**
|
|
34
|
+
* Clean output by removing prompts and input echoes. `readAtPrompt`: whether
|
|
35
|
+
* the process was waiting at a prompt when the input was sent; one that
|
|
36
|
+
* wasn't (a shell reading commands without a prompt) prints none for it.
|
|
37
|
+
*/
|
|
38
|
+
export declare function cleanProcessOutput(output: string, inputSent?: string, readAtPrompt?: boolean): string;
|
|
20
39
|
/**
|
|
21
40
|
* Format process state for user display
|
|
22
41
|
*/
|