Skip to content

Commit ec3911d

Browse files
committed
refactor(toolset): enhance list and listAll methods with flexible parameter support
Update the ToolSet class to provide more flexible parameter handling for list and listAll methods, allowing both legacy and new parameter formats. Also improve API logging with correct request ID header and add example usage in the example file. The changes include: - Refactoring static list and listAll methods to accept parameters in multiple formats - Using listAllResourcesFunction for pagination logic - Updating API logging to use correct x-acs-request-id header - Adding example usage in toolset.ts example file 重构(toolset): 使用灵活的参数支持增强 list 和 listAll 方法 更新 ToolSet 类以提供更灵活的参数处理,支持 list 和 listAll 方法的多种参数格式。同时改进 API 日志记录以使用正确的请求 ID 头部,并在示例文件中添加示例用法。 变更包括: - 重构静态 list 和 listAll 方法以接受多种格式的参数 - 使用 listAllResourcesFunction 进行分页逻辑 - 更新 API 日志以使用正确的 x-acs-request-id 头部 - 在 toolset.ts 示例文件中添加示例用法 Change-Id: I5eb3398fd0120c3674a8c23d81551fdd7c326924 Signed-off-by: OhYee <oyohyee@oyohyee.com>
1 parent bcc63f7 commit ec3911d

File tree

2 files changed

+65
-39
lines changed

2 files changed

+65
-39
lines changed

src/toolset/api/control.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ export class ToolControlAPI {
134134

135135
const response = await client.listToolsetsWithOptions(input, headers || {}, runtime);
136136

137-
logger.debug(`API listToolsets called, Request ID: ${response.body?.requestId}`);
137+
logger.debug(
138+
`API listToolsets called, Request ID: ${response?.headers?.['x-acs-request-id']}`
139+
);
138140

139141
if (!response.body) {
140142
throw new Error('Empty response body');

src/toolset/toolset.ts

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { Config } from '../utils/config';
99
import { logger } from '../utils/log';
10-
import { updateObjectProperties } from '../utils/resource';
10+
import { listAllResourcesFunction, updateObjectProperties } from '../utils/resource';
1111

1212
import {
1313
ToolSetCreateInput,
@@ -50,6 +50,8 @@ export class ToolSet implements ToolSetData {
5050
return new ToolSetClient();
5151
}
5252

53+
uniqIdCallback = () => this.name;
54+
5355
/**
5456
* Create a new ToolSet
5557
*/
@@ -77,48 +79,70 @@ export class ToolSet implements ToolSetData {
7779
/**
7880
* List ToolSets
7981
*/
80-
static async list(input?: ToolSetListInput, config?: Config): Promise<ToolSet[]> {
81-
return await ToolSet.getClient().list({ input, config });
82-
}
8382

84-
/**
85-
* List all ToolSets with pagination
86-
*/
87-
static async listAll(
88-
options?: { prefix?: string; labels?: Record<string, string> },
89-
config?: Config
90-
): Promise<ToolSet[]> {
91-
const toolsets: ToolSet[] = [];
92-
const pageSize = 50;
93-
94-
// eslint-disable-next-line no-constant-condition
95-
while (true) {
96-
const result = await ToolSet.list(
97-
{
98-
prefix: options?.prefix,
99-
labels: options?.labels,
100-
pageSize,
101-
},
102-
config
103-
);
104-
105-
toolsets.push(...result);
83+
static list: /**
84+
* @deprecated
85+
*/
86+
| ((input?: ToolSetListInput, config?: Config) => Promise<ToolSet[]>)
87+
/**
88+
* 枚举 ToolSet 列表 / List ToolSet list
89+
*/
90+
| ((params?: { input?: ToolSetListInput; config?: Config }) => Promise<ToolSet[]>) = async (
91+
...args: any
92+
): Promise<ToolSet[]> => {
93+
let input: ToolSetListInput | undefined;
94+
let config: Config | undefined;
95+
96+
if (args.length >= 1 && 'input' in args[0]) {
97+
input = args[0].input;
98+
} else {
99+
input = args[0];
100+
}
106101

107-
if (result.length < pageSize) {
108-
break;
109-
}
102+
if (args.length >= 1 && 'config' in args[0]) {
103+
config = args[0].config;
104+
} else if (args.length > 1 && args[1] instanceof Config) {
105+
config = args[1];
110106
}
111107

112-
// Deduplicate
113-
const seen = new Set<string>();
114-
return toolsets.filter(t => {
115-
if (!t.uid || seen.has(t.uid)) {
116-
return false;
117-
}
118-
seen.add(t.uid);
119-
return true;
108+
return await this.getClient().list({
109+
input: {
110+
...input,
111+
} as ToolSetListInput,
112+
config,
120113
});
121-
}
114+
};
115+
116+
static listAll: /**
117+
* @deprecated
118+
*/
119+
| ((
120+
options?: { prefix?: string; labels?: Record<string, string> },
121+
config?: Config
122+
) => Promise<ToolSet[]>)
123+
/**
124+
* 枚举 ToolSet 列表 / List ToolSet list
125+
*/
126+
| ((params?: { input?: ToolSetListInput; config?: Config }) => Promise<ToolSet[]>) = async (
127+
...args: any
128+
) => {
129+
let input: ToolSetListInput | undefined;
130+
let config: Config | undefined;
131+
132+
if (args.length >= 1 && 'input' in args[0]) {
133+
input = args[0].input;
134+
} else {
135+
input = args[0];
136+
}
137+
138+
if (args.length >= 1 && 'config' in args[0]) {
139+
config = args[0].config;
140+
} else if (args.length > 1 && args[1] instanceof Config) {
141+
config = args[1];
142+
}
143+
144+
return await listAllResourcesFunction(this.list as any)({ ...input, config });
145+
};
122146

123147
/**
124148
* Update a ToolSet by Name

0 commit comments

Comments
 (0)