diff options
-rw-r--r-- | src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx | 2 | ||||
-rw-r--r-- | src/client/views/nodes/chatbot/utils/AgentDocumentManager.ts | 65 |
2 files changed, 46 insertions, 21 deletions
diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx index 490739be6..d45c6c936 100644 --- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx +++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx @@ -1232,5 +1232,5 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { */ Docs.Prototypes.TemplateMap.set(DocumentType.CHAT, { layout: { view: ChatBox, dataField: 'data' }, - options: { acl: '', _layout_fitWidth: true, chat: '', chat_history: '', chat_thread_id: '', chat_assistant_id: '', chat_vector_store_id: '' }, + options: { acl: '', _layout_fitWidth: true, chat: '', chat_history: '', chat_thread_id: '', chat_assistant_id: '', chat_vector_store_id: '', _forceActive: true }, }); diff --git a/src/client/views/nodes/chatbot/utils/AgentDocumentManager.ts b/src/client/views/nodes/chatbot/utils/AgentDocumentManager.ts index 01815baec..a27220898 100644 --- a/src/client/views/nodes/chatbot/utils/AgentDocumentManager.ts +++ b/src/client/views/nodes/chatbot/utils/AgentDocumentManager.ts @@ -1,21 +1,19 @@ -import { ChatBox } from '../chatboxcomponents/ChatBox'; -import { Doc, FieldType, Opt } from '../../../../../fields/Doc'; -import { DocData } from '../../../../../fields/DocSymbols'; -import { Observation } from '../types/types'; -import { ParametersType, ToolInfo, Parameter } from '../types/tool_types'; -import { BaseTool } from '../tools/BaseTool'; -import { Docs, DocumentOptions } from '../../../../documents/Documents'; -import { CollectionFreeFormDocumentView } from '../../CollectionFreeFormDocumentView'; +import { action, makeObservable, observable, ObservableMap, reaction, runInAction } from 'mobx'; +import { observer } from 'mobx-react'; import { v4 as uuidv4 } from 'uuid'; -import { LinkManager, UPDATE_SERVER_CACHE } from '../../../../util/LinkManager'; +import { Doc, 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 { supportedDocTypes } from '../types/tool_types'; -import { parsedDoc } from '../chatboxcomponents/ChatBox'; -import { faThumbTackSlash } from '@fortawesome/free-solid-svg-icons'; +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 { RAGChunk, CHUNK_TYPE } from '../types/types'; -import { runInAction } from 'mobx'; +import { ChatBox, parsedDoc } from '../chatboxcomponents/ChatBox'; +import { supportedDocTypes } from '../types/tool_types'; +import { CHUNK_TYPE, RAGChunk } from '../types/types'; /** * Interface representing a document in the freeform view @@ -29,7 +27,7 @@ interface AgentDocument { * Class to manage documents in a freeform view */ export class AgentDocumentManager { - private documentsById: Map<string, AgentDocument>; + @observable private documentsById: ObservableMap<string, AgentDocument>; private chatBox: ChatBox; private chatBoxDocument: Doc | null = null; private fieldMetadata: Record<string, any> = {}; @@ -40,9 +38,34 @@ export class AgentDocumentManager { * @param templateDocument The document that serves as a template for new documents */ constructor(chatBox: ChatBox) { - this.documentsById = new Map<string, AgentDocument>(); + makeObservable(this); + const agentDoc = DocCast(chatBox.Document.agentDocument) ?? new Doc(); + agentDoc.title = chatBox.Document.title + '_agentDocument'; + chatBox.Document.agentDocument = agentDoc; + 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 } + ); this.processDocument(this.chatBoxDocument); this.initializeFieldMetadata(); } @@ -171,6 +194,7 @@ export class AgentDocumentManager { * Process a document by ensuring it has an ID and adding it to the appropriate collections * @param doc The document to process */ + @action public processDocument(doc: Doc): string { // Ensure document has a persistent ID const docId = this.ensureDocumentId(doc); @@ -997,6 +1021,7 @@ export class AgentDocumentManager { * @param customId The custom ID to assign to the document * @returns The customId that was assigned */ + @action public addCustomId(doc: Doc, customId: string): string { if (!doc) { console.error('Cannot add null document with custom ID'); @@ -1030,6 +1055,7 @@ export class AgentDocumentManager { * @param docId The parent document ID * @param chunkIds Array of chunk IDs associated with this document */ + @action public registerChunkIds(docId: string, chunkIds: string[]): void { // Get the document if it exists const docInfo = this.documentsById.get(docId); @@ -1048,9 +1074,8 @@ export class AgentDocumentManager { const updatedIds = [...new Set([...existingIds, ...chunkIds])]; // Remove duplicates doc.chunk_ids = JSON.stringify(updatedIds); } - - // Ensure each chunk ID can be linked back to its parent document - chunkIds.forEach(chunkId => { + for (const chunkId of chunkIds) { + // Ensure each chunk ID can be linked back to its parent document // Store a mapping from chunk ID to parent document ID // This allows us to easily find a document by any of its chunk IDs if (!this.documentsById.has(chunkId)) { @@ -1059,7 +1084,7 @@ export class AgentDocumentManager { dataDoc: docInfo.dataDoc, }); } - }); + } } /** |