pipeline-sdlc/wwwroot/api/workspace_tree.dspy

96 lines
2.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# workspace_tree.dspy - 初始加载返回嵌套结构(根节点+children展开时返回平级子节点
import os
node_id = (params_kw or {}).get('id', '').strip()
uid = await get_user()
if not uid:
uid = 'user-01'
dbname = get_module_dbname('pipeline-sdlc')
workspace_base = '/d/pipeline/workspaces'
async with DBPools().sqlorContext(dbname) as sor:
recs = await sor.sqlExe(
"SELECT current_project_id FROM pipeline_agent_settings WHERE user_id=${u}$",
{"u": uid})
pid = getattr(recs[0], 'current_project_id', '') if recs else ''
ws_dir = ''
if pid:
proj = await sor.sqlExe(
"SELECT name, org_id, workspace_dir FROM sd_projects WHERE id=${p}$",
{"p": pid})
if proj:
pname = getattr(proj[0], 'name', '')
org_id = getattr(proj[0], 'org_id', '0') or '0'
ws = getattr(proj[0], 'workspace_dir', '') or ''
ws_dir = ws if ws.startswith('/') else workspace_base + '/' + org_id + '/' + pname
if not ws_dir or not os.path.isdir(ws_dir):
if not node_id:
return [{
"id": "__root__",
"parentid": "",
"label": "📁 工作空间(不可用)",
"path": ws_dir,
"is_leaf": True
}]
return []
# 初始加载:返回带 children 的嵌套结构
if not node_id:
root = {
"id": "__root__",
"parentid": "",
"label": "📁 " + os.path.basename(ws_dir),
"path": ws_dir,
"is_leaf": False,
"children": []
}
try:
entries = sorted(os.listdir(ws_dir))
except Exception:
entries = []
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):
return []
items = []
try:
entries = sorted(os.listdir(scan_path))
except Exception:
return []
for name in entries:
full = os.path.join(scan_path, name)
if name.startswith('.') or name == '__pycache__':
continue
if os.path.isdir(full):
items.append({
"id": node_id + '/' + name,
"parentid": node_id,
"label": "📁 " + name,
"path": full,
"is_leaf": False
})
return items