mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-06-12 17:12:06 +08:00
Compare commits
1 Commits
main
...
feat/unifi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
afc717e69e |
@@ -28,10 +28,14 @@ import {
|
|||||||
} from './services/notification-orchestrator.js';
|
} from './services/notification-orchestrator.js';
|
||||||
import { sessionsService } from './modules/providers/services/sessions.service.js';
|
import { sessionsService } from './modules/providers/services/sessions.service.js';
|
||||||
import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
|
import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
|
||||||
import { createNormalizedMessage } from './shared/utils.js';
|
import { createCompleteMessage, createNormalizedMessage } from './shared/utils.js';
|
||||||
|
|
||||||
const activeSessions = new Map();
|
const activeSessions = new Map();
|
||||||
const pendingToolApprovals = new Map();
|
const pendingToolApprovals = new Map();
|
||||||
|
// Sessions cancelled via abort-session. The abort handler already sent the
|
||||||
|
// terminal `complete` (aborted: true) to the client, so the run loop must not
|
||||||
|
// emit a second one when its generator winds down.
|
||||||
|
const abortedSessionIds = new Set();
|
||||||
|
|
||||||
const TOOL_APPROVAL_TIMEOUT_MS = parseInt(process.env.CLAUDE_TOOL_APPROVAL_TIMEOUT_MS, 10) || 55000;
|
const TOOL_APPROVAL_TIMEOUT_MS = parseInt(process.env.CLAUDE_TOOL_APPROVAL_TIMEOUT_MS, 10) || 55000;
|
||||||
|
|
||||||
@@ -731,14 +735,18 @@ async function queryClaudeSDK(command, options = {}, ws) {
|
|||||||
// Clean up temporary image files
|
// Clean up temporary image files
|
||||||
await cleanupTempFiles(tempImagePaths, tempDir);
|
await cleanupTempFiles(tempImagePaths, tempDir);
|
||||||
|
|
||||||
// Send completion event
|
// Send the terminal completion event — skipped for aborted runs, whose
|
||||||
ws.send(createNormalizedMessage({ kind: 'complete', exitCode: 0, isNewSession: !sessionId && !!command, sessionId: capturedSessionId, provider: 'claude' }));
|
// terminal `complete` (aborted: true) was already sent by abort-session.
|
||||||
|
const wasAborted = capturedSessionId ? abortedSessionIds.delete(capturedSessionId) : false;
|
||||||
|
if (!wasAborted) {
|
||||||
|
ws.send(createCompleteMessage({ provider: 'claude', sessionId: capturedSessionId || sessionId || null, exitCode: 0 }));
|
||||||
|
}
|
||||||
notifyRunStopped({
|
notifyRunStopped({
|
||||||
userId: ws?.userId || null,
|
userId: ws?.userId || null,
|
||||||
provider: 'claude',
|
provider: 'claude',
|
||||||
sessionId: capturedSessionId || sessionId || null,
|
sessionId: capturedSessionId || sessionId || null,
|
||||||
sessionName: sessionSummary,
|
sessionName: sessionSummary,
|
||||||
stopReason: 'completed'
|
stopReason: wasAborted ? 'aborted' : 'completed'
|
||||||
});
|
});
|
||||||
// Complete
|
// Complete
|
||||||
|
|
||||||
@@ -753,14 +761,22 @@ async function queryClaudeSDK(command, options = {}, ws) {
|
|||||||
// Clean up temporary image files on error
|
// Clean up temporary image files on error
|
||||||
await cleanupTempFiles(tempImagePaths, tempDir);
|
await cleanupTempFiles(tempImagePaths, tempDir);
|
||||||
|
|
||||||
|
const wasAborted = capturedSessionId ? abortedSessionIds.delete(capturedSessionId) : false;
|
||||||
|
if (wasAborted) {
|
||||||
|
// The abort already produced the terminal complete; a generator throw
|
||||||
|
// caused by interrupt() is expected noise, not a user-facing error.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if Claude CLI is installed for a clearer error message
|
// Check if Claude CLI is installed for a clearer error message
|
||||||
const installed = await providerAuthService.isProviderInstalled('claude');
|
const installed = await providerAuthService.isProviderInstalled('claude');
|
||||||
const errorContent = !installed
|
const errorContent = !installed
|
||||||
? 'Claude Code is not installed. Please install it first: https://docs.anthropic.com/en/docs/claude-code'
|
? 'Claude Code is not installed. Please install it first: https://docs.anthropic.com/en/docs/claude-code'
|
||||||
: error.message;
|
: error.message;
|
||||||
|
|
||||||
// Send error to WebSocket
|
// Send error to WebSocket, then the terminal complete
|
||||||
ws.send(createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: capturedSessionId || sessionId || null, provider: 'claude' }));
|
ws.send(createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: capturedSessionId || sessionId || null, provider: 'claude' }));
|
||||||
|
ws.send(createCompleteMessage({ provider: 'claude', sessionId: capturedSessionId || sessionId || null, exitCode: 1 }));
|
||||||
notifyRunFailed({
|
notifyRunFailed({
|
||||||
userId: ws?.userId || null,
|
userId: ws?.userId || null,
|
||||||
provider: 'claude',
|
provider: 'claude',
|
||||||
@@ -787,6 +803,10 @@ async function abortClaudeSDKSession(sessionId) {
|
|||||||
try {
|
try {
|
||||||
console.log(`Aborting SDK session: ${sessionId}`);
|
console.log(`Aborting SDK session: ${sessionId}`);
|
||||||
|
|
||||||
|
// Mark before interrupting so the run loop knows not to emit its own
|
||||||
|
// terminal complete (the abort handler sends the aborted one).
|
||||||
|
abortedSessionIds.add(sessionId);
|
||||||
|
|
||||||
// Call interrupt() on the query instance
|
// Call interrupt() on the query instance
|
||||||
await session.instance.interrupt();
|
await session.instance.interrupt();
|
||||||
|
|
||||||
@@ -802,6 +822,8 @@ async function abortClaudeSDKSession(sessionId) {
|
|||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error aborting session ${sessionId}:`, error);
|
console.error(`Error aborting session ${sessionId}:`, error);
|
||||||
|
// The run keeps going; let it emit its own terminal complete.
|
||||||
|
abortedSessionIds.delete(sessionId);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { notifyRunFailed, notifyRunStopped } from './services/notification-orche
|
|||||||
import { sessionsService } from './modules/providers/services/sessions.service.js';
|
import { sessionsService } from './modules/providers/services/sessions.service.js';
|
||||||
import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
|
import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
|
||||||
import { providerModelsService } from './modules/providers/services/provider-models.service.js';
|
import { providerModelsService } from './modules/providers/services/provider-models.service.js';
|
||||||
import { createNormalizedMessage } from './shared/utils.js';
|
import { createCompleteMessage, createNormalizedMessage } from './shared/utils.js';
|
||||||
|
|
||||||
// Use cross-spawn on Windows for better command execution
|
// Use cross-spawn on Windows for better command execution
|
||||||
const spawnFunction = process.platform === 'win32' ? crossSpawn : spawn;
|
const spawnFunction = process.platform === 'win32' ? crossSpawn : spawn;
|
||||||
@@ -34,6 +34,10 @@ async function spawnCursor(command, options = {}, ws) {
|
|||||||
let sessionCreatedSent = false; // Track if we've already sent session-created event
|
let sessionCreatedSent = false; // Track if we've already sent session-created event
|
||||||
let hasRetriedWithTrust = false;
|
let hasRetriedWithTrust = false;
|
||||||
let settled = false;
|
let settled = false;
|
||||||
|
// The unified lifecycle contract requires exactly one terminal `complete`
|
||||||
|
// per run. Cursor surfaces completion twice (the `result` JSON line and
|
||||||
|
// the process close), so the first emission wins.
|
||||||
|
let completeSent = false;
|
||||||
|
|
||||||
// Use tools settings passed from frontend, or defaults
|
// Use tools settings passed from frontend, or defaults
|
||||||
const settings = toolsSettings || {
|
const settings = toolsSettings || {
|
||||||
@@ -197,15 +201,15 @@ async function spawnCursor(command, options = {}, ws) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'result': {
|
case 'result': {
|
||||||
// Session complete — send stream end + lifecycle complete with result payload
|
// Session complete — terminal lifecycle event for this run
|
||||||
const resultText = typeof response.result === 'string' ? response.result : '';
|
if (!completeSent) {
|
||||||
ws.send(createNormalizedMessage({
|
completeSent = true;
|
||||||
kind: 'complete',
|
ws.send(createCompleteMessage({
|
||||||
exitCode: response.subtype === 'success' ? 0 : 1,
|
provider: 'cursor',
|
||||||
resultText,
|
sessionId: capturedSessionId || sessionId || null,
|
||||||
isError: response.subtype !== 'success',
|
exitCode: response.subtype === 'success' ? 0 : 1,
|
||||||
sessionId: capturedSessionId || sessionId, provider: 'cursor',
|
}));
|
||||||
}));
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,7 +275,12 @@ async function spawnCursor(command, options = {}, ws) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.send(createNormalizedMessage({ kind: 'complete', exitCode: code, isNewSession: !sessionId && !!command, sessionId: finalSessionId, provider: 'cursor' }));
|
// Terminal complete — unless the `result` line already sent it, or the
|
||||||
|
// run was aborted (abort-session sent the aborted complete).
|
||||||
|
if (!completeSent && !cursorProcess.aborted) {
|
||||||
|
completeSent = true;
|
||||||
|
ws.send(createCompleteMessage({ provider: 'cursor', sessionId: finalSessionId, exitCode: code }));
|
||||||
|
}
|
||||||
|
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
notifyTerminalState({ code });
|
notifyTerminalState({ code });
|
||||||
@@ -297,6 +306,10 @@ async function spawnCursor(command, options = {}, ws) {
|
|||||||
: error.message;
|
: error.message;
|
||||||
|
|
||||||
ws.send(createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: capturedSessionId || sessionId || null, provider: 'cursor' }));
|
ws.send(createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: capturedSessionId || sessionId || null, provider: 'cursor' }));
|
||||||
|
if (!completeSent && !cursorProcess.aborted) {
|
||||||
|
completeSent = true;
|
||||||
|
ws.send(createCompleteMessage({ provider: 'cursor', sessionId: capturedSessionId || sessionId || null, exitCode: 1 }));
|
||||||
|
}
|
||||||
notifyTerminalState({ error });
|
notifyTerminalState({ error });
|
||||||
|
|
||||||
settleOnce(() => reject(error));
|
settleOnce(() => reject(error));
|
||||||
@@ -314,6 +327,9 @@ function abortCursorSession(sessionId) {
|
|||||||
const process = activeCursorProcesses.get(sessionId);
|
const process = activeCursorProcesses.get(sessionId);
|
||||||
if (process) {
|
if (process) {
|
||||||
console.log(`Aborting Cursor session: ${sessionId}`);
|
console.log(`Aborting Cursor session: ${sessionId}`);
|
||||||
|
// The abort handler sends the terminal complete (aborted: true); flag the
|
||||||
|
// process so its close handler does not emit a second one.
|
||||||
|
process.aborted = true;
|
||||||
process.kill('SIGTERM');
|
process.kill('SIGTERM');
|
||||||
activeCursorProcesses.delete(sessionId);
|
activeCursorProcesses.delete(sessionId);
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import GeminiResponseHandler from './gemini-response-handler.js';
|
|||||||
import { notifyRunFailed, notifyRunStopped } from './services/notification-orchestrator.js';
|
import { notifyRunFailed, notifyRunStopped } from './services/notification-orchestrator.js';
|
||||||
import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
|
import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
|
||||||
import { providerModelsService } from './modules/providers/services/provider-models.service.js';
|
import { providerModelsService } from './modules/providers/services/provider-models.service.js';
|
||||||
import { createNormalizedMessage } from './shared/utils.js';
|
import { createCompleteMessage, createNormalizedMessage } from './shared/utils.js';
|
||||||
|
|
||||||
// Use cross-spawn on Windows for correct .cmd resolution (same pattern as cursor-cli.js)
|
// Use cross-spawn on Windows for correct .cmd resolution (same pattern as cursor-cli.js)
|
||||||
const spawnFunction = process.platform === 'win32' ? crossSpawn : spawn;
|
const spawnFunction = process.platform === 'win32' ? crossSpawn : spawn;
|
||||||
@@ -129,6 +129,9 @@ async function spawnGemini(command, options = {}, ws) {
|
|||||||
let capturedSessionId = sessionId; // Track session ID throughout the process
|
let capturedSessionId = sessionId; // Track session ID throughout the process
|
||||||
let sessionCreatedSent = false; // Track if we've already sent session-created event
|
let sessionCreatedSent = false; // Track if we've already sent session-created event
|
||||||
let assistantBlocks = []; // Accumulate the full response blocks including tools
|
let assistantBlocks = []; // Accumulate the full response blocks including tools
|
||||||
|
// Unified lifecycle contract: exactly one terminal `complete` per run
|
||||||
|
// (close and error handlers can both fire for spawn failures).
|
||||||
|
let completeSent = false;
|
||||||
|
|
||||||
// Use tools settings passed from frontend, or defaults
|
// Use tools settings passed from frontend, or defaults
|
||||||
const settings = toolsSettings || {
|
const settings = toolsSettings || {
|
||||||
@@ -486,7 +489,12 @@ async function spawnGemini(command, options = {}, ws) {
|
|||||||
sessionManager.addMessage(finalSessionId, 'assistant', assistantBlocks);
|
sessionManager.addMessage(finalSessionId, 'assistant', assistantBlocks);
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.send(createNormalizedMessage({ kind: 'complete', exitCode: code, isNewSession: !sessionId && !!command, sessionId: finalSessionId, provider: 'gemini' }));
|
// Terminal complete — skipped for aborted runs (abort-session
|
||||||
|
// already sent the aborted complete on this run's behalf).
|
||||||
|
if (!completeSent && !geminiProcess.aborted) {
|
||||||
|
completeSent = true;
|
||||||
|
ws.send(createCompleteMessage({ provider: 'gemini', sessionId: finalSessionId, exitCode: code }));
|
||||||
|
}
|
||||||
|
|
||||||
// Clean up temporary image files if any
|
// Clean up temporary image files if any
|
||||||
if (geminiProcess.tempImagePaths && geminiProcess.tempImagePaths.length > 0) {
|
if (geminiProcess.tempImagePaths && geminiProcess.tempImagePaths.length > 0) {
|
||||||
@@ -566,6 +574,10 @@ async function spawnGemini(command, options = {}, ws) {
|
|||||||
|
|
||||||
const errorSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : finalSessionId;
|
const errorSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : finalSessionId;
|
||||||
ws.send(createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: errorSessionId, provider: 'gemini' }));
|
ws.send(createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: errorSessionId, provider: 'gemini' }));
|
||||||
|
if (!completeSent && !geminiProcess.aborted) {
|
||||||
|
completeSent = true;
|
||||||
|
ws.send(createCompleteMessage({ provider: 'gemini', sessionId: errorSessionId, exitCode: 1 }));
|
||||||
|
}
|
||||||
notifyTerminalState({ error });
|
notifyTerminalState({ error });
|
||||||
|
|
||||||
reject(error);
|
reject(error);
|
||||||
@@ -590,6 +602,9 @@ function abortGeminiSession(sessionId) {
|
|||||||
|
|
||||||
if (geminiProc) {
|
if (geminiProc) {
|
||||||
try {
|
try {
|
||||||
|
// The abort handler sends the terminal complete (aborted: true);
|
||||||
|
// flag the process so its close handler does not emit a second one.
|
||||||
|
geminiProc.aborted = true;
|
||||||
geminiProc.kill('SIGTERM');
|
geminiProc.kill('SIGTERM');
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (activeGeminiProcesses.has(processKey)) {
|
if (activeGeminiProcesses.has(processKey)) {
|
||||||
|
|||||||
@@ -133,9 +133,10 @@ flowchart TD
|
|||||||
|
|
||||||
### Chat Notes
|
### Chat Notes
|
||||||
|
|
||||||
1. `abort-session` returns a normalized `complete` message with `aborted: true`.
|
1. **Unified terminal lifecycle**: every provider run ends with exactly one `complete` message built by `createCompleteMessage()` (`server/shared/utils.ts`), regardless of provider: `{ kind: "complete", sessionId, actualSessionId, exitCode, success, aborted }`. Failed runs emit an informational `error` message first, then the terminal `complete` with `success: false`. Mid-run `error` messages (e.g. stderr output) are non-terminal; the frontend only treats `complete` as end-of-run.
|
||||||
2. `check-session-status` returns `{ type: "session-status", isProcessing }`.
|
2. `abort-session` sends the terminal `complete` (`aborted: true`) on behalf of the cancelled run; providers detect the abort and skip their own `complete` so the client sees exactly one.
|
||||||
3. Claude status checks can reconnect output stream to the new socket via `reconnectSessionWriter`.
|
3. `check-session-status` returns `{ type: "session-status", isProcessing }`.
|
||||||
|
4. Claude status checks can reconnect output stream to the new socket via `reconnectSessionWriter`.
|
||||||
|
|
||||||
## `/shell` Terminal Flow
|
## `/shell` Terminal Flow
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import type {
|
|||||||
AuthenticatedWebSocketRequest,
|
AuthenticatedWebSocketRequest,
|
||||||
LLMProvider,
|
LLMProvider,
|
||||||
} from '@/shared/types.js';
|
} from '@/shared/types.js';
|
||||||
import { createNormalizedMessage, parseIncomingJsonObject } from '@/shared/utils.js';
|
import { createCompleteMessage, parseIncomingJsonObject } from '@/shared/utils.js';
|
||||||
|
|
||||||
type ChatIncomingMessage = AnyRecord & {
|
type ChatIncomingMessage = AnyRecord & {
|
||||||
type?: string;
|
type?: string;
|
||||||
@@ -173,14 +173,14 @@ export function handleChatConnection(
|
|||||||
success = await dependencies.abortClaudeSDKSession(sessionId);
|
success = await dependencies.abortClaudeSDKSession(sessionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Terminal complete on behalf of the cancelled run — providers skip
|
||||||
|
// their own complete for aborted runs so the client sees exactly one.
|
||||||
writer.send(
|
writer.send(
|
||||||
createNormalizedMessage({
|
createCompleteMessage({
|
||||||
kind: 'complete',
|
provider,
|
||||||
|
sessionId,
|
||||||
exitCode: success ? 0 : 1,
|
exitCode: success ? 0 : 1,
|
||||||
aborted: true,
|
aborted: true,
|
||||||
success,
|
|
||||||
sessionId,
|
|
||||||
provider,
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
@@ -202,13 +202,11 @@ export function handleChatConnection(
|
|||||||
const sessionId = typeof data.sessionId === 'string' ? data.sessionId : '';
|
const sessionId = typeof data.sessionId === 'string' ? data.sessionId : '';
|
||||||
const success = dependencies.abortCursorSession(sessionId);
|
const success = dependencies.abortCursorSession(sessionId);
|
||||||
writer.send(
|
writer.send(
|
||||||
createNormalizedMessage({
|
createCompleteMessage({
|
||||||
kind: 'complete',
|
provider: 'cursor',
|
||||||
|
sessionId,
|
||||||
exitCode: success ? 0 : 1,
|
exitCode: success ? 0 : 1,
|
||||||
aborted: true,
|
aborted: true,
|
||||||
success,
|
|
||||||
sessionId,
|
|
||||||
provider: 'cursor',
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import { notifyRunFailed, notifyRunStopped } from './services/notification-orche
|
|||||||
import { sessionsService } from './modules/providers/services/sessions.service.js';
|
import { sessionsService } from './modules/providers/services/sessions.service.js';
|
||||||
import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
|
import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
|
||||||
import { providerModelsService } from './modules/providers/services/provider-models.service.js';
|
import { providerModelsService } from './modules/providers/services/provider-models.service.js';
|
||||||
import { createNormalizedMessage } from './shared/utils.js';
|
import { createCompleteMessage, createNormalizedMessage } from './shared/utils.js';
|
||||||
|
|
||||||
// Track active sessions
|
// Track active sessions
|
||||||
const activeCodexSessions = new Map();
|
const activeCodexSessions = new Map();
|
||||||
@@ -352,21 +352,26 @@ export async function queryCodex(command, options = {}, ws) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send completion event
|
// Send the terminal completion event — skipped for aborted runs, whose
|
||||||
if (!terminalFailure) {
|
// terminal `complete` (aborted: true) was already sent by abort-session.
|
||||||
sendMessage(ws, createNormalizedMessage({
|
const runSession = capturedSessionId ? activeCodexSessions.get(capturedSessionId) : null;
|
||||||
kind: 'complete',
|
const runAborted = runSession?.status === 'aborted' || abortController.signal.aborted;
|
||||||
actualSessionId: capturedSessionId || thread.id || sessionId || null,
|
if (!runAborted) {
|
||||||
sessionId: capturedSessionId || sessionId || null,
|
sendMessage(ws, createCompleteMessage({
|
||||||
provider: 'codex'
|
|
||||||
}));
|
|
||||||
notifyRunStopped({
|
|
||||||
userId: ws?.userId || null,
|
|
||||||
provider: 'codex',
|
provider: 'codex',
|
||||||
sessionId: capturedSessionId || sessionId || null,
|
sessionId: capturedSessionId || sessionId || null,
|
||||||
sessionName: sessionSummary,
|
actualSessionId: capturedSessionId || thread.id || sessionId || null,
|
||||||
stopReason: 'completed'
|
exitCode: terminalFailure ? 1 : 0,
|
||||||
});
|
}));
|
||||||
|
if (!terminalFailure) {
|
||||||
|
notifyRunStopped({
|
||||||
|
userId: ws?.userId || null,
|
||||||
|
provider: 'codex',
|
||||||
|
sessionId: capturedSessionId || sessionId || null,
|
||||||
|
sessionName: sessionSummary,
|
||||||
|
stopReason: 'completed'
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -386,6 +391,11 @@ export async function queryCodex(command, options = {}, ws) {
|
|||||||
: error.message;
|
: error.message;
|
||||||
|
|
||||||
sendMessage(ws, createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: capturedSessionId || sessionId || null, provider: 'codex' }));
|
sendMessage(ws, createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: capturedSessionId || sessionId || null, provider: 'codex' }));
|
||||||
|
sendMessage(ws, createCompleteMessage({
|
||||||
|
provider: 'codex',
|
||||||
|
sessionId: capturedSessionId || sessionId || null,
|
||||||
|
exitCode: 1,
|
||||||
|
}));
|
||||||
if (!terminalFailure) {
|
if (!terminalFailure) {
|
||||||
notifyRunFailed({
|
notifyRunFailed({
|
||||||
userId: ws?.userId || null,
|
userId: ws?.userId || null,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { sessionsService } from './modules/providers/services/sessions.service.j
|
|||||||
import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
|
import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
|
||||||
import { providerModelsService } from './modules/providers/services/provider-models.service.js';
|
import { providerModelsService } from './modules/providers/services/provider-models.service.js';
|
||||||
import { notifyRunFailed, notifyRunStopped } from './services/notification-orchestrator.js';
|
import { notifyRunFailed, notifyRunStopped } from './services/notification-orchestrator.js';
|
||||||
import { createNormalizedMessage, getOpenCodeDatabasePath } from './shared/utils.js';
|
import { createCompleteMessage, createNormalizedMessage, getOpenCodeDatabasePath } from './shared/utils.js';
|
||||||
|
|
||||||
const spawnFunction = process.platform === 'win32' ? crossSpawn : spawn;
|
const spawnFunction = process.platform === 'win32' ? crossSpawn : spawn;
|
||||||
|
|
||||||
@@ -92,6 +92,9 @@ async function spawnOpenCode(command, options = {}, ws) {
|
|||||||
let stdoutLineBuffer = '';
|
let stdoutLineBuffer = '';
|
||||||
let terminalNotificationSent = false;
|
let terminalNotificationSent = false;
|
||||||
let opencodeProcess = null;
|
let opencodeProcess = null;
|
||||||
|
// Unified lifecycle contract: exactly one terminal `complete` per run
|
||||||
|
// (close and error handlers can both fire for spawn failures).
|
||||||
|
let completeSent = false;
|
||||||
|
|
||||||
const notifyTerminalState = ({ code = null, error = null } = {}) => {
|
const notifyTerminalState = ({ code = null, error = null } = {}) => {
|
||||||
if (terminalNotificationSent) {
|
if (terminalNotificationSent) {
|
||||||
@@ -256,13 +259,12 @@ async function spawnOpenCode(command, options = {}, ws) {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.send(createNormalizedMessage({
|
// Terminal complete — skipped for aborted runs (abort-session
|
||||||
kind: 'complete',
|
// already sent the aborted complete on this run's behalf).
|
||||||
exitCode: code,
|
if (!completeSent && !opencodeProcess.aborted) {
|
||||||
isNewSession: !sessionId && !!command,
|
completeSent = true;
|
||||||
sessionId: finalSessionId,
|
ws.send(createCompleteMessage({ provider: 'opencode', sessionId: finalSessionId, exitCode: code }));
|
||||||
provider: 'opencode',
|
}
|
||||||
}));
|
|
||||||
|
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
notifyTerminalState({ code });
|
notifyTerminalState({ code });
|
||||||
@@ -302,6 +304,10 @@ async function spawnOpenCode(command, options = {}, ws) {
|
|||||||
sessionId: finalSessionId,
|
sessionId: finalSessionId,
|
||||||
provider: 'opencode',
|
provider: 'opencode',
|
||||||
}));
|
}));
|
||||||
|
if (!completeSent && !opencodeProcess.aborted) {
|
||||||
|
completeSent = true;
|
||||||
|
ws.send(createCompleteMessage({ provider: 'opencode', sessionId: finalSessionId, exitCode: 1 }));
|
||||||
|
}
|
||||||
notifyTerminalState({ error });
|
notifyTerminalState({ error });
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
@@ -315,6 +321,9 @@ function abortOpenCodeSession(sessionId) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The abort handler sends the terminal complete (aborted: true); flag the
|
||||||
|
// process so its close handler does not emit a second one.
|
||||||
|
process.aborted = true;
|
||||||
process.kill('SIGTERM');
|
process.kill('SIGTERM');
|
||||||
activeOpenCodeProcesses.delete(sessionId);
|
activeOpenCodeProcesses.delete(sessionId);
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -346,6 +346,43 @@ export function createNormalizedMessage(fields: NormalizedMessageInput): Normali
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the unified terminal `complete` lifecycle message.
|
||||||
|
*
|
||||||
|
* Contract: every provider run ends with exactly one `complete` (the
|
||||||
|
* abort-session handler emits it on behalf of cancelled runs, so aborted runs
|
||||||
|
* must NOT emit their own). The frontend treats `complete` as the only
|
||||||
|
* terminal signal and never needs provider-specific handling:
|
||||||
|
*
|
||||||
|
* - `sessionId` — the id the client knows this run by ('' if never discovered)
|
||||||
|
* - `actualSessionId` — canonical id after the run; equals `sessionId` unless
|
||||||
|
* the provider rewrote it mid-run
|
||||||
|
* - `exitCode` — 0 on success; a missing/null code (e.g. killed process)
|
||||||
|
* is reported as failure
|
||||||
|
* - `success` — exitCode === 0 and not aborted
|
||||||
|
* - `aborted` — run was cancelled by the user
|
||||||
|
*/
|
||||||
|
export function createCompleteMessage(opts: {
|
||||||
|
provider: NormalizedMessage['provider'];
|
||||||
|
sessionId?: string | null;
|
||||||
|
actualSessionId?: string | null;
|
||||||
|
exitCode?: number | null;
|
||||||
|
aborted?: boolean;
|
||||||
|
}): NormalizedMessage {
|
||||||
|
const exitCode = typeof opts.exitCode === 'number' ? opts.exitCode : 1;
|
||||||
|
const aborted = Boolean(opts.aborted);
|
||||||
|
|
||||||
|
return createNormalizedMessage({
|
||||||
|
kind: 'complete',
|
||||||
|
provider: opts.provider,
|
||||||
|
sessionId: opts.sessionId || null,
|
||||||
|
actualSessionId: opts.actualSessionId || opts.sessionId || null,
|
||||||
|
exitCode,
|
||||||
|
success: exitCode === 0 && !aborted,
|
||||||
|
aborted,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
//----------------- MCP CONFIG PARSING UTILITIES ------------
|
//----------------- MCP CONFIG PARSING UTILITIES ------------
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -28,12 +28,9 @@ function AppContentInner() {
|
|||||||
const wasConnectedRef = useRef(false);
|
const wasConnectedRef = useRef(false);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
activeSessions,
|
|
||||||
processingSessions,
|
processingSessions,
|
||||||
markSessionAsActive,
|
markSessionProcessing,
|
||||||
markSessionAsInactive,
|
markSessionIdle,
|
||||||
markSessionAsProcessing,
|
|
||||||
markSessionAsNotProcessing,
|
|
||||||
} = useSessionProtection();
|
} = useSessionProtection();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -57,7 +54,7 @@ function AppContentInner() {
|
|||||||
navigate,
|
navigate,
|
||||||
latestMessage,
|
latestMessage,
|
||||||
isMobile,
|
isMobile,
|
||||||
activeSessions,
|
activeSessions: processingSessions,
|
||||||
});
|
});
|
||||||
|
|
||||||
usePaletteOpsRegister({
|
usePaletteOpsRegister({
|
||||||
@@ -185,10 +182,8 @@ function AppContentInner() {
|
|||||||
onMenuClick={() => setSidebarOpen(true)}
|
onMenuClick={() => setSidebarOpen(true)}
|
||||||
isLoading={isLoadingProjects}
|
isLoading={isLoadingProjects}
|
||||||
onInputFocusChange={setIsInputFocused}
|
onInputFocusChange={setIsInputFocused}
|
||||||
onSessionActive={markSessionAsActive}
|
onSessionProcessing={markSessionProcessing}
|
||||||
onSessionInactive={markSessionAsInactive}
|
onSessionIdle={markSessionIdle}
|
||||||
onSessionProcessing={markSessionAsProcessing}
|
|
||||||
onSessionNotProcessing={markSessionAsNotProcessing}
|
|
||||||
processingSessions={processingSessions}
|
processingSessions={processingSessions}
|
||||||
onNavigateToSession={(targetSessionId: string, options) =>
|
onNavigateToSession={(targetSessionId: string, options) =>
|
||||||
navigate(`/session/${targetSessionId}`, { replace: Boolean(options?.replace) })
|
navigate(`/session/${targetSessionId}`, { replace: Boolean(options?.replace) })
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import type {
|
|||||||
import { useDropzone } from 'react-dropzone';
|
import { useDropzone } from 'react-dropzone';
|
||||||
|
|
||||||
import { authenticatedFetch } from '../../../utils/api';
|
import { authenticatedFetch } from '../../../utils/api';
|
||||||
|
import { PENDING_SESSION_ID } from '../../../hooks/useSessionProtection';
|
||||||
|
import type { MarkSessionProcessing } from '../../../hooks/useSessionProtection';
|
||||||
import { grantClaudeToolPermission } from '../utils/chatPermissions';
|
import { grantClaudeToolPermission } from '../utils/chatPermissions';
|
||||||
import { safeLocalStorage } from '../utils/chatStorage';
|
import { safeLocalStorage } from '../utils/chatStorage';
|
||||||
import type {
|
import type {
|
||||||
@@ -25,10 +27,6 @@ import { escapeRegExp } from '../utils/chatFormatting';
|
|||||||
import { useFileMentions } from './useFileMentions';
|
import { useFileMentions } from './useFileMentions';
|
||||||
import { type SlashCommand, useSlashCommands } from './useSlashCommands';
|
import { type SlashCommand, useSlashCommands } from './useSlashCommands';
|
||||||
|
|
||||||
type PendingViewSession = {
|
|
||||||
startedAt: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
interface UseChatComposerStateArgs {
|
interface UseChatComposerStateArgs {
|
||||||
selectedProject: Project | null;
|
selectedProject: Project | null;
|
||||||
selectedSession: ProjectSession | null;
|
selectedSession: ProjectSession | null;
|
||||||
@@ -46,17 +44,12 @@ interface UseChatComposerStateArgs {
|
|||||||
tokenBudget: Record<string, unknown> | null;
|
tokenBudget: Record<string, unknown> | null;
|
||||||
sendMessage: (message: unknown) => void;
|
sendMessage: (message: unknown) => void;
|
||||||
sendByCtrlEnter?: boolean;
|
sendByCtrlEnter?: boolean;
|
||||||
onSessionActive?: (sessionId?: string | null) => void;
|
onSessionProcessing?: MarkSessionProcessing;
|
||||||
onSessionProcessing?: (sessionId?: string | null) => void;
|
|
||||||
onInputFocusChange?: (focused: boolean) => void;
|
onInputFocusChange?: (focused: boolean) => void;
|
||||||
onFileOpen?: (filePath: string, diffInfo?: unknown) => void;
|
onFileOpen?: (filePath: string, diffInfo?: unknown) => void;
|
||||||
onShowSettings?: () => void;
|
onShowSettings?: () => void;
|
||||||
pendingViewSessionRef: { current: PendingViewSession | null };
|
|
||||||
scrollToBottom: () => void;
|
scrollToBottom: () => void;
|
||||||
addMessage: (msg: ChatMessage) => void;
|
addMessage: (msg: ChatMessage) => void;
|
||||||
setIsLoading: (loading: boolean) => void;
|
|
||||||
setCanAbortSession: (canAbort: boolean) => void;
|
|
||||||
setClaudeStatus: (status: { text: string; tokens: number; can_interrupt: boolean } | null) => void;
|
|
||||||
setIsUserScrolledUp: (isScrolledUp: boolean) => void;
|
setIsUserScrolledUp: (isScrolledUp: boolean) => void;
|
||||||
setPendingPermissionRequests: Dispatch<SetStateAction<PendingPermissionRequest[]>>;
|
setPendingPermissionRequests: Dispatch<SetStateAction<PendingPermissionRequest[]>>;
|
||||||
}
|
}
|
||||||
@@ -177,17 +170,12 @@ export function useChatComposerState({
|
|||||||
tokenBudget,
|
tokenBudget,
|
||||||
sendMessage,
|
sendMessage,
|
||||||
sendByCtrlEnter,
|
sendByCtrlEnter,
|
||||||
onSessionActive,
|
|
||||||
onSessionProcessing,
|
onSessionProcessing,
|
||||||
onInputFocusChange,
|
onInputFocusChange,
|
||||||
onFileOpen,
|
onFileOpen,
|
||||||
onShowSettings,
|
onShowSettings,
|
||||||
pendingViewSessionRef,
|
|
||||||
scrollToBottom,
|
scrollToBottom,
|
||||||
addMessage,
|
addMessage,
|
||||||
setIsLoading,
|
|
||||||
setCanAbortSession,
|
|
||||||
setClaudeStatus,
|
|
||||||
setIsUserScrolledUp,
|
setIsUserScrolledUp,
|
||||||
setPendingPermissionRequests,
|
setPendingPermissionRequests,
|
||||||
}: UseChatComposerStateArgs) {
|
}: UseChatComposerStateArgs) {
|
||||||
@@ -620,27 +608,18 @@ export function useChatComposerState({
|
|||||||
};
|
};
|
||||||
|
|
||||||
addMessage(userMessage);
|
addMessage(userMessage);
|
||||||
setIsLoading(true); // Processing banner starts
|
// Mark this request as processing in the per-session activity map (the
|
||||||
setCanAbortSession(true);
|
// single source of truth the indicator derives from). A brand-new
|
||||||
setClaudeStatus({
|
// conversation has no session id yet, so it is tracked under the
|
||||||
text: 'Processing',
|
// pending placeholder until `session_created` announces the real id.
|
||||||
tokens: 0,
|
onSessionProcessing?.(effectiveSessionId || PENDING_SESSION_ID, {
|
||||||
can_interrupt: true,
|
statusText: null,
|
||||||
|
canInterrupt: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
setIsUserScrolledUp(false);
|
setIsUserScrolledUp(false);
|
||||||
setTimeout(() => scrollToBottom(), 100);
|
setTimeout(() => scrollToBottom(), 100);
|
||||||
|
|
||||||
if (!effectiveSessionId && !selectedSession?.id) {
|
|
||||||
// This tracks only that a request is in flight before the provider has
|
|
||||||
// emitted its real session id; routing still waits for session_created.
|
|
||||||
pendingViewSessionRef.current = { startedAt: Date.now() };
|
|
||||||
}
|
|
||||||
if (effectiveSessionId) {
|
|
||||||
onSessionActive?.(effectiveSessionId);
|
|
||||||
onSessionProcessing?.(effectiveSessionId);
|
|
||||||
}
|
|
||||||
|
|
||||||
const getToolsSettings = () => {
|
const getToolsSettings = () => {
|
||||||
try {
|
try {
|
||||||
const settingsKey =
|
const settingsKey =
|
||||||
@@ -776,19 +755,14 @@ export function useChatComposerState({
|
|||||||
geminiModel,
|
geminiModel,
|
||||||
opencodeModel,
|
opencodeModel,
|
||||||
isLoading,
|
isLoading,
|
||||||
onSessionActive,
|
|
||||||
onSessionProcessing,
|
onSessionProcessing,
|
||||||
pendingViewSessionRef,
|
|
||||||
permissionMode,
|
permissionMode,
|
||||||
provider,
|
provider,
|
||||||
resetCommandMenuState,
|
resetCommandMenuState,
|
||||||
scrollToBottom,
|
scrollToBottom,
|
||||||
selectedProject,
|
selectedProject,
|
||||||
sendMessage,
|
sendMessage,
|
||||||
setCanAbortSession,
|
|
||||||
addMessage,
|
addMessage,
|
||||||
setClaudeStatus,
|
|
||||||
setIsLoading,
|
|
||||||
setIsUserScrolledUp,
|
setIsUserScrolledUp,
|
||||||
slashCommands,
|
slashCommands,
|
||||||
],
|
],
|
||||||
@@ -1000,15 +974,11 @@ export function useChatComposerState({
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
setPendingPermissionRequests((previous) => {
|
setPendingPermissionRequests((previous) =>
|
||||||
const next = previous.filter((request) => !validIds.includes(request.requestId));
|
previous.filter((request) => !validIds.includes(request.requestId)),
|
||||||
if (next.length === 0) {
|
);
|
||||||
setClaudeStatus(null);
|
|
||||||
}
|
|
||||||
return next;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
[sendMessage, setClaudeStatus, setPendingPermissionRequests],
|
[sendMessage, setPendingPermissionRequests],
|
||||||
);
|
);
|
||||||
|
|
||||||
const [isInputFocused, setIsInputFocused] = useState(false);
|
const [isInputFocused, setIsInputFocused] = useState(false);
|
||||||
|
|||||||
@@ -4,14 +4,12 @@ import type { Dispatch, MutableRefObject, SetStateAction } from 'react';
|
|||||||
import { usePaletteOps } from '../../../contexts/PaletteOpsContext';
|
import { usePaletteOps } from '../../../contexts/PaletteOpsContext';
|
||||||
import { showCompletionTitleIndicator } from '../../../utils/pageTitleNotification';
|
import { showCompletionTitleIndicator } from '../../../utils/pageTitleNotification';
|
||||||
import { playChatCompletionSound } from '../../../utils/notificationSound';
|
import { playChatCompletionSound } from '../../../utils/notificationSound';
|
||||||
|
import { PENDING_SESSION_ID } from '../../../hooks/useSessionProtection';
|
||||||
|
import type { MarkSessionIdle, MarkSessionProcessing } from '../../../hooks/useSessionProtection';
|
||||||
import type { PendingPermissionRequest, SessionNavigationOptions } from '../types/types';
|
import type { PendingPermissionRequest, SessionNavigationOptions } from '../types/types';
|
||||||
import type { ProjectSession, LLMProvider } from '../../../types/app';
|
import type { ProjectSession, LLMProvider } from '../../../types/app';
|
||||||
import type { SessionStore, NormalizedMessage } from '../../../stores/useSessionStore';
|
import type { SessionStore, NormalizedMessage } from '../../../stores/useSessionStore';
|
||||||
|
|
||||||
type PendingViewSession = {
|
|
||||||
startedAt: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
type LatestChatMessage = {
|
type LatestChatMessage = {
|
||||||
type?: string;
|
type?: string;
|
||||||
kind?: string;
|
kind?: string;
|
||||||
@@ -55,18 +53,14 @@ interface UseChatRealtimeHandlersArgs {
|
|||||||
selectedSession: ProjectSession | null;
|
selectedSession: ProjectSession | null;
|
||||||
currentSessionId: string | null;
|
currentSessionId: string | null;
|
||||||
setCurrentSessionId: (sessionId: string | null) => void;
|
setCurrentSessionId: (sessionId: string | null) => void;
|
||||||
setIsLoading: (loading: boolean) => void;
|
|
||||||
setCanAbortSession: (canAbort: boolean) => void;
|
|
||||||
setClaudeStatus: (status: { text: string; tokens: number; can_interrupt: boolean } | null) => void;
|
|
||||||
setTokenBudget: (budget: Record<string, unknown> | null) => void;
|
setTokenBudget: (budget: Record<string, unknown> | null) => void;
|
||||||
setPendingPermissionRequests: Dispatch<SetStateAction<PendingPermissionRequest[]>>;
|
setPendingPermissionRequests: Dispatch<SetStateAction<PendingPermissionRequest[]>>;
|
||||||
pendingViewSessionRef: MutableRefObject<PendingViewSession | null>;
|
|
||||||
streamTimerRef: MutableRefObject<number | null>;
|
streamTimerRef: MutableRefObject<number | null>;
|
||||||
accumulatedStreamRef: MutableRefObject<string>;
|
accumulatedStreamRef: MutableRefObject<string>;
|
||||||
onSessionInactive?: (sessionId?: string | null) => void;
|
/** When each session's `check-session-status` was last sent; guards stale idle replies. */
|
||||||
onSessionActive?: (sessionId?: string | null) => void;
|
statusCheckSentAtRef: MutableRefObject<Map<string, number>>;
|
||||||
onSessionProcessing?: (sessionId?: string | null) => void;
|
onSessionProcessing?: MarkSessionProcessing;
|
||||||
onSessionNotProcessing?: (sessionId?: string | null) => void;
|
onSessionIdle?: MarkSessionIdle;
|
||||||
onNavigateToSession?: (sessionId: string, options?: SessionNavigationOptions) => void;
|
onNavigateToSession?: (sessionId: string, options?: SessionNavigationOptions) => void;
|
||||||
onWebSocketReconnect?: () => void;
|
onWebSocketReconnect?: () => void;
|
||||||
sessionStore: SessionStore;
|
sessionStore: SessionStore;
|
||||||
@@ -82,18 +76,13 @@ export function useChatRealtimeHandlers({
|
|||||||
selectedSession,
|
selectedSession,
|
||||||
currentSessionId,
|
currentSessionId,
|
||||||
setCurrentSessionId,
|
setCurrentSessionId,
|
||||||
setIsLoading,
|
|
||||||
setCanAbortSession,
|
|
||||||
setClaudeStatus,
|
|
||||||
setTokenBudget,
|
setTokenBudget,
|
||||||
setPendingPermissionRequests,
|
setPendingPermissionRequests,
|
||||||
pendingViewSessionRef,
|
|
||||||
streamTimerRef,
|
streamTimerRef,
|
||||||
accumulatedStreamRef,
|
accumulatedStreamRef,
|
||||||
onSessionInactive,
|
statusCheckSentAtRef,
|
||||||
onSessionActive,
|
|
||||||
onSessionProcessing,
|
onSessionProcessing,
|
||||||
onSessionNotProcessing,
|
onSessionIdle,
|
||||||
onNavigateToSession,
|
onNavigateToSession,
|
||||||
onWebSocketReconnect,
|
onWebSocketReconnect,
|
||||||
sessionStore,
|
sessionStore,
|
||||||
@@ -138,35 +127,24 @@ export function useChatRealtimeHandlers({
|
|||||||
|
|
||||||
const status = msg.status;
|
const status = msg.status;
|
||||||
if (status) {
|
if (status) {
|
||||||
const statusInfo = {
|
onSessionProcessing?.(statusSessionId, {
|
||||||
text: status.text || 'Working...',
|
statusText: status.text || null,
|
||||||
tokens: status.tokens || 0,
|
canInterrupt: status.can_interrupt !== false,
|
||||||
can_interrupt: status.can_interrupt !== undefined ? status.can_interrupt : true,
|
});
|
||||||
};
|
|
||||||
setClaudeStatus(statusInfo);
|
|
||||||
setIsLoading(true);
|
|
||||||
setCanAbortSession(statusInfo.can_interrupt);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Legacy isProcessing format from check-session-status
|
// Reply to check-session-status (or unsolicited processing update)
|
||||||
const isCurrentSession =
|
|
||||||
statusSessionId === currentSessionId || (selectedSession && statusSessionId === selectedSession.id);
|
|
||||||
|
|
||||||
if (msg.isProcessing) {
|
if (msg.isProcessing) {
|
||||||
onSessionActive?.(statusSessionId);
|
|
||||||
onSessionProcessing?.(statusSessionId);
|
onSessionProcessing?.(statusSessionId);
|
||||||
if (isCurrentSession) { setIsLoading(true); setCanAbortSession(true); }
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
onSessionInactive?.(statusSessionId);
|
// Idle reply: ignore it if a newer request started after the check
|
||||||
onSessionNotProcessing?.(statusSessionId);
|
// was sent — the reply describes the older request.
|
||||||
if (isCurrentSession) {
|
onSessionIdle?.(statusSessionId, {
|
||||||
setIsLoading(false);
|
ifStartedBefore: statusCheckSentAtRef.current.get(statusSessionId),
|
||||||
setCanAbortSession(false);
|
});
|
||||||
setClaudeStatus(null);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,23 +216,15 @@ export function useChatRealtimeHandlers({
|
|||||||
// We no longer synthesize client-side placeholder IDs. Until the provider
|
// We no longer synthesize client-side placeholder IDs. Until the provider
|
||||||
// announces `session_created`, the active id is expected to be null.
|
// announces `session_created`, the active id is expected to be null.
|
||||||
if (!currentSessionId) {
|
if (!currentSessionId) {
|
||||||
console.log('Session created with ID:', newSessionId);
|
|
||||||
console.log('Existing session ID:', currentSessionId);
|
|
||||||
setCurrentSessionId(newSessionId);
|
setCurrentSessionId(newSessionId);
|
||||||
setPendingPermissionRequests((prev) =>
|
setPendingPermissionRequests((prev) =>
|
||||||
prev.map((r) => (r.sessionId ? r : { ...r, sessionId: newSessionId })),
|
prev.map((r) => (r.sessionId ? r : { ...r, sessionId: newSessionId })),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
pendingViewSessionRef.current = null;
|
// The in-flight request now has a concrete session id: migrate the
|
||||||
onSessionActive?.(newSessionId);
|
// processing entry from the pending placeholder.
|
||||||
|
onSessionIdle?.(PENDING_SESSION_ID);
|
||||||
onSessionProcessing?.(newSessionId);
|
onSessionProcessing?.(newSessionId);
|
||||||
setIsLoading(true);
|
|
||||||
setCanAbortSession(true);
|
|
||||||
setClaudeStatus({
|
|
||||||
text: 'Processing',
|
|
||||||
tokens: 0,
|
|
||||||
can_interrupt: true,
|
|
||||||
});
|
|
||||||
onNavigateToSession?.(newSessionId);
|
onNavigateToSession?.(newSessionId);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -271,24 +241,27 @@ export function useChatRealtimeHandlers({
|
|||||||
}
|
}
|
||||||
accumulatedStreamRef.current = '';
|
accumulatedStreamRef.current = '';
|
||||||
|
|
||||||
setIsLoading(false);
|
// `complete` is the unified terminal event — every provider run ends
|
||||||
setCanAbortSession(false);
|
// with exactly one, regardless of success, failure, or abort. The
|
||||||
setClaudeStatus(null);
|
// indicator derives from the processing map, so deleting the entry
|
||||||
|
// hides it immediately and atomically.
|
||||||
|
onSessionIdle?.(sid);
|
||||||
|
onSessionIdle?.(PENDING_SESSION_ID);
|
||||||
setPendingPermissionRequests([]);
|
setPendingPermissionRequests([]);
|
||||||
onSessionInactive?.(sid);
|
|
||||||
onSessionNotProcessing?.(sid);
|
|
||||||
pendingViewSessionRef.current = null;
|
|
||||||
|
|
||||||
// Handle aborted case
|
// Handle aborted case
|
||||||
if (msg.aborted) {
|
if (msg.aborted) {
|
||||||
// Abort was requested — the complete event confirms it
|
// Abort was requested — the complete event confirms it
|
||||||
// No special UI action needed beyond clearing loading state above
|
// No special UI action needed beyond clearing the processing entry above
|
||||||
// The backend already sent any abort-related messages
|
// The backend already sent any abort-related messages
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
showCompletionTitleIndicator();
|
// Celebrate only successful runs (failed runs end with success: false).
|
||||||
void playChatCompletionSound();
|
if (msg.success !== false) {
|
||||||
|
showCompletionTitleIndicator();
|
||||||
|
void playChatCompletionSound();
|
||||||
|
}
|
||||||
|
|
||||||
const actualSessionId =
|
const actualSessionId =
|
||||||
typeof msg.actualSessionId === 'string' && msg.actualSessionId.trim().length > 0
|
typeof msg.actualSessionId === 'string' && msg.actualSessionId.trim().length > 0
|
||||||
@@ -302,6 +275,7 @@ export function useChatRealtimeHandlers({
|
|||||||
|
|
||||||
if (actualSessionId && sid && actualSessionId !== sid) {
|
if (actualSessionId && sid && actualSessionId !== sid) {
|
||||||
sessionStore.replaceSessionId(sid, actualSessionId);
|
sessionStore.replaceSessionId(sid, actualSessionId);
|
||||||
|
onSessionIdle?.(actualSessionId);
|
||||||
|
|
||||||
if (isVisibleSession) {
|
if (isVisibleSession) {
|
||||||
setCurrentSessionId(actualSessionId);
|
setCurrentSessionId(actualSessionId);
|
||||||
@@ -317,15 +291,9 @@ export function useChatRealtimeHandlers({
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'error': {
|
// 'error' is an informational message row, not a terminal event —
|
||||||
setIsLoading(false);
|
// providers emit it for mid-run stderr output too. Run teardown is
|
||||||
setCanAbortSession(false);
|
// always signalled by the unified 'complete' that follows.
|
||||||
setClaudeStatus(null);
|
|
||||||
onSessionInactive?.(sid);
|
|
||||||
onSessionNotProcessing?.(sid);
|
|
||||||
pendingViewSessionRef.current = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'permission_request': {
|
case 'permission_request': {
|
||||||
if (!msg.requestId) break;
|
if (!msg.requestId) break;
|
||||||
@@ -340,9 +308,7 @@ export function useChatRealtimeHandlers({
|
|||||||
receivedAt: new Date(),
|
receivedAt: new Date(),
|
||||||
}];
|
}];
|
||||||
});
|
});
|
||||||
setIsLoading(true);
|
onSessionProcessing?.(sid || PENDING_SESSION_ID);
|
||||||
setCanAbortSession(true);
|
|
||||||
setClaudeStatus({ text: 'Waiting for permission', tokens: 0, can_interrupt: true });
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,13 +323,10 @@ export function useChatRealtimeHandlers({
|
|||||||
if (msg.text === 'token_budget' && msg.tokenBudget) {
|
if (msg.text === 'token_budget' && msg.tokenBudget) {
|
||||||
setTokenBudget(msg.tokenBudget as Record<string, unknown>);
|
setTokenBudget(msg.tokenBudget as Record<string, unknown>);
|
||||||
} else if (msg.text) {
|
} else if (msg.text) {
|
||||||
setClaudeStatus({
|
onSessionProcessing?.(sid || PENDING_SESSION_ID, {
|
||||||
text: msg.text,
|
statusText: msg.text,
|
||||||
tokens: msg.tokens || 0,
|
canInterrupt: msg.canInterrupt !== false,
|
||||||
can_interrupt: msg.canInterrupt !== undefined ? msg.canInterrupt : true,
|
|
||||||
});
|
});
|
||||||
setIsLoading(true);
|
|
||||||
setCanAbortSession(msg.canInterrupt !== false);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -379,18 +342,13 @@ export function useChatRealtimeHandlers({
|
|||||||
selectedSession,
|
selectedSession,
|
||||||
currentSessionId,
|
currentSessionId,
|
||||||
setCurrentSessionId,
|
setCurrentSessionId,
|
||||||
setIsLoading,
|
|
||||||
setCanAbortSession,
|
|
||||||
setClaudeStatus,
|
|
||||||
setTokenBudget,
|
setTokenBudget,
|
||||||
setPendingPermissionRequests,
|
setPendingPermissionRequests,
|
||||||
pendingViewSessionRef,
|
|
||||||
streamTimerRef,
|
streamTimerRef,
|
||||||
accumulatedStreamRef,
|
accumulatedStreamRef,
|
||||||
onSessionInactive,
|
statusCheckSentAtRef,
|
||||||
onSessionActive,
|
|
||||||
onSessionProcessing,
|
onSessionProcessing,
|
||||||
onSessionNotProcessing,
|
onSessionIdle,
|
||||||
onNavigateToSession,
|
onNavigateToSession,
|
||||||
onWebSocketReconnect,
|
onWebSocketReconnect,
|
||||||
sessionStore,
|
sessionStore,
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } fr
|
|||||||
import type { MutableRefObject } from 'react';
|
import type { MutableRefObject } from 'react';
|
||||||
|
|
||||||
import { authenticatedFetch } from '../../../utils/api';
|
import { authenticatedFetch } from '../../../utils/api';
|
||||||
|
import { PENDING_SESSION_ID } from '../../../hooks/useSessionProtection';
|
||||||
|
import type { MarkSessionIdle, SessionActivityMap } from '../../../hooks/useSessionProtection';
|
||||||
import type { Project, ProjectSession, LLMProvider } from '../../../types/app';
|
import type { Project, ProjectSession, LLMProvider } from '../../../types/app';
|
||||||
import type { SessionStore, NormalizedMessage } from '../../../stores/useSessionStore';
|
import type { SessionStore, NormalizedMessage } from '../../../stores/useSessionStore';
|
||||||
import type { ChatMessage, Provider } from '../types/types';
|
import type { ChatMessage, Provider } from '../types/types';
|
||||||
@@ -12,10 +14,6 @@ import { normalizedToChatMessages } from './useChatMessages';
|
|||||||
const MESSAGES_PER_PAGE = 20;
|
const MESSAGES_PER_PAGE = 20;
|
||||||
const INITIAL_VISIBLE_MESSAGES = 100;
|
const INITIAL_VISIBLE_MESSAGES = 100;
|
||||||
|
|
||||||
type PendingViewSession = {
|
|
||||||
startedAt: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
interface UseChatSessionStateArgs {
|
interface UseChatSessionStateArgs {
|
||||||
selectedProject: Project | null;
|
selectedProject: Project | null;
|
||||||
selectedSession: ProjectSession | null;
|
selectedSession: ProjectSession | null;
|
||||||
@@ -24,9 +22,11 @@ interface UseChatSessionStateArgs {
|
|||||||
autoScrollToBottom?: boolean;
|
autoScrollToBottom?: boolean;
|
||||||
externalMessageUpdate?: number;
|
externalMessageUpdate?: number;
|
||||||
newSessionTrigger?: number;
|
newSessionTrigger?: number;
|
||||||
processingSessions?: Set<string>;
|
processingSessions?: SessionActivityMap;
|
||||||
|
onSessionIdle?: MarkSessionIdle;
|
||||||
resetStreamingState: () => void;
|
resetStreamingState: () => void;
|
||||||
pendingViewSessionRef: MutableRefObject<PendingViewSession | null>;
|
/** When each session's `check-session-status` was last sent; guards stale idle replies. */
|
||||||
|
statusCheckSentAtRef: MutableRefObject<Map<string, number>>;
|
||||||
sessionStore: SessionStore;
|
sessionStore: SessionStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,21 +99,19 @@ export function useChatSessionState({
|
|||||||
externalMessageUpdate,
|
externalMessageUpdate,
|
||||||
newSessionTrigger,
|
newSessionTrigger,
|
||||||
processingSessions,
|
processingSessions,
|
||||||
|
onSessionIdle,
|
||||||
resetStreamingState,
|
resetStreamingState,
|
||||||
pendingViewSessionRef,
|
statusCheckSentAtRef,
|
||||||
sessionStore,
|
sessionStore,
|
||||||
}: UseChatSessionStateArgs) {
|
}: UseChatSessionStateArgs) {
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
|
||||||
const [currentSessionId, setCurrentSessionId] = useState<string | null>(selectedSession?.id || null);
|
const [currentSessionId, setCurrentSessionId] = useState<string | null>(selectedSession?.id || null);
|
||||||
const [isLoadingSessionMessages, setIsLoadingSessionMessages] = useState(false);
|
const [isLoadingSessionMessages, setIsLoadingSessionMessages] = useState(false);
|
||||||
const [isLoadingMoreMessages, setIsLoadingMoreMessages] = useState(false);
|
const [isLoadingMoreMessages, setIsLoadingMoreMessages] = useState(false);
|
||||||
const [hasMoreMessages, setHasMoreMessages] = useState(false);
|
const [hasMoreMessages, setHasMoreMessages] = useState(false);
|
||||||
const [totalMessages, setTotalMessages] = useState(0);
|
const [totalMessages, setTotalMessages] = useState(0);
|
||||||
const [canAbortSession, setCanAbortSession] = useState(false);
|
|
||||||
const [isUserScrolledUp, setIsUserScrolledUp] = useState(false);
|
const [isUserScrolledUp, setIsUserScrolledUp] = useState(false);
|
||||||
const [tokenBudget, setTokenBudget] = useState<Record<string, unknown> | null>(null);
|
const [tokenBudget, setTokenBudget] = useState<Record<string, unknown> | null>(null);
|
||||||
const [visibleMessageCount, setVisibleMessageCount] = useState(INITIAL_VISIBLE_MESSAGES);
|
const [visibleMessageCount, setVisibleMessageCount] = useState(INITIAL_VISIBLE_MESSAGES);
|
||||||
const [claudeStatus, setClaudeStatus] = useState<{ text: string; tokens: number; can_interrupt: boolean } | null>(null);
|
|
||||||
const [allMessagesLoaded, setAllMessagesLoaded] = useState(false);
|
const [allMessagesLoaded, setAllMessagesLoaded] = useState(false);
|
||||||
const [isLoadingAllMessages, setIsLoadingAllMessages] = useState(false);
|
const [isLoadingAllMessages, setIsLoadingAllMessages] = useState(false);
|
||||||
const [loadAllJustFinished, setLoadAllJustFinished] = useState(false);
|
const [loadAllJustFinished, setLoadAllJustFinished] = useState(false);
|
||||||
@@ -170,10 +168,7 @@ export function useChatSessionState({
|
|||||||
* - No coupling to unrelated external update signals.
|
* - No coupling to unrelated external update signals.
|
||||||
*/
|
*/
|
||||||
resetStreamingState();
|
resetStreamingState();
|
||||||
pendingViewSessionRef.current = null;
|
onSessionIdle?.(PENDING_SESSION_ID);
|
||||||
setClaudeStatus(null);
|
|
||||||
setCanAbortSession(false);
|
|
||||||
setIsLoading(false);
|
|
||||||
setCurrentSessionId(null);
|
setCurrentSessionId(null);
|
||||||
setPendingUserMessage(null);
|
setPendingUserMessage(null);
|
||||||
sessionStorage.removeItem('cursorSessionId');
|
sessionStorage.removeItem('cursorSessionId');
|
||||||
@@ -204,13 +199,29 @@ export function useChatSessionState({
|
|||||||
clearTimeout(loadAllFinishedTimerRef.current);
|
clearTimeout(loadAllFinishedTimerRef.current);
|
||||||
loadAllFinishedTimerRef.current = null;
|
loadAllFinishedTimerRef.current = null;
|
||||||
}
|
}
|
||||||
}, [newSessionTrigger, pendingViewSessionRef, resetStreamingState]);
|
}, [newSessionTrigger, onSessionIdle, resetStreamingState]);
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------- */
|
||||||
|
/* Derive processing state for the viewed session */
|
||||||
|
/* ---------------------------------------------------------------- */
|
||||||
|
|
||||||
|
const activeSessionId = selectedSession?.id || currentSessionId || null;
|
||||||
|
|
||||||
|
// The activity indicator always reflects the latest status of the session
|
||||||
|
// being viewed (or of the pending not-yet-created session on a fresh
|
||||||
|
// draft) — never stale local UI state from the last time it was open.
|
||||||
|
const sessionActivity = processingSessions?.get(activeSessionId ?? PENDING_SESSION_ID) ?? null;
|
||||||
|
const isProcessing = sessionActivity !== null;
|
||||||
|
const canAbortSession = isProcessing && sessionActivity.canInterrupt;
|
||||||
|
|
||||||
|
// Ref mirror so effects can read the latest map without re-running on
|
||||||
|
// every activity transition.
|
||||||
|
const processingSessionsRef = useRef(processingSessions);
|
||||||
|
processingSessionsRef.current = processingSessions;
|
||||||
|
|
||||||
/* ---------------------------------------------------------------- */
|
/* ---------------------------------------------------------------- */
|
||||||
/* Derive chatMessages from the store */
|
/* Derive chatMessages from the store */
|
||||||
/* ---------------------------------------------------------------- */
|
/* ---------------------------------------------------------------- */
|
||||||
|
|
||||||
const activeSessionId = selectedSession?.id || currentSessionId || null;
|
|
||||||
const [pendingUserMessage, setPendingUserMessage] = useState<ChatMessage | null>(null);
|
const [pendingUserMessage, setPendingUserMessage] = useState<ChatMessage | null>(null);
|
||||||
const flushedPendingUserMessageRef = useRef<ChatMessage | null>(null);
|
const flushedPendingUserMessageRef = useRef<ChatMessage | null>(null);
|
||||||
|
|
||||||
@@ -430,16 +441,12 @@ export function useChatSessionState({
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!selectedSession || !selectedProject) {
|
if (!selectedSession || !selectedProject) {
|
||||||
// A new provider run can be in flight before the router has a canonical
|
// A new provider run can be in flight before the router has a canonical
|
||||||
// selectedSession. Keep the processing banner alive until complete/error.
|
// selectedSession. Keep the draft view intact until complete/error.
|
||||||
if (pendingViewSessionRef.current) {
|
if (processingSessionsRef.current?.has(PENDING_SESSION_ID)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
resetStreamingState();
|
resetStreamingState();
|
||||||
pendingViewSessionRef.current = null;
|
|
||||||
setClaudeStatus(null);
|
|
||||||
setCanAbortSession(false);
|
|
||||||
setIsLoading(false);
|
|
||||||
setCurrentSessionId(null);
|
setCurrentSessionId(null);
|
||||||
sessionStorage.removeItem('cursorSessionId');
|
sessionStorage.removeItem('cursorSessionId');
|
||||||
messagesOffsetRef.current = 0;
|
messagesOffsetRef.current = 0;
|
||||||
@@ -461,9 +468,6 @@ export function useChatSessionState({
|
|||||||
const sessionChanged = currentSessionId !== null && currentSessionId !== selectedSession.id;
|
const sessionChanged = currentSessionId !== null && currentSessionId !== selectedSession.id;
|
||||||
if (sessionChanged) {
|
if (sessionChanged) {
|
||||||
resetStreamingState();
|
resetStreamingState();
|
||||||
pendingViewSessionRef.current = null;
|
|
||||||
setClaudeStatus(null);
|
|
||||||
setCanAbortSession(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset pagination/scroll state
|
// Reset pagination/scroll state
|
||||||
@@ -482,7 +486,6 @@ export function useChatSessionState({
|
|||||||
|
|
||||||
if (sessionChanged) {
|
if (sessionChanged) {
|
||||||
setTokenBudget(null);
|
setTokenBudget(null);
|
||||||
setIsLoading(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setCurrentSessionId(selectedSession.id);
|
setCurrentSessionId(selectedSession.id);
|
||||||
@@ -490,8 +493,11 @@ export function useChatSessionState({
|
|||||||
sessionStorage.setItem('cursorSessionId', selectedSession.id);
|
sessionStorage.setItem('cursorSessionId', selectedSession.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check session status
|
// Reconcile processing state with the server. Recording the send time
|
||||||
|
// lets the reply handler discard idle replies that a newer request has
|
||||||
|
// since outdated.
|
||||||
if (ws) {
|
if (ws) {
|
||||||
|
statusCheckSentAtRef.current.set(selectedSession.id, Date.now());
|
||||||
sendMessage({ type: 'check-session-status', sessionId: selectedSession.id, provider });
|
sendMessage({ type: 'check-session-status', sessionId: selectedSession.id, provider });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -516,11 +522,11 @@ export function useChatSessionState({
|
|||||||
setIsLoadingSessionMessages(false);
|
setIsLoadingSessionMessages(false);
|
||||||
});
|
});
|
||||||
}, [
|
}, [
|
||||||
pendingViewSessionRef,
|
|
||||||
resetStreamingState,
|
resetStreamingState,
|
||||||
selectedProject,
|
selectedProject,
|
||||||
selectedSession?.id,
|
selectedSession?.id,
|
||||||
sendMessage,
|
sendMessage,
|
||||||
|
statusCheckSentAtRef,
|
||||||
ws,
|
ws,
|
||||||
sessionStore,
|
sessionStore,
|
||||||
]);
|
]);
|
||||||
@@ -534,7 +540,7 @@ export function useChatSessionState({
|
|||||||
const provider = (localStorage.getItem('selected-provider') as Provider) || 'claude';
|
const provider = (localStorage.getItem('selected-provider') as Provider) || 'claude';
|
||||||
|
|
||||||
// Skip store refresh during active streaming
|
// Skip store refresh during active streaming
|
||||||
if (!isLoading) {
|
if (!isProcessing) {
|
||||||
await sessionStore.refreshFromServer(selectedSession.id, {
|
await sessionStore.refreshFromServer(selectedSession.id, {
|
||||||
provider: (selectedSession.__provider || provider) as LLMProvider,
|
provider: (selectedSession.__provider || provider) as LLMProvider,
|
||||||
projectId: selectedProject.projectId,
|
projectId: selectedProject.projectId,
|
||||||
@@ -559,7 +565,7 @@ export function useChatSessionState({
|
|||||||
selectedProject,
|
selectedProject,
|
||||||
selectedSession,
|
selectedSession,
|
||||||
sessionStore,
|
sessionStore,
|
||||||
isLoading,
|
isProcessing,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Search navigation target
|
// Search navigation target
|
||||||
@@ -726,16 +732,6 @@ export function useChatSessionState({
|
|||||||
return () => container.removeEventListener('scroll', handleScroll);
|
return () => container.removeEventListener('scroll', handleScroll);
|
||||||
}, [handleScroll]);
|
}, [handleScroll]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const activeViewSessionId = selectedSession?.id || currentSessionId;
|
|
||||||
if (!activeViewSessionId || !processingSessions) return;
|
|
||||||
const shouldBeProcessing = processingSessions.has(activeViewSessionId);
|
|
||||||
if (shouldBeProcessing && !isLoading) {
|
|
||||||
setIsLoading(true);
|
|
||||||
setCanAbortSession(true);
|
|
||||||
}
|
|
||||||
}, [currentSessionId, isLoading, processingSessions, selectedSession?.id]);
|
|
||||||
|
|
||||||
// "Load all" overlay
|
// "Load all" overlay
|
||||||
const prevLoadingRef = useRef(false);
|
const prevLoadingRef = useRef(false);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -817,16 +813,15 @@ export function useChatSessionState({
|
|||||||
addMessage,
|
addMessage,
|
||||||
clearMessages,
|
clearMessages,
|
||||||
rewindMessages,
|
rewindMessages,
|
||||||
isLoading,
|
sessionActivity,
|
||||||
setIsLoading,
|
isProcessing,
|
||||||
|
canAbortSession,
|
||||||
currentSessionId,
|
currentSessionId,
|
||||||
setCurrentSessionId,
|
setCurrentSessionId,
|
||||||
isLoadingSessionMessages,
|
isLoadingSessionMessages,
|
||||||
isLoadingMoreMessages,
|
isLoadingMoreMessages,
|
||||||
hasMoreMessages,
|
hasMoreMessages,
|
||||||
totalMessages,
|
totalMessages,
|
||||||
canAbortSession,
|
|
||||||
setCanAbortSession,
|
|
||||||
isUserScrolledUp,
|
isUserScrolledUp,
|
||||||
setIsUserScrolledUp,
|
setIsUserScrolledUp,
|
||||||
tokenBudget,
|
tokenBudget,
|
||||||
@@ -839,8 +834,6 @@ export function useChatSessionState({
|
|||||||
isLoadingAllMessages,
|
isLoadingAllMessages,
|
||||||
loadAllJustFinished,
|
loadAllJustFinished,
|
||||||
showLoadAllOverlay,
|
showLoadAllOverlay,
|
||||||
claudeStatus,
|
|
||||||
setClaudeStatus,
|
|
||||||
createDiff,
|
createDiff,
|
||||||
scrollContainerRef,
|
scrollContainerRef,
|
||||||
scrollToBottom,
|
scrollToBottom,
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
import type { Project, ProjectSession, LLMProvider } from '../../../types/app';
|
import type { Project, ProjectSession, LLMProvider } from '../../../types/app';
|
||||||
|
import type {
|
||||||
|
MarkSessionIdle,
|
||||||
|
MarkSessionProcessing,
|
||||||
|
SessionActivityMap,
|
||||||
|
} from '../../../hooks/useSessionProtection';
|
||||||
|
|
||||||
export type Provider = LLMProvider;
|
export type Provider = LLMProvider;
|
||||||
|
|
||||||
@@ -110,11 +115,9 @@ export interface ChatInterfaceProps {
|
|||||||
latestMessage: any;
|
latestMessage: any;
|
||||||
onFileOpen?: (filePath: string, diffInfo?: any) => void;
|
onFileOpen?: (filePath: string, diffInfo?: any) => void;
|
||||||
onInputFocusChange?: (focused: boolean) => void;
|
onInputFocusChange?: (focused: boolean) => void;
|
||||||
onSessionActive?: (sessionId?: string | null) => void;
|
onSessionProcessing?: MarkSessionProcessing;
|
||||||
onSessionInactive?: (sessionId?: string | null) => void;
|
onSessionIdle?: MarkSessionIdle;
|
||||||
onSessionProcessing?: (sessionId?: string | null) => void;
|
processingSessions?: SessionActivityMap;
|
||||||
onSessionNotProcessing?: (sessionId?: string | null) => void;
|
|
||||||
processingSessions?: Set<string>;
|
|
||||||
onNavigateToSession?: (targetSessionId: string, options?: SessionNavigationOptions) => void;
|
onNavigateToSession?: (targetSessionId: string, options?: SessionNavigationOptions) => void;
|
||||||
onShowSettings?: () => void;
|
onShowSettings?: () => void;
|
||||||
autoExpandTools?: boolean;
|
autoExpandTools?: boolean;
|
||||||
|
|||||||
@@ -17,10 +17,6 @@ import ChatComposer from './subcomponents/ChatComposer';
|
|||||||
import CommandResultModal from './subcomponents/CommandResultModal';
|
import CommandResultModal from './subcomponents/CommandResultModal';
|
||||||
|
|
||||||
|
|
||||||
type PendingViewSession = {
|
|
||||||
startedAt: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
function ChatInterface({
|
function ChatInterface({
|
||||||
selectedProject,
|
selectedProject,
|
||||||
selectedSession,
|
selectedSession,
|
||||||
@@ -29,10 +25,8 @@ function ChatInterface({
|
|||||||
latestMessage,
|
latestMessage,
|
||||||
onFileOpen,
|
onFileOpen,
|
||||||
onInputFocusChange,
|
onInputFocusChange,
|
||||||
onSessionActive,
|
|
||||||
onSessionInactive,
|
|
||||||
onSessionProcessing,
|
onSessionProcessing,
|
||||||
onSessionNotProcessing,
|
onSessionIdle,
|
||||||
processingSessions,
|
processingSessions,
|
||||||
onNavigateToSession,
|
onNavigateToSession,
|
||||||
onShowSettings,
|
onShowSettings,
|
||||||
@@ -51,7 +45,9 @@ function ChatInterface({
|
|||||||
const sessionStore = useSessionStore();
|
const sessionStore = useSessionStore();
|
||||||
const streamTimerRef = useRef<number | null>(null);
|
const streamTimerRef = useRef<number | null>(null);
|
||||||
const accumulatedStreamRef = useRef('');
|
const accumulatedStreamRef = useRef('');
|
||||||
const pendingViewSessionRef = useRef<PendingViewSession | null>(null);
|
// When each session's `check-session-status` was last sent; idle replies
|
||||||
|
// older than a later local request are discarded as stale.
|
||||||
|
const statusCheckSentAtRef = useRef(new Map<string, number>());
|
||||||
|
|
||||||
const resetStreamingState = useCallback(() => {
|
const resetStreamingState = useCallback(() => {
|
||||||
if (streamTimerRef.current) {
|
if (streamTimerRef.current) {
|
||||||
@@ -92,16 +88,15 @@ function ChatInterface({
|
|||||||
const {
|
const {
|
||||||
chatMessages,
|
chatMessages,
|
||||||
addMessage,
|
addMessage,
|
||||||
isLoading,
|
sessionActivity,
|
||||||
setIsLoading,
|
isProcessing,
|
||||||
|
canAbortSession,
|
||||||
currentSessionId,
|
currentSessionId,
|
||||||
setCurrentSessionId,
|
setCurrentSessionId,
|
||||||
isLoadingSessionMessages,
|
isLoadingSessionMessages,
|
||||||
isLoadingMoreMessages,
|
isLoadingMoreMessages,
|
||||||
hasMoreMessages,
|
hasMoreMessages,
|
||||||
totalMessages,
|
totalMessages,
|
||||||
canAbortSession,
|
|
||||||
setCanAbortSession,
|
|
||||||
isUserScrolledUp,
|
isUserScrolledUp,
|
||||||
setIsUserScrolledUp,
|
setIsUserScrolledUp,
|
||||||
tokenBudget,
|
tokenBudget,
|
||||||
@@ -114,8 +109,6 @@ function ChatInterface({
|
|||||||
isLoadingAllMessages,
|
isLoadingAllMessages,
|
||||||
loadAllJustFinished,
|
loadAllJustFinished,
|
||||||
showLoadAllOverlay,
|
showLoadAllOverlay,
|
||||||
claudeStatus,
|
|
||||||
setClaudeStatus,
|
|
||||||
createDiff,
|
createDiff,
|
||||||
scrollContainerRef,
|
scrollContainerRef,
|
||||||
scrollToBottom,
|
scrollToBottom,
|
||||||
@@ -130,8 +123,9 @@ function ChatInterface({
|
|||||||
externalMessageUpdate,
|
externalMessageUpdate,
|
||||||
newSessionTrigger,
|
newSessionTrigger,
|
||||||
processingSessions,
|
processingSessions,
|
||||||
|
onSessionIdle,
|
||||||
resetStreamingState,
|
resetStreamingState,
|
||||||
pendingViewSessionRef,
|
statusCheckSentAtRef,
|
||||||
sessionStore,
|
sessionStore,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -191,40 +185,40 @@ function ChatInterface({
|
|||||||
codexModel,
|
codexModel,
|
||||||
geminiModel,
|
geminiModel,
|
||||||
opencodeModel,
|
opencodeModel,
|
||||||
isLoading,
|
isLoading: isProcessing,
|
||||||
canAbortSession,
|
canAbortSession,
|
||||||
tokenBudget,
|
tokenBudget,
|
||||||
sendMessage,
|
sendMessage,
|
||||||
sendByCtrlEnter,
|
sendByCtrlEnter,
|
||||||
onSessionActive,
|
|
||||||
onSessionProcessing,
|
onSessionProcessing,
|
||||||
onInputFocusChange,
|
onInputFocusChange,
|
||||||
onFileOpen,
|
onFileOpen,
|
||||||
onShowSettings,
|
onShowSettings,
|
||||||
pendingViewSessionRef,
|
|
||||||
scrollToBottom,
|
scrollToBottom,
|
||||||
addMessage,
|
addMessage,
|
||||||
setIsLoading,
|
|
||||||
setCanAbortSession,
|
|
||||||
setClaudeStatus,
|
|
||||||
setIsUserScrolledUp,
|
setIsUserScrolledUp,
|
||||||
setPendingPermissionRequests,
|
setPendingPermissionRequests,
|
||||||
});
|
});
|
||||||
|
|
||||||
// On WebSocket reconnect, re-fetch the current session's messages from the server
|
// On WebSocket reconnect, re-fetch the current session's messages from the
|
||||||
// so missed streaming events are shown. Also reset isLoading.
|
// server so missed streaming events are shown, then re-check the session's
|
||||||
|
// processing status — the authoritative reply restores or clears the
|
||||||
|
// activity indicator depending on whether the run is still active.
|
||||||
const handleWebSocketReconnect = useCallback(async () => {
|
const handleWebSocketReconnect = useCallback(async () => {
|
||||||
if (!selectedProject || !selectedSession) return;
|
if (!selectedProject || !selectedSession) return;
|
||||||
const providerVal = (localStorage.getItem('selected-provider') as LLMProvider) || 'claude';
|
const providerVal =
|
||||||
|
selectedSession.__provider
|
||||||
|
|| (localStorage.getItem('selected-provider') as LLMProvider)
|
||||||
|
|| 'claude';
|
||||||
await sessionStore.refreshFromServer(selectedSession.id, {
|
await sessionStore.refreshFromServer(selectedSession.id, {
|
||||||
provider: (selectedSession.__provider || providerVal) as LLMProvider,
|
provider: providerVal as LLMProvider,
|
||||||
// Use DB projectId; legacy folder-derived projectName is no longer accepted here.
|
// Use DB projectId; legacy folder-derived projectName is no longer accepted here.
|
||||||
projectId: selectedProject.projectId,
|
projectId: selectedProject.projectId,
|
||||||
projectPath: selectedProject.fullPath || selectedProject.path || '',
|
projectPath: selectedProject.fullPath || selectedProject.path || '',
|
||||||
});
|
});
|
||||||
setIsLoading(false);
|
statusCheckSentAtRef.current.set(selectedSession.id, Date.now());
|
||||||
setCanAbortSession(false);
|
sendMessage({ type: 'check-session-status', sessionId: selectedSession.id, provider: providerVal });
|
||||||
}, [selectedProject, selectedSession, sessionStore, setIsLoading, setCanAbortSession]);
|
}, [selectedProject, selectedSession, sendMessage, sessionStore]);
|
||||||
|
|
||||||
useChatRealtimeHandlers({
|
useChatRealtimeHandlers({
|
||||||
latestMessage,
|
latestMessage,
|
||||||
@@ -232,25 +226,20 @@ function ChatInterface({
|
|||||||
selectedSession,
|
selectedSession,
|
||||||
currentSessionId,
|
currentSessionId,
|
||||||
setCurrentSessionId,
|
setCurrentSessionId,
|
||||||
setIsLoading,
|
|
||||||
setCanAbortSession,
|
|
||||||
setClaudeStatus,
|
|
||||||
setTokenBudget,
|
setTokenBudget,
|
||||||
setPendingPermissionRequests,
|
setPendingPermissionRequests,
|
||||||
pendingViewSessionRef,
|
|
||||||
streamTimerRef,
|
streamTimerRef,
|
||||||
accumulatedStreamRef,
|
accumulatedStreamRef,
|
||||||
onSessionInactive,
|
statusCheckSentAtRef,
|
||||||
onSessionActive,
|
|
||||||
onSessionProcessing,
|
onSessionProcessing,
|
||||||
onSessionNotProcessing,
|
onSessionIdle,
|
||||||
onNavigateToSession,
|
onNavigateToSession,
|
||||||
onWebSocketReconnect: handleWebSocketReconnect,
|
onWebSocketReconnect: handleWebSocketReconnect,
|
||||||
sessionStore,
|
sessionStore,
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isLoading || !canAbortSession) {
|
if (!canAbortSession) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,7 +256,7 @@ function ChatInterface({
|
|||||||
return () => {
|
return () => {
|
||||||
document.removeEventListener('keydown', handleGlobalEscape, { capture: true });
|
document.removeEventListener('keydown', handleGlobalEscape, { capture: true });
|
||||||
};
|
};
|
||||||
}, [canAbortSession, handleAbortSession, isLoading]);
|
}, [canAbortSession, handleAbortSession]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return () => {
|
return () => {
|
||||||
@@ -362,10 +351,9 @@ function ChatInterface({
|
|||||||
pendingPermissionRequests={pendingPermissionRequests}
|
pendingPermissionRequests={pendingPermissionRequests}
|
||||||
handlePermissionDecision={handlePermissionDecision}
|
handlePermissionDecision={handlePermissionDecision}
|
||||||
handleGrantToolPermission={handleGrantToolPermission}
|
handleGrantToolPermission={handleGrantToolPermission}
|
||||||
claudeStatus={claudeStatus}
|
activity={sessionActivity}
|
||||||
isLoading={isLoading}
|
isLoading={isProcessing}
|
||||||
onAbortSession={handleAbortSession}
|
onAbortSession={handleAbortSession}
|
||||||
provider={provider}
|
|
||||||
permissionMode={permissionMode}
|
permissionMode={permissionMode}
|
||||||
onModeSwitch={cyclePermissionMode}
|
onModeSwitch={cyclePermissionMode}
|
||||||
tokenBudget={tokenBudget}
|
tokenBudget={tokenBudget}
|
||||||
|
|||||||
80
src/components/chat/view/subcomponents/ActivityIndicator.tsx
Normal file
80
src/components/chat/view/subcomponents/ActivityIndicator.tsx
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
|
import { Shimmer } from '../../../../shared/view/ui';
|
||||||
|
import type { SessionActivity } from '../../../../hooks/useSessionProtection';
|
||||||
|
|
||||||
|
type ActivityIndicatorProps = {
|
||||||
|
activity: SessionActivity | null;
|
||||||
|
onAbort?: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
const ACTION_KEYS = [
|
||||||
|
'claudeStatus.actions.thinking',
|
||||||
|
'claudeStatus.actions.processing',
|
||||||
|
'claudeStatus.actions.analyzing',
|
||||||
|
'claudeStatus.actions.working',
|
||||||
|
'claudeStatus.actions.computing',
|
||||||
|
'claudeStatus.actions.reasoning',
|
||||||
|
];
|
||||||
|
const DEFAULT_ACTION_WORDS = ['Thinking', 'Processing', 'Analyzing', 'Working', 'Computing', 'Reasoning'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimal response-in-progress indicator, in the spirit of the inline status
|
||||||
|
* lines in Claude Code / Codex / OpenCode: a shimmering activity label, the
|
||||||
|
* elapsed time, and an interrupt affordance. Rendered only while the viewed
|
||||||
|
* session has an entry in the processing map; it disappears the instant that
|
||||||
|
* entry is removed.
|
||||||
|
*/
|
||||||
|
export default function ActivityIndicator({ activity, onAbort }: ActivityIndicatorProps) {
|
||||||
|
const { t } = useTranslation('chat');
|
||||||
|
const startedAt = activity?.startedAt ?? null;
|
||||||
|
const [elapsedSeconds, setElapsedSeconds] = useState(0);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (startedAt === null) return;
|
||||||
|
const update = () => setElapsedSeconds(Math.max(0, Math.floor((Date.now() - startedAt) / 1000)));
|
||||||
|
update();
|
||||||
|
const timer = setInterval(update, 1000);
|
||||||
|
return () => clearInterval(timer);
|
||||||
|
}, [startedAt]);
|
||||||
|
|
||||||
|
if (!activity) return null;
|
||||||
|
|
||||||
|
const actionWords = ACTION_KEYS.map((key, i) => t(key, { defaultValue: DEFAULT_ACTION_WORDS[i] }));
|
||||||
|
const label = (activity.statusText || actionWords[Math.floor(elapsedSeconds / 4) % actionWords.length])
|
||||||
|
.replace(/\.+$/, '');
|
||||||
|
|
||||||
|
const minutes = Math.floor(elapsedSeconds / 60);
|
||||||
|
const seconds = elapsedSeconds % 60;
|
||||||
|
const elapsedLabel = minutes < 1
|
||||||
|
? t('claudeStatus.elapsed.seconds', { count: seconds, defaultValue: '{{count}}s' })
|
||||||
|
: t('claudeStatus.elapsed.minutesSeconds', { minutes, seconds, defaultValue: '{{minutes}}m {{seconds}}s' });
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="animate-in fade-in mb-2 w-full duration-300">
|
||||||
|
<div className="mx-auto flex max-w-4xl items-center gap-2 px-1">
|
||||||
|
<span className="h-1.5 w-1.5 shrink-0 animate-pulse rounded-full bg-primary" aria-hidden />
|
||||||
|
<Shimmer className="text-xs font-medium">{`${label}…`}</Shimmer>
|
||||||
|
<span className="text-xs tabular-nums text-muted-foreground/60">{elapsedLabel}</span>
|
||||||
|
|
||||||
|
{activity.canInterrupt && onAbort && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onAbort}
|
||||||
|
className="ml-auto flex items-center gap-1.5 rounded-md px-2 py-0.5 text-xs text-muted-foreground transition-colors hover:bg-destructive/10 hover:text-destructive"
|
||||||
|
aria-label={t('claudeStatus.stop', { defaultValue: 'Stop' })}
|
||||||
|
>
|
||||||
|
<svg className="h-2.5 w-2.5 fill-current" viewBox="0 0 24 24" aria-hidden>
|
||||||
|
<rect x="5" y="5" width="14" height="14" rx="2" />
|
||||||
|
</svg>
|
||||||
|
<span>{t('claudeStatus.stop', { defaultValue: 'Stop' })}</span>
|
||||||
|
<kbd className="hidden rounded border border-border/60 px-1 text-[10px] text-muted-foreground/70 sm:inline-block">
|
||||||
|
esc
|
||||||
|
</kbd>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -11,7 +11,8 @@ import type {
|
|||||||
} from 'react';
|
} from 'react';
|
||||||
import { ImageIcon, MessageSquareIcon, XIcon, ArrowDownIcon } from 'lucide-react';
|
import { ImageIcon, MessageSquareIcon, XIcon, ArrowDownIcon } from 'lucide-react';
|
||||||
|
|
||||||
import type { PendingPermissionRequest, PermissionMode, Provider } from '../../types/types';
|
import type { SessionActivity } from '../../../../hooks/useSessionProtection';
|
||||||
|
import type { PendingPermissionRequest, PermissionMode } from '../../types/types';
|
||||||
import {
|
import {
|
||||||
PromptInput,
|
PromptInput,
|
||||||
PromptInputHeader,
|
PromptInputHeader,
|
||||||
@@ -24,7 +25,7 @@ import {
|
|||||||
} from '../../../../shared/view/ui';
|
} from '../../../../shared/view/ui';
|
||||||
|
|
||||||
import CommandMenu from './CommandMenu';
|
import CommandMenu from './CommandMenu';
|
||||||
import ClaudeStatus from './ClaudeStatus';
|
import ActivityIndicator from './ActivityIndicator';
|
||||||
import ImageAttachment from './ImageAttachment';
|
import ImageAttachment from './ImageAttachment';
|
||||||
import PermissionRequestsBanner from './PermissionRequestsBanner';
|
import PermissionRequestsBanner from './PermissionRequestsBanner';
|
||||||
import TokenUsageSummary from './TokenUsageSummary';
|
import TokenUsageSummary from './TokenUsageSummary';
|
||||||
@@ -51,10 +52,9 @@ interface ChatComposerProps {
|
|||||||
decision: { allow?: boolean; message?: string; rememberEntry?: string | null; updatedInput?: unknown },
|
decision: { allow?: boolean; message?: string; rememberEntry?: string | null; updatedInput?: unknown },
|
||||||
) => void;
|
) => void;
|
||||||
handleGrantToolPermission: (suggestion: { entry: string; toolName: string }) => { success: boolean };
|
handleGrantToolPermission: (suggestion: { entry: string; toolName: string }) => { success: boolean };
|
||||||
claudeStatus: { text: string; tokens: number; can_interrupt: boolean } | null;
|
activity: SessionActivity | null;
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
onAbortSession: () => void;
|
onAbortSession: () => void;
|
||||||
provider: Provider | string;
|
|
||||||
permissionMode: PermissionMode | string;
|
permissionMode: PermissionMode | string;
|
||||||
onModeSwitch: () => void;
|
onModeSwitch: () => void;
|
||||||
tokenBudget: Record<string, unknown> | null;
|
tokenBudget: Record<string, unknown> | null;
|
||||||
@@ -105,10 +105,9 @@ export default function ChatComposer({
|
|||||||
pendingPermissionRequests,
|
pendingPermissionRequests,
|
||||||
handlePermissionDecision,
|
handlePermissionDecision,
|
||||||
handleGrantToolPermission,
|
handleGrantToolPermission,
|
||||||
claudeStatus,
|
activity,
|
||||||
isLoading,
|
isLoading,
|
||||||
onAbortSession,
|
onAbortSession,
|
||||||
provider,
|
|
||||||
permissionMode,
|
permissionMode,
|
||||||
onModeSwitch,
|
onModeSwitch,
|
||||||
tokenBudget,
|
tokenBudget,
|
||||||
@@ -173,12 +172,7 @@ export default function ChatComposer({
|
|||||||
return (
|
return (
|
||||||
<div className="flex-shrink-0 p-2 pb-2 sm:p-4 sm:pb-4 md:p-4 md:pb-6">
|
<div className="flex-shrink-0 p-2 pb-2 sm:p-4 sm:pb-4 md:p-4 md:pb-6">
|
||||||
{!hasPendingPermissions && (
|
{!hasPendingPermissions && (
|
||||||
<ClaudeStatus
|
<ActivityIndicator activity={activity} onAbort={onAbortSession} />
|
||||||
status={claudeStatus}
|
|
||||||
isLoading={isLoading}
|
|
||||||
onAbort={onAbortSession}
|
|
||||||
provider={provider}
|
|
||||||
/>
|
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{pendingPermissionRequests.length > 0 && (
|
{pendingPermissionRequests.length > 0 && (
|
||||||
|
|||||||
@@ -1,130 +0,0 @@
|
|||||||
import { useEffect, useState } from 'react';
|
|
||||||
import { useTranslation } from 'react-i18next';
|
|
||||||
import { cn } from '../../../../lib/utils';
|
|
||||||
import SessionProviderLogo from '../../../llm-logo-provider/SessionProviderLogo';
|
|
||||||
|
|
||||||
type ClaudeStatusProps = {
|
|
||||||
status: {
|
|
||||||
text?: string;
|
|
||||||
tokens?: number;
|
|
||||||
can_interrupt?: boolean;
|
|
||||||
} | null;
|
|
||||||
onAbort?: () => void;
|
|
||||||
isLoading: boolean;
|
|
||||||
provider?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const ACTION_KEYS = [
|
|
||||||
'claudeStatus.actions.thinking',
|
|
||||||
'claudeStatus.actions.processing',
|
|
||||||
'claudeStatus.actions.analyzing',
|
|
||||||
'claudeStatus.actions.working',
|
|
||||||
'claudeStatus.actions.computing',
|
|
||||||
'claudeStatus.actions.reasoning',
|
|
||||||
];
|
|
||||||
const DEFAULT_ACTION_WORDS = ['Thinking', 'Processing', 'Analyzing', 'Working', 'Computing', 'Reasoning'];
|
|
||||||
|
|
||||||
const PROVIDER_LABEL_KEYS: Record<string, string> = {
|
|
||||||
claude: 'messageTypes.claude',
|
|
||||||
codex: 'messageTypes.codex',
|
|
||||||
cursor: 'messageTypes.cursor',
|
|
||||||
gemini: 'messageTypes.gemini',
|
|
||||||
opencode: 'messageTypes.opencode',
|
|
||||||
};
|
|
||||||
|
|
||||||
function formatElapsedTime(totalSeconds: number) {
|
|
||||||
const mins = Math.floor(totalSeconds / 60);
|
|
||||||
const secs = totalSeconds % 60;
|
|
||||||
return mins < 1 ? `${secs}s` : `${mins}m ${secs}s`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function ClaudeStatus({
|
|
||||||
status,
|
|
||||||
onAbort,
|
|
||||||
isLoading,
|
|
||||||
provider = 'claude',
|
|
||||||
}: ClaudeStatusProps) {
|
|
||||||
const { t } = useTranslation('chat');
|
|
||||||
const [elapsedTime, setElapsedTime] = useState(0);
|
|
||||||
const [dots, setDots] = useState('');
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!isLoading) {
|
|
||||||
setElapsedTime(0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const startTime = Date.now();
|
|
||||||
const timer = setInterval(() => {
|
|
||||||
setElapsedTime(Math.floor((Date.now() - startTime) / 1000));
|
|
||||||
}, 1000);
|
|
||||||
const dotTimer = setInterval(() => {
|
|
||||||
setDots((prev) => (prev.length >= 3 ? '' : prev + '.'));
|
|
||||||
}, 500);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
clearInterval(timer);
|
|
||||||
clearInterval(dotTimer);
|
|
||||||
};
|
|
||||||
}, [isLoading]);
|
|
||||||
|
|
||||||
if (!isLoading && !status) return null;
|
|
||||||
|
|
||||||
const actionWords = ACTION_KEYS.map((key, i) => t(key, { defaultValue: DEFAULT_ACTION_WORDS[i] }));
|
|
||||||
const statusText = (status?.text || actionWords[Math.floor(elapsedTime / 3) % actionWords.length]).replace(/[.]+$/, '');
|
|
||||||
|
|
||||||
const providerLabel = t(PROVIDER_LABEL_KEYS[provider] || 'claudeStatus.providers.assistant', { defaultValue: 'Assistant' });
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="animate-in fade-in slide-in-from-bottom-2 mb-3 w-full duration-500">
|
|
||||||
<div className="mx-auto flex max-w-4xl items-center justify-between gap-3 overflow-hidden rounded-full border border-border/50 bg-slate-100 px-3 py-1.5 shadow-sm backdrop-blur-md dark:bg-slate-900">
|
|
||||||
|
|
||||||
{/* Left Side: Identity & Status */}
|
|
||||||
<div className="flex min-w-0 items-center gap-2.5">
|
|
||||||
<div className="relative flex h-6 w-6 shrink-0 items-center justify-center rounded-full bg-primary/20 ring-1 ring-primary/10">
|
|
||||||
<SessionProviderLogo provider={provider} className="h-3.5 w-3.5" />
|
|
||||||
{isLoading && (
|
|
||||||
<span className="absolute inset-0 animate-pulse rounded-full ring-2 ring-emerald-500/20" />
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="flex min-w-0 flex-col sm:flex-row sm:items-center sm:gap-2">
|
|
||||||
<span className="text-[10px] font-bold uppercase tracking-wider text-muted-foreground/70">
|
|
||||||
{providerLabel}
|
|
||||||
</span>
|
|
||||||
<div className="flex items-center gap-1.5">
|
|
||||||
<span className={cn("h-1.5 w-1.5 rounded-full", isLoading ? "bg-emerald-500 animate-pulse" : "bg-amber-500")} />
|
|
||||||
<p className="truncate text-xs font-medium text-foreground">
|
|
||||||
{statusText}<span className="inline-block w-4 text-primary">{isLoading ? dots : ''}</span>
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Right Side: Metrics & Actions */}
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
{isLoading && status?.can_interrupt !== false && onAbort && (
|
|
||||||
<>
|
|
||||||
<div className="hidden items-center rounded-md bg-muted/50 px-2 py-0.5 text-[10px] font-medium tabular-nums text-muted-foreground sm:flex">
|
|
||||||
{formatElapsedTime(elapsedTime)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={onAbort}
|
|
||||||
className="group flex items-center gap-1.5 rounded-full bg-destructive/10 px-2.5 py-1 text-[10px] font-bold text-destructive transition-all hover:bg-destructive hover:text-destructive-foreground"
|
|
||||||
>
|
|
||||||
<svg className="h-3 w-3 fill-current" viewBox="0 0 24 24">
|
|
||||||
<path d="M6 6h12v12H6z" />
|
|
||||||
</svg>
|
|
||||||
<span className="hidden sm:inline">STOP</span>
|
|
||||||
<kbd className="hidden rounded bg-black/10 px-1 text-[9px] group-hover:bg-white/20 sm:block">
|
|
||||||
ESC
|
|
||||||
</kbd>
|
|
||||||
</button>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,10 +1,13 @@
|
|||||||
import type { Dispatch, SetStateAction } from 'react';
|
import type { Dispatch, SetStateAction } from 'react';
|
||||||
|
|
||||||
import type { AppTab, Project, ProjectSession } from '../../../types/app';
|
import type { AppTab, Project, ProjectSession } from '../../../types/app';
|
||||||
|
import type {
|
||||||
|
MarkSessionIdle,
|
||||||
|
MarkSessionProcessing,
|
||||||
|
SessionActivityMap,
|
||||||
|
} from '../../../hooks/useSessionProtection';
|
||||||
import type { SessionNavigationOptions } from '../../chat/types/types';
|
import type { SessionNavigationOptions } from '../../chat/types/types';
|
||||||
|
|
||||||
export type SessionLifecycleHandler = (sessionId?: string | null) => void;
|
|
||||||
|
|
||||||
export type TaskMasterTask = {
|
export type TaskMasterTask = {
|
||||||
id: string | number;
|
id: string | number;
|
||||||
title?: string;
|
title?: string;
|
||||||
@@ -46,11 +49,9 @@ export type MainContentProps = {
|
|||||||
onMenuClick: () => void;
|
onMenuClick: () => void;
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
onInputFocusChange: (focused: boolean) => void;
|
onInputFocusChange: (focused: boolean) => void;
|
||||||
onSessionActive: SessionLifecycleHandler;
|
onSessionProcessing: MarkSessionProcessing;
|
||||||
onSessionInactive: SessionLifecycleHandler;
|
onSessionIdle: MarkSessionIdle;
|
||||||
onSessionProcessing: SessionLifecycleHandler;
|
processingSessions: SessionActivityMap;
|
||||||
onSessionNotProcessing: SessionLifecycleHandler;
|
|
||||||
processingSessions: Set<string>;
|
|
||||||
onNavigateToSession: (targetSessionId: string, options?: SessionNavigationOptions) => void;
|
onNavigateToSession: (targetSessionId: string, options?: SessionNavigationOptions) => void;
|
||||||
onShowSettings: () => void;
|
onShowSettings: () => void;
|
||||||
externalMessageUpdate: number;
|
externalMessageUpdate: number;
|
||||||
|
|||||||
@@ -42,10 +42,8 @@ function MainContent({
|
|||||||
onMenuClick,
|
onMenuClick,
|
||||||
isLoading,
|
isLoading,
|
||||||
onInputFocusChange,
|
onInputFocusChange,
|
||||||
onSessionActive,
|
|
||||||
onSessionInactive,
|
|
||||||
onSessionProcessing,
|
onSessionProcessing,
|
||||||
onSessionNotProcessing,
|
onSessionIdle,
|
||||||
processingSessions,
|
processingSessions,
|
||||||
onNavigateToSession,
|
onNavigateToSession,
|
||||||
onShowSettings,
|
onShowSettings,
|
||||||
@@ -131,10 +129,8 @@ function MainContent({
|
|||||||
latestMessage={latestMessage}
|
latestMessage={latestMessage}
|
||||||
onFileOpen={handleFileOpen}
|
onFileOpen={handleFileOpen}
|
||||||
onInputFocusChange={onInputFocusChange}
|
onInputFocusChange={onInputFocusChange}
|
||||||
onSessionActive={onSessionActive}
|
|
||||||
onSessionInactive={onSessionInactive}
|
|
||||||
onSessionProcessing={onSessionProcessing}
|
onSessionProcessing={onSessionProcessing}
|
||||||
onSessionNotProcessing={onSessionNotProcessing}
|
onSessionIdle={onSessionIdle}
|
||||||
processingSessions={processingSessions}
|
processingSessions={processingSessions}
|
||||||
onNavigateToSession={onNavigateToSession}
|
onNavigateToSession={onNavigateToSession}
|
||||||
onShowSettings={onShowSettings}
|
onShowSettings={onShowSettings}
|
||||||
|
|||||||
@@ -12,12 +12,14 @@ import type {
|
|||||||
ProjectsUpdatedMessage,
|
ProjectsUpdatedMessage,
|
||||||
} from '../types/app';
|
} from '../types/app';
|
||||||
|
|
||||||
|
import type { SessionActivityMap } from './useSessionProtection';
|
||||||
|
|
||||||
type UseProjectsStateArgs = {
|
type UseProjectsStateArgs = {
|
||||||
sessionId?: string;
|
sessionId?: string;
|
||||||
navigate: NavigateFunction;
|
navigate: NavigateFunction;
|
||||||
latestMessage: AppSocketMessage | null;
|
latestMessage: AppSocketMessage | null;
|
||||||
isMobile: boolean;
|
isMobile: boolean;
|
||||||
activeSessions: Set<string>;
|
activeSessions: SessionActivityMap;
|
||||||
};
|
};
|
||||||
|
|
||||||
type FetchProjectsOptions = {
|
type FetchProjectsOptions = {
|
||||||
|
|||||||
@@ -1,55 +1,103 @@
|
|||||||
import { useCallback, useState } from 'react';
|
import { useCallback, useState } from 'react';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map key for a request that is in flight before the provider has announced
|
||||||
|
* its real session id (a brand-new conversation). `session_created` migrates
|
||||||
|
* the entry to the concrete session id.
|
||||||
|
*/
|
||||||
|
export const PENDING_SESSION_ID = '__pending_session__';
|
||||||
|
|
||||||
|
export interface SessionActivity {
|
||||||
|
/** Provider-supplied status line; null renders the default activity label. */
|
||||||
|
statusText: string | null;
|
||||||
|
canInterrupt: boolean;
|
||||||
|
/**
|
||||||
|
* When this request was first marked as processing (client clock). Drives
|
||||||
|
* the elapsed-time display and the stale `session-status` reply guard.
|
||||||
|
*/
|
||||||
|
startedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SessionActivityMap = ReadonlyMap<string, SessionActivity>;
|
||||||
|
|
||||||
|
export type MarkSessionProcessing = (
|
||||||
|
sessionId?: string | null,
|
||||||
|
activity?: { statusText?: string | null; canInterrupt?: boolean },
|
||||||
|
) => void;
|
||||||
|
|
||||||
|
export type MarkSessionIdle = (
|
||||||
|
sessionId?: string | null,
|
||||||
|
opts?: { ifStartedBefore?: number },
|
||||||
|
) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Single source of truth for which sessions are actively processing a
|
||||||
|
* request. Everything the chat UI shows (activity indicator, abort
|
||||||
|
* availability, status text) is derived from this map; terminal events
|
||||||
|
* (`complete`, `error`, abort, an authoritative idle status reply) delete the
|
||||||
|
* entry atomically. The map also drives session protection: project refreshes
|
||||||
|
* are suppressed for sessions that have an entry here.
|
||||||
|
*/
|
||||||
export function useSessionProtection() {
|
export function useSessionProtection() {
|
||||||
const [activeSessions, setActiveSessions] = useState<Set<string>>(new Set());
|
const [processingSessions, setProcessingSessions] = useState<Map<string, SessionActivity>>(
|
||||||
const [processingSessions, setProcessingSessions] = useState<Set<string>>(new Set());
|
new Map(),
|
||||||
|
);
|
||||||
|
|
||||||
const markSessionAsActive = useCallback((sessionId?: string | null) => {
|
const markSessionProcessing = useCallback<MarkSessionProcessing>((sessionId, activity) => {
|
||||||
if (!sessionId) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setActiveSessions((prev) => new Set([...prev, sessionId]));
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const markSessionAsInactive = useCallback((sessionId?: string | null) => {
|
|
||||||
if (!sessionId) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setActiveSessions((prev) => {
|
|
||||||
const next = new Set(prev);
|
|
||||||
next.delete(sessionId);
|
|
||||||
return next;
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const markSessionAsProcessing = useCallback((sessionId?: string | null) => {
|
|
||||||
if (!sessionId) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setProcessingSessions((prev) => new Set([...prev, sessionId]));
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const markSessionAsNotProcessing = useCallback((sessionId?: string | null) => {
|
|
||||||
if (!sessionId) {
|
if (!sessionId) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setProcessingSessions((prev) => {
|
setProcessingSessions((prev) => {
|
||||||
const next = new Set(prev);
|
const existing = prev.get(sessionId);
|
||||||
next.delete(sessionId);
|
const next: SessionActivity = {
|
||||||
return next;
|
statusText:
|
||||||
|
activity?.statusText !== undefined ? activity.statusText : existing?.statusText ?? null,
|
||||||
|
canInterrupt: activity?.canInterrupt ?? existing?.canInterrupt ?? true,
|
||||||
|
startedAt: existing?.startedAt ?? Date.now(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (
|
||||||
|
existing
|
||||||
|
&& existing.statusText === next.statusText
|
||||||
|
&& existing.canInterrupt === next.canInterrupt
|
||||||
|
) {
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
const updated = new Map(prev);
|
||||||
|
updated.set(sessionId, next);
|
||||||
|
return updated;
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const markSessionIdle = useCallback<MarkSessionIdle>((sessionId, opts) => {
|
||||||
|
if (!sessionId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setProcessingSessions((prev) => {
|
||||||
|
const existing = prev.get(sessionId);
|
||||||
|
if (!existing) {
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Guard against stale `check-session-status` replies: if a new request
|
||||||
|
// started after the check was sent, the idle reply describes the older
|
||||||
|
// request and must not clear the newer one.
|
||||||
|
if (opts?.ifStartedBefore !== undefined && existing.startedAt >= opts.ifStartedBefore) {
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
const updated = new Map(prev);
|
||||||
|
updated.delete(sessionId);
|
||||||
|
return updated;
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
activeSessions,
|
|
||||||
processingSessions,
|
processingSessions,
|
||||||
markSessionAsActive,
|
markSessionProcessing,
|
||||||
markSessionAsInactive,
|
markSessionIdle,
|
||||||
markSessionAsProcessing,
|
|
||||||
markSessionAsNotProcessing,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -224,6 +224,7 @@
|
|||||||
"label": "{{time}} elapsed",
|
"label": "{{time}} elapsed",
|
||||||
"startingNow": "Starting now"
|
"startingNow": "Starting now"
|
||||||
},
|
},
|
||||||
|
"stop": "Stop",
|
||||||
"controls": {
|
"controls": {
|
||||||
"stopGeneration": "Stop Generation",
|
"stopGeneration": "Stop Generation",
|
||||||
"pressEscToStop": "Press Esc anytime to stop"
|
"pressEscToStop": "Press Esc anytime to stop"
|
||||||
|
|||||||
Reference in New Issue
Block a user