@aigne/core 0.4.201 → 0.4.202

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