flock-core 0.4.506__py3-none-any.whl → 0.4.508__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,119 +0,0 @@
1
- # src/flock/core/api/ui/utils.py
2
- """Utility functions for the Flock FastHTML UI."""
3
-
4
- import html
5
- from typing import Any
6
-
7
- from flock.core.logging.logging import get_logger
8
- from flock.core.util.input_resolver import (
9
- split_top_level, # Assuming this is the correct location
10
- )
11
-
12
- logger = get_logger("api.ui.utils")
13
-
14
-
15
- def parse_input_spec(input_spec: str) -> list[dict[str, str]]:
16
- """Parses an agent input string into a list of field definitions."""
17
- fields = []
18
- if not input_spec:
19
- return fields
20
- try:
21
- parts = split_top_level(input_spec)
22
- except NameError:
23
- logger.error("split_top_level utility function not found!")
24
- return fields # Or raise?
25
-
26
- for part in parts:
27
- part = part.strip()
28
- if not part:
29
- continue
30
- field_info = {
31
- "name": "",
32
- "type": "str",
33
- "desc": "",
34
- "html_type": "text",
35
- }
36
- name_type_part, *desc_part = part.split("|", 1)
37
- if desc_part:
38
- field_info["desc"] = desc_part[0].strip()
39
- name_part, *type_part = name_type_part.split(":", 1)
40
- field_info["name"] = name_part.strip()
41
- if type_part:
42
- field_info["type"] = type_part[0].strip().lower()
43
-
44
- step = None
45
- field_type_norm = field_info["type"]
46
- if field_type_norm.startswith("int"):
47
- field_info["html_type"] = "number"
48
- elif field_type_norm.startswith("float"):
49
- field_info["html_type"] = "number"
50
- step = "any"
51
- elif field_type_norm.startswith("bool"):
52
- field_info["html_type"] = "checkbox"
53
- elif "list" in field_type_norm or "dict" in field_type_norm:
54
- field_info["html_type"] = "textarea"
55
- field_info["rows"] = 3
56
-
57
- if step:
58
- field_info["step"] = step
59
- if field_info["name"]:
60
- fields.append(field_info)
61
- else:
62
- logger.warning(
63
- f"Could not parse field name from input spec part: '{part}'"
64
- )
65
- return fields
66
-
67
-
68
- def format_result_to_html(
69
- data: Any, level: int = 0, max_level: int = 5, max_str_len: int = 999999
70
- ) -> str:
71
- """Recursively formats a Python object (dict, list, Box, etc.) into an HTML string."""
72
- if hasattr(data, "to_dict") and callable(data.to_dict):
73
- data = data.to_dict()
74
- if level > max_level:
75
- return html.escape(f"[Max recursion depth {max_level} reached]")
76
-
77
- if isinstance(data, dict):
78
- if not data:
79
- return "<i>(empty dictionary)</i>"
80
- table_html = '<table style="width: 100%; border-collapse: collapse; margin-bottom: 10px; border: 1px solid #dee2e6;">'
81
- table_html += '<thead style="background-color: #e9ecef;"><tr><th style="text-align: left; padding: 8px; border-bottom: 2px solid #dee2e6;">Key</th><th style="text-align: left; padding: 8px; border-bottom: 2px solid #dee2e6;">Value</th></tr></thead>'
82
- table_html += "<tbody>"
83
- for key, value in data.items():
84
- escaped_key = html.escape(str(key))
85
- formatted_value = format_result_to_html(
86
- value, level + 1, max_level, max_str_len
87
- ) # Recursive call
88
- table_html += f'<tr><td style="vertical-align: top; padding: 8px; border-top: 1px solid #dee2e6;"><strong>{escaped_key}</strong></td><td style="padding: 8px; border-top: 1px solid #dee2e6;">{formatted_value}</td></tr>'
89
- table_html += "</tbody></table>"
90
- return table_html
91
- elif isinstance(data, (list, tuple)):
92
- if not data:
93
- return "<i>(empty list)</i>"
94
- list_html = '<dl style="margin-left: 20px; padding-left: 0; margin-bottom: 10px;">'
95
- for i, item in enumerate(data):
96
- formatted_item = format_result_to_html(
97
- item, level + 1, max_level, max_str_len
98
- ) # Recursive call
99
- list_html += f'<dt style="font-weight: bold; margin-top: 5px;">Item {i + 1}:</dt><dd style="margin-left: 20px; margin-bottom: 5px;">{formatted_item}</dd>'
100
- list_html += "</dl>"
101
- return list_html
102
- else:
103
- str_value = str(data)
104
- escaped_value = html.escape(str_value)
105
- if len(str_value) > max_str_len:
106
- escaped_value = (
107
- html.escape(str_value[:max_str_len])
108
- + f"... <i style='color: #6c757d;'>({len(str_value) - max_str_len} more chars)</i>"
109
- )
110
-
111
- style = ""
112
- if isinstance(data, bool):
113
- style = "color: #d63384; font-weight: bold;"
114
- elif isinstance(data, (int, float)):
115
- style = "color: #0d6efd;"
116
- elif data is None:
117
- style = "color: #6c757d; font-style: italic;"
118
- escaped_value = "None"
119
- return f'<code style="{style}">{escaped_value}</code>'