mcp-google-extras 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +80 -0
  3. package/dist/auth.js +225 -0
  4. package/dist/cachedToolsList.js +38 -0
  5. package/dist/clients.js +92 -0
  6. package/dist/googleDocsApiHelpers.js +883 -0
  7. package/dist/googleSheetsApiHelpers.js +808 -0
  8. package/dist/index.js +54 -0
  9. package/dist/logger.js +58 -0
  10. package/dist/markdown-transformer/docsToMarkdown.js +259 -0
  11. package/dist/markdown-transformer/index.js +126 -0
  12. package/dist/markdown-transformer/markdownToDocs.js +834 -0
  13. package/dist/tools/docs/addTab.js +92 -0
  14. package/dist/tools/docs/appendToGoogleDoc.js +81 -0
  15. package/dist/tools/docs/comments/addComment.js +83 -0
  16. package/dist/tools/docs/comments/deleteComment.js +30 -0
  17. package/dist/tools/docs/comments/getComment.js +45 -0
  18. package/dist/tools/docs/comments/index.js +14 -0
  19. package/dist/tools/docs/comments/listComments.js +43 -0
  20. package/dist/tools/docs/comments/replyToComment.js +35 -0
  21. package/dist/tools/docs/comments/resolveComment.js +55 -0
  22. package/dist/tools/docs/deleteRange.js +72 -0
  23. package/dist/tools/docs/findAndReplace.js +54 -0
  24. package/dist/tools/docs/formatting/applyParagraphStyle.js +83 -0
  25. package/dist/tools/docs/formatting/applyTextStyle.js +49 -0
  26. package/dist/tools/docs/formatting/index.js +6 -0
  27. package/dist/tools/docs/index.js +38 -0
  28. package/dist/tools/docs/insertImage.js +122 -0
  29. package/dist/tools/docs/insertPageBreak.js +58 -0
  30. package/dist/tools/docs/insertTable.js +53 -0
  31. package/dist/tools/docs/insertTableWithData.js +135 -0
  32. package/dist/tools/docs/insertText.js +61 -0
  33. package/dist/tools/docs/listDocumentTabs.js +60 -0
  34. package/dist/tools/docs/modifyText.js +158 -0
  35. package/dist/tools/docs/readGoogleDoc.js +165 -0
  36. package/dist/tools/docs/renameTab.js +61 -0
  37. package/dist/tools/drive/copyFile.js +63 -0
  38. package/dist/tools/drive/createDocument.js +89 -0
  39. package/dist/tools/drive/createFolder.js +48 -0
  40. package/dist/tools/drive/createFromTemplate.js +82 -0
  41. package/dist/tools/drive/deleteFile.js +72 -0
  42. package/dist/tools/drive/getDocumentInfo.js +48 -0
  43. package/dist/tools/drive/getFolderInfo.js +48 -0
  44. package/dist/tools/drive/index.js +26 -0
  45. package/dist/tools/drive/listFolderContents.js +82 -0
  46. package/dist/tools/drive/listGoogleDocs.js +67 -0
  47. package/dist/tools/drive/moveFile.js +54 -0
  48. package/dist/tools/drive/renameFile.js +39 -0
  49. package/dist/tools/drive/searchGoogleDocs.js +73 -0
  50. package/dist/tools/extras/index.js +7 -0
  51. package/dist/tools/extras/readFile.js +82 -0
  52. package/dist/tools/extras/searchFileContents.js +81 -0
  53. package/dist/tools/index.js +15 -0
  54. package/dist/tools/sheets/addConditionalFormatting.js +143 -0
  55. package/dist/tools/sheets/addSpreadsheetSheet.js +34 -0
  56. package/dist/tools/sheets/appendSpreadsheetRows.js +43 -0
  57. package/dist/tools/sheets/appendTableRows.js +50 -0
  58. package/dist/tools/sheets/autoResizeColumns.js +67 -0
  59. package/dist/tools/sheets/batchWrite.js +59 -0
  60. package/dist/tools/sheets/clearSpreadsheetRange.js +31 -0
  61. package/dist/tools/sheets/copyFormatting.js +59 -0
  62. package/dist/tools/sheets/createSpreadsheet.js +71 -0
  63. package/dist/tools/sheets/createTable.js +120 -0
  64. package/dist/tools/sheets/deleteChart.js +41 -0
  65. package/dist/tools/sheets/deleteSheet.js +43 -0
  66. package/dist/tools/sheets/deleteTable.js +56 -0
  67. package/dist/tools/sheets/duplicateSheet.js +53 -0
  68. package/dist/tools/sheets/formatCells.js +106 -0
  69. package/dist/tools/sheets/freezeRowsAndColumns.js +58 -0
  70. package/dist/tools/sheets/getSpreadsheetInfo.js +44 -0
  71. package/dist/tools/sheets/getTable.js +48 -0
  72. package/dist/tools/sheets/groupRows.js +62 -0
  73. package/dist/tools/sheets/index.js +66 -0
  74. package/dist/tools/sheets/insertChart.js +225 -0
  75. package/dist/tools/sheets/listGoogleSheets.js +62 -0
  76. package/dist/tools/sheets/listTables.js +55 -0
  77. package/dist/tools/sheets/readCellFormat.js +143 -0
  78. package/dist/tools/sheets/readSpreadsheet.js +36 -0
  79. package/dist/tools/sheets/renameSheet.js +48 -0
  80. package/dist/tools/sheets/setColumnWidths.js +43 -0
  81. package/dist/tools/sheets/setDropdownValidation.js +51 -0
  82. package/dist/tools/sheets/ungroupAllRows.js +66 -0
  83. package/dist/tools/sheets/updateTableRange.js +51 -0
  84. package/dist/tools/sheets/writeSpreadsheet.js +43 -0
  85. package/dist/tools/utils/appendMarkdownToGoogleDoc.js +93 -0
  86. package/dist/tools/utils/index.js +6 -0
  87. package/dist/tools/utils/replaceDocumentWithMarkdown.js +154 -0
  88. package/dist/types.js +186 -0
  89. package/package.json +47 -0
@@ -0,0 +1,883 @@
1
+ import { UserError } from 'fastmcp';
2
+ import { hexToRgbColor, NotImplementedError } from './types.js';
3
+ import { logger } from './logger.js';
4
+ // --- Constants ---
5
+ const MAX_BATCH_UPDATE_REQUESTS = 50; // Google API limits batch size
6
+ // --- Core Helper to Execute Batch Updates ---
7
+ export async function executeBatchUpdate(docs, documentId, requests) {
8
+ if (!requests || requests.length === 0) {
9
+ // console.warn("executeBatchUpdate called with no requests.");
10
+ return {}; // Nothing to do
11
+ }
12
+ // TODO: Consider splitting large request arrays into multiple batches if needed
13
+ if (requests.length > MAX_BATCH_UPDATE_REQUESTS) {
14
+ logger.warn(`Attempting batch update with ${requests.length} requests, exceeding typical limits. May fail.`);
15
+ }
16
+ try {
17
+ const response = await docs.documents.batchUpdate({
18
+ documentId: documentId,
19
+ requestBody: { requests },
20
+ });
21
+ return response.data;
22
+ }
23
+ catch (error) {
24
+ logger.error(`Google API batchUpdate Error for doc ${documentId}:`, error.response?.data || error.message);
25
+ // Translate common API errors to UserErrors
26
+ if (error.code === 400 && error.message.includes('Invalid requests')) {
27
+ // Try to extract more specific info if available
28
+ const details = error.response?.data?.error?.details;
29
+ let detailMsg = '';
30
+ if (details && Array.isArray(details)) {
31
+ detailMsg = details.map((d) => d.description || JSON.stringify(d)).join('; ');
32
+ }
33
+ throw new UserError(`Invalid request sent to Google Docs API. Details: ${detailMsg || error.message}`);
34
+ }
35
+ if (error.code === 404)
36
+ throw new UserError(`Document not found (ID: ${documentId}). Check the ID.`);
37
+ if (error.code === 403)
38
+ throw new UserError(`Permission denied for document (ID: ${documentId}). Ensure the authenticated user has edit access.`);
39
+ // Generic internal error for others
40
+ throw new Error(`Google API Error (${error.code}): ${error.message}`);
41
+ }
42
+ }
43
+ /**
44
+ * Executes batch updates with automatic splitting for large request arrays.
45
+ * Separates insert and format operations, executing inserts first.
46
+ *
47
+ * @param docs - The Google Docs client
48
+ * @param documentId - The document ID
49
+ * @param requests - Array of requests to execute
50
+ * @param log - Optional logger for progress tracking
51
+ * @returns Metadata about the execution (request counts, API calls, timing)
52
+ */
53
+ export async function executeBatchUpdateWithSplitting(docs, documentId, requests, log) {
54
+ const overallStart = performance.now();
55
+ if (!requests || requests.length === 0) {
56
+ return {
57
+ totalRequests: 0,
58
+ phases: {
59
+ delete: { requests: 0, apiCalls: 0, elapsedMs: 0 },
60
+ insert: { requests: 0, apiCalls: 0, elapsedMs: 0 },
61
+ format: { requests: 0, apiCalls: 0, elapsedMs: 0 },
62
+ },
63
+ totalApiCalls: 0,
64
+ totalElapsedMs: 0,
65
+ };
66
+ }
67
+ const MAX_BATCH = MAX_BATCH_UPDATE_REQUESTS;
68
+ // Separate requests into three categories
69
+ // Order of execution: delete → insert → format
70
+ const deleteRequests = requests.filter((r) => 'deleteContentRange' in r);
71
+ const insertRequests = requests.filter((r) => 'insertText' in r ||
72
+ 'insertTable' in r ||
73
+ 'insertPageBreak' in r ||
74
+ 'insertInlineImage' in r ||
75
+ 'insertSectionBreak' in r);
76
+ const formatRequests = requests.filter((r) => !('deleteContentRange' in r) &&
77
+ !('insertText' in r ||
78
+ 'insertTable' in r ||
79
+ 'insertPageBreak' in r ||
80
+ 'insertInlineImage' in r ||
81
+ 'insertSectionBreak' in r));
82
+ let totalApiCalls = 0;
83
+ // Execute delete batches first (must happen before inserts)
84
+ const deleteStart = performance.now();
85
+ if (deleteRequests.length > 0) {
86
+ if (log) {
87
+ log.info(`Executing ${deleteRequests.length} delete requests FIRST (in separate API call)`);
88
+ }
89
+ for (let i = 0; i < deleteRequests.length; i += MAX_BATCH) {
90
+ const batch = deleteRequests.slice(i, i + MAX_BATCH);
91
+ if (log) {
92
+ log.info(`Delete batch content: ${JSON.stringify(batch)}`);
93
+ }
94
+ await executeBatchUpdate(docs, documentId, batch);
95
+ totalApiCalls++;
96
+ if (log) {
97
+ const batchNum = Math.floor(i / MAX_BATCH) + 1;
98
+ const totalBatches = Math.ceil(deleteRequests.length / MAX_BATCH);
99
+ log.info(`Executed delete batch ${batchNum}/${totalBatches} (${batch.length} requests)`);
100
+ }
101
+ }
102
+ if (log) {
103
+ log.info(`Delete batches complete. Document should now be empty (except section break).`);
104
+ }
105
+ }
106
+ const deleteElapsed = performance.now() - deleteStart;
107
+ // Then execute insert batches
108
+ const insertStart = performance.now();
109
+ if (insertRequests.length > 0) {
110
+ for (let i = 0; i < insertRequests.length; i += MAX_BATCH) {
111
+ const batch = insertRequests.slice(i, i + MAX_BATCH);
112
+ await executeBatchUpdate(docs, documentId, batch);
113
+ totalApiCalls++;
114
+ if (log) {
115
+ const batchNum = Math.floor(i / MAX_BATCH) + 1;
116
+ const totalBatches = Math.ceil(insertRequests.length / MAX_BATCH);
117
+ log.info(`Executed insert batch ${batchNum}/${totalBatches} (${batch.length} requests)`);
118
+ }
119
+ }
120
+ }
121
+ const insertElapsed = performance.now() - insertStart;
122
+ // Finally execute format batches
123
+ const formatStart = performance.now();
124
+ if (formatRequests.length > 0) {
125
+ for (let i = 0; i < formatRequests.length; i += MAX_BATCH) {
126
+ const batch = formatRequests.slice(i, i + MAX_BATCH);
127
+ await executeBatchUpdate(docs, documentId, batch);
128
+ totalApiCalls++;
129
+ if (log) {
130
+ const batchNum = Math.floor(i / MAX_BATCH) + 1;
131
+ const totalBatches = Math.ceil(formatRequests.length / MAX_BATCH);
132
+ log.info(`Executed format batch ${batchNum}/${totalBatches} (${batch.length} requests)`);
133
+ }
134
+ }
135
+ }
136
+ const formatElapsed = performance.now() - formatStart;
137
+ const totalElapsedMs = performance.now() - overallStart;
138
+ return {
139
+ totalRequests: requests.length,
140
+ phases: {
141
+ delete: {
142
+ requests: deleteRequests.length,
143
+ apiCalls: Math.ceil(deleteRequests.length / MAX_BATCH) || 0,
144
+ elapsedMs: Math.round(deleteElapsed),
145
+ },
146
+ insert: {
147
+ requests: insertRequests.length,
148
+ apiCalls: Math.ceil(insertRequests.length / MAX_BATCH) || 0,
149
+ elapsedMs: Math.round(insertElapsed),
150
+ },
151
+ format: {
152
+ requests: formatRequests.length,
153
+ apiCalls: Math.ceil(formatRequests.length / MAX_BATCH) || 0,
154
+ elapsedMs: Math.round(formatElapsed),
155
+ },
156
+ },
157
+ totalApiCalls,
158
+ totalElapsedMs: Math.round(totalElapsedMs),
159
+ };
160
+ }
161
+ // --- Text Finding Helper ---
162
+ // This improved version is more robust in handling various text structure scenarios
163
+ export async function findTextRange(docs, documentId, textToFind, instance = 1, tabId) {
164
+ try {
165
+ // Request more detailed information about the document structure
166
+ // When tabId is specified, we need to use includeTabsContent to access tab-specific content
167
+ const needsTabsContent = !!tabId;
168
+ const res = await docs.documents.get({
169
+ documentId,
170
+ ...(needsTabsContent && { includeTabsContent: true }),
171
+ // Request more fields to handle various container types (not just paragraphs)
172
+ fields: needsTabsContent
173
+ ? 'tabs(tabProperties(tabId),documentTab(body(content(paragraph(elements(startIndex,endIndex,textRun(content))),table,sectionBreak,tableOfContents,startIndex,endIndex))))'
174
+ : 'body(content(paragraph(elements(startIndex,endIndex,textRun(content))),table,sectionBreak,tableOfContents,startIndex,endIndex))',
175
+ });
176
+ // Get body content from the correct tab or default
177
+ let bodyContent;
178
+ if (tabId) {
179
+ const targetTab = findTabById(res.data, tabId);
180
+ if (!targetTab) {
181
+ throw new UserError(`Tab with ID "${tabId}" not found in document.`);
182
+ }
183
+ if (!targetTab.documentTab?.body?.content) {
184
+ throw new UserError(`Tab "${tabId}" does not have content (may not be a document tab).`);
185
+ }
186
+ bodyContent = targetTab.documentTab.body.content;
187
+ }
188
+ else {
189
+ bodyContent = res.data.body?.content;
190
+ }
191
+ if (!bodyContent) {
192
+ logger.warn(`No content found in document ${documentId}${tabId ? ` (tab: ${tabId})` : ''}`);
193
+ return null;
194
+ }
195
+ // More robust text collection and index tracking
196
+ let fullText = '';
197
+ const segments = [];
198
+ // Process all content elements, including structural ones
199
+ const collectTextFromContent = (content) => {
200
+ content.forEach((element) => {
201
+ // Handle paragraph elements
202
+ if (element.paragraph?.elements) {
203
+ element.paragraph.elements.forEach((pe) => {
204
+ if (pe.textRun?.content && pe.startIndex !== undefined && pe.endIndex !== undefined) {
205
+ const content = pe.textRun.content;
206
+ fullText += content;
207
+ segments.push({
208
+ text: content,
209
+ start: pe.startIndex,
210
+ end: pe.endIndex,
211
+ });
212
+ }
213
+ });
214
+ }
215
+ // Handle table elements - this is simplified and might need expansion
216
+ if (element.table && element.table.tableRows) {
217
+ element.table.tableRows.forEach((row) => {
218
+ if (row.tableCells) {
219
+ row.tableCells.forEach((cell) => {
220
+ if (cell.content) {
221
+ collectTextFromContent(cell.content);
222
+ }
223
+ });
224
+ }
225
+ });
226
+ }
227
+ // Add handling for other structural elements as needed
228
+ });
229
+ };
230
+ collectTextFromContent(bodyContent);
231
+ // Sort segments by starting position to ensure correct ordering
232
+ segments.sort((a, b) => a.start - b.start);
233
+ logger.debug(`Document ${documentId} contains ${segments.length} text segments and ${fullText.length} characters in total.`);
234
+ // Find the specified instance of the text
235
+ let startIndex = -1;
236
+ let endIndex = -1;
237
+ let foundCount = 0;
238
+ let searchStartIndex = 0;
239
+ while (foundCount < instance) {
240
+ const currentIndex = fullText.indexOf(textToFind, searchStartIndex);
241
+ if (currentIndex === -1) {
242
+ logger.debug(`Search text "${textToFind}" not found for instance ${foundCount + 1} (requested: ${instance})`);
243
+ break;
244
+ }
245
+ foundCount++;
246
+ logger.debug(`Found instance ${foundCount} of "${textToFind}" at position ${currentIndex} in full text`);
247
+ if (foundCount === instance) {
248
+ const targetStartInFullText = currentIndex;
249
+ const targetEndInFullText = currentIndex + textToFind.length;
250
+ let currentPosInFullText = 0;
251
+ logger.debug(`Target text range in full text: ${targetStartInFullText}-${targetEndInFullText}`);
252
+ for (const seg of segments) {
253
+ const segStartInFullText = currentPosInFullText;
254
+ const segTextLength = seg.text.length;
255
+ const segEndInFullText = segStartInFullText + segTextLength;
256
+ // Map from reconstructed text position to actual document indices
257
+ if (startIndex === -1 &&
258
+ targetStartInFullText >= segStartInFullText &&
259
+ targetStartInFullText < segEndInFullText) {
260
+ startIndex = seg.start + (targetStartInFullText - segStartInFullText);
261
+ logger.debug(`Mapped start to segment ${seg.start}-${seg.end}, position ${startIndex}`);
262
+ }
263
+ if (targetEndInFullText > segStartInFullText && targetEndInFullText <= segEndInFullText) {
264
+ endIndex = seg.start + (targetEndInFullText - segStartInFullText);
265
+ logger.debug(`Mapped end to segment ${seg.start}-${seg.end}, position ${endIndex}`);
266
+ break;
267
+ }
268
+ currentPosInFullText = segEndInFullText;
269
+ }
270
+ if (startIndex === -1 || endIndex === -1) {
271
+ logger.warn(`Failed to map text "${textToFind}" instance ${instance} to actual document indices`);
272
+ return { startIndex, endIndex };
273
+ }
274
+ logger.debug(`Successfully mapped "${textToFind}" to document range ${startIndex}-${endIndex}`);
275
+ return { startIndex, endIndex };
276
+ }
277
+ // Prepare for next search iteration
278
+ searchStartIndex = currentIndex + 1;
279
+ }
280
+ logger.warn(`Could not find instance ${instance} of text "${textToFind}" in document ${documentId}`);
281
+ return null; // Instance not found or mapping failed for all attempts
282
+ }
283
+ catch (error) {
284
+ logger.error(`Error finding text "${textToFind}" in doc ${documentId}: ${error.message || 'Unknown error'}`);
285
+ if (error.code === 404)
286
+ throw new UserError(`Document not found while searching text (ID: ${documentId}).`);
287
+ if (error.code === 403)
288
+ throw new UserError(`Permission denied while searching text in doc ${documentId}.`);
289
+ throw new Error(`Failed to retrieve doc for text searching: ${error.message || 'Unknown error'}`);
290
+ }
291
+ }
292
+ // --- Paragraph Boundary Helper ---
293
+ // Enhanced version to handle document structural elements more robustly
294
+ export async function getParagraphRange(docs, documentId, indexWithin, tabId) {
295
+ try {
296
+ logger.debug(`Finding paragraph containing index ${indexWithin} in document ${documentId}${tabId ? ` (tab: ${tabId})` : ''}`);
297
+ // When tabId is specified, we need to use includeTabsContent to access tab-specific content
298
+ const needsTabsContent = !!tabId;
299
+ // Request more detailed document structure to handle nested elements
300
+ const res = await docs.documents.get({
301
+ documentId,
302
+ ...(needsTabsContent && { includeTabsContent: true }),
303
+ // Request more comprehensive structure information
304
+ fields: needsTabsContent
305
+ ? 'tabs(tabProperties(tabId),documentTab(body(content(startIndex,endIndex,paragraph,table,sectionBreak,tableOfContents))))'
306
+ : 'body(content(startIndex,endIndex,paragraph,table,sectionBreak,tableOfContents))',
307
+ });
308
+ // Get body content from the correct tab or default
309
+ let bodyContent;
310
+ if (tabId) {
311
+ const targetTab = findTabById(res.data, tabId);
312
+ if (!targetTab) {
313
+ throw new UserError(`Tab with ID "${tabId}" not found in document.`);
314
+ }
315
+ if (!targetTab.documentTab?.body?.content) {
316
+ throw new UserError(`Tab "${tabId}" does not have content (may not be a document tab).`);
317
+ }
318
+ bodyContent = targetTab.documentTab.body.content;
319
+ }
320
+ else {
321
+ bodyContent = res.data.body?.content;
322
+ }
323
+ if (!bodyContent) {
324
+ logger.warn(`No content found in document ${documentId}${tabId ? ` (tab: ${tabId})` : ''}`);
325
+ return null;
326
+ }
327
+ // Find paragraph containing the index
328
+ // We'll look at all structural elements recursively
329
+ const findParagraphInContent = (content) => {
330
+ for (const element of content) {
331
+ // Check if we have element boundaries defined
332
+ if (element.startIndex !== undefined && element.endIndex !== undefined) {
333
+ // Check if index is within this element's range first
334
+ if (indexWithin >= element.startIndex && indexWithin < element.endIndex) {
335
+ // If it's a paragraph, we've found our target
336
+ if (element.paragraph) {
337
+ logger.debug(`Found paragraph containing index ${indexWithin}, range: ${element.startIndex}-${element.endIndex}`);
338
+ return {
339
+ startIndex: element.startIndex,
340
+ endIndex: element.endIndex,
341
+ };
342
+ }
343
+ // If it's a table, we need to check cells recursively
344
+ if (element.table && element.table.tableRows) {
345
+ logger.debug(`Index ${indexWithin} is within a table, searching cells...`);
346
+ for (const row of element.table.tableRows) {
347
+ if (row.tableCells) {
348
+ for (const cell of row.tableCells) {
349
+ if (cell.content) {
350
+ const result = findParagraphInContent(cell.content);
351
+ if (result)
352
+ return result;
353
+ }
354
+ }
355
+ }
356
+ }
357
+ }
358
+ // For other structural elements, we didn't find a paragraph
359
+ // but we know the index is within this element
360
+ logger.warn(`Index ${indexWithin} is within element (${element.startIndex}-${element.endIndex}) but not in a paragraph`);
361
+ }
362
+ }
363
+ }
364
+ return null;
365
+ };
366
+ const paragraphRange = findParagraphInContent(bodyContent);
367
+ if (!paragraphRange) {
368
+ logger.warn(`Could not find paragraph containing index ${indexWithin}`);
369
+ }
370
+ else {
371
+ logger.debug(`Returning paragraph range: ${paragraphRange.startIndex}-${paragraphRange.endIndex}`);
372
+ }
373
+ return paragraphRange;
374
+ }
375
+ catch (error) {
376
+ logger.error(`Error getting paragraph range for index ${indexWithin} in doc ${documentId}: ${error.message || 'Unknown error'}`);
377
+ if (error.code === 404)
378
+ throw new UserError(`Document not found while finding paragraph (ID: ${documentId}).`);
379
+ if (error.code === 403)
380
+ throw new UserError(`Permission denied while accessing doc ${documentId}.`);
381
+ throw new Error(`Failed to find paragraph: ${error.message || 'Unknown error'}`);
382
+ }
383
+ }
384
+ // --- Style Request Builders ---
385
+ export function buildUpdateTextStyleRequest(startIndex, endIndex, style, tabId) {
386
+ const textStyle = {};
387
+ const fieldsToUpdate = [];
388
+ if (style.bold !== undefined) {
389
+ textStyle.bold = style.bold;
390
+ fieldsToUpdate.push('bold');
391
+ }
392
+ if (style.italic !== undefined) {
393
+ textStyle.italic = style.italic;
394
+ fieldsToUpdate.push('italic');
395
+ }
396
+ if (style.underline !== undefined) {
397
+ textStyle.underline = style.underline;
398
+ fieldsToUpdate.push('underline');
399
+ }
400
+ if (style.strikethrough !== undefined) {
401
+ textStyle.strikethrough = style.strikethrough;
402
+ fieldsToUpdate.push('strikethrough');
403
+ }
404
+ if (style.fontSize !== undefined) {
405
+ textStyle.fontSize = { magnitude: style.fontSize, unit: 'PT' };
406
+ fieldsToUpdate.push('fontSize');
407
+ }
408
+ if (style.fontFamily !== undefined) {
409
+ textStyle.weightedFontFamily = { fontFamily: style.fontFamily };
410
+ fieldsToUpdate.push('weightedFontFamily');
411
+ }
412
+ if (style.foregroundColor !== undefined) {
413
+ const rgbColor = hexToRgbColor(style.foregroundColor);
414
+ if (!rgbColor)
415
+ throw new UserError(`Invalid foreground hex color format: ${style.foregroundColor}`);
416
+ textStyle.foregroundColor = { color: { rgbColor: rgbColor } };
417
+ fieldsToUpdate.push('foregroundColor');
418
+ }
419
+ if (style.backgroundColor !== undefined) {
420
+ const rgbColor = hexToRgbColor(style.backgroundColor);
421
+ if (!rgbColor)
422
+ throw new UserError(`Invalid background hex color format: ${style.backgroundColor}`);
423
+ textStyle.backgroundColor = { color: { rgbColor: rgbColor } };
424
+ fieldsToUpdate.push('backgroundColor');
425
+ }
426
+ if (style.linkUrl !== undefined) {
427
+ textStyle.link = { url: style.linkUrl };
428
+ fieldsToUpdate.push('link');
429
+ }
430
+ // TODO: Handle clearing formatting
431
+ if (fieldsToUpdate.length === 0)
432
+ return null; // No styles to apply
433
+ const range = { startIndex, endIndex };
434
+ if (tabId) {
435
+ range.tabId = tabId;
436
+ }
437
+ const request = {
438
+ updateTextStyle: {
439
+ range,
440
+ textStyle: textStyle,
441
+ fields: fieldsToUpdate.join(','),
442
+ },
443
+ };
444
+ return { request, fields: fieldsToUpdate };
445
+ }
446
+ export function buildUpdateParagraphStyleRequest(startIndex, endIndex, style, tabId) {
447
+ // Create style object and track which fields to update
448
+ const paragraphStyle = {};
449
+ const fieldsToUpdate = [];
450
+ logger.debug(`Building paragraph style request for range ${startIndex}-${endIndex} with options:`, style);
451
+ // Process alignment option (LEFT, CENTER, RIGHT, JUSTIFIED)
452
+ if (style.alignment !== undefined) {
453
+ paragraphStyle.alignment = style.alignment;
454
+ fieldsToUpdate.push('alignment');
455
+ logger.debug(`Setting alignment to ${style.alignment}`);
456
+ }
457
+ // Process indentation options
458
+ if (style.indentStart !== undefined) {
459
+ paragraphStyle.indentStart = { magnitude: style.indentStart, unit: 'PT' };
460
+ fieldsToUpdate.push('indentStart');
461
+ logger.debug(`Setting left indent to ${style.indentStart}pt`);
462
+ }
463
+ if (style.indentEnd !== undefined) {
464
+ paragraphStyle.indentEnd = { magnitude: style.indentEnd, unit: 'PT' };
465
+ fieldsToUpdate.push('indentEnd');
466
+ logger.debug(`Setting right indent to ${style.indentEnd}pt`);
467
+ }
468
+ // Process spacing options
469
+ if (style.spaceAbove !== undefined) {
470
+ paragraphStyle.spaceAbove = { magnitude: style.spaceAbove, unit: 'PT' };
471
+ fieldsToUpdate.push('spaceAbove');
472
+ logger.debug(`Setting space above to ${style.spaceAbove}pt`);
473
+ }
474
+ if (style.spaceBelow !== undefined) {
475
+ paragraphStyle.spaceBelow = { magnitude: style.spaceBelow, unit: 'PT' };
476
+ fieldsToUpdate.push('spaceBelow');
477
+ logger.debug(`Setting space below to ${style.spaceBelow}pt`);
478
+ }
479
+ // Process named style types (headings, etc.)
480
+ if (style.namedStyleType !== undefined) {
481
+ paragraphStyle.namedStyleType = style.namedStyleType;
482
+ fieldsToUpdate.push('namedStyleType');
483
+ logger.debug(`Setting named style to ${style.namedStyleType}`);
484
+ }
485
+ // Process page break control
486
+ if (style.keepWithNext !== undefined) {
487
+ paragraphStyle.keepWithNext = style.keepWithNext;
488
+ fieldsToUpdate.push('keepWithNext');
489
+ logger.debug(`Setting keepWithNext to ${style.keepWithNext}`);
490
+ }
491
+ // Verify we have styles to apply
492
+ if (fieldsToUpdate.length === 0) {
493
+ logger.warn('No paragraph styling options were provided');
494
+ return null; // No styles to apply
495
+ }
496
+ // Build the range with optional tabId
497
+ const range = { startIndex, endIndex };
498
+ if (tabId) {
499
+ range.tabId = tabId;
500
+ }
501
+ // Build the request object
502
+ const request = {
503
+ updateParagraphStyle: {
504
+ range,
505
+ paragraphStyle: paragraphStyle,
506
+ fields: fieldsToUpdate.join(','),
507
+ },
508
+ };
509
+ logger.debug(`Created paragraph style request with fields: ${fieldsToUpdate.join(', ')}`);
510
+ return { request, fields: fieldsToUpdate };
511
+ }
512
+ // --- Specific Feature Helpers ---
513
+ export async function createTable(docs, documentId, rows, columns, index, tabId) {
514
+ if (rows < 1 || columns < 1) {
515
+ throw new UserError('Table must have at least 1 row and 1 column.');
516
+ }
517
+ const location = { index };
518
+ if (tabId) {
519
+ location.tabId = tabId;
520
+ }
521
+ const request = {
522
+ insertTable: {
523
+ location,
524
+ rows: rows,
525
+ columns: columns,
526
+ },
527
+ };
528
+ return executeBatchUpdate(docs, documentId, [request]);
529
+ }
530
+ export async function insertText(docs, documentId, text, index) {
531
+ if (!text)
532
+ return {}; // Nothing to insert
533
+ const request = {
534
+ insertText: {
535
+ location: { index },
536
+ text: text,
537
+ },
538
+ };
539
+ return executeBatchUpdate(docs, documentId, [request]);
540
+ }
541
+ // --- Table Cell Helper ---
542
+ /**
543
+ * Finds the content range of a specific table cell.
544
+ * Returns the start and end indices of the cell's text content (excluding trailing newline).
545
+ */
546
+ export async function getTableCellRange(docs, documentId, tableStartIndex, rowIndex, columnIndex, tabId) {
547
+ const res = await docs.documents.get({
548
+ documentId,
549
+ ...(tabId && { includeTabsContent: true }),
550
+ });
551
+ // Get body content from the correct tab or default
552
+ let bodyContent;
553
+ if (tabId) {
554
+ const allTabs = getAllTabs(res.data);
555
+ const tab = allTabs.find((t) => t.tabProperties?.tabId === tabId);
556
+ if (!tab)
557
+ throw new UserError(`Tab with ID "${tabId}" not found.`);
558
+ bodyContent = tab.documentTab?.body?.content;
559
+ }
560
+ else {
561
+ bodyContent = res.data.body?.content;
562
+ }
563
+ if (!bodyContent) {
564
+ throw new UserError(`No content found in document ${documentId}.`);
565
+ }
566
+ // Find the table element matching tableStartIndex
567
+ const tableElement = bodyContent.find((el) => el.table && el.startIndex === tableStartIndex);
568
+ if (!tableElement || !tableElement.table) {
569
+ throw new UserError(`No table found at startIndex ${tableStartIndex}. Use readGoogleDoc with format='json' to find the correct table startIndex.`);
570
+ }
571
+ const table = tableElement.table;
572
+ const rows = table.tableRows;
573
+ if (!rows || rowIndex < 0 || rowIndex >= rows.length) {
574
+ throw new UserError(`Row index ${rowIndex} is out of range. Table has ${rows?.length ?? 0} rows (0-based).`);
575
+ }
576
+ const cells = rows[rowIndex].tableCells;
577
+ if (!cells || columnIndex < 0 || columnIndex >= cells.length) {
578
+ throw new UserError(`Column index ${columnIndex} is out of range. Row ${rowIndex} has ${cells?.length ?? 0} columns (0-based).`);
579
+ }
580
+ const cell = cells[columnIndex];
581
+ const cellContent = cell.content;
582
+ if (!cellContent || cellContent.length === 0) {
583
+ throw new UserError(`Cell (${rowIndex}, ${columnIndex}) has no content elements.`);
584
+ }
585
+ // Cell always has at least one paragraph with a trailing \n.
586
+ // We want the range covering all content *before* that final \n.
587
+ const firstParagraph = cellContent[0];
588
+ const lastParagraph = cellContent[cellContent.length - 1];
589
+ const cellStartIndex = firstParagraph.startIndex;
590
+ // The endIndex of the last paragraph includes the trailing \n.
591
+ // We subtract 1 to exclude it so delete operations don't remove the cell structure.
592
+ const cellEndIndex = lastParagraph.endIndex;
593
+ if (cellStartIndex == null || cellEndIndex == null) {
594
+ throw new UserError(`Could not determine content range for cell (${rowIndex}, ${columnIndex}).`);
595
+ }
596
+ return { startIndex: cellStartIndex, endIndex: cellEndIndex - 1 };
597
+ }
598
+ // --- Complex / Stubbed Helpers ---
599
+ export async function findParagraphsMatchingStyle(docs, documentId, styleCriteria // Define a proper type for criteria (e.g., { fontFamily: 'Arial', bold: true })
600
+ ) {
601
+ // TODO: Implement logic
602
+ // 1. Get document content with paragraph elements and their styles.
603
+ // 2. Iterate through paragraphs.
604
+ // 3. For each paragraph, check if its computed style matches the criteria.
605
+ // 4. Return ranges of matching paragraphs.
606
+ logger.warn('findParagraphsMatchingStyle is not implemented.');
607
+ throw new NotImplementedError('Finding paragraphs by style criteria is not yet implemented.');
608
+ // return [];
609
+ }
610
+ export async function detectAndFormatLists(docs, documentId, startIndex, endIndex) {
611
+ // TODO: Implement complex logic
612
+ // 1. Get document content (paragraphs, text runs) in the specified range (or whole doc).
613
+ // 2. Iterate through paragraphs.
614
+ // 3. Identify sequences of paragraphs starting with list-like markers (e.g., "-", "*", "1.", "a)").
615
+ // 4. Determine nesting levels based on indentation or marker patterns.
616
+ // 5. Generate CreateParagraphBulletsRequests for the identified sequences.
617
+ // 6. Potentially delete the original marker text.
618
+ // 7. Execute the batch update.
619
+ logger.warn('detectAndFormatLists is not implemented.');
620
+ throw new NotImplementedError('Automatic list detection and formatting is not yet implemented.');
621
+ // return {};
622
+ }
623
+ export async function addCommentHelper(docs, documentId, text, startIndex, endIndex) {
624
+ // NOTE: Adding comments typically requires the Google Drive API v3 and different scopes!
625
+ // 'https://www.googleapis.com/auth/drive' or more specific comment scopes.
626
+ // This helper is a placeholder assuming Drive API client (`drive`) is available and authorized.
627
+ /*
628
+ const drive = google.drive({version: 'v3', auth: authClient}); // Assuming authClient is available
629
+ await drive.comments.create({
630
+ fileId: documentId,
631
+ requestBody: {
632
+ content: text,
633
+ anchor: JSON.stringify({ // Anchor format might need verification
634
+ 'type': 'workbook#textAnchor', // Or appropriate type for Docs
635
+ 'refs': [{
636
+ 'docRevisionId': 'head', // Or specific revision
637
+ 'range': {
638
+ 'start': startIndex,
639
+ 'end': endIndex,
640
+ }
641
+ }]
642
+ })
643
+ },
644
+ fields: 'id'
645
+ });
646
+ */
647
+ logger.warn('addCommentHelper requires Google Drive API and is not implemented.');
648
+ throw new NotImplementedError('Adding comments requires Drive API setup and is not yet implemented.');
649
+ }
650
+ // --- Image Insertion Helpers ---
651
+ /**
652
+ * Inserts an inline image into a document from a publicly accessible URL
653
+ * @param docs - Google Docs API client
654
+ * @param documentId - The document ID
655
+ * @param imageUrl - Publicly accessible URL to the image
656
+ * @param index - Position in the document where image should be inserted (1-based)
657
+ * @param width - Optional width in points
658
+ * @param height - Optional height in points
659
+ * @returns Promise with batch update response
660
+ */
661
+ export async function insertInlineImage(docs, documentId, imageUrl, index, width, height, tabId) {
662
+ // Validate URL format
663
+ try {
664
+ new URL(imageUrl);
665
+ }
666
+ catch (e) {
667
+ throw new UserError(`Invalid image URL format: ${imageUrl}`);
668
+ }
669
+ // Build the insertInlineImage request
670
+ const location = { index };
671
+ if (tabId) {
672
+ location.tabId = tabId;
673
+ }
674
+ const request = {
675
+ insertInlineImage: {
676
+ location,
677
+ uri: imageUrl,
678
+ ...(width &&
679
+ height && {
680
+ objectSize: {
681
+ height: { magnitude: height, unit: 'PT' },
682
+ width: { magnitude: width, unit: 'PT' },
683
+ },
684
+ }),
685
+ },
686
+ };
687
+ return executeBatchUpdate(docs, documentId, [request]);
688
+ }
689
+ /**
690
+ * Uploads a local image file to Google Drive.
691
+ *
692
+ * When `skipPublicSharing` is false (default), the file is made publicly
693
+ * readable and its webContentLink is returned — required for the Docs API
694
+ * insertInlineImage approach.
695
+ *
696
+ * When `skipPublicSharing` is true, only the Drive file ID is returned.
697
+ * Use this with the Apps Script insertion path where no public URL is needed.
698
+ */
699
+ export async function uploadImageToDrive(drive, // drive_v3.Drive type
700
+ localFilePath, parentFolderId, skipPublicSharing = false) {
701
+ const fs = await import('fs');
702
+ const path = await import('path');
703
+ if (!fs.existsSync(localFilePath)) {
704
+ throw new UserError(`Image file not found: ${localFilePath}`);
705
+ }
706
+ const fileName = path.basename(localFilePath);
707
+ const mimeTypeMap = {
708
+ '.jpg': 'image/jpeg',
709
+ '.jpeg': 'image/jpeg',
710
+ '.png': 'image/png',
711
+ '.gif': 'image/gif',
712
+ '.bmp': 'image/bmp',
713
+ '.webp': 'image/webp',
714
+ '.svg': 'image/svg+xml',
715
+ };
716
+ const ext = path.extname(localFilePath).toLowerCase();
717
+ const mimeType = mimeTypeMap[ext] || 'application/octet-stream';
718
+ const fileMetadata = {
719
+ name: fileName,
720
+ mimeType: mimeType,
721
+ };
722
+ if (parentFolderId) {
723
+ fileMetadata.parents = [parentFolderId];
724
+ }
725
+ const media = {
726
+ mimeType: mimeType,
727
+ body: fs.createReadStream(localFilePath),
728
+ };
729
+ const uploadResponse = await drive.files.create({
730
+ requestBody: fileMetadata,
731
+ media: media,
732
+ fields: 'id,webViewLink,webContentLink',
733
+ supportsAllDrives: true,
734
+ });
735
+ const fileId = uploadResponse.data.id;
736
+ if (!fileId) {
737
+ throw new Error('Failed to upload image to Drive - no file ID returned');
738
+ }
739
+ if (skipPublicSharing) {
740
+ return fileId;
741
+ }
742
+ await drive.permissions.create({
743
+ fileId: fileId,
744
+ requestBody: {
745
+ role: 'reader',
746
+ type: 'anyone',
747
+ },
748
+ supportsAllDrives: true,
749
+ });
750
+ const fileInfo = await drive.files.get({
751
+ fileId: fileId,
752
+ fields: 'webContentLink',
753
+ supportsAllDrives: true,
754
+ });
755
+ const webContentLink = fileInfo.data.webContentLink;
756
+ if (!webContentLink) {
757
+ throw new Error('Failed to get public URL for uploaded image');
758
+ }
759
+ return webContentLink;
760
+ }
761
+ /**
762
+ * Inserts an image into a Google Doc via Apps Script.
763
+ *
764
+ * Flow:
765
+ * 1. Insert a unique marker string at the target index using the Docs API.
766
+ * 2. Call the deployed Apps Script which finds the marker and replaces it
767
+ * with the actual image blob from Drive (no public sharing needed).
768
+ */
769
+ export async function insertImageViaAppsScript(docs, scriptClient, // script_v1.Script type
770
+ deploymentId, documentId, driveFileId, charIndex, tabId) {
771
+ const marker = `[mcp-img-${driveFileId}]`;
772
+ // Step 1: Insert marker at the requested position via Docs API
773
+ const location = { index: charIndex };
774
+ if (tabId) {
775
+ location.tabId = tabId;
776
+ }
777
+ await executeBatchUpdate(docs, documentId, [{ insertText: { location, text: marker } }]);
778
+ // Step 2: Call Apps Script to replace the marker with the image
779
+ const response = await scriptClient.scripts.run({
780
+ scriptId: deploymentId,
781
+ requestBody: {
782
+ function: 'insertImageByFileId',
783
+ parameters: [documentId, driveFileId],
784
+ },
785
+ });
786
+ const result = response.data?.response?.result;
787
+ if (!result || !result.success) {
788
+ const msg = result?.message || 'Unknown Apps Script error';
789
+ throw new Error(`Apps Script image insertion failed: ${msg}`);
790
+ }
791
+ }
792
+ /**
793
+ * Recursively collect all tabs from a document in a flat list with hierarchy info
794
+ * @param doc - The Google Doc document object
795
+ * @returns Array of tabs with nesting level information
796
+ */
797
+ export function getAllTabs(doc) {
798
+ const allTabs = [];
799
+ if (!doc.tabs || doc.tabs.length === 0) {
800
+ return allTabs;
801
+ }
802
+ for (const tab of doc.tabs) {
803
+ addCurrentAndChildTabs(tab, allTabs, 0);
804
+ }
805
+ return allTabs;
806
+ }
807
+ /**
808
+ * Recursive helper to add tabs with their nesting level
809
+ * @param tab - The tab to add
810
+ * @param allTabs - The accumulator array
811
+ * @param level - Current nesting level (0 for top-level)
812
+ */
813
+ function addCurrentAndChildTabs(tab, allTabs, level) {
814
+ allTabs.push({ ...tab, level });
815
+ if (tab.childTabs && tab.childTabs.length > 0) {
816
+ for (const childTab of tab.childTabs) {
817
+ addCurrentAndChildTabs(childTab, allTabs, level + 1);
818
+ }
819
+ }
820
+ }
821
+ /**
822
+ * Get the text length from a DocumentTab
823
+ * @param documentTab - The DocumentTab object
824
+ * @returns Total character count
825
+ */
826
+ export function getTabTextLength(documentTab) {
827
+ let totalLength = 0;
828
+ if (!documentTab?.body?.content) {
829
+ return 0;
830
+ }
831
+ documentTab.body.content.forEach((element) => {
832
+ // Handle paragraphs
833
+ if (element.paragraph?.elements) {
834
+ element.paragraph.elements.forEach((pe) => {
835
+ if (pe.textRun?.content) {
836
+ totalLength += pe.textRun.content.length;
837
+ }
838
+ });
839
+ }
840
+ // Handle tables
841
+ if (element.table?.tableRows) {
842
+ element.table.tableRows.forEach((row) => {
843
+ row.tableCells?.forEach((cell) => {
844
+ cell.content?.forEach((cellElement) => {
845
+ cellElement.paragraph?.elements?.forEach((pe) => {
846
+ if (pe.textRun?.content) {
847
+ totalLength += pe.textRun.content.length;
848
+ }
849
+ });
850
+ });
851
+ });
852
+ });
853
+ }
854
+ });
855
+ return totalLength;
856
+ }
857
+ /**
858
+ * Find a specific tab by ID in a document (searches recursively through child tabs)
859
+ * @param doc - The Google Doc document object
860
+ * @param tabId - The tab ID to search for
861
+ * @returns The tab object if found, null otherwise
862
+ */
863
+ export function findTabById(doc, tabId) {
864
+ if (!doc.tabs || doc.tabs.length === 0) {
865
+ return null;
866
+ }
867
+ // Helper function to search through tabs recursively
868
+ const searchTabs = (tabs) => {
869
+ for (const tab of tabs) {
870
+ if (tab.tabProperties?.tabId === tabId) {
871
+ return tab;
872
+ }
873
+ // Recursively search child tabs
874
+ if (tab.childTabs && tab.childTabs.length > 0) {
875
+ const found = searchTabs(tab.childTabs);
876
+ if (found)
877
+ return found;
878
+ }
879
+ }
880
+ return null;
881
+ };
882
+ return searchTabs(doc.tabs);
883
+ }