@cloudbase/manager-node 4.3.2 → 4.3.3
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/agent/index.js +70 -0
- package/lib/agent/type.js +2 -0
- package/lib/cloudrun/index.js +4 -36
- package/lib/environment.js +5 -0
- package/lib/index.js +3 -0
- package/lib/utils/index.js +43 -2
- package/package.json +1 -1
- package/types/agent/index.d.ts +20 -0
- package/types/agent/type.d.ts +22 -0
- package/types/cloudrun/index.d.ts +3 -0
- package/types/environment.d.ts +3 -0
- package/types/index.d.ts +2 -0
- package/types/utils/index.d.ts +24 -2
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.AgentService = void 0;
|
|
13
|
+
const cloudrun_1 = require("../cloudrun");
|
|
14
|
+
const utils_1 = require("../utils");
|
|
15
|
+
const path_1 = __importDefault(require("path"));
|
|
16
|
+
/**
|
|
17
|
+
* Agent 管理类
|
|
18
|
+
*/
|
|
19
|
+
class AgentService {
|
|
20
|
+
constructor(environment) {
|
|
21
|
+
this.environment = environment;
|
|
22
|
+
this.tcbService = new utils_1.CloudService(environment.cloudBaseContext, 'tcb', '2018-06-08');
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 创建函数型 Agent
|
|
26
|
+
* @param {string} [cwd=process.cwd()] 工作目录
|
|
27
|
+
* @param {ICreateFunctionAgentParams} agentInfo Agent 信息
|
|
28
|
+
* @returns {Promise<{ BotId: string; RequestId: string }>} Agent 创建结果
|
|
29
|
+
*/
|
|
30
|
+
async createFunctionAgent(cwd = process.cwd(), agentInfo) {
|
|
31
|
+
const { BotId } = agentInfo;
|
|
32
|
+
const targetPath = path_1.default.join(cwd);
|
|
33
|
+
const envConfig = this.environment.lazyEnvironmentConfig;
|
|
34
|
+
/**
|
|
35
|
+
* 判断 params.IbotId 的数据格式是否为 ibot-xxx-xxx 的格式
|
|
36
|
+
*/
|
|
37
|
+
if (!/^ibot-[a-z0-9_]+-[a-z0-9_]+$/.test(BotId)) {
|
|
38
|
+
throw new Error('BotId格式应为"ibot-xxx-xxx",其中 xxx 仅支持小写字母、数字或下划线');
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 获取函数型云托管服务的名称:ibot-xxx-xxx 中的 ibot-xxx
|
|
42
|
+
*/
|
|
43
|
+
const serverName = BotId.split('-').slice(0, 2).join('-');
|
|
44
|
+
/**
|
|
45
|
+
* 获取部署包上传信息
|
|
46
|
+
*/
|
|
47
|
+
const { UploadUrl: uploadUrl, UploadHeaders: uploadHeaders, PackageName: packageName, PackageVersion: packageVersion } = await this.tcbService.request('DescribeCloudBaseBuildService', {
|
|
48
|
+
EnvId: envConfig.EnvId,
|
|
49
|
+
ServiceName: serverName
|
|
50
|
+
});
|
|
51
|
+
/**
|
|
52
|
+
* 上传部署包
|
|
53
|
+
*/
|
|
54
|
+
const zipFile = await (0, cloudrun_1.codeToZip)(targetPath, { installDependency: true });
|
|
55
|
+
await (0, utils_1.upload)({
|
|
56
|
+
url: uploadUrl,
|
|
57
|
+
file: zipFile,
|
|
58
|
+
headers: (uploadHeaders || []).reduce((map, item) => {
|
|
59
|
+
map[item.Key] = item.Value;
|
|
60
|
+
return map;
|
|
61
|
+
}, {}) || {},
|
|
62
|
+
method: 'PUT'
|
|
63
|
+
});
|
|
64
|
+
return this.tcbService.request('CreateBot', Object.assign(Object.assign({}, agentInfo), { EnvId: envConfig.EnvId, PackageName: packageName, PackageVersion: packageVersion }));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.AgentService = AgentService;
|
|
68
|
+
__decorate([
|
|
69
|
+
(0, utils_1.preLazy)()
|
|
70
|
+
], AgentService.prototype, "createFunctionAgent", null);
|
package/lib/cloudrun/index.js
CHANGED
|
@@ -10,6 +10,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.CloudRunService = void 0;
|
|
13
|
+
exports.codeToZip = codeToZip;
|
|
13
14
|
const archiver_1 = __importDefault(require("archiver"));
|
|
14
15
|
const fs_extra_1 = require("fs-extra");
|
|
15
16
|
const path_1 = __importDefault(require("path"));
|
|
@@ -179,16 +180,14 @@ class CloudRunService {
|
|
|
179
180
|
* 上传部署包
|
|
180
181
|
*/
|
|
181
182
|
const zipFile = await codeToZip(targetPath, { installDependency: true });
|
|
182
|
-
await upload({
|
|
183
|
-
|
|
183
|
+
await (0, utils_1.upload)({
|
|
184
|
+
url: uploadUrl,
|
|
184
185
|
file: zipFile,
|
|
185
|
-
filename: 'file',
|
|
186
186
|
headers: (uploadHeaders || []).reduce((map, item) => {
|
|
187
187
|
map[item.Key] = item.Value;
|
|
188
188
|
return map;
|
|
189
189
|
}, {}) || {},
|
|
190
|
-
method: 'PUT'
|
|
191
|
-
send: (file) => file
|
|
190
|
+
method: 'PUT'
|
|
192
191
|
});
|
|
193
192
|
/**
|
|
194
193
|
* 执行部署
|
|
@@ -336,34 +335,3 @@ async function codeToZip(cwd, options) {
|
|
|
336
335
|
await archive.finalize();
|
|
337
336
|
return bufferPromise;
|
|
338
337
|
}
|
|
339
|
-
function upload({ action, file, method = 'POST', headers = {}, withCredentials = false }) {
|
|
340
|
-
return (0, utils_1.fetchStream)(action, {
|
|
341
|
-
method,
|
|
342
|
-
headers,
|
|
343
|
-
body: file,
|
|
344
|
-
credentials: withCredentials ? 'include' : 'same-origin'
|
|
345
|
-
})
|
|
346
|
-
.then(async (response) => {
|
|
347
|
-
const text = await response.text();
|
|
348
|
-
const dataMeta = {
|
|
349
|
-
data: null,
|
|
350
|
-
context: {
|
|
351
|
-
response,
|
|
352
|
-
file
|
|
353
|
-
}
|
|
354
|
-
};
|
|
355
|
-
try {
|
|
356
|
-
dataMeta.data = JSON.parse(text);
|
|
357
|
-
}
|
|
358
|
-
catch (e) {
|
|
359
|
-
// If parsing fails, keep data as null
|
|
360
|
-
}
|
|
361
|
-
if (!response.ok) {
|
|
362
|
-
throw new Error(`${response.status} ${response.statusText} HTTP ERROR`);
|
|
363
|
-
}
|
|
364
|
-
return dataMeta;
|
|
365
|
-
})
|
|
366
|
-
.catch(error => {
|
|
367
|
-
throw error; // 或者可以在这里进行错误转换
|
|
368
|
-
});
|
|
369
|
-
}
|
package/lib/environment.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.Environment = void 0;
|
|
|
4
4
|
const database_1 = require("./database");
|
|
5
5
|
const function_1 = require("./function");
|
|
6
6
|
const cloudrun_1 = require("./cloudrun");
|
|
7
|
+
const agent_1 = require("./agent");
|
|
7
8
|
const storage_1 = require("./storage");
|
|
8
9
|
const env_1 = require("./env");
|
|
9
10
|
const common_1 = require("./common");
|
|
@@ -24,6 +25,7 @@ class Environment {
|
|
|
24
25
|
// 拉取当前环境 的环境信息 todo
|
|
25
26
|
this.functionService = new function_1.FunctionService(this);
|
|
26
27
|
this.cloudRunService = new cloudrun_1.CloudRunService(this);
|
|
28
|
+
this.agentService = new agent_1.AgentService(this);
|
|
27
29
|
this.databaseService = new database_1.DatabaseService(this);
|
|
28
30
|
this.storageService = new storage_1.StorageService(this);
|
|
29
31
|
this.envService = new env_1.EnvService(this);
|
|
@@ -67,6 +69,9 @@ class Environment {
|
|
|
67
69
|
getCloudRunService() {
|
|
68
70
|
return this.cloudRunService;
|
|
69
71
|
}
|
|
72
|
+
getAgentService() {
|
|
73
|
+
return this.agentService;
|
|
74
|
+
}
|
|
70
75
|
getEnvService() {
|
|
71
76
|
return this.envService;
|
|
72
77
|
}
|
package/lib/index.js
CHANGED
|
@@ -49,6 +49,9 @@ class CloudBase {
|
|
|
49
49
|
get cloudrun() {
|
|
50
50
|
return this.currentEnvironment().getCloudRunService();
|
|
51
51
|
}
|
|
52
|
+
get agent() {
|
|
53
|
+
return this.currentEnvironment().getAgentService();
|
|
54
|
+
}
|
|
52
55
|
get storage() {
|
|
53
56
|
return this.currentEnvironment().getStorageService();
|
|
54
57
|
}
|
package/lib/utils/index.js
CHANGED
|
@@ -20,6 +20,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
20
20
|
exports.guid6 = void 0;
|
|
21
21
|
exports.compressToZip = compressToZip;
|
|
22
22
|
exports.downloadAndExtractRemoteZip = downloadAndExtractRemoteZip;
|
|
23
|
+
exports.upload = upload;
|
|
23
24
|
exports.getRuntime = getRuntime;
|
|
24
25
|
exports.getEnvVar = getEnvVar;
|
|
25
26
|
exports.rsaEncrypt = rsaEncrypt;
|
|
@@ -69,8 +70,8 @@ async function compressToZip(option) {
|
|
|
69
70
|
});
|
|
70
71
|
}
|
|
71
72
|
/**
|
|
72
|
-
*
|
|
73
|
-
* @param {string} downloadUrl
|
|
73
|
+
* 下载并解压压缩包到指定目录
|
|
74
|
+
* @param {string} downloadUrl 压缩包下载地址
|
|
74
75
|
* @param {string} targetPath 目标路径
|
|
75
76
|
* @returns {Promise<void>}
|
|
76
77
|
* @throws 当下载失败或解压失败时抛出错误
|
|
@@ -126,6 +127,46 @@ async function downloadAndExtractRemoteZip(downloadUrl, targetPath) {
|
|
|
126
127
|
}
|
|
127
128
|
});
|
|
128
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* 将文件上传到指定 URL 地址
|
|
132
|
+
* @param {Object} options - 上传配置项
|
|
133
|
+
* @param {string} options.url - 目标上传地址
|
|
134
|
+
* @param {File|Blob|Buffer} options.file - 要上传的文件对象
|
|
135
|
+
* @param {string} [options.method='POST'] - HTTP 方法,默认为 POST
|
|
136
|
+
* @param {Object} [options.headers={}] - 自定义请求头
|
|
137
|
+
* @param {boolean} [options.withCredentials=false] - 是否携带跨域凭据
|
|
138
|
+
*/
|
|
139
|
+
function upload({ url, file, method = 'POST', headers = {}, withCredentials = false }) {
|
|
140
|
+
return (0, http_request_1.fetchStream)(url, {
|
|
141
|
+
method,
|
|
142
|
+
headers,
|
|
143
|
+
body: file,
|
|
144
|
+
credentials: withCredentials ? 'include' : 'same-origin'
|
|
145
|
+
})
|
|
146
|
+
.then(async (response) => {
|
|
147
|
+
const text = await response.text();
|
|
148
|
+
const dataMeta = {
|
|
149
|
+
data: null,
|
|
150
|
+
context: {
|
|
151
|
+
response,
|
|
152
|
+
file
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
try {
|
|
156
|
+
dataMeta.data = JSON.parse(text);
|
|
157
|
+
}
|
|
158
|
+
catch (e) {
|
|
159
|
+
// If parsing fails, keep data as null
|
|
160
|
+
}
|
|
161
|
+
if (!response.ok) {
|
|
162
|
+
throw new Error(`${response.status} ${response.statusText} HTTP ERROR`);
|
|
163
|
+
}
|
|
164
|
+
return dataMeta;
|
|
165
|
+
})
|
|
166
|
+
.catch(error => {
|
|
167
|
+
throw error; // 或者可以在这里进行错误转换
|
|
168
|
+
});
|
|
169
|
+
}
|
|
129
170
|
function getRuntime() {
|
|
130
171
|
return process.env[constant_1.ENV_NAME.ENV_RUNENV];
|
|
131
172
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Environment } from '../environment';
|
|
2
|
+
import { ICreateFunctionAgentParams } from './type';
|
|
3
|
+
/**
|
|
4
|
+
* Agent 管理类
|
|
5
|
+
*/
|
|
6
|
+
export declare class AgentService {
|
|
7
|
+
private environment;
|
|
8
|
+
private tcbService;
|
|
9
|
+
constructor(environment: Environment);
|
|
10
|
+
/**
|
|
11
|
+
* 创建函数型 Agent
|
|
12
|
+
* @param {string} [cwd=process.cwd()] 工作目录
|
|
13
|
+
* @param {ICreateFunctionAgentParams} agentInfo Agent 信息
|
|
14
|
+
* @returns {Promise<{ BotId: string; RequestId: string }>} Agent 创建结果
|
|
15
|
+
*/
|
|
16
|
+
createFunctionAgent(cwd: string, agentInfo: ICreateFunctionAgentParams): Promise<{
|
|
17
|
+
BotId: string;
|
|
18
|
+
RequestId: string;
|
|
19
|
+
}>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface ICreateFunctionAgentParams {
|
|
2
|
+
/**
|
|
3
|
+
* 智能体名称
|
|
4
|
+
* @example "hello"
|
|
5
|
+
*/
|
|
6
|
+
Name: string;
|
|
7
|
+
/**
|
|
8
|
+
* 智能体ID
|
|
9
|
+
* @example "ibot-aaa-bbb"
|
|
10
|
+
*/
|
|
11
|
+
BotId: string;
|
|
12
|
+
/**
|
|
13
|
+
* 智能体简介
|
|
14
|
+
* @example "这是一个智能体"
|
|
15
|
+
*/
|
|
16
|
+
Introduction?: string;
|
|
17
|
+
/**
|
|
18
|
+
* 智能体头像
|
|
19
|
+
* @example "https://picsum.photos/200"
|
|
20
|
+
*/
|
|
21
|
+
Avatar?: string;
|
|
22
|
+
}
|
package/types/environment.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DatabaseService } from './database';
|
|
2
2
|
import { FunctionService } from './function';
|
|
3
3
|
import { CloudRunService } from './cloudrun';
|
|
4
|
+
import { AgentService } from './agent';
|
|
4
5
|
import { StorageService } from './storage';
|
|
5
6
|
import { EnvService } from './env';
|
|
6
7
|
import { CommonService } from './common';
|
|
@@ -19,6 +20,7 @@ export declare class Environment {
|
|
|
19
20
|
private envType?;
|
|
20
21
|
private functionService;
|
|
21
22
|
private cloudRunService;
|
|
23
|
+
private agentService;
|
|
22
24
|
private databaseService;
|
|
23
25
|
private storageService;
|
|
24
26
|
private envService;
|
|
@@ -35,6 +37,7 @@ export declare class Environment {
|
|
|
35
37
|
getDatabaseService(): DatabaseService;
|
|
36
38
|
getFunctionService(): FunctionService;
|
|
37
39
|
getCloudRunService(): CloudRunService;
|
|
40
|
+
getAgentService(): AgentService;
|
|
38
41
|
getEnvService(): EnvService;
|
|
39
42
|
getHostingService(): HostingService;
|
|
40
43
|
getThirdService(): ThirdService;
|
package/types/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { EnvService } from './env';
|
|
2
2
|
import { FunctionService } from './function';
|
|
3
3
|
import { CloudRunService } from './cloudrun';
|
|
4
|
+
import { AgentService } from './agent';
|
|
4
5
|
import { StorageService } from './storage';
|
|
5
6
|
import { DatabaseService } from './database';
|
|
6
7
|
import { CommonService } from './common';
|
|
@@ -39,6 +40,7 @@ declare class CloudBase {
|
|
|
39
40
|
currentEnvironment(): Environment;
|
|
40
41
|
get functions(): FunctionService;
|
|
41
42
|
get cloudrun(): CloudRunService;
|
|
43
|
+
get agent(): AgentService;
|
|
42
44
|
get storage(): StorageService;
|
|
43
45
|
get database(): DatabaseService;
|
|
44
46
|
get hosting(): HostingService;
|
package/types/utils/index.d.ts
CHANGED
|
@@ -14,13 +14,35 @@ interface IZipOption {
|
|
|
14
14
|
}
|
|
15
15
|
export declare function compressToZip(option: IZipOption): Promise<unknown>;
|
|
16
16
|
/**
|
|
17
|
-
*
|
|
18
|
-
* @param {string} downloadUrl
|
|
17
|
+
* 下载并解压压缩包到指定目录
|
|
18
|
+
* @param {string} downloadUrl 压缩包下载地址
|
|
19
19
|
* @param {string} targetPath 目标路径
|
|
20
20
|
* @returns {Promise<void>}
|
|
21
21
|
* @throws 当下载失败或解压失败时抛出错误
|
|
22
22
|
*/
|
|
23
23
|
export declare function downloadAndExtractRemoteZip(downloadUrl: string, targetPath: string): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* 将文件上传到指定 URL 地址
|
|
26
|
+
* @param {Object} options - 上传配置项
|
|
27
|
+
* @param {string} options.url - 目标上传地址
|
|
28
|
+
* @param {File|Blob|Buffer} options.file - 要上传的文件对象
|
|
29
|
+
* @param {string} [options.method='POST'] - HTTP 方法,默认为 POST
|
|
30
|
+
* @param {Object} [options.headers={}] - 自定义请求头
|
|
31
|
+
* @param {boolean} [options.withCredentials=false] - 是否携带跨域凭据
|
|
32
|
+
*/
|
|
33
|
+
export declare function upload({ url, file, method, headers, withCredentials }: {
|
|
34
|
+
url: string;
|
|
35
|
+
file: File | Blob | Buffer;
|
|
36
|
+
method?: string;
|
|
37
|
+
headers?: Record<string, any>;
|
|
38
|
+
withCredentials?: boolean;
|
|
39
|
+
}): Promise<{
|
|
40
|
+
data: any;
|
|
41
|
+
context: {
|
|
42
|
+
response: import("node-fetch").Response;
|
|
43
|
+
file: Buffer | File | Blob;
|
|
44
|
+
};
|
|
45
|
+
}>;
|
|
24
46
|
export declare function getRuntime(): string;
|
|
25
47
|
export declare function getEnvVar(envName: string): string;
|
|
26
48
|
export declare function rsaEncrypt(data: string): string;
|