-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrol.ts
More file actions
287 lines (245 loc) · 7.88 KB
/
control.ts
File metadata and controls
287 lines (245 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* Tool Control API
*
* Tool 管控链路 API
*
* This file is auto generated by the code generation script.
* Do not modify this file manually.
* Use the `make codegen` command to regenerate.
*
* 当前文件为自动生成的控制 API 客户端代码。请勿手动修改此文件。
* 使用 `make codegen` 命令重新生成。
*/
import * as $Devs from '@alicloud/devs20230714';
import * as $OpenApi from '@alicloud/openapi-client';
import * as $Util from '@alicloud/tea-util';
import { Config } from '../../utils/config';
// Handle ESM/CJS interop for Client class
const $DevsClient =
// @ts-expect-error - ESM interop: default.default exists when imported as ESM namespace
$Devs.default?.default ?? $Devs.default ?? $Devs;
import { ClientError, HTTPError, ServerError } from '../../utils/exception';
import { logger } from '../../utils/log';
/**
* Tool Control API
*
* Tool 管控链路 API
*/
export class ToolControlAPI {
private config?: Config;
/**
* Initialize API client
* 初始化 API 客户端
*
* @param config - Global configuration object / 全局配置对象
*/
constructor(config?: Config) {
this.config = config;
}
/**
* Get DevS client
* 获取 DevS 客户端
*/
protected getDevsClient(config?: Config): InstanceType<typeof $DevsClient> {
const cfg = Config.withConfigs(this.config, config);
// Use devs endpoint
let endpoint = cfg.devsEndpoint;
// Remove protocol prefix for SDK
if (endpoint.startsWith('http://') || endpoint.startsWith('https://')) {
endpoint = endpoint.split('://')[1];
}
const openApiConfig = new $OpenApi.Config({
accessKeyId: cfg.accessKeyId,
accessKeySecret: cfg.accessKeySecret,
securityToken: cfg.securityToken || undefined,
regionId: cfg.regionId,
endpoint: endpoint,
connectTimeout: cfg.timeout,
});
return new $DevsClient(openApiConfig);
}
/**
* Get toolset
* 获取工具
*
* @param name - Tool name / Tool 名称
* @param headers - Request headers / 请求头
* @param config - Configuration / 配置
* @returns Toolset object / ToolSet 对象
* @throws AgentRuntimeError - Throws on call failure / 调用失败时抛出
* @throws ClientError - Client error / 客户端错误
* @throws ServerError - Server error / 服务器错误
*/
getToolset = async (params: {
name: string;
headers?: Record<string, string>;
config?: Config;
}): Promise<$Devs.Toolset> => {
const { name, headers, config } = params;
try {
const client = this.getDevsClient(config);
const runtime = new $Util.RuntimeOptions({});
const response = await client.getToolsetWithOptions(name, headers || {}, runtime);
logger.debug(`API getToolset called, Request ID: ${response.body?.requestId}`);
if (!response.body) {
throw new Error('Empty response body');
}
return response.body;
} catch (error) {
if (error instanceof HTTPError) {
throw error.toResourceError('ToolSet', name);
}
this.handleError(error, name);
}
};
/**
* List toolsets
* 枚举 ToolSets
*
* @param input - List configuration / 枚举的配置
* @param headers - Request headers / 请求头
* @param config - Configuration / 配置
* @returns ToolSet list / ToolSet 列表
* @throws AgentRuntimeError - Throws on call failure / 调用失败时抛出
* @throws ClientError - Client error / 客户端错误
* @throws ServerError - Server error / 服务器错误
*/
listToolsets = async (params: {
input: $Devs.ListToolsetsRequest;
headers?: Record<string, string>;
config?: Config;
}): Promise<$Devs.ListToolsetsResponseBody> => {
const { input, headers, config } = params;
try {
const client = this.getDevsClient(config);
const runtime = new $Util.RuntimeOptions({});
const response = await client.listToolsetsWithOptions(input, headers || {}, runtime);
logger.debug(
`API listToolsets called, Request ID: ${response?.headers?.['x-acs-request-id']}`
);
if (!response.body) {
throw new Error('Empty response body');
}
return response.body;
} catch (error) {
this.handleError(error);
}
};
/**
* Create toolset
* 创建工具集
*
* @param input - Toolset input / ToolSet 输入
* @param headers - Request headers / 请求头
* @param config - Configuration / 配置
* @returns Toolset object / ToolSet 对象
*/
createToolset = async (params: {
input: $Devs.Toolset;
headers?: Record<string, string>;
config?: Config;
}): Promise<$Devs.Toolset> => {
const { input, headers, config } = params;
try {
const client = this.getDevsClient(config);
const runtime = new $Util.RuntimeOptions({});
const response = await client.createToolsetWithOptions(
new $Devs.CreateToolsetRequest({ body: input }),
headers || {},
runtime
);
logger.debug(`API createToolset called, Request ID: ${response.body?.requestId}`);
if (!response.body) {
throw new Error('Empty response body');
}
return response.body;
} catch (error) {
this.handleError(error, input?.name);
}
};
/**
* Update toolset
* 更新工具集
*
* @param name - Toolset name / ToolSet 名称
* @param input - Toolset input / ToolSet 输入
* @param headers - Request headers / 请求头
* @param config - Configuration / 配置
* @returns Toolset object / ToolSet 对象
*/
updateToolset = async (params: {
name: string;
input: $Devs.Toolset;
headers?: Record<string, string>;
config?: Config;
}): Promise<$Devs.Toolset> => {
const { name, input, headers, config } = params;
try {
const client = this.getDevsClient(config);
const runtime = new $Util.RuntimeOptions({});
const response = await client.updateToolsetWithOptions(
name,
new $Devs.UpdateToolsetRequest({ body: input }),
headers || {},
runtime
);
logger.debug(`API updateToolset called, Request ID: ${response.body?.requestId}`);
if (!response.body) {
throw new Error('Empty response body');
}
return response.body;
} catch (error) {
this.handleError(error, name);
}
};
/**
* Delete toolset
* 删除工具集
*
* @param name - Toolset name / ToolSet 名称
* @param headers - Request headers / 请求头
* @param config - Configuration / 配置
* @returns Toolset object / ToolSet 对象
*/
deleteToolset = async (params: {
name: string;
headers?: Record<string, string>;
config?: Config;
}): Promise<$Devs.Toolset> => {
const { name, headers, config } = params;
try {
const client = this.getDevsClient(config);
const runtime = new $Util.RuntimeOptions({});
const response = await client.deleteToolsetWithOptions(name, headers || {}, runtime);
logger.debug(`API deleteToolset called, Request ID: ${response.body?.requestId}`);
if (!response.body) {
throw new Error('Empty response body');
}
return response.body;
} catch (error) {
this.handleError(error, name);
}
};
/**
* Handle API errors
* 处理 API 错误
*/
private handleError(error: unknown, _resourceName?: string): never {
if (error && typeof error === 'object' && 'statusCode' in error) {
const e = error as {
statusCode: number;
message: string;
data?: { requestId?: string };
};
const statusCode = e.statusCode;
const message = e.message || 'Unknown error';
const requestId = e.data?.requestId;
if (statusCode >= 400 && statusCode < 500) {
throw new ClientError(statusCode, message, { requestId });
} else if (statusCode >= 500) {
throw new ServerError(statusCode, message, { requestId });
}
}
throw error;
}
}