generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathrich_interface.py
More file actions
269 lines (248 loc) · 11.1 KB
/
rich_interface.py
File metadata and controls
269 lines (248 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import textwrap
from typing import Any
from rich.console import Console
from rich.markdown import Markdown
from rich.panel import Panel
from rich.progress import Progress
from rich.syntax import Syntax
from rich.table import Table
from rich.text import Text
from rich.tree import Tree
from strands.types.tools import ToolResult, ToolUse
TOOL_SPEC = {
"name": "rich_interface",
"description": textwrap.dedent(
"""
Create rich, interactive console interfaces with various components. Supports panels, tables, trees, markdown,
syntax highlighting, progress bars, and text with proper styling.
IMPORTANT ERROR PREVENTION:
1. Data Structure Errors:
• Missing "interface_definition" object
• Missing "components" array
• Empty components array
• Invalid component structure
2. Component-Specific Errors:
• Table: Mismatched headers and row lengths
• Progress: Invalid total/completed values
• Tree: Missing label or invalid items
• Syntax: Invalid language specification
3. Data Type Errors:
• Numbers provided as strings
• Arrays provided as single values
• Boolean values as strings
• Null/undefined values in required fields
4. Schema Validation:
• Always verify interface_definition exists
• Ensure components array is present
• Check required fields per component
• Validate data types before rendering
Key Features:
1. Component Types:
• panel: Boxed content with optional title and border
• table: Structured data with headers and rows
• progress: Progress bars with description
• tree: Hierarchical data structure
• markdown: Formatted text with MD syntax
• syntax: Code with syntax highlighting
• text: Simple text with optional styling
2. Component Requirements:
• panel: { type: "panel", content: string, title?: string }
• table: { type: "table", headers: string[], rows: string[][], title?: string }
• progress: { type: "progress", description: string, total: number, completed: number }
• tree: { type: "tree", label: string, items: string[] }
• markdown: { type: "markdown", content: string }
• syntax: { type: "syntax", code: string, language: string }
• text: { type: "text", content: string }
3. Error Prevention:
a) Common Errors:
• Missing "components" array
• Invalid component type
• Missing required fields
• Incorrect data types
• Null/undefined values
b) Required Validations:
• Check interface_definition exists
• Verify components array presence
• Validate component type
• Ensure required fields per type
• Verify data structure integrity
4. Best Practices:
a) Structure:
• Organize related components in panels
• Use consistent styling
• Group similar data in tables
• Maintain clear hierarchy
b) Data Handling:
• Pre-validate all data
• Handle empty states gracefully
• Use appropriate data types
• Format data before display
c) Performance:
• Limit components per interface
• Optimize large datasets
• Cache repeated content
• Use appropriate component types
5. Example Usage:
{
"interface_definition": {
"components": [
{
"type": "panel",
"title": "Status",
"content": "System operational"
},
{
"type": "table",
"headers": ["ID", "Status"],
"rows": [["1", "Active"]]
}
]
}
}
6. Troubleshooting:
a) Common Issues:
• Component not rendering
• Incorrect data display
• Formatting problems
• Layout issues
b) Solutions:
• Verify component structure
• Check data types
• Validate required fields
• Test component isolation
"""
).strip(),
"inputSchema": {
"json": {
"type": "object",
"properties": {
"interface_definition": {
"type": "object",
"description": "The interface definition object containing components",
"required": ["components"],
"properties": {
"components": {
"type": "array",
"description": "Array of component definitions",
"items": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"panel",
"table",
"progress",
"tree",
"markdown",
"syntax",
"text",
],
"description": "Type of the component",
},
"title": {
"type": "string",
"description": "Title for panel or table components",
},
"content": {
"type": "string",
"description": "Content for panel, markdown, syntax, or text components",
},
"headers": {
"type": "array",
"items": {"type": "string"},
"description": "Headers for table component",
},
"rows": {
"type": "array",
"items": {
"type": "array",
"items": {"type": "string"},
},
"description": "Rows for table component",
},
"description": {
"type": "string",
"description": "Description for progress component",
},
"total": {
"type": "number",
"description": "Total value for progress component",
},
"completed": {
"type": "number",
"description": "Completed value for progress component",
},
"label": {
"type": "string",
"description": "Label for tree component",
},
"items": {
"type": "array",
"items": {"type": "string"},
"description": "Items for tree component",
},
"language": {
"type": "string",
"description": "Language for syntax component",
},
},
"required": ["type"],
},
}
},
}
},
"required": ["interface_definition"],
}
},
}
def rich_interface(tool: ToolUse, **kwargs: Any) -> ToolResult:
tool_use_id = tool["toolUseId"]
tool_input = tool["input"]
console = Console()
interface_definition = tool_input["interface_definition"]
if "components" not in interface_definition:
return {
"toolUseId": tool_use_id,
"status": "error",
"content": [{"text": "No components defined in the interface definition."}],
}
for component in interface_definition["components"]:
if component["type"] == "panel":
panel = Panel(component.get("content", ""), title=component.get("title", ""))
console.print(panel)
elif component["type"] == "table":
table = Table(title=component.get("title", ""))
for header in component.get("headers", []):
table.add_column(header)
for row in component.get("rows", []):
table.add_row(*row)
console.print(table)
elif component["type"] == "progress":
with Progress() as progress:
task = progress.add_task(component.get("description", ""), total=component.get("total", 100))
progress.update(task, advance=component.get("completed", 0))
elif component["type"] == "tree":
tree = Tree(component.get("label", "Root"))
for item in component.get("items", []):
tree.add(item)
console.print(tree)
elif component["type"] == "markdown":
md = Markdown(component.get("content", ""))
console.print(md)
elif component["type"] == "syntax":
syntax = Syntax(
component.get("code", ""),
component.get("language", "python"),
theme="monokai",
)
console.print(syntax)
elif component["type"] == "text":
text = Text(component.get("content", ""))
console.print(text)
return {
"toolUseId": tool_use_id,
"status": "success",
"content": [{"text": "Interface rendered to terminal"}],
}