2026-04-21 17:42:37 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-04-21 14:32:10 +02:00
|
|
|
import argparse
|
2026-04-21 17:42:37 +02:00
|
|
|
from dataclasses import dataclass
|
2026-04-21 14:32:10 +02:00
|
|
|
|
|
|
|
|
|
2026-04-21 17:42:37 +02:00
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class ArgumentSpec:
|
|
|
|
|
name: str
|
|
|
|
|
help: str
|
2026-04-21 21:26:40 +02:00
|
|
|
required: bool = False
|
|
|
|
|
metavar: str | None = None
|
2026-04-21 14:32:10 +02:00
|
|
|
|
|
|
|
|
|
2026-04-21 17:42:37 +02:00
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class CommandSpec:
|
|
|
|
|
name: str
|
|
|
|
|
aliases: tuple[str, ...]
|
|
|
|
|
help: str
|
|
|
|
|
arguments: tuple[ArgumentSpec, ...] = ()
|
2026-04-21 14:32:10 +02:00
|
|
|
|
|
|
|
|
|
2026-04-21 17:42:37 +02:00
|
|
|
COMMAND_SPECS: tuple[CommandSpec, ...] = (
|
|
|
|
|
CommandSpec(
|
|
|
|
|
name="list-collections",
|
|
|
|
|
aliases=("lc",),
|
|
|
|
|
help="List all collections stored in the local Chroma database.",
|
|
|
|
|
),
|
|
|
|
|
CommandSpec(
|
|
|
|
|
name="create-collection",
|
|
|
|
|
aliases=("cc",),
|
|
|
|
|
help="Create a collection in the local Chroma database.",
|
2026-04-21 17:55:11 +02:00
|
|
|
arguments=(ArgumentSpec("collection", "Name of the collection to create."),),
|
2026-04-21 17:42:37 +02:00
|
|
|
),
|
|
|
|
|
CommandSpec(
|
|
|
|
|
name="delete-collection",
|
|
|
|
|
aliases=("dc",),
|
|
|
|
|
help="Delete a collection from the local Chroma database.",
|
2026-04-21 17:55:11 +02:00
|
|
|
arguments=(ArgumentSpec("collection", "Name of the collection to delete."),),
|
2026-04-21 17:42:37 +02:00
|
|
|
),
|
|
|
|
|
CommandSpec(
|
|
|
|
|
name="count",
|
|
|
|
|
aliases=("co",),
|
2026-04-21 14:45:01 +02:00
|
|
|
help="Count records in a collection from the local Chroma database.",
|
2026-04-21 17:55:11 +02:00
|
|
|
arguments=(ArgumentSpec("collection", "Name of the collection to count."),),
|
2026-04-21 17:42:37 +02:00
|
|
|
),
|
|
|
|
|
CommandSpec(
|
|
|
|
|
name="add-data",
|
|
|
|
|
aliases=("ad",),
|
2026-04-22 17:03:01 +02:00
|
|
|
help=(
|
|
|
|
|
"Chunk, embed, and add a file to a collection in the local Chroma database."
|
|
|
|
|
),
|
2026-04-21 17:42:37 +02:00
|
|
|
arguments=(
|
|
|
|
|
ArgumentSpec("collection", "Name of the target collection."),
|
2026-04-21 17:55:11 +02:00
|
|
|
ArgumentSpec(
|
|
|
|
|
"file", "Path to the file to chunk and add to the collection."
|
|
|
|
|
),
|
2026-04-21 17:42:37 +02:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
CommandSpec(
|
|
|
|
|
name="query",
|
|
|
|
|
aliases=("q",),
|
|
|
|
|
help="Query a collection with the provided text.",
|
|
|
|
|
arguments=(
|
|
|
|
|
ArgumentSpec("collection", "Name of the target collection."),
|
|
|
|
|
ArgumentSpec("query_text", "The text to query."),
|
|
|
|
|
),
|
|
|
|
|
),
|
2026-04-21 21:26:40 +02:00
|
|
|
CommandSpec(
|
|
|
|
|
name="delete",
|
|
|
|
|
aliases=("del",),
|
|
|
|
|
help="Delete records from a collection using a metadata filter.",
|
|
|
|
|
arguments=(
|
|
|
|
|
ArgumentSpec("collection", "Name of the target collection."),
|
|
|
|
|
ArgumentSpec(
|
|
|
|
|
"--where",
|
|
|
|
|
"Metadata filter in the format <condition>=<value>.",
|
|
|
|
|
required=True,
|
|
|
|
|
metavar="CONDITION=VALUE",
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2026-04-21 17:42:37 +02:00
|
|
|
)
|
|
|
|
|
|
2026-04-21 17:13:43 +02:00
|
|
|
|
2026-04-21 17:42:37 +02:00
|
|
|
def _add_command(
|
|
|
|
|
subparsers: argparse._SubParsersAction[argparse.ArgumentParser],
|
|
|
|
|
command: CommandSpec,
|
|
|
|
|
) -> None:
|
|
|
|
|
subparser = subparsers.add_parser(
|
|
|
|
|
command.name,
|
|
|
|
|
aliases=list(command.aliases),
|
|
|
|
|
help=command.help,
|
|
|
|
|
description=command.help,
|
2026-04-21 17:13:43 +02:00
|
|
|
)
|
2026-04-21 17:42:37 +02:00
|
|
|
|
|
|
|
|
for argument in command.arguments:
|
2026-04-21 21:26:40 +02:00
|
|
|
if argument.name.startswith("-"):
|
2026-04-22 17:03:01 +02:00
|
|
|
subparser.add_argument(
|
|
|
|
|
argument.name,
|
|
|
|
|
help=argument.help,
|
|
|
|
|
metavar=argument.metavar,
|
|
|
|
|
required=argument.required,
|
|
|
|
|
)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
subparser.add_argument(
|
|
|
|
|
argument.name,
|
|
|
|
|
help=argument.help,
|
|
|
|
|
metavar=argument.metavar,
|
|
|
|
|
)
|
2026-04-21 17:42:37 +02:00
|
|
|
|
|
|
|
|
subparser.set_defaults(command=command.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_parser() -> argparse.ArgumentParser:
|
|
|
|
|
parser = argparse.ArgumentParser(description="Inspect local Chroma collections.")
|
|
|
|
|
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
|
|
|
|
|
|
|
|
for command in COMMAND_SPECS:
|
|
|
|
|
_add_command(subparsers, command)
|
2026-04-21 15:28:20 +02:00
|
|
|
|
2026-04-21 14:32:10 +02:00
|
|
|
return parser
|