coder-agent 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- coder_agent-0.1.0.dist-info/METADATA +262 -0
- coder_agent-0.1.0.dist-info/RECORD +163 -0
- coder_agent-0.1.0.dist-info/WHEEL +5 -0
- coder_agent-0.1.0.dist-info/entry_points.txt +2 -0
- coder_agent-0.1.0.dist-info/licenses/LICENSE.md +21 -0
- coder_agent-0.1.0.dist-info/top_level.txt +1 -0
- coderagent/__init__.py +20 -0
- coderagent/compat/__init__.py +27 -0
- coderagent/compat/cache_control.py +69 -0
- coderagent/compat/config_spec.py +60 -0
- coderagent/compat/exceptions.py +20 -0
- coderagent/compat/retry.py +27 -0
- coderagent/compat/serialize.py +31 -0
- coderagent/config/__init__.py +1 -0
- coderagent/config/defaults.yaml +103 -0
- coderagent/config/loader.py +264 -0
- coderagent/config/schema.py +548 -0
- coderagent/config/writer.py +218 -0
- coderagent/core/__init__.py +1 -0
- coderagent/core/budget.py +47 -0
- coderagent/core/capabilities.py +136 -0
- coderagent/core/checkpoint.py +164 -0
- coderagent/core/content.py +156 -0
- coderagent/core/context.py +618 -0
- coderagent/core/events.py +83 -0
- coderagent/core/history.py +56 -0
- coderagent/core/instructions.py +99 -0
- coderagent/core/loop.py +702 -0
- coderagent/core/memory.py +56 -0
- coderagent/core/mentions.py +81 -0
- coderagent/core/os_sandbox.py +191 -0
- coderagent/core/otel.py +214 -0
- coderagent/core/protocols.py +38 -0
- coderagent/core/redact.py +49 -0
- coderagent/core/sandbox.py +82 -0
- coderagent/core/schema.py +107 -0
- coderagent/core/session.py +344 -0
- coderagent/core/shell_env.py +74 -0
- coderagent/core/subagent.py +474 -0
- coderagent/core/summarize.py +312 -0
- coderagent/core/telemetry.py +209 -0
- coderagent/core/tokens.py +132 -0
- coderagent/core/trust.py +301 -0
- coderagent/environments/__init__.py +11 -0
- coderagent/environments/bubblewrap.py +150 -0
- coderagent/environments/contree.py +146 -0
- coderagent/environments/docker.py +160 -0
- coderagent/environments/local.py +110 -0
- coderagent/environments/singularity.py +138 -0
- coderagent/environments/swerex_docker.py +79 -0
- coderagent/environments/swerex_modal.py +123 -0
- coderagent/eval/__init__.py +1 -0
- coderagent/eval/compare.py +190 -0
- coderagent/eval/report.py +141 -0
- coderagent/eval/runner.py +368 -0
- coderagent/eval/task.py +194 -0
- coderagent/extensions/__init__.py +1 -0
- coderagent/extensions/hooks.py +121 -0
- coderagent/extensions/plugins.py +217 -0
- coderagent/extensions/reflect.py +342 -0
- coderagent/models/__init__.py +33 -0
- coderagent/models/backends.py +218 -0
- coderagent/models/call.py +18 -0
- coderagent/models/check.py +139 -0
- coderagent/models/gateway.py +60 -0
- coderagent/models/litellm_config.py +70 -0
- coderagent/models/provider.py +98 -0
- coderagent/models/reasoning.py +28 -0
- coderagent/models/router.py +136 -0
- coderagent/models/settings.py +172 -0
- coderagent/models/streaming.py +122 -0
- coderagent/models/verbosity.py +14 -0
- coderagent/paths.py +31 -0
- coderagent/run.py +454 -0
- coderagent/skills/__init__.py +119 -0
- coderagent/skills/errors.py +11 -0
- coderagent/skills/install.py +340 -0
- coderagent/skills/presets/code-review/SKILL.md +35 -0
- coderagent/skills/presets/codebase-onboarding/SKILL.md +33 -0
- coderagent/skills/presets/data-analysis/SKILL.md +35 -0
- coderagent/skills/presets/deep-research/SKILL.md +38 -0
- coderagent/skills/presets/frontend-dev/SKILL.md +32 -0
- coderagent/skills/presets/long-document/SKILL.md +37 -0
- coderagent/skills/presets/long-document/scripts/pdf_text.py +37 -0
- coderagent/skills/presets/office-docx/SKILL.md +36 -0
- coderagent/skills/presets/office-docx/scripts/docx_text.py +36 -0
- coderagent/skills/presets/office-pptx/SKILL.md +32 -0
- coderagent/skills/presets/office-xlsx/SKILL.md +34 -0
- coderagent/skills/presets/office-xlsx/scripts/xlsx_text.py +72 -0
- coderagent/skills/presets/writing/SKILL.md +31 -0
- coderagent/skills/sandbox.py +269 -0
- coderagent/skills/sites.py +177 -0
- coderagent/skills/source.py +404 -0
- coderagent/testing/__init__.py +5 -0
- coderagent/testing/factory.py +59 -0
- coderagent/testing/scripted_model.py +54 -0
- coderagent/tools/__init__.py +1 -0
- coderagent/tools/base.py +54 -0
- coderagent/tools/bash_policy.py +486 -0
- coderagent/tools/builtin/__init__.py +35 -0
- coderagent/tools/builtin/fs.py +283 -0
- coderagent/tools/builtin/patch.py +279 -0
- coderagent/tools/builtin/search.py +242 -0
- coderagent/tools/builtin/shell.py +336 -0
- coderagent/tools/builtin/skill.py +41 -0
- coderagent/tools/builtin/task.py +53 -0
- coderagent/tools/builtin/todo.py +63 -0
- coderagent/tools/executor.py +243 -0
- coderagent/tools/mcp.py +317 -0
- coderagent/tools/permissions.py +472 -0
- coderagent/tools/registry.py +83 -0
- coderagent/tools/rules.py +138 -0
- coderagent/ui/__init__.py +1 -0
- coderagent/ui/cli.py +498 -0
- coderagent/ui/commands/__init__.py +109 -0
- coderagent/ui/commands/builtin.py +778 -0
- coderagent/ui/commands/compact.py +12 -0
- coderagent/ui/commands/resume.py +26 -0
- coderagent/ui/commands/review.py +73 -0
- coderagent/ui/commands/rewind.py +28 -0
- coderagent/ui/custom_commands.py +179 -0
- coderagent/ui/notify.py +46 -0
- coderagent/ui/open_file.py +48 -0
- coderagent/ui/render.py +62 -0
- coderagent/ui/spinner.py +68 -0
- coderagent/ui/tui.py +610 -0
- coderagent/ui/widgets/__init__.py +29 -0
- coderagent/ui/widgets/approval_prompt.py +129 -0
- coderagent/ui/widgets/diff_view.py +97 -0
- coderagent/ui/widgets/file_tree.py +105 -0
- coderagent/ui/widgets/markdown_message.py +44 -0
- coderagent/ui/widgets/markup.py +10 -0
- coderagent/ui/widgets/message_block.py +61 -0
- coderagent/ui/widgets/path_suggester.py +66 -0
- coderagent/ui/widgets/session_picker.py +49 -0
- coderagent/ui/widgets/status_bar.py +44 -0
- coderagent/ui/widgets/todo_panel.py +40 -0
- coderagent/ui/widgets/tool_card.py +104 -0
- coderagent/ui/widgets/working_dots.py +26 -0
- coderagent/version.py +7 -0
- coderagent/web/__init__.py +5 -0
- coderagent/web/api.py +449 -0
- coderagent/web/app.py +113 -0
- coderagent/web/approver.py +73 -0
- coderagent/web/config_api.py +53 -0
- coderagent/web/events.py +159 -0
- coderagent/web/files.py +115 -0
- coderagent/web/i18n.py +346 -0
- coderagent/web/server.py +100 -0
- coderagent/web/sessions.py +404 -0
- coderagent/web/skills_api.py +376 -0
- coderagent/web/static/app.css +283 -0
- coderagent/web/static/app.js +1466 -0
- coderagent/web/templates/en/base.html +31 -0
- coderagent/web/templates/en/chat.html +60 -0
- coderagent/web/templates/en/macros.html +9 -0
- coderagent/web/templates/en/settings.html +80 -0
- coderagent/web/templates/en/skills.html +64 -0
- coderagent/web/templates/zh/base.html +31 -0
- coderagent/web/templates/zh/chat.html +60 -0
- coderagent/web/templates/zh/macros.html +9 -0
- coderagent/web/templates/zh/settings.html +77 -0
- coderagent/web/templates/zh/skills.html +61 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
"""The sessions of the web interface.
|
|
2
|
+
|
|
3
|
+
Every session has a worker thread: the turns and the commands run there, so the
|
|
4
|
+
event loop of the server is never blocked. The events of the core are translated
|
|
5
|
+
into items, put into a ring buffer and pushed to whoever is listening.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import asyncio
|
|
9
|
+
import queue
|
|
10
|
+
import threading
|
|
11
|
+
from collections import deque
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from coderagent.config.loader import load_config
|
|
17
|
+
from coderagent.config.schema import Config
|
|
18
|
+
from coderagent.core.content import parts
|
|
19
|
+
from coderagent.core.events import Event, EventBus, EventType
|
|
20
|
+
from coderagent.core.loop import AgentLoop
|
|
21
|
+
from coderagent.core.protocols import ModelBackend
|
|
22
|
+
from coderagent.core.session import Session, SessionStore
|
|
23
|
+
from coderagent.models.backends import has_price, priced_name
|
|
24
|
+
from coderagent.run import build_loop, build_store, remember_capability, settle
|
|
25
|
+
from coderagent.tools.permissions import PermissionMode
|
|
26
|
+
from coderagent.web.approver import WebApprover
|
|
27
|
+
from coderagent.web.events import TRANSIENT_KINDS, normalize
|
|
28
|
+
|
|
29
|
+
BUFFER_SIZE = 1000
|
|
30
|
+
"""How many items a session keeps for the browsers that come late."""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class SessionBusy(RuntimeError):
|
|
34
|
+
"""The session is already running a turn."""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class SessionRuntime:
|
|
38
|
+
"""One session: its loop, its worker thread, its buffer and its subscribers."""
|
|
39
|
+
|
|
40
|
+
def __init__(
|
|
41
|
+
self,
|
|
42
|
+
config: Config,
|
|
43
|
+
cwd: Path,
|
|
44
|
+
*,
|
|
45
|
+
store: SessionStore | None = None,
|
|
46
|
+
session: Session | None = None,
|
|
47
|
+
model: ModelBackend | None = None,
|
|
48
|
+
permission_mode: str | None = None,
|
|
49
|
+
registry: "SessionRegistry | None" = None,
|
|
50
|
+
) -> None:
|
|
51
|
+
self.config = config
|
|
52
|
+
self.cwd = Path(cwd)
|
|
53
|
+
self.store = store or build_store(config)
|
|
54
|
+
self.bus = EventBus()
|
|
55
|
+
self.session = session or self.store.create(self.cwd)
|
|
56
|
+
self.registry = registry
|
|
57
|
+
self.approver = WebApprover(self.bus, self.session.id, config.ui.approval_timeout)
|
|
58
|
+
self._model = model
|
|
59
|
+
self._permission_mode = permission_mode
|
|
60
|
+
self._loop: AgentLoop | None = None
|
|
61
|
+
self.buffer: deque[dict] = deque(maxlen=BUFFER_SIZE)
|
|
62
|
+
self.cancel = threading.Event()
|
|
63
|
+
"""Set to stop the turn that is running."""
|
|
64
|
+
self.busy = threading.Event()
|
|
65
|
+
"""Set while a turn or a command is running."""
|
|
66
|
+
self._lock = threading.Lock()
|
|
67
|
+
self._seq = 0
|
|
68
|
+
self._subscribers: list[tuple[asyncio.AbstractEventLoop, asyncio.Queue]] = []
|
|
69
|
+
self._queue: queue.Queue[Callable[[], None]] = queue.Queue()
|
|
70
|
+
self._worker = threading.Thread(target=self._work, daemon=True, name=f"session-{self.session.id}")
|
|
71
|
+
self._worker.start()
|
|
72
|
+
self._subscribe()
|
|
73
|
+
self._replay()
|
|
74
|
+
|
|
75
|
+
def _replay(self) -> None:
|
|
76
|
+
"""The items of the log, so that a browser that comes late sees them."""
|
|
77
|
+
for event in self.store.events(self.session.id):
|
|
78
|
+
for item in normalize(event):
|
|
79
|
+
self.emit(item)
|
|
80
|
+
|
|
81
|
+
# -- the events of the core -------------------------------------------
|
|
82
|
+
|
|
83
|
+
def _subscribe(self) -> None:
|
|
84
|
+
"""Listen to every kind of event; the bus has no wildcard subscription."""
|
|
85
|
+
for event_type in EventType:
|
|
86
|
+
self.bus.subscribe(event_type, self._on_event)
|
|
87
|
+
|
|
88
|
+
def _on_event(self, event: Event) -> None:
|
|
89
|
+
for item in normalize(event):
|
|
90
|
+
self.emit(item)
|
|
91
|
+
|
|
92
|
+
def emit(self, item: dict) -> dict:
|
|
93
|
+
"""Put an item into the buffer and push it to the subscribers.
|
|
94
|
+
|
|
95
|
+
The streaming increments are transient: the browser sees them while they
|
|
96
|
+
arrive, but they are not kept, so a long answer cannot push the history
|
|
97
|
+
of the session out of the buffer.
|
|
98
|
+
"""
|
|
99
|
+
with self._lock:
|
|
100
|
+
self._seq += 1
|
|
101
|
+
item = {"seq": self._seq, **item}
|
|
102
|
+
if item["kind"] not in TRANSIENT_KINDS:
|
|
103
|
+
self.buffer.append(item)
|
|
104
|
+
subscribers = list(self._subscribers)
|
|
105
|
+
for loop, subscriber in subscribers:
|
|
106
|
+
try:
|
|
107
|
+
loop.call_soon_threadsafe(subscriber.put_nowait, item)
|
|
108
|
+
except RuntimeError: # pragma: no cover - the loop is gone
|
|
109
|
+
pass
|
|
110
|
+
return item
|
|
111
|
+
|
|
112
|
+
def items(self, since: int = 0) -> list[dict]:
|
|
113
|
+
"""The items of the buffer that the browser has not seen yet."""
|
|
114
|
+
with self._lock:
|
|
115
|
+
return [item for item in self.buffer if item["seq"] > since]
|
|
116
|
+
|
|
117
|
+
def attach(self, loop: asyncio.AbstractEventLoop, subscriber: asyncio.Queue, since: int = 0) -> None:
|
|
118
|
+
"""Add a subscriber and give it what it missed."""
|
|
119
|
+
with self._lock:
|
|
120
|
+
for item in self.buffer:
|
|
121
|
+
if item["seq"] > since:
|
|
122
|
+
subscriber.put_nowait(item)
|
|
123
|
+
self._subscribers.append((loop, subscriber))
|
|
124
|
+
|
|
125
|
+
def detach(self, subscriber: asyncio.Queue) -> None:
|
|
126
|
+
with self._lock:
|
|
127
|
+
self._subscribers = [(loop, item) for loop, item in self._subscribers if item is not subscriber]
|
|
128
|
+
|
|
129
|
+
def end_streams(self) -> None:
|
|
130
|
+
"""Tell the browsers that listen that the stream is over.
|
|
131
|
+
|
|
132
|
+
The server stops before the browser goes away. A stream that waits for
|
|
133
|
+
its end keeps the server waiting for the connection, and the server
|
|
134
|
+
that gives up on it says so with an error.
|
|
135
|
+
"""
|
|
136
|
+
with self._lock:
|
|
137
|
+
subscribers = list(self._subscribers)
|
|
138
|
+
for loop, subscriber in subscribers:
|
|
139
|
+
try:
|
|
140
|
+
loop.call_soon_threadsafe(subscriber.put_nowait, {"kind": "stream_end"})
|
|
141
|
+
except RuntimeError: # pragma: no cover - the loop is gone
|
|
142
|
+
pass
|
|
143
|
+
|
|
144
|
+
# -- the work ---------------------------------------------------------
|
|
145
|
+
|
|
146
|
+
def submit(self, text: str, purpose: str = "", images: list[str] | None = None) -> None:
|
|
147
|
+
"""Queue a message for the worker. Raises `SessionBusy` when it is running."""
|
|
148
|
+
self._enqueue(lambda: self._execute(text, purpose, images or []))
|
|
149
|
+
|
|
150
|
+
def _enqueue(self, job: Callable[[], None]) -> None:
|
|
151
|
+
"""Put a job on the worker thread. Only one job runs at a time."""
|
|
152
|
+
with self._lock:
|
|
153
|
+
if self.busy.is_set():
|
|
154
|
+
raise SessionBusy(f"the session {self.session.id} is running")
|
|
155
|
+
self.busy.set()
|
|
156
|
+
self._queue.put(job)
|
|
157
|
+
|
|
158
|
+
def review(self) -> dict[str, Any]:
|
|
159
|
+
"""Review the changes of the project: the diff goes into the prompt of a turn.
|
|
160
|
+
|
|
161
|
+
The turn runs on the worker thread, like every other turn. The diff goes
|
|
162
|
+
to the browser first, so the user sees what is being reviewed.
|
|
163
|
+
"""
|
|
164
|
+
from coderagent.ui.commands.review import changes, prompt
|
|
165
|
+
|
|
166
|
+
diff, untracked, error = changes(self.cwd)
|
|
167
|
+
if error:
|
|
168
|
+
return {"error": error}
|
|
169
|
+
if not diff and not untracked:
|
|
170
|
+
return {"error": "No changes to review."}
|
|
171
|
+
self._enqueue(lambda: self._review_turn(prompt(diff, untracked), diff, untracked))
|
|
172
|
+
return {"diff": diff, "untracked": untracked}
|
|
173
|
+
|
|
174
|
+
def _review_turn(self, text: str, diff: str, untracked: list[str]) -> None:
|
|
175
|
+
"""The card with the diff, then the turn that reviews it."""
|
|
176
|
+
self.emit({"kind": "review", "diff": diff, "untracked": untracked})
|
|
177
|
+
self._run_turn(text, "review")
|
|
178
|
+
|
|
179
|
+
def _work(self) -> None:
|
|
180
|
+
while True:
|
|
181
|
+
job = self._queue.get()
|
|
182
|
+
try:
|
|
183
|
+
job()
|
|
184
|
+
except Exception as error:
|
|
185
|
+
self.emit({"kind": "error", "error": f"{type(error).__name__}: {error}"})
|
|
186
|
+
finally:
|
|
187
|
+
self.busy.clear()
|
|
188
|
+
self._queue.task_done()
|
|
189
|
+
|
|
190
|
+
def _execute(self, text: str, purpose: str = "", images: list[str] | None = None) -> None:
|
|
191
|
+
images = images or []
|
|
192
|
+
if text.startswith("/"):
|
|
193
|
+
if images:
|
|
194
|
+
"""A command runs on the session, not on a message."""
|
|
195
|
+
self.emit({"kind": "error", "error": "a command cannot carry an image"})
|
|
196
|
+
self.emit({"kind": "turn_end", "stop_reason": "error", "cost": 0.0, "error": ""})
|
|
197
|
+
return
|
|
198
|
+
self._run_command(text)
|
|
199
|
+
return
|
|
200
|
+
self._run_turn(text, purpose, images)
|
|
201
|
+
|
|
202
|
+
def _run_turn(self, text: str, purpose: str = "", images: list[str] | None = None) -> None:
|
|
203
|
+
loop = self.loop()
|
|
204
|
+
self.cancel.clear()
|
|
205
|
+
loop.cancel = self.cancel.is_set
|
|
206
|
+
try:
|
|
207
|
+
result = loop.run_turn(parts(text, images) if images else text, purpose)
|
|
208
|
+
except Exception as error:
|
|
209
|
+
self.emit({"kind": "error", "error": f"{type(error).__name__}: {error}"})
|
|
210
|
+
self.emit({"kind": "turn_end", "stop_reason": "error", "cost": 0.0, "error": str(error)})
|
|
211
|
+
return
|
|
212
|
+
if result.stop_reason not in ("error", "cost_exceeded"):
|
|
213
|
+
"""A turn that failed has nothing to remember, and no money to do it."""
|
|
214
|
+
settle(loop, self.config, self.cwd)
|
|
215
|
+
self.emit(
|
|
216
|
+
{
|
|
217
|
+
"kind": "turn_end",
|
|
218
|
+
"stop_reason": result.stop_reason,
|
|
219
|
+
"cost": result.cost,
|
|
220
|
+
"session_cost": loop.session.total_cost,
|
|
221
|
+
"error": result.error,
|
|
222
|
+
}
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
def _run_command(self, line: str) -> None:
|
|
226
|
+
"""A slash command. It may change the session, so the registry is told."""
|
|
227
|
+
from coderagent.ui.commands import CommandContext
|
|
228
|
+
from coderagent.ui.commands import run as run_command
|
|
229
|
+
|
|
230
|
+
loop = self.loop()
|
|
231
|
+
context = CommandContext(
|
|
232
|
+
loop=loop,
|
|
233
|
+
session=loop.session,
|
|
234
|
+
store=self.store,
|
|
235
|
+
bus=self.bus,
|
|
236
|
+
cwd=self.cwd,
|
|
237
|
+
config=self.config,
|
|
238
|
+
policy=loop.executor.policy,
|
|
239
|
+
checkpoints=loop.checkpoints,
|
|
240
|
+
mcp=loop.mcp,
|
|
241
|
+
output=lambda text: self.emit({"kind": "command_output", "text": text}),
|
|
242
|
+
submit=self._run_turn,
|
|
243
|
+
submit_side=self._run_side,
|
|
244
|
+
)
|
|
245
|
+
run_command(context, line)
|
|
246
|
+
if loop.session.id != self.session.id:
|
|
247
|
+
self.switch_to(loop.session)
|
|
248
|
+
self.emit({"kind": "command_end"})
|
|
249
|
+
|
|
250
|
+
def _run_side(self, text: str, session: Session) -> None:
|
|
251
|
+
"""Run a turn in a conversation that goes aside. This one is not touched."""
|
|
252
|
+
loop = self.loop()
|
|
253
|
+
self.cancel.clear()
|
|
254
|
+
loop.cancel = self.cancel.is_set
|
|
255
|
+
try:
|
|
256
|
+
result = loop.run_turn(text, "side", session)
|
|
257
|
+
except Exception as error:
|
|
258
|
+
self.emit({"kind": "error", "error": f"{type(error).__name__}: {error}"})
|
|
259
|
+
return
|
|
260
|
+
self.emit(
|
|
261
|
+
{
|
|
262
|
+
"kind": "turn_end",
|
|
263
|
+
"stop_reason": result.stop_reason,
|
|
264
|
+
"cost": result.cost,
|
|
265
|
+
"session_cost": loop.session.total_cost,
|
|
266
|
+
"error": result.error,
|
|
267
|
+
}
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
def switch_to(self, session: Session) -> None:
|
|
271
|
+
"""The session changed: the registry gets a new key for this runtime."""
|
|
272
|
+
previous = self.session.id
|
|
273
|
+
self.session = session
|
|
274
|
+
self.approver.session_id = session.id
|
|
275
|
+
if self.registry is not None:
|
|
276
|
+
self.registry.rekey(self, previous, session.id)
|
|
277
|
+
self.emit({"kind": "session_switched", "session_id": session.id, "previous_session_id": previous})
|
|
278
|
+
|
|
279
|
+
def interrupt(self) -> None:
|
|
280
|
+
"""Ask the turn that is running to stop."""
|
|
281
|
+
self.cancel.set()
|
|
282
|
+
self.emit({"kind": "interrupted"})
|
|
283
|
+
|
|
284
|
+
def resume(self) -> Session:
|
|
285
|
+
"""Continue the session of the log."""
|
|
286
|
+
session = self.store.rebuild(self.session.id)
|
|
287
|
+
if self._loop is not None:
|
|
288
|
+
self._loop.switch_to(session)
|
|
289
|
+
self.switch_to(session)
|
|
290
|
+
return session
|
|
291
|
+
|
|
292
|
+
# -- the state of the session -----------------------------------------
|
|
293
|
+
|
|
294
|
+
def loop(self) -> AgentLoop:
|
|
295
|
+
"""The loop of this session. It is built when the first turn runs."""
|
|
296
|
+
if self._loop is None:
|
|
297
|
+
self._loop = build_loop(
|
|
298
|
+
self.config,
|
|
299
|
+
self.cwd,
|
|
300
|
+
model=self._model,
|
|
301
|
+
store=self.store,
|
|
302
|
+
session=self.session,
|
|
303
|
+
permission_mode=self._permission_mode,
|
|
304
|
+
bus=self.bus,
|
|
305
|
+
approver=self.approver,
|
|
306
|
+
remember=remember_capability,
|
|
307
|
+
)
|
|
308
|
+
self._loop.cancel = self.cancel.is_set
|
|
309
|
+
return self._loop
|
|
310
|
+
|
|
311
|
+
def status(self) -> dict[str, Any]:
|
|
312
|
+
"""The fields of the status bar."""
|
|
313
|
+
loop = self._loop
|
|
314
|
+
session = loop.session if loop else self.session
|
|
315
|
+
return {
|
|
316
|
+
"session_id": session.id,
|
|
317
|
+
"model": getattr(loop.model.backend, "model_name", "") if loop else self.config.model.model_name,
|
|
318
|
+
"tokens": loop.context.estimated_tokens(loop.tools) if loop else 0,
|
|
319
|
+
"window": loop.context.config.context_window if loop else self.config.context.context_window,
|
|
320
|
+
"cost": session.total_cost,
|
|
321
|
+
"priced": loop.model.priced if loop else has_price(self._model_name()),
|
|
322
|
+
"cwd": str(self.cwd),
|
|
323
|
+
"mode": loop.executor.policy.mode.value if loop else self.mode(),
|
|
324
|
+
"state": session.state.value,
|
|
325
|
+
"sandbox": self.config.sandbox.type,
|
|
326
|
+
"busy": self.busy.is_set(),
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
def mode(self) -> str:
|
|
330
|
+
"""The mode of the session, the one of the loop when it is running."""
|
|
331
|
+
if self._loop is not None:
|
|
332
|
+
return self._loop.executor.policy.mode.value
|
|
333
|
+
return self._permission_mode or self.config.permissions.mode
|
|
334
|
+
|
|
335
|
+
def _model_name(self) -> str:
|
|
336
|
+
"""The model of the server, with the alias of the config file resolved."""
|
|
337
|
+
model = self.config.model
|
|
338
|
+
return priced_name(model.model_name, model.litellm_config)
|
|
339
|
+
|
|
340
|
+
def set_mode(self, mode: PermissionMode) -> None:
|
|
341
|
+
"""Change the mode of the session while it runs.
|
|
342
|
+
|
|
343
|
+
The policy is the object the executor asks, so the change is in place
|
|
344
|
+
right away: the next tool call sees it, and the model is told about it
|
|
345
|
+
the next time the context is put together.
|
|
346
|
+
"""
|
|
347
|
+
self._permission_mode = mode.value
|
|
348
|
+
if self._loop is not None:
|
|
349
|
+
self._loop.executor.policy.mode = mode
|
|
350
|
+
self._loop.session.permission_mode = mode.value
|
|
351
|
+
self.emit({"kind": "mode_changed", "mode": mode.value})
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
class SessionRegistry:
|
|
355
|
+
"""The sessions the web interface knows about."""
|
|
356
|
+
|
|
357
|
+
def __init__(
|
|
358
|
+
self,
|
|
359
|
+
config: Config,
|
|
360
|
+
cwd: Path,
|
|
361
|
+
*,
|
|
362
|
+
store: SessionStore | None = None,
|
|
363
|
+
model: ModelBackend | None = None,
|
|
364
|
+
permission_mode: str | None = None,
|
|
365
|
+
config_spec: list[str] | None = None,
|
|
366
|
+
) -> None:
|
|
367
|
+
self.config = config
|
|
368
|
+
self.cwd = Path(cwd)
|
|
369
|
+
self.store = store or build_store(config)
|
|
370
|
+
self._model = model
|
|
371
|
+
self.permission_mode = permission_mode
|
|
372
|
+
"""The mode the server was started with. It wins over the files."""
|
|
373
|
+
self.config_spec = list(config_spec or [])
|
|
374
|
+
"""The command line overrides. The files are read again for every session."""
|
|
375
|
+
self._runtimes: dict[str, SessionRuntime] = {}
|
|
376
|
+
|
|
377
|
+
def create(self, session: Session | None = None) -> SessionRuntime:
|
|
378
|
+
config = load_config(self.config_spec, cwd=self.cwd)
|
|
379
|
+
"""The configuration as it is now: what the user just wrote is in it."""
|
|
380
|
+
runtime = SessionRuntime(
|
|
381
|
+
config,
|
|
382
|
+
self.cwd,
|
|
383
|
+
store=self.store,
|
|
384
|
+
session=session,
|
|
385
|
+
model=self._model,
|
|
386
|
+
permission_mode=self.permission_mode,
|
|
387
|
+
registry=self,
|
|
388
|
+
)
|
|
389
|
+
self._runtimes[runtime.session.id] = runtime
|
|
390
|
+
return runtime
|
|
391
|
+
|
|
392
|
+
def get(self, session_id: str) -> SessionRuntime | None:
|
|
393
|
+
return self._runtimes.get(session_id)
|
|
394
|
+
|
|
395
|
+
def runtimes(self) -> list[SessionRuntime]:
|
|
396
|
+
return list(self._runtimes.values())
|
|
397
|
+
|
|
398
|
+
def rekey(self, runtime: SessionRuntime, old_id: str, new_id: str) -> None:
|
|
399
|
+
"""The session of a runtime changed; the registry follows."""
|
|
400
|
+
self._runtimes.pop(old_id, None)
|
|
401
|
+
self._runtimes[new_id] = runtime
|
|
402
|
+
|
|
403
|
+
def list(self) -> list[Session]:
|
|
404
|
+
return self.store.list(self.cwd)
|