agentlang 0.7.9 → 0.7.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/out/api/http.d.ts.map +1 -1
  2. package/out/api/http.js +8 -1
  3. package/out/api/http.js.map +1 -1
  4. package/out/cli/main.d.ts.map +1 -1
  5. package/out/cli/main.js +33 -2
  6. package/out/cli/main.js.map +1 -1
  7. package/out/extension/main.cjs +250 -250
  8. package/out/extension/main.cjs.map +2 -2
  9. package/out/language/agentlang-validator.d.ts +1 -2
  10. package/out/language/agentlang-validator.d.ts.map +1 -1
  11. package/out/language/agentlang-validator.js +0 -39
  12. package/out/language/agentlang-validator.js.map +1 -1
  13. package/out/language/generated/ast.d.ts +62 -11
  14. package/out/language/generated/ast.d.ts.map +1 -1
  15. package/out/language/generated/ast.js +75 -3
  16. package/out/language/generated/ast.js.map +1 -1
  17. package/out/language/generated/grammar.d.ts.map +1 -1
  18. package/out/language/generated/grammar.js +758 -239
  19. package/out/language/generated/grammar.js.map +1 -1
  20. package/out/language/main.cjs +1370 -824
  21. package/out/language/main.cjs.map +4 -4
  22. package/out/language/parser.d.ts +1 -0
  23. package/out/language/parser.d.ts.map +1 -1
  24. package/out/language/parser.js +44 -7
  25. package/out/language/parser.js.map +1 -1
  26. package/out/language/syntax.d.ts +1 -1
  27. package/out/language/syntax.d.ts.map +1 -1
  28. package/out/language/syntax.js +22 -13
  29. package/out/language/syntax.js.map +1 -1
  30. package/out/runtime/api.d.ts +2 -0
  31. package/out/runtime/api.d.ts.map +1 -1
  32. package/out/runtime/api.js +5 -0
  33. package/out/runtime/api.js.map +1 -1
  34. package/out/runtime/auth/cognito.d.ts.map +1 -1
  35. package/out/runtime/auth/cognito.js +4 -4
  36. package/out/runtime/auth/cognito.js.map +1 -1
  37. package/out/runtime/defs.d.ts +5 -0
  38. package/out/runtime/defs.d.ts.map +1 -1
  39. package/out/runtime/defs.js +16 -0
  40. package/out/runtime/defs.js.map +1 -1
  41. package/out/runtime/exec-graph.js +1 -1
  42. package/out/runtime/exec-graph.js.map +1 -1
  43. package/out/runtime/interpreter.d.ts +6 -1
  44. package/out/runtime/interpreter.d.ts.map +1 -1
  45. package/out/runtime/interpreter.js +144 -112
  46. package/out/runtime/interpreter.js.map +1 -1
  47. package/out/runtime/loader.d.ts +1 -1
  48. package/out/runtime/loader.d.ts.map +1 -1
  49. package/out/runtime/loader.js +74 -27
  50. package/out/runtime/loader.js.map +1 -1
  51. package/out/runtime/module.d.ts +41 -11
  52. package/out/runtime/module.d.ts.map +1 -1
  53. package/out/runtime/module.js +238 -50
  54. package/out/runtime/module.js.map +1 -1
  55. package/out/runtime/modules/ai.d.ts.map +1 -1
  56. package/out/runtime/modules/ai.js +13 -6
  57. package/out/runtime/modules/ai.js.map +1 -1
  58. package/out/runtime/modules/auth.d.ts +2 -1
  59. package/out/runtime/modules/auth.d.ts.map +1 -1
  60. package/out/runtime/modules/auth.js +93 -3
  61. package/out/runtime/modules/auth.js.map +1 -1
  62. package/out/runtime/modules/core.d.ts +7 -5
  63. package/out/runtime/modules/core.d.ts.map +1 -1
  64. package/out/runtime/modules/core.js +93 -16
  65. package/out/runtime/modules/core.js.map +1 -1
  66. package/out/runtime/monitor.d.ts.map +1 -1
  67. package/out/runtime/monitor.js +1 -0
  68. package/out/runtime/monitor.js.map +1 -1
  69. package/out/runtime/resolvers/interface.d.ts +6 -1
  70. package/out/runtime/resolvers/interface.d.ts.map +1 -1
  71. package/out/runtime/resolvers/interface.js +2 -2
  72. package/out/runtime/resolvers/interface.js.map +1 -1
  73. package/out/runtime/resolvers/sqldb/database.d.ts +19 -2
  74. package/out/runtime/resolvers/sqldb/database.d.ts.map +1 -1
  75. package/out/runtime/resolvers/sqldb/database.js +107 -21
  76. package/out/runtime/resolvers/sqldb/database.js.map +1 -1
  77. package/out/runtime/resolvers/sqldb/dbutil.d.ts +1 -0
  78. package/out/runtime/resolvers/sqldb/dbutil.d.ts.map +1 -1
  79. package/out/runtime/resolvers/sqldb/dbutil.js +25 -3
  80. package/out/runtime/resolvers/sqldb/dbutil.js.map +1 -1
  81. package/out/runtime/resolvers/sqldb/impl.d.ts +3 -2
  82. package/out/runtime/resolvers/sqldb/impl.d.ts.map +1 -1
  83. package/out/runtime/resolvers/sqldb/impl.js +80 -6
  84. package/out/runtime/resolvers/sqldb/impl.js.map +1 -1
  85. package/out/runtime/state.d.ts +58 -0
  86. package/out/runtime/state.d.ts.map +1 -1
  87. package/out/runtime/state.js +12 -0
  88. package/out/runtime/state.js.map +1 -1
  89. package/out/runtime/util.d.ts +1 -1
  90. package/out/runtime/util.d.ts.map +1 -1
  91. package/out/runtime/util.js +27 -13
  92. package/out/runtime/util.js.map +1 -1
  93. package/out/setupClassic.d.ts +98 -0
  94. package/out/setupClassic.d.ts.map +1 -0
  95. package/out/setupClassic.js +38 -0
  96. package/out/setupClassic.js.map +1 -0
  97. package/out/setupCommon.d.ts +2 -0
  98. package/out/setupCommon.d.ts.map +1 -0
  99. package/out/setupCommon.js +33 -0
  100. package/out/setupCommon.js.map +1 -0
  101. package/out/setupExtended.d.ts +40 -0
  102. package/out/setupExtended.d.ts.map +1 -0
  103. package/out/setupExtended.js +67 -0
  104. package/out/setupExtended.js.map +1 -0
  105. package/out/syntaxes/agentlang.monarch.js +1 -1
  106. package/out/syntaxes/agentlang.monarch.js.map +1 -1
  107. package/package.json +187 -185
  108. package/src/api/http.ts +8 -0
  109. package/src/cli/main.ts +38 -2
  110. package/src/language/agentlang-validator.ts +1 -51
  111. package/src/language/agentlang.langium +17 -4
  112. package/src/language/generated/ast.ts +147 -13
  113. package/src/language/generated/grammar.ts +758 -239
  114. package/src/language/parser.ts +43 -8
  115. package/src/language/syntax.ts +25 -12
  116. package/src/runtime/api.ts +8 -0
  117. package/src/runtime/defs.ts +8 -0
  118. package/src/runtime/interpreter.ts +104 -76
  119. package/src/runtime/loader.ts +75 -25
  120. package/src/runtime/module.ts +194 -32
  121. package/src/runtime/modules/ai.ts +10 -4
  122. package/src/runtime/modules/auth.ts +1 -0
  123. package/src/runtime/modules/core.ts +99 -23
  124. package/src/runtime/monitor.ts +1 -0
  125. package/src/runtime/resolvers/interface.ts +9 -2
  126. package/src/runtime/resolvers/sqldb/database.ts +68 -17
  127. package/src/runtime/resolvers/sqldb/dbutil.ts +28 -6
  128. package/src/runtime/resolvers/sqldb/impl.ts +86 -14
  129. package/src/runtime/state.ts +14 -0
  130. package/src/runtime/util.ts +25 -12
  131. package/src/syntaxes/agentlang.monarch.ts +1 -1
@@ -10,13 +10,14 @@ import { AdminSession, AdminUserId } from './auth/defs.js';
10
10
  import { AgentInstance, AgentEntityName, AgentFqName, findAgentByName, trimGeneratedCode, } from './modules/ai.js';
11
11
  import { logger } from './logger.js';
12
12
  import { FlowSuspensionTag, ParentAttributeName, PathAttributeName, PathAttributeNameQuery, } from './defs.js';
13
- import { addCreateAudit, addDeleteAudit, addUpdateAudit, createSuspension, flushMonitoringData, maybeCancelTimer, setTimerRunning, } from './modules/core.js';
13
+ import { addCreateAudit, addDeleteAudit, addUpdateAudit, createSuspension, flushMonitoringData, triggerTimer, } from './modules/core.js';
14
14
  import { invokeModuleFn } from './jsmodules.js';
15
15
  import { invokeOpenApiEvent, isOpenApiEventInstance } from './openapi.js';
16
16
  import { fetchDoc } from './docs.js';
17
17
  import { getAgentFlow } from './agents/flows.js';
18
18
  import { isMonitoringEnabled } from './state.js';
19
19
  import { Monitor, MonitorEntry } from './monitor.js';
20
+ import { detailedDiff } from 'deep-object-diff';
20
21
  const EmptyResult = null;
21
22
  export function isEmptyResult(r) {
22
23
  return r == EmptyResult;
@@ -76,7 +77,7 @@ export class Environment extends Instance {
76
77
  }
77
78
  this.preGeneratedSuspensionId = crypto.randomUUID();
78
79
  }
79
- static from(parent, name, isAsync = false) {
80
+ static from(parent, name, isAsync = false, mergeScratchPad = false) {
80
81
  const env = new Environment(name, parent);
81
82
  if (isAsync) {
82
83
  env.activeResolvers = new Map();
@@ -84,6 +85,11 @@ export class Environment extends Instance {
84
85
  env.activeCatchHandlers = new Array();
85
86
  env.preGeneratedSuspensionId = parent.preGeneratedSuspensionId;
86
87
  }
88
+ if (mergeScratchPad && parent.scratchPad) {
89
+ Object.keys(parent.scratchPad).forEach((k) => {
90
+ env.bind(k, parent.scratchPad[k]);
91
+ });
92
+ }
87
93
  return env;
88
94
  }
89
95
  static fromInstance(inst) {
@@ -359,6 +365,10 @@ export class Environment extends Instance {
359
365
  getActiveModuleName() {
360
366
  return this.activeModule;
361
367
  }
368
+ setActiveModuleName(n) {
369
+ this.activeModule = n;
370
+ return this;
371
+ }
362
372
  switchActiveModuleName(newModuleName) {
363
373
  const oldModuleName = this.activeModule;
364
374
  this.activeModule = newModuleName;
@@ -1082,6 +1092,7 @@ async function patternToInstance(entryName, attributes, env) {
1082
1092
  const attrs = newInstanceAttributes();
1083
1093
  let qattrs;
1084
1094
  let qattrVals;
1095
+ let aggregates;
1085
1096
  const isQueryAll = entryName.endsWith(QuerySuffix);
1086
1097
  if (isQueryAll) {
1087
1098
  entryName = entryName.slice(0, entryName.length - 1);
@@ -1089,23 +1100,30 @@ async function patternToInstance(entryName, attributes, env) {
1089
1100
  if (attributes) {
1090
1101
  for (let i = 0; i < attributes.length; ++i) {
1091
1102
  const a = attributes[i];
1092
- await evaluateExpression(a.value, env);
1093
- const v = env.getLastResult();
1094
- let aname = a.name;
1095
- if (aname.endsWith(QuerySuffix)) {
1096
- if (isQueryAll) {
1097
- throw new Error(`Cannot specifiy query attribute ${aname} here`);
1103
+ if (a.value !== undefined) {
1104
+ await evaluateExpression(a.value, env);
1105
+ const v = env.getLastResult();
1106
+ let aname = a.name;
1107
+ if (aname.endsWith(QuerySuffix)) {
1108
+ if (isQueryAll) {
1109
+ throw new Error(`Cannot specifiy query attribute ${aname} here`);
1110
+ }
1111
+ if (qattrs === undefined)
1112
+ qattrs = newInstanceAttributes();
1113
+ if (qattrVals === undefined)
1114
+ qattrVals = newInstanceAttributes();
1115
+ aname = aname.slice(0, aname.length - 1);
1116
+ qattrs.set(aname, a.op === undefined ? '=' : a.op);
1117
+ qattrVals.set(aname, v);
1118
+ }
1119
+ else {
1120
+ attrs.set(aname, v);
1098
1121
  }
1099
- if (qattrs === undefined)
1100
- qattrs = newInstanceAttributes();
1101
- if (qattrVals === undefined)
1102
- qattrVals = newInstanceAttributes();
1103
- aname = aname.slice(0, aname.length - 1);
1104
- qattrs.set(aname, a.op === undefined ? '=' : a.op);
1105
- qattrVals.set(aname, v);
1106
1122
  }
1107
- else {
1108
- attrs.set(aname, v);
1123
+ else if (a.aggregate !== undefined) {
1124
+ if (aggregates === undefined)
1125
+ aggregates = new Map();
1126
+ aggregates.set(escapeQueryName(a.name), { name: a.aggregate.name, args: a.aggregate.args });
1109
1127
  }
1110
1128
  }
1111
1129
  }
@@ -1117,7 +1135,11 @@ async function patternToInstance(entryName, attributes, env) {
1117
1135
  if (p.hasEntry())
1118
1136
  entryName = p.getEntryName();
1119
1137
  }
1120
- return makeInstance(moduleName, entryName, attrs, qattrs, qattrVals, isQueryAll);
1138
+ const inst = makeInstance(moduleName, entryName, attrs, qattrs, qattrVals, isQueryAll);
1139
+ if (aggregates !== undefined) {
1140
+ return inst.setAggregates(aggregates);
1141
+ }
1142
+ return inst;
1121
1143
  }
1122
1144
  async function instanceFromSource(crud, env) {
1123
1145
  if (crud.source) {
@@ -1168,6 +1190,14 @@ async function maybeValidateOneOfRefs(inst, env) {
1168
1190
  }
1169
1191
  }
1170
1192
  }
1193
+ function maybeSetQueryClauses(inst, crud) {
1194
+ if (crud.groupByClause) {
1195
+ inst.setGroupBy(crud.groupByClause.colNames);
1196
+ }
1197
+ if (crud.orderByClause) {
1198
+ inst.setOrderBy(crud.orderByClause.colNames, crud.orderByClause.order === '@desc');
1199
+ }
1200
+ }
1171
1201
  async function evaluateCrudMap(crud, env) {
1172
1202
  var _a, _b, _c;
1173
1203
  if (!env.isInUpsertMode() && crud.upsert.length > 0) {
@@ -1180,8 +1210,10 @@ async function evaluateCrudMap(crud, env) {
1180
1210
  const moduleName = inst.moduleName;
1181
1211
  const attrs = inst.attributes;
1182
1212
  const qattrs = inst.queryAttributes;
1183
- const isQueryAll = crud.name.endsWith(QuerySuffix);
1213
+ const onlyAggregates = inst.aggregates !== undefined && qattrs === undefined;
1214
+ const isQueryAll = onlyAggregates || crud.name.endsWith(QuerySuffix);
1184
1215
  const distinct = crud.distinct.length > 0;
1216
+ maybeSetQueryClauses(inst, crud);
1185
1217
  if (attrs.size > 0) {
1186
1218
  await maybeValidateOneOfRefs(inst, env);
1187
1219
  }
@@ -1192,8 +1224,8 @@ async function evaluateCrudMap(crud, env) {
1192
1224
  if (qattrs === undefined && !isQueryAll) {
1193
1225
  throw new Error(`Pattern for ${entryName} with 'into' clause must be a query`);
1194
1226
  }
1195
- if (crud.join) {
1196
- await evaluateJoinQuery(crud.join, crud.into, inst, distinct, env);
1227
+ if (crud.joins.length > 0) {
1228
+ await evaluateJoinQuery(crud.joins, crud.into, crud.where, inst, distinct, env);
1197
1229
  }
1198
1230
  else {
1199
1231
  await evaluateJoinQueryWithRelationships(crud.into, inst, crud.relationships, distinct, env);
@@ -1211,7 +1243,7 @@ async function evaluateCrudMap(crud, env) {
1211
1243
  const res = await getResolverForPath(entryName, moduleName, env);
1212
1244
  let r;
1213
1245
  await computeExprAttributes(inst, undefined, undefined, env);
1214
- setMetaAttributes(inst.attributes, env);
1246
+ await setMetaAttributes(inst.attributes, env);
1215
1247
  if (env.isInUpsertMode()) {
1216
1248
  await runPreUpdateEvents(inst, env);
1217
1249
  r = await res.upsertInstance(inst);
@@ -1323,7 +1355,7 @@ async function evaluateCrudMap(crud, env) {
1323
1355
  await computeExprAttributes(lastRes[i], (_b = crud.body) === null || _b === void 0 ? void 0 : _b.attributes, attrs, env);
1324
1356
  env.attributes.set('__patch', attrs);
1325
1357
  await runPreUpdateEvents(lastRes[i], env);
1326
- setMetaAttributes(attrs, env, true);
1358
+ await setMetaAttributes(attrs, env, true);
1327
1359
  const finalInst = await resolver.updateInstance(lastRes[i], attrs);
1328
1360
  await runPostUpdateEvents(finalInst, lastRes[i], env);
1329
1361
  res.push(finalInst);
@@ -1381,47 +1413,6 @@ async function handleDocEvent(inst, env) {
1381
1413
  await parseAndEvaluateStatement(`{${CoreAIModuleName}/Document {title "${title}", content "${s}"}}`, undefined, env);
1382
1414
  }
1383
1415
  }
1384
- function triggerTimer(timerInst) {
1385
- const dur = timerInst.lookup('duration');
1386
- const unit = timerInst.lookup('unit');
1387
- let millisecs = 0;
1388
- switch (unit) {
1389
- case 'millisecond': {
1390
- millisecs = dur;
1391
- break;
1392
- }
1393
- case 'second': {
1394
- millisecs = dur * 1000;
1395
- break;
1396
- }
1397
- case 'minute': {
1398
- millisecs = dur * 60 * 1000;
1399
- break;
1400
- }
1401
- case 'hour': {
1402
- millisecs = dur * 60 * 60 * 1000;
1403
- break;
1404
- }
1405
- }
1406
- const eventName = nameToPath(timerInst.lookup('trigger'));
1407
- const m = eventName.hasModule() ? eventName.getModuleName() : timerInst.moduleName;
1408
- const n = eventName.getEntryName();
1409
- const inst = makeInstance(m, n, newInstanceAttributes());
1410
- const name = timerInst.lookup('name');
1411
- const timer = setInterval(async () => {
1412
- const env = new Environment();
1413
- try {
1414
- await evaluate(inst, (result) => logger.debug(`Timer ${name} ran with result ${result}`), env);
1415
- await env.commitAllTransactions();
1416
- await maybeCancelTimer(name, timer, env);
1417
- }
1418
- catch (reason) {
1419
- logger.error(`Timer ${name} raised error: ${reason}`);
1420
- }
1421
- }, millisecs);
1422
- setTimerRunning(timerInst);
1423
- return timerInst;
1424
- }
1425
1416
  async function computeExprAttributes(inst, origAttrs, updatedAttrs, env) {
1426
1417
  const exprAttrs = inst.getExprAttributes();
1427
1418
  if (exprAttrs || origAttrs) {
@@ -1452,7 +1443,7 @@ async function computeExprAttributes(inst, origAttrs, updatedAttrs, env) {
1452
1443
  for (let i = 0; i < origAttrs.length; ++i) {
1453
1444
  const a = origAttrs[i];
1454
1445
  const n = a.name;
1455
- if (!n.endsWith(QuerySuffix) && updatedAttrs.has(n)) {
1446
+ if (!n.endsWith(QuerySuffix) && updatedAttrs.has(n) && a.value !== undefined) {
1456
1447
  await evaluateExpression(a.value, newEnv);
1457
1448
  const v = newEnv.getLastResult();
1458
1449
  updatedAttrs.set(n, v);
@@ -1462,21 +1453,58 @@ async function computeExprAttributes(inst, origAttrs, updatedAttrs, env) {
1462
1453
  }
1463
1454
  }
1464
1455
  }
1465
- async function evaluateJoinQuery(joinSpec, intoSpec, inst, distinct, env) {
1456
+ async function evalWhereClauses(whereSpec, env) {
1457
+ const result = new Array();
1458
+ const e = new Environment(undefined, env);
1459
+ for (let i = 0; i < whereSpec.clauses.length; ++i) {
1460
+ const c = whereSpec.clauses[i];
1461
+ await evaluateExpression(c.rhs, e);
1462
+ result.push({
1463
+ attrName: escapeQueryName(c.lhs),
1464
+ op: c.op === undefined ? '=' : c.op,
1465
+ qval: e.getLastResult(),
1466
+ });
1467
+ }
1468
+ return result;
1469
+ }
1470
+ async function evaluateJoinQuery(joinSpec, intoSpec, whereSpec, inst, distinct, env) {
1466
1471
  const normIntoSpec = new Map();
1472
+ let aggregates;
1467
1473
  intoSpec.entries.forEach((entry) => {
1468
- normIntoSpec.set(entry.alias, entry.attribute);
1474
+ if (entry.attribute !== undefined)
1475
+ normIntoSpec.set(entry.alias, entry.attribute);
1476
+ else {
1477
+ if (aggregates === undefined)
1478
+ aggregates = new Map();
1479
+ if (entry.aggregate !== undefined)
1480
+ aggregates === null || aggregates === void 0 ? void 0 : aggregates.set(entry.alias, entry.aggregate);
1481
+ }
1469
1482
  });
1483
+ if (aggregates !== undefined) {
1484
+ inst.setAggregates(aggregates);
1485
+ }
1486
+ const clauses = whereSpec ? await evalWhereClauses(whereSpec, env) : undefined;
1470
1487
  const resolver = await getResolverForPath(inst.name, inst.moduleName, env);
1471
- const result = await resolver.queryByJoin(inst, [], normIntoSpec, distinct, joinSpec);
1488
+ const result = await resolver.queryByJoin(inst, [], normIntoSpec, distinct, joinSpec, clauses);
1472
1489
  const transformedResult = transformDateFieldsInJoinResult(result);
1473
1490
  env.setLastResult(transformedResult);
1474
1491
  }
1475
1492
  async function evaluateJoinQueryWithRelationships(intoSpec, inst, relationships, distinct, env) {
1476
1493
  const normIntoSpec = new Map();
1494
+ let aggregates;
1477
1495
  intoSpec.entries.forEach((entry) => {
1478
- normIntoSpec.set(entry.alias, entry.attribute);
1496
+ if (entry.attribute !== undefined)
1497
+ normIntoSpec.set(entry.alias, entry.attribute);
1498
+ else {
1499
+ if (aggregates === undefined)
1500
+ aggregates = new Map();
1501
+ if (entry.aggregate !== undefined)
1502
+ aggregates === null || aggregates === void 0 ? void 0 : aggregates.set(entry.alias, entry.aggregate);
1503
+ }
1479
1504
  });
1505
+ if (aggregates !== undefined) {
1506
+ inst.setAggregates(aggregates);
1507
+ }
1480
1508
  const moduleName = inst.moduleName;
1481
1509
  let joinsSpec = new Array();
1482
1510
  for (let i = 0; i < relationships.length; ++i) {
@@ -1709,7 +1737,10 @@ async function iterateOnFlow(flow, rootAgent, msg, env) {
1709
1737
  Important: Return only the next flow-step or DONE. Do not return any additional description, like your thinking process.\n`;
1710
1738
  env.setFlowContext(initContext);
1711
1739
  await agentInvoke(rootAgent, s, env);
1712
- let step = await preprocessStep(env.getLastResult().trim(), env);
1740
+ const rootModuleName = rootAgent.moduleName;
1741
+ let preprocResult = await preprocessStep(env.getLastResult().trim(), rootModuleName, env);
1742
+ let step = preprocResult.step;
1743
+ let needAgentProcessing = preprocResult.needAgentProcessing;
1713
1744
  let context = initContext;
1714
1745
  let stepc = 0;
1715
1746
  const iterId = crypto.randomUUID();
@@ -1719,6 +1750,7 @@ async function iterateOnFlow(flow, rootAgent, msg, env) {
1719
1750
  if (monitoringEnabled) {
1720
1751
  env.flagMonitorEntryAsFlow().incrementMonitor();
1721
1752
  }
1753
+ let isfxc = false;
1722
1754
  try {
1723
1755
  while (step != 'DONE' && !executedSteps.has(step)) {
1724
1756
  if (stepc > MaxFlowSteps) {
@@ -1726,20 +1758,27 @@ async function iterateOnFlow(flow, rootAgent, msg, env) {
1726
1758
  }
1727
1759
  executedSteps.add(step);
1728
1760
  ++stepc;
1729
- const agent = AgentInstance.FromFlowStep(step, rootAgent, context);
1730
- console.debug(`\n---------------------------------------------------\n`);
1731
- console.debug(`Starting to execute flow step ${step} with agent ${agent.name} with iteration ID ${iterId} and context: \n${context}`);
1732
- const isfxc = agent.isFlowExecutor();
1733
- const isdec = agent.isDecisionExecutor();
1734
- if (isfxc || isdec)
1735
- env.setFlowContext(context);
1736
- else
1737
- env.setFlowContext(initContext);
1738
- if (monitoringEnabled) {
1739
- env.appendEntryToMonitor(step);
1761
+ const agent = needAgentProcessing
1762
+ ? AgentInstance.FromFlowStep(step, rootAgent, context)
1763
+ : undefined;
1764
+ if (agent) {
1765
+ console.debug(`\n---------------------------------------------------\n`);
1766
+ console.debug(`Starting to execute flow step ${step} with agent ${agent.name} with iteration ID ${iterId} and context: \n${context}`);
1767
+ isfxc = agent.isFlowExecutor();
1768
+ const isdec = agent.isDecisionExecutor();
1769
+ if (isfxc || isdec)
1770
+ env.setFlowContext(context);
1771
+ else
1772
+ env.setFlowContext(initContext);
1773
+ if (monitoringEnabled) {
1774
+ env.appendEntryToMonitor(step);
1775
+ }
1776
+ const inst = agent.swapInstruction('');
1777
+ await agentInvoke(agent, inst, env);
1778
+ }
1779
+ else {
1780
+ rootAgent.maybeAddScratchData(env);
1740
1781
  }
1741
- const inst = agent.swapInstruction('');
1742
- await agentInvoke(agent, inst, env);
1743
1782
  if (monitoringEnabled)
1744
1783
  env.setMonitorEntryResult(env.getLastResult());
1745
1784
  if (env.isSuspended()) {
@@ -1753,13 +1792,15 @@ async function iterateOnFlow(flow, rootAgent, msg, env) {
1753
1792
  console.debug(`\n----> Completed execution of step ${step}, iteration id ${iterId} with result:\n${rs}`);
1754
1793
  context = `${context}\n${step} --> ${rs}\n`;
1755
1794
  if (isfxc) {
1756
- step = await preprocessStep(rs.trim(), env);
1795
+ preprocResult = await preprocessStep(rs.trim(), rootModuleName, env);
1757
1796
  }
1758
1797
  else {
1759
1798
  env.setFlowContext(context);
1760
1799
  await agentInvoke(rootAgent, `${s}\n${context}`, env);
1761
- step = await preprocessStep(env.getLastResult().trim(), env);
1800
+ preprocResult = await preprocessStep(env.getLastResult().trim(), rootModuleName, env);
1762
1801
  }
1802
+ step = preprocResult.step;
1803
+ needAgentProcessing = preprocResult.needAgentProcessing;
1763
1804
  }
1764
1805
  }
1765
1806
  finally {
@@ -1767,30 +1808,15 @@ async function iterateOnFlow(flow, rootAgent, msg, env) {
1767
1808
  }
1768
1809
  console.debug(`No more flow steps, completed iteration ${iterId} on flow:\n${flow}`);
1769
1810
  }
1770
- async function preprocessStep(spec, env) {
1811
+ async function preprocessStep(spec, activeModuleName, env) {
1812
+ let needAgentProcessing = true;
1771
1813
  if (spec.startsWith('{')) {
1772
- const stmt = await parseStatement(spec);
1773
- const crudMap = stmt.pattern.crudMap;
1774
- if (crudMap) {
1775
- env.resetTemplateMappings();
1776
- const step = crudMap.name;
1777
- if (crudMap.body) {
1778
- crudMap.body.attributes.forEach((sa) => {
1779
- var _a;
1780
- const v = (_a = sa.value.$cstNode) === null || _a === void 0 ? void 0 : _a.text;
1781
- if (v)
1782
- env.setTemplateMapping(sa.name, v);
1783
- });
1784
- }
1785
- return step;
1786
- }
1787
- else {
1788
- throw new Error(`Invalid step - ${spec}`);
1789
- }
1790
- }
1791
- else {
1792
- return spec;
1814
+ const newEnv = Environment.from(env, env.name + '_flow_eval', false, true).setActiveModuleName(activeModuleName);
1815
+ const r = await parseAndEvaluateStatement(spec, undefined, newEnv);
1816
+ env.setLastResult(r);
1817
+ needAgentProcessing = false;
1793
1818
  }
1819
+ return { step: spec, needAgentProcessing };
1794
1820
  }
1795
1821
  export async function handleOpenApiEvent(eventInst, env) {
1796
1822
  const r = await invokeOpenApiEvent(eventInst.moduleName, eventInst.name, eventInst.attributesAsObject());
@@ -1841,7 +1867,7 @@ export async function maybeDeleteQueriedInstances(queryEnv, env, purge = false)
1841
1867
  const inst = queryEnv.getLastResult();
1842
1868
  let resolver = Resolver.Default;
1843
1869
  if (inst instanceof Array) {
1844
- if (inst.length > 0 && isEntityInstance(inst[0])) {
1870
+ if (inst.length > 0) {
1845
1871
  resolver = await getResolverForPath(inst[0].name, inst[0].moduleName, queryEnv);
1846
1872
  const finalResult = new Array();
1847
1873
  for (let i = 0; i < inst.length; ++i) {
@@ -1856,7 +1882,7 @@ export async function maybeDeleteQueriedInstances(queryEnv, env, purge = false)
1856
1882
  queryEnv.setLastResult(inst);
1857
1883
  }
1858
1884
  }
1859
- else if (isEntityInstance(inst)) {
1885
+ else {
1860
1886
  resolver = await getResolverForPath(inst.name, inst.moduleName, queryEnv);
1861
1887
  await runPreDeleteEvents(inst, env);
1862
1888
  const r = await resolver.deleteInstance(inst, purge);
@@ -2103,7 +2129,7 @@ async function runPreCreateEvents(inst, env) {
2103
2129
  }
2104
2130
  export async function runPostCreateEvents(inst, env) {
2105
2131
  if (inst.requireAudit()) {
2106
- await addCreateAudit(inst.getPath(), env);
2132
+ await addCreateAudit(inst.getPath(), env, { original: inst.userAttributesAsObject() });
2107
2133
  }
2108
2134
  await runPrePostEvents(CrudType.CREATE, false, inst, env);
2109
2135
  }
@@ -2112,7 +2138,13 @@ async function runPreUpdateEvents(inst, env) {
2112
2138
  }
2113
2139
  export async function runPostUpdateEvents(inst, oldInst, env) {
2114
2140
  if (inst.requireAudit()) {
2115
- await addUpdateAudit(inst.getPath(), oldInst, env);
2141
+ let diff;
2142
+ if (oldInst !== undefined) {
2143
+ const oldAttrs = oldInst.userAttributesAsObject();
2144
+ const d = detailedDiff(oldAttrs, inst.userAttributesAsObject());
2145
+ diff = { original: oldAttrs, updated: d.updated };
2146
+ }
2147
+ await addUpdateAudit(inst.getPath(), diff, env);
2116
2148
  }
2117
2149
  await runPrePostEvents(CrudType.UPDATE, false, inst, env);
2118
2150
  }
@@ -2121,7 +2153,7 @@ async function runPreDeleteEvents(inst, env) {
2121
2153
  }
2122
2154
  export async function runPostDeleteEvents(inst, env) {
2123
2155
  if (inst.requireAudit()) {
2124
- await addDeleteAudit(inst.getPath(), inst, env);
2156
+ await addDeleteAudit(inst.getPath(), { deleted: inst.userAttributesAsObject() }, env);
2125
2157
  }
2126
2158
  await runPrePostEvents(CrudType.DELETE, false, inst, env);
2127
2159
  }