gemini-code-parser v1.1.4
Gemini Code Parser
Gemini Code Parser is a Node.js library designed to parse the code response generated by Gemini AI in response to user prompts. It extracts information such as file names, paths, contents, and additional metadata provided by the AI, and emits structured events for easy integration into other applications.
Features
- Streamlined parsing of AI-generated code responses.
- Emits structured events for real-time processing of files and responses.
- Easy-to-extend architecture using
EventEmitter
. - Integration with Google Generative AI API.
Installation
Ensure you have Node.js installed on your system. Then, install the package via npm:
npm install gemini-code-parser
Usage
Here’s how to integrate Gemini Code Parser in your project:
Step 1: Import and Initialize
import { GeminiCodeParser } from 'gemini-code-parser';
const parser = new GeminiCodeParser('<Your_Google_AI_API_Key>', '<Your_Google_Gemini_Model_Name>');
Step 2: Generate Parsed Code Stream
const prompt = "Write a Node.js application to manage tasks.";
parser.generateParsedCodeStream(prompt)
.then(parsedCode => {
console.log(parsedCode);
})
.catch(error => {
console.error("Error generating parsed code:", error);
});
API Reference
GeminiCodeParser
Constructor
new GeminiCodeParser(apiKey: string, modelName: string)
apiKey
: Google AI API key for accessing the Generative AI service.modelName
: Name of the Gemini model to use for generating code.
Methods
generateCodeStream(prompt: string)
- Streams the AI-generated content in response to the given prompt.
- Returns:
AsyncIterable
of content chunks.
generateParsedCodeStream(prompt: string)
- Parses the AI-generated response and returns structured code files and metadata.
- Returns:
Promise
resolving to the parsed code object.
Event Listeners
Gemini Code Parser emits various events to facilitate real-time processing:
file-start
: Emitted when a file parsing starts.- Payload:
{ name, path }
- Payload:
file
: Emitted while parsing file content.- Payload:
{ name, path, content }
- Payload:
file-end
: Emitted when a file parsing ends.- Payload:
{}
- Payload:
response
: Emitted for the general AI response.- Payload:
{ response }
- Payload:
response-end
: Emitted when the AI response ends.- Payload:
{}
- Payload:
title
: Emitted for the title of the generated application.- Payload:
{ title }
- Payload:
Example
Here’s a full example that listens to emitted events:
import { GeminiCodeParser } from 'gemini-code-parser';
const parser = new GeminiCodeParser('<Your_Google_AI_API_Key>', '<Your_Google_Gemini_Model_Name>');
parser.on('file-start', data => {
console.log("File start:", data);
});
parser.on('file', data => {
console.log("File content:", data);
});
parser.on('file-end', () => {
console.log("File end.");
});
parser.on('response', data => {
console.log("General response:", data);
});
parser.generateParsedCodeStream("Create a simple web server using Node.js.")
.then(parsedCode => {
console.log("Parsed code:", parsedCode);
})
.catch(error => {
console.error("Error:", error);
});