feat(v0.3): M6 — new tab opens in the active tab's directory

- title-util.cwdFromOsc7: parse cwd from OSC 7 (file:// URL); 4 unit tests
- terminal-session: register OSC 7 handler → cwd getter; opts.cwd → sent in the
  attach frame on a fresh session
- protocol/types: ClientMessage attach.cwd (absolute path); validated
- session/manager/server: thread cwd → createSession spawn cwd (defaults homeDir)
- tabs: '+' (newTab) opens in the active tab's reported cwd, falls back to home
- Verified: server attach.cwd spawns there; cd /private/tmp → + → new tab pwd is
  /private/tmp. 212 tests green.
This commit is contained in:
Yaojia Wang
2026-06-18 07:14:52 +02:00
parent f79f14b89d
commit 87e11734d8
9 changed files with 97 additions and 13 deletions

View File

@@ -14,7 +14,7 @@ import { FitAddon } from '@xterm/addon-fit'
import { SearchAddon } from '@xterm/addon-search'
import { WebLinksAddon } from '@xterm/addon-web-links'
import type { ClaudeStatus, ClientMessage, ServerMessage } from '../src/types.js'
import { folderFromTitle } from './title-util.js'
import { folderFromTitle, cwdFromOsc7 } from './title-util.js'
const RESET = '\x1b[0m'
const BOLD = '\x1b[1m'
@@ -54,6 +54,8 @@ export interface TerminalSessionOpts {
onStatus?: (status: SessionStatus) => void
/** Optional: fired when Claude Code activity changes (from a hook, H2). */
onClaudeStatus?: (status: ClaudeStatus, detail?: string) => void
/** Optional: spawn a NEW session in this directory ("new tab here", M6). */
cwd?: string
}
export class TerminalSession {
@@ -68,8 +70,10 @@ export class TerminalSession {
private readonly onTitle: ((title: string) => void) | undefined
private readonly onStatus: ((status: SessionStatus) => void) | undefined
private readonly onClaudeStatus: ((status: ClaudeStatus, detail?: string) => void) | undefined
private readonly spawnCwd: string | undefined
private statusValue: SessionStatus = 'connecting'
private claudeStatusValue: ClaudeStatus = 'unknown'
private cwdValue: string | null = null
private pendingApprovalValue = false
private pendingToolValue: string | undefined = undefined
@@ -91,6 +95,7 @@ export class TerminalSession {
this.onTitle = opts.onTitle
this.onStatus = opts.onStatus
this.onClaudeStatus = opts.onClaudeStatus
this.spawnCwd = opts.cwd
this.el = document.createElement('div')
this.el.className = 'term-pane'
@@ -111,6 +116,12 @@ export class TerminalSession {
this.term.onData((data) => this.send(data))
// Tab title = current folder, derived from the shell's OSC title.
this.term.onTitleChange((title) => this.onTitle?.(folderFromTitle(title) ?? title))
// OSC 7 reports the full cwd (M6) — used for "new tab here".
this.term.parser.registerOscHandler(7, (payload) => {
const c = cwdFromOsc7(payload)
if (c !== null) this.cwdValue = c
return true
})
this.resizeObserver = new ResizeObserver(() => this.scheduleResize())
this.resizeObserver.observe(this.el)
@@ -131,6 +142,11 @@ export class TerminalSession {
return this.claudeStatusValue
}
/** Current working directory reported by the shell via OSC 7 (M6), or null. */
get cwd(): string | null {
return this.cwdValue
}
/** Whether a tool approval is held server-side and can be resolved (H3). */
get pendingApproval(): boolean {
return this.pendingApprovalValue
@@ -189,7 +205,13 @@ export class TerminalSession {
this.reconnectDelay = 1000
this.setStatus('connected')
this.term.write(statusLine(`${GREEN}${BOLD}Connected${RESET}`))
socket.send(buildMessage({ type: 'attach', sessionId: this.sessionId }))
// Send cwd only on a fresh session (M6 "new tab here"); on reconnect the
// sessionId is already set and the server ignores cwd.
const attachMsg: ClientMessage =
this.sessionId === null && this.spawnCwd !== undefined
? { type: 'attach', sessionId: null, cwd: this.spawnCwd }
: { type: 'attach', sessionId: this.sessionId }
socket.send(buildMessage(attachMsg))
})
socket.addEventListener('message', (event: MessageEvent<string>) => {