# This structured output is what the evaluation platform will use to compare expected vs actual behavior using the custom metric(s) you define.

def evaluation_task(dataset_item):
    try:
        user_message_content = dataset_item["input"]
        expected_tool = dataset_item["expected_output"]

        # This is where you call your agent with the input message and get the real execution results.
        result = agent_executor.invoke({"messages": [HumanMessage(content=user_message_content)]})

        tool_calls = []

        # Here we extract the tool calls the agent actually made.
        # We loop through the agent's messages, check tool calls,
        # and for each tool call, we capture its metadata.
        for msg in result.get("messages", []):
            if hasattr(msg, "tool_calls") and msg.tool_calls:
                for tool_call in msg.tool_calls:
                    tool_calls.append({
                        "function_name": tool_call.get("name"),
                        "function_parameters": tool_call.get("args", {})
                    })

        return {
            "input": user_message_content,
            "output": result,
            "tool_calls": tool_calls,
            "expected_tool_calls": [{"function_name": expected_tool, "function_parameters": {}}]
        }

    except Exception as e:
        return {
            "input": dataset_item.get("input", {}),
            "output": "Error processing input.",
            "tool_calls": [],
            "expected_tool_calls": [{"function_name": "unknown", "function_parameters": {}}],
            "error": str(e)
        }