agentlang 0.0.28 → 0.0.29

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.
@@ -1740,8 +1740,11 @@ async function runPrePostEvents(
1740
1740
  }
1741
1741
  };
1742
1742
  if (trigInfo.async) {
1743
- evaluate(eventInst, callback).catch(catchHandler);
1743
+ const newEnv = new Environment('async.prepost.env');
1744
+ newEnv.bind('this', inst);
1745
+ evaluate(eventInst, callback, newEnv).catch(catchHandler);
1744
1746
  } else {
1747
+ env.bind('this', inst);
1745
1748
  await evaluate(eventInst, callback, env).catch(catchHandler);
1746
1749
  }
1747
1750
  }
@@ -413,7 +413,7 @@ export function addRelationshipFromDef(
413
413
  }
414
414
 
415
415
  export function addWorkflowFromDef(def: WorkflowDefinition, moduleName: string): Workflow {
416
- return addWorkflow(def.name, moduleName, def.statements, def.hints);
416
+ return addWorkflow(def.name || '', moduleName, def.statements, def.header);
417
417
  }
418
418
 
419
419
  const StandaloneStatements = new Map<string, Statement[]>();
@@ -25,8 +25,6 @@ if (isNodeEnv) {
25
25
  maxFiles: '7d',
26
26
  });
27
27
 
28
- const consoleTransport = new winston.transports.Console();
29
-
30
28
  logger = winston.createLogger({
31
29
  format: winston.format.combine(
32
30
  winston.format.timestamp(),
@@ -34,7 +32,7 @@ if (isNodeEnv) {
34
32
  return `[${timestamp}] ${level}: ${message}`;
35
33
  })
36
34
  ),
37
- transports: [fileTransport, consoleTransport],
35
+ transports: [fileTransport],
38
36
  });
39
37
  } else {
40
38
  function mkLogger(tag: string): Function {
@@ -19,7 +19,7 @@ import {
19
19
  RbacSpecEntry,
20
20
  RbacSpecEntries,
21
21
  RbacOpr,
22
- WorkflowHint,
22
+ WorkflowHeader,
23
23
  } from '../language/generated/ast.js';
24
24
  import {
25
25
  Path,
@@ -45,7 +45,7 @@ import {
45
45
  } from './util.js';
46
46
  import { parseStatement } from '../language/parser.js';
47
47
  import { ActiveSessionInfo, AdminSession } from './auth/defs.js';
48
- import { FetchModuleFn, PathAttributeName, SetSubscription } from './defs.js';
48
+ import { FetchModuleFn, PathAttributeName } from './defs.js';
49
49
  import { logger } from './logger.js';
50
50
 
51
51
  export class ModuleEntry {
@@ -1157,10 +1157,17 @@ export class Relationship extends Record {
1157
1157
 
1158
1158
  export class Workflow extends ModuleEntry {
1159
1159
  statements: Statement[];
1160
+ generatedName: boolean;
1160
1161
 
1161
- constructor(name: string, patterns: Statement[], moduleName: string) {
1162
+ constructor(
1163
+ name: string,
1164
+ patterns: Statement[],
1165
+ moduleName: string,
1166
+ generatedName: boolean = false
1167
+ ) {
1162
1168
  super(name, moduleName);
1163
1169
  this.statements = patterns;
1170
+ this.generatedName = generatedName;
1164
1171
  }
1165
1172
 
1166
1173
  async addStatement(stmtCode: string): Promise<Workflow> {
@@ -1278,7 +1285,8 @@ export class Workflow extends ModuleEntry {
1278
1285
  }
1279
1286
 
1280
1287
  override toString() {
1281
- let s: string = `workflow ${normalizeWorkflowName(this.name)} {\n`;
1288
+ const n = this.generatedName ? untangleWorkflowName(this.name) : this.name;
1289
+ let s: string = `workflow ${normalizeWorkflowName(n)} {\n`;
1282
1290
  const ss = this.statementsToStringsHelper(this.statements);
1283
1291
  s = s.concat(joinStatements(ss));
1284
1292
  return s.concat('\n}');
@@ -1887,8 +1895,11 @@ export function addWorkflow(
1887
1895
  name: string,
1888
1896
  moduleName = activeModule,
1889
1897
  statements?: Statement[],
1890
- hints?: WorkflowHint[]
1898
+ hdr?: WorkflowHeader
1891
1899
  ): Workflow {
1900
+ if (hdr) {
1901
+ name = prePostWorkflowName(hdr.tag, hdr.prefix, hdr.name, moduleName);
1902
+ }
1892
1903
  const module: Module = fetchModule(moduleName);
1893
1904
  if (module.hasEntry(name)) {
1894
1905
  const entry: ModuleEntry = module.getEntry(name);
@@ -1900,39 +1911,48 @@ export function addWorkflow(
1900
1911
  event.addMeta(SystemDefinedEvent, 'true');
1901
1912
  }
1902
1913
  if (!statements) statements = new Array<Statement>();
1903
- if (hints && hints.length > 0) {
1914
+ if (hdr) {
1904
1915
  const eventFqName = makeFqName(moduleName, name);
1905
- hints.forEach((hint: WorkflowHint) => {
1906
- if (hint.subs) {
1907
- SetSubscription(eventFqName, hint.subs.resolverName);
1908
- } else if (hint.trigs) {
1909
- if (hint.trigs.after) {
1910
- hint.trigs.after.triggers.entries.forEach((te: TriggerEntry) => {
1911
- const entityDef = getEntityDefForTrigger(te, moduleName);
1912
- entityDef.addAfterTrigger({
1913
- on: te.on,
1914
- event: eventFqName,
1915
- async: te.async ? true : false,
1916
- });
1917
- });
1918
- } else if (hint.trigs.before) {
1919
- hint.trigs.before.triggers.entries.forEach((te: TriggerEntry) => {
1920
- const entityDef = getEntityDefForTrigger(te, moduleName);
1921
- entityDef.addBeforeTrigger({
1922
- on: te.on,
1923
- event: eventFqName,
1924
- async: te.async ? true : false,
1925
- });
1926
- });
1927
- }
1928
- }
1929
- });
1916
+ const entityDef = getEntityDef(hdr.name, moduleName);
1917
+ if (hdr.tag == '@after') {
1918
+ entityDef?.addAfterTrigger({
1919
+ on: hdr.prefix,
1920
+ event: eventFqName,
1921
+ async: false,
1922
+ });
1923
+ } else {
1924
+ entityDef?.addBeforeTrigger({
1925
+ on: hdr.prefix,
1926
+ event: eventFqName,
1927
+ async: false,
1928
+ });
1929
+ }
1930
+ }
1931
+ return module.addEntry(
1932
+ new Workflow(asWorkflowName(name), statements, moduleName, hdr ? true : false)
1933
+ ) as Workflow;
1934
+ }
1935
+
1936
+ function prePostWorkflowName(
1937
+ tag: '@after' | '@before',
1938
+ opr: 'create' | 'update' | 'delete',
1939
+ entityName: string,
1940
+ moduleName?: string
1941
+ ): string {
1942
+ const parts = splitFqName(entityName);
1943
+ const mname = parts.hasModule() ? parts.getModuleName() : moduleName;
1944
+ if (!mname) {
1945
+ throw new Error(`Cannot infer module name for ${entityName}`);
1930
1946
  }
1931
- return module.addEntry(new Workflow(asWorkflowName(name), statements, moduleName)) as Workflow;
1947
+ return `${tag.substring(1)}_${opr}_${mname}_${parts.getEntryName()}`;
1932
1948
  }
1933
1949
 
1934
- function getEntityDefForTrigger(te: TriggerEntry, moduleName: string): Entity {
1935
- const entityName: string = te.event;
1950
+ function untangleWorkflowName(name: string): string {
1951
+ const parts = name.split('_');
1952
+ return `@${parts[0]} ${parts[1]}:${parts[2]}/${parts[3]}`;
1953
+ }
1954
+
1955
+ function getEntityDef(entityName: string, moduleName: string): Entity | undefined {
1936
1956
  const parts = splitFqName(entityName);
1937
1957
  const mname = parts.hasModule() ? parts.getModuleName() : moduleName;
1938
1958
  return getEntity(parts.getEntryName(), mname);
@@ -1949,12 +1969,13 @@ export function getWorkflow(eventInstance: Instance): Workflow {
1949
1969
  return EmptyWorkflow;
1950
1970
  }
1951
1971
 
1952
- export function getEntity(name: string, moduleName: string): Entity {
1972
+ export function getEntity(name: string, moduleName: string): Entity | undefined {
1953
1973
  const fr: FetchModuleByEntryNameResult = fetchModuleByEntryName(name, moduleName);
1954
1974
  if (fr.module.isEntity(fr.entryName)) {
1955
1975
  return fr.module.getEntry(fr.entryName) as Entity;
1956
1976
  }
1957
- throw new Error(`Entity ${fr.entryName} not found in module ${fr.moduleName}`);
1977
+ logger.error(`Entity ${fr.entryName} not found in module ${fr.moduleName}`);
1978
+ return undefined;
1958
1979
  }
1959
1980
 
1960
1981
  function isEntryOfType(t: RecordType, fqName: string): boolean {
@@ -2782,7 +2803,7 @@ export function getEntityRbacRules(entityFqName: string): RbacSpecification[] |
2782
2803
  const m = isModule(mn) && fetchModule(mn);
2783
2804
  if (m && m.isEntity(en)) {
2784
2805
  const entity = getEntity(en, mn);
2785
- return entity.getRbacSpecifications()?.filter((spec: RbacSpecification) => {
2806
+ return entity?.getRbacSpecifications()?.filter((spec: RbacSpecification) => {
2786
2807
  return spec.expression != undefined;
2787
2808
  });
2788
2809
  }
@@ -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','@into','@meta','@oneof','@rbac','@ref','@subs','@then','@upsert','@with_unique','agent','allow','and','await','between','contains','create','delete','else','entity','error','event','extends','false','for','if','import','in','like','module','not','not_found','onSubscription','or','purge','query','read','record','relationship','resolver','return','roles','subscribe','true','update','upsert','where','workflow'
4
+ '@actions','@after','@as','@async','@before','@catch','@distinct','@enum','@expr','@from','@into','@meta','@oneof','@rbac','@ref','@then','@upsert','@with_unique','agent','allow','and','await','between','contains','create','delete','else','entity','error','event','extends','false','for','if','import','in','like','module','not','not_found','onSubscription','or','purge','query','read','record','relationship','resolver','return','roles','subscribe','true','update','upsert','where','workflow'
5
5
  ],
6
6
  operators: [
7
7
  '!=','*','+',',','-','.','/',':',';','<','<=','<>','=','>','>=','?','@'