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