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AA,MAAM,KAAA,GAAQ,IAAA,CAAK,KAAA,CAAM,GAAG,CAAA;AAC5B,EAAA,IAAI,OAAA,GAAmB,GAAA;AAEvB,EAAA,KAAA,MAAW,QAAQ,KAAA,EAAO;AACtB,IAAA,IAAI,OAAA,KAAY,IAAA,IAAQ,OAAA,KAAY,MAAA,EAAW;AAC3C,MAAA,OAAO,MAAA;AAAA,IACX;AACA,IAAA,IAAI,OAAO,YAAY,QAAA,EAAU;AAC7B,MAAA,OAAO,MAAA;AAAA,IACX;AACA,IAAA,OAAA,GAAW,QAAoC,IAAI,CAAA;AAAA,EACvD;AAEA,EAAA,OAAO,OAAA;AACX;AAiBO,SAAS,kBAAA,CAAmB,SAAkC,aAAA,EAAkC;AACnG,EAAA,KAAA,MAAW,QAAQ,aAAA,EAAe;AAC9B,IAAA,MAAM,KAAA,GAAQ,cAAA,CAAe,OAAA,EAAS,IAAI,CAAA;AAC1C,IAAA,IAAI,KAAA,KAAU,MAAA,IAAa,KAAA,KAAU,IAAA,EAAM;AACvC,MAAA,OAAO,KAAA;AAAA,IACX;AAAA,EACJ;AACA,EAAA,OAAO,IAAA;AACX;AAkBO,SAAS,+BACZ,eAAA,EACuB;AACvB,EAAA,MAAM,SAAkC,EAAC;AAEzC,EAAA,KAAA,MAAW,QAAQ,eAAA,EAAiB;AAChC,IAAA,IAAI,IAAA,CAAK,OAAA,IAAW,OAAO,IAAA,CAAK,YAAY,QAAA,EAAU;AAElD,MAAA,MAAA,CAAO,MAAA,CAAO,MAAA,EAAQ,IAAA,CAAK,OAAO,CAAA;AAAA,IACtC;AAAA,EACJ;AAEA,EAAA,OAAO,MAAA;AACX","file":"template-interpolation.mjs","sourcesContent":["/**\n * Template Interpolation Utilities\n *\n * Provides utilities for interpolating template strings with context values.\n * Used by the Intent Router to fill in command templates with widget context.\n *\n * @since 0.18.0\n */\n\n/**\n * Interpolates a template string with values from a context object.\n * Supports dot notation for nested paths (e.g., {{context.currentJob.jobId}}).\n *\n * @param template - The template string containing {{path}} placeholders\n * @param context - The context object to pull values from\n * @returns The interpolated string\n *\n * @example\n * ```typescript\n * const template = 'Opening job: {{context.currentJob.name}}';\n * const context = { context: { currentJob: { name: 'My Job' } } };\n * interpolateString(template, context); // 'Opening job: My Job'\n * ```\n */\nexport function interpolateString(template: string, context: Record<string, unknown>): string {\n    return template.replace(/\\{\\{([^}]+)\\}\\}/g, (match, path) => {\n        const value = getValueAtPath(context, path.trim());\n        if (value === undefined || value === null) {\n            return match; // Keep the original placeholder if value not found\n        }\n        return String(value);\n    });\n}\n\n/**\n * Interpolates all string values in an object recursively.\n * Non-string values are passed through unchanged.\n *\n * @param obj - The object containing template strings\n * @param context - The context object to pull values from\n * @returns A new object with all string values interpolated\n *\n * @example\n * ```typescript\n * const command = {\n *   type: 'renderTag',\n *   tagId: 'rcs.job',\n *   data: {\n *     jobId: '{{context.currentJob.jobId}}',\n *     tab: 'overview'\n *   }\n * };\n * const context = { context: { currentJob: { jobId: '123' } } };\n * interpolateObject(command, context);\n * // { type: 'renderTag', tagId: 'rcs.job', data: { jobId: '123', tab: 'overview' } }\n * ```\n */\nexport function interpolateObject<T>(obj: T, context: Record<string, unknown>): T {\n    if (obj === null || obj === undefined) {\n        return obj;\n    }\n\n    if (typeof obj === 'string') {\n        return interpolateString(obj, context) as T;\n    }\n\n    if (Array.isArray(obj)) {\n        return obj.map((item) => interpolateObject(item, context)) as T;\n    }\n\n    if (typeof obj === 'object') {\n        const result: Record<string, unknown> = {};\n        for (const [key, value] of Object.entries(obj)) {\n            result[key] = interpolateObject(value, context);\n        }\n        return result as T;\n    }\n\n    // Primitives (number, boolean) pass through unchanged\n    return obj;\n}\n\n/**\n * Gets a value from an object using dot notation path.\n *\n * @param obj - The object to get the value from\n * @param path - The dot notation path (e.g., \"context.currentJob.jobId\")\n * @returns The value at the path, or undefined if not found\n *\n * @example\n * ```typescript\n * const obj = { context: { currentJob: { jobId: '123' } } };\n * getValueAtPath(obj, 'context.currentJob.jobId'); // '123'\n * getValueAtPath(obj, 'context.missing.path'); // undefined\n * ```\n */\nexport function getValueAtPath(obj: Record<string, unknown>, path: string): unknown {\n    const parts = path.split('.');\n    let current: unknown = obj;\n\n    for (const part of parts) {\n        if (current === null || current === undefined) {\n            return undefined;\n        }\n        if (typeof current !== 'object') {\n            return undefined;\n        }\n        current = (current as Record<string, unknown>)[part];\n    }\n\n    return current;\n}\n\n/**\n * Checks if all required context paths exist in the context object.\n * Used to determine if a command's requiresContext conditions are met.\n *\n * @param context - The context object to check\n * @param requiredPaths - Array of dot notation paths that must exist\n * @returns true if all paths exist and have non-null/undefined values\n *\n * @example\n * ```typescript\n * const context = { currentJob: { jobId: '123', name: 'Test' } };\n * hasRequiredContext(context, ['currentJob.jobId']); // true\n * hasRequiredContext(context, ['currentJob.missing']); // false\n * ```\n */\nexport function hasRequiredContext(context: Record<string, unknown>, requiredPaths: string[]): boolean {\n    for (const path of requiredPaths) {\n        const value = getValueAtPath(context, path);\n        if (value === undefined || value === null) {\n            return false;\n        }\n    }\n    return true;\n}\n\n/**\n * Converts LLM context items to a flat context object for template interpolation.\n * Groups context by source and extracts data.\n *\n * @param llmContextItems - Array of LLM context items from the request\n * @returns A flat context object suitable for template interpolation\n *\n * @example\n * ```typescript\n * const items = [\n *   { id: 'job-context', context: { currentJob: { jobId: '123' } } }\n * ];\n * convertLlmContextToFlatContext(items);\n * // { currentJob: { jobId: '123' } }\n * ```\n */\nexport function convertLlmContextToFlatContext(\n    llmContextItems: Array<{ id: string; context: unknown }>\n): Record<string, unknown> {\n    const result: Record<string, unknown> = {};\n\n    for (const item of llmContextItems) {\n        if (item.context && typeof item.context === 'object') {\n            // Merge the context data into the result\n            Object.assign(result, item.context);\n        }\n    }\n\n    return result;\n}\n"]}