rosie-cli
Version:
AI-powered command-line interface tool that uses OpenAI's API to help you interact with your computer through natural language
136 lines (135 loc) • 6.08 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// Silence the punycode deprecation warning
process.removeAllListeners('warning');
process.on('warning', (warning) => {
if (warning.name === 'DeprecationWarning' && warning.message.includes('punycode')) {
return;
}
console.warn(warning.name, warning.message);
});
const commander_1 = require("commander");
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const configManager_1 = require("./configManager");
const main_1 = __importDefault(require("./services/main"));
const executionContext_1 = require("./types/executionContext");
const uuid_1 = require("uuid");
const screenshot_1 = require("./lib/screenshot/screenshot");
const chatgpt_1 = require("./lib/chatgpt");
// Generate a session ID for this CLI instance
// This will remain the same for the duration of the CLI window
// Read package.json to get the version
const packageJsonPath = path_1.default.join(__dirname, '..', 'package.json');
const packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonPath, 'utf-8'));
// Initialize config manager
const configManager = new configManager_1.ConfigManager();
const program = new commander_1.Command();
program
.name('rosie')
.description('A CLI tool for Rosie')
.version(packageJson.version)
.option('--open_ai_key <key>', 'Your OpenAI API key');
// Add config command
program
.command('config')
.description('Configure Rosie CLI settings')
.option('--set-openai-key <key>', 'Set your OpenAI API key')
.option('--set-conversation-id <id>', 'Set active conversation ID')
.option('--show', 'Show current configuration')
.action((options) => {
if (options.setOpenaiKey) {
configManager.setOpenAIKey(options.setOpenaiKey);
console.log('✅ OpenAI API key saved successfully');
}
else if (options.setConversationId) {
configManager.setActiveConversationId(options.setConversationId);
console.log('✅ Active conversation ID set to:', options.setConversationId);
}
else if (options.show) {
const savedKey = configManager.getOpenAIKey();
const activeConversationId = configManager.getActiveConversationId();
if (savedKey) {
// Show just the first and last few characters for security
const maskedKey = savedKey.substring(0, 4) + '...' + savedKey.substring(savedKey.length - 4);
console.log('OpenAI API key:', maskedKey);
}
else {
console.log('No OpenAI API key configured');
}
if (activeConversationId) {
console.log('Active conversation ID:', activeConversationId);
}
else {
console.log('No active conversation ID configured (using new conversation for each session)');
}
}
else {
console.log('Use --set-openai-key to save your OpenAI API key, --set-conversation-id to set active conversation ID, or --show to view current config');
}
});
// Add default command that processes any text input
program
.arguments('[text...]')
.description('Process any text input')
.option('--thinking', 'Enable thinking mode.')
.action((text, options) => __awaiter(void 0, void 0, void 0, function* () {
const globalOptions = program.opts();
const openAIKey = globalOptions.open_ai_key || configManager.getOpenAIKey();
if (!text || text.length === 0) {
// No text provided, show help
program.outputHelp();
return;
}
const userInput = text.join(' ');
if (openAIKey) {
const mainService = new main_1.default();
// Use the active conversation ID from config if available, otherwise use the session ID
let conversationId = configManager.getActiveConversationId();
if (!conversationId) {
conversationId = (0, uuid_1.v4)();
configManager.setActiveConversationId(conversationId);
}
// Create context with the conversation ID
const context = new executionContext_1.ExecutionContext(conversationId);
context.params.openAiKey = openAIKey;
context.params.thinking = options.thinking || false;
const result = yield mainService.processMessage({
text: userInput,
ts: Date.now(),
historyId: conversationId
}, context);
console.log("> " + result.answer.text);
}
else {
console.log('No OpenAI API key provided. Use --open_ai_key or run "rosie config --set-openai-key=YOUR_KEY"');
}
}));
program
.command('test')
.description('Test command that does nothing')
.action(() => __awaiter(void 0, void 0, void 0, function* () {
const screenshots = yield (0, screenshot_1.getScreenshots)();
console.log(screenshots);
const imageBuffer = screenshots[0].buffer;
const prompt = "What is in this image?";
const ctx = new executionContext_1.ExecutionContext((0, uuid_1.v4)());
ctx.params.openAiKey = configManager.getOpenAIKey();
const result = yield (0, chatgpt_1.analyzeImage)(imageBuffer, prompt, ctx);
console.log(result);
}));
// Parse command line arguments
program.parse(process.argv);