-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathweather-task.ts
More file actions
260 lines (225 loc) · 7.64 KB
/
weather-task.ts
File metadata and controls
260 lines (225 loc) · 7.64 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { logger, task } from "@trigger.dev/sdk";
import { mastra } from "../mastra";
export const whatShouldIWearTodayTask = task({
id: "what-should-i-wear-today",
maxDuration: 15,
run: async (payload: { city: string; activity?: string }, { ctx }) => {
const activity = payload.activity || "walking";
logger.info(
`👕 What should I wear today for ${activity} in ${payload.city}`,
{
city: payload.city,
activity,
taskId: ctx.task.id,
runId: ctx.run.id,
},
);
try {
const startTime = Date.now();
const threadId = ctx.run.id;
// Step 1: Get simplified weather data and store in memory
logger.info("🌤️ Getting weather data", {
city: payload.city,
threadId,
});
const weatherResult = await weatherDataTask.triggerAndWait({
city: payload.city,
});
if (!weatherResult.ok) {
throw new Error(`Weather data failed: ${weatherResult.error}`);
}
// Step 2: Get clothing recommendation from memory
logger.info("👔 Getting clothing recommendation", {
city: payload.city,
activity,
threadId: weatherResult.output.threadId,
});
const clothingResult = await clothingAdviceTask.triggerAndWait({
city: payload.city,
activity,
threadId: weatherResult.output.threadId,
});
logger.info("🔍 Clothing result debug", {
ok: clothingResult.ok,
output: clothingResult.ok ? clothingResult.output : null,
error: clothingResult.ok ? null : clothingResult.error,
});
if (!clothingResult.ok) {
throw new Error(`Clothing advice failed: ${clothingResult.error}`);
}
const totalTime = Date.now() - startTime;
logger.info(
`✅ Completed clothing recommendation for ${activity} in ${payload.city}`,
{
city: payload.city,
activity,
totalProcessingTimeMs: totalTime,
finalAdvice: clothingResult.output.advice,
hasAdvice: !!clothingResult.output.advice,
},
);
// Format the date to be used in the response
const formattedDate = new Date(startTime).toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
return [
{
city: payload.city,
activity: activity,
date: formattedDate,
clothingAdvice: clothingResult.output.advice ||
"No clothing advice generated",
},
];
} catch (error) {
logger.error("❌ What should I wear today task failed", {
city: payload.city,
activity,
error: error instanceof Error ? error.message : "Unknown error",
});
throw error;
}
},
});
export const weatherDataTask = task({
id: "weather-data",
maxDuration: 10,
run: async (payload: { city: string }, { ctx }) => {
logger.info(`🌤️ Getting weather data for ${payload.city}`, {
city: payload.city,
taskId: ctx.task.id,
});
try {
const weatherAnalyst = mastra.getAgent("weatherAnalyst");
const startTime = Date.now();
const threadId = ctx.run.id;
const response = await weatherAnalyst.generate(
`Get current weather for ${payload.city}. Use the weather tool to get the data, then store ONLY these 4 fields in working memory:
- location: "${payload.city}"
- temperature: the current temperature from the tool
- rainChance: the rain chance from today's forecast
- windSpeed: the current wind speed
You MUST store this data in working memory so other agents can access it. After storing, confirm what you stored.`,
{
maxSteps: 2,
threadId,
resourceId: payload.city,
},
);
// Extract structured weather data from weatherTool result
let weatherData = null;
if (response.steps) {
for (const step of response.steps) {
if (step.toolResults) {
for (const toolResult of step.toolResults) {
if (toolResult.toolName === "weatherTool" && toolResult.result) {
weatherData = toolResult.result;
break;
}
}
}
if (weatherData) break;
}
}
const processingTime = Date.now() - startTime;
logger.info("✅ Weather data retrieved", {
city: payload.city,
processingTimeMs: processingTime,
hasStructuredData: !!weatherData,
weatherData: weatherData,
responseText: response.text,
stepsCount: response.steps?.length || 0,
});
return {
success: true,
city: payload.city,
weatherData,
threadId,
metadata: {
processingTimeMs: processingTime,
hasStructuredData: !!weatherData,
},
timestamp: new Date().toISOString(),
};
} catch (error) {
logger.error("❌ Weather data task failed", {
city: payload.city,
error: error instanceof Error ? error.message : "Unknown error",
});
return {
success: false,
city: payload.city,
error: error instanceof Error ? error.message : "Unknown error",
timestamp: new Date().toISOString(),
};
}
},
});
export const clothingAdviceTask = task({
id: "clothing-advice",
maxDuration: 10,
run: async (
payload: { city: string; activity: string; threadId?: string },
{ ctx },
) => {
logger.info("👔 Getting clothing advice", {
city: payload.city,
activity: payload.activity,
taskId: ctx.task.id,
});
try {
const clothingAgent = mastra.getAgent("clothingAdvisorAgent");
const startTime = Date.now();
const threadId = payload.threadId || ctx.run.id;
const response = await clothingAgent.generate(
`You must provide clothing advice for ${payload.activity} in ${payload.city}.
First, check your working memory for weather data for ${payload.city}. The data should contain: location, temperature, rainChance, windSpeed.
If you find the weather data in memory, use it. If not, you can use the weather tool.
Based on the weather conditions, provide exactly one paragraph starting with "For ${payload.activity} in ${payload.city} you should wear..." and explain why based on the specific weather conditions.
You MUST provide a response. Do not return empty or blank responses.`,
{
maxSteps: 2,
threadId,
resourceId: payload.city,
},
);
const processingTime = Date.now() - startTime;
logger.info("✅ Clothing advice generated", {
city: payload.city,
activity: payload.activity,
processingTimeMs: processingTime,
responseLength: response.text.length,
responseText: response.text,
hasResponse: !!response.text,
});
return {
success: true,
city: payload.city,
activity: payload.activity,
advice: response.text || "No advice generated",
threadId,
metadata: {
processingTimeMs: processingTime,
responseLength: response.text.length,
hasResponse: !!response.text,
},
timestamp: new Date().toISOString(),
};
} catch (error) {
logger.error("❌ Clothing advice task failed", {
city: payload.city,
activity: payload.activity,
error: error instanceof Error ? error.message : "Unknown error",
});
return {
success: false,
city: payload.city,
activity: payload.activity,
error: error instanceof Error ? error.message : "Unknown error",
timestamp: new Date().toISOString(),
};
}
},
});