ai-code-writer
Version:
An AI code writer application using OpenAI APIs for audio transcription and chat completion.
76 lines (75 loc) • 3.47 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 ActionType_1 = __importDefault(require("../ActionType"));
class GptResponseProcessor {
constructor(commandHandlers) {
this.commandHandlers = commandHandlers;
}
processResponse(response) {
return __awaiter(this, void 0, void 0, function* () {
const correctedResponse = this.ensureInitialCommandWord(response);
const lines = correctedResponse.split('\n');
let currentSection = [];
let comments = [];
let actions = [];
for (const line of lines) {
if (this.isCommand(line)) {
if (currentSection.length > 0) {
const { comments: sectionComments, actions: sectionActions } = yield this.processSection(currentSection);
comments.push(...sectionComments);
actions.push(...sectionActions);
currentSection = [];
}
}
currentSection.push(line);
}
if (currentSection.length > 0) {
const { comments: sectionComments, actions: sectionActions } = yield this.processSection(currentSection);
comments.push(...sectionComments);
actions.push(...sectionActions);
}
return { comments: comments, actions: actions };
});
}
processSection(section) {
return __awaiter(this, void 0, void 0, function* () {
const command = this.removePrefix(section[0]);
for (const handler of this.commandHandlers) {
if (handler.canHandle(command)) {
const processedSection = section.map(line => this.removePrefix(line));
return handler.handle(processedSection);
}
}
return { comments: [], actions: [] };
});
}
isCommand(line) {
return (line.startsWith(ActionType_1.default.COMMENT) ||
line.startsWith(ActionType_1.default.FILE_WRITE) ||
line.startsWith(ActionType_1.default.FILE_DELETE));
}
removePrefix(line) {
const prefix = '^°µ|';
return line.startsWith(prefix) ? line.slice(prefix.length) : line;
}
ensureInitialCommandWord(response) {
const lines = response.split('\n');
if (lines.length > 0 && !this.isCommand(lines[0])) {
lines[0] = `${ActionType_1.default.COMMENT}${lines[0]}`;
}
return lines.join('\n');
}
}
exports.default = GptResponseProcessor;
;