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.
- package/out/api/http.d.ts.map +1 -1
- package/out/api/http.js +136 -0
- package/out/api/http.js.map +1 -1
- package/out/language/generated/ast.d.ts +87 -44
- package/out/language/generated/ast.d.ts.map +1 -1
- package/out/language/generated/ast.js +65 -31
- package/out/language/generated/ast.js.map +1 -1
- package/out/language/generated/grammar.d.ts.map +1 -1
- package/out/language/generated/grammar.js +337 -330
- package/out/language/generated/grammar.js.map +1 -1
- package/out/language/main.cjs +396 -361
- package/out/language/main.cjs.map +2 -2
- package/out/language/parser.d.ts +11 -1
- package/out/language/parser.d.ts.map +1 -1
- package/out/language/parser.js +74 -8
- package/out/language/parser.js.map +1 -1
- package/out/language/syntax.d.ts +14 -0
- package/out/language/syntax.d.ts.map +1 -1
- package/out/language/syntax.js +60 -27
- package/out/language/syntax.js.map +1 -1
- package/out/runtime/agents/common.d.ts +2 -2
- package/out/runtime/agents/common.js +1 -1
- package/out/runtime/api.d.ts +2 -0
- package/out/runtime/api.d.ts.map +1 -1
- package/out/runtime/api.js +3 -0
- package/out/runtime/api.js.map +1 -1
- package/out/runtime/datefns.d.ts +34 -0
- package/out/runtime/datefns.d.ts.map +1 -0
- package/out/runtime/datefns.js +82 -0
- package/out/runtime/datefns.js.map +1 -0
- package/out/runtime/exec-graph.d.ts.map +1 -1
- package/out/runtime/exec-graph.js +22 -3
- package/out/runtime/exec-graph.js.map +1 -1
- package/out/runtime/interpreter.d.ts +9 -0
- package/out/runtime/interpreter.d.ts.map +1 -1
- package/out/runtime/interpreter.js +85 -20
- package/out/runtime/interpreter.js.map +1 -1
- package/out/runtime/module.d.ts +8 -0
- package/out/runtime/module.d.ts.map +1 -1
- package/out/runtime/module.js +23 -0
- package/out/runtime/module.js.map +1 -1
- package/out/runtime/modules/ai.d.ts +9 -8
- package/out/runtime/modules/ai.d.ts.map +1 -1
- package/out/runtime/modules/ai.js +92 -37
- package/out/runtime/modules/ai.js.map +1 -1
- package/out/runtime/modules/core.d.ts.map +1 -1
- package/out/runtime/modules/core.js +5 -1
- package/out/runtime/modules/core.js.map +1 -1
- package/out/runtime/monitor.d.ts +6 -0
- package/out/runtime/monitor.d.ts.map +1 -1
- package/out/runtime/monitor.js +21 -1
- package/out/runtime/monitor.js.map +1 -1
- package/out/runtime/relgraph.d.ts.map +1 -1
- package/out/runtime/relgraph.js +7 -3
- package/out/runtime/relgraph.js.map +1 -1
- package/out/runtime/resolvers/interface.d.ts +3 -2
- package/out/runtime/resolvers/interface.d.ts.map +1 -1
- package/out/runtime/resolvers/interface.js +3 -2
- package/out/runtime/resolvers/interface.js.map +1 -1
- package/out/runtime/resolvers/sqldb/dbutil.d.ts.map +1 -1
- package/out/runtime/resolvers/sqldb/dbutil.js +17 -4
- package/out/runtime/resolvers/sqldb/dbutil.js.map +1 -1
- package/out/runtime/resolvers/sqldb/impl.d.ts +1 -1
- package/out/runtime/resolvers/sqldb/impl.d.ts.map +1 -1
- package/out/runtime/resolvers/sqldb/impl.js +17 -7
- package/out/runtime/resolvers/sqldb/impl.js.map +1 -1
- package/out/runtime/services/documentFetcher.js +2 -2
- package/out/runtime/services/documentFetcher.js.map +1 -1
- package/out/runtime/util.d.ts +12 -1
- package/out/runtime/util.d.ts.map +1 -1
- package/out/runtime/util.js +38 -2
- package/out/runtime/util.js.map +1 -1
- package/out/syntaxes/agentlang.monarch.js +1 -1
- package/out/syntaxes/agentlang.monarch.js.map +1 -1
- package/out/test-harness.d.ts +36 -0
- package/out/test-harness.d.ts.map +1 -0
- package/out/test-harness.js +341 -0
- package/out/test-harness.js.map +1 -0
- package/out/utils/fs/index.d.ts +12 -2
- package/out/utils/fs/index.d.ts.map +1 -1
- package/out/utils/fs/index.js +27 -6
- package/out/utils/fs/index.js.map +1 -1
- package/package.json +4 -1
- package/src/api/http.ts +138 -0
- package/src/language/agentlang.langium +9 -10
- package/src/language/generated/ast.ts +98 -44
- package/src/language/generated/grammar.ts +337 -330
- package/src/language/parser.ts +87 -6
- package/src/language/syntax.ts +71 -24
- package/src/runtime/agents/common.ts +1 -1
- package/src/runtime/api.ts +5 -0
- package/src/runtime/datefns.ts +112 -0
- package/src/runtime/exec-graph.ts +23 -2
- package/src/runtime/interpreter.ts +107 -19
- package/src/runtime/module.ts +26 -0
- package/src/runtime/modules/ai.ts +110 -54
- package/src/runtime/modules/core.ts +5 -1
- package/src/runtime/monitor.ts +27 -1
- package/src/runtime/relgraph.ts +7 -3
- package/src/runtime/resolvers/interface.ts +4 -2
- package/src/runtime/resolvers/sqldb/dbutil.ts +20 -6
- package/src/runtime/resolvers/sqldb/impl.ts +17 -7
- package/src/runtime/services/documentFetcher.ts +2 -2
- package/src/runtime/util.ts +62 -2
- package/src/syntaxes/agentlang.monarch.ts +1 -1
- package/src/test-harness.ts +423 -0
- package/src/utils/fs/index.ts +30 -6
package/src/language/parser.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
ForEach,
|
|
9
9
|
FullTextSearch,
|
|
10
10
|
Group,
|
|
11
|
+
GroupByClause,
|
|
11
12
|
Handler,
|
|
12
13
|
If,
|
|
13
14
|
isExpr,
|
|
@@ -24,17 +25,27 @@ import {
|
|
|
24
25
|
ModuleDefinition,
|
|
25
26
|
NegExpr,
|
|
26
27
|
NotExpr,
|
|
28
|
+
OrderByClause,
|
|
27
29
|
Pattern,
|
|
28
30
|
PrimExpr,
|
|
31
|
+
QueryOption,
|
|
29
32
|
RelationshipPattern,
|
|
30
33
|
Return,
|
|
31
34
|
SelectIntoEntry,
|
|
32
35
|
SelectIntoSpec,
|
|
33
36
|
SetAttribute,
|
|
34
37
|
Statement,
|
|
38
|
+
WhereSpec,
|
|
39
|
+
WhereSpecClause,
|
|
35
40
|
WorkflowDefinition,
|
|
36
41
|
} from './generated/ast.js';
|
|
37
|
-
import {
|
|
42
|
+
import {
|
|
43
|
+
firstAliasSpec,
|
|
44
|
+
firstCatchSpec,
|
|
45
|
+
firstEmptySpec,
|
|
46
|
+
isString,
|
|
47
|
+
QuerySuffix,
|
|
48
|
+
} from '../runtime/util.js';
|
|
38
49
|
import {
|
|
39
50
|
BasePattern,
|
|
40
51
|
CrudPattern,
|
|
@@ -50,6 +61,7 @@ import {
|
|
|
50
61
|
NegExpressionPattern,
|
|
51
62
|
NotExpressionPattern,
|
|
52
63
|
ReturnPattern,
|
|
64
|
+
WhereSpecClausePattern,
|
|
53
65
|
} from './syntax.js';
|
|
54
66
|
|
|
55
67
|
let nextDocumentId = 1;
|
|
@@ -258,6 +270,10 @@ function introspectStatement(stmt: Statement): BasePattern {
|
|
|
258
270
|
r.addHandler(h.except, introspectStatement(h.stmt));
|
|
259
271
|
});
|
|
260
272
|
}
|
|
273
|
+
const emptySpec = firstEmptySpec(stmt);
|
|
274
|
+
if (emptySpec) {
|
|
275
|
+
r.setEmptyHandler(introspectStatement(emptySpec.stmt));
|
|
276
|
+
}
|
|
261
277
|
return r;
|
|
262
278
|
}
|
|
263
279
|
|
|
@@ -269,8 +285,9 @@ function introspectPattern(pat: Pattern): BasePattern {
|
|
|
269
285
|
} else {
|
|
270
286
|
r = introspectCreatePattern(pat.crudMap);
|
|
271
287
|
}
|
|
272
|
-
|
|
273
|
-
|
|
288
|
+
const opts = extractQueryOptions(pat.crudMap);
|
|
289
|
+
if (opts.into) {
|
|
290
|
+
r = introspectInto(opts.into, r as CrudPattern);
|
|
274
291
|
}
|
|
275
292
|
} else if (pat.expr) {
|
|
276
293
|
r = introspectExpression(pat.expr);
|
|
@@ -294,6 +311,14 @@ function introspectPattern(pat: Pattern): BasePattern {
|
|
|
294
311
|
function introspectInto(intoSpec: SelectIntoSpec, p: CrudPattern): CrudPattern {
|
|
295
312
|
intoSpec.entries.forEach((se: SelectIntoEntry) => {
|
|
296
313
|
if (se.attribute) p.addInto(se.alias, se.attribute);
|
|
314
|
+
else if (se.aggregate) {
|
|
315
|
+
const args = se.aggregate.args
|
|
316
|
+
.map((s: string) => {
|
|
317
|
+
return s;
|
|
318
|
+
})
|
|
319
|
+
.join(', ');
|
|
320
|
+
p.addInto(se.alias, `@${se.aggregate?.name}(${args})`);
|
|
321
|
+
}
|
|
297
322
|
});
|
|
298
323
|
return p;
|
|
299
324
|
}
|
|
@@ -360,10 +385,11 @@ function introspectQueryPattern(crudMap: CrudMap): CrudPattern {
|
|
|
360
385
|
crudMap.body?.attributes.forEach((sa: SetAttribute) => {
|
|
361
386
|
cp.addAttribute(sa.name, introspectExpression(sa.value), sa.op);
|
|
362
387
|
});
|
|
363
|
-
|
|
388
|
+
const opts = extractQueryOptions(crudMap);
|
|
389
|
+
crudMap.relationships?.forEach((rp: RelationshipPattern) => {
|
|
364
390
|
cp.addRelationship(rp.name, introspectPattern(rp.pattern) as CrudPattern | CrudPattern[]);
|
|
365
391
|
});
|
|
366
|
-
|
|
392
|
+
opts.joins?.forEach((js: JoinSpec) => {
|
|
367
393
|
const jp: JoinPattern = {
|
|
368
394
|
type: js.type,
|
|
369
395
|
targetEntity: js.name,
|
|
@@ -373,6 +399,18 @@ function introspectQueryPattern(crudMap: CrudMap): CrudPattern {
|
|
|
373
399
|
};
|
|
374
400
|
cp.joins.push(jp);
|
|
375
401
|
});
|
|
402
|
+
if (opts.where?.clauses) {
|
|
403
|
+
cp.where = new Array<WhereSpecClausePattern>();
|
|
404
|
+
opts.where.clauses.forEach((wc: WhereSpecClause) => {
|
|
405
|
+
cp.where?.push({ lhs: wc.lhs, op: wc.op || '=', rhs: wc.rhs.$cstNode?.text || '' });
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
if (opts.groupByClause) {
|
|
409
|
+
cp.groupBy = opts.groupByClause.colNames;
|
|
410
|
+
}
|
|
411
|
+
if (opts.orderByClause) {
|
|
412
|
+
cp.orderBy = opts.orderByClause.colNames;
|
|
413
|
+
}
|
|
376
414
|
cp.isCreate = false;
|
|
377
415
|
cp.isQueryUpdate = false;
|
|
378
416
|
cp.isQuery = true;
|
|
@@ -381,6 +419,49 @@ function introspectQueryPattern(crudMap: CrudMap): CrudPattern {
|
|
|
381
419
|
throw new Error(`Failed to introspect query-pattern: ${crudMap}`);
|
|
382
420
|
}
|
|
383
421
|
|
|
422
|
+
export type ExtractedQueryOptions = {
|
|
423
|
+
joins: JoinSpec[] | undefined;
|
|
424
|
+
into: SelectIntoSpec | undefined;
|
|
425
|
+
where: WhereSpec | undefined;
|
|
426
|
+
groupByClause: GroupByClause | undefined;
|
|
427
|
+
orderByClause: OrderByClause | undefined;
|
|
428
|
+
upsert: '@upsert' | undefined;
|
|
429
|
+
distinct: '@distinct' | undefined;
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
export function extractQueryOptions(crudMap: CrudMap): ExtractedQueryOptions {
|
|
433
|
+
const r: ExtractedQueryOptions = {
|
|
434
|
+
joins: undefined,
|
|
435
|
+
into: undefined,
|
|
436
|
+
where: undefined,
|
|
437
|
+
groupByClause: undefined,
|
|
438
|
+
orderByClause: undefined,
|
|
439
|
+
upsert: undefined,
|
|
440
|
+
distinct: undefined,
|
|
441
|
+
};
|
|
442
|
+
crudMap.queryOptions.forEach((qo: QueryOption) => {
|
|
443
|
+
if (qo.join) {
|
|
444
|
+
if (r.joins === undefined) {
|
|
445
|
+
r.joins = new Array<JoinSpec>();
|
|
446
|
+
}
|
|
447
|
+
r.joins.push(qo.join);
|
|
448
|
+
} else if (qo.into) {
|
|
449
|
+
r.into = qo.into;
|
|
450
|
+
} else if (qo.where) {
|
|
451
|
+
r.where = qo.where;
|
|
452
|
+
} else if (qo.groupByClause) {
|
|
453
|
+
r.groupByClause = qo.groupByClause;
|
|
454
|
+
} else if (qo.orderByClause) {
|
|
455
|
+
r.orderByClause = qo.orderByClause;
|
|
456
|
+
} else if (qo.upsert) {
|
|
457
|
+
r.upsert = qo.upsert;
|
|
458
|
+
} else if (qo.distinct) {
|
|
459
|
+
r.distinct = qo.distinct;
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
return r;
|
|
463
|
+
}
|
|
464
|
+
|
|
384
465
|
function introspectCreatePattern(crudMap: CrudMap): CrudPattern {
|
|
385
466
|
if (crudMap) {
|
|
386
467
|
const cp: CrudPattern = new CrudPattern(crudMap.name);
|
|
@@ -393,7 +474,7 @@ function introspectCreatePattern(crudMap: CrudMap): CrudPattern {
|
|
|
393
474
|
}
|
|
394
475
|
cp.addAttribute(sa.name, introspectExpression(sa.value), sa.op);
|
|
395
476
|
});
|
|
396
|
-
crudMap.relationships
|
|
477
|
+
crudMap.relationships?.forEach((rp: RelationshipPattern) => {
|
|
397
478
|
cp.addRelationship(rp.name, introspectPattern(rp.pattern) as CrudPattern | CrudPattern[]);
|
|
398
479
|
});
|
|
399
480
|
cp.isQueryUpdate = qup;
|
package/src/language/syntax.ts
CHANGED
|
@@ -15,6 +15,7 @@ export class BasePattern {
|
|
|
15
15
|
alias: string | undefined;
|
|
16
16
|
aliases: string[] | undefined;
|
|
17
17
|
handlers: Map<string, BasePattern> | undefined;
|
|
18
|
+
emptyHandler: BasePattern | undefined;
|
|
18
19
|
|
|
19
20
|
setAlias(alias: string) {
|
|
20
21
|
this.alias = alias;
|
|
@@ -44,6 +45,11 @@ export class BasePattern {
|
|
|
44
45
|
this.handlers.set(k, handler);
|
|
45
46
|
}
|
|
46
47
|
|
|
48
|
+
setEmptyHandler(handler: BasePattern) {
|
|
49
|
+
this.emptyHandler = handler;
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
|
|
47
53
|
private aliasesAsString(): string | undefined {
|
|
48
54
|
if (this.alias) {
|
|
49
55
|
return ` @as ${this.alias}`;
|
|
@@ -54,24 +60,30 @@ export class BasePattern {
|
|
|
54
60
|
}
|
|
55
61
|
}
|
|
56
62
|
|
|
63
|
+
private emptyHandlerAsString(): string | undefined {
|
|
64
|
+
if (this.emptyHandler) {
|
|
65
|
+
return ` @empty ${this.emptyHandler.toString()}`;
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
57
70
|
private handlersAsString(): string | undefined {
|
|
58
|
-
if (this.handlers) {
|
|
59
|
-
let s = '{';
|
|
71
|
+
if (this.handlers && this.handlers.size > 0) {
|
|
72
|
+
let s = ' @catch {';
|
|
60
73
|
this.handlers.forEach((handler: BasePattern, k: string) => {
|
|
61
|
-
s = `${s}
|
|
74
|
+
s = `${s}${k} ${handler.toString()} `;
|
|
62
75
|
});
|
|
63
|
-
return s + '}';
|
|
64
|
-
} else {
|
|
65
|
-
return undefined;
|
|
76
|
+
return s.trimEnd() + '}';
|
|
66
77
|
}
|
|
78
|
+
return undefined;
|
|
67
79
|
}
|
|
80
|
+
|
|
68
81
|
hintsAsString(): string {
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return `${a}\n${h}`;
|
|
82
|
+
// Order matters: @as, then @catch, then @empty (must be last because it's greedy in the grammar)
|
|
83
|
+
const a = this.aliasesAsString() ?? '';
|
|
84
|
+
const h = this.handlersAsString() ?? '';
|
|
85
|
+
const e = this.emptyHandlerAsString() ?? '';
|
|
86
|
+
return `${a}${h}${e}`;
|
|
75
87
|
}
|
|
76
88
|
|
|
77
89
|
toString(): string {
|
|
@@ -386,12 +398,21 @@ function joinPatternToString(jp: JoinPattern): string {
|
|
|
386
398
|
return `${jp.type} ${jp.targetEntity} {${jp.conditionLhs}${opr} ${jp.conditionRhs}}`;
|
|
387
399
|
}
|
|
388
400
|
|
|
401
|
+
export type WhereSpecClausePattern = {
|
|
402
|
+
lhs: string;
|
|
403
|
+
op: string;
|
|
404
|
+
rhs: string;
|
|
405
|
+
};
|
|
406
|
+
|
|
389
407
|
export class CrudPattern extends BasePattern {
|
|
390
408
|
recordName: string;
|
|
391
409
|
attributes: Array<AttributePattern>;
|
|
392
410
|
relationships: Map<string, CrudPattern[] | CrudPattern> | undefined;
|
|
393
411
|
joins: JoinPattern[];
|
|
394
412
|
into: Map<string, string> | undefined;
|
|
413
|
+
where: WhereSpecClausePattern[] | undefined;
|
|
414
|
+
groupBy: string[] | undefined;
|
|
415
|
+
orderBy: string[] | undefined;
|
|
395
416
|
isQuery: boolean = false;
|
|
396
417
|
isQueryUpdate: boolean = false;
|
|
397
418
|
isCreate: boolean = false;
|
|
@@ -521,15 +542,29 @@ export class CrudPattern extends BasePattern {
|
|
|
521
542
|
return escapeQueryName(this.recordName);
|
|
522
543
|
}
|
|
523
544
|
|
|
524
|
-
private intoAsString(): string
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
545
|
+
private intoAsString(): string {
|
|
546
|
+
const ss = new Array<string>();
|
|
547
|
+
this.into?.forEach((attr: string, alias: string) => {
|
|
548
|
+
ss.push(`${alias} ${attr}`);
|
|
549
|
+
});
|
|
550
|
+
return `@into { ${ss.join(',\n')} }`;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
private whereAsString(): string {
|
|
554
|
+
const ss = new Array<string>();
|
|
555
|
+
this.where?.forEach((wc: WhereSpecClausePattern) => {
|
|
556
|
+
if (wc.op == '=') ss.push(`${wc.lhs} ${wc.rhs}`);
|
|
557
|
+
else ss.push(`${wc.lhs} ${wc.op} ${wc.rhs}`);
|
|
558
|
+
});
|
|
559
|
+
return `@where {${ss.join(`,\n`)}}`;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
private groupByAsString(): string {
|
|
563
|
+
return `@groupBy(${this.groupBy?.join(', ')})`;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
private orderByAsString(): string {
|
|
567
|
+
return `@orderBy(${this.orderBy?.join(', ')})`;
|
|
533
568
|
}
|
|
534
569
|
|
|
535
570
|
override toString(): string {
|
|
@@ -542,10 +577,22 @@ export class CrudPattern extends BasePattern {
|
|
|
542
577
|
const js = this.joins.map(joinPatternToString);
|
|
543
578
|
s = s.concat(`,\n${js.join(',\n')}`);
|
|
544
579
|
}
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
580
|
+
if (this.into) {
|
|
581
|
+
const ins = this.intoAsString();
|
|
582
|
+
if (ins) {
|
|
583
|
+
s = s.concat(`,\n${ins}`);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
if (this.where) {
|
|
587
|
+
s = s.concat(`,\n${this.whereAsString()}`);
|
|
548
588
|
}
|
|
589
|
+
if (this.groupBy) {
|
|
590
|
+
s = s.concat(`,\n${this.groupByAsString()}`);
|
|
591
|
+
}
|
|
592
|
+
if (this.orderBy) {
|
|
593
|
+
s = s.concat(`,\n${this.orderByAsString()}`);
|
|
594
|
+
}
|
|
595
|
+
|
|
549
596
|
return s.concat('}', this.hintsAsString());
|
|
550
597
|
}
|
|
551
598
|
}
|
|
@@ -209,7 +209,7 @@ A pattern may execute asynchronously and its eventual result can be handled by p
|
|
|
209
209
|
If you are instructed that a particular event will be called asynchronously, always provide the patterns that follows in its '@then' clause. You must add the
|
|
210
210
|
'@then' clause only if an event's documentation or instruction explicitly requires to do so.
|
|
211
211
|
|
|
212
|
-
Earlier we discussed
|
|
212
|
+
Earlier we discussed the concept of 'between' relationship. Here's the 'CreateEmployee' workflow updated to create the Employee with the his/her Profile attached:
|
|
213
213
|
|
|
214
214
|
workflow CreateEmployee {
|
|
215
215
|
{Erp/Employee {firstName CreateEmployee.firstName,
|
package/src/runtime/api.ts
CHANGED
|
@@ -5,9 +5,11 @@ import {
|
|
|
5
5
|
} from './module.js';
|
|
6
6
|
import { getLocalEnv as al_getLocalEnv, setLocalEnv as al_setLocalEnv } from './auth/defs.js';
|
|
7
7
|
import { now } from './util.js';
|
|
8
|
+
import { initDateFns } from './datefns.js';
|
|
8
9
|
|
|
9
10
|
declare global {
|
|
10
11
|
var agentlang: any | undefined;
|
|
12
|
+
var dateFns: ReturnType<typeof initDateFns> | undefined;
|
|
11
13
|
function getLocalEnv(k: string, defaultValue?: string): string | undefined;
|
|
12
14
|
function setLocalEnv(k: string, v: string): string;
|
|
13
15
|
function uuid(): string;
|
|
@@ -32,6 +34,9 @@ export function initGlobalApi() {
|
|
|
32
34
|
globalThis.getLocalEnv = al_getLocalEnv;
|
|
33
35
|
globalThis.setLocalEnv = al_setLocalEnv;
|
|
34
36
|
|
|
37
|
+
// Expose date-fns functions globally as dateFns.*
|
|
38
|
+
globalThis.dateFns = initDateFns();
|
|
39
|
+
|
|
35
40
|
ApiInited = true;
|
|
36
41
|
}
|
|
37
42
|
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addDays as _addDays,
|
|
3
|
+
addWeeks as _addWeeks,
|
|
4
|
+
addMonths as _addMonths,
|
|
5
|
+
startOfWeek as _startOfWeek,
|
|
6
|
+
endOfWeek as _endOfWeek,
|
|
7
|
+
startOfMonth as _startOfMonth,
|
|
8
|
+
endOfMonth as _endOfMonth,
|
|
9
|
+
getISOWeek,
|
|
10
|
+
format,
|
|
11
|
+
parse,
|
|
12
|
+
differenceInDays,
|
|
13
|
+
differenceInWeeks,
|
|
14
|
+
} from 'date-fns';
|
|
15
|
+
import { formatInTimeZone } from 'date-fns-tz';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Parse a date string as local midnight to avoid timezone day-shift issues.
|
|
19
|
+
* date-fns operates in local time, so we must parse date-only strings
|
|
20
|
+
* (YYYY-MM-DD) as local midnight rather than UTC midnight.
|
|
21
|
+
*/
|
|
22
|
+
function toDate(dateStr: string): Date {
|
|
23
|
+
if (!dateStr.includes('T')) {
|
|
24
|
+
const [y, m, d] = dateStr.split('-').map(Number);
|
|
25
|
+
return new Date(y, m - 1, d);
|
|
26
|
+
}
|
|
27
|
+
return new Date(dateStr);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function toDateStr(d: Date): string {
|
|
31
|
+
return format(d, 'yyyy-MM-dd');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function addDays(dateStr: string, days: number): string {
|
|
35
|
+
return toDateStr(_addDays(toDate(dateStr), days));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function addWeeks(dateStr: string, weeks: number): string {
|
|
39
|
+
return toDateStr(_addWeeks(toDate(dateStr), weeks));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function addMonths(dateStr: string, months: number): string {
|
|
43
|
+
return toDateStr(_addMonths(toDate(dateStr), months));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function startOfWeek(dateStr: string): string {
|
|
47
|
+
return toDateStr(_startOfWeek(toDate(dateStr), { weekStartsOn: 1 }));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function endOfWeek(dateStr: string): string {
|
|
51
|
+
return toDateStr(_endOfWeek(toDate(dateStr), { weekStartsOn: 1 }));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function startOfMonth(dateStr: string): string {
|
|
55
|
+
return toDateStr(_startOfMonth(toDate(dateStr)));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function endOfMonth(dateStr: string): string {
|
|
59
|
+
return toDateStr(_endOfMonth(toDate(dateStr)));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function getWeek(dateStr: string): number {
|
|
63
|
+
return getISOWeek(toDate(dateStr));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function dayName(dateStr: string): string {
|
|
67
|
+
return format(toDate(dateStr), 'EEEE');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function formatDate(dateStr: string, fmt: string): string {
|
|
71
|
+
return format(toDate(dateStr), fmt);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function parseDate(str: string, fmt: string): string {
|
|
75
|
+
return toDateStr(parse(str, fmt, new Date()));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function diffInDays(dateStr1: string, dateStr2: string): number {
|
|
79
|
+
return differenceInDays(toDate(dateStr1), toDate(dateStr2));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function diffInWeeks(dateStr1: string, dateStr2: string): number {
|
|
83
|
+
return differenceInWeeks(toDate(dateStr1), toDate(dateStr2));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function today(): string {
|
|
87
|
+
return toDateStr(new Date());
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function toTimezone(dateStr: string, tz: string): string {
|
|
91
|
+
return formatInTimeZone(new Date(dateStr), tz, 'yyyy-MM-dd HH:mm:ssXXX');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function initDateFns() {
|
|
95
|
+
return {
|
|
96
|
+
addDays,
|
|
97
|
+
addWeeks,
|
|
98
|
+
addMonths,
|
|
99
|
+
startOfWeek,
|
|
100
|
+
endOfWeek,
|
|
101
|
+
startOfMonth,
|
|
102
|
+
endOfMonth,
|
|
103
|
+
getWeek,
|
|
104
|
+
dayName,
|
|
105
|
+
formatDate,
|
|
106
|
+
parseDate,
|
|
107
|
+
diffInDays,
|
|
108
|
+
diffInWeeks,
|
|
109
|
+
today,
|
|
110
|
+
toTimezone,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
@@ -304,8 +304,8 @@ export async function executeGraph(execGraph: ExecGraph, env: Environment): Prom
|
|
|
304
304
|
throw reason;
|
|
305
305
|
} finally {
|
|
306
306
|
if (monitoringEnabled) {
|
|
307
|
-
if (monitorIncr) env.decrementMonitor();
|
|
308
307
|
env.setMonitorEntryResult(env.getLastResult());
|
|
308
|
+
if (monitorIncr) env.decrementMonitor();
|
|
309
309
|
}
|
|
310
310
|
}
|
|
311
311
|
}
|
|
@@ -425,10 +425,28 @@ export async function executeEvent(
|
|
|
425
425
|
env.setActiveEvent(eventInstance);
|
|
426
426
|
await executeEventHelper(eventInstance, env);
|
|
427
427
|
} else if (isAgentEventInstance(eventInstance)) {
|
|
428
|
+
env.setActiveEvent(eventInstance);
|
|
429
|
+
if (isMonitoringEnabled()) {
|
|
430
|
+
env.appendEntryToMonitor(
|
|
431
|
+
`{${eventInstance.getFqName()} {message "${eventInstance.lookup('message')}"}}`
|
|
432
|
+
);
|
|
433
|
+
}
|
|
428
434
|
await handleAgentInvocation(eventInstance, env);
|
|
435
|
+
if (isMonitoringEnabled()) {
|
|
436
|
+
env.setMonitorEntryResult(env.getLastResult());
|
|
437
|
+
}
|
|
429
438
|
}
|
|
430
439
|
const r = env.getLastResult();
|
|
431
|
-
if (continuation)
|
|
440
|
+
if (continuation) {
|
|
441
|
+
if (env.getLastPattern() && isAgentEventInstance(eventInstance)) {
|
|
442
|
+
continuation({
|
|
443
|
+
result: r.map((res: any) => (res.attributes ? Object.fromEntries(res.attributes) : res)),
|
|
444
|
+
pattern: env.getLastPattern(),
|
|
445
|
+
});
|
|
446
|
+
} else {
|
|
447
|
+
continuation(r);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
432
450
|
return r;
|
|
433
451
|
} catch (err) {
|
|
434
452
|
if (env && env.hasHandlers()) {
|
|
@@ -490,6 +508,9 @@ export async function executeEventHelper(eventInstance: Instance, env?: Environm
|
|
|
490
508
|
);
|
|
491
509
|
}
|
|
492
510
|
await handleAgentInvocation(eventInstance, env);
|
|
511
|
+
if (isMonitoringEnabled()) {
|
|
512
|
+
env.setMonitorEntryResult(env.getLastResult());
|
|
513
|
+
}
|
|
493
514
|
}
|
|
494
515
|
if (isLocalEnv) {
|
|
495
516
|
await env.commitAllTransactions();
|