pipeline_core/wwwroot/api/skill_pack.dspy
yumoqing b9998ed00d refactor(skill): 单一技能树——七级 scope 全在全局 skills/ 下,去掉 ensure_org_skills 复制副本
- skill_loader: 加 project_common scope(projects/ 项目通用,不分 project_id) + project 私有改挂 orgs/{org_id}/{project_id}/,SCOPE_PRIORITY 七级
- skill_pack: ensure_org_skills 去复制逻辑改薄封装返回 get_skills_base(),删 _sync_tree/get_org_skills_dir dead code
- skill_pack.dspy/skill_proposals.dspy: base_dir 改全局 skills/,删同步所有机构循环
2026-08-21 19:18:45 +08:00

81 lines
3.1 KiB
Plaintext
Raw Permalink 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.

# skill_pack.dspy - 技能集选装/安装机构管理员pipeline-core 通用能力)
# action=list: 列出可安装的技能集
# action=installed: 查当前机构已安装的技能集
# action=install: 安装技能集到当前机构org scope
# action=uninstall: 卸载技能集
import json
import os
uid = await get_user()
if not uid:
return json.dumps({"success": False, "error": "请先登录"}, ensure_ascii=False)
action = (params_kw or {}).get('action', 'list')
dbname = get_module_dbname('pipeline_core')
# 查用户 org_id + 角色pipeline 库)
org_id = ''
is_admin = False
try:
async with DBPools().sqlorContext(dbname) as sor:
urecs = await sor.sqlExe("SELECT orgid FROM users WHERE id=${u}$", {"u": uid})
if urecs:
org_id = getattr(urecs[0], 'orgid', '') or ''
rrecs = await sor.sqlExe(
"SELECT r.name FROM userrole ur JOIN role r ON ur.roleid=r.id WHERE ur.userid=${u}$",
{"u": uid})
roles = [getattr(r, 'name', '') for r in (rrecs or [])]
is_admin = ('superuser' in roles) or ('admin' in roles)
except Exception:
pass
if not org_id:
return json.dumps({"success": False, "error": "用户未关联机构"}, ensure_ascii=False)
from pipeline_core.skill_pack import list_packs, install_pack, uninstall_pack, installed_packs, get_skills_base
# 机构技能根目录 = 全局 skills/单一技能树所有机构共享读2026-08-21 重构)
base_dir = get_skills_base()
if action == 'list':
return json.dumps({"success": True, "is_admin": is_admin, "packs": list_packs()},
ensure_ascii=False)
elif action == 'installed':
return json.dumps({"success": True, "packs": installed_packs(base_dir, org_id)},
ensure_ascii=False)
elif action == 'install':
if not is_admin:
return json.dumps({"success": False, "error": "仅机构管理员可安装技能集"}, ensure_ascii=False)
pack_name = (params_kw or {}).get('pack', '').strip()
if not pack_name:
return json.dumps({"success": False, "error": "pack 必填"}, ensure_ascii=False)
r = install_pack(pack_name, base_dir, org_id)
if r.get("success"):
try:
from pipeline_core.skill_loader import get_skill_loader
get_skill_loader().reload()
except Exception:
pass
return json.dumps(r, ensure_ascii=False)
elif action == 'uninstall':
if not is_admin:
return json.dumps({"success": False, "error": "仅机构管理员可卸载技能集"}, ensure_ascii=False)
pack_name = (params_kw or {}).get('pack', '').strip()
if not pack_name:
return json.dumps({"success": False, "error": "pack 必填"}, ensure_ascii=False)
r = uninstall_pack(pack_name, base_dir, org_id)
if r.get("success"):
try:
from pipeline_core.skill_loader import get_skill_loader
get_skill_loader().reload()
except Exception:
pass
return json.dumps(r, ensure_ascii=False)
else:
return json.dumps({"error": "Unknown action: " + str(action)}, ensure_ascii=False)