# Conversations
# Requires `--features openai-conversations,store-sqlite` because these filters are opt-in.
#
# Local /v1/conversations endpoints for conversation lifecycle,
# backed by the ConversationItemStore. All requests are handled
# locally — none are forwarded upstream.
#
#   POST   /v1/conversations        — create conversation
#   GET    /v1/conversations/{id}   — get conversation
#   POST   /v1/conversations/{id}   — update conversation
#   DELETE /v1/conversations/{id}   — delete conversation
#
# Usage:
#   cargo run -p praxis-ai-proxy --features openai-conversations,store-sqlite -- \
#     -c examples/configs/openai/conversations/conversations.yaml
#
# This local example deliberately uses one shared owner. Deployments with
# multiple authenticated users must use `trusted_owner`; see the
# `openai/responses/state-ownership.yaml` example.

listeners:
  - name: conversations-gateway
    address: "127.0.0.1:8080"
    filter_chains: [conversations-pipeline]

filter_chains:
  - name: conversations-pipeline
    filters:
      # Standalone compatibility mode: all callers share one owner.
      # Multi-user deployments should use a trusted owner mode instead.
      - filter: state_owner
        mode: single_tenant
        tenant_id: default

      # Required request-head classifier for openai_conversations.
      - filter: ai_operation

      - filter: openai_conversations
        # Reuses the core request limiter, shared across all eight operations.
        # Global budgets cover this filter instance; per_ip uses the TCP peer IP.
        # Each admitted request consumes one token, including later client errors.
        # A rejection returns OpenAI JSON with Retry-After integer seconds.
        # Omit this block to disable admission. A reload starts a fresh budget.
        rate_limit:
          scope: local
          mode: global
          rate: 100
          burst: 200
        # Invalid input returns OpenAI JSON 400; missing owner-scoped resources
        # return 404; storage failures return a generic JSON 500. Error code and
        # param are always present, possibly null. Initial items and their new
        # conversation commit together, so failed writes leave no partial create.
        backend: sqlite
        # In-memory:
        #   database_url: "sqlite::memory:"
        # File-backed (WAL is enabled automatically; use local storage, not NFS):
        # Use SQLite-aware backups because live changes may remain in the WAL.
        database_url: "sqlite://conversations.db?mode=rwc"
        # conversations_table defaults to "openai_conversations"
        # items_table defaults to "openai_conversation_items"
        # Item rows preserve complete ordered history; mutations do not rebuild
        # a second full-history cache. Model context limits belong to the native
        # Responses backend's truncation policy. Gateway transport/storage limits
        # and file-search continuation max_state_bytes still apply.
        # See docs/architecture/response-store.md for legacy cache conversion,
        # stop-traffic upgrade, and rollback requirements. Do not mix old and new
        # binaries against the same conversation tables.
        #
        # Connection pool tuning (optional, sqlx defaults apply when omitted):
        #   pool:
        #     max_connections: 10      # maximum pool connections (default: 10)
        #     min_connections: 0       # minimum idle connections (default: 0)
        #     idle_timeout_secs: 600   # seconds before idle connections close (default: 600, 0 to disable)
        #     acquire_timeout_secs: 30 # seconds to wait for a connection (default: 30)
        #
        # TTL and automatic purge (optional, off by default):
        #   Time-to-live retention policy that purges records older than the
        #   specified age. A background coordinator runs one reaper per distinct
        #   store backend (so two filters on one database with different table sets
        #   each get a reaper), sweeping expired records in bounded batches at the
        #   configured interval.
        #
        #   IMPORTANT: TTL is measured from record CREATION time; activity does
        #   NOT extend retention. An actively-used conversation or response older
        #   than `ttl` is purged even if recently accessed. TTL is not a storage-
        #   capacity guarantee; tenant byte quotas are tracked separately in #1626.
        #
        #   Each table is swept INDEPENDENTLY by its own created_at; the reaper
        #   does NOT cascade. A child row (conversation item, event, approval)
        #   created later than its parent can outlive a swept parent and persists
        #   until its own created_at passes the cutoff — possibly much longer than
        #   one sweep interval. (DELETE /v1/responses/{id} cascades to its approval
        #   and event rows; DELETE /v1/conversations/{id} does NOT cascade — item
        #   rows are preserved by design. The reaper is independent of both.)
        #
        #   ttl: 7d                      # retention period (humantime: 7d, 24h, 30m, etc.)
        #                                # omit or comment out to disable TTL purging
        #   sweep_interval: 1h           # how often to run the reaper (default: 1h)
        #   sweep_batch_size: 1000       # records purged per sweep batch (default: 1000)
        #
        #   The coordinator persists its policy (ttl, sweep_interval, sweep_batch_size)
        #   on each config commit. Policy changes take effect when the pipeline
        #   reloads. The reaper stops gracefully on shutdown and across config reloads.
        #
        # PostgreSQL backend:
        #   backend: postgres
        #   database_url: "postgres://user:pass@db.example.com:5432/praxis"
        #   allow_private_database_url: true
        #   ssl_mode: verify-full      # default: verify-full
        #   ssl_root_cert: /path/to/ca.pem

      - filter: router
        routes:
          - path_prefix: "/"
            cluster: "fallback-backend"

      - filter: load_balancer
        clusters:
          - name: "fallback-backend"
            endpoints:
              - "127.0.0.1:8000"

insecure_options:
  allow_private_endpoints: true # example proxies to local backends
