Skip to content

Commit 5a27ed7

Browse files
authored
Merge pull request #3 from Serverless-Devs/feat-embedding
refactor: enhance resource management and API consistency
2 parents 29d92f3 + b5eaf74 commit 5a27ed7

49 files changed

Lines changed: 4001 additions & 4390 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/agent-runtime.ts

Lines changed: 19 additions & 14 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+
callback: (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+
callback: (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

@@ -232,8 +234,9 @@ async function deleteAgentRuntime(ar: AgentRuntime): Promise<void> {
232234
// Wait for deletion
233235
log('等待删除完成 / Waiting for deletion...');
234236
try {
235-
await ar.waitUntilReady({
236-
beforeCheck: (runtime) => log(` 当前状态 / Current status: ${runtime.status}`),
237+
await ar.waitUntilReadyOrFailed({
238+
callback: (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: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ async function createOrGetModelService(): Promise<ModelService> {
6767
}
6868

6969
// 等待就绪 / Wait for ready
70-
await ms.waitUntilReady({
71-
beforeCheck: (service: ModelService) => log(` 当前状态 / Current status: ${service.status}`),
70+
await ms.waitUntilReadyOrFailed({
71+
beforeCheck: (service: ModelService) =>
72+
log(` 当前状态 / Current status: ${service.status}`),
7273
});
7374

7475
if (ms.status !== Status.READY) {
@@ -95,7 +96,7 @@ async function updateModelService(ms: ModelService): Promise<void> {
9596
},
9697
});
9798

98-
await ms.waitUntilReady();
99+
await ms.waitUntilReadyOrFailed();
99100

100101
if (ms.status !== Status.READY) {
101102
throw new Error(`状态异常 / Unexpected status: ${ms.status}`);
@@ -124,7 +125,7 @@ async function listModelServices(): Promise<void> {
124125
async function invokeModelService(ms: ModelService): Promise<void> {
125126
log('调用模型服务进行推理 / Invoking model service for inference');
126127

127-
const result = await ms.completions({
128+
const result = await ms.completion({
128129
messages: [{ role: 'user', content: '你好,请介绍一下你自己' }],
129130
stream: true,
130131
});
@@ -196,8 +197,9 @@ async function createOrGetModelProxy(): Promise<ModelProxy> {
196197
}
197198

198199
// 等待就绪 / Wait for ready
199-
await mp.waitUntilReady({
200-
beforeCheck: (proxy: ModelProxy) => log(` 当前状态 / Current status: ${proxy.status}`),
200+
await mp.waitUntilReadyOrFailed({
201+
beforeCheck: (proxy: ModelProxy) =>
202+
log(` 当前状态 / Current status: ${proxy.status}`),
201203
});
202204

203205
if (mp.status !== Status.READY) {
@@ -226,7 +228,7 @@ async function updateModelProxy(mp: ModelProxy): Promise<void> {
226228
},
227229
});
228230

229-
await mp.waitUntilReady();
231+
await mp.waitUntilReadyOrFailed();
230232

231233
if (mp.status !== Status.READY) {
232234
throw new Error(`状态异常 / Unexpected status: ${mp.status}`);

0 commit comments

Comments
 (0)