@aigne/core 0.4.201 → 0.4.203

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.
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.schemaToDataType = schemaToDataType;
4
+ const nanoid_1 = require("nanoid");
5
+ const utils_1 = require("./utils");
6
+ function schemaToDataType(dataType) {
7
+ return utils_1.OrderedRecord.fromArray(Object.entries(dataType).map(([name, schema]) => {
8
+ const base = {
9
+ ...schema,
10
+ id: (0, nanoid_1.nanoid)(),
11
+ name,
12
+ };
13
+ switch (schema.type) {
14
+ case 'string':
15
+ return {
16
+ ...base,
17
+ type: 'string',
18
+ };
19
+ case 'number':
20
+ return {
21
+ ...base,
22
+ type: 'number',
23
+ };
24
+ case 'boolean':
25
+ return {
26
+ ...base,
27
+ type: 'boolean',
28
+ };
29
+ case 'object':
30
+ return {
31
+ ...base,
32
+ type: 'object',
33
+ properties: schemaToDataType(schema.properties),
34
+ };
35
+ case 'array':
36
+ return {
37
+ ...base,
38
+ type: 'array',
39
+ items: utils_1.OrderedRecord.find(schemaToDataType({ items: schema.items }), (i) => i.name === 'items'),
40
+ };
41
+ default: {
42
+ throw new Error(`Unknown data type: ${schema.type}`);
43
+ }
44
+ }
45
+ }));
46
+ }
@@ -18,6 +18,7 @@ exports.createFunctionAgentDefinition = createFunctionAgentDefinition;
18
18
  const nanoid_1 = require("nanoid");
19
19
  const tsyringe_1 = require("tsyringe");
20
20
  const constants_1 = require("./constants");
21
+ const data_type_schema_1 = require("./data-type-schema");
21
22
  const function_runner_1 = require("./function-runner");
22
23
  const runnable_1 = require("./runnable");
23
24
  const utils_1 = require("./utils");
@@ -57,24 +58,12 @@ exports.FunctionAgent = FunctionAgent = FunctionAgent_1 = __decorate([
57
58
  __metadata("design:paramtypes", [Object, function_runner_1.FunctionRunner])
58
59
  ], FunctionAgent);
59
60
  function createFunctionAgentDefinition(options) {
60
- const inputs = utils_1.OrderedRecord.fromArray(options.inputs?.map((i) => ({
61
- id: (0, nanoid_1.nanoid)(),
62
- name: i.name,
63
- type: i.type,
64
- required: i.required,
65
- })));
66
- const outputs = utils_1.OrderedRecord.fromArray(options.outputs?.map((i) => ({
67
- id: (0, nanoid_1.nanoid)(),
68
- name: i.name,
69
- type: i.type,
70
- required: i.required,
71
- })));
72
61
  return {
73
62
  id: options.id || options.name || (0, nanoid_1.nanoid)(),
74
63
  name: options.name,
75
64
  type: 'function_agent',
76
- inputs,
77
- outputs,
65
+ inputs: (0, data_type_schema_1.schemaToDataType)(options.inputs),
66
+ outputs: (0, data_type_schema_1.schemaToDataType)(options.outputs),
78
67
  language: options.language,
79
68
  code: options.code,
80
69
  };
package/lib/cjs/index.js CHANGED
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./utils"), exports);
18
18
  __exportStar(require("./constants"), exports);
19
19
  __exportStar(require("./data-type"), exports);
20
+ __exportStar(require("./data-type-schema"), exports);
20
21
  __exportStar(require("./context"), exports);
21
22
  __exportStar(require("./runnable"), exports);
22
23
  __exportStar(require("./pipeline-agent"), exports);
@@ -19,6 +19,7 @@ const lodash_1 = require("lodash");
19
19
  const nanoid_1 = require("nanoid");
20
20
  const tsyringe_1 = require("tsyringe");
21
21
  const constants_1 = require("./constants");
22
+ const data_type_schema_1 = require("./data-type-schema");
22
23
  const llm_model_1 = require("./llm-model");
23
24
  const runnable_1 = require("./runnable");
24
25
  const utils_1 = require("./utils");
@@ -112,26 +113,18 @@ exports.LLMAgent = LLMAgent = LLMAgent_1 = __decorate([
112
113
  __metadata("design:paramtypes", [Object, llm_model_1.LLMModel])
113
114
  ], LLMAgent);
114
115
  function createLLMAgentDefinition(options) {
115
- const inputs = ordered_map_1.OrderedRecord.fromArray(options.inputs?.map((i) => ({
116
- id: (0, nanoid_1.nanoid)(),
117
- name: i.name,
118
- type: i.type,
119
- required: i.required,
120
- })));
121
- const outputs = ordered_map_1.OrderedRecord.fromArray(options.outputs?.map((i) => ({ ...i, id: (0, nanoid_1.nanoid)() })));
122
- const messages = ordered_map_1.OrderedRecord.fromArray(options.messages?.map((i) => ({
123
- id: (0, nanoid_1.nanoid)(),
124
- role: i.role,
125
- content: i.content,
126
- })));
127
116
  return {
128
117
  id: options.id || options.name || (0, nanoid_1.nanoid)(),
129
118
  name: options.name,
130
119
  type: 'llm_agent',
131
- inputs,
132
- outputs,
133
- messages,
120
+ inputs: (0, data_type_schema_1.schemaToDataType)(options.inputs),
121
+ outputs: (0, data_type_schema_1.schemaToDataType)(options.outputs),
134
122
  modelOptions: options.modelOptions,
123
+ messages: ordered_map_1.OrderedRecord.fromArray(options.messages?.map((i) => ({
124
+ id: (0, nanoid_1.nanoid)(),
125
+ role: i.role,
126
+ content: i.content,
127
+ }))),
135
128
  };
136
129
  }
137
130
  function outputsToJsonSchema(outputs) {
@@ -15,7 +15,6 @@ var LLMDecisionAgent_1;
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.LLMDecisionAgent = void 0;
17
17
  exports.createLLMDecisionAgentDefinition = createLLMDecisionAgentDefinition;
18
- const lodash_1 = require("lodash");
19
18
  const nanoid_1 = require("nanoid");
20
19
  const tsyringe_1 = require("tsyringe");
21
20
  const constants_1 = require("./constants");
@@ -82,27 +81,8 @@ let LLMDecisionAgent = LLMDecisionAgent_1 = class LLMDecisionAgent extends runna
82
81
  const caseToCall = cases.find((i) => i.name === functionNameToCall);
83
82
  if (!caseToCall)
84
83
  throw new Error('Case not found');
85
- // NOTE: 将 input 转换为 variables,其中 key 为 inputId,value 为 input 的值
86
- const variables = Object.fromEntries(utils_1.OrderedRecord.map(this.definition.inputs, (i) => {
87
- const value = input[i.name || i.id];
88
- if ((0, lodash_1.isNil)(value))
89
- return null;
90
- return [i.id, value];
91
- }).filter(utils_1.isNonNullable));
92
- const inputForCase = Object.fromEntries(Object.entries(caseToCall.input ?? {})
93
- .map(([inputId, { from, fromVariableId, fromVariablePropPath }]) => {
94
- const targetInput = utils_1.OrderedRecord.find(caseToCall.runnable.definition.inputs, (i) => i.id === inputId);
95
- if (!targetInput?.name)
96
- return null;
97
- if (from !== 'variable' || !fromVariableId)
98
- return null;
99
- const v = variables[fromVariableId];
100
- const value = fromVariablePropPath?.length ? (0, lodash_1.get)(v, fromVariablePropPath) : v;
101
- return [targetInput.name, value];
102
- })
103
- .filter(utils_1.isNonNullable));
104
84
  // TODO: check result structure and omit undefined values
105
- return (await caseToCall.runnable.run(inputForCase, options));
85
+ return (await caseToCall.runnable.run(input, options));
106
86
  }
107
87
  };
108
88
  exports.LLMDecisionAgent = LLMDecisionAgent;
@@ -114,12 +94,6 @@ exports.LLMDecisionAgent = LLMDecisionAgent = LLMDecisionAgent_1 = __decorate([
114
94
  __metadata("design:paramtypes", [Object, llm_model_1.LLMModel, Object])
115
95
  ], LLMDecisionAgent);
116
96
  function createLLMDecisionAgentDefinition(options) {
117
- const inputs = utils_1.OrderedRecord.fromArray(options.inputs?.map((i) => ({
118
- id: (0, nanoid_1.nanoid)(),
119
- name: i.name,
120
- type: i.type,
121
- required: i.required,
122
- })));
123
97
  const messages = utils_1.OrderedRecord.fromArray([
124
98
  {
125
99
  id: (0, nanoid_1.nanoid)(),
@@ -132,34 +106,13 @@ function createLLMDecisionAgentDefinition(options) {
132
106
  name: c.name || c.runnable.name,
133
107
  description: c.description,
134
108
  runnable: { id: c.runnable.id },
135
- // TODO: pass input from decision to case runnable
136
- input: Object.fromEntries(utils_1.OrderedRecord.map(c.runnable.definition.inputs, (inputOfCase) => {
137
- const i = c.input?.[inputOfCase.name || inputOfCase.id];
138
- if (!i) {
139
- if (inputOfCase.required) {
140
- throw new Error(`Input ${inputOfCase.name || inputOfCase.id} for case ${c.runnable.name || c.runnable.id} is required`);
141
- }
142
- // ignore optional input
143
- return null;
144
- }
145
- const inputFromDecision = utils_1.OrderedRecord.find(inputs, (input) => input.name === i.fromVariable);
146
- if (!inputFromDecision)
147
- throw new Error(`Input ${i.fromVariable} not found`);
148
- return [
149
- inputOfCase.id,
150
- {
151
- from: 'variable',
152
- fromVariableId: inputFromDecision.id,
153
- fromVariablePropPath: i.fromVariablePropPath,
154
- },
155
- ];
156
- }).filter(utils_1.isNonNullable)),
157
109
  })));
158
110
  return {
159
111
  id: options.id || options.name || (0, nanoid_1.nanoid)(),
160
112
  name: options.name,
161
113
  type: 'llm_decision_agent',
162
- inputs,
114
+ // TODO: decision agent inputs should be the intersection of all case inputs
115
+ inputs: utils_1.OrderedRecord.fromArray([]),
163
116
  // TODO: decision agent outputs should be the union of all case outputs
164
117
  outputs: utils_1.OrderedRecord.fromArray([]),
165
118
  messages,
@@ -18,6 +18,7 @@ exports.createLocalFunctionAgentDefinition = createLocalFunctionAgentDefinition;
18
18
  const nanoid_1 = require("nanoid");
19
19
  const tsyringe_1 = require("tsyringe");
20
20
  const constants_1 = require("./constants");
21
+ const data_type_schema_1 = require("./data-type-schema");
21
22
  const runnable_1 = require("./runnable");
22
23
  const utils_1 = require("./utils");
23
24
  let LocalFunctionAgent = LocalFunctionAgent_1 = class LocalFunctionAgent extends runnable_1.Runnable {
@@ -57,18 +58,8 @@ exports.LocalFunctionAgent = LocalFunctionAgent = LocalFunctionAgent_1 = __decor
57
58
  __metadata("design:paramtypes", [Object, Object])
58
59
  ], LocalFunctionAgent);
59
60
  function createLocalFunctionAgentDefinition(options) {
60
- const inputs = utils_1.OrderedRecord.fromArray(options.inputs?.map((i) => ({
61
- id: (0, nanoid_1.nanoid)(),
62
- name: i.name,
63
- type: i.type,
64
- required: i.required,
65
- })));
66
- const outputs = utils_1.OrderedRecord.fromArray(options.outputs?.map((i) => ({
67
- id: (0, nanoid_1.nanoid)(),
68
- name: i.name,
69
- type: i.type,
70
- required: i.required,
71
- })));
61
+ const inputs = (0, data_type_schema_1.schemaToDataType)(options.inputs);
62
+ const outputs = (0, data_type_schema_1.schemaToDataType)(options.outputs);
72
63
  return {
73
64
  id: options.id || options.name || (0, nanoid_1.nanoid)(),
74
65
  name: options.name,
@@ -22,6 +22,7 @@ const lodash_1 = require("lodash");
22
22
  const nanoid_1 = require("nanoid");
23
23
  const tsyringe_1 = require("tsyringe");
24
24
  const constants_1 = require("./constants");
25
+ const data_type_schema_1 = require("./data-type-schema");
25
26
  const logger_1 = __importDefault(require("./logger"));
26
27
  const runnable_1 = require("./runnable");
27
28
  const utils_1 = require("./utils");
@@ -145,12 +146,7 @@ exports.PipelineAgent = PipelineAgent = PipelineAgent_1 = __decorate([
145
146
  __metadata("design:paramtypes", [Object, Object])
146
147
  ], PipelineAgent);
147
148
  function createPipelineAgentDefinition(options) {
148
- const inputs = ordered_map_1.OrderedRecord.fromArray(options.inputs?.map((i) => ({
149
- id: (0, nanoid_1.nanoid)(),
150
- name: i.name,
151
- type: i.type,
152
- required: i.required,
153
- })));
149
+ const inputs = (0, data_type_schema_1.schemaToDataType)(options.inputs);
154
150
  const processes = ordered_map_1.OrderedRecord.fromArray([]);
155
151
  for (const p of options.processes || []) {
156
152
  ordered_map_1.OrderedRecord.push(processes, {
@@ -181,20 +177,13 @@ function createPipelineAgentDefinition(options) {
181
177
  }).filter(is_non_nullable_1.isNonNullable)),
182
178
  });
183
179
  }
184
- const outputs = ordered_map_1.OrderedRecord.fromArray(options.outputs.map((output) => {
185
- const from = ordered_map_1.OrderedRecord.find(inputs, (i) => i.name === output.fromVariable) ||
186
- ordered_map_1.OrderedRecord.find(processes, (p) => p.name === output.fromVariable);
180
+ const outputs = ordered_map_1.OrderedRecord.fromArray(ordered_map_1.OrderedRecord.map((0, data_type_schema_1.schemaToDataType)(options.outputs), (output) => {
181
+ const { fromVariable, fromVariablePropPath } = options.outputs[output.name];
182
+ const from = ordered_map_1.OrderedRecord.find(inputs, (i) => i.name === fromVariable) ||
183
+ ordered_map_1.OrderedRecord.find(processes, (p) => p.name === fromVariable);
187
184
  if (!from)
188
185
  throw new Error(`Output ${output.name} not found in inputs or processes`);
189
- return {
190
- id: (0, nanoid_1.nanoid)(),
191
- name: output.name,
192
- type: output.type,
193
- required: output.required,
194
- from: 'variable',
195
- fromVariableId: from.id,
196
- fromVariablePropPath: output.fromVariablePropPath,
197
- };
186
+ return { ...output, from: 'variable', fromVariableId: from.id, fromVariablePropPath };
198
187
  }));
199
188
  return {
200
189
  id: options.id || options.name || (0, nanoid_1.nanoid)(),