1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
// CanvasDocsTool.ts - Provides access to all documents on the canvas
import { BaseTool } from './BaseTool';
import { Observation } from '../types/types';
import { ParametersType, ToolInfo } from '../types/tool_types';
import { DocumentView } from '../../DocumentView';
import { Doc } from '../../../../../fields/Doc';
import { v4 as uuidv4 } from 'uuid';
import { Id } from '../../../../../fields/FieldSymbols';
import { DocumentType } from '../../../../documents/DocumentTypes';
const parameterRules = [
{
name: 'action',
type: 'string',
description: 'The action to perform: "list" to get all canvas documents, "getById" to get a specific document, "getByType" to filter by document type',
required: true,
},
{
name: 'documentId',
type: 'string',
description: 'The ID of a specific document to retrieve (required for "getById" action)',
required: false,
},
{
name: 'documentType',
type: 'string',
description: 'Filter documents by type: text, image, pdf, video, audio, web, diagram, dataviz, collection, etc. (required for "getByType" action)',
required: false,
},
{
name: 'includeSystemDocs',
type: 'boolean',
description: 'Whether to include system documents in the results (default: false)',
required: false,
},
] as const;
const toolInfo: ToolInfo<typeof parameterRules> = {
name: 'canvasDocs',
description: 'Access and analyze all documents currently visible on the canvas/workspace. This tool provides broader document recognition beyond just linked documents, allowing the agent to work with any document in the current view. Useful for comprehensive document analysis, finding related content, and understanding the full context of the workspace.',
parameterRules,
citationRules: 'Cite documents by their title and type when referencing canvas documents.',
};
export class CanvasDocsTool extends BaseTool<typeof parameterRules> {
constructor() {
super(toolInfo);
}
/**
* Get all documents currently visible on the canvas using parent view approach
*/
private getAllCanvasDocuments(includeSystemDocs: boolean = false): Doc[] {
try {
console.log('[CanvasDocsTool] Starting canvas document discovery...');
const canvasDocs: Doc[] = [];
const seenIds = new Set<string>();
const sources = { parentView: 0, allViews: 0, lightbox: 0 };
// Method 1: Try to get documents from parent view if it's a collection
const parentViewDocs = this.getParentViewDocuments();
if (parentViewDocs.length > 0) {
console.log(`[CanvasDocsTool] Found ${parentViewDocs.length} documents from parent view`);
const parentDocsAdded = parentViewDocs.reduce((count, doc) => {
return this.addDocumentToResults(doc, canvasDocs, seenIds, includeSystemDocs, 'parentView') ? count + 1 : count;
}, 0);
sources.parentView = parentDocsAdded;
}
// Method 2: Get documents from all rendered views (fallback)
const viewDocs = this.getDocumentsFromViews();
console.log(`[CanvasDocsTool] Found ${viewDocs.length} documents from document views`);
const viewDocsAdded = viewDocs.reduce((count, doc) => {
return this.addDocumentToResults(doc, canvasDocs, seenIds, includeSystemDocs, 'allViews') ? count + 1 : count;
}, 0);
sources.allViews = viewDocsAdded;
// Method 3: Include lightbox document if active
const lightboxDoc = this.getLightboxDocument();
if (lightboxDoc) {
console.log('[CanvasDocsTool] Found lightbox document');
sources.lightbox = this.addDocumentToResults(lightboxDoc, canvasDocs, seenIds, includeSystemDocs, 'lightbox') ? 1 : 0;
}
console.log(`[CanvasDocsTool] Total: ${canvasDocs.length} documents found from sources:`, sources);
return canvasDocs;
} catch (error) {
console.error('[CanvasDocsTool] Error getting canvas documents:', error);
return [];
}
}
/**
* Get documents from the parent view if it's a collection
*/
private getParentViewDocuments(): Doc[] {
try {
// Get all DocumentViews and look for collections with child documents
const allViews = DocumentView.allViews();
const parentDocs: Doc[] = [];
for (const view of allViews) {
// Check if this view has a ComponentView with child documents
const componentView = (view as any).ComponentView || (view as any)._componentView;
if (componentView) {
// Method 1: Try hasChildDocs() which returns layout documents
if (typeof componentView.hasChildDocs === 'function') {
try {
const childDocs = componentView.hasChildDocs();
if (Array.isArray(childDocs) && childDocs.length > 0) {
console.log(`[CanvasDocsTool] Found collection with ${childDocs.length} child documents from hasChildDocs`);
childDocs.forEach((doc: any, index: number) => {
console.log(`[CanvasDocsTool] Child doc ${index}: id=${doc?.[Id]}, title="${doc?.title}", type=${typeof doc}`);
if (doc && typeof doc === 'object' && doc[Id]) {
parentDocs.push(doc);
}
});
}
} catch (e) {
console.log('[CanvasDocsTool] Error calling hasChildDocs:', e);
}
}
// Method 2: Try childLayoutPairs property
if (componentView.childLayoutPairs) {
try {
const childPairs = componentView.childLayoutPairs;
if (Array.isArray(childPairs)) {
console.log(`[CanvasDocsTool] Found ${childPairs.length} childLayoutPairs`);
childPairs.forEach((pair: any, index: number) => {
const layoutDoc = pair.layout;
console.log(`[CanvasDocsTool] Pair ${index}: layout id=${layoutDoc?.[Id]}, title="${layoutDoc?.title}"`);
if (layoutDoc && layoutDoc[Id]) {
parentDocs.push(layoutDoc);
}
});
}
} catch (e) {
console.log('[CanvasDocsTool] Error accessing childLayoutPairs:', e);
}
}
// Method 3: Try childDocs property (source data)
if (componentView.childDocs) {
try {
const childDocs = componentView.childDocs;
if (Array.isArray(childDocs)) {
console.log(`[CanvasDocsTool] Found ${childDocs.length} raw childDocs`);
childDocs.forEach((doc: any, index: number) => {
console.log(`[CanvasDocsTool] Raw doc ${index}: id=${doc?.[Id]}, title="${doc?.title}"`);
if (doc && doc[Id]) {
parentDocs.push(doc);
}
});
}
} catch (e) {
console.log('[CanvasDocsTool] Error accessing childDocs:', e);
}
}
}
}
console.log(`[CanvasDocsTool] Total parent docs collected: ${parentDocs.length}`);
return parentDocs;
} catch (error) {
console.error('[CanvasDocsTool] Error getting parent view documents:', error);
return [];
}
}
/**
* Get documents from rendered DocumentViews
*/
private getDocumentsFromViews(): Doc[] {
try {
const allViews = DocumentView.allViews();
const viewDocs: Doc[] = [];
for (const view of allViews) {
const doc = view.Document;
if (doc && doc[Id]) {
viewDocs.push(doc);
}
}
return viewDocs;
} catch (error) {
console.error('[CanvasDocsTool] Error getting view documents:', error);
return [];
}
}
/**
* Get the current lightbox document if any
*/
private getLightboxDocument(): Doc | null {
try {
// Try to get lightbox document using DocumentView static method
if (typeof DocumentView.LightboxDoc === 'function') {
const lightboxDoc = DocumentView.LightboxDoc();
return lightboxDoc || null;
}
return null;
} catch (error) {
console.log('[CanvasDocsTool] No lightbox document found');
return null;
}
}
/**
* Add a document to results if it passes filters
*/
private addDocumentToResults(doc: any, canvasDocs: Doc[], seenIds: Set<string>, includeSystemDocs: boolean, source: string): boolean {
if (!doc || !doc[Id]) {
console.log(`[CanvasDocsTool] Skipped document from ${source}: no doc or no ID`);
return false;
}
const docId = String(doc[Id]);
if (seenIds.has(docId)) {
console.log(`[CanvasDocsTool] Skipped document from ${source}: duplicate ID ${docId}`);
return false;
}
seenIds.add(docId);
// Skip system documents unless explicitly requested
if (!includeSystemDocs && Doc.IsSystem(doc)) {
console.log(`[CanvasDocsTool] Skipped document from ${source}: system document ${doc.title || docId}`);
return false;
}
// Skip hidden documents
if (doc.hidden) {
console.log(`[CanvasDocsTool] Skipped document from ${source}: hidden document ${doc.title || docId}`);
return false;
}
canvasDocs.push(doc);
console.log(`[CanvasDocsTool] Added document from ${source}: ${doc.title || 'Untitled'} (${doc.type || 'unknown'})`);
return true;
}
/**
* Get document summary for display
*/
private getDocumentSummary(doc: Doc): any {
try {
return {
id: String(doc[Id] || 'unknown'),
title: String(doc.title || 'Untitled'),
type: doc.type || 'unknown',
x: doc.x || 0,
y: doc.y || 0,
width: doc._width || 'auto',
height: doc._height || 'auto',
tags: doc.tags ? Array.from(doc.tags as any) : [],
hasData: !!doc.data,
dataType: typeof doc.data,
lastModified: doc.lastModified || 'unknown',
layout: {
fitWidth: doc._layout_fitWidth || false,
autoHeight: doc._layout_autoHeight || false,
showTitle: doc._layout_showTitle || false,
}
};
} catch (error) {
console.warn('[CanvasDocsTool] Error getting document summary for doc:', doc[Id], error);
return {
id: String(doc[Id] || 'unknown'),
title: 'Error reading document',
type: 'unknown',
error: String(error)
};
}
}
async execute(args: ParametersType<typeof parameterRules>): Promise<Observation[]> {
const chunkId = uuidv4();
console.log('[CanvasDocsTool] Executing action:', args.action);
try {
const includeSystemDocs = args.includeSystemDocs || false;
const allCanvasDocs = this.getAllCanvasDocuments(includeSystemDocs);
// Filter out container/dashboard documents to only show actual content
const canvasDocs = allCanvasDocs.filter(doc => {
// Filter out collection containers and docking views (dashboards)
const isContainer = doc.type === 'collection' || doc._type_collection === 'Docking';
if (isContainer) {
console.log(`[CanvasDocsTool] Filtering out container document: ${doc.title || 'Untitled'} (type: ${doc.type}, _type_collection: ${doc._type_collection})`);
return false;
}
return true;
});
console.log(`[CanvasDocsTool] Found ${allCanvasDocs.length} total documents, ${canvasDocs.length} content documents after filtering containers`);
switch (args.action) {
case 'list': {
const documentSummaries = canvasDocs.map(doc => this.getDocumentSummary(doc));
return [
{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="canvas_documents">
Found ${canvasDocs.length} documents on the canvas:
${documentSummaries.map(doc =>
`• ${doc.title} (${doc.type}) - ID: ${doc.id} - Position: (${doc.x}, ${doc.y}) - Size: ${doc.width}x${doc.height}`
).join('\n')}
Document Details:
${JSON.stringify(documentSummaries, null, 2)}
</chunk>`,
},
];
}
case 'getById': {
if (!args.documentId) {
return [
{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="error">
documentId parameter is required for getById action.
</chunk>`,
},
];
}
const targetDoc = canvasDocs.find(doc => String(doc[Id]) === args.documentId);
if (!targetDoc) {
return [
{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="error">
Document with ID "${args.documentId}" not found on canvas.
Available document IDs: ${canvasDocs.map(d => d[Id]).join(', ')}
</chunk>`,
},
];
}
const docSummary = this.getDocumentSummary(targetDoc);
return [
{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="canvas_document">
Document found: ${docSummary.title} (${docSummary.type})
${JSON.stringify(docSummary, null, 2)}
</chunk>`,
},
];
}
case 'getByType': {
if (!args.documentType) {
return [
{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="error">
documentType parameter is required for getByType action.
Available types: ${Array.from(new Set(canvasDocs.map(d => d.type))).join(', ')}
</chunk>`,
},
];
}
const filteredDocs = canvasDocs.filter(doc =>
doc.type === args.documentType ||
(doc.type && typeof doc.type === 'string' && doc.type.toLowerCase() === args.documentType.toLowerCase())
);
if (filteredDocs.length === 0) {
return [
{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="canvas_documents">
No documents of type "${args.documentType}" found on canvas.
Available types: ${Array.from(new Set(canvasDocs.map(d => d.type))).join(', ')}
</chunk>`,
},
];
}
const documentSummaries = filteredDocs.map(doc => this.getDocumentSummary(doc));
return [
{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="canvas_documents">
Found ${filteredDocs.length} documents of type "${args.documentType}" on canvas:
${documentSummaries.map(doc =>
`• ${doc.title} - ID: ${doc.id} - Position: (${doc.x}, ${doc.y})`
).join('\n')}
Document Details:
${JSON.stringify(documentSummaries, null, 2)}
</chunk>`,
},
];
}
default:
return [
{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="error">
Unknown action: "${args.action}". Available actions: list, getById, getByType
</chunk>`,
},
];
}
} catch (error) {
console.error('[CanvasDocsTool] Execution error:', error);
return [
{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="error">
Canvas documents access failed: ${error instanceof Error ? error.message : String(error)}
</chunk>`,
},
];
}
}
/**
* Get all canvas documents as a simple array (for external use)
*/
static getAllCanvasDocuments(includeSystemDocs: boolean = false): Doc[] {
// Create a temporary instance to use the private methods
const tool = new CanvasDocsTool();
return tool.getAllCanvasDocuments(includeSystemDocs);
}
}
|