@convo-lang/convo-lang
Version:
The language of AI
162 lines • 4.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convoGraphToConvo = void 0;
const common_1 = require("@iyio/common");
const convo_lib_1 = require("./convo-lib");
const convoGraphToConvo = (db) => {
const out = [];
const tab = '';
if (db.metadata) {
out.push('> graph\n``` json\n');
out.push(JSON.stringify(db.metadata, (0, common_1.createJsonRefReplacer)(), 4));
out.push('\n```\n');
}
if (db.nodes) {
for (const node of db.nodes) {
writeNode(node, out, tab);
}
}
if (db.edges) {
for (const edge of db.edges) {
writeEdge(edge, out, tab);
}
}
if (db.inputs) {
for (const input of db.inputs) {
writeInput(input, out, tab);
}
}
return out.join('');
};
exports.convoGraphToConvo = convoGraphToConvo;
const writeNode = (node, out, tab) => {
const isName = nameReg.test(node.key ?? '');
if (out.length) {
out.push('\n\n');
}
out.push(`${tab}> node ${isName ? node.key : ''}(`);
for (const e in node) {
const value = node[e];
if (value === undefined) {
continue;
}
switch (e) {
case 'key':
if (!isName) {
out.push(` ${e}: ${JSON.stringify(value)}`);
}
break;
case 'sharedConvo':
case 'steps':
break;
default:
out.push(` ${e}: ${JSON.stringify(value)}`);
break;
}
}
out.push(' )\n\n');
tab += ' ';
if (node.sharedConvo) {
out.push((0, common_1.setIndentation)(tab.length, node.sharedConvo));
out.push('\n\n');
}
for (const step of node.steps) {
writeStep(step, out, tab);
}
};
const writeStep = (step, out, tab) => {
out.push(`${tab}> step (`);
for (const e in step) {
const value = step[e];
if (value === undefined) {
continue;
}
switch (e) {
case 'convo':
break;
default:
out.push(` ${e}: ${JSON.stringify(value)}`);
break;
}
}
out.push(' )\n');
tab += ' ';
if (step.convo) {
out.push('\n');
out.push((0, common_1.setIndentation)(tab.length, step.convo));
out.push('\n\n');
}
};
const writeEdge = (edge, out, tab) => {
const isFromName = nameReg.test(edge.from ?? '');
const isToName = nameReg.test(edge.to ?? '');
if (out.length) {
out.push('\n\n');
}
out.push(`${tab}> edge ${isFromName ? edge.from : ''}(`);
for (const e in edge) {
const value = edge[e];
if (value === undefined) {
continue;
}
switch (e) {
case 'from':
if (!isFromName) {
out.push(` ${tab}${e}: ${JSON.stringify(value)}`);
}
break;
case 'to':
if (!isToName) {
out.push(` ${tab}${e}: ${JSON.stringify(value)}`);
}
break;
case 'conditionConvo':
break;
default:
out.push(` ${tab}${e}: ${JSON.stringify(value)}`);
break;
}
}
out.push(` ) -> ${isToName ? ` ${edge.to}` : ''}`);
if (edge.conditionConvo) {
out.push(' (\n');
out.push((0, common_1.setIndentation)(tab.length + 4, edge.conditionConvo));
out.push(`\n${tab})\n`);
}
else {
out.push('()\n');
}
};
const writeInput = (input, out, tab) => {
if (out.length) {
out.push('\n\n');
}
for (const e in input) {
const value = input[e];
if (value === undefined) {
continue;
}
switch (e) {
case 'value':
case 'isJson':
break;
default:
out.push(`${tab}@${e} ${(0, convo_lib_1.escapeConvoTagValue)(value?.toString() ?? '')}\n`);
break;
}
}
out.push(`${tab}> input\n`);
if (input.value) {
if (input.isJson) {
out.push(`${tab}\`\`\` json\n`);
out.push((0, convo_lib_1.escapeConvoMessageContent)(input.value));
out.push(`\n${tab}\`\`\``);
}
else {
out.push((0, convo_lib_1.escapeConvoMessageContent)(input.value));
out.push('\n');
}
}
};
const nameReg = /^\w+$/;
//# sourceMappingURL=convo-graph-converter.js.map