1313from typing import Any , Dict , List , Tuple
1414
1515from codewiki .mcp .session import SessionStore
16- from codewiki .src .config import FIRST_MODULE_TREE_FILENAME , MODULE_TREE_FILENAME
16+ from codewiki .src .be .doc_layout import docs_layout , module_doc_relpath
17+ from codewiki .src .config import DEFAULT_LAYOUT , FIRST_MODULE_TREE_FILENAME , MODULE_TREE_FILENAME
1718
1819logger = logging .getLogger (__name__ )
1920
@@ -30,12 +31,15 @@ def _cap(ids: List[str]) -> Tuple[List[str], bool]:
3031
3132
3233def _get_processing_order (
33- module_tree : Dict [str , Any ], parent_path : List [str ] | None = None
34+ module_tree : Dict [str , Any ],
35+ parent_path : List [str ] | None = None ,
36+ layout : str = DEFAULT_LAYOUT ,
3437) -> List [Dict [str , Any ]]:
3538 """Compute leaf-first processing order from a module tree.
3639
37- Returns a list of dicts with module path, name, leaf status, and
38- component/children info.
40+ Returns a list of dicts with module path, name, leaf status,
41+ component/children info and ``doc_path`` (the page to write, relative to
42+ the output dir, for the docs ``layout``).
3943 """
4044 if parent_path is None :
4145 parent_path = []
@@ -54,6 +58,7 @@ def _collect(tree: Dict[str, Any], path: List[str]) -> None:
5458 "module" : module_name ,
5559 "path" : current_path ,
5660 "is_leaf" : False ,
61+ "doc_path" : module_doc_relpath (current_path , layout ),
5762 "children" : list (children .keys ()),
5863 "components" : module_info .get ("components" , []),
5964 }
@@ -64,6 +69,7 @@ def _collect(tree: Dict[str, Any], path: List[str]) -> None:
6469 "module" : module_name ,
6570 "path" : current_path ,
6671 "is_leaf" : True ,
72+ "doc_path" : module_doc_relpath (current_path , layout ),
6773 "components" : module_info .get ("components" , []),
6874 }
6975 )
@@ -189,7 +195,7 @@ def handle_save_module_tree(
189195 logger .info ("save_module_tree for session %s: %s" , session_id , note )
190196
191197 # Compute processing order and write to workspace file
192- order = _get_processing_order (module_tree )
198+ order = _get_processing_order (module_tree , layout = docs_layout ( session . output_dir ) )
193199 order_file = None
194200 if session .workspace is not None :
195201 order_path = session .workspace .write_json ("processing_order.json" , order )
@@ -206,7 +212,9 @@ def handle_save_module_tree(
206212 "Read the processing_order.json file for the leaf-first generation order. "
207213 "Process leaf modules first (is_leaf=true), then parent modules. "
208214 "For each leaf module: get_prompt('system_leaf') + read_code_components + write_doc_file. "
209- "For each parent module: get_prompt('overview_module') + write_doc_file."
215+ "For each parent module: get_prompt('overview_module') + write_doc_file. "
216+ "Save each module's page at its doc_path (sub-folders are created automatically) "
217+ "and link pages with paths relative to the linking page."
210218 ),
211219 }
212220 if warning :
@@ -237,7 +245,7 @@ def handle_get_processing_order(
237245 else :
238246 return json .dumps ({"error" : "Module tree not found. Call save_module_tree first." })
239247
240- order = _get_processing_order (module_tree )
248+ order = _get_processing_order (module_tree , layout = docs_layout ( session . output_dir ) )
241249
242250 # Write to workspace file
243251 order_file = None
0 commit comments