agentlang 0.9.5 → 0.9.7

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/out/cli/main.d.ts.map +1 -1
  2. package/out/cli/main.js +8 -3
  3. package/out/cli/main.js.map +1 -1
  4. package/out/language/generated/ast.d.ts +80 -3
  5. package/out/language/generated/ast.d.ts.map +1 -1
  6. package/out/language/generated/ast.js +62 -0
  7. package/out/language/generated/ast.js.map +1 -1
  8. package/out/language/generated/grammar.d.ts.map +1 -1
  9. package/out/language/generated/grammar.js +432 -218
  10. package/out/language/generated/grammar.js.map +1 -1
  11. package/out/language/main.cjs +485 -218
  12. package/out/language/main.cjs.map +2 -2
  13. package/out/language/parser.js +8 -8
  14. package/out/language/parser.js.map +1 -1
  15. package/out/runtime/agents/common.d.ts +7 -1
  16. package/out/runtime/agents/common.d.ts.map +1 -1
  17. package/out/runtime/agents/common.js +101 -0
  18. package/out/runtime/agents/common.js.map +1 -1
  19. package/out/runtime/agents/impl/anthropic.js +4 -4
  20. package/out/runtime/agents/impl/anthropic.js.map +1 -1
  21. package/out/runtime/agents/impl/openai.js +4 -4
  22. package/out/runtime/agents/impl/openai.js.map +1 -1
  23. package/out/runtime/docs.d.ts.map +1 -1
  24. package/out/runtime/docs.js +109 -7
  25. package/out/runtime/docs.js.map +1 -1
  26. package/out/runtime/embeddings/chunker.d.ts +9 -0
  27. package/out/runtime/embeddings/chunker.d.ts.map +1 -0
  28. package/out/runtime/embeddings/chunker.js +41 -0
  29. package/out/runtime/embeddings/chunker.js.map +1 -0
  30. package/out/runtime/embeddings/index.d.ts +6 -0
  31. package/out/runtime/embeddings/index.d.ts.map +1 -0
  32. package/out/runtime/embeddings/index.js +6 -0
  33. package/out/runtime/embeddings/index.js.map +1 -0
  34. package/out/runtime/embeddings/openai.d.ts +15 -0
  35. package/out/runtime/embeddings/openai.d.ts.map +1 -0
  36. package/out/runtime/embeddings/openai.js +34 -0
  37. package/out/runtime/embeddings/openai.js.map +1 -0
  38. package/out/runtime/embeddings/provider.d.ts +20 -0
  39. package/out/runtime/embeddings/provider.d.ts.map +1 -0
  40. package/out/runtime/embeddings/provider.js +17 -0
  41. package/out/runtime/embeddings/provider.js.map +1 -0
  42. package/out/runtime/embeddings/registry.d.ts +3 -0
  43. package/out/runtime/embeddings/registry.d.ts.map +1 -0
  44. package/out/runtime/embeddings/registry.js +16 -0
  45. package/out/runtime/embeddings/registry.js.map +1 -0
  46. package/out/runtime/exec-graph.d.ts.map +1 -1
  47. package/out/runtime/exec-graph.js +5 -0
  48. package/out/runtime/exec-graph.js.map +1 -1
  49. package/out/runtime/interpreter.d.ts +4 -0
  50. package/out/runtime/interpreter.d.ts.map +1 -1
  51. package/out/runtime/interpreter.js +27 -7
  52. package/out/runtime/interpreter.js.map +1 -1
  53. package/out/runtime/loader.d.ts.map +1 -1
  54. package/out/runtime/loader.js +42 -5
  55. package/out/runtime/loader.js.map +1 -1
  56. package/out/runtime/module.d.ts +22 -4
  57. package/out/runtime/module.d.ts.map +1 -1
  58. package/out/runtime/module.js +76 -10
  59. package/out/runtime/module.js.map +1 -1
  60. package/out/runtime/modules/ai.d.ts +20 -2
  61. package/out/runtime/modules/ai.d.ts.map +1 -1
  62. package/out/runtime/modules/ai.js +333 -37
  63. package/out/runtime/modules/ai.js.map +1 -1
  64. package/out/runtime/modules/auth.d.ts.map +1 -1
  65. package/out/runtime/modules/auth.js +11 -5
  66. package/out/runtime/modules/auth.js.map +1 -1
  67. package/out/runtime/resolvers/interface.d.ts +1 -1
  68. package/out/runtime/resolvers/interface.d.ts.map +1 -1
  69. package/out/runtime/resolvers/interface.js.map +1 -1
  70. package/out/runtime/resolvers/sqldb/database.d.ts +1 -1
  71. package/out/runtime/resolvers/sqldb/database.d.ts.map +1 -1
  72. package/out/runtime/resolvers/sqldb/database.js +139 -50
  73. package/out/runtime/resolvers/sqldb/database.js.map +1 -1
  74. package/out/runtime/resolvers/sqldb/impl.d.ts +22 -2
  75. package/out/runtime/resolvers/sqldb/impl.d.ts.map +1 -1
  76. package/out/runtime/resolvers/sqldb/impl.js +178 -47
  77. package/out/runtime/resolvers/sqldb/impl.js.map +1 -1
  78. package/out/runtime/state.d.ts +1 -0
  79. package/out/runtime/state.d.ts.map +1 -1
  80. package/out/runtime/state.js +3 -0
  81. package/out/runtime/state.js.map +1 -1
  82. package/out/syntaxes/agentlang.monarch.js +1 -1
  83. package/out/syntaxes/agentlang.monarch.js.map +1 -1
  84. package/package.json +188 -185
  85. package/public/pdf.worker.mjs +65152 -0
  86. package/src/cli/main.ts +7 -2
  87. package/src/language/agentlang.langium +8 -2
  88. package/src/language/generated/ast.ts +96 -2
  89. package/src/language/generated/grammar.ts +432 -218
  90. package/src/language/parser.ts +8 -8
  91. package/src/runtime/agents/common.ts +107 -0
  92. package/src/runtime/agents/impl/anthropic.ts +4 -4
  93. package/src/runtime/agents/impl/openai.ts +4 -4
  94. package/src/runtime/docs.ts +120 -9
  95. package/src/runtime/embeddings/chunker.ts +50 -0
  96. package/src/runtime/embeddings/index.ts +5 -0
  97. package/src/runtime/embeddings/openai.ts +49 -0
  98. package/src/runtime/embeddings/provider.ts +37 -0
  99. package/src/runtime/embeddings/registry.ts +17 -0
  100. package/src/runtime/exec-graph.ts +4 -0
  101. package/src/runtime/interpreter.ts +39 -16
  102. package/src/runtime/loader.ts +42 -3
  103. package/src/runtime/module.ts +127 -41
  104. package/src/runtime/modules/ai.ts +467 -38
  105. package/src/runtime/modules/auth.ts +11 -5
  106. package/src/runtime/resolvers/interface.ts +1 -1
  107. package/src/runtime/resolvers/sqldb/database.ts +146 -56
  108. package/src/runtime/resolvers/sqldb/impl.ts +238 -61
  109. package/src/runtime/state.ts +4 -0
  110. package/src/syntaxes/agentlang.monarch.ts +1 -1
  111. package/out/setupClassic.d.ts +0 -98
  112. package/out/setupClassic.d.ts.map +0 -1
  113. package/out/setupClassic.js +0 -38
  114. package/out/setupClassic.js.map +0 -1
  115. package/out/setupCommon.d.ts +0 -2
  116. package/out/setupCommon.d.ts.map +0 -1
  117. package/out/setupCommon.js +0 -33
  118. package/out/setupCommon.js.map +0 -1
  119. package/out/setupExtended.d.ts +0 -40
  120. package/out/setupExtended.d.ts.map +0 -1
  121. package/out/setupExtended.js +0 -67
  122. package/out/setupExtended.js.map +0 -1
@@ -32,8 +32,8 @@ import {
32
32
  WhereSpec,
33
33
  } from '../language/generated/ast.js';
34
34
  import {
35
- maybeInstanceAsString,
36
35
  defineAgentEvent,
36
+ Event,
37
37
  getOneOfRef,
38
38
  getRelationship,
39
39
  getWorkflow,
@@ -46,15 +46,15 @@ import {
46
46
  isEntityInstance,
47
47
  isEventInstance,
48
48
  isInstanceOfType,
49
+ isOneToOneBetweenRelationship,
49
50
  isTimer,
50
51
  makeInstance,
52
+ maybeInstanceAsString,
51
53
  newInstanceAttributes,
52
54
  PlaceholderRecordEntry,
53
55
  Relationship,
54
- Workflow,
55
56
  setMetaAttributes,
56
- Event,
57
- isOneToOneBetweenRelationship,
57
+ Workflow,
58
58
  } from './module.js';
59
59
  import { JoinInfo, Resolver, WhereClause } from './resolvers/interface.js';
60
60
  import { ResolverAuthInfo } from './resolvers/authinfo.js';
@@ -65,28 +65,28 @@ import {
65
65
  escapeFqName,
66
66
  escapeQueryName,
67
67
  fqNameFromPath,
68
+ isCoreModule,
68
69
  isFqName,
69
70
  isPath,
70
71
  isString,
71
72
  makeCoreModuleName,
72
73
  makeFqName,
74
+ nameToPath,
73
75
  Path,
76
+ preprocessRawConfig,
74
77
  QuerySuffix,
75
78
  restoreSpecialChars,
76
- nameToPath,
77
79
  splitRefs,
78
- isCoreModule,
79
- preprocessRawConfig,
80
80
  } from './util.js';
81
81
  import { getResolver, getResolverNameForPath } from './resolvers/registry.js';
82
82
  import { parseStatement, parseWorkflow } from '../language/parser.js';
83
83
  import { ActiveSessionInfo, AdminSession, AdminUserId } from './auth/defs.js';
84
84
  import {
85
- AgentInstance,
86
85
  AgentEntityName,
87
86
  AgentFqName,
87
+ AgentInstance,
88
88
  findAgentByName,
89
- trimGeneratedCode,
89
+ normalizeGeneratedCode,
90
90
  } from './modules/ai.js';
91
91
  import { logger } from './logger.js';
92
92
  import {
@@ -165,6 +165,7 @@ export class Environment extends Instance {
165
165
  private agentMode: 'chat' | 'planner' | undefined = undefined;
166
166
  private agentChatId: string | undefined = undefined;
167
167
  private monitor: Monitor | undefined = undefined;
168
+ private escalatedRole: string | undefined;
168
169
 
169
170
  private activeUserData: any = undefined;
170
171
 
@@ -191,6 +192,7 @@ export class Environment extends Instance {
191
192
  this.eventExecutor = parent.eventExecutor;
192
193
  this.agentChatId = parent.agentChatId;
193
194
  this.monitor = parent.monitor;
195
+ this.escalatedRole = parent.escalatedRole;
194
196
  } else {
195
197
  this.activeModule = DefaultModuleName;
196
198
  this.activeResolvers = new Map<string, Resolver>();
@@ -287,6 +289,20 @@ export class Environment extends Instance {
287
289
  return this;
288
290
  }
289
291
 
292
+ setEscalatedRole(s: string): Environment {
293
+ this.escalatedRole = s;
294
+ return this;
295
+ }
296
+
297
+ getEscalatedRole(): string | undefined {
298
+ return this.escalatedRole;
299
+ }
300
+
301
+ resetEscalatedRole(): Environment {
302
+ this.escalatedRole = undefined;
303
+ return this;
304
+ }
305
+
290
306
  private static FlowContextTag = 'flow-context';
291
307
 
292
308
  setFlowContext(s: string): Environment {
@@ -900,6 +916,8 @@ export let evaluate = async function (
900
916
  if (!isEmptyWorkflow(wf)) {
901
917
  env = new Environment(eventInstance.name + '.env', activeEnv);
902
918
  env.setActiveEvent(eventInstance);
919
+ const er = wf.getRoleEscalation();
920
+ if (er) env.setEscalatedRole(er);
903
921
  if (kernelCall) {
904
922
  env.setInKernelMode(true);
905
923
  }
@@ -934,6 +952,7 @@ export let evaluate = async function (
934
952
  throw err;
935
953
  }
936
954
  } finally {
955
+ env?.resetEscalatedRole();
937
956
  if (!txnRolledBack && env !== undefined && activeEnv === undefined) {
938
957
  await env.commitAllTransactions();
939
958
  }
@@ -1736,11 +1755,15 @@ async function handleDocEvent(inst: Instance, env: Environment): Promise<void> {
1736
1755
  const s = await fetchDoc(inst.lookup('url'));
1737
1756
  if (s) {
1738
1757
  const title = inst.lookup('title');
1739
- await parseAndEvaluateStatement(
1740
- `{${CoreAIModuleName}/Document {title "${title}", content "${s}"}}`,
1741
- undefined,
1742
- env
1758
+ const doc = makeInstance(
1759
+ CoreAIModuleName,
1760
+ 'Document',
1761
+ newInstanceAttributes().set('title', title).set('content', s)
1743
1762
  );
1763
+ await computeExprAttributes(doc, undefined, undefined, env);
1764
+ await setMetaAttributes(doc.attributes, env);
1765
+ const res: Resolver = await getResolverForPath('Document', CoreAIModuleName, env);
1766
+ await res.createInstance(doc);
1744
1767
  }
1745
1768
  }
1746
1769
 
@@ -1959,7 +1982,7 @@ async function agentInvoke(agent: AgentInstance, msg: string, env: Environment):
1959
1982
  let retries = 0;
1960
1983
  while (true) {
1961
1984
  try {
1962
- let rs: string = result ? trimGeneratedCode(result) : '';
1985
+ let rs: string = result ? normalizeGeneratedCode(result) : '';
1963
1986
  let isWf = rs.startsWith('workflow');
1964
1987
  if (isWf && !agent.runWorkflows) {
1965
1988
  await parseWorkflow(rs);
@@ -2013,7 +2036,7 @@ async function agentInvoke(agent: AgentInstance, msg: string, env: Environment):
2013
2036
  let retries = 0;
2014
2037
  while (true) {
2015
2038
  try {
2016
- result = trimGeneratedCode(result);
2039
+ result = normalizeGeneratedCode(result);
2017
2040
  const obj = agent.maybeValidateJsonResponse(result);
2018
2041
  if (obj !== undefined) {
2019
2042
  env.setLastResult(obj);
@@ -2219,7 +2242,7 @@ async function preprocessStep(
2219
2242
  env: Environment
2220
2243
  ): Promise<PreprocStepResult> {
2221
2244
  let needAgentProcessing = true;
2222
- spec = trimGeneratedCode(spec);
2245
+ spec = normalizeGeneratedCode(spec);
2223
2246
  if (spec.startsWith('{') || spec.indexOf(' ') > 0) {
2224
2247
  const newEnv = Environment.from(env, env.name + '_flow_eval', false, true).setActiveModuleName(
2225
2248
  activeModuleName
@@ -1,4 +1,3 @@
1
- import chalk from 'chalk';
2
1
  import { createAgentlangServices } from '../language/agentlang-module.js';
3
2
  import {
4
3
  Import,
@@ -48,6 +47,8 @@ import {
48
47
  RetryDefinition,
49
48
  SetAttribute,
50
49
  CrudMap,
50
+ isAgentEvaluatorDefinition,
51
+ AgentEvaluatorDefinition,
51
52
  } from '../language/generated/ast.js';
52
53
  import {
53
54
  addEntity,
@@ -70,6 +71,7 @@ import {
70
71
  fetchModule,
71
72
  Retry,
72
73
  addGlobalRetry,
74
+ AgentEvaluator,
73
75
  } from './module.js';
74
76
  import {
75
77
  asStringLiteralsMap,
@@ -102,7 +104,13 @@ import {
102
104
  import { logger } from './logger.js';
103
105
  import { Environment, evaluateStatements, GlobalEnvironment } from './interpreter.js';
104
106
  import { createPermission, createRole } from './modules/auth.js';
105
- import { AgentEntityName, CoreAIModuleName, LlmEntityName } from './modules/ai.js';
107
+ import {
108
+ AgentEntityName,
109
+ AgentInstance,
110
+ AgentLearnerType,
111
+ CoreAIModuleName,
112
+ LlmEntityName,
113
+ } from './modules/ai.js';
106
114
  import { getDefaultLLMService } from './agents/registry.js';
107
115
  import { GenericResolver, GenericResolverMethods } from './resolvers/interface.js';
108
116
  import { registerResolver, setResolver } from './resolvers/registry.js';
@@ -125,6 +133,7 @@ import {
125
133
  registerAgentScenarios,
126
134
  registerAgentScratchNames,
127
135
  } from './agents/common.js';
136
+ import chalk from 'chalk';
128
137
 
129
138
  export async function extractDocument(
130
139
  fileName: string,
@@ -584,7 +593,7 @@ export function addWorkflowFromDef(
584
593
  moduleName: string,
585
594
  ispub: boolean = false
586
595
  ): Workflow {
587
- return addWorkflow(def.name || '', moduleName, def.statements, def.header, ispub);
596
+ return addWorkflow(def.name || '', moduleName, def.statements, def.header, def.directives, ispub);
588
597
  }
589
598
 
590
599
  const StandaloneStatements = new Map<string, Statement[]>();
@@ -842,6 +851,12 @@ async function addAgentDefinition(
842
851
  const service = getDefaultLLMService();
843
852
  wf = `{${CoreAIModuleName}/${LlmEntityName} {name "${llmName}", service "${service}"}, @upsert}; ${wf}`;
844
853
  }
854
+ if (attrs.get('type') === 'planner' || attrs.get('tools')) {
855
+ const llmn = llmName || attrs.get('llm');
856
+ wf = `${wf}; {${CoreAIModuleName}/${AgentEntityName}
857
+ {name "${name}_${AgentLearnerType}", moduleName "${moduleName}", llm "${llmn}",
858
+ type "${AgentLearnerType}", role "You are an agent that summarizes user-provided scenarios."}, @upsert}`;
859
+ }
845
860
  (await parseWorkflow(`workflow A {${wf}}`)).statements.forEach((stmt: Statement) => {
846
861
  addStandaloneStatement(stmt, moduleName, false);
847
862
  });
@@ -1090,6 +1105,29 @@ function addRetryDefinition(def: RetryDefinition, moduleName: string) {
1090
1105
  fetchModule(moduleName).addRetry(retry);
1091
1106
  }
1092
1107
 
1108
+ function addAgentEvaluatorDefinition(def: AgentEvaluatorDefinition, moduleName: string) {
1109
+ if (!def.name) throw new Error('Evaluator definition must have a name');
1110
+ const e = new AgentEvaluator(def.name, moduleName);
1111
+ def.attributes.forEach((sa: SetAttribute) => {
1112
+ const isins = sa.name === 'instruction';
1113
+ if (isins || sa.name === 'llm') {
1114
+ if (isLiteral(sa.value)) {
1115
+ const s = sa.value.id || sa.value.str;
1116
+ if (s) {
1117
+ if (isins) e.setInstruction(s);
1118
+ else e.setLlm(s);
1119
+ } else throw new Error(`Invalid value for evaluator-${sa.name} in ${def.name}`);
1120
+ } else {
1121
+ throw new Error(`evaluator ${sa.name} must be a string in ${def.name}`);
1122
+ }
1123
+ } else {
1124
+ throw new Error(`invalid attribute ${sa.name} in evaluator ${def.name}`);
1125
+ }
1126
+ });
1127
+ fetchModule(moduleName).addAgentEvaluator(e);
1128
+ AgentInstance.RegisterEvaluator(e);
1129
+ }
1130
+
1093
1131
  function addResolverDefinition(def: ResolverDefinition, moduleName: string) {
1094
1132
  const resolverName = `${moduleName}/${def.name}`;
1095
1133
  const paths = def.paths;
@@ -1157,6 +1195,7 @@ export async function addFromDef(def: Definition, moduleName: string) {
1157
1195
  else if (isDirectiveDefinition(def)) addDirectiveDefintion(def, moduleName);
1158
1196
  else if (isGlossaryEntryDefinition(def)) addGlossaryEntryDefintion(def, moduleName);
1159
1197
  else if (isRetryDefinition(def)) addRetryDefinition(def, moduleName);
1198
+ else if (isAgentEvaluatorDefinition(def)) addAgentEvaluatorDefinition(def, moduleName);
1160
1199
  }
1161
1200
 
1162
1201
  export async function parseAndIntern(code: string, moduleName?: string) {
@@ -1,56 +1,57 @@
1
1
  import chalk from 'chalk';
2
2
  import {
3
- Statement,
4
- KvPair,
5
- Literal,
3
+ AttributeDefinition,
4
+ Expr,
5
+ FlowDefinition,
6
+ FlowEntry,
6
7
  FnCall,
7
- RelNodes,
8
+ isLiteral,
8
9
  isRelNodes,
9
- AttributeDefinition,
10
- PropertyDefinition,
11
- NodeDefinition,
12
- RecordSchemaDefinition,
10
+ KvPair,
11
+ Literal,
13
12
  MapEntry,
14
- isLiteral,
15
13
  MetaDefinition,
14
+ NodeDefinition,
16
15
  PrePostTriggerDefinition,
17
- TriggerEntry,
18
- Expr,
19
- RbacSpecEntry,
20
- RbacSpecEntries,
16
+ PropertyDefinition,
21
17
  RbacOpr,
18
+ RbacSpecEntries,
19
+ RbacSpecEntry,
20
+ RecordSchemaDefinition,
21
+ RelNodes,
22
+ Statement,
23
+ TriggerEntry,
24
+ WorkflowDirectives,
22
25
  WorkflowHeader,
23
- FlowDefinition,
24
- FlowEntry,
25
26
  } from '../language/generated/ast.js';
26
27
  import {
27
- Path,
28
- nameToPath,
29
- isString,
30
- isNumber,
31
- isBoolean,
32
- isFqName,
33
- makeFqName,
28
+ asCrudType,
29
+ CrudType,
34
30
  DefaultModuleName,
35
31
  DefaultModules,
36
- joinStatements,
37
- isMinusZero,
38
- now,
39
- findMetaSchema,
32
+ encryptPassword,
33
+ escapeFqName,
40
34
  findAllPrePostTriggerSchema,
41
- CrudType,
42
- asCrudType,
43
- isPath,
35
+ findMetaSchema,
44
36
  findUqCompositeAttributes,
45
- escapeFqName,
46
- encryptPassword,
47
- splitFqName,
48
- splitRefs,
49
37
  forceAsFqName,
50
- validateIdFormat,
38
+ isBoolean,
39
+ isFqName,
40
+ isMinusZero,
41
+ isNumber,
42
+ isPath,
43
+ isString,
44
+ joinStatements,
45
+ makeFqName,
51
46
  nameContainsSepEscape,
47
+ nameToPath,
48
+ now,
49
+ Path,
52
50
  registerInitFunction,
53
51
  ScratchModuleName,
52
+ splitFqName,
53
+ splitRefs,
54
+ validateIdFormat,
54
55
  } from './util.js';
55
56
  import { parseStatement } from '../language/parser.js';
56
57
  import { ActiveSessionInfo, AdminSession } from './auth/defs.js';
@@ -512,7 +513,10 @@ export class Record extends ModuleEntry {
512
513
  }
513
514
 
514
515
  getFullTextSearchAttributes(): string[] | undefined {
515
- const fts: string[] | string | undefined = this.getMeta('fullTextSearch');
516
+ let fts: string[] | string | undefined = this.getMeta('fullTextSearchAttributes');
517
+ if (!fts) {
518
+ fts = this.getMeta('fullTextSearch');
519
+ }
516
520
  if (fts) {
517
521
  if (fts instanceof Array) {
518
522
  return fts as string[];
@@ -526,6 +530,14 @@ export class Record extends ModuleEntry {
526
530
  }
527
531
  }
528
532
 
533
+ getEmbeddingConfig(): { [key: string]: any } | undefined {
534
+ const config = this.getMeta('embeddingConfig');
535
+ if (config && typeof config === 'object') {
536
+ return config as { [key: string]: any };
537
+ }
538
+ return undefined;
539
+ }
540
+
529
541
  private resetUserAttrs() {
530
542
  this.userAttrNames = undefined;
531
543
  this.userAttrsSchema = undefined;
@@ -1517,11 +1529,19 @@ export class Relationship extends Record {
1517
1529
  export class Workflow extends ModuleEntry {
1518
1530
  statements: Statement[];
1519
1531
  isPrePost: boolean;
1532
+ directives: WorkflowDirectives | undefined;
1520
1533
 
1521
- constructor(name: string, patterns: Statement[], moduleName: string, isPrePost: boolean = false) {
1534
+ constructor(
1535
+ name: string,
1536
+ patterns: Statement[],
1537
+ moduleName: string,
1538
+ isPrePost: boolean = false,
1539
+ directives?: WorkflowDirectives
1540
+ ) {
1522
1541
  super(name, moduleName);
1523
1542
  this.statements = patterns;
1524
1543
  this.isPrePost = isPrePost;
1544
+ this.directives = directives;
1525
1545
  }
1526
1546
 
1527
1547
  async addStatement(stmtCode: string): Promise<Workflow> {
@@ -1648,10 +1668,29 @@ export class Workflow extends ModuleEntry {
1648
1668
  return this;
1649
1669
  }
1650
1670
 
1671
+ getRoleEscalation(): string | undefined {
1672
+ if (this.directives !== undefined) {
1673
+ for (let i = 0; i < this.directives.entries.length; ++i) {
1674
+ const e = this.directives.entries[i];
1675
+ if (e.tag === '@withRole') {
1676
+ return e.value;
1677
+ }
1678
+ }
1679
+ }
1680
+ return undefined;
1681
+ }
1682
+
1683
+ private directivesToString(): string {
1684
+ if (this.directives !== undefined) {
1685
+ return ` ${this.directives.$cstNode?.text || ''} `;
1686
+ }
1687
+ return ' ';
1688
+ }
1689
+
1651
1690
  override toString() {
1652
1691
  const n = this.isPrePost ? untangleWorkflowName(this.name) : this.name;
1653
1692
  const nn = normalizeWorkflowName(n);
1654
- let s: string = `workflow ${nn} {\n`;
1693
+ let s: string = `workflow ${nn}${this.directivesToString()}{\n`;
1655
1694
  const ss = this.statementsToStringsHelper(this.statements);
1656
1695
  s = s.concat(joinStatements(ss));
1657
1696
  if (!this.isPrePost) {
@@ -2091,6 +2130,49 @@ export class Retry extends ModuleEntry {
2091
2130
  }
2092
2131
  }
2093
2132
 
2133
+ export class AgentEvaluator extends ModuleEntry {
2134
+ instruction: string | undefined;
2135
+ llm: string | undefined;
2136
+
2137
+ constructor(name: string, moduleName: string) {
2138
+ super(name, moduleName);
2139
+ }
2140
+
2141
+ setInstruction(ins: string): AgentEvaluator {
2142
+ this.instruction = ins;
2143
+ return this;
2144
+ }
2145
+
2146
+ setLlm(llm: string): AgentEvaluator {
2147
+ this.llm = llm;
2148
+ return this;
2149
+ }
2150
+
2151
+ private static suffix = '_eval';
2152
+
2153
+ escapeName(): AgentEvaluator {
2154
+ this.name = `${this.name}${AgentEvaluator.suffix}`;
2155
+ return this;
2156
+ }
2157
+
2158
+ normalizedName(): string {
2159
+ const i = this.name.lastIndexOf(AgentEvaluator.suffix);
2160
+ if (i >= 0) {
2161
+ return this.name.substring(0, i);
2162
+ }
2163
+ return this.name;
2164
+ }
2165
+
2166
+ override toString(): string {
2167
+ let s = '';
2168
+ if (this.instruction) s = ` instruction "${this.instruction}"`;
2169
+ if (this.llm) s = `${s}\n llm "${this.llm}"`;
2170
+ return `evaluator ${this.normalizedName()} {
2171
+ ${s}
2172
+ }`;
2173
+ }
2174
+ }
2175
+
2094
2176
  export class Decision extends ModuleEntry {
2095
2177
  cases: string[];
2096
2178
 
@@ -2395,6 +2477,11 @@ export class Module {
2395
2477
  return this;
2396
2478
  }
2397
2479
 
2480
+ addAgentEvaluator(e: AgentEvaluator): Module {
2481
+ this.addEntry(e.escapeName());
2482
+ return this;
2483
+ }
2484
+
2398
2485
  getRetry(name: string): Retry | undefined {
2399
2486
  if (this.hasEntry(name)) {
2400
2487
  const e = this.getEntry(name);
@@ -2420,7 +2507,7 @@ export class Module {
2420
2507
 
2421
2508
  private getEntryIndex(entryName: string): number {
2422
2509
  return this.entries.findIndex((v: ModuleEntry) => {
2423
- return v.name == entryName;
2510
+ return v.name === entryName;
2424
2511
  });
2425
2512
  }
2426
2513
 
@@ -2706,8 +2793,6 @@ export function removeModule(name: string): boolean {
2706
2793
  return false;
2707
2794
  }
2708
2795
 
2709
- addModule(DefaultModuleName);
2710
- addRecord('env', DefaultModuleName);
2711
2796
  addModule(ScratchModuleName);
2712
2797
 
2713
2798
  export function getModuleNames(): string[] {
@@ -3092,6 +3177,7 @@ export function addWorkflow(
3092
3177
  moduleName = activeModule,
3093
3178
  statements?: Statement[],
3094
3179
  hdr?: WorkflowHeader | ThinWfHeader,
3180
+ directives?: WorkflowDirectives,
3095
3181
  ispub: boolean = false
3096
3182
  ): Workflow {
3097
3183
  if (hdr) {
@@ -3130,7 +3216,7 @@ export function addWorkflow(
3130
3216
  });
3131
3217
  }
3132
3218
  return module.addEntry(
3133
- new Workflow(asWorkflowName(name), statements, moduleName, hdr ? true : false)
3219
+ new Workflow(asWorkflowName(name), statements, moduleName, hdr ? true : false, directives)
3134
3220
  ) as Workflow;
3135
3221
  }
3136
3222