Skip to content

Commit 5e20938

Browse files
committed
refactor(model): update client method calls and improve pagination logic
update client method calls to use object parameters instead of individual arguments for create, delete, get, and update methods in both ModelProxy and ModelService classes. replace listAllResources with manual pagination using listPage for better performance and deduplication. update stream text result types to use ToolSet instead of CoreTool records. 改进客户端方法调用,将参数从单独参数改为对象参数,并改善分页逻辑 将 ModelProxy 和 ModelService 类中的 create、delete、get 和 update 方法的客户端调用从单独参数改为对象参数。用 listPage 手动分页替换 listAllResources 以获得更好的性能和去重功能。更新流文本结果类型以使用 ToolSet 而不是 CoreTool 记录。 BREAKING CHANGE: client method signatures changed from individual parameters to object parameters 重大变更:客户端方法签名从单独参数改为对象参数 Change-Id: I6090ef264a7d41cddfb59b9b167f251c9f794642 Signed-off-by: OhYee <[email protected]>
1 parent f23e952 commit 5e20938

File tree

4 files changed

+83
-53
lines changed

4 files changed

+83
-53
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ pnpm-lock.yaml
4949

5050
.coverage
5151

52-
bun.lock
52+
bun.lock
53+
package-lock.json

src/index.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,3 @@ export type {
146146
} from './toolset';
147147
export { ToolSetSchemaType } from './toolset';
148148

149-
// Server
150-
export { AgentRunServer } from './server';
151-
export type {
152-
AgentRequest,
153-
AgentResult,
154-
Message,
155-
ServerConfig,
156-
} from './server';
157-
export { MessageRole, EventType } from './server';
158-
159-
// Integration
160-
export * from './integration';

src/model/model-proxy.ts

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class ModelProxy
8181
config?: Config;
8282
}): Promise<ModelProxy> {
8383
const { input, config } = params;
84-
return await this.getClient().create(input, config);
84+
return await this.getClient().create({ input, config });
8585
}
8686

8787
/**
@@ -95,11 +95,11 @@ export class ModelProxy
9595
config?: Config;
9696
}): Promise<ModelProxy> {
9797
const { name, config } = params;
98-
return await this.getClient().delete(
98+
return await this.getClient().delete({
9999
name,
100-
BackendType.PROXY,
100+
backendType: BackendType.PROXY,
101101
config
102-
);
102+
});
103103
}
104104

105105
/**
@@ -114,7 +114,7 @@ export class ModelProxy
114114
config?: Config;
115115
}): Promise<ModelProxy> {
116116
const { name, input, config } = params;
117-
return await this.getClient().update(name, input, config);
117+
return await this.getClient().update({ name, input, config });
118118
}
119119

120120
/**
@@ -128,11 +128,11 @@ export class ModelProxy
128128
config?: Config;
129129
}): Promise<ModelProxy> {
130130
const { name, config } = params;
131-
return await this.getClient().get(
131+
return await this.getClient().get({
132132
name,
133-
BackendType.PROXY,
133+
backendType: BackendType.PROXY,
134134
config
135-
);
135+
});
136136
}
137137

138138
/**
@@ -148,13 +148,14 @@ export class ModelProxy
148148
config?: Config,
149149
kwargs?: Partial<ModelProxyListInput>
150150
): Promise<ModelProxy[]> {
151-
return await this.getClient().list(
152-
{
151+
return await this.getClient().list({
152+
input: {
153+
modelProxyName: undefined, // 标识这是 ModelProxyListInput
153154
...kwargs,
154155
...pageInput,
155156
} as ModelProxyListInput,
156157
config
157-
);
158+
});
158159
}
159160

160161
/**
@@ -169,14 +170,34 @@ export class ModelProxy
169170
status?: Status;
170171
config?: Config;
171172
}): Promise<ModelProxy[]> {
172-
return await this.listAllResources(
173-
(m: ModelProxy) => m.modelProxyId || '',
174-
options?.config,
175-
{
176-
proxyMode: options?.proxyMode,
177-
status: options?.status,
173+
const allResults: ModelProxy[] = [];
174+
let page = 1;
175+
const pageSize = 50;
176+
while (true) {
177+
const pageResults = await this.listPage(
178+
{ pageNumber: page, pageSize },
179+
options?.config,
180+
{
181+
proxyMode: options?.proxyMode,
182+
status: options?.status,
183+
}
184+
);
185+
page += 1;
186+
allResults.push(...pageResults);
187+
if (pageResults.length < pageSize) break;
188+
}
189+
190+
// 去重
191+
const resultSet = new Set<string>();
192+
const results: ModelProxy[] = [];
193+
for (const item of allResults) {
194+
const uniqId = item.modelProxyId || '';
195+
if (!resultSet.has(uniqId)) {
196+
resultSet.add(uniqId);
197+
results.push(item);
178198
}
179-
);
199+
}
200+
return results;
180201
}
181202

182203
/**
@@ -307,8 +328,8 @@ export class ModelProxy
307328
config?: Config;
308329
[key: string]: any;
309330
}): Promise<
310-
| import('ai').StreamTextResult<Record<string, import('ai').CoreTool>, never>
311-
| import('ai').GenerateTextResult<Record<string, import('ai').CoreTool>, never>
331+
| import('ai').StreamTextResult<import('ai').ToolSet, any>
332+
| import('ai').GenerateTextResult<import('ai').ToolSet, any>
312333
> => {
313334
const { messages, model, stream = false, config, ...kwargs } = params;
314335
const info = await this.modelInfo({ config });

src/model/model-service.ts

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class ModelService
7474
config?: Config;
7575
}): Promise<ModelService> {
7676
const { input, config } = params;
77-
return await this.getClient().create(input, config);
77+
return await this.getClient().create({ input, config });
7878
}
7979

8080
/**
@@ -88,11 +88,11 @@ export class ModelService
8888
config?: Config;
8989
}): Promise<ModelService> {
9090
const { name, config } = params;
91-
return await this.getClient().delete(
91+
return await this.getClient().delete({
9292
name,
93-
BackendType.SERVICE,
93+
backendType: BackendType.SERVICE,
9494
config
95-
);
95+
});
9696
}
9797

9898
/**
@@ -107,7 +107,7 @@ export class ModelService
107107
config?: Config;
108108
}): Promise<ModelService> {
109109
const { name, input, config } = params;
110-
return await this.getClient().update(name, input, config);
110+
return await this.getClient().update({ name, input, config });
111111
}
112112

113113
/**
@@ -121,11 +121,11 @@ export class ModelService
121121
config?: Config;
122122
}): Promise<ModelService> {
123123
const { name, config } = params;
124-
return await this.getClient().get(
124+
return await this.getClient().get({
125125
name,
126-
BackendType.SERVICE,
126+
backendType: BackendType.SERVICE,
127127
config
128-
);
128+
});
129129
}
130130

131131
/**
@@ -141,13 +141,13 @@ export class ModelService
141141
config?: Config,
142142
kwargs?: Partial<ModelServiceListInput>
143143
): Promise<ModelService[]> {
144-
return await this.getClient().list(
145-
{
144+
return await this.getClient().list({
145+
input: {
146146
...kwargs,
147147
...pageInput,
148148
} as ModelServiceListInput,
149149
config
150-
);
150+
});
151151
}
152152

153153
/**
@@ -162,14 +162,34 @@ export class ModelService
162162
provider?: string;
163163
config?: Config;
164164
}): Promise<ModelService[]> {
165-
return await this.listAllResources(
166-
(m: ModelService) => m.modelServiceId || '',
167-
options?.config,
168-
{
169-
modelType: options?.modelType,
170-
provider: options?.provider,
165+
const allResults: ModelService[] = [];
166+
let page = 1;
167+
const pageSize = 50;
168+
while (true) {
169+
const pageResults = await this.listPage(
170+
{ pageNumber: page, pageSize },
171+
options?.config,
172+
{
173+
modelType: options?.modelType,
174+
provider: options?.provider,
175+
}
176+
);
177+
page += 1;
178+
allResults.push(...pageResults);
179+
if (pageResults.length < pageSize) break;
180+
}
181+
182+
// 去重
183+
const resultSet = new Set<string>();
184+
const results: ModelService[] = [];
185+
for (const item of allResults) {
186+
const uniqId = item.modelServiceId || '';
187+
if (!resultSet.has(uniqId)) {
188+
resultSet.add(uniqId);
189+
results.push(item);
171190
}
172-
);
191+
}
192+
return results;
173193
}
174194

175195
/**
@@ -304,8 +324,8 @@ export class ModelService
304324
config?: Config;
305325
[key: string]: any;
306326
}): Promise<
307-
| import('ai').StreamTextResult<Record<string, import('ai').CoreTool>, never>
308-
| import('ai').GenerateTextResult<Record<string, import('ai').CoreTool>, never>
327+
| import('ai').StreamTextResult<import('ai').ToolSet, any>
328+
| import('ai').GenerateTextResult<import('ai').ToolSet, any>
309329
> => {
310330
const { messages, model, stream = false, config, ...kwargs } = params;
311331
const info = await this.modelInfo({ config });

0 commit comments

Comments
 (0)