forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
253 lines (204 loc) · 9.49 KB
/
Copy path__init__.py
File metadata and controls
253 lines (204 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Temporal Integration for ADK.
This module provides the necessary components to run ADK Agents within Temporal Workflows.
"""
from __future__ import annotations
import uuid
import dataclasses
import inspect
import functools
import time
import asyncio
from datetime import timedelta
from typing import Callable, Any, Optional, List, AsyncGenerator
from collections.abc import Sequence
from temporalio import workflow, activity
from temporalio.common import RetryPolicy, RawValue
from temporalio.worker import WorkflowRunner
from temporalio.worker import UnsandboxedWorkflowRunner
from temporalio.converter import DataConverter, DefaultPayloadConverter
from temporalio.plugin import SimplePlugin
from temporalio.contrib.pydantic import PydanticPayloadConverter as _DefaultPydanticPayloadConverter
from google.adk.plugins import BasePlugin
from google.adk.models import LLMRegistry, BaseLlm, LlmRequest, LlmResponse
from google.adk.agents.invocation_context import InvocationContext
from temporalio.worker import (
WorkflowInboundInterceptor,
Interceptor,
ExecuteWorkflowInput,
WorkflowInterceptorClassInput
)
from google.adk.agents.callback_context import CallbackContext
from google.genai import types
def setup_deterministic_runtime():
"""Configures ADK runtime for Temporal determinism.
This should be called at the start of a Temporal Workflow before any ADK components
(like SessionService) are used, if they rely on runtime.get_time() or runtime.new_uuid().
"""
try:
from google.adk import runtime
# Define safer, context-aware providers
def _deterministic_time_provider() -> float:
if workflow.in_workflow():
return workflow.now().timestamp()
return time.time()
def _deterministic_id_provider() -> str:
if workflow.in_workflow():
return str(workflow.uuid4())
return str(uuid.uuid4())
runtime.set_time_provider(_deterministic_time_provider)
runtime.set_id_provider(_deterministic_id_provider)
except ImportError:
pass
except Exception as e:
print(f"Warning: Failed to set deterministic runtime providers: {e}")
class AdkWorkflowInboundInterceptor(WorkflowInboundInterceptor):
async def execute_workflow(self, input: ExecuteWorkflowInput) -> Any:
# Global runtime setup before ANY user code runs
setup_deterministic_runtime()
return await super().execute_workflow(input)
class AdkInterceptor(Interceptor):
def workflow_interceptor_class(
self, input: WorkflowInterceptorClassInput
) -> type[WorkflowInboundInterceptor] | None:
return AdkWorkflowInboundInterceptor
class AgentPlugin(BasePlugin):
"""ADK Plugin for Temporal integration.
This plugin automatically configures the ADK runtime to be deterministic when running
inside a Temporal workflow, and intercepts model calls to execute them as Temporal Activities.
"""
def __init__(self, activity_options: Optional[dict[str, Any]] = None):
"""Initializes the Temporal Plugin.
Args:
activity_options: Default options for model activities (e.g. start_to_close_timeout).
"""
super().__init__(name="temporal_plugin")
self.activity_options = activity_options or {}
@staticmethod
def activity_tool(activity_def: Callable, **kwargs: Any) -> Callable:
"""Decorator/Wrapper to wrap a Temporal Activity as an ADK Tool.
This ensures the activity's signature is preserved for ADK's tool schema generation
while marking it as a tool that executes via 'workflow.execute_activity'.
"""
async def wrapper(*args, **kw):
# Inspect signature to bind arguments
sig = inspect.signature(activity_def)
bound = sig.bind(*args, **kw)
bound.apply_defaults()
# Convert to positional args for Temporal
activity_args = list(bound.arguments.values())
# Decorator kwargs are defaults.
options = kwargs.copy()
return await workflow.execute_activity(
activity_def,
*activity_args,
**options
)
# Copy metadata
wrapper.__name__ = activity_def.__name__
wrapper.__doc__ = activity_def.__doc__
wrapper.__signature__ = inspect.signature(activity_def)
return wrapper
async def before_model_callback(
self, *, callback_context: CallbackContext, llm_request: LlmRequest
) -> LlmResponse | None:
# Construct dynamic activity name for visibility
agent_name = callback_context.agent_name
activity_name = f"{agent_name}.generate_content"
# Execute with dynamic name
response_dicts = await workflow.execute_activity(
activity_name,
args=[llm_request],
**self.activity_options
)
# Rehydrate LlmResponse objects safely
responses = []
for d in response_dicts:
try:
responses.append(LlmResponse.model_validate(d))
except Exception as e:
raise RuntimeError(f"Failed to deserialized LlmResponse from activity result: {e}") from e
# Simple consolidation: return the last complete response
return responses[-1] if responses else None
class WorkerPlugin(SimplePlugin):
"""A Temporal Worker Plugin configured for ADK.
This plugin configures:
1. Pydantic Payload Converter (required for ADK objects).
2. Sandbox Passthrough for `google.adk` and `google.genai`.
"""
def __init__(self):
super().__init__(
name="adk_worker_plugin",
data_converter=self._configure_data_converter,
workflow_runner=self._configure_workflow_runner,
activities=[self.dynamic_activity],
worker_interceptors=[AdkInterceptor()]
)
@staticmethod
@activity.defn(dynamic=True)
async def dynamic_activity(args: Sequence[RawValue]) -> Any:
"""Handles dynamic ADK activities (e.g. 'AgentName.generate_content')."""
activity_type = activity.info().activity_type
# Check if this is a generate_content call
if activity_type.endswith(".generate_content") or activity_type == "google.adk.generate_content":
return await WorkerPlugin._handle_generate_content(args)
raise ValueError(f"Unknown dynamic activity: {activity_type}")
@staticmethod
async def _handle_generate_content(args: List[Any]) -> list[dict[str, Any]]:
"""Implementation of content generation."""
# 1. Decode Arguments
# Dynamic activities receive RawValue wrappers (which host the Payload).
# We must manually decode them using the activity's configured data converter.
converter = activity.payload_converter()
# We expect a single argument: LlmRequest
if not args:
raise ValueError("Missing llm_request argument for generate_content")
# Extract payloads from RawValue wrappers
payloads = [arg.payload for arg in args]
# Decode
# from_payloads returns a list of decoded objects.
# We specify the types we expect for each argument.
try:
decoded_args = converter.from_payloads(payloads, [LlmRequest])
llm_request: LlmRequest = decoded_args[0]
except Exception as e:
activity.logger.error(f"Failed to decode arguments: {e}")
raise ValueError(f"Argument decoding failed: {e}") from e
# 3. Model Initialization
llm = LLMRegistry.new_llm(llm_request.model)
if not llm:
raise ValueError(f"Failed to create LLM for model: {llm_request.model}")
# 4. Execution
responses = [response async for response in llm.generate_content_async(llm_request=llm_request)]
# 5. Serialization
# Return dicts to avoid Pydantic strictness issues on rehydration
return [
r.model_dump(mode='json', by_alias=True)
for r in responses
]
def _configure_data_converter(self, converter: DataConverter | None) -> DataConverter:
if converter is None:
return DataConverter(
payload_converter_class=_DefaultPydanticPayloadConverter
)
elif converter.payload_converter_class is DefaultPayloadConverter:
return dataclasses.replace(
converter, payload_converter_class=_DefaultPydanticPayloadConverter
)
return converter
def _configure_workflow_runner(self, runner: WorkflowRunner | None) -> WorkflowRunner:
from temporalio.worker import UnsandboxedWorkflowRunner
# TODO: Not sure implications here. is this a good default an allow user override?
return UnsandboxedWorkflowRunner()