|
8 | 8 | import json |
9 | 9 | import os |
10 | 10 | import os.path |
| 11 | +from pathlib import Path |
11 | 12 | import platform |
12 | | -import ssl |
| 13 | +import socket |
13 | 14 | import sys |
14 | 15 | import threading |
15 | 16 | import time |
| 17 | +import typer |
| 18 | +import uuid |
16 | 19 | import webbrowser |
17 | 20 |
|
| 21 | +from holmes.config import Config |
| 22 | +from holmes.core.prompt import build_initial_ask_messages |
| 23 | +from holmes.interactive import run_interactive_loop |
| 24 | +from holmes.plugins.destinations import DestinationType |
| 25 | +from holmes.plugins.interfaces import Issue |
| 26 | +from holmes.plugins.prompts import load_and_render_prompt |
| 27 | +from holmes.utils.console.logging import init_logging |
| 28 | +from holmes.utils.console.result import handle_result |
| 29 | + |
| 30 | + |
18 | 31 | from azext_aks_preview._client_factory import ( |
19 | 32 | CUSTOM_MGMT_AKS_PREVIEW, |
20 | 33 | cf_agent_pools, |
@@ -4333,3 +4346,105 @@ def aks_loadbalancer_rebalance_nodes( |
4333 | 4346 | } |
4334 | 4347 |
|
4335 | 4348 | return aks_loadbalancer_rebalance_internal(managed_clusters_client, parameters) |
| 4349 | + |
| 4350 | + |
| 4351 | +def aks_agent( |
| 4352 | + prompt, |
| 4353 | + api_key, |
| 4354 | + model, |
| 4355 | + interactive, |
| 4356 | + max_steps, |
| 4357 | + echo, |
| 4358 | + show_tool_output, |
| 4359 | + config_file, |
| 4360 | + ): |
| 4361 | + |
| 4362 | + # TODO: make log verbose configurable, currently disbled by []. |
| 4363 | + console = init_logging([]) |
| 4364 | + |
| 4365 | + # Detect and read piped input |
| 4366 | + piped_data = None |
| 4367 | + if not sys.stdin.isatty(): |
| 4368 | + piped_data = sys.stdin.read().strip() |
| 4369 | + if interactive: |
| 4370 | + console.print( |
| 4371 | + "[bold yellow]Interactive mode disabled when reading piped input[/bold yellow]" |
| 4372 | + ) |
| 4373 | + interactive = False |
| 4374 | + |
| 4375 | + config_file = Path(config_file) |
| 4376 | + config = Config.load_from_file( |
| 4377 | + config_file, |
| 4378 | + api_key=api_key, |
| 4379 | + model=model, |
| 4380 | + max_steps=max_steps, |
| 4381 | + ) |
| 4382 | + |
| 4383 | + ai = config.create_console_toolcalling_llm( |
| 4384 | + dal=None |
| 4385 | + ) |
| 4386 | + template_context = { |
| 4387 | + "toolsets": ai.tool_executor.toolsets, |
| 4388 | + "runbooks": config.get_runbook_catalog(), |
| 4389 | + } |
| 4390 | + # TODO: extend the system prompt with AKS context |
| 4391 | + system_prompt= "builtin://generic_ask.jinja2" |
| 4392 | + system_prompt_rendered = load_and_render_prompt(system_prompt, template_context) # type: ignore |
| 4393 | + |
| 4394 | + if not prompt and not interactive and not piped_data: |
| 4395 | + raise typer.BadParameter( |
| 4396 | + "Either the 'prompt' argument must be provided (unless using --interactive mode)." |
| 4397 | + ) |
| 4398 | + |
| 4399 | + # Handle piped data |
| 4400 | + if piped_data: |
| 4401 | + if prompt: |
| 4402 | + # User provided both piped data and a prompt |
| 4403 | + prompt = f"Here's some piped output:\n\n{piped_data}\n\n{prompt}" |
| 4404 | + else: |
| 4405 | + # Only piped data, no prompt - ask what to do with it |
| 4406 | + prompt = f"Here's some piped output:\n\n{piped_data}\n\nWhat can you tell me about this output?" |
| 4407 | + |
| 4408 | + if echo and not interactive and prompt: |
| 4409 | + console.print("[bold yellow]User:[/bold yellow] " + prompt) |
| 4410 | + |
| 4411 | + # TODO: add refresh-toolset to refresh the toolset if it has changed |
| 4412 | + if interactive: |
| 4413 | + run_interactive_loop( |
| 4414 | + ai, |
| 4415 | + console, |
| 4416 | + system_prompt_rendered, |
| 4417 | + prompt, |
| 4418 | + None, |
| 4419 | + None, |
| 4420 | + show_tool_output=show_tool_output, |
| 4421 | + ) |
| 4422 | + return |
| 4423 | + |
| 4424 | + messages = build_initial_ask_messages( |
| 4425 | + console, |
| 4426 | + system_prompt_rendered, |
| 4427 | + prompt, # type: ignore |
| 4428 | + ) |
| 4429 | + |
| 4430 | + response = ai.call(messages) |
| 4431 | + |
| 4432 | + |
| 4433 | + messages = response.messages # type: ignore # Update messages with the full history |
| 4434 | + |
| 4435 | + issue = Issue( |
| 4436 | + id=str(uuid.uuid4()), |
| 4437 | + name=prompt, |
| 4438 | + source_type="holmes-ask", |
| 4439 | + raw={"prompt": prompt, "full_conversation": messages}, |
| 4440 | + source_instance_id=socket.gethostname(), |
| 4441 | + ) |
| 4442 | + handle_result( |
| 4443 | + response, |
| 4444 | + console, |
| 4445 | + DestinationType.CLI, |
| 4446 | + config, |
| 4447 | + issue, |
| 4448 | + show_tool_output, |
| 4449 | + False, |
| 4450 | + ) |
0 commit comments