File size: 1,443 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 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { AgentInputs } from './types.js';
/**
* Replaces `${...}` placeholders in a template string with values from AgentInputs.
*
* @param template The template string containing placeholders.
* @param inputs The AgentInputs object providing placeholder values.
* @returns The populated string with all placeholders replaced.
* @throws {Error} if any placeholder key is not found in the inputs.
*/
export function templateString(template: string, inputs: AgentInputs): string {
const placeholderRegex = /\$\{(\w+)\}/g;
// First, find all unique keys required by the template.
const requiredKeys = new Set(
Array.from(template.matchAll(placeholderRegex), (match) => match[1]),
);
// Check if all required keys exist in the inputs.
const inputKeys = new Set(Object.keys(inputs));
const missingKeys = Array.from(requiredKeys).filter(
(key) => !inputKeys.has(key),
);
if (missingKeys.length > 0) {
// Enhanced error message showing both missing and available keys
throw new Error(
`Template validation failed: Missing required input parameters: ${missingKeys.join(', ')}. ` +
`Available inputs: ${Object.keys(inputs).join(', ')}`,
);
}
// Perform the replacement using a replacer function.
return template.replace(placeholderRegex, (_match, key) =>
String(inputs[key]),
);
}
|