mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 23:02:02 +03:00
Auto-reply: include weekday in envelope timestamps (#12438)
This commit is contained in:
@@ -23,7 +23,7 @@ describe("formatAgentEnvelope", () => {
|
|||||||
|
|
||||||
process.env.TZ = originalTz;
|
process.env.TZ = originalTz;
|
||||||
|
|
||||||
expect(body).toBe("[WebChat user1 mac-mini 10.0.0.5 2025-01-02T03:04Z] hello");
|
expect(body).toBe("[WebChat user1 mac-mini 10.0.0.5 Thu 2025-01-02T03:04Z] hello");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("formats timestamps in local timezone by default", () => {
|
it("formats timestamps in local timezone by default", () => {
|
||||||
@@ -39,7 +39,7 @@ describe("formatAgentEnvelope", () => {
|
|||||||
|
|
||||||
process.env.TZ = originalTz;
|
process.env.TZ = originalTz;
|
||||||
|
|
||||||
expect(body).toMatch(/\[WebChat 2025-01-01 19:04 [^\]]+\] hello/);
|
expect(body).toMatch(/\[WebChat Wed 2025-01-01 19:04 [^\]]+\] hello/);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("formats timestamps in UTC when configured", () => {
|
it("formats timestamps in UTC when configured", () => {
|
||||||
@@ -56,7 +56,7 @@ describe("formatAgentEnvelope", () => {
|
|||||||
|
|
||||||
process.env.TZ = originalTz;
|
process.env.TZ = originalTz;
|
||||||
|
|
||||||
expect(body).toBe("[WebChat 2025-01-02T03:04Z] hello");
|
expect(body).toBe("[WebChat Thu 2025-01-02T03:04Z] hello");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("formats timestamps in user timezone when configured", () => {
|
it("formats timestamps in user timezone when configured", () => {
|
||||||
@@ -68,7 +68,7 @@ describe("formatAgentEnvelope", () => {
|
|||||||
body: "hello",
|
body: "hello",
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(body).toMatch(/\[WebChat 2025-01-02 04:04 [^\]]+\] hello/);
|
expect(body).toMatch(/\[WebChat Thu 2025-01-02 04:04 [^\]]+\] hello/);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("omits timestamps when configured", () => {
|
it("omits timestamps when configured", () => {
|
||||||
|
|||||||
@@ -107,13 +107,35 @@ function formatTimestamp(
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const zone = resolveEnvelopeTimezone(resolved);
|
const zone = resolveEnvelopeTimezone(resolved);
|
||||||
if (zone.mode === "utc") {
|
// Include a weekday prefix so models do not need to derive DOW from the date
|
||||||
return formatUtcTimestamp(date);
|
// (small models are notoriously unreliable at that).
|
||||||
|
const weekday = (() => {
|
||||||
|
try {
|
||||||
|
if (zone.mode === "utc") {
|
||||||
|
return new Intl.DateTimeFormat("en-US", { timeZone: "UTC", weekday: "short" }).format(date);
|
||||||
|
}
|
||||||
|
if (zone.mode === "local") {
|
||||||
|
return new Intl.DateTimeFormat("en-US", { weekday: "short" }).format(date);
|
||||||
|
}
|
||||||
|
return new Intl.DateTimeFormat("en-US", { timeZone: zone.timeZone, weekday: "short" }).format(
|
||||||
|
date,
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
const formatted =
|
||||||
|
zone.mode === "utc"
|
||||||
|
? formatUtcTimestamp(date)
|
||||||
|
: zone.mode === "local"
|
||||||
|
? formatZonedTimestamp(date)
|
||||||
|
: formatZonedTimestamp(date, { timeZone: zone.timeZone });
|
||||||
|
|
||||||
|
if (!formatted) {
|
||||||
|
return undefined;
|
||||||
}
|
}
|
||||||
if (zone.mode === "local") {
|
return weekday ? `${weekday} ${formatted}` : formatted;
|
||||||
return formatZonedTimestamp(date);
|
|
||||||
}
|
|
||||||
return formatZonedTimestamp(date, { timeZone: zone.timeZone });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatAgentEnvelope(params: AgentEnvelopeParams): string {
|
export function formatAgentEnvelope(params: AgentEnvelopeParams): string {
|
||||||
|
|||||||
@@ -7,13 +7,30 @@ type EnvelopeTimestampZone = string;
|
|||||||
|
|
||||||
export function formatEnvelopeTimestamp(date: Date, zone: EnvelopeTimestampZone = "utc"): string {
|
export function formatEnvelopeTimestamp(date: Date, zone: EnvelopeTimestampZone = "utc"): string {
|
||||||
const normalized = zone.trim().toLowerCase();
|
const normalized = zone.trim().toLowerCase();
|
||||||
|
const weekday = (() => {
|
||||||
|
try {
|
||||||
|
if (normalized === "utc" || normalized === "gmt") {
|
||||||
|
return new Intl.DateTimeFormat("en-US", { timeZone: "UTC", weekday: "short" }).format(date);
|
||||||
|
}
|
||||||
|
if (normalized === "local" || normalized === "host") {
|
||||||
|
return new Intl.DateTimeFormat("en-US", { weekday: "short" }).format(date);
|
||||||
|
}
|
||||||
|
return new Intl.DateTimeFormat("en-US", { timeZone: zone, weekday: "short" }).format(date);
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
if (normalized === "utc" || normalized === "gmt") {
|
if (normalized === "utc" || normalized === "gmt") {
|
||||||
return formatUtcTimestamp(date);
|
const ts = formatUtcTimestamp(date);
|
||||||
|
return weekday ? `${weekday} ${ts}` : ts;
|
||||||
}
|
}
|
||||||
if (normalized === "local" || normalized === "host") {
|
if (normalized === "local" || normalized === "host") {
|
||||||
return formatZonedTimestamp(date) ?? formatUtcTimestamp(date);
|
const ts = formatZonedTimestamp(date) ?? formatUtcTimestamp(date);
|
||||||
|
return weekday ? `${weekday} ${ts}` : ts;
|
||||||
}
|
}
|
||||||
return formatZonedTimestamp(date, { timeZone: zone }) ?? formatUtcTimestamp(date);
|
const ts = formatZonedTimestamp(date, { timeZone: zone }) ?? formatUtcTimestamp(date);
|
||||||
|
return weekday ? `${weekday} ${ts}` : ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatLocalEnvelopeTimestamp(date: Date): string {
|
export function formatLocalEnvelopeTimestamp(date: Date): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user