@convo-lang/convo-lang-cli
Version:
The language of AI
386 lines (383 loc) • 14.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConvoCli = exports.createConvoCliAsync = exports.initConvoCliAsync = exports.getConvoCliConfigAsync = void 0;
const convo_lang_1 = require("@convo-lang/convo-lang");
const convo_lang_bedrock_1 = require("@convo-lang/convo-lang-bedrock");
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 zod_1 = require("zod");
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)({
[convo_lang_1.openAiApiKeyParam.typeName]: config.apiKey,
[convo_lang_1.openAiBaseUrlParam.typeName]: config.apiBaseUrl,
[convo_lang_1.openAiChatModelParam.typeName]: config.chatModel,
[convo_lang_1.openAiAudioModelParam.typeName]: config.audioModel,
[convo_lang_1.openAiImageModelParam.typeName]: config.imageModel,
[convo_lang_1.openAiVisionModelParam.typeName]: config.visionModel,
[convo_lang_1.openAiSecretsParam.typeName]: config.secrets,
[convo_lang_1.convoCapabilitiesParams.typeName]: config.capabilities,
[convo_lang_1.convoDefaultModelParam.typeName]: 'gpt-4.1',
}));
reg.use(node_common_1.nodeCommonModule);
reg.use(convo_lang_1.convoOpenAiModule);
reg.use(convo_lang_bedrock_1.convoBedrockModule);
});
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, {
importHandler: async (_import) => {
const isFileHttp = (0, common_1.isHttp)(_import.name);
const startDir = isFileHttp ? undefined : globalThis.process?.cwd();
try {
if (_import.sourceDirectory) {
globalThis.process?.chdir(_import.sourceDirectory);
}
const [file, filePath] = await Promise.all([
isFileHttp ? (0, common_1.httpClient)().getStringAsync(_import.name) : (0, node_common_1.readFileAsStringAsync)(_import.name),
isFileHttp ? _import.name : (0, promises_1.realpath)(_import.name),
]);
return {
name: _import.name,
convo: file,
filePath
};
}
finally {
if (startDir) {
globalThis.process.chdir(startDir);
}
}
}
});
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;
}
if (this.options.source) {
this.convo.unregisteredVars[convo_lang_1.convoVars.__file] = this.options.source;
}
}
get isDisposed() { return this._isDisposed; }
dispose() {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
this.convo.dispose();
}
async runReplAsync(cancel) {
if (!this.allowExec) {
this.allowExec = 'ask';
}
this.registerExec(true);
this.convo.append(/*convo*/ `
> do
__cwd=_getCwd()
> system
The current working directory is: "{{__cwd}}"
The current date and time is: "{{dateTime()}}"
`);
(0, node_common_1.startReadingStdIn)();
let len = this.convo.convo.length;
console.log("Entering Convo-Lang REPL");
while (!this._isDisposed && !cancel?.isCanceled) {
console.log('> user');
const line = await (0, node_common_1.readStdInLineAsync)();
if (this._isDisposed || cancel?.isCanceled) {
return;
}
if (!line.trim()) {
continue;
}
await this.convo.completeAsync('> user\n' + (0, convo_lang_1.escapeConvo)(line));
if (this._isDisposed || cancel?.isCanceled) {
return;
}
const c = this.convo.convo;
const append = c.substring(len);
len = c.length;
console.log(append);
}
}
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(cancel) {
const config = await (0, exports.initConvoCliAsync)(this.options);
if (!this.allowExec) {
this.allowExec = config.allowExec ?? 'ask';
}
if (this.options.inline) {
if (this.options.convert) {
this.convertCodeAsync(this.options.inline);
}
else 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.convert) {
this.convertCodeAsync(source);
}
else if (this.options.parse) {
await this.parseCodeAsync(source);
}
else {
await this.executeSourceCode(source);
}
this.writeOutputAsync();
}
if (this.options.repl) {
await this.runReplAsync(cancel);
}
}
registerExec(fullEnv) {
this.convo.defineFunction({
name: 'exec',
registerOnly: !fullEnv,
description: 'Executes a shell command on the users computer',
paramsType: zod_1.z.object({ cmd: zod_1.z.string().describe('The shell command to execute') }),
scopeCallback: (0, convo_exec_1.createConvoExec)(typeof this.allowExec === 'function' ?
this.allowExec : this.execConfirmAsync)
});
if (fullEnv) {
this.convo.defineFunction({
name: 'changeDirectory',
description: 'Changes the current working directory',
paramsType: zod_1.z.object({ dir: zod_1.z.string().describe('The directory to change to') }),
callback: async ({ dir }) => {
try {
globalThis.process?.chdir(dir);
return `Working directory change to ${globalThis.process.cwd()}`;
}
catch (ex) {
return `Unable to move to specified directory: ${(0, common_1.getErrorMessage)(ex)}`;
}
}
});
this.convo.defineFunction({
name: '_getCwd',
local: true,
callback: async (dir) => {
return globalThis.process.cwd();
}
});
}
}
async appendCodeAsync(code, options) {
let filePath = options?.filePath ?? this.options.sourcePath ?? this.options.source;
if (filePath) {
filePath = await (0, promises_1.realpath)(filePath);
}
this.convo.append(code, { ...options, filePath });
}
async executeSourceCode(code) {
this.registerExec(false);
await this.appendCodeAsync(code);
const r = await this.convo.completeAsync();
if (r.error) {
throw r.error;
}
await this.outAsync(this.convo.convo);
}
async convertCodeAsync(code) {
await this.appendCodeAsync(code);
const r = await this.convo.toModelInputStringAsync();
await this.outAsync(r);
}
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