@1mcp/agent 0.22.3 → 0.23.0
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/build/commands/mcp/tokens.d.ts +6 -0
- package/build/commands/mcp/tokens.d.ts.map +1 -1
- package/build/commands/mcp/tokens.js +181 -66
- package/build/commands/mcp/tokens.js.map +1 -1
- package/build/commands/serve/index.d.ts +6 -0
- package/build/commands/serve/index.d.ts.map +1 -1
- package/build/commands/serve/index.js +12 -0
- package/build/commands/serve/index.js.map +1 -1
- package/build/commands/serve/serve.d.ts +1 -0
- package/build/commands/serve/serve.d.ts.map +1 -1
- package/build/commands/serve/serve.js +84 -3
- package/build/commands/serve/serve.js.map +1 -1
- package/build/constants.d.ts +6 -1
- package/build/constants.d.ts.map +1 -1
- package/build/constants.js +9 -1
- package/build/constants.js.map +1 -1
- package/build/core/client/clientManager.d.ts +13 -0
- package/build/core/client/clientManager.d.ts.map +1 -1
- package/build/core/client/clientManager.js +48 -0
- package/build/core/client/clientManager.js.map +1 -1
- package/build/core/filtering/filteringService.d.ts +85 -0
- package/build/core/filtering/filteringService.d.ts.map +1 -0
- package/build/core/filtering/filteringService.js +253 -0
- package/build/core/filtering/filteringService.js.map +1 -0
- package/build/core/instructions/instructionAggregator.d.ts +141 -0
- package/build/core/instructions/instructionAggregator.d.ts.map +1 -0
- package/build/core/instructions/instructionAggregator.js +385 -0
- package/build/core/instructions/instructionAggregator.js.map +1 -0
- package/build/core/instructions/templateTypes.d.ts +77 -0
- package/build/core/instructions/templateTypes.d.ts.map +1 -0
- package/build/core/instructions/templateTypes.js +90 -0
- package/build/core/instructions/templateTypes.js.map +1 -0
- package/build/core/instructions/templateValidator.d.ts +53 -0
- package/build/core/instructions/templateValidator.d.ts.map +1 -0
- package/build/core/instructions/templateValidator.js +142 -0
- package/build/core/instructions/templateValidator.js.map +1 -0
- package/build/core/server/serverManager.d.ts +11 -0
- package/build/core/server/serverManager.d.ts.map +1 -1
- package/build/core/server/serverManager.js +38 -1
- package/build/core/server/serverManager.js.map +1 -1
- package/build/core/types/client.d.ts +2 -0
- package/build/core/types/client.d.ts.map +1 -1
- package/build/core/types/server.d.ts +2 -1
- package/build/core/types/server.d.ts.map +1 -1
- package/build/core/types/server.js.map +1 -1
- package/build/handlers/requestHandlers.d.ts.map +1 -1
- package/build/handlers/requestHandlers.js +14 -42
- package/build/handlers/requestHandlers.js.map +1 -1
- package/build/server.d.ts +3 -0
- package/build/server.d.ts.map +1 -1
- package/build/server.js +13 -0
- package/build/server.js.map +1 -1
- package/package.json +4 -2
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { ClientStatus } from '../types/client.js';
|
|
2
|
+
import { TagQueryParser } from '../../utils/tagQueryParser.js';
|
|
3
|
+
import { TagQueryEvaluator } from '../../utils/tagQueryEvaluator.js';
|
|
4
|
+
import { normalizeTag } from '../../utils/sanitization.js';
|
|
5
|
+
import logger from '../../logger/logger.js';
|
|
6
|
+
/**
|
|
7
|
+
* Unified filtering service that consolidates all filtering logic across the application.
|
|
8
|
+
* This service provides a single source of truth for filtering MCP server connections
|
|
9
|
+
* based on various criteria including tags, capabilities, and advanced expressions.
|
|
10
|
+
*/
|
|
11
|
+
export class FilteringService {
|
|
12
|
+
/**
|
|
13
|
+
* Get filtered connections based on inbound connection configuration
|
|
14
|
+
* This is the main entry point for filtering and should be used by most components
|
|
15
|
+
*
|
|
16
|
+
* @param connections Map of outbound connections to filter
|
|
17
|
+
* @param config Inbound connection configuration containing filter criteria
|
|
18
|
+
* @returns Filtered map of connections
|
|
19
|
+
*/
|
|
20
|
+
static getFilteredConnections(connections, config) {
|
|
21
|
+
logger.debug('FilteringService: Filtering connections', {
|
|
22
|
+
totalConnections: connections.size,
|
|
23
|
+
filterMode: config.tagFilterMode,
|
|
24
|
+
tags: config.tags,
|
|
25
|
+
hasTagExpression: !!config.tagExpression,
|
|
26
|
+
hasTagQuery: !!config.tagQuery,
|
|
27
|
+
});
|
|
28
|
+
// Only include connected clients in filtering
|
|
29
|
+
const connectedClients = new Map();
|
|
30
|
+
for (const [name, connection] of connections) {
|
|
31
|
+
if (connection.status === ClientStatus.Connected) {
|
|
32
|
+
connectedClients.set(name, connection);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
logger.debug('FilteringService: Connected clients', {
|
|
36
|
+
connectedCount: connectedClients.size,
|
|
37
|
+
connectedNames: Array.from(connectedClients.keys()),
|
|
38
|
+
});
|
|
39
|
+
if (!config.tagFilterMode || config.tagFilterMode === 'none') {
|
|
40
|
+
logger.debug('FilteringService: No filtering specified, returning all connected clients');
|
|
41
|
+
return connectedClients;
|
|
42
|
+
}
|
|
43
|
+
const filter = this.createFilter(config);
|
|
44
|
+
const filteredConnections = filter(connectedClients);
|
|
45
|
+
logger.debug('FilteringService: Filtering completed', {
|
|
46
|
+
filteredCount: filteredConnections.size,
|
|
47
|
+
filteredNames: Array.from(filteredConnections.keys()),
|
|
48
|
+
});
|
|
49
|
+
return filteredConnections;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Create a filter function based on inbound connection configuration
|
|
53
|
+
*
|
|
54
|
+
* @param config Inbound connection configuration
|
|
55
|
+
* @returns Filter function that can be applied to connections
|
|
56
|
+
*/
|
|
57
|
+
static createFilter(config) {
|
|
58
|
+
if (config.tagFilterMode === 'preset' && config.tagQuery) {
|
|
59
|
+
return this.byTagQuery(config.tagQuery);
|
|
60
|
+
}
|
|
61
|
+
else if (config.tagFilterMode === 'advanced' && config.tagExpression) {
|
|
62
|
+
return this.byTagExpression(config.tagExpression);
|
|
63
|
+
}
|
|
64
|
+
else if (config.tagFilterMode === 'simple-or' || config.tags) {
|
|
65
|
+
return this.byTags(config.tags);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// No filtering - return function that passes all clients through
|
|
69
|
+
return this.byTags(undefined);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Filter connections by tags using OR logic
|
|
74
|
+
* If no tags are provided, all connections are returned
|
|
75
|
+
*
|
|
76
|
+
* @param tags Array of tags to filter by
|
|
77
|
+
* @returns Filter function
|
|
78
|
+
*/
|
|
79
|
+
static byTags(tags) {
|
|
80
|
+
return (connections) => {
|
|
81
|
+
logger.debug(`FilteringService.byTags: Filtering for tags: ${tags ? tags.join(', ') : 'none'}`);
|
|
82
|
+
if (!tags || tags.length === 0) {
|
|
83
|
+
logger.debug('FilteringService.byTags: No tags specified, returning all connections');
|
|
84
|
+
return connections;
|
|
85
|
+
}
|
|
86
|
+
// Normalize the filter tags for consistent comparison
|
|
87
|
+
const normalizedFilterTags = tags.map((tag) => normalizeTag(tag));
|
|
88
|
+
return Array.from(connections.entries()).reduce((filtered, [name, connection]) => {
|
|
89
|
+
const clientTags = connection.transport.tags || [];
|
|
90
|
+
// Normalize client tags for comparison
|
|
91
|
+
const normalizedClientTags = clientTags.map((tag) => normalizeTag(tag));
|
|
92
|
+
const hasMatchingTags = normalizedClientTags.some((clientTag) => normalizedFilterTags.includes(clientTag));
|
|
93
|
+
logger.debug(`FilteringService.byTags: Connection ${name}`, {
|
|
94
|
+
clientTags,
|
|
95
|
+
normalizedClientTags,
|
|
96
|
+
requiredTags: tags,
|
|
97
|
+
normalizedRequiredTags: normalizedFilterTags,
|
|
98
|
+
hasMatchingTags,
|
|
99
|
+
});
|
|
100
|
+
if (hasMatchingTags) {
|
|
101
|
+
filtered.set(name, connection);
|
|
102
|
+
}
|
|
103
|
+
return filtered;
|
|
104
|
+
}, new Map());
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Filter connections by advanced tag expression
|
|
109
|
+
*
|
|
110
|
+
* @param expression Parsed tag expression to evaluate
|
|
111
|
+
* @returns Filter function
|
|
112
|
+
*/
|
|
113
|
+
static byTagExpression(expression) {
|
|
114
|
+
return (connections) => {
|
|
115
|
+
logger.debug(`FilteringService.byTagExpression: Filtering with expression: ${TagQueryParser.expressionToString(expression)}`);
|
|
116
|
+
return Array.from(connections.entries()).reduce((filtered, [name, connection]) => {
|
|
117
|
+
const clientTags = connection.transport.tags || [];
|
|
118
|
+
const matches = TagQueryParser.evaluate(expression, clientTags);
|
|
119
|
+
logger.debug(`FilteringService.byTagExpression: Connection ${name}`, {
|
|
120
|
+
clientTags,
|
|
121
|
+
expression: TagQueryParser.expressionToString(expression),
|
|
122
|
+
matches,
|
|
123
|
+
});
|
|
124
|
+
if (matches) {
|
|
125
|
+
filtered.set(name, connection);
|
|
126
|
+
}
|
|
127
|
+
return filtered;
|
|
128
|
+
}, new Map());
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Filter connections by MongoDB-style tag query
|
|
133
|
+
*
|
|
134
|
+
* @param query Tag query to evaluate
|
|
135
|
+
* @returns Filter function
|
|
136
|
+
*/
|
|
137
|
+
static byTagQuery(query) {
|
|
138
|
+
return (connections) => {
|
|
139
|
+
logger.debug('FilteringService.byTagQuery: Filtering with tag query', { query });
|
|
140
|
+
const filtered = new Map();
|
|
141
|
+
for (const [name, connection] of connections.entries()) {
|
|
142
|
+
if (connection.status !== ClientStatus.Connected) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const clientTags = connection.transport.tags || [];
|
|
146
|
+
try {
|
|
147
|
+
if (TagQueryEvaluator.evaluate(query, clientTags)) {
|
|
148
|
+
filtered.set(name, connection);
|
|
149
|
+
logger.debug(`FilteringService.byTagQuery: Connection ${name} matches query`, {
|
|
150
|
+
clientTags,
|
|
151
|
+
query,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
logger.warn(`FilteringService.byTagQuery: Failed to evaluate query for connection ${name}`, {
|
|
157
|
+
error: error instanceof Error ? error.message : 'Unknown error',
|
|
158
|
+
clientTags,
|
|
159
|
+
query,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return filtered;
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Filter connections by server capabilities
|
|
168
|
+
*
|
|
169
|
+
* @param requiredCapabilities Capabilities that must be present
|
|
170
|
+
* @returns Filter function
|
|
171
|
+
*/
|
|
172
|
+
static byCapabilities(requiredCapabilities) {
|
|
173
|
+
return (connections) => {
|
|
174
|
+
const requiredCaps = Object.keys(requiredCapabilities);
|
|
175
|
+
logger.debug(`FilteringService.byCapabilities: Filtering for capabilities: ${requiredCaps.join(', ')}`);
|
|
176
|
+
return Array.from(connections.entries()).reduce((filtered, [name, connection]) => {
|
|
177
|
+
const clientCaps = connection.capabilities ? Object.keys(connection.capabilities) : [];
|
|
178
|
+
const hasCapabilities = requiredCaps.every((cap) => connection.capabilities && cap in connection.capabilities);
|
|
179
|
+
logger.debug(`FilteringService.byCapabilities: Connection ${name}`, {
|
|
180
|
+
clientCapabilities: clientCaps,
|
|
181
|
+
requiredCapabilities: requiredCaps,
|
|
182
|
+
hasCapabilities,
|
|
183
|
+
clientCapabilitiesObject: connection.capabilities,
|
|
184
|
+
});
|
|
185
|
+
if (hasCapabilities) {
|
|
186
|
+
filtered.set(name, connection);
|
|
187
|
+
}
|
|
188
|
+
return filtered;
|
|
189
|
+
}, new Map());
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Combine multiple filters using AND logic
|
|
194
|
+
* All filters must pass for a connection to be included
|
|
195
|
+
*
|
|
196
|
+
* @param filters Array of filter functions to combine
|
|
197
|
+
* @returns Combined filter function
|
|
198
|
+
*/
|
|
199
|
+
static combineFilters(...filters) {
|
|
200
|
+
return (connections) => {
|
|
201
|
+
logger.debug(`FilteringService.combineFilters: Starting with ${connections.size} connections`, {
|
|
202
|
+
connectionNames: Array.from(connections.keys()),
|
|
203
|
+
filterCount: filters.length,
|
|
204
|
+
});
|
|
205
|
+
const result = filters.reduce((filteredConnections, filter, index) => {
|
|
206
|
+
const beforeCount = filteredConnections.size;
|
|
207
|
+
const afterFiltering = filter(filteredConnections);
|
|
208
|
+
const afterCount = afterFiltering.size;
|
|
209
|
+
logger.debug(`FilteringService.combineFilters: Filter ${index} reduced connections from ${beforeCount} to ${afterCount}`, {
|
|
210
|
+
beforeNames: Array.from(filteredConnections.keys()),
|
|
211
|
+
afterNames: Array.from(afterFiltering.keys()),
|
|
212
|
+
});
|
|
213
|
+
return afterFiltering;
|
|
214
|
+
}, connections);
|
|
215
|
+
logger.debug(`FilteringService.combineFilters: Final result has ${result.size} connections`, {
|
|
216
|
+
finalNames: Array.from(result.keys()),
|
|
217
|
+
});
|
|
218
|
+
return result;
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get a summary of filtering results for logging and debugging
|
|
223
|
+
*
|
|
224
|
+
* @param originalConnections Original connection map before filtering
|
|
225
|
+
* @param filteredConnections Filtered connection map after filtering
|
|
226
|
+
* @param config Filter configuration used
|
|
227
|
+
* @returns Summary object with filtering statistics
|
|
228
|
+
*/
|
|
229
|
+
static getFilteringSummary(originalConnections, filteredConnections, config) {
|
|
230
|
+
const originalNames = Array.from(originalConnections.keys());
|
|
231
|
+
const filteredNames = Array.from(filteredConnections.keys());
|
|
232
|
+
const removedNames = originalNames.filter((name) => !filteredNames.includes(name));
|
|
233
|
+
let filterType = 'none';
|
|
234
|
+
if (config.tagFilterMode === 'preset') {
|
|
235
|
+
filterType = 'preset';
|
|
236
|
+
}
|
|
237
|
+
else if (config.tagFilterMode === 'advanced') {
|
|
238
|
+
filterType = 'advanced';
|
|
239
|
+
}
|
|
240
|
+
else if (config.tagFilterMode === 'simple-or' || config.tags) {
|
|
241
|
+
filterType = 'simple-or';
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
original: originalConnections.size,
|
|
245
|
+
filtered: filteredConnections.size,
|
|
246
|
+
removed: removedNames.length,
|
|
247
|
+
filterType,
|
|
248
|
+
filteredNames: filteredNames.sort(),
|
|
249
|
+
removedNames: removedNames.sort(),
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
//# sourceMappingURL=filteringService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filteringService.js","sourceRoot":"","sources":["../../../src/core/filtering/filteringService.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAiB,MAAM,+BAA+B,CAAC;AAC9E,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAErE,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,MAAM,MAAM,wBAAwB,CAAC;AAO5C;;;;GAIG;AACH,MAAM,OAAO,gBAAgB;IAC3B;;;;;;;OAOG;IACI,MAAM,CAAC,sBAAsB,CAClC,WAAgC,EAChC,MAA+B;QAE/B,MAAM,CAAC,KAAK,CAAC,yCAAyC,EAAE;YACtD,gBAAgB,EAAE,WAAW,CAAC,IAAI;YAClC,UAAU,EAAE,MAAM,CAAC,aAAa;YAChC,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,gBAAgB,EAAE,CAAC,CAAC,MAAM,CAAC,aAAa;YACxC,WAAW,EAAE,CAAC,CAAC,MAAM,CAAC,QAAQ;SAC/B,CAAC,CAAC;QAEH,8CAA8C;QAC9C,MAAM,gBAAgB,GAAG,IAAI,GAAG,EAA8B,CAAC;QAC/D,KAAK,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,IAAI,WAAW,EAAE,CAAC;YAC7C,IAAI,UAAU,CAAC,MAAM,KAAK,YAAY,CAAC,SAAS,EAAE,CAAC;gBACjD,gBAAgB,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;YACzC,CAAC;QACH,CAAC;QAED,MAAM,CAAC,KAAK,CAAC,qCAAqC,EAAE;YAClD,cAAc,EAAE,gBAAgB,CAAC,IAAI;YACrC,cAAc,EAAE,KAAK,CAAC,IAA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C,IAAI,EAAE,CAAC;YAC/D,UAAU,GAAG,WAAW,CAAC;QAC3B,CAAC;QAED,OAAO;YACL,QAAQ,EAAE,mBAAmB,CAAC,IAAI;YAClC,QAAQ,EAAE,mBAAmB,CAAC,IAAI;YAClC,OAAO,EAAE,YAAY,CAAC,MAAM;YAC5B,UAAU;YACV,aAAa,EAAE,aAAa,CAAC,IAAI,EAAE;YACnC,YAAY,EAAE,YAAY,CAAC,IAAI,EAAE;SAClC,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { OutboundConnections, InboundConnectionConfig } from '../types/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Events emitted by InstructionAggregator
|
|
5
|
+
*/
|
|
6
|
+
export interface InstructionAggregatorEvents {
|
|
7
|
+
'instructions-changed': () => void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Aggregates instructions from multiple MCP servers into a single instruction string.
|
|
11
|
+
* Provides both simple concatenation and filtered instructions with educational templates.
|
|
12
|
+
* The aggregator acts as an educational prompt to help LLMs understand 1MCP better.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const aggregator = new InstructionAggregator();
|
|
17
|
+
* aggregator.on('instructions-changed', () => {
|
|
18
|
+
* // Server instructions have changed
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* // When server comes online
|
|
22
|
+
* aggregator.setInstructions('server1', 'Server 1 instructions');
|
|
23
|
+
*
|
|
24
|
+
* // Get filtered instructions for a client
|
|
25
|
+
* const filtered = aggregator.getFilteredInstructions(config, connections);
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare class InstructionAggregator extends EventEmitter {
|
|
29
|
+
private serverInstructions;
|
|
30
|
+
private isInitialized;
|
|
31
|
+
private templateCache;
|
|
32
|
+
constructor();
|
|
33
|
+
/**
|
|
34
|
+
* Set or update instructions for a specific server
|
|
35
|
+
* @param serverName The name of the server
|
|
36
|
+
* @param instructions The instruction string from the server, or undefined to remove
|
|
37
|
+
*/
|
|
38
|
+
setInstructions(serverName: string, instructions: string | undefined): void;
|
|
39
|
+
/**
|
|
40
|
+
* Remove instructions for a specific server
|
|
41
|
+
* @param serverName The name of the server to remove
|
|
42
|
+
*/
|
|
43
|
+
removeServer(serverName: string): void;
|
|
44
|
+
/**
|
|
45
|
+
* Get filtered instructions for a specific client based on their configuration
|
|
46
|
+
* This is the main method that should be used by server connections
|
|
47
|
+
*
|
|
48
|
+
* @param config Client's inbound connection configuration
|
|
49
|
+
* @param connections All available outbound connections
|
|
50
|
+
* @returns Formatted instruction string with educational template or custom template
|
|
51
|
+
*/
|
|
52
|
+
getFilteredInstructions(config: InboundConnectionConfig, connections: OutboundConnections): string;
|
|
53
|
+
/**
|
|
54
|
+
* Get the number of servers that have provided instructions
|
|
55
|
+
* @returns The count of servers with instructions
|
|
56
|
+
*/
|
|
57
|
+
getServerCount(): number;
|
|
58
|
+
/**
|
|
59
|
+
* Get a list of server names that have provided instructions
|
|
60
|
+
* @returns Array of server names
|
|
61
|
+
*/
|
|
62
|
+
getServerNames(): string[];
|
|
63
|
+
/**
|
|
64
|
+
* Check if a specific server has instructions
|
|
65
|
+
* @param serverName The server name to check
|
|
66
|
+
* @returns True if the server has instructions
|
|
67
|
+
*/
|
|
68
|
+
hasInstructions(serverName: string): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Get instructions for a specific server
|
|
71
|
+
* @param serverName The server name
|
|
72
|
+
* @returns The instructions for the server, or undefined if not found
|
|
73
|
+
*/
|
|
74
|
+
getServerInstructions(serverName: string): string | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Clear all instructions (useful for testing)
|
|
77
|
+
*/
|
|
78
|
+
clear(): void;
|
|
79
|
+
/**
|
|
80
|
+
* Get filter context description for the template
|
|
81
|
+
*/
|
|
82
|
+
private getFilterContext;
|
|
83
|
+
/**
|
|
84
|
+
* Get a summary of current instruction state for logging
|
|
85
|
+
*/
|
|
86
|
+
getSummary(): string;
|
|
87
|
+
/**
|
|
88
|
+
* Render a custom Handlebars template with template variables
|
|
89
|
+
* @param template Custom template string
|
|
90
|
+
* @param filteredConnections Filtered server connections
|
|
91
|
+
* @param config Client configuration
|
|
92
|
+
* @returns Rendered template string
|
|
93
|
+
*/
|
|
94
|
+
private renderCustomTemplate;
|
|
95
|
+
/**
|
|
96
|
+
* Get or compile a Handlebars template with caching
|
|
97
|
+
* @param template Template string
|
|
98
|
+
* @returns Compiled template function
|
|
99
|
+
*/
|
|
100
|
+
private getCompiledTemplate;
|
|
101
|
+
/**
|
|
102
|
+
* Generate template variables for rendering
|
|
103
|
+
* @param filteredConnections Filtered server connections
|
|
104
|
+
* @param config Client configuration
|
|
105
|
+
* @returns Template variables object
|
|
106
|
+
*/
|
|
107
|
+
private generateTemplateVariables;
|
|
108
|
+
/**
|
|
109
|
+
* Clear template cache (useful for testing)
|
|
110
|
+
*/
|
|
111
|
+
clearTemplateCache(): void;
|
|
112
|
+
/**
|
|
113
|
+
* Get template cache statistics for monitoring
|
|
114
|
+
*/
|
|
115
|
+
getTemplateCacheStats(): {
|
|
116
|
+
size: number;
|
|
117
|
+
maxSize: number;
|
|
118
|
+
calculatedSize: number;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Invalidate template cache with reason logging
|
|
122
|
+
* @param reason Reason for cache invalidation
|
|
123
|
+
*/
|
|
124
|
+
private invalidateTemplateCache;
|
|
125
|
+
/**
|
|
126
|
+
* Force template cache invalidation (for external triggers)
|
|
127
|
+
* @param reason Reason for forced invalidation
|
|
128
|
+
*/
|
|
129
|
+
forceTemplateCacheInvalidation(reason?: string): void;
|
|
130
|
+
/**
|
|
131
|
+
* Cryptographically secure hash function for template cache keys
|
|
132
|
+
* Uses SHA-256 to prevent collision attacks
|
|
133
|
+
*/
|
|
134
|
+
private hashString;
|
|
135
|
+
/**
|
|
136
|
+
* Cleanup method to remove all event listeners and clear caches
|
|
137
|
+
* Should be called when the aggregator is no longer needed
|
|
138
|
+
*/
|
|
139
|
+
cleanup(): void;
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=instructionAggregator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"instructionAggregator.d.ts","sourceRoot":"","sources":["../../../src/core/instructions/instructionAggregator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAKtC,OAAO,EAAE,mBAAmB,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAQjF;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C,sBAAsB,EAAE,MAAM,IAAI,CAAC;CACpC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,qBAAsB,SAAQ,YAAY;IACrD,OAAO,CAAC,kBAAkB,CAA6B;IACvD,OAAO,CAAC,aAAa,CAAkB;IACvC,OAAO,CAAC,aAAa,CAAgD;;IAuBrE;;;;OAIG;IACI,eAAe,CAAC,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI;IAuBlF;;;OAGG;IACI,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAU7C;;;;;;;OAOG;IACI,uBAAuB,CAAC,MAAM,EAAE,uBAAuB,EAAE,WAAW,EAAE,mBAAmB,GAAG,MAAM;IAoBzG;;;OAGG;IACI,cAAc,IAAI,MAAM;IAI/B;;;OAGG;IACI,cAAc,IAAI,MAAM,EAAE;IAIjC;;;;OAIG;IACI,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO;IAInD;;;;OAIG;IACI,qBAAqB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAIpE;;OAEG;IACI,KAAK,IAAI,IAAI;IAUpB;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAoBxB;;OAEG;IACI,UAAU,IAAI,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