yieldstar 0.1.0-alpha.0 → 0.1.3
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 +59 -50
- package/package.json +7 -6
- package/src/exports/sdk-http.ts +5 -1
- package/src/exports/workflow.ts +13 -6
- package/src/internal/serialise.ts +10 -3
- package/yieldstar-0.1.0-alpha.0.tgz +0 -0
package/dist/index.js
CHANGED
|
@@ -25,8 +25,56 @@ 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
|
+
|
|
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
|
|
30
78
|
function createHttpSdkFactory(workflowRouter) {
|
|
31
79
|
return (params) => {
|
|
32
80
|
return {
|
|
@@ -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,
|
|
@@ -132,6 +140,7 @@ import {
|
|
|
132
140
|
StepDelay as StepDelay2,
|
|
133
141
|
StepCacheCheck
|
|
134
142
|
} from "@yieldstar/core";
|
|
143
|
+
var stepRunner = { run, delay, poll };
|
|
135
144
|
async function* run(arg1, arg2) {
|
|
136
145
|
let key = null;
|
|
137
146
|
let fn;
|
|
@@ -209,7 +218,6 @@ async function* poll(arg1, arg2, arg3) {
|
|
|
209
218
|
yield* run(task);
|
|
210
219
|
}
|
|
211
220
|
}
|
|
212
|
-
var stepRunner = { run, delay, poll };
|
|
213
221
|
|
|
214
222
|
// src/exports/workflow.ts
|
|
215
223
|
import {
|
|
@@ -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;
|
|
@@ -246,7 +254,7 @@ function createWorkflow(workflowFn) {
|
|
|
246
254
|
stepKey = iteratorResult.value.key;
|
|
247
255
|
} else {
|
|
248
256
|
keylessStepIndex++;
|
|
249
|
-
stepKey =
|
|
257
|
+
stepKey = `$$step-index-${keylessStepIndex}$$`;
|
|
250
258
|
}
|
|
251
259
|
let cached = await heapClient.readStep({
|
|
252
260
|
executionId,
|
|
@@ -268,14 +276,15 @@ 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)) {
|
|
275
283
|
throw new Error("Step runners must yield a CacheCheck object");
|
|
276
284
|
}
|
|
277
285
|
if (isIterable(stepResponse)) {
|
|
278
|
-
console.log(
|
|
286
|
+
console.log(`[ERR] Steps must be yielded using yield*
|
|
287
|
+
`);
|
|
279
288
|
stepResponse = new StepInvalid;
|
|
280
289
|
} else if (!(stepResponse instanceof StepResponse3)) {
|
|
281
290
|
console.log(`[ERR] Iterators should yield a StepResponse. Got ${JSON.stringify(stepResponse)}`);
|
|
@@ -288,7 +297,7 @@ function createWorkflow(workflowFn) {
|
|
|
288
297
|
stepKey,
|
|
289
298
|
stepAttempt,
|
|
290
299
|
stepDone: !needsRetry,
|
|
291
|
-
stepResponseJson:
|
|
300
|
+
stepResponseJson: serializeStepResponse(stepResponse)
|
|
292
301
|
});
|
|
293
302
|
}
|
|
294
303
|
if (stepResponse instanceof WorkflowResult2) {
|
package/package.json
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yieldstar",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "bun build src/index.ts --outfile dist/index.js --target bun --packages=external"
|
|
9
|
+
},
|
|
7
10
|
"dependencies": {
|
|
8
11
|
"serialize-error": "^11.0.3"
|
|
9
12
|
},
|
|
10
13
|
"devDependencies": {
|
|
11
14
|
"@types/bun": "latest",
|
|
12
|
-
"@yieldstar/core": "
|
|
13
|
-
|
|
14
|
-
"scripts": {
|
|
15
|
-
"build": "bun build src/index.ts --outfile dist/index.js --target bun --packages=external"
|
|
15
|
+
"@yieldstar/core": "workspace:*",
|
|
16
|
+
"pino": "^9.6.0"
|
|
16
17
|
}
|
|
17
|
-
}
|
|
18
|
+
}
|
package/src/exports/sdk-http.ts
CHANGED
|
@@ -4,6 +4,7 @@ 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
9
|
export function createHttpSdkFactory<W extends WorkflowRouter>(
|
|
9
10
|
workflowRouter: W
|
|
@@ -57,7 +58,10 @@ export function createHttpSdkFactory<W extends WorkflowRouter>(
|
|
|
57
58
|
const json = await result.json();
|
|
58
59
|
|
|
59
60
|
if (isErrorLike(json)) {
|
|
60
|
-
throw
|
|
61
|
+
throw errorWithOriginalStack(
|
|
62
|
+
deserializeError(json),
|
|
63
|
+
this.triggerAndWait
|
|
64
|
+
);
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
return json as Promise<WorkflowGeneratorReturnType<W[K]>>;
|
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);
|
|
File without changes
|