From ec5a32f9070b9cf16724bed8d692f4fbb448eec6 Mon Sep 17 00:00:00 2001 From: jingrow Date: Mon, 16 Mar 2026 22:16:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0execute=5Flocal=5Fai=5Fagent?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/jingrow/jingrow/ai/utils/jlocal.py | 60 +++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/apps/jingrow/jingrow/ai/utils/jlocal.py b/apps/jingrow/jingrow/ai/utils/jlocal.py index 070fefb..296719c 100644 --- a/apps/jingrow/jingrow/ai/utils/jlocal.py +++ b/apps/jingrow/jingrow/ai/utils/jlocal.py @@ -17,8 +17,10 @@ import requests from fastapi import HTTPException import jingrow +from jingrow.ai.pagetype.local_ai_agent.local_ai_agent import create_agent_job from jingrow.config import Config from jingrow.utils.auth import get_request_session_cookie +from jingrow.utils.jingrow_api import get_agent_detail from jingrow.utils.path import get_jingrow_root logger = logging.getLogger(__name__) @@ -320,3 +322,61 @@ def package_node(node_type: str) -> Dict[str, Any]: jingrow.log_error(f"打包节点失败: {node_type}", str(e)) jingrow.throw("打包失败", str(e)) + +@jingrow.whitelist() +def execute_local_ai_agent(name: str) -> Dict[str, Any]: + """ + Execute local AI agent by name (jlocal version) + Creates a Dramatiq task queue job for async execution + + Args: + name: Agent name (the 'name' field of Local Ai Agent record) + + Returns: + {'success': True, 'message': ..., 'job_id': ...} or {'success': False, 'error': ...} + """ + if not name: + return { + 'success': False, + 'error': 'Agent name is required' + } + + try: + # Get agent detail from SaaS + agent_detail = get_agent_detail(name) + if not agent_detail: + return { + 'success': False, + 'error': f'Agent not found: {name}' + } + + agent_id = agent_detail.get('name') or name + agent_name = agent_detail.get('agent_name') or name + + # Get session cookie from request context + session_cookie = get_request_session_cookie() + + # Create job and enqueue to Dramatiq + job_id = create_agent_job( + agent_id=agent_id, + agent_name=agent_name, + session_cookie=session_cookie, + route='jingrow.ai.utils.jlocal.execute_local_ai_agent' + ) + + logger.info(f"Agent {name} queued for execution, job_id: {job_id}") + + return { + 'success': True, + 'message': 'Agent execution started successfully', + 'job_id': job_id, + 'agent_id': agent_id, + 'agent_name': agent_name + } + + except Exception as e: + logger.error(f"Failed to execute agent {name}: {e}", exc_info=True) + return { + 'success': False, + 'error': str(e) + }