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
|
@@ -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;
|
|
@@ -61,12 +61,12 @@ let ClaimDueAutomationsUseCase = class extends import_base.BaseUseCase {
|
|
|
61
61
|
const skipped = [];
|
|
62
62
|
for (const candidate of due) {
|
|
63
63
|
const id = String(candidate._id);
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
const schedule = candidate.triggers?.schedule;
|
|
65
|
+
if (!schedule) continue;
|
|
66
|
+
const scheduledFor = schedule.nextRunAt ? new Date(schedule.nextRunAt) : void 0;
|
|
67
67
|
let nextRunAt = null;
|
|
68
68
|
try {
|
|
69
|
-
nextRunAt = (0, import_next_run.computeNextRun)(
|
|
69
|
+
nextRunAt = (0, import_next_run.computeNextRun)(schedule.recurrence, schedule.timezone, now);
|
|
70
70
|
} catch (error) {
|
|
71
71
|
this.log.error({ error: error?.message ?? error, automationId: id }, "[AUTOMATIONS] The recurrence of an automation cannot be calculated, skipping it");
|
|
72
72
|
continue;
|
|
@@ -33,12 +33,12 @@ let ClaimDueAutomationsUseCase = class extends BaseUseCase {
|
|
|
33
33
|
const skipped = [];
|
|
34
34
|
for (const candidate of due) {
|
|
35
35
|
const id = String(candidate._id);
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
const schedule = candidate.triggers?.schedule;
|
|
37
|
+
if (!schedule) continue;
|
|
38
|
+
const scheduledFor = schedule.nextRunAt ? new Date(schedule.nextRunAt) : void 0;
|
|
39
39
|
let nextRunAt = null;
|
|
40
40
|
try {
|
|
41
|
-
nextRunAt = computeNextRun(
|
|
41
|
+
nextRunAt = computeNextRun(schedule.recurrence, schedule.timezone, now);
|
|
42
42
|
} catch (error) {
|
|
43
43
|
this.log.error({ error: error?.message ?? error, automationId: id }, "[AUTOMATIONS] The recurrence of an automation cannot be calculated, skipping it");
|
|
44
44
|
continue;
|
|
@@ -64,7 +64,7 @@ let ClaimQueuedAutomationRunsUseCase = class extends import_base.BaseUseCase {
|
|
|
64
64
|
if (seen.has(automationId)) continue;
|
|
65
65
|
seen.add(automationId);
|
|
66
66
|
const existing = await this.automationRepository.findById(automationId);
|
|
67
|
-
const reason = !existing ? "The automation no longer exists" : !existing.enabled ? "The automation is turned off" : void 0;
|
|
67
|
+
const reason = !existing ? "The automation no longer exists" : !existing.triggers?.event?.enabled ? "The event trigger of the automation is turned off" : void 0;
|
|
68
68
|
if (reason) {
|
|
69
69
|
const settled = await this.automationRunRepository.update(import_entities.AutomationRun.fromJSON({ ...run, status: "skipped", finishedAt: now, skipReason: reason }));
|
|
70
70
|
if (existing && settled) skipped.push({ automation: existing, run: settled });
|
|
@@ -36,7 +36,7 @@ let ClaimQueuedAutomationRunsUseCase = class extends BaseUseCase {
|
|
|
36
36
|
if (seen.has(automationId)) continue;
|
|
37
37
|
seen.add(automationId);
|
|
38
38
|
const existing = await this.automationRepository.findById(automationId);
|
|
39
|
-
const reason = !existing ? "The automation no longer exists" : !existing.enabled ? "The automation is turned off" : void 0;
|
|
39
|
+
const reason = !existing ? "The automation no longer exists" : !existing.triggers?.event?.enabled ? "The event trigger of the automation is turned off" : void 0;
|
|
40
40
|
if (reason) {
|
|
41
41
|
const settled = await this.automationRunRepository.update(AutomationRun.fromJSON({ ...run, status: "skipped", finishedAt: now, skipReason: reason }));
|
|
42
42
|
if (existing && settled) skipped.push({ automation: existing, run: settled });
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { BaseUseCase, BaseUseCaseParams, BaseUseCaseOutput } from '../../../common/base/base.use-case.js';
|
|
2
|
-
import { AutomationProps } from '../entities';
|
|
2
|
+
import { AutomationPayload, AutomationProps } from '../entities';
|
|
3
3
|
import { AutomationRepository } from '../repositories';
|
|
4
4
|
export interface Params extends BaseUseCaseParams {
|
|
5
|
-
payload?:
|
|
5
|
+
payload?: AutomationPayload;
|
|
6
6
|
}
|
|
7
7
|
export interface Output extends BaseUseCaseOutput {
|
|
8
8
|
data: AutomationProps;
|
|
@@ -49,15 +49,16 @@ let CreateAutomationUseCase = class extends import_base.BaseUseCase {
|
|
|
49
49
|
const automation = import_entities.Automation.fromJSON({
|
|
50
50
|
name: fields.name,
|
|
51
51
|
instructions: fields.instructions,
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
triggers: fields.triggers,
|
|
53
|
+
eventTokenVersion: 1,
|
|
54
54
|
modelProvider: fields.modelProvider ?? void 0,
|
|
55
55
|
modelName: fields.modelName ?? void 0
|
|
56
56
|
});
|
|
57
57
|
automation.setCreatedBy(currentUser);
|
|
58
58
|
automation.setOwner(currentUser);
|
|
59
59
|
automation.scheduleNextRun();
|
|
60
|
-
|
|
60
|
+
const schedule = automation.triggers?.schedule;
|
|
61
|
+
if (schedule?.enabled && !schedule.nextRunAt) throw new Error("The date of the recurrence has already passed");
|
|
61
62
|
await this.automationRepository?.create(automation);
|
|
62
63
|
return { data: automation.toJSON() };
|
|
63
64
|
}
|
|
@@ -21,15 +21,16 @@ let CreateAutomationUseCase = class extends BaseUseCase {
|
|
|
21
21
|
const automation = Automation.fromJSON({
|
|
22
22
|
name: fields.name,
|
|
23
23
|
instructions: fields.instructions,
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
triggers: fields.triggers,
|
|
25
|
+
eventTokenVersion: 1,
|
|
26
26
|
modelProvider: fields.modelProvider ?? void 0,
|
|
27
27
|
modelName: fields.modelName ?? void 0
|
|
28
28
|
});
|
|
29
29
|
automation.setCreatedBy(currentUser);
|
|
30
30
|
automation.setOwner(currentUser);
|
|
31
31
|
automation.scheduleNextRun();
|
|
32
|
-
|
|
32
|
+
const schedule = automation.triggers?.schedule;
|
|
33
|
+
if (schedule?.enabled && !schedule.nextRunAt) throw new Error("The date of the recurrence has already passed");
|
|
33
34
|
await this.automationRepository?.create(automation);
|
|
34
35
|
return { data: automation.toJSON() };
|
|
35
36
|
}
|
|
@@ -50,7 +50,7 @@ let DeleteAutomationUseCase = class extends import_base.BaseUseCase {
|
|
|
50
50
|
}
|
|
51
51
|
const automation = import_entities.Automation.fromJSON({ _id: id });
|
|
52
52
|
automation.setDeletedBy(currentUser);
|
|
53
|
-
automation.unset(["nextRunAt"]);
|
|
53
|
+
automation.unset(["triggers.schedule.nextRunAt"]);
|
|
54
54
|
const result = await this.automationRepository?.update(automation);
|
|
55
55
|
return { data: result };
|
|
56
56
|
}
|
|
@@ -22,7 +22,7 @@ let DeleteAutomationUseCase = class extends BaseUseCase {
|
|
|
22
22
|
}
|
|
23
23
|
const automation = Automation.fromJSON({ _id: id });
|
|
24
24
|
automation.setDeletedBy(currentUser);
|
|
25
|
-
automation.unset(["nextRunAt"]);
|
|
25
|
+
automation.unset(["triggers.schedule.nextRunAt"]);
|
|
26
26
|
const result = await this.automationRepository?.update(automation);
|
|
27
27
|
return { data: result };
|
|
28
28
|
}
|
|
@@ -45,8 +45,8 @@ let IssueAutomationEventTokenUseCase = class extends import_base.BaseUseCase {
|
|
|
45
45
|
const automation = await this.automationRepository?.findById(id);
|
|
46
46
|
if (!automation) throw new Error("Automation not found");
|
|
47
47
|
if (!currentUser?.isAdmin() && String(automation.ownerId) !== currentUser?.userId) this.throwError403();
|
|
48
|
-
if (automation.
|
|
49
|
-
return { data: (0, import_event_token.signEventToken)({ automationId: id, tokenVersion: automation.
|
|
48
|
+
if (!automation.triggers?.event) throw new Error("Only an automation with an event trigger has a token");
|
|
49
|
+
return { data: (0, import_event_token.signEventToken)({ automationId: id, tokenVersion: automation.eventTokenVersion, expiresIn }) };
|
|
50
50
|
}
|
|
51
51
|
};
|
|
52
52
|
IssueAutomationEventTokenUseCase = __decorateClass([
|
|
@@ -17,8 +17,8 @@ let IssueAutomationEventTokenUseCase = class extends BaseUseCase {
|
|
|
17
17
|
const automation = await this.automationRepository?.findById(id);
|
|
18
18
|
if (!automation) throw new Error("Automation not found");
|
|
19
19
|
if (!currentUser?.isAdmin() && String(automation.ownerId) !== currentUser?.userId) this.throwError403();
|
|
20
|
-
if (automation.
|
|
21
|
-
return { data: signEventToken({ automationId: id, tokenVersion: automation.
|
|
20
|
+
if (!automation.triggers?.event) throw new Error("Only an automation with an event trigger has a token");
|
|
21
|
+
return { data: signEventToken({ automationId: id, tokenVersion: automation.eventTokenVersion, expiresIn }) };
|
|
22
22
|
}
|
|
23
23
|
};
|
|
24
24
|
IssueAutomationEventTokenUseCase = __decorateClass([
|
|
@@ -59,8 +59,9 @@ let ReceiveAutomationEventUseCase = class extends import_base.BaseUseCase {
|
|
|
59
59
|
if (claims.sub !== automationId) throw new import_automation_event_error.AutomationEventError("invalid");
|
|
60
60
|
const automation = await this.automationRepository.findById(automationId);
|
|
61
61
|
if (!automation) throw new import_automation_event_error.AutomationEventError("not_found");
|
|
62
|
-
|
|
63
|
-
if (!automation.
|
|
62
|
+
const event = automation.triggers?.event;
|
|
63
|
+
if (!event || automation.eventTokenVersion !== claims.v) throw new import_automation_event_error.AutomationEventError("invalid");
|
|
64
|
+
if (!event.enabled) throw new import_automation_event_error.AutomationEventError("disabled");
|
|
64
65
|
const { data: { revoked } } = await (0, import_authentication.isTokenRevoked)({ jti: claims.jti });
|
|
65
66
|
if (revoked) throw new import_automation_event_error.AutomationEventError("revoked");
|
|
66
67
|
const payload = serialize(params.payload);
|
|
@@ -31,8 +31,9 @@ let ReceiveAutomationEventUseCase = class extends BaseUseCase {
|
|
|
31
31
|
if (claims.sub !== automationId) throw new AutomationEventError("invalid");
|
|
32
32
|
const automation = await this.automationRepository.findById(automationId);
|
|
33
33
|
if (!automation) throw new AutomationEventError("not_found");
|
|
34
|
-
|
|
35
|
-
if (!automation.
|
|
34
|
+
const event = automation.triggers?.event;
|
|
35
|
+
if (!event || automation.eventTokenVersion !== claims.v) throw new AutomationEventError("invalid");
|
|
36
|
+
if (!event.enabled) throw new AutomationEventError("disabled");
|
|
36
37
|
const { data: { revoked } } = await isTokenRevoked({ jti: claims.jti });
|
|
37
38
|
if (revoked) throw new AutomationEventError("revoked");
|
|
38
39
|
const payload = serialize(params.payload);
|
|
@@ -45,8 +45,8 @@ let RotateAutomationEventTokensUseCase = class extends import_base.BaseUseCase {
|
|
|
45
45
|
const existing = await this.automationRepository?.findById(id);
|
|
46
46
|
if (!existing) throw new Error("Automation not found");
|
|
47
47
|
if (!currentUser?.isAdmin() && String(existing.ownerId) !== currentUser?.userId) this.throwError403();
|
|
48
|
-
if (existing.
|
|
49
|
-
const automation = import_entities.Automation.fromJSON({ ...existing,
|
|
48
|
+
if (!existing.triggers?.event) throw new Error("Only an automation with an event trigger has tokens");
|
|
49
|
+
const automation = import_entities.Automation.fromJSON({ ...existing, eventTokenVersion: existing.eventTokenVersion + 1 });
|
|
50
50
|
automation.setUpdatedBy(currentUser);
|
|
51
51
|
const result = await this.automationRepository?.update(automation, { expectedVersion: existing.__v });
|
|
52
52
|
if (!result) throw new Error("Automation not found or update failed");
|
|
@@ -17,8 +17,8 @@ let RotateAutomationEventTokensUseCase = class extends BaseUseCase {
|
|
|
17
17
|
const existing = await this.automationRepository?.findById(id);
|
|
18
18
|
if (!existing) throw new Error("Automation not found");
|
|
19
19
|
if (!currentUser?.isAdmin() && String(existing.ownerId) !== currentUser?.userId) this.throwError403();
|
|
20
|
-
if (existing.
|
|
21
|
-
const automation = Automation.fromJSON({ ...existing,
|
|
20
|
+
if (!existing.triggers?.event) throw new Error("Only an automation with an event trigger has tokens");
|
|
21
|
+
const automation = Automation.fromJSON({ ...existing, eventTokenVersion: existing.eventTokenVersion + 1 });
|
|
22
22
|
automation.setUpdatedBy(currentUser);
|
|
23
23
|
const result = await this.automationRepository?.update(automation, { expectedVersion: existing.__v });
|
|
24
24
|
if (!result) throw new Error("Automation not found or update failed");
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BaseUseCase, BaseUseCaseParams, BaseUseCaseOutput } from '../../../common/base/base.use-case.js';
|
|
2
|
-
import { AutomationProps } from '../entities';
|
|
2
|
+
import { AutomationPayload, AutomationProps } from '../entities';
|
|
3
3
|
import { AutomationRepository } from '../repositories';
|
|
4
4
|
export interface Params extends BaseUseCaseParams {
|
|
5
5
|
id: string;
|
|
6
|
-
payload?:
|
|
6
|
+
payload?: AutomationPayload;
|
|
7
7
|
}
|
|
8
8
|
export interface Output extends BaseUseCaseOutput {
|
|
9
9
|
data: AutomationProps;
|
|
@@ -48,7 +48,7 @@ let UpdateAutomationUseCase = class extends import_base.BaseUseCase {
|
|
|
48
48
|
const existing = await this.automationRepository?.findById(id);
|
|
49
49
|
if (!existing) throw new Error("Automation not found or update failed");
|
|
50
50
|
if (payload.__v !== void 0 && payload.__v !== existing.__v) throw new import_mongo_db.VersionConflictError();
|
|
51
|
-
const changes = (0, import_automation_fields.readAutomationFields)(payload, { complete: false,
|
|
51
|
+
const changes = (0, import_automation_fields.readAutomationFields)(payload, { complete: false, triggers: existing.triggers });
|
|
52
52
|
const { modelProvider, modelName, ...rest } = changes;
|
|
53
53
|
const provider = modelProvider === void 0 ? existing.modelProvider : modelProvider;
|
|
54
54
|
const name = modelName === void 0 ? existing.modelName : modelName;
|
|
@@ -62,11 +62,14 @@ let UpdateAutomationUseCase = class extends import_base.BaseUseCase {
|
|
|
62
62
|
});
|
|
63
63
|
automation.setUpdatedBy(currentUser);
|
|
64
64
|
if (!provider) automation.unset(["modelProvider", "modelName"]);
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
if (
|
|
65
|
+
const removed = ["schedule", "event"].filter((key) => existing.triggers?.[key] && !automation.triggers?.[key]);
|
|
66
|
+
if (removed.length) automation.toUnset = Object.fromEntries(removed.map((key) => [`triggers.${key}`, ""]));
|
|
67
|
+
if (removed.includes("event")) automation.eventTokenVersion = existing.eventTokenVersion + 1;
|
|
68
|
+
const schedule = automation.triggers?.schedule;
|
|
69
|
+
const scheduleChanged = payload.triggers?.schedule !== void 0;
|
|
70
|
+
if (schedule && (scheduleChanged || schedule.enabled && !schedule.nextRunAt)) {
|
|
68
71
|
automation.scheduleNextRun();
|
|
69
|
-
if (
|
|
72
|
+
if (schedule.enabled && !schedule.nextRunAt) throw new Error("The date of the recurrence has already passed");
|
|
70
73
|
}
|
|
71
74
|
const result = await this.automationRepository?.update(automation, { expectedVersion: existing.__v });
|
|
72
75
|
if (!result) throw new Error("Automation not found or update failed");
|
|
@@ -20,7 +20,7 @@ let UpdateAutomationUseCase = class extends BaseUseCase {
|
|
|
20
20
|
const existing = await this.automationRepository?.findById(id);
|
|
21
21
|
if (!existing) throw new Error("Automation not found or update failed");
|
|
22
22
|
if (payload.__v !== void 0 && payload.__v !== existing.__v) throw new VersionConflictError();
|
|
23
|
-
const changes = readAutomationFields(payload, { complete: false,
|
|
23
|
+
const changes = readAutomationFields(payload, { complete: false, triggers: existing.triggers });
|
|
24
24
|
const { modelProvider, modelName, ...rest } = changes;
|
|
25
25
|
const provider = modelProvider === void 0 ? existing.modelProvider : modelProvider;
|
|
26
26
|
const name = modelName === void 0 ? existing.modelName : modelName;
|
|
@@ -34,11 +34,14 @@ let UpdateAutomationUseCase = class extends BaseUseCase {
|
|
|
34
34
|
});
|
|
35
35
|
automation.setUpdatedBy(currentUser);
|
|
36
36
|
if (!provider) automation.unset(["modelProvider", "modelName"]);
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
if (
|
|
37
|
+
const removed = ["schedule", "event"].filter((key) => existing.triggers?.[key] && !automation.triggers?.[key]);
|
|
38
|
+
if (removed.length) automation.toUnset = Object.fromEntries(removed.map((key) => [`triggers.${key}`, ""]));
|
|
39
|
+
if (removed.includes("event")) automation.eventTokenVersion = existing.eventTokenVersion + 1;
|
|
40
|
+
const schedule = automation.triggers?.schedule;
|
|
41
|
+
const scheduleChanged = payload.triggers?.schedule !== void 0;
|
|
42
|
+
if (schedule && (scheduleChanged || schedule.enabled && !schedule.nextRunAt)) {
|
|
40
43
|
automation.scheduleNextRun();
|
|
41
|
-
if (
|
|
44
|
+
if (schedule.enabled && !schedule.nextRunAt) throw new Error("The date of the recurrence has already passed");
|
|
42
45
|
}
|
|
43
46
|
const result = await this.automationRepository?.update(automation, { expectedVersion: existing.__v });
|
|
44
47
|
if (!result) throw new Error("Automation not found or update failed");
|
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AutomationTriggers } from '../entities/trigger';
|
|
2
2
|
export interface AutomationFields {
|
|
3
3
|
name?: string;
|
|
4
4
|
instructions?: string;
|
|
5
|
-
|
|
6
|
-
trigger?: AutomationTrigger;
|
|
5
|
+
triggers?: AutomationTriggers;
|
|
7
6
|
modelProvider?: string | null;
|
|
8
7
|
modelName?: string | null;
|
|
9
8
|
}
|
|
10
9
|
export declare function readAutomationFields(payload: Record<string, any> | undefined, options: {
|
|
11
10
|
complete: boolean;
|
|
12
|
-
|
|
11
|
+
triggers?: AutomationTriggers;
|
|
13
12
|
}): AutomationFields;
|
|
14
13
|
export declare function assertModel(provider?: string | null, name?: string | null): void;
|
|
@@ -40,11 +40,9 @@ function readAutomationFields(payload, options) {
|
|
|
40
40
|
const has = (key) => input[key] !== void 0;
|
|
41
41
|
if (options.complete || has("name")) fields.name = text(input.name, "name", MAX_NAME_CHARS);
|
|
42
42
|
if (options.complete || has("instructions")) fields.instructions = text(input.instructions, "instructions", MAX_INSTRUCTIONS_CHARS);
|
|
43
|
-
if (
|
|
44
|
-
if (has("enabled"))
|
|
45
|
-
|
|
46
|
-
fields.enabled = input.enabled;
|
|
47
|
-
}
|
|
43
|
+
if (has("trigger")) throw new Error("The field trigger was replaced by triggers");
|
|
44
|
+
if (has("enabled")) throw new Error("The field enabled belongs to each trigger, send it inside triggers.schedule or triggers.event");
|
|
45
|
+
if (options.complete || has("triggers")) fields.triggers = (0, import_trigger.readTriggers)(input.triggers, options.triggers);
|
|
48
46
|
if (has("modelProvider")) fields.modelProvider = optionalText(input.modelProvider, "model provider");
|
|
49
47
|
if (has("modelName")) fields.modelName = optionalText(input.modelName, "model name");
|
|
50
48
|
return fields;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "../../../chunk-CF3WOMB3.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { readTriggers } from "../entities/trigger";
|
|
3
3
|
const MAX_NAME_CHARS = 200;
|
|
4
4
|
const MAX_INSTRUCTIONS_CHARS = 2e4;
|
|
5
5
|
const text = (value, label, max) => {
|
|
@@ -17,11 +17,9 @@ function readAutomationFields(payload, options) {
|
|
|
17
17
|
const has = (key) => input[key] !== void 0;
|
|
18
18
|
if (options.complete || has("name")) fields.name = text(input.name, "name", MAX_NAME_CHARS);
|
|
19
19
|
if (options.complete || has("instructions")) fields.instructions = text(input.instructions, "instructions", MAX_INSTRUCTIONS_CHARS);
|
|
20
|
-
if (
|
|
21
|
-
if (has("enabled"))
|
|
22
|
-
|
|
23
|
-
fields.enabled = input.enabled;
|
|
24
|
-
}
|
|
20
|
+
if (has("trigger")) throw new Error("The field trigger was replaced by triggers");
|
|
21
|
+
if (has("enabled")) throw new Error("The field enabled belongs to each trigger, send it inside triggers.schedule or triggers.event");
|
|
22
|
+
if (options.complete || has("triggers")) fields.triggers = readTriggers(input.triggers, options.triggers);
|
|
25
23
|
if (has("modelProvider")) fields.modelProvider = optionalText(input.modelProvider, "model provider");
|
|
26
24
|
if (has("modelName")) fields.modelName = optionalText(input.modelName, "model name");
|
|
27
25
|
return fields;
|
package/docs/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Start with the [README](../README.md) for what `openloops` is and how to run it.
|
|
|
14
14
|
| --- | --- |
|
|
15
15
|
| [The `Agent`](agent.md) | Options, running a turn, the result and its statuses, confirmations, autonomous mode, hooks and interruption. |
|
|
16
16
|
| [Configuration](configuration.md) | Environment variables, choosing the model and what is recorded. |
|
|
17
|
-
| [Automations](automations.md) | Tasks that the agent runs by itself, on a schedule
|
|
17
|
+
| [Automations](automations.md) | Tasks that the agent runs by itself, on a schedule, when an event arrives or both: triggers, tokens, the queue, the history of runs and the runner that executes them. If you used `agentic-jobs`, read [the migration steps](automations.md#migrating-from-agentic-jobs). Updating to 0.5.0 breaks the shape of the triggers, read [the migration to 0.5.0](automations.md#migrating-to-050). |
|
|
18
18
|
| [Chats](chats.md) | The chat record, its messages and their `source`, listing, reading and deleting chats. |
|
|
19
19
|
|
|
20
20
|
## Shape how the agent works
|
package/docs/agent.md
CHANGED
|
@@ -15,7 +15,7 @@ const agent = new Agent({
|
|
|
15
15
|
tools: [],
|
|
16
16
|
guards: [],
|
|
17
17
|
identity: {
|
|
18
|
-
name: '
|
|
18
|
+
name: 'Ava',
|
|
19
19
|
gender: 'Female',
|
|
20
20
|
role: 'Personal assistant and daily companion',
|
|
21
21
|
},
|
|
@@ -199,7 +199,7 @@ await agent.run({ input: { chatId, message }, currentUser })
|
|
|
199
199
|
stop()
|
|
200
200
|
```
|
|
201
201
|
|
|
202
|
-
The listener receives every change of the activity of the run while it is in progress. It gets no start or end marker: the run is over when `agent.run()` resolves. This is the only place where the real activity is available: since 0.3.0 it is not saved with the chat, and `state.currentActivity` is deprecated (see [chats](chats.md#statecurrentactivity-is-deprecated)). It does not receive a value that repeats the previous one. With `AgiLoop` the value is a short sentence written by the model in the language of the conversation, such as `
|
|
202
|
+
The listener receives every change of the activity of the run while it is in progress. It gets no start or end marker: the run is over when `agent.run()` resolves. This is the only place where the real activity is available: since 0.3.0 it is not saved with the chat, and `state.currentActivity` is deprecated (see [chats](chats.md#statecurrentactivity-is-deprecated)). It does not receive a value that repeats the previous one. With `AgiLoop` the value is a short sentence written by the model in the language of the conversation, such as `Searching your flight expenses for August`, shown from the answer that asked for the actions until the next answer. When the model gives none, the listener hears nothing and the client keeps what it shows. See [loops](loops.md#agiloop).
|
|
203
203
|
|
|
204
204
|
- The listener belongs to that `Agent` instance and to the chat of its current run. Nothing is shared between instances, so concurrent runs in the same process, even of different users, never hear each other. As always, create one `Agent` per run and do not share it between concurrent requests.
|
|
205
205
|
- It is called synchronously from the loop, so keep it fast and do not await inside it. An error thrown by a listener is logged and ignored, the run goes on and the other listeners still run.
|