Format the user prompt with module name and organized core component codes. Args: module_name: Name of the module to document core_component_ids: List of component IDs to include components: Dictionary mapping component IDs to CodeComponent objects Retu
(module_name: str, core_component_ids: list[str], components: Dict[str, Any], module_tree: dict[str, any])
| 249 | |
| 250 | |
| 251 | def format_user_prompt(module_name: str, core_component_ids: list[str], components: Dict[str, Any], module_tree: dict[str, any]) -> str: |
| 252 | """ |
| 253 | Format the user prompt with module name and organized core component codes. |
| 254 | |
| 255 | Args: |
| 256 | module_name: Name of the module to document |
| 257 | core_component_ids: List of component IDs to include |
| 258 | components: Dictionary mapping component IDs to CodeComponent objects |
| 259 | |
| 260 | Returns: |
| 261 | Formatted user prompt string |
| 262 | """ |
| 263 | |
| 264 | # format module tree |
| 265 | lines = [] |
| 266 | |
| 267 | def _format_module_tree(module_tree: dict[str, any], indent: int = 0): |
| 268 | for key, value in module_tree.items(): |
| 269 | if key == module_name: |
| 270 | lines.append(f"{' ' * indent}{key} (current module)") |
| 271 | else: |
| 272 | lines.append(f"{' ' * indent}{key}") |
| 273 | |
| 274 | # Group components by file |
| 275 | from collections import defaultdict |
| 276 | by_file = defaultdict(list) |
| 277 | for c in value['components']: |
| 278 | if "::" in c: |
| 279 | fpath, name = c.split("::", 1) |
| 280 | by_file[fpath].append(name) |
| 281 | else: |
| 282 | by_file[""].append(c) |
| 283 | for fpath, names in by_file.items(): |
| 284 | if fpath: |
| 285 | lines.append(f"{' ' * (indent + 1)} {fpath}: {', '.join(names)}") |
| 286 | else: |
| 287 | lines.append(f"{' ' * (indent + 1)} {', '.join(names)}") |
| 288 | |
| 289 | if isinstance(value["children"], dict) and len(value["children"]) > 0: |
| 290 | lines.append(f"{' ' * (indent + 1)} Children:") |
| 291 | _format_module_tree(value["children"], indent + 2) |
| 292 | |
| 293 | _format_module_tree(module_tree, 0) |
| 294 | formatted_module_tree = "\n".join(lines) |
| 295 | |
| 296 | # print(f"Formatted module tree:\n{formatted_module_tree}") |
| 297 | |
| 298 | # Group core component IDs by their file path |
| 299 | grouped_components: dict[str, list[str]] = {} |
| 300 | for component_id in core_component_ids: |
| 301 | if component_id not in components: |
| 302 | continue |
| 303 | component = components[component_id] |
| 304 | path = component.relative_path |
| 305 | if path not in grouped_components: |
| 306 | grouped_components[path] = [] |
| 307 | grouped_components[path].append(component_id) |
| 308 |
no test coverage detected