Skip to content

Commit 7f993bc

Browse files
committed
Fix trigger script
Normalize trigger scripts across workbenches
1 parent 00efdfb commit 7f993bc

File tree

30 files changed

+323
-67
lines changed

30 files changed

+323
-67
lines changed

workbench/example/workflows/1_simple.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import { FatalError } from 'workflow';
22

3+
function deepStepCallSync() {
4+
throw new FatalError('Deep step call error');
5+
}
6+
7+
async function deepStepCall() {
8+
deepStepCallSync();
9+
}
10+
311
async function add(a: number, b: number): Promise<number> {
412
'use step';
513

14+
await deepStepCall();
15+
616
// Mimic a retryable error 50% of the time
717
if (Math.random() < 0.5) {
818
throw new Error('Retryable error');
@@ -16,10 +26,19 @@ async function add(a: number, b: number): Promise<number> {
1626
return a + b;
1727
}
1828

29+
async function deepWorkflowCall(i: number) {
30+
throw new Error('Error in workflow itself');
31+
32+
const a = await add(i, 7);
33+
return a;
34+
}
35+
1936
export async function simple(i: number) {
2037
'use workflow';
2138
console.log('Simple workflow started');
2239

40+
await deepWorkflowCall(i);
41+
2342
const a = await add(i, 7);
2443
console.log('Workflow step 1 completed - Result:', a);
2544

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { createWebhook, sleep } from 'workflow';
2+
3+
export async function handleUserSignup(email: string) {
4+
'use workflow';
5+
6+
const user = await createUser(email);
7+
await sendWelcomeEmail(user);
8+
9+
await sleep('5s');
10+
11+
const webhook = createWebhook();
12+
await sendOnboardingEmail(user, webhook.url);
13+
14+
await webhook;
15+
console.log('Webhook Resolved');
16+
17+
return { userId: user.id, status: 'onboarded' };
18+
}
19+
20+
async function createUser(email: string) {
21+
'use step';
22+
23+
console.log(`Creating a new user with email: ${email}`);
24+
25+
return { id: crypto.randomUUID(), email };
26+
}
27+
28+
async function sendWelcomeEmail(user: { id: string; email: string }) {
29+
'use step';
30+
31+
console.log(`Sending welcome email to user: ${user.id}`);
32+
}
33+
34+
async function sendOnboardingEmail(
35+
user: { id: string; email: string },
36+
callback: string
37+
) {
38+
'use step';
39+
40+
console.log(`Sending onboarding email to user: ${user.id}`);
41+
42+
console.log(`Click this link to resolve the webhook: ${callback}`);
43+
}

workbench/hono/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ manifest.js
44
.output
55
.data
66
.vercel
7+
_workflows.ts

workbench/hono/_workflows.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

workbench/hono/_workflows.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Auto-generated by workbench/scripts/generate-workflows-registry.js
2+
// Do not edit this file manually - it will be regenerated on build
3+
4+
import * as workflow_0_demo from './workflows/0_demo.js';
5+
import * as workflow_1_simple from './workflows/1_simple.js';
6+
import * as workflow_2_control_flow from './workflows/2_control_flow.js';
7+
import * as workflow_3_streams from './workflows/3_streams.js';
8+
import * as workflow_4_ai from './workflows/4_ai.js';
9+
import * as workflow_5_hooks from './workflows/5_hooks.js';
10+
import * as workflow_6_batching from './workflows/6_batching.js';
11+
import * as workflow_7_full from './workflows/7_full.js';
12+
import * as workflow_98_duplicate_case from './workflows/98_duplicate_case.js';
13+
import * as workflow_99_e2e from './workflows/99_e2e.js';
14+
15+
export const allWorkflows = {
16+
'workflows/0_demo.ts': workflow_0_demo,
17+
'workflows/1_simple.ts': workflow_1_simple,
18+
'workflows/2_control_flow.ts': workflow_2_control_flow,
19+
'workflows/3_streams.ts': workflow_3_streams,
20+
'workflows/4_ai.ts': workflow_4_ai,
21+
'workflows/5_hooks.ts': workflow_5_hooks,
22+
'workflows/6_batching.ts': workflow_6_batching,
23+
'workflows/7_full.ts': workflow_7_full,
24+
'workflows/98_duplicate_case.ts': workflow_98_duplicate_case,
25+
'workflows/99_e2e.ts': workflow_99_e2e,
26+
} as const;

workbench/hono/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"version": "0.0.0",
66
"license": "Apache-2.0",
77
"scripts": {
8+
"generate:workflows": "node ../scripts/generate-workflows-registry.js",
9+
"predev": "pnpm generate:workflows",
10+
"prebuild": "pnpm generate:workflows",
811
"dev": "nitro dev",
912
"build": "nitro build"
1013
},

workbench/nextjs-turbopack/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ yarn-error.log*
4040
*.tsbuildinfo
4141
next-env.d.ts
4242
.env*.local
43+
44+
# workflow
45+
_workflows.ts

workbench/nextjs-turbopack/app/api/trigger/route.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { getRun, start } from 'workflow/api';
22
import { hydrateWorkflowArguments } from 'workflow/internal/serialization';
3-
import * as batchingWorkflow from '@/workflows/6_batching';
4-
import * as duplicateE2e from '@/workflows/98_duplicate_case';
5-
import * as e2eWorkflows from '@/workflows/99_e2e';
3+
import { allWorkflows } from '@/_workflows';
64

75
export async function POST(req: Request) {
86
const url = new URL(req.url);
@@ -35,16 +33,23 @@ export async function POST(req: Request) {
3533
);
3634

3735
try {
38-
let workflows;
39-
if (workflowFile === 'workflows/99_e2e.ts') {
40-
workflows = e2eWorkflows;
41-
} else if (workflowFile === 'workflows/6_batching.ts') {
42-
workflows = batchingWorkflow;
43-
} else {
44-
workflows = duplicateE2e;
36+
const workflows = allWorkflows[workflowFile as keyof typeof allWorkflows];
37+
if (!workflows) {
38+
return Response.json(
39+
{ error: `Workflow file "${workflowFile}" not found` },
40+
{ status: 404 }
41+
);
42+
}
43+
44+
const workflow = workflows[workflowFn as keyof typeof workflows];
45+
if (!workflow) {
46+
return Response.json(
47+
{ error: `Function "${workflowFn}" not found in ${workflowFile}` },
48+
{ status: 400 }
49+
);
4550
}
4651

47-
const run = await start((workflows as any)[workflowFn], args);
52+
const run = await start(workflow as any, args);
4853
console.log('Run:', run);
4954
return Response.json(run);
5055
} catch (err) {

workbench/nextjs-turbopack/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
"private": true,
55
"license": "Apache-2.0",
66
"scripts": {
7+
"generate:workflows": "node ../scripts/generate-workflows-registry.js",
8+
"predev": "pnpm generate:workflows",
9+
"prebuild": "pnpm generate:workflows",
710
"dev": "next dev --turbopack",
811
"build": "next build --turbopack",
9-
"clean": "rm -rf .next .swc app/.well-known/workflow",
12+
"clean": "rm -rf .next .swc app/.well-known/workflow _workflows.ts",
1013
"start": "next start",
1114
"lint": "next lint"
1215
},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../example/workflows/1_simple.ts

0 commit comments

Comments
 (0)