tree: nested children for initial load

This commit is contained in:
p 2026-08-12 18:39:46 +08:00
parent af7d7fc716
commit 3349bb1b33

View File

@ -1,4 +1,4 @@
# workspace_tree.dspy - 懒加载:返回指定节点的直接子目录 # workspace_tree.dspy - 初始加载返回嵌套结构(根节点+children),展开时返回平级子节点
import os import os
@ -30,25 +30,46 @@ async with DBPools().sqlorContext(dbname) as sor:
if not ws_dir or not os.path.isdir(ws_dir): if not ws_dir or not os.path.isdir(ws_dir):
if not node_id: if not node_id:
return [{"id": "__root__", "parentid": "", "label": "📁 工作空间(不可用)", "path": ws_dir, "is_leaf": False}] return [{
"id": "__root__",
"parentid": "",
"label": "📁 工作空间(不可用)",
"path": ws_dir,
"is_leaf": True
}]
return [] return []
# 初始加载:无 id,返回根节点 # 初始加载:返回带 children 的嵌套结构
if not node_id: if not node_id:
return [{ root = {
"id": "__root__", "id": "__root__",
"parentid": "", "parentid": "",
"label": "📁 " + os.path.basename(ws_dir), "label": "📁 " + os.path.basename(ws_dir),
"path": ws_dir, "path": ws_dir,
"is_leaf": False "is_leaf": False,
}] "children": []
}
# 展开节点:id=__root__ → 扫描 ws_dir。否则拼完整路径 try:
if node_id == '__root__': entries = sorted(os.listdir(ws_dir))
scan_path = ws_dir except Exception:
else: entries = []
scan_path = ws_dir + '/' + node_id for name in entries:
full = os.path.join(ws_dir, name)
if name.startswith('.') or name == '__pycache__':
continue
if os.path.isdir(full):
root["children"].append({
"id": name,
"parentid": "__root__",
"label": "📁 " + name,
"path": full,
"is_leaf": False,
"children": []
})
return [root]
# 展开节点
scan_path = ws_dir + '/' + node_id
if not os.path.isdir(scan_path): if not os.path.isdir(scan_path):
return [] return []
@ -63,9 +84,8 @@ for name in entries:
if name.startswith('.') or name == '__pycache__': if name.startswith('.') or name == '__pycache__':
continue continue
if os.path.isdir(full): if os.path.isdir(full):
rel_path = node_id + '/' + name if node_id != '__root__' else name
items.append({ items.append({
"id": rel_path, "id": node_id + '/' + name,
"parentid": node_id, "parentid": node_id,
"label": "📁 " + name, "label": "📁 " + name,
"path": full, "path": full,