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,366 @@
|
|
|
1
|
+
"""异步 SDK:与同步客户端一一对应,用 ``await`` 与 ``async for``。
|
|
2
|
+
|
|
3
|
+
```python
|
|
4
|
+
from braid_coding_agent.sdk import AsyncBraidClient
|
|
5
|
+
|
|
6
|
+
async with AsyncBraidClient("http://127.0.0.1:4096", token="...") as client:
|
|
7
|
+
session = await client.create_session(name="演示")
|
|
8
|
+
async for event in await client.events(session.id):
|
|
9
|
+
print(event.type)
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
方法名与参数和同步版本完全相同,便于两处互相迁移。共享的常量、错误类型与事件
|
|
13
|
+
解析都在 :mod:`.client` / :mod:`.events` 里,这里只重复 I/O 的形状。
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from collections.abc import AsyncIterator, Callable, Mapping, Sequence
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
import httpx
|
|
22
|
+
|
|
23
|
+
from .client import DEFAULT_BASE_URL, DEFAULT_TIMEOUT, JsonBody
|
|
24
|
+
from .errors import BraidApiError
|
|
25
|
+
from .events import Event, FrameParser, last_assistant_text
|
|
26
|
+
from .types import RunResult, Session
|
|
27
|
+
from .wire import clean_params, decode_json, dict_items, require_dict, seg
|
|
28
|
+
|
|
29
|
+
__all__ = ["AsyncBraidClient", "AsyncEventStream"]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class AsyncEventStream:
|
|
33
|
+
"""一条打开的 SSE 连接(异步)。"""
|
|
34
|
+
|
|
35
|
+
def __init__(self, response: httpx.Response) -> None:
|
|
36
|
+
self._response = response
|
|
37
|
+
|
|
38
|
+
def __aiter__(self) -> AsyncIterator[Event]:
|
|
39
|
+
return self._iterate()
|
|
40
|
+
|
|
41
|
+
async def _iterate(self) -> AsyncIterator[Event]:
|
|
42
|
+
parser = FrameParser()
|
|
43
|
+
async for line in self._response.aiter_lines():
|
|
44
|
+
event = parser.feed(line)
|
|
45
|
+
if event is not None:
|
|
46
|
+
yield event
|
|
47
|
+
tail = parser.flush()
|
|
48
|
+
if tail is not None:
|
|
49
|
+
yield tail
|
|
50
|
+
|
|
51
|
+
async def aclose(self) -> None:
|
|
52
|
+
await self._response.aclose()
|
|
53
|
+
|
|
54
|
+
async def __aenter__(self) -> AsyncEventStream:
|
|
55
|
+
return self
|
|
56
|
+
|
|
57
|
+
async def __aexit__(self, *_: object) -> None:
|
|
58
|
+
await self.aclose()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class AsyncBraidClient:
|
|
62
|
+
"""异步客户端。参数含义与 :class:`~braid_coding_agent.sdk.client.BraidClient` 相同。"""
|
|
63
|
+
|
|
64
|
+
def __init__(
|
|
65
|
+
self,
|
|
66
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
67
|
+
*,
|
|
68
|
+
token: str | None = None,
|
|
69
|
+
timeout: httpx.Timeout | float | None = None,
|
|
70
|
+
client: httpx.AsyncClient | None = None,
|
|
71
|
+
transport: httpx.AsyncBaseTransport | None = None,
|
|
72
|
+
) -> None:
|
|
73
|
+
headers = {"Authorization": f"Bearer {token}"} if token else {}
|
|
74
|
+
self._owns_client = client is None
|
|
75
|
+
self._client = client or httpx.AsyncClient(
|
|
76
|
+
base_url=base_url.rstrip("/"),
|
|
77
|
+
headers=headers,
|
|
78
|
+
timeout=DEFAULT_TIMEOUT if timeout is None else timeout,
|
|
79
|
+
transport=transport,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
async def aclose(self) -> None:
|
|
83
|
+
if self._owns_client:
|
|
84
|
+
await self._client.aclose()
|
|
85
|
+
|
|
86
|
+
async def __aenter__(self) -> AsyncBraidClient:
|
|
87
|
+
return self
|
|
88
|
+
|
|
89
|
+
async def __aexit__(self, *_: object) -> None:
|
|
90
|
+
await self.aclose()
|
|
91
|
+
|
|
92
|
+
# ------------------------------------------------------------------ 元信息
|
|
93
|
+
|
|
94
|
+
async def health(self) -> dict[str, Any]:
|
|
95
|
+
return await self._json("GET", "/health")
|
|
96
|
+
|
|
97
|
+
async def openapi(self) -> dict[str, Any]:
|
|
98
|
+
return await self._json("GET", "/openapi.json")
|
|
99
|
+
|
|
100
|
+
async def models(self) -> list[dict[str, Any]]:
|
|
101
|
+
return dict_items((await self._json("GET", "/models")).get("models"))
|
|
102
|
+
|
|
103
|
+
# -------------------------------------------------------------------- 会话
|
|
104
|
+
|
|
105
|
+
async def create_session(
|
|
106
|
+
self, name: str | None = None, *, parent: str | None = None
|
|
107
|
+
) -> Session:
|
|
108
|
+
payload = await self._json(
|
|
109
|
+
"POST", "/sessions", body={"name": name, "parentSession": parent}, expected=(201,)
|
|
110
|
+
)
|
|
111
|
+
return Session.from_wire(require_dict(payload.get("session")))
|
|
112
|
+
|
|
113
|
+
async def list_sessions(self, *, limit: int = 50) -> list[Session]:
|
|
114
|
+
payload = await self._json("GET", "/sessions", params={"limit": limit})
|
|
115
|
+
return [Session.from_wire(item) for item in dict_items(payload.get("sessions"))]
|
|
116
|
+
|
|
117
|
+
async def get_session(self, session_id: str) -> Session:
|
|
118
|
+
payload = await self._json("GET", f"/sessions/{seg(session_id)}")
|
|
119
|
+
return Session.from_wire(require_dict(payload.get("session")))
|
|
120
|
+
|
|
121
|
+
async def delete_session(self, session_id: str) -> None:
|
|
122
|
+
await self._request("DELETE", f"/sessions/{seg(session_id)}", expected=(204,))
|
|
123
|
+
|
|
124
|
+
async def fork_session(self, session_id: str, entry_id: str | None = None) -> Session:
|
|
125
|
+
payload = await self._json(
|
|
126
|
+
"POST",
|
|
127
|
+
f"/sessions/{seg(session_id)}/fork",
|
|
128
|
+
body={"entryId": entry_id},
|
|
129
|
+
expected=(201,),
|
|
130
|
+
)
|
|
131
|
+
return Session.from_wire(require_dict(payload.get("session")))
|
|
132
|
+
|
|
133
|
+
async def state(self, session_id: str) -> dict[str, Any]:
|
|
134
|
+
return await self._json("GET", f"/sessions/{seg(session_id)}/state")
|
|
135
|
+
|
|
136
|
+
async def plan(
|
|
137
|
+
self, session_id: str, action: str, *, goal: str | None = None
|
|
138
|
+
) -> dict[str, Any]:
|
|
139
|
+
"""进入、批准或退出规划模式,返回 ``planMode`` 状态。"""
|
|
140
|
+
body: dict[str, Any] = {"action": action}
|
|
141
|
+
if goal is not None:
|
|
142
|
+
body["goal"] = goal
|
|
143
|
+
payload = await self._json("POST", f"/sessions/{seg(session_id)}/plan", body=body)
|
|
144
|
+
return require_dict(payload.get("planMode"))
|
|
145
|
+
|
|
146
|
+
# -------------------------------------------------------------------- 消息
|
|
147
|
+
|
|
148
|
+
async def send_message(
|
|
149
|
+
self,
|
|
150
|
+
session_id: str,
|
|
151
|
+
text: str,
|
|
152
|
+
*,
|
|
153
|
+
images: Sequence[Mapping[str, str]] | None = None,
|
|
154
|
+
behavior: str = "prompt",
|
|
155
|
+
wait: bool = False,
|
|
156
|
+
) -> dict[str, Any]:
|
|
157
|
+
body: dict[str, Any] = {"text": text, "behavior": behavior, "wait": wait}
|
|
158
|
+
if images is not None:
|
|
159
|
+
body["images"] = list(images)
|
|
160
|
+
return await self._json("POST", f"/sessions/{seg(session_id)}/messages", body=body)
|
|
161
|
+
|
|
162
|
+
async def messages(self, session_id: str) -> list[dict[str, Any]]:
|
|
163
|
+
payload = await self._json("GET", f"/sessions/{seg(session_id)}/messages")
|
|
164
|
+
return dict_items(payload.get("messages"))
|
|
165
|
+
|
|
166
|
+
async def abort(self, session_id: str) -> None:
|
|
167
|
+
await self._json("POST", f"/sessions/{seg(session_id)}/abort")
|
|
168
|
+
|
|
169
|
+
# -------------------------------------------------------------------- 配置
|
|
170
|
+
|
|
171
|
+
async def set_model(self, session_id: str, provider: str, model_id: str) -> dict[str, Any]:
|
|
172
|
+
return await self._json(
|
|
173
|
+
"PUT",
|
|
174
|
+
f"/sessions/{seg(session_id)}/model",
|
|
175
|
+
body={"provider": provider, "modelId": model_id},
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
async def set_thinking(self, session_id: str, level: str) -> dict[str, Any]:
|
|
179
|
+
return await self._json(
|
|
180
|
+
"PUT", f"/sessions/{seg(session_id)}/thinking", body={"level": level}
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
async def compact(self, session_id: str, instructions: str | None = None) -> dict[str, Any]:
|
|
184
|
+
return await self._json(
|
|
185
|
+
"POST",
|
|
186
|
+
f"/sessions/{seg(session_id)}/compact",
|
|
187
|
+
body={"instructions": instructions},
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
async def undo(self, session_id: str) -> dict[str, Any]:
|
|
191
|
+
return await self._json("POST", f"/sessions/{seg(session_id)}/undo")
|
|
192
|
+
|
|
193
|
+
async def redo(self, session_id: str) -> dict[str, Any]:
|
|
194
|
+
return await self._json("POST", f"/sessions/{seg(session_id)}/redo")
|
|
195
|
+
|
|
196
|
+
async def diff(self, session_id: str) -> dict[str, Any]:
|
|
197
|
+
return await self._json("GET", f"/sessions/{seg(session_id)}/diff")
|
|
198
|
+
|
|
199
|
+
# ---------------------------------------------------------------- 批准与提问
|
|
200
|
+
|
|
201
|
+
async def pending(self, session_id: str) -> dict[str, Any]:
|
|
202
|
+
return await self._json("GET", f"/sessions/{seg(session_id)}/pending")
|
|
203
|
+
|
|
204
|
+
async def answer_permission(
|
|
205
|
+
self, session_id: str, request_id: str, scope: str
|
|
206
|
+
) -> dict[str, Any]:
|
|
207
|
+
return await self._json(
|
|
208
|
+
"POST",
|
|
209
|
+
f"/sessions/{seg(session_id)}/permissions/{seg(request_id)}",
|
|
210
|
+
body={"scope": scope},
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
async def answer_question(
|
|
214
|
+
self,
|
|
215
|
+
session_id: str,
|
|
216
|
+
request_id: str,
|
|
217
|
+
*,
|
|
218
|
+
selected: Sequence[str] | None = None,
|
|
219
|
+
custom: str | None = None,
|
|
220
|
+
) -> dict[str, Any]:
|
|
221
|
+
return await self._json(
|
|
222
|
+
"POST",
|
|
223
|
+
f"/sessions/{seg(session_id)}/questions/{seg(request_id)}",
|
|
224
|
+
body={
|
|
225
|
+
"selected": list(selected) if selected is not None else None,
|
|
226
|
+
"custom": custom,
|
|
227
|
+
},
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
# -------------------------------------------------------------------- 文件
|
|
231
|
+
|
|
232
|
+
async def read_file(
|
|
233
|
+
self, path: str, *, offset: int | None = None, limit: int | None = None
|
|
234
|
+
) -> dict[str, Any]:
|
|
235
|
+
return await self._json(
|
|
236
|
+
"GET", "/files", params={"path": path, "offset": offset, "limit": limit}
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
async def search(
|
|
240
|
+
self,
|
|
241
|
+
pattern: str,
|
|
242
|
+
*,
|
|
243
|
+
path: str | None = None,
|
|
244
|
+
glob: str | None = None,
|
|
245
|
+
ignore_case: bool = False,
|
|
246
|
+
literal: bool = False,
|
|
247
|
+
context: int = 0,
|
|
248
|
+
limit: int = 100,
|
|
249
|
+
) -> dict[str, Any]:
|
|
250
|
+
return await self._json(
|
|
251
|
+
"GET",
|
|
252
|
+
"/files/search",
|
|
253
|
+
params={
|
|
254
|
+
"pattern": pattern,
|
|
255
|
+
"path": path,
|
|
256
|
+
"glob": glob,
|
|
257
|
+
"ignoreCase": ignore_case,
|
|
258
|
+
"literal": literal,
|
|
259
|
+
"context": context,
|
|
260
|
+
"limit": limit,
|
|
261
|
+
},
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
async def find(
|
|
265
|
+
self, pattern: str, *, path: str | None = None, limit: int = 1000
|
|
266
|
+
) -> dict[str, Any]:
|
|
267
|
+
return await self._json(
|
|
268
|
+
"GET", "/files/find", params={"pattern": pattern, "path": path, "limit": limit}
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
# --------------------------------------------------------------------- MCP
|
|
272
|
+
|
|
273
|
+
async def mcp(self) -> dict[str, Any]:
|
|
274
|
+
return await self._json("GET", "/mcp")
|
|
275
|
+
|
|
276
|
+
async def enable_mcp(self, name: str) -> dict[str, Any]:
|
|
277
|
+
return await self._json("POST", f"/mcp/{seg(name)}/enable")
|
|
278
|
+
|
|
279
|
+
async def disable_mcp(self, name: str) -> dict[str, Any]:
|
|
280
|
+
return await self._json("POST", f"/mcp/{seg(name)}/disable")
|
|
281
|
+
|
|
282
|
+
# -------------------------------------------------------------------- 事件
|
|
283
|
+
|
|
284
|
+
async def events(self, session_id: str, *, since: int | None = None) -> AsyncEventStream:
|
|
285
|
+
"""打开事件流。返回的对象要 ``aclose()``(或用 ``async with``)。"""
|
|
286
|
+
request = self._client.build_request(
|
|
287
|
+
"GET",
|
|
288
|
+
f"/sessions/{seg(session_id)}/events",
|
|
289
|
+
params=None if since is None else {"since": since},
|
|
290
|
+
)
|
|
291
|
+
response = await self._client.send(request, stream=True)
|
|
292
|
+
if response.status_code != 200:
|
|
293
|
+
await response.aread()
|
|
294
|
+
try:
|
|
295
|
+
payload: Any = response.json()
|
|
296
|
+
except ValueError:
|
|
297
|
+
payload = response.text
|
|
298
|
+
await response.aclose()
|
|
299
|
+
raise BraidApiError.from_payload(response.status_code, payload)
|
|
300
|
+
return AsyncEventStream(response)
|
|
301
|
+
|
|
302
|
+
async def run(
|
|
303
|
+
self,
|
|
304
|
+
session_id: str,
|
|
305
|
+
text: str,
|
|
306
|
+
*,
|
|
307
|
+
images: Sequence[Mapping[str, str]] | None = None,
|
|
308
|
+
on_event: Callable[[Event], None] | None = None,
|
|
309
|
+
) -> RunResult:
|
|
310
|
+
"""发一条消息并读到 ``agent_settled``。语义与同步客户端一致。"""
|
|
311
|
+
settled = False
|
|
312
|
+
aborted = False
|
|
313
|
+
stream = await self.events(session_id)
|
|
314
|
+
try:
|
|
315
|
+
frames = stream.__aiter__()
|
|
316
|
+
await frames.__anext__() # ready
|
|
317
|
+
await self.send_message(session_id, text, images=images)
|
|
318
|
+
async for event in frames:
|
|
319
|
+
if on_event is not None:
|
|
320
|
+
on_event(event)
|
|
321
|
+
if event.type == "agent_settled":
|
|
322
|
+
settled = True
|
|
323
|
+
aborted = bool(event.data.get("aborted"))
|
|
324
|
+
break
|
|
325
|
+
finally:
|
|
326
|
+
await stream.aclose()
|
|
327
|
+
messages = await self.messages(session_id)
|
|
328
|
+
return RunResult(
|
|
329
|
+
text=last_assistant_text(messages),
|
|
330
|
+
messages=messages,
|
|
331
|
+
aborted=aborted,
|
|
332
|
+
settled=settled,
|
|
333
|
+
)
|
|
334
|
+
|
|
335
|
+
# -------------------------------------------------------------------- 内部
|
|
336
|
+
|
|
337
|
+
async def _request(
|
|
338
|
+
self,
|
|
339
|
+
method: str,
|
|
340
|
+
path: str,
|
|
341
|
+
*,
|
|
342
|
+
params: Mapping[str, Any] | None = None,
|
|
343
|
+
body: JsonBody = None,
|
|
344
|
+
expected: tuple[int, ...] = (200,),
|
|
345
|
+
) -> httpx.Response:
|
|
346
|
+
response = await self._client.request(
|
|
347
|
+
method,
|
|
348
|
+
path,
|
|
349
|
+
params=clean_params(params),
|
|
350
|
+
json=dict(body) if body is not None else None,
|
|
351
|
+
)
|
|
352
|
+
if response.status_code not in expected:
|
|
353
|
+
raise BraidApiError.from_payload(response.status_code, decode_json(response))
|
|
354
|
+
return response
|
|
355
|
+
|
|
356
|
+
async def _json(
|
|
357
|
+
self,
|
|
358
|
+
method: str,
|
|
359
|
+
path: str,
|
|
360
|
+
*,
|
|
361
|
+
params: Mapping[str, Any] | None = None,
|
|
362
|
+
body: JsonBody = None,
|
|
363
|
+
expected: tuple[int, ...] = (200,),
|
|
364
|
+
) -> dict[str, Any]:
|
|
365
|
+
response = await self._request(method, path, params=params, body=body, expected=expected)
|
|
366
|
+
return require_dict(decode_json(response))
|