diff --git a/lib/crewai/src/crewai/events/types/a2a_events.py b/lib/crewai/src/crewai/events/types/a2a_events.py index 55de064f8..4131a1fea 100644 --- a/lib/crewai/src/crewai/events/types/a2a_events.py +++ b/lib/crewai/src/crewai/events/types/a2a_events.py @@ -73,7 +73,7 @@ class A2ADelegationStartedEvent(A2AEventBase): extensions: List of A2A extension URIs in use. """ - type: str = "a2a_delegation_started" + type: Literal["a2a_delegation_started"] = "a2a_delegation_started" endpoint: str task_description: str agent_id: str @@ -106,7 +106,7 @@ class A2ADelegationCompletedEvent(A2AEventBase): extensions: List of A2A extension URIs in use. """ - type: str = "a2a_delegation_completed" + type: Literal["a2a_delegation_completed"] = "a2a_delegation_completed" status: str result: str | None = None error: str | None = None @@ -140,7 +140,7 @@ class A2AConversationStartedEvent(A2AEventBase): extensions: List of A2A extension URIs in use. """ - type: str = "a2a_conversation_started" + type: Literal["a2a_conversation_started"] = "a2a_conversation_started" agent_id: str endpoint: str context_id: str | None = None @@ -171,7 +171,7 @@ class A2AMessageSentEvent(A2AEventBase): extensions: List of A2A extension URIs in use. """ - type: str = "a2a_message_sent" + type: Literal["a2a_message_sent"] = "a2a_message_sent" message: str turn_number: int context_id: str | None = None @@ -203,7 +203,7 @@ class A2AResponseReceivedEvent(A2AEventBase): extensions: List of A2A extension URIs in use. """ - type: str = "a2a_response_received" + type: Literal["a2a_response_received"] = "a2a_response_received" response: str turn_number: int context_id: str | None = None @@ -237,7 +237,7 @@ class A2AConversationCompletedEvent(A2AEventBase): extensions: List of A2A extension URIs in use. """ - type: str = "a2a_conversation_completed" + type: Literal["a2a_conversation_completed"] = "a2a_conversation_completed" status: Literal["completed", "failed"] final_result: str | None = None error: str | None = None @@ -263,7 +263,7 @@ class A2APollingStartedEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_polling_started" + type: Literal["a2a_polling_started"] = "a2a_polling_started" task_id: str context_id: str | None = None polling_interval: float @@ -286,7 +286,7 @@ class A2APollingStatusEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_polling_status" + type: Literal["a2a_polling_status"] = "a2a_polling_status" task_id: str context_id: str | None = None state: str @@ -309,7 +309,9 @@ class A2APushNotificationRegisteredEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_push_notification_registered" + type: Literal["a2a_push_notification_registered"] = ( + "a2a_push_notification_registered" + ) task_id: str context_id: str | None = None callback_url: str @@ -334,7 +336,7 @@ class A2APushNotificationReceivedEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_push_notification_received" + type: Literal["a2a_push_notification_received"] = "a2a_push_notification_received" task_id: str context_id: str | None = None state: str @@ -359,7 +361,7 @@ class A2APushNotificationSentEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_push_notification_sent" + type: Literal["a2a_push_notification_sent"] = "a2a_push_notification_sent" task_id: str context_id: str | None = None callback_url: str @@ -381,7 +383,7 @@ class A2APushNotificationTimeoutEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_push_notification_timeout" + type: Literal["a2a_push_notification_timeout"] = "a2a_push_notification_timeout" task_id: str context_id: str | None = None timeout_seconds: float @@ -405,7 +407,7 @@ class A2AStreamingStartedEvent(A2AEventBase): extensions: List of A2A extension URIs in use. """ - type: str = "a2a_streaming_started" + type: Literal["a2a_streaming_started"] = "a2a_streaming_started" task_id: str | None = None context_id: str | None = None endpoint: str @@ -434,7 +436,7 @@ class A2AStreamingChunkEvent(A2AEventBase): extensions: List of A2A extension URIs in use. """ - type: str = "a2a_streaming_chunk" + type: Literal["a2a_streaming_chunk"] = "a2a_streaming_chunk" task_id: str | None = None context_id: str | None = None chunk: str @@ -462,7 +464,7 @@ class A2AAgentCardFetchedEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_agent_card_fetched" + type: Literal["a2a_agent_card_fetched"] = "a2a_agent_card_fetched" endpoint: str a2a_agent_name: str | None = None agent_card: dict[str, Any] | None = None @@ -486,7 +488,7 @@ class A2AAuthenticationFailedEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_authentication_failed" + type: Literal["a2a_authentication_failed"] = "a2a_authentication_failed" endpoint: str auth_type: str | None = None error: str @@ -517,7 +519,7 @@ class A2AArtifactReceivedEvent(A2AEventBase): extensions: List of A2A extension URIs in use. """ - type: str = "a2a_artifact_received" + type: Literal["a2a_artifact_received"] = "a2a_artifact_received" task_id: str artifact_id: str artifact_name: str | None = None @@ -550,7 +552,7 @@ class A2AConnectionErrorEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_connection_error" + type: Literal["a2a_connection_error"] = "a2a_connection_error" endpoint: str error: str error_type: str | None = None @@ -571,7 +573,7 @@ class A2AServerTaskStartedEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_server_task_started" + type: Literal["a2a_server_task_started"] = "a2a_server_task_started" task_id: str context_id: str metadata: dict[str, Any] | None = None @@ -587,7 +589,7 @@ class A2AServerTaskCompletedEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_server_task_completed" + type: Literal["a2a_server_task_completed"] = "a2a_server_task_completed" task_id: str context_id: str result: str @@ -603,7 +605,7 @@ class A2AServerTaskCanceledEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_server_task_canceled" + type: Literal["a2a_server_task_canceled"] = "a2a_server_task_canceled" task_id: str context_id: str metadata: dict[str, Any] | None = None @@ -619,7 +621,7 @@ class A2AServerTaskFailedEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_server_task_failed" + type: Literal["a2a_server_task_failed"] = "a2a_server_task_failed" task_id: str context_id: str error: str @@ -634,7 +636,7 @@ class A2AParallelDelegationStartedEvent(A2AEventBase): task_description: Description of the task being delegated. """ - type: str = "a2a_parallel_delegation_started" + type: Literal["a2a_parallel_delegation_started"] = "a2a_parallel_delegation_started" endpoints: list[str] task_description: str @@ -649,7 +651,9 @@ class A2AParallelDelegationCompletedEvent(A2AEventBase): results: Summary of results from each agent. """ - type: str = "a2a_parallel_delegation_completed" + type: Literal["a2a_parallel_delegation_completed"] = ( + "a2a_parallel_delegation_completed" + ) endpoints: list[str] success_count: int failure_count: int @@ -675,7 +679,7 @@ class A2ATransportNegotiatedEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_transport_negotiated" + type: Literal["a2a_transport_negotiated"] = "a2a_transport_negotiated" endpoint: str a2a_agent_name: str | None = None negotiated_transport: str @@ -708,7 +712,7 @@ class A2AContentTypeNegotiatedEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_content_type_negotiated" + type: Literal["a2a_content_type_negotiated"] = "a2a_content_type_negotiated" endpoint: str a2a_agent_name: str | None = None skill_name: str | None = None @@ -738,7 +742,7 @@ class A2AContextCreatedEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_context_created" + type: Literal["a2a_context_created"] = "a2a_context_created" context_id: str created_at: float metadata: dict[str, Any] | None = None @@ -755,7 +759,7 @@ class A2AContextExpiredEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_context_expired" + type: Literal["a2a_context_expired"] = "a2a_context_expired" context_id: str created_at: float age_seconds: float @@ -775,7 +779,7 @@ class A2AContextIdleEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_context_idle" + type: Literal["a2a_context_idle"] = "a2a_context_idle" context_id: str idle_seconds: float task_count: int @@ -792,7 +796,7 @@ class A2AContextCompletedEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_context_completed" + type: Literal["a2a_context_completed"] = "a2a_context_completed" context_id: str total_tasks: int duration_seconds: float @@ -811,7 +815,7 @@ class A2AContextPrunedEvent(A2AEventBase): metadata: Custom A2A metadata key-value pairs. """ - type: str = "a2a_context_pruned" + type: Literal["a2a_context_pruned"] = "a2a_context_pruned" context_id: str task_count: int age_seconds: float diff --git a/lib/crewai/src/crewai/events/types/agent_events.py b/lib/crewai/src/crewai/events/types/agent_events.py index 49e24e059..8c811d176 100644 --- a/lib/crewai/src/crewai/events/types/agent_events.py +++ b/lib/crewai/src/crewai/events/types/agent_events.py @@ -3,7 +3,7 @@ from __future__ import annotations from collections.abc import Sequence -from typing import Any +from typing import Any, Literal from pydantic import ConfigDict, model_validator from typing_extensions import Self @@ -21,7 +21,7 @@ class AgentExecutionStartedEvent(BaseEvent): task: Any tools: Sequence[BaseTool | CrewStructuredTool] | None task_prompt: str - type: str = "agent_execution_started" + type: Literal["agent_execution_started"] = "agent_execution_started" model_config = ConfigDict(arbitrary_types_allowed=True) @@ -38,7 +38,7 @@ class AgentExecutionCompletedEvent(BaseEvent): agent: BaseAgent task: Any output: str - type: str = "agent_execution_completed" + type: Literal["agent_execution_completed"] = "agent_execution_completed" model_config = ConfigDict(arbitrary_types_allowed=True) @@ -55,7 +55,7 @@ class AgentExecutionErrorEvent(BaseEvent): agent: BaseAgent task: Any error: str - type: str = "agent_execution_error" + type: Literal["agent_execution_error"] = "agent_execution_error" model_config = ConfigDict(arbitrary_types_allowed=True) @@ -73,7 +73,7 @@ class LiteAgentExecutionStartedEvent(BaseEvent): agent_info: dict[str, Any] tools: Sequence[BaseTool | CrewStructuredTool] | None messages: str | list[dict[str, str]] - type: str = "lite_agent_execution_started" + type: Literal["lite_agent_execution_started"] = "lite_agent_execution_started" model_config = ConfigDict(arbitrary_types_allowed=True) @@ -83,7 +83,7 @@ class LiteAgentExecutionCompletedEvent(BaseEvent): agent_info: dict[str, Any] output: str - type: str = "lite_agent_execution_completed" + type: Literal["lite_agent_execution_completed"] = "lite_agent_execution_completed" class LiteAgentExecutionErrorEvent(BaseEvent): @@ -91,7 +91,7 @@ class LiteAgentExecutionErrorEvent(BaseEvent): agent_info: dict[str, Any] error: str - type: str = "lite_agent_execution_error" + type: Literal["lite_agent_execution_error"] = "lite_agent_execution_error" # Agent Eval events @@ -100,7 +100,7 @@ class AgentEvaluationStartedEvent(BaseEvent): agent_role: str task_id: str | None = None iteration: int - type: str = "agent_evaluation_started" + type: Literal["agent_evaluation_started"] = "agent_evaluation_started" class AgentEvaluationCompletedEvent(BaseEvent): @@ -110,7 +110,7 @@ class AgentEvaluationCompletedEvent(BaseEvent): iteration: int metric_category: Any score: Any - type: str = "agent_evaluation_completed" + type: Literal["agent_evaluation_completed"] = "agent_evaluation_completed" class AgentEvaluationFailedEvent(BaseEvent): @@ -119,7 +119,7 @@ class AgentEvaluationFailedEvent(BaseEvent): task_id: str | None = None iteration: int error: str - type: str = "agent_evaluation_failed" + type: Literal["agent_evaluation_failed"] = "agent_evaluation_failed" def _set_agent_fingerprint(event: BaseEvent, agent: BaseAgent) -> None: diff --git a/lib/crewai/src/crewai/events/types/crew_events.py b/lib/crewai/src/crewai/events/types/crew_events.py index fa198f5ae..cf71cbfe3 100644 --- a/lib/crewai/src/crewai/events/types/crew_events.py +++ b/lib/crewai/src/crewai/events/types/crew_events.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, Literal from crewai.events.base_events import BaseEvent @@ -37,14 +37,14 @@ class CrewKickoffStartedEvent(CrewBaseEvent): """Event emitted when a crew starts execution""" inputs: dict[str, Any] | None - type: str = "crew_kickoff_started" + type: Literal["crew_kickoff_started"] = "crew_kickoff_started" class CrewKickoffCompletedEvent(CrewBaseEvent): """Event emitted when a crew completes execution""" output: Any - type: str = "crew_kickoff_completed" + type: Literal["crew_kickoff_completed"] = "crew_kickoff_completed" total_tokens: int = 0 @@ -52,7 +52,7 @@ class CrewKickoffFailedEvent(CrewBaseEvent): """Event emitted when a crew fails to complete execution""" error: str - type: str = "crew_kickoff_failed" + type: Literal["crew_kickoff_failed"] = "crew_kickoff_failed" class CrewTrainStartedEvent(CrewBaseEvent): @@ -61,7 +61,7 @@ class CrewTrainStartedEvent(CrewBaseEvent): n_iterations: int filename: str inputs: dict[str, Any] | None - type: str = "crew_train_started" + type: Literal["crew_train_started"] = "crew_train_started" class CrewTrainCompletedEvent(CrewBaseEvent): @@ -69,14 +69,14 @@ class CrewTrainCompletedEvent(CrewBaseEvent): n_iterations: int filename: str - type: str = "crew_train_completed" + type: Literal["crew_train_completed"] = "crew_train_completed" class CrewTrainFailedEvent(CrewBaseEvent): """Event emitted when a crew fails to complete training""" error: str - type: str = "crew_train_failed" + type: Literal["crew_train_failed"] = "crew_train_failed" class CrewTestStartedEvent(CrewBaseEvent): @@ -85,20 +85,20 @@ class CrewTestStartedEvent(CrewBaseEvent): n_iterations: int eval_llm: str | Any | None inputs: dict[str, Any] | None - type: str = "crew_test_started" + type: Literal["crew_test_started"] = "crew_test_started" class CrewTestCompletedEvent(CrewBaseEvent): """Event emitted when a crew completes testing""" - type: str = "crew_test_completed" + type: Literal["crew_test_completed"] = "crew_test_completed" class CrewTestFailedEvent(CrewBaseEvent): """Event emitted when a crew fails to complete testing""" error: str - type: str = "crew_test_failed" + type: Literal["crew_test_failed"] = "crew_test_failed" class CrewTestResultEvent(CrewBaseEvent): @@ -107,4 +107,4 @@ class CrewTestResultEvent(CrewBaseEvent): quality: float execution_duration: float model: str - type: str = "crew_test_result" + type: Literal["crew_test_result"] = "crew_test_result" diff --git a/lib/crewai/src/crewai/events/types/flow_events.py b/lib/crewai/src/crewai/events/types/flow_events.py index d820b8a05..c2c1e2912 100644 --- a/lib/crewai/src/crewai/events/types/flow_events.py +++ b/lib/crewai/src/crewai/events/types/flow_events.py @@ -1,4 +1,4 @@ -from typing import Any +from typing import Any, Literal from pydantic import BaseModel, ConfigDict @@ -17,14 +17,14 @@ class FlowStartedEvent(FlowEvent): flow_name: str inputs: dict[str, Any] | None = None - type: str = "flow_started" + type: Literal["flow_started"] = "flow_started" class FlowCreatedEvent(FlowEvent): """Event emitted when a flow is created""" flow_name: str - type: str = "flow_created" + type: Literal["flow_created"] = "flow_created" class MethodExecutionStartedEvent(FlowEvent): @@ -34,7 +34,7 @@ class MethodExecutionStartedEvent(FlowEvent): method_name: str state: dict[str, Any] | BaseModel params: dict[str, Any] | None = None - type: str = "method_execution_started" + type: Literal["method_execution_started"] = "method_execution_started" class MethodExecutionFinishedEvent(FlowEvent): @@ -44,7 +44,7 @@ class MethodExecutionFinishedEvent(FlowEvent): method_name: str result: Any = None state: dict[str, Any] | BaseModel - type: str = "method_execution_finished" + type: Literal["method_execution_finished"] = "method_execution_finished" class MethodExecutionFailedEvent(FlowEvent): @@ -53,7 +53,7 @@ class MethodExecutionFailedEvent(FlowEvent): flow_name: str method_name: str error: Exception - type: str = "method_execution_failed" + type: Literal["method_execution_failed"] = "method_execution_failed" model_config = ConfigDict(arbitrary_types_allowed=True) @@ -78,7 +78,7 @@ class MethodExecutionPausedEvent(FlowEvent): flow_id: str message: str emit: list[str] | None = None - type: str = "method_execution_paused" + type: Literal["method_execution_paused"] = "method_execution_paused" class FlowFinishedEvent(FlowEvent): @@ -86,7 +86,7 @@ class FlowFinishedEvent(FlowEvent): flow_name: str result: Any | None = None - type: str = "flow_finished" + type: Literal["flow_finished"] = "flow_finished" state: dict[str, Any] | BaseModel @@ -110,14 +110,14 @@ class FlowPausedEvent(FlowEvent): state: dict[str, Any] | BaseModel message: str emit: list[str] | None = None - type: str = "flow_paused" + type: Literal["flow_paused"] = "flow_paused" class FlowPlotEvent(FlowEvent): """Event emitted when a flow plot is created""" flow_name: str - type: str = "flow_plot" + type: Literal["flow_plot"] = "flow_plot" class FlowInputRequestedEvent(FlowEvent): @@ -138,7 +138,7 @@ class FlowInputRequestedEvent(FlowEvent): method_name: str message: str metadata: dict[str, Any] | None = None - type: str = "flow_input_requested" + type: Literal["flow_input_requested"] = "flow_input_requested" class FlowInputReceivedEvent(FlowEvent): @@ -163,7 +163,7 @@ class FlowInputReceivedEvent(FlowEvent): response: str | None = None metadata: dict[str, Any] | None = None response_metadata: dict[str, Any] | None = None - type: str = "flow_input_received" + type: Literal["flow_input_received"] = "flow_input_received" class HumanFeedbackRequestedEvent(FlowEvent): @@ -187,7 +187,7 @@ class HumanFeedbackRequestedEvent(FlowEvent): message: str emit: list[str] | None = None request_id: str | None = None - type: str = "human_feedback_requested" + type: Literal["human_feedback_requested"] = "human_feedback_requested" class HumanFeedbackReceivedEvent(FlowEvent): @@ -209,4 +209,4 @@ class HumanFeedbackReceivedEvent(FlowEvent): feedback: str outcome: str | None = None request_id: str | None = None - type: str = "human_feedback_received" + type: Literal["human_feedback_received"] = "human_feedback_received" diff --git a/lib/crewai/src/crewai/events/types/knowledge_events.py b/lib/crewai/src/crewai/events/types/knowledge_events.py index a2d9af728..086e89377 100644 --- a/lib/crewai/src/crewai/events/types/knowledge_events.py +++ b/lib/crewai/src/crewai/events/types/knowledge_events.py @@ -1,4 +1,4 @@ -from typing import Any +from typing import Any, Literal from crewai.events.base_events import BaseEvent @@ -20,14 +20,16 @@ class KnowledgeEventBase(BaseEvent): class KnowledgeRetrievalStartedEvent(KnowledgeEventBase): """Event emitted when a knowledge retrieval is started.""" - type: str = "knowledge_search_query_started" + type: Literal["knowledge_search_query_started"] = "knowledge_search_query_started" class KnowledgeRetrievalCompletedEvent(KnowledgeEventBase): """Event emitted when a knowledge retrieval is completed.""" query: str - type: str = "knowledge_search_query_completed" + type: Literal["knowledge_search_query_completed"] = ( + "knowledge_search_query_completed" + ) retrieved_knowledge: str @@ -35,13 +37,13 @@ class KnowledgeQueryStartedEvent(KnowledgeEventBase): """Event emitted when a knowledge query is started.""" task_prompt: str - type: str = "knowledge_query_started" + type: Literal["knowledge_query_started"] = "knowledge_query_started" class KnowledgeQueryFailedEvent(KnowledgeEventBase): """Event emitted when a knowledge query fails.""" - type: str = "knowledge_query_failed" + type: Literal["knowledge_query_failed"] = "knowledge_query_failed" error: str @@ -49,12 +51,12 @@ class KnowledgeQueryCompletedEvent(KnowledgeEventBase): """Event emitted when a knowledge query is completed.""" query: str - type: str = "knowledge_query_completed" + type: Literal["knowledge_query_completed"] = "knowledge_query_completed" class KnowledgeSearchQueryFailedEvent(KnowledgeEventBase): """Event emitted when a knowledge search query fails.""" query: str - type: str = "knowledge_search_query_failed" + type: Literal["knowledge_search_query_failed"] = "knowledge_search_query_failed" error: str diff --git a/lib/crewai/src/crewai/events/types/llm_events.py b/lib/crewai/src/crewai/events/types/llm_events.py index 4b8c96d9e..b138f908c 100644 --- a/lib/crewai/src/crewai/events/types/llm_events.py +++ b/lib/crewai/src/crewai/events/types/llm_events.py @@ -1,5 +1,5 @@ from enum import Enum -from typing import Any +from typing import Any, Literal from pydantic import BaseModel @@ -43,7 +43,7 @@ class LLMCallStartedEvent(LLMEventBase): multimodal content (text, images, etc.) """ - type: str = "llm_call_started" + type: Literal["llm_call_started"] = "llm_call_started" messages: str | list[dict[str, Any]] | None = None tools: list[dict[str, Any]] | None = None callbacks: list[Any] | None = None @@ -53,7 +53,7 @@ class LLMCallStartedEvent(LLMEventBase): class LLMCallCompletedEvent(LLMEventBase): """Event emitted when a LLM call completes""" - type: str = "llm_call_completed" + type: Literal["llm_call_completed"] = "llm_call_completed" messages: str | list[dict[str, Any]] | None = None response: Any call_type: LLMCallType @@ -64,7 +64,7 @@ class LLMCallFailedEvent(LLMEventBase): """Event emitted when a LLM call fails""" error: str - type: str = "llm_call_failed" + type: Literal["llm_call_failed"] = "llm_call_failed" class FunctionCall(BaseModel): @@ -82,7 +82,7 @@ class ToolCall(BaseModel): class LLMStreamChunkEvent(LLMEventBase): """Event emitted when a streaming chunk is received""" - type: str = "llm_stream_chunk" + type: Literal["llm_stream_chunk"] = "llm_stream_chunk" chunk: str tool_call: ToolCall | None = None call_type: LLMCallType | None = None @@ -92,6 +92,6 @@ class LLMStreamChunkEvent(LLMEventBase): class LLMThinkingChunkEvent(LLMEventBase): """Event emitted when a thinking/reasoning chunk is received from a thinking model""" - type: str = "llm_thinking_chunk" + type: Literal["llm_thinking_chunk"] = "llm_thinking_chunk" chunk: str response_id: str | None = None diff --git a/lib/crewai/src/crewai/events/types/llm_guardrail_events.py b/lib/crewai/src/crewai/events/types/llm_guardrail_events.py index fdf82cd2a..8bbcf6e0b 100644 --- a/lib/crewai/src/crewai/events/types/llm_guardrail_events.py +++ b/lib/crewai/src/crewai/events/types/llm_guardrail_events.py @@ -1,6 +1,6 @@ from collections.abc import Callable from inspect import getsource -from typing import Any +from typing import Any, Literal from crewai.events.base_events import BaseEvent @@ -27,7 +27,7 @@ class LLMGuardrailStartedEvent(LLMGuardrailBaseEvent): retry_count: The number of times the guardrail has been retried """ - type: str = "llm_guardrail_started" + type: Literal["llm_guardrail_started"] = "llm_guardrail_started" guardrail: str | Callable[..., Any] retry_count: int @@ -53,7 +53,7 @@ class LLMGuardrailCompletedEvent(LLMGuardrailBaseEvent): retry_count: The number of times the guardrail has been retried """ - type: str = "llm_guardrail_completed" + type: Literal["llm_guardrail_completed"] = "llm_guardrail_completed" success: bool result: Any error: str | None = None @@ -68,6 +68,6 @@ class LLMGuardrailFailedEvent(LLMGuardrailBaseEvent): retry_count: The number of times the guardrail has been retried """ - type: str = "llm_guardrail_failed" + type: Literal["llm_guardrail_failed"] = "llm_guardrail_failed" error: str retry_count: int diff --git a/lib/crewai/src/crewai/events/types/logging_events.py b/lib/crewai/src/crewai/events/types/logging_events.py index 31b8bdd1e..6bd0ff3e3 100644 --- a/lib/crewai/src/crewai/events/types/logging_events.py +++ b/lib/crewai/src/crewai/events/types/logging_events.py @@ -1,6 +1,6 @@ """Agent logging events that don't reference BaseAgent to avoid circular imports.""" -from typing import Any +from typing import Any, Literal from pydantic import ConfigDict @@ -13,7 +13,7 @@ class AgentLogsStartedEvent(BaseEvent): agent_role: str task_description: str | None = None verbose: bool = False - type: str = "agent_logs_started" + type: Literal["agent_logs_started"] = "agent_logs_started" class AgentLogsExecutionEvent(BaseEvent): @@ -22,6 +22,6 @@ class AgentLogsExecutionEvent(BaseEvent): agent_role: str formatted_answer: Any verbose: bool = False - type: str = "agent_logs_execution" + type: Literal["agent_logs_execution"] = "agent_logs_execution" model_config = ConfigDict(arbitrary_types_allowed=True) diff --git a/lib/crewai/src/crewai/events/types/mcp_events.py b/lib/crewai/src/crewai/events/types/mcp_events.py index a89d4df70..c9278dec0 100644 --- a/lib/crewai/src/crewai/events/types/mcp_events.py +++ b/lib/crewai/src/crewai/events/types/mcp_events.py @@ -1,5 +1,5 @@ from datetime import datetime -from typing import Any +from typing import Any, Literal from crewai.events.base_events import BaseEvent @@ -24,7 +24,7 @@ class MCPEvent(BaseEvent): class MCPConnectionStartedEvent(MCPEvent): """Event emitted when starting to connect to an MCP server.""" - type: str = "mcp_connection_started" + type: Literal["mcp_connection_started"] = "mcp_connection_started" connect_timeout: int | None = None is_reconnect: bool = ( False # True if this is a reconnection, False for first connection @@ -34,7 +34,7 @@ class MCPConnectionStartedEvent(MCPEvent): class MCPConnectionCompletedEvent(MCPEvent): """Event emitted when successfully connected to an MCP server.""" - type: str = "mcp_connection_completed" + type: Literal["mcp_connection_completed"] = "mcp_connection_completed" started_at: datetime | None = None completed_at: datetime | None = None connection_duration_ms: float | None = None @@ -46,7 +46,7 @@ class MCPConnectionCompletedEvent(MCPEvent): class MCPConnectionFailedEvent(MCPEvent): """Event emitted when connection to an MCP server fails.""" - type: str = "mcp_connection_failed" + type: Literal["mcp_connection_failed"] = "mcp_connection_failed" error: str error_type: str | None = None # "timeout", "authentication", "network", etc. started_at: datetime | None = None @@ -56,7 +56,7 @@ class MCPConnectionFailedEvent(MCPEvent): class MCPToolExecutionStartedEvent(MCPEvent): """Event emitted when starting to execute an MCP tool.""" - type: str = "mcp_tool_execution_started" + type: Literal["mcp_tool_execution_started"] = "mcp_tool_execution_started" tool_name: str tool_args: dict[str, Any] | None = None @@ -64,7 +64,7 @@ class MCPToolExecutionStartedEvent(MCPEvent): class MCPToolExecutionCompletedEvent(MCPEvent): """Event emitted when MCP tool execution completes.""" - type: str = "mcp_tool_execution_completed" + type: Literal["mcp_tool_execution_completed"] = "mcp_tool_execution_completed" tool_name: str tool_args: dict[str, Any] | None = None result: Any | None = None @@ -76,7 +76,7 @@ class MCPToolExecutionCompletedEvent(MCPEvent): class MCPToolExecutionFailedEvent(MCPEvent): """Event emitted when MCP tool execution fails.""" - type: str = "mcp_tool_execution_failed" + type: Literal["mcp_tool_execution_failed"] = "mcp_tool_execution_failed" tool_name: str tool_args: dict[str, Any] | None = None error: str @@ -92,7 +92,7 @@ class MCPConfigFetchFailedEvent(BaseEvent): failed, or native MCP resolution failed after config was fetched. """ - type: str = "mcp_config_fetch_failed" + type: Literal["mcp_config_fetch_failed"] = "mcp_config_fetch_failed" slug: str error: str error_type: str | None = None # "not_connected", "api_error", "connection_failed" diff --git a/lib/crewai/src/crewai/events/types/memory_events.py b/lib/crewai/src/crewai/events/types/memory_events.py index 0fd57a352..1d6b05017 100644 --- a/lib/crewai/src/crewai/events/types/memory_events.py +++ b/lib/crewai/src/crewai/events/types/memory_events.py @@ -1,4 +1,4 @@ -from typing import Any +from typing import Any, Literal from crewai.events.base_events import BaseEvent @@ -23,7 +23,7 @@ class MemoryBaseEvent(BaseEvent): class MemoryQueryStartedEvent(MemoryBaseEvent): """Event emitted when a memory query is started""" - type: str = "memory_query_started" + type: Literal["memory_query_started"] = "memory_query_started" query: str limit: int score_threshold: float | None = None @@ -32,7 +32,7 @@ class MemoryQueryStartedEvent(MemoryBaseEvent): class MemoryQueryCompletedEvent(MemoryBaseEvent): """Event emitted when a memory query is completed successfully""" - type: str = "memory_query_completed" + type: Literal["memory_query_completed"] = "memory_query_completed" query: str results: Any limit: int @@ -43,7 +43,7 @@ class MemoryQueryCompletedEvent(MemoryBaseEvent): class MemoryQueryFailedEvent(MemoryBaseEvent): """Event emitted when a memory query fails""" - type: str = "memory_query_failed" + type: Literal["memory_query_failed"] = "memory_query_failed" query: str limit: int score_threshold: float | None = None @@ -53,7 +53,7 @@ class MemoryQueryFailedEvent(MemoryBaseEvent): class MemorySaveStartedEvent(MemoryBaseEvent): """Event emitted when a memory save operation is started""" - type: str = "memory_save_started" + type: Literal["memory_save_started"] = "memory_save_started" value: str | None = None metadata: dict[str, Any] | None = None agent_role: str | None = None @@ -62,7 +62,7 @@ class MemorySaveStartedEvent(MemoryBaseEvent): class MemorySaveCompletedEvent(MemoryBaseEvent): """Event emitted when a memory save operation is completed successfully""" - type: str = "memory_save_completed" + type: Literal["memory_save_completed"] = "memory_save_completed" value: str metadata: dict[str, Any] | None = None agent_role: str | None = None @@ -72,7 +72,7 @@ class MemorySaveCompletedEvent(MemoryBaseEvent): class MemorySaveFailedEvent(MemoryBaseEvent): """Event emitted when a memory save operation fails""" - type: str = "memory_save_failed" + type: Literal["memory_save_failed"] = "memory_save_failed" value: str | None = None metadata: dict[str, Any] | None = None agent_role: str | None = None @@ -82,14 +82,14 @@ class MemorySaveFailedEvent(MemoryBaseEvent): class MemoryRetrievalStartedEvent(MemoryBaseEvent): """Event emitted when memory retrieval for a task prompt starts""" - type: str = "memory_retrieval_started" + type: Literal["memory_retrieval_started"] = "memory_retrieval_started" task_id: str | None = None class MemoryRetrievalCompletedEvent(MemoryBaseEvent): """Event emitted when memory retrieval for a task prompt completes successfully""" - type: str = "memory_retrieval_completed" + type: Literal["memory_retrieval_completed"] = "memory_retrieval_completed" task_id: str | None = None memory_content: str retrieval_time_ms: float @@ -98,6 +98,6 @@ class MemoryRetrievalCompletedEvent(MemoryBaseEvent): class MemoryRetrievalFailedEvent(MemoryBaseEvent): """Event emitted when memory retrieval for a task prompt fails.""" - type: str = "memory_retrieval_failed" + type: Literal["memory_retrieval_failed"] = "memory_retrieval_failed" task_id: str | None = None error: str diff --git a/lib/crewai/src/crewai/events/types/observation_events.py b/lib/crewai/src/crewai/events/types/observation_events.py index 2c95f3ae0..beac6d235 100644 --- a/lib/crewai/src/crewai/events/types/observation_events.py +++ b/lib/crewai/src/crewai/events/types/observation_events.py @@ -5,7 +5,7 @@ PlannerObserver analyzes step execution results and decides on plan continuation, refinement, or replanning. """ -from typing import Any +from typing import Any, Literal from crewai.events.base_events import BaseEvent @@ -32,7 +32,7 @@ class StepObservationStartedEvent(ObservationEvent): Fires after every step execution, before the observation LLM call. """ - type: str = "step_observation_started" + type: Literal["step_observation_started"] = "step_observation_started" class StepObservationCompletedEvent(ObservationEvent): @@ -42,7 +42,7 @@ class StepObservationCompletedEvent(ObservationEvent): the plan is still valid, and what action to take next. """ - type: str = "step_observation_completed" + type: Literal["step_observation_completed"] = "step_observation_completed" step_completed_successfully: bool = True key_information_learned: str = "" remaining_plan_still_valid: bool = True @@ -59,7 +59,7 @@ class StepObservationFailedEvent(ObservationEvent): but the event allows monitoring/alerting on observation failures. """ - type: str = "step_observation_failed" + type: Literal["step_observation_failed"] = "step_observation_failed" error: str = "" @@ -70,7 +70,7 @@ class PlanRefinementEvent(ObservationEvent): sharpening pending todo descriptions based on new information. """ - type: str = "plan_refinement" + type: Literal["plan_refinement"] = "plan_refinement" refined_step_count: int = 0 refinements: list[str] | None = None @@ -82,7 +82,7 @@ class PlanReplanTriggeredEvent(ObservationEvent): regenerated from scratch, preserving completed step results. """ - type: str = "plan_replan_triggered" + type: Literal["plan_replan_triggered"] = "plan_replan_triggered" replan_reason: str = "" replan_count: int = 0 completed_steps_preserved: int = 0 @@ -94,6 +94,6 @@ class GoalAchievedEarlyEvent(ObservationEvent): Remaining steps will be skipped and execution will finalize. """ - type: str = "goal_achieved_early" + type: Literal["goal_achieved_early"] = "goal_achieved_early" steps_remaining: int = 0 steps_completed: int = 0 diff --git a/lib/crewai/src/crewai/events/types/reasoning_events.py b/lib/crewai/src/crewai/events/types/reasoning_events.py index f9c9c1dc3..cb565a66e 100644 --- a/lib/crewai/src/crewai/events/types/reasoning_events.py +++ b/lib/crewai/src/crewai/events/types/reasoning_events.py @@ -1,4 +1,4 @@ -from typing import Any +from typing import Any, Literal from crewai.events.base_events import BaseEvent @@ -24,7 +24,7 @@ class ReasoningEvent(BaseEvent): class AgentReasoningStartedEvent(ReasoningEvent): """Event emitted when an agent starts reasoning about a task.""" - type: str = "agent_reasoning_started" + type: Literal["agent_reasoning_started"] = "agent_reasoning_started" agent_role: str task_id: str @@ -32,7 +32,7 @@ class AgentReasoningStartedEvent(ReasoningEvent): class AgentReasoningCompletedEvent(ReasoningEvent): """Event emitted when an agent finishes its reasoning process.""" - type: str = "agent_reasoning_completed" + type: Literal["agent_reasoning_completed"] = "agent_reasoning_completed" agent_role: str task_id: str plan: str @@ -42,7 +42,7 @@ class AgentReasoningCompletedEvent(ReasoningEvent): class AgentReasoningFailedEvent(ReasoningEvent): """Event emitted when the reasoning process fails.""" - type: str = "agent_reasoning_failed" + type: Literal["agent_reasoning_failed"] = "agent_reasoning_failed" agent_role: str task_id: str error: str diff --git a/lib/crewai/src/crewai/events/types/skill_events.py b/lib/crewai/src/crewai/events/types/skill_events.py index f99d6bd70..aab625dda 100644 --- a/lib/crewai/src/crewai/events/types/skill_events.py +++ b/lib/crewai/src/crewai/events/types/skill_events.py @@ -6,7 +6,7 @@ Events emitted during skill discovery, loading, and activation. from __future__ import annotations from pathlib import Path -from typing import Any +from typing import Any, Literal from crewai.events.base_events import BaseEvent @@ -28,14 +28,14 @@ class SkillEvent(BaseEvent): class SkillDiscoveryStartedEvent(SkillEvent): """Event emitted when skill discovery begins.""" - type: str = "skill_discovery_started" + type: Literal["skill_discovery_started"] = "skill_discovery_started" search_path: Path class SkillDiscoveryCompletedEvent(SkillEvent): """Event emitted when skill discovery completes.""" - type: str = "skill_discovery_completed" + type: Literal["skill_discovery_completed"] = "skill_discovery_completed" search_path: Path skills_found: int skill_names: list[str] @@ -44,19 +44,19 @@ class SkillDiscoveryCompletedEvent(SkillEvent): class SkillLoadedEvent(SkillEvent): """Event emitted when a skill is loaded at metadata level.""" - type: str = "skill_loaded" + type: Literal["skill_loaded"] = "skill_loaded" disclosure_level: int = 1 class SkillActivatedEvent(SkillEvent): """Event emitted when a skill is activated (promoted to instructions level).""" - type: str = "skill_activated" + type: Literal["skill_activated"] = "skill_activated" disclosure_level: int = 2 class SkillLoadFailedEvent(SkillEvent): """Event emitted when skill loading fails.""" - type: str = "skill_load_failed" + type: Literal["skill_load_failed"] = "skill_load_failed" error: str diff --git a/lib/crewai/src/crewai/events/types/task_events.py b/lib/crewai/src/crewai/events/types/task_events.py index d042db22e..69609e3fd 100644 --- a/lib/crewai/src/crewai/events/types/task_events.py +++ b/lib/crewai/src/crewai/events/types/task_events.py @@ -1,4 +1,4 @@ -from typing import Any +from typing import Any, Literal from crewai.events.base_events import BaseEvent from crewai.tasks.task_output import TaskOutput @@ -24,7 +24,7 @@ def _set_task_fingerprint(event: BaseEvent, task: Any) -> None: class TaskStartedEvent(BaseEvent): """Event emitted when a task starts""" - type: str = "task_started" + type: Literal["task_started"] = "task_started" context: str | None task: Any | None = None @@ -37,7 +37,7 @@ class TaskCompletedEvent(BaseEvent): """Event emitted when a task completes""" output: TaskOutput - type: str = "task_completed" + type: Literal["task_completed"] = "task_completed" task: Any | None = None def __init__(self, **data: Any) -> None: @@ -49,7 +49,7 @@ class TaskFailedEvent(BaseEvent): """Event emitted when a task fails""" error: str - type: str = "task_failed" + type: Literal["task_failed"] = "task_failed" task: Any | None = None def __init__(self, **data: Any) -> None: @@ -60,7 +60,7 @@ class TaskFailedEvent(BaseEvent): class TaskEvaluationEvent(BaseEvent): """Event emitted when a task evaluation is completed""" - type: str = "task_evaluation" + type: Literal["task_evaluation"] = "task_evaluation" evaluation_type: str task: Any | None = None diff --git a/lib/crewai/src/crewai/events/types/tool_usage_events.py b/lib/crewai/src/crewai/events/types/tool_usage_events.py index c4e681546..44edbe0ac 100644 --- a/lib/crewai/src/crewai/events/types/tool_usage_events.py +++ b/lib/crewai/src/crewai/events/types/tool_usage_events.py @@ -1,6 +1,6 @@ from collections.abc import Callable from datetime import datetime -from typing import Any +from typing import Any, Literal from pydantic import ConfigDict @@ -55,7 +55,7 @@ class ToolUsageEvent(BaseEvent): class ToolUsageStartedEvent(ToolUsageEvent): """Event emitted when a tool execution is started""" - type: str = "tool_usage_started" + type: Literal["tool_usage_started"] = "tool_usage_started" class ToolUsageFinishedEvent(ToolUsageEvent): @@ -65,35 +65,35 @@ class ToolUsageFinishedEvent(ToolUsageEvent): finished_at: datetime from_cache: bool = False output: Any - type: str = "tool_usage_finished" + type: Literal["tool_usage_finished"] = "tool_usage_finished" class ToolUsageErrorEvent(ToolUsageEvent): """Event emitted when a tool execution encounters an error""" error: Any - type: str = "tool_usage_error" + type: Literal["tool_usage_error"] = "tool_usage_error" class ToolValidateInputErrorEvent(ToolUsageEvent): """Event emitted when a tool input validation encounters an error""" error: Any - type: str = "tool_validate_input_error" + type: Literal["tool_validate_input_error"] = "tool_validate_input_error" class ToolSelectionErrorEvent(ToolUsageEvent): """Event emitted when a tool selection encounters an error""" error: Any - type: str = "tool_selection_error" + type: Literal["tool_selection_error"] = "tool_selection_error" class ToolExecutionErrorEvent(BaseEvent): """Event emitted when a tool execution encounters an error""" error: Any - type: str = "tool_execution_error" + type: Literal["tool_execution_error"] = "tool_execution_error" tool_name: str tool_args: dict[str, Any] tool_class: Callable[..., Any] diff --git a/lib/crewai/src/crewai/state/event_record.py b/lib/crewai/src/crewai/state/event_record.py index a06dec398..7b8c20c5b 100644 --- a/lib/crewai/src/crewai/state/event_record.py +++ b/lib/crewai/src/crewai/state/event_record.py @@ -6,14 +6,48 @@ sequential relationships. Provides O(1) lookups and traversal. from __future__ import annotations -from typing import Literal +from typing import Annotated, Any, Literal -from pydantic import BaseModel, Field, PrivateAttr +from pydantic import BaseModel, BeforeValidator, Field, PlainSerializer, PrivateAttr from crewai.events.base_events import BaseEvent from crewai.utilities.rw_lock import RWLock +_event_type_map: dict[str, type[BaseEvent]] = {} + + +def _resolve_event(v: Any) -> BaseEvent: + """Validate an event value into the correct BaseEvent subclass.""" + if isinstance(v, BaseEvent): + return v + if not isinstance(v, dict): + return BaseEvent.model_validate(v) + if not _event_type_map: + _build_event_type_map() + event_type = v.get("type", "") + cls = _event_type_map.get(event_type, BaseEvent) + if cls is BaseEvent: + return BaseEvent.model_validate(v) + try: + return cls.model_validate(v) + except Exception: + return BaseEvent.model_validate(v) + + +def _build_event_type_map() -> None: + """Populate _event_type_map from all BaseEvent subclasses.""" + + def _collect(cls: type[BaseEvent]) -> None: + for sub in cls.__subclasses__(): + type_field = sub.model_fields.get("type") + if type_field and type_field.default: + _event_type_map[type_field.default] = sub + _collect(sub) + + _collect(BaseEvent) + + EdgeType = Literal[ "parent", "child", @@ -29,7 +63,11 @@ EdgeType = Literal[ class EventNode(BaseModel): """A node wrapping a single event with its adjacency lists.""" - event: BaseEvent + event: Annotated[ + BaseEvent, + BeforeValidator(_resolve_event), + PlainSerializer(lambda v: v.model_dump()), + ] edges: dict[EdgeType, list[str]] = Field(default_factory=dict) def add_edge(self, edge_type: EdgeType, target_id: str) -> None: