Skip to content

Commit e721cc5

Browse files
committed
Iterate on code.
1 parent c44efc8 commit e721cc5

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

agent.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import os
15-
from typing import Literal, Optional, Union
15+
from typing import Literal, Optional, Union, Any
1616
from google import genai
1717
from google.genai import types
1818
import termcolor
@@ -99,6 +99,7 @@ def __init__(
9999
),
100100
types.Tool(function_declarations=custom_functions),
101101
],
102+
thinking_config=types.ThinkingConfig(include_thoughts=True),
102103
)
103104

104105
def handle_action(self, action: types.FunctionCall) -> FunctionResponseT:
@@ -269,7 +270,15 @@ def run_one_iteration(self) -> Literal["COMPLETE", "CONTINUE"]:
269270

270271
function_responses = []
271272
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)
273282
if isinstance(fc_result, EnvState):
274283
function_responses.append(
275284
FunctionResponse(
@@ -298,27 +307,23 @@ def run_one_iteration(self) -> Literal["COMPLETE", "CONTINUE"]:
298307
)
299308
return "CONTINUE"
300309

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"
322327

323328
def agent_loop(self):
324329
status = "CONTINUE"

0 commit comments

Comments
 (0)