initial commit

This commit is contained in:
Matyi Kari 2025-02-25 12:59:32 -06:00
commit 3157e4e0dd
5 changed files with 144 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
.env
dist

29
package.json Normal file
View File

@ -0,0 +1,29 @@
{
"name": "zzk",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"build": "rimraf dist && webpack",
"start": "node -r dotenv/config dist/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^22.13.5",
"rimraf": "^6.0.1",
"ts-loader": "^9.5.2",
"ts-node": "^10.9.2",
"typescript": "^5.7.3",
"webpack": "^5.98.0",
"webpack-cli": "^6.0.1"
},
"dependencies": {
"child_process": "^1.0.2",
"dotenv": "^16.4.7",
"openai": "^4.85.4",
"readline": "^1.3.0"
}
}

80
src/index.ts Normal file
View File

@ -0,0 +1,80 @@
import { config } from 'dotenv';
import { exec } from 'child_process';
import OpenAI from 'openai';
import readline from 'readline';
// Load environment variables from .env
config();
if (!process.env.OPENAI_API_KEY) {
console.error('Error: Missing OpenAI API key. Set OPENAI_API_KEY in .env');
process.exit(1);
}
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
// Get command from CLI arguments
const userInput = process.argv.slice(2).join(' ');
if (!userInput) {
console.log('Usage: ai-cli <your natural language command>');
process.exit(1);
}
// Function to prompt for confirmation
function confirmExecution(command: string) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(`\nRun this command? [y/N]: ${command} `, (answer) => {
rl.close();
if (answer.toLowerCase() === 'y') {
executeCommand(command);
} else {
console.log('Command execution canceled.');
process.exit(0);
}
});
}
// Function to execute command
function executeCommand(command: string) {
console.log(`\nExecuting: ${command}\n`);
exec(command, (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${stderr}`);
process.exit(1);
}
console.log(stdout);
});
}
// Function to process input using AI
async function processCommand(input: string) {
console.log(`Thinking...`);
const response = await openai.chat.completions.create({
model: 'gpt-4-turbo',
messages: [
{
role: 'system',
content:
"Convert the user's request into a shell command without explanation."
},
{ role: 'user', content: input }
]
});
const command = response.choices[0]?.message?.content?.trim();
if (!command) {
console.log("Couldn't generate a command.");
process.exit(1);
}
confirmExecution(command);
}
processCommand(userInput);

9
tsconfig.json Normal file
View File

@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "CommonJS",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true
}
}

23
webpack.config.js Normal file
View File

@ -0,0 +1,23 @@
const path = require('path');
module.exports = {
entry: './src/index.ts',
target: 'node',
mode: 'production',
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist')
},
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
}
};