agentlang 0.3.7 → 0.3.8

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 (73) hide show
  1. package/README.md +20 -16
  2. package/out/extension/main.cjs +250 -250
  3. package/out/extension/main.cjs.map +2 -2
  4. package/out/language/agentlang-validator.d.ts.map +1 -1
  5. package/out/language/agentlang-validator.js +1 -0
  6. package/out/language/agentlang-validator.js.map +1 -1
  7. package/out/language/generated/ast.d.ts +21 -1
  8. package/out/language/generated/ast.d.ts.map +1 -1
  9. package/out/language/generated/ast.js +31 -2
  10. package/out/language/generated/ast.js.map +1 -1
  11. package/out/language/generated/grammar.d.ts.map +1 -1
  12. package/out/language/generated/grammar.js +306 -127
  13. package/out/language/generated/grammar.js.map +1 -1
  14. package/out/language/main.cjs +833 -632
  15. package/out/language/main.cjs.map +3 -3
  16. package/out/language/parser.d.ts +3 -2
  17. package/out/language/parser.d.ts.map +1 -1
  18. package/out/language/parser.js +1 -1
  19. package/out/language/parser.js.map +1 -1
  20. package/out/language/syntax.d.ts +7 -0
  21. package/out/language/syntax.d.ts.map +1 -1
  22. package/out/language/syntax.js +15 -0
  23. package/out/language/syntax.js.map +1 -1
  24. package/out/runtime/agents/common.d.ts +7 -2
  25. package/out/runtime/agents/common.d.ts.map +1 -1
  26. package/out/runtime/agents/common.js +22 -8
  27. package/out/runtime/agents/common.js.map +1 -1
  28. package/out/runtime/auth/cognito.d.ts +2 -0
  29. package/out/runtime/auth/cognito.d.ts.map +1 -1
  30. package/out/runtime/auth/cognito.js +84 -0
  31. package/out/runtime/auth/cognito.js.map +1 -1
  32. package/out/runtime/auth/interface.d.ts +1 -0
  33. package/out/runtime/auth/interface.d.ts.map +1 -1
  34. package/out/runtime/loader.d.ts.map +1 -1
  35. package/out/runtime/loader.js +64 -20
  36. package/out/runtime/loader.js.map +1 -1
  37. package/out/runtime/module.d.ts.map +1 -1
  38. package/out/runtime/module.js +26 -16
  39. package/out/runtime/module.js.map +1 -1
  40. package/out/runtime/modules/ai.d.ts.map +1 -1
  41. package/out/runtime/modules/ai.js +6 -1
  42. package/out/runtime/modules/ai.js.map +1 -1
  43. package/out/runtime/modules/auth.d.ts +1 -0
  44. package/out/runtime/modules/auth.d.ts.map +1 -1
  45. package/out/runtime/modules/auth.js +33 -1
  46. package/out/runtime/modules/auth.js.map +1 -1
  47. package/out/runtime/modules/files.d.ts +18 -0
  48. package/out/runtime/modules/files.d.ts.map +1 -0
  49. package/out/runtime/modules/files.js +116 -0
  50. package/out/runtime/modules/files.js.map +1 -0
  51. package/out/setupClassic.d.ts +98 -0
  52. package/out/setupClassic.d.ts.map +1 -0
  53. package/out/setupClassic.js +38 -0
  54. package/out/setupClassic.js.map +1 -0
  55. package/out/setupCommon.d.ts +2 -0
  56. package/out/setupCommon.d.ts.map +1 -0
  57. package/out/setupCommon.js +33 -0
  58. package/out/setupCommon.js.map +1 -0
  59. package/out/setupExtended.d.ts +40 -0
  60. package/out/setupExtended.d.ts.map +1 -0
  61. package/out/setupExtended.js +67 -0
  62. package/out/setupExtended.js.map +1 -0
  63. package/package.json +1 -1
  64. package/src/language/agentlang-validator.ts +1 -0
  65. package/src/language/agentlang.langium +7 -3
  66. package/src/language/generated/ast.ts +54 -4
  67. package/src/language/generated/grammar.ts +306 -127
  68. package/src/language/parser.ts +1 -1
  69. package/src/language/syntax.ts +21 -0
  70. package/src/runtime/agents/common.ts +30 -9
  71. package/src/runtime/loader.ts +64 -18
  72. package/src/runtime/module.ts +25 -16
  73. package/src/runtime/modules/ai.ts +5 -1
@@ -381,7 +381,7 @@ function introspectForEach(forEach: ForEach): ForEachPattern {
381
381
  return fp;
382
382
  }
383
383
 
384
- function introspectIf(ifpat: If): IfPattern {
384
+ export function introspectIf(ifpat: If): IfPattern {
385
385
  const ifp: IfPattern = new IfPattern(introspectExpression(ifpat.cond));
386
386
  ifpat.statements.forEach((stmt: Statement) => {
387
387
  ifp.addPattern(introspectStatement(stmt));
@@ -670,6 +670,27 @@ export function isIfPattern(p: BasePattern): boolean {
670
670
  return p instanceof IfPattern;
671
671
  }
672
672
 
673
+ export class CasePattern extends BasePattern {
674
+ condition: BasePattern;
675
+ body: BasePattern;
676
+
677
+ constructor(condition: BasePattern, body: BasePattern) {
678
+ super();
679
+ this.condition = condition;
680
+ this.body = body;
681
+ }
682
+
683
+ override toString(): string {
684
+ return `case (${this.condition.toString()}) {
685
+ ${this.body.toString()}
686
+ }`;
687
+ }
688
+ }
689
+
690
+ export function isCasePattern(p: BasePattern): boolean {
691
+ return p instanceof CasePattern;
692
+ }
693
+
673
694
  export function newCreatePattern(recName: string): CrudPattern {
674
695
  const cp: CrudPattern = new CrudPattern(recName);
675
696
  cp.isCreate = true;
@@ -1,3 +1,6 @@
1
+ import { IfPattern } from '../../language/syntax.js';
2
+ import { trimQuotes } from '../util.js';
3
+
1
4
  export const PlannerInstructions = `Agentlang is a very-high-level declarative language that makes it easy to define business applications as 'models'.
2
5
  The model of a business application consists of entity definitions and workflows defined in "modules".
3
6
  A module is be encoded in a syntax inspired by JavaScript and JSON. Example of a simple module follows:
@@ -357,16 +360,22 @@ export type AgentCondition = {
357
360
  if: string;
358
361
  then: string;
359
362
  internal: boolean;
363
+ ifPattern: IfPattern | undefined;
360
364
  };
361
365
 
362
366
  const AgentDirectives = new Map<string, AgentCondition[]>();
363
367
 
364
368
  export function newAgentDirective(
365
369
  cond: string,
366
- then: string,
367
- internal: boolean = false
370
+ then: string = '',
371
+ internal: boolean = false,
372
+ ifPattern: IfPattern | undefined = undefined
368
373
  ): AgentCondition {
369
- return { if: cond, then, internal };
374
+ return { if: cond, then, internal, ifPattern };
375
+ }
376
+
377
+ export function newAgentDirectiveFromIf(ifPattern: IfPattern): AgentCondition {
378
+ return newAgentDirective(ifPattern.toString(), '', false, ifPattern);
370
379
  }
371
380
 
372
381
  export function registerAgentDirectives(agentFqName: string, conds: AgentCondition[]) {
@@ -385,9 +394,13 @@ export function getAgentDirectivesInternal(agentFqName: string): AgentCondition[
385
394
 
386
395
  export function getAgentDirectivesJson(agentFqName: string): string | undefined {
387
396
  const conds = getAgentDirectivesInternal(agentFqName);
388
- if (conds) {
397
+ if (conds && conds.length > 0) {
389
398
  const fmted = conds.map((c: AgentCondition) => {
390
- return { if: c.if, then: c.then };
399
+ if (c.ifPattern) {
400
+ return c.ifPattern.toString();
401
+ } else {
402
+ return { if: c.if, then: c.then };
403
+ }
391
404
  });
392
405
  return JSON.stringify(fmted);
393
406
  }
@@ -408,14 +421,22 @@ export type AgentScenario = {
408
421
  user: string;
409
422
  ai: string;
410
423
  internal: boolean;
424
+ ifPattern: IfPattern | undefined;
411
425
  };
412
426
 
413
427
  export function newAgentScenario(
414
428
  user: string,
415
429
  ai: string,
416
- internal: boolean = false
430
+ internal: boolean = false,
431
+ ifPattern: IfPattern | undefined = undefined
417
432
  ): AgentScenario {
418
- return { user, ai, internal };
433
+ return { user, ai, internal, ifPattern };
434
+ }
435
+
436
+ export function newAgentScenarioFromIf(ifPattern: IfPattern): AgentScenario {
437
+ const user = trimQuotes(ifPattern.condition.toString());
438
+ const ai = trimQuotes(ifPattern.body[0].toString());
439
+ return newAgentScenario(user, ai, false, ifPattern);
419
440
  }
420
441
 
421
442
  const AgentScenarios = new Map<string, AgentScenario[]>();
@@ -430,7 +451,7 @@ export function getAgentScenarios(agentFqName: string): AgentScenario[] | undefi
430
451
 
431
452
  export function getAgentScenariosJson(agentFqName: string): string | undefined {
432
453
  const scns = getAgentScenariosInternal(agentFqName);
433
- if (scns) {
454
+ if (scns && scns.length > 0) {
434
455
  const fmtd = scns.map((scn: AgentScenario) => {
435
456
  return {
436
457
  user: scn.user,
@@ -492,7 +513,7 @@ export function getAgentGlossaryInternal(agentFqName: string): AgentGlossaryEntr
492
513
 
493
514
  export function getAgentGlossaryJson(agentFqName: string): string | undefined {
494
515
  const gls = getAgentGlossaryInternal(agentFqName);
495
- if (gls) {
516
+ if (gls && gls.length > 0) {
496
517
  const fmtd = gls.map((ge: AgentGlossaryEntry) => {
497
518
  return {
498
519
  name: ge.name,
@@ -42,6 +42,8 @@ import {
42
42
  isPublicWorkflowDefinition,
43
43
  isPublicAgentDefinition,
44
44
  isPublicEventDefinition,
45
+ AgentXtraAttribute,
46
+ If,
45
47
  } from '../language/generated/ast.js';
46
48
  import {
47
49
  addEntity,
@@ -80,6 +82,7 @@ import { AstNode, LangiumCoreServices, LangiumDocument } from 'langium';
80
82
  import { isNodeEnv, path } from '../utils/runtime.js';
81
83
  import { CoreModules, registerCoreModules } from './modules/core.js';
82
84
  import {
85
+ introspectIf,
83
86
  maybeGetValidationErrors,
84
87
  maybeRaiseParserErrors,
85
88
  parse,
@@ -528,8 +531,7 @@ function processAgentDirectives(agentName: string, value: Literal): AgentConditi
528
531
  }
529
532
  });
530
533
  if (cond && then) {
531
- const internal = true;
532
- conds?.push({ if: cond, then, internal });
534
+ conds?.push({ if: cond, then, internal: true, ifPattern: undefined });
533
535
  } else {
534
536
  throw new Error(`Invalid condition spec in agent ${agentName}`);
535
537
  }
@@ -560,7 +562,7 @@ function processAgentScenarios(agentName: string, value: Literal): AgentScenario
560
562
  });
561
563
  if (user && ai) {
562
564
  const internal = true;
563
- scenarios.push({ user, ai, internal });
565
+ scenarios.push({ user, ai, internal, ifPattern: undefined });
564
566
  } else {
565
567
  throw new Error(`Invalid glossary spec in agent ${agentName}`);
566
568
  }
@@ -812,17 +814,47 @@ function addDecisionDefinition(def: DecisionDefinition, moduleName: string) {
812
814
  }
813
815
  }
814
816
 
817
+ function agentXtraAttributesAsMap(xtras: AgentXtraAttribute[] | undefined): Map<string, string> {
818
+ const result = new Map<string, string>();
819
+ xtras?.forEach((v: AgentXtraAttribute) => {
820
+ result.set(v.name, v.value);
821
+ });
822
+ return result;
823
+ }
824
+
825
+ function scenarioConditionAsMap(cond: If | undefined) {
826
+ const result = new Map<string, any>();
827
+ if (cond) {
828
+ if (isLiteral(cond.cond)) {
829
+ const s = cond.cond.str;
830
+ if (s === undefined) {
831
+ throw new Error(`scenario condition must be a string - ${cond.cond.$cstNode?.text}`);
832
+ }
833
+ const stmt = cond.statements[0];
834
+ const v = stmt.pattern.$cstNode?.text;
835
+ if (v === undefined) {
836
+ throw new Error(
837
+ `scenario consequent must be a string or name - ${cond.cond.$cstNode?.text}`
838
+ );
839
+ }
840
+ result.set('user', s).set('ai', v).set('if', introspectIf(cond));
841
+ }
842
+ }
843
+ return result;
844
+ }
845
+
815
846
  function addScenarioDefintion(def: ScenarioDefinition, moduleName: string) {
816
- if (def.body) {
847
+ if (def.body || def.scn) {
817
848
  let n = rootRef(def.name);
818
849
  if (!isFqName(n)) {
819
850
  n = makeFqName(moduleName, n);
820
851
  }
821
- const m = asStringLiteralsMap(def.body);
852
+ const m = def.body ? asStringLiteralsMap(def.body) : scenarioConditionAsMap(def.scn);
822
853
  const user = m.get('user');
823
854
  const ai = m.get('ai');
855
+ const ifPattern = m.get('if');
824
856
  if (user && ai) {
825
- const scn = { user: user, ai: ai, internal: false };
857
+ const scn = { user: user, ai: ai, internal: false, ifPattern };
826
858
  addAgentScenario(n, scn);
827
859
  fetchModule(moduleName).addScenario(def.name, scn);
828
860
  } else throw new Error(`scenario ${def.name} requires both user and ai entries`);
@@ -830,30 +862,44 @@ function addScenarioDefintion(def: ScenarioDefinition, moduleName: string) {
830
862
  }
831
863
 
832
864
  function addDirectiveDefintion(def: DirectiveDefinition, moduleName: string) {
833
- if (def.body) {
865
+ if (def.body || def.dir) {
834
866
  let n = rootRef(def.name);
835
867
  if (!isFqName(n)) {
836
868
  n = makeFqName(moduleName, n);
837
869
  }
838
- const m = asStringLiteralsMap(def.body);
839
- const cond = m.get('if');
840
- const then = m.get('then');
841
- if (cond && then) {
842
- const dir = { if: cond, then: then, internal: false };
843
- addAgentDirective(n, dir);
844
- fetchModule(moduleName).addDirective(def.name, dir);
845
- } else throw new Error(`directive ${def.name} requires both if and then entries`);
870
+ if (def.body) {
871
+ const m = asStringLiteralsMap(def.body);
872
+ const cond = m.get('if');
873
+ const then = m.get('then');
874
+ if (cond && then) {
875
+ const dir = { if: cond, then: then, internal: false, ifPattern: undefined };
876
+ addAgentDirective(n, dir);
877
+ fetchModule(moduleName).addDirective(def.name, dir);
878
+ } else throw new Error(`directive ${def.name} requires both if and then entries`);
879
+ } else if (def.dir) {
880
+ const cond = def.dir.$cstNode?.text;
881
+ if (cond) {
882
+ const ifPattern = introspectIf(def.dir);
883
+ const dir = { if: cond, then: '', internal: false, ifPattern };
884
+ addAgentDirective(n, dir);
885
+ fetchModule(moduleName).addDirective(def.name, dir);
886
+ } else {
887
+ throw new Error(`directive ${def.name} requires a valid if expression`);
888
+ }
889
+ }
846
890
  }
847
891
  }
848
892
 
849
893
  function addGlossaryEntryDefintion(def: GlossaryEntryDefinition, moduleName: string) {
850
- if (def.body) {
894
+ if (def.body || def.glos) {
851
895
  let n = rootRef(def.name);
852
896
  if (!isFqName(n)) {
853
897
  n = makeFqName(moduleName, n);
854
898
  }
855
- const m = asStringLiteralsMap(def.body);
856
- const name = m.get('name');
899
+ const m = def.body
900
+ ? asStringLiteralsMap(def.body)
901
+ : agentXtraAttributesAsMap(def.glos?.attributes);
902
+ const name = m.get('name') || m.get('word');
857
903
  const meaning = m.get('meaning');
858
904
  const syn = m.get('synonyms');
859
905
  if (name && meaning) {
@@ -1602,11 +1602,13 @@ export class Scenario extends ModuleEntry {
1602
1602
  }
1603
1603
 
1604
1604
  override toString(): string {
1605
- const obj: any = {
1606
- user: this.def.user,
1607
- ai: this.def.ai,
1608
- };
1609
- return `scenario ${this.name} ${JSON.stringify(obj)}`;
1605
+ if (this.def.ifPattern) {
1606
+ return `scenario ${this.name} {\n ${this.def.ifPattern.toString()}\n}\n`;
1607
+ }
1608
+ const s = `if ("${this.def.user}") {
1609
+ ${this.def.ai}
1610
+ }`;
1611
+ return `scenario ${this.name} {\n ${s}\n}\n`;
1610
1612
  }
1611
1613
  }
1612
1614
 
@@ -1619,11 +1621,17 @@ export class Directive extends ModuleEntry {
1619
1621
  }
1620
1622
 
1621
1623
  override toString(): string {
1622
- const obj: any = {
1623
- if: this.def.if,
1624
- then: this.def.then,
1625
- };
1626
- return `directive ${this.name} ${JSON.stringify(obj)}`;
1624
+ if (this.def.ifPattern) {
1625
+ return `directive ${this.name} {
1626
+ ${this.def.if}
1627
+ }`;
1628
+ } else {
1629
+ const obj: any = {
1630
+ if: this.def.if,
1631
+ then: this.def.then,
1632
+ };
1633
+ return `directive ${this.name} ${JSON.stringify(obj)}`;
1634
+ }
1627
1635
  }
1628
1636
  }
1629
1637
 
@@ -1636,12 +1644,13 @@ export class GlossaryEntry extends ModuleEntry {
1636
1644
  }
1637
1645
 
1638
1646
  override toString(): string {
1639
- const obj: any = {
1640
- name: this.def.name,
1641
- meaning: this.def.meaning,
1642
- synonyms: this.def.synonyms,
1643
- };
1644
- return `glossaryEntry ${this.name} ${JSON.stringify(obj)}`;
1647
+ const ss = new Array<string>();
1648
+ ss.push(` name "${this.def.name}"`);
1649
+ ss.push(` meaning "${this.def.meaning}"`);
1650
+ if (this.def.synonyms) {
1651
+ ss.push(` synonyms "${this.def.synonyms}"`);
1652
+ }
1653
+ return `glossaryEntry ${this.name} \n{\n${ss.join(',\n')}\n}`;
1645
1654
  }
1646
1655
  }
1647
1656
 
@@ -229,7 +229,11 @@ export class AgentInstance {
229
229
  '\nUse the following guidelines to take more accurate decisions in relevant scenarios.\n'
230
230
  );
231
231
  conds.forEach((ac: AgentCondition) => {
232
- ss.push(`if ${ac.if}, then ${ac.then}`);
232
+ if (ac.ifPattern) {
233
+ ss.push(ac.if);
234
+ } else {
235
+ ss.push(`if ${ac.if}, then ${ac.then}`);
236
+ }
233
237
  });
234
238
  return `${ss.join('\n')}\n`;
235
239
  }