agentlang 0.0.3 → 0.0.5

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 (122) hide show
  1. package/README.md +16 -47
  2. package/out/api/http.d.ts.map +1 -1
  3. package/out/api/http.js +39 -19
  4. package/out/api/http.js.map +1 -1
  5. package/out/cli/main.d.ts +1 -3
  6. package/out/cli/main.d.ts.map +1 -1
  7. package/out/cli/main.js +6 -12
  8. package/out/cli/main.js.map +1 -1
  9. package/out/language/generated/ast.d.ts +98 -19
  10. package/out/language/generated/ast.d.ts.map +1 -1
  11. package/out/language/generated/ast.js +144 -26
  12. package/out/language/generated/ast.js.map +1 -1
  13. package/out/language/generated/grammar.d.ts +1 -1
  14. package/out/language/generated/grammar.d.ts.map +1 -1
  15. package/out/language/generated/grammar.js +1000 -305
  16. package/out/language/generated/grammar.js.map +1 -1
  17. package/out/language/generated/module.d.ts +1 -1
  18. package/out/language/generated/module.js +1 -1
  19. package/out/language/main.cjs +1097 -312
  20. package/out/language/main.cjs.map +2 -2
  21. package/out/language/parser.js +13 -6
  22. package/out/language/parser.js.map +1 -1
  23. package/out/runtime/agents/common.d.ts +1 -1
  24. package/out/runtime/agents/common.d.ts.map +1 -1
  25. package/out/runtime/agents/common.js +1 -1
  26. package/out/runtime/auth/cognito.d.ts +4 -1
  27. package/out/runtime/auth/cognito.d.ts.map +1 -1
  28. package/out/runtime/auth/cognito.js +540 -73
  29. package/out/runtime/auth/cognito.js.map +1 -1
  30. package/out/runtime/auth/defs.d.ts +3 -0
  31. package/out/runtime/auth/defs.d.ts.map +1 -1
  32. package/out/runtime/auth/defs.js +17 -1
  33. package/out/runtime/auth/defs.js.map +1 -1
  34. package/out/runtime/auth/interface.d.ts +6 -1
  35. package/out/runtime/auth/interface.d.ts.map +1 -1
  36. package/out/runtime/defs.d.ts +21 -0
  37. package/out/runtime/defs.d.ts.map +1 -1
  38. package/out/runtime/defs.js +35 -0
  39. package/out/runtime/defs.js.map +1 -1
  40. package/out/runtime/interpreter.d.ts.map +1 -1
  41. package/out/runtime/interpreter.js +45 -36
  42. package/out/runtime/interpreter.js.map +1 -1
  43. package/out/runtime/loader.d.ts +4 -2
  44. package/out/runtime/loader.d.ts.map +1 -1
  45. package/out/runtime/loader.js +164 -29
  46. package/out/runtime/loader.js.map +1 -1
  47. package/out/runtime/module.d.ts +49 -5
  48. package/out/runtime/module.d.ts.map +1 -1
  49. package/out/runtime/module.js +214 -9
  50. package/out/runtime/module.js.map +1 -1
  51. package/out/runtime/modules/ai.d.ts +7 -5
  52. package/out/runtime/modules/ai.d.ts.map +1 -1
  53. package/out/runtime/modules/ai.js +50 -24
  54. package/out/runtime/modules/ai.js.map +1 -1
  55. package/out/runtime/modules/auth.d.ts +17 -1
  56. package/out/runtime/modules/auth.d.ts.map +1 -1
  57. package/out/runtime/modules/auth.js +295 -32
  58. package/out/runtime/modules/auth.js.map +1 -1
  59. package/out/runtime/modules/core.d.ts.map +1 -1
  60. package/out/runtime/modules/core.js +3 -1
  61. package/out/runtime/modules/core.js.map +1 -1
  62. package/out/runtime/relgraph.d.ts.map +1 -1
  63. package/out/runtime/relgraph.js +2 -2
  64. package/out/runtime/relgraph.js.map +1 -1
  65. package/out/runtime/resolvers/interface.d.ts +37 -2
  66. package/out/runtime/resolvers/interface.d.ts.map +1 -1
  67. package/out/runtime/resolvers/interface.js +103 -5
  68. package/out/runtime/resolvers/interface.js.map +1 -1
  69. package/out/runtime/resolvers/registry.d.ts +3 -2
  70. package/out/runtime/resolvers/registry.d.ts.map +1 -1
  71. package/out/runtime/resolvers/registry.js +3 -0
  72. package/out/runtime/resolvers/registry.js.map +1 -1
  73. package/out/runtime/resolvers/sqldb/database.d.ts +4 -2
  74. package/out/runtime/resolvers/sqldb/database.d.ts.map +1 -1
  75. package/out/runtime/resolvers/sqldb/database.js +43 -4
  76. package/out/runtime/resolvers/sqldb/database.js.map +1 -1
  77. package/out/runtime/resolvers/sqldb/impl.d.ts.map +1 -1
  78. package/out/runtime/resolvers/sqldb/impl.js +7 -3
  79. package/out/runtime/resolvers/sqldb/impl.js.map +1 -1
  80. package/out/runtime/state.d.ts +31 -3
  81. package/out/runtime/state.d.ts.map +1 -1
  82. package/out/runtime/state.js +11 -1
  83. package/out/runtime/state.js.map +1 -1
  84. package/out/runtime/util.d.ts +5 -0
  85. package/out/runtime/util.d.ts.map +1 -1
  86. package/out/runtime/util.js +27 -0
  87. package/out/runtime/util.js.map +1 -1
  88. package/out/syntaxes/agentlang.monarch.js +2 -2
  89. package/out/syntaxes/agentlang.monarch.js.map +1 -1
  90. package/out/utils/http.d.ts +2 -0
  91. package/out/utils/http.d.ts.map +1 -0
  92. package/out/utils/http.js +5 -0
  93. package/out/utils/http.js.map +1 -0
  94. package/package.json +8 -6
  95. package/src/api/http.ts +41 -17
  96. package/src/cli/main.ts +6 -12
  97. package/src/language/agentlang.langium +35 -10
  98. package/src/language/generated/ast.ts +257 -45
  99. package/src/language/generated/grammar.ts +1000 -305
  100. package/src/language/generated/module.ts +1 -1
  101. package/src/language/parser.ts +12 -8
  102. package/src/runtime/agents/common.ts +1 -1
  103. package/src/runtime/auth/cognito.ts +605 -74
  104. package/src/runtime/auth/defs.ts +17 -1
  105. package/src/runtime/auth/interface.ts +6 -1
  106. package/src/runtime/defs.ts +45 -0
  107. package/src/runtime/interpreter.ts +43 -34
  108. package/src/runtime/loader.ts +172 -30
  109. package/src/runtime/module.ts +257 -10
  110. package/src/runtime/modules/ai.ts +52 -28
  111. package/src/runtime/modules/auth.ts +343 -40
  112. package/src/runtime/modules/core.ts +3 -1
  113. package/src/runtime/relgraph.ts +2 -8
  114. package/src/runtime/resolvers/interface.ts +141 -6
  115. package/src/runtime/resolvers/registry.ts +5 -2
  116. package/src/runtime/resolvers/sqldb/database.ts +55 -4
  117. package/src/runtime/resolvers/sqldb/impl.ts +8 -7
  118. package/src/runtime/state.ts +11 -1
  119. package/src/runtime/util.ts +29 -0
  120. package/src/syntaxes/agentlang.monarch.ts +2 -2
  121. package/src/utils/http.ts +5 -0
  122. package/src/index.ts +0 -29
@@ -16,6 +16,9 @@ import {
16
16
  PrePostTriggerDefinition,
17
17
  TriggerEntry,
18
18
  Expr,
19
+ RbacSpecEntry,
20
+ RbacSpecEntries,
21
+ RbacOpr,
19
22
  } from '../language/generated/ast.js';
20
23
  import {
21
24
  Path,
@@ -26,6 +29,7 @@ import {
26
29
  isFqName,
27
30
  makeFqName,
28
31
  DefaultModuleName,
32
+ DefaultModules,
29
33
  joinStatements,
30
34
  isMinusZero,
31
35
  now,
@@ -97,7 +101,7 @@ function recordSchemaToString(scm: RecordSchema): string {
97
101
  ss.push(` ${n} ${attributeSpecToString(attrSpec)}`);
98
102
  }
99
103
  });
100
- return `{ \n${ss.join(',\n')} \n}`;
104
+ return `\n${ss.join(',\n')} \n`;
101
105
  }
102
106
 
103
107
  function attributeSpecToString(attrSpec: AttributeSpec): string {
@@ -105,8 +109,8 @@ function attributeSpecToString(attrSpec: AttributeSpec): string {
105
109
  if (attrSpec.properties) {
106
110
  const ps: Array<string> = [];
107
111
  attrSpec.properties.forEach((v: any, k: string) => {
108
- if (v == true) ps.push(`@${k}`);
109
- else ps.push(`@${k}${v}`);
112
+ if (v == true) ps.push(` @${k}`);
113
+ else ps.push(` @${k}(${v})`);
110
114
  });
111
115
  s = s.concat(ps.join(' '));
112
116
  }
@@ -128,6 +132,7 @@ export enum RecordType {
128
132
  ENTITY,
129
133
  EVENT,
130
134
  RELATIONSHIP,
135
+ AGENT,
131
136
  }
132
137
 
133
138
  function normalizeMetaValue(metaValue: any): any {
@@ -181,6 +186,7 @@ export class Record extends ModuleEntry {
181
186
  beforeTriggers: Map<CrudType, TriggerInfo> | undefined;
182
187
  compositeUqAttributes: Array<string> | undefined;
183
188
  oneOfRefAttributes: Array<string> | undefined;
189
+ protected rbac: RbacSpecification[] | undefined;
184
190
 
185
191
  constructor(
186
192
  name: string,
@@ -413,8 +419,14 @@ export class Record extends ModuleEntry {
413
419
  if (this.parentEntryName) {
414
420
  s = s.concat(` extends ${this.parentEntryName}`);
415
421
  }
416
- const scms = recordSchemaToString(this.schema);
417
- return s.concat('\n', scms, '\n');
422
+ let scms = recordSchemaToString(this.schema);
423
+ if (this.rbac && this.rbac.length > 0) {
424
+ const rbs = this.rbac.map((rs: RbacSpecification) => {
425
+ return rs.toString();
426
+ });
427
+ scms = `${scms} @rbac [${rbs.join(',\n')}]`;
428
+ }
429
+ return s.concat('\n{', scms, '}\n');
418
430
  }
419
431
 
420
432
  getUserAttributes(): RecordSchema {
@@ -562,6 +574,24 @@ export class RbacSpecification {
562
574
  this.permissions = new Set();
563
575
  }
564
576
 
577
+ static from(def: RbacSpecEntries): RbacSpecification {
578
+ const result = new RbacSpecification();
579
+ def.entries.forEach((se: RbacSpecEntry) => {
580
+ if (se.role) {
581
+ result.setRoles(se.role.roles);
582
+ } else if (se.allow) {
583
+ result.setPermissions(
584
+ se.allow.oprs.map((opr: RbacOpr) => {
585
+ return opr.value;
586
+ })
587
+ );
588
+ } else if (se.expr) {
589
+ result.setExpression(se.expr.lhs, se.expr.rhs);
590
+ }
591
+ });
592
+ return result;
593
+ }
594
+
565
595
  setResource(s: string): RbacSpecification {
566
596
  this.resource = s;
567
597
  return this;
@@ -624,11 +654,146 @@ export class RbacSpecification {
624
654
  };
625
655
  return this;
626
656
  }
657
+
658
+ toString(): string {
659
+ const rs = new Array<string>();
660
+ this.roles.forEach((r: string) => {
661
+ rs.push(r);
662
+ });
663
+ let cond = '';
664
+ if (this.expression) {
665
+ cond = `where: ${this.expression.lhs} = ${this.expression.rhs}`;
666
+ } else {
667
+ cond = `roles: [${rs.join(',')}]`;
668
+ }
669
+ const perms = new Array<string>();
670
+ this.permissions.forEach((p: RbacPermissionFlag) => {
671
+ perms.push(RbacPermissionFlag[p].toLowerCase());
672
+ });
673
+ return `(${cond}, allow: [${perms.join(',')}])`;
674
+ }
675
+ }
676
+
677
+ export class Agent extends Record {
678
+ override type: RecordType = RecordType.AGENT;
679
+ attributes: InstanceAttributes;
680
+
681
+ constructor(name: string, moduleName: string, attrs?: InstanceAttributes) {
682
+ super(Agent.EscapeName(name), moduleName);
683
+ this.attributes = attrs ? attrs : newInstanceAttributes();
684
+ }
685
+
686
+ setName(n: string): Agent {
687
+ this.name = Agent.EscapeName(n);
688
+ return this;
689
+ }
690
+
691
+ setLLM(llm: string): Agent {
692
+ this.attributes.set('llm', llm);
693
+ return this;
694
+ }
695
+
696
+ getLLM(): string {
697
+ return this.attributes.get('llm');
698
+ }
699
+
700
+ private removeAgentAttribute(n: string): Agent {
701
+ this.attributes.delete(n);
702
+ return this;
703
+ }
704
+
705
+ removeLLM(): Agent {
706
+ return this.removeAgentAttribute('llm');
707
+ }
708
+
709
+ setInstruction(s: string): Agent {
710
+ this.attributes.set('instruction', s);
711
+ return this;
712
+ }
713
+
714
+ getInstruction(): string {
715
+ return this.attributes.get('instruction');
716
+ }
717
+
718
+ removeInstruction(): Agent {
719
+ return this.removeAgentAttribute('instruction');
720
+ }
721
+
722
+ setType(type: 'chat' | 'planner'): Agent {
723
+ this.attributes.set('type', type);
724
+ return this;
725
+ }
726
+
727
+ getType(): string {
728
+ return this.attributes.get('type');
729
+ }
730
+
731
+ removeType(): Agent {
732
+ return this.removeAgentAttribute('type');
733
+ }
734
+
735
+ setTools(tools: string[]): Agent {
736
+ this.attributes.set('tools', tools.join(','));
737
+ return this;
738
+ }
739
+
740
+ getTools(): string {
741
+ return this.attributes.get('tools');
742
+ }
743
+
744
+ removeTools(): Agent {
745
+ return this.removeAgentAttribute('tools');
746
+ }
747
+
748
+ setDocuments(docs: string[]): Agent {
749
+ this.attributes.set('documents', docs.join(','));
750
+ return this;
751
+ }
752
+
753
+ getDocuments(): string {
754
+ return this.attributes.get('documents');
755
+ }
756
+
757
+ removeDocuments(): Agent {
758
+ return this.removeAgentAttribute('documents');
759
+ }
760
+
761
+ override toString(): string {
762
+ const attrs = new Array<string>();
763
+ this.attributes.forEach((value: any, key: string) => {
764
+ const v = isString(value) ? `"${value}"` : value;
765
+ attrs.push(` ${key} ${v}`);
766
+ });
767
+ return `agent ${Agent.NormalizeName(this.name)}
768
+ {
769
+ ${attrs.join(',\n')}
770
+ }`;
771
+ }
772
+
773
+ static Suffix = '__agent';
774
+
775
+ static EscapeName(n: string): string {
776
+ if (n.endsWith(Agent.Suffix)) {
777
+ return n;
778
+ }
779
+ return `${n}${Agent.Suffix}`;
780
+ }
781
+
782
+ static NormalizeName(n: string): string {
783
+ if (n.endsWith(Agent.Suffix)) {
784
+ return n.substring(0, n.lastIndexOf(Agent.Suffix));
785
+ } else {
786
+ return n;
787
+ }
788
+ }
789
+
790
+ getName(): string {
791
+ return Agent.NormalizeName(this.name);
792
+ }
627
793
  }
628
794
 
629
795
  export class Entity extends Record {
630
796
  override type: RecordType = RecordType.ENTITY;
631
- rbac: RbacSpecification[] | undefined;
632
797
 
633
798
  constructor(
634
799
  name: string,
@@ -651,6 +816,10 @@ export class Entity extends Record {
651
816
  this.rbac = rbac;
652
817
  return this;
653
818
  }
819
+
820
+ getRbacSpecifications(): RbacSpecification[] | undefined {
821
+ return this.rbac;
822
+ }
654
823
  }
655
824
 
656
825
  export class Event extends Record {
@@ -1015,6 +1184,22 @@ export class Module {
1015
1184
  return entry;
1016
1185
  }
1017
1186
 
1187
+ addAgent(agentEntry: Agent): Agent {
1188
+ return this.addEntry(agentEntry) as Agent;
1189
+ }
1190
+
1191
+ getAgent(agentName: string): Agent | undefined {
1192
+ const n = Agent.EscapeName(agentName);
1193
+ if (this.hasEntry(n)) {
1194
+ return this.getEntry(n) as Agent;
1195
+ }
1196
+ return undefined;
1197
+ }
1198
+
1199
+ removeAgent(agentName: string): boolean {
1200
+ return this.removeEntry(Agent.EscapeName(agentName));
1201
+ }
1202
+
1018
1203
  private getEntryIndex(entryName: string): number {
1019
1204
  return this.entries.findIndex((v: ModuleEntry) => {
1020
1205
  return v.name == entryName;
@@ -1078,6 +1263,16 @@ export class Module {
1078
1263
  return this.getEntriesOfType(RecordType.RECORD) as Record[];
1079
1264
  }
1080
1265
 
1266
+ getAgents(): Agent[] {
1267
+ return this.getEntriesOfType(RecordType.AGENT) as Agent[];
1268
+ }
1269
+
1270
+ getAgentNames(): string[] {
1271
+ return this.getAgents().map((ae: Agent) => {
1272
+ return Agent.NormalizeName(ae.name);
1273
+ });
1274
+ }
1275
+
1081
1276
  getRelationshipEntries(): Relationship[] {
1082
1277
  return this.getEntriesOfType(RecordType.RELATIONSHIP) as Relationship[];
1083
1278
  }
@@ -1182,6 +1377,9 @@ export class Module {
1182
1377
  toString(): string {
1183
1378
  const ss: Array<string> = [];
1184
1379
  this.entries.forEach((me: ModuleEntry) => {
1380
+ if (me instanceof Event && isAgentEvent(me)) {
1381
+ return;
1382
+ }
1185
1383
  ss.push(me.toString());
1186
1384
  });
1187
1385
  return `module ${this.name}\n\n${ss.join('\n')}`;
@@ -1221,7 +1419,7 @@ export function getModuleNames(): string[] {
1221
1419
  export function getUserModuleNames(): string[] {
1222
1420
  const result: Array<string> = new Array<string>();
1223
1421
  Array.from(moduleDb.keys()).forEach((n: string) => {
1224
- if (n != DefaultModuleName) {
1422
+ if (!DefaultModules.has(n)) {
1225
1423
  result.push(n);
1226
1424
  }
1227
1425
  });
@@ -1486,6 +1684,11 @@ export function addContainsRelationship(
1486
1684
  return addRelationship(name, 'contains', nodes, moduleName);
1487
1685
  }
1488
1686
 
1687
+ export function addAgent(name: string, attrs: InstanceAttributes, moduleName: string): Agent {
1688
+ const m = fetchModule(moduleName);
1689
+ return m.addAgent(new Agent(name, moduleName, attrs)) as Agent;
1690
+ }
1691
+
1489
1692
  function asWorkflowName(n: string): string {
1490
1693
  return n + '--workflow';
1491
1694
  }
@@ -1536,6 +1739,28 @@ export function getEntity(name: string, moduleName: string): Entity {
1536
1739
  throw new Error(`Entity ${fr.entryName} not found in module ${fr.moduleName}`);
1537
1740
  }
1538
1741
 
1742
+ function isEntryOfType(t: RecordType, fqName: string): boolean {
1743
+ const path = splitFqName(fqName);
1744
+ const mod = fetchModule(path.getModuleName());
1745
+ return mod.isEntryOfType(t, path.getEntryName());
1746
+ }
1747
+
1748
+ export function isEntity(fqName: string): boolean {
1749
+ return isEntryOfType(RecordType.ENTITY, fqName);
1750
+ }
1751
+
1752
+ export function isEvent(fqName: string): boolean {
1753
+ return isEntryOfType(RecordType.EVENT, fqName);
1754
+ }
1755
+
1756
+ export function isRecord(fqName: string): boolean {
1757
+ return isEntryOfType(RecordType.RECORD, fqName);
1758
+ }
1759
+
1760
+ export function isRelationship(fqName: string): boolean {
1761
+ return isEntryOfType(RecordType.RELATIONSHIP, fqName);
1762
+ }
1763
+
1539
1764
  export function getEvent(name: string, moduleName: string): Event {
1540
1765
  const fr: FetchModuleByEntryNameResult = fetchModuleByEntryName(name, moduleName);
1541
1766
  if (fr.module.isEvent(fr.entryName)) {
@@ -1819,10 +2044,14 @@ export class Instance {
1819
2044
  return this.record.getMeta('audit');
1820
2045
  }
1821
2046
 
1822
- lookup(k: string): any | undefined {
2047
+ lookup(k: string): any {
1823
2048
  return this.attributes.get(k);
1824
2049
  }
1825
2050
 
2051
+ lookupQueryVal(k: string): any {
2052
+ return this.queryAttributeValues?.get(k);
2053
+ }
2054
+
1826
2055
  getPath(): string {
1827
2056
  return this.lookup(PathAttributeName);
1828
2057
  }
@@ -2171,11 +2400,15 @@ export function isTimer(eventInst: Instance): boolean {
2171
2400
  return eventInst.getFqName() == 'agentlang/timer';
2172
2401
  }
2173
2402
 
2174
- export function isAgentEvent(eventInst: Instance): boolean {
2175
- const flag = eventInst.record.getMeta(IsAgentEventMeta);
2403
+ export function isAgentEvent(record: Record): boolean {
2404
+ const flag = record.getMeta(IsAgentEventMeta);
2176
2405
  return flag != undefined && flag == 'y';
2177
2406
  }
2178
2407
 
2408
+ export function isAgentEventInstance(eventInst: Instance): boolean {
2409
+ return isAgentEvent(eventInst.record);
2410
+ }
2411
+
2179
2412
  export function eventAgentName(eventInst: Instance): string | undefined {
2180
2413
  return eventInst.record.getMeta(EventAgentName);
2181
2414
  }
@@ -2186,3 +2419,17 @@ export function instanceToObject<Type>(inst: Instance, obj: any): Type {
2186
2419
  });
2187
2420
  return obj as Type;
2188
2421
  }
2422
+
2423
+ export function getEntityRbacRules(entityFqName: string): RbacSpecification[] | undefined {
2424
+ const p = splitFqName(entityFqName);
2425
+ const mn = p.getModuleName();
2426
+ const en = p.getEntryName();
2427
+ const m = isModule(mn) && fetchModule(mn);
2428
+ if (m && m.isEntity(en)) {
2429
+ const entity = getEntity(en, mn);
2430
+ return entity.getRbacSpecifications()?.filter((spec: RbacSpecification) => {
2431
+ return spec.expression != undefined;
2432
+ });
2433
+ }
2434
+ return undefined;
2435
+ }
@@ -1,4 +1,4 @@
1
- import { makeCoreModuleName, makeFqName } from '../util.js';
1
+ import { isFqName, makeCoreModuleName, makeFqName, splitFqName } from '../util.js';
2
2
  import { Environment, makeEventEvaluator, parseAndEvaluateStatement } from '../interpreter.js';
3
3
  import { fetchModule, Instance, instanceToObject, isModule } from '../module.js';
4
4
  import { provider } from '../agents/registry.js';
@@ -14,21 +14,23 @@ import { PlannerInstructions } from '../agents/common.js';
14
14
  import { PathAttributeNameQuery } from '../defs.js';
15
15
 
16
16
  export const CoreAIModuleName = makeCoreModuleName('ai');
17
+ export const AgentEntityName = 'Agent';
18
+ export const LlmEntityName = 'LLM';
17
19
 
18
20
  export default `module ${CoreAIModuleName}
19
21
 
20
- entity llm {
22
+ entity ${LlmEntityName} {
21
23
  name String @id,
22
24
  service String @default("openai"),
23
25
  config Map @optional
24
26
  }
25
27
 
26
- entity agent {
28
+ entity ${AgentEntityName} {
27
29
  name String @id,
28
30
  type @enum("chat", "planner") @default("chat"),
29
31
  instruction String @optional,
30
- tools String[] @optional,
31
- documents String[] @optional,
32
+ tools String @optional, // comma-separated values
33
+ documents String @optional, // comma-separated values
32
34
  llm String
33
35
  }
34
36
 
@@ -43,33 +45,33 @@ workflow findAgentChatSession {
43
45
  }
44
46
 
45
47
  workflow saveAgentChatSession {
46
- upsert {agentChatSession {id saveAgentChatSession.id, messages saveAgentChatSession.messages}}
48
+ {agentChatSession {id saveAgentChatSession.id, messages saveAgentChatSession.messages}, @upsert}
47
49
  }
48
50
 
49
- entity document {
51
+ entity Document {
50
52
  title String @id,
51
53
  content String,
52
54
  @meta {"fullTextSearch": "*"}
53
55
  }
54
56
  `;
55
57
 
56
- export const AgentFqName = makeFqName(CoreAIModuleName, 'agent');
58
+ export const AgentFqName = makeFqName(CoreAIModuleName, AgentEntityName);
57
59
 
58
60
  const ProviderDb = new Map<string, AgentServiceProvider>();
59
61
 
60
- export class Agent {
62
+ export class AgentInstance {
61
63
  llm: string = '';
62
64
  name: string = '';
63
65
  chatId: string | undefined;
64
66
  instruction: string = '';
65
67
  type: string = 'chat';
66
- tools: string[] | undefined;
67
- documents: string[] | undefined;
68
+ tools: string | undefined;
69
+ documents: string | undefined;
68
70
 
69
71
  private constructor() {}
70
72
 
71
- static FromInstance(agentInstance: Instance): Agent {
72
- return instanceToObject<Agent>(agentInstance, new Agent());
73
+ static FromInstance(agentInstance: Instance): AgentInstance {
74
+ return instanceToObject<AgentInstance>(agentInstance, new AgentInstance());
73
75
  }
74
76
 
75
77
  isPlanner(): boolean {
@@ -111,14 +113,14 @@ export class Agent {
111
113
 
112
114
  private async maybeAddRelevantDocuments(message: string, env: Environment): Promise<string> {
113
115
  if (this.documents && this.documents.length > 0) {
114
- const s = `${message}. Relevant documents are: ${this.documents.join(',')}`;
115
- const result: any[] = await parseHelper(`{agentlang_ai/document? "${s}"}`, env);
116
+ const s = `${message}. Relevant documents are: ${this.documents}`;
117
+ const result: any[] = await parseHelper(`{agentlang_ai/Document? "${s}"}`, env);
116
118
  if (result && result.length > 0) {
117
119
  const docs: Instance[] = [];
118
120
  for (let i = 0; i < result.length; ++i) {
119
121
  const v: any = result[i];
120
122
  const r: Instance[] = await parseHelper(
121
- `{agentlang_ai/document {${PathAttributeNameQuery} "${v.id}"}}`,
123
+ `{agentlang_ai/Document {${PathAttributeNameQuery} "${v.id}"}}`,
122
124
  env
123
125
  );
124
126
  if (r && r.length > 0) {
@@ -141,14 +143,36 @@ export class Agent {
141
143
 
142
144
  private toolsAsString(): string {
143
145
  if (this.tools) {
144
- return this.tools
145
- .filter((s: string) => {
146
- return isModule(s);
147
- })
148
- .map((moduleName: string) => {
149
- return fetchModule(moduleName).toString();
150
- })
151
- .join('\n');
146
+ const tooldefs = new Array<string>();
147
+ const slimModules = new Map<string, string[]>();
148
+ this.tools.split(',').forEach((n: string) => {
149
+ let moduleName: string | undefined;
150
+ let entryName: string | undefined;
151
+ if (isFqName(n)) {
152
+ const parts = splitFqName(n);
153
+ moduleName = parts.getModuleName();
154
+ entryName = parts.getEntryName();
155
+ } else {
156
+ moduleName = n;
157
+ }
158
+ if (isModule(moduleName)) {
159
+ const m = fetchModule(moduleName);
160
+ if (entryName) {
161
+ const hasmod = slimModules.has(moduleName);
162
+ const defs = hasmod ? slimModules.get(moduleName) : new Array<string>();
163
+ defs?.push(m.getEntry(entryName).toString());
164
+ if (!hasmod && defs) {
165
+ slimModules.set(moduleName, defs);
166
+ }
167
+ } else {
168
+ tooldefs.push(fetchModule(moduleName).toString());
169
+ }
170
+ }
171
+ });
172
+ slimModules.forEach((defs: string[], modName: string) => {
173
+ tooldefs.push(`module ${modName}\n${defs.join('\n')}`);
174
+ });
175
+ return tooldefs.join('\n');
152
176
  } else {
153
177
  return '';
154
178
  }
@@ -160,11 +184,11 @@ async function parseHelper(stmt: string, env: Environment): Promise<any> {
160
184
  return env.getLastResult();
161
185
  }
162
186
 
163
- export async function findAgentByName(name: string, env: Environment): Promise<Agent> {
164
- const result = await parseHelper(`{agentlang_ai/agent {name? "${name}"}}`, env);
187
+ export async function findAgentByName(name: string, env: Environment): Promise<AgentInstance> {
188
+ const result = await parseHelper(`{${AgentFqName} {name? "${name}"}}`, env);
165
189
  if (result instanceof Array && result.length > 0) {
166
190
  const agentInstance: Instance = result[0];
167
- return Agent.FromInstance(agentInstance);
191
+ return AgentInstance.FromInstance(agentInstance);
168
192
  } else {
169
193
  throw new Error(`Failed to fine agent ${name}`);
170
194
  }
@@ -177,7 +201,7 @@ export async function findProviderForLLM(
177
201
  let p: AgentServiceProvider | undefined = ProviderDb.get(llmName);
178
202
  if (p == undefined) {
179
203
  const result: Instance[] = await parseAndEvaluateStatement(
180
- `{${CoreAIModuleName}/llm {name? "${llmName}"}}`,
204
+ `{${CoreAIModuleName}/${LlmEntityName} {name? "${llmName}"}}`,
181
205
  undefined,
182
206
  env
183
207
  );