-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.py
More file actions
87 lines (67 loc) · 3.03 KB
/
actions.py
File metadata and controls
87 lines (67 loc) · 3.03 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
from typing import TYPE_CHECKING
from agent_chat_cli.utils.enums import ControlCommand
from agent_chat_cli.components.chat_history import ChatHistory
from agent_chat_cli.components.messages import Message, MessageType
from agent_chat_cli.components.tool_permission_prompt import ToolPermissionPrompt
from agent_chat_cli.utils.logger import log_json
if TYPE_CHECKING:
from agent_chat_cli.app import AgentChatCLIApp
class Actions:
def __init__(self, app: "AgentChatCLIApp") -> None:
self.app = app
def quit(self) -> None:
self.app.exit()
async def add_message_to_chat(self, type: MessageType, content: str) -> None:
match type:
case MessageType.USER:
message = Message.user(content)
case MessageType.SYSTEM:
message = Message.system(content)
case MessageType.AGENT:
message = Message.agent(content)
case _:
raise ValueError(f"Unsupported message type: {type}")
chat_history = self.app.query_one(ChatHistory)
chat_history.add_message(message)
async def submit_user_message(self, message: str) -> None:
chat_history = self.app.query_one(ChatHistory)
chat_history.add_message(Message.user(message))
self.app.ui_state.start_thinking()
await self.app.ui_state.scroll_to_bottom()
await self._query(message)
async def post_system_message(self, message: str) -> None:
await self.add_message_to_chat(MessageType.SYSTEM, message)
async def render_message(self, message) -> None:
await self.app.renderer.render_message(message)
async def interrupt(self) -> None:
permission_prompt = self.app.query_one(ToolPermissionPrompt)
if permission_prompt.is_visible:
return
self.app.ui_state.set_interrupting(True)
await self.app.agent_loop.client.interrupt()
self.app.ui_state.stop_thinking()
async def clear(self) -> None:
chat_history = self.app.query_one(ChatHistory)
await chat_history.remove_children()
self.app.ui_state.stop_thinking()
async def new(self) -> None:
await self.app.agent_loop.query_queue.put(ControlCommand.NEW_CONVERSATION)
await self.clear()
async def respond_to_tool_permission(self, response: str) -> None:
log_json(
{
"event": "permission_response_action",
"response": response,
}
)
await self.app.agent_loop.permission_response_queue.put(response)
self.app.ui_state.hide_permission_prompt()
self.app.ui_state.start_thinking()
normalized = response.lower().strip()
if normalized not in ["y", "yes", "allow", ""]:
if normalized in ["n", "no", "deny"]:
await self._query("The user has denied the tool")
else:
await self.submit_user_message(response)
async def _query(self, user_input: str) -> None:
await self.app.agent_loop.query_queue.put(user_input)