agentlang 0.0.56 → 0.0.58

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.
@@ -47,6 +47,7 @@ import { parseStatement } from '../language/parser.js';
47
47
  import { ActiveSessionInfo, AdminSession } from './auth/defs.js';
48
48
  import { FetchModuleFn, PathAttributeName } from './defs.js';
49
49
  import { logger } from './logger.js';
50
+ import { FlowStepPattern } from '../language/syntax.js';
50
51
 
51
52
  export class ModuleEntry {
52
53
  name: string;
@@ -1316,6 +1317,13 @@ export class Workflow extends ModuleEntry {
1316
1317
  }
1317
1318
  }
1318
1319
 
1320
+ export type FlowGraphNode = {
1321
+ label: string;
1322
+ type: 'action' | 'condition';
1323
+ on?: string[] | undefined;
1324
+ next: string[];
1325
+ };
1326
+
1319
1327
  export class Flow extends ModuleEntry {
1320
1328
  flowSteps: string[];
1321
1329
 
@@ -1344,10 +1352,47 @@ export class Flow extends ModuleEntry {
1344
1352
  return this;
1345
1353
  }
1346
1354
 
1355
+ appendStep(s: string): Flow {
1356
+ this.flowSteps.push(s);
1357
+ return this;
1358
+ }
1359
+
1347
1360
  stepsCount(): number {
1348
1361
  return this.flowSteps.length;
1349
1362
  }
1350
1363
 
1364
+ toGraph(): FlowGraphNode[] {
1365
+ const result = new Array<FlowGraphNode>();
1366
+ this.flowSteps.forEach((s: string) => {
1367
+ const fp = FlowStepPattern.Parse(s);
1368
+ if (fp.condition) {
1369
+ const orig = result.find((v: FlowGraphNode) => {
1370
+ return v.label == fp.first;
1371
+ });
1372
+ if (orig) {
1373
+ const nxs = orig.next;
1374
+ nxs?.push(fp.next);
1375
+ const conds = orig.on;
1376
+ conds?.push(fp.condition);
1377
+ } else {
1378
+ result.push({
1379
+ label: fp.first,
1380
+ type: 'condition',
1381
+ on: [fp.condition],
1382
+ next: [fp.next],
1383
+ });
1384
+ }
1385
+ } else {
1386
+ result.push({
1387
+ label: fp.first,
1388
+ type: 'action',
1389
+ next: [fp.next],
1390
+ });
1391
+ }
1392
+ });
1393
+ return result;
1394
+ }
1395
+
1351
1396
  override toString(): string {
1352
1397
  return `flow ${this.name} {
1353
1398
  ${this.getFlow()}
@@ -1355,6 +1400,40 @@ export class Flow extends ModuleEntry {
1355
1400
  }
1356
1401
  }
1357
1402
 
1403
+ export function flowGraphNext(
1404
+ graph: FlowGraphNode[],
1405
+ currentNode?: FlowGraphNode,
1406
+ onCondition?: string
1407
+ ): FlowGraphNode | undefined {
1408
+ if (!currentNode) {
1409
+ return graph[0];
1410
+ }
1411
+ const node = graph.find((n: FlowGraphNode) => {
1412
+ return n.label == currentNode.label;
1413
+ });
1414
+ if (node) {
1415
+ if (onCondition) {
1416
+ const c = node.on?.findIndex((v: string) => {
1417
+ return v == onCondition;
1418
+ });
1419
+ if (c != undefined) {
1420
+ const next = node.next[c];
1421
+ const r = graph.find((n: FlowGraphNode) => {
1422
+ return n.label == next;
1423
+ });
1424
+ return r || { label: next, type: 'action', next: [] };
1425
+ } else {
1426
+ return undefined;
1427
+ }
1428
+ } else {
1429
+ return graph.find((n: FlowGraphNode) => {
1430
+ return n.label == node.next[0];
1431
+ });
1432
+ }
1433
+ }
1434
+ return undefined;
1435
+ }
1436
+
1358
1437
  class StandaloneStatement extends ModuleEntry {
1359
1438
  stmt: Statement;
1360
1439
 
@@ -1378,17 +1457,14 @@ export function isEmptyWorkflow(wf: Workflow): boolean {
1378
1457
  export class Module {
1379
1458
  name: string;
1380
1459
  entries: ModuleEntry[];
1381
- entriesByTypeCache: Map<RecordType, ModuleEntry[]> | null;
1382
1460
 
1383
1461
  constructor(name: string) {
1384
1462
  this.name = name;
1385
1463
  this.entries = new Array<ModuleEntry>();
1386
- this.entriesByTypeCache = null;
1387
1464
  }
1388
1465
 
1389
1466
  addEntry(entry: ModuleEntry): ModuleEntry {
1390
1467
  this.entries.push(entry);
1391
- if (this.entriesByTypeCache != null) this.entriesByTypeCache = null;
1392
1468
  return entry;
1393
1469
  }
1394
1470
 
@@ -1427,6 +1503,19 @@ export class Module {
1427
1503
  return undefined;
1428
1504
  }
1429
1505
 
1506
+ getAllFlows(): Flow[] {
1507
+ return this.entries.filter((v: ModuleEntry) => {
1508
+ return v instanceof Flow;
1509
+ });
1510
+ }
1511
+
1512
+ removeFlow(name: string): boolean {
1513
+ if (this.getFlow(name)) {
1514
+ return this.removeEntry(name);
1515
+ }
1516
+ return false;
1517
+ }
1518
+
1430
1519
  addStandaloneStatement(stmt: Statement): StandaloneStatement {
1431
1520
  const s = new StandaloneStatement(stmt, this.name);
1432
1521
  this.addEntry(s);
@@ -1468,27 +1557,16 @@ export class Module {
1468
1557
  const idx: number = this.getEntryIndex(entryName);
1469
1558
  if (idx >= 0) {
1470
1559
  this.entries.splice(idx, 1);
1471
- if (this.entriesByTypeCache != null) this.entriesByTypeCache = null;
1472
1560
  return true;
1473
1561
  }
1474
1562
  return false;
1475
1563
  }
1476
1564
 
1477
1565
  private getEntriesOfType(t: RecordType): ModuleEntry[] {
1478
- if (this.entriesByTypeCache != null && this.entriesByTypeCache.has(t)) {
1479
- const result: ModuleEntry[] | undefined = this.entriesByTypeCache.get(t);
1480
- if (result == undefined) return new Array<ModuleEntry>();
1481
- return result;
1482
- } else {
1483
- const result: ModuleEntry[] = this.entries.filter((v: ModuleEntry) => {
1484
- const r: Record = v as Record;
1485
- return r.type == t;
1486
- });
1487
- if (this.entriesByTypeCache != null)
1488
- this.entriesByTypeCache = new Map<RecordType, ModuleEntry[]>();
1489
- this.entriesByTypeCache?.set(t, result);
1490
- return result;
1491
- }
1566
+ return this.entries.filter((v: ModuleEntry) => {
1567
+ const r: Record = v as Record;
1568
+ return r.type == t;
1569
+ });
1492
1570
  }
1493
1571
 
1494
1572
  getEntityEntries(): Entity[] {
@@ -2142,9 +2220,14 @@ export function parsePrePostWorkflowName(name: string): ThinWfHeader {
2142
2220
  }
2143
2221
 
2144
2222
  function getEntityDef(entityName: string, moduleName: string): Entity | undefined {
2145
- const parts = splitFqName(entityName);
2146
- const mname = parts.hasModule() ? parts.getModuleName() : moduleName;
2147
- return getEntity(parts.getEntryName(), mname);
2223
+ try {
2224
+ const parts = splitFqName(entityName);
2225
+ const mname = parts.hasModule() ? parts.getModuleName() : moduleName;
2226
+ return getEntity(parts.getEntryName(), mname);
2227
+ } catch (reason: any) {
2228
+ logger.error(`getEntityDef: ${reason}`);
2229
+ }
2230
+ return undefined;
2148
2231
  }
2149
2232
 
2150
2233
  export function getWorkflow(eventInstance: Instance): Workflow {
@@ -516,3 +516,14 @@ export function fileExtension(fileName: string): string {
516
516
  }
517
517
  return '';
518
518
  }
519
+
520
+ export function trimQuotes(s: string): string {
521
+ let ss = s.trim();
522
+ if (ss[0] == '"') {
523
+ ss = ss.substring(1);
524
+ }
525
+ if (ss[ss.length - 1] == '"') {
526
+ return ss.substring(0, ss.length - 1);
527
+ }
528
+ return ss;
529
+ }