@wonderwhy-er/desktop-commander 0.2.52 → 0.3.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 +55 -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 +17 -3
- package/dist/remote-device/desktop-commander-integration.d.ts +1 -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/diagnostics/device-log.d.ts +63 -0
- package/dist/remote-device/diagnostics/device-log.js +219 -0
- package/dist/remote-device/diagnostics/redact.d.ts +1 -0
- package/dist/remote-device/diagnostics/redact.js +76 -0
- package/dist/remote-device/diagnostics/report.d.ts +118 -0
- package/dist/remote-device/diagnostics/report.js +748 -0
- package/dist/remote-device/diagnostics/upload.d.ts +11 -0
- package/dist/remote-device/diagnostics/upload.js +59 -0
- package/dist/remote-device/realtime-health-telemetry.d.ts +35 -0
- package/dist/remote-device/realtime-health-telemetry.js +110 -0
- package/dist/remote-device/remote-channel.d.ts +36 -4
- package/dist/remote-device/remote-channel.js +230 -50
- package/dist/remote-device/scripts/blocking-offline-update.js +108 -69
- 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 +124 -222
- 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 -11
- package/dist/tools/schemas.js +15 -14
- package/dist/types.d.ts +1 -1
- package/dist/ui/config-editor/config-editor-runtime.js +53 -53
- package/dist/ui/config-editor/src/app.js +2 -15
- package/dist/ui/file-preview/preview-runtime.js +190 -182
- 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 +25 -0
- package/dist/utils/lazy-package.js +44 -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.d.ts +1 -0
- package/dist/utils/system-info.js +40 -25
- package/dist/utils/trackTools.js +2 -1
- package/dist/utils/usageTracker.d.ts +0 -39
- package/dist/utils/usageTracker.js +1 -161
- 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 +32 -16
- 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/docx.js
CHANGED
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
* Round-trip: DOCX → unzip → pretty-print → [outline or raw] → edit → compact → repack
|
|
17
17
|
*/
|
|
18
18
|
import fs from 'fs/promises';
|
|
19
|
-
import
|
|
19
|
+
import { createRequire } from 'module';
|
|
20
|
+
import { LazyPackage } from '../lazy-package.js';
|
|
20
21
|
// ════════════════════════════════════════════════════════════════
|
|
21
22
|
// XML Pretty-Print / Compact
|
|
22
23
|
// ════════════════════════════════════════════════════════════════
|
|
@@ -51,8 +52,15 @@ function prettyPrintXml(xml) {
|
|
|
51
52
|
function compactXml(prettyXml) {
|
|
52
53
|
return prettyXml.split('\n').map(l => l.trimStart()).join('');
|
|
53
54
|
}
|
|
55
|
+
const require = createRequire(import.meta.url);
|
|
56
|
+
export const pizzipPackage = new LazyPackage('pizzip', 'DOCX support', () => require('pizzip'));
|
|
57
|
+
/** Opens a zip from its bytes, or a new, empty one */
|
|
58
|
+
function openZip(data) {
|
|
59
|
+
const PizZipClass = pizzipPackage.load();
|
|
60
|
+
return data === undefined ? new PizZipClass() : new PizZipClass(data);
|
|
61
|
+
}
|
|
54
62
|
function loadDocxZip(buf) {
|
|
55
|
-
const zip =
|
|
63
|
+
const zip = openZip(buf);
|
|
56
64
|
const docFile = zip.file('word/document.xml');
|
|
57
65
|
if (!docFile)
|
|
58
66
|
throw new Error('Invalid DOCX: missing word/document.xml');
|
|
@@ -367,6 +375,11 @@ function splitTopLevelElements(xml) {
|
|
|
367
375
|
}
|
|
368
376
|
return elements.filter(e => e.length > 0);
|
|
369
377
|
}
|
|
378
|
+
/** A header or footer part (word/header1.xml, word/footer4.xml, …), of any section */
|
|
379
|
+
function isHeaderFooterPart(relativePath) {
|
|
380
|
+
return (relativePath.startsWith('word/header') || relativePath.startsWith('word/footer'))
|
|
381
|
+
&& relativePath.endsWith('.xml');
|
|
382
|
+
}
|
|
370
383
|
/**
|
|
371
384
|
* Extract outline info for headers and footers from the DOCX zip.
|
|
372
385
|
*/
|
|
@@ -374,8 +387,7 @@ function extractHeaderFooterOutline(zip) {
|
|
|
374
387
|
const parts = [];
|
|
375
388
|
const zipFiles = zip.files;
|
|
376
389
|
for (const relativePath of Object.keys(zipFiles)) {
|
|
377
|
-
if ((relativePath
|
|
378
|
-
&& relativePath.endsWith('.xml')) {
|
|
390
|
+
if (isHeaderFooterPart(relativePath)) {
|
|
379
391
|
try {
|
|
380
392
|
const xml = zipFiles[relativePath].asText();
|
|
381
393
|
const text = extractAllText(xml);
|
|
@@ -400,7 +412,7 @@ function escapeXml(text) {
|
|
|
400
412
|
.replace(/"/g, '"').replace(/'/g, ''');
|
|
401
413
|
}
|
|
402
414
|
function createMinimalDocxZip(documentXml) {
|
|
403
|
-
const zip =
|
|
415
|
+
const zip = openZip();
|
|
404
416
|
zip.file('[Content_Types].xml', `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +
|
|
405
417
|
`<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">` +
|
|
406
418
|
`<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>` +
|
|
@@ -568,7 +580,7 @@ export class DocxFileHandler {
|
|
|
568
580
|
}
|
|
569
581
|
// Load and pretty-print
|
|
570
582
|
const buf = await fs.readFile(path);
|
|
571
|
-
const zip =
|
|
583
|
+
const zip = openZip(buf);
|
|
572
584
|
const docFile = zip.file('word/document.xml');
|
|
573
585
|
if (!docFile)
|
|
574
586
|
throw new Error('Invalid DOCX: missing word/document.xml');
|
|
@@ -578,10 +590,13 @@ export class DocxFileHandler {
|
|
|
578
590
|
let targetPretty = pretty;
|
|
579
591
|
let targetFile = 'word/document.xml';
|
|
580
592
|
let matchCount = countOccurrences(pretty, oldStr);
|
|
581
|
-
// If not found in document.xml, search through headers/footers
|
|
593
|
+
// If not found in document.xml, search through headers/footers: every section's
|
|
594
|
+
// (Word numbers them across sections), headers first, each in number order
|
|
582
595
|
if (matchCount === 0) {
|
|
583
|
-
const
|
|
584
|
-
|
|
596
|
+
const partNumber = (name) => Number(/(\d+)\.xml$/.exec(name)?.[1] ?? 0);
|
|
597
|
+
const isFooter = (name) => (name.startsWith('word/footer') ? 1 : 0);
|
|
598
|
+
const xmlFiles = Object.keys(zip.files).filter(isHeaderFooterPart)
|
|
599
|
+
.sort((a, b) => isFooter(a) - isFooter(b) || partNumber(a) - partNumber(b));
|
|
585
600
|
for (const xmlPath of xmlFiles) {
|
|
586
601
|
const f = zip.file(xmlPath);
|
|
587
602
|
if (!f)
|
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
* Excel file handler using ExcelJS
|
|
3
3
|
* Handles reading, writing, and editing Excel files (.xlsx, .xls, .xlsm)
|
|
4
4
|
*/
|
|
5
|
+
import type ExcelJS from 'exceljs';
|
|
6
|
+
import { LazyPackage } from '../lazy-package.js';
|
|
5
7
|
import { FileHandler, ReadOptions, FileResult, EditResult, FileInfo } from './base.js';
|
|
8
|
+
export declare const exceljsPackage: LazyPackage<typeof ExcelJS>;
|
|
6
9
|
/**
|
|
7
10
|
* Excel file handler implementation using ExcelJS
|
|
8
11
|
* Supports: .xlsx, .xls, .xlsm files
|
|
@@ -15,6 +18,13 @@ export declare class ExcelFileHandler implements FileHandler {
|
|
|
15
18
|
getInfo(path: string): Promise<FileInfo>;
|
|
16
19
|
private checkFileSize;
|
|
17
20
|
private extractMetadata;
|
|
21
|
+
/**
|
|
22
|
+
* The last row and the last column holding a value (0 for an empty sheet):
|
|
23
|
+
* where the sheet's data ends, for reads, sheet info and appends alike.
|
|
24
|
+
* Not actualRowCount/actualColumnCount: those COUNT the used rows and
|
|
25
|
+
* columns, so data in columns A and AH only would end at column B.
|
|
26
|
+
*/
|
|
27
|
+
private dataExtent;
|
|
18
28
|
private worksheetToArray;
|
|
19
29
|
private writeDataToSheet;
|
|
20
30
|
private writeRowsStartingAt;
|
|
@@ -2,8 +2,15 @@
|
|
|
2
2
|
* Excel file handler using ExcelJS
|
|
3
3
|
* Handles reading, writing, and editing Excel files (.xlsx, .xls, .xlsm)
|
|
4
4
|
*/
|
|
5
|
-
import ExcelJS from 'exceljs';
|
|
6
5
|
import fs from 'fs/promises';
|
|
6
|
+
import { createRequire } from 'module';
|
|
7
|
+
import { LazyPackage } from '../lazy-package.js';
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
9
|
+
// require(), not import(): Node keeps a failed import() failed, so it couldn't load again
|
|
10
|
+
export const exceljsPackage = new LazyPackage('exceljs', 'Excel support', () => require('exceljs'));
|
|
11
|
+
async function newWorkbook() {
|
|
12
|
+
return new (exceljsPackage.load().Workbook)();
|
|
13
|
+
}
|
|
7
14
|
// File size limit: 10MB
|
|
8
15
|
const FILE_SIZE_LIMIT = 10 * 1024 * 1024;
|
|
9
16
|
/**
|
|
@@ -17,14 +24,17 @@ export class ExcelFileHandler {
|
|
|
17
24
|
}
|
|
18
25
|
async read(path, options) {
|
|
19
26
|
await this.checkFileSize(path);
|
|
20
|
-
const workbook =
|
|
27
|
+
const workbook = await newWorkbook();
|
|
21
28
|
await workbook.xlsx.readFile(path);
|
|
22
29
|
const metadata = await this.extractMetadata(workbook, path);
|
|
23
|
-
const { sheetName, data, totalRows, returnedRows } = this.worksheetToArray(workbook, options?.sheet, options?.range, options?.offset, options?.length);
|
|
24
|
-
// Format output with sheet info header, usage hint, and JSON data
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
const { sheetName, data, totalRows, returnedRows, firstRow } = this.worksheetToArray(workbook, options?.sheet, options?.range, options?.offset, options?.length);
|
|
31
|
+
// Format output with sheet info header, usage hint, and JSON data.
|
|
32
|
+
// No rows while there are some means the offset starts past the end.
|
|
33
|
+
const paginationInfo = totalRows <= returnedRows
|
|
34
|
+
? ''
|
|
35
|
+
: returnedRows > 0
|
|
36
|
+
? `\n[Showing rows ${firstRow}-${firstRow + returnedRows - 1} of ${totalRows} total. Use offset/length to paginate.]`
|
|
37
|
+
: `\n[No rows returned: row ${firstRow} is past the end (${totalRows} rows total). Use offset/length to paginate.]`;
|
|
28
38
|
const sheetHasSpace = /\s/.test(sheetName);
|
|
29
39
|
const exampleSheet = sheetHasSpace ? sheetName : 'Sheet1';
|
|
30
40
|
const content = `[Sheet: '${sheetName}' from ${path}]${paginationInfo}
|
|
@@ -67,15 +77,15 @@ ${JSON.stringify(data)}`;
|
|
|
67
77
|
// Handle append mode by finding last row and writing after it
|
|
68
78
|
if (mode === 'append') {
|
|
69
79
|
try {
|
|
70
|
-
const workbook =
|
|
80
|
+
const workbook = await newWorkbook();
|
|
71
81
|
await workbook.xlsx.readFile(path);
|
|
72
82
|
if (Array.isArray(parsedContent)) {
|
|
73
|
-
// Append to Sheet1
|
|
74
|
-
let worksheet = workbook.getWorksheet('Sheet1');
|
|
83
|
+
// Append to Sheet1, or to the first sheet of a workbook without one
|
|
84
|
+
let worksheet = workbook.getWorksheet('Sheet1') ?? workbook.worksheets[0];
|
|
75
85
|
if (!worksheet) {
|
|
76
86
|
worksheet = workbook.addWorksheet('Sheet1');
|
|
77
87
|
}
|
|
78
|
-
const startRow = (worksheet.
|
|
88
|
+
const startRow = this.dataExtent(worksheet).lastRow + 1;
|
|
79
89
|
this.writeRowsStartingAt(worksheet, startRow, parsedContent);
|
|
80
90
|
}
|
|
81
91
|
else if (typeof parsedContent === 'object' && parsedContent !== null) {
|
|
@@ -86,7 +96,7 @@ ${JSON.stringify(data)}`;
|
|
|
86
96
|
if (!worksheet) {
|
|
87
97
|
worksheet = workbook.addWorksheet(sheetName);
|
|
88
98
|
}
|
|
89
|
-
const startRow = (worksheet.
|
|
99
|
+
const startRow = this.dataExtent(worksheet).lastRow + 1;
|
|
90
100
|
this.writeRowsStartingAt(worksheet, startRow, data);
|
|
91
101
|
}
|
|
92
102
|
}
|
|
@@ -103,7 +113,7 @@ ${JSON.stringify(data)}`;
|
|
|
103
113
|
}
|
|
104
114
|
}
|
|
105
115
|
// Rewrite mode (or append to non-existent file): create new workbook
|
|
106
|
-
const workbook =
|
|
116
|
+
const workbook = await newWorkbook();
|
|
107
117
|
if (Array.isArray(parsedContent)) {
|
|
108
118
|
// Single sheet from 2D array
|
|
109
119
|
this.writeDataToSheet(workbook, 'Sheet1', parsedContent);
|
|
@@ -139,7 +149,7 @@ ${JSON.stringify(data)}`;
|
|
|
139
149
|
}
|
|
140
150
|
// Parse range: "Sheet1!A1:C10" or "Sheet1"
|
|
141
151
|
const [sheetName, cellRange] = this.parseRange(range);
|
|
142
|
-
const workbook =
|
|
152
|
+
const workbook = await newWorkbook();
|
|
143
153
|
await workbook.xlsx.readFile(path);
|
|
144
154
|
// Get or create sheet
|
|
145
155
|
let worksheet = workbook.getWorksheet(sheetName);
|
|
@@ -148,7 +158,19 @@ ${JSON.stringify(data)}`;
|
|
|
148
158
|
}
|
|
149
159
|
if (cellRange) {
|
|
150
160
|
// Write to specific range
|
|
151
|
-
const { startRow, startCol } = this.parseCellRange(cellRange);
|
|
161
|
+
const { startRow, startCol, endRow, endCol } = this.parseCellRange(cellRange);
|
|
162
|
+
// A FROM:TO range bounds the write: content that doesn't fit is refused
|
|
163
|
+
// before any cell changes, so no cell outside the range is overwritten
|
|
164
|
+
if (endRow !== undefined && endCol !== undefined) {
|
|
165
|
+
const rows = content.length;
|
|
166
|
+
const columns = Math.max(0, ...content.map((row) => Array.isArray(row) ? row.length : 0));
|
|
167
|
+
const rangeRows = endRow - startRow + 1;
|
|
168
|
+
const rangeColumns = endCol - startCol + 1;
|
|
169
|
+
if (rows > rangeRows || columns > rangeColumns) {
|
|
170
|
+
throw new Error(`Content has ${rows} row(s) and ${columns} column(s) but range ${cellRange} holds ` +
|
|
171
|
+
`${rangeRows} row(s) and ${rangeColumns} column(s); nothing was written`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
152
174
|
for (let r = 0; r < content.length; r++) {
|
|
153
175
|
const rowData = content[r];
|
|
154
176
|
if (!Array.isArray(rowData))
|
|
@@ -197,7 +219,7 @@ ${JSON.stringify(data)}`;
|
|
|
197
219
|
async getInfo(path) {
|
|
198
220
|
const stats = await fs.stat(path);
|
|
199
221
|
try {
|
|
200
|
-
const workbook =
|
|
222
|
+
const workbook = await newWorkbook();
|
|
201
223
|
await workbook.xlsx.readFile(path);
|
|
202
224
|
const metadata = await this.extractMetadata(workbook, path);
|
|
203
225
|
return {
|
|
@@ -247,20 +269,42 @@ ${JSON.stringify(data)}`;
|
|
|
247
269
|
}
|
|
248
270
|
async extractMetadata(workbook, path) {
|
|
249
271
|
const stats = await fs.stat(path);
|
|
250
|
-
const sheets = workbook.worksheets.map(ws =>
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}));
|
|
272
|
+
const sheets = workbook.worksheets.map(ws => {
|
|
273
|
+
const { lastRow, lastCol } = this.dataExtent(ws);
|
|
274
|
+
return { name: ws.name, rowCount: lastRow, colCount: lastCol };
|
|
275
|
+
});
|
|
255
276
|
return {
|
|
256
277
|
sheets,
|
|
257
278
|
fileSize: stats.size,
|
|
258
279
|
isLargeFile: stats.size > FILE_SIZE_LIMIT
|
|
259
280
|
};
|
|
260
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* The last row and the last column holding a value (0 for an empty sheet):
|
|
284
|
+
* where the sheet's data ends, for reads, sheet info and appends alike.
|
|
285
|
+
* Not actualRowCount/actualColumnCount: those COUNT the used rows and
|
|
286
|
+
* columns, so data in columns A and AH only would end at column B.
|
|
287
|
+
*/
|
|
288
|
+
dataExtent(worksheet) {
|
|
289
|
+
let lastRow = 0;
|
|
290
|
+
let lastCol = 0;
|
|
291
|
+
worksheet.eachRow((row, rowNumber) => {
|
|
292
|
+
lastRow = rowNumber;
|
|
293
|
+
row.eachCell((_cell, colNumber) => {
|
|
294
|
+
lastCol = Math.max(lastCol, colNumber);
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
return { lastRow, lastCol };
|
|
298
|
+
}
|
|
261
299
|
worksheetToArray(workbook, sheetRef, range, offset, length) {
|
|
262
300
|
if (workbook.worksheets.length === 0) {
|
|
263
|
-
return { sheetName: '', data: [], totalRows: 0, returnedRows: 0 };
|
|
301
|
+
return { sheetName: '', data: [], totalRows: 0, returnedRows: 0, firstRow: 1 };
|
|
302
|
+
}
|
|
303
|
+
// `sheet` is a name or a 0-based index as a string ("0"). A number that is
|
|
304
|
+
// no sheet's index but a sheet's name ("2024") means that sheet.
|
|
305
|
+
if (typeof sheetRef === 'string' && /^\d+$/.test(sheetRef)
|
|
306
|
+
&& (Number(sheetRef) < workbook.worksheets.length || !workbook.getWorksheet(sheetRef))) {
|
|
307
|
+
sheetRef = Number(sheetRef);
|
|
264
308
|
}
|
|
265
309
|
// Accept range with embedded sheet prefix (parity with edit_block).
|
|
266
310
|
// E.g. range:"Sheet1!A1:B2" or "'My Sheet'!A1" — strip the sheet
|
|
@@ -295,11 +339,12 @@ ${JSON.stringify(data)}`;
|
|
|
295
339
|
}
|
|
296
340
|
sheetName = sheetRef;
|
|
297
341
|
}
|
|
298
|
-
// Determine range to read
|
|
342
|
+
// Determine range to read: from A1 to the last cell of the data
|
|
343
|
+
const { lastRow, lastCol } = this.dataExtent(worksheet);
|
|
299
344
|
let startRow = 1;
|
|
300
|
-
let endRow =
|
|
345
|
+
let endRow = lastRow || 1;
|
|
301
346
|
let startCol = 1;
|
|
302
|
-
let endCol =
|
|
347
|
+
let endCol = lastCol || 1;
|
|
303
348
|
if (cellRangeOnly) {
|
|
304
349
|
const parsed = this.parseCellRange(cellRangeOnly);
|
|
305
350
|
startRow = parsed.startRow;
|
|
@@ -309,8 +354,11 @@ ${JSON.stringify(data)}`;
|
|
|
309
354
|
if (parsed.endCol)
|
|
310
355
|
endCol = parsed.endCol;
|
|
311
356
|
}
|
|
312
|
-
// Calculate total rows before pagination
|
|
313
|
-
|
|
357
|
+
// Calculate total rows before pagination. A single cell reads to the end
|
|
358
|
+
// of the data, so one below the data (A10 on a 5-row sheet) has no rows,
|
|
359
|
+
// and one right of it (Z1 on a sheet ending at column C) has no cells.
|
|
360
|
+
const totalRows = startCol > endCol ? 0 : Math.max(0, endRow - startRow + 1);
|
|
361
|
+
const rangeStartRow = startRow;
|
|
314
362
|
// Apply offset/length pagination (row-based, matching text file behavior)
|
|
315
363
|
if (offset !== undefined) {
|
|
316
364
|
if (offset < 0) {
|
|
@@ -328,9 +376,12 @@ ${JSON.stringify(data)}`;
|
|
|
328
376
|
if (length !== undefined && length > 0 && (offset === undefined || offset >= 0)) {
|
|
329
377
|
endRow = Math.min(endRow, startRow + length - 1);
|
|
330
378
|
}
|
|
379
|
+
// 1-based position of the first returned row within the totalRows,
|
|
380
|
+
// taken from the pagination actually applied (not the raw offset)
|
|
381
|
+
const firstRow = startRow - rangeStartRow + 1;
|
|
331
382
|
// Ensure valid range
|
|
332
|
-
if (startRow > endRow) {
|
|
333
|
-
return { sheetName, data: [], totalRows, returnedRows: 0 };
|
|
383
|
+
if (startRow > endRow || startCol > endCol) {
|
|
384
|
+
return { sheetName, data: [], totalRows, returnedRows: 0, firstRow };
|
|
334
385
|
}
|
|
335
386
|
// Build 2D array (preserving types)
|
|
336
387
|
const data = [];
|
|
@@ -368,7 +419,7 @@ ${JSON.stringify(data)}`;
|
|
|
368
419
|
}
|
|
369
420
|
data.push(rowData);
|
|
370
421
|
}
|
|
371
|
-
return { sheetName, data, totalRows, returnedRows: data.length };
|
|
422
|
+
return { sheetName, data, totalRows, returnedRows: data.length, firstRow };
|
|
372
423
|
}
|
|
373
424
|
writeDataToSheet(workbook, sheetName, data) {
|
|
374
425
|
// Remove existing sheet if it exists
|
|
@@ -421,16 +472,25 @@ ${JSON.stringify(data)}`;
|
|
|
421
472
|
const startCol = this.columnToNumber(match[1]);
|
|
422
473
|
const startRow = parseInt(match[2], 10);
|
|
423
474
|
if (match[3] && match[4]) {
|
|
475
|
+
// Any two opposite corners name the range, as in Excel: A5:C2,
|
|
476
|
+
// C2:A5 and C5:A2 are all A2:C5
|
|
424
477
|
const endCol = this.columnToNumber(match[3]);
|
|
425
478
|
const endRow = parseInt(match[4], 10);
|
|
426
|
-
return {
|
|
479
|
+
return {
|
|
480
|
+
startRow: Math.min(startRow, endRow),
|
|
481
|
+
startCol: Math.min(startCol, endCol),
|
|
482
|
+
endRow: Math.max(startRow, endRow),
|
|
483
|
+
endCol: Math.max(startCol, endCol)
|
|
484
|
+
};
|
|
427
485
|
}
|
|
428
486
|
return { startRow, startCol };
|
|
429
487
|
}
|
|
430
488
|
columnToNumber(col) {
|
|
489
|
+
// Column letters are case-insensitive, as in Excel: "b" is column B (2)
|
|
490
|
+
const letters = col.toUpperCase();
|
|
431
491
|
let result = 0;
|
|
432
|
-
for (let i = 0; i <
|
|
433
|
-
result = result * 26 +
|
|
492
|
+
for (let i = 0; i < letters.length; i++) {
|
|
493
|
+
result = result * 26 + letters.charCodeAt(i) - 64;
|
|
434
494
|
}
|
|
435
495
|
return result;
|
|
436
496
|
}
|
|
@@ -22,9 +22,12 @@ import { FileHandler } from './base.js';
|
|
|
22
22
|
* 6. Text files (default)
|
|
23
23
|
*
|
|
24
24
|
* @param filePath File path to get handler for
|
|
25
|
+
* @param options svgAsImage: an SVG goes to the image handler, for the file preview widget
|
|
25
26
|
* @returns FileHandler instance that can handle this file
|
|
26
27
|
*/
|
|
27
|
-
export declare function getFileHandler(filePath: string
|
|
28
|
+
export declare function getFileHandler(filePath: string, options?: {
|
|
29
|
+
svgAsImage?: boolean;
|
|
30
|
+
}): Promise<FileHandler>;
|
|
28
31
|
/**
|
|
29
32
|
* Check if a file path is an Excel file
|
|
30
33
|
* Delegates to ExcelFileHandler.canHandle to avoid duplicating extension logic
|
|
@@ -39,3 +42,21 @@ export declare function isExcelFile(path: string): boolean;
|
|
|
39
42
|
* @returns true if file is an image format
|
|
40
43
|
*/
|
|
41
44
|
export declare function isImageFile(path: string): boolean;
|
|
45
|
+
export type FileAction = 'read' | 'write' | 'edit';
|
|
46
|
+
/**
|
|
47
|
+
* Right after initialize (#777): the packages Excel, DOCX and PDF files need,
|
|
48
|
+
* loaded in the background one at a time, smaller first, so each pause of the
|
|
49
|
+
* main thread is one package.
|
|
50
|
+
*/
|
|
51
|
+
export declare function preloadFileSupport(): void;
|
|
52
|
+
/**
|
|
53
|
+
* The answer for a tool call that would `action` `filePath` while a package it
|
|
54
|
+
* needs is still loading, naming the file; undefined once loaded. A package
|
|
55
|
+
* not preloading yet (before initialize, or after a failure) starts here,
|
|
56
|
+
* after the call has answered. `isPdf` for a PDF whatever its name (write_pdf),
|
|
57
|
+
* `isUrl` for a URL read.
|
|
58
|
+
*/
|
|
59
|
+
export declare function stillLoadingError(filePath: string, action: FileAction, { isPdf, isUrl }?: {
|
|
60
|
+
isPdf?: boolean;
|
|
61
|
+
isUrl?: boolean;
|
|
62
|
+
}): string | undefined;
|
|
@@ -5,12 +5,17 @@
|
|
|
5
5
|
* Each handler implements canHandle() which can be sync (extension-based)
|
|
6
6
|
* or async (content-based like BinaryFileHandler using isBinaryFile)
|
|
7
7
|
*/
|
|
8
|
+
import path from 'path';
|
|
8
9
|
import { TextFileHandler } from './text.js';
|
|
9
10
|
import { ImageFileHandler } from './image.js';
|
|
10
11
|
import { BinaryFileHandler } from './binary.js';
|
|
11
|
-
import { ExcelFileHandler } from './excel.js';
|
|
12
|
+
import { ExcelFileHandler, exceljsPackage } from './excel.js';
|
|
12
13
|
import { PdfFileHandler } from './pdf.js';
|
|
13
|
-
import { DocxFileHandler } from './docx.js';
|
|
14
|
+
import { DocxFileHandler, pizzipPackage } from './docx.js';
|
|
15
|
+
import { pdfLibPackage } from '../../tools/pdf/manipulations.js';
|
|
16
|
+
import { pdf2mdPackage } from '../../tools/pdf/lib/pdf2md.js';
|
|
17
|
+
import { unpdfPackage } from '../../tools/pdf/extract-images.js';
|
|
18
|
+
import { mdToPdfPackage } from '../../tools/pdf/markdown.js';
|
|
14
19
|
// Singleton instances of each handler
|
|
15
20
|
let excelHandler = null;
|
|
16
21
|
let imageHandler = null;
|
|
@@ -67,9 +72,10 @@ function getDocxHandler() {
|
|
|
67
72
|
* 6. Text files (default)
|
|
68
73
|
*
|
|
69
74
|
* @param filePath File path to get handler for
|
|
75
|
+
* @param options svgAsImage: an SVG goes to the image handler, for the file preview widget
|
|
70
76
|
* @returns FileHandler instance that can handle this file
|
|
71
77
|
*/
|
|
72
|
-
export async function getFileHandler(filePath) {
|
|
78
|
+
export async function getFileHandler(filePath, options) {
|
|
73
79
|
// Check DOCX first (extension-based, sync)
|
|
74
80
|
if (getDocxHandler().canHandle(filePath)) {
|
|
75
81
|
return getDocxHandler();
|
|
@@ -83,7 +89,7 @@ export async function getFileHandler(filePath) {
|
|
|
83
89
|
return getExcelHandler();
|
|
84
90
|
}
|
|
85
91
|
// Check Image (extension-based, sync - images are binary but handled specially)
|
|
86
|
-
if (getImageHandler().canHandle(filePath)) {
|
|
92
|
+
if (getImageHandler().canHandle(filePath, options)) {
|
|
87
93
|
return getImageHandler();
|
|
88
94
|
}
|
|
89
95
|
// Check Binary (content-based, async via isBinaryFile)
|
|
@@ -111,3 +117,52 @@ export function isExcelFile(path) {
|
|
|
111
117
|
export function isImageFile(path) {
|
|
112
118
|
return getImageHandler().canHandle(path);
|
|
113
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Right after initialize (#777): the packages Excel, DOCX and PDF files need,
|
|
122
|
+
* loaded in the background one at a time, smaller first, so each pause of the
|
|
123
|
+
* main thread is one package.
|
|
124
|
+
*/
|
|
125
|
+
export function preloadFileSupport() {
|
|
126
|
+
void (async () => {
|
|
127
|
+
for (const pkg of [pizzipPackage, pdfLibPackage, pdf2mdPackage, unpdfPackage, exceljsPackage, mdToPdfPackage]) {
|
|
128
|
+
await pkg.preload();
|
|
129
|
+
}
|
|
130
|
+
})();
|
|
131
|
+
}
|
|
132
|
+
/** The packages a file needs for `action`, by the handlers' canHandle() (its name, no disk access) */
|
|
133
|
+
function packagesFor(filePath, action, isPdf, isUrl) {
|
|
134
|
+
if (isPdf) {
|
|
135
|
+
// Editing a PDF can insert markdown pages, which are rendered
|
|
136
|
+
return action === 'read' ? [pdf2mdPackage, unpdfPackage] : action === 'write' ? [mdToPdfPackage] : [pdfLibPackage, mdToPdfPackage];
|
|
137
|
+
}
|
|
138
|
+
// A URL is fetched, not opened by a file handler: only a PDF one is parsed
|
|
139
|
+
if (isUrl)
|
|
140
|
+
return [];
|
|
141
|
+
if (getDocxHandler().canHandle(filePath))
|
|
142
|
+
return [pizzipPackage];
|
|
143
|
+
if (getExcelHandler().canHandle(filePath))
|
|
144
|
+
return [exceljsPackage];
|
|
145
|
+
return [];
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The answer for a tool call that would `action` `filePath` while a package it
|
|
149
|
+
* needs is still loading, naming the file; undefined once loaded. A package
|
|
150
|
+
* not preloading yet (before initialize, or after a failure) starts here,
|
|
151
|
+
* after the call has answered. `isPdf` for a PDF whatever its name (write_pdf),
|
|
152
|
+
* `isUrl` for a URL read.
|
|
153
|
+
*/
|
|
154
|
+
export function stillLoadingError(filePath, action, { isPdf = getPdfHandler().canHandle(filePath), isUrl = false } = {}) {
|
|
155
|
+
const pending = packagesFor(filePath, action, isPdf, isUrl).find((pkg) => !pkg.loaded);
|
|
156
|
+
if (!pending)
|
|
157
|
+
return undefined;
|
|
158
|
+
const file = path.basename(filePath);
|
|
159
|
+
const { error, needsRestart, support } = pending;
|
|
160
|
+
if (!needsRestart)
|
|
161
|
+
void pending.preload();
|
|
162
|
+
if (!error) {
|
|
163
|
+
return `Can't ${action} ${file} yet: Desktop Commander is still loading its ${support} (it starts right after launch). Try again in a few seconds.`;
|
|
164
|
+
}
|
|
165
|
+
return needsRestart
|
|
166
|
+
? `Can't ${action} ${file}: Desktop Commander couldn't load its ${support} (${error}). Restart Desktop Commander to load it again.`
|
|
167
|
+
: `Can't ${action} ${file}: Desktop Commander couldn't load its ${support} (${error}). It's loading it again; try again in a few seconds.`;
|
|
168
|
+
}
|
|
@@ -3,16 +3,24 @@
|
|
|
3
3
|
* Handles reading image files and converting to base64
|
|
4
4
|
*/
|
|
5
5
|
import { FileHandler, ReadOptions, FileResult, FileInfo } from './base.js';
|
|
6
|
+
/**
|
|
7
|
+
* Whether content of this MIME type is answered as an image: every image type
|
|
8
|
+
* but SVG, which is text (XML), read and written as text. Only the file preview
|
|
9
|
+
* widget draws an SVG as an image (svgAsImage). Files and URLs both decide here.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isImageAnswer(mimeType: string, svgAsImage?: boolean): boolean;
|
|
6
12
|
/**
|
|
7
13
|
* Image file handler implementation
|
|
8
|
-
* Supports: PNG, JPEG, GIF, WebP, BMP
|
|
14
|
+
* Supports: PNG, JPEG, GIF, WebP, BMP; an SVG only for the file preview widget
|
|
15
|
+
* (isImageAnswer), everyone else reads and writes it as text.
|
|
9
16
|
*/
|
|
10
17
|
export declare class ImageFileHandler implements FileHandler {
|
|
11
|
-
private static readonly IMAGE_EXTENSIONS;
|
|
12
18
|
private static readonly IMAGE_MIME_TYPES;
|
|
13
|
-
canHandle(path: string
|
|
19
|
+
canHandle(path: string, options?: {
|
|
20
|
+
svgAsImage?: boolean;
|
|
21
|
+
}): boolean;
|
|
14
22
|
read(path: string, options?: ReadOptions): Promise<FileResult>;
|
|
15
|
-
write(path: string, content: Buffer | string): Promise<void>;
|
|
23
|
+
write(path: string, content: Buffer | string, mode?: 'rewrite' | 'append'): Promise<void>;
|
|
16
24
|
getInfo(path: string): Promise<FileInfo>;
|
|
17
25
|
/**
|
|
18
26
|
* Get MIME type for image based on file extension
|
|
@@ -3,14 +3,24 @@
|
|
|
3
3
|
* Handles reading image files and converting to base64
|
|
4
4
|
*/
|
|
5
5
|
import fs from "fs/promises";
|
|
6
|
+
const SVG_MIME_TYPE = 'image/svg+xml';
|
|
7
|
+
/**
|
|
8
|
+
* Whether content of this MIME type is answered as an image: every image type
|
|
9
|
+
* but SVG, which is text (XML), read and written as text. Only the file preview
|
|
10
|
+
* widget draws an SVG as an image (svgAsImage). Files and URLs both decide here.
|
|
11
|
+
*/
|
|
12
|
+
export function isImageAnswer(mimeType, svgAsImage = false) {
|
|
13
|
+
const type = mimeType.toLowerCase().split(';')[0].trim();
|
|
14
|
+
return type.startsWith('image/') && (svgAsImage || type !== SVG_MIME_TYPE);
|
|
15
|
+
}
|
|
6
16
|
/**
|
|
7
17
|
* Image file handler implementation
|
|
8
|
-
* Supports: PNG, JPEG, GIF, WebP, BMP
|
|
18
|
+
* Supports: PNG, JPEG, GIF, WebP, BMP; an SVG only for the file preview widget
|
|
19
|
+
* (isImageAnswer), everyone else reads and writes it as text.
|
|
9
20
|
*/
|
|
10
21
|
export class ImageFileHandler {
|
|
11
|
-
canHandle(path) {
|
|
12
|
-
|
|
13
|
-
return ImageFileHandler.IMAGE_EXTENSIONS.some(ext => lowerPath.endsWith(ext));
|
|
22
|
+
canHandle(path, options) {
|
|
23
|
+
return isImageAnswer(this.getMimeType(path), options?.svgAsImage);
|
|
14
24
|
}
|
|
15
25
|
async read(path, options) {
|
|
16
26
|
// Images are always read in full, ignoring offset and length
|
|
@@ -25,7 +35,11 @@ export class ImageFileHandler {
|
|
|
25
35
|
}
|
|
26
36
|
};
|
|
27
37
|
}
|
|
28
|
-
async write(path, content) {
|
|
38
|
+
async write(path, content, mode) {
|
|
39
|
+
// An image can't take text at its end: writing the content would replace the file
|
|
40
|
+
if (mode === 'append') {
|
|
41
|
+
throw new Error('Image append not supported.');
|
|
42
|
+
}
|
|
29
43
|
// If content is base64 string, convert to buffer
|
|
30
44
|
if (typeof content === 'string') {
|
|
31
45
|
const buffer = Buffer.from(content, 'base64');
|
|
@@ -64,9 +78,6 @@ export class ImageFileHandler {
|
|
|
64
78
|
return 'application/octet-stream'; // Fallback
|
|
65
79
|
}
|
|
66
80
|
}
|
|
67
|
-
ImageFileHandler.IMAGE_EXTENSIONS = [
|
|
68
|
-
'.png', '.jpg', '.jpeg', '.gif', '.webp', '.bmp', '.svg'
|
|
69
|
-
];
|
|
70
81
|
ImageFileHandler.IMAGE_MIME_TYPES = {
|
|
71
82
|
'.png': 'image/png',
|
|
72
83
|
'.jpg': 'image/jpeg',
|
|
@@ -3,8 +3,10 @@
|
|
|
3
3
|
* Exports all file handlers, interfaces, and utilities
|
|
4
4
|
*/
|
|
5
5
|
export * from './base.js';
|
|
6
|
-
export { getFileHandler, isExcelFile, isImageFile } from './factory.js';
|
|
6
|
+
export { getFileHandler, isExcelFile, isImageFile, preloadFileSupport, stillLoadingError } from './factory.js';
|
|
7
|
+
export type { FileAction } from './factory.js';
|
|
7
8
|
export { TextFileHandler } from './text.js';
|
|
8
|
-
export { ImageFileHandler } from './image.js';
|
|
9
|
+
export { ImageFileHandler, isImageAnswer } from './image.js';
|
|
9
10
|
export { BinaryFileHandler } from './binary.js';
|
|
10
|
-
export { ExcelFileHandler } from './excel.js';
|
|
11
|
+
export { ExcelFileHandler, exceljsPackage } from './excel.js';
|
|
12
|
+
export { pizzipPackage } from './docx.js';
|
|
@@ -5,9 +5,10 @@
|
|
|
5
5
|
// Base interfaces and types
|
|
6
6
|
export * from './base.js';
|
|
7
7
|
// Factory function
|
|
8
|
-
export { getFileHandler, isExcelFile, isImageFile } from './factory.js';
|
|
8
|
+
export { getFileHandler, isExcelFile, isImageFile, preloadFileSupport, stillLoadingError } from './factory.js';
|
|
9
9
|
// File handlers
|
|
10
10
|
export { TextFileHandler } from './text.js';
|
|
11
|
-
export { ImageFileHandler } from './image.js';
|
|
11
|
+
export { ImageFileHandler, isImageAnswer } from './image.js';
|
|
12
12
|
export { BinaryFileHandler } from './binary.js';
|
|
13
|
-
export { ExcelFileHandler } from './excel.js';
|
|
13
|
+
export { ExcelFileHandler, exceljsPackage } from './excel.js';
|
|
14
|
+
export { pizzipPackage } from './docx.js';
|