openloops 0.4.5 → 0.5.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/CHANGELOG.md +25 -0
- package/dist/db/indexes/automations.js +1 -1
- package/dist/db/indexes/automations.mjs +1 -1
- package/dist/db/migrations/automations.d.ts +1 -0
- package/dist/db/migrations/automations.js +32 -5
- package/dist/db/migrations/automations.mjs +32 -5
- package/dist/services/automations/entities/automation.entity.d.ts +9 -9
- package/dist/services/automations/entities/automation.entity.js +21 -20
- package/dist/services/automations/entities/automation.entity.mjs +21 -20
- package/dist/services/automations/entities/trigger.d.ts +16 -8
- package/dist/services/automations/entities/trigger.js +28 -17
- package/dist/services/automations/entities/trigger.mjs +27 -16
- package/dist/services/automations/repositories/automation.repository.js +4 -4
- package/dist/services/automations/repositories/automation.repository.mjs +4 -4
- package/dist/services/automations/runner/automation-runner.js +1 -1
- package/dist/services/automations/runner/automation-runner.mjs +1 -1
- package/dist/services/automations/use-cases/claim-due-automations.use-case.js +4 -4
- package/dist/services/automations/use-cases/claim-due-automations.use-case.mjs +4 -4
- package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.js +1 -1
- package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.mjs +1 -1
- package/dist/services/automations/use-cases/create-automation.use-case.d.ts +2 -2
- package/dist/services/automations/use-cases/create-automation.use-case.js +4 -3
- package/dist/services/automations/use-cases/create-automation.use-case.mjs +4 -3
- package/dist/services/automations/use-cases/delete-automation.use-case.js +1 -1
- package/dist/services/automations/use-cases/delete-automation.use-case.mjs +1 -1
- package/dist/services/automations/use-cases/issue-automation-event-token.use-case.js +2 -2
- package/dist/services/automations/use-cases/issue-automation-event-token.use-case.mjs +2 -2
- package/dist/services/automations/use-cases/receive-automation-event.use-case.js +3 -2
- package/dist/services/automations/use-cases/receive-automation-event.use-case.mjs +3 -2
- package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.js +2 -2
- package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.mjs +2 -2
- package/dist/services/automations/use-cases/update-automation.use-case.d.ts +2 -2
- package/dist/services/automations/use-cases/update-automation.use-case.js +8 -5
- package/dist/services/automations/use-cases/update-automation.use-case.mjs +8 -5
- package/dist/services/automations/utils/automation-fields.d.ts +3 -4
- package/dist/services/automations/utils/automation-fields.js +3 -5
- package/dist/services/automations/utils/automation-fields.mjs +4 -6
- package/docs/README.md +1 -1
- package/docs/agent.md +2 -2
- package/docs/automations.md +139 -37
- package/docs/mcp-servers.md +4 -4
- package/examples/automations/create-automation.ts +6 -5
- package/examples/automations/create-event-automation.ts +1 -1
- package/examples/automations/list-automations.ts +3 -2
- package/examples/automations/preview-automation-runs.ts +1 -1
- package/examples/automations/revoke-automation-event-token.ts +1 -1
- package/examples/automations/update-automation.ts +4 -5
- package/examples/mcps/internal-mcp.ts +3 -3
- package/examples/run-agent/run-agent-with-identity.ts +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to `openloops` are written here, newest first. The format follows [Keep a Changelog](https://keepachangelog.com) and the versions follow [semantic versioning](https://semver.org), where a minor version can still break the API while the package is in beta. Changes before 0.4.0 are not recorded here, see the commits and the tags.
|
|
4
4
|
|
|
5
|
+
## 0.5.0
|
|
6
|
+
|
|
7
|
+
This version breaks the shape of the automations. Follow the [migration steps](docs/automations.md#migrating-to-050) when you update.
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- An automation can have a schedule and an event trigger at the same time. `triggers` is an object with the optional keys `schedule` and `event`, at least one of them, and never more than one of each type.
|
|
12
|
+
- Every trigger has its own `enabled`. `updateAutomation` merges the triggers key by key, adds a trigger by sending its key and removes it with `null`. The last trigger cannot be removed.
|
|
13
|
+
- `migrateAutomations` also moves `trigger` into `triggers` and replaces the index `enabled_1_nextRunAt_1`. Its report has a new `triggersMoved` count.
|
|
14
|
+
- The documentation of what `issueAutomationEventToken`, `revokeAutomationEventToken` and `rotateAutomationEventTokens` do and which of them changes `eventTokenVersion`.
|
|
15
|
+
- `AutomationPayload`, the type of the payload of `createAutomation` and `updateAutomation`, where a trigger can be sent partially (`event: {}`, `schedule: { enabled: false }`) or as `null` to remove it.
|
|
16
|
+
- Removing the event trigger counts as a rotation of its tokens, so the tokens issued before do not work again if an event is added later.
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- `trigger` is now `triggers`. The schedule is `triggers.schedule` with `enabled`, `recurrence`, `timezone` and `nextRunAt`, and the event is `triggers.event` with only `enabled`. The event trigger has no `timezone`. `tokenVersion` is now `eventTokenVersion`, a field of the automation that starts at 1 and only goes up, so it survives when the event trigger is removed.
|
|
21
|
+
- `enabled` and `nextRunAt` moved from the automation to the trigger they belong to. There is no switch for the whole automation, so pausing everything means turning off both triggers. `createAutomation` and `updateAutomation` fail when they receive `trigger` or `enabled`.
|
|
22
|
+
- Events are refused with the reason `disabled` when `triggers.event.enabled` is `false`, and the queued events of an automation without an enabled event trigger are skipped with the reason `The event trigger of the automation is turned off`.
|
|
23
|
+
- The runner gives the agent the timezone of the owner of the automation, taken from their preferences, and not the one of the trigger.
|
|
24
|
+
- The index of the automations is now `{ 'triggers.schedule.enabled': 1, 'triggers.schedule.nextRunAt': 1 }`. Run `ensureIndexes` or the migration.
|
|
25
|
+
|
|
26
|
+
### Removed
|
|
27
|
+
|
|
28
|
+
- The fields `trigger`, `enabled` and `nextRunAt` of the automation, and the possibility to keep one trigger only. There are no aliases.
|
|
29
|
+
|
|
5
30
|
## 0.4.4
|
|
6
31
|
|
|
7
32
|
### Fixed
|
|
@@ -22,7 +22,7 @@ __export(automations_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(automations_exports);
|
|
24
24
|
const automations = [
|
|
25
|
-
{ key: { enabled: 1, nextRunAt: 1 } },
|
|
25
|
+
{ key: { "triggers.schedule.enabled": 1, "triggers.schedule.nextRunAt": 1 } },
|
|
26
26
|
{ key: { ownerId: 1, createdAt: -1 } }
|
|
27
27
|
];
|
|
28
28
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -27,10 +27,11 @@ const RENAMES = [
|
|
|
27
27
|
["agentic_job_runs", "automation_runs"]
|
|
28
28
|
];
|
|
29
29
|
const OLD_RUN_INDEX = "jobId_1_createdAt_-1";
|
|
30
|
+
const OLD_AUTOMATION_INDEX = "enabled_1_nextRunAt_1";
|
|
30
31
|
const exists = async (db, name) => (await db.listCollections({ name }).toArray()).length > 0;
|
|
31
32
|
async function migrateAutomations(db, options = {}) {
|
|
32
33
|
const dryRun = Boolean(options.dryRun);
|
|
33
|
-
const report = { dryRun, renamedCollections: [], triggersCreated: 0, runsUpdated: 0, droppedIndexes: [] };
|
|
34
|
+
const report = { dryRun, renamedCollections: [], triggersCreated: 0, triggersMoved: 0, runsUpdated: 0, droppedIndexes: [] };
|
|
34
35
|
for (const [from, to] of RENAMES) {
|
|
35
36
|
if (!await exists(db, from)) continue;
|
|
36
37
|
if (await exists(db, to)) throw new Error(`Cannot rename ${from} to ${to} because ${to} already exists`);
|
|
@@ -40,26 +41,52 @@ async function migrateAutomations(db, options = {}) {
|
|
|
40
41
|
const automations = db.collection(dryRun && report.renamedCollections.length ? "agentic_jobs" : "automations");
|
|
41
42
|
const runs = db.collection(dryRun && report.renamedCollections.length ? "agentic_job_runs" : "automation_runs");
|
|
42
43
|
const withoutTrigger = { trigger: { $exists: false }, recurrence: { $exists: true } };
|
|
44
|
+
const withTrigger = { trigger: { $exists: true } };
|
|
43
45
|
const withJobId = { jobId: { $exists: true } };
|
|
44
46
|
if (dryRun) {
|
|
45
47
|
report.triggersCreated = await automations.countDocuments(withoutTrigger);
|
|
48
|
+
report.triggersMoved = await automations.countDocuments(withTrigger) + report.triggersCreated;
|
|
46
49
|
report.runsUpdated = await runs.countDocuments(withJobId);
|
|
47
50
|
} else {
|
|
48
51
|
const triggers = await automations.updateMany(withoutTrigger, [
|
|
49
52
|
{ $set: { trigger: { type: "schedule", recurrence: "$recurrence", timezone: "$timezone" } } },
|
|
50
53
|
{ $unset: ["recurrence", "timezone"] }
|
|
51
54
|
]);
|
|
55
|
+
const split = await automations.updateMany(withTrigger, [
|
|
56
|
+
{
|
|
57
|
+
$set: {
|
|
58
|
+
triggers: {
|
|
59
|
+
$cond: [
|
|
60
|
+
{ $eq: ["$trigger.type", "schedule"] },
|
|
61
|
+
{
|
|
62
|
+
schedule: {
|
|
63
|
+
$mergeObjects: [
|
|
64
|
+
{ enabled: { $ifNull: ["$enabled", true] }, recurrence: "$trigger.recurrence", timezone: "$trigger.timezone" },
|
|
65
|
+
{ $cond: [{ $ifNull: ["$nextRunAt", false] }, { nextRunAt: "$nextRunAt" }, {}] }
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
{ event: { enabled: { $ifNull: ["$enabled", true] } } }
|
|
70
|
+
]
|
|
71
|
+
},
|
|
72
|
+
eventTokenVersion: { $ifNull: ["$trigger.tokenVersion", 1] }
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
{ $unset: ["trigger", "enabled", "nextRunAt"] }
|
|
76
|
+
]);
|
|
52
77
|
const moved = await runs.updateMany(withJobId, [
|
|
53
78
|
{ $set: { automationId: "$jobId" } },
|
|
54
79
|
{ $unset: "jobId" }
|
|
55
80
|
]);
|
|
56
81
|
report.triggersCreated = triggers.modifiedCount;
|
|
82
|
+
report.triggersMoved = split.modifiedCount;
|
|
57
83
|
report.runsUpdated = moved.modifiedCount;
|
|
58
84
|
}
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
if (!
|
|
62
|
-
|
|
85
|
+
for (const [collection, name] of [[runs, OLD_RUN_INDEX], [automations, OLD_AUTOMATION_INDEX]]) {
|
|
86
|
+
const indexes = await collection.indexes().catch(() => []);
|
|
87
|
+
if (!indexes.some((index) => index.name === name)) continue;
|
|
88
|
+
if (!dryRun) await collection.dropIndex(name);
|
|
89
|
+
report.droppedIndexes.push(name);
|
|
63
90
|
}
|
|
64
91
|
if (!dryRun) await (0, import_indexes.ensureIndexes)(db);
|
|
65
92
|
return report;
|
|
@@ -5,10 +5,11 @@ const RENAMES = [
|
|
|
5
5
|
["agentic_job_runs", "automation_runs"]
|
|
6
6
|
];
|
|
7
7
|
const OLD_RUN_INDEX = "jobId_1_createdAt_-1";
|
|
8
|
+
const OLD_AUTOMATION_INDEX = "enabled_1_nextRunAt_1";
|
|
8
9
|
const exists = async (db, name) => (await db.listCollections({ name }).toArray()).length > 0;
|
|
9
10
|
async function migrateAutomations(db, options = {}) {
|
|
10
11
|
const dryRun = Boolean(options.dryRun);
|
|
11
|
-
const report = { dryRun, renamedCollections: [], triggersCreated: 0, runsUpdated: 0, droppedIndexes: [] };
|
|
12
|
+
const report = { dryRun, renamedCollections: [], triggersCreated: 0, triggersMoved: 0, runsUpdated: 0, droppedIndexes: [] };
|
|
12
13
|
for (const [from, to] of RENAMES) {
|
|
13
14
|
if (!await exists(db, from)) continue;
|
|
14
15
|
if (await exists(db, to)) throw new Error(`Cannot rename ${from} to ${to} because ${to} already exists`);
|
|
@@ -18,26 +19,52 @@ async function migrateAutomations(db, options = {}) {
|
|
|
18
19
|
const automations = db.collection(dryRun && report.renamedCollections.length ? "agentic_jobs" : "automations");
|
|
19
20
|
const runs = db.collection(dryRun && report.renamedCollections.length ? "agentic_job_runs" : "automation_runs");
|
|
20
21
|
const withoutTrigger = { trigger: { $exists: false }, recurrence: { $exists: true } };
|
|
22
|
+
const withTrigger = { trigger: { $exists: true } };
|
|
21
23
|
const withJobId = { jobId: { $exists: true } };
|
|
22
24
|
if (dryRun) {
|
|
23
25
|
report.triggersCreated = await automations.countDocuments(withoutTrigger);
|
|
26
|
+
report.triggersMoved = await automations.countDocuments(withTrigger) + report.triggersCreated;
|
|
24
27
|
report.runsUpdated = await runs.countDocuments(withJobId);
|
|
25
28
|
} else {
|
|
26
29
|
const triggers = await automations.updateMany(withoutTrigger, [
|
|
27
30
|
{ $set: { trigger: { type: "schedule", recurrence: "$recurrence", timezone: "$timezone" } } },
|
|
28
31
|
{ $unset: ["recurrence", "timezone"] }
|
|
29
32
|
]);
|
|
33
|
+
const split = await automations.updateMany(withTrigger, [
|
|
34
|
+
{
|
|
35
|
+
$set: {
|
|
36
|
+
triggers: {
|
|
37
|
+
$cond: [
|
|
38
|
+
{ $eq: ["$trigger.type", "schedule"] },
|
|
39
|
+
{
|
|
40
|
+
schedule: {
|
|
41
|
+
$mergeObjects: [
|
|
42
|
+
{ enabled: { $ifNull: ["$enabled", true] }, recurrence: "$trigger.recurrence", timezone: "$trigger.timezone" },
|
|
43
|
+
{ $cond: [{ $ifNull: ["$nextRunAt", false] }, { nextRunAt: "$nextRunAt" }, {}] }
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
{ event: { enabled: { $ifNull: ["$enabled", true] } } }
|
|
48
|
+
]
|
|
49
|
+
},
|
|
50
|
+
eventTokenVersion: { $ifNull: ["$trigger.tokenVersion", 1] }
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
{ $unset: ["trigger", "enabled", "nextRunAt"] }
|
|
54
|
+
]);
|
|
30
55
|
const moved = await runs.updateMany(withJobId, [
|
|
31
56
|
{ $set: { automationId: "$jobId" } },
|
|
32
57
|
{ $unset: "jobId" }
|
|
33
58
|
]);
|
|
34
59
|
report.triggersCreated = triggers.modifiedCount;
|
|
60
|
+
report.triggersMoved = split.modifiedCount;
|
|
35
61
|
report.runsUpdated = moved.modifiedCount;
|
|
36
62
|
}
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
if (!
|
|
40
|
-
|
|
63
|
+
for (const [collection, name] of [[runs, OLD_RUN_INDEX], [automations, OLD_AUTOMATION_INDEX]]) {
|
|
64
|
+
const indexes = await collection.indexes().catch(() => []);
|
|
65
|
+
if (!indexes.some((index) => index.name === name)) continue;
|
|
66
|
+
if (!dryRun) await collection.dropIndex(name);
|
|
67
|
+
report.droppedIndexes.push(name);
|
|
41
68
|
}
|
|
42
69
|
if (!dryRun) await ensureIndexes(db);
|
|
43
70
|
return report;
|
|
@@ -1,38 +1,38 @@
|
|
|
1
1
|
import { ObjectId } from 'mongodb';
|
|
2
2
|
import { BaseEntity, BaseEntityProps } from '../../../common/base/base.entity.js';
|
|
3
|
-
import {
|
|
3
|
+
import { AutomationTriggers, AutomationTriggersInput } from './trigger';
|
|
4
4
|
import type { AutomationRunStatus } from './automation-run.entity';
|
|
5
5
|
export interface AutomationProps extends BaseEntityProps {
|
|
6
6
|
name?: string;
|
|
7
7
|
instructions?: string;
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
triggers?: AutomationTriggers;
|
|
9
|
+
eventTokenVersion?: number;
|
|
10
10
|
modelProvider?: string;
|
|
11
11
|
modelName?: string;
|
|
12
|
-
nextRunAt?: string | Date | null;
|
|
13
12
|
lastRunAt?: string | Date | null;
|
|
14
13
|
lastRunStatus?: AutomationRunStatus;
|
|
15
14
|
lockedBy?: string | null;
|
|
16
15
|
lockedUntil?: string | Date | null;
|
|
17
16
|
activeRunId?: string | ObjectId | null;
|
|
18
17
|
}
|
|
18
|
+
export type AutomationPayload = Omit<AutomationProps, 'triggers'> & {
|
|
19
|
+
triggers?: AutomationTriggersInput;
|
|
20
|
+
};
|
|
19
21
|
export declare class Automation extends BaseEntity {
|
|
20
22
|
private readonly props;
|
|
21
23
|
name?: string | undefined;
|
|
22
24
|
instructions?: string | undefined;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
triggers?: AutomationTriggers | undefined;
|
|
26
|
+
eventTokenVersion?: number | undefined;
|
|
25
27
|
modelProvider?: string | undefined;
|
|
26
28
|
modelName?: string | undefined;
|
|
27
|
-
nextRunAt?: Date | undefined;
|
|
28
29
|
lastRunAt?: Date | undefined;
|
|
29
30
|
lastRunStatus?: AutomationRunStatus | undefined;
|
|
30
31
|
lockedBy?: string | undefined;
|
|
31
32
|
lockedUntil?: Date | undefined;
|
|
32
33
|
activeRunId?: ObjectId | undefined;
|
|
33
34
|
static fromJSON(props: AutomationProps): Automation;
|
|
34
|
-
constructor(props: AutomationProps, name?: string | undefined, instructions?: string | undefined,
|
|
35
|
+
constructor(props: AutomationProps, name?: string | undefined, instructions?: string | undefined, triggers?: AutomationTriggers | undefined, eventTokenVersion?: number | undefined, modelProvider?: string | undefined, modelName?: string | undefined, lastRunAt?: Date | undefined, lastRunStatus?: AutomationRunStatus | undefined, lockedBy?: string | undefined, lockedUntil?: Date | undefined, activeRunId?: ObjectId | undefined);
|
|
35
36
|
toJSON(): AutomationProps;
|
|
36
|
-
isScheduled(): boolean;
|
|
37
37
|
scheduleNextRun(from?: Date): this;
|
|
38
38
|
}
|
|
@@ -29,17 +29,22 @@ const toDate = (value) => {
|
|
|
29
29
|
if (Number.isNaN(date.getTime())) throw new Error("{{value_must_be_a_date}}");
|
|
30
30
|
return date;
|
|
31
31
|
};
|
|
32
|
+
const readTriggerDates = (triggers) => {
|
|
33
|
+
if (!triggers?.schedule) return triggers;
|
|
34
|
+
const { nextRunAt, ...schedule } = triggers.schedule;
|
|
35
|
+
const date = toDate(nextRunAt);
|
|
36
|
+
return { ...triggers, schedule: { ...schedule, ...date ? { nextRunAt: date } : {} } };
|
|
37
|
+
};
|
|
32
38
|
class Automation extends import_base.BaseEntity {
|
|
33
|
-
constructor(props, name, instructions,
|
|
39
|
+
constructor(props, name, instructions, triggers, eventTokenVersion, modelProvider, modelName, lastRunAt, lastRunStatus, lockedBy, lockedUntil, activeRunId) {
|
|
34
40
|
super(props);
|
|
35
41
|
this.props = props;
|
|
36
42
|
this.name = name;
|
|
37
43
|
this.instructions = instructions;
|
|
38
|
-
this.
|
|
39
|
-
this.
|
|
44
|
+
this.triggers = triggers;
|
|
45
|
+
this.eventTokenVersion = eventTokenVersion;
|
|
40
46
|
this.modelProvider = modelProvider;
|
|
41
47
|
this.modelName = modelName;
|
|
42
|
-
this.nextRunAt = nextRunAt;
|
|
43
48
|
this.lastRunAt = lastRunAt;
|
|
44
49
|
this.lastRunStatus = lastRunStatus;
|
|
45
50
|
this.lockedBy = lockedBy;
|
|
@@ -49,11 +54,10 @@ class Automation extends import_base.BaseEntity {
|
|
|
49
54
|
props;
|
|
50
55
|
name;
|
|
51
56
|
instructions;
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
triggers;
|
|
58
|
+
eventTokenVersion;
|
|
54
59
|
modelProvider;
|
|
55
60
|
modelName;
|
|
56
|
-
nextRunAt;
|
|
57
61
|
lastRunAt;
|
|
58
62
|
lastRunStatus;
|
|
59
63
|
lockedBy;
|
|
@@ -64,11 +68,10 @@ class Automation extends import_base.BaseEntity {
|
|
|
64
68
|
props,
|
|
65
69
|
props.name || void 0,
|
|
66
70
|
props.instructions || void 0,
|
|
67
|
-
props.
|
|
68
|
-
props.
|
|
71
|
+
readTriggerDates(props.triggers),
|
|
72
|
+
props.eventTokenVersion,
|
|
69
73
|
props.modelProvider || void 0,
|
|
70
74
|
props.modelName || void 0,
|
|
71
|
-
toDate(props.nextRunAt),
|
|
72
75
|
toDate(props.lastRunAt),
|
|
73
76
|
props.lastRunStatus,
|
|
74
77
|
props.lockedBy || void 0,
|
|
@@ -81,11 +84,10 @@ class Automation extends import_base.BaseEntity {
|
|
|
81
84
|
...super.toJSON(),
|
|
82
85
|
name: this.name,
|
|
83
86
|
instructions: this.instructions,
|
|
84
|
-
|
|
85
|
-
|
|
87
|
+
triggers: this.triggers,
|
|
88
|
+
eventTokenVersion: this.eventTokenVersion,
|
|
86
89
|
modelProvider: this.modelProvider,
|
|
87
90
|
modelName: this.modelName,
|
|
88
|
-
nextRunAt: this.nextRunAt,
|
|
89
91
|
lastRunAt: this.lastRunAt,
|
|
90
92
|
lastRunStatus: this.lastRunStatus,
|
|
91
93
|
lockedBy: this.lockedBy,
|
|
@@ -93,16 +95,15 @@ class Automation extends import_base.BaseEntity {
|
|
|
93
95
|
activeRunId: this.activeRunId
|
|
94
96
|
};
|
|
95
97
|
}
|
|
96
|
-
isScheduled() {
|
|
97
|
-
return this.trigger?.type === "schedule";
|
|
98
|
-
}
|
|
99
98
|
scheduleNextRun(from = /* @__PURE__ */ new Date()) {
|
|
100
|
-
const
|
|
101
|
-
|
|
99
|
+
const schedule = this.triggers?.schedule;
|
|
100
|
+
if (!schedule) return this;
|
|
101
|
+
const next = schedule.enabled ? (0, import_next_run.computeNextRun)(schedule.recurrence, schedule.timezone, from) : null;
|
|
102
102
|
if (next) {
|
|
103
|
-
|
|
103
|
+
schedule.nextRunAt = next;
|
|
104
104
|
} else {
|
|
105
|
-
|
|
105
|
+
delete schedule.nextRunAt;
|
|
106
|
+
this.toUnset = { ...this.toUnset, "triggers.schedule.nextRunAt": "" };
|
|
106
107
|
}
|
|
107
108
|
return this;
|
|
108
109
|
}
|
|
@@ -7,17 +7,22 @@ const toDate = (value) => {
|
|
|
7
7
|
if (Number.isNaN(date.getTime())) throw new Error("{{value_must_be_a_date}}");
|
|
8
8
|
return date;
|
|
9
9
|
};
|
|
10
|
+
const readTriggerDates = (triggers) => {
|
|
11
|
+
if (!triggers?.schedule) return triggers;
|
|
12
|
+
const { nextRunAt, ...schedule } = triggers.schedule;
|
|
13
|
+
const date = toDate(nextRunAt);
|
|
14
|
+
return { ...triggers, schedule: { ...schedule, ...date ? { nextRunAt: date } : {} } };
|
|
15
|
+
};
|
|
10
16
|
class Automation extends BaseEntity {
|
|
11
|
-
constructor(props, name, instructions,
|
|
17
|
+
constructor(props, name, instructions, triggers, eventTokenVersion, modelProvider, modelName, lastRunAt, lastRunStatus, lockedBy, lockedUntil, activeRunId) {
|
|
12
18
|
super(props);
|
|
13
19
|
this.props = props;
|
|
14
20
|
this.name = name;
|
|
15
21
|
this.instructions = instructions;
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
22
|
+
this.triggers = triggers;
|
|
23
|
+
this.eventTokenVersion = eventTokenVersion;
|
|
18
24
|
this.modelProvider = modelProvider;
|
|
19
25
|
this.modelName = modelName;
|
|
20
|
-
this.nextRunAt = nextRunAt;
|
|
21
26
|
this.lastRunAt = lastRunAt;
|
|
22
27
|
this.lastRunStatus = lastRunStatus;
|
|
23
28
|
this.lockedBy = lockedBy;
|
|
@@ -27,11 +32,10 @@ class Automation extends BaseEntity {
|
|
|
27
32
|
props;
|
|
28
33
|
name;
|
|
29
34
|
instructions;
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
triggers;
|
|
36
|
+
eventTokenVersion;
|
|
32
37
|
modelProvider;
|
|
33
38
|
modelName;
|
|
34
|
-
nextRunAt;
|
|
35
39
|
lastRunAt;
|
|
36
40
|
lastRunStatus;
|
|
37
41
|
lockedBy;
|
|
@@ -42,11 +46,10 @@ class Automation extends BaseEntity {
|
|
|
42
46
|
props,
|
|
43
47
|
props.name || void 0,
|
|
44
48
|
props.instructions || void 0,
|
|
45
|
-
props.
|
|
46
|
-
props.
|
|
49
|
+
readTriggerDates(props.triggers),
|
|
50
|
+
props.eventTokenVersion,
|
|
47
51
|
props.modelProvider || void 0,
|
|
48
52
|
props.modelName || void 0,
|
|
49
|
-
toDate(props.nextRunAt),
|
|
50
53
|
toDate(props.lastRunAt),
|
|
51
54
|
props.lastRunStatus,
|
|
52
55
|
props.lockedBy || void 0,
|
|
@@ -59,11 +62,10 @@ class Automation extends BaseEntity {
|
|
|
59
62
|
...super.toJSON(),
|
|
60
63
|
name: this.name,
|
|
61
64
|
instructions: this.instructions,
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
triggers: this.triggers,
|
|
66
|
+
eventTokenVersion: this.eventTokenVersion,
|
|
64
67
|
modelProvider: this.modelProvider,
|
|
65
68
|
modelName: this.modelName,
|
|
66
|
-
nextRunAt: this.nextRunAt,
|
|
67
69
|
lastRunAt: this.lastRunAt,
|
|
68
70
|
lastRunStatus: this.lastRunStatus,
|
|
69
71
|
lockedBy: this.lockedBy,
|
|
@@ -71,16 +73,15 @@ class Automation extends BaseEntity {
|
|
|
71
73
|
activeRunId: this.activeRunId
|
|
72
74
|
};
|
|
73
75
|
}
|
|
74
|
-
isScheduled() {
|
|
75
|
-
return this.trigger?.type === "schedule";
|
|
76
|
-
}
|
|
77
76
|
scheduleNextRun(from = /* @__PURE__ */ new Date()) {
|
|
78
|
-
const
|
|
79
|
-
|
|
77
|
+
const schedule = this.triggers?.schedule;
|
|
78
|
+
if (!schedule) return this;
|
|
79
|
+
const next = schedule.enabled ? computeNextRun(schedule.recurrence, schedule.timezone, from) : null;
|
|
80
80
|
if (next) {
|
|
81
|
-
|
|
81
|
+
schedule.nextRunAt = next;
|
|
82
82
|
} else {
|
|
83
|
-
|
|
83
|
+
delete schedule.nextRunAt;
|
|
84
|
+
this.toUnset = { ...this.toUnset, "triggers.schedule.nextRunAt": "" };
|
|
84
85
|
}
|
|
85
86
|
return this;
|
|
86
87
|
}
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import { Recurrence } from './recurrence';
|
|
2
|
-
export
|
|
3
|
-
|
|
2
|
+
export interface ScheduleTrigger {
|
|
3
|
+
enabled: boolean;
|
|
4
4
|
recurrence: Recurrence;
|
|
5
5
|
timezone: string;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
export
|
|
6
|
+
nextRunAt?: string | Date | null;
|
|
7
|
+
}
|
|
8
|
+
export interface EventTrigger {
|
|
9
|
+
enabled: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface AutomationTriggers {
|
|
12
|
+
schedule?: ScheduleTrigger;
|
|
13
|
+
event?: EventTrigger;
|
|
14
|
+
}
|
|
15
|
+
export interface AutomationTriggersInput {
|
|
16
|
+
schedule?: Partial<Pick<ScheduleTrigger, 'enabled' | 'recurrence' | 'timezone'>> | null;
|
|
17
|
+
event?: Partial<EventTrigger> | null;
|
|
18
|
+
}
|
|
19
|
+
export declare function readTriggers(input: unknown, existing?: AutomationTriggers): AutomationTriggers;
|
|
@@ -18,32 +18,43 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var trigger_exports = {};
|
|
20
20
|
__export(trigger_exports, {
|
|
21
|
-
|
|
21
|
+
readTriggers: () => readTriggers
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(trigger_exports);
|
|
24
24
|
var import_recurrence = require("./recurrence");
|
|
25
25
|
var import_next_run = require("../utils/next-run");
|
|
26
26
|
const isRecord = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
27
|
-
|
|
28
|
-
if (
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
const readEnabled = (value, fallback) => {
|
|
28
|
+
if (value === void 0) return fallback;
|
|
29
|
+
if (typeof value !== "boolean") throw new Error("The field enabled must be true or false");
|
|
30
|
+
return value;
|
|
31
|
+
};
|
|
32
|
+
function readSchedule(input, existing) {
|
|
33
|
+
if (!isRecord(input)) throw new Error("The schedule trigger must be an object");
|
|
33
34
|
const has = (key) => input[key] !== void 0;
|
|
34
|
-
if (type === "event") {
|
|
35
|
-
const event = previous;
|
|
36
|
-
const timezone = has("timezone") ? (0, import_next_run.assertTimezone)(input.timezone) : event?.timezone;
|
|
37
|
-
return { type, tokenVersion: event?.tokenVersion ?? 1, ...timezone ? { timezone } : {} };
|
|
38
|
-
}
|
|
39
|
-
const schedule = previous;
|
|
40
35
|
return {
|
|
41
|
-
|
|
42
|
-
recurrence: has("recurrence") || !
|
|
43
|
-
timezone: has("timezone") || !
|
|
36
|
+
enabled: readEnabled(input.enabled, existing?.enabled ?? true),
|
|
37
|
+
recurrence: has("recurrence") || !existing ? (0, import_recurrence.validateRecurrence)(input.recurrence) : existing.recurrence,
|
|
38
|
+
timezone: has("timezone") || !existing ? (0, import_next_run.assertTimezone)(input.timezone) : existing.timezone,
|
|
39
|
+
...existing?.nextRunAt ? { nextRunAt: existing.nextRunAt } : {}
|
|
44
40
|
};
|
|
45
41
|
}
|
|
42
|
+
function readEvent(input, existing) {
|
|
43
|
+
if (!isRecord(input)) throw new Error("The event trigger must be an object");
|
|
44
|
+
return { enabled: readEnabled(input.enabled, existing?.enabled ?? true) };
|
|
45
|
+
}
|
|
46
|
+
function readTriggers(input, existing = {}) {
|
|
47
|
+
if (!isRecord(input)) throw new Error("The triggers are required");
|
|
48
|
+
if (Object.keys(input).some((key) => key !== "schedule" && key !== "event")) throw new Error("The triggers can only be schedule or event");
|
|
49
|
+
const triggers = { ...existing };
|
|
50
|
+
if (input.schedule === null) delete triggers.schedule;
|
|
51
|
+
else if (input.schedule !== void 0) triggers.schedule = readSchedule(input.schedule, existing.schedule);
|
|
52
|
+
if (input.event === null) delete triggers.event;
|
|
53
|
+
else if (input.event !== void 0) triggers.event = readEvent(input.event, existing.event);
|
|
54
|
+
if (!triggers.schedule && !triggers.event) throw new Error("The automation needs at least one trigger");
|
|
55
|
+
return triggers;
|
|
56
|
+
}
|
|
46
57
|
// Annotate the CommonJS export names for ESM import in node:
|
|
47
58
|
0 && (module.exports = {
|
|
48
|
-
|
|
59
|
+
readTriggers
|
|
49
60
|
});
|
|
@@ -2,25 +2,36 @@ import "../../../chunk-CF3WOMB3.mjs";
|
|
|
2
2
|
import { validateRecurrence } from "./recurrence";
|
|
3
3
|
import { assertTimezone } from "../utils/next-run";
|
|
4
4
|
const isRecord = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
5
|
-
|
|
6
|
-
if (
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
const readEnabled = (value, fallback) => {
|
|
6
|
+
if (value === void 0) return fallback;
|
|
7
|
+
if (typeof value !== "boolean") throw new Error("The field enabled must be true or false");
|
|
8
|
+
return value;
|
|
9
|
+
};
|
|
10
|
+
function readSchedule(input, existing) {
|
|
11
|
+
if (!isRecord(input)) throw new Error("The schedule trigger must be an object");
|
|
11
12
|
const has = (key) => input[key] !== void 0;
|
|
12
|
-
if (type === "event") {
|
|
13
|
-
const event = previous;
|
|
14
|
-
const timezone = has("timezone") ? assertTimezone(input.timezone) : event?.timezone;
|
|
15
|
-
return { type, tokenVersion: event?.tokenVersion ?? 1, ...timezone ? { timezone } : {} };
|
|
16
|
-
}
|
|
17
|
-
const schedule = previous;
|
|
18
13
|
return {
|
|
19
|
-
|
|
20
|
-
recurrence: has("recurrence") || !
|
|
21
|
-
timezone: has("timezone") || !
|
|
14
|
+
enabled: readEnabled(input.enabled, existing?.enabled ?? true),
|
|
15
|
+
recurrence: has("recurrence") || !existing ? validateRecurrence(input.recurrence) : existing.recurrence,
|
|
16
|
+
timezone: has("timezone") || !existing ? assertTimezone(input.timezone) : existing.timezone,
|
|
17
|
+
...existing?.nextRunAt ? { nextRunAt: existing.nextRunAt } : {}
|
|
22
18
|
};
|
|
23
19
|
}
|
|
20
|
+
function readEvent(input, existing) {
|
|
21
|
+
if (!isRecord(input)) throw new Error("The event trigger must be an object");
|
|
22
|
+
return { enabled: readEnabled(input.enabled, existing?.enabled ?? true) };
|
|
23
|
+
}
|
|
24
|
+
function readTriggers(input, existing = {}) {
|
|
25
|
+
if (!isRecord(input)) throw new Error("The triggers are required");
|
|
26
|
+
if (Object.keys(input).some((key) => key !== "schedule" && key !== "event")) throw new Error("The triggers can only be schedule or event");
|
|
27
|
+
const triggers = { ...existing };
|
|
28
|
+
if (input.schedule === null) delete triggers.schedule;
|
|
29
|
+
else if (input.schedule !== void 0) triggers.schedule = readSchedule(input.schedule, existing.schedule);
|
|
30
|
+
if (input.event === null) delete triggers.event;
|
|
31
|
+
else if (input.event !== void 0) triggers.event = readEvent(input.event, existing.event);
|
|
32
|
+
if (!triggers.schedule && !triggers.event) throw new Error("The automation needs at least one trigger");
|
|
33
|
+
return triggers;
|
|
34
|
+
}
|
|
24
35
|
export {
|
|
25
|
-
|
|
36
|
+
readTriggers
|
|
26
37
|
};
|
|
@@ -62,18 +62,18 @@ let AutomationRepository = class {
|
|
|
62
62
|
}
|
|
63
63
|
async findDue(now, limit) {
|
|
64
64
|
return await this._.findMany({
|
|
65
|
-
where: { enabled: true, nextRunAt: { $lte: now }, ...this.unlocked(now) },
|
|
66
|
-
options: { page: 1, limit, sortBy: { nextRunAt: 1 } }
|
|
65
|
+
where: { "triggers.schedule.enabled": true, "triggers.schedule.nextRunAt": { $lte: now }, ...this.unlocked(now) },
|
|
66
|
+
options: { page: 1, limit, sortBy: { "triggers.schedule.nextRunAt": 1 } }
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
69
|
async claim(params) {
|
|
70
70
|
const { id, workerId, now, leaseMs, nextRunAt } = params;
|
|
71
71
|
return await this._.updateOne({
|
|
72
|
-
where: { _id: this._.parseId(id), enabled: true, nextRunAt: { $lte: now }, ...this.unlocked(now) },
|
|
72
|
+
where: { _id: this._.parseId(id), "triggers.schedule.enabled": true, "triggers.schedule.nextRunAt": { $lte: now }, ...this.unlocked(now) },
|
|
73
73
|
payload: {
|
|
74
74
|
lockedBy: workerId,
|
|
75
75
|
lockedUntil: new Date(now.getTime() + leaseMs),
|
|
76
|
-
...nextRunAt ? { nextRunAt } : { toUnset: { nextRunAt: "" } }
|
|
76
|
+
...nextRunAt ? { "triggers.schedule.nextRunAt": nextRunAt } : { toUnset: { "triggers.schedule.nextRunAt": "" } }
|
|
77
77
|
}
|
|
78
78
|
});
|
|
79
79
|
}
|
|
@@ -34,18 +34,18 @@ let AutomationRepository = class {
|
|
|
34
34
|
}
|
|
35
35
|
async findDue(now, limit) {
|
|
36
36
|
return await this._.findMany({
|
|
37
|
-
where: { enabled: true, nextRunAt: { $lte: now }, ...this.unlocked(now) },
|
|
38
|
-
options: { page: 1, limit, sortBy: { nextRunAt: 1 } }
|
|
37
|
+
where: { "triggers.schedule.enabled": true, "triggers.schedule.nextRunAt": { $lte: now }, ...this.unlocked(now) },
|
|
38
|
+
options: { page: 1, limit, sortBy: { "triggers.schedule.nextRunAt": 1 } }
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
async claim(params) {
|
|
42
42
|
const { id, workerId, now, leaseMs, nextRunAt } = params;
|
|
43
43
|
return await this._.updateOne({
|
|
44
|
-
where: { _id: this._.parseId(id), enabled: true, nextRunAt: { $lte: now }, ...this.unlocked(now) },
|
|
44
|
+
where: { _id: this._.parseId(id), "triggers.schedule.enabled": true, "triggers.schedule.nextRunAt": { $lte: now }, ...this.unlocked(now) },
|
|
45
45
|
payload: {
|
|
46
46
|
lockedBy: workerId,
|
|
47
47
|
lockedUntil: new Date(now.getTime() + leaseMs),
|
|
48
|
-
...nextRunAt ? { nextRunAt } : { toUnset: { nextRunAt: "" } }
|
|
48
|
+
...nextRunAt ? { "triggers.schedule.nextRunAt": nextRunAt } : { toUnset: { "triggers.schedule.nextRunAt": "" } }
|
|
49
49
|
}
|
|
50
50
|
});
|
|
51
51
|
}
|
|
@@ -162,7 +162,7 @@ class AutomationRunner {
|
|
|
162
162
|
input: { message: (0, import_event_message.buildMessage)(automation.instructions, run.payload), chatId },
|
|
163
163
|
options: { executionMode: "autonomous", ...model },
|
|
164
164
|
currentUser,
|
|
165
|
-
currentSession: import_base.CurrentSession.fromJSON({ location: { timezone:
|
|
165
|
+
currentSession: import_base.CurrentSession.fromJSON({ location: { timezone: currentUser.preferences?.timezone?.ianaIdentifier } }),
|
|
166
166
|
serviceToken: process.env.INTERNAL_MCP_TOKEN ? import_secret_manager.secretManager.get("INTERNAL_MCP_TOKEN") : void 0
|
|
167
167
|
});
|
|
168
168
|
const chat = result.data;
|
|
@@ -139,7 +139,7 @@ class AutomationRunner {
|
|
|
139
139
|
input: { message: buildMessage(automation.instructions, run.payload), chatId },
|
|
140
140
|
options: { executionMode: "autonomous", ...model },
|
|
141
141
|
currentUser,
|
|
142
|
-
currentSession: CurrentSession.fromJSON({ location: { timezone:
|
|
142
|
+
currentSession: CurrentSession.fromJSON({ location: { timezone: currentUser.preferences?.timezone?.ianaIdentifier } }),
|
|
143
143
|
serviceToken: process.env.INTERNAL_MCP_TOKEN ? secretManager.get("INTERNAL_MCP_TOKEN") : void 0
|
|
144
144
|
});
|
|
145
145
|
const chat = result.data;
|