@hugesb/dsh-stream-agent 0.0.0-stage → 0.3.2

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 (161) hide show
  1. package/CHANGELOG.md +622 -0
  2. package/CONTRIBUTING.md +72 -0
  3. package/LICENSE +202 -0
  4. package/README.en.md +349 -0
  5. package/README.md +300 -3
  6. package/cordis.patch.yml +77 -0
  7. package/dist/entry/action-gate.js +206 -0
  8. package/dist/entry/adapter-kit.js +192 -0
  9. package/dist/entry/context-render.js +119 -0
  10. package/dist/entry/control-bar-cli.js +289 -0
  11. package/dist/entry/control-bar.js +123 -0
  12. package/dist/entry/control-session.js +115 -0
  13. package/dist/entry/control-state.js +125 -0
  14. package/dist/entry/cordis-compat.js +95 -0
  15. package/dist/entry/eval-probe.js +112 -0
  16. package/dist/entry/intent-log.js +121 -0
  17. package/dist/entry/json-persistence.js +306 -0
  18. package/dist/entry/loop.js +4163 -0
  19. package/dist/entry/mock-provider.js +549 -0
  20. package/dist/entry/obs-broker.js +178 -0
  21. package/dist/entry/ptc-bridge.js +405 -0
  22. package/dist/entry/state-handle.js +217 -0
  23. package/dist/entry/takeover-registry.js +60 -0
  24. package/dist/kernel/control-bar.js +559 -0
  25. package/dist/kernel/final.js +62 -0
  26. package/dist/kernel/gate.js +464 -0
  27. package/dist/kernel/modes.js +253 -0
  28. package/dist/kernel/obs.js +517 -0
  29. package/dist/kernel/parser.js +1081 -0
  30. package/dist/kernel/render.js +466 -0
  31. package/dist/kernel/state.js +416 -0
  32. package/dist/version.js +9 -0
  33. package/docs/TEST-PROPOSITIONS.md +139 -0
  34. package/docs/adr-001-segment-boundary.md +61 -0
  35. package/docs/architecture.md +104 -0
  36. package/docs/desktop-ui-feasibility.md +277 -0
  37. package/docs/disabled-lines-audit.md +53 -0
  38. package/docs/limitations.md +181 -0
  39. package/docs/protocol.md +99 -0
  40. package/docs/v0.4.0-plan.md +50 -0
  41. package/eval/METRICS.md +128 -0
  42. package/eval/RESULTS.md +246 -0
  43. package/eval/tasks/fixtures/big-report.md +298 -0
  44. package/eval/tasks/fixtures/build-manifest.txt +5 -0
  45. package/eval/tasks/fixtures/deploy.yaml +4 -0
  46. package/eval/tasks/fixtures/error.log +200 -0
  47. package/eval/tasks/fixtures/features.yaml +4 -0
  48. package/eval/tasks/fixtures/pipeline.yaml +4 -0
  49. package/eval/tasks/fixtures/sales-q1.csv +61 -0
  50. package/eval/tasks/fixtures/settings.yaml +4 -0
  51. package/eval/tasks/fixtures/users-roster.json +304 -0
  52. package/eval/tasks/fixtures/users-scores.json +20 -0
  53. package/eval/tasks/gen-fixtures.cjs +80 -0
  54. package/eval/tasks/tasks.json +150 -0
  55. package/eval/tools/f6-kill-drill.cjs +176 -0
  56. package/eval/tools/f6-kill-drill.worker.cjs +131 -0
  57. package/eval/tools/gen-results.cjs +191 -0
  58. package/eval/tools/machine-test.cjs +200 -0
  59. package/eval/tools/manual-code-scenarios.md +59 -0
  60. package/eval/tools/replay-check.cjs +311 -0
  61. package/eval/tools/runner.cjs +219 -0
  62. package/eval/tools/session-log.cjs +106 -0
  63. package/eval/tools/summarize.cjs +185 -0
  64. package/eval/tools/t3-cells.cjs +173 -0
  65. package/package.json +73 -6
  66. package/patches/adapters-smoke.yml +41 -0
  67. package/patches/eval-account.yml +36 -0
  68. package/patches/eval-baseline.yml +33 -0
  69. package/patches/eval-stream-code-off.yml +46 -0
  70. package/patches/eval-stream-code-on.yml +47 -0
  71. package/patches/eval-stream.yml +54 -0
  72. package/patches/takeover.yml +42 -0
  73. package/patches/test-preset.yml +23 -0
  74. package/preset/stream/cordis.patch.yml +39 -0
  75. package/preset/stream/package.json +13 -0
  76. package/preset.yml +56 -0
  77. package/profiles/stream/README.md +66 -0
  78. package/profiles/stream/cordis.patch.yml +12 -0
  79. package/profiles/stream/package.json +17 -0
  80. package/profiles/stream-headless/package.json +16 -0
  81. package/scripts/build.mjs +103 -0
  82. package/scripts/check-consistency.mjs +146 -0
  83. package/scripts/gate.mjs +138 -0
  84. package/scripts/install-check.mjs +116 -0
  85. package/scripts/install-cycle.mjs +88 -0
  86. package/scripts/release.mjs +428 -0
  87. package/scripts/repair-session-logs.mjs +498 -0
  88. package/src/entry/action-gate.ts +206 -0
  89. package/src/entry/adapter-kit.ts +192 -0
  90. package/src/entry/context-render.ts +119 -0
  91. package/src/entry/control-bar-cli.ts +289 -0
  92. package/src/entry/control-bar.ts +123 -0
  93. package/src/entry/control-session.ts +115 -0
  94. package/src/entry/control-state.ts +125 -0
  95. package/src/entry/cordis-compat.ts +95 -0
  96. package/src/entry/eval-probe.ts +112 -0
  97. package/src/entry/intent-log.ts +121 -0
  98. package/src/entry/json-persistence.ts +306 -0
  99. package/src/entry/loop.ts +4163 -0
  100. package/src/entry/mock-provider.ts +549 -0
  101. package/src/entry/obs-broker.ts +178 -0
  102. package/src/entry/ptc-bridge.ts +405 -0
  103. package/src/entry/state-handle.ts +217 -0
  104. package/src/entry/takeover-registry.ts +60 -0
  105. package/src/kernel/control-bar.ts +559 -0
  106. package/src/kernel/final.ts +62 -0
  107. package/src/kernel/gate.ts +464 -0
  108. package/src/kernel/modes.ts +253 -0
  109. package/src/kernel/obs.ts +517 -0
  110. package/src/kernel/parser.ts +1081 -0
  111. package/src/kernel/render.ts +466 -0
  112. package/src/kernel/state.ts +416 -0
  113. package/src/version.ts +9 -0
  114. package/test/activation.test.ts +187 -0
  115. package/test/adapters.test.ts +676 -0
  116. package/test/control-bar.test.ts +486 -0
  117. package/test/desktop-ui.test.ts +359 -0
  118. package/test/eval-probe.test.ts +139 -0
  119. package/test/f5-plan.test.ts +221 -0
  120. package/test/f6-intent.test.ts +168 -0
  121. package/test/f7-segment.test.ts +190 -0
  122. package/test/fix-limits.test.ts +599 -0
  123. package/test/gate.test.ts +380 -0
  124. package/test/helpers/stub-loop.ts +240 -0
  125. package/test/host-conformance.test.ts +418 -0
  126. package/test/kernel/obs.test.ts +329 -0
  127. package/test/kernel/parser.test.ts +451 -0
  128. package/test/loop.test.ts +1393 -0
  129. package/test/mock-provider.test.ts +386 -0
  130. package/test/ptc-bridge.test.ts +356 -0
  131. package/test/render.test.ts +418 -0
  132. package/test/s2-install-check.test.ts +46 -0
  133. package/test/scenarios/3-file-basic.json +13 -0
  134. package/test/scenarios/5-file-basic.json +22 -0
  135. package/test/scenarios/burn-more.json +9 -0
  136. package/test/scenarios/degraded-final.json +10 -0
  137. package/test/scenarios/fault-abort-xml.json +11 -0
  138. package/test/scenarios/fault-malformed-command.json +12 -0
  139. package/test/scenarios/fault-midstream-abort.json +11 -0
  140. package/test/scenarios/fault-saturating-output.json +9 -0
  141. package/test/scenarios/fault-timeout.json +9 -0
  142. package/test/scenarios/peek-first.json +11 -0
  143. package/test/scenarios/peek-second.json +11 -0
  144. package/test/scenarios/peek-third.json +10 -0
  145. package/test/scenarios/recover-final.json +10 -0
  146. package/test/scenarios/reroute-first.json +17 -0
  147. package/test/scenarios/reroute-second.json +16 -0
  148. package/test/scenarios/starvation-final.json +10 -0
  149. package/test/scenarios/starvation-promote.json +11 -0
  150. package/test/scenarios/wait-tools-continue.json +10 -0
  151. package/test/scenarios/wait-tools-xml.json +22 -0
  152. package/test/scenarios/wait-tools.json +15 -0
  153. package/test/session-format.test.ts +234 -0
  154. package/test/state.test.ts +335 -0
  155. package/test/stream-v02.test.ts +264 -0
  156. package/test/v031-display.test.ts +333 -0
  157. package/test/v032-conflict.test.ts +191 -0
  158. package/test/v032-truncation.test.ts +291 -0
  159. package/ui/client.js +576 -0
  160. package/ui/index.js +282 -0
  161. package/ui/package.json +26 -0
package/ui/index.js ADDED
@@ -0,0 +1,282 @@
1
+ // ui/index.js —— 桌面控制页的宿主半边(v0.2.1)
2
+ //
3
+ // 为什么是独立子包:dsh-client-modules 以 **包** 为单位扫描 dsh.client,
4
+ // 同一个包被多个 Loader 行引用时会抛 "resolves from multiple active Loader sources"。
5
+ // 主包已经有六个 entry(loop / state / obs / gate / render / control-bar),
6
+ // 所以浏览器半边必须住在自己的包(ui/package.json)里,主包不能声明 dsh.client。
7
+ //
8
+ // 本文件只做三件事:
9
+ // 1. 注册同源路由 /stream-agent/control(GET 读状态,POST 改模式/语言)
10
+ // 2. 复用宿主的 connection.admit 做鉴权(桌面端是 cookie,Web 端是同一套会话)
11
+ // 3. 优先调用 loop 提供的 streamAgentRuntime 服务;服务缺席时降级为只读诊断
12
+ //
13
+ // 刻意不引入任何 npm 依赖:只 import 本包 dist/ 里已有的内核模块。
14
+
15
+ import { readPersistedControl, writePersistedControl } from '../dist/entry/control-state.js';
16
+ import { isModeId, modeConfig, publicModeCatalog } from '../dist/kernel/modes.js';
17
+ import { isLang } from '../dist/kernel/control-bar.js';
18
+ import { VERSION } from '../dist/version.js';
19
+
20
+ export const name = 'stream-agent-ui';
21
+ /** 宿主侧不需要任何服务才能装载;webServer 在 apply 里可选等待。 */
22
+ export const inject = [];
23
+ export const CONTROL_ROUTE = '/stream-agent/control';
24
+ const MAX_BODY_BYTES = 4096;
25
+
26
+ function sendJson(res, status, body) {
27
+ const payload = JSON.stringify(body);
28
+ res.writeHead(status, {
29
+ 'content-type': 'application/json; charset=utf-8',
30
+ 'content-length': Buffer.byteLength(payload),
31
+ 'cache-control': 'no-store',
32
+ });
33
+ res.end(payload);
34
+ }
35
+
36
+ function publicModes() {
37
+ return publicModeCatalog();
38
+ }
39
+
40
+ /** 可读的降级状态:loop 没接管时仍让用户看到文件里的值与 CLI 指引。 */
41
+ function fallbackStatus() {
42
+ const persisted = readPersistedControl();
43
+ const preset = modeConfig(persisted.state.mode);
44
+ return {
45
+ version: VERSION,
46
+ takeover: false,
47
+ degraded: true,
48
+ mode: persisted.state.mode,
49
+ modeSource: persisted.found ? 'file' : 'default',
50
+ modeLocked: isModeId(process.env.STREAM_AGENT_MODE),
51
+ lang: persisted.state.lang,
52
+ langSource: persisted.found ? 'file' : 'default',
53
+ langLocked: isLang(process.env.STREAM_AGENT_LANG),
54
+ effective: {
55
+ selfReadFeedback: preset.selfReadFeedback,
56
+ enableCode: preset.enableCode === 'auto' ? 'auto' : preset.enableCode,
57
+ persistFallback: preset.persistFallback,
58
+ maxParallelToolCalls: preset.maxParallelToolCalls,
59
+ maxReentries: preset.maxReentries,
60
+ budgetTokens: preset.budgetTokens ?? null,
61
+ },
62
+ modes: publicModes(),
63
+ updatedAt: persisted.updatedAt ?? null,
64
+ stateFile: persisted.file,
65
+ };
66
+ }
67
+
68
+ function currentStatus(ctx) {
69
+ const runtime = ctx.get?.('streamAgentRuntime');
70
+ if (runtime !== undefined && typeof runtime.status === 'function') {
71
+ try {
72
+ return runtime.status();
73
+ } catch {
74
+ // 运行时服务坏了不该让页面 500:退回文件视图,保留诊断信息
75
+ return { ...fallbackStatus(), degraded: true, runtimeError: true };
76
+ }
77
+ }
78
+ return fallbackStatus();
79
+ }
80
+
81
+ /**
82
+ * 复用宿主的连接鉴权:桌面端由 preload 注入 cookie,Web 端是同一套会话 cookie。
83
+ * connection 缺席时只允许回环地址(测试/极简宿主)。
84
+ */
85
+ function admitted(ctx, req) {
86
+ const connection = ctx.get?.('connection');
87
+ if (connection !== undefined && typeof connection.admit === 'function') {
88
+ try {
89
+ const admission = connection.admit(req);
90
+ return admission === undefined || admission === null || !('rejection' in admission);
91
+ } catch {
92
+ return false;
93
+ }
94
+ }
95
+ const address = req.socket?.remoteAddress ?? '';
96
+ return address === '127.0.0.1' || address === '::1' || address === '::ffff:127.0.0.1';
97
+ }
98
+
99
+ function readJson(req) {
100
+ return new Promise((resolve, reject) => {
101
+ let size = 0;
102
+ let settled = false;
103
+ const chunks = [];
104
+ req.on('data', (chunk) => {
105
+ if (settled) return;
106
+ size += chunk.length;
107
+ if (size > MAX_BODY_BYTES) {
108
+ settled = true;
109
+ reject(Object.assign(new Error('body too large'), { code: 'too-large' }));
110
+ return;
111
+ }
112
+ chunks.push(chunk);
113
+ });
114
+ req.on('end', () => {
115
+ if (settled) return;
116
+ settled = true;
117
+ try {
118
+ const text = Buffer.concat(chunks).toString('utf8').trim();
119
+ resolve(text === '' ? {} : JSON.parse(text));
120
+ } catch {
121
+ reject(Object.assign(new Error('invalid json'), { code: 'bad-json' }));
122
+ }
123
+ });
124
+ req.on('error', (error) => {
125
+ if (settled) return;
126
+ settled = true;
127
+ reject(error);
128
+ });
129
+ });
130
+ }
131
+
132
+ function sameOriginOnly(req) {
133
+ const site = req.headers?.['sec-fetch-site'];
134
+ return site === undefined || site === 'same-origin' || site === 'none';
135
+ }
136
+
137
+ function expectedMatches(body, status) {
138
+ if (!Object.prototype.hasOwnProperty.call(body, 'expectedUpdatedAt')) return true;
139
+ const expected = body.expectedUpdatedAt;
140
+ if (expected === null) return status.updatedAt === null || status.updatedAt === undefined;
141
+ return expected === status.updatedAt;
142
+ }
143
+
144
+ function conflict(res, status) {
145
+ sendJson(res, 409, {
146
+ ok: false,
147
+ error: { code: 'stale', message: '状态已被其他窗口或 CLI 修改,请刷新后重试' },
148
+ status,
149
+ });
150
+ }
151
+
152
+ /** 路由处理函数(导出供测试直接调用,不经过 http 服务器)。 */
153
+ export async function handleControlRequest(ctx, req, res) {
154
+ if (!admitted(ctx, req)) {
155
+ sendJson(res, 401, { ok: false, error: { code: 'unauthorized', message: 'unauthorized' } });
156
+ return;
157
+ }
158
+ if (!sameOriginOnly(req)) {
159
+ sendJson(res, 403, { ok: false, error: { code: 'origin', message: 'cross-origin request refused' } });
160
+ return;
161
+ }
162
+
163
+ if (req.method === 'GET') {
164
+ sendJson(res, 200, { ok: true, status: currentStatus(ctx) });
165
+ return;
166
+ }
167
+
168
+ if (req.method !== 'POST') {
169
+ res.writeHead(405, { allow: 'GET, POST', 'content-type': 'application/json; charset=utf-8' });
170
+ res.end(JSON.stringify({ ok: false, error: { code: 'method', message: 'GET or POST only' } }));
171
+ return;
172
+ }
173
+
174
+ const contentType = String(req.headers?.['content-type'] ?? '').toLowerCase();
175
+ if (!contentType.includes('application/json')) {
176
+ sendJson(res, 415, { ok: false, error: { code: 'content-type', message: 'application/json required' } });
177
+ return;
178
+ }
179
+
180
+ let body;
181
+ try {
182
+ body = await readJson(req);
183
+ } catch (error) {
184
+ const code = error?.code === 'too-large' ? 'too-large' : 'bad-json';
185
+ sendJson(res, code === 'too-large' ? 413 : 400, {
186
+ ok: false,
187
+ error: { code, message: code === 'too-large' ? 'body too large' : 'invalid json' },
188
+ });
189
+ return;
190
+ }
191
+ if (body === null || typeof body !== 'object' || Array.isArray(body)) {
192
+ sendJson(res, 400, { ok: false, error: { code: 'bad-body', message: 'object body required' } });
193
+ return;
194
+ }
195
+
196
+ const runtime = ctx.get?.('streamAgentRuntime');
197
+ const live = currentStatus(ctx);
198
+ if (!expectedMatches(body, live)) {
199
+ conflict(res, live);
200
+ return;
201
+ }
202
+
203
+ const wantsMode = body.mode !== undefined;
204
+ const wantsLang = body.lang !== undefined;
205
+ if (!wantsMode && !wantsLang) {
206
+ sendJson(res, 400, { ok: false, error: { code: 'empty', message: 'mode or lang required' }, status: live });
207
+ return;
208
+ }
209
+ if (wantsMode && !isModeId(body.mode)) {
210
+ sendJson(res, 400, { ok: false, error: { code: 'invalid-mode', message: `unknown mode: ${String(body.mode)}` }, status: live });
211
+ return;
212
+ }
213
+ if (wantsLang && !isLang(body.lang)) {
214
+ sendJson(res, 400, { ok: false, error: { code: 'invalid-lang', message: `unknown language: ${String(body.lang)}` }, status: live });
215
+ return;
216
+ }
217
+
218
+ // 锁定判断放在路由层:运行时服务即使实现有出入,也不会绕过装配配置/环境变量。
219
+ if ((wantsMode && live.modeLocked === true) || (wantsLang && live.langLocked === true)) {
220
+ sendJson(res, 409, {
221
+ ok: false,
222
+ error: { code: 'locked', message: '当前值由装配配置或环境变量锁定' },
223
+ status: live,
224
+ });
225
+ return;
226
+ }
227
+
228
+ // 运行时在位:走服务,模式立即对新会话生效并落盘。
229
+ if (runtime !== undefined && (typeof runtime.setMode === 'function' || typeof runtime.setLang === 'function')) {
230
+ let result = { ok: true, persisted: true };
231
+ if (wantsMode) result = runtime.setMode(body.mode);
232
+ if (result.ok !== false && wantsLang) result = runtime.setLang(body.lang);
233
+ if (result.ok === false) {
234
+ const statusCode = result.code === 'locked' ? 409 : 400;
235
+ sendJson(res, statusCode, {
236
+ ok: false,
237
+ error: { code: result.code ?? 'refused', message: result.code === 'locked' ? '当前值由装配配置或环境变量锁定' : 'invalid value' },
238
+ status: currentStatus(ctx),
239
+ });
240
+ return;
241
+ }
242
+ sendJson(res, 200, { ok: true, persisted: result.persisted !== false, status: currentStatus(ctx) });
243
+ return;
244
+ }
245
+
246
+ // 降级:loop 没接管(装载失败/headless),只改状态文件,并明确告知需要重启。
247
+ const next = {
248
+ mode: wantsMode ? body.mode : live.mode,
249
+ lang: wantsLang ? body.lang : live.lang,
250
+ };
251
+ try {
252
+ writePersistedControl(next);
253
+ } catch (error) {
254
+ sendJson(res, 500, {
255
+ ok: false,
256
+ error: { code: 'write-failed', message: String(error?.message ?? error) },
257
+ status: live,
258
+ });
259
+ return;
260
+ }
261
+ sendJson(res, 200, {
262
+ ok: true,
263
+ persisted: true,
264
+ degraded: true,
265
+ notice: 'loop 未接管:已写入状态文件,修复装载后重启生效',
266
+ status: currentStatus(ctx),
267
+ });
268
+ }
269
+
270
+ /** Cordis 宿主插件体:webServer 是可选的(headless 组合里没有它)。 */
271
+ export function apply(ctx) {
272
+ ctx.inject(['webServer'], (webCtx) => {
273
+ webCtx.effect(
274
+ () => webCtx.webServer.register({
275
+ kind: 'exact',
276
+ path: CONTROL_ROUTE,
277
+ handler: (req, res) => { void handleControlRequest(ctx, req, res); },
278
+ }),
279
+ 'stream-agent-ui: control route',
280
+ );
281
+ });
282
+ }
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@hugesb/dsh-stream-agent-ui",
3
+ "version": "0.2.1",
4
+ "private": true,
5
+ "type": "module",
6
+ "description": "Desktop control page for @hugesb/dsh-stream-agent: a dsh web-client plugin (main panel + sidebar rail), served by the desktop Host.",
7
+ "main": "./index.js",
8
+ "exports": {
9
+ ".": "./index.js",
10
+ "./client": "./client.js",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "dsh": {
14
+ "client": {
15
+ "platform": "web",
16
+ "inject": [
17
+ "@deepseek-ai/dsh-client-ui-layout",
18
+ "@deepseek-ai/dsh-client-ui-sidebar"
19
+ ],
20
+ "external": [
21
+ "react"
22
+ ]
23
+ }
24
+ },
25
+ "license": "Apache-2.0"
26
+ }