276 lines
10 KiB
JavaScript
276 lines
10 KiB
JavaScript
"use strict";
|
|
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 () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.complete = exports.completeAssistant = exports.messages = exports.parse = exports.pathVariables = exports.targets = exports.fromUI = void 0;
|
|
const path = __importStar(require("path"));
|
|
const globBase = require('glob-base');
|
|
const read_1 = require("@plastichub/fs/read");
|
|
const exists_1 = require("@plastichub/fs/exists");
|
|
const primitives_1 = require("@plastichub/core/primitives");
|
|
const osr_commons_1 = require("@plastichub/osr-commons");
|
|
const osr_commons_2 = require("@plastichub/osr-commons");
|
|
const osr_commons_3 = require("@plastichub/osr-commons");
|
|
const types_1 = require("./types");
|
|
const electron_1 = require("../ui/electron");
|
|
const index_1 = require("./index");
|
|
const index_2 = require("../../index");
|
|
const filters_1 = require("../filters");
|
|
const fromUI = async (options, client) => {
|
|
let ui_opts = null;
|
|
let ui_opts_variables = {};
|
|
if (options.gui === 'electron') {
|
|
const promptsFile = path.resolve((0, osr_commons_2.resolve)(options.prompts));
|
|
const prompts = JSON.stringify((0, read_1.sync)(promptsFile, 'json') || []);
|
|
ui_opts = await (0, electron_1.web_prompt)(options.query, options.dst, {
|
|
...ui_opts_variables,
|
|
PROMPTS: prompts,
|
|
MODEL: options.model,
|
|
FILES: (options.files || []).map((f) => `<li>${f}</li>`).join('<br/>'),
|
|
MODELS: JSON.stringify(Object.values(types_1.EModels), null, 2)
|
|
});
|
|
}
|
|
if (ui_opts) {
|
|
if (ui_opts.files && ui_opts.files.length > 0) {
|
|
options.files = ui_opts.files;
|
|
}
|
|
if (ui_opts.textAreaValue !== options.query) {
|
|
options.query = ui_opts.textAreaValue;
|
|
}
|
|
if (ui_opts.target.length && ui_opts.target !== options.dst) {
|
|
options.dst = ui_opts.target;
|
|
}
|
|
}
|
|
/*
|
|
const attachments = await Promise.all((options.files as string[]).map(async (file: string) => {
|
|
const file_id = await createOpenAIFile(client, file)
|
|
return {
|
|
file_id: file_id.id,
|
|
tools: [{ type: "file_search" }]
|
|
}
|
|
}))*/
|
|
return options;
|
|
};
|
|
exports.fromUI = fromUI;
|
|
const targets = (f, options) => {
|
|
const srcParts = path.parse(f);
|
|
const variables = {
|
|
...options.variables,
|
|
...osr_commons_1.DEFAULT_ROOTS
|
|
};
|
|
const targets = [];
|
|
variables.SRC_NAME = srcParts.name;
|
|
variables.SRC_DIR = srcParts.dir;
|
|
variables.SRC_EXT = srcParts.ext;
|
|
if (variables.ROOT) {
|
|
variables.SRC_REL = path.relative(variables.ROOT, srcParts.dir);
|
|
}
|
|
const dashed = srcParts.name.split('-');
|
|
if (dashed.length > 1) {
|
|
for (let i = 0; i < dashed.length; i++) {
|
|
variables[`SRC_NAME-${i}`] = dashed[i];
|
|
}
|
|
}
|
|
const dotted = srcParts.name.split('.');
|
|
if (dotted.length > 1) {
|
|
for (let i = 0; i < dotted.length; i++) {
|
|
variables[`SRC_NAME.${i}`] = dotted[i];
|
|
}
|
|
}
|
|
const underscored = srcParts.name.split('_');
|
|
if (underscored.length > 1) {
|
|
for (let i = 0; i < underscored.length; i++) {
|
|
variables[`SRC_NAME_${i}`] = underscored[i];
|
|
}
|
|
}
|
|
if (options.targetInfo && options.targetInfo.IS_GLOB) {
|
|
options.targetInfo.GLOB_EXTENSIONS.forEach((e) => {
|
|
let targetPath = (0, osr_commons_2.resolve)(options.variables.DST_PATH, options.alt, variables);
|
|
targetPath = path.resolve(targetPath.replace(options.variables.DST_FILE_EXT, '') + e);
|
|
targets.push(targetPath);
|
|
});
|
|
}
|
|
else {
|
|
let targetPath = (0, osr_commons_2.resolve)(options.dst, options.alt, variables);
|
|
//targetPath = path.resolve(targetPath.replace(options.pathVariables.DST_FILE_EXT, ''));
|
|
targets.push(targetPath);
|
|
}
|
|
return targets;
|
|
};
|
|
exports.targets = targets;
|
|
const updateVariables = (filePath, prefix, variables) => {
|
|
const srcParts = path.parse(filePath);
|
|
variables[`${prefix}_NAME`] = srcParts.name;
|
|
variables[`${prefix}_DIR`] = srcParts.dir;
|
|
variables[`${prefix}_EXT`] = srcParts.ext;
|
|
if (variables.ROOT) {
|
|
variables[`${prefix}_REL`] = path.relative(variables.ROOT, srcParts.dir);
|
|
}
|
|
const dashed = srcParts.name.split('-');
|
|
if (dashed.length > 1) {
|
|
for (let i = 0; i < dashed.length; i++) {
|
|
variables[`${prefix}_NAME-${i}`] = dashed[i];
|
|
}
|
|
}
|
|
const dotted = srcParts.name.split('.');
|
|
if (dotted.length > 1) {
|
|
for (let i = 0; i < dotted.length; i++) {
|
|
variables[`${prefix}_NAME.${i}`] = dotted[i];
|
|
}
|
|
}
|
|
const underscored = srcParts.name.split('_');
|
|
if (underscored.length > 1) {
|
|
for (let i = 0; i < underscored.length; i++) {
|
|
variables[`${prefix}_NAME_${i}`] = underscored[i];
|
|
}
|
|
}
|
|
};
|
|
const pathVariables = (options) => {
|
|
const variables = {
|
|
...options.variables,
|
|
...osr_commons_1.DEFAULT_ROOTS
|
|
};
|
|
if (options.source) {
|
|
updateVariables(options.source, 'SRC', variables);
|
|
}
|
|
if (options.dst) {
|
|
updateVariables(options.dst, 'DST', variables);
|
|
}
|
|
return variables;
|
|
};
|
|
exports.pathVariables = pathVariables;
|
|
const parse = (options, argv = {}) => {
|
|
let variables = options.variables || {};
|
|
if (options.files) {
|
|
let srcInfo = null;
|
|
if (options.files && (0, osr_commons_2.isFile)(options.files)) {
|
|
options.files = [options.files];
|
|
}
|
|
else {
|
|
let files = options.files;
|
|
const srcIn = (0, osr_commons_2.resolve)(files, options.alt, variables);
|
|
files = (0, osr_commons_3.forward_slash)((0, osr_commons_1.substitute)(options.alt, srcIn, variables));
|
|
const glob_base = globBase(options.files);
|
|
const file = files.replace(glob_base.glob, '').replace(/\/$/, '');
|
|
if ((0, exists_1.sync)(file) && (0, osr_commons_2.isFile)(file)) {
|
|
files = file;
|
|
}
|
|
srcInfo = (0, osr_commons_3.pathInfo)((0, osr_commons_2.resolve)(files, options.alt, variables));
|
|
if (srcInfo && srcInfo.FILES && srcInfo.FILES.length) {
|
|
options.filesInfo = srcInfo;
|
|
for (const key in srcInfo) {
|
|
if (Object.prototype.hasOwnProperty.call(srcInfo, key)) {
|
|
variables['SRC_' + key] = srcInfo[key];
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
options.files = (0, osr_commons_2.resolve)(files, options.alt, variables);
|
|
}
|
|
}
|
|
}
|
|
if (options.filesInfo && options.filesInfo.FILES.length) {
|
|
options.files = options.filesInfo.FILES;
|
|
}
|
|
if (options.files && (0, primitives_1.isString)(options.files)) {
|
|
options.files = [options.files];
|
|
}
|
|
const filters = (0, primitives_1.isString)(options.filters) ? (options.filters || '').split(',') : options.filters;
|
|
options.filters = [];
|
|
filters.forEach((f) => {
|
|
if (filters_1.Filters[f]) {
|
|
(options.filters).push(filters_1.Filters[f]);
|
|
}
|
|
});
|
|
options.variables = variables;
|
|
options.files = options.files ? options.files : [];
|
|
options.query = (0, osr_commons_1.substitute)(false, options.query || '', variables);
|
|
return options;
|
|
};
|
|
exports.parse = parse;
|
|
const messages = (options, defaults = []) => {
|
|
let messages = [];
|
|
if (options.system && (0, exists_1.sync)(options.system)) {
|
|
options.debug && index_2.logger.debug('Reading system instructions from', options.system);
|
|
try {
|
|
const system = ((0, read_1.sync)(options.system, 'json') || []);
|
|
if (system) {
|
|
// messages = [...system as []]
|
|
}
|
|
}
|
|
catch (error) {
|
|
index_2.logger.error('Error reading system instructions', error);
|
|
messages = defaults;
|
|
}
|
|
}
|
|
else {
|
|
messages = defaults;
|
|
}
|
|
return messages;
|
|
};
|
|
exports.messages = messages;
|
|
const completeAssistant = async (options, client, defaults = []) => {
|
|
const opts = options;
|
|
opts.attachments = await Promise.all(options.files.map(async (file) => {
|
|
const file_id = await (0, index_1.createOpenAIFile)(client, file);
|
|
return {
|
|
file_id: file_id.id,
|
|
tools: [{ type: "file_search" }]
|
|
};
|
|
}));
|
|
return opts;
|
|
};
|
|
exports.completeAssistant = completeAssistant;
|
|
const complete = async (options, defaults = []) => {
|
|
const opts = options;
|
|
opts.images = (0, index_1.toImages)(options.files, options);
|
|
opts.messages = [
|
|
...opts.images.map((i) => {
|
|
return {
|
|
role: "user",
|
|
content: [
|
|
{ ...i } /*,
|
|
{
|
|
type: "text",
|
|
content: ""
|
|
}*/
|
|
]
|
|
};
|
|
}),
|
|
...(0, exports.messages)(options, defaults)
|
|
];
|
|
return opts;
|
|
};
|
|
exports.complete = complete;
|
|
//# sourceMappingURL=options.js.map
|