mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-04-30 23:02:50 +00:00
Some checks failed
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Notify Downstream / notify-downstream (push) Has been cancelled
Build uv cache / build-cache (3.10) (push) Has been cancelled
Build uv cache / build-cache (3.11) (push) Has been cancelled
Build uv cache / build-cache (3.12) (push) Has been cancelled
Build uv cache / build-cache (3.13) (push) Has been cancelled
Introduce ContextVar-backed hooks and small API/behavior changes to improve extensibility and testability. Changes include: - agents: mark configure_structured_output as abstract and change its parameter to task to reflect use of task metadata. - tracing: convert _first_time_trace_hook to a ContextVar and call .get() to safely retrieve the hook. - console formatter: add _disable_version_check ContextVar and skip version checks when set (avoids noisy checks in certain contexts). - flow: use current_triggering_event_id variable when scheduling listener tasks to keep naming consistent. - hallucination guardrail: make context optional, add _validate_output_hook to allow custom validation hooks, update examples and return contract to allow hooks to override behavior. - agent utilities: add _create_plus_client_hook for injecting a Plus client (used in tests/alternate flows), ensure structured tools have current_usage_count initialized and propagate to original tool, and fall back to creating PlusAPI client when no hook is provided.
96 lines
2.5 KiB
Python
96 lines
2.5 KiB
Python
"""Utility for colored console output."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Final, Literal, NamedTuple
|
|
|
|
from crewai.events.utils.console_formatter import should_suppress_console_output
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
from _typeshed import SupportsWrite
|
|
|
|
PrinterColor = Literal[
|
|
"purple",
|
|
"bold_purple",
|
|
"green",
|
|
"bold_green",
|
|
"cyan",
|
|
"bold_cyan",
|
|
"magenta",
|
|
"bold_magenta",
|
|
"yellow",
|
|
"bold_yellow",
|
|
"red",
|
|
"blue",
|
|
"bold_blue",
|
|
]
|
|
|
|
_COLOR_CODES: Final[dict[PrinterColor, str]] = {
|
|
"purple": "\033[95m",
|
|
"bold_purple": "\033[1m\033[95m",
|
|
"red": "\033[91m",
|
|
"bold_green": "\033[1m\033[92m",
|
|
"green": "\033[32m",
|
|
"blue": "\033[94m",
|
|
"bold_blue": "\033[1m\033[94m",
|
|
"yellow": "\033[93m",
|
|
"bold_yellow": "\033[1m\033[93m",
|
|
"cyan": "\033[96m",
|
|
"bold_cyan": "\033[1m\033[96m",
|
|
"magenta": "\033[35m",
|
|
"bold_magenta": "\033[1m\033[35m",
|
|
}
|
|
|
|
RESET: Final[str] = "\033[0m"
|
|
|
|
|
|
class ColoredText(NamedTuple):
|
|
"""Represents text with an optional color for console output.
|
|
|
|
Attributes:
|
|
text: The text content to be printed.
|
|
color: Optional color for the text, specified as a PrinterColor.
|
|
"""
|
|
|
|
text: str
|
|
color: PrinterColor | None
|
|
|
|
|
|
class Printer:
|
|
"""Handles colored console output formatting."""
|
|
|
|
@staticmethod
|
|
def print(
|
|
content: str | list[ColoredText],
|
|
color: PrinterColor | None = None,
|
|
sep: str | None = " ",
|
|
end: str | None = "\n",
|
|
file: SupportsWrite[str] | None = None,
|
|
flush: Literal[False] = False,
|
|
) -> None:
|
|
"""Prints content to the console with optional color formatting.
|
|
|
|
Args:
|
|
content: Either a string or a list of ColoredText objects for multicolor output.
|
|
color: Optional color for the text when content is a string. Ignored when content is a list.
|
|
sep: Separator to use between the text and color.
|
|
end: String appended after the last value.
|
|
file: A file-like object (stream); defaults to the current sys.stdout.
|
|
flush: Whether to forcibly flush the stream.
|
|
"""
|
|
if should_suppress_console_output():
|
|
return
|
|
if isinstance(content, str):
|
|
content = [ColoredText(content, color)]
|
|
print(
|
|
"".join(
|
|
f"{_COLOR_CODES[c.color] if c.color else ''}{c.text}{RESET}"
|
|
for c in content
|
|
),
|
|
sep=sep,
|
|
end=end,
|
|
file=file,
|
|
flush=flush,
|
|
)
|