fix: wrap dspy async in asyncio.run_until_complete

This commit is contained in:
yumoqing 2026-07-22 18:42:24 +08:00
parent c48aa36f1b
commit 95c71a9b88
2 changed files with 81 additions and 84 deletions

View File

@ -1,47 +1,46 @@
env = request._run_ns
userorgid = await env.get_userorgid()
import asyncio
async with get_sor_context(env, 'rag') as sor:
recs = await sor.R("knowledge_bases", {"org_id": userorgid})
cards = []
for r in recs:
kb_id = r.id
doc_recs = await sor.R("documents", {"kb_id": kb_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')}}]
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
}
cards.append(card)
result = {
"widgettype": "HBox",
"options": {"width": "100%", "spacing": "12px", "wrap": True},
"subwidgets": cards
}
result = asyncio.get_event_loop().run_until_complete(_do())

View File

@ -1,44 +1,42 @@
action = params_kw.get("action", "")
kb_id = params_kw.get("kb_id", "")
env = request._run_ns
import asyncio
if action == "create_dir":
dir_name = params_kw.get("dir_name", "")
parent_id = params_kw.get("parent_id", "")
if dir_name:
async with get_sor_context(env, 'rag') as sor:
dir_id = "dir_" + str(abs(hash(dir_name + parent_id)))[:12]
await sor.sqlExe(
"INSERT INTO document_chunks (id, doc_id, kb_id, chunk_index, chunk_type, content, description, created_at) "
"VALUES (${id}$, '', ${kb_id}$, 0, 'directory', ${name}$, ${parent}$, NOW())",
{"id": dir_id, "kb_id": kb_id, "name": dir_name, "parent": parent_id})
async def _do():
action = params_kw.get("action", "")
kb_id = params_kw.get("kb_id", "")
env = request._run_ns
folder_label = params_kw.get("folder_label", "")
elif action == "delete_item":
item_id = params_kw.get("item_id", "")
if item_id:
async with get_sor_context(env, 'rag') as sor:
await sor.sqlExe("DELETE FROM document_chunks WHERE id=${id}$", {"id": item_id})
if action == "create_dir":
dir_name = params_kw.get("dir_name", "")
parent_id = params_kw.get("parent_id", "")
if dir_name:
async with get_sor_context(env, 'rag') as sor:
dir_id = "dir_" + str(abs(hash(dir_name + parent_id)))[:12]
await sor.sqlExe(
"INSERT INTO document_chunks (id, doc_id, kb_id, chunk_index, chunk_type, content, description, created_at) "
"VALUES (${id}$, '', ${kb_id}$, 0, 'directory', ${name}$, ${parent}$, NOW())",
{"id": dir_id, "kb_id": kb_id, "name": dir_name, "parent": parent_id})
folder_label = folder_label or dir_name
# Return refreshed folder content widget
folder_id = params_kw.get("folder_id", "")
folder_label = params_kw.get("folder_label", "")
folder_label = folder_label or dir_name or "全部文件"
elif action == "delete_item":
item_id = params_kw.get("item_id", "")
if item_id:
async with get_sor_context(env, 'rag') as sor:
await sor.sqlExe("DELETE FROM document_chunks WHERE id=${id}$", {"id": item_id})
result = {
"widgettype": "VBox",
"options": {"spacing": "12px"},
"subwidgets": [
{
"widgettype": "HBox",
"options": {"spacing": "4px"},
"subwidgets": [
{"widgettype": "Text", "options": {"text": "📂 " + str(folder_label), "cfontsize": 16, "fontWeight": "bold", "color": "#333"}},
{"widgettype": "Text", "options": {"text": "操作成功", "cfontsize": 12, "color": "#50b86c"}}
]
},
{
"widgettype": "Text",
"options": {"text": "操作已完成,请刷新目录树", "color": "#888", "cfontsize": 13}
}
]
}
return {
"widgettype": "VBox",
"options": {"spacing": "12px"},
"subwidgets": [
{
"widgettype": "HBox",
"options": {"spacing": "4px"},
"subwidgets": [
{"widgettype": "Text", "options": {"text": "📂 " + str(folder_label), "cfontsize": 16, "fontWeight": "bold", "color": "#333"}},
{"widgettype": "Text", "options": {"text": "操作成功", "cfontsize": 12, "color": "#50b86c"}}
]
}
]
}
result = asyncio.get_event_loop().run_until_complete(_do())