chatsaver 0.1.0__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 (89) hide show
  1. chatsaver-0.1.0/.editorconfig +15 -0
  2. chatsaver-0.1.0/.gitattributes +9 -0
  3. chatsaver-0.1.0/.github/workflows/build.yml +58 -0
  4. chatsaver-0.1.0/.github/workflows/test.yml +46 -0
  5. chatsaver-0.1.0/.gitignore +49 -0
  6. chatsaver-0.1.0/.pre-commit-config.yaml +17 -0
  7. chatsaver-0.1.0/.python-version +1 -0
  8. chatsaver-0.1.0/.readthedocs.yaml +18 -0
  9. chatsaver-0.1.0/AGENTS.md +155 -0
  10. chatsaver-0.1.0/CLAUDE.md +1 -0
  11. chatsaver-0.1.0/Makefile +45 -0
  12. chatsaver-0.1.0/PKG-INFO +150 -0
  13. chatsaver-0.1.0/README.md +128 -0
  14. chatsaver-0.1.0/chatsaver/__init__.py +9 -0
  15. chatsaver-0.1.0/chatsaver/alembic/alembic.ini +141 -0
  16. chatsaver-0.1.0/chatsaver/alembic/env.py +49 -0
  17. chatsaver-0.1.0/chatsaver/alembic/script.py.mako +26 -0
  18. chatsaver-0.1.0/chatsaver/alembic/versions/.gitkeep +0 -0
  19. chatsaver-0.1.0/chatsaver/backends.py +214 -0
  20. chatsaver-0.1.0/chatsaver/cli/__init__.py +79 -0
  21. chatsaver-0.1.0/chatsaver/cli/chat.py +59 -0
  22. chatsaver-0.1.0/chatsaver/cli/db/__init__.py +22 -0
  23. chatsaver-0.1.0/chatsaver/cli/db/revision.py +26 -0
  24. chatsaver-0.1.0/chatsaver/cli/db/upgrade.py +25 -0
  25. chatsaver-0.1.0/chatsaver/cli/serve.py +74 -0
  26. chatsaver-0.1.0/chatsaver/cli/sessions.py +60 -0
  27. chatsaver-0.1.0/chatsaver/cli/version.py +17 -0
  28. chatsaver-0.1.0/chatsaver/client.py +519 -0
  29. chatsaver-0.1.0/chatsaver/data/.gitkeep +0 -0
  30. chatsaver-0.1.0/chatsaver/engine.py +446 -0
  31. chatsaver-0.1.0/chatsaver/matcher.py +103 -0
  32. chatsaver-0.1.0/chatsaver/plugins/__init__.py +3 -0
  33. chatsaver-0.1.0/chatsaver/plugins/example/__init__.py +25 -0
  34. chatsaver-0.1.0/chatsaver/server/__init__.py +5 -0
  35. chatsaver-0.1.0/chatsaver/server/app.py +102 -0
  36. chatsaver-0.1.0/chatsaver/server/models.py +26 -0
  37. chatsaver-0.1.0/chatsaver/server/routes/__init__.py +14 -0
  38. chatsaver-0.1.0/chatsaver/server/routes/health.py +17 -0
  39. chatsaver-0.1.0/chatsaver/server/routes/llm.py +24 -0
  40. chatsaver-0.1.0/chatsaver/server/routes/openai.py +196 -0
  41. chatsaver-0.1.0/chatsaver/server/routes/sessions.py +152 -0
  42. chatsaver-0.1.0/chatsaver/server/run.py +50 -0
  43. chatsaver-0.1.0/chatsaver/server/static/assets/index-DDRsrd_H.css +1 -0
  44. chatsaver-0.1.0/chatsaver/server/static/assets/index-DH8FQnQG.js +70 -0
  45. chatsaver-0.1.0/chatsaver/server/static/assets/index-DH8FQnQG.js.map +1 -0
  46. chatsaver-0.1.0/chatsaver/server/static/index.html +14 -0
  47. chatsaver-0.1.0/chatsaver/settings.py +104 -0
  48. chatsaver-0.1.0/chatsaver/store.py +300 -0
  49. chatsaver-0.1.0/chatsaver/ui/.gitignore +3 -0
  50. chatsaver-0.1.0/chatsaver/ui/.nvmrc +1 -0
  51. chatsaver-0.1.0/chatsaver/ui/index.html +13 -0
  52. chatsaver-0.1.0/chatsaver/ui/package-lock.json +3960 -0
  53. chatsaver-0.1.0/chatsaver/ui/package.json +30 -0
  54. chatsaver-0.1.0/chatsaver/ui/src/App.css +10 -0
  55. chatsaver-0.1.0/chatsaver/ui/src/App.tsx +32 -0
  56. chatsaver-0.1.0/chatsaver/ui/src/index.css +12 -0
  57. chatsaver-0.1.0/chatsaver/ui/src/main.tsx +10 -0
  58. chatsaver-0.1.0/chatsaver/ui/src/services/api.ts +17 -0
  59. chatsaver-0.1.0/chatsaver/ui/src/vite-env.d.ts +10 -0
  60. chatsaver-0.1.0/chatsaver/ui/tsconfig.json +24 -0
  61. chatsaver-0.1.0/chatsaver/ui/vite.config.ts +34 -0
  62. chatsaver-0.1.0/docs/_static/.gitkeep +0 -0
  63. chatsaver-0.1.0/docs/api.rst +7 -0
  64. chatsaver-0.1.0/docs/conf.py +24 -0
  65. chatsaver-0.1.0/docs/index.md +9 -0
  66. chatsaver-0.1.0/examples/.gitkeep +0 -0
  67. chatsaver-0.1.0/pyproject.toml +98 -0
  68. chatsaver-0.1.0/scripts/changed_tests.py +73 -0
  69. chatsaver-0.1.0/tests/conftest.py +125 -0
  70. chatsaver-0.1.0/tests/end2end/.gitkeep +0 -0
  71. chatsaver-0.1.0/tests/integration/.gitkeep +0 -0
  72. chatsaver-0.1.0/tests/unit/cli/db/test_init.py +15 -0
  73. chatsaver-0.1.0/tests/unit/cli/test_chat.py +34 -0
  74. chatsaver-0.1.0/tests/unit/cli/test_init.py +15 -0
  75. chatsaver-0.1.0/tests/unit/cli/test_serve.py +34 -0
  76. chatsaver-0.1.0/tests/unit/cli/test_sessions.py +26 -0
  77. chatsaver-0.1.0/tests/unit/cli/test_version.py +9 -0
  78. chatsaver-0.1.0/tests/unit/plugins/example/test_init.py +8 -0
  79. chatsaver-0.1.0/tests/unit/server/routes/test_health.py +15 -0
  80. chatsaver-0.1.0/tests/unit/server/routes/test_llm.py +6 -0
  81. chatsaver-0.1.0/tests/unit/server/routes/test_openai.py +119 -0
  82. chatsaver-0.1.0/tests/unit/server/routes/test_sessions.py +57 -0
  83. chatsaver-0.1.0/tests/unit/test_backends.py +67 -0
  84. chatsaver-0.1.0/tests/unit/test_client.py +162 -0
  85. chatsaver-0.1.0/tests/unit/test_engine.py +185 -0
  86. chatsaver-0.1.0/tests/unit/test_matcher.py +47 -0
  87. chatsaver-0.1.0/tests/unit/test_settings.py +45 -0
  88. chatsaver-0.1.0/tests/unit/test_store.py +59 -0
  89. chatsaver-0.1.0/uv.lock +1965 -0
@@ -0,0 +1,15 @@
1
+ root = true
2
+
3
+ [*]
4
+ end_of_line = lf
5
+ charset = utf-8
6
+ insert_final_newline = true
7
+ trim_trailing_whitespace = true
8
+ indent_style = space
9
+ indent_size = 4
10
+
11
+ [*.{ts,tsx,js,json,yml,yaml,css,html}]
12
+ indent_size = 2
13
+
14
+ [Makefile]
15
+ indent_style = tab
@@ -0,0 +1,9 @@
1
+ # Always use LF line endings, on every platform
2
+ * text=auto eol=lf
3
+
4
+ # Binary files
5
+ *.png binary
6
+ *.jpg binary
7
+ *.ico binary
8
+ *.db binary
9
+ *.pkl binary
@@ -0,0 +1,58 @@
1
+ name: Build & Publish
2
+
3
+ # Release: push a tag `vX.Y.Z` — the version is derived from the tag (hatch-vcs).
4
+ # Publishing uses PyPI Trusted Publishing (no token): on pypi.org add a publisher
5
+ # for this repository with workflow `build.yml` and environment `pypi`.
6
+ on:
7
+ push:
8
+ tags: ["v*"]
9
+ workflow_dispatch:
10
+
11
+ jobs:
12
+ build:
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - uses: actions/checkout@v7
16
+ with:
17
+ fetch-depth: 0 # tags are needed to compute the version
18
+
19
+ - uses: actions/setup-node@v7
20
+ with:
21
+ node-version-file: chatsaver/ui/.nvmrc
22
+
23
+ - name: Build UI
24
+ working-directory: chatsaver/ui
25
+ run: npm install && npm run build
26
+ env:
27
+ VITE_API_URL: ""
28
+ VITE_BASE_PATH: "/"
29
+
30
+ - uses: astral-sh/setup-uv@v7
31
+
32
+ - name: Build sdist & wheel
33
+ run: uv build
34
+
35
+ - name: Smoke test the wheel
36
+ run: uv run --isolated --no-project --with dist/*.whl chatsaver version
37
+
38
+ - uses: actions/upload-artifact@v7
39
+ with:
40
+ name: dist
41
+ path: dist/
42
+
43
+ publish:
44
+ needs: build
45
+ runs-on: ubuntu-latest
46
+ if: startsWith(github.ref, 'refs/tags/v')
47
+ environment:
48
+ name: pypi
49
+ url: https://pypi.org/p/chatsaver
50
+ permissions:
51
+ id-token: write # Trusted Publishing
52
+ steps:
53
+ - uses: actions/download-artifact@v8
54
+ with:
55
+ name: dist
56
+ path: dist/
57
+
58
+ - uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,46 @@
1
+ name: Test
2
+
3
+ on:
4
+ push:
5
+ branches: [main, master]
6
+ pull_request:
7
+
8
+ jobs:
9
+ lint:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v7
13
+ - uses: astral-sh/setup-uv@v7
14
+ - run: uv run ruff check .
15
+ - run: uv run ruff format --check .
16
+
17
+ test:
18
+ runs-on: ubuntu-latest
19
+ strategy:
20
+ fail-fast: false
21
+ matrix:
22
+ python-version: ["3.12", "3.14"]
23
+
24
+ steps:
25
+ - uses: actions/checkout@v7
26
+ with:
27
+ fetch-depth: 0
28
+
29
+ - uses: astral-sh/setup-uv@v7
30
+ with:
31
+ python-version: ${{ matrix.python-version }}
32
+ enable-cache: true
33
+
34
+ - name: Test
35
+ run: uv run pytest --cov=chatsaver --cov-report=xml
36
+
37
+ ui:
38
+ runs-on: ubuntu-latest
39
+ steps:
40
+ - uses: actions/checkout@v7
41
+ - uses: actions/setup-node@v7
42
+ with:
43
+ node-version-file: chatsaver/ui/.nvmrc
44
+ - name: Build UI
45
+ working-directory: chatsaver/ui
46
+ run: npm install && npm run build
@@ -0,0 +1,49 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *$py.class
4
+ *.so
5
+ *.egg-info/
6
+ *.egg
7
+ dist/
8
+ build/
9
+ .eggs/
10
+
11
+ # Virtual environment
12
+ .uv/
13
+ .venv/
14
+ venv/
15
+
16
+ # IDE
17
+ .idea/
18
+ .vscode/
19
+ *.sw*
20
+ *~
21
+
22
+ # Testing
23
+ .pytest_cache/
24
+ .coverage
25
+ htmlcov/
26
+ .ruff_cache/
27
+
28
+ # Database
29
+ *.db
30
+
31
+ # Built UI (generated by `make build-ui`)
32
+ chatsaver/server/static/
33
+
34
+ # OS
35
+ .DS_Store
36
+ Thumbs.db
37
+
38
+ # Environment
39
+ .env
40
+ .env.local
41
+
42
+ # Docs
43
+ docs/_build/
44
+
45
+ # UI
46
+ node_modules/
47
+
48
+ # chatsaver chat history (when --sessions-dir points inside the repo)
49
+ sessions/
@@ -0,0 +1,17 @@
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v6.0.0
4
+ hooks:
5
+ - id: mixed-line-ending
6
+ args: ["--fix=lf"]
7
+ - id: end-of-file-fixer
8
+ - id: trailing-whitespace
9
+ - id: check-yaml
10
+ - id: check-toml
11
+
12
+ - repo: https://github.com/astral-sh/ruff-pre-commit
13
+ rev: v0.17.6
14
+ hooks:
15
+ - id: ruff-check
16
+ args: ["--fix"]
17
+ - id: ruff-format
@@ -0,0 +1 @@
1
+ 3.12
@@ -0,0 +1,18 @@
1
+ version: 2
2
+
3
+ build:
4
+ os: ubuntu-24.04
5
+ tools:
6
+ python: "3.12"
7
+ jobs:
8
+ pre_create_environment:
9
+ - asdf plugin add uv
10
+ - asdf install uv latest
11
+ - asdf global uv latest
12
+ create_environment:
13
+ - uv venv "${READTHEDOCS_VIRTUALENV_PATH}"
14
+ install:
15
+ - UV_PROJECT_ENVIRONMENT="${READTHEDOCS_VIRTUALENV_PATH}" uv sync --frozen --group docs
16
+
17
+ sphinx:
18
+ configuration: docs/conf.py
@@ -0,0 +1,155 @@
1
+ # chatsaver
2
+
3
+ > Resumable session wrapper around LLM inference servers (vLLM, llama.cpp, ...)
4
+
5
+ ## Rules for agents
6
+
7
+ 1. **Every task you do should be a CLI command.** If you write code to do something
8
+ (import data, run a migration, compute a report, fix records...) expose it as an
9
+ argklass command so the user can run exactly what you ran:
10
+ `chatsaver <command> [--options]`. Do not leave one-off scripts or inline `python -c`
11
+ snippets as the only way to do a task. Then run it through the CLI yourself.
12
+ See *Adding a CLI command* below.
13
+ 2. **Run ruff before you finish:** `make format` then `make lint` must pass.
14
+ 3. **Line endings are always LF (`\n`)**, on every OS and for every file type.
15
+ Never write CRLF. `.gitattributes`, `.editorconfig` and ruff (`line-ending = "lf"`)
16
+ enforce it; do not change those settings.
17
+ 4. **The virtual environment is `./.venv`** (at the project root). Use it if it exists,
18
+ otherwise create it with `uv sync --all-groups` (Python version from `.python-version`).
19
+ Never create another venv (`venv/`, `.uv/`, `env/`...) or use the system / conda Python.
20
+ Run things through `uv run ...` (which always targets `./.venv`) or `.venv/bin/<tool>`.
21
+ 5. Use `uv` for everything Python: `uv run ...`, `uv add <pkg>`, `uv add --dev <pkg>`.
22
+ Never `pip install` into the environment and never edit `uv.lock` by hand.
23
+ 6. **Tests mirror the source tree** (see *Testing*). Add or update the matching test file
24
+ for every module you change. While iterating run `make test-changed`; run the full
25
+ `make test` before you finish (CI always runs everything).
26
+
27
+ ## Testing
28
+
29
+ `tests/unit/` mirrors the package, one test file per source module:
30
+
31
+ | Source | Test |
32
+ |--------|------|
33
+ | `chatsaver/cli/version.py` | `tests/unit/cli/test_version.py` |
34
+ | `chatsaver/server/routes/health.py` | `tests/unit/server/routes/test_health.py` |
35
+ | `chatsaver/cli/db/__init__.py` | `tests/unit/cli/db/test_init.py` |
36
+ | `chatsaver/engine.py` | `tests/unit/test_engine.py` |
37
+ | `chatsaver/server/routes/openai.py` | `tests/unit/server/routes/test_openai.py` |
38
+
39
+ The LLM server is faked by `FakeLLM` in `tests/conftest.py` (an httpx transport that streams
40
+ OpenAI chunks and can drop the connection, hang, or return errors), so tests need no GPU.
41
+
42
+ - `tests/integration/`: several components together (DB + API, CLI + DB...).
43
+ - `tests/end2end/`: the built app as a user sees it.
44
+ - Shared fixtures go in `tests/conftest.py` (e.g. `client`, a FastAPI `TestClient`).
45
+ - `make test-changed` runs only the tests mapped from files changed vs `HEAD`
46
+ (uncommitted + untracked), plus changed test files, and lists source files that have
47
+ no test yet. `make test-changed BASE=origin/master` compares against a branch instead.
48
+ The mapping lives in `scripts/changed_tests.py`.
49
+
50
+ ## Commands
51
+
52
+ | Task | Command |
53
+ |------|---------|
54
+ | Install everything | `make install` (`uv sync --all-groups` + `npm install`) |
55
+ | Run backend (:8000, reload) | `make back` / `uv run chatsaver serve --reload` |
56
+ | Run frontend (:3000) | `make front` |
57
+ | Tests (all) | `make test` |
58
+ | Tests (changed files only) | `make test-changed` |
59
+ | Lint / format | `make lint` / `make format` |
60
+ | Docs | `make docs` |
61
+ | Build wheel (with UI) | `make build` |
62
+ | DB migrate | `uv run chatsaver db upgrade` |
63
+ | New migration | `uv run chatsaver db revision "add users" --autogenerate` |
64
+ | List all commands | `uv run chatsaver --help` |
65
+
66
+ Prerequisites: [uv](https://docs.astral.sh/uv/), Node.js LTS (see `chatsaver/ui/.nvmrc`).
67
+ Python environment: `./.venv`, created by `make install` (`uv sync`) if missing.
68
+
69
+ ## Adding a CLI command
70
+
71
+ Commands are discovered automatically: any module in `chatsaver/cli/` (or in
72
+ `chatsaver/plugins/`) that defines `COMMANDS` is registered. One command per module.
73
+
74
+ ```python
75
+ # chatsaver/cli/import_data.py
76
+ from dataclasses import dataclass
77
+
78
+ from argklass.command import Command
79
+
80
+
81
+ class ImportData(Command):
82
+ """Import records from a CSV file.""" # shown in --help
83
+
84
+ name: str = "import-data"
85
+
86
+ @dataclass
87
+ class Arguments:
88
+ path: str # CSV file to import (no default -> positional argument)
89
+ dry_run: bool = False # Only print what would be imported
90
+
91
+ def execute(self, args):
92
+ from chatsaver.tasks.import_data import run # keep imports lazy: fast CLI startup
93
+
94
+ return run(args.path, dry_run=args.dry_run)
95
+
96
+
97
+ COMMANDS = ImportData
98
+ ```
99
+
100
+ - Fields with a default become `--options`, fields without one are positional;
101
+ the trailing `#` comment is the help text.
102
+ - Keep the logic in a regular, importable, tested function; the command only parses and calls it.
103
+ - Command groups (`chatsaver db upgrade`): make a package whose `__init__.py`
104
+ defines a `ParentCommand` subclass, one module per subcommand (see `cli/db/`).
105
+ - Test it via `chatsaver.cli.main(["import-data", "data.csv", "--dry-run"])` (see `tests/unit/cli/`).
106
+
107
+ ## Project Structure
108
+
109
+ ```
110
+ chatsaver/ ← project root (git repo)
111
+ ├── pyproject.toml ← metadata, deps, dependency groups, ruff/pytest config
112
+ ├── uv.lock ← locked deps (commit it)
113
+ ├── Makefile ← dev shortcuts (all go through uv)
114
+ ├── docs/ ← Sphinx + MyST (markdown)
115
+ ├── scripts/changed_tests.py ← `make test-changed`
116
+ ├── tests/ ← pytest; tests/unit mirrors the package
117
+ └── chatsaver/ ← Python package (namespace, pkgutil.extend_path)
118
+ ├── __init__.py ← __version__ (from installed metadata)
119
+ ├── cli/ ← argklass CLI, one command per module
120
+ │ ├── __init__.py ← discovery + main()
121
+ │ ├── serve.py version.py
122
+ │ └── db/ ← `db` command group (alembic)
123
+ ├── server/ ← FastAPI backend
124
+ │ ├── run.py ← uvicorn runner + ASGI factory
125
+ │ ├── app.py ← create_app(), SPA serving
126
+ │ ├── models.py ← SQLAlchemy base + engine (DATABASE_URL)
127
+ │ ├── static/ ← built React app (gitignored, bundled in wheel)
128
+ │ └── routes/ ← API route modules
129
+ ├── plugins/ ← namespace package, third parties can add commands
130
+ ├── data/ ← bundled data files
131
+ ├── alembic/ ← migrations (driven by `chatsaver db ...`)
132
+ └── ui/ ← React + Vite + TypeScript + Chakra UI
133
+ ```
134
+
135
+ ## Architecture
136
+
137
+ | Mode | UI served by | API |
138
+ |------|-------------|-----|
139
+ | Development | Vite `:3000` | FastAPI `:8000`, Vite proxies `/api` |
140
+ | Production (wheel) | FastAPI `server/static/` | same process, `/api` prefix stripped by `_StripApiPrefix` |
141
+ | Static (GitHub Pages) | pre-built HTML/JS | pre-generated JSON, no server |
142
+
143
+ - SPA fallback: non-API routes return `index.html` (HashRouter).
144
+ - Database: SQLAlchemy 2.0, SQLite by default, override with `DATABASE_URL`.
145
+
146
+ ## Release
147
+
148
+ The version comes from git tags (hatch-vcs). To release:
149
+
150
+ ```bash
151
+ git tag v1.2.3 && git push origin v1.2.3
152
+ ```
153
+
154
+ `.github/workflows/build.yml` builds the UI, builds sdist + wheel with `uv build`, and publishes
155
+ to PyPI through Trusted Publishing (no API token).
@@ -0,0 +1 @@
1
+ @AGENTS.md
@@ -0,0 +1,45 @@
1
+ PROJECT = chatsaver
2
+ UI = $(PROJECT)/ui
3
+
4
+ .PHONY: install front back build-ui build test test-changed lint format docs clean
5
+
6
+ # Python deps (.venv via uv) + UI deps
7
+ install:
8
+ uv sync --all-groups
9
+ cd $(UI) && npm install
10
+
11
+ front:
12
+ cd $(UI) && npm run dev
13
+
14
+ back:
15
+ uv run $(PROJECT) serve --reload
16
+
17
+ build-ui:
18
+ cd $(UI) && npm install && VITE_API_URL= VITE_BASE_PATH=/ npm run build
19
+
20
+ build: build-ui
21
+ uv build
22
+
23
+ test:
24
+ uv run pytest
25
+
26
+ # Only the tests of the files changed vs BASE (default: HEAD, i.e. uncommitted changes)
27
+ BASE ?= HEAD
28
+ test-changed:
29
+ uv run python scripts/changed_tests.py $(BASE)
30
+
31
+ lint:
32
+ uv run ruff check .
33
+ uv run ruff format --check .
34
+
35
+ format:
36
+ uv run ruff check --fix .
37
+ uv run ruff format .
38
+
39
+ docs:
40
+ uv run --group docs sphinx-build -W -b html docs docs/_build/html
41
+
42
+ clean:
43
+ rm -rf dist/ build/ docs/_build
44
+ rm -rf $(PROJECT)/server/static
45
+ find . -type d -name __pycache__ -exec rm -rf {} +
@@ -0,0 +1,150 @@
1
+ Metadata-Version: 2.5
2
+ Name: chatsaver
3
+ Version: 0.1.0
4
+ Summary: Resumable session wrapper around LLM inference servers (vLLM, llama.cpp, ...)
5
+ Project-URL: Homepage, https://github.com/Delaunay/chatsaver
6
+ Project-URL: Issues, https://github.com/Delaunay/chatsaver/issues
7
+ Author-email: Pierre Delaunay <pierre.delaunay@mila.quebec>
8
+ License-Expression: BSD-3-Clause
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Programming Language :: Python :: 3
11
+ Requires-Python: >=3.12
12
+ Requires-Dist: alembic
13
+ Requires-Dist: argklass>=2.4
14
+ Requires-Dist: fastapi
15
+ Requires-Dist: httpx
16
+ Requires-Dist: python-multipart
17
+ Requires-Dist: sqlalchemy>=2.0
18
+ Requires-Dist: uvicorn[standard]
19
+ Provides-Extra: client
20
+ Requires-Dist: openai>=3.60.0; extra == 'client'
21
+ Description-Content-Type: text/markdown
22
+
23
+ # chatsaver
24
+
25
+ [![PyPI](https://img.shields.io/pypi/v/chatsaver.svg)](https://pypi.org/project/chatsaver/)
26
+ [![Python Versions](https://img.shields.io/pypi/pyversions/chatsaver.svg)](https://pypi.org/project/chatsaver/)
27
+ [![Tests](https://github.com/Delaunay/chatsaver/actions/workflows/test.yml/badge.svg)](https://github.com/Delaunay/chatsaver/actions/workflows/test.yml)
28
+
29
+ Resumable session wrapper around LLM inference servers (vLLM, llama.cpp, ...)
30
+
31
+ Clients talk to chatsaver instead of the LLM server. chatsaver launches and supervises
32
+ the LLM server, runs every generation in the background, and saves each conversation
33
+ to its own folder on disk. If the client loses its connection, it reconnects and picks up
34
+ the stream where it stopped. If the LLM server hangs or dies, chatsaver retries the turn.
35
+ If chatsaver itself is restarted, the conversations are still on disk.
36
+
37
+ ```bash
38
+ pip install chatsaver # or: uv tool install chatsaver
39
+
40
+ # launch vLLM (or --backend llamacpp / sglang) behind chatsaver on :8000
41
+ chatsaver serve --backend vllm --model Qwen/Qwen3-8B --llm-args "--max-model-len 32768"
42
+ # or wrap an LLM server that is already running
43
+ chatsaver serve --upstream http://gpu-node:8000
44
+
45
+ chatsaver chat --session my-run # interactive, resumable chat
46
+ chatsaver sessions [--show my-run] # inspect saved sessions (offline)
47
+ ```
48
+
49
+ ### Session folder
50
+
51
+ ```
52
+ sessions/<session_id>/
53
+ session.json model, default params, system prompt
54
+ turns/000001/
55
+ request.json request_id + the full prompt sent to the LLM
56
+ status.json pending | running | done | failed | interrupted | cancelled
57
+ chunks.jsonl every streamed delta, append-only (line number = event id)
58
+ response.json final assistant message, finish_reason, usage
59
+ ```
60
+
61
+ ### Native API (resumable)
62
+
63
+ | Endpoint | |
64
+ |---|---|
65
+ | `POST /sessions` | create or resume a session (`session_id` optional) |
66
+ | `POST /sessions/{id}/turns` | `{messages, params, request_id, stream, wait}`; the same `request_id` reattaches to the same turn |
67
+ | `GET /sessions/{id}/turns/{n}/events?offset=k` | SSE replay from event `k`, then tail (`Last-Event-ID` works too) |
68
+ | `GET /sessions/{id}/turns/{n}` | status and final response |
69
+ | `POST /sessions/{id}/turns/{n}/retry` | restart the turn (`{"continue_partial": true}` keeps the partial answer) |
70
+ | `POST /sessions/{id}/turns/{n}/cancel` | stop a generation |
71
+ | `GET /sessions/{id}/messages` | conversation history |
72
+ | `GET /llm`, `POST /llm/restart` | status and restart of the launched LLM server |
73
+
74
+ ### OpenAI-compatible API (no client change)
75
+
76
+ chatsaver listens on `:8000`, where clients expect vLLM, and starts the LLM server on `:8001`.
77
+ Existing clients keep calling `http://<host>:8000/v1`:
78
+
79
+ - `POST /v1/chat/completions` is recorded. chatsaver works out which session a request
80
+ belongs to from its messages: a request that resends a saved conversation plus a new
81
+ message is the next turn of that session. Going back to an earlier point of a
82
+ conversation starts a new session whose `parent` points to the original.
83
+ - A request resent within `--dedup-window` seconds (same messages and params), for example
84
+ after a timeout or a lost connection, reattaches to the generation in progress or gets
85
+ the saved answer. Nothing is generated twice. Use `--dedup-window 0` if identical
86
+ requests should produce independent samples.
87
+ - If a generation has to restart because the LLM server dropped, stalled or crashed, the
88
+ stream ends with an error. The client's usual retry then gets the restarted generation.
89
+ - Every other `/v1/*` endpoint (models, completions, embeddings, ...) is forwarded unchanged.
90
+ Requests with `n > 1` are forwarded but not recorded.
91
+
92
+ Clients that want to choose the session themselves can skip the matching with the header
93
+ `X-Chatsaver-Session: <id>`, or the body field `"chatsaver_session": "<id>"` (OpenAI SDK:
94
+ `extra_headers` / `extra_body`). `Idempotency-Key: <key>` (or `X-Request-Id`) sets the
95
+ request identity explicitly; a request resent with the same key always reattaches.
96
+ Responses carry `X-Chatsaver-Session` and `X-Chatsaver-Turn`.
97
+
98
+ ### Python client: the OpenAI client, plus sessions
99
+
100
+ `chatsaver.client.Client` is an `openai.OpenAI` (`AsyncClient` is an `openai.AsyncOpenAI`),
101
+ so all existing OpenAI code works with it. It adds:
102
+
103
+ - an `Idempotency-Key` on every chat completion, so when the connection drops it reconnects,
104
+ the server reattaches the request, and a stream continues from the chunk where it stopped;
105
+ - `client.session(id)`: a conversation pinned to one session that keeps its own history;
106
+ - `client.chatsaver`: the session store (`list`, `get`, `messages`, `turn`, `retry`, `cancel`).
107
+
108
+ ```python
109
+ import chatsaver.client
110
+
111
+ session = chatsaver.client.session("my-agent") # new, or resumed with its history
112
+ session.send("Hello!") # -> answer text
113
+ for text in session.stream("Tell me more"):
114
+ print(text, end="", flush=True)
115
+ session.messages # the whole conversation
116
+
117
+ session.chat.completions.create(model=..., messages=[...]) # raw OpenAI call, recorded in the session
118
+
119
+ client = chatsaver.client.Client(base_url="http://gpu-node:8000/v1") # or $CHATSAVER_URL
120
+ other = client.session("other-agent") # sessions are independent, can run in parallel
121
+ ```
122
+
123
+ `pip install chatsaver[client]` installs the `openai` dependency.
124
+
125
+ ## Development
126
+
127
+ Requires [uv](https://docs.astral.sh/uv/) and Node.js LTS.
128
+
129
+ ```bash
130
+ git clone https://github.com/Delaunay/chatsaver.git
131
+ cd chatsaver
132
+ make install # creates ./.venv (uv sync) + npm install
133
+
134
+ make front # terminal 1: UI on :3000
135
+ make back # terminal 2: API on :8000
136
+
137
+ make test # pytest
138
+ make format # ruff
139
+ uv run chatsaver --help
140
+ ```
141
+
142
+ ## Release
143
+
144
+ ```bash
145
+ git tag v0.1.0 && git push origin v0.1.0 # builds and publishes to PyPI
146
+ ```
147
+
148
+ ## License
149
+
150
+ BSD-3-Clause