dsh-mobile-ios 0.2.6 → 0.2.8

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.
@@ -1,99 +1,99 @@
1
- // adapter/mux-host.mjs — HTTP 探测式宿主适配器(X2 考古后降级为诊断用途)
2
- // 纪律(ADR-12):这一层是唯一允许出现宿主 API 的地方。
3
- //
4
- // X2 结论(2026-11-16,见 RUNBOOK §10):宿主不存在 `/api/remote.mux` HTTP 端点
5
- // (asar 内 mux 命中全是 tmux-context);宿主的「mux」是 Gateway 的
6
- // RemoteStreamMuxServer——Typert RPC 挂在 webServer 的 WebSocket `/api` 上。
7
- // 进程内路径由 cordis-host.mjs 承担;本适配器仅保留 probe 诊断能力,
8
- // 任何业务方法都显式抛 4021,绝不假装成功——
9
- // 假成功比连不上更糟:它会让客户端在错误的前提下通过验收。
10
-
11
- import { fail } from '../protocol.mjs';
12
-
13
- const DEFAULT_BASE = 'http://127.0.0.1:3000';
14
-
15
- export function createMuxHost({ baseUrl = DEFAULT_BASE, token, log = () => {}, timeoutMs = 2500 } = {}) {
16
- const root = baseUrl.replace(/\/+$/, '');
17
- let cachedInfo = null;
18
- let lastProbe = null;
19
-
20
- const authHeaders = token ? { authorization: `Bearer ${token}` } : {};
21
-
22
- async function probe() {
23
- const startedAt = Date.now();
24
- const url = `${root}/api/remote.mux`;
25
- try {
26
- const ctrl = new AbortController();
27
- const timer = setTimeout(() => ctrl.abort(), timeoutMs);
28
- const res = await fetch(url, { method: 'GET', headers: authHeaders, signal: ctrl.signal });
29
- clearTimeout(timer);
30
- const text = await res.text();
31
- lastProbe = {
32
- ok: res.ok,
33
- status: res.status,
34
- ms: Date.now() - startedAt,
35
- url,
36
- bodyPreview: text.slice(0, 200),
37
- };
38
- if (res.ok) {
39
- try {
40
- const body = JSON.parse(text);
41
- cachedInfo = normalizeInfo(body);
42
- } catch {
43
- /* 返回非 JSON 也接受:说明端点存在,但报文格式未确认 */
44
- }
45
- }
46
- return lastProbe;
47
- } catch (err) {
48
- lastProbe = { ok: false, status: 0, ms: Date.now() - startedAt, url, error: err.message };
49
- return lastProbe;
50
- }
51
- }
52
-
53
- function normalizeInfo(body = {}) {
54
- return {
55
- hostId: body.hostId || 'host_unknown',
56
- hostName: body.hostName || body.name || 'DSH 宿主',
57
- hostVersion: body.hostVersion || body.version || 'unknown',
58
- platform: body.platform || process.platform,
59
- protocolVersion: '1.0',
60
- caps: Array.isArray(body.caps) ? body.caps : [],
61
- maxConcurrentTasks: body.maxConcurrentTasks ?? 3,
62
- };
63
- }
64
-
65
- const notMapped = (method) =>
66
- fail(4021, `方法 ${method} 尚未与 /api/remote.mux 对齐(X2 未闭环)`, {
67
- detail: lastProbe ? `probe status=${lastProbe.status}` : '尚未探测',
68
- });
69
-
70
- const host = {
71
- kind: 'mux',
72
- info() {
73
- if (!cachedInfo) {
74
- throw fail(4021, '宿主信息尚未取得:/api/remote.mux 不可达或格式未确认', {
75
- detail: lastProbe ? `status=${lastProbe.status} url=${lastProbe.url}` : '尚未探测',
76
- });
77
- }
78
- return cachedInfo;
79
- },
80
- probe,
81
- lastProbe: () => lastProbe,
82
- listTasks: async () => notMapped('tasks.list'),
83
- getTask: async () => notMapped('tasks.get'),
84
- listArtifacts: async () => notMapped('artifacts.list'),
85
- createTask: async () => notMapped('tasks.create'),
86
- appendMessage: async () => notMapped('tasks.message'),
87
- cancelTask: async () => notMapped('tasks.cancel'),
88
- answerTodo: async () => notMapped('todos.answer'),
89
- diag: async () => ({ adapter: 'mux', probe: lastProbe }),
90
- subscribe(fn) {
91
- // 真实宿主的事件流要挂 mux 的推送通道;未接通前不产生任何事件
92
- log('mux subscribe:事件通道未接通(等 X2 方法表)');
93
- return () => fn;
94
- },
95
- async close() {},
96
- };
97
-
98
- return host;
99
- }
1
+ // adapter/mux-host.mjs — HTTP 探测式宿主适配器(X2 考古后降级为诊断用途)
2
+ // 纪律(ADR-12):这一层是唯一允许出现宿主 API 的地方。
3
+ //
4
+ // X2 结论(2026-11-16,见 RUNBOOK §10):宿主不存在 `/api/remote.mux` HTTP 端点
5
+ // (asar 内 mux 命中全是 tmux-context);宿主的「mux」是 Gateway 的
6
+ // RemoteStreamMuxServer——Typert RPC 挂在 webServer 的 WebSocket `/api` 上。
7
+ // 进程内路径由 cordis-host.mjs 承担;本适配器仅保留 probe 诊断能力,
8
+ // 任何业务方法都显式抛 4021,绝不假装成功——
9
+ // 假成功比连不上更糟:它会让客户端在错误的前提下通过验收。
10
+
11
+ import { fail } from '../protocol.mjs';
12
+
13
+ const DEFAULT_BASE = 'http://127.0.0.1:3000';
14
+
15
+ export function createMuxHost({ baseUrl = DEFAULT_BASE, token, log = () => {}, timeoutMs = 2500 } = {}) {
16
+ const root = baseUrl.replace(/\/+$/, '');
17
+ let cachedInfo = null;
18
+ let lastProbe = null;
19
+
20
+ const authHeaders = token ? { authorization: `Bearer ${token}` } : {};
21
+
22
+ async function probe() {
23
+ const startedAt = Date.now();
24
+ const url = `${root}/api/remote.mux`;
25
+ try {
26
+ const ctrl = new AbortController();
27
+ const timer = setTimeout(() => ctrl.abort(), timeoutMs);
28
+ const res = await fetch(url, { method: 'GET', headers: authHeaders, signal: ctrl.signal });
29
+ clearTimeout(timer);
30
+ const text = await res.text();
31
+ lastProbe = {
32
+ ok: res.ok,
33
+ status: res.status,
34
+ ms: Date.now() - startedAt,
35
+ url,
36
+ bodyPreview: text.slice(0, 200),
37
+ };
38
+ if (res.ok) {
39
+ try {
40
+ const body = JSON.parse(text);
41
+ cachedInfo = normalizeInfo(body);
42
+ } catch {
43
+ /* 返回非 JSON 也接受:说明端点存在,但报文格式未确认 */
44
+ }
45
+ }
46
+ return lastProbe;
47
+ } catch (err) {
48
+ lastProbe = { ok: false, status: 0, ms: Date.now() - startedAt, url, error: err.message };
49
+ return lastProbe;
50
+ }
51
+ }
52
+
53
+ function normalizeInfo(body = {}) {
54
+ return {
55
+ hostId: body.hostId || 'host_unknown',
56
+ hostName: body.hostName || body.name || 'DSH 宿主',
57
+ hostVersion: body.hostVersion || body.version || 'unknown',
58
+ platform: body.platform || process.platform,
59
+ protocolVersion: '1.0',
60
+ caps: Array.isArray(body.caps) ? body.caps : [],
61
+ maxConcurrentTasks: body.maxConcurrentTasks ?? 3,
62
+ };
63
+ }
64
+
65
+ const notMapped = (method) =>
66
+ fail(4021, `方法 ${method} 尚未与 /api/remote.mux 对齐(X2 未闭环)`, {
67
+ detail: lastProbe ? `probe status=${lastProbe.status}` : '尚未探测',
68
+ });
69
+
70
+ const host = {
71
+ kind: 'mux',
72
+ info() {
73
+ if (!cachedInfo) {
74
+ throw fail(4021, '宿主信息尚未取得:/api/remote.mux 不可达或格式未确认', {
75
+ detail: lastProbe ? `status=${lastProbe.status} url=${lastProbe.url}` : '尚未探测',
76
+ });
77
+ }
78
+ return cachedInfo;
79
+ },
80
+ probe,
81
+ lastProbe: () => lastProbe,
82
+ listTasks: async () => notMapped('tasks.list'),
83
+ getTask: async () => notMapped('tasks.get'),
84
+ listArtifacts: async () => notMapped('artifacts.list'),
85
+ createTask: async () => notMapped('tasks.create'),
86
+ appendMessage: async () => notMapped('tasks.message'),
87
+ cancelTask: async () => notMapped('tasks.cancel'),
88
+ answerTodo: async () => notMapped('todos.answer'),
89
+ diag: async () => ({ adapter: 'mux', probe: lastProbe }),
90
+ subscribe(fn) {
91
+ // 真实宿主的事件流要挂 mux 的推送通道;未接通前不产生任何事件
92
+ log('mux subscribe:事件通道未接通(等 X2 方法表)');
93
+ return () => fn;
94
+ },
95
+ async close() {},
96
+ };
97
+
98
+ return host;
99
+ }