openloops 0.1.38 → 0.1.39
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/README.md +21 -2
- package/dist/services/agentic-jobs/entities/agentic-job-run.entity.d.ts +42 -0
- package/dist/services/agentic-jobs/entities/agentic-job-run.entity.js +103 -0
- package/dist/services/agentic-jobs/entities/agentic-job-run.entity.mjs +80 -0
- package/dist/services/agentic-jobs/entities/agentic-job.entity.d.ts +39 -0
- package/dist/services/agentic-jobs/entities/agentic-job.entity.js +113 -0
- package/dist/services/agentic-jobs/entities/agentic-job.entity.mjs +90 -0
- package/dist/services/agentic-jobs/entities/index.d.ts +3 -0
- package/dist/services/agentic-jobs/entities/index.js +26 -0
- package/dist/services/agentic-jobs/entities/index.mjs +3 -0
- package/dist/services/agentic-jobs/entities/recurrence.d.ts +18 -0
- package/dist/services/agentic-jobs/entities/recurrence.js +84 -0
- package/dist/services/agentic-jobs/entities/recurrence.mjs +59 -0
- package/dist/services/agentic-jobs/index.d.ts +4 -0
- package/dist/services/agentic-jobs/index.js +28 -0
- package/dist/services/agentic-jobs/index.mjs +4 -0
- package/dist/services/agentic-jobs/module.d.ts +31 -0
- package/dist/services/agentic-jobs/module.js +132 -0
- package/dist/services/agentic-jobs/module.mjs +95 -0
- package/dist/services/agentic-jobs/repositories/agentic-job-run.repository.d.ts +11 -0
- package/dist/services/agentic-jobs/repositories/agentic-job-run.repository.js +71 -0
- package/dist/services/agentic-jobs/repositories/agentic-job-run.repository.mjs +42 -0
- package/dist/services/agentic-jobs/repositories/agentic-job.repository.d.ts +27 -0
- package/dist/services/agentic-jobs/repositories/agentic-job.repository.js +127 -0
- package/dist/services/agentic-jobs/repositories/agentic-job.repository.mjs +98 -0
- package/dist/services/agentic-jobs/repositories/index.d.ts +2 -0
- package/dist/services/agentic-jobs/repositories/index.js +24 -0
- package/dist/services/agentic-jobs/repositories/index.mjs +2 -0
- package/dist/services/agentic-jobs/runner/agentic-job-runner.d.ts +48 -0
- package/dist/services/agentic-jobs/runner/agentic-job-runner.js +181 -0
- package/dist/services/agentic-jobs/runner/agentic-job-runner.mjs +157 -0
- package/dist/services/agentic-jobs/use-cases/attach-agentic-job-run-chat.use-case.d.ts +15 -0
- package/dist/services/agentic-jobs/use-cases/attach-agentic-job-run-chat.use-case.js +58 -0
- package/dist/services/agentic-jobs/use-cases/attach-agentic-job-run-chat.use-case.mjs +29 -0
- package/dist/services/agentic-jobs/use-cases/claim-due-agentic-jobs.use-case.d.ts +29 -0
- package/dist/services/agentic-jobs/use-cases/claim-due-agentic-jobs.use-case.js +110 -0
- package/dist/services/agentic-jobs/use-cases/claim-due-agentic-jobs.use-case.mjs +81 -0
- package/dist/services/agentic-jobs/use-cases/create-agentic-job.use-case.d.ts +14 -0
- package/dist/services/agentic-jobs/use-cases/create-agentic-job.use-case.js +73 -0
- package/dist/services/agentic-jobs/use-cases/create-agentic-job.use-case.mjs +44 -0
- package/dist/services/agentic-jobs/use-cases/delete-agentic-job.use-case.d.ts +15 -0
- package/dist/services/agentic-jobs/use-cases/delete-agentic-job.use-case.js +65 -0
- package/dist/services/agentic-jobs/use-cases/delete-agentic-job.use-case.mjs +36 -0
- package/dist/services/agentic-jobs/use-cases/finish-agentic-job-run.use-case.d.ts +19 -0
- package/dist/services/agentic-jobs/use-cases/finish-agentic-job-run.use-case.js +75 -0
- package/dist/services/agentic-jobs/use-cases/finish-agentic-job-run.use-case.mjs +46 -0
- package/dist/services/agentic-jobs/use-cases/get-agentic-job-by-id.use-case.d.ts +14 -0
- package/dist/services/agentic-jobs/use-cases/get-agentic-job-by-id.use-case.js +55 -0
- package/dist/services/agentic-jobs/use-cases/get-agentic-job-by-id.use-case.mjs +26 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-job-runs-by-job.use-case.d.ts +16 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-job-runs-by-job.use-case.js +57 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-job-runs-by-job.use-case.mjs +28 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-job-runs-by-user.use-case.d.ts +16 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-job-runs-by-user.use-case.js +57 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-job-runs-by-user.use-case.mjs +28 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-jobs-by-user.use-case.d.ts +15 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-jobs-by-user.use-case.js +57 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-jobs-by-user.use-case.mjs +28 -0
- package/dist/services/agentic-jobs/use-cases/preview-agentic-job-runs.use-case.d.ts +15 -0
- package/dist/services/agentic-jobs/use-cases/preview-agentic-job-runs.use-case.js +52 -0
- package/dist/services/agentic-jobs/use-cases/preview-agentic-job-runs.use-case.mjs +23 -0
- package/dist/services/agentic-jobs/use-cases/renew-agentic-job-lease.use-case.d.ts +18 -0
- package/dist/services/agentic-jobs/use-cases/renew-agentic-job-lease.use-case.js +58 -0
- package/dist/services/agentic-jobs/use-cases/renew-agentic-job-lease.use-case.mjs +29 -0
- package/dist/services/agentic-jobs/use-cases/start-agentic-job-run.use-case.d.ts +21 -0
- package/dist/services/agentic-jobs/use-cases/start-agentic-job-run.use-case.js +67 -0
- package/dist/services/agentic-jobs/use-cases/start-agentic-job-run.use-case.mjs +38 -0
- package/dist/services/agentic-jobs/use-cases/sync-agentic-job-run.use-case.d.ts +20 -0
- package/dist/services/agentic-jobs/use-cases/sync-agentic-job-run.use-case.js +86 -0
- package/dist/services/agentic-jobs/use-cases/sync-agentic-job-run.use-case.mjs +56 -0
- package/dist/services/agentic-jobs/use-cases/sync-agentic-job-runs.use-case.d.ts +19 -0
- package/dist/services/agentic-jobs/use-cases/sync-agentic-job-runs.use-case.js +68 -0
- package/dist/services/agentic-jobs/use-cases/sync-agentic-job-runs.use-case.mjs +39 -0
- package/dist/services/agentic-jobs/use-cases/update-agentic-job.use-case.d.ts +15 -0
- package/dist/services/agentic-jobs/use-cases/update-agentic-job.use-case.js +83 -0
- package/dist/services/agentic-jobs/use-cases/update-agentic-job.use-case.mjs +54 -0
- package/dist/services/agentic-jobs/utils/job-fields.d.ts +14 -0
- package/dist/services/agentic-jobs/utils/job-fields.js +61 -0
- package/dist/services/agentic-jobs/utils/job-fields.mjs +37 -0
- package/dist/services/agentic-jobs/utils/next-run.d.ts +4 -0
- package/dist/services/agentic-jobs/utils/next-run.js +62 -0
- package/dist/services/agentic-jobs/utils/next-run.mjs +37 -0
- package/dist/services/agentic-jobs/utils/open-run.d.ts +12 -0
- package/dist/services/agentic-jobs/utils/open-run.js +47 -0
- package/dist/services/agentic-jobs/utils/open-run.mjs +24 -0
- package/dist/services/agentic-jobs/utils/settings.d.ts +2 -0
- package/dist/services/agentic-jobs/utils/settings.js +34 -0
- package/dist/services/agentic-jobs/utils/settings.mjs +10 -0
- package/docs/README.md +46 -0
- package/docs/agent.md +201 -0
- package/docs/agentic-jobs.md +207 -0
- package/docs/authentication.md +40 -0
- package/docs/chats.md +88 -0
- package/docs/configuration.md +69 -0
- package/docs/guardrails.md +71 -0
- package/docs/harness.md +81 -0
- package/docs/integration-guide.md +138 -0
- package/docs/llms.txt +35 -0
- package/docs/loops.md +147 -0
- package/docs/mcp-servers.md +148 -0
- package/docs/skills.md +104 -0
- package/docs/tools.md +90 -0
- package/docs/usage.md +86 -0
- package/docs/users.md +85 -0
- package/examples/agentic-jobs/create-agentic-job.ts +24 -0
- package/examples/agentic-jobs/list-agentic-job-runs.ts +28 -0
- package/examples/agentic-jobs/list-agentic-jobs.ts +25 -0
- package/examples/agentic-jobs/preview-agentic-job-runs.ts +16 -0
- package/examples/agentic-jobs/run-agentic-job-now.ts +19 -0
- package/examples/agentic-jobs/run-agentic-job-worker.ts +29 -0
- package/examples/agentic-jobs/update-agentic-job.ts +36 -0
- package/examples/authentication/login.ts +17 -0
- package/examples/chats/delete-chat.ts +32 -0
- package/examples/chats/get-chat-by-id.ts +32 -0
- package/examples/chats/list-autonomous-chats.ts +23 -0
- package/examples/chats/list-chats.ts +20 -0
- package/examples/guardrails/custom-guard.ts +11 -0
- package/examples/loops/custom-loop.ts +50 -0
- package/examples/mcps/create-mcp.ts +24 -0
- package/examples/mcps/decide-mcp.ts +40 -0
- package/examples/mcps/delete-mcp.ts +32 -0
- package/examples/mcps/get-mcp-by-id.ts +17 -0
- package/examples/mcps/list-all-mcps.ts +14 -0
- package/examples/mcps/list-user-mcps.ts +23 -0
- package/examples/mcps/update-mcp.ts +23 -0
- package/examples/mcps/verify-mcp.ts +17 -0
- package/examples/run-agent/interrupt-agent.ts +16 -0
- package/examples/run-agent/run-agent-autonomous.ts +34 -0
- package/examples/run-agent/run-agent-strict-mode.ts +30 -0
- package/examples/run-agent/run-agent-with-confirmation.ts +49 -0
- package/examples/run-agent/run-agent-with-guards.ts +33 -0
- package/examples/run-agent/run-agent-with-hooks.ts +55 -0
- package/examples/run-agent/run-agent-with-identity.ts +36 -0
- package/examples/run-agent/run-agent-with-model.ts +33 -0
- package/examples/run-agent/run-agent-with-tools.ts +31 -0
- package/examples/tools/basic-tool.ts +35 -0
- package/examples/tools/destructive-tool.ts +34 -0
- package/examples/tools/web-search-tool.ts +37 -0
- package/examples/usage/get-chat-usage.ts +22 -0
- package/examples/usage/get-message-usage.ts +22 -0
- package/examples/usage/get-usage-stats-by-range.ts +23 -0
- package/examples/usage/get-usage-stats.ts +18 -0
- package/examples/users/create-user.ts +19 -0
- package/examples/users/delete-user.ts +36 -0
- package/examples/users/get-user-by-id.ts +18 -0
- package/examples/users/list-users.ts +18 -0
- package/examples/users/update-user.ts +22 -0
- package/package.json +9 -2
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__decorateClass,
|
|
3
|
+
__decorateParam
|
|
4
|
+
} from "../../../chunk-CF3WOMB3.mjs";
|
|
5
|
+
import { autoInjectable, inject } from "tsyringe";
|
|
6
|
+
import { BaseUseCase } from "../../../common/base/base.use-case.js";
|
|
7
|
+
import { AgenticJobRepository, AgenticJobRunRepository } from "../repositories";
|
|
8
|
+
import { openRun } from "../utils/open-run";
|
|
9
|
+
import { leaseMs as defaultLeaseMs } from "../utils/settings";
|
|
10
|
+
let StartAgenticJobRunUseCase = class extends BaseUseCase {
|
|
11
|
+
constructor(agenticJobRepository, agenticJobRunRepository) {
|
|
12
|
+
super();
|
|
13
|
+
this.agenticJobRepository = agenticJobRepository;
|
|
14
|
+
this.agenticJobRunRepository = agenticJobRunRepository;
|
|
15
|
+
}
|
|
16
|
+
agenticJobRepository;
|
|
17
|
+
agenticJobRunRepository;
|
|
18
|
+
async execute(params) {
|
|
19
|
+
const { jobId, workerId } = params;
|
|
20
|
+
if (!jobId || !workerId) throw new Error("The job and the worker are required");
|
|
21
|
+
const now = params.now ?? /* @__PURE__ */ new Date();
|
|
22
|
+
const existing = await this.agenticJobRepository.findById(jobId);
|
|
23
|
+
if (!existing) throw new Error("Agentic job not found");
|
|
24
|
+
const job = await this.agenticJobRepository.claimManual({ id: jobId, workerId, now, leaseMs: params.leaseMs ?? defaultLeaseMs() });
|
|
25
|
+
if (!job) throw new Error("The agentic job is already running");
|
|
26
|
+
const run = await openRun(this.agenticJobRunRepository, { job, trigger: "manual", status: "running", workerId, now });
|
|
27
|
+
await this.agenticJobRepository.setActiveRun(jobId, workerId, String(run._id));
|
|
28
|
+
return { data: { job, run: run.toJSON() } };
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
StartAgenticJobRunUseCase = __decorateClass([
|
|
32
|
+
autoInjectable(),
|
|
33
|
+
__decorateParam(0, inject(AgenticJobRepository)),
|
|
34
|
+
__decorateParam(1, inject(AgenticJobRunRepository))
|
|
35
|
+
], StartAgenticJobRunUseCase);
|
|
36
|
+
export {
|
|
37
|
+
StartAgenticJobRunUseCase
|
|
38
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { BaseUseCase, BaseUseCaseParams, BaseUseCaseOutput } from '../../../common/base/base.use-case.js';
|
|
2
|
+
import { AgenticJobRunProps } from '../entities';
|
|
3
|
+
import { AgenticJobRepository, AgenticJobRunRepository } from '../repositories';
|
|
4
|
+
export interface Params extends BaseUseCaseParams {
|
|
5
|
+
runId: string;
|
|
6
|
+
now?: Date;
|
|
7
|
+
}
|
|
8
|
+
export interface Output extends BaseUseCaseOutput {
|
|
9
|
+
data: AgenticJobRunProps | null;
|
|
10
|
+
}
|
|
11
|
+
export declare const LOST_WORKER_MESSAGE = "The worker that was running this job was lost before it finished";
|
|
12
|
+
export declare class SyncAgenticJobRunUseCase extends BaseUseCase<Params, Output> {
|
|
13
|
+
private readonly agenticJobRepository?;
|
|
14
|
+
private readonly agenticJobRunRepository?;
|
|
15
|
+
constructor(agenticJobRepository?: AgenticJobRepository | undefined, agenticJobRunRepository?: AgenticJobRunRepository | undefined);
|
|
16
|
+
execute(params: Params): Promise<Output>;
|
|
17
|
+
private syncPaused;
|
|
18
|
+
private syncRunning;
|
|
19
|
+
private settle;
|
|
20
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
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 __decorateClass = (decorators, target, key, kind) => {
|
|
20
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
21
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
22
|
+
if (decorator = decorators[i])
|
|
23
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
24
|
+
if (kind && result) __defProp(target, key, result);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
28
|
+
var sync_agentic_job_run_use_case_exports = {};
|
|
29
|
+
__export(sync_agentic_job_run_use_case_exports, {
|
|
30
|
+
LOST_WORKER_MESSAGE: () => LOST_WORKER_MESSAGE,
|
|
31
|
+
SyncAgenticJobRunUseCase: () => SyncAgenticJobRunUseCase
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(sync_agentic_job_run_use_case_exports);
|
|
34
|
+
var import_tsyringe = require("tsyringe");
|
|
35
|
+
var import_base = require("../../../common/base/base.use-case.js");
|
|
36
|
+
var import_chats = require("../../chats/index.js");
|
|
37
|
+
var import_entities = require("../entities");
|
|
38
|
+
var import_repositories = require("../repositories");
|
|
39
|
+
const LOST_WORKER_MESSAGE = "The worker that was running this job was lost before it finished";
|
|
40
|
+
let SyncAgenticJobRunUseCase = class extends import_base.BaseUseCase {
|
|
41
|
+
constructor(agenticJobRepository, agenticJobRunRepository) {
|
|
42
|
+
super();
|
|
43
|
+
this.agenticJobRepository = agenticJobRepository;
|
|
44
|
+
this.agenticJobRunRepository = agenticJobRunRepository;
|
|
45
|
+
}
|
|
46
|
+
agenticJobRepository;
|
|
47
|
+
agenticJobRunRepository;
|
|
48
|
+
async execute(params) {
|
|
49
|
+
const now = params.now ?? /* @__PURE__ */ new Date();
|
|
50
|
+
const run = await this.agenticJobRunRepository?.findById(params.runId);
|
|
51
|
+
if (!run) return { data: null };
|
|
52
|
+
if (run.status === "paused") return { data: await this.syncPaused(run, now) };
|
|
53
|
+
if (run.status === "running") return { data: await this.syncRunning(run, now) };
|
|
54
|
+
return { data: run };
|
|
55
|
+
}
|
|
56
|
+
async syncPaused(run, now) {
|
|
57
|
+
if (!run.chatId) return run;
|
|
58
|
+
const { data: chat } = await (0, import_chats.getChatById)({ id: String(run.chatId) });
|
|
59
|
+
const status = chat?.state?.status;
|
|
60
|
+
if (status === "idle") return await this.settle(run, "completed", now);
|
|
61
|
+
if (status === "failed") return await this.settle(run, "failed", now, { code: chat?.state?.lastError?.code, message: chat?.state?.lastError?.message ?? "The run failed" });
|
|
62
|
+
return run;
|
|
63
|
+
}
|
|
64
|
+
async syncRunning(run, now) {
|
|
65
|
+
const job = run.jobId ? await this.agenticJobRepository?.findById(String(run.jobId)) : null;
|
|
66
|
+
const lockedUntil = job?.lockedUntil ? new Date(job.lockedUntil).getTime() : 0;
|
|
67
|
+
if (job && lockedUntil > now.getTime()) return run;
|
|
68
|
+
return await this.settle(run, "failed", now, { code: "WORKER_LOST", message: LOST_WORKER_MESSAGE });
|
|
69
|
+
}
|
|
70
|
+
async settle(run, status, now, error) {
|
|
71
|
+
const updated = import_entities.AgenticJobRun.fromJSON({ ...run, status, finishedAt: now, ...error ? { error } : {} });
|
|
72
|
+
const saved = await this.agenticJobRunRepository?.update(updated);
|
|
73
|
+
if (run.jobId && run._id) await this.agenticJobRepository?.settleRun(String(run.jobId), String(run._id), status, now);
|
|
74
|
+
return saved ?? updated.toJSON();
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
SyncAgenticJobRunUseCase = __decorateClass([
|
|
78
|
+
(0, import_tsyringe.autoInjectable)(),
|
|
79
|
+
__decorateParam(0, (0, import_tsyringe.inject)(import_repositories.AgenticJobRepository)),
|
|
80
|
+
__decorateParam(1, (0, import_tsyringe.inject)(import_repositories.AgenticJobRunRepository))
|
|
81
|
+
], SyncAgenticJobRunUseCase);
|
|
82
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
83
|
+
0 && (module.exports = {
|
|
84
|
+
LOST_WORKER_MESSAGE,
|
|
85
|
+
SyncAgenticJobRunUseCase
|
|
86
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__decorateClass,
|
|
3
|
+
__decorateParam
|
|
4
|
+
} from "../../../chunk-CF3WOMB3.mjs";
|
|
5
|
+
import { autoInjectable, inject } from "tsyringe";
|
|
6
|
+
import { BaseUseCase } from "../../../common/base/base.use-case.js";
|
|
7
|
+
import { getChatById } from "../../chats/index.js";
|
|
8
|
+
import { AgenticJobRun } from "../entities";
|
|
9
|
+
import { AgenticJobRepository, AgenticJobRunRepository } from "../repositories";
|
|
10
|
+
const LOST_WORKER_MESSAGE = "The worker that was running this job was lost before it finished";
|
|
11
|
+
let SyncAgenticJobRunUseCase = class extends BaseUseCase {
|
|
12
|
+
constructor(agenticJobRepository, agenticJobRunRepository) {
|
|
13
|
+
super();
|
|
14
|
+
this.agenticJobRepository = agenticJobRepository;
|
|
15
|
+
this.agenticJobRunRepository = agenticJobRunRepository;
|
|
16
|
+
}
|
|
17
|
+
agenticJobRepository;
|
|
18
|
+
agenticJobRunRepository;
|
|
19
|
+
async execute(params) {
|
|
20
|
+
const now = params.now ?? /* @__PURE__ */ new Date();
|
|
21
|
+
const run = await this.agenticJobRunRepository?.findById(params.runId);
|
|
22
|
+
if (!run) return { data: null };
|
|
23
|
+
if (run.status === "paused") return { data: await this.syncPaused(run, now) };
|
|
24
|
+
if (run.status === "running") return { data: await this.syncRunning(run, now) };
|
|
25
|
+
return { data: run };
|
|
26
|
+
}
|
|
27
|
+
async syncPaused(run, now) {
|
|
28
|
+
if (!run.chatId) return run;
|
|
29
|
+
const { data: chat } = await getChatById({ id: String(run.chatId) });
|
|
30
|
+
const status = chat?.state?.status;
|
|
31
|
+
if (status === "idle") return await this.settle(run, "completed", now);
|
|
32
|
+
if (status === "failed") return await this.settle(run, "failed", now, { code: chat?.state?.lastError?.code, message: chat?.state?.lastError?.message ?? "The run failed" });
|
|
33
|
+
return run;
|
|
34
|
+
}
|
|
35
|
+
async syncRunning(run, now) {
|
|
36
|
+
const job = run.jobId ? await this.agenticJobRepository?.findById(String(run.jobId)) : null;
|
|
37
|
+
const lockedUntil = job?.lockedUntil ? new Date(job.lockedUntil).getTime() : 0;
|
|
38
|
+
if (job && lockedUntil > now.getTime()) return run;
|
|
39
|
+
return await this.settle(run, "failed", now, { code: "WORKER_LOST", message: LOST_WORKER_MESSAGE });
|
|
40
|
+
}
|
|
41
|
+
async settle(run, status, now, error) {
|
|
42
|
+
const updated = AgenticJobRun.fromJSON({ ...run, status, finishedAt: now, ...error ? { error } : {} });
|
|
43
|
+
const saved = await this.agenticJobRunRepository?.update(updated);
|
|
44
|
+
if (run.jobId && run._id) await this.agenticJobRepository?.settleRun(String(run.jobId), String(run._id), status, now);
|
|
45
|
+
return saved ?? updated.toJSON();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
SyncAgenticJobRunUseCase = __decorateClass([
|
|
49
|
+
autoInjectable(),
|
|
50
|
+
__decorateParam(0, inject(AgenticJobRepository)),
|
|
51
|
+
__decorateParam(1, inject(AgenticJobRunRepository))
|
|
52
|
+
], SyncAgenticJobRunUseCase);
|
|
53
|
+
export {
|
|
54
|
+
LOST_WORKER_MESSAGE,
|
|
55
|
+
SyncAgenticJobRunUseCase
|
|
56
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BaseUseCase, BaseUseCaseParams, BaseUseCaseOutput } from '../../../common/base/base.use-case.js';
|
|
2
|
+
import { AgenticJobRunRepository } from '../repositories';
|
|
3
|
+
import { SyncAgenticJobRunUseCase } from './sync-agentic-job-run.use-case';
|
|
4
|
+
export interface Params extends BaseUseCaseParams {
|
|
5
|
+
limit?: number;
|
|
6
|
+
now?: Date;
|
|
7
|
+
}
|
|
8
|
+
export interface Output extends BaseUseCaseOutput {
|
|
9
|
+
data: {
|
|
10
|
+
checked: number;
|
|
11
|
+
settled: number;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export declare class SyncAgenticJobRunsUseCase extends BaseUseCase<Params, Output> {
|
|
15
|
+
private readonly agenticJobRunRepository?;
|
|
16
|
+
private readonly syncRun?;
|
|
17
|
+
constructor(agenticJobRunRepository?: AgenticJobRunRepository | undefined, syncRun?: SyncAgenticJobRunUseCase | undefined);
|
|
18
|
+
execute(params: Params): Promise<Output>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
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 __decorateClass = (decorators, target, key, kind) => {
|
|
20
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
21
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
22
|
+
if (decorator = decorators[i])
|
|
23
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
24
|
+
if (kind && result) __defProp(target, key, result);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
28
|
+
var sync_agentic_job_runs_use_case_exports = {};
|
|
29
|
+
__export(sync_agentic_job_runs_use_case_exports, {
|
|
30
|
+
SyncAgenticJobRunsUseCase: () => SyncAgenticJobRunsUseCase
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(sync_agentic_job_runs_use_case_exports);
|
|
33
|
+
var import_tsyringe = require("tsyringe");
|
|
34
|
+
var import_base = require("../../../common/base/base.use-case.js");
|
|
35
|
+
var import_repositories = require("../repositories");
|
|
36
|
+
var import_sync_agentic_job_run = require("./sync-agentic-job-run.use-case");
|
|
37
|
+
const DEFAULT_LIMIT = 100;
|
|
38
|
+
let SyncAgenticJobRunsUseCase = class extends import_base.BaseUseCase {
|
|
39
|
+
constructor(agenticJobRunRepository, syncRun) {
|
|
40
|
+
super();
|
|
41
|
+
this.agenticJobRunRepository = agenticJobRunRepository;
|
|
42
|
+
this.syncRun = syncRun;
|
|
43
|
+
}
|
|
44
|
+
agenticJobRunRepository;
|
|
45
|
+
syncRun;
|
|
46
|
+
async execute(params) {
|
|
47
|
+
const limit = params.limit ?? DEFAULT_LIMIT;
|
|
48
|
+
const open = [
|
|
49
|
+
...await this.agenticJobRunRepository?.findByStatus("paused", limit) ?? [],
|
|
50
|
+
...await this.agenticJobRunRepository?.findByStatus("running", limit) ?? []
|
|
51
|
+
];
|
|
52
|
+
let settled = 0;
|
|
53
|
+
for (const run of open) {
|
|
54
|
+
const { data } = await this.syncRun.execute({ runId: String(run._id), now: params.now });
|
|
55
|
+
if (data && data.status !== run.status) settled++;
|
|
56
|
+
}
|
|
57
|
+
return { data: { checked: open.length, settled } };
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
SyncAgenticJobRunsUseCase = __decorateClass([
|
|
61
|
+
(0, import_tsyringe.autoInjectable)(),
|
|
62
|
+
__decorateParam(0, (0, import_tsyringe.inject)(import_repositories.AgenticJobRunRepository)),
|
|
63
|
+
__decorateParam(1, (0, import_tsyringe.inject)(import_sync_agentic_job_run.SyncAgenticJobRunUseCase))
|
|
64
|
+
], SyncAgenticJobRunsUseCase);
|
|
65
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
66
|
+
0 && (module.exports = {
|
|
67
|
+
SyncAgenticJobRunsUseCase
|
|
68
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__decorateClass,
|
|
3
|
+
__decorateParam
|
|
4
|
+
} from "../../../chunk-CF3WOMB3.mjs";
|
|
5
|
+
import { autoInjectable, inject } from "tsyringe";
|
|
6
|
+
import { BaseUseCase } from "../../../common/base/base.use-case.js";
|
|
7
|
+
import { AgenticJobRunRepository } from "../repositories";
|
|
8
|
+
import { SyncAgenticJobRunUseCase } from "./sync-agentic-job-run.use-case";
|
|
9
|
+
const DEFAULT_LIMIT = 100;
|
|
10
|
+
let SyncAgenticJobRunsUseCase = class extends BaseUseCase {
|
|
11
|
+
constructor(agenticJobRunRepository, syncRun) {
|
|
12
|
+
super();
|
|
13
|
+
this.agenticJobRunRepository = agenticJobRunRepository;
|
|
14
|
+
this.syncRun = syncRun;
|
|
15
|
+
}
|
|
16
|
+
agenticJobRunRepository;
|
|
17
|
+
syncRun;
|
|
18
|
+
async execute(params) {
|
|
19
|
+
const limit = params.limit ?? DEFAULT_LIMIT;
|
|
20
|
+
const open = [
|
|
21
|
+
...await this.agenticJobRunRepository?.findByStatus("paused", limit) ?? [],
|
|
22
|
+
...await this.agenticJobRunRepository?.findByStatus("running", limit) ?? []
|
|
23
|
+
];
|
|
24
|
+
let settled = 0;
|
|
25
|
+
for (const run of open) {
|
|
26
|
+
const { data } = await this.syncRun.execute({ runId: String(run._id), now: params.now });
|
|
27
|
+
if (data && data.status !== run.status) settled++;
|
|
28
|
+
}
|
|
29
|
+
return { data: { checked: open.length, settled } };
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
SyncAgenticJobRunsUseCase = __decorateClass([
|
|
33
|
+
autoInjectable(),
|
|
34
|
+
__decorateParam(0, inject(AgenticJobRunRepository)),
|
|
35
|
+
__decorateParam(1, inject(SyncAgenticJobRunUseCase))
|
|
36
|
+
], SyncAgenticJobRunsUseCase);
|
|
37
|
+
export {
|
|
38
|
+
SyncAgenticJobRunsUseCase
|
|
39
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BaseUseCase, BaseUseCaseParams, BaseUseCaseOutput } from '../../../common/base/base.use-case.js';
|
|
2
|
+
import { AgenticJobProps } from '../entities';
|
|
3
|
+
import { AgenticJobRepository } from '../repositories';
|
|
4
|
+
export interface Params extends BaseUseCaseParams {
|
|
5
|
+
id: string;
|
|
6
|
+
payload?: AgenticJobProps;
|
|
7
|
+
}
|
|
8
|
+
export interface Output extends BaseUseCaseOutput {
|
|
9
|
+
data: AgenticJobProps;
|
|
10
|
+
}
|
|
11
|
+
export declare class UpdateAgenticJobUseCase extends BaseUseCase<Params, Output> {
|
|
12
|
+
private readonly agenticJobRepository?;
|
|
13
|
+
constructor(agenticJobRepository?: AgenticJobRepository | undefined);
|
|
14
|
+
execute(params: Params): Promise<Output>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
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 __decorateClass = (decorators, target, key, kind) => {
|
|
20
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
21
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
22
|
+
if (decorator = decorators[i])
|
|
23
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
24
|
+
if (kind && result) __defProp(target, key, result);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
28
|
+
var update_agentic_job_use_case_exports = {};
|
|
29
|
+
__export(update_agentic_job_use_case_exports, {
|
|
30
|
+
UpdateAgenticJobUseCase: () => UpdateAgenticJobUseCase
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(update_agentic_job_use_case_exports);
|
|
33
|
+
var import_tsyringe = require("tsyringe");
|
|
34
|
+
var import_base = require("../../../common/base/base.use-case.js");
|
|
35
|
+
var import_mongo_db = require("../../../common/repositories/mongo-db.repository.js");
|
|
36
|
+
var import_entities = require("../entities");
|
|
37
|
+
var import_repositories = require("../repositories");
|
|
38
|
+
var import_job_fields = require("../utils/job-fields");
|
|
39
|
+
let UpdateAgenticJobUseCase = class extends import_base.BaseUseCase {
|
|
40
|
+
constructor(agenticJobRepository) {
|
|
41
|
+
super();
|
|
42
|
+
this.agenticJobRepository = agenticJobRepository;
|
|
43
|
+
}
|
|
44
|
+
agenticJobRepository;
|
|
45
|
+
async execute(params) {
|
|
46
|
+
const { id, payload, currentUser } = params;
|
|
47
|
+
if (!payload || Object.keys(payload).length === 0) throw new Error("Payload is required for updating an agentic job");
|
|
48
|
+
const existing = await this.agenticJobRepository?.findById(id);
|
|
49
|
+
if (!existing) throw new Error("Agentic job not found or update failed");
|
|
50
|
+
if (payload.__v !== void 0 && payload.__v !== existing.__v) throw new import_mongo_db.VersionConflictError();
|
|
51
|
+
const changes = (0, import_job_fields.readJobFields)(payload, { complete: false });
|
|
52
|
+
const { modelProvider, modelName, ...rest } = changes;
|
|
53
|
+
const provider = modelProvider === void 0 ? existing.modelProvider : modelProvider;
|
|
54
|
+
const name = modelName === void 0 ? existing.modelName : modelName;
|
|
55
|
+
(0, import_job_fields.assertModel)(provider, name);
|
|
56
|
+
const job = import_entities.AgenticJob.fromJSON({
|
|
57
|
+
...existing,
|
|
58
|
+
...rest,
|
|
59
|
+
modelProvider: provider ?? void 0,
|
|
60
|
+
modelName: name ?? void 0,
|
|
61
|
+
_id: id
|
|
62
|
+
});
|
|
63
|
+
job.setUpdatedBy(currentUser);
|
|
64
|
+
if (!provider) job.unset(["modelProvider", "modelName"]);
|
|
65
|
+
const schedulingChanged = rest.recurrence !== void 0 || rest.timezone !== void 0 || rest.enabled !== void 0;
|
|
66
|
+
const missingNextRun = job.enabled && !job.nextRunAt;
|
|
67
|
+
if (schedulingChanged || missingNextRun) {
|
|
68
|
+
job.scheduleNextRun();
|
|
69
|
+
if (job.enabled && !job.nextRunAt) throw new Error("The date of the recurrence has already passed");
|
|
70
|
+
}
|
|
71
|
+
const result = await this.agenticJobRepository?.update(job, { expectedVersion: existing.__v });
|
|
72
|
+
if (!result) throw new Error("Agentic job not found or update failed");
|
|
73
|
+
return { data: result };
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
UpdateAgenticJobUseCase = __decorateClass([
|
|
77
|
+
(0, import_tsyringe.autoInjectable)(),
|
|
78
|
+
__decorateParam(0, (0, import_tsyringe.inject)(import_repositories.AgenticJobRepository))
|
|
79
|
+
], UpdateAgenticJobUseCase);
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
UpdateAgenticJobUseCase
|
|
83
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__decorateClass,
|
|
3
|
+
__decorateParam
|
|
4
|
+
} from "../../../chunk-CF3WOMB3.mjs";
|
|
5
|
+
import { autoInjectable, inject } from "tsyringe";
|
|
6
|
+
import { BaseUseCase } from "../../../common/base/base.use-case.js";
|
|
7
|
+
import { VersionConflictError } from "../../../common/repositories/mongo-db.repository.js";
|
|
8
|
+
import { AgenticJob } from "../entities";
|
|
9
|
+
import { AgenticJobRepository } from "../repositories";
|
|
10
|
+
import { readJobFields, assertModel } from "../utils/job-fields";
|
|
11
|
+
let UpdateAgenticJobUseCase = class extends BaseUseCase {
|
|
12
|
+
constructor(agenticJobRepository) {
|
|
13
|
+
super();
|
|
14
|
+
this.agenticJobRepository = agenticJobRepository;
|
|
15
|
+
}
|
|
16
|
+
agenticJobRepository;
|
|
17
|
+
async execute(params) {
|
|
18
|
+
const { id, payload, currentUser } = params;
|
|
19
|
+
if (!payload || Object.keys(payload).length === 0) throw new Error("Payload is required for updating an agentic job");
|
|
20
|
+
const existing = await this.agenticJobRepository?.findById(id);
|
|
21
|
+
if (!existing) throw new Error("Agentic job not found or update failed");
|
|
22
|
+
if (payload.__v !== void 0 && payload.__v !== existing.__v) throw new VersionConflictError();
|
|
23
|
+
const changes = readJobFields(payload, { complete: false });
|
|
24
|
+
const { modelProvider, modelName, ...rest } = changes;
|
|
25
|
+
const provider = modelProvider === void 0 ? existing.modelProvider : modelProvider;
|
|
26
|
+
const name = modelName === void 0 ? existing.modelName : modelName;
|
|
27
|
+
assertModel(provider, name);
|
|
28
|
+
const job = AgenticJob.fromJSON({
|
|
29
|
+
...existing,
|
|
30
|
+
...rest,
|
|
31
|
+
modelProvider: provider ?? void 0,
|
|
32
|
+
modelName: name ?? void 0,
|
|
33
|
+
_id: id
|
|
34
|
+
});
|
|
35
|
+
job.setUpdatedBy(currentUser);
|
|
36
|
+
if (!provider) job.unset(["modelProvider", "modelName"]);
|
|
37
|
+
const schedulingChanged = rest.recurrence !== void 0 || rest.timezone !== void 0 || rest.enabled !== void 0;
|
|
38
|
+
const missingNextRun = job.enabled && !job.nextRunAt;
|
|
39
|
+
if (schedulingChanged || missingNextRun) {
|
|
40
|
+
job.scheduleNextRun();
|
|
41
|
+
if (job.enabled && !job.nextRunAt) throw new Error("The date of the recurrence has already passed");
|
|
42
|
+
}
|
|
43
|
+
const result = await this.agenticJobRepository?.update(job, { expectedVersion: existing.__v });
|
|
44
|
+
if (!result) throw new Error("Agentic job not found or update failed");
|
|
45
|
+
return { data: result };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
UpdateAgenticJobUseCase = __decorateClass([
|
|
49
|
+
autoInjectable(),
|
|
50
|
+
__decorateParam(0, inject(AgenticJobRepository))
|
|
51
|
+
], UpdateAgenticJobUseCase);
|
|
52
|
+
export {
|
|
53
|
+
UpdateAgenticJobUseCase
|
|
54
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Recurrence } from '../entities/recurrence';
|
|
2
|
+
export interface JobFields {
|
|
3
|
+
name?: string;
|
|
4
|
+
instructions?: string;
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
recurrence?: Recurrence;
|
|
7
|
+
timezone?: string;
|
|
8
|
+
modelProvider?: string | null;
|
|
9
|
+
modelName?: string | null;
|
|
10
|
+
}
|
|
11
|
+
export declare function readJobFields(payload: Record<string, any> | undefined, options: {
|
|
12
|
+
complete: boolean;
|
|
13
|
+
}): JobFields;
|
|
14
|
+
export declare function assertModel(provider?: string | null, name?: string | null): void;
|
|
@@ -0,0 +1,61 @@
|
|
|
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 job_fields_exports = {};
|
|
20
|
+
__export(job_fields_exports, {
|
|
21
|
+
assertModel: () => assertModel,
|
|
22
|
+
readJobFields: () => readJobFields
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(job_fields_exports);
|
|
25
|
+
var import_recurrence = require("../entities/recurrence");
|
|
26
|
+
var import_next_run = require("./next-run");
|
|
27
|
+
const MAX_NAME_CHARS = 200;
|
|
28
|
+
const MAX_INSTRUCTIONS_CHARS = 2e4;
|
|
29
|
+
const text = (value, label, max) => {
|
|
30
|
+
if (typeof value !== "string" || !value.trim()) throw new Error(`The ${label} is required`);
|
|
31
|
+
if (value.length > max) throw new Error(`The ${label} cannot be longer than ${max} characters`);
|
|
32
|
+
return value.trim();
|
|
33
|
+
};
|
|
34
|
+
const optionalText = (value, label) => {
|
|
35
|
+
if (value === null || value === "") return null;
|
|
36
|
+
return text(value, label, MAX_NAME_CHARS);
|
|
37
|
+
};
|
|
38
|
+
function readJobFields(payload, options) {
|
|
39
|
+
const input = payload ?? {};
|
|
40
|
+
const fields = {};
|
|
41
|
+
const has = (key) => input[key] !== void 0;
|
|
42
|
+
if (options.complete || has("name")) fields.name = text(input.name, "name", MAX_NAME_CHARS);
|
|
43
|
+
if (options.complete || has("instructions")) fields.instructions = text(input.instructions, "instructions", MAX_INSTRUCTIONS_CHARS);
|
|
44
|
+
if (options.complete || has("recurrence")) fields.recurrence = (0, import_recurrence.validateRecurrence)(input.recurrence);
|
|
45
|
+
if (options.complete || has("timezone")) fields.timezone = (0, import_next_run.assertTimezone)(input.timezone);
|
|
46
|
+
if (has("enabled")) {
|
|
47
|
+
if (typeof input.enabled !== "boolean") throw new Error("The field enabled must be true or false");
|
|
48
|
+
fields.enabled = input.enabled;
|
|
49
|
+
}
|
|
50
|
+
if (has("modelProvider")) fields.modelProvider = optionalText(input.modelProvider, "model provider");
|
|
51
|
+
if (has("modelName")) fields.modelName = optionalText(input.modelName, "model name");
|
|
52
|
+
return fields;
|
|
53
|
+
}
|
|
54
|
+
function assertModel(provider, name) {
|
|
55
|
+
if (Boolean(provider) !== Boolean(name)) throw new Error("Set both the model provider and the model name, or neither");
|
|
56
|
+
}
|
|
57
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
58
|
+
0 && (module.exports = {
|
|
59
|
+
assertModel,
|
|
60
|
+
readJobFields
|
|
61
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import "../../../chunk-CF3WOMB3.mjs";
|
|
2
|
+
import { validateRecurrence } from "../entities/recurrence";
|
|
3
|
+
import { assertTimezone } from "./next-run";
|
|
4
|
+
const MAX_NAME_CHARS = 200;
|
|
5
|
+
const MAX_INSTRUCTIONS_CHARS = 2e4;
|
|
6
|
+
const text = (value, label, max) => {
|
|
7
|
+
if (typeof value !== "string" || !value.trim()) throw new Error(`The ${label} is required`);
|
|
8
|
+
if (value.length > max) throw new Error(`The ${label} cannot be longer than ${max} characters`);
|
|
9
|
+
return value.trim();
|
|
10
|
+
};
|
|
11
|
+
const optionalText = (value, label) => {
|
|
12
|
+
if (value === null || value === "") return null;
|
|
13
|
+
return text(value, label, MAX_NAME_CHARS);
|
|
14
|
+
};
|
|
15
|
+
function readJobFields(payload, options) {
|
|
16
|
+
const input = payload ?? {};
|
|
17
|
+
const fields = {};
|
|
18
|
+
const has = (key) => input[key] !== void 0;
|
|
19
|
+
if (options.complete || has("name")) fields.name = text(input.name, "name", MAX_NAME_CHARS);
|
|
20
|
+
if (options.complete || has("instructions")) fields.instructions = text(input.instructions, "instructions", MAX_INSTRUCTIONS_CHARS);
|
|
21
|
+
if (options.complete || has("recurrence")) fields.recurrence = validateRecurrence(input.recurrence);
|
|
22
|
+
if (options.complete || has("timezone")) fields.timezone = assertTimezone(input.timezone);
|
|
23
|
+
if (has("enabled")) {
|
|
24
|
+
if (typeof input.enabled !== "boolean") throw new Error("The field enabled must be true or false");
|
|
25
|
+
fields.enabled = input.enabled;
|
|
26
|
+
}
|
|
27
|
+
if (has("modelProvider")) fields.modelProvider = optionalText(input.modelProvider, "model provider");
|
|
28
|
+
if (has("modelName")) fields.modelName = optionalText(input.modelName, "model name");
|
|
29
|
+
return fields;
|
|
30
|
+
}
|
|
31
|
+
function assertModel(provider, name) {
|
|
32
|
+
if (Boolean(provider) !== Boolean(name)) throw new Error("Set both the model provider and the model name, or neither");
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
assertModel,
|
|
36
|
+
readJobFields
|
|
37
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Recurrence } from '../entities/recurrence';
|
|
2
|
+
export declare function assertTimezone(timezone: unknown): string;
|
|
3
|
+
export declare function computeNextRun(recurrence: Recurrence, timezone: string, from?: Date): Date | null;
|
|
4
|
+
export declare function previewNextRuns(recurrence: Recurrence, timezone: string, count: number, from?: Date): Date[];
|