@satyamtechie/ask-ai
v1.0.2
Published
``` npm install @satyamtechie/ask-ai ```
Readme
🚀 Installing
Using npm
npm install @satyamtechie/ask-aiOnce the package is installed, you can import the library using import or require approach:
import { askGemini } from '@satyamtechie/ask-ai'💡 Example
async function fetchData() {
try {
const res = await fetch("https://jsonplaceholder.typicode.www/posts/1"); // Intentional error
const data = await res.json();
console.log(data);
} catch (error) {
console.log(error.message);
// Get the function body as a string
const functionBody = fetchData.toString();
// Extract the try block content using regular expressions
const tryBlockRegex = /try\s*\{([\s\S]*?)\}\s*catch/;
const tryBlockMatch = functionBody.match(tryBlockRegex);
if (tryBlockMatch && tryBlockMatch[1]) {
const tryBlockContent = tryBlockMatch[1].trim();
// Send the error message and the try block content to askGemini
const response = await askGemini(error.message, tryBlockContent);
console.log(response);
} else {
console.log("Could not extract try block content.");
const response = await askGemini(error.message);
console.log(response);
}
}
}
fetchData();Sample Output
fetch failed
The error "fetch failed" is because you have a typo in your URL: `https://jsonplaceholder.typicode.www/posts/1`
should be `https://jsonplaceholder.typicode.com/posts/1`. You have `.www` instead of `.com`.
javascript
async function fetchData() {
try {
const res = await fetch("https://jsonplaceholder.typicode.com/posts/1");
const data = await res.json();
console.log(data);
} catch (error) {
console.error("Fetch failed:", error);
}
}
fetchData();
This version includes a `catch` block to handle potential errors more gracefully. If the fetch fails for any reason (network issues, server errors, etc.), the `catch` block will log the error to the console instead of crashing the script.
This is crucial for robust code. The original code would throw an error and halt execution. This improved version will handle the failure without causing the entire program to stop.🚀 fetchData Function
The fetchData function is an asynchronous JavaScript function that demonstrates how to fetch data from an API, handle errors, and use AI assistance (askGemini) to analyze and resolve issues when errors occur.
⚡ Functionality
API Request:
- Tries to fetch data from a sample API (
https://jsonplaceholder.typicode.www/posts/1). - An intentional error is introduced in the URL (
.www) to trigger the error-handling logic.
- Tries to fetch data from a sample API (
Error Handling:
- Captures any errors using a
try...catchblock. - Logs the error message to the console.
- Captures any errors using a
Dynamic Code Analysis:
- Converts the entire function (
fetchData) to a string. - Extracts the code inside the
tryblock using a regular expression. - Sends the error message and extracted code to the
askGeminifunction for AI-assisted debugging.
- Converts the entire function (
AI Assistance:
- The
askGeminifunction (not defined here) is expected to analyze the provided code and error message, returning suggestions or fixes.
- The
🛠 Key Components
fetch()– Makes an HTTP request to the API.try...catch– Handles any errors that occur during the fetch operation.fetchData.toString()– Converts the function to a string for analysis.- Regular Expression:
- Pattern:
/try\s*\{([\s\S]*?)\}\s*catch/ - Purpose: Extracts the content inside the
tryblock.
- Pattern:
askGemini(error.message, tryBlockContent)– Sends the error and code to AI for suggestions.
🚨 Error Handling Example
Given the incorrect URL in the fetch request, the function will likely produce an error like:
TypeError: Failed to fetchThis error, along with the problematic code snippet, is sent to askGemini, which might return helpful debugging advice.
🚀 How to Use
Integrate
askGeminiFunction:
Ensure that theaskGeminifunction is correctly implemented to handle two parameters:error.message— The error description.tryBlockContent— The code snippet from thetryblock.
Run the Script:
node yourScript.jsReview Output:
- Check the console for:
- The original error message.
- The response from Gemini with possible fixes.
- Check the console for:
🔥 Potential Enhancements
Better Code Parsing:
Use a JavaScript parser (like Babel) instead of regular expressions for more robust code extraction.Enhanced Error Context:
Include stack traces or additional context when sending data toaskGemini.
