from fastapi import APIRouter, HTTPException, Request
from typing import Dict, Any
from jingrow.ai.pagetype.local_ai_agent.executor import NodeExecutor

router = APIRouter()

@router.post("/jingrow/nodes/{node_type}/execute")
async def execute_node(node_type: str, request: Request, request_data: Dict[str, Any]):
    """
    统一节点执行入口
    """
    try:
        context = request_data.get('context', {})
        inputs = request_data.get('inputs', {})
        config = request_data.get('config', {})
        
        # 获取session cookie
        session_cookie = request.cookies.get('sid')
        
        # 从context中获取flow_id
        flow_id = context.get('flow_id', '')
        
        # 使用节点执行器执行节点（支持点分隔 node_type）
        executor = NodeExecutor()
        result = await executor.execute_node(node_type, flow_id, context, inputs, config, session_cookie)
        
        return result
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
