@notionhq/apps 0.0.17 → 0.0.18
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.
- package/AGENTS.md +27 -0
- package/README.md +60 -35
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/notion-as-code/database.d.ts +88 -48
- package/dist/notion-as-code/database.d.ts.map +1 -1
- package/dist/notion-as-code/database.js +132 -45
- package/dist/notion-as-code/database.test.d.ts +2 -0
- package/dist/notion-as-code/database.test.d.ts.map +1 -0
- package/dist/notion-as-code/index.d.ts +2 -2
- package/dist/notion-as-code/index.d.ts.map +1 -1
- package/dist/notion-as-code/intents.d.ts +34 -2
- package/dist/notion-as-code/intents.d.ts.map +1 -1
- package/dist/notion-as-code/schema.d.ts +8 -4
- package/dist/notion-as-code/schema.d.ts.map +1 -1
- package/dist/notion-as-code/views.d.ts +29 -10
- package/dist/notion-as-code/views.d.ts.map +1 -1
- package/dist/sync.d.ts +33 -15
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +12 -0
- package/dist/workflow-state.d.ts +13 -0
- package/dist/workflow-state.d.ts.map +1 -0
- package/dist/workflow-state.js +107 -0
- package/dist/workflow.d.ts +48 -3
- package/dist/workflow.d.ts.map +1 -1
- package/dist/workflow.js +111 -4
- package/docs/BUILD.md +96 -0
- package/package.json +1 -1
- package/skills/notion-as-code/SKILL.md +81 -49
- package/skills/sync/SKILL.md +21 -18
- package/skills/workflow/SKILL.md +52 -3
- package/src/cli/build.test.ts +124 -64
- package/src/index.ts +1 -0
- package/src/notion-as-code/database.test.ts +661 -0
- package/src/notion-as-code/database.ts +344 -127
- package/src/notion-as-code/index.ts +16 -1
- package/src/notion-as-code/intents.ts +39 -2
- package/src/notion-as-code/schema.ts +11 -3
- package/src/notion-as-code/views.ts +29 -10
- package/src/sync.test.ts +295 -0
- package/src/sync.ts +85 -21
- package/src/workflow-state.ts +152 -0
- package/src/workflow.test.ts +329 -1
- package/src/workflow.ts +180 -6
package/dist/workflow.js
CHANGED
|
@@ -12,6 +12,8 @@ import { writeOutput } from "./output.js";
|
|
|
12
12
|
import { resolveRuntimeInput } from "./runtime-input.js";
|
|
13
13
|
import { readRunMetadata } from "./runtime-metadata.js";
|
|
14
14
|
import { createWorkflowTriggers } from "./triggers.generated.js";
|
|
15
|
+
import { createWorkflowStepState } from "./workflow-state.js";
|
|
16
|
+
const MAX_WORKFLOW_WAIT_MS = 7 * 24 * 60 * 60 * 1e3;
|
|
15
17
|
import {
|
|
16
18
|
connections
|
|
17
19
|
} from "./connections.js";
|
|
@@ -33,6 +35,7 @@ function workflow(configuration) {
|
|
|
33
35
|
event = await resolveRuntimeInput(event);
|
|
34
36
|
const runMetadata = readRunMetadata();
|
|
35
37
|
const baseContext = createCapabilityContext();
|
|
38
|
+
const step = createStep(runMetadata);
|
|
36
39
|
const capabilityContext = {
|
|
37
40
|
...baseContext,
|
|
38
41
|
connections: createWorkflowConnections(
|
|
@@ -40,7 +43,8 @@ function workflow(configuration) {
|
|
|
40
43
|
connectionDeclarations
|
|
41
44
|
),
|
|
42
45
|
...runMetadata,
|
|
43
|
-
step
|
|
46
|
+
step,
|
|
47
|
+
wait: createWait(step)
|
|
44
48
|
};
|
|
45
49
|
await configuration.handler(event, capabilityContext);
|
|
46
50
|
if (options?.concreteOutput) {
|
|
@@ -48,6 +52,16 @@ function workflow(configuration) {
|
|
|
48
52
|
}
|
|
49
53
|
writeOutput({ _tag: "success", value: { status: "success" } });
|
|
50
54
|
} catch (err) {
|
|
55
|
+
if (err instanceof WorkflowWaitInterrupt) {
|
|
56
|
+
if (options?.concreteOutput) {
|
|
57
|
+
return err.result;
|
|
58
|
+
}
|
|
59
|
+
writeOutput({
|
|
60
|
+
_tag: "wait",
|
|
61
|
+
wait: err.result.wait
|
|
62
|
+
});
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
51
65
|
const error = new ExecutionError(err);
|
|
52
66
|
if (!options?.concreteOutput) {
|
|
53
67
|
writeOutput({
|
|
@@ -81,7 +95,8 @@ function writeStepEvent(event, value, startedAt, emittedAt = performance.now())
|
|
|
81
95
|
`
|
|
82
96
|
);
|
|
83
97
|
}
|
|
84
|
-
function createStep(
|
|
98
|
+
function createStep(metadata) {
|
|
99
|
+
const { runGroupId } = metadata;
|
|
85
100
|
const stepNameByKey = /* @__PURE__ */ new Map();
|
|
86
101
|
async function step(name, optionsOrFn, maybeFn) {
|
|
87
102
|
const options = typeof optionsOrFn === "function" ? void 0 : optionsOrFn;
|
|
@@ -98,7 +113,9 @@ function createStep({ runGroupId }) {
|
|
|
98
113
|
);
|
|
99
114
|
}
|
|
100
115
|
stepNameByKey.set(key, name);
|
|
101
|
-
const
|
|
116
|
+
const id = `${runGroupId}:${key}`;
|
|
117
|
+
const stepState = createWorkflowStepState(metadata, id);
|
|
118
|
+
const context = { id, state: stepState.state };
|
|
102
119
|
const event = { id: context.id, name, key };
|
|
103
120
|
const startedAt = performance.now();
|
|
104
121
|
writeStepEvent("started", event, startedAt, startedAt);
|
|
@@ -124,7 +141,7 @@ function createStep({ runGroupId }) {
|
|
|
124
141
|
);
|
|
125
142
|
return checkpoint.value;
|
|
126
143
|
}
|
|
127
|
-
const value = normalizeWorkflowStepResult(await fn(context));
|
|
144
|
+
const value = normalizeWorkflowStepResult(await stepState.run(() => fn(context)));
|
|
128
145
|
writeStepEvent("success", { ...event, value }, startedAt);
|
|
129
146
|
writeStepEvent(
|
|
130
147
|
"completed",
|
|
@@ -162,6 +179,96 @@ function createStep({ runGroupId }) {
|
|
|
162
179
|
}
|
|
163
180
|
return step;
|
|
164
181
|
}
|
|
182
|
+
class WorkflowWaitInterrupt extends Error {
|
|
183
|
+
constructor(result) {
|
|
184
|
+
super(`Workflow is waiting until ${new Date(result.wait.resumeAtMs).toISOString()}`);
|
|
185
|
+
this.result = result;
|
|
186
|
+
this.name = "WorkflowWaitInterrupt";
|
|
187
|
+
}
|
|
188
|
+
result;
|
|
189
|
+
}
|
|
190
|
+
function createWait(step) {
|
|
191
|
+
return {
|
|
192
|
+
async until(name, options) {
|
|
193
|
+
const keyInput = options.key ?? name;
|
|
194
|
+
const keySegments = [
|
|
195
|
+
"wait",
|
|
196
|
+
"until",
|
|
197
|
+
...typeof keyInput === "string" ? [keyInput] : keyInput
|
|
198
|
+
];
|
|
199
|
+
const resumeAtMs = await step(
|
|
200
|
+
name,
|
|
201
|
+
{ key: keySegments },
|
|
202
|
+
() => resolveWaitUntilMs(options)
|
|
203
|
+
);
|
|
204
|
+
if (Date.now() >= resumeAtMs) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
throw new WorkflowWaitInterrupt({
|
|
208
|
+
status: "waiting",
|
|
209
|
+
wait: {
|
|
210
|
+
type: "until",
|
|
211
|
+
name,
|
|
212
|
+
resumeAtMs
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function resolveWaitUntilMs(options) {
|
|
219
|
+
const now = Date.now();
|
|
220
|
+
if ("at" in options) {
|
|
221
|
+
const resumeAtMs = options.at.getTime();
|
|
222
|
+
if (!Number.isFinite(resumeAtMs)) {
|
|
223
|
+
throw new Error("Workflow wait date must be valid.");
|
|
224
|
+
}
|
|
225
|
+
assertWaitFitsCheckpointRetention(resumeAtMs - now);
|
|
226
|
+
return resumeAtMs;
|
|
227
|
+
}
|
|
228
|
+
const durationMs = workflowWaitDurationMs(options.after);
|
|
229
|
+
assertWaitFitsCheckpointRetention(durationMs);
|
|
230
|
+
return now + durationMs;
|
|
231
|
+
}
|
|
232
|
+
function workflowWaitDurationMs(duration) {
|
|
233
|
+
const entries = Object.entries(duration);
|
|
234
|
+
if (entries.length === 0) {
|
|
235
|
+
throw new Error("Workflow wait duration must specify at least one unit.");
|
|
236
|
+
}
|
|
237
|
+
let durationMs = 0;
|
|
238
|
+
for (const [unit, value] of entries) {
|
|
239
|
+
if (!Number.isFinite(value) || !Number.isInteger(value) || value <= 0) {
|
|
240
|
+
throw new Error("Workflow wait duration values must be positive finite integers.");
|
|
241
|
+
}
|
|
242
|
+
durationMs += value * waitDurationUnitMultiplier(unit);
|
|
243
|
+
if (!Number.isSafeInteger(durationMs)) {
|
|
244
|
+
throw new Error("Workflow wait duration is too large.");
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return durationMs;
|
|
248
|
+
}
|
|
249
|
+
function assertWaitFitsCheckpointRetention(durationMs) {
|
|
250
|
+
if (durationMs >= MAX_WORKFLOW_WAIT_MS) {
|
|
251
|
+
throw new Error("Workflow waits must be shorter than 7 days.");
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
function waitDurationUnitMultiplier(unit) {
|
|
255
|
+
switch (unit) {
|
|
256
|
+
case "milliseconds":
|
|
257
|
+
return 1;
|
|
258
|
+
case "seconds":
|
|
259
|
+
return 1e3;
|
|
260
|
+
case "minutes":
|
|
261
|
+
return 6e4;
|
|
262
|
+
case "hours":
|
|
263
|
+
return 36e5;
|
|
264
|
+
case "days":
|
|
265
|
+
return 864e5;
|
|
266
|
+
case "weeks":
|
|
267
|
+
return 6048e5;
|
|
268
|
+
default:
|
|
269
|
+
throw new Error(`Unsupported workflow wait duration unit: ${unit}`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
165
272
|
function createWorkflowStepKey(segments) {
|
|
166
273
|
if (segments.length === 0) {
|
|
167
274
|
throw new Error("Workflow step keys must contain at least one segment.");
|
package/docs/BUILD.md
CHANGED
|
@@ -20,6 +20,7 @@ corresponding capability. The filename becomes its key:
|
|
|
20
20
|
```text
|
|
21
21
|
my-app/
|
|
22
22
|
├── src/
|
|
23
|
+
│ ├── notion.ts
|
|
23
24
|
│ ├── workflows/
|
|
24
25
|
│ │ └── onPageCreated.ts
|
|
25
26
|
│ └── lib/
|
|
@@ -32,6 +33,10 @@ my-app/
|
|
|
32
33
|
|
|
33
34
|
Files elsewhere under `src/` are ordinary modules and enter the build only when imported
|
|
34
35
|
by a capability. Capability discovery does not recurse into subdirectories.
|
|
36
|
+
Notion-as-Code declarations may be inline or in an imported helper. Use a local
|
|
37
|
+
module such as `src/workflows/myWorkflow/lib/notion.ts` for resources owned by one
|
|
38
|
+
capability, and `src/notion.ts` for app-wide declarations or resources without a
|
|
39
|
+
natural capability owner.
|
|
35
40
|
|
|
36
41
|
## Pipeline
|
|
37
42
|
|
|
@@ -47,6 +52,97 @@ Capability modules must therefore be importable without secrets or network acces
|
|
|
47
52
|
Read required environment variables and make requests inside handlers or workflow
|
|
48
53
|
steps, not at module scope.
|
|
49
54
|
|
|
55
|
+
## Declaring Notion-as-Code databases
|
|
56
|
+
|
|
57
|
+
Database declarations use explicit resource IDs and keyed schemas. For a single
|
|
58
|
+
data source, supply the database ID as the first argument and the distinct data
|
|
59
|
+
source ID in `dataSourceResourceId`:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { notion } from "@notionhq/apps/notion-as-code";
|
|
63
|
+
|
|
64
|
+
const tasks = notion.database("tasks-db", {
|
|
65
|
+
dataSourceResourceId: "tasks-source",
|
|
66
|
+
name: "Tasks",
|
|
67
|
+
schema: {
|
|
68
|
+
Name: { type: "title", resourceId: "tasks-name" },
|
|
69
|
+
Effort: { type: "text", resourceId: "tasks-effort" },
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Here `name` is the single-source shorthand: it supplies both the database and
|
|
75
|
+
data-source display names. The source handle is `tasks.dataSource`; pass it to
|
|
76
|
+
`sync({ dataSource, ... })` from `@notionhq/apps`.
|
|
77
|
+
|
|
78
|
+
For multiple sources, use `datasources` instead of the top-level `schema`:
|
|
79
|
+
|
|
80
|
+
````ts
|
|
81
|
+
const work = notion.database("work-db", {
|
|
82
|
+
name: "Work",
|
|
83
|
+
datasources: {
|
|
84
|
+
Tasks: {
|
|
85
|
+
resourceId: "work-tasks-source",
|
|
86
|
+
schema: {
|
|
87
|
+
Name: { type: "title", resourceId: "work-tasks-name" },
|
|
88
|
+
Effort: { type: "text", resourceId: "work-tasks-effort" },
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
Projects: {
|
|
92
|
+
resourceId: "work-projects-source",
|
|
93
|
+
schema: {
|
|
94
|
+
Name: { type: "title", resourceId: "work-projects-name" },
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
The keys in `datasources` are the source names, so the handles are indexed by
|
|
101
|
+
those names: `work.datasources.Tasks` and `work.datasources.Projects`. Nested
|
|
102
|
+
data-source configs do not accept `name`. The top-level database `name` remains
|
|
103
|
+
supported and names the database; in a single-source declaration it also
|
|
104
|
+
supplies the source display name. Property configs do not accept `name`; each
|
|
105
|
+
schema key is always the property's name. Typed property access uses
|
|
106
|
+
`work.datasources.Tasks.schema.Effort`; sync primary keys and row properties use
|
|
107
|
+
the string/object key `"Effort"`.
|
|
108
|
+
|
|
109
|
+
Every database, source, and property ID remains explicit and must be unique
|
|
110
|
+
across the provisioning declarations. No IDs are generated from keys or names.
|
|
111
|
+
Preserve explicit IDs when changing authoring keys or display names.
|
|
112
|
+
|
|
113
|
+
Page and teamspace handles accept the same forms through
|
|
114
|
+
`page.addDatabase("database-id", args)` and `teamspace.addDatabase("database-id", args)`.
|
|
115
|
+
Top-level declarations also accept
|
|
116
|
+
`parent: { type: "resourceId", resourceId: "parent-id" }`; omitting it retains the
|
|
117
|
+
private Apps workspace parent. A database must declare at least one data source
|
|
118
|
+
or at least one view. A linked-only declaration may omit `datasources`, or use
|
|
119
|
+
`datasources: {}`, only with a nonempty `views` list. The empty forms `{}`,
|
|
120
|
+
`{ datasources: {} }`, and `{ datasources: {}, views: [] }` are rejected; widened
|
|
121
|
+
or dynamic maps and arrays are checked at runtime. The single-source `schema` and
|
|
122
|
+
multi-source `datasources` forms cannot be combined.
|
|
123
|
+
|
|
124
|
+
Here is a linked-only database whose typed table view references an explicit
|
|
125
|
+
data source resource ID:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
const linked = notion.database("work-linked-db", {
|
|
129
|
+
views: [
|
|
130
|
+
{
|
|
131
|
+
resourceId: "work-issues-table",
|
|
132
|
+
type: "table",
|
|
133
|
+
dataSourceResourceId: "issues-source",
|
|
134
|
+
properties: [{ property: "issue-title", visible: true }],
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
});
|
|
138
|
+
````
|
|
139
|
+
|
|
140
|
+
The build converts these declarations to the existing serialized database
|
|
141
|
+
intents: `dataSources` and `properties` remain arrays with exact resource IDs
|
|
142
|
+
and names from the authoring keys. Authoring-only `schema`, `datasources`, and
|
|
143
|
+
database-level `dataSourceResourceId` fields do not leak into the serialized
|
|
144
|
+
database intent. The provisioning JSON envelope and server API are unchanged.
|
|
145
|
+
|
|
50
146
|
## Provisioning artifact and deployment
|
|
51
147
|
|
|
52
148
|
The optional provisioning artifact uses `$schema: "notion:apps-provisioning:v1"`,
|
package/package.json
CHANGED
|
@@ -17,12 +17,12 @@ Import the resource creators you use, such as
|
|
|
17
17
|
resources for deployment; they do not make Notion API requests when called.
|
|
18
18
|
Use `context.notion` for runtime API operations instead.
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
credentials or network access.
|
|
20
|
+
Declare resources at module scope, not inside handlers or durable steps. Keep a
|
|
21
|
+
small declaration inline when it belongs to one capability. For a larger set
|
|
22
|
+
owned by one capability, use a local module such as
|
|
23
|
+
`src/workflows/myWorkflow/lib/notion.ts`. Use `src/notion.ts` when declarations
|
|
24
|
+
are shared across capabilities or do not have a natural capability owner. Module
|
|
25
|
+
evaluation must work without credentials or network access.
|
|
26
26
|
|
|
27
27
|
## Where declarations are picked up
|
|
28
28
|
|
|
@@ -33,24 +33,27 @@ same rule. Their resource type does not determine their file location.
|
|
|
33
33
|
|
|
34
34
|
```text
|
|
35
35
|
src/
|
|
36
|
-
|
|
37
|
-
resources.ts Shared page/database declarations
|
|
36
|
+
notion.ts App-wide resource declarations
|
|
38
37
|
syncs/
|
|
39
|
-
issues.ts Default-exported sync; imports ../
|
|
40
|
-
lib/
|
|
41
|
-
issueSchema.ts Sync-specific helper; must be imported
|
|
38
|
+
issues.ts Default-exported sync; imports ../notion.js
|
|
42
39
|
workflows/
|
|
43
|
-
notify.ts
|
|
40
|
+
notify.ts May declare a small related resource inline
|
|
41
|
+
myWorkflow.ts Imports ./myWorkflow/lib/notion.js
|
|
42
|
+
myWorkflow/
|
|
43
|
+
lib/
|
|
44
|
+
notion.ts Resources owned by myWorkflow
|
|
44
45
|
```
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
in `src/syncs/issues.ts`
|
|
48
|
-
module can be loaded with a side-effect
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
Choose the location based on ownership, not resource type. Export an app-wide
|
|
48
|
+
database handle from `src/notion.ts` and import it in `src/syncs/issues.ts` when
|
|
49
|
+
the sync uses it. A declaration-only module can be loaded with a side-effect
|
|
50
|
+
import. Keep these imports at module scope so build evaluation runs the
|
|
51
|
+
declarations.
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
Resource helper modules, including `src/notion.ts`, are not automatically scanned.
|
|
54
|
+
There is no automatically scanned `src/pages/` or `src/databases/` directory
|
|
55
|
+
either. Helpers under capability-specific `lib/` directories are not discovered
|
|
56
|
+
on their own.
|
|
54
57
|
Every direct file in a capability directory must still default-export that
|
|
55
58
|
capability, so do not put a resource-only file there. Imports reached only
|
|
56
59
|
through `src/customBlocks/` or browser code do not enter the Notion as Code recording
|
|
@@ -63,12 +66,14 @@ Read the installed Notion as Code types before choosing fields. The Apps
|
|
|
63
66
|
root exports support:
|
|
64
67
|
|
|
65
68
|
- `teamspace({ resourceId, name, accessLevel })`, with `addPage` and
|
|
66
|
-
`addDatabase` on the returned handle.
|
|
69
|
+
`teamspace.addDatabase(id, args)` on the returned handle.
|
|
67
70
|
- `page({ resourceId, parent?, properties?, content? })`, with
|
|
68
|
-
`addPage` and `addDatabase` for children.
|
|
69
|
-
- `database({
|
|
70
|
-
|
|
71
|
-
`
|
|
71
|
+
`addPage` and `page.addDatabase(id, args)` for children.
|
|
72
|
+
- `database(resourceId, { dataSourceResourceId, name, schema, parent?, views? })`,
|
|
73
|
+
returning a single source as `handle.dataSource`, or
|
|
74
|
+
`database(resourceId, { datasources: { Key: { resourceId, schema } }, views?, parent? })`,
|
|
75
|
+
returning sources as `handle.datasources.Key`. A source's `addPage` declares a row;
|
|
76
|
+
the database handle also exposes `addView`.
|
|
72
77
|
- `customAgent({ resourceId, name, instructions?, sharedResources? })`.
|
|
73
78
|
Shared resources are declared resource IDs; inspect the installed types
|
|
74
79
|
before specifying models or triggers.
|
|
@@ -77,56 +82,71 @@ root exports support:
|
|
|
77
82
|
Use property resource IDs in view filters, sorts, and layout options; calendar
|
|
78
83
|
views require `calendarBy`, and timeline views require `timelineBy`.
|
|
79
84
|
|
|
85
|
+
A database declaration must include at least one data source or at least one
|
|
86
|
+
view. A linked-only database may omit `datasources`, or use `datasources: {}`,
|
|
87
|
+
only when `views` is nonempty. `{}`, `{ datasources: {} }`, and
|
|
88
|
+
`{ datasources: {}, views: [] }` are invalid; widened or dynamic maps and arrays
|
|
89
|
+
are checked at runtime as well as by the type-level forms.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
const linkedIssues = database("project-issues-linked", {
|
|
93
|
+
views: [
|
|
94
|
+
{
|
|
95
|
+
resourceId: "project-issues-table",
|
|
96
|
+
type: "table",
|
|
97
|
+
dataSourceResourceId: "issues-source",
|
|
98
|
+
properties: [{ property: "issue-title", visible: true }],
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The table view above is typed by its `type: "table"` schema and links to the
|
|
105
|
+
explicit `issues-source` resource ID.
|
|
106
|
+
|
|
80
107
|
Keep resource IDs stable across builds. They identify declarations, not live
|
|
81
108
|
Notion UUIDs. Avoid the reserved `__notion_apps_` prefix. Explicit parents
|
|
82
109
|
use `{ type: "resourceId", resourceId: parent.resourceId }`; child helpers set
|
|
83
110
|
this reference for you. Pages and databases without a parent default to private
|
|
84
111
|
top-level resources in the Apps workspace.
|
|
85
112
|
|
|
86
|
-
Several fields, including database covers
|
|
87
|
-
|
|
113
|
+
Several fields, including database covers and custom-agent triggers,
|
|
114
|
+
still have incomplete types. A field typed as `unknown` is not proof
|
|
88
115
|
that any payload is supported. Check implementation and examples before using
|
|
89
116
|
it; do not assume parity with other Notion as Code packages. Database views are
|
|
90
117
|
typed with `ViewSchema`; standalone declarations accept `ViewArgs`, which adds
|
|
91
118
|
the owning `databaseResourceId`.
|
|
92
119
|
|
|
93
120
|
The Apps SDK exposes a subset of Notion as Code. Data sources are nested inside
|
|
94
|
-
`database({
|
|
121
|
+
`database(resourceId, { datasources: { ... } })`, not declared by a separate
|
|
95
122
|
`dataSource` function. It has no `space` workspace declaration:
|
|
96
123
|
Apps deployment rejects workspace creation or changes and supplies the App's
|
|
97
124
|
workspace binding itself. Value helpers and types stay on their existing
|
|
98
125
|
subpaths. For example, import `{ notion }` from `@notionhq/apps/notion-as-code`
|
|
99
126
|
for `notion.text(...)` or `notion.file(resourceId)`. The latter creates a file
|
|
100
127
|
reference, not an upload or file declaration. These helpers are not root exports.
|
|
101
|
-
CLI acceptance of
|
|
102
|
-
|
|
128
|
+
CLI acceptance of a serialized intent envelope alone does not establish server
|
|
129
|
+
support for its contents.
|
|
103
130
|
|
|
104
131
|
## Use a declared data source in a sync
|
|
105
132
|
|
|
106
|
-
This example
|
|
107
|
-
declaration into an imported helper when sharing it with other capabilities.
|
|
133
|
+
This sync-specific example declares the resource inline in `src/syncs/issues.ts`:
|
|
108
134
|
|
|
109
135
|
```ts
|
|
110
136
|
import { database, sync } from "@notionhq/apps";
|
|
111
137
|
import { Builder } from "@notionhq/apps/builder";
|
|
112
138
|
|
|
113
|
-
const issues = database({
|
|
114
|
-
|
|
139
|
+
const issues = database("issues-db", {
|
|
140
|
+
dataSourceResourceId: "issues-source",
|
|
115
141
|
name: "Issues",
|
|
116
|
-
|
|
117
|
-
{
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
properties: [
|
|
121
|
-
{ resourceId: "issue-name", name: "Name", type: "title" },
|
|
122
|
-
{ resourceId: "issue-id", name: "External ID", type: "text" },
|
|
123
|
-
],
|
|
124
|
-
},
|
|
125
|
-
],
|
|
142
|
+
schema: {
|
|
143
|
+
Name: { resourceId: "issue-name", type: "title" },
|
|
144
|
+
"External ID": { resourceId: "issue-id", type: "text" },
|
|
145
|
+
},
|
|
126
146
|
});
|
|
127
147
|
|
|
128
148
|
export default sync({
|
|
129
|
-
dataSource: issues.
|
|
149
|
+
dataSource: issues.dataSource,
|
|
130
150
|
primaryKey: "External ID",
|
|
131
151
|
mode: "incremental",
|
|
132
152
|
handler: async () => ({
|
|
@@ -144,11 +164,23 @@ export default sync({
|
|
|
144
164
|
|
|
145
165
|
Pass a data source handle, not the whole database handle or a UUID. The SDK
|
|
146
166
|
derives the sync schema from that handle. `primaryKey` names a title or text
|
|
147
|
-
property by its
|
|
148
|
-
upsert values; the SDK fills it from `key`.
|
|
149
|
-
Follow the [sync skill](../sync/SKILL.md)
|
|
150
|
-
|
|
151
|
-
|
|
167
|
+
property by its schema key, which is also its serialized property name, not its
|
|
168
|
+
resource ID. Omit that property from upsert values; the SDK fills it from `key`.
|
|
169
|
+
Use Apps `Builder` for sync values. Follow the [sync skill](../sync/SKILL.md)
|
|
170
|
+
for pagination and reconciliation.
|
|
171
|
+
|
|
172
|
+
Author properties in a keyed `schema` with explicit `resourceId` and `type`.
|
|
173
|
+
Both `datasources` keys and `schema` keys are names: each data-source key is
|
|
174
|
+
that source's name, and each schema key is the property's name used for typed
|
|
175
|
+
access, sync values, and serialized property arrays. Nested data-source configs
|
|
176
|
+
and property configs do not accept `name`. The top-level database `name` remains
|
|
177
|
+
supported; in the single-source shorthand it supplies both the database and
|
|
178
|
+
data-source display names. In a multi-source declaration, it names the database
|
|
179
|
+
while each source name comes from its key. The serialized intent retains the
|
|
180
|
+
server's existing arrays. Authoring-only `schema`, `datasources`, and
|
|
181
|
+
database-level `dataSourceResourceId` fields do not appear in the serialized
|
|
182
|
+
database intent. The provisioning JSON envelope is unchanged.
|
|
183
|
+
No IDs are generated.
|
|
152
184
|
Each data source needs exactly one title property
|
|
153
185
|
and unique property names and IDs. The current adapter supports title, text,
|
|
154
186
|
number, select, multi-select, status, date, checkbox, URL, email, phone, and file
|
package/skills/sync/SKILL.md
CHANGED
|
@@ -25,26 +25,21 @@ including declaration discovery and the supported schema types.
|
|
|
25
25
|
This example declares an Issues database and syncs into its data source:
|
|
26
26
|
|
|
27
27
|
```ts
|
|
28
|
+
// src/syncs/issues.ts
|
|
28
29
|
import { database, sync } from "@notionhq/apps";
|
|
29
30
|
import { Builder } from "@notionhq/apps/builder";
|
|
30
31
|
|
|
31
|
-
const issues = database({
|
|
32
|
-
|
|
32
|
+
const issues = database("issues-db", {
|
|
33
|
+
dataSourceResourceId: "issues-source",
|
|
33
34
|
name: "Issues",
|
|
34
|
-
|
|
35
|
-
{
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
properties: [
|
|
39
|
-
{ resourceId: "issue-name", name: "Name", type: "title" },
|
|
40
|
-
{ resourceId: "issue-id", name: "External ID", type: "text" },
|
|
41
|
-
],
|
|
42
|
-
},
|
|
43
|
-
],
|
|
35
|
+
schema: {
|
|
36
|
+
Name: { resourceId: "issue-name", type: "title" },
|
|
37
|
+
"External ID": { resourceId: "issue-id", type: "text" },
|
|
38
|
+
},
|
|
44
39
|
});
|
|
45
40
|
|
|
46
41
|
export default sync({
|
|
47
|
-
dataSource: issues.
|
|
42
|
+
dataSource: issues.dataSource,
|
|
48
43
|
primaryKey: "External ID",
|
|
49
44
|
mode: "incremental",
|
|
50
45
|
handler: async () => ({
|
|
@@ -60,11 +55,19 @@ export default sync({
|
|
|
60
55
|
});
|
|
61
56
|
```
|
|
62
57
|
|
|
63
|
-
Set `primaryKey` on the sync; it names a title or text property by its
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
`
|
|
58
|
+
Set `primaryKey` on the sync; it names a title or text property by its schema
|
|
59
|
+
key, which is also the serialized property name. Every upsert must include at least one
|
|
60
|
+
known non-primary-key schema property, while any other non-primary-key properties may be
|
|
61
|
+
omitted; unknown property keys are rejected by TypeScript. Omit the primary-key property
|
|
62
|
+
from upsert properties: the SDK supplies it from `change.key`. Use Apps `Builder` values for
|
|
63
|
+
sync results. Keep resource IDs stable and reuse the declared data source handle. A
|
|
64
|
+
sync-specific database may be declared inline or in a local helper. Shared or
|
|
65
|
+
otherwise app-wide declarations often fit in `src/notion.ts`.
|
|
66
|
+
Both `datasources` keys and `schema` keys are names: each data-source key is
|
|
67
|
+
the source's name, and each schema key is the property's name. Nested
|
|
68
|
+
data-source configs do not accept `name`. The top-level database `name` remains
|
|
69
|
+
supported; in the single-source shorthand it supplies both the database and
|
|
70
|
+
data-source display names.
|
|
68
71
|
|
|
69
72
|
This skill covers only syncs backed by Notion as Code data sources. If the user
|
|
70
73
|
asks to attach an existing database, explain that this recipe does not cover
|
package/skills/workflow/SKILL.md
CHANGED
|
@@ -38,6 +38,53 @@ and Notion API calls, mutable state reads, timestamps, random values, generated
|
|
|
38
38
|
IDs, messages, creates, and updates. Keep deterministic transforms of the event
|
|
39
39
|
and completed step results outside a step.
|
|
40
40
|
|
|
41
|
+
Workflow-level key-value state is available through the step closure's `state`.
|
|
42
|
+
State is shared across runs of the same workflow and every operation executes
|
|
43
|
+
inside that step's durability boundary:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
await context.step("Update cursor", async ({ state }) => {
|
|
47
|
+
state.set("cursor", nextCursor);
|
|
48
|
+
const pendingCursor = await state.get("cursor"); // Reads the buffered write.
|
|
49
|
+
if (!(await saveCursor(pendingCursor))) {
|
|
50
|
+
throw new Error("Save failed"); // The state write is discarded.
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Use `get` and `set`; passing `undefined` to `set` deletes the key, while `null`
|
|
56
|
+
is stored as a value. Mutations are buffered and committed atomically only
|
|
57
|
+
after the step callback succeeds. Repeated mutations of one key are allowed,
|
|
58
|
+
reads observe the latest buffered value, and only the final mutation is
|
|
59
|
+
committed. If the callback throws, none of that step's state mutations are
|
|
60
|
+
persisted.
|
|
61
|
+
|
|
62
|
+
Use `context.wait.until(name, { after })` for a relative wait shorter than seven
|
|
63
|
+
days, or `context.wait.until(name, { at })` for an absolute date within the next
|
|
64
|
+
seven days. The runtime checkpoints the target time, releases the workflow's
|
|
65
|
+
compute, and resumes it from the same point later:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
await context.wait.until("Cooling-off period", {
|
|
69
|
+
after: { days: 1, hours: 6 },
|
|
70
|
+
});
|
|
71
|
+
await context.wait.until("Launch time", { at: launchAt });
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Relative durations can combine `milliseconds`, `seconds`, `minutes`, `hours`,
|
|
75
|
+
`days`, and `weeks`. Every specified value must be a positive finite integer.
|
|
76
|
+
Repeated waits can keep one stable display name when each call has a unique
|
|
77
|
+
composite `key`:
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
for (const page of pages) {
|
|
81
|
+
await context.wait.until("Rate limit delay", {
|
|
82
|
+
key: ["rate-limit", page.id],
|
|
83
|
+
after: { seconds: 1 },
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
41
88
|
Completed steps replay saved results. Do not rely on in-memory mutations inside
|
|
42
89
|
a step. Return the values required by later code. Keep calls in a stable order
|
|
43
90
|
and give every step a stable display name. The name is the replay key by
|
|
@@ -67,8 +114,9 @@ Use [Notion as Code](../notion-as-code/SKILL.md) for pages and databases that
|
|
|
67
114
|
should be created during deployment. Prefer it over equivalent manual setup
|
|
68
115
|
or API calls that create the App's resources. Use runtime API calls for dynamic
|
|
69
116
|
data changes, not as a substitute for supported Notion as Code setup.
|
|
70
|
-
Keep declarations at module scope
|
|
71
|
-
|
|
117
|
+
Keep declarations at module scope. A small workflow-specific resource can be
|
|
118
|
+
declared inline. A larger set can live in a local module; for example, declare a
|
|
119
|
+
guide page in `src/workflows/sayHello/lib/notion.ts`:
|
|
72
120
|
|
|
73
121
|
```ts
|
|
74
122
|
import { page } from "@notionhq/apps";
|
|
@@ -82,10 +130,11 @@ export const guide = page({
|
|
|
82
130
|
Import the module from `src/workflows/sayHello.ts` so the build records it:
|
|
83
131
|
|
|
84
132
|
```ts
|
|
85
|
-
import "../lib/resources";
|
|
86
133
|
import { workflow } from "@notionhq/apps";
|
|
87
134
|
import { triggers } from "@notionhq/apps/triggers";
|
|
88
135
|
|
|
136
|
+
import "./sayHello/lib/notion.js";
|
|
137
|
+
|
|
89
138
|
export default workflow({
|
|
90
139
|
name: "Say Hello",
|
|
91
140
|
description: "Says hello on a recurring schedule.",
|