Skip to content

Commit adf6b08

Browse files
authored
refactor: consolidate duration parsing and formatting (#105988)
* refactor: consolidate duration handling * docs: leave release notes to release workflow * fix: preserve build timing precision * ci: align plugin sdk surface budgets
1 parent 8a1201a commit adf6b08

43 files changed

Lines changed: 295 additions & 390 deletions

Some content is hidden

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

‎extensions/discord/src/monitor/provider.deploy-errors.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ function readNonNegativeInteger(value: unknown): number | undefined {
116116
return parseStrictNonNegativeInteger(value);
117117
}
118118

119-
function formatDurationMs(ms: number): string {
120-
return formatDurationSeconds(ms, { decimals: ms >= 1000 ? 1 : 0 });
121-
}
122-
123119
function isAbortLikeError(err: unknown): boolean {
124120
if (!err || typeof err !== "object") {
125121
return false;
@@ -176,10 +172,14 @@ export function formatDiscordDeployErrorMessage(err: unknown): string {
176172
}
177173
const timing: string[] = [];
178174
if (timeoutMs !== undefined) {
179-
timing.push(`timeout=${formatDurationMs(timeoutMs)}`);
175+
timing.push(
176+
`timeout=${formatDurationSeconds(timeoutMs, { decimals: timeoutMs >= 1000 ? 1 : 0 })}`,
177+
);
180178
}
181179
if (requestMs !== undefined) {
182-
timing.push(`observed=${formatDurationMs(requestMs)}`);
180+
timing.push(
181+
`observed=${formatDurationSeconds(requestMs, { decimals: requestMs >= 1000 ? 1 : 0 })}`,
182+
);
183183
}
184184
const timingText = timing.length > 0 ? ` (${timing.join(", ")})` : "";
185185
if (timeoutMs !== undefined && requestMs !== undefined && requestMs >= timeoutMs) {

‎extensions/google-meet/npm-shrinkwrap.json‎

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎extensions/google-meet/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"type": "module",
1010
"dependencies": {
1111
"commander": "15.0.0",
12+
"pretty-ms": "9.3.0",
1213
"typebox": "1.3.3"
1314
},
1415
"devDependencies": {

‎extensions/google-meet/src/cli.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
clampTimerTimeoutMs,
1212
parseStrictPositiveInteger,
1313
} from "openclaw/plugin-sdk/number-runtime";
14+
import prettyMilliseconds from "pretty-ms";
1415
import {
1516
buildGoogleMeetCalendarDayWindow,
1617
findGoogleMeetCalendarEvent,
@@ -344,13 +345,11 @@ function formatDuration(value: number | undefined): string {
344345
if (value === undefined) {
345346
return "n/a";
346347
}
347-
const totalSeconds = Math.round(value / 1000);
348-
const hours = Math.floor(totalSeconds / 3600);
349-
const minutes = Math.floor((totalSeconds % 3600) / 60);
350-
const seconds = totalSeconds % 60;
351-
return hours > 0
352-
? `${hours}h ${minutes.toString().padStart(2, "0")}m`
353-
: `${minutes}m ${seconds.toString().padStart(2, "0")}s`;
348+
return prettyMilliseconds(Math.max(0, Math.round(value / 1000) * 1000), {
349+
millisecondsDecimalDigits: 0,
350+
secondsDecimalDigits: 0,
351+
unitCount: 2,
352+
});
354353
}
355354

356355
function writeDoctorStatus(status: Awaited<ReturnType<GoogleMeetRuntime["status"]>>): void {

‎extensions/phone-control/index.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ describe("phone-control plugin", () => {
321321
}
322322

323323
expect(writeConfigFile).toHaveBeenCalledTimes(1);
324+
expect(text).toContain("armed for 30s");
324325
expect(nodes.allowCommands).toEqual([...WRITE_COMMANDS]);
325326
expect(nodes.denyCommands).toStrictEqual(["computer.act"]);
326327
expect(text).toContain("sms.send");

‎extensions/phone-control/index.ts‎

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Phone Control plugin entrypoint registers its OpenClaw integration.
22
import { randomUUID } from "node:crypto";
3+
import milliseconds from "ms";
34
import {
45
asDateTimestampMs,
56
resolveExpiresAtMsFromDurationMs,
@@ -10,6 +11,7 @@ import {
1011
normalizeStringEntries,
1112
sortUniqueStrings,
1213
} from "openclaw/plugin-sdk/string-coerce-runtime";
14+
import prettyMilliseconds from "pretty-ms";
1315
import {
1416
definePluginEntry,
1517
type OpenClawPluginApi,
@@ -94,38 +96,20 @@ function formatGroupList(): string {
9496

9597
function parseDurationMs(input: string | undefined): number | null {
9698
const raw = normalizeOptionalLowercaseString(input);
97-
if (!raw) {
99+
if (!raw || !/^\d+(?:\.\d+)?(?:ms|s|m|h|d)$/.test(raw)) {
98100
return null;
99101
}
100-
const m = raw.match(/^(\d+)(s|m|h|d)$/);
101-
if (!m) {
102-
return null;
103-
}
104-
const n = Number.parseInt(m[1] ?? "", 10);
105-
if (!Number.isFinite(n) || n <= 0) {
106-
return null;
107-
}
108-
const unit = m[2];
109-
const mult = unit === "s" ? 1000 : unit === "m" ? 60_000 : unit === "h" ? 3_600_000 : 86_400_000;
110-
const durationMs = n * mult;
111-
return Number.isSafeInteger(durationMs) ? durationMs : null;
102+
const durationMs = milliseconds(raw as Parameters<typeof milliseconds>[0]);
103+
return Number.isSafeInteger(durationMs) && durationMs > 0 ? durationMs : null;
112104
}
113105

114106
function formatDuration(ms: number): string {
115-
const s = Math.max(0, Math.floor(ms / 1000));
116-
if (s < 60) {
117-
return `${s}s`;
118-
}
119-
const m = Math.floor(s / 60);
120-
if (m < 60) {
121-
return `${m}m`;
122-
}
123-
const h = Math.floor(m / 60);
124-
if (h < 48) {
125-
return `${h}h`;
126-
}
127-
const d = Math.floor(h / 24);
128-
return `${d}d`;
107+
const roundedMs = ms < 1000 ? Math.round(ms) : Math.round(ms / 1000) * 1000;
108+
return prettyMilliseconds(Math.max(0, roundedMs), {
109+
compact: true,
110+
hideYear: true,
111+
secondsDecimalDigits: 0,
112+
});
129113
}
130114

131115
function openArmStateStore(api: OpenClawPluginApi) {

‎extensions/qa-lab/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@copilotkit/aimock": "1.35.0",
99
"@modelcontextprotocol/sdk": "1.29.0",
1010
"playwright-core": "1.61.1",
11+
"pretty-ms": "9.3.0",
1112
"semver": "7.8.5",
1213
"yaml": "2.9.0",
1314
"zod": "4.4.3"

‎extensions/qa-lab/src/character-eval.ts‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from "node:fs/promises";
33
import path from "node:path";
44
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
55
import { normalizeStringEntries, uniqueStrings } from "openclaw/plugin-sdk/string-coerce-runtime";
6+
import prettyMilliseconds from "pretty-ms";
67
import { createQaArtifactRunId } from "./artifact-run-id.js";
78
import { isQaFastModeModelRef, type QaProviderMode } from "./model-selection.js";
89
import {
@@ -261,17 +262,12 @@ function formatDuration(ms: number) {
261262
if (!Number.isFinite(ms) || ms < 0) {
262263
return "unknown";
263264
}
264-
if (ms < 1_000) {
265-
return `${Math.round(ms)}ms`;
266-
}
267-
if (ms < 60_000) {
268-
const seconds = ms / 1_000;
269-
return `${seconds >= 10 ? Math.round(seconds) : Number(seconds.toFixed(1))}s`;
270-
}
271-
const totalSeconds = Math.round(ms / 1_000);
272-
const minutes = Math.floor(totalSeconds / 60);
273-
const seconds = totalSeconds % 60;
274-
return seconds === 0 ? `${minutes}m` : `${minutes}m ${seconds}s`;
265+
const roundedMs = ms < 1000 ? Math.round(ms) : Math.round(ms / 1000) * 1000;
266+
return prettyMilliseconds(roundedMs, {
267+
millisecondsDecimalDigits: 0,
268+
secondsDecimalDigits: 0,
269+
unitCount: 2,
270+
});
275271
}
276272

277273
function logCharacterEvalProgress(

‎extensions/qa-lab/web/src/ui-render.ts‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { sliceUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
2+
import prettyMilliseconds from "pretty-ms";
23
import type {
34
QaEvidenceArtifactView,
45
QaEvidenceGalleryEntryView,
@@ -403,16 +404,12 @@ function formatIso(iso?: string) {
403404
}
404405

405406
function formatDuration(ms: number): string {
406-
if (ms < 1000) {
407-
return `${Math.round(ms)}ms`;
408-
}
409-
const seconds = ms / 1000;
410-
if (seconds < 60) {
411-
return `${seconds.toFixed(seconds >= 10 ? 0 : 1)}s`;
412-
}
413-
const minutes = Math.floor(seconds / 60);
414-
const remainingSeconds = Math.round(seconds % 60);
415-
return `${minutes}m ${remainingSeconds}s`;
407+
const roundedMs = ms < 1000 ? Math.round(ms) : Math.round(ms / 1000) * 1000;
408+
return prettyMilliseconds(Math.max(0, roundedMs), {
409+
millisecondsDecimalDigits: 0,
410+
secondsDecimalDigits: 0,
411+
unitCount: 2,
412+
});
416413
}
417414

418415
function esc(text: string) {

‎extensions/qa-matrix/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"description": "OpenClaw Matrix QA runner plugin",
66
"type": "module",
77
"dependencies": {
8+
"pretty-ms": "9.3.0",
89
"undici": "8.5.0"
910
},
1011
"devDependencies": {

0 commit comments

Comments
 (0)