Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions templates/calendar/actions/get-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@ import { getRequestUserEmail } from "@agent-native/core/server";
import { getUserSetting } from "@agent-native/core/settings";
import { z } from "zod";

import { getDefaultSettings } from "../server/lib/calendar-settings.js";
import type { Settings } from "../shared/api.js";

const DEFAULT_SETTINGS: Settings = {
timezone: "America/New_York",
bookingPageTitle: "Book a Meeting",
bookingPageDescription: "Select a time that works for you.",
defaultEventDuration: 30,
};

export default defineAction({
description: "Get calendar settings",
schema: z.object({}),
http: { method: "GET" },
run: async () => {
const email = getRequestUserEmail();
if (!email) throw new Error("no authenticated user");
const settings =
(await getUserSetting(email, "calendar-settings")) || DEFAULT_SETTINGS;
return settings;
const settings = (await getUserSetting(
email,
"calendar-settings",
)) as Settings | null;
return settings || getDefaultSettings();
},
});
62 changes: 61 additions & 1 deletion templates/calendar/actions/list-events.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

const getRequestTimezoneMock = vi.hoisted(() => vi.fn());
const getRequestUserEmailMock = vi.hoisted(() => vi.fn());
Expand Down Expand Up @@ -252,6 +252,10 @@ describe("list-events inventory contract", () => {
verifyShortLivedTokenMock.mockReturnValue({ ok: true });
});

afterEach(() => {
vi.useRealTimers();
});

it("keeps legacy callers on CalendarEvent arrays", async () => {
const result = await (listEventsAction as any).run(
{ from: "2026-06-17", to: "2026-06-18" },
Expand Down Expand Up @@ -704,6 +708,62 @@ describe("list-events inventory contract", () => {
expect(listGoogleEventsMock).not.toHaveBeenCalled();
});

it("uses the saved timezone for omitted-range inventory cursors", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-06-17T12:00:00.000Z"));
getRequestTimezoneMock.mockReturnValue("UTC");
getUserSettingMock.mockResolvedValue({ timezone: "America/New_York" });
listGoogleEventsMock.mockResolvedValue({
events: [
{
id: "google-event-1",
googleEventId: "event-1",
title: "First",
description: "",
start: "2026-06-17T16:00:00.000Z",
end: "2026-06-17T16:30:00.000Z",
location: "",
allDay: false,
source: "google",
accountEmail: "steve@example.com",
createdAt: "2026-06-12T10:13:39.746Z",
updatedAt: "2026-06-12T10:13:39.746Z",
},
{
id: "google-event-2",
googleEventId: "event-2",
title: "Second",
description: "",
start: "2026-06-17T17:00:00.000Z",
end: "2026-06-17T17:30:00.000Z",
location: "",
allDay: false,
source: "google",
accountEmail: "steve@example.com",
createdAt: "2026-06-12T10:13:39.746Z",
updatedAt: "2026-06-12T10:13:39.746Z",
},
],
errors: [],
});

const first = await (listEventsAction as any).run(
{ format: "inventory", pageSize: 1, sources: ["google"] },
{ caller: "mcp" },
);
const second = await (listEventsAction as any).run(
{
format: "inventory",
pageSize: 1,
sources: ["google"],
cursor: first.page.nextCursor,
},
{ caller: "mcp" },
);

expect(second.items.map((item: any) => item.id)).toEqual(["event-2"]);
});

it("rejects a malformed inventory cursor before provider reads", async () => {
await expect(
(listEventsAction as any).run(
Expand Down
9 changes: 9 additions & 0 deletions templates/calendar/actions/list-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { and, gte, inArray, lte, ne } from "drizzle-orm";
import { z } from "zod";

import { getDb, schema } from "../server/db/index.js";
import { getCalendarTimezone } from "../server/lib/calendar-settings.js";
import * as googleCalendar from "../server/lib/google-calendar.js";
import { fetchICalEvents } from "../server/lib/ical-fetcher.js";
import type { CalendarEvent, ExternalCalendar } from "../shared/api.js";
Expand Down Expand Up @@ -74,6 +75,7 @@ interface ListCalendarEventsArgs {
interface ListCalendarEventsOptions {
ownedAccounts?: string[];
range?: CalendarEventRange;
timezone?: string;
}

type CalendarInventorySource = "google" | "bookings" | "ics" | "overlays";
Expand Down Expand Up @@ -578,11 +580,13 @@ export async function listCalendarEvents(
): Promise<CalendarEventsResult> {
const email = getRequestUserEmail();
if (!email) throw new Error("no authenticated user");
const timezone = options.timezone ?? (await getCalendarTimezone(email));
const range =
options.range ??
resolveCalendarEventRange({
from: args.from,
to: args.to,
timezone,
});

const sources = resolveInventorySources(args.sources);
Expand Down Expand Up @@ -829,6 +833,9 @@ export default defineAction({
args.format === "inventory" || (ctx?.caller === "mcp" && !args.format);
const owner = inventory ? getRequestUserEmail() : undefined;
if (inventory && !owner) throw new Error("no authenticated user");
const calendarTimezone = inventory
? await getCalendarTimezone(owner!)
: undefined;

// Reject invalid, expired, owner-bound, and query-bound cursors before any
// provider call. Omitted account filters require the cheap owned-account
Expand All @@ -842,6 +849,7 @@ export default defineAction({
preparedRange = resolveCalendarEventRange({
from: args.from,
to: args.to,
timezone: calendarTimezone,
});
preparedOwnedAccounts = args.accountEmails
? undefined
Expand All @@ -868,6 +876,7 @@ export default defineAction({
{
ownedAccounts: preparedOwnedAccounts,
range: preparedRange,
timezone: calendarTimezone,
},
);

Expand Down
10 changes: 9 additions & 1 deletion templates/calendar/actions/update-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import { getRequestUserEmail } from "@agent-native/core/server";
import { putUserSetting, putSetting } from "@agent-native/core/settings";
import { z } from "zod";

import { isCalendarTimezone } from "../server/lib/calendar-settings.js";
import type { Settings } from "../shared/api.js";

export default defineAction({
description: "Update calendar settings",
schema: z.object({
timezone: z.string().optional().describe("Timezone"),
timezone: z
.string()
.trim()
.refine(isCalendarTimezone, {
message: "Timezone must be a valid IANA timezone.",
})
.optional()
.describe("IANA timezone, e.g. Europe/Warsaw"),
bookingPageTitle: z.string().optional().describe("Booking page title"),
bookingPageDescription: z
.string()
Expand Down
23 changes: 13 additions & 10 deletions templates/calendar/actions/view-screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { defineAction } from "@agent-native/core";
import { readAppState } from "@agent-native/core/application-state";
import { getRequestUserEmail } from "@agent-native/core/server";
import { accessFilter } from "@agent-native/core/sharing";
import { addDays, parseISO, startOfWeek } from "date-fns";
import { fromZonedTime, toZonedTime } from "date-fns-tz";
import { z } from "zod";

import { getDb, schema } from "../server/db/index.js";
import { rowToBookingLink } from "../server/lib/booking-link-utils.js";
import { getCalendarTimezone } from "../server/lib/calendar-settings.js";
import type { CalendarEvent, CalendarEventDraft } from "../shared/api.js";
import {
CALENDAR_VIEW_PREFERENCES_KEY,
Expand Down Expand Up @@ -67,18 +70,18 @@ export default defineAction({
const nav = navigation as any;

if (nav?.view === "calendar" || !nav?.view) {
const now = new Date();
const viewDate = nav?.date ? new Date(nav.date) : now;

const from = new Date(viewDate);
from.setDate(from.getDate() - from.getDay());
from.setHours(0, 0, 0, 0);
const to = new Date(from);
to.setDate(to.getDate() + 7);
const email = getRequestUserEmail();
if (!email) throw new Error("no authenticated user");
const timezone = await getCalendarTimezone(email);
const viewDate = nav?.date
? parseISO(nav.date)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Parse navigation dates in the configured timezone

navigate documents date as a bare YYYY-MM-DD, but parseISO(nav.date) creates a server-local date before startOfWeek and fromZonedTime are applied. When the server timezone differs from the saved calendar timezone, view-screen can compute a shifted week and return context for the wrong dates; parse the date-only value as a wall-clock date in timezone instead.

Fix in Builder

: toZonedTime(new Date(), timezone);
const from = startOfWeek(viewDate);
const to = addDays(from, 7);

const eventResult = await fetchEventsForRange(
from.toISOString(),
to.toISOString(),
fromZonedTime(from, timezone).toISOString(),
fromZonedTime(to, timezone).toISOString(),
);
const { events } = eventResult;

Expand Down
10 changes: 9 additions & 1 deletion templates/calendar/app/components/calendar/CommandPalette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "@tabler/icons-react";
import * as chrono from "chrono-node";
import { format, parseISO, parse, isValid } from "date-fns";
import { toZonedTime } from "date-fns-tz";

import { cn } from "@/lib/utils";

Expand All @@ -28,6 +29,7 @@ interface CommandPaletteProps {
open: boolean;
onClose: () => void;
events: CalendarEvent[];
timezone?: string;
onGoToDate: (date: Date) => void;
onEventClick: (event: CalendarEvent) => void;
onCreateEvent: () => void;
Expand Down Expand Up @@ -80,6 +82,7 @@ export function CommandPalette({
open,
onClose,
events,
timezone,
onGoToDate,
onEventClick,
onCreateEvent,
Expand Down Expand Up @@ -192,7 +195,12 @@ export function CommandPalette({
/>
<span className="flex-1 truncate">{event.title}</span>
<span className="ml-2 text-xs text-muted-foreground">
{format(parseISO(event.start), "MMM d")}
{format(
event.allDay || !timezone
? parseISO(event.start)
: toZonedTime(event.start, timezone),
Comment thread
builder-io-integration[bot] marked this conversation as resolved.
"MMM d",
)}
</span>
</CommandMenu.Item>
))}
Expand Down
Loading
Loading