feat: ClawTap v0.2.0

Interactive Prompts:
- Unified InteractivePrompt type across all 3 adapters (Claude/Codex/Gemini)
- InteractivePromptOverlay component with options, text input, countdown
- Gemini + Codex pane monitors detect tool confirmation, ask user, plan approval
- respondInteractivePrompt routing: permission → respondPermission, options → _selectOption
- Claude AskUserQuestion nested questions[0] structure parsing

Cross-AI Review:
- Client-generated reviewId, removed pendingReview state
- FloatingReviewPanel uses CSS display:none instead of unmount (keeps hooks alive)
- Child review sessions default to YOLO/bypass permission mode
- Send back to parent, send to existing/new review, tab switching, end review
- Collapsed review cards with read-only panel for ended reviews
- Full reconnect support: active + ended reviews restore correctly

AskUserQuestion Tool Card UI:
- Dedicated renderer replaces raw JSON display
- Options shown with selected (green) / unselected (gray) indicators
- Free text answers shown in quoted format with green border
- Collapsed summary: question → answer
- Shared parseAskQuestionInput utility (client + server)
- Historical tool results attached via _result on tool_use blocks

Adapter Fixes:
- Session→adapter mapping persisted in SQLite (survives server restart)
- SESSION_CREATED deferred for pendingRekey adapters (Codex/Gemini)
- session-rekeyed handler sends complete SESSION_CREATED with adapter + cwd
- Gemini: auto-accept folder trust, privacy notice, IDE nudge, YOLO * prompt
- Claude: auto-accept bypass permissions confirmation (v2.1.85+)
- Port fallback (EADDRINUSE → try +1), statusLine shell script wrapper

Other:
- Desktop Enter sends / Shift+Enter newline; Mobile Enter newline
- Strip CLAWTAP_REF marker from session list
- Active sessions tab shows adapter badge
- Rename CLAUDE_UI_PASSWORD → CLAWTAP_PASSWORD

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
kuannnn
2026-03-27 14:46:00 +08:00
parent 16f75379af
commit 0fcf66fc22
50 changed files with 2179 additions and 400 deletions
+44 -29
View File
@@ -30,14 +30,11 @@ interface HookIdentifiers {
interface ClaudeSettings {
hooks?: Record<string, HookEntry[]>;
statusLine?: { type: string; command: string };
_clawtapOriginalStatusLine?: string;
_clawtapOriginalStatusLine?: string; // legacy, cleaned up on uninstall
[key: string]: unknown;
}
export class ClaudeHookConfig {
/** Shared between install() wrapper construction and _extractOriginalFromWrapper() */
private static readonly WRAPPER_TAIL = `fi; printf '%s' "$input" | `;
port: number | string;
useHttps: boolean;
@@ -79,16 +76,14 @@ export class ClaudeHookConfig {
existing.hooks[event] = [...filtered, ...configs];
}
// Wrap statusLine to also POST to our server (non-blocking).
// - Has custom statusLine → wrap it (POST + original coexist)
// Insert our statusLine script into the pipe chain (if not already there).
// Our script is a passthrough: reads stdin, POSTs to server (background), outputs stdin.
// - Has custom statusLine → pipe through our script first
// - No custom statusLine → don't touch it, preserve Claude Code built-in
const wrapperScript = this._ensureStatusLineScript(statuslineUrl);
const existingCmd = existing.statusLine?.command || '';
if (existingCmd && !existingCmd.includes(`:${port}/api/hooks/claude/statusline`)) {
existing._clawtapOriginalStatusLine = existingCmd;
const portCheck = this._portCheckCmd();
const curlK = this.useHttps ? ' -k' : '';
const wrapperCmd = `input=$(cat); if ${portCheck}; then printf '%s' "$input" | curl -sf${curlK} -X POST -H 'Content-Type:application/json' -d @- ${statuslineUrl} &>/dev/null & ${ClaudeHookConfig.WRAPPER_TAIL}${existingCmd}`;
existing.statusLine = { type: 'command', command: wrapperCmd };
if (existingCmd && !existingCmd.includes(wrapperScript)) {
existing.statusLine = { type: 'command', command: `${wrapperScript} | ${existingCmd}` };
console.log(`[hooks] Wrapped statusLine to POST to ${statuslineUrl}`);
}
@@ -129,19 +124,23 @@ export class ClaudeHookConfig {
if (Object.keys(existing.hooks).length === 0) delete existing.hooks;
}
// --- Restore statusLine (independent of hooks) ---
// Restore original statusLine: try extraction from wrapper first (most reliable),
// then fall back to backup field, then delete only if truly no original existed.
if (existing.statusLine?.command?.includes(portTag)) {
const original = this._extractOriginalFromWrapper(existing.statusLine.command);
if (original) {
existing.statusLine = { type: 'command', command: original };
} else if (existing._clawtapOriginalStatusLine) {
existing.statusLine = { type: 'command', command: existing._clawtapOriginalStatusLine };
// --- Restore statusLine: remove our script from the pipe chain ---
const wrapperScript = this._statusLineScriptPath();
if (existing.statusLine?.command?.includes(wrapperScript)) {
// Remove our script + pipe from the command string
const restored = existing.statusLine.command
.replace(`${wrapperScript} | `, '')
.replace(wrapperScript, '')
.replace(/\s*\|\s*$/, '') // trailing pipe
.replace(/^\s*\|\s*/, '') // leading pipe
.trim();
if (restored) {
existing.statusLine = { type: 'command', command: restored };
} else {
delete existing.statusLine;
}
}
// Clean up legacy backup field from old versions
delete existing._clawtapOriginalStatusLine;
writeFileSync(settingsPath, JSON.stringify(existing, null, 2));
@@ -160,14 +159,30 @@ export class ClaudeHookConfig {
};
}
/** Extract the original statusLine command from our wrapper using WRAPPER_TAIL. */
private _extractOriginalFromWrapper(cmd: string): string | null {
const tail = ClaudeHookConfig.WRAPPER_TAIL;
const idx = cmd.lastIndexOf(tail);
if (idx < 0) return null;
const original = cmd.substring(idx + tail.length).trim();
if (!original || original.includes('/api/hooks/claude')) return null;
return original;
/** Path to our statusLine wrapper script */
private _statusLineScriptPath(): string {
return join(homedir(), '.clawtap', 'hooks', 'claude-statusline.sh');
}
/** Create or update the statusLine wrapper script */
private _ensureStatusLineScript(statuslineUrl: string): string {
const scriptPath = this._statusLineScriptPath();
const scriptDir = join(homedir(), '.clawtap', 'hooks');
mkdirSync(scriptDir, { recursive: true });
const portCheck = this._portCheckCmd();
const curlInsecure = this.useHttps ? ' -k' : '';
const script = `#!/bin/bash
input=$(cat)
# POST to ClawTap server (non-blocking, skip if server not running)
if ${portCheck}; then
printf '%s' "$input" | curl -sf${curlInsecure} --connect-timeout 2 --max-time 5 -X POST -H 'Content-Type:application/json' -d @- ${statuslineUrl} &>/dev/null &
fi
# Pass through to stdout
printf '%s' "$input"
`;
writeFileSync(scriptPath, script, { mode: 0o755 });
return scriptPath;
}
private _isOurHookEntry(entry: HookEntry, portTag: string): boolean {
+2 -1
View File
@@ -79,10 +79,10 @@ export class ClaudeAdapter extends IAdapter {
}
setup(app: Express): void {
this.installHooks();
this._registerHookRoutes(app);
}
setHookPort(port: number | string): void { this._hookConfig.port = port; }
installHooks(): void { this._hookConfig.install(); }
uninstallHooks(): void { this._hookConfig.uninstall(); }
@@ -206,6 +206,7 @@ export class ClaudeAdapter extends IAdapter {
async switchPermissionMode(sid: string, mode: string): Promise<boolean> { return this._tmux.switchPermissionMode(sid, mode); }
respondPermission(reqId: string, behavior: PermissionBehavior): void { this._tmux.respondPermission(reqId, behavior); }
async respondQuestion(reqId: string, answer: string): Promise<void> { return this._tmux.respondQuestion(reqId, answer); }
respondInteractivePrompt(reqId: string, opt?: string, text?: string): void { this._tmux.respondInteractivePrompt(reqId, opt, text); }
releaseAllPending(sid: string): void { this._tmux.releaseAllPending(sid); }
resolveAllPendingAs(sid: string, behavior: PermissionBehavior): void { this._tmux.resolveAllPendingAs(sid, behavior); }
+18 -2
View File
@@ -139,6 +139,7 @@ export async function getMessages(sessionId: string, dir?: string): Promise<GetM
try {
const messages: unknown[] = [];
const subToolMap: Map<string, SubToolBlock[]> = new Map(); // parentToolUseId → sub-tool blocks
const toolUseIndex: Map<string, ContentBlock> = new Map(); // tool_use id → content block
const stream = createReadStream(filePath);
const rl = createInterface({ input: stream, crlfDelay: Infinity });
try {
@@ -162,9 +163,24 @@ export async function getMessages(sessionId: string, dir?: string): Promise<GetM
if (entry.type === 'assistant') {
if (isNoResponseMessage(text)) continue;
messages.push(entry.message);
// Index tool_use blocks for O(1) result attachment
if (Array.isArray(content)) {
for (const block of content as ContentBlock[]) {
if (block.type === 'tool_use' && block.id) toolUseIndex.set(block.id, block);
}
}
} else if (entry.type === 'user') {
// Skip messages containing tool results (not needed for display)
if (Array.isArray(content) && content.some((b: ContentBlock) => b.type === 'tool_result')) continue;
// Attach tool results to their matching tool_use blocks
const toolResults = Array.isArray(content)
? (content as ContentBlock[]).filter((b: ContentBlock) => b.type === 'tool_result' && b.tool_use_id)
: [];
if (toolResults.length > 0) {
for (const block of toolResults) {
const match = toolUseIndex.get(block.tool_use_id as string);
if (match) match._result = block;
}
continue;
}
// Skip system/CLI messages (empty text, system patterns)
if (isSystemMessage(text, content)) continue;
// Convert "Implement the following plan:" messages to plan type
+39
View File
@@ -611,6 +611,28 @@ export class TmuxAdapter extends EventEmitter {
}
}
respondInteractivePrompt(requestId: string, selectedOption?: string, textValue?: string): void {
if (textValue != null) {
this.respondQuestion(requestId, textValue);
} else if (selectedOption != null) {
// Permission behaviors are named ('allow', 'allow_session', 'deny')
// Question options are numeric indices ('0', '1', '2')
const isPermission = ['allow', 'allow_session', 'deny'].includes(selectedOption);
if (isPermission) {
this.respondPermission(requestId, selectedOption as any);
} else {
// Numeric index — validate before consuming the pending entry
const index = parseInt(selectedOption);
if (isNaN(index)) return;
const pending = this._permissions.resolveQuestion(requestId);
if (!pending) return;
const session = this.sessions.get(pending.sessionId);
if (!session) return;
this._selectOption(session.windowId, index).catch(() => {});
}
}
}
/**
* Respond to the CLI's plan approval selector.
* Options: 0=bypass (auto-accept edits), 1=manually approve, 2=text feedback
@@ -876,6 +898,23 @@ export class TmuxAdapter extends EventEmitter {
if (attempt <= 3 || attempt % 5 === 0) {
console.log(`[adapter] waitForReady #${attempt}: window=${windowId} prompt=${hasPrompt} lines=${lineCount}`);
}
// Auto-accept bypass permissions confirmation prompt (Claude v2.1.85+).
// Detect by structure (numbered selection list) + context (bypass permissions).
const isSelectionPrompt = /\s+\d+\./.test(content);
const isBypassPrompt = /[Bb]ypass\s+[Pp]ermissions/.test(content);
if (isSelectionPrompt && isBypassPrompt) {
const acceptMatch = content.match(/(\d+)\.\s+Yes/);
const acceptOption = acceptMatch ? parseInt(acceptMatch[1]) : 2;
console.log(`[adapter] Bypass permissions prompt detected, selecting option ${acceptOption}`);
for (let i = 1; i < acceptOption; i++) {
await tmuxManager.sendControl(windowId, 'Down');
await new Promise<void>(r => setTimeout(r, 50));
}
await tmuxManager.sendControl(windowId, 'Enter');
await new Promise<void>(r => setTimeout(r, 500));
continue;
}
if (hasPrompt && lineCount >= 3) {
console.log(`[adapter] CLI ready for ${windowId} in ${Date.now() - start}ms`);
await new Promise<void>(r => setTimeout(r, 300));