@hashgraphonline/conversational-agent 0.2.216 → 0.2.217
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/cli/readme.md +181 -0
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/SwarmPlugin.d.ts +17 -0
- package/dist/cjs/plugins/community/swarm/__tests__/SwarmPlugin.mocks.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/SwarmPlugin.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/CreatePostageStampTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/DownloadDataTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/DownloadFilesTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/ExtendPostageStampTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/GetPostageStampTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/ListPostageStampsTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/QueryUploadProgressTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/ReadFeedTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/SwarmTool.mocks.d.ts +14 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/UpdateFeedTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/UploadDataTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/UploadFileTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/UploadFolderTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/config.d.ts +6 -0
- package/dist/cjs/plugins/community/swarm/constants.d.ts +8 -0
- package/dist/cjs/plugins/community/swarm/index.d.ts +2 -0
- package/dist/cjs/plugins/community/swarm/model.d.ts +23 -0
- package/dist/cjs/plugins/community/swarm/tools/CreatePostageStampTool.d.ts +47 -0
- package/dist/cjs/plugins/community/swarm/tools/DownloadDataTool.d.ts +35 -0
- package/dist/cjs/plugins/community/swarm/tools/DownloadFilesTool.d.ts +41 -0
- package/dist/cjs/plugins/community/swarm/tools/ExtendPostageStampTool.d.ts +47 -0
- package/dist/cjs/plugins/community/swarm/tools/GetPostageStampTool.d.ts +35 -0
- package/dist/cjs/plugins/community/swarm/tools/ListPostageStampsTool.d.ts +53 -0
- package/dist/cjs/plugins/community/swarm/tools/QueryUploadProgressTool.d.ts +35 -0
- package/dist/cjs/plugins/community/swarm/tools/ReadFeedTool.d.ts +41 -0
- package/dist/cjs/plugins/community/swarm/tools/UpdateFeedTool.d.ts +47 -0
- package/dist/cjs/plugins/community/swarm/tools/UploadDataTool.d.ts +47 -0
- package/dist/cjs/plugins/community/swarm/tools/UploadFileTool.d.ts +53 -0
- package/dist/cjs/plugins/community/swarm/tools/UploadFolderTool.d.ts +47 -0
- package/dist/cjs/plugins/community/swarm/utils.d.ts +22 -0
- package/dist/cjs/plugins/index.d.ts +1 -0
- package/dist/esm/index.js +33 -31
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index10.js +13 -677
- package/dist/esm/index10.js.map +1 -1
- package/dist/esm/index11.js +601 -234
- package/dist/esm/index11.js.map +1 -1
- package/dist/esm/index12.js +296 -136
- package/dist/esm/index12.js.map +1 -1
- package/dist/esm/index13.js +127 -235
- package/dist/esm/index13.js.map +1 -1
- package/dist/esm/index14.js +247 -84
- package/dist/esm/index14.js.map +1 -1
- package/dist/esm/index15.js +81 -159
- package/dist/esm/index15.js.map +1 -1
- package/dist/esm/index16.js +155 -229
- package/dist/esm/index16.js.map +1 -1
- package/dist/esm/index17.js +238 -140
- package/dist/esm/index17.js.map +1 -1
- package/dist/esm/index18.js +139 -493
- package/dist/esm/index18.js.map +1 -1
- package/dist/esm/index19.js +479 -91
- package/dist/esm/index19.js.map +1 -1
- package/dist/esm/index20.js +88 -147
- package/dist/esm/index20.js.map +1 -1
- package/dist/esm/index21.js +127 -666
- package/dist/esm/index21.js.map +1 -1
- package/dist/esm/index22.js +698 -44
- package/dist/esm/index22.js.map +1 -1
- package/dist/esm/index23.js +45 -304
- package/dist/esm/index23.js.map +1 -1
- package/dist/esm/index24.js +303 -153
- package/dist/esm/index24.js.map +1 -1
- package/dist/esm/index25.js +150 -117
- package/dist/esm/index25.js.map +1 -1
- package/dist/esm/index26.js +154 -18
- package/dist/esm/index26.js.map +1 -1
- package/dist/esm/index27.js +18 -22
- package/dist/esm/index27.js.map +1 -1
- package/dist/esm/index28.js +15 -74
- package/dist/esm/index28.js.map +1 -1
- package/dist/esm/index29.js +70 -295
- package/dist/esm/index29.js.map +1 -1
- package/dist/esm/index30.js +279 -100
- package/dist/esm/index30.js.map +1 -1
- package/dist/esm/index31.js +86 -922
- package/dist/esm/index31.js.map +1 -1
- package/dist/esm/index32.js +904 -189
- package/dist/esm/index32.js.map +1 -1
- package/dist/esm/index33.js +185 -1169
- package/dist/esm/index33.js.map +1 -1
- package/dist/esm/index34.js +1218 -112
- package/dist/esm/index34.js.map +1 -1
- package/dist/esm/index35.js +111 -99
- package/dist/esm/index35.js.map +1 -1
- package/dist/esm/index36.js +113 -8
- package/dist/esm/index36.js.map +1 -1
- package/dist/esm/index37.js +8 -45
- package/dist/esm/index37.js.map +1 -1
- package/dist/esm/index38.js +41 -102
- package/dist/esm/index38.js.map +1 -1
- package/dist/esm/index39.js +96 -55
- package/dist/esm/index39.js.map +1 -1
- package/dist/esm/index4.js +1 -1
- package/dist/esm/index40.js +58 -71
- package/dist/esm/index40.js.map +1 -1
- package/dist/esm/index41.js +79 -21
- package/dist/esm/index41.js.map +1 -1
- package/dist/esm/index42.js +21 -5
- package/dist/esm/index42.js.map +1 -1
- package/dist/esm/index43.js +4 -11
- package/dist/esm/index43.js.map +1 -1
- package/dist/esm/index44.js +12 -322
- package/dist/esm/index44.js.map +1 -1
- package/dist/esm/index45.js +280 -142
- package/dist/esm/index45.js.map +1 -1
- package/dist/esm/index46.js +181 -24
- package/dist/esm/index46.js.map +1 -1
- package/dist/esm/index47.js +7 -95
- package/dist/esm/index47.js.map +1 -1
- package/dist/esm/index48.js +77 -7
- package/dist/esm/index48.js.map +1 -1
- package/dist/esm/index49.js +75 -0
- package/dist/esm/index49.js.map +1 -0
- package/dist/esm/index5.js +2 -2
- package/dist/esm/index50.js +57 -0
- package/dist/esm/index50.js.map +1 -0
- package/dist/esm/index51.js +103 -0
- package/dist/esm/index51.js.map +1 -0
- package/dist/esm/index52.js +79 -0
- package/dist/esm/index52.js.map +1 -0
- package/dist/esm/index53.js +75 -0
- package/dist/esm/index53.js.map +1 -0
- package/dist/esm/index54.js +124 -0
- package/dist/esm/index54.js.map +1 -0
- package/dist/esm/index55.js +58 -0
- package/dist/esm/index55.js.map +1 -0
- package/dist/esm/index56.js +83 -0
- package/dist/esm/index56.js.map +1 -0
- package/dist/esm/index57.js +100 -0
- package/dist/esm/index57.js.map +1 -0
- package/dist/esm/index58.js +118 -0
- package/dist/esm/index58.js.map +1 -0
- package/dist/esm/index59.js +108 -0
- package/dist/esm/index59.js.map +1 -0
- package/dist/esm/index6.js +132 -833
- package/dist/esm/index6.js.map +1 -1
- package/dist/esm/index60.js +30 -0
- package/dist/esm/index60.js.map +1 -0
- package/dist/esm/index61.js +98 -0
- package/dist/esm/index61.js.map +1 -0
- package/dist/esm/index62.js +131 -0
- package/dist/esm/index62.js.map +1 -0
- package/dist/esm/index63.js +19 -0
- package/dist/esm/index63.js.map +1 -0
- package/dist/esm/index7.js +826 -75
- package/dist/esm/index7.js.map +1 -1
- package/dist/esm/index8.js +91 -13
- package/dist/esm/index8.js.map +1 -1
- package/dist/esm/index9.js +13 -17
- package/dist/esm/index9.js.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/plugins/community/swarm/SwarmPlugin.d.ts +17 -0
- package/dist/types/plugins/community/swarm/__tests__/SwarmPlugin.mocks.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/SwarmPlugin.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/CreatePostageStampTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/DownloadDataTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/DownloadFilesTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/ExtendPostageStampTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/GetPostageStampTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/ListPostageStampsTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/QueryUploadProgressTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/ReadFeedTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/SwarmTool.mocks.d.ts +14 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/UpdateFeedTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/UploadDataTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/UploadFileTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/UploadFolderTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/config.d.ts +6 -0
- package/dist/types/plugins/community/swarm/constants.d.ts +8 -0
- package/dist/types/plugins/community/swarm/index.d.ts +2 -0
- package/dist/types/plugins/community/swarm/model.d.ts +23 -0
- package/dist/types/plugins/community/swarm/tools/CreatePostageStampTool.d.ts +47 -0
- package/dist/types/plugins/community/swarm/tools/DownloadDataTool.d.ts +35 -0
- package/dist/types/plugins/community/swarm/tools/DownloadFilesTool.d.ts +41 -0
- package/dist/types/plugins/community/swarm/tools/ExtendPostageStampTool.d.ts +47 -0
- package/dist/types/plugins/community/swarm/tools/GetPostageStampTool.d.ts +35 -0
- package/dist/types/plugins/community/swarm/tools/ListPostageStampsTool.d.ts +53 -0
- package/dist/types/plugins/community/swarm/tools/QueryUploadProgressTool.d.ts +35 -0
- package/dist/types/plugins/community/swarm/tools/ReadFeedTool.d.ts +41 -0
- package/dist/types/plugins/community/swarm/tools/UpdateFeedTool.d.ts +47 -0
- package/dist/types/plugins/community/swarm/tools/UploadDataTool.d.ts +47 -0
- package/dist/types/plugins/community/swarm/tools/UploadFileTool.d.ts +53 -0
- package/dist/types/plugins/community/swarm/tools/UploadFolderTool.d.ts +47 -0
- package/dist/types/plugins/community/swarm/utils.d.ts +22 -0
- package/dist/types/plugins/index.d.ts +1 -0
- package/package.json +36 -26
- package/src/index.ts +1 -0
- package/src/plugins/community/swarm/README.md +279 -0
- package/src/plugins/community/swarm/SwarmPlugin.ts +178 -0
- package/src/plugins/community/swarm/__tests__/SwarmPlugin.mocks.ts +105 -0
- package/src/plugins/community/swarm/__tests__/SwarmPlugin.test.ts +93 -0
- package/src/plugins/community/swarm/__tests__/tools/CreatePostageStampTool.test.ts +152 -0
- package/src/plugins/community/swarm/__tests__/tools/DownloadDataTool.test.ts +93 -0
- package/src/plugins/community/swarm/__tests__/tools/DownloadFilesTool.test.ts +163 -0
- package/src/plugins/community/swarm/__tests__/tools/ExtendPostageStampTool.test.ts +132 -0
- package/src/plugins/community/swarm/__tests__/tools/GetPostageStampTool.test.ts +83 -0
- package/src/plugins/community/swarm/__tests__/tools/ListPostageStampsTool.test.ts +219 -0
- package/src/plugins/community/swarm/__tests__/tools/QueryUploadProgressTool.test.ts +169 -0
- package/src/plugins/community/swarm/__tests__/tools/ReadFeedTool.test.ts +133 -0
- package/src/plugins/community/swarm/__tests__/tools/SwarmTool.mocks.ts +67 -0
- package/src/plugins/community/swarm/__tests__/tools/UpdateFeedTool.test.ts +148 -0
- package/src/plugins/community/swarm/__tests__/tools/UploadDataTool.test.ts +125 -0
- package/src/plugins/community/swarm/__tests__/tools/UploadFileTool.test.ts +194 -0
- package/src/plugins/community/swarm/__tests__/tools/UploadFolderTool.test.ts +118 -0
- package/src/plugins/community/swarm/config.ts +6 -0
- package/src/plugins/community/swarm/constants.ts +12 -0
- package/src/plugins/community/swarm/index.ts +2 -0
- package/src/plugins/community/swarm/model.ts +23 -0
- package/src/plugins/community/swarm/tools/CreatePostageStampTool.ts +137 -0
- package/src/plugins/community/swarm/tools/DownloadDataTool.ts +79 -0
- package/src/plugins/community/swarm/tools/DownloadFilesTool.ts +155 -0
- package/src/plugins/community/swarm/tools/ExtendPostageStampTool.ts +112 -0
- package/src/plugins/community/swarm/tools/GetPostageStampTool.ts +92 -0
- package/src/plugins/community/swarm/tools/ListPostageStampsTool.ts +124 -0
- package/src/plugins/community/swarm/tools/QueryUploadProgressTool.ts +109 -0
- package/src/plugins/community/swarm/tools/ReadFeedTool.ts +110 -0
- package/src/plugins/community/swarm/tools/UpdateFeedTool.ts +149 -0
- package/src/plugins/community/swarm/tools/UploadDataTool.ts +109 -0
- package/src/plugins/community/swarm/tools/UploadFileTool.ts +163 -0
- package/src/plugins/community/swarm/tools/UploadFolderTool.ts +150 -0
- package/src/plugins/community/swarm/utils.ts +172 -0
- package/src/plugins/index.ts +1 -0
- package/cli/dist/CLIApp.d.ts +0 -11
- package/cli/dist/CLIApp.d.ts.map +0 -1
- package/cli/dist/CLIApp.js +0 -128
- package/cli/dist/CLIApp.js.map +0 -1
- package/cli/dist/LocalConversationalAgent.d.ts +0 -37
- package/cli/dist/LocalConversationalAgent.js +0 -58
- package/cli/dist/app.d.ts +0 -18
- package/cli/dist/app.d.ts.map +0 -1
- package/cli/dist/app.js +0 -14
- package/cli/dist/app.js.map +0 -1
- package/cli/dist/cli.d.ts +0 -3
- package/cli/dist/cli.d.ts.map +0 -1
- package/cli/dist/cli.js +0 -87
- package/cli/dist/cli.js.map +0 -1
- package/cli/dist/components/AppContainer.d.ts +0 -16
- package/cli/dist/components/AppContainer.js +0 -24
- package/cli/dist/components/AppScreens.d.ts +0 -2
- package/cli/dist/components/AppScreens.js +0 -259
- package/cli/dist/components/ChatScreen.d.ts +0 -21
- package/cli/dist/components/ChatScreen.d.ts.map +0 -1
- package/cli/dist/components/ChatScreen.js +0 -40
- package/cli/dist/components/ChatScreen.js.map +0 -1
- package/cli/dist/components/DebugLoadingScreen.d.ts +0 -5
- package/cli/dist/components/DebugLoadingScreen.js +0 -31
- package/cli/dist/components/LoadingScreen.d.ts +0 -3
- package/cli/dist/components/LoadingScreen.d.ts.map +0 -1
- package/cli/dist/components/LoadingScreen.js +0 -17
- package/cli/dist/components/LoadingScreen.js.map +0 -1
- package/cli/dist/components/LoadingScreenDebug.d.ts +0 -5
- package/cli/dist/components/LoadingScreenDebug.js +0 -27
- package/cli/dist/components/MCPConfigScreen.d.ts +0 -28
- package/cli/dist/components/MCPConfigScreen.d.ts.map +0 -1
- package/cli/dist/components/MCPConfigScreen.js +0 -186
- package/cli/dist/components/MCPConfigScreen.js.map +0 -1
- package/cli/dist/components/ScreenRouter.d.ts +0 -13
- package/cli/dist/components/ScreenRouter.d.ts.map +0 -1
- package/cli/dist/components/ScreenRouter.js +0 -23
- package/cli/dist/components/ScreenRouter.js.map +0 -1
- package/cli/dist/components/SetupScreen.d.ts +0 -16
- package/cli/dist/components/SetupScreen.d.ts.map +0 -1
- package/cli/dist/components/SetupScreen.js +0 -67
- package/cli/dist/components/SetupScreen.js.map +0 -1
- package/cli/dist/components/SingleLoadingScreen.d.ts +0 -5
- package/cli/dist/components/SingleLoadingScreen.js +0 -27
- package/cli/dist/components/StatusBadge.d.ts +0 -10
- package/cli/dist/components/StatusBadge.d.ts.map +0 -1
- package/cli/dist/components/StatusBadge.js +0 -24
- package/cli/dist/components/StatusBadge.js.map +0 -1
- package/cli/dist/components/TerminalWindow.d.ts +0 -9
- package/cli/dist/components/TerminalWindow.d.ts.map +0 -1
- package/cli/dist/components/TerminalWindow.js +0 -19
- package/cli/dist/components/TerminalWindow.js.map +0 -1
- package/cli/dist/components/WelcomeScreen.d.ts +0 -12
- package/cli/dist/components/WelcomeScreen.d.ts.map +0 -1
- package/cli/dist/components/WelcomeScreen.js +0 -47
- package/cli/dist/components/WelcomeScreen.js.map +0 -1
- package/cli/dist/context/AppContext.d.ts +0 -68
- package/cli/dist/context/AppContext.js +0 -363
- package/cli/dist/headless-runner.d.ts +0 -17
- package/cli/dist/headless-runner.d.ts.map +0 -1
- package/cli/dist/headless-runner.js +0 -128
- package/cli/dist/headless-runner.js.map +0 -1
- package/cli/dist/hooks/useInitializeAgent.d.ts +0 -19
- package/cli/dist/hooks/useInitializeAgent.d.ts.map +0 -1
- package/cli/dist/hooks/useInitializeAgent.js +0 -29
- package/cli/dist/hooks/useInitializeAgent.js.map +0 -1
- package/cli/dist/hooks/useStableState.d.ts +0 -38
- package/cli/dist/hooks/useStableState.d.ts.map +0 -1
- package/cli/dist/hooks/useStableState.js +0 -69
- package/cli/dist/hooks/useStableState.js.map +0 -1
- package/cli/dist/managers/AgentManager.d.ts +0 -58
- package/cli/dist/managers/AgentManager.d.ts.map +0 -1
- package/cli/dist/managers/AgentManager.js +0 -121
- package/cli/dist/managers/AgentManager.js.map +0 -1
- package/cli/dist/managers/ConfigManager.d.ts +0 -54
- package/cli/dist/managers/ConfigManager.d.ts.map +0 -1
- package/cli/dist/managers/ConfigManager.js +0 -188
- package/cli/dist/managers/ConfigManager.js.map +0 -1
- package/cli/dist/types.d.ts +0 -52
- package/cli/dist/types.d.ts.map +0 -1
- package/cli/dist/types.js +0 -34
- package/cli/dist/types.js.map +0 -1
package/dist/esm/index22.js
CHANGED
|
@@ -1,68 +1,722 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { ReferenceIdGenerator } from "./index23.js";
|
|
2
|
+
import { DEFAULT_CONTENT_REFERENCE_CONFIG, ContentReferenceError } from "./index60.js";
|
|
3
|
+
const _ContentStorage = class _ContentStorage {
|
|
4
|
+
constructor(maxStorage = _ContentStorage.DEFAULT_MAX_STORAGE, referenceConfig) {
|
|
5
|
+
this.messages = [];
|
|
6
|
+
this.idCounter = 0;
|
|
7
|
+
this.contentStore = /* @__PURE__ */ new Map();
|
|
8
|
+
this.maxStorage = maxStorage;
|
|
9
|
+
this.referenceConfig = {
|
|
10
|
+
...DEFAULT_CONTENT_REFERENCE_CONFIG,
|
|
11
|
+
...referenceConfig
|
|
12
|
+
};
|
|
13
|
+
this.referenceStats = {
|
|
14
|
+
activeReferences: 0,
|
|
15
|
+
totalStorageBytes: 0,
|
|
16
|
+
recentlyCleanedUp: 0,
|
|
17
|
+
totalResolutions: 0,
|
|
18
|
+
failedResolutions: 0,
|
|
19
|
+
averageContentSize: 0,
|
|
20
|
+
storageUtilization: 0,
|
|
21
|
+
performanceMetrics: {
|
|
22
|
+
averageCreationTimeMs: 0,
|
|
23
|
+
averageResolutionTimeMs: 0,
|
|
24
|
+
averageCleanupTimeMs: 0,
|
|
25
|
+
creationTimes: [],
|
|
26
|
+
resolutionTimes: [],
|
|
27
|
+
cleanupTimes: []
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
if (this.referenceConfig.enableAutoCleanup) {
|
|
31
|
+
this.startReferenceCleanupTimer();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Store messages in the content storage
|
|
36
|
+
* Automatically drops oldest messages if storage limit is exceeded
|
|
37
|
+
* @param messages - Messages to store
|
|
38
|
+
* @returns Result indicating how many messages were stored and dropped
|
|
39
|
+
*/
|
|
40
|
+
storeMessages(messages) {
|
|
41
|
+
if (messages.length === 0) {
|
|
42
|
+
return { stored: 0, dropped: 0 };
|
|
43
|
+
}
|
|
44
|
+
const now = /* @__PURE__ */ new Date();
|
|
45
|
+
let dropped = 0;
|
|
46
|
+
const storedMessages = messages.map((message) => ({
|
|
47
|
+
message,
|
|
48
|
+
storedAt: now,
|
|
49
|
+
id: this.generateId()
|
|
50
|
+
}));
|
|
51
|
+
this.messages.push(...storedMessages);
|
|
52
|
+
while (this.messages.length > this.maxStorage) {
|
|
53
|
+
this.messages.shift();
|
|
54
|
+
dropped++;
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
stored: storedMessages.length,
|
|
58
|
+
dropped
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Get the most recent messages from storage
|
|
63
|
+
* @param count - Number of recent messages to retrieve
|
|
64
|
+
* @returns Array of recent messages in chronological order
|
|
65
|
+
*/
|
|
66
|
+
getRecentMessages(count) {
|
|
67
|
+
if (count <= 0 || this.messages.length === 0) {
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
const startIndex = Math.max(0, this.messages.length - count);
|
|
71
|
+
return this.messages.slice(startIndex).map((stored) => stored.message);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Search for messages containing specific text or patterns
|
|
75
|
+
* @param query - Search term or regex pattern
|
|
76
|
+
* @param options - Search configuration options
|
|
77
|
+
* @returns Array of matching messages
|
|
78
|
+
*/
|
|
79
|
+
searchMessages(query, options = {}) {
|
|
80
|
+
if (!query || this.messages.length === 0) {
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
const { caseSensitive = false, limit, useRegex = false } = options;
|
|
84
|
+
let matches = [];
|
|
85
|
+
if (useRegex) {
|
|
86
|
+
try {
|
|
87
|
+
const regex = new RegExp(query, caseSensitive ? "g" : "gi");
|
|
88
|
+
matches = this.messages.filter((stored) => regex.test(stored.message.content)).map((stored) => stored.message);
|
|
89
|
+
} catch {
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
const searchTerm = caseSensitive ? query : query.toLowerCase();
|
|
94
|
+
matches = this.messages.filter((stored) => {
|
|
95
|
+
const content = stored.message.content;
|
|
96
|
+
const searchContent = caseSensitive ? content : content.toLowerCase();
|
|
97
|
+
return searchContent.includes(searchTerm);
|
|
98
|
+
}).map((stored) => stored.message);
|
|
99
|
+
}
|
|
100
|
+
return limit ? matches.slice(0, limit) : matches;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Get messages from a specific time range
|
|
104
|
+
* @param startTime - Start of time range (inclusive)
|
|
105
|
+
* @param endTime - End of time range (inclusive)
|
|
106
|
+
* @returns Array of messages within the time range
|
|
107
|
+
*/
|
|
108
|
+
getMessagesFromTimeRange(startTime, endTime) {
|
|
109
|
+
if (startTime > endTime || this.messages.length === 0) {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
return this.messages.filter(
|
|
113
|
+
(stored) => stored.storedAt >= startTime && stored.storedAt <= endTime
|
|
114
|
+
).map((stored) => stored.message);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Get storage statistics and usage information
|
|
118
|
+
* @returns Current storage statistics
|
|
119
|
+
*/
|
|
120
|
+
getStorageStats() {
|
|
121
|
+
const totalMessages = this.messages.length;
|
|
122
|
+
const usagePercentage = totalMessages > 0 ? Math.round(totalMessages / this.maxStorage * 100) : 0;
|
|
123
|
+
let oldestMessageTime;
|
|
124
|
+
let newestMessageTime;
|
|
125
|
+
if (totalMessages > 0) {
|
|
126
|
+
oldestMessageTime = this.messages[0].storedAt;
|
|
127
|
+
newestMessageTime = this.messages[totalMessages - 1].storedAt;
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
totalMessages,
|
|
131
|
+
maxStorageLimit: this.maxStorage,
|
|
132
|
+
usagePercentage,
|
|
133
|
+
oldestMessageTime,
|
|
134
|
+
newestMessageTime
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Clear all stored messages
|
|
139
|
+
*/
|
|
140
|
+
clear() {
|
|
141
|
+
this.messages = [];
|
|
142
|
+
this.idCounter = 0;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Get total number of stored messages
|
|
146
|
+
* @returns Number of messages currently in storage
|
|
147
|
+
*/
|
|
148
|
+
getTotalStoredMessages() {
|
|
149
|
+
return this.messages.length;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Update the maximum storage limit
|
|
153
|
+
* @param newLimit - New maximum storage limit
|
|
154
|
+
*/
|
|
155
|
+
updateStorageLimit(newLimit) {
|
|
156
|
+
if (newLimit <= 0) {
|
|
157
|
+
throw new Error("Storage limit must be greater than 0");
|
|
158
|
+
}
|
|
159
|
+
this.maxStorage = newLimit;
|
|
160
|
+
while (this.messages.length > this.maxStorage) {
|
|
161
|
+
this.messages.shift();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get messages by message type
|
|
166
|
+
* @param messageType - Type of messages to retrieve ('human', 'ai', 'system', etc.)
|
|
167
|
+
* @param limit - Maximum number of messages to return
|
|
168
|
+
* @returns Array of messages of the specified type
|
|
169
|
+
*/
|
|
170
|
+
getMessagesByType(messageType, limit) {
|
|
171
|
+
const filtered = this.messages.filter((stored) => stored.message._getType() === messageType).map((stored) => stored.message);
|
|
172
|
+
return limit ? filtered.slice(0, limit) : filtered;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Get the current storage configuration
|
|
176
|
+
* @returns Storage configuration object
|
|
177
|
+
*/
|
|
178
|
+
getConfig() {
|
|
179
|
+
return {
|
|
180
|
+
maxStorage: this.maxStorage,
|
|
181
|
+
currentUsage: this.messages.length,
|
|
182
|
+
utilizationPercentage: this.messages.length / this.maxStorage * 100
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Generate a unique ID for stored messages
|
|
187
|
+
* @returns Unique string identifier
|
|
188
|
+
*/
|
|
189
|
+
generateId() {
|
|
190
|
+
return `msg_${++this.idCounter}_${Date.now()}`;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Get messages stored within the last N minutes
|
|
194
|
+
* @param minutes - Number of minutes to look back
|
|
195
|
+
* @returns Array of messages from the last N minutes
|
|
196
|
+
*/
|
|
197
|
+
getRecentMessagesByTime(minutes) {
|
|
198
|
+
if (minutes <= 0 || this.messages.length === 0) {
|
|
199
|
+
return [];
|
|
200
|
+
}
|
|
201
|
+
const cutoffTime = new Date(Date.now() - minutes * 60 * 1e3);
|
|
202
|
+
return this.messages.filter((stored) => stored.storedAt >= cutoffTime).map((stored) => stored.message);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Export messages to a JSON-serializable format
|
|
206
|
+
* @returns Serializable representation of stored messages
|
|
207
|
+
*/
|
|
208
|
+
exportMessages() {
|
|
209
|
+
return this.messages.map((stored) => ({
|
|
210
|
+
content: typeof stored.message.content === "string" ? stored.message.content : JSON.stringify(stored.message.content),
|
|
211
|
+
type: stored.message._getType(),
|
|
212
|
+
storedAt: stored.storedAt.toISOString(),
|
|
213
|
+
id: stored.id
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
3
216
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @param content The content to generate a reference ID for
|
|
7
|
-
* @returns Deterministic reference ID based on content hash
|
|
217
|
+
* Determine if content should be stored as a reference based on size
|
|
8
218
|
*/
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
return hash.digest("hex");
|
|
219
|
+
shouldUseReference(content) {
|
|
220
|
+
const size = Buffer.isBuffer(content) ? content.length : Buffer.byteLength(content, "utf8");
|
|
221
|
+
return size > this.referenceConfig.sizeThresholdBytes;
|
|
13
222
|
}
|
|
14
223
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
224
|
+
* Store content and return a reference if it exceeds the size threshold
|
|
225
|
+
* Otherwise returns null to indicate direct content should be used
|
|
226
|
+
*
|
|
227
|
+
* Special case: Image files are ALWAYS stored as references regardless of size
|
|
228
|
+
* because they need special handling for inscription tools
|
|
19
229
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
230
|
+
async storeContentIfLarge(content, metadata) {
|
|
231
|
+
const buffer = Buffer.isBuffer(content) ? content : Buffer.from(content, "utf8");
|
|
232
|
+
const isImageFile = this.isImageContent(
|
|
233
|
+
metadata.mimeType,
|
|
234
|
+
metadata.fileName
|
|
235
|
+
);
|
|
236
|
+
if (!isImageFile && !this.shouldUseReference(buffer)) {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
const storeMetadata = {
|
|
240
|
+
contentType: metadata.contentType || this.detectContentType(buffer, metadata.mimeType),
|
|
241
|
+
sizeBytes: buffer.length,
|
|
242
|
+
source: metadata.source,
|
|
243
|
+
tags: []
|
|
244
|
+
};
|
|
245
|
+
if (metadata.mimeType !== void 0) {
|
|
246
|
+
storeMetadata.mimeType = metadata.mimeType;
|
|
247
|
+
}
|
|
248
|
+
if (metadata.mcpToolName !== void 0) {
|
|
249
|
+
storeMetadata.mcpToolName = metadata.mcpToolName;
|
|
250
|
+
}
|
|
251
|
+
if (metadata.fileName !== void 0) {
|
|
252
|
+
storeMetadata.fileName = metadata.fileName;
|
|
253
|
+
}
|
|
254
|
+
if (metadata.tags !== void 0) {
|
|
255
|
+
storeMetadata.tags = metadata.tags;
|
|
256
|
+
}
|
|
257
|
+
if (metadata.customMetadata !== void 0) {
|
|
258
|
+
storeMetadata.customMetadata = metadata.customMetadata;
|
|
259
|
+
}
|
|
260
|
+
return await this.storeContent(buffer, storeMetadata);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Store content and return a reference (implements ContentReferenceStore)
|
|
264
|
+
*/
|
|
265
|
+
async storeContent(content, metadata) {
|
|
266
|
+
const startTime = Date.now();
|
|
267
|
+
try {
|
|
268
|
+
const now = /* @__PURE__ */ new Date();
|
|
269
|
+
const referenceId = ReferenceIdGenerator.generateId(content);
|
|
270
|
+
const fullMetadata = {
|
|
271
|
+
...metadata,
|
|
272
|
+
createdAt: now,
|
|
273
|
+
lastAccessedAt: now,
|
|
274
|
+
accessCount: 0
|
|
275
|
+
};
|
|
276
|
+
const storedContent = {
|
|
277
|
+
content,
|
|
278
|
+
metadata: fullMetadata,
|
|
279
|
+
state: "active"
|
|
280
|
+
};
|
|
281
|
+
const expirationTime = this.calculateExpirationTime(metadata.source);
|
|
282
|
+
if (expirationTime !== void 0) {
|
|
283
|
+
storedContent.expiresAt = expirationTime;
|
|
284
|
+
}
|
|
285
|
+
this.contentStore.set(referenceId, storedContent);
|
|
286
|
+
this.updateStatsAfterStore(content.length);
|
|
287
|
+
await this.enforceReferenceStorageLimits();
|
|
288
|
+
const preview = this.createContentPreview(
|
|
289
|
+
content,
|
|
290
|
+
fullMetadata.contentType
|
|
291
|
+
);
|
|
292
|
+
const referenceMetadata = {
|
|
293
|
+
contentType: fullMetadata.contentType,
|
|
294
|
+
sizeBytes: fullMetadata.sizeBytes,
|
|
295
|
+
source: fullMetadata.source
|
|
296
|
+
};
|
|
297
|
+
if (fullMetadata.fileName !== void 0) {
|
|
298
|
+
referenceMetadata.fileName = fullMetadata.fileName;
|
|
299
|
+
}
|
|
300
|
+
if (fullMetadata.mimeType !== void 0) {
|
|
301
|
+
referenceMetadata.mimeType = fullMetadata.mimeType;
|
|
302
|
+
}
|
|
303
|
+
const reference = {
|
|
304
|
+
referenceId,
|
|
305
|
+
state: "active",
|
|
306
|
+
preview,
|
|
307
|
+
metadata: referenceMetadata,
|
|
308
|
+
createdAt: now,
|
|
309
|
+
format: "ref://{id}"
|
|
310
|
+
};
|
|
311
|
+
const duration = Date.now() - startTime;
|
|
312
|
+
this.recordPerformanceMetric("creation", duration);
|
|
313
|
+
return reference;
|
|
314
|
+
} catch (error) {
|
|
315
|
+
const duration = Date.now() - startTime;
|
|
316
|
+
this.recordPerformanceMetric("creation", duration);
|
|
317
|
+
throw new ContentReferenceError(
|
|
318
|
+
`Failed to store content: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
319
|
+
"system_error",
|
|
320
|
+
void 0,
|
|
321
|
+
["Try again", "Check storage limits", "Contact administrator"]
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Resolve a reference to its content (implements ContentReferenceStore)
|
|
327
|
+
*/
|
|
328
|
+
async resolveReference(referenceId) {
|
|
329
|
+
const startTime = Date.now();
|
|
330
|
+
try {
|
|
331
|
+
if (!ReferenceIdGenerator.isValidReferenceId(referenceId)) {
|
|
332
|
+
this.referenceStats.failedResolutions++;
|
|
333
|
+
return {
|
|
334
|
+
success: false,
|
|
335
|
+
error: "Invalid reference ID format",
|
|
336
|
+
errorType: "not_found",
|
|
337
|
+
suggestedActions: [
|
|
338
|
+
"Check the reference ID format",
|
|
339
|
+
"Ensure the reference ID is complete"
|
|
340
|
+
]
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
const storedContent = this.contentStore.get(referenceId);
|
|
344
|
+
if (!storedContent) {
|
|
345
|
+
this.referenceStats.failedResolutions++;
|
|
346
|
+
return {
|
|
347
|
+
success: false,
|
|
348
|
+
error: "Reference not found",
|
|
349
|
+
errorType: "not_found",
|
|
350
|
+
suggestedActions: [
|
|
351
|
+
"Verify the reference ID",
|
|
352
|
+
"Check if the content has expired",
|
|
353
|
+
"Request fresh content"
|
|
354
|
+
]
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
if (storedContent.expiresAt && storedContent.expiresAt < /* @__PURE__ */ new Date()) {
|
|
358
|
+
storedContent.state = "expired";
|
|
359
|
+
this.referenceStats.failedResolutions++;
|
|
360
|
+
return {
|
|
361
|
+
success: false,
|
|
362
|
+
error: "Reference has expired",
|
|
363
|
+
errorType: "expired",
|
|
364
|
+
suggestedActions: [
|
|
365
|
+
"Request fresh content",
|
|
366
|
+
"Use alternative content source"
|
|
367
|
+
]
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
if (storedContent.state !== "active") {
|
|
371
|
+
this.referenceStats.failedResolutions++;
|
|
372
|
+
return {
|
|
373
|
+
success: false,
|
|
374
|
+
error: `Reference is ${storedContent.state}`,
|
|
375
|
+
errorType: storedContent.state === "expired" ? "expired" : "corrupted",
|
|
376
|
+
suggestedActions: [
|
|
377
|
+
"Request fresh content",
|
|
378
|
+
"Check reference validity"
|
|
379
|
+
]
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
storedContent.metadata.lastAccessedAt = /* @__PURE__ */ new Date();
|
|
383
|
+
storedContent.metadata.accessCount++;
|
|
384
|
+
this.referenceStats.totalResolutions++;
|
|
385
|
+
const duration = Date.now() - startTime;
|
|
386
|
+
this.recordPerformanceMetric("resolution", duration);
|
|
387
|
+
return {
|
|
388
|
+
success: true,
|
|
389
|
+
content: storedContent.content,
|
|
390
|
+
metadata: storedContent.metadata
|
|
391
|
+
};
|
|
392
|
+
} catch (error) {
|
|
393
|
+
const duration = Date.now() - startTime;
|
|
394
|
+
this.recordPerformanceMetric("resolution", duration);
|
|
395
|
+
this.referenceStats.failedResolutions++;
|
|
396
|
+
return {
|
|
397
|
+
success: false,
|
|
398
|
+
error: `System error resolving reference: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
399
|
+
errorType: "system_error",
|
|
400
|
+
suggestedActions: ["Try again", "Contact administrator"]
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Check if a reference exists and is valid
|
|
406
|
+
*/
|
|
407
|
+
async hasReference(referenceId) {
|
|
408
|
+
if (!ReferenceIdGenerator.isValidReferenceId(referenceId)) {
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
const storedContent = this.contentStore.get(referenceId);
|
|
412
|
+
if (!storedContent) {
|
|
22
413
|
return false;
|
|
23
414
|
}
|
|
24
|
-
if (
|
|
415
|
+
if (storedContent.expiresAt && storedContent.expiresAt < /* @__PURE__ */ new Date()) {
|
|
416
|
+
storedContent.state = "expired";
|
|
25
417
|
return false;
|
|
26
418
|
}
|
|
27
|
-
return
|
|
419
|
+
return storedContent.state === "active";
|
|
28
420
|
}
|
|
29
421
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* @param input Input string that may contain a reference ID
|
|
33
|
-
* @returns Extracted reference ID or null if not found
|
|
422
|
+
* Mark a reference for cleanup
|
|
34
423
|
*/
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
424
|
+
async cleanupReference(referenceId) {
|
|
425
|
+
const storedContent = this.contentStore.get(referenceId);
|
|
426
|
+
if (!storedContent) {
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
this.referenceStats.totalStorageBytes -= storedContent.content.length;
|
|
430
|
+
this.referenceStats.activeReferences--;
|
|
431
|
+
this.referenceStats.recentlyCleanedUp++;
|
|
432
|
+
this.contentStore.delete(referenceId);
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Get current reference storage statistics (implements ContentReferenceStore)
|
|
437
|
+
*/
|
|
438
|
+
async getStats() {
|
|
439
|
+
this.updateReferenceStorageStats();
|
|
440
|
+
return {
|
|
441
|
+
...this.referenceStats,
|
|
442
|
+
performanceMetrics: {
|
|
443
|
+
averageCreationTimeMs: this.calculateAverage(
|
|
444
|
+
this.referenceStats.performanceMetrics.creationTimes
|
|
445
|
+
),
|
|
446
|
+
averageResolutionTimeMs: this.calculateAverage(
|
|
447
|
+
this.referenceStats.performanceMetrics.resolutionTimes
|
|
448
|
+
),
|
|
449
|
+
averageCleanupTimeMs: this.calculateAverage(
|
|
450
|
+
this.referenceStats.performanceMetrics.cleanupTimes
|
|
451
|
+
)
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Update reference configuration
|
|
457
|
+
*/
|
|
458
|
+
async updateConfig(config) {
|
|
459
|
+
this.referenceConfig = { ...this.referenceConfig, ...config };
|
|
460
|
+
if (this.cleanupTimer) {
|
|
461
|
+
clearInterval(this.cleanupTimer);
|
|
462
|
+
delete this.cleanupTimer;
|
|
38
463
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return refFormatMatch[1];
|
|
464
|
+
if (this.referenceConfig.enableAutoCleanup) {
|
|
465
|
+
this.startReferenceCleanupTimer();
|
|
42
466
|
}
|
|
43
|
-
return this.isValidReferenceId(input) ? input : null;
|
|
44
467
|
}
|
|
45
468
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* @param referenceId The reference ID to format
|
|
49
|
-
* @returns Formatted reference string
|
|
469
|
+
* Perform cleanup based on current policies (implements ContentReferenceStore)
|
|
50
470
|
*/
|
|
51
|
-
|
|
52
|
-
|
|
471
|
+
async performCleanup() {
|
|
472
|
+
const startTime = Date.now();
|
|
473
|
+
const errors = [];
|
|
474
|
+
let cleanedUp = 0;
|
|
475
|
+
try {
|
|
476
|
+
const now = /* @__PURE__ */ new Date();
|
|
477
|
+
const toCleanup = [];
|
|
478
|
+
for (const [referenceId, storedContent] of this.contentStore.entries()) {
|
|
479
|
+
let shouldCleanup = false;
|
|
480
|
+
if (storedContent.expiresAt && storedContent.expiresAt < now) {
|
|
481
|
+
shouldCleanup = true;
|
|
482
|
+
storedContent.state = "expired";
|
|
483
|
+
}
|
|
484
|
+
const ageMs = now.getTime() - storedContent.metadata.createdAt.getTime();
|
|
485
|
+
const policy = this.getCleanupPolicy(storedContent.metadata.source);
|
|
486
|
+
if (ageMs > policy.maxAgeMs) {
|
|
487
|
+
shouldCleanup = true;
|
|
488
|
+
}
|
|
489
|
+
if (storedContent.state === "cleanup_pending") {
|
|
490
|
+
shouldCleanup = true;
|
|
491
|
+
}
|
|
492
|
+
if (shouldCleanup) {
|
|
493
|
+
toCleanup.push(referenceId);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
toCleanup.sort((a, b) => {
|
|
497
|
+
const aContent = this.contentStore.get(a);
|
|
498
|
+
const bContent = this.contentStore.get(b);
|
|
499
|
+
const aPriority = this.getCleanupPolicy(
|
|
500
|
+
aContent.metadata.source
|
|
501
|
+
).priority;
|
|
502
|
+
const bPriority = this.getCleanupPolicy(
|
|
503
|
+
bContent.metadata.source
|
|
504
|
+
).priority;
|
|
505
|
+
return bPriority - aPriority;
|
|
506
|
+
});
|
|
507
|
+
for (const referenceId of toCleanup) {
|
|
508
|
+
try {
|
|
509
|
+
const success = await this.cleanupReference(referenceId);
|
|
510
|
+
if (success) {
|
|
511
|
+
cleanedUp++;
|
|
512
|
+
}
|
|
513
|
+
} catch (error) {
|
|
514
|
+
errors.push(
|
|
515
|
+
`Failed to cleanup ${referenceId}: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
if (this.contentStore.size > this.referenceConfig.maxReferences) {
|
|
520
|
+
const sortedByAge = Array.from(this.contentStore.entries()).sort(
|
|
521
|
+
([, a], [, b]) => a.metadata.lastAccessedAt.getTime() - b.metadata.lastAccessedAt.getTime()
|
|
522
|
+
);
|
|
523
|
+
const excessCount = this.contentStore.size - this.referenceConfig.maxReferences;
|
|
524
|
+
for (let i = 0; i < excessCount && i < sortedByAge.length; i++) {
|
|
525
|
+
const [referenceId] = sortedByAge[i];
|
|
526
|
+
try {
|
|
527
|
+
const success = await this.cleanupReference(referenceId);
|
|
528
|
+
if (success) {
|
|
529
|
+
cleanedUp++;
|
|
530
|
+
}
|
|
531
|
+
} catch (error) {
|
|
532
|
+
errors.push(
|
|
533
|
+
`Failed to cleanup excess reference ${referenceId}: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
const duration = Date.now() - startTime;
|
|
539
|
+
this.recordPerformanceMetric("cleanup", duration);
|
|
540
|
+
return { cleanedUp, errors };
|
|
541
|
+
} catch (error) {
|
|
542
|
+
const duration = Date.now() - startTime;
|
|
543
|
+
this.recordPerformanceMetric("cleanup", duration);
|
|
544
|
+
const errorMessage = `Cleanup process failed: ${error instanceof Error ? error.message : "Unknown error"}`;
|
|
545
|
+
errors.push(errorMessage);
|
|
546
|
+
return { cleanedUp, errors };
|
|
547
|
+
}
|
|
53
548
|
}
|
|
54
549
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* @param testSeed A test seed to generate a fake but valid ID format
|
|
58
|
-
* @returns A valid format reference ID for testing
|
|
550
|
+
* Get reference configuration for debugging
|
|
59
551
|
*/
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
552
|
+
getReferenceConfig() {
|
|
553
|
+
return { ...this.referenceConfig };
|
|
554
|
+
}
|
|
555
|
+
async enforceReferenceStorageLimits() {
|
|
556
|
+
if (this.contentStore.size >= this.referenceConfig.maxReferences) {
|
|
557
|
+
await this.performCleanup();
|
|
558
|
+
}
|
|
559
|
+
if (this.referenceStats.totalStorageBytes >= this.referenceConfig.maxTotalStorageBytes) {
|
|
560
|
+
await this.performCleanup();
|
|
561
|
+
}
|
|
63
562
|
}
|
|
64
|
-
|
|
563
|
+
calculateExpirationTime(source) {
|
|
564
|
+
const policy = this.getCleanupPolicy(source);
|
|
565
|
+
return new Date(Date.now() + policy.maxAgeMs);
|
|
566
|
+
}
|
|
567
|
+
getCleanupPolicy(source) {
|
|
568
|
+
switch (source) {
|
|
569
|
+
case "mcp_tool":
|
|
570
|
+
return this.referenceConfig.cleanupPolicies.recent;
|
|
571
|
+
case "user_upload":
|
|
572
|
+
return this.referenceConfig.cleanupPolicies.userContent;
|
|
573
|
+
case "agent_generated":
|
|
574
|
+
return this.referenceConfig.cleanupPolicies.agentGenerated;
|
|
575
|
+
default:
|
|
576
|
+
return this.referenceConfig.cleanupPolicies.default;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
detectContentType(content, mimeType) {
|
|
580
|
+
if (mimeType) {
|
|
581
|
+
if (mimeType === "text/html") return "html";
|
|
582
|
+
if (mimeType === "text/markdown") return "markdown";
|
|
583
|
+
if (mimeType === "application/json") return "json";
|
|
584
|
+
if (mimeType.startsWith("text/")) return "text";
|
|
585
|
+
return "binary";
|
|
586
|
+
}
|
|
587
|
+
const contentStr = content.toString(
|
|
588
|
+
"utf8",
|
|
589
|
+
0,
|
|
590
|
+
Math.min(content.length, 1e3)
|
|
591
|
+
);
|
|
592
|
+
if (contentStr.startsWith("{") || contentStr.startsWith("[")) return "json";
|
|
593
|
+
if (contentStr.includes("<html>") || contentStr.includes("<!DOCTYPE"))
|
|
594
|
+
return "html";
|
|
595
|
+
if (contentStr.includes("#") && contentStr.includes("\n"))
|
|
596
|
+
return "markdown";
|
|
597
|
+
return "text";
|
|
598
|
+
}
|
|
599
|
+
createContentPreview(content, contentType) {
|
|
600
|
+
const maxLength = 200;
|
|
601
|
+
let preview = content.toString(
|
|
602
|
+
"utf8",
|
|
603
|
+
0,
|
|
604
|
+
Math.min(content.length, maxLength * 2)
|
|
605
|
+
);
|
|
606
|
+
if (contentType === "html") {
|
|
607
|
+
preview = preview.replace(/<[^>]*>/g, "").replace(/\s+/g, " ").trim();
|
|
608
|
+
} else if (contentType === "json") {
|
|
609
|
+
try {
|
|
610
|
+
const parsed = JSON.parse(preview);
|
|
611
|
+
preview = JSON.stringify(parsed, null, 0);
|
|
612
|
+
} catch {
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
preview = preview.trim();
|
|
616
|
+
if (preview.length > maxLength) {
|
|
617
|
+
preview = preview.substring(0, maxLength) + "...";
|
|
618
|
+
}
|
|
619
|
+
return preview || "[Binary content]";
|
|
620
|
+
}
|
|
621
|
+
updateStatsAfterStore(sizeBytes) {
|
|
622
|
+
this.referenceStats.activeReferences++;
|
|
623
|
+
this.referenceStats.totalStorageBytes += sizeBytes;
|
|
624
|
+
this.updateReferenceStorageStats();
|
|
625
|
+
}
|
|
626
|
+
updateReferenceStorageStats() {
|
|
627
|
+
if (this.referenceStats.activeReferences > 0) {
|
|
628
|
+
this.referenceStats.averageContentSize = this.referenceStats.totalStorageBytes / this.referenceStats.activeReferences;
|
|
629
|
+
}
|
|
630
|
+
this.referenceStats.storageUtilization = this.referenceStats.totalStorageBytes / this.referenceConfig.maxTotalStorageBytes * 100;
|
|
631
|
+
let mostAccessedId;
|
|
632
|
+
let maxAccess = 0;
|
|
633
|
+
for (const [referenceId, storedContent] of this.contentStore.entries()) {
|
|
634
|
+
if (storedContent.metadata.accessCount > maxAccess) {
|
|
635
|
+
maxAccess = storedContent.metadata.accessCount;
|
|
636
|
+
mostAccessedId = referenceId;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
if (mostAccessedId !== void 0) {
|
|
640
|
+
this.referenceStats.mostAccessedReferenceId = mostAccessedId;
|
|
641
|
+
} else {
|
|
642
|
+
delete this.referenceStats.mostAccessedReferenceId;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Check if content is an image file based on MIME type or filename
|
|
647
|
+
*/
|
|
648
|
+
isImageContent(mimeType, fileName) {
|
|
649
|
+
if (mimeType && mimeType.startsWith("image/")) {
|
|
650
|
+
return true;
|
|
651
|
+
}
|
|
652
|
+
if (fileName) {
|
|
653
|
+
const lowerFileName = fileName.toLowerCase();
|
|
654
|
+
const imageExtensions = [
|
|
655
|
+
".png",
|
|
656
|
+
".jpg",
|
|
657
|
+
".jpeg",
|
|
658
|
+
".gif",
|
|
659
|
+
".bmp",
|
|
660
|
+
".webp",
|
|
661
|
+
".svg",
|
|
662
|
+
".tiff",
|
|
663
|
+
".ico"
|
|
664
|
+
];
|
|
665
|
+
return imageExtensions.some((ext) => lowerFileName.endsWith(ext));
|
|
666
|
+
}
|
|
667
|
+
return false;
|
|
668
|
+
}
|
|
669
|
+
recordPerformanceMetric(type, timeMs) {
|
|
670
|
+
const metrics = this.referenceStats.performanceMetrics;
|
|
671
|
+
const maxRecords = 100;
|
|
672
|
+
switch (type) {
|
|
673
|
+
case "creation":
|
|
674
|
+
metrics.creationTimes.push(timeMs);
|
|
675
|
+
if (metrics.creationTimes.length > maxRecords) {
|
|
676
|
+
metrics.creationTimes.shift();
|
|
677
|
+
}
|
|
678
|
+
break;
|
|
679
|
+
case "resolution":
|
|
680
|
+
metrics.resolutionTimes.push(timeMs);
|
|
681
|
+
if (metrics.resolutionTimes.length > maxRecords) {
|
|
682
|
+
metrics.resolutionTimes.shift();
|
|
683
|
+
}
|
|
684
|
+
break;
|
|
685
|
+
case "cleanup":
|
|
686
|
+
metrics.cleanupTimes.push(timeMs);
|
|
687
|
+
if (metrics.cleanupTimes.length > maxRecords) {
|
|
688
|
+
metrics.cleanupTimes.shift();
|
|
689
|
+
}
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
calculateAverage(times) {
|
|
694
|
+
if (times.length === 0) return 0;
|
|
695
|
+
return times.reduce((sum, time) => sum + time, 0) / times.length;
|
|
696
|
+
}
|
|
697
|
+
startReferenceCleanupTimer() {
|
|
698
|
+
this.cleanupTimer = setInterval(async () => {
|
|
699
|
+
try {
|
|
700
|
+
await this.performCleanup();
|
|
701
|
+
} catch {
|
|
702
|
+
}
|
|
703
|
+
}, this.referenceConfig.cleanupIntervalMs);
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* Clean up resources (enhanced to include reference cleanup)
|
|
707
|
+
*/
|
|
708
|
+
async dispose() {
|
|
709
|
+
if (this.cleanupTimer) {
|
|
710
|
+
clearInterval(this.cleanupTimer);
|
|
711
|
+
delete this.cleanupTimer;
|
|
712
|
+
}
|
|
713
|
+
this.contentStore.clear();
|
|
714
|
+
this.clear();
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
_ContentStorage.DEFAULT_MAX_STORAGE = 1e3;
|
|
718
|
+
let ContentStorage = _ContentStorage;
|
|
65
719
|
export {
|
|
66
|
-
|
|
720
|
+
ContentStorage
|
|
67
721
|
};
|
|
68
722
|
//# sourceMappingURL=index22.js.map
|