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,152 @@
1
+ """编码层的转录投影:把非 LLM 角色的消息折成模型能读的 user 消息。
2
+
3
+ 这一层存在的理由,是 :mod:`braid_agent.types` 把"转录"与"模型输入"分开:
4
+
5
+ - 转录里的 ``AgentMessage`` 允许自定义角色(``custom`` / ``branchSummary`` /
6
+ ``compactionSummary``),它们带着扩展自己的语义;
7
+ - 模型只认四种角色(system / user / assistant / toolResult);
8
+ - 两者之间的唯一通道是 ``convert_to_llm``。
9
+
10
+ ## 三类自定义消息各自怎么折
11
+
12
+ - ``custom`` → 原样的 user 消息,内容与时间戳都保留。它是"参与上下文但不是对话"的
13
+ 消息,扩展注入的正是模型要读的话。
14
+ - ``branchSummary`` → 带 ``<summary>`` 包裹的 user 消息。模型要知道"上面那段是被
15
+ 放弃的分支",否则会把摘要当成用户说过的话。
16
+ - ``compactionSummary`` → 带 ``<summary>`` 包裹的 user 消息。同上,但要换引导语:
17
+ 读的人(模型)正在看被替换掉的历史。
18
+
19
+ 包裹文本不是装饰。摘要本身是模型生成的普通句子,不加边界它就和真实的用户发言混在
20
+ 一起:模型无法判断"这段是历史概括"还是"用户刚说的要求",压缩之后往往会去执行摘要里
21
+ 提到的旧任务。
22
+
23
+ ## 为什么要复制一层,而不是就地改 role
24
+
25
+ ``SessionManager`` 里的条目是**事实**,投影是**视图**。把 ``custom`` 就地改成 user 会写
26
+ 回会话文件,下次恢复时那条消息的来历就消失了(它再也回不到 ``custom``)。
27
+ :func:`convert_to_llm` 每次都新建消息,原始对象不动。
28
+
29
+ ## 未知角色
30
+
31
+ ``bashExecution``(``!`` 命令的产物)没有建模:当前没有生产者。
32
+ 未知角色直接丢弃——这是唯一安全的选择,因为转录里可能出现未来版本写入的角色,
33
+ 而"猜一个转换方式"会让模型读到一段语义错误的上下文(与 :mod:`braid_agent.agent` 的
34
+ ``default_convert_to_llm`` 同一策略)。
35
+ """
36
+
37
+ from __future__ import annotations
38
+
39
+ from collections.abc import Sequence
40
+
41
+ from braid_agent.types import AgentMessage
42
+ from braid_ai.types import (
43
+ AssistantMessage,
44
+ ImageContent,
45
+ Message,
46
+ SystemMessage,
47
+ TextContent,
48
+ ToolResultMessage,
49
+ UserMessage,
50
+ )
51
+ from braid_coding_agent.session.entries import (
52
+ BranchSummaryMessage,
53
+ CompactionSummaryMessage,
54
+ CustomMessage,
55
+ ProjectedMessage,
56
+ TextImageContent,
57
+ )
58
+
59
+ #: 压缩摘要的引导语。改一个字都会让模型对"这是历史"的判断变弱。
60
+ COMPACTION_SUMMARY_PREFIX = (
61
+ "The conversation history before this point was compacted into the following summary:\n\n"
62
+ "<summary>\n"
63
+ )
64
+ COMPACTION_SUMMARY_SUFFIX = "\n</summary>"
65
+
66
+ #: 分支摘要的引导语。与压缩摘要分开写,因为它们描述的是两件事:
67
+ #: 一段历史被概括了,与一条分支被放弃了。
68
+ BRANCH_SUMMARY_PREFIX = (
69
+ "The following is a summary of a branch that this conversation came back from:\n\n<summary>\n"
70
+ )
71
+ BRANCH_SUMMARY_SUFFIX = "</summary>"
72
+
73
+ #: 有明确转换规则的七种消息。顺序即类型判断顺序,子类关系不存在,顺序不影响语义。
74
+ _PROJECTED_TYPES = (
75
+ SystemMessage,
76
+ UserMessage,
77
+ AssistantMessage,
78
+ ToolResultMessage,
79
+ CustomMessage,
80
+ BranchSummaryMessage,
81
+ CompactionSummaryMessage,
82
+ )
83
+
84
+
85
+ def convert_to_llm(messages: Sequence[AgentMessage]) -> list[Message]:
86
+ """转录 → 模型输入。未知角色被丢弃,四种 LLM 角色原样通过。
87
+
88
+ 这是 :class:`braid_agent.types.ConvertToLlm` 的编码层实现,交给
89
+ ``Agent(convert_to_llm=...)`` 使用。纯函数:不读时间、不改入参。
90
+ """
91
+ converted: list[Message] = []
92
+ for message in messages:
93
+ projected = _as_projected(message)
94
+ if projected is None:
95
+ continue
96
+ match projected:
97
+ case CustomMessage():
98
+ converted.append(_to_user(projected.content, projected.timestamp))
99
+ case BranchSummaryMessage():
100
+ converted.append(
101
+ _text_to_user(
102
+ f"{BRANCH_SUMMARY_PREFIX}{projected.summary}{BRANCH_SUMMARY_SUFFIX}",
103
+ projected.timestamp,
104
+ )
105
+ )
106
+ case CompactionSummaryMessage():
107
+ converted.append(
108
+ _text_to_user(
109
+ f"{COMPACTION_SUMMARY_PREFIX}{projected.summary}{COMPACTION_SUMMARY_SUFFIX}",
110
+ projected.timestamp,
111
+ )
112
+ )
113
+ case _:
114
+ converted.append(projected)
115
+ return converted
116
+
117
+
118
+ def _as_projected(message: AgentMessage) -> ProjectedMessage | None:
119
+ """窄化到"有明确规则"的七种消息;其余(未来的自定义角色)返回 ``None``。
120
+
121
+ ``AgentMessage`` 是 ``Message | CustomAgentMessage`` 的联合,后者是结构化协议,
122
+ 直接 ``isinstance`` 无法把类型收窄到具体类。先过一道具体类型判断,``match`` 才能
123
+ 逐类分派而不需要 ``cast``。
124
+ """
125
+ if isinstance(message, _PROJECTED_TYPES):
126
+ return message
127
+ return None
128
+
129
+
130
+ def _to_user(content: TextImageContent, timestamp: int) -> UserMessage:
131
+ """``custom`` 消息的内容与时间戳原样搬进 user 消息。
132
+
133
+ 字符串形态归一成单块数组:模型请求里 user 内容的两种形态等价,但统一成数组之后
134
+ 下游(图片处理、上下文估算)只需要处理一种形状。
135
+ """
136
+ if isinstance(content, str):
137
+ return UserMessage(timestamp=timestamp, content=[TextContent(text=content)])
138
+ blocks: list[TextContent | ImageContent] = list(content)
139
+ return UserMessage(timestamp=timestamp, content=blocks)
140
+
141
+
142
+ def _text_to_user(text: str, timestamp: int) -> UserMessage:
143
+ return UserMessage(timestamp=timestamp, content=[TextContent(text=text)])
144
+
145
+
146
+ __all__ = [
147
+ "BRANCH_SUMMARY_PREFIX",
148
+ "BRANCH_SUMMARY_SUFFIX",
149
+ "COMPACTION_SUMMARY_PREFIX",
150
+ "COMPACTION_SUMMARY_SUFFIX",
151
+ "convert_to_llm",
152
+ ]
@@ -0,0 +1,105 @@
1
+ """模型目录的唯一呈现:``braid models`` 与 ``/models`` 输出同一组行。
2
+
3
+ 两处各写一份渲染的后果很具体:``braid models`` 里能看到价格,``/models`` 里看不到;
4
+ 或者一边改了字段名,另一边还在打旧字段。这里只保留一个函数,两处都调它。
5
+
6
+ 一行回答五个问题:
7
+
8
+ - 这是哪个模型(``provider/id``,与 ``--model``、会话记录、``/models`` 一致);
9
+ - 装得下多少(上下文窗口)与写多长(输出上限);
10
+ - 能不能看图(输入模态)、会不会思考(档位);
11
+ - 花多少钱(美元 / 百万 token,只显示模型目录声明过的价格);
12
+ - 现在能不能用(不可用的模型标一个 ``不可用``,原因统一写在末尾的「凭证」一行)。
13
+
14
+ 价格只在输入或输出有一个非零时出现:没有声明价格的模型打 ``0/0`` 会被读成"免费"。
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ from collections.abc import Sequence
20
+
21
+ from braid_ai.types import Model
22
+ from braid_coding_agent.config import AgentConfig
23
+ from braid_coding_agent.credentials import Credentials
24
+
25
+
26
+ def format_model(
27
+ model: Model,
28
+ *,
29
+ current: Model | None,
30
+ config: AgentConfig | None = None,
31
+ credentials: Credentials | None = None,
32
+ mark: str = "*",
33
+ ) -> str:
34
+ """一个模型一行。``mark`` 是当前选中项的标记(视觉模式用 ``●``,纯文本用 ``*``)。"""
35
+ parts = [f"{mark if _same_model(model, current) else ' '} {model.provider}/{model.id}"]
36
+ if model.name and model.name != model.id:
37
+ parts.append(model.name)
38
+ parts.append(f"上下文 {model.context_window}")
39
+ parts.append(f"输出 {model.max_tokens}")
40
+ parts.append(f"输入 {'+'.join(model.input)}")
41
+ if model.thinking_levels:
42
+ parts.append(f"思考 {'/'.join(model.thinking_levels)}")
43
+ elif model.reasoning:
44
+ parts.append("推理")
45
+ if model.cost.input or model.cost.output:
46
+ parts.append(f"价格 {model.cost.input:g}/{model.cost.output:g}")
47
+ if config is not None and config.is_discovered(model.provider, model.id):
48
+ parts.append("端点刷新")
49
+ if credentials is not None and config is not None:
50
+ entry = config.provider(model.provider)
51
+ if entry is not None and not credentials.status(entry.id).available:
52
+ # 原因统一写在末尾的「凭证」一行:每个模型都重复一遍会把目录刷成一片红。
53
+ parts.append("不可用")
54
+ return " · ".join(parts)
55
+
56
+
57
+ def catalog_lines(
58
+ models: Sequence[Model],
59
+ *,
60
+ current: Model | None,
61
+ config: AgentConfig | None = None,
62
+ credentials: Credentials | None = None,
63
+ mark: str = "*",
64
+ ) -> list[str]:
65
+ """完整的目录视图:标题、每个模型一行、末尾的来源说明。
66
+
67
+ :param models: 要显示的模型。``/models`` 传运行时的注册表(可能含 ``faux``),
68
+ ``braid models`` 传配置对象里的目录。
69
+ :param config: 给了它才显示默认模型、配置文件与环境变量的来源。
70
+ :param credentials: 给了它才显示"哪些模型现在不可用"。
71
+ """
72
+ providers = list(dict.fromkeys(model.provider for model in models))
73
+ lines = [f"模型目录({len(providers)} 个 provider,{len(models)} 个模型):"]
74
+ lines.extend(
75
+ format_model(model, current=current, config=config, credentials=credentials, mark=mark)
76
+ for model in models
77
+ )
78
+ if config is None:
79
+ return lines
80
+ if config.default_model is not None:
81
+ lines.append(f"默认模型:{config.default_model}(来自 {config.default_model_source})")
82
+ else:
83
+ lines.append("默认模型:未声明(启动时按命令行与会话记录决定)")
84
+ if config.sources:
85
+ lines.append(f"配置文件:{' → '.join(config.sources)}")
86
+ else:
87
+ lines.append("配置文件:没有读到(只用了内置默认值)")
88
+ if config.env_overrides:
89
+ lines.append(f"环境变量覆盖:{', '.join(config.env_overrides)}")
90
+ if credentials is not None:
91
+ lines.append(f"凭证:{_credential_summary(credentials)}")
92
+ return lines
93
+
94
+
95
+ def _credential_summary(credentials: Credentials) -> str:
96
+ """``deepseek ← 环境变量 X;openai ← 未配置:…`` 一行说完。"""
97
+ parts = [f"{status.provider_id} ← {status.detail}" for status in credentials.statuses()]
98
+ return ";".join(parts) if parts else "(模型目录里没有 provider)"
99
+
100
+
101
+ def _same_model(left: Model, right: Model | None) -> bool:
102
+ return right is not None and left.provider == right.provider and left.id == right.id
103
+
104
+
105
+ __all__ = ["catalog_lines", "format_model"]
@@ -0,0 +1,64 @@
1
+ """modes:进程级运行模式。
2
+
3
+ REPL 的两个模块**不**在这里再导出:``braid_coding_agent.modes.repl`` 依赖
4
+ ``prompt_toolkit``,而 ``PrintModeOptions`` 这类轻量入口(例如别处的库调用)
5
+ 不该为了 import 一个数据类就把终端库拖进来。需要 REPL 的调用方显式
6
+ ``from braid_coding_agent.modes.repl import Repl``。
7
+
8
+ RPC 没有这层顾虑:它只用标准库与 ``braid_ai``/本包的会话类型,所以 ``RpcServer``
9
+ 在这里导出,方便把服务嵌进别人的进程。
10
+ """
11
+
12
+ import importlib
13
+ from typing import TYPE_CHECKING
14
+
15
+ if TYPE_CHECKING:
16
+ from braid_coding_agent.modes.json_mode import (
17
+ compaction_result_to_wire,
18
+ model_to_wire,
19
+ session_event_to_wire,
20
+ session_header_to_wire,
21
+ transcript_message_to_wire,
22
+ )
23
+ from braid_coding_agent.modes.print_mode import PrintModeOptions, run_print_mode
24
+ from braid_coding_agent.modes.rpc import NewSessionFn, RpcServer, Stdout, iter_json_lines
25
+
26
+ _LAZY_EXPORTS: dict[str, str] = {
27
+ "compaction_result_to_wire": "braid_coding_agent.modes.json_mode",
28
+ "model_to_wire": "braid_coding_agent.modes.json_mode",
29
+ "session_event_to_wire": "braid_coding_agent.modes.json_mode",
30
+ "session_header_to_wire": "braid_coding_agent.modes.json_mode",
31
+ "transcript_message_to_wire": "braid_coding_agent.modes.json_mode",
32
+ "PrintModeOptions": "braid_coding_agent.modes.print_mode",
33
+ "run_print_mode": "braid_coding_agent.modes.print_mode",
34
+ "NewSessionFn": "braid_coding_agent.modes.rpc",
35
+ "RpcServer": "braid_coding_agent.modes.rpc",
36
+ "Stdout": "braid_coding_agent.modes.rpc",
37
+ "iter_json_lines": "braid_coding_agent.modes.rpc",
38
+ }
39
+
40
+
41
+ def __getattr__(name: str) -> object:
42
+ """按需导入导出名。"""
43
+ module_name = _LAZY_EXPORTS.get(name)
44
+ if module_name is None:
45
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
46
+ module = importlib.import_module(module_name)
47
+ value = getattr(module, name)
48
+ globals()[name] = value
49
+ return value
50
+
51
+
52
+ __all__ = [
53
+ "NewSessionFn",
54
+ "PrintModeOptions",
55
+ "RpcServer",
56
+ "Stdout",
57
+ "compaction_result_to_wire",
58
+ "iter_json_lines",
59
+ "model_to_wire",
60
+ "run_print_mode",
61
+ "session_event_to_wire",
62
+ "session_header_to_wire",
63
+ "transcript_message_to_wire",
64
+ ]