ai-code-writer
Version:
An AI code writer application using OpenAI APIs for audio transcription and chat completion.
100 lines (99 loc) • 4.84 kB
JavaScript
"use strict";
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 });
const AudioResponse_1 = __importDefault(require("./Response/AudioResponse"));
const ConversationRequest_1 = __importDefault(require("./ConversationRequest"));
const AddToConversationHistoryRequest_1 = __importDefault(require("./AddToConversationHistoryRequest"));
class StartController {
constructor(audioUseCase, gptConversationUseCase, directoryWatcher, conversationHandler, exitHandler, pauseHandler) {
this.audioUseCase = audioUseCase;
this.gptConversationUseCase = gptConversationUseCase;
this.directoryWatcher = directoryWatcher;
this.conversationHandler = conversationHandler;
this.exitHandler = exitHandler;
this.pauseHandler = pauseHandler;
this.directoryWatcher.onChange(this.handleDirectoryChange.bind(this));
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', this.handleKeyPress.bind(this));
process.on('SIGINT', this.exitHandler.exitProgramm.bind(this.exitHandler));
}
start() {
return __awaiter(this, void 0, void 0, function* () {
yield this.audioUseCase.measureNoiseLevel();
yield this.gptConversationUseCase.initialize();
yield this.introduceWithAi();
yield this.gptConversationUseCase.addProjectFiles();
this.directoryWatcher.startWatching();
yield this.main();
});
}
introduceWithAi() {
return __awaiter(this, void 0, void 0, function* () {
const introductionRequest = new ConversationRequest_1.default();
introductionRequest.role = 'system';
introductionRequest.transcription = 'Bitte begrüße den Benutzer.';
yield this.gptConversationUseCase.addUserMessageToConversation(introductionRequest);
yield this.conversationHandler.completeConversation();
});
}
main() {
return __awaiter(this, void 0, void 0, function* () {
// noinspection InfiniteLoopJS
while (true) {
const pauseState = this.pauseHandler.getPauseState();
if (pauseState.isPaused) {
yield new Promise(resolve => setTimeout(resolve, 1000));
continue;
}
const response = new AudioResponse_1.default();
console.log('Assistent hört...');
yield this.audioUseCase.recordAndProcess(response);
if (response.transcription == '')
continue;
console.log('Ihre Eingabe:', response.transcription);
yield this.conversationHandler.runConversation(response);
}
});
}
handleDirectoryChange(_, filePath, content) {
return __awaiter(this, void 0, void 0, function* () {
const addToConversationHistoryRequest = new AddToConversationHistoryRequest_1.default();
addToConversationHistoryRequest.fileName = filePath;
addToConversationHistoryRequest.content = content
? 'Changed file ' + filePath + `:
\`\`\`
${content}
\`\`\`
`
: 'File ' + filePath + ' was deleted by user.';
addToConversationHistoryRequest.role = 'system';
yield this.gptConversationUseCase.addToConversationHistory(addToConversationHistoryRequest);
// console.log('Datei geändert:', '(' + action + ')' + filePath);
});
}
handleKeyPress(key) {
return __awaiter(this, void 0, void 0, function* () {
const keyString = key.toString();
let charCode = [...key.values()][0] || 0;
if (keyString.toLowerCase() === 'p') {
yield this.pauseHandler.pause();
}
else if (keyString.toLowerCase() === 'e' || charCode == 3) {
this.exitHandler.exitProgramm();
}
});
}
}
exports.default = StartController;