create-mastra 0.1.0-alpha.35 → 0.1.0-alpha.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,182 +0,0 @@
1
- import { openai } from '@ai-sdk/openai';
2
- import { Agent } from '@mastra/core/agent';
3
- import { Step, Workflow } from '@mastra/core/workflows';
4
- import { z } from 'zod';
5
-
6
- const llm = openai('gpt-4o');
7
-
8
- const agent = new Agent({
9
- name: 'Weather Agent',
10
- model: llm,
11
- instructions: `
12
- You are a local activities and travel expert who excels at weather-based planning. Analyze the weather data and provide practical activity recommendations.
13
-
14
- For each day in the forecast, structure your response exactly as follows:
15
-
16
- 📅 [Day, Month Date, Year]
17
- ═══════════════════════════
18
-
19
- 🌡️ WEATHER SUMMARY
20
- • Conditions: [brief description]
21
- • Temperature: [X°C/Y°F to A°C/B°F]
22
- • Precipitation: [X% chance]
23
-
24
- 🌅 MORNING ACTIVITIES
25
- Outdoor:
26
- • [Activity Name] - [Brief description including specific location/route]
27
- Best timing: [specific time range]
28
- Note: [relevant weather consideration]
29
-
30
- 🌞 AFTERNOON ACTIVITIES
31
- Outdoor:
32
- • [Activity Name] - [Brief description including specific location/route]
33
- Best timing: [specific time range]
34
- Note: [relevant weather consideration]
35
-
36
- 🏠 INDOOR ALTERNATIVES
37
- • [Activity Name] - [Brief description including specific venue]
38
- Ideal for: [weather condition that would trigger this alternative]
39
-
40
- ⚠️ SPECIAL CONSIDERATIONS
41
- • [Any relevant weather warnings, UV index, wind conditions, etc.]
42
-
43
- Guidelines:
44
- - Suggest 2-3 time-specific outdoor activities per day
45
- - Include 1-2 indoor backup options
46
- - For precipitation >50%, lead with indoor activities
47
- - All activities must be specific to the location
48
- - Include specific venues, trails, or locations
49
- - Consider activity intensity based on temperature
50
- - Keep descriptions concise but informative
51
-
52
- Maintain this exact formatting for consistency, using the emoji and section headers as shown.
53
- `,
54
- });
55
-
56
- const fetchWeather = new Step({
57
- id: 'fetch-weather',
58
- description: 'Fetches weather forecast for a given city',
59
- inputSchema: z.object({
60
- city: z.string().describe('The city to get the weather for'),
61
- }),
62
- execute: async ({ context }) => {
63
- const triggerData = context?.getStepPayload<{ city: string }>('trigger');
64
-
65
- if (!triggerData) {
66
- throw new Error('Trigger data not found');
67
- }
68
-
69
- const geocodingUrl = `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(triggerData.city)}&count=1`;
70
- const geocodingResponse = await fetch(geocodingUrl);
71
- const geocodingData = (await geocodingResponse.json()) as {
72
- results: { latitude: number; longitude: number; name: string }[];
73
- };
74
-
75
- if (!geocodingData.results?.[0]) {
76
- throw new Error(`Location '${triggerData.city}' not found`);
77
- }
78
-
79
- const { latitude, longitude, name } = geocodingData.results[0];
80
-
81
- const weatherUrl = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&daily=temperature_2m_max,temperature_2m_min,precipitation_probability_mean,weathercode&timezone=auto`;
82
- const response = await fetch(weatherUrl);
83
- const data = (await response.json()) as {
84
- daily: {
85
- time: string[];
86
- temperature_2m_max: number[];
87
- temperature_2m_min: number[];
88
- precipitation_probability_mean: number[];
89
- weathercode: number[];
90
- };
91
- };
92
-
93
- const forecast = data.daily.time.map((date: string, index: number) => ({
94
- date,
95
- maxTemp: data.daily.temperature_2m_max[index],
96
- minTemp: data.daily.temperature_2m_min[index],
97
- precipitationChance: data.daily.precipitation_probability_mean[index],
98
- condition: getWeatherCondition(data.daily.weathercode[index]!),
99
- location: name,
100
- }));
101
-
102
- return forecast;
103
- },
104
- });
105
-
106
- const forecastSchema = z.array(
107
- z.object({
108
- date: z.string(),
109
- maxTemp: z.number(),
110
- minTemp: z.number(),
111
- precipitationChance: z.number(),
112
- condition: z.string(),
113
- location: z.string(),
114
- }),
115
- );
116
-
117
- const planActivities = new Step({
118
- id: 'plan-activities',
119
- description: 'Suggests activities based on weather conditions',
120
- inputSchema: forecastSchema,
121
- execute: async ({ context, mastra }) => {
122
- const forecast = context?.getStepPayload<z.infer<typeof forecastSchema>>('fetch-weather');
123
-
124
- if (!forecast) {
125
- throw new Error('Forecast data not found');
126
- }
127
-
128
- const prompt = `Based on the following weather forecast for ${forecast[0].location}, suggest appropriate activities:
129
- ${JSON.stringify(forecast, null, 2)}
130
- `;
131
-
132
- const response = await agent.stream([
133
- {
134
- role: 'user',
135
- content: prompt,
136
- },
137
- ]);
138
-
139
- for await (const chunk of response.textStream) {
140
- process.stdout.write(chunk);
141
- }
142
-
143
- return {
144
- activities: response.text,
145
- };
146
- },
147
- });
148
-
149
- function getWeatherCondition(code: number): string {
150
- const conditions: Record<number, string> = {
151
- 0: 'Clear sky',
152
- 1: 'Mainly clear',
153
- 2: 'Partly cloudy',
154
- 3: 'Overcast',
155
- 45: 'Foggy',
156
- 48: 'Depositing rime fog',
157
- 51: 'Light drizzle',
158
- 53: 'Moderate drizzle',
159
- 55: 'Dense drizzle',
160
- 61: 'Slight rain',
161
- 63: 'Moderate rain',
162
- 65: 'Heavy rain',
163
- 71: 'Slight snow fall',
164
- 73: 'Moderate snow fall',
165
- 75: 'Heavy snow fall',
166
- 95: 'Thunderstorm',
167
- };
168
- return conditions[code] || 'Unknown';
169
- }
170
-
171
- const weatherWorkflow = new Workflow({
172
- name: 'weather-workflow',
173
- triggerSchema: z.object({
174
- city: z.string().describe('The city to get the weather for'),
175
- }),
176
- })
177
- .step(fetchWeather)
178
- .then(planActivities);
179
-
180
- weatherWorkflow.commit();
181
-
182
- export { weatherWorkflow };
@@ -1,26 +0,0 @@
1
- // @ts-ignore
2
- import { mastra } from '#mastra';
3
- // @ts-ignore
4
- import { createNodeServer } from '#server';
5
- import { evaluate } from '@mastra/core/eval';
6
- import { AvailableHooks, registerHook } from '@mastra/core/hooks';
7
-
8
- // @ts-ignore
9
- const evalStore = [];
10
- // @ts-ignore
11
- await createNodeServer(mastra, { playground: true, swaggerUI: true, evalStore });
12
-
13
- registerHook(AvailableHooks.ON_GENERATION, ({ input, output, metric, runId, agentName }) => {
14
- evaluate({
15
- agentName,
16
- input,
17
- metric,
18
- output,
19
- runId,
20
- globalRunId: runId,
21
- });
22
- });
23
-
24
- registerHook(AvailableHooks.ON_EVALUATION, async traceObject => {
25
- evalStore.push(traceObject);
26
- });