aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/utils/AgentDocumentManager.ts
blob: dcb70845063331c9cabcde7fca584f2787807555 (plain)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
import { action, computed, makeObservable, observable, ObservableMap, reaction, runInAction } from 'mobx';
import { Doc, FieldResult, StrListCast } from '../../../../../fields/Doc';
import { DocData } from '../../../../../fields/DocSymbols';
import { Id } from '../../../../../fields/FieldSymbols';
import { List } from '../../../../../fields/List';
import { DocCast, StrCast } from '../../../../../fields/Types';
import { DocServer } from '../../../../DocServer';
import { Docs, DocumentOptions } from '../../../../documents/Documents';
import { DocumentManager } from '../../../../util/DocumentManager';
import { LinkManager, UPDATE_SERVER_CACHE } from '../../../../util/LinkManager';
import { DocumentView } from '../../DocumentView';
import { ChatBox, parsedDoc } from '../chatboxcomponents/ChatBox';
import { supportedDocTypes } from '../types/tool_types';
import { CHUNK_TYPE, RAGChunk, SimplifiedChunk } from '../types/types';

/**
 * Interface representing a document in the freeform view
 */
interface AgentDocument {
    layoutDoc: Doc;
    dataDoc: Doc;
}

/**
 * Class to manage documents in a freeform view
 */
export class AgentDocumentManager {
    @observable private documentsById: ObservableMap<string, AgentDocument>;
    private chatBox: ChatBox;
    private chatBoxDocument: Doc | null = null;
    private fieldMetadata: Record<string, any> = {}; // bcz: CHANGE any to a proper type!
    @observable private simplifiedChunks: ObservableMap<string, SimplifiedChunk>;

    /**
     * Creates a new DocumentManager
     * @param templateDocument The document that serves as a template for new documents
     */
    constructor(chatBox: ChatBox) {
        makeObservable(this);
        const agentDoc = DocCast(chatBox.Document.agentDocument) ?? new Doc();
        const chunk_simpl = DocCast(agentDoc.chunk_simpl) ?? new Doc();

        agentDoc.title = chatBox.Document.title + '_agentDocument';
        chunk_simpl.title = '_chunk_simpl';
        chatBox.Document.agentDocument = agentDoc;
        DocCast(chatBox.Document.agentDocument)!.chunk_simpl = chunk_simpl;

        this.simplifiedChunks = StrListCast(chunk_simpl.mapping).reduce((mapping, chunks) => {
            StrListCast(chunks).forEach(chunk => {
                const parsed = JSON.parse(StrCast(chunk));
                mapping.set(parsed.chunkId, parsed);
            });
            return mapping;
        }, new ObservableMap<string, SimplifiedChunk>());

        this.documentsById = StrListCast(agentDoc.mapping).reduce((mapping, content) => {
            const [id, layoutId, docId] = content.split(':');
            const layoutDoc = DocServer.GetCachedRefField(layoutId);
            const dataDoc = DocServer.GetCachedRefField(docId);
            if (!layoutDoc || !dataDoc) {
                console.warn(`Document with ID ${id} not found in mapping`);
            } else {
                mapping.set(id, { layoutDoc, dataDoc });
            }
            return mapping;
        }, new ObservableMap<string, AgentDocument>());
        console.log(`AgentDocumentManager initialized with ${this.documentsById.size} documents`);
        this.chatBox = chatBox;
        this.chatBoxDocument = chatBox.Document;

        reaction(
            () => this.documentsById.values(),
            () => {
                if (this.chatBoxDocument && DocCast(this.chatBoxDocument.agentDocument)) {
                    DocCast(this.chatBoxDocument.agentDocument)!.mapping = new List<string>(Array.from(this.documentsById.entries()).map(([id, agent]) => `${id}:${agent.dataDoc[Id]}:${agent.layoutDoc[Id]}`));
                }
            }
            //{ fireImmediately: true }
        );
        reaction(
            () => this.simplifiedChunks.values(),
            () => {
                if (this.chatBoxDocument && DocCast(this.chatBoxDocument.agentDocument)) {
                    DocCast(DocCast(this.chatBoxDocument.agentDocument)!.chunk_simpl)!.mapping = new List<string>(Array.from(this.simplifiedChunks.values()).map(chunk => JSON.stringify(chunk)));
                }
            }
            //{ fireImmediately: true }
        );
        this.processDocument(this.chatBoxDocument);
        this.initializeFieldMetadata();
    }

    /**
     * Extracts field metadata from DocumentOptions class
     */
    private initializeFieldMetadata() {
        // Parse DocumentOptions to extract field definitions
        const documentOptionsInstance = new DocumentOptions();
        const documentOptionsEntries = Object.entries(documentOptionsInstance);

        for (const [fieldName, fieldInfo] of documentOptionsEntries) {
            // Extract field information
            const fieldData: Record<string, any> = {
                // bcz: CHANGE any to a proper type!
                name: fieldName,
                withoutUnderscore: fieldName.startsWith('_') ? fieldName.substring(1) : fieldName,
                description: '',
                type: 'unknown',
                required: false,
                defaultValue: undefined,
                possibleValues: [],
            };

            // Check if fieldInfo has description property (it's likely a FInfo instance)
            if (fieldInfo && typeof fieldInfo === 'object' && 'description' in fieldInfo) {
                fieldData.description = fieldInfo.description;

                // Extract field type if available
                if ('fieldType' in fieldInfo) {
                    fieldData.type = fieldInfo.fieldType;
                }

                // Extract possible values if available
                if ('values' in fieldInfo && Array.isArray(fieldInfo.values)) {
                    fieldData.possibleValues = fieldInfo.values;
                }
            }

            this.fieldMetadata[fieldName] = fieldData;
        }
    }

    /**
     * Gets all documents in the same Freeform view as the ChatBox
     * Uses the LinkManager to get all linked documents, similar to how ChatBox does it
     */
    public initializeFindDocsFreeform() {
        // Reset collections
        //this.documentsById.clear();

        try {
            // Use the LinkManager approach which is proven to work in ChatBox
            if (this.chatBoxDocument) {
                console.log('Finding documents linked to ChatBox document with ID:', this.chatBoxDocument[Id]);

                // Get directly linked documents via LinkManager
                const linkedDocs = LinkManager.Instance.getAllRelatedLinks(this.chatBoxDocument)
                    .map(d => DocCast(LinkManager.getOppositeAnchor(d, this.chatBoxDocument!)))
                    .map(d => DocCast(d?.annotationOn, d))
                    .filter(d => d);

                console.log(`Found ${linkedDocs.length} linked documents via LinkManager`);

                // Process the linked documents
                linkedDocs.forEach(async (doc: Doc | undefined) => {
                    if (doc) {
                        await this.processDocument(doc);
                        console.log('Processed linked document:', doc[Id], doc.title, doc.type);
                    }
                });
            }
        } catch (error) {
            console.error('Error finding documents in Freeform view:', error);
        }
    }

    /**
     * Process a document by ensuring it has an ID and adding it to the appropriate collections
     * @param doc The document to process
     */
    @action
    public async processDocument(doc: Doc): Promise<string> {
        // Ensure document has a persistent ID
        const docId = this.ensureDocumentId(doc);
        if (doc.chunk_simplified) {
            const newChunks: SimplifiedChunk[] = [];
            for (const chunk of JSON.parse(StrCast(doc.chunk_simplified))) {
                console.log('chunk', chunk);
                newChunks.push(chunk as SimplifiedChunk);
            }
            console.log('Added simplified chunks to simplifiedChunks:', docId, newChunks);
            this.addSimplifiedChunks(newChunks);
            //DocCast(DocCast(this.chatBoxDocument!.agentDocument)!.chunk_simpl)!.mapping = new List<string>(Array.from(this.simplifiedChunks.values()).map(chunk => JSON.stringify(chunk)));
        }
        // Only add if we haven't already processed this document
        if (!this.documentsById.has(docId)) {
            this.documentsById.set(docId, { layoutDoc: doc, dataDoc: doc[DocData] });
            console.log('Added document to documentsById:', doc[Id], docId, doc[Id], doc[DocData][Id]);
        }
        return docId;
    }

    /**
     * Ensures a document has a persistent ID stored in its metadata
     * @param doc The document to ensure has an ID
     * @returns The document's ID
     */
    private ensureDocumentId(doc: Doc): string {
        let docId: string | undefined;

        // 1. Try the direct id property if it exists
        if (doc[Id]) {
            console.log('Found document ID (normal):', doc[Id]);
            docId = doc[Id];
        } else {
            throw new Error('No document ID found');
        }

        return docId;
    }

    /**
     * Extracts metadata from a specific document
     * @param docId The ID of the document to extract metadata from
     * @returns An object containing the document's metadata
     */
    public extractDocumentMetadata(id: string) {
        if (!id) return null;
        const agentDoc = this.documentsById.get(id);
        if (!agentDoc) return null;
        const layoutDoc = agentDoc.layoutDoc;
        const dataDoc = agentDoc.dataDoc;

        const metadata: Record<string, any> = {
            // bcz: CHANGE any to a proper type!
            id: layoutDoc[Id] || dataDoc[Id] || '',
            title: layoutDoc.title || '',
            type: layoutDoc.type || '',
            fields: {
                layout: {},
                data: {},
            },
            fieldLocationMap: {},
        };

        // Process all known field definitions
        Object.keys(this.fieldMetadata).forEach(fieldName => {
            // const fieldDef = this.fieldMetadata[fieldName];
            const strippedName = fieldName.startsWith('_') ? fieldName.substring(1) : fieldName;

            // Check if field exists on layout document
            let layoutValue = undefined;
            if (layoutDoc) {
                layoutValue = layoutDoc[fieldName];
                if (layoutValue !== undefined) {
                    // Field exists on layout document
                    metadata.fields.layout[fieldName] = this.formatFieldValue(layoutValue);
                    metadata.fieldLocationMap[strippedName] = 'layout';
                }
            }

            // Check if field exists on data document
            let dataValue = undefined;
            if (dataDoc) {
                dataValue = dataDoc[fieldName];
                if (dataValue !== undefined) {
                    // Field exists on data document
                    metadata.fields.data[fieldName] = this.formatFieldValue(dataValue);
                    if (!metadata.fieldLocationMap[strippedName]) {
                        metadata.fieldLocationMap[strippedName] = 'data';
                    }
                }
            }

            // For fields with stripped names (without leading underscore),
            // also check if they exist on documents without the underscore
            if (fieldName.startsWith('_')) {
                const nonUnderscoreFieldName = fieldName.substring(1);

                if (layoutDoc) {
                    const nonUnderscoreLayoutValue = layoutDoc[nonUnderscoreFieldName];
                    if (nonUnderscoreLayoutValue !== undefined) {
                        metadata.fields.layout[nonUnderscoreFieldName] = this.formatFieldValue(nonUnderscoreLayoutValue);
                        metadata.fieldLocationMap[nonUnderscoreFieldName] = 'layout';
                    }
                }

                if (dataDoc) {
                    const nonUnderscoreDataValue = dataDoc[nonUnderscoreFieldName];
                    if (nonUnderscoreDataValue !== undefined) {
                        metadata.fields.data[nonUnderscoreFieldName] = this.formatFieldValue(nonUnderscoreDataValue);
                        if (!metadata.fieldLocationMap[nonUnderscoreFieldName]) {
                            metadata.fieldLocationMap[nonUnderscoreFieldName] = 'data';
                        }
                    }
                }
            }
        });

        // Add common field aliases for easier discovery
        // This helps users understand both width and _width refer to the same property
        if (metadata.fields.layout._width !== undefined && metadata.fields.layout.width === undefined) {
            metadata.fields.layout.width = metadata.fields.layout._width;
            metadata.fieldLocationMap.width = 'layout';
        }

        if (metadata.fields.layout._height !== undefined && metadata.fields.layout.height === undefined) {
            metadata.fields.layout.height = metadata.fields.layout._height;
            metadata.fieldLocationMap.height = 'layout';
        }

        return metadata;
    }

    /**
     * Formats a field value for JSON output
     * @param value The field value to format
     * @returns A JSON-friendly representation of the field value
     */
    private formatFieldValue(value: FieldResult | undefined) {
        if (value === undefined || value === null) {
            return null;
        }

        // Handle Doc objects
        if (value instanceof Doc) {
            return {
                type: 'Doc',
                id: value[Id] || this.ensureDocumentId(value),
                title: value.title || '',
                docType: value.type || '',
            };
        }

        // Handle RichTextField (try to extract plain text)
        if (typeof value === 'string' && value.includes('"type":"doc"') && value.includes('"content":')) {
            try {
                const rtfObj = JSON.parse(value);
                // If this looks like a rich text field structure
                if (rtfObj.doc && rtfObj.doc.content) {
                    // Recursively extract text from the content
                    let plainText = '';
                    const extractText = (node: { text: string; content?: unknown[] }) => {
                        if (node.text) {
                            plainText += node.text;
                        }
                        if (node.content && Array.isArray(node.content)) {
                            node.content.forEach(child => extractText(child as { text: string; content?: unknown[] }));
                        }
                    };

                    extractText(rtfObj.doc);

                    // If we successfully extracted text, show it, but also preserve the original value
                    if (plainText) {
                        return {
                            type: 'RichText',
                            text: plainText,
                            length: plainText.length,
                            // Don't include the full value as it can be very large
                        };
                    }
                }
            } catch {
                // If parsing fails, just treat as a regular string
            }
        }

        // Handle arrays and complex objects
        if (typeof value === 'object') {
            // If the object has a toString method, use it
            if (value.toString && value.toString !== Object.prototype.toString) {
                return value.toString();
            }

            try {
                // Try to convert to JSON string
                return JSON.stringify(value);
            } catch {
                return '[Complex Object]';
            }
        }

        // Return primitive values as is
        return value;
    }

    /**
     * Converts a string field value to the appropriate type based on field metadata
     * @param fieldName The name of the field
     * @param fieldValue The string value to convert
     * @returns The converted value with the appropriate type
     */
    private convertFieldValue(fieldName: string, fieldValueIn: string | number | boolean): FieldResult | undefined {
        // If fieldValue is already a number or boolean, we don't need to convert it from string
        if (typeof fieldValueIn === 'number' || typeof fieldValueIn === 'boolean') {
            return fieldValueIn;
        }

        // If fieldValue is a string "true" or "false", convert to boolean
        if (typeof fieldValueIn === 'string') {
            if (fieldValueIn.toLowerCase() === 'true') {
                return true;
            }
            if (fieldValueIn.toLowerCase() === 'false') {
                return false;
            }
        }

        // coerce fieldvValue to a string
        const fieldValue = typeof fieldValueIn !== 'string' ? String(fieldValueIn) : fieldValueIn;

        // Special handling for text field - convert to proper RichTextField format
        if (fieldName === 'text') {
            try {
                // Check if it's already a valid JSON RichTextField
                JSON.parse(fieldValue);
                return fieldValue;
            } catch {
                // It's a plain text string, so convert it to RichTextField format
                const rtf = {
                    doc: {
                        type: 'doc',
                        content: [
                            {
                                type: 'paragraph',
                                content: [
                                    {
                                        type: 'text',
                                        text: fieldValue,
                                    },
                                ],
                            },
                        ],
                    },
                };
                return JSON.stringify(rtf);
            }
        }

        // Get field metadata
        const normalizedFieldName = fieldName.startsWith('_') ? fieldName : `_${fieldName}`;
        const strippedFieldName = fieldName.startsWith('_') ? fieldName.substring(1) : fieldName;

        // Check both versions of the field name in metadata
        const fieldMeta = this.fieldMetadata[normalizedFieldName] || this.fieldMetadata[strippedFieldName];

        // Special handling for width and height without metadata
        if (!fieldMeta && (fieldName === '_width' || fieldName === '_height' || fieldName === 'width' || fieldName === 'height')) {
            const num = Number(fieldValue);
            return isNaN(num) ? fieldValue : num;
        }

        if (!fieldMeta) {
            // If no metadata found, just return the string value
            return fieldValue;
        }

        // Convert based on field type
        const fieldType = fieldMeta.type;

        if (fieldType === 'boolean') {
            // Convert to boolean
            return fieldValue.toLowerCase() === 'true';
        } else if (fieldType === 'number') {
            // Convert to number
            const num = Number(fieldValue);
            return isNaN(num) ? fieldValue : num;
        } else if (fieldType === 'date') {
            // Try to convert to date (stored as number timestamp)
            try {
                return new Date(fieldValue).getTime();
            } catch {
                return fieldValue;
            }
        } else if (fieldType.includes('list') || fieldType.includes('array')) {
            // Try to parse as JSON array
            try {
                return JSON.parse(fieldValue) as FieldResult; //  bcz: this needs to be typed properly.  Dash fields can't accept a generic 'objext'
            } catch {
                return fieldValue;
            }
        } else if (fieldType === 'json' || fieldType === 'object') {
            // Try to parse as JSON object
            try {
                return JSON.parse(fieldValue) as FieldResult; //  bcz: this needs to be typed properly.  Dash fields can't accept a generic 'objext'
            } catch {
                return fieldValue;
            }
        }

        // Default to string
        return fieldValue;
    }

    /**
     * Extracts all field metadata from DocumentOptions
     * @returns A structured object containing metadata about all available document fields
     */
    public getAllFieldMetadata() {
        // Start with our already populated fieldMetadata from the DocumentOptions class
        const result: Record<string, any> = {
            // bcz: CHANGE any to a proper type!
            fieldCount: Object.keys(this.fieldMetadata).length,
            fields: {},
            fieldsByType: {
                string: [],
                number: [],
                boolean: [],
                //doc: [],
                //list: [],
                //date: [],
                //enumeration: [],
                //other: [],
            },
            fieldNameMappings: {},
            commonFields: {
                appearance: [],
                position: [],
                size: [],
                content: [],
                behavior: [],
                layout: [],
            },
        };

        // Process each field in the metadata
        Object.entries(this.fieldMetadata).forEach(([fieldName, fieldInfo]) => {
            const strippedName = fieldName.startsWith('_') ? fieldName.substring(1) : fieldName;

            // Add to fieldNameMappings
            if (fieldName.startsWith('_')) {
                result.fieldNameMappings[strippedName] = fieldName;
            }

            // Create structured field metadata
            const fieldData: Record<string, any> = {
                // bcz: CHANGE any to a proper type!
                name: fieldName,
                displayName: strippedName,
                description: fieldInfo.description || '',
                type: fieldInfo.fieldType || 'unknown',
                possibleValues: fieldInfo.values || [],
            };

            // Add field to fields collection
            result.fields[fieldName] = fieldData;

            // Categorize by field type
            const type = fieldInfo.fieldType?.toLowerCase() || 'unknown';
            if (type === 'string') {
                result.fieldsByType.string.push(fieldName);
            } else if (type === 'number') {
                result.fieldsByType.number.push(fieldName);
            } else if (type === 'boolean') {
                result.fieldsByType.boolean.push(fieldName);
            } else if (type === 'doc') {
                //result.fieldsByType.doc.push(fieldName);
            } else if (type === 'list') {
                //result.fieldsByType.list.push(fieldName);
            } else if (type === 'date') {
                //result.fieldsByType.date.push(fieldName);
            } else if (type === 'enumeration') {
                //result.fieldsByType.enumeration.push(fieldName);
            } else {
                //result.fieldsByType.other.push(fieldName);
            }

            // Categorize by field purpose
            if (fieldName.includes('width') || fieldName.includes('height') || fieldName.includes('size')) {
                result.commonFields.size.push(fieldName);
            } else if (fieldName.includes('color') || fieldName.includes('background') || fieldName.includes('border')) {
                result.commonFields.appearance.push(fieldName);
            } else if (fieldName.includes('x') || fieldName.includes('y') || fieldName.includes('position') || fieldName.includes('pan')) {
                result.commonFields.position.push(fieldName);
            } else if (fieldName.includes('text') || fieldName.includes('title') || fieldName.includes('data')) {
                result.commonFields.content.push(fieldName);
            } else if (fieldName.includes('action') || fieldName.includes('click') || fieldName.includes('event')) {
                result.commonFields.behavior.push(fieldName);
            } else if (fieldName.includes('layout')) {
                result.commonFields.layout.push(fieldName);
            }
        });

        // Add special section for auto-sizing related fields
        result.autoSizingFields = {
            height: {
                autoHeightField: '_layout_autoHeight',
                heightField: '_height',
                displayName: 'height',
                usage: 'To manually set height, first set layout_autoHeight to false',
            },
            width: {
                autoWidthField: '_layout_autoWidth',
                widthField: '_width',
                displayName: 'width',
                usage: 'To manually set width, first set layout_autoWidth to false',
            },
        };

        // Add special section for text field format
        result.specialFields = {
            text: {
                name: 'text',
                description: 'Document text content',
                format: 'RichTextField',
                note: 'When setting text, provide plain text - it will be automatically converted to the correct format',
                example: 'For setting: "Hello world" (plain text); For getting: Will be converted to plaintext for display',
            },
        };

        return result;
    }

    /**
     * Edits a specific field on a document
     * @param docId The ID of the document to edit
     * @param fieldName The name of the field to edit
     * @param fieldValue The new value for the field (string, number, or boolean)
     * @returns Object with success status, message, and additional information
     */
    public editDocumentField(
        docId: string,
        fieldName: string,
        fieldValue: string | number | boolean
    ): {
        success: boolean;
        message: string;
        fieldName?: string;
        originalFieldName?: string;
        newValue?: string | number | boolean | object;
        warning?: string;
    } {
        // Normalize field name (handle with/without underscore)
        let normalizedFieldName = fieldName.startsWith('_') ? fieldName : fieldName;
        // const strippedFieldName = fieldName.startsWith('_') ? fieldName.substring(1) : fieldName;

        // Handle common field name aliases (width → _width, height → _height)
        // Many document fields use '_' prefix for layout properties
        if (fieldName === 'width') {
            normalizedFieldName = '_width';
        } else if (fieldName === 'height') {
            normalizedFieldName = '_height';
        }

        // Get the documents
        const doc = this.documentsById.get(docId);
        if (!doc) {
            return { success: false, message: `Document with ID ${docId} not found` };
        }

        const { layoutDoc, dataDoc } = this.documentsById.get(docId) ?? { layoutDoc: null, dataDoc: null };

        if (!layoutDoc && !dataDoc) {
            return { success: false, message: `Could not find layout or data document for document with ID ${docId}` };
        }

        try {
            // Convert the field value to the appropriate type based on field metadata
            const convertedValue = this.convertFieldValue(normalizedFieldName, fieldValue);

            let targetDoc: Doc | undefined;
            let targetLocation: string;

            // First, check if field exists on layout document using Doc.Get
            if (layoutDoc) {
                const fieldExistsOnLayout = Doc.Get(layoutDoc, normalizedFieldName, true) !== undefined;

                // If it exists on layout document, update it there
                if (fieldExistsOnLayout) {
                    targetDoc = layoutDoc;
                    targetLocation = 'layout';
                }
                // If it has an underscore prefix, it's likely a layout property even if not yet set
                else if (normalizedFieldName.startsWith('_')) {
                    targetDoc = layoutDoc;
                    targetLocation = 'layout';
                }
                // Otherwise, look for or create on data document
                else if (dataDoc) {
                    targetDoc = dataDoc;
                    targetLocation = 'data';
                }
                // If no data document available, default to layout
                else {
                    targetDoc = layoutDoc;
                    targetLocation = 'layout';
                }
            }
            // If no layout document, use data document
            else if (dataDoc) {
                targetDoc = dataDoc;
                targetLocation = 'data';
            } else {
                return { success: false, message: `No valid document found for editing` };
            }

            if (!targetDoc) {
                return { success: false, message: `Target document not available` };
            }

            // Set the field value on the target document
            targetDoc[normalizedFieldName] = convertedValue; //  bcz: converteValue needs to be typed properly.  Dash fields can't accept a generic 'objext'

            return {
                success: true,
                message: `Successfully updated field '${normalizedFieldName}' on ${targetLocation} document (ID: ${docId})`,
                fieldName: normalizedFieldName,
                originalFieldName: fieldName,
                newValue: convertedValue,
            };
        } catch (error) {
            console.error('Error editing document field:', error);
            return {
                success: false,
                message: `Error updating field: ${error instanceof Error ? error.message : String(error)}`,
            };
        }
    }
    /**
     * Gets metadata for a specific document or all documents
     * @param documentId Optional ID of a specific document to get metadata for
     * @returns Document metadata or metadata for all documents
     */
    public getDocumentMetadata(documentId?: string) {
        if (documentId) {
            console.log(`Returning document metadata for docID, ${documentId}:`, this.extractDocumentMetadata(documentId));
            return this.extractDocumentMetadata(documentId);
        } else {
            // Get metadata for all documents
            const documentsMetadata: Record<string, Record<string, any>> = {}; // bcz: CHANGE any to a proper type!
            for (const docid of this.documentsById.keys()) {
                const metadata = this.extractDocumentMetadata(docid);
                if (metadata) {
                    documentsMetadata[docid] = metadata;
                } else {
                    console.warn(`No metadata found for document with ID: ${docid}`);
                }
            }
            return {
                documentCount: this.documentsById.size,
                documents: documentsMetadata,
                //fieldDefinitions: this.fieldMetadata, // TODO: remove this, if fieldDefinitions are not needed.
            };
        }
    }

    /**
     * Adds links between documents based on their IDs
     * @param docIds Array of document IDs to link
     * @param relationship Optional relationship type for the links
     * @returns Array of created link documents
     */
    public addLinks(docIds: string[]): Doc[] {
        const createdLinks: Doc[] = [];
        // Use string keys for Set instead of arrays which don't work as expected as keys
        const alreadyLinked = new Set<string>();

        // Iterate over the document IDs and add links
        docIds.forEach(docId1 => {
            const doc1 = this.documentsById.get(docId1);
            docIds.forEach(docId2 => {
                if (docId1 === docId2) return; // Skip self-linking

                // Create a consistent key regardless of document order
                const linkKey = [docId1, docId2].sort().join('_');
                if (alreadyLinked.has(linkKey)) return;

                const doc2 = this.documentsById.get(docId2);
                if (doc1?.layoutDoc && doc2?.layoutDoc) {
                    try {
                        // Create a link document between doc1 and doc2
                        const linkDoc = Docs.Create.LinkDocument(doc1.layoutDoc, doc2.layoutDoc);

                        // Set a default color if relationship doesn't specify one
                        if (!linkDoc.color) {
                            linkDoc.color = 'lightBlue'; // Default blue color
                        }

                        // Ensure link is visible by setting essential properties
                        linkDoc.link_visible = true;
                        linkDoc.link_enabled = true;
                        linkDoc.link_autoMove = true;
                        linkDoc.link_showDirected = true;

                        // Set the embedContainer to ensure visibility
                        // This is shown in the image as a key difference between visible/non-visible links
                        if (this.chatBoxDocument && this.chatBoxDocument.parent && typeof this.chatBoxDocument.parent === 'object' && 'title' in this.chatBoxDocument.parent) {
                            linkDoc.embedContainer = String(this.chatBoxDocument.parent.title);
                        } else if (doc1.layoutDoc.parent && typeof doc1.layoutDoc.parent === 'object' && 'title' in doc1.layoutDoc.parent) {
                            linkDoc.embedContainer = String(doc1.layoutDoc.parent.title);
                        } else {
                            // Default to a tab name if we can't find one
                            linkDoc.embedContainer = 'Untitled Tab 1';
                        }

                        // Add the link to the document system
                        LinkManager.Instance.addLink(linkDoc);

                        const ancestor = DocumentView.linkCommonAncestor(linkDoc);
                        ancestor?.ComponentView?.addDocument?.(linkDoc);
                        // Add to user document list to make it visible in the UI
                        Doc.AddDocToList(Doc.UserDoc(), 'links', linkDoc);

                        // Create a visual link for display
                        if (this.chatBoxDocument) {
                            // Make sure the docs are visible in the UI
                            this.chatBox._props.addDocument?.(doc1.layoutDoc);
                            this.chatBox._props.addDocument?.(doc2.layoutDoc);

                            // Use DocumentManager to ensure documents are visible
                            DocumentManager.Instance.showDocument(doc1.layoutDoc, { willZoomCentered: false });
                            DocumentManager.Instance.showDocument(doc2.layoutDoc, { willZoomCentered: false });
                        }

                        createdLinks.push(linkDoc);
                        alreadyLinked.add(linkKey);
                    } catch (error) {
                        console.error('Error creating link between documents:', error);
                    }
                }
            });
        });

        // Force update of the UI to show new links
        setTimeout(() => {
            try {
                // Update server cache to ensure links are persisted
                UPDATE_SERVER_CACHE && typeof UPDATE_SERVER_CACHE === 'function' && UPDATE_SERVER_CACHE();
            } catch (e) {
                console.warn('Could not update server cache after creating links:', e);
            }
        }, 100);

        return createdLinks;
    }
    /**
     * Helper method to validate a document type and ensure it's a valid supportedDocType
     * @param docType The document type to validate
     * @returns True if the document type is valid, false otherwise
     */
    private isValidDocType(docType: string): boolean {
        return Object.values(supportedDocTypes).includes(docType as supportedDocTypes);
    }
    /**
     * Creates a document in the dashboard and returns its ID.
     * This is a public API used by tools like SearchTool.
     *
     * @param docType The type of document to create
     * @param data The data for the document
     * @param options Optional configuration options
     * @returns The ID of the created document
     */

    public async createDocInDash(docType: string, data: string, options?: DocumentOptions): Promise<string> {
        // Validate doc_type
        if (!this.isValidDocType(docType)) {
            throw new Error(`Invalid document type: ${docType}`);
        }

        try {
            // Create simple document with just title and data
            const simpleDoc: parsedDoc = {
                doc_type: docType,
                title: options?.title ?? `Untitled Document ${this.documentsById.size + 1}`,
                data: data,
                x: options?.x ?? 0,
                y: options?.y ?? 0,
                _width: 300,
                _height: 300,
                _layout_fitWidth: false,
                _layout_autoHeight: true,
            };

            // Additional handling for web documents
            if (docType === 'web') {
                // For web documents, don't sanitize the URL here
                // Instead, set properties to handle content safely when loaded
                simpleDoc._disable_resource_loading = true;
                simpleDoc._sandbox_iframe = true;
                simpleDoc.data_useCors = true;

                // Specify a more permissive sandbox to allow content to render properly
                // but still maintain security
                simpleDoc._iframe_sandbox = 'allow-same-origin allow-scripts allow-popups allow-forms';
            }

            // Use the chatBox's createDocInDash method to create the document
            if (!this.chatBox) {
                throw new Error('ChatBox instance not available for creating document');
            }

            const doc = this.chatBox.whichDoc(simpleDoc, false);
            if (doc) {
                // Use MobX runInAction to properly modify observable state
                runInAction(() => {
                    if (this.chatBoxDocument && doc) {
                        // Create link and add it to the document system
                        const linkDoc = Docs.Create.LinkDocument(this.chatBoxDocument, doc);
                        LinkManager.Instance.addLink(linkDoc);
                        if (doc.type !== 'web') {
                            // Add document to view
                            this.chatBox._props.addDocument?.(doc);

                            // Show document - defer actual display to prevent immediate resource loading
                            setTimeout(() => {
                                DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {});
                            }, 100);
                        }
                    }
                });

                const id = await this.processDocument(doc);
                return id;
            } else {
                throw new Error(`Error creating document. Created document not found.`);
            }
        } catch (error) {
            throw new Error(`Error creating document: ${error}`);
        }
    }

    /**
     * Sanitizes web content to prevent errors with external resources
     * @param content The web content to sanitize
     * @returns Sanitized content
     */
    private sanitizeWebContent(content: string): string {
        if (!content) return content;

        try {
            // Replace problematic resource references that might cause errors
            const sanitized = content
                // Remove preload links that might cause errors
                .replace(/<link[^>]*rel=["']preload["'][^>]*>/gi, '')
                // Remove map file references
                .replace(/\/\/# sourceMappingURL=.*\.map/gi, '')
                // Remove external CSS map files references
                .replace(/\/\*# sourceMappingURL=.*\.css\.map.*\*\//gi, '')
                // Add sandbox to iframes
                .replace(/<iframe/gi, '<iframe sandbox="allow-same-origin" loading="lazy"')
                // Prevent automatic resource loading for images
                .replace(/<img/gi, '<img loading="lazy"')
                // Prevent automatic resource loading for scripts
                .replace(/<script/gi, '<script type="text/disabled"')
                // Handle invalid URIs by converting relative URLs to absolute ones
                .replace(/href=["'](\/[^"']+)["']/gi, (match, p1) => {
                    // Only handle relative URLs starting with /
                    if (p1.startsWith('/')) {
                        return `href="#disabled-link"`;
                    }
                    return match;
                })
                // Prevent automatic loading of CSS
                .replace(/<link[^>]*rel=["']stylesheet["'][^>]*href=["']([^"']+)["']/gi, (match, href) => `<link rel="prefetch" data-original-href="${href}" />`);

            // Wrap the content in a sandboxed container
            return `
            <div class="sandboxed-web-content">
                <style>
                /* Override styles to prevent external resource loading */
                @font-face { font-family: 'disabled'; src: local('Arial'); }
                * { font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif !important; }
                img, iframe, frame, embed, object { max-width: 100%; }
                </style>
                ${sanitized}
            </div>`;
        } catch (e) {
            console.warn('Error sanitizing web content:', e);
            // Fall back to a safe container with the content as text
            return `
            <div class="sandboxed-web-content">
                <p>Content could not be safely displayed. Raw content:</p>
                <pre>${content.replace(/</g, '&lt;').replace(/>/g, '&gt;')}</pre>
            </div>`;
        }
    }

    public has(docId: string) {
        return this.documentsById.has(docId);
    }

    /**
     * Returns a list of all document IDs in the manager.
     * @returns An array of document IDs (strings).
     */
    @computed
    public get listDocs(): string {
        const xmlDocs = Array.from(this.documentsById.entries()).map(([id, agentDoc]) => {
            return `<document>
  <id>${id}</id>
  <title>${this.escapeXml(StrCast(agentDoc.layoutDoc.title))}</title>
  <type>${this.escapeXml(StrCast(agentDoc.layoutDoc.type))}</type>
  <summary>${this.escapeXml(StrCast(agentDoc.layoutDoc.summary))}</summary>
</document>`;
        });

        return xmlDocs.join('\n');
    }

    private escapeXml(str: string): string {
        return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&apos;');
    }

    @computed
    public get docIds(): string[] {
        return Array.from(this.documentsById.keys());
    }

    /**
     * Gets a document by its ID
     * @param docId The ID of the document to retrieve
     * @returns The document if found, undefined otherwise
     */
    public getDocument(docId: string): Doc | undefined {
        const docInfo = this.documentsById.get(docId);
        return docInfo?.layoutDoc;
    }

    public getDataDocument(docId: string): Doc | undefined {
        const docInfo = this.documentsById.get(docId);
        return docInfo?.dataDoc;
    }
    /**
     * Adds simplified chunks to a document for citation handling
     * @param doc The document to add simplified chunks to
     * @param chunks Array of full RAG chunks to simplify
     * @param docType The type of document (e.g., 'pdf', 'video', 'audio', etc.)
     * @returns The updated document with simplified chunks
     */
    @action
    public addSimplifiedChunks(simplifiedChunks: SimplifiedChunk[]) {
        simplifiedChunks.forEach(chunk => {
            this.simplifiedChunks.set(chunk.chunkId, chunk);
        });
    }

    public getSimplifiedChunks(chunks: RAGChunk[], docType: string): SimplifiedChunk[] {
        console.log('chunks', chunks, 'simplifiedChunks', this.simplifiedChunks);
        const simplifiedChunks: SimplifiedChunk[] = [];
        // Create array of simplified chunks based on document type
        for (const chunk of chunks) {
            // Common properties across all chunk types
            const baseChunk: SimplifiedChunk = {
                chunkId: chunk.id,
                //text: chunk.metadata.text,
                doc_id: chunk.metadata.doc_id,
                chunkType: chunk.metadata.type || CHUNK_TYPE.TEXT,
            };

            // Add type-specific properties
            if (docType === 'video' || docType === 'audio') {
                simplifiedChunks.push({
                    ...baseChunk,
                    start_time: chunk.metadata.start_time,
                    end_time: chunk.metadata.end_time,
                    indexes: chunk.metadata.indexes,
                    chunkType: docType === 'video' ? CHUNK_TYPE.VIDEO : CHUNK_TYPE.AUDIO,
                } as SimplifiedChunk);
            } else if (docType === 'pdf') {
                simplifiedChunks.push({
                    ...baseChunk,
                    startPage: chunk.metadata.start_page,
                    endPage: chunk.metadata.end_page,
                    location: chunk.metadata.location,
                } as SimplifiedChunk);
            } else if (docType === 'csv' && 'row_start' in chunk.metadata && 'row_end' in chunk.metadata && 'col_start' in chunk.metadata && 'col_end' in chunk.metadata) {
                simplifiedChunks.push({
                    ...baseChunk,
                    rowStart: chunk.metadata.row_start,
                    rowEnd: chunk.metadata.row_end,
                    colStart: chunk.metadata.col_start,
                    colEnd: chunk.metadata.col_end,
                } as SimplifiedChunk);
            } else {
                // Default for other document types
                simplifiedChunks.push(baseChunk as SimplifiedChunk);
            }
        }
        return simplifiedChunks;
    }

    /**
     * Gets a specific simplified chunk by ID
     * @param doc The document containing chunks
     * @param chunkId The ID of the chunk to retrieve
     * @returns The simplified chunk if found, undefined otherwise
     */
    @action
    public getSimplifiedChunkById(chunkId: string) {
        return { foundChunk: this.simplifiedChunks.get(chunkId), doc: this.getDocument(this.simplifiedChunks.get(chunkId)?.doc_id || chunkId), dataDoc: this.getDataDocument(this.simplifiedChunks.get(chunkId)?.doc_id || chunkId) };
    }

    public getChunkIdsFromDocIds(docIds: string[]): string[] {
        return docIds
            .map(docId => {
                for (const chunk of this.simplifiedChunks.values()) {
                    if (chunk.doc_id === docId) {
                        return chunk.chunkId;
                    }
                }
            })
            .filter(chunkId => chunkId !== undefined) as string[];
    }

    /**
     * Gets the original segments from a media document
     * @param doc The document containing original media segments
     * @returns Array of media segments or empty array if none exist
     */
    public getOriginalSegments(doc: Doc): { text: string; index: string; start: number }[] {
        if (!doc || !doc.original_segments) {
            return [];
        }

        try {
            return JSON.parse(StrCast(doc.original_segments)) || [];
        } catch (e) {
            console.error('Error parsing original segments:', e);
            return [];
        }
    }
}