@luanpdd/kit-mcp 1.35.0 → 1.36.0
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/bin/cli.js +2 -2
- package/bin/mcp.js +6 -6
- package/bin/ui.js +74 -74
- package/gates/ai-prompt-stability.md +120 -120
- package/gates/budget-description.md +68 -68
- package/gates/confidence.md +29 -29
- package/gates/dependency-check.md +33 -33
- package/gates/dept-cycle-prevention.md +179 -179
- package/gates/golden-signals-coverage.md +133 -133
- package/gates/legacy-refactor-safety.md +178 -178
- package/gates/multi-tenant-rls-coverage.md +102 -102
- package/gates/no-personal-uuid.md +72 -72
- package/gates/obs-agents-mcp-supabase.md +86 -86
- package/gates/obs-skills-frontmatter.md +76 -76
- package/gates/observability-coverage.md +151 -151
- package/gates/omm-no-regression.md +83 -83
- package/gates/postmortem-template-required.md +127 -127
- package/gates/prr-checklist-coverage.md +128 -128
- package/gates/regression.md +32 -32
- package/gates/release-pipeline-policy.md +132 -132
- package/gates/secrets-scan.md +33 -33
- package/gates/service-role-not-in-user-facing.md +113 -113
- package/gates/skill-must-include.md +71 -71
- package/gates/sync-idempotent.md +62 -62
- package/gates/verify-phase-goal.md +34 -34
- package/kit/agents/designer-ui.md +216 -216
- package/kit/agents/workflow-generator.md +537 -167
- package/kit/commands/adicionar-backlog.md +1 -1
- package/kit/commands/adicionar-fase.md +1 -1
- package/kit/commands/adicionar-tarefa.md +1 -1
- package/kit/commands/auditar-observabilidade.md +103 -103
- package/kit/commands/auditar-toil.md +129 -129
- package/kit/commands/caracterizar-prompt.md +195 -195
- package/kit/commands/criar-workflow.md +158 -158
- package/kit/commands/definir-perfil.md +1 -1
- package/kit/commands/definir-slo.md +108 -108
- package/kit/commands/fio.md +1 -1
- package/kit/commands/golden-signals.md +142 -142
- package/kit/commands/instrumentar-fase.md +200 -200
- package/kit/commands/investigar-producao.md +162 -162
- package/kit/commands/observabilidade.md +118 -118
- package/kit/commands/postmortem.md +179 -179
- package/kit/commands/prr.md +205 -205
- package/kit/commands/publicar-rapido.md +207 -207
- package/kit/commands/risk-budget.md +220 -220
- package/kit/commands/sre.md +230 -230
- package/kit/file-manifest.json +424 -424
- package/kit/framework/references/output-style.md +22 -22
- package/kit/hooks/post-apply-migration.js +199 -199
- package/kit/hooks/sidecar-tool-publisher.js +210 -210
- package/kit/skills/_shared-dados-distribuidos/glossary.md +224 -224
- package/kit/skills/_shared-legacy/glossary.md +389 -389
- package/kit/skills/_shared-multi-tenant/glossary.md +186 -186
- package/kit/skills/_shared-observability/glossary.md +396 -396
- package/kit/skills/_shared-sre/glossary.md +712 -712
- package/kit/skills/_shared-supabase/glossary.md +234 -234
- package/kit/skills/blameless-postmortems/SKILL.md +340 -340
- package/kit/skills/burn-rate-alerting/SKILL.md +258 -258
- package/kit/skills/cascading-failures/SKILL.md +311 -311
- package/kit/skills/core-analysis-loop/SKILL.md +352 -352
- package/kit/skills/distributed-tracing/SKILL.md +362 -362
- package/kit/skills/dynamic-workflow-authoring/SKILL.md +327 -223
- package/kit/skills/eliminating-toil/SKILL.md +243 -243
- package/kit/skills/event-based-slos/SKILL.md +296 -296
- package/kit/skills/four-golden-signals/SKILL.md +314 -314
- package/kit/skills/hermetic-builds/SKILL.md +323 -323
- package/kit/skills/legacy-monster-methods/SKILL.md +444 -444
- package/kit/skills/llm-as-dependency/SKILL.md +436 -436
- package/kit/skills/load-shedding-graceful-degradation/SKILL.md +396 -396
- package/kit/skills/observability-driven-development/SKILL.md +315 -315
- package/kit/skills/observability-maturity-model/SKILL.md +222 -222
- package/kit/skills/opentelemetry-standard/SKILL.md +351 -351
- package/kit/skills/production-readiness-review/SKILL.md +305 -305
- package/kit/skills/release-engineering/SKILL.md +367 -367
- package/kit/skills/retry-strategies/SKILL.md +372 -372
- package/kit/skills/sre-risk-management/SKILL.md +221 -221
- package/kit/skills/structured-events/SKILL.md +265 -265
- package/kit/skills/supabase-cron-queues/SKILL.md +275 -275
- package/kit/skills/supabase-database-functions/SKILL.md +332 -332
- package/kit/skills/supabase-declarative-schema/SKILL.md +183 -183
- package/kit/skills/supabase-pgvector-rag/SKILL.md +253 -253
- package/kit/skills/supabase-postgres-style/SKILL.md +138 -138
- package/kit/skills/supabase-storage/SKILL.md +234 -234
- package/kit/skills/telemetry-pipelines/SKILL.md +259 -259
- package/kit/skills/telemetry-sampling/SKILL.md +256 -256
- package/kit/skills/ui-anti-padroes-ia/SKILL.md +261 -261
- package/kit/skills/ui-contexto-produto/SKILL.md +248 -248
- package/kit/skills/ui-cor-estrategia/SKILL.md +213 -213
- package/kit/skills/ui-critica-auditoria/SKILL.md +260 -260
- package/kit/skills/ui-motion-funcional/SKILL.md +264 -264
- package/kit/skills/ui-ritmo-espacial/SKILL.md +259 -259
- package/kit/skills/ui-tipografia/SKILL.md +211 -211
- package/package.json +1 -1
- package/src/cli/index.js +1114 -1114
- package/src/cli/render.js +194 -194
- package/src/cli/upgrade-check.js +135 -135
- package/src/core/error-redaction.js +76 -76
- package/src/core/failures.js +153 -153
- package/src/core/gate-runner.js +205 -205
- package/src/core/gates.js +82 -82
- package/src/core/logger.js +170 -170
- package/src/core/manifest-verify.js +174 -174
- package/src/core/metrics.js +268 -268
- package/src/core/notify.js +60 -60
- package/src/core/path-safety.js +141 -141
- package/src/core/replays.js +120 -120
- package/src/core/ui.js +185 -185
- package/src/mcp-server/install.js +149 -149
- package/src/mcp-server/roots.js +124 -124
- package/src/ui/auto-spawn.js +113 -113
- package/src/ui/browser.js +78 -78
- package/src/ui/client.js +130 -130
- package/src/ui/events.js +65 -65
- package/src/ui/lockfile.js +191 -191
- package/src/ui/port.js +67 -67
- package/src/ui/server.js +547 -547
- package/src/ui/wrapper.js +129 -129
package/src/ui/client.js
CHANGED
|
@@ -1,130 +1,130 @@
|
|
|
1
|
-
// src/ui/client.js
|
|
2
|
-
// Fire-and-forget publisher. Reads the lockfile to discover the running sidecar's
|
|
3
|
-
// port, then POSTs an event to /publish. If the sidecar isn't running (no lockfile,
|
|
4
|
-
// ECONNREFUSED, healthz mismatch), publish() resolves silently — publishers MUST NOT
|
|
5
|
-
// fail just because the optional UI isn't up.
|
|
6
|
-
|
|
7
|
-
import http from 'node:http';
|
|
8
|
-
import { readLock } from './lockfile.js';
|
|
9
|
-
import { validateEvent } from './events.js';
|
|
10
|
-
|
|
11
|
-
// Cache the resolved sidecar (port + token) across calls in a single process.
|
|
12
|
-
// SEC-14-02: token is needed for Authorization on every publish() — read from
|
|
13
|
-
// the same lockfile read as port to avoid double I/O.
|
|
14
|
-
const sidecarCache = new Map(); // projectRoot -> { port, token } | { port: 0, token: null }
|
|
15
|
-
const SIDECAR_CACHE_TTL_MS = 5_000;
|
|
16
|
-
const cacheTimestamps = new Map();
|
|
17
|
-
|
|
18
|
-
function readCachedSidecar(projectRoot) {
|
|
19
|
-
const ts = cacheTimestamps.get(projectRoot);
|
|
20
|
-
if (!ts || Date.now() - ts > SIDECAR_CACHE_TTL_MS) return undefined;
|
|
21
|
-
return sidecarCache.get(projectRoot);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function writeCachedSidecar(projectRoot, sidecar) {
|
|
25
|
-
sidecarCache.set(projectRoot, sidecar);
|
|
26
|
-
cacheTimestamps.set(projectRoot, Date.now());
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Backward-compat name; clears port + token cache. Tests + callers using
|
|
30
|
-
// clearPortCache continue to work without code change.
|
|
31
|
-
export function clearPortCache() {
|
|
32
|
-
sidecarCache.clear();
|
|
33
|
-
cacheTimestamps.clear();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function resolveSidecar(projectRoot) {
|
|
37
|
-
const cached = readCachedSidecar(projectRoot);
|
|
38
|
-
if (cached !== undefined) return cached;
|
|
39
|
-
const lock = readLock(projectRoot);
|
|
40
|
-
const sidecar = {
|
|
41
|
-
port: lock?.port ?? 0,
|
|
42
|
-
// SEC-14-02: null if missing (lockfile from older sidecar version pre-v1.14).
|
|
43
|
-
// Triggers degraded path: no Authorization header → server 401 → soft-fail.
|
|
44
|
-
token: typeof lock?.token === 'string' ? lock.token : null,
|
|
45
|
-
};
|
|
46
|
-
writeCachedSidecar(projectRoot, sidecar);
|
|
47
|
-
return sidecar;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// publish(event, { projectRoot, timeoutMs }): always resolves. Returns
|
|
51
|
-
// { sent: true, status } on 2xx
|
|
52
|
-
// { sent: false, reason } in every other case (no sidecar, validation, network)
|
|
53
|
-
export async function publish(event, { projectRoot, timeoutMs = 1500 } = {}) {
|
|
54
|
-
if (!projectRoot) return { sent: false, reason: 'no_project_root' };
|
|
55
|
-
|
|
56
|
-
const validationErr = validateEvent(event);
|
|
57
|
-
if (validationErr) return { sent: false, reason: `invalid_event: ${validationErr.message}` };
|
|
58
|
-
|
|
59
|
-
const { port, token } = resolveSidecar(projectRoot);
|
|
60
|
-
if (!port) return { sent: false, reason: 'no_sidecar' };
|
|
61
|
-
|
|
62
|
-
const body = JSON.stringify(event);
|
|
63
|
-
|
|
64
|
-
return new Promise((resolve) => {
|
|
65
|
-
const req = http.request({
|
|
66
|
-
method: 'POST',
|
|
67
|
-
host: '127.0.0.1',
|
|
68
|
-
port,
|
|
69
|
-
path: '/publish',
|
|
70
|
-
agent: false,
|
|
71
|
-
headers: {
|
|
72
|
-
'host': `127.0.0.1:${port}`,
|
|
73
|
-
'content-type': 'application/json',
|
|
74
|
-
'content-length': Buffer.byteLength(body, 'utf8'),
|
|
75
|
-
'origin': `http://127.0.0.1:${port}`,
|
|
76
|
-
'connection': 'close',
|
|
77
|
-
// SEC-14-02: attach Bearer token if lockfile has one. If not (older
|
|
78
|
-
// sidecar pre-v1.14), server returns 401 → resolves as { sent: false,
|
|
79
|
-
// reason: 'http_401' } via the soft-fail flow below.
|
|
80
|
-
...(token ? { 'authorization': `Bearer ${token}` } : {}),
|
|
81
|
-
},
|
|
82
|
-
}, (res) => {
|
|
83
|
-
// Drain — we don't actually care about the body, just the status.
|
|
84
|
-
res.resume();
|
|
85
|
-
res.on('end', () => {
|
|
86
|
-
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
87
|
-
resolve({ sent: true, status: res.statusCode });
|
|
88
|
-
} else {
|
|
89
|
-
// Stale lockfile or rotated token? Drop cache so next call re-reads.
|
|
90
|
-
// SEC-14-02: invalidate on 401 too — token may have rotated after
|
|
91
|
-
// sidecar restart; cache TTL of 5s would otherwise prolong recovery.
|
|
92
|
-
if (res.statusCode === 401 || res.statusCode === 403 || res.statusCode === 404) {
|
|
93
|
-
sidecarCache.delete(projectRoot);
|
|
94
|
-
cacheTimestamps.delete(projectRoot);
|
|
95
|
-
}
|
|
96
|
-
resolve({ sent: false, reason: `http_${res.statusCode}` });
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
req.on('error', (err) => {
|
|
102
|
-
// Most common: ECONNREFUSED (lockfile points at a dead port).
|
|
103
|
-
if (err.code === 'ECONNREFUSED' || err.code === 'ECONNRESET') {
|
|
104
|
-
sidecarCache.delete(projectRoot);
|
|
105
|
-
cacheTimestamps.delete(projectRoot);
|
|
106
|
-
}
|
|
107
|
-
resolve({ sent: false, reason: `error: ${err.code || err.message}` });
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
req.setTimeout(timeoutMs, () => {
|
|
111
|
-
try { req.destroy(); } catch { /* noop */ }
|
|
112
|
-
resolve({ sent: false, reason: 'timeout' });
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
req.write(body);
|
|
116
|
-
req.end();
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// publishMany emits a sequence of events one after another. Used by callers
|
|
121
|
-
// that want best-effort guaranteed ordering — http.request is async, so
|
|
122
|
-
// firing in parallel doesn't preserve order at the server.
|
|
123
|
-
export async function publishMany(events, opts) {
|
|
124
|
-
const results = [];
|
|
125
|
-
for (const evt of events) {
|
|
126
|
-
// eslint-disable-next-line no-await-in-loop
|
|
127
|
-
results.push(await publish(evt, opts));
|
|
128
|
-
}
|
|
129
|
-
return results;
|
|
130
|
-
}
|
|
1
|
+
// src/ui/client.js
|
|
2
|
+
// Fire-and-forget publisher. Reads the lockfile to discover the running sidecar's
|
|
3
|
+
// port, then POSTs an event to /publish. If the sidecar isn't running (no lockfile,
|
|
4
|
+
// ECONNREFUSED, healthz mismatch), publish() resolves silently — publishers MUST NOT
|
|
5
|
+
// fail just because the optional UI isn't up.
|
|
6
|
+
|
|
7
|
+
import http from 'node:http';
|
|
8
|
+
import { readLock } from './lockfile.js';
|
|
9
|
+
import { validateEvent } from './events.js';
|
|
10
|
+
|
|
11
|
+
// Cache the resolved sidecar (port + token) across calls in a single process.
|
|
12
|
+
// SEC-14-02: token is needed for Authorization on every publish() — read from
|
|
13
|
+
// the same lockfile read as port to avoid double I/O.
|
|
14
|
+
const sidecarCache = new Map(); // projectRoot -> { port, token } | { port: 0, token: null }
|
|
15
|
+
const SIDECAR_CACHE_TTL_MS = 5_000;
|
|
16
|
+
const cacheTimestamps = new Map();
|
|
17
|
+
|
|
18
|
+
function readCachedSidecar(projectRoot) {
|
|
19
|
+
const ts = cacheTimestamps.get(projectRoot);
|
|
20
|
+
if (!ts || Date.now() - ts > SIDECAR_CACHE_TTL_MS) return undefined;
|
|
21
|
+
return sidecarCache.get(projectRoot);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function writeCachedSidecar(projectRoot, sidecar) {
|
|
25
|
+
sidecarCache.set(projectRoot, sidecar);
|
|
26
|
+
cacheTimestamps.set(projectRoot, Date.now());
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Backward-compat name; clears port + token cache. Tests + callers using
|
|
30
|
+
// clearPortCache continue to work without code change.
|
|
31
|
+
export function clearPortCache() {
|
|
32
|
+
sidecarCache.clear();
|
|
33
|
+
cacheTimestamps.clear();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function resolveSidecar(projectRoot) {
|
|
37
|
+
const cached = readCachedSidecar(projectRoot);
|
|
38
|
+
if (cached !== undefined) return cached;
|
|
39
|
+
const lock = readLock(projectRoot);
|
|
40
|
+
const sidecar = {
|
|
41
|
+
port: lock?.port ?? 0,
|
|
42
|
+
// SEC-14-02: null if missing (lockfile from older sidecar version pre-v1.14).
|
|
43
|
+
// Triggers degraded path: no Authorization header → server 401 → soft-fail.
|
|
44
|
+
token: typeof lock?.token === 'string' ? lock.token : null,
|
|
45
|
+
};
|
|
46
|
+
writeCachedSidecar(projectRoot, sidecar);
|
|
47
|
+
return sidecar;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// publish(event, { projectRoot, timeoutMs }): always resolves. Returns
|
|
51
|
+
// { sent: true, status } on 2xx
|
|
52
|
+
// { sent: false, reason } in every other case (no sidecar, validation, network)
|
|
53
|
+
export async function publish(event, { projectRoot, timeoutMs = 1500 } = {}) {
|
|
54
|
+
if (!projectRoot) return { sent: false, reason: 'no_project_root' };
|
|
55
|
+
|
|
56
|
+
const validationErr = validateEvent(event);
|
|
57
|
+
if (validationErr) return { sent: false, reason: `invalid_event: ${validationErr.message}` };
|
|
58
|
+
|
|
59
|
+
const { port, token } = resolveSidecar(projectRoot);
|
|
60
|
+
if (!port) return { sent: false, reason: 'no_sidecar' };
|
|
61
|
+
|
|
62
|
+
const body = JSON.stringify(event);
|
|
63
|
+
|
|
64
|
+
return new Promise((resolve) => {
|
|
65
|
+
const req = http.request({
|
|
66
|
+
method: 'POST',
|
|
67
|
+
host: '127.0.0.1',
|
|
68
|
+
port,
|
|
69
|
+
path: '/publish',
|
|
70
|
+
agent: false,
|
|
71
|
+
headers: {
|
|
72
|
+
'host': `127.0.0.1:${port}`,
|
|
73
|
+
'content-type': 'application/json',
|
|
74
|
+
'content-length': Buffer.byteLength(body, 'utf8'),
|
|
75
|
+
'origin': `http://127.0.0.1:${port}`,
|
|
76
|
+
'connection': 'close',
|
|
77
|
+
// SEC-14-02: attach Bearer token if lockfile has one. If not (older
|
|
78
|
+
// sidecar pre-v1.14), server returns 401 → resolves as { sent: false,
|
|
79
|
+
// reason: 'http_401' } via the soft-fail flow below.
|
|
80
|
+
...(token ? { 'authorization': `Bearer ${token}` } : {}),
|
|
81
|
+
},
|
|
82
|
+
}, (res) => {
|
|
83
|
+
// Drain — we don't actually care about the body, just the status.
|
|
84
|
+
res.resume();
|
|
85
|
+
res.on('end', () => {
|
|
86
|
+
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
87
|
+
resolve({ sent: true, status: res.statusCode });
|
|
88
|
+
} else {
|
|
89
|
+
// Stale lockfile or rotated token? Drop cache so next call re-reads.
|
|
90
|
+
// SEC-14-02: invalidate on 401 too — token may have rotated after
|
|
91
|
+
// sidecar restart; cache TTL of 5s would otherwise prolong recovery.
|
|
92
|
+
if (res.statusCode === 401 || res.statusCode === 403 || res.statusCode === 404) {
|
|
93
|
+
sidecarCache.delete(projectRoot);
|
|
94
|
+
cacheTimestamps.delete(projectRoot);
|
|
95
|
+
}
|
|
96
|
+
resolve({ sent: false, reason: `http_${res.statusCode}` });
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
req.on('error', (err) => {
|
|
102
|
+
// Most common: ECONNREFUSED (lockfile points at a dead port).
|
|
103
|
+
if (err.code === 'ECONNREFUSED' || err.code === 'ECONNRESET') {
|
|
104
|
+
sidecarCache.delete(projectRoot);
|
|
105
|
+
cacheTimestamps.delete(projectRoot);
|
|
106
|
+
}
|
|
107
|
+
resolve({ sent: false, reason: `error: ${err.code || err.message}` });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
req.setTimeout(timeoutMs, () => {
|
|
111
|
+
try { req.destroy(); } catch { /* noop */ }
|
|
112
|
+
resolve({ sent: false, reason: 'timeout' });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
req.write(body);
|
|
116
|
+
req.end();
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// publishMany emits a sequence of events one after another. Used by callers
|
|
121
|
+
// that want best-effort guaranteed ordering — http.request is async, so
|
|
122
|
+
// firing in parallel doesn't preserve order at the server.
|
|
123
|
+
export async function publishMany(events, opts) {
|
|
124
|
+
const results = [];
|
|
125
|
+
for (const evt of events) {
|
|
126
|
+
// eslint-disable-next-line no-await-in-loop
|
|
127
|
+
results.push(await publish(evt, opts));
|
|
128
|
+
}
|
|
129
|
+
return results;
|
|
130
|
+
}
|
package/src/ui/events.js
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
// src/ui/events.js
|
|
2
|
-
// Schema and helpers for sidecar event payloads.
|
|
3
|
-
// Pure module: no I/O, no module-level state. Safe to import from any context.
|
|
4
|
-
|
|
5
|
-
import { randomBytes } from 'node:crypto';
|
|
6
|
-
|
|
7
|
-
export const EVENT_TYPES = Object.freeze([
|
|
8
|
-
'run.start',
|
|
9
|
-
'run.end',
|
|
10
|
-
'tool_invocation',
|
|
11
|
-
'progress',
|
|
12
|
-
'milestone',
|
|
13
|
-
'error',
|
|
14
|
-
'shutdown',
|
|
15
|
-
]);
|
|
16
|
-
|
|
17
|
-
export const EVENT_TYPE_SET = new Set(EVENT_TYPES);
|
|
18
|
-
|
|
19
|
-
const MAX_PAYLOAD_BYTES = 64 * 1024;
|
|
20
|
-
|
|
21
|
-
export function newRunId() {
|
|
22
|
-
return randomBytes(8).toString('hex');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function makeEvent({ type, runId, payload, ts }) {
|
|
26
|
-
if (!EVENT_TYPE_SET.has(type)) {
|
|
27
|
-
throw new TypeError(`Unknown event type: ${type}. Valid: ${EVENT_TYPES.join(', ')}`);
|
|
28
|
-
}
|
|
29
|
-
return {
|
|
30
|
-
type,
|
|
31
|
-
ts: typeof ts === 'number' ? ts : Date.now(),
|
|
32
|
-
runId: runId ?? null,
|
|
33
|
-
payload: payload ?? null,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// validateEvent returns null on success, or an Error explaining the rejection.
|
|
38
|
-
// Used by the server's POST /publish endpoint. Never throws.
|
|
39
|
-
export function validateEvent(value) {
|
|
40
|
-
if (value === null || typeof value !== 'object') {
|
|
41
|
-
return new Error('event must be an object');
|
|
42
|
-
}
|
|
43
|
-
if (!EVENT_TYPE_SET.has(value.type)) {
|
|
44
|
-
return new Error(`event.type must be one of ${EVENT_TYPES.join(', ')}`);
|
|
45
|
-
}
|
|
46
|
-
if (typeof value.ts !== 'number' || !Number.isFinite(value.ts)) {
|
|
47
|
-
return new Error('event.ts must be a finite number (epoch ms)');
|
|
48
|
-
}
|
|
49
|
-
if (value.runId !== null && value.runId !== undefined && typeof value.runId !== 'string') {
|
|
50
|
-
return new Error('event.runId must be string or null');
|
|
51
|
-
}
|
|
52
|
-
// payload may be anything serializable; cap raw size
|
|
53
|
-
let serialized;
|
|
54
|
-
try {
|
|
55
|
-
serialized = JSON.stringify(value);
|
|
56
|
-
} catch (err) {
|
|
57
|
-
return new Error(`event not serializable: ${err.message}`);
|
|
58
|
-
}
|
|
59
|
-
if (Buffer.byteLength(serialized, 'utf8') > MAX_PAYLOAD_BYTES) {
|
|
60
|
-
return new Error(`event exceeds ${MAX_PAYLOAD_BYTES} bytes (got ${Buffer.byteLength(serialized, 'utf8')})`);
|
|
61
|
-
}
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export const __test = { MAX_PAYLOAD_BYTES };
|
|
1
|
+
// src/ui/events.js
|
|
2
|
+
// Schema and helpers for sidecar event payloads.
|
|
3
|
+
// Pure module: no I/O, no module-level state. Safe to import from any context.
|
|
4
|
+
|
|
5
|
+
import { randomBytes } from 'node:crypto';
|
|
6
|
+
|
|
7
|
+
export const EVENT_TYPES = Object.freeze([
|
|
8
|
+
'run.start',
|
|
9
|
+
'run.end',
|
|
10
|
+
'tool_invocation',
|
|
11
|
+
'progress',
|
|
12
|
+
'milestone',
|
|
13
|
+
'error',
|
|
14
|
+
'shutdown',
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
export const EVENT_TYPE_SET = new Set(EVENT_TYPES);
|
|
18
|
+
|
|
19
|
+
const MAX_PAYLOAD_BYTES = 64 * 1024;
|
|
20
|
+
|
|
21
|
+
export function newRunId() {
|
|
22
|
+
return randomBytes(8).toString('hex');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function makeEvent({ type, runId, payload, ts }) {
|
|
26
|
+
if (!EVENT_TYPE_SET.has(type)) {
|
|
27
|
+
throw new TypeError(`Unknown event type: ${type}. Valid: ${EVENT_TYPES.join(', ')}`);
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
type,
|
|
31
|
+
ts: typeof ts === 'number' ? ts : Date.now(),
|
|
32
|
+
runId: runId ?? null,
|
|
33
|
+
payload: payload ?? null,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// validateEvent returns null on success, or an Error explaining the rejection.
|
|
38
|
+
// Used by the server's POST /publish endpoint. Never throws.
|
|
39
|
+
export function validateEvent(value) {
|
|
40
|
+
if (value === null || typeof value !== 'object') {
|
|
41
|
+
return new Error('event must be an object');
|
|
42
|
+
}
|
|
43
|
+
if (!EVENT_TYPE_SET.has(value.type)) {
|
|
44
|
+
return new Error(`event.type must be one of ${EVENT_TYPES.join(', ')}`);
|
|
45
|
+
}
|
|
46
|
+
if (typeof value.ts !== 'number' || !Number.isFinite(value.ts)) {
|
|
47
|
+
return new Error('event.ts must be a finite number (epoch ms)');
|
|
48
|
+
}
|
|
49
|
+
if (value.runId !== null && value.runId !== undefined && typeof value.runId !== 'string') {
|
|
50
|
+
return new Error('event.runId must be string or null');
|
|
51
|
+
}
|
|
52
|
+
// payload may be anything serializable; cap raw size
|
|
53
|
+
let serialized;
|
|
54
|
+
try {
|
|
55
|
+
serialized = JSON.stringify(value);
|
|
56
|
+
} catch (err) {
|
|
57
|
+
return new Error(`event not serializable: ${err.message}`);
|
|
58
|
+
}
|
|
59
|
+
if (Buffer.byteLength(serialized, 'utf8') > MAX_PAYLOAD_BYTES) {
|
|
60
|
+
return new Error(`event exceeds ${MAX_PAYLOAD_BYTES} bytes (got ${Buffer.byteLength(serialized, 'utf8')})`);
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const __test = { MAX_PAYLOAD_BYTES };
|