@mxf-dev/core 1.0.1 → 2.0.1
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/README.md +18 -0
- package/dist/adapters/FrameworkAdapter.js +1 -1
- package/dist/adapters/FrameworkAdapter.js.map +1 -1
- package/dist/config/ConfigManager.d.ts +9 -8
- package/dist/config/ConfigManager.d.ts.map +1 -1
- package/dist/config/ConfigManager.js +47 -17
- package/dist/config/ConfigManager.js.map +1 -1
- package/dist/config/memory-strata.config.d.ts +25 -0
- package/dist/config/memory-strata.config.d.ts.map +1 -0
- package/dist/config/memory-strata.config.js +107 -0
- package/dist/config/memory-strata.config.js.map +1 -0
- package/dist/constants/ToolNames.d.ts +1 -29
- package/dist/constants/ToolNames.d.ts.map +1 -1
- package/dist/constants/ToolNames.js +4 -17
- package/dist/constants/ToolNames.js.map +1 -1
- package/dist/database/DatabaseAdapterFactory.d.ts +14 -2
- package/dist/database/DatabaseAdapterFactory.d.ts.map +1 -1
- package/dist/database/DatabaseAdapterFactory.js +21 -6
- package/dist/database/DatabaseAdapterFactory.js.map +1 -1
- package/dist/database/adapters/mongodb/MongoBaseRepository.d.ts +7 -1
- package/dist/database/adapters/mongodb/MongoBaseRepository.d.ts.map +1 -1
- package/dist/database/adapters/mongodb/MongoBaseRepository.js +12 -2
- package/dist/database/adapters/mongodb/MongoBaseRepository.js.map +1 -1
- package/dist/events/ClientEventBus.d.ts +16 -77
- package/dist/events/ClientEventBus.d.ts.map +1 -1
- package/dist/events/ClientEventBus.js +38 -311
- package/dist/events/ClientEventBus.js.map +1 -1
- package/dist/events/EventBus.d.ts +1 -1
- package/dist/events/EventBus.d.ts.map +1 -1
- package/dist/events/EventBus.js +5 -4
- package/dist/events/EventBus.js.map +1 -1
- package/dist/events/EventBusBase.d.ts +139 -34
- package/dist/events/EventBusBase.d.ts.map +1 -1
- package/dist/events/EventBusBase.js +366 -88
- package/dist/events/EventBusBase.js.map +1 -1
- package/dist/events/EventNames.d.ts +10 -1
- package/dist/events/EventNames.d.ts.map +1 -1
- package/dist/events/EventNames.js +3 -0
- package/dist/events/EventNames.js.map +1 -1
- package/dist/events/PublicEvents.d.ts +6 -2
- package/dist/events/PublicEvents.d.ts.map +1 -1
- package/dist/events/PublicEvents.js +7 -4
- package/dist/events/PublicEvents.js.map +1 -1
- package/dist/events/ServerEventBus.d.ts +23 -112
- package/dist/events/ServerEventBus.d.ts.map +1 -1
- package/dist/events/ServerEventBus.js +58 -323
- package/dist/events/ServerEventBus.js.map +1 -1
- package/dist/events/event-definitions/LlmBudgetEvents.d.ts +68 -0
- package/dist/events/event-definitions/LlmBudgetEvents.d.ts.map +1 -0
- package/dist/events/event-definitions/LlmBudgetEvents.js +35 -0
- package/dist/events/event-definitions/LlmBudgetEvents.js.map +1 -0
- package/dist/interfaces/IMemoryPersistence.d.ts +41 -0
- package/dist/interfaces/IMemoryPersistence.d.ts.map +1 -0
- package/dist/interfaces/IMemoryPersistence.js +15 -0
- package/dist/interfaces/IMemoryPersistence.js.map +1 -0
- package/dist/middleware/MxpMiddleware.d.ts +8 -1
- package/dist/middleware/MxpMiddleware.d.ts.map +1 -1
- package/dist/middleware/MxpMiddleware.js +42 -20
- package/dist/middleware/MxpMiddleware.js.map +1 -1
- package/dist/models/channelKey.d.ts +1 -0
- package/dist/models/channelKey.d.ts.map +1 -1
- package/dist/models/channelKey.js +5 -0
- package/dist/models/channelKey.js.map +1 -1
- package/dist/models/memoryUtility.d.ts +39 -0
- package/dist/models/memoryUtility.d.ts.map +1 -0
- package/dist/models/memoryUtility.js +39 -0
- package/dist/models/memoryUtility.js.map +1 -0
- package/dist/models/user.d.ts.map +1 -1
- package/dist/models/user.js +23 -1
- package/dist/models/user.js.map +1 -1
- package/dist/mxp/BinaryProtocolLayer.d.ts +24 -8
- package/dist/mxp/BinaryProtocolLayer.d.ts.map +1 -1
- package/dist/mxp/BinaryProtocolLayer.js +90 -103
- package/dist/mxp/BinaryProtocolLayer.js.map +1 -1
- package/dist/mxp/ContextCompressionEngine.d.ts +57 -10
- package/dist/mxp/ContextCompressionEngine.d.ts.map +1 -1
- package/dist/mxp/ContextCompressionEngine.js +170 -128
- package/dist/mxp/ContextCompressionEngine.js.map +1 -1
- package/dist/mxp/MxpTokenOptimizer.d.ts +7 -3
- package/dist/mxp/MxpTokenOptimizer.d.ts.map +1 -1
- package/dist/mxp/MxpTokenOptimizer.js +8 -5
- package/dist/mxp/MxpTokenOptimizer.js.map +1 -1
- package/dist/prompts/MxfAgentSystemPrompt.d.ts +1 -1
- package/dist/prompts/MxfAgentSystemPrompt.d.ts.map +1 -1
- package/dist/prompts/MxfAgentSystemPrompt.js +47 -57
- package/dist/prompts/MxfAgentSystemPrompt.js.map +1 -1
- package/dist/protocols/mcp/ToolError.d.ts +123 -0
- package/dist/protocols/mcp/ToolError.d.ts.map +1 -0
- package/dist/protocols/mcp/ToolError.js +108 -0
- package/dist/protocols/mcp/ToolError.js.map +1 -0
- package/dist/protocols/mcp/defineTool.d.ts +133 -0
- package/dist/protocols/mcp/defineTool.d.ts.map +1 -0
- package/dist/protocols/mcp/defineTool.js +236 -0
- package/dist/protocols/mcp/defineTool.js.map +1 -0
- package/dist/protocols/mcp/security/HttpTargetGuard.d.ts +38 -0
- package/dist/protocols/mcp/security/HttpTargetGuard.d.ts.map +1 -0
- package/dist/protocols/mcp/security/HttpTargetGuard.js +227 -0
- package/dist/protocols/mcp/security/HttpTargetGuard.js.map +1 -0
- package/dist/protocols/mcp/security/McpSecurityGuard.d.ts +9 -1
- package/dist/protocols/mcp/security/McpSecurityGuard.d.ts.map +1 -1
- package/dist/protocols/mcp/security/McpSecurityGuard.js +10 -2
- package/dist/protocols/mcp/security/McpSecurityGuard.js.map +1 -1
- package/dist/protocols/mcp/security/McpToolPolicy.d.ts +81 -0
- package/dist/protocols/mcp/security/McpToolPolicy.d.ts.map +1 -0
- package/dist/protocols/mcp/security/McpToolPolicy.js +202 -0
- package/dist/protocols/mcp/security/McpToolPolicy.js.map +1 -0
- package/dist/protocols/mcp/services/ExternalMcpServerManager.d.ts +51 -18
- package/dist/protocols/mcp/services/ExternalMcpServerManager.d.ts.map +1 -1
- package/dist/protocols/mcp/services/ExternalMcpServerManager.js +256 -283
- package/dist/protocols/mcp/services/ExternalMcpServerManager.js.map +1 -1
- package/dist/protocols/mcp/services/ExternalServerConfigs.d.ts +39 -6
- package/dist/protocols/mcp/services/ExternalServerConfigs.d.ts.map +1 -1
- package/dist/protocols/mcp/services/ExternalServerConfigs.js +76 -18
- package/dist/protocols/mcp/services/ExternalServerConfigs.js.map +1 -1
- package/dist/protocols/mcp/tools/AnalyticsTools.js +1 -1
- package/dist/protocols/mcp/tools/AnalyticsTools.js.map +1 -1
- package/dist/protocols/mcp/tools/CodeAnalysisTools.d.ts.map +1 -1
- package/dist/protocols/mcp/tools/CodeAnalysisTools.js +15 -0
- package/dist/protocols/mcp/tools/CodeAnalysisTools.js.map +1 -1
- package/dist/protocols/mcp/tools/EffectivenessTools.js +3 -3
- package/dist/protocols/mcp/tools/EffectivenessTools.js.map +1 -1
- package/dist/protocols/mcp/tools/GitTools.d.ts +9 -369
- package/dist/protocols/mcp/tools/GitTools.d.ts.map +1 -1
- package/dist/protocols/mcp/tools/GitTools.js +222 -517
- package/dist/protocols/mcp/tools/GitTools.js.map +1 -1
- package/dist/protocols/mcp/tools/InfrastructureTools.d.ts +56 -24
- package/dist/protocols/mcp/tools/InfrastructureTools.d.ts.map +1 -1
- package/dist/protocols/mcp/tools/InfrastructureTools.js +117 -26
- package/dist/protocols/mcp/tools/InfrastructureTools.js.map +1 -1
- package/dist/protocols/mcp/tools/MemorySearchTools.d.ts +0 -3
- package/dist/protocols/mcp/tools/MemorySearchTools.d.ts.map +1 -1
- package/dist/protocols/mcp/tools/MemorySearchTools.js +58 -1
- package/dist/protocols/mcp/tools/MemorySearchTools.js.map +1 -1
- package/dist/protocols/mcp/tools/OrparTools.d.ts +8 -0
- package/dist/protocols/mcp/tools/OrparTools.d.ts.map +1 -1
- package/dist/protocols/mcp/tools/OrparTools.js +66 -0
- package/dist/protocols/mcp/tools/OrparTools.js.map +1 -1
- package/dist/protocols/mcp/tools/SafetyTools.d.ts +7 -525
- package/dist/protocols/mcp/tools/SafetyTools.d.ts.map +1 -1
- package/dist/protocols/mcp/tools/SafetyTools.js +349 -514
- package/dist/protocols/mcp/tools/SafetyTools.js.map +1 -1
- package/dist/protocols/mcp/tools/TaskPlanningTools.d.ts.map +1 -1
- package/dist/protocols/mcp/tools/TaskPlanningTools.js +56 -26
- package/dist/protocols/mcp/tools/TaskPlanningTools.js.map +1 -1
- package/dist/protocols/mcp/tools/TestTools.d.ts +15 -376
- package/dist/protocols/mcp/tools/TestTools.d.ts.map +1 -1
- package/dist/protocols/mcp/tools/TestTools.js +241 -363
- package/dist/protocols/mcp/tools/TestTools.js.map +1 -1
- package/dist/protocols/mcp/tools/TypeScriptTools.d.ts +6 -276
- package/dist/protocols/mcp/tools/TypeScriptTools.d.ts.map +1 -1
- package/dist/protocols/mcp/tools/TypeScriptTools.js +223 -337
- package/dist/protocols/mcp/tools/TypeScriptTools.js.map +1 -1
- package/dist/protocols/mcp/tools/WebTools.d.ts +15 -728
- package/dist/protocols/mcp/tools/WebTools.d.ts.map +1 -1
- package/dist/protocols/mcp/tools/WebTools.js +151 -257
- package/dist/protocols/mcp/tools/WebTools.js.map +1 -1
- package/dist/protocols/mcp/tools/shell/ShellExecuteHandler.d.ts +5 -3
- package/dist/protocols/mcp/tools/shell/ShellExecuteHandler.d.ts.map +1 -1
- package/dist/protocols/mcp/tools/shell/ShellExecuteHandler.js +31 -6
- package/dist/protocols/mcp/tools/shell/ShellExecuteHandler.js.map +1 -1
- package/dist/schemas/EventPayloadSchema.d.ts +21 -0
- package/dist/schemas/EventPayloadSchema.d.ts.map +1 -1
- package/dist/schemas/EventPayloadSchema.js +14 -0
- package/dist/schemas/EventPayloadSchema.js.map +1 -1
- package/dist/services/BackgroundTaskManager.d.ts.map +1 -1
- package/dist/services/BackgroundTaskManager.js +12 -4
- package/dist/services/BackgroundTaskManager.js.map +1 -1
- package/dist/services/BrowserManager.d.ts +1 -1
- package/dist/services/BrowserManager.d.ts.map +1 -1
- package/dist/services/BrowserManager.js +26 -3
- package/dist/services/BrowserManager.js.map +1 -1
- package/dist/services/MemoryCompressor.d.ts +11 -2
- package/dist/services/MemoryCompressor.d.ts.map +1 -1
- package/dist/services/MemoryCompressor.js +8 -26
- package/dist/services/MemoryCompressor.js.map +1 -1
- package/dist/services/MemoryService.d.ts +16 -3
- package/dist/services/MemoryService.d.ts.map +1 -1
- package/dist/services/MemoryService.js +79 -55
- package/dist/services/MemoryService.js.map +1 -1
- package/dist/services/MxfMLService.d.ts +5 -0
- package/dist/services/MxfMLService.d.ts.map +1 -1
- package/dist/services/MxfMLService.js +32 -6
- package/dist/services/MxfMLService.js.map +1 -1
- package/dist/services/PredictiveAnalyticsService.d.ts +45 -9
- package/dist/services/PredictiveAnalyticsService.d.ts.map +1 -1
- package/dist/services/PredictiveAnalyticsService.js +82 -44
- package/dist/services/PredictiveAnalyticsService.js.map +1 -1
- package/dist/services/QValueManager.d.ts +7 -0
- package/dist/services/QValueManager.d.ts.map +1 -1
- package/dist/services/QValueManager.js +9 -0
- package/dist/services/QValueManager.js.map +1 -1
- package/dist/services/RewardSignalProcessor.d.ts +38 -1
- package/dist/services/RewardSignalProcessor.d.ts.map +1 -1
- package/dist/services/RewardSignalProcessor.js +109 -7
- package/dist/services/RewardSignalProcessor.js.map +1 -1
- package/dist/services/SharedStateService.d.ts +16 -2
- package/dist/services/SharedStateService.d.ts.map +1 -1
- package/dist/services/SharedStateService.js +44 -3
- package/dist/services/SharedStateService.js.map +1 -1
- package/dist/services/TaskEffectivenessService.d.ts +34 -18
- package/dist/services/TaskEffectivenessService.d.ts.map +1 -1
- package/dist/services/TaskEffectivenessService.js +91 -155
- package/dist/services/TaskEffectivenessService.js.map +1 -1
- package/dist/services/WebSearchService.d.ts.map +1 -1
- package/dist/services/WebSearchService.js.map +1 -1
- package/dist/services/dag/TaskDagService.d.ts.map +1 -1
- package/dist/services/dag/TaskDagService.js +2 -0
- package/dist/services/dag/TaskDagService.js.map +1 -1
- package/dist/services/orpar-memory/OrparMemoryCoordinator.d.ts +7 -0
- package/dist/services/orpar-memory/OrparMemoryCoordinator.d.ts.map +1 -1
- package/dist/services/orpar-memory/OrparMemoryCoordinator.js +71 -10
- package/dist/services/orpar-memory/OrparMemoryCoordinator.js.map +1 -1
- package/dist/types/MemoryUtilityTypes.d.ts +2 -0
- package/dist/types/MemoryUtilityTypes.d.ts.map +1 -1
- package/dist/types/MemoryUtilityTypes.js.map +1 -1
- package/dist/utils/Logger.d.ts +43 -3
- package/dist/utils/Logger.d.ts.map +1 -1
- package/dist/utils/Logger.js +78 -36
- package/dist/utils/Logger.js.map +1 -1
- package/dist/utils/MxpEncryption.d.ts +19 -6
- package/dist/utils/MxpEncryption.d.ts.map +1 -1
- package/dist/utils/MxpEncryption.js +48 -16
- package/dist/utils/MxpEncryption.js.map +1 -1
- package/package.json +19 -4
- package/src/adapters/FrameworkAdapter.ts +1 -1
- package/src/config/ConfigManager.ts +52 -19
- package/src/config/memory-strata.config.ts +136 -0
- package/src/constants/ToolNames.ts +4 -18
- package/src/database/DatabaseAdapterFactory.ts +23 -6
- package/src/database/adapters/mongodb/MongoBaseRepository.ts +16 -2
- package/src/events/ClientEventBus.ts +68 -403
- package/src/events/EventBus.ts +7 -8
- package/src/events/EventBusBase.ts +479 -116
- package/src/events/EventNames.ts +6 -0
- package/src/events/PublicEvents.ts +7 -4
- package/src/events/ServerEventBus.ts +78 -473
- package/src/events/event-definitions/LlmBudgetEvents.ts +74 -0
- package/src/interfaces/IMemoryPersistence.ts +51 -0
- package/src/middleware/MxpMiddleware.ts +51 -24
- package/src/models/channelKey.ts +18 -4
- package/src/models/memoryUtility.ts +64 -0
- package/src/models/user.ts +36 -4
- package/src/mxp/BinaryProtocolLayer.ts +108 -124
- package/src/mxp/ContextCompressionEngine.ts +219 -161
- package/src/mxp/MxpTokenOptimizer.ts +10 -10
- package/src/prompts/MxfAgentSystemPrompt.ts +45 -55
- package/src/protocols/mcp/ToolError.ts +201 -0
- package/src/protocols/mcp/defineTool.ts +379 -0
- package/src/protocols/mcp/security/HttpTargetGuard.ts +267 -0
- package/src/protocols/mcp/security/McpSecurityGuard.ts +10 -3
- package/src/protocols/mcp/security/McpToolPolicy.ts +221 -0
- package/src/protocols/mcp/services/ExternalMcpServerManager.ts +340 -322
- package/src/protocols/mcp/services/ExternalServerConfigs.ts +82 -19
- package/src/protocols/mcp/tools/AnalyticsTools.ts +1 -1
- package/src/protocols/mcp/tools/CodeAnalysisTools.ts +15 -0
- package/src/protocols/mcp/tools/EffectivenessTools.ts +3 -3
- package/src/protocols/mcp/tools/GitTools.ts +290 -569
- package/src/protocols/mcp/tools/InfrastructureTools.ts +189 -43
- package/src/protocols/mcp/tools/MemorySearchTools.ts +77 -1
- package/src/protocols/mcp/tools/OrparTools.ts +87 -0
- package/src/protocols/mcp/tools/SafetyTools.ts +501 -579
- package/src/protocols/mcp/tools/TaskPlanningTools.ts +81 -39
- package/src/protocols/mcp/tools/TestTools.ts +339 -419
- package/src/protocols/mcp/tools/TypeScriptTools.ts +316 -376
- package/src/protocols/mcp/tools/WebTools.ts +209 -307
- package/src/protocols/mcp/tools/shell/ShellExecuteHandler.ts +53 -14
- package/src/schemas/EventPayloadSchema.ts +37 -0
- package/src/services/BackgroundTaskManager.ts +12 -4
- package/src/services/BrowserManager.ts +41 -1
- package/src/services/MemoryCompressor.ts +20 -32
- package/src/services/MemoryService.ts +96 -58
- package/src/services/MxfMLService.ts +41 -7
- package/src/services/PredictiveAnalyticsService.ts +101 -51
- package/src/services/QValueManager.ts +10 -0
- package/src/services/RewardSignalProcessor.ts +138 -9
- package/src/services/SharedStateService.ts +56 -8
- package/src/services/TaskEffectivenessService.ts +114 -187
- package/src/services/WebSearchService.ts +2 -1
- package/src/services/dag/TaskDagService.ts +2 -0
- package/src/services/orpar-memory/OrparMemoryCoordinator.ts +86 -10
- package/src/types/MemoryUtilityTypes.ts +2 -0
- package/src/utils/Logger.ts +116 -40
- package/src/utils/MxpEncryption.ts +69 -34
- package/dist/mxp/MxpEventForwardingEnhancer.d.ts +0 -134
- package/dist/mxp/MxpEventForwardingEnhancer.d.ts.map +0 -1
- package/dist/mxp/MxpEventForwardingEnhancer.js +0 -361
- package/dist/mxp/MxpEventForwardingEnhancer.js.map +0 -1
- package/dist/protocols/mcp/services/ToolSchemaEnhancements.d.ts +0 -56
- package/dist/protocols/mcp/services/ToolSchemaEnhancements.d.ts.map +0 -1
- package/dist/protocols/mcp/services/ToolSchemaEnhancements.js +0 -57
- package/dist/protocols/mcp/services/ToolSchemaEnhancements.js.map +0 -1
- package/dist/protocols/mcp/tools/ControlLoopLifecycle.d.ts +0 -270
- package/dist/protocols/mcp/tools/ControlLoopLifecycle.d.ts.map +0 -1
- package/dist/protocols/mcp/tools/ControlLoopLifecycle.js +0 -256
- package/dist/protocols/mcp/tools/ControlLoopLifecycle.js.map +0 -1
- package/dist/protocols/mcp/tools/LspTools.d.ts +0 -495
- package/dist/protocols/mcp/tools/LspTools.d.ts.map +0 -1
- package/dist/protocols/mcp/tools/LspTools.js +0 -469
- package/dist/protocols/mcp/tools/LspTools.js.map +0 -1
- package/src/mxp/MxpEventForwardingEnhancer.ts +0 -471
- package/src/protocols/mcp/services/ToolSchemaEnhancements.ts +0 -92
- package/src/protocols/mcp/tools/ControlLoopLifecycle.ts +0 -302
- package/src/protocols/mcp/tools/LspTools.ts +0 -476
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2024 Brad Anderson
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
* @author Brad Anderson <BradA1878@pm.me>
|
|
17
|
+
* @repository https://github.com/BradA1878/model-exchange-framework
|
|
18
|
+
* @documentation https://mxf-dev.github.io/mxf/
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* LLM Budget Events
|
|
23
|
+
*
|
|
24
|
+
* Emitted as SystemLLM spend approaches and crosses its daily ceiling. SystemLLM runs on
|
|
25
|
+
* Claude Opus and had no absolute spend cap — only per-channel and global cooldowns — so
|
|
26
|
+
* a busy or looping channel could run up real cost with nothing to stop it. These events
|
|
27
|
+
* make that spend observable, and the EXCEEDED event marks the point where SystemLLM
|
|
28
|
+
* calls start being refused.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { AgentId, ChannelId } from '../../types/ChannelContext.js';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Events emitted as SystemLLM spend approaches and crosses the daily ceiling.
|
|
35
|
+
*/
|
|
36
|
+
export const LlmBudgetEvents = {
|
|
37
|
+
/** Spend crossed the warning threshold. SystemLLM is still running. */
|
|
38
|
+
WARNING: 'llm:budget:warning',
|
|
39
|
+
/** Spend reached the daily ceiling. SystemLLM calls are refused until reset. */
|
|
40
|
+
EXCEEDED: 'llm:budget:exceeded',
|
|
41
|
+
/** A new budget day started and spend went back to zero. */
|
|
42
|
+
RESET: 'llm:budget:reset'
|
|
43
|
+
} as const;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Spend figures carried by every budget event.
|
|
47
|
+
*/
|
|
48
|
+
export interface LlmBudgetEventData {
|
|
49
|
+
/** Money spent so far in the current budget day, in USD. */
|
|
50
|
+
spentUsd: number;
|
|
51
|
+
/** The daily ceiling, in USD. */
|
|
52
|
+
limitUsd: number;
|
|
53
|
+
/** Fraction of the ceiling used (spentUsd / limitUsd). */
|
|
54
|
+
utilization: number;
|
|
55
|
+
/** Start of the current budget day, as an ISO timestamp. */
|
|
56
|
+
periodStart: string;
|
|
57
|
+
/** Model whose call triggered the event, when there was one. */
|
|
58
|
+
model?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Payload types for LLM budget events.
|
|
63
|
+
*/
|
|
64
|
+
export interface LlmBudgetPayloads {
|
|
65
|
+
'llm:budget:warning': LlmBudgetEventData;
|
|
66
|
+
'llm:budget:exceeded': LlmBudgetEventData;
|
|
67
|
+
'llm:budget:reset': LlmBudgetEventData;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Actor recorded on budget events. Spend is a property of the server, not an agent. */
|
|
71
|
+
export const LLM_BUDGET_ACTOR_ID = 'system-llm-budget' as AgentId;
|
|
72
|
+
|
|
73
|
+
/** Channel recorded on budget events. The ceiling is global, not per channel. */
|
|
74
|
+
export const LLM_BUDGET_CHANNEL_ID = 'global' as ChannelId;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory persistence contract.
|
|
3
|
+
*
|
|
4
|
+
* MemoryService lives in @mxf-dev/core but the concrete persistence implementation
|
|
5
|
+
* lives in the server (it owns the Mongoose connection). Core must not import from
|
|
6
|
+
* src/**, so the server injects its implementation into MemoryService at boot.
|
|
7
|
+
*
|
|
8
|
+
* This interface is that injection point. It exists so the dependency is typed:
|
|
9
|
+
* MemoryService previously held the persistence service as `any`, which allowed a
|
|
10
|
+
* call to `updateAgentMemoryUtility?.()` — a method that was never implemented — to
|
|
11
|
+
* compile and then silently no-op forever, discarding every learned Q-value. Typing
|
|
12
|
+
* the contract turns that class of mistake into a compile error.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { Observable } from 'rxjs';
|
|
16
|
+
import { IAgentMemory, IChannelMemory } from '../types/MemoryTypes.js';
|
|
17
|
+
import { MemoryUtilitySubdocument } from '../types/MemoryUtilityTypes.js';
|
|
18
|
+
|
|
19
|
+
export interface IMemoryPersistence {
|
|
20
|
+
/** Load an agent's memory document. */
|
|
21
|
+
getAgentMemory(agentId: string): Observable<IAgentMemory>;
|
|
22
|
+
|
|
23
|
+
/** Persist an agent's memory document. */
|
|
24
|
+
saveAgentMemory(memory: IAgentMemory): Observable<IAgentMemory>;
|
|
25
|
+
|
|
26
|
+
/** Persist a channel's memory document. */
|
|
27
|
+
saveChannelMemory(memory: IChannelMemory): Observable<IChannelMemory>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Persist the MULS utility subdocument for a single memory.
|
|
31
|
+
*
|
|
32
|
+
* Called by QValueManager's persistence callback whenever a Q-value changes, and
|
|
33
|
+
* again when a dirty cache entry is evicted. Must throw on failure — a silent
|
|
34
|
+
* failure here means the system reports learning it did not retain.
|
|
35
|
+
*/
|
|
36
|
+
updateAgentMemoryUtility(
|
|
37
|
+
memoryId: string,
|
|
38
|
+
utility: Partial<MemoryUtilitySubdocument>
|
|
39
|
+
): Promise<void>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Load utility subdocuments for a batch of memories.
|
|
43
|
+
*
|
|
44
|
+
* Used to hydrate the Q-value cache on retrieval so learning survives a restart.
|
|
45
|
+
* Memories with no stored utility are simply absent from the returned map; the
|
|
46
|
+
* caller falls back to the configured default Q-value for those.
|
|
47
|
+
*/
|
|
48
|
+
getAgentMemoryUtilities(
|
|
49
|
+
memoryIds: string[]
|
|
50
|
+
): Promise<Map<string, MemoryUtilitySubdocument>>;
|
|
51
|
+
}
|
|
@@ -97,23 +97,23 @@ export class MxpMiddleware {
|
|
|
97
97
|
this.stats.mxpMessages++;
|
|
98
98
|
const mxpMsg = message as MxpMessage;
|
|
99
99
|
|
|
100
|
-
// Decrypt if needed
|
|
100
|
+
// Decrypt if needed. decryptMessage() throws on failure — an
|
|
101
|
+
// undecryptable message must not be passed on as if it were fine.
|
|
101
102
|
if (mxpMsg.encrypted && isEncryptedPayload(mxpMsg.payload)) {
|
|
102
|
-
|
|
103
|
-
|
|
103
|
+
try {
|
|
104
|
+
const decrypted = mxpEncryption.decryptMessage(mxpMsg.payload);
|
|
104
105
|
this.stats.messagesDecrypted++;
|
|
105
106
|
return {
|
|
106
107
|
...mxpMsg,
|
|
107
108
|
payload: decrypted,
|
|
108
109
|
encrypted: false
|
|
109
110
|
};
|
|
110
|
-
}
|
|
111
|
-
logger.error('Failed to decrypt MXP message');
|
|
111
|
+
} catch (error) {
|
|
112
112
|
this.stats.encryptionFailures++;
|
|
113
|
-
throw
|
|
113
|
+
throw error;
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
|
-
|
|
116
|
+
|
|
117
117
|
return mxpMsg;
|
|
118
118
|
}
|
|
119
119
|
|
|
@@ -206,7 +206,14 @@ export class MxpMiddleware {
|
|
|
206
206
|
}
|
|
207
207
|
|
|
208
208
|
/**
|
|
209
|
-
* Handle encryption for MXP messages
|
|
209
|
+
* Handle encryption for MXP messages.
|
|
210
|
+
*
|
|
211
|
+
* `forceEncryption` means exactly that: if the message cannot be encrypted,
|
|
212
|
+
* this throws rather than putting the plaintext on the wire. An earlier
|
|
213
|
+
* version incremented an `encryptionFailures` counter and returned the
|
|
214
|
+
* message unencrypted, so callers that asked for encryption silently got none.
|
|
215
|
+
*
|
|
216
|
+
* @throws If forceEncryption is set and encryption is unavailable or fails.
|
|
210
217
|
*/
|
|
211
218
|
private static handleMxpEncryption(
|
|
212
219
|
message: MxpMessage,
|
|
@@ -216,26 +223,46 @@ export class MxpMiddleware {
|
|
|
216
223
|
if (message.encrypted && isEncryptedPayload(message.payload)) {
|
|
217
224
|
return message;
|
|
218
225
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
226
|
+
|
|
227
|
+
if (isEncryptedPayload(message.payload)) {
|
|
228
|
+
return message;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (forceEncryption && !mxpEncryption.isEncryptionEnabled()) {
|
|
232
|
+
this.stats.encryptionFailures++;
|
|
233
|
+
throw new Error(
|
|
234
|
+
'forceEncryption was requested but MXP encryption is not configured. ' +
|
|
235
|
+
'Set MXP_ENCRYPTION_KEY and MXP_ENCRYPTION_SALT, or stop forcing encryption.'
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Reasoning messages stay in the clear unless encryption is forced:
|
|
240
|
+
// they carry no operational payload and are meant to be human-readable.
|
|
241
|
+
const shouldEncrypt = forceEncryption ||
|
|
222
242
|
(mxpEncryption.isEncryptionEnabled() && message.type !== MxpMessageType.REASONING);
|
|
223
|
-
|
|
224
|
-
if (shouldEncrypt
|
|
243
|
+
|
|
244
|
+
if (!shouldEncrypt) {
|
|
245
|
+
return message;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
// Throws on cipher failure; returns null only when encryption is off,
|
|
250
|
+
// which forceEncryption has already ruled out above.
|
|
225
251
|
const encrypted = mxpEncryption.encrypt(message.payload as MxpPayload);
|
|
226
|
-
if (encrypted) {
|
|
227
|
-
|
|
228
|
-
return {
|
|
229
|
-
...message,
|
|
230
|
-
encrypted: true,
|
|
231
|
-
payload: encrypted
|
|
232
|
-
};
|
|
233
|
-
} else {
|
|
234
|
-
this.stats.encryptionFailures++;
|
|
252
|
+
if (!encrypted) {
|
|
253
|
+
return message;
|
|
235
254
|
}
|
|
255
|
+
|
|
256
|
+
this.stats.messagesEncrypted++;
|
|
257
|
+
return {
|
|
258
|
+
...message,
|
|
259
|
+
encrypted: true,
|
|
260
|
+
payload: encrypted
|
|
261
|
+
};
|
|
262
|
+
} catch (error) {
|
|
263
|
+
this.stats.encryptionFailures++;
|
|
264
|
+
throw error;
|
|
236
265
|
}
|
|
237
|
-
|
|
238
|
-
return message;
|
|
239
266
|
}
|
|
240
267
|
|
|
241
268
|
/**
|
package/src/models/channelKey.ts
CHANGED
|
@@ -35,13 +35,22 @@ import crypto from 'crypto';
|
|
|
35
35
|
export interface IChannelKey extends Document {
|
|
36
36
|
// The unique key identifier
|
|
37
37
|
keyId: string;
|
|
38
|
-
|
|
39
|
-
//
|
|
38
|
+
|
|
39
|
+
// bcrypt hash of the secret. The plaintext is shown once at creation and never stored —
|
|
40
|
+
// see ChannelKeyService. It used to be kept in the clear, so any read of this
|
|
41
|
+
// collection exposed every channel credential.
|
|
40
42
|
secretKey: string;
|
|
41
|
-
|
|
43
|
+
|
|
42
44
|
// Reference to the channel this key provides access to
|
|
43
45
|
channelId: string;
|
|
44
|
-
|
|
46
|
+
|
|
47
|
+
// The agent this key authenticates as.
|
|
48
|
+
//
|
|
49
|
+
// Socket authentication reads the agent's identity from here rather than from the
|
|
50
|
+
// client handshake. Previously the key authenticated the *channel* while the client
|
|
51
|
+
// chose its own agentId, so one shared key let a client present any agent identity.
|
|
52
|
+
agentId: string;
|
|
53
|
+
|
|
45
54
|
// Optional name for the key (for user reference)
|
|
46
55
|
name?: string;
|
|
47
56
|
|
|
@@ -79,6 +88,11 @@ const ChannelKeySchema: Schema = new Schema({
|
|
|
79
88
|
index: true,
|
|
80
89
|
ref: 'ChannelRegistration',
|
|
81
90
|
},
|
|
91
|
+
agentId: {
|
|
92
|
+
type: String,
|
|
93
|
+
required: true,
|
|
94
|
+
index: true,
|
|
95
|
+
},
|
|
82
96
|
name: {
|
|
83
97
|
type: String,
|
|
84
98
|
required: false,
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory Utility model (MULS).
|
|
3
|
+
*
|
|
4
|
+
* Stores the learned Q-value and reward statistics for a single *retrievable* memory —
|
|
5
|
+
* the unit QValueManager actually learns about, which is a document in the search index
|
|
6
|
+
* (a conversation message, action, or pattern), identified by its search-index id.
|
|
7
|
+
*
|
|
8
|
+
* Why this is its own collection: the `utility` subdocument on AgentMemory /
|
|
9
|
+
* ChannelMemory / RelationshipMemory is keyed by the id of a whole memory-scope
|
|
10
|
+
* document, whereas retrieval returns individual indexed items. Those are different
|
|
11
|
+
* granularities, so utility written onto a scope document would never match the id a
|
|
12
|
+
* retrieval learned about. That mismatch is why the previous persistence path silently
|
|
13
|
+
* matched nothing. Keying by the retrieved memory id is what makes learning durable.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import mongoose, { Schema, Document, Model } from 'mongoose';
|
|
17
|
+
import { QValueInitSource } from '../types/MemoryUtilityTypes.js';
|
|
18
|
+
|
|
19
|
+
export interface IMemoryUtilityDocument extends Document {
|
|
20
|
+
/** The retrieved memory's id — matches the search-index document id. */
|
|
21
|
+
memoryId: string;
|
|
22
|
+
/** Agent this memory's utility was learned for (scoping and analytics). */
|
|
23
|
+
agentId?: string;
|
|
24
|
+
/** Channel this memory's utility was learned in (scoping and analytics). */
|
|
25
|
+
channelId?: string;
|
|
26
|
+
qValue: number;
|
|
27
|
+
qValueHistory: Array<{ value: number; reward: number; timestamp: Date; phase?: string }>;
|
|
28
|
+
retrievalCount: number;
|
|
29
|
+
successCount: number;
|
|
30
|
+
failureCount: number;
|
|
31
|
+
lastRewardAt?: Date;
|
|
32
|
+
initializedFrom: QValueInitSource;
|
|
33
|
+
updatedAt: Date;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const memoryUtilitySchema = new Schema<IMemoryUtilityDocument>({
|
|
37
|
+
memoryId: { type: String, required: true, unique: true, index: true },
|
|
38
|
+
agentId: { type: String, index: true },
|
|
39
|
+
channelId: { type: String, index: true },
|
|
40
|
+
|
|
41
|
+
qValue: { type: Number, default: 0.5, min: 0, max: 1 },
|
|
42
|
+
|
|
43
|
+
qValueHistory: [{
|
|
44
|
+
value: { type: Number, required: true },
|
|
45
|
+
reward: { type: Number, required: true },
|
|
46
|
+
timestamp: { type: Date, default: Date.now },
|
|
47
|
+
phase: { type: String }
|
|
48
|
+
}],
|
|
49
|
+
|
|
50
|
+
retrievalCount: { type: Number, default: 0 },
|
|
51
|
+
successCount: { type: Number, default: 0 },
|
|
52
|
+
failureCount: { type: Number, default: 0 },
|
|
53
|
+
lastRewardAt: { type: Date },
|
|
54
|
+
initializedFrom: { type: String, default: 'default' },
|
|
55
|
+
updatedAt: { type: Date, default: Date.now }
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Ranking reads sort by Q-value, scoped by agent or channel.
|
|
59
|
+
memoryUtilitySchema.index({ agentId: 1, qValue: -1 });
|
|
60
|
+
memoryUtilitySchema.index({ channelId: 1, qValue: -1 });
|
|
61
|
+
|
|
62
|
+
export const MemoryUtility: Model<IMemoryUtilityDocument> =
|
|
63
|
+
mongoose.models.MemoryUtility ||
|
|
64
|
+
mongoose.model<IMemoryUtilityDocument>('MemoryUtility', memoryUtilitySchema);
|
package/src/models/user.ts
CHANGED
|
@@ -19,7 +19,36 @@
|
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
21
|
import mongoose, { Document, Schema } from 'mongoose';
|
|
22
|
-
|
|
22
|
+
// bcrypt is an OPTIONAL peer dependency: it is a native module that has to be
|
|
23
|
+
// compiled at install time, and only applications that store user passwords need
|
|
24
|
+
// it. Loaded on demand; a clear error is thrown if a password operation is
|
|
25
|
+
// attempted without it installed.
|
|
26
|
+
import type * as BcryptModule from 'bcrypt';
|
|
27
|
+
|
|
28
|
+
let bcryptModule: typeof BcryptModule | null = null;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Load bcrypt on demand.
|
|
32
|
+
*
|
|
33
|
+
* @throws If bcrypt is not installed. Passwords are never stored unhashed.
|
|
34
|
+
*/
|
|
35
|
+
const loadBcrypt = async (): Promise<typeof BcryptModule> => {
|
|
36
|
+
if (bcryptModule) {
|
|
37
|
+
return bcryptModule;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const imported = await import('bcrypt');
|
|
42
|
+
bcryptModule = (imported.default ?? imported) as typeof BcryptModule;
|
|
43
|
+
return bcryptModule;
|
|
44
|
+
} catch (error) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
'Password hashing requires the optional peer dependency "bcrypt", which is not installed. ' +
|
|
47
|
+
'Install it with `bun add bcrypt` (or `npm install bcrypt`) to use the User model. ' +
|
|
48
|
+
`Underlying error: ${error instanceof Error ? error.message : String(error)}`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
23
52
|
|
|
24
53
|
/**
|
|
25
54
|
* User role enum
|
|
@@ -123,15 +152,17 @@ UserSchema.pre('save', async function(next) {
|
|
|
123
152
|
}
|
|
124
153
|
|
|
125
154
|
try {
|
|
155
|
+
const bcrypt = await loadBcrypt();
|
|
156
|
+
|
|
126
157
|
// Generate a salt
|
|
127
158
|
const salt = await bcrypt.genSalt(10);
|
|
128
|
-
|
|
159
|
+
|
|
129
160
|
// Hash the password along with the new salt
|
|
130
161
|
this.password = await bcrypt.hash(this.password, salt);
|
|
131
|
-
|
|
162
|
+
|
|
132
163
|
// Update the updatedAt field
|
|
133
164
|
this.updatedAt = new Date();
|
|
134
|
-
|
|
165
|
+
|
|
135
166
|
next();
|
|
136
167
|
} catch (error) {
|
|
137
168
|
next(error as Error);
|
|
@@ -140,6 +171,7 @@ UserSchema.pre('save', async function(next) {
|
|
|
140
171
|
|
|
141
172
|
// Method to compare passwords
|
|
142
173
|
UserSchema.methods.comparePassword = async function(candidatePassword: string): Promise<boolean> {
|
|
174
|
+
const bcrypt = await loadBcrypt();
|
|
143
175
|
return bcrypt.compare(candidatePassword, this.password);
|
|
144
176
|
};
|
|
145
177
|
|