-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommands.py
More file actions
242 lines (210 loc) · 7.53 KB
/
commands.py
File metadata and controls
242 lines (210 loc) · 7.53 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""Slash command framework for interactive use."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from apecode.skills import SkillCatalog
from apecode.subagents import SubagentProxy
from apecode.tools import ToolRegistry
CommandHandler = Callable[[str], "CommandResult"]
@dataclass(slots=True)
class CommandResult:
"""Result of one slash command execution."""
output: str
agent_input: str | None = None
should_exit: bool = False
@dataclass(slots=True)
class SlashCommand:
"""Command metadata and handler."""
name: str
description: str
usage: str
handler: CommandHandler
class CommandRegistry:
"""Registry for slash commands."""
def __init__(self) -> None:
self._commands: dict[str, SlashCommand] = {}
def register(self, command: SlashCommand, *, replace: bool = False) -> None:
if not replace and command.name in self._commands:
raise ValueError(f"command already registered: /{command.name}")
self._commands[command.name] = command
def get(self, name: str) -> SlashCommand | None:
return self._com
F426
mands.get(name)
def list_commands(self) -> list[SlashCommand]:
return [self._commands[name] for name in sorted(self._commands)]
def run(self, raw_input: str) -> CommandResult | None:
if not raw_input.startswith("/"):
return None
payload = raw_input[1:].strip()
if not payload:
return CommandResult(output="Empty slash command. Use /help.")
if " " in payload:
name, args = payload.split(" ", 1)
args = args.strip()
else:
name, args = payload, ""
command = self.get(name)
if command is None:
return CommandResult(output=f"Unknown command: /{name}. Use /help.")
return command.handler(args)
def create_template_command(
*,
name: str,
description: str,
usage: str,
output: str,
agent_input_template: str | None = None,
) -> SlashCommand:
"""Build a slash command from a simple text template."""
def _handler(args: str) -> CommandResult:
agent_input: str | None = None
if agent_input_template is not None:
agent_input = agent_input_template.replace("{args}", args.strip())
return CommandResult(output=output, agent_input=agent_input)
return SlashCommand(
name=name,
description=description,
usage=usage,
handler=_handler,
)
def create_default_commands(
*,
tools: ToolRegistry,
skills: SkillCatalog,
subagents: SubagentProxy | None = None,
) -> CommandRegistry:
"""Build built-in slash commands."""
registry = CommandRegistry()
def _help(_args: str) -> CommandResult:
lines = ["Available commands:"]
for command in registry.list_commands():
lines.append(f"- /{command.name}: {command.description} ({command.usage})")
return CommandResult(output="\n".join(lines))
def _tools(_args: str) -> CommandResult:
names = tools.list_tool_names()
if not names:
return CommandResult(output="No tools registered.")
return CommandResult(output="Tools:\n" + "\n".join(f"- {name}" for name in names))
def _skills(_args: str) -> CommandResult:
records = skills.list_skills()
if not records:
return CommandResult(output="No skills found.")
lines = ["Skills:"]
for skill in records:
lines.append(f"- {skill.name}: {skill.description}")
return CommandResult(output="\n".join(lines))
def _skill(args: str) -> CommandResult:
if not args:
return CommandResult(output="Usage: /skill <name> [extra request]")
parts = args.split(" ", 1)
name = parts[0].strip().lower()
extra = parts[1].strip() if len(parts) > 1 else ""
skill = skills.get(name)
if skill is None:
return CommandResult(output=f"Skill not found: {name}")
body = skill.read_text()
if extra:
body = f"{body}\n\nUser request:\n{extra}"
return CommandResult(output=f"Running skill `{name}`...", agent_input=body)
def _plan(_args: str) -> CommandResult:
plan = tools.context.plan
if not plan:
return CommandResult(output="Plan is empty.")
lines = ["Current plan:"]
for item in plan:
lines.append(f"- [{item['status']}] {item['step']}")
return CommandResult(output="\n".join(lines))
def _exit(_args: str) -> CommandResult:
return CommandResult(output="Bye.", should_exit=True)
def _subagents(_args: str) -> CommandResult:
if subagents is None:
return CommandResult(output="Subagents are disabled.")
profiles = subagents.list_profiles()
lines = ["Subagent profiles:"]
for profile in profiles:
lines.append(f"- {profile['name']}: {profile['description']}")
return CommandResult(output="\n".join(lines))
def _delegate(args: str) -> CommandResult:
if subagents is None:
return CommandResult(output="Subagents are disabled.")
if not args.strip():
return CommandResult(output="Usage: /delegate [profile::] <task>")
if "::" in args:
profile, task = args.split("::", 1)
profile = profile.strip().lower()
task = task.strip()
else:
profile, task = "general", args.strip()
if not task:
return CommandResult(output="Usage: /delegate [profile::] <task>")
try:
output = subagents.run(task=task, profile=profile)
except ValueError as exc:
return CommandResult(output=str(exc))
except RuntimeError as exc:
return CommandResult(output=f"Subagent execution failed: {exc}")
return CommandResult(output=f"Subagent `{profile}`:\n{output}")
registry.register(
SlashCommand(
name="help",
description="Show available slash commands.",
usage="/help",
handler=_help,
)
)
registry.register(
SlashCommand(
name="tools",
description="List currently registered tools.",
usage="/tools",
handler=_tools,
)
)
registry.register(
SlashCommand(
name="skills",
description="List discovered skills.",
usage="/skills",
handler=_skills,
)
)
registry.register(
SlashCommand(
name="skill",
description="Run one skill as a prompt template.",
usage="/skill <name> [extra request]",
handler=_skill,
)
)
registry.register(
SlashCommand(
name="plan",
description="Print the latest in-memory plan.",
usage="/plan",
handler=_plan,
)
)
registry.register(
SlashCommand(
name="subagents",
description="List available subagent profiles.",
usage="/subagents",
handler=_subagents,
)
)
registry.register(
SlashCommand(
name="delegate",
description="Delegate a focused sub-task to a subagent.",
usage="/delegate [profile::] <task>",
handler=_delegate,
)
)
registry.register(
SlashCommand(
name="exit",
description="Exit current session.",
usage="/exit",
handler=_exit,
)
)
return registry