|
1 | 1 | """ |
2 | 2 | Tools command group for PraisonAI CLI. |
3 | 3 |
|
4 | | -Provides tool management commands. |
| 4 | +Provides tool management commands including: |
| 5 | +- List available tools from all sources |
| 6 | +- Validate YAML tool references |
| 7 | +- Show tool information |
5 | 8 | """ |
6 | 9 |
|
| 10 | +from typing import Optional |
| 11 | + |
7 | 12 | import typer |
| 13 | +from rich.console import Console |
| 14 | +from rich.table import Table |
8 | 15 |
|
9 | | -app = typer.Typer(help="Tool management") |
| 16 | +app = typer.Typer(help="Tool management and discovery") |
| 17 | +console = Console() |
10 | 18 |
|
11 | 19 |
|
12 | 20 | @app.command("list") |
13 | 21 | def tools_list( |
| 22 | + source: Optional[str] = typer.Option( |
| 23 | + None, "--source", "-s", |
| 24 | + help="Filter by source: builtin, local, external" |
| 25 | + ), |
14 | 26 | verbose: bool = typer.Option(False, "--verbose", "-v", help="Show detailed info"), |
15 | 27 | ): |
16 | | - """List available tools.""" |
17 | | - from praisonai.cli.main import PraisonAI |
18 | | - import sys |
| 28 | + """List all available tools that can be used in YAML files. |
| 29 | + |
| 30 | + Shows tools from: |
| 31 | + - Built-in tools (praisonaiagents.tools) |
| 32 | + - Local tools.py (if present) |
| 33 | + - External tools (praisonai-tools package) |
| 34 | + """ |
| 35 | + from praisonai.tool_resolver import ToolResolver |
| 36 | + |
| 37 | + resolver = ToolResolver() |
| 38 | + available = resolver.list_available() |
| 39 | + |
| 40 | + if not available: |
| 41 | + console.print("[yellow]No tools available.[/yellow]") |
| 42 | + return |
| 43 | + |
| 44 | + # Categorize tools |
| 45 | + builtin_tools = {} |
| 46 | + local_tools = {} |
| 47 | + external_tools = {} |
| 48 | + |
| 49 | + for name, desc in available.items(): |
| 50 | + if "Local tool" in desc: |
| 51 | + local_tools[name] = desc |
| 52 | + elif "praisonai-tools" in desc: |
| 53 | + external_tools[name] = desc |
| 54 | + else: |
| 55 | + builtin_tools[name] = desc |
19 | 56 |
|
20 | | - argv = ['tools', 'list'] |
| 57 | + # Filter by source if specified |
| 58 | + if source == "builtin": |
| 59 | + available = builtin_tools |
| 60 | + elif source == "local": |
| 61 | + available = local_tools |
| 62 | + elif source == "external": |
| 63 | + available = external_tools |
| 64 | + |
| 65 | + # Create table |
| 66 | + table = Table(title="Available Tools", show_header=True, header_style="bold cyan") |
| 67 | + table.add_column("Tool Name", style="green") |
| 68 | + table.add_column("Source", style="blue") |
21 | 69 | if verbose: |
22 | | - argv.append('--verbose') |
| 70 | + table.add_column("Description", style="dim") |
| 71 | + |
| 72 | + # Add rows |
| 73 | + for name in sorted(available.keys()): |
| 74 | + desc = available[name] |
| 75 | + if "Local tool" in desc: |
| 76 | + src = "local" |
| 77 | + elif "praisonai-tools" in desc: |
| 78 | + src = "external" |
| 79 | + else: |
| 80 | + src = "builtin" |
| 81 | + |
| 82 | + if verbose: |
| 83 | + table.add_row(name, src, desc[:60] + "..." if len(desc) > 60 else desc) |
| 84 | + else: |
| 85 | + table.add_row(name, src) |
| 86 | + |
| 87 | + console.print(table) |
| 88 | + console.print(f"\n[dim]Total: {len(available)} tools[/dim]") |
| 89 | + |
| 90 | + if not source: |
| 91 | + console.print(f"[dim] Built-in: {len(builtin_tools)} | Local: {len(local_tools)} | External: {len(external_tools)}[/dim]") |
| 92 | + |
| 93 | + |
| 94 | +@app.command("validate") |
| 95 | +def tools_validate( |
| 96 | + yaml_file: str = typer.Argument("agents.yaml", help="YAML file to validate"), |
| 97 | +): |
| 98 | + """Validate that all tools in a YAML file can be resolved. |
| 99 | + |
| 100 | + Checks that every tool name in the YAML can be found in: |
| 101 | + - Local tools.py |
| 102 | + - Built-in tools (praisonaiagents.tools) |
| 103 | + - External tools (praisonai-tools) |
| 104 | + """ |
| 105 | + import yaml |
| 106 | + from pathlib import Path |
| 107 | + from praisonai.tool_resolver import ToolResolver |
23 | 108 |
|
24 | | - original_argv = sys.argv |
25 | | - sys.argv = ['praisonai'] + argv |
| 109 | + yaml_path = Path(yaml_file) |
| 110 | + if not yaml_path.exists(): |
| 111 | + console.print(f"[red]Error: File not found: {yaml_file}[/red]") |
| 112 | + raise typer.Exit(1) |
26 | 113 |
|
27 | 114 | try: |
28 | | - praison = PraisonAI() |
29 | | - praison.main() |
30 | | - except SystemExit: |
31 | | - pass |
32 | | - finally: |
33 | | - sys.argv = original_argv |
| 115 | + with open(yaml_path) as f: |
| 116 | + config = yaml.safe_load(f) |
| 117 | + except Exception as e: |
| 118 | + console.print(f"[red]Error parsing YAML: {e}[/red]") |
| 119 | + raise typer.Exit(1) |
| 120 | + |
| 121 | + resolver = ToolResolver() |
| 122 | + missing = resolver.validate_yaml_tools(config) |
| 123 | + |
| 124 | + if not missing: |
| 125 | + console.print(f"[green]✓ All tools in {yaml_file} are valid![/green]") |
| 126 | + |
| 127 | + # Show which tools were found |
| 128 | + roles = config.get('roles', config.get('agents', {})) |
| 129 | + all_tools = set() |
| 130 | + for role_config in roles.values(): |
| 131 | + if isinstance(role_config, dict): |
| 132 | + all_tools.update(role_config.get('tools', [])) |
| 133 | + |
| 134 | + if all_tools: |
| 135 | + console.print(f"[dim]Tools found: {', '.join(sorted(all_tools))}[/dim]") |
| 136 | + else: |
| 137 | + console.print(f"[red]✗ Missing tools in {yaml_file}:[/red]") |
| 138 | + for tool in missing: |
| 139 | + console.print(f" [red]• {tool}[/red]") |
| 140 | + |
| 141 | + console.print("\n[yellow]Hint: Run 'praisonai tools list' to see available tools.[/yellow]") |
| 142 | + raise typer.Exit(1) |
34 | 143 |
|
35 | 144 |
|
36 | 145 | @app.command("info") |
37 | 146 | def tools_info( |
38 | 147 | name: str = typer.Argument(..., help="Tool name"), |
39 | 148 | ): |
40 | | - """Show tool information.""" |
41 | | - from praisonai.cli.main import PraisonAI |
42 | | - import sys |
| 149 | + """Show detailed information about a tool.""" |
| 150 | + from praisonai.tool_resolver import ToolResolver |
43 | 151 |
|
44 | | - argv = ['tools', 'info', name] |
| 152 | + resolver = ToolResolver() |
| 153 | + tool = resolver.resolve(name) |
45 | 154 |
|
46 | | - original_argv = sys.argv |
47 | | - sys.argv = ['praisonai'] + argv |
| 155 | + if tool is None: |
| 156 | + console.print(f"[red]Tool '{name}' not found.[/red]") |
| 157 | + console.print("[yellow]Hint: Run 'praisonai tools list' to see available tools.[/yellow]") |
| 158 | + raise typer.Exit(1) |
48 | 159 |
|
| 160 | + console.print(f"\n[bold green]{name}[/bold green]") |
| 161 | + console.print("-" * 40) |
| 162 | + |
| 163 | + # Get docstring |
| 164 | + doc = getattr(tool, '__doc__', None) |
| 165 | + if doc: |
| 166 | + console.print(f"[dim]{doc}[/dim]") |
| 167 | + |
| 168 | + # Get signature if possible |
| 169 | + import inspect |
49 | 170 | try: |
50 | | - praison = PraisonAI() |
51 | | - praison.main() |
52 | | - except SystemExit: |
| 171 | + sig = inspect.signature(tool) |
| 172 | + console.print(f"\n[cyan]Signature:[/cyan] {name}{sig}") |
| 173 | + except (ValueError, TypeError): |
53 | 174 | pass |
54 | | - finally: |
55 | | - sys.argv = original_argv |
| 175 | + |
| 176 | + # Show source |
| 177 | + available = resolver.list_available() |
| 178 | + if name in available: |
| 179 | + desc = available[name] |
| 180 | + if "Local tool" in desc: |
| 181 | + console.print("\n[blue]Source:[/blue] Local tools.py") |
| 182 | + elif "praisonai-tools" in desc: |
| 183 | + console.print("\n[blue]Source:[/blue] praisonai-tools package") |
| 184 | + else: |
| 185 | + console.print("\n[blue]Source:[/blue] praisonaiagents.tools (built-in)") |
56 | 186 |
|
57 | 187 |
|
58 | 188 | @app.command("test") |
59 | 189 | def tools_test( |
60 | 190 | name: str = typer.Argument(..., help="Tool name to test"), |
61 | 191 | ): |
62 | | - """Test a tool.""" |
63 | | - from praisonai.cli.main import PraisonAI |
64 | | - import sys |
| 192 | + """Test a tool with a simple invocation.""" |
| 193 | + from praisonai.tool_resolver import ToolResolver |
65 | 194 |
|
66 | | - argv = ['tools', 'test', name] |
| 195 | + resolver = ToolResolver() |
| 196 | + tool = resolver.resolve(name) |
67 | 197 |
|
68 | | - original_argv = sys.argv |
69 | | - sys.argv = ['praisonai'] + argv |
| 198 | + if tool is None: |
| 199 | + console.print(f"[red]Tool '{name}' not found.[/red]") |
| 200 | + raise typer.Exit(1) |
70 | 201 |
|
71 | | - try: |
72 | | - praison = PraisonAI() |
73 | | - praison.main() |
74 | | - except SystemExit: |
75 | | - pass |
76 | | - finally: |
77 | | - sys.argv = original_argv |
| 202 | + console.print(f"[green]✓ Tool '{name}' resolved successfully![/green]") |
| 203 | + console.print(f"[dim]Type: {type(tool).__name__}[/dim]") |
| 204 | + console.print(f"[dim]Callable: {callable(tool)}[/dim]") |
0 commit comments