forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest_confirmation.py
More file actions
169 lines (143 loc) · 5.67 KB
/
request_confirmation.py
File metadata and controls
169 lines (143 loc) · 5.67 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
# 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.
from __future__ import annotations
import json
import logging
from typing import AsyncGenerator
from typing import TYPE_CHECKING
from google.genai import types
from typing_extensions import override
from . import functions
from ...agents.invocation_context import InvocationContext
from ...agents.readonly_context import ReadonlyContext
from ...events.event import Event
from ...models.llm_request import LlmRequest
from ...tools.tool_confirmation import ToolConfirmation
from ._base_llm_processor import BaseLlmRequestProcessor
from .functions import REQUEST_CONFIRMATION_FUNCTION_CALL_NAME
if TYPE_CHECKING:
from ...agents.llm_agent import LlmAgent
logger = logging.getLogger('google_adk.' + __name__)
class _RequestConfirmationLlmRequestProcessor(BaseLlmRequestProcessor):
"""Handles tool confirmation information to build the LLM request."""
@override
async def run_async(
self, invocation_context: InvocationContext, llm_request: LlmRequest
) -> AsyncGenerator[Event, None]:
from ...agents.llm_agent import LlmAgent
agent = invocation_context.agent
# Only look at events in the current branch.
events = invocation_context._get_events(current_branch=True)
if not events:
return
request_confirmation_function_responses = (
dict()
) # {function call id, tool confirmation}
confirmation_event_index = -1
for k in range(len(events) - 1, -1, -1):
event = events[k]
# Find the first event authored by user
if not event.author or event.author != 'user':
continue
responses = event.get_function_responses()
if not responses:
return
for function_response in responses:
if function_response.name != REQUEST_CONFIRMATION_FUNCTION_CALL_NAME:
continue
# Find the FunctionResponse event that contains the user provided tool
# confirmation
if (
function_response.response
and len(function_response.response.values()) == 1
and 'response' in function_response.response.keys()
):
# ADK web client will send a request that is always encapsulated in a
# 'response' key.
tool_confirmation = ToolConfirmation.model_validate(
json.loads(function_response.response['response'])
)
else:
tool_confirmation = ToolConfirmation.model_validate(
function_response.response
)
request_confirmation_function_responses[function_response.id] = (
tool_confirmation
)
confirmation_event_index = k
break
if not request_confirmation_function_responses:
return
for i in range(len(events) - 2, -1, -1):
event = events[i]
# Find the system generated FunctionCall event requesting the tool
# confirmation
function_calls = event.get_function_calls()
if not function_calls:
continue
tools_to_resume_with_confirmation = (
dict()
) # {Function call id, tool confirmation}
tools_to_resume_with_args = dict() # {Function call id, function calls}
for function_call in function_calls:
if (
function_call.id
not in request_confirmation_function_responses.keys()
):
continue
args = function_call.args
if 'originalFunctionCall' not in args:
continue
original_function_call = types.FunctionCall(
**args['originalFunctionCall']
)
tools_to_resume_with_confirmation[original_function_call.id] = (
request_confirmation_function_responses[function_call.id]
)
tools_to_resume_with_args[original_function_call.id] = (
original_function_call
)
if not tools_to_resume_with_confirmation:
continue
# Remove the tools that have already been confirmed.
for i in range(len(events) - 1, confirmation_event_index, -1):
event = events[i]
function_response = event.get_function_responses()
if not function_response:
continue
for function_response in event.get_function_responses():
if function_response.id in tools_to_resume_with_confirmation:
tools_to_resume_with_confirmation.pop(function_response.id)
tools_to_resume_with_args.pop(function_response.id)
if not tools_to_resume_with_confirmation:
break
if not tools_to_resume_with_confirmation:
continue
if function_response_event := await functions.handle_function_call_list_async(
invocation_context,
tools_to_resume_with_args.values(),
{
tool.name: tool
for tool in await agent.canonical_tools(
ReadonlyContext(invocation_context)
)
},
# There could be parallel function calls that require input
# response would be a dict keyed by function call id
tools_to_resume_with_confirmation.keys(),
tools_to_resume_with_confirmation,
):
yield function_response_event
return
request_processor = _RequestConfirmationLlmRequestProcessor()