-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcli.py
More file actions
197 lines (152 loc) · 6.57 KB
/
Copy pathcli.py
File metadata and controls
197 lines (152 loc) · 6.57 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
from pathlib import Path
import click
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt
from devops_agent.core.master_agent import execute_master_agent
from devops_agent.core.log_analysis_agent import execute_log_analysis_agent
console = Console()
@click.group()
@click.version_option(version="0.1.0")
def cli():
"""DevOps Agent - Your AI-powered DevOps assistant"""
pass
def default_provider() -> str:
return "openai"
@cli.command()
@click.option('--log-file', type=click.Path(exists=True), help='Path to log file to analyze')
@click.option('--provider', type=str, help='Configure the agent with one of the enterprise grade providers like OpenAI, Anthropic, Gemini')
@click.option('--query', type=str, help='Query to ask the DevOps agent')
@click.option('--output', type=click.Path(), help='Output file path (optional)')
@click.option('--format', type=click.Choice(['text', 'json', 'markdown']), default='text', help='Output format')
@click.option('--interactive', '-i', is_flag=True, help='Run in interactive mode')
def run(log_file, provider, query, output, format, interactive):
"""Run the DevOps agent with specified options"""
if not provider:
console.print("[yellow]No provider specified, defaulting to openai[/yellow]")
provider = default_provider()
# Interactive mode
if interactive:
run_interactive_mode(provider, output, format)
return
# Single query mode (original behavior)
if not log_file and not query:
console.print("[red]Error: You must provide either --log-file, --query, or use --interactive mode[/red]")
raise click.Abort()
if log_file and query:
console.print("[red]Error: Cannot use both --log-file and --query simultaneously[/red]")
raise click.Abort()
console.print(Panel.fit(
"[bold cyan]DevOps Agent[/bold cyan]\n[dim]Initializing...[/dim]",
border_style="cyan"
))
if log_file:
console.print(f"[yellow]Analyzing log file:[/yellow] {log_file}")
try:
file_path = Path(__file__).parent.joinpath(log_file)
response = execute_log_analysis_agent(provider=provider, log_file=file_path)
console.print(Panel.fit(
f"[bold yellow]Assistant:[/bold yellow] [dim]{response}[/dim]",
border_style="yellow"
))
if output:
save_to_file(output, query, response, format)
console.print(f"\n[dim]Response saved to {output}[/dim]")
except Exception as e:
console.print(f"\n[red]Error:[/red] {str(e)}")
if query:
process_query(provider, query, output, format)
def run_interactive_mode(provider: str, output: str = None, format: str = 'text'):
"""Run the agent in interactive mode with continuous conversation"""
console.print(Panel.fit(
"[bold cyan]DevOps Agent - Interactive Mode[/bold cyan]\n"
"[dim]Type your questions or commands.[/dim]\n"
"[dim]Type 'quit', 'exit', or 'bye' to exit.[/dim]",
border_style="cyan"
))
# Main interactive loop
while True:
try:
# Get user input
user_input = Prompt.ask("\n[bold green]You[/bold green]")
# Check for exit commands
if user_input.lower().strip() in ['quit', 'exit', 'bye', 'q']:
console.print("\n[yellow]Goodbye! 👋[/yellow]")
break
# Skip empty inputs
if not user_input.strip():
continue
# Process the query
console.print(Panel.fit(
"[bold cyan]DevOps Team[/bold cyan] [dim]Thinking...[/dim]",
border_style="cyan"
))
try:
response = execute_master_agent(provider=provider, user_query=user_input)
console.print(Panel.fit(
f"[bold yellow]Assistant:[/bold yellow] [dim]{response}[/dim]",
border_style="yellow"
))
# Save to output file if specified
if output:
save_to_file(output, user_input, response, format)
console.print(f"\n[dim]Response saved to {output}[/dim]")
except Exception as e:
console.print(f"\n[red]Error processing query:[/red] {str(e)}")
except KeyboardInterrupt:
console.print("\n\n[yellow]Interrupted. Type 'quit' to exit or continue with your next question.[/yellow]")
continue
except EOFError:
console.print("\n[yellow]Goodbye! 👋[/yellow]")
break
def process_query(provider: str, query: str, output: str = None, format: str = 'text'):
"""Process a single query"""
console.print(f"[yellow]Processing query:[/yellow] {query}")
console.print(Panel.fit(
"[bold cyan]DevOps Agent[/bold cyan] [dim]Thinking...[/dim]",
border_style="cyan"
))
try:
response = execute_master_agent(provider=provider, user_query=query)
console.print(Panel.fit(
f"[bold yellow]Assistant:[/bold yellow] [dim]{response}[/dim]",
border_style="yellow"
))
if output:
save_to_file(output, query, response, format)
console.print(f"\n[dim]Response saved to {output}[/dim]")
except Exception as e:
console.print(f"\n[red]Error:[/red] {str(e)}")
def save_to_file(filepath: str, query: str, response: str, format: str):
"""Save the conversation to a file"""
import json
from pathlib import Path
output_path = Path(filepath)
if format == 'json':
content = json.dumps({
"query": query,
"response": response
}, indent=2)
elif format == 'markdown':
content = f"# Query\n\n{query}\n\n# Response\n\n{response}\n"
else: # text
content = f"Query: {query}\n\nResponse: {response}\n"
# Append mode for interactive sessions
mode = 'a' if output_path.exists() else 'w'
with open(output_path, mode) as f:
if mode == 'a':
f.write("\n" + "="*50 + "\n\n")
f.write(content)
@cli.command()
def config():
"""Configure the DevOps agent"""
console.print("[yellow]Configuration interface will be implemented here[/yellow]")
@cli.command()
@click.argument('template_type', type=click.Choice(['terraform', 'kubernetes', 'docker']))
def template(template_type):
"""Generate templates for various DevOps tools"""
console.print(f"[yellow]Generating {template_type} template...[/yellow]")
def main():
cli()
if __name__ == '__main__':
main()