yieldstar 0.1.5-alpha.1 → 0.2.0-alpha.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/index.js CHANGED
@@ -1,32 +1,30 @@
1
1
  // @bun
2
2
  // src/exports/sdk-local.ts
3
- import { randomUUID } from "crypto";
3
+ var {randomUUIDv7 } = globalThis.Bun;
4
4
  function createLocalSdk(invoker) {
5
5
  return {
6
- async trigger(workflowId, opts) {
7
- const { workflowParams } = opts ?? {};
8
- const executionId = opts?.executionId ?? randomUUID();
9
- await invoker.execute({
10
- executionId,
11
- workflowId,
12
- params: workflowParams
13
- });
14
- return { executionId };
6
+ async trigger(event) {
7
+ const executionEvent = {
8
+ executionId: event.executionId ?? randomUUIDv7(),
9
+ workflowId: event.workflowId,
10
+ params: event.params
11
+ };
12
+ await invoker.execute(executionEvent);
13
+ return { executionId: executionEvent.executionId };
15
14
  },
16
- async triggerAndWait(workflowId, opts) {
17
- const { workflowParams } = opts ?? {};
18
- const executionId = opts?.executionId ?? randomUUID();
15
+ async triggerAndWait(event) {
16
+ const executionId = event.executionId ?? randomUUIDv7();
19
17
  const workflowCompletePromise = new Promise((resolve) => {
20
18
  invoker.workflowEndEmitter.once(executionId, resolve);
21
19
  });
22
- await this.trigger(workflowId, { executionId, workflowParams });
20
+ await this.trigger({ ...event, executionId });
23
21
  return workflowCompletePromise;
24
22
  }
25
23
  };
26
24
  }
27
25
  // src/exports/sdk-http.ts
26
+ var {randomUUIDv7: randomUUIDv72 } = globalThis.Bun;
28
27
  import { deserializeError as deserializeError2, isErrorLike as isErrorLike2 } from "serialize-error";
29
- import { randomUUID as randomUUID2 } from "crypto";
30
28
 
31
29
  // src/internal/serialise.ts
32
30
  import {
@@ -78,16 +76,15 @@ function deserializeStepResponse(jsonString) {
78
76
  function createHttpSdkFactory() {
79
77
  return (params) => {
80
78
  return {
81
- async trigger(workflowId, opts) {
82
- const { workflowParams } = opts ?? {};
83
- const executionId = opts?.executionId ?? randomUUID2();
79
+ async trigger(event) {
80
+ const executionEvent = {
81
+ executionId: event?.executionId ?? randomUUIDv72(),
82
+ workflowId: event?.workflowId,
83
+ params: event?.params
84
+ };
84
85
  const res = await fetch(`${params.host}:${params.port}/trigger`, {
85
86
  method: "POST",
86
- body: JSON.stringify({
87
- executionId,
88
- workflowId,
89
- params: workflowParams
90
- })
87
+ body: JSON.stringify(executionEvent)
91
88
  });
92
89
  if (res.ok) {
93
90
  return res.json();
@@ -95,10 +92,8 @@ function createHttpSdkFactory() {
95
92
  throw res.statusText;
96
93
  }
97
94
  },
98
- async triggerAndWait(workflowId, opts) {
99
- const { workflowParams } = opts ?? {};
100
- const executionId = opts?.executionId ?? randomUUID2();
101
- await this.trigger(workflowId, { executionId, workflowParams });
95
+ async triggerAndWait(event) {
96
+ const { executionId } = await this.trigger(event);
102
97
  const result = await fetch(`${params.host}:${params.port}/events`, {
103
98
  method: "POST",
104
99
  body: JSON.stringify({ executionId })
@@ -230,10 +225,10 @@ import {
230
225
  StepInvalid,
231
226
  WorkflowResult as WorkflowResult2
232
227
  } from "@yieldstar/core";
233
- function createWorkflow(workflowFn) {
234
- return async function* workflowGenerator(params) {
235
- const { executionId, heapClient, logger } = params;
236
- const workflowIterator = workflowFn(stepRunner, logger);
228
+ function workflow(workflowFn) {
229
+ return async function* workflowGenerator({ event, heapClient, logger }) {
230
+ const workflowIterator = workflowFn(stepRunner, event, logger);
231
+ const { executionId } = event;
237
232
  let keylessStepIndex = -1;
238
233
  let iteratorResult = null;
239
234
  let nextIteratorResult = null;
@@ -326,7 +321,9 @@ function createWorkflow(workflowFn) {
326
321
  throw new Error("Critical error");
327
322
  };
328
323
  }
324
+ var createWorkflow = workflow;
329
325
  export {
326
+ workflow,
330
327
  createWorkflowRouter,
331
328
  createWorkflow,
332
329
  createLocalSdk,
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "yieldstar",
3
- "version": "0.1.5-alpha.1",
3
+ "version": "0.2.0-alpha.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "src/index.ts",
7
7
  "scripts": {
8
- "build": "bun build src/index.ts --outfile dist/index.js --target bun --packages=external"
8
+ "build": "bun build src/index.ts --outfile dist/index.js --target bun --packages=external",
9
+ "check": "tsc --noEmit"
9
10
  },
10
11
  "dependencies": {
11
12
  "serialize-error": "^11.0.3"
@@ -1,52 +1,43 @@
1
1
  import type {
2
+ EventParamsOf,
2
3
  WorkflowRouter,
3
4
  WorkflowGeneratorReturnType,
5
+ TriggerEvent,
6
+ ExecutionEvent,
4
7
  } from "@yieldstar/core";
8
+ import { randomUUIDv7 } from "bun";
5
9
  import { deserializeError, isErrorLike } from "serialize-error";
6
- import { randomUUID } from "node:crypto";
7
10
  import { errorWithOriginalStack } from "../internal/serialise";
8
11
 
9
12
  export function createHttpSdkFactory<W extends WorkflowRouter>() {
10
13
  return (params: { host: string; port: number }) => {
11
14
  return {
12
- async trigger<K extends keyof W>(
13
- workflowId: K,
14
- opts?: {
15
- executionId?: string;
16
- workflowParams?: Parameters<W[K]>[0] extends never
17
- ? never
18
- : Parameters<W[K]>[0];
19
- }
15
+ async trigger<K extends string & keyof W>(
16
+ event: TriggerEvent<K, EventParamsOf<W[K]>>
20
17
  ) {
21
- const { workflowParams } = opts ?? {};
22
- const executionId = opts?.executionId ?? randomUUID();
18
+ const executionEvent: ExecutionEvent = {
19
+ executionId: event?.executionId ?? randomUUIDv7(),
20
+ workflowId: event?.workflowId as string,
21
+ params: event?.params,
22
+ };
23
+
23
24
  const res = await fetch(`${params.host}:${params.port}/trigger`, {
24
25
  method: "POST",
25
- body: JSON.stringify({
26
- executionId,
27
- workflowId,
28
- params: workflowParams,
29
- }),
26
+ body: JSON.stringify(executionEvent),
30
27
  });
28
+
31
29
  if (res.ok) {
32
30
  return res.json() as Promise<{ executionId: string }>;
33
31
  } else {
34
32
  throw res.statusText;
35
33
  }
36
34
  },
37
- async triggerAndWait<K extends keyof W>(
38
- workflowId: K,
39
- opts?: {
40
- executionId?: string;
41
- workflowParams?: Parameters<W[K]>[0] extends never
42
- ? never
43
- : Parameters<W[K]>[0];
44
- }
35
+ async triggerAndWait<K extends string & keyof W>(
36
+ event: TriggerEvent<K, EventParamsOf<W[K]>>
45
37
  ) {
46
- const { workflowParams } = opts ?? {};
47
- const executionId = opts?.executionId ?? randomUUID();
48
38
  // todo: add timeout and cancel request
49
- await this.trigger(workflowId, { executionId, workflowParams });
39
+ const { executionId } = await this.trigger(event);
40
+
50
41
  // todo: set up subscription first (maybe just use ws)
51
42
  const result = await fetch(`${params.host}:${params.port}/events`, {
52
43
  method: "POST",
@@ -1,47 +1,36 @@
1
1
  import type {
2
+ EventParamsOf,
2
3
  WorkflowRouter,
3
4
  WorkflowGeneratorReturnType,
4
5
  WorkflowInvoker,
6
+ TriggerEvent,
7
+ ExecutionEvent,
5
8
  } from "@yieldstar/core";
6
- import { randomUUID } from "node:crypto";
9
+ import { randomUUIDv7 } from "bun";
7
10
 
8
11
  export function createLocalSdk<W extends WorkflowRouter>(
9
12
  invoker: WorkflowInvoker
10
13
  ) {
11
14
  return {
12
- async trigger<K extends keyof W>(
13
- workflowId: K,
14
- opts?: {
15
- executionId?: string;
16
- workflowParams?: Parameters<W[K]>[0] extends never
17
- ? never
18
- : Parameters<W[K]>[0];
19
- }
15
+ async trigger<K extends string & keyof W>(
16
+ event: TriggerEvent<K, EventParamsOf<W[K]>>
20
17
  ) {
21
- const { workflowParams } = opts ?? {};
22
- const executionId = opts?.executionId ?? randomUUID();
23
- await invoker.execute({
24
- executionId,
25
- workflowId: workflowId as string,
26
- params: workflowParams,
27
- });
28
- return { executionId };
18
+ const executionEvent: ExecutionEvent = {
19
+ executionId: event.executionId ?? randomUUIDv7(),
20
+ workflowId: event.workflowId,
21
+ params: event.params,
22
+ };
23
+ await invoker.execute(executionEvent);
24
+ return { executionId: executionEvent.executionId };
29
25
  },
30
- async triggerAndWait<K extends keyof W>(
31
- workflowId: K,
32
- opts?: {
33
- executionId?: string;
34
- workflowParams?: Parameters<W[K]>[0] extends never
35
- ? never
36
- : Parameters<W[K]>[0];
37
- }
26
+ async triggerAndWait<K extends string & keyof W>(
27
+ event: TriggerEvent<K, EventParamsOf<W[K]>>
38
28
  ) {
39
- const { workflowParams } = opts ?? {};
40
- const executionId = opts?.executionId ?? randomUUID();
29
+ const executionId = event.executionId ?? randomUUIDv7();
41
30
  const workflowCompletePromise = new Promise((resolve) => {
42
31
  invoker.workflowEndEmitter.once(executionId, resolve);
43
32
  });
44
- await this.trigger(workflowId, { executionId, workflowParams });
33
+ await this.trigger({ ...event, executionId });
45
34
  return workflowCompletePromise as Promise<
46
35
  WorkflowGeneratorReturnType<W[K]>
47
36
  >;
@@ -1,5 +1,5 @@
1
1
  import type { Logger } from "pino";
2
- import type { WorkflowGenerator } from "@yieldstar/core";
2
+ import type { ExecutionEvent, WorkflowGenerator } from "@yieldstar/core";
3
3
  import type { StepRunner } from "../internal/step-runner";
4
4
  import { isIterable } from "../internal/utils";
5
5
  import {
@@ -18,22 +18,23 @@ import {
18
18
  WorkflowResult,
19
19
  } from "@yieldstar/core";
20
20
 
21
- export type WorkflowFn<T> = (
21
+ export type WorkflowFn<EventParams, Result> = (
22
22
  step: StepRunner,
23
+ event: ExecutionEvent<EventParams>,
23
24
  logger: Logger
24
- ) => AsyncGenerator<any, T>;
25
+ ) => AsyncGenerator<any, Result>;
25
26
 
26
- export function createWorkflow<T>(
27
- workflowFn: WorkflowFn<T>
28
- ): WorkflowGenerator<T> {
27
+ export function workflow<EventParams = void, Result = void>(
28
+ workflowFn: WorkflowFn<EventParams, Result>
29
+ ): WorkflowGenerator<EventParams, Result> {
29
30
  /**
30
31
  * @description Advances workflow steps, handling any workflow logic, and
31
32
  * yielding control to a workflow executor to do async work
32
33
  * @yields {StepResponse}
33
34
  */
34
- return async function* workflowGenerator(params) {
35
- const { executionId, heapClient, logger } = params;
36
- const workflowIterator = workflowFn(stepRunner, logger);
35
+ return async function* workflowGenerator({ event, heapClient, logger }) {
36
+ const workflowIterator = workflowFn(stepRunner, event, logger);
37
+ const { executionId } = event;
37
38
 
38
39
  let keylessStepIndex = -1;
39
40
  let iteratorResult: IteratorResult<any> | null = null;
@@ -225,3 +226,5 @@ export function createWorkflow<T>(
225
226
  throw new Error("Critical error");
226
227
  };
227
228
  }
229
+
230
+ export const createWorkflow = workflow;
package/src/index.ts CHANGED
@@ -2,5 +2,5 @@ export { createLocalSdk } from "./exports/sdk-local";
2
2
  export { createHttpSdkFactory } from "./exports/sdk-http";
3
3
  export { createWorkflowRouter } from "./exports/router";
4
4
  export { RetryableError } from "./exports/errors";
5
- export { createWorkflow } from "./exports/workflow";
5
+ export { createWorkflow, workflow } from "./exports/workflow";
6
6
  export type { WorkflowFn } from "./exports/workflow";