@aigne/core 0.4.205 → 0.4.206-0
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 +27 -1
- package/lib/cjs/data-type-schema.js +46 -0
- package/lib/cjs/definitions/data-type-schema.js +2 -2
- package/lib/cjs/definitions/data-type.js +2 -0
- package/lib/cjs/function-agent.js +34 -29
- package/lib/cjs/function-runner.js +6 -4
- package/lib/cjs/index.js +1 -1
- package/lib/cjs/llm-agent.js +22 -54
- package/lib/cjs/llm-decision-agent.js +8 -12
- package/lib/cjs/llm-model.js +2 -2
- package/lib/cjs/local-function-agent.js +7 -21
- package/lib/cjs/memory.js +32 -0
- package/lib/cjs/pipeline-agent.js +9 -19
- package/lib/cjs/runnable.js +1 -0
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/index.js +5 -2
- package/lib/cjs/utils/stream-utils.js +35 -13
- package/lib/esm/agent.js +29 -3
- package/lib/esm/data-type-schema.js +43 -0
- package/lib/esm/definitions/data-type-schema.js +2 -2
- package/lib/esm/definitions/data-type.js +1 -0
- package/lib/esm/function-agent.js +33 -28
- package/lib/esm/function-runner.js +6 -4
- package/lib/esm/index.js +1 -1
- package/lib/esm/llm-agent.js +22 -53
- package/lib/esm/llm-decision-agent.js +8 -11
- package/lib/esm/llm-model.js +2 -2
- package/lib/esm/local-function-agent.js +7 -20
- package/lib/esm/memory.js +27 -0
- package/lib/esm/pipeline-agent.js +9 -18
- package/lib/esm/runnable.js +1 -0
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/index.js +5 -2
- package/lib/esm/utils/stream-utils.js +33 -13
- package/lib/types/agent.d.ts +8 -9
- package/lib/types/context.d.ts +2 -0
- package/lib/types/data-type-schema.d.ts +46 -0
- package/lib/types/definitions/data-type-schema.d.ts +7 -5
- package/lib/types/definitions/data-type.d.ts +32 -0
- package/lib/types/function-agent.d.ts +33 -20
- package/lib/types/function-runner.d.ts +20 -7
- package/lib/types/index.d.ts +1 -1
- package/lib/types/llm-agent.d.ts +27 -30
- package/lib/types/llm-decision-agent.d.ts +15 -22
- package/lib/types/llm-model.d.ts +2 -2
- package/lib/types/local-function-agent.d.ts +31 -34
- package/lib/types/memory.d.ts +184 -0
- package/lib/types/pipeline-agent.d.ts +21 -55
- package/lib/types/runnable.d.ts +1 -1
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/utils/index.d.ts +5 -2
- package/lib/types/utils/stream-utils.d.ts +5 -3
- package/lib/types/utils/union.d.ts +1 -2
- package/package.json +3 -2
package/lib/cjs/agent.js
CHANGED
|
@@ -59,7 +59,33 @@ class Agent extends runnable_1.Runnable {
|
|
|
59
59
|
}
|
|
60
60
|
async run(input, options) {
|
|
61
61
|
const memories = await this.loadMemories(input, this.context);
|
|
62
|
-
|
|
62
|
+
const processResult = await this.process(input, { ...options, memories });
|
|
63
|
+
if (options?.stream) {
|
|
64
|
+
const stream = processResult instanceof ReadableStream ||
|
|
65
|
+
(0, utils_1.isAsyncGenerator)(processResult)
|
|
66
|
+
? processResult
|
|
67
|
+
: (0, utils_1.objectToRunnableResponseStream)(processResult);
|
|
68
|
+
return (0, utils_1.extractOutputsFromRunnableOutput)(stream, async (result) => {
|
|
69
|
+
// TODO: validate result against outputs schema
|
|
70
|
+
await this.onResult(result);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const result = processResult instanceof ReadableStream
|
|
74
|
+
? await (0, utils_1.runnableResponseStreamToObject)(processResult)
|
|
75
|
+
: Symbol.asyncIterator in processResult
|
|
76
|
+
? await (0, utils_1.runnableResponseStreamToObject)(processResult)
|
|
77
|
+
: processResult;
|
|
78
|
+
// TODO: validate result against outputs schema
|
|
79
|
+
await this.onResult(result);
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Hook that is called before the agent result is returned.
|
|
84
|
+
* @param _result The agent result.
|
|
85
|
+
*/
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
87
|
+
async onResult(_result) {
|
|
88
|
+
// Override this method to perform additional operations before the result is returned
|
|
63
89
|
}
|
|
64
90
|
}
|
|
65
91
|
exports.Agent = Agent;
|
|
@@ -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
|
+
}
|
|
@@ -30,13 +30,13 @@ function schemaToDataType(dataType) {
|
|
|
30
30
|
return {
|
|
31
31
|
...base,
|
|
32
32
|
type: 'object',
|
|
33
|
-
properties: schemaToDataType(schema.properties),
|
|
33
|
+
properties: schema.properties && schemaToDataType(schema.properties),
|
|
34
34
|
};
|
|
35
35
|
case 'array':
|
|
36
36
|
return {
|
|
37
37
|
...base,
|
|
38
38
|
type: 'array',
|
|
39
|
-
items: utils_1.OrderedRecord.find(schemaToDataType({ items: schema.items }), (i) => i.name === 'items'),
|
|
39
|
+
items: schema.items && utils_1.OrderedRecord.find(schemaToDataType({ items: schema.items }), (i) => i.name === 'items'),
|
|
40
40
|
};
|
|
41
41
|
default: {
|
|
42
42
|
throw new Error(`Unknown data type: ${schema.type}`);
|
|
@@ -11,60 +11,65 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
|
-
var FunctionAgent_1;
|
|
15
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
15
|
exports.FunctionAgent = void 0;
|
|
17
|
-
exports.
|
|
16
|
+
exports.create = create;
|
|
18
17
|
const nanoid_1 = require("nanoid");
|
|
19
18
|
const tsyringe_1 = require("tsyringe");
|
|
19
|
+
const agent_1 = require("./agent");
|
|
20
20
|
const constants_1 = require("./constants");
|
|
21
21
|
const data_type_schema_1 = require("./definitions/data-type-schema");
|
|
22
|
+
const memory_1 = require("./definitions/memory");
|
|
22
23
|
const function_runner_1 = require("./function-runner");
|
|
23
|
-
|
|
24
|
-
const utils_1 = require("./utils");
|
|
25
|
-
let FunctionAgent = FunctionAgent_1 = class FunctionAgent extends runnable_1.Runnable {
|
|
24
|
+
let FunctionAgent = class FunctionAgent extends agent_1.Agent {
|
|
26
25
|
definition;
|
|
27
26
|
runner;
|
|
28
|
-
static create
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
constructor(definition, runner) {
|
|
33
|
-
super(definition);
|
|
27
|
+
static create = create;
|
|
28
|
+
constructor(definition, context, runner) {
|
|
29
|
+
super(definition, context);
|
|
34
30
|
this.definition = definition;
|
|
35
31
|
this.runner = runner;
|
|
32
|
+
this.runner ??= context?.resolveDependency(constants_1.TYPES.functionRunner);
|
|
36
33
|
}
|
|
37
|
-
async
|
|
38
|
-
const { definition: { language, code, ...definition }, runner, } = this;
|
|
34
|
+
async process(input, options) {
|
|
35
|
+
const { definition: { language, code, ...definition }, runner, context, } = this;
|
|
39
36
|
if (!runner)
|
|
40
37
|
throw new Error('Function runner is required');
|
|
41
|
-
if (!
|
|
42
|
-
throw new Error('
|
|
43
|
-
|
|
38
|
+
if (!code)
|
|
39
|
+
throw new Error('Code is required');
|
|
40
|
+
if (!context)
|
|
41
|
+
throw new Error('Context is required');
|
|
42
|
+
return await runner.run({
|
|
44
43
|
name: definition.name || definition.id,
|
|
45
44
|
language,
|
|
46
45
|
code,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
input,
|
|
47
|
+
memories: options.memories,
|
|
48
|
+
context: { state: context.state },
|
|
49
|
+
}, { stream: true });
|
|
51
50
|
}
|
|
52
51
|
};
|
|
53
52
|
exports.FunctionAgent = FunctionAgent;
|
|
54
|
-
exports.FunctionAgent = FunctionAgent =
|
|
53
|
+
exports.FunctionAgent = FunctionAgent = __decorate([
|
|
55
54
|
(0, tsyringe_1.injectable)(),
|
|
56
55
|
__param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
|
|
57
|
-
__param(1, (0, tsyringe_1.inject)(constants_1.TYPES.
|
|
58
|
-
|
|
56
|
+
__param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
|
|
57
|
+
__param(2, (0, tsyringe_1.inject)(constants_1.TYPES.functionRunner)),
|
|
58
|
+
__metadata("design:paramtypes", [Object, Object, function_runner_1.FunctionRunner])
|
|
59
59
|
], FunctionAgent);
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
function create({ context, ...options }) {
|
|
61
|
+
const agentId = options.name || (0, nanoid_1.nanoid)();
|
|
62
|
+
const inputs = (0, data_type_schema_1.schemaToDataType)(options.inputs);
|
|
63
|
+
const outputs = (0, data_type_schema_1.schemaToDataType)(options.outputs);
|
|
64
|
+
const memories = (0, memory_1.toRunnableMemories)(agentId, inputs, options.memories ?? {});
|
|
65
|
+
return new FunctionAgent({
|
|
66
|
+
id: agentId,
|
|
63
67
|
name: options.name,
|
|
64
68
|
type: 'function_agent',
|
|
65
|
-
inputs
|
|
66
|
-
outputs
|
|
69
|
+
inputs,
|
|
70
|
+
outputs,
|
|
71
|
+
memories,
|
|
67
72
|
language: options.language,
|
|
68
73
|
code: options.code,
|
|
69
|
-
};
|
|
74
|
+
}, context);
|
|
70
75
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FunctionRunner = void 0;
|
|
4
|
-
const
|
|
4
|
+
const agent_1 = require("./agent");
|
|
5
5
|
const utils_1 = require("./utils");
|
|
6
|
-
class FunctionRunner extends
|
|
6
|
+
class FunctionRunner extends agent_1.Agent {
|
|
7
7
|
constructor(context) {
|
|
8
8
|
super({
|
|
9
9
|
id: 'function_runner',
|
|
@@ -12,9 +12,11 @@ class FunctionRunner extends runnable_1.Runnable {
|
|
|
12
12
|
description: 'Run a function',
|
|
13
13
|
inputs: utils_1.OrderedRecord.fromArray([
|
|
14
14
|
{ id: 'name', name: 'name', type: 'string', required: true },
|
|
15
|
-
{ id: 'language', name: 'language', type: 'string'
|
|
15
|
+
{ id: 'language', name: 'language', type: 'string' },
|
|
16
16
|
{ id: 'code', name: 'code', type: 'string', required: true },
|
|
17
|
-
{ id: '
|
|
17
|
+
{ id: 'input', name: 'input', type: 'object', required: true },
|
|
18
|
+
{ id: 'memories', name: 'memories', type: 'object', required: true },
|
|
19
|
+
{ id: 'context', name: 'context', type: 'object', required: true },
|
|
18
20
|
]),
|
|
19
21
|
outputs: utils_1.OrderedRecord.fromArray([
|
|
20
22
|
{
|
package/lib/cjs/index.js
CHANGED
|
@@ -16,7 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./utils"), exports);
|
|
18
18
|
__exportStar(require("./constants"), exports);
|
|
19
|
-
__exportStar(require("./data-type"), exports);
|
|
19
|
+
__exportStar(require("./definitions/data-type"), exports);
|
|
20
20
|
__exportStar(require("./definitions/data-type-schema"), exports);
|
|
21
21
|
__exportStar(require("./context"), exports);
|
|
22
22
|
__exportStar(require("./runnable"), exports);
|
package/lib/cjs/llm-agent.js
CHANGED
|
@@ -11,10 +11,8 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
|
-
var LLMAgent_1;
|
|
15
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
15
|
exports.LLMAgent = void 0;
|
|
17
|
-
exports.createLLMAgentDefinition = createLLMAgentDefinition;
|
|
18
16
|
const nanoid_1 = require("nanoid");
|
|
19
17
|
const tsyringe_1 = require("tsyringe");
|
|
20
18
|
const agent_1 = require("./agent");
|
|
@@ -22,24 +20,21 @@ const constants_1 = require("./constants");
|
|
|
22
20
|
const data_type_schema_1 = require("./definitions/data-type-schema");
|
|
23
21
|
const memory_1 = require("./definitions/memory");
|
|
24
22
|
const llm_model_1 = require("./llm-model");
|
|
25
|
-
const utils_1 = require("./utils");
|
|
26
23
|
const message_utils_1 = require("./utils/message-utils");
|
|
27
24
|
const mustache_utils_1 = require("./utils/mustache-utils");
|
|
28
25
|
const ordered_map_1 = require("./utils/ordered-map");
|
|
29
26
|
const structured_output_schema_1 = require("./utils/structured-output-schema");
|
|
30
|
-
let LLMAgent =
|
|
27
|
+
let LLMAgent = class LLMAgent extends agent_1.Agent {
|
|
31
28
|
definition;
|
|
32
29
|
model;
|
|
33
|
-
static create
|
|
34
|
-
const definition = createLLMAgentDefinition(options);
|
|
35
|
-
return new LLMAgent_1(definition);
|
|
36
|
-
}
|
|
30
|
+
static create = create;
|
|
37
31
|
constructor(definition, context, model) {
|
|
38
32
|
super(definition, context);
|
|
39
33
|
this.definition = definition;
|
|
40
34
|
this.model = model;
|
|
35
|
+
this.model ??= context?.resolveDependency(constants_1.TYPES.llmModel);
|
|
41
36
|
}
|
|
42
|
-
async process(input, options) {
|
|
37
|
+
async *process(input, options) {
|
|
43
38
|
const { definition, model } = this;
|
|
44
39
|
if (!model)
|
|
45
40
|
throw new Error('LLM model is required');
|
|
@@ -48,46 +43,21 @@ let LLMAgent = LLMAgent_1 = class LLMAgent extends agent_1.Agent {
|
|
|
48
43
|
messages: messagesWithMemory,
|
|
49
44
|
modelOptions: definition.modelOptions,
|
|
50
45
|
};
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
{ role: 'assistant', content: (0, mustache_utils_1.renderMessage)('{{text}}\n{{json}}', { text, json }).trim() },
|
|
59
|
-
]);
|
|
60
|
-
};
|
|
61
|
-
if (options?.stream) {
|
|
62
|
-
let $text = '';
|
|
63
|
-
return new ReadableStream({
|
|
64
|
-
start: async (controller) => {
|
|
65
|
-
try {
|
|
66
|
-
if (textOutput) {
|
|
67
|
-
for await (const chunk of textOutput) {
|
|
68
|
-
$text += chunk.$text || '';
|
|
69
|
-
controller.enqueue({ $text: chunk.$text });
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
const json = await jsonOutput;
|
|
73
|
-
controller.enqueue({ delta: json });
|
|
74
|
-
await updateMemories($text || undefined, json);
|
|
75
|
-
}
|
|
76
|
-
catch (error) {
|
|
77
|
-
controller.error(error);
|
|
78
|
-
}
|
|
79
|
-
finally {
|
|
80
|
-
controller.close();
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
});
|
|
46
|
+
let $text = '';
|
|
47
|
+
const hasTextOutput = ordered_map_1.OrderedRecord.find(definition.outputs, (i) => i.name === constants_1.StreamTextOutputName);
|
|
48
|
+
if (hasTextOutput) {
|
|
49
|
+
for await (const chunk of await this.runWithTextOutput(llmInputs)) {
|
|
50
|
+
$text += chunk.$text || '';
|
|
51
|
+
yield { $text: chunk.$text };
|
|
52
|
+
}
|
|
84
53
|
}
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
54
|
+
const json = await this.runWithStructuredOutput(llmInputs);
|
|
55
|
+
if (json)
|
|
56
|
+
yield { delta: json };
|
|
57
|
+
await this.updateMemories([
|
|
58
|
+
...originalMessages,
|
|
59
|
+
{ role: 'assistant', content: (0, mustache_utils_1.renderMessage)('{{$text}}\n{{json}}', { $text, json }).trim() },
|
|
88
60
|
]);
|
|
89
|
-
await updateMemories($text, json);
|
|
90
|
-
return { $text, ...json };
|
|
91
61
|
}
|
|
92
62
|
async runWithStructuredOutput(llmInputs) {
|
|
93
63
|
const jsonOutputs = ordered_map_1.OrderedRecord.filter(this.definition.outputs, (i) => i.name !== constants_1.StreamTextOutputName // ignore `$text` output
|
|
@@ -111,9 +81,7 @@ let LLMAgent = LLMAgent_1 = class LLMAgent extends agent_1.Agent {
|
|
|
111
81
|
});
|
|
112
82
|
if (!response.$text)
|
|
113
83
|
throw new Error('No text in JSON mode response');
|
|
114
|
-
|
|
115
|
-
// TODO: validate json with outputJsonSchema
|
|
116
|
-
return json;
|
|
84
|
+
return JSON.parse(response.$text);
|
|
117
85
|
}
|
|
118
86
|
async runWithTextOutput(llmInputs) {
|
|
119
87
|
const { model } = this;
|
|
@@ -123,7 +91,7 @@ let LLMAgent = LLMAgent_1 = class LLMAgent extends agent_1.Agent {
|
|
|
123
91
|
}
|
|
124
92
|
};
|
|
125
93
|
exports.LLMAgent = LLMAgent;
|
|
126
|
-
exports.LLMAgent = LLMAgent =
|
|
94
|
+
exports.LLMAgent = LLMAgent = __decorate([
|
|
127
95
|
(0, tsyringe_1.injectable)(),
|
|
128
96
|
__param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
|
|
129
97
|
__param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
|
|
@@ -135,7 +103,7 @@ exports.LLMAgent = LLMAgent = LLMAgent_1 = __decorate([
|
|
|
135
103
|
* @param options Options to create LLMAgent.
|
|
136
104
|
* @returns LLMAgent definition.
|
|
137
105
|
*/
|
|
138
|
-
function
|
|
106
|
+
function create({ context, ...options }) {
|
|
139
107
|
const agentId = options.name || (0, nanoid_1.nanoid)();
|
|
140
108
|
const inputs = (0, data_type_schema_1.schemaToDataType)(options.inputs);
|
|
141
109
|
const outputs = (0, data_type_schema_1.schemaToDataType)(options.outputs);
|
|
@@ -151,7 +119,7 @@ function createLLMAgentDefinition(options) {
|
|
|
151
119
|
role: i.role,
|
|
152
120
|
content: i.content,
|
|
153
121
|
})));
|
|
154
|
-
return {
|
|
122
|
+
return new LLMAgent({
|
|
155
123
|
id: agentId,
|
|
156
124
|
name: options.name,
|
|
157
125
|
type: 'llm_agent',
|
|
@@ -161,5 +129,5 @@ function createLLMAgentDefinition(options) {
|
|
|
161
129
|
memories,
|
|
162
130
|
modelOptions: options.modelOptions,
|
|
163
131
|
messages,
|
|
164
|
-
};
|
|
132
|
+
}, context);
|
|
165
133
|
}
|
|
@@ -11,10 +11,8 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
|
-
var LLMDecisionAgent_1;
|
|
15
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
15
|
exports.LLMDecisionAgent = void 0;
|
|
17
|
-
exports.createLLMDecisionAgentDefinition = createLLMDecisionAgentDefinition;
|
|
18
16
|
const nanoid_1 = require("nanoid");
|
|
19
17
|
const tsyringe_1 = require("tsyringe");
|
|
20
18
|
const agent_1 = require("./agent");
|
|
@@ -23,17 +21,15 @@ const memory_1 = require("./definitions/memory");
|
|
|
23
21
|
const llm_model_1 = require("./llm-model");
|
|
24
22
|
const utils_1 = require("./utils");
|
|
25
23
|
const message_utils_1 = require("./utils/message-utils");
|
|
26
|
-
let LLMDecisionAgent =
|
|
24
|
+
let LLMDecisionAgent = class LLMDecisionAgent extends agent_1.Agent {
|
|
27
25
|
definition;
|
|
28
26
|
model;
|
|
29
|
-
static create
|
|
30
|
-
const definition = createLLMDecisionAgentDefinition(options);
|
|
31
|
-
return new LLMDecisionAgent_1(definition);
|
|
32
|
-
}
|
|
27
|
+
static create = create;
|
|
33
28
|
constructor(definition, context, model) {
|
|
34
29
|
super(definition, context);
|
|
35
30
|
this.definition = definition;
|
|
36
31
|
this.model = model;
|
|
32
|
+
this.model ??= context?.resolveDependency(constants_1.TYPES.llmModel);
|
|
37
33
|
}
|
|
38
34
|
async process(input, options) {
|
|
39
35
|
const { definition, context, model } = this;
|
|
@@ -77,7 +73,7 @@ let LLMDecisionAgent = LLMDecisionAgent_1 = class LLMDecisionAgent extends agent
|
|
|
77
73
|
if (!caseToCall)
|
|
78
74
|
throw new Error('Case not found');
|
|
79
75
|
// TODO: check result structure and omit undefined values
|
|
80
|
-
const output =
|
|
76
|
+
const output = await caseToCall.runnable.run(input, { stream: true });
|
|
81
77
|
return (0, utils_1.extractOutputsFromRunnableOutput)(output, ({ $text, ...json }) => this.updateMemories([
|
|
82
78
|
...originalMessages,
|
|
83
79
|
{ role: 'assistant', content: (0, utils_1.renderMessage)('{{$text}}\n{{json}}', { $text, json }).trim() },
|
|
@@ -85,14 +81,14 @@ let LLMDecisionAgent = LLMDecisionAgent_1 = class LLMDecisionAgent extends agent
|
|
|
85
81
|
}
|
|
86
82
|
};
|
|
87
83
|
exports.LLMDecisionAgent = LLMDecisionAgent;
|
|
88
|
-
exports.LLMDecisionAgent = LLMDecisionAgent =
|
|
84
|
+
exports.LLMDecisionAgent = LLMDecisionAgent = __decorate([
|
|
89
85
|
(0, tsyringe_1.injectable)(),
|
|
90
86
|
__param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
|
|
91
87
|
__param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
|
|
92
88
|
__param(2, (0, tsyringe_1.inject)(constants_1.TYPES.llmModel)),
|
|
93
89
|
__metadata("design:paramtypes", [Object, Object, llm_model_1.LLMModel])
|
|
94
90
|
], LLMDecisionAgent);
|
|
95
|
-
function
|
|
91
|
+
function create({ context, ...options }) {
|
|
96
92
|
const agentId = options.name || (0, nanoid_1.nanoid)();
|
|
97
93
|
const cases = utils_1.OrderedRecord.fromArray(Object.entries(options.cases).map(([name, c]) => ({
|
|
98
94
|
id: (0, nanoid_1.nanoid)(),
|
|
@@ -114,7 +110,7 @@ function createLLMDecisionAgentDefinition(options) {
|
|
|
114
110
|
role: i.role,
|
|
115
111
|
content: i.content,
|
|
116
112
|
})));
|
|
117
|
-
return {
|
|
113
|
+
return new LLMDecisionAgent({
|
|
118
114
|
id: agentId,
|
|
119
115
|
name: options.name,
|
|
120
116
|
type: 'llm_decision_agent',
|
|
@@ -125,5 +121,5 @@ function createLLMDecisionAgentDefinition(options) {
|
|
|
125
121
|
memories,
|
|
126
122
|
modelOptions: options.modelOptions,
|
|
127
123
|
cases,
|
|
128
|
-
};
|
|
124
|
+
}, context);
|
|
129
125
|
}
|
package/lib/cjs/llm-model.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LLMModel = void 0;
|
|
4
|
-
const
|
|
4
|
+
const agent_1 = require("./agent");
|
|
5
5
|
const utils_1 = require("./utils");
|
|
6
|
-
class LLMModel extends
|
|
6
|
+
class LLMModel extends agent_1.Agent {
|
|
7
7
|
constructor(context) {
|
|
8
8
|
super({
|
|
9
9
|
id: 'llm_model',
|
|
@@ -11,23 +11,17 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
|
-
var LocalFunctionAgent_1;
|
|
15
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
15
|
exports.LocalFunctionAgent = void 0;
|
|
17
|
-
exports.createLocalFunctionAgentDefinition = createLocalFunctionAgentDefinition;
|
|
18
16
|
const nanoid_1 = require("nanoid");
|
|
19
17
|
const tsyringe_1 = require("tsyringe");
|
|
20
18
|
const agent_1 = require("./agent");
|
|
21
19
|
const constants_1 = require("./constants");
|
|
22
20
|
const data_type_schema_1 = require("./definitions/data-type-schema");
|
|
23
21
|
const memory_1 = require("./definitions/memory");
|
|
24
|
-
|
|
25
|
-
let LocalFunctionAgent = LocalFunctionAgent_1 = class LocalFunctionAgent extends agent_1.Agent {
|
|
22
|
+
let LocalFunctionAgent = class LocalFunctionAgent extends agent_1.Agent {
|
|
26
23
|
definition;
|
|
27
|
-
static create
|
|
28
|
-
const definition = createLocalFunctionAgentDefinition(options);
|
|
29
|
-
return new LocalFunctionAgent_1(definition);
|
|
30
|
-
}
|
|
24
|
+
static create = create;
|
|
31
25
|
constructor(definition, context) {
|
|
32
26
|
super(definition, context);
|
|
33
27
|
this.definition = definition;
|
|
@@ -38,30 +32,22 @@ let LocalFunctionAgent = LocalFunctionAgent_1 = class LocalFunctionAgent extends
|
|
|
38
32
|
throw new Error('Function is required');
|
|
39
33
|
if (!context)
|
|
40
34
|
throw new Error('Context is required');
|
|
41
|
-
|
|
42
|
-
// TODO: validate the result against the definition.outputs
|
|
43
|
-
return options?.stream
|
|
44
|
-
? result instanceof ReadableStream
|
|
45
|
-
? result
|
|
46
|
-
: (0, utils_1.objectToRunnableResponseStream)(result)
|
|
47
|
-
: result instanceof ReadableStream
|
|
48
|
-
? (0, utils_1.runnableResponseStreamToObject)(result)
|
|
49
|
-
: result;
|
|
35
|
+
return await func(input, { context, memories: options.memories });
|
|
50
36
|
}
|
|
51
37
|
};
|
|
52
38
|
exports.LocalFunctionAgent = LocalFunctionAgent;
|
|
53
|
-
exports.LocalFunctionAgent = LocalFunctionAgent =
|
|
39
|
+
exports.LocalFunctionAgent = LocalFunctionAgent = __decorate([
|
|
54
40
|
(0, tsyringe_1.injectable)(),
|
|
55
41
|
__param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
|
|
56
42
|
__param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
|
|
57
43
|
__metadata("design:paramtypes", [Object, Object])
|
|
58
44
|
], LocalFunctionAgent);
|
|
59
|
-
function
|
|
45
|
+
function create({ context, ...options }) {
|
|
60
46
|
const agentId = options.name || (0, nanoid_1.nanoid)();
|
|
61
47
|
const inputs = (0, data_type_schema_1.schemaToDataType)(options.inputs);
|
|
62
48
|
const outputs = (0, data_type_schema_1.schemaToDataType)(options.outputs);
|
|
63
49
|
const memories = (0, memory_1.toRunnableMemories)(agentId, inputs, options.memories || {});
|
|
64
|
-
return {
|
|
50
|
+
return new LocalFunctionAgent({
|
|
65
51
|
id: agentId,
|
|
66
52
|
name: options.name,
|
|
67
53
|
type: 'local_function_agent',
|
|
@@ -69,5 +55,5 @@ function createLocalFunctionAgentDefinition(options) {
|
|
|
69
55
|
outputs,
|
|
70
56
|
memories,
|
|
71
57
|
function: options.function,
|
|
72
|
-
};
|
|
58
|
+
}, context);
|
|
73
59
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MemoryRunner = exports.Memory = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
const runnable_1 = require("./runnable");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
class Memory extends runnable_1.Runnable {
|
|
8
|
+
constructor() {
|
|
9
|
+
super({
|
|
10
|
+
id: 'memory',
|
|
11
|
+
type: 'memory',
|
|
12
|
+
name: 'Memory',
|
|
13
|
+
inputs: utils_1.OrderedRecord.fromArray([]),
|
|
14
|
+
outputs: utils_1.OrderedRecord.fromArray([]),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.Memory = Memory;
|
|
19
|
+
class MemoryRunner extends runnable_1.Runnable {
|
|
20
|
+
constructor(name) {
|
|
21
|
+
const id = `${(0, lodash_1.camelCase)(name)}_runner`;
|
|
22
|
+
super({
|
|
23
|
+
id,
|
|
24
|
+
type: id,
|
|
25
|
+
name: `${(0, lodash_1.startCase)(name)} Runner`,
|
|
26
|
+
description: `${(0, lodash_1.startCase)(name)} Runner`,
|
|
27
|
+
inputs: utils_1.OrderedRecord.fromArray([]),
|
|
28
|
+
outputs: utils_1.OrderedRecord.fromArray([]),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.MemoryRunner = MemoryRunner;
|
|
@@ -14,10 +14,8 @@ 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");
|
|
@@ -26,21 +24,16 @@ const constants_1 = require("./constants");
|
|
|
26
24
|
const data_type_schema_1 = require("./definitions/data-type-schema");
|
|
27
25
|
const memory_1 = require("./definitions/memory");
|
|
28
26
|
const logger_1 = __importDefault(require("./logger"));
|
|
29
|
-
const utils_1 = require("./utils");
|
|
30
27
|
const is_non_nullable_1 = require("./utils/is-non-nullable");
|
|
31
28
|
const ordered_map_1 = require("./utils/ordered-map");
|
|
32
|
-
let PipelineAgent =
|
|
29
|
+
let PipelineAgent = class PipelineAgent extends agent_1.Agent {
|
|
33
30
|
definition;
|
|
34
|
-
static create
|
|
35
|
-
const definition = createPipelineAgentDefinition(options);
|
|
36
|
-
return new PipelineAgent_1(definition);
|
|
37
|
-
}
|
|
31
|
+
static create = create;
|
|
38
32
|
constructor(definition, context) {
|
|
39
33
|
super(definition, context);
|
|
40
34
|
this.definition = definition;
|
|
41
35
|
}
|
|
42
36
|
async process(input, options) {
|
|
43
|
-
// TODO: validate the input against the definition
|
|
44
37
|
const { definition, context } = this;
|
|
45
38
|
if (!context)
|
|
46
39
|
throw new Error('Context is required');
|
|
@@ -48,7 +41,7 @@ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends agent_1.Agent
|
|
|
48
41
|
if (!processes?.$indexes.length) {
|
|
49
42
|
throw new Error('No processes defined');
|
|
50
43
|
}
|
|
51
|
-
|
|
44
|
+
return new ReadableStream({
|
|
52
45
|
async start(controller) {
|
|
53
46
|
try {
|
|
54
47
|
// NOTE: 将 input 转换为 variables,其中 key 为 inputId,value 为 input 的值
|
|
@@ -109,6 +102,8 @@ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends agent_1.Agent
|
|
|
109
102
|
result = Object.fromEntries(ordered_map_1.OrderedRecord.map(definition.outputs, (output) => {
|
|
110
103
|
if (!output.name)
|
|
111
104
|
return null;
|
|
105
|
+
if (output.name === constants_1.StreamTextOutputName)
|
|
106
|
+
return null;
|
|
112
107
|
let value;
|
|
113
108
|
if (output.from === 'variable') {
|
|
114
109
|
const v = variables[output.fromVariableId];
|
|
@@ -133,21 +128,16 @@ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends agent_1.Agent
|
|
|
133
128
|
}
|
|
134
129
|
},
|
|
135
130
|
});
|
|
136
|
-
if (options?.stream) {
|
|
137
|
-
return result;
|
|
138
|
-
}
|
|
139
|
-
// TODO: validate the result against the definition.outputs
|
|
140
|
-
return await (0, utils_1.runnableResponseStreamToObject)(result);
|
|
141
131
|
}
|
|
142
132
|
};
|
|
143
133
|
exports.PipelineAgent = PipelineAgent;
|
|
144
|
-
exports.PipelineAgent = PipelineAgent =
|
|
134
|
+
exports.PipelineAgent = PipelineAgent = __decorate([
|
|
145
135
|
(0, tsyringe_1.injectable)(),
|
|
146
136
|
__param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
|
|
147
137
|
__param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
|
|
148
138
|
__metadata("design:paramtypes", [Object, Object])
|
|
149
139
|
], PipelineAgent);
|
|
150
|
-
function
|
|
140
|
+
function create({ context, ...options }) {
|
|
151
141
|
const agentId = options.name || (0, nanoid_1.nanoid)();
|
|
152
142
|
const inputs = (0, data_type_schema_1.schemaToDataType)(options.inputs);
|
|
153
143
|
const memories = (0, memory_1.toRunnableMemories)(agentId, inputs, options.memories || {});
|
|
@@ -189,7 +179,7 @@ function createPipelineAgentDefinition(options) {
|
|
|
189
179
|
throw new Error(`Output ${output.name} not found in inputs or processes`);
|
|
190
180
|
return { ...output, from: 'variable', fromVariableId: from.id, fromVariablePropPath };
|
|
191
181
|
}));
|
|
192
|
-
return {
|
|
182
|
+
return new PipelineAgent({
|
|
193
183
|
id: agentId,
|
|
194
184
|
name: options.name,
|
|
195
185
|
type: 'pipeline_agent',
|
|
@@ -197,5 +187,5 @@ function createPipelineAgentDefinition(options) {
|
|
|
197
187
|
memories,
|
|
198
188
|
outputs,
|
|
199
189
|
processes,
|
|
200
|
-
};
|
|
190
|
+
}, context);
|
|
201
191
|
}
|