@aigne/core 0.4.211-beta.2 → 1.0.1
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 +18 -11
- package/lib/cjs/constants.js +5 -5
- package/lib/cjs/definitions/data-type-schema.js +12 -11
- package/lib/cjs/definitions/memory.js +3 -1
- package/lib/cjs/function-agent.js +5 -6
- package/lib/cjs/function-runner.js +13 -13
- package/lib/cjs/llm-agent.js +16 -15
- package/lib/cjs/llm-decision-agent.js +15 -13
- package/lib/cjs/llm-model.js +11 -11
- package/lib/cjs/local-function-agent.js +3 -3
- package/lib/cjs/memorable.js +3 -3
- package/lib/cjs/open-api-agent.js +4 -4
- package/lib/cjs/pipeline-agent.js +26 -17
- package/lib/cjs/runnable.js +2 -2
- package/lib/cjs/utils/fetch-open-api.js +9 -8
- package/lib/cjs/utils/fetch.js +3 -2
- package/lib/cjs/utils/is-non-nullable.js +1 -4
- package/lib/cjs/utils/message-utils.js +23 -16
- package/lib/cjs/utils/mustache-utils.js +2 -5
- package/lib/cjs/utils/open-api-parameter.js +19 -22
- package/lib/cjs/utils/stream-utils.js +6 -5
- package/lib/cjs/utils/structured-output-schema.js +10 -9
- package/lib/esm/agent.js +20 -10
- package/lib/esm/constants.js +5 -5
- package/lib/esm/definitions/data-type-schema.js +14 -13
- package/lib/esm/definitions/memory.js +5 -3
- package/lib/esm/function-agent.js +11 -12
- package/lib/esm/function-runner.js +15 -15
- package/lib/esm/index.js +18 -18
- package/lib/esm/llm-agent.js +26 -25
- package/lib/esm/llm-decision-agent.js +22 -20
- package/lib/esm/llm-model.js +13 -13
- package/lib/esm/local-function-agent.js +9 -9
- package/lib/esm/memorable.js +6 -6
- package/lib/esm/open-api-agent.js +11 -11
- package/lib/esm/pipeline-agent.js +35 -23
- package/lib/esm/runnable.js +3 -3
- package/lib/esm/utils/fetch-open-api.js +11 -9
- package/lib/esm/utils/fetch.js +3 -2
- package/lib/esm/utils/index.js +10 -10
- package/lib/esm/utils/is-non-nullable.js +1 -1
- package/lib/esm/utils/message-utils.js +26 -16
- package/lib/esm/utils/mustache-utils.js +2 -2
- package/lib/esm/utils/open-api-parameter.js +21 -21
- package/lib/esm/utils/stream-utils.js +7 -6
- package/lib/esm/utils/structured-output-schema.js +12 -8
- package/lib/types/agent.d.ts +3 -3
- package/lib/types/context.d.ts +2 -2
- package/lib/types/definitions/data-type-schema.d.ts +14 -14
- package/lib/types/definitions/data-type.d.ts +8 -8
- package/lib/types/definitions/memory.d.ts +5 -5
- package/lib/types/definitions/open-api.d.ts +9 -9
- package/lib/types/function-agent.d.ts +9 -9
- package/lib/types/function-runner.d.ts +4 -4
- package/lib/types/index.d.ts +18 -18
- package/lib/types/llm-agent.d.ts +12 -12
- package/lib/types/llm-decision-agent.d.ts +20 -20
- package/lib/types/llm-model.d.ts +12 -12
- package/lib/types/local-function-agent.d.ts +14 -16
- package/lib/types/memorable.d.ts +51 -51
- package/lib/types/open-api-agent.d.ts +12 -12
- package/lib/types/pipeline-agent.d.ts +14 -14
- package/lib/types/runnable.d.ts +5 -5
- package/lib/types/utils/fetch-open-api.d.ts +2 -2
- package/lib/types/utils/index.d.ts +10 -10
- package/lib/types/utils/message-utils.d.ts +4 -4
- package/lib/types/utils/open-api-parameter.d.ts +3 -3
- package/lib/types/utils/runnable-type.d.ts +1 -1
- package/lib/types/utils/stream-utils.d.ts +1 -1
- package/lib/types/utils/structured-output-schema.d.ts +2 -2
- package/package.json +11 -10
- package/tsconfig.json +3 -1
- package/lib/cjs/api-agent.js +0 -70
- package/lib/cjs/data-type-schema.js +0 -46
- package/lib/cjs/data-type.js +0 -2
- package/lib/cjs/definitions/api-parameter.js +0 -10
- package/lib/cjs/memory.js +0 -32
- package/lib/cjs/tsconfig.tsbuildinfo +0 -1
- package/lib/cjs/utils/fetch-api.js +0 -35
- package/lib/cjs/utils/flatten-openapi.js +0 -14
- package/lib/cjs/utils/format-parameter.js +0 -126
- package/lib/esm/api-agent.js +0 -67
- package/lib/esm/data-type-schema.js +0 -43
- package/lib/esm/data-type.js +0 -1
- package/lib/esm/definitions/api-parameter.js +0 -7
- package/lib/esm/memory.js +0 -27
- package/lib/esm/tsconfig.tsbuildinfo +0 -1
- package/lib/esm/utils/fetch-api.js +0 -31
- package/lib/esm/utils/flatten-openapi.js +0 -11
- package/lib/esm/utils/format-parameter.js +0 -116
- package/lib/types/api-agent.d.ts +0 -53
- package/lib/types/data-type-schema.d.ts +0 -46
- package/lib/types/data-type.d.ts +0 -32
- package/lib/types/definitions/api-parameter.d.ts +0 -55
- package/lib/types/memory.d.ts +0 -184
- package/lib/types/tsconfig.tsbuildinfo +0 -1
- package/lib/types/utils/fetch-api.d.ts +0 -3
- package/lib/types/utils/flatten-openapi.d.ts +0 -25
- package/lib/types/utils/format-parameter.d.ts +0 -6
package/lib/cjs/agent.js
CHANGED
|
@@ -1,24 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.Agent = void 0;
|
|
7
|
-
const logger_1 =
|
|
4
|
+
const logger_1 = require("./logger");
|
|
8
5
|
const runnable_1 = require("./runnable");
|
|
9
6
|
const utils_1 = require("./utils");
|
|
10
7
|
class Agent extends runnable_1.Runnable {
|
|
11
8
|
async getMemoryQuery(input, query) {
|
|
12
|
-
if (query?.from ===
|
|
9
|
+
if (query?.from === "variable") {
|
|
13
10
|
const i = utils_1.OrderedRecord.find(this.definition.inputs, (i) => i.id === query.fromVariableId);
|
|
14
11
|
if (!i)
|
|
15
12
|
throw new Error(`Input variable ${query.fromVariableId} not found`);
|
|
16
13
|
const value = input[i.name];
|
|
17
|
-
return (0, utils_1.renderMessage)(
|
|
14
|
+
return (0, utils_1.renderMessage)("{{value}}", { value });
|
|
18
15
|
}
|
|
19
16
|
return Object.entries(input)
|
|
20
17
|
.map(([key, value]) => `${key} ${value}`)
|
|
21
|
-
.join(
|
|
18
|
+
.join("\n");
|
|
22
19
|
}
|
|
23
20
|
/**
|
|
24
21
|
* Load memories that are defined in the agent definition.
|
|
@@ -33,7 +30,11 @@ class Agent extends runnable_1.Runnable {
|
|
|
33
30
|
if (!name || !memory)
|
|
34
31
|
return null;
|
|
35
32
|
const q = await this.getMemoryQuery(input, query);
|
|
36
|
-
const { results: memories } = await memory.search(q, {
|
|
33
|
+
const { results: memories } = await memory.search(q, {
|
|
34
|
+
...options,
|
|
35
|
+
userId,
|
|
36
|
+
sessionId,
|
|
37
|
+
});
|
|
37
38
|
return [
|
|
38
39
|
[id, memories],
|
|
39
40
|
[name, memories],
|
|
@@ -58,7 +59,9 @@ class Agent extends runnable_1.Runnable {
|
|
|
58
59
|
}));
|
|
59
60
|
}
|
|
60
61
|
async run(input, options) {
|
|
61
|
-
logger_1.default.debug(`AIGNE core: run agent ${this.name || this.id} with`, {
|
|
62
|
+
logger_1.default.debug(`AIGNE core: run agent ${this.name || this.id} with`, {
|
|
63
|
+
input,
|
|
64
|
+
});
|
|
62
65
|
const memories = await this.loadMemories(input, this.context);
|
|
63
66
|
const processResult = await this.process(input, { ...options, memories });
|
|
64
67
|
if (options?.stream) {
|
|
@@ -68,7 +71,9 @@ class Agent extends runnable_1.Runnable {
|
|
|
68
71
|
: (0, utils_1.objectToRunnableResponseStream)(processResult);
|
|
69
72
|
return (0, utils_1.extractOutputsFromRunnableOutput)(stream, async (result) => {
|
|
70
73
|
// TODO: validate result against outputs schema
|
|
71
|
-
logger_1.default.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
|
|
74
|
+
logger_1.default.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
|
|
75
|
+
result,
|
|
76
|
+
});
|
|
72
77
|
await this.onResult(result);
|
|
73
78
|
});
|
|
74
79
|
}
|
|
@@ -77,7 +82,9 @@ class Agent extends runnable_1.Runnable {
|
|
|
77
82
|
: Symbol.asyncIterator in processResult
|
|
78
83
|
? await (0, utils_1.runnableResponseStreamToObject)(processResult)
|
|
79
84
|
: processResult;
|
|
80
|
-
logger_1.default.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
|
|
85
|
+
logger_1.default.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
|
|
86
|
+
result,
|
|
87
|
+
});
|
|
81
88
|
// TODO: validate result against outputs schema
|
|
82
89
|
await this.onResult(result);
|
|
83
90
|
return result;
|
package/lib/cjs/constants.js
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StreamTextOutputName = exports.TYPES = void 0;
|
|
4
4
|
exports.TYPES = {
|
|
5
|
-
context: Symbol.for(
|
|
6
|
-
definition: Symbol.for(
|
|
7
|
-
llmModel: Symbol.for(
|
|
8
|
-
functionRunner: Symbol.for(
|
|
5
|
+
context: Symbol.for("AIGNE_CONTEXT"),
|
|
6
|
+
definition: Symbol.for("AIGNE_DEFINITION"),
|
|
7
|
+
llmModel: Symbol.for("AIGNE_LLM_MODEL"),
|
|
8
|
+
functionRunner: Symbol.for("AIGNE_FUNCTION_RUNNER"),
|
|
9
9
|
};
|
|
10
|
-
exports.StreamTextOutputName =
|
|
10
|
+
exports.StreamTextOutputName = "$text";
|
|
@@ -11,32 +11,33 @@ function schemaToDataType(dataType) {
|
|
|
11
11
|
name,
|
|
12
12
|
};
|
|
13
13
|
switch (schema.type) {
|
|
14
|
-
case
|
|
14
|
+
case "string":
|
|
15
15
|
return {
|
|
16
16
|
...base,
|
|
17
|
-
type:
|
|
17
|
+
type: "string",
|
|
18
18
|
};
|
|
19
|
-
case
|
|
19
|
+
case "number":
|
|
20
20
|
return {
|
|
21
21
|
...base,
|
|
22
|
-
type:
|
|
22
|
+
type: "number",
|
|
23
23
|
};
|
|
24
|
-
case
|
|
24
|
+
case "boolean":
|
|
25
25
|
return {
|
|
26
26
|
...base,
|
|
27
|
-
type:
|
|
27
|
+
type: "boolean",
|
|
28
28
|
};
|
|
29
|
-
case
|
|
29
|
+
case "object":
|
|
30
30
|
return {
|
|
31
31
|
...base,
|
|
32
|
-
type:
|
|
32
|
+
type: "object",
|
|
33
33
|
properties: schema.properties && schemaToDataType(schema.properties),
|
|
34
34
|
};
|
|
35
|
-
case
|
|
35
|
+
case "array":
|
|
36
36
|
return {
|
|
37
37
|
...base,
|
|
38
|
-
type:
|
|
39
|
-
items: schema.items &&
|
|
38
|
+
type: "array",
|
|
39
|
+
items: schema.items &&
|
|
40
|
+
ordered_map_1.OrderedRecord.find(schemaToDataType({ items: schema.items }), (i) => i.name === "items"),
|
|
40
41
|
};
|
|
41
42
|
default: {
|
|
42
43
|
throw new Error(`Unknown data type: ${schema.type}`);
|
|
@@ -14,7 +14,9 @@ function toRunnableMemories(agentName, inputs, memories) {
|
|
|
14
14
|
id: name || (0, nanoid_1.nanoid)(),
|
|
15
15
|
name,
|
|
16
16
|
memory,
|
|
17
|
-
query: queryFromVariable
|
|
17
|
+
query: queryFromVariable
|
|
18
|
+
? { from: "variable", fromVariableId: queryFromVariable.id }
|
|
19
|
+
: undefined,
|
|
18
20
|
options,
|
|
19
21
|
};
|
|
20
22
|
}));
|
|
@@ -20,7 +20,6 @@ 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
22
|
const memory_1 = require("./definitions/memory");
|
|
23
|
-
const function_runner_1 = require("./function-runner");
|
|
24
23
|
let FunctionAgent = class FunctionAgent extends agent_1.Agent {
|
|
25
24
|
definition;
|
|
26
25
|
runner;
|
|
@@ -34,11 +33,11 @@ let FunctionAgent = class FunctionAgent extends agent_1.Agent {
|
|
|
34
33
|
async process(input, options) {
|
|
35
34
|
const { definition: { language, code, ...definition }, runner, context, } = this;
|
|
36
35
|
if (!runner)
|
|
37
|
-
throw new Error(
|
|
36
|
+
throw new Error("Function runner is required");
|
|
38
37
|
if (!code)
|
|
39
|
-
throw new Error(
|
|
38
|
+
throw new Error("Code is required");
|
|
40
39
|
if (!context)
|
|
41
|
-
throw new Error(
|
|
40
|
+
throw new Error("Context is required");
|
|
42
41
|
return await runner.run({
|
|
43
42
|
name: definition.name || definition.id,
|
|
44
43
|
language,
|
|
@@ -55,7 +54,7 @@ exports.FunctionAgent = FunctionAgent = __decorate([
|
|
|
55
54
|
__param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
|
|
56
55
|
__param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
|
|
57
56
|
__param(2, (0, tsyringe_1.inject)(constants_1.TYPES.functionRunner)),
|
|
58
|
-
__metadata("design:paramtypes", [Object, Object,
|
|
57
|
+
__metadata("design:paramtypes", [Object, Object, Function])
|
|
59
58
|
], FunctionAgent);
|
|
60
59
|
function create({ context, ...options }) {
|
|
61
60
|
const agentId = options.name || (0, nanoid_1.nanoid)();
|
|
@@ -65,7 +64,7 @@ function create({ context, ...options }) {
|
|
|
65
64
|
return new FunctionAgent({
|
|
66
65
|
id: agentId,
|
|
67
66
|
name: options.name,
|
|
68
|
-
type:
|
|
67
|
+
type: "function_agent",
|
|
69
68
|
inputs,
|
|
70
69
|
outputs,
|
|
71
70
|
memories,
|
|
@@ -6,23 +6,23 @@ const utils_1 = require("./utils");
|
|
|
6
6
|
class FunctionRunner extends agent_1.Agent {
|
|
7
7
|
constructor(context) {
|
|
8
8
|
super({
|
|
9
|
-
id:
|
|
10
|
-
type:
|
|
11
|
-
name:
|
|
12
|
-
description:
|
|
9
|
+
id: "function_runner",
|
|
10
|
+
type: "function_runner",
|
|
11
|
+
name: "Function Runner",
|
|
12
|
+
description: "Run a function",
|
|
13
13
|
inputs: utils_1.OrderedRecord.fromArray([
|
|
14
|
-
{ id:
|
|
15
|
-
{ id:
|
|
16
|
-
{ id:
|
|
17
|
-
{ id:
|
|
18
|
-
{ id:
|
|
19
|
-
{ id:
|
|
14
|
+
{ id: "name", name: "name", type: "string", required: true },
|
|
15
|
+
{ id: "language", name: "language", type: "string" },
|
|
16
|
+
{ id: "code", name: "code", type: "string", required: true },
|
|
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 },
|
|
20
20
|
]),
|
|
21
21
|
outputs: utils_1.OrderedRecord.fromArray([
|
|
22
22
|
{
|
|
23
|
-
id:
|
|
24
|
-
name:
|
|
25
|
-
type:
|
|
23
|
+
id: "result",
|
|
24
|
+
name: "result",
|
|
25
|
+
type: "object",
|
|
26
26
|
},
|
|
27
27
|
]),
|
|
28
28
|
}, context);
|
package/lib/cjs/llm-agent.js
CHANGED
|
@@ -19,7 +19,6 @@ const agent_1 = require("./agent");
|
|
|
19
19
|
const constants_1 = require("./constants");
|
|
20
20
|
const data_type_schema_1 = require("./definitions/data-type-schema");
|
|
21
21
|
const memory_1 = require("./definitions/memory");
|
|
22
|
-
const llm_model_1 = require("./llm-model");
|
|
23
22
|
const message_utils_1 = require("./utils/message-utils");
|
|
24
23
|
const mustache_utils_1 = require("./utils/mustache-utils");
|
|
25
24
|
const ordered_map_1 = require("./utils/ordered-map");
|
|
@@ -37,17 +36,17 @@ let LLMAgent = class LLMAgent extends agent_1.Agent {
|
|
|
37
36
|
async *process(input, options) {
|
|
38
37
|
const { definition, model } = this;
|
|
39
38
|
if (!model)
|
|
40
|
-
throw new Error(
|
|
39
|
+
throw new Error("LLM model is required");
|
|
41
40
|
const { originalMessages, messagesWithMemory } = (0, message_utils_1.prepareMessages)(definition, input, options.memories);
|
|
42
41
|
const llmInputs = {
|
|
43
42
|
messages: messagesWithMemory,
|
|
44
43
|
modelOptions: definition.modelOptions,
|
|
45
44
|
};
|
|
46
|
-
let $text =
|
|
45
|
+
let $text = "";
|
|
47
46
|
const hasTextOutput = ordered_map_1.OrderedRecord.find(definition.outputs, (i) => i.name === constants_1.StreamTextOutputName);
|
|
48
47
|
if (hasTextOutput) {
|
|
49
48
|
for await (const chunk of await this.runWithTextOutput(llmInputs)) {
|
|
50
|
-
$text += chunk.$text ||
|
|
49
|
+
$text += chunk.$text || "";
|
|
51
50
|
yield { $text: chunk.$text };
|
|
52
51
|
}
|
|
53
52
|
}
|
|
@@ -56,37 +55,39 @@ let LLMAgent = class LLMAgent extends agent_1.Agent {
|
|
|
56
55
|
yield { delta: json };
|
|
57
56
|
await this.updateMemories([
|
|
58
57
|
...originalMessages,
|
|
59
|
-
{
|
|
58
|
+
{
|
|
59
|
+
role: "assistant",
|
|
60
|
+
content: (0, mustache_utils_1.renderMessage)("{{$text}}\n{{json}}", { $text, json }).trim(),
|
|
61
|
+
},
|
|
60
62
|
]);
|
|
61
63
|
}
|
|
62
64
|
async runWithStructuredOutput(llmInputs) {
|
|
63
|
-
const jsonOutputs = ordered_map_1.OrderedRecord.filter(this.definition.outputs, (i) => i.name !== constants_1.StreamTextOutputName
|
|
64
|
-
);
|
|
65
|
+
const jsonOutputs = ordered_map_1.OrderedRecord.filter(this.definition.outputs, (i) => i.name !== constants_1.StreamTextOutputName);
|
|
65
66
|
if (!jsonOutputs.length)
|
|
66
67
|
return null;
|
|
67
68
|
const schema = (0, structured_output_schema_1.outputsToJsonSchema)(ordered_map_1.OrderedRecord.fromArray(jsonOutputs));
|
|
68
69
|
const { model } = this;
|
|
69
70
|
if (!model)
|
|
70
|
-
throw new Error(
|
|
71
|
+
throw new Error("LLM model is required");
|
|
71
72
|
const response = await model.run({
|
|
72
73
|
...llmInputs,
|
|
73
74
|
responseFormat: {
|
|
74
|
-
type:
|
|
75
|
+
type: "json_schema",
|
|
75
76
|
jsonSchema: {
|
|
76
|
-
name:
|
|
77
|
+
name: "output",
|
|
77
78
|
schema,
|
|
78
79
|
strict: true,
|
|
79
80
|
},
|
|
80
81
|
},
|
|
81
82
|
});
|
|
82
83
|
if (!response.$text)
|
|
83
|
-
throw new Error(
|
|
84
|
+
throw new Error("No text in JSON mode response");
|
|
84
85
|
return JSON.parse(response.$text);
|
|
85
86
|
}
|
|
86
87
|
async runWithTextOutput(llmInputs) {
|
|
87
88
|
const { model } = this;
|
|
88
89
|
if (!model)
|
|
89
|
-
throw new Error(
|
|
90
|
+
throw new Error("LLM model is required");
|
|
90
91
|
return model.run(llmInputs, { stream: true });
|
|
91
92
|
}
|
|
92
93
|
};
|
|
@@ -96,7 +97,7 @@ exports.LLMAgent = LLMAgent = __decorate([
|
|
|
96
97
|
__param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
|
|
97
98
|
__param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
|
|
98
99
|
__param(2, (0, tsyringe_1.inject)(constants_1.TYPES.llmModel)),
|
|
99
|
-
__metadata("design:paramtypes", [Object, Object,
|
|
100
|
+
__metadata("design:paramtypes", [Object, Object, Function])
|
|
100
101
|
], LLMAgent);
|
|
101
102
|
/**
|
|
102
103
|
* Create LLMAgent definition.
|
|
@@ -112,7 +113,7 @@ function create({ context, ...options }) {
|
|
|
112
113
|
.filter(([, i]) => i.primary)
|
|
113
114
|
.map(([name]) => name);
|
|
114
115
|
if (primaryMemoryNames && primaryMemoryNames.length > 1) {
|
|
115
|
-
throw new Error(
|
|
116
|
+
throw new Error("Only one primary memory is allowed");
|
|
116
117
|
}
|
|
117
118
|
const messages = ordered_map_1.OrderedRecord.fromArray(options.messages?.map((i) => ({
|
|
118
119
|
id: (0, nanoid_1.nanoid)(),
|
|
@@ -122,7 +123,7 @@ function create({ context, ...options }) {
|
|
|
122
123
|
return new LLMAgent({
|
|
123
124
|
id: agentId,
|
|
124
125
|
name: options.name,
|
|
125
|
-
type:
|
|
126
|
+
type: "llm_agent",
|
|
126
127
|
inputs,
|
|
127
128
|
outputs,
|
|
128
129
|
primaryMemoryId: primaryMemoryNames?.at(0),
|
|
@@ -18,7 +18,6 @@ const tsyringe_1 = require("tsyringe");
|
|
|
18
18
|
const agent_1 = require("./agent");
|
|
19
19
|
const constants_1 = require("./constants");
|
|
20
20
|
const memory_1 = require("./definitions/memory");
|
|
21
|
-
const llm_model_1 = require("./llm-model");
|
|
22
21
|
const utils_1 = require("./utils");
|
|
23
22
|
const message_utils_1 = require("./utils/message-utils");
|
|
24
23
|
let LLMDecisionAgent = class LLMDecisionAgent extends agent_1.Agent {
|
|
@@ -34,18 +33,18 @@ let LLMDecisionAgent = class LLMDecisionAgent extends agent_1.Agent {
|
|
|
34
33
|
async process(input, options) {
|
|
35
34
|
const { definition, context, model } = this;
|
|
36
35
|
if (!model)
|
|
37
|
-
throw new Error(
|
|
36
|
+
throw new Error("LLM model is required");
|
|
38
37
|
if (!context)
|
|
39
|
-
throw new Error(
|
|
38
|
+
throw new Error("Context is required");
|
|
40
39
|
const { originalMessages, messagesWithMemory } = (0, message_utils_1.prepareMessages)(definition, input, options.memories);
|
|
41
40
|
const cases = await Promise.all(utils_1.OrderedRecord.map(definition.cases, async (t) => {
|
|
42
41
|
if (!t.runnable?.id)
|
|
43
|
-
throw new Error(
|
|
42
|
+
throw new Error("Runnable is required");
|
|
44
43
|
const runnable = await context.resolve(t.runnable.id);
|
|
45
44
|
// TODO: auto generate name by llm model if needed
|
|
46
45
|
const name = t.name || runnable.name;
|
|
47
46
|
if (!name)
|
|
48
|
-
throw new Error(
|
|
47
|
+
throw new Error("Case name is required");
|
|
49
48
|
return { name, description: t.description, runnable };
|
|
50
49
|
}));
|
|
51
50
|
const llmInputs = {
|
|
@@ -54,7 +53,7 @@ let LLMDecisionAgent = class LLMDecisionAgent extends agent_1.Agent {
|
|
|
54
53
|
tools: cases.map((t) => {
|
|
55
54
|
// TODO: auto generate parameters by llm model if needed
|
|
56
55
|
return {
|
|
57
|
-
type:
|
|
56
|
+
type: "function",
|
|
58
57
|
function: {
|
|
59
58
|
name: t.name,
|
|
60
59
|
description: t.description,
|
|
@@ -62,21 +61,24 @@ let LLMDecisionAgent = class LLMDecisionAgent extends agent_1.Agent {
|
|
|
62
61
|
},
|
|
63
62
|
};
|
|
64
63
|
}),
|
|
65
|
-
toolChoice:
|
|
64
|
+
toolChoice: "required",
|
|
66
65
|
};
|
|
67
66
|
const { toolCalls } = await model.run(llmInputs);
|
|
68
67
|
// TODO: support run multiple calls
|
|
69
68
|
const functionNameToCall = toolCalls?.[0]?.function?.name;
|
|
70
69
|
if (!functionNameToCall)
|
|
71
|
-
throw new Error(
|
|
70
|
+
throw new Error("No any runnable called");
|
|
72
71
|
const caseToCall = cases.find((i) => i.name === functionNameToCall);
|
|
73
72
|
if (!caseToCall)
|
|
74
|
-
throw new Error(
|
|
73
|
+
throw new Error("Case not found");
|
|
75
74
|
// TODO: check result structure and omit undefined values
|
|
76
75
|
const output = await caseToCall.runnable.run(input, { stream: true });
|
|
77
76
|
return (0, utils_1.extractOutputsFromRunnableOutput)(output, ({ $text, ...json }) => this.updateMemories([
|
|
78
77
|
...originalMessages,
|
|
79
|
-
{
|
|
78
|
+
{
|
|
79
|
+
role: "assistant",
|
|
80
|
+
content: (0, utils_1.renderMessage)("{{$text}}\n{{json}}", { $text, json }).trim(),
|
|
81
|
+
},
|
|
80
82
|
]));
|
|
81
83
|
}
|
|
82
84
|
};
|
|
@@ -86,7 +88,7 @@ exports.LLMDecisionAgent = LLMDecisionAgent = __decorate([
|
|
|
86
88
|
__param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
|
|
87
89
|
__param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
|
|
88
90
|
__param(2, (0, tsyringe_1.inject)(constants_1.TYPES.llmModel)),
|
|
89
|
-
__metadata("design:paramtypes", [Object, Object,
|
|
91
|
+
__metadata("design:paramtypes", [Object, Object, Function])
|
|
90
92
|
], LLMDecisionAgent);
|
|
91
93
|
function create({ context, ...options }) {
|
|
92
94
|
const agentId = options.name || (0, nanoid_1.nanoid)();
|
|
@@ -103,7 +105,7 @@ function create({ context, ...options }) {
|
|
|
103
105
|
.filter(([, i]) => i.primary)
|
|
104
106
|
.map(([name]) => name);
|
|
105
107
|
if (primaryMemoryNames && primaryMemoryNames.length > 1) {
|
|
106
|
-
throw new Error(
|
|
108
|
+
throw new Error("Only one primary memory is allowed");
|
|
107
109
|
}
|
|
108
110
|
const messages = utils_1.OrderedRecord.fromArray(options.messages?.map((i) => ({
|
|
109
111
|
id: (0, nanoid_1.nanoid)(),
|
|
@@ -113,7 +115,7 @@ function create({ context, ...options }) {
|
|
|
113
115
|
return new LLMDecisionAgent({
|
|
114
116
|
id: agentId,
|
|
115
117
|
name: options.name,
|
|
116
|
-
type:
|
|
118
|
+
type: "llm_decision_agent",
|
|
117
119
|
inputs,
|
|
118
120
|
outputs,
|
|
119
121
|
messages,
|
package/lib/cjs/llm-model.js
CHANGED
|
@@ -6,20 +6,20 @@ const utils_1 = require("./utils");
|
|
|
6
6
|
class LLMModel extends agent_1.Agent {
|
|
7
7
|
constructor(context) {
|
|
8
8
|
super({
|
|
9
|
-
id:
|
|
10
|
-
type:
|
|
11
|
-
name:
|
|
12
|
-
description:
|
|
9
|
+
id: "llm_model",
|
|
10
|
+
type: "llm_model",
|
|
11
|
+
name: "LLM Model",
|
|
12
|
+
description: "Run a LLM model",
|
|
13
13
|
inputs: utils_1.OrderedRecord.fromArray([
|
|
14
|
-
{ id:
|
|
15
|
-
{ id:
|
|
16
|
-
{ id:
|
|
17
|
-
{ id:
|
|
18
|
-
{ id:
|
|
14
|
+
{ id: "messages", name: "messages", type: "array", required: true },
|
|
15
|
+
{ id: "responseFormat", name: "responseFormat", type: "object" },
|
|
16
|
+
{ id: "tools", name: "tools", type: "array" },
|
|
17
|
+
{ id: "toolChoice", name: "toolChoice", type: "object" },
|
|
18
|
+
{ id: "modelOptions", name: "modelOptions", type: "object" },
|
|
19
19
|
]),
|
|
20
20
|
outputs: utils_1.OrderedRecord.fromArray([
|
|
21
|
-
{ id:
|
|
22
|
-
{ id:
|
|
21
|
+
{ id: "$text", name: "$text", type: "string" },
|
|
22
|
+
{ id: "toolCalls", name: "toolCalls", type: "object" },
|
|
23
23
|
]),
|
|
24
24
|
}, context);
|
|
25
25
|
}
|
|
@@ -29,9 +29,9 @@ let LocalFunctionAgent = class LocalFunctionAgent extends agent_1.Agent {
|
|
|
29
29
|
async process(input, options) {
|
|
30
30
|
const { definition: { function: func }, context, } = this;
|
|
31
31
|
if (!func)
|
|
32
|
-
throw new Error(
|
|
32
|
+
throw new Error("Function is required");
|
|
33
33
|
if (!context)
|
|
34
|
-
throw new Error(
|
|
34
|
+
throw new Error("Context is required");
|
|
35
35
|
return await func(input, { context, memories: options.memories });
|
|
36
36
|
}
|
|
37
37
|
};
|
|
@@ -50,7 +50,7 @@ function create({ context, ...options }) {
|
|
|
50
50
|
return new LocalFunctionAgent({
|
|
51
51
|
id: agentId,
|
|
52
52
|
name: options.name,
|
|
53
|
-
type:
|
|
53
|
+
type: "local_function_agent",
|
|
54
54
|
inputs,
|
|
55
55
|
outputs,
|
|
56
56
|
memories,
|
package/lib/cjs/memorable.js
CHANGED
|
@@ -7,9 +7,9 @@ const utils_1 = require("./utils");
|
|
|
7
7
|
class Memorable extends runnable_1.Runnable {
|
|
8
8
|
constructor() {
|
|
9
9
|
super({
|
|
10
|
-
id:
|
|
11
|
-
type:
|
|
12
|
-
name:
|
|
10
|
+
id: "memory",
|
|
11
|
+
type: "memory",
|
|
12
|
+
name: "Memory",
|
|
13
13
|
inputs: utils_1.OrderedRecord.fromArray([]),
|
|
14
14
|
outputs: utils_1.OrderedRecord.fromArray([]),
|
|
15
15
|
});
|
|
@@ -28,13 +28,13 @@ let OpenAPIAgent = class OpenAPIAgent extends agent_1.Agent {
|
|
|
28
28
|
this.definition = definition;
|
|
29
29
|
}
|
|
30
30
|
async process(input) {
|
|
31
|
-
const { url, method =
|
|
31
|
+
const { url, method = "get", auth, inputs } = this.definition;
|
|
32
32
|
if (!url)
|
|
33
|
-
throw new Error(
|
|
33
|
+
throw new Error("API url is required");
|
|
34
34
|
const request = await (0, open_api_parameter_1.formatOpenAPIRequest)({ url, method, auth }, inputs, input);
|
|
35
35
|
return this.fetch(request);
|
|
36
36
|
}
|
|
37
|
-
fetch(request) {
|
|
37
|
+
async fetch(request) {
|
|
38
38
|
return (0, fetch_open_api_1.fetchOpenApi)(request);
|
|
39
39
|
}
|
|
40
40
|
};
|
|
@@ -52,7 +52,7 @@ function create({ context, ...options }) {
|
|
|
52
52
|
return new OpenAPIAgent({
|
|
53
53
|
id: agentId,
|
|
54
54
|
name: options.name,
|
|
55
|
-
type:
|
|
55
|
+
type: "open_api_agent",
|
|
56
56
|
inputs,
|
|
57
57
|
outputs,
|
|
58
58
|
url: options.url,
|
|
@@ -11,9 +11,6 @@ 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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
|
-
};
|
|
17
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
15
|
exports.PipelineAgent = void 0;
|
|
19
16
|
const lodash_1 = require("lodash");
|
|
@@ -23,7 +20,7 @@ const agent_1 = require("./agent");
|
|
|
23
20
|
const constants_1 = require("./constants");
|
|
24
21
|
const data_type_schema_1 = require("./definitions/data-type-schema");
|
|
25
22
|
const memory_1 = require("./definitions/memory");
|
|
26
|
-
const logger_1 =
|
|
23
|
+
const logger_1 = require("./logger");
|
|
27
24
|
const is_non_nullable_1 = require("./utils/is-non-nullable");
|
|
28
25
|
const ordered_map_1 = require("./utils/ordered-map");
|
|
29
26
|
let PipelineAgent = class PipelineAgent extends agent_1.Agent {
|
|
@@ -36,10 +33,10 @@ let PipelineAgent = class PipelineAgent extends agent_1.Agent {
|
|
|
36
33
|
async process(input, options) {
|
|
37
34
|
const { definition, context } = this;
|
|
38
35
|
if (!context)
|
|
39
|
-
throw new Error(
|
|
36
|
+
throw new Error("Context is required");
|
|
40
37
|
const { processes } = definition;
|
|
41
38
|
if (!processes?.$indexes.length) {
|
|
42
|
-
throw new Error(
|
|
39
|
+
throw new Error("No processes defined");
|
|
43
40
|
}
|
|
44
41
|
return new ReadableStream({
|
|
45
42
|
async start(controller) {
|
|
@@ -59,7 +56,7 @@ let PipelineAgent = class PipelineAgent extends agent_1.Agent {
|
|
|
59
56
|
let result = {};
|
|
60
57
|
for (const process of ordered_map_1.OrderedRecord.iterator(processes)) {
|
|
61
58
|
if (!process.runnable?.id) {
|
|
62
|
-
logger_1.default.warn(
|
|
59
|
+
logger_1.default.warn("Runnable id is required for process", process);
|
|
63
60
|
continue;
|
|
64
61
|
}
|
|
65
62
|
const runnable = await context.resolve(process.runnable.id);
|
|
@@ -71,18 +68,21 @@ let PipelineAgent = class PipelineAgent extends agent_1.Agent {
|
|
|
71
68
|
if (!targetInput?.name)
|
|
72
69
|
return null;
|
|
73
70
|
let value;
|
|
74
|
-
if (input.from ===
|
|
71
|
+
if (input.from === "variable") {
|
|
75
72
|
const v = variables[input.fromVariableId];
|
|
76
|
-
value = input.fromVariablePropPath?.length
|
|
73
|
+
value = input.fromVariablePropPath?.length
|
|
74
|
+
? (0, lodash_1.get)(v, input.fromVariablePropPath)
|
|
75
|
+
: v;
|
|
77
76
|
}
|
|
78
77
|
else {
|
|
79
|
-
throw new Error(
|
|
78
|
+
throw new Error("Unsupported input source");
|
|
80
79
|
}
|
|
81
80
|
return [targetInput.name, value];
|
|
82
81
|
})
|
|
83
82
|
.filter(is_non_nullable_1.isNonNullable));
|
|
84
83
|
const stream = await runnable.run(inputValues, { stream: true });
|
|
85
|
-
const needRespondTextStream = textStreamOutput?.from ===
|
|
84
|
+
const needRespondTextStream = textStreamOutput?.from === "variable" &&
|
|
85
|
+
textStreamOutput.fromVariableId === process.id;
|
|
86
86
|
// const needRespondJsonStream = outputs.some(
|
|
87
87
|
// (i) => i.name !== StreamTextOutputName && i.from === 'variable' && i.fromVariableId === process.id
|
|
88
88
|
// );
|
|
@@ -90,7 +90,9 @@ let PipelineAgent = class PipelineAgent extends agent_1.Agent {
|
|
|
90
90
|
variables[process.id] = processResult;
|
|
91
91
|
for await (const chunk of stream) {
|
|
92
92
|
if (chunk.$text) {
|
|
93
|
-
Object.assign(processResult, {
|
|
93
|
+
Object.assign(processResult, {
|
|
94
|
+
$text: (processResult.$text || "") + chunk.$text,
|
|
95
|
+
});
|
|
94
96
|
if (needRespondTextStream) {
|
|
95
97
|
controller.enqueue({ $text: chunk.$text });
|
|
96
98
|
}
|
|
@@ -105,9 +107,11 @@ let PipelineAgent = class PipelineAgent extends agent_1.Agent {
|
|
|
105
107
|
if (output.name === constants_1.StreamTextOutputName)
|
|
106
108
|
return null;
|
|
107
109
|
let value;
|
|
108
|
-
if (output.from ===
|
|
110
|
+
if (output.from === "variable") {
|
|
109
111
|
const v = variables[output.fromVariableId];
|
|
110
|
-
value = output.fromVariablePropPath?.length
|
|
112
|
+
value = output.fromVariablePropPath?.length
|
|
113
|
+
? (0, lodash_1.get)(v, output.fromVariablePropPath)
|
|
114
|
+
: v;
|
|
111
115
|
}
|
|
112
116
|
else {
|
|
113
117
|
throw new Error(`Unsupported output source ${output.from}`);
|
|
@@ -163,7 +167,7 @@ function create({ context, ...options }) {
|
|
|
163
167
|
return [
|
|
164
168
|
inputOfProcess.id,
|
|
165
169
|
{
|
|
166
|
-
from:
|
|
170
|
+
from: "variable",
|
|
167
171
|
fromVariableId: inputFromPipeline.id,
|
|
168
172
|
fromVariablePropPath: i.fromVariablePropPath,
|
|
169
173
|
},
|
|
@@ -177,12 +181,17 @@ function create({ context, ...options }) {
|
|
|
177
181
|
ordered_map_1.OrderedRecord.find(processes, (p) => p.name === fromVariable);
|
|
178
182
|
if (!from)
|
|
179
183
|
throw new Error(`Output ${output.name} not found in inputs or processes`);
|
|
180
|
-
return {
|
|
184
|
+
return {
|
|
185
|
+
...output,
|
|
186
|
+
from: "variable",
|
|
187
|
+
fromVariableId: from.id,
|
|
188
|
+
fromVariablePropPath,
|
|
189
|
+
};
|
|
181
190
|
}));
|
|
182
191
|
return new PipelineAgent({
|
|
183
192
|
id: agentId,
|
|
184
193
|
name: options.name,
|
|
185
|
-
type:
|
|
194
|
+
type: "pipeline_agent",
|
|
186
195
|
inputs,
|
|
187
196
|
memories,
|
|
188
197
|
outputs,
|