@nocville/npc 0.2.1 → 0.2.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 +58 -120
- package/dist/cjs/cli.js +55 -34
- package/dist/cjs/core/base-npc.js +8 -0
- package/dist/cjs/core/config.js +19 -2
- package/dist/cjs/core/errors.js +23 -0
- package/dist/cjs/core/http.js +121 -0
- package/dist/cjs/core/pixel-art.js +110 -0
- package/dist/cjs/core/polling-npc.js +99 -14
- package/dist/cjs/core/session-filter.js +3 -1
- package/dist/cjs/core/text.js +13 -0
- package/dist/cjs/core/thresholds.js +29 -0
- package/dist/cjs/index.js +10 -2
- package/dist/cjs/npcs/agent-common/session.js +1 -0
- package/dist/cjs/npcs/github-watcher/defaults.js +7 -1
- package/dist/cjs/npcs/github-watcher/github-api.js +82 -45
- package/dist/cjs/npcs/github-watcher/index.js +82 -36
- package/dist/cjs/npcs/internet-connectivity/checks.js +70 -67
- package/dist/cjs/npcs/internet-connectivity/defaults.js +5 -2
- package/dist/cjs/npcs/internet-connectivity/index.js +134 -131
- package/dist/cjs/npcs/log-watcher/buffer.js +61 -0
- package/dist/cjs/npcs/log-watcher/index.js +109 -103
- package/dist/cjs/npcs/log-watcher/tail.js +100 -53
- package/dist/cjs/npcs/process-watcher/index.js +61 -51
- package/dist/cjs/npcs/process-watcher/process-scanner.js +63 -69
- package/dist/cjs/npcs/receptionist/defaults.js +4 -0
- package/dist/cjs/npcs/receptionist/index.js +45 -19
- package/dist/cjs/npcs/receptionist/weather.js +42 -47
- package/dist/cjs/npcs/system-monitor/collectors.js +66 -17
- package/dist/cjs/npcs/system-monitor/index.js +52 -28
- package/dist/cjs/npcs/web-page-watcher/basic-mode.js +30 -14
- package/dist/cjs/npcs/web-page-watcher/capture.js +23 -1
- package/dist/cjs/npcs/web-page-watcher/defaults.js +44 -2
- package/dist/cjs/npcs/web-page-watcher/index.js +119 -68
- package/dist/cjs/npcs/web-page-watcher/pixelate.js +2 -1
- package/dist/cli.js +56 -35
- package/dist/core/base-npc.d.ts +5 -0
- package/dist/core/base-npc.js +8 -0
- package/dist/core/config.d.ts +5 -0
- package/dist/core/config.js +18 -2
- package/dist/core/errors.d.ts +11 -0
- package/dist/core/errors.js +18 -0
- package/dist/core/http.d.ts +52 -0
- package/dist/core/http.js +111 -0
- package/dist/core/pixel-art.d.ts +38 -0
- package/dist/core/pixel-art.js +105 -0
- package/dist/core/polling-npc.d.ts +22 -1
- package/dist/core/polling-npc.js +98 -13
- package/dist/core/session-filter.d.ts +2 -2
- package/dist/core/session-filter.js +3 -1
- package/dist/core/text.d.ts +6 -0
- package/dist/core/text.js +12 -0
- package/dist/core/thresholds.d.ts +9 -1
- package/dist/core/thresholds.js +27 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.js +4 -2
- package/dist/npcs/agent-common/session.js +1 -0
- package/dist/npcs/github-watcher/defaults.d.ts +6 -0
- package/dist/npcs/github-watcher/defaults.js +7 -1
- package/dist/npcs/github-watcher/github-api.d.ts +21 -7
- package/dist/npcs/github-watcher/github-api.js +80 -45
- package/dist/npcs/github-watcher/index.d.ts +6 -0
- package/dist/npcs/github-watcher/index.js +83 -37
- package/dist/npcs/internet-connectivity/checks.d.ts +15 -18
- package/dist/npcs/internet-connectivity/checks.js +70 -67
- package/dist/npcs/internet-connectivity/defaults.d.ts +3 -0
- package/dist/npcs/internet-connectivity/defaults.js +5 -2
- package/dist/npcs/internet-connectivity/index.d.ts +19 -6
- package/dist/npcs/internet-connectivity/index.js +135 -133
- package/dist/npcs/log-watcher/buffer.d.ts +22 -0
- package/dist/npcs/log-watcher/buffer.js +58 -0
- package/dist/npcs/log-watcher/index.d.ts +22 -21
- package/dist/npcs/log-watcher/index.js +112 -106
- package/dist/npcs/log-watcher/tail.d.ts +23 -3
- package/dist/npcs/log-watcher/tail.js +101 -54
- package/dist/npcs/process-watcher/index.d.ts +1 -1
- package/dist/npcs/process-watcher/index.js +62 -52
- package/dist/npcs/process-watcher/process-scanner.d.ts +29 -1
- package/dist/npcs/process-watcher/process-scanner.js +61 -71
- package/dist/npcs/receptionist/defaults.d.ts +4 -0
- package/dist/npcs/receptionist/defaults.js +4 -0
- package/dist/npcs/receptionist/index.d.ts +12 -2
- package/dist/npcs/receptionist/index.js +46 -20
- package/dist/npcs/receptionist/weather.d.ts +8 -6
- package/dist/npcs/receptionist/weather.js +39 -45
- package/dist/npcs/system-monitor/collectors.d.ts +15 -3
- package/dist/npcs/system-monitor/collectors.js +64 -17
- package/dist/npcs/system-monitor/defaults.d.ts +1 -1
- package/dist/npcs/system-monitor/index.d.ts +2 -1
- package/dist/npcs/system-monitor/index.js +54 -30
- package/dist/npcs/web-page-watcher/basic-mode.d.ts +2 -2
- package/dist/npcs/web-page-watcher/basic-mode.js +30 -14
- package/dist/npcs/web-page-watcher/capture.d.ts +18 -0
- package/dist/npcs/web-page-watcher/capture.js +21 -1
- package/dist/npcs/web-page-watcher/defaults.d.ts +23 -0
- package/dist/npcs/web-page-watcher/defaults.js +41 -1
- package/dist/npcs/web-page-watcher/index.d.ts +18 -1
- package/dist/npcs/web-page-watcher/index.js +121 -70
- package/dist/npcs/web-page-watcher/pixelate.js +2 -1
- package/package.json +2 -7
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Small, bounded HTTP helpers for NPCs that read untrusted remote content
|
|
4
|
+
* (weather/geo APIs, GitHub, watched web pages). Every call has a timeout and
|
|
5
|
+
* every body read has a byte cap, so a slow or hostile server can neither hang
|
|
6
|
+
* a collection nor balloon memory.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.DEFAULT_MAX_BODY_BYTES = exports.NPC_USER_AGENT = void 0;
|
|
10
|
+
exports.isHttpUrl = isHttpUrl;
|
|
11
|
+
exports.displayUrl = displayUrl;
|
|
12
|
+
exports.readTextCapped = readTextCapped;
|
|
13
|
+
exports.discardBody = discardBody;
|
|
14
|
+
exports.fetchJson = fetchJson;
|
|
15
|
+
exports.asRecord = asRecord;
|
|
16
|
+
exports.finiteNumber = finiteNumber;
|
|
17
|
+
/** The `User-Agent` sent by every NPC request. */
|
|
18
|
+
exports.NPC_USER_AGENT = 'nocville-npc';
|
|
19
|
+
/** Default cap for a response body read into memory. */
|
|
20
|
+
exports.DEFAULT_MAX_BODY_BYTES = 1024 * 1024;
|
|
21
|
+
/** True for an absolute http(s) URL (anything else — file:, data:, javascript: — is refused). */
|
|
22
|
+
function isHttpUrl(value) {
|
|
23
|
+
try {
|
|
24
|
+
const url = new URL(value);
|
|
25
|
+
return url.protocol === 'http:' || url.protocol === 'https:';
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** A URL rendered for logs/screens: scheme, host and path only — never credentials or query. */
|
|
32
|
+
function displayUrl(value) {
|
|
33
|
+
try {
|
|
34
|
+
const url = new URL(value);
|
|
35
|
+
return `${url.protocol}//${url.host}${url.pathname === '/' ? '' : url.pathname}`;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return '(invalid url)';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Read a response body as UTF-8, keeping at most `maxBytes`. Streams when the
|
|
43
|
+
* body is available (cancelling the rest); otherwise falls back to `text()`
|
|
44
|
+
* after rejecting an oversized declared Content-Length. Returns the (possibly
|
|
45
|
+
* truncated) text and whether it was truncated.
|
|
46
|
+
*/
|
|
47
|
+
async function readTextCapped(res, maxBytes = exports.DEFAULT_MAX_BODY_BYTES) {
|
|
48
|
+
const reader = res.body?.getReader();
|
|
49
|
+
if (!reader) {
|
|
50
|
+
const declared = Number(res.headers?.get('content-length') ?? NaN);
|
|
51
|
+
if (Number.isFinite(declared) && declared > maxBytes)
|
|
52
|
+
return { text: '', truncated: true };
|
|
53
|
+
const text = await res.text();
|
|
54
|
+
return text.length > maxBytes
|
|
55
|
+
? { text: text.slice(0, maxBytes), truncated: true }
|
|
56
|
+
: { text, truncated: false };
|
|
57
|
+
}
|
|
58
|
+
const chunks = [];
|
|
59
|
+
let total = 0;
|
|
60
|
+
let truncated = false;
|
|
61
|
+
try {
|
|
62
|
+
for (;;) {
|
|
63
|
+
const { done, value } = await reader.read();
|
|
64
|
+
if (done)
|
|
65
|
+
break;
|
|
66
|
+
if (total + value.byteLength > maxBytes) {
|
|
67
|
+
chunks.push(value.subarray(0, maxBytes - total));
|
|
68
|
+
total = maxBytes;
|
|
69
|
+
truncated = true;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
chunks.push(value);
|
|
73
|
+
total += value.byteLength;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
if (truncated)
|
|
78
|
+
await reader.cancel().catch(() => undefined);
|
|
79
|
+
reader.releaseLock();
|
|
80
|
+
}
|
|
81
|
+
return { text: new TextDecoder().decode(Buffer.concat(chunks)), truncated };
|
|
82
|
+
}
|
|
83
|
+
/** Discard a response body without reading it (frees the connection). */
|
|
84
|
+
async function discardBody(res) {
|
|
85
|
+
await res.body?.cancel().catch(() => undefined);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* GET a JSON document with a timeout and size cap. Returns null on any network
|
|
89
|
+
* error, non-2xx status, oversized body or malformed JSON — callers degrade
|
|
90
|
+
* gracefully rather than throw.
|
|
91
|
+
*/
|
|
92
|
+
async function fetchJson(url, opts) {
|
|
93
|
+
try {
|
|
94
|
+
const res = await fetch(url, {
|
|
95
|
+
signal: AbortSignal.timeout(opts.timeoutMs),
|
|
96
|
+
headers: { 'User-Agent': exports.NPC_USER_AGENT, Accept: 'application/json', ...opts.headers },
|
|
97
|
+
});
|
|
98
|
+
if (!res.ok) {
|
|
99
|
+
await discardBody(res);
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const { text, truncated } = await readTextCapped(res, opts.maxBytes ?? 256 * 1024);
|
|
103
|
+
if (truncated)
|
|
104
|
+
return null;
|
|
105
|
+
return JSON.parse(text);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/** Narrow an unknown JSON value to a plain object (arrays and null excluded). */
|
|
112
|
+
function asRecord(value) {
|
|
113
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
114
|
+
? value
|
|
115
|
+
: null;
|
|
116
|
+
}
|
|
117
|
+
/** A finite number from an unknown JSON value, else null. */
|
|
118
|
+
function finiteNumber(value) {
|
|
119
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : null;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodePixelArt = encodePixelArt;
|
|
4
|
+
exports.pixelArtFromPng = pixelArtFromPng;
|
|
5
|
+
exports.pixelDevice = pixelDevice;
|
|
6
|
+
const node_fs_1 = require("node:fs");
|
|
7
|
+
const pngjs_1 = require("pngjs");
|
|
8
|
+
/**
|
|
9
|
+
* Helpers for custom pixel-art companions (`screenType: 'pixel-device'`): your NPC's own
|
|
10
|
+
* device, sign or machine, drawn frameless beside it in the office.
|
|
11
|
+
*
|
|
12
|
+
* Art is 32 pixels per tile (a 2×3-tile device is 64×96, like the office furniture) and is
|
|
13
|
+
* sent as `pixel-image` screen data: a palette of up to 255 colours plus one byte per pixel.
|
|
14
|
+
* Fully transparent pixels (alpha < 128) become see-through, and several frames stacked top
|
|
15
|
+
* to bottom animate at `fps`. Push a new image whenever its state changes (e.g. an LED turns
|
|
16
|
+
* red); the server rate-limits pushes, so animate with frames rather than many pushes.
|
|
17
|
+
*/
|
|
18
|
+
/** Largest single screen-data value the API accepts (base64 pixels). */
|
|
19
|
+
const MAX_VALUE = 220_000;
|
|
20
|
+
const MAX_COLORS = 255;
|
|
21
|
+
const MAX_FRAMES = 16;
|
|
22
|
+
/** Encode RGBA frames as `pixel-image` screen data. Throws with a fix-it message. */
|
|
23
|
+
function encodePixelArt(art) {
|
|
24
|
+
const { width, height, frames } = art;
|
|
25
|
+
if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1)
|
|
26
|
+
throw new Error('Pixel art needs a positive whole-number width and height.');
|
|
27
|
+
if (width > 192 || height > 192)
|
|
28
|
+
throw new Error(`Pixel art is ${width}×${height}; keep it within 192×192 (6×6 tiles).`);
|
|
29
|
+
if (frames.length < 1 || frames.length > MAX_FRAMES)
|
|
30
|
+
throw new Error(`Pixel art needs 1–${MAX_FRAMES} frames (got ${frames.length}).`);
|
|
31
|
+
const size = width * height;
|
|
32
|
+
for (const frame of frames)
|
|
33
|
+
if (frame.length !== size * 4)
|
|
34
|
+
throw new Error(`Each frame must be ${width}×${height} RGBA (${size * 4} bytes).`);
|
|
35
|
+
// Index 0 is reserved for transparency when any pixel is see-through.
|
|
36
|
+
const transparent = frames.some((f) => {
|
|
37
|
+
for (let i = 3; i < f.length; i += 4)
|
|
38
|
+
if (f[i] < 128)
|
|
39
|
+
return true;
|
|
40
|
+
return false;
|
|
41
|
+
});
|
|
42
|
+
const colors = new Map();
|
|
43
|
+
const palette = transparent ? [0, 0, 0] : [];
|
|
44
|
+
const pixels = Buffer.alloc(size * frames.length);
|
|
45
|
+
frames.forEach((frame, f) => {
|
|
46
|
+
for (let i = 0; i < size; i++) {
|
|
47
|
+
const r = frame[i * 4], g = frame[i * 4 + 1], b = frame[i * 4 + 2];
|
|
48
|
+
if (transparent && frame[i * 4 + 3] < 128) {
|
|
49
|
+
pixels[f * size + i] = 0;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const key = (r << 16) | (g << 8) | b;
|
|
53
|
+
let index = colors.get(key);
|
|
54
|
+
if (index === undefined) {
|
|
55
|
+
index = palette.length / 3;
|
|
56
|
+
if (index > MAX_COLORS)
|
|
57
|
+
throw new Error(`Pixel art uses more than ${MAX_COLORS} colours; reduce the palette (pixel art usually needs 8–32).`);
|
|
58
|
+
colors.set(key, index);
|
|
59
|
+
palette.push(r, g, b);
|
|
60
|
+
}
|
|
61
|
+
pixels[f * size + i] = index;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
const tokens = [`idx8`, `${width}x${height}`, `p${palette.length / 3}`];
|
|
65
|
+
if (transparent)
|
|
66
|
+
tokens.push('t0');
|
|
67
|
+
if (frames.length > 1) {
|
|
68
|
+
const fps = Math.min(12, Math.max(1, Math.round(art.fps ?? 4)));
|
|
69
|
+
tokens.push(`f${frames.length}`, `fps${fps}`);
|
|
70
|
+
}
|
|
71
|
+
const encoded = pixels.toString('base64');
|
|
72
|
+
if (encoded.length > MAX_VALUE)
|
|
73
|
+
throw new Error('Pixel art is too large to send; use fewer frames or a smaller size.');
|
|
74
|
+
return {
|
|
75
|
+
layout: 'pixel-image',
|
|
76
|
+
values: [
|
|
77
|
+
{ label: 'fmt', value: tokens.join(';') },
|
|
78
|
+
{ label: 'palette', value: Buffer.from(palette).toString('base64') },
|
|
79
|
+
{ label: 'pixels', value: encoded },
|
|
80
|
+
],
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Load a PNG (path or bytes) as pixel-device screen data. For animation, stack frames
|
|
85
|
+
* top to bottom in one PNG and pass `frameHeight`.
|
|
86
|
+
*/
|
|
87
|
+
function pixelArtFromPng(png, opts = {}) {
|
|
88
|
+
const image = pngjs_1.PNG.sync.read(typeof png === 'string' ? (0, node_fs_1.readFileSync)(png) : png);
|
|
89
|
+
const frameHeight = opts.frameHeight ?? image.height;
|
|
90
|
+
if (frameHeight < 1 || image.height % frameHeight !== 0)
|
|
91
|
+
throw new Error(`PNG height ${image.height} is not a whole number of ${frameHeight}px frames.`);
|
|
92
|
+
const frameBytes = image.width * frameHeight * 4;
|
|
93
|
+
const frames = [];
|
|
94
|
+
for (let offset = 0; offset < image.data.length; offset += frameBytes)
|
|
95
|
+
frames.push(image.data.subarray(offset, offset + frameBytes));
|
|
96
|
+
return encodePixelArt({ width: image.width, height: frameHeight, frames, fps: opts.fps });
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Companion declaration for a pixel device: `size` in tiles (art is 32px per tile).
|
|
100
|
+
* Freestanding by default so it sits on the floor next to the NPC's home.
|
|
101
|
+
*/
|
|
102
|
+
function pixelDevice(opts) {
|
|
103
|
+
return {
|
|
104
|
+
screenType: 'pixel-device',
|
|
105
|
+
size: { w: opts.w, h: opts.h },
|
|
106
|
+
placement: opts.placement ?? 'freestanding',
|
|
107
|
+
...(opts.label ? { label: opts.label } : {}),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=pixel-art.js.map
|
|
@@ -1,17 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PollingNPC = void 0;
|
|
3
|
+
exports.PollingNPC = exports.MIN_POLL_INTERVAL_MS = void 0;
|
|
4
4
|
const base_npc_js_1 = require("./base-npc.js");
|
|
5
|
+
/**
|
|
6
|
+
* Lower bound for any poll interval, so a typo can't exhaust the API's
|
|
7
|
+
* per-service rate limit (60 pushes/min) or hammer the watched source.
|
|
8
|
+
*/
|
|
9
|
+
exports.MIN_POLL_INTERVAL_MS = 5000;
|
|
10
|
+
/** How long `stop()` waits for an in-flight collection before pushing offline anyway. */
|
|
11
|
+
const STOP_DRAIN_TIMEOUT_MS = 10_000;
|
|
5
12
|
function clamp(n, min, max) {
|
|
6
13
|
return Math.min(max, Math.max(min, n));
|
|
7
14
|
}
|
|
8
15
|
class PollingNPC extends base_npc_js_1.BaseNPC {
|
|
9
16
|
timer = null;
|
|
10
|
-
|
|
17
|
+
keepaliveTimer = null;
|
|
18
|
+
/** The running collection + push, if any (awaited by `onStop`). */
|
|
19
|
+
inflight = null;
|
|
20
|
+
/** A `collectNow()` arrived while a collection was in flight: run once more after it. */
|
|
21
|
+
rerunRequested = false;
|
|
22
|
+
/** The last successfully pushed cycle, replayed (without its screen) by the keepalive. */
|
|
23
|
+
lastPushed = null;
|
|
24
|
+
lastPushAt = 0;
|
|
11
25
|
pollInterval;
|
|
12
26
|
constructor(config) {
|
|
13
27
|
super(config);
|
|
14
|
-
|
|
28
|
+
const requested = config.pollInterval ?? 15000;
|
|
29
|
+
this.pollInterval = Number.isFinite(requested)
|
|
30
|
+
? Math.max(exports.MIN_POLL_INTERVAL_MS, requested)
|
|
31
|
+
: 15000;
|
|
15
32
|
}
|
|
16
33
|
/** Scale staleness to the poll cadence: the server marks unknown at ~2x the interval. */
|
|
17
34
|
get heartbeatSeconds() {
|
|
@@ -20,14 +37,22 @@ class PollingNPC extends base_npc_js_1.BaseNPC {
|
|
|
20
37
|
async onStart() {
|
|
21
38
|
// Collect immediately on start
|
|
22
39
|
await this.runCollection();
|
|
40
|
+
// Stopped while the first collection ran: don't arm timers nobody will clear.
|
|
41
|
+
if (this.state !== 'running')
|
|
42
|
+
return;
|
|
23
43
|
// Then on interval
|
|
24
44
|
this.timer = setInterval(() => {
|
|
25
45
|
void this.runCollection();
|
|
26
46
|
}, this.pollInterval);
|
|
27
|
-
//
|
|
28
|
-
// collections
|
|
29
|
-
|
|
30
|
-
|
|
47
|
+
// Only very slow pollers (> heartbeatSeconds, i.e. > 30 min given the clamp)
|
|
48
|
+
// could go stale between collections. Keep them alive by replaying the
|
|
49
|
+
// last cycle. The SDK's own heartbeat is not used: it omits `map`, `name`
|
|
50
|
+
// and `alertConfig`, so it would spawn a twin on the team's default map.
|
|
51
|
+
const heartbeatMs = this.heartbeatSeconds * 1000;
|
|
52
|
+
if (this.pollInterval > heartbeatMs) {
|
|
53
|
+
this.keepaliveTimer = setInterval(() => {
|
|
54
|
+
void this.keepalive(heartbeatMs);
|
|
55
|
+
}, heartbeatMs);
|
|
31
56
|
}
|
|
32
57
|
}
|
|
33
58
|
async onStop() {
|
|
@@ -35,29 +60,89 @@ class PollingNPC extends base_npc_js_1.BaseNPC {
|
|
|
35
60
|
clearInterval(this.timer);
|
|
36
61
|
this.timer = null;
|
|
37
62
|
}
|
|
38
|
-
this.
|
|
63
|
+
if (this.keepaliveTimer) {
|
|
64
|
+
clearInterval(this.keepaliveTimer);
|
|
65
|
+
this.keepaliveTimer = null;
|
|
66
|
+
}
|
|
67
|
+
this.rerunRequested = false;
|
|
68
|
+
// Let an in-flight push land before the offline push, so it can't arrive
|
|
69
|
+
// afterwards and respawn the NPC. Bounded: a hung collector never blocks exit.
|
|
70
|
+
if (this.inflight) {
|
|
71
|
+
let timeout;
|
|
72
|
+
await Promise.race([
|
|
73
|
+
this.inflight,
|
|
74
|
+
new Promise((resolve) => {
|
|
75
|
+
timeout = setTimeout(resolve, STOP_DRAIN_TIMEOUT_MS);
|
|
76
|
+
}),
|
|
77
|
+
]);
|
|
78
|
+
clearTimeout(timeout);
|
|
79
|
+
}
|
|
39
80
|
}
|
|
40
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Collect and push now instead of waiting for the next tick — for NPCs that
|
|
83
|
+
* also react to events (e.g. a fresh error line). Coalesces: if a collection
|
|
84
|
+
* is already running, exactly one more runs after it.
|
|
85
|
+
*/
|
|
86
|
+
collectNow() {
|
|
41
87
|
if (this.state !== 'running')
|
|
42
88
|
return;
|
|
89
|
+
if (this.inflight) {
|
|
90
|
+
this.rerunRequested = true;
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
void this.runCollection();
|
|
94
|
+
}
|
|
95
|
+
runCollection() {
|
|
96
|
+
if (this.state !== 'running')
|
|
97
|
+
return Promise.resolve();
|
|
43
98
|
// Overlap guard: if a slow collector is still running, skip this tick rather
|
|
44
99
|
// than stacking collections (load-bearing for screenshot/render collectors).
|
|
45
|
-
if (this.
|
|
100
|
+
if (this.inflight) {
|
|
46
101
|
this.logger.debug('Previous collection still in flight, skipping this tick');
|
|
47
|
-
return;
|
|
102
|
+
return this.inflight;
|
|
48
103
|
}
|
|
49
|
-
this.
|
|
104
|
+
const run = this.collectAndPush().finally(() => {
|
|
105
|
+
this.inflight = null;
|
|
106
|
+
if (this.rerunRequested && this.state === 'running') {
|
|
107
|
+
this.rerunRequested = false;
|
|
108
|
+
void this.runCollection();
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
this.inflight = run;
|
|
112
|
+
return run;
|
|
113
|
+
}
|
|
114
|
+
async collectAndPush() {
|
|
50
115
|
try {
|
|
51
116
|
const metrics = await this.collect();
|
|
117
|
+
// Stopped mid-collection: never push after (or race) the offline push.
|
|
118
|
+
if (this.state !== 'running')
|
|
119
|
+
return;
|
|
52
120
|
await this.pushMetrics(metrics);
|
|
121
|
+
this.lastPushed = metrics;
|
|
122
|
+
this.lastPushAt = Date.now();
|
|
53
123
|
}
|
|
54
124
|
catch (err) {
|
|
125
|
+
if (this.state !== 'running')
|
|
126
|
+
return; // teardown aborted the collector
|
|
55
127
|
this.logger.error('Collection failed', {
|
|
56
128
|
error: err instanceof Error ? err.message : String(err),
|
|
57
129
|
});
|
|
58
130
|
}
|
|
59
|
-
|
|
60
|
-
|
|
131
|
+
}
|
|
132
|
+
/** Replay the last cycle (minus its screen, which the server keeps) if nothing was pushed recently. */
|
|
133
|
+
async keepalive(heartbeatMs) {
|
|
134
|
+
if (this.state !== 'running' || this.inflight || !this.lastPushed)
|
|
135
|
+
return;
|
|
136
|
+
if (Date.now() - this.lastPushAt < heartbeatMs / 2)
|
|
137
|
+
return;
|
|
138
|
+
try {
|
|
139
|
+
await this.pushMetrics({ ...this.lastPushed, screenData: undefined });
|
|
140
|
+
this.lastPushAt = Date.now();
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
this.logger.debug('Keepalive push failed', {
|
|
144
|
+
error: err instanceof Error ? err.message : String(err),
|
|
145
|
+
});
|
|
61
146
|
}
|
|
62
147
|
}
|
|
63
148
|
}
|
|
@@ -113,7 +113,9 @@ function projectMatches(pattern, projectDir) {
|
|
|
113
113
|
const re = globRegExp(pattern, false);
|
|
114
114
|
return dir.split('/').some((segment) => segment && re.test(segment));
|
|
115
115
|
}
|
|
116
|
-
|
|
116
|
+
// A trailing `/*` or `/**` means "this folder and everything in it": subfolders already
|
|
117
|
+
// match through their ancestors, so drop it and the folder itself matches too.
|
|
118
|
+
const expanded = normalizePath((pattern.startsWith('~') ? (0, node_os_1.homedir)() + pattern.slice(1) : pattern).replace(/(\/\*{1,2})+$/, ''));
|
|
117
119
|
const re = globRegExp(expanded, true);
|
|
118
120
|
// The directory itself or any ancestor: a rule for a folder covers its subfolders.
|
|
119
121
|
for (let p = dir; p; p = p.includes('/') ? p.slice(0, p.lastIndexOf('/')) : '') {
|
package/dist/cjs/core/text.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.truncate = truncate;
|
|
4
|
+
exports.sanitizeText = sanitizeText;
|
|
4
5
|
/**
|
|
5
6
|
* Truncate a string to at most `max` characters, appending an ellipsis when cut.
|
|
6
7
|
* Used to keep messages and screen lines within API limits.
|
|
@@ -12,4 +13,16 @@ function truncate(s, max = 200) {
|
|
|
12
13
|
return s.slice(0, Math.max(0, max));
|
|
13
14
|
return s.slice(0, max - 1) + '…';
|
|
14
15
|
}
|
|
16
|
+
// C0/C1 controls (incl. ESC, so no terminal escapes) plus the bidi overrides,
|
|
17
|
+
// isolates and marks that can visually reorder text on a banner.
|
|
18
|
+
// eslint-disable-next-line no-control-regex
|
|
19
|
+
const UNSAFE_CHARS = /[\u0000-\u0008\u000B-\u001F\u007F-\u009F--]/g;
|
|
20
|
+
/**
|
|
21
|
+
* Make untrusted remote text (commit messages, page titles, log lines) safe to
|
|
22
|
+
* display: drop control and bidi-override characters, collapse whitespace
|
|
23
|
+
* (including newlines and tabs) to single spaces, trim, and truncate.
|
|
24
|
+
*/
|
|
25
|
+
function sanitizeText(s, max = 200) {
|
|
26
|
+
return truncate(s.replace(UNSAFE_CHARS, '').replace(/\s+/g, ' ').trim(), max);
|
|
27
|
+
}
|
|
15
28
|
//# sourceMappingURL=text.js.map
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.evaluateThreshold = evaluateThreshold;
|
|
4
4
|
exports.worstStatus = worstStatus;
|
|
5
|
+
exports.moodForStatus = moodForStatus;
|
|
6
|
+
exports.thresholdProblem = thresholdProblem;
|
|
5
7
|
/**
|
|
6
8
|
* Evaluate a numeric value against thresholds to determine status.
|
|
7
9
|
*/
|
|
@@ -45,4 +47,31 @@ function worstStatus(statuses) {
|
|
|
45
47
|
}
|
|
46
48
|
return worst;
|
|
47
49
|
}
|
|
50
|
+
/** The NPC mood that matches a status (drives the NPC's demeanour and emotes). */
|
|
51
|
+
function moodForStatus(status) {
|
|
52
|
+
switch (status) {
|
|
53
|
+
case 'healthy':
|
|
54
|
+
return 'happy';
|
|
55
|
+
case 'warning':
|
|
56
|
+
return 'concerned';
|
|
57
|
+
case 'critical':
|
|
58
|
+
return 'stressed';
|
|
59
|
+
default:
|
|
60
|
+
return 'idle';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Validate a warning/critical pair from user flags: both finite, and critical
|
|
65
|
+
* at least as severe as warning for the threshold's direction. Returns an error
|
|
66
|
+
* message, or null when the pair is usable.
|
|
67
|
+
*/
|
|
68
|
+
function thresholdProblem(label, t) {
|
|
69
|
+
if (!Number.isFinite(t.warning) || !Number.isFinite(t.critical)) {
|
|
70
|
+
return `${label}: thresholds must be numbers`;
|
|
71
|
+
}
|
|
72
|
+
const inverted = (t.direction ?? 'above') === 'above' ? t.critical < t.warning : t.critical > t.warning;
|
|
73
|
+
return inverted
|
|
74
|
+
? `${label}: critical (${t.critical}) must be at least as severe as warning (${t.warning})`
|
|
75
|
+
: null;
|
|
76
|
+
}
|
|
48
77
|
//# sourceMappingURL=thresholds.js.map
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.normalizeCursorEvent = exports.isCursorInvocation = exports.CURSOR_HOOK_EVENTS = exports.installCursorHooksInto = exports.uninstallCursorHooks = exports.setupCursorHooks = exports.handleCursorHookEvent = exports.cursorServiceId = exports.CURSOR_ADAPTER = exports.CURSOR_DEFAULTS = exports.GEMINI_HOOK_EVENTS = exports.installGeminiHooksInto = exports.uninstallGeminiHooks = exports.setupGeminiHooks = exports.handleGeminiHookEvent = void 0;
|
|
3
|
+
exports.ReceptionistNPC = exports.LogWatcherNPC = exports.ProcessWatcherNPC = exports.InternetConnectivityNPC = exports.ensureSessionFilterTemplate = exports.sessionPublishDecision = exports.sessionFilterPath = exports.loadSessionFilter = exports.pixelDevice = exports.pixelArtFromPng = exports.encodePixelArt = exports.userConfigPath = exports.saveUserConfig = exports.loadUserConfig = exports.redactSecrets = exports.shortPath = exports.shortCommand = exports.describeToolUse = exports.CodexWatcher = exports.parseRolloutLine = exports.uninstallCodexHooks = exports.setupCodexHooks = exports.codexTransition = exports.handleCodexHookEvent = exports.handleCodexCommand = exports.claudeTransition = exports.processHookEvent = exports.uninstallHooks = exports.setupHooks = exports.handleHookEvent = exports.handleClaudeCodeCommand = exports.ClaudeCodeNPC = exports.SystemMonitorNPC = exports.pushHistory = exports.statusColor = exports.NpcConfigError = exports.sanitizeText = exports.truncate = exports.moodForStatus = exports.worstStatus = exports.evaluateThreshold = exports.numberFlag = exports.boolFlag = exports.stringFlag = exports.resolveConfig = exports.parseArgs = exports.Logger = exports.EventNPC = exports.PollingNPC = exports.BaseNPC = void 0;
|
|
4
|
+
exports.normalizeCursorEvent = exports.isCursorInvocation = exports.CURSOR_HOOK_EVENTS = exports.installCursorHooksInto = exports.uninstallCursorHooks = exports.setupCursorHooks = exports.handleCursorHookEvent = exports.cursorServiceId = exports.CURSOR_ADAPTER = exports.CURSOR_DEFAULTS = exports.GEMINI_HOOK_EVENTS = exports.installGeminiHooksInto = exports.uninstallGeminiHooks = exports.setupGeminiHooks = exports.handleGeminiHookEvent = exports.geminiServiceId = exports.geminiTransition = exports.GEMINI_ADAPTER = exports.GEMINI_DEFAULTS = exports.WebPageWatcherNPC = exports.GitHubWatcherNPC = void 0;
|
|
5
5
|
// Core
|
|
6
6
|
var base_npc_js_1 = require("./core/base-npc.js");
|
|
7
7
|
Object.defineProperty(exports, "BaseNPC", { enumerable: true, get: function () { return base_npc_js_1.BaseNPC; } });
|
|
@@ -20,8 +20,12 @@ Object.defineProperty(exports, "numberFlag", { enumerable: true, get: function (
|
|
|
20
20
|
var thresholds_js_1 = require("./core/thresholds.js");
|
|
21
21
|
Object.defineProperty(exports, "evaluateThreshold", { enumerable: true, get: function () { return thresholds_js_1.evaluateThreshold; } });
|
|
22
22
|
Object.defineProperty(exports, "worstStatus", { enumerable: true, get: function () { return thresholds_js_1.worstStatus; } });
|
|
23
|
+
Object.defineProperty(exports, "moodForStatus", { enumerable: true, get: function () { return thresholds_js_1.moodForStatus; } });
|
|
23
24
|
var text_js_1 = require("./core/text.js");
|
|
24
25
|
Object.defineProperty(exports, "truncate", { enumerable: true, get: function () { return text_js_1.truncate; } });
|
|
26
|
+
Object.defineProperty(exports, "sanitizeText", { enumerable: true, get: function () { return text_js_1.sanitizeText; } });
|
|
27
|
+
var errors_js_1 = require("./core/errors.js");
|
|
28
|
+
Object.defineProperty(exports, "NpcConfigError", { enumerable: true, get: function () { return errors_js_1.NpcConfigError; } });
|
|
25
29
|
var screen_js_1 = require("./core/screen.js");
|
|
26
30
|
Object.defineProperty(exports, "statusColor", { enumerable: true, get: function () { return screen_js_1.statusColor; } });
|
|
27
31
|
Object.defineProperty(exports, "pushHistory", { enumerable: true, get: function () { return screen_js_1.pushHistory; } });
|
|
@@ -53,6 +57,10 @@ var user_config_js_1 = require("./core/user-config.js");
|
|
|
53
57
|
Object.defineProperty(exports, "loadUserConfig", { enumerable: true, get: function () { return user_config_js_1.loadUserConfig; } });
|
|
54
58
|
Object.defineProperty(exports, "saveUserConfig", { enumerable: true, get: function () { return user_config_js_1.saveUserConfig; } });
|
|
55
59
|
Object.defineProperty(exports, "userConfigPath", { enumerable: true, get: function () { return user_config_js_1.userConfigPath; } });
|
|
60
|
+
var pixel_art_js_1 = require("./core/pixel-art.js");
|
|
61
|
+
Object.defineProperty(exports, "encodePixelArt", { enumerable: true, get: function () { return pixel_art_js_1.encodePixelArt; } });
|
|
62
|
+
Object.defineProperty(exports, "pixelArtFromPng", { enumerable: true, get: function () { return pixel_art_js_1.pixelArtFromPng; } });
|
|
63
|
+
Object.defineProperty(exports, "pixelDevice", { enumerable: true, get: function () { return pixel_art_js_1.pixelDevice; } });
|
|
56
64
|
var session_filter_js_1 = require("./core/session-filter.js");
|
|
57
65
|
Object.defineProperty(exports, "loadSessionFilter", { enumerable: true, get: function () { return session_filter_js_1.loadSessionFilter; } });
|
|
58
66
|
Object.defineProperty(exports, "sessionFilterPath", { enumerable: true, get: function () { return session_filter_js_1.sessionFilterPath; } });
|
|
@@ -244,6 +244,7 @@ function buildAgentPayload(adapter, state, config) {
|
|
|
244
244
|
name,
|
|
245
245
|
message: current ? (0, text_js_1.truncate)(current, 120) : undefined,
|
|
246
246
|
archetype: 'ai-agent',
|
|
247
|
+
kind: adapter.command,
|
|
247
248
|
metrics: { sessionStartedAt: state.startedAt ?? state.lastUpdate },
|
|
248
249
|
appearance: { family: adapter.family },
|
|
249
250
|
agentStats: state.agentStats,
|
|
@@ -2,10 +2,16 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DEFAULTS = void 0;
|
|
4
4
|
exports.DEFAULTS = {
|
|
5
|
-
pollInterval: 60000, // 60s
|
|
5
|
+
pollInterval: 60000, // 60s with a token
|
|
6
|
+
/** Without a token GitHub allows 60 requests/hour (304s included): poll every 90s. */
|
|
7
|
+
unauthenticatedPollInterval: 90000,
|
|
6
8
|
fetchTimeoutMs: 5000,
|
|
7
9
|
perPage: 10,
|
|
10
|
+
/** Commit rows on the board (plus a header row). */
|
|
11
|
+
boardRows: 8,
|
|
8
12
|
/** Polls to keep the 'new-commit' sub-status active after a fresh commit. */
|
|
9
13
|
newCommitPolls: 2,
|
|
14
|
+
/** Consecutive network/5xx failures tolerated (board kept, healthy) before warning. */
|
|
15
|
+
failuresBeforeWarning: 3,
|
|
10
16
|
};
|
|
11
17
|
//# sourceMappingURL=defaults.js.map
|