ai-code-writer
Version:
An AI code writer application using OpenAI APIs for audio transcription and chat completion.
84 lines (83 loc) • 4.24 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 });
const CommandActionType_1 = __importDefault(require("../../Core/CommandActionType"));
const AddToConversationHistoryRequest_1 = __importDefault(require("../AddToConversationHistoryRequest"));
class ActionHandler {
constructor(directoryWatcher, fileActionExecutor, modeUseCase, audioUseCase, conversationUseCase, pauseHandler, exitHandler) {
this.directoryWatcher = directoryWatcher;
this.fileActionExecutor = fileActionExecutor;
this.modeUseCase = modeUseCase;
this.audioUseCase = audioUseCase;
this.conversationUseCase = conversationUseCase;
this.pauseHandler = pauseHandler;
this.exitHandler = exitHandler;
}
executeActions(conversationResponse) {
return __awaiter(this, void 0, void 0, function* () {
const toolCalls = conversationResponse.result.toolCalls;
const fileActions = toolCalls.filter(t => t.actionType == 'file');
const commandActions = toolCalls.filter(t => t.actionType == 'command');
yield Promise.all([
this.executeFileActions(fileActions),
this.executeCommandActions(commandActions, conversationResponse)
]);
});
}
executeFileActions(toolCalls) {
return __awaiter(this, void 0, void 0, function* () {
this.directoryWatcher.pauseWatching();
for (const call of toolCalls)
yield this.fileActionExecutor.executeCall(call);
this.directoryWatcher.resumeWatching();
});
}
executeCommandActions(toolCalls, conversationResponse) {
return __awaiter(this, void 0, void 0, function* () {
for (const action of toolCalls) {
yield this.addToHistory(action, 'Command executed.');
switch (action.type) {
case CommandActionType_1.default.PAUSE:
yield this.pauseHandler.pause();
break;
case CommandActionType_1.default.SUSPEND:
console.log('Gehe in den Schlummer-Modus.');
this.modeUseCase.enableSuspendMode();
conversationResponse.conversationComplete = true;
break;
case CommandActionType_1.default.RESUME:
console.log('Gehen zurück in den Normal-Modus.');
this.modeUseCase.disableSuspendMode();
yield this.conversationUseCase.wakeUpConversation();
conversationResponse.conversationComplete = false;
break;
case CommandActionType_1.default.EXIT_PROGRAMM:
this.exitHandler.exitProgramm();
break;
}
}
});
}
addToHistory(fileAction, resultContent) {
return __awaiter(this, void 0, void 0, function* () {
const request = new AddToConversationHistoryRequest_1.default();
request.callId = fileAction.id;
request.fileName = fileAction.filePath;
request.content = resultContent;
request.role = 'tool';
yield this.conversationUseCase.addToConversationHistory(request);
});
}
}
exports.default = ActionHandler;
;