@agentpress/sdk 0.2.78 → 0.2.82
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/dist/index.cjs +337 -341
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +193 -186
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +313 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +330 -308
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -8
- package/dist/index.d.ts +0 -306
package/dist/index.mjs
CHANGED
|
@@ -1,335 +1,357 @@
|
|
|
1
|
-
|
|
1
|
+
import { createHmac, randomUUID, timingSafeEqual } from "node:crypto";
|
|
2
|
+
//#region src/errors.ts
|
|
3
|
+
/**
|
|
4
|
+
* Base error class for all SDK errors. Catch this to handle any error
|
|
5
|
+
* thrown by the AgentPress SDK regardless of specific type.
|
|
6
|
+
*/
|
|
2
7
|
var AgentPressError = class extends Error {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "AgentPressError";
|
|
11
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
12
|
+
}
|
|
8
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Thrown at construction time when `AgentPressOptions` contains invalid values
|
|
16
|
+
* (e.g., non-positive timeout, malformed `webhookSecret`).
|
|
17
|
+
*/
|
|
9
18
|
var ConfigurationError = class extends AgentPressError {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
constructor(message) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "ConfigurationError";
|
|
22
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
23
|
+
}
|
|
15
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Thrown when the API returns a non-2xx HTTP response.
|
|
27
|
+
*
|
|
28
|
+
* Properties:
|
|
29
|
+
* - `statusCode` - HTTP status code (e.g., 401, 404, 500)
|
|
30
|
+
* - `responseBody` - Raw response body text for debugging
|
|
31
|
+
* - `url` - The full request URL that failed
|
|
32
|
+
*/
|
|
16
33
|
var HttpError = class extends AgentPressError {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
34
|
+
statusCode;
|
|
35
|
+
responseBody;
|
|
36
|
+
url;
|
|
37
|
+
constructor(statusCode, responseBody, url) {
|
|
38
|
+
super(`HTTP ${statusCode} from ${url}`);
|
|
39
|
+
this.name = "HttpError";
|
|
40
|
+
this.statusCode = statusCode;
|
|
41
|
+
this.responseBody = responseBody;
|
|
42
|
+
this.url = url;
|
|
43
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
44
|
+
}
|
|
28
45
|
};
|
|
46
|
+
/** Thrown when a request exceeds the configured `timeout` (default 30s). */
|
|
29
47
|
var TimeoutError = class extends AgentPressError {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
48
|
+
constructor(url, timeout) {
|
|
49
|
+
super(`Request to ${url} timed out after ${timeout}ms`);
|
|
50
|
+
this.name = "TimeoutError";
|
|
51
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
52
|
+
}
|
|
35
53
|
};
|
|
54
|
+
/** Thrown by {@link WebhooksClient.verifyOrThrow} when an inbound webhook signature is invalid or expired. */
|
|
36
55
|
var WebhookSignatureError = class extends AgentPressError {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
56
|
+
constructor(message) {
|
|
57
|
+
super(message);
|
|
58
|
+
this.name = "WebhookSignatureError";
|
|
59
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
60
|
+
}
|
|
42
61
|
};
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
import { randomUUID } from "crypto";
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/utils.ts
|
|
46
64
|
function randomMessageId() {
|
|
47
|
-
|
|
65
|
+
return `msg_${randomUUID()}`;
|
|
48
66
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/webhooks/signing.ts
|
|
69
|
+
const SIGNATURE_PREFIX = "v1,";
|
|
70
|
+
const DEFAULT_TOLERANCE_SECONDS = 300;
|
|
71
|
+
/**
|
|
72
|
+
* Sign a webhook payload using Svix-compatible HMAC-SHA256.
|
|
73
|
+
*
|
|
74
|
+
* @param secret - The webhook secret (with "whsec_" prefix)
|
|
75
|
+
* @param msgId - Unique message ID (e.g., "msg_<uuid>")
|
|
76
|
+
* @param timestamp - Unix timestamp in seconds
|
|
77
|
+
* @param body - JSON stringified payload
|
|
78
|
+
* @returns Signature string in format "v1,<base64>"
|
|
79
|
+
*/
|
|
54
80
|
function sign(secret, msgId, timestamp, body) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return `${SIGNATURE_PREFIX}${signature}`;
|
|
81
|
+
const secretBytes = Buffer.from(secret.replace(/^whsec_/, ""), "base64");
|
|
82
|
+
const message = `${msgId}.${timestamp}.${body}`;
|
|
83
|
+
return `${SIGNATURE_PREFIX}${createHmac("sha256", secretBytes).update(message).digest("base64")}`;
|
|
59
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Verify a Svix webhook signature.
|
|
87
|
+
*
|
|
88
|
+
* @param secret - The webhook secret (with "whsec_" prefix)
|
|
89
|
+
* @param payload - Raw request body (string or Buffer)
|
|
90
|
+
* @param headers - Svix headers object
|
|
91
|
+
* @param toleranceSeconds - Max age of signature in seconds (default: 5 min)
|
|
92
|
+
* @returns true if valid, false if invalid or expired
|
|
93
|
+
*/
|
|
60
94
|
function verify(secret, payload, headers, toleranceSeconds = DEFAULT_TOLERANCE_SECONDS) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return false;
|
|
95
|
+
const msgId = headers["svix-id"];
|
|
96
|
+
const timestampStr = headers["svix-timestamp"];
|
|
97
|
+
const signatureHeader = headers["svix-signature"];
|
|
98
|
+
const timestamp = parseInt(timestampStr, 10);
|
|
99
|
+
if (Number.isNaN(timestamp)) return false;
|
|
100
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
101
|
+
if (Math.abs(now - timestamp) > toleranceSeconds) return false;
|
|
102
|
+
const expected = sign(secret, msgId, timestamp, typeof payload === "string" ? payload : payload.toString("utf-8"));
|
|
103
|
+
const signatures = signatureHeader.split(" ");
|
|
104
|
+
const expectedBuf = Buffer.from(expected);
|
|
105
|
+
for (const sig of signatures) {
|
|
106
|
+
const sigBuf = Buffer.from(sig);
|
|
107
|
+
if (sigBuf.length === expectedBuf.length && timingSafeEqual(sigBuf, expectedBuf)) return true;
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
79
110
|
}
|
|
80
|
-
|
|
81
|
-
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/actions/client.ts
|
|
113
|
+
/**
|
|
114
|
+
* Client for programmatically approving or rejecting staged actions.
|
|
115
|
+
* Uses HMAC-SHA256 signing (Svix-compatible), identical to {@link WebhooksClient}.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* const client = new AgentPress({ webhookSecret: "whsec_...", org: "my-org" });
|
|
120
|
+
*
|
|
121
|
+
* // Approve a staged action
|
|
122
|
+
* await client.actions.approve("act_123", {
|
|
123
|
+
* action: "my_webhook_action",
|
|
124
|
+
* });
|
|
125
|
+
*
|
|
126
|
+
* // Reject with a reason
|
|
127
|
+
* await client.actions.reject("act_456", {
|
|
128
|
+
* action: "my_webhook_action",
|
|
129
|
+
* reason: "Insufficient data",
|
|
130
|
+
* });
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
82
133
|
var ActionsClient = class {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
timestamp,
|
|
127
|
-
bodyStr
|
|
128
|
-
);
|
|
129
|
-
return this.http.request(path, {
|
|
130
|
-
method: "POST",
|
|
131
|
-
body: bodyStr,
|
|
132
|
-
headers: {
|
|
133
|
-
"svix-id": msgId,
|
|
134
|
-
"svix-timestamp": String(timestamp),
|
|
135
|
-
"svix-signature": signature
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
}
|
|
134
|
+
options;
|
|
135
|
+
http;
|
|
136
|
+
constructor(options, http) {
|
|
137
|
+
this.options = options;
|
|
138
|
+
this.http = http;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Approve a staged action, optionally modifying the tool call.
|
|
142
|
+
*
|
|
143
|
+
* @throws ConfigurationError if webhookSecret is not configured
|
|
144
|
+
* @throws HttpError on non-2xx response
|
|
145
|
+
* @throws TimeoutError if request exceeds timeout
|
|
146
|
+
*/
|
|
147
|
+
async approve(actionId, params) {
|
|
148
|
+
return this.manage(actionId, params.action, "approve", { editedToolCall: params.editedToolCall });
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Reject a staged action.
|
|
152
|
+
*
|
|
153
|
+
* @throws ConfigurationError if webhookSecret is not configured
|
|
154
|
+
* @throws HttpError on non-2xx response
|
|
155
|
+
* @throws TimeoutError if request exceeds timeout
|
|
156
|
+
*/
|
|
157
|
+
async reject(actionId, params) {
|
|
158
|
+
return this.manage(actionId, params.action, "reject", { reason: params.reason });
|
|
159
|
+
}
|
|
160
|
+
async manage(actionId, webhookAction, operation, body) {
|
|
161
|
+
if (!this.options.webhookSecret) throw new ConfigurationError("webhookSecret is required for action management operations");
|
|
162
|
+
const path = `/webhooks/actions/${this.options.org}/${webhookAction}/manage/${actionId}/${operation}`;
|
|
163
|
+
const bodyStr = JSON.stringify(body);
|
|
164
|
+
const msgId = randomMessageId();
|
|
165
|
+
const timestamp = Math.floor(Date.now() / 1e3);
|
|
166
|
+
const signature = sign(this.options.webhookSecret, msgId, timestamp, bodyStr);
|
|
167
|
+
return this.http.request(path, {
|
|
168
|
+
method: "POST",
|
|
169
|
+
body: bodyStr,
|
|
170
|
+
headers: {
|
|
171
|
+
"svix-id": msgId,
|
|
172
|
+
"svix-timestamp": String(timestamp),
|
|
173
|
+
"svix-signature": signature
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
139
177
|
};
|
|
140
|
-
|
|
141
|
-
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/http.ts
|
|
180
|
+
/**
|
|
181
|
+
* Internal shared HTTP client. Not part of the public API -- used by
|
|
182
|
+
* namespace clients (e.g., `WebhooksClient`) to make authenticated requests.
|
|
183
|
+
* @internal
|
|
184
|
+
*/
|
|
142
185
|
var HttpClient = class {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
try {
|
|
190
|
-
return JSON.parse(text);
|
|
191
|
-
} catch {
|
|
192
|
-
throw new AgentPressError(
|
|
193
|
-
`Expected JSON response from ${url} but received: ${text.slice(0, 200)}`
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
186
|
+
baseUrl;
|
|
187
|
+
timeout;
|
|
188
|
+
onRequest;
|
|
189
|
+
onResponse;
|
|
190
|
+
constructor(options) {
|
|
191
|
+
this.baseUrl = options.baseUrl;
|
|
192
|
+
this.timeout = options.timeout;
|
|
193
|
+
this.onRequest = options.onRequest;
|
|
194
|
+
this.onResponse = options.onResponse;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Send an HTTP request to the API. Constructs the full URL from `baseUrl` + `path`,
|
|
198
|
+
* applies the configured timeout via `AbortSignal`, fires `onRequest`/`onResponse`
|
|
199
|
+
* hooks, and parses the JSON response.
|
|
200
|
+
*
|
|
201
|
+
* @throws {TimeoutError} If the request exceeds the configured timeout.
|
|
202
|
+
* @throws {HttpError} If the API returns a non-2xx status code.
|
|
203
|
+
* @throws {AgentPressError} On network failures or non-JSON responses.
|
|
204
|
+
*/
|
|
205
|
+
async request(path, init) {
|
|
206
|
+
const url = `${this.baseUrl}${path}`;
|
|
207
|
+
const headers = new Headers(init.headers);
|
|
208
|
+
if (!headers.has("Content-Type")) headers.set("Content-Type", "application/json");
|
|
209
|
+
const requestInit = {
|
|
210
|
+
...init,
|
|
211
|
+
headers,
|
|
212
|
+
signal: AbortSignal.timeout(this.timeout)
|
|
213
|
+
};
|
|
214
|
+
this.onRequest?.(url, requestInit);
|
|
215
|
+
let response;
|
|
216
|
+
try {
|
|
217
|
+
response = await fetch(url, requestInit);
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (error instanceof Error && (error.name === "TimeoutError" || error.name === "AbortError")) throw new TimeoutError(url, this.timeout);
|
|
220
|
+
throw new AgentPressError(`Request to ${url} failed: ${error instanceof Error ? error.message : "Unknown fetch error"}`);
|
|
221
|
+
}
|
|
222
|
+
this.onResponse?.(url, response.clone());
|
|
223
|
+
const text = await response.text();
|
|
224
|
+
if (!response.ok) throw new HttpError(response.status, text, url);
|
|
225
|
+
try {
|
|
226
|
+
return JSON.parse(text);
|
|
227
|
+
} catch {
|
|
228
|
+
throw new AgentPressError(`Expected JSON response from ${url} but received: ${text.slice(0, 200)}`);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
197
231
|
};
|
|
198
|
-
|
|
199
|
-
|
|
232
|
+
//#endregion
|
|
233
|
+
//#region src/webhooks/client.ts
|
|
200
234
|
var WebhooksClient = class {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
*
|
|
271
|
-
* @throws WebhookSignatureError if signature is invalid or expired
|
|
272
|
-
* @throws ConfigurationError if webhookSecret is not configured
|
|
273
|
-
* @throws AgentPressError if payload is not valid JSON
|
|
274
|
-
*/
|
|
275
|
-
constructEvent(params) {
|
|
276
|
-
this.verifyOrThrow(params);
|
|
277
|
-
const body = typeof params.payload === "string" ? params.payload : params.payload.toString("utf-8");
|
|
278
|
-
try {
|
|
279
|
-
return JSON.parse(body);
|
|
280
|
-
} catch {
|
|
281
|
-
throw new AgentPressError("Webhook payload is not valid JSON");
|
|
282
|
-
}
|
|
283
|
-
}
|
|
235
|
+
options;
|
|
236
|
+
http;
|
|
237
|
+
constructor(options, http) {
|
|
238
|
+
this.options = options;
|
|
239
|
+
this.http = http;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Send an arbitrary webhook payload to AgentPress.
|
|
243
|
+
* Signs the payload with HMAC-SHA256 (Svix-compatible).
|
|
244
|
+
*
|
|
245
|
+
* @throws ConfigurationError if webhookSecret is not configured
|
|
246
|
+
* @throws HttpError on non-2xx response
|
|
247
|
+
* @throws TimeoutError if request exceeds timeout
|
|
248
|
+
*/
|
|
249
|
+
async send(params) {
|
|
250
|
+
if (!this.options.webhookSecret) throw new ConfigurationError("webhookSecret is required for webhook operations");
|
|
251
|
+
const path = `/webhooks/actions/${this.options.org}/${params.action}`;
|
|
252
|
+
const body = JSON.stringify(params.payload);
|
|
253
|
+
const msgId = randomMessageId();
|
|
254
|
+
const timestamp = Math.floor(Date.now() / 1e3);
|
|
255
|
+
const signature = sign(this.options.webhookSecret, msgId, timestamp, body);
|
|
256
|
+
return this.http.request(path, {
|
|
257
|
+
method: "POST",
|
|
258
|
+
body,
|
|
259
|
+
headers: {
|
|
260
|
+
"svix-id": msgId,
|
|
261
|
+
"svix-timestamp": String(timestamp),
|
|
262
|
+
"svix-signature": signature
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Verify an inbound Svix webhook signature.
|
|
268
|
+
*
|
|
269
|
+
* @returns true if valid, false if invalid or expired
|
|
270
|
+
* @throws ConfigurationError if webhookSecret is not configured
|
|
271
|
+
*/
|
|
272
|
+
verify(params) {
|
|
273
|
+
if (!this.options.webhookSecret) throw new ConfigurationError("webhookSecret is required for webhook verification");
|
|
274
|
+
return verify(this.options.webhookSecret, params.payload, params.headers);
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Verify an inbound Svix webhook signature, throwing on failure.
|
|
278
|
+
* Useful for middleware patterns where invalid signatures should halt processing.
|
|
279
|
+
*
|
|
280
|
+
* @throws WebhookSignatureError if signature is invalid or expired
|
|
281
|
+
* @throws ConfigurationError if webhookSecret is not configured
|
|
282
|
+
*/
|
|
283
|
+
verifyOrThrow(params) {
|
|
284
|
+
if (!this.verify(params)) throw new WebhookSignatureError("Invalid webhook signature");
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Verify and parse an inbound webhook from AgentPress.
|
|
288
|
+
* Combines signature verification with JSON parsing and type casting.
|
|
289
|
+
* This is the recommended way to handle incoming webhooks.
|
|
290
|
+
*
|
|
291
|
+
* @throws WebhookSignatureError if signature is invalid or expired
|
|
292
|
+
* @throws ConfigurationError if webhookSecret is not configured
|
|
293
|
+
* @throws AgentPressError if payload is not valid JSON
|
|
294
|
+
*/
|
|
295
|
+
constructEvent(params) {
|
|
296
|
+
this.verifyOrThrow(params);
|
|
297
|
+
const body = typeof params.payload === "string" ? params.payload : params.payload.toString("utf-8");
|
|
298
|
+
try {
|
|
299
|
+
return JSON.parse(body);
|
|
300
|
+
} catch {
|
|
301
|
+
throw new AgentPressError("Webhook payload is not valid JSON");
|
|
302
|
+
}
|
|
303
|
+
}
|
|
284
304
|
};
|
|
285
|
-
|
|
286
|
-
|
|
305
|
+
//#endregion
|
|
306
|
+
//#region src/client.ts
|
|
307
|
+
/**
|
|
308
|
+
* Main entry point for the AgentPress SDK. Provides namespaced access to API
|
|
309
|
+
* resources (e.g., `client.webhooks.send()`, `client.actions.approve()`).
|
|
310
|
+
* Validates all configuration options at construction time, so invalid config fails fast.
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```ts
|
|
314
|
+
* const client = new AgentPress({
|
|
315
|
+
* apiKey: "ak_...",
|
|
316
|
+
* webhookSecret: "whsec_...",
|
|
317
|
+
* });
|
|
318
|
+
* await client.webhooks.send({ action: "my_action", payload: { ... } });
|
|
319
|
+
* await client.actions.approve("act_123", { action: "my_action" });
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
287
322
|
var AgentPress = class {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
323
|
+
/** Webhook operations: send outbound webhooks and verify inbound signatures. */
|
|
324
|
+
webhooks;
|
|
325
|
+
/** Action management: approve or reject staged actions. */
|
|
326
|
+
actions;
|
|
327
|
+
/**
|
|
328
|
+
* @param options - SDK configuration. All fields are optional with sensible defaults.
|
|
329
|
+
* @throws {ConfigurationError} If `timeout` is non-positive or `webhookSecret` has an invalid prefix.
|
|
330
|
+
*/
|
|
331
|
+
constructor(options = {}) {
|
|
332
|
+
const resolved = resolveOptions(options);
|
|
333
|
+
const http = new HttpClient(resolved);
|
|
334
|
+
this.webhooks = new WebhooksClient(resolved, http);
|
|
335
|
+
this.actions = new ActionsClient(resolved, http);
|
|
336
|
+
}
|
|
302
337
|
};
|
|
303
338
|
function resolveOptions(options) {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
timeout,
|
|
319
|
-
org,
|
|
320
|
-
webhookSecret: options.webhookSecret,
|
|
321
|
-
apiKey: options.apiKey,
|
|
322
|
-
onRequest: options.onRequest,
|
|
323
|
-
onResponse: options.onResponse
|
|
324
|
-
};
|
|
339
|
+
const baseUrl = (options.baseUrl ?? "https://api.agent.press").replace(/\/+$/, "");
|
|
340
|
+
const timeout = options.timeout ?? 3e4;
|
|
341
|
+
const org = options.org ?? "default-org";
|
|
342
|
+
if (timeout <= 0 || !Number.isFinite(timeout)) throw new ConfigurationError("timeout must be a positive number");
|
|
343
|
+
if (options.webhookSecret !== void 0 && !options.webhookSecret.startsWith("whsec_")) throw new ConfigurationError("webhookSecret must start with \"whsec_\"");
|
|
344
|
+
return {
|
|
345
|
+
baseUrl,
|
|
346
|
+
timeout,
|
|
347
|
+
org,
|
|
348
|
+
webhookSecret: options.webhookSecret,
|
|
349
|
+
apiKey: options.apiKey,
|
|
350
|
+
onRequest: options.onRequest,
|
|
351
|
+
onResponse: options.onResponse
|
|
352
|
+
};
|
|
325
353
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
AgentPressError,
|
|
330
|
-
ConfigurationError,
|
|
331
|
-
HttpError,
|
|
332
|
-
TimeoutError,
|
|
333
|
-
WebhookSignatureError
|
|
334
|
-
};
|
|
354
|
+
//#endregion
|
|
355
|
+
export { ActionsClient, AgentPress, AgentPressError, ConfigurationError, HttpError, TimeoutError, WebhookSignatureError };
|
|
356
|
+
|
|
335
357
|
//# sourceMappingURL=index.mjs.map
|