unreal-engine-mcp-server 0.4.0 → 0.4.4

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.
Files changed (135) hide show
  1. package/.env.production +1 -1
  2. package/.github/copilot-instructions.md +45 -0
  3. package/.github/workflows/publish-mcp.yml +3 -2
  4. package/README.md +21 -5
  5. package/dist/index.js +124 -31
  6. package/dist/prompts/index.d.ts +10 -3
  7. package/dist/prompts/index.js +186 -7
  8. package/dist/resources/actors.d.ts +19 -1
  9. package/dist/resources/actors.js +55 -64
  10. package/dist/resources/assets.js +46 -62
  11. package/dist/resources/levels.d.ts +21 -3
  12. package/dist/resources/levels.js +29 -54
  13. package/dist/tools/actors.d.ts +3 -14
  14. package/dist/tools/actors.js +246 -302
  15. package/dist/tools/animation.d.ts +57 -102
  16. package/dist/tools/animation.js +429 -450
  17. package/dist/tools/assets.d.ts +13 -2
  18. package/dist/tools/assets.js +52 -44
  19. package/dist/tools/audio.d.ts +22 -13
  20. package/dist/tools/audio.js +467 -121
  21. package/dist/tools/blueprint.d.ts +32 -13
  22. package/dist/tools/blueprint.js +699 -448
  23. package/dist/tools/build_environment_advanced.d.ts +0 -1
  24. package/dist/tools/build_environment_advanced.js +190 -45
  25. package/dist/tools/consolidated-tool-definitions.js +78 -252
  26. package/dist/tools/consolidated-tool-handlers.js +506 -133
  27. package/dist/tools/debug.d.ts +72 -10
  28. package/dist/tools/debug.js +167 -31
  29. package/dist/tools/editor.d.ts +9 -2
  30. package/dist/tools/editor.js +30 -44
  31. package/dist/tools/foliage.d.ts +34 -15
  32. package/dist/tools/foliage.js +97 -107
  33. package/dist/tools/introspection.js +19 -21
  34. package/dist/tools/landscape.d.ts +1 -2
  35. package/dist/tools/landscape.js +311 -168
  36. package/dist/tools/level.d.ts +3 -28
  37. package/dist/tools/level.js +642 -192
  38. package/dist/tools/lighting.d.ts +14 -3
  39. package/dist/tools/lighting.js +236 -123
  40. package/dist/tools/materials.d.ts +25 -7
  41. package/dist/tools/materials.js +102 -79
  42. package/dist/tools/niagara.d.ts +10 -12
  43. package/dist/tools/niagara.js +74 -94
  44. package/dist/tools/performance.d.ts +12 -4
  45. package/dist/tools/performance.js +38 -79
  46. package/dist/tools/physics.d.ts +34 -10
  47. package/dist/tools/physics.js +364 -292
  48. package/dist/tools/rc.js +97 -23
  49. package/dist/tools/sequence.d.ts +1 -0
  50. package/dist/tools/sequence.js +125 -22
  51. package/dist/tools/ui.d.ts +31 -4
  52. package/dist/tools/ui.js +83 -66
  53. package/dist/tools/visual.d.ts +11 -0
  54. package/dist/tools/visual.js +245 -30
  55. package/dist/types/tool-types.d.ts +0 -6
  56. package/dist/types/tool-types.js +1 -8
  57. package/dist/unreal-bridge.d.ts +32 -2
  58. package/dist/unreal-bridge.js +621 -127
  59. package/dist/utils/elicitation.d.ts +57 -0
  60. package/dist/utils/elicitation.js +104 -0
  61. package/dist/utils/error-handler.d.ts +0 -33
  62. package/dist/utils/error-handler.js +4 -111
  63. package/dist/utils/http.d.ts +2 -22
  64. package/dist/utils/http.js +12 -75
  65. package/dist/utils/normalize.d.ts +4 -4
  66. package/dist/utils/normalize.js +15 -7
  67. package/dist/utils/python-output.d.ts +18 -0
  68. package/dist/utils/python-output.js +290 -0
  69. package/dist/utils/python.d.ts +2 -0
  70. package/dist/utils/python.js +4 -0
  71. package/dist/utils/response-validator.js +28 -2
  72. package/dist/utils/result-helpers.d.ts +27 -0
  73. package/dist/utils/result-helpers.js +147 -0
  74. package/dist/utils/safe-json.d.ts +0 -2
  75. package/dist/utils/safe-json.js +0 -43
  76. package/dist/utils/validation.d.ts +16 -0
  77. package/dist/utils/validation.js +70 -7
  78. package/mcp-config-example.json +2 -2
  79. package/package.json +10 -9
  80. package/server.json +37 -14
  81. package/src/index.ts +130 -33
  82. package/src/prompts/index.ts +211 -13
  83. package/src/resources/actors.ts +59 -44
  84. package/src/resources/assets.ts +48 -51
  85. package/src/resources/levels.ts +35 -45
  86. package/src/tools/actors.ts +269 -313
  87. package/src/tools/animation.ts +556 -539
  88. package/src/tools/assets.ts +53 -43
  89. package/src/tools/audio.ts +507 -113
  90. package/src/tools/blueprint.ts +778 -462
  91. package/src/tools/build_environment_advanced.ts +266 -64
  92. package/src/tools/consolidated-tool-definitions.ts +90 -264
  93. package/src/tools/consolidated-tool-handlers.ts +630 -121
  94. package/src/tools/debug.ts +176 -33
  95. package/src/tools/editor.ts +35 -37
  96. package/src/tools/foliage.ts +110 -104
  97. package/src/tools/introspection.ts +24 -22
  98. package/src/tools/landscape.ts +334 -181
  99. package/src/tools/level.ts +683 -182
  100. package/src/tools/lighting.ts +244 -123
  101. package/src/tools/materials.ts +114 -83
  102. package/src/tools/niagara.ts +87 -81
  103. package/src/tools/performance.ts +49 -88
  104. package/src/tools/physics.ts +393 -299
  105. package/src/tools/rc.ts +102 -24
  106. package/src/tools/sequence.ts +136 -28
  107. package/src/tools/ui.ts +101 -70
  108. package/src/tools/visual.ts +250 -29
  109. package/src/types/tool-types.ts +0 -9
  110. package/src/unreal-bridge.ts +658 -140
  111. package/src/utils/elicitation.ts +129 -0
  112. package/src/utils/error-handler.ts +4 -159
  113. package/src/utils/http.ts +16 -115
  114. package/src/utils/normalize.ts +20 -10
  115. package/src/utils/python-output.ts +351 -0
  116. package/src/utils/python.ts +3 -0
  117. package/src/utils/response-validator.ts +25 -2
  118. package/src/utils/result-helpers.ts +193 -0
  119. package/src/utils/safe-json.ts +0 -50
  120. package/src/utils/validation.ts +94 -7
  121. package/tests/run-unreal-tool-tests.mjs +720 -0
  122. package/tsconfig.json +2 -2
  123. package/dist/python-utils.d.ts +0 -29
  124. package/dist/python-utils.js +0 -54
  125. package/dist/types/index.d.ts +0 -323
  126. package/dist/types/index.js +0 -28
  127. package/dist/utils/cache-manager.d.ts +0 -64
  128. package/dist/utils/cache-manager.js +0 -176
  129. package/dist/utils/errors.d.ts +0 -133
  130. package/dist/utils/errors.js +0 -256
  131. package/src/python/editor_compat.py +0 -181
  132. package/src/python-utils.ts +0 -57
  133. package/src/types/index.ts +0 -414
  134. package/src/utils/cache-manager.ts +0 -213
  135. package/src/utils/errors.ts +0 -312
package/tsconfig.json CHANGED
@@ -15,8 +15,8 @@
15
15
  "resolveJsonModule": true,
16
16
  "incremental": true,
17
17
  "tsBuildInfoFile": ".tsbuildinfo",
18
- "noUnusedLocals": false,
19
- "noUnusedParameters": false,
18
+ "noUnusedLocals": false,
19
+ "noUnusedParameters": false,
20
20
  "noImplicitReturns": true,
21
21
  "noFallthroughCasesInSwitch": true,
22
22
  "allowUnreachableCode": false,
@@ -1,29 +0,0 @@
1
- /**
2
- * Python Utilities for Modern Unreal Engine API
3
- *
4
- * This module provides Python code snippets that use the modern
5
- * Unreal Engine Python API instead of deprecated functions.
6
- */
7
- export declare class PythonUtils {
8
- /**
9
- * Get all actors in the level using modern API
10
- * @returns Python code to get all level actors
11
- */
12
- static getAllLevelActors(): string;
13
- /**
14
- * Get selected actors using modern API
15
- * @returns Python code to get selected actors
16
- */
17
- static getSelectedActors(): string;
18
- /**
19
- * Spawn actor from class using modern API
20
- * @returns Python code to spawn actor
21
- */
22
- static spawnActorFromClass(): string;
23
- /**
24
- * Get a safe way to access actors
25
- * @returns Python code with modern API
26
- */
27
- static getSafeActorAccess(): string;
28
- }
29
- //# sourceMappingURL=python-utils.d.ts.map
@@ -1,54 +0,0 @@
1
- /**
2
- * Python Utilities for Modern Unreal Engine API
3
- *
4
- * This module provides Python code snippets that use the modern
5
- * Unreal Engine Python API instead of deprecated functions.
6
- */
7
- export class PythonUtils {
8
- /**
9
- * Get all actors in the level using modern API
10
- * @returns Python code to get all level actors
11
- */
12
- static getAllLevelActors() {
13
- return `
14
- # Use modern EditorActorSubsystem instead of deprecated EditorLevelLibrary
15
- editor_actor_subsystem = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
16
- actors = editor_actor_subsystem.get_all_level_actors()
17
- `.trim();
18
- }
19
- /**
20
- * Get selected actors using modern API
21
- * @returns Python code to get selected actors
22
- */
23
- static getSelectedActors() {
24
- return `
25
- editor_actor_subsystem = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
26
- selected_actors = editor_actor_subsystem.get_selected_level_actors()
27
- `.trim();
28
- }
29
- /**
30
- * Spawn actor from class using modern API
31
- * @returns Python code to spawn actor
32
- */
33
- static spawnActorFromClass() {
34
- return `
35
- editor_actor_subsystem = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
36
- # Use spawn_actor_from_class for spawning
37
- `.trim();
38
- }
39
- /**
40
- * Get a safe way to access actors
41
- * @returns Python code with modern API
42
- */
43
- static getSafeActorAccess() {
44
- return `
45
- # Use modern API
46
- try:
47
- editor_actor_subsystem = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
48
- actors = editor_actor_subsystem.get_all_level_actors() if editor_actor_subsystem else []
49
- except:
50
- actors = []
51
- `.trim();
52
- }
53
- }
54
- //# sourceMappingURL=python-utils.js.map
@@ -1,323 +0,0 @@
1
- /**
2
- * Comprehensive type definitions for Unreal Engine MCP Server
3
- */
4
- export * from './env.js';
5
- export * from './tool-types.js';
6
- export interface Vector3 {
7
- x: number;
8
- y: number;
9
- z: number;
10
- }
11
- export interface Rotation3 {
12
- pitch: number;
13
- yaw: number;
14
- roll: number;
15
- }
16
- export interface Transform {
17
- location: Vector3;
18
- rotation: Rotation3;
19
- scale?: Vector3;
20
- }
21
- export interface Color {
22
- r: number;
23
- g: number;
24
- b: number;
25
- a?: number;
26
- }
27
- export interface Asset {
28
- Name: string;
29
- Path: string;
30
- Class: string;
31
- PackagePath: string;
32
- Type?: string;
33
- Size?: number;
34
- LastModified?: Date;
35
- }
36
- export interface Material extends Asset {
37
- BaseColor?: Color;
38
- Metallic?: number;
39
- Roughness?: number;
40
- Emissive?: Color;
41
- }
42
- export interface Texture extends Asset {
43
- Width?: number;
44
- Height?: number;
45
- Format?: string;
46
- MipLevels?: number;
47
- }
48
- export interface Actor {
49
- Name: string;
50
- Class: string;
51
- Path: string;
52
- Transform?: Transform;
53
- Components?: Component[];
54
- Tags?: string[];
55
- IsSelected?: boolean;
56
- IsHidden?: boolean;
57
- }
58
- export interface Component {
59
- Name: string;
60
- Class: string;
61
- Properties?: Record<string, any>;
62
- }
63
- export interface Level {
64
- Name: string;
65
- Path: string;
66
- IsLoaded: boolean;
67
- IsVisible: boolean;
68
- Actors?: Actor[];
69
- StreamingLevels?: StreamingLevel[];
70
- }
71
- export interface StreamingLevel {
72
- Name: string;
73
- Path: string;
74
- LoadingState: 'Unloaded' | 'Loading' | 'Loaded';
75
- ShouldBeLoaded: boolean;
76
- ShouldBeVisible: boolean;
77
- }
78
- export interface Blueprint {
79
- Name: string;
80
- Path: string;
81
- ParentClass: string;
82
- Components?: BlueprintComponent[];
83
- Variables?: BlueprintVariable[];
84
- Functions?: BlueprintFunction[];
85
- }
86
- export interface BlueprintComponent {
87
- Name: string;
88
- Type: string;
89
- DefaultProperties?: Record<string, any>;
90
- }
91
- export interface BlueprintVariable {
92
- Name: string;
93
- Type: string;
94
- DefaultValue?: any;
95
- IsPublic?: boolean;
96
- Category?: string;
97
- }
98
- export interface BlueprintFunction {
99
- Name: string;
100
- ReturnType?: string;
101
- Parameters?: FunctionParameter[];
102
- IsPublic?: boolean;
103
- }
104
- export interface FunctionParameter {
105
- Name: string;
106
- Type: string;
107
- DefaultValue?: any;
108
- IsOptional?: boolean;
109
- }
110
- export interface AnimationSequence {
111
- Name: string;
112
- Path: string;
113
- Duration: number;
114
- FrameRate: number;
115
- Skeleton?: string;
116
- }
117
- export interface AnimationMontage {
118
- Name: string;
119
- Path: string;
120
- Sections?: MontageSection[];
121
- BlendIn?: number;
122
- BlendOut?: number;
123
- }
124
- export interface MontageSection {
125
- Name: string;
126
- StartTime: number;
127
- EndTime: number;
128
- NextSection?: string;
129
- }
130
- export interface PhysicsBody {
131
- Mass: number;
132
- LinearDamping: number;
133
- AngularDamping: number;
134
- EnableGravity: boolean;
135
- IsKinematic: boolean;
136
- CollisionEnabled: boolean;
137
- }
138
- export interface PhysicsConstraint {
139
- Name: string;
140
- Actor1: string;
141
- Actor2: string;
142
- LinearLimits?: Vector3;
143
- AngularLimits?: Vector3;
144
- }
145
- export interface NiagaraSystem {
146
- Name: string;
147
- Path: string;
148
- Emitters?: NiagaraEmitter[];
149
- Parameters?: NiagaraParameter[];
150
- }
151
- export interface NiagaraEmitter {
152
- Name: string;
153
- SpawnRate: number;
154
- Lifetime: number;
155
- VelocityModule?: Vector3;
156
- ColorModule?: Color;
157
- }
158
- export interface NiagaraParameter {
159
- Name: string;
160
- Type: string;
161
- Value: any;
162
- }
163
- export interface Landscape {
164
- Name: string;
165
- ComponentCount: number;
166
- Resolution: {
167
- x: number;
168
- y: number;
169
- };
170
- Scale: Vector3;
171
- Materials?: string[];
172
- Layers?: LandscapeLayer[];
173
- }
174
- export interface LandscapeLayer {
175
- Name: string;
176
- BlendMode: string;
177
- Weight: number;
178
- }
179
- export interface RemoteControlPreset {
180
- Name: string;
181
- Path: string;
182
- ExposedProperties?: ExposedProperty[];
183
- ExposedFunctions?: ExposedFunction[];
184
- }
185
- export interface ExposedProperty {
186
- Name: string;
187
- DisplayName?: string;
188
- ObjectPath: string;
189
- PropertyPath: string;
190
- Type: string;
191
- Value?: any;
192
- Metadata?: Record<string, any>;
193
- }
194
- export interface ExposedFunction {
195
- Name: string;
196
- DisplayName?: string;
197
- ObjectPath: string;
198
- FunctionName: string;
199
- Parameters?: FunctionParameter[];
200
- }
201
- export interface LevelSequence {
202
- Name: string;
203
- Path: string;
204
- Duration: number;
205
- FrameRate: number;
206
- Tracks?: SequencerTrack[];
207
- }
208
- export interface SequencerTrack {
209
- Name: string;
210
- Type: string;
211
- BoundObject?: string;
212
- Sections?: SequencerSection[];
213
- }
214
- export interface SequencerSection {
215
- StartFrame: number;
216
- EndFrame: number;
217
- Properties?: Record<string, any>;
218
- }
219
- export interface PerformanceMetrics {
220
- FPS: number;
221
- FrameTime: number;
222
- GameThreadTime: number;
223
- RenderThreadTime: number;
224
- GPUTime: number;
225
- DrawCalls: number;
226
- Triangles: number;
227
- Memory: MemoryMetrics;
228
- }
229
- export interface MemoryMetrics {
230
- Physical: number;
231
- Virtual: number;
232
- GPU: number;
233
- TextureMemory: number;
234
- MeshMemory: number;
235
- }
236
- export interface EngineVersion {
237
- Major: number;
238
- Minor: number;
239
- Patch: number;
240
- Build?: number;
241
- Branch?: string;
242
- Compatible?: boolean;
243
- }
244
- export interface ProjectInfo {
245
- Name: string;
246
- Path: string;
247
- EngineVersion: string;
248
- Plugins?: PluginInfo[];
249
- Settings?: ProjectSettings;
250
- }
251
- export interface PluginInfo {
252
- Name: string;
253
- Version: string;
254
- Enabled: boolean;
255
- Category?: string;
256
- }
257
- export interface ProjectSettings {
258
- DefaultMap?: string;
259
- DefaultGameMode?: string;
260
- TargetFrameRate?: number;
261
- EnableRayTracing?: boolean;
262
- EnableNanite?: boolean;
263
- }
264
- export interface SuccessResponse<T = any> {
265
- success: true;
266
- data?: T;
267
- message?: string;
268
- metadata?: Record<string, any>;
269
- }
270
- export interface ErrorResponse {
271
- success: false;
272
- error: string;
273
- code?: string;
274
- statusCode?: number;
275
- details?: Record<string, any>;
276
- }
277
- export type ApiResponse<T = any> = SuccessResponse<T> | ErrorResponse;
278
- export interface ToolContext {
279
- bridge: any;
280
- tools: Record<string, any>;
281
- cache?: any;
282
- metrics?: any;
283
- }
284
- export interface ToolResult<T = any> {
285
- content: Array<{
286
- type: 'text' | 'json' | 'error';
287
- text?: string;
288
- data?: T;
289
- }>;
290
- isError?: boolean;
291
- metadata?: Record<string, any>;
292
- }
293
- export interface UnrealEvent {
294
- type: string;
295
- timestamp: Date;
296
- data: any;
297
- source?: string;
298
- }
299
- export interface PropertyChangeEvent extends UnrealEvent {
300
- type: 'property_change';
301
- objectPath: string;
302
- propertyName: string;
303
- oldValue: any;
304
- newValue: any;
305
- }
306
- export interface ActorSpawnEvent extends UnrealEvent {
307
- type: 'actor_spawn';
308
- actorName: string;
309
- actorClass: string;
310
- location: Vector3;
311
- }
312
- export type DeepPartial<T> = {
313
- [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
314
- };
315
- export type Nullable<T> = T | null;
316
- export type Optional<T> = T | undefined;
317
- export type AsyncResult<T> = Promise<ApiResponse<T>>;
318
- export type Callback<T> = (error: Error | null, result?: T) => void;
319
- export declare function isVector3(value: any): value is Vector3;
320
- export declare function isRotation3(value: any): value is Rotation3;
321
- export declare function isSuccessResponse<T>(response: ApiResponse<T>): response is SuccessResponse<T>;
322
- export declare function isErrorResponse(response: ApiResponse): response is ErrorResponse;
323
- //# sourceMappingURL=index.d.ts.map
@@ -1,28 +0,0 @@
1
- /**
2
- * Comprehensive type definitions for Unreal Engine MCP Server
3
- */
4
- // Re-export existing types
5
- export * from './env.js';
6
- export * from './tool-types.js';
7
- // Type guards
8
- export function isVector3(value) {
9
- return (typeof value === 'object' &&
10
- value !== null &&
11
- typeof value.x === 'number' &&
12
- typeof value.y === 'number' &&
13
- typeof value.z === 'number');
14
- }
15
- export function isRotation3(value) {
16
- return (typeof value === 'object' &&
17
- value !== null &&
18
- typeof value.pitch === 'number' &&
19
- typeof value.yaw === 'number' &&
20
- typeof value.roll === 'number');
21
- }
22
- export function isSuccessResponse(response) {
23
- return response.success === true;
24
- }
25
- export function isErrorResponse(response) {
26
- return response.success === false;
27
- }
28
- //# sourceMappingURL=index.js.map
@@ -1,64 +0,0 @@
1
- /**
2
- * Cache Manager for API responses
3
- * Implements LRU cache with TTL support for optimizing repeated API calls
4
- */
5
- interface CacheOptions {
6
- maxSize?: number;
7
- defaultTTL?: number;
8
- enableMetrics?: boolean;
9
- }
10
- interface CacheMetrics {
11
- hits: number;
12
- misses: number;
13
- evictions: number;
14
- size: number;
15
- }
16
- export declare class CacheManager<T = any> {
17
- private cache;
18
- private readonly maxSize;
19
- private readonly defaultTTL;
20
- private readonly enableMetrics;
21
- private metrics;
22
- constructor(options?: CacheOptions);
23
- /**
24
- * Get item from cache
25
- */
26
- get(key: string): T | null;
27
- /**
28
- * Set item in cache
29
- */
30
- set(key: string, data: T): void;
31
- /**
32
- * Check if key exists and is valid
33
- */
34
- has(key: string): boolean;
35
- /**
36
- * Clear specific key or all cache
37
- */
38
- clear(key?: string): void;
39
- /**
40
- * Get cache metrics
41
- */
42
- getMetrics(): CacheMetrics;
43
- /**
44
- * Get cache hit rate
45
- */
46
- getHitRate(): number;
47
- /**
48
- * Wrap async function with cache
49
- */
50
- wrap<R = T>(key: string, fn: () => Promise<R>): Promise<R>;
51
- /**
52
- * Batch get multiple keys
53
- */
54
- getBatch(keys: string[]): Map<string, T | null>;
55
- /**
56
- * Invalidate cache entries by pattern
57
- */
58
- invalidatePattern(pattern: RegExp): number;
59
- }
60
- export declare const assetCache: CacheManager<any>;
61
- export declare const engineCache: CacheManager<any>;
62
- export declare const commandCache: CacheManager<any>;
63
- export {};
64
- //# sourceMappingURL=cache-manager.d.ts.map
@@ -1,176 +0,0 @@
1
- /**
2
- * Cache Manager for API responses
3
- * Implements LRU cache with TTL support for optimizing repeated API calls
4
- */
5
- export class CacheManager {
6
- cache;
7
- maxSize;
8
- defaultTTL;
9
- enableMetrics;
10
- metrics;
11
- constructor(options = {}) {
12
- this.cache = new Map();
13
- this.maxSize = options.maxSize || 100;
14
- this.defaultTTL = options.defaultTTL || 60000; // 1 minute default
15
- this.enableMetrics = options.enableMetrics || false;
16
- this.metrics = {
17
- hits: 0,
18
- misses: 0,
19
- evictions: 0,
20
- size: 0
21
- };
22
- }
23
- /**
24
- * Get item from cache
25
- */
26
- get(key) {
27
- const entry = this.cache.get(key);
28
- if (!entry) {
29
- if (this.enableMetrics)
30
- this.metrics.misses++;
31
- return null;
32
- }
33
- // Check if expired
34
- if (Date.now() - entry.timestamp > this.defaultTTL) {
35
- this.cache.delete(key);
36
- if (this.enableMetrics) {
37
- this.metrics.misses++;
38
- this.metrics.size--;
39
- }
40
- return null;
41
- }
42
- // Update hit count and move to end (LRU)
43
- entry.hits++;
44
- this.cache.delete(key);
45
- this.cache.set(key, entry);
46
- if (this.enableMetrics)
47
- this.metrics.hits++;
48
- return entry.data;
49
- }
50
- /**
51
- * Set item in cache
52
- */
53
- set(key, data) {
54
- // Evict oldest if at max size
55
- if (this.cache.size >= this.maxSize && !this.cache.has(key)) {
56
- const firstKey = this.cache.keys().next().value;
57
- if (firstKey) {
58
- this.cache.delete(firstKey);
59
- if (this.enableMetrics) {
60
- this.metrics.evictions++;
61
- this.metrics.size--;
62
- }
63
- }
64
- }
65
- const entry = {
66
- data,
67
- timestamp: Date.now(),
68
- hits: 0
69
- };
70
- this.cache.set(key, entry);
71
- if (this.enableMetrics)
72
- this.metrics.size = this.cache.size;
73
- }
74
- /**
75
- * Check if key exists and is valid
76
- */
77
- has(key) {
78
- const entry = this.cache.get(key);
79
- if (!entry)
80
- return false;
81
- // Check expiration
82
- if (Date.now() - entry.timestamp > this.defaultTTL) {
83
- this.cache.delete(key);
84
- if (this.enableMetrics)
85
- this.metrics.size--;
86
- return false;
87
- }
88
- return true;
89
- }
90
- /**
91
- * Clear specific key or all cache
92
- */
93
- clear(key) {
94
- if (key) {
95
- this.cache.delete(key);
96
- if (this.enableMetrics)
97
- this.metrics.size = this.cache.size;
98
- }
99
- else {
100
- this.cache.clear();
101
- if (this.enableMetrics) {
102
- this.metrics.size = 0;
103
- }
104
- }
105
- }
106
- /**
107
- * Get cache metrics
108
- */
109
- getMetrics() {
110
- return { ...this.metrics };
111
- }
112
- /**
113
- * Get cache hit rate
114
- */
115
- getHitRate() {
116
- const total = this.metrics.hits + this.metrics.misses;
117
- return total > 0 ? this.metrics.hits / total : 0;
118
- }
119
- /**
120
- * Wrap async function with cache
121
- */
122
- async wrap(key, fn) {
123
- // Check cache first
124
- const cached = this.get(key);
125
- if (cached !== null) {
126
- return cached;
127
- }
128
- // Execute function and cache result
129
- const result = await fn();
130
- this.set(key, result);
131
- return result;
132
- }
133
- /**
134
- * Batch get multiple keys
135
- */
136
- getBatch(keys) {
137
- const results = new Map();
138
- for (const key of keys) {
139
- results.set(key, this.get(key));
140
- }
141
- return results;
142
- }
143
- /**
144
- * Invalidate cache entries by pattern
145
- */
146
- invalidatePattern(pattern) {
147
- let count = 0;
148
- for (const key of this.cache.keys()) {
149
- if (pattern.test(key)) {
150
- this.cache.delete(key);
151
- count++;
152
- }
153
- }
154
- if (this.enableMetrics) {
155
- this.metrics.size = this.cache.size;
156
- }
157
- return count;
158
- }
159
- }
160
- // Global cache instances for different purposes
161
- export const assetCache = new CacheManager({
162
- maxSize: 500,
163
- defaultTTL: 300000, // 5 minutes for assets
164
- enableMetrics: true
165
- });
166
- export const engineCache = new CacheManager({
167
- maxSize: 50,
168
- defaultTTL: 600000, // 10 minutes for engine info
169
- enableMetrics: true
170
- });
171
- export const commandCache = new CacheManager({
172
- maxSize: 100,
173
- defaultTTL: 30000, // 30 seconds for commands
174
- enableMetrics: true
175
- });
176
- //# sourceMappingURL=cache-manager.js.map