@phnx-labs/agents-cli 1.23.12 → 1.23.14
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/CHANGELOG.md +42 -0
- package/README.md +30 -59
- package/dist/cli/command-registry.js +0 -24
- package/dist/commands/accounts.js +3 -3
- package/dist/commands/daemon.js +5 -147
- package/dist/commands/doctor.js +7 -0
- package/dist/commands/events-logs.d.ts +2 -0
- package/dist/commands/{logs.js → events-logs.js} +28 -127
- package/dist/commands/events.js +3 -1
- package/dist/commands/exec.d.ts +0 -1
- package/dist/commands/exec.js +3 -11
- package/dist/commands/feed.d.ts +1 -1
- package/dist/commands/feed.js +4 -7
- package/dist/commands/mcp.js +1 -1
- package/dist/commands/menubar.js +12 -226
- package/dist/commands/projects.js +1 -2
- package/dist/commands/{trash.d.ts → prune-trash.d.ts} +1 -1
- package/dist/commands/{trash.js → prune-trash.js} +8 -8
- package/dist/commands/prune.js +3 -1
- package/dist/commands/send.js +2 -5
- package/dist/commands/sessions.js +1 -1
- package/dist/commands/setup.js +1 -1
- package/dist/commands/ssh.js +2 -2
- package/dist/commands/utils.d.ts +1 -5
- package/dist/commands/utils.js +1 -7
- package/dist/lib/agent-spec/agents.d.ts +0 -2
- package/dist/lib/agent-spec/agents.js +0 -12
- package/dist/lib/agent-spec/index.d.ts +0 -4
- package/dist/lib/agent-spec/index.js +0 -4
- package/dist/lib/commands.d.ts +1 -5
- package/dist/lib/commands.js +2 -69
- package/dist/lib/comms-render.d.ts +0 -7
- package/dist/lib/comms-render.js +0 -41
- package/dist/lib/config-keys.d.ts +1 -1
- package/dist/lib/config-keys.js +8 -0
- package/dist/lib/daemon/daemon.js +1 -6
- package/dist/lib/daemon-services.d.ts +1 -1
- package/dist/lib/daemon-services.js +0 -5
- package/dist/lib/device-config.d.ts +2 -0
- package/dist/lib/device-config.js +81 -8
- package/dist/lib/devices/doctor-findings.d.ts +6 -1
- package/dist/lib/devices/doctor-findings.js +34 -1
- package/dist/lib/git.d.ts +1 -0
- package/dist/lib/git.js +5 -0
- package/dist/lib/hooks/install.d.ts +0 -7
- package/dist/lib/hooks/install.js +0 -55
- package/dist/lib/hosts/passthrough.js +2 -4
- package/dist/lib/hosts/progress.js +2 -2
- package/dist/lib/hosts/remote-cmd.js +0 -1
- package/dist/lib/installations/migrate.d.ts +1 -0
- package/dist/lib/installations/migrate.js +18 -1
- package/dist/lib/installations/versions.js +2 -2
- package/dist/lib/loop.js +1 -1
- package/dist/lib/mailbox-target.d.ts +0 -31
- package/dist/lib/mailbox-target.js +0 -37
- package/dist/lib/mcp-lookup.d.ts +7 -0
- package/dist/lib/mcp-lookup.js +71 -0
- package/dist/lib/mcp.d.ts +0 -8
- package/dist/lib/mcp.js +0 -47
- package/dist/lib/menubar/install-menubar.d.ts +0 -15
- package/dist/lib/menubar/install-menubar.js +24 -118
- package/dist/lib/menubar/resolve-version.d.ts +9 -10
- package/dist/lib/menubar/resolve-version.js +70 -30
- package/dist/lib/paths.d.ts +0 -1
- package/dist/lib/paths.js +0 -18
- package/dist/lib/plugins/skills.d.ts +0 -10
- package/dist/lib/plugins/skills.js +2 -165
- package/dist/lib/project-pull.js +1 -2
- package/dist/lib/resources.d.ts +1 -1
- package/dist/lib/routines-project.js +1 -1
- package/dist/lib/session/local-tmux-attach.js +2 -2
- package/dist/lib/startup/command-registry.js +20 -7
- package/dist/lib/teams/agents.js +1 -1
- package/dist/lib/triggers/webhook.d.ts +2 -59
- package/dist/lib/triggers/webhook.js +2 -422
- package/package.json +2 -1
- package/dist/commands/funnel.d.ts +0 -2
- package/dist/commands/funnel.js +0 -77
- package/dist/commands/logs.d.ts +0 -2
- package/dist/commands/mailboxes.d.ts +0 -2
- package/dist/commands/mailboxes.js +0 -400
- package/dist/commands/message.d.ts +0 -2
- package/dist/commands/message.js +0 -259
- package/dist/commands/packages-materialize.d.ts +0 -6
- package/dist/commands/packages-materialize.js +0 -90
- package/dist/commands/packages.d.ts +0 -5
- package/dist/commands/packages.js +0 -763
- package/dist/commands/route.d.ts +0 -2
- package/dist/commands/route.js +0 -259
- package/dist/commands/testdata/feed-hub.d.ts +0 -1
- package/dist/commands/testdata/feed-hub.js +0 -11
- package/dist/commands/tmux.d.ts +0 -7
- package/dist/commands/tmux.js +0 -529
- package/dist/commands/traces.d.ts +0 -2
- package/dist/commands/traces.js +0 -204
- package/dist/commands/webhook.d.ts +0 -5
- package/dist/commands/webhook.js +0 -105
- package/dist/lib/agent-spec/materialize.d.ts +0 -3
- package/dist/lib/agent-spec/materialize.js +0 -277
- package/dist/lib/agent-spec/package-resolve.d.ts +0 -4
- package/dist/lib/agent-spec/package-resolve.js +0 -231
- package/dist/lib/agent-spec/package-schema.d.ts +0 -3
- package/dist/lib/agent-spec/package-schema.js +0 -136
- package/dist/lib/agent-spec/package-types.d.ts +0 -94
- package/dist/lib/agent-spec/package-types.js +0 -10
- package/dist/lib/daemon/testdata/process-view-daemon.d.ts +0 -1
- package/dist/lib/daemon/testdata/process-view-daemon.js +0 -12
- package/dist/lib/daemon/testdata/process-view-start.d.ts +0 -1
- package/dist/lib/daemon/testdata/process-view-start.js +0 -41
- package/dist/lib/daemon/webhook-receiver-service.d.ts +0 -8
- package/dist/lib/daemon/webhook-receiver-service.js +0 -16
- package/dist/lib/daemon-webhooks.d.ts +0 -48
- package/dist/lib/daemon-webhooks.js +0 -177
- package/dist/lib/funnel.d.ts +0 -6
- package/dist/lib/funnel.js +0 -31
- package/dist/lib/net-close.d.ts +0 -2
- package/dist/lib/net-close.js +0 -20
- package/dist/lib/packages/output-home.d.ts +0 -9
- package/dist/lib/packages/output-home.js +0 -89
- package/dist/lib/registry.d.ts +0 -65
- package/dist/lib/registry.js +0 -432
- package/dist/lib/routers.d.ts +0 -25
- package/dist/lib/routers.js +0 -122
- package/dist/lib/run-trace-sync.d.ts +0 -7
- package/dist/lib/run-trace-sync.js +0 -35
- package/dist/lib/session/testdata/pid-registry-namespace.d.ts +0 -1
- package/dist/lib/session/testdata/pid-registry-namespace.js +0 -25
- package/dist/lib/session/testdata/process-view-namespace.d.ts +0 -1
- package/dist/lib/session/testdata/process-view-namespace.js +0 -56
- package/dist/lib/session/testdata/process-view-private.d.ts +0 -1
- package/dist/lib/session/testdata/process-view-private.js +0 -11
- package/dist/lib/testdata/daemon-health-writer.d.ts +0 -1
- package/dist/lib/testdata/daemon-health-writer.js +0 -8
- package/dist/lib/testdata/owner-notify-api.d.ts +0 -23
- package/dist/lib/testdata/owner-notify-api.js +0 -82
- package/dist/lib/testdata/refresh-coordinator-worker.d.ts +0 -1
- package/dist/lib/testdata/refresh-coordinator-worker.js +0 -23
- package/dist/lib/traces/backend.d.ts +0 -8
- package/dist/lib/traces/backend.js +0 -33
- package/dist/lib/traces/classify.d.ts +0 -40
- package/dist/lib/traces/classify.js +0 -70
- package/dist/lib/traces/config.d.ts +0 -2
- package/dist/lib/traces/config.js +0 -2
- package/dist/lib/traces/insights.d.ts +0 -30
- package/dist/lib/traces/insights.js +0 -198
- package/dist/lib/traces/phenotype.d.ts +0 -18
- package/dist/lib/traces/phenotype.js +0 -335
- package/dist/lib/traces/provision.d.ts +0 -16
- package/dist/lib/traces/provision.js +0 -25
- package/dist/lib/traces/schema2-build.d.ts +0 -28
- package/dist/lib/traces/schema2-build.js +0 -496
- package/dist/lib/traces/schema2-danger.d.ts +0 -7
- package/dist/lib/traces/schema2-danger.js +0 -123
- package/dist/lib/traces/schema2.d.ts +0 -139
- package/dist/lib/traces/schema2.js +0 -20
- package/dist/lib/traces/segments.d.ts +0 -73
- package/dist/lib/traces/segments.js +0 -236
- package/dist/lib/traces/sync.d.ts +0 -197
- package/dist/lib/traces/sync.js +0 -654
- package/dist/lib/traces/worker-template.d.ts +0 -1
- package/dist/lib/traces/worker-template.js +0 -292
- package/dist/lib/triggers/handlers.d.ts +0 -66
- package/dist/lib/triggers/handlers.js +0 -383
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import * as http from 'http';
|
|
4
|
-
import * as path from 'path';
|
|
5
|
-
import { jobRunsOnThisDevice, listJobs, substituteWebhookPrompt } from '../scheduling/routines.js';
|
|
1
|
+
import { jobRunsOnThisDevice, listJobs } from '../scheduling/routines.js';
|
|
6
2
|
import { executeJobDetached } from '../daemon/runner.js';
|
|
7
3
|
import { emit } from '../feed/events.js';
|
|
8
|
-
import { listHandlers, handlerMatchesWebhook, executeHandler, buildWebhookContext, } from './handlers.js';
|
|
9
4
|
export function webhookRepo(payload) {
|
|
10
5
|
const repo = payload?.repository;
|
|
11
6
|
const fullName = repo?.full_name;
|
|
@@ -165,22 +160,15 @@ class WebhookDispatchError extends Error {
|
|
|
165
160
|
export async function fireWebhookJobs(webhook, options = {}) {
|
|
166
161
|
const jobs = options.jobs ?? listJobs();
|
|
167
162
|
const dispatch = options.dispatch ?? executeJobDetached;
|
|
168
|
-
const skipJobNames = options.skipJobNames ?? new Set();
|
|
169
163
|
const matched = matchJobsToWebhook(jobs, webhook);
|
|
170
164
|
const fired = [];
|
|
171
165
|
const failures = [];
|
|
172
166
|
for (const job of matched) {
|
|
173
|
-
if (skipJobNames.has(job.name))
|
|
174
|
-
continue;
|
|
175
167
|
emit('webhook.matched', { source: webhook.source, event: webhook.event, jobName: job.name });
|
|
176
168
|
try {
|
|
177
|
-
const
|
|
178
|
-
? { ...job, prompt: substituteWebhookPrompt(job.prompt, options.context) }
|
|
179
|
-
: job;
|
|
180
|
-
const meta = await dispatch(jobToDispatch);
|
|
169
|
+
const meta = await dispatch(job);
|
|
181
170
|
const firedJob = { jobName: job.name, runId: meta.runId };
|
|
182
171
|
fired.push(firedJob);
|
|
183
|
-
options.onJobFired?.(job, firedJob);
|
|
184
172
|
}
|
|
185
173
|
catch (err) {
|
|
186
174
|
failures.push({ jobName: job.name, error: err });
|
|
@@ -191,411 +179,3 @@ export async function fireWebhookJobs(webhook, options = {}) {
|
|
|
191
179
|
}
|
|
192
180
|
return fired;
|
|
193
181
|
}
|
|
194
|
-
function header(headers, name) {
|
|
195
|
-
const value = headers[name.toLowerCase()];
|
|
196
|
-
if (Array.isArray(value))
|
|
197
|
-
return value[0];
|
|
198
|
-
return value;
|
|
199
|
-
}
|
|
200
|
-
function timingSafeHexEqual(received, expected) {
|
|
201
|
-
if (!received || !/^[a-f0-9]+$/i.test(received))
|
|
202
|
-
return false;
|
|
203
|
-
const a = Buffer.from(received, 'hex');
|
|
204
|
-
const b = Buffer.from(expected, 'hex');
|
|
205
|
-
return a.length === b.length && crypto.timingSafeEqual(a, b);
|
|
206
|
-
}
|
|
207
|
-
function hmacHex(secret, rawBody) {
|
|
208
|
-
return crypto.createHmac('sha256', secret).update(rawBody).digest('hex');
|
|
209
|
-
}
|
|
210
|
-
export function verifyGithubSignature(headers, rawBody, secret) {
|
|
211
|
-
const received = header(headers, 'x-hub-signature-256');
|
|
212
|
-
const signature = received?.startsWith('sha256=') ? received.slice('sha256='.length) : undefined;
|
|
213
|
-
return timingSafeHexEqual(signature, hmacHex(secret, rawBody));
|
|
214
|
-
}
|
|
215
|
-
export function verifyLinearSignature(headers, rawBody, secret) {
|
|
216
|
-
return timingSafeHexEqual(header(headers, 'linear-signature'), hmacHex(secret, rawBody));
|
|
217
|
-
}
|
|
218
|
-
function verifyLinearTimestamp(payload, now = Date.now(), toleranceMs = 60_000) {
|
|
219
|
-
const ts = payload.webhookTimestamp;
|
|
220
|
-
return typeof ts === 'number' && Math.abs(now - ts) <= toleranceMs;
|
|
221
|
-
}
|
|
222
|
-
export function verifySlackSignature(headers, rawBody, secret, now = Date.now(), toleranceSec = 300) {
|
|
223
|
-
const ts = header(headers, 'x-slack-request-timestamp');
|
|
224
|
-
if (!ts || !/^\d+$/.test(ts))
|
|
225
|
-
return false;
|
|
226
|
-
if (Math.abs(Math.floor(now / 1000) - Number(ts)) > toleranceSec)
|
|
227
|
-
return false;
|
|
228
|
-
const received = header(headers, 'x-slack-signature');
|
|
229
|
-
const signature = received?.startsWith('v0=') ? received.slice('v0='.length) : undefined;
|
|
230
|
-
const base = Buffer.concat([Buffer.from(`v0:${ts}:`, 'utf-8'), rawBody]);
|
|
231
|
-
const expected = crypto.createHmac('sha256', secret).update(base).digest('hex');
|
|
232
|
-
return timingSafeHexEqual(signature, expected);
|
|
233
|
-
}
|
|
234
|
-
export function parseSlackBody(contentType, rawBody) {
|
|
235
|
-
if ((contentType ?? '').includes('application/x-www-form-urlencoded')) {
|
|
236
|
-
const form = new URLSearchParams(rawBody.toString('utf-8'));
|
|
237
|
-
return {
|
|
238
|
-
type: 'slash_command',
|
|
239
|
-
command: form.get('command') ?? undefined,
|
|
240
|
-
text: form.get('text') ?? undefined,
|
|
241
|
-
channel: form.get('channel_id') ?? undefined,
|
|
242
|
-
user: form.get('user_id') ?? undefined,
|
|
243
|
-
response_url: form.get('response_url') ?? undefined,
|
|
244
|
-
team: form.get('team_id') ?? undefined,
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
const json = rawBody.length > 0 ? JSON.parse(rawBody.toString('utf-8')) : {};
|
|
248
|
-
const type = String(json.type ?? '');
|
|
249
|
-
if (type === 'url_verification') {
|
|
250
|
-
return { type, challenge: typeof json.challenge === 'string' ? json.challenge : '' };
|
|
251
|
-
}
|
|
252
|
-
const event = (json.event ?? {});
|
|
253
|
-
return {
|
|
254
|
-
type: type || 'event_callback',
|
|
255
|
-
event_id: typeof json.event_id === 'string' ? json.event_id : undefined,
|
|
256
|
-
event_type: typeof event.type === 'string' ? event.type : undefined,
|
|
257
|
-
text: typeof event.text === 'string' ? event.text : undefined,
|
|
258
|
-
channel: typeof event.channel === 'string' ? event.channel : undefined,
|
|
259
|
-
thread_ts: typeof event.thread_ts === 'string'
|
|
260
|
-
? event.thread_ts
|
|
261
|
-
: typeof event.ts === 'string'
|
|
262
|
-
? event.ts
|
|
263
|
-
: undefined,
|
|
264
|
-
user: typeof event.user === 'string' ? event.user : undefined,
|
|
265
|
-
team: typeof json.team_id === 'string' ? json.team_id : undefined,
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
export function slackEventName(payload) {
|
|
269
|
-
if (payload.type === 'slash_command')
|
|
270
|
-
return payload.command ?? 'slash_command';
|
|
271
|
-
return payload.event_type ?? payload.type;
|
|
272
|
-
}
|
|
273
|
-
function createMemoryDeliveryStore(maxEntries = 1000) {
|
|
274
|
-
const seen = new Map();
|
|
275
|
-
const touch = (id) => {
|
|
276
|
-
let current = seen.get(id);
|
|
277
|
-
if (!current) {
|
|
278
|
-
current = { complete: false, jobs: new Set(), updatedAt: Date.now() };
|
|
279
|
-
seen.set(id, current);
|
|
280
|
-
}
|
|
281
|
-
current.updatedAt = Date.now();
|
|
282
|
-
while (seen.size > maxEntries) {
|
|
283
|
-
let oldestId = null;
|
|
284
|
-
let oldestAt = Number.POSITIVE_INFINITY;
|
|
285
|
-
for (const [key, value] of seen) {
|
|
286
|
-
if (value.updatedAt < oldestAt) {
|
|
287
|
-
oldestAt = value.updatedAt;
|
|
288
|
-
oldestId = key;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
if (!oldestId)
|
|
292
|
-
break;
|
|
293
|
-
seen.delete(oldestId);
|
|
294
|
-
}
|
|
295
|
-
return current;
|
|
296
|
-
};
|
|
297
|
-
return {
|
|
298
|
-
seen: (id) => seen.get(id)?.complete === true,
|
|
299
|
-
mark: (id) => {
|
|
300
|
-
touch(id).complete = true;
|
|
301
|
-
},
|
|
302
|
-
completedJobs: (id) => new Set(seen.get(id)?.jobs ?? []),
|
|
303
|
-
markJob: (id, jobName) => {
|
|
304
|
-
touch(id).jobs.add(jobName);
|
|
305
|
-
},
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
export function createFileDeliveryStore(filePath, retentionMs = 14 * 24 * 60 * 60 * 1000) {
|
|
309
|
-
const seen = new Map();
|
|
310
|
-
try {
|
|
311
|
-
const raw = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
312
|
-
const loadedAt = Date.now();
|
|
313
|
-
for (const [id, entry] of Object.entries(raw)) {
|
|
314
|
-
if (typeof entry?.updatedAt !== 'number' || loadedAt - entry.updatedAt > retentionMs)
|
|
315
|
-
continue;
|
|
316
|
-
seen.set(id, {
|
|
317
|
-
complete: entry.complete === true,
|
|
318
|
-
jobs: new Set(Array.isArray(entry.jobs) ? entry.jobs : []),
|
|
319
|
-
updatedAt: entry.updatedAt,
|
|
320
|
-
});
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
catch {
|
|
324
|
-
}
|
|
325
|
-
const persist = () => {
|
|
326
|
-
const snapshot = {};
|
|
327
|
-
for (const [id, entry] of seen) {
|
|
328
|
-
snapshot[id] = { complete: entry.complete, jobs: [...entry.jobs], updatedAt: entry.updatedAt };
|
|
329
|
-
}
|
|
330
|
-
try {
|
|
331
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
332
|
-
const tmp = `${filePath}.${process.pid}.tmp`;
|
|
333
|
-
fs.writeFileSync(tmp, JSON.stringify(snapshot), 'utf-8');
|
|
334
|
-
fs.renameSync(tmp, filePath);
|
|
335
|
-
}
|
|
336
|
-
catch {
|
|
337
|
-
}
|
|
338
|
-
};
|
|
339
|
-
const prune = (now) => {
|
|
340
|
-
for (const [id, entry] of seen) {
|
|
341
|
-
if (now - entry.updatedAt > retentionMs)
|
|
342
|
-
seen.delete(id);
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
const touch = (id) => {
|
|
346
|
-
const now = Date.now();
|
|
347
|
-
prune(now);
|
|
348
|
-
let current = seen.get(id);
|
|
349
|
-
if (!current) {
|
|
350
|
-
current = { complete: false, jobs: new Set(), updatedAt: now };
|
|
351
|
-
seen.set(id, current);
|
|
352
|
-
}
|
|
353
|
-
current.updatedAt = now;
|
|
354
|
-
return current;
|
|
355
|
-
};
|
|
356
|
-
return {
|
|
357
|
-
seen: (id) => {
|
|
358
|
-
const entry = seen.get(id);
|
|
359
|
-
if (!entry)
|
|
360
|
-
return false;
|
|
361
|
-
if (Date.now() - entry.updatedAt > retentionMs)
|
|
362
|
-
return false;
|
|
363
|
-
return entry.complete === true;
|
|
364
|
-
},
|
|
365
|
-
mark: (id) => {
|
|
366
|
-
touch(id).complete = true;
|
|
367
|
-
persist();
|
|
368
|
-
},
|
|
369
|
-
completedJobs: (id) => new Set(seen.get(id)?.jobs ?? []),
|
|
370
|
-
markJob: (id, jobName) => {
|
|
371
|
-
touch(id).jobs.add(jobName);
|
|
372
|
-
persist();
|
|
373
|
-
},
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
function createMemoryRateLimiter(limit, windowMs) {
|
|
377
|
-
const buckets = new Map();
|
|
378
|
-
return {
|
|
379
|
-
take: (key) => {
|
|
380
|
-
const now = Date.now();
|
|
381
|
-
const current = buckets.get(key);
|
|
382
|
-
if (!current || now >= current.resetAt) {
|
|
383
|
-
buckets.set(key, { resetAt: now + windowMs, count: 1 });
|
|
384
|
-
return true;
|
|
385
|
-
}
|
|
386
|
-
if (current.count >= limit)
|
|
387
|
-
return false;
|
|
388
|
-
current.count += 1;
|
|
389
|
-
return true;
|
|
390
|
-
},
|
|
391
|
-
};
|
|
392
|
-
}
|
|
393
|
-
function deliveryId(source, headers, rawBody) {
|
|
394
|
-
const named = source === 'github'
|
|
395
|
-
? header(headers, 'x-github-delivery')
|
|
396
|
-
: header(headers, 'linear-delivery');
|
|
397
|
-
return `${source}:${named ?? crypto.createHash('sha256').update(rawBody).digest('hex')}`;
|
|
398
|
-
}
|
|
399
|
-
function sourceFromPath(pathname) {
|
|
400
|
-
const match = /^\/hooks\/(github|linear|slack)\/?$/.exec(pathname ?? '');
|
|
401
|
-
return match ? match[1] : null;
|
|
402
|
-
}
|
|
403
|
-
async function readRawBody(req, maxBytes) {
|
|
404
|
-
const chunks = [];
|
|
405
|
-
let total = 0;
|
|
406
|
-
for await (const chunk of req) {
|
|
407
|
-
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
408
|
-
total += buf.length;
|
|
409
|
-
if (total > maxBytes)
|
|
410
|
-
throw new Error(`payload exceeds ${maxBytes} bytes`);
|
|
411
|
-
chunks.push(buf);
|
|
412
|
-
}
|
|
413
|
-
return Buffer.concat(chunks);
|
|
414
|
-
}
|
|
415
|
-
export function waitForListening(server) {
|
|
416
|
-
if (server.listening)
|
|
417
|
-
return Promise.resolve();
|
|
418
|
-
return new Promise((resolve, reject) => {
|
|
419
|
-
const cleanup = () => {
|
|
420
|
-
server.off('listening', onListening);
|
|
421
|
-
server.off('error', onError);
|
|
422
|
-
};
|
|
423
|
-
const onListening = () => { cleanup(); resolve(); };
|
|
424
|
-
const onError = (err) => { cleanup(); reject(err); };
|
|
425
|
-
server.once('listening', onListening);
|
|
426
|
-
server.once('error', onError);
|
|
427
|
-
});
|
|
428
|
-
}
|
|
429
|
-
export function startWebhookServer(options) {
|
|
430
|
-
const deliveryStore = options.deliveryStore ?? createMemoryDeliveryStore();
|
|
431
|
-
const inFlight = new Set();
|
|
432
|
-
const rateLimiter = options.rateLimiter ?? createMemoryRateLimiter(options.rateLimitPerMinute ?? 60, 60_000);
|
|
433
|
-
const ipRateLimiter = options.ipRateLimiter ?? createMemoryRateLimiter(options.ipRateLimitPerMinute ?? 120, 60_000);
|
|
434
|
-
const maxBodyBytes = options.maxBodyBytes ?? 1024 * 1024;
|
|
435
|
-
async function settleDelivery(webhook, id) {
|
|
436
|
-
const { source, event: webhookEvent } = webhook;
|
|
437
|
-
try {
|
|
438
|
-
const context = buildWebhookContext(webhook);
|
|
439
|
-
const fireOptions = options.fire ?? {};
|
|
440
|
-
const matchedJobs = matchJobsToWebhook(fireOptions.jobs ?? listJobs(), webhook);
|
|
441
|
-
const matchedHandlers = listHandlers().filter((handler) => handlerMatchesWebhook(handler, webhook));
|
|
442
|
-
options.onMatch?.(webhook, matchedJobs.map((job) => job.name), matchedHandlers.map((handler) => handler.name));
|
|
443
|
-
const firedJobs = await fireWebhookJobs(webhook, {
|
|
444
|
-
...fireOptions,
|
|
445
|
-
jobs: matchedJobs,
|
|
446
|
-
context,
|
|
447
|
-
skipJobNames: deliveryStore.completedJobs(id),
|
|
448
|
-
onJobFired: (job, firedJob) => {
|
|
449
|
-
emit('webhook.fired', { source, event: webhookEvent, deliveryId: id, jobName: job.name, runId: firedJob.runId });
|
|
450
|
-
deliveryStore.markJob(id, job.name);
|
|
451
|
-
fireOptions.onJobFired?.(job, firedJob);
|
|
452
|
-
},
|
|
453
|
-
});
|
|
454
|
-
const firedHandlers = [];
|
|
455
|
-
const handlerErrors = [];
|
|
456
|
-
await Promise.allSettled(matchedHandlers.map(async (handler) => {
|
|
457
|
-
if (deliveryStore.completedJobs(id).has(handler.name))
|
|
458
|
-
return;
|
|
459
|
-
emit('webhook.matched', { source, event: webhookEvent, deliveryId: id, handlerName: handler.name });
|
|
460
|
-
try {
|
|
461
|
-
const result = await executeHandler(handler, webhook);
|
|
462
|
-
deliveryStore.markJob(id, handler.name);
|
|
463
|
-
firedHandlers.push(result);
|
|
464
|
-
}
|
|
465
|
-
catch (err) {
|
|
466
|
-
handlerErrors.push({ handlerName: handler.name, error: err.message });
|
|
467
|
-
}
|
|
468
|
-
}));
|
|
469
|
-
deliveryStore.mark(id);
|
|
470
|
-
for (const failure of handlerErrors) {
|
|
471
|
-
emit('webhook.failed', { source, event: webhookEvent, deliveryId: id, handlerName: failure.handlerName, error: failure.error });
|
|
472
|
-
}
|
|
473
|
-
options.onDelivery?.(webhook, firedJobs, firedHandlers);
|
|
474
|
-
}
|
|
475
|
-
catch (err) {
|
|
476
|
-
const error = err;
|
|
477
|
-
emit('webhook.failed', { source, event: webhookEvent, deliveryId: id, error: error.message });
|
|
478
|
-
options.onDeliveryError?.(webhook, error);
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
const server = http.createServer((req, res) => {
|
|
482
|
-
void (async () => {
|
|
483
|
-
if (req.method !== 'POST') {
|
|
484
|
-
res.writeHead(405, { 'content-type': 'text/plain' });
|
|
485
|
-
res.end('method not allowed');
|
|
486
|
-
return;
|
|
487
|
-
}
|
|
488
|
-
const source = sourceFromPath(req.url?.split('?')[0]);
|
|
489
|
-
if (!source) {
|
|
490
|
-
res.writeHead(404, { 'content-type': 'text/plain' });
|
|
491
|
-
res.end('not found');
|
|
492
|
-
return;
|
|
493
|
-
}
|
|
494
|
-
const secret = options.secrets[source];
|
|
495
|
-
if (!secret) {
|
|
496
|
-
emit('webhook.rejected', { source, reason: 'missing webhook secret' });
|
|
497
|
-
res.writeHead(503, { 'content-type': 'application/json' });
|
|
498
|
-
res.end(JSON.stringify({ ok: false, error: `missing ${source} webhook secret` }));
|
|
499
|
-
return;
|
|
500
|
-
}
|
|
501
|
-
const ip = req.socket.remoteAddress ?? 'unknown';
|
|
502
|
-
if (!ipRateLimiter.take(ip)) {
|
|
503
|
-
emit('webhook.rejected', { source, reason: 'ip rate limit exceeded' });
|
|
504
|
-
res.writeHead(429, { 'content-type': 'application/json' });
|
|
505
|
-
res.end(JSON.stringify({ ok: false, error: 'rate limit exceeded' }));
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
const declaredLength = Number.parseInt(header(req.headers, 'content-length') ?? '', 10);
|
|
509
|
-
if (Number.isFinite(declaredLength) && declaredLength > maxBodyBytes) {
|
|
510
|
-
emit('webhook.rejected', { source, reason: 'payload too large' });
|
|
511
|
-
res.writeHead(413, { 'content-type': 'application/json' });
|
|
512
|
-
res.end(JSON.stringify({ ok: false, error: `payload exceeds ${maxBodyBytes} bytes` }));
|
|
513
|
-
return;
|
|
514
|
-
}
|
|
515
|
-
try {
|
|
516
|
-
const rawBody = await readRawBody(req, maxBodyBytes);
|
|
517
|
-
const valid = source === 'github'
|
|
518
|
-
? verifyGithubSignature(req.headers, rawBody, secret)
|
|
519
|
-
: source === 'linear'
|
|
520
|
-
? verifyLinearSignature(req.headers, rawBody, secret)
|
|
521
|
-
: verifySlackSignature(req.headers, rawBody, secret);
|
|
522
|
-
if (!valid) {
|
|
523
|
-
emit('webhook.rejected', { source, reason: 'invalid signature' });
|
|
524
|
-
res.writeHead(401, { 'content-type': 'application/json' });
|
|
525
|
-
res.end(JSON.stringify({ ok: false, error: 'invalid signature' }));
|
|
526
|
-
return;
|
|
527
|
-
}
|
|
528
|
-
if (source === 'slack') {
|
|
529
|
-
const slack = parseSlackBody(header(req.headers, 'content-type'), rawBody);
|
|
530
|
-
if (slack.type === 'url_verification') {
|
|
531
|
-
res.writeHead(200, { 'content-type': 'application/json' });
|
|
532
|
-
res.end(JSON.stringify({ challenge: slack.challenge ?? '' }));
|
|
533
|
-
return;
|
|
534
|
-
}
|
|
535
|
-
const slackEvent = slackEventName(slack);
|
|
536
|
-
const slackId = `slack:${slack.event_id ?? crypto.createHash('sha256').update(rawBody).digest('hex')}`;
|
|
537
|
-
emit('webhook.received', { source, event: slackEvent, deliveryId: slackId });
|
|
538
|
-
if (deliveryStore.seen(slackId) || inFlight.has(slackId)) {
|
|
539
|
-
res.writeHead(200, { 'content-type': 'application/json' });
|
|
540
|
-
res.end(JSON.stringify({ ok: true, duplicate: true }));
|
|
541
|
-
return;
|
|
542
|
-
}
|
|
543
|
-
if (!rateLimiter.take(source)) {
|
|
544
|
-
emit('webhook.rejected', { source, event: slackEvent, deliveryId: slackId, reason: 'rate limit exceeded' });
|
|
545
|
-
res.writeHead(429, { 'content-type': 'application/json' });
|
|
546
|
-
res.end(JSON.stringify({ ok: false, error: 'rate limit exceeded' }));
|
|
547
|
-
return;
|
|
548
|
-
}
|
|
549
|
-
const slackWebhook = { source, event: slackEvent, payload: slack };
|
|
550
|
-
emit('webhook.authorized', { source, event: slackEvent, deliveryId: slackId });
|
|
551
|
-
inFlight.add(slackId);
|
|
552
|
-
res.writeHead(200, { 'content-type': 'application/json' });
|
|
553
|
-
res.end(slack.type === 'slash_command'
|
|
554
|
-
? JSON.stringify({ response_type: 'ephemeral', text: 'On it — replying in this channel.' })
|
|
555
|
-
: '');
|
|
556
|
-
void settleDelivery(slackWebhook, slackId).finally(() => inFlight.delete(slackId));
|
|
557
|
-
return;
|
|
558
|
-
}
|
|
559
|
-
const id = deliveryId(source, req.headers, rawBody);
|
|
560
|
-
const event = source === 'github' ? (header(req.headers, 'x-github-event') ?? '') : '';
|
|
561
|
-
emit('webhook.received', { source, event, deliveryId: id });
|
|
562
|
-
if (deliveryStore.seen(id) || inFlight.has(id)) {
|
|
563
|
-
res.writeHead(200, { 'content-type': 'application/json' });
|
|
564
|
-
res.end(JSON.stringify({ ok: true, duplicate: true, fired: [] }));
|
|
565
|
-
return;
|
|
566
|
-
}
|
|
567
|
-
const payload = rawBody.length > 0 ? JSON.parse(rawBody.toString('utf-8')) : {};
|
|
568
|
-
const webhookEvent = source === 'github' ? event : String(payload.type ?? '');
|
|
569
|
-
if (source === 'linear' && !verifyLinearTimestamp(payload)) {
|
|
570
|
-
emit('webhook.rejected', { source, event: webhookEvent, deliveryId: id, reason: 'stale linear webhook timestamp' });
|
|
571
|
-
res.writeHead(401, { 'content-type': 'application/json' });
|
|
572
|
-
res.end(JSON.stringify({ ok: false, error: 'stale linear webhook timestamp' }));
|
|
573
|
-
return;
|
|
574
|
-
}
|
|
575
|
-
if (!rateLimiter.take(source)) {
|
|
576
|
-
emit('webhook.rejected', { source, event: webhookEvent, deliveryId: id, reason: 'rate limit exceeded' });
|
|
577
|
-
res.writeHead(429, { 'content-type': 'application/json' });
|
|
578
|
-
res.end(JSON.stringify({ ok: false, error: 'rate limit exceeded' }));
|
|
579
|
-
return;
|
|
580
|
-
}
|
|
581
|
-
const webhook = {
|
|
582
|
-
source,
|
|
583
|
-
event: webhookEvent,
|
|
584
|
-
payload,
|
|
585
|
-
};
|
|
586
|
-
emit('webhook.authorized', { source, event: webhookEvent, deliveryId: id });
|
|
587
|
-
inFlight.add(id);
|
|
588
|
-
res.writeHead(202, { 'content-type': 'application/json' });
|
|
589
|
-
res.end(JSON.stringify({ ok: true, accepted: true, deliveryId: id }));
|
|
590
|
-
void settleDelivery(webhook, id).finally(() => inFlight.delete(id));
|
|
591
|
-
}
|
|
592
|
-
catch (err) {
|
|
593
|
-
res.writeHead(400, { 'content-type': 'application/json' });
|
|
594
|
-
res.end(JSON.stringify({ ok: false, error: err.message }));
|
|
595
|
-
}
|
|
596
|
-
})();
|
|
597
|
-
});
|
|
598
|
-
server.maxConnections = options.maxConnections ?? 256;
|
|
599
|
-
server.listen(options.port ?? 0, options.host ?? '127.0.0.1');
|
|
600
|
-
return server;
|
|
601
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phnx-labs/agents-cli",
|
|
3
|
-
"version": "1.23.
|
|
3
|
+
"version": "1.23.14",
|
|
4
4
|
"description": "One CLI for all your AI coding agents - versions, config, cloud dispatch, sessions, and teams (now with first-class Grok Build CLI support)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"dist/**/*.d.ts",
|
|
24
24
|
"dist/**/*.json",
|
|
25
25
|
"dist/**/*.sh",
|
|
26
|
+
"!dist/**/testdata/**",
|
|
26
27
|
"scripts/postinstall.js",
|
|
27
28
|
"scripts/install-helper.js",
|
|
28
29
|
"CHANGELOG.md",
|
package/dist/commands/funnel.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import { buildFunnelDownCommand, buildFunnelStatusCommand, buildFunnelUpCommand, parseFunnelPort } from '../lib/funnel.js';
|
|
3
|
-
import { resolveHost } from '../lib/hosts/registry.js';
|
|
4
|
-
import { resolveRemoteOsSync } from '../lib/hosts/remote-os.js';
|
|
5
|
-
import { sshTargetFor } from '../lib/hosts/types.js';
|
|
6
|
-
import { sshExec } from '../lib/ssh-exec.js';
|
|
7
|
-
async function resolveIngressHost(name) {
|
|
8
|
-
const host = await resolveHost(name);
|
|
9
|
-
if (!host)
|
|
10
|
-
throw new Error(`Unknown host "${name}". Enroll it with agents hosts add, or sync devices first.`);
|
|
11
|
-
const os = host.os ?? resolveRemoteOsSync(host.name);
|
|
12
|
-
if (os === 'windows') {
|
|
13
|
-
throw new Error('Tailscale Funnel requires a host with the Tailscale CLI; Windows cannot host this ingress path.');
|
|
14
|
-
}
|
|
15
|
-
return host;
|
|
16
|
-
}
|
|
17
|
-
async function runOnHost(hostName, command) {
|
|
18
|
-
const host = await resolveIngressHost(hostName);
|
|
19
|
-
const target = sshTargetFor(host);
|
|
20
|
-
const result = sshExec(target, command, { timeoutMs: 30_000, multiplex: true });
|
|
21
|
-
if (result.code !== 0) {
|
|
22
|
-
throw new Error((result.stderr || result.stdout).trim() || `ssh exited ${result.code ?? 'without a code'}`);
|
|
23
|
-
}
|
|
24
|
-
const out = result.stdout.trim();
|
|
25
|
-
if (out)
|
|
26
|
-
console.log(out);
|
|
27
|
-
}
|
|
28
|
-
export function registerFunnelCommand(program) {
|
|
29
|
-
const funnel = program
|
|
30
|
-
.command('funnel')
|
|
31
|
-
.description('Manage Tailscale Funnel exposure for a fleet webhook receiver.');
|
|
32
|
-
funnel
|
|
33
|
-
.command('status <host>')
|
|
34
|
-
.description('Show Tailscale Funnel status on a fleet host.')
|
|
35
|
-
.action(async (host) => {
|
|
36
|
-
try {
|
|
37
|
-
await runOnHost(host, buildFunnelStatusCommand());
|
|
38
|
-
}
|
|
39
|
-
catch (err) {
|
|
40
|
-
console.error(chalk.red(err.message));
|
|
41
|
-
process.exit(1);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
funnel
|
|
45
|
-
.command('up <host>')
|
|
46
|
-
.description('Expose a localhost webhook receiver through Tailscale Funnel.')
|
|
47
|
-
.requiredOption('--local-port <n>', 'Local receiver port on the remote host')
|
|
48
|
-
.option('--port <n>', 'Public Funnel port: 443, 8443, or 10000', '443')
|
|
49
|
-
.action(async (host, opts) => {
|
|
50
|
-
try {
|
|
51
|
-
const publicPort = parseFunnelPort(opts.port ?? '443');
|
|
52
|
-
const localPort = Number.parseInt(opts.localPort, 10);
|
|
53
|
-
const command = buildFunnelUpCommand(publicPort, localPort);
|
|
54
|
-
await runOnHost(host, command);
|
|
55
|
-
console.log(chalk.green(`Funnel enabled on ${host}: public :${publicPort} → localhost:${localPort}`));
|
|
56
|
-
}
|
|
57
|
-
catch (err) {
|
|
58
|
-
console.error(chalk.red(err.message));
|
|
59
|
-
process.exit(1);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
funnel
|
|
63
|
-
.command('down <host>')
|
|
64
|
-
.description('Disable Tailscale Funnel exposure for a public HTTPS port.')
|
|
65
|
-
.option('--port <n>', 'Public Funnel port: 443, 8443, or 10000', '443')
|
|
66
|
-
.action(async (host, opts) => {
|
|
67
|
-
try {
|
|
68
|
-
const publicPort = parseFunnelPort(opts.port ?? '443');
|
|
69
|
-
await runOnHost(host, buildFunnelDownCommand(publicPort));
|
|
70
|
-
console.log(chalk.green(`Funnel disabled on ${host}: public :${publicPort}`));
|
|
71
|
-
}
|
|
72
|
-
catch (err) {
|
|
73
|
-
console.error(chalk.red(err.message));
|
|
74
|
-
process.exit(1);
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
}
|
package/dist/commands/logs.d.ts
DELETED