@aigne/core 0.4.205-1 → 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 +37 -6
- package/lib/cjs/definitions/data-type-schema.js +2 -2
- package/lib/cjs/definitions/data-type.js +2 -0
- package/lib/cjs/definitions/memory.js +2 -2
- 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 +23 -55
- 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/pipeline-agent.js +29 -32
- 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 +36 -14
- package/lib/esm/agent.js +39 -8
- package/lib/esm/definitions/data-type-schema.js +2 -2
- package/lib/esm/definitions/data-type.js +1 -0
- package/lib/esm/definitions/memory.js +2 -2
- 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 +23 -54
- 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/pipeline-agent.js +29 -31
- 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 +34 -14
- package/lib/types/agent.d.ts +9 -10
- package/lib/types/context.d.ts +2 -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/memorable.d.ts +1 -1
- package/lib/types/pipeline-agent.d.ts +40 -33
- package/lib/types/runnable.d.ts +3 -3
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/utils/index.d.ts +5 -2
- package/lib/types/utils/message-utils.d.ts +3 -3
- 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
|
@@ -24,18 +24,23 @@ class Agent extends runnable_1.Runnable {
|
|
|
24
24
|
* Load memories that are defined in the agent definition.
|
|
25
25
|
* @param input The agent input.
|
|
26
26
|
* @param context The AIGNE context.
|
|
27
|
-
* @returns A dictionary of memories, where the key is the memory name and the value is an array of memory items.
|
|
27
|
+
* @returns A dictionary of memories, where the key is the memory id or name and the value is an array of memory items.
|
|
28
28
|
*/
|
|
29
29
|
async loadMemories(input, context) {
|
|
30
30
|
const { memories } = this.definition;
|
|
31
31
|
const { userId, sessionId } = context?.state ?? {};
|
|
32
|
-
return Object.fromEntries((await Promise.all(utils_1.OrderedRecord.map(memories, async ({ name, memory, query, options }) => {
|
|
32
|
+
return Object.fromEntries((await Promise.all(utils_1.OrderedRecord.map(memories, async ({ id, name, memory, query, options }) => {
|
|
33
33
|
if (!name || !memory)
|
|
34
34
|
return null;
|
|
35
35
|
const q = await this.getMemoryQuery(input, query);
|
|
36
36
|
const { results: memories } = await memory.search(q, { ...options, userId, sessionId });
|
|
37
|
-
return [
|
|
38
|
-
|
|
37
|
+
return [
|
|
38
|
+
[id, memories],
|
|
39
|
+
[name, memories],
|
|
40
|
+
];
|
|
41
|
+
})))
|
|
42
|
+
.flat()
|
|
43
|
+
.filter(utils_1.isNonNullable));
|
|
39
44
|
}
|
|
40
45
|
/**
|
|
41
46
|
* Update memories by user messages and assistant responses.
|
|
@@ -49,12 +54,38 @@ class Agent extends runnable_1.Runnable {
|
|
|
49
54
|
logger_1.default.warn(`Memory is not defined in agent ${this.name || this.id}`);
|
|
50
55
|
return;
|
|
51
56
|
}
|
|
52
|
-
|
|
57
|
+
await memory.add(messages, { userId, sessionId });
|
|
53
58
|
}));
|
|
54
59
|
}
|
|
55
60
|
async run(input, options) {
|
|
56
61
|
const memories = await this.loadMemories(input, this.context);
|
|
57
|
-
|
|
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
|
|
58
89
|
}
|
|
59
90
|
}
|
|
60
91
|
exports.Agent = Agent;
|
|
@@ -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}`);
|
|
@@ -12,8 +12,8 @@ function toRunnableMemories(agentName, inputs, memories) {
|
|
|
12
12
|
throw new Error(`LLMAgent ${agentName} -> Memory ${name} -> Query variable ${query.fromVariable.toString()} not found`);
|
|
13
13
|
return {
|
|
14
14
|
id: name || (0, nanoid_1.nanoid)(),
|
|
15
|
-
name
|
|
16
|
-
memory
|
|
15
|
+
name,
|
|
16
|
+
memory,
|
|
17
17
|
query: queryFromVariable ? { from: 'variable', fromVariableId: queryFromVariable.id } : undefined,
|
|
18
18
|
options,
|
|
19
19
|
};
|
|
@@ -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
|
|
@@ -104,16 +74,14 @@ let LLMAgent = LLMAgent_1 = class LLMAgent extends agent_1.Agent {
|
|
|
104
74
|
type: 'json_schema',
|
|
105
75
|
jsonSchema: {
|
|
106
76
|
name: 'output',
|
|
107
|
-
schema
|
|
77
|
+
schema,
|
|
108
78
|
strict: true,
|
|
109
79
|
},
|
|
110
80
|
},
|
|
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
|
}
|