v1.0
This commit is contained in:
37
src/aiProcessor.js
Normal file
37
src/aiProcessor.js
Normal file
@@ -0,0 +1,37 @@
|
||||
// Module for processing content with Google Gemini
|
||||
require('dotenv').config();
|
||||
|
||||
const API_KEY = process.env.GEMINI_API_KEY;
|
||||
|
||||
if (!API_KEY) {
|
||||
console.error('Error: GEMINI_API_KEY environment variable not set.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const { GoogleGenerativeAI } = require('@google/generative-ai');
|
||||
const genAI = new GoogleGenerativeAI(API_KEY);
|
||||
|
||||
/**
|
||||
* Sends note content to the Gemini API for organization.
|
||||
* @param {string[]} noteContents - An array of strings, where each string is the content of a note.
|
||||
* @returns {Promise<string>} - The organized notes as a single string from the AI.
|
||||
*/
|
||||
async function organizeNotesWithAI(noteContents) {
|
||||
const model = genAI.getGenerativeModel({ model: "gemini-2.5-pro"});
|
||||
const combinedNotes = noteContents.join('\n\n---\n\n');
|
||||
const prompt = `Please organize the following language notes. Group related topics under clear markdown headings (e.g., ## Grammar, ## Vocabulary, ## Cultural Notes). Here are the notes:\n\n${combinedNotes}`;
|
||||
|
||||
try {
|
||||
const result = await model.generateContent(prompt);
|
||||
const response = await result.response;
|
||||
const text = response.text();
|
||||
return text;
|
||||
} catch (error) {
|
||||
console.error('Error communicating with the Gemini API:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { organizeNotesWithAI };
|
||||
|
||||
|
||||
43
src/fileReader.js
Normal file
43
src/fileReader.js
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
// Module for reading and filtering files
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* Recursively scans a directory, filters files by a pattern, and reads their content.
|
||||
* @param {string} dirPath - The path to the directory.
|
||||
* @param {string} pattern - The keyword pattern to match in the file name.
|
||||
* @returns {{filePath: string, content: string}[]} - An array of objects, each with the file path and its content.
|
||||
*/
|
||||
function getFileContents(dirPath, pattern) {
|
||||
let fileData = [];
|
||||
|
||||
function scan(directory) {
|
||||
try {
|
||||
const entries = fs.readdirSync(directory, { withFileTypes: true });
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(directory, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
scan(fullPath);
|
||||
} else {
|
||||
const regex = new RegExp(pattern, 'i');
|
||||
if (!pattern || regex.test(entry.name)) {
|
||||
try {
|
||||
const content = fs.readFileSync(fullPath, 'utf8');
|
||||
fileData.push({ filePath: fullPath, content });
|
||||
} catch (readError) {
|
||||
console.error(`Error reading file ${fullPath}:`, readError);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (scanError) {
|
||||
console.error(`Error scanning directory ${directory}:`, scanError);
|
||||
}
|
||||
}
|
||||
|
||||
scan(dirPath);
|
||||
return fileData;
|
||||
}
|
||||
|
||||
module.exports = { getFileContents };
|
||||
28
src/noteOrganizer.js
Normal file
28
src/noteOrganizer.js
Normal file
@@ -0,0 +1,28 @@
|
||||
// Module to organize notes by bridging other modules
|
||||
const path = require('path');
|
||||
const { getFileContents } = require('./fileReader');
|
||||
const { organizeNotesWithAI } = require('./aiProcessor');
|
||||
|
||||
/**
|
||||
* Orchestrates the process of reading, processing, and organizing notes.
|
||||
* @param {string} dirPath - The path to the directory containing notes.
|
||||
* @param {string} pattern - The file name pattern to match.
|
||||
* @returns {Promise<string|null>} - The organized notes as a single markdown string, or null on error.
|
||||
*/
|
||||
async function processAndOrganizeNotes(dirPath, pattern) {
|
||||
const fileData = getFileContents(dirPath, pattern);
|
||||
if (fileData.length === 0) {
|
||||
console.log('No matching files found to organize.');
|
||||
return null;
|
||||
}
|
||||
|
||||
const contextualizedNotes = fileData.map(data => {
|
||||
const fileName = path.basename(data.filePath);
|
||||
return `--- Note from ${fileName} ---\n\n${data.content}`;
|
||||
});
|
||||
|
||||
const organizedNotes = await organizeNotesWithAI(contextualizedNotes);
|
||||
return organizedNotes;
|
||||
}
|
||||
|
||||
module.exports = { processAndOrganizeNotes };
|
||||
21
src/outputWriter.js
Normal file
21
src/outputWriter.js
Normal file
@@ -0,0 +1,21 @@
|
||||
// Module for writing the final output to a file
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* Writes the organized notes to a markdown file.
|
||||
* @param {string} outputDir - The directory to write the file in.
|
||||
* @param {string} content - The markdown content to write.
|
||||
*/
|
||||
function writeOrganizedNotes(outputDir, content) {
|
||||
const outputFilePath = path.join(outputDir, 'organized_notes.md');
|
||||
try {
|
||||
fs.writeFileSync(outputFilePath, content, 'utf8');
|
||||
console.log(`Successfully wrote organized notes to ${outputFilePath}`);
|
||||
} catch (error) {
|
||||
console.error(`Error writing to file ${outputFilePath}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { writeOrganizedNotes };
|
||||
|
||||
Reference in New Issue
Block a user