@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
@@ -0,0 +1,97 @@
1
+ import { type PacerDeclaration, type PacerEntry } from "../pacer_internal.js";
2
+ import type { HandlerOptions, Schedule } from "../types.js";
3
+ import type { CapabilityContext } from "./context.js";
4
+ import { type StatefulCapabilityMode, type StatefulExecutionResult, type RuntimeContext as StatefulRuntimeContext } from "./stateful-capability.js";
5
+ export type AiConnectorArchetype = "chat" | "project";
6
+ /**
7
+ * AI connector syncs are incremental-only for now.
8
+ *
9
+ * Unlike collection syncs, AI connector ingestion does not yet support
10
+ * replace-style reconciliation semantics, so exposing `replace` here would
11
+ * encourage a misleading reset-and-replay model.
12
+ */
13
+ export type AiConnectorMode = Exclude<StatefulCapabilityMode, "replace">;
14
+ export type AiConnectorChatAuthor = {
15
+ id: string;
16
+ username: string;
17
+ };
18
+ export type AiConnectorChatMessage = {
19
+ id: string;
20
+ content: string;
21
+ timestamp: string;
22
+ editedTimestamp?: string;
23
+ author: AiConnectorChatAuthor;
24
+ };
25
+ export type AiConnectorChatChannel = {
26
+ id: string;
27
+ name: string;
28
+ };
29
+ export type AiConnectorChatRecord = {
30
+ createdAt: string;
31
+ updatedAt: string;
32
+ externalUrl?: string;
33
+ channel: AiConnectorChatChannel;
34
+ threadId?: string;
35
+ messages: AiConnectorChatMessage[];
36
+ chatType?: string;
37
+ };
38
+ export type AiConnectorProjectBlock = {
39
+ id: string;
40
+ type: string;
41
+ content: string;
42
+ };
43
+ export type AiConnectorProjectRecord = {
44
+ name: string;
45
+ createdAt: string;
46
+ updatedAt: string;
47
+ externalUrl?: string;
48
+ priority?: string;
49
+ teams?: string[];
50
+ targetEnd?: string;
51
+ createdBy?: {
52
+ id: string;
53
+ };
54
+ blocks: AiConnectorProjectBlock[];
55
+ };
56
+ export type AiConnectorRecordByArchetype = {
57
+ chat: AiConnectorChatRecord;
58
+ project: AiConnectorProjectRecord;
59
+ };
60
+ export type AiConnectorChangeUpsert<A extends AiConnectorArchetype> = {
61
+ type: "upsert";
62
+ key: string;
63
+ record: AiConnectorRecordByArchetype[A];
64
+ };
65
+ export type AiConnectorChange<A extends AiConnectorArchetype> = AiConnectorChangeUpsert<A>;
66
+ export type AiConnectorExecutionResult<A extends AiConnectorArchetype, State = unknown> = StatefulExecutionResult<AiConnectorChange<A>, State>;
67
+ export type AiConnectorConfiguration<A extends AiConnectorArchetype, State = unknown> = {
68
+ /**
69
+ * The Notion record ID of an existing custom connector in the target workspace.
70
+ *
71
+ * This must be the connector record UUID that Notion created, not an external
72
+ * source identifier.
73
+ */
74
+ aiConnectorId: string;
75
+ archetype: A;
76
+ mode?: AiConnectorMode;
77
+ schedule?: Schedule;
78
+ execute: (state: State | undefined, context: CapabilityContext) => Promise<AiConnectorExecutionResult<A, State>>;
79
+ };
80
+ export type AiConnectorCapability = ReturnType<typeof createAiConnectorCapability>;
81
+ export declare function createAiConnectorCapability<A extends AiConnectorArchetype, State = unknown>(key: string, configuration: AiConnectorConfiguration<A, State>, pacerDeclarations?: readonly PacerDeclaration[]): {
82
+ _tag: "ai_connector";
83
+ key: string;
84
+ config: {
85
+ schedule?: import("../types.js").SyncSchedule;
86
+ mode?: "incremental";
87
+ aiConnectorId: string;
88
+ archetype: A;
89
+ };
90
+ handler(runtimeContext?: StatefulRuntimeContext<State>, options?: HandlerOptions): Promise<{
91
+ nextPacerStates: Record<string, PacerEntry>;
92
+ nextUserContext?: State & ({} | null);
93
+ changes: AiConnectorChange<A>[];
94
+ hasMore: boolean;
95
+ }>;
96
+ };
97
+ //# sourceMappingURL=ai_connector.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai_connector.d.ts","sourceRoot":"","sources":["../../src/capabilities/ai_connector.ts"],"names":[],"mappings":"AACA,OAAO,EAGN,KAAK,gBAAgB,EACrB,KAAK,UAAU,EACf,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAGtD,OAAO,EAEN,KAAK,sBAAsB,EAC3B,KAAK,uBAAuB,EAC5B,KAAK,cAAc,IAAI,sBAAsB,EAC7C,MAAM,0BAA0B,CAAC;AAElC,MAAM,MAAM,oBAAoB,GAAG,MAAM,GAAG,SAAS,CAAC;AACtD;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,GAAG,OAAO,CAAC,sBAAsB,EAAE,SAAS,CAAC,CAAC;AAEzE,MAAM,MAAM,qBAAqB,GAAG;IACnC,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IACpC,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,MAAM,EAAE,qBAAqB,CAAC;CAC9B,CAAC;AAEF,MAAM,MAA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BAAoB,IAC3D,uBAAuB,CAAC,CAAC,CAAC,CAAC;AAE5B,MAAM,MAAM,0BAA0B,CACrC,CAAC,SAAS,oBAAoB,EAC9B,KAAK,GAAG,OAAO,IACZ,uBAAuB,CAAC,iBAAiB,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AAEzD,MAAM,MAAM,wBAAwB,CACnC,CAAC,SAAS,oBAAoB,EAC9B,KAAK,GAAG,OAAO,IACZ;IACH;;;;;OAKG;IACH,aAAa,EAAE,MAAM,CAAC;IACtB,SAAS,EAAE,CAAC,CAAC;IACb,IAAI,CAAC,EAAE,eAAe,CAAC;IACvB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,OAAO,EAAE,CACR,KAAK,EAAE,KAAK,GAAG,SAAS,EACxB,OAAO,EAAE,iBAAiB,KACtB,OAAO,CAAC,0BAA0B,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;CACnD,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,UAAU,CAC7C,OAAO,2BAA2B,CAClC,CAAC;AAgBF,wBAAgB,2BAA2B,CAC1C,CAAC,SAAS,oBAAoB,EAC9B,KAAK,GAAG,OAAO,EAEf,GAAG,EAAE,MAAM,EACX,aAAa,EAAE,wBAAwB,CAAC,CAAC,EAAE,KAAK,CAAC,EACjD,iBAAiB,CAAC,EAAE,SAAS,gBAAgB,EAAE;;;;;;;;;6BAc5B,sBAAsB,CAAC,KAAK,CAAC,YACpC,cAAc;;;;;;EAuC1B"}
@@ -0,0 +1,54 @@
1
+ import { ExecutionError } from "../error.js";
2
+ import {
3
+ getPacerState,
4
+ initPacerState
5
+ } from "../pacer_internal.js";
6
+ import { createCapabilityContext } from "./context.js";
7
+ import { writeOutput } from "./output.js";
8
+ import {
9
+ parseSchedule
10
+ } from "./stateful-capability.js";
11
+ function createAiConnectorCapability(key, configuration, pacerDeclarations) {
12
+ return {
13
+ _tag: "ai_connector",
14
+ key,
15
+ config: {
16
+ aiConnectorId: configuration.aiConnectorId,
17
+ archetype: configuration.archetype,
18
+ ...configuration.mode === void 0 ? {} : { mode: configuration.mode },
19
+ ...configuration.schedule === void 0 ? {} : { schedule: parseSchedule(configuration.schedule) }
20
+ },
21
+ async handler(runtimeContext, options) {
22
+ const capabilityContext = createCapabilityContext("ai_connector");
23
+ const state = runtimeContext?.state ?? runtimeContext?.userContext;
24
+ initPacerState(runtimeContext?.pacers, pacerDeclarations);
25
+ let executionResult;
26
+ try {
27
+ executionResult = await configuration.execute(state, capabilityContext);
28
+ } catch (err) {
29
+ const error = new ExecutionError(err);
30
+ if (!options?.concreteOutput) {
31
+ writeOutput({
32
+ _tag: "error",
33
+ error: { name: error.name, message: error.message }
34
+ });
35
+ }
36
+ throw error;
37
+ }
38
+ const result = {
39
+ changes: executionResult.changes,
40
+ hasMore: executionResult.hasMore,
41
+ ...executionResult.nextState === void 0 ? {} : { nextUserContext: executionResult.nextState },
42
+ nextPacerStates: getPacerState().pacers
43
+ };
44
+ if (options?.concreteOutput) {
45
+ return result;
46
+ }
47
+ writeOutput({ _tag: "success", value: result });
48
+ return result;
49
+ }
50
+ };
51
+ }
52
+ export {
53
+ createAiConnectorCapability
54
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ai_connector.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai_connector.test.d.ts","sourceRoot":"","sources":["../../src/capabilities/ai_connector.test.ts"],"names":[],"mappings":""}
@@ -1 +1 @@
1
- {"version":3,"file":"automation.d.ts","sourceRoot":"","sources":["../../src/capabilities/automation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAGtD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC/B;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,UAAU,EAAE,MAAM,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BAAuB;IACvC,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,MAAM,oBAAoB,GAAG,UAAU,CAC5C,OAAO,0BAA0B,CACjC,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CACzC,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,uBAAuB;;;;;;;mBAUtB,eAAe,YACZ,cAAc,GACtB,OAAO,CAAC;QAAE,MAAM,EAAE,SAAS,CAAA;KAAE,GAAG,SAAS,CAAC;EA4B9C"}
@@ -1,5 +1,6 @@
1
1
  import { ExecutionError } from "../error.js";
2
2
  import { createCapabilityContext } from "./context.js";
3
+ import { writeOutput } from "./output.js";
3
4
  function createAutomationCapability(key, config) {
4
5
  return {
5
6
  _tag: "automation",
@@ -10,25 +11,24 @@ function createAutomationCapability(key, config) {
10
11
  },
11
12
  async handler(event, options) {
12
13
  try {
13
- const capabilityContext = createCapabilityContext();
14
+ const capabilityContext = createCapabilityContext("automation");
14
15
  await config.execute(event, capabilityContext);
15
16
  if (options?.concreteOutput) {
16
17
  return { status: "success" };
17
18
  } else {
18
- process.stdout.write(
19
- `
20
- <__notion_output__>${JSON.stringify({ _tag: "success", value: { status: "success" } })}</__notion_output__>
21
- `
22
- );
19
+ writeOutput({ _tag: "success", value: { status: "success" } });
23
20
  }
24
21
  } catch (err) {
25
22
  const error = new ExecutionError(err);
26
23
  if (!options?.concreteOutput) {
27
- process.stdout.write(
28
- `
29
- <__notion_output__>${JSON.stringify({ _tag: "error", error: { name: error.name, message: error.message, trace: error.stack } })}</__notion_output__>
30
- `
31
- );
24
+ writeOutput({
25
+ _tag: "error",
26
+ error: {
27
+ name: error.name,
28
+ message: error.message,
29
+ trace: error.stack
30
+ }
31
+ });
32
32
  }
33
33
  throw error;
34
34
  }
@@ -1,7 +1,8 @@
1
1
  import { Client } from "@notionhq/client";
2
+ import type { CapabilityType } from "../worker.js";
2
3
  export type CapabilityContext = {
3
4
  /** Notion API SDK client for this execution. */
4
5
  notion: Client;
5
6
  };
6
- export declare function createCapabilityContext(): CapabilityContext;
7
+ export declare function createCapabilityContext(capabilityType: CapabilityType): CapabilityContext;
7
8
  //# sourceMappingURL=context.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/capabilities/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG1C,MAAM,MAAM,iBAAiB,GAAG;IAC/B,gDAAgD;IAChD,MAAM,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,wBAAgB,uBAAuB,IAAI,iBAAiB,CAc3D"}
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/capabilities/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE1C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD,MAAM,MAAM,iBAAiB,GAAG;IAC/B,gDAAgD;IAChD,MAAM,EAAE,MAAM,CAAC;CACf,CAAC;AAwCF,wBAAgB,uBAAuB,CACtC,cAAc,EAAE,cAAc,GAC5B,iBAAiB,CAenB"}
@@ -1,12 +1,30 @@
1
1
  import { Client } from "@notionhq/client";
2
- function createCapabilityContext() {
2
+ const FIX_STEPS = "\n\nTo fix this:\n1. Create an internal integration at https://www.notion.so/profile/integrations/internal\n2. Give the integration access to the relevant pages/databases\n3. Add NOTION_API_TOKEN=<your-token> to your .env file\n4. For deployed workers, run: ntn workers env push";
3
+ function missingTokenMessage(capabilityType) {
4
+ if (capabilityType === "tool") {
5
+ return "NOTION_API_TOKEN is not set. When tools are called through a Custom Agent, the platform sets NOTION_API_TOKEN automatically, using the permissions of the Custom Agent. Outside of that context (e.g. ntn workers exec), you must set it yourself." + FIX_STEPS;
6
+ }
7
+ return `NOTION_API_TOKEN is not set. context.notion requires an API token to make requests in ${capabilityType} capabilities to Notion.` + FIX_STEPS;
8
+ }
9
+ function createUnauthenticatedNotionProxy(capabilityType) {
10
+ return new Proxy({}, {
11
+ get(_target, prop) {
12
+ if (prop === "then" || typeof prop === "symbol") {
13
+ return void 0;
14
+ }
15
+ throw new Error(missingTokenMessage(capabilityType));
16
+ }
17
+ });
18
+ }
19
+ function createCapabilityContext(capabilityType) {
3
20
  const options = {};
4
21
  if (process.env.NOTION_API_BASE_URL) {
5
22
  options.baseUrl = process.env.NOTION_API_BASE_URL;
6
23
  }
7
- if (process.env.NOTION_API_TOKEN) {
8
- options.auth = process.env.NOTION_API_TOKEN;
24
+ if (!process.env.NOTION_API_TOKEN) {
25
+ return { notion: createUnauthenticatedNotionProxy(capabilityType) };
9
26
  }
27
+ options.auth = process.env.NOTION_API_TOKEN;
10
28
  const notion = new Client(options);
11
29
  return { notion };
12
30
  }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Writes a notion_output envelope to stdout using the shared wire format.
3
+ */
4
+ export declare function writeOutput(value: unknown): void;
5
+ //# sourceMappingURL=output.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"output.d.ts","sourceRoot":"","sources":["../../src/capabilities/output.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAIhD"}
@@ -0,0 +1,10 @@
1
+ function writeOutput(value) {
2
+ process.stdout.write(
3
+ `
4
+ <__notion_output__>${JSON.stringify(value)}</__notion_output__>
5
+ `
6
+ );
7
+ }
8
+ export {
9
+ writeOutput
10
+ };
@@ -0,0 +1,30 @@
1
+ import type { PacerEntry } from "../pacer_internal.js";
2
+ import type { Schedule, SyncSchedule } from "../types.js";
3
+ /**
4
+ * Shared lifecycle mode used by sync-like capabilities.
5
+ */
6
+ export type StatefulCapabilityMode = "replace" | "incremental";
7
+ /**
8
+ * Base runtime context shared by sync-like capabilities.
9
+ */
10
+ export type RuntimeContext<UserContext = unknown> = {
11
+ /** The user-defined/-controlled state (cursor, pagination state, etc.) */
12
+ state?: UserContext;
13
+ /** Legacy field for user-defined/-controlled state. */
14
+ userContext?: UserContext;
15
+ /** Pacer state from the server for rate limiting. */
16
+ pacers?: Record<string, PacerEntry>;
17
+ };
18
+ /**
19
+ * Shared execution result returned from stateful capability execute functions.
20
+ */
21
+ export type StatefulExecutionResult<Change, State = unknown> = {
22
+ changes: Change[];
23
+ hasMore: boolean;
24
+ nextState?: State;
25
+ };
26
+ /**
27
+ * Parses a user-friendly schedule string into the normalized backend format.
28
+ */
29
+ export declare function parseSchedule(schedule: Schedule): SyncSchedule;
30
+ //# sourceMappingURL=stateful-capability.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stateful-capability.d.ts","sourceRoot":"","sources":["../../src/capabilities/stateful-capability.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAY,MAAM,aAAa,CAAC;AAEpE;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG,SAAS,GAAG,aAAa,CAAC;AAE/D;;GAEG;AACH,MAAM,MAAM,cAAc,CAAC,WAAW,GAAG,OAAO,IAAI;IACnD,0EAA0E;IAC1E,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,uDAAuD;IACvD,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,qDAAqD;IACrD,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CACpC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,uBAAuB,CAAC,MAAM,EAAE,KAAK,GAAG,OAAO,IAAI;IAC9D,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,CAAC,EAAE,KAAK,CAAC;CAClB,CAAC;AASF;;GAEG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,QAAQ,GAAG,YAAY,CA8C9D"}
@@ -0,0 +1,50 @@
1
+ import { unreachable } from "../error.js";
2
+ const MS_PER_MINUTE = 60 * 1e3;
3
+ const MS_PER_HOUR = 60 * MS_PER_MINUTE;
4
+ const MS_PER_DAY = 24 * MS_PER_HOUR;
5
+ const MIN_INTERVAL_MS = MS_PER_MINUTE;
6
+ const MAX_INTERVAL_MS = 7 * MS_PER_DAY;
7
+ function parseSchedule(schedule) {
8
+ if (schedule === "continuous") {
9
+ return { type: "continuous" };
10
+ }
11
+ if (schedule === "manual") {
12
+ return { type: "manual" };
13
+ }
14
+ const match = schedule.match(/^(\d+)(m|h|d)$/);
15
+ if (!match || !match[1] || !match[2]) {
16
+ throw new Error(
17
+ `Invalid schedule format: "${schedule}". Use "continuous" or an interval like "30m", "1h", "1d".`
18
+ );
19
+ }
20
+ const value = parseInt(match[1], 10);
21
+ const unit = match[2];
22
+ let intervalMs;
23
+ switch (unit) {
24
+ case "m":
25
+ intervalMs = value * MS_PER_MINUTE;
26
+ break;
27
+ case "h":
28
+ intervalMs = value * MS_PER_HOUR;
29
+ break;
30
+ case "d":
31
+ intervalMs = value * MS_PER_DAY;
32
+ break;
33
+ default:
34
+ unreachable(unit);
35
+ }
36
+ if (intervalMs < MIN_INTERVAL_MS) {
37
+ throw new Error(
38
+ `Schedule interval must be at least 1 minute. Got: "${schedule}"`
39
+ );
40
+ }
41
+ if (intervalMs > MAX_INTERVAL_MS) {
42
+ throw new Error(
43
+ `Schedule interval must be at most 7 days. Got: "${schedule}"`
44
+ );
45
+ }
46
+ return { type: "interval", intervalMs };
47
+ }
48
+ export {
49
+ parseSchedule
50
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=stateful-capability.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stateful-capability.test.d.ts","sourceRoot":"","sources":["../../src/capabilities/stateful-capability.test.ts"],"names":[],"mappings":""}
@@ -1,8 +1,9 @@
1
- import { type PacerEntry } from "../pacer_internal.js";
1
+ import { type PacerDeclaration } from "../pacer_internal.js";
2
2
  import type { PropertyConfiguration, PropertySchema, Schema } from "../schema.js";
3
- import type { HandlerOptions, Icon, PeopleValue, PlaceValue, RelationValue, Schedule, SyncSchedule, TextValue } from "../types.js";
3
+ import type { HandlerOptions, Icon, PeopleValue, PlaceValue, RelationValue, Schedule, TextValue } from "../types.js";
4
4
  import type { DatabaseHandle } from "../worker.js";
5
5
  import type { CapabilityContext } from "./context.js";
6
+ import { type StatefulCapabilityMode, type RuntimeContext as StatefulRuntimeContext } from "./stateful-capability.js";
6
7
  /**
7
8
  * Maps a property configuration to its corresponding value type.
8
9
  */
@@ -16,7 +17,7 @@ type PropertyValueType<T extends PropertyConfiguration> = T extends {
16
17
  /**
17
18
  * Sync mode determines how the sync handles data lifecycle.
18
19
  */
19
- export type SyncMode = "replace" | "incremental";
20
+ export type SyncMode = StatefulCapabilityMode;
20
21
  /**
21
22
  * A change representing a record to be created or updated.
22
23
  */
@@ -156,17 +157,6 @@ export type SyncConfiguration<PK extends string, S extends Schema<PK>, State = u
156
157
  execute: (state: State | undefined, context: CapabilityContext) => Promise<SyncExecutionResult<PK, State>>;
157
158
  };
158
159
  export type SyncCapability = ReturnType<typeof createSyncCapability>;
159
- /**
160
- * Runtime context object passed from the runtime to sync capability handlers.
161
- */
162
- type RuntimeContext<UserContext = unknown> = {
163
- /** The user-defined/-controlled state (cursor, pagination state, etc.) */
164
- state?: UserContext;
165
- /** Legacy field for user-defined/-controlled state. */
166
- userContext?: UserContext;
167
- /** Pacer state from the server for rate limiting. */
168
- pacers?: Record<string, PacerEntry>;
169
- };
170
160
  /**
171
161
  * Creates a special handler for syncing third-party data to a database.
172
162
  *
@@ -174,16 +164,16 @@ type RuntimeContext<UserContext = unknown> = {
174
164
  * @returns A handler function that executes the sync function, and passes data
175
165
  * needed to complete the sync back to the platform.
176
166
  */
177
- export declare function createSyncCapability<PK extends string, S extends Schema<PK>, Context = unknown>(key: string, syncConfiguration: SyncConfiguration<PK, S, Context>): {
167
+ export declare function createSyncCapability<PK extends string, S extends Schema<PK>, Context = unknown>(key: string, syncConfiguration: SyncConfiguration<PK, S, Context>, pacerDeclarations?: readonly PacerDeclaration[]): {
178
168
  _tag: "sync";
179
169
  key: string;
180
170
  config: {
181
171
  databaseKey: string;
182
172
  primaryKeyProperty: PK;
183
- mode: SyncMode | undefined;
184
- schedule: SyncSchedule | undefined;
173
+ mode: StatefulCapabilityMode | undefined;
174
+ schedule: import("../types.js").SyncSchedule | undefined;
185
175
  };
186
- handler(runtimeContext?: RuntimeContext<Context>, options?: HandlerOptions): Promise<{
176
+ handler(runtimeContext?: StatefulRuntimeContext<Context>, options?: HandlerOptions): Promise<{
187
177
  changes: ({
188
178
  targetDatabaseKey: string;
189
179
  /**
@@ -230,7 +220,7 @@ export declare function createSyncCapability<PK extends string, S extends Schema
230
220
  })[];
231
221
  hasMore: boolean;
232
222
  nextUserContext: Context | undefined;
233
- nextPacerStates: Record<string, PacerEntry>;
223
+ nextPacerStates: Record<string, import("../pacer_internal.js").PacerEntry>;
234
224
  }>;
235
225
  };
236
226
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../../src/capabilities/sync.ts"],"names":[],"mappings":"AACA,OAAO,EAEN,KAAK,UAAU,EAEf,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EACX,qBAAqB,EACrB,cAAc,EACd,MAAM,EACN,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EACX,cAAc,EACd,IAAI,EACJ,WAAW,EACX,UAAU,EACV,aAAa,EACb,QAAQ,EACR,YAAY,EACZ,SAAS,EAET,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAGtD;;GAEG;AACH,KAAK,iBAAiB,CAAC,CAAC,SAAS,qBAAqB,IAAI,CAAC,SAAS;IACnE,IAAI,EAAE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AAiB,CAC5B,EAAE,SAAS,MAAM,EACjB,CAAC,SAAS,MAAM,CAAC,EAAE,CAAC,EACpB,KAAK,GAAG,OAAO,IACZ;IACH;;OAEG;IACH,QAAQ,EAAE,cAAc,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IAEhC;;;;;;;;;OASG;IACH,IAAI,CAAC,EAAE,QAAQ,CAAC;IAEhB;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAEpB;;;;;;;;;;;;;;OAcG;IACH,OAAO,EAAE,CACR,KAAK,EAAE,KAAK,GAAG,SAAS,EACxB,OAAO,EAAE,iBAAiB,KACtB,OAAO,CAAC,mBAAmB,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,CAAC;CAC7C,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAErE;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CACnC,EAAE,SAAS,MAAM,EACjB,CAAC,SAAS,MAAM,CAAC,EAAE,CAAC,EACpB,OAAO,GAAG,OAAO,EAEjB,GAAG,EAAE,MAAM,EACX,iBAAiB,EAAE,iBAAiB,CAAC,EAAE,EAAE,CAAC,EAAE,OAAO,CAAC,EACpD,iBAAiB,CAAC,EAAE,SAAS,gBAAgB,EAAE;;;;;;;;;6BAc5B,sBAAsB,CAAC,OAAO,CAAC,YACtC,cAAc;;;YA3I1B;;eAEG;kBACG,QAAQ;YACd;;eAEG;iBACE,MAAM;;;YApDX;;eAEG;kBACG,QAAQ;YACd;;eAEG;iBACE,MAAM;YAMX;;;;eAIG;6FAED,QAAQ;YAEV;;;;;eAKG;gCACiB,MAAM;YAC1B;;;eAGG;mBACI,IAAI;YACX;;;eAGG;kCACmB,MAAM;;;;;;EAwN5B"}
@@ -1,10 +1,14 @@
1
- import { ExecutionError, unreachable } from "../error.js";
1
+ import { ExecutionError, RateLimitError } from "../error.js";
2
2
  import {
3
3
  getPacerState,
4
- setPacerState
4
+ initPacerState
5
5
  } from "../pacer_internal.js";
6
6
  import { createCapabilityContext } from "./context.js";
7
- function createSyncCapability(key, syncConfiguration) {
7
+ import { writeOutput } from "./output.js";
8
+ import {
9
+ parseSchedule
10
+ } from "./stateful-capability.js";
11
+ function createSyncCapability(key, syncConfiguration, pacerDeclarations) {
8
12
  return {
9
13
  _tag: "sync",
10
14
  key,
@@ -15,9 +19,9 @@ function createSyncCapability(key, syncConfiguration) {
15
19
  schedule: syncConfiguration.schedule ? parseSchedule(syncConfiguration.schedule) : void 0
16
20
  },
17
21
  async handler(runtimeContext, options) {
18
- const capabilityContext = createCapabilityContext();
22
+ const capabilityContext = createCapabilityContext("sync");
19
23
  const state = runtimeContext?.state ?? runtimeContext?.userContext;
20
- setPacerState({ pacers: runtimeContext?.pacers ?? {} });
24
+ initPacerState(runtimeContext?.pacers, pacerDeclarations);
21
25
  let executionResult;
22
26
  try {
23
27
  executionResult = await syncConfiguration.execute(
@@ -25,13 +29,32 @@ function createSyncCapability(key, syncConfiguration) {
25
29
  capabilityContext
26
30
  );
27
31
  } catch (err) {
32
+ if (err instanceof RateLimitError) {
33
+ if (!options?.concreteOutput) {
34
+ const envelope = {
35
+ _tag: "error",
36
+ error: {
37
+ _tag: "rate_limit",
38
+ name: err.name,
39
+ message: err.message,
40
+ ...err.retryAfter !== void 0 ? { retryAfter: err.retryAfter } : {}
41
+ }
42
+ };
43
+ writeOutput(envelope);
44
+ }
45
+ throw err;
46
+ }
28
47
  const error = new ExecutionError(err);
29
48
  if (!options?.concreteOutput) {
30
- process.stdout.write(
31
- `
32
- <__notion_output__>${JSON.stringify({ _tag: "error", error: { name: error.name, message: error.message } })}</__notion_output__>
33
- `
34
- );
49
+ const envelope = {
50
+ _tag: "error",
51
+ error: {
52
+ _tag: "generic",
53
+ name: error.name,
54
+ message: error.message
55
+ }
56
+ };
57
+ writeOutput(envelope);
35
58
  }
36
59
  throw error;
37
60
  }
@@ -48,62 +71,12 @@ function createSyncCapability(key, syncConfiguration) {
48
71
  if (options?.concreteOutput) {
49
72
  return result;
50
73
  } else {
51
- process.stdout.write(
52
- `
53
- <__notion_output__>${JSON.stringify({ _tag: "success", value: result })}</__notion_output__>
54
- `
55
- );
74
+ writeOutput({ _tag: "success", value: result });
56
75
  }
57
76
  return result;
58
77
  }
59
78
  };
60
79
  }
61
- const MS_PER_MINUTE = 60 * 1e3;
62
- const MS_PER_HOUR = 60 * MS_PER_MINUTE;
63
- const MS_PER_DAY = 24 * MS_PER_HOUR;
64
- const MIN_INTERVAL_MS = MS_PER_MINUTE;
65
- const MAX_INTERVAL_MS = 7 * MS_PER_DAY;
66
- function parseSchedule(schedule) {
67
- if (schedule === "continuous") {
68
- return { type: "continuous" };
69
- }
70
- if (schedule === "manual") {
71
- return { type: "manual" };
72
- }
73
- const match = schedule.match(/^(\d+)(m|h|d)$/);
74
- if (!match || !match[1] || !match[2]) {
75
- throw new Error(
76
- `Invalid schedule format: "${schedule}". Use "continuous" or an interval like "30m", "1h", "1d".`
77
- );
78
- }
79
- const value = parseInt(match[1], 10);
80
- const unit = match[2];
81
- let intervalMs;
82
- switch (unit) {
83
- case "m":
84
- intervalMs = value * MS_PER_MINUTE;
85
- break;
86
- case "h":
87
- intervalMs = value * MS_PER_HOUR;
88
- break;
89
- case "d":
90
- intervalMs = value * MS_PER_DAY;
91
- break;
92
- default:
93
- unreachable(unit);
94
- }
95
- if (intervalMs < MIN_INTERVAL_MS) {
96
- throw new Error(
97
- `Schedule interval must be at least 1 minute. Got: "${schedule}"`
98
- );
99
- }
100
- if (intervalMs > MAX_INTERVAL_MS) {
101
- throw new Error(
102
- `Schedule interval must be at most 7 days. Got: "${schedule}"`
103
- );
104
- }
105
- return { type: "interval", intervalMs };
106
- }
107
80
  export {
108
81
  createSyncCapability
109
82
  };
@@ -1,11 +1,31 @@
1
1
  import type { SchemaBuilder } from "../schema-builder.js";
2
2
  import type { HandlerOptions, JSONValue } from "../types.js";
3
3
  import type { CapabilityContext } from "./context.js";
4
+ /**
5
+ * Hints describing a tool's behavior. Advisory metadata read by the platform
6
+ * (not the model) to decide whether a call is safe to auto-execute or
7
+ * requires user confirmation.
8
+ *
9
+ * Field names mirror the MCP tool annotation shape
10
+ * (`McpServerToolAnnotations`) so worker tools flow through the same
11
+ * confirmation / auto-execute machinery as MCP tools.
12
+ *
13
+ * Only hints with active consumers in the Notion client are included.
14
+ */
15
+ export interface ToolHints {
16
+ /**
17
+ * The tool only reads state and has no side effects. Safe to call
18
+ * repeatedly; safe to auto-execute under default policy. Tools without
19
+ * this hint are treated as write tools and prompt for confirmation.
20
+ */
21
+ readOnlyHint?: boolean;
22
+ }
4
23
  export interface ToolConfiguration<I extends JSONValue, O extends JSONValue = JSONValue> {
5
24
  title: string;
6
25
  description: string;
7
26
  schema: SchemaBuilder<I>;
8
27
  outputSchema?: SchemaBuilder<O>;
28
+ hints?: ToolHints;
9
29
  execute: (input: I, context: CapabilityContext) => O | Promise<O>;
10
30
  }
11
31
  /**
@@ -56,6 +76,7 @@ export declare function createToolCapability<I extends JSONValue, O extends JSON
56
76
  description: string;
57
77
  schema: import("../json-schema.js").JSONSchema<I>;
58
78
  outputSchema: import("../json-schema.js").JSONSchema<O> | undefined;
79
+ hints: ToolHints | undefined;
59
80
  };
60
81
  handler: {
61
82
  (input: JSONValue, options: HandlerOptions): Promise<O>;
@@ -1 +1 @@
1
- {"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/capabilities/tool.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC7D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAiEtD,MAAM,WAAW,iBAAiB,CACjC,CAAC,SAAS,SAAS,EACnB,CAAC,SAAS,SAAS,GAAG,SAAS;IAE/B,KAAK,EAAE,MAAM,CAAC;IACd,WAA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