@cuylabs/agent-runtime 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/LICENSE +201 -0
- package/README.md +242 -0
- package/dist/chunk-DBQSJ476.js +153 -0
- package/dist/chunk-H6BHDIUX.js +1466 -0
- package/dist/chunk-ZVISWF7S.js +108 -0
- package/dist/driver-KdyMlXQq.d.ts +127 -0
- package/dist/drivers/in-memory.d.ts +27 -0
- package/dist/drivers/in-memory.js +7 -0
- package/dist/index-CbLKNFMq.d.ts +266 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +408 -0
- package/dist/orchestration/index.d.ts +2 -0
- package/dist/orchestration/index.js +13 -0
- package/package.json +66 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// src/schedule.ts
|
|
2
|
+
import { Cron } from "croner";
|
|
3
|
+
var CRON_CACHE_MAX = 256;
|
|
4
|
+
var cronCache = /* @__PURE__ */ new Map();
|
|
5
|
+
function parseAtMs(at) {
|
|
6
|
+
if (typeof at === "number") {
|
|
7
|
+
return Number.isFinite(at) ? at : void 0;
|
|
8
|
+
}
|
|
9
|
+
if (at instanceof Date) {
|
|
10
|
+
const ms2 = at.getTime();
|
|
11
|
+
return Number.isFinite(ms2) ? ms2 : void 0;
|
|
12
|
+
}
|
|
13
|
+
const ms = Date.parse(at);
|
|
14
|
+
return Number.isFinite(ms) ? ms : void 0;
|
|
15
|
+
}
|
|
16
|
+
function resolveCachedCron(schedule) {
|
|
17
|
+
const timezone = schedule.timezone?.trim() || "";
|
|
18
|
+
const key = `${timezone}\0${schedule.expr}`;
|
|
19
|
+
const cached = cronCache.get(key);
|
|
20
|
+
if (cached) {
|
|
21
|
+
return cached;
|
|
22
|
+
}
|
|
23
|
+
if (cronCache.size >= CRON_CACHE_MAX) {
|
|
24
|
+
const oldest = cronCache.keys().next().value;
|
|
25
|
+
if (oldest) {
|
|
26
|
+
cronCache.delete(oldest);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const created = new Cron(schedule.expr, {
|
|
30
|
+
timezone: timezone || void 0,
|
|
31
|
+
catch: false
|
|
32
|
+
});
|
|
33
|
+
cronCache.set(key, created);
|
|
34
|
+
return created;
|
|
35
|
+
}
|
|
36
|
+
function normalizeSchedule(input) {
|
|
37
|
+
if (input.kind === "at") {
|
|
38
|
+
const atMs = parseAtMs(input.at);
|
|
39
|
+
if (atMs === void 0) {
|
|
40
|
+
throw new Error(`Invalid at-schedule timestamp: ${String(input.at)}`);
|
|
41
|
+
}
|
|
42
|
+
return { kind: "at", at: atMs };
|
|
43
|
+
}
|
|
44
|
+
if (input.kind === "every") {
|
|
45
|
+
const everyMs = Math.floor(input.everyMs);
|
|
46
|
+
if (!Number.isFinite(everyMs) || everyMs <= 0) {
|
|
47
|
+
throw new Error(`Invalid every-schedule interval: ${String(input.everyMs)}`);
|
|
48
|
+
}
|
|
49
|
+
let anchorMs;
|
|
50
|
+
if (input.anchorMs !== void 0) {
|
|
51
|
+
const nextAnchor = Math.floor(input.anchorMs);
|
|
52
|
+
if (!Number.isFinite(nextAnchor) || nextAnchor < 0) {
|
|
53
|
+
throw new Error(`Invalid every-schedule anchor: ${String(input.anchorMs)}`);
|
|
54
|
+
}
|
|
55
|
+
anchorMs = nextAnchor;
|
|
56
|
+
}
|
|
57
|
+
return { kind: "every", everyMs, ...anchorMs !== void 0 ? { anchorMs } : {} };
|
|
58
|
+
}
|
|
59
|
+
const expr = input.expr.trim();
|
|
60
|
+
if (!expr) {
|
|
61
|
+
throw new Error("Invalid cron schedule: empty expression");
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
kind: "cron",
|
|
65
|
+
expr,
|
|
66
|
+
...input.timezone?.trim() ? { timezone: input.timezone.trim() } : {}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function computeNextRunAtMs(schedule, nowMs) {
|
|
70
|
+
if (schedule.kind === "at") {
|
|
71
|
+
return schedule.at > nowMs ? schedule.at : void 0;
|
|
72
|
+
}
|
|
73
|
+
if (schedule.kind === "every") {
|
|
74
|
+
const anchor = schedule.anchorMs ?? nowMs;
|
|
75
|
+
if (nowMs < anchor) {
|
|
76
|
+
return anchor;
|
|
77
|
+
}
|
|
78
|
+
const elapsed = nowMs - anchor;
|
|
79
|
+
const steps = Math.max(1, Math.floor((elapsed + schedule.everyMs - 1) / schedule.everyMs));
|
|
80
|
+
return anchor + steps * schedule.everyMs;
|
|
81
|
+
}
|
|
82
|
+
const cron = resolveCachedCron(schedule);
|
|
83
|
+
const first = cron.nextRun(new Date(nowMs));
|
|
84
|
+
if (!first) {
|
|
85
|
+
return void 0;
|
|
86
|
+
}
|
|
87
|
+
const firstMs = first.getTime();
|
|
88
|
+
if (!Number.isFinite(firstMs)) {
|
|
89
|
+
return void 0;
|
|
90
|
+
}
|
|
91
|
+
if (firstMs > nowMs) {
|
|
92
|
+
return firstMs;
|
|
93
|
+
}
|
|
94
|
+
const retry = cron.nextRun(new Date(Math.floor(nowMs / 1e3) * 1e3 + 1e3));
|
|
95
|
+
if (!retry) {
|
|
96
|
+
return void 0;
|
|
97
|
+
}
|
|
98
|
+
const retryMs = retry.getTime();
|
|
99
|
+
if (!Number.isFinite(retryMs) || retryMs <= nowMs) {
|
|
100
|
+
return void 0;
|
|
101
|
+
}
|
|
102
|
+
return retryMs;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export {
|
|
106
|
+
normalizeSchedule,
|
|
107
|
+
computeNextRunAtMs
|
|
108
|
+
};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
type RuntimeRunStatus = "ok" | "error" | "skipped";
|
|
2
|
+
type RuntimeTrigger = "due" | "manual";
|
|
3
|
+
type RuntimeRetryStrategy = "fixed" | "exponential";
|
|
4
|
+
interface RuntimeRetryPolicy {
|
|
5
|
+
maxAttempts: number;
|
|
6
|
+
backoffMs?: number;
|
|
7
|
+
strategy?: RuntimeRetryStrategy;
|
|
8
|
+
maxBackoffMs?: number;
|
|
9
|
+
}
|
|
10
|
+
interface RuntimeAtSchedule {
|
|
11
|
+
kind: "at";
|
|
12
|
+
at: number;
|
|
13
|
+
}
|
|
14
|
+
interface RuntimeEverySchedule {
|
|
15
|
+
kind: "every";
|
|
16
|
+
everyMs: number;
|
|
17
|
+
anchorMs?: number;
|
|
18
|
+
}
|
|
19
|
+
interface RuntimeCronSchedule {
|
|
20
|
+
kind: "cron";
|
|
21
|
+
expr: string;
|
|
22
|
+
timezone?: string;
|
|
23
|
+
}
|
|
24
|
+
type RuntimeJobSchedule = RuntimeAtSchedule | RuntimeEverySchedule | RuntimeCronSchedule;
|
|
25
|
+
interface RuntimeAtScheduleInput {
|
|
26
|
+
kind: "at";
|
|
27
|
+
at: number | string | Date;
|
|
28
|
+
}
|
|
29
|
+
type RuntimeJobScheduleInput = RuntimeAtScheduleInput | RuntimeEverySchedule | RuntimeCronSchedule;
|
|
30
|
+
interface RuntimeJobState {
|
|
31
|
+
nextRunAtMs?: number;
|
|
32
|
+
lastRunAtMs?: number;
|
|
33
|
+
lastStatus?: RuntimeRunStatus;
|
|
34
|
+
lastError?: string;
|
|
35
|
+
lastAttemptCount?: number;
|
|
36
|
+
deadLetterCount?: number;
|
|
37
|
+
lastDeadLetterAtMs?: number;
|
|
38
|
+
running?: boolean;
|
|
39
|
+
}
|
|
40
|
+
interface RuntimeJobRecord<TPayload = unknown> {
|
|
41
|
+
id: string;
|
|
42
|
+
name: string;
|
|
43
|
+
payload: TPayload;
|
|
44
|
+
schedule: RuntimeJobSchedule;
|
|
45
|
+
retryPolicy?: RuntimeRetryPolicy;
|
|
46
|
+
enabled: boolean;
|
|
47
|
+
metadata?: Record<string, string>;
|
|
48
|
+
createdAt: string;
|
|
49
|
+
updatedAt: string;
|
|
50
|
+
state: RuntimeJobState;
|
|
51
|
+
}
|
|
52
|
+
interface RuntimeScheduleInput<TPayload = unknown> {
|
|
53
|
+
id?: string;
|
|
54
|
+
name: string;
|
|
55
|
+
payload: TPayload;
|
|
56
|
+
schedule: RuntimeJobScheduleInput;
|
|
57
|
+
retryPolicy?: RuntimeRetryPolicy;
|
|
58
|
+
enabled?: boolean;
|
|
59
|
+
metadata?: Record<string, string>;
|
|
60
|
+
}
|
|
61
|
+
interface RuntimeSchedulePatch<TPayload = unknown> {
|
|
62
|
+
name?: string;
|
|
63
|
+
payload?: TPayload;
|
|
64
|
+
schedule?: RuntimeJobScheduleInput;
|
|
65
|
+
retryPolicy?: RuntimeRetryPolicy;
|
|
66
|
+
enabled?: boolean;
|
|
67
|
+
metadata?: Record<string, string>;
|
|
68
|
+
}
|
|
69
|
+
interface RuntimeExecutionContext {
|
|
70
|
+
trigger: RuntimeTrigger;
|
|
71
|
+
startedAt: number;
|
|
72
|
+
attempt: number;
|
|
73
|
+
signal: AbortSignal;
|
|
74
|
+
deadlineAt?: number;
|
|
75
|
+
}
|
|
76
|
+
interface RuntimeExecutionResult {
|
|
77
|
+
status?: RuntimeRunStatus;
|
|
78
|
+
error?: string;
|
|
79
|
+
}
|
|
80
|
+
type RuntimeExecutor<TPayload = unknown> = (job: RuntimeJobRecord<TPayload>, context: RuntimeExecutionContext) => Promise<RuntimeExecutionResult | void> | RuntimeExecutionResult | void;
|
|
81
|
+
interface RuntimeDispatchInput {
|
|
82
|
+
jobId: string;
|
|
83
|
+
trigger: RuntimeTrigger;
|
|
84
|
+
}
|
|
85
|
+
interface RuntimeRunResult {
|
|
86
|
+
jobId: string;
|
|
87
|
+
trigger: RuntimeTrigger;
|
|
88
|
+
status: RuntimeRunStatus;
|
|
89
|
+
startedAt: number;
|
|
90
|
+
endedAt: number;
|
|
91
|
+
error?: string;
|
|
92
|
+
}
|
|
93
|
+
interface RuntimeDeadLetterEvent<TPayload = unknown> {
|
|
94
|
+
job: RuntimeJobRecord<TPayload>;
|
|
95
|
+
trigger: RuntimeTrigger;
|
|
96
|
+
attempts: number;
|
|
97
|
+
startedAt: number;
|
|
98
|
+
endedAt: number;
|
|
99
|
+
error: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
type RuntimeLogMeta = Record<string, unknown>;
|
|
103
|
+
interface RuntimeLogger {
|
|
104
|
+
debug(message: string, meta?: RuntimeLogMeta): void;
|
|
105
|
+
info(message: string, meta?: RuntimeLogMeta): void;
|
|
106
|
+
warn(message: string, meta?: RuntimeLogMeta): void;
|
|
107
|
+
error(message: string, meta?: RuntimeLogMeta): void;
|
|
108
|
+
}
|
|
109
|
+
declare const silentRuntimeLogger: RuntimeLogger;
|
|
110
|
+
declare function createConsoleRuntimeLogger(prefix?: string): RuntimeLogger;
|
|
111
|
+
|
|
112
|
+
interface RuntimeDriverContext<TPayload = unknown> {
|
|
113
|
+
now: () => number;
|
|
114
|
+
onDue: (jobId: string) => Promise<void | RuntimeRunResult>;
|
|
115
|
+
logger: RuntimeLogger;
|
|
116
|
+
}
|
|
117
|
+
interface RuntimeDriver<TPayload = unknown> {
|
|
118
|
+
readonly name: string;
|
|
119
|
+
start(context: RuntimeDriverContext<TPayload>): Promise<void>;
|
|
120
|
+
stop(): Promise<void>;
|
|
121
|
+
listJobs(): Promise<RuntimeJobRecord<TPayload>[]>;
|
|
122
|
+
getJob?(jobId: string): Promise<RuntimeJobRecord<TPayload> | undefined>;
|
|
123
|
+
upsertJob(job: RuntimeJobRecord<TPayload>): Promise<RuntimeJobRecord<TPayload>>;
|
|
124
|
+
removeJob(jobId: string): Promise<boolean>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export { type RuntimeDriver as R, type RuntimeDriverContext as a, type RuntimeJobRecord as b, type RuntimeJobSchedule as c, type RuntimeJobScheduleInput as d, type RuntimeAtSchedule as e, type RuntimeAtScheduleInput as f, type RuntimeCronSchedule as g, type RuntimeDeadLetterEvent as h, type RuntimeDispatchInput as i, type RuntimeEverySchedule as j, type RuntimeExecutionContext as k, type RuntimeExecutionResult as l, type RuntimeExecutor as m, type RuntimeJobState as n, type RuntimeLogMeta as o, type RuntimeLogger as p, type RuntimeRetryPolicy as q, type RuntimeRetryStrategy as r, type RuntimeRunResult as s, type RuntimeRunStatus as t, type RuntimeScheduleInput as u, type RuntimeSchedulePatch as v, type RuntimeTrigger as w, createConsoleRuntimeLogger as x, silentRuntimeLogger as y };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { R as RuntimeDriver, a as RuntimeDriverContext, b as RuntimeJobRecord } from '../driver-KdyMlXQq.js';
|
|
2
|
+
|
|
3
|
+
interface InMemoryRuntimeDriverOptions {
|
|
4
|
+
maxTimerDelayMs?: number;
|
|
5
|
+
}
|
|
6
|
+
declare class InMemoryRuntimeDriver<TPayload = unknown> implements RuntimeDriver<TPayload> {
|
|
7
|
+
readonly name = "in-memory";
|
|
8
|
+
private readonly jobs;
|
|
9
|
+
private readonly scheduled;
|
|
10
|
+
private readonly maxTimerDelayMs;
|
|
11
|
+
private context;
|
|
12
|
+
private started;
|
|
13
|
+
constructor(options?: InMemoryRuntimeDriverOptions);
|
|
14
|
+
start(context: RuntimeDriverContext<TPayload>): Promise<void>;
|
|
15
|
+
stop(): Promise<void>;
|
|
16
|
+
listJobs(): Promise<RuntimeJobRecord<TPayload>[]>;
|
|
17
|
+
getJob(jobId: string): Promise<RuntimeJobRecord<TPayload> | undefined>;
|
|
18
|
+
upsertJob(job: RuntimeJobRecord<TPayload>): Promise<RuntimeJobRecord<TPayload>>;
|
|
19
|
+
removeJob(jobId: string): Promise<boolean>;
|
|
20
|
+
private now;
|
|
21
|
+
private clearScheduled;
|
|
22
|
+
private armJob;
|
|
23
|
+
private armTimeout;
|
|
24
|
+
private trigger;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { InMemoryRuntimeDriver };
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { w as RuntimeTrigger, b as RuntimeJobRecord, t as RuntimeRunStatus, h as RuntimeDeadLetterEvent, R as RuntimeDriver, m as RuntimeExecutor, p as RuntimeLogger, u as RuntimeScheduleInput, v as RuntimeSchedulePatch, s as RuntimeRunResult, i as RuntimeDispatchInput } from './driver-KdyMlXQq.js';
|
|
2
|
+
|
|
3
|
+
interface RuntimeLifecycleEvent {
|
|
4
|
+
driver: string;
|
|
5
|
+
at: number;
|
|
6
|
+
}
|
|
7
|
+
interface RuntimeDispatchQueuedEvent {
|
|
8
|
+
jobId: string;
|
|
9
|
+
trigger: RuntimeTrigger;
|
|
10
|
+
queuedAt: number;
|
|
11
|
+
queueLength: number;
|
|
12
|
+
}
|
|
13
|
+
interface RuntimeDispatchDroppedEvent {
|
|
14
|
+
jobId: string;
|
|
15
|
+
trigger: RuntimeTrigger;
|
|
16
|
+
droppedAt: number;
|
|
17
|
+
queueLength: number;
|
|
18
|
+
reason: string;
|
|
19
|
+
}
|
|
20
|
+
interface RuntimeRunStartEvent<TPayload = unknown> {
|
|
21
|
+
job: RuntimeJobRecord<TPayload>;
|
|
22
|
+
trigger: RuntimeTrigger;
|
|
23
|
+
startedAt: number;
|
|
24
|
+
}
|
|
25
|
+
interface RuntimeRunRetryEvent<TPayload = unknown> {
|
|
26
|
+
job: RuntimeJobRecord<TPayload>;
|
|
27
|
+
trigger: RuntimeTrigger;
|
|
28
|
+
attempt: number;
|
|
29
|
+
nextAttempt: number;
|
|
30
|
+
delayMs: number;
|
|
31
|
+
error?: string;
|
|
32
|
+
}
|
|
33
|
+
interface RuntimeRunCompleteEvent<TPayload = unknown> {
|
|
34
|
+
job: RuntimeJobRecord<TPayload>;
|
|
35
|
+
trigger: RuntimeTrigger;
|
|
36
|
+
startedAt: number;
|
|
37
|
+
endedAt: number;
|
|
38
|
+
attempts: number;
|
|
39
|
+
status: RuntimeRunStatus;
|
|
40
|
+
error?: string;
|
|
41
|
+
deadLettered?: boolean;
|
|
42
|
+
}
|
|
43
|
+
interface RuntimeObserver<TPayload = unknown> {
|
|
44
|
+
notifyRuntimeStart?(event: RuntimeLifecycleEvent): void | Promise<void>;
|
|
45
|
+
notifyRuntimeStop?(event: RuntimeLifecycleEvent): void | Promise<void>;
|
|
46
|
+
notifyDispatchQueued?(event: RuntimeDispatchQueuedEvent): void | Promise<void>;
|
|
47
|
+
notifyDispatchDropped?(event: RuntimeDispatchDroppedEvent): void | Promise<void>;
|
|
48
|
+
notifyRunStart?(event: RuntimeRunStartEvent<TPayload>): void | Promise<void>;
|
|
49
|
+
notifyRunRetry?(event: RuntimeRunRetryEvent<TPayload>): void | Promise<void>;
|
|
50
|
+
notifyRunComplete?(event: RuntimeRunCompleteEvent<TPayload>): void | Promise<void>;
|
|
51
|
+
notifyDeadLetter?(event: RuntimeDeadLetterEvent<TPayload>): void | Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface AgentRuntimeOptions<TPayload = unknown> {
|
|
55
|
+
driver: RuntimeDriver<TPayload>;
|
|
56
|
+
execute: RuntimeExecutor<TPayload>;
|
|
57
|
+
logger?: RuntimeLogger;
|
|
58
|
+
maxConcurrentRuns?: number;
|
|
59
|
+
maxQueuedDispatches?: number;
|
|
60
|
+
createId?: () => string;
|
|
61
|
+
executionTimeoutMs?: number;
|
|
62
|
+
abortInFlightOnStop?: boolean;
|
|
63
|
+
onDeadLetter?: (event: RuntimeDeadLetterEvent<TPayload>) => void | Promise<void>;
|
|
64
|
+
observers?: RuntimeObserver<TPayload>[];
|
|
65
|
+
}
|
|
66
|
+
declare class AgentRuntime<TPayload = unknown> {
|
|
67
|
+
private readonly driver;
|
|
68
|
+
private readonly executeJob;
|
|
69
|
+
private readonly logger;
|
|
70
|
+
private readonly maxConcurrentRuns;
|
|
71
|
+
private readonly maxQueuedDispatches;
|
|
72
|
+
private readonly createId;
|
|
73
|
+
private readonly executionTimeoutMs;
|
|
74
|
+
private readonly abortInFlightOnStop;
|
|
75
|
+
private readonly onDeadLetter;
|
|
76
|
+
private readonly observers;
|
|
77
|
+
private started;
|
|
78
|
+
private activeRuns;
|
|
79
|
+
private readonly inFlight;
|
|
80
|
+
private readonly runControllers;
|
|
81
|
+
private readonly pendingDispatches;
|
|
82
|
+
private readonly pendingDispatchByJobId;
|
|
83
|
+
constructor(options: AgentRuntimeOptions<TPayload>);
|
|
84
|
+
private forEachObserver;
|
|
85
|
+
start(): Promise<void>;
|
|
86
|
+
stop(): Promise<void>;
|
|
87
|
+
status(): {
|
|
88
|
+
started: boolean;
|
|
89
|
+
driver: string;
|
|
90
|
+
inFlightJobs: string[];
|
|
91
|
+
queuedJobs: string[];
|
|
92
|
+
maxConcurrentRuns: number;
|
|
93
|
+
maxQueuedDispatches?: number;
|
|
94
|
+
};
|
|
95
|
+
listJobs(): Promise<RuntimeJobRecord<TPayload>[]>;
|
|
96
|
+
getJob(jobId: string): Promise<RuntimeJobRecord<TPayload> | undefined>;
|
|
97
|
+
schedule(input: RuntimeScheduleInput<TPayload>): Promise<RuntimeJobRecord<TPayload>>;
|
|
98
|
+
update(jobId: string, patch: RuntimeSchedulePatch<TPayload>): Promise<RuntimeJobRecord<TPayload>>;
|
|
99
|
+
pause(jobId: string): Promise<RuntimeJobRecord<TPayload>>;
|
|
100
|
+
resume(jobId: string): Promise<RuntimeJobRecord<TPayload>>;
|
|
101
|
+
remove(jobId: string): Promise<boolean>;
|
|
102
|
+
runNow(jobId: string): Promise<RuntimeRunResult>;
|
|
103
|
+
dispatch(input: RuntimeDispatchInput): Promise<RuntimeRunResult>;
|
|
104
|
+
dispatchDue(jobId: string): Promise<RuntimeRunResult>;
|
|
105
|
+
private runJob;
|
|
106
|
+
private requireJob;
|
|
107
|
+
private tryReserveRun;
|
|
108
|
+
private enqueuePendingDispatch;
|
|
109
|
+
private executeJobAttempt;
|
|
110
|
+
private emitDeadLetter;
|
|
111
|
+
private executeReservedJob;
|
|
112
|
+
private releaseRunReservation;
|
|
113
|
+
private drainPendingDispatches;
|
|
114
|
+
}
|
|
115
|
+
declare function createAgentRuntime<TPayload = unknown>(options: AgentRuntimeOptions<TPayload>): AgentRuntime<TPayload>;
|
|
116
|
+
|
|
117
|
+
type OrchestratorRunStatus = "queued" | "running" | "completed" | "failed" | "cancelled";
|
|
118
|
+
type OrchestratorRunMode = "invoke" | "guide";
|
|
119
|
+
interface OrchestratorRunState<TResult = unknown> {
|
|
120
|
+
status: OrchestratorRunStatus;
|
|
121
|
+
running: boolean;
|
|
122
|
+
attempt: number;
|
|
123
|
+
guideCount: number;
|
|
124
|
+
startedAtMs?: number;
|
|
125
|
+
endedAtMs?: number;
|
|
126
|
+
error?: string;
|
|
127
|
+
result?: TResult;
|
|
128
|
+
}
|
|
129
|
+
interface OrchestratorRunRecord<TInput = unknown, TResult = unknown> {
|
|
130
|
+
id: string;
|
|
131
|
+
label: string;
|
|
132
|
+
input: TInput;
|
|
133
|
+
parentRunId?: string;
|
|
134
|
+
metadata?: Record<string, string>;
|
|
135
|
+
createdAt: string;
|
|
136
|
+
updatedAt: string;
|
|
137
|
+
state: OrchestratorRunState<TResult>;
|
|
138
|
+
}
|
|
139
|
+
interface OrchestratorJobPayload<TInput = unknown> {
|
|
140
|
+
runId: string;
|
|
141
|
+
input: TInput;
|
|
142
|
+
mode: OrchestratorRunMode;
|
|
143
|
+
guideMessage?: string;
|
|
144
|
+
}
|
|
145
|
+
interface OrchestratorInvokeInput<TInput = unknown> {
|
|
146
|
+
id?: string;
|
|
147
|
+
label?: string;
|
|
148
|
+
input: TInput;
|
|
149
|
+
parentRunId?: string;
|
|
150
|
+
metadata?: Record<string, string>;
|
|
151
|
+
}
|
|
152
|
+
interface OrchestratorInvokeResult<TInput = unknown, TResult = unknown> {
|
|
153
|
+
run: OrchestratorRunRecord<TInput, TResult>;
|
|
154
|
+
jobId: string;
|
|
155
|
+
}
|
|
156
|
+
interface OrchestratorWaitOptions {
|
|
157
|
+
timeoutMs?: number;
|
|
158
|
+
signal?: AbortSignal;
|
|
159
|
+
}
|
|
160
|
+
interface OrchestratorListOptions {
|
|
161
|
+
status?: OrchestratorRunStatus | OrchestratorRunStatus[];
|
|
162
|
+
parentRunId?: string;
|
|
163
|
+
limit?: number;
|
|
164
|
+
}
|
|
165
|
+
interface OrchestratorCloseOptions {
|
|
166
|
+
reason?: string;
|
|
167
|
+
}
|
|
168
|
+
interface OrchestratorGuideInput<TInput = unknown> {
|
|
169
|
+
runId: string;
|
|
170
|
+
message: string;
|
|
171
|
+
input?: TInput;
|
|
172
|
+
metadata?: Record<string, string>;
|
|
173
|
+
}
|
|
174
|
+
interface OrchestratorExecutionContext {
|
|
175
|
+
signal: AbortSignal;
|
|
176
|
+
trigger: RuntimeTrigger;
|
|
177
|
+
mode: OrchestratorRunMode;
|
|
178
|
+
attempt: number;
|
|
179
|
+
startedAt: number;
|
|
180
|
+
deadlineAt?: number;
|
|
181
|
+
guideMessage?: string;
|
|
182
|
+
}
|
|
183
|
+
type OrchestratorExecutor<TInput = unknown, TResult = unknown> = (run: OrchestratorRunRecord<TInput, TResult>, context: OrchestratorExecutionContext) => Promise<TResult> | TResult;
|
|
184
|
+
type OrchestratorGuidedInputResolver<TInput = unknown, TResult = unknown> = (params: {
|
|
185
|
+
run: OrchestratorRunRecord<TInput, TResult>;
|
|
186
|
+
guideMessage: string;
|
|
187
|
+
}) => TInput;
|
|
188
|
+
declare function isTerminalRunStatus(status: OrchestratorRunStatus): boolean;
|
|
189
|
+
|
|
190
|
+
interface OrchestratorRunStore<TInput = unknown, TResult = unknown> {
|
|
191
|
+
start?(): Promise<void>;
|
|
192
|
+
stop?(): Promise<void>;
|
|
193
|
+
get(runId: string): Promise<OrchestratorRunRecord<TInput, TResult> | undefined>;
|
|
194
|
+
list(): Promise<OrchestratorRunRecord<TInput, TResult>[]>;
|
|
195
|
+
upsert(run: OrchestratorRunRecord<TInput, TResult>): Promise<OrchestratorRunRecord<TInput, TResult>>;
|
|
196
|
+
remove(runId: string): Promise<boolean>;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
declare class InMemoryOrchestratorRunStore<TInput = unknown, TResult = unknown> implements OrchestratorRunStore<TInput, TResult> {
|
|
200
|
+
private readonly runs;
|
|
201
|
+
get(runId: string): Promise<OrchestratorRunRecord<TInput, TResult> | undefined>;
|
|
202
|
+
list(): Promise<OrchestratorRunRecord<TInput, TResult>[]>;
|
|
203
|
+
upsert(run: OrchestratorRunRecord<TInput, TResult>): Promise<OrchestratorRunRecord<TInput, TResult>>;
|
|
204
|
+
remove(runId: string): Promise<boolean>;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
interface AgentOrchestratorOptions<TInput = unknown, TResult = unknown> {
|
|
208
|
+
driver: RuntimeDriver<OrchestratorJobPayload<TInput>>;
|
|
209
|
+
execute: OrchestratorExecutor<TInput, TResult>;
|
|
210
|
+
runtime?: Omit<AgentRuntimeOptions<OrchestratorJobPayload<TInput>>, "driver" | "execute">;
|
|
211
|
+
store?: OrchestratorRunStore<TInput, TResult>;
|
|
212
|
+
logger?: RuntimeLogger;
|
|
213
|
+
createId?: () => string;
|
|
214
|
+
jobIdPrefix?: string;
|
|
215
|
+
runLabelPrefix?: string;
|
|
216
|
+
scheduleAheadMs?: number;
|
|
217
|
+
resolveGuidedInput?: OrchestratorGuidedInputResolver<TInput, TResult>;
|
|
218
|
+
}
|
|
219
|
+
declare class AgentOrchestrator<TInput = unknown, TResult = unknown> {
|
|
220
|
+
readonly runtime: AgentRuntime<OrchestratorJobPayload<TInput>>;
|
|
221
|
+
private readonly store;
|
|
222
|
+
private readonly executeRun;
|
|
223
|
+
private readonly logger;
|
|
224
|
+
private readonly createId;
|
|
225
|
+
private readonly jobIdPrefix;
|
|
226
|
+
private readonly runLabelPrefix;
|
|
227
|
+
private readonly scheduleAheadMs;
|
|
228
|
+
private readonly resolveGuidedInput;
|
|
229
|
+
private readonly waiters;
|
|
230
|
+
private readonly runControllers;
|
|
231
|
+
private readonly closeReasons;
|
|
232
|
+
private readonly pendingGuides;
|
|
233
|
+
constructor(options: AgentOrchestratorOptions<TInput, TResult>);
|
|
234
|
+
start(): Promise<void>;
|
|
235
|
+
stop(): Promise<void>;
|
|
236
|
+
status(): Promise<{
|
|
237
|
+
runtime: ReturnType<AgentRuntime<OrchestratorJobPayload<TInput>>["status"]>;
|
|
238
|
+
runs: {
|
|
239
|
+
total: number;
|
|
240
|
+
queued: number;
|
|
241
|
+
running: number;
|
|
242
|
+
completed: number;
|
|
243
|
+
failed: number;
|
|
244
|
+
cancelled: number;
|
|
245
|
+
};
|
|
246
|
+
}>;
|
|
247
|
+
invoke(input: OrchestratorInvokeInput<TInput>): Promise<OrchestratorInvokeResult<TInput, TResult>>;
|
|
248
|
+
get(runId: string): Promise<OrchestratorRunRecord<TInput, TResult> | undefined>;
|
|
249
|
+
listInvocations(options?: OrchestratorListOptions): Promise<OrchestratorRunRecord<TInput, TResult>[]>;
|
|
250
|
+
waitForInvocation(runId: string, options?: OrchestratorWaitOptions): Promise<OrchestratorRunRecord<TInput, TResult>>;
|
|
251
|
+
close(runId: string, options?: OrchestratorCloseOptions): Promise<OrchestratorRunRecord<TInput, TResult>>;
|
|
252
|
+
guide(input: OrchestratorGuideInput<TInput>): Promise<OrchestratorRunRecord<TInput, TResult>>;
|
|
253
|
+
private ensureStarted;
|
|
254
|
+
private toJobId;
|
|
255
|
+
private resolveStatusFilter;
|
|
256
|
+
private requireRun;
|
|
257
|
+
private failRunLaunch;
|
|
258
|
+
private failPendingRunLaunch;
|
|
259
|
+
private disableRunJob;
|
|
260
|
+
private launchRun;
|
|
261
|
+
private notifyWaiters;
|
|
262
|
+
private executeRuntimeJob;
|
|
263
|
+
}
|
|
264
|
+
declare function createAgentOrchestrator<TInput = unknown, TResult = unknown>(options: AgentOrchestratorOptions<TInput, TResult>): AgentOrchestrator<TInput, TResult>;
|
|
265
|
+
|
|
266
|
+
export { AgentOrchestrator as A, InMemoryOrchestratorRunStore as I, type OrchestratorCloseOptions as O, type RuntimeObserver as R, type AgentOrchestratorOptions as a, AgentRuntime as b, type AgentRuntimeOptions as c, type OrchestratorExecutionContext as d, type OrchestratorExecutor as e, type OrchestratorGuideInput as f, type OrchestratorGuidedInputResolver as g, type OrchestratorInvokeInput as h, type OrchestratorInvokeResult as i, type OrchestratorJobPayload as j, type OrchestratorListOptions as k, type OrchestratorRunMode as l, type OrchestratorRunRecord as m, type OrchestratorRunState as n, type OrchestratorRunStatus as o, type OrchestratorRunStore as p, type OrchestratorWaitOptions as q, type RuntimeDispatchDroppedEvent as r, type RuntimeDispatchQueuedEvent as s, type RuntimeLifecycleEvent as t, type RuntimeRunCompleteEvent as u, type RuntimeRunRetryEvent as v, type RuntimeRunStartEvent as w, createAgentOrchestrator as x, createAgentRuntime as y, isTerminalRunStatus as z };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { c as RuntimeJobSchedule, d as RuntimeJobScheduleInput } from './driver-KdyMlXQq.js';
|
|
2
|
+
export { e as RuntimeAtSchedule, f as RuntimeAtScheduleInput, g as RuntimeCronSchedule, h as RuntimeDeadLetterEvent, i as RuntimeDispatchInput, R as RuntimeDriver, a as RuntimeDriverContext, j as RuntimeEverySchedule, k as RuntimeExecutionContext, l as RuntimeExecutionResult, m as RuntimeExecutor, b as RuntimeJobRecord, n as RuntimeJobState, o as RuntimeLogMeta, p as RuntimeLogger, q as RuntimeRetryPolicy, r as RuntimeRetryStrategy, s as RuntimeRunResult, t as RuntimeRunStatus, u as RuntimeScheduleInput, v as RuntimeSchedulePatch, w as RuntimeTrigger, x as createConsoleRuntimeLogger, y as silentRuntimeLogger } from './driver-KdyMlXQq.js';
|
|
3
|
+
import { R as RuntimeObserver } from './index-CbLKNFMq.js';
|
|
4
|
+
export { A as AgentOrchestrator, a as AgentOrchestratorOptions, b as AgentRuntime, c as AgentRuntimeOptions, I as InMemoryOrchestratorRunStore, O as OrchestratorCloseOptions, d as OrchestratorExecutionContext, e as OrchestratorExecutor, f as OrchestratorGuideInput, g as OrchestratorGuidedInputResolver, h as OrchestratorInvokeInput, i as OrchestratorInvokeResult, j as OrchestratorJobPayload, k as OrchestratorListOptions, l as OrchestratorRunMode, m as OrchestratorRunRecord, n as OrchestratorRunState, o as OrchestratorRunStatus, p as OrchestratorRunStore, q as OrchestratorWaitOptions, r as RuntimeDispatchDroppedEvent, s as RuntimeDispatchQueuedEvent, t as RuntimeLifecycleEvent, u as RuntimeRunCompleteEvent, v as RuntimeRunRetryEvent, w as RuntimeRunStartEvent, x as createAgentOrchestrator, y as createAgentRuntime, z as isTerminalRunStatus } from './index-CbLKNFMq.js';
|
|
5
|
+
export { InMemoryRuntimeDriver } from './drivers/in-memory.js';
|
|
6
|
+
|
|
7
|
+
declare const PROMETHEUS_TEXT_CONTENT_TYPE = "text/plain; version=0.0.4; charset=utf-8";
|
|
8
|
+
interface PrometheusRuntimeMetricsOptions {
|
|
9
|
+
metricPrefix?: string;
|
|
10
|
+
defaultLabels?: Record<string, string>;
|
|
11
|
+
durationBucketsMs?: number[];
|
|
12
|
+
}
|
|
13
|
+
interface PrometheusMetricSample {
|
|
14
|
+
labels: Record<string, string>;
|
|
15
|
+
value: number;
|
|
16
|
+
}
|
|
17
|
+
interface PrometheusHistogramBucketSample {
|
|
18
|
+
le: number;
|
|
19
|
+
value: number;
|
|
20
|
+
}
|
|
21
|
+
interface PrometheusHistogramSample {
|
|
22
|
+
labels: Record<string, string>;
|
|
23
|
+
count: number;
|
|
24
|
+
sum: number;
|
|
25
|
+
buckets: PrometheusHistogramBucketSample[];
|
|
26
|
+
}
|
|
27
|
+
interface PrometheusRuntimeMetricsSnapshot {
|
|
28
|
+
counters: Record<string, PrometheusMetricSample[]>;
|
|
29
|
+
gauges: Record<string, PrometheusMetricSample[]>;
|
|
30
|
+
histograms: Record<string, PrometheusHistogramSample[]>;
|
|
31
|
+
}
|
|
32
|
+
interface PrometheusRuntimeMetrics<TPayload = unknown> {
|
|
33
|
+
observer: RuntimeObserver<TPayload>;
|
|
34
|
+
contentType: typeof PROMETHEUS_TEXT_CONTENT_TYPE;
|
|
35
|
+
snapshot(): PrometheusRuntimeMetricsSnapshot;
|
|
36
|
+
render(): string;
|
|
37
|
+
reset(): void;
|
|
38
|
+
}
|
|
39
|
+
declare function createPrometheusRuntimeMetrics<TPayload = unknown>(options?: PrometheusRuntimeMetricsOptions): PrometheusRuntimeMetrics<TPayload>;
|
|
40
|
+
|
|
41
|
+
declare function normalizeSchedule(input: RuntimeJobScheduleInput): RuntimeJobSchedule;
|
|
42
|
+
declare function computeNextRunAtMs(schedule: RuntimeJobSchedule, nowMs: number): number | undefined;
|
|
43
|
+
|
|
44
|
+
export { PROMETHEUS_TEXT_CONTENT_TYPE, type PrometheusHistogramBucketSample, type PrometheusHistogramSample, type PrometheusMetricSample, type PrometheusRuntimeMetrics, type PrometheusRuntimeMetricsOptions, type PrometheusRuntimeMetricsSnapshot, RuntimeJobSchedule, RuntimeJobScheduleInput, RuntimeObserver, computeNextRunAtMs, createPrometheusRuntimeMetrics, normalizeSchedule };
|