-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
210 lines (176 loc) · 7.14 KB
/
cli.py
File metadata and controls
210 lines (176 loc) · 7.14 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
"""
Command Line Interface for the Component Generator.
"""
from pathlib import Path
import typer
from rich.console import Console
from rich.table import Table
from rich.progress import track
from rich.syntax import Syntax
from src.services.component_loader import ComponentLoader
from src.services.vector_store import VectorStore
from src.services.anthropic_service import AnthropicService
app = typer.Typer(help="Component Generator CLI")
console = Console()
@app.command()
def load_analysis(
analysis_file: Path = typer.Argument(
...,
exists=True,
dir_okay=False,
help="JSON file containing component analysis"
)
):
"""Load component analysis into vector store."""
try:
# Initialize services
loader = ComponentLoader()
vector_store = VectorStore()
console.print("\n📚 Loading component analysis...", style="blue")
# Load components from analysis
components = loader.load_analysis(analysis_file)
console.print(f"✅ Found {len(components)} components", style="green")
# Create summary table
table = Table(title="Loaded Components")
table.add_column("Name", style="cyan")
table.add_column("Patterns", style="magenta")
table.add_column("State Complexity", style="blue")
table.add_column("Features", style="green")
# Store components
for component in track(components, description="Storing components..."):
vector_store.add_component(component)
table.add_row(
component.name,
", ".join(component.patterns.type),
str(component.patterns.indicators.state_management.complexity),
", ".join(
feature for feature, enabled in {
"Content Projection": component.patterns.indicators.content_projection,
"Service Usage": component.patterns.indicators.service_usage,
"User Interaction": component.patterns.indicators.user_interaction,
"Form Integration": component.patterns.indicators.form_integration
}.items() if enabled
)
)
console.print(table)
console.print("\n✅ All components stored successfully!", style="green")
except Exception as e:
console.print(f"\n❌ Error: {str(e)}", style="red")
raise typer.Exit(1)
@app.command()
def find_similar(
description: str = typer.Argument(
...,
help="Description of the component to find"
),
count: int = typer.Option(
3,
help="Number of similar components to return"
)
):
"""Find similar components based on description."""
try:
vector_store = VectorStore()
console.print("\n🔍 Searching for similar components...", style="blue")
results = vector_store.find_similar(description, count)
table = Table(title="Similar Components")
table.add_column("Name", style="cyan")
table.add_column("Patterns", style="magenta")
table.add_column("Similarity", style="green")
table.add_column("Features", style="blue")
for result in results:
component = result['component']
table.add_row(
component.name,
", ".join(component.patterns.type),
f"{result['similarity_score']:.2%}",
", ".join(
feature for feature, enabled in {
"Content Projection": component.patterns.indicators.content_projection,
"Service Usage": component.patterns.indicators.service_usage,
"User Interaction": component.patterns.indicators.user_interaction,
"Form Integration": component.patterns.indicators.form_integration
}.items() if enabled
)
)
console.print(table)
except Exception as e:
console.print(f"\n❌ Error: {str(e)}", style="red")
raise typer.Exit(1)
@app.command()
def generate(
description: str = typer.Argument(
...,
help="Description of the component to generate using our existing components"
),
output_dir: Path = typer.Option(
Path("generated"),
help="Directory to save generated component"
),
num_references: int = typer.Option(
5, # Increased from 3 to get more component references
help="Number of similar components to use as reference"
)
):
"""Generate a new component composition using our existing components."""
try:
# Initialize services
vector_store = VectorStore()
anthropic = AnthropicService()
console.print(
"\n🔍 Finding relevant components to use...", style="blue")
similar = vector_store.find_similar(description, num_references)
# Show reference components
table = Table(title="Components Available for Use")
table.add_column("Component", style="cyan")
table.add_column("Type", style="magenta")
table.add_column("Inputs", style="green")
table.add_column("Template Patterns", style="blue")
for result in similar:
component = result['component']
table.add_row(
component.name,
", ".join(component.patterns.type),
", ".join(component.patterns.code_markers.inputs or ["None"]),
", ".join(
component.patterns.code_markers.template_patterns or ["None"])
)
console.print(table)
console.print("\n🤖 Generating component composition...", style="blue")
# Generate component using updated anthropic service
generated_code = anthropic.generate_component(
description,
[result['component'] for result in similar]
)
# Create output directory
output_dir.mkdir(parents=True, exist_ok=True)
# Generate component name from description
component_name = "_".join(
word.lower() for word in description.split()[:3]
if word.isalnum()
)
component_file = output_dir / f"{component_name}.component.ts"
# Save generated code
component_file.write_text(generated_code)
# Show generated code with syntax highlighting
console.print("\n✨ Generated Component:", style="green")
syntax = Syntax(
generated_code,
"typescript",
theme="monokai",
line_numbers=True
)
console.print(syntax)
# Add usage examples
console.print("\n📝 Example Usage:", style="blue")
console.print("Add this to your module:", style="cyan")
console.print("```typescript")
console.print(
f"import {{ {component_name.title()}Component }} from './generated/{component_name}.component';")
console.print("```")
console.print(f"\n💾 Saved to: {component_file}", style="blue")
except Exception as e:
console.print(f"\n❌ Error: {str(e)}", style="red")
raise typer.Exit(1)
if __name__ == "__main__":
app()