feat(v0.3): O2 — resume past Claude sessions

- src/http/history.ts: listSessions() reads ~/.claude/projects/*/*.jsonl
  (mtime-sorted, top 50), parseSessionMeta() extracts cwd + first user prompt
  (pure, 4 unit tests); GET /sessions
- terminal-session: opts.initialInput (typed ~700ms after the shell is ready)
- tabs.newTabForResume(cwd,id): new tab in the project dir running
  'claude --resume <id>'
- public/history.ts: 🕘 panel listing sessions (project · time · preview) with
  Resume buttons
- Verified: /sessions returns 44 real sessions; panel lists project/time/preview.
  216 tests green.
This commit is contained in:
Yaojia Wang
2026-06-18 08:04:04 +02:00
parent 25269cad3f
commit e36ca272ed
8 changed files with 366 additions and 1 deletions

109
public/history.ts Normal file
View File

@@ -0,0 +1,109 @@
/**
* public/history.ts — resume a past Claude Code session (O2).
*
* A 🕘 toolbar button opens a list of past sessions (GET /sessions); clicking
* "Resume" opens a new tab in that project's directory and runs
* `claude --resume <id>`.
*/
export interface HistoryHooks {
resume: (cwd: string, id: string) => void
}
interface HistorySession {
id: string
cwd: string
project: string
mtimeMs: number
preview: string
}
function relTime(ms: number): string {
const s = Math.max(0, (Date.now() - ms) / 1000)
if (s < 60) return 'just now'
if (s < 3600) return `${Math.floor(s / 60)}m ago`
if (s < 86400) return `${Math.floor(s / 3600)}h ago`
return `${Math.floor(s / 86400)}d ago`
}
export function mountHistory(toolbar: HTMLElement, hooks: HistoryHooks): void {
const overlay = document.createElement('div')
overlay.id = 'historymodal'
overlay.style.display = 'none'
const card = document.createElement('div')
card.className = 'hist-card'
overlay.appendChild(card)
document.body.appendChild(overlay)
const hide = (): void => {
overlay.style.display = 'none'
}
overlay.addEventListener('click', (e) => {
if (e.target === overlay) hide()
})
const open = async (): Promise<void> => {
card.replaceChildren()
const title = document.createElement('div')
title.className = 'hist-title'
title.textContent = 'Resume a Claude session'
card.appendChild(title)
const status = document.createElement('div')
status.className = 'hist-empty'
status.textContent = 'Loading…'
card.appendChild(status)
overlay.style.display = 'flex'
let sessions: HistorySession[]
try {
const res = await fetch('/sessions')
sessions = (await res.json()) as HistorySession[]
} catch {
status.textContent = 'Failed to load sessions.'
return
}
if (sessions.length === 0) {
status.textContent = 'No past Claude sessions found.'
return
}
status.remove()
for (const s of sessions) {
const row = document.createElement('div')
row.className = 'hist-row'
const main = document.createElement('div')
main.className = 'hist-main'
const proj = document.createElement('div')
proj.className = 'hist-proj'
proj.textContent = `${s.project} · ${relTime(s.mtimeMs)}`
const prev = document.createElement('div')
prev.className = 'hist-prev'
prev.textContent = s.preview || '(no preview)'
prev.title = s.cwd
main.append(proj, prev)
const btn = document.createElement('button')
btn.className = 'hist-resume'
btn.textContent = 'Resume'
btn.addEventListener('click', () => {
hooks.resume(s.cwd, s.id)
hide()
})
row.append(main, btn)
card.appendChild(row)
}
}
const toggle = document.createElement('button')
toggle.className = 'toolbtn'
toggle.textContent = '🕘'
toggle.title = 'Resume a past Claude session'
toggle.setAttribute('aria-label', 'Session history')
toggle.addEventListener('click', () => {
if (overlay.style.display === 'none') void open()
else hide()
})
toolbar.appendChild(toggle)
}

View File

@@ -17,6 +17,7 @@ import { mountSearch } from './search.js'
import { mountQrConnect } from './qr.js'
import { mountSettings, loadSettings, type Settings } from './settings.js'
import { mountDashboard } from './dashboard.js'
import { mountHistory } from './history.js'
const paneHost = document.getElementById('term')
const tabs = document.getElementById('tabs')
@@ -51,6 +52,9 @@ mountDashboard(toolbar, {
snapshot: () => app.snapshot(),
focus: (idx) => app.focusTab(idx),
})
mountHistory(toolbar, {
resume: (cwd, id) => app.newTabForResume(cwd, id),
})
mountQrConnect(toolbar)
// PWA: register the service worker (installable + offline shell, M4).

View File

@@ -253,6 +253,73 @@ html, body {
font-weight: 600;
}
/* History / resume modal (O2) */
#historymodal {
position: fixed;
inset: 0;
z-index: 1200;
display: flex;
align-items: flex-start;
justify-content: center;
padding-top: 56px;
background: rgba(0, 0, 0, 0.6);
}
.hist-card {
background: #222;
border: 1px solid #3a3a3a;
border-radius: 10px;
min-width: 360px;
max-width: 92vw;
max-height: 72vh;
overflow-y: auto;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}
.hist-title {
padding: 12px 16px;
font-weight: 600;
color: #fff;
border-bottom: 1px solid #3a3a3a;
}
.hist-empty {
padding: 16px;
color: #aaa;
}
.hist-row {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 16px;
border-bottom: 1px solid #2a2a2a;
}
.hist-main {
flex: 1 1 auto;
min-width: 0;
}
.hist-proj {
color: #fff;
font-size: 13px;
}
.hist-prev {
color: #aaa;
font-size: 12px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.hist-resume {
flex: none;
background: #2ea043;
color: #fff;
border: none;
border-radius: 5px;
padding: 6px 14px;
cursor: pointer;
font: inherit;
}
.hist-resume:hover {
background: #3fb950;
}
.tab {
display: flex;
align-items: center;

View File

@@ -151,7 +151,12 @@ export class TabApp {
/* ── tab lifecycle ───────────────────────────────────────────── */
private addEntry(sessionId: string | null, customTitle: string | null, cwd?: string): TabEntry {
private addEntry(
sessionId: string | null,
customTitle: string | null,
cwd?: string,
initialInput?: string,
): TabEntry {
const entry: TabEntry = {
session: null as unknown as TerminalSession,
customTitle,
@@ -162,6 +167,7 @@ export class TabApp {
entry.session = new TerminalSession({
sessionId,
...(cwd !== undefined ? { cwd } : {}),
...(initialInput !== undefined ? { initialInput } : {}),
onSessionId: () => this.persist(),
// onActivity only fires for hidden (inactive) panes (see TerminalSession).
onActivity: () => {
@@ -198,6 +204,14 @@ export class TabApp {
this.activate(this.tabs.length - 1)
}
/** O2: open a new tab in `cwd` and run `claude --resume <id>`. */
newTabForResume(cwd: string, sessionId: string): void {
this.addEntry(null, null, cwd || undefined, `claude --resume ${sessionId}`)
this.persist()
this.rebuild()
this.activate(this.tabs.length - 1)
}
activate(i: number): void {
if (i < 0 || i >= this.tabs.length) return
this.maybeAskNotify() // first switch is a user gesture — request notif permission

View File

@@ -54,6 +54,8 @@ export interface TerminalSessionOpts {
onClaudeStatus?: (status: ClaudeStatus, detail?: string) => void
/** Optional: spawn a NEW session in this directory ("new tab here", M6). */
cwd?: string
/** Optional: type this once the new session's shell is ready (O2 resume). */
initialInput?: string
}
export class TerminalSession {
@@ -69,6 +71,8 @@ export class TerminalSession {
private readonly onStatus: ((status: SessionStatus) => void) | undefined
private readonly onClaudeStatus: ((status: ClaudeStatus, detail?: string) => void) | undefined
private readonly spawnCwd: string | undefined
private readonly initialInput: string | undefined
private initialSent = false
private statusValue: SessionStatus = 'connecting'
private claudeStatusValue: ClaudeStatus = 'unknown'
private cwdValue: string | null = null
@@ -94,6 +98,7 @@ export class TerminalSession {
this.onStatus = opts.onStatus
this.onClaudeStatus = opts.onClaudeStatus
this.spawnCwd = opts.cwd
this.initialInput = opts.initialInput
this.el = document.createElement('div')
this.el.className = 'term-pane'
@@ -238,6 +243,12 @@ export class TerminalSession {
this.onSessionId(msg.sessionId)
const dims = this.safefit()
if (dims !== null) this.sendResize(dims.cols, dims.rows)
// O2: once the shell is up, type the resume command (only on first attach).
if (this.initialInput !== undefined && !this.initialSent) {
this.initialSent = true
const cmd = this.initialInput
setTimeout(() => this.send(cmd), 700)
}
break
}
case 'output': {