@aigne/core 0.4.205-1 → 0.4.205

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/lib/cjs/agent.js CHANGED
@@ -24,18 +24,23 @@ class Agent extends runnable_1.Runnable {
24
24
  * Load memories that are defined in the agent definition.
25
25
  * @param input The agent input.
26
26
  * @param context The AIGNE context.
27
- * @returns A dictionary of memories, where the key is the memory name and the value is an array of memory items.
27
+ * @returns A dictionary of memories, where the key is the memory id or name and the value is an array of memory items.
28
28
  */
29
29
  async loadMemories(input, context) {
30
30
  const { memories } = this.definition;
31
31
  const { userId, sessionId } = context?.state ?? {};
32
- return Object.fromEntries((await Promise.all(utils_1.OrderedRecord.map(memories, async ({ name, memory, query, options }) => {
32
+ return Object.fromEntries((await Promise.all(utils_1.OrderedRecord.map(memories, async ({ id, name, memory, query, options }) => {
33
33
  if (!name || !memory)
34
34
  return null;
35
35
  const q = await this.getMemoryQuery(input, query);
36
36
  const { results: memories } = await memory.search(q, { ...options, userId, sessionId });
37
- return [name, memories];
38
- }))).filter(utils_1.isNonNullable));
37
+ return [
38
+ [id, memories],
39
+ [name, memories],
40
+ ];
41
+ })))
42
+ .flat()
43
+ .filter(utils_1.isNonNullable));
39
44
  }
40
45
  /**
41
46
  * Update memories by user messages and assistant responses.
@@ -49,7 +54,7 @@ class Agent extends runnable_1.Runnable {
49
54
  logger_1.default.warn(`Memory is not defined in agent ${this.name || this.id}`);
50
55
  return;
51
56
  }
52
- return await memory.add(messages, { userId, sessionId });
57
+ await memory.add(messages, { userId, sessionId });
53
58
  }));
54
59
  }
55
60
  async run(input, options) {
@@ -12,8 +12,8 @@ function toRunnableMemories(agentName, inputs, memories) {
12
12
  throw new Error(`LLMAgent ${agentName} -> Memory ${name} -> Query variable ${query.fromVariable.toString()} not found`);
13
13
  return {
14
14
  id: name || (0, nanoid_1.nanoid)(),
15
- name: name,
16
- memory: memory,
15
+ name,
16
+ memory,
17
17
  query: queryFromVariable ? { from: 'variable', fromVariableId: queryFromVariable.id } : undefined,
18
18
  options,
19
19
  };
@@ -104,7 +104,7 @@ let LLMAgent = LLMAgent_1 = class LLMAgent extends agent_1.Agent {
104
104
  type: 'json_schema',
105
105
  jsonSchema: {
106
106
  name: 'output',
107
- schema: schema,
107
+ schema,
108
108
  strict: true,
109
109
  },
110
110
  },
@@ -21,14 +21,15 @@ exports.createPipelineAgentDefinition = createPipelineAgentDefinition;
21
21
  const lodash_1 = require("lodash");
22
22
  const nanoid_1 = require("nanoid");
23
23
  const tsyringe_1 = require("tsyringe");
24
+ const agent_1 = require("./agent");
24
25
  const constants_1 = require("./constants");
25
26
  const data_type_schema_1 = require("./definitions/data-type-schema");
27
+ const memory_1 = require("./definitions/memory");
26
28
  const logger_1 = __importDefault(require("./logger"));
27
- const runnable_1 = require("./runnable");
28
29
  const utils_1 = require("./utils");
29
30
  const is_non_nullable_1 = require("./utils/is-non-nullable");
30
31
  const ordered_map_1 = require("./utils/ordered-map");
31
- let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends runnable_1.Runnable {
32
+ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends agent_1.Agent {
32
33
  definition;
33
34
  static create(options) {
34
35
  const definition = createPipelineAgentDefinition(options);
@@ -38,7 +39,7 @@ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends runnable_1.Run
38
39
  super(definition, context);
39
40
  this.definition = definition;
40
41
  }
41
- async run(input, options) {
42
+ async process(input, options) {
42
43
  // TODO: validate the input against the definition
43
44
  const { definition, context } = this;
44
45
  if (!context)
@@ -51,12 +52,15 @@ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends runnable_1.Run
51
52
  async start(controller) {
52
53
  try {
53
54
  // NOTE: 将 input 转换为 variables,其中 key 为 inputId,value 为 input 的值
54
- const variables = Object.fromEntries(ordered_map_1.OrderedRecord.map(definition.inputs, (i) => {
55
- const value = input[i.name || i.id];
56
- if ((0, lodash_1.isNil)(value))
57
- return null;
58
- return [i.id, value];
59
- }).filter(is_non_nullable_1.isNonNullable));
55
+ const variables = {
56
+ ...options.memories,
57
+ ...Object.fromEntries(ordered_map_1.OrderedRecord.map(definition.inputs, (i) => {
58
+ const value = input[i.name || i.id];
59
+ if ((0, lodash_1.isNil)(value))
60
+ return null;
61
+ return [i.id, value];
62
+ }).filter(is_non_nullable_1.isNonNullable)),
63
+ };
60
64
  const outputs = ordered_map_1.OrderedRecord.toArray(definition.outputs);
61
65
  const textStreamOutput = outputs.find((i) => i.name === constants_1.StreamTextOutputName);
62
66
  let result = {};
@@ -144,15 +148,17 @@ exports.PipelineAgent = PipelineAgent = PipelineAgent_1 = __decorate([
144
148
  __metadata("design:paramtypes", [Object, Object])
145
149
  ], PipelineAgent);
146
150
  function createPipelineAgentDefinition(options) {
151
+ const agentId = options.name || (0, nanoid_1.nanoid)();
147
152
  const inputs = (0, data_type_schema_1.schemaToDataType)(options.inputs);
153
+ const memories = (0, memory_1.toRunnableMemories)(agentId, inputs, options.memories || {});
148
154
  const processes = ordered_map_1.OrderedRecord.fromArray([]);
149
- for (const p of options.processes || []) {
155
+ for (const [name, p] of Object.entries(options.processes)) {
150
156
  ordered_map_1.OrderedRecord.push(processes, {
151
157
  id: (0, nanoid_1.nanoid)(),
152
- name: p.name || p.runnable?.name,
158
+ name: name || p.runnable?.name,
153
159
  runnable: { id: p.runnable.id },
154
160
  input: Object.fromEntries(ordered_map_1.OrderedRecord.map(p.runnable.definition.inputs, (inputOfProcess) => {
155
- const i = p.input?.[inputOfProcess.name || inputOfProcess.id];
161
+ const i = p.input[inputOfProcess.name || inputOfProcess.id];
156
162
  if (!i) {
157
163
  if (inputOfProcess.required) {
158
164
  throw new Error(`Input ${inputOfProcess.name || inputOfProcess.id} for case ${p.runnable.name || p.runnable.id} is required`);
@@ -163,7 +169,7 @@ function createPipelineAgentDefinition(options) {
163
169
  const inputFromPipeline = ordered_map_1.OrderedRecord.find(inputs, (input) => input.name === i.fromVariable) ||
164
170
  ordered_map_1.OrderedRecord.find(processes, (p) => p.name === i.fromVariable);
165
171
  if (!inputFromPipeline)
166
- throw new Error(`Input ${i.fromVariable} not found`);
172
+ throw new Error(`Input ${i.fromVariable.toString()} not found`);
167
173
  return [
168
174
  inputOfProcess.id,
169
175
  {
@@ -184,10 +190,11 @@ function createPipelineAgentDefinition(options) {
184
190
  return { ...output, from: 'variable', fromVariableId: from.id, fromVariablePropPath };
185
191
  }));
186
192
  return {
187
- id: options.id || options.name || (0, nanoid_1.nanoid)(),
193
+ id: agentId,
188
194
  name: options.name,
189
195
  type: 'pipeline_agent',
190
196
  inputs,
197
+ memories,
191
198
  outputs,
192
199
  processes,
193
200
  };