@aigne/core 0.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/assistant/generate-output.js +101 -0
- package/lib/cjs/assistant/select-agent.js +76 -0
- package/lib/cjs/assistant/type.js +11 -0
- package/lib/cjs/common/aid.js +42 -0
- package/lib/cjs/common/index.js +238 -0
- package/lib/cjs/common/resource-manager.js +199 -0
- package/lib/cjs/constants.js +9 -0
- package/lib/cjs/executor/agent.js +10 -0
- package/lib/cjs/executor/aigc.js +28 -0
- package/lib/cjs/executor/api.js +64 -0
- package/lib/cjs/executor/base.js +676 -0
- package/lib/cjs/executor/blocklet.js +25 -0
- package/lib/cjs/executor/call-agent.js +105 -0
- package/lib/cjs/executor/decision.js +478 -0
- package/lib/cjs/executor/image-blender.js +32 -0
- package/lib/cjs/executor/index.js +81 -0
- package/lib/cjs/executor/llm.js +379 -0
- package/lib/cjs/executor/logic.js +167 -0
- package/lib/cjs/index.js +17 -0
- package/lib/cjs/libs/blocklet/vc.js +92 -0
- package/lib/cjs/libs/openapi/request/index.js +24 -0
- package/lib/cjs/libs/openapi/request/util.js +146 -0
- package/lib/cjs/libs/openapi/types/index.js +17 -0
- package/lib/cjs/libs/openapi/util/call.js +15 -0
- package/lib/cjs/libs/openapi/util/check-schema.js +67 -0
- package/lib/cjs/libs/openapi/util/convert-schema.js +44 -0
- package/lib/cjs/libs/openapi/util/flatten-open-api.js +21 -0
- package/lib/cjs/libs/openapi/util/get-open-api-i18n-text.js +7 -0
- package/lib/cjs/logger.js +4 -0
- package/lib/cjs/runtime/resource-blocklet.js +5 -0
- package/lib/cjs/runtime/runtime.js +143 -0
- package/lib/cjs/types/assistant/index.js +31 -0
- package/lib/cjs/types/assistant/mustache/ReadableMustache.js +69 -0
- package/lib/cjs/types/assistant/mustache/directive.js +35 -0
- package/lib/cjs/types/assistant/mustache/mustache.js +688 -0
- package/lib/cjs/types/common/index.js +2 -0
- package/lib/cjs/types/index.js +20 -0
- package/lib/cjs/types/resource/index.js +47 -0
- package/lib/cjs/types/resource/project.js +35 -0
- package/lib/cjs/types/runtime/agent.js +2 -0
- package/lib/cjs/types/runtime/error.js +18 -0
- package/lib/cjs/types/runtime/index.js +37 -0
- package/lib/cjs/types/runtime/runtime-resource-blocklet-state.js +4 -0
- package/lib/cjs/types/runtime/schema.js +259 -0
- package/lib/cjs/utils/cron-job.js +48 -0
- package/lib/cjs/utils/extract-metadata-transform.js +58 -0
- package/lib/cjs/utils/extract-metadata-transform.test.js +61 -0
- package/lib/cjs/utils/fs.js +49 -0
- package/lib/cjs/utils/get-blocklet-agent.js +351 -0
- package/lib/cjs/utils/geti.js +37 -0
- package/lib/cjs/utils/is-non-nullable.js +20 -0
- package/lib/cjs/utils/render-message.js +23 -0
- package/lib/cjs/utils/resolve-secret-inputs.js +49 -0
- package/lib/cjs/utils/retry.js +19 -0
- package/lib/cjs/utils/task-id.js +7 -0
- package/lib/cjs/utils/tool-calls-transform.js +18 -0
- package/lib/esm/assistant/generate-output.js +91 -0
- package/lib/esm/assistant/select-agent.js +71 -0
- package/lib/esm/assistant/type.js +7 -0
- package/lib/esm/common/aid.js +38 -0
- package/lib/esm/common/index.js +232 -0
- package/lib/esm/common/resource-manager.js +192 -0
- package/lib/esm/constants.js +6 -0
- package/lib/esm/executor/agent.js +6 -0
- package/lib/esm/executor/aigc.js +24 -0
- package/lib/esm/executor/api.js +34 -0
- package/lib/esm/executor/base.js +668 -0
- package/lib/esm/executor/blocklet.js +21 -0
- package/lib/esm/executor/call-agent.js +98 -0
- package/lib/esm/executor/decision.js +471 -0
- package/lib/esm/executor/image-blender.js +25 -0
- package/lib/esm/executor/index.js +74 -0
- package/lib/esm/executor/llm.js +372 -0
- package/lib/esm/executor/logic.js +160 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/libs/blocklet/vc.js +85 -0
- package/lib/esm/libs/openapi/request/index.js +20 -0
- package/lib/esm/libs/openapi/request/util.js +136 -0
- package/lib/esm/libs/openapi/types/index.js +1 -0
- package/lib/esm/libs/openapi/util/call.js +8 -0
- package/lib/esm/libs/openapi/util/check-schema.js +62 -0
- package/lib/esm/libs/openapi/util/convert-schema.js +42 -0
- package/lib/esm/libs/openapi/util/flatten-open-api.js +19 -0
- package/lib/esm/libs/openapi/util/get-open-api-i18n-text.js +5 -0
- package/lib/esm/logger.js +2 -0
- package/lib/esm/runtime/resource-blocklet.js +2 -0
- package/lib/esm/runtime/runtime.js +136 -0
- package/lib/esm/types/assistant/index.js +9 -0
- package/lib/esm/types/assistant/mustache/ReadableMustache.js +63 -0
- package/lib/esm/types/assistant/mustache/directive.js +29 -0
- package/lib/esm/types/assistant/mustache/mustache.js +686 -0
- package/lib/esm/types/common/index.js +1 -0
- package/lib/esm/types/index.js +4 -0
- package/lib/esm/types/resource/index.js +26 -0
- package/lib/esm/types/resource/project.js +29 -0
- package/lib/esm/types/runtime/agent.js +1 -0
- package/lib/esm/types/runtime/error.js +14 -0
- package/lib/esm/types/runtime/index.js +20 -0
- package/lib/esm/types/runtime/runtime-resource-blocklet-state.js +1 -0
- package/lib/esm/types/runtime/schema.js +249 -0
- package/lib/esm/utils/cron-job.js +44 -0
- package/lib/esm/utils/extract-metadata-transform.js +54 -0
- package/lib/esm/utils/extract-metadata-transform.test.js +59 -0
- package/lib/esm/utils/fs.js +41 -0
- package/lib/esm/utils/get-blocklet-agent.js +344 -0
- package/lib/esm/utils/geti.js +30 -0
- package/lib/esm/utils/is-non-nullable.js +13 -0
- package/lib/esm/utils/render-message.js +20 -0
- package/lib/esm/utils/resolve-secret-inputs.js +46 -0
- package/lib/esm/utils/retry.js +16 -0
- package/lib/esm/utils/task-id.js +3 -0
- package/lib/esm/utils/tool-calls-transform.js +15 -0
- package/lib/types/assistant/generate-output.d.ts +29 -0
- package/lib/types/assistant/select-agent.d.ts +14 -0
- package/lib/types/assistant/type.d.ts +61 -0
- package/lib/types/common/aid.d.ts +18 -0
- package/lib/types/common/index.d.ts +7 -0
- package/lib/types/common/resource-manager.d.ts +88 -0
- package/lib/types/constants.d.ts +6 -0
- package/lib/types/executor/agent.d.ts +5 -0
- package/lib/types/executor/aigc.d.ts +9 -0
- package/lib/types/executor/api.d.ts +9 -0
- package/lib/types/executor/base.d.ts +209 -0
- package/lib/types/executor/blocklet.d.ts +9 -0
- package/lib/types/executor/call-agent.d.ts +12 -0
- package/lib/types/executor/decision.d.ts +20 -0
- package/lib/types/executor/image-blender.d.ts +9 -0
- package/lib/types/executor/index.d.ts +8 -0
- package/lib/types/executor/llm.d.ts +38 -0
- package/lib/types/executor/logic.d.ts +9 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/libs/blocklet/vc.d.ts +17 -0
- package/lib/types/libs/openapi/request/index.d.ts +17 -0
- package/lib/types/libs/openapi/request/util.d.ts +40 -0
- package/lib/types/libs/openapi/types/index.d.ts +20 -0
- package/lib/types/libs/openapi/util/call.d.ts +2 -0
- package/lib/types/libs/openapi/util/check-schema.d.ts +3 -0
- package/lib/types/libs/openapi/util/convert-schema.d.ts +8 -0
- package/lib/types/libs/openapi/util/flatten-open-api.d.ts +3 -0
- package/lib/types/libs/openapi/util/get-open-api-i18n-text.d.ts +2 -0
- package/lib/types/logger.d.ts +2 -0
- package/lib/types/runtime/resource-blocklet.d.ts +2 -0
- package/lib/types/runtime/runtime.d.ts +20 -0
- package/lib/types/types/assistant/index.d.ts +405 -0
- package/lib/types/types/assistant/mustache/ReadableMustache.d.ts +2 -0
- package/lib/types/types/assistant/mustache/directive.d.ts +6 -0
- package/lib/types/types/assistant/mustache/mustache.d.ts +2 -0
- package/lib/types/types/common/index.d.ts +45 -0
- package/lib/types/types/index.d.ts +4 -0
- package/lib/types/types/resource/index.d.ts +17 -0
- package/lib/types/types/resource/project.d.ts +41 -0
- package/lib/types/types/runtime/agent.d.ts +33 -0
- package/lib/types/types/runtime/error.d.ts +10 -0
- package/lib/types/types/runtime/index.d.ts +116 -0
- package/lib/types/types/runtime/runtime-resource-blocklet-state.d.ts +5 -0
- package/lib/types/types/runtime/schema.d.ts +110 -0
- package/lib/types/utils/cron-job.d.ts +22 -0
- package/lib/types/utils/extract-metadata-transform.d.ts +16 -0
- package/lib/types/utils/extract-metadata-transform.test.d.ts +1 -0
- package/lib/types/utils/fs.d.ts +9 -0
- package/lib/types/utils/get-blocklet-agent.d.ts +219 -0
- package/lib/types/utils/geti.d.ts +1 -0
- package/lib/types/utils/is-non-nullable.d.ts +2 -0
- package/lib/types/utils/render-message.d.ts +6 -0
- package/lib/types/utils/resolve-secret-inputs.d.ts +11 -0
- package/lib/types/utils/retry.d.ts +1 -0
- package/lib/types/utils/task-id.d.ts +1 -0
- package/lib/types/utils/tool-calls-transform.d.ts +2 -0
- package/package.json +67 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.authService = void 0;
|
|
7
|
+
exports.issueVC = issueVC;
|
|
8
|
+
const auth_1 = __importDefault(require("@blocklet/sdk/lib/service/auth"));
|
|
9
|
+
const constants_1 = require("../../constants");
|
|
10
|
+
exports.authService = new auth_1.default();
|
|
11
|
+
async function issueVC({ context: { entry }, userDid, name, title, description, reissue = false, displayUrl, notify = true, }) {
|
|
12
|
+
// NOTE: ensure name starts with AIGNE_ISSUE_VC_PREFIX to avoid conflict with internal passports such as admin/owner
|
|
13
|
+
if (!name.startsWith(constants_1.AIGNE_ISSUE_VC_PREFIX))
|
|
14
|
+
name = `${constants_1.AIGNE_ISSUE_VC_PREFIX}${name}`;
|
|
15
|
+
await createPassportIfNotExist({ name, title, description });
|
|
16
|
+
const userResult = await exports.authService.getUser(userDid, { includeTags: true });
|
|
17
|
+
if (!userResult.user)
|
|
18
|
+
throw new Error(`User not found ${userDid}`);
|
|
19
|
+
if (!reissue) {
|
|
20
|
+
const vc = userResult.user.passports.find((p) => p.role === name);
|
|
21
|
+
if (vc)
|
|
22
|
+
return { user: userResult.user, vc };
|
|
23
|
+
}
|
|
24
|
+
const issueResult = await exports.authService.issuePassportToUser({
|
|
25
|
+
userDid,
|
|
26
|
+
role: name,
|
|
27
|
+
display: displayUrl ? { type: 'url', content: displayUrl } : undefined,
|
|
28
|
+
notify,
|
|
29
|
+
notification: JSON.stringify({
|
|
30
|
+
title: `You just received a VC ${title}`,
|
|
31
|
+
body: description,
|
|
32
|
+
attachments: [
|
|
33
|
+
{
|
|
34
|
+
type: 'image',
|
|
35
|
+
data: {
|
|
36
|
+
url: displayUrl,
|
|
37
|
+
alt: title,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
appInfo: {
|
|
42
|
+
title: entry.project.name,
|
|
43
|
+
// logo: getProjectIconUrl(entry.project.id, {
|
|
44
|
+
// blockletDid: entry.blockletDid,
|
|
45
|
+
// working: entry.working,
|
|
46
|
+
// updatedAt: entry.project.updatedAt,
|
|
47
|
+
// }),
|
|
48
|
+
url: entry.appUrl,
|
|
49
|
+
description: entry.project.description,
|
|
50
|
+
},
|
|
51
|
+
poweredBy: {
|
|
52
|
+
name: 'AIGNE',
|
|
53
|
+
url: 'https://www.aigne.io',
|
|
54
|
+
},
|
|
55
|
+
severity: 'success',
|
|
56
|
+
}),
|
|
57
|
+
});
|
|
58
|
+
if (issueResult.code !== 'ok')
|
|
59
|
+
throw new Error(`Issue VC failed got ${issueResult.code}`);
|
|
60
|
+
const vc = issueResult.user.passports.findLast((p) => p.role === name);
|
|
61
|
+
if (!vc)
|
|
62
|
+
throw new Error('Issue VC got empty result');
|
|
63
|
+
return { user: issueResult.user, vc };
|
|
64
|
+
}
|
|
65
|
+
const CREATE_PASSPORT_TASKS = {};
|
|
66
|
+
async function createPassportIfNotExist({ name, title, description, }) {
|
|
67
|
+
CREATE_PASSPORT_TASKS[name] ??= (async () => {
|
|
68
|
+
try {
|
|
69
|
+
let result = await exports.authService.getRole(name);
|
|
70
|
+
if (!result.role) {
|
|
71
|
+
// Create a role for custom display
|
|
72
|
+
result = await exports.authService.createRole({
|
|
73
|
+
title,
|
|
74
|
+
name,
|
|
75
|
+
description,
|
|
76
|
+
permissions: [],
|
|
77
|
+
extra: JSON.stringify({
|
|
78
|
+
display: 'custom',
|
|
79
|
+
types: [name],
|
|
80
|
+
}),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
// NOTE: reset task if failed
|
|
87
|
+
delete CREATE_PASSPORT_TASKS[name];
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
})();
|
|
91
|
+
return CREATE_PASSPORT_TASKS[name];
|
|
92
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.callBlockletApi = void 0;
|
|
4
|
+
const component_1 = require("@blocklet/sdk/lib/component");
|
|
5
|
+
const util_1 = require("./util");
|
|
6
|
+
const callBlockletApi = (pathItem, data, options) => {
|
|
7
|
+
const requestConfig = (0, util_1.getRequestConfig)(pathItem, data, {
|
|
8
|
+
params: options?.params || {},
|
|
9
|
+
data: options?.data || {},
|
|
10
|
+
});
|
|
11
|
+
const { headers, method, url, params, ...config } = requestConfig;
|
|
12
|
+
const did = options?.user?.did || '';
|
|
13
|
+
if (!pathItem.name)
|
|
14
|
+
throw new Error('Blocklet name is required to call blocklet api');
|
|
15
|
+
return (0, component_1.call)({
|
|
16
|
+
...config,
|
|
17
|
+
method: method,
|
|
18
|
+
name: pathItem.name,
|
|
19
|
+
path: url,
|
|
20
|
+
headers,
|
|
21
|
+
params: { ...params, userId: did },
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
exports.callBlockletApi = callBlockletApi;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getRequestConfig = void 0;
|
|
7
|
+
exports.extractRequestBodyParameters = extractRequestBodyParameters;
|
|
8
|
+
exports.getAllParameters = getAllParameters;
|
|
9
|
+
exports.getRequiredFields = getRequiredFields;
|
|
10
|
+
const convert_schema_1 = __importDefault(require("../util/convert-schema"));
|
|
11
|
+
const getRequestConfig = (pathItem, requestData, options) => {
|
|
12
|
+
let url = pathItem.path || '';
|
|
13
|
+
const config = {
|
|
14
|
+
method: pathItem.method,
|
|
15
|
+
url,
|
|
16
|
+
headers: {},
|
|
17
|
+
params: options?.params || {},
|
|
18
|
+
data: options?.data || {},
|
|
19
|
+
cookies: {},
|
|
20
|
+
body: {},
|
|
21
|
+
};
|
|
22
|
+
const data = { ...requestData };
|
|
23
|
+
const requestBodyData = { ...data };
|
|
24
|
+
for (const parameter of pathItem.parameters || []) {
|
|
25
|
+
if (Object.prototype.hasOwnProperty.call(data, parameter.name)) {
|
|
26
|
+
const value = data[parameter.name];
|
|
27
|
+
switch (parameter.in) {
|
|
28
|
+
case 'path':
|
|
29
|
+
url = url.replace(`{${parameter.name}}`, encodeURIComponent(value));
|
|
30
|
+
break;
|
|
31
|
+
case 'query':
|
|
32
|
+
config.params[parameter.name] = value;
|
|
33
|
+
break;
|
|
34
|
+
case 'header':
|
|
35
|
+
config.headers[parameter.name] = value;
|
|
36
|
+
break;
|
|
37
|
+
case 'cookie':
|
|
38
|
+
config.cookies[parameter.name] = value;
|
|
39
|
+
break;
|
|
40
|
+
default:
|
|
41
|
+
throw new Error('Unsupported parameter type');
|
|
42
|
+
}
|
|
43
|
+
delete requestBodyData[parameter.name];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (['POST', 'PUT', 'PATCH'].includes(pathItem.method.toUpperCase()) && pathItem.requestBody) {
|
|
47
|
+
config.headers['Content-Type'] = getContentType(pathItem.requestBody);
|
|
48
|
+
config.data = getRequestBody(pathItem.requestBody, requestBodyData || {});
|
|
49
|
+
}
|
|
50
|
+
config.url = url;
|
|
51
|
+
config.body = requestBodyData;
|
|
52
|
+
return config;
|
|
53
|
+
};
|
|
54
|
+
exports.getRequestConfig = getRequestConfig;
|
|
55
|
+
function getContentType(requestBody) {
|
|
56
|
+
if (requestBody && requestBody.content) {
|
|
57
|
+
const [firstMediaType] = Object.keys(requestBody.content);
|
|
58
|
+
return firstMediaType || 'application/json';
|
|
59
|
+
}
|
|
60
|
+
return 'application/json';
|
|
61
|
+
}
|
|
62
|
+
function getRequestBody(requestBody, requestBodyData) {
|
|
63
|
+
if (requestBody && requestBody.content) {
|
|
64
|
+
const contentType = getContentType(requestBody);
|
|
65
|
+
const mediaTypeObject = requestBody.content[contentType];
|
|
66
|
+
if (mediaTypeObject && mediaTypeObject.schema) {
|
|
67
|
+
switch (contentType) {
|
|
68
|
+
case 'application/json':
|
|
69
|
+
return JSON.stringify(requestBodyData);
|
|
70
|
+
case 'application/x-www-form-urlencoded':
|
|
71
|
+
return serializeFormEncoded(requestBodyData);
|
|
72
|
+
case 'multipart/form-data':
|
|
73
|
+
// eslint-disable-next-line no-case-declarations
|
|
74
|
+
const formData = new FormData();
|
|
75
|
+
Object.keys(requestBodyData).forEach((key) => {
|
|
76
|
+
const value = requestBodyData[key];
|
|
77
|
+
if (Array.isArray(value)) {
|
|
78
|
+
value.forEach((item, index) => formData.append(`${key}[${index}]`, item));
|
|
79
|
+
}
|
|
80
|
+
else if (typeof value === 'object' && value !== null) {
|
|
81
|
+
Object.keys(value).forEach((subKey) => {
|
|
82
|
+
formData.append(`${key}[${subKey}]`, value[subKey]);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
formData.append(key, value);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
return formData;
|
|
90
|
+
default:
|
|
91
|
+
throw new Error(`Unsupported content type: ${contentType}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return requestBodyData;
|
|
96
|
+
}
|
|
97
|
+
function serializeFormEncoded(data) {
|
|
98
|
+
return Object.keys(data)
|
|
99
|
+
.map((key) => {
|
|
100
|
+
const value = data[key];
|
|
101
|
+
if (typeof value === 'object' && value !== null) {
|
|
102
|
+
return serializeNestedObject(key, value);
|
|
103
|
+
}
|
|
104
|
+
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
105
|
+
})
|
|
106
|
+
.join('&');
|
|
107
|
+
}
|
|
108
|
+
function serializeNestedObject(parentKey, obj) {
|
|
109
|
+
return Object.keys(obj)
|
|
110
|
+
.map((key) => {
|
|
111
|
+
const value = obj[key];
|
|
112
|
+
const fullKey = `${parentKey}[${key}]`;
|
|
113
|
+
if (typeof value === 'object' && value !== null) {
|
|
114
|
+
return serializeNestedObject(fullKey, value);
|
|
115
|
+
}
|
|
116
|
+
return `${encodeURIComponent(fullKey)}=${encodeURIComponent(value)}`;
|
|
117
|
+
})
|
|
118
|
+
.join('&');
|
|
119
|
+
}
|
|
120
|
+
function extractRequestBodyParameters(requestBody) {
|
|
121
|
+
if (!requestBody || !requestBody.content) {
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
const contentType = getContentType(requestBody);
|
|
125
|
+
const mediaTypeObject = requestBody.content[contentType];
|
|
126
|
+
if (!mediaTypeObject || !mediaTypeObject.schema) {
|
|
127
|
+
console.warn(`No schema available for content type: ${contentType}`);
|
|
128
|
+
return [];
|
|
129
|
+
}
|
|
130
|
+
const { schema } = mediaTypeObject;
|
|
131
|
+
const schemaObject = (0, convert_schema_1.default)(schema);
|
|
132
|
+
return Object.entries(schemaObject).map(([key, value]) => ({ name: key, ...value })) || [];
|
|
133
|
+
}
|
|
134
|
+
function getAllParameters(dataset) {
|
|
135
|
+
const requestBody = extractRequestBodyParameters(dataset?.requestBody);
|
|
136
|
+
const datasetParameters = [
|
|
137
|
+
...(dataset?.parameters ?? []).map((i) => ({ ...i, type: i.schema?.type })),
|
|
138
|
+
...(requestBody ?? []),
|
|
139
|
+
];
|
|
140
|
+
return datasetParameters;
|
|
141
|
+
}
|
|
142
|
+
function getRequiredFields(dataset) {
|
|
143
|
+
const parameterFields = dataset.parameters?.filter((param) => param.required).map((param) => param.name) || [];
|
|
144
|
+
const requestBodyFields = Object.values(dataset.requestBody?.content || {}).flatMap((mediaObject) => mediaObject.schema?.required || []);
|
|
145
|
+
return [...new Set([...parameterFields, ...requestBodyFields])];
|
|
146
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("openapi3-ts/oas31"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.call = void 0;
|
|
7
|
+
const component_1 = require("@blocklet/sdk/lib/component");
|
|
8
|
+
const isNil_1 = __importDefault(require("lodash/isNil"));
|
|
9
|
+
const call = (options) => {
|
|
10
|
+
return (0, component_1.call)({
|
|
11
|
+
...options,
|
|
12
|
+
params: Object.fromEntries(Object.entries(options.params ?? {}).map(([key, value]) => [key, (0, isNil_1.default)(value) ? '' : String(value)])),
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
exports.call = call;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const joi_1 = __importDefault(require("joi"));
|
|
7
|
+
const CustomJoi = joi_1.default.extend((joi) => ({
|
|
8
|
+
type: 'string',
|
|
9
|
+
base: joi.string(),
|
|
10
|
+
messages: {
|
|
11
|
+
'string.customId': '{{#label}} does not match the expected format (type:path:method)',
|
|
12
|
+
},
|
|
13
|
+
rules: {
|
|
14
|
+
customId: {
|
|
15
|
+
validate(value, helpers) {
|
|
16
|
+
const [, path, method] = value.split(':');
|
|
17
|
+
const parts = helpers.state.ancestors[0];
|
|
18
|
+
if (!path || !method) {
|
|
19
|
+
return helpers.error('string.customId');
|
|
20
|
+
}
|
|
21
|
+
if (path !== parts.path || (method || '').toLocaleLowerCase() !== (parts.method || '').toLocaleLowerCase()) {
|
|
22
|
+
return helpers.error('string.customId');
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
}));
|
|
29
|
+
const BaseParameterObjectSchema = joi_1.default.object({
|
|
30
|
+
description: joi_1.default.string().optional(),
|
|
31
|
+
required: joi_1.default.boolean().optional(),
|
|
32
|
+
deprecated: joi_1.default.boolean().optional(),
|
|
33
|
+
allowEmptyValue: joi_1.default.boolean().optional(),
|
|
34
|
+
style: joi_1.default.string()
|
|
35
|
+
.valid('matrix', 'label', 'form', 'simple', 'spaceDelimited', 'pipeDelimited', 'deepObject')
|
|
36
|
+
.insensitive()
|
|
37
|
+
.optional(),
|
|
38
|
+
explode: joi_1.default.boolean().optional(),
|
|
39
|
+
allowReserved: joi_1.default.boolean().optional(),
|
|
40
|
+
schema: joi_1.default.any().optional(),
|
|
41
|
+
examples: joi_1.default.object().pattern(joi_1.default.string(), joi_1.default.any).optional(),
|
|
42
|
+
example: joi_1.default.any().optional(),
|
|
43
|
+
content: joi_1.default.any().optional(),
|
|
44
|
+
}).optional();
|
|
45
|
+
const ParameterObjectSchema = BaseParameterObjectSchema.keys({
|
|
46
|
+
name: joi_1.default.string().required(),
|
|
47
|
+
in: joi_1.default.string().valid('query', 'header', 'path', 'cookie').insensitive().required(),
|
|
48
|
+
});
|
|
49
|
+
const RequestBodyObjectSchema = joi_1.default.object({});
|
|
50
|
+
const ResponsesObjectSchema = joi_1.default.object({});
|
|
51
|
+
exports.default = joi_1.default.object({
|
|
52
|
+
id: CustomJoi.string().customId().required(),
|
|
53
|
+
name: joi_1.default.string().optional().allow('').empty([null, '']),
|
|
54
|
+
type: joi_1.default.string().optional().allow('').empty([null, '']),
|
|
55
|
+
url: joi_1.default.string()
|
|
56
|
+
.pattern(/^https?:\/\//)
|
|
57
|
+
.optional()
|
|
58
|
+
.allow('')
|
|
59
|
+
.empty([null, '']),
|
|
60
|
+
path: joi_1.default.string().required(),
|
|
61
|
+
method: joi_1.default.string().valid('GET', 'POST', 'PUT', 'DELETE', 'PATCH').insensitive().required(),
|
|
62
|
+
summary: joi_1.default.string().optional().allow('').empty([null, '']),
|
|
63
|
+
description: joi_1.default.string().optional().allow('').empty([null, '']),
|
|
64
|
+
parameters: joi_1.default.array().items(ParameterObjectSchema).optional().allow(null, '').empty([null, '']),
|
|
65
|
+
requestBody: RequestBodyObjectSchema.optional().allow('').empty([null, '']),
|
|
66
|
+
responses: ResponsesObjectSchema.optional().allow('').empty([null, '']),
|
|
67
|
+
}).unknown(true);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const convertSchemaToObject = (schema) => {
|
|
4
|
+
if (!schema || typeof schema !== 'object') {
|
|
5
|
+
throw new Error('Invalid schema: Schema must be an object');
|
|
6
|
+
}
|
|
7
|
+
switch (schema.type) {
|
|
8
|
+
case 'object':
|
|
9
|
+
return convertObjectSchema(schema);
|
|
10
|
+
case 'array':
|
|
11
|
+
return convertArraySchema(schema);
|
|
12
|
+
default:
|
|
13
|
+
return convertPrimitiveSchema(schema);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const convertObjectSchema = (schema) => {
|
|
17
|
+
const result = {};
|
|
18
|
+
if (schema.properties && typeof schema.properties === 'object') {
|
|
19
|
+
Object.keys(schema.properties).forEach((property) => {
|
|
20
|
+
result[property] = schema.properties[property];
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
const convertArraySchema = (schema) => {
|
|
26
|
+
if (!schema.items) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
return [convertSchemaToObject(schema.items)];
|
|
30
|
+
};
|
|
31
|
+
const convertPrimitiveSchema = (schema) => {
|
|
32
|
+
switch (schema.type) {
|
|
33
|
+
case 'string':
|
|
34
|
+
return { ...schema, value: schema.default ?? (schema.enum ? schema.enum[0] : '') };
|
|
35
|
+
case 'integer':
|
|
36
|
+
case 'number':
|
|
37
|
+
return { ...schema, value: schema.default ?? 0 };
|
|
38
|
+
case 'boolean':
|
|
39
|
+
return { ...schema, value: schema.default ?? false };
|
|
40
|
+
default:
|
|
41
|
+
return { ...schema, value: schema.default ?? null };
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
exports.default = convertSchemaToObject;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function flattenApiStructure(apiStructure) {
|
|
4
|
+
const flattened = [];
|
|
5
|
+
const paths = apiStructure?.paths || {};
|
|
6
|
+
for (const path in paths) {
|
|
7
|
+
for (const method in paths[path]) {
|
|
8
|
+
const endpoint = paths[path]?.[method];
|
|
9
|
+
flattened.push({
|
|
10
|
+
id: endpoint['x-id'],
|
|
11
|
+
name: endpoint['x-did'],
|
|
12
|
+
path: endpoint['x-path'],
|
|
13
|
+
method: endpoint['x-method'],
|
|
14
|
+
did: endpoint['x-did'],
|
|
15
|
+
...endpoint,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return flattened;
|
|
20
|
+
}
|
|
21
|
+
exports.default = flattenApiStructure;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.AIGNERuntime = void 0;
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const call_1 = require("@blocklet/ai-kit/api/call");
|
|
9
|
+
const fs_extra_1 = require("fs-extra");
|
|
10
|
+
const glob_1 = require("glob");
|
|
11
|
+
const yaml_1 = require("yaml");
|
|
12
|
+
const common_1 = require("../common");
|
|
13
|
+
const aid_1 = require("../common/aid");
|
|
14
|
+
const executor_1 = require("../executor");
|
|
15
|
+
const logger_1 = __importDefault(require("../logger"));
|
|
16
|
+
const task_id_1 = require("../utils/task-id");
|
|
17
|
+
const resource_blocklet_1 = require("./resource-blocklet");
|
|
18
|
+
class AIGNERuntime {
|
|
19
|
+
project;
|
|
20
|
+
static async load(options) {
|
|
21
|
+
const projectFilePath = (0, path_1.join)(options.path, 'project.yaml');
|
|
22
|
+
const project = (0, yaml_1.parse)((await (0, fs_extra_1.readFile)(projectFilePath)).toString());
|
|
23
|
+
// TODO: validate parsed project
|
|
24
|
+
const agentFilePaths = await (0, glob_1.glob)((0, path_1.join)(options.path, 'prompts', '**/*.yaml'));
|
|
25
|
+
const agents = await Promise.all(agentFilePaths.map(async (filename) => {
|
|
26
|
+
const agent = (0, yaml_1.parse)((await (0, fs_extra_1.readFile)(filename)).toString());
|
|
27
|
+
// TODO: validate parsed agent
|
|
28
|
+
return agent;
|
|
29
|
+
}));
|
|
30
|
+
const memoryFilePath = (0, path_1.join)(options.path, 'config/variable.yaml');
|
|
31
|
+
const memories = (0, yaml_1.parse)((await (0, fs_extra_1.readFile)(memoryFilePath)).toString())?.variables;
|
|
32
|
+
// TODO: validate parsed memories
|
|
33
|
+
const p = {
|
|
34
|
+
...project,
|
|
35
|
+
agents,
|
|
36
|
+
memories,
|
|
37
|
+
};
|
|
38
|
+
return new AIGNERuntime(p);
|
|
39
|
+
}
|
|
40
|
+
constructor(project) {
|
|
41
|
+
this.project = project;
|
|
42
|
+
}
|
|
43
|
+
async runAgent(agentId, inputs) {
|
|
44
|
+
const agent = this.project.agents?.find((a) => a.id === agentId);
|
|
45
|
+
if (!agent)
|
|
46
|
+
throw new Error(`Agent ${agentId} not found`);
|
|
47
|
+
const taskId = (0, task_id_1.nextId)();
|
|
48
|
+
const messageId = (0, task_id_1.nextId)();
|
|
49
|
+
const callAI = async ({ input }) => {
|
|
50
|
+
return await (0, call_1.chatCompletions)({ ...input, model: input.model || common_1.defaultTextModel });
|
|
51
|
+
};
|
|
52
|
+
const callAIImage = async ({ assistant, input }) => {
|
|
53
|
+
const imageAssistant = assistant.type === 'image' ? assistant : undefined;
|
|
54
|
+
const supportImages = await (0, common_1.getSupportedImagesModels)();
|
|
55
|
+
const imageModel = supportImages.find((i) => i.model === (imageAssistant?.model || common_1.defaultImageModel));
|
|
56
|
+
const model = {
|
|
57
|
+
model: input.model || imageModel?.model,
|
|
58
|
+
n: input.n || imageModel?.nDefault,
|
|
59
|
+
quality: input.quality || imageModel?.qualityDefault,
|
|
60
|
+
style: input.style || imageModel?.styleDefault,
|
|
61
|
+
size: input.size || imageModel?.sizeDefault,
|
|
62
|
+
};
|
|
63
|
+
return (0, call_1.imageGenerations)({
|
|
64
|
+
...input,
|
|
65
|
+
...model,
|
|
66
|
+
responseFormat: 'url',
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
const executor = new executor_1.RuntimeExecutor({
|
|
70
|
+
entry: { project: this.project },
|
|
71
|
+
callback: () => { },
|
|
72
|
+
callAI,
|
|
73
|
+
callAIImage,
|
|
74
|
+
getMemoryVariables: async (options) => {
|
|
75
|
+
if (options.projectId === this.project.id)
|
|
76
|
+
return this.project.memories ?? [];
|
|
77
|
+
logger_1.default.warn('Unsupported to get memory variables from other projects');
|
|
78
|
+
return [];
|
|
79
|
+
},
|
|
80
|
+
getAgent: async (options) => {
|
|
81
|
+
const identity = (0, aid_1.parseIdentity)(options.aid, { rejectWhenError: true });
|
|
82
|
+
let agent;
|
|
83
|
+
if (identity.blockletDid) {
|
|
84
|
+
const { blockletDid, projectId, agentId } = identity;
|
|
85
|
+
const res = await resource_blocklet_1.resourceManager.getAgent({ blockletDid, projectId, agentId });
|
|
86
|
+
if (res) {
|
|
87
|
+
agent = {
|
|
88
|
+
...res.agent,
|
|
89
|
+
project: res.project,
|
|
90
|
+
identity: {
|
|
91
|
+
blockletDid,
|
|
92
|
+
projectId,
|
|
93
|
+
agentId,
|
|
94
|
+
aid: (0, aid_1.stringifyIdentity)({ blockletDid, projectId, agentId }),
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else if (identity.projectId === this.project.id) {
|
|
100
|
+
const a = this.project.agents?.find((a) => a.id === identity.agentId);
|
|
101
|
+
if (a) {
|
|
102
|
+
agent = {
|
|
103
|
+
...a,
|
|
104
|
+
project: this.project,
|
|
105
|
+
identity: {
|
|
106
|
+
projectId: this.project.id,
|
|
107
|
+
agentId: a.id,
|
|
108
|
+
aid: (0, aid_1.stringifyIdentity)({ projectId: this.project.id, agentId: a.id }),
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (options.rejectOnEmpty && !agent)
|
|
114
|
+
throw new Error('No such agent');
|
|
115
|
+
return agent;
|
|
116
|
+
},
|
|
117
|
+
entryProjectId: this.project.id,
|
|
118
|
+
sessionId: this.project.id,
|
|
119
|
+
messageId,
|
|
120
|
+
clientTime: new Date().toISOString(),
|
|
121
|
+
queryCache: async ({ aid, cacheKey }) => {
|
|
122
|
+
// TODO: implement cache
|
|
123
|
+
return null;
|
|
124
|
+
},
|
|
125
|
+
setCache: async ({ aid, cacheKey, inputs, outputs }) => {
|
|
126
|
+
// TODO: implement cache
|
|
127
|
+
},
|
|
128
|
+
}, {
|
|
129
|
+
...agent,
|
|
130
|
+
identity: {
|
|
131
|
+
projectId: this.project.id,
|
|
132
|
+
agentId: agent.id,
|
|
133
|
+
aid: (0, aid_1.stringifyIdentity)({ projectId: this.project.id, agentId: agent.id }),
|
|
134
|
+
},
|
|
135
|
+
project: this.project,
|
|
136
|
+
}, {
|
|
137
|
+
inputs,
|
|
138
|
+
taskId,
|
|
139
|
+
});
|
|
140
|
+
return executor.execute();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.AIGNERuntime = AIGNERuntime;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.OnTaskCompletion = exports.Mustache = void 0;
|
|
21
|
+
exports.isUserInputParameter = isUserInputParameter;
|
|
22
|
+
var mustache_1 = require("./mustache/mustache");
|
|
23
|
+
Object.defineProperty(exports, "Mustache", { enumerable: true, get: function () { return __importDefault(mustache_1).default; } });
|
|
24
|
+
__exportStar(require("./mustache/directive"), exports);
|
|
25
|
+
var OnTaskCompletion;
|
|
26
|
+
(function (OnTaskCompletion) {
|
|
27
|
+
OnTaskCompletion["EXIT"] = "EXIT";
|
|
28
|
+
})(OnTaskCompletion || (exports.OnTaskCompletion = OnTaskCompletion = {}));
|
|
29
|
+
function isUserInputParameter(parameter) {
|
|
30
|
+
return parameter.type !== 'source';
|
|
31
|
+
}
|