llm fragments list should order fragments by date

Closes #973
This commit is contained in:
Simon Willison 2025-05-04 12:45:28 -07:00
parent 07ccfbcee5
commit 768a1789a2
2 changed files with 66 additions and 1 deletions

View file

@ -2203,7 +2203,8 @@ def fragments_list(queries, aliases, json_):
fragment_aliases on fragment_aliases.fragment_id = fragments.id
{where}
group by
fragments.id, fragments.hash, fragments.content, fragments.datetime_utc, fragments.source;
fragments.id, fragments.hash, fragments.content, fragments.datetime_utc, fragments.source
order by fragments.datetime_utc
""".format(
where=where
)

View file

@ -1,6 +1,8 @@
from click.testing import CliRunner
from llm.cli import cli
import yaml
import sqlite_utils
import textwrap
def test_fragments_set_show_remove(user_path):
@ -59,3 +61,65 @@ def test_fragments_set_show_remove(user_path):
# And --aliases list should be empty
assert runner.invoke(cli, ["fragments", "list", "--aliases"]).output == ""
def test_fragments_list(user_path):
runner = CliRunner()
with runner.isolated_filesystem():
# This is just to create the database schema
open("fragment1.txt", "w").write("1")
assert (
runner.invoke(cli, ["fragments", "set", "f1", "fragment1.txt"]).exit_code
== 0
)
# Now add the rest directly to the database
db = sqlite_utils.Database(str(user_path / "logs.db"))
db["fragments"].delete_where()
db["fragments"].insert(
{
"content": "1",
"datetime_utc": "2023-10-01T00:00:00Z",
"source": "file1.txt",
"hash": "hash1",
},
)
db["fragments"].insert(
{
"content": "2",
"datetime_utc": "2022-10-01T00:00:00Z",
"source": "file2.txt",
"hash": "hash2",
},
)
db["fragments"].insert(
{
"content": "3",
"datetime_utc": "2024-10-01T00:00:00Z",
"source": "file3.txt",
"hash": "hash3",
},
)
result = runner.invoke(cli, ["fragments", "list"])
assert result.exit_code == 0
assert result.output.strip() == (
textwrap.dedent(
"""
- hash: hash2
aliases: []
datetime_utc: '2022-10-01T00:00:00Z'
source: file2.txt
content: '2'
- hash: hash1
aliases:
- f1
datetime_utc: '2023-10-01T00:00:00Z'
source: file1.txt
content: '1'
- hash: hash3
aliases: []
datetime_utc: '2024-10-01T00:00:00Z'
source: file3.txt
content: '3'
"""
).strip()
)