memlord 0.2.3__tar.gz

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.
Files changed (108) hide show
  1. memlord-0.2.3/.dockerignore +12 -0
  2. memlord-0.2.3/.env.example +28 -0
  3. memlord-0.2.3/.github/workflows/ci.yml +140 -0
  4. memlord-0.2.3/.gitignore +219 -0
  5. memlord-0.2.3/.mcp.json.example +19 -0
  6. memlord-0.2.3/CLAUDE.md +91 -0
  7. memlord-0.2.3/DESIGN.md +460 -0
  8. memlord-0.2.3/Dockerfile +38 -0
  9. memlord-0.2.3/LICENSE +661 -0
  10. memlord-0.2.3/LICENSE-COMMERCIAL +35 -0
  11. memlord-0.2.3/PKG-INFO +683 -0
  12. memlord-0.2.3/README.md +221 -0
  13. memlord-0.2.3/alembic.ini +149 -0
  14. memlord-0.2.3/docker-compose.yml +42 -0
  15. memlord-0.2.3/docker-entrypoint.sh +13 -0
  16. memlord-0.2.3/media/logo.svg +42 -0
  17. memlord-0.2.3/migrations/README +1 -0
  18. memlord-0.2.3/migrations/env.py +45 -0
  19. memlord-0.2.3/migrations/script.py.mako +28 -0
  20. memlord-0.2.3/migrations/versions/2026_03_19_1334-8f44baaf5579_init.py +239 -0
  21. memlord-0.2.3/migrations/versions/2026_03_19_1754-ffd5e65054bc_add_workspace_description.py +26 -0
  22. memlord-0.2.3/migrations/versions/2026_03_20_0001-a1b2c3d4e5f6_add_revoked_tokens.py +31 -0
  23. memlord-0.2.3/migrations/versions/2026_03_22_2300-b942da1196f0_email.py +64 -0
  24. memlord-0.2.3/pyproject.toml +70 -0
  25. memlord-0.2.3/pyrightconfig.json +7 -0
  26. memlord-0.2.3/scripts/download_model.py +22 -0
  27. memlord-0.2.3/scripts/reembed.py +34 -0
  28. memlord-0.2.3/src/memlord/__init__.py +0 -0
  29. memlord-0.2.3/src/memlord/auth.py +42 -0
  30. memlord-0.2.3/src/memlord/config.py +34 -0
  31. memlord-0.2.3/src/memlord/dao/__init__.py +4 -0
  32. memlord-0.2.3/src/memlord/dao/email_token.py +65 -0
  33. memlord-0.2.3/src/memlord/dao/memory.py +289 -0
  34. memlord-0.2.3/src/memlord/dao/user.py +105 -0
  35. memlord-0.2.3/src/memlord/dao/workspace.py +411 -0
  36. memlord-0.2.3/src/memlord/db.py +40 -0
  37. memlord-0.2.3/src/memlord/embeddings.py +64 -0
  38. memlord-0.2.3/src/memlord/main.py +79 -0
  39. memlord-0.2.3/src/memlord/models/__init__.py +10 -0
  40. memlord-0.2.3/src/memlord/models/base.py +17 -0
  41. memlord-0.2.3/src/memlord/models/email_token.py +22 -0
  42. memlord-0.2.3/src/memlord/models/memory.py +43 -0
  43. memlord-0.2.3/src/memlord/models/memory_tag.py +14 -0
  44. memlord-0.2.3/src/memlord/models/oauth_client.py +13 -0
  45. memlord-0.2.3/src/memlord/models/revoked_token.py +10 -0
  46. memlord-0.2.3/src/memlord/models/schema_version.py +10 -0
  47. memlord-0.2.3/src/memlord/models/tag.py +10 -0
  48. memlord-0.2.3/src/memlord/models/user.py +16 -0
  49. memlord-0.2.3/src/memlord/models/workspace.py +61 -0
  50. memlord-0.2.3/src/memlord/oauth.py +703 -0
  51. memlord-0.2.3/src/memlord/onnx/.gitignore +1 -0
  52. memlord-0.2.3/src/memlord/schemas/__init__.py +9 -0
  53. memlord-0.2.3/src/memlord/schemas/delete.py +6 -0
  54. memlord-0.2.3/src/memlord/schemas/list_memories.py +23 -0
  55. memlord-0.2.3/src/memlord/schemas/memory_type.py +8 -0
  56. memlord-0.2.3/src/memlord/schemas/recall.py +14 -0
  57. memlord-0.2.3/src/memlord/schemas/search.py +25 -0
  58. memlord-0.2.3/src/memlord/schemas/store.py +19 -0
  59. memlord-0.2.3/src/memlord/schemas/update.py +10 -0
  60. memlord-0.2.3/src/memlord/schemas/user.py +8 -0
  61. memlord-0.2.3/src/memlord/schemas/workspace.py +28 -0
  62. memlord-0.2.3/src/memlord/search.py +144 -0
  63. memlord-0.2.3/src/memlord/server.py +46 -0
  64. memlord-0.2.3/src/memlord/templates/_invite_link.html +5 -0
  65. memlord-0.2.3/src/memlord/templates/_memory_content.html +120 -0
  66. memlord-0.2.3/src/memlord/templates/base.html +775 -0
  67. memlord-0.2.3/src/memlord/templates/forgot_password.html +71 -0
  68. memlord-0.2.3/src/memlord/templates/icon.svg +12 -0
  69. memlord-0.2.3/src/memlord/templates/index.html +260 -0
  70. memlord-0.2.3/src/memlord/templates/login.html +76 -0
  71. memlord-0.2.3/src/memlord/templates/memory.html +13 -0
  72. memlord-0.2.3/src/memlord/templates/register.html +86 -0
  73. memlord-0.2.3/src/memlord/templates/reset_password.html +77 -0
  74. memlord-0.2.3/src/memlord/templates/search.html +48 -0
  75. memlord-0.2.3/src/memlord/templates/verify_email.html +62 -0
  76. memlord-0.2.3/src/memlord/templates/workspace_detail.html +125 -0
  77. memlord-0.2.3/src/memlord/templates/workspace_join.html +27 -0
  78. memlord-0.2.3/src/memlord/templates/workspace_new.html +35 -0
  79. memlord-0.2.3/src/memlord/templates/workspaces.html +39 -0
  80. memlord-0.2.3/src/memlord/tools/__init__.py +10 -0
  81. memlord-0.2.3/src/memlord/tools/delete.py +25 -0
  82. memlord-0.2.3/src/memlord/tools/get_memory.py +23 -0
  83. memlord-0.2.3/src/memlord/tools/list_memories.py +95 -0
  84. memlord-0.2.3/src/memlord/tools/move.py +37 -0
  85. memlord-0.2.3/src/memlord/tools/recall.py +103 -0
  86. memlord-0.2.3/src/memlord/tools/retrieve.py +78 -0
  87. memlord-0.2.3/src/memlord/tools/search_by_tag.py +87 -0
  88. memlord-0.2.3/src/memlord/tools/store.py +57 -0
  89. memlord-0.2.3/src/memlord/tools/update.py +43 -0
  90. memlord-0.2.3/src/memlord/tools/workspaces.py +19 -0
  91. memlord-0.2.3/src/memlord/ui/__init__.py +12 -0
  92. memlord-0.2.3/src/memlord/ui/base.py +344 -0
  93. memlord-0.2.3/src/memlord/ui/data.py +128 -0
  94. memlord-0.2.3/src/memlord/ui/login.py +241 -0
  95. memlord-0.2.3/src/memlord/ui/utils.py +86 -0
  96. memlord-0.2.3/src/memlord/ui/workspaces.py +241 -0
  97. memlord-0.2.3/src/memlord/utils/__init__.py +0 -0
  98. memlord-0.2.3/src/memlord/utils/dt.py +5 -0
  99. memlord-0.2.3/src/memlord/utils/inject_client_id.py +49 -0
  100. memlord-0.2.3/src/memlord/utils/mail_send.py +25 -0
  101. memlord-0.2.3/tests/conftest.py +117 -0
  102. memlord-0.2.3/tests/test_dao.py +50 -0
  103. memlord-0.2.3/tests/test_data.py +31 -0
  104. memlord-0.2.3/tests/test_move.py +71 -0
  105. memlord-0.2.3/tests/test_oauth.py +127 -0
  106. memlord-0.2.3/tests/test_pipeline.py +54 -0
  107. memlord-0.2.3/tests/test_search.py +167 -0
  108. memlord-0.2.3/uv.lock +1879 -0
@@ -0,0 +1,12 @@
1
+ .venv
2
+ .git
3
+ __pycache__
4
+ *.pyc
5
+ *.pyo
6
+ .env
7
+ *.db
8
+ *.db-shm
9
+ *.db-wal
10
+ .pytest_cache
11
+ .mypy_cache
12
+ data/
@@ -0,0 +1,28 @@
1
+ # ── Database ──────────────────────────────────────────────────────────────────
2
+ MEMLORD_DB_URL=postgresql+asyncpg://postgres:postgres@postgres/memlord
3
+
4
+ # ── ONNX model ────────────────────────────────────────────────────────────────
5
+ # Directory containing model.onnx and tokenizer.json.
6
+ MEMLORD_MODEL_DIR=/app/src/memlord/onnx
7
+
8
+ # ── Server ────────────────────────────────────────────────────────────────────
9
+ MEMLORD_HOST=0.0.0.0
10
+ MEMLORD_PORT=8000
11
+
12
+ # ── Search tuning ─────────────────────────────────────────────────────────────
13
+ # MEMLORD_RRF_K=60
14
+ # MEMLORD_DEFAULT_LIMIT=10
15
+ # MEMLORD_SIM_THRESHOLD=0.7
16
+ # MEMLORD_DEDUP_THRESHOLD=0.95
17
+
18
+ # ── OAuth 2.1 ─────────────────────────────────────────────────────────────────
19
+ # MEMLORD_BASE_URL=http://localhost:8000
20
+ # MEMLORD_OAUTH_JWT_SECRET=change-me-to-a-long-random-secret
21
+
22
+ # ── Email / SMTP ───────────────────────────────────────────────────────────────
23
+ # MEMLORD_SMTP_HOST=smtp.resend.com
24
+ # MEMLORD_SMTP_PORT=587
25
+ # MEMLORD_SMTP_USER=resend
26
+ # MEMLORD_SMTP_PASSWORD=re_xxxxxxxxxxxx
27
+ # MEMLORD_SMTP_FROM=noreply@example.com
28
+ # MEMLORD_SMTP_TLS=true
@@ -0,0 +1,140 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ tags: ["v*"]
7
+ pull_request:
8
+
9
+ jobs:
10
+ lint:
11
+ name: Lint & type check
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v5
15
+ - uses: astral-sh/setup-uv@v7
16
+ - run: uv sync --dev
17
+ - run: uv run black --check .
18
+
19
+ migrations:
20
+ name: Migration check
21
+ runs-on: ubuntu-latest
22
+ services:
23
+ postgres:
24
+ image: pgvector/pgvector:pg17
25
+ env:
26
+ POSTGRES_USER: postgres
27
+ POSTGRES_PASSWORD: postgres
28
+ POSTGRES_DB: postgres
29
+ options: >-
30
+ --health-cmd pg_isready
31
+ --health-interval 10s
32
+ --health-timeout 5s
33
+ --health-retries 5
34
+ ports:
35
+ - 5432:5432
36
+ env:
37
+ MEMLORD_DB_URL: postgresql+asyncpg://postgres:postgres@localhost/postgres
38
+ steps:
39
+ - uses: actions/checkout@v5
40
+ - uses: astral-sh/setup-uv@v7
41
+ - run: uv sync --dev
42
+ - run: uv run alembic upgrade head
43
+ - run: uv run alembic-autogen-check
44
+
45
+ test:
46
+ name: Tests
47
+ runs-on: ubuntu-latest
48
+ services:
49
+ postgres:
50
+ image: pgvector/pgvector:pg17
51
+ env:
52
+ POSTGRES_USER: postgres
53
+ POSTGRES_PASSWORD: postgres
54
+ POSTGRES_DB: postgres
55
+ options: >-
56
+ --health-cmd pg_isready
57
+ --health-interval 10s
58
+ --health-timeout 5s
59
+ --health-retries 5
60
+ ports:
61
+ - 5432:5432
62
+ env:
63
+ MEMLORD_MODEL_DIR: src/memlord/onnx
64
+ MEMLORD_DB_URL: postgresql+asyncpg://postgres:postgres@localhost/postgres
65
+ steps:
66
+ - uses: actions/checkout@v5
67
+ - uses: astral-sh/setup-uv@v7
68
+ - uses: actions/cache@v4
69
+ with:
70
+ path: src/memlord/onnx
71
+ key: onnx-model-v1
72
+ - run: uv sync --dev
73
+ - run: uv run python scripts/download_model.py
74
+ - run: uv run pytest tests/ -x -q
75
+
76
+ publish:
77
+ name: Publish to PyPI
78
+ runs-on: ubuntu-latest
79
+ needs: [lint, migrations, test]
80
+ if: startsWith(github.ref, 'refs/tags/v')
81
+ permissions:
82
+ id-token: write
83
+ environment:
84
+ name: pypi
85
+ url: https://pypi.org/p/memlord
86
+ steps:
87
+ - uses: actions/checkout@v5
88
+ with:
89
+ fetch-depth: 0
90
+ - uses: astral-sh/setup-uv@v7
91
+ - run: uv build
92
+ - uses: pypa/gh-action-pypi-publish@release/v1
93
+
94
+ docker:
95
+ runs-on: ubuntu-latest
96
+ needs: [lint, migrations, test]
97
+ if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
98
+ permissions:
99
+ contents: read
100
+ packages: write
101
+ steps:
102
+ - uses: actions/checkout@v5
103
+ - uses: docker/setup-buildx-action@v4
104
+ - uses: docker/login-action@v4
105
+ with:
106
+ registry: ghcr.io
107
+ username: ${{ github.actor }}
108
+ password: ${{ secrets.GITHUB_TOKEN }}
109
+ - name: Image name
110
+ run: echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"
111
+ - name: Docker metadata
112
+ id: meta
113
+ uses: docker/metadata-action@v6
114
+ with:
115
+ images: ${{ env.IMAGE }}
116
+ tags: |
117
+ type=semver,pattern={{version}}
118
+ type=semver,pattern={{major}}.{{minor}}
119
+ type=semver,pattern={{major}}
120
+ type=sha,format=short
121
+ type=raw,value=latest,enable={{is_default_branch}}
122
+ - name: Resolve version
123
+ id: version
124
+ run: |
125
+ if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
126
+ echo "value=${GITHUB_REF#refs/tags/v}" >> "$GITHUB_OUTPUT"
127
+ else
128
+ echo "value=0.0.0" >> "$GITHUB_OUTPUT"
129
+ fi
130
+ - name: Build and push
131
+ uses: docker/build-push-action@v7
132
+ with:
133
+ context: .
134
+ push: true
135
+ platforms: linux/amd64,linux/arm64
136
+ tags: ${{ steps.meta.outputs.tags }}
137
+ labels: ${{ steps.meta.outputs.labels }}
138
+ build-args: VERSION=${{ steps.version.outputs.value }}
139
+ cache-from: type=gha
140
+ cache-to: type=gha,mode=max
@@ -0,0 +1,219 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ # Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ # poetry.lock
109
+ # poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ # pdm.lock
116
+ # pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ # pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # Redis
135
+ *.rdb
136
+ *.aof
137
+ *.pid
138
+
139
+ # RabbitMQ
140
+ mnesia/
141
+ rabbitmq/
142
+ rabbitmq-data/
143
+
144
+ # ActiveMQ
145
+ activemq-data/
146
+
147
+ # SageMath parsed files
148
+ *.sage.py
149
+
150
+ # Environments
151
+ .env
152
+ .envrc
153
+ .venv
154
+ env/
155
+ venv/
156
+ ENV/
157
+ env.bak/
158
+ venv.bak/
159
+
160
+ # Spyder project settings
161
+ .spyderproject
162
+ .spyproject
163
+
164
+ # Rope project settings
165
+ .ropeproject
166
+
167
+ # mkdocs documentation
168
+ /site
169
+
170
+ # mypy
171
+ .mypy_cache/
172
+ .dmypy.json
173
+ dmypy.json
174
+
175
+ # Pyre type checker
176
+ .pyre/
177
+
178
+ # pytype static type analyzer
179
+ .pytype/
180
+
181
+ # Cython debug symbols
182
+ cython_debug/
183
+
184
+ # PyCharm
185
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
186
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
187
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
188
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
189
+ .idea/
190
+
191
+ # Abstra
192
+ # Abstra is an AI-powered process automation framework.
193
+ # Ignore directories containing user credentials, local state, and settings.
194
+ # Learn more at https://abstra.io/docs
195
+ .abstra/
196
+
197
+ # Visual Studio Code
198
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
199
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
200
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
201
+ # you could uncomment the following to ignore the entire vscode folder
202
+ # .vscode/
203
+
204
+ # Ruff stuff:
205
+ .ruff_cache/
206
+
207
+ # PyPI configuration file
208
+ .pypirc
209
+
210
+ # Marimo
211
+ marimo/_static/
212
+ marimo/_lsp/
213
+ __marimo__/
214
+
215
+ # Streamlit
216
+ .streamlit/secrets.toml
217
+ *.db
218
+ data/
219
+ .mcp.json
@@ -0,0 +1,19 @@
1
+ {
2
+ "mcpServers": {
3
+ "memlord-local": {
4
+ "command": "python",
5
+ "args": [
6
+ "src/memlord/main.py",
7
+ "--stdio"
8
+ ],
9
+ "env": {
10
+ "MEMLORD_DB_URL": "postgresql+asyncpg://postgres:postgres@localhost/memlord",
11
+ "MEMLORD_STDIO_USER_ID": "1"
12
+ }
13
+ },
14
+ "memlord-cloud": {
15
+ "type": "http",
16
+ "url": "http://app.memlord.com/mcp"
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,91 @@
1
+ # CLAUDE.md
2
+
3
+ This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
+
5
+ ## Commands
6
+
7
+ ```bash
8
+ # Install dependencies
9
+ uv sync --dev
10
+
11
+ # Run migrations
12
+ alembic upgrade head
13
+
14
+ # Create a new migration (autogenerate)
15
+ alembic revision --autogenerate -m "description"
16
+
17
+ # Check migrations are up to date (CI)
18
+ alembic-autogen-check
19
+
20
+ # Run the server
21
+ memlord
22
+
23
+ # Type check
24
+ pyright src/
25
+
26
+ # Format
27
+ black .
28
+
29
+ # Run tests
30
+ pytest
31
+
32
+ # Run a single test
33
+ pytest tests/path/test_file.py::test_name -x
34
+ ```
35
+
36
+ ## Architecture
37
+
38
+ **Memlord** is an MCP memory server with hybrid BM25 + vector search, backed by PostgreSQL + pgvector.
39
+
40
+ **Request path:** FastAPI app (root `/`) mounts FastMCP at `/mcp`. Single uvicorn process, single port. OAuth 2.1 is optional — enabled only when all three are set: `MEMLORD_OAUTH_JWT_SECRET`, `MEMLORD_OAUTH_PASSWORD`, `MEMLORD_BASE_URL`.
41
+
42
+ **Search pipeline:** query → parallel BM25 (`search_vector @@ websearch_to_tsquery`) + vector KNN (`embedding <=>`, cosine distance) → Reciprocal Rank Fusion (`rrf = 1/(k+rank_bm25) + 1/(k+rank_vec)`, k=60) → top-N.
43
+
44
+ **Embedding pipeline:** `content` → tokenizer.json → ONNX inference (all-MiniLM-L6-v2) → mean pooling with attention mask → L2 normalize → `float32[384]` stored as `vector(384)` column in `memories`.
45
+
46
+ **DB sync:** `search_vector` is a `TSVECTOR GENERATED ALWAYS AS (to_tsvector('simple', content)) STORED` computed column — updated automatically by PostgreSQL. `embedding` is updated manually by the application on write.
47
+
48
+ ## Key Conventions
49
+
50
+ **SQLAlchemy usage:** Core queries only — no ORM relationships (`relationship`), no lazy loading. Queries are written with `select()`, `insert()`, etc. Use `sa.text()` only for PostgreSQL-specific syntax that cannot be expressed via Core. Use `.mappings().all()` for multi-column row results — access by column name, not index. Sessions used as async context managers via `session()` (general use) or `SessionDep` (FastAPI dependency). **Never call `commit()` or `rollback()` manually** — `session()` commits on success and rolls back on exception automatically.
51
+
52
+ **Vector parameters:** Pass `list[float]` directly — `Vector(384).bind_processor` converts to `'[v1,v2,...]'` string, asyncpg sends as text, PostgreSQL casts server-side. Do NOT use `register_vector` (causes codec conflict with bind_processor). Do NOT manually format vec strings.
53
+
54
+ **FastMCP tool structure:** Each tool file defines its own `mcp = FastMCP()` and registers tools with `@mcp.tool`. `tools/__init__.py` re-exports each `mcp` instance. `server.py` mounts them via `mcp.mount()`. Sessions injected via `s: AsyncSession = SessionDep # type: ignore[assignment]` using `fastmcp.dependencies.Depends`.
55
+
56
+ **Model style:** Models are defined with `import sqlalchemy as sa` and classical `sa.Column(...)` — no `Mapped`, no `mapped_column`, no type annotations on columns, no `from __future__ import annotations`, no `__all__`.
57
+
58
+ **Structured data:** Use Pydantic `BaseModel` for structured objects — no `dataclass`.
59
+
60
+ **Alembic engine:** `migrations/env.py` uses `create_async_engine` with asyncpg and runs migrations via `asyncio.run()`. No sync driver needed — pgvector is a server-side extension, no client-side loading required.
61
+
62
+ **No logic in `__init__.py`:** All logic lives in dedicated modules. `__init__.py` files are re-exports only.
63
+
64
+ **Config prefix:** All env vars use `MEMLORD_` prefix. `.env` file is supported.
65
+
66
+ **ONNX model files** (`src/memlord/onnx/model.onnx`, `tokenizer.json`) are excluded from git. Download before running: `uv run python scripts/download_model.py`. Downloaded from `sentence-transformers/all-MiniLM-L6-v2` on HuggingFace.
67
+
68
+ ## Project Layout
69
+
70
+ ```
71
+ src/memlord/
72
+ ├── config.py # pydantic-settings, MEMLORD_* env vars
73
+ ├── db.py # async SQLAlchemy engine (asyncpg)
74
+ ├── embeddings.py # ONNX session, tokenize, mean pool, L2 norm
75
+ ├── search.py # BM25 + vector KNN + RRF fusion (PostgreSQL Core queries)
76
+ ├── oauth.py # custom OAuthProvider (fastmcp.server.auth)
77
+ ├── main.py # FastAPI app + mount MCP + uvicorn entrypoint
78
+ ├── models/ # table definitions (no relationships)
79
+ ├── schemas/ # Pydantic request/response schemas
80
+ ├── tools/ # one file per MCP tool
81
+ ├── ui/ # web UI (FastAPI routers)
82
+ │ ├── __init__.py # assembles ui_router from sub-routers
83
+ │ ├── base.py # pages: index, search, memory detail, update, delete
84
+ │ ├── data.py # export/import JSON (uses ImportItem schema)
85
+ │ ├── login.py # login form (GET/POST /ui/login)
86
+ │ └── utils.py # templates, session_token, require_auth
87
+ └── onnx/ # model.onnx + tokenizer.json (committed)
88
+ migrations/
89
+ ├── env.py # async alembic env (asyncpg)
90
+ └── versions/ # migrations
91
+ ```