braid-coding-agent 0.1.0__tar.gz
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-0.1.0/LICENSE +21 -0
- braid_coding_agent-0.1.0/PKG-INFO +19 -0
- braid_coding_agent-0.1.0/pyproject.toml +39 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/__init__.py +13 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/assembly.py +460 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/cancellation.py +25 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/checkpoint/__init__.py +106 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/checkpoint/git.py +305 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/checkpoint/settings.py +26 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/checkpoint/store.py +1261 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/checkpoint/workspace.py +220 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/cli.py +2168 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/commands/__init__.py +50 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/commands/auth.py +161 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/commands/config.py +180 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/commands/doctor.py +511 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/commands/mcp.py +928 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/commands/models.py +95 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/commands/setup.py +89 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/commands/uninstall.py +277 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/commands/upgrade.py +381 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/compaction/__init__.py +174 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/compaction/prepare.py +430 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/compaction/summarize.py +542 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/compaction/tokens.py +204 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/compaction/tool_results.py +196 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/compaction/utils.py +181 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/config.py +2776 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/config_files.py +191 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/credentials.py +252 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/discovery.py +415 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/extensions/__init__.py +77 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/extensions/api.py +480 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/extensions/runner.py +553 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/formatting/__init__.py +62 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/formatting/settings.py +87 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/formatting/tool.py +179 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/lsp/__init__.py +105 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/lsp/client.py +546 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/lsp/manager.py +187 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/lsp/model.py +316 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/lsp/protocol.py +105 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/lsp/settings.py +120 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/lsp/tool.py +429 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/mcp/__init__.py +101 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/mcp/cache.py +230 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/mcp/catalog.py +108 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/mcp/config.py +664 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/mcp/config_store.py +142 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/mcp/inspector.py +85 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/mcp/panel.py +254 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/mcp/runtime.py +1060 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/mcp/tools.py +579 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/mcp/trust.py +150 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/mcp/tui_panel.py +444 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/messages.py +152 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/models_view.py +105 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/__init__.py +64 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/json_mode.py +480 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/lifecycle.py +217 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/print_mode.py +257 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/repl.py +2687 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/repl_commands.py +263 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/rpc.py +953 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/skill_commands.py +88 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/tui.py +2226 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/tui_control.py +90 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/tui_model.py +1043 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/tui_render.py +327 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/tui_rich.py +751 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/tui_style.py +42 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/modes/wire.py +81 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/netsafe.py +82 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/network/__init__.py +20 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/network/cache.py +77 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/network/fetch.py +311 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/network/html_text.py +160 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/network/search.py +347 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/network/settings.py +103 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/network/urls.py +89 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/observability/__init__.py +101 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/observability/audit.py +124 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/observability/collector.py +314 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/observability/exporter.py +74 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/observability/logging_setup.py +149 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/observability/redaction.py +96 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/observability/settings.py +29 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/observability/spans.py +239 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/permissions/__init__.py +185 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/permissions/command.py +586 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/permissions/engine.py +522 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/permissions/evaluate.py +309 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/permissions/matching.py +233 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/permissions/model.py +278 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/permissions/policy.py +99 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/permissions/readonly.py +236 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/permissions/store.py +379 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/project_init.py +313 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/prompt/__init__.py +98 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/prompt/context_files.py +253 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/prompt/project_context.py +172 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/prompt/system_prompt.py +378 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/questions.py +71 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/render/__init__.py +56 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/render/batch.py +66 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/render/history.py +143 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/render/stream.py +823 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/render/theme.py +243 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sandbox/__init__.py +89 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sandbox/backend.py +184 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sandbox/bubblewrap.py +96 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sandbox/operations.py +301 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sandbox/policy.py +221 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sandbox/proxy.py +320 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sandbox/seatbelt.py +222 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sdk/__init__.py +31 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sdk/async_client.py +366 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sdk/client.py +386 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sdk/errors.py +52 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sdk/events.py +162 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sdk/types.py +63 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/sdk/wire.py +59 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/server/__init__.py +54 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/server/errors.py +52 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/server/http_server.py +419 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/server/hub.py +530 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/server/openapi.py +157 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/server/registry.py +354 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/server/router.py +275 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/server/run.py +148 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/server/schema.py +194 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/server/service.py +975 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/__init__.py +293 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/agent_session.py +2706 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/context_breakdown.py +302 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/discovery.py +212 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/entries.py +809 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/evidence.py +295 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/lock.py +176 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/manager.py +1185 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/plan.py +243 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/plan_mode.py +423 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/projection.py +262 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/rewind.py +257 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/session/status.py +240 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/settings.py +94 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/skills/__init__.py +70 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/skills/loader.py +681 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/startup/__init__.py +76 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/startup/cache.py +72 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/startup/diagnostics.py +37 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/startup/monitor.py +245 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/startup/phases.py +79 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/startup/project.py +165 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/startup/report.py +560 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/startup/scheduler.py +347 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/statusline.py +163 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/subagents/__init__.py +22 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/subagents/contract.py +75 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/subagents/definitions.py +583 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/subagents/registry.py +277 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/subagents/runtime.py +610 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/subagents/settings.py +156 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/subagents/worktree.py +63 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/__init__.py +327 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/args.py +87 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/bash.py +359 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/edit.py +298 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/edit_diff.py +344 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/find.py +158 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/grep.py +305 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/ignore.py +198 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/ls.py +127 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/mutation_queue.py +90 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/operations.py +274 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/output_accumulator.py +282 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/output_store.py +244 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/path_utils.py +91 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/plan.py +141 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/prompt_notes.py +52 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/question.py +180 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/read.py +223 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/submit_plan.py +102 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/truncate.py +423 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/tools/write.py +100 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/untrusted.py +50 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/verify/__init__.py +78 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/verify/runner.py +260 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/verify/settings.py +64 -0
- braid_coding_agent-0.1.0/src/braid_coding_agent/verify/tool.py +143 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Braid contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: braid-coding-agent
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 产品组装层:会话、编码工具、提示词与命令行入口
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Dist: braid-ai>=0.1.0,<0.2
|
|
8
|
+
Requires-Dist: braid-agent>=0.1.0,<0.2
|
|
9
|
+
Requires-Dist: braid-mcp>=0.1.0,<0.2
|
|
10
|
+
Requires-Dist: braid-tui>=0.1.0,<0.2
|
|
11
|
+
Requires-Dist: httpx>=0.28
|
|
12
|
+
Requires-Dist: keyring>=25.0
|
|
13
|
+
Requires-Dist: packaging>=24.0
|
|
14
|
+
Requires-Dist: pathspec>=0.12
|
|
15
|
+
Requires-Dist: prompt-toolkit>=3.0
|
|
16
|
+
Requires-Dist: pyyaml>=6.0
|
|
17
|
+
Requires-Dist: rich>=14.0
|
|
18
|
+
Requires-Dist: wcwidth>=0.2
|
|
19
|
+
Requires-Python: >=3.12
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "braid-coding-agent"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "产品组装层:会话、编码工具、提示词与命令行入口"
|
|
5
|
+
requires-python = ">=3.12"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
dependencies = [
|
|
9
|
+
"braid-ai>=0.1.0,<0.2",
|
|
10
|
+
"braid-agent>=0.1.0,<0.2",
|
|
11
|
+
"braid-mcp>=0.1.0,<0.2",
|
|
12
|
+
"braid-tui>=0.1.0,<0.2",
|
|
13
|
+
# HTTP 服务的 SDK 客户端:同步/异步请求与 SSE 流式读取。
|
|
14
|
+
# 服务端本身只用标准库 http.server。
|
|
15
|
+
"httpx>=0.28",
|
|
16
|
+
"keyring>=25.0",
|
|
17
|
+
# 版本比较用 packaging:PEP 440 的预发布、本地版本号与区间判定不适合自己实现。
|
|
18
|
+
"packaging>=24.0",
|
|
19
|
+
"pathspec>=0.12",
|
|
20
|
+
"prompt-toolkit>=3.0",
|
|
21
|
+
"pyyaml>=6.0",
|
|
22
|
+
# TUI 的终端内容渲染:Markdown、代码高亮、Panel、Diff 与进度条。
|
|
23
|
+
# prompt-toolkit 负责交互与布局,Rich 只产出按行结构化的 Segment。
|
|
24
|
+
"rich>=14.0",
|
|
25
|
+
"wcwidth>=0.2",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
braid = "braid_coding_agent.cli:main"
|
|
30
|
+
|
|
31
|
+
[tool.uv.sources]
|
|
32
|
+
braid-ai = { workspace = true }
|
|
33
|
+
braid-agent = { workspace = true }
|
|
34
|
+
braid-mcp = { workspace = true }
|
|
35
|
+
braid-tui = { workspace = true }
|
|
36
|
+
|
|
37
|
+
[build-system]
|
|
38
|
+
requires = ["uv_build>=0.11,<0.12"]
|
|
39
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""braid_coding_agent:产品组装层。
|
|
2
|
+
|
|
3
|
+
把模型边界(braid_ai)与 Agent 内核(braid_agent)组装成会话、编码工具与 CLI。
|
|
4
|
+
只允许依赖前两者,不允许被它们反向依赖。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import Final
|
|
10
|
+
|
|
11
|
+
__version__: Final[str] = "0.1.0"
|
|
12
|
+
|
|
13
|
+
__all__ = ["__version__"]
|
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
"""会话装配:CLI 与 HTTP 服务共用的唯一入口。
|
|
2
|
+
|
|
3
|
+
一次进程的所有装配选择(模型目录、工具白名单、权限、沙箱、检查点、提示词、扩展、
|
|
4
|
+
MCP、子代理)都收敛到 :class:`AssemblyOptions`,由 :class:`SessionAssembler` 变成
|
|
5
|
+
一个个 ``AgentSession``。CLI 的四种通道与 ``braid serve`` 都走这一条路径:任何一处
|
|
6
|
+
自己再装一遍工具或提示词,两边行为就会慢慢分叉,而这种分叉只会在用户换入口之后
|
|
7
|
+
才暴露出来。
|
|
8
|
+
|
|
9
|
+
## 为什么装配是一个对象而不是一个函数
|
|
10
|
+
|
|
11
|
+
需要被复用的不只是"造第一个会话",还有三件后续动作:
|
|
12
|
+
|
|
13
|
+
- REPL 的 ``/clear``、RPC 的 ``new_session`` 换一棵会话树但沿用同一套装配;
|
|
14
|
+
- 子代理派生:同一套工具与权限,换一个会话文件与工作目录;
|
|
15
|
+
- HTTP 服务的每个会话:同一个进程里同时活着多个 ``AgentSession``。
|
|
16
|
+
|
|
17
|
+
``SessionAssembler`` 因此持有装配参数,只把"用哪棵树"留给调用方。
|
|
18
|
+
|
|
19
|
+
## 批准与提问通道为什么要单独一层
|
|
20
|
+
|
|
21
|
+
权限引擎与 ``question`` 工具在构造时就拿到回调,而回调的实现方(REPL、RPC 服务端、
|
|
22
|
+
HTTP 服务端)总是晚于装配出现。:class:`Channels` 是这个环的断点:装配时传进去的
|
|
23
|
+
是"分派器",实现方在构造完成之后绑定真实现。没有绑定实现时按失败关闭处理——
|
|
24
|
+
需要批准的工具被拒绝,提问按取消收尾。
|
|
25
|
+
|
|
26
|
+
## 子会话
|
|
27
|
+
|
|
28
|
+
``build(..., spec=...)`` 装一个子代理会话:工具集由定义的白名单决定,权限模式不
|
|
29
|
+
高于父会话,不装扩展、不装检查点、不压缩。这些差异全部写在这里,子代理运行时只
|
|
30
|
+
负责会话文件、工作树与后台任务。
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
from __future__ import annotations
|
|
34
|
+
|
|
35
|
+
from collections.abc import Sequence
|
|
36
|
+
from dataclasses import dataclass, field, replace
|
|
37
|
+
from pathlib import Path
|
|
38
|
+
from typing import TYPE_CHECKING
|
|
39
|
+
|
|
40
|
+
from braid_agent import Agent
|
|
41
|
+
from braid_agent.types import DEFAULT_MAX_TURNS
|
|
42
|
+
from braid_ai import Models, RetryPolicy, SimpleStreamOptions
|
|
43
|
+
from braid_ai.cancel import CancelToken
|
|
44
|
+
from braid_ai.types import Model, ModelThinkingLevel
|
|
45
|
+
from braid_ai.utils.clock import Clock
|
|
46
|
+
from braid_coding_agent.checkpoint import CheckpointStore
|
|
47
|
+
from braid_coding_agent.extensions import ExtensionRunner
|
|
48
|
+
from braid_coding_agent.formatting import create_format_tool
|
|
49
|
+
from braid_coding_agent.lsp import LspManager, create_lsp_tool
|
|
50
|
+
from braid_coding_agent.messages import convert_to_llm
|
|
51
|
+
from braid_coding_agent.observability.collector import ObservabilityCollector
|
|
52
|
+
from braid_coding_agent.observability.exporter import SpanExporter
|
|
53
|
+
from braid_coding_agent.observability.redaction import Redactor
|
|
54
|
+
from braid_coding_agent.observability.settings import ObservabilitySettings
|
|
55
|
+
from braid_coding_agent.permissions.engine import (
|
|
56
|
+
ApprovalAnswer,
|
|
57
|
+
ApprovalHandler,
|
|
58
|
+
PermissionEngine,
|
|
59
|
+
PermissionMode,
|
|
60
|
+
PermissionPrompt,
|
|
61
|
+
PermissionSettings,
|
|
62
|
+
)
|
|
63
|
+
from braid_coding_agent.permissions.store import PermissionStore
|
|
64
|
+
from braid_coding_agent.prompt import ProjectContext
|
|
65
|
+
from braid_coding_agent.prompt.context_files import ContextFile
|
|
66
|
+
from braid_coding_agent.questions import QuestionAnswer, QuestionHandler, QuestionRequest
|
|
67
|
+
from braid_coding_agent.session import AgentSession, PlanStore, PromptBuilder, SessionManager
|
|
68
|
+
from braid_coding_agent.session.plan_mode import PlanMode
|
|
69
|
+
from braid_coding_agent.skills.loader import SkillsLoader, create_skill_tool
|
|
70
|
+
from braid_coding_agent.subagents.definitions import DefinitionDiagnostic
|
|
71
|
+
from braid_coding_agent.subagents.runtime import SpawnRequest, SubagentRuntime
|
|
72
|
+
from braid_coding_agent.subagents.settings import (
|
|
73
|
+
SubagentSpec,
|
|
74
|
+
clamp_permission_mode,
|
|
75
|
+
resolve_tools,
|
|
76
|
+
)
|
|
77
|
+
from braid_coding_agent.tools import (
|
|
78
|
+
PROMPT_GUIDELINES,
|
|
79
|
+
PROMPT_SNIPPETS,
|
|
80
|
+
ToolOutputStore,
|
|
81
|
+
create_encoding_tools,
|
|
82
|
+
create_network_tools,
|
|
83
|
+
create_question_tool,
|
|
84
|
+
create_update_plan_tool,
|
|
85
|
+
)
|
|
86
|
+
from braid_coding_agent.tools.operations import LocalShellOperations, ShellOperations
|
|
87
|
+
from braid_coding_agent.tools.submit_plan import create_submit_plan_tool
|
|
88
|
+
from braid_coding_agent.verify import VerifyRunner, create_verify_tool
|
|
89
|
+
|
|
90
|
+
if TYPE_CHECKING:
|
|
91
|
+
# 配置层在导入期会反向拉进会话包(config → compaction.prepare),运行期再导入
|
|
92
|
+
# 会撞上"模块只执行了一半"。注解延迟求值,这里只借类型与沙箱实现。
|
|
93
|
+
from braid_coding_agent.config import AgentConfig
|
|
94
|
+
from braid_coding_agent.credentials import Credentials
|
|
95
|
+
from braid_coding_agent.sandbox.operations import SandboxOperations
|
|
96
|
+
|
|
97
|
+
__all__ = ["AssemblyOptions", "Channels", "SessionAssembler"]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class Channels:
|
|
101
|
+
"""批准与提问的分派点。
|
|
102
|
+
|
|
103
|
+
装配出来的每个会话都只认这个对象;实现方(终端、RPC 服务端、HTTP 服务端)在
|
|
104
|
+
自己构造完成之后给 :attr:`approval` / :attr:`question` 赋值。没赋值时按失败
|
|
105
|
+
关闭处理:``print`` 与 ``json`` 两种非交互入口就是这种状态。
|
|
106
|
+
"""
|
|
107
|
+
|
|
108
|
+
def __init__(
|
|
109
|
+
self,
|
|
110
|
+
*,
|
|
111
|
+
approval: ApprovalHandler | None = None,
|
|
112
|
+
question: QuestionHandler | None = None,
|
|
113
|
+
) -> None:
|
|
114
|
+
self.approval = approval
|
|
115
|
+
self.question = question
|
|
116
|
+
|
|
117
|
+
async def request_approval(
|
|
118
|
+
self, prompt: PermissionPrompt, signal: CancelToken | None
|
|
119
|
+
) -> ApprovalAnswer:
|
|
120
|
+
"""转给已绑定的批准实现。没有实现时拒绝,理由写进工具结果。"""
|
|
121
|
+
handler = self.approval
|
|
122
|
+
if handler is None:
|
|
123
|
+
return ApprovalAnswer(scope="deny", note="当前运行模式没有批准通道")
|
|
124
|
+
return await handler(prompt, signal)
|
|
125
|
+
|
|
126
|
+
async def request_question(
|
|
127
|
+
self, request: QuestionRequest, signal: CancelToken | None
|
|
128
|
+
) -> QuestionAnswer:
|
|
129
|
+
"""转给已绑定的提问实现。没有实现时按取消收尾。"""
|
|
130
|
+
handler = self.question
|
|
131
|
+
if handler is None:
|
|
132
|
+
return QuestionAnswer(cancelled=True)
|
|
133
|
+
return await handler(request, signal)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@dataclass(slots=True)
|
|
137
|
+
class AssemblyOptions:
|
|
138
|
+
"""一次进程的全部装配参数。构造一次,之后所有会话共用。"""
|
|
139
|
+
|
|
140
|
+
cwd: str
|
|
141
|
+
agent_dir: str
|
|
142
|
+
config: AgentConfig
|
|
143
|
+
models: Models
|
|
144
|
+
model: Model
|
|
145
|
+
thinking: ModelThinkingLevel
|
|
146
|
+
clock: Clock
|
|
147
|
+
retry_policy: RetryPolicy
|
|
148
|
+
permission: PermissionSettings
|
|
149
|
+
sandbox: SandboxOperations | None
|
|
150
|
+
checkpointing: bool
|
|
151
|
+
project_context: ProjectContext | None
|
|
152
|
+
context_files: Sequence[ContextFile]
|
|
153
|
+
skills: SkillsLoader
|
|
154
|
+
extensions: ExtensionRunner | None
|
|
155
|
+
lsp: LspManager
|
|
156
|
+
tools: tuple[str, ...]
|
|
157
|
+
keep_tool_output: bool
|
|
158
|
+
max_tokens: int | None
|
|
159
|
+
max_turns: int | None
|
|
160
|
+
append_prompt: str
|
|
161
|
+
credentials: Credentials
|
|
162
|
+
subagents: Sequence[SubagentSpec]
|
|
163
|
+
#: 定义文件里允许出现的工具名(内建 + 本次运行接上的 MCP 工具)。
|
|
164
|
+
known_tools: tuple[str, ...]
|
|
165
|
+
channels: Channels
|
|
166
|
+
#: 进程级可观测性设置。每个会话在 build() 里用它造一份独立收集器。
|
|
167
|
+
observability: ObservabilitySettings = field(default_factory=ObservabilitySettings)
|
|
168
|
+
span_exporter: SpanExporter | None = None
|
|
169
|
+
secret_values: tuple[str, ...] = ()
|
|
170
|
+
#: 定义扫描阶段被忽略或被跳过的内容,用来在启动提示与 ``/agents`` 里展示。
|
|
171
|
+
definition_diagnostics: tuple[DefinitionDiagnostic, ...] = ()
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class SessionAssembler:
|
|
175
|
+
"""按 :class:`AssemblyOptions` 造会话。
|
|
176
|
+
|
|
177
|
+
子代理运行时在构造时一并建立:它的 ``spawn`` 指回 :meth:`build`,与主会话共享
|
|
178
|
+
同一套工具、权限与时钟。把它放在外面手工创建,容易出现"运行时晚于第一次装配"
|
|
179
|
+
的初始化顺序问题。
|
|
180
|
+
"""
|
|
181
|
+
|
|
182
|
+
def __init__(self, options: AssemblyOptions) -> None:
|
|
183
|
+
self.options = options
|
|
184
|
+
self.subagents = SubagentRuntime(
|
|
185
|
+
settings=options.config.subagents,
|
|
186
|
+
specs=options.subagents,
|
|
187
|
+
spawn=self._spawn_child,
|
|
188
|
+
session_root=f"{options.agent_dir}/subagents",
|
|
189
|
+
worktree_root=f"{options.agent_dir}/worktrees",
|
|
190
|
+
parent_cwd=options.cwd,
|
|
191
|
+
agent_dir=options.agent_dir,
|
|
192
|
+
known_tools=options.known_tools,
|
|
193
|
+
definition_diagnostics=options.definition_diagnostics,
|
|
194
|
+
clock=options.clock,
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
# ------------------------------------------------------------------ 会话
|
|
198
|
+
|
|
199
|
+
def build(
|
|
200
|
+
self,
|
|
201
|
+
target: SessionManager,
|
|
202
|
+
*,
|
|
203
|
+
compaction: bool = False,
|
|
204
|
+
spec: SubagentSpec | None = None,
|
|
205
|
+
child_cwd: str | None = None,
|
|
206
|
+
depth: int = 0,
|
|
207
|
+
channels: Channels | None = None,
|
|
208
|
+
parent_mode: PermissionMode | None = None,
|
|
209
|
+
) -> AgentSession:
|
|
210
|
+
"""按装配参数为新会话造一个 Agent。
|
|
211
|
+
|
|
212
|
+
:param compaction: 是否允许自动压缩。长驻入口(REPL、RPC、HTTP)传真,
|
|
213
|
+
一次性单发不启用。
|
|
214
|
+
:param spec: 子代理定义。给出时按定义装一个独立上下文的会话。
|
|
215
|
+
:param child_cwd: 子会话的工作目录;``None`` 表示与主会话相同。
|
|
216
|
+
:param depth: 子代理嵌套深度,到达上限的会话不再拿到 ``task`` 工具。
|
|
217
|
+
:param channels: 批准与提问通道。服务端为每个会话绑一个,默认用进程级那个。
|
|
218
|
+
:param parent_mode: 派生这个会话的父会话**当前**的权限模式。子代理按它与
|
|
219
|
+
定义里的模式取更严的一个——父会话在运行时进了规划模式或改成只读时,
|
|
220
|
+
子代理不能还按启动配置的宽松模式跑。
|
|
221
|
+
"""
|
|
222
|
+
options = self.options
|
|
223
|
+
session_cwd = child_cwd if child_cwd is not None else options.cwd
|
|
224
|
+
owner = self.subagents.new_owner(depth=depth)
|
|
225
|
+
allowed = options.tools if spec is None else resolve_tools(spec, options.tools)
|
|
226
|
+
bound = channels if channels is not None else options.channels
|
|
227
|
+
|
|
228
|
+
store = ToolOutputStore.for_session(target, keep=options.keep_tool_output)
|
|
229
|
+
tools = [
|
|
230
|
+
tool
|
|
231
|
+
for tool in create_encoding_tools(
|
|
232
|
+
session_cwd, output_store=store, sandbox=options.sandbox
|
|
233
|
+
)
|
|
234
|
+
if tool.name in allowed
|
|
235
|
+
]
|
|
236
|
+
if options.config.web.enabled:
|
|
237
|
+
# 网络工具不碰工作区,父子会话用同一份配置与同一套域名规则。
|
|
238
|
+
tools.extend(
|
|
239
|
+
tool for tool in create_network_tools(options.config.web) if tool.name in allowed
|
|
240
|
+
)
|
|
241
|
+
# 计划与提问工具只属于主会话:子代理的工具集由定义里的白名单决定。
|
|
242
|
+
plan_store = (
|
|
243
|
+
PlanStore(session=target) if spec is None and "update_plan" in allowed else None
|
|
244
|
+
)
|
|
245
|
+
if plan_store is not None:
|
|
246
|
+
tools.append(create_update_plan_tool(plan_store))
|
|
247
|
+
if spec is None and "question" in allowed:
|
|
248
|
+
tools.append(create_question_tool(bound.request_question))
|
|
249
|
+
|
|
250
|
+
# 子代理的权限模式不能比派生它的会话更宽,否则只读主会话能借子代理写文件。
|
|
251
|
+
# 比较的基准是父会话**当前**的模式(``parent_mode``),不是启动配置里的那份:
|
|
252
|
+
# ``/plan`` 与 ``/permission mode`` 都能在运行中改模式,只看配置会让子代理绕过它。
|
|
253
|
+
base_mode = parent_mode if parent_mode is not None else options.permission.mode
|
|
254
|
+
child_mode = (
|
|
255
|
+
clamp_permission_mode(base_mode, spec.permission_mode)
|
|
256
|
+
if spec is not None
|
|
257
|
+
else options.permission.mode
|
|
258
|
+
)
|
|
259
|
+
permission_store = (
|
|
260
|
+
PermissionStore(agent_dir=Path(options.agent_dir), cwd=session_cwd)
|
|
261
|
+
if spec is None
|
|
262
|
+
else None
|
|
263
|
+
)
|
|
264
|
+
permission_engine = PermissionEngine(
|
|
265
|
+
settings=replace(options.permission, mode=child_mode),
|
|
266
|
+
cwd=session_cwd,
|
|
267
|
+
# 子代理没有交互批准通道:ask 一律按拒绝处理(失败关闭)。
|
|
268
|
+
approval=bound.request_approval if spec is None else None,
|
|
269
|
+
# 子代理不能写项目规则:它的规则集合是父会话的只读副本。
|
|
270
|
+
store=permission_store,
|
|
271
|
+
session_id=target.get_session_id(),
|
|
272
|
+
)
|
|
273
|
+
# 规划模式只属于主会话:子代理是被派出去做一件具体事的一次性会话,没有"等用户
|
|
274
|
+
# 批准计划"的对象;它的只读保证由父会话的模式钳制给出,不需要再进规划模式。
|
|
275
|
+
plan_mode = PlanMode(session=target, engine=permission_engine) if spec is None else None
|
|
276
|
+
if plan_mode is not None:
|
|
277
|
+
plan_mode.attach_tool(create_submit_plan_tool(plan_mode))
|
|
278
|
+
# 技能工具只属于主会话:清单在提示词里,正文由模型按名字按需加载。
|
|
279
|
+
if spec is None and "skill" in allowed and options.skills.documents:
|
|
280
|
+
tools.append(create_skill_tool(options.skills, permission_engine, cwd=session_cwd))
|
|
281
|
+
# 工程能力三件套:语言服务器、验证与格式化。三者都只在配置齐备时出现——
|
|
282
|
+
# 一个没有服务器可用的 ``lsp`` 工具只会让模型反复尝试同一件做不成的事。
|
|
283
|
+
shell: ShellOperations = (
|
|
284
|
+
options.sandbox if options.sandbox is not None else LocalShellOperations()
|
|
285
|
+
)
|
|
286
|
+
if "lsp" in allowed and options.config.lsp.has_servers:
|
|
287
|
+
tools.append(create_lsp_tool(options.lsp, cwd=session_cwd))
|
|
288
|
+
if "format" in allowed and options.config.format.has_commands:
|
|
289
|
+
tools.append(create_format_tool(options.config.format, cwd=session_cwd, shell=shell))
|
|
290
|
+
verify_runner = (
|
|
291
|
+
VerifyRunner(options.config.verify, cwd=session_cwd, shell=shell)
|
|
292
|
+
if "verify" in allowed and options.config.verify.has_commands
|
|
293
|
+
else None
|
|
294
|
+
)
|
|
295
|
+
if verify_runner is not None:
|
|
296
|
+
tools.append(create_verify_tool(verify_runner))
|
|
297
|
+
# 主会话遵守工具白名单;子会话由嵌套深度决定是否还能派生。
|
|
298
|
+
# 到达上限的会话连工具声明都没有,模型不会白试一次。
|
|
299
|
+
if depth < options.config.subagents.max_depth and "task" in allowed:
|
|
300
|
+
tools.append(self.subagents.make_task_tool(owner))
|
|
301
|
+
|
|
302
|
+
# 子代理不装检查点:它的文件改动由独立会话与工作树隔离,撤销是父会话
|
|
303
|
+
# 的能力,diff 是后台任务的交付物;再开一个影子仓库没有收益。
|
|
304
|
+
checkpoints = (
|
|
305
|
+
CheckpointStore(
|
|
306
|
+
cwd=session_cwd,
|
|
307
|
+
session_dir=target.get_session_dir(),
|
|
308
|
+
session_id=target.get_session_id(),
|
|
309
|
+
clock=options.clock,
|
|
310
|
+
settings=options.config.checkpoint,
|
|
311
|
+
)
|
|
312
|
+
if spec is None and options.checkpointing and target.is_persisted()
|
|
313
|
+
else None
|
|
314
|
+
)
|
|
315
|
+
# 提示词只列真的创建出来的工具。白名单里的名字决定了顺序,
|
|
316
|
+
# 但"配置了 lsp 却没有服务器"这类情况不该在提示词里留下一个空工具。
|
|
317
|
+
created = [name for name in allowed if any(tool.name == name for tool in tools)]
|
|
318
|
+
custom_prompt = spec.system_prompt if spec is not None else None
|
|
319
|
+
if spec is not None and spec.skills:
|
|
320
|
+
custom_prompt = self._with_preloaded_skills(
|
|
321
|
+
custom_prompt or "", spec=spec, options=options
|
|
322
|
+
)
|
|
323
|
+
builder = PromptBuilder(
|
|
324
|
+
cwd=session_cwd,
|
|
325
|
+
tools=tools,
|
|
326
|
+
tool_snippets={
|
|
327
|
+
name: PROMPT_SNIPPETS[name] for name in created if name in PROMPT_SNIPPETS
|
|
328
|
+
},
|
|
329
|
+
tool_guidelines={
|
|
330
|
+
name: PROMPT_GUIDELINES[name] for name in created if name in PROMPT_GUIDELINES
|
|
331
|
+
},
|
|
332
|
+
context_files=options.context_files,
|
|
333
|
+
skills=options.skills.prompt_skills() if spec is None else (),
|
|
334
|
+
custom_prompt=custom_prompt,
|
|
335
|
+
append_system_prompt=options.append_prompt,
|
|
336
|
+
clock=options.clock,
|
|
337
|
+
)
|
|
338
|
+
child_model = self._model_for_spec(spec)
|
|
339
|
+
agent = Agent(
|
|
340
|
+
stream_fn=options.models.stream,
|
|
341
|
+
model=child_model,
|
|
342
|
+
thinking_level=options.thinking,
|
|
343
|
+
clock=options.clock,
|
|
344
|
+
convert_to_llm=convert_to_llm,
|
|
345
|
+
# None 表示不设上限:Provider 用模型目录的 max_tokens 填请求体。
|
|
346
|
+
stream_options=SimpleStreamOptions(max_tokens=options.max_tokens),
|
|
347
|
+
max_turns=(
|
|
348
|
+
spec.max_turns
|
|
349
|
+
if spec is not None
|
|
350
|
+
else (options.max_turns if options.max_turns is not None else DEFAULT_MAX_TURNS)
|
|
351
|
+
),
|
|
352
|
+
)
|
|
353
|
+
# 观测:进程级配置 + 会话级收集器。子代理也有自己的 trace,但共享导出器。
|
|
354
|
+
exporter = options.span_exporter
|
|
355
|
+
observability = (
|
|
356
|
+
ObservabilityCollector(
|
|
357
|
+
settings=options.observability,
|
|
358
|
+
clock=options.clock,
|
|
359
|
+
redactor=Redactor(options.secret_values),
|
|
360
|
+
exporter=exporter,
|
|
361
|
+
session=target,
|
|
362
|
+
)
|
|
363
|
+
if options.observability.enabled
|
|
364
|
+
else None
|
|
365
|
+
)
|
|
366
|
+
session = AgentSession(
|
|
367
|
+
agent=agent,
|
|
368
|
+
session=target,
|
|
369
|
+
models=options.models,
|
|
370
|
+
prompt_builder=builder,
|
|
371
|
+
clock=options.clock,
|
|
372
|
+
# 子代理是有界的一次性任务,靠 max_turns 与 timeout 收口,不压缩。
|
|
373
|
+
# 子代理同样可以压缩:它有自己的上下文窗口与用量账单,长任务撞到窗口时
|
|
374
|
+
# 应当按同一套阈值处理,而不是靠 maxTurns 或超时被动中断。
|
|
375
|
+
compaction_settings=(
|
|
376
|
+
options.config.compaction if compaction or spec is not None else None
|
|
377
|
+
),
|
|
378
|
+
# 扩展与 MCP 工具不继承:子代理的工具白名单是唯一的边界。
|
|
379
|
+
extensions=options.extensions if spec is None else None,
|
|
380
|
+
tool_outputs=store,
|
|
381
|
+
retry_policy=options.retry_policy,
|
|
382
|
+
config=options.config,
|
|
383
|
+
credentials=options.credentials,
|
|
384
|
+
project_context=options.project_context if spec is None else None,
|
|
385
|
+
skills_loader=options.skills if spec is None else None,
|
|
386
|
+
permission_engine=permission_engine,
|
|
387
|
+
plan_mode=plan_mode,
|
|
388
|
+
sandbox=options.sandbox,
|
|
389
|
+
checkpoints=checkpoints,
|
|
390
|
+
plan=plan_store,
|
|
391
|
+
verify=None if verify_runner is None else verify_runner.budget,
|
|
392
|
+
observability=observability,
|
|
393
|
+
)
|
|
394
|
+
owner.session = session
|
|
395
|
+
self.subagents.bind(owner)
|
|
396
|
+
return session
|
|
397
|
+
|
|
398
|
+
def new_session(self, current: AgentSession, parent: str | None = None) -> AgentSession:
|
|
399
|
+
"""换一棵空树,沿用当前会话的 cwd 与落盘策略。
|
|
400
|
+
|
|
401
|
+
REPL 的 ``/clear``、RPC 的 ``new_session`` 与 HTTP 的会话创建都走这里,
|
|
402
|
+
差别只在是否把父会话写进头。
|
|
403
|
+
"""
|
|
404
|
+
manager = current.session
|
|
405
|
+
clock = self.options.clock
|
|
406
|
+
if manager.is_persisted():
|
|
407
|
+
target = SessionManager.create(
|
|
408
|
+
manager.get_cwd(),
|
|
409
|
+
manager.get_session_dir(),
|
|
410
|
+
parent_session=parent,
|
|
411
|
+
clock=clock,
|
|
412
|
+
)
|
|
413
|
+
else:
|
|
414
|
+
target = SessionManager.in_memory(manager.get_cwd(), clock=clock)
|
|
415
|
+
return self.build(target, compaction=True)
|
|
416
|
+
|
|
417
|
+
# ---------------------------------------------------------------- 子代理
|
|
418
|
+
|
|
419
|
+
def _spawn_child(self, request: SpawnRequest) -> AgentSession:
|
|
420
|
+
"""子代理运行时的装配入口:一份独立会话文件 + 一次同样的 build。"""
|
|
421
|
+
manager = (
|
|
422
|
+
request.fork
|
|
423
|
+
if request.fork is not None
|
|
424
|
+
else SessionManager.create(
|
|
425
|
+
request.cwd,
|
|
426
|
+
request.session_dir,
|
|
427
|
+
parent_session=request.parent_session_file,
|
|
428
|
+
clock=self.options.clock,
|
|
429
|
+
)
|
|
430
|
+
)
|
|
431
|
+
return self.build(
|
|
432
|
+
manager,
|
|
433
|
+
spec=request.spec,
|
|
434
|
+
child_cwd=request.cwd,
|
|
435
|
+
depth=request.depth,
|
|
436
|
+
parent_mode=request.permission_mode,
|
|
437
|
+
)
|
|
438
|
+
|
|
439
|
+
@staticmethod
|
|
440
|
+
def _with_preloaded_skills(prompt: str, *, spec: SubagentSpec, options: AssemblyOptions) -> str:
|
|
441
|
+
"""把定义里 ``skills`` 指定的正文装进系统提示词。找不到名字就地失败。"""
|
|
442
|
+
blocks: list[str] = []
|
|
443
|
+
for name in spec.skills:
|
|
444
|
+
document = options.skills.find(name)
|
|
445
|
+
if document is None:
|
|
446
|
+
raise ValueError(
|
|
447
|
+
f"子代理 {spec.name} 预加载的技能 {name!r} 不存在(用 /skills 查看可用技能)"
|
|
448
|
+
)
|
|
449
|
+
blocks.append(f'<skill name="{document.name}">\n{document.body}\n</skill>')
|
|
450
|
+
return prompt + "\n\n" + "\n\n".join(blocks)
|
|
451
|
+
|
|
452
|
+
def _model_for_spec(self, spec: SubagentSpec | None) -> Model:
|
|
453
|
+
"""子代理定义的模型;没定义时用主会话的模型。"""
|
|
454
|
+
if spec is None or spec.model is None:
|
|
455
|
+
return self.options.model
|
|
456
|
+
provider_id, _, model_id = spec.model.partition("/")
|
|
457
|
+
found = self.options.models.get_model(provider_id, model_id)
|
|
458
|
+
if found is None:
|
|
459
|
+
raise ValueError(f"子代理 {spec.name} 指定的模型 {spec.model} 不在模型目录里")
|
|
460
|
+
return found
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""取消在工具侧的出口:一个异常加一个检查函数。
|
|
2
|
+
|
|
3
|
+
放在独立模块里,是为了让**不依赖文件系统工具**的模块(例如网络工具)也能用同一
|
|
4
|
+
个取消语义,而不必导入 ``tools`` 包——那个包会反过来导入它们,形成循环。
|
|
5
|
+
|
|
6
|
+
``OperationAborted`` 是工具取消的唯一出口。工具自己吞掉取消会让循环以为「做完了」,
|
|
7
|
+
于是带着一份不完整的结果继续下一回合。
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from braid_ai.cancel import CancelToken
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class OperationAborted(RuntimeError):
|
|
16
|
+
"""操作因取消而终止。"""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def ensure_not_cancelled(signal: CancelToken | None) -> None:
|
|
20
|
+
"""在 I/O 前后检查取消标志。工具不必在每个 ``await`` 上都检查,但每个阶段之间必须。"""
|
|
21
|
+
if signal is not None and signal.cancelled:
|
|
22
|
+
raise OperationAborted("Operation aborted")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
__all__ = ["OperationAborted", "ensure_not_cancelled"]
|