@unieai/rabi-client-hmr 0.0.0-stage → 0.2.1-rc.3
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 +21 -0
- package/README.md +7 -2
- package/lib/client.js +315 -0
- package/lib/index.js +172 -0
- package/lib/types/client/index.d.ts +14 -0
- package/lib/types/events.d.ts +44 -0
- package/lib/types/index.d.ts +21 -0
- package/locale/en.json +5 -0
- package/locale/zh.json +6 -0
- package/package.json +45 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DeepSeek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @unieai/rabi-client-hmr
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Web client graph synchronization and rebuilt-bundle reload transport
|
|
4
|
+
|
|
5
|
+
Part of Rabi, the agent harness UnieAI Code runs on. Installed as a dependency of `@unieai/rabi`;
|
|
6
|
+
not meant to be used on its own.
|
|
7
|
+
|
|
8
|
+
See LICENSE for the license and copyright of this package.
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: "@unieai/rabi-client-hmr",
|
|
3
|
+
factory: (require) => {
|
|
4
|
+
var module = { exports: {} };
|
|
5
|
+
var exports = module.exports;
|
|
6
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
7
|
+
//#region ../../../node_modules/.pnpm/eventsource-parser@3.1.0/node_modules/eventsource-parser/dist/index.js
|
|
8
|
+
var ParseError = class extends Error {
|
|
9
|
+
constructor(message, options) {
|
|
10
|
+
super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const LF = 10, CR = 13, SPACE = 32;
|
|
14
|
+
function noop(_arg) {}
|
|
15
|
+
function createParser(config) {
|
|
16
|
+
if (typeof config == "function") throw new TypeError("`config` must be an object, got a function instead. Did you mean `createParser({onEvent: fn})`?");
|
|
17
|
+
const { onEvent = noop, onError = noop, onRetry = noop, onComment, maxBufferSize } = config, pendingFragments = [];
|
|
18
|
+
let pendingFragmentsLength = 0, isFirstChunk = !0, id, data = "", dataLines = 0, eventType, terminated = !1;
|
|
19
|
+
function feed(chunk) {
|
|
20
|
+
if (terminated) throw new Error("Cannot feed parser: it was terminated after exceeding the configured max buffer size. Call `reset()` to resume parsing.");
|
|
21
|
+
if (isFirstChunk && (isFirstChunk = !1, chunk.charCodeAt(0) === 239 && chunk.charCodeAt(1) === 187 && chunk.charCodeAt(2) === 191 && (chunk = chunk.slice(3))), pendingFragments.length === 0) {
|
|
22
|
+
const trailing2 = processLines(chunk);
|
|
23
|
+
trailing2 !== "" && (pendingFragments.push(trailing2), pendingFragmentsLength = trailing2.length), checkBufferSize();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (chunk.indexOf(`
|
|
27
|
+
`) === -1 && chunk.indexOf("\r") === -1) {
|
|
28
|
+
pendingFragments.push(chunk), pendingFragmentsLength += chunk.length, checkBufferSize();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
pendingFragments.push(chunk);
|
|
32
|
+
const input = pendingFragments.join("");
|
|
33
|
+
pendingFragments.length = 0, pendingFragmentsLength = 0;
|
|
34
|
+
const trailing = processLines(input);
|
|
35
|
+
trailing !== "" && (pendingFragments.push(trailing), pendingFragmentsLength = trailing.length), checkBufferSize();
|
|
36
|
+
}
|
|
37
|
+
function checkBufferSize() {
|
|
38
|
+
maxBufferSize !== void 0 && (pendingFragmentsLength + data.length <= maxBufferSize || (terminated = !0, pendingFragments.length = 0, pendingFragmentsLength = 0, id = void 0, data = "", dataLines = 0, eventType = void 0, onError(new ParseError(`Buffered data exceeded max buffer size of ${maxBufferSize} characters`, { type: "max-buffer-size-exceeded" }))));
|
|
39
|
+
}
|
|
40
|
+
function processLines(chunk) {
|
|
41
|
+
let searchIndex = 0;
|
|
42
|
+
if (chunk.indexOf("\r") === -1) {
|
|
43
|
+
let lfIndex = chunk.indexOf(`
|
|
44
|
+
`, searchIndex);
|
|
45
|
+
for (; lfIndex !== -1;) {
|
|
46
|
+
if (searchIndex === lfIndex) {
|
|
47
|
+
dataLines > 0 && onEvent({
|
|
48
|
+
id,
|
|
49
|
+
event: eventType,
|
|
50
|
+
data
|
|
51
|
+
}), id = void 0, data = "", dataLines = 0, eventType = void 0, searchIndex = lfIndex + 1, lfIndex = chunk.indexOf(`
|
|
52
|
+
`, searchIndex);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const firstCharCode = chunk.charCodeAt(searchIndex);
|
|
56
|
+
if (isDataPrefix(chunk, searchIndex, firstCharCode)) {
|
|
57
|
+
const valueStart = chunk.charCodeAt(searchIndex + 5) === SPACE ? searchIndex + 6 : searchIndex + 5, value = chunk.slice(valueStart, lfIndex);
|
|
58
|
+
if (dataLines === 0 && chunk.charCodeAt(lfIndex + 1) === LF) {
|
|
59
|
+
onEvent({
|
|
60
|
+
id,
|
|
61
|
+
event: eventType,
|
|
62
|
+
data: value
|
|
63
|
+
}), id = void 0, data = "", eventType = void 0, searchIndex = lfIndex + 2, lfIndex = chunk.indexOf(`
|
|
64
|
+
`, searchIndex);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
data = dataLines === 0 ? value : `${data}
|
|
68
|
+
${value}`, dataLines++;
|
|
69
|
+
} else isEventPrefix(chunk, searchIndex, firstCharCode) ? eventType = chunk.slice(chunk.charCodeAt(searchIndex + 6) === SPACE ? searchIndex + 7 : searchIndex + 6, lfIndex) || void 0 : parseLine(chunk, searchIndex, lfIndex);
|
|
70
|
+
searchIndex = lfIndex + 1, lfIndex = chunk.indexOf(`
|
|
71
|
+
`, searchIndex);
|
|
72
|
+
}
|
|
73
|
+
return chunk.slice(searchIndex);
|
|
74
|
+
}
|
|
75
|
+
for (; searchIndex < chunk.length;) {
|
|
76
|
+
const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
|
|
77
|
+
`, searchIndex);
|
|
78
|
+
let lineEnd = -1;
|
|
79
|
+
if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = crIndex < lfIndex ? crIndex : lfIndex : crIndex !== -1 ? crIndex === chunk.length - 1 ? lineEnd = -1 : lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1) break;
|
|
80
|
+
parseLine(chunk, searchIndex, lineEnd), searchIndex = lineEnd + 1, chunk.charCodeAt(searchIndex - 1) === CR && chunk.charCodeAt(searchIndex) === LF && searchIndex++;
|
|
81
|
+
}
|
|
82
|
+
return chunk.slice(searchIndex);
|
|
83
|
+
}
|
|
84
|
+
function parseLine(chunk, start, end) {
|
|
85
|
+
if (start === end) {
|
|
86
|
+
dispatchEvent();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const firstCharCode = chunk.charCodeAt(start);
|
|
90
|
+
if (isDataPrefix(chunk, start, firstCharCode)) {
|
|
91
|
+
const valueStart = chunk.charCodeAt(start + 5) === SPACE ? start + 6 : start + 5, value2 = chunk.slice(valueStart, end);
|
|
92
|
+
data = dataLines === 0 ? value2 : `${data}
|
|
93
|
+
${value2}`, dataLines++;
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (isEventPrefix(chunk, start, firstCharCode)) {
|
|
97
|
+
eventType = chunk.slice(chunk.charCodeAt(start + 6) === SPACE ? start + 7 : start + 6, end) || void 0;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (firstCharCode === 105 && chunk.charCodeAt(start + 1) === 100 && chunk.charCodeAt(start + 2) === 58) {
|
|
101
|
+
const value2 = chunk.slice(chunk.charCodeAt(start + 3) === SPACE ? start + 4 : start + 3, end);
|
|
102
|
+
id = value2.includes("\0") ? void 0 : value2;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (firstCharCode === 58) {
|
|
106
|
+
if (onComment) onComment(chunk.slice(start, end).slice(chunk.charCodeAt(start + 1) === SPACE ? 2 : 1));
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const line = chunk.slice(start, end), fieldSeparatorIndex = line.indexOf(":");
|
|
110
|
+
if (fieldSeparatorIndex === -1) {
|
|
111
|
+
processField(line, "", line);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const field = line.slice(0, fieldSeparatorIndex), offset = line.charCodeAt(fieldSeparatorIndex + 1) === SPACE ? 2 : 1;
|
|
115
|
+
processField(field, line.slice(fieldSeparatorIndex + offset), line);
|
|
116
|
+
}
|
|
117
|
+
function processField(field, value, line) {
|
|
118
|
+
switch (field) {
|
|
119
|
+
case "event":
|
|
120
|
+
eventType = value || void 0;
|
|
121
|
+
break;
|
|
122
|
+
case "data":
|
|
123
|
+
data = dataLines === 0 ? value : `${data}
|
|
124
|
+
${value}`, dataLines++;
|
|
125
|
+
break;
|
|
126
|
+
case "id":
|
|
127
|
+
id = value.includes("\0") ? void 0 : value;
|
|
128
|
+
break;
|
|
129
|
+
case "retry":
|
|
130
|
+
/^\d+$/.test(value) ? onRetry(parseInt(value, 10)) : onError(new ParseError(`Invalid \`retry\` value: "${value}"`, {
|
|
131
|
+
type: "invalid-retry",
|
|
132
|
+
value,
|
|
133
|
+
line
|
|
134
|
+
}));
|
|
135
|
+
break;
|
|
136
|
+
default:
|
|
137
|
+
onError(new ParseError(`Unknown field "${field.length > 20 ? `${field.slice(0, 20)}\u2026` : field}"`, {
|
|
138
|
+
type: "unknown-field",
|
|
139
|
+
field,
|
|
140
|
+
value,
|
|
141
|
+
line
|
|
142
|
+
}));
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function dispatchEvent() {
|
|
147
|
+
dataLines > 0 && onEvent({
|
|
148
|
+
id,
|
|
149
|
+
event: eventType,
|
|
150
|
+
data
|
|
151
|
+
}), id = void 0, data = "", dataLines = 0, eventType = void 0;
|
|
152
|
+
}
|
|
153
|
+
function reset(options = {}) {
|
|
154
|
+
if (options.consume && pendingFragments.length > 0) {
|
|
155
|
+
const incompleteLine = pendingFragments.join("");
|
|
156
|
+
parseLine(incompleteLine, 0, incompleteLine.length);
|
|
157
|
+
}
|
|
158
|
+
isFirstChunk = !0, id = void 0, data = "", dataLines = 0, eventType = void 0, pendingFragments.length = 0, pendingFragmentsLength = 0, terminated = !1;
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
feed,
|
|
162
|
+
reset
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function isDataPrefix(chunk, i, firstCharCode) {
|
|
166
|
+
return firstCharCode === 100 && chunk.charCodeAt(i + 1) === 97 && chunk.charCodeAt(i + 2) === 116 && chunk.charCodeAt(i + 3) === 97 && chunk.charCodeAt(i + 4) === 58;
|
|
167
|
+
}
|
|
168
|
+
function isEventPrefix(chunk, i, firstCharCode) {
|
|
169
|
+
return firstCharCode === 101 && chunk.charCodeAt(i + 1) === 118 && chunk.charCodeAt(i + 2) === 101 && chunk.charCodeAt(i + 3) === 110 && chunk.charCodeAt(i + 4) === 116 && chunk.charCodeAt(i + 5) === 58;
|
|
170
|
+
}
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region ../../../node_modules/.pnpm/eventsource-parser@3.1.0/node_modules/eventsource-parser/dist/stream.js
|
|
173
|
+
var EventSourceParserStream = class extends TransformStream {
|
|
174
|
+
constructor({ onError, onRetry, onComment, maxBufferSize } = {}) {
|
|
175
|
+
let parser;
|
|
176
|
+
super({
|
|
177
|
+
start(controller) {
|
|
178
|
+
parser = createParser({
|
|
179
|
+
onEvent: (event) => {
|
|
180
|
+
controller.enqueue(event);
|
|
181
|
+
},
|
|
182
|
+
onError(error) {
|
|
183
|
+
typeof onError == "function" && onError(error), (onError === "terminate" || error.type === "max-buffer-size-exceeded") && controller.error(error);
|
|
184
|
+
},
|
|
185
|
+
onRetry,
|
|
186
|
+
onComment,
|
|
187
|
+
maxBufferSize
|
|
188
|
+
});
|
|
189
|
+
},
|
|
190
|
+
transform(chunk) {
|
|
191
|
+
parser.feed(chunk);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region lib/types/events.js
|
|
198
|
+
/**
|
|
199
|
+
* Wire protocol of the `/plugins/events` SSE channel — single source for
|
|
200
|
+
* both halves of this package. Frames still cross a wire boundary: the
|
|
201
|
+
* browser half validates them at its JSON parse point; sharing the type keeps
|
|
202
|
+
* the two ends from drifting, not from parsing.
|
|
203
|
+
*/
|
|
204
|
+
/**
|
|
205
|
+
* Validate the frame envelope; the module controller parses the complete graph before updating its index.
|
|
206
|
+
* @param value - Parsed JSON value from the EventSource message.
|
|
207
|
+
* @returns the known frame, an unknown-type marker, or an invalid marker.
|
|
208
|
+
*/
|
|
209
|
+
function parsePluginsEventFrame(value) {
|
|
210
|
+
if (typeof value !== "object" || value === null) return { kind: "invalid" };
|
|
211
|
+
const record = value;
|
|
212
|
+
switch (record.type) {
|
|
213
|
+
case "rebuilt": return typeof record.id === "string" && typeof record.rev === "string" ? {
|
|
214
|
+
kind: "frame",
|
|
215
|
+
frame: {
|
|
216
|
+
type: "rebuilt",
|
|
217
|
+
id: record.id,
|
|
218
|
+
rev: record.rev
|
|
219
|
+
}
|
|
220
|
+
} : { kind: "invalid" };
|
|
221
|
+
case "graph": return typeof record.graph === "object" && record.graph !== null ? {
|
|
222
|
+
kind: "frame",
|
|
223
|
+
frame: {
|
|
224
|
+
type: "graph",
|
|
225
|
+
graph: record.graph
|
|
226
|
+
}
|
|
227
|
+
} : { kind: "invalid" };
|
|
228
|
+
default: return typeof record.type === "string" ? { kind: "unknown" } : { kind: "invalid" };
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Document-relative form of {@link EVENTS_ENDPOINT} used by the browser half.
|
|
233
|
+
* See .agents/notes/implemented/architecture/2026-09-14-web-document-relative-app-routes.md.
|
|
234
|
+
*/
|
|
235
|
+
const EVENTS_ROUTE = "/plugins/events".slice(1);
|
|
236
|
+
//#endregion
|
|
237
|
+
//#region lib/types/client/index.js
|
|
238
|
+
/** Cordis plugin name. */
|
|
239
|
+
const name = "client-hmr";
|
|
240
|
+
/** Required service: the client module system whose entry controller handles received frames. */
|
|
241
|
+
const inject = ["modules"];
|
|
242
|
+
/**
|
|
243
|
+
* Read the document-relative events route through a shell-owned transport until it closes or `signal` aborts.
|
|
244
|
+
* @param fetch - The shell's request carrier.
|
|
245
|
+
* @param signal - Aborts the stream when the plugin disposes.
|
|
246
|
+
* @param receive - Handles one SSE `data` payload.
|
|
247
|
+
* @throws Error when the Host refuses the stream or the stream ends while the plugin is active.
|
|
248
|
+
*/
|
|
249
|
+
async function readPageTransportEvents(fetch, signal, receive) {
|
|
250
|
+
const response = await fetch(EVENTS_ROUTE, {
|
|
251
|
+
method: "GET",
|
|
252
|
+
headers: { accept: "text/event-stream" },
|
|
253
|
+
signal
|
|
254
|
+
});
|
|
255
|
+
if (!response.ok || response.body === null) throw new Error(`client-hmr: ${EVENTS_ROUTE} answered ${String(response.status)}`);
|
|
256
|
+
const reader = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).getReader();
|
|
257
|
+
for (;;) {
|
|
258
|
+
const { done, value } = await reader.read();
|
|
259
|
+
if (done) break;
|
|
260
|
+
receive(value.data);
|
|
261
|
+
}
|
|
262
|
+
if (!signal.aborted) throw new Error(`client-hmr: ${EVENTS_ROUTE} closed; client graph changes stop until the page reloads`);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Forward graph snapshots and rebuilds to the page's shared serial controller. The event stream arrives through the
|
|
266
|
+
* shell transport when the page has one, otherwise through the browser `EventSource`.
|
|
267
|
+
* @param ctx - Plugin context with the client module system.
|
|
268
|
+
*/
|
|
269
|
+
function apply(ctx) {
|
|
270
|
+
const entries = ctx.modules.entries;
|
|
271
|
+
const handle = (frame) => {
|
|
272
|
+
(frame.type === "graph" ? Promise.resolve().then(() => entries.sync(frame.graph)) : entries.reload(frame.id, frame.rev)).catch((error) => {
|
|
273
|
+
ctx.logger.error(error);
|
|
274
|
+
});
|
|
275
|
+
};
|
|
276
|
+
const receive = (data) => {
|
|
277
|
+
let value;
|
|
278
|
+
try {
|
|
279
|
+
value = JSON.parse(data);
|
|
280
|
+
} catch {
|
|
281
|
+
ctx.logger.warn(`client-hmr: unparseable event frame: ${data}`);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const parsed = parsePluginsEventFrame(value);
|
|
285
|
+
if (parsed.kind === "invalid") ctx.logger.warn(`client-hmr: invalid event frame: ${data}`);
|
|
286
|
+
else if (parsed.kind === "frame") handle(parsed.frame);
|
|
287
|
+
};
|
|
288
|
+
ctx.effect(() => {
|
|
289
|
+
const transport = globalThis.__RABI_TRANSPORT__;
|
|
290
|
+
if (transport?.fetch !== void 0) {
|
|
291
|
+
const controller = new AbortController();
|
|
292
|
+
readPageTransportEvents(transport.fetch, controller.signal, receive).catch((error) => {
|
|
293
|
+
if (!controller.signal.aborted) ctx.logger.error(error);
|
|
294
|
+
});
|
|
295
|
+
return () => {
|
|
296
|
+
controller.abort();
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
const source = new EventSource(EVENTS_ROUTE);
|
|
300
|
+
source.addEventListener("message", (event) => {
|
|
301
|
+
receive(event.data);
|
|
302
|
+
});
|
|
303
|
+
return () => {
|
|
304
|
+
source.close();
|
|
305
|
+
};
|
|
306
|
+
}, "client-hmr: event source");
|
|
307
|
+
}
|
|
308
|
+
//#endregion
|
|
309
|
+
exports.apply = apply;
|
|
310
|
+
exports.inject = inject;
|
|
311
|
+
exports.name = name;
|
|
312
|
+
return module.exports;
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { statSync } from "node:fs";
|
|
2
|
+
import z from "@unieai/schemastery";
|
|
3
|
+
//#region lib/types/events.js
|
|
4
|
+
/** System SSE endpoint pushing graph/rebuilt frames (wire protocol constant). */
|
|
5
|
+
const EVENTS_ENDPOINT = "/plugins/events";
|
|
6
|
+
EVENTS_ENDPOINT.slice(1);
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region lib/types/index.js
|
|
9
|
+
/**
|
|
10
|
+
* Host transport for Web client graph changes and rebuilt bundles. One interval
|
|
11
|
+
* stat-polls every graph row's client bundle (polling by design: network mounts
|
|
12
|
+
* deliver no inotify events), reports changes through
|
|
13
|
+
* `clientModules.rebuilt(id)`, and serves the `/plugins/events` SSE channel
|
|
14
|
+
* broadcasting graph/rebuilt frames to the browser half (src/client/).
|
|
15
|
+
* The Web composition mounts this transport for live graph updates;
|
|
16
|
+
* a development rebuild watcher also supplies bundle changes.
|
|
17
|
+
*/
|
|
18
|
+
/** Cordis plugin name. */
|
|
19
|
+
const name = "client-hmr";
|
|
20
|
+
/** Required services: the client graph and Web route registry. */
|
|
21
|
+
const inject = ["clientModules", "webServer"];
|
|
22
|
+
const Config = z.object({ pollIntervalMs: z.number().step(1).min(1).default(500) });
|
|
23
|
+
/** Serialize one frame as an SSE data line. */
|
|
24
|
+
function sseData(frame) {
|
|
25
|
+
return `data: ${JSON.stringify(frame)}\n\n`;
|
|
26
|
+
}
|
|
27
|
+
/** Snapshot the executable bundle metadata that drives reloads. */
|
|
28
|
+
function bundleStat(path) {
|
|
29
|
+
const bundle = statSync(path);
|
|
30
|
+
return {
|
|
31
|
+
mtimeMs: bundle.mtimeMs,
|
|
32
|
+
ctimeMs: bundle.ctimeMs,
|
|
33
|
+
size: bundle.size
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** Whether the executable bundle metadata is unchanged since its last publication. */
|
|
37
|
+
function sameBundleStat(left, right) {
|
|
38
|
+
return left.mtimeMs === right.mtimeMs && left.ctimeMs === right.ctimeMs && left.size === right.size;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Mount bundle watches and graph/rebuilt SSE delivery.
|
|
42
|
+
* @param ctx - host plugin context carrying clientModules and webServer.
|
|
43
|
+
* @param config - validated {@link Config}.
|
|
44
|
+
*/
|
|
45
|
+
function apply(ctx, config) {
|
|
46
|
+
const pollIntervalMs = config.pollIntervalMs;
|
|
47
|
+
const watched = /* @__PURE__ */ new Map();
|
|
48
|
+
const publish = (id, watch, current) => {
|
|
49
|
+
try {
|
|
50
|
+
ctx.clientModules.rebuilt(id);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (error.code === "ENOENT") {
|
|
53
|
+
watch.dirty = true;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
ctx.logger.warn(error);
|
|
57
|
+
}
|
|
58
|
+
watch.mtimeMs = current.mtimeMs;
|
|
59
|
+
watch.ctimeMs = current.ctimeMs;
|
|
60
|
+
watch.size = current.size;
|
|
61
|
+
watch.dirty = false;
|
|
62
|
+
};
|
|
63
|
+
const watchRow = (id, baseline) => {
|
|
64
|
+
const watch = {
|
|
65
|
+
...baseline,
|
|
66
|
+
dirty: false
|
|
67
|
+
};
|
|
68
|
+
watched.set(id, watch);
|
|
69
|
+
let current;
|
|
70
|
+
try {
|
|
71
|
+
current = bundleStat(baseline.path);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
watch.dirty = true;
|
|
74
|
+
if (error.code !== "ENOENT") ctx.logger.warn(error);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (!sameBundleStat(current, watch)) publish(id, watch, current);
|
|
78
|
+
};
|
|
79
|
+
const pollWatches = () => {
|
|
80
|
+
for (const [id, watch] of watched) {
|
|
81
|
+
let current;
|
|
82
|
+
try {
|
|
83
|
+
current = bundleStat(watch.path);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
watch.dirty = true;
|
|
86
|
+
if (error.code !== "ENOENT") ctx.logger.warn(error);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (!watch.dirty && sameBundleStat(current, watch)) continue;
|
|
90
|
+
publish(id, watch, current);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const syncWatches = () => {
|
|
94
|
+
const rows = /* @__PURE__ */ new Map();
|
|
95
|
+
for (const row of ctx.clientModules.graph().entries) {
|
|
96
|
+
const watch = ctx.clientModules.artifactBaseline(row.id);
|
|
97
|
+
if (watch !== void 0) rows.set(row.id, watch);
|
|
98
|
+
}
|
|
99
|
+
for (const [id, watch] of watched) {
|
|
100
|
+
if (rows.get(id)?.path === watch.path) continue;
|
|
101
|
+
watched.delete(id);
|
|
102
|
+
}
|
|
103
|
+
for (const [id, watch] of rows) if (!watched.has(id)) watchRow(id, watch);
|
|
104
|
+
};
|
|
105
|
+
ctx.effect(() => {
|
|
106
|
+
syncWatches();
|
|
107
|
+
const unsubscribe = ctx.clientModules.onGraphChanged(syncWatches);
|
|
108
|
+
const timer = setInterval(pollWatches, pollIntervalMs);
|
|
109
|
+
timer.unref();
|
|
110
|
+
return () => {
|
|
111
|
+
unsubscribe();
|
|
112
|
+
clearInterval(timer);
|
|
113
|
+
watched.clear();
|
|
114
|
+
};
|
|
115
|
+
}, "client-hmr: bundle watches");
|
|
116
|
+
const connections = /* @__PURE__ */ new Set();
|
|
117
|
+
const publishGraph = () => {
|
|
118
|
+
const line = sseData({
|
|
119
|
+
type: "graph",
|
|
120
|
+
graph: ctx.clientModules.graph()
|
|
121
|
+
});
|
|
122
|
+
for (const res of connections) res.write(line);
|
|
123
|
+
};
|
|
124
|
+
const connect = (res) => {
|
|
125
|
+
res.writeHead(200, {
|
|
126
|
+
"content-type": "text/event-stream",
|
|
127
|
+
"cache-control": "no-cache",
|
|
128
|
+
"connection": "keep-alive"
|
|
129
|
+
});
|
|
130
|
+
res.write(": connected\n\n");
|
|
131
|
+
connections.add(res);
|
|
132
|
+
res.write(sseData({
|
|
133
|
+
type: "graph",
|
|
134
|
+
graph: ctx.clientModules.graph()
|
|
135
|
+
}));
|
|
136
|
+
res.on("close", () => {
|
|
137
|
+
connections.delete(res);
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
ctx.effect(() => {
|
|
141
|
+
const disposeRoute = ctx.webServer.register({
|
|
142
|
+
kind: "exact",
|
|
143
|
+
path: EVENTS_ENDPOINT,
|
|
144
|
+
handler: (req, res) => {
|
|
145
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
146
|
+
res.writeHead(405);
|
|
147
|
+
res.end();
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
connect(res);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
const unsubscribeGraph = ctx.clientModules.onGraphChanged(publishGraph);
|
|
154
|
+
const unsubscribe = ctx.clientModules.onRebuilt((id, rev) => {
|
|
155
|
+
const line = sseData({
|
|
156
|
+
type: "rebuilt",
|
|
157
|
+
id,
|
|
158
|
+
rev
|
|
159
|
+
});
|
|
160
|
+
for (const res of connections) res.write(line);
|
|
161
|
+
});
|
|
162
|
+
return () => {
|
|
163
|
+
unsubscribeGraph();
|
|
164
|
+
unsubscribe();
|
|
165
|
+
disposeRoute();
|
|
166
|
+
for (const res of connections) res.destroy();
|
|
167
|
+
connections.clear();
|
|
168
|
+
};
|
|
169
|
+
}, "client-hmr: /plugins/events channel");
|
|
170
|
+
}
|
|
171
|
+
//#endregion
|
|
172
|
+
export { Config, EVENTS_ENDPOINT, apply, inject, name };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Web SSE transport for page-owned client entry reconciliation and rebuilt code replacement. */
|
|
2
|
+
import type { Context } from '@unieai/cordis';
|
|
3
|
+
export type { PluginsEventFrame } from '../events.ts';
|
|
4
|
+
/** Cordis plugin name. */
|
|
5
|
+
export declare const name = "client-hmr";
|
|
6
|
+
/** Required service: the client module system whose entry controller handles received frames. */
|
|
7
|
+
export declare const inject: string[];
|
|
8
|
+
/**
|
|
9
|
+
* Forward graph snapshots and rebuilds to the page's shared serial controller. The event stream arrives through the
|
|
10
|
+
* shell transport when the page has one, otherwise through the browser `EventSource`.
|
|
11
|
+
* @param ctx - Plugin context with the client module system.
|
|
12
|
+
*/
|
|
13
|
+
export declare function apply(ctx: Context): void;
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire protocol of the `/plugins/events` SSE channel — single source for
|
|
3
|
+
* both halves of this package. Frames still cross a wire boundary: the
|
|
4
|
+
* browser half validates them at its JSON parse point; sharing the type keeps
|
|
5
|
+
* the two ends from drifting, not from parsing.
|
|
6
|
+
*/
|
|
7
|
+
import type { WebBootGraph } from '@unieai/rabi-client-modules';
|
|
8
|
+
/** One SSE frame: the settled graph on connection or change, or one rebuilt bundle notice. */
|
|
9
|
+
export type PluginsEventFrame = {
|
|
10
|
+
type: 'graph';
|
|
11
|
+
graph: WebBootGraph;
|
|
12
|
+
} | {
|
|
13
|
+
type: 'rebuilt';
|
|
14
|
+
id: string;
|
|
15
|
+
rev: string;
|
|
16
|
+
};
|
|
17
|
+
/** Browser wire-parse result: known frame, forward-compatible unknown type, or malformed payload. */
|
|
18
|
+
export type PluginsEventParseResult = {
|
|
19
|
+
kind: 'frame';
|
|
20
|
+
frame: {
|
|
21
|
+
type: 'graph';
|
|
22
|
+
graph: unknown;
|
|
23
|
+
} | Extract<PluginsEventFrame, {
|
|
24
|
+
type: 'rebuilt';
|
|
25
|
+
}>;
|
|
26
|
+
} | {
|
|
27
|
+
kind: 'unknown';
|
|
28
|
+
} | {
|
|
29
|
+
kind: 'invalid';
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Validate the frame envelope; the module controller parses the complete graph before updating its index.
|
|
33
|
+
* @param value - Parsed JSON value from the EventSource message.
|
|
34
|
+
* @returns the known frame, an unknown-type marker, or an invalid marker.
|
|
35
|
+
*/
|
|
36
|
+
export declare function parsePluginsEventFrame(value: unknown): PluginsEventParseResult;
|
|
37
|
+
/** System SSE endpoint pushing graph/rebuilt frames (wire protocol constant). */
|
|
38
|
+
export declare const EVENTS_ENDPOINT = "/plugins/events";
|
|
39
|
+
/**
|
|
40
|
+
* Document-relative form of {@link EVENTS_ENDPOINT} used by the browser half.
|
|
41
|
+
* See .agents/notes/implemented/architecture/2026-09-14-web-document-relative-app-routes.md.
|
|
42
|
+
*/
|
|
43
|
+
export declare const EVENTS_ROUTE: string;
|
|
44
|
+
//# sourceMappingURL=events.d.ts.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Context } from '@unieai/cordis';
|
|
2
|
+
import z from '@unieai/schemastery';
|
|
3
|
+
export type { PluginsEventFrame } from './events.ts';
|
|
4
|
+
export { EVENTS_ENDPOINT } from './events.ts';
|
|
5
|
+
/** Cordis plugin name. */
|
|
6
|
+
export declare const name = "client-hmr";
|
|
7
|
+
/** Required services: the client graph and Web route registry. */
|
|
8
|
+
export declare const inject: string[];
|
|
9
|
+
/** Plugin config, validated by the same-named schemastery schema. */
|
|
10
|
+
export interface Config {
|
|
11
|
+
/** Bundle stat-poll interval in milliseconds (default 500, the build-side watcher's polling default). */
|
|
12
|
+
pollIntervalMs?: number;
|
|
13
|
+
}
|
|
14
|
+
export declare const Config: z<Config>;
|
|
15
|
+
/**
|
|
16
|
+
* Mount bundle watches and graph/rebuilt SSE delivery.
|
|
17
|
+
* @param ctx - host plugin context carrying clientModules and webServer.
|
|
18
|
+
* @param config - validated {@link Config}.
|
|
19
|
+
*/
|
|
20
|
+
export declare function apply(ctx: Context, config: Config): void;
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unieai/rabi-client-hmr",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "Web client graph synchronization and rebuilt-bundle reload transport",
|
|
4
|
+
"version": "0.2.1-rc.3",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "lib/index.js",
|
|
10
|
+
"types": "lib/types/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./lib/types/index.d.ts",
|
|
14
|
+
"default": "./lib/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./client": {
|
|
17
|
+
"types": "./lib/types/client/index.d.ts",
|
|
18
|
+
"default": "./lib/client.js"
|
|
19
|
+
},
|
|
20
|
+
"./src/*": "./src/*",
|
|
21
|
+
"./package.json": "./package.json",
|
|
22
|
+
"./locale/*.json": "./locale/*.json"
|
|
23
|
+
},
|
|
24
|
+
"rabi": {
|
|
25
|
+
"client": {
|
|
26
|
+
"inject": [
|
|
27
|
+
"@unieai/rabi-client-modules"
|
|
28
|
+
],
|
|
29
|
+
"platform": "web",
|
|
30
|
+
"immediately": true
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@unieai/schemastery": "3.18.5"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@unieai/cordis": "4.0.5"
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"locale/*.json",
|
|
42
|
+
"lib/index.js",
|
|
43
|
+
"lib/client.js",
|
|
44
|
+
"lib/types/**/*.d.ts",
|
|
45
|
+
"README.md"
|
|
46
|
+
]
|
|
47
|
+
}
|