@phnx-labs/agents-cli 1.23.13 → 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 +38 -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/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,383 +0,0 @@
|
|
|
1
|
-
import { exec } from 'child_process';
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import * as path from 'path';
|
|
4
|
-
import * as yaml from 'yaml';
|
|
5
|
-
import { emit } from '../feed/events.js';
|
|
6
|
-
import { machineId, normalizeHost } from '../machine-id.js';
|
|
7
|
-
import { safeJoin } from '../paths.js';
|
|
8
|
-
import { pickFleetDevice } from '../routines-placement.js';
|
|
9
|
-
import { assertShellSubstitutionSupported, readJob, substituteWebhookCommand, substituteWebhookPrompt, } from '../scheduling/routines.js';
|
|
10
|
-
import { ensureAgentsDir, getProjectWebhooksDir, getSystemWebhooksDir, getWebhooksDir } from '../state.js';
|
|
11
|
-
const HANDLER_DEFAULTS = {
|
|
12
|
-
enabled: true,
|
|
13
|
-
};
|
|
14
|
-
function payloadRepo(payload) {
|
|
15
|
-
const repo = payload?.repository;
|
|
16
|
-
const fullName = repo?.full_name;
|
|
17
|
-
return typeof fullName === 'string' && fullName.length > 0 ? fullName : null;
|
|
18
|
-
}
|
|
19
|
-
function shortRef(ref) {
|
|
20
|
-
return ref.replace(/^refs\/(heads|tags)\//, '');
|
|
21
|
-
}
|
|
22
|
-
function payloadBranches(event, payload) {
|
|
23
|
-
const branches = new Set();
|
|
24
|
-
const add = (v) => {
|
|
25
|
-
if (typeof v === 'string' && v.length > 0)
|
|
26
|
-
branches.add(shortRef(v));
|
|
27
|
-
};
|
|
28
|
-
switch (event) {
|
|
29
|
-
case 'push':
|
|
30
|
-
add(payload.ref);
|
|
31
|
-
break;
|
|
32
|
-
case 'pull_request': {
|
|
33
|
-
const pr = payload.pull_request;
|
|
34
|
-
add(pr?.base?.ref);
|
|
35
|
-
add(pr?.head?.ref);
|
|
36
|
-
break;
|
|
37
|
-
}
|
|
38
|
-
case 'workflow_run': {
|
|
39
|
-
const run = payload.workflow_run;
|
|
40
|
-
add(run?.head_branch);
|
|
41
|
-
break;
|
|
42
|
-
}
|
|
43
|
-
default:
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
return [...branches];
|
|
47
|
-
}
|
|
48
|
-
function linearAction(payload) {
|
|
49
|
-
return typeof payload.action === 'string' ? payload.action : null;
|
|
50
|
-
}
|
|
51
|
-
function linearTeamKey(payload) {
|
|
52
|
-
const data = payload.data;
|
|
53
|
-
const identifier = data?.identifier;
|
|
54
|
-
if (typeof identifier === 'string') {
|
|
55
|
-
const match = /^([A-Z][A-Z0-9]*)-\d+$/.exec(identifier);
|
|
56
|
-
if (match)
|
|
57
|
-
return match[1];
|
|
58
|
-
}
|
|
59
|
-
const team = data?.team;
|
|
60
|
-
return typeof team?.key === 'string' ? team.key : null;
|
|
61
|
-
}
|
|
62
|
-
function linearLabels(payload) {
|
|
63
|
-
const data = payload.data;
|
|
64
|
-
const labels = Array.isArray(data?.labels) ? data?.labels : [];
|
|
65
|
-
return labels
|
|
66
|
-
.map((n) => n.name)
|
|
67
|
-
.filter((n) => typeof n === 'string' && n.length > 0);
|
|
68
|
-
}
|
|
69
|
-
function githubAction(payload) {
|
|
70
|
-
return typeof payload.action === 'string' ? payload.action : null;
|
|
71
|
-
}
|
|
72
|
-
function githubLabels(payload) {
|
|
73
|
-
const names = new Set();
|
|
74
|
-
const add = (value) => {
|
|
75
|
-
if (typeof value === 'string' && value.length > 0)
|
|
76
|
-
names.add(value);
|
|
77
|
-
};
|
|
78
|
-
const deliveryLabel = payload.label;
|
|
79
|
-
add(deliveryLabel?.name);
|
|
80
|
-
const pr = payload.pull_request;
|
|
81
|
-
const prLabels = Array.isArray(pr?.labels) ? pr.labels : [];
|
|
82
|
-
for (const label of prLabels) {
|
|
83
|
-
add(label.name);
|
|
84
|
-
}
|
|
85
|
-
const issue = payload.issue;
|
|
86
|
-
const issueLabels = Array.isArray(issue?.labels) ? issue.labels : [];
|
|
87
|
-
for (const label of issueLabels) {
|
|
88
|
-
add(label.name);
|
|
89
|
-
}
|
|
90
|
-
return [...names];
|
|
91
|
-
}
|
|
92
|
-
function readHandlerFile(filePath) {
|
|
93
|
-
try {
|
|
94
|
-
const content = fs.readFileSync(filePath, 'utf-8');
|
|
95
|
-
const parsed = yaml.parse(content);
|
|
96
|
-
if (!parsed || typeof parsed !== 'object')
|
|
97
|
-
return null;
|
|
98
|
-
return {
|
|
99
|
-
...HANDLER_DEFAULTS,
|
|
100
|
-
...parsed,
|
|
101
|
-
name: parsed.name || path.basename(filePath).replace(/\.ya?ml$/, ''),
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
catch {
|
|
105
|
-
return null;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
function handlerRunsOnThisDevice(handler) {
|
|
109
|
-
if (!handler.devices || handler.devices.length === 0)
|
|
110
|
-
return true;
|
|
111
|
-
const self = machineId();
|
|
112
|
-
return handler.devices.some((d) => normalizeHost(d) === self);
|
|
113
|
-
}
|
|
114
|
-
const HOST_PLATFORMS = ['linux', 'macos', 'windows'];
|
|
115
|
-
function parseHostPlatform(raw) {
|
|
116
|
-
const parts = raw
|
|
117
|
-
.split('/')
|
|
118
|
-
.map((s) => s.trim().toLowerCase())
|
|
119
|
-
.filter((s) => s.length > 0);
|
|
120
|
-
const platformIdx = parts.findIndex((p) => HOST_PLATFORMS.includes(p));
|
|
121
|
-
if (platformIdx === -1)
|
|
122
|
-
return { base: parts.join('/') };
|
|
123
|
-
const platform = parts[platformIdx];
|
|
124
|
-
const rest = parts.filter((_, i) => i !== platformIdx).join('/');
|
|
125
|
-
return { base: rest, platform };
|
|
126
|
-
}
|
|
127
|
-
export function resolveHandlerHost(host) {
|
|
128
|
-
if (!host || host.trim() === '')
|
|
129
|
-
return {};
|
|
130
|
-
const { base, platform } = parseHostPlatform(host);
|
|
131
|
-
const isFleet = base === '' || base === 'fleet';
|
|
132
|
-
if (isFleet) {
|
|
133
|
-
const picked = pickFleetDevice(undefined, platform);
|
|
134
|
-
if (!picked) {
|
|
135
|
-
throw new Error(`handler host '${host}': no eligible online fleet device`);
|
|
136
|
-
}
|
|
137
|
-
if (normalizeHost(picked) === machineId())
|
|
138
|
-
return {};
|
|
139
|
-
return { host: picked, hostStrategy: 'host' };
|
|
140
|
-
}
|
|
141
|
-
if (normalizeHost(base) === machineId())
|
|
142
|
-
return {};
|
|
143
|
-
return { host: base, hostStrategy: 'host' };
|
|
144
|
-
}
|
|
145
|
-
export function listHandlers(cwd) {
|
|
146
|
-
ensureAgentsDir();
|
|
147
|
-
const seen = new Set();
|
|
148
|
-
const handlers = [];
|
|
149
|
-
const dirs = [];
|
|
150
|
-
if (cwd) {
|
|
151
|
-
const projectDir = getProjectWebhooksDir(cwd);
|
|
152
|
-
if (projectDir)
|
|
153
|
-
dirs.push({ scope: 'project', path: projectDir });
|
|
154
|
-
}
|
|
155
|
-
dirs.push({ scope: 'user', path: getWebhooksDir() });
|
|
156
|
-
dirs.push({ scope: 'system', path: getSystemWebhooksDir() });
|
|
157
|
-
for (const { path: dir } of dirs) {
|
|
158
|
-
if (!fs.existsSync(dir))
|
|
159
|
-
continue;
|
|
160
|
-
const files = fs.readdirSync(dir).filter((f) => f.endsWith('.yml') || f.endsWith('.yaml'));
|
|
161
|
-
for (const file of files) {
|
|
162
|
-
const handler = readHandlerFile(safeJoin(dir, file));
|
|
163
|
-
if (!handler)
|
|
164
|
-
continue;
|
|
165
|
-
if (seen.has(handler.name))
|
|
166
|
-
continue;
|
|
167
|
-
seen.add(handler.name);
|
|
168
|
-
handlers.push(handler);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
return handlers;
|
|
172
|
-
}
|
|
173
|
-
export function handlerMatchesWebhook(handler, webhook) {
|
|
174
|
-
if (handler.enabled === false)
|
|
175
|
-
return false;
|
|
176
|
-
if (handler.source !== webhook.source)
|
|
177
|
-
return false;
|
|
178
|
-
if (handler.event && handler.event !== webhook.event)
|
|
179
|
-
return false;
|
|
180
|
-
if (!handlerRunsOnThisDevice(handler))
|
|
181
|
-
return false;
|
|
182
|
-
if (webhook.source === 'slack') {
|
|
183
|
-
const slack = webhook.payload;
|
|
184
|
-
if (handler.command && slack.command !== handler.command)
|
|
185
|
-
return false;
|
|
186
|
-
if (handler.channel && slack.channel !== handler.channel)
|
|
187
|
-
return false;
|
|
188
|
-
return true;
|
|
189
|
-
}
|
|
190
|
-
if (handler.action) {
|
|
191
|
-
const action = webhook.source === 'github' ? githubAction(webhook.payload) : linearAction(webhook.payload);
|
|
192
|
-
if (action !== handler.action)
|
|
193
|
-
return false;
|
|
194
|
-
}
|
|
195
|
-
if (webhook.source === 'linear') {
|
|
196
|
-
if (handler.teamKey && linearTeamKey(webhook.payload) !== handler.teamKey)
|
|
197
|
-
return false;
|
|
198
|
-
if (handler.label) {
|
|
199
|
-
const expected = handler.label.toLowerCase();
|
|
200
|
-
if (!linearLabels(webhook.payload).some((name) => name.toLowerCase() === expected))
|
|
201
|
-
return false;
|
|
202
|
-
}
|
|
203
|
-
if (handler.stateTo) {
|
|
204
|
-
const data = webhook.payload.data;
|
|
205
|
-
const current = data?.state?.name;
|
|
206
|
-
if (current !== handler.stateTo)
|
|
207
|
-
return false;
|
|
208
|
-
const updatedTo = webhook.payload.updatedFrom;
|
|
209
|
-
if (!updatedTo || (updatedTo.state === undefined && updatedTo.stateId === undefined))
|
|
210
|
-
return false;
|
|
211
|
-
}
|
|
212
|
-
if (handler.stateFrom) {
|
|
213
|
-
const updatedFrom = webhook.payload.updatedFrom;
|
|
214
|
-
const previous = updatedFrom?.state?.name;
|
|
215
|
-
if (previous !== handler.stateFrom)
|
|
216
|
-
return false;
|
|
217
|
-
}
|
|
218
|
-
return true;
|
|
219
|
-
}
|
|
220
|
-
if (handler.repo) {
|
|
221
|
-
const repo = payloadRepo(webhook.payload);
|
|
222
|
-
if (!repo || repo.toLowerCase() !== handler.repo.toLowerCase())
|
|
223
|
-
return false;
|
|
224
|
-
}
|
|
225
|
-
if (handler.branch) {
|
|
226
|
-
const branches = payloadBranches(webhook.event, webhook.payload);
|
|
227
|
-
if (!branches.some((b) => b === handler.branch))
|
|
228
|
-
return false;
|
|
229
|
-
}
|
|
230
|
-
if (handler.label) {
|
|
231
|
-
const expected = handler.label.toLowerCase();
|
|
232
|
-
if (!githubLabels(webhook.payload).some((name) => name.toLowerCase() === expected))
|
|
233
|
-
return false;
|
|
234
|
-
}
|
|
235
|
-
return true;
|
|
236
|
-
}
|
|
237
|
-
const asString = (v) => (typeof v === 'string' ? v : '');
|
|
238
|
-
function parseSlackMessage(payload) {
|
|
239
|
-
const cleaned = asString(payload.text).replace(/^\s*<@[^>]+>\s*/, '').trim();
|
|
240
|
-
const m = /^([A-Za-z0-9][\w.-]*)\s*:\s+([\s\S]+)$/.exec(cleaned);
|
|
241
|
-
return {
|
|
242
|
-
text: cleaned,
|
|
243
|
-
project: m ? m[1] : '',
|
|
244
|
-
prompt: m ? m[2].trim() : cleaned,
|
|
245
|
-
channel: asString(payload.channel),
|
|
246
|
-
thread_ts: asString(payload.thread_ts),
|
|
247
|
-
user: asString(payload.user),
|
|
248
|
-
command: asString(payload.command),
|
|
249
|
-
response_url: asString(payload.response_url),
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
export function buildWebhookContext(webhook) {
|
|
253
|
-
if (webhook.source === 'slack') {
|
|
254
|
-
const ctx = {
|
|
255
|
-
source: webhook.source,
|
|
256
|
-
event: webhook.event,
|
|
257
|
-
slack: parseSlackMessage(webhook.payload),
|
|
258
|
-
};
|
|
259
|
-
return ctx;
|
|
260
|
-
}
|
|
261
|
-
const action = webhook.source === 'github'
|
|
262
|
-
? githubAction(webhook.payload) ?? undefined
|
|
263
|
-
: linearAction(webhook.payload) ?? undefined;
|
|
264
|
-
if (webhook.source === 'linear') {
|
|
265
|
-
return {
|
|
266
|
-
source: webhook.source,
|
|
267
|
-
event: webhook.event,
|
|
268
|
-
action,
|
|
269
|
-
issue: webhook.payload.data,
|
|
270
|
-
updatedFrom: webhook.payload.updatedFrom,
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
return {
|
|
274
|
-
source: webhook.source,
|
|
275
|
-
event: webhook.event,
|
|
276
|
-
action,
|
|
277
|
-
repository: webhook.payload.repository,
|
|
278
|
-
pull_request: webhook.payload.pull_request,
|
|
279
|
-
issue: webhook.payload.issue,
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
|
-
function defaultExecCommand(command) {
|
|
283
|
-
return new Promise((resolve) => {
|
|
284
|
-
exec(command, (error, stdout, stderr) => {
|
|
285
|
-
const output = stdout + stderr;
|
|
286
|
-
if (error) {
|
|
287
|
-
resolve({ exitCode: typeof error.code === 'number' ? error.code : 1, output });
|
|
288
|
-
}
|
|
289
|
-
else {
|
|
290
|
-
resolve({ exitCode: 0, output });
|
|
291
|
-
}
|
|
292
|
-
});
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
function dispatchDefault(config) {
|
|
296
|
-
return import('../daemon/runner.js').then((m) => m.executeJobDetached(config));
|
|
297
|
-
}
|
|
298
|
-
export async function executeHandler(handler, webhook, opts = {}) {
|
|
299
|
-
const context = buildWebhookContext(webhook);
|
|
300
|
-
const base = { handlerName: handler.name };
|
|
301
|
-
emit('webhook.handler.start', {
|
|
302
|
-
source: webhook.source,
|
|
303
|
-
event: webhook.event,
|
|
304
|
-
handlerName: handler.name,
|
|
305
|
-
});
|
|
306
|
-
try {
|
|
307
|
-
const result = await executeHandlerAction(handler, webhook, context, opts);
|
|
308
|
-
emit('webhook.handler.end', {
|
|
309
|
-
source: webhook.source,
|
|
310
|
-
event: webhook.event,
|
|
311
|
-
handlerName: handler.name,
|
|
312
|
-
status: 'success',
|
|
313
|
-
...result,
|
|
314
|
-
});
|
|
315
|
-
return { ...base, ...result };
|
|
316
|
-
}
|
|
317
|
-
catch (err) {
|
|
318
|
-
const error = err.message;
|
|
319
|
-
emit('webhook.handler.end', {
|
|
320
|
-
source: webhook.source,
|
|
321
|
-
event: webhook.event,
|
|
322
|
-
handlerName: handler.name,
|
|
323
|
-
status: 'error',
|
|
324
|
-
error,
|
|
325
|
-
});
|
|
326
|
-
throw err;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
async function executeHandlerAction(handler, webhook, context, opts) {
|
|
330
|
-
const substitutedPrompt = handler.run?.prompt ? substituteWebhookPrompt(handler.run.prompt, context) : '';
|
|
331
|
-
const substitutedProject = handler.project ? substituteWebhookPrompt(handler.project, context).trim() : '';
|
|
332
|
-
const substitutedCwd = handler.cwd ? substituteWebhookPrompt(handler.cwd, context).trim() : '';
|
|
333
|
-
const hostFields = resolveHandlerHost(handler.host);
|
|
334
|
-
if (handler.run?.agent || handler.run?.workflow) {
|
|
335
|
-
const config = {
|
|
336
|
-
name: handler.name,
|
|
337
|
-
mode: handler.mode ?? 'auto',
|
|
338
|
-
effort: 'auto',
|
|
339
|
-
timeout: '10m',
|
|
340
|
-
enabled: true,
|
|
341
|
-
prompt: substitutedPrompt,
|
|
342
|
-
...(handler.run.agent ? { agent: handler.run.agent } : { workflow: handler.run.workflow }),
|
|
343
|
-
...(handler.devices ? { devices: handler.devices } : {}),
|
|
344
|
-
...(handler.run.env ? { env: handler.run.env } : {}),
|
|
345
|
-
...(substitutedProject ? { project: substitutedProject } : {}),
|
|
346
|
-
...(substitutedCwd ? { cwd: substitutedCwd } : {}),
|
|
347
|
-
...(hostFields.host ? { host: hostFields.host } : {}),
|
|
348
|
-
...(hostFields.hostStrategy ? { hostStrategy: hostFields.hostStrategy } : {}),
|
|
349
|
-
dispatchedBy: 'webhook',
|
|
350
|
-
};
|
|
351
|
-
const dispatch = handler.run.agent
|
|
352
|
-
? (opts.dispatchAgent ?? dispatchDefault)
|
|
353
|
-
: (opts.dispatchWorkflow ?? dispatchDefault);
|
|
354
|
-
const meta = await dispatch(config);
|
|
355
|
-
return { runId: meta.runId };
|
|
356
|
-
}
|
|
357
|
-
if (handler.run?.command) {
|
|
358
|
-
assertShellSubstitutionSupported(handler.run.command);
|
|
359
|
-
const command = substituteWebhookCommand(handler.run.command, context);
|
|
360
|
-
const exec = opts.execCommand ?? defaultExecCommand;
|
|
361
|
-
const { exitCode, output } = await exec(command);
|
|
362
|
-
return { exitCode, output };
|
|
363
|
-
}
|
|
364
|
-
if (handler.routine) {
|
|
365
|
-
const routine = readJob(handler.routine);
|
|
366
|
-
if (!routine)
|
|
367
|
-
throw new Error(`routine '${handler.routine}' not found`);
|
|
368
|
-
const config = {
|
|
369
|
-
...routine,
|
|
370
|
-
prompt: substituteWebhookPrompt(routine.prompt, context),
|
|
371
|
-
...(handler.devices ? { devices: handler.devices } : {}),
|
|
372
|
-
...(substitutedProject ? { project: substitutedProject } : {}),
|
|
373
|
-
...(substitutedCwd ? { cwd: substitutedCwd } : {}),
|
|
374
|
-
...(handler.mode ? { mode: handler.mode } : {}),
|
|
375
|
-
...(hostFields.host ? { host: hostFields.host } : {}),
|
|
376
|
-
...(hostFields.hostStrategy ? { hostStrategy: hostFields.hostStrategy } : {}),
|
|
377
|
-
};
|
|
378
|
-
const dispatch = opts.dispatchRoutine ?? dispatchDefault;
|
|
379
|
-
const meta = await dispatch(config);
|
|
380
|
-
return { runId: meta.runId };
|
|
381
|
-
}
|
|
382
|
-
throw new Error(`handler '${handler.name}' has no run or routine action`);
|
|
383
|
-
}
|