MCPcopy Create free account
hub / github.com/anus-dev/ANUS / templateString

Function templateString

packages/core/src/core/subagent.ts:201–227  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

199 * @throws {Error} if any placeholder key is not found in the context.
200 */
201function 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.

Callers 1

buildChatSystemPromptMethod · 0.85

Calls 2

get_keysMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected