@wonderwhy-er/desktop-commander 0.2.52 → 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/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 +2 -3
- 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.js +19 -25
- package/dist/remote-device/device.d.ts +14 -0
- package/dist/remote-device/device.js +137 -30
- 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 +12 -0
- package/dist/remote-device/remote-channel.js +55 -8
- 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 +44 -11
- 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
|
@@ -11,7 +11,7 @@ export declare function handleReadFile(args: unknown): Promise<ServerResult>;
|
|
|
11
11
|
/**
|
|
12
12
|
* Handle read_multiple_files command
|
|
13
13
|
*/
|
|
14
|
-
export declare function handleReadMultipleFiles(args: unknown): Promise<ServerResult>;
|
|
14
|
+
export declare function handleReadMultipleFiles(args: unknown, stillLoadingError?: (filePath: string) => string | undefined): Promise<ServerResult>;
|
|
15
15
|
/**
|
|
16
16
|
* Handle write_file command
|
|
17
17
|
*/
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { readFile, readMultipleFiles, writeFile, createDirectory, listDirectory, moveFile, getFileInfo, writePdf, getDefaultEditorMetadata } from '../tools/filesystem.js';
|
|
2
|
+
import { TextFileHandler } from '../utils/files/text.js';
|
|
2
3
|
import { withTimeout } from '../utils/withTimeout.js';
|
|
3
4
|
import { createErrorResponse } from '../error-handlers.js';
|
|
4
5
|
import { configManager } from '../config-manager.js';
|
|
@@ -58,18 +59,22 @@ export async function handleReadFile(args) {
|
|
|
58
59
|
if (!config) {
|
|
59
60
|
return createErrorResponse('Configuration not available');
|
|
60
61
|
}
|
|
62
|
+
// The schema fills in `length: 1000` (the default tools/list shows), so
|
|
63
|
+
// whether the caller sent a length is read from the raw arguments: an
|
|
64
|
+
// omitted length follows the fileReadLineLimit setting, as documented.
|
|
61
65
|
const defaultLimit = config.fileReadLineLimit ?? 1000;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
sheetParam = parseInt(parsed.sheet, 10);
|
|
66
|
-
}
|
|
66
|
+
const lengthGiven = args.length !== undefined;
|
|
67
|
+
// The sheet goes as given: the Excel handler knows the sheet names, so it
|
|
68
|
+
// decides whether "2024" is an index or a sheet named 2024
|
|
67
69
|
const options = {
|
|
68
70
|
isUrl: parsed.isUrl,
|
|
69
71
|
offset: parsed.offset ?? 0,
|
|
70
|
-
length: parsed.length
|
|
71
|
-
sheet:
|
|
72
|
-
range: parsed.range
|
|
72
|
+
length: lengthGiven ? parsed.length : defaultLimit,
|
|
73
|
+
sheet: parsed.sheet,
|
|
74
|
+
range: parsed.range,
|
|
75
|
+
// The preview widget draws an SVG as an image (ui/file-preview/src/image-preview.ts);
|
|
76
|
+
// everyone else reads it as the text it is
|
|
77
|
+
svgAsImage: parsed.origin === 'ui'
|
|
73
78
|
};
|
|
74
79
|
// Resolve to absolute path for local files (not URLs) so "Open in folder" works
|
|
75
80
|
const resolvedFilePath = parsed.isUrl
|
|
@@ -193,9 +198,9 @@ export async function handleReadFile(args) {
|
|
|
193
198
|
/**
|
|
194
199
|
* Handle read_multiple_files command
|
|
195
200
|
*/
|
|
196
|
-
export async function handleReadMultipleFiles(args) {
|
|
201
|
+
export async function handleReadMultipleFiles(args, stillLoadingError) {
|
|
197
202
|
const parsed = ReadMultipleFilesArgsSchema.parse(args);
|
|
198
|
-
const fileResults = await readMultipleFiles(parsed.paths);
|
|
203
|
+
const fileResults = await readMultipleFiles(parsed.paths, stillLoadingError);
|
|
199
204
|
// Create a text summary of all files
|
|
200
205
|
const textSummary = fileResults.map(result => {
|
|
201
206
|
if (result.error) {
|
|
@@ -215,10 +220,13 @@ export async function handleReadMultipleFiles(args) {
|
|
|
215
220
|
const contentItems = [];
|
|
216
221
|
// Add the text summary
|
|
217
222
|
contentItems.push({ type: "text", text: textSummary });
|
|
218
|
-
// Add each file content
|
|
223
|
+
// Add each file content after its path: several images, or a PDF's pages,
|
|
224
|
+
// can't be told apart by their order alone
|
|
219
225
|
for (const result of fileResults) {
|
|
220
226
|
if (!result.error && result.content !== undefined) {
|
|
227
|
+
const header = `\n--- ${result.path} contents: ---\n`;
|
|
221
228
|
if (result.isPdf) {
|
|
229
|
+
contentItems.push({ type: "text", text: header });
|
|
222
230
|
result.payload?.pages.forEach((page, i) => {
|
|
223
231
|
page.images.forEach((image, i) => {
|
|
224
232
|
contentItems.push({
|
|
@@ -235,6 +243,7 @@ export async function handleReadMultipleFiles(args) {
|
|
|
235
243
|
}
|
|
236
244
|
else if (result.isImage && result.mimeType) {
|
|
237
245
|
// For image files, add an image content item
|
|
246
|
+
contentItems.push({ type: "text", text: header });
|
|
238
247
|
contentItems.push({
|
|
239
248
|
type: "image",
|
|
240
249
|
data: result.content,
|
|
@@ -245,7 +254,7 @@ export async function handleReadMultipleFiles(args) {
|
|
|
245
254
|
// For text files, add a text summary
|
|
246
255
|
contentItems.push({
|
|
247
256
|
type: "text",
|
|
248
|
-
text:
|
|
257
|
+
text: `${header}${result.content}`
|
|
249
258
|
});
|
|
250
259
|
}
|
|
251
260
|
}
|
|
@@ -284,9 +293,8 @@ export async function handleWriteFile(args) {
|
|
|
284
293
|
// Get the line limit from configuration
|
|
285
294
|
const config = await configManager.getConfig();
|
|
286
295
|
const MAX_LINES = config.fileWriteLineLimit ?? 50; // Default to 50 if not set
|
|
287
|
-
//
|
|
288
|
-
const
|
|
289
|
-
const lineCount = lines.length;
|
|
296
|
+
// Lines as read_file counts them: a final line break doesn't start another line
|
|
297
|
+
const lineCount = TextFileHandler.countLines(parsed.content);
|
|
290
298
|
let errorMessage = "";
|
|
291
299
|
if (lineCount > MAX_LINES) {
|
|
292
300
|
errorMessage = `✅ File written successfully! (${lineCount} lines)
|
|
@@ -427,10 +435,12 @@ export async function handleGetFileInfo(args) {
|
|
|
427
435
|
export async function handleWritePdf(args) {
|
|
428
436
|
try {
|
|
429
437
|
const parsed = WritePdfArgsSchema.parse(args);
|
|
430
|
-
await writePdf(parsed.path, parsed.content, parsed.outputPath, parsed.options);
|
|
438
|
+
const ignoredOptions = await writePdf(parsed.path, parsed.content, parsed.outputPath, parsed.options);
|
|
431
439
|
const targetPath = parsed.outputPath || parsed.path;
|
|
432
440
|
return {
|
|
433
441
|
content: [{ type: "text", text: `Successfully wrote PDF to ${targetPath}${parsed.outputPath ? `\nOriginal file: ${parsed.path}` : ''}` }],
|
|
442
|
+
// Which options were ignored, and why: internal (tests), not sent to the client (see utils/internal-facts.ts)
|
|
443
|
+
...(ignoredOptions.length > 0 ? { structuredContent: { ignoredOptions } } : {}),
|
|
434
444
|
};
|
|
435
445
|
}
|
|
436
446
|
catch (error) {
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { SearchResult, SearchState, Unsearched } from '../search-manager.js';
|
|
2
|
+
import type { ServerResult } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The words of the search answers (start_search, get_more_search_results), in
|
|
5
|
+
* one place (#768). Each answer ends saying how the search ended: its outcome
|
|
6
|
+
* (SearchOutcome in search-manager.ts).
|
|
7
|
+
*/
|
|
8
|
+
export declare const SEARCH_WORDS: {
|
|
9
|
+
/** "Total results: …", the same in both answers */
|
|
10
|
+
total: (matches: number, rows: number) => string;
|
|
11
|
+
matchRow: (file: string, line: number | undefined, text: string) => string;
|
|
12
|
+
contextRow: (file: string, line: number | undefined, text: string) => string;
|
|
13
|
+
fileRow: (file: string) => string;
|
|
14
|
+
/** A page with no results */
|
|
15
|
+
noMatches: string;
|
|
16
|
+
noneAtOffset: (offset: number, rows: number) => string;
|
|
17
|
+
noneYet: (offset: number, rows: number) => string;
|
|
18
|
+
/** After a page's results: whether there are more */
|
|
19
|
+
moreResults: (nextOffset: number) => string;
|
|
20
|
+
moreMayCome: string;
|
|
21
|
+
inProgress: string;
|
|
22
|
+
/** How the search ended, by outcome */
|
|
23
|
+
completed: string;
|
|
24
|
+
timedOut: (ms: number | undefined) => string;
|
|
25
|
+
stopped: string;
|
|
26
|
+
maxResults: (n: number | undefined) => string;
|
|
27
|
+
/** Partial, when the only files it couldn't search were ones it may not read: as it always said */
|
|
28
|
+
permissionsOnly: string;
|
|
29
|
+
partial: (reasons: string[]) => string;
|
|
30
|
+
unsearched: {
|
|
31
|
+
permissions: () => string;
|
|
32
|
+
ripgrep_error: (u: Unsearched) => string;
|
|
33
|
+
ripgrep_ended: (u: Unsearched) => string;
|
|
34
|
+
excel_search: (u: Unsearched) => string;
|
|
35
|
+
docx_search: (u: Unsearched) => string;
|
|
36
|
+
excel_file: (u: Unsearched) => string;
|
|
37
|
+
docx_file: (u: Unsearched) => string;
|
|
38
|
+
excel_folder: (u: Unsearched) => string;
|
|
39
|
+
docx_folder: (u: Unsearched) => string;
|
|
40
|
+
};
|
|
41
|
+
/** A failed search's whole answer */
|
|
42
|
+
failedWithError: (sessionId: string, error: string) => string;
|
|
43
|
+
failedRipgrepEnded: (sessionId: string, how: string) => string;
|
|
44
|
+
};
|
|
45
|
+
/** start_search's answer: the search as it is once it started, with its first results */
|
|
46
|
+
export declare function startSearchAnswer(search: SearchState & {
|
|
47
|
+
results: SearchResult[];
|
|
48
|
+
}, args: {
|
|
49
|
+
searchType: 'files' | 'content';
|
|
50
|
+
pattern: string;
|
|
51
|
+
path: string;
|
|
52
|
+
}): ServerResult;
|
|
53
|
+
/** get_more_search_results' answer: one page of a search's results (a negative offset: its last results) */
|
|
54
|
+
export declare function searchResultsAnswer(page: SearchState & {
|
|
55
|
+
results: SearchResult[];
|
|
56
|
+
returnedCount: number;
|
|
57
|
+
hasMoreResults: boolean;
|
|
58
|
+
}, offset: number): ServerResult;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { SHOWN_TEXT_CHARS, MAX_OUTPUT_LINE_CHARS } from '../search-manager.js';
|
|
2
|
+
/**
|
|
3
|
+
* The words of the search answers (start_search, get_more_search_results), in
|
|
4
|
+
* one place (#768). Each answer ends saying how the search ended: its outcome
|
|
5
|
+
* (SearchOutcome in search-manager.ts).
|
|
6
|
+
*/
|
|
7
|
+
export const SEARCH_WORDS = {
|
|
8
|
+
/** "Total results: …", the same in both answers */
|
|
9
|
+
total: (matches, rows) => `${matches === 1 ? '1 match' : `${matches} matches`}${rows > matches ? ` (${rows} rows with context)` : ''}`,
|
|
10
|
+
matchRow: (file, line, text) => `📄 ${file}:${line} - ${text}`,
|
|
11
|
+
contextRow: (file, line, text) => ` ${file}:${line} · ${text}`,
|
|
12
|
+
fileRow: (file) => `📁 ${file}`,
|
|
13
|
+
/** A page with no results */
|
|
14
|
+
noMatches: 'No matches found.',
|
|
15
|
+
noneAtOffset: (offset, rows) => `No results at offset ${offset} (${rows} in total).`,
|
|
16
|
+
noneYet: (offset, rows) => `Still running: ${rows === 1 ? '1 result' : `${rows} results`} so far${offset >= 0 ? `, none at offset ${offset} yet` : ''}.`,
|
|
17
|
+
/** After a page's results: whether there are more */
|
|
18
|
+
moreResults: (nextOffset) => `📖 More results available. Use get_more_search_results with offset: ${nextOffset}`,
|
|
19
|
+
moreMayCome: 'Still running: more may come.',
|
|
20
|
+
inProgress: '🔄 Search in progress. Use get_more_search_results to get more results.',
|
|
21
|
+
/** How the search ended, by outcome */
|
|
22
|
+
completed: '✅ Search completed.',
|
|
23
|
+
timedOut: (ms) => `⏱️ Stopped after ${ms} ms: results may be incomplete.`,
|
|
24
|
+
stopped: '⏹️ Stopped on request: results may be incomplete.',
|
|
25
|
+
maxResults: (n) => `Stopped at maxResults (${n}): there may be more.`,
|
|
26
|
+
/** Partial, when the only files it couldn't search were ones it may not read: as it always said */
|
|
27
|
+
permissionsOnly: '✅ Search completed.\n⚠️ Warning: Some files were inaccessible due to permissions. Results may be incomplete.',
|
|
28
|
+
partial: (reasons) => `⚠️ Completed, but some files couldn't be searched: ${reasons.join('; ')}.`,
|
|
29
|
+
unsearched: {
|
|
30
|
+
permissions: () => 'some files were inaccessible due to permissions',
|
|
31
|
+
ripgrep_error: (u) => `ripgrep: ${u.example}${andMore(u)}`,
|
|
32
|
+
ripgrep_ended: (u) => `ripgrep stopped unexpectedly (${u.example})`,
|
|
33
|
+
excel_search: (u) => `the Excel search failed (${u.example})`,
|
|
34
|
+
docx_search: (u) => `the DOCX search failed (${u.example})`,
|
|
35
|
+
excel_file: (u) => `${counted(u, 'an Excel file', 'Excel files')} couldn't be read (${u.example}${andMore(u)})`,
|
|
36
|
+
docx_file: (u) => `${counted(u, 'a DOCX file', 'DOCX files')} couldn't be read (${u.example}${andMore(u)})`,
|
|
37
|
+
excel_folder: (u) => `${counted(u, 'a folder', 'folders')} couldn't be listed for the Excel search (${u.example}${andMore(u)})`,
|
|
38
|
+
docx_folder: (u) => `${counted(u, 'a folder', 'folders')} couldn't be listed for the DOCX search (${u.example}${andMore(u)})`,
|
|
39
|
+
},
|
|
40
|
+
/** A failed search's whole answer */
|
|
41
|
+
failedWithError: (sessionId, error) => `Search session ${sessionId} encountered an error: ${error}`,
|
|
42
|
+
failedRipgrepEnded: (sessionId, how) => `Search session ${sessionId} failed: ripgrep stopped unexpectedly (${how}).`,
|
|
43
|
+
};
|
|
44
|
+
const andMore = (u) => (u.count > 1 ? ` and ${u.count - 1} more` : '');
|
|
45
|
+
const counted = (u, one, many) => (u.count === 1 ? one : `${u.count} ${many}`);
|
|
46
|
+
/**
|
|
47
|
+
* Which lines a search skipped as too long: how many, in which files. Part of a
|
|
48
|
+
* successful answer, worded so that nobody retries: the same search skips them again.
|
|
49
|
+
*/
|
|
50
|
+
function describeSkippedLines(skipped) {
|
|
51
|
+
const lines = skipped.reduce((sum, { count }) => sum + count, 0);
|
|
52
|
+
const files = skipped.slice(0, 5).map(({ file }) => file || 'a file whose name is not valid UTF-8').join(', ');
|
|
53
|
+
const more = skipped.length > 5 ? ` and ${skipped.length - 5} more files` : '';
|
|
54
|
+
const exclude = skipped.length === 1 ? "that file (filePattern) if it isn't" : "those files (filePattern) if they aren't";
|
|
55
|
+
return `Skipped ${lines === 1 ? 'a line' : `${lines} lines`} over ${Math.round(MAX_OUTPUT_LINE_CHARS / 1024 / 1024)} MB in ${files}${more}: too long to search. ` +
|
|
56
|
+
`Searching again gives the same result; exclude ${exclude} needed.`;
|
|
57
|
+
}
|
|
58
|
+
/** A result as answers list it: a match, a line around one, or a file */
|
|
59
|
+
function resultRow(result) {
|
|
60
|
+
if (result.type !== 'content')
|
|
61
|
+
return SEARCH_WORDS.fileRow(result.file);
|
|
62
|
+
const text = `${result.match?.substring(0, SHOWN_TEXT_CHARS)}${result.match && result.match.length > SHOWN_TEXT_CHARS ? '...' : ''}`;
|
|
63
|
+
return (result.context ? SEARCH_WORDS.contextRow : SEARCH_WORDS.matchRow)(result.file, result.line, text);
|
|
64
|
+
}
|
|
65
|
+
/** How a complete search ended, as its answer's last line(s); undefined while it runs */
|
|
66
|
+
function endedText(state) {
|
|
67
|
+
switch (state.outcome) {
|
|
68
|
+
case 'completed': return SEARCH_WORDS.completed;
|
|
69
|
+
case 'timed_out': return SEARCH_WORDS.timedOut(state.timeLimitMs);
|
|
70
|
+
case 'stopped': return SEARCH_WORDS.stopped;
|
|
71
|
+
case 'max_results': return SEARCH_WORDS.maxResults(state.maxResults);
|
|
72
|
+
case 'partial':
|
|
73
|
+
return state.unsearched.every(u => u.kind === 'permissions')
|
|
74
|
+
? SEARCH_WORDS.permissionsOnly
|
|
75
|
+
: SEARCH_WORDS.partial(state.unsearched.map(u => SEARCH_WORDS.unsearched[u.kind](u)));
|
|
76
|
+
default: return undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** The answer to a search that failed, whichever tool reads it */
|
|
80
|
+
function failedAnswer(state) {
|
|
81
|
+
const failure = state.failure;
|
|
82
|
+
const text = 'error' in failure
|
|
83
|
+
? SEARCH_WORDS.failedWithError(state.sessionId, failure.error)
|
|
84
|
+
: SEARCH_WORDS.failedRipgrepEnded(state.sessionId, failure.ripgrepEnded);
|
|
85
|
+
return {
|
|
86
|
+
content: [{ type: 'text', text }],
|
|
87
|
+
isError: true,
|
|
88
|
+
structuredContent: { sessionId: state.sessionId, isComplete: state.isComplete, outcome: state.outcome },
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** start_search's answer: the search as it is once it started, with its first results */
|
|
92
|
+
export function startSearchAnswer(search, args) {
|
|
93
|
+
if (search.outcome === 'failed')
|
|
94
|
+
return failedAnswer(search);
|
|
95
|
+
let text = `Started ${args.searchType === 'content' ? 'content search' : 'file search'} session: ${search.sessionId}\n`;
|
|
96
|
+
text += `Pattern: "${args.pattern}"\n`;
|
|
97
|
+
text += `Path: ${args.path}\n`;
|
|
98
|
+
text += `Status: ${search.isComplete ? 'COMPLETED' : 'RUNNING'}\n`;
|
|
99
|
+
text += `Runtime: ${Math.round(search.runtime)}ms\n`;
|
|
100
|
+
text += `Total results: ${SEARCH_WORDS.total(search.totalMatches, search.totalResults)}\n\n`;
|
|
101
|
+
if (search.results.length > 0) {
|
|
102
|
+
text += 'Initial results:\n';
|
|
103
|
+
for (const result of search.results.slice(0, 10)) {
|
|
104
|
+
text += `${resultRow(result)}\n`;
|
|
105
|
+
}
|
|
106
|
+
if (search.results.length > 10) {
|
|
107
|
+
text += `... and ${search.results.length - 10} more results\n`;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
text += `\n${endedText(search) ?? SEARCH_WORDS.inProgress}`;
|
|
111
|
+
return {
|
|
112
|
+
content: [{ type: 'text', text }],
|
|
113
|
+
structuredContent: {
|
|
114
|
+
sessionId: search.sessionId,
|
|
115
|
+
isComplete: search.isComplete,
|
|
116
|
+
outcome: search.outcome,
|
|
117
|
+
totalResults: search.totalMatches,
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** get_more_search_results' answer: one page of a search's results (a negative offset: its last results) */
|
|
122
|
+
export function searchResultsAnswer(page, offset) {
|
|
123
|
+
if (page.outcome === 'failed')
|
|
124
|
+
return failedAnswer(page);
|
|
125
|
+
let text = `Search session: ${page.sessionId}\n`;
|
|
126
|
+
text += `Status: ${page.isComplete ? 'COMPLETED' : 'IN PROGRESS'}\n`;
|
|
127
|
+
text += `Runtime: ${Math.round(page.runtime / 1000)}s\n`;
|
|
128
|
+
text += `Total results: ${SEARCH_WORDS.total(page.totalMatches, page.totalResults)}\n`;
|
|
129
|
+
if (page.returnedCount > 0) {
|
|
130
|
+
text += offset < 0
|
|
131
|
+
? `Showing last ${page.returnedCount} results\n`
|
|
132
|
+
: `Showing results ${offset}-${offset + page.returnedCount - 1}\n`;
|
|
133
|
+
}
|
|
134
|
+
text += '\n';
|
|
135
|
+
const nextOffset = offset + page.returnedCount;
|
|
136
|
+
if (page.returnedCount === 0) {
|
|
137
|
+
if (!page.isComplete)
|
|
138
|
+
text += SEARCH_WORDS.noneYet(offset, page.totalResults);
|
|
139
|
+
else
|
|
140
|
+
text += page.totalResults === 0 ? SEARCH_WORDS.noMatches : SEARCH_WORDS.noneAtOffset(offset, page.totalResults);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
text += 'Results:\n';
|
|
144
|
+
for (const result of page.results) {
|
|
145
|
+
text += `${resultRow(result)}\n`;
|
|
146
|
+
}
|
|
147
|
+
if (offset >= 0 && nextOffset < page.totalResults) {
|
|
148
|
+
text += `\n${SEARCH_WORDS.moreResults(nextOffset)}`;
|
|
149
|
+
}
|
|
150
|
+
else if (!page.isComplete) {
|
|
151
|
+
text += `\n${SEARCH_WORDS.moreMayCome}`;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (page.skippedLines.length > 0) {
|
|
155
|
+
text += `\n${describeSkippedLines(page.skippedLines)}`;
|
|
156
|
+
}
|
|
157
|
+
const ended = endedText(page);
|
|
158
|
+
if (ended)
|
|
159
|
+
text += `\n${ended}`;
|
|
160
|
+
return {
|
|
161
|
+
content: [{ type: 'text', text }],
|
|
162
|
+
structuredContent: {
|
|
163
|
+
sessionId: page.sessionId,
|
|
164
|
+
isComplete: page.isComplete,
|
|
165
|
+
outcome: page.outcome,
|
|
166
|
+
totalResults: page.totalResults,
|
|
167
|
+
totalMatches: page.totalMatches,
|
|
168
|
+
returnedCount: page.returnedCount,
|
|
169
|
+
hasMoreResults: page.hasMoreResults,
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { searchManager } from '../search-manager.js';
|
|
2
2
|
import { StartSearchArgsSchema, GetMoreSearchResultsArgsSchema, StopSearchArgsSchema } from '../tools/schemas.js';
|
|
3
3
|
import { capture } from '../utils/capture.js';
|
|
4
|
+
import { startSearchAnswer, searchResultsAnswer } from './search-answers.js';
|
|
4
5
|
/**
|
|
5
6
|
* Handle start_search command
|
|
6
7
|
*/
|
|
@@ -26,36 +27,7 @@ export async function handleStartSearch(args) {
|
|
|
26
27
|
earlyTermination: parsed.data.earlyTermination,
|
|
27
28
|
literalSearch: parsed.data.literalSearch,
|
|
28
29
|
});
|
|
29
|
-
|
|
30
|
-
let output = `Started ${searchTypeText} session: ${result.sessionId}\n`;
|
|
31
|
-
output += `Pattern: "${parsed.data.pattern}"\n`;
|
|
32
|
-
output += `Path: ${parsed.data.path}\n`;
|
|
33
|
-
output += `Status: ${result.isComplete ? 'COMPLETED' : 'RUNNING'}\n`;
|
|
34
|
-
output += `Runtime: ${Math.round(result.runtime)}ms\n`;
|
|
35
|
-
output += `Total results: ${result.totalResults}\n\n`;
|
|
36
|
-
if (result.results.length > 0) {
|
|
37
|
-
output += "Initial results:\n";
|
|
38
|
-
for (const searchResult of result.results.slice(0, 10)) {
|
|
39
|
-
if (searchResult.type === 'content') {
|
|
40
|
-
output += `📄 ${searchResult.file}:${searchResult.line} - ${searchResult.match?.substring(0, 100)}${searchResult.match && searchResult.match.length > 100 ? '...' : ''}\n`;
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
output += `📁 ${searchResult.file}\n`;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
if (result.results.length > 10) {
|
|
47
|
-
output += `... and ${result.results.length - 10} more results\n`;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
if (result.isComplete) {
|
|
51
|
-
output += `\n✅ Search completed.`;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
output += `\n🔄 Search in progress. Use get_more_search_results to get more results.`;
|
|
55
|
-
}
|
|
56
|
-
return {
|
|
57
|
-
content: [{ type: "text", text: output }],
|
|
58
|
-
};
|
|
30
|
+
return startSearchAnswer(result, parsed.data);
|
|
59
31
|
}
|
|
60
32
|
catch (error) {
|
|
61
33
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
@@ -78,68 +50,8 @@ export async function handleGetMoreSearchResults(args) {
|
|
|
78
50
|
};
|
|
79
51
|
}
|
|
80
52
|
try {
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
// Permission errors should not block returning found results
|
|
84
|
-
if (results.isError && results.totalResults === 0 && results.error?.trim()) {
|
|
85
|
-
return {
|
|
86
|
-
content: [{
|
|
87
|
-
type: "text",
|
|
88
|
-
text: `Search session ${parsed.data.sessionId} encountered an error: ${results.error}`
|
|
89
|
-
}],
|
|
90
|
-
isError: true,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
// Format results for display
|
|
94
|
-
let output = `Search session: ${parsed.data.sessionId}\n`;
|
|
95
|
-
output += `Status: ${results.isComplete ? 'COMPLETED' : 'IN PROGRESS'}\n`;
|
|
96
|
-
output += `Runtime: ${Math.round(results.runtime / 1000)}s\n`;
|
|
97
|
-
output += `Total results found: ${results.totalResults} (${results.totalMatches} matches)\n`;
|
|
98
|
-
const offset = parsed.data.offset;
|
|
99
|
-
if (offset < 0) {
|
|
100
|
-
// Negative offset - tail behavior
|
|
101
|
-
output += `Showing last ${results.returnedCount} results\n\n`;
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
// Positive offset - range behavior
|
|
105
|
-
const startPos = offset;
|
|
106
|
-
const endPos = startPos + results.returnedCount - 1;
|
|
107
|
-
output += `Showing results ${startPos}-${endPos}\n\n`;
|
|
108
|
-
}
|
|
109
|
-
if (results.results.length === 0) {
|
|
110
|
-
if (results.isComplete) {
|
|
111
|
-
output += results.totalResults === 0 ? "No matches found." : "No results in this range.";
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
output += "No results yet, search is still running...";
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
output += "Results:\n";
|
|
119
|
-
for (const result of results.results) {
|
|
120
|
-
if (result.type === 'content') {
|
|
121
|
-
output += `📄 ${result.file}:${result.line} - ${result.match?.substring(0, 100)}${result.match && result.match.length > 100 ? '...' : ''}\n`;
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
output += `📁 ${result.file}\n`;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
// Add pagination hints
|
|
129
|
-
if (offset >= 0 && results.hasMoreResults) {
|
|
130
|
-
const nextOffset = offset + results.returnedCount;
|
|
131
|
-
output += `\n📖 More results available. Use get_more_search_results with offset: ${nextOffset}`;
|
|
132
|
-
}
|
|
133
|
-
if (results.isComplete) {
|
|
134
|
-
output += `\n✅ Search completed.`;
|
|
135
|
-
// Warn users if search was incomplete due to permission issues
|
|
136
|
-
if (results.wasIncomplete) {
|
|
137
|
-
output += `\n⚠️ Warning: Some files were inaccessible due to permissions. Results may be incomplete.`;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return {
|
|
141
|
-
content: [{ type: "text", text: output }],
|
|
142
|
-
};
|
|
53
|
+
const page = searchManager.readSearchResults(parsed.data.sessionId, parsed.data.offset, parsed.data.length);
|
|
54
|
+
return searchResultsAnswer(page, parsed.data.offset);
|
|
143
55
|
}
|
|
144
56
|
catch (error) {
|
|
145
57
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
package/dist/index.js
CHANGED
|
@@ -10,8 +10,10 @@ import { runSetup } from './npm-scripts/setup.js';
|
|
|
10
10
|
import { runUninstall } from './npm-scripts/uninstall.js';
|
|
11
11
|
import { capture } from './utils/capture.js';
|
|
12
12
|
import { logToStderr, logger } from './utils/logger.js';
|
|
13
|
+
import { exitProcess } from './utils/exit-process.js';
|
|
13
14
|
import { runRemote } from './npm-scripts/remote.js';
|
|
14
15
|
import { ensureChromeAvailable } from './tools/pdf/markdown.js';
|
|
16
|
+
import { preloadFileSupport } from './utils/files/index.js';
|
|
15
17
|
// Store messages to defer until after initialization
|
|
16
18
|
const deferredMessages = [];
|
|
17
19
|
function deferLog(level, message) {
|
|
@@ -74,21 +76,23 @@ async function runServer() {
|
|
|
74
76
|
error: errorMessage
|
|
75
77
|
});
|
|
76
78
|
logger.error(`Uncaught exception: ${errorMessage}`);
|
|
77
|
-
|
|
79
|
+
exitProcess(1);
|
|
78
80
|
});
|
|
79
|
-
// Handle unhandled rejections
|
|
81
|
+
// Handle unhandled rejections: log and keep serving. The code that created
|
|
82
|
+
// the promise has already moved on (often a library's detached cleanup, as
|
|
83
|
+
// with puppeteer's profile removal), so the server's state is intact, and
|
|
84
|
+
// exiting would drop every running process, session and search.
|
|
80
85
|
process.on('unhandledRejection', async (reason) => {
|
|
81
86
|
const errorMessage = reason instanceof Error ? reason.message : String(reason);
|
|
82
|
-
//
|
|
87
|
+
// A JSON parsing error is logged as before, and not sent to telemetry
|
|
83
88
|
if (errorMessage.includes('JSON') && errorMessage.includes('Unexpected token')) {
|
|
84
89
|
logger.error(`JSON parsing rejection: ${errorMessage}`);
|
|
85
|
-
return;
|
|
90
|
+
return;
|
|
86
91
|
}
|
|
87
92
|
capture('run_server_unhandled_rejection', {
|
|
88
93
|
error: errorMessage
|
|
89
94
|
});
|
|
90
95
|
logger.error(`Unhandled rejection: ${errorMessage}`);
|
|
91
|
-
process.exit(1);
|
|
92
96
|
});
|
|
93
97
|
capture('run_server_start');
|
|
94
98
|
deferLog('info', 'Connecting server...');
|
|
@@ -108,6 +112,9 @@ async function runServer() {
|
|
|
108
112
|
transport.sendLog('info', 'MCP fully initialized, all startup messages sent');
|
|
109
113
|
// Preemptively check/download Chrome for PDF generation (runs in background)
|
|
110
114
|
ensureChromeAvailable();
|
|
115
|
+
// Load the Excel, DOCX and PDF packages, one at a time, now that initialize
|
|
116
|
+
// has been answered; until then the calls that need them answer at once
|
|
117
|
+
preloadFileSupport();
|
|
111
118
|
};
|
|
112
119
|
await server.connect(transport);
|
|
113
120
|
}
|
|
@@ -131,7 +138,7 @@ async function runServer() {
|
|
|
131
138
|
capture('run_server_failed_start_error', {
|
|
132
139
|
error: errorMessage
|
|
133
140
|
});
|
|
134
|
-
|
|
141
|
+
exitProcess(1);
|
|
135
142
|
}
|
|
136
143
|
}
|
|
137
144
|
runServer().catch(async (error) => {
|
|
@@ -146,5 +153,5 @@ runServer().catch(async (error) => {
|
|
|
146
153
|
capture('run_server_fatal_error', {
|
|
147
154
|
error: errorMessage
|
|
148
155
|
});
|
|
149
|
-
|
|
156
|
+
exitProcess(1);
|
|
150
157
|
});
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { MCPDevice, getRemoteDeviceConfigPath } from '../remote-device/device.js';
|
|
2
|
-
import fs from 'fs/promises';
|
|
1
|
+
import { MCPDevice, getRemoteDeviceConfigPath, removeRemoteDeviceConfig } from '../remote-device/device.js';
|
|
3
2
|
import os from 'os';
|
|
4
3
|
import { captureRemote } from '../utils/capture.js';
|
|
5
4
|
const BLUE = '\x1b[34m';
|
|
@@ -43,7 +42,7 @@ Note:
|
|
|
43
42
|
if (process.argv.includes('--logout')) {
|
|
44
43
|
const configPath = getRemoteDeviceConfigPath();
|
|
45
44
|
try {
|
|
46
|
-
await
|
|
45
|
+
await removeRemoteDeviceConfig(configPath);
|
|
47
46
|
console.log('🔓 Logged out locally. Saved Remote MCP device credentials were removed.');
|
|
48
47
|
console.log(` ${configPath}`);
|
|
49
48
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type TransportObservation = {
|
|
2
|
+
stage: 'received';
|
|
3
|
+
payload: any;
|
|
4
|
+
deviceId: string | null;
|
|
5
|
+
userId: string | undefined;
|
|
6
|
+
} | {
|
|
7
|
+
stage: string;
|
|
8
|
+
callId: string;
|
|
9
|
+
deviceId: string | null | undefined;
|
|
10
|
+
fields?: Record<string, string | number>;
|
|
11
|
+
};
|
|
12
|
+
/** Record a device transport checkpoint without affecting command handling. */
|
|
13
|
+
export declare function observeTransport(input: TransportObservation): {
|
|
14
|
+
observation_id: `${string}-${string}-${string}-${string}-${string}`;
|
|
15
|
+
source_process_id: `${string}-${string}-${string}-${string}-${string}`;
|
|
16
|
+
schema_version: number;
|
|
17
|
+
timestamp_utc: string;
|
|
18
|
+
monotonic_ms: number;
|
|
19
|
+
operation_id?: `${string}-${string}-${string}-${string}-${string}` | undefined;
|
|
20
|
+
operation_attempt?: number | undefined;
|
|
21
|
+
call_id: string;
|
|
22
|
+
device_id: string;
|
|
23
|
+
transport: string;
|
|
24
|
+
stage: string;
|
|
25
|
+
notification_id: string;
|
|
26
|
+
attempt_number: number;
|
|
27
|
+
} | null;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { capture } from '../utils/capture.js';
|
|
3
|
+
const ID = /^[A-Za-z0-9_-]{1,128}$/;
|
|
4
|
+
const PROCESS_ID = randomUUID();
|
|
5
|
+
/** Record a device transport checkpoint without affecting command handling. */
|
|
6
|
+
export function observeTransport(input) {
|
|
7
|
+
try {
|
|
8
|
+
const timestamp_utc = new Date().toISOString();
|
|
9
|
+
const monotonic_ms = performance.now();
|
|
10
|
+
const receipt = 'payload' in input;
|
|
11
|
+
const callId = receipt ? input.payload?.call_id : input.callId;
|
|
12
|
+
const deviceId = input.deviceId;
|
|
13
|
+
if (typeof callId !== 'string' || !ID.test(callId) ||
|
|
14
|
+
typeof deviceId !== 'string' || !ID.test(deviceId))
|
|
15
|
+
return null;
|
|
16
|
+
if (receipt && (input.payload.device_id !== deviceId || !input.userId ||
|
|
17
|
+
(input.payload.user_id !== undefined && input.payload.user_id !== input.userId)))
|
|
18
|
+
return null;
|
|
19
|
+
const event = {
|
|
20
|
+
...(receipt ? {} : input.fields),
|
|
21
|
+
call_id: callId,
|
|
22
|
+
device_id: deviceId,
|
|
23
|
+
transport: 'broadcast',
|
|
24
|
+
stage: input.stage,
|
|
25
|
+
notification_id: `${callId}:broadcast:1`,
|
|
26
|
+
attempt_number: 1,
|
|
27
|
+
...(input.stage === 'operation_end' ? { operation_id: randomUUID(), operation_attempt: 1 } : {}),
|
|
28
|
+
observation_id: randomUUID(),
|
|
29
|
+
source_process_id: PROCESS_ID,
|
|
30
|
+
schema_version: 1,
|
|
31
|
+
timestamp_utc,
|
|
32
|
+
monotonic_ms,
|
|
33
|
+
};
|
|
34
|
+
void capture(input.stage, event);
|
|
35
|
+
return event;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// Telemetry must never affect command handling.
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|