@beeos-ai/bma-dsh-adapter 0.0.0-stage → 0.2.0
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.
- package/LICENSE +201 -0
- package/NOTICE +11 -0
- package/README.md +32 -2
- package/UPGRADING.md +25 -0
- package/lib/adapter.d.ts +58 -0
- package/lib/adapter.js +212 -0
- package/lib/approval.d.ts +31 -0
- package/lib/approval.js +24 -0
- package/lib/async-queue.d.ts +12 -0
- package/lib/async-queue.js +41 -0
- package/lib/catalog-gate.d.ts +7 -0
- package/lib/catalog-gate.js +28 -0
- package/lib/conformance/golden/shell-tool-events.json +198 -0
- package/lib/conformance/golden/turn-reasoning-text.json +26 -0
- package/lib/conformance/golden/turn-tool-call.json +26 -0
- package/lib/conformance/index.d.ts +32 -0
- package/lib/conformance/index.js +155 -0
- package/lib/conformance/shell.d.ts +64 -0
- package/lib/conformance/shell.js +202 -0
- package/lib/events.d.ts +21 -0
- package/lib/events.js +30 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +13 -0
- package/lib/kernel.d.ts +18 -0
- package/lib/kernel.js +54 -0
- package/lib/model-catalog.d.ts +51 -0
- package/lib/model-catalog.js +98 -0
- package/lib/model.d.ts +62 -0
- package/lib/model.js +366 -0
- package/lib/recovery.d.ts +8 -0
- package/lib/recovery.js +19 -0
- package/lib/spi.d.ts +179 -0
- package/lib/spi.js +24 -0
- package/lib/stop.d.ts +21 -0
- package/lib/stop.js +53 -0
- package/lib/system-prompt.d.ts +3 -0
- package/lib/system-prompt.js +13 -0
- package/lib/testing/index.d.ts +3 -0
- package/lib/testing/index.js +3 -0
- package/lib/testing/scripted-model.d.ts +78 -0
- package/lib/testing/scripted-model.js +56 -0
- package/lib/testing/tmp.d.ts +24 -0
- package/lib/testing/tmp.js +55 -0
- package/lib/testing/tools.d.ts +3 -0
- package/lib/testing/tools.js +11 -0
- package/lib/version.d.ts +1 -0
- package/lib/version.js +4 -0
- package/package.json +75 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
@beeos-ai/bma-dsh-adapter
|
|
2
|
+
Copyright 2026 BeeOS
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
5
|
+
|
|
6
|
+
This package contains original BeeOS code (it is not derived from the
|
|
7
|
+
Omnara-based platform sources). It does not contain code from the BeeOS
|
|
8
|
+
desktop application or any other code under an internal license.
|
|
9
|
+
|
|
10
|
+
The DSH libraries (@deepseek-ai/*, MIT) and the other dependencies are not
|
|
11
|
+
bundled: they are installed from the npm registry under their own licenses.
|
package/README.md
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @beeos-ai/bma-dsh-adapter
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
BMA 的共享 DSH 适配层(Apache-2.0)。把 [DSH](https://www.npmjs.com/org/deepseek-ai) 的 npm 库嵌进宿主进程,只给模型宿主提供的平台工具;宿主外壳(服务端 `brain-host`、后续的 `bma-dsh-acp` ACP stdio 外壳等)依赖本包,不各自重写。来源与取舍见 `bma-control/research/dsh-cloud-vs-desktop-brain-20261108.md`(Desktop 的实现是内部代码,这里只按行为描述实现)。
|
|
4
|
+
|
|
5
|
+
| 模块 | 内容 | 钉住它的测试 |
|
|
6
|
+
|---|---|---|
|
|
7
|
+
| `kernel.ts` | DSH 内核组装(LLM、会话、JSONL、系统提示、工具运行时、附件、TokenMeter、工具结果裁剪、CompactionBasic、ImageOffload、AgentLoop) | `test/*`(升级时的破坏点都在这里) |
|
|
8
|
+
| `catalog-gate.ts` | 工具目录门禁:`restrict({allow:[]})` 后只注册平台工具;`catalogViolation` 供宿主拒绝多出/缺少工具的会话 | `gate-catalog.test.ts`、`events-approval.test.ts`、一致性套件 |
|
|
9
|
+
| `events.ts` | DSH session 事件 / 流帧 → `HarnessEvent` → CMA `agent.message` / `agent.thinking` | `events-approval.test.ts`、golden |
|
|
10
|
+
| `approval.ts` | 审批代理中与传输无关的部分:确定性 `approvalId`(`appr_`+sha256(session,turn,call))、permission 请求、拒绝文案 | `events-approval.test.ts`(id 向量钉死) |
|
|
11
|
+
| `recovery.ts` + `adapter.ts` | JSONL resume(无日志 → `SessionNotFoundError`)、按 input id 幂等(已记录的输入只发续跑提示) | 一致性套件 resume |
|
|
12
|
+
| `stop.ts` + `spi.ts` | 停止原因映射:`output_limit_reached` / `context_too_large` / `model_stream_idle`(`TurnStopError`,消息以 `<code>: ` 开头),其余通用失败;`AGENT_ERROR_CODES` / `agentErrorCode` 对应执行端契约码表(`contracts/bma/schemas/agent.json` `AgentErrorCode`) | 一致性套件 stop reasons、`stop-contract.test.ts`、`model-limits.test.ts` |
|
|
13
|
+
| `system-prompt.ts` | `BASE_SYSTEM_PROMPT` + `composeSystemPrompt(agentSystem?)`:所有外壳在这里拼系统提示,不自己写死 | 一致性套件 system prompt、`system-prompt.test.ts` |
|
|
14
|
+
| `model.ts`、`model-catalog.ts` | OpenAI Chat 适配器:按模型取 max_tokens / 窗口(声明 > 目录文件 > 保守默认,记录)、图片 `image_url`、流空闲守卫(首个内容增量前空闲:退避 1–2 s 自动重试 1 次,已有内容后不重试) | `model-limits.test.ts` |
|
|
15
|
+
| `spi.ts` | `HarnessAdapter` / `ModelPort` / `PlatformTool` 契约(harness 无关) | — |
|
|
16
|
+
|
|
17
|
+
审批的等待逻辑(interaction.request/response、停机时保持打开)属于宿主的通道,留在宿主里。
|
|
18
|
+
|
|
19
|
+
## 一致性套件
|
|
20
|
+
|
|
21
|
+
`import { registerConformance } from "@beeos-ai/bma-dsh-adapter/conformance"`,宿主测试传入一个工厂(挂载自己的 adapter 接线到 `@beeos-ai/bma-dsh-adapter/testing` 的脚本化模型),即得同一组用例:工具目录、CMA 事件 golden(`src/conformance/golden/*.json`,`UPDATE_GOLDEN=1` 重写后须审 diff)、resume 与输入幂等、取消、带类型停止原因。本包自己的接线(只含内核级用例)见 `test/conformance.test.ts`。
|
|
22
|
+
|
|
23
|
+
外壳级用例:`registerConformance(name, kernelFactory, { shell })`,`shell` 是 `ShellFactory`(`Shell` 接口:起停外壳进程、开回合/取消、读平台收到的持久事件与 interaction、重投回合)。每个连平台的外壳(brain-host、bma-dsh-acp)必须传入,得到:工具事件 golden(`shell-tool-events.json`:`agent.tool_use`/`agent.tool_result`、预览、失败与拒绝的调用)、每个 turn 恰有一个终态且为最后一个事件(失败带契约停止码)、取消在 `CANCEL_ACK_BUDGET_MS`(10 s)内确认且撤回待决审批、模型流空闲终态 `model_stream_idle`、审批 id 跨重启不变且已答复的不再问用户。brain-host 的接线见 `brain-host/test/conformance.test.ts`。
|
|
24
|
+
|
|
25
|
+
## 安装(npm)
|
|
26
|
+
|
|
27
|
+
`npm install @beeos-ai/bma-dsh-adapter`(Node ≥ 24,已发布为 JavaScript + `.d.ts`,`exports`:`.`、`./testing`、`./conformance`)。**不打包 DSH**:`@deepseek-ai/*` 由 npm 从官方源安装,版本在本包精确钉死(含 DSH 自身留有区间的 cosmokit / schemastery / flock addon),所以同一版本号总装到同一份 DSH。ACP 外壳见 [`@beeos-ai/bma-dsh-acp`](../dsh-acp/README.md)(`npx @beeos-ai/bma-dsh-acp`)。发布:tag `dsh-acp-vX.Y.Z`,两包版本一致(`.github/workflows/dsh-acp-release.yaml`);工作区内 `exports` 仍指向 `src/*.ts`,发布时由 `publishConfig.exports` 换成 `lib/*.js`(Node 不对 `node_modules` 下的 `.ts` 做类型剥离)。构建:`pnpm run build`。
|
|
28
|
+
|
|
29
|
+
## 运行(工作区)
|
|
30
|
+
|
|
31
|
+
Node ≥ 24(TypeScript 原生类型剥离,无构建步骤)。`pnpm test`(在 brain-host 根目录跑整个 workspace:`scripts/ci.sh`)。本包不依赖执行端 SDK,也不含任何模型密钥。
|
|
32
|
+
|
|
33
|
+
DSH 版本钉在本包 `package.json`,升级流程见 [UPGRADING.md](UPGRADING.md)。
|
package/UPGRADING.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# 升级 DSH(唯一钉版点)
|
|
2
|
+
|
|
3
|
+
所有宿主通过本包得到 DSH:`package.json` 里全部 `@deepseek-ai/dsh-*` 钉**同一个精确版本**,`@deepseek-ai/cordis` 钉该版本 peer 范围内的精确版本;宿主自己不得再声明 dsh 依赖(`test/pins.test.ts` 强制)。
|
|
4
|
+
|
|
5
|
+
| 项 | 现值 |
|
|
6
|
+
|---|---|
|
|
7
|
+
| dsh | 0.2.2-rc.1(2026-11-09 自 0.2.1-rc.5 升级) |
|
|
8
|
+
| cordis | 4.1.0(rc.1 的 peer `~4.1.0`) |
|
|
9
|
+
| 发布延迟豁免 | `pnpm-workspace.yaml` 按精确版本豁免 dsh 0.2.2-rc.1 全部包及其必需的 cordis 4.1.0 / cosmokit 1.9.0 / schemastery 3.19.0(用户 11-09 指令);下次升级时删除 |
|
|
10
|
+
| 下一候选 | npm `next` dist-tag(暂无补丁) |
|
|
11
|
+
|
|
12
|
+
## 流程
|
|
13
|
+
|
|
14
|
+
1. **先跑候选门禁**:`packages/dsh-adapter/scripts/gate-candidate.sh [版本]`(默认 dsh 的 npm `next` dist-tag;`scripts/ci.sh` 每次都会跑)。它把本包复制到临时目录、套用 `scripts/upgrade/<x.y.z>.patch`、改写全部 dsh 钉版、安装并运行 `test/gate-catalog.test.ts`。红 = 候选版本悄悄多出了工具,或内核组装需要改。
|
|
15
|
+
2. **改 `src/kernel.ts`**:破坏点至今都在这里(导出形态、插件选项、新必需插件;0.2.2 起 `ToolRuntime` 去掉 `mode`、必须挂 `dsh-tool-executor-native`)。把改动写成 `scripts/upgrade/<x.y.z>.patch`(+ `.deps` 列新增包)。补丁基于当前 kernel.ts,改了 kernel.ts 就要重生成。
|
|
16
|
+
3. **升级 PR**:在 `package.json` 一次性改全部 dsh 钉版与 cordis,`pnpm install`(更新 lockfile),合入补丁内容并删除 `scripts/upgrade/<x.y.z>.*`(已并入 kernel.ts),跑 `scripts/ci.sh`(含集成测试:SIGKILL/SIGTERM 续审批、RSS)。
|
|
17
|
+
4. **一致性套件与 golden 必须全绿**;golden 变化要逐项审(事件形状变了是对外行为变化)。
|
|
18
|
+
5. 新版本默认走 7 天发布延迟(`pnpm-workspace.yaml` 的 `minimumReleaseAge`);候选门禁临时豁免 `@deepseek-ai/*`(只在临时目录)。不足 7 天的版本只有用户明确要求时才升,且在 `minimumReleaseAgeExclude` 按精确版本(`name@version`)列出,不放宽全局。
|
|
19
|
+
6. 其他宿主(bma-dsh-acp 等)升级时只改这一处,并在各自测试里跑 `registerConformance`。
|
|
20
|
+
|
|
21
|
+
与 Desktop 内置 DSH(0.2.2-alpha.2)的版本线统一属 Lead 裁决 Q8:本包已到 0.2.2-rc.1;Desktop 运行时改钉随其 1.3.0 发版进行。
|
|
22
|
+
|
|
23
|
+
## 同时发 npm 时要同步的钉版(`@beeos-ai/bma-dsh-acp` 以“不打包”形态发布)
|
|
24
|
+
|
|
25
|
+
升级 PR 里还要改:`src/version.ts` 的 `DSH_VERSION`;DSH 自身留有区间的传递依赖(`@deepseek-ai/cosmokit`、`schemastery`、`node-addon-system`,见 `package.json` 里的精确直接依赖)要与新版本的范围一致(`test/pins.test.ts` 与 `scripts/check-npm.sh` 的 `check-npm-tree.ts` 检查安装结果等于钉版)。发布本身:两包同版本号、严格前进,打 tag `dsh-acp-vX.Y.Z`。
|
package/lib/adapter.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Context } from "@deepseek-ai/cordis";
|
|
2
|
+
import { type HarnessAdapter, type HarnessCapabilities, type HarnessEvent, type HarnessInput, type ModelPort, type PlatformTool, type SessionInit } from "./spi.ts";
|
|
3
|
+
export interface DshAdapterOptions {
|
|
4
|
+
home: string;
|
|
5
|
+
/** Concurrent read-only tool calls per step (dsh agent-loop); omitted = dsh's own default. */
|
|
6
|
+
maxParallelToolCalls?: number;
|
|
7
|
+
/** Model stream idle limit in ms (0 disables); default DEFAULT_STREAM_IDLE_MS. */
|
|
8
|
+
streamIdleMs?: number;
|
|
9
|
+
/** Pause range (ms) before the one retry of a stream that idled before any content; default 1000..2000. */
|
|
10
|
+
idleRetryBackoffMs?: readonly [number, number];
|
|
11
|
+
}
|
|
12
|
+
export declare class DshAdapter implements HarnessAdapter {
|
|
13
|
+
readonly id = "dsh";
|
|
14
|
+
/** The dsh kernel (exposed for the fail-closed gate tests). */
|
|
15
|
+
readonly ctx: Context;
|
|
16
|
+
private readonly sessions;
|
|
17
|
+
private tools;
|
|
18
|
+
private readonly streamIdleMs;
|
|
19
|
+
private readonly idleRetryBackoffMs;
|
|
20
|
+
private port;
|
|
21
|
+
private readonly toolImages;
|
|
22
|
+
private constructor();
|
|
23
|
+
static mount(options: DshAdapterOptions): Promise<DshAdapter>;
|
|
24
|
+
capabilities(): HarnessCapabilities;
|
|
25
|
+
configureModel(port: ModelPort): void;
|
|
26
|
+
registerTools(tools: readonly PlatformTool[]): void;
|
|
27
|
+
createSession(init: SessionInit): Promise<void>;
|
|
28
|
+
resumeSession(init: SessionInit): Promise<void>;
|
|
29
|
+
private open;
|
|
30
|
+
private get;
|
|
31
|
+
toolCatalog(sessionId: string): readonly string[];
|
|
32
|
+
submitInput(sessionId: string, input: HarnessInput, signal: AbortSignal): AsyncIterable<HarnessEvent>;
|
|
33
|
+
/** Text-only input only: the host queues an input with images as the next turn instead. */
|
|
34
|
+
appendInput(sessionId: string, input: HarnessInput): void;
|
|
35
|
+
/**
|
|
36
|
+
* Whether this build can take image input: the attachment service decodes
|
|
37
|
+
* images with sharp (a native module), which a single-file build may lack.
|
|
38
|
+
* Probed with a 1x1 PNG (content-addressed, so it is stored once).
|
|
39
|
+
*/
|
|
40
|
+
probeImageInput(): Promise<boolean>;
|
|
41
|
+
/** The user message content; images are validated, normalized and stored by the attachment service first. */
|
|
42
|
+
private content;
|
|
43
|
+
/**
|
|
44
|
+
* The conversation so far as text, oldest first: user inputs and the
|
|
45
|
+
* thinking / message events a turn produced (for an ACP session/load
|
|
46
|
+
* replay). Continuation notes of re-delivered inputs are left out.
|
|
47
|
+
*/
|
|
48
|
+
history(sessionId: string): {
|
|
49
|
+
role: "user" | "assistant";
|
|
50
|
+
type: "message" | "thinking";
|
|
51
|
+
text: string;
|
|
52
|
+
}[];
|
|
53
|
+
checkpoint(sessionId: string): Promise<void>;
|
|
54
|
+
close(sessionId: string): Promise<void>;
|
|
55
|
+
dispose(): Promise<void>;
|
|
56
|
+
private onSessionEvent;
|
|
57
|
+
private onStreamFrame;
|
|
58
|
+
}
|
package/lib/adapter.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { MessageId, freezeMessage } from "@deepseek-ai/dsh-llm";
|
|
2
|
+
import { SessionId } from "@deepseek-ai/dsh-session";
|
|
3
|
+
import {} from "./spi.js";
|
|
4
|
+
import { AsyncQueue } from "./async-queue.js";
|
|
5
|
+
import { restrictToPlatformTools } from "./catalog-gate.js";
|
|
6
|
+
import { requestMaxTokens } from "./model-catalog.js";
|
|
7
|
+
import { harnessEventFromStreamFrame, harnessEventsFromSessionEvent } from "./events.js";
|
|
8
|
+
import { CONTINUE_NOTE, assertResumable, continuationId, hasRecordedInput } from "./recovery.js";
|
|
9
|
+
import { mountKernel } from "./kernel.js";
|
|
10
|
+
import { ChatCompletionsAdapter, DEFAULT_STREAM_IDLE_MS, PROVIDER, RecentToolImages } from "./model.js";
|
|
11
|
+
import { stopForTurnEnd } from "./stop.js";
|
|
12
|
+
/**
|
|
13
|
+
* Target size of an image in a model request (no industry basis): long edge
|
|
14
|
+
* 2048 px, 3.75 MiB encoded — under the 5 MB per-image limit of the common
|
|
15
|
+
* vision APIs after base64 expansion is not guaranteed, but the platform
|
|
16
|
+
* endpoint answers an oversized request with an error rather than dropping it.
|
|
17
|
+
*/
|
|
18
|
+
const REQUEST_IMAGE_TARGET = { width: 2048, height: 2048, maxBytes: 3_932_160 };
|
|
19
|
+
const PROBE_PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==";
|
|
20
|
+
export class DshAdapter {
|
|
21
|
+
id = "dsh";
|
|
22
|
+
/** The dsh kernel (exposed for the fail-closed gate tests). */
|
|
23
|
+
ctx;
|
|
24
|
+
sessions = new Map();
|
|
25
|
+
tools = [];
|
|
26
|
+
streamIdleMs;
|
|
27
|
+
idleRetryBackoffMs;
|
|
28
|
+
port;
|
|
29
|
+
toolImages = new RecentToolImages();
|
|
30
|
+
constructor(ctx, streamIdleMs, idleRetryBackoffMs) {
|
|
31
|
+
this.ctx = ctx;
|
|
32
|
+
this.streamIdleMs = streamIdleMs;
|
|
33
|
+
this.idleRetryBackoffMs = idleRetryBackoffMs;
|
|
34
|
+
}
|
|
35
|
+
static async mount(options) {
|
|
36
|
+
const ctx = await mountKernel({ home: options.home, ...(options.maxParallelToolCalls !== undefined ? { maxParallelToolCalls: options.maxParallelToolCalls } : {}) });
|
|
37
|
+
return new DshAdapter(ctx, options.streamIdleMs ?? DEFAULT_STREAM_IDLE_MS, options.idleRetryBackoffMs);
|
|
38
|
+
}
|
|
39
|
+
capabilities() {
|
|
40
|
+
return { resume: true, deltas: true, midTurnInput: true, imageInput: true };
|
|
41
|
+
}
|
|
42
|
+
configureModel(port) {
|
|
43
|
+
this.port = port;
|
|
44
|
+
const attachments = this.ctx.attachments;
|
|
45
|
+
this.ctx.llm.registerAdapter([PROVIDER], new ChatCompletionsAdapter(port, {
|
|
46
|
+
streamIdleMs: this.streamIdleMs,
|
|
47
|
+
...(this.idleRetryBackoffMs ? { idleRetryBackoffMs: this.idleRetryBackoffMs } : {}),
|
|
48
|
+
toolImages: (sessionId, callId) => this.toolImages.get(sessionId, callId),
|
|
49
|
+
readImage: async (block, signal) => {
|
|
50
|
+
const image = await attachments.readImageRequest(block.attachment, REQUEST_IMAGE_TARGET, signal);
|
|
51
|
+
return { mediaType: image.mediaType, data: Buffer.from(image.data).toString("base64") };
|
|
52
|
+
},
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
registerTools(tools) {
|
|
56
|
+
this.tools = tools;
|
|
57
|
+
}
|
|
58
|
+
createSession(init) {
|
|
59
|
+
return this.open(init, false);
|
|
60
|
+
}
|
|
61
|
+
async resumeSession(init) {
|
|
62
|
+
await assertResumable(this.ctx, init.sessionId);
|
|
63
|
+
return this.open(init, true);
|
|
64
|
+
}
|
|
65
|
+
async open(init, resume) {
|
|
66
|
+
if (this.sessions.has(init.sessionId))
|
|
67
|
+
throw new Error(`session ${init.sessionId} is already open`);
|
|
68
|
+
const sessionId = SessionId(init.sessionId);
|
|
69
|
+
const state = { init };
|
|
70
|
+
const setup = (scope) => {
|
|
71
|
+
scope.systemPrompt.section({ name: "bma", order: 0, complete: true, interpolate: false, text: () => state.init.systemPrompt });
|
|
72
|
+
restrictToPlatformTools(scope, init.sessionId, this.tools, (callId, images) => this.toolImages.remember(init.sessionId, callId, images));
|
|
73
|
+
scope.on("session/event", (session, event) => {
|
|
74
|
+
if (session.id === sessionId)
|
|
75
|
+
this.onSessionEvent(state, event);
|
|
76
|
+
});
|
|
77
|
+
scope.on("agent/assistant-stream", ({ frame }) => this.onStreamFrame(state, frame));
|
|
78
|
+
};
|
|
79
|
+
const options = { agentOptions: { provider: PROVIDER, model: init.model }, setup };
|
|
80
|
+
state.handle = resume
|
|
81
|
+
? await this.ctx.agents.resume({ resumeSessionId: sessionId, ...options })
|
|
82
|
+
: await this.ctx.agents.create({ sessionId, meta: { cwd: init.cwd }, ...options });
|
|
83
|
+
this.sessions.set(init.sessionId, state);
|
|
84
|
+
}
|
|
85
|
+
get(sessionId) {
|
|
86
|
+
const state = this.sessions.get(sessionId);
|
|
87
|
+
if (!state)
|
|
88
|
+
throw new Error(`session ${sessionId} is not open`);
|
|
89
|
+
return state;
|
|
90
|
+
}
|
|
91
|
+
toolCatalog(sessionId) {
|
|
92
|
+
return this.ctx.tools.schemas(this.get(sessionId).handle.agent).map((s) => s.name);
|
|
93
|
+
}
|
|
94
|
+
async *submitInput(sessionId, input, signal) {
|
|
95
|
+
const state = this.get(sessionId);
|
|
96
|
+
const agent = state.handle.agent;
|
|
97
|
+
const queue = new AsyncQueue();
|
|
98
|
+
state.sink = (e) => queue.push(e);
|
|
99
|
+
const onAbort = () => agent.cancel({ kind: "user" });
|
|
100
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
101
|
+
const from = agent.session.snapshotEvents().length;
|
|
102
|
+
const seen = hasRecordedInput(agent.session.snapshotEvents(), input.id);
|
|
103
|
+
agent.followup(seen ? userMessage(continuationId(input.id), [{ type: "text", text: CONTINUE_NOTE }]) : userMessage(input.id, await this.content(input)));
|
|
104
|
+
const idle = agent.whenIdle().then(() => queue.end(), (err) => queue.fail(err));
|
|
105
|
+
try {
|
|
106
|
+
yield* queue;
|
|
107
|
+
await idle;
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
state.sink = undefined;
|
|
111
|
+
signal.removeEventListener("abort", onAbort);
|
|
112
|
+
}
|
|
113
|
+
if (signal.aborted)
|
|
114
|
+
return;
|
|
115
|
+
const end = agent.session.snapshotEvents().slice(from).findLast((e) => e.type === "turn/end");
|
|
116
|
+
const reason = end?.data?.reason;
|
|
117
|
+
if (!reason)
|
|
118
|
+
throw new Error("dsh turn ended without a turn/end event");
|
|
119
|
+
const limits = this.port?.limits(state.init.model);
|
|
120
|
+
const stop = stopForTurnEnd(reason, { maxOutputTokens: limits && requestMaxTokens(limits), streamIdleMs: this.streamIdleMs });
|
|
121
|
+
if (stop)
|
|
122
|
+
throw stop;
|
|
123
|
+
}
|
|
124
|
+
/** Text-only input only: the host queues an input with images as the next turn instead. */
|
|
125
|
+
appendInput(sessionId, input) {
|
|
126
|
+
if (input.images?.length)
|
|
127
|
+
throw new Error("appendInput does not take images");
|
|
128
|
+
this.get(sessionId).handle.agent.followup(userMessage(input.id, [{ type: "text", text: input.text }]));
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Whether this build can take image input: the attachment service decodes
|
|
132
|
+
* images with sharp (a native module), which a single-file build may lack.
|
|
133
|
+
* Probed with a 1x1 PNG (content-addressed, so it is stored once).
|
|
134
|
+
*/
|
|
135
|
+
async probeImageInput() {
|
|
136
|
+
try {
|
|
137
|
+
await this.ctx.attachments.admitPromptContent([{ type: "image", mediaType: "image/png", data: PROBE_PNG }]);
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/** The user message content; images are validated, normalized and stored by the attachment service first. */
|
|
145
|
+
async content(input) {
|
|
146
|
+
if (!input.images?.length)
|
|
147
|
+
return [{ type: "text", text: input.text }];
|
|
148
|
+
const parts = await this.ctx.attachments.admitPromptContent([
|
|
149
|
+
...(input.text ? [{ type: "text", text: input.text }] : []),
|
|
150
|
+
...input.images.map((i) => ({ type: "image", mediaType: i.mediaType, data: i.data, ...(i.name ? { name: i.name } : {}) })),
|
|
151
|
+
]);
|
|
152
|
+
return parts;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The conversation so far as text, oldest first: user inputs and the
|
|
156
|
+
* thinking / message events a turn produced (for an ACP session/load
|
|
157
|
+
* replay). Continuation notes of re-delivered inputs are left out.
|
|
158
|
+
*/
|
|
159
|
+
history(sessionId) {
|
|
160
|
+
const out = [];
|
|
161
|
+
for (const event of this.get(sessionId).handle.agent.session.snapshotEvents()) {
|
|
162
|
+
if (event.type === "user/message") {
|
|
163
|
+
const message = event.data;
|
|
164
|
+
if (String(message.id).includes(":continue:"))
|
|
165
|
+
continue;
|
|
166
|
+
const text = message.content.filter((b) => b.type === "text").map((b) => b.text).join("");
|
|
167
|
+
if (text)
|
|
168
|
+
out.push({ role: "user", type: "message", text });
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
for (const e of harnessEventsFromSessionEvent(event))
|
|
172
|
+
if (e.type !== "delta")
|
|
173
|
+
out.push({ role: "assistant", type: e.type, text: e.text });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
async checkpoint(sessionId) {
|
|
179
|
+
await this.ctx.sessions.flush(this.get(sessionId).handle.agent.session);
|
|
180
|
+
}
|
|
181
|
+
async close(sessionId) {
|
|
182
|
+
const state = this.sessions.get(sessionId);
|
|
183
|
+
if (!state)
|
|
184
|
+
return;
|
|
185
|
+
this.sessions.delete(sessionId);
|
|
186
|
+
this.toolImages.forget(sessionId);
|
|
187
|
+
try {
|
|
188
|
+
await this.ctx.sessions.flush(state.handle.agent.session);
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
await state.handle.dispose();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async dispose() {
|
|
195
|
+
for (const id of [...this.sessions.keys()])
|
|
196
|
+
await this.close(id);
|
|
197
|
+
await this.ctx.fiber.dispose();
|
|
198
|
+
}
|
|
199
|
+
onSessionEvent(state, event) {
|
|
200
|
+
if (state.sink)
|
|
201
|
+
for (const e of harnessEventsFromSessionEvent(event))
|
|
202
|
+
state.sink(e);
|
|
203
|
+
}
|
|
204
|
+
onStreamFrame(state, frame) {
|
|
205
|
+
const e = state.sink ? harnessEventFromStreamFrame(frame) : undefined;
|
|
206
|
+
if (e)
|
|
207
|
+
state.sink(e);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function userMessage(id, content) {
|
|
211
|
+
return freezeMessage({ id: MessageId(id), role: "user", source: { kind: "user" }, content });
|
|
212
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ToolResult } from "./spi.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Deterministic: the same tool call asks the same platform interaction from
|
|
4
|
+
* any process, so an approval survives a host restart and a re-ask is
|
|
5
|
+
* idempotent on the platform. 24 hex chars of sha256(session NUL turn NUL call).
|
|
6
|
+
* The turn is part of it because some providers reuse call ids such as
|
|
7
|
+
* "call_0" in every turn; the turn id survives a restart, so the id stays stable.
|
|
8
|
+
*/
|
|
9
|
+
export declare function approvalId(sessionId: string, turnId: string, callId: string): string;
|
|
10
|
+
/** interaction.request for a permission, shaped as the executor protocol's InteractionRequestParams. */
|
|
11
|
+
export interface PermissionRequest {
|
|
12
|
+
interaction_id: string;
|
|
13
|
+
kind: "permission";
|
|
14
|
+
tool_use_id: string;
|
|
15
|
+
tool_name: string;
|
|
16
|
+
tool_input: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
export declare function permissionRequest(call: {
|
|
19
|
+
sessionId: string;
|
|
20
|
+
turnId: string;
|
|
21
|
+
callId: string;
|
|
22
|
+
toolUseId: string;
|
|
23
|
+
tool: string;
|
|
24
|
+
input: Record<string, unknown>;
|
|
25
|
+
}): PermissionRequest;
|
|
26
|
+
export interface PermissionAnswer {
|
|
27
|
+
outcome: string;
|
|
28
|
+
deny_message?: string;
|
|
29
|
+
}
|
|
30
|
+
/** The refusal the model reads when the answer is not "allow"; undefined when the call may run. */
|
|
31
|
+
export declare function refusalFor(tool: string, answer: PermissionAnswer): ToolResult | undefined;
|
package/lib/approval.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Approval proxy: the transport-independent half of "ask before a tool call".
|
|
2
|
+
// The wait itself (interaction.request / response, withdrawal on cancel,
|
|
3
|
+
// staying open when the host stops) belongs to the host's channel.
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
/**
|
|
6
|
+
* Deterministic: the same tool call asks the same platform interaction from
|
|
7
|
+
* any process, so an approval survives a host restart and a re-ask is
|
|
8
|
+
* idempotent on the platform. 24 hex chars of sha256(session NUL turn NUL call).
|
|
9
|
+
* The turn is part of it because some providers reuse call ids such as
|
|
10
|
+
* "call_0" in every turn; the turn id survives a restart, so the id stays stable.
|
|
11
|
+
*/
|
|
12
|
+
export function approvalId(sessionId, turnId, callId) {
|
|
13
|
+
return `appr_${createHash("sha256").update(`${sessionId}\0${turnId}\0${callId}`).digest("hex").slice(0, 24)}`;
|
|
14
|
+
}
|
|
15
|
+
export function permissionRequest(call) {
|
|
16
|
+
return { interaction_id: approvalId(call.sessionId, call.turnId, call.callId), kind: "permission", tool_use_id: call.toolUseId, tool_name: call.tool, tool_input: call.input };
|
|
17
|
+
}
|
|
18
|
+
/** The refusal the model reads when the answer is not "allow"; undefined when the call may run. */
|
|
19
|
+
export function refusalFor(tool, answer) {
|
|
20
|
+
if (answer.outcome === "allow")
|
|
21
|
+
return undefined;
|
|
22
|
+
const why = answer.deny_message ? `: ${answer.deny_message}` : "";
|
|
23
|
+
return { text: `The user did not approve this ${tool} call (${answer.outcome}${why}).`, isError: true };
|
|
24
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Push-to-pull bridge: producers push, one consumer iterates until end() or fail(). */
|
|
2
|
+
export declare class AsyncQueue<T> implements AsyncIterable<T> {
|
|
3
|
+
private readonly items;
|
|
4
|
+
private waiter?;
|
|
5
|
+
private closed;
|
|
6
|
+
private error?;
|
|
7
|
+
push(item: T): void;
|
|
8
|
+
end(): void;
|
|
9
|
+
fail(error: unknown): void;
|
|
10
|
+
private wake;
|
|
11
|
+
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
12
|
+
}
|