import asyncio async def _do(): env = request._run_ns userorgid = await env.get_userorgid() cards = [] async with get_sor_context(env, 'rag') as sor: recs = await sor.R("knowledge_bases", {"org_id": userorgid}) for r in recs: doc_recs = await sor.R("documents", {"kb_id": r.id}) doc_count = len(doc_recs) total_size = r.total_size or 0 if total_size > 1073741824: size_str = f"{total_size/1073741824:.1f}GB" elif total_size > 1048576: size_str = f"{total_size/1048576:.0f}MB" else: size_str = f"{total_size/1024:.0f}KB" engine = r.embedding_engine or "CLIP" card = { "widgettype": "VBox", "options": { "cwidth": 16, "cheight": 12, "bgcolor": "#f0f7ff", "padding": "16px", "css": "card clickable", "border": "1px solid #d0e4f7" }, "subwidgets": [ {"widgettype": "Text", "options": {"text": "๐Ÿ“š " + str(r.name), "cfontsize": 16, "fontWeight": "bold"}}, {"widgettype": "Text", "options": {"text": str(engine) + " ยท 1024็ปด", "cfontsize": 12, "color": "#888"}}, {"widgettype": "HBox", "options": {"spacing": "12px"}, "subwidgets": [ {"widgettype": "Text", "options": {"text": "๐Ÿ“„ " + str(doc_count) + "ๆ–‡ๆกฃ", "cfontsize": 12, "color": "#666"}}, {"widgettype": "Text", "options": {"text": "๐Ÿ’พ " + size_str, "cfontsize": 12, "color": "#666"}} ]} ], "binds": [{"wid": "self", "event": "click", "actiontype": "urlwidget", "target": "app.rag_main_content", "mode": "replace", "options": {"url": entire_url('/knowledge_bases_list/detail.ui')}}] } cards.append(card) return { "widgettype": "HBox", "options": {"width": "100%", "spacing": "12px", "wrap": True}, "subwidgets": cards } result = asyncio.get_event_loop().run_until_complete(_do())