Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

19.9 KB
Raw Blame
import express from 'express';
import { z } from 'zod';
import { asyncHandler, ServerError } from '../lib/errorHandler.js';
import { validateRequest, parsePagination, calendarAutoConfigureBodySchema, dailyReviewQuerySchema } from '../lib/validation.js';
import { UUID_RE } from '../lib/fileUtils.js';
import { MEETING_URL_MAX } from '../lib/meetingUrl.js';
import * as calendarAccounts from '../services/calendarAccounts.js';
import * as calendarSync from '../services/calendarSync.js';
import * as calendarGoogleSync from '../services/calendarGoogleSync.js';
import * as dailyReview from '../services/dailyReview.js';
import * as googleAuth from '../services/googleAuth.js';
import * as calendarGoogleApiSync from '../services/calendarGoogleApiSync.js';
import * as googleOAuthAutoConfig from '../services/googleOAuthAutoConfig.js';
import { getToken, getTokenStatus, clearTokenCache } from '../services/messageTokenExtractor.js';
import { getUserTimezone } from '../services/userTimezone.js';
import { localDayWindowUtc } from '../lib/timezone.js';

const router = express.Router();

/** A string bounded by its TRIMMED length, so padding can't fail an otherwise fine value. */
const trimmedString = (max) =>
  z.preprocess((v) => (typeof v === 'string' ? v.trim() : v), z.string().max(max));

/** An array CAPPED at `max` rather than rejected past it, so a valid leading item survives. */
const cappedArray = (max, element) =>
  z.preprocess((v) => (Array.isArray(v) ? v.slice(0, max) : v), z.array(element));

// === Validation Schemas ===
const createAccountSchema = z.object({
  name: z.string().min(1),
  type: z.enum(['outlook-calendar', 'google-calendar']),
  email: z.union([z.string().email(), z.literal('')]).optional().default(''),
  syncConfig: z.object({
    maxAge: z.string().optional(),
    syncInterval: z.number().int().positive().optional(),
    calendarIds: z.array(z.string()).optional()
  }).optional()
});

const updateAccountSchema = z.object({
  name: z.string().min(1).optional(),
  email: z.union([z.string().email(), z.literal('')]).optional(),
  enabled: z.boolean().optional(),
  syncConfig: z.object({
    maxAge: z.string().optional(),
    syncInterval: z.number().int().positive().optional(),
    calendarIds: z.array(z.string()).optional()
  }).optional(),
  syncMethod: z.enum(['claude-mcp', 'google-api']).optional()
});

const subcalendarSchema = z.object({
  calendarId: z.string().min(1),
  name: z.string().min(1),
  color: z.string().optional().default(''),
  enabled: z.boolean().optional().default(true),
  dormant: z.boolean().optional().default(false),
  goalIds: z.array(z.string()).optional().default([]),
  addedAt: z.string().optional()
});

const updateSubcalendarsSchema = z.object({
  subcalendars: z.array(subcalendarSchema)
});

const pushSyncSchema = z.object({
  calendarId: z.string().min(1),
  calendarName: z.string().min(1),
  events: z.array(z.object({
    id: z.string().optional(),
    summary: z.string().optional().default(''),
    start: z.object({
      dateTime: z.string().optional(),
      date: z.string().optional()
    }).optional(),
    end: z.object({
      dateTime: z.string().optional(),
      date: z.string().optional()
    }).optional(),
    location: z.string().optional().default(''),
    description: z.string().optional().default(''),
    status: z.string().optional().default('confirmed'),
    htmlLink: z.string().optional(),
    // No defaults: legacy push clients omit identity; explicit null/[] clears it.
    organizer: z.object({
      displayName: z.string().optional(),
      email: z.string().optional(),
      self: z.boolean().optional()
    }).nullable().optional(),
    attendees: z.array(z.object({
      displayName: z.string().optional(),
      email: z.string().optional(),
      self: z.boolean().optional(),
      responseStatus: z.string().optional()
    })).nullable().optional(),
    // Conference metadata feeds the cached `meetingUrl` join link (#6289).
    // OPTIONAL with no default, and nullable: `selectMeetingUrl` distinguishes
    // a producer that omitted them (preserve the cached link) from one that
    // explicitly sent none (clear it), so a default here would make every
    // legacy push wipe a working Join action. Non-video entry types are
    // accepted and ignored downstream, so ordinary phone/SIP metadata can't
    // reject an otherwise valid event; Zod strips the conference passwords and
    // dial-in codes Google ships alongside them, and the length bound comes
    // from the selector's own module so the two can't drift.
    //
    // The bounds FAIL SOFT via `.catch`, at THREE granularities, because what
    // a malformed value should cost depends on how much of the payload it
    // implicates. Throwing costs the most: `validateRequest` 400s the WHOLE
    // batch, so one 1301-character `uri` — even on a `phone` entry the
    // selector ignores — silently stops the user's entire calendar from
    // syncing, while an unbounded `description` sails through.
    //
    //   per ENTRY  → a bad entry becomes `{}`, which the selector skips. This
    //                is the granularity that matters: a malformed phone entry
    //                must not erase the usable video link sitting beside it.
    //   per FIELD  → a bad `hangoutLink` / `conferenceData` becomes null, i.e.
    //                "described, nothing usable came of it" — exactly true.
    //   per EVENT  → nothing. Every other event in the batch still syncs.
    //
    // Omission short-circuits ahead of every catch, so an older client that
    // sends neither field still reads as "not described" and keeps its link.
    // The bounds also NORMALIZE before measuring, so this ingress is not
    // needlessly stricter than the selector it feeds — anything it rejects
    // reads downstream as "this meeting has no conference" and CLEARS a link
    // the API and MCP paths keep for the same input. Untrimmed padding counted
    // toward the length cap (1300 leading spaces sank a URL the selector trims
    // and accepts), and an over-long array was rejected outright rather than
    // capped, losing a valid leading video entry to its junk siblings.
    //
    // Truncation past 100 entries is the one place a bound can still drop a
    // usable candidate, and it is the deliberate residue of having any bound at
    // all: Google emits a handful of entry points, so 100 is ~20x headroom, and
    // capping strictly beats rejecting the whole array as it used to.
    hangoutLink: trimmedString(MEETING_URL_MAX).nullable().optional().catch(null),
    conferenceData: z.object({
      entryPoints: cappedArray(100, z.object({
        entryPointType: trimmedString(32).nullable().optional(),
        uri: trimmedString(MEETING_URL_MAX).nullable().optional()
      }).catch(() => ({}))).nullable().optional().catch(null)
    }).nullable().optional().catch(null)
  }))
});

const confirmEventSchema = z.object({
  eventId: z.string().min(1),
  happened: z.boolean(),
  goalId: z.string().optional(),
  durationMinutes: z.number().int().min(1).max(1440).optional(),
  note: z.string().max(1000).optional()
});

// === Account Routes ===
router.get('/accounts', asyncHandler(async (req, res) => {
  const accounts = await calendarAccounts.listAccounts();
  res.json(accounts);
}));

router.post('/accounts', asyncHandler(async (req, res) => {
  const data = validateRequest(createAccountSchema, req.body);
  const account = await calendarAccounts.createAccount(data);
  req.app.get('io')?.emit('calendar:changed', {});
  res.status(201).json(account);
}));

router.put('/accounts/:id', asyncHandler(async (req, res) => {
  if (!UUID_RE.test(req.params.id)) {
    throw new ServerError('Invalid account ID format', { status: 400, code: 'VALIDATION_ERROR' });
  }
  const updates = validateRequest(updateAccountSchema, req.body);
  const account = await calendarAccounts.updateAccount(req.params.id, updates);
  if (!account) throw new ServerError('Account not found', { status: 404, code: 'NOT_FOUND' });
  req.app.get('io')?.emit('calendar:changed', {});
  res.json(account);
}));

router.delete('/accounts/:id', asyncHandler(async (req, res) => {
  if (!UUID_RE.test(req.params.id)) {
    throw new ServerError('Invalid account ID format', { status: 400, code: 'VALIDATION_ERROR' });
  }
  // Remove metadata first so queued/provider-delayed writes cannot recreate
  // the cache. Retry cleanup even if an earlier attempt removed the account.
  await calendarAccounts.deleteAccount(req.params.id);
  await calendarSync.deleteCache(req.params.id);
  // Derived copies (titles, attendees) live in the Timeline and Tribe (#12984).
  // Lazy: these reach settings/DB modules that most route suites (and boot) never need.
  const [humanActivity, { deleteAutoTouchpoints }] = await Promise.all([import('../services/humanActivity.js'), import('../services/tribe.js')]);
  await humanActivity.deleteEvents({ accountId: req.params.id });
  await deleteAutoTouchpoints({ accountId: req.params.id });
  req.app.get('io')?.emit('calendar:changed', {});
  res.status(204).send();
}));

// === Sync Routes ===
router.post('/sync/:accountId', asyncHandler(async (req, res) => {
  if (!UUID_RE.test(req.params.accountId)) {
    throw new ServerError('Invalid account ID format', { status: 400, code: 'VALIDATION_ERROR' });
  }
  const io = req.app.get('io');
  const result = await calendarSync.syncAccount(req.params.accountId, io);
  res.json(result);
}));

router.get('/sync/:accountId/status', asyncHandler(async (req, res) => {
  if (!UUID_RE.test(req.params.accountId)) {
    throw new ServerError('Invalid account ID format', { status: 400, code: 'VALIDATION_ERROR' });
  }
  const status = await calendarSync.getSyncStatus(req.params.accountId);
  if (!status) throw new ServerError('Account not found', { status: 404 });
  res.json(status);
}));

// === Event Routes ===

// Today's agenda in the user's timezone — the dashboard widget's read. The
// day window comes from localDayWindowUtc (shared with the voice
// calendar_today tool) so DST-transition days keep the right bounds.
router.get('/agenda', asyncHandler(async (req, res) => {
  const { limit } = parsePagination(req.query, { defaultLimit: 8, maxLimit: 20 });
  const accounts = await calendarAccounts.listAccounts();
  const accountCount = accounts.filter((a) => a.enabled !== false).length;
  if (accountCount === 0) {
    return res.json({ date: null, timezone: null, accountCount: 0, events: [], total: 0 });
  }
  const timezone = await getUserTimezone();
  const { date, startDate, endDate } = localDayWindowUtc(timezone);
  const { events = [], total = 0 } = await calendarSync.getEvents({ startDate, endDate, limit });
  res.json({
    date,
    timezone,
    accountCount,
    events: events.map((e) => ({
      id: e.id,
      accountId: e.accountId,
      title: e.title || 'Untitled event',
      startTime: e.startTime,
      endTime: e.endTime ?? null,
      isAllDay: !!e.isAllDay,
      location: e.location || null
    })),
    total
  });
}));

router.get('/events', asyncHandler(async (req, res) => {
  const { accountId, search, startDate, endDate } = req.query;
  if (accountId && !UUID_RE.test(accountId)) {
    throw new ServerError('Invalid accountId format', { status: 400 });
  }
  const { limit: parsedLimit, offset: parsedOffset } = parsePagination(req.query, { defaultLimit: 50, maxLimit: 200 });
  const result = await calendarSync.getEvents({
    accountId,
    search,
    startDate,
    endDate,
    limit: parsedLimit,
    offset: parsedOffset
  });
  res.json(result);
}));

router.get('/events/:accountId/:eventId', asyncHandler(async (req, res) => {
  if (!UUID_RE.test(req.params.accountId)) {
    throw new ServerError('Invalid accountId format', { status: 400 });
  }
  const event = await calendarSync.getEvent(req.params.accountId, req.params.eventId);
  if (!event) throw new ServerError('Event not found', { status: 404 });
  res.json(event);
}));

// === Subcalendar Routes ===
router.get('/accounts/:id/subcalendars', asyncHandler(async (req, res) => {
  if (!UUID_RE.test(req.params.id)) {
    throw new ServerError('Invalid account ID format', { status: 400, code: 'VALIDATION_ERROR' });
  }
  const account = await calendarAccounts.getAccount(req.params.id);
  if (!account) throw new ServerError('Account not found', { status: 404, code: 'NOT_FOUND' });
  res.json(account.subcalendars || []);
}));

router.put('/accounts/:id/subcalendars', asyncHandler(async (req, res) => {
  if (!UUID_RE.test(req.params.id)) {
    throw new ServerError('Invalid account ID format', { status: 400, code: 'VALIDATION_ERROR' });
  }
  const { subcalendars } = validateRequest(updateSubcalendarsSchema, req.body);
  const account = await calendarAccounts.updateSubcalendars(req.params.id, subcalendars);
  if (!account) throw new ServerError('Account not found', { status: 404, code: 'NOT_FOUND' });
  // Auto-purge cached events for disabled subcalendars
  await calendarSync.purgeDisabledSubcalendars(req.params.id);
  req.app.get('io')?.emit('calendar:changed', {});
  res.json(account);
}));

// === Push Sync Route ===
router.post('/sync/:accountId/push', asyncHandler(async (req, res) => {
  if (!UUID_RE.test(req.params.accountId)) {
    throw new ServerError('Invalid account ID format', { status: 400, code: 'VALIDATION_ERROR' });
  }
  const data = validateRequest(pushSyncSchema, req.body);
  const io = req.app.get('io');
  const result = await calendarGoogleSync.pushSyncEvents(req.params.accountId, data.calendarId, data.calendarName, data.events, io);
  res.json(result);
}));

// === MCP Discover Calendars Route ===
router.post('/sync/:accountId/discover', asyncHandler(async (req, res) => {
  if (!UUID_RE.test(req.params.accountId)) {
    throw new ServerError('Invalid account ID format', { status: 400, code: 'VALIDATION_ERROR' });
  }
  const io = req.app.get('io');
  const result = await calendarGoogleSync.mcpDiscoverCalendars(req.params.accountId, io);
  req.app.get('io')?.emit('calendar:changed', {});
  res.json(result);
}));

// === MCP Sync Route ===
router.post('/sync/:accountId/google', asyncHandler(async (req, res) => {
  if (!UUID_RE.test(req.params.accountId)) {
    throw new ServerError('Invalid account ID format', { status: 400, code: 'VALIDATION_ERROR' });
  }
  const io = req.app.get('io');
  const result = await calendarGoogleSync.mcpSyncAccount(req.params.accountId, io);
  res.json(result);
}));

// === Google OAuth Routes ===
router.get('/google/auth/status', asyncHandler(async (req, res) => {
  const status = await googleAuth.getAuthStatus();
  res.json(status);
}));

router.post('/google/auth/credentials', asyncHandler(async (req, res) => {
  const schema = z.object({
    clientId: z.string().min(1),
    clientSecret: z.string().min(1)
  });
  const data = validateRequest(schema, req.body);
  const result = await googleAuth.saveCredentials(data);
  res.json(result);
}));

router.get('/google/auth/url', asyncHandler(async (req, res) => {
  const { returnTo } = validateRequest(
    z.object({ returnTo: z.enum(['calendar', 'messages']).optional().default('calendar') }),
    req.query
  );
  const result = await googleAuth.getAuthUrl(returnTo);
  res.json(result);
}));

router.get('/google/oauth/callback', asyncHandler(async (req, res) => {
  // This endpoint is hit by a BROWSER redirect from Google, not by the SPA —
  // render every outcome as a redirect to the config page (which toasts the
  // oauthError param) instead of the JSON envelope the middleware would send.
  const configPath = req.query.state === 'messages' ? '/messages/config' : '/calendar/config';
  const configUrl = (error) => error
    ? `${configPath}?oauthError=${encodeURIComponent(error)}`
    : configPath;
  const code = req.query.code;
  if (!code) return res.redirect(configUrl('Missing authorization code'));
  const error = await googleAuth.handleCallback(code).then(() => null)
    .catch((err) => {
      // This catch replaces asyncHandler's logging (the redirect swallows the
      // throw), so keep the failure visible in server logs.
      console.error(`❌ Google OAuth callback failed: ${err.message}`);
      return err.message || 'Google OAuth callback failed';
    });
  res.redirect(configUrl(error));
}));

router.post('/google/auth/clear', asyncHandler(async (req, res) => {
  await googleAuth.clearAuth();
  res.json({ cleared: true });
}));

// === Google OAuth Auto-Configure via CDP Browser ===
router.post('/google/auto-configure/start', asyncHandler(async (req, res) => {
  const io = req.app.get('io');
  const result = await googleOAuthAutoConfig.startAutoConfig(io);
  res.json(result);
}));

router.post('/google/auto-configure/capture', asyncHandler(async (req, res) => {
  const io = req.app.get('io');
  const result = await googleOAuthAutoConfig.captureCredentials(io);
  res.json(result);
}));

router.post('/google/auto-configure/run', asyncHandler(async (req, res) => {
  const io = req.app.get('io');
  const { email = '', requestId } = validateRequest(calendarAutoConfigureBodySchema, req.body ?? {});
  const result = await googleOAuthAutoConfig.runAutomatedSetup(email, io, requestId);
  res.json(result);
}));

// === Google API Sync Routes ===
router.post('/sync/:accountId/api', asyncHandler(async (req, res) => {
  if (!UUID_RE.test(req.params.accountId)) {
    throw new ServerError('Invalid account ID format', { status: 400, code: 'VALIDATION_ERROR' });
  }
  const io = req.app.get('io');
  const result = await calendarGoogleApiSync.apiSyncAccount(req.params.accountId, io);
  res.json(result);
}));

router.post('/sync/:accountId/discover-api', asyncHandler(async (req, res) => {
  if (!UUID_RE.test(req.params.accountId)) {
    throw new ServerError('Invalid account ID format', { status: 400, code: 'VALIDATION_ERROR' });
  }
  const result = await calendarGoogleApiSync.apiDiscoverCalendars(req.params.accountId);
  req.app.get('io')?.emit('calendar:changed', {});
  res.json(result);
}));

// === Daily Review Routes ===
// NOTE: /review/history must come before /review/:date so Express doesn't match "history" as a date param
router.get('/review/history', asyncHandler(async (req, res) => {
  const { startDate, endDate } = req.query;
  const history = await dailyReview.getDailyReviewHistory(startDate, endDate);
  res.json(history);
}));

router.get('/review/:date', asyncHandler(async (req, res) => {
  const dateStr = req.params.date;
  if (!/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
    throw new ServerError('Invalid date format, use YYYY-MM-DD', { status: 400, code: 'VALIDATION_ERROR' });
  }
  const { limit, offset } = validateRequest(dailyReviewQuerySchema, req.query);
  const review = await dailyReview.getDailyReview(dateStr, { limit, offset });
  res.json(review);
}));

router.post('/review/:date/confirm', asyncHandler(async (req, res) => {
  const dateStr = req.params.date;
  if (!/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
    throw new ServerError('Invalid date format, use YYYY-MM-DD', { status: 400, code: 'VALIDATION_ERROR' });
  }
  const data = validateRequest(confirmEventSchema, req.body);
  const result = await dailyReview.confirmEvent(dateStr, data);
  res.json(result);
}));

// === Debug: Token Status (reuse message token extractor) ===
router.get('/debug/token-status', asyncHandler(async (req, res) => {
  const statuses = ['outlook'].map(p => getTokenStatus(p));
  res.json({ providers: statuses });
}));

router.post('/debug/test-token', asyncHandler(async (req, res) => {
  const provider = 'outlook';
  const tokenResult = await getToken(provider);
  if (tokenResult.error) {
    throw new ServerError(tokenResult.message || tokenResult.error, {
      status: 503,
      context: { reason: tokenResult.error, provider: tokenResult.provider }
    });
  }

  const decoded = tokenResult.decoded || {};
  const tokenInfo = {
    provider,
    fresh: tokenResult.fresh,
    length: tokenResult.token.length,
    expires: decoded.exp ? new Date(decoded.exp * 1000).toISOString() : 'unknown',
    audience: decoded.aud || 'unknown',
    scopes: decoded.scp || decoded.roles || 'unknown'
  };

  res.json({ token: tokenInfo });
}));

router.post('/debug/clear-token', asyncHandler(async (req, res) => {
  clearTokenCache('outlook');
  res.json({ cleared: true, provider: 'outlook' });
}));

export default router;