UNPKG

@hashgraphonline/conversational-agent

Version:

Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera. https://hol.org

131 lines (126 loc) 4.41 kB
import { Logger } from "@hashgraphonline/standards-sdk"; class AttachmentProcessor { constructor() { this.logger = new Logger({ module: "AttachmentProcessor" }); } /** * Process attachments and create content references */ async processAttachments(content, attachments, contentStoreManager) { if (attachments.length === 0) { return content; } this.logger.info("Processing attachments with content reference system:", { attachmentCount: attachments.length, totalSize: attachments.reduce((sum, att) => sum + att.size, 0) }); if (contentStoreManager && contentStoreManager.isInitialized()) { return this.processWithContentStore(content, attachments, contentStoreManager); } else { this.logger.warn("Content storage not available, creating simple file references"); return this.processWithSimpleReferences(content, attachments); } } /** * Process attachments using content store manager */ async processWithContentStore(content, attachments, contentStoreManager) { const contentReferences = []; for (const attachment of attachments) { try { const base64Data = attachment.data.includes("base64,") ? attachment.data.split("base64,")[1] : attachment.data; const buffer = Buffer.from(base64Data, "base64"); const contentRef = await contentStoreManager.storeContentIfLarge( buffer, { mimeType: attachment.type, source: "user_upload", fileName: attachment.name, tags: ["attachment", "user_file"] } ); if (contentRef) { if (attachment.type.startsWith("image/")) { contentReferences.push( `[Image File: ${attachment.name}] (content-ref:${contentRef.referenceId})` ); } else { contentReferences.push( `[File: ${attachment.name}] (content-ref:${contentRef.referenceId})` ); } } else { contentReferences.push( this.createInlineReference(attachment, base64Data) ); } } catch (error) { this.logger.error("Failed to process attachment:", { fileName: attachment.name, error: error instanceof Error ? error.message : "Unknown error" }); contentReferences.push( `[File: ${attachment.name} - Error processing file: ${error instanceof Error ? error.message : "Unknown error"}]` ); } } const fileList = this.createFileList(attachments); return content ? `${content} Attached files: ${fileList} ${contentReferences.join("\n")}` : `Attached files: ${fileList} ${contentReferences.join("\n")}`; } /** * Process attachments with simple file references */ processWithSimpleReferences(content, attachments) { const fileReferences = attachments.map((attachment) => { const sizeStr = this.formatFileSize(attachment.size); if (attachment.type.startsWith("image/")) { return `📎 Image: ${attachment.name} (${sizeStr}, ${attachment.type})`; } else { return `📎 File: ${attachment.name} (${sizeStr}, ${attachment.type})`; } }); return content ? `${content} Attached files: ${fileReferences.join("\n")}` : `Attached files: ${fileReferences.join("\n")}`; } /** * Create inline reference for small files */ createInlineReference(attachment, base64Data) { if (attachment.size < 5e4) { if (attachment.type.startsWith("image/")) { return `![${attachment.name}](data:${attachment.type};base64,${base64Data})`; } else { return `[File: ${attachment.name} (${this.formatFileSize(attachment.size)})] Content: ${base64Data}`; } } else { return `[File: ${attachment.name} (${this.formatFileSize(attachment.size)}) - Content too large to include inline]`; } } /** * Create formatted file list */ createFileList(attachments) { return attachments.map((file) => { const sizeStr = this.formatFileSize(file.size); return `📎 ${file.name} (${sizeStr})`; }).join("\n"); } /** * Format file size for display */ formatFileSize(size) { return size >= 1024 * 1024 ? `${(size / (1024 * 1024)).toFixed(1)}MB` : `${(size / 1024).toFixed(1)}KB`; } } export { AttachmentProcessor }; //# sourceMappingURL=index31.js.map