braid-coding-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.
- braid_coding_agent/__init__.py +13 -0
- braid_coding_agent/assembly.py +460 -0
- braid_coding_agent/cancellation.py +25 -0
- braid_coding_agent/checkpoint/__init__.py +106 -0
- braid_coding_agent/checkpoint/git.py +305 -0
- braid_coding_agent/checkpoint/settings.py +26 -0
- braid_coding_agent/checkpoint/store.py +1261 -0
- braid_coding_agent/checkpoint/workspace.py +220 -0
- braid_coding_agent/cli.py +2168 -0
- braid_coding_agent/commands/__init__.py +50 -0
- braid_coding_agent/commands/auth.py +161 -0
- braid_coding_agent/commands/config.py +180 -0
- braid_coding_agent/commands/doctor.py +511 -0
- braid_coding_agent/commands/mcp.py +928 -0
- braid_coding_agent/commands/models.py +95 -0
- braid_coding_agent/commands/setup.py +89 -0
- braid_coding_agent/commands/uninstall.py +277 -0
- braid_coding_agent/commands/upgrade.py +381 -0
- braid_coding_agent/compaction/__init__.py +174 -0
- braid_coding_agent/compaction/prepare.py +430 -0
- braid_coding_agent/compaction/summarize.py +542 -0
- braid_coding_agent/compaction/tokens.py +204 -0
- braid_coding_agent/compaction/tool_results.py +196 -0
- braid_coding_agent/compaction/utils.py +181 -0
- braid_coding_agent/config.py +2776 -0
- braid_coding_agent/config_files.py +191 -0
- braid_coding_agent/credentials.py +252 -0
- braid_coding_agent/discovery.py +415 -0
- braid_coding_agent/extensions/__init__.py +77 -0
- braid_coding_agent/extensions/api.py +480 -0
- braid_coding_agent/extensions/runner.py +553 -0
- braid_coding_agent/formatting/__init__.py +62 -0
- braid_coding_agent/formatting/settings.py +87 -0
- braid_coding_agent/formatting/tool.py +179 -0
- braid_coding_agent/lsp/__init__.py +105 -0
- braid_coding_agent/lsp/client.py +546 -0
- braid_coding_agent/lsp/manager.py +187 -0
- braid_coding_agent/lsp/model.py +316 -0
- braid_coding_agent/lsp/protocol.py +105 -0
- braid_coding_agent/lsp/settings.py +120 -0
- braid_coding_agent/lsp/tool.py +429 -0
- braid_coding_agent/mcp/__init__.py +101 -0
- braid_coding_agent/mcp/cache.py +230 -0
- braid_coding_agent/mcp/catalog.py +108 -0
- braid_coding_agent/mcp/config.py +664 -0
- braid_coding_agent/mcp/config_store.py +142 -0
- braid_coding_agent/mcp/inspector.py +85 -0
- braid_coding_agent/mcp/panel.py +254 -0
- braid_coding_agent/mcp/runtime.py +1060 -0
- braid_coding_agent/mcp/tools.py +579 -0
- braid_coding_agent/mcp/trust.py +150 -0
- braid_coding_agent/mcp/tui_panel.py +444 -0
- braid_coding_agent/messages.py +152 -0
- braid_coding_agent/models_view.py +105 -0
- braid_coding_agent/modes/__init__.py +64 -0
- braid_coding_agent/modes/json_mode.py +480 -0
- braid_coding_agent/modes/lifecycle.py +217 -0
- braid_coding_agent/modes/print_mode.py +257 -0
- braid_coding_agent/modes/repl.py +2687 -0
- braid_coding_agent/modes/repl_commands.py +263 -0
- braid_coding_agent/modes/rpc.py +953 -0
- braid_coding_agent/modes/skill_commands.py +88 -0
- braid_coding_agent/modes/tui.py +2226 -0
- braid_coding_agent/modes/tui_control.py +90 -0
- braid_coding_agent/modes/tui_model.py +1043 -0
- braid_coding_agent/modes/tui_render.py +327 -0
- braid_coding_agent/modes/tui_rich.py +751 -0
- braid_coding_agent/modes/tui_style.py +42 -0
- braid_coding_agent/modes/wire.py +81 -0
- braid_coding_agent/netsafe.py +82 -0
- braid_coding_agent/network/__init__.py +20 -0
- braid_coding_agent/network/cache.py +77 -0
- braid_coding_agent/network/fetch.py +311 -0
- braid_coding_agent/network/html_text.py +160 -0
- braid_coding_agent/network/search.py +347 -0
- braid_coding_agent/network/settings.py +103 -0
- braid_coding_agent/network/urls.py +89 -0
- braid_coding_agent/observability/__init__.py +101 -0
- braid_coding_agent/observability/audit.py +124 -0
- braid_coding_agent/observability/collector.py +314 -0
- braid_coding_agent/observability/exporter.py +74 -0
- braid_coding_agent/observability/logging_setup.py +149 -0
- braid_coding_agent/observability/redaction.py +96 -0
- braid_coding_agent/observability/settings.py +29 -0
- braid_coding_agent/observability/spans.py +239 -0
- braid_coding_agent/permissions/__init__.py +185 -0
- braid_coding_agent/permissions/command.py +586 -0
- braid_coding_agent/permissions/engine.py +522 -0
- braid_coding_agent/permissions/evaluate.py +309 -0
- braid_coding_agent/permissions/matching.py +233 -0
- braid_coding_agent/permissions/model.py +278 -0
- braid_coding_agent/permissions/policy.py +99 -0
- braid_coding_agent/permissions/readonly.py +236 -0
- braid_coding_agent/permissions/store.py +379 -0
- braid_coding_agent/project_init.py +313 -0
- braid_coding_agent/prompt/__init__.py +98 -0
- braid_coding_agent/prompt/context_files.py +253 -0
- braid_coding_agent/prompt/project_context.py +172 -0
- braid_coding_agent/prompt/system_prompt.py +378 -0
- braid_coding_agent/questions.py +71 -0
- braid_coding_agent/render/__init__.py +56 -0
- braid_coding_agent/render/batch.py +66 -0
- braid_coding_agent/render/history.py +143 -0
- braid_coding_agent/render/stream.py +823 -0
- braid_coding_agent/render/theme.py +243 -0
- braid_coding_agent/sandbox/__init__.py +89 -0
- braid_coding_agent/sandbox/backend.py +184 -0
- braid_coding_agent/sandbox/bubblewrap.py +96 -0
- braid_coding_agent/sandbox/operations.py +301 -0
- braid_coding_agent/sandbox/policy.py +221 -0
- braid_coding_agent/sandbox/proxy.py +320 -0
- braid_coding_agent/sandbox/seatbelt.py +222 -0
- braid_coding_agent/sdk/__init__.py +31 -0
- braid_coding_agent/sdk/async_client.py +366 -0
- braid_coding_agent/sdk/client.py +386 -0
- braid_coding_agent/sdk/errors.py +52 -0
- braid_coding_agent/sdk/events.py +162 -0
- braid_coding_agent/sdk/types.py +63 -0
- braid_coding_agent/sdk/wire.py +59 -0
- braid_coding_agent/server/__init__.py +54 -0
- braid_coding_agent/server/errors.py +52 -0
- braid_coding_agent/server/http_server.py +419 -0
- braid_coding_agent/server/hub.py +530 -0
- braid_coding_agent/server/openapi.py +157 -0
- braid_coding_agent/server/registry.py +354 -0
- braid_coding_agent/server/router.py +275 -0
- braid_coding_agent/server/run.py +148 -0
- braid_coding_agent/server/schema.py +194 -0
- braid_coding_agent/server/service.py +975 -0
- braid_coding_agent/session/__init__.py +293 -0
- braid_coding_agent/session/agent_session.py +2706 -0
- braid_coding_agent/session/context_breakdown.py +302 -0
- braid_coding_agent/session/discovery.py +212 -0
- braid_coding_agent/session/entries.py +809 -0
- braid_coding_agent/session/evidence.py +295 -0
- braid_coding_agent/session/lock.py +176 -0
- braid_coding_agent/session/manager.py +1185 -0
- braid_coding_agent/session/plan.py +243 -0
- braid_coding_agent/session/plan_mode.py +423 -0
- braid_coding_agent/session/projection.py +262 -0
- braid_coding_agent/session/rewind.py +257 -0
- braid_coding_agent/session/status.py +240 -0
- braid_coding_agent/settings.py +94 -0
- braid_coding_agent/skills/__init__.py +70 -0
- braid_coding_agent/skills/loader.py +681 -0
- braid_coding_agent/startup/__init__.py +76 -0
- braid_coding_agent/startup/cache.py +72 -0
- braid_coding_agent/startup/diagnostics.py +37 -0
- braid_coding_agent/startup/monitor.py +245 -0
- braid_coding_agent/startup/phases.py +79 -0
- braid_coding_agent/startup/project.py +165 -0
- braid_coding_agent/startup/report.py +560 -0
- braid_coding_agent/startup/scheduler.py +347 -0
- braid_coding_agent/statusline.py +163 -0
- braid_coding_agent/subagents/__init__.py +22 -0
- braid_coding_agent/subagents/contract.py +75 -0
- braid_coding_agent/subagents/definitions.py +583 -0
- braid_coding_agent/subagents/registry.py +277 -0
- braid_coding_agent/subagents/runtime.py +610 -0
- braid_coding_agent/subagents/settings.py +156 -0
- braid_coding_agent/subagents/worktree.py +63 -0
- braid_coding_agent/tools/__init__.py +327 -0
- braid_coding_agent/tools/args.py +87 -0
- braid_coding_agent/tools/bash.py +359 -0
- braid_coding_agent/tools/edit.py +298 -0
- braid_coding_agent/tools/edit_diff.py +344 -0
- braid_coding_agent/tools/find.py +158 -0
- braid_coding_agent/tools/grep.py +305 -0
- braid_coding_agent/tools/ignore.py +198 -0
- braid_coding_agent/tools/ls.py +127 -0
- braid_coding_agent/tools/mutation_queue.py +90 -0
- braid_coding_agent/tools/operations.py +274 -0
- braid_coding_agent/tools/output_accumulator.py +282 -0
- braid_coding_agent/tools/output_store.py +244 -0
- braid_coding_agent/tools/path_utils.py +91 -0
- braid_coding_agent/tools/plan.py +141 -0
- braid_coding_agent/tools/prompt_notes.py +52 -0
- braid_coding_agent/tools/question.py +180 -0
- braid_coding_agent/tools/read.py +223 -0
- braid_coding_agent/tools/submit_plan.py +102 -0
- braid_coding_agent/tools/truncate.py +423 -0
- braid_coding_agent/tools/write.py +100 -0
- braid_coding_agent/untrusted.py +50 -0
- braid_coding_agent/verify/__init__.py +78 -0
- braid_coding_agent/verify/runner.py +260 -0
- braid_coding_agent/verify/settings.py +64 -0
- braid_coding_agent/verify/tool.py +143 -0
- braid_coding_agent-0.1.0.dist-info/METADATA +19 -0
- braid_coding_agent-0.1.0.dist-info/RECORD +192 -0
- braid_coding_agent-0.1.0.dist-info/WHEEL +4 -0
- braid_coding_agent-0.1.0.dist-info/entry_points.txt +3 -0
- braid_coding_agent-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"""slash 命令的解析表。
|
|
2
|
+
|
|
3
|
+
把"认出是哪条命令"抽成纯函数:它需要被单独测试,而副作用需要会话对象,
|
|
4
|
+
两者耦合在一起就只能靠终端手工验证。
|
|
5
|
+
|
|
6
|
+
## 契约
|
|
7
|
+
|
|
8
|
+
- 只有以 ``/`` 开头的输入才是命令;其余是普通提问(返回 ``None``)。
|
|
9
|
+
- 命令名大小写不敏感,别名归一成规范名(``/q`` → ``quit``)。
|
|
10
|
+
- 参数是第一个空格之后的**原文**(保留内部空格),首尾空白去掉。
|
|
11
|
+
- 不接受参数的命令带了参数 → :class:`CommandError`,而不是静默忽略:
|
|
12
|
+
用户打了 ``/quit now`` 说明他以为有别的用法,静默退出更糟。
|
|
13
|
+
- 未知命令 → :class:`CommandError`,由 REPL 打印后继续循环(不退出)。
|
|
14
|
+
|
|
15
|
+
命令不进入模型上下文:解析发生在 :meth:`Repl._handle_command`,
|
|
16
|
+
它不发 ``session.prompt``。唯一可能产生消息的是命令自己明确调用的动作
|
|
17
|
+
(``/compact`` 会写一条压缩条目),那条消息属于会话操作的结果。
|
|
18
|
+
|
|
19
|
+
## 可用命令与预留接口
|
|
20
|
+
|
|
21
|
+
命令表里每条命令带一个 ``status``:``ready`` 表示 REPL 已经实现它的动作,
|
|
22
|
+
``reserved`` 表示只保留指令入口。预留接口照样可以解析、补全、带参数,执行时
|
|
23
|
+
统一输出 ``note`` 里的依赖说明;它们对应的能力需要改 agent 执行层,或者落在
|
|
24
|
+
尚未实现的切片里,本次改动不进入那一层。``/help`` 按这两组分别列出。
|
|
25
|
+
|
|
26
|
+
## 扩展命令
|
|
27
|
+
|
|
28
|
+
``parse_slash_command`` 接受一张额外的命令表(``extra``):扩展注册的命令由 REPL
|
|
29
|
+
在每次解析时传进来。合并规则是"内置优先"——扩展不能遮蔽 ``/help`` 这类内建命令,
|
|
30
|
+
冲突由 REPL 报出来(见 :meth:`Repl._extension_specs`)。放在解析参数里而不是让
|
|
31
|
+
解析器持有扩展运行时:这个函数是纯的,可以被单元测试直接喂字符串。
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
from __future__ import annotations
|
|
35
|
+
|
|
36
|
+
from collections.abc import Mapping
|
|
37
|
+
from dataclasses import dataclass
|
|
38
|
+
from typing import Literal
|
|
39
|
+
|
|
40
|
+
__all__ = [
|
|
41
|
+
"ALIASES",
|
|
42
|
+
"BUILTIN_COMMAND_NAMES",
|
|
43
|
+
"SPECS",
|
|
44
|
+
"CommandError",
|
|
45
|
+
"CommandSpec",
|
|
46
|
+
"CompletionCandidate",
|
|
47
|
+
"SlashCommand",
|
|
48
|
+
"parse_slash_command",
|
|
49
|
+
"reserved_spec",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class CommandError(Exception):
|
|
54
|
+
"""命令不可用(未知、空名、多余参数)。REPL 打印它,不退出。"""
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass(slots=True, frozen=True)
|
|
58
|
+
class CommandSpec:
|
|
59
|
+
"""一条命令的元信息:规范名、用法、说明、参数、实现状态与依赖说明。"""
|
|
60
|
+
|
|
61
|
+
name: str
|
|
62
|
+
usage: str
|
|
63
|
+
summary: str
|
|
64
|
+
takes_args: bool = False
|
|
65
|
+
status: Literal["ready", "reserved"] = "ready"
|
|
66
|
+
note: str = ""
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@dataclass(slots=True, frozen=True)
|
|
70
|
+
class CompletionCandidate:
|
|
71
|
+
"""一条补全候选:接受后写进输入行的文本、列表里显示的用法与一句话说明。
|
|
72
|
+
|
|
73
|
+
技能与内建命令共用这个形状:两者的差别只在注册表,补全列表因此只有一份。
|
|
74
|
+
``hint`` 是带参数占位的用法,``summary`` 是弹出列表右侧的说明。
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
insert: str
|
|
78
|
+
summary: str = ""
|
|
79
|
+
hint: str = ""
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
#: 命令表。``/help`` 按 ready / reserved 分组,组内顺序即这里的顺序。
|
|
83
|
+
SPECS: tuple[CommandSpec, ...] = (
|
|
84
|
+
CommandSpec("help", "/help", "列出可用命令与预留接口"),
|
|
85
|
+
CommandSpec("model", "/model [provider/id]", "查看模型目录或切换模型", True),
|
|
86
|
+
CommandSpec("models", "/models", "查看模型目录、当前选中项与可用性"),
|
|
87
|
+
CommandSpec(
|
|
88
|
+
"mcp",
|
|
89
|
+
"/mcp [show|tools|schema|history|enable|disable|reconnect|refresh|reload]",
|
|
90
|
+
"查看 MCP 状态、工具 Schema 与调用历史,管理连接",
|
|
91
|
+
True,
|
|
92
|
+
),
|
|
93
|
+
CommandSpec("status", "/status", "查看会话状态、上下文占用、用量与费用"),
|
|
94
|
+
CommandSpec("context", "/context", "查看上下文占用、规则文件与自动压缩阈值"),
|
|
95
|
+
CommandSpec("monitor", "/monitor [specs|tasks]", "查看启动检查点、性能指标与后台任务"),
|
|
96
|
+
CommandSpec(
|
|
97
|
+
"statusline",
|
|
98
|
+
"/statusline [字段,...|reset]",
|
|
99
|
+
"查看或修改底部状态栏显示的字段",
|
|
100
|
+
True,
|
|
101
|
+
),
|
|
102
|
+
CommandSpec("init", "/init", "扫描仓库并生成 AGENTS.md 草稿"),
|
|
103
|
+
CommandSpec("compact", "/compact [要求]", "压缩当前上下文,可给摘要侧重点", True),
|
|
104
|
+
CommandSpec("resume", "/resume [序号|会话文件]", "续接一份会话,并显示它的记录", True),
|
|
105
|
+
CommandSpec("clear", "/clear", "开始一个新会话(旧会话已落盘)"),
|
|
106
|
+
CommandSpec("quit", "/quit", "退出 REPL"),
|
|
107
|
+
CommandSpec("branch", "/branch [序号]", "从当前会话的某条消息开新会话", True),
|
|
108
|
+
CommandSpec("export", "/export [路径]", "导出当前对话为 Markdown", True),
|
|
109
|
+
CommandSpec("effort", "/effort [级别]", "查看或设置推理强度", True),
|
|
110
|
+
CommandSpec("undo", "/undo", "恢复最近一次用户回合之前的文件状态"),
|
|
111
|
+
CommandSpec("redo", "/redo", "重新应用被撤销的那一回合的改动"),
|
|
112
|
+
CommandSpec(
|
|
113
|
+
"diff",
|
|
114
|
+
"/diff [--git]",
|
|
115
|
+
"查看本次会话产生的文件改动(--git 看原始 git diff)",
|
|
116
|
+
True,
|
|
117
|
+
),
|
|
118
|
+
CommandSpec("commit", "/commit [提交消息]", "查看改动、确认后提交当前仓库", True),
|
|
119
|
+
CommandSpec(
|
|
120
|
+
"worktree",
|
|
121
|
+
"/worktree [list|add <路径> <分支>|remove <路径>]",
|
|
122
|
+
"查看、添加、删除 Git 工作树",
|
|
123
|
+
True,
|
|
124
|
+
),
|
|
125
|
+
CommandSpec("git", "/git <branch|log> [参数]", "查看分支与最近提交", True),
|
|
126
|
+
CommandSpec("hooks", "/hooks", "查看 hook 配置与已装载扩展"),
|
|
127
|
+
CommandSpec(
|
|
128
|
+
"rewind",
|
|
129
|
+
"/rewind [序号] [--conversation|--files|--all]",
|
|
130
|
+
"回退会话(可选连同文件)到某一轮提问之前",
|
|
131
|
+
True,
|
|
132
|
+
),
|
|
133
|
+
CommandSpec(
|
|
134
|
+
"memory",
|
|
135
|
+
"/memory [子命令]",
|
|
136
|
+
"管理项目记忆",
|
|
137
|
+
True,
|
|
138
|
+
"reserved",
|
|
139
|
+
"项目记忆功能尚未实现,当前只保留管理入口",
|
|
140
|
+
),
|
|
141
|
+
CommandSpec(
|
|
142
|
+
"btw",
|
|
143
|
+
"/btw <问题>",
|
|
144
|
+
"临时询问,不写入会话历史",
|
|
145
|
+
True,
|
|
146
|
+
"reserved",
|
|
147
|
+
"side question 需要一次不进入主会话转录的独立模型请求,涉及 agent 执行层",
|
|
148
|
+
),
|
|
149
|
+
CommandSpec(
|
|
150
|
+
"plan",
|
|
151
|
+
"/plan [目标|show|approve|off]",
|
|
152
|
+
"进入规划模式(只读探索并产出计划);show 看计划,approve 批准,off 退出",
|
|
153
|
+
True,
|
|
154
|
+
),
|
|
155
|
+
CommandSpec(
|
|
156
|
+
"review",
|
|
157
|
+
"/review [范围]",
|
|
158
|
+
"发起代码审查",
|
|
159
|
+
True,
|
|
160
|
+
"reserved",
|
|
161
|
+
"代码审查需要一份专用代理定义;可用 /agents 查看现有子代理",
|
|
162
|
+
),
|
|
163
|
+
CommandSpec(
|
|
164
|
+
"agents",
|
|
165
|
+
"/agents [list|reload|create <名字>|<任务号>|switch <会话文件>]",
|
|
166
|
+
"查看、创建、重载子代理定义与任务结果,或切换到子会话",
|
|
167
|
+
True,
|
|
168
|
+
),
|
|
169
|
+
CommandSpec(
|
|
170
|
+
"tasks",
|
|
171
|
+
"/tasks [list|show <任务号>|cancel <任务号>|send <任务号> <消息>|clear]",
|
|
172
|
+
"查看与管理子代理任务:取消、追加消息、清理已结束记录",
|
|
173
|
+
True,
|
|
174
|
+
),
|
|
175
|
+
CommandSpec(
|
|
176
|
+
"background",
|
|
177
|
+
"/background <任务描述>",
|
|
178
|
+
"把当前会话分离成后台 Agent,继续完成一项独立任务",
|
|
179
|
+
True,
|
|
180
|
+
),
|
|
181
|
+
CommandSpec("skills", "/skills [关键词]", "列出已发现的技能、来源与调用方式", True),
|
|
182
|
+
CommandSpec("skill", "/skill <name>", "显式加载一个技能的正文到上下文", True),
|
|
183
|
+
CommandSpec(
|
|
184
|
+
"plugin",
|
|
185
|
+
"/plugin [子命令]",
|
|
186
|
+
"管理插件",
|
|
187
|
+
True,
|
|
188
|
+
"reserved",
|
|
189
|
+
"插件安装与启停尚未实现;当前版本通过 --extension 装载 Python 扩展",
|
|
190
|
+
),
|
|
191
|
+
CommandSpec(
|
|
192
|
+
"permission",
|
|
193
|
+
"/permission [子命令]",
|
|
194
|
+
"查看或管理权限模式、规则与本会话批准;/permissions 是别名",
|
|
195
|
+
True,
|
|
196
|
+
),
|
|
197
|
+
CommandSpec("sandbox", "/sandbox", "查看沙箱模式、后端、网络策略与最近的拒绝记录"),
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
_SPEC_BY_NAME: dict[str, CommandSpec] = {spec.name: spec for spec in SPECS}
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def reserved_spec(name: str) -> CommandSpec | None:
|
|
204
|
+
"""预留接口的元信息;可用命令与扩展命令返回 ``None``。"""
|
|
205
|
+
spec = _SPEC_BY_NAME.get(name)
|
|
206
|
+
return spec if spec is not None and spec.status == "reserved" else None
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
#: 别名 → 规范名。只保留敲一两次键就能打到的那几个。
|
|
210
|
+
ALIASES: dict[str, str] = {
|
|
211
|
+
"?": "help",
|
|
212
|
+
"h": "help",
|
|
213
|
+
"q": "quit",
|
|
214
|
+
"exit": "quit",
|
|
215
|
+
"new": "clear",
|
|
216
|
+
"ctx": "context",
|
|
217
|
+
"permissions": "permission",
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
#: 内建命令名与别名。技能名不能占用这些名字:``/help`` 这类入口被遮住就没法自救。
|
|
221
|
+
BUILTIN_COMMAND_NAMES: frozenset[str] = frozenset(_SPEC_BY_NAME) | frozenset(ALIASES)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
@dataclass(slots=True, frozen=True)
|
|
225
|
+
class SlashCommand:
|
|
226
|
+
"""解析结果:规范名 + 参数原文。"""
|
|
227
|
+
|
|
228
|
+
name: str
|
|
229
|
+
args: str = ""
|
|
230
|
+
raw: str = ""
|
|
231
|
+
|
|
232
|
+
@property
|
|
233
|
+
def spec(self) -> CommandSpec:
|
|
234
|
+
"""对应的命令元信息(``name`` 一定是规范名)。"""
|
|
235
|
+
return _SPEC_BY_NAME[self.name]
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def parse_slash_command(
|
|
239
|
+
text: str, extra: Mapping[str, CommandSpec] | None = None
|
|
240
|
+
) -> SlashCommand | None:
|
|
241
|
+
"""把一行输入解析成命令;不是命令时返回 ``None``。
|
|
242
|
+
|
|
243
|
+
:param extra: 扩展注册的命令。键是命令名,不含前导 ``/``。内建命令优先:
|
|
244
|
+
同名项被忽略,调用方负责报冲突。
|
|
245
|
+
:raises CommandError: 以 ``/`` 开头但不是合法命令(空名、未知、多余参数)。
|
|
246
|
+
"""
|
|
247
|
+
stripped = text.strip()
|
|
248
|
+
if not stripped.startswith("/"):
|
|
249
|
+
return None
|
|
250
|
+
body = stripped[1:]
|
|
251
|
+
if not body.strip():
|
|
252
|
+
raise CommandError("空命令:`/` 后面要跟命令名,输入 /help 查看")
|
|
253
|
+
head, _, rest = body.partition(" ")
|
|
254
|
+
name = ALIASES.get(head.lower(), head.lower())
|
|
255
|
+
spec = _SPEC_BY_NAME.get(name)
|
|
256
|
+
if spec is None and extra is not None:
|
|
257
|
+
spec = extra.get(name)
|
|
258
|
+
if spec is None:
|
|
259
|
+
raise CommandError(f"未知命令:/{head}(输入 /help 查看可用命令)")
|
|
260
|
+
args = rest.strip()
|
|
261
|
+
if args and not spec.takes_args:
|
|
262
|
+
raise CommandError(f"{spec.usage} 不接受参数,收到:{args}")
|
|
263
|
+
return SlashCommand(name=name, args=args, raw=stripped)
|