Files
chat_backend/llm_be/chat_backend/signals.py
T
westfarn d54094f5e0
Unit Tests / test (push) Successful in 10s
Deploy Beta / unit-tests (push) Successful in 10s
Deploy Beta / docker (push) Successful in 21s
Deploy Beta / deploy-beta (push) Successful in 40s
Tier-gated RAG + Drive document sources (#42) (#54)
## Summary

Implements epic [#42](#42) (children #43–#53) and advances [#11](#11).

- **Entitlement:** `allows_rag` on plans (founders / backer / pro / business; not standard); exposed as `features.rag`
- **Gates:** document REST + WS `PromptType.RAG` use `assert_feature_allowed(..., "rag")`
- **Lifecycle:** dedupe ingest, delete vectors by `document_id`, honor `active`, fix document detail PATCH/DELETE
- **Workspaces:** auto-create default company workspace; fail-closed scoping
- **Drive:** personal + company Google/Microsoft connect (`link_drive` / `link_company_drive`), resource selection, sync, webhooks stubs, `sync_drive_connections` management command
- **Docs/env:** README + `.env*.example` updated

Companion FE: `chat_web_app` branch `feature/rag-epic-42-ui` (#81–#85).

## Test plan

- [x] `SKIP_RAG_INIT=1 uv run python manage.py test` (457 OK)
- [ ] Migrate finance `0004` + chat_backend `0028` on beta
- [ ] Verify Standard user: Documents API 403 + no RAG retrieval
- [ ] Verify Founders/Pro: upload + list + active toggle
- [ ] Connect Google/Microsoft Drive (incremental scopes) and Sync
- [ ] Company manager: `link_company_drive`; non-manager 403
- [ ] Run `manage.py sync_drive_connections`Reviewed-on: #54
2026-08-01 14:02:36 -07:00

31 lines
1.1 KiB
Python

from django.db.models.signals import post_delete
from django.dispatch import receiver
import os
from chat_backend.models import Document
def _rag_init_skipped() -> bool:
return os.environ.get("SKIP_RAG_INIT", "").lower() in {"1", "true", "yes"}
@receiver(post_delete, sender=Document)
def delete_vector_on_remove(sender, instance, **kwargs):
"""Remove the deleted document's chunks from the vector store (#45).
There is intentionally no post_save handler: ``DocumentUploadView``
already calls ``add_files_to_store`` for the uploaded file, so a
post_save re-ingest would duplicate that work (and, previously,
re-ingested the *entire* workspace on every save). Deletion only needs to
drop that document's own chunks, not rebuild everything else.
"""
if _rag_init_skipped():
return
try:
from .services.rag_services import AsyncRAGService
rag_service = AsyncRAGService()
rag_service.delete_document_vectors(instance.id)
except Exception as exc:
print(f"Skipping vector cleanup on Document delete: {exc}")