@project-ajax/create 0.0.19 → 0.0.20
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/package.json
CHANGED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Client } from "@notionhq/client";
|
|
2
|
+
import { automation } from "@project-ajax/sdk";
|
|
3
|
+
|
|
4
|
+
// Initialize the Notion client with OAuth token from environment
|
|
5
|
+
const notion = new Client({
|
|
6
|
+
auth: process.env.NOTION_API_TOKEN,
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
type RichTextProperty = {
|
|
10
|
+
type: "rich_text";
|
|
11
|
+
rich_text: Array<{ plain_text: string }>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Example automation that processes questions from database pages.
|
|
16
|
+
*
|
|
17
|
+
* This automation:
|
|
18
|
+
* 1. Reads a question from a page property
|
|
19
|
+
* 2. Processes it (calls an API, performs logic, etc.)
|
|
20
|
+
* 3. Updates the page with the answer
|
|
21
|
+
*/
|
|
22
|
+
export const questionAnswerAutomation = automation({
|
|
23
|
+
title: "Question Answer Automation",
|
|
24
|
+
description:
|
|
25
|
+
"Reads questions from database pages and updates them with answers",
|
|
26
|
+
execute: async ({ pageId, pageData }) => {
|
|
27
|
+
// Extract email from the page dat
|
|
28
|
+
const emailProperty = pageData?.properties?.Email as
|
|
29
|
+
| RichTextProperty
|
|
30
|
+
| undefined;
|
|
31
|
+
|
|
32
|
+
// Extract text content from the property
|
|
33
|
+
let emailValue = "";
|
|
34
|
+
if (emailProperty?.type === "rich_text") {
|
|
35
|
+
emailValue = emailProperty.rich_text.map((rt) => rt.plain_text).join("");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Handle empty email
|
|
39
|
+
if (!emailValue) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
await sendEmail(emailValue);
|
|
44
|
+
|
|
45
|
+
// Update the page to indicate the email has been sent
|
|
46
|
+
await notion.pages.update({
|
|
47
|
+
page_id: pageId,
|
|
48
|
+
properties: {
|
|
49
|
+
EmailSent: {
|
|
50
|
+
// Notion has a 2000 character limit for rich_text
|
|
51
|
+
checkbox: true,
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
async function sendEmail(email: string): Promise<void> {
|
|
59
|
+
console.log(`Sending email to ${email}`);
|
|
60
|
+
}
|
package/template/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Use this repo to write workers to enhance Notion. With Notion Workers, you can
|
|
4
4
|
write JavaScript that syncs data into Notion collections, provides tools to
|
|
5
|
-
Custom Agents,
|
|
5
|
+
Custom Agents, and powers database automations.
|
|
6
6
|
|
|
7
7
|
## Prerequisites
|
|
8
8
|
|
|
@@ -127,6 +127,13 @@ your Notion workspace.
|
|
|
127
127
|
|
|
128
128
|
See the [tool example](./.examples/tool-example.ts) for a complete example.
|
|
129
129
|
|
|
130
|
+
### Writing an automation
|
|
131
|
+
|
|
132
|
+
[Automations](#automations) provide custom actions that can be triggered from
|
|
133
|
+
database automations in your Notion workspace.
|
|
134
|
+
|
|
135
|
+
See the [automation example](./.examples/automation-example.ts) for a complete example.
|
|
136
|
+
|
|
130
137
|
## Secrets
|
|
131
138
|
|
|
132
139
|
Your function might require sensitive values, like API tokens, to run. You can
|
|
@@ -239,6 +246,39 @@ npx workers exec myTool
|
|
|
239
246
|
# To test tools with specific inputs, use them with a custom agent in Notion.
|
|
240
247
|
```
|
|
241
248
|
|
|
249
|
+
### Automations
|
|
250
|
+
|
|
251
|
+
The **automation** capability allows you to create custom actions that can be
|
|
252
|
+
triggered from database automations in your Notion workspace. When you create a
|
|
253
|
+
database automation and select "Run worker" as an action, you can choose from
|
|
254
|
+
your deployed automation capabilities.
|
|
255
|
+
|
|
256
|
+
#### Properties
|
|
257
|
+
|
|
258
|
+
- **`title`** (string, required): The name of the automation shown in the UI when selecting automations.
|
|
259
|
+
- **`description`** (string, required): A brief description of what this automation does.
|
|
260
|
+
- **`execute`** (function, required): Async function that runs when the automation is triggered.
|
|
261
|
+
- **Parameters**: `context` (AutomationContext) - Context about the trigger, including:
|
|
262
|
+
- `actionType` (string): The type of automation action
|
|
263
|
+
- `pageId` (string, optional): ID of the page that triggered the automation
|
|
264
|
+
- `pageData` (object, optional): Full page object from Notion's Public API, including all properties
|
|
265
|
+
- **Returns**: Promise that resolves when the automation completes.
|
|
266
|
+
|
|
267
|
+
#### Example
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
automation({
|
|
271
|
+
title: "Send Welcome Email",
|
|
272
|
+
description: "Sends a welcome email when a user is added",
|
|
273
|
+
execute: async ({ pageData }) => {
|
|
274
|
+
if (pageData) {
|
|
275
|
+
const email = pageData.properties.Email;
|
|
276
|
+
await sendEmail(email);
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
})
|
|
280
|
+
```
|
|
281
|
+
|
|
242
282
|
## CLI reference
|
|
243
283
|
|
|
244
284
|
The `workers` CLI provides commands to authenticate, deploy, execute, and manage
|
package/template/src/index.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { tool } from "@project-ajax/sdk";
|
|
1
|
+
import { automation, sync, tool } from "@project-ajax/sdk";
|
|
2
2
|
import * as Builder from "@project-ajax/sdk/builder";
|
|
3
3
|
import * as Schema from "@project-ajax/sdk/schema";
|
|
4
|
-
import { sync } from "@project-ajax/sdk/sync";
|
|
5
4
|
|
|
6
5
|
// Sample data for demonstration
|
|
7
6
|
const sampleTasks = [
|
|
@@ -149,3 +148,38 @@ export const taskSearchTool = tool({
|
|
|
149
148
|
};
|
|
150
149
|
},
|
|
151
150
|
});
|
|
151
|
+
|
|
152
|
+
// Example automation that runs when triggered from a database automation
|
|
153
|
+
export const completeTaskAutomation = automation({
|
|
154
|
+
title: "Mark Task Complete",
|
|
155
|
+
description: "Automatically marks a task as complete when triggered",
|
|
156
|
+
execute: async ({ pageId, actionType, pageData }) => {
|
|
157
|
+
// The pageData parameter contains the full page object from Notion's Public API
|
|
158
|
+
// with all the database properties already encoded and ready to use.
|
|
159
|
+
|
|
160
|
+
console.log(`Automation triggered for page: ${pageId}`);
|
|
161
|
+
console.log(`Action type: ${actionType}`);
|
|
162
|
+
|
|
163
|
+
if (pageData) {
|
|
164
|
+
// Access all page properties directly
|
|
165
|
+
console.log("Page properties:", pageData.properties);
|
|
166
|
+
|
|
167
|
+
// Example: Access specific properties by their name
|
|
168
|
+
// const taskName = pageData.properties.Name;
|
|
169
|
+
// const status = pageData.properties.Status;
|
|
170
|
+
// const assignee = pageData.properties.Assignee;
|
|
171
|
+
|
|
172
|
+
// The properties are in Notion's Public API format
|
|
173
|
+
// See: https://developers.notion.com/reference/property-value-object
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// In a real implementation, you would:
|
|
177
|
+
// 1. Use the page properties to determine what action to take
|
|
178
|
+
// 2. Update the task status in your system
|
|
179
|
+
// 3. Call external APIs, send notifications, etc.
|
|
180
|
+
|
|
181
|
+
// Example: You could call an external API, update a database, send notifications, etc.
|
|
182
|
+
// For this demo, we just log the execution
|
|
183
|
+
console.log("Task marked as complete!");
|
|
184
|
+
},
|
|
185
|
+
});
|