# workspace_open.dspy - 预览/打开文件(文本文件显示内容,二进制提示) import os file_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 if not file_id: return {"widgettype": "Message", "options": {"title": "打开失败", "message": "未指定文件"}} full_path = ws_dir + '/' + file_id if ws_dir else file_id if not os.path.isfile(full_path): return {"widgettype": "Message", "options": {"title": "打开失败", "message": "文件不存在: " + file_id}} name = os.path.basename(full_path) ext = os.path.splitext(name)[1].lower() text_exts = ['.md', '.py', '.js', '.html', '.css', '.json', '.txt', '.xml', '.yaml', '.yml', '.toml', '.cfg', '.sh', '.sql', '.dspy', '.ui', '.csv', '.ts', '.tsx', '.jsx', '.java', '.c', '.cpp', '.h', '.go', '.rs', '.rb', '.php', '.vue', '.scss', '.less', '.ini'] if ext not in text_exts: return {"widgettype": "Message", "options": {"title": "无法预览", "message": "二进制文件无法预览: " + name}} try: with open(full_path, 'r', encoding='utf-8', errors='replace') as f: content = f.read() except Exception as e: return {"widgettype": "Message", "options": {"title": "打开失败", "message": str(e)}} if len(content) > 200000: content = content[:200000] + "\n\n...(内容过长,已截断)" return { "widgettype": "PopupWindow", "options": {"title": name, "width": "80%", "height": "80%", "auto_open": True}, "subwidgets": [{ "widgettype": "VScrollPanel", "options": {"css": "filler", "padding": "12px"}, "subwidgets": [ {"widgettype": "Text", "options": {"text": content, "cfontsize": 0.85, "wrap": True}} ] }] }