(config: PrefixExtractorConfig)
| 90 | * @returns A memoized async function that extracts command prefixes |
| 91 | */ |
| 92 | export function createCommandPrefixExtractor(config: PrefixExtractorConfig) { |
| 93 | const { toolName, policySpec, eventName, querySource, preCheck } = config |
| 94 | |
| 95 | const memoized = memoizeWithLRU( |
| 96 | ( |
| 97 | command: string, |
| 98 | abortSignal: AbortSignal, |
| 99 | isNonInteractiveSession: boolean, |
| 100 | ): Promise<CommandPrefixResult | null> => { |
| 101 | const promise = getCommandPrefixImpl( |
| 102 | command, |
| 103 | abortSignal, |
| 104 | isNonInteractiveSession, |
| 105 | toolName, |
| 106 | policySpec, |
| 107 | eventName, |
| 108 | querySource, |
| 109 | preCheck, |
| 110 | ) |
| 111 | // Evict on rejection so aborted calls don't poison future turns. |
| 112 | // Identity guard: after LRU eviction, a newer promise may occupy |
| 113 | // this key; a stale rejection must not delete it. |
| 114 | promise.catch(() => { |
| 115 | if (memoized.cache.get(command) === promise) { |
| 116 | memoized.cache.delete(command) |
| 117 | } |
| 118 | }) |
| 119 | return promise |
| 120 | }, |
| 121 | command => command, // memoize by command only |
| 122 | 200, |
| 123 | ) |
| 124 | |
| 125 | return memoized |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Creates a memoized function to get prefixes for compound commands with subcommands. |
no test coverage detected