UNPKG

@xynehq/jaf

Version:

Juspay Agent Framework - A purely functional agent framework with immutable state and composable tools

201 lines 8.1 kB
/** * JAF ADK - Production News Tool * * Real news data fetching using NewsAPI */ import { createFunctionTool } from '../tools'; import { ToolParameterType } from '../types'; /** * Create a production news tool using NewsAPI * Requires NEWS_API_KEY environment variable */ export const createNewsTool = (apiKey) => { const key = apiKey || process.env.NEWS_API_KEY; return createFunctionTool({ name: 'get_news', description: 'Get latest news headlines from various sources', execute: async (params) => { const { category = 'general', country = 'us', query, limit = 5 } = params; if (!key) { // Fallback to mock data if no API key return getMockNewsData(category, limit); } try { let url; if (query) { // Search for specific query url = `https://newsapi.org/v2/everything?q=${encodeURIComponent(query)}&pageSize=${limit}&apiKey=${key}`; } else { // Get top headlines by category url = `https://newsapi.org/v2/top-headlines?category=${category}&country=${country}&pageSize=${limit}&apiKey=${key}`; } const response = await fetch(url); if (!response.ok) { throw new Error(`News API error: ${response.status}`); } const data = await response.json(); if (data.status !== 'ok') { throw new Error(data.message || 'News API error'); } return { category, country, query, totalResults: data.totalResults, articles: data.articles.map((article) => ({ title: article.title, description: article.description, source: article.source.name, author: article.author, url: article.url, publishedAt: article.publishedAt, content: article.content })) }; } catch (error) { // Fallback to mock data on error console.warn('News API error, using mock data:', error); return getMockNewsData(category, limit); } }, parameters: [ { name: 'category', type: ToolParameterType.STRING, description: 'News category', required: false, enum: ['business', 'entertainment', 'general', 'health', 'science', 'sports', 'technology'], default: 'general' }, { name: 'country', type: ToolParameterType.STRING, description: 'Country code (e.g., us, gb, de)', required: false, default: 'us' }, { name: 'query', type: ToolParameterType.STRING, description: 'Search query for specific news', required: false }, { name: 'limit', type: ToolParameterType.NUMBER, description: 'Number of articles to return', required: false, default: 5 } ] }); }; /** * Fallback mock news data for development/testing */ const getMockNewsData = (category, limit) => { const mockArticles = { technology: [ { title: 'AI Breakthrough: New Model Achieves Human-Level Performance', description: 'Researchers announce major advancement in artificial intelligence', source: 'Tech News', author: 'Jane Doe', url: 'https://example.com/ai-breakthrough', publishedAt: new Date().toISOString(), content: 'Full article content here...' }, { title: 'Quantum Computing Milestone Reached', description: 'New quantum processor demonstrates significant speedup', source: 'Science Daily', author: 'John Smith', url: 'https://example.com/quantum-milestone', publishedAt: new Date(Date.now() - 3600000).toISOString(), content: 'Full article content here...' }, { title: 'Cybersecurity Alert: New Vulnerability Discovered', description: 'Critical security flaw found in popular software', source: 'Security Weekly', author: 'Security Team', url: 'https://example.com/security-alert', publishedAt: new Date(Date.now() - 7200000).toISOString(), content: 'Full article content here...' } ], business: [ { title: 'Stock Markets Reach New Heights', description: 'Major indices show strong performance this quarter', source: 'Financial Times', author: 'Market Analyst', url: 'https://example.com/market-news', publishedAt: new Date().toISOString(), content: 'Full article content here...' }, { title: 'Tech Startup Secures Major Funding', description: 'Innovative company raises $100M in Series B', source: 'Business Wire', author: 'Business Reporter', url: 'https://example.com/startup-funding', publishedAt: new Date(Date.now() - 3600000).toISOString(), content: 'Full article content here...' } ], science: [ { title: 'New Exoplanet Discovered in Habitable Zone', description: 'Astronomers find potentially Earth-like planet', source: 'Space News', author: 'Astronomy Team', url: 'https://example.com/exoplanet', publishedAt: new Date().toISOString(), content: 'Full article content here...' }, { title: 'Medical Breakthrough in Cancer Treatment', description: 'New therapy shows promising results in trials', source: 'Medical Journal', author: 'Dr. Research', url: 'https://example.com/cancer-treatment', publishedAt: new Date(Date.now() - 3600000).toISOString(), content: 'Full article content here...' } ], general: [ { title: 'Breaking: Major Policy Change Announced', description: 'Government unveils new initiatives', source: 'News Network', author: 'Political Reporter', url: 'https://example.com/policy-change', publishedAt: new Date().toISOString(), content: 'Full article content here...' }, { title: 'Community Event Brings Thousands Together', description: 'Annual festival celebrates local culture', source: 'Local News', author: 'Community Reporter', url: 'https://example.com/community-event', publishedAt: new Date(Date.now() - 3600000).toISOString(), content: 'Full article content here...' } ] }; const articles = mockArticles[category] || mockArticles.general; return { category, country: 'us', query: null, totalResults: articles.length, articles: articles.slice(0, limit), mock: true }; }; export default createNewsTool; //# sourceMappingURL=news.js.map