@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
@@ -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,MAAM,sBAAsB,GAAG;IACpC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,sBAAsB,CAAC;IAChC,QAAQ,CAAC,E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G,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();
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,CAAC;IACnB;;OAEG;IACH,QAAQ,CAAC,EAAE,kBAAkB,CAAC;CAC9B;AAED;;;;;GAKG;AACH,MAAM,WAAW,kBAAkB;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,EAAE,EAAE,MAAM,CAAC;IACX,YAAY,EAAE,MAAM,CAAC;IACrB,gBAAgB,EAAE,MAAM,CAAC;IACzB,UAAU,EAAE;QAAE,EAAE,EAAE,MAAM,CAAA;K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O,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",
@@ -15,20 +16,19 @@ function createAutomationCapability(key, config) {
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
  }
@@ -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,a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;YAEV;;;;;eAKG;gCACiB,MAAM;YAC1B;;;eAGG;mBACI,IAAI;YACX;;;eAGG;kCACmB,MAAM;;;;;;EAiM5B"}
@@ -1,10 +1,14 @@
1
- import { ExecutionError, unreachable } from "../error.js";
1
+ import { ExecutionError } 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,
@@ -17,7 +21,7 @@ function createSyncCapability(key, syncConfiguration) {
17
21
  async handler(runtimeContext, options) {
18
22
  const capabilityContext = createCapabilityContext();
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(
@@ -27,11 +31,10 @@ function createSyncCapability(key, syncConfiguration) {
27
31
  } catch (err) {
28
32
  const error = new ExecutionError(err);
29
33
  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
- );
34
+ writeOutput({
35
+ _tag: "error",
36
+ error: { name: error.name, message: error.message }
37
+ });
35
38
  }
36
39
  throw error;
37
40
  }
@@ -48,62 +51,12 @@ function createSyncCapability(key, syncConfiguration) {
48
51
  if (options?.concreteOutput) {
49
52
  return result;
50
53
  } else {
51
- process.stdout.write(
52
- `
53
- <__notion_output__>${JSON.stringify({ _tag: "success", value: result })}</__notion_output__>
54
- `
55
- );
54
+ writeOutput({ _tag: "success", value: result });
56
55
  }
57
56
  return result;
58
57
  }
59
58
  };
60
59
  }
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
60
  export {
108
61
  createSyncCapability
109
62
  };
@@ -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,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S,GAAG,SAAS,EAC9B,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC;;;;;;;;;;gBAiBf,SAAS,WAAW,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC;gBAC/C,SAAS,GAAG,OAAO,CAC9C;YACA,IAAI,EAAE,SAAS,CAAC;YAChB,KAAK,EAAE,CAAC,CAAC;SACR,GACD;YACA,IAAI,EAAE,OAAO,CAAC;YACd,KAAK,EAAE;gBAAE,IAAI,EAAE,MAAM,CAAC;gBAAC,OAAO,EAAE,MAAM,CAAC;gBAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;aAAE,CAAC;SACnE,CACH;;EA4GD"}
@@ -2,6 +2,7 @@ import { Ajv } from "ajv";
2
2
  import addFormats from "ajv-formats";
3
3
  import { getSchema } from "../schema-builder.js";
4
4
  import { createCapabilityContext } from "./context.js";
5
+ import { writeOutput } from "./output.js";
5
6
  function validateRequiredProperties(schema, path = "") {
6
7
  if ("type" in schema && schema.type === "object" && "properties" in schema) {
7
8
  const propKeys = Object.keys(schema.properties).sort();
@@ -109,11 +110,7 @@ function createToolCapability(key, config) {
109
110
  _tag: "error",
110
111
  error: { name: error.name, message: error.message, trace: error.stack }
111
112
  };
112
- process.stdout.write(
113
- `
114
- <__notion_output__>${JSON.stringify(result)}</__notion_output__>
115
- `
116
- );
113
+ writeOutput(result);
117
114
  return result;
118
115
  }
119
116
  try {
@@ -134,21 +131,13 @@ function createToolCapability(key, config) {
134
131
  trace: error.stack
135
132
  }
136
133
  };
137
- process.stdout.write(
138
- `
139
- <__notion_output__>${JSON.stringify(result2)}</__notion_output__>
140
- `
141
- );
134
+ writeOutput(result2);
142
135
  return result2;
143
136
  }
144
137
  if (options?.concreteOutput) {
145
138
  return result;
146
139
  }
147
- process.stdout.write(
148
- `
149
- <__notion_output__>${JSON.stringify({ _tag: "success", value: result })}</__notion_output__>
150
- `
151
- );
140
+ writeOutput({ _tag: "success", value: result });
152
141
  return {
153
142
  _tag: "success",
154
143
  value: result
@@ -164,11 +153,7 @@ function createToolCapability(key, config) {
164
153
  _tag: "error",
165
154
  error: { name: error.name, message: error.message, trace: error.stack }
166
155
  };
167
- process.stdout.write(
168
- `
169
- <__notion_output__>${JSON.stringify(result)}</__notion_output__>
170
- `
171
- );
156
+ writeOutput(result);
172
157
  return result;
173
158
  }
174
159
  }