|
| 1 | +--- |
| 2 | +title: "Human-in-the-loop workflow with ReactFlow and Trigger.dev waitpoint tokens" |
| 3 | +sidebarTitle: "Human-in-the-loop workflow" |
| 4 | +description: "This example project creates audio summaries of newspaper articles using a human-in-the-loop workflow built with ReactFlow and Trigger.dev waitpoint tokens." |
| 5 | +--- |
| 6 | + |
| 7 | +import UpgradeToV4Note from "/snippets/upgrade-to-v4-note.mdx"; |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +This demo is a full stack example that uses the following: |
| 12 | + |
| 13 | +- [Next.js](https://nextjs.org/) for the web application |
| 14 | +- [ReactFlow](https://reactflow.dev/) for the workflow UI |
| 15 | +- [Trigger.dev Realtime](/realtime/overview) to subscribe to task runs and show the real-time status of the workflow steps |
| 16 | +- [Trigger.dev waitpoint tokens](/wait-for-token) to create a human-in-the-loop flow with a review step |
| 17 | +- [OpenAI API](https://openai.com/api/) to generate article summaries |
| 18 | +- [ElevenLabs](https://elevenlabs.io/text-to-speech) to convert text to speech |
| 19 | + |
| 20 | +## GitHub repo |
| 21 | + |
| 22 | +<Card |
| 23 | + title="View the human-in-the-loop workflow repo" |
| 24 | + icon="GitHub" |
| 25 | + href="https://github.com/triggerdotdev/examples/tree/main/article-summary-workflow" |
| 26 | +> |
| 27 | + Click here to view the full code for this project in our examples repository on GitHub. You can |
| 28 | + fork it and use it as a starting point for your own project. |
| 29 | +</Card> |
| 30 | + |
| 31 | +## Video |
| 32 | + |
| 33 | +<video |
| 34 | + controls |
| 35 | + className="w-full aspect-video" |
| 36 | + src="https://content.trigger.dev/reactflow-waitpoints-example.mov" |
| 37 | +></video> |
| 38 | + |
| 39 | +## Relevant code |
| 40 | + |
| 41 | +Each node in the workflow corresponds to a Trigger.dev task. The idea is to enable building flows by composition of different tasks. The output of one task serves as input for another. |
| 42 | + |
| 43 | +- **Trigger.dev task splitting**: |
| 44 | + - The [summarizeArticle](https://github.com/triggerdotdev/examples/blob/main/article-summary-workflow/src/trigger/summarizeArticle.ts) task uses the OpenAI API to generate a summary an article. |
| 45 | + - The [convertTextToSpeech](https://github.com/triggerdotdev/examples/blob/main/article-summary-workflow/src/trigger/convertTextToSpeech.ts) task uses the ElevenLabs API to convert the summary into an audio stream and upload it to an S3 bucket. |
| 46 | + - The [reviewSummary](https://github.com/triggerdotdev/examples/blob/main/article-summary-workflow/src/trigger/reviewSummary.ts) task is a human-in-the-loop step that shows the result and waits for approval of the summary before continuing. |
| 47 | + - [articleWorkflow](https://github.com/triggerdotdev/examples/blob/main/article-summary-workflow/src/trigger/articleWorkflow.ts) is the entrypoint that ties the workflow together and orchestrates the tasks. You might choose to approach the orchestration differently, depending on your use case. |
| 48 | +- **ReactFlow Nodes**: there are three types of nodes in this example. All of them are custom ReactFlow nodes. |
| 49 | + - The [InputNode](https://github.com/triggerdotdev/examples/blob/main/article-summary-workflow/src/components/InputNode.tsx) is the starting node of the workflow. It triggers the workflow by submitting an article URL. |
| 50 | + - The [ActionNode](https://github.com/triggerdotdev/examples/blob/main/article-summary-workflow/src/components/ActionNode.tsx) is a node that shows the status of a task run in Trigger.dev, in real-time using the React hooks for Trigger.dev. |
| 51 | + - The [ReviewNode](https://github.com/triggerdotdev/examples/blob/main/article-summary-workflow/src/components/ReviewNode.tsx) is a node that shows the summary result and prompts the user for approval before continuing. It uses the Realtime API to fetch details about the review status. Also, it interacts with the Trigger.dev waitpoint API for completing the waitpoint token using Next.js server actions. |
| 52 | +- **Workflow orchestration**: |
| 53 | + - The workflow is orchestrated by the [Flow](https://github.com/triggerdotdev/examples/blob/main/article-summary-workflow/src/components/Flow.tsx) component. It lays out the nodes, the connections between them, as well as the mapping to the Trigger.dev tasks. |
| 54 | + It also uses the `useRealtimeRunsWithTag` hook to subscribe to task runs associated with the workflow and passes down the run details to the nodes. |
| 55 | + |
| 56 | +The waitpoint token is created in [a Next.js server action](https://github.com/triggerdotdev/examples/blob/main/article-summary-workflow/src/app/actions.ts#L26): |
| 57 | + |
| 58 | +```ts |
| 59 | +const reviewWaitpointToken = await wait.createToken({ |
| 60 | + tags: [workflowTag], |
| 61 | + timeout: "1h", |
| 62 | + idempotencyKey: `review-summary-${workflowTag}`, |
| 63 | +}); |
| 64 | +``` |
| 65 | + |
| 66 | +and later completed in another server action in the same file: |
| 67 | + |
| 68 | +```ts |
| 69 | +await wait.completeToken<ReviewPayload>( |
| 70 | + { id: tokenId }, |
| 71 | + { |
| 72 | + approved: true, |
| 73 | + approvedAt: new Date(), |
| 74 | + approvedBy: user, |
| 75 | + } |
| 76 | +); |
| 77 | +``` |
| 78 | + |
| 79 | +<UpgradeToV4Note /> |
| 80 | + |
| 81 | + |
| 82 | +While the workflow in this example is static and does not allow changing the connections between nodes in the UI, it serves as a good baseline for understanding how to build completely custom workflow builders using Trigger.dev and ReactFlow. |
| 83 | + |
| 84 | +## Learn more about Trigger.dev Realtime and waitpoint tokens |
| 85 | + |
| 86 | +To learn more, take a look at the following resources: |
| 87 | + |
| 88 | +- [Trigger.dev Realtime](/realtime) - learn more about how to subscribe to runs and get real-time updates |
| 89 | +- [Realtime streaming](/realtime/streams) - learn more about streaming data from your tasks |
| 90 | +- [React hooks](/frontend/react-hooks) - learn more about using React hooks to interact with the Trigger.dev API |
| 91 | +- [Waitpoint tokens](/wait-for-token) - learn about waitpoint tokens in Trigger.dev and human-in-the-loop flows |
0 commit comments