Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions templates/ts-mastraai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
"node": ">=18.0.0"
},
"dependencies": {
"@ai-sdk/openai": "^2.0.3",
"@mastra/core": "^0.24.9",
"apify": "^3.5.2",
"mastra": "^0.18.9",
"zod": "^3.25.67"
"@ai-sdk/openai": "^3.0.23",
"@mastra/core": "^1.1.0",
"apify": "^3.5.3",
"mastra": "^1.1.0",
"zod": "^4.3.6"
},
"devDependencies": {
"@apify/eslint-config": "^1.0.0",
"@apify/tsconfig": "^0.1.1",
"@types/node": "^22.15.32",
"eslint": "^9.29.0",
"eslint-config-prettier": "^10.1.5",
"globals": "^17.0.0",
"prettier": "^3.5.3",
"tsx": "^4.20.3",
"@types/node": "^25.1.0",
"eslint": "^9.39.2",
"eslint-config-prettier": "^10.1.8",
"globals": "^17.2.0",
"prettier": "^3.8.1",
"tsx": "^4.21.0",
"typescript": "^5.9.3",
"typescript-eslint": "^8.34.1"
"typescript-eslint": "^8.54.0"
},
"scripts": {
"start": "npm run start:dev",
Expand Down
1 change: 1 addition & 0 deletions templates/ts-mastraai/src/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { instagramScraperTool } from './tools.js';

export const createSocialMediaAgent = (modelName: string): Agent =>
new Agent({
id: 'social-media-agent',
name: 'Social Media Agent',
instructions: `You are an expert social media analyst specializing in Instagram analysis.
You help users understand social media data and extract meaningful insights from
Expand Down
2 changes: 1 addition & 1 deletion templates/ts-mastraai/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const agent = createSocialMediaAgent(modelName);
log.info(`Querying the agent with the following query: ${query}`);

// Query the agent and get the response
const response = await agent.generateVNext([{ role: 'user', content: query }]);
const response = await agent.generate([{ role: 'user', content: query }]);

log.info(`Agent response: ${response.text}`);

Expand Down
87 changes: 42 additions & 45 deletions templates/ts-mastraai/src/tools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// eslint-disable-next-line import/extensions
import type { Tool } from '@mastra/core/tools';
// eslint-disable-next-line import/extensions
import { createTool } from '@mastra/core/tools';
import { ApifyClient, log } from 'apify';
import { z } from 'zod';
Expand All @@ -17,46 +15,45 @@ const instagramScraperOutputSchema = z.object({
});

// Define the Instagram Scraper Tool
export const instagramScraperTool: Tool<typeof instagramScraperInputSchema, typeof instagramScraperOutputSchema> =
createTool({
id: 'instagram-profile-posts-scraper',
description: "Tool to scrape Instagram profile posts using Apify's Instagram Scraper.",
inputSchema: instagramScraperInputSchema,
outputSchema: instagramScraperOutputSchema,
execute: async ({ context }) => {
const token = process.env.APIFY_TOKEN;
if (!token) {
throw new Error('APIFY_TOKEN environment variable is missing!');
}

const { handle, maxPosts } = context;
const runInput = {
directUrls: [`https://www.instagram.com/${handle}/`],
resultsLimit: maxPosts,
resultsType: 'posts',
searchLimit: 1,
};

const apifyClient = new ApifyClient({ token });

// Call the Apify Instagram Scraper Actor
const run = await apifyClient.actor('apify/instagram-scraper').call(runInput);
if (!run) {
throw new Error('Failed to start the Actor apify/instagram-scraper');
}

// Fetch dataset items
const datasetId = run.defaultDatasetId;
const dataset = await apifyClient.dataset(datasetId).listItems();
const datasetItems: unknown[] = dataset.items;

// Validate and convert dataset items to InstagramPosts
try {
const posts: InstagramPosts = InstagramPosts.fromRaw(datasetItems);
return { posts: posts.root };
} catch (error) {
log.warning(`Received invalid dataset items: ${JSON.stringify(datasetItems)}. Error: ${error}`);
throw new Error('Received invalid dataset items.');
}
},
});
export const instagramScraperTool = createTool({
id: 'instagram-profile-posts-scraper',
description: "Tool to scrape Instagram profile posts using Apify's Instagram Scraper.",
inputSchema: instagramScraperInputSchema,
outputSchema: instagramScraperOutputSchema,
execute: async (inputData) => {
const token = process.env.APIFY_TOKEN;
if (!token) {
throw new Error('APIFY_TOKEN environment variable is missing!');
}

const { handle, maxPosts } = inputData;
const runInput = {
directUrls: [`https://www.instagram.com/${handle}/`],
resultsLimit: maxPosts,
resultsType: 'posts',
searchLimit: 1,
};

const apifyClient = new ApifyClient({ token });

// Call the Apify Instagram Scraper Actor
const run = await apifyClient.actor('apify/instagram-scraper').call(runInput);
if (!run) {
throw new Error('Failed to start the Actor apify/instagram-scraper');
}

// Fetch dataset items
const datasetId = run.defaultDatasetId;
const dataset = await apifyClient.dataset(datasetId).listItems();
const datasetItems: unknown[] = dataset.items;

// Validate and convert dataset items to InstagramPosts
try {
const posts: InstagramPosts = InstagramPosts.fromRaw(datasetItems);
return { posts: posts.root };
} catch (error) {
log.warning(`Received invalid dataset items: ${JSON.stringify(datasetItems)}. Error: ${error}`);
throw new Error('Received invalid dataset items.');
}
},
});