Skip to content

Commit 2f8eaff

Browse files
committed
feat: add utility functions to stream widgets, events and client tools / update version to 0.0.5
1 parent a6994f6 commit 2f8eaff

File tree

8 files changed

+61
-25
lines changed

8 files changed

+61
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Make sure you open this repository in vscode `devcontainer` and all dependencies
3131
cp .env.example .env
3232
```
3333

34-
There is one backend and one frontend that hosts 3 agents (chatkit servers) and their corresponding user interface.
34+
There is one backend and one frontend that hosts 4 agents (chatkit servers) and their corresponding user interface.
3535

3636
```bash
3737
# Run the backend

adk-chatkit/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "adk-chatkit"
3-
version = "0.0.4"
3+
version = "0.0.5"
44
description = "OpenAI chatkit backend support for Google ADK"
55
readme = "README.md"
66
authors = [
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
__author__ = "Kapil Sachdeva"
22
__application__ = "adk-chatkit"
3-
__version__ = "0.0.4"
3+
__version__ = "0.0.5"

adk-chatkit/src/adk_chatkit/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .__about__ import __application__, __author__, __version__
22
from ._client_tool_call import ClientToolCallState
3-
from ._context import ADKAgentContext, ADKContext, ChatkitRunConfig
3+
from ._context import ADKAgentContext, ADKContext, ChatkitRunConfig, issue_client_tool_call, stream_event, stream_widget
44
from ._response import stream_agent_response
55
from ._store import ADKStore
66
from ._widgets import serialize_widget_item
@@ -16,4 +16,7 @@
1616
"ClientToolCallState",
1717
"ChatkitRunConfig",
1818
"serialize_widget_item",
19+
"issue_client_tool_call",
20+
"stream_widget",
21+
"stream_event",
1922
]

adk-chatkit/src/adk_chatkit/_context.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,48 @@ def _complete(self) -> None:
6363

6464
class ChatkitRunConfig(RunConfig):
6565
context: ADKAgentContext
66+
67+
68+
async def stream_event(event: ThreadStreamEvent, tool_context: ToolContext) -> None:
69+
"""Stream an event to the chat interface.
70+
71+
Args:
72+
event: The event to stream.
73+
tool_context: The tool context associated with the event.
74+
"""
75+
chatkit_run_config = tool_context._invocation_context.run_config
76+
if not isinstance(chatkit_run_config, ChatkitRunConfig):
77+
raise ValueError("Make sure to set run_config for runner to ChatkitRunConfig")
78+
79+
await chatkit_run_config.context.stream(event)
80+
81+
82+
async def stream_widget(widget: WidgetRoot, tool_context: ToolContext) -> None:
83+
"""Stream a widget to the chat interface.
84+
85+
Args:
86+
widget: The widget to stream.
87+
tool_context: The tool context associated with the widget.
88+
"""
89+
chatkit_run_config = tool_context._invocation_context.run_config
90+
if not isinstance(chatkit_run_config, ChatkitRunConfig):
91+
raise ValueError("Make sure to set run_config for runner to ChatkitRunConfig")
92+
93+
await chatkit_run_config.context.stream_widget(widget, tool_context)
94+
95+
96+
async def issue_client_tool_call(
97+
client_tool_call: ClientToolCallState,
98+
tool_context: ToolContext,
99+
) -> None:
100+
"""Issue a client tool call to the chat interface.
101+
102+
Args:
103+
client_tool_call: The client tool call state to issue.
104+
tool_context: The tool context associated with the client tool call.
105+
"""
106+
chatkit_run_config = tool_context._invocation_context.run_config
107+
if not isinstance(chatkit_run_config, ChatkitRunConfig):
108+
raise ValueError("Make sure to set run_config for runner to ChatkitRunConfig")
109+
110+
await chatkit_run_config.context.issue_client_tool_call(client_tool_call, tool_context)

examples/backend/src/backend/agents/facts/_tools.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import Any, Final, Literal, cast
1+
from typing import Any, Final, Literal
22

3-
from adk_chatkit import ChatkitRunConfig, ClientToolCallState
3+
from adk_chatkit import ClientToolCallState, issue_client_tool_call, stream_widget
44
from google.adk.tools import ToolContext
55

66
from ._sample_widget import render_weather_widget
@@ -50,9 +50,7 @@ async def save_fact(
5050

5151
result = {"fact_id": confirmed.id, "status": "saved"}
5252

53-
# add_client_tool_call_to_tool_response(result, client_tool_call, tool_context)
54-
chatkit_run_config = cast(ChatkitRunConfig, tool_context._invocation_context.run_config)
55-
await chatkit_run_config.context.issue_client_tool_call(client_tool_call, tool_context)
53+
await issue_client_tool_call(client_tool_call, tool_context)
5654

5755
return result
5856

@@ -75,9 +73,7 @@ async def switch_theme(theme: str, tool_context: ToolContext) -> dict[str, str]:
7573

7674
result = {"theme": requested}
7775

78-
# add_client_tool_call_to_tool_response(result, client_tool_call, tool_context)
79-
chatkit_run_config = cast(ChatkitRunConfig, tool_context._invocation_context.run_config)
80-
await chatkit_run_config.context.issue_client_tool_call(client_tool_call, tool_context)
76+
await issue_client_tool_call(client_tool_call, tool_context)
8177

8278
return result
8379

@@ -131,10 +127,8 @@ async def get_weather(
131127
if observed:
132128
result["observation_time"] = observed
133129

134-
chatkit_run_config = cast(ChatkitRunConfig, tool_context._invocation_context.run_config)
135-
136130
try:
137-
await chatkit_run_config.context.stream_widget(widget, tool_context)
131+
await stream_widget(widget, tool_context)
138132
except Exception as exc: # noqa: BLE001
139133
print("[WeatherTool] widget stream failed", {"error": str(exc)})
140134
raise ValueError("Weather data is currently unavailable for that location.") from exc
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
from typing import cast
33

4-
from adk_chatkit import ChatkitRunConfig
4+
from adk_chatkit import ChatkitRunConfig, stream_event, stream_widget
55
from chatkit.types import ProgressUpdateEvent
66
from google.adk.tools import ToolContext
77

@@ -13,17 +13,11 @@ async def render_tasks_widget(tool_context: ToolContext) -> dict[str, str]:
1313

1414
result = dict(success="true")
1515

16-
# we are fetching the list of tasks
17-
run_config = tool_context._invocation_context.run_config
18-
assert run_config is not None
19-
20-
chatkit_run_config = cast(ChatkitRunConfig, run_config)
21-
22-
await chatkit_run_config.context.stream(ProgressUpdateEvent(text="Fetching tasks widget..."))
16+
await stream_event(ProgressUpdateEvent(text="Fetching tasks widget..."), tool_context)
2317
await asyncio.sleep(2)
2418

2519
widget = make_widget()
2620

27-
await chatkit_run_config.context.stream_widget(widget, tool_context)
21+
await stream_widget(widget, tool_context)
2822

2923
return result

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)