agentlang 0.0.68 → 0.1.1

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.
@@ -22,9 +22,10 @@ import {
22
22
  systemMessage,
23
23
  } from '../agents/provider.js';
24
24
  import { AIMessage, BaseMessage, HumanMessage } from '@langchain/core/messages';
25
- import { FlowExecInstructions, FlowStep, PlannerInstructions } from '../agents/common.js';
25
+ import { FlowExecInstructions, PlannerInstructions } from '../agents/common.js';
26
26
  import { PathAttributeNameQuery } from '../defs.js';
27
27
  import { logger } from '../logger.js';
28
+ import { FlowStep } from '../agents/flows.js';
28
29
 
29
30
  export const CoreAIModuleName = makeCoreModuleName('ai');
30
31
  export const AgentEntityName = 'Agent';
@@ -82,6 +83,52 @@ export const AgentFqName = makeFqName(CoreAIModuleName, AgentEntityName);
82
83
 
83
84
  const ProviderDb = new Map<string, AgentServiceProvider>();
84
85
 
86
+ export type AgentCondition = {
87
+ cond: string;
88
+ then: string;
89
+ };
90
+
91
+ const AgentDirectives = new Map<string, AgentCondition[]>();
92
+
93
+ export function registerAgentDirectives(
94
+ moduleName: string,
95
+ agentName: string,
96
+ conds: AgentCondition[]
97
+ ) {
98
+ AgentDirectives.set(makeFqName(moduleName, agentName), conds);
99
+ }
100
+
101
+ export type AgentScenario = {
102
+ user: string;
103
+ ai: string;
104
+ };
105
+
106
+ const AgentScenarios = new Map<string, AgentScenario[]>();
107
+
108
+ export function registerAgentScenarios(
109
+ moduleName: string,
110
+ agentName: string,
111
+ scenarios: AgentScenario[]
112
+ ) {
113
+ AgentScenarios.set(makeFqName(moduleName, agentName), scenarios);
114
+ }
115
+
116
+ export type AgentGlossaryEntry = {
117
+ name: string;
118
+ meaning: string;
119
+ synonyms: string | undefined;
120
+ };
121
+
122
+ const AgentGlossary = new Map<string, AgentGlossaryEntry[]>();
123
+
124
+ export function registerAgentGlossary(
125
+ moduleName: string,
126
+ agentName: string,
127
+ glossary: AgentGlossaryEntry[]
128
+ ) {
129
+ AgentGlossary.set(makeFqName(moduleName, agentName), glossary);
130
+ }
131
+
85
132
  export class AgentInstance {
86
133
  llm: string = '';
87
134
  name: string = '';
@@ -173,6 +220,56 @@ export class AgentInstance {
173
220
  return this.type == 'flow-exec';
174
221
  }
175
222
 
223
+ private directivesAsString(fqName: string): string {
224
+ const conds = AgentDirectives.get(fqName);
225
+ if (conds) {
226
+ const ss = new Array<string>();
227
+ ss.push(
228
+ '\nUse the following guidelines to take more accurate decisions in relevant scenarios.\n'
229
+ );
230
+ conds.forEach((ac: AgentCondition) => {
231
+ ss.push(`if ${ac.cond}, then ${ac.then}`);
232
+ });
233
+ return `${ss.join('\n')}\n`;
234
+ }
235
+ return '';
236
+ }
237
+
238
+ private cachedInstruction: string | undefined = undefined;
239
+
240
+ private getFullInstructions(): string {
241
+ if (this.cachedInstruction) {
242
+ return this.cachedInstruction;
243
+ }
244
+ const fqName = makeFqName(this.moduleName, this.name);
245
+ this.cachedInstruction = `${this.instruction || ''} ${this.directivesAsString(fqName)}`;
246
+ const gls = AgentGlossary.get(fqName);
247
+ if (gls) {
248
+ const glss = new Array<string>();
249
+ gls.forEach((age: AgentGlossaryEntry) => {
250
+ glss.push(
251
+ `${age.name}: ${age.meaning}. ${age.synonyms ? `These words are synonyms for ${age.name}: ${age.synonyms}` : ''}`
252
+ );
253
+ });
254
+ this.cachedInstruction = `${this.cachedInstruction}\nThe following glossary will be helpful for understanding user requests.
255
+ ${glss.join('\n')}\n`;
256
+ }
257
+ const scenarios = AgentScenarios.get(fqName);
258
+ if (scenarios) {
259
+ const scs = new Array<string>();
260
+ scenarios.forEach((sc: AgentScenario) => {
261
+ scs.push(`User: ${sc.user}\nAI: ${sc.ai}\n`);
262
+ });
263
+ this.cachedInstruction = `${this.cachedInstruction}\nHere are some example user requests and the corresponding responses you are supposed to produce:\n${scs.join('\n')}`;
264
+ }
265
+ return this.cachedInstruction;
266
+ }
267
+
268
+ markAsFlowExecutor(): AgentInstance {
269
+ this.type = 'flow-exec';
270
+ return this;
271
+ }
272
+
176
273
  async invoke(message: string, env: Environment) {
177
274
  const p = await findProviderForLLM(this.llm, env);
178
275
  const agentName = this.name;
@@ -190,14 +287,16 @@ export class AgentInstance {
190
287
  if (sess) {
191
288
  msgs = sess.lookup('messages');
192
289
  } else {
193
- msgs = [systemMessage(this.instruction || '')];
290
+ msgs = [systemMessage(this.getFullInstructions() || '')];
194
291
  }
195
292
  if (msgs) {
196
293
  try {
197
294
  const sysMsg = msgs[0];
198
295
  if (isplnr || isflow) {
199
296
  const s = isplnr ? PlannerInstructions : FlowExecInstructions;
200
- const newSysMsg = systemMessage(`${s}\n${this.toolsAsString()}\n${this.instruction}`);
297
+ const newSysMsg = systemMessage(
298
+ `${s}\n${this.toolsAsString()}\n${this.getFullInstructions()}`
299
+ );
201
300
  msgs[0] = newSysMsg;
202
301
  }
203
302
  msgs.push(humanMessage(await this.maybeAddRelevantDocuments(message, env)));
@@ -12,10 +12,14 @@ if (isNodeEnv) {
12
12
  });
13
13
  }
14
14
 
15
- const ExecGraphEnabled = /*isNodeEnv ? process.env['AL_EXEC_GRAPH_ENABLED'] == 'true' :*/ true;
16
-
17
15
  export function isExecGraphEnabled(): boolean {
18
- return ExecGraphEnabled;
16
+ if (isNodeEnv) {
17
+ const flag = process.env['AL_EXEC_GRAPH_ENABLED'];
18
+ if (flag != undefined && flag == 'false') {
19
+ return false;
20
+ }
21
+ }
22
+ return true;
19
23
  }
20
24
 
21
25
  // Browser-compatible path utilities