generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_template.py
More file actions
317 lines (257 loc) · 10.1 KB
/
setup_template.py
File metadata and controls
317 lines (257 loc) · 10.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
"""
Template Setup Script
Run this after cloning the template to customize it for your project.
This script will:
1. Ask for your project details
2. Ask which components you want to keep
3. Rename files and directories
4. Replace placeholder values throughout the codebase
5. Delete unused components
6. Delete itself when done
Usage:
python setup_template.py
"""
import os
import re
import shutil
import sys
COMPONENTS = {
"tool": {
"name": "Tool",
"description": "Add capabilities to agents using the @tool decorator",
"files": ["tool.py"],
"test_files": ["test_tool.py"],
"exports": ["template_tool"],
},
"model": {
"name": "Model Provider",
"description": "Integrate custom LLM APIs",
"files": ["model.py"],
"test_files": ["test_model.py"],
"exports": ["TemplateModel"],
},
"plugin": {
"name": "Plugin",
"description": "Extend agent behavior with hooks and tools in a composable package",
"files": ["plugin.py"],
"test_files": ["test_plugin.py"],
"exports": ["TemplatePlugin"],
},
"session_manager": {
"name": "Session Manager",
"description": "Persist conversations across restarts",
"files": ["session_manager.py"],
"test_files": ["test_session_manager.py"],
"exports": ["TemplateSessionManager"],
},
"conversation_manager": {
"name": "Conversation Manager",
"description": "Control context window and message history",
"files": ["conversation_manager.py"],
"test_files": ["test_conversation_manager.py"],
"exports": ["TemplateConversationManager"],
},
}
def to_snake_case(name: str) -> str:
"""Convert to snake_case (e.g., my_tool)."""
s = re.sub(r"[-\s]+", "_", name)
s = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", s)
s = re.sub(r"([a-z\d])([A-Z])", r"\1_\2", s)
return s.lower()
def to_pascal_case(name: str) -> str:
"""Convert to PascalCase (e.g., MyTool)."""
words = re.split(r"[-_\s]+", name)
return "".join(word.capitalize() for word in words)
def to_kebab_case(name: str) -> str:
"""Convert to kebab-case (e.g., my-tool)."""
s = to_snake_case(name)
return s.replace("_", "-")
def get_input(prompt: str, default: str = "") -> str:
"""Get user input with optional default."""
if default:
result = input(f"{prompt} [{default}]: ").strip()
return result if result else default
return input(f"{prompt}: ").strip()
def select_components() -> list[str]:
"""Prompt user to select which components to keep."""
print("\nWhich components do you want to include?\n")
for i, (key, info) in enumerate(COMPONENTS.items(), 1):
print(f" {i}. {info['name']} - {info['description']}")
print()
selection = get_input("Enter numbers separated by commas (e.g., 1,2)", "1")
selected = []
for num in selection.split(","):
num = num.strip()
if num.isdigit():
idx = int(num) - 1
if 0 <= idx < len(COMPONENTS):
selected.append(list(COMPONENTS.keys())[idx])
if not selected:
print("❌ No valid components selected")
sys.exit(1)
return selected
def replace_in_file(filepath: str, replacements: dict[str, str]) -> None:
"""Replace all occurrences in a file."""
with open(filepath, "r", encoding="utf-8") as f:
content = f.read()
for old, new in replacements.items():
content = content.replace(old, new)
with open(filepath, "w", encoding="utf-8") as f:
f.write(content)
def update_init_file(src_dir: str, selected: list[str], replacements: dict[str, str]) -> None:
"""Update __init__.py to only export selected components."""
init_path = os.path.join(src_dir, "__init__.py")
imports = []
exports = []
for key in selected:
info = COMPONENTS[key]
for export in info["exports"]:
# Apply replacements to get the new name
new_export = export
for old, new in replacements.items():
new_export = new_export.replace(old, new)
module = info["files"][0].replace(".py", "")
# Apply replacements to module name too
new_module = module
for old, new in replacements.items():
new_module = new_module.replace(old, new)
# Get the package name from src_dir (apply replacements for renamed package)
package_name = os.path.basename(src_dir)
for old, new in replacements.items():
package_name = package_name.replace(old, new)
imports.append(f"from {package_name}.{new_module} import {new_export}")
exports.append(f' "{new_export}",')
imports.sort()
exports.sort()
content = f'''"""Strands Package."""
{chr(10).join(imports)}
__all__ = [
{chr(10).join(exports)}
]
'''
with open(init_path, "w", encoding="utf-8") as f:
f.write(content)
def delete_unused_components(src_dir: str, selected: list[str]) -> None:
"""Delete component files that weren't selected."""
for key, info in COMPONENTS.items():
if key not in selected:
# Delete source files
for filename in info["files"]:
filepath = os.path.join(src_dir, filename)
if os.path.exists(filepath):
os.remove(filepath)
print(f" ✓ Removed {filepath}")
# Delete test files
for filename in info["test_files"]:
filepath = os.path.join("tests", filename)
if os.path.exists(filepath):
os.remove(filepath)
print(f" ✓ Removed {filepath}")
def main() -> None:
print("\n🔧 Strands Template Setup\n")
print("This will customize the template for your project.\n")
# Gather information
package_name = get_input("Package name (e.g., 'google', 'aws', 'slack')")
if not package_name:
print("❌ Package name is required")
sys.exit(1)
# Generate variations
snake_name = to_snake_case(package_name)
pascal_name = to_pascal_case(package_name)
kebab_name = to_kebab_case(package_name)
print(f"\n PyPI package: strands-{kebab_name}")
print(f" Module: strands_{snake_name}")
print(f" Classes: {pascal_name}Model, {pascal_name}Hooks, etc.")
# Select components
selected = select_components()
selected_names = [COMPONENTS[k]["name"] for k in selected]
print(f"\n Selected: {', '.join(selected_names)}")
# Optional info
print()
author_name = get_input("Author name", "Your Name")
author_email = get_input("Author email", "your.email@example.com")
github_username = get_input("GitHub username", "yourusername")
description = get_input("Package description", f"Strands Agents components for {package_name}")
# Confirm
print("\n" + "=" * 50)
confirm = get_input("\nProceed with setup? (y/n)", "y")
if confirm.lower() != "y":
print("Setup cancelled.")
sys.exit(0)
print("\n⏳ Setting up project...\n")
# Define replacements
replacements = {
# Package/module names
"strands-template": f"strands-{kebab_name}",
"strands_template": f"strands_{snake_name}",
# Class names
"TemplateModel": f"{pascal_name}Model",
"TemplatePlugin": f"{pascal_name}Plugin",
"TemplateSessionManager": f"{pascal_name}SessionManager",
"TemplateConversationManager": f"{pascal_name}ConversationManager",
# Function names
"template_tool": f"{snake_name}_tool",
# Plugin name
"template-plugin": f"{kebab_name}-plugin",
# Author info
"Your Name": author_name,
"your.email@example.com": author_email,
"yourusername": github_username,
"Your package description": description,
}
# Determine which files to process based on selection
files_to_process = ["pyproject.toml", "README.md"]
for key in selected:
info = COMPONENTS[key]
for filename in info["files"]:
files_to_process.append(f"src/strands_template/{filename}")
for filename in info["test_files"]:
files_to_process.append(f"tests/{filename}")
# Always process __init__.py
files_to_process.append("src/strands_template/__init__.py")
# Process files
for filepath in files_to_process:
if os.path.exists(filepath):
replace_in_file(filepath, replacements)
print(f" ✓ Updated {filepath}")
# Delete unused components
print("\n🗑️ Removing unused components...")
delete_unused_components("src/strands_template", selected)
# Update __init__.py with only selected exports
new_src = f"src/strands_{snake_name}"
update_init_file("src/strands_template", selected, replacements)
# Rename source directory
old_src = "src/strands_template"
if os.path.exists(old_src) and old_src != new_src:
shutil.move(old_src, new_src)
print(f"\n ✓ Renamed {old_src} → {new_src}")
# Clean up
print("\n🧹 Cleaning up...")
# Remove template-specific files that don't belong to the user's project
cleanup_targets = [
"CODE_OF_CONDUCT.md", # template repo's own conduct file
"CONTRIBUTING.md", # template repo's own contributing guide
"NOTICE", # Amazon's copyright notice for the template
]
for target in cleanup_targets:
if os.path.exists(target):
if os.path.isdir(target):
shutil.rmtree(target)
else:
os.remove(target)
print(f" ✓ Removed {target}")
# Remove this setup script
script_path = os.path.abspath(__file__)
if os.path.exists(script_path):
os.remove(script_path)
print(" ✓ Removed setup_template.py")
print("\n✅ Setup complete!\n")
print("Next steps:")
print(" 1. Review the generated files")
print(" 2. Install dev dependencies: pip install -e '.[dev]'")
print(" 3. Run checks: hatch run prepare")
print(" 4. Start implementing your components")
print()
if __name__ == "__main__":
main()