-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathparallelAgents.ts
More file actions
142 lines (127 loc) · 4.18 KB
/
parallelAgents.ts
File metadata and controls
142 lines (127 loc) · 4.18 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { Agent, run } from "@openai/agents";
import { batch, logger, task } from "@trigger.dev/sdk";
// Example payload for testing:
// {
// "text": "The new iPhone 15 has amazing battery life and camera quality. However, the price is quite high for what you get.",
// "includeWordCount": true,
// "language": "english"
// }
export interface TextAnalysisPayload {
text: string;
includeWordCount?: boolean;
language?: "english" | "spanish" | "french" | "general";
}
// Create individual agent tasks that can be run in parallel
export const analyzeTextSentiment = task({
id: "analyze-sentiment",
run: async (payload: { text: string; language?: string }) => {
const agent = new Agent({
name: "Sentiment Analyzer",
instructions: `Analyze the sentiment of the given text in ${
payload.language || "english"
}. Respond with: POSITIVE, NEGATIVE, or NEUTRAL, followed by a brief explanation.`,
});
const result = await run(
agent,
`Analyze the sentiment of this text: "${payload.text}"`,
);
return {
analysis: result.finalOutput,
agentName: agent.name,
};
},
});
export const extractKeywords = task({
id: "extract-keywords",
run: async (payload: { text: string; includeWordCount?: boolean }) => {
const agent = new Agent({
name: "Keyword Extractor",
instructions:
`Extract the 5 most important keywords from the given text. Return them as a comma-separated list.
${payload.includeWordCount ? "Also include the total word count." : ""}`,
});
const result = await run(
agent,
`Extract keywords from this text: "${payload.text}"`,
);
return {
keywords: result.finalOutput,
agentName: agent.name,
};
},
});
export const summarizeText = task({
id: "summarize-text",
run: async (payload: { text: string; language?: string }) => {
const agent = new Agent({
name: "Text Summarizer",
instructions:
`Create a concise 1-2 sentence summary of the given text in ${
payload.language || "english"
}, capturing the main points.`,
});
const result = await run(agent, `Summarize this text: "${payload.text}"`);
return {
summary: result.finalOutput,
agentName: agent.name,
};
},
});
// Main orchestrator task that runs all analysis in parallel
export const parallelAgents = task({
id: "parallel-agents",
maxDuration: 180,
run: async (payload: TextAnalysisPayload) => {
logger.info("Starting parallel agent analysis", {
textLength: payload.text.length,
language: payload.language,
includeWordCount: payload.includeWordCount,
});
const startTime = Date.now();
// Run all three analysis tasks in parallel using batch.triggerByTaskAndWait
const results = await batch.triggerByTaskAndWait([
{
task: analyzeTextSentiment,
payload: { text: payload.text, language: payload.language },
},
{
task: extractKeywords,
payload: {
text: payload.text,
includeWordCount: payload.includeWordCount,
},
},
{
task: summarizeText,
payload: { text: payload.text, language: payload.language },
},
]);
const endTime = Date.now();
const executionTime = endTime - startTime;
// Process results
const sentiment = results.runs[0].ok ? results.runs[0].output : null;
const keywords = results.runs[1].ok ? results.runs[1].output : null;
const summary = results.runs[2].ok ? results.runs[2].output : null;
logger.info("Parallel agent analysis completed", {
executionTime,
successfulTasks: results.runs.filter((r) => r.ok).length,
totalTasks: results.runs.length,
language: payload.language || "english",
});
return {
originalText: payload.text,
sentiment: sentiment?.analysis,
keywords: keywords?.keywords,
summary: summary?.summary,
executionTime,
parallelTasks: 3,
language: payload.language || "english",
includeWordCount: payload.includeWordCount || false,
agents: [
sentiment?.agentName,
keywords?.agentName,
summary?.agentName,
].filter(Boolean),
};
},
});