|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 | import os |
15 | | -from typing import Literal, Optional, Union |
| 15 | +from typing import Literal, Optional, Union, Any |
16 | 16 | from google import genai |
17 | 17 | from google.genai import types |
18 | 18 | import termcolor |
@@ -99,6 +99,7 @@ def __init__( |
99 | 99 | ), |
100 | 100 | types.Tool(function_declarations=custom_functions), |
101 | 101 | ], |
| 102 | + thinking_config=types.ThinkingConfig(include_thoughts=True), |
102 | 103 | ) |
103 | 104 |
|
104 | 105 | def handle_action(self, action: types.FunctionCall) -> FunctionResponseT: |
@@ -269,7 +270,15 @@ def run_one_iteration(self) -> Literal["COMPLETE", "CONTINUE"]: |
269 | 270 |
|
270 | 271 | function_responses = [] |
271 | 272 | for function_call in function_calls: |
272 | | - fc_result = self._execute_function_call(function_call) |
| 273 | + if function_call.args and ( |
| 274 | + safety := function_call.args.get("safety_decision") |
| 275 | + ): |
| 276 | + decision = self._get_safety_confirmation(safety) |
| 277 | + if decision == "TERMINATE": |
| 278 | + print("Terminating agent loop") |
| 279 | + return "COMPLETE" |
| 280 | + with console.status("Sending command to Computer...", spinner_style=None): |
| 281 | + fc_result = self.handle_action(function_call) |
273 | 282 | if isinstance(fc_result, EnvState): |
274 | 283 | function_responses.append( |
275 | 284 | FunctionResponse( |
@@ -298,27 +307,23 @@ def run_one_iteration(self) -> Literal["COMPLETE", "CONTINUE"]: |
298 | 307 | ) |
299 | 308 | return "CONTINUE" |
300 | 309 |
|
301 | | - def _execute_function_call( |
302 | | - self, function_call: types.FunctionCall |
303 | | - ) -> FunctionResponseT: |
304 | | - if safety := function_call.args.get("safety_decision"): |
305 | | - if safety["decision"] == "require_confirmation": |
306 | | - termcolor.cprint( |
307 | | - "Safety service requires explicit confirmation!", |
308 | | - color="yellow", |
309 | | - attrs=["bold"], |
310 | | - ) |
311 | | - print(safety["explanation"]) |
312 | | - decision = "" |
313 | | - while decision.lower() not in ("y", "n", "ye", "yes", "no"): |
314 | | - decision = input("Do you wish to proceed? [Y]es/[n]o\n") |
315 | | - if decision.lower() in ("n", "no"): |
316 | | - print("Terminating agent loop.") |
317 | | - return "COMPLETE" |
318 | | - print("Proceeding with agent loop.\n") |
319 | | - |
320 | | - with console.status("Sending command to Computer...", spinner_style=None): |
321 | | - return self.handle_action(function_call) |
| 310 | + def _get_safety_confirmation( |
| 311 | + self, safety: dict[str, Any] |
| 312 | + ) -> Literal["CONTINUE", "TERMINATE"]: |
| 313 | + if safety["decision"] != "require_confirmation": |
| 314 | + raise ValueError(f"Unknown safety decision: safety['decision']") |
| 315 | + termcolor.cprint( |
| 316 | + "Safety service requires explicit confirmation!", |
| 317 | + color="yellow", |
| 318 | + attrs=["bold"], |
| 319 | + ) |
| 320 | + print(safety["explanation"]) |
| 321 | + decision = "" |
| 322 | + while decision.lower() not in ("y", "n", "ye", "yes", "no"): |
| 323 | + decision = input("Do you wish to proceed? [Y]es/[n]o\n") |
| 324 | + if decision.lower() in ("n", "no"): |
| 325 | + return "TERMINATE" |
| 326 | + return "CONTINUE" |
322 | 327 |
|
323 | 328 | def agent_loop(self): |
324 | 329 | status = "CONTINUE" |
|
0 commit comments