flock-core 0.2.13__py3-none-any.whl → 0.2.15__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of flock-core might be problematic. Click here for more details.

@@ -1,132 +0,0 @@
1
- from typing import Any
2
-
3
- from devtools import pprint
4
- from temporalio import workflow
5
-
6
- from flock.core.logging.formatters.base_formatter import BaseFormatter
7
-
8
- with workflow.unsafe.imports_passed_through():
9
- from rich.console import Console, Group
10
- from rich.panel import Panel
11
- from rich.table import Table
12
-
13
-
14
- def create_rich_renderable(
15
- value: Any,
16
- level: int = 0,
17
- max_length: int = -1,
18
- ) -> Any:
19
- """Recursively creates a Rich renderable for a given value.
20
-
21
- - If the value is a dict, return a Table representing the dict.
22
- - If the value is a list or tuple:
23
- - If all items are dicts, return a Group of subtables.
24
- - Otherwise, render each item recursively and either join them as text
25
- (if all rendered items are strings) or return a Group.
26
- - Otherwise, return a string (with extra newlines if it's a multi-line string).
27
- """
28
- if isinstance(value, dict):
29
- # Create a subtable for the dictionary.
30
- # You can tweak the table styles or add a title that indicates the level.
31
- table = Table(
32
- show_header=True,
33
- header_style="bold green",
34
- title=f"Subtable (Level {level})" if level > 0 else None,
35
- title_style="bold blue",
36
- border_style="bright_blue",
37
- show_lines=True,
38
- )
39
- table.add_column("Key", style="cyan")
40
- table.add_column("Value", style="green")
41
- for k, v in value.items():
42
- table.add_row(
43
- str(k),
44
- create_rich_renderable(v, level + 1, max_length=max_length),
45
- )
46
- return table
47
-
48
- elif isinstance(value, list | tuple):
49
- # If all items are dicts, build a Group of subtables with an index.
50
- if all(isinstance(item, dict) for item in value):
51
- sub_tables = []
52
- for i, item in enumerate(value):
53
- sub_tables.append(f"[bold]Item {i + 1}[/bold]")
54
- sub_tables.append(
55
- create_rich_renderable(
56
- item, level + 1, max_length=max_length
57
- )
58
- )
59
- return Group(*sub_tables)
60
- else:
61
- # For a mixed list, render each item recursively.
62
- rendered_items = [
63
- create_rich_renderable(item, level + 1, max_length=max_length)
64
- for item in value
65
- ]
66
- # If all items ended up as strings, join them.
67
- if all(isinstance(item, str) for item in rendered_items):
68
- return "\n".join(rendered_items)
69
- else:
70
- return Group(*rendered_items)
71
-
72
- else:
73
- s = str(value).strip()
74
- if max_length > 0 and len(s) > max_length:
75
- omitted = len(s) - max_length
76
- s = (
77
- s[:max_length]
78
- + f"[bold bright_yellow]...(+{omitted}chars)[/bold bright_yellow]"
79
- )
80
- if isinstance(value, str) and "\n" in value:
81
- return f"\n{s}\n"
82
- return s
83
-
84
-
85
- class RichTables(BaseFormatter):
86
- """Formats agent results in a beautiful Rich table with nested subtables."""
87
-
88
- def __init__(self, max_length: int = -1):
89
- self.max_length = max_length
90
-
91
- def format_result(self, result: dict[str, Any], agent_name: str) -> Panel:
92
- """Format an agent's result as a Rich panel containing a table."""
93
- # Create the main table.
94
- table = Table(
95
- show_lines=True,
96
- show_header=True,
97
- header_style="bold green",
98
- title=f"Agent Results: {agent_name}",
99
- title_style="bold blue",
100
- border_style="bright_blue",
101
- )
102
- table.add_column("Output", style="cyan")
103
- table.add_column("Value", style="green")
104
-
105
- # For each key/value pair, use the recursive renderable.
106
- for key, value in result.items():
107
- rich_renderable = create_rich_renderable(
108
- value, level=0, max_length=self.max_length
109
- )
110
- table.add_row(key, rich_renderable)
111
-
112
- # Wrap the table in a panel.
113
- return Panel(
114
- table,
115
- title="🐤🐧🐓🦆",
116
- title_align="left",
117
- border_style="blue",
118
- padding=(1, 2),
119
- )
120
-
121
- def display_result(
122
- self, result: dict[str, Any], agent_name: str, **kwargs
123
- ) -> None:
124
- """Print an agent's result using Rich formatting."""
125
- console = Console()
126
- panel = self.format_result(result=result, agent_name=agent_name)
127
- # pprint(result) # Optional: Print the raw result with pprint.
128
- console.print(panel)
129
-
130
- def display_data(self, data: dict[str, Any], **kwargs) -> None:
131
- """Print an agent's result using Rich formatting."""
132
- pprint(data)