mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-03 08:12:39 +00:00
Fix lint errors (B023, W293, B007, PERF102)
Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -957,15 +957,24 @@ class Crew(FlowTrackable, BaseModel):
|
||||
)
|
||||
|
||||
# Wrap task execution to capture tokens immediately after completion
|
||||
async def _wrapped_task_execution():
|
||||
result = await task.aexecute_sync(
|
||||
agent=exec_data.agent,
|
||||
context=context,
|
||||
tools=exec_data.tools,
|
||||
# Use default arguments to bind loop variables at definition time (fixes B023)
|
||||
agent = exec_data.agent
|
||||
tools = exec_data.tools
|
||||
|
||||
async def _wrapped_task_execution(
|
||||
_task=task,
|
||||
_agent=agent,
|
||||
_tools=tools,
|
||||
_context=context,
|
||||
):
|
||||
result = await _task.aexecute_sync(
|
||||
agent=_agent,
|
||||
context=_context,
|
||||
tools=_tools,
|
||||
)
|
||||
# Capture tokens immediately after task completes
|
||||
# This reduces (but doesn't eliminate) race conditions
|
||||
tokens_after = self._get_agent_token_usage(exec_data.agent)
|
||||
tokens_after = self._get_agent_token_usage(_agent)
|
||||
return result, tokens_after
|
||||
|
||||
async_task = asyncio.create_task(_wrapped_task_execution())
|
||||
@@ -1730,7 +1739,7 @@ class Crew(FlowTrackable, BaseModel):
|
||||
if workflow_metrics.per_task:
|
||||
# Sum up tokens for each agent from their tasks
|
||||
# We need to find which agent_id corresponds to each task's agent_name
|
||||
for task_name, task_metrics in workflow_metrics.per_task.items():
|
||||
for task_metrics in workflow_metrics.per_task.values():
|
||||
agent_name = task_metrics.agent_name
|
||||
# Find the agent_id for this agent_name from agent_info_map
|
||||
# For now, we'll use the agent_name as a temporary key but this needs improvement
|
||||
@@ -2126,7 +2135,7 @@ To enable tracing, do any one of these:
|
||||
|
||||
if isinstance(agent.llm, BaseLLM):
|
||||
return agent.llm.get_token_usage_summary()
|
||||
elif hasattr(agent, "_token_process"):
|
||||
if hasattr(agent, "_token_process"):
|
||||
return agent._token_process.get_summary()
|
||||
|
||||
return UsageMetrics()
|
||||
|
||||
Reference in New Issue
Block a user