agentlang 0.7.1 → 0.7.3

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 (74) hide show
  1. package/out/api/http.d.ts.map +1 -1
  2. package/out/api/http.js +46 -26
  3. package/out/api/http.js.map +1 -1
  4. package/out/cli/main.d.ts +1 -0
  5. package/out/cli/main.d.ts.map +1 -1
  6. package/out/cli/main.js +15 -2
  7. package/out/cli/main.js.map +1 -1
  8. package/out/language/generated/ast.d.ts +15 -6
  9. package/out/language/generated/ast.d.ts.map +1 -1
  10. package/out/language/generated/ast.js +15 -2
  11. package/out/language/generated/ast.js.map +1 -1
  12. package/out/language/generated/grammar.d.ts.map +1 -1
  13. package/out/language/generated/grammar.js +215 -164
  14. package/out/language/generated/grammar.js.map +1 -1
  15. package/out/language/main.cjs +227 -166
  16. package/out/language/main.cjs.map +2 -2
  17. package/out/runtime/defs.d.ts +7 -1
  18. package/out/runtime/defs.d.ts.map +1 -1
  19. package/out/runtime/defs.js +22 -1
  20. package/out/runtime/defs.js.map +1 -1
  21. package/out/runtime/exec-graph.d.ts.map +1 -1
  22. package/out/runtime/exec-graph.js +8 -0
  23. package/out/runtime/exec-graph.js.map +1 -1
  24. package/out/runtime/interpreter.d.ts +2 -1
  25. package/out/runtime/interpreter.d.ts.map +1 -1
  26. package/out/runtime/interpreter.js +13 -3
  27. package/out/runtime/interpreter.js.map +1 -1
  28. package/out/runtime/loader.d.ts +1 -0
  29. package/out/runtime/loader.d.ts.map +1 -1
  30. package/out/runtime/loader.js +6 -0
  31. package/out/runtime/loader.js.map +1 -1
  32. package/out/runtime/module.d.ts +3 -2
  33. package/out/runtime/module.d.ts.map +1 -1
  34. package/out/runtime/module.js +7 -3
  35. package/out/runtime/module.js.map +1 -1
  36. package/out/runtime/modules/core.d.ts +5 -0
  37. package/out/runtime/modules/core.d.ts.map +1 -1
  38. package/out/runtime/modules/core.js +47 -2
  39. package/out/runtime/modules/core.js.map +1 -1
  40. package/out/runtime/resolvers/sqldb/database.d.ts.map +1 -1
  41. package/out/runtime/resolvers/sqldb/database.js +2 -1
  42. package/out/runtime/resolvers/sqldb/database.js.map +1 -1
  43. package/out/runtime/resolvers/sqldb/impl.d.ts.map +1 -1
  44. package/out/runtime/resolvers/sqldb/impl.js +3 -2
  45. package/out/runtime/resolvers/sqldb/impl.js.map +1 -1
  46. package/out/setupClassic.d.ts +98 -0
  47. package/out/setupClassic.d.ts.map +1 -0
  48. package/out/setupClassic.js +38 -0
  49. package/out/setupClassic.js.map +1 -0
  50. package/out/setupCommon.d.ts +2 -0
  51. package/out/setupCommon.d.ts.map +1 -0
  52. package/out/setupCommon.js +33 -0
  53. package/out/setupCommon.js.map +1 -0
  54. package/out/setupExtended.d.ts +40 -0
  55. package/out/setupExtended.d.ts.map +1 -0
  56. package/out/setupExtended.js +67 -0
  57. package/out/setupExtended.js.map +1 -0
  58. package/out/syntaxes/agentlang.monarch.js +1 -1
  59. package/out/syntaxes/agentlang.monarch.js.map +1 -1
  60. package/package.json +186 -185
  61. package/src/api/http.ts +51 -25
  62. package/src/cli/main.ts +20 -0
  63. package/src/language/agentlang.langium +3 -1
  64. package/src/language/generated/ast.ts +31 -7
  65. package/src/language/generated/grammar.ts +215 -164
  66. package/src/runtime/defs.ts +27 -0
  67. package/src/runtime/exec-graph.ts +13 -0
  68. package/src/runtime/interpreter.ts +15 -3
  69. package/src/runtime/loader.ts +7 -0
  70. package/src/runtime/module.ts +12 -3
  71. package/src/runtime/modules/core.ts +67 -2
  72. package/src/runtime/resolvers/sqldb/database.ts +2 -0
  73. package/src/runtime/resolvers/sqldb/impl.ts +3 -10
  74. package/src/syntaxes/agentlang.monarch.ts +1 -1
@@ -125,6 +125,7 @@ export enum SubGraphType {
125
125
  PURGE,
126
126
  RETURN,
127
127
  AGENT,
128
+ THROW,
128
129
  NONE,
129
130
  }
130
131
 
@@ -406,3 +407,29 @@ export function isRuntimeMode_generate_migration(): boolean {
406
407
  export function isRuntimeMode_undo_migration(): boolean {
407
408
  return RuntimeMode === RuntimeModeTag.UNDO_MIGRATION;
408
409
  }
410
+
411
+ let UpdateEventEndpoints: Function | undefined;
412
+ let UpdateEntityEndpoints: Function | undefined;
413
+
414
+ export function setEventEndpointsUpdater(f: Function) {
415
+ UpdateEventEndpoints = f;
416
+ }
417
+
418
+ export function setEntityEndpointsUpdater(f: Function) {
419
+ UpdateEntityEndpoints = f;
420
+ }
421
+
422
+ export function updateEndpoints(moduleName: string) {
423
+ if (UpdateEventEndpoints !== undefined) {
424
+ UpdateEventEndpoints(moduleName);
425
+ }
426
+ if (UpdateEntityEndpoints !== undefined) {
427
+ UpdateEntityEndpoints(moduleName);
428
+ }
429
+ }
430
+
431
+ export let InternDynamicModule: Function | undefined;
432
+
433
+ export function setInternDynamicModuleFn(f: Function) {
434
+ InternDynamicModule = f;
435
+ }
@@ -13,6 +13,7 @@ import {
13
13
  Purge,
14
14
  Return,
15
15
  Statement,
16
+ ThrowError,
16
17
  } from '../language/generated/ast.js';
17
18
  import { parseModule, parseStatement } from '../language/parser.js';
18
19
  import { ExecGraph, ExecGraphNode, ExecGraphWalker, SubGraphType } from './defs.js';
@@ -157,6 +158,15 @@ class GraphGenerator extends PatternHandler {
157
158
  this.handleSubPattern(SubGraphType.RETURN, ret.pattern, env);
158
159
  }
159
160
 
161
+ override async handleThrow(throwErr: ThrowError, env: Environment) {
162
+ const handler = new GraphGenerator();
163
+ await handler.handleExpression(
164
+ throwErr.reason,
165
+ Environment.from(env).setActiveUserData(throwErr.reason)
166
+ );
167
+ this.addSubGraph(SubGraphType.THROW, handler.getGraph(), env);
168
+ }
169
+
160
170
  getGraph(): ExecGraph {
161
171
  return this.graph;
162
172
  }
@@ -261,6 +271,9 @@ export async function executeGraph(execGraph: ExecGraph, env: Environment): Prom
261
271
  case SubGraphType.RETURN:
262
272
  await executeReturnSubGraph(subg, env);
263
273
  return;
274
+ case SubGraphType.THROW:
275
+ await evaluateExpression(subg.getRootNodes()[0].code as Expr, env);
276
+ throw new Error(env.getLastResult());
264
277
  default:
265
278
  throw new Error(`Invalid sub-graph type: ${node.subGraphType}`);
266
279
  }
@@ -27,6 +27,7 @@ import {
27
27
  SelectIntoSpec,
28
28
  SetAttribute,
29
29
  Statement,
30
+ ThrowError,
30
31
  } from '../language/generated/ast.js';
31
32
  import {
32
33
  maybeInstanceAsString,
@@ -49,7 +50,7 @@ import {
49
50
  PlaceholderRecordEntry,
50
51
  Relationship,
51
52
  Workflow,
52
- maybeSetMetaAttributes,
53
+ setMetaAttributes,
53
54
  } from './module.js';
54
55
  import { JoinInfo, Resolver } from './resolvers/interface.js';
55
56
  import { ResolverAuthInfo } from './resolvers/authinfo.js';
@@ -1217,6 +1218,10 @@ export class PatternHandler {
1217
1218
  async handleReturn(ret: Return, env: Environment) {
1218
1219
  await evaluatePattern(ret.pattern, env);
1219
1220
  }
1221
+
1222
+ async handleThrow(throwErr: ThrowError, env: Environment) {
1223
+ await evaluateThrowError(throwErr, env);
1224
+ }
1220
1225
  }
1221
1226
 
1222
1227
  const DefaultPatternHandler = new PatternHandler();
@@ -1243,9 +1248,16 @@ export async function evaluatePattern(
1243
1248
  } else if (pat.return) {
1244
1249
  await handler.handleReturn(pat.return, env);
1245
1250
  env.markForReturn();
1251
+ } else if (pat.throwError) {
1252
+ await handler.handleThrow(pat.throwError, env);
1246
1253
  }
1247
1254
  }
1248
1255
 
1256
+ async function evaluateThrowError(throwErr: ThrowError, env: Environment) {
1257
+ await evaluateExpression(throwErr.reason, env);
1258
+ throw new Error(env.getLastResult());
1259
+ }
1260
+
1249
1261
  async function evaluateFullTextSearch(fts: FullTextSearch, env: Environment): Promise<void> {
1250
1262
  let n = escapeQueryName(fts.name);
1251
1263
  if (!isFqName(n)) {
@@ -1461,7 +1473,7 @@ async function evaluateCrudMap(crud: CrudMap, env: Environment): Promise<void> {
1461
1473
  const res: Resolver = await getResolverForPath(entryName, moduleName, env);
1462
1474
  let r: Instance | undefined;
1463
1475
  await computeExprAttributes(inst, undefined, undefined, env);
1464
- maybeSetMetaAttributes(inst.attributes, env);
1476
+ setMetaAttributes(inst.attributes, env);
1465
1477
  if (env.isInUpsertMode()) {
1466
1478
  await runPreUpdateEvents(inst, env);
1467
1479
  r = await res.upsertInstance(inst);
@@ -1581,7 +1593,7 @@ async function evaluateCrudMap(crud: CrudMap, env: Environment): Promise<void> {
1581
1593
  await computeExprAttributes(lastRes[i], crud.body?.attributes, attrs, env);
1582
1594
  env.attributes.set('__patch', attrs);
1583
1595
  await runPreUpdateEvents(lastRes[i], env);
1584
- maybeSetMetaAttributes(attrs, env, true);
1596
+ setMetaAttributes(attrs, env, true);
1585
1597
  const finalInst: Instance = await resolver.updateInstance(lastRes[i], attrs);
1586
1598
  await runPostUpdateEvents(finalInst, lastRes[i], env);
1587
1599
  res.push(finalInst);
@@ -1137,6 +1137,13 @@ export async function parseAndIntern(code: string, moduleName?: string) {
1137
1137
  await internModule(r.parseResult.value);
1138
1138
  }
1139
1139
 
1140
+ export async function refreshModuleDefinition(moduleName: string, moduleDefinition: string) {
1141
+ removeModule(moduleName);
1142
+ const r = await parse(moduleDefinition);
1143
+ maybeRaiseParserErrors(r);
1144
+ await internModule(r.parseResult.value);
1145
+ }
1146
+
1140
1147
  export async function internModule(
1141
1148
  module: ModuleDefinition,
1142
1149
  moduleFileName?: string
@@ -1248,7 +1248,7 @@ export class Relationship extends Record {
1248
1248
 
1249
1249
  constructor(
1250
1250
  name: string,
1251
- typ: string,
1251
+ type: string,
1252
1252
  node1: RelationshipNode,
1253
1253
  node2: RelationshipNode,
1254
1254
  moduleName: string,
@@ -1256,7 +1256,7 @@ export class Relationship extends Record {
1256
1256
  props?: Map<string, any>
1257
1257
  ) {
1258
1258
  super(name, moduleName, scm);
1259
- if (typ == 'between') {
1259
+ if (type == 'between') {
1260
1260
  this.relType = RelType.BETWEEN;
1261
1261
  this.addMetaAttributes();
1262
1262
  }
@@ -3999,7 +3999,7 @@ export function getAttributeNames(entityFqName: string): Array<string> {
3999
3999
  return [...scm.keys()];
4000
4000
  }
4001
4001
 
4002
- export function maybeSetMetaAttributes(
4002
+ export function setMetaAttributes(
4003
4003
  attrs: InstanceAttributes,
4004
4004
  env: Environment,
4005
4005
  inUpdateMode: boolean = false
@@ -4010,3 +4010,12 @@ export function maybeSetMetaAttributes(
4010
4010
  attrs.set(SysAttr_CreatedBy, user);
4011
4011
  }
4012
4012
  }
4013
+
4014
+ export function setAllMetaAttributes(
4015
+ attrs: InstanceAttributes,
4016
+ env: Environment,
4017
+ inUpdateMode: boolean = false
4018
+ ) {
4019
+ attrs.set(SysAttr_Created, now());
4020
+ setMetaAttributes(attrs, env, inUpdateMode);
4021
+ }
@@ -9,7 +9,15 @@ import {
9
9
  restoreSpecialChars,
10
10
  makeCoreModuleName,
11
11
  } from '../util.js';
12
- import { Instance, isInstanceOfType, makeInstance, newInstanceAttributes } from '../module.js';
12
+ import {
13
+ fetchModule,
14
+ Instance,
15
+ isInstanceOfType,
16
+ isModule,
17
+ makeInstance,
18
+ newInstanceAttributes,
19
+ removeModule,
20
+ } from '../module.js';
13
21
  import {
14
22
  Environment,
15
23
  evaluate,
@@ -21,7 +29,12 @@ import { logger } from '../logger.js';
21
29
  import { Statement } from '../../language/generated/ast.js';
22
30
  import { parseModule, parseStatements } from '../../language/parser.js';
23
31
  import { Resolver } from '../resolvers/interface.js';
24
- import { FlowSuspensionTag, ForceReadPermFlag, PathAttributeName } from '../defs.js';
32
+ import {
33
+ FlowSuspensionTag,
34
+ ForceReadPermFlag,
35
+ InternDynamicModule,
36
+ PathAttributeName,
37
+ } from '../defs.js';
25
38
  import { getMonitor, getMonitorsForEvent, Monitor } from '../monitor.js';
26
39
 
27
40
  const CoreModuleDefinition = `module ${DefaultModuleName}
@@ -128,6 +141,18 @@ workflow validateModule {
128
141
  await Core.validateModule(validateModule.data)
129
142
  }
130
143
 
144
+ entity Module {
145
+ name String @id,
146
+ definition String
147
+ }
148
+
149
+ resolver moduleResolver [agentlang/Module] {
150
+ create Core.createModule,
151
+ update Core.updateModule,
152
+ delete Core.deleteModule,
153
+ query Core.getModule
154
+ }
155
+
131
156
  entity Migration {
132
157
  appVersion String @id,
133
158
  ups String @optional,
@@ -427,6 +452,46 @@ export async function validateModule(moduleDef: any): Promise<Instance> {
427
452
  }
428
453
  }
429
454
 
455
+ export async function internModuleHelper(
456
+ name: string,
457
+ definition: string
458
+ ): Promise<string | undefined> {
459
+ if (InternDynamicModule !== undefined) {
460
+ return await InternDynamicModule(name, definition);
461
+ } else {
462
+ return undefined;
463
+ }
464
+ }
465
+
466
+ export async function createModule(_: Resolver, inst: Instance) {
467
+ await internModuleHelper(inst.lookup('name'), inst.lookup('definition'));
468
+ return inst;
469
+ }
470
+
471
+ export async function updateModule(r: Resolver, inst: Instance) {
472
+ return await createModule(r, inst);
473
+ }
474
+
475
+ export async function deleteModule(_: Resolver, inst: Instance) {
476
+ removeModule(inst.lookup('name'));
477
+ return inst;
478
+ }
479
+
480
+ export async function getModule(_: Resolver, inst: Instance) {
481
+ const p = inst.lookupQueryVal(PathAttributeName);
482
+ if (p !== undefined) {
483
+ const idx = p.lastIndexOf('/');
484
+ const n = p.substring(idx + 1);
485
+ if (isModule(n)) {
486
+ const m = fetchModule(n);
487
+ const defn = inst.lookup('definition') || m.toString();
488
+ const attrs = newInstanceAttributes().set('name', n).set('definition', defn);
489
+ return [makeInstance('agentlang', 'Module', attrs)];
490
+ }
491
+ }
492
+ return null;
493
+ }
494
+
430
495
  const SqlSep = ';\n\n';
431
496
 
432
497
  export async function saveMigration(
@@ -24,6 +24,7 @@ import {
24
24
  RbacPermissionFlag,
25
25
  RbacSpecification,
26
26
  Relationship,
27
+ setAllMetaAttributes,
27
28
  } from '../../module.js';
28
29
  import { isString } from '../../util.js';
29
30
  import {
@@ -646,6 +647,7 @@ export async function insertBetweenRow(
646
647
  if (relEntry.isOneToMany()) {
647
648
  attrs.set(relEntry.joinNodesAttributeName(), `${p1}_${p2}`);
648
649
  }
650
+ setAllMetaAttributes(attrs, ctx.activeEnv);
649
651
  const row = Object.fromEntries(attrs);
650
652
  await insertRow(n, row, ctx.clone().setNeedAuthCheck(false), false);
651
653
  } else {
@@ -435,6 +435,7 @@ export class SqlDbResolver extends Resolver {
435
435
  const a1: string = relEntry.node1.alias;
436
436
  const a2: string = relEntry.node2.alias;
437
437
  const n1path: any = orUpdate ? firstNode.lookup(PathAttributeName) : undefined;
438
+ const ctx = this.getDbContext(relEntry.getFqName());
438
439
  if (relEntry.isOneToOne()) {
439
440
  await this.updateInstance(
440
441
  node1,
@@ -452,18 +453,10 @@ export class SqlDbResolver extends Resolver {
452
453
  [a1, n1path],
453
454
  [a2, secondNode.lookup(PathAttributeName)],
454
455
  ],
455
- this.getDbContext(relEntry.getFqName())
456
+ ctx
456
457
  );
457
458
  }
458
- await insertBetweenRow(
459
- n,
460
- a1,
461
- a2,
462
- firstNode,
463
- secondNode,
464
- relEntry,
465
- this.getDbContext(relEntry.getFqName())
466
- );
459
+ await insertBetweenRow(n, a1, a2, firstNode, secondNode, relEntry, ctx);
467
460
  }
468
461
  }
469
462
 
@@ -1,7 +1,7 @@
1
1
  // Monarch syntax highlighting for the agentlang language.
2
2
  export default {
3
3
  keywords: [
4
- '@actions','@after','@as','@async','@before','@catch','@distinct','@enum','@expr','@from','@full_join','@inner_join','@into','@join','@left_join','@meta','@oneof','@public','@rbac','@ref','@right_join','@then','@upsert','@with_unique','agent','agentlang/retry','allow','and','attempts','await','backoff','between','case','commitTransaction','contains','create','decision','delete','directive','else','entity','error','event','extends','false','flow','for','glossaryEntry','if','import','in','like','module','not','not_found','onSubscription','or','purge','query','read','record','relationship','resolver','return','roles','rollbackTransaction','scenario','startTransaction','subscribe','true','update','upsert','where','workflow'
4
+ '@actions','@after','@as','@async','@before','@catch','@distinct','@enum','@expr','@from','@full_join','@inner_join','@into','@join','@left_join','@meta','@oneof','@public','@rbac','@ref','@right_join','@then','@upsert','@with_unique','agent','agentlang/retry','allow','and','attempts','await','backoff','between','case','commitTransaction','contains','create','decision','delete','directive','else','entity','error','event','extends','false','flow','for','glossaryEntry','if','import','in','like','module','not','not_found','onSubscription','or','purge','query','read','record','relationship','resolver','return','roles','rollbackTransaction','scenario','startTransaction','subscribe','throw','true','update','upsert','where','workflow'
5
5
  ],
6
6
  operators: [
7
7
  '!=','*','+',',','-','-->','.','/',':',';','<','<=','<>','=','==','>','>=','?','@'