Files
Moltbot/src/agents/current-time.ts
Tak Hoffman d2c2f4185b Heartbeat: inject cron-style current time into prompts (#13733)
* Heartbeat: inject cron-style current time into prompts

* Tests: fix type for web heartbeat timestamp test

* Infra: inline heartbeat current-time injection
2026-02-10 18:58:45 -06:00

40 lines
1.1 KiB
TypeScript

import {
type TimeFormatPreference,
formatUserTime,
resolveUserTimeFormat,
resolveUserTimezone,
} from "./date-time.js";
export type CronStyleNow = {
userTimezone: string;
formattedTime: string;
timeLine: string;
};
type TimeConfigLike = {
agents?: {
defaults?: {
userTimezone?: string;
timeFormat?: TimeFormatPreference;
};
};
};
export function resolveCronStyleNow(cfg: TimeConfigLike, nowMs: number): CronStyleNow {
const userTimezone = resolveUserTimezone(cfg.agents?.defaults?.userTimezone);
const userTimeFormat = resolveUserTimeFormat(cfg.agents?.defaults?.timeFormat);
const formattedTime =
formatUserTime(new Date(nowMs), userTimezone, userTimeFormat) ?? new Date(nowMs).toISOString();
const timeLine = `Current time: ${formattedTime} (${userTimezone})`;
return { userTimezone, formattedTime, timeLine };
}
export function appendCronStyleCurrentTimeLine(text: string, cfg: TimeConfigLike, nowMs: number) {
const base = text.trimEnd();
if (!base || base.includes("Current time:")) {
return base;
}
const { timeLine } = resolveCronStyleNow(cfg, nowMs);
return `${base}\n${timeLine}`;
}