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.
Files changed (192) hide show
  1. braid_coding_agent/__init__.py +13 -0
  2. braid_coding_agent/assembly.py +460 -0
  3. braid_coding_agent/cancellation.py +25 -0
  4. braid_coding_agent/checkpoint/__init__.py +106 -0
  5. braid_coding_agent/checkpoint/git.py +305 -0
  6. braid_coding_agent/checkpoint/settings.py +26 -0
  7. braid_coding_agent/checkpoint/store.py +1261 -0
  8. braid_coding_agent/checkpoint/workspace.py +220 -0
  9. braid_coding_agent/cli.py +2168 -0
  10. braid_coding_agent/commands/__init__.py +50 -0
  11. braid_coding_agent/commands/auth.py +161 -0
  12. braid_coding_agent/commands/config.py +180 -0
  13. braid_coding_agent/commands/doctor.py +511 -0
  14. braid_coding_agent/commands/mcp.py +928 -0
  15. braid_coding_agent/commands/models.py +95 -0
  16. braid_coding_agent/commands/setup.py +89 -0
  17. braid_coding_agent/commands/uninstall.py +277 -0
  18. braid_coding_agent/commands/upgrade.py +381 -0
  19. braid_coding_agent/compaction/__init__.py +174 -0
  20. braid_coding_agent/compaction/prepare.py +430 -0
  21. braid_coding_agent/compaction/summarize.py +542 -0
  22. braid_coding_agent/compaction/tokens.py +204 -0
  23. braid_coding_agent/compaction/tool_results.py +196 -0
  24. braid_coding_agent/compaction/utils.py +181 -0
  25. braid_coding_agent/config.py +2776 -0
  26. braid_coding_agent/config_files.py +191 -0
  27. braid_coding_agent/credentials.py +252 -0
  28. braid_coding_agent/discovery.py +415 -0
  29. braid_coding_agent/extensions/__init__.py +77 -0
  30. braid_coding_agent/extensions/api.py +480 -0
  31. braid_coding_agent/extensions/runner.py +553 -0
  32. braid_coding_agent/formatting/__init__.py +62 -0
  33. braid_coding_agent/formatting/settings.py +87 -0
  34. braid_coding_agent/formatting/tool.py +179 -0
  35. braid_coding_agent/lsp/__init__.py +105 -0
  36. braid_coding_agent/lsp/client.py +546 -0
  37. braid_coding_agent/lsp/manager.py +187 -0
  38. braid_coding_agent/lsp/model.py +316 -0
  39. braid_coding_agent/lsp/protocol.py +105 -0
  40. braid_coding_agent/lsp/settings.py +120 -0
  41. braid_coding_agent/lsp/tool.py +429 -0
  42. braid_coding_agent/mcp/__init__.py +101 -0
  43. braid_coding_agent/mcp/cache.py +230 -0
  44. braid_coding_agent/mcp/catalog.py +108 -0
  45. braid_coding_agent/mcp/config.py +664 -0
  46. braid_coding_agent/mcp/config_store.py +142 -0
  47. braid_coding_agent/mcp/inspector.py +85 -0
  48. braid_coding_agent/mcp/panel.py +254 -0
  49. braid_coding_agent/mcp/runtime.py +1060 -0
  50. braid_coding_agent/mcp/tools.py +579 -0
  51. braid_coding_agent/mcp/trust.py +150 -0
  52. braid_coding_agent/mcp/tui_panel.py +444 -0
  53. braid_coding_agent/messages.py +152 -0
  54. braid_coding_agent/models_view.py +105 -0
  55. braid_coding_agent/modes/__init__.py +64 -0
  56. braid_coding_agent/modes/json_mode.py +480 -0
  57. braid_coding_agent/modes/lifecycle.py +217 -0
  58. braid_coding_agent/modes/print_mode.py +257 -0
  59. braid_coding_agent/modes/repl.py +2687 -0
  60. braid_coding_agent/modes/repl_commands.py +263 -0
  61. braid_coding_agent/modes/rpc.py +953 -0
  62. braid_coding_agent/modes/skill_commands.py +88 -0
  63. braid_coding_agent/modes/tui.py +2226 -0
  64. braid_coding_agent/modes/tui_control.py +90 -0
  65. braid_coding_agent/modes/tui_model.py +1043 -0
  66. braid_coding_agent/modes/tui_render.py +327 -0
  67. braid_coding_agent/modes/tui_rich.py +751 -0
  68. braid_coding_agent/modes/tui_style.py +42 -0
  69. braid_coding_agent/modes/wire.py +81 -0
  70. braid_coding_agent/netsafe.py +82 -0
  71. braid_coding_agent/network/__init__.py +20 -0
  72. braid_coding_agent/network/cache.py +77 -0
  73. braid_coding_agent/network/fetch.py +311 -0
  74. braid_coding_agent/network/html_text.py +160 -0
  75. braid_coding_agent/network/search.py +347 -0
  76. braid_coding_agent/network/settings.py +103 -0
  77. braid_coding_agent/network/urls.py +89 -0
  78. braid_coding_agent/observability/__init__.py +101 -0
  79. braid_coding_agent/observability/audit.py +124 -0
  80. braid_coding_agent/observability/collector.py +314 -0
  81. braid_coding_agent/observability/exporter.py +74 -0
  82. braid_coding_agent/observability/logging_setup.py +149 -0
  83. braid_coding_agent/observability/redaction.py +96 -0
  84. braid_coding_agent/observability/settings.py +29 -0
  85. braid_coding_agent/observability/spans.py +239 -0
  86. braid_coding_agent/permissions/__init__.py +185 -0
  87. braid_coding_agent/permissions/command.py +586 -0
  88. braid_coding_agent/permissions/engine.py +522 -0
  89. braid_coding_agent/permissions/evaluate.py +309 -0
  90. braid_coding_agent/permissions/matching.py +233 -0
  91. braid_coding_agent/permissions/model.py +278 -0
  92. braid_coding_agent/permissions/policy.py +99 -0
  93. braid_coding_agent/permissions/readonly.py +236 -0
  94. braid_coding_agent/permissions/store.py +379 -0
  95. braid_coding_agent/project_init.py +313 -0
  96. braid_coding_agent/prompt/__init__.py +98 -0
  97. braid_coding_agent/prompt/context_files.py +253 -0
  98. braid_coding_agent/prompt/project_context.py +172 -0
  99. braid_coding_agent/prompt/system_prompt.py +378 -0
  100. braid_coding_agent/questions.py +71 -0
  101. braid_coding_agent/render/__init__.py +56 -0
  102. braid_coding_agent/render/batch.py +66 -0
  103. braid_coding_agent/render/history.py +143 -0
  104. braid_coding_agent/render/stream.py +823 -0
  105. braid_coding_agent/render/theme.py +243 -0
  106. braid_coding_agent/sandbox/__init__.py +89 -0
  107. braid_coding_agent/sandbox/backend.py +184 -0
  108. braid_coding_agent/sandbox/bubblewrap.py +96 -0
  109. braid_coding_agent/sandbox/operations.py +301 -0
  110. braid_coding_agent/sandbox/policy.py +221 -0
  111. braid_coding_agent/sandbox/proxy.py +320 -0
  112. braid_coding_agent/sandbox/seatbelt.py +222 -0
  113. braid_coding_agent/sdk/__init__.py +31 -0
  114. braid_coding_agent/sdk/async_client.py +366 -0
  115. braid_coding_agent/sdk/client.py +386 -0
  116. braid_coding_agent/sdk/errors.py +52 -0
  117. braid_coding_agent/sdk/events.py +162 -0
  118. braid_coding_agent/sdk/types.py +63 -0
  119. braid_coding_agent/sdk/wire.py +59 -0
  120. braid_coding_agent/server/__init__.py +54 -0
  121. braid_coding_agent/server/errors.py +52 -0
  122. braid_coding_agent/server/http_server.py +419 -0
  123. braid_coding_agent/server/hub.py +530 -0
  124. braid_coding_agent/server/openapi.py +157 -0
  125. braid_coding_agent/server/registry.py +354 -0
  126. braid_coding_agent/server/router.py +275 -0
  127. braid_coding_agent/server/run.py +148 -0
  128. braid_coding_agent/server/schema.py +194 -0
  129. braid_coding_agent/server/service.py +975 -0
  130. braid_coding_agent/session/__init__.py +293 -0
  131. braid_coding_agent/session/agent_session.py +2706 -0
  132. braid_coding_agent/session/context_breakdown.py +302 -0
  133. braid_coding_agent/session/discovery.py +212 -0
  134. braid_coding_agent/session/entries.py +809 -0
  135. braid_coding_agent/session/evidence.py +295 -0
  136. braid_coding_agent/session/lock.py +176 -0
  137. braid_coding_agent/session/manager.py +1185 -0
  138. braid_coding_agent/session/plan.py +243 -0
  139. braid_coding_agent/session/plan_mode.py +423 -0
  140. braid_coding_agent/session/projection.py +262 -0
  141. braid_coding_agent/session/rewind.py +257 -0
  142. braid_coding_agent/session/status.py +240 -0
  143. braid_coding_agent/settings.py +94 -0
  144. braid_coding_agent/skills/__init__.py +70 -0
  145. braid_coding_agent/skills/loader.py +681 -0
  146. braid_coding_agent/startup/__init__.py +76 -0
  147. braid_coding_agent/startup/cache.py +72 -0
  148. braid_coding_agent/startup/diagnostics.py +37 -0
  149. braid_coding_agent/startup/monitor.py +245 -0
  150. braid_coding_agent/startup/phases.py +79 -0
  151. braid_coding_agent/startup/project.py +165 -0
  152. braid_coding_agent/startup/report.py +560 -0
  153. braid_coding_agent/startup/scheduler.py +347 -0
  154. braid_coding_agent/statusline.py +163 -0
  155. braid_coding_agent/subagents/__init__.py +22 -0
  156. braid_coding_agent/subagents/contract.py +75 -0
  157. braid_coding_agent/subagents/definitions.py +583 -0
  158. braid_coding_agent/subagents/registry.py +277 -0
  159. braid_coding_agent/subagents/runtime.py +610 -0
  160. braid_coding_agent/subagents/settings.py +156 -0
  161. braid_coding_agent/subagents/worktree.py +63 -0
  162. braid_coding_agent/tools/__init__.py +327 -0
  163. braid_coding_agent/tools/args.py +87 -0
  164. braid_coding_agent/tools/bash.py +359 -0
  165. braid_coding_agent/tools/edit.py +298 -0
  166. braid_coding_agent/tools/edit_diff.py +344 -0
  167. braid_coding_agent/tools/find.py +158 -0
  168. braid_coding_agent/tools/grep.py +305 -0
  169. braid_coding_agent/tools/ignore.py +198 -0
  170. braid_coding_agent/tools/ls.py +127 -0
  171. braid_coding_agent/tools/mutation_queue.py +90 -0
  172. braid_coding_agent/tools/operations.py +274 -0
  173. braid_coding_agent/tools/output_accumulator.py +282 -0
  174. braid_coding_agent/tools/output_store.py +244 -0
  175. braid_coding_agent/tools/path_utils.py +91 -0
  176. braid_coding_agent/tools/plan.py +141 -0
  177. braid_coding_agent/tools/prompt_notes.py +52 -0
  178. braid_coding_agent/tools/question.py +180 -0
  179. braid_coding_agent/tools/read.py +223 -0
  180. braid_coding_agent/tools/submit_plan.py +102 -0
  181. braid_coding_agent/tools/truncate.py +423 -0
  182. braid_coding_agent/tools/write.py +100 -0
  183. braid_coding_agent/untrusted.py +50 -0
  184. braid_coding_agent/verify/__init__.py +78 -0
  185. braid_coding_agent/verify/runner.py +260 -0
  186. braid_coding_agent/verify/settings.py +64 -0
  187. braid_coding_agent/verify/tool.py +143 -0
  188. braid_coding_agent-0.1.0.dist-info/METADATA +19 -0
  189. braid_coding_agent-0.1.0.dist-info/RECORD +192 -0
  190. braid_coding_agent-0.1.0.dist-info/WHEEL +4 -0
  191. braid_coding_agent-0.1.0.dist-info/entry_points.txt +3 -0
  192. braid_coding_agent-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,240 @@
1
+ """会话状态快照的数据结构与 wire 转换。
2
+
3
+ 取数在 ``AgentSession.context_status()`` 完成:只有会话对象同时持有模型目录、
4
+ 会话树、压缩设置、工具表与待注入队列。这里只定义只读结果与序列化形状,
5
+ 避免状态模块反过来依赖会话对象。
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from collections.abc import Sequence
11
+ from dataclasses import dataclass
12
+ from typing import TYPE_CHECKING, Final
13
+
14
+ from braid_ai.types import AssistantMessage, JsonValue, Usage
15
+ from braid_coding_agent.session.entries import (
16
+ BranchSummaryEntry,
17
+ CompactionEntry,
18
+ SessionEntry,
19
+ SessionMessageEntry,
20
+ UsageEntry,
21
+ )
22
+ from braid_coding_agent.session.plan import PlanState, plan_to_wire
23
+
24
+ if TYPE_CHECKING:
25
+ from braid_coding_agent.session.plan_mode import PlanModeState
26
+
27
+ __all__ = [
28
+ "CONTEXT_WARNING_RATIO",
29
+ "ContextStatus",
30
+ "SessionTotals",
31
+ "context_wire",
32
+ "cost_wire",
33
+ "plan_mode_wire",
34
+ "plan_wire",
35
+ "status_wire_parts",
36
+ "tools_wire",
37
+ "usage_wire",
38
+ ]
39
+
40
+ #: 距离自动压缩触发线的剩余空间低于这个比例时发一次提示事件。
41
+ CONTEXT_WARNING_RATIO: Final[float] = 0.2
42
+
43
+
44
+ @dataclass(slots=True, frozen=True)
45
+ class SessionTotals:
46
+ """整段会话的累计用量与费用(美元)。
47
+
48
+ 与 ``SessionUsage`` 的口径不同:``SessionUsage`` 只累计本进程经过
49
+ ``message_end`` 的消息,这里遍历会话树的全部条目,恢复进来的历史也计入。
50
+ """
51
+
52
+ input: int = 0
53
+ output: int = 0
54
+ cache_read: int = 0
55
+ cache_write: int = 0
56
+ total_tokens: int = 0
57
+ cost_input: float = 0.0
58
+ cost_output: float = 0.0
59
+ cost_cache_read: float = 0.0
60
+ cost_cache_write: float = 0.0
61
+ cost_total: float = 0.0
62
+
63
+ @property
64
+ def cache_hit_rate(self) -> float:
65
+ """缓存读在全部输入 token 里的占比。分母只含输入侧三类。
66
+
67
+ 口径与 :attr:`SessionUsage.cache_hit_rate` 相同:缓存命中的意义是"这段前缀
68
+ 不用按输入价再付一次",输出 token 与它无关。
69
+ """
70
+ prompt = self.input + self.cache_read + self.cache_write
71
+ return self.cache_read / prompt if prompt else 0.0
72
+
73
+ @classmethod
74
+ def from_entries(cls, entries: Sequence[SessionEntry]) -> SessionTotals:
75
+ """汇总会话树里全部计费条目。
76
+
77
+ 助手消息、压缩摘要、分支摘要与 ``usage`` 条目(缓存预热)都带用量;
78
+ 工具结果里内联的 ``usage`` 属于工具内部调用,不计入主对话。
79
+ ``total_tokens`` 为 0 时按四个分量相加补齐,与压缩模块的算法一致。
80
+ """
81
+ input_tokens = output_tokens = cache_read = cache_write = total = 0
82
+ cost_input = cost_output = cost_cache_read = cost_cache_write = cost_total = 0.0
83
+ for entry in entries:
84
+ usage = _usage_of(entry)
85
+ if usage is None:
86
+ continue
87
+ input_tokens += usage.input
88
+ output_tokens += usage.output
89
+ cache_read += usage.cache_read
90
+ cache_write += usage.cache_write
91
+ total += usage.total_tokens or (
92
+ usage.input + usage.output + usage.cache_read + usage.cache_write
93
+ )
94
+ cost_input += usage.cost.input
95
+ cost_output += usage.cost.output
96
+ cost_cache_read += usage.cost.cache_read
97
+ cost_cache_write += usage.cost.cache_write
98
+ cost_total += usage.cost.total
99
+ return cls(
100
+ input=input_tokens,
101
+ output=output_tokens,
102
+ cache_read=cache_read,
103
+ cache_write=cache_write,
104
+ total_tokens=total,
105
+ cost_input=cost_input,
106
+ cost_output=cost_output,
107
+ cost_cache_read=cost_cache_read,
108
+ cost_cache_write=cost_cache_write,
109
+ cost_total=cost_total,
110
+ )
111
+
112
+
113
+ def _usage_of(entry: SessionEntry) -> Usage | None:
114
+ """一条会话条目是否携带要计入主对话的用量。"""
115
+ if isinstance(entry, SessionMessageEntry) and isinstance(entry.message, AssistantMessage):
116
+ return entry.message.usage
117
+ if isinstance(entry, (CompactionEntry, BranchSummaryEntry, UsageEntry)):
118
+ return entry.usage
119
+ return None
120
+
121
+
122
+ @dataclass(slots=True, frozen=True)
123
+ class ContextStatus:
124
+ """一次状态查询的只读结果。
125
+
126
+ ``reserve_tokens`` / ``threshold`` / ``remaining_tokens`` 在自动压缩关闭时都是
127
+ ``None``:不显示一条不存在的触发线。``remaining_tokens`` 可以为负,表示上下文
128
+ 已经越过触发线,下一次请求前会触发压缩。
129
+ """
130
+
131
+ model: str
132
+ context_window: int
133
+ tokens: int
134
+ usage_tokens: int
135
+ trailing_tokens: int
136
+ percent: float
137
+ auto_compaction_enabled: bool
138
+ reserve_tokens: int | None
139
+ threshold: int | None
140
+ remaining_tokens: int | None
141
+ near_limit: bool
142
+ totals: SessionTotals
143
+ tool_names: tuple[str, ...]
144
+ queue_length: int
145
+ #: 当前计划。这次运行没装配 ``update_plan`` 时为 ``None``;有计划但还没提交过
146
+ #: 步骤时是空的 :class:`PlanState`(``total == 0``)。
147
+ plan: PlanState | None = None
148
+ #: 规划模式状态。没装配规划模式时为 ``None``;装配了但从未进入过时是
149
+ #: ``active=False`` 的默认状态——两种情况的区别是"这个入口支不支持规划模式"。
150
+ plan_mode: PlanModeState | None = None
151
+
152
+ @property
153
+ def tool_count(self) -> int:
154
+ """当前可执行工具的数量。"""
155
+ return len(self.tool_names)
156
+
157
+
158
+ def usage_wire(totals: SessionTotals) -> dict[str, JsonValue]:
159
+ """累计用量 → wire。字段名与 ``message_end.message.usage`` 的计数键一致。
160
+
161
+ ``cacheHitRate`` 是派生值(0–1),客户端不必自己按公式算一遍;口径见
162
+ :attr:`SessionTotals.cache_hit_rate`。
163
+ """
164
+ return {
165
+ "input": totals.input,
166
+ "output": totals.output,
167
+ "cacheRead": totals.cache_read,
168
+ "cacheWrite": totals.cache_write,
169
+ "cacheHitRate": round(totals.cache_hit_rate, 4),
170
+ "totalTokens": totals.total_tokens,
171
+ }
172
+
173
+
174
+ def cost_wire(totals: SessionTotals) -> dict[str, JsonValue]:
175
+ """累计费用 → wire。"""
176
+ return {
177
+ "input": totals.cost_input,
178
+ "output": totals.cost_output,
179
+ "cacheRead": totals.cost_cache_read,
180
+ "cacheWrite": totals.cost_cache_write,
181
+ "total": totals.cost_total,
182
+ }
183
+
184
+
185
+ def context_wire(status: ContextStatus) -> dict[str, JsonValue]:
186
+ """上下文与压缩状态 → wire。"""
187
+ return {
188
+ "model": status.model,
189
+ "contextWindow": status.context_window,
190
+ "usageTokens": status.usage_tokens,
191
+ "trailingTokens": status.trailing_tokens,
192
+ "tokens": status.tokens,
193
+ "percent": round(status.percent, 2),
194
+ "autoCompactionEnabled": status.auto_compaction_enabled,
195
+ "reserveTokens": status.reserve_tokens,
196
+ "threshold": status.threshold,
197
+ "remainingTokens": status.remaining_tokens,
198
+ "nearLimit": status.near_limit,
199
+ }
200
+
201
+
202
+ def tools_wire(status: ContextStatus) -> dict[str, JsonValue]:
203
+ """可执行工具与待注入队列 → wire。"""
204
+ return {
205
+ "count": status.tool_count,
206
+ "names": list(status.tool_names),
207
+ "queueLength": status.queue_length,
208
+ }
209
+
210
+
211
+ def plan_wire(status: ContextStatus) -> dict[str, JsonValue] | None:
212
+ """当前计划 → wire。没装配、没提交过步骤时都是 ``None``。
213
+
214
+ 客户端不必区分"这个版本没有计划功能"与"有功能但还没用":两种情况下
215
+ ``/plan`` 的界面都是"还没有计划"。
216
+ """
217
+ if status.plan is None or status.plan.is_empty:
218
+ return None
219
+ return plan_to_wire(status.plan)
220
+
221
+
222
+ def status_wire_parts(status: ContextStatus) -> dict[str, JsonValue]:
223
+ """RPC ``get_state`` 的四个新增字段。既有字段由调用方自己写。"""
224
+ return {
225
+ "usage": usage_wire(status.totals),
226
+ "context": context_wire(status),
227
+ "cost": cost_wire(status.totals),
228
+ "tools": tools_wire(status),
229
+ }
230
+
231
+
232
+ def plan_mode_wire(status: ContextStatus) -> dict[str, JsonValue] | None:
233
+ """规划模式 → wire。没装配时为 ``None``。
234
+
235
+ 装配了但从未进入过时仍然给出 ``active=false`` 的对象:客户端据此画出"可以 /plan"
236
+ 的入口,空 ``None`` 只能说明这个入口不支持规划模式。
237
+ """
238
+ if status.plan_mode is None:
239
+ return None
240
+ return status.plan_mode.to_wire()
@@ -0,0 +1,94 @@
1
+ """``settings.json`` 的公共字段与 MCP 配置。
2
+
3
+ 配置文件由 :mod:`braid_coding_agent.config` 统一读取;本模块只定义配置对象里
4
+ 跨模块共享的 MCP 设置,避免 ``config``、``cli`` 与 ``mcp`` 之间互相导入。
5
+ 服务器定义仍放在独立的 ``mcp.json``,这里只保存所有服务器共用的加载方式与预算。
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from dataclasses import dataclass
11
+ from typing import Literal
12
+
13
+ from braid_ai.types import JsonValue
14
+
15
+ #: 文件名。位置是 ``agent_dir / SETTINGS_FILE_NAME``(默认 ``~/.braid/settings.json``)。
16
+ SETTINGS_FILE_NAME = "settings.json"
17
+
18
+ _MCP_KEYS = frozenset(
19
+ {"loading", "tool_token_budget", "connect_timeout_s", "call_timeout_s", "cache"}
20
+ )
21
+
22
+
23
+ class SettingsError(ValueError):
24
+ """``settings.json`` 的字段不合法。消息带字段路径,不带密钥值。"""
25
+
26
+
27
+ @dataclass(slots=True, frozen=True)
28
+ class McpSettings:
29
+ """``settings.json`` 的 ``mcp`` 节:全局加载方式与预算。"""
30
+
31
+ #: 默认把工具延迟加载。``eager`` 只适合工具很少且每次都要用的服务器。
32
+ loading: Literal["lazy", "eager"] = "lazy"
33
+ #: 延迟加载目录在系统提示词里的近似 Token 上限。0 表示不放目录。
34
+ tool_token_budget: int = 4_000
35
+ #: 建立连接与握手的上限。
36
+ connect_timeout_s: float = 15.0
37
+ #: 单次工具调用的默认上限;服务器条目里的 ``timeout`` 优先。
38
+ call_timeout_s: float = 60.0
39
+ #: 是否把发现结果写进 ``mcp_cache.json``,供 ``braid mcp get`` 与诊断使用。
40
+ cache: bool = True
41
+
42
+
43
+ def parse_mcp_settings(
44
+ value: JsonValue | None, *, where: str, base: McpSettings | None = None
45
+ ) -> McpSettings:
46
+ """解析 ``mcp`` 节,缺省字段沿用 ``base`` 里的值。"""
47
+ if value is None:
48
+ return base if base is not None else McpSettings()
49
+ if not isinstance(value, dict):
50
+ raise SettingsError(f"{where}: 必须是 JSON 对象")
51
+ current = base if base is not None else McpSettings()
52
+ unknown = sorted(set(value) - _MCP_KEYS)
53
+ if unknown:
54
+ raise SettingsError(
55
+ f"{where}: 未知字段 {', '.join(unknown)}(可用: {', '.join(sorted(_MCP_KEYS))})"
56
+ )
57
+ loading = value.get("loading", current.loading)
58
+ if loading not in ("lazy", "eager"):
59
+ raise SettingsError(f'{where}.loading: 只能是 "lazy" 或 "eager"')
60
+ budget = value.get("tool_token_budget", current.tool_token_budget)
61
+ if isinstance(budget, bool) or not isinstance(budget, int) or budget < 0:
62
+ raise SettingsError(f"{where}.tool_token_budget: 必须是非负整数")
63
+ connect_timeout = value.get("connect_timeout_s", current.connect_timeout_s)
64
+ if (
65
+ isinstance(connect_timeout, bool)
66
+ or not isinstance(connect_timeout, int | float)
67
+ or connect_timeout <= 0
68
+ ):
69
+ raise SettingsError(f"{where}.connect_timeout_s: 必须是正数(秒)")
70
+ call_timeout = value.get("call_timeout_s", current.call_timeout_s)
71
+ if (
72
+ isinstance(call_timeout, bool)
73
+ or not isinstance(call_timeout, int | float)
74
+ or call_timeout <= 0
75
+ ):
76
+ raise SettingsError(f"{where}.call_timeout_s: 必须是正数(秒)")
77
+ cache = value.get("cache", current.cache)
78
+ if not isinstance(cache, bool):
79
+ raise SettingsError(f"{where}.cache: 必须是 true 或 false")
80
+ return McpSettings(
81
+ loading=loading,
82
+ tool_token_budget=budget,
83
+ connect_timeout_s=float(connect_timeout),
84
+ call_timeout_s=float(call_timeout),
85
+ cache=cache,
86
+ )
87
+
88
+
89
+ __all__ = [
90
+ "SETTINGS_FILE_NAME",
91
+ "McpSettings",
92
+ "SettingsError",
93
+ "parse_mcp_settings",
94
+ ]
@@ -0,0 +1,70 @@
1
+ """Skills:把重复工作流写成一份带 frontmatter 的 Markdown,模型按需读取正文。
2
+
3
+ 三层分工:
4
+
5
+ - :mod:`.loader` 负责发现(项目 / 全局 / Claude 兼容三类目录)、解析 frontmatter、
6
+ 同名冲突裁决与文件变化刷新;
7
+ - 渲染复用 :func:`braid_coding_agent.prompt.format_skills_for_prompt`:清单(名字、
8
+ 说明、路径)进系统提示词的 ``skills`` 分节,**正文不进提示词**;
9
+ - :func:`create_skill_tool` 提供按名字加载正文的可取消工具,加载时经权限引擎检查。
10
+
11
+ 这里刻意不在 ``__init__`` 里做发现:加载要读磁盘,是装配期动作,且需要 ``cwd``
12
+ 与 ``agent_dir`` 两个参数。导入包本身应当零 I/O。
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import importlib
18
+ from typing import TYPE_CHECKING
19
+
20
+ if TYPE_CHECKING:
21
+ from braid_coding_agent.skills.loader import (
22
+ CLAUDE_SKILLS_DIRNAME,
23
+ PROJECT_SKILLS_DIRNAME,
24
+ USER_SKILLS_DIRNAME,
25
+ SkillAccess,
26
+ SkillDiagnostic,
27
+ SkillDocument,
28
+ SkillsLoader,
29
+ check_skill_access,
30
+ create_skill_tool,
31
+ parse_skill_document,
32
+ )
33
+
34
+ _LAZY_EXPORTS: dict[str, str] = {
35
+ "CLAUDE_SKILLS_DIRNAME": "braid_coding_agent.skills.loader",
36
+ "PROJECT_SKILLS_DIRNAME": "braid_coding_agent.skills.loader",
37
+ "USER_SKILLS_DIRNAME": "braid_coding_agent.skills.loader",
38
+ "SkillAccess": "braid_coding_agent.skills.loader",
39
+ "SkillDiagnostic": "braid_coding_agent.skills.loader",
40
+ "SkillDocument": "braid_coding_agent.skills.loader",
41
+ "SkillsLoader": "braid_coding_agent.skills.loader",
42
+ "check_skill_access": "braid_coding_agent.skills.loader",
43
+ "create_skill_tool": "braid_coding_agent.skills.loader",
44
+ "parse_skill_document": "braid_coding_agent.skills.loader",
45
+ }
46
+
47
+
48
+ def __getattr__(name: str) -> object:
49
+ """按需导入导出名。"""
50
+ module_name = _LAZY_EXPORTS.get(name)
51
+ if module_name is None:
52
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
53
+ module = importlib.import_module(module_name)
54
+ value = getattr(module, name)
55
+ globals()[name] = value
56
+ return value
57
+
58
+
59
+ __all__ = [
60
+ "CLAUDE_SKILLS_DIRNAME",
61
+ "PROJECT_SKILLS_DIRNAME",
62
+ "USER_SKILLS_DIRNAME",
63
+ "SkillAccess",
64
+ "SkillDiagnostic",
65
+ "SkillDocument",
66
+ "SkillsLoader",
67
+ "check_skill_access",
68
+ "create_skill_tool",
69
+ "parse_skill_document",
70
+ ]