@drej/core 0.3.0 → 0.4.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/dist/ledger.d.ts DELETED
@@ -1,102 +0,0 @@
1
- /** Status of a workflow run derived from its ledger events. */
2
- export declare enum RunStatus {
3
- Running = "running",
4
- Completed = "completed",
5
- Failed = "failed",
6
- Cancelled = "cancelled"
7
- }
8
- /** Derived metadata for a workflow run, computed from its ledger events. */
9
- export interface RunDetails {
10
- workflowName: string;
11
- runId: string;
12
- status: RunStatus;
13
- /** Unix timestamp (ms) of the `run_started` event. */
14
- startedAt: number;
15
- /** Unix timestamp (ms) of the terminal event, if the run has ended. */
16
- completedAt?: number;
17
- /** Number of steps that completed successfully. */
18
- stepCount: number;
19
- /** Error message, present when status is `Failed`. */
20
- error?: string;
21
- }
22
- /** Options for filtering run listings. */
23
- export interface ListRunsOptions {
24
- status?: RunStatus;
25
- /** Max number of results to return. */
26
- limit?: number;
27
- /** Return only runs that started before this Unix timestamp (ms). */
28
- before?: number;
29
- }
30
- /** Events emitted during workflow execution and stored in the ledger. */
31
- export declare enum LedgerEvent {
32
- /** Emitted once when a workflow run starts, before any steps execute. */
33
- RunStarted = "run_started",
34
- /** Emitted at the beginning of each step. */
35
- StepStart = "step_start",
36
- /** Emitted when a step finishes successfully. */
37
- StepComplete = "step_complete",
38
- /** Emitted when a step throws an unrecoverable error. */
39
- StepFailed = "step_failed",
40
- /** Emitted when a step's rollback handler completes during saga compensation. */
41
- StepRolledBack = "step_rolled_back",
42
- /** Emitted after all steps finish without error. */
43
- WorkflowComplete = "workflow_complete",
44
- /** Emitted after rollback completes following a step failure. */
45
- WorkflowFailed = "workflow_failed",
46
- /** Durable resumption point written after each successful step. */
47
- Checkpoint = "checkpoint",
48
- /** Streaming output chunk from `exec()` or `execCode()`. */
49
- ExecEvent = "exec_event",
50
- /** Emitted when a sandbox snapshot is captured mid-run. */
51
- Snapshot = "snapshot"
52
- }
53
- /** A single event record written to the storage adapter during a workflow run. */
54
- export interface LedgerEntry {
55
- /** Unix timestamp in milliseconds. */
56
- ts: number;
57
- workflowName: string;
58
- runId: string;
59
- /** Zero-based index of the step that produced this event. `-1` for run-level events. */
60
- stepIndex: number;
61
- /** Parallel branch index, when this step is inside a `parallel()` block. */
62
- branch?: number;
63
- event: LedgerEvent;
64
- /** Event-specific data (step output, snapshot ID, exec text, etc.). */
65
- payload?: unknown;
66
- /** Error message when `event` is `StepFailed` or `WorkflowFailed`. */
67
- error?: string;
68
- }
69
- /**
70
- * Persistence interface for workflow event storage.
71
- *
72
- * Implement this interface to plug in any storage backend. drej ships two
73
- * official implementations: `@drej/sqlite` (local dev, zero infra) and
74
- * `@drej/postgres` (production).
75
- *
76
- * @example
77
- * ```ts
78
- * import { SQLiteAdapter } from "@drej/sqlite";
79
- * const client = new DrejClient({ baseUrl, adapter: new SQLiteAdapter("./drej.db") });
80
- * await client.connect();
81
- * ```
82
- */
83
- export interface IStorageAdapter {
84
- /** Run migrations / open connections. Must be called before first use. */
85
- connect?(): Promise<void>;
86
- /** Release connections and resources. Call on graceful shutdown. */
87
- close?(): Promise<void>;
88
- /** Persist a single ledger event. Called automatically during workflow execution. */
89
- append(entry: LedgerEntry): Promise<void>;
90
- /** Return all events for a specific run, in ascending timestamp order. */
91
- readAll(workflowName: string, runId: string): Promise<LedgerEntry[]>;
92
- /** Return the most recent checkpoint entry for a run, or `null` if none exists. */
93
- lastCheckpoint(workflowName: string, runId: string): Promise<LedgerEntry | null>;
94
- /** Return details for all runs of a workflow, newest first. */
95
- listRunDetails(workflowName: string, opts?: ListRunsOptions): Promise<RunDetails[]>;
96
- /** Return details for runs across all workflows, newest first. */
97
- listAllRunDetails(opts?: ListRunsOptions): Promise<RunDetails[]>;
98
- /** Return details for a single run, or `null` if not found. */
99
- getRunDetails(workflowName: string, runId: string): Promise<RunDetails | null>;
100
- /** Delete all ledger events for a run. */
101
- deleteRun(workflowName: string, runId: string): Promise<void>;
102
- }
package/dist/logger.d.ts DELETED
@@ -1,23 +0,0 @@
1
- export declare enum LogLevel {
2
- Debug = 0,
3
- Info = 1,
4
- Warn = 2,
5
- Error = 3,
6
- Silent = 4
7
- }
8
- export interface ILogger {
9
- debug(msg: string, meta?: Record<string, unknown>): void;
10
- info(msg: string, meta?: Record<string, unknown>): void;
11
- warn(msg: string, meta?: Record<string, unknown>): void;
12
- error(msg: string, meta?: Record<string, unknown>): void;
13
- }
14
- export declare class ConsoleLogger implements ILogger {
15
- private readonly minLevel;
16
- constructor(minLevel?: LogLevel);
17
- private log;
18
- debug(msg: string, meta?: Record<string, unknown>): void;
19
- info(msg: string, meta?: Record<string, unknown>): void;
20
- warn(msg: string, meta?: Record<string, unknown>): void;
21
- error(msg: string, meta?: Record<string, unknown>): void;
22
- }
23
- export declare const noopLogger: ILogger;
@@ -1,19 +0,0 @@
1
- import type { WorkflowStep } from "../workflow";
2
- import { StepType, type StepDef } from "./types";
3
- type BuildStepFn = (def: StepDef) => WorkflowStep;
4
- export declare function buildRetryStep(def: Extract<StepDef, {
5
- type: StepType.Retry;
6
- }>, buildStepFn: BuildStepFn): WorkflowStep;
7
- export declare function buildConditionalStep(def: Extract<StepDef, {
8
- type: StepType.Conditional;
9
- }>, buildStepFn: BuildStepFn): WorkflowStep;
10
- export declare function buildLoopStep(def: Extract<StepDef, {
11
- type: StepType.Loop;
12
- }>, buildStepFn: BuildStepFn): WorkflowStep;
13
- export declare function buildParallelStep(def: Extract<StepDef, {
14
- type: StepType.Parallel;
15
- }>, buildStepFn: BuildStepFn): WorkflowStep;
16
- export declare function buildSequenceStep(def: Extract<StepDef, {
17
- type: StepType.Sequence;
18
- }>, buildStepFn: BuildStepFn): WorkflowStep;
19
- export {};
@@ -1,8 +0,0 @@
1
- import type { WorkflowStep } from "../workflow";
2
- import { StepType, type StepDef } from "./types";
3
- export declare function buildExecCommandStep(def: Extract<StepDef, {
4
- type: StepType.ExecCommand;
5
- }>): WorkflowStep;
6
- export declare function buildExecCodeStep(def: Extract<StepDef, {
7
- type: StepType.ExecCode;
8
- }>): WorkflowStep;
@@ -1,35 +0,0 @@
1
- import type { WorkflowStep } from "../workflow";
2
- import { StepType, type StepDef } from "./types";
3
- export declare function buildWriteFileStep(def: Extract<StepDef, {
4
- type: StepType.WriteFile;
5
- }>): WorkflowStep;
6
- export declare function buildReadFileStep(def: Extract<StepDef, {
7
- type: StepType.ReadFile;
8
- }>): WorkflowStep;
9
- export declare function buildDeleteFileStep(def: Extract<StepDef, {
10
- type: StepType.DeleteFile;
11
- }>): WorkflowStep;
12
- export declare function buildMoveFileStep(def: Extract<StepDef, {
13
- type: StepType.MoveFile;
14
- }>): WorkflowStep;
15
- export declare function buildListDirectoryStep(def: Extract<StepDef, {
16
- type: StepType.ListDirectory;
17
- }>): WorkflowStep;
18
- export declare function buildSearchFilesStep(def: Extract<StepDef, {
19
- type: StepType.SearchFiles;
20
- }>): WorkflowStep;
21
- export declare function buildCreateDirectoryStep(def: Extract<StepDef, {
22
- type: StepType.CreateDirectory;
23
- }>): WorkflowStep;
24
- export declare function buildDeleteDirectoryStep(def: Extract<StepDef, {
25
- type: StepType.DeleteDirectory;
26
- }>): WorkflowStep;
27
- export declare function buildSetPermissionsStep(def: Extract<StepDef, {
28
- type: StepType.SetPermissions;
29
- }>): WorkflowStep;
30
- export declare function buildReplaceInFilesStep(def: Extract<StepDef, {
31
- type: StepType.ReplaceInFiles;
32
- }>): WorkflowStep;
33
- export declare function buildGetFileInfoStep(def: Extract<StepDef, {
34
- type: StepType.GetFileInfo;
35
- }>): WorkflowStep;
@@ -1,7 +0,0 @@
1
- import type { WorkflowStep } from "../workflow";
2
- import { type StepDef } from "./types";
3
- export declare function buildStep(def: StepDef): WorkflowStep;
4
- export { StepType, Encoding, Backoff } from "./types";
5
- export type { StepDef, Predicate, WorkflowState, SnapshotConfig } from "./types";
6
- export { resolveExecClient } from "./sandbox";
7
- export { shouldSnapshot, waitForSnapshot } from "./snapshot";
@@ -1,8 +0,0 @@
1
- import { ControlClient, ExecClient } from "@drej/opensandbox";
2
- import type { WorkflowStep } from "../workflow";
3
- import { StepType, type StepDef } from "./types";
4
- export declare function resolveExecClient(control: ControlClient, sandboxId: string, retries?: number, delayMs?: number): Promise<ExecClient>;
5
- export declare function buildCreateSandboxStep(def: Extract<StepDef, {
6
- type: StepType.CreateSandbox;
7
- }>): WorkflowStep;
8
- export declare function buildDeleteSandboxStep(): WorkflowStep;
@@ -1,6 +0,0 @@
1
- import type { ControlClient } from "@drej/opensandbox";
2
- import type { WorkflowStep } from "../workflow";
3
- import { type SnapshotConfig } from "./types";
4
- export declare function shouldSnapshot(config: SnapshotConfig, stepIndex: number): boolean;
5
- export declare function waitForSnapshot(control: ControlClient, snapshotId: string, timeoutMs?: number): Promise<void>;
6
- export declare function buildSnapshotStep(): WorkflowStep;
@@ -1,178 +0,0 @@
1
- import { CodeLanguage } from "@drej/opensandbox";
2
- export type Predicate = {
3
- op: "eq" | "neq";
4
- field: string;
5
- value: unknown;
6
- } | {
7
- op: "gt" | "lt" | "gte" | "lte";
8
- field: string;
9
- value: number;
10
- } | {
11
- op: "exists" | "not_exists";
12
- field: string;
13
- } | {
14
- op: "and" | "or";
15
- predicates: Predicate[];
16
- };
17
- export declare enum StepType {
18
- CreateSandbox = "create_sandbox",
19
- ExecCode = "exec_code",
20
- ExecCommand = "exec_command",
21
- DeleteSandbox = "delete_sandbox",
22
- WriteFile = "write_file",
23
- ReadFile = "read_file",
24
- DeleteFile = "delete_file",
25
- MoveFile = "move_file",
26
- ListDirectory = "list_directory",
27
- SearchFiles = "search_files",
28
- CreateDirectory = "create_directory",
29
- DeleteDirectory = "delete_directory",
30
- SetPermissions = "set_permissions",
31
- ReplaceInFiles = "replace_in_files",
32
- GetFileInfo = "get_file_info",
33
- Snapshot = "snapshot",
34
- Retry = "retry",
35
- Conditional = "conditional",
36
- Loop = "loop",
37
- Parallel = "parallel",
38
- Sequence = "sequence"
39
- }
40
- export declare enum Encoding {
41
- UTF8 = "utf8",
42
- Base64 = "base64"
43
- }
44
- export declare enum Backoff {
45
- Fixed = "fixed",
46
- Exponential = "exponential"
47
- }
48
- export type StepDef = {
49
- type: StepType.CreateSandbox;
50
- image?: {
51
- uri: string;
52
- auth?: {
53
- username: string;
54
- password: string;
55
- };
56
- };
57
- snapshotId?: string;
58
- timeout?: number;
59
- entrypoint?: string[];
60
- env?: Record<string, string>;
61
- metadata?: Record<string, string>;
62
- resourceLimits?: {
63
- cpu?: string;
64
- memory?: string;
65
- gpu?: string;
66
- };
67
- } | {
68
- type: StepType.ExecCode;
69
- code: string;
70
- context?: {
71
- id: string;
72
- language: CodeLanguage;
73
- };
74
- timeoutMs?: number;
75
- } | {
76
- type: StepType.ExecCommand;
77
- command: string;
78
- cwd?: string;
79
- envs?: Record<string, string>;
80
- capture?: string;
81
- strict?: boolean;
82
- timeoutMs?: number;
83
- } | {
84
- type: StepType.DeleteSandbox;
85
- } | {
86
- type: StepType.WriteFile;
87
- path: string;
88
- content: string;
89
- encoding?: Encoding;
90
- timeoutMs?: number;
91
- } | {
92
- type: StepType.ReadFile;
93
- path: string;
94
- as: string;
95
- encoding?: Encoding;
96
- timeoutMs?: number;
97
- } | {
98
- type: StepType.DeleteFile;
99
- path: string;
100
- timeoutMs?: number;
101
- } | {
102
- type: StepType.MoveFile;
103
- from: string;
104
- to: string;
105
- timeoutMs?: number;
106
- } | {
107
- type: StepType.ListDirectory;
108
- path: string;
109
- as: string;
110
- depth?: number;
111
- timeoutMs?: number;
112
- } | {
113
- type: StepType.SearchFiles;
114
- pattern: string;
115
- as: string;
116
- dir?: string;
117
- timeoutMs?: number;
118
- } | {
119
- type: StepType.CreateDirectory;
120
- path: string;
121
- timeoutMs?: number;
122
- } | {
123
- type: StepType.DeleteDirectory;
124
- path: string;
125
- timeoutMs?: number;
126
- } | {
127
- type: StepType.SetPermissions;
128
- path: string;
129
- mode: string;
130
- timeoutMs?: number;
131
- } | {
132
- type: StepType.ReplaceInFiles;
133
- replacements: Array<{
134
- path: string;
135
- old: string;
136
- new: string;
137
- }>;
138
- timeoutMs?: number;
139
- } | {
140
- type: StepType.GetFileInfo;
141
- path: string;
142
- as: string;
143
- timeoutMs?: number;
144
- } | {
145
- type: StepType.Snapshot;
146
- } | {
147
- type: StepType.Retry;
148
- step: StepDef;
149
- maxAttempts: number;
150
- delayMs?: number;
151
- backoff?: Backoff;
152
- } | {
153
- type: StepType.Conditional;
154
- condition: Predicate;
155
- then: StepDef[];
156
- else?: StepDef[];
157
- } | {
158
- type: StepType.Loop;
159
- over?: string;
160
- items?: unknown[];
161
- as: string;
162
- steps: StepDef[];
163
- maxConcurrency?: number;
164
- } | {
165
- type: StepType.Parallel;
166
- steps: StepDef[];
167
- maxConcurrency?: number;
168
- } | {
169
- type: StepType.Sequence;
170
- steps: StepDef[];
171
- };
172
- export type WorkflowState = Record<string, unknown> & {
173
- sandboxId?: string;
174
- };
175
- export interface SnapshotConfig {
176
- afterSteps?: number[];
177
- everyNSteps?: number;
178
- }
@@ -1,5 +0,0 @@
1
- import type { Predicate, WorkflowState } from "./types";
2
- export declare function getPath(obj: unknown, path: string): unknown;
3
- export declare function interpolate(template: string, state: WorkflowState): string;
4
- export declare function evaluate(predicate: Predicate, state: unknown): boolean;
5
- export declare function runWithConcurrency<T>(tasks: Array<() => Promise<T>>, max: number): Promise<T[]>;
@@ -1,2 +0,0 @@
1
- import { type StepDef } from "./steps";
2
- export declare function validateWorkflow(name: string, steps: StepDef[]): void;
@@ -1,106 +0,0 @@
1
- import type { IStorageAdapter, LedgerEntry } from "./ledger";
2
- import type { ControlClient, ExecClient } from "@drej/opensandbox";
3
- import type { ILogger } from "./logger";
4
- export interface WorkflowRunContext {
5
- readonly workflowName: string;
6
- readonly runId: string;
7
- readonly stepIndex: number;
8
- readonly control: ControlClient;
9
- readonly resolveExec: (sandboxId: string) => Promise<ExecClient>;
10
- emit(entry: LedgerEntry): Promise<void>;
11
- }
12
- export interface WorkflowStep {
13
- readonly id: string;
14
- /**
15
- * Maximum milliseconds this step may run. When exceeded, an `AbortSignal`
16
- * fires on the step's execution context and `StepTimeoutError` is thrown.
17
- * Set per-step in the builder (`exec("cmd", { timeoutMs: 5000 })`) or as a
18
- * global default via `RunOptions.stepTimeoutMs`.
19
- */
20
- readonly timeoutMs?: number;
21
- run(input: unknown, ctx: WorkflowRunContext): Promise<unknown>;
22
- rollback?(output: unknown, ctx: WorkflowRunContext): Promise<void>;
23
- }
24
- export interface WorkflowCheckpoint {
25
- workflowName: string;
26
- runId: string;
27
- stepIndex: number;
28
- completedSteps: Record<number, {
29
- output: unknown;
30
- completedAt: number;
31
- }>;
32
- timestamp: number;
33
- }
34
- export declare enum WorkflowStatus {
35
- Idle = "idle",
36
- Running = "running",
37
- Completed = "completed",
38
- Failed = "failed",
39
- RolledBack = "rolled_back"
40
- }
41
- export interface WorkflowHookInfo {
42
- workflowName: string;
43
- runId: string;
44
- }
45
- export interface StepHookInfo extends WorkflowHookInfo {
46
- stepIndex: number;
47
- stepId: string;
48
- }
49
- export interface StepCompleteHookInfo extends StepHookInfo {
50
- output: unknown;
51
- }
52
- export interface StepFailedHookInfo extends StepHookInfo {
53
- error: Error;
54
- }
55
- export interface WorkflowCompleteHookInfo extends WorkflowHookInfo {
56
- output: unknown;
57
- }
58
- export interface WorkflowFailedHookInfo extends WorkflowHookInfo {
59
- error: Error;
60
- }
61
- export interface WorkflowHooks {
62
- onWorkflowStart?(info: WorkflowHookInfo): void | Promise<void>;
63
- onStepStart?(info: StepHookInfo): void | Promise<void>;
64
- onStepComplete?(info: StepCompleteHookInfo): void | Promise<void>;
65
- onStepFailed?(info: StepFailedHookInfo): void | Promise<void>;
66
- onStepRolledBack?(info: StepHookInfo): void | Promise<void>;
67
- onWorkflowComplete?(info: WorkflowCompleteHookInfo): void | Promise<void>;
68
- onWorkflowFailed?(info: WorkflowFailedHookInfo): void | Promise<void>;
69
- }
70
- export declare function mergeHooks(...hooks: (WorkflowHooks | undefined)[]): WorkflowHooks;
71
- export interface WorkflowDeps {
72
- control: ControlClient;
73
- resolveExec: (sandboxId: string) => Promise<ExecClient>;
74
- adapter: IStorageAdapter;
75
- logger?: ILogger;
76
- hooks?: WorkflowHooks;
77
- /** Global per-step timeout in ms. Applied to any step that does not set its own `timeoutMs`. */
78
- stepTimeoutMs?: number;
79
- /**
80
- * External abort signal. When fired, the current in-flight step is aborted
81
- * via its per-step `AbortController` and `StepTimeoutError` (or the signal's
82
- * reason) propagates out of `Workflow.run()`.
83
- */
84
- signal?: AbortSignal;
85
- }
86
- export declare class Workflow {
87
- private readonly steps;
88
- private readonly deps;
89
- readonly name: string;
90
- readonly runId: string;
91
- private _status;
92
- private readonly completedSteps;
93
- private readonly log;
94
- get status(): WorkflowStatus;
95
- constructor(name: string, runId: string, steps: WorkflowStep[], deps: WorkflowDeps);
96
- private callHook;
97
- run(input: unknown, startFromStep?: number): Promise<unknown>;
98
- rollback(toStep?: number): Promise<void>;
99
- snapshot(): WorkflowCheckpoint;
100
- static resumeFromLedger(workflowName: string, runId: string, steps: WorkflowStep[], deps: WorkflowDeps): Promise<{
101
- workflow: Workflow;
102
- nextStep: number;
103
- lastOutput: unknown;
104
- }>;
105
- private makeContext;
106
- }