@aikirun/workflow 0.15.0 → 0.16.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/README.md CHANGED
@@ -52,7 +52,6 @@ Run workflows on a schedule using cron expressions or intervals:
52
52
  import { schedule } from "@aikirun/workflow";
53
53
 
54
54
  const dailyReport = schedule({
55
- name: "daily-report",
56
55
  type: "cron",
57
56
  expression: "0 9 * * *", // Every day at 9 AM
58
57
  });
package/dist/index.d.ts CHANGED
@@ -10,7 +10,7 @@ import { Serializable } from '@aikirun/types/serializable';
10
10
  import { DistributiveOmit, RequireAtLeastOneProp } from '@aikirun/types/utils';
11
11
  import { TaskId } from '@aikirun/types/task';
12
12
  import { WorkflowRunStateRequest, WorkflowRunTransitionTaskStateRequestV1 } from '@aikirun/types/workflow-run-api';
13
- import { OverlapPolicy, ScheduleName, ScheduleId } from '@aikirun/types/schedule';
13
+ import { OverlapPolicy, ScheduleActivateOptions, ScheduleId } from '@aikirun/types/schedule';
14
14
 
15
15
  type NonEmptyArray<T> = [T, ...T[]];
16
16
 
@@ -319,18 +319,19 @@ interface IntervalScheduleParams {
319
319
  type ScheduleParams = CronScheduleParams | IntervalScheduleParams;
320
320
  interface ScheduleHandle {
321
321
  id: ScheduleId;
322
- name: ScheduleName;
323
322
  pause(): Promise<void>;
324
323
  resume(): Promise<void>;
325
324
  delete(): Promise<void>;
326
325
  }
326
+ interface ScheduleBuilder {
327
+ opt<Path extends PathFromObject<ScheduleActivateOptions>>(path: Path, value: TypeOfValueAtPath<ScheduleActivateOptions, Path>): ScheduleBuilder;
328
+ activate<Input, Output, AppContext, TEvents extends EventsDefinition>(client: Client<AppContext>, workflow: WorkflowVersion<Input, Output, AppContext, TEvents>, ...args: Input extends void ? [] : [Input]): Promise<ScheduleHandle>;
329
+ }
327
330
  type ScheduleDefinition = ScheduleParams & {
328
- name: ScheduleName;
331
+ with(): ScheduleBuilder;
329
332
  activate<Input, Output, AppContext, TEvents extends EventsDefinition>(client: Client<AppContext>, workflow: WorkflowVersion<Input, Output, AppContext, TEvents>, ...args: Input extends void ? [] : [Input]): Promise<ScheduleHandle>;
330
333
  };
331
- declare function schedule(params: {
332
- name: string;
333
- } & ScheduleParams): ScheduleDefinition;
334
+ declare function schedule(params: ScheduleParams): ScheduleDefinition;
334
335
 
335
336
  /**
336
337
  * Defines a durable workflow with versioning and multiple task execution.
package/dist/index.js CHANGED
@@ -292,8 +292,8 @@ function getRetryParams(attempts, strategy) {
292
292
  import { INTERNAL } from "@aikirun/types/symbols";
293
293
  import { SchemaValidationError } from "@aikirun/types/validator";
294
294
  import {
295
- WorkflowRunConflictError,
296
295
  WorkflowRunFailedError,
296
+ WorkflowRunRevisionConflictError,
297
297
  WorkflowRunSuspendedError
298
298
  } from "@aikirun/types/workflow-run";
299
299
  function event(params) {
@@ -361,7 +361,7 @@ function createEventWaiter(handle, eventName, schema, logger) {
361
361
  ...timeoutInMs !== void 0 ? { "aiki.timeoutInMs": timeoutInMs } : {}
362
362
  });
363
363
  } catch (error) {
364
- if (error instanceof WorkflowRunConflictError) {
364
+ if (error instanceof WorkflowRunRevisionConflictError) {
365
365
  throw new WorkflowRunSuspendedError(handle.run.id);
366
366
  }
367
367
  throw error;
@@ -485,8 +485,8 @@ function createEventMulticaster(workflowName, workflowVersionId, eventName, sche
485
485
  import { INTERNAL as INTERNAL2 } from "@aikirun/types/symbols";
486
486
  import {
487
487
  isTerminalWorkflowRunStatus,
488
- WorkflowRunConflictError as WorkflowRunConflictError2,
489
- WorkflowRunNotExecutableError
488
+ WorkflowRunNotExecutableError,
489
+ WorkflowRunRevisionConflictError as WorkflowRunRevisionConflictError2
490
490
  } from "@aikirun/types/workflow-run";
491
491
  async function workflowRunHandle(client, runOrId, eventsDefinition, logger) {
492
492
  const run = typeof runOrId !== "string" ? runOrId : (await client.api.workflowRun.getByIdV1({ id: runOrId })).run;
@@ -622,7 +622,7 @@ var WorkflowRunHandleImpl = class {
622
622
  this._run = run;
623
623
  } catch (error) {
624
624
  if (isConflictError(error)) {
625
- throw new WorkflowRunConflictError2(this.run.id);
625
+ throw new WorkflowRunRevisionConflictError2(this.run.id);
626
626
  }
627
627
  throw error;
628
628
  }
@@ -638,7 +638,7 @@ var WorkflowRunHandleImpl = class {
638
638
  return { taskId };
639
639
  } catch (error) {
640
640
  if (isConflictError(error)) {
641
- throw new WorkflowRunConflictError2(this.run.id);
641
+ throw new WorkflowRunRevisionConflictError2(this.run.id);
642
642
  }
643
643
  throw error;
644
644
  }
@@ -656,7 +656,10 @@ function isConflictError(error) {
656
656
 
657
657
  // run/sleeper.ts
658
658
  import { INTERNAL as INTERNAL3 } from "@aikirun/types/symbols";
659
- import { WorkflowRunConflictError as WorkflowRunConflictError3, WorkflowRunSuspendedError as WorkflowRunSuspendedError2 } from "@aikirun/types/workflow-run";
659
+ import {
660
+ WorkflowRunRevisionConflictError as WorkflowRunRevisionConflictError3,
661
+ WorkflowRunSuspendedError as WorkflowRunSuspendedError2
662
+ } from "@aikirun/types/workflow-run";
660
663
  var MAX_SLEEP_YEARS = 10;
661
664
  var MAX_SLEEP_MS = MAX_SLEEP_YEARS * 365 * 24 * 60 * 60 * 1e3;
662
665
  function createSleeper(handle, logger) {
@@ -678,7 +681,7 @@ function createSleeper(handle, logger) {
678
681
  "aiki.durationMs": durationMs
679
682
  });
680
683
  } catch (error) {
681
- if (error instanceof WorkflowRunConflictError3) {
684
+ if (error instanceof WorkflowRunRevisionConflictError3) {
682
685
  throw new WorkflowRunSuspendedError2(handle.run.id);
683
686
  }
684
687
  throw error;
@@ -726,7 +729,7 @@ function createSleeper(handle, logger) {
726
729
  "aiki.durationMs": durationMs
727
730
  });
728
731
  } catch (error) {
729
- if (error instanceof WorkflowRunConflictError3) {
732
+ if (error instanceof WorkflowRunRevisionConflictError3) {
730
733
  throw new WorkflowRunSuspendedError2(handle.run.id);
731
734
  }
732
735
  throw error;
@@ -737,48 +740,61 @@ function createSleeper(handle, logger) {
737
740
 
738
741
  // schedule.ts
739
742
  function schedule(params) {
740
- const { name, ...scheduleParams } = params;
743
+ async function activateWithOpts(client, workflow2, options, ...args) {
744
+ const input = args[0];
745
+ let scheduleSpec;
746
+ if (params.type === "interval") {
747
+ const { every, ...rest } = params;
748
+ scheduleSpec = {
749
+ ...rest,
750
+ everyMs: toMilliseconds(every)
751
+ };
752
+ } else {
753
+ scheduleSpec = params;
754
+ }
755
+ const { schedule: schedule2 } = await client.api.schedule.activateV1({
756
+ workflowName: workflow2.name,
757
+ workflowVersionId: workflow2.versionId,
758
+ spec: scheduleSpec,
759
+ input,
760
+ options
761
+ });
762
+ client.logger.info("Schedule activated", {
763
+ scheduleSpec,
764
+ workflowName: workflow2.name,
765
+ workflowVersionId: workflow2.versionId,
766
+ referenceId: options?.reference?.id
767
+ });
768
+ const scheduleId = schedule2.id;
769
+ return {
770
+ id: scheduleId,
771
+ pause: async () => {
772
+ await client.api.schedule.pauseV1({ id: scheduleId });
773
+ },
774
+ resume: async () => {
775
+ await client.api.schedule.resumeV1({ id: scheduleId });
776
+ },
777
+ delete: async () => {
778
+ await client.api.schedule.deleteV1({ id: scheduleId });
779
+ }
780
+ };
781
+ }
782
+ function createBuilder(optsBuilder) {
783
+ return {
784
+ opt: (path, value) => createBuilder(optsBuilder.with(path, value)),
785
+ async activate(client, workflow2, ...args) {
786
+ return activateWithOpts(client, workflow2, optsBuilder.build(), ...args);
787
+ }
788
+ };
789
+ }
741
790
  return {
742
- name,
743
- ...scheduleParams,
791
+ ...params,
792
+ with() {
793
+ const optsOverrider = objectOverrider({});
794
+ return createBuilder(optsOverrider());
795
+ },
744
796
  async activate(client, workflow2, ...args) {
745
- const input = args[0];
746
- let scheduleSpec;
747
- if (scheduleParams.type === "interval") {
748
- const { every, ...rest } = scheduleParams;
749
- scheduleSpec = {
750
- ...rest,
751
- everyMs: toMilliseconds(every)
752
- };
753
- } else {
754
- scheduleSpec = scheduleParams;
755
- }
756
- const { schedule: schedule2 } = await client.api.schedule.activateV1({
757
- name,
758
- workflowName: workflow2.name,
759
- workflowVersionId: workflow2.versionId,
760
- spec: scheduleSpec,
761
- input
762
- });
763
- client.logger.info("Schedule activated", {
764
- scheduleSpec,
765
- workflowName: workflow2.name,
766
- workflowVersionId: workflow2.versionId
767
- });
768
- const scheduleId = schedule2.id;
769
- return {
770
- id: scheduleId,
771
- name,
772
- pause: async () => {
773
- await client.api.schedule.pauseV1({ id: scheduleId });
774
- },
775
- resume: async () => {
776
- await client.api.schedule.resumeV1({ id: scheduleId });
777
- },
778
- delete: async () => {
779
- await client.api.schedule.deleteV1({ id: scheduleId });
780
- }
781
- };
797
+ return activateWithOpts(client, workflow2, {}, ...args);
782
798
  }
783
799
  };
784
800
  }
@@ -796,8 +812,8 @@ import { INTERNAL as INTERNAL5 } from "@aikirun/types/symbols";
796
812
  import { TaskFailedError } from "@aikirun/types/task";
797
813
  import { SchemaValidationError as SchemaValidationError2 } from "@aikirun/types/validator";
798
814
  import {
799
- WorkflowRunConflictError as WorkflowRunConflictError5,
800
815
  WorkflowRunFailedError as WorkflowRunFailedError2,
816
+ WorkflowRunRevisionConflictError as WorkflowRunRevisionConflictError5,
801
817
  WorkflowRunSuspendedError as WorkflowRunSuspendedError4
802
818
  } from "@aikirun/types/workflow-run";
803
819
 
@@ -805,7 +821,7 @@ import {
805
821
  import { INTERNAL as INTERNAL4 } from "@aikirun/types/symbols";
806
822
  import {
807
823
  isTerminalWorkflowRunStatus as isTerminalWorkflowRunStatus2,
808
- WorkflowRunConflictError as WorkflowRunConflictError4,
824
+ WorkflowRunRevisionConflictError as WorkflowRunRevisionConflictError4,
809
825
  WorkflowRunSuspendedError as WorkflowRunSuspendedError3
810
826
  } from "@aikirun/types/workflow-run";
811
827
  async function childWorkflowRunHandle(client, run, parentRun, logger, eventsDefinition) {
@@ -884,7 +900,7 @@ function createStatusWaiter(handle, parentRun, logger) {
884
900
  ...timeoutInMs !== void 0 ? { "aiki.timeoutInMs": timeoutInMs } : {}
885
901
  });
886
902
  } catch (error) {
887
- if (error instanceof WorkflowRunConflictError4) {
903
+ if (error instanceof WorkflowRunRevisionConflictError4) {
888
904
  throw new WorkflowRunSuspendedError3(parentRun.id);
889
905
  }
890
906
  throw error;
@@ -1065,7 +1081,7 @@ var WorkflowVersionImpl = class {
1065
1081
  const output = await this.parse(handle, this.params.schema?.output, outputRaw, run.logger);
1066
1082
  return output;
1067
1083
  } catch (error) {
1068
- if (error instanceof WorkflowRunSuspendedError4 || error instanceof WorkflowRunFailedError2 || error instanceof WorkflowRunConflictError5) {
1084
+ if (error instanceof WorkflowRunSuspendedError4 || error instanceof WorkflowRunFailedError2 || error instanceof WorkflowRunRevisionConflictError5) {
1069
1085
  throw error;
1070
1086
  }
1071
1087
  const attempts = handle.run.attempts;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aikirun/workflow",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "Workflow SDK for Aiki - define durable workflows with tasks, sleeps, waits, and event handling",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -18,7 +18,7 @@
18
18
  "build": "tsup"
19
19
  },
20
20
  "dependencies": {
21
- "@aikirun/types": "0.15.0",
21
+ "@aikirun/types": "0.16.0",
22
22
  "@standard-schema/spec": "^1.1.0"
23
23
  },
24
24
  "publishConfig": {