@aigne/core 0.4.205-1 → 0.4.206

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 (58) hide show
  1. package/lib/cjs/agent.js +37 -6
  2. package/lib/cjs/definitions/data-type-schema.js +2 -2
  3. package/lib/cjs/definitions/memory.js +2 -2
  4. package/lib/cjs/function-agent.js +34 -29
  5. package/lib/cjs/function-runner.js +6 -4
  6. package/lib/cjs/index.js +1 -1
  7. package/lib/cjs/llm-agent.js +23 -55
  8. package/lib/cjs/llm-decision-agent.js +8 -12
  9. package/lib/cjs/llm-model.js +2 -2
  10. package/lib/cjs/local-function-agent.js +7 -21
  11. package/lib/cjs/pipeline-agent.js +29 -32
  12. package/lib/cjs/runnable.js +1 -0
  13. package/lib/cjs/tsconfig.tsbuildinfo +1 -1
  14. package/lib/cjs/utils/index.js +5 -2
  15. package/lib/cjs/utils/stream-utils.js +36 -14
  16. package/lib/esm/agent.js +39 -8
  17. package/lib/esm/definitions/data-type-schema.js +2 -2
  18. package/lib/esm/definitions/memory.js +2 -2
  19. package/lib/esm/function-agent.js +33 -28
  20. package/lib/esm/function-runner.js +6 -4
  21. package/lib/esm/index.js +1 -1
  22. package/lib/esm/llm-agent.js +23 -54
  23. package/lib/esm/llm-decision-agent.js +8 -11
  24. package/lib/esm/llm-model.js +2 -2
  25. package/lib/esm/local-function-agent.js +7 -20
  26. package/lib/esm/pipeline-agent.js +29 -31
  27. package/lib/esm/runnable.js +1 -0
  28. package/lib/esm/tsconfig.tsbuildinfo +1 -1
  29. package/lib/esm/utils/index.js +5 -2
  30. package/lib/esm/utils/stream-utils.js +34 -14
  31. package/lib/types/agent.d.ts +9 -10
  32. package/lib/types/context.d.ts +2 -0
  33. package/lib/types/definitions/data-type-schema.d.ts +7 -5
  34. package/lib/types/{data-type.d.ts → definitions/data-type.d.ts} +2 -2
  35. package/lib/types/function-agent.d.ts +33 -20
  36. package/lib/types/function-runner.d.ts +20 -7
  37. package/lib/types/index.d.ts +1 -1
  38. package/lib/types/llm-agent.d.ts +27 -30
  39. package/lib/types/llm-decision-agent.d.ts +15 -22
  40. package/lib/types/llm-model.d.ts +2 -2
  41. package/lib/types/local-function-agent.d.ts +31 -34
  42. package/lib/types/memorable.d.ts +1 -1
  43. package/lib/types/pipeline-agent.d.ts +40 -33
  44. package/lib/types/runnable.d.ts +3 -3
  45. package/lib/types/tsconfig.tsbuildinfo +1 -1
  46. package/lib/types/utils/index.d.ts +5 -2
  47. package/lib/types/utils/message-utils.d.ts +3 -3
  48. package/lib/types/utils/stream-utils.d.ts +5 -3
  49. package/lib/types/utils/union.d.ts +1 -2
  50. package/package.json +3 -2
  51. package/lib/cjs/data-type-schema.js +0 -46
  52. package/lib/cjs/memory.js +0 -32
  53. package/lib/esm/data-type-schema.js +0 -43
  54. package/lib/esm/memory.js +0 -27
  55. package/lib/types/data-type-schema.d.ts +0 -46
  56. package/lib/types/memory.d.ts +0 -184
  57. /package/lib/cjs/{data-type.js → definitions/data-type.js} +0 -0
  58. /package/lib/esm/{data-type.js → definitions/data-type.js} +0 -0
@@ -14,32 +14,26 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
14
14
  var __importDefault = (this && this.__importDefault) || function (mod) {
15
15
  return (mod && mod.__esModule) ? mod : { "default": mod };
16
16
  };
17
- var PipelineAgent_1;
18
17
  Object.defineProperty(exports, "__esModule", { value: true });
19
18
  exports.PipelineAgent = void 0;
20
- exports.createPipelineAgentDefinition = createPipelineAgentDefinition;
21
19
  const lodash_1 = require("lodash");
22
20
  const nanoid_1 = require("nanoid");
23
21
  const tsyringe_1 = require("tsyringe");
22
+ const agent_1 = require("./agent");
24
23
  const constants_1 = require("./constants");
25
24
  const data_type_schema_1 = require("./definitions/data-type-schema");
25
+ const memory_1 = require("./definitions/memory");
26
26
  const logger_1 = __importDefault(require("./logger"));
27
- const runnable_1 = require("./runnable");
28
- const utils_1 = require("./utils");
29
27
  const is_non_nullable_1 = require("./utils/is-non-nullable");
30
28
  const ordered_map_1 = require("./utils/ordered-map");
31
- let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends runnable_1.Runnable {
29
+ let PipelineAgent = class PipelineAgent extends agent_1.Agent {
32
30
  definition;
33
- static create(options) {
34
- const definition = createPipelineAgentDefinition(options);
35
- return new PipelineAgent_1(definition);
36
- }
31
+ static create = create;
37
32
  constructor(definition, context) {
38
33
  super(definition, context);
39
34
  this.definition = definition;
40
35
  }
41
- async run(input, options) {
42
- // TODO: validate the input against the definition
36
+ async process(input, options) {
43
37
  const { definition, context } = this;
44
38
  if (!context)
45
39
  throw new Error('Context is required');
@@ -47,16 +41,19 @@ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends runnable_1.Run
47
41
  if (!processes?.$indexes.length) {
48
42
  throw new Error('No processes defined');
49
43
  }
50
- const result = new ReadableStream({
44
+ return new ReadableStream({
51
45
  async start(controller) {
52
46
  try {
53
47
  // 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));
48
+ const variables = {
49
+ ...options.memories,
50
+ ...Object.fromEntries(ordered_map_1.OrderedRecord.map(definition.inputs, (i) => {
51
+ const value = input[i.name || i.id];
52
+ if ((0, lodash_1.isNil)(value))
53
+ return null;
54
+ return [i.id, value];
55
+ }).filter(is_non_nullable_1.isNonNullable)),
56
+ };
60
57
  const outputs = ordered_map_1.OrderedRecord.toArray(definition.outputs);
61
58
  const textStreamOutput = outputs.find((i) => i.name === constants_1.StreamTextOutputName);
62
59
  let result = {};
@@ -105,6 +102,8 @@ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends runnable_1.Run
105
102
  result = Object.fromEntries(ordered_map_1.OrderedRecord.map(definition.outputs, (output) => {
106
103
  if (!output.name)
107
104
  return null;
105
+ if (output.name === constants_1.StreamTextOutputName)
106
+ return null;
108
107
  let value;
109
108
  if (output.from === 'variable') {
110
109
  const v = variables[output.fromVariableId];
@@ -129,30 +128,27 @@ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends runnable_1.Run
129
128
  }
130
129
  },
131
130
  });
132
- if (options?.stream) {
133
- return result;
134
- }
135
- // TODO: validate the result against the definition.outputs
136
- return await (0, utils_1.runnableResponseStreamToObject)(result);
137
131
  }
138
132
  };
139
133
  exports.PipelineAgent = PipelineAgent;
140
- exports.PipelineAgent = PipelineAgent = PipelineAgent_1 = __decorate([
134
+ exports.PipelineAgent = PipelineAgent = __decorate([
141
135
  (0, tsyringe_1.injectable)(),
142
136
  __param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
143
137
  __param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
144
138
  __metadata("design:paramtypes", [Object, Object])
145
139
  ], PipelineAgent);
146
- function createPipelineAgentDefinition(options) {
140
+ function create({ context, ...options }) {
141
+ const agentId = options.name || (0, nanoid_1.nanoid)();
147
142
  const inputs = (0, data_type_schema_1.schemaToDataType)(options.inputs);
143
+ const memories = (0, memory_1.toRunnableMemories)(agentId, inputs, options.memories || {});
148
144
  const processes = ordered_map_1.OrderedRecord.fromArray([]);
149
- for (const p of options.processes || []) {
145
+ for (const [name, p] of Object.entries(options.processes)) {
150
146
  ordered_map_1.OrderedRecord.push(processes, {
151
147
  id: (0, nanoid_1.nanoid)(),
152
- name: p.name || p.runnable?.name,
148
+ name: name || p.runnable?.name,
153
149
  runnable: { id: p.runnable.id },
154
150
  input: Object.fromEntries(ordered_map_1.OrderedRecord.map(p.runnable.definition.inputs, (inputOfProcess) => {
155
- const i = p.input?.[inputOfProcess.name || inputOfProcess.id];
151
+ const i = p.input[inputOfProcess.name || inputOfProcess.id];
156
152
  if (!i) {
157
153
  if (inputOfProcess.required) {
158
154
  throw new Error(`Input ${inputOfProcess.name || inputOfProcess.id} for case ${p.runnable.name || p.runnable.id} is required`);
@@ -163,7 +159,7 @@ function createPipelineAgentDefinition(options) {
163
159
  const inputFromPipeline = ordered_map_1.OrderedRecord.find(inputs, (input) => input.name === i.fromVariable) ||
164
160
  ordered_map_1.OrderedRecord.find(processes, (p) => p.name === i.fromVariable);
165
161
  if (!inputFromPipeline)
166
- throw new Error(`Input ${i.fromVariable} not found`);
162
+ throw new Error(`Input ${i.fromVariable.toString()} not found`);
167
163
  return [
168
164
  inputOfProcess.id,
169
165
  {
@@ -183,12 +179,13 @@ function createPipelineAgentDefinition(options) {
183
179
  throw new Error(`Output ${output.name} not found in inputs or processes`);
184
180
  return { ...output, from: 'variable', fromVariableId: from.id, fromVariablePropPath };
185
181
  }));
186
- return {
187
- id: options.id || options.name || (0, nanoid_1.nanoid)(),
182
+ return new PipelineAgent({
183
+ id: agentId,
188
184
  name: options.name,
189
185
  type: 'pipeline_agent',
190
186
  inputs,
187
+ memories,
191
188
  outputs,
192
189
  processes,
193
- };
190
+ }, context);
194
191
  }
@@ -10,6 +10,7 @@ class Runnable {
10
10
  constructor(definition, context) {
11
11
  this.definition = definition;
12
12
  this.context = context;
13
+ context?.register(this);
13
14
  this.inputs = Object.fromEntries(ordered_map_1.OrderedRecord.map(definition.inputs, (i) => [i.name || i.id, i]));
14
15
  this.outputs = Object.fromEntries(ordered_map_1.OrderedRecord.map(definition.outputs, (i) => [i.name || i.id, i]));
15
16
  }