@aigne/core 0.4.211-beta.2 → 1.0.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 +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/runnable.js
CHANGED
|
@@ -25,8 +25,8 @@ class Runnable {
|
|
|
25
25
|
}
|
|
26
26
|
exports.Runnable = Runnable;
|
|
27
27
|
function isRunnableResponseDelta(chunk) {
|
|
28
|
-
return
|
|
28
|
+
return "$text" in chunk || "delta" in chunk;
|
|
29
29
|
}
|
|
30
30
|
function isRunnableResponseError(chunk) {
|
|
31
|
-
return
|
|
31
|
+
return "error" in chunk;
|
|
32
32
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fetchOpenApi =
|
|
3
|
+
exports.fetchOpenApi = fetchOpenApi;
|
|
4
4
|
const ufo_1 = require("ufo");
|
|
5
5
|
const constants_1 = require("./constants");
|
|
6
6
|
const fetch_1 = require("./fetch");
|
|
7
|
-
|
|
7
|
+
async function fetchOpenApi(request) {
|
|
8
8
|
const cookie = request.cookies
|
|
9
9
|
? Object.entries(request.cookies)
|
|
10
10
|
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
11
|
-
.join(
|
|
11
|
+
.join("; ")
|
|
12
12
|
.trim()
|
|
13
13
|
: undefined;
|
|
14
14
|
const controller = new AbortController();
|
|
@@ -16,15 +16,16 @@ const fetchOpenApi = async (request) => {
|
|
|
16
16
|
const response = await fetch((0, ufo_1.withQuery)(request.url, request.query ?? {}), {
|
|
17
17
|
method: request.method,
|
|
18
18
|
headers: {
|
|
19
|
-
|
|
19
|
+
"Content-Type": "application/json",
|
|
20
20
|
...request.headers,
|
|
21
21
|
...(cookie && { cookie }),
|
|
22
22
|
},
|
|
23
|
-
body: request.method.toLowerCase() !==
|
|
24
|
-
|
|
23
|
+
body: request.method.toLowerCase() !== "get" && request.body
|
|
24
|
+
? JSON.stringify(request.body)
|
|
25
|
+
: undefined,
|
|
26
|
+
credentials: request.cookies ? "include" : "same-origin",
|
|
25
27
|
signal: controller.signal,
|
|
26
28
|
}).finally(() => clearTimeout(abortTimer));
|
|
27
29
|
await (0, fetch_1.checkFetchResponse)(response);
|
|
28
30
|
return response.json();
|
|
29
|
-
}
|
|
30
|
-
exports.fetchOpenApi = fetchOpenApi;
|
|
31
|
+
}
|
package/lib/cjs/utils/fetch.js
CHANGED
|
@@ -7,14 +7,15 @@ async function checkFetchResponse(result) {
|
|
|
7
7
|
try {
|
|
8
8
|
const json = await result.json();
|
|
9
9
|
const msg = json.error?.message || json.message;
|
|
10
|
-
if (msg && typeof msg ===
|
|
10
|
+
if (msg && typeof msg === "string") {
|
|
11
11
|
message = msg;
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
catch {
|
|
15
15
|
// ignore
|
|
16
16
|
}
|
|
17
|
-
throw new Error(message ||
|
|
17
|
+
throw new Error(message ||
|
|
18
|
+
`Failed to fetch url ${result.url} with status ${result.status}`);
|
|
18
19
|
}
|
|
19
20
|
return result;
|
|
20
21
|
}
|
|
@@ -1,11 +1,8 @@
|
|
|
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.isNonNullable = isNonNullable;
|
|
7
4
|
exports.isPropsNonNullable = isPropsNonNullable;
|
|
8
|
-
const isNil_1 =
|
|
5
|
+
const isNil_1 = require("lodash/isNil");
|
|
9
6
|
function isNonNullable(value) {
|
|
10
7
|
return !(0, isNil_1.default)(value);
|
|
11
8
|
}
|
|
@@ -1,29 +1,34 @@
|
|
|
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.mergeHistoryMessages = mergeHistoryMessages;
|
|
7
4
|
exports.memoriesToMessages = memoriesToMessages;
|
|
8
5
|
exports.prepareMessages = prepareMessages;
|
|
9
|
-
const omit_1 =
|
|
6
|
+
const omit_1 = require("lodash/omit");
|
|
10
7
|
const is_non_nullable_1 = require("./is-non-nullable");
|
|
11
8
|
const mustache_utils_1 = require("./mustache-utils");
|
|
12
9
|
const ordered_map_1 = require("./ordered-map");
|
|
13
10
|
function mergeHistoryMessages(messages, history) {
|
|
14
|
-
const firstUserMessageIndex = messages.findIndex((m) => m.role ===
|
|
11
|
+
const firstUserMessageIndex = messages.findIndex((m) => m.role === "user");
|
|
15
12
|
if (firstUserMessageIndex >= 0) {
|
|
16
|
-
return [
|
|
13
|
+
return [
|
|
14
|
+
...messages.slice(0, firstUserMessageIndex),
|
|
15
|
+
...history,
|
|
16
|
+
...messages.slice(firstUserMessageIndex),
|
|
17
|
+
];
|
|
17
18
|
}
|
|
18
19
|
return [...history, ...messages];
|
|
19
20
|
}
|
|
20
21
|
function memoriesToMessages(memories, { primaryMemoryName } = {}) {
|
|
21
22
|
const primary = (primaryMemoryName && memories[primaryMemoryName]) || [];
|
|
22
|
-
const otherMemories = primaryMemoryName
|
|
23
|
+
const otherMemories = primaryMemoryName
|
|
24
|
+
? (0, omit_1.default)(memories, primaryMemoryName)
|
|
25
|
+
: memories;
|
|
23
26
|
const primaryMemory = primary
|
|
24
27
|
.map((i) => {
|
|
25
|
-
const content = (0, mustache_utils_1.renderMessage)(
|
|
26
|
-
const role = [
|
|
28
|
+
const content = (0, mustache_utils_1.renderMessage)("{{memory}}", { memory: i.memory }).trim();
|
|
29
|
+
const role = ["user", "assistant"].includes(i.metadata.role)
|
|
30
|
+
? i.metadata.role
|
|
31
|
+
: undefined;
|
|
27
32
|
if (!role || !content)
|
|
28
33
|
return null;
|
|
29
34
|
return { role, content };
|
|
@@ -31,10 +36,10 @@ function memoriesToMessages(memories, { primaryMemoryName } = {}) {
|
|
|
31
36
|
.filter(is_non_nullable_1.isNonNullable);
|
|
32
37
|
const memory = Object.values(otherMemories)
|
|
33
38
|
.map((i) => i
|
|
34
|
-
.map((j) => (0, mustache_utils_1.renderMessage)(
|
|
39
|
+
.map((j) => (0, mustache_utils_1.renderMessage)("{{memory}}\n{{metadata}}", j).trim() || null)
|
|
35
40
|
.filter(is_non_nullable_1.isNonNullable)
|
|
36
|
-
.join(
|
|
37
|
-
.join(
|
|
41
|
+
.join("\n"))
|
|
42
|
+
.join("\n");
|
|
38
43
|
return {
|
|
39
44
|
primaryMemory,
|
|
40
45
|
memory,
|
|
@@ -44,10 +49,12 @@ function prepareMessages(definition, input, memories) {
|
|
|
44
49
|
const variables = { ...input, ...memories };
|
|
45
50
|
const originalMessages = ordered_map_1.OrderedRecord.toArray(definition.messages).map(({ role, content }) => ({
|
|
46
51
|
role,
|
|
47
|
-
content: typeof content ===
|
|
52
|
+
content: typeof content === "string"
|
|
53
|
+
? (0, mustache_utils_1.renderMessage)(content, variables)
|
|
54
|
+
: content,
|
|
48
55
|
}));
|
|
49
56
|
if (!originalMessages.length)
|
|
50
|
-
throw new Error(
|
|
57
|
+
throw new Error("Messages are required");
|
|
51
58
|
const { primaryMemory, memory } = memoriesToMessages(memories, {
|
|
52
59
|
primaryMemoryName: ordered_map_1.OrderedRecord.find(definition.memories, (i) => i.id === definition.primaryMemoryId)?.name,
|
|
53
60
|
});
|
|
@@ -55,13 +62,13 @@ function prepareMessages(definition, input, memories) {
|
|
|
55
62
|
// Add memory to a system message
|
|
56
63
|
if (memory) {
|
|
57
64
|
const message = {
|
|
58
|
-
role:
|
|
65
|
+
role: "system",
|
|
59
66
|
content: `\
|
|
60
67
|
Here are the memories about the user:
|
|
61
68
|
${memory}
|
|
62
69
|
`,
|
|
63
70
|
};
|
|
64
|
-
const lastSystemMessageIndex = messagesWithMemory.findLastIndex((i) => i.role ===
|
|
71
|
+
const lastSystemMessageIndex = messagesWithMemory.findLastIndex((i) => i.role === "assistant");
|
|
65
72
|
messagesWithMemory.splice(lastSystemMessageIndex + 1, 0, message);
|
|
66
73
|
}
|
|
67
74
|
// Add primary memory to messages
|
|
@@ -1,14 +1,11 @@
|
|
|
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.renderMessage = renderMessage;
|
|
7
|
-
const mustache_1 =
|
|
4
|
+
const mustache_1 = require("mustache");
|
|
8
5
|
function renderMessage(template, variables) {
|
|
9
6
|
return mustache_1.default.render(template, variables, undefined, {
|
|
10
7
|
escape: (v) => {
|
|
11
|
-
return typeof v ===
|
|
8
|
+
return typeof v === "object" ? JSON.stringify(v) : v;
|
|
12
9
|
},
|
|
13
10
|
});
|
|
14
11
|
}
|
|
@@ -1,17 +1,14 @@
|
|
|
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.formatOpenAPIRequest = formatOpenAPIRequest;
|
|
7
4
|
const lodash_1 = require("lodash");
|
|
8
|
-
const logger_1 =
|
|
5
|
+
const logger_1 = require("../logger");
|
|
9
6
|
const ordered_map_1 = require("./ordered-map");
|
|
10
7
|
async function formatOpenAPIRequest(api, inputs, input) {
|
|
11
8
|
const { url, method, ...inputParams } = processParameters(api, inputs, input);
|
|
12
|
-
logger_1.default.debug(
|
|
9
|
+
logger_1.default.debug("inputParams", inputParams);
|
|
13
10
|
const authParams = await getAuthParams(api.auth);
|
|
14
|
-
logger_1.default.debug(
|
|
11
|
+
logger_1.default.debug("authParams", authParams);
|
|
15
12
|
return {
|
|
16
13
|
url,
|
|
17
14
|
method,
|
|
@@ -26,18 +23,18 @@ async function formatOpenAPIRequest(api, inputs, input) {
|
|
|
26
23
|
async function getAuthParams(auth) {
|
|
27
24
|
if (!auth)
|
|
28
25
|
return {};
|
|
29
|
-
if (auth.type ===
|
|
26
|
+
if (auth.type === "custom") {
|
|
30
27
|
return await auth.getValue();
|
|
31
28
|
}
|
|
32
29
|
const { type, key, token } = auth;
|
|
33
30
|
switch (auth.in) {
|
|
34
|
-
case
|
|
35
|
-
return { query: { [key ||
|
|
36
|
-
case
|
|
37
|
-
return { cookies: { [key ||
|
|
31
|
+
case "query":
|
|
32
|
+
return { query: { [key || "token"]: token } };
|
|
33
|
+
case "cookie":
|
|
34
|
+
return { cookies: { [key || "token"]: token } };
|
|
38
35
|
default: {
|
|
39
|
-
const prefix = type ===
|
|
40
|
-
return { headers: { [key ||
|
|
36
|
+
const prefix = type === "bearer" ? "Bearer " : type === "basic" ? "Basic " : "";
|
|
37
|
+
return { headers: { [key || "Authorization"]: `${prefix}${token}` } };
|
|
41
38
|
}
|
|
42
39
|
}
|
|
43
40
|
}
|
|
@@ -50,35 +47,35 @@ function processParameters(api, inputs, input) {
|
|
|
50
47
|
cookies: {},
|
|
51
48
|
body: {},
|
|
52
49
|
};
|
|
53
|
-
|
|
50
|
+
for (const [key, value] of Object.entries(input)) {
|
|
54
51
|
const schema = ordered_map_1.OrderedRecord.find(inputs, (x) => x.name === key);
|
|
55
52
|
if (!schema)
|
|
56
|
-
|
|
53
|
+
continue;
|
|
57
54
|
switch (schema.in) {
|
|
58
|
-
case
|
|
55
|
+
case "query":
|
|
59
56
|
result.query[key] = value;
|
|
60
57
|
break;
|
|
61
|
-
case
|
|
58
|
+
case "header":
|
|
62
59
|
result.headers[key] = value;
|
|
63
60
|
break;
|
|
64
|
-
case
|
|
61
|
+
case "cookie":
|
|
65
62
|
result.cookies[key] = value;
|
|
66
63
|
break;
|
|
67
|
-
case
|
|
64
|
+
case "body":
|
|
68
65
|
result.body[key] = value;
|
|
69
66
|
break;
|
|
70
|
-
case
|
|
67
|
+
case "path":
|
|
71
68
|
result.url = result.url.replace(`{${key}}`, String(value));
|
|
72
69
|
break;
|
|
73
70
|
default:
|
|
74
71
|
// 没有指定 in 的情况
|
|
75
|
-
if (result.method.toLowerCase() ===
|
|
72
|
+
if (result.method.toLowerCase() === "get") {
|
|
76
73
|
result.query[key] = value;
|
|
77
74
|
}
|
|
78
75
|
else {
|
|
79
76
|
result.body[key] = value;
|
|
80
77
|
}
|
|
81
78
|
}
|
|
82
|
-
}
|
|
79
|
+
}
|
|
83
80
|
return result;
|
|
84
81
|
}
|
|
@@ -15,11 +15,11 @@ function objectToRunnableResponseStream(obj) {
|
|
|
15
15
|
});
|
|
16
16
|
}
|
|
17
17
|
async function runnableResponseStreamToObject(stream) {
|
|
18
|
-
let $text =
|
|
18
|
+
let $text = "";
|
|
19
19
|
const result = {};
|
|
20
20
|
for await (const value of stream) {
|
|
21
21
|
if ((0, runnable_1.isRunnableResponseDelta)(value)) {
|
|
22
|
-
$text += value.$text ||
|
|
22
|
+
$text += value.$text || "";
|
|
23
23
|
Object.assign(result, value.delta);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
@@ -35,16 +35,17 @@ async function runnableResponseStreamToObject(stream) {
|
|
|
35
35
|
* @returns The runnable output stream or object
|
|
36
36
|
*/
|
|
37
37
|
async function extractOutputsFromRunnableOutput(output, resolve) {
|
|
38
|
-
if (output instanceof ReadableStream ||
|
|
38
|
+
if (output instanceof ReadableStream ||
|
|
39
|
+
isAsyncGenerator(output)) {
|
|
39
40
|
return new ReadableStream({
|
|
40
41
|
async start(controller) {
|
|
41
42
|
try {
|
|
42
43
|
const result = {};
|
|
43
|
-
let $text =
|
|
44
|
+
let $text = "";
|
|
44
45
|
for await (const value of output) {
|
|
45
46
|
if ((0, runnable_1.isRunnableResponseDelta)(value)) {
|
|
46
47
|
controller.enqueue(value);
|
|
47
|
-
$text += value.$text ||
|
|
48
|
+
$text += value.$text || "";
|
|
48
49
|
Object.assign(result, value.delta);
|
|
49
50
|
}
|
|
50
51
|
}
|
|
@@ -1,15 +1,12 @@
|
|
|
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.outputsToJsonSchema = outputsToJsonSchema;
|
|
7
|
-
const omitBy_1 =
|
|
4
|
+
const omitBy_1 = require("lodash/omitBy");
|
|
8
5
|
const is_non_nullable_1 = require("./is-non-nullable");
|
|
9
6
|
const ordered_map_1 = require("./ordered-map");
|
|
10
7
|
function outputsToJsonSchema(outputs) {
|
|
11
8
|
const outputToSchema = (output) => {
|
|
12
|
-
const properties = output.type ===
|
|
9
|
+
const properties = output.type === "object" && output.properties?.$indexes.length
|
|
13
10
|
? ordered_map_1.OrderedRecord.map(output.properties, (property) => {
|
|
14
11
|
if (!property.name)
|
|
15
12
|
return null;
|
|
@@ -25,15 +22,19 @@ function outputsToJsonSchema(outputs) {
|
|
|
25
22
|
properties: properties?.length
|
|
26
23
|
? Object.fromEntries(properties.map((p) => [p.property.name, p.schema]))
|
|
27
24
|
: undefined,
|
|
28
|
-
items: output.type ===
|
|
29
|
-
|
|
25
|
+
items: output.type === "array" && output.items
|
|
26
|
+
? outputToSchema(output.items)
|
|
27
|
+
: undefined,
|
|
28
|
+
additionalProperties: output.type === "object" ? false : undefined,
|
|
30
29
|
required: properties?.length
|
|
31
|
-
? properties
|
|
30
|
+
? properties
|
|
31
|
+
.filter((i) => i.property.required)
|
|
32
|
+
.map((i) => i.property.name)
|
|
32
33
|
: undefined,
|
|
33
34
|
}, (v) => v === undefined);
|
|
34
35
|
};
|
|
35
36
|
return outputToSchema({
|
|
36
|
-
type:
|
|
37
|
+
type: "object",
|
|
37
38
|
properties: outputs,
|
|
38
39
|
});
|
|
39
40
|
}
|
package/lib/esm/agent.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import logger from
|
|
2
|
-
import { Runnable, } from
|
|
3
|
-
import { OrderedRecord, extractOutputsFromRunnableOutput, isAsyncGenerator, isNonNullable, objectToRunnableResponseStream, renderMessage, runnableResponseStreamToObject, } from
|
|
1
|
+
import logger from "./logger";
|
|
2
|
+
import { Runnable, } from "./runnable";
|
|
3
|
+
import { OrderedRecord, extractOutputsFromRunnableOutput, isAsyncGenerator, isNonNullable, objectToRunnableResponseStream, renderMessage, runnableResponseStreamToObject, } from "./utils";
|
|
4
4
|
export class Agent extends Runnable {
|
|
5
5
|
async getMemoryQuery(input, query) {
|
|
6
|
-
if (query?.from ===
|
|
6
|
+
if (query?.from === "variable") {
|
|
7
7
|
const i = OrderedRecord.find(this.definition.inputs, (i) => i.id === query.fromVariableId);
|
|
8
8
|
if (!i)
|
|
9
9
|
throw new Error(`Input variable ${query.fromVariableId} not found`);
|
|
10
10
|
const value = input[i.name];
|
|
11
|
-
return renderMessage(
|
|
11
|
+
return renderMessage("{{value}}", { value });
|
|
12
12
|
}
|
|
13
13
|
return Object.entries(input)
|
|
14
14
|
.map(([key, value]) => `${key} ${value}`)
|
|
15
|
-
.join(
|
|
15
|
+
.join("\n");
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* Load memories that are defined in the agent definition.
|
|
@@ -27,7 +27,11 @@ export class Agent extends Runnable {
|
|
|
27
27
|
if (!name || !memory)
|
|
28
28
|
return null;
|
|
29
29
|
const q = await this.getMemoryQuery(input, query);
|
|
30
|
-
const { results: memories } = await memory.search(q, {
|
|
30
|
+
const { results: memories } = await memory.search(q, {
|
|
31
|
+
...options,
|
|
32
|
+
userId,
|
|
33
|
+
sessionId,
|
|
34
|
+
});
|
|
31
35
|
return [
|
|
32
36
|
[id, memories],
|
|
33
37
|
[name, memories],
|
|
@@ -52,7 +56,9 @@ export class Agent extends Runnable {
|
|
|
52
56
|
}));
|
|
53
57
|
}
|
|
54
58
|
async run(input, options) {
|
|
55
|
-
logger.debug(`AIGNE core: run agent ${this.name || this.id} with`, {
|
|
59
|
+
logger.debug(`AIGNE core: run agent ${this.name || this.id} with`, {
|
|
60
|
+
input,
|
|
61
|
+
});
|
|
56
62
|
const memories = await this.loadMemories(input, this.context);
|
|
57
63
|
const processResult = await this.process(input, { ...options, memories });
|
|
58
64
|
if (options?.stream) {
|
|
@@ -62,7 +68,9 @@ export class Agent extends Runnable {
|
|
|
62
68
|
: objectToRunnableResponseStream(processResult);
|
|
63
69
|
return extractOutputsFromRunnableOutput(stream, async (result) => {
|
|
64
70
|
// TODO: validate result against outputs schema
|
|
65
|
-
logger.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
|
|
71
|
+
logger.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
|
|
72
|
+
result,
|
|
73
|
+
});
|
|
66
74
|
await this.onResult(result);
|
|
67
75
|
});
|
|
68
76
|
}
|
|
@@ -71,7 +79,9 @@ export class Agent extends Runnable {
|
|
|
71
79
|
: Symbol.asyncIterator in processResult
|
|
72
80
|
? await runnableResponseStreamToObject(processResult)
|
|
73
81
|
: processResult;
|
|
74
|
-
logger.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
|
|
82
|
+
logger.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
|
|
83
|
+
result,
|
|
84
|
+
});
|
|
75
85
|
// TODO: validate result against outputs schema
|
|
76
86
|
await this.onResult(result);
|
|
77
87
|
return result;
|
package/lib/esm/constants.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export const TYPES = {
|
|
2
|
-
context: Symbol.for(
|
|
3
|
-
definition: Symbol.for(
|
|
4
|
-
llmModel: Symbol.for(
|
|
5
|
-
functionRunner: Symbol.for(
|
|
2
|
+
context: Symbol.for("AIGNE_CONTEXT"),
|
|
3
|
+
definition: Symbol.for("AIGNE_DEFINITION"),
|
|
4
|
+
llmModel: Symbol.for("AIGNE_LLM_MODEL"),
|
|
5
|
+
functionRunner: Symbol.for("AIGNE_FUNCTION_RUNNER"),
|
|
6
6
|
};
|
|
7
|
-
export const StreamTextOutputName =
|
|
7
|
+
export const StreamTextOutputName = "$text";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { nanoid } from
|
|
2
|
-
import { OrderedRecord } from
|
|
1
|
+
import { nanoid } from "nanoid";
|
|
2
|
+
import { OrderedRecord } from "../utils/ordered-map";
|
|
3
3
|
export function schemaToDataType(dataType) {
|
|
4
4
|
return OrderedRecord.fromArray(Object.entries(dataType).map(([name, schema]) => {
|
|
5
5
|
const base = {
|
|
@@ -8,32 +8,33 @@ export function schemaToDataType(dataType) {
|
|
|
8
8
|
name,
|
|
9
9
|
};
|
|
10
10
|
switch (schema.type) {
|
|
11
|
-
case
|
|
11
|
+
case "string":
|
|
12
12
|
return {
|
|
13
13
|
...base,
|
|
14
|
-
type:
|
|
14
|
+
type: "string",
|
|
15
15
|
};
|
|
16
|
-
case
|
|
16
|
+
case "number":
|
|
17
17
|
return {
|
|
18
18
|
...base,
|
|
19
|
-
type:
|
|
19
|
+
type: "number",
|
|
20
20
|
};
|
|
21
|
-
case
|
|
21
|
+
case "boolean":
|
|
22
22
|
return {
|
|
23
23
|
...base,
|
|
24
|
-
type:
|
|
24
|
+
type: "boolean",
|
|
25
25
|
};
|
|
26
|
-
case
|
|
26
|
+
case "object":
|
|
27
27
|
return {
|
|
28
28
|
...base,
|
|
29
|
-
type:
|
|
29
|
+
type: "object",
|
|
30
30
|
properties: schema.properties && schemaToDataType(schema.properties),
|
|
31
31
|
};
|
|
32
|
-
case
|
|
32
|
+
case "array":
|
|
33
33
|
return {
|
|
34
34
|
...base,
|
|
35
|
-
type:
|
|
36
|
-
items: schema.items &&
|
|
35
|
+
type: "array",
|
|
36
|
+
items: schema.items &&
|
|
37
|
+
OrderedRecord.find(schemaToDataType({ items: schema.items }), (i) => i.name === "items"),
|
|
37
38
|
};
|
|
38
39
|
default: {
|
|
39
40
|
throw new Error(`Unknown data type: ${schema.type}`);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { nanoid } from
|
|
2
|
-
import { OrderedRecord } from
|
|
1
|
+
import { nanoid } from "nanoid";
|
|
2
|
+
import { OrderedRecord } from "../utils";
|
|
3
3
|
export function toRunnableMemories(agentName, inputs, memories) {
|
|
4
4
|
return OrderedRecord.fromArray(Object.entries(memories).map(([name, { memory, query, options }]) => {
|
|
5
5
|
const queryFromVariable = query?.fromVariable
|
|
@@ -11,7 +11,9 @@ export function toRunnableMemories(agentName, inputs, memories) {
|
|
|
11
11
|
id: name || nanoid(),
|
|
12
12
|
name,
|
|
13
13
|
memory,
|
|
14
|
-
query: queryFromVariable
|
|
14
|
+
query: queryFromVariable
|
|
15
|
+
? { from: "variable", fromVariableId: queryFromVariable.id }
|
|
16
|
+
: undefined,
|
|
15
17
|
options,
|
|
16
18
|
};
|
|
17
19
|
}));
|
|
@@ -10,13 +10,12 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
|
-
import { nanoid } from
|
|
14
|
-
import { inject, injectable } from
|
|
15
|
-
import { Agent } from
|
|
16
|
-
import { TYPES } from
|
|
17
|
-
import { schemaToDataType } from
|
|
18
|
-
import { toRunnableMemories } from
|
|
19
|
-
import { FunctionRunner } from './function-runner';
|
|
13
|
+
import { nanoid } from "nanoid";
|
|
14
|
+
import { inject, injectable } from "tsyringe";
|
|
15
|
+
import { Agent } from "./agent";
|
|
16
|
+
import { TYPES } from "./constants";
|
|
17
|
+
import { schemaToDataType, } from "./definitions/data-type-schema";
|
|
18
|
+
import { toRunnableMemories, } from "./definitions/memory";
|
|
20
19
|
let FunctionAgent = class FunctionAgent extends Agent {
|
|
21
20
|
definition;
|
|
22
21
|
runner;
|
|
@@ -30,11 +29,11 @@ let FunctionAgent = class FunctionAgent extends Agent {
|
|
|
30
29
|
async process(input, options) {
|
|
31
30
|
const { definition: { language, code, ...definition }, runner, context, } = this;
|
|
32
31
|
if (!runner)
|
|
33
|
-
throw new Error(
|
|
32
|
+
throw new Error("Function runner is required");
|
|
34
33
|
if (!code)
|
|
35
|
-
throw new Error(
|
|
34
|
+
throw new Error("Code is required");
|
|
36
35
|
if (!context)
|
|
37
|
-
throw new Error(
|
|
36
|
+
throw new Error("Context is required");
|
|
38
37
|
return await runner.run({
|
|
39
38
|
name: definition.name || definition.id,
|
|
40
39
|
language,
|
|
@@ -50,7 +49,7 @@ FunctionAgent = __decorate([
|
|
|
50
49
|
__param(0, inject(TYPES.definition)),
|
|
51
50
|
__param(1, inject(TYPES.context)),
|
|
52
51
|
__param(2, inject(TYPES.functionRunner)),
|
|
53
|
-
__metadata("design:paramtypes", [Object, Object,
|
|
52
|
+
__metadata("design:paramtypes", [Object, Object, Function])
|
|
54
53
|
], FunctionAgent);
|
|
55
54
|
export { FunctionAgent };
|
|
56
55
|
export function create({ context, ...options }) {
|
|
@@ -61,7 +60,7 @@ export function create({ context, ...options }) {
|
|
|
61
60
|
return new FunctionAgent({
|
|
62
61
|
id: agentId,
|
|
63
62
|
name: options.name,
|
|
64
|
-
type:
|
|
63
|
+
type: "function_agent",
|
|
65
64
|
inputs,
|
|
66
65
|
outputs,
|
|
67
66
|
memories,
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { Agent } from
|
|
2
|
-
import { OrderedRecord } from
|
|
1
|
+
import { Agent } from "./agent";
|
|
2
|
+
import { OrderedRecord } from "./utils";
|
|
3
3
|
export class FunctionRunner extends Agent {
|
|
4
4
|
constructor(context) {
|
|
5
5
|
super({
|
|
6
|
-
id:
|
|
7
|
-
type:
|
|
8
|
-
name:
|
|
9
|
-
description:
|
|
6
|
+
id: "function_runner",
|
|
7
|
+
type: "function_runner",
|
|
8
|
+
name: "Function Runner",
|
|
9
|
+
description: "Run a function",
|
|
10
10
|
inputs: OrderedRecord.fromArray([
|
|
11
|
-
{ id:
|
|
12
|
-
{ id:
|
|
13
|
-
{ id:
|
|
14
|
-
{ id:
|
|
15
|
-
{ id:
|
|
16
|
-
{ id:
|
|
11
|
+
{ id: "name", name: "name", type: "string", required: true },
|
|
12
|
+
{ id: "language", name: "language", type: "string" },
|
|
13
|
+
{ id: "code", name: "code", type: "string", required: true },
|
|
14
|
+
{ id: "input", name: "input", type: "object", required: true },
|
|
15
|
+
{ id: "memories", name: "memories", type: "object", required: true },
|
|
16
|
+
{ id: "context", name: "context", type: "object", required: true },
|
|
17
17
|
]),
|
|
18
18
|
outputs: OrderedRecord.fromArray([
|
|
19
19
|
{
|
|
20
|
-
id:
|
|
21
|
-
name:
|
|
22
|
-
type:
|
|
20
|
+
id: "result",
|
|
21
|
+
name: "result",
|
|
22
|
+
type: "object",
|
|
23
23
|
},
|
|
24
24
|
]),
|
|
25
25
|
}, context);
|