增加execute_local_ai_agent函数
This commit is contained in:
parent
0c24e60d04
commit
ec5a32f907
@ -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)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user