@convo-lang/convo-lang-cli
Version:
A Conversational Language
270 lines • 10.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConvoCli = exports.createConvoCliAsync = exports.initConvoCliAsync = exports.getConvoCliConfigAsync = void 0;
const ai_complete_openai_1 = require("@convo-lang/ai-complete-openai");
const convo_lang_1 = require("@convo-lang/convo-lang");
const convo_lang_openai_1 = require("@convo-lang/convo-lang-openai");
const common_1 = require("@iyio/common");
const json5_1 = require("@iyio/json5");
const node_common_1 = require("@iyio/node-common");
const promises_1 = require("fs/promises");
const node_os_1 = require("node:os");
const convo_exec_1 = require("./convo-exec");
let configPromise = null;
const getConvoCliConfigAsync = (options) => {
return configPromise ?? (configPromise = _getConfigAsync(options));
};
exports.getConvoCliConfigAsync = getConvoCliConfigAsync;
const _getConfigAsync = async (options) => {
if (options.inlineConfig) {
const inlineConfig = (0, json5_1.parseJson5)(options.inlineConfig);
if (inlineConfig.overrideEnv === undefined) {
inlineConfig.overrideEnv = true;
}
return inlineConfig;
}
let configPath = options.config ?? '~/.config/convo/convo.json';
if (configPath.startsWith('~')) {
configPath = (0, node_os_1.homedir)() + configPath.substring(1);
}
const configExists = await (0, node_common_1.pathExistsAsync)(configPath);
if (!configExists && options.config !== undefined) {
throw new Error(`Convo config file not found a path - ${configPath}`);
}
return await (0, node_common_1.readFileAsJsonAsync)(configPath);
};
let initPromise = null;
const initConvoCliAsync = (options) => {
return initPromise ?? (initPromise = _initAsync(options));
};
exports.initConvoCliAsync = initConvoCliAsync;
const _initAsync = async (options) => {
const config = await (0, exports.getConvoCliConfigAsync)(options);
(0, common_1.initRootScope)(reg => {
if (config.env && !config.overrideEnv) {
reg.addParams(config.env);
}
if (!options.config && !options.inlineConfig) {
reg.addParams(new common_1.EnvParams());
}
if (config.env && config.overrideEnv) {
reg.addParams(config.env);
}
reg.addParams((0, common_1.deleteUndefined)({
[ai_complete_openai_1.openAiApiKeyParam.typeName]: config.apiKey,
[ai_complete_openai_1.openAiBaseUrlParam.typeName]: config.apiBaseUrl,
[ai_complete_openai_1.openAiChatModelParam.typeName]: config.chatModel,
[ai_complete_openai_1.openAiAudioModelParam.typeName]: config.audioModel,
[ai_complete_openai_1.openAiImageModelParam.typeName]: config.imageModel,
[ai_complete_openai_1.openAiVisionModelParam.typeName]: config.visionModel,
[ai_complete_openai_1.openAiSecretsParam.typeName]: config.secrets,
[convo_lang_1.convoCapabilitiesParams.typeName]: config.capabilities,
}));
reg.use(node_common_1.nodeCommonModule);
reg.use(convo_lang_openai_1.openaiConvoModule);
});
await common_1.rootScope.getInitPromise();
return config;
};
/**
* Initializes the ConvoCli environment the returns a new ConvoCli object
*/
const createConvoCliAsync = async (options) => {
await (0, exports.initConvoCliAsync)(options);
return new ConvoCli(options);
};
exports.createConvoCliAsync = createConvoCliAsync;
const appendOut = (prefix, value, out) => {
if (typeof value !== 'string') {
value = JSON.stringify(value, (0, common_1.createJsonRefReplacer)(), 4);
}
if (!prefix) {
out.push(value + '\n');
}
else {
const ary = value.split('\n');
for (let i = 0; i < ary.length; i++) {
out.push(prefix + ary[i] + '\n');
}
}
};
class ConvoCli {
constructor(options) {
this.buffer = [];
this._isDisposed = false;
this.dynamicFunctionCallback = (scope) => {
return new Promise((r, j) => {
(0, node_common_1.readStdInLineAsync)().then(v => {
if (v.startsWith('ERROR:')) {
j(v.substring(6).trim());
return;
}
if (v.startsWith('RESULT:')) {
v = v.substring(7);
}
v = v.trim();
try {
r(JSON.parse(v));
}
catch (ex) {
j(ex);
}
});
const fn = scope.s.fn ?? 'function';
globalThis.process?.stdout.write(`CALL:${JSON.stringify({ fn, args: scope.paramValues ?? [] })}\n`);
});
};
this.execConfirmAsync = async (command) => {
if (this.allowExec === 'allow') {
return true;
}
else if (this.allowExec === 'ask') {
process.stdout.write(`Exec command requested\n> ${command}\nAllow y/N?\n`);
const line = (await (0, node_common_1.readStdInLineAsync)()).toLowerCase();
return line === 'yes' || line === 'y';
}
else {
return false;
}
};
this.allowExec = options.allowExec;
this.options = options;
this.convo = (0, convo_lang_1.createConversationFromScope)(common_1.rootScope);
if (options.cmdMode) {
this.convo.dynamicFunctionCallback = this.dynamicFunctionCallback;
(0, node_common_1.startReadingStdIn)();
}
if (options.prepend) {
this.convo.append(options.prepend);
}
if (this.options.exeCwd) {
this.convo.unregisteredVars[convo_lang_1.convoVars.__cwd] = this.options.exeCwd;
}
}
get isDisposed() { return this._isDisposed; }
dispose() {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
this.convo.dispose();
}
async outAsync(...chunks) {
const { prefixOutput, cmdMode } = this.options;
if (prefixOutput) {
const str = chunks.join('');
chunks = [];
appendOut(':', str, chunks);
}
if (this.options.printFlat || this.options.printState) {
await this.convo.flattenAsync();
}
if (this.options.printFlat) {
const messages = this.convo.flat?.messages ?? [];
for (const m of messages) {
if (m.fn?.body) {
delete m.fn.body;
}
}
if (cmdMode) {
chunks.push('FLAT:\n');
}
appendOut(prefixOutput ? 'f:' : null, messages, chunks);
}
if (this.options.printState) {
const vars = this.convo.flat?.exe.getUserSharedVars() ?? {};
if (cmdMode) {
chunks.push('STATE:\n');
}
appendOut(prefixOutput ? 's:' : null, vars, chunks);
}
if (this.options.printMessages) {
const messages = this.convo.messages;
if (cmdMode) {
chunks.push('MESSAGES:\n');
}
appendOut(prefixOutput ? 'm:' : null, messages, chunks);
}
if (cmdMode) {
chunks.push('END:\n');
}
if (this.options.out || this.options.bufferOutput) {
if (typeof this.options.out === 'function') {
this.options.out(...chunks);
}
else {
this.buffer.push(...chunks);
}
}
else {
if (globalThis?.process?.stdout) {
for (let i = 0; i < chunks.length; i++) {
globalThis.process.stdout.write(chunks[i] ?? '');
}
}
else {
console.log(chunks.join(''));
}
}
}
async executeAsync() {
const config = await (0, exports.initConvoCliAsync)(this.options);
if (!this.allowExec) {
this.allowExec = config.allowExec ?? 'ask';
}
if (this.options.inline) {
if (this.options.parse) {
await this.parseCodeAsync(this.options.inline);
}
else {
await this.executeSourceCode(this.options.inline);
}
this.writeOutputAsync();
}
else if (this.options.source || this.options.stdin) {
const source = this.options.stdin ?
await (0, node_common_1.readStdInAsStringAsync)() :
await (0, node_common_1.readFileAsStringAsync)(this.options.source ?? '');
if (this.options.parse) {
await this.parseCodeAsync(source);
}
else {
await this.executeSourceCode(source);
}
this.writeOutputAsync();
}
}
async executeSourceCode(code) {
this.convo.defineFunction({
name: 'exec',
registerOnly: true,
scopeCallback: (0, convo_exec_1.createConvoExec)(typeof this.allowExec === 'function' ?
this.allowExec : this.execConfirmAsync)
});
this.convo.append(code);
const r = await this.convo.completeAsync();
if (r.error) {
throw r.error;
}
await this.outAsync(this.convo.convo);
}
async parseCodeAsync(code) {
const r = (0, convo_lang_1.parseConvoCode)(code);
if (r.error) {
throw r.error;
}
await this.outAsync(JSON.stringify(r.result, null, this.options.parseFormat));
}
async writeOutputAsync() {
let out = this.options.out;
if (out === '.') {
out = this.options.source;
}
if (typeof out !== 'string') {
return;
}
await (0, promises_1.writeFile)(out, this.buffer.join(''));
}
}
exports.ConvoCli = ConvoCli;
//# sourceMappingURL=ConvoCli.js.map