@aigne/core 0.4.205-0 → 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 +65 -0
- package/lib/cjs/{data-type-schema.js → definitions/data-type-schema.js} +1 -1
- package/lib/cjs/definitions/memory.js +21 -0
- package/lib/cjs/function-agent.js +1 -1
- package/lib/cjs/function-runner.js +2 -2
- package/lib/cjs/index.js +1 -1
- package/lib/cjs/llm-agent.js +20 -127
- package/lib/cjs/llm-decision-agent.js +40 -33
- package/lib/cjs/llm-model.js +2 -2
- package/lib/cjs/local-function-agent.js +11 -9
- package/lib/cjs/pipeline-agent.js +23 -18
- package/lib/cjs/runnable.js +3 -1
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/message-utils.js +62 -0
- package/lib/cjs/utils/nullable.js +2 -0
- package/lib/cjs/utils/ordered-map.js +12 -0
- package/lib/cjs/utils/stream-utils.js +43 -5
- package/lib/esm/agent.js +58 -0
- package/lib/esm/{data-type-schema.js → definitions/data-type-schema.js} +1 -1
- package/lib/esm/definitions/memory.js +18 -0
- package/lib/esm/function-agent.js +1 -1
- package/lib/esm/function-runner.js +2 -2
- package/lib/esm/index.js +1 -1
- package/lib/esm/llm-agent.js +22 -126
- package/lib/esm/llm-decision-agent.js +41 -34
- package/lib/esm/llm-model.js +2 -2
- package/lib/esm/local-function-agent.js +11 -9
- package/lib/esm/pipeline-agent.js +23 -18
- package/lib/esm/runnable.js +3 -1
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/message-utils.js +57 -0
- package/lib/esm/utils/nullable.js +1 -0
- package/lib/esm/utils/ordered-map.js +12 -0
- package/lib/esm/utils/stream-utils.js +42 -5
- package/lib/types/agent.d.ts +42 -0
- package/lib/types/{data-type-schema.d.ts → definitions/data-type-schema.d.ts} +3 -9
- package/lib/types/definitions/memory.d.ts +40 -0
- package/lib/types/function-agent.d.ts +1 -1
- package/lib/types/function-runner.d.ts +2 -1
- package/lib/types/index.d.ts +1 -1
- package/lib/types/llm-agent.d.ts +34 -63
- package/lib/types/llm-decision-agent.d.ts +54 -30
- package/lib/types/llm-model.d.ts +2 -1
- package/lib/types/local-function-agent.d.ts +50 -19
- package/lib/types/memorable.d.ts +3 -2
- package/lib/types/pipeline-agent.d.ts +63 -23
- package/lib/types/runnable.d.ts +10 -4
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/utils/message-utils.d.ts +19 -1
- package/lib/types/utils/nullable.d.ts +7 -0
- package/lib/types/utils/ordered-map.d.ts +3 -0
- package/lib/types/utils/stream-utils.d.ts +12 -1
- package/lib/types/utils/union.d.ts +1 -1
- package/package.json +1 -1
- package/lib/cjs/memory.js +0 -32
- package/lib/esm/memory.js +0 -27
- package/lib/types/memory.d.ts +0 -184
|
@@ -13,29 +13,28 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
13
13
|
var LocalFunctionAgent_1;
|
|
14
14
|
import { nanoid } from 'nanoid';
|
|
15
15
|
import { inject, injectable } from 'tsyringe';
|
|
16
|
+
import { Agent } from './agent';
|
|
16
17
|
import { TYPES } from './constants';
|
|
17
|
-
import { schemaToDataType } from './data-type-schema';
|
|
18
|
-
import {
|
|
18
|
+
import { schemaToDataType } from './definitions/data-type-schema';
|
|
19
|
+
import { toRunnableMemories } from './definitions/memory';
|
|
19
20
|
import { objectToRunnableResponseStream, runnableResponseStreamToObject } from './utils';
|
|
20
|
-
let LocalFunctionAgent = LocalFunctionAgent_1 = class LocalFunctionAgent extends
|
|
21
|
+
let LocalFunctionAgent = LocalFunctionAgent_1 = class LocalFunctionAgent extends Agent {
|
|
21
22
|
definition;
|
|
22
|
-
context;
|
|
23
23
|
static create(options) {
|
|
24
24
|
const definition = createLocalFunctionAgentDefinition(options);
|
|
25
25
|
return new LocalFunctionAgent_1(definition);
|
|
26
26
|
}
|
|
27
27
|
constructor(definition, context) {
|
|
28
|
-
super(definition);
|
|
28
|
+
super(definition, context);
|
|
29
29
|
this.definition = definition;
|
|
30
|
-
this.context = context;
|
|
31
30
|
}
|
|
32
|
-
async
|
|
31
|
+
async process(input, options) {
|
|
33
32
|
const { definition: { function: func }, context, } = this;
|
|
34
33
|
if (!func)
|
|
35
34
|
throw new Error('Function is required');
|
|
36
35
|
if (!context)
|
|
37
36
|
throw new Error('Context is required');
|
|
38
|
-
const result =
|
|
37
|
+
const result = await func(input, { context, memories: options.memories });
|
|
39
38
|
// TODO: validate the result against the definition.outputs
|
|
40
39
|
return options?.stream
|
|
41
40
|
? result instanceof ReadableStream
|
|
@@ -54,14 +53,17 @@ LocalFunctionAgent = LocalFunctionAgent_1 = __decorate([
|
|
|
54
53
|
], LocalFunctionAgent);
|
|
55
54
|
export { LocalFunctionAgent };
|
|
56
55
|
export function createLocalFunctionAgentDefinition(options) {
|
|
56
|
+
const agentId = options.name || nanoid();
|
|
57
57
|
const inputs = schemaToDataType(options.inputs);
|
|
58
58
|
const outputs = schemaToDataType(options.outputs);
|
|
59
|
+
const memories = toRunnableMemories(agentId, inputs, options.memories || {});
|
|
59
60
|
return {
|
|
60
|
-
id:
|
|
61
|
+
id: agentId,
|
|
61
62
|
name: options.name,
|
|
62
63
|
type: 'local_function_agent',
|
|
63
64
|
inputs,
|
|
64
65
|
outputs,
|
|
66
|
+
memories,
|
|
65
67
|
function: options.function,
|
|
66
68
|
};
|
|
67
69
|
}
|
|
@@ -14,26 +14,25 @@ var PipelineAgent_1;
|
|
|
14
14
|
import { get, isNil } from 'lodash';
|
|
15
15
|
import { nanoid } from 'nanoid';
|
|
16
16
|
import { inject, injectable } from 'tsyringe';
|
|
17
|
+
import { Agent } from './agent';
|
|
17
18
|
import { StreamTextOutputName, TYPES } from './constants';
|
|
18
|
-
import { schemaToDataType } from './data-type-schema';
|
|
19
|
+
import { schemaToDataType } from './definitions/data-type-schema';
|
|
20
|
+
import { toRunnableMemories } from './definitions/memory';
|
|
19
21
|
import logger from './logger';
|
|
20
|
-
import { Runnable, } from './runnable';
|
|
21
22
|
import { runnableResponseStreamToObject } from './utils';
|
|
22
23
|
import { isNonNullable } from './utils/is-non-nullable';
|
|
23
24
|
import { OrderedRecord } from './utils/ordered-map';
|
|
24
|
-
let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends
|
|
25
|
+
let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends Agent {
|
|
25
26
|
definition;
|
|
26
|
-
context;
|
|
27
27
|
static create(options) {
|
|
28
28
|
const definition = createPipelineAgentDefinition(options);
|
|
29
29
|
return new PipelineAgent_1(definition);
|
|
30
30
|
}
|
|
31
31
|
constructor(definition, context) {
|
|
32
|
-
super(definition);
|
|
32
|
+
super(definition, context);
|
|
33
33
|
this.definition = definition;
|
|
34
|
-
this.context = context;
|
|
35
34
|
}
|
|
36
|
-
async
|
|
35
|
+
async process(input, options) {
|
|
37
36
|
// TODO: validate the input against the definition
|
|
38
37
|
const { definition, context } = this;
|
|
39
38
|
if (!context)
|
|
@@ -46,12 +45,15 @@ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends Runnable {
|
|
|
46
45
|
async start(controller) {
|
|
47
46
|
try {
|
|
48
47
|
// NOTE: 将 input 转换为 variables,其中 key 为 inputId,value 为 input 的值
|
|
49
|
-
const variables =
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
const variables = {
|
|
49
|
+
...options.memories,
|
|
50
|
+
...Object.fromEntries(OrderedRecord.map(definition.inputs, (i) => {
|
|
51
|
+
const value = input[i.name || i.id];
|
|
52
|
+
if (isNil(value))
|
|
53
|
+
return null;
|
|
54
|
+
return [i.id, value];
|
|
55
|
+
}).filter(isNonNullable)),
|
|
56
|
+
};
|
|
55
57
|
const outputs = OrderedRecord.toArray(definition.outputs);
|
|
56
58
|
const textStreamOutput = outputs.find((i) => i.name === StreamTextOutputName);
|
|
57
59
|
let result = {};
|
|
@@ -139,15 +141,17 @@ PipelineAgent = PipelineAgent_1 = __decorate([
|
|
|
139
141
|
], PipelineAgent);
|
|
140
142
|
export { PipelineAgent };
|
|
141
143
|
export function createPipelineAgentDefinition(options) {
|
|
144
|
+
const agentId = options.name || nanoid();
|
|
142
145
|
const inputs = schemaToDataType(options.inputs);
|
|
146
|
+
const memories = toRunnableMemories(agentId, inputs, options.memories || {});
|
|
143
147
|
const processes = OrderedRecord.fromArray([]);
|
|
144
|
-
for (const p of options.processes
|
|
148
|
+
for (const [name, p] of Object.entries(options.processes)) {
|
|
145
149
|
OrderedRecord.push(processes, {
|
|
146
150
|
id: nanoid(),
|
|
147
|
-
name:
|
|
151
|
+
name: name || p.runnable?.name,
|
|
148
152
|
runnable: { id: p.runnable.id },
|
|
149
153
|
input: Object.fromEntries(OrderedRecord.map(p.runnable.definition.inputs, (inputOfProcess) => {
|
|
150
|
-
const i = p.input
|
|
154
|
+
const i = p.input[inputOfProcess.name || inputOfProcess.id];
|
|
151
155
|
if (!i) {
|
|
152
156
|
if (inputOfProcess.required) {
|
|
153
157
|
throw new Error(`Input ${inputOfProcess.name || inputOfProcess.id} for case ${p.runnable.name || p.runnable.id} is required`);
|
|
@@ -158,7 +162,7 @@ export function createPipelineAgentDefinition(options) {
|
|
|
158
162
|
const inputFromPipeline = OrderedRecord.find(inputs, (input) => input.name === i.fromVariable) ||
|
|
159
163
|
OrderedRecord.find(processes, (p) => p.name === i.fromVariable);
|
|
160
164
|
if (!inputFromPipeline)
|
|
161
|
-
throw new Error(`Input ${i.fromVariable} not found`);
|
|
165
|
+
throw new Error(`Input ${i.fromVariable.toString()} not found`);
|
|
162
166
|
return [
|
|
163
167
|
inputOfProcess.id,
|
|
164
168
|
{
|
|
@@ -179,10 +183,11 @@ export function createPipelineAgentDefinition(options) {
|
|
|
179
183
|
return { ...output, from: 'variable', fromVariableId: from.id, fromVariablePropPath };
|
|
180
184
|
}));
|
|
181
185
|
return {
|
|
182
|
-
id:
|
|
186
|
+
id: agentId,
|
|
183
187
|
name: options.name,
|
|
184
188
|
type: 'pipeline_agent',
|
|
185
189
|
inputs,
|
|
190
|
+
memories,
|
|
186
191
|
outputs,
|
|
187
192
|
processes,
|
|
188
193
|
};
|
package/lib/esm/runnable.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { OrderedRecord } from './utils/ordered-map';
|
|
2
2
|
export class Runnable {
|
|
3
3
|
definition;
|
|
4
|
-
|
|
4
|
+
context;
|
|
5
|
+
constructor(definition, context) {
|
|
5
6
|
this.definition = definition;
|
|
7
|
+
this.context = context;
|
|
6
8
|
this.inputs = Object.fromEntries(OrderedRecord.map(definition.inputs, (i) => [i.name || i.id, i]));
|
|
7
9
|
this.outputs = Object.fromEntries(OrderedRecord.map(definition.outputs, (i) => [i.name || i.id, i]));
|
|
8
10
|
}
|