@aikirun/workflow 0.24.2 → 0.25.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
@@ -1,6 +1,6 @@
1
1
  # @aikirun/workflow
2
2
 
3
- Workflow SDK for Aiki durable execution platform.
3
+ Workflow and Task SDK for Aiki durable execution platform.
4
4
 
5
5
  ## Installation
6
6
 
@@ -11,15 +11,29 @@ npm install @aikirun/workflow
11
11
  ## Quick Start
12
12
 
13
13
  ```typescript
14
- import { workflow } from "@aikirun/workflow";
15
- import { sendEmail, createProfile } from "./tasks.ts";
14
+ import { task, workflow } from "@aikirun/workflow";
15
+
16
+ // Define tasks
17
+ const sendEmail = task({
18
+ name: "send-email",
19
+ async handler(input: { email: string; message: string }) {
20
+ return emailService.send(input.email, input.message);
21
+ },
22
+ });
23
+
24
+ const createProfile = task({
25
+ name: "create-profile",
26
+ async handler(input: { email: string }) {
27
+ return profileService.create(input.email);
28
+ },
29
+ });
16
30
 
17
31
  // Define a workflow
18
32
  export const onboardingWorkflow = workflow({ name: "user-onboarding" });
19
33
 
20
34
  export const onboardingWorkflowV1 = onboardingWorkflow.v("1.0.0", {
21
35
  async handler(run, input: { email: string }) {
22
- await sendEmail.start(run, { email: input.email });
36
+ await sendEmail.start(run, { email: input.email, message: "Welcome!" });
23
37
  await run.sleep("welcome-delay", { days: 1 });
24
38
  await createProfile.start(run, { email: input.email });
25
39
  return { success: true };
@@ -63,6 +77,7 @@ await dailyReport.activate(aikiClient, onboardingWorkflowV1, { email: "daily@exa
63
77
 
64
78
  - **Durable Execution** - Workflows survive crashes and restarts
65
79
  - **Task Orchestration** - Coordinate multiple tasks
80
+ - **Schema Validation** - Validate workflow & task input and output at runtime
66
81
  - **Durable Sleep** - Sleep without blocking workers
67
82
  - **Event Handling** - Wait for external events with timeouts
68
83
  - **Child Workflows** - Compose workflows together
@@ -72,11 +87,10 @@ await dailyReport.activate(aikiClient, onboardingWorkflowV1, { email: "daily@exa
72
87
 
73
88
  ## Documentation
74
89
 
75
- For comprehensive documentation including retry strategies, schema validation, child workflows, and best practices, see the [Workflows Guide](https://github.com/aikirun/aiki/blob/main/docs/core-concepts/workflows.md).
90
+ For comprehensive documentation including retry strategies, schema validation, child workflows, and best practices, see the [Workflows Guide](https://github.com/aikirun/aiki/blob/main/docs/core-concepts/workflows.md) and [Tasks Guide](https://github.com/aikirun/aiki/blob/main/docs/core-concepts/tasks.md).
76
91
 
77
92
  ## Related Packages
78
93
 
79
- - [@aikirun/task](https://www.npmjs.com/package/@aikirun/task) - Define tasks
80
94
  - [@aikirun/client](https://www.npmjs.com/package/@aikirun/client) - Start workflows
81
95
  - [@aikirun/worker](https://www.npmjs.com/package/@aikirun/worker) - Execute workflows
82
96
 
package/dist/index.d.ts CHANGED
@@ -10,7 +10,7 @@ import { ReplayManifest } from '@aikirun/types/replay-manifest';
10
10
  import { SleepResult } from '@aikirun/types/sleep';
11
11
  import { EventSendOptions, EventWaitOptions, EventWaitResult } from '@aikirun/types/event';
12
12
  import { Serializable } from '@aikirun/types/serializable';
13
- import { TaskInfo } from '@aikirun/types/task';
13
+ import { TaskInfo, TaskName, TaskStartOptions, TaskDefinitionOptions } from '@aikirun/types/task';
14
14
  import { WorkflowRunStateRequest, WorkflowRunTransitionTaskStateRequestV1 } from '@aikirun/types/workflow-run-api';
15
15
  import { ScheduleOverlapPolicy, ScheduleActivateOptions, ScheduleId } from '@aikirun/types/schedule';
16
16
 
@@ -340,6 +340,70 @@ declare function schedule(params: ScheduleParams): ScheduleDefinition;
340
340
 
341
341
  declare function getSystemWorkflows(api: ApiClient): AnyWorkflowVersion[];
342
342
 
343
+ type UnknownWorkflowRunContext = WorkflowRunContext<unknown, unknown>;
344
+ /**
345
+ * Defines a durable task with deterministic execution and automatic retries.
346
+ *
347
+ * Tasks must be deterministic - the same input should always produce the same output.
348
+ * Tasks can be retried multiple times, so they should be idempotent when possible.
349
+ * Tasks execute within a workflow context and can access logging.
350
+ *
351
+ * @template Input - Type of task input (must be JSON serializable)
352
+ * @template Output - Type of task output (must be JSON serializable)
353
+ * @param params - Task configuration
354
+ * @param params.name - Unique task name used for execution tracking
355
+ * @param params.handler - Async function that executes the task logic
356
+ * @returns Task instance with retry and option configuration methods
357
+ *
358
+ * @example
359
+ * ```typescript
360
+ * // Simple task without retry
361
+ * export const sendEmail = task({
362
+ * name: "send-email",
363
+ * handler(input: { email: string; message: string }) {
364
+ * return emailService.send(input.email, input.message);
365
+ * },
366
+ * });
367
+ *
368
+ * // Task with retry configuration
369
+ * export const chargeCard = task({
370
+ * name: "charge-card",
371
+ * handler(input: { cardId: string; amount: number }) {
372
+ * return paymentService.charge(input.cardId, input.amount);
373
+ * },
374
+ * options: {
375
+ * retry: {
376
+ * type: "fixed",
377
+ * maxAttempts: 3,
378
+ * delayMs: 1000,
379
+ * },
380
+ * },
381
+ * });
382
+ *
383
+ * // Execute task in workflow
384
+ * const result = await chargeCard.start(run, { cardId: "123", amount: 9999 });
385
+ * ```
386
+ */
387
+ declare function task<Input extends Serializable, Output extends Serializable>(params: TaskParams<Input, Output>): Task<Input, Output>;
388
+ interface TaskParams<Input, Output> {
389
+ name: string;
390
+ handler: (input: Input) => Promise<Output>;
391
+ options?: TaskDefinitionOptions;
392
+ schema?: RequireAtLeastOneProp<{
393
+ input?: StandardSchemaV1<Input>;
394
+ output?: StandardSchemaV1<Output>;
395
+ }>;
396
+ }
397
+ interface Task<Input, Output> {
398
+ name: TaskName;
399
+ with(): TaskBuilder<Input, Output>;
400
+ start: (run: UnknownWorkflowRunContext, ...args: Input extends void ? [] : [Input]) => Promise<Output>;
401
+ }
402
+ interface TaskBuilder<Input, Output> {
403
+ opt<Path extends PathFromObject<TaskStartOptions>>(path: Path, value: TypeOfValueAtPath<TaskStartOptions, Path>): TaskBuilder<Input, Output>;
404
+ start: Task<Input, Output>["start"];
405
+ }
406
+
343
407
  /**
344
408
  * Defines a durable workflow with versioning and multiple task execution.
345
409
  *
@@ -400,4 +464,4 @@ interface Workflow {
400
464
  };
401
465
  }
402
466
 
403
- export { type AnyWorkflowVersion, type EventDefinition, type EventMulticaster, type EventMulticasters, type EventSender, type EventSenders, type EventWaiter, type EventWaiters, type ExecuteWorkflowParams, type ScheduleDefinition, type ScheduleHandle, type ScheduleParams, type Workflow, type WorkflowExecutionOptions, type WorkflowParams, type WorkflowRegistry, type WorkflowRunContext, type WorkflowRunHandle, type WorkflowRunWaitOptions, type WorkflowVersion, type WorkflowVersionParams, createEventSenders, createEventWaiters, createReplayManifest, createSleeper, event, executeWorkflowRun, getSystemWorkflows, schedule, workflow, workflowRegistry, workflowRunHandle };
467
+ export { type AnyWorkflowVersion, type EventDefinition, type EventMulticaster, type EventMulticasters, type EventSender, type EventSenders, type EventWaiter, type EventWaiters, type ExecuteWorkflowParams, type ScheduleDefinition, type ScheduleHandle, type ScheduleParams, type Task, type TaskParams, type Workflow, type WorkflowExecutionOptions, type WorkflowParams, type WorkflowRegistry, type WorkflowRunContext, type WorkflowRunHandle, type WorkflowRunWaitOptions, type WorkflowVersion, type WorkflowVersionParams, createEventSenders, createEventWaiters, createReplayManifest, createSleeper, event, executeWorkflowRun, getSystemWorkflows, schedule, task, workflow, workflowRegistry, workflowRunHandle };
package/dist/index.js CHANGED
@@ -965,7 +965,7 @@ function createSerializableError(error) {
965
965
  };
966
966
  }
967
967
 
968
- // ../task/task.ts
968
+ // task.ts
969
969
  import { INTERNAL as INTERNAL5 } from "@aikirun/types/symbols";
970
970
  import { TaskFailedError } from "@aikirun/types/task-error";
971
971
  import {
@@ -1644,6 +1644,7 @@ export {
1644
1644
  executeWorkflowRun,
1645
1645
  getSystemWorkflows,
1646
1646
  schedule,
1647
+ task,
1647
1648
  workflow,
1648
1649
  workflowRegistry,
1649
1650
  workflowRunHandle
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aikirun/workflow",
3
- "version": "0.24.2",
4
- "description": "Workflow SDK for Aiki - define durable workflows with tasks, sleeps, waits, and event handling",
3
+ "version": "0.25.0",
4
+ "description": "Workflow and Task SDK for Aiki - define durable workflows and tasks with automatic retries, deterministic execution, and replay",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -18,7 +18,7 @@
18
18
  "build": "tsup"
19
19
  },
20
20
  "dependencies": {
21
- "@aikirun/types": "0.24.2",
21
+ "@aikirun/types": "0.25.0",
22
22
  "@standard-schema/spec": "^1.1.0"
23
23
  },
24
24
  "publishConfig": {