Скелет проекта

This commit is contained in:
2026-01-30 22:21:12 +03:00
commit 84ded7d7a9
30 changed files with 752 additions and 0 deletions

29
scripts/create_db.py Normal file
View File

@@ -0,0 +1,29 @@
#!/usr/bin/env python3
"""
Create RAG vector DB schema in Postgres (extension + stories, documents, chunks).
Requires RAG_DB_DSN and RAG_EMBEDDINGS_DIM (optional, default 1536).
Run from repo root with package installed: pip install -e . && python scripts/create_db.py
"""
from __future__ import annotations
import sys
from pathlib import Path
# Allow importing rag_agent when run as scripts/create_db.py
repo_root = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(repo_root / "src"))
from rag_agent.config import load_config
from rag_agent.index.postgres import connect, ensure_schema
def main() -> None:
config = load_config()
conn = connect(config.db_dsn)
ensure_schema(conn, config.embeddings_dim)
conn.close()
print("Schema created successfully.")
if __name__ == "__main__":
main()

35
scripts/post-commit Normal file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/env sh
# Git post-commit hook: index changed files into RAG vector DB for the current story.
# Install: cp scripts/post-commit .git/hooks/post-commit && chmod +x .git/hooks/post-commit
# Requires: RAG_REPO_PATH, RAG_DB_DSN, RAG_EMBEDDINGS_DIM; story from RAG_STORY or .rag-story or current branch name.
set -e
cd "$(git rev-parse --show-toplevel)"
# Resolve story: env RAG_STORY > file .rag-story > current branch name
if [ -n "${RAG_STORY}" ]; then
STORY="${RAG_STORY}"
elif [ -f .rag-story ]; then
STORY=$(head -n1 .rag-story | tr -d '\n\r')
else
STORY=$(git branch --show-current)
fi
if [ -z "$STORY" ]; then
echo "post-commit: RAG_STORY or .rag-story or branch name required for indexing."
exit 0
fi
# Run index (changed files only: previous commit -> HEAD)
if command -v rag-agent >/dev/null 2>&1; then
rag-agent index --changed --base-ref HEAD~1 --head-ref HEAD --story "$STORY"
elif [ -n "${VIRTUAL_ENV}" ]; then
rag-agent index --changed --base-ref HEAD~1 --head-ref HEAD --story "$STORY"
else
# Try repo venv or python -m
if [ -f "venv/bin/rag-agent" ]; then
venv/bin/rag-agent index --changed --base-ref HEAD~1 --head-ref HEAD --story "$STORY"
else
PYTHONPATH=src python -m rag_agent.cli index --changed --base-ref HEAD~1 --head-ref HEAD --story "$STORY" 2>/dev/null || true
fi
fi

33
scripts/schema.sql Normal file
View File

@@ -0,0 +1,33 @@
-- RAG vector DB schema for Postgres (pgvector).
-- Run once against an empty DB. If RAG_EMBEDDINGS_DIM is not 1536, change vector(1536) below.
-- Usage: psql "$RAG_DB_DSN" -f scripts/schema.sql
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE IF NOT EXISTS stories (
id SERIAL PRIMARY KEY,
slug TEXT UNIQUE NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT (NOW() AT TIME ZONE 'utc')
);
CREATE TABLE IF NOT EXISTS documents (
id SERIAL PRIMARY KEY,
story_id INTEGER NOT NULL REFERENCES stories(id) ON DELETE CASCADE,
path TEXT NOT NULL,
version TEXT NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
UNIQUE(story_id, path)
);
CREATE TABLE IF NOT EXISTS chunks (
id SERIAL PRIMARY KEY,
document_id INTEGER NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
chunk_index INTEGER NOT NULL,
hash TEXT NOT NULL,
content TEXT NOT NULL,
embedding vector(1536) NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_documents_story_id ON documents(story_id);
CREATE INDEX IF NOT EXISTS idx_chunks_document_id ON chunks(document_id);
CREATE INDEX IF NOT EXISTS idx_chunks_embedding ON chunks USING ivfflat (embedding vector_cosine_ops);