import { z } from "zod"; import { DEFAULT_STALE_MINUTES, isStale, getDataFreshness as readDataFreshness, } from "../utils/freshness"; import { defineCapability } from "./context"; export const getDataFreshness = defineCapability({ name: "getDataFreshness", description: "Check how recently activity was ingested and synced. Use before generating invoices or reports to warn the user when recent work may be missing and they should run sync/ingest first.", requiredScope: "timeline:read", inputSchema: z.object({ maxAgeMinutes: z .number() .optional() .default(DEFAULT_STALE_MINUTES) .describe("Treat ingest older than this many minutes as stale."), }), execute: async (ctx, { maxAgeMinutes }) => { // Scope to the caller's org/devices so the hub can't observe another org's // ingest/sync state (scan/sync history is local-only and unscoped). const freshness = await readDataFreshness(ctx.db, { organizationId: ctx.organizationId, deviceIds: ctx.deviceIds, }); return { ...freshness, stale: isStale({ lastIngestAt: freshness.lastIngestAt, maxAgeMinutes }), maxAgeMinutes, }; }, });