diff --git a/wwwroot/api/agent_menu_open.dspy b/wwwroot/api/agent_menu_open.dspy index 47a3adf..6126ddf 100644 --- a/wwwroot/api/agent_menu_open.dspy +++ b/wwwroot/api/agent_menu_open.dspy @@ -2,6 +2,9 @@ # 供 AgentIO 上方功能菜单按钮用(按钮 actiontype=urlwidget 打开本 dspy,通过参数控制弹窗大小) # 参数:url(必填) title width height(百分比字符串,默认 85%/80%) # 注意:PopupWindow 用 width/height 百分比(cwidth/cheight 是字符单位,对列表页太小) +# +# 「需要项目」校验在【点击时】做(本请求实时查会话项目),不在菜单渲染时固化—— +# 渲染时固化是老 bug:页面加载时还没切项目 → 按钮写死报错脚本,之后切了项目仍报错。 import json @@ -9,6 +12,9 @@ url = (params_kw or {}).get('url', '') or '' title = (params_kw or {}).get('title', '') or '功能' width = (params_kw or {}).get('width', '') or '85%' height = (params_kw or {}).get('height', '') or '80%' +require_project = str((params_kw or {}).get('require_project', '') or '') in ('1', 'true', 'True') +session_id = (params_kw or {}).get('session_id', '') or '' +pipeline_id = (params_kw or {}).get('pipeline_id', '') or '' if not url: return json.dumps({ @@ -16,6 +22,35 @@ if not url: "options": {"text": "缺少 url 参数", "cfontsize": 1, "color": "#64748b", "padding": "20px"} }, ensure_ascii=False) +# 需要项目的入口:点击时实时解析会话项目(与菜单解析同一函数,语义不漂移) +if require_project: + _has_project = False + try: + uid = await get_user() + if not uid: + uid = 'user-01' + from pipeline_service.workspace import get_session_project_id + dbname = get_module_dbname('pipeline_core') + async with DBPools().sqlorContext(dbname) as sor: + _pid = await get_session_project_id(sor, uid, session_id) or '' + if _pid: + # 产线隔离:跨产线项目不算本产线的项目 + precs = await sor.sqlExe( + "SELECT pipeline_id FROM sd_projects WHERE id=${p}$", {"p": _pid}) + proj_pl = getattr(precs[0], 'pipeline_id', '') or '' if precs else '' + _has_project = (not pipeline_id) or (proj_pl == pipeline_id) + except Exception: + _has_project = True # 解析异常保守放行(不因校验故障误伤功能) + if not _has_project: + return json.dumps({ + "widgettype": "PopupWindow", + "options": {"title": "提示", "cwidth": 30, "cheight": 8, "auto_open": True}, + "subwidgets": [{ + "widgettype": "Text", + "options": {"text": "请先在会话中切换项目", "cfontsize": 1, "color": "#64748b", "padding": "20px"} + }] + }, ensure_ascii=False) + return json.dumps({ "widgettype": "PopupWindow", "options": {"title": title, "width": width, "height": height, "auto_open": True}, diff --git a/wwwroot/api/agent_menus.dspy b/wwwroot/api/agent_menus.dspy index f170453..e0fdf09 100644 --- a/wwwroot/api/agent_menus.dspy +++ b/wwwroot/api/agent_menus.dspy @@ -58,25 +58,14 @@ except Exception: # 注意:这里不预先 urlencode 查询参数。前端 bricks_fetch 的 url_parse 会把 url 里的 # 查询参数提取后再 encodeURIComponent 编码一次,若此处已 urlencode 会导致双重编码 # (title 的 emoji/中文会变成 %F0%9F... 明文显示)。 +# +# 「需要项目」判断在【点击时】由 agent_menu_open 服务端做(传 require_project/session_id/pipeline_id): +# 渲染时固化判断是老 bug——页面加载时还没切项目 → 按钮写死报错脚本,之后切了项目仍报错。 buttons = [] -# 无当前项目时,点击「需要项目」的按钮弹报错(不打开页面) -_no_proj_script = "var m=new bricks.Message({title:'提示',message:'请先在会话中切换项目'});m.open();" - -def _bind(url, require_project): - if require_project and not pid: - return { - "wid": "self", "event": "click", "actiontype": "script", "target": "self", - "script": _no_proj_script, - } - return { - "wid": "self", "event": "click", "actiontype": "urlwidget", - "target": "self", "options": {"url": url}, - } - # 固定「工作空间」按钮(通用能力,所有 agent 都有;依赖当前项目) -# 带产线参数:入口产线与会话项目跨产线时视为无项目(产线隔离) +# 带产线参数:入口产线与会话项目跨产线时视为无项目(产线隔离,弹窗端点自身在点击时判断) _ws_url = entire_url("/pipeline-sdlc/api/workspace_popup.dspy") _ws_q = [] if session_id: @@ -88,7 +77,10 @@ if _ws_q: buttons.append({ "widgettype": "Button", "options": {"label": "🗂 工作空间", "css": "small", "id": "menu_btn_ws"}, - "binds": [_bind(_ws_url, True)], + "binds": [{ + "wid": "self", "event": "click", "actiontype": "urlwidget", + "target": "self", "options": {"url": _ws_url}, + }], }) for i, m in enumerate(menus): @@ -99,11 +91,21 @@ for i, m in enumerate(menus): require_project = bool(m.get("require_project", False)) width = str(m.get("width", "85%") or "85%") height = str(m.get("height", "80%") or "80%") - open_url = entire_url("/pipeline_core/api/agent_menu_open.dspy") + "?url=" + url + "&title=" + label + "&width=" + width + "&height=" + height + open_url = (entire_url("/pipeline_core/api/agent_menu_open.dspy") + + "?url=" + url + "&title=" + label + + "&width=" + width + "&height=" + height + + ("&require_project=1" if require_project else "")) + if session_id: + open_url += "&session_id=" + session_id + if pipeline_id: + open_url += "&pipeline_id=" + pipeline_id buttons.append({ "widgettype": "Button", "options": {"label": label, "css": "small", "id": "menu_btn_%d" % i}, - "binds": [_bind(open_url, require_project)], + "binds": [{ + "wid": "self", "event": "click", "actiontype": "urlwidget", + "target": "self", "options": {"url": open_url}, + }], }) return _json.dumps({