keyboard input tool

frdel committed Dec 8, 2024 at 00:17 UTC 8ec3b24696e0346a3aea22d0f304ca297004bdcc
6 files changed +71 -39
prompts/default/agent.system.tool.code_exe.md
+1 -16
@@ -10,7 +10,6 @@ IMPORTANT: Never use implicit print or implicit output, it does not work! If you
10 When tool outputs error, you need to change your code accordingly before trying again. knowledge_tool can help analyze errors.
11 IMPORTANT!: Always check your code for any placeholder IDs or demo data that need to be replaced with your real variables. Do not simply reuse code snippets from tutorials.
12 Do not use in combination with other tools except for thoughts. Wait for response before using other tools.
13 -When writing own code, ALWAYS put print/log statements inside and at the end of your code to get results!
13 **Example usages:**
14 1. Execute python code
15 ~~~json
@@ -56,21 +55,7 @@ When writing own code, ALWAYS put print/log statements inside and at the end of
55 }
56 ~~~
57
59 -2. 2. Answer terminal dialog
60 -~~~json
61 -{
62 - "thoughts": [
63 - "Program needs confirmation...",
64 - ],
65 - "tool_name": "code_execution_tool",
66 - "tool_args": {
67 - "runtime": "terminal",
68 - "code": "Y",
69 - }
70 -}
71 -~~~
72 -
73 -2. 3. Reset terminal
58 +2. 2. Reset terminal
59 ~~~json
60 {
61 "thoughts": [
prompts/default/agent.system.tool.input.md new
+15
@@ -0,0 +1,15 @@
1 +### input:
2 +Use "keyboard" argument of input tool to provide keyboard input to progams.
3 +Answer dialogs, enter passwords, etc.
4 +**Example usage**:
5 +~~~json
6 +{
7 + "thoughts": [
8 + "The program asks for Y/N...",
9 + ],
10 + "tool_name": "input",
11 + "tool_args": {
12 + "keyboard": "Y",
13 + }
14 +}
15 +~~~
\ No newline at end of file
prompts/default/agent.system.tools.md
+2
@@ -12,4 +12,6 @@
12
13 {{ include './agent.system.tool.code_exe.md' }}
14
15 +{{ include './agent.system.tool.input.md' }}
16 +
17 {{ include './agent.system.tool.web.md' }}
\ No newline at end of file
python/helpers/tool.py
+5 -2
@@ -23,13 +23,13 @@ class Tool:
23
24 async def before_execution(self, **kwargs):
25 PrintStyle(font_color="#1B4F72", padding=True, background_color="white", bold=True).print(f"{self.agent.agent_name}: Using tool '{self.name}'")
26 - self.log = self.agent.context.log.log(type="tool", heading=f"{self.agent.agent_name}: Using tool '{self.name}'", content="", kvps=self.args)
26 + self.log = self.get_log_object()
27 if self.args and isinstance(self.args, dict):
28 for key, value in self.args.items():
29 PrintStyle(font_color="#85C1E9", bold=True).stream(self.nice_key(key)+": ")
30 PrintStyle(font_color="#85C1E9", padding=isinstance(value,str) and "\n" in value).stream(value)
31 PrintStyle().print()
32 -
32 +
33 async def after_execution(self, response: Response, **kwargs):
34 text = response.message.strip()
35 await self.agent.hist_add_tool_result(self.name, text)
@@ -37,6 +37,9 @@ class Tool:
37 PrintStyle(font_color="#85C1E9").print(response.message)
38 self.log.update(content=response.message)
39
40 + def get_log_object(self):
41 + return self.agent.context.log.log(type="tool", heading=f"{self.agent.agent_name}: Using tool '{self.name}'", content="", kvps=self.args)
42 +
43 def nice_key(self, key:str):
44 words = key.split('_')
45 words = [words[0].capitalize()] + [word.lower() for word in words[1:]]
python/tools/code_execution_tool.py
+25 -21
@@ -49,27 +49,31 @@ class CodeExecution(Tool):
49 response = self.agent.read_prompt("fw.code_no_output.md")
50 return Response(message=response, break_loop=False)
51
52 - async def before_execution(self, **kwargs):
53 - await self.agent.handle_intervention() # wait for intervention and handle it, if paused
54 - PrintStyle(
55 - font_color="#1B4F72", padding=True, background_color="white", bold=True
56 - ).print(f"{self.agent.agent_name}: Using tool '{self.name}'")
57 - self.log = self.agent.context.log.log(
58 - type="code_exe",
59 - heading=f"{self.agent.agent_name}: Using tool '{self.name}'",
60 - content="",
61 - kvps=self.args,
62 - )
63 - if self.args and isinstance(self.args, dict):
64 - for key, value in self.args.items():
65 - PrintStyle(font_color="#85C1E9", bold=True).stream(
66 - self.nice_key(key) + ": "
67 - )
68 - PrintStyle(
69 - font_color="#85C1E9",
70 - padding=isinstance(value, str) and "\n" in value,
71 - ).stream(value)
72 - PrintStyle().print()
52 + # async def before_execution(self, **kwargs):
53 + # await self.agent.handle_intervention() # wait for intervention and handle it, if paused
54 + # PrintStyle(
55 + # font_color="#1B4F72", padding=True, background_color="white", bold=True
56 + # ).print(f"{self.agent.agent_name}: Using tool '{self.name}'")
57 + # self.log = self.agent.context.log.log(
58 + # type="code_exe",
59 + # heading=f"{self.agent.agent_name}: Using tool '{self.name}'",
60 + # content="",
61 + # kvps=self.args,
62 + # )
63 + # if self.args and isinstance(self.args, dict):
64 + # for key, value in self.args.items():
65 + # PrintStyle(font_color="#85C1E9", bold=True).stream(
66 + # self.nice_key(key) + ": "
67 + # )
68 + # PrintStyle(
69 + # font_color="#85C1E9",
70 + # padding=isinstance(value, str) and "\n" in value,
71 + # ).stream(value)
72 + # PrintStyle().print()
73 +
74 + def get_log_object(self):
75 + return self.agent.context.log.log(type="code_exe", heading=f"{self.agent.agent_name}: Using tool '{self.name}'", content="", kvps=self.args)
76 +
77
78 async def after_execution(self, response, **kwargs):
79 await self.agent.hist_add_tool_result(self.name, response.message)
python/tools/input.py new
+23
@@ -0,0 +1,23 @@
1 +from agent import Agent, UserMessage
2 +from python.helpers.tool import Tool, Response
3 +from python.tools.code_execution_tool import CodeExecution
4 +
5 +
6 +class Input(Tool):
7 +
8 + async def execute(self, keyboard="", **kwargs):
9 + # normalize keyboard input
10 + keyboard = keyboard.rstrip()
11 + keyboard += "\n"
12 +
13 + # forward keyboard input to code execution tool
14 + args = {"runtime": "terminal", "code": keyboard}
15 + cot = CodeExecution(self.agent, "code_execution_tool", args, self.message)
16 + cot.log = self.log
17 + return await cot.execute(**args)
18 +
19 + def get_log_object(self):
20 + return self.agent.context.log.log(type="code_exe", heading=f"{self.agent.agent_name}: Using tool '{self.name}'", content="", kvps=self.args)
21 +
22 + async def after_execution(self, response, **kwargs):
23 + await self.agent.hist_add_tool_result(self.name, response.message)
\ No newline at end of file