3tears-langgraph 0.14.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.
- 3tears_langgraph-0.14.0/.gitignore +216 -0
- 3tears_langgraph-0.14.0/LICENSE +21 -0
- 3tears_langgraph-0.14.0/PKG-INFO +118 -0
- 3tears_langgraph-0.14.0/README.md +90 -0
- 3tears_langgraph-0.14.0/pyproject.toml +52 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/__init__.py +158 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/caching.py +556 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/catalog.py +65 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/checkpoint.py +904 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/events.py +540 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/middleware.py +162 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/middleware_catalog.py +194 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/middleware_context.py +193 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/middleware_offload.py +241 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/middleware_schema.py +439 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/middleware_summarize.py +211 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/migrations/__init__.py +52 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/migrations/v001_create_checkpoint_tables.py +66 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/offload.py +202 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/protocols.py +186 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/py.typed +0 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/serde.py +85 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/state.py +46 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/streaming.py +1093 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/summarize.py +159 -0
- 3tears_langgraph-0.14.0/src/threetears/langgraph/util.py +37 -0
- 3tears_langgraph-0.14.0/tests/__init__.py +0 -0
- 3tears_langgraph-0.14.0/tests/enforcement/__init__.py +0 -0
- 3tears_langgraph-0.14.0/tests/enforcement/test_no_metallm_imports.py +26 -0
- 3tears_langgraph-0.14.0/tests/enforcement/test_no_stdlib_logging.py +167 -0
- 3tears_langgraph-0.14.0/tests/test_caching.py +704 -0
- 3tears_langgraph-0.14.0/tests/test_catalog.py +260 -0
- 3tears_langgraph-0.14.0/tests/test_checkpoint.py +363 -0
- 3tears_langgraph-0.14.0/tests/test_events.py +359 -0
- 3tears_langgraph-0.14.0/tests/test_middleware.py +161 -0
- 3tears_langgraph-0.14.0/tests/test_middleware_context.py +134 -0
- 3tears_langgraph-0.14.0/tests/test_middleware_schema.py +328 -0
- 3tears_langgraph-0.14.0/tests/test_middleware_summarize.py +97 -0
- 3tears_langgraph-0.14.0/tests/test_offload.py +635 -0
- 3tears_langgraph-0.14.0/tests/test_smoke.py +31 -0
- 3tears_langgraph-0.14.0/tests/test_state.py +39 -0
- 3tears_langgraph-0.14.0/tests/test_streaming.py +1117 -0
- 3tears_langgraph-0.14.0/tests/test_summarize.py +106 -0
|
@@ -0,0 +1,216 @@
|
|
|
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
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
208
|
+
|
|
209
|
+
# Claude Code local state
|
|
210
|
+
.claude/
|
|
211
|
+
|
|
212
|
+
# prawduct session evidence (local governance artifacts, never shipped)
|
|
213
|
+
.prawduct/
|
|
214
|
+
|
|
215
|
+
# macOS folder metadata
|
|
216
|
+
.DS_Store
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mark Pace
|
|
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,118 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: 3tears-langgraph
|
|
3
|
+
Version: 0.14.0
|
|
4
|
+
Summary: Three-tier LangGraph checkpoint saver: L1 SQLite -> L2 NATS KV -> L3 PostgreSQL
|
|
5
|
+
Project-URL: Repository, https://github.com/pacepace/3tears
|
|
6
|
+
Author: pace
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Framework :: AsyncIO
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Classifier: Topic :: Database
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Python: >=3.14
|
|
18
|
+
Requires-Dist: 3tears
|
|
19
|
+
Requires-Dist: 3tears-media-contracts
|
|
20
|
+
Requires-Dist: 3tears-observe
|
|
21
|
+
Requires-Dist: asyncpg
|
|
22
|
+
Requires-Dist: langchain-core>=1.0
|
|
23
|
+
Requires-Dist: langchain>=1.0
|
|
24
|
+
Requires-Dist: langgraph-checkpoint>=4.0
|
|
25
|
+
Requires-Dist: langgraph>=1.0
|
|
26
|
+
Requires-Dist: uuid-utils
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# 3tears-langgraph
|
|
30
|
+
|
|
31
|
+
Three-tier LangGraph checkpoint saver: L1 (SQLite) -> L2 (NATS KV) -> L3 (PostgreSQL).
|
|
32
|
+
|
|
33
|
+
L1 and L2 are optional cache layers that degrade gracefully on failure. L3 (PostgreSQL) is the source of truth, reached through the `AsyncQueryExecutor` protocol so the same saver serves trusted services (direct asyncpg pool) and sandboxed agents (NATS L3 proxy).
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install 3tears-langgraph
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from threetears.langgraph import (
|
|
45
|
+
AsyncpgPoolAdapter,
|
|
46
|
+
ThreeTierCheckpointSaver,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
# Trusted service with direct asyncpg.Pool: wrap once
|
|
50
|
+
saver = ThreeTierCheckpointSaver(executor=AsyncpgPoolAdapter(pool))
|
|
51
|
+
|
|
52
|
+
# Sandboxed agent: NatsProxyL3Backend already implements
|
|
53
|
+
# AsyncQueryExecutor, pass it straight through
|
|
54
|
+
saver = ThreeTierCheckpointSaver(executor=nats_l3_backend)
|
|
55
|
+
|
|
56
|
+
graph = builder.compile(checkpointer=saver)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Middleware
|
|
60
|
+
|
|
61
|
+
The package ships platform-level [`AgentMiddleware`](https://docs.langchain.com/oss/python/langchain/middleware) for `langchain.agents.create_agent` — the framework-aligned successor to the old hand-rolled `AgentNodeHook` / `ToolNodeHook` protocols. Consumer-specific policy lives in each consumer as its own middleware; only the reusable platform seams live here:
|
|
62
|
+
|
|
63
|
+
- `PromptCachingMiddleware` (`wrap_model_call`) — annotates a leading bare-string system message with Anthropic `cache_control={"type": "ephemeral"}` when the model supports it, then normalizes cache-hit/creation counters onto `usage_metadata["cache_usage"]`. Non-Anthropic adapters degrade silently to bare-string system messages.
|
|
64
|
+
- `ToolResultOffloadMiddleware` (`wrap_tool_call`) — when a `ToolResultOffloader` is injected on `config["configurable"]` and a tool result exceeds `offload_threshold_chars`, stores the full content out-of-band and shows the model `"<summary>\n\n[ctx:<handle>]"` (the structured `artifact` is preserved). Opt-in: no offloader ⇒ byte-for-byte no-op.
|
|
65
|
+
- `ObjectCatalogMiddleware` (`wrap_tool_call`) — when a tool returns an `ObjectHandle` in its result artifact and an `ObjectCataloger` is injected, persists a catalog record under the verified call identity. Soft-fail side-effect: a catalog error never breaks the tool result.
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from langchain.agents import create_agent
|
|
69
|
+
|
|
70
|
+
from threetears.langgraph import (
|
|
71
|
+
ObjectCatalogMiddleware,
|
|
72
|
+
PromptCachingMiddleware,
|
|
73
|
+
ToolResultOffloadMiddleware,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
agent = create_agent(
|
|
77
|
+
model=chat_anthropic,
|
|
78
|
+
tools=tools,
|
|
79
|
+
middleware=[
|
|
80
|
+
PromptCachingMiddleware(),
|
|
81
|
+
ToolResultOffloadMiddleware(),
|
|
82
|
+
ObjectCatalogMiddleware(),
|
|
83
|
+
],
|
|
84
|
+
)
|
|
85
|
+
# after a run, PromptCachingMiddleware has stamped:
|
|
86
|
+
# message.usage_metadata["cache_usage"]
|
|
87
|
+
# == {"cache_read_input_tokens": ..., "cache_creation_input_tokens": ..., "cached_tokens": ...}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The offload / catalog contracts (`ToolResultOffloader`, `ObjectCataloger`) are pure structural `Protocol`s exported from `threetears.langgraph.offload` / `threetears.langgraph.catalog`; a consumer injects a concrete implementation on `config["configurable"]` (e.g. `tool_result_offloader`, `object_cataloger`) without the package taking any dependency on the consumer's context store.
|
|
91
|
+
|
|
92
|
+
See [`3tears/docs/prompt-caching.md`](../../docs/prompt-caching.md) for the full caching contract, summarization interaction, downstream wiring checklist, and a worked example.
|
|
93
|
+
|
|
94
|
+
## Streaming
|
|
95
|
+
|
|
96
|
+
The package ships `StreamingResponse`, a transport-agnostic primitive that owns the lifecycle of one streaming response: `start` -> any number of `emit_token` / `emit_tool_call_*` -> mutually-exclusive `end` (success) or `error` (failure) terminal. `run_graph(compiled_graph, state, config)` consumes a LangGraph `astream_events(version="v2")` loop with the start/end ordering managed; on graph exception it fires `error(code="AGENT_FAILED", ...)` and re-raises so the caller still sees the failure on the synchronous path.
|
|
97
|
+
|
|
98
|
+
The wire vocabulary is fixed: `StreamStartEvent` / `StreamTokenEvent` / `StreamEndEvent` / `StreamErrorEvent` / `ToolCallStartEvent` / `ToolCallEndEvent` / `ToolCallProgressEvent`, dispatched via the `StreamEvent` discriminated union and the `parse_stream_event(payload)` adapter. The transport seam is the `StreamTransport` Protocol -- one method, `async def publish(self, payload: bytes) -> None`. Any wire (NATS subject, websocket, chunked HTTP body) satisfies it.
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from threetears.langgraph import StreamingResponse, StreamTransport
|
|
102
|
+
|
|
103
|
+
class WebSocketStreamTransport:
|
|
104
|
+
"""example transport for a websocket consumer."""
|
|
105
|
+
def __init__(self, ws): self._ws = ws
|
|
106
|
+
async def publish(self, payload: bytes) -> None:
|
|
107
|
+
await self._ws.send_bytes(payload)
|
|
108
|
+
|
|
109
|
+
stream = StreamingResponse(
|
|
110
|
+
transport=WebSocketStreamTransport(ws),
|
|
111
|
+
correlation_id=correlation_id,
|
|
112
|
+
conversation_id=conversation_id,
|
|
113
|
+
start_time_monotonic=request_start,
|
|
114
|
+
)
|
|
115
|
+
final_state = await stream.run_graph(compiled_graph, state, config)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
A reference adapter can bind the primitive to a per-correlation-id stream subject via `nc.publish_raw`. Tool-call observation envelopes flow through `ToolCallProgressHook` reading the active `StreamingResponse` from `config["configurable"]["streaming_response"]`.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# 3tears-langgraph
|
|
2
|
+
|
|
3
|
+
Three-tier LangGraph checkpoint saver: L1 (SQLite) -> L2 (NATS KV) -> L3 (PostgreSQL).
|
|
4
|
+
|
|
5
|
+
L1 and L2 are optional cache layers that degrade gracefully on failure. L3 (PostgreSQL) is the source of truth, reached through the `AsyncQueryExecutor` protocol so the same saver serves trusted services (direct asyncpg pool) and sandboxed agents (NATS L3 proxy).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install 3tears-langgraph
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from threetears.langgraph import (
|
|
17
|
+
AsyncpgPoolAdapter,
|
|
18
|
+
ThreeTierCheckpointSaver,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
# Trusted service with direct asyncpg.Pool: wrap once
|
|
22
|
+
saver = ThreeTierCheckpointSaver(executor=AsyncpgPoolAdapter(pool))
|
|
23
|
+
|
|
24
|
+
# Sandboxed agent: NatsProxyL3Backend already implements
|
|
25
|
+
# AsyncQueryExecutor, pass it straight through
|
|
26
|
+
saver = ThreeTierCheckpointSaver(executor=nats_l3_backend)
|
|
27
|
+
|
|
28
|
+
graph = builder.compile(checkpointer=saver)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Middleware
|
|
32
|
+
|
|
33
|
+
The package ships platform-level [`AgentMiddleware`](https://docs.langchain.com/oss/python/langchain/middleware) for `langchain.agents.create_agent` — the framework-aligned successor to the old hand-rolled `AgentNodeHook` / `ToolNodeHook` protocols. Consumer-specific policy lives in each consumer as its own middleware; only the reusable platform seams live here:
|
|
34
|
+
|
|
35
|
+
- `PromptCachingMiddleware` (`wrap_model_call`) — annotates a leading bare-string system message with Anthropic `cache_control={"type": "ephemeral"}` when the model supports it, then normalizes cache-hit/creation counters onto `usage_metadata["cache_usage"]`. Non-Anthropic adapters degrade silently to bare-string system messages.
|
|
36
|
+
- `ToolResultOffloadMiddleware` (`wrap_tool_call`) — when a `ToolResultOffloader` is injected on `config["configurable"]` and a tool result exceeds `offload_threshold_chars`, stores the full content out-of-band and shows the model `"<summary>\n\n[ctx:<handle>]"` (the structured `artifact` is preserved). Opt-in: no offloader ⇒ byte-for-byte no-op.
|
|
37
|
+
- `ObjectCatalogMiddleware` (`wrap_tool_call`) — when a tool returns an `ObjectHandle` in its result artifact and an `ObjectCataloger` is injected, persists a catalog record under the verified call identity. Soft-fail side-effect: a catalog error never breaks the tool result.
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from langchain.agents import create_agent
|
|
41
|
+
|
|
42
|
+
from threetears.langgraph import (
|
|
43
|
+
ObjectCatalogMiddleware,
|
|
44
|
+
PromptCachingMiddleware,
|
|
45
|
+
ToolResultOffloadMiddleware,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
agent = create_agent(
|
|
49
|
+
model=chat_anthropic,
|
|
50
|
+
tools=tools,
|
|
51
|
+
middleware=[
|
|
52
|
+
PromptCachingMiddleware(),
|
|
53
|
+
ToolResultOffloadMiddleware(),
|
|
54
|
+
ObjectCatalogMiddleware(),
|
|
55
|
+
],
|
|
56
|
+
)
|
|
57
|
+
# after a run, PromptCachingMiddleware has stamped:
|
|
58
|
+
# message.usage_metadata["cache_usage"]
|
|
59
|
+
# == {"cache_read_input_tokens": ..., "cache_creation_input_tokens": ..., "cached_tokens": ...}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The offload / catalog contracts (`ToolResultOffloader`, `ObjectCataloger`) are pure structural `Protocol`s exported from `threetears.langgraph.offload` / `threetears.langgraph.catalog`; a consumer injects a concrete implementation on `config["configurable"]` (e.g. `tool_result_offloader`, `object_cataloger`) without the package taking any dependency on the consumer's context store.
|
|
63
|
+
|
|
64
|
+
See [`3tears/docs/prompt-caching.md`](../../docs/prompt-caching.md) for the full caching contract, summarization interaction, downstream wiring checklist, and a worked example.
|
|
65
|
+
|
|
66
|
+
## Streaming
|
|
67
|
+
|
|
68
|
+
The package ships `StreamingResponse`, a transport-agnostic primitive that owns the lifecycle of one streaming response: `start` -> any number of `emit_token` / `emit_tool_call_*` -> mutually-exclusive `end` (success) or `error` (failure) terminal. `run_graph(compiled_graph, state, config)` consumes a LangGraph `astream_events(version="v2")` loop with the start/end ordering managed; on graph exception it fires `error(code="AGENT_FAILED", ...)` and re-raises so the caller still sees the failure on the synchronous path.
|
|
69
|
+
|
|
70
|
+
The wire vocabulary is fixed: `StreamStartEvent` / `StreamTokenEvent` / `StreamEndEvent` / `StreamErrorEvent` / `ToolCallStartEvent` / `ToolCallEndEvent` / `ToolCallProgressEvent`, dispatched via the `StreamEvent` discriminated union and the `parse_stream_event(payload)` adapter. The transport seam is the `StreamTransport` Protocol -- one method, `async def publish(self, payload: bytes) -> None`. Any wire (NATS subject, websocket, chunked HTTP body) satisfies it.
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from threetears.langgraph import StreamingResponse, StreamTransport
|
|
74
|
+
|
|
75
|
+
class WebSocketStreamTransport:
|
|
76
|
+
"""example transport for a websocket consumer."""
|
|
77
|
+
def __init__(self, ws): self._ws = ws
|
|
78
|
+
async def publish(self, payload: bytes) -> None:
|
|
79
|
+
await self._ws.send_bytes(payload)
|
|
80
|
+
|
|
81
|
+
stream = StreamingResponse(
|
|
82
|
+
transport=WebSocketStreamTransport(ws),
|
|
83
|
+
correlation_id=correlation_id,
|
|
84
|
+
conversation_id=conversation_id,
|
|
85
|
+
start_time_monotonic=request_start,
|
|
86
|
+
)
|
|
87
|
+
final_state = await stream.run_graph(compiled_graph, state, config)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
A reference adapter can bind the primitive to a per-correlation-id stream subject via `nc.publish_raw`. Tool-call observation envelopes flow through `ToolCallProgressHook` reading the active `StreamingResponse` from `config["configurable"]["streaming_response"]`.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "3tears-langgraph"
|
|
7
|
+
version = "0.14.0"
|
|
8
|
+
description = "Three-tier LangGraph checkpoint saver: L1 SQLite -> L2 NATS KV -> L3 PostgreSQL"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.14"
|
|
11
|
+
authors = [{name = "pace"}]
|
|
12
|
+
license = "MIT"
|
|
13
|
+
license-files = ["LICENSE"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Framework :: AsyncIO",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.14",
|
|
20
|
+
"Topic :: Database",
|
|
21
|
+
"Topic :: Software Development :: Libraries",
|
|
22
|
+
"Typing :: Typed",
|
|
23
|
+
]
|
|
24
|
+
dependencies = [
|
|
25
|
+
"3tears",
|
|
26
|
+
"3tears-media-contracts",
|
|
27
|
+
"3tears-observe",
|
|
28
|
+
"langchain>=1.0",
|
|
29
|
+
"langchain-core>=1.0",
|
|
30
|
+
"langgraph>=1.0",
|
|
31
|
+
"langgraph-checkpoint>=4.0",
|
|
32
|
+
"asyncpg",
|
|
33
|
+
"uuid-utils",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Repository = "https://github.com/pacepace/3tears"
|
|
38
|
+
|
|
39
|
+
[tool.hatch.build.targets.wheel]
|
|
40
|
+
packages = ["src/threetears"]
|
|
41
|
+
|
|
42
|
+
[tool.uv.sources]
|
|
43
|
+
3tears = { workspace = true }
|
|
44
|
+
3tears-media-contracts = { workspace = true }
|
|
45
|
+
3tears-observe = { workspace = true }
|
|
46
|
+
|
|
47
|
+
[tool.mypy]
|
|
48
|
+
strict = true
|
|
49
|
+
mypy_path = "src"
|
|
50
|
+
packages = ["threetears.langgraph"]
|
|
51
|
+
explicit_package_bases = true
|
|
52
|
+
ignore_missing_imports = true
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"""3tears-langgraph: LangGraph integration with three-tier persistence.
|
|
2
|
+
|
|
3
|
+
provides checkpoint savers, graph builders, and context management
|
|
4
|
+
for building LangGraph agents backed by 3tears infrastructure.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
# Version derived from pyproject.toml so the metadata is the single
|
|
8
|
+
# source of truth -- a future release that bumps pyproject without
|
|
9
|
+
# updating ``__init__.py`` can't drift the runtime ``__version__``.
|
|
10
|
+
# The except guard handles the rare case where the package isn't
|
|
11
|
+
# installed via importlib.metadata (e.g. running directly from a
|
|
12
|
+
# checked-out source tree without ``uv sync``); the fallback keeps
|
|
13
|
+
# imports working but reports ``unknown`` rather than crashing.
|
|
14
|
+
from importlib.metadata import PackageNotFoundError as _PackageNotFoundError
|
|
15
|
+
from importlib.metadata import version as _version
|
|
16
|
+
|
|
17
|
+
try:
|
|
18
|
+
__version__ = _version("3tears-langgraph")
|
|
19
|
+
except _PackageNotFoundError: # pragma: no cover - dev fallback
|
|
20
|
+
__version__ = "unknown"
|
|
21
|
+
|
|
22
|
+
from threetears.langgraph.caching import (
|
|
23
|
+
ChatModelCapabilities,
|
|
24
|
+
annotate_system_prompt,
|
|
25
|
+
compute_tool_key,
|
|
26
|
+
detect_capabilities,
|
|
27
|
+
extract_cache_usage,
|
|
28
|
+
should_bind_tools_fresh,
|
|
29
|
+
)
|
|
30
|
+
from threetears.langgraph.catalog import ObjectCataloger
|
|
31
|
+
from threetears.langgraph.checkpoint import ThreeTierCheckpointSaver
|
|
32
|
+
from threetears.langgraph.events import (
|
|
33
|
+
FrameworkEvent,
|
|
34
|
+
FrameworkEventRegistry,
|
|
35
|
+
ImageGeneratedEvent,
|
|
36
|
+
PromptBuiltEvent,
|
|
37
|
+
ReasoningStreamedEvent,
|
|
38
|
+
ResponseCompletedEvent,
|
|
39
|
+
ResponseFailedEvent,
|
|
40
|
+
ToolCompletedEvent,
|
|
41
|
+
ToolDispatchedEvent,
|
|
42
|
+
ToolStartedEvent,
|
|
43
|
+
WorkflowCompletedEvent,
|
|
44
|
+
WorkflowStartedEvent,
|
|
45
|
+
WorkflowStepCompletedEvent,
|
|
46
|
+
default_registry,
|
|
47
|
+
dispatch_event,
|
|
48
|
+
)
|
|
49
|
+
from threetears.langgraph.middleware import PromptCachingMiddleware
|
|
50
|
+
from threetears.langgraph.middleware_catalog import ObjectCatalogMiddleware
|
|
51
|
+
from threetears.langgraph.middleware_context import (
|
|
52
|
+
ContextMergeMiddleware,
|
|
53
|
+
ConversationContextProvider,
|
|
54
|
+
)
|
|
55
|
+
from threetears.langgraph.middleware_offload import ToolResultOffloadMiddleware
|
|
56
|
+
from threetears.langgraph.middleware_schema import SchemaPrimingMiddleware
|
|
57
|
+
from threetears.langgraph.middleware_summarize import SummarizationMiddleware
|
|
58
|
+
from threetears.langgraph.offload import (
|
|
59
|
+
DEFAULT_OFFLOAD_THRESHOLD_CHARS,
|
|
60
|
+
NEVER_OFFLOAD_TOOLS,
|
|
61
|
+
OffloadResult,
|
|
62
|
+
ToolResultOffloader,
|
|
63
|
+
format_offload_handle,
|
|
64
|
+
has_offload_handle,
|
|
65
|
+
is_never_offload_tool,
|
|
66
|
+
)
|
|
67
|
+
from threetears.langgraph.protocols import (
|
|
68
|
+
AsyncpgPoolAdapter,
|
|
69
|
+
AsyncQueryExecutor,
|
|
70
|
+
CheckpointL1Cache,
|
|
71
|
+
CheckpointL2Cache,
|
|
72
|
+
FlushCallback,
|
|
73
|
+
)
|
|
74
|
+
from threetears.langgraph.serde import UUIDSafeSerializer
|
|
75
|
+
from threetears.langgraph.state import merge_metadata
|
|
76
|
+
from threetears.langgraph.streaming import (
|
|
77
|
+
NOSTREAM_TAG,
|
|
78
|
+
StreamEndEvent,
|
|
79
|
+
StreamErrorEvent,
|
|
80
|
+
StreamEvent,
|
|
81
|
+
StreamingResponse,
|
|
82
|
+
StreamingResponseError,
|
|
83
|
+
StreamStartEvent,
|
|
84
|
+
StreamTokenEvent,
|
|
85
|
+
StreamTransport,
|
|
86
|
+
ToolCallEndEvent,
|
|
87
|
+
ToolCallProgressEvent,
|
|
88
|
+
ToolCallStartEvent,
|
|
89
|
+
parse_stream_event,
|
|
90
|
+
)
|
|
91
|
+
from threetears.langgraph.summarize import (
|
|
92
|
+
DEFAULT_SUMMARIZATION_PROMPT,
|
|
93
|
+
summarize_older_messages,
|
|
94
|
+
)
|
|
95
|
+
from threetears.langgraph.util import summarize_args
|
|
96
|
+
|
|
97
|
+
__all__ = [
|
|
98
|
+
"DEFAULT_OFFLOAD_THRESHOLD_CHARS",
|
|
99
|
+
"DEFAULT_SUMMARIZATION_PROMPT",
|
|
100
|
+
"NOSTREAM_TAG",
|
|
101
|
+
"AsyncQueryExecutor",
|
|
102
|
+
"AsyncpgPoolAdapter",
|
|
103
|
+
"ChatModelCapabilities",
|
|
104
|
+
"CheckpointL1Cache",
|
|
105
|
+
"CheckpointL2Cache",
|
|
106
|
+
"ContextMergeMiddleware",
|
|
107
|
+
"ConversationContextProvider",
|
|
108
|
+
"FlushCallback",
|
|
109
|
+
"FrameworkEvent",
|
|
110
|
+
"FrameworkEventRegistry",
|
|
111
|
+
"ImageGeneratedEvent",
|
|
112
|
+
"NEVER_OFFLOAD_TOOLS",
|
|
113
|
+
"ObjectCataloger",
|
|
114
|
+
"ObjectCatalogMiddleware",
|
|
115
|
+
"OffloadResult",
|
|
116
|
+
"PromptBuiltEvent",
|
|
117
|
+
"PromptCachingMiddleware",
|
|
118
|
+
"ReasoningStreamedEvent",
|
|
119
|
+
"ResponseCompletedEvent",
|
|
120
|
+
"ResponseFailedEvent",
|
|
121
|
+
"SchemaPrimingMiddleware",
|
|
122
|
+
"StreamEndEvent",
|
|
123
|
+
"StreamErrorEvent",
|
|
124
|
+
"StreamEvent",
|
|
125
|
+
"StreamStartEvent",
|
|
126
|
+
"StreamTokenEvent",
|
|
127
|
+
"StreamTransport",
|
|
128
|
+
"StreamingResponse",
|
|
129
|
+
"StreamingResponseError",
|
|
130
|
+
"SummarizationMiddleware",
|
|
131
|
+
"ThreeTierCheckpointSaver",
|
|
132
|
+
"ToolCallEndEvent",
|
|
133
|
+
"ToolCallProgressEvent",
|
|
134
|
+
"ToolCallStartEvent",
|
|
135
|
+
"ToolCompletedEvent",
|
|
136
|
+
"ToolDispatchedEvent",
|
|
137
|
+
"ToolResultOffloadMiddleware",
|
|
138
|
+
"ToolResultOffloader",
|
|
139
|
+
"ToolStartedEvent",
|
|
140
|
+
"UUIDSafeSerializer",
|
|
141
|
+
"WorkflowCompletedEvent",
|
|
142
|
+
"WorkflowStartedEvent",
|
|
143
|
+
"WorkflowStepCompletedEvent",
|
|
144
|
+
"annotate_system_prompt",
|
|
145
|
+
"compute_tool_key",
|
|
146
|
+
"default_registry",
|
|
147
|
+
"detect_capabilities",
|
|
148
|
+
"dispatch_event",
|
|
149
|
+
"extract_cache_usage",
|
|
150
|
+
"format_offload_handle",
|
|
151
|
+
"has_offload_handle",
|
|
152
|
+
"is_never_offload_tool",
|
|
153
|
+
"merge_metadata",
|
|
154
|
+
"parse_stream_event",
|
|
155
|
+
"should_bind_tools_fresh",
|
|
156
|
+
"summarize_args",
|
|
157
|
+
"summarize_older_messages",
|
|
158
|
+
]
|