Skip to content

Commit 2d18ae3

Browse files
committed
fixed workflow and added tests for compile_llms_txt.py, converted connection to a private function
1 parent 5be3f58 commit 2d18ae3

7 files changed

Lines changed: 111 additions & 102 deletions

File tree

.github/workflows/compile-llms-txt.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Compile llms.txt
2020
run: |
2121
cd docs
22-
python compile_llms.py
22+
python compile_llms_txt.py
2323
- name: Commit and push changes
2424
run: |
2525
git config --local user.email "action@github.com"

agentstack/_tools/py_sql/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import psycopg2
33
from typing import Dict, Any
44

5-
def get_connection():
5+
def _get_connection():
66
"""Get PostgreSQL database connection"""
77
return psycopg2.connect(
88
dbname=os.getenv('POSTGRES_DB'),
@@ -18,7 +18,7 @@ def get_schema() -> Dict[str, Any]:
1818
Returns a dictionary containing the database schema.
1919
"""
2020
try:
21-
conn = get_connection()
21+
conn = _get_connection()
2222
cursor = conn.cursor()
2323

2424
# Query to get all tables in the current schema
@@ -63,7 +63,7 @@ def execute_query(query: str) -> list:
6363
List of query results
6464
"""
6565
try:
66-
conn = get_connection()
66+
conn = _get_connection()
6767
cursor = conn.cursor()
6868

6969
# Execute the query

agentstack/_tools/py_sql/config.json

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,11 @@
33
"url": "https://pypi.org/project/psycopg2/",
44
"category": "database",
55
"env": {
6-
"POSTGRES_DB": {
7-
"description": "PostgreSQL database name",
8-
"required": true
9-
},
10-
"POSTGRES_USER": {
11-
"description": "PostgreSQL username",
12-
"required": true
13-
},
14-
"POSTGRES_PASSWORD": {
15-
"description": "PostgreSQL password",
16-
"required": true
17-
},
18-
"POSTGRES_HOST": {
19-
"description": "PostgreSQL host address",
20-
"required": true,
21-
"default": "localhost"
22-
},
23-
"POSTGRES_PORT": {
24-
"description": "PostgreSQL port number",
25-
"required": true,
26-
"default": "5432"
27-
}
6+
"POSTGRES_DB": "...",
7+
"POSTGRES_USER": "...",
8+
"POSTGRES_PASSWORD": "...",
9+
"POSTGRES_HOST": "...",
10+
"POSTGRES_PORT": "..."
2811
},
2912
"dependencies": [
3013
"psycopg2-binary>=2.9.9"

agentstack/_tools/py_sql/test.db

-12 KB
Binary file not shown.

agentstack/_tools/py_sql/test.py

Lines changed: 0 additions & 66 deletions
This file was deleted.

docs/compile_llms_txt.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
import os
2+
from pathlib import Path
23

34
def compile_llms_txt():
4-
# Get the docs directory path (where this script is located)
5-
docs_dir = os.path.dirname(os.path.abspath(__file__))
5+
# Get the current working directory
6+
current_dir = Path(os.getcwd())
67
content = ''
8+
79
# Define names of directories and files to exclude
810
excluded_names = {'tool'}
911

10-
# Change to docs directory
11-
os.chdir(docs_dir)
12-
1312
for root, _, files in os.walk('.'):
1413
# Get the last part of the current directory
1514
current_dir = os.path.basename(root)
1615
if current_dir in excluded_names:
1716
continue
1817

1918
for file in files:
19+
# Check if the file is an MDX file and not in excluded names
2020
if file.endswith('.mdx'):
21-
if file in excluded_names:
21+
# Extract the base name without extension for exclusion check
22+
base_name = os.path.splitext(file)[0]
23+
if base_name in excluded_names:
2224
continue
2325

2426
file_path = os.path.join(root, file)
@@ -28,10 +30,9 @@ def compile_llms_txt():
2830
file_content = f.read()
2931
content += f"## {relative_path}\n\n{file_content}\n\n"
3032

31-
# Write the complete content, replacing the existing file
32-
output_path = os.path.join(docs_dir, 'llms.txt')
33-
with open(output_path, 'w', encoding='utf-8') as f:
34-
f.write(content)
33+
# Write the complete content to llms.txt in the current directory
34+
output_path = Path('llms.txt')
35+
output_path.write_text(content, encoding='utf-8')
3536

3637
if __name__ == "__main__":
3738
compile_llms_txt()

tests/test_compile_llms.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import os
2+
import shutil
3+
import tempfile
4+
import unittest
5+
from pathlib import Path
6+
7+
import sys
8+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9+
from docs.compile_llms_txt import compile_llms_txt
10+
11+
class TestCompileLLMsTxt(unittest.TestCase):
12+
def setUp(self):
13+
# Create a temporary directory for test files
14+
self.test_dir = tempfile.mkdtemp()
15+
self.docs_dir = Path(self.test_dir)
16+
17+
# Save current working directory
18+
self.original_cwd = os.getcwd()
19+
os.chdir(self.docs_dir)
20+
21+
def tearDown(self):
22+
os.chdir(self.original_cwd)
23+
shutil.rmtree(self.test_dir)
24+
25+
def create_test_mdx_file(self, path: str, content: str):
26+
"""Helper to create test MDX files"""
27+
file_path = self.docs_dir / path
28+
file_path.parent.mkdir(parents=True, exist_ok=True)
29+
file_path.write_text(content)
30+
31+
def test_basic_compilation(self):
32+
"""Test basic MDX file compilation"""
33+
# Create test MDX files
34+
self.create_test_mdx_file("test1.mdx", "Test content 1")
35+
self.create_test_mdx_file("test2.mdx", "Test content 2")
36+
37+
# Run compilation
38+
compile_llms_txt()
39+
40+
# Check output file exists and contains expected content
41+
output_path = self.docs_dir / "llms.txt"
42+
self.assertTrue(output_path.exists())
43+
44+
content = output_path.read_text()
45+
self.assertIn("## test1.mdx", content)
46+
self.assertIn("Test content 1", content)
47+
self.assertIn("## test2.mdx", content)
48+
self.assertIn("Test content 2", content)
49+
50+
def test_excluded_directories(self):
51+
"""Test that files in excluded directories are skipped"""
52+
# Create files in both regular and excluded directories
53+
self.create_test_mdx_file("regular/file.mdx", "Regular content")
54+
self.create_test_mdx_file("tool/file.mdx", "Tool content")
55+
56+
compile_llms_txt()
57+
58+
content = (self.docs_dir / "llms.txt").read_text()
59+
self.assertIn("Regular content", content)
60+
self.assertNotIn("Tool content", content)
61+
62+
def test_excluded_files(self):
63+
"""Test that excluded files are skipped"""
64+
self.create_test_mdx_file("regular.mdx", "Regular content")
65+
self.create_test_mdx_file("tool.mdx", "Tool content")
66+
67+
compile_llms_txt()
68+
69+
content = (self.docs_dir / "llms.txt").read_text()
70+
self.assertIn("Regular content", content)
71+
self.assertNotIn("Tool content", content)
72+
73+
def test_nested_directories(self):
74+
"""Test compilation from nested directory structure"""
75+
self.create_test_mdx_file("dir1/test1.mdx", "Content 1")
76+
self.create_test_mdx_file("dir1/dir2/test2.mdx", "Content 2")
77+
78+
compile_llms_txt()
79+
80+
content = (self.docs_dir / "llms.txt").read_text()
81+
self.assertIn("## dir1/test1.mdx", content)
82+
self.assertIn("## dir1/dir2/test2.mdx", content)
83+
self.assertIn("Content 1", content)
84+
self.assertIn("Content 2", content)
85+
86+
def test_empty_directory(self):
87+
"""Test compilation with no MDX files"""
88+
compile_llms_txt()
89+
90+
content = (self.docs_dir / "llms.txt").read_text()
91+
self.assertEqual(content, "")

0 commit comments

Comments
 (0)