yieldstar 0.1.0 → 0.1.4-alpha.1
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 +57 -49
- package/package.json +3 -2
- package/src/exports/sdk-http.ts +6 -4
- package/src/exports/sdk-local.ts +0 -1
- package/src/exports/workflow.ts +13 -6
- package/src/internal/serialise.ts +10 -3
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// src/exports/sdk-local.ts
|
|
3
3
|
import { randomUUID } from "crypto";
|
|
4
|
-
function createLocalSdk(
|
|
4
|
+
function createLocalSdk(invoker) {
|
|
5
5
|
return {
|
|
6
6
|
async trigger(workflowId, opts) {
|
|
7
7
|
const { workflowParams } = opts ?? {};
|
|
@@ -25,9 +25,57 @@ function createLocalSdk(workflowRouter, invoker) {
|
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
27
|
// src/exports/sdk-http.ts
|
|
28
|
-
import { deserializeError, isErrorLike } from "serialize-error";
|
|
28
|
+
import { deserializeError as deserializeError2, isErrorLike as isErrorLike2 } from "serialize-error";
|
|
29
29
|
import { randomUUID as randomUUID2 } from "crypto";
|
|
30
|
-
|
|
30
|
+
|
|
31
|
+
// src/internal/serialise.ts
|
|
32
|
+
import {
|
|
33
|
+
StepDelay,
|
|
34
|
+
StepError,
|
|
35
|
+
StepResult,
|
|
36
|
+
WorkflowResult
|
|
37
|
+
} from "@yieldstar/core";
|
|
38
|
+
import { isErrorLike, serializeError, deserializeError } from "serialize-error";
|
|
39
|
+
function errorWithOriginalStack(error, fn) {
|
|
40
|
+
const err = new Error(error.message);
|
|
41
|
+
Error.captureStackTrace(err, fn);
|
|
42
|
+
err.stack = error.stack;
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
45
|
+
function serializeStepResponse(data) {
|
|
46
|
+
const replaceErrors = (key, value) => {
|
|
47
|
+
if (value instanceof Error) {
|
|
48
|
+
return serializeError(value);
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
};
|
|
52
|
+
return JSON.stringify(data, replaceErrors);
|
|
53
|
+
}
|
|
54
|
+
function deserializeStepResponse(jsonString) {
|
|
55
|
+
const reviveErrors = (key, value) => {
|
|
56
|
+
if (isErrorLike(value)) {
|
|
57
|
+
return deserializeError(value);
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
};
|
|
61
|
+
const stepResponse = JSON.parse(jsonString, reviveErrors);
|
|
62
|
+
switch (stepResponse.type) {
|
|
63
|
+
case "step-error":
|
|
64
|
+
const { err, ...params } = stepResponse;
|
|
65
|
+
return new StepError(stepResponse.err, params);
|
|
66
|
+
case "step-delay":
|
|
67
|
+
return new StepDelay(stepResponse.resumeIn);
|
|
68
|
+
case "step-result":
|
|
69
|
+
return new StepResult(stepResponse.result);
|
|
70
|
+
case "workflow-result":
|
|
71
|
+
return new WorkflowResult(stepResponse.result);
|
|
72
|
+
default:
|
|
73
|
+
throw new Error("Unknown step result type: " + stepResponse.type);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/exports/sdk-http.ts
|
|
78
|
+
function createHttpSdkFactory() {
|
|
31
79
|
return (params) => {
|
|
32
80
|
return {
|
|
33
81
|
async trigger(workflowId, opts) {
|
|
@@ -56,8 +104,8 @@ function createHttpSdkFactory(workflowRouter) {
|
|
|
56
104
|
body: JSON.stringify({ executionId })
|
|
57
105
|
});
|
|
58
106
|
const json = await result.json();
|
|
59
|
-
if (
|
|
60
|
-
throw
|
|
107
|
+
if (isErrorLike2(json)) {
|
|
108
|
+
throw errorWithOriginalStack(deserializeError2(json), this.triggerAndWait);
|
|
61
109
|
}
|
|
62
110
|
return json;
|
|
63
111
|
}
|
|
@@ -84,46 +132,6 @@ var isIterable = (i) => {
|
|
|
84
132
|
return typeof i === "object" && i != null && Symbol.iterator in i;
|
|
85
133
|
};
|
|
86
134
|
|
|
87
|
-
// src/internal/serialise.ts
|
|
88
|
-
import { serializeError, deserializeError as deserializeError2, isErrorLike as isErrorLike2 } from "serialize-error";
|
|
89
|
-
import {
|
|
90
|
-
StepDelay,
|
|
91
|
-
StepError,
|
|
92
|
-
StepResult,
|
|
93
|
-
WorkflowResult
|
|
94
|
-
} from "@yieldstar/core";
|
|
95
|
-
function serialize(data) {
|
|
96
|
-
const replaceErrors = (key, value) => {
|
|
97
|
-
if (value instanceof Error) {
|
|
98
|
-
return serializeError(value);
|
|
99
|
-
}
|
|
100
|
-
return value;
|
|
101
|
-
};
|
|
102
|
-
return JSON.stringify(data, replaceErrors);
|
|
103
|
-
}
|
|
104
|
-
function deserialize(jsonString) {
|
|
105
|
-
const reviveErrors = (key, value) => {
|
|
106
|
-
if (isErrorLike2(value)) {
|
|
107
|
-
return deserializeError2(value);
|
|
108
|
-
}
|
|
109
|
-
return value;
|
|
110
|
-
};
|
|
111
|
-
const stepResponse = JSON.parse(jsonString, reviveErrors);
|
|
112
|
-
switch (stepResponse.type) {
|
|
113
|
-
case "step-error":
|
|
114
|
-
const { err, ...params } = stepResponse;
|
|
115
|
-
return new StepError(stepResponse.err, params);
|
|
116
|
-
case "step-delay":
|
|
117
|
-
return new StepDelay(stepResponse.resumeIn);
|
|
118
|
-
case "step-result":
|
|
119
|
-
return new StepResult(stepResponse.result);
|
|
120
|
-
case "workflow-result":
|
|
121
|
-
return new WorkflowResult(stepResponse.result);
|
|
122
|
-
default:
|
|
123
|
-
throw new Error("Unknown step result type: " + stepResponse.type);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
135
|
// src/internal/step-runner.ts
|
|
128
136
|
import {
|
|
129
137
|
StepKey,
|
|
@@ -224,8 +232,8 @@ import {
|
|
|
224
232
|
} from "@yieldstar/core";
|
|
225
233
|
function createWorkflow(workflowFn) {
|
|
226
234
|
return async function* workflowGenerator(params) {
|
|
227
|
-
const
|
|
228
|
-
const
|
|
235
|
+
const { executionId, heapClient, logger } = params;
|
|
236
|
+
const workflowIterator = workflowFn(stepRunner, logger);
|
|
229
237
|
let keylessStepIndex = -1;
|
|
230
238
|
let iteratorResult = null;
|
|
231
239
|
let nextIteratorResult = null;
|
|
@@ -268,7 +276,7 @@ function createWorkflow(workflowFn) {
|
|
|
268
276
|
stepResponse = iteratorResult.value;
|
|
269
277
|
}
|
|
270
278
|
if (stepResponse instanceof StepCacheCheck2) {
|
|
271
|
-
const cachedResponse = cached ?
|
|
279
|
+
const cachedResponse = cached ? deserializeStepResponse(cached.stepResponseJson) : null;
|
|
272
280
|
iteratorResult = await workflowIterator.next(cachedResponse);
|
|
273
281
|
stepResponse = iteratorResult.value;
|
|
274
282
|
} else if (!(stepResponse instanceof WorkflowResult2)) {
|
|
@@ -289,7 +297,7 @@ function createWorkflow(workflowFn) {
|
|
|
289
297
|
stepKey,
|
|
290
298
|
stepAttempt,
|
|
291
299
|
stepDone: !needsRetry,
|
|
292
|
-
stepResponseJson:
|
|
300
|
+
stepResponseJson: serializeStepResponse(stepResponse)
|
|
293
301
|
});
|
|
294
302
|
}
|
|
295
303
|
if (stepResponse instanceof WorkflowResult2) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yieldstar",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4-alpha.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"devDependencies": {
|
|
14
14
|
"@types/bun": "latest",
|
|
15
|
-
"@yieldstar/core": "workspace:*"
|
|
15
|
+
"@yieldstar/core": "workspace:*",
|
|
16
|
+
"pino": "^9.6.0"
|
|
16
17
|
}
|
|
17
18
|
}
|
package/src/exports/sdk-http.ts
CHANGED
|
@@ -4,10 +4,9 @@ import type {
|
|
|
4
4
|
} from "@yieldstar/core";
|
|
5
5
|
import { deserializeError, isErrorLike } from "serialize-error";
|
|
6
6
|
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { errorWithOriginalStack } from "../internal/serialise";
|
|
7
8
|
|
|
8
|
-
export function createHttpSdkFactory<W extends WorkflowRouter>(
|
|
9
|
-
workflowRouter: W
|
|
10
|
-
) {
|
|
9
|
+
export function createHttpSdkFactory<W extends WorkflowRouter>() {
|
|
11
10
|
return (params: { host: string; port: number }) => {
|
|
12
11
|
return {
|
|
13
12
|
async trigger<K extends keyof W>(
|
|
@@ -57,7 +56,10 @@ export function createHttpSdkFactory<W extends WorkflowRouter>(
|
|
|
57
56
|
const json = await result.json();
|
|
58
57
|
|
|
59
58
|
if (isErrorLike(json)) {
|
|
60
|
-
throw
|
|
59
|
+
throw errorWithOriginalStack(
|
|
60
|
+
deserializeError(json),
|
|
61
|
+
this.triggerAndWait
|
|
62
|
+
);
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
return json as Promise<WorkflowGeneratorReturnType<W[K]>>;
|
package/src/exports/sdk-local.ts
CHANGED
package/src/exports/workflow.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
import type { Logger } from "pino";
|
|
1
2
|
import type { WorkflowGenerator } from "@yieldstar/core";
|
|
2
3
|
import type { StepRunner } from "../internal/step-runner";
|
|
3
4
|
import { isIterable } from "../internal/utils";
|
|
4
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
deserializeStepResponse,
|
|
7
|
+
serializeStepResponse,
|
|
8
|
+
} from "../internal/serialise";
|
|
5
9
|
import { stepRunner } from "../internal/step-runner";
|
|
6
10
|
import {
|
|
7
11
|
StepResponse,
|
|
@@ -14,7 +18,10 @@ import {
|
|
|
14
18
|
WorkflowResult,
|
|
15
19
|
} from "@yieldstar/core";
|
|
16
20
|
|
|
17
|
-
export type WorkflowFn<T> = (
|
|
21
|
+
export type WorkflowFn<T> = (
|
|
22
|
+
step: StepRunner,
|
|
23
|
+
logger: Logger
|
|
24
|
+
) => AsyncGenerator<any, T>;
|
|
18
25
|
|
|
19
26
|
export function createWorkflow<T>(
|
|
20
27
|
workflowFn: WorkflowFn<T>
|
|
@@ -25,8 +32,8 @@ export function createWorkflow<T>(
|
|
|
25
32
|
* @yields {StepResponse}
|
|
26
33
|
*/
|
|
27
34
|
return async function* workflowGenerator(params) {
|
|
28
|
-
const
|
|
29
|
-
const
|
|
35
|
+
const { executionId, heapClient, logger } = params;
|
|
36
|
+
const workflowIterator = workflowFn(stepRunner, logger);
|
|
30
37
|
|
|
31
38
|
let keylessStepIndex = -1;
|
|
32
39
|
let iteratorResult: IteratorResult<any> | null = null;
|
|
@@ -115,7 +122,7 @@ export function createWorkflow<T>(
|
|
|
115
122
|
*/
|
|
116
123
|
if (stepResponse instanceof StepCacheCheck) {
|
|
117
124
|
const cachedResponse = cached
|
|
118
|
-
?
|
|
125
|
+
? deserializeStepResponse(cached.stepResponseJson)
|
|
119
126
|
: null;
|
|
120
127
|
iteratorResult = await workflowIterator.next(cachedResponse);
|
|
121
128
|
stepResponse = iteratorResult.value;
|
|
@@ -159,7 +166,7 @@ export function createWorkflow<T>(
|
|
|
159
166
|
stepKey,
|
|
160
167
|
stepAttempt,
|
|
161
168
|
stepDone: !needsRetry,
|
|
162
|
-
stepResponseJson:
|
|
169
|
+
stepResponseJson: serializeStepResponse(stepResponse),
|
|
163
170
|
});
|
|
164
171
|
}
|
|
165
172
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { serializeError, deserializeError, isErrorLike } from "serialize-error";
|
|
2
1
|
import {
|
|
3
2
|
StepDelay,
|
|
4
3
|
StepError,
|
|
@@ -6,8 +5,16 @@ import {
|
|
|
6
5
|
StepResponse,
|
|
7
6
|
WorkflowResult,
|
|
8
7
|
} from "@yieldstar/core";
|
|
8
|
+
import { isErrorLike, serializeError, deserializeError } from "serialize-error";
|
|
9
9
|
|
|
10
|
-
export function
|
|
10
|
+
export function errorWithOriginalStack(error: Error, fn: Function) {
|
|
11
|
+
const err = new Error(error.message);
|
|
12
|
+
Error.captureStackTrace(err, fn);
|
|
13
|
+
err.stack = error.stack;
|
|
14
|
+
throw err;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function serializeStepResponse(data: StepResponse): string {
|
|
11
18
|
const replaceErrors = (key: string, value: any) => {
|
|
12
19
|
if (value instanceof Error) {
|
|
13
20
|
return serializeError(value);
|
|
@@ -18,7 +25,7 @@ export function serialize(data: StepResponse): string {
|
|
|
18
25
|
return JSON.stringify(data, replaceErrors);
|
|
19
26
|
}
|
|
20
27
|
|
|
21
|
-
export function
|
|
28
|
+
export function deserializeStepResponse(jsonString: string): StepResponse {
|
|
22
29
|
const reviveErrors = (key: string, value: any) => {
|
|
23
30
|
if (isErrorLike(value)) {
|
|
24
31
|
return deserializeError(value);
|