giga-code
Version:
A personal AI CLI assistant powered by Grok for local development.
163 lines ⢠7.23 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const ink_1 = require("ink");
const commander_1 = require("commander");
const dotenv = __importStar(require("dotenv"));
const giga_agent_1 = require("./agent/giga-agent");
const chat_interface_1 = __importDefault(require("./ui/components/chat-interface"));
const confirmation_service_1 = require("./utils/confirmation-service");
// Load environment variables
dotenv.config();
const api_keys_1 = require("./utils/api-keys");
const config_initializer_1 = require("./utils/config-initializer");
// Initialize global configuration on startup
(0, config_initializer_1.initializeGlobalConfig)();
// We'll set up the double Ctrl+C handler after Ink starts
// Check if any API keys are available from environment variables, shell files, or settings
function hasAnyApiKey() {
const apiKeys = (0, api_keys_1.loadApiKeys)();
const availableKeys = Object.values(apiKeys).filter(Boolean);
return availableKeys.length > 0;
}
commander_1.program
.name("giga")
.description("A conversational AI CLI tool powered by Grok-3 with text editor capabilities")
.version("1.0.0")
.option("-p, --prompt <prompt>", "run a single prompt in headless mode")
.option("-d, --directory <dir>", "set working directory", process.cwd())
.action(async (options) => {
if (options.directory) {
try {
process.chdir(options.directory);
}
catch (error) {
console.error(`ā Error changing directory to ${options.directory}:`, error.message);
process.exit(1);
}
}
// If prompt is provided, run in headless mode
if (options.prompt) {
try {
// Enable headless mode in confirmation service
const confirmationService = confirmation_service_1.ConfirmationService.getInstance();
confirmationService.setHeadlessMode(true);
// Check if any API keys are available
const hasKeys = hasAnyApiKey();
if (!hasKeys) {
console.error("ā Error: API key is required. Set environment variables or configure in settings");
process.exit(1);
}
const agent = new giga_agent_1.GigaAgent('', '');
// Check if a model is configured
const currentModel = agent.getCurrentModel();
if (!currentModel) {
console.error("ā No model configured. Please set up giga first:");
console.error("1. Run 'giga' to enter interactive mode");
console.error("2. Configure API keys: /providers");
console.error("3. Add models: /add-model");
console.error("4. Select a model: /models");
process.exit(1);
}
console.log(`š¤ Processing prompt: ${options.prompt}`);
console.log(`š Working directory: ${process.cwd()}\n`);
// Process the prompt and stream results
for await (const chunk of agent.processUserMessageStream(options.prompt)) {
if (chunk.type === 'content') {
process.stdout.write(chunk.content || '');
}
else if (chunk.type === 'tool_calls') {
console.log(`\nš§ Using tools: ${chunk.toolCalls?.map(tc => tc.function.name).join(', ')}`);
}
else if (chunk.type === 'tool_result') {
if (!chunk.toolResult?.success) {
console.log(`\nā Tool error: ${chunk.toolResult?.error}`);
}
}
else if (chunk.type === 'done') {
console.log('\n\nā
Done');
break;
}
}
}
catch (error) {
console.error("ā Error executing prompt:", error.message);
process.exit(1);
}
}
else {
// Run in interactive mode
try {
// Check if any API keys are available
const hasKeys = hasAnyApiKey();
const agent = hasKeys ? new giga_agent_1.GigaAgent('', '') : undefined;
const app = (0, ink_1.render)(react_1.default.createElement(chat_interface_1.default, { agent }));
// Set up double Ctrl+C handler after Ink is running
let lastCtrlCTime = 0;
let ctrlCTimeout = null;
// Remove any existing SIGINT listeners first
process.removeAllListeners('SIGINT');
process.on('SIGINT', () => {
const now = Date.now();
const timeSinceLastCtrlC = now - lastCtrlCTime;
if (timeSinceLastCtrlC < 1000 && lastCtrlCTime > 0) {
// Second Ctrl+C within 1 second - exit immediately
if (ctrlCTimeout) {
clearTimeout(ctrlCTimeout);
ctrlCTimeout = null;
}
console.log('\nš Goodbye!');
app.unmount();
process.exit(0);
}
else {
// First Ctrl+C or too late - show message and start timer
lastCtrlCTime = now;
console.log('\nPress Ctrl+C again within 1 second to exit');
// Clear any existing timeout
if (ctrlCTimeout) {
clearTimeout(ctrlCTimeout);
}
// Reset the timer after 1 second
ctrlCTimeout = setTimeout(() => {
lastCtrlCTime = 0;
ctrlCTimeout = null;
}, 1000);
}
});
}
catch (error) {
console.error("ā Error initializing GIGA:", error.message);
process.exit(1);
}
}
});
commander_1.program.parse();
//# sourceMappingURL=index.js.map