Resolve a prompt template with optional variable substitution.
(prompt_type: str, variables: Dict[str, Any])
| 104 | "prompt_type": prompt_type, |
| 105 | "description": catalog_entry["description"], |
| 106 | "usage_hint": catalog_entry["usage_hint"], |
| 107 | "content": content, |
| 108 | } |
| 109 | return json.dumps(result, indent=2, ensure_ascii=False) |
| 110 | |
| 111 | |
| 112 | def _page_location(variables: Dict[str, Any], module_name: str) -> tuple[list[str], str]: |
| 113 | """``module_path`` and docs ``layout`` from the variables (top-level module by default).""" |
| 114 | module_path = variables.get("module_path") or [module_name] |
| 115 | if isinstance(module_path, str): |
| 116 | module_path = [p for p in module_path.split("/") if p] |
| 117 | return list(module_path), variables.get("layout") or DEFAULT_LAYOUT |
| 118 | |
| 119 | |
| 120 | def _resolve_prompt(prompt_type: str, variables: Dict[str, Any]) -> str: |
| 121 | """Resolve a prompt template with optional variable substitution.""" |
| 122 | |
| 123 | if prompt_type == "cluster": |
| 124 | potential_core_components = variables.get( |
| 125 | "potential_core_components", "<POTENTIAL_CORE_COMPONENTS placeholder>" |
| 126 | ) |
| 127 | module_tree = variables.get("module_tree", {}) |
| 128 | module_name = variables.get("module_name", None) |
| 129 | return format_cluster_prompt( |
| 130 | potential_core_components=potential_core_components, |
| 131 | module_tree=module_tree, |
| 132 | module_name=module_name, |
| 133 | ) |
| 134 | |
| 135 | elif prompt_type == "system_complex": |
| 136 | module_name = variables.get("module_name", "MODULE_NAME") |
| 137 | custom_instructions = variables.get("custom_instructions", None) |
| 138 | module_path, layout = _page_location(variables, module_name) |
| 139 | return format_system_prompt(module_name, custom_instructions, module_path, layout) |
| 140 | |
| 141 | elif prompt_type == "system_leaf": |
| 142 | module_name = variables.get("module_name", "MODULE_NAME") |
| 143 | custom_instructions = variables.get("custom_instructions", None) |
| 144 | module_path, layout = _page_location(variables, module_name) |
| 145 | return format_leaf_system_prompt(module_name, custom_instructions, module_path, layout) |
| 146 | |
| 147 | elif prompt_type == "user": |
| 148 | module_name = variables.get("module_name", "MODULE_NAME") |
| 149 | module_tree = variables.get("module_tree", {}) |
| 150 | |
| 151 | module_path, layout = _page_location(variables, module_name) |
| 152 | # Return the template with placeholders filled as possible |
| 153 | return ( |
| 154 | USER_PROMPT.format( |
| 155 | module_name=module_name, |
| 156 | module_tree=json.dumps(module_tree, indent=2) |
| 157 | if module_tree |
| 158 | else "<MODULE_TREE placeholder>", |
| 159 | formatted_core_component_codes=variables.get( |
| 160 | "formatted_core_component_codes", |
| 161 | "<CORE_COMPONENT_CODES placeholder — use read_code_components to get source code>", |
| 162 | ), |
no test coverage detected