contextmd 0.1.1__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 (38) hide show
  1. contextmd-0.1.1/.github/workflows/publish.yml +74 -0
  2. contextmd-0.1.1/.gitignore +190 -0
  3. contextmd-0.1.1/CHANGELOG.md +22 -0
  4. contextmd-0.1.1/LICENSE +21 -0
  5. contextmd-0.1.1/PKG-INFO +258 -0
  6. contextmd-0.1.1/README.md +218 -0
  7. contextmd-0.1.1/pyproject.toml +91 -0
  8. contextmd-0.1.1/src/contextmd/CHANGELOG.md +0 -0
  9. contextmd-0.1.1/src/contextmd/__init__.py +9 -0
  10. contextmd-0.1.1/src/contextmd/_version.py +34 -0
  11. contextmd-0.1.1/src/contextmd/adapters/__init__.py +6 -0
  12. contextmd-0.1.1/src/contextmd/adapters/anthropic.py +106 -0
  13. contextmd-0.1.1/src/contextmd/adapters/base.py +118 -0
  14. contextmd-0.1.1/src/contextmd/adapters/litellm.py +151 -0
  15. contextmd-0.1.1/src/contextmd/adapters/openai.py +110 -0
  16. contextmd-0.1.1/src/contextmd/adapters/registry.py +89 -0
  17. contextmd-0.1.1/src/contextmd/cli/__init__.py +5 -0
  18. contextmd-0.1.1/src/contextmd/cli/main.py +237 -0
  19. contextmd-0.1.1/src/contextmd/client.py +296 -0
  20. contextmd-0.1.1/src/contextmd/config.py +102 -0
  21. contextmd-0.1.1/src/contextmd/extraction/__init__.py +5 -0
  22. contextmd-0.1.1/src/contextmd/extraction/dedup.py +147 -0
  23. contextmd-0.1.1/src/contextmd/extraction/engine.py +199 -0
  24. contextmd-0.1.1/src/contextmd/extraction/prompts.py +43 -0
  25. contextmd-0.1.1/src/contextmd/memory/__init__.py +7 -0
  26. contextmd-0.1.1/src/contextmd/memory/bootstrap.py +55 -0
  27. contextmd-0.1.1/src/contextmd/memory/router.py +129 -0
  28. contextmd-0.1.1/src/contextmd/memory/types.py +81 -0
  29. contextmd-0.1.1/src/contextmd/session.py +85 -0
  30. contextmd-0.1.1/src/contextmd/storage/__init__.py +6 -0
  31. contextmd-0.1.1/src/contextmd/storage/base.py +78 -0
  32. contextmd-0.1.1/src/contextmd/storage/markdown.py +53 -0
  33. contextmd-0.1.1/src/contextmd/storage/memory.py +194 -0
  34. contextmd-0.1.1/tests/__init__.py +1 -0
  35. contextmd-0.1.1/tests/conftest.py +40 -0
  36. contextmd-0.1.1/tests/test_adapters.py +180 -0
  37. contextmd-0.1.1/tests/test_memory.py +122 -0
  38. contextmd-0.1.1/tests/test_storage.py +134 -0
@@ -0,0 +1,74 @@
1
+ name: Test & Publish to PyPI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+ release:
9
+ types: [published]
10
+
11
+ jobs:
12
+ test:
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ python-version: ["3.10", "3.11", "3.12"]
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ - name: Set up Python ${{ matrix.python-version }}
20
+ uses: actions/setup-python@v5
21
+ with:
22
+ python-version: ${{ matrix.python-version }}
23
+ - name: Install dependencies
24
+ run: |
25
+ python -m pip install --upgrade pip
26
+ pip install -e ".[dev,all]"
27
+ - name: Lint with ruff
28
+ run: ruff check src/
29
+ - name: Type check with mypy
30
+ run: mypy src/contextmd --ignore-missing-imports
31
+ - name: Run tests
32
+ run: pytest --cov=contextmd --cov-report=xml
33
+ - name: Upload coverage
34
+ if: matrix.python-version == '3.11'
35
+ uses: codecov/codecov-action@v4
36
+ with:
37
+ files: ./coverage.xml
38
+ fail_ci_if_error: false
39
+
40
+ build:
41
+ needs: test
42
+ runs-on: ubuntu-latest
43
+ steps:
44
+ - uses: actions/checkout@v4
45
+ with:
46
+ fetch-depth: 0 # Required for hatch-vcs
47
+ - uses: actions/setup-python@v5
48
+ with:
49
+ python-version: "3.11"
50
+ - name: Install build tools
51
+ run: pip install build
52
+ - name: Build package
53
+ run: python -m build
54
+ - name: Upload build artifacts
55
+ uses: actions/upload-artifact@v4
56
+ with:
57
+ name: dist
58
+ path: dist/
59
+
60
+ publish:
61
+ needs: build
62
+ runs-on: ubuntu-latest
63
+ if: github.event_name == 'release' && github.event.action == 'published'
64
+ environment: pypi
65
+ permissions:
66
+ id-token: write # Required for trusted publishing
67
+ steps:
68
+ - name: Download build artifacts
69
+ uses: actions/download-artifact@v4
70
+ with:
71
+ name: dist
72
+ path: dist/
73
+ - name: Publish to PyPI
74
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,190 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
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
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/#use-with-ide
110
+ .pdm.toml
111
+
112
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113
+ __pypackages__/
114
+
115
+ # Celery stuff
116
+ celerybeat-schedule
117
+ celerybeat.pid
118
+
119
+ # SageMath parsed files
120
+ *.sage.py
121
+
122
+ # Environments
123
+ .env
124
+ .venv
125
+ env/
126
+ venv/
127
+ ENV/
128
+ env.bak/
129
+ venv.bak/
130
+
131
+ # Spyder project settings
132
+ .spyderproject
133
+ .spyproject
134
+
135
+ # Rope project settings
136
+ .ropeproject
137
+
138
+ # mkdocs documentation
139
+ /site
140
+
141
+ # mypy
142
+ .mypy_cache/
143
+ .dmypy.json
144
+ dmypy.json
145
+
146
+ # Pyre type checker
147
+ .pyre/
148
+
149
+ # pytype static type analyzer
150
+ .pytype/
151
+
152
+ # Cython debug symbols
153
+ cython_debug/
154
+
155
+ # PyCharm
156
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157
+ # be added to the global gitignore or merged into this project gitignore. For a PyCharm
158
+ # project, it is recommended to include the following files:
159
+ .idea/
160
+ *.iws
161
+ *.iml
162
+ *.ipr
163
+
164
+ # VS Code
165
+ .vscode/
166
+
167
+ # macOS
168
+ .DS_Store
169
+ .AppleDouble
170
+ .LSOverride
171
+
172
+ # Windows
173
+ Thumbs.db
174
+ ehthumbs.db
175
+ Desktop.ini
176
+
177
+ # Linux
178
+ *~
179
+
180
+ # Temporary files
181
+ *.tmp
182
+ *.temp
183
+ *.swp
184
+ *.swo
185
+ *~
186
+
187
+ # Log files
188
+ *.log
189
+
190
+ prd/
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-03-02
11
+
12
+ ### Added
13
+ - Initial release of contextmd
14
+ - Provider-agnostic middleware for LLM API calls
15
+ - Persistent memory using local Markdown files
16
+ - Support for OpenAI, Anthropic, and LiteLLM adapters
17
+ - Memory extraction engine with configurable prompts
18
+ - CLI interface for memory management
19
+ - Deduplication support for memory entries
20
+
21
+ [Unreleased]: https://github.com/subham/contextmd/compare/v0.1.0...HEAD
22
+ [0.1.0]: https://github.com/subham/contextmd/releases/tag/v0.1.0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Subham
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,258 @@
1
+ Metadata-Version: 2.4
2
+ Name: contextmd
3
+ Version: 0.1.1
4
+ Summary: A provider-agnostic middleware that gives LLM API calls persistent, human-readable memory using local Markdown files
5
+ Project-URL: Homepage, https://github.com/subham/contextmd
6
+ Project-URL: Documentation, https://github.com/subham/contextmd#readme
7
+ Project-URL: Repository, https://github.com/subham/contextmd
8
+ Project-URL: Issues, https://github.com/subham/contextmd/issues
9
+ Author-email: Subham <subhamkundu999@gmail.com>
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: anthropic,context,litellm,llm,markdown,memory,middleware,openai
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Requires-Python: >=3.10
23
+ Provides-Extra: all
24
+ Requires-Dist: anthropic>=0.18.0; extra == 'all'
25
+ Requires-Dist: litellm>=1.0.0; extra == 'all'
26
+ Requires-Dist: openai>=1.0.0; extra == 'all'
27
+ Provides-Extra: anthropic
28
+ Requires-Dist: anthropic>=0.18.0; extra == 'anthropic'
29
+ Provides-Extra: dev
30
+ Requires-Dist: mypy>=1.0.0; extra == 'dev'
31
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == 'dev'
32
+ Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
33
+ Requires-Dist: pytest>=7.0.0; extra == 'dev'
34
+ Requires-Dist: ruff>=0.1.0; extra == 'dev'
35
+ Provides-Extra: litellm
36
+ Requires-Dist: litellm>=1.0.0; extra == 'litellm'
37
+ Provides-Extra: openai
38
+ Requires-Dist: openai>=1.0.0; extra == 'openai'
39
+ Description-Content-Type: text/markdown
40
+
41
+ # ContextMD
42
+
43
+ A provider-agnostic middleware that gives OpenAI, Anthropic, and LiteLLM API calls persistent, human-readable memory using local Markdown files.
44
+
45
+ ## Installation
46
+
47
+ ```bash
48
+ pip install contextmd
49
+
50
+ # With provider support
51
+ pip install contextmd[openai] # OpenAI only
52
+ pip install contextmd[anthropic] # Anthropic only
53
+ pip install contextmd[litellm] # LiteLLM (100+ providers)
54
+ pip install contextmd[all] # All providers
55
+ ```
56
+
57
+ ## Quick Start
58
+
59
+ ### OpenAI
60
+
61
+ ```python
62
+ from openai import OpenAI
63
+ from contextmd import ContextMD
64
+
65
+ # Wrap your existing client
66
+ client = ContextMD(OpenAI(), memory_dir=".contextmd/")
67
+
68
+ # Use exactly like normal - memory is automatic
69
+ response = client.chat.completions.create(
70
+ model="gpt-5.2",
71
+ messages=[{"role": "user", "content": "Hello!"}]
72
+ )
73
+ ```
74
+
75
+ ### Anthropic
76
+
77
+ ```python
78
+ from anthropic import Anthropic
79
+ from contextmd import ContextMD
80
+
81
+ client = ContextMD(Anthropic(), memory_dir=".contextmd/")
82
+
83
+ response = client.messages.create(
84
+ model="claude-opus-4-6",
85
+ max_tokens=1024,
86
+ messages=[{"role": "user", "content": "Hello!"}]
87
+ )
88
+ ```
89
+
90
+ ### LiteLLM (100+ providers)
91
+
92
+ ```python
93
+ import litellm
94
+ from contextmd import ContextMD
95
+
96
+ client = ContextMD(litellm, memory_dir=".contextmd/")
97
+
98
+ # Works with any LiteLLM-supported model
99
+ response = client.completion(
100
+ model="gpt-5.2",
101
+ messages=[{"role": "user", "content": "Hello!"}]
102
+ )
103
+
104
+ # Or use Claude, Gemini, etc.
105
+ response = client.completion(
106
+ model="claude-opus-4-6",
107
+ messages=[{"role": "user", "content": "Hello!"}]
108
+ )
109
+ ```
110
+
111
+ ## How It Works
112
+
113
+ ContextMD intercepts your API calls and:
114
+
115
+ 1. **Bootstrap Loading**: Injects stored memory into every request
116
+ 2. **Response Processing**: Tracks token usage and extracts memorable facts
117
+ 3. **Memory Storage**: Saves facts to human-readable Markdown files
118
+
119
+ ### Memory Types
120
+
121
+ - **Semantic**: Permanent facts (preferences, tech stack, project context)
122
+ - **Episodic**: Time-stamped events (decisions, tasks completed)
123
+ - **Procedural**: Learned workflows ("Always use pnpm")
124
+
125
+ ### File Structure
126
+
127
+ ```
128
+ .contextmd/
129
+ ├── MEMORY.md # Semantic facts (200 line cap)
130
+ ├── config.md # Configuration
131
+ ├── memory/
132
+ │ ├── 2025-03-01.md # Daily episodic logs
133
+ │ └── 2025-03-02.md
134
+ └── sessions/
135
+ └── 2025-03-01-auth.md # Session snapshots
136
+ ```
137
+
138
+ ## API Reference
139
+
140
+ ### Manual Memory
141
+
142
+ ```python
143
+ # Remember something explicitly
144
+ client.remember("User prefers dark mode", type="semantic")
145
+ client.remember("Completed auth feature", type="episodic")
146
+ client.remember("Always run tests before commit", type="procedural")
147
+ ```
148
+
149
+ ### Session Management
150
+
151
+ ```python
152
+ # Create a named session
153
+ with client.new_session("auth-implementation") as session:
154
+ response = client.chat.completions.create(...)
155
+ # Session snapshot saved automatically on exit
156
+
157
+ # Or manually
158
+ session = client.new_session("feature-work")
159
+ # ... do work ...
160
+ session.end() # Saves snapshot
161
+ ```
162
+
163
+ ### Configuration
164
+
165
+ ```python
166
+ from contextmd import ContextMD, ContextMDConfig
167
+
168
+ config = ContextMDConfig(
169
+ memory_line_cap=200, # Max lines in MEMORY.md
170
+ bootstrap_window_hours=48, # Hours of episodic memory to load
171
+ compaction_threshold=0.8, # Token threshold for extraction
172
+ snapshot_message_count=15, # Messages in session snapshots
173
+ extraction_frequency="session_end", # When to extract
174
+ )
175
+
176
+ client = ContextMD(openai_client, config=config)
177
+ ```
178
+
179
+ ## CLI
180
+
181
+ ```bash
182
+ # Initialize in current directory
183
+ contextmd init
184
+
185
+ # View memory
186
+ contextmd show
187
+
188
+ # View recent activity
189
+ contextmd history --hours 24
190
+
191
+ # List sessions
192
+ contextmd sessions
193
+
194
+ # Add memory manually
195
+ contextmd add "User prefers TypeScript" --type semantic
196
+
197
+ # View statistics
198
+ contextmd stats
199
+
200
+ # Reset all memory
201
+ contextmd reset
202
+ ```
203
+
204
+ ## Architecture
205
+
206
+ ```
207
+ ┌─────────────────────────────────────────────────────────────┐
208
+ │ Your Application │
209
+ └─────────────────────────────────────────────────────────────┘
210
+
211
+
212
+ ┌─────────────────────────────────────────────────────────────┐
213
+ │ ContextMD Wrapper │
214
+ │ ┌─────────────┐ ┌──────────────┐ ┌───────────────────┐ │
215
+ │ │ Client │ │ Memory │ │ Extraction │ │
216
+ │ │ Wrapper │──│ Router │──│ Engine │ │
217
+ │ └─────────────┘ └──────────────┘ └───────────────────┘ │
218
+ │ │ │ │ │
219
+ │ │ ▼ │ │
220
+ │ │ ┌──────────────┐ │ │
221
+ │ │ │ Storage │◄────────────┘ │
222
+ │ │ │ Layer │ │
223
+ │ │ └──────────────┘ │
224
+ └─────────│───────────────────────────────────────────────────┘
225
+
226
+
227
+ ┌─────────────────────────────────────────────────────────────┐
228
+ │ Provider Adapters │
229
+ │ ┌─────────┐ ┌───────────┐ ┌──────────┐ │
230
+ │ │ OpenAI │ │ Anthropic │ │ LiteLLM │ │
231
+ │ └─────────┘ └───────────┘ └──────────┘ │
232
+ └─────────────────────────────────────────────────────────────┘
233
+
234
+
235
+ ┌─────────────────────────────────────────────────────────────┐
236
+ │ LLM Provider │
237
+ └─────────────────────────────────────────────────────────────┘
238
+ ```
239
+
240
+ ## Development
241
+
242
+ ```bash
243
+ # Install with dev dependencies
244
+ pip install -e ".[dev]"
245
+
246
+ # Run tests
247
+ pytest
248
+
249
+ # Type checking
250
+ mypy src/contextmd
251
+
252
+ # Linting
253
+ ruff check src/contextmd
254
+ ```
255
+
256
+ ## License
257
+
258
+ MIT