# workspace_files.dspy - 返回目录文件列表(无 id 或 id=__root__ 时返回根目录文件) import os folder_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: ws = getattr(proj[0], 'workspace_dir', '') or '' if ws.startswith('/'): ws_dir = ws else: pname = getattr(proj[0], 'name', '') org_id = getattr(proj[0], 'org_id', '0') or '0' ws_dir = workspace_base + '/' + org_id + '/' + pname # 无 id 或 __root__ 都指向工作空间根目录 if not folder_id or folder_id == '__root__': full_dir = ws_dir else: full_dir = ws_dir + '/' + folder_id if ws_dir else folder_id if not os.path.isdir(full_dir): return {"widgettype": "Text", "options": {"text": "目录不可用: " + folder_id, "cfontsize": 0.9}} files = [] try: entries = sorted(os.listdir(full_dir)) except Exception as e: return {"widgettype": "Text", "options": {"text": "读取错误: " + str(e), "cfontsize": 0.9}} for name in entries: full = os.path.join(full_dir, name) if name.startswith('.'): continue if os.path.isfile(full): size = os.path.getsize(full) ext = os.path.splitext(name)[1].lower() is_text = ext in ['.md','.py','.js','.html','.css','.json','.txt','.xml','.yaml','.yml','.toml','.cfg','.sh','.sql','.dspy','.ui'] icon = "📄" if is_text else "🎬" if ext in ['.png','.jpg','.mp4'] else "📁" row = { "widgettype": "HBox", "options": {"padding": "6px 10px", "alignItems": "center", "gap": "8px"}, "subwidgets": [ {"widgettype": "Text", "options": {"text": icon, "cfontsize": 1.2}}, {"widgettype": "Text", "options": {"text": name, "cfontsize": 0.9, "color": "#1e293b", "css": "filler"}}, {"widgettype": "Text", "options": {"text": f"{size/1024:.1f}KB", "cfontsize": 0.75, "color": "#94a3b8"}}, ], } files.append(row) if not files: return {"widgettype": "Text", "options": {"text": "(空目录)", "cfontsize": 0.85, "color": "#94a3b8", "padding": "8px"}} return { "widgettype": "VScrollPanel", "options": {"css": "filler", "padding": "8px", "gap": "4px"}, "subwidgets": files }