forked from dipseth/dataproc-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic-resolver.ts
More file actions
593 lines (507 loc) · 15.4 KB
/
dynamic-resolver.ts
File metadata and controls
593 lines (507 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/**
* Dynamic Resolver Service
* Parses and resolves dynamic template functions like {{job_output()}} and {{qdrant_query()}}
*/
import {
FunctionCall,
DynamicResolutionContext,
DynamicCacheEntry,
FunctionExecutionResult,
DynamicResolverConfig,
DynamicResolverMetrics,
DynamicResolutionError,
DynamicFunctionType,
} from '../types/dynamic-templating.js';
import { TemplateResolutionContext } from '../types/templating.js';
import { logger } from '../utils/logger.js';
/**
* Default configuration for dynamic resolver
*/
const DEFAULT_CONFIG: DynamicResolverConfig = {
enableCaching: true,
defaultTtlMs: 300000, // 5 minutes
maxCacheSize: 1000,
executionTimeoutMs: 10000, // 10 seconds
enableMetrics: true,
jobOutput: {
defaultTimeoutMs: 30000, // 30 seconds
waitForCompletion: false,
cacheTtlMs: 600000, // 10 minutes
},
qdrantQuery: {
defaultMinConfidence: 0.7,
defaultMaxResults: 5,
cacheTtlMs: 1800000, // 30 minutes
},
};
/**
* Dynamic Resolver class for parsing and executing template functions
*/
export class DynamicResolver {
private config: DynamicResolverConfig;
private cache: Map<string, DynamicCacheEntry> = new Map();
private metrics: DynamicResolverMetrics;
private context?: DynamicResolutionContext;
private cleanupInterval: NodeJS.Timeout | null = null;
constructor(config?: Partial<DynamicResolverConfig>) {
this.config = { ...DEFAULT_CONFIG, ...config };
this.metrics = this.initializeMetrics();
// Start cache cleanup interval
if (this.config.enableCaching) {
this.cleanupInterval = setInterval(() => this.cleanupCache(), 60000); // Every minute
}
logger.debug('DynamicResolver: Initialized with config', this.config);
}
/**
* Set the resolution context (services needed for function execution)
*/
setContext(context: DynamicResolutionContext): void {
this.context = context;
logger.debug('DynamicResolver: Context set');
}
/**
* Main entry point: resolve dynamic parameters in a parameter set
*/
async resolveDynamicParameters(
parameters: Record<string, unknown>,
templateContext: TemplateResolutionContext
): Promise<Record<string, unknown>> {
const startTime = Date.now();
if (!this.context) {
throw new Error('DynamicResolver: Context not set. Call setContext() first.');
}
// Update template context in resolution context
this.context.templateContext = templateContext;
const resolved: Record<string, unknown> = {};
const resolutionPromises: Promise<void>[] = [];
// Process each parameter
for (const [key, value] of Object.entries(parameters)) {
if (typeof value === 'string' && this.containsDynamicFunction(value)) {
// Resolve dynamic function asynchronously
resolutionPromises.push(
this.resolveParameterValue(key, value).then((resolvedValue) => {
resolved[key] = resolvedValue;
})
);
} else {
// Static value, copy as-is
resolved[key] = value;
}
}
// Wait for all dynamic resolutions to complete
await Promise.all(resolutionPromises);
const resolutionTime = Date.now() - startTime;
logger.debug(
`DynamicResolver: Resolved ${resolutionPromises.length} dynamic parameters in ${resolutionTime}ms`
);
return resolved;
}
/**
* Check if a string contains dynamic function calls
*/
private containsDynamicFunction(value: string): boolean {
return /\{\{[^}]+\}\}/.test(value);
}
/**
* Resolve a single parameter value that may contain dynamic functions
*/
private async resolveParameterValue(key: string, value: string): Promise<unknown> {
const functionCalls = this.parseFunctionCalls(value);
if (functionCalls.length === 0) {
return value; // No functions found
}
if (functionCalls.length === 1 && functionCalls[0].original === value) {
// Single function call that is the entire value
const result = await this.executeFunction(functionCalls[0]);
return result.value;
}
// Multiple functions or mixed content - replace each function with its result
let resolvedValue = value;
for (const functionCall of functionCalls) {
const result = await this.executeFunction(functionCall);
resolvedValue = resolvedValue.replace(functionCall.original, String(result.value));
}
return resolvedValue;
}
/**
* Parse function calls from a template string
*/
parseFunctionCalls(template: string): FunctionCall[] {
const functionCalls: FunctionCall[] = [];
const regex = /\{\{([^}]+)\}\}/g;
let match;
while ((match = regex.exec(template)) !== null) {
const fullMatch = match[0];
const functionContent = match[1].trim();
try {
const parsed = this.parseSingleFunction(functionContent);
if (parsed) {
functionCalls.push({
...parsed,
original: fullMatch,
position: {
start: match.index,
end: match.index + fullMatch.length,
},
});
}
} catch (error) {
logger.warn(`DynamicResolver: Failed to parse function "${functionContent}":`, error);
// Continue parsing other functions
}
}
return functionCalls;
}
/**
* Parse a single function call string
*/
private parseSingleFunction(
functionContent: string
): Omit<FunctionCall, 'original' | 'position'> | null {
// Match function_name(arg1, arg2, ...)
const match = functionContent.match(/^(\w+)\s*\(\s*(.*?)\s*\)$/);
if (!match) {
return null;
}
const [, functionName, argsString] = match;
// Validate function name
if (!this.isValidFunctionName(functionName)) {
throw new Error(`Unknown function: ${functionName}`);
}
// Parse arguments
const args = this.parseArguments(argsString);
return {
name: functionName as DynamicFunctionType,
args,
};
}
/**
* Check if function name is valid
*/
private isValidFunctionName(name: string): name is DynamicFunctionType {
return ['job_output', 'qdrant_query'].includes(name);
}
/**
* Parse function arguments from string
*/
private parseArguments(argsString: string): string[] {
if (!argsString.trim()) {
return [];
}
const args: string[] = [];
let current = '';
let inQuotes = false;
let quoteChar = '';
let depth = 0;
for (let i = 0; i < argsString.length; i++) {
const char = argsString[i];
const prevChar = i > 0 ? argsString[i - 1] : '';
if (!inQuotes && (char === '"' || char === "'")) {
inQuotes = true;
quoteChar = char;
current += char;
} else if (inQuotes && char === quoteChar && prevChar !== '\\') {
inQuotes = false;
quoteChar = '';
current += char;
} else if (!inQuotes && char === '(') {
depth++;
current += char;
} else if (!inQuotes && char === ')') {
depth--;
current += char;
} else if (!inQuotes && char === ',' && depth === 0) {
args.push(this.cleanArgument(current));
current = '';
} else {
current += char;
}
}
if (current.trim()) {
args.push(this.cleanArgument(current));
}
return args;
}
/**
* Clean and validate a single argument
*/
private cleanArgument(arg: string): string {
const cleaned = arg.trim();
// Remove surrounding quotes if present
if (
(cleaned.startsWith('"') && cleaned.endsWith('"')) ||
(cleaned.startsWith("'") && cleaned.endsWith("'"))
) {
return cleaned.slice(1, -1);
}
return cleaned;
}
/**
* Execute a parsed function call
*/
private async executeFunction(functionCall: FunctionCall): Promise<FunctionExecutionResult> {
const startTime = Date.now();
// Check cache first
if (this.config.enableCaching) {
const cached = this.getCachedResult(functionCall);
if (cached) {
this.updateMetrics(functionCall.name, true, Date.now() - startTime, true);
return {
value: cached.value,
success: true,
executionTimeMs: Date.now() - startTime,
cached: true,
metadata: cached.metadata,
};
}
}
try {
// Import and execute the appropriate function
const { TemplateFunctions } = await import('./template-functions.js');
const templateFunctions = new TemplateFunctions(this.context!);
let result: unknown;
switch (functionCall.name) {
case 'job_output':
if (functionCall.args.length !== 2) {
throw new Error(
`job_output requires exactly 2 arguments, got ${functionCall.args.length}`
);
}
result = await templateFunctions.jobOutput(functionCall.args[0], functionCall.args[1]);
break;
case 'qdrant_query':
if (functionCall.args.length !== 2) {
throw new Error(
`qdrant_query requires exactly 2 arguments, got ${functionCall.args.length}`
);
}
result = await templateFunctions.qdrantQuery(functionCall.args[0], functionCall.args[1]);
break;
default:
throw new Error(`Unsupported function: ${functionCall.name}`);
}
const executionTime = Date.now() - startTime;
// Cache the result
if (this.config.enableCaching) {
this.cacheResult(functionCall, result, executionTime);
}
this.updateMetrics(functionCall.name, true, executionTime, false);
return {
value: result,
success: true,
executionTimeMs: executionTime,
cached: false,
};
} catch (error) {
const executionTime = Date.now() - startTime;
this.updateMetrics(functionCall.name, false, executionTime, false);
logger.error(`DynamicResolver: Function execution failed for ${functionCall.name}:`, error);
throw new DynamicResolutionError(
`Failed to execute ${functionCall.name}: ${error instanceof Error ? error.message : 'Unknown error'}`,
functionCall,
error instanceof Error ? error : undefined
);
}
}
/**
* Get cached result for a function call
*/
private getCachedResult(functionCall: FunctionCall): DynamicCacheEntry | null {
const cacheKey = this.createCacheKey(functionCall);
const cached = this.cache.get(cacheKey);
if (!cached) {
return null;
}
// Check if cache entry is still valid
const age = Date.now() - cached.timestamp.getTime();
if (age > cached.ttl) {
this.cache.delete(cacheKey);
return null;
}
return cached;
}
/**
* Cache a function result
*/
private cacheResult(functionCall: FunctionCall, value: unknown, executionTimeMs: number): void {
const cacheKey = this.createCacheKey(functionCall);
const ttl = this.getTtlForFunction(functionCall.name);
const cacheEntry: DynamicCacheEntry = {
value,
timestamp: new Date(),
ttl,
functionCall,
metadata: {
resolutionTimeMs: executionTimeMs,
source: 'fresh',
},
};
this.cache.set(cacheKey, cacheEntry);
// Enforce cache size limit
if (this.cache.size > this.config.maxCacheSize) {
this.evictOldestCacheEntry();
}
}
/**
* Create cache key for a function call
*/
private createCacheKey(functionCall: FunctionCall): string {
return `${functionCall.name}:${functionCall.args.join(':')}`;
}
/**
* Get TTL for a specific function type
*/
private getTtlForFunction(functionName: DynamicFunctionType): number {
switch (functionName) {
case 'job_output':
return this.config.jobOutput.cacheTtlMs;
case 'qdrant_query':
return this.config.qdrantQuery.cacheTtlMs;
default:
return this.config.defaultTtlMs;
}
}
/**
* Evict oldest cache entry
*/
private evictOldestCacheEntry(): void {
let oldestKey: string | null = null;
let oldestTime = Date.now();
for (const [key, entry] of this.cache.entries()) {
if (entry.timestamp.getTime() < oldestTime) {
oldestTime = entry.timestamp.getTime();
oldestKey = key;
}
}
if (oldestKey) {
this.cache.delete(oldestKey);
}
}
/**
* Clean up expired cache entries
*/
private cleanupCache(): void {
const now = Date.now();
let cleanedCount = 0;
for (const [key, entry] of this.cache.entries()) {
const age = now - entry.timestamp.getTime();
if (age > entry.ttl) {
this.cache.delete(key);
cleanedCount++;
}
}
if (cleanedCount > 0) {
logger.debug(`DynamicResolver: Cleaned up ${cleanedCount} expired cache entries`);
}
}
/**
* Update metrics
*/
private updateMetrics(
functionName: DynamicFunctionType,
success: boolean,
executionTimeMs: number,
cached: boolean
): void {
if (!this.config.enableMetrics) {
return;
}
this.metrics.totalCalls++;
if (success) {
this.metrics.successfulResolutions++;
} else {
this.metrics.failedResolutions++;
}
if (cached) {
this.metrics.cacheHits++;
} else {
this.metrics.cacheMisses++;
}
// Update average resolution time
const totalTime =
this.metrics.averageResolutionTimeMs * (this.metrics.totalCalls - 1) + executionTimeMs;
this.metrics.averageResolutionTimeMs = totalTime / this.metrics.totalCalls;
// Update function-specific metrics
const funcMetrics = this.metrics.functionMetrics[functionName];
funcMetrics.calls++;
if (success) {
funcMetrics.successes++;
} else {
funcMetrics.failures++;
}
// Update function average time
const funcTotalTime = funcMetrics.averageTimeMs * (funcMetrics.calls - 1) + executionTimeMs;
funcMetrics.averageTimeMs = funcTotalTime / funcMetrics.calls;
}
/**
* Initialize metrics structure
*/
private initializeMetrics(): DynamicResolverMetrics {
return {
totalCalls: 0,
successfulResolutions: 0,
failedResolutions: 0,
cacheHits: 0,
cacheMisses: 0,
averageResolutionTimeMs: 0,
functionMetrics: {
job_output: {
calls: 0,
successes: 0,
failures: 0,
averageTimeMs: 0,
},
qdrant_query: {
calls: 0,
successes: 0,
failures: 0,
averageTimeMs: 0,
averageConfidence: 0,
},
},
};
}
/**
* Get current metrics
*/
getMetrics(): DynamicResolverMetrics {
return { ...this.metrics };
}
/**
* Clear cache
*/
clearCache(): void {
this.cache.clear();
logger.debug('DynamicResolver: Cache cleared');
}
/**
* Get cache statistics
*/
getCacheStats(): {
size: number;
maxSize: number;
hitRate: number;
} {
const totalRequests = this.metrics.cacheHits + this.metrics.cacheMisses;
const hitRate = totalRequests > 0 ? this.metrics.cacheHits / totalRequests : 0;
return {
size: this.cache.size,
maxSize: this.config.maxCacheSize,
hitRate,
};
}
/**
* Graceful shutdown - cleanup intervals and resources
*/
async shutdown(): Promise<void> {
logger.info('DynamicResolver: Initiating graceful shutdown');
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
this.cleanupInterval = null;
logger.debug('DynamicResolver: Cleanup interval stopped');
}
// Clear cache
this.cache.clear();
logger.info('DynamicResolver: Shutdown complete');
}
}