llm fragments --aliases option, closes #891

This commit is contained in:
Simon Willison 2025-04-06 22:30:27 -07:00
parent df2fce25f0
commit d0255a1eda
4 changed files with 19 additions and 1 deletions

View file

@ -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

View file

@ -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.
```

View file

@ -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}"

View file

@ -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 == ""