google-adk-mongo-session-service 0.0.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.
- google_adk_mongo_session_service-0.0.1/.github/workflows/publish-to-pypi.yml +56 -0
- google_adk_mongo_session_service-0.0.1/.gitignore +207 -0
- google_adk_mongo_session_service-0.0.1/.python-version +1 -0
- google_adk_mongo_session_service-0.0.1/LICENSE +21 -0
- google_adk_mongo_session_service-0.0.1/PKG-INFO +57 -0
- google_adk_mongo_session_service-0.0.1/README.md +22 -0
- google_adk_mongo_session_service-0.0.1/pyproject.toml +40 -0
- google_adk_mongo_session_service-0.0.1/src/google_adk_mongo_session_service/__init__.py +3 -0
- google_adk_mongo_session_service-0.0.1/src/google_adk_mongo_session_service/models.py +185 -0
- google_adk_mongo_session_service-0.0.1/src/google_adk_mongo_session_service/mongo_session_service.py +339 -0
- google_adk_mongo_session_service-0.0.1/uv.lock +2814 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*"
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
build:
|
|
10
|
+
name: Build distributions
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
with:
|
|
15
|
+
persist-credentials: false
|
|
16
|
+
|
|
17
|
+
- name: Set up Python
|
|
18
|
+
uses: actions/setup-python@v5
|
|
19
|
+
with:
|
|
20
|
+
python-version: "3.13"
|
|
21
|
+
|
|
22
|
+
- name: Install build backend
|
|
23
|
+
run: python -m pip install --upgrade build
|
|
24
|
+
|
|
25
|
+
- name: Build sdist and wheel
|
|
26
|
+
run: python -m build
|
|
27
|
+
|
|
28
|
+
- name: Upload dist artifacts
|
|
29
|
+
uses: actions/upload-artifact@v4
|
|
30
|
+
with:
|
|
31
|
+
name: python-package-distributions
|
|
32
|
+
path: dist/
|
|
33
|
+
|
|
34
|
+
publish:
|
|
35
|
+
name: Publish to PyPI
|
|
36
|
+
needs: build
|
|
37
|
+
runs-on: ubuntu-latest
|
|
38
|
+
|
|
39
|
+
# Recommended: put publishing behind an Environment
|
|
40
|
+
environment:
|
|
41
|
+
name: pypi
|
|
42
|
+
url: https://pypi.org/p/google-adk-mongo-session-service
|
|
43
|
+
|
|
44
|
+
# IMPORTANT for Trusted Publishing (OIDC):
|
|
45
|
+
permissions:
|
|
46
|
+
id-token: write
|
|
47
|
+
|
|
48
|
+
steps:
|
|
49
|
+
- name: Download dist artifacts
|
|
50
|
+
uses: actions/download-artifact@v4
|
|
51
|
+
with:
|
|
52
|
+
name: python-package-distributions
|
|
53
|
+
path: dist/
|
|
54
|
+
|
|
55
|
+
- name: Publish to PyPI via Trusted Publishing
|
|
56
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,207 @@
|
|
|
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__/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.13
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kirill Lutcenko
|
|
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,57 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: google-adk-mongo-session-service
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Implementation of `google.adk.sessions.base_session_service.BaseSessionService` for storing session information in MongoDB.
|
|
5
|
+
Project-URL: Homepage, https://github.com/kirlut/google-adk-mongo-session-service
|
|
6
|
+
Project-URL: Repository, https://github.com/kirlut/google-adk-mongo-session-service
|
|
7
|
+
Project-URL: Issues, https://github.com/kirlut/google-adk-mongo-session-service/issues
|
|
8
|
+
Author: Kirill Lutcenko
|
|
9
|
+
License: MIT License
|
|
10
|
+
|
|
11
|
+
Copyright (c) 2026 Kirill Lutcenko
|
|
12
|
+
|
|
13
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
14
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
15
|
+
in the Software without restriction, including without limitation the rights
|
|
16
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
17
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
18
|
+
furnished to do so, subject to the following conditions:
|
|
19
|
+
|
|
20
|
+
The above copyright notice and this permission notice shall be included in all
|
|
21
|
+
copies or substantial portions of the Software.
|
|
22
|
+
|
|
23
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
24
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
25
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
26
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
27
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
28
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
29
|
+
SOFTWARE.
|
|
30
|
+
License-File: LICENSE
|
|
31
|
+
Requires-Python: >=3.13
|
|
32
|
+
Requires-Dist: google-adk>=1.23.0
|
|
33
|
+
Requires-Dist: pymongo>=4.16.0
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
[](https://pypi.org/project/google-adk-mongo-session-service/)
|
|
37
|
+
|
|
38
|
+
**Disclaimer:** This code was written with the help of an AI agent and has never been tested in production. Please test it carefully before using it in production.
|
|
39
|
+
|
|
40
|
+
Implementation of `google.adk.sessions.base_session_service.BaseSessionService` for storing session information in MongoDB. It plugs into [Google ADK](https://github.com/google/adk) agents so sessions can be backed by MongoDB instead of in-memory/default storage.
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install google-adk-mongo-session-service
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from google_adk_mongo_session_service import MongoSessionService
|
|
52
|
+
|
|
53
|
+
session_service = MongoSessionService(conn_string="mongodb://...", db_name="mydb")
|
|
54
|
+
# Pass session_service to your Agent / Runner as the session backend
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**Full example:** [example/](https://github.com/kirlut/google-adk-mongo-session-service/tree/main/example/) — runnable weather agent with MongoSessionService; see [example/README.md](https://github.com/kirlut/google-adk-mongo-session-service/blob/main/example/README.md) for setup and run instructions.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[](https://pypi.org/project/google-adk-mongo-session-service/)
|
|
2
|
+
|
|
3
|
+
**Disclaimer:** This code was written with the help of an AI agent and has never been tested in production. Please test it carefully before using it in production.
|
|
4
|
+
|
|
5
|
+
Implementation of `google.adk.sessions.base_session_service.BaseSessionService` for storing session information in MongoDB. It plugs into [Google ADK](https://github.com/google/adk) agents so sessions can be backed by MongoDB instead of in-memory/default storage.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install google-adk-mongo-session-service
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from google_adk_mongo_session_service import MongoSessionService
|
|
17
|
+
|
|
18
|
+
session_service = MongoSessionService(conn_string="mongodb://...", db_name="mydb")
|
|
19
|
+
# Pass session_service to your Agent / Runner as the session backend
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Full example:** [example/](https://github.com/kirlut/google-adk-mongo-session-service/tree/main/example/) — runnable weather agent with MongoSessionService; see [example/README.md](https://github.com/kirlut/google-adk-mongo-session-service/blob/main/example/README.md) for setup and run instructions.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "google-adk-mongo-session-service"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
description = "Implementation of `google.adk.sessions.base_session_service.BaseSessionService` for storing session information in MongoDB."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.13"
|
|
11
|
+
license = { file = "LICENSE" }
|
|
12
|
+
authors = [{ name = "Kirill Lutcenko" }]
|
|
13
|
+
|
|
14
|
+
dependencies = [
|
|
15
|
+
"google-adk>=1.23.0",
|
|
16
|
+
"pymongo>=4.16.0",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
Homepage = "https://github.com/kirlut/google-adk-mongo-session-service"
|
|
21
|
+
Repository = "https://github.com/kirlut/google-adk-mongo-session-service"
|
|
22
|
+
Issues = "https://github.com/kirlut/google-adk-mongo-session-service/issues"
|
|
23
|
+
|
|
24
|
+
[tool.hatch.build.targets.wheel]
|
|
25
|
+
packages = ["src/google_adk_mongo_session_service"]
|
|
26
|
+
|
|
27
|
+
[tool.hatch.build.targets.sdist]
|
|
28
|
+
exclude = [
|
|
29
|
+
"/example",
|
|
30
|
+
"/.venv",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[tool.uv]
|
|
34
|
+
|
|
35
|
+
[dependency-groups]
|
|
36
|
+
example = [
|
|
37
|
+
"anthropic>=0.43.0",
|
|
38
|
+
"python-dotenv>=1.2.1",
|
|
39
|
+
"litellm>=1.80.15"
|
|
40
|
+
]
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"""DAL models and _id helpers for MongoSessionService.
|
|
2
|
+
|
|
3
|
+
Document _id is computed at read/write time from PK fields (not stored in model).
|
|
4
|
+
Collections: sessions, events, app_states, user_states, adk_internal_metadata.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from datetime import datetime
|
|
10
|
+
from datetime import timezone
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from pydantic import BaseModel
|
|
14
|
+
from pydantic import Field
|
|
15
|
+
|
|
16
|
+
from google.adk.events.event import Event
|
|
17
|
+
from google.adk.sessions.session import Session
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# --- _id helpers (deterministic, PK-derived) ---
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def session_doc_id(app_name: str, user_id: str, id: str) -> str:
|
|
24
|
+
return f"{app_name}_{user_id}_{id}"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def event_doc_id(event_id: str, app_name: str, user_id: str, session_id: str) -> str:
|
|
28
|
+
return f"{event_id}_{app_name}_{user_id}_{session_id}"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def app_state_doc_id(app_name: str) -> str:
|
|
32
|
+
return app_name
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def user_state_doc_id(app_name: str, user_id: str) -> str:
|
|
36
|
+
return f"{app_name}_{user_id}"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def metadata_doc_id(key: str) -> str:
|
|
40
|
+
return key
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# --- DAL models (no _id field; _id added at serialization time) ---
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class MongoSession(BaseModel):
|
|
47
|
+
"""Document model for sessions collection."""
|
|
48
|
+
|
|
49
|
+
app_name: str
|
|
50
|
+
user_id: str
|
|
51
|
+
id: str
|
|
52
|
+
state: dict[str, Any] = Field(default_factory=dict)
|
|
53
|
+
create_time: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
54
|
+
update_time: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
55
|
+
|
|
56
|
+
def to_doc(self) -> dict[str, Any]:
|
|
57
|
+
doc = self.model_dump()
|
|
58
|
+
doc["_id"] = session_doc_id(self.app_name, self.user_id, self.id)
|
|
59
|
+
return doc
|
|
60
|
+
|
|
61
|
+
@classmethod
|
|
62
|
+
def from_doc(cls, doc: dict[str, Any]) -> MongoSession:
|
|
63
|
+
data = {k: v for k, v in doc.items() if k != "_id"}
|
|
64
|
+
return cls.model_validate(data)
|
|
65
|
+
|
|
66
|
+
def to_session(
|
|
67
|
+
self,
|
|
68
|
+
state: dict[str, Any] | None = None,
|
|
69
|
+
events: list[Event] | None = None,
|
|
70
|
+
) -> Session:
|
|
71
|
+
if state is None:
|
|
72
|
+
state = {}
|
|
73
|
+
if events is None:
|
|
74
|
+
events = []
|
|
75
|
+
update_ts = self.update_time.timestamp()
|
|
76
|
+
if self.update_time.tzinfo is None:
|
|
77
|
+
update_ts = self.update_time.replace(tzinfo=timezone.utc).timestamp()
|
|
78
|
+
return Session(
|
|
79
|
+
app_name=self.app_name,
|
|
80
|
+
user_id=self.user_id,
|
|
81
|
+
id=self.id,
|
|
82
|
+
state=state,
|
|
83
|
+
events=events,
|
|
84
|
+
last_update_time=update_ts,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class MongoEvent(BaseModel):
|
|
89
|
+
"""Document model for events collection."""
|
|
90
|
+
|
|
91
|
+
id: str
|
|
92
|
+
app_name: str
|
|
93
|
+
user_id: str
|
|
94
|
+
session_id: str
|
|
95
|
+
invocation_id: str = ""
|
|
96
|
+
timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
97
|
+
event_data: dict[str, Any] | None = None
|
|
98
|
+
|
|
99
|
+
def to_doc(self) -> dict[str, Any]:
|
|
100
|
+
doc = self.model_dump()
|
|
101
|
+
doc["_id"] = event_doc_id(self.id, self.app_name, self.user_id, self.session_id)
|
|
102
|
+
return doc
|
|
103
|
+
|
|
104
|
+
@classmethod
|
|
105
|
+
def from_doc(cls, doc: dict[str, Any]) -> MongoEvent:
|
|
106
|
+
data = {k: v for k, v in doc.items() if k != "_id"}
|
|
107
|
+
return cls.model_validate(data)
|
|
108
|
+
|
|
109
|
+
def to_event(self) -> Event:
|
|
110
|
+
ts = self.timestamp.timestamp()
|
|
111
|
+
if self.timestamp.tzinfo is None:
|
|
112
|
+
ts = self.timestamp.replace(tzinfo=timezone.utc).timestamp()
|
|
113
|
+
return Event.model_validate({
|
|
114
|
+
**(self.event_data or {}),
|
|
115
|
+
"id": self.id,
|
|
116
|
+
"invocation_id": self.invocation_id,
|
|
117
|
+
"timestamp": ts,
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
@classmethod
|
|
121
|
+
def from_event(cls, session: Session, event: Event) -> MongoEvent:
|
|
122
|
+
ts = datetime.fromtimestamp(event.timestamp, tz=timezone.utc)
|
|
123
|
+
return cls(
|
|
124
|
+
id=event.id,
|
|
125
|
+
invocation_id=event.invocation_id,
|
|
126
|
+
session_id=session.id,
|
|
127
|
+
app_name=session.app_name,
|
|
128
|
+
user_id=session.user_id,
|
|
129
|
+
timestamp=ts,
|
|
130
|
+
event_data=event.model_dump(exclude_none=True, mode="json"),
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class MongoAppState(BaseModel):
|
|
135
|
+
"""Document model for app_states collection."""
|
|
136
|
+
|
|
137
|
+
app_name: str
|
|
138
|
+
state: dict[str, Any] = Field(default_factory=dict)
|
|
139
|
+
update_time: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
140
|
+
|
|
141
|
+
def to_doc(self) -> dict[str, Any]:
|
|
142
|
+
doc = self.model_dump()
|
|
143
|
+
doc["_id"] = app_state_doc_id(self.app_name)
|
|
144
|
+
return doc
|
|
145
|
+
|
|
146
|
+
@classmethod
|
|
147
|
+
def from_doc(cls, doc: dict[str, Any]) -> MongoAppState:
|
|
148
|
+
data = {k: v for k, v in doc.items() if k != "_id"}
|
|
149
|
+
return cls.model_validate(data)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class MongoUserState(BaseModel):
|
|
153
|
+
"""Document model for user_states collection."""
|
|
154
|
+
|
|
155
|
+
app_name: str
|
|
156
|
+
user_id: str
|
|
157
|
+
state: dict[str, Any] = Field(default_factory=dict)
|
|
158
|
+
update_time: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
159
|
+
|
|
160
|
+
def to_doc(self) -> dict[str, Any]:
|
|
161
|
+
doc = self.model_dump()
|
|
162
|
+
doc["_id"] = user_state_doc_id(self.app_name, self.user_id)
|
|
163
|
+
return doc
|
|
164
|
+
|
|
165
|
+
@classmethod
|
|
166
|
+
def from_doc(cls, doc: dict[str, Any]) -> MongoUserState:
|
|
167
|
+
data = {k: v for k, v in doc.items() if k != "_id"}
|
|
168
|
+
return cls.model_validate(data)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class MongoMetadata(BaseModel):
|
|
172
|
+
"""Document model for adk_internal_metadata collection."""
|
|
173
|
+
|
|
174
|
+
key: str
|
|
175
|
+
value: str
|
|
176
|
+
|
|
177
|
+
def to_doc(self) -> dict[str, Any]:
|
|
178
|
+
doc = self.model_dump()
|
|
179
|
+
doc["_id"] = metadata_doc_id(self.key)
|
|
180
|
+
return doc
|
|
181
|
+
|
|
182
|
+
@classmethod
|
|
183
|
+
def from_doc(cls, doc: dict[str, Any]) -> MongoMetadata:
|
|
184
|
+
data = {k: v for k, v in doc.items() if k != "_id"}
|
|
185
|
+
return cls.model_validate(data)
|