{"version":3,"file":"context.cjs","names":[],"sources":["../../src/utils/context.ts"],"sourcesContent":["/**\n * A tagged template function for creating formatted strings.\n *\n * This utility provides a clean, template literal-based API for string formatting\n * that can be used for prompts, descriptions, and other text formatting needs.\n *\n * It automatically handles whitespace normalization and indentation, making it\n * ideal for multi-line strings in code.\n *\n * When using this utility, it will:\n * - Strip common leading indentation from all lines\n * - Trim leading/trailing whitespace\n * - Align multi-line interpolated values to match indentation\n * - Support escape sequences: `\\\\n` (newline), `\\\\`` (backtick), `\\\\$` (dollar), `\\\\{` (brace)\n *\n * @example\n * ```typescript\n * import { context } from \"@langchain/core/utils/context\";\n *\n * const role = \"agent\";\n * const prompt = context`\n *   You are an ${role}.\n *   Your task is to help users.\n * `;\n * // Returns: \"You are an agent.\\nYour task is to help users.\"\n * ```\n *\n * @example\n * ```typescript\n * // Multi-line interpolated values are aligned\n * const items = \"- Item 1\\n- Item 2\\n- Item 3\";\n * const message = context`\n *   Shopping list:\n *     ${items}\n *   End of list.\n * `;\n * // The items will be indented to match \"    \" (4 spaces)\n * ```\n */\nexport function context(\n  strings: TemplateStringsArray,\n  ...values: unknown[]\n): string {\n  const raw = strings.raw;\n  let result = \"\";\n\n  for (let i = 0; i < raw.length; i++) {\n    // Handle escaped characters in template literals\n    const next = raw[i]\n      .replace(/\\\\\\n[ \\t]*/g, \"\") // escaped newlines (line continuation)\n      .replace(/\\\\`/g, \"`\") // escaped backticks\n      .replace(/\\\\\\$/g, \"$\") // escaped dollar signs\n      .replace(/\\\\\\{/g, \"{\"); // escaped braces\n\n    result += next;\n\n    if (i < values.length) {\n      const value = alignValue(values[i], result);\n      result += typeof value === \"string\" ? value : JSON.stringify(value);\n    }\n  }\n\n  // Strip common indentation\n  result = stripIndent(result);\n\n  // Trim leading/trailing whitespace\n  result = result.trim();\n\n  // Handle escaped \\n at the end (preserve intentional newlines)\n  result = result.replace(/\\\\n/g, \"\\n\");\n\n  return result;\n}\n\n/**\n * Adjusts the indentation of a multi-line interpolated value to match the current line.\n *\n * @param value - The interpolated value\n * @param precedingText - The text that comes before this value\n * @returns The value with adjusted indentation\n */\nfunction alignValue(value: unknown, precedingText: string): unknown {\n  if (typeof value !== \"string\" || !value.includes(\"\\n\")) {\n    return value;\n  }\n\n  const currentLine = precedingText.slice(precedingText.lastIndexOf(\"\\n\") + 1);\n  const indentMatch = currentLine.match(/^(\\s+)/);\n\n  if (indentMatch) {\n    const indent = indentMatch[1];\n    return value.replace(/\\n/g, `\\n${indent}`);\n  }\n\n  return value;\n}\n\n/**\n * Strips common leading indentation from all lines.\n *\n * @param text - The text to process\n * @returns The text with common indentation removed\n */\nfunction stripIndent(text: string): string {\n  const lines = text.split(\"\\n\");\n\n  // Find minimum indentation (only from lines that have content)\n  let minIndent: number | null = null;\n  for (const line of lines) {\n    const match = line.match(/^(\\s+)\\S+/);\n    if (match) {\n      const indent = match[1].length;\n      if (minIndent === null) {\n        minIndent = indent;\n      } else {\n        minIndent = Math.min(minIndent, indent);\n      }\n    }\n  }\n\n  if (minIndent === null) {\n    return text;\n  }\n\n  // Remove the common indentation from all lines\n  return lines\n    .map((line) =>\n      line[0] === \" \" || line[0] === \"\\t\" ? line.slice(minIndent) : line\n    )\n    .join(\"\\n\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,SAAgB,QACd,SACA,GAAG,QACK;CACR,MAAM,MAAM,QAAQ;CACpB,IAAI,SAAS;AAEb,MAAK,IAAI,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;EAEnC,MAAM,OAAO,IAAI,GACd,QAAQ,eAAe,GAAG,CAC1B,QAAQ,QAAQ,IAAI,CACpB,QAAQ,SAAS,IAAI,CACrB,QAAQ,SAAS,IAAI;AAExB,YAAU;AAEV,MAAI,IAAI,OAAO,QAAQ;GACrB,MAAM,QAAQ,WAAW,OAAO,IAAI,OAAO;AAC3C,aAAU,OAAO,UAAU,WAAW,QAAQ,KAAK,UAAU,MAAM;;;AA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