UNPKG

gpt-research

Version:

Autonomous AI research agent that conducts comprehensive research on any topic and generates detailed reports with citations

196 lines 5.77 kB
"use strict"; /** * Memory management for GPT Research * Stores research context, sources, and reports */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Memory = void 0; class Memory { entries = new Map(); entriesByType = new Map(); searchResults = []; scrapedContent = new Map(); visitedUrls = new Set(); reports = new Map(); context = []; subtopics = new Set(); researchResults = []; nextId = 1; constructor() { // Initialize maps } add(type, content, metadata) { const id = `${type}_${this.nextId++}`; const entry = { id, type, content, metadata, timestamp: new Date() }; this.entries.set(id, entry); if (!this.entriesByType.has(type)) { this.entriesByType.set(type, []); } this.entriesByType.get(type).push(entry); return id; } get(id) { return this.entries.get(id); } getByType(type) { return this.entriesByType.get(type) || []; } addSearchResults(query, results) { this.add('searchQueries', query); this.searchResults.push(...results); // Mark URLs as visited results.forEach(result => { if (result.url) { this.visitedUrls.add(result.url); } }); } getSearchResults() { return this.searchResults; } addScrapedContent(url, content) { this.scrapedContent.set(url, content); this.visitedUrls.add(url); } getScrapedContent(url) { return this.scrapedContent.get(url); } getAllScrapedContent() { return this.scrapedContent; } isUrlVisited(url) { return this.visitedUrls.has(url); } addReport(reportType, report) { this.reports.set(reportType, report); } getReport(reportType) { return this.reports.get(reportType); } getAllReports() { return this.reports; } addContext(context) { this.context.push(context); } getContext() { return this.context; } clearContext() { this.context = []; } addSubtopic(subtopic) { this.subtopics.add(subtopic); } addSubtopics(subtopics) { subtopics.forEach(topic => this.subtopics.add(topic)); } getSubtopics() { return Array.from(this.subtopics); } addResearchResult(result) { this.researchResults.push(result); } getResearchResults() { return this.researchResults; } clear() { this.entries.clear(); this.entriesByType.clear(); this.searchResults = []; this.scrapedContent.clear(); this.visitedUrls.clear(); this.reports.clear(); this.context = []; this.subtopics.clear(); this.researchResults = []; this.nextId = 1; } getStats() { const stats = { totalEntries: this.entries.size, searchQueries: this.getByType('searchQueries').length, searchResults: this.searchResults.length, scrapedUrls: this.scrapedContent.size, visitedUrls: this.visitedUrls.size, reports: this.reports.size, contextItems: this.context.length, subtopics: this.subtopics.size, researchResults: this.researchResults.length }; for (const [type, entries] of this.entriesByType.entries()) { stats[type] = entries.length; } return stats; } export() { return { entries: Array.from(this.entries.entries()), entriesByType: Array.from(this.entriesByType.entries()).map(([type, entries]) => [ type, entries ]), searchResults: this.searchResults, scrapedContent: Array.from(this.scrapedContent.entries()), visitedUrls: Array.from(this.visitedUrls), reports: Array.from(this.reports.entries()), context: this.context, subtopics: Array.from(this.subtopics), researchResults: this.researchResults, nextId: this.nextId }; } import(data) { let parsedData; // If data is a string, try to parse it as JSON if (typeof data === 'string') { try { parsedData = JSON.parse(data); } catch (error) { throw new Error('Invalid JSON data'); } } else { parsedData = data; } if (parsedData.entries) { this.entries = new Map(parsedData.entries); } if (parsedData.entriesByType) { this.entriesByType = new Map(parsedData.entriesByType); } if (parsedData.searchResults) { this.searchResults = parsedData.searchResults; } if (parsedData.scrapedContent) { this.scrapedContent = new Map(parsedData.scrapedContent); } if (parsedData.visitedUrls) { this.visitedUrls = new Set(parsedData.visitedUrls); } if (parsedData.reports) { this.reports = new Map(parsedData.reports); } if (parsedData.context) { this.context = parsedData.context; } if (parsedData.subtopics) { this.subtopics = new Set(parsedData.subtopics); } if (parsedData.researchResults) { this.researchResults = parsedData.researchResults; } if (parsedData.nextId) { this.nextId = parsedData.nextId; } } } exports.Memory = Memory; //# sourceMappingURL=Memory.js.map