@ohos-perf/cli 0.20.0 → 0.22.0

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.
Files changed (2) hide show
  1. package/dist/index.js +182 -0
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "fs
5
5
  import { join } from "path";
6
6
  import {
7
7
  discoverCases,
8
+ describeDecision,
8
9
  loadCases,
9
10
  OhosPerfError as OhosPerfError2,
10
11
  parseScenario
@@ -371,6 +372,82 @@ stopped ${stopped}
371
372
  `, exitCode: 1 };
372
373
  }
373
374
  }
375
+ async function runAgentCommand(deps2, req, opts) {
376
+ if (req.task.trim() === "") {
377
+ return {
378
+ stdout: "",
379
+ stderr: "agent: the task must not be empty\n",
380
+ exitCode: 1
381
+ };
382
+ }
383
+ try {
384
+ const result = await deps2.agent(req);
385
+ if (opts.json) return { stdout: json(result), stderr: "", exitCode: 0 };
386
+ const { transcript, device } = result;
387
+ const outcome = transcript.status === "completed" ? "completed (the screen confirmed it)" : transcript.status === "unverified" ? "unverified (the screen did not confirm it)" : transcript.status === "refused" ? "refused (an irreversible action was stopped)" : `exhausted (the step cap, ${req.maxSteps}, was reached)`;
388
+ const lines = transcript.steps.map((step) => {
389
+ const did = step.action === void 0 ? step.status : `${describeStep(step)} \u2014 ${step.status}`;
390
+ const moved = step.navigated ? "moved" : "no change";
391
+ const detail = step.detail === void 0 ? "" : `
392
+ ${step.detail}`;
393
+ return ` ${String(step.index + 1).padStart(2)}. ${did} (${moved}): ${step.reason}${detail}`;
394
+ });
395
+ return {
396
+ stdout: `agent ${transcript.app} on ${device.id} (${device.kind})
397
+ task ${transcript.task}
398
+ result ${outcome}
399
+ walk ${transcript.steps.length} steps, ${transcript.statesReached} states reached
400
+ ` + lines.map((line) => `${line}
401
+ `).join(""),
402
+ stderr: "",
403
+ exitCode: transcript.status === "completed" ? 0 : 1
404
+ };
405
+ } catch (err) {
406
+ return { stdout: "", stderr: `${failureLine(err)}
407
+ `, exitCode: 1 };
408
+ }
409
+ }
410
+ function describeStep(step) {
411
+ if (step.action === void 0) return step.status;
412
+ return describeDecision({ action: step.action, reason: step.reason });
413
+ }
414
+ async function runSymbolRecovery(deps2, req, opts) {
415
+ if (!existsSync(req.library)) {
416
+ return { stdout: "", stderr: `symbols: no such library: ${req.library}
417
+ `, exitCode: 1 };
418
+ }
419
+ try {
420
+ const result = await deps2.symbols(req);
421
+ if (opts.json) return { stdout: json(result), stderr: "", exitCode: 0 };
422
+ const lines = [
423
+ `library ${result.library}`,
424
+ `result ${result.recovered.length} recovered, ${result.unresolved.length} unresolved`
425
+ ];
426
+ for (const entry of result.recovered) {
427
+ lines.push(` ${entry.address} ${entry.name} (0x${entry.start.toString(16)}..0x${entry.end.toString(16)})`);
428
+ }
429
+ for (const entry of result.unresolved) {
430
+ lines.push(` ${entry.address} ${entry.reason}`);
431
+ }
432
+ if (result.naming !== void 0) {
433
+ const naming = result.naming;
434
+ if (naming.accuracy === void 0) {
435
+ lines.push("naming no nameable function in this file to hold out");
436
+ } else {
437
+ const percent = (naming.accuracy * 100).toFixed(1);
438
+ lines.push(`naming ${naming.correct} of ${naming.heldOut} named correctly (${percent}%)`);
439
+ for (const entry of naming.cases.filter((item) => !item.correct)) {
440
+ lines.push(` ${entry.truth} answered ${JSON.stringify(entry.answer)}`);
441
+ }
442
+ }
443
+ }
444
+ return { stdout: `${lines.join("\n")}
445
+ `, stderr: "", exitCode: 0 };
446
+ } catch (err) {
447
+ return { stdout: "", stderr: `${failureLine(err)}
448
+ `, exitCode: 1 };
449
+ }
450
+ }
374
451
  async function runRender(deps2, req, opts) {
375
452
  try {
376
453
  const result = await deps2.render(req);
@@ -799,6 +876,11 @@ import {
799
876
  hapSection,
800
877
  analyzeBinaries,
801
878
  extractBinaries,
879
+ httpAgentModel,
880
+ readAgentConfig,
881
+ runAgent,
882
+ recoverSymbols,
883
+ measureNaming,
802
884
  readSceneMetadata,
803
885
  readUiTree,
804
886
  resolveAppIdentity,
@@ -820,6 +902,17 @@ function packageVersion(path = manifestPath) {
820
902
  return "0.0.0";
821
903
  }
822
904
  }
905
+ var HEX_DUMP_LIMIT = 512;
906
+ function hexDump(bytes) {
907
+ if (bytes.length === 0) return "(the symbol points outside .text, so no bytes are available)";
908
+ const lines = [];
909
+ for (let at = 0; at + 4 <= Math.min(bytes.length, HEX_DUMP_LIMIT); at += 4) {
910
+ const word = new DataView(bytes.buffer, bytes.byteOffset + at, 4).getUint32(0, true);
911
+ lines.push(`+${at.toString(16).padStart(4, "0")} ${word.toString(16).padStart(8, "0")}`);
912
+ }
913
+ if (bytes.length > HEX_DUMP_LIMIT) lines.push(`(${bytes.length - HEX_DUMP_LIMIT} more bytes)`);
914
+ return lines.join("\n");
915
+ }
823
916
  var OFFLINE_PROBE = () => ({ command: "", stdout: "", stderr: "", code: 0 });
824
917
  function offlineState(id, collector) {
825
918
  return {
@@ -1023,6 +1116,21 @@ function realDeps(version = packageVersion(), overrides = {}) {
1023
1116
  const collectDevice = overrides.collectDevice ?? ((target) => connect(target === void 0 ? {} : { target }));
1024
1117
  const gestureDevice = overrides.gestureDevice ?? ((target) => connect(target === void 0 ? {} : { target }));
1025
1118
  const exploreDevice = overrides.exploreDevice ?? ((target) => connect(target === void 0 ? {} : { target }));
1119
+ const agentDevice = overrides.agentDevice ?? exploreDevice;
1120
+ const agentModel = overrides.agentModel ?? ((name) => {
1121
+ const config = readAgentConfig(process.env);
1122
+ if (config === void 0) {
1123
+ throw new OhosPerfError3(
1124
+ "no model endpoint is configured; set OHOS_PERF_AGENT_BASE_URL and OHOS_PERF_AGENT_API_KEY (or GLM_BASE_URL and GLM_API_KEY), and optionally OHOS_PERF_AGENT_MODEL (or GLM_MODEL)",
1125
+ { code: "AGENT_BAD_CONFIG" }
1126
+ );
1127
+ }
1128
+ return httpAgentModel({
1129
+ ...config,
1130
+ ...name === void 0 ? {} : { model: name },
1131
+ fetch: (url, init) => fetch(url, init)
1132
+ });
1133
+ });
1026
1134
  const doctor = {
1027
1135
  resolveHdc,
1028
1136
  async resolveStreamer() {
@@ -1439,6 +1547,43 @@ function realDeps(version = packageVersion(), overrides = {}) {
1439
1547
  ])]
1440
1548
  };
1441
1549
  },
1550
+ async agent(req) {
1551
+ const model = agentModel(req.model);
1552
+ const device = await agentDevice(req.target);
1553
+ const transcript = await runAgent(device, model, {
1554
+ task: req.task,
1555
+ app: req.app,
1556
+ maxSteps: req.maxSteps,
1557
+ ...req.allowIrreversible === true ? { allowIrreversible: true } : {}
1558
+ });
1559
+ return { transcript, device: { id: device.id, kind: device.kind } };
1560
+ },
1561
+ async symbols(req) {
1562
+ const bytes = readFileSync2(req.library);
1563
+ const library = basename(req.library);
1564
+ let result;
1565
+ try {
1566
+ result = recoverSymbols(bytes, req.addresses);
1567
+ } catch (err) {
1568
+ throw new OhosPerfError3(`${library}: ${err.message}`, { cause: err });
1569
+ }
1570
+ if (req.measure !== true) return { library, ...result };
1571
+ const model = agentModel(req.model);
1572
+ const naming = await measureNaming(bytes, async (question) => {
1573
+ const counts = Object.entries(question.counts).filter(([name, value]) => name !== "total" && value > 0).map(([name, value]) => `${name}=${String(value)}`).join(" ");
1574
+ const request = {
1575
+ task: `Name the function at 0x${question.start.toString(16)} in ${library} (${question.size} bytes; instruction mix: ${counts || "none"}).`,
1576
+ app: library,
1577
+ step: 0,
1578
+ maxSteps: 1,
1579
+ screen: hexDump(question.body),
1580
+ history: []
1581
+ };
1582
+ const decision = await model.decide(request);
1583
+ return "done" in decision ? decision.reason : decision.reason;
1584
+ });
1585
+ return { library, ...result, naming };
1586
+ },
1442
1587
  async run(req) {
1443
1588
  const device = await connect(req.target === void 0 ? {} : { target: req.target });
1444
1589
  const runner = new ScenarioRunner(device, {
@@ -1903,6 +2048,43 @@ function buildProgram(deps2, io, opts = {}) {
1903
2048
  )
1904
2049
  );
1905
2050
  });
2051
+ program2.command("agent").description("drive an app toward a task with a model, and verify the screen reached").argument("<app>", "bundle name to drive, e.g. com.example.app").argument("<task>", "what the model should accomplish, in your words").option("--max-steps <n>", "stop after this many model turns", "50").option("--model <name>", "model name to send; defaults to the configured one").option("--allow-irreversible", "let the guard's refusals through (payment, ordering, ...)").option("--target <id>", "drive this hdc target instead of the first connected one").option("--json", "print the transcript as JSON").action(async (app, task, opts2) => {
2052
+ let maxSteps;
2053
+ try {
2054
+ maxSteps = parseMaxSteps(opts2.maxSteps);
2055
+ } catch (err) {
2056
+ io.emit({ stdout: "", stderr: `${failureLine(err)}\\n`, exitCode: 1 });
2057
+ return;
2058
+ }
2059
+ io.emit(
2060
+ await runAgentCommand(
2061
+ deps2,
2062
+ {
2063
+ app,
2064
+ task,
2065
+ maxSteps,
2066
+ ...opts2.model === void 0 ? {} : { model: opts2.model },
2067
+ ...opts2.allowIrreversible === true ? { allowIrreversible: true } : {},
2068
+ ...opts2.target === void 0 ? {} : { target: opts2.target }
2069
+ },
2070
+ { json: opts2.json }
2071
+ )
2072
+ );
2073
+ });
2074
+ program2.command("symbols").description("name the addresses a capture could not, from the libraries it ran").command("recover").description("name unresolved addresses from a library's own symbol table").argument("<library>", "shared object to read, e.g. libs/arm64/libapp.so").argument("[addresses...]", "libfoo.so+0xoffset references, as a capture writes them").option("--measure", "ask the model to name held-out functions and report its accuracy").option("--model <name>", "model name to send when --measure is used").option("--json", "print the recovery as JSON").action(async (library, addresses, opts2) => {
2075
+ io.emit(
2076
+ await runSymbolRecovery(
2077
+ deps2,
2078
+ {
2079
+ library,
2080
+ addresses,
2081
+ ...opts2.measure === true ? { measure: true } : {},
2082
+ ...opts2.model === void 0 ? {} : { model: opts2.model }
2083
+ },
2084
+ { json: opts2.json }
2085
+ )
2086
+ );
2087
+ });
1906
2088
  program2.command("tools").description("list the named drill-down queries an agent can call").option("--json", "print the query definitions as JSON").action((opts2) => {
1907
2089
  io.emit(runTools(deps2, opts2));
1908
2090
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ohos-perf/cli",
3
- "version": "0.20.0",
3
+ "version": "0.22.0",
4
4
  "description": "Command-line interface for HarmonyOS performance analysis: doctor, caps, collect, analyze, query, tools, compare, report, gesture, emulator and selector-driven scenarios",
5
5
  "keywords": [
6
6
  "harmonyos",
@@ -36,7 +36,7 @@
36
36
  "access": "public"
37
37
  },
38
38
  "dependencies": {
39
- "@ohos-perf/core": "0.20.0",
39
+ "@ohos-perf/core": "0.22.0",
40
40
  "commander": "^12.1.0"
41
41
  },
42
42
  "scripts": {