@iyio/convo-lang
Version:
A conversational language.
870 lines • 32.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConvoMarkdownVar = exports.defaultConvoVars = void 0;
const common_1 = require("@iyio/common");
const date_fns_1 = require("date-fns");
const zod_1 = require("zod");
const ConvoError_1 = require("./ConvoError");
const convo_lib_1 = require("./convo-lib");
const convo_pipe_1 = require("./convo-pipe");
const convo_types_1 = require("./convo-types");
const convo_zod_1 = require("./convo-zod");
const ifFalse = Symbol();
const ifTrue = Symbol();
const breakIteration = Symbol();
const mapFn = (0, convo_lib_1.makeAnyConvoType)('map', (0, convo_lib_1.createConvoScopeFunction)({
usesLabels: true,
}, convo_lib_1.convoLabeledScopeParamsToObj));
const arrayFn = (0, convo_lib_1.makeAnyConvoType)('array', (scope) => {
return scope.paramValues ?? [];
});
const and = (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
nextParam(scope) {
const value = scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
if (value) {
return scope.i + 1;
}
else {
return false;
}
}
}, scope => {
const value = scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
return value ? true : false;
});
const or = (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
nextParam(scope) {
const value = scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
if (value) {
return false;
}
else {
return scope.i + 1;
}
}
}, scope => {
return scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
});
const describeStruct = (0, convo_lib_1.createConvoScopeFunction)(scope => {
const type = (0, convo_zod_1.convoValueToZodType)(scope.paramValues?.[0]);
if (!(type instanceof zod_1.ZodObject)) {
throw new ConvoError_1.ConvoError('invalid-args', { statement: scope.s }, 'The first arg of new should be a type variable');
}
return (0, convo_zod_1.describeConvoScheme)(type, scope.paramValues?.[1]);
});
exports.defaultConvoVars = {
[convo_lib_1.convoBodyFnName]: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
catchReturn: true,
}),
string: (0, convo_lib_1.createConvoBaseTypeDef)('string'),
number: (0, convo_lib_1.createConvoBaseTypeDef)('number'),
int: (0, convo_lib_1.createConvoBaseTypeDef)('int'),
time: (0, convo_lib_1.createConvoBaseTypeDef)('time'),
void: (0, convo_lib_1.createConvoBaseTypeDef)('void'),
boolean: (0, convo_lib_1.createConvoBaseTypeDef)('boolean'),
any: (0, convo_lib_1.createConvoBaseTypeDef)('any'),
['true']: true,
['false']: false,
['null']: null,
['undefined']: undefined,
[convo_lib_1.convoGlobalRef]: undefined,
[convo_lib_1.convoPipeFnName]: convo_pipe_1.convoPipeScopeFunction,
[convo_lib_1.convoStructFnName]: (0, convo_lib_1.makeAnyConvoType)('map', (0, convo_lib_1.createConvoScopeFunction)({
usesLabels: true,
}, (scope) => {
scope.cm = true;
return (0, convo_lib_1.convoLabeledScopeParamsToObj)(scope);
})),
[convo_lib_1.convoMapFnName]: mapFn,
[convo_lib_1.convoArrayFnName]: arrayFn,
[convo_lib_1.convoJsonMapFnName]: mapFn,
[convo_lib_1.convoJsonArrayFnName]: arrayFn,
[convo_lib_1.convoArgsName]: undefined,
[convo_lib_1.convoEnumFnName]: (0, convo_lib_1.createConvoScopeFunction)(scope => {
const type = (0, convo_lib_1.createConvoType)({
type: 'enum',
enumValues: scope.paramValues ?? [],
});
const metadata = (0, convo_lib_1.createConvoMetadataForStatement)(scope.s);
type[convo_lib_1.convoMetadataKey] = metadata;
return type;
}),
is: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues || scope.paramValues.length < 2) {
return false;
}
const type = scope.paramValues[scope.paramValues.length - 1];
if (!type || (typeof type !== 'object')) {
return false;
}
const scheme = (0, convo_zod_1.convoValueToZodType)(type);
for (let i = 0; i < scope.paramValues.length - 1; i++) {
const p = scheme.safeParse(scope.paramValues[i]);
if (!p.success) {
return false;
}
}
return true;
}),
and,
or,
not: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues?.length) {
return true;
}
for (let i = 0; i < scope.paramValues.length; i++) {
if (scope.paramValues[i]) {
return false;
}
}
return true;
}),
if: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
nextParam(scope, parentScope) {
const value = scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
if (value) {
return scope.i + 1;
}
else {
if (parentScope) {
parentScope.i++;
}
return false;
}
}
}, (scope) => {
const value = scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
return value ? ifTrue : ifFalse;
}),
elif: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
shouldExecute(scope, parentScope) {
const prev = (parentScope?.paramValues && parentScope.paramValues[parentScope.paramValues.length - 1]);
return prev === ifFalse;
},
nextParam(scope) {
const value = scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
if (value) {
return scope.i + 1;
}
else {
return false;
}
}
}, scope => {
const value = scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
return value ? ifTrue : ifFalse;
}),
else: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
shouldExecute(scope, parentScope) {
const prev = (parentScope?.paramValues && parentScope.paramValues[parentScope.paramValues.length - 1]);
return prev === ifFalse;
},
}, () => {
return ifTrue;
}),
then: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
shouldExecute(scope, parentScope) {
const prev = (parentScope?.paramValues && parentScope.paramValues[parentScope.paramValues.length - 1]);
return prev === ifTrue;
},
}, () => {
return ifTrue;
}),
while: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
nextParam(scope, parentScope) {
const value = scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
if (scope.i === 0 && parentScope) {
delete parentScope.fromIndex;
}
if (scope.s.params && scope.i === scope.s.params.length - 1 && parentScope) {
if (value) {
parentScope.fromIndex = parentScope.i + 1;
parentScope.gotoIndex = parentScope.i;
parentScope.li = parentScope.i + 1;
}
}
if (value) {
return scope.i + 1;
}
else {
if (parentScope) {
parentScope.i++;
}
return false;
}
}
}, (scope) => {
const value = scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
if (value) {
scope.ctrlData = ifTrue;
}
return scope.ctrlData ?? ifFalse;
}),
foreach: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
keepData: true,
startParam(scope, parentScope) {
if (!scope.s.params?.length) {
if (parentScope) {
parentScope.i++;
}
return false;
}
return 0;
},
nextParam(scope, parentScope) {
if (scope.paramValues?.[0] === breakIteration) {
if (parentScope) {
parentScope.i++;
}
return false;
}
if (parentScope && scope.i + 1 === scope.s.params?.length) {
parentScope.fromIndex = parentScope.i + 1;
parentScope.gotoIndex = parentScope.i;
parentScope.li = parentScope.i + 1;
}
return scope.i + 1;
}
}, (scope) => {
const value = scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
if (value) {
scope.ctrlData = ifTrue;
}
return scope.ctrlData ?? ifFalse;
}),
in: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
keepData: true,
nextParam(scope) {
const value = scope.paramValues?.[0];
if (!value || (typeof value !== 'object')) {
return false;
}
let it = scope.ctrlData;
if (!it) {
it = { i: 0 };
if (!Array.isArray(value)) {
it.keys = Object.keys(value);
}
scope.ctrlData = it;
}
return false;
}
}, (scope) => {
const it = scope.ctrlData;
if (!it) {
return breakIteration;
}
const value = scope.paramValues?.[0];
const isArray = Array.isArray(value);
const ary = it.keys ?? value;
if (it.i >= ary.length) {
return breakIteration;
}
const r = isArray ? value[it.i] : { key: ary[it.i], value: value[ary[it.i]] };
it.i++;
return r;
}),
break: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
nextParam(scope, parentScope) {
if (parentScope?.paramValues) {
scope.ctrlData = parentScope.paramValues[parentScope.paramValues.length - 1];
}
return scope.i + 1;
}
}, (scope) => {
if (!scope.paramValues?.length) {
scope.bl = true;
return scope.ctrlData;
}
for (let i = 0; i < scope.paramValues.length; i++) {
if (scope.paramValues[i]) {
scope.bl = true;
return scope.ctrlData;
}
}
return scope.ctrlData;
}),
do: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
}, scope => {
return scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
}),
fn: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
catchReturn: true,
}, () => {
return undefined;
}),
return: (0, convo_lib_1.createConvoScopeFunction)(scope => {
const value = scope.paramValues ? scope.paramValues[scope.paramValues.length - 1] : undefined;
scope.r = true;
return value;
}),
eq: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues || scope.paramValues.length < 2) {
return false;
}
for (let i = 1; i < scope.paramValues.length; i++) {
if (scope.paramValues[i - 1] !== scope.paramValues[i]) {
return false;
}
}
return true;
}),
gt: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues || scope.paramValues.length < 2) {
return false;
}
for (let i = 1; i < scope.paramValues.length; i++) {
if (!(scope.paramValues[i - 1] > scope.paramValues[i])) {
return false;
}
}
return true;
}),
gte: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues || scope.paramValues.length < 2) {
return false;
}
for (let i = 1; i < scope.paramValues.length; i++) {
if (!(scope.paramValues[i - 1] >= scope.paramValues[i])) {
return false;
}
}
return true;
}),
lt: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues || scope.paramValues.length < 2) {
return false;
}
for (let i = 1; i < scope.paramValues.length; i++) {
if (!(scope.paramValues[i - 1] < scope.paramValues[i])) {
return false;
}
}
return true;
}),
lte: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues || scope.paramValues.length < 2) {
return false;
}
for (let i = 1; i < scope.paramValues.length; i++) {
if (!(scope.paramValues[i - 1] <= scope.paramValues[i])) {
return false;
}
}
return true;
}),
add: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues?.length) {
return undefined;
}
let value = scope.paramValues[0];
for (let i = 1; i < scope.paramValues.length; i++) {
const v = scope.paramValues[i];
if (v !== undefined) {
if (value === undefined) {
value = v;
}
else {
value += v;
}
}
}
return value;
}),
sub: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues?.length) {
return undefined;
}
let value = scope.paramValues[0];
for (let i = 1; i < scope.paramValues.length; i++) {
const v = scope.paramValues[i];
if (v !== undefined) {
if (value === undefined) {
value = v;
}
else {
value -= v;
}
}
}
return value;
}),
mul: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues?.length) {
return undefined;
}
let value = scope.paramValues[0];
for (let i = 1; i < scope.paramValues.length; i++) {
const v = scope.paramValues[i];
if (v !== undefined) {
if (value === undefined) {
value = v;
}
else {
value *= v;
}
}
}
return value;
}),
div: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues?.length) {
return undefined;
}
let value = scope.paramValues[0];
for (let i = 1; i < scope.paramValues.length; i++) {
const v = scope.paramValues[i];
if (v !== undefined) {
if (value === undefined) {
value = v;
}
else {
value /= v;
}
}
}
return value;
}),
mod: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues?.length) {
return undefined;
}
let value = scope.paramValues[0];
for (let i = 1; i < scope.paramValues.length; i++) {
const v = scope.paramValues[i];
if (v !== undefined) {
if (value === undefined) {
value = v;
}
else {
value %= v;
}
}
}
return value;
}),
pow: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues?.length) {
return undefined;
}
let value = scope.paramValues[0];
for (let i = 1; i < scope.paramValues.length; i++) {
const v = scope.paramValues[i];
if (v !== undefined) {
if (value === undefined) {
value = v;
}
else {
value = Math.pow(value, v);
}
}
}
return value;
}),
print: (0, convo_lib_1.createConvoScopeFunction)((scope, ctx) => {
if (scope.paramValues) {
ctx.print(...scope.paramValues);
}
return scope.paramValues?.[scope.paramValues?.length ?? 0];
}),
inc: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
startParam() {
return 1;
}
}, (scope, ctx) => {
if (!scope.s.params) {
return undefined;
}
const value = scope.paramValues?.[0] ?? 1;
let lastValue = value;
const s = scope.s.params[0];
const sv = ctx.getRefValue(s, scope, false);
lastValue = sv === undefined ? value : sv + value;
ctx.setRefValue(s, lastValue, scope);
return lastValue;
}),
dec: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
startParam() {
return 1;
}
}, (scope, ctx) => {
if (!scope.s.params) {
return undefined;
}
const value = scope.paramValues?.[0] ?? 1;
let lastValue = value;
const s = scope.s.params[0];
const sv = ctx.getRefValue(s, scope, false);
lastValue = sv === undefined ? -value : sv - value;
ctx.setRefValue(s, lastValue, scope);
return lastValue;
}),
[convo_lib_1.convoSwitchFnName]: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
nextParam(scope) {
if (scope.i === 0) {
if (scope.s.hmc) {
scope.sv = scope.paramValues?.[0];
return 1;
}
else {
return scope.paramValues?.[0] ? 1 : 2;
}
}
if (scope.s.hmc) {
if (scope.s.params && !scope.s.params[scope.i]?.mc) {
scope.sv = scope.paramValues?.[0];
}
const nextIndex = scope.ctrlData;
if (nextIndex === undefined) {
return scope.i + 1;
}
else {
delete scope.ctrlData;
return nextIndex;
}
}
else {
return false;
}
}
}, scope => {
return scope.paramValues?.[0];
}),
[convo_lib_1.convoCaseFnName]: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
nextParam(scope, parentScope) {
if (parentScope?.s.fn !== convo_lib_1.convoSwitchFnName || !scope.s.params?.length) {
return false;
}
const isMatch = parentScope.sv === scope.paramValues?.[0];
if (isMatch) { // let control flow move to next statement and do not check anymore statements
parentScope.bi = parentScope.i + 2;
return false;
}
if (scope.i === scope.s.params.length - 1) { // no matches found, skip next statement
parentScope.ctrlData = parentScope.i + 2;
return false;
}
return scope.i + 1;
}
}, () => {
return undefined;
}),
[convo_lib_1.convoDefaultFnName]: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
startParam(scope, parentScope) {
if (parentScope?.s.fn !== convo_lib_1.convoSwitchFnName) {
return false;
}
parentScope.bi = parentScope.i + 2;
return false;
}
}, () => {
return undefined;
}),
[convo_lib_1.convoTestFnName]: (0, convo_lib_1.createConvoScopeFunction)({
discardParams: true,
nextParam(scope, parentScope) {
if (parentScope?.s.fn !== convo_lib_1.convoSwitchFnName || !scope.s.params?.length) {
return false;
}
const isMatch = scope.paramValues?.[0] ? true : false;
if (isMatch) { // let control flow move to next statement and do not check anymore statements
parentScope.bi = parentScope.i + 2;
return false;
}
if (scope.i === scope.s.params.length - 1) { // no matches found, skip next statement
parentScope.ctrlData = parentScope.i + 2;
return false;
}
return scope.i + 1;
}
}, () => {
return undefined;
}),
sleep: (0, convo_lib_1.createConvoScopeFunction)(scope => {
const start = Date.now();
const delay = scope.paramValues?.[0];
return new Promise(r => {
setTimeout(() => {
r(Date.now() - start);
}, typeof delay === 'number' ? delay : 0);
});
}),
rand: (0, convo_lib_1.createConvoScopeFunction)(scope => {
const range = scope.paramValues?.[0];
if (typeof range === 'number') {
return Math.round(Math.random() * range);
}
else {
return Math.random();
}
}),
httpGet: (0, convo_lib_1.createConvoScopeFunction)(async (scope) => {
let url = scope.paramValues?.[0];
let options = scope.paramValues?.[1];
if (typeof url !== 'string') {
throw new ConvoError_1.ConvoError('invalid-args', { statement: scope.s }, "First arg must be a string URL");
}
if (options && (typeof options !== 'object')) {
url += `${url.includes('?') ? '&' : '?'}__input=${encodeURIComponent(options.toString())}`;
options = undefined;
}
return await (0, common_1.httpClient)().getAsync(url, options);
}),
httpGetString: (0, convo_lib_1.createConvoScopeFunction)(async (scope) => {
const url = scope.paramValues?.[0];
const options = scope.paramValues?.[1];
if (typeof url !== 'string') {
throw new ConvoError_1.ConvoError('invalid-args', { statement: scope.s }, "First arg must be a string URL");
}
return await (0, common_1.httpClient)().getStringAsync(url, options);
}),
httpPost: (0, convo_lib_1.createConvoScopeFunction)(async (scope) => {
const url = scope.paramValues?.[0];
const body = scope.paramValues?.[1];
const options = scope.paramValues?.[2];
if (typeof url !== 'string') {
throw new ConvoError_1.ConvoError('invalid-args', { statement: scope.s }, "First arg must be a string URL");
}
return await (0, common_1.httpClient)().postAsync(url, body, options);
}),
httpPut: (0, convo_lib_1.createConvoScopeFunction)(async (scope) => {
const url = scope.paramValues?.[0];
const body = scope.paramValues?.[1];
const options = scope.paramValues?.[2];
if (typeof url !== 'string') {
throw new ConvoError_1.ConvoError('invalid-args', { statement: scope.s }, "First arg must be a string URL");
}
return await (0, common_1.httpClient)().putAsync(url, body, options);
}),
httpPatch: (0, convo_lib_1.createConvoScopeFunction)(async (scope) => {
const url = scope.paramValues?.[0];
const body = scope.paramValues?.[1];
const options = scope.paramValues?.[2];
if (typeof url !== 'string') {
throw new ConvoError_1.ConvoError('invalid-args', { statement: scope.s }, "First arg must be a string URL");
}
return await (0, common_1.httpClient)().patchAsync(url, body, options);
}),
httpDelete: (0, convo_lib_1.createConvoScopeFunction)(async (scope) => {
const url = scope.paramValues?.[0];
const options = scope.paramValues?.[1];
if (typeof url !== 'string') {
throw new ConvoError_1.ConvoError('invalid-args', { statement: scope.s }, "First arg must be a string URL");
}
return await (0, common_1.httpClient)().deleteAsync(url, options);
}),
encodeURI: (0, convo_lib_1.createConvoScopeFunction)(scope => {
return encodeURI(scope.paramValues?.[0]?.toString() ?? '');
}),
encodeURIComponent: (0, convo_lib_1.createConvoScopeFunction)(scope => {
return encodeURIComponent(scope.paramValues?.[0]?.toString() ?? '');
}),
now: (0, convo_lib_1.createConvoScopeFunction)(() => {
return Date.now();
}),
dateTime: (0, convo_lib_1.createConvoScopeFunction)(scope => {
const f = scope.paramValues?.[0] ?? convo_lib_1.convoDateFormat;
let time = scope.paramValues?.[1] ?? new Date();
switch (typeof time) {
case 'string':
time = new Date(time);
break;
case 'number':
break;
default:
if (!(time instanceof Date)) {
throw new ConvoError_1.ConvoError('invalid-args', { statement: scope.s }, 'Second arg of timeTime must be a string, number or Date object');
}
break;
}
try {
return (0, date_fns_1.format)(time, f);
}
catch {
return (0, date_fns_1.format)(time, convo_lib_1.convoDateFormat);
}
}),
md: (0, convo_lib_1.createConvoScopeFunction)((scope) => {
if (!scope.paramValues?.length) {
return '';
}
const out = [];
for (let i = 0; i < scope.paramValues.length; i++) {
const value = scope.paramValues[i];
(0, common_1.objectToMarkdownBuffer)(value, out, '', 5);
}
return out.join('');
}),
toMarkdown: (0, convo_lib_1.createConvoScopeFunction)(scope => {
if (!scope.paramValues?.length) {
return '';
}
const maxDepth = scope.paramValues[0];
if (typeof maxDepth !== 'number') {
throw new ConvoError_1.ConvoError('invalid-args', { statement: scope.s }, 'The first arg of toMarkdown must be a number indicating the max depth');
}
const out = [];
for (let i = 1; i < scope.paramValues.length; i++) {
(0, common_1.objectToMarkdownBuffer)(scope.paramValues[i], out, '', maxDepth);
}
return out.join('');
}),
toJson: (0, convo_lib_1.createConvoScopeFunction)(scope => {
const value = scope.paramValues?.[0];
if (value === undefined) {
return 'undefined';
}
return JSON.stringify(value, (0, common_1.createJsonRefReplacer)(), 4);
}),
toJsonMdBlock: (0, convo_lib_1.createConvoScopeFunction)(scope => {
const value = scope.paramValues?.[0];
return ('``` json\n' +
value === undefined ? 'undefined' : JSON.stringify(value, (0, common_1.createJsonRefReplacer)(), 4) +
'\n```');
}),
toJsonScheme: (0, convo_lib_1.createConvoScopeFunction)(scope => {
const value = scope.paramValues?.[0];
return JSON.stringify((0, convo_zod_1.convoTypeToJsonScheme)(value));
}),
toCsv: (0, convo_lib_1.createConvoScopeFunction)(scope => {
const value = scope.paramValues?.[0];
if (!Array.isArray(value)) {
return '"!Invalid CSV Data"';
}
return (0, common_1.toCsvLines)(value);
}),
toCsvMdBlock: (0, convo_lib_1.createConvoScopeFunction)(scope => {
const value = scope.paramValues?.[0];
if (!Array.isArray(value)) {
return '"!Invalid CSV Data"';
}
return '``` csv\n' + (0, common_1.toCsvLines)(value) + '\n```';
}),
merge: (0, convo_lib_1.createConvoScopeFunction)(scope => {
const value = {};
if (scope.paramValues) {
for (let i = 0; i < scope.paramValues.length; i++) {
const item = scope.paramValues[i];
if (item && (typeof item === 'object')) {
for (const e in item) {
const v = item[e];
if (v !== undefined) {
value[e] = v;
}
}
}
}
}
return value;
}),
['new']: (0, convo_lib_1.createConvoScopeFunction)(scope => {
const type = (0, convo_zod_1.convoValueToZodType)(scope.paramValues?.[0]);
if (!(type instanceof zod_1.ZodObject)) {
throw new ConvoError_1.ConvoError('invalid-args', { statement: scope.s }, 'The first arg of new should be a type variable');
}
const r = type.safeParse(scope.paramValues?.[1] ?? {});
if (r.success) {
return r.data;
}
else {
throw new ConvoError_1.ConvoError('missing-defaults', { statement: scope.s }, 'The type has missing property defaults and can not be used with the new function - ' + r.error.message);
}
}),
tmpl: (0, convo_lib_1.createConvoScopeFunction)((scope, ctx) => {
const name = scope.paramValues?.[0];
const format = scope.paramValues?.[1] ?? 'plain';
const md = ctx.getVar(convo_lib_1.convoVars.__md, null, null);
if (!md) {
return '';
}
return (0, exports.getConvoMarkdownVar)(name, format, ctx);
}),
html: (0, convo_lib_1.createConvoScopeFunction)((scope) => {
const params = scope.paramValues;
if (!params?.length) {
return '';
}
if (params.length === 1) {
return (0, common_1.escapeHtml)(params[0]?.toString?.() ?? '');
}
const out = [];
for (let i = 0; i < params.length; i++) {
const p = params[i];
if (!p) {
out.push((0, common_1.escapeHtml)(p.toString?.() ?? ''));
}
}
return out.join('\n');
}),
describeStruct,
xAtt: (0, convo_lib_1.createConvoScopeFunction)((scope) => {
const value = scope.paramValues?.[0];
switch (typeof value) {
case 'string':
if (value.startsWith('{') && value.endsWith('}')) {
return `"\\${(0, common_1.escapeHtml)(value)}"`;
}
else {
return `"${(0, common_1.escapeHtml)(value)}"`;
}
case 'number':
case 'boolean':
case 'bigint':
return `"{${value}}"`;
case 'undefined':
return '"{undefined}"';
default:
try {
return `'{${(0, common_1.escapeHtmlKeepDoubleQuote)(JSON.stringify(value))}}'`;
}
catch (ex) {
return `'{${(0, common_1.escapeHtmlKeepDoubleQuote)(JSON.stringify({
__error: (0, common_1.getErrorMessage)(ex)
}))}}'`;
}
}
}),
openBrowserWindow: (0, convo_lib_1.createConvoScopeFunction)((scope) => {
const url = scope.paramValues?.[0];
const target = scope.paramValues?.[1] ?? '_blank';
if ((typeof url !== 'string') || (typeof target !== 'string')) {
return false;
}
globalThis.window?.open(url, target);
return true;
}),
};
Object.freeze(exports.defaultConvoVars);
const getConvoMarkdownVar = (name, format, ctx) => {
const md = ctx.getVar(convo_lib_1.convoVars.__md, null, null);
if (!md) {
return '';
}
const v = md[name];
if ((0, convo_types_1.isConvoMarkdownLine)(v)) {
return (0, common_1.markdownLineToString)(format, v.line);
}
else {
return v?.toString?.() ?? '';
}
};
exports.getConvoMarkdownVar = getConvoMarkdownVar;
//# sourceMappingURL=convo-default-vars.js.map