tprompter
Version:
```bash $ ask anything ```
149 lines (148 loc) • 6.91 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
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 TextDataEnricher_1;
import { InjectLogger } from '../logger/logger.decorator.js';
import { IO } from '../utils/IO.js';
import { join } from 'node:path';
import { getFilePathsFromLine } from '../utils/enrichTextData.js';
import { Service } from 'typedi';
import { parse } from 'yaml';
import { minimatch } from 'minimatch';
import { longestCommonPrefix } from '../utils/longestCommonPrefix.js';
const DEFAULT_CONFIG = {
exclude: ['node_modules', '.git', 'package-lock.json', 'yarn.lock'],
};
let TextDataEnricher = TextDataEnricher_1 = class TextDataEnricher {
constructor(io, logger) {
this.io = io;
this.logger = logger;
}
enrichRawInput(data_1) {
return __awaiter(this, arguments, void 0, function* (data, config = DEFAULT_CONFIG) {
const lines = data.split('\n');
const result = [];
for (const line of lines) {
const paths = getFilePathsFromLine(line);
if (paths.length) {
for (const path of paths) {
const content = yield this.handlePath(path, config);
result.push(...content);
}
}
else {
result.push({
content: line,
});
}
}
return this.convertPiecesToContent(result);
});
}
handlePath(path, config) {
return __awaiter(this, void 0, void 0, function* () {
const stat = yield this.io.stat(path);
if (stat.isFile()) {
return this.handleFile(path, config);
}
if (stat.isDirectory()) {
return this.handleDirectory(path, config);
}
this.logger.info(`Path is not a file or directory: ${path}`);
return [];
});
}
handleFile(path, config) {
return __awaiter(this, void 0, void 0, function* () {
this.logger.debug(`Reading file: ${path}`);
const content = yield this.io.readFile(path);
return [{ path, content }];
});
}
handleDirectory(path, config) {
return __awaiter(this, void 0, void 0, function* () {
this.logger.debug(`Reading directory: ${path}`);
const configPath = join(path, TextDataEnricher_1.CONFIGURATION_FILENAME);
let actualConfig = config;
if (yield this.io.exists(configPath)) {
const configRaw = yield this.io.readFile(configPath);
const configParsed = parse(configRaw);
this.logger.debug(`Apply configuration file: ${configPath}, ${JSON.stringify(configParsed)}`);
actualConfig = Object.assign(Object.assign({}, actualConfig), configParsed);
}
const pieces = [];
let items = yield this.io.readdir(path);
this.logger.debug(`Found ${items.length} items in directory: ${path}`);
items = items
.filter((item) => item.isFile() || item.isDirectory())
.filter((item) => item.name !== TextDataEnricher_1.CONFIGURATION_FILENAME)
.filter((item) => {
var _a, _b;
if ((_a = actualConfig.include) === null || _a === void 0 ? void 0 : _a.length) {
const res = actualConfig.include.some((pattern) => minimatch(item.name, pattern));
if (!res) {
this.logger.debug(`Skipping file because of include pattern: ${item.name}`);
}
return res;
}
if ((_b = actualConfig.exclude) === null || _b === void 0 ? void 0 : _b.length) {
const res = !actualConfig.exclude.some((pattern) => minimatch(item.name, pattern));
if (!res) {
this.logger.debug(`Skipping file because of exclude pattern: ${item.name}`);
}
return res;
}
return true;
})
.sort((a, b) => a.name.localeCompare(b.name));
for (const item of items) {
const content = yield this.handlePath(join(path, item.name), actualConfig);
pieces.push(...content);
}
return pieces;
});
}
convertPiecesToContent(pieces) {
const pathPrefix = longestCommonPrefix(pieces.map((piece) => piece.path).filter(Boolean));
let result = '';
for (const piece of pieces) {
if (piece.path) {
const relativePath = piece.path.slice(pathPrefix.length);
result += this.formatPieceName(relativePath) + '\n';
}
result += piece.content;
if (!result.endsWith('\n')) {
result += '\n';
}
}
return result;
}
formatPieceName(name) {
return `--- File: ${name} ---`;
}
};
TextDataEnricher.CONFIGURATION_FILENAME = '.promptconfig.yaml';
TextDataEnricher = TextDataEnricher_1 = __decorate([
Service(),
__param(1, InjectLogger(TextDataEnricher)),
__metadata("design:paramtypes", [IO, Object])
], TextDataEnricher);
export { TextDataEnricher };