flash-tool-oss
Version:
Fast AI-powered code completion using OpenAI API - No API key setup required!
85 lines (70 loc) • 2.67 kB
JavaScript
const { spawn } = require('child_process');
const path = require('path');
class Flash {
constructor() {
this.pythonScriptPath = path.join(__dirname, 'extension.py');
}
// Function to call Python script
callPythonScript(args) {
return new Promise((resolve, reject) => {
const pythonProcess = spawn('python', [this.pythonScriptPath, ...args]);
let output = '';
let errorOutput = '';
pythonProcess.stdout.on('data', (data) => {
output += data.toString();
});
pythonProcess.stderr.on('data', (data) => {
errorOutput += data.toString();
});
pythonProcess.on('close', (code) => {
if (code === 0) {
resolve(output.trim());
} else {
reject(new Error(`Python script failed: ${errorOutput}`));
}
});
});
}
/**
* Complete code using AI
* @param {string} context - The code context to complete
* @param {string} language - Programming language (default: 'python')
* @returns {Promise<string>} The completed code
*/
async complete(context, language = 'python') {
if (!context || typeof context !== 'string') {
throw new Error('Context must be a non-empty string');
}
return await this.callPythonScript(['complete', context, language]);
}
/**
* Chat with AI assistant
* @param {string} message - The message to send to AI
* @returns {Promise<string>} The AI response
*/
async chat(message) {
if (!message || typeof message !== 'string') {
throw new Error('Message must be a non-empty string');
}
return await this.callPythonScript(['chat', message]);
}
/**
* Process "using oss" commands (for Cursor chat integration)
* @param {string} message - The full message including "using oss"
* @returns {Promise<string|null>} The AI response or null if not a "using oss" command
*/
async processUsingOss(message) {
if (!message || typeof message !== 'string') {
return null;
}
if (!message.toLowerCase().startsWith('using oss')) {
return null;
}
const prompt = message.substring(9).trim(); // Remove "using oss "
if (!prompt) {
return "Please provide a prompt after 'using oss'";
}
return await this.chat(prompt);
}
}
module.exports = Flash;