remdb 0.3.103__py3-none-any.whl → 0.3.141__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of remdb might be problematic. Click here for more details.

Files changed (74) hide show
  1. rem/agentic/agents/sse_simulator.py +2 -0
  2. rem/agentic/context.py +51 -27
  3. rem/agentic/mcp/tool_wrapper.py +155 -18
  4. rem/agentic/otel/setup.py +93 -4
  5. rem/agentic/providers/phoenix.py +371 -108
  6. rem/agentic/providers/pydantic_ai.py +195 -46
  7. rem/agentic/schema.py +361 -21
  8. rem/agentic/tools/rem_tools.py +3 -3
  9. rem/api/main.py +85 -16
  10. rem/api/mcp_router/resources.py +1 -1
  11. rem/api/mcp_router/server.py +18 -4
  12. rem/api/mcp_router/tools.py +394 -16
  13. rem/api/routers/admin.py +218 -1
  14. rem/api/routers/chat/completions.py +280 -7
  15. rem/api/routers/chat/models.py +81 -7
  16. rem/api/routers/chat/otel_utils.py +33 -0
  17. rem/api/routers/chat/sse_events.py +17 -1
  18. rem/api/routers/chat/streaming.py +177 -3
  19. rem/api/routers/feedback.py +142 -329
  20. rem/api/routers/query.py +360 -0
  21. rem/api/routers/shared_sessions.py +13 -13
  22. rem/cli/commands/README.md +237 -64
  23. rem/cli/commands/cluster.py +1808 -0
  24. rem/cli/commands/configure.py +4 -7
  25. rem/cli/commands/db.py +354 -143
  26. rem/cli/commands/experiments.py +436 -30
  27. rem/cli/commands/process.py +14 -8
  28. rem/cli/commands/schema.py +92 -45
  29. rem/cli/commands/session.py +336 -0
  30. rem/cli/dreaming.py +2 -2
  31. rem/cli/main.py +29 -6
  32. rem/config.py +8 -1
  33. rem/models/core/experiment.py +54 -0
  34. rem/models/core/rem_query.py +5 -2
  35. rem/models/entities/ontology.py +1 -1
  36. rem/models/entities/ontology_config.py +1 -1
  37. rem/models/entities/shared_session.py +2 -28
  38. rem/registry.py +10 -4
  39. rem/schemas/agents/examples/contract-analyzer.yaml +1 -1
  40. rem/schemas/agents/examples/contract-extractor.yaml +1 -1
  41. rem/schemas/agents/examples/cv-parser.yaml +1 -1
  42. rem/services/content/service.py +30 -8
  43. rem/services/embeddings/api.py +4 -4
  44. rem/services/embeddings/worker.py +16 -16
  45. rem/services/phoenix/client.py +59 -18
  46. rem/services/postgres/README.md +151 -26
  47. rem/services/postgres/__init__.py +2 -1
  48. rem/services/postgres/diff_service.py +531 -0
  49. rem/services/postgres/pydantic_to_sqlalchemy.py +427 -129
  50. rem/services/postgres/schema_generator.py +205 -4
  51. rem/services/postgres/service.py +6 -6
  52. rem/services/rem/parser.py +44 -9
  53. rem/services/rem/service.py +36 -2
  54. rem/services/session/compression.py +7 -0
  55. rem/services/session/reload.py +1 -1
  56. rem/settings.py +288 -16
  57. rem/sql/background_indexes.sql +19 -24
  58. rem/sql/migrations/001_install.sql +252 -69
  59. rem/sql/migrations/002_install_models.sql +2197 -619
  60. rem/sql/migrations/003_optional_extensions.sql +326 -0
  61. rem/sql/migrations/004_cache_system.sql +548 -0
  62. rem/utils/__init__.py +18 -0
  63. rem/utils/date_utils.py +2 -2
  64. rem/utils/schema_loader.py +110 -15
  65. rem/utils/sql_paths.py +146 -0
  66. rem/utils/vision.py +1 -1
  67. rem/workers/__init__.py +3 -1
  68. rem/workers/db_listener.py +579 -0
  69. rem/workers/unlogged_maintainer.py +463 -0
  70. {remdb-0.3.103.dist-info → remdb-0.3.141.dist-info}/METADATA +300 -215
  71. {remdb-0.3.103.dist-info → remdb-0.3.141.dist-info}/RECORD +73 -64
  72. rem/sql/migrations/003_seed_default_user.sql +0 -48
  73. {remdb-0.3.103.dist-info → remdb-0.3.141.dist-info}/WHEEL +0 -0
  74. {remdb-0.3.103.dist-info → remdb-0.3.141.dist-info}/entry_points.txt +0 -0
@@ -348,8 +348,27 @@ results = await service.vector_search(
348
348
 
349
349
  ### Initialize Service
350
350
 
351
+ There are two ways to initialize the PostgresService:
352
+
353
+ **Option 1: Factory function (recommended for apps using remdb as a library)**
354
+
355
+ ```python
356
+ from rem.services.postgres import get_postgres_service
357
+
358
+ # Uses POSTGRES__CONNECTION_STRING from environment
359
+ pg = get_postgres_service()
360
+ if pg is None:
361
+ raise RuntimeError("Database not configured - set POSTGRES__CONNECTION_STRING")
362
+
363
+ await pg.connect()
364
+ # ... use pg ...
365
+ await pg.disconnect()
366
+ ```
367
+
368
+ **Option 2: Direct instantiation**
369
+
351
370
  ```python
352
- from rem.services.postgres import PostgresService, Repository
371
+ from rem.services.postgres import PostgresService
353
372
 
354
373
  service = PostgresService(
355
374
  connection_string="postgresql://user:pass@localhost/remdb",
@@ -359,6 +378,9 @@ service = PostgresService(
359
378
  await service.connect()
360
379
  ```
361
380
 
381
+ > **Note**: `get_postgres_service()` returns the service directly. It does NOT support
382
+ > `async with` context manager syntax. Always call `connect()` and `disconnect()` explicitly.
383
+
362
384
  ### Using Repository Pattern
363
385
 
364
386
  **Generic Repository** for simple CRUD operations:
@@ -514,53 +536,156 @@ results = await service.vector_search(
514
536
  - HNSW parameters: `m=16, ef_construction=64` (tunable)
515
537
  - Monitor shared_buffers and work_mem
516
538
 
517
- ## Migrations
539
+ ## Schema Management
540
+
541
+ REM uses a **code-as-source-of-truth** approach. Pydantic models define the schema, and the database is kept in sync via diff-based migrations.
542
+
543
+ ### File Structure
544
+
545
+ ```
546
+ src/rem/sql/
547
+ ├── migrations/
548
+ │ ├── 001_install.sql # Core infrastructure (manual)
549
+ │ └── 002_install_models.sql # Entity tables (auto-generated)
550
+ └── background_indexes.sql # HNSW vector indexes (optional)
551
+ ```
552
+
553
+ **Key principle**: Only two migration files. No incremental `003_`, `004_` files.
518
554
 
519
- ### Using the CLI (Recommended)
555
+ ### CLI Commands
520
556
 
521
557
  ```bash
522
- # Apply all migrations
558
+ # Apply migrations (installs extensions, core tables, entity tables)
523
559
  rem db migrate
524
560
 
525
561
  # Check migration status
526
562
  rem db status
563
+
564
+ # Generate schema SQL from models (for remdb development)
565
+ rem db schema generate --models src/rem/models/entities
566
+
567
+ # Validate models for schema generation
568
+ rem db schema validate --models src/rem/models/entities
527
569
  ```
528
570
 
529
- ### Migration Files
571
+ ### Model Registry
530
572
 
531
- Located in `src/rem/sql/migrations/`:
532
- - `001_install.sql` - Core infrastructure (extensions, functions, kv_store)
533
- - `002_install_models.sql` - Entity tables (auto-generated from Pydantic models)
534
- - `003_seed_default_user.sql` - Default user setup
573
+ Models are discovered via the registry:
535
574
 
536
- Background indexes (after data load):
537
- ```bash
538
- rem db migrate --background-indexes
575
+ ```python
576
+ import rem
577
+ from rem.models.core import CoreModel
578
+
579
+ @rem.register_model
580
+ class MyEntity(CoreModel):
581
+ name: str
582
+ description: str # Auto-embeds
539
583
  ```
540
584
 
541
- ## CLI Usage
585
+ ## Using REM as a Library (Downstream Apps)
542
586
 
543
- ### Generate Schema from Models
587
+ When building an application that **depends on remdb as a package** (e.g., `pip install remdb`),
588
+ there are important differences from developing remdb itself.
544
589
 
545
- When you add or modify Pydantic models, regenerate the schema:
590
+ ### What Works Out of the Box
546
591
 
547
- ```bash
548
- # Generate 002_install_models.sql from entity models
549
- rem db schema generate --models src/rem/models/entities
592
+ 1. **All core entity tables** - Resources, Messages, Users, Sessions, etc.
593
+ 2. **PostgresService** - Full database access via `get_postgres_service()`
594
+ 3. **Repository pattern** - CRUD operations for core entities
595
+ 4. **Migrations** - `rem db migrate` applies the bundled SQL files
596
+
597
+ ```python
598
+ # In your downstream app (e.g., myapp/main.py)
599
+ from rem.services.postgres import get_postgres_service
600
+ from rem.models.entities import Message, Resource
601
+
602
+ pg = get_postgres_service()
603
+ await pg.connect()
550
604
 
551
- # Output: src/rem/sql/migrations/002_install_models.sql
552
- # Then apply: rem db migrate
605
+ # Use core entities - tables already exist
606
+ messages = await pg.query(Message, {"session_id": "abc"})
553
607
  ```
554
608
 
555
- **Workflow for adding new models:**
556
- 1. Add/modify models in `src/rem/models/entities/`
557
- 2. Run `rem db schema generate -m src/rem/models/entities`
558
- 3. Run `rem db migrate` to apply changes
609
+ ### Custom Models in Downstream Apps
559
610
 
560
- ### Validate Models
611
+ The `@rem.register_model` decorator registers models in the **runtime registry**, which is useful for:
612
+ - Schema introspection at runtime
613
+ - Future tooling that reads the registry
614
+
615
+ However, **`rem db migrate` only applies SQL files bundled in the remdb package**.
616
+ Custom models from downstream apps do NOT automatically get tables created.
617
+
618
+ **Options for custom model tables:**
619
+
620
+ **Option A: Use core entities with metadata**
621
+
622
+ Store custom data in the `metadata` JSONB field of existing entities:
623
+
624
+ ```python
625
+ resource = Resource(
626
+ name="my-custom-thing",
627
+ content="...",
628
+ metadata={"custom_field": "value", "another": 123}
629
+ )
630
+ ```
631
+
632
+ **Option B: Create tables manually**
633
+
634
+ Write and apply your own SQL:
635
+
636
+ ```sql
637
+ -- myapp/sql/custom_tables.sql
638
+ CREATE TABLE IF NOT EXISTS conversation_summaries (
639
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
640
+ session_ref TEXT NOT NULL,
641
+ summary TEXT NOT NULL,
642
+ -- ... include CoreModel fields for compatibility
643
+ user_id VARCHAR(256),
644
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
645
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
646
+ );
647
+ ```
561
648
 
562
649
  ```bash
563
- rem db schema validate --models src/rem/models/entities
650
+ psql $DATABASE_URL -f myapp/sql/custom_tables.sql
651
+ ```
652
+
653
+ **Option C: Contribute upstream**
654
+
655
+ If your model is generally useful, contribute it to remdb so it's included in
656
+ the next release and `rem db migrate` creates it automatically.
657
+
658
+ ### Example: Downstream App Structure
659
+
660
+ ```
661
+ myapp/
662
+ ├── main.py # Import models, start API
663
+ ├── models/
664
+ │ └── __init__.py # @rem.register_model decorators
665
+ ├── sql/
666
+ │ └── custom.sql # Manual migrations for custom tables
667
+ ├── .env # POSTGRES__CONNECTION_STRING, LLM keys
668
+ └── pyproject.toml # dependencies = ["remdb>=0.3.110"]
669
+ ```
670
+
671
+ ```python
672
+ # myapp/models/__init__.py
673
+ import rem
674
+ from rem.models.core import CoreModel
675
+
676
+ @rem.register_model
677
+ class ConversationSummary(CoreModel):
678
+ """Registered for introspection, but table created via sql/custom.sql"""
679
+ session_ref: str
680
+ summary: str
681
+ ```
682
+
683
+ ```python
684
+ # myapp/main.py
685
+ import models # Registers custom models
686
+
687
+ from rem.api.main import app # Use REM's FastAPI app
688
+ # Or build your own app using rem.services
564
689
  ```
565
690
 
566
691
  ## Configuration
@@ -2,6 +2,7 @@
2
2
  PostgreSQL service for CloudNativePG database operations.
3
3
  """
4
4
 
5
+ from .diff_service import DiffService, SchemaDiff
5
6
  from .repository import Repository
6
7
  from .service import PostgresService
7
8
 
@@ -20,4 +21,4 @@ def get_postgres_service() -> PostgresService | None:
20
21
  return PostgresService()
21
22
 
22
23
 
23
- __all__ = ["PostgresService", "get_postgres_service", "Repository"]
24
+ __all__ = ["PostgresService", "get_postgres_service", "Repository", "DiffService", "SchemaDiff"]