* Replaces `${...}` placeholders in a template string with values from a context. * * This function identifies all placeholders in the format `${key}`, validates that * each key exists in the provided `ContextState`, and then performs the substitution. * * @param template The template string co
(template: string, context: ContextState)
| 199 | * @throws {Error} if any placeholder key is not found in the context. |
| 200 | */ |
| 201 | function templateString(template: string, context: ContextState): string { |
| 202 | const placeholderRegex = /\$\{(\w+)\}/g; |
| 203 | |
| 204 | // First, find all unique keys required by the template. |
| 205 | const requiredKeys = new Set( |
| 206 | Array.from(template.matchAll(placeholderRegex), (match) => match[1]), |
| 207 | ); |
| 208 | |
| 209 | // Check if all required keys exist in the context. |
| 210 | const contextKeys = new Set(context.get_keys()); |
| 211 | const missingKeys = Array.from(requiredKeys).filter( |
| 212 | (key) => !contextKeys.has(key), |
| 213 | ); |
| 214 | |
| 215 | if (missingKeys.length > 0) { |
| 216 | throw new Error( |
| 217 | `Missing context values for the following keys: ${missingKeys.join( |
| 218 | ', ', |
| 219 | )}`, |
| 220 | ); |
| 221 | } |
| 222 | |
| 223 | // Perform the replacement using a replacer function. |
| 224 | return template.replace(placeholderRegex, (_match, key) => |
| 225 | String(context.get(key)), |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Represents the scope and execution environment for a subagent. |
no test coverage detected