monitai 0.0.0-stage → 0.4.4
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 +75 -2
- package/dist/actions-registry-B5JBDO5F.js +43 -0
- package/dist/chunk-63FHD5ZT.js +169 -0
- package/dist/chunk-JU76LRZA.js +623 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +984 -0
- package/dist/client-Dq7v7zCx.d.ts +91 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.js +26 -0
- package/dist/index.d.ts +163 -0
- package/dist/index.js +98 -0
- package/dist/internal/island-runtime.d.ts +3 -0
- package/dist/internal/island-runtime.js +19 -0
- package/package.json +61 -4
- package/template/.env.example +16 -0
- package/template/_gitignore +8 -0
- package/template/islands/counter/counter.view.tsx +18 -0
- package/template/islands/incident-simulator/incident-simulator.actions.ts +36 -0
- package/template/islands/incident-simulator/incident-simulator.view.tsx +50 -0
- package/template/islands/service-monitor/service-monitor.server.ts +24 -0
- package/template/islands/service-monitor/service-monitor.types.ts +20 -0
- package/template/islands/service-monitor/service-monitor.view.tsx +38 -0
- package/template/islands/summary-banner/summary-banner.view.tsx +51 -0
- package/template/islands/weather/weather.server.ts +66 -0
- package/template/islands/weather/weather.types.ts +14 -0
- package/template/islands/weather/weather.view.tsx +35 -0
- package/template/main.tsx +45 -0
- package/template/package.json +16 -0
- package/template/styles.css +1 -0
- package/template/tsconfig.json +17 -0
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
// src/internal/interval.ts
|
|
2
|
+
function parseInterval(input) {
|
|
3
|
+
if (typeof input === "number") {
|
|
4
|
+
if (!Number.isFinite(input) || input <= 0) {
|
|
5
|
+
throw new Error(`Invalid interval ms: ${input}`);
|
|
6
|
+
}
|
|
7
|
+
return Math.floor(input);
|
|
8
|
+
}
|
|
9
|
+
const trimmed = input.trim();
|
|
10
|
+
const match = /^(\d+(?:\.\d+)?)(ms|s|m|h)$/i.exec(trimmed);
|
|
11
|
+
if (!match) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
`Invalid interval "${input}". Use a number (ms) or strings like "10s", "5m", "1h".`
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
const value = Number(match[1]);
|
|
17
|
+
const unit = match[2].toLowerCase();
|
|
18
|
+
const mult = unit === "ms" ? 1 : unit === "s" ? 1e3 : unit === "m" ? 6e4 : 36e5;
|
|
19
|
+
const ms = Math.floor(value * mult);
|
|
20
|
+
if (ms <= 0) throw new Error(`Invalid interval "${input}"`);
|
|
21
|
+
return ms;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/internal/store.ts
|
|
25
|
+
var MemoryStore = class {
|
|
26
|
+
sources = /* @__PURE__ */ new Map();
|
|
27
|
+
meta = /* @__PURE__ */ new Map();
|
|
28
|
+
summary = null;
|
|
29
|
+
listeners = /* @__PURE__ */ new Set();
|
|
30
|
+
serverStartedAt = Date.now();
|
|
31
|
+
registerSource(config) {
|
|
32
|
+
this.registerSourceWithMeta({
|
|
33
|
+
...config,
|
|
34
|
+
paramsHash: ""
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Returns true when paramsHash changed (or source is new).
|
|
39
|
+
* Idempotent when paramsHash is unchanged. Does not clear isPaused.
|
|
40
|
+
*/
|
|
41
|
+
registerSourceWithMeta(config) {
|
|
42
|
+
const existingMeta = this.meta.get(config.id);
|
|
43
|
+
const existing = this.sources.get(config.id);
|
|
44
|
+
if (existing && existingMeta && existingMeta.paramsHash === config.paramsHash) {
|
|
45
|
+
existingMeta.stale = false;
|
|
46
|
+
existing.description = config.description;
|
|
47
|
+
existing.intervalMs = config.intervalMs;
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
this.meta.set(config.id, { paramsHash: config.paramsHash, stale: false });
|
|
51
|
+
if (existing) {
|
|
52
|
+
existing.description = config.description;
|
|
53
|
+
existing.intervalMs = config.intervalMs;
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
this.sources.set(config.id, {
|
|
57
|
+
id: config.id,
|
|
58
|
+
description: config.description,
|
|
59
|
+
data: null,
|
|
60
|
+
status: "idle",
|
|
61
|
+
updatedAt: null,
|
|
62
|
+
intervalMs: config.intervalMs,
|
|
63
|
+
isPaused: false
|
|
64
|
+
});
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
touchSource(id) {
|
|
68
|
+
const m = this.meta.get(id);
|
|
69
|
+
if (m) m.stale = false;
|
|
70
|
+
}
|
|
71
|
+
markAllStale() {
|
|
72
|
+
for (const m of this.meta.values()) {
|
|
73
|
+
m.stale = true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
removeStaleSources() {
|
|
77
|
+
const removed = [];
|
|
78
|
+
for (const [id, m] of this.meta) {
|
|
79
|
+
if (!m.stale) continue;
|
|
80
|
+
this.sources.delete(id);
|
|
81
|
+
this.meta.delete(id);
|
|
82
|
+
removed.push(id);
|
|
83
|
+
}
|
|
84
|
+
return removed;
|
|
85
|
+
}
|
|
86
|
+
setSourceLoading(id) {
|
|
87
|
+
const entry = this.sources.get(id);
|
|
88
|
+
if (!entry) return;
|
|
89
|
+
entry.status = "loading";
|
|
90
|
+
this.emit({ type: "source:updated", source: { ...entry } });
|
|
91
|
+
}
|
|
92
|
+
setSourceData(id, data) {
|
|
93
|
+
const entry = this.sources.get(id);
|
|
94
|
+
if (!entry) return;
|
|
95
|
+
entry.data = data;
|
|
96
|
+
entry.status = "ready";
|
|
97
|
+
entry.error = void 0;
|
|
98
|
+
entry.updatedAt = Date.now();
|
|
99
|
+
this.emit({ type: "source:updated", source: { ...entry } });
|
|
100
|
+
}
|
|
101
|
+
setSourceError(id, error) {
|
|
102
|
+
const entry = this.sources.get(id);
|
|
103
|
+
if (!entry) return;
|
|
104
|
+
entry.status = "error";
|
|
105
|
+
entry.error = error;
|
|
106
|
+
this.emit({ type: "source:updated", source: { ...entry } });
|
|
107
|
+
}
|
|
108
|
+
setSourcePaused(id, paused) {
|
|
109
|
+
const entry = this.sources.get(id);
|
|
110
|
+
if (!entry) return;
|
|
111
|
+
entry.isPaused = paused;
|
|
112
|
+
this.emit({ type: "source:updated", source: { ...entry } });
|
|
113
|
+
}
|
|
114
|
+
/** HMR-only: clear pause flags so warm-up can restart pollers. */
|
|
115
|
+
resetAllPaused() {
|
|
116
|
+
for (const entry of this.sources.values()) {
|
|
117
|
+
if (!entry.isPaused) continue;
|
|
118
|
+
entry.isPaused = false;
|
|
119
|
+
this.emit({ type: "source:updated", source: { ...entry } });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
getSource(id) {
|
|
123
|
+
const entry = this.sources.get(id);
|
|
124
|
+
return entry;
|
|
125
|
+
}
|
|
126
|
+
getSnapshot() {
|
|
127
|
+
const sources = {};
|
|
128
|
+
for (const [id, entry] of this.sources) {
|
|
129
|
+
sources[id] = { ...entry };
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
sources,
|
|
133
|
+
summary: this.summary ? { ...this.summary } : null,
|
|
134
|
+
serverStartedAt: this.serverStartedAt
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
setSummary(summary) {
|
|
138
|
+
this.summary = summary;
|
|
139
|
+
this.emit({ type: "summary:updated", summary: { ...summary } });
|
|
140
|
+
}
|
|
141
|
+
subscribe(listener) {
|
|
142
|
+
this.listeners.add(listener);
|
|
143
|
+
return () => {
|
|
144
|
+
this.listeners.delete(listener);
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
emit(event) {
|
|
148
|
+
for (const listener of this.listeners) {
|
|
149
|
+
try {
|
|
150
|
+
listener(event);
|
|
151
|
+
} catch {
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// src/internal/poller.ts
|
|
158
|
+
function getPollerGlobal() {
|
|
159
|
+
const g = globalThis;
|
|
160
|
+
if (!g.__monitai_poller) {
|
|
161
|
+
g.__monitai_poller = {
|
|
162
|
+
timers: /* @__PURE__ */ new Map(),
|
|
163
|
+
intervals: /* @__PURE__ */ new Map(),
|
|
164
|
+
fetchers: /* @__PURE__ */ new Map(),
|
|
165
|
+
running: /* @__PURE__ */ new Set()
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
return g.__monitai_poller;
|
|
169
|
+
}
|
|
170
|
+
function resetPoller() {
|
|
171
|
+
const g = getPollerGlobal();
|
|
172
|
+
for (const timer of g.timers.values()) clearTimeout(timer);
|
|
173
|
+
g.timers.clear();
|
|
174
|
+
g.intervals.clear();
|
|
175
|
+
g.fetchers.clear();
|
|
176
|
+
g.running.clear();
|
|
177
|
+
}
|
|
178
|
+
var ServerPoller = class {
|
|
179
|
+
constructor(store) {
|
|
180
|
+
this.store = store;
|
|
181
|
+
}
|
|
182
|
+
store;
|
|
183
|
+
reset() {
|
|
184
|
+
resetPoller();
|
|
185
|
+
}
|
|
186
|
+
unregister(id) {
|
|
187
|
+
const g = getPollerGlobal();
|
|
188
|
+
const prev = g.timers.get(id);
|
|
189
|
+
if (prev) clearTimeout(prev);
|
|
190
|
+
g.timers.delete(id);
|
|
191
|
+
g.intervals.delete(id);
|
|
192
|
+
g.fetchers.delete(id);
|
|
193
|
+
g.running.delete(id);
|
|
194
|
+
}
|
|
195
|
+
pause(sourceId) {
|
|
196
|
+
const g = getPollerGlobal();
|
|
197
|
+
const prev = g.timers.get(sourceId);
|
|
198
|
+
if (prev) clearTimeout(prev);
|
|
199
|
+
g.timers.delete(sourceId);
|
|
200
|
+
this.store.setSourcePaused(sourceId, true);
|
|
201
|
+
}
|
|
202
|
+
resume(sourceId) {
|
|
203
|
+
this.store.setSourcePaused(sourceId, false);
|
|
204
|
+
this.triggerNow(sourceId);
|
|
205
|
+
}
|
|
206
|
+
/** Fire one fetch tick immediately (keeps schedule unless paused). */
|
|
207
|
+
triggerNow(id) {
|
|
208
|
+
const g = getPollerGlobal();
|
|
209
|
+
if (!g.fetchers.has(id)) return;
|
|
210
|
+
if (this.store.getSource(id)?.isPaused) return;
|
|
211
|
+
const intervalMs = g.intervals.get(id) ?? 6e4;
|
|
212
|
+
const prev = g.timers.get(id);
|
|
213
|
+
if (prev) clearTimeout(prev);
|
|
214
|
+
void this.tick(id, intervalMs);
|
|
215
|
+
}
|
|
216
|
+
register(id, intervalMs, fetcher, options) {
|
|
217
|
+
const g = getPollerGlobal();
|
|
218
|
+
const prev = g.timers.get(id);
|
|
219
|
+
if (prev) clearTimeout(prev);
|
|
220
|
+
g.fetchers.set(id, fetcher);
|
|
221
|
+
g.intervals.set(id, intervalMs);
|
|
222
|
+
if (this.store.getSource(id)?.isPaused) {
|
|
223
|
+
g.timers.delete(id);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (options?.immediate !== false) {
|
|
227
|
+
void this.tick(id, intervalMs);
|
|
228
|
+
} else {
|
|
229
|
+
g.timers.set(
|
|
230
|
+
id,
|
|
231
|
+
setTimeout(() => void this.tick(id, intervalMs), intervalMs)
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
async tick(id, intervalMs) {
|
|
236
|
+
const g = getPollerGlobal();
|
|
237
|
+
if (this.store.getSource(id)?.isPaused) {
|
|
238
|
+
g.timers.delete(id);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (g.running.has(id)) {
|
|
242
|
+
g.timers.set(
|
|
243
|
+
id,
|
|
244
|
+
setTimeout(() => void this.tick(id, intervalMs), intervalMs)
|
|
245
|
+
);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
g.running.add(id);
|
|
249
|
+
this.store.setSourceLoading(id);
|
|
250
|
+
try {
|
|
251
|
+
const fn = g.fetchers.get(id);
|
|
252
|
+
if (!fn) return;
|
|
253
|
+
const data = await fn();
|
|
254
|
+
if (this.store.getSource(id)?.isPaused) return;
|
|
255
|
+
this.store.setSourceData(id, data);
|
|
256
|
+
} catch (err) {
|
|
257
|
+
if (this.store.getSource(id)?.isPaused) return;
|
|
258
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
259
|
+
this.store.setSourceError(id, message);
|
|
260
|
+
} finally {
|
|
261
|
+
g.running.delete(id);
|
|
262
|
+
const still = g.fetchers.has(id);
|
|
263
|
+
const paused = this.store.getSource(id)?.isPaused;
|
|
264
|
+
if (still && !paused) {
|
|
265
|
+
const ms = g.intervals.get(id) ?? intervalMs;
|
|
266
|
+
g.timers.set(
|
|
267
|
+
id,
|
|
268
|
+
setTimeout(() => void this.tick(id, ms), ms)
|
|
269
|
+
);
|
|
270
|
+
} else {
|
|
271
|
+
g.timers.delete(id);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
// src/internal/llm-agent.ts
|
|
278
|
+
var DEFAULT_MIN_INTERVAL_MS = 6e4;
|
|
279
|
+
var DEFAULT_LOOP_MS = 12e4;
|
|
280
|
+
function hashSnapshot(snapshot) {
|
|
281
|
+
const payload = Object.values(snapshot.sources).map((s) => ({
|
|
282
|
+
id: s.id,
|
|
283
|
+
updatedAt: s.updatedAt,
|
|
284
|
+
status: s.status,
|
|
285
|
+
data: s.data
|
|
286
|
+
})).sort((a, b) => a.id.localeCompare(b.id));
|
|
287
|
+
const str = JSON.stringify(payload);
|
|
288
|
+
let h2 = 2166136261;
|
|
289
|
+
for (let i = 0; i < str.length; i++) {
|
|
290
|
+
h2 ^= str.charCodeAt(i);
|
|
291
|
+
h2 = Math.imul(h2, 16777619);
|
|
292
|
+
}
|
|
293
|
+
return (h2 >>> 0).toString(16);
|
|
294
|
+
}
|
|
295
|
+
function buildPrompt(snapshot) {
|
|
296
|
+
const lines = Object.values(snapshot.sources).map((s) => {
|
|
297
|
+
return [
|
|
298
|
+
`### ${s.id}`,
|
|
299
|
+
`description: ${s.description}`,
|
|
300
|
+
`status: ${s.status}`,
|
|
301
|
+
`updatedAt: ${s.updatedAt ? new Date(s.updatedAt).toISOString() : "null"}`,
|
|
302
|
+
`error: ${s.error ?? "none"}`,
|
|
303
|
+
`data: ${JSON.stringify(s.data)}`
|
|
304
|
+
].join("\n");
|
|
305
|
+
});
|
|
306
|
+
return [
|
|
307
|
+
"You are a monitoring dashboard analyst. From the source data, write a short summary in English (2\u20134 sentences): what matters right now.",
|
|
308
|
+
"Reply with STRICT JSON only, no markdown:",
|
|
309
|
+
'{"text":"...","severity":"normal"|"warning"|"critical"}',
|
|
310
|
+
"",
|
|
311
|
+
"Sources:",
|
|
312
|
+
lines.join("\n\n") || "(no sources)"
|
|
313
|
+
].join("\n");
|
|
314
|
+
}
|
|
315
|
+
var LlmAgent = class {
|
|
316
|
+
constructor(store) {
|
|
317
|
+
this.store = store;
|
|
318
|
+
this.enabled = Boolean(
|
|
319
|
+
typeof process !== "undefined" && process.env?.MONITAI_LLM_API_KEY
|
|
320
|
+
);
|
|
321
|
+
this.minIntervalMs = Number(
|
|
322
|
+
typeof process !== "undefined" && process.env?.MONITAI_LLM_MIN_INTERVAL_MS || DEFAULT_MIN_INTERVAL_MS
|
|
323
|
+
);
|
|
324
|
+
this.loopMs = Number(
|
|
325
|
+
typeof process !== "undefined" && process.env?.MONITAI_LLM_LOOP_MS || DEFAULT_LOOP_MS
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
store;
|
|
329
|
+
timer = null;
|
|
330
|
+
lastHash = null;
|
|
331
|
+
lastRunAt = 0;
|
|
332
|
+
running = false;
|
|
333
|
+
enabled;
|
|
334
|
+
minIntervalMs;
|
|
335
|
+
loopMs;
|
|
336
|
+
start() {
|
|
337
|
+
if (!this.enabled) return;
|
|
338
|
+
this.schedule();
|
|
339
|
+
}
|
|
340
|
+
stop() {
|
|
341
|
+
if (this.timer) clearTimeout(this.timer);
|
|
342
|
+
this.timer = null;
|
|
343
|
+
}
|
|
344
|
+
async triggerSummary(force = false) {
|
|
345
|
+
if (!this.enabled) return null;
|
|
346
|
+
return this.run(force);
|
|
347
|
+
}
|
|
348
|
+
schedule() {
|
|
349
|
+
if (this.timer) clearTimeout(this.timer);
|
|
350
|
+
this.timer = setTimeout(() => {
|
|
351
|
+
void this.run(false).finally(() => this.schedule());
|
|
352
|
+
}, this.loopMs);
|
|
353
|
+
}
|
|
354
|
+
async run(force) {
|
|
355
|
+
if (this.running) return null;
|
|
356
|
+
const now = Date.now();
|
|
357
|
+
if (!force && now - this.lastRunAt < this.minIntervalMs) return null;
|
|
358
|
+
const snapshot = this.store.getSnapshot();
|
|
359
|
+
const sourceIds = Object.keys(snapshot.sources);
|
|
360
|
+
if (sourceIds.length === 0) return null;
|
|
361
|
+
const hash = hashSnapshot(snapshot);
|
|
362
|
+
if (!force && hash === this.lastHash) return snapshot.summary;
|
|
363
|
+
const anyReady = Object.values(snapshot.sources).some(
|
|
364
|
+
(s) => s.status === "ready" || s.data != null
|
|
365
|
+
);
|
|
366
|
+
if (!anyReady) return null;
|
|
367
|
+
this.running = true;
|
|
368
|
+
this.lastRunAt = now;
|
|
369
|
+
try {
|
|
370
|
+
const summary = await this.callLlm(snapshot);
|
|
371
|
+
this.lastHash = hash;
|
|
372
|
+
this.store.setSummary(summary);
|
|
373
|
+
return summary;
|
|
374
|
+
} catch (err) {
|
|
375
|
+
console.error(
|
|
376
|
+
"[monitai] LLM summary failed:",
|
|
377
|
+
err instanceof Error ? err.message : err
|
|
378
|
+
);
|
|
379
|
+
return null;
|
|
380
|
+
} finally {
|
|
381
|
+
this.running = false;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
async callLlm(snapshot) {
|
|
385
|
+
const apiKey = process.env.MONITAI_LLM_API_KEY;
|
|
386
|
+
const baseUrl = (process.env.MONITAI_LLM_BASE_URL ?? "https://api.openai.com/v1").replace(/\/$/, "");
|
|
387
|
+
const model = process.env.MONITAI_LLM_MODEL ?? "gpt-4o-mini";
|
|
388
|
+
const res = await fetch(`${baseUrl}/chat/completions`, {
|
|
389
|
+
method: "POST",
|
|
390
|
+
headers: {
|
|
391
|
+
"Content-Type": "application/json",
|
|
392
|
+
Authorization: `Bearer ${apiKey}`
|
|
393
|
+
},
|
|
394
|
+
body: JSON.stringify({
|
|
395
|
+
model,
|
|
396
|
+
temperature: 0.2,
|
|
397
|
+
messages: [
|
|
398
|
+
{
|
|
399
|
+
role: "user",
|
|
400
|
+
content: buildPrompt(snapshot)
|
|
401
|
+
}
|
|
402
|
+
]
|
|
403
|
+
})
|
|
404
|
+
});
|
|
405
|
+
if (!res.ok) {
|
|
406
|
+
const body = await res.text();
|
|
407
|
+
throw new Error(`LLM HTTP ${res.status}: ${body.slice(0, 200)}`);
|
|
408
|
+
}
|
|
409
|
+
const json = await res.json();
|
|
410
|
+
const content = json.choices?.[0]?.message?.content?.trim() ?? "";
|
|
411
|
+
const parsed = parseLlmJson(content);
|
|
412
|
+
return {
|
|
413
|
+
text: parsed.text,
|
|
414
|
+
severity: parsed.severity,
|
|
415
|
+
generatedAt: Date.now(),
|
|
416
|
+
basedOnSources: Object.keys(snapshot.sources)
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
function parseLlmJson(content) {
|
|
421
|
+
const fence = content.match(/\{[\s\S]*\}/);
|
|
422
|
+
const raw = fence ? fence[0] : content;
|
|
423
|
+
try {
|
|
424
|
+
const obj = JSON.parse(raw);
|
|
425
|
+
const severity = obj.severity === "warning" || obj.severity === "critical" ? obj.severity : "normal";
|
|
426
|
+
return {
|
|
427
|
+
text: obj.text?.trim() || content,
|
|
428
|
+
severity
|
|
429
|
+
};
|
|
430
|
+
} catch {
|
|
431
|
+
return { text: content || "No data for summary.", severity: "normal" };
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// src/internal/runtime.ts
|
|
436
|
+
var RUNTIME_KEY = "__monitai_runtime__";
|
|
437
|
+
function isServer() {
|
|
438
|
+
return typeof window === "undefined";
|
|
439
|
+
}
|
|
440
|
+
function getRuntime() {
|
|
441
|
+
if (!isServer()) {
|
|
442
|
+
throw new Error("Monitai runtime (store/poller/agent) is server-only");
|
|
443
|
+
}
|
|
444
|
+
const g = globalThis;
|
|
445
|
+
if (!g[RUNTIME_KEY]) {
|
|
446
|
+
const store = new MemoryStore();
|
|
447
|
+
const poller = new ServerPoller(store);
|
|
448
|
+
const agent = new LlmAgent(store);
|
|
449
|
+
g[RUNTIME_KEY] = { store, poller, agent };
|
|
450
|
+
agent.start();
|
|
451
|
+
}
|
|
452
|
+
return g[RUNTIME_KEY];
|
|
453
|
+
}
|
|
454
|
+
function getStore() {
|
|
455
|
+
return getRuntime().store;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// src/ref-source.ts
|
|
459
|
+
function stableHash(value) {
|
|
460
|
+
const str = JSON.stringify(value);
|
|
461
|
+
let h2 = 2166136261;
|
|
462
|
+
for (let i = 0; i < str.length; i++) {
|
|
463
|
+
h2 ^= str.charCodeAt(i);
|
|
464
|
+
h2 = Math.imul(h2, 16777619);
|
|
465
|
+
}
|
|
466
|
+
return (h2 >>> 0).toString(16);
|
|
467
|
+
}
|
|
468
|
+
function pickPublicProps(params, publicProps) {
|
|
469
|
+
if (!publicProps) return {};
|
|
470
|
+
if (typeof publicProps === "function") return publicProps(params);
|
|
471
|
+
const out = {};
|
|
472
|
+
for (const key of publicProps) {
|
|
473
|
+
out[key] = params[key];
|
|
474
|
+
}
|
|
475
|
+
return out;
|
|
476
|
+
}
|
|
477
|
+
function refSource(serverConfig, params) {
|
|
478
|
+
if (typeof window !== "undefined") {
|
|
479
|
+
throw new Error("refSource() is server-only");
|
|
480
|
+
}
|
|
481
|
+
const id = typeof serverConfig.id === "function" ? serverConfig.id(params) : serverConfig.id;
|
|
482
|
+
const description = serverConfig.description == null ? id : typeof serverConfig.description === "function" ? serverConfig.description(params) : serverConfig.description;
|
|
483
|
+
const intervalMs = serverConfig.interval != null ? parseInterval(serverConfig.interval) : 6e4 * 60 * 24;
|
|
484
|
+
const safeParams = pickPublicProps(
|
|
485
|
+
params,
|
|
486
|
+
serverConfig.publicProps
|
|
487
|
+
);
|
|
488
|
+
const { store, poller } = getRuntime();
|
|
489
|
+
const paramsHash = stableHash({
|
|
490
|
+
params,
|
|
491
|
+
interval: serverConfig.interval ?? null,
|
|
492
|
+
description
|
|
493
|
+
});
|
|
494
|
+
const existingData = store.getSource(id)?.data ?? null;
|
|
495
|
+
const changed = store.registerSourceWithMeta({
|
|
496
|
+
id,
|
|
497
|
+
description,
|
|
498
|
+
intervalMs,
|
|
499
|
+
paramsHash
|
|
500
|
+
});
|
|
501
|
+
if (serverConfig.fetcher) {
|
|
502
|
+
const fetcher = () => serverConfig.fetcher(params);
|
|
503
|
+
poller.register(id, intervalMs, fetcher, {
|
|
504
|
+
immediate: existingData == null && !store.getSource(id)?.isPaused
|
|
505
|
+
});
|
|
506
|
+
if (changed && existingData != null && !store.getSource(id)?.isPaused) {
|
|
507
|
+
poller.triggerNow(id);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
const initialData = store.getSource(id)?.data ?? null;
|
|
511
|
+
const ref = {
|
|
512
|
+
__isSourceRef: true,
|
|
513
|
+
id,
|
|
514
|
+
params: safeParams,
|
|
515
|
+
initialData,
|
|
516
|
+
toJSON() {
|
|
517
|
+
return {
|
|
518
|
+
__isSourceRef: true,
|
|
519
|
+
id,
|
|
520
|
+
params: safeParams,
|
|
521
|
+
initialData: store.getSource(id)?.data ?? ref.initialData
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
return ref;
|
|
526
|
+
}
|
|
527
|
+
function isSourceRef(value) {
|
|
528
|
+
return !!value && typeof value === "object" && value.__isSourceRef === true && typeof value.id === "string";
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// src/island.ts
|
|
532
|
+
import { h, Fragment } from "preact";
|
|
533
|
+
var instanceCounter = 0;
|
|
534
|
+
var registeredIslandNames = /* @__PURE__ */ new Set();
|
|
535
|
+
function resetIslandInstanceCounter() {
|
|
536
|
+
instanceCounter = 0;
|
|
537
|
+
}
|
|
538
|
+
function getRegisteredIslandNames() {
|
|
539
|
+
return [...registeredIslandNames];
|
|
540
|
+
}
|
|
541
|
+
function clearRegisteredIslandNames() {
|
|
542
|
+
registeredIslandNames.clear();
|
|
543
|
+
}
|
|
544
|
+
function resolvePropsWithSources(props) {
|
|
545
|
+
let store = null;
|
|
546
|
+
try {
|
|
547
|
+
store = getStore();
|
|
548
|
+
} catch {
|
|
549
|
+
store = null;
|
|
550
|
+
}
|
|
551
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
552
|
+
function walk(value) {
|
|
553
|
+
if (value == null || typeof value !== "object") return value;
|
|
554
|
+
if (isSourceRef(value)) {
|
|
555
|
+
const data = store?.getSource(value.id)?.data ?? value.initialData ?? null;
|
|
556
|
+
return {
|
|
557
|
+
__isSourceRef: true,
|
|
558
|
+
id: value.id,
|
|
559
|
+
params: value.params,
|
|
560
|
+
initialData: data,
|
|
561
|
+
toJSON() {
|
|
562
|
+
return {
|
|
563
|
+
__isSourceRef: true,
|
|
564
|
+
id: value.id,
|
|
565
|
+
params: value.params,
|
|
566
|
+
initialData: data
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
if (seen.has(value)) return value;
|
|
572
|
+
seen.add(value);
|
|
573
|
+
if (Array.isArray(value)) return value.map(walk);
|
|
574
|
+
const out = {};
|
|
575
|
+
for (const [k, v] of Object.entries(value)) {
|
|
576
|
+
out[k] = walk(v);
|
|
577
|
+
}
|
|
578
|
+
return out;
|
|
579
|
+
}
|
|
580
|
+
return walk(props);
|
|
581
|
+
}
|
|
582
|
+
function island(name, View) {
|
|
583
|
+
registeredIslandNames.add(name);
|
|
584
|
+
function IslandComponent(props) {
|
|
585
|
+
if (typeof window !== "undefined") {
|
|
586
|
+
return h(View, props);
|
|
587
|
+
}
|
|
588
|
+
const resolved = resolvePropsWithSources(props);
|
|
589
|
+
const uid = `${name}:${++instanceCounter}`;
|
|
590
|
+
const safeJson = JSON.stringify(resolved).replace(/</g, "\\u003c");
|
|
591
|
+
return h(
|
|
592
|
+
Fragment,
|
|
593
|
+
null,
|
|
594
|
+
h(
|
|
595
|
+
"monitai-island",
|
|
596
|
+
{ "data-name": name, "data-uid": uid },
|
|
597
|
+
h(View, resolved)
|
|
598
|
+
),
|
|
599
|
+
h("script", {
|
|
600
|
+
type: "application/json",
|
|
601
|
+
"data-island-props": uid,
|
|
602
|
+
dangerouslySetInnerHTML: { __html: safeJson }
|
|
603
|
+
})
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
;
|
|
607
|
+
IslandComponent.__isMonitaiIsland = true;
|
|
608
|
+
IslandComponent.displayName = `Island(${name})`;
|
|
609
|
+
return IslandComponent;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
export {
|
|
613
|
+
parseInterval,
|
|
614
|
+
getRuntime,
|
|
615
|
+
getStore,
|
|
616
|
+
refSource,
|
|
617
|
+
isSourceRef,
|
|
618
|
+
resetIslandInstanceCounter,
|
|
619
|
+
getRegisteredIslandNames,
|
|
620
|
+
clearRegisteredIslandNames,
|
|
621
|
+
resolvePropsWithSources,
|
|
622
|
+
island
|
|
623
|
+
};
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|