@agentuity/server 1.0.22 → 1.0.23

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.
Files changed (62) hide show
  1. package/dist/api/index.d.ts +1 -0
  2. package/dist/api/index.d.ts.map +1 -1
  3. package/dist/api/index.js +1 -0
  4. package/dist/api/index.js.map +1 -1
  5. package/dist/api/project/deploy.d.ts +8 -0
  6. package/dist/api/project/deploy.d.ts.map +1 -1
  7. package/dist/api/project/deploy.js +14 -13
  8. package/dist/api/project/deploy.js.map +1 -1
  9. package/dist/api/sandbox/execute.d.ts +2 -2
  10. package/dist/api/sandbox/execution.d.ts +4 -4
  11. package/dist/api/sandbox/snapshot-build.d.ts +2 -0
  12. package/dist/api/sandbox/snapshot-build.d.ts.map +1 -1
  13. package/dist/api/sandbox/snapshot-build.js +4 -0
  14. package/dist/api/sandbox/snapshot-build.js.map +1 -1
  15. package/dist/api/webhook/deliveries.d.ts +94 -0
  16. package/dist/api/webhook/deliveries.d.ts.map +1 -0
  17. package/dist/api/webhook/deliveries.js +79 -0
  18. package/dist/api/webhook/deliveries.js.map +1 -0
  19. package/dist/api/webhook/destinations.d.ts +136 -0
  20. package/dist/api/webhook/destinations.d.ts.map +1 -0
  21. package/dist/api/webhook/destinations.js +137 -0
  22. package/dist/api/webhook/destinations.js.map +1 -0
  23. package/dist/api/webhook/index.d.ts +41 -0
  24. package/dist/api/webhook/index.d.ts.map +1 -0
  25. package/dist/api/webhook/index.js +59 -0
  26. package/dist/api/webhook/index.js.map +1 -0
  27. package/dist/api/webhook/receipts.d.ts +77 -0
  28. package/dist/api/webhook/receipts.d.ts.map +1 -0
  29. package/dist/api/webhook/receipts.js +78 -0
  30. package/dist/api/webhook/receipts.js.map +1 -0
  31. package/dist/api/webhook/types.d.ts +249 -0
  32. package/dist/api/webhook/types.d.ts.map +1 -0
  33. package/dist/api/webhook/types.js +221 -0
  34. package/dist/api/webhook/types.js.map +1 -0
  35. package/dist/api/webhook/util.d.ts +202 -0
  36. package/dist/api/webhook/util.d.ts.map +1 -0
  37. package/dist/api/webhook/util.js +201 -0
  38. package/dist/api/webhook/util.js.map +1 -0
  39. package/dist/api/webhook/webhooks.d.ts +146 -0
  40. package/dist/api/webhook/webhooks.d.ts.map +1 -0
  41. package/dist/api/webhook/webhooks.js +165 -0
  42. package/dist/api/webhook/webhooks.js.map +1 -0
  43. package/dist/config.d.ts +1 -0
  44. package/dist/config.d.ts.map +1 -1
  45. package/dist/config.js +1 -0
  46. package/dist/config.js.map +1 -1
  47. package/dist/runtime-bootstrap.d.ts.map +1 -1
  48. package/dist/runtime-bootstrap.js +3 -0
  49. package/dist/runtime-bootstrap.js.map +1 -1
  50. package/package.json +4 -4
  51. package/src/api/index.ts +1 -0
  52. package/src/api/project/deploy.ts +16 -16
  53. package/src/api/sandbox/snapshot-build.ts +6 -0
  54. package/src/api/webhook/deliveries.ts +129 -0
  55. package/src/api/webhook/destinations.ts +224 -0
  56. package/src/api/webhook/index.ts +133 -0
  57. package/src/api/webhook/receipts.ts +124 -0
  58. package/src/api/webhook/types.ts +309 -0
  59. package/src/api/webhook/util.ts +237 -0
  60. package/src/api/webhook/webhooks.ts +260 -0
  61. package/src/config.ts +2 -0
  62. package/src/runtime-bootstrap.ts +3 -0
@@ -0,0 +1,309 @@
1
+ import { z } from 'zod';
2
+
3
+ // ============================================================================
4
+ // Webhook Destination Types
5
+ // ============================================================================
6
+
7
+ /**
8
+ * Webhook destination type schema.
9
+ *
10
+ * Currently only 'url' destinations are supported.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const destType = WebhookDestinationTypeSchema.parse('url');
15
+ * ```
16
+ */
17
+ export const WebhookDestinationTypeSchema = z.enum(['url']);
18
+
19
+ /**
20
+ * Webhook destination type.
21
+ */
22
+ export type WebhookDestinationType = z.infer<typeof WebhookDestinationTypeSchema>;
23
+
24
+ // ============================================================================
25
+ // Webhook Delivery Status
26
+ // ============================================================================
27
+
28
+ /**
29
+ * Webhook delivery status schema.
30
+ *
31
+ * - `pending`: Delivery is queued and waiting to be sent.
32
+ * - `success`: Delivery was completed successfully.
33
+ * - `failed`: Delivery failed after all retry attempts.
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * const status = WebhookDeliveryStatusSchema.parse('success');
38
+ * ```
39
+ */
40
+ export const WebhookDeliveryStatusSchema = z.enum(['pending', 'success', 'failed']);
41
+
42
+ /**
43
+ * Webhook delivery status type.
44
+ */
45
+ export type WebhookDeliveryStatus = z.infer<typeof WebhookDeliveryStatusSchema>;
46
+
47
+ // ============================================================================
48
+ // Core Schemas
49
+ // ============================================================================
50
+
51
+ /**
52
+ * Webhook schema representing a webhook endpoint.
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * const webhook = await getWebhook(client, 'wh_abc123');
57
+ * console.log(`Webhook: ${webhook.name} (${webhook.id})`);
58
+ * ```
59
+ */
60
+ export const WebhookSchema = z.object({
61
+ /** Unique identifier for the webhook (prefixed with wh_). */
62
+ id: z.string(),
63
+ /** ISO 8601 timestamp when the webhook was created. */
64
+ created_at: z.string(),
65
+ /** ISO 8601 timestamp when the webhook was last updated. */
66
+ updated_at: z.string(),
67
+ /** ID of the user who created the webhook. */
68
+ created_by: z.string(),
69
+ /** Human-readable webhook name. */
70
+ name: z.string(),
71
+ /** Optional description of the webhook's purpose. */
72
+ description: z.string().nullable().optional(),
73
+ /** Fully-qualified ingest URL for sending events to this webhook. Only present on create. */
74
+ url: z.string().optional(),
75
+ });
76
+
77
+ /**
78
+ * Webhook type.
79
+ */
80
+ export type Webhook = z.infer<typeof WebhookSchema>;
81
+
82
+ /**
83
+ * Webhook destination schema representing a delivery target for webhook events.
84
+ *
85
+ * Destinations define where incoming webhook payloads are forwarded to.
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * const destinations = await listWebhookDestinations(client, 'wh_abc123');
90
+ * for (const dest of destinations) {
91
+ * console.log(`Destination ${dest.id}: type=${dest.type}`);
92
+ * }
93
+ * ```
94
+ */
95
+ export const WebhookDestinationSchema = z.object({
96
+ /** Unique identifier for the destination (prefixed with whds_). */
97
+ id: z.string(),
98
+ /** ISO 8601 timestamp when the destination was created. */
99
+ created_at: z.string(),
100
+ /** ISO 8601 timestamp when the destination was last updated. */
101
+ updated_at: z.string(),
102
+ /** ID of the user who created the destination. */
103
+ created_by: z.string(),
104
+ /** ID of the webhook this destination belongs to. */
105
+ webhook_id: z.string(),
106
+ /** Type of destination (currently only 'url'). */
107
+ type: WebhookDestinationTypeSchema,
108
+ /** Configuration object for the destination (e.g., URL, headers). */
109
+ config: z.record(z.string(), z.unknown()),
110
+ });
111
+
112
+ /**
113
+ * Webhook destination type.
114
+ */
115
+ export type WebhookDestination = z.infer<typeof WebhookDestinationSchema>;
116
+
117
+ /**
118
+ * Webhook receipt schema representing an incoming webhook payload that was received.
119
+ *
120
+ * Receipts capture the raw payload and headers of each incoming webhook request.
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * const receipt = await getWebhookReceipt(client, 'wh_abc123', 'whrc_def456');
125
+ * console.log(`Receipt ${receipt.id}: received at ${receipt.date}`);
126
+ * ```
127
+ */
128
+ export const WebhookReceiptSchema = z.object({
129
+ /** Unique identifier for the receipt (prefixed with whrc_). */
130
+ id: z.string(),
131
+ /** ISO 8601 timestamp when the receipt was recorded. */
132
+ date: z.string(),
133
+ /** ID of the webhook this receipt belongs to. */
134
+ webhook_id: z.string(),
135
+ /** HTTP headers from the incoming webhook request. */
136
+ headers: z.record(z.string(), z.unknown()),
137
+ /** Raw payload from the incoming webhook request (can be any type). */
138
+ payload: z.unknown(),
139
+ });
140
+
141
+ /**
142
+ * Webhook receipt type.
143
+ */
144
+ export type WebhookReceipt = z.infer<typeof WebhookReceiptSchema>;
145
+
146
+ /**
147
+ * Webhook delivery schema representing a delivery attempt to a destination.
148
+ *
149
+ * Deliveries track the status and result of forwarding a received webhook
150
+ * payload to a configured destination.
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * const deliveries = await listWebhookDeliveries(client, 'wh_abc123');
155
+ * for (const delivery of deliveries) {
156
+ * console.log(`Delivery ${delivery.id}: ${delivery.status}`);
157
+ * }
158
+ * ```
159
+ */
160
+ export const WebhookDeliverySchema = z.object({
161
+ /** Unique identifier for the delivery (prefixed with whdv_). */
162
+ id: z.string(),
163
+ /** ISO 8601 timestamp when the delivery was attempted. */
164
+ date: z.string(),
165
+ /** ID of the webhook this delivery belongs to. */
166
+ webhook_id: z.string(),
167
+ /** ID of the destination this delivery was sent to. */
168
+ webhook_destination_id: z.string(),
169
+ /** ID of the receipt that triggered this delivery. */
170
+ webhook_receipt_id: z.string(),
171
+ /** Current status of the delivery. */
172
+ status: WebhookDeliveryStatusSchema,
173
+ /** Number of retry attempts made. */
174
+ retries: z.number(),
175
+ /** Error message if the delivery failed. */
176
+ error: z.string().nullable().optional(),
177
+ /** Response data from the destination (if available). */
178
+ response: z.record(z.string(), z.unknown()).nullable().optional(),
179
+ });
180
+
181
+ /**
182
+ * Webhook delivery type.
183
+ */
184
+ export type WebhookDelivery = z.infer<typeof WebhookDeliverySchema>;
185
+
186
+ // ============================================================================
187
+ // API Options
188
+ // ============================================================================
189
+
190
+ /**
191
+ * Common options for webhook API calls.
192
+ *
193
+ * Used to pass organization context when calling from CLI or other
194
+ * contexts where the org is not implicit in the authentication token.
195
+ */
196
+ export interface WebhookApiOptions {
197
+ /**
198
+ * Organization ID for the request.
199
+ * Required when using user authentication (CLI) instead of SDK key.
200
+ */
201
+ orgId?: string;
202
+ }
203
+
204
+ // ============================================================================
205
+ // Request Schemas
206
+ // ============================================================================
207
+
208
+ /**
209
+ * Request schema for creating a new webhook.
210
+ *
211
+ * @example
212
+ * ```typescript
213
+ * const request: CreateWebhookRequest = {
214
+ * name: 'github-events',
215
+ * description: 'Receives GitHub webhook events',
216
+ * };
217
+ * ```
218
+ */
219
+ export const CreateWebhookRequestSchema = z.object({
220
+ /** Human-readable name for the webhook. */
221
+ name: z.string(),
222
+ /** Optional description of the webhook's purpose. */
223
+ description: z.string().optional(),
224
+ });
225
+
226
+ /** Request type for creating a webhook. */
227
+ export type CreateWebhookRequest = z.infer<typeof CreateWebhookRequestSchema>;
228
+
229
+ /**
230
+ * Request schema for updating an existing webhook.
231
+ *
232
+ * @example
233
+ * ```typescript
234
+ * const request: UpdateWebhookRequest = {
235
+ * name: 'github-events-v2',
236
+ * description: 'Updated description',
237
+ * };
238
+ * ```
239
+ */
240
+ export const UpdateWebhookRequestSchema = z.object({
241
+ /** New name for the webhook. */
242
+ name: z.string(),
243
+ /** New description for the webhook. */
244
+ description: z.string().optional(),
245
+ });
246
+
247
+ /** Request type for updating a webhook. */
248
+ export type UpdateWebhookRequest = z.infer<typeof UpdateWebhookRequestSchema>;
249
+
250
+ /**
251
+ * Request schema for creating a webhook destination.
252
+ *
253
+ * @example
254
+ * ```typescript
255
+ * const request: CreateWebhookDestinationRequest = {
256
+ * type: 'url',
257
+ * config: { url: 'https://api.example.com/webhook' },
258
+ * };
259
+ * ```
260
+ */
261
+ export const CreateWebhookDestinationRequestSchema = z.object({
262
+ /** Type of destination to create. */
263
+ type: WebhookDestinationTypeSchema,
264
+ /** Configuration object for the destination. */
265
+ config: z.record(z.string(), z.unknown()),
266
+ });
267
+
268
+ /** Request type for creating a webhook destination. */
269
+ export type CreateWebhookDestinationRequest = z.infer<typeof CreateWebhookDestinationRequestSchema>;
270
+
271
+ /**
272
+ * Request schema for updating a webhook destination.
273
+ *
274
+ * @example
275
+ * ```typescript
276
+ * const request: UpdateWebhookDestinationRequest = {
277
+ * config: { url: 'https://api.example.com/webhook/v2' },
278
+ * };
279
+ * ```
280
+ */
281
+ export const UpdateWebhookDestinationRequestSchema = z.object({
282
+ /** Updated configuration object for the destination. */
283
+ config: z.record(z.string(), z.unknown()).optional(),
284
+ });
285
+
286
+ /** Request type for updating a webhook destination. */
287
+ export type UpdateWebhookDestinationRequest = z.infer<typeof UpdateWebhookDestinationRequestSchema>;
288
+
289
+ export const PaginationRequestSchema = z.object({
290
+ limit: z.number().optional(),
291
+ offset: z.number().optional(),
292
+ });
293
+
294
+ export type PaginationRequest = z.infer<typeof PaginationRequestSchema>;
295
+
296
+ export const ListWebhooksRequestSchema = PaginationRequestSchema;
297
+
298
+ /** Request type for listing webhooks. */
299
+ export type ListWebhooksRequest = z.infer<typeof ListWebhooksRequestSchema>;
300
+
301
+ export const ListWebhookReceiptsRequestSchema = PaginationRequestSchema;
302
+
303
+ /** Request type for listing webhook receipts. */
304
+ export type ListWebhookReceiptsRequest = z.infer<typeof ListWebhookReceiptsRequestSchema>;
305
+
306
+ export const ListWebhookDeliveriesRequestSchema = PaginationRequestSchema;
307
+
308
+ /** Request type for listing webhook deliveries. */
309
+ export type ListWebhookDeliveriesRequest = z.infer<typeof ListWebhookDeliveriesRequestSchema>;
@@ -0,0 +1,237 @@
1
+ import { StructuredError } from '@agentuity/core';
2
+ import { APIError } from '../api.ts';
3
+
4
+ // ============================================================================
5
+ // Error Types
6
+ // ============================================================================
7
+
8
+ /**
9
+ * General webhook operation error.
10
+ *
11
+ * Thrown when a webhook operation fails for reasons other than not-found.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * try {
16
+ * await createWebhook(client, { name: 'my-webhook' });
17
+ * } catch (error) {
18
+ * if (error instanceof WebhookError) {
19
+ * console.error(`Webhook operation failed: ${error.message}`);
20
+ * }
21
+ * }
22
+ * ```
23
+ */
24
+ export const WebhookError = StructuredError('WebhookError')<{ webhookId?: string }>();
25
+
26
+ /**
27
+ * Error thrown when a webhook is not found.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * try {
32
+ * await getWebhook(client, 'wh_nonexistent');
33
+ * } catch (error) {
34
+ * if (error instanceof WebhookNotFoundError) {
35
+ * console.error(`Webhook not found: ${error.webhookId}`);
36
+ * }
37
+ * }
38
+ * ```
39
+ */
40
+ export const WebhookNotFoundError = StructuredError('WebhookNotFoundError')<{
41
+ webhookId: string;
42
+ }>();
43
+
44
+ /**
45
+ * Error thrown when a webhook destination is not found.
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * try {
50
+ * await deleteWebhookDestination(client, 'wh_abc', 'whds_nonexistent');
51
+ * } catch (error) {
52
+ * if (error instanceof WebhookDestinationNotFoundError) {
53
+ * console.error(`Destination ${error.destinationId} not found`);
54
+ * }
55
+ * }
56
+ * ```
57
+ */
58
+ export const WebhookDestinationNotFoundError = StructuredError('WebhookDestinationNotFoundError')<{
59
+ webhookId: string;
60
+ destinationId: string;
61
+ }>();
62
+
63
+ /**
64
+ * Error thrown when a webhook receipt is not found.
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * try {
69
+ * await getWebhookReceipt(client, 'wh_abc', 'whrc_nonexistent');
70
+ * } catch (error) {
71
+ * if (error instanceof WebhookReceiptNotFoundError) {
72
+ * console.error(`Receipt ${error.receiptId} not found`);
73
+ * }
74
+ * }
75
+ * ```
76
+ */
77
+ export const WebhookReceiptNotFoundError = StructuredError('WebhookReceiptNotFoundError')<{
78
+ webhookId: string;
79
+ receiptId: string;
80
+ }>();
81
+
82
+ /**
83
+ * Error thrown when a webhook delivery is not found.
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * try {
88
+ * await retryWebhookDelivery(client, 'wh_abc', 'whdv_nonexistent');
89
+ * } catch (error) {
90
+ * if (error instanceof WebhookDeliveryNotFoundError) {
91
+ * console.error(`Delivery ${error.deliveryId} not found`);
92
+ * }
93
+ * }
94
+ * ```
95
+ */
96
+ export const WebhookDeliveryNotFoundError = StructuredError('WebhookDeliveryNotFoundError')<{
97
+ webhookId: string;
98
+ deliveryId: string;
99
+ }>();
100
+
101
+ // ============================================================================
102
+ // API Path Helpers
103
+ // ============================================================================
104
+
105
+ /** Current Webhook API version. */
106
+ const WEBHOOK_API_VERSION = '2026-02-24';
107
+
108
+ /**
109
+ * Constructs a full API path for webhook operations.
110
+ *
111
+ * Pattern: `/webhook/${VERSION}/${verb}/${segments.join('/')}`
112
+ * Each verb is unique to avoid route conflicts with the Go mux.
113
+ *
114
+ * @param verb - The action verb (e.g., 'create', 'list', 'get', 'destination-list')
115
+ * @param segments - Additional path segments (e.g., webhook ID, sub-resource ID)
116
+ * @returns The full API path with version and verb prefix
117
+ *
118
+ * @internal
119
+ */
120
+ export function webhookApiPath(verb: string, ...segments: string[]): string {
121
+ const encoded = segments.map((s) => encodeURIComponent(s)).join('/');
122
+ if (encoded) {
123
+ return `/webhook/${WEBHOOK_API_VERSION}/${verb}/${encoded}`;
124
+ }
125
+ return `/webhook/${WEBHOOK_API_VERSION}/${verb}`;
126
+ }
127
+
128
+ /**
129
+ * Constructs a full API path for webhook operations with query string.
130
+ *
131
+ * @param verb - The action verb
132
+ * @param queryString - Query string to append (without leading ?)
133
+ * @param segments - Additional path segments
134
+ * @returns The full API path with version, verb, and query string
135
+ *
136
+ * @internal
137
+ */
138
+ export function webhookApiPathWithQuery(
139
+ verb: string,
140
+ queryString: string | undefined,
141
+ ...segments: string[]
142
+ ): string {
143
+ const basePath = webhookApiPath(verb, ...segments);
144
+ return queryString ? `${basePath}?${queryString}` : basePath;
145
+ }
146
+
147
+ /**
148
+ * Constructs the public ingest URL for a webhook.
149
+ *
150
+ * Pattern: `/webhook/{orgId}-{webhookId}` (non-versioned, public)
151
+ *
152
+ * @param orgId - The organization ID
153
+ * @param webhookId - The webhook ID (prefixed with wh_)
154
+ * @returns The public ingest URL
155
+ *
156
+ * @internal
157
+ */
158
+ export function webhookIngestPath(orgId: string, webhookId: string): string {
159
+ return `/webhook/${encodeURIComponent(orgId)}-${encodeURIComponent(webhookId)}`;
160
+ }
161
+
162
+ // ============================================================================
163
+ // Header Builder
164
+ // ============================================================================
165
+
166
+ /**
167
+ * Builds headers for webhook API requests.
168
+ *
169
+ * @param orgId - Optional organization ID for CLI authentication
170
+ * @returns Headers object to pass to API client
171
+ *
172
+ * @internal
173
+ */
174
+ export function buildWebhookHeaders(orgId?: string): Record<string, string> | undefined {
175
+ if (orgId) {
176
+ return { 'x-agentuity-orgid': orgId };
177
+ }
178
+ return undefined;
179
+ }
180
+
181
+ // ============================================================================
182
+ // Error Handling
183
+ // ============================================================================
184
+
185
+ /**
186
+ * Wraps an API call and translates APIError with HTTP status codes to domain-specific webhook errors.
187
+ *
188
+ * - 404 → WebhookNotFoundError / WebhookDestinationNotFoundError / WebhookReceiptNotFoundError / WebhookDeliveryNotFoundError
189
+ *
190
+ * @internal
191
+ */
192
+ export async function withWebhookErrorHandling<T>(
193
+ apiCall: () => Promise<T>,
194
+ context: {
195
+ webhookId?: string;
196
+ destinationId?: string;
197
+ receiptId?: string;
198
+ deliveryId?: string;
199
+ }
200
+ ): Promise<T> {
201
+ try {
202
+ return await apiCall();
203
+ } catch (error) {
204
+ if (error instanceof APIError) {
205
+ if (error.status === 404) {
206
+ if (context.deliveryId && context.webhookId) {
207
+ throw new WebhookDeliveryNotFoundError({
208
+ webhookId: context.webhookId,
209
+ deliveryId: context.deliveryId,
210
+ message: error.message,
211
+ });
212
+ }
213
+ if (context.receiptId && context.webhookId) {
214
+ throw new WebhookReceiptNotFoundError({
215
+ webhookId: context.webhookId,
216
+ receiptId: context.receiptId,
217
+ message: error.message,
218
+ });
219
+ }
220
+ if (context.destinationId && context.webhookId) {
221
+ throw new WebhookDestinationNotFoundError({
222
+ webhookId: context.webhookId,
223
+ destinationId: context.destinationId,
224
+ message: error.message,
225
+ });
226
+ }
227
+ if (context.webhookId) {
228
+ throw new WebhookNotFoundError({
229
+ webhookId: context.webhookId,
230
+ message: error.message,
231
+ });
232
+ }
233
+ }
234
+ }
235
+ throw error;
236
+ }
237
+ }