openloops 0.4.3 → 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 +32 -0
- package/dist/clients/llm/llm-client.js +8 -5
- package/dist/clients/llm/llm-client.mjs +8 -5
- package/dist/clients/llm/llm-error.js +1 -1
- package/dist/clients/llm/llm-error.mjs +1 -1
- package/dist/clients/llm/sampling-rules.d.ts +1 -0
- package/dist/clients/llm/sampling-rules.js +33 -0
- package/dist/clients/llm/sampling-rules.mjs +10 -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,38 @@
|
|
|
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
|
+
|
|
30
|
+
## 0.4.4
|
|
31
|
+
|
|
32
|
+
### Fixed
|
|
33
|
+
|
|
34
|
+
- Claude models on Amazon Bedrock failed with `temperature may only be set to 1 when thinking is enabled`. The temperature is no longer sent to them when thinking is on. The rules live in `src/clients/llm/sampling-rules.ts`, one entry per provider and model, so another model that rejects the temperature is one more line.
|
|
35
|
+
- A provider error saying the temperature "may only" take some value is now seen as a rejected sampling parameter, so the existing retry without temperature also covers it.
|
|
36
|
+
|
|
5
37
|
## 0.4.3
|
|
6
38
|
|
|
7
39
|
### Added
|
|
@@ -39,6 +39,7 @@ var import_llmcalls = require("../../services/llmcalls/index.js");
|
|
|
39
39
|
var import_pi_ai_adapter = require("./pi-ai-adapter");
|
|
40
40
|
var import_number_setting = require("../../common/utils/number-setting.js");
|
|
41
41
|
var import_model_resolution = require("./model-resolution");
|
|
42
|
+
var import_sampling_rules = require("./sampling-rules");
|
|
42
43
|
var import_llm_error = require("./llm-error");
|
|
43
44
|
const DEFAULT_TEMPERATURE = 0.7;
|
|
44
45
|
const MAX_RETRIES = 4;
|
|
@@ -99,15 +100,16 @@ class LLMClient {
|
|
|
99
100
|
const resolvedChoice = toolChoice === "required" ? REQUIRED_TOOL_CHOICE[model.api] : toolChoice;
|
|
100
101
|
if (toolChoice === "required" && !resolvedChoice) throw new Error(`[LLM Client] forced tool choice is not supported for api ${model.api}`);
|
|
101
102
|
const context = (0, import_pi_ai_adapter.toPiContext)({ messages, tools }, model);
|
|
103
|
+
const reasoning = thinking && thinking !== "off" ? thinking : void 0;
|
|
104
|
+
const ruleOmitsTemperature = (0, import_sampling_rules.omitsTemperature)(provider, modelName, Boolean(reasoning));
|
|
102
105
|
const send = (omitTemperature) => this.withDeadline(thinking === "high" ? this.callTimeoutMs * HIGH_THINKING_TIMEOUT_FACTOR : this.callTimeoutMs, (signal) => {
|
|
103
|
-
const options = { ...omitTemperature ? {} : { temperature }, maxRetries: MAX_RETRIES, signal, ...sessionId ? { sessionId } : {} };
|
|
104
|
-
const reasoning = thinking && thinking !== "off" ? thinking : void 0;
|
|
106
|
+
const options = { ...omitTemperature || ruleOmitsTemperature ? {} : { temperature }, maxRetries: MAX_RETRIES, signal, ...sessionId ? { sessionId } : {} };
|
|
105
107
|
return resolvedChoice || thinking ? models.completeSimple(model, context, { ...options, ...resolvedChoice ? { toolChoice: resolvedChoice } : {}, ...reasoning ? { reasoning } : {} }) : models.complete(model, context, options);
|
|
106
108
|
});
|
|
107
109
|
const raw = await this.traced(
|
|
108
110
|
{ origin, sessionId, answerId, currentUser, modelName, provider, temperature },
|
|
109
111
|
context,
|
|
110
|
-
() => this.withoutRejectedTemperature(
|
|
112
|
+
() => this.withoutRejectedTemperature({ provider, model: modelName, thinking: Boolean(reasoning) }, send)
|
|
111
113
|
);
|
|
112
114
|
if (raw.stopReason === "error" || raw.stopReason === "aborted") {
|
|
113
115
|
throw import_llm_error.LLMError.fromProvider(raw.errorMessage ?? `request ${raw.stopReason}`);
|
|
@@ -155,12 +157,13 @@ class LLMClient {
|
|
|
155
157
|
clearTimeout(timer);
|
|
156
158
|
}
|
|
157
159
|
}
|
|
158
|
-
async withoutRejectedTemperature(
|
|
160
|
+
async withoutRejectedTemperature(call, send) {
|
|
161
|
+
const key = `${call.provider}/${call.model}`;
|
|
159
162
|
const known = LLMClient.modelsWithoutTemperature.has(key);
|
|
160
163
|
const response = await this.waitingOnRateLimit(() => send(known));
|
|
161
164
|
if (known || response.stopReason !== "error" || !(0, import_llm_error.rejectsSamplingParameter)(response.errorMessage)) return response;
|
|
162
165
|
LLMClient.modelsWithoutTemperature.add(key);
|
|
163
|
-
this.logger.warn({
|
|
166
|
+
this.logger.warn({ ...call, error: response.errorMessage }, "[LLM Service] The model rejected the temperature setting, retrying without it. Add a rule in sampling-rules.ts to avoid this retry");
|
|
164
167
|
return this.waitingOnRateLimit(() => send(true));
|
|
165
168
|
}
|
|
166
169
|
async waitingOnRateLimit(run) {
|
|
@@ -7,6 +7,7 @@ import { createLLMCall } from "../../services/llmcalls/index.js";
|
|
|
7
7
|
import { toPiContext, fromPiMessage } from "./pi-ai-adapter";
|
|
8
8
|
import { numberSetting } from "../../common/utils/number-setting.js";
|
|
9
9
|
import { resolveModel, toModelRef } from "./model-resolution";
|
|
10
|
+
import { omitsTemperature } from "./sampling-rules";
|
|
10
11
|
import { LLMError, retryDelayMs, rejectsSamplingParameter } from "./llm-error";
|
|
11
12
|
const DEFAULT_TEMPERATURE = 0.7;
|
|
12
13
|
const MAX_RETRIES = 4;
|
|
@@ -67,15 +68,16 @@ class LLMClient {
|
|
|
67
68
|
const resolvedChoice = toolChoice === "required" ? REQUIRED_TOOL_CHOICE[model.api] : toolChoice;
|
|
68
69
|
if (toolChoice === "required" && !resolvedChoice) throw new Error(`[LLM Client] forced tool choice is not supported for api ${model.api}`);
|
|
69
70
|
const context = toPiContext({ messages, tools }, model);
|
|
71
|
+
const reasoning = thinking && thinking !== "off" ? thinking : void 0;
|
|
72
|
+
const ruleOmitsTemperature = omitsTemperature(provider, modelName, Boolean(reasoning));
|
|
70
73
|
const send = (omitTemperature) => this.withDeadline(thinking === "high" ? this.callTimeoutMs * HIGH_THINKING_TIMEOUT_FACTOR : this.callTimeoutMs, (signal) => {
|
|
71
|
-
const options = { ...omitTemperature ? {} : { temperature }, maxRetries: MAX_RETRIES, signal, ...sessionId ? { sessionId } : {} };
|
|
72
|
-
const reasoning = thinking && thinking !== "off" ? thinking : void 0;
|
|
74
|
+
const options = { ...omitTemperature || ruleOmitsTemperature ? {} : { temperature }, maxRetries: MAX_RETRIES, signal, ...sessionId ? { sessionId } : {} };
|
|
73
75
|
return resolvedChoice || thinking ? models.completeSimple(model, context, { ...options, ...resolvedChoice ? { toolChoice: resolvedChoice } : {}, ...reasoning ? { reasoning } : {} }) : models.complete(model, context, options);
|
|
74
76
|
});
|
|
75
77
|
const raw = await this.traced(
|
|
76
78
|
{ origin, sessionId, answerId, currentUser, modelName, provider, temperature },
|
|
77
79
|
context,
|
|
78
|
-
() => this.withoutRejectedTemperature(
|
|
80
|
+
() => this.withoutRejectedTemperature({ provider, model: modelName, thinking: Boolean(reasoning) }, send)
|
|
79
81
|
);
|
|
80
82
|
if (raw.stopReason === "error" || raw.stopReason === "aborted") {
|
|
81
83
|
throw LLMError.fromProvider(raw.errorMessage ?? `request ${raw.stopReason}`);
|
|
@@ -123,12 +125,13 @@ class LLMClient {
|
|
|
123
125
|
clearTimeout(timer);
|
|
124
126
|
}
|
|
125
127
|
}
|
|
126
|
-
async withoutRejectedTemperature(
|
|
128
|
+
async withoutRejectedTemperature(call, send) {
|
|
129
|
+
const key = `${call.provider}/${call.model}`;
|
|
127
130
|
const known = LLMClient.modelsWithoutTemperature.has(key);
|
|
128
131
|
const response = await this.waitingOnRateLimit(() => send(known));
|
|
129
132
|
if (known || response.stopReason !== "error" || !rejectsSamplingParameter(response.errorMessage)) return response;
|
|
130
133
|
LLMClient.modelsWithoutTemperature.add(key);
|
|
131
|
-
this.logger.warn({
|
|
134
|
+
this.logger.warn({ ...call, error: response.errorMessage }, "[LLM Service] The model rejected the temperature setting, retrying without it. Add a rule in sampling-rules.ts to avoid this retry");
|
|
132
135
|
return this.waitingOnRateLimit(() => send(true));
|
|
133
136
|
}
|
|
134
137
|
async waitingOnRateLimit(run) {
|
|
@@ -41,7 +41,7 @@ const RETRYABLE = {
|
|
|
41
41
|
};
|
|
42
42
|
const MAX_WAIT_MS = 6e4;
|
|
43
43
|
const SAMPLING_PARAMETER = /\b(?:temperature|top_p|top_k)\b/i;
|
|
44
|
-
const REJECTION = /unsupported|not supported|does not support|doesn't support|deprecated|not allowed|only the default|no longer|removed|cannot be|can't be|must be|invalid/i;
|
|
44
|
+
const REJECTION = /unsupported|not supported|does not support|doesn't support|deprecated|not allowed|only the default|may only|no longer|removed|cannot be|can't be|must be|invalid/i;
|
|
45
45
|
class LLMError extends Error {
|
|
46
46
|
code;
|
|
47
47
|
isRetryable;
|
|
@@ -17,7 +17,7 @@ const RETRYABLE = {
|
|
|
17
17
|
};
|
|
18
18
|
const MAX_WAIT_MS = 6e4;
|
|
19
19
|
const SAMPLING_PARAMETER = /\b(?:temperature|top_p|top_k)\b/i;
|
|
20
|
-
const REJECTION = /unsupported|not supported|does not support|doesn't support|deprecated|not allowed|only the default|no longer|removed|cannot be|can't be|must be|invalid/i;
|
|
20
|
+
const REJECTION = /unsupported|not supported|does not support|doesn't support|deprecated|not allowed|only the default|may only|no longer|removed|cannot be|can't be|must be|invalid/i;
|
|
21
21
|
class LLMError extends Error {
|
|
22
22
|
code;
|
|
23
23
|
isRetryable;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function omitsTemperature(provider: string, model: string, thinking: boolean): boolean;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var sampling_rules_exports = {};
|
|
20
|
+
__export(sampling_rules_exports, {
|
|
21
|
+
omitsTemperature: () => omitsTemperature
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(sampling_rules_exports);
|
|
24
|
+
const NO_TEMPERATURE = [
|
|
25
|
+
{ provider: "amazon-bedrock", model: /claude/, withThinking: true }
|
|
26
|
+
];
|
|
27
|
+
function omitsTemperature(provider, model, thinking) {
|
|
28
|
+
return NO_TEMPERATURE.some((rule) => rule.provider === provider && (!rule.model || rule.model.test(model)) && (!rule.withThinking || thinking));
|
|
29
|
+
}
|
|
30
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
31
|
+
0 && (module.exports = {
|
|
32
|
+
omitsTemperature
|
|
33
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import "../../chunk-CF3WOMB3.mjs";
|
|
2
|
+
const NO_TEMPERATURE = [
|
|
3
|
+
{ provider: "amazon-bedrock", model: /claude/, withThinking: true }
|
|
4
|
+
];
|
|
5
|
+
function omitsTemperature(provider, model, thinking) {
|
|
6
|
+
return NO_TEMPERATURE.some((rule) => rule.provider === provider && (!rule.model || rule.model.test(model)) && (!rule.withThinking || thinking));
|
|
7
|
+
}
|
|
8
|
+
export {
|
|
9
|
+
omitsTemperature
|
|
10
|
+
};
|
|
@@ -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;
|