|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the terms described in the LICENSE file in |
| 5 | +# the root directory of this source tree. |
| 6 | + |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +import click |
| 10 | +from rich.console import Console |
| 11 | +from rich.table import Table |
| 12 | + |
| 13 | +from ..common.utils import handle_client_errors |
| 14 | + |
| 15 | + |
| 16 | +@click.group() |
| 17 | +def toolgroups(): |
| 18 | + """Query details about available toolgroups on Llama Stack distribution.""" |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +@click.command( |
| 23 | + name="list", help="Show available llama toolgroups at distribution endpoint" |
| 24 | +) |
| 25 | +@click.pass_context |
| 26 | +@handle_client_errors("list toolgroups") |
| 27 | +def list_toolgroups(ctx): |
| 28 | + client = ctx.obj["client"] |
| 29 | + console = Console() |
| 30 | + |
| 31 | + headers = ["identifier", "provider_id", "args", "mcp_endpoint"] |
| 32 | + response = client.toolgroups.list() |
| 33 | + if response: |
| 34 | + table = Table() |
| 35 | + for header in headers: |
| 36 | + table.add_column(header) |
| 37 | + |
| 38 | + for item in response: |
| 39 | + row = [str(getattr(item, header)) for header in headers] |
| 40 | + table.add_row(*row) |
| 41 | + console.print(table) |
| 42 | + |
| 43 | + |
| 44 | +@click.command(name="get") |
| 45 | +@click.argument("toolgroup_id") |
| 46 | +@click.pass_context |
| 47 | +@handle_client_errors("get toolgroup details") |
| 48 | +def get_toolgroup(ctx, toolgroup_id: str): |
| 49 | + """Show available llama toolgroups at distribution endpoint""" |
| 50 | + client = ctx.obj["client"] |
| 51 | + console = Console() |
| 52 | + |
| 53 | + toolgroups_get_response = client.tools.list() |
| 54 | + # filter response to only include provided toolgroup_id |
| 55 | + toolgroups_get_response = [ |
| 56 | + toolgroup |
| 57 | + for toolgroup in toolgroups_get_response |
| 58 | + if toolgroup.toolgroup_id == toolgroup_id |
| 59 | + ] |
| 60 | + if len(toolgroups_get_response) == 0: |
| 61 | + console.print( |
| 62 | + f"Toolgroup {toolgroup_id} is not found at distribution endpoint. " |
| 63 | + "Please ensure endpoint is serving specified toolgroup.", |
| 64 | + style="bold red", |
| 65 | + ) |
| 66 | + return |
| 67 | + |
| 68 | + headers = sorted(toolgroups_get_response[0].__dict__.keys()) |
| 69 | + table = Table() |
| 70 | + for header in headers: |
| 71 | + table.add_column(header) |
| 72 | + |
| 73 | + for toolgroup in toolgroups_get_response: |
| 74 | + row = [str(getattr(toolgroup, header)) for header in headers] |
| 75 | + table.add_row(*row) |
| 76 | + console.print(table) |
| 77 | + |
| 78 | + |
| 79 | +@click.command( |
| 80 | + name="register", help="Register a new toolgroup at distribution endpoint" |
| 81 | +) |
| 82 | +@click.argument("toolgroup_id") |
| 83 | +@click.option("--provider-id", help="Provider ID for the toolgroup", default=None) |
| 84 | +@click.option("--provider-toolgroup-id", help="Provider's toolgroup ID", default=None) |
| 85 | +@click.option("--mcp-config", help="JSON mcp_config for the toolgroup", default=None) |
| 86 | +@click.option("--args", help="JSON args for the toolgroup", default=None) |
| 87 | +@click.pass_context |
| 88 | +@handle_client_errors("register toolgroup") |
| 89 | +def register_toolgroup( |
| 90 | + ctx, |
| 91 | + toolgroup_id: str, |
| 92 | + provider_id: Optional[str], |
| 93 | + provider_toolgroup_id: Optional[str], |
| 94 | + mcp_config: Optional[str], |
| 95 | + args: Optional[str], |
| 96 | +): |
| 97 | + """Register a new toolgroup at distribution endpoint""" |
| 98 | + client = ctx.obj["client"] |
| 99 | + console = Console() |
| 100 | + |
| 101 | + response = client.toolgroups.register( |
| 102 | + toolgroup_id=toolgroup_id, |
| 103 | + provider_id=provider_id, |
| 104 | + args=args, |
| 105 | + mcp_config=mcp_config, |
| 106 | + ) |
| 107 | + if response: |
| 108 | + console.print( |
| 109 | + f"[green]Successfully registered toolgroup {toolgroup_id}[/green]" |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +@click.command( |
| 114 | + name="unregister", help="Unregister a toolgroup from distribution endpoint" |
| 115 | +) |
| 116 | +@click.argument("toolgroup_id") |
| 117 | +@click.pass_context |
| 118 | +@handle_client_errors("unregister toolgroup") |
| 119 | +def unregister_toolgroup(ctx, toolgroup_id: str): |
| 120 | + client = ctx.obj["client"] |
| 121 | + console = Console() |
| 122 | + |
| 123 | + response = client.toolgroups.unregister(tool_group_id=toolgroup_id) |
| 124 | + if response: |
| 125 | + console.print(f"[green]Successfully deleted toolgroup {toolgroup_id}[/green]") |
| 126 | + |
| 127 | + |
| 128 | +# Register subcommands |
| 129 | +toolgroups.add_command(list_toolgroups) |
| 130 | +toolgroups.add_command(get_toolgroup) |
| 131 | +toolgroups.add_command(register_toolgroup) |
| 132 | +toolgroups.add_command(unregister_toolgroup) |
0 commit comments