agentlang 0.10.0 → 0.10.2

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 (107) hide show
  1. package/out/api/http.d.ts.map +1 -1
  2. package/out/api/http.js +136 -0
  3. package/out/api/http.js.map +1 -1
  4. package/out/language/generated/ast.d.ts +87 -44
  5. package/out/language/generated/ast.d.ts.map +1 -1
  6. package/out/language/generated/ast.js +65 -31
  7. package/out/language/generated/ast.js.map +1 -1
  8. package/out/language/generated/grammar.d.ts.map +1 -1
  9. package/out/language/generated/grammar.js +337 -330
  10. package/out/language/generated/grammar.js.map +1 -1
  11. package/out/language/main.cjs +396 -361
  12. package/out/language/main.cjs.map +2 -2
  13. package/out/language/parser.d.ts +11 -1
  14. package/out/language/parser.d.ts.map +1 -1
  15. package/out/language/parser.js +74 -8
  16. package/out/language/parser.js.map +1 -1
  17. package/out/language/syntax.d.ts +14 -0
  18. package/out/language/syntax.d.ts.map +1 -1
  19. package/out/language/syntax.js +60 -27
  20. package/out/language/syntax.js.map +1 -1
  21. package/out/runtime/agents/common.d.ts +2 -2
  22. package/out/runtime/agents/common.js +1 -1
  23. package/out/runtime/api.d.ts +2 -0
  24. package/out/runtime/api.d.ts.map +1 -1
  25. package/out/runtime/api.js +3 -0
  26. package/out/runtime/api.js.map +1 -1
  27. package/out/runtime/datefns.d.ts +34 -0
  28. package/out/runtime/datefns.d.ts.map +1 -0
  29. package/out/runtime/datefns.js +82 -0
  30. package/out/runtime/datefns.js.map +1 -0
  31. package/out/runtime/exec-graph.d.ts.map +1 -1
  32. package/out/runtime/exec-graph.js +22 -3
  33. package/out/runtime/exec-graph.js.map +1 -1
  34. package/out/runtime/interpreter.d.ts +9 -0
  35. package/out/runtime/interpreter.d.ts.map +1 -1
  36. package/out/runtime/interpreter.js +85 -20
  37. package/out/runtime/interpreter.js.map +1 -1
  38. package/out/runtime/module.d.ts +8 -0
  39. package/out/runtime/module.d.ts.map +1 -1
  40. package/out/runtime/module.js +23 -0
  41. package/out/runtime/module.js.map +1 -1
  42. package/out/runtime/modules/ai.d.ts +9 -8
  43. package/out/runtime/modules/ai.d.ts.map +1 -1
  44. package/out/runtime/modules/ai.js +92 -37
  45. package/out/runtime/modules/ai.js.map +1 -1
  46. package/out/runtime/modules/core.d.ts.map +1 -1
  47. package/out/runtime/modules/core.js +5 -1
  48. package/out/runtime/modules/core.js.map +1 -1
  49. package/out/runtime/monitor.d.ts +6 -0
  50. package/out/runtime/monitor.d.ts.map +1 -1
  51. package/out/runtime/monitor.js +21 -1
  52. package/out/runtime/monitor.js.map +1 -1
  53. package/out/runtime/relgraph.d.ts.map +1 -1
  54. package/out/runtime/relgraph.js +7 -3
  55. package/out/runtime/relgraph.js.map +1 -1
  56. package/out/runtime/resolvers/interface.d.ts +3 -2
  57. package/out/runtime/resolvers/interface.d.ts.map +1 -1
  58. package/out/runtime/resolvers/interface.js +3 -2
  59. package/out/runtime/resolvers/interface.js.map +1 -1
  60. package/out/runtime/resolvers/sqldb/dbutil.d.ts.map +1 -1
  61. package/out/runtime/resolvers/sqldb/dbutil.js +17 -4
  62. package/out/runtime/resolvers/sqldb/dbutil.js.map +1 -1
  63. package/out/runtime/resolvers/sqldb/impl.d.ts +1 -1
  64. package/out/runtime/resolvers/sqldb/impl.d.ts.map +1 -1
  65. package/out/runtime/resolvers/sqldb/impl.js +17 -7
  66. package/out/runtime/resolvers/sqldb/impl.js.map +1 -1
  67. package/out/runtime/services/documentFetcher.js +2 -2
  68. package/out/runtime/services/documentFetcher.js.map +1 -1
  69. package/out/runtime/util.d.ts +12 -1
  70. package/out/runtime/util.d.ts.map +1 -1
  71. package/out/runtime/util.js +38 -2
  72. package/out/runtime/util.js.map +1 -1
  73. package/out/syntaxes/agentlang.monarch.js +1 -1
  74. package/out/syntaxes/agentlang.monarch.js.map +1 -1
  75. package/out/test-harness.d.ts +36 -0
  76. package/out/test-harness.d.ts.map +1 -0
  77. package/out/test-harness.js +341 -0
  78. package/out/test-harness.js.map +1 -0
  79. package/out/utils/fs/index.d.ts +12 -2
  80. package/out/utils/fs/index.d.ts.map +1 -1
  81. package/out/utils/fs/index.js +27 -6
  82. package/out/utils/fs/index.js.map +1 -1
  83. package/package.json +4 -1
  84. package/src/api/http.ts +138 -0
  85. package/src/language/agentlang.langium +9 -10
  86. package/src/language/generated/ast.ts +98 -44
  87. package/src/language/generated/grammar.ts +337 -330
  88. package/src/language/parser.ts +87 -6
  89. package/src/language/syntax.ts +71 -24
  90. package/src/runtime/agents/common.ts +1 -1
  91. package/src/runtime/api.ts +5 -0
  92. package/src/runtime/datefns.ts +112 -0
  93. package/src/runtime/exec-graph.ts +23 -2
  94. package/src/runtime/interpreter.ts +107 -19
  95. package/src/runtime/module.ts +26 -0
  96. package/src/runtime/modules/ai.ts +110 -54
  97. package/src/runtime/modules/core.ts +5 -1
  98. package/src/runtime/monitor.ts +27 -1
  99. package/src/runtime/relgraph.ts +7 -3
  100. package/src/runtime/resolvers/interface.ts +4 -2
  101. package/src/runtime/resolvers/sqldb/dbutil.ts +20 -6
  102. package/src/runtime/resolvers/sqldb/impl.ts +17 -7
  103. package/src/runtime/services/documentFetcher.ts +2 -2
  104. package/src/runtime/util.ts +62 -2
  105. package/src/syntaxes/agentlang.monarch.ts +1 -1
  106. package/src/test-harness.ts +423 -0
  107. package/src/utils/fs/index.ts +30 -6
@@ -5,7 +5,7 @@ import { ResolverAuthInfo } from './resolvers/authinfo.js';
5
5
  import { SqlDbResolver } from './resolvers/sqldb/impl.js';
6
6
  import { CrudType, DefaultModuleName, escapeFqName, escapeQueryName, fqNameFromPath, isCoreModule, isFqName, isPath, isString, makeCoreModuleName, makeFqName, nameToPath, preprocessRawConfig, QuerySuffix, restoreSpecialChars, splitRefs, } from './util.js';
7
7
  import { getResolver, getResolverNameForPath } from './resolvers/registry.js';
8
- import { parseStatement, parseWorkflow } from '../language/parser.js';
8
+ import { extractQueryOptions, parseStatement, parseWorkflow, } from '../language/parser.js';
9
9
  import { AdminSession, AdminUserId } from './auth/defs.js';
10
10
  import { AgentEntityName, AgentFqName, AgentInstance, findAgentByName, normalizeGeneratedCode, saveFlowStepResult, } from './modules/ai.js';
11
11
  import { logger } from './logger.js';
@@ -20,6 +20,7 @@ import { Monitor, MonitorEntry } from './monitor.js';
20
20
  import { detailedDiff } from 'deep-object-diff';
21
21
  import { callMcpTool, mcpClientNameFromToolEvent } from './mcpclient.js';
22
22
  import { isNodeEnv } from '../utils/runtime.js';
23
+ import Handlebars from 'handlebars';
23
24
  const EmptyResult = null;
24
25
  export function isEmptyResult(r) {
25
26
  return r == EmptyResult;
@@ -70,6 +71,7 @@ export class Environment extends Instance {
70
71
  this.agentChatId = parent.agentChatId;
71
72
  this.monitor = parent.monitor;
72
73
  this.escalatedRole = parent.escalatedRole;
74
+ this.activeChatId = parent.activeChatId;
73
75
  }
74
76
  else {
75
77
  this.activeModule = DefaultModuleName;
@@ -180,6 +182,13 @@ export class Environment extends Instance {
180
182
  getFlowContext() {
181
183
  return this.attributes.get(Environment.FlowContextTag);
182
184
  }
185
+ setActiveChatId(chatId) {
186
+ this.activeChatId = chatId;
187
+ return this;
188
+ }
189
+ getActiveChatId() {
190
+ return this.activeChatId;
191
+ }
183
192
  addToScratchPad(k, data) {
184
193
  if (this.scratchPad === undefined) {
185
194
  this.scratchPad = {};
@@ -244,6 +253,10 @@ export class Environment extends Instance {
244
253
  (_a = this.templateMappings) === null || _a === void 0 ? void 0 : _a.clear();
245
254
  return this;
246
255
  }
256
+ maybeRewriteTemplatePatterns(instruction, scratchPad) {
257
+ const templ = Handlebars.compile(this.rewriteTemplateMappings(instruction));
258
+ return templ(scratchPad);
259
+ }
247
260
  bindSuspensionUserData(userData) {
248
261
  this.bind(Environment.SuspensionUserData, userData);
249
262
  return this;
@@ -380,6 +393,13 @@ export class Environment extends Instance {
380
393
  getLastResult() {
381
394
  return this.lastResult;
382
395
  }
396
+ setLastPattern(pattern) {
397
+ this.lastPattern = pattern;
398
+ return this;
399
+ }
400
+ getLastPattern() {
401
+ return this.lastPattern;
402
+ }
383
403
  getActiveModuleName() {
384
404
  return this.activeModule;
385
405
  }
@@ -666,6 +686,12 @@ export class Environment extends Instance {
666
686
  }
667
687
  return this;
668
688
  }
689
+ setMonitorEntryLlmTokenUsage(input, output, total) {
690
+ if (this.monitor !== undefined) {
691
+ this.monitor.setEntryLlmTokenUsage(input, output, total);
692
+ }
693
+ return this;
694
+ }
669
695
  incrementMonitor() {
670
696
  if (this.monitor !== undefined) {
671
697
  this.monitor = this.monitor.increment();
@@ -709,8 +735,14 @@ export let evaluate = async function (eventInstance, continuation, activeEnv, ke
709
735
  else if (isAgentEventInstance(eventInstance)) {
710
736
  env = new Environment(eventInstance.name + '.env', activeEnv);
711
737
  await handleAgentInvocation(eventInstance, env);
712
- if (continuation)
713
- continuation(env.getLastResult());
738
+ if (continuation) {
739
+ if (env.getLastPattern()) {
740
+ continuation({ result: env.getLastResult(), pattern: env.getLastPattern() });
741
+ }
742
+ else {
743
+ continuation(env.getLastResult());
744
+ }
745
+ }
714
746
  }
715
747
  else if (isOpenApiEventInstance(eventInstance)) {
716
748
  env = new Environment(eventInstance.name + '.env', activeEnv);
@@ -833,6 +865,9 @@ export async function evaluateStatement(stmt, env) {
833
865
  handlersPushed = env.pushHandlers(handlers);
834
866
  }
835
867
  await evaluatePattern(stmt.pattern, env);
868
+ if (hasHints) {
869
+ await maybeHandleEmpty(hints, env);
870
+ }
836
871
  if (hasHints) {
837
872
  maybeBindStatementResultToAlias(hints, env);
838
873
  }
@@ -860,6 +895,21 @@ async function maybeHandleNotFound(handlers, env) {
860
895
  }
861
896
  }
862
897
  }
898
+ async function maybeHandleEmpty(hints, env) {
899
+ const lastResult = env.getLastResult();
900
+ if (lastResult === null ||
901
+ lastResult === undefined ||
902
+ (lastResult instanceof Array && lastResult.length == 0)) {
903
+ for (const rh of hints) {
904
+ if (rh.emptySpec) {
905
+ const newEnv = new Environment('empty-env', env).unsetEventExecutor();
906
+ await evaluateStatement(rh.emptySpec.stmt, newEnv);
907
+ env.setLastResult(newEnv.getLastResult());
908
+ break;
909
+ }
910
+ }
911
+ }
912
+ }
863
913
  async function maybeHandleError(handlers, reason, env) {
864
914
  const handler = handlers ? handlers.get('error') : undefined;
865
915
  if (handler) {
@@ -1219,17 +1269,18 @@ async function maybeValidateOneOfRefs(inst, env) {
1219
1269
  }
1220
1270
  }
1221
1271
  }
1222
- function maybeSetQueryClauses(inst, crud) {
1223
- if (crud.groupByClause) {
1224
- inst.setGroupBy(crud.groupByClause.colNames);
1272
+ function maybeSetQueryClauses(inst, qopts) {
1273
+ if (qopts.groupByClause) {
1274
+ inst.setGroupBy(qopts.groupByClause.colNames);
1225
1275
  }
1226
- if (crud.orderByClause) {
1227
- inst.setOrderBy(crud.orderByClause.colNames, crud.orderByClause.order === '@desc');
1276
+ if (qopts.orderByClause) {
1277
+ inst.setOrderBy(qopts.orderByClause.colNames, qopts.orderByClause.order === '@desc');
1228
1278
  }
1229
1279
  }
1230
1280
  async function evaluateCrudMap(crud, env) {
1231
1281
  var _a, _b, _c;
1232
- if (!env.isInUpsertMode() && crud.upsert.length > 0) {
1282
+ const qopts = extractQueryOptions(crud);
1283
+ if (!env.isInUpsertMode() && qopts.upsert !== undefined) {
1233
1284
  return await evaluateUpsert(crud, env);
1234
1285
  }
1235
1286
  const inst = crud.source
@@ -1241,23 +1292,23 @@ async function evaluateCrudMap(crud, env) {
1241
1292
  const qattrs = inst.queryAttributes;
1242
1293
  const onlyAggregates = inst.aggregates !== undefined && qattrs === undefined;
1243
1294
  const isQueryAll = onlyAggregates || crud.name.endsWith(QuerySuffix);
1244
- const distinct = crud.distinct.length > 0;
1245
- maybeSetQueryClauses(inst, crud);
1295
+ const distinct = qopts.distinct !== undefined;
1296
+ maybeSetQueryClauses(inst, qopts);
1246
1297
  if (attrs.size > 0) {
1247
1298
  await maybeValidateOneOfRefs(inst, env);
1248
1299
  }
1249
- if (crud.into) {
1300
+ if (qopts.into) {
1250
1301
  if (attrs.size > 0) {
1251
1302
  throw new Error(`Query pattern for ${entryName} with 'into' clause cannot be used to update attributes`);
1252
1303
  }
1253
1304
  if (qattrs === undefined && !isQueryAll) {
1254
1305
  throw new Error(`Pattern for ${entryName} with 'into' clause must be a query`);
1255
1306
  }
1256
- if (crud.joins.length > 0) {
1257
- await evaluateJoinQuery(crud.joins, crud.into, crud.where, inst, distinct, env);
1307
+ if (qopts.joins && qopts.joins.length > 0) {
1308
+ await evaluateJoinQuery(qopts.joins, qopts.into, qopts.where, inst, distinct, env);
1258
1309
  }
1259
1310
  else {
1260
- await evaluateJoinQueryWithRelationships(crud.into, inst, crud.relationships, distinct, env);
1311
+ await evaluateJoinQueryWithRelationships(qopts.into, inst, crud.relationships || [], distinct, env);
1261
1312
  }
1262
1313
  return;
1263
1314
  }
@@ -1336,7 +1387,7 @@ async function evaluateCrudMap(crud, env) {
1336
1387
  }
1337
1388
  else if (betRelInfo !== undefined) {
1338
1389
  res = await getResolverForPath(betRelInfo.relationship.name, betRelInfo.relationship.moduleName, env);
1339
- const insts = await res.queryConnectedInstances(betRelInfo.relationship, betRelInfo.connectedInstance, inst);
1390
+ const insts = await res.queryConnectedInstances(betRelInfo.relationship, betRelInfo.connectedInstance, inst, betRelInfo.connectedAlias);
1340
1391
  env.setLastResult(insts);
1341
1392
  }
1342
1393
  else {
@@ -1349,7 +1400,7 @@ async function evaluateCrudMap(crud, env) {
1349
1400
  }
1350
1401
  if (oneToOne && rel !== undefined) {
1351
1402
  await res.handleInstancesLink(inst.lookupQueryVal(rel.node1.alias), inst.lookupQueryVal(rel.node2.alias), rel, false, true);
1352
- env.setLastResult(inst);
1403
+ env.setLastResult([inst]);
1353
1404
  }
1354
1405
  else {
1355
1406
  const insts = await res.queryInstances(inst, isQueryAll, distinct);
@@ -1379,7 +1430,12 @@ async function evaluateCrudMap(crud, env) {
1379
1430
  lastRes[j].attachRelatedInstances(rel.name, newEnv.getLastResult());
1380
1431
  }
1381
1432
  else if (isBetweenRelationship(rel.name, moduleName)) {
1382
- newEnv.setBetweenRelInfo({ relationship: relEntry, connectedInstance: lastRes[j] });
1433
+ const connAlias = relEntry.isSelfReferencing() ? relEntry.node1.alias : undefined;
1434
+ newEnv.setBetweenRelInfo({
1435
+ relationship: relEntry,
1436
+ connectedInstance: lastRes[j],
1437
+ connectedAlias: connAlias,
1438
+ });
1383
1439
  await evaluatePattern(rel.pattern, newEnv);
1384
1440
  lastRes[j].attachRelatedInstances(rel.name, newEnv.getLastResult());
1385
1441
  }
@@ -1413,7 +1469,7 @@ async function evaluateCrudMap(crud, env) {
1413
1469
  await runPreUpdateEvents(lastRes, env);
1414
1470
  const finalInst = await res.updateInstance(lastRes, attrs);
1415
1471
  await runPostUpdateEvents(finalInst, lastRes, env);
1416
- env.setLastResult(finalInst);
1472
+ env.setLastResult([finalInst]);
1417
1473
  }
1418
1474
  }
1419
1475
  }
@@ -1670,6 +1726,12 @@ async function agentInvoke(agent, msg, env) {
1670
1726
  while (true) {
1671
1727
  try {
1672
1728
  let rs = result ? normalizeGeneratedCode(result) : '';
1729
+ if (agent.tools) {
1730
+ env.setLastPattern(rs);
1731
+ }
1732
+ else {
1733
+ env.setLastPattern(undefined);
1734
+ }
1673
1735
  let isWf = rs.startsWith('workflow');
1674
1736
  if (isWf && !agent.runWorkflows) {
1675
1737
  await parseWorkflow(rs);
@@ -1764,6 +1826,10 @@ async function agentInvoke(agent, msg, env) {
1764
1826
  }
1765
1827
  export async function handleAgentInvocation(agentEventInst, env) {
1766
1828
  const agent = await findAgentByName(agentEventInst.name, env);
1829
+ const chatId = agentEventInst.lookup('chatId');
1830
+ if (chatId) {
1831
+ env.setActiveChatId(chatId);
1832
+ }
1767
1833
  const origMsg = agentEventInst.lookup('message') || JSON.stringify(agentEventInst.asObject());
1768
1834
  const msg = isString(origMsg) ? origMsg : maybeInstanceAsString(origMsg);
1769
1835
  const flow = getAgentFlow(agent.name, agent.moduleName);
@@ -1773,7 +1839,6 @@ export async function handleAgentInvocation(agentEventInst, env) {
1773
1839
  else {
1774
1840
  const mode = agentEventInst.lookup('mode');
1775
1841
  let activeEnv = env;
1776
- const chatId = agentEventInst.lookup('chatId');
1777
1842
  if (chatId !== undefined) {
1778
1843
  activeEnv.setAgentChatId(chatId);
1779
1844
  }