test-wuying-agentbay-sdk 0.13.1-beta.20251224103203 → 0.13.1-beta.20251224113236
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +87 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +42 -1
- package/dist/index.d.ts +42 -1
- package/dist/index.mjs +64 -0
- package/dist/index.mjs.map +1 -1
- package/docs/api/common-features/basics/session.md +0 -2
- package/docs/examples/README.md +1 -0
- package/docs/examples/common-features/advanced/session-metrics/README.md +19 -0
- package/docs/examples/common-features/advanced/session-metrics/main.ts +33 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -17237,6 +17237,70 @@ var _Session = class _Session {
|
|
|
17237
17237
|
};
|
|
17238
17238
|
}
|
|
17239
17239
|
}
|
|
17240
|
+
/**
|
|
17241
|
+
* Get runtime metrics for this session via the MCP get_metrics tool.
|
|
17242
|
+
*
|
|
17243
|
+
* The underlying MCP tool returns a JSON string. This method parses it and
|
|
17244
|
+
* returns a structured result.
|
|
17245
|
+
*
|
|
17246
|
+
* @example
|
|
17247
|
+
* ```typescript
|
|
17248
|
+
* const agentBay = new AgentBay({ apiKey: "your_api_key" });
|
|
17249
|
+
* const create = await agentBay.create({ imageId: "linux_latest" });
|
|
17250
|
+
* if (create.success && create.session) {
|
|
17251
|
+
* const metrics = await create.session.getMetrics();
|
|
17252
|
+
* console.log(metrics.data);
|
|
17253
|
+
* await create.session.delete();
|
|
17254
|
+
* }
|
|
17255
|
+
* ```
|
|
17256
|
+
*/
|
|
17257
|
+
async getMetrics() {
|
|
17258
|
+
const toolResult = await this.callMcpTool("get_metrics", {});
|
|
17259
|
+
const requestId = toolResult.requestId || "";
|
|
17260
|
+
if (!toolResult.success) {
|
|
17261
|
+
return {
|
|
17262
|
+
requestId,
|
|
17263
|
+
success: false,
|
|
17264
|
+
data: void 0,
|
|
17265
|
+
raw: void 0,
|
|
17266
|
+
errorMessage: toolResult.errorMessage || ""
|
|
17267
|
+
};
|
|
17268
|
+
}
|
|
17269
|
+
try {
|
|
17270
|
+
const raw = typeof toolResult.data === "string" ? JSON.parse(toolResult.data) : toolResult.data;
|
|
17271
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
|
|
17272
|
+
throw new Error("get_metrics returned non-object JSON");
|
|
17273
|
+
}
|
|
17274
|
+
const metrics = {
|
|
17275
|
+
cpuCount: Number(_nullishCoalesce(raw.cpu_count, () => ( 0))),
|
|
17276
|
+
cpuUsedPct: Number(_nullishCoalesce(raw.cpu_used_pct, () => ( 0))),
|
|
17277
|
+
diskTotal: Number(_nullishCoalesce(raw.disk_total, () => ( 0))),
|
|
17278
|
+
diskUsed: Number(_nullishCoalesce(raw.disk_used, () => ( 0))),
|
|
17279
|
+
memTotal: Number(_nullishCoalesce(raw.mem_total, () => ( 0))),
|
|
17280
|
+
memUsed: Number(_nullishCoalesce(raw.mem_used, () => ( 0))),
|
|
17281
|
+
rxRateKBps: Number(_nullishCoalesce(raw.rx_rate_KBps, () => ( 0))),
|
|
17282
|
+
txRateKBps: Number(_nullishCoalesce(raw.tx_rate_KBps, () => ( 0))),
|
|
17283
|
+
rxUsedKB: Number(_nullishCoalesce(raw.rx_used_KB, () => ( 0))),
|
|
17284
|
+
txUsedKB: Number(_nullishCoalesce(raw.tx_used_KB, () => ( 0))),
|
|
17285
|
+
timestamp: String(_nullishCoalesce(raw.timestamp, () => ( "")))
|
|
17286
|
+
};
|
|
17287
|
+
return {
|
|
17288
|
+
requestId,
|
|
17289
|
+
success: true,
|
|
17290
|
+
data: metrics,
|
|
17291
|
+
raw,
|
|
17292
|
+
errorMessage: ""
|
|
17293
|
+
};
|
|
17294
|
+
} catch (err) {
|
|
17295
|
+
return {
|
|
17296
|
+
requestId,
|
|
17297
|
+
success: false,
|
|
17298
|
+
data: void 0,
|
|
17299
|
+
raw: void 0,
|
|
17300
|
+
errorMessage: `Failed to parse get_metrics response: ${_optionalChain([err, 'optionalAccess', _192 => _192.message]) || String(err)}`
|
|
17301
|
+
};
|
|
17302
|
+
}
|
|
17303
|
+
}
|
|
17240
17304
|
/**
|
|
17241
17305
|
* Asynchronously pause this session, putting it into a dormant state.
|
|
17242
17306
|
*
|
|
@@ -17682,8 +17746,8 @@ var _AgentBay = class _AgentBay {
|
|
|
17682
17746
|
async create(params) {
|
|
17683
17747
|
try {
|
|
17684
17748
|
const paramsCopy = this.deepCopyParams(params);
|
|
17685
|
-
logDebug(`default context syncs length: ${_optionalChain([paramsCopy, 'access',
|
|
17686
|
-
if (_optionalChain([paramsCopy, 'access',
|
|
17749
|
+
logDebug(`default context syncs length: ${_optionalChain([paramsCopy, 'access', _193 => _193.contextSync, 'optionalAccess', _194 => _194.length])}`);
|
|
17750
|
+
if (_optionalChain([paramsCopy, 'access', _195 => _195.extraConfigs, 'optionalAccess', _196 => _196.mobile, 'optionalAccess', _197 => _197.simulateConfig])) {
|
|
17687
17751
|
const mobileSimContextId = paramsCopy.extraConfigs.mobile.simulateConfig.simulatedContextId;
|
|
17688
17752
|
if (mobileSimContextId) {
|
|
17689
17753
|
const mobileSimContextSync = new ContextSync(
|
|
@@ -17703,7 +17767,7 @@ var _AgentBay = class _AgentBay {
|
|
|
17703
17767
|
if (paramsCopy.enableBrowserReplay === false) {
|
|
17704
17768
|
request.enableRecord = false;
|
|
17705
17769
|
}
|
|
17706
|
-
const framework = _optionalChain([paramsCopy, 'optionalAccess',
|
|
17770
|
+
const framework = _optionalChain([paramsCopy, 'optionalAccess', _198 => _198.framework]) || "";
|
|
17707
17771
|
const sdkStatsJson = `{"source":"sdk","sdk_language":"typescript","sdk_version":"${VERSION}","is_release":${IS_RELEASE},"framework":"${framework}"}`;
|
|
17708
17772
|
request.sdkStats = sdkStatsJson;
|
|
17709
17773
|
if (this.config.region_id) {
|
|
@@ -17760,7 +17824,7 @@ var _AgentBay = class _AgentBay {
|
|
|
17760
17824
|
}
|
|
17761
17825
|
if (paramsCopy.extraConfigs) {
|
|
17762
17826
|
request.extraConfigs = JSON.stringify(paramsCopy.extraConfigs);
|
|
17763
|
-
if (_optionalChain([paramsCopy, 'access',
|
|
17827
|
+
if (_optionalChain([paramsCopy, 'access', _199 => _199.extraConfigs, 'access', _200 => _200.mobile, 'optionalAccess', _201 => _201.simulateConfig, 'optionalAccess', _202 => _202.simulate])) {
|
|
17764
17828
|
mobileSimPath = paramsCopy.extraConfigs.mobile.simulateConfig.simulatePath;
|
|
17765
17829
|
if (!mobileSimPath) {
|
|
17766
17830
|
logInfo("mobile_sim_path is not set now, skip mobile simulate operation");
|
|
@@ -18003,9 +18067,9 @@ var _AgentBay = class _AgentBay {
|
|
|
18003
18067
|
}
|
|
18004
18068
|
const response2 = await this.client.listSession(request2);
|
|
18005
18069
|
const requestId2 = extractRequestId(response2) || "";
|
|
18006
|
-
if (!_optionalChain([response2, 'access',
|
|
18007
|
-
const code = _optionalChain([response2, 'access',
|
|
18008
|
-
const message = _optionalChain([response2, 'access',
|
|
18070
|
+
if (!_optionalChain([response2, 'access', _203 => _203.body, 'optionalAccess', _204 => _204.success])) {
|
|
18071
|
+
const code = _optionalChain([response2, 'access', _205 => _205.body, 'optionalAccess', _206 => _206.code]) || "Unknown";
|
|
18072
|
+
const message = _optionalChain([response2, 'access', _207 => _207.body, 'optionalAccess', _208 => _208.message]) || "Unknown error";
|
|
18009
18073
|
return {
|
|
18010
18074
|
requestId: requestId2,
|
|
18011
18075
|
success: false,
|
|
@@ -18047,9 +18111,9 @@ var _AgentBay = class _AgentBay {
|
|
|
18047
18111
|
const response = await this.client.listSession(request);
|
|
18048
18112
|
const requestId = extractRequestId(response) || "";
|
|
18049
18113
|
setRequestId(requestId);
|
|
18050
|
-
if (!_optionalChain([response, 'access',
|
|
18051
|
-
const code = _optionalChain([response, 'access',
|
|
18052
|
-
const message = _optionalChain([response, 'access',
|
|
18114
|
+
if (!_optionalChain([response, 'access', _209 => _209.body, 'optionalAccess', _210 => _210.success])) {
|
|
18115
|
+
const code = _optionalChain([response, 'access', _211 => _211.body, 'optionalAccess', _212 => _212.code]) || "Unknown";
|
|
18116
|
+
const message = _optionalChain([response, 'access', _213 => _213.body, 'optionalAccess', _214 => _214.message]) || "Unknown error";
|
|
18053
18117
|
logAPIResponseWithDetails(
|
|
18054
18118
|
"ListSession",
|
|
18055
18119
|
requestId,
|
|
@@ -18157,7 +18221,7 @@ var _AgentBay = class _AgentBay {
|
|
|
18157
18221
|
const requestId = extractRequestId(response) || "";
|
|
18158
18222
|
const body = response.body;
|
|
18159
18223
|
setRequestId(requestId);
|
|
18160
|
-
if (_optionalChain([body, 'optionalAccess',
|
|
18224
|
+
if (_optionalChain([body, 'optionalAccess', _215 => _215.success]) === false && body.code) {
|
|
18161
18225
|
logAPIResponseWithDetails(
|
|
18162
18226
|
"GetSession",
|
|
18163
18227
|
requestId,
|
|
@@ -18176,12 +18240,12 @@ var _AgentBay = class _AgentBay {
|
|
|
18176
18240
|
}
|
|
18177
18241
|
const result = {
|
|
18178
18242
|
requestId,
|
|
18179
|
-
httpStatusCode: _optionalChain([body, 'optionalAccess',
|
|
18180
|
-
code: _optionalChain([body, 'optionalAccess',
|
|
18181
|
-
success: _optionalChain([body, 'optionalAccess',
|
|
18243
|
+
httpStatusCode: _optionalChain([body, 'optionalAccess', _216 => _216.httpStatusCode]) || 0,
|
|
18244
|
+
code: _optionalChain([body, 'optionalAccess', _217 => _217.code]) || "",
|
|
18245
|
+
success: _optionalChain([body, 'optionalAccess', _218 => _218.success]) || false,
|
|
18182
18246
|
errorMessage: ""
|
|
18183
18247
|
};
|
|
18184
|
-
if (_optionalChain([body, 'optionalAccess',
|
|
18248
|
+
if (_optionalChain([body, 'optionalAccess', _219 => _219.data])) {
|
|
18185
18249
|
const contextsList = body.data.contexts || [];
|
|
18186
18250
|
const contexts = [];
|
|
18187
18251
|
if (Array.isArray(contextsList)) {
|
|
@@ -18221,7 +18285,7 @@ var _AgentBay = class _AgentBay {
|
|
|
18221
18285
|
return result;
|
|
18222
18286
|
} catch (error) {
|
|
18223
18287
|
const errorStr = String(error);
|
|
18224
|
-
const errorCode = _optionalChain([error, 'optionalAccess',
|
|
18288
|
+
const errorCode = _optionalChain([error, 'optionalAccess', _220 => _220.data, 'optionalAccess', _221 => _221.Code]) || _optionalChain([error, 'optionalAccess', _222 => _222.code]) || "";
|
|
18225
18289
|
if (errorCode === "InvalidMcpSession.NotFound" || errorStr.includes("NotFound")) {
|
|
18226
18290
|
logInfo(`Session not found: ${sessionId}`);
|
|
18227
18291
|
logDebug(`GetSession error details: ${errorStr}`);
|
|
@@ -18259,9 +18323,9 @@ var _AgentBay = class _AgentBay {
|
|
|
18259
18323
|
});
|
|
18260
18324
|
const response = await this.client.getSessionDetail(request);
|
|
18261
18325
|
const body = response.body;
|
|
18262
|
-
const requestId = extractRequestId(response) || _optionalChain([body, 'optionalAccess',
|
|
18326
|
+
const requestId = extractRequestId(response) || _optionalChain([body, 'optionalAccess', _223 => _223.requestId]) || "";
|
|
18263
18327
|
setRequestId(requestId);
|
|
18264
|
-
if (_optionalChain([body, 'optionalAccess',
|
|
18328
|
+
if (_optionalChain([body, 'optionalAccess', _224 => _224.success]) === false && body.code) {
|
|
18265
18329
|
logAPIResponseWithDetails(
|
|
18266
18330
|
"GetSessionDetail",
|
|
18267
18331
|
requestId,
|
|
@@ -18283,12 +18347,12 @@ var _AgentBay = class _AgentBay {
|
|
|
18283
18347
|
}
|
|
18284
18348
|
const result = {
|
|
18285
18349
|
requestId,
|
|
18286
|
-
httpStatusCode: _optionalChain([body, 'optionalAccess',
|
|
18287
|
-
code: _optionalChain([body, 'optionalAccess',
|
|
18288
|
-
success: _optionalChain([body, 'optionalAccess',
|
|
18350
|
+
httpStatusCode: _optionalChain([body, 'optionalAccess', _225 => _225.httpStatusCode]) || 0,
|
|
18351
|
+
code: _optionalChain([body, 'optionalAccess', _226 => _226.code]) || "",
|
|
18352
|
+
success: _optionalChain([body, 'optionalAccess', _227 => _227.success]) || false,
|
|
18289
18353
|
errorMessage: ""
|
|
18290
18354
|
};
|
|
18291
|
-
if (_optionalChain([body, 'optionalAccess',
|
|
18355
|
+
if (_optionalChain([body, 'optionalAccess', _228 => _228.data])) {
|
|
18292
18356
|
result.data = {
|
|
18293
18357
|
aliuid: body.data.aliuid || "",
|
|
18294
18358
|
apikeyId: body.data.apikeyId || "",
|
|
@@ -19275,7 +19339,7 @@ var _MobileSimulateService = class _MobileSimulateService {
|
|
|
19275
19339
|
if (!contextSync) {
|
|
19276
19340
|
throw new Error("contextSync is required for external context");
|
|
19277
19341
|
}
|
|
19278
|
-
if (_optionalChain([contextSync, 'access',
|
|
19342
|
+
if (_optionalChain([contextSync, 'access', _229 => _229.policy, 'optionalAccess', _230 => _230.bwList, 'optionalAccess', _231 => _231.whiteLists])) {
|
|
19279
19343
|
const exists = contextSync.policy.bwList.whiteLists.some(
|
|
19280
19344
|
(whiteList) => whiteList.path === MOBILE_INFO_SUB_PATH
|
|
19281
19345
|
);
|