adk-chatkit 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.
- adk_chatkit-0.0.1/.gitignore +210 -0
- adk_chatkit-0.0.1/PKG-INFO +17 -0
- adk_chatkit-0.0.1/README.md +0 -0
- adk_chatkit-0.0.1/pyproject.toml +31 -0
- adk_chatkit-0.0.1/src/adk_chatkit/__about__.py +3 -0
- adk_chatkit-0.0.1/src/adk_chatkit/__init__.py +20 -0
- adk_chatkit-0.0.1/src/adk_chatkit/_callbacks.py +21 -0
- adk_chatkit-0.0.1/src/adk_chatkit/_client_tool_call.py +47 -0
- adk_chatkit-0.0.1/src/adk_chatkit/_constants.py +5 -0
- adk_chatkit-0.0.1/src/adk_chatkit/_context.py +6 -0
- adk_chatkit-0.0.1/src/adk_chatkit/_response.py +118 -0
- adk_chatkit-0.0.1/src/adk_chatkit/_store.py +258 -0
- adk_chatkit-0.0.1/src/adk_chatkit/_thread_utils.py +37 -0
- adk_chatkit-0.0.1/src/adk_chatkit/_widgets.py +18 -0
|
@@ -0,0 +1,210 @@
|
|
|
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
|
+
lib64/
|
|
18
|
+
parts/
|
|
19
|
+
sdist/
|
|
20
|
+
var/
|
|
21
|
+
wheels/
|
|
22
|
+
share/python-wheels/
|
|
23
|
+
*.egg-info/
|
|
24
|
+
.installed.cfg
|
|
25
|
+
*.egg
|
|
26
|
+
MANIFEST
|
|
27
|
+
|
|
28
|
+
# PyInstaller
|
|
29
|
+
# Usually these files are written by a python script from a template
|
|
30
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
31
|
+
*.manifest
|
|
32
|
+
*.spec
|
|
33
|
+
|
|
34
|
+
# Installer logs
|
|
35
|
+
pip-log.txt
|
|
36
|
+
pip-delete-this-directory.txt
|
|
37
|
+
|
|
38
|
+
# Unit test / coverage reports
|
|
39
|
+
htmlcov/
|
|
40
|
+
.tox/
|
|
41
|
+
.nox/
|
|
42
|
+
.coverage
|
|
43
|
+
.coverage.*
|
|
44
|
+
.cache
|
|
45
|
+
nosetests.xml
|
|
46
|
+
coverage.xml
|
|
47
|
+
*.cover
|
|
48
|
+
*.py.cover
|
|
49
|
+
.hypothesis/
|
|
50
|
+
.pytest_cache/
|
|
51
|
+
cover/
|
|
52
|
+
|
|
53
|
+
# Translations
|
|
54
|
+
*.mo
|
|
55
|
+
*.pot
|
|
56
|
+
|
|
57
|
+
# Django stuff:
|
|
58
|
+
*.log
|
|
59
|
+
local_settings.py
|
|
60
|
+
db.sqlite3
|
|
61
|
+
db.sqlite3-journal
|
|
62
|
+
|
|
63
|
+
# Flask stuff:
|
|
64
|
+
instance/
|
|
65
|
+
.webassets-cache
|
|
66
|
+
|
|
67
|
+
# Scrapy stuff:
|
|
68
|
+
.scrapy
|
|
69
|
+
|
|
70
|
+
# Sphinx documentation
|
|
71
|
+
docs/_build/
|
|
72
|
+
|
|
73
|
+
# PyBuilder
|
|
74
|
+
.pybuilder/
|
|
75
|
+
target/
|
|
76
|
+
|
|
77
|
+
# Jupyter Notebook
|
|
78
|
+
.ipynb_checkpoints
|
|
79
|
+
|
|
80
|
+
# IPython
|
|
81
|
+
profile_default/
|
|
82
|
+
ipython_config.py
|
|
83
|
+
|
|
84
|
+
# pyenv
|
|
85
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
86
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
87
|
+
# .python-version
|
|
88
|
+
|
|
89
|
+
# pipenv
|
|
90
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
91
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
92
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
93
|
+
# install all needed dependencies.
|
|
94
|
+
#Pipfile.lock
|
|
95
|
+
|
|
96
|
+
# UV
|
|
97
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
98
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
99
|
+
# commonly ignored for libraries.
|
|
100
|
+
#uv.lock
|
|
101
|
+
|
|
102
|
+
# poetry
|
|
103
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
104
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
105
|
+
# commonly ignored for libraries.
|
|
106
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
107
|
+
#poetry.lock
|
|
108
|
+
#poetry.toml
|
|
109
|
+
|
|
110
|
+
# pdm
|
|
111
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
112
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
113
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
114
|
+
#pdm.lock
|
|
115
|
+
#pdm.toml
|
|
116
|
+
.pdm-python
|
|
117
|
+
.pdm-build/
|
|
118
|
+
|
|
119
|
+
# pixi
|
|
120
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
121
|
+
#pixi.lock
|
|
122
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
123
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
124
|
+
.pixi
|
|
125
|
+
|
|
126
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
127
|
+
__pypackages__/
|
|
128
|
+
|
|
129
|
+
# Celery stuff
|
|
130
|
+
celerybeat-schedule
|
|
131
|
+
celerybeat.pid
|
|
132
|
+
|
|
133
|
+
# SageMath parsed files
|
|
134
|
+
*.sage.py
|
|
135
|
+
|
|
136
|
+
# Environments
|
|
137
|
+
.env
|
|
138
|
+
.envrc
|
|
139
|
+
.venv
|
|
140
|
+
env/
|
|
141
|
+
venv/
|
|
142
|
+
ENV/
|
|
143
|
+
env.bak/
|
|
144
|
+
venv.bak/
|
|
145
|
+
|
|
146
|
+
# Spyder project settings
|
|
147
|
+
.spyderproject
|
|
148
|
+
.spyproject
|
|
149
|
+
|
|
150
|
+
# Rope project settings
|
|
151
|
+
.ropeproject
|
|
152
|
+
|
|
153
|
+
# mkdocs documentation
|
|
154
|
+
/site
|
|
155
|
+
|
|
156
|
+
# mypy
|
|
157
|
+
.mypy_cache/
|
|
158
|
+
.dmypy.json
|
|
159
|
+
dmypy.json
|
|
160
|
+
|
|
161
|
+
# Pyre type checker
|
|
162
|
+
.pyre/
|
|
163
|
+
|
|
164
|
+
# pytype static type analyzer
|
|
165
|
+
.pytype/
|
|
166
|
+
|
|
167
|
+
# Cython debug symbols
|
|
168
|
+
cython_debug/
|
|
169
|
+
|
|
170
|
+
# PyCharm
|
|
171
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
172
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
173
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
174
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
175
|
+
#.idea/
|
|
176
|
+
|
|
177
|
+
# Abstra
|
|
178
|
+
# Abstra is an AI-powered process automation framework.
|
|
179
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
180
|
+
# Learn more at https://abstra.io/docs
|
|
181
|
+
.abstra/
|
|
182
|
+
|
|
183
|
+
# Visual Studio Code
|
|
184
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
185
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
186
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
187
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
188
|
+
# .vscode/
|
|
189
|
+
|
|
190
|
+
# Ruff stuff:
|
|
191
|
+
.ruff_cache/
|
|
192
|
+
|
|
193
|
+
# PyPI configuration file
|
|
194
|
+
.pypirc
|
|
195
|
+
|
|
196
|
+
# Cursor
|
|
197
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
198
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
199
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
200
|
+
.cursorignore
|
|
201
|
+
.cursorindexingignore
|
|
202
|
+
|
|
203
|
+
# Marimo
|
|
204
|
+
marimo/_static/
|
|
205
|
+
marimo/_lsp/
|
|
206
|
+
__marimo__/
|
|
207
|
+
|
|
208
|
+
.env
|
|
209
|
+
node_modules
|
|
210
|
+
chatkit.db
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: adk-chatkit
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Google ADK with openai chatkit
|
|
5
|
+
Project-URL: repository, https://github.com/ksachdeva/adk-chatkit
|
|
6
|
+
Author: Sachdeva, Kapil
|
|
7
|
+
Classifier: Intended Audience :: Developers
|
|
8
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
16
|
+
Requires-Python: >=3.11
|
|
17
|
+
Requires-Dist: openai-chatkit>=1.0.2
|
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "adk-chatkit"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "Google ADK with openai chatkit"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Sachdeva, Kapil"}
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"openai-chatkit>=1.0.2",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Programming Language :: Python",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.11",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
23
|
+
"License :: OSI Approved :: Apache Software License",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
repository = "https://github.com/ksachdeva/adk-chatkit"
|
|
28
|
+
|
|
29
|
+
[build-system]
|
|
30
|
+
requires = ["hatchling"]
|
|
31
|
+
build-backend = "hatchling.build"
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from .__about__ import __application__, __author__, __version__
|
|
2
|
+
from ._callbacks import remove_widgets_and_client_tool_calls
|
|
3
|
+
from ._client_tool_call import ClientToolCallState, add_client_tool_call_to_tool_response
|
|
4
|
+
from ._context import ADKContext
|
|
5
|
+
from ._response import stream_agent_response
|
|
6
|
+
from ._store import ADKStore
|
|
7
|
+
from ._widgets import add_widget_to_tool_response
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"__version__",
|
|
11
|
+
"__application__",
|
|
12
|
+
"__author__",
|
|
13
|
+
"ADKContext",
|
|
14
|
+
"ADKStore",
|
|
15
|
+
"stream_agent_response",
|
|
16
|
+
"ClientToolCallState",
|
|
17
|
+
"add_client_tool_call_to_tool_response",
|
|
18
|
+
"remove_widgets_and_client_tool_calls",
|
|
19
|
+
"add_widget_to_tool_response",
|
|
20
|
+
]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from google.adk.agents.callback_context import CallbackContext
|
|
2
|
+
from google.adk.models.llm_request import LlmRequest
|
|
3
|
+
from google.adk.models.llm_response import LlmResponse
|
|
4
|
+
|
|
5
|
+
from ._constants import CLIENT_TOOL_KEY_IN_TOOL_RESPONSE, WIDGET_KEY_IN_TOOL_RESPONSE
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def remove_widgets_and_client_tool_calls(
|
|
9
|
+
callback_context: CallbackContext, llm_request: LlmRequest
|
|
10
|
+
) -> LlmResponse | None:
|
|
11
|
+
for c in llm_request.contents:
|
|
12
|
+
if c.parts is None:
|
|
13
|
+
continue
|
|
14
|
+
for p in c.parts:
|
|
15
|
+
if not p.function_response:
|
|
16
|
+
continue
|
|
17
|
+
if p.function_response.response:
|
|
18
|
+
p.function_response.response.pop(WIDGET_KEY_IN_TOOL_RESPONSE, None)
|
|
19
|
+
p.function_response.response.pop(CLIENT_TOOL_KEY_IN_TOOL_RESPONSE, None)
|
|
20
|
+
|
|
21
|
+
return None
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from typing import Any, Literal
|
|
2
|
+
from uuid import uuid4
|
|
3
|
+
|
|
4
|
+
from google.adk.tools import ToolContext
|
|
5
|
+
from pydantic import BaseModel, Field
|
|
6
|
+
|
|
7
|
+
from ._constants import CHATKIT_THREAD_METADTA_KEY, CLIENT_TOOL_KEY_IN_TOOL_RESPONSE
|
|
8
|
+
from ._thread_utils import (
|
|
9
|
+
add_client_tool_status,
|
|
10
|
+
serialize_thread_metadata,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ClientToolCallState(BaseModel):
|
|
15
|
+
"""
|
|
16
|
+
Returned from tool methods to indicate a client-side tool call.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
id: str = Field(default_factory=lambda: uuid4().hex)
|
|
20
|
+
|
|
21
|
+
name: str
|
|
22
|
+
arguments: dict[str, Any]
|
|
23
|
+
status: Literal["pending", "completed"] = "pending"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def add_client_tool_call_to_tool_response(
|
|
27
|
+
response: dict[str, Any],
|
|
28
|
+
client_tool_call: ClientToolCallState,
|
|
29
|
+
tool_context: ToolContext,
|
|
30
|
+
) -> None:
|
|
31
|
+
"""Add a client tool call to a tool response dictionary.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
response: The tool response dictionary to modify.
|
|
35
|
+
client_tool_call: The client tool call state to add.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
thread_metadata = add_client_tool_status(
|
|
39
|
+
tool_context.state,
|
|
40
|
+
client_tool_call.id,
|
|
41
|
+
client_tool_call.status,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
# update the state
|
|
45
|
+
tool_context.state[CHATKIT_THREAD_METADTA_KEY] = serialize_thread_metadata(thread_metadata)
|
|
46
|
+
|
|
47
|
+
response[CLIENT_TOOL_KEY_IN_TOOL_RESPONSE] = client_tool_call
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
from collections.abc import AsyncGenerator, AsyncIterator
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
|
|
5
|
+
from chatkit.types import (
|
|
6
|
+
AssistantMessageContent,
|
|
7
|
+
AssistantMessageContentPartAdded,
|
|
8
|
+
AssistantMessageContentPartDone,
|
|
9
|
+
AssistantMessageContentPartTextDelta,
|
|
10
|
+
AssistantMessageItem,
|
|
11
|
+
ClientToolCallItem,
|
|
12
|
+
ThreadItemAddedEvent,
|
|
13
|
+
ThreadItemDoneEvent,
|
|
14
|
+
ThreadItemUpdated,
|
|
15
|
+
ThreadMetadata,
|
|
16
|
+
ThreadStreamEvent,
|
|
17
|
+
WidgetItem,
|
|
18
|
+
)
|
|
19
|
+
from google.adk.events import Event
|
|
20
|
+
|
|
21
|
+
from ._client_tool_call import ClientToolCallState
|
|
22
|
+
from ._constants import CLIENT_TOOL_KEY_IN_TOOL_RESPONSE, WIDGET_KEY_IN_TOOL_RESPONSE
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
async def stream_agent_response(
|
|
26
|
+
thread: ThreadMetadata,
|
|
27
|
+
adk_response: AsyncGenerator[Event, None],
|
|
28
|
+
) -> AsyncIterator[ThreadStreamEvent]:
|
|
29
|
+
if adk_response is None:
|
|
30
|
+
return
|
|
31
|
+
|
|
32
|
+
response_id = str(uuid.uuid4())
|
|
33
|
+
|
|
34
|
+
content_index = 0
|
|
35
|
+
async for event in adk_response:
|
|
36
|
+
if event.content is None:
|
|
37
|
+
# we need to throw item added event first
|
|
38
|
+
yield ThreadItemAddedEvent(
|
|
39
|
+
item=AssistantMessageItem(
|
|
40
|
+
id=response_id,
|
|
41
|
+
content=[],
|
|
42
|
+
thread_id=thread.id,
|
|
43
|
+
created_at=datetime.fromtimestamp(event.timestamp),
|
|
44
|
+
)
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
# and also yield an empty part added event
|
|
48
|
+
yield ThreadItemUpdated(
|
|
49
|
+
item_id=response_id,
|
|
50
|
+
update=AssistantMessageContentPartAdded(
|
|
51
|
+
content_index=content_index,
|
|
52
|
+
content=AssistantMessageContent(text=""),
|
|
53
|
+
),
|
|
54
|
+
)
|
|
55
|
+
else:
|
|
56
|
+
# Since Widgets are recorded in the function responses
|
|
57
|
+
# they are handled here
|
|
58
|
+
if fn_responses := event.get_function_responses():
|
|
59
|
+
for fn_response in fn_responses:
|
|
60
|
+
if not fn_response.response:
|
|
61
|
+
continue
|
|
62
|
+
widget = fn_response.response.get(WIDGET_KEY_IN_TOOL_RESPONSE, None)
|
|
63
|
+
if widget:
|
|
64
|
+
# No Streaming for Widgets for now
|
|
65
|
+
yield ThreadItemDoneEvent(
|
|
66
|
+
item=WidgetItem(
|
|
67
|
+
id=str(uuid.uuid4()),
|
|
68
|
+
thread_id=thread.id,
|
|
69
|
+
created_at=datetime.fromtimestamp(event.timestamp),
|
|
70
|
+
widget=widget,
|
|
71
|
+
)
|
|
72
|
+
)
|
|
73
|
+
adk_client_tool: ClientToolCallState | None = fn_response.response.get(
|
|
74
|
+
CLIENT_TOOL_KEY_IN_TOOL_RESPONSE, None
|
|
75
|
+
)
|
|
76
|
+
if adk_client_tool:
|
|
77
|
+
yield ThreadItemDoneEvent(
|
|
78
|
+
item=ClientToolCallItem(
|
|
79
|
+
id=event.id,
|
|
80
|
+
thread_id=thread.id,
|
|
81
|
+
name=adk_client_tool.name,
|
|
82
|
+
arguments=adk_client_tool.arguments,
|
|
83
|
+
status=adk_client_tool.status,
|
|
84
|
+
created_at=datetime.fromtimestamp(event.timestamp),
|
|
85
|
+
call_id=adk_client_tool.id,
|
|
86
|
+
),
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
if event.content.parts:
|
|
90
|
+
text_from_final_update = ""
|
|
91
|
+
for p in event.content.parts:
|
|
92
|
+
if p.text:
|
|
93
|
+
update: AssistantMessageContentPartTextDelta | AssistantMessageContentPartDone
|
|
94
|
+
if event.partial:
|
|
95
|
+
update = AssistantMessageContentPartTextDelta(
|
|
96
|
+
delta=p.text,
|
|
97
|
+
content_index=content_index,
|
|
98
|
+
)
|
|
99
|
+
else:
|
|
100
|
+
update = AssistantMessageContentPartDone(
|
|
101
|
+
content=AssistantMessageContent(text=p.text),
|
|
102
|
+
content_index=content_index,
|
|
103
|
+
)
|
|
104
|
+
text_from_final_update = p.text
|
|
105
|
+
|
|
106
|
+
yield ThreadItemUpdated(
|
|
107
|
+
item_id=response_id,
|
|
108
|
+
update=update,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
yield ThreadItemDoneEvent(
|
|
112
|
+
item=AssistantMessageItem(
|
|
113
|
+
id=response_id,
|
|
114
|
+
content=[AssistantMessageContent(text=text_from_final_update)],
|
|
115
|
+
thread_id=thread.id,
|
|
116
|
+
created_at=datetime.fromtimestamp(event.timestamp),
|
|
117
|
+
)
|
|
118
|
+
)
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
from uuid import uuid4
|
|
3
|
+
|
|
4
|
+
from chatkit.store import Store
|
|
5
|
+
from chatkit.types import (
|
|
6
|
+
AssistantMessageContent,
|
|
7
|
+
AssistantMessageItem,
|
|
8
|
+
Attachment,
|
|
9
|
+
ClientToolCallItem,
|
|
10
|
+
InferenceOptions,
|
|
11
|
+
Page,
|
|
12
|
+
ThreadItem,
|
|
13
|
+
ThreadMetadata,
|
|
14
|
+
UserMessageContent,
|
|
15
|
+
UserMessageItem,
|
|
16
|
+
UserMessageTextContent,
|
|
17
|
+
WidgetItem,
|
|
18
|
+
)
|
|
19
|
+
from chatkit.widgets import Card
|
|
20
|
+
from google.adk.events import Event, EventActions
|
|
21
|
+
from google.adk.sessions import BaseSessionService
|
|
22
|
+
from google.adk.sessions.base_session_service import ListSessionsResponse
|
|
23
|
+
|
|
24
|
+
from ._client_tool_call import ClientToolCallState
|
|
25
|
+
from ._constants import CHATKIT_THREAD_METADTA_KEY, CLIENT_TOOL_KEY_IN_TOOL_RESPONSE, WIDGET_KEY_IN_TOOL_RESPONSE
|
|
26
|
+
from ._context import ADKContext
|
|
27
|
+
from ._thread_utils import (
|
|
28
|
+
add_client_tool_status,
|
|
29
|
+
get_client_tool_status,
|
|
30
|
+
get_thread_metadata_from_state,
|
|
31
|
+
serialize_thread_metadata,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _to_user_message_content(event: Event) -> list[UserMessageContent]:
|
|
36
|
+
if not event.content or not event.content.parts:
|
|
37
|
+
return []
|
|
38
|
+
|
|
39
|
+
contents: list[UserMessageContent] = []
|
|
40
|
+
for part in event.content.parts:
|
|
41
|
+
if part.text:
|
|
42
|
+
contents.append(UserMessageTextContent(text=part.text))
|
|
43
|
+
|
|
44
|
+
return contents
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _to_assistant_message_content(event: Event) -> list[AssistantMessageContent]:
|
|
48
|
+
if not event.content or not event.content.parts:
|
|
49
|
+
return []
|
|
50
|
+
|
|
51
|
+
contents: list[AssistantMessageContent] = []
|
|
52
|
+
for part in event.content.parts:
|
|
53
|
+
if part.text:
|
|
54
|
+
contents.append(AssistantMessageContent(text=part.text))
|
|
55
|
+
|
|
56
|
+
return contents
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class ADKStore(Store[ADKContext]):
|
|
60
|
+
def __init__(self, session_service: BaseSessionService) -> None:
|
|
61
|
+
self._session_service = session_service
|
|
62
|
+
|
|
63
|
+
async def load_thread(self, thread_id: str, context: ADKContext) -> ThreadMetadata:
|
|
64
|
+
session = await self._session_service.get_session(
|
|
65
|
+
app_name=context["app_name"],
|
|
66
|
+
user_id=context["user_id"],
|
|
67
|
+
session_id=thread_id,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
if not session:
|
|
71
|
+
raise ValueError(
|
|
72
|
+
f"Session with id {thread_id} not found for user {context['user_id']} in app {context['app_name']}"
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
return get_thread_metadata_from_state(session.state)
|
|
76
|
+
|
|
77
|
+
async def save_thread(self, thread: ThreadMetadata, context: ADKContext) -> None:
|
|
78
|
+
session = await self._session_service.get_session(
|
|
79
|
+
app_name=context["app_name"],
|
|
80
|
+
user_id=context["user_id"],
|
|
81
|
+
session_id=thread.id,
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
if not session:
|
|
85
|
+
session = await self._session_service.create_session(
|
|
86
|
+
app_name=context["app_name"],
|
|
87
|
+
user_id=context["user_id"],
|
|
88
|
+
session_id=thread.id,
|
|
89
|
+
state={CHATKIT_THREAD_METADTA_KEY: serialize_thread_metadata(thread)},
|
|
90
|
+
)
|
|
91
|
+
else:
|
|
92
|
+
state_delta = {
|
|
93
|
+
CHATKIT_THREAD_METADTA_KEY: serialize_thread_metadata(thread),
|
|
94
|
+
}
|
|
95
|
+
actions_with_update = EventActions(state_delta=state_delta)
|
|
96
|
+
system_event = Event(
|
|
97
|
+
invocation_id=uuid4().hex,
|
|
98
|
+
author="system",
|
|
99
|
+
actions=actions_with_update,
|
|
100
|
+
timestamp=datetime.now().timestamp(),
|
|
101
|
+
)
|
|
102
|
+
await self._session_service.append_event(session, system_event)
|
|
103
|
+
|
|
104
|
+
async def load_thread_items(
|
|
105
|
+
self,
|
|
106
|
+
thread_id: str,
|
|
107
|
+
after: str | None,
|
|
108
|
+
limit: int,
|
|
109
|
+
order: str,
|
|
110
|
+
context: ADKContext,
|
|
111
|
+
) -> Page[ThreadItem]:
|
|
112
|
+
session = await self._session_service.get_session(
|
|
113
|
+
app_name=context["app_name"],
|
|
114
|
+
user_id=context["user_id"],
|
|
115
|
+
session_id=thread_id,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
if not session:
|
|
119
|
+
raise ValueError(
|
|
120
|
+
f"Session with id {thread_id} not found for user {context['user_id']} in app {context['app_name']}"
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
thread_items: list[ThreadItem] = []
|
|
124
|
+
for event in session.events:
|
|
125
|
+
an_item: ThreadItem | None = None
|
|
126
|
+
if event.author == "user":
|
|
127
|
+
an_item = UserMessageItem(
|
|
128
|
+
id=event.id,
|
|
129
|
+
thread_id=thread_id,
|
|
130
|
+
created_at=datetime.fromtimestamp(event.timestamp),
|
|
131
|
+
content=_to_user_message_content(event),
|
|
132
|
+
attachments=[],
|
|
133
|
+
inference_options=InferenceOptions(),
|
|
134
|
+
)
|
|
135
|
+
else:
|
|
136
|
+
# we should only send the message if it has content
|
|
137
|
+
# that is not function calls or response
|
|
138
|
+
text_message_content = _to_assistant_message_content(event)
|
|
139
|
+
|
|
140
|
+
if text_message_content:
|
|
141
|
+
an_item = AssistantMessageItem(
|
|
142
|
+
id=event.id,
|
|
143
|
+
thread_id=thread_id,
|
|
144
|
+
created_at=datetime.fromtimestamp(event.timestamp),
|
|
145
|
+
content=text_message_content,
|
|
146
|
+
)
|
|
147
|
+
else:
|
|
148
|
+
# let's see if this a function call response
|
|
149
|
+
# with a widget. If yes, then we will tranmist WidgetItem
|
|
150
|
+
if fn_responses := event.get_function_responses():
|
|
151
|
+
for fn_response in fn_responses:
|
|
152
|
+
if not fn_response.response:
|
|
153
|
+
continue
|
|
154
|
+
# let's check for widget in the response
|
|
155
|
+
widget = fn_response.response.get(WIDGET_KEY_IN_TOOL_RESPONSE, None)
|
|
156
|
+
if widget:
|
|
157
|
+
an_item = WidgetItem(
|
|
158
|
+
id=event.id,
|
|
159
|
+
thread_id=thread_id,
|
|
160
|
+
created_at=datetime.fromtimestamp(event.timestamp),
|
|
161
|
+
widget=Card.model_validate(widget),
|
|
162
|
+
)
|
|
163
|
+
# let's check for adk-client-tool in the response
|
|
164
|
+
adk_client_tool = fn_response.response.get(CLIENT_TOOL_KEY_IN_TOOL_RESPONSE, None)
|
|
165
|
+
if adk_client_tool:
|
|
166
|
+
adk_client_tool = ClientToolCallState.model_validate(adk_client_tool)
|
|
167
|
+
status = get_client_tool_status(
|
|
168
|
+
session.state,
|
|
169
|
+
adk_client_tool.id,
|
|
170
|
+
)
|
|
171
|
+
if status:
|
|
172
|
+
an_item = ClientToolCallItem(
|
|
173
|
+
id=event.id,
|
|
174
|
+
thread_id=thread_id,
|
|
175
|
+
name=adk_client_tool.name,
|
|
176
|
+
arguments=adk_client_tool.arguments,
|
|
177
|
+
status=status, # type: ignore
|
|
178
|
+
created_at=datetime.fromtimestamp(event.timestamp),
|
|
179
|
+
call_id=adk_client_tool.id,
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
if an_item:
|
|
183
|
+
thread_items.append(an_item)
|
|
184
|
+
|
|
185
|
+
return Page(data=thread_items)
|
|
186
|
+
|
|
187
|
+
async def add_thread_item(self, thread_id: str, item: ThreadItem, context: ADKContext) -> None:
|
|
188
|
+
# items are added to the session by runner
|
|
189
|
+
pass
|
|
190
|
+
|
|
191
|
+
async def save_attachment(self, attachment: Attachment, context: ADKContext) -> None:
|
|
192
|
+
raise NotImplementedError()
|
|
193
|
+
|
|
194
|
+
async def load_attachment(self, attachment_id: str, context: ADKContext) -> Attachment:
|
|
195
|
+
raise NotImplementedError()
|
|
196
|
+
|
|
197
|
+
async def delete_attachment(self, attachment_id: str, context: ADKContext) -> None:
|
|
198
|
+
raise NotImplementedError()
|
|
199
|
+
|
|
200
|
+
async def delete_thread_item(self, thread_id: str, item_id: str, context: ADKContext) -> None:
|
|
201
|
+
# simply ignoring it for now (ClientToolCallItem is typically not deleted because of this)
|
|
202
|
+
pass
|
|
203
|
+
|
|
204
|
+
async def delete_thread(self, thread_id: str, context: ADKContext) -> None:
|
|
205
|
+
raise NotImplementedError()
|
|
206
|
+
|
|
207
|
+
async def save_item(self, thread_id: str, item: ThreadItem, context: ADKContext) -> None:
|
|
208
|
+
# we will only handle specify types of items here
|
|
209
|
+
# as quite many are automatically handled by runner
|
|
210
|
+
if isinstance(item, ClientToolCallItem):
|
|
211
|
+
session = await self._session_service.get_session(
|
|
212
|
+
app_name=context["app_name"],
|
|
213
|
+
user_id=context["user_id"],
|
|
214
|
+
session_id=thread_id,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
if not session:
|
|
218
|
+
raise ValueError(
|
|
219
|
+
f"Session with id {thread_id} not found for user {context['user_id']} in app {context['app_name']}"
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
thread_metadata = add_client_tool_status(session.state, item.call_id, item.status)
|
|
223
|
+
|
|
224
|
+
state_delta = {
|
|
225
|
+
CHATKIT_THREAD_METADTA_KEY: serialize_thread_metadata(thread_metadata),
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
actions_with_update = EventActions(state_delta=state_delta)
|
|
229
|
+
system_event = Event(
|
|
230
|
+
invocation_id=uuid4().hex,
|
|
231
|
+
author="system",
|
|
232
|
+
actions=actions_with_update,
|
|
233
|
+
timestamp=datetime.now().timestamp(),
|
|
234
|
+
)
|
|
235
|
+
await self._session_service.append_event(session, system_event)
|
|
236
|
+
|
|
237
|
+
async def load_item(self, thread_id: str, item_id: str, context: ADKContext) -> ThreadItem:
|
|
238
|
+
raise NotImplementedError()
|
|
239
|
+
|
|
240
|
+
async def load_threads(
|
|
241
|
+
self,
|
|
242
|
+
limit: int,
|
|
243
|
+
after: str | None,
|
|
244
|
+
order: str,
|
|
245
|
+
context: ADKContext,
|
|
246
|
+
) -> Page[ThreadMetadata]:
|
|
247
|
+
sessions_response: ListSessionsResponse = await self._session_service.list_sessions(
|
|
248
|
+
app_name=context["app_name"],
|
|
249
|
+
user_id=context["user_id"],
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
items: list[ThreadMetadata] = []
|
|
253
|
+
|
|
254
|
+
for session in sessions_response.sessions:
|
|
255
|
+
thread_metadata = get_thread_metadata_from_state(session.state)
|
|
256
|
+
items.append(thread_metadata)
|
|
257
|
+
|
|
258
|
+
return Page(data=items)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from chatkit.types import ThreadMetadata
|
|
5
|
+
from google.adk.sessions.state import State
|
|
6
|
+
|
|
7
|
+
from ._constants import CHATKIT_THREAD_METADTA_KEY
|
|
8
|
+
|
|
9
|
+
_CLIENT_TOOL_PREFIX = "client-tool"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def serialize_thread_metadata(thread: ThreadMetadata) -> dict[str, Any]:
|
|
13
|
+
json_dump = thread.model_dump_json(exclude_none=True, exclude={"items"})
|
|
14
|
+
return json.loads(json_dump) # type: ignore
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def get_thread_metadata_from_state(state: State | dict[str, Any]) -> ThreadMetadata:
|
|
18
|
+
thread_metadata_dict = state[CHATKIT_THREAD_METADTA_KEY]
|
|
19
|
+
return ThreadMetadata.model_validate(thread_metadata_dict)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def add_client_tool_status(
|
|
23
|
+
state: State | dict[str, Any],
|
|
24
|
+
client_tool_id: str,
|
|
25
|
+
status: str,
|
|
26
|
+
) -> ThreadMetadata:
|
|
27
|
+
thread_metadata = get_thread_metadata_from_state(state)
|
|
28
|
+
thread_metadata.metadata[f"{_CLIENT_TOOL_PREFIX}-{client_tool_id}"] = status
|
|
29
|
+
return thread_metadata
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def get_client_tool_status(
|
|
33
|
+
state: State | dict[str, Any],
|
|
34
|
+
client_tool_id: str,
|
|
35
|
+
) -> str | None:
|
|
36
|
+
thread_metadata = get_thread_metadata_from_state(state)
|
|
37
|
+
return thread_metadata.metadata.get(f"{_CLIENT_TOOL_PREFIX}-{client_tool_id}", None)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from chatkit.widgets import WidgetRoot
|
|
4
|
+
|
|
5
|
+
from ._constants import WIDGET_KEY_IN_TOOL_RESPONSE
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def add_widget_to_tool_response(
|
|
9
|
+
response: dict[str, Any],
|
|
10
|
+
widget: WidgetRoot,
|
|
11
|
+
) -> None:
|
|
12
|
+
"""Add a widget to a tool response dictionary.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
response: The tool response dictionary to modify.
|
|
16
|
+
widget: The widget to add.
|
|
17
|
+
"""
|
|
18
|
+
response[WIDGET_KEY_IN_TOOL_RESPONSE] = widget
|