A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.
TypeScript
import { localDateTime } from "../../../packages/domain/src/date-time.ts";
export { localDateTime };
/** Resolve a local wall-clock time, rejecting gaps at daylight-saving transitions. */
export function zonedInstant(date: string, time: string, timeZone: string): string {
if (!/^\d{4}-\d{2}-\d{2}$/.test(date) || !/^\d{2}:\d{2}$/.test(time))
throw new Error("Enter a complete date and time.");
const desired = Date.parse(`${date}T${time}:00Z`);
if (
!Number.isFinite(desired) ||
new Date(desired).toISOString().slice(0, 16) !== `${date}T${time}`
)
throw new Error("Choose a valid date and time.");
let candidate = desired;
for (let pass = 0; pass < 4; pass++) {
const local = localDateTime(new Date(candidate).toISOString(), timeZone);
const actual = Date.parse(`${local.date}T${local.time}:00Z`);
const delta = desired - actual;
if (delta === 0) return new Date(candidate).toISOString();
candidate += delta;
}
throw new Error("This time does not exist in the selected time zone. Choose another time.");
}
/**
* The first existing instant of a local day. Day-range boundaries are an internal
* computation, not a user-entered appointment time, so when a DST gap removes
* midnight (e.g. America/Santiago springs forward 00:00→01:00) the boundary
* clamps to the first existing local time instead of failing the whole query.
*/
export function startOfZonedDay(date: string, timeZone: string): string {
try {
return zonedInstant(date, "00:00", timeZone);
} catch (error) {
if (!(error instanceof Error) || !error.message.includes("does not exist")) throw error;
}
for (let minutes = 1; minutes < 24 * 60; minutes++) {
const time = `${String(Math.floor(minutes / 60)).padStart(2, "0")}:${String(minutes % 60).padStart(2, "0")}`;
try {
return zonedInstant(date, time, timeZone);
} catch {
// Still inside the gap; keep walking toward the first existing time.
}
}
throw new Error("This day has no existing local time in the selected time zone.");
}
/** Only fully serialized instants may reset a date editor's local text. */
export function isCompleteInstant(value: string): boolean {
return (
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/.test(value) &&
Number.isFinite(Date.parse(value))
);
}