File size: 3,738 Bytes
7a1ad33 | 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 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { HookRegistry, HookRegistryEntry } from './hookRegistry.js';
import {
getHookKey,
type HookExecutionPlan,
type HookEventName,
} from './types.js';
import { debugLogger } from '../utils/debugLogger.js';
/**
* Hook planner that selects matching hooks and creates execution plans
*/
export class HookPlanner {
private readonly hookRegistry: HookRegistry;
constructor(hookRegistry: HookRegistry) {
this.hookRegistry = hookRegistry;
}
/**
* Create execution plan for a hook event
*/
createExecutionPlan(
eventName: HookEventName,
context?: HookEventContext,
): HookExecutionPlan | null {
const hookEntries = this.hookRegistry.getHooksForEvent(eventName);
if (hookEntries.length === 0) {
return null;
}
// Filter hooks by matcher
const matchingEntries = hookEntries.filter((entry) =>
this.matchesContext(entry, context),
);
if (matchingEntries.length === 0) {
return null;
}
// Deduplicate identical hooks
const deduplicatedEntries = this.deduplicateHooks(matchingEntries);
// Extract hook configs
const hookConfigs = deduplicatedEntries.map((entry) => entry.config);
// Determine execution strategy - if ANY hook definition has sequential=true, run all sequentially
const sequential = deduplicatedEntries.some(
(entry) => entry.sequential === true,
);
const plan: HookExecutionPlan = {
eventName,
hookConfigs,
sequential,
};
debugLogger.debug(
`Created execution plan for ${eventName}: ${hookConfigs.length} hook(s) to execute ${sequential ? 'sequentially' : 'in parallel'}`,
);
return plan;
}
/**
* Check if a hook entry matches the given context
*/
private matchesContext(
entry: HookRegistryEntry,
context?: HookEventContext,
): boolean {
if (!entry.matcher || !context) {
return true; // No matcher means match all
}
const matcher = entry.matcher.trim();
if (matcher === '' || matcher === '*') {
return true; // Empty string or wildcard matches all
}
// For tool events, match against tool name
if (context.toolName) {
return this.matchesToolName(matcher, context.toolName);
}
// For other events, match against trigger/source
if (context.trigger) {
return this.matchesTrigger(matcher, context.trigger);
}
return true;
}
/**
* Match tool name against matcher pattern
*/
private matchesToolName(matcher: string, toolName: string): boolean {
try {
// Attempt to treat the matcher as a regular expression.
const regex = new RegExp(matcher);
return regex.test(toolName);
} catch {
// If it's not a valid regex, treat it as a literal string for an exact match.
return matcher === toolName;
}
}
/**
* Match trigger/source against matcher pattern
*/
private matchesTrigger(matcher: string, trigger: string): boolean {
return matcher === trigger;
}
/**
* Deduplicate identical hook configurations
*/
private deduplicateHooks(entries: HookRegistryEntry[]): HookRegistryEntry[] {
const seen = new Set<string>();
const deduplicated: HookRegistryEntry[] = [];
for (const entry of entries) {
const key = getHookKey(entry.config);
if (!seen.has(key)) {
seen.add(key);
deduplicated.push(entry);
} else {
debugLogger.debug(`Deduplicated hook: ${key}`);
}
}
return deduplicated;
}
}
/**
* Context information for hook event matching
*/
export interface HookEventContext {
toolName?: string;
trigger?: string;
}
|