SLOPSHOPPER

convo-digest

Summarize your finished Claude Code conversations into a searchable recall index, surface relevant past work when you start something new, and see it all in…

panebandcommandtoaststatusmodel
v0.5.5no licenseupdated 2026-10-10pariidanDKE/convo-digest
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · convo-digest
│ ┃ Mission Control ✕ › fix the failing auth test and add an audit log call │ ┃ Mission Control │ ┃ Couldn't collect the data: JSON Parse error: ⏺ Read(src/auth.ts) │ ┃ Unexpected identifier "dev" ⎿ Read 6 lines │ ┃ [ Retry ] ⏺ 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 │ │ › /mission │ ⎿ convo-digest: Mission Control is open. │ │ ◉ Mission Control Summarize this chat ⟨Claude Code's own drawing⟩ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Band
◉ Mission Control Summarize this chat ⟨Claude Code's own drawing⟩
Pane · Mission Control
Mission Control Couldn't collect the data: JSON Parse error: Unexpected identifier "dev" [ Retry ]
README

convo-digest

A Claude Code plugin that summarizes your finished conversations into a searchable recall index, so you (and Claude) can find past work when you start something new — and triage what you're done with.

Runs entirely locally on your Claude Code subscription. No API key, no data leaves your machine.

What you get

Skills (namespaced under convo-digest):

SkillWhat it does
/convo-digest:digestSummarize conversations that changed since the last run into the recall index.
/convo-digest:recallFind a relevant past conversation for what you're starting on, and offer to resume it.
/convo-digest:digest-archiveReview recent conversations and archive the ones you're done with so they stop cluttering recall.
/convo-digest:setup-nightlySchedule the digest to run itself overnight (macOS, Linux, Windows).
/convo-digest:profile-reposTag repos work/personal so recall can label and rank results.

Plus a SessionStart nudge: when finished conversations aren't indexed yet, it offers to refresh (once per conversation). Because a SessionStart hook can only reach you through the model relaying it, the offer is self-healing — it keeps re-surfacing each new conversation until the backlog is actually cleared or you opt out ("not today" or off for good), so a silently-dropped offer isn't lost for the day.

Set it and forget it

If you'd rather never be asked, let the digest run itself overnight:

/convo-digest:setup-nightly

You pick one of two mechanisms:

  • OS scheduler (default) — launchd on macOS, a systemd user timer (or cron) on Linux, Task Scheduler on Windows — all firing the same generated launcher at ~/.claude/digest/run-nightly.*, which you can also run by hand to debug a bad night. Fires even when the app is closed (macOS/systemd catch up on wake), and is the only option on CLI-only / headless / server installs. On-plan: no API key, no impact on your interactive session usage.
  • Claude Desktop task — runs inside the desktop app, so it uses the app's login, always the current plugin version, and leaves a run history in the app sidebar. Only fires while the app is open, and draws your interactive subscription usage like any session. Desktop app only. Schedule it for a time your machine is awake (e.g. 09:20): runs that started while the laptop was asleep, or caught up right after it woke, account for nearly every failed night — the machine slept mid-run, or the login had gone stale.

Once it's set up the daily nudge goes quiet, because the schedule owns the drain. A few things still get through, so automation can't fail silently: if the last run failed or never finished, if no run has succeeded in 36 hours, if the backlog grows past 25 (OS scheduler), or if the scheduled job disappears, the hook tells you the automation is broken instead of saying nothing.

Scheduled-task sessions themselves (the nightly run, and any other scheduled task such as a standup brief) are kept out of the index and never renamed.

Manage it directly with:

python3 <plugin>/src/install_schedule.py --status      # installed? which mechanism?
python3 <plugin>/src/install_schedule.py --start       # run it right now
python3 <plugin>/src/install_schedule.py --time 02:00  # re-schedule
python3 <plugin>/src/install_schedule.py --uninstall   # remove it
python3 <plugin>/src/ledger.py show                    # recent runs + logged issues

Run record and issue log

Every digest run — scheduled or manual — records its start and end in ~/.claude/digest/ledger.jsonl, and logs anything that went wrong along the way (timeouts, failed summarizers, refused tools, retries) as an issue in the same append-only file. Review it with ledger.py show [--days N]. A lock in the same directory keeps two digests from draining at once.

Unattended runs follow strict rules: they never act on setup nudges, never change code, config or memory, retry a failed step at most once, and log every problem instead of improvising a fix.

macOS note: launchd agents have no Full Disk Access, so the installer refuses to schedule a plugin living under Documents, Desktop or Downloads — the job would die with a bare EPERM every night. Keep the plugin somewhere like ~/convo-digest.

Linux note: without sudo loginctl enable-linger $USER, a user timer only runs while you're logged in.

Requirements

  • Claude Code (with plugins enabled).
  • python3 ≥ 3.10 on your PATH. That's the only hard dependency — the engine is pure Python and falls back to a dependency-free token estimator.
  • (Optional) pip install tiktoken for sharper token counts. Not required; without it, a conservative character heuristic is used.

Install

/plugin marketplace add pariidanDKE/convo-digest
/plugin install convo-digest@pariidan-plugins

Then start a new session (the SessionStart hook installs the summarization workflow into ~/.claude/workflows/ on first run). After that, just say "refresh the digest" or run /convo-digest:digest.

Titles in the desktop app

With title writeback on, each conversation gets the digest's title in two places:

  • the transcript's custom-title record — the claude --resume picker;
  • the desktop app's own session title — the sidebar and the app's search box.

App titles are set through the app's own rename tool after each run, and every indexed conversation is re-checked each time, so a title the app reverts is put back on the next run. Only titles the app generated (or the digest set earlier) are replaced; a name you gave a session yourself is never touched.

Know the app search's limits: it matches titles only across your 50 most recently active sessions, and its text search scans message content (not titles) across roughly the 200 most recent. For anything older, use /convo-digest:recall.

Mission Control

A pane over the same index: type /mission, or click the ◉ Mission Control band above the prompt.

  • The day's standup brief up top, folded to its first bullets.
  • An Ask bar that answers from your summaries and links the conversations it cites.
  • A timeline of your conversations, grouped by workstream, project, status, kind of work or day. Filter it by workstream and kind; pick today, yesterday, the last 7 days or your own dates.
  • Per conversation: its gist, a read-only reader with the messages, and buttons to open it, summarize it again, move it to another project, pin it to the sidebar or archive it.
  • Your routines and the digest's health at the bottom.

/mission home makes the chat you type it in Mission Control's home: it opens the pane by itself, and the band in every other chat takes you there. On macOS, tools/raise/build.sh builds a small helper app that brings that chat's own window to the front (it needs Accessibility permission); without it the band says so.

The flow

flowchart TD
    N["🔔 SessionStart nudge<br/><i>“N finished convos aren't indexed — refresh?”</i>"]
    T[("📄 Local transcripts<br/>~/.claude/projects/…/*.jsonl")]
    P["<b>Prepare</b> · no model calls<br/>detect changed convos, strip to text,<br/>tier: whole / over-cap / trivial"]
    S["<b>Summarize</b> · background workflow<br/>one small Haiku agent per convo<br/>→ record<br/>(title · topics · gist · status · unresolved · entities · workstream · kind)"]
    I[("🗂 Recall index<br/>~/.claude/digest/index.json")]
    R["🔍 <b>/recall</b> — pull<br/>search past work,<br/>offer to resume it"]
    A["🧹 <b>/digest-archive</b> — push<br/>morning triage: archive<br/>what you're done with"]

    N -. offers /digest .-> P
    T --> P --> S --> I
    I --> R
    I --> A

Everything runs locally, incrementally, and checkpointed — a digest run only touches conversations that changed since the last one.

How it works

  • Reads your local Claude Code transcripts (~/.claude/projects/**/*.jsonl), strips each to user+assistant text, and summarizes changed ones into a compact record (title, topics, gist, status, unresolved, key entities, workstream, kind) stored in ~/.claude/digest/index.json.
  • Every conversation is filed under a workstream: an epic-level goal that spans several tickets and conversations ("Atlas AI tool access"). The list lives in ~/.claude/digest/workstreams.json and grows as new epics appear; the summarizer reuses an existing name when one fits, and a small curator pass merges a new name into an existing one when they are the same epic. Each conversation also gets a kind: build, fix, review, investigate, plan or admin. Conversations summarized before these existed are tagged from their stored summary, a few batches per run.
  • Large conversations are downsampled to a tail-weighted view and expanded on demand under a token budget, so no single summary blows the model's context.
  • Summarization runs as a background workflow orchestrating small Read-only agents on the Haiku model — your interactive session stays free.
  • Everything is incremental and checkpointed: only changed conversations are re-summarized, and the change-detector only advances after a summary is written.

Privacy

All processing is local. The plugin never sends your conversations anywhere; it only reads local transcript files and writes a local index under ~/.claude/digest/.

Source 5 files
hooks/mission-control/register.tsx 1343 lines
1// Mission Control: one pane over all your Claude Code work.
2//
3//   /mission [today|yesterday|week]   open the pane (optionally on a range)
4//   /mission ask <question>           open it and ask the agent straight away
5//   /mission home [off]               this session opens the pane by itself
6//   /mission band on|off              the one-line way in above every prompt
7//
8// Top to bottom: the day's standup brief (folded, unfolds in full), an Ask box
9// answered from the digest index, a timeline of your conversations grouped by workstream
10// (click one for its gist and a way to open it), then the routines (scheduled tasks)
11// and the digest's run health. A status-line entry keeps the routines' state visible
12// with the pane closed.
13//
14// Data comes from src/mission_control.py (read-only over the transcripts, the digest
15// index, the app's session files and the digest run ledger).
16import { atom, read, update } from 'claude-code'
17import type { Elements, EngineInterface, Register, RenderElement, RenderInput } from 'claude-code'
18
19import type {
20  McAsk,
21  McColorMode,
22  McKind,
23  McRangeKey,
24  McReader,
25  McRef,
26  McRoutine,
27  McRowsMode,
28  McSession,
29  McSnapshot,
30  McStatus,
31  McTask,
32  McView,
33} from '../../types'
34import {
35  duration,
36  hm,
37  rowsOptions,
38  routineName,
39  runDot,
40  statusColor,
41  statusLabel,
42  timeline,
43  visible as inView,
44  workedMinutes,
45  timelineStrips,
46  truncate,
47  when,
48} from './layout'
49
50const PLUGIN = 'convo-digest'
51const PANE = 'mission-control'
52const TITLE = 'Mission Control'
53const ASK_MODEL = 'sonnet'
54const VIEW_VERSION = 3                // bump to move everyone onto a new default view
55const PX_PER_CELL = 8                 // the desktop's pane width per text cell, about
56const WIDE = 140                      // a pane this many cells wide reads beside the timeline
57const READ_LAST = 40                  // turns the reader shows; "Show earlier" adds as many
58const READ_POLL_MS = 4000             // how often an open reader looks for new messages
59
60const snapAtom = atom({ plugin: 'convo-digest', key: 'snap' } as const, null)
61const viewAtom = atom({ plugin: 'convo-digest', key: 'view' } as const,
62  { range: 'today', rows: 'workstream', color: 'project' } as McView)
63const selectedAtom = atom({ plugin: 'convo-digest', key: 'selected' } as const, null)
64const askAtom = atom({ plugin: 'convo-digest', key: 'ask' } as const,
65  { question: '', busy: false, answer: null, refs: {}, error: null } as McAsk)
66const statusAtom = atom({ plugin: 'convo-digest', key: 'status' } as const,
67  { loading: false, error: null, tasks: null } as McStatus)
68const bandAtom = atom({ plugin: 'convo-digest', key: 'band' } as const, true)
69const standupOpenAtom = atom({ plugin: 'convo-digest', key: 'standupOpen' } as const, false)
70const summarizingAtom = atom({ plugin: 'convo-digest', key: 'summarizing' } as const, null as number | null)
71const collapsedAtom = atom({ plugin: 'convo-digest', key: 'collapsed' } as const, [] as string[])
72const actingAtom = atom({ plugin: 'convo-digest', key: 'acting' } as const, null as string | null)
73const readerAtom = atom({ plugin: 'convo-digest', key: 'reader' } as const, null as McReader | null)
74const resummarizingAtom = atom({ plugin: 'convo-digest', key: 'resummarizing' } as const, null as string | null)
75
76/** Fold or unfold one timeline group, by its heading. */
77async function toggleGroup($: $T, label: string): Promise<void> {
78  await update($, collapsedAtom, list => (list.includes(label) ? list.filter(l => l !== label) : [...list, label]))
79}
80const DIGEST = { command: 'convo-digest:digest', args: 'now' } as const
81const SESSION_ACTION = 'convo-digest:session'       // this plugin's skill: the app's archive and pin tools
82
83type $T = EngineInterface
84/** What the Pane render hook receives. */
85type PaneRender = RenderInput<'Pane'>
86type El = Pick<Elements['desktop'], 'Box' | 'Text' | 'Button' | 'Select' | 'Input' | 'Markdown' | 'Client'>
87
88const errText = (err: unknown) => (err instanceof Error ? err.message : String(err))
89
90/** Best-effort trace at ~/.claude/mission-control/mod-status.json: when the module
91 *  started, the last refresh and its error, so a pane that stays empty can be diagnosed. */
92async function breadcrumb($: $T, patch: Record<string, unknown>): Promise<void> {
93  try {
94    const home = await $.env.get('HOME')
95    if (!home) return
96    const path = `${home}/.claude/mission-control/mod-status.json`
97    let cur: Record<string, unknown> = {}
98    try {
99      cur = JSON.parse(String(await $.fs.read(path))) as Record<string, unknown>
100    } catch {
101      cur = {}
102    }
103    await $.fs.write(path, JSON.stringify({ ...cur, ...patch }, null, 1))
104  } catch {
105    // tracing must never break the pane
106  }
107}
108
109// ------------------------------------------------------------------ data
110async function collect($: $T, args: string[]): Promise<unknown> {
111  const script = `${$.plugin.root}/src/mission_control.py`
112  const r = await $.process.run(['python3', script, ...args], { timeoutMs: 120_000 })
113  if (r.exitCode !== 0) {
114    const last = r.stderr.trim().split('\n').pop()
115    throw new Error(last || `collect.py exited ${r.exitCode}`)
116  }
117  return JSON.parse(r.stdout)
118}
119
120async function loadTasks($: $T): Promise<McTask[] | null> {
121  try {
122    const res = await $.mcp.call('scheduled-tasks', 'list_scheduled_tasks', {})
123    if (res.isError) return null
124    const block = res.content.find(b => b.type === 'text') as { text?: string } | undefined
125    const list = JSON.parse(block?.text ?? '[]') as Record<string, unknown>[]
126    return list.map(t => ({
127      taskId: String(t.taskId),
128      enabled: t.enabled !== false,
129      schedule: typeof t.schedule === 'string' ? t.schedule : null,
130      nextRunAt: typeof t.nextRunAt === 'string' ? t.nextRunAt : null,
131    }))
132  } catch {
133    return null                       // the app's task server isn't reachable from here
134  }
135}
136
137const MARK: Record<string, string> = { ok: '✓', nothing: '✓', warn: '!', failed: '✗', running: '…' }
138
139/** The status line says only what needs attention (a routine whose last run failed or
140 *  warned, digest issues since its last good run) and is empty otherwise. */
141function statusLine($: $T, snap: McSnapshot): void {
142  const parts: string[] = []
143  for (const r of snap.routines.filter(x => !x.oneTime)) {
144    const run = r.runs[0]
145    if (run && (run.status === 'failed' || run.status === 'warn')) {
146      parts.push(`${routineName(r).toLowerCase()} ${MARK[run.status]}`)
147    }
148  }
149  if (snap.digest.issuesSinceOk) parts.push(`${snap.digest.issuesSinceOk} digest issue(s)`)
150  $.ui.status(parts.length ? `◉ MC · ${parts.join(' · ')}` : undefined)
151}
152
153/** A local calendar day `back` days before `now`, as YYYY-MM-DD and as a label. */
154function dayAt(now: number, tzOffsetMin: number, back: number): { value: string; label: string } {
155  const d = new Date(now + tzOffsetMin * 60_000 - back * 86_400_000)
156  const value = d.toISOString().slice(0, 10)
157  const label = back === 0 ? 'Today' : back === 1 ? 'Yesterday'
158    : `${['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][d.getUTCDay()]} ${d.getUTCDate()} ` +
159      `${['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][d.getUTCMonth()]}` +
160      (back > 300 ? ` ${d.getUTCFullYear()}` : '')
161  return { value, label }
162}
163
164const CUSTOM_DAYS = 60                  // how far back the pickers reach (a Select holds 64 at most)
165const MAX_SPAN = 31                     // the collector's MAX_DAYS
166
167/** The collector's --range for a view: a custom range is its two days. */
168function rangeArg(view: McView): string {
169  return view.range === 'custom' && view.from && view.to ? `${view.from}..${view.to}` : view.range === 'custom'
170    ? '7d' : view.range
171}
172
173/** Whether the view shows a single day (where one row per day means nothing). */
174function oneDay(view: McView): boolean {
175  return view.range === 'today' || view.range === 'yesterday' ||
176    (view.range === 'custom' && !!view.from && view.from === view.to)
177}
178
179async function refresh($: $T): Promise<void> {
180  const key = rangeArg(await read($, viewAtom))
181  await update($, statusAtom, s => ({ ...s, loading: true }))
182  try {
183    const [snap, tasks] = await Promise.all([
184      collect($, ['snapshot', '--range', key]) as Promise<McSnapshot>,
185      loadTasks($),
186    ])
187    await update($, snapAtom, () => snap)
188    await update($, statusAtom, s => ({ ...s, loading: false, error: null, tasks: tasks ?? s.tasks }))
189    statusLine($, snap)
190    await breadcrumb($, { lastRefreshAt: snap.generatedAt, range: key, tookMs: snap.tookMs,
191      tasksReachable: tasks !== null, lastError: null })
192  } catch (err) {
193    await update($, statusAtom, s => ({ ...s, loading: false, error: errText(err) }))
194    await breadcrumb($, { lastErrorAt: await $.clock.now(), lastError: errText(err) })
195  }
196}
197
198async function setView($: $T, patch: Partial<McView>): Promise<void> {
199  const before = await read($, viewAtom)
200  const next = { ...before, ...patch }
201  if (next.range === 'custom') {
202    // From after To swaps; a span past what the collector draws keeps its end
203    if (next.from && next.to && next.from > next.to) [next.from, next.to] = [next.to, next.from]
204    if (next.from && next.to) {
205      const span = (Date.parse(next.to) - Date.parse(next.from)) / 86_400_000 + 1
206      if (span > MAX_SPAN) {
207        const keep = patch.from ? 'from' : 'to'
208        const shift = (MAX_SPAN - 1) * 86_400_000
209        if (keep === 'from') next.to = new Date(Date.parse(next.from) + shift).toISOString().slice(0, 10)
210        else next.from = new Date(Date.parse(next.to) - shift).toISOString().slice(0, 10)
211      }
212    }
213  }
214  if (oneDay(next) && next.rows === 'day') next.rows = 'project'
215  await update($, viewAtom, () => next)
216  await $.store.set('view', { ...next, version: VIEW_VERSION })
217  if (rangeArg(next) !== rangeArg(before)) {
218    await update($, selectedAtom, () => null)
219    await refresh($)
220  }
221}
222
223function findSession(snap: McSnapshot | null, ask: McAsk, id: string): { app: string | null; title: string } | null {
224  const s = snap?.sessions.find(x => x.id === id)
225  if (s) return { app: s.app, title: s.title }
226  const ref = Object.values(ask.refs).find(r => r.id === id)
227  return ref ? { app: ref.app, title: ref.title } : null
228}
229
230async function openSession($: $T, id: string, surface?: Parameters<$T['ui']['copy']>[0]['surface']): Promise<void> {
231  const found = findSession(await read($, snapAtom), await read($, askAtom), id)
232  if (!found?.app) {
233    await $.ui.copy({ text: `claude --resume ${id}`, surface })
234    $.ui.toast('No desktop session for this one — copied `claude --resume` to the clipboard')
235    return
236  }
237  const r = await $.process.run(['open', `claude://claude.ai/epitaxy/${found.app}`])
238  if (r.exitCode !== 0) $.ui.toast(`Couldn't open the session: ${r.stderr.trim() || r.exitCode}`)
239}
240
241// ------------------------------------------------------------------ reader
242/**
243 * Open a conversation in the reader: beside the timeline in a wide pane, in place of
244 * the overview in a narrow one. It reads the transcript, read-only, and reads it
245 * again every few seconds while it's open, so a running conversation shows its replies.
246 */
247async function openReader($: $T, id: string, wide: boolean): Promise<void> {
248  const cur = await read($, readerAtom)
249  if (cur?.id === id) return closeReader($)
250  await update($, readerAtom, () => ({ id, last: READ_LAST, loading: true, expanded: false }))
251  await update($, selectedAtom, () => id)
252  await loadReader($, true)
253  await $.ui.scroll(wide ? { to: { key: 'reader' }, in: PANE, block: 'nearest' } : { to: 'start', in: PANE })
254    .catch(() => undefined)
255}
256
257async function closeReader($: $T): Promise<void> {
258  const cur = await read($, readerAtom)
259  await update($, readerAtom, () => null)
260  // back where the conversation was clicked
261  if (cur) await $.ui.scroll({ to: { key: `go-${cur.id}` }, in: PANE, block: 'center' }).catch(() => undefined)
262}
263
264/** Read the open conversation; unless `force`, only when its file has changed. */
265async function loadReader($: $T, force: boolean): Promise<void> {
266  const cur = await read($, readerAtom)
267  if (!cur) return
268  const args = ['transcript', '--id', cur.id, '--last', String(cur.last)]
269  if (!force && cur.mtime !== undefined) args.push('--if-newer', String(cur.mtime))
270  try {
271    const got = await collect($, args) as Omit<McReader, 'last' | 'loading'> & { unchanged?: boolean }
272    await update($, readerAtom, r => {
273      if (!r || r.id !== cur.id) return r                  // closed or switched meanwhile
274      if (got.unchanged) return { ...r, loading: false }
275      return { ...got, id: r.id, last: r.last, loading: false, expanded: r.expanded }
276    })
277  } catch (err) {
278    await update($, readerAtom, r => (r && r.id === cur.id ? { ...r, loading: false, error: errText(err) } : r))
279  }
280}
281
282async function readEarlier($: $T): Promise<void> {
283  await update($, readerAtom, r => (r ? { ...r, last: r.last + READ_LAST, loading: true } : r))
284  await loadReader($, true)
285}
286
287/** The reader: what the conversation was about, the way in, then its messages. */
288function Reader(el: El, $: $T, r: McReader, s: McSession | null, snap: McSnapshot, now: number, self: string,
289  busy: string | null) {
290  const { Box, Text, Button, Markdown } = el
291  const tz = snap.tzOffsetMin
292  const color = s ? statusColor(s.scheduled ? 'routine' : s.status) : '#30363d'
293  const title = s?.title ?? r.id.slice(0, 8)
294  const live = s ? now - s.last < 5 * 60_000 : false
295  const turns = r.turns ?? []
296  return (
297    <Box key="reader" flexDirection="column" borderStyle="round" borderColor={color} paddingX={1}>
298      <Box flexDirection="row" justifyContent="space-between" gap={2}>
299        <Text bold wrap="wrap">{title}</Text>
300        <Box flexDirection="row" gap={2}>
301          <Button key="reader-open" label={!s || s.app ? 'Open ↗' : 'Copy resume command'} variant="primary"
302            onPress={e => { void openSession($, r.id, e.surface) }} />
303          <Button key="reader-close" label="Close ✕" plain dimColor onPress={() => { void closeReader($) }} />
304        </Box>
305      </Box>
306      {s
307        ? (
308          <Text dimColor wrap="wrap">
309            {`${s.project}${s.branch ? ` · ${s.branch}` : ''} · ${when(s.first, snap, now)}–${hm(s.last, tz)} · ` +
310              `${duration(s.activeMin)} active${s.tickets.length ? ` · ${s.tickets.join(', ')}` : ''}` +
311              (s.archived ? ' · archived' : '')}
312            {live ? <Text color="#3fb950">{'  ● live'}</Text> : ''}
313          </Text>
314        )
315        : null}
316      {s?.gist ? <Markdown text={s.gist} /> : null}
317      {s?.open ? <Text dimColor wrap="wrap">{`Left for later: ${s.open}`}</Text> : null}
318      {s && !s.scheduled
319        ? (
320          <Box flexDirection="row" gap={2} marginTop={1}>
321            {SummarizeButton(el, $, s, `summarize-r-${s.id}`, self, busy)}
322            {PinButton(el, $, s, `pin-r-${s.id}`)}
323            {ArchiveButton(el, $, s, `archive-r-${s.id}`, self)}
324            {MoveTo(el, $, s, snap)}
325          </Box>
326        )
327        : null}
328      <Box flexDirection="row" gap={2} marginTop={1} alignItems="center">
329        <Button key="reader-toggle" plain
330          label={`${r.expanded ? '▾' : '▸'} Conversation · ${r.loading && r.total === undefined ? '…'
331            : `${r.total ?? 0} message${r.total === 1 ? '' : 's'}`}`}
332          onPress={() => { void update($, readerAtom, x => (x ? { ...x, expanded: !x.expanded } : x)) }} />
333        {r.expanded
334          ? <Text dimColor>{`${r.hidden ? `last ${turns.length} shown · ` : ''}read-only, updates while open`}</Text>
335          : null}
336      </Box>
337      {r.error ? <Text color="#f85149" wrap="wrap">{r.error}</Text> : null}
338      {r.expanded && r.hidden
339        ? <Button key="reader-earlier" label={`↑ Show earlier messages (${r.hidden} more)`} plain dimColor
340            onPress={() => { void readEarlier($) }} />
341        : null}
342      {(r.expanded ? turns : []).map((t, i) => t.role === 'note'
343        ? <Text key={`t-${i}`} dimColor italic>{`— ${t.text} —`}</Text>
344        : (
345          <Box key={`t-${i}`} flexDirection="column" marginTop={1}
346            {...(t.role === 'user' ? { backgroundColor: '#262626', paddingX: 1 } : {})}>
347            <Text bold color={t.role === 'user' ? '#58a6ff' : '#d2a8ff'}>
348              {t.role === 'user' ? 'You' : 'Claude'}
349              <Text dimColor>{t.ts ? `  ${when(t.ts, snap, now)}` : ''}</Text>
350            </Text>
351            {t.text ? <Markdown text={t.text} /> : null}
352            {t.tools.length ? <Text dimColor wrap="wrap">{`⚙ ${t.tools.join(' · ')}`}</Text> : null}
353          </Box>
354        ))}
355      {r.expanded && turns.length > 3
356        ? (
357          <Box flexDirection="row" gap={2} marginTop={1}>
358            <Button key="reader-open-2" label={!s || s.app ? 'Open ↗' : 'Copy resume command'} plain
359              onPress={e => { void openSession($, r.id, e.surface) }} />
360            <Button key="reader-collapse" label="▴ Fold the conversation" plain dimColor
361              onPress={() => { void update($, readerAtom, x => (x ? { ...x, expanded: false } : x)) }} />
362            <Button key="reader-close-2" label="Close ✕" plain dimColor onPress={() => { void closeReader($) }} />
363          </Box>
364        )
365        : null}
366    </Box>
367  )
368}
369
370/** Drop the answer and empty the field, ready for the next question. */
371async function clearAsk($: $T): Promise<void> {
372  await update($, askAtom, a => a.busy ? a : { ...a, question: '', answer: null, refs: {}, error: null })
373}
374
375async function runAsk($: $T, question: string): Promise<void> {
376  const q = question.trim()
377  if (!q) return
378  await update($, askAtom, a => ({ ...a, question: q, busy: true, error: null }))
379  try {
380    const view = await read($, viewAtom)
381    const ctx = await collect($, ['ask', '--question', q, '--range', rangeArg(view)]) as {
382      system: string; prompt: string; refs: Record<string, McRef>
383    }
384    const res = await $.model.complete({
385      model: ASK_MODEL, system: ctx.system, prompt: ctx.prompt, maxTokens: 900, timeoutMs: 120_000,
386    })
387    if (!res.isAnswered) throw new Error(`the model gave no answer (${res.reason})`)
388    await update($, askAtom, a => ({ ...a, busy: false, answer: res.text, refs: ctx.refs }))
389  } catch (err) {
390    await update($, askAtom, a => ({ ...a, busy: false, error: errText(err) }))
391  }
392}
393
394async function runNow($: $T, r: McRoutine): Promise<void> {
395  try {
396    const res = await $.mcp.call('scheduled-tasks', 'run_scheduled_task', { taskId: r.task })
397    const text = res.content.find(b => b.type === 'text') as { text?: string } | undefined
398    $.ui.toast(res.isError ? `Couldn't start ${routineName(r)}: ${text?.text ?? 'error'}` : `Started ${routineName(r)}`)
399    if (!res.isError) setTimeout30($)
400  } catch (err) {
401    $.ui.toast(`Couldn't reach the app's scheduled tasks: ${errText(err)}`)
402  }
403}
404
405function setTimeout30($: $T): void {
406  $.clock.after(30_000, () => { void refresh($) })
407}
408
409// ------------------------------------------------------------------ drawing
410function cites(answer: string, refs: Record<string, McRef>): McRef[] {
411  const seen = new Set<string>()
412  const out: McRef[] = []
413  for (const m of answer.matchAll(/\[(\d+)\]/g)) {
414    const ref = m[1] ? refs[m[1]] : undefined
415    if (ref && !seen.has(ref.id)) {
416      seen.add(ref.id)
417      out.push(ref)
418    }
419  }
420  return out.slice(0, 8)
421}
422
423/** True when a run ended badly enough to call out. */
424const isBad = (status: string | undefined) => status === 'failed' || status === 'warn'
425
426function Routines(el: El, $: $T, snap: McSnapshot, tasks: McTask[] | null, now: number) {
427  const { Box, Text, Button } = el
428  // one-off tasks only earn a line when their run went wrong
429  const list = snap.routines.filter(r => !r.oneTime || isBad(r.runs[0]?.status))
430  if (!list.length) return <Text dimColor>No scheduled tasks have run in the last two weeks.</Text>
431  return (
432    <Box flexDirection="column">
433      {list.map(r => {
434        const last = r.runs[0]
435        const dot = runDot(last?.status)
436        const history = [...r.runs].slice(0, 7).reverse()
437        const task = tasks?.find(t => t.taskId === r.task)
438        const next = task?.nextRunAt ? Date.parse(task.nextRunAt) : NaN
439        const lastText = last?.start ? `${when(last.start, snap, now)} · ${dot.word}` : 'no runs yet'
440        return (
441          <Box key={`routine-${r.task}`} flexDirection="column">
442            <Box flexDirection="row" gap={1} alignItems="center">
443              <Text color={dot.color}>●</Text>
444              <Box width={22}><Text bold wrap="truncate-end">{routineName(r)}</Text></Box>
445              <Box width={30}>
446                <Text dimColor wrap="truncate-end">
447                  {`${lastText}${Number.isFinite(next) ? ` · next ${when(next, snap, now)}` : ''}` +
448                    `${task && !task.enabled ? ' · paused' : ''}`}
449                </Text>
450              </Box>
451              <Text>{history.map(x => <Text color={runDot(x.status).color}>{runDot(x.status).glyph}</Text>)}</Text>
452              {last?.app
453                ? <Button key={`open-run-${r.task}`} label="Open" plain dimColor
454                    onPress={() => { void openSessionByApp($, last.app as string) }} />
455                : null}
456              {!r.oneTime && tasks !== null
457                ? <Button key={`run-${r.task}`} label="Run now" plain dimColor
458                    onPress={() => { void runNow($, r) }} />
459                : null}
460            </Box>
461            {isBad(last?.status) && last?.detail
462              ? <Text color={dot.color} wrap="truncate-end">{`    ${truncate(last.detail, 160)}`}</Text>
463              : null}
464          </Box>
465        )
466      })}
467    </Box>
468  )
469}
470
471/** Conversations the digest hasn't summarized yet, or that moved on since it did —
472 *  less this session, which the digest never summarizes while it is being written. */
473function unsummarized(snap: McSnapshot, selfId: string): McSession[] {
474  return snap.sessions.filter(s => !s.scheduled && s.id !== selfId && (!s.digested || s.changedSinceDigest))
475}
476
477/** Summarize now: run `/convo-digest:digest now` in this session, as if typed. The skill
478 *  does the rest — run log, lock, title sync — and `turn.complete` refreshes the pane
479 *  when it is done. (A mod may not type a slash command into the prompt; it runs it.) */
480async function summarizeNow($: $T): Promise<void> {
481  if (await read($, summarizingAtom)) return              // one at a time
482  const startedAt = await $.clock.now()
483  await update($, summarizingAtom, () => startedAt)
484  // not awaited: the command is queued until the session is idle and runs a long turn
485  $.command.run({ command: DIGEST.command, args: DIGEST.args }).catch(async err => {
486    await update($, summarizingAtom, () => null)
487    $.ui.toast(`Couldn't start the digest: ${errText(err)}`)
488  })
489}
490
491/** Whether a drawn tree shows nothing: what the band's chain gives back when no other
492 *  plugin draws there. */
493function drawsNothing(el: RenderElement | null | undefined): boolean {
494  if (!el) return true
495  if (el.type !== 'Box' && el.type !== 'Text') return false
496  return (el.children ?? []).every(k =>
497    typeof k === 'string' ? k.trim() === '' : drawsNothing(k as RenderElement))
498}
499
500// ------------------------------------------------------------------ home session
501// Which chat is Mission Control's home lives in a file, not $.store: every copy of the
502// plugin (the installed one, a development one) keeps a store of its own, and the band
503// in every chat has to agree on where home is.
504type Home = { cli: string; app: string | null }
505
506async function homeFile($: $T): Promise<string | null> {
507  const home = await $.env.get('HOME').catch(() => undefined)
508  return home ? `${home}/.claude/mission-control/home.json` : null
509}
510
511async function readHome($: $T): Promise<Home | null> {
512  const path = await homeFile($)
513  if (!path) return null
514  try {
515    const h = JSON.parse(String(await $.fs.read(path))) as Partial<Home>
516    return typeof h.cli === 'string' ? { cli: h.cli, app: typeof h.app === 'string' ? h.app : null } : null
517  } catch {
518    return null
519  }
520}
521
522/** Make this chat the home, with its desktop id so the band elsewhere can link to it. */
523async function writeHome($: $T): Promise<void> {
524  const path = await homeFile($)
525  if (!path) return
526  const raw = await $.env.get('CLAUDE_CODE_HOST_SESSION_ID').catch(() => undefined)
527  const app = typeof raw === 'string' && /^local_[\w-]+$/.test(raw) ? raw : null
528  await $.fs.write(path, JSON.stringify({ cli: await $.session.id(), app }, null, 1))
529}
530
531/**
532 * Go to the home chat's own window. The app's link can only open a chat in the main window,
533 * so the band opens ~/Applications/Mission Control Raise.app, a tiny AppleScript app with
534 * Accessibility permission that switches to the other Claude window through the Window menu,
535 * and reports when it can't. No fallback: without it the band says so.
536 */
537async function goHome($: $T): Promise<void> {
538  const home = await $.env.get('HOME').catch(() => undefined)
539  const raise = home ? `${home}/Applications/Mission Control Raise.app` : null
540  if (!raise || !(await $.fs.exists(raise).catch(() => false))) {
541    $.ui.toast('Mission Control Raise.app is not in ~/Applications, so the band cannot switch windows')
542    return
543  }
544  const r = await $.process.run(['open', '-a', raise])
545  if (r.exitCode !== 0) $.ui.toast(`Couldn't start Mission Control Raise: ${r.stderr.trim() || r.exitCode}`)
546}
547
548async function clearHome($: $T): Promise<void> {
549  const path = await homeFile($)
550  if (path) await $.fs.write(path, '{}')
551}
552
553/**
554 * Open the pane and fill it: what the command, the band and the home session share.
555 * The desktop asks for a wide pane (room to read a conversation beside the timeline);
556 * the home session, which is there only for this, for all the room the app will give.
557 * Both are requests: a width the person dragged the pane to wins, and is kept.
558 */
559async function openPane($: $T, isHome = false): Promise<boolean> {
560  const desktop = (await $.session.surface().catch(() => null)) === 'desktop'
561  const columns = desktop ? (isHome ? 1000 : 240) : undefined
562  const opened = await $.ui.open({ id: PANE, title: TITLE, ...(columns ? { columns } : {}) })
563  await refresh($)
564  return opened.isPlaced
565}
566
567/**
568 * Archive a conversation or bring it back, pin it or unpin it. Only the app's own session
569 * and sidebar tools can, and a plugin can't reach the app's tools, so this hands the
570 * session a short turn that calls them; the pane refreshes when that turn ends.
571 */
572type SessionVerb = 'archive' | 'unarchive' | 'pin' | 'unpin'
573const DOING: Record<SessionVerb, string> = { archive: 'Archiving', unarchive: 'Unarchiving', pin: 'Pinning',
574  unpin: 'Unpinning' }
575
576async function sessionAction($: $T, s: McSession, verb: SessionVerb): Promise<void> {
577  if (!s.app || (await read($, actingAtom))) return
578  await update($, actingAtom, () => s.app)
579  $.ui.toast(`${DOING[verb]} “${truncate(s.title, 40)}”`)
580  // not awaited: the command is queued until the session is idle
581  $.command.run({ command: SESSION_ACTION, args: `${verb} ${s.app}` }).catch(async err => {
582    await update($, actingAtom, () => null)
583    $.ui.toast(`Couldn't ${verb} it: ${errText(err)}`)
584  })
585}
586
587/**
588 * Summarize one conversation now, or again: hand this session a digest turn for just that
589 * conversation (`/convo-digest:digest conversation <id>`), even if it is unchanged. The
590 * pane refreshes when the turn ends. One at a time.
591 */
592async function summarizeOne($: $T, s: Pick<McSession, 'id' | 'title' | 'digested'>): Promise<void> {
593  if (await read($, resummarizingAtom)) return
594  await update($, resummarizingAtom, () => s.id)
595  $.ui.toast(`${s.digested ? 'Re-summarizing' : 'Summarizing'} “${truncate(s.title, 40)}”`)
596  // not awaited: the command is queued until the session is idle
597  $.command.run({ command: DIGEST.command, args: `conversation ${s.id}` }).catch(async err => {
598    await update($, resummarizingAtom, () => null)
599    $.ui.toast(`Couldn't start the digest: ${errText(err)}`)
600  })
601}
602
603/** Summarize or Re-summarize — not for routine runs, which the digest keeps out. */
604function SummarizeButton(el: El, $: $T, s: McSession, key: string, self: string, busy: string | null) {
605  const { Button } = el
606  if (s.scheduled) return null
607  const label = busy === s.id ? 'Summarizing…' : s.digested ? 'Re-summarize' : 'Summarize'
608  return <Button key={key} label={label} plain dimColor onPress={() => { void summarizeOne($, s) }} />
609}
610
611/** Archive or Unarchive — never for the session the pane runs in, which archiving would end. */
612function ArchiveButton(el: El, $: $T, s: McSession, key: string, self: string) {
613  const { Button } = el
614  if (!s.app || s.id === self) return null
615  return <Button key={key} label={s.archived ? 'Unarchive' : 'Archive'} plain dimColor
616    onPress={() => { void sessionAction($, s, s.archived ? 'unarchive' : 'archive') }} />
617}
618
619/** Pin it in the app's sidebar. Whether it is pinned already only the app knows (in no
620 *  file a plugin can read), so this pins; pinning a pinned chat changes nothing, and
621 *  unpinning stays in the sidebar's own menu. */
622function PinButton(el: El, $: $T, s: McSession, key: string) {
623  const { Button } = el
624  if (!s.app) return null
625  return <Button key={key} label="Pin to sidebar" plain dimColor onPress={() => { void sessionAction($, s, 'pin') }} />
626}
627
628/** Pin a conversation to another project; the collector remembers it. */
629async function moveSession($: $T, s: McSession, project: string): Promise<void> {
630  if (project === s.project) return
631  try {
632    await collect($, ['move', '--id', s.id, '--project', project])
633    $.ui.toast(`Moved “${truncate(s.title, 40)}” to ${project}`)
634    await refresh($)
635  } catch (err) {
636    $.ui.toast(`Couldn't move it: ${errText(err)}`)
637  }
638}
639
640/** The project picker under a conversation; the list is every known project. */
641function MoveTo(el: El, $: $T, s: McSession, snap: McSnapshot) {
642  const { Box, Text, Select } = el
643  const names = [...new Set([...(snap.projects ?? []), s.project])].sort((a, b) => a.localeCompare(b))
644  return (
645    <Box key={`mv-${s.id}`} flexDirection="row" gap={1}>
646      <Text dimColor>Project</Text>
647      <Select key={`move-${s.id}`} value={s.project}
648        options={names.map(n => ({ value: n, label: n }))}
649        onSelect={v => { void moveSession($, s, v) }} />
650    </Box>
651  )
652}
653
654async function openSessionByApp($: $T, app: string): Promise<void> {
655  const r = await $.process.run(['open', `claude://claude.ai/epitaxy/${app}`])
656  if (r.exitCode !== 0) $.ui.toast(`Couldn't open the session: ${r.stderr.trim() || r.exitCode}`)
657}
658
659function Detail(el: El, $: $T, s: McSession, snap: McSnapshot, now: number, self: string, busy: string | null) {
660  const { Box, Text, Button, Markdown } = el
661  const tz = snap.tzOffsetMin
662  const status = s.scheduled ? 'routine run' : statusLabel(s.status)
663  const color = statusColor(s.scheduled ? 'routine' : s.status)
664  return (
665    <Box key="detail" flexDirection="column" borderStyle="round" borderColor={color} paddingX={1}>
666      <Text bold wrap="wrap">{s.title}</Text>
667      <Text dimColor wrap="wrap">
668        {`${s.project}${s.branch ? ` · ${s.branch}` : ''} · ${when(s.first, snap, now)}–${hm(s.last, tz)} · ` +
669          `${duration(s.activeMin)} active · `}
670        <Text color={color}>{status}</Text>
671        {s.changedSinceDigest ? ' · has new activity since its summary' : ''}
672        {s.tickets.length ? ` · ${s.tickets.join(', ')}` : ''}
673        {s.archived ? ' · archived' : ''}
674      </Text>
675      {s.gist
676        ? <Markdown text={s.gist} />
677        : <Text dimColor>{s.scheduled
678          ? 'Scheduled runs are kept out of the digest.'
679          : 'Not summarized yet — the next digest run will pick it up.'}</Text>}
680      {s.open ? <Text color={statusColor('unresolved')} wrap="wrap">{`Still open: ${s.open}`}</Text> : null}
681      {s.needsAction ? <Text color={statusColor('routine-failed')} wrap="wrap">{`Needs action: ${s.needsAction}`}</Text> : null}
682      <Box flexDirection="row" gap={2} marginTop={1}>
683        <Button key="read-selected" label="Read" variant="primary" hotkey="v"
684          onPress={() => { void openReader($, s.id, false) }} />
685        <Button key="open-selected" label={s.app ? 'Open in app' : 'Copy resume command'}
686          hotkey="o" onPress={e => { void openSession($, s.id, e.surface) }} />
687        {SummarizeButton(el, $, s, 'summarize-selected', self, busy)}
688        {PinButton(el, $, s, 'pin-selected')}
689        {ArchiveButton(el, $, s, 'archive-selected', self)}
690        <Button key="clear-selected" label="Clear" plain dimColor
691          onPress={() => { void update($, selectedAtom, () => null) }} />
692        {s.scheduled ? null : MoveTo(el, $, s, snap)}
693      </Box>
694    </Box>
695  )
696}
697
698const ASK_HINT = 'Ask about your work — e.g. what is still open from last week?'
699
700function Ask(el: El, $: $T, ask: McAsk, snap: McSnapshot, view: McView, surface: string) {
701  const { Box, Text, Button, Input, Markdown, Client } = el
702  const cited = ask.answer ? cites(ask.answer, ask.refs) : []
703  const range = snap.range.key === 'today' ? 'today' : snap.range.key === 'yesterday' ? 'yesterday'
704    : snap.range.key === '7d' ? 'this week' : snap.range.days.length > 1 ? 'in this period' : 'that day'
705  // the two workstreams with most time in view
706  const time = new Map<string, number>()
707  for (const s of inView(snap, view)) if (s.workstream) time.set(s.workstream, (time.get(s.workstream) ?? 0) + s.activeMin)
708  const top = [...time.entries()].sort((a, b) => b[1] - a[1]).slice(0, 2).map(([n]) => n)
709  const suggestions = [
710    `What did I work on ${range}, by workstream?`,
711    'What is still unfinished that I should pick back up?',
712    ...top.map(n => `Where does "${n}" stand?`),
713  ]
714  return (
715    <Box flexDirection="column" width="100%">
716      {surface === 'desktop'
717        // the desktop's Input can't be widened; this draws a full-width field itself
718        ? <Client key="askbar" module="./askbar.tsx" width="100%" height={3}
719            props={{ placeholder: ASK_HINT, question: ask.question, busy: ask.busy }} />
720        : <Input key="ask" placeholder={ASK_HINT} submitLabel="Ask" value={ask.busy ? ask.question : undefined}
721            onSubmit={value => { void runAsk($, value) }} />}
722      <Box flexDirection="row" flexWrap="wrap" gap={1} marginTop={1}>
723        {suggestions.map((q, i) => (
724          <Box key={`chip-${i}`} borderStyle="round" borderColor="#3a3a3a" paddingX={1}
725            hover={{ borderColor: '#a371f7' }}>
726            <Button key={`suggest-${i}`} label={q} plain dimColor onPress={() => { void runAsk($, q) }} />
727          </Box>
728        ))}
729      </Box>
730      {ask.busy && surface !== 'desktop'
731        ? <Text dimColor>{`Thinking about “${truncate(ask.question, 80)}”…`}</Text> : null}
732      {ask.error
733        ? (
734          <Box flexDirection="row" gap={2}>
735            <Text color="#f85149" wrap="wrap">{ask.error}</Text>
736            <Button key="ask-clear-error" label="Clear" plain dimColor onPress={() => { void clearAsk($) }} />
737          </Box>
738        )
739        : null}
740      {!ask.busy && ask.answer
741        ? (
742          <Box flexDirection="column" marginTop={1} borderStyle="round" borderColor="#30363d" paddingX={1}>
743            <Box flexDirection="row" justifyContent="space-between" gap={2}>
744              <Text dimColor wrap="wrap">{ask.question}</Text>
745              <Button key="ask-clear" label="Clear ✕" plain dimColor onPress={() => { void clearAsk($) }} />
746            </Box>
747            <Markdown text={ask.answer} />
748            {cited.length
749              ? (
750                <Box flexDirection="column" marginTop={1}>
751                  {cited.map((ref, i) => (
752                    <Button key={`cite-${i}`} label={`Open: ${truncate(ref.title, 90)}`} plain
753                      onPress={e => { void openCited($, ref, e.surface) }} />
754                  ))}
755                </Box>
756              )
757              : null}
758          </Box>
759        )
760        : null}
761    </Box>
762  )
763}
764
765/** The first few bullets of the script: what the folded standup shows. */
766function preview(script: string, n: number): { text: string; more: number } {
767  const lines = script.split('\n')
768  const bullets = lines.filter(l => /^\s*[-*] /.test(l))
769  const shown = bullets.slice(0, n).map(l => truncate(l, 140))
770  return { text: shown.join('\n'), more: Math.max(0, bullets.length - shown.length) }
771}
772
773/** The day's standup brief, up top: folded to its first bullets, the rest a click away. */
774function Standup(el: El, $: $T, snap: McSnapshot, now: number, isOpen: boolean) {
775  const { Box, Text, Button, Markdown } = el
776  const st = snap.standup
777  if (!st) {
778    return <Text dimColor>No standup brief yet — the standup-brief task writes one each weekday morning.</Text>
779  }
780  const short = preview(st.script, 3)
781  return (
782    <Box flexDirection="column" borderStyle="round" borderColor="#3fb950" paddingX={1}>
783      <Box flexDirection="row" justifyContent="space-between">
784        <Text bold>{st.heading}</Text>
785        <Text dimColor>{st.at ? `written ${when(st.at, snap, now)}` : ''}</Text>
786      </Box>
787      {isOpen
788        ? (
789          <Box flexDirection="column">
790            {st.preamble ? <Text dimColor italic wrap="wrap">{st.preamble}</Text> : null}
791            <Markdown text={st.script} />
792            {st.table ? <Markdown text={st.table} /> : null}
793          </Box>
794        )
795        : <Markdown text={short.text || truncate(st.script, 300)} />}
796      <Box flexDirection="row" gap={2} marginTop={1}>
797        <Button key="standup-toggle" plain
798          label={isOpen ? '▴ Hide the full standup' : `▾ Show the full standup${short.more ? ` (+${short.more} more)` : ''}`}
799          onPress={() => { void update($, standupOpenAtom, v => !v) }} />
800        <Button key="standup-copy" label="Copy script" plain dimColor onPress={e => {
801          void (async () => {
802            const r = await $.ui.copy({ text: `${st.heading}\n\n${st.script}`, surface: e.surface })
803            $.ui.toast(r.isCopied ? 'Standup script copied' : "Couldn't copy the script")
804          })()
805        }} />
806        {st.app
807          ? <Button key="standup-open" label="Open the run" plain dimColor
808              onPress={() => { void openSessionByApp($, st.app as string) }} />
809          : null}
810      </Box>
811    </Box>
812  )
813}
814
815/** The panel under a clicked conversation on the desktop: what it was, then a way in. */
816function Expanded(el: El, $: $T, s: McSession, snap: McSnapshot, now: number, color: string, self: string,
817  busy: string | null) {
818  const { Box, Text, Button, Markdown } = el
819  const tz = snap.tzOffsetMin
820  return (
821    <Box key={`x-${s.id}`} flexDirection="column" borderStyle="round" borderColor={color}
822      paddingX={1} marginLeft={2} marginBottom={1}>
823      <Text bold wrap="wrap">{s.title}</Text>
824      <Text dimColor wrap="wrap">
825        {`${s.project}${s.branch ? ` · ${s.branch}` : ''} · ${when(s.first, snap, now)}–${hm(s.last, tz)} · ` +
826          `${duration(s.activeMin)} active${s.tickets.length ? ` · ${s.tickets.join(', ')}` : ''}` +
827          (s.archived ? ' · archived' : '')}
828      </Text>
829      {s.gist
830        ? <Markdown text={s.gist} />
831        : <Text dimColor>{s.scheduled ? 'A scheduled run — kept out of the digest.'
832          : 'Not summarized yet — the next digest run will pick it up.'}</Text>}
833      {s.open ? <Text dimColor wrap="wrap">{`Left for later: ${s.open}`}</Text> : null}
834      <Box flexDirection="row" gap={2} marginTop={1}>
835        <Button key={`read-${s.id}`} label="Read conversation" variant="primary"
836          onPress={() => { void openReader($, s.id, false) }} />
837        <Button key={`open-${s.id}`} label={s.app ? 'Open ↗' : 'Copy resume command'}
838          onPress={e => { void openSession($, s.id, e.surface) }} />
839        {SummarizeButton(el, $, s, `summarize-${s.id}`, self, busy)}
840        {PinButton(el, $, s, `pin-${s.id}`)}
841        {ArchiveButton(el, $, s, `archive-${s.id}`, self)}
842        {s.scheduled ? null : MoveTo(el, $, s, snap)}
843      </Box>
844    </Box>
845  )
846}
847
848async function openCited($: $T, ref: McRef, surface?: Parameters<$T['ui']['copy']>[0]['surface']) {
849  if (ref.app) return openSessionByApp($, ref.app)
850  await $.ui.copy({ text: `claude --resume ${ref.id}`, surface })
851  $.ui.toast('No desktop session for this one — copied `claude --resume` to the clipboard')
852}
853
854function Health(el: El, snap: McSnapshot, now: number) {
855  const { Box, Text } = el
856  const run = snap.digest.lastRun
857  const g = runDot(run?.status === 'ok' ? 'ok' : run?.status === 'nothing' ? 'nothing'
858    : run?.status === 'failed' ? 'failed' : run?.status ? 'warn' : undefined)
859  return (
860    <Box flexDirection="column">
861      <Text bold>Digest health</Text>
862      <Text wrap="wrap">
863        <Text color={g.color}>● </Text>
864        {run?.start
865          ? `last run ${when(run.start, snap, now)} (${run.trigger ?? '?'}): ${run.status ?? 'never finished'}` +
866            `${run.indexed != null ? ` · ${run.indexed} indexed` : ''}` +
867            `${run.renamed != null ? ` · ${run.renamed} titles synced` : ''}`
868          : 'no runs recorded yet'}
869      </Text>
870      {snap.digest.issuesSinceOk
871        ? <Text color={statusColor('unresolved')}>{`${snap.digest.issuesSinceOk} issue(s) since the last good run`}</Text>
872        : <Text dimColor>No issues since the last good run.</Text>}
873      {snap.digest.issues.slice(0, 3).map((i, n) => (
874        <Text dimColor wrap="truncate-end">
875          {`  ${i.ts ? when(i.ts, snap, now) : ''} ${i.kind}: ${truncate(i.detail ?? '', 140)}`}
876        </Text>
877      ))}
878    </Box>
879  )
880}
881
882/** The pane, drawn; `selfId` is the session it is drawn in. */
883async function drawPane($: $T, e: PaneRender, selfId: string) {
884  const snap = await read($, snapAtom)
885  const view = await read($, viewAtom)
886  const selected = await read($, selectedAtom)
887  const ask = await read($, askAtom)
888  const status = await read($, statusAtom)
889  const standupOpen = await read($, standupOpenAtom)
890  const summarizing = await read($, summarizingAtom)
891  const collapsed = await read($, collapsedAtom)
892  const reader = await read($, readerAtom)
893  const busyOne = await read($, resummarizingAtom)
894  const now = await $.clock.now()
895  // wide enough to read a conversation beside the timeline; else it takes the pane
896  const wide = e.surface === 'desktop' && e.props.bodyColumns >= WIDE
897
898  if (e.surface === 'mobile' || e.surface === 'vscode') {
899    const { Box, Text } = $.ui.resolve(e)
900    return (
901      <Box flexDirection="column">
902        <Text bold>{TITLE}</Text>
903        {(snap?.sessions ?? []).filter(s => !s.scheduled).slice(-20).map(s => (
904          <Text wrap="truncate-end">{`${hm(s.first, snap?.tzOffsetMin ?? 0)} ${s.project} · ${s.title}`}</Text>
905        ))}
906      </Box>
907    )
908  }
909
910  const el = $.ui.resolve(e) as unknown as El
911  const { Box, Text, Button, Select, Client } = el
912
913  if (!snap) {
914    return (
915      <Box flexDirection="column">
916        <Text bold>{TITLE}</Text>
917        {status.error
918          ? <Text color="#f85149" wrap="wrap">{`Couldn't collect the data: ${status.error}`}</Text>
919          : <Text dimColor>Collecting your conversations…</Text>}
920        <Button key="refresh" label="Retry" onPress={() => { void refresh($) }} />
921      </Box>
922    )
923  }
924
925  const tz = snap.tzOffsetMin
926  const pending = unsummarized(snap, selfId)
927  const t = timeline(snap, view, now)
928  const shown = inView(snap, view)
929  const filtered = Boolean(view.ws || view.kind)
930  // the filters' choices: what this range holds, most time first, and the current pick
931  const tally = (key: (s: McSession) => string | null) => {
932    const m = new Map<string, number>()
933    for (const s of snap.sessions) {
934      const k = s.scheduled ? null : key(s)
935      if (k) m.set(k, (m.get(k) ?? 0) + s.activeMin)
936    }
937    return [...m.entries()].sort((a, b) => b[1] - a[1]).map(([k]) => k)
938  }
939  const wsChoices = tally(s => s.workstream)
940  if (view.ws && !wsChoices.includes(view.ws)) wsChoices.push(view.ws)
941  const kindChoices = tally(s => s.kind)
942  if (view.kind && !kindChoices.includes(view.kind)) kindChoices.push(view.kind)
943  const sel = selected ? snap.sessions.find(s => s.id === selected) ?? null : null
944  const multiDay = snap.range.days.length > 1
945  const rows = rowsOptions(multiDay)
946  const pickDays = Array.from({ length: CUSTOM_DAYS }, (_, i) => dayAt(now, tz, i))
947  // older days come from `/mission YYYY-MM-DD..YYYY-MM-DD`: keep those picked
948  const pickWith = (v: string | undefined) => v && !pickDays.some(d => d.value === v)
949    ? [...pickDays, { value: v, label: v }] : pickDays
950  // axis + rows + a blank line before each heading but the first + blank + footer
951  // the terminal timeline's rows: axis, headings (a blank line before all but the
952  // first), the conversations of unfolded groups, then a blank line and the footer
953  let foldedNow = false
954  const visible = t.rows.filter(r => {
955    if (r.kind === 'header') {
956      foldedNow = collapsed.includes(r.label)
957      return true
958    }
959    return !foldedNow
960  }).length
961  const height = 1 + visible + t.rows.filter((r, i) => r.kind === 'header' && i > 0).length + 2
962  const groups = t.rows.filter(r => r.kind === 'header').map(r => r.label)
963  const allFolded = groups.length > 0 && groups.every(g => collapsed.includes(g))
964  const tl = { collapsed, rows: t.rows, hourFrom: t.hourFrom, hourTo: t.hourTo, ticks: t.ticks, now: t.now,
965    nowBase: t.nowBase, selected, order: t.order }
966  const readSession = reader ? snap.sessions.find(s => s.id === reader.id) ?? null : null
967
968  if (reader && !wide) {
969    // a narrow pane: the conversation takes it over, the way back on top
970    return (
971      <Box flexDirection="column" gap={1}>
972        <Box flexDirection="row" justifyContent="space-between">
973          <Button key="reader-back" label={`← ${TITLE} · ${snap.range.label}`} plain
974            onPress={() => { void closeReader($) }} />
975          <Text dimColor>{reader.loading ? 'reading…' : `updated ${hm(now, tz)}`}</Text>
976        </Box>
977        {Reader(el, $, reader, readSession, snap, now, selfId, busyOne)}
978      </Box>
979    )
980  }
981
982  return (
983    <Box flexDirection="column" gap={1}>
984      <Box flexDirection="column">
985        <Box flexDirection="row" justifyContent="space-between">
986          <Text bold>{`${TITLE} · ${snap.range.label}`}</Text>
987          <Box flexDirection="row" gap={1}>
988            <Text dimColor>{status.loading ? 'refreshing…' : `updated ${hm(snap.generatedAt, tz)}`}</Text>
989            <Button key="refresh" label="Refresh" hotkey="r" plain onPress={() => { void refresh($) }} />
990          </Box>
991        </Box>
992        <Box flexDirection="row" gap={2} alignItems="center" flexWrap="wrap">
993          <Text dimColor wrap="wrap">
994            {`${snap.totals.conversations} conversations · ${duration(snap.totals.activeMin)} active · ` +
995              `${snap.totals.projects} project${snap.totals.projects === 1 ? '' : 's'}`}
996          </Text>
997          {pending.length || summarizing
998            ? <Button key="summarize" plain variant="primary"
999                label={summarizing ? 'Summarizing… (follow it in the conversation)'
1000                  : `Summarize ${pending.length} now`}
1001                onPress={() => { void summarizeNow($) }} />
1002            : <Text dimColor>· everything is summarized</Text>}
1003        </Box>
1004        {status.error ? <Text color="#f85149" wrap="wrap">{`Last refresh failed: ${status.error}`}</Text> : null}
1005      </Box>
1006
1007      {Standup(el, $, snap, now, standupOpen)}
1008
1009      {Ask(el, $, ask, snap, view, e.surface)}
1010
1011      <Box flexDirection="row" gap={2} flexWrap="wrap">
1012        <Select key="range" value={view.range}
1013          options={[{ value: 'today', label: 'Today' }, { value: 'yesterday', label: 'Yesterday' },
1014            { value: '7d', label: 'Last 7 days' }, { value: 'custom', label: 'Custom range…' }]}
1015          onSelect={v => {
1016            // a first custom range starts as the last two weeks
1017            void setView($, v === 'custom' && !view.from
1018              ? { range: 'custom', from: pickDays[13]!.value, to: pickDays[0]!.value }
1019              : { range: v as McRangeKey })
1020          }} />
1021        {view.range === 'custom'
1022          ? (
1023            <Box key="custom-range" flexDirection="row" gap={1} alignItems="center">
1024              <Text dimColor>From</Text>
1025              <Select key="from" value={view.from ?? pickDays[13]!.value} options={pickWith(view.from)}
1026                onSelect={v => { void setView($, { from: v }) }} />
1027              <Text dimColor>to</Text>
1028              <Select key="to" value={view.to ?? pickDays[0]!.value} options={pickWith(view.to)}
1029                onSelect={v => { void setView($, { to: v }) }} />
1030            </Box>
1031          )
1032          : null}
1033        <Select key="rows" value={rows.some(r => r.value === view.rows) ? view.rows : 'project'}
1034          options={rows} onSelect={v => { void setView($, { rows: v as McRowsMode }) }} />
1035        {t.rows.some(r => r.kind === 'header')
1036          ? <Button key="fold-all" plain dimColor label={allFolded ? '▾ Unfold all' : '▸ Fold all'}
1037              onPress={() => { void update($, collapsedAtom, () => (allFolded ? [] : groups)) }} />
1038          : null}
1039        <Select key="filter-ws" value={view.ws ?? '*'}
1040          options={[{ value: '*', label: 'All workstreams' }, ...wsChoices.slice(0, 60).map(n => ({ value: n, label: n }))]}
1041          onSelect={v => { void setView($, { ws: v === '*' ? undefined : v }) }} />
1042        <Select key="filter-kind" value={view.kind ?? '*'}
1043          options={[{ value: '*', label: 'All kinds of work' }, ...kindChoices.map(k => ({ value: k, label: k }))]}
1044          onSelect={v => { void setView($, { kind: v === '*' ? undefined : v as McKind }) }} />
1045        {filtered
1046          ? <Button key="filter-clear" label="Clear filters" plain dimColor
1047              onPress={() => { void setView($, { ws: undefined, kind: undefined }) }} />
1048          : null}
1049        <Select key="color" value={view.color}
1050          options={[{ value: 'project', label: 'Colour: project' }, { value: 'status', label: 'Colour: digest status' }]}
1051          onSelect={v => { void setView($, { color: v as McColorMode }) }} />
1052      </Box>
1053
1054      <Box flexDirection="row" gap={2} alignItems="flex-start">
1055      <Box flexDirection="column" width={wide && reader ? '45%' : '100%'} flexShrink={0}>
1056        <Text bold>Timeline</Text>
1057        {t.hidden
1058          ? <Text dimColor wrap="wrap">{`Drawing the ${shown.length - t.hidden} conversations with the most time; ` +
1059              `${t.hidden} more in this view. Pick a workstream or a kind of work to see them.`}</Text>
1060          : null}
1061        {filtered
1062          ? <Text dimColor>{`Showing ${shown.length} of ${snap.totals.conversations} conversations · ` +
1063              `${duration(workedMinutes(shown))} active` +
1064              `${view.ws ? ` · ${view.ws}` : ''}${view.kind ? ` · ${view.kind}` : ''}`}</Text>
1065          : null}
1066        {e.surface === 'desktop'
1067          ? (() => {
1068            // the desktop draws vectors: each row is a clickable name beside its own
1069            // small strip of bars, so the rows line up and a click opens the session
1070            const { Svg } = $.ui.resolve(e)
1071            // beside the reader the timeline has a bit under half the pane
1072            const cols = wide && reader ? Math.floor(e.props.bodyColumns * 0.45) : e.props.bodyColumns
1073            // the title matters more than the hours: it gets most of the row, whole
1074            const labelCells = Math.max(30, Math.min(110, Math.floor(cols * 0.6)))
1075            const stripPx = Math.max(160, (cols - labelCells - 2) * PX_PER_CELL)
1076            const strips = timelineStrips(tl, stripPx)
1077            return (
1078              <Box flexDirection="column">
1079                <Box flexDirection="row">
1080                  <Box width={labelCells} />
1081                  <Box flexGrow={1}><Svg source={strips.axis} alt="Hours" /></Box>
1082                </Box>
1083                {t.rows.map((r, i) => {
1084                  if (r.kind === 'header') {
1085                    const isFolded = collapsed.includes(r.label)
1086                    return (
1087                      <Box key={`h-${i}`} flexDirection="column" marginTop={i ? 1 : 0}>
1088                        <Box flexDirection="row" alignItems="center">
1089                          <Box width={labelCells} flexDirection="row" overflow="hidden">
1090                            <Button key={`fold-${i}`} plain label={`${isFolded ? '▸' : '▾'} ${r.label}`}
1091                              onPress={() => { void toggleGroup($, r.label) }} />
1092                          </Box>
1093                          {isFolded && strips.rows[i]
1094                            ? <Box flexGrow={1}><Svg source={strips.rows[i] as string} alt={`${r.label}, all conversations`} /></Box>
1095                            : null}
1096                        </Box>
1097                        <Text dimColor wrap="truncate-end">{`  ${r.sub}`}</Text>
1098                        {r.note && !isFolded ? <Text dimColor italic wrap="truncate-end">{`  ${r.note}`}</Text> : null}
1099                      </Box>
1100                    )
1101                  }
1102                  // a folded group hides its conversations
1103                  const heading = [...t.rows.slice(0, i)].reverse().find(x => x.kind === 'header')
1104                  if (heading && collapsed.includes(heading.label)) return null
1105                  const dot = r.bars[0]?.color ?? '#8b949e'
1106                  const isSel = r.id !== null && r.id === selected
1107                  const convo = r.id
1108                  const session = isSel ? snap.sessions.find(x => x.id === r.id) ?? null : null
1109                  return (
1110                    <Box key={`r-${i}`} flexDirection="column">
1111                    <Box flexDirection="row" alignItems="center">
1112                      <Box width={labelCells} flexDirection="row" overflow="hidden">
1113                        {convo
1114                          ? (
1115                            <Box flexGrow={1} flexShrink={1} flexDirection="row">
1116                              <Text color={dot}>{isSel ? '▸ ' : '● '}</Text>
1117                              <Button key={`go-${convo}`} plain label={r.label}
1118                                onPress={() => {
1119                                  // wide: read it beside the timeline; narrow: its gist under the row
1120                                  void (wide ? openReader($, convo, true)
1121                                    : update($, selectedAtom, cur => (cur === convo ? null : convo)))
1122                                }} />
1123                            </Box>
1124                          )
1125                          : <Box flexGrow={1} flexShrink={1} overflow="hidden"><Text bold wrap="truncate-end">{r.label}</Text></Box>}
1126                        <Box width={6} justifyContent="flex-end"><Text dimColor>{r.sub}</Text></Box>
1127                      </Box>
1128                      <Box flexGrow={1}><Svg source={strips.rows[i] ?? ''} alt={r.label} /></Box>
1129                    </Box>
1130                    {session && !wide ? Expanded(el, $, session, snap, now, dot, selfId, busyOne) : null}
1131                    </Box>
1132                  )
1133                })}
1134                <Text dimColor>{wide ? 'Click a conversation to read it here.'
1135                  : 'Click a conversation to see what it was about, then read it or open it.'}</Text>
1136              </Box>
1137            )
1138          })()
1139          : <Client key="timeline" module="./timeline.tsx" props={tl} width="100%" height={height} />}
1140        <Box flexDirection="row" gap={2} flexWrap="wrap">
1141          {t.legend.map((l, i) => (
1142            <Text><Text color={l.color}>■</Text>{` ${l.label}`}</Text>
1143          ))}
1144        </Box>
1145      </Box>
1146      {wide && reader
1147        ? <Box flexGrow={1} flexShrink={1} width="55%">{Reader(el, $, reader, readSession, snap, now, selfId, busyOne)}</Box>
1148        : null}
1149      </Box>
1150
1151      {sel && e.surface !== 'desktop' ? Detail(el, $, sel, snap, now, selfId, busyOne) : null}
1152
1153      <Box flexDirection="column">
1154        <Text bold>Routines</Text>
1155        {Routines(el, $, snap, status.tasks, now)}
1156      </Box>
1157
1158      {Health(el, snap, now)}
1159    </Box>
1160  )
1161}
1162
1163// ------------------------------------------------------------------ register
1164export const register: Register = on => {
1165  let ticks = 0
1166  let selfId = ''
1167  let paneDrawn = false
1168
1169  on('session.start', async ($, e, next) => {
1170    await $.command.register({
1171      name: 'mission',
1172      description: 'Mission Control: routines, a clickable timeline of your conversations, and an Ask box',
1173      argumentHint: '[today|yesterday|week|YYYY-MM-DD..YYYY-MM-DD] | ask <question> | home [off] | band on|off',
1174    })
1175    await breadcrumb($, { sessionStartAt: await $.clock.now(), surface: e.surface })
1176    selfId = await $.session.id().catch(() => '')
1177    const stored = await $.store.get('view')
1178    if (stored && typeof stored === 'object') {
1179      const saved = stored as Partial<McView> & { version?: number }
1180      // a new default grouping (workstreams) replaces an older saved one, once
1181      // and the digest's open/solved guess stops being the default colour, once
1182      const fresh = saved.version === VIEW_VERSION
1183      await update($, viewAtom, v => ({ ...v, ...saved,
1184        rows: fresh && saved.rows ? saved.rows : 'workstream',
1185        color: fresh && saved.color ? saved.color : 'project' }))
1186    }
1187    const band = await $.store.get('band')
1188    if (band === false) await update($, bandAtom, () => false)
1189    // the home session opens the pane by itself: click it in the sidebar and it's there
1190    const home = await readHome($)
1191    if (home && home.cli === (await $.session.id())) {
1192      await writeHome($)                                // refreshes the desktop id the band links to
1193      void openPane($, true)
1194    } else {
1195      void refresh($)                                   // fills the status line too
1196    }
1197    $.clock.every(READ_POLL_MS, () => {
1198      void (async () => {
1199        if (!(await read($, readerAtom))) return
1200        if ((await $.ui.panes()).some(p => p.id === PANE)) await loadReader($, false)
hooks/mission-control/layout.ts 449 lines
1// Pure layout for the Mission Control pane: snapshot + view → the timeline's rows,
2// the legend, and the small formatters. No `$`, no clock: times are placed against
3// the local midnights the collector computed and formatted with its timezone offset,
4// so the sandbox's own timezone never matters.
5import type {
6  McBar,
7  McColorMode,
8  McRoutine,
9  McRow,
10  McRowsMode,
11  McSession,
12  McSnapshot,
13  McTick,
14  McTimelineProps,
15  McView,
16} from '../../types'
17
18const HOUR = 3_600_000
19const DAY = 86_400_000
20
21export const STATUS_COLORS: Record<string, string> = {
22  solved: '#3fb950',
23  unresolved: '#d29922',
24  exploratory: '#58a6ff',
25  abandoned: '#8b949e',
26  new: '#bc8cff',
27  routine: '#8b949e',
28  'routine-failed': '#f85149',
29  'routine-warn': '#d29922',
30}
31
32export const STATUS_LABELS: Record<string, string> = {
33  solved: 'solved',
34  unresolved: 'open',
35  exploratory: 'exploratory',
36  abandoned: 'abandoned',
37  new: 'not summarized yet',
38  routine: 'routine run',
39}
40
41const PROJECT_COLORS = ['#58a6ff', '#3fb950', '#d29922', '#bc8cff', '#f778ba', '#39c5cf',
42  '#ff9a4c', '#a5d6ff', '#7ee787', '#e3b341']
43
44/** A run's status as a coloured dot (the routine history) and a word. */
45export type Dot = { glyph: string; color: string; word: string }
46const DOTS: Record<string, Dot> = {
47  ok: { glyph: '●', color: '#3fb950', word: 'ok' },
48  nothing: { glyph: '●', color: '#6e7681', word: 'nothing to do' },
49  warn: { glyph: '●', color: '#d29922', word: 'needs a look' },
50  failed: { glyph: '●', color: '#f85149', word: 'failed' },
51  running: { glyph: '●', color: '#58a6ff', word: 'running' },
52}
53const UNKNOWN_DOT: Dot = { glyph: '○', color: '#6e7681', word: 'no summary' }
54export const runDot = (status: string | null | undefined): Dot =>
55  (status ? DOTS[status] : undefined) ?? UNKNOWN_DOT
56export const statusColor = (status: string): string => STATUS_COLORS[status] ?? '#8b949e'
57export const statusLabel = (status: string): string => STATUS_LABELS[status] ?? status
58
59const NAMES: Record<string, string> = {
60  'convo-digest-nightly': 'Digest',
61  'standup-brief': 'Standup brief',
62  'ai-daily-writeup': 'AI daily write-up',
63}
64
65/** "convo-digest-nightly" → "Digest"; others title-cased, "ai" kept as "AI". */
66export function routineName(r: Pick<McRoutine, 'task' | 'name'>): string {
67  const known = NAMES[r.task]
68  if (known) return known
69  const words = r.task.replace(/-\d{4}-\d\d-\d\d$/, '').split('-').filter(Boolean)
70  const name = words.map((w, i) => (w === 'ai' ? 'AI' : i === 0 ? w[0]!.toUpperCase() + w.slice(1) : w)).join(' ')
71  const date = r.task.match(/(\d{4}-\d\d-\d\d)$/)?.[1]
72  return date ? `${name} (one-off ${date})` : name
73}
74
75// ---------------------------------------------------------------- formatting
76const pad = (n: number) => String(n).padStart(2, '0')
77
78/** Local clock parts of an epoch-ms instant, from the collector's offset. */
79export function local(ms: number, tzOffsetMin: number) {
80  const d = new Date(ms + tzOffsetMin * 60_000)
81  return { hh: d.getUTCHours(), mm: d.getUTCMinutes(), day: d.getUTCDate(),
82    month: d.getUTCMonth(), dow: d.getUTCDay() }
83}
84
85const DOW = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
86const MON = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
87
88export function hm(ms: number, tz: number): string {
89  const p = local(ms, tz)
90  return `${pad(p.hh)}:${pad(p.mm)}`
91}
92
93export function dayLabel(ms: number, tz: number): string {
94  const p = local(ms, tz)
95  return `${DOW[p.dow]} ${p.day} ${MON[p.month]}`
96}
97
98/** "today 09:22", "yesterday 18:04", or "Mon 5 Oct 09:22". */
99export function when(ms: number, snap: McSnapshot, nowMs: number): string {
100  const tz = snap.tzOffsetMin
101  const sameDay = (a: number, b: number) => {
102    const x = local(a, tz)
103    const y = local(b, tz)
104    return x.day === y.day && x.month === y.month
105  }
106  if (sameDay(ms, nowMs)) return `today ${hm(ms, tz)}`
107  if (sameDay(ms, nowMs - DAY)) return `yesterday ${hm(ms, tz)}`
108  return `${dayLabel(ms, tz)} ${hm(ms, tz)}`
109}
110
111/**
112 * Time worked across conversations, counting parallel ones once: the union of their
113 * active stretches, optionally clipped to [from, to).
114 */
115export function workedMinutes(sessions: McSession[], from = -Infinity, to = Infinity): number {
116  const merged: [number, number][] = []
117  const segs = sessions.flatMap(s => s.segments)
118    .map(([a, b]) => [Math.max(a, from), Math.min(b, to)] as [number, number])
119    .filter(([a, b]) => b > a)
120    .sort((x, y) => x[0] - y[0])
121  for (const [a, b] of segs) {
122    const last = merged[merged.length - 1]
123    if (last && a <= last[1]) last[1] = Math.max(last[1], b)
124    else merged.push([a, b])
125  }
126  return Math.round(merged.reduce((n, [a, b]) => n + (b - a) / 60_000, 0))
127}
128
129export function duration(min: number): string {
130  if (min < 60) return `${min}m`
131  const h = Math.floor(min / 60)
132  const m = min % 60
133  return m ? `${h}h${pad(m)}` : `${h}h`
134}
135
136export function truncate(text: string, max: number): string {
137  const t = text.replace(/\s+/g, ' ').trim()
138  return t.length <= max ? t : `${t.slice(0, Math.max(1, max - 1))}…`
139}
140
141// ---------------------------------------------------------------- colouring
142export function projectColors(sessions: McSession[]): Record<string, string> {
143  const names = [...new Set(sessions.filter(s => !s.scheduled).map(s => s.project))].sort()
144  return Object.fromEntries(names.map((n, i) => [n, PROJECT_COLORS[i % PROJECT_COLORS.length] ?? '#58a6ff']))
145}
146
147/** The colour half-way to grey: what a bar looks like while another one is selected. */
148export function mute(hex: string): string {
149  const m = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(hex)
150  if (!m) return hex
151  const grey = 0x6e
152  const mix = (c: string) => Math.round((parseInt(c, 16) + grey * 2) / 3).toString(16).padStart(2, '0')
153  return `#${mix(m[1]!)}${mix(m[2]!)}${mix(m[3]!)}`
154}
155
156function runStatus(s: McSession, snap: McSnapshot): string {
157  for (const r of snap.routines) {
158    const run = r.runs.find(x => x.cli === s.id)
159    if (run?.status === 'failed') return 'routine-failed'
160    if (run?.status === 'warn') return 'routine-warn'
161  }
162  return 'routine'
163}
164
165export function colorOf(s: McSession, snap: McSnapshot, mode: McColorMode,
166  projects: Record<string, string>): string {
167  if (s.scheduled) return statusColor(runStatus(s, snap))
168  if (mode === 'project') return projects[s.project] ?? '#8b949e'
169  return statusColor(s.status)
170}
171
172export function legend(snap: McSnapshot, view: McView): { label: string; color: string }[] {
173  if (view.color === 'project') {
174    return Object.entries(projectColors(snap.sessions)).map(([label, color]) => ({ label, color }))
175  }
176  const present = new Set(snap.sessions.filter(s => !s.scheduled).map(s => s.status))
177  const out = ['solved', 'unresolved', 'exploratory', 'abandoned', 'new']
178    .filter(k => present.has(k))
179    .map(k => ({ label: statusLabel(k), color: statusColor(k) }))
180  return out
181}
182
183// ---------------------------------------------------------------- timeline
184function tip(s: McSession, tz: number): string {
185  const status = s.scheduled ? 'routine run' : statusLabel(s.status)
186  return `${hm(s.first, tz)}–${hm(s.last, tz)} · ${duration(s.activeMin)} active · ${s.project} · ` +
187    `${status} · ${s.title}`
188}
189
190/** The most conversations the timeline draws; the rest are counted, and a filter shows them. */
191export const MAX_ROWS = 80
192
193/** Where a conversation without a workstream or kind yet goes, last. */
194export const UNFILED = 'Not summarized yet'
195
196function groupKey(s: McSession, rows: McRowsMode): string {
197  if (rows === 'status') return statusLabel(s.status)
198  if (rows === 'category') return s.category === 'unknown' ? 'not profiled' : s.category
199  if (rows === 'workstream') return s.workstream ?? UNFILED
200  if (rows === 'kind') return s.kind ?? UNFILED
201  return s.project
202}
203
204/** The conversations a view shows: routine runs left out, then its workstream and kind
205 *  filters applied. */
206export function visible(snap: McSnapshot, view: McView): McSession[] {
207  return snap.sessions.filter(s => !s.scheduled && (!view.ws || s.workstream === view.ws) &&
208    (!view.kind || s.kind === view.kind))
209}
210
211const STATUS_ORDER = ['open', 'not summarized yet', 'exploratory', 'solved', 'abandoned']
212
213/** Snapshot + view → everything the timeline Client draws, plus the legend. */
214export function timeline(snap: McSnapshot, view: McView, nowMs: number): McTimelineProps & {
215  legend: { label: string; color: string }[]
216  /** conversations in the view not drawn, past MAX_ROWS */
217  hidden: number
218} {
219  const tz = snap.tzOffsetMin
220  const projects = projectColors(snap.sessions)
221  const bar = (s: McSession): McBar => {
222    const color = colorOf(s, snap, view.color, projects)
223    return { id: s.id, color, muted: mute(color), segs: s.segments, tip: tip(s, tz),
224      ...(s.scheduled ? { routine: true as const } : {}) }
225  }
226  const convos = visible(snap, view)
227  const days = snap.range.days
228  const multiDay = days.length > 1
229  const byDay = multiDay && view.rows === 'day'
230  const rows: McRow[] = []
231  const minutes = (g: McSession[]) => workedMinutes(g)
232  let hidden = 0
233
234  if (byDay) {
235    // one row per day, the hours of the day across: when in the day you work
236    for (const d of days) {
237      const inDay = (s: McSession) => s.segments.some(([a, b]) => a < d.to && b > d.from)
238      const clip = (s: McSession): McSession =>
239        ({ ...s, segments: s.segments.filter(([a, b]) => a < d.to && b > d.from) })
240      const work = convos.filter(inDay).map(clip)
241      const mins = workedMinutes(work, d.from, d.to)
242      rows.push({ kind: 'track', label: d.label, sub: mins ? duration(mins) : '', base: d.from, id: null,
243        bars: work.map(bar) })
244    }
245  } else {
246    const base = snap.range.from
247    const keyOf = (s: McSession) => groupKey(s, view.rows)
248    const groups = new Map<string, McSession[]>()
249    for (const s of convos) groups.set(keyOf(s), [...(groups.get(keyOf(s)) ?? []), s])
250    // most time first; the not-yet-summarized last; status keeps its own order
251    const ordered = [...groups.entries()].sort((a, b) =>
252      view.rows === 'status'
253        ? STATUS_ORDER.indexOf(a[0]) - STATUS_ORDER.indexOf(b[0])
254        : a[0] === UNFILED ? 1 : b[0] === UNFILED ? -1 : minutes(b[1]) - minutes(a[1]))
255    const described = new Map(snap.workstreams.map(w => [w.name, w.description]))
256    // at most MAX_ROWS conversations, whole groups at a time (the first group is always
257    // drawn, cut to the limit): a pane past a few hundred kilobytes is one the desktop
258    // won't draw at all, and a month of conversations gets there
259    let drawn = 0
260    for (const [k, g] of ordered) {
261      if (drawn >= MAX_ROWS || (drawn > 0 && drawn + g.length > MAX_ROWS)) {
262        hidden += g.length
263        continue
264      }
265      const note = view.rows === 'workstream' ? described.get(k) : null
266      rows.push({ kind: 'header', label: k,
267        sub: `${g.length} conversation${g.length > 1 ? 's' : ''} · ${duration(minutes(g))}`,
268        // Client props are plain data: leave `note` out rather than undefined
269        ...(note ? { note } : {}) })
270      // the conversation you spent most time on first, in every grouping; ties by start
271      for (const s of [...g].sort((a, b) => b.activeMin - a.activeMin || a.first - b.first)) {
272        if (drawn >= MAX_ROWS) {
273          hidden += 1
274          continue
275        }
276        rows.push({ kind: 'track', label: s.title, sub: duration(s.activeMin), base, id: s.id, bars: [bar(s)] })
277        drawn += 1
278      }
279    }
280  }
281
282  // x axis: the days across for a multi-day range, else the hours worth showing
283  let hourFrom = 0
284  let hourTo = 24
285  const ticks: McTick[] = []
286  const tracks = rows.filter((r): r is Extract<McRow, { kind: 'track' }> => r.kind === 'track')
287  if (multiDay && !byDay) {
288    hourTo = days.length * 24
289    days.forEach((d, i) => ticks.push({ at: i * 24, label: d.label.split(' ').slice(0, 2).join(' ') }))
290  } else {
291    const hours: number[] = []
292    const work = tracks.some(r => r.bars.some(b => !b.routine))
293    for (const row of tracks) {
294      for (const b of row.bars) {
295        if (work && b.routine) continue
296        for (const [x, y] of b.segs) hours.push((x - row.base) / HOUR, (y - row.base) / HOUR)
297      }
298    }
299    const isToday = !multiDay && nowMs >= snap.range.from && nowMs < snap.range.to
300    if (isToday) hours.push((nowMs - snap.range.from) / HOUR)
301    if (hours.length) {
302      hourFrom = Math.max(0, Math.floor(Math.min(...hours)) - 1)
303      hourTo = Math.min(24, Math.ceil(Math.max(...hours)) + 1)
304    } else {
305      hourFrom = 8
306      hourTo = 18
307    }
308    if (hourTo - hourFrom < 8) hourTo = Math.min(24, hourFrom + 8)
309    if (hourTo - hourFrom < 8) hourFrom = Math.max(0, hourTo - 8)
310    const step = hourTo - hourFrom > 14 ? 2 : 1
311    for (let h = Math.ceil(hourFrom / step) * step; h < hourTo; h += step) ticks.push({ at: h, label: pad(h) })
312  }
313
314  const isToday = nowMs >= snap.range.from && nowMs < snap.range.to
315  const nowBase = !isToday ? null : byDay
316    ? (days.find(d => nowMs >= d.from && nowMs < d.to)?.from ?? null)
317    : snap.range.from
318  // keyboard order: the conversation rows top to bottom, as drawn
319  const order = tracks.filter(r => r.id).map(r => r.id as string)
320  return { rows, hourFrom, hourTo, ticks, now: isToday ? nowMs : null, nowBase, selected: null,
321    order, collapsed: [], legend: legend(snap, view), hidden }
322}
323
324// ---------------------------------------------------------------- timeline as a picture
325// Text colours for a drawing that can't see the app's theme: tuned for the dark theme,
326// still legible on a light one.
327const INK = '#a5adb7'
328const INK_DIM = '#7d8590'
329const GRID = '#8b949e'
330
331const esc = (t: string) => t.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
332/** Roughly how many characters of `size`-px text fit in `px` (proportional font). */
333const fit = (text: string, px: number, size: number) =>
334  truncate(text, Math.max(4, Math.floor(px / (size * 0.56))))
335
336// ---------------------------------------------------------------- timeline as strips
337/**
338 * The timeline as one small vector strip per row, for the desktop: the pane lays the
339 * rows out (a clickable name, then its strip), so names and bars always line up, and
340 * each strip draws just that row's bars — or, over a multi-day range, one shaded
341 * cell per day with the time worked in it. `axis` is the strip of hour/day labels.
342 * Every strip is `width` px wide with the same horizontal scale.
343 */
344export function timelineStrips(t: McTimelineProps, width: number): { axis: string; rows: (string | null)[] } {
345  const W = Math.max(200, Math.round(width))
346  const span = Math.max(1, t.hourTo - t.hourFrom)
347  const multiDay = span > 24
348  const days = multiDay ? Math.round(span / 24) : 0
349  const dayW = multiDay ? W / days : 0
350  const xHour = (h: number) => ((h - t.hourFrom) / span) * W
351  const xAt = (ms: number, base: number) => xHour((ms - base) / HOUR)
352  const svg = (h: number, body: string) =>
353    `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${h}" viewBox="0 0 ${W} ${h}" ` +
354    `font-family="-apple-system, BlinkMacSystemFont, Helvetica, sans-serif">${body}</svg>`
355  // the guide lines, and across days the alternate-day shading, as one repeating pattern:
356  // a few hundred characters per strip however many days or hours it spans. Drawn as
357  // separate lines and boxes they ran to ~4k characters a strip, and a month of
358  // conversations to a pane too large for the desktop to draw at all.
359  const grid = (h: number) => {
360    if (t.ticks.length < 2) return ''
361    const x0 = xHour(t.ticks[0]!.at)
362    const step = xHour(t.ticks[1]!.at) - x0
363    const line = (x: number) =>
364      `<line x1="${x.toFixed(1)}" y1="0" x2="${x.toFixed(1)}" y2="${h}" stroke="${GRID}" stroke-opacity="0.18"/>`
365    const tile = multiDay
366      ? `<rect width="${step.toFixed(2)}" height="${h}" fill="${GRID}" fill-opacity="0.06"/>${line(0.5)}${line(step + 0.5)}`
367      : line(0.5)
368    return `<defs><pattern id="g" x="${x0.toFixed(2)}" y="0" width="${(multiDay ? 2 * step : step).toFixed(2)}" ` +
369      `height="${h}" patternUnits="userSpaceOnUse">${tile}</pattern></defs><rect width="${W}" height="${h}" fill="url(#g)"/>`
370  }
371
372  // a long range has narrow days: label every few so the labels don't run together
373  const labelEvery = multiDay ? Math.max(1, Math.ceil(48 / dayW)) : 1
374  const axis = svg(18, t.ticks.map((tick, i) => {
375    if (i % labelEvery) return ''
376    const x = multiDay ? xHour(tick.at) + dayW / 2 : xHour(tick.at)
377    return `<text x="${x.toFixed(1)}" y="13" fill="${INK_DIM}" font-size="11" text-anchor="middle">${esc(tick.label)}</text>`
378  }).join('') + (t.now !== null && t.nowBase !== null && !multiDay
379    ? `<text x="${(xAt(t.now, t.nowBase) + 3).toFixed(1)}" y="13" fill="#f85149" font-size="10">now</text>` : ''))
380
381  const H = 20
382  type Track = Extract<McRow, { kind: 'track' }>
383  const strip = (row: Track) => {
384    const parts: string[] = [grid(H)]
385    if (multiDay) {
386      for (let d = 0; d < days; d++) {
387        const from = row.base + (t.hourFrom + d * 24) * HOUR
388        const to = from + 24 * HOUR
389        const mins = Math.round(row.bars.reduce((n, bar) => n + bar.segs.reduce((m, [a, b]) =>
390          m + Math.max(0, Math.min(b, to) - Math.max(a, from)) / 60_000, 0), 0))
391        if (!mins) continue
392        const bar = row.bars.find(b => b.segs.some(([a, z]) => a < to && z > from)) ?? row.bars[0]!
393        const strength = 0.5 + 0.5 * Math.min(1, mins / 120)
394        const faded = t.selected !== null && !row.bars.some(b => b.id === t.selected)
395        const cx = d * dayW
396        parts.push(`<rect x="${(cx + 3).toFixed(1)}" y="2" width="${(dayW - 6).toFixed(1)}" height="16" rx="4" ` +
397          `fill="${bar.color}" fill-opacity="${(faded ? strength * 0.35 : strength).toFixed(2)}"/>`)
398        if (dayW >= 44) {
399          parts.push(`<text x="${(cx + dayW / 2).toFixed(1)}" y="14" font-size="10" text-anchor="middle" ` +
400            `fill="${strength > 0.8 && !faded ? '#0d1117' : '#f0f6fc'}">${duration(mins)}</text>`)
401        }
402      }
403    } else {
404      for (const bar of row.bars) {
405        const opacity = t.selected === null ? 0.9 : bar.id === t.selected ? 1 : 0.3
406        const [by, bh] = bar.routine && !row.id ? [14, 3] : [4, 12]
407        for (const [a, b] of bar.segs) {
408          const xa = Math.max(0, xAt(a, row.base))
409          const xb = Math.min(W, xAt(b, row.base))
410          if (xb <= 0 || xa >= W) continue
411          parts.push(`<rect x="${xa.toFixed(1)}" y="${by}" width="${Math.max(3, xb - xa).toFixed(1)}" height="${bh}" ` +
412            `rx="${bh > 4 ? 3 : 1}" fill="${bar.color}" fill-opacity="${opacity}"/>`)
413        }
414      }
415      if (t.now !== null && row.base === t.nowBase) {
416        const x = xAt(t.now, row.base)
417        if (x >= 0 && x <= W) parts.push(`<line x1="${x.toFixed(1)}" y1="0" x2="${x.toFixed(1)}" y2="${H}" stroke="#f85149" stroke-width="1.5"/>`)
418      }
419    }
420    return svg(H, parts.join(''))
421  }
422  // a heading's strip is its whole group's bars in one row: what a folded group shows
423  const rows = t.rows.map((row, i) => {
424    if (row.kind === 'track') return strip(row)
425    const group: Track[] = []
426    for (const r of t.rows.slice(i + 1)) {
427      if (r.kind !== 'track') break
428      group.push(r)
429    }
430    if (!group.length) return null
431    return strip({ kind: 'track', label: row.label, sub: '', base: group[0]!.base, id: null,
432      bars: group.flatMap(r => r.bars) })
433  })
434  return { axis, rows }
435}
436
437/** The rows choices that make sense for a range: "day" only for multi-day ranges. */
438export function rowsOptions(multiDay: boolean): { value: McRowsMode; label: string }[] {
439  const base: { value: McRowsMode; label: string }[] = [
440    { value: 'workstream', label: 'Group: workstream' },
441    { value: 'project', label: 'Group: project' },
442    { value: 'status', label: 'Group: status' },
443    { value: 'kind', label: 'Group: kind of work' },
444    { value: 'category', label: 'Group: work / personal' },
445  ]
446  return multiDay ? [{ value: 'day', label: 'One row per day' }, ...base] : base
447}
448
449
hooks/mission-control/askbar.tsx 139 lines
1// The Ask bar on the desktop: a Client surface module, because the desktop's own Input
2// draws at a fixed narrow width with no way to widen it. This one fills the row: a
3// filled field with a caret, the placeholder while empty, and an Ask pill at the end.
4//
5// A click gives it the keyboard (Escape hands it back). Cmd/Ctrl+A selects the text, so
6// typing replaces it and Backspace clears it; Cmd/Ctrl+C and X copy and cut it through
7// the hooks module, which has the clipboard. The text lives in local state;
8// Enter, or a click on the pill, posts the question to the hooks module, which asks.
9// When the hooks module asks something else (a suggestion was clicked) the field takes
10// that question over, so what's in the field is always what was last asked.
11import type { ClientKeyEvent, ClientModule, ClientPointerEvent, RenderElement } from 'claude-code'
12
13import type { McAskBarProps } from '../../types'
14
15type Local = { text: string; cursor: number; focused: boolean; seen: string; pillHover: boolean; all: boolean }
16
17const FILL = '#262626'
18const FILL_FOCUS = '#2d2d2d'
19const ACCENT = '#a371f7'
20const SELECTED = '#4c3a75'
21const PILL = '#3a3a3a'
22const PILL_HOVER = '#4a4a4a'
23const PILL_W = 9                       // " Ask  ↵ " and a cell of air
24const ICON_W = 3
25
26/** Key names that are keys, not text; anything else that arrives is typed (or pasted). */
27const SPECIAL = new Set(['up', 'down', 'left', 'right', 'return', 'enter', 'tab', 'backspace', 'delete',
28  'pageup', 'pagedown', 'home', 'end', 'escape', 'insert'])
29
30const AskBar: ClientModule<McAskBarProps, Local> = (props, surface) => {
31  const { Box, Text } = surface.elements
32  const columns = surface.columns
33  if (!columns) return <Text dimColor>…</Text>
34
35  const prev = surface.state
36  // a question asked from outside the field (a suggestion) replaces what's typed
37  const st: Local = !prev
38    ? { text: props.question, cursor: props.question.length, focused: false, seen: props.question, pillHover: false,
39        all: false }
40    : prev.seen !== props.question
41      ? { ...prev, text: props.question, cursor: props.question.length, seen: props.question, all: false }
42      : prev
43
44  const set = (patch: Partial<Local>) => surface.setState({ ...st, ...patch })
45  const submit = () => {
46    const q = st.text.trim()
47    if (q && !props.busy) surface.post({ type: 'ask', question: q })
48  }
49  const pillFrom = columns - 1 - PILL_W
50
51  surface.onPointer((ev: ClientPointerEvent) => {
52    const onPill = ev.x >= pillFrom && ev.x < columns - 1
53    if (ev.type === 'down' && ev.button === 'left') {
54      if (onPill) submit()
55      if (!st.focused || st.all) set({ focused: true, all: false })
56    } else if (ev.type === 'move' || ev.type === 'enter') {
57      if (onPill !== st.pillHover) set({ pillHover: onPill })
58    } else if (ev.type === 'leave' && st.pillHover) {
59      set({ pillHover: false })
60    }
61  })
62
63  surface.onKey((ev: ClientKeyEvent) => {
64    const { text, cursor } = st
65    const k = ev.key
66    const mod = ev.meta || ev.ctrl
67    if (k === 'return' || k === 'enter') return submit()
68    if (mod && k === 'a') return set({ all: text.length > 0, cursor: text.length, focused: true })
69    if (mod && (k === 'c' || k === 'x')) {
70      if (!st.all || !text) return
71      surface.post({ type: 'copy', text })
72      if (k === 'x') set({ text: '', cursor: 0, all: false })
73      return
74    }
75    if (st.all) {
76      // the whole text is selected: an edit replaces it, a move drops the selection
77      if (k === 'backspace' || k === 'delete') return set({ text: '', cursor: 0, all: false })
78      if (k === 'left' || k === 'home' || k === 'up') return set({ cursor: 0, all: false })
79      if (k === 'right' || k === 'end' || k === 'down') return set({ cursor: text.length, all: false })
80      if (SPECIAL.has(k) || /^f\d{1,2}$/.test(k) || mod) return set({ all: false })
81      const typed = (k === 'space' ? ' ' : k).replace(/[\r\n\t]+/g, ' ')
82      return set({ text: typed, cursor: typed.length, all: false })
83    }
84    if (k === 'backspace') {
85      if (ev.meta || (ev.ctrl && !ev.shift)) return set({ text: text.slice(cursor), cursor: 0 })
86      if (cursor > 0) set({ text: text.slice(0, cursor - 1) + text.slice(cursor), cursor: cursor - 1 })
87      return
88    }
89    if (k === 'delete') {
90      if (cursor < text.length) set({ text: text.slice(0, cursor) + text.slice(cursor + 1) })
91      return
92    }
93    if (k === 'left') return set({ cursor: ev.meta ? 0 : Math.max(0, cursor - 1) })
94    if (k === 'right') return set({ cursor: ev.meta ? text.length : Math.min(text.length, cursor + 1) })
95    if (k === 'home') return set({ cursor: 0 })
96    if (k === 'end' || (ev.ctrl && k === 'e')) return set({ cursor: text.length })
97    if (ev.ctrl && k === 'u') return set({ text: text.slice(cursor), cursor: 0 })
98    if (SPECIAL.has(k) || /^f\d{1,2}$/.test(k) || ev.ctrl || ev.meta) return
99    const typed = (k === 'space' ? ' ' : k).replace(/[\r\n\t]+/g, ' ')
100    set({ text: text.slice(0, cursor) + typed + text.slice(cursor), cursor: cursor + typed.length, focused: true })
101  })
102
103  // what fits between the icon and the pill; scrolled so the caret stays in view
104  const room = Math.max(4, columns - 2 - ICON_W - PILL_W)
105  const start = Math.max(0, st.cursor - (room - 2))
106  const shown = st.text.slice(start, start + room - 1)
107  const at = st.cursor - start
108
109  let field: RenderElement[]
110  if (!st.text) {
111    field = [
112      ...(st.focused ? [<Text color={ACCENT}>▏</Text>] : []),
113      <Text dimColor wrap="truncate-end">{props.placeholder}</Text>,
114    ]
115  } else if (st.all && !props.busy) {
116    field = [<Text backgroundColor={SELECTED}>{shown}</Text>]
117  } else if (st.focused && !props.busy) {
118    field = [<Text>{shown.slice(0, at)}</Text>, <Text color={ACCENT}>▏</Text>, <Text>{shown.slice(at)}</Text>]
119  } else {
120    field = [<Text wrap="truncate-end">{shown}</Text>]
121  }
122
123  return (
124    <Box flexDirection="row" width={columns} height={3} alignItems="center"
125      backgroundColor={st.focused ? FILL_FOCUS : FILL}>
126      <Box width={1} height={3} />
127      <Box width={ICON_W} height={1}><Text color={ACCENT}>✦</Text></Box>
128      <Box width={room} height={1} flexDirection="row" overflow="hidden">{field}</Box>
129      <Box width={PILL_W} height={1} justifyContent="center"
130        backgroundColor={props.busy ? FILL : st.pillHover ? PILL_HOVER : PILL}>
131        {props.busy ? <Text dimColor>thinking…</Text> : <Text bold>Ask ↵</Text>}
132      </Box>
133      <Box width={1} height={3} />
134    </Box>
135  )
136}
137
138export default AskBar
139
hooks/mission-control/timeline.tsx 211 lines
1// The Mission Control timeline: a Client surface module, so hover and click work on
2// both the desktop and the terminal.
3//
4// Everything is laid out in whole cells as a row of fixed-width boxes — never by
5// padding text with spaces, since the desktop draws text in a proportional font, and
6// never with absolute offsets, which the desktop resolves against the whole region
7// rather than the row. A label column of fixed width, then a track: each run of cells
8// is one box, a solid coloured bar where a session was active, a 1-cell hour guide
9// or "now" mark, or blank space.
10// Clicks and keys go back to the hooks module through `post`; the selection lives in
11// the plugin's state and arrives here as props, only the hover is local.
12import type { ClientModule, ClientPointerEvent, RenderElement } from 'claude-code'
13
14import type { McBar, McRow, McTimelineProps } from '../../types'
15
16type Local = { hover: string | null }
17type Shown = McRow | { kind: 'spacer' }
18
19const HOUR = 3_600_000
20const NOW = '#f85149'
21const SUB_W = 6                     // the active-time column at the end of a label
22
23const Timeline: ClientModule<McTimelineProps, Local> = (props, surface) => {
24  const { Box, Text } = surface.elements
25  const columns = surface.columns
26  if (!columns) return <Text dimColor>…</Text>
27
28  // the title matters more than the hours: it gets most of the row
29  const labelW = Math.max(24, Math.min(90, Math.floor(columns * 0.55)))
30  const trackW = Math.max(12, columns - labelW - 1)
31  const span = Math.max(1, props.hourTo - props.hourFrom)
32  const perHour = trackW / span
33  const cellAt = (ms: number, base: number) => ((ms - base) / HOUR - props.hourFrom) * perHour
34  const hover = surface.state?.hover ?? null
35
36  // a blank line before every heading but the first: the rows as drawn, axis at y 0
37  const shown: Shown[] = []
38  let folded = false
39  props.rows.forEach((row, i) => {
40    if (row.kind === 'header') {
41      if (i > 0) shown.push({ kind: 'spacer' })
42      folded = props.collapsed.includes(row.label)
43      shown.push(row)
44    } else if (!folded) {
45      shown.push(row)
46    }
47  })
48
49  const spans = (row: Extract<McRow, { kind: 'track' }>) =>
50    row.bars.flatMap((b: McBar) => b.segs.map(([a, z]) => {
51      const c0 = Math.max(0, Math.floor(cellAt(a, row.base)))
52      const c1 = Math.min(trackW, Math.max(c0 + 1, Math.ceil(cellAt(z, row.base))))
53      return { bar: b, c0, c1 }
54    })).filter(s => s.c0 < trackW && s.c1 > 0)
55
56  const hit = (x: number, y: number): string | null => {
57    const row = shown[y - 1]
58    if (!row || row.kind !== 'track') return null
59    if (x < labelW) return row.id
60    const col = x - labelW - 1
61    const found = spans(row).find(s => col >= s.c0 - 1 && col < s.c1 + 1)
62    return found ? found.bar.id : row.id
63  }
64
65  surface.onPointer((ev: ClientPointerEvent) => {
66    if (ev.type === 'leave') {
67      if (hover !== null) surface.setState({ hover: null })
68      return
69    }
70    const id = hit(ev.x, ev.y)
71    if (ev.type === 'down' && ev.button === 'left') {
72      const row = shown[ev.y - 1]
73      if (row?.kind === 'header') surface.post({ type: 'toggle', group: row.label })
74      else surface.post({ type: 'select', id })
75      return
76    }
77    if ((ev.type === 'move' || ev.type === 'enter') && id !== hover) surface.setState({ hover: id })
78  })
79
80  surface.onKey(ev => {
81    const order = props.order
82    const at = props.selected ? order.indexOf(props.selected) : -1
83    if (ev.key === 'down' || ev.key === 'right') {
84      surface.post({ type: 'select', id: order[Math.min(order.length - 1, at + 1)] ?? null })
85    } else if (ev.key === 'up' || ev.key === 'left') {
86      surface.post({ type: 'select', id: order[Math.max(0, at - 1)] ?? null })
87    } else if ((ev.key === 'return' || ev.key === 'o') && props.selected) {
88      surface.post({ type: 'open', id: props.selected })
89    }
90  })
91
92  const tickCells = props.ticks
93    .map(t => ({ ...t, c: Math.round((t.at - props.hourFrom) * perHour) }))
94    .filter(t => t.c >= 0 && t.c < trackW)
95  const nowCell = (base: number) =>
96    props.now !== null && props.nowBase === base ? Math.floor(cellAt(props.now, base)) : -1
97  const axisNow = props.nowBase !== null ? nowCell(props.nowBase) : -1
98
99  // the axis: tick labels and the "now" mark, each a box starting at its cell
100  const marks = [
101    ...tickCells.map(t => ({ c: t.c, text: t.label, isNow: false })),
102    ...(axisNow >= 0 ? [{ c: axisNow, text: '▼', isNow: true }] : []),
103  ].sort((a, b) => a.c - b.c)
104  const axisParts: RenderElement[] = []
105  let pos = 0
106  marks.forEach((m, i) => {
107    if (m.c < pos) return                                  // no room left for this one
108    if (m.c > pos) axisParts.push(<Box width={m.c - pos} height={1} />)
109    const next = marks.slice(i + 1).find(n => n.c > m.c)?.c ?? trackW
110    const w = Math.max(1, Math.min(next - m.c, m.isNow ? 1 : m.text.length + 1))
111    axisParts.push(
112      <Box width={w} height={1} overflow="hidden">
113        {m.isNow ? <Text color={NOW}>{m.text}</Text> : <Text dimColor wrap="truncate-end">{m.text}</Text>}
114      </Box>,
115    )
116    pos = m.c + w
117  })
118  const axis = (
119    <Box flexDirection="row" height={1}>
120      <Box width={labelW + 1} height={1} />
121      <Box width={trackW} height={1} flexDirection="row">{axisParts}</Box>
122    </Box>
123  )
124
125  /** One track as consecutive fixed-width boxes. */
126  const track = (row: Extract<McRow, { kind: 'track' }>): RenderElement[] => {
127    const owner: (McBar | null)[] = new Array(trackW).fill(null)
128    for (const s of spans(row)) for (let c = s.c0; c < s.c1; c++) owner[c] = s.bar
129    const ticks = new Set(tickCells.map(t => t.c))
130    const now = nowCell(row.base)
131    const parts: RenderElement[] = []
132    let c = 0
133    while (c < trackW) {
134      const bar = owner[c]
135      if (bar) {
136        let e = c + 1
137        while (e < trackW && owner[e] === bar) e++
138        const lit = props.selected === null || bar.id === props.selected || bar.id === hover
139        parts.push(<Box width={e - c} height={1} backgroundColor={lit ? bar.color : bar.muted} />)
140        c = e
141      } else if (c === now) {
142        parts.push(<Box width={1} height={1}><Text color={NOW}>│</Text></Box>)
143        c += 1
144      } else if (ticks.has(c)) {
145        parts.push(<Box width={1} height={1}><Text dimColor>┆</Text></Box>)
146        c += 1
147      } else {
148        let e = c + 1
149        while (e < trackW && !owner[e] && e !== now && !ticks.has(e)) e++
150        parts.push(<Box width={e - c} height={1} />)
151        c = e
152      }
153    }
154    return parts
155  }
156
157  const lines: RenderElement[] = shown.map(row => {
158    if (row.kind === 'spacer') return <Box height={1} />
159    if (row.kind === 'header') {
160      return (
161        <Box flexDirection="row" height={1} overflow="hidden">
162          <Text bold>{`${props.collapsed.includes(row.label) ? '▸' : '▾'} ${row.label}`}</Text>
163          <Text dimColor wrap="truncate-end">
164            {`${row.sub ? `  ${row.sub}` : ''}${row.note ? `  —  ${row.note}` : ''}`}
165          </Text>
166        </Box>
167      )
168    }
169    const isSel = row.id !== null && row.id === props.selected
170    const isHover = row.id !== null && row.id === hover
171    return (
172      <Box flexDirection="row" height={1}>
173        <Box width={labelW} height={1} flexDirection="row" overflow="hidden">
174          <Box width={2} height={1}>
175            {isSel
176              ? <Text bold>▸</Text>
177              : row.id && row.bars[0] ? <Text color={row.bars[0].color}>●</Text> : <Text> </Text>}
178          </Box>
179          <Box width={labelW - 2 - SUB_W} height={1} overflow="hidden">
180            <Text wrap="truncate-end" bold={isSel} underline={isHover}
181              dimColor={!isSel && !isHover && props.selected !== null}>
182              {row.label}
183            </Text>
184          </Box>
185          <Box width={SUB_W} height={1} justifyContent="flex-end">
186            <Text dimColor>{row.sub}</Text>
187          </Box>
188        </Box>
189        <Box width={1} height={1} />
190        <Box width={trackW} height={1} flexDirection="row">{track(row)}</Box>
191      </Box>
192    )
193  })
194
195  const focus = props.rows
196    .flatMap(r => (r.kind === 'track' ? r.bars : []))
197    .find(b => b.id === (hover ?? props.selected))
198  return (
199    <Box flexDirection="column">
200      {axis}
201      {lines.length ? lines : [<Text dimColor>Nothing in this range.</Text>]}
202      <Box height={1} />
203      <Text dimColor={!focus} wrap="truncate-end">
204        {focus ? focus.tip : 'Hover a bar for details · click a bar or a name to select it · ↑/↓ step · Enter opens'}
205      </Text>
206    </Box>
207  )
208}
209
210export default Timeline
211
types/index.d.ts 214 lines
1// Mission Control's state contract: what src/mission_control.py hands the pane,
2// and the values the pane keeps in `$.state` for the session.
3
4export type McRangeKey = 'today' | 'yesterday' | '7d' | 'custom'
5/** How the timeline groups conversations: by workstream (the digest's epic for each),
6 *  project, status, kind of work, work/personal — or one row per day. */
7export type McRowsMode = 'workstream' | 'project' | 'status' | 'kind' | 'category' | 'day'
8/** What sort of work a conversation was, as the digest's summarizer judged it. */
9export type McKind = 'build' | 'fix' | 'review' | 'investigate' | 'plan' | 'admin'
10/** One of the digest's workstreams (~/.claude/digest/workstreams.json). */
11export type McWorkstream = { name: string; description: string | null; count: number }
12export type McColorMode = 'status' | 'project'
13export type McSegment = [number, number]
14
15export type McSession = {
16  id: string
17  app: string | null
18  apps: (string | null)[]
19  title: string
20  appTitle: string | null
21  project: string
22  root: string | null
23  cwd: string | null
24  category: string
25  status: string
26  changedSinceDigest: boolean
27  digested: boolean
28  gist: string | null
29  open: string | null
30  topics: string[]
31  tickets: string[]
32  branch: string | null
33  segments: McSegment[]
34  first: number
35  last: number
36  activeMin: number
37  scheduled: string | null
38  archived: boolean
39  needsAction: string | null
40  /** the digest's epic for it, and what sort of work it was; null until summarized */
41  workstream: string | null
42  kind: McKind | null
43}
44
45export type McRun = {
46  start: number | null
47  end: number | null
48  status: string
49  detail: string | null
50  app: string | null
51  cli: string | null
52  title: string | null
53}
54
55export type McRoutine = {
56  task: string
57  name: string
58  description: string | null
59  oneTime: boolean
60  runs: McRun[]
61}
62
63export type McDay = { date: string; label: string; from: number; to: number }
64
65export type McLedgerRun = {
66  start: number | null
67  end?: number | null
68  trigger: string | null
69  status: string | null
70  indexed?: number | null
71  renamed?: number | null
72  note?: string | null
73}
74
75export type McIssue = { ts: number | null; kind: string | null; detail: string | null; severity: string }
76
77/** The latest standup brief, read from the standup-brief task's last run. */
78export type McStandup = {
79  app: string | null
80  cli: string | null
81  at: number | null
82  heading: string
83  script: string
84  table: string | null
85  preamble: string | null
86}
87
88export type McSnapshot = {
89  generatedAt: number
90  tookMs: number
91  tzOffsetMin: number
92  range: { key: string; label: string; from: number; to: number; days: McDay[] }
93  sessions: McSession[]
94  totals: { conversations: number; activeMin: number; open: number; new: number; projects: number }
95  routines: McRoutine[]
96  digest: { lastRun: McLedgerRun | null; lastOk: number | null; issues: McIssue[]; issuesSinceOk: number }
97  standup: McStandup | null
98  /** projects a conversation can be moved to */
99  projects: string[]
100  /** the digest's workstreams, most recently seen first */
101  workstreams: McWorkstream[]
102}
103
104/** One turn of a conversation in the reader: the person's message or Claude's reply. */
105export type McTurn = { role: 'user' | 'assistant' | 'note'; ts: number | null; text: string; tools: string[] }
106
107/** A conversation as the reader shows it (collect.py transcript). */
108export type McReader = {
109  id: string
110  mtime?: number
111  total?: number
112  hidden?: number
113  turns?: McTurn[]
114  error?: string
115  /** how many turns were asked for: "Show earlier" raises it */
116  last: number
117  /** whether the messages are unfolded; closed by default, the gist is enough to start */
118  expanded: boolean
119  loading: boolean
120}
121
122/** What the desktop Ask bar (hooks/askbar.tsx) is drawn from. */
123export type McAskBarProps = { placeholder: string; question: string; busy: boolean }
124
125export type McView = {
126  range: McRangeKey
127  rows: McRowsMode
128  color: McColorMode
129  /** a custom range's first and last day, local YYYY-MM-DD, both included */
130  from?: string
131  to?: string
132  /** show only this workstream / this kind of work; absent shows all */
133  ws?: string
134  kind?: McKind
135}
136
137export type McRef = { id: string; app: string | null; title: string }
138
139export type McAsk = {
140  question: string
141  busy: boolean
142  answer: string | null
143  refs: Record<string, McRef>
144  error: string | null
145}
146
147/** What the app's scheduled-tasks server says about a task, when the mod can reach it. */
148export type McTask = {
149  taskId: string
150  enabled: boolean
151  schedule: string | null
152  nextRunAt: string | null
153}
154
155export type McStatus = {
156  loading: boolean
157  error: string | null
158  tasks: McTask[] | null
159}
160
161/** One bar's worth of a session on a timeline row: its active stretches. */
162export type McBar = { id: string; color: string; muted: string; segs: McSegment[]; tip: string; routine?: true }
163
164/**
165 * One row of the timeline: a group heading, or a track (a conversation, a routine,
166 * or a whole day) whose bars are placed against `base`, the local midnight its
167 * hours count from. `id` is set when the row IS one conversation, so its label
168 * selects it too.
169 */
170export type McRow =
171  | { kind: 'header'; label: string; sub: string; note?: string }
172  | { kind: 'track'; label: string; sub: string; base: number; id: string | null; bars: McBar[] }
173
174export type McTick = { at: number; label: string }
175
176export type McTimelineProps = {
177  rows: McRow[]
178  hourFrom: number
179  hourTo: number
180  ticks: McTick[]
181  now: number | null
182  nowBase: number | null
183  selected: string | null
184  order: string[]
185  /** group headings whose conversations are folded away */
186  collapsed: string[]
187}
188
189declare module 'claude-code' {
190  interface PluginState {
191    'convo-digest': {
192      snap: McSnapshot | null
193      view: McView
194      selected: string | null
195      ask: McAsk
196      status: McStatus
197      /** whether the one-line "Mission Control" band shows above the prompt */
198      band: boolean
199      /** whether the standup brief is unfolded in full */
200      standupOpen: boolean
201      /** when Summarize now handed the digest a turn, until that turn completes */
202      summarizing: number | null
203      /** timeline groups folded away (by heading); empty, the default, is all unfolded */
204      collapsed: string[]
205      /** the app session an Archive or Pin click handed a turn for, until that turn completes */
206      acting: string | null
207      /** the conversation open in the reader, and what was read of it */
208      reader: McReader | null
209      /** the conversation a Summarize click handed a digest turn for, until that turn ends */
210      resummarizing: string | null
211    }
212  }
213}
214