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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 sergaph
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,76 @@
1
- # Temporary Holding Version
1
+ # Monitai
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ CLI framework for building **ops dashboards** with Preact islands, server-only data sources, SSE live updates, and server actions — on native `node:http` + esbuild (no Vite/Hono).
4
+
5
+ ## Requirements
6
+
7
+ - Node.js **20+**
8
+ - Peer: `preact` ^10
9
+
10
+ ## Quick start
11
+
12
+ ```bash
13
+ npm create # or: mkdir my-dash && cd my-dash
14
+ npx monitai init # scaffold
15
+ # or, developing against a local clone:
16
+ npx monitai init-dev
17
+
18
+ npm install
19
+ cp .env.example .env # optional
20
+ npx monitai dev
21
+ ```
22
+
23
+ Production:
24
+
25
+ ```bash
26
+ npx monitai build
27
+ npx monitai start
28
+ ```
29
+
30
+ ## Island DX (v0.4)
31
+
32
+ ```
33
+ islands/<name>/<name>.view.tsx # UI only
34
+ islands/<name>/<name>.server.ts # defineSource (optional)
35
+ islands/<name>/<name>.actions.ts # defineAction (optional)
36
+ islands/<name>/<name>.types.ts # shared types for the view
37
+ ```
38
+
39
+ ```tsx
40
+ import { createPage, island, refSource } from 'monitai'
41
+
42
+ const Weather = island('weather', weatherView)
43
+ const moscow = refSource(weatherServer, {
44
+ city: 'Moscow',
45
+ lat: 55.75,
46
+ lon: 37.62,
47
+ apiKey: process.env.WEATHER_API_KEY,
48
+ })
49
+
50
+ export default createPage(
51
+ () => (
52
+ <main>
53
+ <Weather source={moscow} />
54
+ </main>
55
+ ),
56
+ { title: 'Dashboard', actions: [/* defineAction descriptors */] },
57
+ )
58
+ ```
59
+
60
+ - Secrets stay on the server (`publicProps` whitelist / `SourceRef.toJSON`).
61
+ - Client bundle cannot import `*.server.ts` / `*.actions.ts`.
62
+ - Actions: `POST /_monitai/actions/:id` (trust-network in 0.4.x; auth planned for 0.5).
63
+
64
+ ## CLI
65
+
66
+ | Command | Description |
67
+ |--------|-------------|
68
+ | `monitai init` | Scaffold a new app |
69
+ | `monitai init-dev` | Scaffold + `file:` dependency on local monitai |
70
+ | `monitai dev` | Dev server (esbuild watch + Tailwind + SSE reload) |
71
+ | `monitai build` | Production bundles in `dist/` |
72
+ | `monitai start` | Run `dist/server.mjs` |
73
+
74
+ ## License
75
+
76
+ MIT © sergaph <simasle@yandex.com>
@@ -0,0 +1,43 @@
1
+ // src/internal/actions-registry.ts
2
+ function getActionsGlobal() {
3
+ const g = globalThis;
4
+ if (!g.__monitai_actions) {
5
+ g.__monitai_actions = { byId: /* @__PURE__ */ new Map() };
6
+ }
7
+ return g.__monitai_actions;
8
+ }
9
+ function clearActions() {
10
+ getActionsGlobal().byId.clear();
11
+ }
12
+ function registerActions(actions) {
13
+ const map = getActionsGlobal().byId;
14
+ map.clear();
15
+ if (!actions?.length) return;
16
+ const seen = /* @__PURE__ */ new Set();
17
+ for (const action of actions) {
18
+ if (seen.has(action.id) || map.has(action.id)) {
19
+ console.warn(
20
+ `[monitai] duplicate action id "${action.id}" \u2014 last registration wins`
21
+ );
22
+ }
23
+ seen.add(action.id);
24
+ map.set(action.id, {
25
+ ...action,
26
+ maxBodyBytes: action.maxBodyBytes ?? 65536,
27
+ lastCalledAt: 0
28
+ });
29
+ }
30
+ }
31
+ function getAction(id) {
32
+ return getActionsGlobal().byId.get(id);
33
+ }
34
+ function touchActionCalled(id) {
35
+ const a = getActionsGlobal().byId.get(id);
36
+ if (a) a.lastCalledAt = Date.now();
37
+ }
38
+ export {
39
+ clearActions,
40
+ getAction,
41
+ registerActions,
42
+ touchActionCalled
43
+ };
@@ -0,0 +1,169 @@
1
+ // src/hooks.ts
2
+ import { useEffect, useState } from "preact/hooks";
3
+
4
+ // src/internal/sse-client.ts
5
+ function getSingleton() {
6
+ const w = window;
7
+ if (!w.__monitai_sse) {
8
+ w.__monitai_sse = {
9
+ es: null,
10
+ sourceListeners: /* @__PURE__ */ new Map(),
11
+ summaryListeners: /* @__PURE__ */ new Set(),
12
+ lastSources: /* @__PURE__ */ new Map(),
13
+ lastSummary: null
14
+ };
15
+ }
16
+ return w.__monitai_sse;
17
+ }
18
+ function ensureConnected() {
19
+ const s = getSingleton();
20
+ if (s.es) return s;
21
+ const es = new EventSource("/_monitai/stream");
22
+ s.es = es;
23
+ es.addEventListener("source:updated", (ev) => {
24
+ try {
25
+ const event = JSON.parse(ev.data);
26
+ s.lastSources.set(event.source.id, event.source);
27
+ const listeners = s.sourceListeners.get(event.source.id);
28
+ if (listeners) {
29
+ for (const l of listeners) l(event.source);
30
+ }
31
+ } catch {
32
+ }
33
+ });
34
+ es.addEventListener("summary:updated", (ev) => {
35
+ try {
36
+ const event = JSON.parse(ev.data);
37
+ s.lastSummary = event.summary;
38
+ for (const l of s.summaryListeners) l(event.summary);
39
+ } catch {
40
+ }
41
+ });
42
+ es.addEventListener("reload", () => {
43
+ window.location.reload();
44
+ });
45
+ es.onerror = () => {
46
+ };
47
+ return s;
48
+ }
49
+ function subscribeSource(sourceId, listener) {
50
+ if (typeof window === "undefined") return () => {
51
+ };
52
+ const s = ensureConnected();
53
+ let set = s.sourceListeners.get(sourceId);
54
+ if (!set) {
55
+ set = /* @__PURE__ */ new Set();
56
+ s.sourceListeners.set(sourceId, set);
57
+ }
58
+ set.add(listener);
59
+ const cached = s.lastSources.get(sourceId);
60
+ if (cached) listener(cached);
61
+ return () => {
62
+ set.delete(listener);
63
+ };
64
+ }
65
+ function subscribeSummary(listener) {
66
+ if (typeof window === "undefined") return () => {
67
+ };
68
+ const s = ensureConnected();
69
+ s.summaryListeners.add(listener);
70
+ if (s.lastSummary) listener(s.lastSummary);
71
+ return () => {
72
+ s.summaryListeners.delete(listener);
73
+ };
74
+ }
75
+
76
+ // src/hooks.ts
77
+ function isSourceRefLike(value) {
78
+ return !!value && typeof value === "object" && value.__isSourceRef === true && typeof value.id === "string";
79
+ }
80
+ function useSource(source, initialData) {
81
+ const sourceId = isSourceRefLike(source) ? source.id : source;
82
+ const seed = isSourceRefLike(source) ? source.initialData ?? null : initialData ?? null;
83
+ const [entry, setEntry] = useState({
84
+ data: seed,
85
+ status: seed != null ? "ready" : "loading",
86
+ updatedAt: null,
87
+ isPaused: false
88
+ });
89
+ useEffect(() => {
90
+ return subscribeSource(sourceId, (next) => {
91
+ setEntry({
92
+ data: next.data ?? null,
93
+ status: next.status,
94
+ error: next.error,
95
+ updatedAt: next.updatedAt,
96
+ isPaused: next.isPaused ?? false
97
+ });
98
+ });
99
+ }, [sourceId]);
100
+ return entry;
101
+ }
102
+ function useSummary(initialSummary) {
103
+ const [summary, setSummary] = useState(
104
+ initialSummary ?? null
105
+ );
106
+ const [isLoading, setIsLoading] = useState(false);
107
+ useEffect(() => {
108
+ return subscribeSummary((next) => {
109
+ setSummary(next);
110
+ setIsLoading(false);
111
+ });
112
+ }, []);
113
+ const refresh = async () => {
114
+ setIsLoading(true);
115
+ try {
116
+ const res = await fetch("/_monitai/summary/refresh", { method: "POST" });
117
+ if (res.ok) {
118
+ const json = await res.json();
119
+ if (json.summary) setSummary(json.summary);
120
+ }
121
+ } finally {
122
+ setIsLoading(false);
123
+ }
124
+ };
125
+ return { summary, isLoading, refresh };
126
+ }
127
+ function useAction(actionId) {
128
+ const [isPending, setIsPending] = useState(false);
129
+ const [error, setError] = useState(null);
130
+ const [data, setData] = useState(null);
131
+ const execute = async (input) => {
132
+ setIsPending(true);
133
+ setError(null);
134
+ try {
135
+ const res = await fetch(
136
+ `/_monitai/actions/${encodeURIComponent(actionId)}`,
137
+ {
138
+ method: "POST",
139
+ headers: { "Content-Type": "application/json" },
140
+ body: JSON.stringify(input ?? null)
141
+ }
142
+ );
143
+ const json = await res.json().catch(() => ({}));
144
+ if (!res.ok) {
145
+ const msg = json.error || `Action failed (${res.status})`;
146
+ setError(msg);
147
+ setData(null);
148
+ return null;
149
+ }
150
+ const result = json.data ?? null;
151
+ setData(result);
152
+ return result;
153
+ } catch (err) {
154
+ const msg = err instanceof Error ? err.message : String(err);
155
+ setError(msg);
156
+ setData(null);
157
+ return null;
158
+ } finally {
159
+ setIsPending(false);
160
+ }
161
+ };
162
+ return { execute, isPending, error, data };
163
+ }
164
+
165
+ export {
166
+ useSource,
167
+ useSummary,
168
+ useAction
169
+ };