@notionhq/workers 0.2.0 → 0.3.0

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 (50) hide show
  1. package/dist/capabilities/ai_connector.d.ts +97 -0
  2. package/dist/capabilities/ai_connector.d.ts.map +1 -0
  3. package/dist/capabilities/ai_connector.js +54 -0
  4. package/dist/capabilities/ai_connector.test.d.ts +2 -0
  5. package/dist/capabilities/ai_connector.test.d.ts.map +1 -0
  6. package/dist/capabilities/automation.d.ts.map +1 -1
  7. package/dist/capabilities/automation.js +10 -10
  8. package/dist/capabilities/output.d.ts +5 -0
  9. package/dist/capabilities/output.d.ts.map +1 -0
  10. package/dist/capabilities/output.js +10 -0
  11. package/dist/capabilities/stateful-capability.d.ts +30 -0
  12. package/dist/capabilities/stateful-capability.d.ts.map +1 -0
  13. package/dist/capabilities/stateful-capability.js +50 -0
  14. package/dist/capabilities/stateful-capability.test.d.ts +2 -0
  15. package/dist/capabilities/stateful-capability.test.d.ts.map +1 -0
  16. package/dist/capabilities/sync.d.ts +9 -19
  17. package/dist/capabilities/sync.d.ts.map +1 -1
  18. package/dist/capabilities/sync.js +13 -60
  19. package/dist/capabilities/tool.d.ts.map +1 -1
  20. package/dist/capabilities/tool.js +5 -20
  21. package/dist/capabilities/webhook.d.ts +101 -0
  22. package/dist/capabilities/webhook.d.ts.map +1 -0
  23. package/dist/capabilities/webhook.js +47 -0
  24. package/dist/index.d.ts +3 -0
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +2 -0
  27. package/dist/pacer_internal.d.ts +7 -0
  28. package/dist/pacer_internal.d.ts.map +1 -1
  29. package/dist/pacer_internal.js +17 -0
  30. package/dist/schema.d.ts +28 -11
  31. package/dist/schema.d.ts.map +1 -1
  32. package/dist/schema.js +2 -2
  33. package/dist/worker.d.ts +91 -9
  34. package/dist/worker.d.ts.map +1 -1
  35. package/dist/worker.js +111 -1
  36. package/package.json +1 -1
  37. package/src/capabilities/ai_connector.test.ts +341 -0
  38. package/src/capabilities/ai_connector.ts +194 -0
  39. package/src/capabilities/automation.ts +10 -6
  40. package/src/capabilities/output.ts +8 -0
  41. package/src/capabilities/stateful-capability.test.ts +25 -0
  42. package/src/capabilities/stateful-capability.ts +87 -0
  43. package/src/capabilities/sync.test.ts +89 -3
  44. package/src/capabilities/sync.ts +22 -86
  45. package/src/capabilities/tool.ts +5 -12
  46. package/src/capabilities/webhook.ts +148 -0
  47. package/src/index.ts +22 -0
  48. package/src/pacer_internal.ts +34 -0
  49. package/src/schema.ts +29 -12
  50. package/src/worker.ts +137 -10
package/src/worker.ts CHANGED
@@ -1,3 +1,9 @@
1
+ import type {
2
+ AiConnectorArchetype,
3
+ AiConnectorCapability,
4
+ AiConnectorConfiguration,
5
+ } from "./capabilities/ai_connector.js";
6
+ import { createAiConnectorCapability } from "./capabilities/ai_connector.js";
1
7
  import type {
2
8
  AutomationCapability,
3
9
  AutomationConfiguration,
@@ -16,6 +22,16 @@ import type { SyncCapability, SyncConfiguration } from "./capabilities/sync.js";
16
22
  import { createSyncCapability } from "./capabilities/sync.js";
17
23
  import type { ToolCapability, ToolConfiguration } from "./capabilities/tool.js";
18
24
  import { createToolCapability } from "./capabilities/tool.js";
25
+ import type {
26
+ WebhookCapability,
27
+ WebhookConfiguration,
28
+ WebhookEvent,
29
+ } from "./capabilities/webhook.js";
30
+ import {
31
+ createWebhookCapability,
32
+ WebhookVerificationError,
33
+ } from "./capabilities/webhook.js";
34
+ import type { PacerDeclaration } from "./pacer_internal.js";
19
35
  import { pacerWait } from "./pacer_internal.js";
20
36
  import type { Schema } from "./schema.js";
21
37
  import type { HandlerOptions, JSONValue } from "./types.js";
@@ -24,13 +40,17 @@ import type { HandlerOptions, JSONValue } from "./types.js";
24
40
  export type {
25
41
  AutomationConfiguration,
26
42
  AutomationEvent,
43
+ AiConnectorConfiguration,
27
44
  CapabilityContext,
28
45
  OAuthConfiguration,
29
46
  NotionManagedOAuthConfiguration,
30
47
  UserManagedOAuthConfiguration,
31
48
  SyncConfiguration,
32
49
  ToolConfiguration,
50
+ WebhookConfiguration,
51
+ WebhookEvent,
33
52
  };
53
+ export { WebhookVerificationError };
34
54
 
35
55
  // ============================================================================
36
56
  // Capability types
@@ -38,10 +58,12 @@ export type {
38
58
 
39
59
  type Capability =
40
60
  | SyncCapability
41
- // biome-ignore lint/suspicious/noExplicitAny: any is used to allow any input and output types
61
+ | AiConnectorCapability
62
+ // biome-ignore lint/suspicious/noExplicitAny: tool capabilities are generic over arbitrary input/output payloads.
42
63
  | ToolCapability<any, any>
43
64
  | AutomationCapability
44
- | OAuthCapability;
65
+ | OAuthCapability
66
+ | WebhookCapability;
45
67
 
46
68
  /**
47
69
  * Configuration for a database that Notion creates and manages on behalf
@@ -132,13 +154,8 @@ export type PacerHandle = {
132
154
  wait(): Promise<void>;
133
155
  };
134
156
 
135
- /**
136
- * A pacer declaration as it appears in the manifest.
137
- */
138
- export type PacerDeclaration = {
139
- readonly key: string;
140
- readonly config: PacerConfig;
141
- };
157
+ // Re-exported from pacer_internal.js (the canonical definition).
158
+ export type { PacerDeclaration };
142
159
 
143
160
  /** A capability entry stripped of its handler, used in the manifest. */
144
161
  type CapabilityManifestEntry = Pick<Capability, "_tag" | "key" | "config">;
@@ -286,7 +303,78 @@ export class Worker {
286
303
  config: SyncConfiguration<PK, S, Context>,
287
304
  ): SyncCapability {
288
305
  this.#validateUniqueKey(key);
289
- const capability = createSyncCapability(key, config);
306
+ const capability = createSyncCapability(
307
+ key,
308
+ config,
309
+ Array.from(this.#pacers.values()),
310
+ ) as SyncCapability;
311
+ this.#capabilities.set(key, capability);
312
+ return capability;
313
+ }
314
+
315
+ /**
316
+ * Register an AI connector capability.
317
+ *
318
+ * Example:
319
+ *
320
+ * ```ts
321
+ * const worker = new Worker();
322
+ * export default worker;
323
+ *
324
+ * worker.aiConnector("supportChats", {
325
+ * aiConnectorId: "connector-record-id",
326
+ * archetype: "chat",
327
+ * mode: "incremental",
328
+ * schedule: "15m",
329
+ * execute: async (state) => ({
330
+ * changes: [
331
+ * {
332
+ * type: "upsert",
333
+ * key: "thread-1",
334
+ * record: {
335
+ * createdAt: "2026-01-01T00:00:00Z",
336
+ * updatedAt: "2026-01-01T00:00:00Z",
337
+ * channel: {
338
+ * id: "channel-1",
339
+ * name: "Support",
340
+ * },
341
+ * threadId: "thread-1",
342
+ * messages: [
343
+ * {
344
+ * id: "message-1",
345
+ * content: "Hello from support",
346
+ * timestamp: "2026-01-01T00:00:00Z",
347
+ * author: {
348
+ * id: "user-1",
349
+ * username: "alice",
350
+ * },
351
+ * },
352
+ * ],
353
+ * },
354
+ * },
355
+ * ],
356
+ * hasMore: false,
357
+ * }),
358
+ * });
359
+ * ```
360
+ *
361
+ * `aiConnectorId` must be the Notion record ID of an existing custom
362
+ * connector in the workspace where you deploy this worker.
363
+ *
364
+ * @param key - The unique key for this capability.
365
+ * @param config - The AI connector configuration.
366
+ * @returns The capability object.
367
+ */
368
+ aiConnector<A extends AiConnectorArchetype, Context = unknown>(
369
+ key: string,
370
+ config: AiConnectorConfiguration<A, Context>,
371
+ ): AiConnectorCapability {
372
+ this.#validateUniqueKey(key);
373
+ const capability = createAiConnectorCapability(
374
+ key,
375
+ config,
376
+ Array.from(this.#pacers.values()),
377
+ ) as AiConnectorCapability;
290
378
  this.#capabilities.set(key, capability);
291
379
  return capability;
292
380
  }
@@ -366,6 +454,41 @@ export class Worker {
366
454
  return capability;
367
455
  }
368
456
 
457
+ /**
458
+ * Register a webhook capability.
459
+ *
460
+ * Webhooks expose an HTTP endpoint that external services can call.
461
+ * When hit, the request is processed by the worker's execute function.
462
+ *
463
+ * Example:
464
+ *
465
+ * ```ts
466
+ * const worker = new Worker();
467
+ * export default worker;
468
+ *
469
+ * worker.webhook("onGithubPush", {
470
+ * title: "GitHub Push Webhook",
471
+ * description: "Handles push events from GitHub",
472
+ * execute: async (events, { notion }) => {
473
+ * for (const event of events) {
474
+ * console.log("Received webhook:", event.body);
475
+ * console.log("Headers:", event.headers);
476
+ * }
477
+ * },
478
+ * })
479
+ * ```
480
+ *
481
+ * @param key - The unique key for this capability. Used as the webhook name in the URL.
482
+ * @param config - The webhook configuration.
483
+ * @returns The capability object.
484
+ */
485
+ webhook(key: string, config: WebhookConfiguration): WebhookCapability {
486
+ this.#validateUniqueKey(key);
487
+ const capability = createWebhookCapability(key, config);
488
+ this.#capabilities.set(key, capability);
489
+ return capability;
490
+ }
491
+
369
492
  /**
370
493
  * Register an OAuth capability.
371
494
  *
@@ -459,6 +582,10 @@ export class Worker {
459
582
  );
460
583
  }
461
584
 
585
+ if (!capability.handler) {
586
+ throw new Error(`Capability "${key}" cannot be executed`);
587
+ }
588
+
462
589
  // biome-ignore lint/suspicious/noExplicitAny: context is unknown, passed by external non-typed code.
463
590
  return capability.handler(context as any, options);
464
591
  }