Skip to main content
Feature ID: PF-60
Version: 1.0
Last Updated: 2026-01-30
Status: 📋 Specification

Overview

This document describes how PF-60 (RAG Infrastructure) integrates with other platform components and cores. RAG (Retrieval-Augmented Generation) provides semantic search capabilities that enable AI features to reference organization-specific documents and knowledge.

Integration Pattern

Pattern Type: Event-Based Integration + Platform Layer PF-60 acts as both:
  1. Event Consumer: Subscribes to document lifecycle events from PF-11
  2. Platform Layer: Provides semantic search API consumed by all AI features

Publisher Dependencies

PF-11: Document Management

PF-60 subscribes to document lifecycle events to maintain embedding synchronization. Events are emitted with the DomainEvent envelope (see EVENT_CONTRACTS.md): top-level event_type, payload, and metadata. The payload contains only identifiers and timestamps; consumers must fetch document content from pf_documents under RLS. Payload-only interfaces (align with EVENT_CONTRACTS and migration 20260226140000_pf_rag_document_knowledge_events.sql):
Consumers use these types to type the payload field of the envelope. For embedding generation, the generate-embeddings edge function fetches title, extracted_content, category, tags from pf_documents by document_id; the event does not carry full content on the wire.

Consumer Cores

PF-60 provides semantic search capabilities to all modules with AI features.

Core Consumers

Integration Flow


API Contracts

Database Functions

pf_search_embeddings Performs semantic similarity search against document embeddings.
Security: SECURITY DEFINER with SET search_path = public

Edge Functions

generate-embeddings Generates embeddings for a document and stores in database.
ai-assistant (Updated) Enhanced to support RAG when useRAG: true.

Data Flow Diagrams

Document Indexing Flow

RAG Query Flow


Security Considerations

Multi-Tenancy

  • All embeddings include organization_id
  • RLS policies enforce tenant isolation on all operations
  • pf_search_embeddings uses SECURITY DEFINER but validates org_id
  • UPDATE policy includes WITH CHECK to prevent org_id modification

Access Control

Embeddings inherit access control from source documents:
  • Users can only search embeddings from their organization
  • Same permissions as source documents apply
  • No cross-organization embedding access

PHI/PII Handling

  • Embedding content field contains document text (may include PHI)
  • RLS provides the same protection as source documents
  • Embeddings are cascade deleted when source is deleted
  • No PHI stored in metadata field

Performance Considerations

Indexing Performance

Search Performance

Index Configuration


Migration Guide

For Existing Documents

Use the bulk indexing job to index existing documents:

For New Integrations

  1. Emit document_published event when content is created
  2. Emit event with identifiers only (organization_id, document_id, timestamp, user_id). The consumer (generate-embeddings) fetches content from pf_documents by document_id under RLS.
  3. PF-60 handles embedding generation automatically

Troubleshooting

Common Issues

Monitoring

Check embedding status:

Dependencies


  • Spec: specs/pf/specs/PF-60-rag-infrastructure.md
  • Tasks: specs/pf/tasks/PF-60-TASKS.md
  • AI Strategy: docs/architecture/analysis/AI_INTEGRATION_STRATEGY_2026.md
  • Event Contracts: docs/architecture/integrations/EVENT_CONTRACTS.md

Last Updated: 2026-01-30