From d0255a1eda1c0b702baab2317aadec5daf1535d1 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 6 Apr 2025 22:30:27 -0700 Subject: [PATCH] llm fragments --aliases option, closes #891 --- docs/fragments.md | 5 +++++ docs/help.md | 1 + llm/cli.py | 5 ++++- tests/test_fragments_cli.py | 9 +++++++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/fragments.md b/docs/fragments.md index 3f9e9a3..1b611f8 100644 --- a/docs/fragments.md +++ b/docs/fragments.md @@ -97,6 +97,11 @@ You can then use that alias in place of the fragment hash ID: ```bash llm -f mydocs 'How do I access metadata?' ``` +Use `llm fragments --aliases` to see a full list of fragments that have been assigned aliases: +```bash +llm fragments --aliases +``` + (fragments-logs)= ## Viewing fragments in your logs diff --git a/docs/help.md b/docs/help.md index b2cd307..cced702 100644 --- a/docs/help.md +++ b/docs/help.md @@ -691,6 +691,7 @@ Usage: llm fragments list [OPTIONS] Options: -q, --query TEXT Search for fragments matching these strings + --aliases Show only fragments with aliases --json Output as JSON --help Show this message and exit. ``` diff --git a/llm/cli.py b/llm/cli.py index 7600cce..56eecc2 100644 --- a/llm/cli.py +++ b/llm/cli.py @@ -2069,14 +2069,17 @@ def fragments(): multiple=True, help="Search for fragments matching these strings", ) +@click.option("--aliases", is_flag=True, help="Show only fragments with aliases") @click.option("json_", "--json", is_flag=True, help="Output as JSON") -def fragments_list(queries, json_): +def fragments_list(queries, aliases, json_): "List current fragments" db = sqlite_utils.Database(logs_db_path()) migrate(db) params = {} param_count = 0 where_bits = [] + if aliases: + where_bits.append("fragment_aliases.alias is not null") for q in queries: param_count += 1 p = f"p{param_count}" diff --git a/tests/test_fragments_cli.py b/tests/test_fragments_cli.py index 160d06d..750ebc2 100644 --- a/tests/test_fragments_cli.py +++ b/tests/test_fragments_cli.py @@ -7,6 +7,9 @@ def test_fragments_set_show_remove(user_path): runner = CliRunner() with runner.isolated_filesystem(): open("fragment1.txt", "w").write("Hello fragment 1") + + # llm fragments --aliases should return nothing + assert runner.invoke(cli, ["fragments", "list", "--aliases"]).output == "" assert ( runner.invoke(cli, ["fragments", "set", "f1", "fragment1.txt"]).exit_code == 0 @@ -21,6 +24,9 @@ def test_fragments_set_show_remove(user_path): assert result2.exit_code == 0 return yaml.safe_load(result2.output) + # And in llm fragments --aliases + assert "f1" in runner.invoke(cli, ["fragments", "list", "--aliases"]).output + loaded1 = get_list() assert set(loaded1[0].keys()) == { "aliases", @@ -50,3 +56,6 @@ def test_fragments_set_show_remove(user_path): loaded2 = get_list() assert loaded2[0]["aliases"] == [] assert loaded2[0]["content"] == "Hello fragment 1" + + # And --aliases list should be empty + assert runner.invoke(cli, ["fragments", "list", "--aliases"]).output == ""