-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patharticleWorkflow.ts
90 lines (79 loc) · 2.55 KB
/
articleWorkflow.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { batch, Context, task } from "@trigger.dev/sdk";
import { scrape } from "./scrapeSite";
import { summarizeArticle } from "./summarizeArticle";
import { convertTextToSpeech } from "./convertTextToSpeech";
import { reviewSummary } from "./reviewSummary";
import { publishSummary } from "./publishSummary";
import { sendSlackNotification } from "./sendSlackNotification";
export const articleWorkflow = task({
id: "article-workflow",
run: async (
payload: { articleUrl: string; approvalWaitpointTokenId: string },
{ ctx }: { ctx: Context }
) => {
const scrapeResult = await scrape.triggerAndWait(
{
url: payload.articleUrl,
},
{ tags: ctx.run.tags }
);
if (!scrapeResult.ok) {
throw new Error("Failed to scrape site");
}
const summarizeResult = await summarizeArticle.triggerAndWait(
{
content: scrapeResult.output.content,
},
{ tags: ctx.run.tags }
);
if (!summarizeResult.ok) {
throw new Error("Failed to summarize article");
}
const convertTextToSpeechResult = await convertTextToSpeech.triggerAndWait(
{
text: summarizeResult.output.summary,
},
{ tags: ctx.run.tags }
);
if (!convertTextToSpeechResult.ok) {
throw new Error("Failed to convert text to speech");
}
const reviewSummaryResult = await reviewSummary.triggerAndWait(
{
audioSummaryUrl: convertTextToSpeechResult.output.audioUrl,
waitpointTokenId: payload.approvalWaitpointTokenId,
},
{ tags: ctx.run.tags }
);
if (!reviewSummaryResult.ok) {
throw new Error("Failed to review summary");
}
if (reviewSummaryResult.output.approved) {
const {
runs: [sendSlackNotificationRun, publishSummaryRun],
} = await batch.triggerByTaskAndWait([
{
task: sendSlackNotification,
payload: {
message: `Article summary was approved by ${reviewSummaryResult.output.approvedBy} at ${reviewSummaryResult.output.approvedAt}`,
},
options: { tags: ctx.run.tags },
},
{
task: publishSummary,
payload: {
audioSummaryUrl: convertTextToSpeechResult.output.audioUrl,
articleUrl: payload.articleUrl,
},
options: { tags: ctx.run.tags },
},
]);
if (!sendSlackNotificationRun.ok) {
throw new Error("Failed to send Slack notification");
}
if (!publishSummaryRun.ok) {
throw new Error("Failed to publish summary");
}
}
},
});