@notionhq/workers 0.5.0 → 0.7.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.
- package/dist/alpha/ajv-formats.d.js +0 -0
- package/dist/alpha/builder.d.ts +149 -0
- package/dist/alpha/builder.d.ts.map +1 -0
- package/dist/alpha/builder.js +260 -0
- package/dist/alpha/builder.test.d.ts +2 -0
- package/dist/alpha/builder.test.d.ts.map +1 -0
- package/dist/alpha/cli/build.d.ts +13 -0
- package/dist/alpha/cli/build.d.ts.map +1 -0
- package/dist/alpha/cli/build.js +36 -0
- package/dist/alpha/cli/build.test.d.ts +2 -0
- package/dist/alpha/cli/build.test.d.ts.map +1 -0
- package/dist/alpha/cli/codegen.d.ts +10 -0
- package/dist/alpha/cli/codegen.d.ts.map +1 -0
- package/dist/alpha/cli/codegen.js +41 -0
- package/dist/alpha/cli/codegen.test.d.ts +2 -0
- package/dist/alpha/cli/codegen.test.d.ts.map +1 -0
- package/dist/alpha/cli/discover.d.ts +35 -0
- package/dist/alpha/cli/discover.d.ts.map +1 -0
- package/dist/alpha/cli/discover.js +39 -0
- package/dist/alpha/cli/discover.test.d.ts +2 -0
- package/dist/alpha/cli/discover.test.d.ts.map +1 -0
- package/dist/alpha/cli/emit-manifest.d.ts +33 -0
- package/dist/alpha/cli/emit-manifest.d.ts.map +1 -0
- package/dist/alpha/cli/emit-manifest.js +266 -0
- package/dist/alpha/cli/emit-manifest.test.d.ts +2 -0
- package/dist/alpha/cli/emit-manifest.test.d.ts.map +1 -0
- package/dist/alpha/cli/index.d.ts +3 -0
- package/dist/alpha/cli/index.d.ts.map +1 -0
- package/dist/alpha/cli/index.js +36 -0
- package/dist/alpha/context.d.ts +8 -0
- package/dist/alpha/context.d.ts.map +1 -0
- package/dist/alpha/context.js +33 -0
- package/dist/alpha/database.d.ts +90 -0
- package/dist/alpha/database.d.ts.map +1 -0
- package/dist/alpha/database.js +9 -0
- package/dist/alpha/error.d.ts +41 -0
- package/dist/alpha/error.d.ts.map +1 -0
- package/dist/alpha/error.js +25 -0
- package/dist/alpha/icon-names.d.ts +6 -0
- package/dist/alpha/icon-names.d.ts.map +1 -0
- package/dist/alpha/icon-names.js +0 -0
- package/dist/alpha/json-schema.d.ts +117 -0
- package/dist/alpha/json-schema.d.ts.map +1 -0
- package/dist/alpha/json-schema.js +0 -0
- package/dist/alpha/manifest.d.ts +44 -0
- package/dist/alpha/manifest.d.ts.map +1 -0
- package/dist/alpha/manifest.js +0 -0
- package/dist/alpha/output.d.ts +7 -0
- package/dist/alpha/output.d.ts.map +1 -0
- package/dist/alpha/output.js +8 -0
- package/dist/alpha/pacer.d.ts +74 -0
- package/dist/alpha/pacer.d.ts.map +1 -0
- package/dist/alpha/pacer.js +66 -0
- package/dist/alpha/pacer.test.d.ts +2 -0
- package/dist/alpha/pacer.test.d.ts.map +1 -0
- package/dist/alpha/schedule.d.ts +6 -0
- package/dist/alpha/schedule.d.ts.map +1 -0
- package/dist/alpha/schedule.js +43 -0
- package/dist/alpha/schedule.test.d.ts +2 -0
- package/dist/alpha/schedule.test.d.ts.map +1 -0
- package/dist/alpha/schema-builder.d.ts +79 -0
- package/dist/alpha/schema-builder.d.ts.map +1 -0
- package/dist/alpha/schema-builder.js +135 -0
- package/dist/alpha/schema.d.ts +185 -0
- package/dist/alpha/schema.d.ts.map +1 -0
- package/dist/alpha/schema.js +115 -0
- package/dist/alpha/sync.d.ts +233 -0
- package/dist/alpha/sync.d.ts.map +1 -0
- package/dist/alpha/sync.js +70 -0
- package/dist/alpha/sync.test.d.ts +2 -0
- package/dist/alpha/sync.test.d.ts.map +1 -0
- package/dist/alpha/tool.d.ts +132 -0
- package/dist/alpha/tool.d.ts.map +1 -0
- package/dist/alpha/tool.js +156 -0
- package/dist/alpha/tool.test.d.ts +2 -0
- package/dist/alpha/tool.test.d.ts.map +1 -0
- package/dist/alpha/triggers.d.ts +365 -0
- package/dist/alpha/triggers.d.ts.map +1 -0
- package/dist/alpha/triggers.generated.d.ts +246 -0
- package/dist/alpha/triggers.generated.d.ts.map +1 -0
- package/dist/alpha/triggers.generated.js +239 -0
- package/dist/alpha/triggers.js +0 -0
- package/dist/alpha/types.d.ts +228 -0
- package/dist/alpha/types.d.ts.map +1 -0
- package/dist/alpha/types.js +0 -0
- package/dist/alpha/workflow.d.ts +70 -0
- package/dist/alpha/workflow.d.ts.map +1 -0
- package/dist/alpha/workflow.js +39 -0
- package/dist/alpha/workflow.test.d.ts +2 -0
- package/dist/alpha/workflow.test.d.ts.map +1 -0
- package/dist/block.d.ts.map +1 -1
- package/dist/builder.d.ts.map +1 -1
- package/dist/builder.js +2 -6
- package/dist/capabilities/ai_connector.d.ts.map +1 -1
- package/dist/capabilities/automation.d.ts.map +1 -1
- package/dist/capabilities/context.d.ts.map +1 -1
- package/dist/capabilities/oauth.d.ts.map +1 -1
- package/dist/capabilities/output.d.ts.map +1 -1
- package/dist/capabilities/output.js +2 -4
- package/dist/capabilities/stateful-capability.d.ts.map +1 -1
- package/dist/capabilities/stateful-capability.js +2 -6
- package/dist/capabilities/sync.d.ts.map +1 -1
- package/dist/capabilities/sync.js +2 -8
- package/dist/capabilities/tool.d.ts.map +1 -1
- package/dist/capabilities/tool.js +6 -20
- package/dist/capabilities/webhook.d.ts.map +1 -1
- package/dist/capabilities/workflow.d.ts +2 -2
- package/dist/capabilities/workflow.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -7
- package/dist/json-schema.d.ts.map +1 -1
- package/dist/schema-builder.d.ts.map +1 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +1 -4
- package/package.json +74 -14
- package/src/alpha/ajv-formats.d.ts +7 -0
- package/src/alpha/builder.test.ts +197 -0
- package/src/alpha/builder.ts +338 -0
- package/src/alpha/cli/build.test.ts +240 -0
- package/src/alpha/cli/build.ts +57 -0
- package/src/alpha/cli/codegen.test.ts +31 -0
- package/src/alpha/cli/codegen.ts +51 -0
- package/src/alpha/cli/discover.test.ts +43 -0
- package/src/alpha/cli/discover.ts +81 -0
- package/src/alpha/cli/emit-manifest.test.ts +45 -0
- package/src/alpha/cli/emit-manifest.ts +409 -0
- package/src/alpha/cli/index.ts +41 -0
- package/src/alpha/context.ts +61 -0
- package/src/alpha/database.ts +102 -0
- package/src/alpha/error.ts +49 -0
- package/src/alpha/icon-names.ts +890 -0
- package/src/alpha/json-schema.ts +180 -0
- package/src/alpha/manifest.ts +47 -0
- package/src/alpha/output.ts +8 -0
- package/src/alpha/pacer.test.ts +81 -0
- package/src/alpha/pacer.ts +181 -0
- package/src/alpha/schedule.test.ts +31 -0
- package/src/alpha/schedule.ts +53 -0
- package/src/alpha/schema-builder.ts +193 -0
- package/src/alpha/schema.ts +222 -0
- package/src/alpha/sync.test.ts +191 -0
- package/src/alpha/sync.ts +340 -0
- package/src/alpha/tool.test.ts +116 -0
- package/src/alpha/tool.ts +319 -0
- package/src/alpha/triggers.generated.ts +489 -0
- package/src/alpha/triggers.ts +500 -0
- package/src/alpha/types.ts +284 -0
- package/src/alpha/workflow.test.ts +84 -0
- package/src/alpha/workflow.ts +131 -0
- package/src/block.ts +1 -5
- package/src/builder.test.ts +28 -60
- package/src/builder.ts +3 -10
- package/src/capabilities/ai_connector.test.ts +9 -32
- package/src/capabilities/ai_connector.ts +5 -17
- package/src/capabilities/automation.test.ts +3 -12
- package/src/capabilities/automation.ts +3 -11
- package/src/capabilities/context.ts +3 -6
- package/src/capabilities/oauth.test.ts +1 -0
- package/src/capabilities/oauth.ts +1 -3
- package/src/capabilities/output.ts +1 -3
- package/src/capabilities/stateful-capability.test.ts +1 -0
- package/src/capabilities/stateful-capability.ts +2 -6
- package/src/capabilities/sync.test.ts +15 -45
- package/src/capabilities/sync.ts +10 -40
- package/src/capabilities/tool.test.ts +106 -146
- package/src/capabilities/tool.ts +17 -39
- package/src/capabilities/webhook.ts +3 -10
- package/src/capabilities/workflow.test.ts +10 -31
- package/src/capabilities/workflow.ts +5 -12
- package/src/index.ts +2 -12
- package/src/json-schema.test.ts +2 -4
- package/src/json-schema.ts +7 -6
- package/src/pacer.test.ts +1 -3
- package/src/pacer_internal.ts +1 -5
- package/src/schema-builder.test.ts +10 -35
- package/src/schema-builder.ts +5 -16
- package/src/schema.ts +2 -10
- package/src/types.ts +1 -7
- package/src/worker.ts +8 -34
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Client } from "@notionhq/client";
|
|
2
|
+
import type { ClientOptions } from "@notionhq/client/build/src/Client.js";
|
|
3
|
+
|
|
4
|
+
import type { CapabilityType } from "./manifest.js";
|
|
5
|
+
|
|
6
|
+
export type CapabilityContext = {
|
|
7
|
+
/** Notion API SDK client for this execution. */
|
|
8
|
+
notion: Client;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const FIX_STEPS =
|
|
12
|
+
"\n\nTo fix this:\n" +
|
|
13
|
+
"1. Create a personal access token at https://app.notion.com/developers/tokens\n" +
|
|
14
|
+
"2. Add NOTION_API_TOKEN=<your-token> to your .env file\n" +
|
|
15
|
+
"3. For deployed workers, run: ntn workers env push";
|
|
16
|
+
|
|
17
|
+
function missingTokenMessage(capabilityType: CapabilityType): string {
|
|
18
|
+
if (capabilityType === "tool") {
|
|
19
|
+
return (
|
|
20
|
+
"NOTION_API_TOKEN is not set. " +
|
|
21
|
+
"When tools are called through a Custom Agent, the platform sets NOTION_API_TOKEN automatically, " +
|
|
22
|
+
"using the permissions of the Custom Agent. " +
|
|
23
|
+
"Outside of that context (e.g. ntn workers exec), you must set it yourself." +
|
|
24
|
+
FIX_STEPS
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
"NOTION_API_TOKEN is not set. " +
|
|
30
|
+
`context.notion requires an API token to make requests in ${capabilityType} capabilities to Notion.` +
|
|
31
|
+
FIX_STEPS
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function createUnauthenticatedNotionProxy(capabilityType: CapabilityType): Client {
|
|
36
|
+
return new Proxy({} as Client, {
|
|
37
|
+
get(_target, prop) {
|
|
38
|
+
if (prop === "then" || typeof prop === "symbol") {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
throw new Error(missingTokenMessage(capabilityType));
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function createCapabilityContext(capabilityType: CapabilityType): CapabilityContext {
|
|
47
|
+
const options: ClientOptions = {};
|
|
48
|
+
|
|
49
|
+
if (process.env.NOTION_API_BASE_URL) {
|
|
50
|
+
options.baseUrl = process.env.NOTION_API_BASE_URL;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!process.env.NOTION_API_TOKEN) {
|
|
54
|
+
return { notion: createUnauthenticatedNotionProxy(capabilityType) };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
options.auth = process.env.NOTION_API_TOKEN;
|
|
58
|
+
const notion = new Client(options);
|
|
59
|
+
|
|
60
|
+
return { notion };
|
|
61
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { PropertySchema, Schema } from "./schema.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for a database that Notion creates and manages on behalf
|
|
5
|
+
* of the worker. The database is created on first deploy and its schema
|
|
6
|
+
* is migrated on subsequent deploys.
|
|
7
|
+
*/
|
|
8
|
+
export type ManagedDatabaseConfig<S extends PropertySchema = PropertySchema> = {
|
|
9
|
+
type: "managed";
|
|
10
|
+
/**
|
|
11
|
+
* The title for the database when it is first created in Notion.
|
|
12
|
+
* Defaults to the database's name.
|
|
13
|
+
*
|
|
14
|
+
* Updating this after the database has been created will not change the
|
|
15
|
+
* title of the database — users may rename it freely.
|
|
16
|
+
*/
|
|
17
|
+
initialTitle?: string;
|
|
18
|
+
/**
|
|
19
|
+
* The property that serves as the primary key for matching records.
|
|
20
|
+
* Must be a property defined in the schema. The value of this property
|
|
21
|
+
* should match the `key` field on each sync change.
|
|
22
|
+
*/
|
|
23
|
+
primaryKey: Extract<keyof S, string>;
|
|
24
|
+
/**
|
|
25
|
+
* The database schema. Notion will migrate the database to match
|
|
26
|
+
* the given schema on worker deploy.
|
|
27
|
+
*/
|
|
28
|
+
schema: Schema<S>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Configuration union for all database types.
|
|
33
|
+
* Currently only `"managed"` is supported.
|
|
34
|
+
*/
|
|
35
|
+
export type DatabaseConfig<S extends PropertySchema = PropertySchema> = ManagedDatabaseConfig<S>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A database declaration as returned by {@link createDatabase}.
|
|
39
|
+
*/
|
|
40
|
+
export type Database<S extends PropertySchema = PropertySchema> = {
|
|
41
|
+
_tag: "database";
|
|
42
|
+
name: string;
|
|
43
|
+
config: DatabaseConfig<S>;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Declare a database.
|
|
48
|
+
*
|
|
49
|
+
* A database can be declared anywhere — inline in a sync's config, in the
|
|
50
|
+
* sync's file, or in a shared module that several syncs import. The build
|
|
51
|
+
* discovers databases by walking capability configs: any database attached
|
|
52
|
+
* to a sync (or referenced by a `Schema.relation(...)`) is included in the
|
|
53
|
+
* manifest and created on deploy.
|
|
54
|
+
*
|
|
55
|
+
* The `name` is the database's stable identity across deploys — it is how
|
|
56
|
+
* the platform matches the declaration to the actual Notion database it
|
|
57
|
+
* created previously. Renaming it orphans that database, so treat it like a
|
|
58
|
+
* primary key: pick it once and leave it alone. Two distinct
|
|
59
|
+
* `createDatabase(...)` calls sharing a name fail the build; share one
|
|
60
|
+
* instance via import instead.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* // src/lib/databases.ts — shared by several syncs
|
|
65
|
+
* import { createDatabase } from "@notionhq/workers/alpha/database";
|
|
66
|
+
* import { Schema } from "@notionhq/workers/alpha/schema";
|
|
67
|
+
*
|
|
68
|
+
* export const projects = createDatabase("projects", {
|
|
69
|
+
* type: "managed",
|
|
70
|
+
* primaryKey: "Project ID",
|
|
71
|
+
* schema: {
|
|
72
|
+
* properties: {
|
|
73
|
+
* Name: Schema.title(),
|
|
74
|
+
* "Project ID": Schema.richText(),
|
|
75
|
+
* },
|
|
76
|
+
* },
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* export const tasks = createDatabase("tasks", {
|
|
80
|
+
* type: "managed",
|
|
81
|
+
* initialTitle: "Tasks",
|
|
82
|
+
* primaryKey: "Task ID",
|
|
83
|
+
* schema: {
|
|
84
|
+
* properties: {
|
|
85
|
+
* Title: Schema.title(),
|
|
86
|
+
* "Task ID": Schema.richText(),
|
|
87
|
+
* Project: Schema.relation(projects),
|
|
88
|
+
* },
|
|
89
|
+
* },
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export function createDatabase<S extends PropertySchema>(
|
|
94
|
+
name: string,
|
|
95
|
+
config: DatabaseConfig<S>,
|
|
96
|
+
): Database<S> {
|
|
97
|
+
if (!name || typeof name !== "string") {
|
|
98
|
+
throw new Error("Database name must be a non-empty string");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return { _tag: "database", name, config };
|
|
102
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An error that occurred during the execution of a worker capability.
|
|
3
|
+
*/
|
|
4
|
+
export class ExecutionError extends Error {
|
|
5
|
+
override readonly cause: unknown;
|
|
6
|
+
|
|
7
|
+
constructor(cause: unknown) {
|
|
8
|
+
super(`Error during worker execution: ${cause}`);
|
|
9
|
+
this.name = "ExecutionError";
|
|
10
|
+
this.cause = cause;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Throw this error from your sync handler to signal that the external API
|
|
16
|
+
* returned a rate-limit response (e.g. HTTP 429).
|
|
17
|
+
*
|
|
18
|
+
* When the platform receives this error it applies exponential backoff
|
|
19
|
+
* instead of retrying immediately. If `retryAfter` is provided, the
|
|
20
|
+
* platform will wait at least that many seconds before the next attempt.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* handler: async () => {
|
|
25
|
+
* const res = await fetch("https://api.example.com/issues");
|
|
26
|
+
* if (res.status === 429) {
|
|
27
|
+
* const retryAfter = Number(res.headers.get("Retry-After"));
|
|
28
|
+
* throw new RateLimitError({
|
|
29
|
+
* retryAfter: Number.isFinite(retryAfter) ? retryAfter : undefined,
|
|
30
|
+
* });
|
|
31
|
+
* }
|
|
32
|
+
* // ...
|
|
33
|
+
* },
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export class RateLimitError extends ExecutionError {
|
|
37
|
+
/**
|
|
38
|
+
* Optional number of seconds the external API asked us to wait
|
|
39
|
+
* before retrying. The platform takes the max of this value and its
|
|
40
|
+
* own exponential-backoff delay.
|
|
41
|
+
*/
|
|
42
|
+
readonly retryAfter: number | undefined;
|
|
43
|
+
|
|
44
|
+
constructor(options?: { retryAfter?: number }) {
|
|
45
|
+
super("Rate limited by external API");
|
|
46
|
+
this.name = "RateLimitError";
|
|
47
|
+
this.retryAfter = options?.retryAfter;
|
|
48
|
+
}
|
|
49
|
+
}
|