@hashgraphonline/conversational-agent 0.1.204 → 0.1.206
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.
- package/dist/cjs/base-agent.d.ts +6 -0
- package/dist/cjs/context/ReferenceContextManager.d.ts +84 -0
- package/dist/cjs/context/ReferenceResponseProcessor.d.ts +76 -0
- package/dist/cjs/conversational-agent.d.ts +34 -0
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/langchain-agent.d.ts +1 -0
- package/dist/cjs/mcp/ContentProcessor.d.ts +37 -0
- package/dist/cjs/mcp/MCPClientManager.d.ts +19 -1
- package/dist/cjs/memory/ContentStorage.d.ts +205 -0
- package/dist/cjs/memory/MemoryWindow.d.ts +114 -0
- package/dist/cjs/memory/ReferenceIdGenerator.d.ts +45 -0
- package/dist/cjs/memory/SmartMemoryManager.d.ts +201 -0
- package/dist/cjs/memory/TokenCounter.d.ts +61 -0
- package/dist/cjs/memory/index.d.ts +7 -0
- package/dist/cjs/plugins/hbar-transfer/TransferHbarTool.d.ts +18 -0
- package/dist/cjs/services/ContentStoreManager.d.ts +54 -0
- package/dist/cjs/types/content-reference.d.ts +213 -0
- package/dist/cjs/types/index.d.ts +4 -0
- package/dist/esm/index12.js +15 -2
- package/dist/esm/index12.js.map +1 -1
- package/dist/esm/index14.js +119 -95
- package/dist/esm/index14.js.map +1 -1
- package/dist/esm/index15.js +159 -114
- package/dist/esm/index15.js.map +1 -1
- package/dist/esm/index16.js +122 -81
- package/dist/esm/index16.js.map +1 -1
- package/dist/esm/index17.js +236 -0
- package/dist/esm/index17.js.map +1 -0
- package/dist/esm/index18.js +95 -0
- package/dist/esm/index18.js.map +1 -0
- package/dist/esm/index19.js +663 -0
- package/dist/esm/index19.js.map +1 -0
- package/dist/esm/index2.js +3 -1
- package/dist/esm/index2.js.map +1 -1
- package/dist/esm/index20.js +233 -0
- package/dist/esm/index20.js.map +1 -0
- package/dist/esm/index21.js +182 -0
- package/dist/esm/index21.js.map +1 -0
- package/dist/esm/index22.js +126 -0
- package/dist/esm/index22.js.map +1 -0
- package/dist/esm/index23.js +68 -0
- package/dist/esm/index23.js.map +1 -0
- package/dist/esm/index24.js +38 -0
- package/dist/esm/index24.js.map +1 -0
- package/dist/esm/index6.js +143 -84
- package/dist/esm/index6.js.map +1 -1
- package/dist/esm/index7.js.map +1 -1
- package/dist/esm/index8.js +69 -5
- package/dist/esm/index8.js.map +1 -1
- package/dist/types/base-agent.d.ts +6 -0
- package/dist/types/context/ReferenceContextManager.d.ts +84 -0
- package/dist/types/context/ReferenceResponseProcessor.d.ts +76 -0
- package/dist/types/conversational-agent.d.ts +34 -0
- package/dist/types/langchain-agent.d.ts +1 -0
- package/dist/types/mcp/ContentProcessor.d.ts +37 -0
- package/dist/types/mcp/MCPClientManager.d.ts +19 -1
- package/dist/types/memory/ContentStorage.d.ts +205 -0
- package/dist/types/memory/MemoryWindow.d.ts +114 -0
- package/dist/types/memory/ReferenceIdGenerator.d.ts +45 -0
- package/dist/types/memory/SmartMemoryManager.d.ts +201 -0
- package/dist/types/memory/TokenCounter.d.ts +61 -0
- package/dist/types/memory/index.d.ts +7 -0
- package/dist/types/plugins/hbar-transfer/TransferHbarTool.d.ts +18 -0
- package/dist/types/services/ContentStoreManager.d.ts +54 -0
- package/dist/types/types/content-reference.d.ts +213 -0
- package/dist/types/types/index.d.ts +4 -0
- package/package.json +30 -26
- package/src/base-agent.ts +6 -0
- package/src/context/ReferenceContextManager.ts +345 -0
- package/src/context/ReferenceResponseProcessor.ts +296 -0
- package/src/conversational-agent.ts +166 -92
- package/src/langchain-agent.ts +89 -2
- package/src/mcp/ContentProcessor.ts +317 -0
- package/src/mcp/MCPClientManager.ts +61 -1
- package/src/mcp/adapters/langchain.ts +9 -4
- package/src/memory/ContentStorage.ts +954 -0
- package/src/memory/MemoryWindow.ts +247 -0
- package/src/memory/ReferenceIdGenerator.ts +84 -0
- package/src/memory/SmartMemoryManager.ts +323 -0
- package/src/memory/TokenCounter.ts +152 -0
- package/src/memory/index.ts +8 -0
- package/src/plugins/hbar-transfer/TransferHbarTool.ts +19 -1
- package/src/plugins/hcs-10/HCS10Plugin.ts +5 -4
- package/src/services/ContentStoreManager.ts +199 -0
- package/src/types/content-reference.ts +281 -0
- package/src/types/index.ts +6 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index19.js","sources":["../../src/memory/ContentStorage.ts"],"sourcesContent":["import type { BaseMessage } from '@langchain/core/messages';\nimport { ReferenceIdGenerator } from './ReferenceIdGenerator';\nimport {\n ReferenceId,\n ContentReference,\n ContentMetadata,\n ReferenceResolutionResult,\n ContentReferenceConfig,\n ContentReferenceStore,\n ContentReferenceStats,\n ContentReferenceError,\n ContentType,\n ContentSource,\n ReferenceLifecycleState,\n DEFAULT_CONTENT_REFERENCE_CONFIG\n} from '../types/content-reference';\n\n/**\n * Stored message with metadata\n */\ninterface StoredMessage {\n message: BaseMessage;\n storedAt: Date;\n id: string;\n}\n\n/**\n * Search options for message queries\n */\ninterface SearchOptions {\n /** Whether to perform case-sensitive search */\n caseSensitive?: boolean;\n /** Maximum number of results to return */\n limit?: number;\n /** Whether to use regex pattern matching */\n useRegex?: boolean;\n}\n\n/**\n * Result of storing messages\n */\ninterface StoreResult {\n /** Number of messages successfully stored */\n stored: number;\n /** Number of old messages dropped to make room */\n dropped: number;\n}\n\n/**\n * Storage statistics\n */\nexport interface StorageStats {\n /** Total number of messages currently stored */\n totalMessages: number;\n /** Maximum storage capacity */\n maxStorageLimit: number;\n /** Percentage of storage used */\n usagePercentage: number;\n /** Timestamp of oldest message */\n oldestMessageTime: Date | undefined;\n /** Timestamp of newest message */\n newestMessageTime: Date | undefined;\n}\n\n/**\n * Stored content with reference metadata\n */\ninterface StoredContent {\n /** The actual content buffer */\n content: Buffer;\n \n /** Complete metadata */\n metadata: ContentMetadata;\n \n /** Current lifecycle state */\n state: ReferenceLifecycleState;\n \n /** When this reference expires (if applicable) */\n expiresAt?: Date;\n}\n\n/**\n * Content storage for managing pruned conversation messages and large content references\n * Provides searchable storage with time-based querying and automatic cleanup.\n * \n * Extended to support reference-based storage for large content to optimize context window usage.\n */\nexport class ContentStorage implements ContentReferenceStore {\n private messages: StoredMessage[] = [];\n private maxStorage: number;\n private idCounter: number = 0;\n\n // Reference-based content storage\n private contentStore: Map<ReferenceId, StoredContent> = new Map();\n private referenceConfig: ContentReferenceConfig;\n private cleanupTimer?: NodeJS.Timeout;\n private referenceStats: Omit<ContentReferenceStats, 'performanceMetrics'> & {\n performanceMetrics: ContentReferenceStats['performanceMetrics'] & {\n creationTimes: number[];\n resolutionTimes: number[];\n cleanupTimes: number[];\n };\n };\n\n // Default storage limit for messages\n public static readonly DEFAULT_MAX_STORAGE = 1000;\n\n constructor(\n maxStorage: number = ContentStorage.DEFAULT_MAX_STORAGE,\n referenceConfig?: Partial<ContentReferenceConfig>\n ) {\n this.maxStorage = maxStorage;\n \n // Initialize reference-based storage\n this.referenceConfig = { ...DEFAULT_CONTENT_REFERENCE_CONFIG, ...referenceConfig };\n this.referenceStats = {\n activeReferences: 0,\n totalStorageBytes: 0,\n recentlyCleanedUp: 0,\n totalResolutions: 0,\n failedResolutions: 0,\n averageContentSize: 0,\n storageUtilization: 0,\n performanceMetrics: {\n averageCreationTimeMs: 0,\n averageResolutionTimeMs: 0,\n averageCleanupTimeMs: 0,\n creationTimes: [],\n resolutionTimes: [],\n cleanupTimes: []\n }\n };\n \n // Start cleanup timer if enabled\n if (this.referenceConfig.enableAutoCleanup) {\n this.startReferenceCleanupTimer();\n }\n }\n\n /**\n * Store messages in the content storage\n * Automatically drops oldest messages if storage limit is exceeded\n * @param messages - Messages to store\n * @returns Result indicating how many messages were stored and dropped\n */\n storeMessages(messages: BaseMessage[]): StoreResult {\n if (messages.length === 0) {\n return { stored: 0, dropped: 0 };\n }\n\n const now = new Date();\n let dropped = 0;\n\n // Convert messages to stored format\n const storedMessages: StoredMessage[] = messages.map(message => ({\n message,\n storedAt: now,\n id: this.generateId()\n }));\n\n // Add new messages\n this.messages.push(...storedMessages);\n\n // Remove oldest messages if we exceed the limit\n while (this.messages.length > this.maxStorage) {\n this.messages.shift();\n dropped++;\n }\n\n return {\n stored: storedMessages.length,\n dropped\n };\n }\n\n /**\n * Get the most recent messages from storage\n * @param count - Number of recent messages to retrieve\n * @returns Array of recent messages in chronological order\n */\n getRecentMessages(count: number): BaseMessage[] {\n if (count <= 0 || this.messages.length === 0) {\n return [];\n }\n\n const startIndex = Math.max(0, this.messages.length - count);\n return this.messages\n .slice(startIndex)\n .map(stored => stored.message);\n }\n\n /**\n * Search for messages containing specific text or patterns\n * @param query - Search term or regex pattern\n * @param options - Search configuration options\n * @returns Array of matching messages\n */\n searchMessages(query: string, options: SearchOptions = {}): BaseMessage[] {\n if (!query || this.messages.length === 0) {\n return [];\n }\n\n const {\n caseSensitive = false,\n limit,\n useRegex = false\n } = options;\n\n let matches: BaseMessage[] = [];\n\n if (useRegex) {\n try {\n const regex = new RegExp(query, caseSensitive ? 'g' : 'gi');\n matches = this.messages\n .filter(stored => regex.test(stored.message.content as string))\n .map(stored => stored.message);\n } catch (error) {\n console.warn('Invalid regex pattern:', query, error);\n return [];\n }\n } else {\n const searchTerm = caseSensitive ? query : query.toLowerCase();\n matches = this.messages\n .filter(stored => {\n const content = stored.message.content as string;\n const searchContent = caseSensitive ? content : content.toLowerCase();\n return searchContent.includes(searchTerm);\n })\n .map(stored => stored.message);\n }\n\n return limit ? matches.slice(0, limit) : matches;\n }\n\n /**\n * Get messages from a specific time range\n * @param startTime - Start of time range (inclusive)\n * @param endTime - End of time range (inclusive)\n * @returns Array of messages within the time range\n */\n getMessagesFromTimeRange(startTime: Date, endTime: Date): BaseMessage[] {\n if (startTime > endTime || this.messages.length === 0) {\n return [];\n }\n\n return this.messages\n .filter(stored => \n stored.storedAt >= startTime && stored.storedAt <= endTime\n )\n .map(stored => stored.message);\n }\n\n /**\n * Get storage statistics and usage information\n * @returns Current storage statistics\n */\n getStorageStats(): StorageStats {\n const totalMessages = this.messages.length;\n const usagePercentage = totalMessages > 0 \n ? Math.round((totalMessages / this.maxStorage) * 100)\n : 0;\n\n let oldestMessageTime: Date | undefined;\n let newestMessageTime: Date | undefined;\n\n if (totalMessages > 0) {\n oldestMessageTime = this.messages[0].storedAt;\n newestMessageTime = this.messages[totalMessages - 1].storedAt;\n }\n\n return {\n totalMessages,\n maxStorageLimit: this.maxStorage,\n usagePercentage,\n oldestMessageTime,\n newestMessageTime\n };\n }\n\n /**\n * Clear all stored messages\n */\n clear(): void {\n this.messages = [];\n this.idCounter = 0;\n }\n\n /**\n * Get total number of stored messages\n * @returns Number of messages currently in storage\n */\n getTotalStoredMessages(): number {\n return this.messages.length;\n }\n\n /**\n * Update the maximum storage limit\n * @param newLimit - New maximum storage limit\n */\n updateStorageLimit(newLimit: number): void {\n if (newLimit <= 0) {\n throw new Error('Storage limit must be greater than 0');\n }\n\n this.maxStorage = newLimit;\n\n // Prune messages if the new limit is smaller\n while (this.messages.length > this.maxStorage) {\n this.messages.shift();\n }\n }\n\n /**\n * Get messages by message type\n * @param messageType - Type of messages to retrieve ('human', 'ai', 'system', etc.)\n * @param limit - Maximum number of messages to return\n * @returns Array of messages of the specified type\n */\n getMessagesByType(messageType: string, limit?: number): BaseMessage[] {\n const filtered = this.messages\n .filter(stored => stored.message._getType() === messageType)\n .map(stored => stored.message);\n\n return limit ? filtered.slice(0, limit) : filtered;\n }\n\n /**\n * Get the current storage configuration\n * @returns Storage configuration object\n */\n getConfig() {\n return {\n maxStorage: this.maxStorage,\n currentUsage: this.messages.length,\n utilizationPercentage: (this.messages.length / this.maxStorage) * 100\n };\n }\n\n /**\n * Generate a unique ID for stored messages\n * @returns Unique string identifier\n */\n private generateId(): string {\n return `msg_${++this.idCounter}_${Date.now()}`;\n }\n\n /**\n * Get messages stored within the last N minutes\n * @param minutes - Number of minutes to look back\n * @returns Array of messages from the last N minutes\n */\n getRecentMessagesByTime(minutes: number): BaseMessage[] {\n if (minutes <= 0 || this.messages.length === 0) {\n return [];\n }\n\n const cutoffTime = new Date(Date.now() - (minutes * 60 * 1000));\n \n return this.messages\n .filter(stored => stored.storedAt >= cutoffTime)\n .map(stored => stored.message);\n }\n\n /**\n * Export messages to a JSON-serializable format\n * @returns Serializable representation of stored messages\n */\n exportMessages() {\n return this.messages.map(stored => ({\n content: stored.message.content,\n type: stored.message._getType(),\n storedAt: stored.storedAt.toISOString(),\n id: stored.id\n }));\n }\n\n // ========== Reference-Based Content Storage Methods ==========\n\n /**\n * Determine if content should be stored as a reference based on size\n */\n shouldUseReference(content: Buffer | string): boolean {\n const size = Buffer.isBuffer(content) ? content.length : Buffer.byteLength(content, 'utf8');\n return size > this.referenceConfig.sizeThresholdBytes;\n }\n\n /**\n * Store content and return a reference if it exceeds the size threshold\n * Otherwise returns null to indicate direct content should be used\n */\n async storeContentIfLarge(\n content: Buffer | string,\n metadata: {\n contentType?: ContentType;\n mimeType?: string;\n source: ContentSource;\n mcpToolName?: string;\n fileName?: string;\n tags?: string[];\n customMetadata?: Record<string, unknown>;\n }\n ): Promise<ContentReference | null> {\n const buffer = Buffer.isBuffer(content) ? content : Buffer.from(content, 'utf8');\n \n if (!this.shouldUseReference(buffer)) {\n return null;\n }\n \n const storeMetadata: Omit<ContentMetadata, 'createdAt' | 'lastAccessedAt' | 'accessCount'> = {\n contentType: metadata.contentType || this.detectContentType(buffer, metadata.mimeType),\n sizeBytes: buffer.length,\n source: metadata.source,\n tags: []\n };\n \n if (metadata.mimeType !== undefined) {\n storeMetadata.mimeType = metadata.mimeType;\n }\n if (metadata.mcpToolName !== undefined) {\n storeMetadata.mcpToolName = metadata.mcpToolName;\n }\n if (metadata.fileName !== undefined) {\n storeMetadata.fileName = metadata.fileName;\n }\n if (metadata.tags !== undefined) {\n storeMetadata.tags = metadata.tags;\n }\n if (metadata.customMetadata !== undefined) {\n storeMetadata.customMetadata = metadata.customMetadata;\n }\n \n return await this.storeContent(buffer, storeMetadata);\n }\n\n /**\n * Store content and return a reference (implements ContentReferenceStore)\n */\n async storeContent(\n content: Buffer,\n metadata: Omit<ContentMetadata, 'createdAt' | 'lastAccessedAt' | 'accessCount'>\n ): Promise<ContentReference> {\n const startTime = Date.now();\n \n try {\n const now = new Date();\n const referenceId = ReferenceIdGenerator.generateId(content);\n \n const fullMetadata: ContentMetadata = {\n ...metadata,\n createdAt: now,\n lastAccessedAt: now,\n accessCount: 0\n };\n \n const storedContent: StoredContent = {\n content,\n metadata: fullMetadata,\n state: 'active'\n };\n \n const expirationTime = this.calculateExpirationTime(metadata.source);\n if (expirationTime !== undefined) {\n storedContent.expiresAt = expirationTime;\n }\n \n this.contentStore.set(referenceId, storedContent);\n \n // Update statistics\n this.updateStatsAfterStore(content.length);\n \n // Enforce storage limits after storing\n await this.enforceReferenceStorageLimits();\n \n // Create preview\n const preview = this.createContentPreview(content, fullMetadata.contentType);\n \n const referenceMetadata: Pick<ContentMetadata, 'contentType' | 'sizeBytes' | 'source' | 'fileName' | 'mimeType'> = {\n contentType: fullMetadata.contentType,\n sizeBytes: fullMetadata.sizeBytes,\n source: fullMetadata.source\n };\n \n if (fullMetadata.fileName !== undefined) {\n referenceMetadata.fileName = fullMetadata.fileName;\n }\n if (fullMetadata.mimeType !== undefined) {\n referenceMetadata.mimeType = fullMetadata.mimeType;\n }\n \n const reference: ContentReference = {\n referenceId,\n state: 'active',\n preview,\n metadata: referenceMetadata,\n createdAt: now,\n format: 'ref://{id}' as const\n };\n \n // Record performance\n const duration = Date.now() - startTime;\n this.recordPerformanceMetric('creation', duration);\n \n console.log(`[ContentStorage] Stored content with reference ID: ${referenceId} (${content.length} bytes)`);\n \n return reference;\n } catch (error) {\n const duration = Date.now() - startTime;\n this.recordPerformanceMetric('creation', duration);\n \n console.error('[ContentStorage] Failed to store content:', error);\n throw new ContentReferenceError(\n `Failed to store content: ${error instanceof Error ? error.message : 'Unknown error'}`,\n 'system_error',\n undefined,\n ['Try again', 'Check storage limits', 'Contact administrator']\n );\n }\n }\n\n /**\n * Resolve a reference to its content (implements ContentReferenceStore)\n */\n async resolveReference(referenceId: ReferenceId): Promise<ReferenceResolutionResult> {\n const startTime = Date.now();\n \n try {\n // Validate reference ID format\n if (!ReferenceIdGenerator.isValidReferenceId(referenceId)) {\n this.referenceStats.failedResolutions++;\n return {\n success: false,\n error: 'Invalid reference ID format',\n errorType: 'not_found',\n suggestedActions: ['Check the reference ID format', 'Ensure the reference ID is complete']\n };\n }\n \n const storedContent = this.contentStore.get(referenceId);\n \n if (!storedContent) {\n this.referenceStats.failedResolutions++;\n return {\n success: false,\n error: 'Reference not found',\n errorType: 'not_found',\n suggestedActions: ['Verify the reference ID', 'Check if the content has expired', 'Request fresh content']\n };\n }\n \n // Check if expired\n if (storedContent.expiresAt && storedContent.expiresAt < new Date()) {\n storedContent.state = 'expired';\n this.referenceStats.failedResolutions++;\n return {\n success: false,\n error: 'Reference has expired',\n errorType: 'expired',\n suggestedActions: ['Request fresh content', 'Use alternative content source']\n };\n }\n \n // Check state\n if (storedContent.state !== 'active') {\n this.referenceStats.failedResolutions++;\n return {\n success: false,\n error: `Reference is ${storedContent.state}`,\n errorType: storedContent.state === 'expired' ? 'expired' : 'corrupted',\n suggestedActions: ['Request fresh content', 'Check reference validity']\n };\n }\n \n // Update access tracking\n storedContent.metadata.lastAccessedAt = new Date();\n storedContent.metadata.accessCount++;\n \n // Update statistics\n this.referenceStats.totalResolutions++;\n \n // Record performance\n const duration = Date.now() - startTime;\n this.recordPerformanceMetric('resolution', duration);\n \n console.log(`[ContentStorage] Resolved reference ${referenceId} (${storedContent.content.length} bytes, access count: ${storedContent.metadata.accessCount})`);\n \n return {\n success: true,\n content: storedContent.content,\n metadata: storedContent.metadata\n };\n } catch (error) {\n const duration = Date.now() - startTime;\n this.recordPerformanceMetric('resolution', duration);\n \n this.referenceStats.failedResolutions++;\n console.error(`[ContentStorage] Error resolving reference ${referenceId}:`, error);\n \n return {\n success: false,\n error: `System error resolving reference: ${error instanceof Error ? error.message : 'Unknown error'}`,\n errorType: 'system_error',\n suggestedActions: ['Try again', 'Contact administrator']\n };\n }\n }\n\n /**\n * Check if a reference exists and is valid\n */\n async hasReference(referenceId: ReferenceId): Promise<boolean> {\n if (!ReferenceIdGenerator.isValidReferenceId(referenceId)) {\n return false;\n }\n \n const storedContent = this.contentStore.get(referenceId);\n if (!storedContent) {\n return false;\n }\n \n // Check if expired\n if (storedContent.expiresAt && storedContent.expiresAt < new Date()) {\n storedContent.state = 'expired';\n return false;\n }\n \n return storedContent.state === 'active';\n }\n\n /**\n * Mark a reference for cleanup\n */\n async cleanupReference(referenceId: ReferenceId): Promise<boolean> {\n const storedContent = this.contentStore.get(referenceId);\n if (!storedContent) {\n return false;\n }\n \n // Update statistics\n this.referenceStats.totalStorageBytes -= storedContent.content.length;\n this.referenceStats.activeReferences--;\n this.referenceStats.recentlyCleanedUp++;\n \n this.contentStore.delete(referenceId);\n \n console.log(`[ContentStorage] Cleaned up reference ${referenceId} (${storedContent.content.length} bytes)`);\n return true;\n }\n\n /**\n * Get current reference storage statistics (implements ContentReferenceStore)\n */\n async getStats(): Promise<ContentReferenceStats> {\n this.updateReferenceStorageStats();\n \n return {\n ...this.referenceStats,\n performanceMetrics: {\n averageCreationTimeMs: this.calculateAverage(this.referenceStats.performanceMetrics.creationTimes),\n averageResolutionTimeMs: this.calculateAverage(this.referenceStats.performanceMetrics.resolutionTimes),\n averageCleanupTimeMs: this.calculateAverage(this.referenceStats.performanceMetrics.cleanupTimes)\n }\n };\n }\n\n /**\n * Update reference configuration\n */\n async updateConfig(config: Partial<ContentReferenceConfig>): Promise<void> {\n this.referenceConfig = { ...this.referenceConfig, ...config };\n \n // Restart cleanup timer if needed\n if (this.cleanupTimer) {\n clearInterval(this.cleanupTimer);\n delete this.cleanupTimer;\n }\n \n if (this.referenceConfig.enableAutoCleanup) {\n this.startReferenceCleanupTimer();\n }\n \n console.log('[ContentStorage] Reference configuration updated');\n }\n\n /**\n * Perform cleanup based on current policies (implements ContentReferenceStore)\n */\n async performCleanup(): Promise<{ cleanedUp: number; errors: string[] }> {\n const startTime = Date.now();\n const errors: string[] = [];\n let cleanedUp = 0;\n \n try {\n console.log('[ContentStorage] Starting reference cleanup process...');\n \n const now = new Date();\n const toCleanup: ReferenceId[] = [];\n \n // Identify references for cleanup\n for (const [referenceId, storedContent] of this.contentStore.entries()) {\n let shouldCleanup = false;\n \n // Check expiration\n if (storedContent.expiresAt && storedContent.expiresAt < now) {\n shouldCleanup = true;\n storedContent.state = 'expired';\n }\n \n // Check age-based policies\n const ageMs = now.getTime() - storedContent.metadata.createdAt.getTime();\n const policy = this.getCleanupPolicy(storedContent.metadata.source);\n \n if (ageMs > policy.maxAgeMs) {\n shouldCleanup = true;\n }\n \n // Check if marked for cleanup\n if (storedContent.state === 'cleanup_pending') {\n shouldCleanup = true;\n }\n \n if (shouldCleanup) {\n toCleanup.push(referenceId);\n }\n }\n \n // Sort by priority (higher priority = cleanup first)\n toCleanup.sort((a, b) => {\n const aContent = this.contentStore.get(a)!;\n const bContent = this.contentStore.get(b)!;\n const aPriority = this.getCleanupPolicy(aContent.metadata.source).priority;\n const bPriority = this.getCleanupPolicy(bContent.metadata.source).priority;\n return bPriority - aPriority; // Higher priority first\n });\n \n // Perform cleanup\n for (const referenceId of toCleanup) {\n try {\n const success = await this.cleanupReference(referenceId);\n if (success) {\n cleanedUp++;\n }\n } catch (error) {\n errors.push(`Failed to cleanup ${referenceId}: ${error instanceof Error ? error.message : 'Unknown error'}`);\n }\n }\n \n // Check storage limits and cleanup oldest if needed\n if (this.contentStore.size > this.referenceConfig.maxReferences) {\n const sortedByAge = Array.from(this.contentStore.entries())\n .sort(([, a], [, b]) => a.metadata.lastAccessedAt.getTime() - b.metadata.lastAccessedAt.getTime());\n \n const excessCount = this.contentStore.size - this.referenceConfig.maxReferences;\n for (let i = 0; i < excessCount && i < sortedByAge.length; i++) {\n const [referenceId] = sortedByAge[i];\n try {\n const success = await this.cleanupReference(referenceId);\n if (success) {\n cleanedUp++;\n }\n } catch (error) {\n errors.push(`Failed to cleanup excess reference ${referenceId}: ${error instanceof Error ? error.message : 'Unknown error'}`);\n }\n }\n }\n \n const duration = Date.now() - startTime;\n this.recordPerformanceMetric('cleanup', duration);\n \n console.log(`[ContentStorage] Reference cleanup completed: ${cleanedUp} references cleaned up, ${errors.length} errors`);\n \n return { cleanedUp, errors };\n } catch (error) {\n const duration = Date.now() - startTime;\n this.recordPerformanceMetric('cleanup', duration);\n \n const errorMessage = `Cleanup process failed: ${error instanceof Error ? error.message : 'Unknown error'}`;\n console.error('[ContentStorage]', errorMessage);\n errors.push(errorMessage);\n \n return { cleanedUp, errors };\n }\n }\n\n /**\n * Get reference configuration for debugging\n */\n getReferenceConfig(): ContentReferenceConfig {\n return { ...this.referenceConfig };\n }\n\n // ========== Private Reference Storage Helper Methods ==========\n\n private async enforceReferenceStorageLimits(): Promise<void> {\n // Check reference count limit\n if (this.contentStore.size >= this.referenceConfig.maxReferences) {\n await this.performCleanup();\n }\n \n // Check total storage size limit\n if (this.referenceStats.totalStorageBytes >= this.referenceConfig.maxTotalStorageBytes) {\n await this.performCleanup();\n }\n }\n\n private calculateExpirationTime(source: ContentSource): Date | undefined {\n const policy = this.getCleanupPolicy(source);\n return new Date(Date.now() + policy.maxAgeMs);\n }\n\n private getCleanupPolicy(source: ContentSource) {\n switch (source) {\n case 'mcp_tool':\n return this.referenceConfig.cleanupPolicies.recent;\n case 'user_upload':\n return this.referenceConfig.cleanupPolicies.userContent;\n case 'agent_generated':\n return this.referenceConfig.cleanupPolicies.agentGenerated;\n default:\n return this.referenceConfig.cleanupPolicies.default;\n }\n }\n\n private detectContentType(content: Buffer, mimeType?: string): ContentType {\n if (mimeType) {\n if (mimeType === 'text/html') return 'html';\n if (mimeType === 'text/markdown') return 'markdown';\n if (mimeType === 'application/json') return 'json';\n if (mimeType.startsWith('text/')) return 'text';\n return 'binary';\n }\n \n // Simple content detection\n const contentStr = content.toString('utf8', 0, Math.min(content.length, 1000));\n if (contentStr.startsWith('{') || contentStr.startsWith('[')) return 'json';\n if (contentStr.includes('<html>') || contentStr.includes('<!DOCTYPE')) return 'html';\n if (contentStr.includes('#') && contentStr.includes('\\n')) return 'markdown';\n \n return 'text';\n }\n\n private createContentPreview(content: Buffer, contentType: ContentType): string {\n const maxLength = 200;\n let preview = content.toString('utf8', 0, Math.min(content.length, maxLength * 2));\n \n // Clean up based on content type\n if (contentType === 'html') {\n // Remove all HTML tags and normalize whitespace\n preview = preview\n .replace(/<[^>]*>/g, '')\n .replace(/\\s+/g, ' ')\n .trim();\n } else if (contentType === 'json') {\n try {\n const parsed = JSON.parse(preview);\n preview = JSON.stringify(parsed, null, 0);\n } catch {\n // Keep original if not valid JSON\n }\n }\n \n preview = preview.trim();\n if (preview.length > maxLength) {\n preview = preview.substring(0, maxLength) + '...';\n }\n \n return preview || '[Binary content]';\n }\n\n private updateStatsAfterStore(sizeBytes: number): void {\n this.referenceStats.activeReferences++;\n this.referenceStats.totalStorageBytes += sizeBytes;\n this.updateReferenceStorageStats();\n }\n\n private updateReferenceStorageStats(): void {\n if (this.referenceStats.activeReferences > 0) {\n this.referenceStats.averageContentSize = this.referenceStats.totalStorageBytes / this.referenceStats.activeReferences;\n }\n \n this.referenceStats.storageUtilization = (this.referenceStats.totalStorageBytes / this.referenceConfig.maxTotalStorageBytes) * 100;\n \n // Find most accessed reference\n let mostAccessedId: ReferenceId | undefined;\n let maxAccess = 0;\n \n for (const [referenceId, storedContent] of this.contentStore.entries()) {\n if (storedContent.metadata.accessCount > maxAccess) {\n maxAccess = storedContent.metadata.accessCount;\n mostAccessedId = referenceId;\n }\n }\n \n if (mostAccessedId !== undefined) {\n this.referenceStats.mostAccessedReferenceId = mostAccessedId;\n } else {\n delete this.referenceStats.mostAccessedReferenceId;\n }\n }\n\n private recordPerformanceMetric(type: 'creation' | 'resolution' | 'cleanup', timeMs: number): void {\n const metrics = this.referenceStats.performanceMetrics;\n const maxRecords = 100; // Keep last 100 measurements\n \n switch (type) {\n case 'creation':\n metrics.creationTimes.push(timeMs);\n if (metrics.creationTimes.length > maxRecords) {\n metrics.creationTimes.shift();\n }\n break;\n case 'resolution':\n metrics.resolutionTimes.push(timeMs);\n if (metrics.resolutionTimes.length > maxRecords) {\n metrics.resolutionTimes.shift();\n }\n break;\n case 'cleanup':\n metrics.cleanupTimes.push(timeMs);\n if (metrics.cleanupTimes.length > maxRecords) {\n metrics.cleanupTimes.shift();\n }\n break;\n }\n }\n\n private calculateAverage(times: number[]): number {\n if (times.length === 0) return 0;\n return times.reduce((sum, time) => sum + time, 0) / times.length;\n }\n\n private startReferenceCleanupTimer(): void {\n this.cleanupTimer = setInterval(async () => {\n try {\n await this.performCleanup();\n } catch (error) {\n console.error('[ContentStorage] Error in scheduled reference cleanup:', error);\n }\n }, this.referenceConfig.cleanupIntervalMs);\n }\n\n /**\n * Clean up resources (enhanced to include reference cleanup)\n */\n async dispose(): Promise<void> {\n if (this.cleanupTimer) {\n clearInterval(this.cleanupTimer);\n delete this.cleanupTimer;\n }\n \n this.contentStore.clear();\n \n this.clear();\n }\n}"],"names":[],"mappings":";;AAuFO,MAAM,kBAAN,MAAM,gBAAgD;AAAA,EAoB3D,YACE,aAAqB,gBAAe,qBACpC,iBACA;AAtBF,SAAQ,WAA4B,CAAA;AAEpC,SAAQ,YAAoB;AAG5B,SAAQ,mCAAoD,IAAA;AAkB1D,SAAK,aAAa;AAGlB,SAAK,kBAAkB,EAAE,GAAG,kCAAkC,GAAG,gBAAA;AACjE,SAAK,iBAAiB;AAAA,MACpB,kBAAkB;AAAA,MAClB,mBAAmB;AAAA,MACnB,mBAAmB;AAAA,MACnB,kBAAkB;AAAA,MAClB,mBAAmB;AAAA,MACnB,oBAAoB;AAAA,MACpB,oBAAoB;AAAA,MACpB,oBAAoB;AAAA,QAClB,uBAAuB;AAAA,QACvB,yBAAyB;AAAA,QACzB,sBAAsB;AAAA,QACtB,eAAe,CAAA;AAAA,QACf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AAM,0DAA0D,KAAK;AAAA,MAC/E;AAAA,IACF,GAAG,KAAK,gBAAgB,iBAAiB;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,UAAyB;AAC7B,QAAI,KAAK,cAAc;AACrB,oBAAc,KAAK,YAAY;AAC/B,aAAO,KAAK;AAAA,IACd;AAEA,SAAK,aAAa,MAAA;AAElB,SAAK,MAAA;AAAA,EACP;AACF;AAh1BE,gBAAuB,sBAAsB;AAlBxC,IAAM,iBAAN;"}
|
package/dist/esm/index2.js
CHANGED
|
@@ -64,7 +64,9 @@ class HCS10Plugin extends BasePlugin {
|
|
|
64
64
|
logLevel: "error"
|
|
65
65
|
});
|
|
66
66
|
this.stateManager.initializeConnectionsManager(hcs10Client);
|
|
67
|
-
this.context.logger.info(
|
|
67
|
+
this.context.logger.info(
|
|
68
|
+
"ConnectionsManager initialized in HCS10Plugin"
|
|
69
|
+
);
|
|
68
70
|
}
|
|
69
71
|
this.initializeTools();
|
|
70
72
|
this.context.logger.info("HCS-10 Plugin initialized successfully");
|
package/dist/esm/index2.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index2.js","sources":["../../src/plugins/hcs-10/HCS10Plugin.ts"],"sourcesContent":["import {\n GenericPluginContext,\n HederaTool,\n BasePlugin,\n HederaAgentKit,\n} from 'hedera-agent-kit';\nimport {\n IStateManager,\n OpenConvaiState,\n HCS10Builder,\n RegisterAgentTool,\n FindRegistrationsTool,\n InitiateConnectionTool,\n ListConnectionsTool,\n SendMessageToConnectionTool,\n CheckMessagesTool,\n ConnectionMonitorTool,\n ManageConnectionRequestsTool,\n AcceptConnectionRequestTool,\n RetrieveProfileTool,\n ListUnapprovedConnectionRequestsTool,\n} from '@hashgraphonline/standards-agent-kit';\nimport { HCS10Client } from '@hashgraphonline/standards-sdk';\n\nexport class HCS10Plugin extends BasePlugin {\n id = 'hcs-10';\n name = 'HCS-10 Plugin';\n description =\n 'HCS-10 agent tools for decentralized agent registration, connections, and messaging on Hedera';\n version = '1.0.0';\n author = 'Hashgraph Online';\n namespace = 'hcs10';\n\n private stateManager?: IStateManager;\n private tools: HederaTool[] = [];\n appConfig?: Record<string, unknown>;\n\n override async initialize(context: GenericPluginContext): Promise<void> {\n await super.initialize(context);\n\n const hederaKit = context.config.hederaKit as HederaAgentKit;\n if (!hederaKit) {\n this.context.logger.warn(\n 'HederaKit not found in context. HCS-10 tools will not be available.'\n );\n return;\n }\n\n try {\n this.stateManager =\n (context.stateManager as IStateManager) ||\n (context.config.stateManager as IStateManager) ||\n (this.appConfig?.stateManager as IStateManager) ||\n new OpenConvaiState();\n\n const accountId = hederaKit.signer.getAccountId().toString();\n let inboundTopicId = '';\n let outboundTopicId = '';\n\n try {\n const privateKey = hederaKit.signer.getOperatorPrivateKey().toString();\n\n const hcs10Client = new HCS10Client({\n network: hederaKit.network as 'mainnet' | 'testnet',\n operatorId: accountId,\n operatorPrivateKey: privateKey,\n logLevel: 'error',\n });\n\n const profileResponse = await hcs10Client.retrieveProfile(accountId);\n if (profileResponse.success && profileResponse.topicInfo) {\n inboundTopicId = profileResponse.topicInfo.inboundTopic;\n outboundTopicId = profileResponse.topicInfo.outboundTopic;\n }\n } catch (profileError) {\n this.context.logger.warn(\n 'Could not retrieve profile topics:',\n profileError\n );\n }\n\n this.stateManager.setCurrentAgent({\n name: `Agent ${accountId}`,\n accountId: accountId,\n inboundTopicId,\n outboundTopicId,\n privateKey: hederaKit.signer.getOperatorPrivateKey().toString(),\n });\n\n this.context.logger.info(\n `Set current agent: ${accountId} with topics ${inboundTopicId}/${outboundTopicId}`\n );\n\n
|
|
1
|
+
{"version":3,"file":"index2.js","sources":["../../src/plugins/hcs-10/HCS10Plugin.ts"],"sourcesContent":["import {\n GenericPluginContext,\n HederaTool,\n BasePlugin,\n HederaAgentKit,\n} from 'hedera-agent-kit';\nimport {\n IStateManager,\n OpenConvaiState,\n HCS10Builder,\n RegisterAgentTool,\n FindRegistrationsTool,\n InitiateConnectionTool,\n ListConnectionsTool,\n SendMessageToConnectionTool,\n CheckMessagesTool,\n ConnectionMonitorTool,\n ManageConnectionRequestsTool,\n AcceptConnectionRequestTool,\n RetrieveProfileTool,\n ListUnapprovedConnectionRequestsTool,\n} from '@hashgraphonline/standards-agent-kit';\nimport { HCS10Client } from '@hashgraphonline/standards-sdk';\n\nexport class HCS10Plugin extends BasePlugin {\n id = 'hcs-10';\n name = 'HCS-10 Plugin';\n description =\n 'HCS-10 agent tools for decentralized agent registration, connections, and messaging on Hedera';\n version = '1.0.0';\n author = 'Hashgraph Online';\n namespace = 'hcs10';\n\n private stateManager?: IStateManager;\n private tools: HederaTool[] = [];\n appConfig?: Record<string, unknown>;\n\n override async initialize(context: GenericPluginContext): Promise<void> {\n await super.initialize(context);\n\n const hederaKit = context.config.hederaKit as HederaAgentKit;\n if (!hederaKit) {\n this.context.logger.warn(\n 'HederaKit not found in context. HCS-10 tools will not be available.'\n );\n return;\n }\n\n try {\n this.stateManager =\n (context.stateManager as IStateManager) ||\n (context.config.stateManager as IStateManager) ||\n (this.appConfig?.stateManager as IStateManager) ||\n new OpenConvaiState();\n\n const accountId = hederaKit.signer.getAccountId().toString();\n let inboundTopicId = '';\n let outboundTopicId = '';\n\n try {\n const privateKey = hederaKit.signer.getOperatorPrivateKey().toString();\n\n const hcs10Client = new HCS10Client({\n network: hederaKit.network as 'mainnet' | 'testnet',\n operatorId: accountId,\n operatorPrivateKey: privateKey,\n logLevel: 'error',\n });\n\n const profileResponse = await hcs10Client.retrieveProfile(accountId);\n if (profileResponse.success && profileResponse.topicInfo) {\n inboundTopicId = profileResponse.topicInfo.inboundTopic;\n outboundTopicId = profileResponse.topicInfo.outboundTopic;\n }\n } catch (profileError) {\n this.context.logger.warn(\n 'Could not retrieve profile topics:',\n profileError\n );\n }\n\n this.stateManager.setCurrentAgent({\n name: `Agent ${accountId}`,\n accountId: accountId,\n inboundTopicId,\n outboundTopicId,\n privateKey: hederaKit.signer.getOperatorPrivateKey().toString(),\n });\n\n this.context.logger.info(\n `Set current agent: ${accountId} with topics ${inboundTopicId}/${outboundTopicId}`\n );\n\n if (this.stateManager && !this.stateManager.getConnectionsManager()) {\n const privateKey = hederaKit.signer.getOperatorPrivateKey().toString();\n const hcs10Client = new HCS10Client({\n network: hederaKit.network as 'mainnet' | 'testnet',\n operatorId: accountId,\n operatorPrivateKey: privateKey,\n logLevel: 'error',\n });\n\n this.stateManager.initializeConnectionsManager(hcs10Client as any);\n this.context.logger.info(\n 'ConnectionsManager initialized in HCS10Plugin'\n );\n }\n\n this.initializeTools();\n\n this.context.logger.info('HCS-10 Plugin initialized successfully');\n } catch (error) {\n this.context.logger.error('Failed to initialize HCS-10 plugin:', error);\n }\n }\n\n private initializeTools(): void {\n if (!this.stateManager) {\n throw new Error('StateManager must be initialized before creating tools');\n }\n\n const hederaKit = this.context.config.hederaKit as HederaAgentKit;\n if (!hederaKit) {\n throw new Error('HederaKit not found in context config');\n }\n\n const hcs10Builder = new HCS10Builder(hederaKit, this.stateManager);\n\n this.tools = [\n new RegisterAgentTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new FindRegistrationsTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new RetrieveProfileTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new InitiateConnectionTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new ListConnectionsTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new SendMessageToConnectionTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new CheckMessagesTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new ConnectionMonitorTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new ManageConnectionRequestsTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new AcceptConnectionRequestTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new ListUnapprovedConnectionRequestsTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n ];\n }\n\n getTools(): HederaTool[] {\n return this.tools;\n }\n\n getStateManager(): IStateManager | undefined {\n return this.stateManager;\n }\n\n override async cleanup(): Promise<void> {\n this.tools = [];\n delete this.stateManager;\n if (this.context?.logger) {\n this.context.logger.info('HCS-10 Plugin cleaned up');\n }\n }\n}\n"],"names":[],"mappings":";;;AAwBO,MAAM,oBAAoB,WAAW;AAAA,EAArC,cAAA;AAAA,UAAA,GAAA,SAAA;AACL,SAAA,KAAK;AACL,SAAA,OAAO;AACP,SAAA,cACE;AACF,SAAA,UAAU;AACV,SAAA,SAAS;AACT,SAAA,YAAY;AAGZ,SAAQ,QAAsB,CAAA;AAAA,EAAC;AAAA,EAG/B,MAAe,WAAW,SAA8C;AACtE,UAAM,MAAM,WAAW,OAAO;AAE9B,UAAM,YAAY,QAAQ,OAAO;AACjC,QAAI,CAAC,WAAW;AACd,WAAK,QAAQ,OAAO;AAAA,QAClB;AAAA,MAAA;AAEF;AAAA,IACF;AAEA,QAAI;AACF,WAAK,eACF,QAAQ,gBACR,QAAQ,OAAO,gBACf,KAAK,WAAW,gBACjB,IAAI,gBAAA;AAEN,YAAM,YAAY,UAAU,OAAO,aAAA,EAAe,SAAA;AAClD,UAAI,iBAAiB;AACrB,UAAI,kBAAkB;AAEtB,UAAI;AACF,cAAM,aAAa,UAAU,OAAO,sBAAA,EAAwB,SAAA;AAE5D,cAAM,cAAc,IAAI,YAAY;AAAA,UAClC,SAAS,UAAU;AAAA,UACnB,YAAY;AAAA,UACZ,oBAAoB;AAAA,UACpB,UAAU;AAAA,QAAA,CACX;AAED,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UAAyB;AACtC,SAAK,QAAQ,CAAA;AACb,WAAO,KAAK;AACZ,QAAI,KAAK,SAAS,QAAQ;AACxB,WAAK,QAAQ,OAAO,KAAK,0BAA0B;AAAA,IACrD;AAAA,EACF;AACF;"}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
class MCPContentProcessor {
|
|
2
|
+
constructor(contentStorage, logger) {
|
|
3
|
+
this.contentStorage = contentStorage;
|
|
4
|
+
this.logger = logger;
|
|
5
|
+
}
|
|
6
|
+
analyzeResponse(response) {
|
|
7
|
+
const contents = [];
|
|
8
|
+
let totalSize = 0;
|
|
9
|
+
this.extractContentFromResponse(response, contents);
|
|
10
|
+
totalSize = contents.reduce((sum, content) => sum + content.sizeBytes, 0);
|
|
11
|
+
const largestContentSize = contents.reduce((max, content) => Math.max(max, content.sizeBytes), 0);
|
|
12
|
+
const shouldProcess = contents.some(
|
|
13
|
+
(content) => this.contentStorage.shouldUseReference(
|
|
14
|
+
typeof content.content === "string" ? content.content : JSON.stringify(content.content)
|
|
15
|
+
)
|
|
16
|
+
);
|
|
17
|
+
return {
|
|
18
|
+
shouldProcess,
|
|
19
|
+
contents,
|
|
20
|
+
totalSize,
|
|
21
|
+
largestContentSize
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
async processResponse(response, serverName, toolName) {
|
|
25
|
+
try {
|
|
26
|
+
const analysis = this.analyzeResponse(response);
|
|
27
|
+
if (!analysis.shouldProcess) {
|
|
28
|
+
return {
|
|
29
|
+
content: response,
|
|
30
|
+
wasProcessed: false
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const processedResponse = await this.createReferencedResponse(
|
|
34
|
+
response,
|
|
35
|
+
analysis,
|
|
36
|
+
serverName,
|
|
37
|
+
toolName
|
|
38
|
+
);
|
|
39
|
+
return processedResponse;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
this.logger.error("Error processing MCP response:", error);
|
|
42
|
+
return {
|
|
43
|
+
content: response,
|
|
44
|
+
wasProcessed: false,
|
|
45
|
+
errors: [error instanceof Error ? error.message : "Unknown processing error"]
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
extractContentFromResponse(obj, contents) {
|
|
50
|
+
if (obj === null || obj === void 0) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (Array.isArray(obj)) {
|
|
54
|
+
obj.forEach((item) => this.extractContentFromResponse(item, contents));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (typeof obj === "object") {
|
|
58
|
+
const record = obj;
|
|
59
|
+
if (record.type === "text" && typeof record.text === "string") {
|
|
60
|
+
contents.push({
|
|
61
|
+
content: record.text,
|
|
62
|
+
type: "text",
|
|
63
|
+
sizeBytes: Buffer.byteLength(record.text, "utf8"),
|
|
64
|
+
mimeType: "text/plain"
|
|
65
|
+
});
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (record.type === "image" && typeof record.data === "string") {
|
|
69
|
+
contents.push({
|
|
70
|
+
content: record.data,
|
|
71
|
+
type: "image",
|
|
72
|
+
sizeBytes: Math.ceil(record.data.length * 0.75),
|
|
73
|
+
mimeType: record.mimeType || "image/jpeg"
|
|
74
|
+
});
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (record.type === "resource" && record.resource) {
|
|
78
|
+
const resourceStr = JSON.stringify(record.resource);
|
|
79
|
+
contents.push({
|
|
80
|
+
content: resourceStr,
|
|
81
|
+
type: "resource",
|
|
82
|
+
sizeBytes: Buffer.byteLength(resourceStr, "utf8"),
|
|
83
|
+
mimeType: "application/json"
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
Object.values(record).forEach((value) => this.extractContentFromResponse(value, contents));
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (typeof obj === "string") {
|
|
91
|
+
if (obj.length > 1e3) {
|
|
92
|
+
contents.push({
|
|
93
|
+
content: obj,
|
|
94
|
+
type: "text",
|
|
95
|
+
sizeBytes: Buffer.byteLength(obj, "utf8"),
|
|
96
|
+
mimeType: this.detectMimeType(obj)
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async createReferencedResponse(originalResponse, analysis, serverName, toolName) {
|
|
102
|
+
const processedResponse = this.deepClone(originalResponse);
|
|
103
|
+
const errors = [];
|
|
104
|
+
let referenceCreated = false;
|
|
105
|
+
let totalReferenceSize = 0;
|
|
106
|
+
for (const contentInfo of analysis.contents) {
|
|
107
|
+
if (this.contentStorage.shouldUseReference(
|
|
108
|
+
typeof contentInfo.content === "string" ? contentInfo.content : JSON.stringify(contentInfo.content)
|
|
109
|
+
)) {
|
|
110
|
+
try {
|
|
111
|
+
const contentBuffer = Buffer.from(
|
|
112
|
+
typeof contentInfo.content === "string" ? contentInfo.content : JSON.stringify(contentInfo.content),
|
|
113
|
+
"utf8"
|
|
114
|
+
);
|
|
115
|
+
const contentType = this.mapMimeTypeToContentType(contentInfo.mimeType);
|
|
116
|
+
const metadata = {
|
|
117
|
+
contentType,
|
|
118
|
+
source: "mcp_tool",
|
|
119
|
+
mcpToolName: `${serverName}::${toolName}`,
|
|
120
|
+
tags: ["mcp_response", serverName, toolName]
|
|
121
|
+
};
|
|
122
|
+
if (contentInfo.mimeType !== void 0) {
|
|
123
|
+
metadata.mimeType = contentInfo.mimeType;
|
|
124
|
+
}
|
|
125
|
+
const reference = await this.contentStorage.storeContentIfLarge(
|
|
126
|
+
contentBuffer,
|
|
127
|
+
metadata
|
|
128
|
+
);
|
|
129
|
+
if (reference) {
|
|
130
|
+
this.replaceContentInResponse(
|
|
131
|
+
processedResponse,
|
|
132
|
+
contentInfo.content,
|
|
133
|
+
this.createLightweightReference(reference)
|
|
134
|
+
);
|
|
135
|
+
referenceCreated = true;
|
|
136
|
+
totalReferenceSize += contentBuffer.length;
|
|
137
|
+
}
|
|
138
|
+
} catch (error) {
|
|
139
|
+
errors.push(`Failed to create reference: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const result = {
|
|
144
|
+
content: processedResponse,
|
|
145
|
+
wasProcessed: true,
|
|
146
|
+
referenceCreated,
|
|
147
|
+
originalSize: totalReferenceSize
|
|
148
|
+
};
|
|
149
|
+
if (errors.length > 0) {
|
|
150
|
+
result.errors = errors;
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
createLightweightReference(reference) {
|
|
155
|
+
return {
|
|
156
|
+
type: "content_reference",
|
|
157
|
+
referenceId: reference.referenceId,
|
|
158
|
+
preview: reference.preview,
|
|
159
|
+
size: reference.metadata.sizeBytes,
|
|
160
|
+
contentType: reference.metadata.contentType,
|
|
161
|
+
format: "ref://{id}",
|
|
162
|
+
_isReference: true
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
replaceContentInResponse(obj, oldContent, newContent) {
|
|
166
|
+
if (obj === null || obj === void 0) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (Array.isArray(obj)) {
|
|
170
|
+
for (let i = 0; i < obj.length; i++) {
|
|
171
|
+
if (obj[i] === oldContent) {
|
|
172
|
+
obj[i] = newContent;
|
|
173
|
+
} else {
|
|
174
|
+
this.replaceContentInResponse(obj[i], oldContent, newContent);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (typeof obj === "object") {
|
|
180
|
+
const record = obj;
|
|
181
|
+
for (const key in record) {
|
|
182
|
+
if (record[key] === oldContent) {
|
|
183
|
+
record[key] = newContent;
|
|
184
|
+
} else {
|
|
185
|
+
this.replaceContentInResponse(record[key], oldContent, newContent);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
detectMimeType(content) {
|
|
191
|
+
if (content.trim().startsWith("{") || content.trim().startsWith("[")) {
|
|
192
|
+
return "application/json";
|
|
193
|
+
}
|
|
194
|
+
if (content.includes("<html>") || content.includes("<!DOCTYPE")) {
|
|
195
|
+
return "text/html";
|
|
196
|
+
}
|
|
197
|
+
if (content.includes("# ") || content.includes("## ")) {
|
|
198
|
+
return "text/markdown";
|
|
199
|
+
}
|
|
200
|
+
return "text/plain";
|
|
201
|
+
}
|
|
202
|
+
mapMimeTypeToContentType(mimeType) {
|
|
203
|
+
if (!mimeType) return "text";
|
|
204
|
+
if (mimeType.startsWith("text/plain")) return "text";
|
|
205
|
+
if (mimeType === "application/json") return "json";
|
|
206
|
+
if (mimeType === "text/html") return "html";
|
|
207
|
+
if (mimeType === "text/markdown") return "markdown";
|
|
208
|
+
if (mimeType.startsWith("text/")) return "text";
|
|
209
|
+
return "binary";
|
|
210
|
+
}
|
|
211
|
+
deepClone(obj) {
|
|
212
|
+
if (obj === null || typeof obj !== "object") {
|
|
213
|
+
return obj;
|
|
214
|
+
}
|
|
215
|
+
if (obj instanceof Date) {
|
|
216
|
+
return new Date(obj.getTime());
|
|
217
|
+
}
|
|
218
|
+
if (Array.isArray(obj)) {
|
|
219
|
+
return obj.map((item) => this.deepClone(item));
|
|
220
|
+
}
|
|
221
|
+
const cloned = {};
|
|
222
|
+
for (const key in obj) {
|
|
223
|
+
if (obj.hasOwnProperty(key)) {
|
|
224
|
+
cloned[key] = this.deepClone(obj[key]);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return cloned;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
export {
|
|
231
|
+
MCPContentProcessor
|
|
232
|
+
};
|
|
233
|
+
//# sourceMappingURL=index20.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index20.js","sources":["../../src/mcp/ContentProcessor.ts"],"sourcesContent":["import type { ContentType, ContentSource } from '../types/content-reference';\nimport type { ContentStorage } from '../memory/ContentStorage';\nimport { Logger } from '@hashgraphonline/standards-sdk';\n\nexport interface MCPResponseContent {\n content: unknown;\n type: 'text' | 'image' | 'resource' | 'text[]' | 'image[]';\n sizeBytes: number;\n mimeType?: string;\n}\n\nexport interface ProcessedResponse {\n content: unknown;\n wasProcessed: boolean;\n referenceCreated?: boolean;\n referenceId?: string;\n originalSize?: number;\n errors?: string[];\n}\n\nexport interface ContentAnalysis {\n shouldProcess: boolean;\n contents: MCPResponseContent[];\n totalSize: number;\n largestContentSize: number;\n}\n\nexport class MCPContentProcessor {\n private contentStorage: ContentStorage;\n private logger: Logger;\n\n constructor(contentStorage: ContentStorage, logger: Logger) {\n this.contentStorage = contentStorage;\n this.logger = logger;\n }\n\n analyzeResponse(response: unknown): ContentAnalysis {\n const contents: MCPResponseContent[] = [];\n let totalSize = 0;\n\n this.extractContentFromResponse(response, contents);\n\n totalSize = contents.reduce((sum, content) => sum + content.sizeBytes, 0);\n const largestContentSize = contents.reduce((max, content) => \n Math.max(max, content.sizeBytes), 0);\n\n const shouldProcess = contents.some(content => \n this.contentStorage.shouldUseReference(\n typeof content.content === 'string' \n ? content.content \n : JSON.stringify(content.content)\n )\n );\n\n return {\n shouldProcess,\n contents,\n totalSize,\n largestContentSize\n };\n }\n\n async processResponse(\n response: unknown,\n serverName: string,\n toolName: string\n ): Promise<ProcessedResponse> {\n try {\n const analysis = this.analyzeResponse(response);\n \n if (!analysis.shouldProcess) {\n return {\n content: response,\n wasProcessed: false\n };\n }\n\n const processedResponse = await this.createReferencedResponse(\n response,\n analysis,\n serverName,\n toolName\n );\n\n return processedResponse;\n } catch (error) {\n this.logger.error('Error processing MCP response:', error);\n return {\n content: response,\n wasProcessed: false,\n errors: [error instanceof Error ? error.message : 'Unknown processing error']\n };\n }\n }\n\n private extractContentFromResponse(obj: unknown, contents: MCPResponseContent[]): void {\n if (obj === null || obj === undefined) {\n return;\n }\n\n if (Array.isArray(obj)) {\n obj.forEach(item => this.extractContentFromResponse(item, contents));\n return;\n }\n\n if (typeof obj === 'object') {\n const record = obj as Record<string, unknown>;\n \n if (record.type === 'text' && typeof record.text === 'string') {\n contents.push({\n content: record.text,\n type: 'text',\n sizeBytes: Buffer.byteLength(record.text, 'utf8'),\n mimeType: 'text/plain'\n });\n return;\n }\n\n if (record.type === 'image' && typeof record.data === 'string') {\n contents.push({\n content: record.data,\n type: 'image',\n sizeBytes: Math.ceil(record.data.length * 0.75),\n mimeType: record.mimeType as string || 'image/jpeg'\n });\n return;\n }\n\n if (record.type === 'resource' && record.resource) {\n const resourceStr = JSON.stringify(record.resource);\n contents.push({\n content: resourceStr,\n type: 'resource',\n sizeBytes: Buffer.byteLength(resourceStr, 'utf8'),\n mimeType: 'application/json'\n });\n return;\n }\n\n Object.values(record).forEach(value => \n this.extractContentFromResponse(value, contents));\n return;\n }\n\n if (typeof obj === 'string') {\n if (obj.length > 1000) {\n contents.push({\n content: obj,\n type: 'text',\n sizeBytes: Buffer.byteLength(obj, 'utf8'),\n mimeType: this.detectMimeType(obj)\n });\n }\n }\n }\n\n private async createReferencedResponse(\n originalResponse: unknown,\n analysis: ContentAnalysis,\n serverName: string,\n toolName: string\n ): Promise<ProcessedResponse> {\n const processedResponse = this.deepClone(originalResponse);\n const errors: string[] = [];\n let referenceCreated = false;\n let totalReferenceSize = 0;\n\n for (const contentInfo of analysis.contents) {\n if (this.contentStorage.shouldUseReference(\n typeof contentInfo.content === 'string' \n ? contentInfo.content \n : JSON.stringify(contentInfo.content)\n )) {\n try {\n const contentBuffer = Buffer.from(\n typeof contentInfo.content === 'string' \n ? contentInfo.content \n : JSON.stringify(contentInfo.content),\n 'utf8'\n );\n\n const contentType = this.mapMimeTypeToContentType(contentInfo.mimeType);\n \n const metadata: Parameters<typeof this.contentStorage.storeContentIfLarge>[1] = {\n contentType,\n source: 'mcp_tool' as ContentSource,\n mcpToolName: `${serverName}::${toolName}`,\n tags: ['mcp_response', serverName, toolName]\n };\n \n if (contentInfo.mimeType !== undefined) {\n metadata.mimeType = contentInfo.mimeType;\n }\n \n const reference = await this.contentStorage.storeContentIfLarge(\n contentBuffer,\n metadata\n );\n\n if (reference) {\n this.replaceContentInResponse(\n processedResponse,\n contentInfo.content,\n this.createLightweightReference(reference)\n );\n referenceCreated = true;\n totalReferenceSize += contentBuffer.length;\n }\n } catch (error) {\n errors.push(`Failed to create reference: ${error instanceof Error ? error.message : 'Unknown error'}`);\n }\n }\n }\n\n const result: ProcessedResponse = {\n content: processedResponse,\n wasProcessed: true,\n referenceCreated,\n originalSize: totalReferenceSize\n };\n \n if (errors.length > 0) {\n result.errors = errors;\n }\n \n return result;\n }\n\n private createLightweightReference(reference: any): Record<string, unknown> {\n return {\n type: 'content_reference',\n referenceId: reference.referenceId,\n preview: reference.preview,\n size: reference.metadata.sizeBytes,\n contentType: reference.metadata.contentType,\n format: 'ref://{id}',\n _isReference: true\n };\n }\n\n private replaceContentInResponse(obj: unknown, oldContent: unknown, newContent: unknown): void {\n if (obj === null || obj === undefined) {\n return;\n }\n\n if (Array.isArray(obj)) {\n for (let i = 0; i < obj.length; i++) {\n if (obj[i] === oldContent) {\n obj[i] = newContent;\n } else {\n this.replaceContentInResponse(obj[i], oldContent, newContent);\n }\n }\n return;\n }\n\n if (typeof obj === 'object') {\n const record = obj as Record<string, unknown>;\n \n for (const key in record) {\n if (record[key] === oldContent) {\n record[key] = newContent;\n } else {\n this.replaceContentInResponse(record[key], oldContent, newContent);\n }\n }\n }\n }\n\n private detectMimeType(content: string): string {\n if (content.trim().startsWith('{') || content.trim().startsWith('[')) {\n return 'application/json';\n }\n if (content.includes('<html>') || content.includes('<!DOCTYPE')) {\n return 'text/html';\n }\n if (content.includes('# ') || content.includes('## ')) {\n return 'text/markdown';\n }\n return 'text/plain';\n }\n\n private mapMimeTypeToContentType(mimeType?: string): ContentType {\n if (!mimeType) return 'text';\n \n if (mimeType.startsWith('text/plain')) return 'text';\n if (mimeType === 'application/json') return 'json';\n if (mimeType === 'text/html') return 'html';\n if (mimeType === 'text/markdown') return 'markdown';\n if (mimeType.startsWith('text/')) return 'text';\n \n return 'binary';\n }\n\n private deepClone<T>(obj: T): T {\n if (obj === null || typeof obj !== 'object') {\n return obj;\n }\n \n if (obj instanceof Date) {\n return new Date(obj.getTime()) as T;\n }\n \n if (Array.isArray(obj)) {\n return obj.map(item => this.deepClone(item)) as T;\n }\n \n const cloned = {} as T;\n for (const key in obj) {\n if (obj.hasOwnProperty(key)) {\n cloned[key] = this.deepClone(obj[key]);\n }\n }\n \n return cloned;\n }\n}"],"names":[],"mappings":"AA2BO,MAAM,oBAAoB;AAAA,EAI/B,YAAY,gBAAgC,QAAgB;AAC1D,SAAK,iBAAiB;AACtB,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,gBAAgB,UAAoC;AAClD,UAAM,WAAiC,CAAA;AACvC,QAAI,YAAY;AAEhB,SAAK,2BAA2B,UAAU,QAAQ;AAElD,gBAAY,SAAS,OAAO,CAAC,KAAK,YAAY,MAAM,QAAQ,WAAW,CAAC;AACxE,UAAM,qBAAqB,SAAS,OAAO,CAAC,KAAK,YAC/C,KAAK,IAAI,KAAK,QAAQ,SAAS,GAAG,CAAC;AAErC,UAAM,gBAAgB,SAAS;AAAA,MAAK,CAAA,YAClC,KAAK,eAAe;AAAA,QAClB,OAAO,QAAQ,YAAY,WACvB,QAAQ,UACR,KAAK,UAAU,QAAQ,OAAO;AAAA,MAAA;AAAA,IACpC;AAGF,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAAA,EAEJ;AAAA,EAEA,MAAM,gBACJ,UACA,YACA,UAC4B;AAC5B,QAAI;AACF,YAAM,WAAW,KAAK,gBAAgB,QAAQ;AAE9C,UAAI,CAAC,SAAS,eAAe;AAC3B,eAAO;AAAA,UACL,SAAS;AAAA,UACT,cAAc;AAAA,QAAA;AAAA,MAElB;AAEA,YAAM,oBAAoB,MAAM,KAAK;AAAA,QACnC;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MAAA;AAGF,aAAO;AAAA,IACT,SAAS,OAAO;AACd,WAAK,OAAO,MAAM,kCAAkC,KAAK;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AAA,IACT;AACA,QAAI,QAAQ,SAAS,IAAI,KAAK,QAAQ,SAAS,KAAK,GAAG;AACrD,aAAO;AAAA,IACT;AACA,WAAO;AAAA,EACT;AAAA,EAEQ,yBAAyB,UAAgC;AAC/D,QAAI,CAAC,SAAU,QAAO;AAEtB,QAAI,SAAS,WAAW,YAAY,EAAG,QAAO;AAC9C,QAAI,aAAa,mBAAoB,QAAO;AAC5C,QAAI,aAAa,YAAa,QAAO;AACrC,QAAI,aAAa,gBAAiB,QAAO;AACzC,QAAI,SAAS,WAAW,OAAO,EAAG,QAAO;AAEzC,WAAO;AAAA,EACT;AAAA,EAEQ,UAAa,KAAW;AAC9B,QAAI,QAAQ,QAAQ,OAAO,QAAQ,UAAU;AAC3C,aAAO;AAAA,IACT;AAEA,QAAI,eAAe,MAAM;AACvB,aAAO,IAAI,KAAK,IAAI,SAAS;AAAA,IAC/B;AAEA,QAAI,MAAM,QAAQ,GAAG,GAAG;AACtB,aAAO,IAAI,IAAI,CAAA,SAAQ,KAAK,UAAU,IAAI,CAAC;AAAA,IAC7C;AAEA,UAAM,SAAS,CAAA;AACf,eAAW,OAAO,KAAK;AACrB,UAAI,IAAI,eAAe,GAAG,GAAG;AAC3B,eAAO,GAAG,IAAI,KAAK,UAAU,IAAI,GAAG,CAAC;AAAA,MACvC;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { TokenCounter } from "./index22.js";
|
|
2
|
+
const _MemoryWindow = class _MemoryWindow {
|
|
3
|
+
// Remove messages in pairs to maintain conversation flow
|
|
4
|
+
constructor(maxTokens = _MemoryWindow.DEFAULT_MAX_TOKENS, reserveTokens = _MemoryWindow.DEFAULT_RESERVE_TOKENS, tokenCounter) {
|
|
5
|
+
this.messages = [];
|
|
6
|
+
this.systemPrompt = "";
|
|
7
|
+
this.systemPromptTokens = 0;
|
|
8
|
+
if (reserveTokens >= maxTokens) {
|
|
9
|
+
throw new Error("Reserve tokens must be less than max tokens");
|
|
10
|
+
}
|
|
11
|
+
this.maxTokens = maxTokens;
|
|
12
|
+
this.reserveTokens = reserveTokens;
|
|
13
|
+
this.tokenCounter = tokenCounter || new TokenCounter();
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Add a message to the memory window, pruning old messages if necessary
|
|
17
|
+
* @param message - The message to add
|
|
18
|
+
* @returns Result of the add operation including any pruned messages
|
|
19
|
+
*/
|
|
20
|
+
addMessage(message) {
|
|
21
|
+
this.tokenCounter.countMessageTokens(message);
|
|
22
|
+
this.messages.push(message);
|
|
23
|
+
const currentTokens = this.getCurrentTokenCount();
|
|
24
|
+
const availableTokens = this.maxTokens - this.reserveTokens;
|
|
25
|
+
let prunedMessages = [];
|
|
26
|
+
if (currentTokens > availableTokens) {
|
|
27
|
+
this.messages.pop();
|
|
28
|
+
prunedMessages = this.pruneToFit();
|
|
29
|
+
this.messages.push(message);
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
added: true,
|
|
33
|
+
prunedMessages,
|
|
34
|
+
currentTokenCount: this.getCurrentTokenCount(),
|
|
35
|
+
remainingCapacity: this.getRemainingTokenCapacity()
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Prune old messages to fit within token limits
|
|
40
|
+
* Removes messages in pairs to maintain conversational flow
|
|
41
|
+
* @returns Array of pruned messages
|
|
42
|
+
*/
|
|
43
|
+
pruneToFit() {
|
|
44
|
+
const prunedMessages = [];
|
|
45
|
+
const targetTokens = this.maxTokens - this.reserveTokens;
|
|
46
|
+
while (this.getCurrentTokenCount() > targetTokens && this.messages.length > 0) {
|
|
47
|
+
const batchSize = Math.min(_MemoryWindow.PRUNING_BATCH_SIZE, this.messages.length);
|
|
48
|
+
for (let i = 0; i < batchSize; i++) {
|
|
49
|
+
const prunedMessage = this.messages.shift();
|
|
50
|
+
if (prunedMessage) {
|
|
51
|
+
prunedMessages.push(prunedMessage);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (prunedMessages.length > 1e3) {
|
|
55
|
+
console.warn("MemoryWindow: Excessive pruning detected, stopping to prevent infinite loop");
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return prunedMessages;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Get current token count including system prompt and messages
|
|
63
|
+
* @returns Current token count
|
|
64
|
+
*/
|
|
65
|
+
getCurrentTokenCount() {
|
|
66
|
+
const messageTokens = this.tokenCounter.countMessagesTokens(this.messages);
|
|
67
|
+
return this.systemPromptTokens + messageTokens;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Get remaining token capacity before hitting the reserve limit
|
|
71
|
+
* @returns Remaining tokens that can be used
|
|
72
|
+
*/
|
|
73
|
+
getRemainingTokenCapacity() {
|
|
74
|
+
return Math.max(0, this.maxTokens - this.getCurrentTokenCount());
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Check if a message can be added without exceeding limits
|
|
78
|
+
* @param message - The message to check
|
|
79
|
+
* @returns True if message can be added within reserve limits
|
|
80
|
+
*/
|
|
81
|
+
canAddMessage(message) {
|
|
82
|
+
const messageTokens = this.tokenCounter.countMessageTokens(message);
|
|
83
|
+
const currentTokens = this.getCurrentTokenCount();
|
|
84
|
+
const wouldExceedReserve = currentTokens + messageTokens > this.maxTokens - this.reserveTokens;
|
|
85
|
+
if (messageTokens > this.maxTokens) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
return !wouldExceedReserve || this.messages.length > 0;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get all messages in the memory window
|
|
92
|
+
* @returns Copy of current messages array
|
|
93
|
+
*/
|
|
94
|
+
getMessages() {
|
|
95
|
+
return [...this.messages];
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Clear all messages from the memory window
|
|
99
|
+
*/
|
|
100
|
+
clear() {
|
|
101
|
+
this.messages = [];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Set the system prompt and update token calculations
|
|
105
|
+
* @param systemPrompt - The system prompt text
|
|
106
|
+
*/
|
|
107
|
+
setSystemPrompt(systemPrompt) {
|
|
108
|
+
this.systemPrompt = systemPrompt;
|
|
109
|
+
this.systemPromptTokens = this.tokenCounter.estimateSystemPromptTokens(systemPrompt);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Get the current system prompt
|
|
113
|
+
* @returns Current system prompt
|
|
114
|
+
*/
|
|
115
|
+
getSystemPrompt() {
|
|
116
|
+
return this.systemPrompt;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Get current configuration
|
|
120
|
+
* @returns Memory window configuration
|
|
121
|
+
*/
|
|
122
|
+
getConfig() {
|
|
123
|
+
return {
|
|
124
|
+
maxTokens: this.maxTokens,
|
|
125
|
+
reserveTokens: this.reserveTokens,
|
|
126
|
+
currentTokens: this.getCurrentTokenCount(),
|
|
127
|
+
messageCount: this.messages.length,
|
|
128
|
+
systemPromptTokens: this.systemPromptTokens
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Update token limits
|
|
133
|
+
* @param maxTokens - New maximum token limit
|
|
134
|
+
* @param reserveTokens - New reserve token amount
|
|
135
|
+
*/
|
|
136
|
+
updateLimits(maxTokens, reserveTokens) {
|
|
137
|
+
if (reserveTokens !== void 0 && reserveTokens >= maxTokens) {
|
|
138
|
+
throw new Error("Reserve tokens must be less than max tokens");
|
|
139
|
+
}
|
|
140
|
+
this.maxTokens = maxTokens;
|
|
141
|
+
if (reserveTokens !== void 0) {
|
|
142
|
+
this.reserveTokens = reserveTokens;
|
|
143
|
+
}
|
|
144
|
+
if (this.getCurrentTokenCount() > this.maxTokens - this.reserveTokens) {
|
|
145
|
+
this.pruneToFit();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Get statistics about the memory window
|
|
150
|
+
* @returns Memory usage statistics
|
|
151
|
+
*/
|
|
152
|
+
getStats() {
|
|
153
|
+
const currentTokens = this.getCurrentTokenCount();
|
|
154
|
+
const capacity = this.maxTokens;
|
|
155
|
+
const usagePercentage = currentTokens / capacity * 100;
|
|
156
|
+
return {
|
|
157
|
+
totalMessages: this.messages.length,
|
|
158
|
+
currentTokens,
|
|
159
|
+
maxTokens: capacity,
|
|
160
|
+
reserveTokens: this.reserveTokens,
|
|
161
|
+
systemPromptTokens: this.systemPromptTokens,
|
|
162
|
+
usagePercentage: Math.round(usagePercentage * 100) / 100,
|
|
163
|
+
remainingCapacity: this.getRemainingTokenCapacity(),
|
|
164
|
+
canAcceptMore: this.getRemainingTokenCapacity() > this.reserveTokens
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Clean up resources
|
|
169
|
+
*/
|
|
170
|
+
dispose() {
|
|
171
|
+
this.clear();
|
|
172
|
+
this.tokenCounter.dispose();
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
_MemoryWindow.DEFAULT_MAX_TOKENS = 8e3;
|
|
176
|
+
_MemoryWindow.DEFAULT_RESERVE_TOKENS = 1e3;
|
|
177
|
+
_MemoryWindow.PRUNING_BATCH_SIZE = 2;
|
|
178
|
+
let MemoryWindow = _MemoryWindow;
|
|
179
|
+
export {
|
|
180
|
+
MemoryWindow
|
|
181
|
+
};
|
|
182
|
+
//# sourceMappingURL=index21.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index21.js","sources":["../../src/memory/MemoryWindow.ts"],"sourcesContent":["import type { BaseMessage } from '@langchain/core/messages';\nimport { TokenCounter } from './TokenCounter';\n\n/**\n * Result of adding a message to the memory window\n */\nexport interface AddMessageResult {\n /** Whether the message was successfully added */\n added: boolean;\n /** Messages that were pruned to make room */\n prunedMessages: BaseMessage[];\n /** Current token count after operation */\n currentTokenCount: number;\n /** Remaining token capacity */\n remainingCapacity: number;\n}\n\n/**\n * Memory window that manages conversation history with token-based size limits\n * Automatically prunes old messages to stay within token limits while preserving conversational context\n */\nexport class MemoryWindow {\n private messages: BaseMessage[] = [];\n private maxTokens: number;\n private reserveTokens: number;\n private tokenCounter: TokenCounter;\n private systemPrompt: string = '';\n private systemPromptTokens: number = 0;\n\n // Default token limits for different model contexts\n public static readonly DEFAULT_MAX_TOKENS = 8000; // Conservative limit for most models\n public static readonly DEFAULT_RESERVE_TOKENS = 1000; // Reserve for response generation\n public static readonly PRUNING_BATCH_SIZE = 2; // Remove messages in pairs to maintain conversation flow\n\n constructor(\n maxTokens: number = MemoryWindow.DEFAULT_MAX_TOKENS,\n reserveTokens: number = MemoryWindow.DEFAULT_RESERVE_TOKENS,\n tokenCounter?: TokenCounter\n ) {\n if (reserveTokens >= maxTokens) {\n throw new Error('Reserve tokens must be less than max tokens');\n }\n\n this.maxTokens = maxTokens;\n this.reserveTokens = reserveTokens;\n this.tokenCounter = tokenCounter || new TokenCounter();\n }\n\n /**\n * Add a message to the memory window, pruning old messages if necessary\n * @param message - The message to add\n * @returns Result of the add operation including any pruned messages\n */\n addMessage(message: BaseMessage): AddMessageResult {\n // Calculate tokens for the new message\n const messageTokens = this.tokenCounter.countMessageTokens(message);\n \n // Add the message first\n this.messages.push(message);\n \n // Check if we need to prune\n const currentTokens = this.getCurrentTokenCount();\n const availableTokens = this.maxTokens - this.reserveTokens;\n \n let prunedMessages: BaseMessage[] = [];\n \n if (currentTokens > availableTokens) {\n // Need to prune - remove the new message temporarily\n this.messages.pop();\n \n // Prune old messages to make room\n prunedMessages = this.pruneToFit();\n \n // Add the new message back\n this.messages.push(message);\n }\n\n return {\n added: true,\n prunedMessages,\n currentTokenCount: this.getCurrentTokenCount(),\n remainingCapacity: this.getRemainingTokenCapacity()\n };\n }\n\n /**\n * Prune old messages to fit within token limits\n * Removes messages in pairs to maintain conversational flow\n * @returns Array of pruned messages\n */\n pruneToFit(): BaseMessage[] {\n const prunedMessages: BaseMessage[] = [];\n const targetTokens = this.maxTokens - this.reserveTokens;\n \n while (this.getCurrentTokenCount() > targetTokens && this.messages.length > 0) {\n // Remove messages in batches to maintain conversation flow\n const batchSize = Math.min(MemoryWindow.PRUNING_BATCH_SIZE, this.messages.length);\n \n for (let i = 0; i < batchSize; i++) {\n const prunedMessage = this.messages.shift();\n if (prunedMessage) {\n prunedMessages.push(prunedMessage);\n }\n }\n \n // Safety check to prevent infinite loop\n if (prunedMessages.length > 1000) {\n console.warn('MemoryWindow: Excessive pruning detected, stopping to prevent infinite loop');\n break;\n }\n }\n\n return prunedMessages;\n }\n\n /**\n * Get current token count including system prompt and messages\n * @returns Current token count\n */\n getCurrentTokenCount(): number {\n const messageTokens = this.tokenCounter.countMessagesTokens(this.messages);\n return this.systemPromptTokens + messageTokens;\n }\n\n /**\n * Get remaining token capacity before hitting the reserve limit\n * @returns Remaining tokens that can be used\n */\n getRemainingTokenCapacity(): number {\n return Math.max(0, this.maxTokens - this.getCurrentTokenCount());\n }\n\n /**\n * Check if a message can be added without exceeding limits\n * @param message - The message to check\n * @returns True if message can be added within reserve limits\n */\n canAddMessage(message: BaseMessage): boolean {\n const messageTokens = this.tokenCounter.countMessageTokens(message);\n const currentTokens = this.getCurrentTokenCount();\n const wouldExceedReserve = (currentTokens + messageTokens) > (this.maxTokens - this.reserveTokens);\n \n // Always allow adding if we can prune to make room\n // Only return false if the message itself is larger than our total capacity\n if (messageTokens > this.maxTokens) {\n return false;\n }\n \n return !wouldExceedReserve || this.messages.length > 0;\n }\n\n /**\n * Get all messages in the memory window\n * @returns Copy of current messages array\n */\n getMessages(): BaseMessage[] {\n return [...this.messages];\n }\n\n /**\n * Clear all messages from the memory window\n */\n clear(): void {\n this.messages = [];\n }\n\n /**\n * Set the system prompt and update token calculations\n * @param systemPrompt - The system prompt text\n */\n setSystemPrompt(systemPrompt: string): void {\n this.systemPrompt = systemPrompt;\n this.systemPromptTokens = this.tokenCounter.estimateSystemPromptTokens(systemPrompt);\n }\n\n /**\n * Get the current system prompt\n * @returns Current system prompt\n */\n getSystemPrompt(): string {\n return this.systemPrompt;\n }\n\n /**\n * Get current configuration\n * @returns Memory window configuration\n */\n getConfig() {\n return {\n maxTokens: this.maxTokens,\n reserveTokens: this.reserveTokens,\n currentTokens: this.getCurrentTokenCount(),\n messageCount: this.messages.length,\n systemPromptTokens: this.systemPromptTokens\n };\n }\n\n /**\n * Update token limits\n * @param maxTokens - New maximum token limit\n * @param reserveTokens - New reserve token amount\n */\n updateLimits(maxTokens: number, reserveTokens?: number): void {\n if (reserveTokens !== undefined && reserveTokens >= maxTokens) {\n throw new Error('Reserve tokens must be less than max tokens');\n }\n\n this.maxTokens = maxTokens;\n if (reserveTokens !== undefined) {\n this.reserveTokens = reserveTokens;\n }\n\n // Prune if necessary after updating limits\n if (this.getCurrentTokenCount() > (this.maxTokens - this.reserveTokens)) {\n this.pruneToFit();\n }\n }\n\n /**\n * Get statistics about the memory window\n * @returns Memory usage statistics\n */\n getStats() {\n const currentTokens = this.getCurrentTokenCount();\n const capacity = this.maxTokens;\n const usagePercentage = (currentTokens / capacity) * 100;\n \n return {\n totalMessages: this.messages.length,\n currentTokens,\n maxTokens: capacity,\n reserveTokens: this.reserveTokens,\n systemPromptTokens: this.systemPromptTokens,\n usagePercentage: Math.round(usagePercentage * 100) / 100,\n remainingCapacity: this.getRemainingTokenCapacity(),\n canAcceptMore: this.getRemainingTokenCapacity() > this.reserveTokens\n };\n }\n\n /**\n * Clean up resources\n */\n dispose(): void {\n this.clear();\n this.tokenCounter.dispose();\n }\n}"],"names":[],"mappings":";AAqBO,MAAM,gBAAN,MAAM,cAAa;AAAA;AAAA,EAaxB,YACE,YAAoB,cAAa,oBACjC,gBAAwB,cAAa,wBACrC,cACA;AAhBF,SAAQ,WAA0B,CAAA;AAIlC,SAAQ,eAAuB;AAC/B,SAAQ,qBAA6B;AAYnC,QAAI,iBAAiB,WAAW;AAC9B,YAAM,IAAI,MAAM,6CAA6C;AAAA,IAC/D;AAEA,SAAK,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