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:
@@ -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 {
|
||||
|
||||
@@ -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); }
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -19,6 +19,7 @@ import type { ReconnectState } from '../../types/adapter.js';
|
||||
import type { ActiveSessionInfo } from '../interface.js';
|
||||
import { isLargeContent } from '../interface.js';
|
||||
import { PermissionManager } from '../../permission-manager.js';
|
||||
import { findActiveSession } from '../shared/find-active-session.js';
|
||||
import { readFile } from 'fs/promises';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -149,7 +150,7 @@ export class CodexTmuxAdapter extends EventEmitter {
|
||||
|
||||
this._startMonitor(finalId, windowId);
|
||||
|
||||
return { sessionId: finalId };
|
||||
return { sessionId: finalId, pendingRekey: finalId === tempKey };
|
||||
}
|
||||
|
||||
async resumeSession(sessionId: string, cwd: string, options: QueryOptions = {}): Promise<{ sessionId: string }> {
|
||||
@@ -606,6 +607,23 @@ export class CodexTmuxAdapter extends EventEmitter {
|
||||
await tmuxManager.sendKeys(session.windowId, answer, true);
|
||||
}
|
||||
|
||||
respondInteractivePrompt(requestId: string, selectedOption?: string, textValue?: string): void {
|
||||
const pending = this._permissions.resolvePermission(requestId)
|
||||
|| this._permissions.resolveQuestion(requestId);
|
||||
const sessionId = pending?.sessionId || findActiveSession(this.sessions);
|
||||
if (!sessionId) return;
|
||||
const session = this.sessions.get(sessionId);
|
||||
if (!session) return;
|
||||
|
||||
if (selectedOption != null) {
|
||||
// Codex uses single-key shortcuts (y, a, p, d, n)
|
||||
tmuxManager.sendKeys(session.windowId, selectedOption, false).catch(() => {});
|
||||
}
|
||||
if (textValue != null) {
|
||||
tmuxManager.sendKeys(session.windowId, textValue, true).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
/** Release all pending requests for a session (e.g., when Mobile disconnects). */
|
||||
releaseAllPending(sessionId: string): void {
|
||||
this._permissions.dismissAll(sessionId);
|
||||
|
||||
@@ -87,10 +87,10 @@ export class CodexAdapter 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(); }
|
||||
|
||||
@@ -164,6 +164,7 @@ export class CodexAdapter 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); }
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { join } from 'path';
|
||||
import { homedir } from 'os';
|
||||
import { createReadStream } from 'fs';
|
||||
import { createInterface } from 'readline';
|
||||
import { stripMarker } from '../shared/content-utils.js';
|
||||
import { CodexTranscriptParser } from './transcript-parser.js';
|
||||
import type { CodexJsonlEntry } from './transcript-parser.js';
|
||||
import type { DirectoryEntry, MessagesResult } from '../interface.js';
|
||||
@@ -202,7 +203,7 @@ export async function getSessions(dir?: string, limit?: number): Promise<Session
|
||||
cwd,
|
||||
lastModified: entry.ts * 1000, // Convert to ms timestamp
|
||||
firstPrompt: entry.text
|
||||
? entry.text.replace(/^(?:\[CLAWTAP_REF:[^\]]+\]|\d+\])(?:\\n|\n)?/, '').slice(0, 200)
|
||||
? stripMarker(entry.text).slice(0, 200)
|
||||
: null,
|
||||
model,
|
||||
};
|
||||
|
||||
@@ -13,6 +13,16 @@
|
||||
// that will be refined through empirical testing with the actual Codex TUI.
|
||||
|
||||
import { EventEmitter } from 'events';
|
||||
import { InteractivePrompt } from '../../types/messages.js';
|
||||
|
||||
function simpleHash(str: string): string {
|
||||
let hash = 0;
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
hash = ((hash << 5) - hash) + str.charCodeAt(i);
|
||||
hash |= 0;
|
||||
}
|
||||
return Math.abs(hash).toString(36);
|
||||
}
|
||||
|
||||
/** Minimal interface for the tmux manager dependency */
|
||||
interface TmuxCapture {
|
||||
@@ -42,6 +52,7 @@ export class CodexPaneMonitor {
|
||||
private interval: ReturnType<typeof setInterval> | null = null;
|
||||
private _lastContent: string = '';
|
||||
private _lastResponseText: string = '';
|
||||
private lastPromptId: string | null = null;
|
||||
|
||||
constructor(
|
||||
sessionId: string,
|
||||
@@ -84,7 +95,19 @@ export class CodexPaneMonitor {
|
||||
if (content === this._lastContent) return;
|
||||
this._lastContent = content;
|
||||
|
||||
// 1. Check for approval prompt (highest priority — blocks everything)
|
||||
// 0. Check for interactive prompt (highest priority)
|
||||
const interactivePrompt = this._detectPrompt(content);
|
||||
if (interactivePrompt) {
|
||||
if (interactivePrompt.requestId !== this.lastPromptId) {
|
||||
this.lastPromptId = interactivePrompt.requestId;
|
||||
this.emitter.emit('interactive-prompt', this.sessionId, interactivePrompt);
|
||||
}
|
||||
return; // Don't process streaming while prompt is showing
|
||||
} else if (this.lastPromptId) {
|
||||
this.lastPromptId = null;
|
||||
}
|
||||
|
||||
// 1. Check for approval prompt (legacy — kept for backwards compat)
|
||||
const approval = detectApprovalPrompt(content);
|
||||
if (approval) {
|
||||
this.emitter.emit('approval-prompt', this.sessionId, approval);
|
||||
@@ -108,6 +131,78 @@ export class CodexPaneMonitor {
|
||||
// Silently ignore — tmux window may have been killed
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect an interactive prompt in the Codex CLI pane content.
|
||||
* Returns an InteractivePrompt if one is detected, null otherwise.
|
||||
*/
|
||||
private _detectPrompt(content: string): InteractivePrompt | null {
|
||||
// Command/File/Network Approval: "(y)" with proceed/run/make patterns
|
||||
if (
|
||||
content.includes('(y)') &&
|
||||
(/proceed/i.test(content) || /Would you like to run/i.test(content) || /Would you like to make/i.test(content))
|
||||
) {
|
||||
const options = this._parseCodexOptions(content);
|
||||
const lines = content.split('\n');
|
||||
const tail = lines.slice(-20);
|
||||
const promptLine = tail.find(l => /proceed|\brun\b|\bmake\b/i.test(l)) || 'Approve action';
|
||||
const description = tail.join('\n').trim();
|
||||
return {
|
||||
requestId: `codex-perm-${simpleHash(description)}`,
|
||||
promptType: 'permission',
|
||||
title: typeof promptLine === 'string' ? promptLine.trim() : 'Approve action',
|
||||
description,
|
||||
options: options.length > 0 ? options : [
|
||||
{ value: 'y', label: 'Yes' },
|
||||
{ value: 'n', label: 'No' },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
// User Input: "enter to submit" AND "esc to cancel" (but NOT approval patterns)
|
||||
if (
|
||||
/enter to submit/i.test(content) &&
|
||||
/esc to cancel/i.test(content) &&
|
||||
!content.includes('(y)')
|
||||
) {
|
||||
const lines = content.split('\n');
|
||||
const tail = lines.slice(-20);
|
||||
const options = this._parseCodexOptions(content);
|
||||
const description = tail.join('\n').trim();
|
||||
if (options.length > 0) {
|
||||
return {
|
||||
requestId: `codex-ask-${simpleHash(description)}`,
|
||||
promptType: 'question',
|
||||
title: 'User Input',
|
||||
description,
|
||||
options,
|
||||
};
|
||||
}
|
||||
return {
|
||||
requestId: `codex-ask-${simpleHash(description)}`,
|
||||
promptType: 'question',
|
||||
title: 'User Input',
|
||||
description,
|
||||
textInput: { placeholder: 'Type your response...' },
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse Codex-style options from content.
|
||||
* Matches patterns like "(y) Yes" or "(a) Always approve".
|
||||
*/
|
||||
private _parseCodexOptions(content: string): { value: string; label: string }[] {
|
||||
const results: { value: string; label: string }[] = [];
|
||||
const regex = /\(([a-z])\)\s+(.+?)(?:\n|$)/g;
|
||||
let match: RegExpExecArray | null;
|
||||
while ((match = regex.exec(content)) !== null) {
|
||||
results.push({ value: match[1]!, label: match[2]!.trim() });
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
@@ -19,6 +19,7 @@ import type { ReconnectState } from '../../types/adapter.js';
|
||||
import type { ActiveSessionInfo } from '../interface.js';
|
||||
import { isLargeContent } from '../interface.js';
|
||||
import { PermissionManager } from '../../permission-manager.js';
|
||||
import { findActiveSession } from '../shared/find-active-session.js';
|
||||
import { readFile } from 'fs/promises';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -116,12 +117,13 @@ export class GeminiTmuxAdapter extends EventEmitter {
|
||||
|
||||
// === Session Lifecycle ===
|
||||
|
||||
async startSession(cwd: string, options: QueryOptions = {}): Promise<{ sessionId: string }> {
|
||||
async startSession(cwd: string, options: QueryOptions = {}): Promise<{ sessionId: string; pendingRekey?: boolean }> {
|
||||
const mode = options.permissionMode || 'default';
|
||||
const parts = ['gemini', '--approval-mode', this._toCliApprovalMode(mode)];
|
||||
if (options.model) parts.push('-m', options.model);
|
||||
|
||||
const tempName = `gemini-${Date.now()}`;
|
||||
console.log(`[gemini-tmux] startSession: tempName=${tempName} cwd=${cwd} mode=${mode}`);
|
||||
const windowId = await tmuxManager.createWindow(tempName, cwd, parts.join(' '));
|
||||
|
||||
// Register session BEFORE _waitForReady — SessionStart hook fires during
|
||||
@@ -137,14 +139,16 @@ export class GeminiTmuxAdapter extends EventEmitter {
|
||||
const rekeyed = [...this.sessions.entries()].find(([, s]) => s.windowId === windowId)?.[0];
|
||||
if (rekeyed) {
|
||||
finalId = rekeyed;
|
||||
console.log(`[gemini-tmux] startSession: rekeyed ${tempName} → ${rekeyed}`);
|
||||
} else {
|
||||
console.warn(`[gemini-tmux] Session ${tempName} vanished during startup (windowId=${windowId})`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[gemini-tmux] startSession: finalId=${finalId} pendingRekey=${finalId === tempName}`);
|
||||
this._startMonitor(finalId, windowId);
|
||||
|
||||
return { sessionId: finalId };
|
||||
return { sessionId: finalId, pendingRekey: finalId === tempName };
|
||||
}
|
||||
|
||||
async resumeSession(sessionId: string, cwd: string, options: QueryOptions = {}): Promise<{ sessionId: string }> {
|
||||
@@ -581,6 +585,27 @@ export class GeminiTmuxAdapter extends EventEmitter {
|
||||
await tmuxManager.sendKeys(session.windowId, answer, true);
|
||||
}
|
||||
|
||||
respondInteractivePrompt(requestId: string, selectedOption?: string, textValue?: string): void {
|
||||
const pending = this._permissions.resolvePermission(requestId)
|
||||
|| this._permissions.resolveQuestion(requestId);
|
||||
// For pane-monitor-detected prompts, there may be no pending entry — find session from active sessions
|
||||
const sessionId = pending?.sessionId || findActiveSession(this.sessions);
|
||||
if (!sessionId) return;
|
||||
const session = this.sessions.get(sessionId);
|
||||
if (!session) return;
|
||||
|
||||
if (selectedOption != null) {
|
||||
const index = parseInt(selectedOption);
|
||||
if (!isNaN(index)) {
|
||||
// Gemini numbered options: navigate Down × index, then Enter
|
||||
this._selectNumberedOption(session.windowId, index).catch(() => {});
|
||||
}
|
||||
}
|
||||
if (textValue != null) {
|
||||
tmuxManager.sendKeys(session.windowId, textValue, true).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
/** Release all pending requests for a session */
|
||||
releaseAllPending(sessionId: string): void {
|
||||
this._permissions.dismissAll(sessionId);
|
||||
@@ -598,6 +623,14 @@ export class GeminiTmuxAdapter extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
private async _selectNumberedOption(windowId: string, targetIndex: number): Promise<void> {
|
||||
for (let i = 0; i < targetIndex; i++) {
|
||||
await tmuxManager.sendControl(windowId, 'Down');
|
||||
await new Promise<void>(r => setTimeout(r, 50));
|
||||
}
|
||||
await tmuxManager.sendControl(windowId, 'Enter');
|
||||
}
|
||||
|
||||
// === Cleanup ===
|
||||
|
||||
async destroy(): Promise<void> {
|
||||
@@ -662,7 +695,7 @@ export class GeminiTmuxAdapter extends EventEmitter {
|
||||
* Wait for Gemini CLI to be ready.
|
||||
* Polls tmux pane content until a prompt indicator appears.
|
||||
*/
|
||||
private async _waitForReady(windowId: string, timeoutMs: number = 30000): Promise<void> {
|
||||
private async _waitForReady(windowId: string, timeoutMs: number = 60000): Promise<void> {
|
||||
const start = Date.now();
|
||||
let attempt = 0;
|
||||
while (Date.now() - start < timeoutMs) {
|
||||
@@ -670,17 +703,69 @@ export class GeminiTmuxAdapter extends EventEmitter {
|
||||
try {
|
||||
const content = await tmuxManager.capturePane(windowId);
|
||||
const lines = content.split('\n');
|
||||
// Gemini shows > or similar prompt indicator
|
||||
const hasPrompt = lines.some(l => /^\s*[>❯]/.test(l));
|
||||
const lineCount = lines.filter(l => l.trim()).length;
|
||||
if (attempt <= 3 || attempt % 5 === 0) {
|
||||
console.log(`[gemini-tmux] waitForReady #${attempt}: window=${windowId} prompt=${hasPrompt} lines=${lineCount}`);
|
||||
}
|
||||
if (hasPrompt && lineCount >= 2) {
|
||||
// Gemini shows > (default), * (yolo), or ❯ as prompt indicator
|
||||
const hasPrompt = lines.some(l => /^\s*[>*❯]/.test(l));
|
||||
|
||||
if (hasPrompt) {
|
||||
console.log(`[gemini-tmux] CLI ready for ${windowId} in ${Date.now() - start}ms`);
|
||||
await new Promise<void>(r => setTimeout(r, 300));
|
||||
return;
|
||||
}
|
||||
|
||||
// Privacy Notice or Terms of Service popup — dismiss with Esc
|
||||
if (!hasPrompt && (content.includes('Privacy Notice') ||
|
||||
(content.includes('Terms of Service') && !content.includes('trust the files')))) {
|
||||
console.log('[gemini-tmux] Privacy/ToS notice detected, dismissing');
|
||||
await tmuxManager.sendControl(windowId, 'Escape');
|
||||
await new Promise<void>(r => setTimeout(r, 500));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Multi-folder trust dialog ("trust the following folders")
|
||||
if (!hasPrompt && content.includes('trust the following folders')) {
|
||||
console.log('[gemini-tmux] Multi-folder trust detected, accepting');
|
||||
await tmuxManager.sendControl(windowId, 'Enter');
|
||||
await new Promise<void>(r => setTimeout(r, 1000));
|
||||
continue;
|
||||
}
|
||||
|
||||
// IDE integration nudge — decline
|
||||
if (!hasPrompt && content.includes('Do you want to connect') && content.includes('Gemini CLI')) {
|
||||
console.log('[gemini-tmux] IDE nudge detected, declining');
|
||||
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;
|
||||
}
|
||||
|
||||
// Auto-accept folder trust prompt (Gemini asks on first use in a directory).
|
||||
// Only runs when prompt is NOT yet visible.
|
||||
if (content.includes('trust the files') && content.includes('Trust folder')) {
|
||||
const parentMatch = content.match(/(\d+)\.\s+Trust parent folder/);
|
||||
if (parentMatch) {
|
||||
const targetOption = parseInt(parentMatch[1]);
|
||||
console.log(`[gemini-tmux] Folder trust prompt detected, selecting option ${targetOption} (Trust parent folder)`);
|
||||
for (let i = 1; i < targetOption; i++) {
|
||||
await tmuxManager.sendControl(windowId, 'Down');
|
||||
await new Promise<void>(r => setTimeout(r, 50));
|
||||
}
|
||||
} else {
|
||||
console.log(`[gemini-tmux] Folder trust prompt detected, accepting default (Trust folder)`);
|
||||
}
|
||||
await tmuxManager.sendControl(windowId, 'Enter');
|
||||
await new Promise<void>(r => setTimeout(r, 1000));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (attempt <= 3 || attempt % 5 === 0) {
|
||||
const lineCount = lines.filter(l => l.trim()).length;
|
||||
console.log(`[gemini-tmux] waitForReady #${attempt}: window=${windowId} prompt=${hasPrompt} lines=${lineCount}`);
|
||||
if (lineCount > 0) {
|
||||
const nonEmpty = lines.filter(l => l.trim());
|
||||
console.log(`[gemini-tmux] waitForReady content: first="${nonEmpty[0]?.substring(0, 60)}" last="${nonEmpty[nonEmpty.length - 1]?.substring(0, 60)}"`);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(`[gemini-tmux] waitForReady #${attempt}: ERROR ${(err as Error).message}`);
|
||||
}
|
||||
|
||||
@@ -73,10 +73,10 @@ export class GeminiAdapter 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(); }
|
||||
|
||||
@@ -167,6 +167,7 @@ export class GeminiAdapter 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); }
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { readdirSync, readFileSync, statSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { homedir } from 'os';
|
||||
import { extractUserText } from './message-utils.js';
|
||||
import { stripMarker } from '../shared/content-utils.js';
|
||||
import type { DirectoryEntry } from '../interface.js';
|
||||
import type { SessionInfo } from '../../types/adapter.js';
|
||||
|
||||
@@ -123,7 +124,7 @@ export function getSessions(dir?: string, limit?: number): SessionInfo[] {
|
||||
if (m.type === 'user' && m.content != null) {
|
||||
const text = extractUserText(m.content);
|
||||
if (text.trim()) {
|
||||
firstPrompt = text.slice(0, 200);
|
||||
firstPrompt = stripMarker(text).slice(0, 200);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -223,7 +224,6 @@ export function getSessionMessages(
|
||||
const projectName = getProjectName(dir);
|
||||
if (projectName) {
|
||||
const chatsDir = join(GEMINI_TMP_DIR, projectName, 'chats');
|
||||
// Try exact match first, then scan
|
||||
try {
|
||||
const files = readdirSync(chatsDir);
|
||||
for (const file of files) {
|
||||
@@ -245,6 +245,8 @@ export function getSessionMessages(
|
||||
// chats dir not readable
|
||||
}
|
||||
}
|
||||
// Fallback: project name mapping failed — scan all projects
|
||||
if (!filePath) filePath = findSessionFile(sessionId);
|
||||
} else {
|
||||
filePath = findSessionFile(sessionId);
|
||||
}
|
||||
|
||||
@@ -16,6 +16,16 @@
|
||||
// that will be refined through empirical testing with the actual Gemini CLI.
|
||||
|
||||
import { EventEmitter } from 'events';
|
||||
import { InteractivePrompt } from '../../types/messages.js';
|
||||
|
||||
function simpleHash(str: string): string {
|
||||
let hash = 0;
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
hash = ((hash << 5) - hash) + str.charCodeAt(i);
|
||||
hash |= 0;
|
||||
}
|
||||
return Math.abs(hash).toString(36);
|
||||
}
|
||||
|
||||
/** Minimal interface for the tmux manager dependency */
|
||||
interface TmuxCapture {
|
||||
@@ -44,6 +54,7 @@ export class GeminiPaneMonitor {
|
||||
private interval: ReturnType<typeof setInterval> | null = null;
|
||||
private _lastContent: string = '';
|
||||
private _lastResponseText: string = '';
|
||||
private lastPromptId: string | null = null;
|
||||
|
||||
constructor(
|
||||
sessionId: string,
|
||||
@@ -86,6 +97,20 @@ export class GeminiPaneMonitor {
|
||||
if (content === this._lastContent) return;
|
||||
this._lastContent = content;
|
||||
|
||||
const lines = content.split('\n');
|
||||
|
||||
// 0. Check for interactive prompt (highest priority)
|
||||
const prompt = this._detectPrompt(content, lines);
|
||||
if (prompt) {
|
||||
if (prompt.requestId !== this.lastPromptId) {
|
||||
this.lastPromptId = prompt.requestId;
|
||||
this.emitter.emit('interactive-prompt', this.sessionId, prompt);
|
||||
}
|
||||
return; // Don't process streaming while prompt is showing
|
||||
} else if (this.lastPromptId) {
|
||||
this.lastPromptId = null;
|
||||
}
|
||||
|
||||
// 1. Check for thinking indicator
|
||||
const thinking = detectThinking(content);
|
||||
if (thinking) {
|
||||
@@ -103,6 +128,103 @@ export class GeminiPaneMonitor {
|
||||
// Silently ignore — tmux window may have been killed
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect an interactive prompt in the Gemini CLI pane content.
|
||||
* Returns an InteractivePrompt if one is detected, null otherwise.
|
||||
*/
|
||||
private _detectPrompt(content: string, _lines: string[]): InteractivePrompt | null {
|
||||
// Tool Confirmation: "Action Required" with numbered options
|
||||
if (content.includes('Action Required') && /●\s+\d+\./.test(content)) {
|
||||
const description = this._extractBetween(content, 'Action Required', '●');
|
||||
const options = this._parseNumberedOptions(content);
|
||||
return {
|
||||
requestId: `gemini-perm-${simpleHash(description)}`,
|
||||
promptType: 'permission',
|
||||
title: 'Action Required',
|
||||
description: description.trim(),
|
||||
options,
|
||||
};
|
||||
}
|
||||
|
||||
// Plan Approval: "Approval" with "Yes" and "feedback"
|
||||
if (content.includes('Approval') && /Yes/.test(content) && /feedback/i.test(content)) {
|
||||
const description = this._extractBetween(content, 'Approval', '●');
|
||||
const options = this._parseNumberedOptions(content);
|
||||
return {
|
||||
requestId: `gemini-plan-${simpleHash(description)}`,
|
||||
promptType: 'plan',
|
||||
title: 'Plan Approval',
|
||||
description: description.trim(),
|
||||
options,
|
||||
textInput: { placeholder: 'Provide feedback...' },
|
||||
};
|
||||
}
|
||||
|
||||
// AskUser: "Answer Questions"
|
||||
if (content.includes('Answer Questions')) {
|
||||
const description = this._extractBetween(content, 'Answer Questions', '●');
|
||||
const options = this._parseNumberedOptions(content);
|
||||
if (options.length > 0) {
|
||||
return {
|
||||
requestId: `gemini-ask-${simpleHash(description)}`,
|
||||
promptType: 'question',
|
||||
title: 'Answer Questions',
|
||||
description: description.trim(),
|
||||
options,
|
||||
};
|
||||
}
|
||||
return {
|
||||
requestId: `gemini-ask-${simpleHash(description)}`,
|
||||
promptType: 'question',
|
||||
title: 'Answer Questions',
|
||||
description: description.trim(),
|
||||
textInput: { placeholder: 'Type your answer...' },
|
||||
};
|
||||
}
|
||||
|
||||
// Loop Detection: "potential loop was detected"
|
||||
if (content.includes('potential loop was detected')) {
|
||||
const options = this._parseNumberedOptions(content);
|
||||
return {
|
||||
requestId: `gemini-loop-${simpleHash('loop-detected')}`,
|
||||
promptType: 'loop-detected',
|
||||
title: 'Loop Detected',
|
||||
description: 'A potential loop was detected.',
|
||||
options,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse numbered options from Gemini CLI content.
|
||||
* Matches patterns like "● 1. Allow this action" or "1. Allow this action".
|
||||
* Returns 0-based index values.
|
||||
*/
|
||||
private _parseNumberedOptions(content: string): { value: string; label: string }[] {
|
||||
const results: { value: string; label: string }[] = [];
|
||||
const regex = /(?:●\s+)?(\d+)\.\s+(.+?)(?:\n|$)/g;
|
||||
let match: RegExpExecArray | null;
|
||||
while ((match = regex.exec(content)) !== null) {
|
||||
const index = parseInt(match[1]!, 10);
|
||||
results.push({ value: String(index - 1), label: match[2]!.trim() });
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract text between two markers in the content.
|
||||
*/
|
||||
private _extractBetween(content: string, start: string, end: string): string {
|
||||
const startIdx = content.indexOf(start);
|
||||
if (startIdx === -1) return '';
|
||||
const afterStart = startIdx + start.length;
|
||||
const endIdx = content.indexOf(end, afterStart);
|
||||
if (endIdx === -1) return content.slice(afterStart).trim();
|
||||
return content.slice(afterStart, endIdx).trim();
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
@@ -178,7 +300,7 @@ export function extractResponseText(content: string): string {
|
||||
// Gemini user prompt patterns (conservative):
|
||||
// - ">" or "❯" at start of line followed by user text
|
||||
// - "user:" prefix
|
||||
if (/^\s*[>❯]\s+\S/.test(line) || /^\s*user:\s/i.test(line)) {
|
||||
if (/^\s*[>*❯]\s+\S/.test(line) || /^\s*user:\s/i.test(line)) {
|
||||
lastUserPrompt = i;
|
||||
break;
|
||||
}
|
||||
@@ -204,7 +326,7 @@ export function extractResponseText(content: string): string {
|
||||
// Horizontal rules
|
||||
/^[─━═\-]{5,}/.test(line.trim()) ||
|
||||
// New user prompt
|
||||
/^\s*[>❯]\s+\S/.test(line) ||
|
||||
/^\s*[>*❯]\s+\S/.test(line) ||
|
||||
// Spinner/thinking indicators (braille set)
|
||||
/^\s*[⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏]\s*/.test(line)
|
||||
) {
|
||||
|
||||
@@ -92,7 +92,7 @@ export class IAdapter extends EventEmitter {
|
||||
|
||||
// --- Session Lifecycle ---
|
||||
|
||||
async startSession(cwd: string, options?: QueryOptions): Promise<{ sessionId: string }> { throw new Error('Not implemented: startSession'); }
|
||||
async startSession(cwd: string, options?: QueryOptions): Promise<{ sessionId: string; pendingRekey?: boolean }> { throw new Error('Not implemented: startSession'); }
|
||||
async resumeSession(sessionId: string, cwd: string, options?: QueryOptions): Promise<{ sessionId: string }> { throw new Error('Not implemented: resumeSession'); }
|
||||
async attachSession(sessionId: string, cwd: string, options?: QueryOptions): Promise<{ sessionId: string }> { throw new Error('Not implemented: attachSession'); }
|
||||
async destroySession(sessionId: string): Promise<void> { throw new Error('Not implemented: destroySession'); }
|
||||
@@ -155,10 +155,14 @@ export class IAdapter extends EventEmitter {
|
||||
|
||||
// --- Hooks ---
|
||||
|
||||
/** Update the port used by hooks (called when port fallback changes the port). */
|
||||
setHookPort(port: number | string): void {}
|
||||
/** Install adapter-specific hooks (e.g., write to CLI settings). No server needed. */
|
||||
installHooks(): void {}
|
||||
/** Remove adapter-specific hooks. No server needed. */
|
||||
uninstallHooks(): void {}
|
||||
/** Respond to an interactive prompt (permission, question, plan, etc). */
|
||||
respondInteractivePrompt(requestId: string, selectedOption?: string, textValue?: string): void {}
|
||||
|
||||
// --- Lifecycle ---
|
||||
|
||||
|
||||
@@ -75,6 +75,14 @@ export function initAll(app: Express): Map<string, IAdapter> {
|
||||
return adapters;
|
||||
}
|
||||
|
||||
/** Install hooks with confirmed port (called after server.listen succeeds) */
|
||||
export function installAllHooks(port: number | string): void {
|
||||
for (const [, adapter] of adapters) {
|
||||
adapter.setHookPort(port);
|
||||
adapter.installHooks();
|
||||
}
|
||||
}
|
||||
|
||||
export function getAll(): Map<string, IAdapter> {
|
||||
return adapters;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
/** Parse Claude's AskUserQuestion nested input structure into a flat format */
|
||||
export function parseAskQuestionInput(input: any): {
|
||||
question: string;
|
||||
header?: string;
|
||||
options?: { label: string; value: string }[];
|
||||
} {
|
||||
const q = input?.questions?.[0] || input || {};
|
||||
const question = q.question || q.text || input?.question || input?.text || '';
|
||||
const header = q.header;
|
||||
const rawOpts = q.options || input?.options;
|
||||
const options = Array.isArray(rawOpts) && rawOpts.length > 0
|
||||
? rawOpts.map((o: any, i: number) => ({
|
||||
value: typeof o === 'string' ? String(i) : (o.value ?? String(i)),
|
||||
label: typeof o === 'string' ? o : (o.label || o.text || `Option ${i + 1}`),
|
||||
}))
|
||||
: undefined;
|
||||
return { question, header, options };
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
/** Strip [CLAWTAP_REF:...] and [CODETAP_REF:...] markers from message text. */
|
||||
const MARKER_REGEX = /^(?:\[(?:CLAWTAP_REF|CODETAP_REF):[^\]]+\]|\d+\])(?:\\n|\n)?/;
|
||||
|
||||
export function stripMarker(text: string): string {
|
||||
return text.replace(MARKER_REGEX, '');
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Find the session most likely showing an interactive prompt.
|
||||
* Checks for actively processing sessions first, then falls back to most recent.
|
||||
*/
|
||||
export function findActiveSession(
|
||||
sessions: Map<string, { isProcessing: boolean; lastActivity: number | null }>
|
||||
): string | null {
|
||||
for (const [id, session] of sessions) {
|
||||
if (session.isProcessing) return id;
|
||||
}
|
||||
let latest: string | null = null;
|
||||
let latestTime = 0;
|
||||
for (const [id, session] of sessions) {
|
||||
if (session.lastActivity && session.lastActivity > latestTime) {
|
||||
latestTime = session.lastActivity;
|
||||
latest = id;
|
||||
}
|
||||
}
|
||||
return latest;
|
||||
}
|
||||
+3
-3
@@ -22,12 +22,12 @@ export interface AppConfig {
|
||||
}
|
||||
|
||||
export function loadConfig(): AppConfig {
|
||||
const password = process.env.CLAUDE_UI_PASSWORD;
|
||||
const password = process.env.CLAWTAP_PASSWORD;
|
||||
if (!password) {
|
||||
throw new Error(
|
||||
'CLAUDE_UI_PASSWORD is required.\n' +
|
||||
'CLAWTAP_PASSWORD is required.\n' +
|
||||
'Set it and try again:\n' +
|
||||
' export CLAUDE_UI_PASSWORD=your-password'
|
||||
' export CLAWTAP_PASSWORD=your-password'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,11 @@ export function initDB(config: AppConfig): void {
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_reviews_parent ON session_reviews(parent_cli_session_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS session_adapters (
|
||||
session_id TEXT PRIMARY KEY,
|
||||
adapter TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS saved_instructions (
|
||||
id TEXT PRIMARY KEY,
|
||||
label TEXT NOT NULL,
|
||||
@@ -122,6 +127,8 @@ interface PreparedStatements {
|
||||
instructionCreate: BetterSqlite3.Statement;
|
||||
instructionGetAll: BetterSqlite3.Statement;
|
||||
instructionDelete: BetterSqlite3.Statement;
|
||||
sessionAdapterSet: BetterSqlite3.Statement;
|
||||
sessionAdapterGet: BetterSqlite3.Statement;
|
||||
}
|
||||
|
||||
let _stmts: PreparedStatements | null = null;
|
||||
@@ -202,6 +209,12 @@ function stmts(): PreparedStatements {
|
||||
instructionDelete: d.prepare(
|
||||
`DELETE FROM saved_instructions WHERE id = ?`
|
||||
),
|
||||
sessionAdapterSet: d.prepare(
|
||||
`INSERT OR REPLACE INTO session_adapters (session_id, adapter) VALUES (?, ?)`
|
||||
),
|
||||
sessionAdapterGet: d.prepare(
|
||||
`SELECT adapter FROM session_adapters WHERE session_id = ?`
|
||||
),
|
||||
};
|
||||
}
|
||||
return _stmts;
|
||||
@@ -284,6 +297,18 @@ export const preferences = {
|
||||
|
||||
// --- Session Review Operations ---
|
||||
|
||||
// --- Session → Adapter Mapping (persists across restarts) ---
|
||||
|
||||
export const sessionAdapters = {
|
||||
set(sessionId: string, adapter: string): void {
|
||||
stmts().sessionAdapterSet.run(sessionId, adapter);
|
||||
},
|
||||
get(sessionId: string): string | null {
|
||||
const row = stmts().sessionAdapterGet.get(sessionId) as { adapter: string } | undefined;
|
||||
return row?.adapter ?? null;
|
||||
},
|
||||
};
|
||||
|
||||
let _childIdCache: Set<string> | null = null;
|
||||
|
||||
export const sessionReviews = {
|
||||
|
||||
+42
-11
@@ -12,7 +12,7 @@ import {
|
||||
authMiddleware,
|
||||
} from './auth.js';
|
||||
import './adapters/init.js';
|
||||
import { initAll, listAvailable, get as getAdapter, getAll as getAllAdapters, cleanupAll, DEFAULT_ADAPTER } from './adapters/registry.js';
|
||||
import { initAll, installAllHooks, listAvailable, get as getAdapter, getAll as getAllAdapters, cleanupAll, DEFAULT_ADAPTER } from './adapters/registry.js';
|
||||
import { initPush, getVapidPublicKey, saveSubscription, removeSubscription, getPendingSessions } from './push.js';
|
||||
import {
|
||||
setupSessionManager,
|
||||
@@ -25,7 +25,7 @@ import {
|
||||
import { WebSocketTransport } from './transport/websocket-transport.js';
|
||||
import { loadConfig } from './config.js';
|
||||
import type { AppConfig } from './config.js';
|
||||
import { initDB, closeDB, sessionReviews, savedInstructions } from './db.js';
|
||||
import { initDB, closeDB, sessionReviews, sessionAdapters, savedInstructions } from './db.js';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
@@ -99,6 +99,10 @@ async function start(): Promise<void> {
|
||||
)
|
||||
);
|
||||
const allSessions = results.flat();
|
||||
// Persist session→adapter mapping so server knows which adapter owns each session
|
||||
for (const s of allSessions) {
|
||||
if (s.sessionId && s.adapter) sessionAdapters.set(s.sessionId, s.adapter);
|
||||
}
|
||||
allSessions.sort((a, b) => {
|
||||
const aTime = typeof a.lastModified === 'number' ? a.lastModified : new Date(a.lastModified || 0).getTime();
|
||||
const bTime = typeof b.lastModified === 'number' ? b.lastModified : new Date(b.lastModified || 0).getTime();
|
||||
@@ -251,9 +255,9 @@ async function start(): Promise<void> {
|
||||
// Register a review after the child session is already created via QUERY
|
||||
app.post('/api/reviews/register', authMiddleware, async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { parentCliSessionId, childSessionId, targetAdapter, anchorMessageId, prompt, title } = req.body;
|
||||
if (!parentCliSessionId || !childSessionId) {
|
||||
return res.status(400).json({ error: 'parentCliSessionId and childSessionId required' });
|
||||
const { reviewId, parentCliSessionId, childSessionId, targetAdapter, anchorMessageId, prompt, title } = req.body;
|
||||
if (!reviewId || !parentCliSessionId || !childSessionId) {
|
||||
return res.status(400).json({ error: 'reviewId, parentCliSessionId and childSessionId required' });
|
||||
}
|
||||
|
||||
// Find which adapter owns the parent session
|
||||
@@ -262,7 +266,6 @@ async function start(): Promise<void> {
|
||||
if (a.getSession(parentCliSessionId)) { parentAdapterName = name; break; }
|
||||
}
|
||||
|
||||
const reviewId = randomUUID();
|
||||
sessionReviews.create(reviewId, parentCliSessionId, childSessionId, targetAdapter, parentAdapterName, anchorMessageId, prompt, title);
|
||||
|
||||
// Ensure adapter mapping exists for the child session
|
||||
@@ -436,21 +439,49 @@ async function start(): Promise<void> {
|
||||
}
|
||||
});
|
||||
|
||||
// Initialize all adapters (registers hook routes, configures CLI hooks)
|
||||
// Register adapter routes (before listen — routes don't depend on port)
|
||||
initAll(app);
|
||||
|
||||
setupSessionManager();
|
||||
|
||||
// --- Initialize and Listen ---
|
||||
// --- Find available port and Listen ---
|
||||
|
||||
await initAuth(config);
|
||||
initPush(config);
|
||||
writeFileSync(config.paths.pid, String(process.pid));
|
||||
|
||||
const protocol = config.https ? 'https' : 'http';
|
||||
server.listen(config.port, '0.0.0.0', () => {
|
||||
console.log(`ClawTap running on ${protocol}://0.0.0.0:${config.port}${config.https ? ' (HTTPS)' : ''}`);
|
||||
const actualPort = await new Promise<number>((resolve, reject) => {
|
||||
const maxRetries = 10;
|
||||
let attempt = 0;
|
||||
function tryListen(port: number) {
|
||||
const onError = (err: NodeJS.ErrnoException) => {
|
||||
if (err.code === 'EADDRINUSE' && attempt < maxRetries) {
|
||||
attempt++;
|
||||
const nextPort = port + 1;
|
||||
console.log(`Port ${port} in use, trying ${nextPort}...`);
|
||||
server.close(() => tryListen(nextPort));
|
||||
} else {
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
server.once('error', onError);
|
||||
server.listen(port, '0.0.0.0', () => {
|
||||
server.removeListener('error', onError);
|
||||
resolve(port);
|
||||
});
|
||||
}
|
||||
tryListen(config.port);
|
||||
});
|
||||
|
||||
// Update config with actual port (may differ if fallback occurred)
|
||||
config.port = actualPort;
|
||||
|
||||
// Install hooks AFTER port is confirmed (hooks embed the port in CLI configs)
|
||||
installAllHooks(actualPort);
|
||||
|
||||
writeFileSync(config.paths.pid, String(process.pid));
|
||||
console.log(`ClawTap running on ${protocol}://0.0.0.0:${actualPort}${config.https ? ' (HTTPS)' : ''}`);
|
||||
|
||||
// --- Graceful Shutdown ---
|
||||
|
||||
async function shutdown(signal: string): Promise<void> {
|
||||
|
||||
+127
-34
@@ -5,7 +5,8 @@ import type { ClientMessage, QueryOptions, PermissionBehavior } from './types/me
|
||||
import { sendPush, incrementPending, clearPending, getPendingSessions } from './push.js';
|
||||
import { basename } from 'path';
|
||||
import type { ClientConnection } from './transport/client-connection.js';
|
||||
import { sessionReviews } from './db.js';
|
||||
import { sessionReviews, sessionAdapters } from './db.js';
|
||||
import { parseAskQuestionInput } from './adapters/shared/ask-question-utils.js';
|
||||
|
||||
/** Push notification options */
|
||||
interface PushOptions {
|
||||
@@ -94,14 +95,48 @@ export function setupSessionManager(): void {
|
||||
triggerPush(adapter, sessionId, { title: 'Claude finished', body: 'Turn complete', tagPrefix: 'idle' });
|
||||
});
|
||||
|
||||
adapter.on('permission-request', (sessionId: string, data: { toolName?: string; [key: string]: unknown }) => {
|
||||
broadcast(sessionId, { type: WS.PERMISSION_REQUEST, ...data });
|
||||
adapter.on('permission-request', (sessionId: string, data: { requestId?: string; toolName?: string; input?: any; [key: string]: unknown }) => {
|
||||
// Convert legacy Claude hook event to InteractivePrompt format
|
||||
broadcast(sessionId, {
|
||||
type: WS.INTERACTIVE_PROMPT,
|
||||
requestId: data.requestId || `perm-${Date.now()}`,
|
||||
promptType: 'permission',
|
||||
title: 'Permission Request',
|
||||
description: `${data.toolName || 'Tool'} wants to execute`,
|
||||
toolName: data.toolName,
|
||||
toolInput: data.input,
|
||||
options: [
|
||||
{ value: 'allow', label: 'Allow' },
|
||||
{ value: 'allow_session', label: 'Allow All' },
|
||||
{ value: 'deny', label: 'Deny' },
|
||||
],
|
||||
});
|
||||
triggerPush(adapter, sessionId, { title: 'Permission needed', body: data.toolName || 'tool', tagPrefix: 'perm' });
|
||||
});
|
||||
|
||||
adapter.on('ask-question', (sessionId: string, data: { toolName?: string; [key: string]: unknown }) => {
|
||||
broadcast(sessionId, { type: WS.PERMISSION_REQUEST, ...data });
|
||||
triggerPush(adapter, sessionId, { title: 'Question from Claude', body: 'Waiting for answer', tagPrefix: 'ask' });
|
||||
adapter.on('ask-question', (sessionId: string, data: { requestId?: string; toolName?: string; input?: any; [key: string]: unknown }) => {
|
||||
const parsed = parseAskQuestionInput(data.input || {});
|
||||
broadcast(sessionId, {
|
||||
type: WS.INTERACTIVE_PROMPT,
|
||||
requestId: data.requestId || `ask-${Date.now()}`,
|
||||
promptType: 'question',
|
||||
title: parsed.header || 'Question',
|
||||
description: parsed.question,
|
||||
toolName: 'AskUserQuestion',
|
||||
toolInput: data.input || {},
|
||||
options: parsed.options,
|
||||
textInput: parsed.options ? undefined : { placeholder: 'Enter your response...' },
|
||||
});
|
||||
triggerPush(adapter, sessionId, { title: 'Question', body: questionText.substring(0, 50) || 'Waiting for answer', tagPrefix: 'ask' });
|
||||
});
|
||||
|
||||
adapter.on('interactive-prompt', (sessionId: string, prompt: any) => {
|
||||
broadcast(sessionId, { type: WS.INTERACTIVE_PROMPT, ...prompt });
|
||||
const pushTitle = prompt.promptType === 'permission' ? 'Permission needed'
|
||||
: prompt.promptType === 'question' ? 'Question'
|
||||
: prompt.promptType === 'plan' ? 'Plan approval'
|
||||
: 'Action needed';
|
||||
triggerPush(adapter, sessionId, { title: pushTitle, body: prompt.title || '', tagPrefix: 'prompt' });
|
||||
});
|
||||
|
||||
adapter.on('status-update', (sessionId: string, status: Record<string, unknown>) => {
|
||||
@@ -181,15 +216,12 @@ export function setupSessionManager(): void {
|
||||
}
|
||||
// Update any active reviews that reference the old key as child (FIX 3)
|
||||
sessionReviews.updateChildCliId(oldKey, newKey);
|
||||
// Send updated SESSION_CREATED so frontend knows the real ID
|
||||
// Send SESSION_CREATED with the real UUID — for pendingRekey adapters,
|
||||
// this is the ONLY SESSION_CREATED the client receives.
|
||||
const resolvedAdapter = getAdapter(adapterName || DEFAULT_ADAPTER);
|
||||
if (resolvedAdapter && clients) {
|
||||
for (const conn of clients) {
|
||||
send(conn, {
|
||||
type: WS.SESSION_CREATED,
|
||||
sessionId: newKey,
|
||||
permissionMode: (resolvedAdapter.getSession(newKey) as any)?.permissionMode || (resolvedAdapter.getSession(newKey) as any)?.approvalPolicy,
|
||||
});
|
||||
sendSessionCreated(conn, resolvedAdapter, newKey);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -204,20 +236,60 @@ export function setupSessionManager(): void {
|
||||
|
||||
// === Helper: resolve adapter for a session ===
|
||||
|
||||
function getAdapterForSession(conn: ClientConnection, sessionId?: string): { adapter: IAdapter | undefined; sid: string } {
|
||||
/**
|
||||
* Resolve which adapter owns a session.
|
||||
* 1. In-memory map (fastest, covers active sessions)
|
||||
* 2. SQLite (populated when /api/sessions is fetched, survives restarts)
|
||||
* 3. Probe each adapter with getMessages (one-time cost, then cached)
|
||||
* Returns null if no adapter recognizes the session.
|
||||
*/
|
||||
async function resolveAdapterForSession(sessionId: string): Promise<string | null> {
|
||||
// 1. Memory
|
||||
const mapped = sessionAdapterMap.get(sessionId);
|
||||
if (mapped) return mapped;
|
||||
|
||||
// 2. SQLite
|
||||
const persisted = sessionAdapters.get(sessionId);
|
||||
if (persisted) {
|
||||
sessionAdapterMap.set(sessionId, persisted);
|
||||
return persisted;
|
||||
}
|
||||
|
||||
// 3. Probe each adapter — runs once per unknown session, then cached
|
||||
for (const [name, adapter] of getAllAdapters()) {
|
||||
try {
|
||||
const { messages } = await adapter.getMessages(sessionId);
|
||||
if (messages.length > 0) {
|
||||
sessionAdapterMap.set(sessionId, name);
|
||||
sessionAdapters.set(sessionId, name);
|
||||
return name;
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn(`[resolveAdapter] probe ${name} for ${sessionId.slice(0, 8)} failed:`, (err as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function getAdapterForSession(conn: ClientConnection, sessionId?: string): Promise<{ adapter: IAdapter | undefined; sid: string }> {
|
||||
const sid = sessionId || conn.sessionId || '';
|
||||
const name = sessionAdapterMap.get(sid) || DEFAULT_ADAPTER;
|
||||
return { adapter: getAdapter(name), sid };
|
||||
const name = await resolveAdapterForSession(sid);
|
||||
return { adapter: name ? getAdapter(name) : undefined, sid };
|
||||
}
|
||||
|
||||
function sendSessionCreated(conn: ClientConnection, adapter: IAdapter, sessionId: string): void {
|
||||
const sessionObj = adapter.getSession(sessionId) as {
|
||||
permissionMode?: string;
|
||||
approvalPolicy?: string;
|
||||
cwd?: string;
|
||||
} | null;
|
||||
const adapterName = sessionAdapterMap.get(sessionId) || sessionAdapters.get(sessionId);
|
||||
send(conn, {
|
||||
type: WS.SESSION_CREATED,
|
||||
sessionId,
|
||||
adapter: adapterName,
|
||||
cwd: sessionObj?.cwd,
|
||||
permissionMode: sessionObj?.permissionMode || sessionObj?.approvalPolicy,
|
||||
});
|
||||
}
|
||||
@@ -238,13 +310,18 @@ export async function handleIncomingMessage(conn: ClientConnection, msg: ClientM
|
||||
case WS.ABORT:
|
||||
return handleAbort(conn, msg.sessionId as string | undefined);
|
||||
case WS.RECONNECT:
|
||||
return handleReconnect(conn, msg.sessionId as string | undefined, msg.adapter as string | undefined);
|
||||
return handleReconnect(conn, msg.sessionId as string | undefined);
|
||||
case WS.SET_PERMISSION_MODE:
|
||||
return handleSetPermissionMode(conn, msg.sessionId as string, msg.mode as string);
|
||||
case WS.SET_MODEL:
|
||||
return handleSetModel(conn, msg.sessionId as string, msg.model as string);
|
||||
case WS.PLAN_RESPONSE:
|
||||
return handlePlanResponse(conn, msg.sessionId as string, msg.optionIndex as number, msg.text as string | undefined);
|
||||
case WS.PROMPT_RESPONSE:
|
||||
return handlePromptResponse(conn, msg.requestId as string, {
|
||||
selectedOption: msg.selectedOption as string | undefined,
|
||||
textValue: msg.textValue as string | undefined,
|
||||
});
|
||||
default:
|
||||
conn.send({ type: 'error', error: `Unknown message type: ${msg.type}` });
|
||||
}
|
||||
@@ -253,20 +330,29 @@ export async function handleIncomingMessage(conn: ClientConnection, msg: ClientM
|
||||
// === Message Handlers ===
|
||||
|
||||
export async function handleQuery(conn: ClientConnection, prompt: string, options: QueryOptions): Promise<void> {
|
||||
const { cwd, model, sessionId, permissionMode, images, adapter: adapterName } = options;
|
||||
const adapter = getAdapter(adapterName || DEFAULT_ADAPTER);
|
||||
let { cwd, model, sessionId, permissionMode, images, adapter: adapterName } = options;
|
||||
let adapter = getAdapter(adapterName || DEFAULT_ADAPTER);
|
||||
if (!adapter) throw new Error(`Unknown adapter: ${adapterName}`);
|
||||
|
||||
let handle: { sessionId: string };
|
||||
let handle: { sessionId: string; pendingRekey?: boolean };
|
||||
if (sessionId) {
|
||||
const resolvedName = await resolveAdapterForSession(sessionId);
|
||||
if (resolvedName && resolvedName !== (adapterName || DEFAULT_ADAPTER)) {
|
||||
adapter = getAdapter(resolvedName)!;
|
||||
adapterName = resolvedName;
|
||||
}
|
||||
handle = await adapter.resumeSession(sessionId, cwd as string, { permissionMode });
|
||||
} else {
|
||||
handle = await adapter.startSession(cwd || process.cwd(), { model, permissionMode });
|
||||
}
|
||||
|
||||
sessionAdapterMap.set(handle.sessionId, adapterName || DEFAULT_ADAPTER);
|
||||
registerSessionAdapter(handle.sessionId, adapterName || DEFAULT_ADAPTER);
|
||||
registerClient(conn, handle.sessionId);
|
||||
sendSessionCreated(conn, adapter, handle.sessionId);
|
||||
// Adapters with pendingRekey (Codex/Gemini) don't get SESSION_CREATED here —
|
||||
// the session-rekeyed handler sends it after rekey with the real UUID.
|
||||
if (!handle.pendingRekey) {
|
||||
sendSessionCreated(conn, adapter, handle.sessionId);
|
||||
}
|
||||
|
||||
// Send the message (images sent as text description for now)
|
||||
let messageText = prompt;
|
||||
@@ -280,8 +366,8 @@ export async function handleQuery(conn: ClientConnection, prompt: string, option
|
||||
await adapter.sendMessage(handle.sessionId, messageText, { clientId: conn.clientId });
|
||||
}
|
||||
|
||||
export function handlePermissionResponse(conn: ClientConnection, requestId: string, response: { behavior: PermissionBehavior; alwaysAllow?: boolean }): void {
|
||||
const { adapter, sid } = getAdapterForSession(conn);
|
||||
export async function handlePermissionResponse(conn: ClientConnection, requestId: string, response: { behavior: PermissionBehavior; alwaysAllow?: boolean }): Promise<void> {
|
||||
const { adapter, sid } = await getAdapterForSession(conn);
|
||||
if (adapter) {
|
||||
adapter.respondPermission(requestId, response.behavior);
|
||||
broadcast(sid, { type: WS.PERMISSION_DISMISSED, requestId });
|
||||
@@ -289,20 +375,28 @@ export function handlePermissionResponse(conn: ClientConnection, requestId: stri
|
||||
}
|
||||
|
||||
export async function handleAskResponse(conn: ClientConnection, requestId: string, answers: string): Promise<void> {
|
||||
const { adapter, sid } = getAdapterForSession(conn);
|
||||
const { adapter, sid } = await getAdapterForSession(conn);
|
||||
if (adapter) {
|
||||
adapter.respondQuestion(requestId, answers);
|
||||
broadcast(sid, { type: WS.PERMISSION_DISMISSED, requestId });
|
||||
}
|
||||
}
|
||||
|
||||
export async function handlePromptResponse(conn: ClientConnection, requestId: string, response: { selectedOption?: string; textValue?: string }): Promise<void> {
|
||||
const { adapter, sid } = await getAdapterForSession(conn);
|
||||
if (adapter) {
|
||||
adapter.respondInteractivePrompt(requestId, response.selectedOption, response.textValue);
|
||||
broadcast(sid, { type: WS.PROMPT_DISMISSED, requestId });
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleAbort(conn: ClientConnection, sessionId?: string): Promise<void> {
|
||||
const { adapter, sid } = getAdapterForSession(conn, sessionId);
|
||||
const { adapter, sid } = await getAdapterForSession(conn, sessionId);
|
||||
if (sid && adapter) await adapter.interrupt(sid);
|
||||
}
|
||||
|
||||
export async function handlePlanResponse(conn: ClientConnection, sessionId: string, optionIndex: number, text?: string): Promise<void> {
|
||||
const { adapter, sid } = getAdapterForSession(conn, sessionId);
|
||||
const { adapter, sid } = await getAdapterForSession(conn, sessionId);
|
||||
if (!sid || !adapter) return;
|
||||
await adapter.respondPlan(sid, optionIndex, text);
|
||||
// Broadcast synthetic user message so plan card transitions to read-only on ALL clients
|
||||
@@ -312,17 +406,17 @@ export async function handlePlanResponse(conn: ClientConnection, sessionId: stri
|
||||
broadcast(sid, { type: WS.MESSAGE_COMPLETE, messages: [{ role: 'user', content: msg }] });
|
||||
}
|
||||
|
||||
export async function handleReconnect(conn: ClientConnection, sessionId?: string, adapterHint?: string): Promise<void> {
|
||||
export async function handleReconnect(conn: ClientConnection, sessionId?: string): Promise<void> {
|
||||
if (!sessionId) return;
|
||||
|
||||
// Resolve rekey alias (Codex temp key → real UUID)
|
||||
const resolvedId = rekeyAliases.get(sessionId) || sessionId;
|
||||
|
||||
const adapterName = sessionAdapterMap.get(resolvedId) || adapterHint || DEFAULT_ADAPTER;
|
||||
const adapterName = await resolveAdapterForSession(resolvedId);
|
||||
if (!adapterName) return;
|
||||
const adapter = getAdapter(adapterName);
|
||||
if (!adapter) return;
|
||||
|
||||
registerClient(conn, sessionId); // registerClient also resolves alias internally
|
||||
registerClient(conn, sessionId);
|
||||
sessionAdapterMap.set(resolvedId, adapterName);
|
||||
|
||||
// Clear pending push notifications for this session and update badge (only if there were pending)
|
||||
@@ -374,8 +468,6 @@ export async function handleReconnect(conn: ClientConnection, sessionId?: string
|
||||
const childAdapterObj = getAdapter(review.child_adapter);
|
||||
if (!childAdapterObj) continue;
|
||||
|
||||
// Check if child session still exists in adapter's in-memory Map.
|
||||
// If not (server restarted or windows killed), mark review as ended.
|
||||
if (!childAdapterObj.getSession(review.child_cli_session_id)) {
|
||||
sessionReviews.endReview(review.id);
|
||||
continue;
|
||||
@@ -397,14 +489,14 @@ export async function handleReconnect(conn: ClientConnection, sessionId?: string
|
||||
}
|
||||
|
||||
export async function handleSetModel(conn: ClientConnection, sessionId: string, model: string): Promise<void> {
|
||||
const { adapter, sid } = getAdapterForSession(conn, sessionId);
|
||||
const { adapter, sid } = await getAdapterForSession(conn, sessionId);
|
||||
if (adapter && sid) {
|
||||
await adapter.switchModel(sid, model);
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleSetPermissionMode(conn: ClientConnection, sessionId: string, mode: string): Promise<void> {
|
||||
const { adapter, sid } = getAdapterForSession(conn, sessionId);
|
||||
const { adapter, sid } = await getAdapterForSession(conn, sessionId);
|
||||
if (!sid || !adapter) return;
|
||||
|
||||
const success = await adapter.switchPermissionMode(sid, mode);
|
||||
@@ -512,4 +604,5 @@ export function getClientCount(sessionId: string): number {
|
||||
|
||||
export function registerSessionAdapter(sessionId: string, adapterName: string): void {
|
||||
sessionAdapterMap.set(sessionId, adapterName);
|
||||
sessionAdapters.set(sessionId, adapterName);
|
||||
}
|
||||
|
||||
@@ -65,3 +65,20 @@ export interface SessionStatus {
|
||||
model: string;
|
||||
cost: number;
|
||||
}
|
||||
|
||||
export interface InteractivePrompt {
|
||||
requestId: string;
|
||||
promptType: 'permission' | 'question' | 'plan' | 'loop-detected';
|
||||
title: string;
|
||||
description: string;
|
||||
toolName?: string;
|
||||
toolInput?: any;
|
||||
options?: { value: string; label: string }[];
|
||||
textInput?: { placeholder?: string };
|
||||
}
|
||||
|
||||
export interface PromptResponse {
|
||||
requestId: string;
|
||||
selectedOption?: string;
|
||||
textValue?: string;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,10 @@ export const WS = {
|
||||
CLIENT_ID: 'client-id',
|
||||
PENDING_NOTIFICATIONS: 'pending-notifications',
|
||||
ERROR: 'error',
|
||||
// Interactive Prompts (unified permission/question/plan overlay)
|
||||
INTERACTIVE_PROMPT: 'interactive-prompt',
|
||||
PROMPT_RESPONSE: 'prompt-response',
|
||||
PROMPT_DISMISSED: 'prompt-dismissed',
|
||||
// Cross-AI Review
|
||||
REVIEW_STARTED: 'review-started',
|
||||
REVIEW_ENDED: 'review-ended',
|
||||
|
||||
Reference in New Issue
Block a user