SLOPSHOPPER

calendar

[Experimental] Upcoming Google Calendar events in-session — /cal toggles a band above the prompt with the next 7 days, /cal N for N days (Claude Mod; needs…

bandcommandprocesstimer
★ 2v0.1.6MITupdated 2026-10-16musingfox/cc-plugins/calendar
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · calendar
› fix the failing auth test and add an audit log call ⏺ Read(src/auth.ts) ⎿ Read 6 lines ⏺ Update(src/auth.ts) ⎿ Added 2 lines, removed 1 line ⏺ Bash(bun test) ⎿ 3 pass, 1 fail ● Done. refresh now rejects expired claims and logs an audit event. ✻ Worked for 42s · done 4:20 PM › /cal Unavailable: Google Calendar connector did not answer ⟨Claude Code's own drawing⟩ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Band
Unavailable: Google Calendar connector did not answer ⟨Claude Code's own drawing⟩
README

calendar

Shows upcoming Google Calendar events inside a Claude Code session, as one Claude Mod (a function-hooks module, hooks/register.ts). Nothing it shows enters the conversation.

  • /cal: toggles a band above the prompt listing the next 7 days of events.
  • /cal N: lists the next N days (1–31), turns the band on, and remembers N.

Needs Claude Code 2.1.287 or later, where Claude Mods are on by default. Built and tested against Claude Code 2.1.287.

Where events come from

Every fetch goes through the claude.ai Google Calendar connector with $.mcp.call: one list_calendars, then one list_events per calendar for the window from now to N days ahead. Every calendar you can see is listed except those named in the plugin option exclude_calendars (names or ids). Claude Code asks for it when the plugin is enabled; a list option has no /config row, so change it later under pluginConfigs["calendar@<marketplace>"].options in ~/.claude/settings.json.

Events are fetched when /cal turns the band on, at session start when the band was left on, and every 15 minutes while it is on. Nothing is fetched while the band is off. A fetch never blocks a prompt or a tool call. The claude.ai connectors are still connecting when a session starts, so a failed fetch with nothing yet on screen is retried every 30 s, at most 10 times.

$.mcp.call asks no permission. Any installed Claude Mod can read your mail and calendar through a connected claude.ai connector this way. Install third-party mods with that in mind.

The band

One line per event, soonest first, in the machine's time zone (TZ when set):

今天      14:00–15:00        Dentist  @Clinic  還有 4h  [ 日曆 ]
明天      全天               中秋節  [ 日曆 ]
09/27 日  22:00–09/28 01:30  Trip  [ 日曆 ]  [ 連結 ]
09/28 一  18:00–21:00        Dinner  @Home  [ 日曆 ]  [ 連結 ]
  • The day reads 今天, 明天, or MM/DD <weekday>, in blue.
  • An all-day event reads 全天; a timed one its start and end, with the end's date when it ends on another day.
  • The first timed event (the one under way, or the next to start) has its title in bold green. Today's timed events end in a green countdown, 還有 3h 45m, or 進行中 once started; the band redraws every minute so it keeps moving.
  • @<location> follows the title, dimmed, when the event has one and it is not a link.
  • Each line ends in buttons that open a page in the browser without printing its URL. [ 日曆 ] opens the event in Google Calendar, or its day when the event has no link. [ 連結 ] opens the event's meeting (Google Meet or another conference's video link), or its location when the location is an https link. On the terminal a button runs open (xdg-open where there is no open); the desktop app draws them as links.
  • Cancelled events are left out, and an event shared into several calendars shows once.
  • A dim notice line says Fetching calendar events, Unavailable: <reason> before any fetch succeeded, Stale: <reason>; showing data from <age> ago after a failed one, or No events in the next N days.
  • More events than the band holds end in a dim … N more. Every line is cut at the band's width.

The band shares its space with any band another mod draws there (/quota, for one) instead of hiding it. The band's on/off state and N are kept in the plugin's store and read at session start.

Tests

claude plugin test calendar

The fixtures in tests/fixtures/world.ts are synthetic; no real calendar data belongs in this repo.

Source 4 files
hooks/register.ts 185 lines
1import type { On } from 'claude-code'
2import { CALENDAR_BUTTON, calModelOf, URL_BUTTON } from './cal-model.ts'
3import type { CalView } from './cal-model.ts'
4import { calendarsOf, eventsOf, payloadOf, wantedOf } from './events.ts'
5import type { CalEvent } from './events.ts'
6import { drawable } from './text.ts'
7
8const SERVER = 'claude.ai Google Calendar'
9const DEFAULT_DAYS = 7
10const MAX_DAYS = 31
11const DAY_MS = 86_400_000
12const POLL_MS = 900_000
13const RETRY_MS = 30_000
14const MAX_RETRIES = 10
15const BAND_KEY = 'band'
16const DAYS_KEY = 'days'
17const REASON_MAX = 9900
18
19type Reading = { ok: true; events: CalEvent[] } | { ok: false; reason: string }
20
21let view: CalView = { events: null, failure: null, lastGoodAt: null, days: DEFAULT_DAYS }
22let bandOn = false
23let excluded: string[] = []
24let poll: { cancel(): void } | null = null
25let tick: { cancel(): void } | null = null
26let fetchesStarted = 0
27let lastPublished = 0
28let retries = 0
29
30async function call($: any, tool: string, args: Record<string, unknown>): Promise<unknown> {
31  try {
32    return payloadOf(await $.mcp.call(SERVER, tool, args))
33  } catch {
34    return undefined
35  }
36}
37
38async function fetchEvents($: any, days: number): Promise<Reading> {
39  const calendars = calendarsOf(await call($, 'list_calendars', {}))
40  if (!calendars) return { ok: false, reason: 'Google Calendar connector did not answer' }
41  const now = await $.clock.now()
42  const window = { startTime: new Date(now).toISOString(), endTime: new Date(now + days * DAY_MS).toISOString() }
43  const wanted = wantedOf(calendars, excluded)
44  const lists = await Promise.all(
45    wanted.map(async (c) =>
46      eventsOf(await call($, 'list_events', { calendarId: c.id, ...window, orderBy: 'startTime', pageSize: 100 })),
47    ),
48  )
49  const failed = wanted.filter((_, i) => lists[i] === null).map((c) => c.name)
50  // The notice adds its own prefix and a duration, so the reason stays under the 10000 draw bound with room to spare.
51  if (failed.length) return { ok: false, reason: drawable(`could not read ${failed.join(', ')}`, REASON_MAX) }
52  return { ok: true, events: lists.flatMap((l) => l ?? []) }
53}
54
55// A fetch that started before one already published would put older data back on screen.
56async function fetchAndPublish($: any) {
57  const seq = ++fetchesStarted
58  const reading = await fetchEvents($, view.days)
59  const now = await $.clock.now()
60  if (seq < lastPublished) return
61  lastPublished = seq
62  view = reading.ok
63    ? { ...view, events: reading.events, failure: null, lastGoodAt: now }
64    : { ...view, failure: reading.reason }
65  $.ui.invalidate('ui.render')
66  // At session start the claude.ai connectors are still connecting, so a band left on
67  // would otherwise read "Unavailable" until the 15-minute poll.
68  if (reading.ok) retries = 0
69  else if (!view.events && bandOn && retries < MAX_RETRIES) {
70    retries += 1
71    $.clock.after(RETRY_MS, () => {
72      void fetchAndPublish($).catch(() => {})
73    })
74  }
75}
76
77async function remember($: any, key: string, value: unknown) {
78  try {
79    await $.store.set(key, value)
80  } catch {
81    // The band still follows the command for this session; only the next one will not remember it.
82  }
83}
84
85async function readStore($: any) {
86  try {
87    bandOn = (await $.store.get(BAND_KEY)) === true
88    const days = await $.store.get(DAYS_KEY)
89    if (Number.isInteger(days) && days >= 1 && days <= MAX_DAYS) view = { ...view, days }
90  } catch {
91    bandOn = false
92  }
93}
94
95async function showBand($: any, on: boolean) {
96  bandOn = on
97  await remember($, BAND_KEY, on)
98  $.ui.invalidate('ui.render')
99  if (on) void fetchAndPublish($).catch(() => {})
100  return {}
101}
102
103const DAY_COLOR = '#5b9cf5'
104const NEXT_COLOR = '#46a758'
105
106// The engine has no call that opens a URL, and a Link the terminal cannot open prints its URL.
107function openUrl($: any, url: string) {
108  return $.process.run(['open', url]).catch(() => $.process.run(['xdg-open', url]))
109}
110
111async function renderBand($: any, e: any, beneath: unknown) {
112  const { Box, Text, Link, Button } = await $.ui.resolve(e)
113  // Only the terminal can run a command: `process` is CLI only.
114  const button = (key: string, label: string, href: string) =>
115    e.surface === 'terminal'
116      ? Button({ key, label, onPress: () => void openUrl($, href).catch(() => {}) })
117      : Text({ children: [Link({ href, children: [`[ ${label} ]`] })] })
118  const tz = (await $.env.get('TZ')) || Intl.DateTimeFormat().resolvedOptions().timeZone
119  const model = calModelOf(view, await $.clock.now(), tz)
120  const room = Math.max(1, (e.props.maxRows ?? 10) - (model.notice ? 1 : 0))
121  const hidden = model.rows.length > room ? model.rows.length - room + 1 : 0
122  const lines = []
123  if (model.notice) lines.push(Text({ dimColor: true, wrap: 'truncate-end', children: [model.notice] }))
124  for (const [i, row] of (hidden ? model.rows.slice(0, room - 1) : model.rows).entries()) {
125    const children = [
126      Text({ color: DAY_COLOR, children: [row.day] }),
127      `  ${row.span}  `,
128      row.isNext ? Text({ color: NEXT_COLOR, bold: true, children: [row.title] }) : row.title,
129    ]
130    if (row.location) children.push(Text({ dimColor: true, children: [`  @${row.location}`] }))
131    if (row.note) children.push(Text({ color: NEXT_COLOR, children: [`  ${row.note}`] }))
132    const buttons = [button(`calendar-${i}`, CALENDAR_BUTTON, row.calendarHref)]
133    if (row.urlHref) buttons.push(button(`url-${i}`, URL_BUTTON, row.urlHref))
134    lines.push(Box({ flexDirection: 'row', gap: 2, children: [Text({ wrap: 'truncate-end', children }), ...buttons] }))
135  }
136  if (hidden) lines.push(Text({ dimColor: true, children: [`… ${hidden} more`] }))
137  if (beneath) lines.push(beneath)
138  return Box({ flexDirection: 'column', children: lines })
139}
140
141export function register(on: On, options: { exclude_calendars?: string[] } = {}) {
142  excluded = Array.isArray(options.exclude_calendars) ? options.exclude_calendars : []
143
144  on('session.start', async ($, e, next) => {
145    try {
146      await $.command.register({
147        name: 'cal',
148        description: 'Show upcoming Google Calendar events above the prompt',
149        argumentHint: '[days]',
150        immediate: true,
151      })
152    } catch {
153      // A refused /cal leaves nothing else to run.
154    }
155    await readStore($)
156    if (bandOn) void fetchAndPublish($).catch(() => {})
157    // A reload re-fires session.start on this instance; a second timer would double the cadence.
158    poll?.cancel()
159    poll = $.clock.every(POLL_MS, () => {
160      if (bandOn) void fetchAndPublish($).catch(() => {})
161    })
162    tick?.cancel()
163    tick = $.clock.every(60_000, () => {
164      if (bandOn) $.ui.invalidate('ui.render')
165    })
166    return next(e)
167  })
168
169  on('command.run', { command: 'cal' }, async ($, e) => {
170    const args = (e.args ?? '').trim()
171    if (args === '') return showBand($, !bandOn)
172    const days = Number(args)
173    if (!/^\d+$/.test(args) || days < 1 || days > MAX_DAYS) return { text: `usage: /cal [days], days 1–${MAX_DAYS}` }
174    view = { ...view, days }
175    await remember($, DAYS_KEY, days)
176    return showBand($, true)
177  })
178
179  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
180    if (!bandOn || e.props.hasSurvey) return next(e)
181    // The band is one instance: a tree that drops next(e) hides every other plugin's band.
182    return renderBand($, e, await next(e))
183  })
184}
185
hooks/cal-model.ts 156 lines
1import type { CalEvent, CalTime } from './events.ts'
2
3export type CalView = { events: CalEvent[] | null; failure: string | null; lastGoodAt: number | null; days: number }
4
5// `day` and `span` arrive padded to their column; `note` is today's countdown.
6export type CalRow = {
7  day: string
8  span: string
9  title: string
10  location: string
11  note: string
12  isNext: boolean
13  calendarHref: string
14  urlHref: string | null
15}
16
17export type CalModel = { notice: string | null; rows: CalRow[] }
18
19const MINUTE = 60_000
20const HOUR = 60 * MINUTE
21const DAY_MS = 24 * HOUR
22const WEEKDAYS = ['日', '一', '二', '三', '四', '五', '六']
23const ALL_DAY = '全天'
24export const CALENDAR_BUTTON = '日曆'
25export const URL_BUTTON = '連結'
26
27type Local = { date: string; time: string }
28
29function localOf(ms: number, tz: string): Local {
30  const parts = new Intl.DateTimeFormat('en-CA', {
31    timeZone: tz,
32    year: 'numeric',
33    month: '2-digit',
34    day: '2-digit',
35    hour: '2-digit',
36    minute: '2-digit',
37    hourCycle: 'h23',
38  }).formatToParts(ms)
39  const part = (type: string) => parts.find((p) => p.type === type)?.value ?? ''
40  return { date: `${part('year')}-${part('month')}-${part('day')}`, time: `${part('hour')}:${part('minute')}` }
41}
42
43function monthDayOf(date: string): string {
44  return `${date.slice(5, 7)}/${date.slice(8, 10)}`
45}
46
47function dayLabelOf(date: string, today: string, tomorrow: string): string {
48  if (date === today) return '今天'
49  if (date === tomorrow) return '明天'
50  const [y, m, d] = date.split('-').map(Number)
51  return `${monthDayOf(date)} ${WEEKDAYS[new Date(Date.UTC(y!, m! - 1, d!)).getUTCDay()]}`
52}
53
54function startOf(time: CalTime, tz: string): Local {
55  return time.kind === 'date' ? { date: time.date, time: '' } : localOf(time.ms, tz)
56}
57
58function spanOf(event: CalEvent, start: Local, tz: string): string {
59  if (event.start.kind === 'date') return ALL_DAY
60  if (!event.end || event.end.kind === 'date') return start.time
61  const end = localOf(event.end.ms, tz)
62  return end.date === start.date ? `${start.time}–${end.time}` : `${start.time}–${monthDayOf(end.date)} ${end.time}`
63}
64
65// Terminal cells: a CJK character takes two.
66export function cellsOf(text: string): number {
67  let cells = 0
68  for (const ch of text) cells += /[ᄀ-ᅟ⺀-꓏가-힣豈-﫿︰-﹏＀-⦆¢-₩]/.test(ch) ? 2 : 1
69  return cells
70}
71
72function padCells(text: string, width: number): string {
73  return text + ' '.repeat(Math.max(0, width - cellsOf(text)))
74}
75
76// A Link refuses anything but an https URL spelled as the URL parser spells it.
77function httpsHref(text: string): string | null {
78  try {
79    const url = new URL(text)
80    return url.protocol === 'https:' ? url.href : null
81  } catch {
82    return null
83  }
84}
85
86function dayHrefOf(date: string): string {
87  const [y, m, d] = date.split('-').map(Number)
88  return `https://calendar.google.com/calendar/r/day/${y}/${m}/${d}`
89}
90
91function durationOf(ms: number): string {
92  const h = Math.floor(ms / HOUR)
93  const m = Math.floor((ms % HOUR) / MINUTE)
94  return h && m ? `${h}h ${m}m` : h ? `${h}h` : `${m}m`
95}
96
97function noteOf(event: CalEvent, start: Local, today: string, nowMs: number): string {
98  if (event.start.kind !== 'time' || start.date !== today) return ''
99  return event.start.ms <= nowMs ? '進行中' : `還有 ${durationOf(event.start.ms - nowMs)}`
100}
101
102export function lineOf(row: CalRow): string {
103  const buttons = [CALENDAR_BUTTON, row.urlHref && URL_BUTTON].filter(Boolean).map((label) => `[ ${label} ]`)
104  return [row.day, row.span, row.title, row.location && `@${row.location}`, row.note, ...buttons].filter(Boolean).join('  ')
105}
106
107function rowsOf(events: CalEvent[], nowMs: number, tz: string): CalRow[] {
108  const today = localOf(nowMs, tz).date
109  const tomorrow = localOf(nowMs + DAY_MS, tz).date
110  const seen = new Set<string>()
111  const lines = events
112    .map((event) => ({ event, start: startOf(event.start, tz) }))
113    .filter(({ event, start }) => {
114      const key = `${event.id}|${start.date}|${start.time}`
115      if (seen.has(key)) return false
116      seen.add(key)
117      return true
118    })
119    .sort((a, b) => `${a.start.date} ${a.start.time}`.localeCompare(`${b.start.date} ${b.start.time}`))
120    .map(({ event, start }) => ({
121      date: start.date,
122      day: dayLabelOf(start.date, today, tomorrow),
123      span: spanOf(event, start, tz),
124      event,
125      note: noteOf(event, start, today, nowMs),
126    }))
127  const dayWidth = Math.max(0, ...lines.map((l) => cellsOf(l.day)))
128  const spanWidth = Math.max(0, ...lines.map((l) => cellsOf(l.span)))
129  const next = lines.findIndex((l) => l.event.start.kind === 'time')
130  return lines.map(({ date, day, span, event, note }, i) => {
131    const urlHref = event.url ? httpsHref(event.url) : null
132    return {
133      day: padCells(day, dayWidth),
134      span: padCells(span, spanWidth),
135      title: event.title,
136      location: event.location === event.url && urlHref ? '' : event.location,
137      note,
138      isNext: i === next,
139      calendarHref: (event.link && httpsHref(event.link)) || dayHrefOf(date),
140      urlHref,
141    }
142  })
143}
144
145export function calModelOf(view: CalView, nowMs: number, tz: string): CalModel {
146  if (!view.events) {
147    return { notice: view.failure ? `Unavailable: ${view.failure}` : 'Fetching calendar events', rows: [] }
148  }
149  const stale = view.failure
150    ? `Stale: ${view.failure}; showing data from ${durationOf(nowMs - (view.lastGoodAt ?? nowMs))} ago`
151    : null
152  const rows = rowsOf(view.events, nowMs, tz)
153  const empty = rows.length ? null : `No events in the next ${view.days} days`
154  return { notice: stale ?? empty, rows }
155}
156
hooks/events.ts 84 lines
1import { oneLine } from './text.ts'
2
3export type CalTime = { kind: 'date'; date: string } | { kind: 'time'; ms: number }
4
5export type CalEvent = { id: string; title: string; location: string; link: string; url: string; start: CalTime; end: CalTime | null }
6
7export type Calendar = { id: string; name: string }
8
9function field(value: unknown, key: string): unknown {
10  return value !== null && typeof value === 'object' ? (value as Record<string, unknown>)[key] : undefined
11}
12
13function stringOf(value: unknown): string {
14  return typeof value === 'string' ? value : ''
15}
16
17// An MCP tool answers its JSON as the text of its content blocks.
18export function payloadOf(result: unknown): unknown {
19  if (field(result, 'isError') === true) return undefined
20  const blocks = field(result, 'content')
21  if (!Array.isArray(blocks)) return undefined
22  const text = blocks.map((b) => (field(b, 'type') === 'text' ? stringOf(field(b, 'text')) : '')).join('')
23  try {
24    return JSON.parse(text)
25  } catch {
26    return undefined
27  }
28}
29
30function timeOf(raw: unknown): CalTime | null {
31  const dateTime = stringOf(field(raw, 'dateTime'))
32  if (dateTime) {
33    const ms = Date.parse(dateTime)
34    return Number.isFinite(ms) ? { kind: 'time', ms } : null
35  }
36  // The connector spells an all-day date as midnight UTC (`2026-10-09T00:00:00Z`); the
37  // calendar day is the date part, which parsing it as a time would shift west of UTC.
38  const date = stringOf(field(raw, 'date'))
39  return /^\d{4}-\d{2}-\d{2}(T|$)/.test(date) ? { kind: 'date', date: date.slice(0, 10) } : null
40}
41
42// A meeting's join link, or a location that is itself a link.
43function urlOf(raw: unknown, location: string): string {
44  const entryPoints = field(field(raw, 'conferenceData'), 'entryPoints')
45  const video = Array.isArray(entryPoints) ? entryPoints.find((p) => field(p, 'entryPointType') === 'video') : undefined
46  return stringOf(field(raw, 'hangoutLink')) || stringOf(field(video, 'uri')) || (/^https:\/\//.test(location) ? location : '')
47}
48
49export function calendarsOf(payload: unknown): Calendar[] | null {
50  const list = field(payload, 'calendars')
51  if (!Array.isArray(list)) return null
52  return list.flatMap((c) => {
53    const id = stringOf(field(c, 'id'))
54    return id ? [{ id, name: oneLine(stringOf(field(c, 'summary'))) || oneLine(id) }] : []
55  })
56}
57
58// A calendar with no events in the window answers no `events` key at all.
59export function eventsOf(payload: unknown): CalEvent[] | null {
60  if (payload === null || typeof payload !== 'object') return null
61  const list = field(payload, 'events') ?? []
62  if (!Array.isArray(list)) return null
63  return list.flatMap((raw) => {
64    const start = timeOf(field(raw, 'start'))
65    if (!start || field(raw, 'status') === 'cancelled') return []
66    const location = oneLine(stringOf(field(raw, 'location')))
67    return [
68      {
69        id: stringOf(field(raw, 'id')),
70        title: oneLine(stringOf(field(raw, 'summary'))) || '(no title)',
71        location,
72        link: stringOf(field(raw, 'htmlLink')),
73        url: urlOf(raw, location),
74        start,
75        end: timeOf(field(raw, 'end')),
76      },
77    ]
78  })
79}
80
81export function wantedOf(calendars: Calendar[], excluded: string[]): Calendar[] {
82  return calendars.filter((c) => !excluded.includes(c.name) && !excluded.includes(c.id))
83}
84
hooks/text.ts 18 lines
1// Everything the engine would refuse to draw, except the tab and newline a pane lays out with.
2const CONTROL = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/g
3const LINE_MAX = 1000
4
5export function drawable(text: string, max = 10000): string {
6  const clean = text.replace(CONTROL, '')
7  if (clean.length <= max) return clean
8  // Copied from obsidian-workspace/hooks/bounds.ts: a cut must not leave a lone high surrogate before the ellipsis.
9  let end = max - 1
10  const last = clean.charCodeAt(end - 1)
11  if (last >= 0xd800 && last <= 0xdbff) end -= 1
12  return `${clean.slice(0, end)}…`
13}
14
15export function oneLine(text: string): string {
16  return drawable(text, LINE_MAX).replace(/\s*[\n\t]+\s*/g, ' ').trim()
17}
18