Skip to content

Commit ef73dc7

Browse files
committed
Apply workflow function transformation in "step" mode
1 parent 0757912 commit ef73dc7

File tree

12 files changed

+309
-136
lines changed

12 files changed

+309
-136
lines changed

.changeset/cruel-crabs-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@workflow/swc-plugin": patch
3+
---
4+
5+
Apply workflow function transformation in "step" mode

packages/swc-plugin-workflow/transform/src/lib.rs

Lines changed: 276 additions & 58 deletions
Large diffs are not rendered by default.

packages/swc-plugin-workflow/transform/tests/errors/non-async-functions/output-step.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function validStep() {
2222
return 42;
2323
}
2424
export const validWorkflow = async ()=>{
25-
'use workflow';
26-
return 'test';
25+
throw new Error("You attempted to execute workflow validWorkflow function directly. To start a workflow, use start(validWorkflow) from workflow/api");
2726
};
27+
validWorkflow.workflowId = "workflow//input.js//validWorkflow";
2828
registerStepFunction("step//input.js//validStep", validStep);

packages/swc-plugin-workflow/transform/tests/fixture/mixed-functions/output-step.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ async function stepFunctionWithoutExport(a, b) {
77
return a - b;
88
}
99
export async function workflowFunction(a, b) {
10-
'use workflow';
11-
const result = await stepFunction(a, b);
12-
const result2 = await stepFunctionWithoutExport(a, b);
13-
return result + result2;
10+
throw new Error("You attempted to execute workflow workflowFunction function directly. To start a workflow, use start(workflowFunction) from workflow/api");
1411
}
12+
workflowFunction.workflowId = "workflow//input.js//workflowFunction";
1513
export async function normalFunction(a, b) {
1614
return a * b;
1715
}

packages/swc-plugin-workflow/transform/tests/fixture/module-level-workflow/output-step.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ const localArrow = async (input)=>{
77
return input.bar;
88
};
99
export async function workflow(input) {
10-
return input.foo;
10+
throw new Error("You attempted to execute workflow workflow function directly. To start a workflow, use start(workflow) from workflow/api");
1111
}
12+
workflow.workflowId = "workflow//input.js//workflow";
1213
export const arrowWorkflow = async (input)=>{
1314
return input.bar;
1415
};
16+
arrowWorkflow.workflowId = "workflow//input.js//arrowWorkflow";
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,21 @@
11
import { registerStepFunction } from "workflow/internal/private";
2-
/**__internal_workflows{"workflows":{"input.js":{"example":{"workflowId":"workflow//input.js//example"}}},"steps":{"input.js":{"arrowStep":{"stepId":"step//input.js//arrowStep"},"helpers/objectStep":{"stepId":"step//input.js//helpers/objectStep"},"letArrowStep":{"stepId":"step//input.js//letArrowStep"},"step":{"stepId":"step//input.js//step"},"varArrowStep":{"stepId":"step//input.js//varArrowStep"}}}}*/;
2+
/**__internal_workflows{"workflows":{"input.js":{"example":{"workflowId":"workflow//input.js//example"}}},"steps":{"input.js":{"arrowStep":{"stepId":"step//input.js//arrowStep"},"helpers/objectStep":{"stepId":"step//input.js//example/helpers/objectStep"},"letArrowStep":{"stepId":"step//input.js//letArrowStep"},"step":{"stepId":"step//input.js//step"},"varArrowStep":{"stepId":"step//input.js//varArrowStep"}}}}*/;
33
// Function declaration step
44
async function example$step(a, b) {
55
return a + b;
66
}
77
var example$arrowStep = async (x, y)=>x * y;
88
var example$letArrowStep = async (x, y)=>x - y;
99
var example$varArrowStep = async (x, y)=>x / y;
10-
var helpers$objectStep = async (x, y)=>{
10+
var example$helpers$objectStep = async (x, y)=>{
1111
return x + y + 10;
1212
};
1313
export async function example(a, b) {
14-
"use workflow";
15-
const step = example$step;
16-
// Arrow function with const
17-
const arrowStep = example$arrowStep;
18-
// Arrow function with let
19-
let letArrowStep = example$letArrowStep;
20-
// Arrow function with var
21-
var varArrowStep = example$varArrowStep;
22-
// Object with step method
23-
const helpers = {
24-
objectStep: helpers$objectStep
25-
};
26-
const val = await step(a, b);
27-
const val2 = await arrowStep(a, b);
28-
const val3 = await letArrowStep(a, b);
29-
const val4 = await varArrowStep(a, b);
30-
const val5 = await helpers.objectStep(a, b);
31-
return val + val2 + val3 + val4 + val5;
14+
throw new Error("You attempted to execute workflow example function directly. To start a workflow, use start(example) from workflow/api");
3215
}
16+
example.workflowId = "workflow//input.js//example";
3317
registerStepFunction("step//input.js//example/step", example$step);
3418
registerStepFunction("step//input.js//example/arrowStep", example$arrowStep);
3519
registerStepFunction("step//input.js//example/letArrowStep", example$letArrowStep);
3620
registerStepFunction("step//input.js//example/varArrowStep", example$varArrowStep);
37-
registerStepFunction("step//input.js//helpers/objectStep", helpers$objectStep);
21+
registerStepFunction("step//input.js//example/helpers/objectStep", example$helpers$objectStep);

packages/swc-plugin-workflow/transform/tests/fixture/nested-step-in-workflow/output-workflow.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**__internal_workflows{"workflows":{"input.js":{"example":{"workflowId":"workflow//input.js//example"}}},"steps":{"input.js":{"arrowStep":{"stepId":"step//input.js//arrowStep"},"helpers/objectStep":{"stepId":"step//input.js//helpers/objectStep"},"letArrowStep":{"stepId":"step//input.js//letArrowStep"},"step":{"stepId":"step//input.js//step"},"varArrowStep":{"stepId":"step//input.js//varArrowStep"}}}}*/;
1+
/**__internal_workflows{"workflows":{"input.js":{"example":{"workflowId":"workflow//input.js//example"}}},"steps":{"input.js":{"arrowStep":{"stepId":"step//input.js//arrowStep"},"helpers/objectStep":{"stepId":"step//input.js//example/helpers/objectStep"},"letArrowStep":{"stepId":"step//input.js//letArrowStep"},"step":{"stepId":"step//input.js//step"},"varArrowStep":{"stepId":"step//input.js//varArrowStep"}}}}*/;
22
export async function example(a, b) {
33
var step = globalThis[Symbol.for("WORKFLOW_USE_STEP")]("step//input.js//example/step");
44
// Arrow function with const
@@ -9,7 +9,7 @@ export async function example(a, b) {
99
var varArrowStep = globalThis[Symbol.for("WORKFLOW_USE_STEP")]("step//input.js//example/varArrowStep");
1010
// Object with step method
1111
const helpers = {
12-
objectStep: globalThis[Symbol.for("WORKFLOW_USE_STEP")]("step//input.js//helpers/objectStep")
12+
objectStep: globalThis[Symbol.for("WORKFLOW_USE_STEP")]("step//input.js//example/helpers/objectStep")
1313
};
1414
const val = await step(a, b);
1515
const val2 = await arrowStep(a, b);

packages/swc-plugin-workflow/transform/tests/fixture/nested-step-with-closure/output-step.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,9 @@ const arrowWrapperReturnNamedFunctionVar = (a, b, c)=>{
8080
return fn;
8181
};
8282
export async function wflow() {
83-
'use workflow';
84-
let count = 42;
85-
const namedStepWithClosureVars = wflow$namedStepWithClosureVars;
86-
const agent = new DurableAgent({
87-
arrowFunctionWithClosureVars: _anonymousStep2,
88-
namedFunctionWithClosureVars: _anonymousStep3,
89-
methodWithClosureVars: _anonymousStep4
90-
});
91-
await stepWrapperReturnArrowFunctionVar(1, 2, 3)();
92-
await stepWrapperReturnNamedFunction(1, 2, 3)();
93-
await stepWrapperReturnArrowFunction(1, 2, 3)();
94-
await stepWrapperReturnNamedFunctionVar(1, 2, 3)();
95-
await arrowWrapperReturnArrowFunctionVar(1, 2, 3)();
96-
await arrowWrapperReturnNamedFunction(1, 2, 3)();
97-
await arrowWrapperReturnArrowFunction(1, 2, 3)();
98-
await arrowWrapperReturnNamedFunctionVar(1, 2, 3)();
83+
throw new Error("You attempted to execute workflow wflow function directly. To start a workflow, use start(wflow) from workflow/api");
9984
}
85+
wflow.workflowId = "workflow//input.js//wflow";
10086
registerStepFunction("step//input.js//stepWrapperReturnArrowFunctionVar/fn", stepWrapperReturnArrowFunctionVar$fn);
10187
registerStepFunction("step//input.js//stepWrapperReturnNamedFunction/f", stepWrapperReturnNamedFunction$f);
10288
registerStepFunction("step//input.js//stepWrapperReturnArrowFunction/_anonymousStep0", stepWrapperReturnArrowFunction$_anonymousStep0);

packages/swc-plugin-workflow/transform/tests/fixture/nested-steps-in-object-constructor/output-step.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,8 @@ import * as z from 'zod';
66
var test$_anonymousStep0 = async ()=>gateway('openai/gpt-5');
77
var test$_anonymousStep1 = async ({ location })=>`Weather in ${location}: Sunny, 72°F`;
88
export async function test() {
9-
'use workflow';
10-
const agent = new DurableAgent({
11-
model: _anonymousStep0,
12-
tools: {
13-
getWeather: tool({
14-
description: 'Get weather for a location',
15-
inputSchema: z.object({
16-
location: z.string()
17-
}),
18-
execute: _anonymousStep1
19-
})
20-
}
21-
});
22-
await agent.stream({
23-
messages: [
24-
{
25-
role: 'user',
26-
content: 'What is the weather in San Francisco?'
27-
}
28-
]
29-
});
9+
throw new Error("You attempted to execute workflow test function directly. To start a workflow, use start(test) from workflow/api");
3010
}
11+
test.workflowId = "workflow//input.js//test";
3112
registerStepFunction("step//input.js//test/_anonymousStep0", test$_anonymousStep0);
3213
registerStepFunction("step//input.js//test/_anonymousStep1", test$_anonymousStep1);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**__internal_workflows{"workflows":{"input.js":{"workflow":{"workflowId":"workflow//input.js//workflow"}}}}*/;
22
export async function workflow(a, b) {
3-
'use workflow';
4-
return add(a, b);
3+
throw new Error("You attempted to execute workflow workflow function directly. To start a workflow, use start(workflow) from workflow/api");
54
}
5+
workflow.workflowId = "workflow//input.js//workflow";

0 commit comments

Comments
 (0)