loopctl-mcp-server 3.45.0 → 3.46.1
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 +25 -1
- package/index.js +561 -7
- package/lib/anomaly-resolution.js +57 -0
- package/lib/api-keys.js +9 -13
- package/lib/cost-analytics.js +141 -0
- package/lib/secret-file.js +27 -0
- package/lib/token-budgets.js +131 -0
- package/lib/token-usage-admin.js +78 -0
- package/lib/webhooks.js +271 -0
- package/package.json +1 -1
package/lib/webhooks.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `webhook_create` / `webhook_list` / `webhook_update` / `webhook_delete` / `webhook_test` /
|
|
3
|
+
* `webhook_deliveries` — `POST /api/v1/webhooks`, `GET /api/v1/webhooks`,
|
|
4
|
+
* `PATCH /api/v1/webhooks/:id`, `DELETE /api/v1/webhooks/:id`, `POST /api/v1/webhooks/:id/test`
|
|
5
|
+
* and `GET /api/v1/webhooks/:id/deliveries` (`WebhookController`), loopctl #872.
|
|
6
|
+
*
|
|
7
|
+
* KEY: the controller has ONE plug, `role: :user` WITH the hierarchy, so an agent or
|
|
8
|
+
* orchestrator key is 403 insufficient_role on every action. They travel on LOOPCTL_USER_KEY
|
|
9
|
+
* verbatim when set (index.js `userKeyApiCall`). Not human-anchored and not custody surface.
|
|
10
|
+
*
|
|
11
|
+
* THE SIGNING SECRET. Create answers the subscription's HMAC signing secret ONCE, and a tool
|
|
12
|
+
* result lands in the session transcript, so the secret NEVER appears in the result: it is
|
|
13
|
+
* written to `secret_file` (an absolute path that must not exist yet, created 0600 BEFORE the
|
|
14
|
+
* request, lib/secret-file.js) and stripped from the answer. A path that exists or a missing
|
|
15
|
+
* directory is refused with nothing created. Only a failure after the create (the write
|
|
16
|
+
* itself) can leave a subscription without its secret, and the result then names it to delete.
|
|
17
|
+
*
|
|
18
|
+
* CHECKED HERE, because the server would answer them WRONGLY rather than refuse:
|
|
19
|
+
*
|
|
20
|
+
* - `page` / `page_size`: loopctl drops a non-integer and clamps `page_size` to 1..100 and
|
|
21
|
+
* `page` to at least 1, so an out-of-range value answers a page nobody asked for;
|
|
22
|
+
* - an update with no field, or with `project_id: null`: loopctl drops every nil field and
|
|
23
|
+
* answers 200 with the subscription unchanged (a subscription's project cannot be cleared);
|
|
24
|
+
* - `events` that is not a non-empty list of strings, `active` that is not a boolean;
|
|
25
|
+
* - a non-UUID webhook id, which would not answer the plain 404 a missing subscription does.
|
|
26
|
+
*
|
|
27
|
+
* Unknown event types, a bad URL and a destination the egress guard refuses are loopctl's to
|
|
28
|
+
* judge (422 with the reason), so they are sent.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { isAbsolute } from "node:path";
|
|
32
|
+
|
|
33
|
+
import { uuid } from "./delivery-loop.js";
|
|
34
|
+
import { createSecretFile, secretOutcomeUnknown, unknownOutcomeDetail } from "./secret-file.js";
|
|
35
|
+
|
|
36
|
+
export const WEBHOOK_MAX_PAGE_SIZE = 100;
|
|
37
|
+
|
|
38
|
+
function refuse(body) {
|
|
39
|
+
return { error: true, status: 0, body };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ONE template literal per builder (see `test/tool-surface.js`).
|
|
43
|
+
export function webhooksPath(query = "") {
|
|
44
|
+
return `/api/v1/webhooks${query}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function webhookPath(webhookId) {
|
|
48
|
+
return `/api/v1/webhooks/${encodeURIComponent(webhookId)}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function webhookTestPath(webhookId) {
|
|
52
|
+
return `/api/v1/webhooks/${encodeURIComponent(webhookId)}/test`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function webhookDeliveriesPath(webhookId, query = "") {
|
|
56
|
+
return `/api/v1/webhooks/${encodeURIComponent(webhookId)}/deliveries${query}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function pageQuery(page, page_size, defaultSize) {
|
|
60
|
+
if (page != null && !(Number.isInteger(page) && page >= 1)) {
|
|
61
|
+
return { bad: refuse("`page` must be a positive integer.") };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (
|
|
65
|
+
page_size != null &&
|
|
66
|
+
!(Number.isInteger(page_size) && page_size >= 1 && page_size <= WEBHOOK_MAX_PAGE_SIZE)
|
|
67
|
+
) {
|
|
68
|
+
return {
|
|
69
|
+
bad: refuse(
|
|
70
|
+
`\`page_size\` must be an integer from 1 to ${WEBHOOK_MAX_PAGE_SIZE} (default ` +
|
|
71
|
+
`${defaultSize}); loopctl would clamp any other value without saying so.`,
|
|
72
|
+
),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const params = new URLSearchParams();
|
|
77
|
+
if (page != null) params.set("page", String(page));
|
|
78
|
+
if (page_size != null) params.set("page_size", String(page_size));
|
|
79
|
+
const qs = params.toString();
|
|
80
|
+
|
|
81
|
+
return { query: qs === "" ? "" : `?${qs}` };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function badEvents(events) {
|
|
85
|
+
if (
|
|
86
|
+
!Array.isArray(events) ||
|
|
87
|
+
events.length === 0 ||
|
|
88
|
+
events.some((e) => typeof e !== "string" || e.trim() === "")
|
|
89
|
+
) {
|
|
90
|
+
return refuse(
|
|
91
|
+
"`events` must be a non-empty list of event type strings (e.g. story.verified); " +
|
|
92
|
+
"loopctl answers 422 naming any it does not know.",
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function badProjectId(project_id) {
|
|
99
|
+
if (typeof project_id !== "string" || project_id.trim() === "") {
|
|
100
|
+
return refuse(
|
|
101
|
+
"`project_id` must be a project's UUID, slug or repo name. A subscription's project " +
|
|
102
|
+
"cannot be cleared: loopctl drops a null and answers 200 unchanged.",
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* `deps.apiCall(method, path, body)` is the user-key caller; `deps.openSecret(path)` creates the
|
|
110
|
+
* file and returns `{ writeSecret(text), discardSecret() }` (lib/secret-file.js; injected so a
|
|
111
|
+
* test owns the disk it touches).
|
|
112
|
+
*/
|
|
113
|
+
export async function webhookCreate(
|
|
114
|
+
{ url, events, project_id, secret_file } = {},
|
|
115
|
+
{ apiCall, openSecret = createSecretFile } = {},
|
|
116
|
+
) {
|
|
117
|
+
if (typeof url !== "string" || url.trim() === "") {
|
|
118
|
+
return refuse("`url` is required: the HTTPS (or HTTP) URL deliveries are POSTed to.");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const eventsBad = badEvents(events);
|
|
122
|
+
if (eventsBad) return eventsBad;
|
|
123
|
+
|
|
124
|
+
if (project_id !== undefined) {
|
|
125
|
+
const bad = badProjectId(project_id);
|
|
126
|
+
if (bad) return bad;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (typeof secret_file !== "string" || !isAbsolute(secret_file)) {
|
|
130
|
+
return refuse(
|
|
131
|
+
"`secret_file` must be an absolute path that does not exist yet. The signing secret is " +
|
|
132
|
+
"written there (mode 0600) and never returned, because a tool result lands in the " +
|
|
133
|
+
"transcript.",
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let sink;
|
|
138
|
+
try {
|
|
139
|
+
sink = openSecret(secret_file);
|
|
140
|
+
} catch (error) {
|
|
141
|
+
return refuse(
|
|
142
|
+
`${secret_file} could not be created (${error.code || error.message}); nothing was ` +
|
|
143
|
+
"created. Pass a path that does not exist yet, in a directory that does.",
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const body = { url, events };
|
|
148
|
+
if (project_id !== undefined) body.project_id = project_id;
|
|
149
|
+
|
|
150
|
+
let result;
|
|
151
|
+
try {
|
|
152
|
+
result = await apiCall("POST", webhooksPath(), body);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
sink.discardSecret();
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const created = result && result.webhook;
|
|
159
|
+
|
|
160
|
+
if (!created || typeof created.signing_secret !== "string") {
|
|
161
|
+
sink.discardSecret();
|
|
162
|
+
return secretOutcomeUnknown(result) ? mayHaveCreated(result, url) : stripSecret(result);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const { signing_secret, ...rest } = created;
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
sink.writeSecret(signing_secret + "\n");
|
|
169
|
+
} catch (error) {
|
|
170
|
+
sink.discardSecret();
|
|
171
|
+
return {
|
|
172
|
+
error: true,
|
|
173
|
+
status: 0,
|
|
174
|
+
body:
|
|
175
|
+
`The webhook ${rest.id} was created but ${secret_file} could not be written ` +
|
|
176
|
+
`(${error.code || error.message}); its signing secret is lost and no delivery can be ` +
|
|
177
|
+
`verified. Delete it (webhook_delete ${rest.id}) and create another with a new ` +
|
|
178
|
+
"secret_file.",
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return { webhook: rest, secret_file };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export async function webhookList({ page, page_size } = {}, { apiCall } = {}) {
|
|
186
|
+
const { bad, query } = pageQuery(page, page_size, 20);
|
|
187
|
+
if (bad) return bad;
|
|
188
|
+
|
|
189
|
+
return apiCall("GET", webhooksPath(query), null);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export async function webhookUpdate(
|
|
193
|
+
{ webhook_id, url, events, project_id, active } = {},
|
|
194
|
+
{ apiCall } = {},
|
|
195
|
+
) {
|
|
196
|
+
const idBad = uuid(webhook_id, "webhook_id");
|
|
197
|
+
if (idBad) return idBad;
|
|
198
|
+
|
|
199
|
+
const body = {};
|
|
200
|
+
|
|
201
|
+
if (url !== undefined) {
|
|
202
|
+
if (typeof url !== "string" || url.trim() === "") {
|
|
203
|
+
return refuse("`url` must be a non-empty URL string.");
|
|
204
|
+
}
|
|
205
|
+
body.url = url;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (events !== undefined) {
|
|
209
|
+
const bad = badEvents(events);
|
|
210
|
+
if (bad) return bad;
|
|
211
|
+
body.events = events;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (project_id !== undefined) {
|
|
215
|
+
const bad = badProjectId(project_id);
|
|
216
|
+
if (bad) return bad;
|
|
217
|
+
body.project_id = project_id;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (active !== undefined) {
|
|
221
|
+
if (typeof active !== "boolean") return refuse("`active` must be a boolean.");
|
|
222
|
+
body.active = active;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (Object.keys(body).length === 0) {
|
|
226
|
+
return refuse(
|
|
227
|
+
"Name at least one of `url`, `events`, `project_id`, `active`: loopctl would answer 200 " +
|
|
228
|
+
"with the subscription unchanged.",
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return apiCall("PATCH", webhookPath(webhook_id), body);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export async function webhookDelete({ webhook_id } = {}, { apiCall } = {}) {
|
|
236
|
+
return uuid(webhook_id, "webhook_id") ?? apiCall("DELETE", webhookPath(webhook_id), null);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export async function webhookTest({ webhook_id } = {}, { apiCall } = {}) {
|
|
240
|
+
return uuid(webhook_id, "webhook_id") ?? apiCall("POST", webhookTestPath(webhook_id), null);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export async function webhookDeliveries({ webhook_id, page, page_size } = {}, { apiCall } = {}) {
|
|
244
|
+
const idBad = uuid(webhook_id, "webhook_id");
|
|
245
|
+
if (idBad) return idBad;
|
|
246
|
+
|
|
247
|
+
const { bad, query } = pageQuery(page, page_size, 25);
|
|
248
|
+
if (bad) return bad;
|
|
249
|
+
|
|
250
|
+
return apiCall("GET", webhookDeliveriesPath(webhook_id, query), null);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function mayHaveCreated(result, url) {
|
|
254
|
+
return {
|
|
255
|
+
error: true,
|
|
256
|
+
status: result && Number.isInteger(result.status) ? result.status : 0,
|
|
257
|
+
body:
|
|
258
|
+
`Create outcome unknown (${unknownOutcomeDetail(result)}). ` +
|
|
259
|
+
`The request may have created a webhook for ${url} whose signing secret is lost: list ` +
|
|
260
|
+
"the tenant's webhooks (webhook_list) and delete any for that URL before retrying.",
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// A refusal body never carries the secret, but nothing from this route is passed on unread.
|
|
265
|
+
function stripSecret(result) {
|
|
266
|
+
if (result && result.webhook && "signing_secret" in result.webhook) {
|
|
267
|
+
const { signing_secret: _secret, ...rest } = result.webhook;
|
|
268
|
+
return { ...result, webhook: rest };
|
|
269
|
+
}
|
|
270
|
+
return result;
|
|
271
|
+
}
|