Skip to content

Commit ed653c8

Browse files
committed
refactor: enhance resource management and API consistency
This commit refactors the resource management system by implementing a more consistent API client structure across modules. The changes include: - Updated import paths and module references for better consistency - Enhanced ResourceBase class with improved listAll functionality - Standardized API client methods and error handling - Improved type safety and code maintainability The changes affect AgentRuntime, ModelProxy, ModelService, Credential, Sandbox, and Toolset modules with consistent method signatures and return types. The waitUntilReadyOrFailed method has been renamed to waitUntilReadyOrFailed for better clarity and consistency. Additionally, dependencies have been updated and test coverage has been improved across modules. 更新资源管理和 API 一致性 此提交通过实现更一致的 API 客户端结构来重构资源管理系统。 更改包括: - 更新导入路径和模块引用以实现更好的一致性 - 使用改进的 listAll 功能增强 ResourceBase 类 - 标准化 API 客户端方法和错误处理 - 提高类型安全性和代码可维护性 这些更改影响 AgentRuntime、ModelProxy、ModelService、Credential、 Sandbox 和 Toolset 模块,具有统一的方法签名和返回类型。 waitUntilReadyOrFailed 方法已重命名为 waitUntilReadyOrFailed 以提高清晰度和一致性。 此外,依赖项已更新,各模块的测试覆盖率已提高。 Change-Id: I104aac42064fd96c867b9a13ab4140e5e182db3d Signed-off-by: OhYee <oyohyee@oyohyee.com>
1 parent 29d92f3 commit ed653c8

33 files changed

Lines changed: 2226 additions & 2227 deletions

examples/agent-runtime.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ async function createOrGetAgentRuntime(): Promise<AgentRuntime> {
8888
codeConfiguration: await codeFromFile(
8989
AgentRuntimeLanguage.NODEJS18,
9090
['node', 'index.js'],
91-
codePath,
91+
codePath
9292
),
9393
port: 9000,
9494
cpu: 2,
9595
memory: 4096,
96-
}
96+
},
9797
});
9898

9999
log(`创建成功 / Created successfully: ${ar.agentRuntimeId}`);
@@ -113,10 +113,10 @@ async function createOrGetAgentRuntime(): Promise<AgentRuntime> {
113113
ar.status === Status.DELETE_FAILED
114114
) {
115115
log(
116-
`已存在的 Agent Runtime 处于失败状态: ${ar.status}, 删除并重新创建 / Existing Agent Runtime is in failed state: ${ar.status}, deleting and recreating`,
116+
`已存在的 Agent Runtime 处于失败状态: ${ar.status}, 删除并重新创建 / Existing Agent Runtime is in failed state: ${ar.status}, deleting and recreating`
117117
);
118118
await ar.delete();
119-
119+
120120
// Wait for deletion to complete
121121
log('等待删除完成 / Waiting for deletion to complete...');
122122
let deleted = false;
@@ -134,7 +134,7 @@ async function createOrGetAgentRuntime(): Promise<AgentRuntime> {
134134
break;
135135
}
136136
}
137-
137+
138138
if (!deleted) {
139139
throw new Error('等待删除超时 / Deletion timeout');
140140
}
@@ -155,20 +155,21 @@ async function createOrGetAgentRuntime(): Promise<AgentRuntime> {
155155
codeConfiguration: await codeFromFile(
156156
AgentRuntimeLanguage.NODEJS18,
157157
['node', 'index.js'],
158-
codePath,
158+
codePath
159159
),
160160
port: 9000,
161161
cpu: 2,
162162
memory: 4096,
163-
}
163+
},
164164
});
165165
log(`创建成功 / Created successfully: ${ar.agentRuntimeId}`);
166166
}
167167

168168
// Wait for ready or failed
169169
log('等待就绪 / Waiting for ready...');
170170
await ar.waitUntilReadyOrFailed({
171-
beforeCheck: (runtime) => log(` 当前状态 / Current status: ${runtime.status}`),
171+
beforeCheck: (runtime) =>
172+
log(` 当前状态 / Current status: ${runtime.status}`),
172173
});
173174

174175
if (ar.status !== Status.READY) {
@@ -196,7 +197,8 @@ async function updateAgentRuntime(ar: AgentRuntime): Promise<void> {
196197
});
197198

198199
await ar.waitUntilReadyOrFailed({
199-
beforeCheck: (runtime) => log(` 当前状态 / Current status: ${runtime.status}`),
200+
beforeCheck: (runtime) =>
201+
log(` 当前状态 / Current status: ${runtime.status}`),
200202
});
201203

202204
if (ar.status !== Status.READY) {
@@ -213,10 +215,10 @@ async function updateAgentRuntime(ar: AgentRuntime): Promise<void> {
213215
async function listAgentRuntimes(): Promise<void> {
214216
log('枚举资源列表 / Listing resources');
215217

216-
const runtimes = await client.listAll();
218+
const runtimes = await AgentRuntime.listAll();
217219
log(
218220
`共有 ${runtimes.length} 个资源 / Total ${runtimes.length} resources:`,
219-
runtimes.map((r) => r.agentRuntimeName),
221+
runtimes.map((r) => r.agentRuntimeName)
220222
);
221223
}
222224

@@ -233,7 +235,8 @@ async function deleteAgentRuntime(ar: AgentRuntime): Promise<void> {
233235
log('等待删除完成 / Waiting for deletion...');
234236
try {
235237
await ar.waitUntilReady({
236-
beforeCheck: (runtime) => log(` 当前状态 / Current status: ${runtime.status}`),
238+
beforeCheck: (runtime) =>
239+
log(` 当前状态 / Current status: ${runtime.status}`),
237240
});
238241
} catch (error) {
239242
// Expected to fail when resource is deleted
@@ -245,7 +248,9 @@ async function deleteAgentRuntime(ar: AgentRuntime): Promise<void> {
245248
log('资源仍然存在 / Resource still exists');
246249
} catch (error) {
247250
if (error instanceof ResourceNotExistError) {
248-
log('得到资源不存在报错,删除成功 / Resource not found, deletion successful');
251+
log(
252+
'得到资源不存在报错,删除成功 / Resource not found, deletion successful'
253+
);
249254
} else {
250255
throw error;
251256
}

examples/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ async function listModelServices(): Promise<void> {
124124
async function invokeModelService(ms: ModelService): Promise<void> {
125125
log('调用模型服务进行推理 / Invoking model service for inference');
126126

127-
const result = await ms.completions({
127+
const result = await ms.completion({
128128
messages: [{ role: 'user', content: '你好,请介绍一下你自己' }],
129129
stream: true,
130130
});

examples/toolset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function toolsetExample() {
2626
// Example 1: Using Baidu Search Tool (OpenAPI)
2727
logger.info('==== OpenAPI ToolSet Example ====');
2828
try {
29-
const baiduToolset = await client.getToolSet({
29+
const baiduToolset = await client.get({
3030
name: 'web-search-baidu-8wox', // 替换为您的百度搜索工具名称
3131
});
3232

@@ -53,7 +53,7 @@ async function toolsetExample() {
5353
// Example 2: Using MCP Time Tool
5454
logger.info('\n==== MCP ToolSet Example ====');
5555
try {
56-
const mcpToolset = await client.getToolSet({
56+
const mcpToolset = await client.get({
5757
name: 'start-mcp-time-ggda', // 替换为您的 MCP 时间工具名称
5858
});
5959

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
},
5656
"dependencies": {
5757
"@ai-sdk/openai": "^3.0.0",
58+
"@ai-sdk/openai-compatible": "^2.0.13",
5859
"@alicloud/agentrun20250910": "^5.0.0",
5960
"@alicloud/devs20230714": "^2.4.1",
6061
"@alicloud/openapi-client": "^0.4.12",

scripts/codegen.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import * as fs from "fs";
1414
import * as path from "path";
1515
import * as yaml from "yaml";
16+
import { fileURLToPath } from 'node:url';
17+
18+
const __filename = fileURLToPath(import.meta.url);
19+
const __dirname = path.dirname(__filename);
1620

1721
const projectRoot = path.resolve(__dirname, "..");
1822
const configsDir = path.join(projectRoot, "codegen", "configs");

src/agent-runtime/client.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* This module provides the client API for Agent Runtime.
66
*/
77

8-
import { Config } from "../utils/config";
8+
import { Config } from '../utils/config';
99

10-
import { AgentRuntimeControlAPI } from "./api/control";
11-
import { AgentRuntimeDataAPI, InvokeArgs } from "./api/data";
12-
import { AgentRuntimeEndpoint } from "./endpoint";
10+
import { AgentRuntimeControlAPI } from './api/control';
11+
import { AgentRuntimeDataAPI, InvokeArgs } from './api/data';
12+
import { AgentRuntimeEndpoint } from './endpoint';
1313
import {
1414
AgentRuntimeCreateInput,
1515
AgentRuntimeEndpointCreateInput,
@@ -19,8 +19,8 @@ import {
1919
AgentRuntimeUpdateInput,
2020
AgentRuntimeVersion,
2121
AgentRuntimeVersionListInput,
22-
} from "./model";
23-
import { AgentRuntime } from "./runtime";
22+
} from './model';
23+
import { AgentRuntime } from './runtime';
2424

2525
/**
2626
* Agent Runtime Client
@@ -51,7 +51,10 @@ export class AgentRuntimeClient {
5151
/**
5252
* Delete an Agent Runtime
5353
*/
54-
delete = async (params: { id: string; config?: Config }): Promise<AgentRuntime> => {
54+
delete = async (params: {
55+
id: string;
56+
config?: Config;
57+
}): Promise<AgentRuntime> => {
5558
const { id, config } = params;
5659
return AgentRuntime.delete({ id, config: config ?? this.config });
5760
};
@@ -71,7 +74,10 @@ export class AgentRuntimeClient {
7174
/**
7275
* Get an Agent Runtime
7376
*/
74-
get = async (params: { id: string; config?: Config }): Promise<AgentRuntime> => {
77+
get = async (params: {
78+
id: string;
79+
config?: Config;
80+
}): Promise<AgentRuntime> => {
7581
const { id, config } = params;
7682
return AgentRuntime.get({ id, config: config ?? this.config });
7783
};
@@ -84,23 +90,23 @@ export class AgentRuntimeClient {
8490
config?: Config;
8591
}): Promise<AgentRuntime[]> => {
8692
const { input, config } = params ?? {};
87-
return AgentRuntime.list(input, config ?? this.config);
93+
return AgentRuntime.list({ input, config: config ?? this.config });
8894
};
8995

90-
/**
91-
* List all Agent Runtimes (with pagination)
92-
*/
93-
listAll = async (params?: {
94-
options?: {
95-
agentRuntimeName?: string;
96-
tags?: string;
97-
searchMode?: string;
98-
};
99-
config?: Config;
100-
}): Promise<AgentRuntime[]> => {
101-
const { options, config } = params ?? {};
102-
return AgentRuntime.listAll(options, config ?? this.config);
103-
};
96+
// /**
97+
// * List all Agent Runtimes (with pagination)
98+
// */
99+
// listAll = async (params?: {
100+
// options?: {
101+
// agentRuntimeName?: string;
102+
// tags?: string;
103+
// searchMode?: string;
104+
// };
105+
// config?: Config;
106+
// }): Promise<AgentRuntime[]> => {
107+
// const { options, config } = params ?? {};
108+
// return AgentRuntime.listAll(options, config ?? this.config);
109+
// };
104110

105111
/**
106112
* Create an endpoint for an Agent Runtime
@@ -223,11 +229,11 @@ export class AgentRuntimeClient {
223229
params: {
224230
agentRuntimeName: string;
225231
agentRuntimeEndpointName?: string;
226-
} & InvokeArgs,
232+
} & InvokeArgs
227233
) => {
228234
const {
229235
agentRuntimeName,
230-
agentRuntimeEndpointName = "Default",
236+
agentRuntimeEndpointName = 'Default',
231237
messages,
232238
stream,
233239
config,
@@ -240,7 +246,7 @@ export class AgentRuntimeClient {
240246
const dataApi = new AgentRuntimeDataAPI(
241247
agentRuntimeName,
242248
agentRuntimeEndpointName,
243-
cfg,
249+
cfg
244250
);
245251

246252
return dataApi.invokeOpenai({

0 commit comments

Comments
 (0)