-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy path__init__.py
More file actions
191 lines (164 loc) · 7.07 KB
/
__init__.py
File metadata and controls
191 lines (164 loc) · 7.07 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import asyncio
import contextlib
from collections.abc import AsyncGenerator, AsyncIterable
from datetime import datetime
from hashlib import md5
from pathlib import Path
from prompt_toolkit import PromptSession
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.formatted_text import FormattedText
from prompt_toolkit.history import FileHistory
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.patch_stdout import patch_stdout
from republic import StreamEvent
from rich import get_console
from rich.live import Live
from bub.builtin.agent import Agent
from bub.builtin.tape import TapeInfo
from bub.channels.base import Channel
from bub.channels.cli.renderer import CliRenderer
from bub.channels.message import ChannelMessage
from bub.envelope import field_of
from bub.tools import REGISTRY
from bub.types import MessageHandler
class CliChannel(Channel):
"""A simple CLI channel for testing and debugging."""
name = "cli"
_stop_event: asyncio.Event
def __init__(self, on_receive: MessageHandler, agent: Agent) -> None:
self._on_receive = on_receive
self._agent = agent
self._message_template = {
"chat_id": "cli_chat",
"channel": self.name,
"session_id": "cli_session",
}
self._mode = "agent" # or "shell"
self._main_task: asyncio.Task | None = None
self._renderer = CliRenderer(get_console())
self._last_tape_info: TapeInfo | None = None
self._workspace = self._agent.framework.workspace
self._prompt = self._build_prompt(self._workspace)
async def _refresh_tape_info(self) -> None:
tape = self._agent.tapes.session_tape(self._message_template["session_id"], self._workspace)
info = await self._agent.tapes.info(tape.name)
self._last_tape_info = info
def set_metadata(self, session_id: str | None = None, chat_id: str | None = None) -> None:
if session_id is not None:
self._message_template["session_id"] = session_id
if chat_id is not None:
self._message_template["chat_id"] = chat_id
async def start(self, stop_event: asyncio.Event) -> None:
self._stop_event = stop_event
self._main_task = asyncio.create_task(self._main_loop())
async def stop(self) -> None:
if self._main_task is not None:
self._main_task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await self._main_task
async def send(self, message: ChannelMessage) -> None:
if message.kind != "error":
return
self._renderer.error(message.content)
async def _main_loop(self) -> None:
self._renderer.welcome(model=self._agent.settings.model, workspace=str(self._workspace))
await self._refresh_tape_info()
request_completed = asyncio.Event()
while not self._stop_event.is_set():
try:
with patch_stdout(raw=True):
raw = (await self._prompt.prompt_async(self._prompt_message())).strip()
except KeyboardInterrupt:
self._renderer.info("Interrupted. Use ',quit' to exit.")
continue
except EOFError:
break
if not raw:
continue
if raw in {",quit", ",exit"}:
break
request = self._normalize_input(raw)
message = ChannelMessage(
session_id=self._message_template["session_id"],
channel=self._message_template["channel"],
chat_id=self._message_template["chat_id"],
content=request,
lifespan=self.message_lifespan(request_completed),
)
await self._on_receive(message)
await request_completed.wait()
request_completed.clear()
self._renderer.info("Bye.")
self._stop_event.set()
@contextlib.asynccontextmanager
async def message_lifespan(self, request_completed: asyncio.Event) -> AsyncGenerator[None, None]:
try:
yield
finally:
await self._refresh_tape_info()
request_completed.set()
def _normalize_input(self, raw: str) -> str:
if self._mode != "shell":
return raw
if raw.startswith(","):
return raw
return f",{raw}"
def _prompt_message(self) -> FormattedText:
cwd = Path.cwd().name
symbol = ">" if self._mode == "agent" else ","
return FormattedText([("bold", f"{cwd} {symbol} ")])
async def stream_events(
self, message: ChannelMessage, stream: AsyncIterable[StreamEvent]
) -> AsyncIterable[StreamEvent]:
live: Live | None = None
text = ""
try:
async for event in stream:
if event.kind == "text":
content = str(event.data.get("delta", ""))
if not content.strip() and not text:
continue # skip leading whitespace-only events
if live is None:
live = self._renderer.start_stream(message.kind)
text += content
self._renderer.update_stream(live, kind=message.kind, text=text)
yield event
finally:
if live is not None:
self._renderer.finish_stream(live, kind=message.kind, text=text)
def _build_prompt(self, workspace: Path) -> PromptSession[str]:
kb = KeyBindings()
@kb.add("c-x", eager=True)
def _toggle_mode(event) -> None:
self._mode = "shell" if self._mode == "agent" else "agent"
event.app.invalidate()
def _tool_sort_key(tool_name: str) -> tuple[str, str]:
section, _, name = tool_name.rpartition(".")
return (section, name)
history_file = self._history_file(self._agent.settings.home, workspace)
history_file.parent.mkdir(parents=True, exist_ok=True)
history = FileHistory(str(history_file))
tool_names = sorted((f",{name}" for name in REGISTRY), key=_tool_sort_key)
completer = WordCompleter(tool_names, ignore_case=True)
return PromptSession(
completer=completer,
complete_while_typing=True,
key_bindings=kb,
history=history,
bottom_toolbar=self._render_bottom_toolbar,
)
def _render_bottom_toolbar(self) -> FormattedText:
info = self._last_tape_info
now = datetime.now().strftime("%H:%M")
left = f"{now} mode:{self._mode}"
right = (
f"model:{self._agent.settings.model} "
f"entries:{field_of(info, 'entries', '-')} "
f"anchors:{field_of(info, 'anchors', '-')} "
f"last:{field_of(info, 'last_anchor', None) or '-'}"
)
return FormattedText([("", f"{left} {right}")])
@staticmethod
def _history_file(home: Path, workspace: Path) -> Path:
workspace_hash = md5(str(workspace).encode("utf-8"), usedforsecurity=False).hexdigest()
return home / "history" / f"{workspace_hash}.history"