@ulm0/gonogo 0.1.1 → 0.2.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/README.md +5 -11
- package/dist/client/assets/index-DxUnpGbS.js +52 -0
- package/dist/client/assets/index-tpcmltjw.css +1 -0
- package/dist/client/index.html +2 -2
- package/dist/server/audited.js +156 -0
- package/dist/server/blackbox.js +261 -0
- package/dist/server/board.js +123 -0
- package/dist/server/cleanroom.js +7 -51
- package/dist/server/clearance.js +158 -0
- package/dist/server/cli.js +229 -28
- package/dist/server/constants.js +54 -3
- package/dist/server/hangar.js +311 -0
- package/dist/server/home.js +41 -2
- package/dist/server/hub.js +280 -29
- package/dist/server/launcher.js +25 -13
- package/dist/server/logchart.js +58 -0
- package/dist/server/massbudget.js +169 -0
- package/dist/server/mcp.js +73 -2
- package/dist/server/migrate.js +7 -3
- package/dist/server/missionlog.js +2 -0
- package/dist/server/missions.js +310 -0
- package/dist/server/models.js +73 -0
- package/dist/server/phone.js +122 -0
- package/dist/server/phonetag.js +47 -0
- package/dist/server/prices.js +112 -0
- package/dist/server/propellant.js +243 -16
- package/dist/server/providers.js +123 -9
- package/dist/server/recorder.js +176 -0
- package/dist/server/relay.js +53 -7
- package/dist/server/relaykinds.js +11 -1
- package/dist/server/relief.js +226 -0
- package/dist/server/remotecontrol.js +15 -0
- package/dist/server/rollout.js +172 -0
- package/dist/server/tasks.js +44 -8
- package/dist/server/telemetry.js +110 -0
- package/dist/server/uplinks.js +7 -0
- package/dist/server/web.js +310 -10
- package/dist/server/worktree.js +137 -0
- package/dist/shared/fuel.js +8 -0
- package/dist/shared/livery.js +1 -1
- package/dist/shared/positions.js +1 -1
- package/dist/shared/prices.js +75 -0
- package/dist/shared/protocol.js +61 -15
- package/package.json +1 -1
- package/dist/client/assets/index-B6MsSoeo.css +0 -1
- package/dist/client/assets/index-DRtiRh1Y.js +0 -31
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rollout (docs/rollout-spec.md): tells the Director when a newer @ulm0/gonogo exists.
|
|
3
|
+
*
|
|
4
|
+
* Once a day (not more) a plain GET asks registry.npmjs.org for the `latest` version of the package. The request has an Accept
|
|
5
|
+
* header and nothing else of ours: no version, no id, no cookie, nothing about the user or the machine. The answer is read
|
|
6
|
+
* up to a limit, must hold a stable `x.y.z`, and is cached in rollout.json (mode 0600, with the time of the try). Nothing is
|
|
7
|
+
* ever installed: `updateCommand` only says which command updates the way this gonogo was installed. Off with
|
|
8
|
+
* GONOGO_UPDATE_CHECK=0 or --no-update-check; then nothing is asked and no note shows. The clock and fetch are injected for tests.
|
|
9
|
+
*/
|
|
10
|
+
import { randomBytes } from 'node:crypto';
|
|
11
|
+
import { readFileSync } from 'node:fs';
|
|
12
|
+
import { readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
13
|
+
import { join } from 'node:path';
|
|
14
|
+
import { FILE_MODE, nullLogger, ROLLOUT_EVERY_MS, ROLLOUT_FILE, ROLLOUT_MAX_BYTES, ROLLOUT_OFF_WORDS, ROLLOUT_TIMEOUT_MS, ROLLOUT_URL } from './constants.js';
|
|
15
|
+
const STABLE = /^\d{1,6}\.\d{1,6}\.\d{1,6}$/;
|
|
16
|
+
const CHECK_TICK_MS = 60 * 60 * 1000;
|
|
17
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
18
|
+
/** GONOGO_UPDATE_CHECK says no: `0`, `off`, `false` or `no`. */
|
|
19
|
+
export const updateCheckOff = (value) => ROLLOUT_OFF_WORDS.includes((value ?? '').trim().toLowerCase());
|
|
20
|
+
/** `latest` is a stable x.y.z above `current`; a prerelease of the same x.y.z is below it. Anything else is not newer. */
|
|
21
|
+
export function isNewer(latest, current) {
|
|
22
|
+
const next = STABLE.test(latest) ? latest.split('.').map(Number) : null;
|
|
23
|
+
const now = /^(\d+)\.(\d+)\.(\d+)(-|$)/.exec(current);
|
|
24
|
+
if (next === null || now === null)
|
|
25
|
+
return false;
|
|
26
|
+
for (let i = 0; i < 3; i += 1) {
|
|
27
|
+
const have = Number(now[i + 1]);
|
|
28
|
+
if (next[i] !== have)
|
|
29
|
+
return (next[i] ?? 0) > have;
|
|
30
|
+
}
|
|
31
|
+
return now[4] === '-';
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The command that updates the way this gonogo was installed, from the path of its own file. `docker`: it runs in a container;
|
|
35
|
+
* `clone`: its folder is a git checkout. Never run by gonogo.
|
|
36
|
+
* shortcut: an install is told from its path (pnpm's global and dlx folders, npm's lib/node_modules); anything else gets the pnpm global command.
|
|
37
|
+
*/
|
|
38
|
+
export function updateCommand(where) {
|
|
39
|
+
const path = where.file.replace(/\\/g, '/');
|
|
40
|
+
if (where.docker)
|
|
41
|
+
return 'docker compose pull && docker compose up -d';
|
|
42
|
+
if (where.clone)
|
|
43
|
+
return 'git pull && pnpm install && pnpm build';
|
|
44
|
+
if (path.includes('/pnpm/dlx/'))
|
|
45
|
+
return 'pnpm dlx @ulm0/gonogo@latest';
|
|
46
|
+
if (path.includes('/lib/node_modules/@ulm0/gonogo/'))
|
|
47
|
+
return 'npm install -g @ulm0/gonogo@latest';
|
|
48
|
+
return 'pnpm add -g @ulm0/gonogo@latest';
|
|
49
|
+
}
|
|
50
|
+
/** What `gonogo update` prints. */
|
|
51
|
+
export function updateText(command, docker, view) {
|
|
52
|
+
const state = view.newer ? `gonogo ${view.latest} is out; this is ${view.current}.` : `This is gonogo ${view.current}${view.latest === null ? '' : `; the newest the last check saw is ${view.latest}`}.`;
|
|
53
|
+
return [state, 'To update, run:', '', ` ${command}`, '', docker ? 'in the folder of your compose.yaml. gonogo never updates itself.' : 'gonogo never updates itself.'].join('\n') + '\n';
|
|
54
|
+
}
|
|
55
|
+
/** The last check as `gonogo --version` shows it, from the file alone: no network. */
|
|
56
|
+
export function cachedLatest(home) {
|
|
57
|
+
try {
|
|
58
|
+
const saved = JSON.parse(readFileSync(join(home, ROLLOUT_FILE), 'utf8'));
|
|
59
|
+
return isRecord(saved) && typeof saved.latest === 'string' && STABLE.test(saved.latest) ? saved.latest : null;
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** The body of an answer, or null when it is longer than `max` bytes. */
|
|
66
|
+
async function readCapped(res, max) {
|
|
67
|
+
const reader = res.body?.getReader();
|
|
68
|
+
if (reader === undefined)
|
|
69
|
+
return null;
|
|
70
|
+
const chunks = [];
|
|
71
|
+
let size = 0;
|
|
72
|
+
for (;;) {
|
|
73
|
+
const { done, value } = await reader.read();
|
|
74
|
+
if (done)
|
|
75
|
+
break;
|
|
76
|
+
size += value.length;
|
|
77
|
+
if (size > max) {
|
|
78
|
+
await reader.cancel();
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
chunks.push(value);
|
|
82
|
+
}
|
|
83
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
84
|
+
}
|
|
85
|
+
export function createRollout(options) {
|
|
86
|
+
const log = options.log ?? nullLogger;
|
|
87
|
+
const path = join(options.dir, ROLLOUT_FILE);
|
|
88
|
+
let latest = null;
|
|
89
|
+
let checkedAt = null;
|
|
90
|
+
let timer;
|
|
91
|
+
let closed = false;
|
|
92
|
+
let running = null;
|
|
93
|
+
const view = () => ({
|
|
94
|
+
enabled: options.enabled,
|
|
95
|
+
current: options.current,
|
|
96
|
+
latest,
|
|
97
|
+
checkedAt,
|
|
98
|
+
newer: options.enabled && latest !== null && isNewer(latest, options.current),
|
|
99
|
+
command: options.command,
|
|
100
|
+
});
|
|
101
|
+
async function save() {
|
|
102
|
+
const temp = `${path}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`;
|
|
103
|
+
try {
|
|
104
|
+
await writeFile(temp, JSON.stringify({ version: 1, checkedAt, latest }, null, 2), { flag: 'wx', mode: FILE_MODE });
|
|
105
|
+
await rename(temp, path);
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
await rm(temp, { force: true }).catch(() => undefined);
|
|
109
|
+
log.warn(`could not save ${ROLLOUT_FILE}: ${error instanceof Error ? error.message : String(error)}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** One GET. A refusal, a timeout, a long answer or a version that is not x.y.z leaves `latest` as it was; the try still counts. */
|
|
113
|
+
async function ask() {
|
|
114
|
+
try {
|
|
115
|
+
const res = await options.fetch(ROLLOUT_URL, { headers: { accept: 'application/json' }, signal: AbortSignal.timeout(ROLLOUT_TIMEOUT_MS) });
|
|
116
|
+
// a refusal's body is not read: let the connection go
|
|
117
|
+
if (!res.ok)
|
|
118
|
+
await res.body?.cancel().catch(() => undefined);
|
|
119
|
+
const text = res.ok ? await readCapped(res, ROLLOUT_MAX_BYTES) : null;
|
|
120
|
+
const doc = text === null ? null : JSON.parse(text);
|
|
121
|
+
if (isRecord(doc) && typeof doc.version === 'string' && STABLE.test(doc.version))
|
|
122
|
+
latest = doc.version;
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
log.debug(`the update check did not get an answer: ${error instanceof Error ? error.name : 'error'}`);
|
|
126
|
+
}
|
|
127
|
+
checkedAt = options.now();
|
|
128
|
+
await save();
|
|
129
|
+
const done = view();
|
|
130
|
+
if (!closed)
|
|
131
|
+
options.emit(done);
|
|
132
|
+
return done;
|
|
133
|
+
}
|
|
134
|
+
const check = () => (options.enabled ? (running ??= ask().finally(() => (running = null))) : Promise.resolve(view()));
|
|
135
|
+
const due = () => checkedAt === null || options.now() - checkedAt >= ROLLOUT_EVERY_MS || checkedAt > options.now();
|
|
136
|
+
return {
|
|
137
|
+
async load() {
|
|
138
|
+
try {
|
|
139
|
+
const saved = JSON.parse(await readFile(path, 'utf8'));
|
|
140
|
+
if (isRecord(saved) && saved.version === 1) {
|
|
141
|
+
if (typeof saved.latest === 'string' && STABLE.test(saved.latest))
|
|
142
|
+
latest = saved.latest;
|
|
143
|
+
if (typeof saved.checkedAt === 'number' && Number.isFinite(saved.checkedAt))
|
|
144
|
+
checkedAt = saved.checkedAt;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
// no file yet, or one that is not ours: the next check makes it. Nothing in it is a secret.
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
view,
|
|
152
|
+
check,
|
|
153
|
+
start() {
|
|
154
|
+
if (!options.enabled || closed)
|
|
155
|
+
return;
|
|
156
|
+
const tick = () => {
|
|
157
|
+
if (closed)
|
|
158
|
+
return;
|
|
159
|
+
if (due())
|
|
160
|
+
void check();
|
|
161
|
+
timer = setTimeout(tick, CHECK_TICK_MS);
|
|
162
|
+
timer.unref();
|
|
163
|
+
};
|
|
164
|
+
tick();
|
|
165
|
+
},
|
|
166
|
+
async close() {
|
|
167
|
+
closed = true;
|
|
168
|
+
clearTimeout(timer);
|
|
169
|
+
await running?.catch(() => undefined);
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
package/dist/server/tasks.js
CHANGED
|
@@ -22,8 +22,9 @@ import { UNSAFE_IN_NAME } from './schedules.js';
|
|
|
22
22
|
const NAME_RE = new RegExp(AGENT_NAME_PATTERN);
|
|
23
23
|
const STATION_RE = new RegExp(STATION_NAME_PATTERN);
|
|
24
24
|
const ID_RE = /^t[1-9]\d{0,5}$/;
|
|
25
|
-
const
|
|
26
|
-
const
|
|
25
|
+
const MISSION_ID_RE = /^ms[1-9]\d{0,5}$/;
|
|
26
|
+
const CREATE_FIELDS = ['title', 'detail', 'assignee', 'missionId'];
|
|
27
|
+
const PATCH_FIELDS = ['status', 'assignee', 'note', 'missionId'];
|
|
27
28
|
const bad = (message, field) => new HubError('bad_request', message, field);
|
|
28
29
|
const copy = (task) => ({ ...task, notes: [...task.notes] });
|
|
29
30
|
const isOpen = (task) => task.status !== 'done';
|
|
@@ -32,7 +33,7 @@ const stamp = (at) => `${new Date(at).toISOString().slice(0, 16).replace('T', '
|
|
|
32
33
|
const director = (actor) => (actor.kind === 'dock' ? `Director of ${actor.station}` : 'Director');
|
|
33
34
|
const line = (actor, text, at) => `${stamp(at)} ${actor.kind === 'agent' ? actor.name : director(actor)}: ${text}`;
|
|
34
35
|
/** The fields a request may carry, checked one by one. A task is made with a title; a change has at least one field. */
|
|
35
|
-
function validate(input, whole, isAgent) {
|
|
36
|
+
function validate(input, whole, isAgent, hasMission) {
|
|
36
37
|
if (!isRecord(input))
|
|
37
38
|
throw bad('request body must be a JSON object');
|
|
38
39
|
const allowed = whole ? CREATE_FIELDS : PATCH_FIELDS;
|
|
@@ -42,7 +43,7 @@ function validate(input, whole, isAgent) {
|
|
|
42
43
|
if (whole && input.title === undefined)
|
|
43
44
|
throw bad('title is required', 'title');
|
|
44
45
|
if (!whole && Object.keys(input).length === 0)
|
|
45
|
-
throw bad('nothing to change: give a status, an assignee or a note');
|
|
46
|
+
throw bad('nothing to change: give a status, an assignee, a mission or a note');
|
|
46
47
|
const out = {};
|
|
47
48
|
if (input.title !== undefined) {
|
|
48
49
|
const title = typeof input.title === 'string' ? input.title.trim() : '';
|
|
@@ -67,6 +68,14 @@ function validate(input, whole, isAgent) {
|
|
|
67
68
|
}
|
|
68
69
|
out.assignee = input.assignee;
|
|
69
70
|
}
|
|
71
|
+
if (input.missionId !== undefined) {
|
|
72
|
+
if (input.missionId !== null && (typeof input.missionId !== 'string' || !MISSION_ID_RE.test(input.missionId) || !hasMission(input.missionId))) {
|
|
73
|
+
throw bad('missionId must be the id of a mission, or null', 'missionId');
|
|
74
|
+
}
|
|
75
|
+
if (whole && input.missionId === null)
|
|
76
|
+
throw bad('missionId must be the id of a mission', 'missionId');
|
|
77
|
+
out.missionId = input.missionId;
|
|
78
|
+
}
|
|
70
79
|
if (input.note !== undefined) {
|
|
71
80
|
// one line: the history is a list of lines
|
|
72
81
|
const note = typeof input.note === 'string' ? input.note.replace(/\s+/g, ' ').trim() : '';
|
|
@@ -80,7 +89,7 @@ function validate(input, whole, isAgent) {
|
|
|
80
89
|
function fromRecord(raw, at) {
|
|
81
90
|
if (!isRecord(raw) || typeof raw.id !== 'string' || !ID_RE.test(raw.id))
|
|
82
91
|
throw new Error('no valid id');
|
|
83
|
-
const { id, title, detail, status, assignee, createdBy, createdAt, updatedAt, notes, handedOff } = raw;
|
|
92
|
+
const { id, title, detail, status, assignee, createdBy, createdAt, updatedAt, notes, handedOff, missionId } = raw;
|
|
84
93
|
if (typeof title !== 'string' || title === '' || title.length > TASK_TITLE_MAX_CHARS)
|
|
85
94
|
throw new Error('no valid title');
|
|
86
95
|
if (typeof status !== 'string' || !TASK_STATUSES.includes(status))
|
|
@@ -96,6 +105,7 @@ function fromRecord(raw, at) {
|
|
|
96
105
|
updatedAt: typeof updatedAt === 'number' ? updatedAt : at,
|
|
97
106
|
notes: Array.isArray(notes) ? notes.filter((n) => typeof n === 'string').slice(-TASK_NOTES_MAX) : [],
|
|
98
107
|
...(typeof handedOff === 'string' && STATION_RE.test(handedOff) ? { handedOff } : {}),
|
|
108
|
+
...(typeof missionId === 'string' && MISSION_ID_RE.test(missionId) ? { missionId } : {}),
|
|
99
109
|
};
|
|
100
110
|
}
|
|
101
111
|
export function createTasks(options) {
|
|
@@ -118,10 +128,12 @@ export function createTasks(options) {
|
|
|
118
128
|
await rename(tmp, path);
|
|
119
129
|
});
|
|
120
130
|
};
|
|
131
|
+
const hasMission = (id) => options.hasMission?.(id) ?? false;
|
|
121
132
|
const list = () => tasks.map(copy);
|
|
122
133
|
/** Tells the browsers and saves. */
|
|
123
134
|
const changed = () => {
|
|
124
135
|
options.emit(list());
|
|
136
|
+
options.afterChange?.(list());
|
|
125
137
|
return save();
|
|
126
138
|
};
|
|
127
139
|
const find = (id) => {
|
|
@@ -187,7 +199,7 @@ export function createTasks(options) {
|
|
|
187
199
|
},
|
|
188
200
|
list,
|
|
189
201
|
async create(actor, input) {
|
|
190
|
-
const request = validate(input, true, options.isAgent);
|
|
202
|
+
const request = validate(input, true, options.isAgent, hasMission);
|
|
191
203
|
if (tasks.filter(isOpen).length >= TASKS_OPEN_MAX)
|
|
192
204
|
throw full();
|
|
193
205
|
const at = now();
|
|
@@ -201,8 +213,9 @@ export function createTasks(options) {
|
|
|
201
213
|
createdAt: at,
|
|
202
214
|
updatedAt: at,
|
|
203
215
|
notes: [],
|
|
216
|
+
...(request.missionId === undefined || request.missionId === null ? {} : { missionId: request.missionId }),
|
|
204
217
|
};
|
|
205
|
-
task.notes.push(line(actor, task.assignee === null ? 'created' : `created; assigned to ${task.assignee}`, at));
|
|
218
|
+
task.notes.push(line(actor, `${task.assignee === null ? 'created' : `created; assigned to ${task.assignee}`}${task.missionId === undefined ? '' : `; mission ${task.missionId}`}`, at));
|
|
206
219
|
tell(actor, task, at);
|
|
207
220
|
seq += 1;
|
|
208
221
|
tasks.push(task);
|
|
@@ -228,7 +241,7 @@ export function createTasks(options) {
|
|
|
228
241
|
},
|
|
229
242
|
async update(actor, id, input) {
|
|
230
243
|
const task = find(id);
|
|
231
|
-
const patch = validate(input, false, options.isAgent);
|
|
244
|
+
const patch = validate(input, false, options.isAgent, hasMission);
|
|
232
245
|
const at = now();
|
|
233
246
|
const next = copy(task);
|
|
234
247
|
const parts = [];
|
|
@@ -242,6 +255,13 @@ export function createTasks(options) {
|
|
|
242
255
|
next.assignee = patch.assignee;
|
|
243
256
|
parts.push(patch.assignee === null ? 'unassigned' : `assigned to ${patch.assignee}`);
|
|
244
257
|
}
|
|
258
|
+
if (patch.missionId !== undefined && (patch.missionId ?? undefined) !== task.missionId) {
|
|
259
|
+
if (patch.missionId === null)
|
|
260
|
+
delete next.missionId;
|
|
261
|
+
else
|
|
262
|
+
next.missionId = patch.missionId;
|
|
263
|
+
parts.push(patch.missionId === null ? 'unlinked from its mission' : `mission ${patch.missionId}`);
|
|
264
|
+
}
|
|
245
265
|
if (patch.note !== undefined)
|
|
246
266
|
parts.push(patch.note);
|
|
247
267
|
if (parts.length === 0)
|
|
@@ -265,6 +285,22 @@ export function createTasks(options) {
|
|
|
265
285
|
tasks = tasks.filter((x) => x !== task);
|
|
266
286
|
await changed();
|
|
267
287
|
},
|
|
288
|
+
async unlinkMission(missionId) {
|
|
289
|
+
const at = now();
|
|
290
|
+
let any = false;
|
|
291
|
+
tasks = tasks.map((t) => {
|
|
292
|
+
if (t.missionId !== missionId)
|
|
293
|
+
return t;
|
|
294
|
+
any = true;
|
|
295
|
+
const next = copy(t);
|
|
296
|
+
delete next.missionId;
|
|
297
|
+
next.notes.push(line({ kind: 'user' }, `unlinked: mission ${missionId} was removed`, at));
|
|
298
|
+
next.notes = next.notes.slice(-TASK_NOTES_MAX);
|
|
299
|
+
return next;
|
|
300
|
+
});
|
|
301
|
+
if (any)
|
|
302
|
+
await changed();
|
|
303
|
+
},
|
|
268
304
|
close() {
|
|
269
305
|
return chain.then(() => undefined);
|
|
270
306
|
},
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telemetry: the tool calls of each agent, from the hooks gonogo already receives (docs/telemetry-spec.md).
|
|
3
|
+
*
|
|
4
|
+
* A PreToolUse opens a call; the PostToolUse or PostToolUseFailure with the same `tool_use_id` closes it (Gemini CLI sends
|
|
5
|
+
* no id: the oldest open call of the same tool is closed). Each agent keeps its newest TELEMETRY_PER_AGENT calls in memory.
|
|
6
|
+
* A closed call is appended to telemetry.jsonl; at start the file is read back and rewritten with only the kept calls,
|
|
7
|
+
* and during a run it is rewritten the same way once it holds TELEMETRY_FILE_SLACK more lines than were kept.
|
|
8
|
+
*
|
|
9
|
+
* shortcut: calls are keyed by agent name, so a renamed agent starts an empty list and a removed agent's calls stay on disk
|
|
10
|
+
* (at most TELEMETRY_PER_AGENT per name); key by record id and drop them on remove if that ever matters.
|
|
11
|
+
*/
|
|
12
|
+
import { appendFile, readFile, writeFile } from 'node:fs/promises';
|
|
13
|
+
import { join } from 'node:path';
|
|
14
|
+
import { TELEMETRY_PER_AGENT } from '../shared/protocol.js';
|
|
15
|
+
import { FILE_MODE } from './constants.js';
|
|
16
|
+
import { describeDoing } from './hub.js';
|
|
17
|
+
const FILE = 'telemetry.jsonl';
|
|
18
|
+
/** Lines appended since the last rewrite before the file is cut back to the kept calls. */
|
|
19
|
+
export const TELEMETRY_FILE_SLACK = 2000;
|
|
20
|
+
const str = (v) => (typeof v === 'string' && v !== '' ? v : null);
|
|
21
|
+
function isCall(v) {
|
|
22
|
+
if (typeof v !== 'object' || v === null)
|
|
23
|
+
return false;
|
|
24
|
+
const c = v;
|
|
25
|
+
return typeof c.agent === 'string' && typeof c.at === 'number' && (c.ms === null || typeof c.ms === 'number') && typeof c.tool === 'string' && typeof c.summary === 'string' && typeof c.failed === 'boolean';
|
|
26
|
+
}
|
|
27
|
+
const plain = (c) => ({ at: c.at, ms: c.ms, tool: c.tool, summary: c.summary, failed: c.failed });
|
|
28
|
+
export function createTelemetry(options) {
|
|
29
|
+
const { now, log } = options;
|
|
30
|
+
const path = join(options.dir, FILE);
|
|
31
|
+
/** Agent to its calls, oldest first, at most TELEMETRY_PER_AGENT. */
|
|
32
|
+
const byAgent = new Map();
|
|
33
|
+
let appended = 0;
|
|
34
|
+
let chain = Promise.resolve();
|
|
35
|
+
const later = (step) => {
|
|
36
|
+
chain = chain.then(step).catch((error) => log.warn(`${FILE} could not be written`, error));
|
|
37
|
+
};
|
|
38
|
+
const push = (agent, call) => {
|
|
39
|
+
const list = byAgent.get(agent) ?? [];
|
|
40
|
+
list.push(call);
|
|
41
|
+
if (list.length > TELEMETRY_PER_AGENT)
|
|
42
|
+
list.splice(0, list.length - TELEMETRY_PER_AGENT);
|
|
43
|
+
byAgent.set(agent, list);
|
|
44
|
+
};
|
|
45
|
+
const snapshot = () => [...byAgent].flatMap(([agent, list]) => list.filter((c) => c.ms !== null).map((c) => JSON.stringify({ agent, ...plain(c) }) + '\n')).join('');
|
|
46
|
+
return {
|
|
47
|
+
async load() {
|
|
48
|
+
let text = '';
|
|
49
|
+
try {
|
|
50
|
+
text = await readFile(path, 'utf8');
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (error.code !== 'ENOENT')
|
|
54
|
+
log.warn(`${FILE} could not be read; telemetry starts empty`, error);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const read = [];
|
|
58
|
+
for (const line of text.split('\n')) {
|
|
59
|
+
if (line === '')
|
|
60
|
+
continue;
|
|
61
|
+
try {
|
|
62
|
+
const v = JSON.parse(line);
|
|
63
|
+
if (isCall(v))
|
|
64
|
+
read.push(v);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// a torn last line after a crash: skipped
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// lines are written when a call ends; the list is in start order
|
|
71
|
+
for (const v of read.sort((x, y) => x.at - y.at))
|
|
72
|
+
push(v.agent, { at: v.at, ms: v.ms, tool: v.tool, summary: v.summary, failed: v.failed, id: null });
|
|
73
|
+
const kept = snapshot();
|
|
74
|
+
if (kept !== text)
|
|
75
|
+
later(() => writeFile(path, kept, { mode: FILE_MODE }));
|
|
76
|
+
},
|
|
77
|
+
hook(agent, event, payload) {
|
|
78
|
+
const tool = str(payload.tool_name) ?? 'tool';
|
|
79
|
+
const id = str(payload.tool_use_id);
|
|
80
|
+
if (event === 'PreToolUse') {
|
|
81
|
+
push(agent, { id, at: now(), ms: null, tool, summary: describeDoing(payload.tool_name, payload.tool_input), failed: false });
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (event !== 'PostToolUse' && event !== 'PostToolUseFailure')
|
|
85
|
+
return;
|
|
86
|
+
const list = byAgent.get(agent) ?? [];
|
|
87
|
+
const call = id !== null ? list.find((c) => c.id === id && c.ms === null) : list.find((c) => c.ms === null && c.tool === tool);
|
|
88
|
+
if (call === undefined)
|
|
89
|
+
return;
|
|
90
|
+
call.ms = Math.max(0, now() - call.at);
|
|
91
|
+
call.failed = event === 'PostToolUseFailure';
|
|
92
|
+
appended += 1;
|
|
93
|
+
if (appended >= TELEMETRY_FILE_SLACK) {
|
|
94
|
+
appended = 0;
|
|
95
|
+
const kept = snapshot();
|
|
96
|
+
later(() => writeFile(path, kept, { mode: FILE_MODE }));
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
const line = JSON.stringify({ agent, ...plain(call) }) + '\n';
|
|
100
|
+
later(() => appendFile(path, line, { mode: FILE_MODE }));
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
calls(agent) {
|
|
104
|
+
return (byAgent.get(agent) ?? []).map(plain).reverse();
|
|
105
|
+
},
|
|
106
|
+
close() {
|
|
107
|
+
return chain;
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
package/dist/server/uplinks.js
CHANGED
|
@@ -252,6 +252,7 @@ export function createUplinks(options) {
|
|
|
252
252
|
}
|
|
253
253
|
},
|
|
254
254
|
list,
|
|
255
|
+
secretOf: (kind) => uplinks.find((u) => u.kind === kind && u.enabled)?.secret ?? null,
|
|
255
256
|
async create(input) {
|
|
256
257
|
const request = validate(input, true);
|
|
257
258
|
const kind = request.kind ?? 'generic';
|
|
@@ -330,6 +331,12 @@ export function createUplinks(options) {
|
|
|
330
331
|
// before the rate limit: what the agents never hear of (chatter in a channel) must not use up the deliveries of a minute
|
|
331
332
|
if (made === null)
|
|
332
333
|
return IGNORED;
|
|
334
|
+
// a button is not a message: it has its own limit (phone.ts), and the answer to Telegram is plain 200
|
|
335
|
+
if ('callback' in made) {
|
|
336
|
+
record(u, 'answered', 'a button of the phone was pressed');
|
|
337
|
+
options.press?.(u.secret, made.callback);
|
|
338
|
+
return { ok: true, answer: { ok: true } };
|
|
339
|
+
}
|
|
333
340
|
if ('answer' in made) {
|
|
334
341
|
record(u, 'answered', made.note);
|
|
335
342
|
return { ok: true, answer: made.answer };
|