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,129 @@
|
|
|
1
|
+
"""The question the agent asks when a tool needs the permission of the user."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from textual.app import ComposeResult
|
|
7
|
+
from textual.containers import Vertical
|
|
8
|
+
from textual.widgets import Static
|
|
9
|
+
|
|
10
|
+
from coderagent.ui.widgets.diff_view import DiffView
|
|
11
|
+
from coderagent.ui.widgets.markup import literal
|
|
12
|
+
from coderagent.ui.widgets.tool_card import summarize
|
|
13
|
+
|
|
14
|
+
OPTIONS = (("y", "allow"), ("n", "deny"), ("a", "always allow"))
|
|
15
|
+
"""The answers, in the order the arrow keys walk through them."""
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Options(Static):
|
|
19
|
+
"""The answers of a prompt, one per line.
|
|
20
|
+
|
|
21
|
+
Every line is a numbered box: `[*]` when the answer is picked (the star is
|
|
22
|
+
red), `[-]` when it is not (the dash is grey).
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, items: Sequence[tuple[str, str]], *, multi: bool = False, **kwargs) -> None:
|
|
26
|
+
super().__init__("", classes="options", **kwargs)
|
|
27
|
+
self.items = list(items)
|
|
28
|
+
"""The answers as (key, label) pairs."""
|
|
29
|
+
self.multi = multi
|
|
30
|
+
"""Whether more than one answer can be picked."""
|
|
31
|
+
self.cursor = 0
|
|
32
|
+
"""The index of the answer the arrow keys are on."""
|
|
33
|
+
self.selected = {0}
|
|
34
|
+
"""The indexes of the answers that are picked."""
|
|
35
|
+
self.update(self.markup())
|
|
36
|
+
|
|
37
|
+
def move(self, delta: int) -> None:
|
|
38
|
+
"""Move the cursor, wrapping around at the ends."""
|
|
39
|
+
self.cursor = (self.cursor + delta) % len(self.items)
|
|
40
|
+
if not self.multi:
|
|
41
|
+
self.selected = {self.cursor}
|
|
42
|
+
self.update(self.markup())
|
|
43
|
+
|
|
44
|
+
def toggle(self, index: int) -> None:
|
|
45
|
+
"""Pick an answer, or unpick it when it already is."""
|
|
46
|
+
if self.multi:
|
|
47
|
+
self.selected.symmetric_difference_update({index})
|
|
48
|
+
else:
|
|
49
|
+
self.selected = {index}
|
|
50
|
+
self.cursor = index
|
|
51
|
+
self.update(self.markup())
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def selected_key(self) -> str:
|
|
55
|
+
"""The key of the answer under the cursor."""
|
|
56
|
+
return self.items[self.cursor][0]
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def selected_keys(self) -> list[str]:
|
|
60
|
+
"""The keys of the answers that are picked."""
|
|
61
|
+
return [self.items[index][0] for index in sorted(self.selected)]
|
|
62
|
+
|
|
63
|
+
def markup(self) -> str:
|
|
64
|
+
"""The answers, one per line, the picked ones with a red star."""
|
|
65
|
+
lines = []
|
|
66
|
+
for index, (key, label) in enumerate(self.items):
|
|
67
|
+
marker = "[red]*[/red]" if index in self.selected else "[gray]-[/gray]"
|
|
68
|
+
number = f"[bold]{index + 1}[/bold]" if index == self.cursor else f"[gray]{index + 1}[/gray]"
|
|
69
|
+
lines.append(f"{number} \\[{marker}] [bold]{key}[/bold] {label}")
|
|
70
|
+
lines.append(f"[dim]{self.hint()}[/dim]")
|
|
71
|
+
return "\n".join(lines)
|
|
72
|
+
|
|
73
|
+
def hint(self) -> str:
|
|
74
|
+
"""What the keys do."""
|
|
75
|
+
if self.multi:
|
|
76
|
+
return "↑/↓ 移动,空格选中,回车确认"
|
|
77
|
+
return "↑/↓ 移动,回车确认"
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class ApprovalPrompt(Vertical):
|
|
81
|
+
"""Shows what is about to happen, with the diff when it changes a file.
|
|
82
|
+
|
|
83
|
+
The answers can be picked with the arrow keys; the picked one is starred
|
|
84
|
+
in red.
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
DEFAULT_CSS = """
|
|
88
|
+
ApprovalPrompt {
|
|
89
|
+
height: auto;
|
|
90
|
+
margin: 1 0 0 0;
|
|
91
|
+
padding: 0 1;
|
|
92
|
+
border: round $warning;
|
|
93
|
+
}
|
|
94
|
+
ApprovalPrompt > .options {
|
|
95
|
+
height: auto;
|
|
96
|
+
}
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
def __init__(self, tool: str, params: dict[str, Any], reason: str = "", diff: str = "", **kwargs) -> None:
|
|
100
|
+
super().__init__(**kwargs)
|
|
101
|
+
self.tool = tool
|
|
102
|
+
self.params = params
|
|
103
|
+
self.reason = reason
|
|
104
|
+
self.diff = diff
|
|
105
|
+
self.options = Options(OPTIONS)
|
|
106
|
+
"""The answers the user picks from."""
|
|
107
|
+
|
|
108
|
+
def compose(self) -> ComposeResult:
|
|
109
|
+
yield Static(self._header(), classes="question")
|
|
110
|
+
yield self.options
|
|
111
|
+
yield DiffView(self.diff)
|
|
112
|
+
|
|
113
|
+
def _header(self) -> str:
|
|
114
|
+
lines = [f"[bold]Allow '{literal(self.tool)}'?[/bold]"]
|
|
115
|
+
summary = summarize(self.params)
|
|
116
|
+
if summary:
|
|
117
|
+
lines.append(f" {literal(summary)}")
|
|
118
|
+
if self.reason:
|
|
119
|
+
lines.append(f" [dim]{literal(self.reason)}[/dim]")
|
|
120
|
+
return "\n".join(lines)
|
|
121
|
+
|
|
122
|
+
def move(self, delta: int) -> None:
|
|
123
|
+
"""Move the cursor, wrapping around at the ends."""
|
|
124
|
+
self.options.move(delta)
|
|
125
|
+
|
|
126
|
+
@property
|
|
127
|
+
def selected_key(self) -> str:
|
|
128
|
+
"""The key of the answer under the cursor."""
|
|
129
|
+
return self.options.selected_key
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""The diff of a change, with the new lines in green and the old ones in red."""
|
|
2
|
+
|
|
3
|
+
import difflib
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from textual.widgets import Static
|
|
7
|
+
|
|
8
|
+
from coderagent.ui.widgets.markup import literal
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def make_diff(before: str, after: str, path: str = "") -> str:
|
|
12
|
+
"""The unified diff between two texts."""
|
|
13
|
+
return "\n".join(
|
|
14
|
+
difflib.unified_diff(
|
|
15
|
+
before.splitlines(),
|
|
16
|
+
after.splitlines(),
|
|
17
|
+
fromfile=f"a/{path}" if path else "a",
|
|
18
|
+
tofile=f"b/{path}" if path else "b",
|
|
19
|
+
lineterm="",
|
|
20
|
+
)
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def diff_of_file(path: Path, before: str) -> str:
|
|
25
|
+
"""The diff between what the file was and what it is now."""
|
|
26
|
+
try:
|
|
27
|
+
after = path.read_text(encoding="utf-8")
|
|
28
|
+
except OSError:
|
|
29
|
+
return ""
|
|
30
|
+
return make_diff(before, after, path.name)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def prospective_diff(tool: str, params: dict, cwd: Path) -> str:
|
|
34
|
+
"""The diff a tool is about to make, before it makes it."""
|
|
35
|
+
if tool == "apply_patch":
|
|
36
|
+
from coderagent.tools.builtin.patch import preview
|
|
37
|
+
|
|
38
|
+
return preview(str(params.get("patch", "")), cwd)
|
|
39
|
+
path = params.get("path")
|
|
40
|
+
if not isinstance(path, str) or not path:
|
|
41
|
+
return ""
|
|
42
|
+
target = Path(path)
|
|
43
|
+
if not target.is_absolute():
|
|
44
|
+
target = cwd / target
|
|
45
|
+
try:
|
|
46
|
+
before = target.read_text(encoding="utf-8")
|
|
47
|
+
except OSError:
|
|
48
|
+
before = ""
|
|
49
|
+
if tool == "write":
|
|
50
|
+
after = str(params.get("content", ""))
|
|
51
|
+
elif tool == "edit":
|
|
52
|
+
after = before.replace(str(params.get("old_string", "")), str(params.get("new_string", "")), 1)
|
|
53
|
+
elif tool == "multi_edit":
|
|
54
|
+
after = before
|
|
55
|
+
for edit in params.get("edits") or []:
|
|
56
|
+
after = after.replace(str(edit.get("old_string", "")), str(edit.get("new_string", "")), 1)
|
|
57
|
+
else:
|
|
58
|
+
return ""
|
|
59
|
+
return make_diff(before, after, target.name)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def colorize_diff(diff: str) -> str:
|
|
63
|
+
"""The diff, with the colors of the lines."""
|
|
64
|
+
lines = []
|
|
65
|
+
for line in diff.splitlines():
|
|
66
|
+
text = literal(line)
|
|
67
|
+
if line.startswith(("+++", "---")):
|
|
68
|
+
lines.append(f"[dim]{text}[/dim]")
|
|
69
|
+
elif line.startswith("+"):
|
|
70
|
+
lines.append(f"[green]{text}[/green]")
|
|
71
|
+
elif line.startswith("-"):
|
|
72
|
+
lines.append(f"[red]{text}[/red]")
|
|
73
|
+
elif line.startswith("@@"):
|
|
74
|
+
lines.append(f"[cyan]{text}[/cyan]")
|
|
75
|
+
else:
|
|
76
|
+
lines.append(text)
|
|
77
|
+
return "\n".join(lines)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class DiffView(Static):
|
|
81
|
+
"""Shows a diff. Empty when there is nothing to show."""
|
|
82
|
+
|
|
83
|
+
DEFAULT_CSS = """
|
|
84
|
+
DiffView {
|
|
85
|
+
height: auto;
|
|
86
|
+
}
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
def __init__(self, diff: str = "", **kwargs) -> None:
|
|
90
|
+
super().__init__(colorize_diff(diff), **kwargs)
|
|
91
|
+
self.diff = diff
|
|
92
|
+
|
|
93
|
+
def show_diff(self, diff: str) -> None:
|
|
94
|
+
"""Show a diff. An empty diff hides the view."""
|
|
95
|
+
self.diff = diff
|
|
96
|
+
self.update(colorize_diff(diff))
|
|
97
|
+
self.display = bool(diff)
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""The tree of the working directory, beside the conversation."""
|
|
2
|
+
|
|
3
|
+
import subprocess
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from textual.widgets import Tree
|
|
7
|
+
|
|
8
|
+
from coderagent.tools.builtin.search import list_entries
|
|
9
|
+
|
|
10
|
+
MAX_DEPTH = 3
|
|
11
|
+
"""How many levels of directories the tree shows."""
|
|
12
|
+
|
|
13
|
+
MARKS = {"M": "●", "A": "+", "?": "?", "D": "-", "R": "●"}
|
|
14
|
+
"""The mark of a file that git knows about: changed, added, untracked, deleted."""
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def mark_of(status: str) -> str:
|
|
18
|
+
"""The mark of a file with this git status."""
|
|
19
|
+
if "?" in status:
|
|
20
|
+
return MARKS["?"]
|
|
21
|
+
if "D" in status:
|
|
22
|
+
return MARKS["D"]
|
|
23
|
+
if "A" in status:
|
|
24
|
+
return MARKS["A"]
|
|
25
|
+
if "M" in status or "R" in status:
|
|
26
|
+
return MARKS["M"]
|
|
27
|
+
return ""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def changed_files(cwd: Path) -> dict[str, str]:
|
|
31
|
+
"""The files that git says are changed. Empty when there is no git here."""
|
|
32
|
+
try:
|
|
33
|
+
completed = subprocess.run(
|
|
34
|
+
["git", "status", "--porcelain"],
|
|
35
|
+
cwd=cwd,
|
|
36
|
+
capture_output=True,
|
|
37
|
+
text=True,
|
|
38
|
+
check=False,
|
|
39
|
+
timeout=10,
|
|
40
|
+
)
|
|
41
|
+
except (OSError, subprocess.SubprocessError):
|
|
42
|
+
return {}
|
|
43
|
+
if completed.returncode != 0:
|
|
44
|
+
return {}
|
|
45
|
+
changes = {}
|
|
46
|
+
for line in completed.stdout.splitlines():
|
|
47
|
+
if len(line) < 4:
|
|
48
|
+
continue
|
|
49
|
+
path = line[3:].strip()
|
|
50
|
+
if " -> " in path:
|
|
51
|
+
"""A file that was renamed: the new name is the one that matters."""
|
|
52
|
+
path = path.split(" -> ")[-1]
|
|
53
|
+
changes[path] = line[:2].strip() or "?"
|
|
54
|
+
return changes
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class FileTree(Tree):
|
|
58
|
+
"""The files of the directories that are open to the agent, with the ones that changed marked."""
|
|
59
|
+
|
|
60
|
+
def __init__(self, cwd: Path, roots: list[Path] | None = None, **kwargs) -> None:
|
|
61
|
+
self.cwd = Path(cwd)
|
|
62
|
+
self.roots = [Path(root) for root in (roots or [self.cwd])]
|
|
63
|
+
"""The directories the user opened to the agent: the tree lists those and nothing else."""
|
|
64
|
+
super().__init__(self.roots[0].name if len(self.roots) == 1 else "files", **kwargs)
|
|
65
|
+
self.show_root = False
|
|
66
|
+
self.display = False
|
|
67
|
+
"""The tree is hidden until the user asks for it."""
|
|
68
|
+
|
|
69
|
+
def reload(self) -> None:
|
|
70
|
+
"""Read the directories again and show what is there now."""
|
|
71
|
+
self.clear()
|
|
72
|
+
for root in self.roots:
|
|
73
|
+
parent = self.root if len(self.roots) == 1 else self.root.add(root.name + "/", data=root.as_posix())
|
|
74
|
+
self._fill(parent, root)
|
|
75
|
+
|
|
76
|
+
def _fill(self, parent, root: Path) -> None:
|
|
77
|
+
"""The entries of a directory, under the node that holds it."""
|
|
78
|
+
changes = changed_files(root)
|
|
79
|
+
nodes = {Path("."): parent}
|
|
80
|
+
for path, kind in list_entries(root, MAX_DEPTH):
|
|
81
|
+
relative = path.relative_to(root)
|
|
82
|
+
node_parent = nodes.get(relative.parent)
|
|
83
|
+
if node_parent is None:
|
|
84
|
+
continue
|
|
85
|
+
label = f"{relative.name}/" if kind == "d" else relative.name
|
|
86
|
+
mark = mark_of(changes.get(relative.as_posix(), ""))
|
|
87
|
+
if mark:
|
|
88
|
+
label = f"{mark} {label}"
|
|
89
|
+
node = (
|
|
90
|
+
node_parent.add_leaf(label, data=self._data(path))
|
|
91
|
+
if kind == "f"
|
|
92
|
+
else node_parent.add(label, data=self._data(path))
|
|
93
|
+
)
|
|
94
|
+
nodes[relative] = node
|
|
95
|
+
|
|
96
|
+
def _data(self, path: Path) -> str:
|
|
97
|
+
"""The path of a node, the way the agent can be given it.
|
|
98
|
+
|
|
99
|
+
A file of the working directory is named relative to it; a file of another
|
|
100
|
+
directory has to be named with its whole path.
|
|
101
|
+
"""
|
|
102
|
+
try:
|
|
103
|
+
return path.relative_to(self.cwd).as_posix()
|
|
104
|
+
except ValueError:
|
|
105
|
+
return path.as_posix()
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""A message of the agent, rendered as Markdown.
|
|
2
|
+
|
|
3
|
+
The text arrives piece by piece, so it is shown as it comes. When the answer is
|
|
4
|
+
complete it is rendered as Markdown: the code blocks, the tables and the lists
|
|
5
|
+
look the way they look in the README of a project.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from textual.app import ComposeResult
|
|
9
|
+
from textual.containers import Vertical
|
|
10
|
+
from textual.widgets import Markdown, Static
|
|
11
|
+
|
|
12
|
+
from coderagent.ui.widgets.markup import literal
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class MarkdownMessage(Vertical):
|
|
16
|
+
"""The answer of the agent, with its Markdown rendered."""
|
|
17
|
+
|
|
18
|
+
DEFAULT_CSS = """
|
|
19
|
+
MarkdownMessage {
|
|
20
|
+
height: auto;
|
|
21
|
+
margin: 1 0 0 0;
|
|
22
|
+
padding: 0 1;
|
|
23
|
+
|
|
24
|
+
Markdown {
|
|
25
|
+
padding: 0;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(self, content: str = "", reasoning: str = "", label: str = "Agent", **kwargs) -> None:
|
|
31
|
+
super().__init__(**kwargs)
|
|
32
|
+
self.content = content
|
|
33
|
+
"""The answer, as it was said."""
|
|
34
|
+
self.reasoning = reasoning
|
|
35
|
+
self.label = label
|
|
36
|
+
|
|
37
|
+
def compose(self) -> ComposeResult:
|
|
38
|
+
yield Static(f"[bold]{self.label}[/bold]")
|
|
39
|
+
if self.reasoning:
|
|
40
|
+
yield Static(f"[dim]{literal(self.reasoning)}[/dim]")
|
|
41
|
+
yield Markdown(self.content)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
__all__ = ["MarkdownMessage"]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""Text that comes from the model, as markup the interface can show."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def literal(text: str) -> str:
|
|
5
|
+
"""Escape a text, so that Textual does not read it as markup.
|
|
6
|
+
|
|
7
|
+
`textual.markup.escape` only escapes the tags of Rich; Textual reads every
|
|
8
|
+
`[` as the start of a tag, so the brackets are escaped by hand.
|
|
9
|
+
"""
|
|
10
|
+
return text.replace("[", "\\[")
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""A message of the conversation: what the user said, what the agent answered."""
|
|
2
|
+
|
|
3
|
+
from textual.widgets import Static
|
|
4
|
+
|
|
5
|
+
from coderagent.ui.widgets.markup import literal
|
|
6
|
+
|
|
7
|
+
ROLE_LABEL = {"user": "You", "assistant": "Agent", "command": "Command", "error": "Error", "notice": "Notice"}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MessageBlock(Static):
|
|
11
|
+
"""One message. The reasoning of the agent is shown small and dim."""
|
|
12
|
+
|
|
13
|
+
DEFAULT_CSS = """
|
|
14
|
+
MessageBlock {
|
|
15
|
+
height: auto;
|
|
16
|
+
margin: 1 0 0 0;
|
|
17
|
+
padding: 0 1;
|
|
18
|
+
}
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, role: str, content: str = "", reasoning: str = "", **kwargs) -> None:
|
|
22
|
+
super().__init__(**kwargs)
|
|
23
|
+
self.role = role
|
|
24
|
+
self.text = content
|
|
25
|
+
"""The message, as it was said. It is not markup."""
|
|
26
|
+
self.reasoning = reasoning
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def content(self) -> str:
|
|
30
|
+
"""The message, as it was said. It is not markup."""
|
|
31
|
+
return self.text
|
|
32
|
+
|
|
33
|
+
@content.setter
|
|
34
|
+
def content(self, text: str) -> None:
|
|
35
|
+
"""Set the message. The text of the model is not markup."""
|
|
36
|
+
self.text = text
|
|
37
|
+
self.clear_cached_dimensions()
|
|
38
|
+
self.refresh(layout=True)
|
|
39
|
+
|
|
40
|
+
def render(self) -> str:
|
|
41
|
+
lines = [f"[bold]{ROLE_LABEL.get(self.role, self.role)}[/bold]"]
|
|
42
|
+
if self.reasoning:
|
|
43
|
+
lines.append(f"[dim]{literal(self.reasoning)}[/dim]")
|
|
44
|
+
if self.text:
|
|
45
|
+
lines.append(literal(self.text))
|
|
46
|
+
return "\n".join(lines)
|
|
47
|
+
|
|
48
|
+
def append(self, text: str) -> None:
|
|
49
|
+
"""Add text to the message, while it is still arriving."""
|
|
50
|
+
self.text += text
|
|
51
|
+
self.refresh()
|
|
52
|
+
|
|
53
|
+
def set_content(self, text: str) -> None:
|
|
54
|
+
"""Set the text of the message."""
|
|
55
|
+
self.text = text
|
|
56
|
+
self.clear_cached_dimensions()
|
|
57
|
+
self.refresh(layout=True)
|
|
58
|
+
|
|
59
|
+
def add_reasoning(self, text: str) -> None:
|
|
60
|
+
self.reasoning += text
|
|
61
|
+
self.refresh()
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Complete the path of a file when the user writes `@`."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import time
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from textual.suggester import Suggester
|
|
8
|
+
|
|
9
|
+
IGNORED = {
|
|
10
|
+
".git",
|
|
11
|
+
".mypy_cache",
|
|
12
|
+
".pytest_cache",
|
|
13
|
+
".ruff_cache",
|
|
14
|
+
".tox",
|
|
15
|
+
".venv",
|
|
16
|
+
"__pycache__",
|
|
17
|
+
"build",
|
|
18
|
+
"dist",
|
|
19
|
+
"node_modules",
|
|
20
|
+
"venv",
|
|
21
|
+
}
|
|
22
|
+
"""The directories that hold no file the user wants to mention."""
|
|
23
|
+
|
|
24
|
+
LISTED_FOR = 5.0
|
|
25
|
+
"""The seconds a list of files is used before it is made again."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class PathSuggester(Suggester):
|
|
29
|
+
"""Suggests the path of a file when the user writes `@`.
|
|
30
|
+
|
|
31
|
+
The suggestion is the whole input with the path filled in, because that is
|
|
32
|
+
what the widget of textual expects.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(self, cwd: Path) -> None:
|
|
36
|
+
super().__init__(case_sensitive=True, use_cache=False)
|
|
37
|
+
"""The matching ignores the case, but the path that comes out keeps it."""
|
|
38
|
+
self.cwd = Path(cwd)
|
|
39
|
+
self._paths: list[str] = []
|
|
40
|
+
self._listed = 0.0
|
|
41
|
+
|
|
42
|
+
def paths(self) -> list[str]:
|
|
43
|
+
"""The files under the working directory, relative to it, the shallow ones first."""
|
|
44
|
+
if self._paths and time.time() - self._listed < LISTED_FOR:
|
|
45
|
+
return self._paths
|
|
46
|
+
found = []
|
|
47
|
+
for root, directories, names in os.walk(self.cwd, followlinks=False):
|
|
48
|
+
directories[:] = [name for name in directories if name not in IGNORED and not name.startswith(".")]
|
|
49
|
+
for name in names:
|
|
50
|
+
found.append(str(Path(root, name).relative_to(self.cwd)))
|
|
51
|
+
self._paths = sorted(found, key=lambda path: (path.count("/"), path))
|
|
52
|
+
self._listed = time.time()
|
|
53
|
+
return self._paths
|
|
54
|
+
|
|
55
|
+
async def get_suggestion(self, value: str) -> str | None:
|
|
56
|
+
"""The input with the path after the last `@` completed."""
|
|
57
|
+
at = value.rfind("@")
|
|
58
|
+
if at < 0 or (at > 0 and not value[at - 1].isspace()):
|
|
59
|
+
return None
|
|
60
|
+
fragment = value[at + 1 :]
|
|
61
|
+
if " " in fragment:
|
|
62
|
+
return None
|
|
63
|
+
for path in self.paths():
|
|
64
|
+
if path.casefold().startswith(fragment.casefold()):
|
|
65
|
+
return value[: at + 1] + path
|
|
66
|
+
return None
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""A panel to pick another session of this directory."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
from textual.app import ComposeResult
|
|
6
|
+
from textual.containers import Vertical
|
|
7
|
+
from textual.screen import ModalScreen
|
|
8
|
+
from textual.widgets import OptionList, Static
|
|
9
|
+
|
|
10
|
+
from coderagent.core.session import Session
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class SessionPicker(ModalScreen[str | None]):
|
|
14
|
+
"""The sessions of this directory; the one that is picked is the one to continue."""
|
|
15
|
+
|
|
16
|
+
BINDINGS = [("escape", "dismiss", "Close")]
|
|
17
|
+
|
|
18
|
+
DEFAULT_CSS = """
|
|
19
|
+
SessionPicker {
|
|
20
|
+
align: center middle;
|
|
21
|
+
}
|
|
22
|
+
SessionPicker > Vertical {
|
|
23
|
+
width: 70%;
|
|
24
|
+
height: 60%;
|
|
25
|
+
border: round $accent;
|
|
26
|
+
background: $surface;
|
|
27
|
+
}
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(self, sessions: list[Session]) -> None:
|
|
31
|
+
super().__init__()
|
|
32
|
+
self.sessions = sessions
|
|
33
|
+
|
|
34
|
+
def compose(self) -> ComposeResult:
|
|
35
|
+
with Vertical():
|
|
36
|
+
yield Static("The sessions of this directory. Enter continues it, escape closes.")
|
|
37
|
+
yield OptionList(*(self.label(session) for session in self.sessions), id="sessions")
|
|
38
|
+
|
|
39
|
+
def on_mount(self) -> None:
|
|
40
|
+
self.query_one("#sessions", OptionList).focus()
|
|
41
|
+
|
|
42
|
+
def on_option_list_option_selected(self, event: OptionList.OptionSelected) -> None:
|
|
43
|
+
self.dismiss(self.sessions[event.option_index].id)
|
|
44
|
+
|
|
45
|
+
@staticmethod
|
|
46
|
+
def label(session: Session) -> str:
|
|
47
|
+
"""The line of a session in the list."""
|
|
48
|
+
when = time.strftime("%m-%d %H:%M", time.localtime(session.updated_at))
|
|
49
|
+
return f"{session.summary or session.id} {session.turn_count:>3} turns {when}"
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""The bar at the top: model, tokens, cost, directory and permission mode."""
|
|
2
|
+
|
|
3
|
+
from textual.widgets import Static
|
|
4
|
+
|
|
5
|
+
from coderagent.core.telemetry import money
|
|
6
|
+
from coderagent.ui.widgets.markup import literal
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class StatusBar(Static):
|
|
10
|
+
"""One line with the state of the session."""
|
|
11
|
+
|
|
12
|
+
DEFAULT_CSS = """
|
|
13
|
+
StatusBar {
|
|
14
|
+
height: 1;
|
|
15
|
+
background: $panel;
|
|
16
|
+
color: $text-muted;
|
|
17
|
+
}
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self, **kwargs) -> None:
|
|
21
|
+
super().__init__(**kwargs)
|
|
22
|
+
self.model = ""
|
|
23
|
+
self.tokens = 0
|
|
24
|
+
self.window = 0
|
|
25
|
+
self.cost = 0.0
|
|
26
|
+
self.priced = True
|
|
27
|
+
self.cwd = ""
|
|
28
|
+
self.mode = ""
|
|
29
|
+
self.state = ""
|
|
30
|
+
self.sandbox = ""
|
|
31
|
+
|
|
32
|
+
def update_status(self, **kwargs) -> None:
|
|
33
|
+
"""Change what the bar shows and redraw it."""
|
|
34
|
+
for key, value in kwargs.items():
|
|
35
|
+
setattr(self, key, value)
|
|
36
|
+
self.render_bar()
|
|
37
|
+
|
|
38
|
+
def render_bar(self) -> None:
|
|
39
|
+
tokens = f"{self.tokens / 1000:.1f}k/{self.window / 1000:.0f}k" if self.window else f"{self.tokens}"
|
|
40
|
+
parts = [part for part in (self.model, tokens, money(self.cost, self.priced), self.cwd) if part]
|
|
41
|
+
if self.sandbox and self.sandbox != "local":
|
|
42
|
+
parts.append(self.sandbox)
|
|
43
|
+
parts += [part for part in (self.mode, self.state) if part]
|
|
44
|
+
self.update(literal(" | ".join(parts)))
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""The list of things the agent wants to do."""
|
|
2
|
+
|
|
3
|
+
from textual.widgets import Static
|
|
4
|
+
|
|
5
|
+
from coderagent.ui.widgets.markup import literal
|
|
6
|
+
|
|
7
|
+
MARKS = {"completed": "◉", "in_progress": "◐", "pending": "○"}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class TodoPanel(Static):
|
|
11
|
+
"""The task list of the agent. Hidden when there is nothing to show."""
|
|
12
|
+
|
|
13
|
+
DEFAULT_CSS = """
|
|
14
|
+
TodoPanel {
|
|
15
|
+
height: auto;
|
|
16
|
+
max-height: 8;
|
|
17
|
+
padding: 0 1;
|
|
18
|
+
color: $text-muted;
|
|
19
|
+
}
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self, **kwargs) -> None:
|
|
23
|
+
super().__init__(**kwargs)
|
|
24
|
+
self.todos: list[dict] = []
|
|
25
|
+
self.display = False
|
|
26
|
+
|
|
27
|
+
def show_todos(self, todos: list[dict]) -> None:
|
|
28
|
+
"""Show the task list."""
|
|
29
|
+
self.todos = list(todos or [])
|
|
30
|
+
self.display = bool(self.todos)
|
|
31
|
+
self.update(self._text())
|
|
32
|
+
|
|
33
|
+
def _text(self) -> str:
|
|
34
|
+
lines = ["Todo"]
|
|
35
|
+
for todo in self.todos:
|
|
36
|
+
mark = MARKS.get(todo.get("status", "pending"), "○")
|
|
37
|
+
content = literal(str(todo.get("content", "")))
|
|
38
|
+
status = literal(str(todo.get("status", "pending")))
|
|
39
|
+
lines.append(f" {mark} {content} ({status})")
|
|
40
|
+
return "\n".join(lines)
|