Files
autocoder/ui/src/hooks/useAssistantChat.ts
Auto a0f7e72361 fix: consolidate auth error handling and fix start.bat credential check
This commit addresses issues found during review of PRs #12 and #28:

## PR #12 (Auth Error Handling) Fixes

- Create shared auth.py module with centralized AUTH_ERROR_PATTERNS,
  is_auth_error(), and print_auth_error_help() functions
- Fix start.bat to use directory check instead of outdated
  .credentials.json file check (matching start.sh behavior)
- Update process_manager.py to import from shared auth module
- Update start.py to import from shared auth module
- Update documentation comments in autonomous_agent_demo.py and
  client.py to remove references to deprecated .credentials.json

## PR #28 (Feature Management) Improvements

- Add _priority_lock threading lock to feature_mcp.py to prevent
  race conditions when multiple features are created simultaneously
- Apply lock to feature_create, feature_create_bulk, and feature_skip
- Add checkAndSendTimeoutRef cleanup in useAssistantChat.ts to
  prevent memory leaks on component unmount
- Clear currentAssistantMessageRef on response_done

## Code Quality

- All Python files pass ruff linting
- All security tests pass (91/91)
- UI passes ESLint and TypeScript compilation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 12:19:32 +02:00

353 lines
11 KiB
TypeScript
Executable File

/**
* Hook for managing assistant chat WebSocket connection
*/
import { useState, useCallback, useRef, useEffect } from "react";
import type { ChatMessage, AssistantChatServerMessage } from "../lib/types";
type ConnectionStatus = "disconnected" | "connecting" | "connected" | "error";
interface UseAssistantChatOptions {
projectName: string;
onError?: (error: string) => void;
}
interface UseAssistantChatReturn {
messages: ChatMessage[];
isLoading: boolean;
connectionStatus: ConnectionStatus;
conversationId: number | null;
start: (conversationId?: number | null) => void;
sendMessage: (content: string) => void;
disconnect: () => void;
clearMessages: () => void;
}
function generateId(): string {
return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
}
export function useAssistantChat({
projectName,
onError,
}: UseAssistantChatOptions): UseAssistantChatReturn {
const [messages, setMessages] = useState<ChatMessage[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [connectionStatus, setConnectionStatus] =
useState<ConnectionStatus>("disconnected");
const [conversationId, setConversationId] = useState<number | null>(null);
const wsRef = useRef<WebSocket | null>(null);
const currentAssistantMessageRef = useRef<string | null>(null);
const reconnectAttempts = useRef(0);
const maxReconnectAttempts = 3;
const pingIntervalRef = useRef<number | null>(null);
const reconnectTimeoutRef = useRef<number | null>(null);
const checkAndSendTimeoutRef = useRef<number | null>(null);
// Clean up on unmount
useEffect(() => {
return () => {
if (pingIntervalRef.current) {
clearInterval(pingIntervalRef.current);
}
if (reconnectTimeoutRef.current) {
clearTimeout(reconnectTimeoutRef.current);
}
if (checkAndSendTimeoutRef.current) {
clearTimeout(checkAndSendTimeoutRef.current);
}
if (wsRef.current) {
wsRef.current.close();
}
currentAssistantMessageRef.current = null;
};
}, []);
const connect = useCallback(() => {
// Prevent multiple connection attempts
if (
wsRef.current?.readyState === WebSocket.OPEN ||
wsRef.current?.readyState === WebSocket.CONNECTING
) {
return;
}
setConnectionStatus("connecting");
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const host = window.location.host;
const wsUrl = `${protocol}//${host}/api/assistant/ws/${encodeURIComponent(projectName)}`;
const ws = new WebSocket(wsUrl);
wsRef.current = ws;
ws.onopen = () => {
setConnectionStatus("connected");
reconnectAttempts.current = 0;
// Start ping interval to keep connection alive
pingIntervalRef.current = window.setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ type: "ping" }));
}
}, 30000);
};
ws.onclose = () => {
setConnectionStatus("disconnected");
if (pingIntervalRef.current) {
clearInterval(pingIntervalRef.current);
pingIntervalRef.current = null;
}
// Attempt reconnection if not intentionally closed
if (reconnectAttempts.current < maxReconnectAttempts) {
reconnectAttempts.current++;
const delay = Math.min(
1000 * Math.pow(2, reconnectAttempts.current),
10000,
);
reconnectTimeoutRef.current = window.setTimeout(connect, delay);
}
};
ws.onerror = () => {
setConnectionStatus("error");
onError?.("WebSocket connection error");
};
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data) as AssistantChatServerMessage;
switch (data.type) {
case "text": {
// Append text to current assistant message or create new one
setMessages((prev) => {
const lastMessage = prev[prev.length - 1];
if (
lastMessage?.role === "assistant" &&
lastMessage.isStreaming
) {
// Append to existing streaming message
return [
...prev.slice(0, -1),
{
...lastMessage,
content: lastMessage.content + data.content,
},
];
} else {
// Create new assistant message
currentAssistantMessageRef.current = generateId();
return [
...prev,
{
id: currentAssistantMessageRef.current,
role: "assistant",
content: data.content,
timestamp: new Date(),
isStreaming: true,
},
];
}
});
break;
}
case "tool_call": {
// Generate user-friendly tool descriptions
let toolDescription = `Using tool: ${data.tool}`;
if (data.tool === "mcp__features__feature_create") {
const input = data.input as { name?: string; category?: string };
toolDescription = `Creating feature: "${input.name || "New Feature"}" in ${input.category || "General"}`;
} else if (data.tool === "mcp__features__feature_create_bulk") {
const input = data.input as {
features?: Array<{ name: string }>;
};
const count = input.features?.length || 0;
toolDescription = `Creating ${count} feature${count !== 1 ? "s" : ""}`;
} else if (data.tool === "mcp__features__feature_skip") {
toolDescription = `Skipping feature (moving to end of queue)`;
} else if (data.tool === "mcp__features__feature_get_stats") {
toolDescription = `Checking project progress`;
} else if (data.tool === "mcp__features__feature_get_next") {
toolDescription = `Getting next pending feature`;
} else if (data.tool === "Read") {
const input = data.input as { file_path?: string };
const path = input.file_path || "";
const filename = path.split("/").pop() || path;
toolDescription = `Reading file: ${filename}`;
} else if (data.tool === "Glob") {
const input = data.input as { pattern?: string };
toolDescription = `Searching for files: ${input.pattern || "..."}`;
} else if (data.tool === "Grep") {
const input = data.input as { pattern?: string };
toolDescription = `Searching for: ${input.pattern || "..."}`;
}
// Show tool call as system message
setMessages((prev) => [
...prev,
{
id: generateId(),
role: "system",
content: toolDescription,
timestamp: new Date(),
},
]);
break;
}
case "conversation_created": {
setConversationId(data.conversation_id);
break;
}
case "response_done": {
setIsLoading(false);
currentAssistantMessageRef.current = null;
// Mark current message as done streaming
setMessages((prev) => {
const lastMessage = prev[prev.length - 1];
if (
lastMessage?.role === "assistant" &&
lastMessage.isStreaming
) {
return [
...prev.slice(0, -1),
{ ...lastMessage, isStreaming: false },
];
}
return prev;
});
break;
}
case "error": {
setIsLoading(false);
onError?.(data.content);
// Add error as system message
setMessages((prev) => [
...prev,
{
id: generateId(),
role: "system",
content: `Error: ${data.content}`,
timestamp: new Date(),
},
]);
break;
}
case "pong": {
// Keep-alive response, nothing to do
break;
}
}
} catch (e) {
console.error("Failed to parse WebSocket message:", e);
}
};
}, [projectName, onError]);
const start = useCallback(
(existingConversationId?: number | null) => {
// Clear any pending check timeout from previous call
if (checkAndSendTimeoutRef.current) {
clearTimeout(checkAndSendTimeoutRef.current);
checkAndSendTimeoutRef.current = null;
}
connect();
// Wait for connection then send start message
const checkAndSend = () => {
if (wsRef.current?.readyState === WebSocket.OPEN) {
checkAndSendTimeoutRef.current = null;
setIsLoading(true);
const payload: { type: string; conversation_id?: number } = {
type: "start",
};
if (existingConversationId) {
payload.conversation_id = existingConversationId;
setConversationId(existingConversationId);
}
wsRef.current.send(JSON.stringify(payload));
} else if (wsRef.current?.readyState === WebSocket.CONNECTING) {
checkAndSendTimeoutRef.current = window.setTimeout(checkAndSend, 100);
} else {
checkAndSendTimeoutRef.current = null;
}
};
checkAndSendTimeoutRef.current = window.setTimeout(checkAndSend, 100);
},
[connect],
);
const sendMessage = useCallback(
(content: string) => {
if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) {
onError?.("Not connected");
return;
}
// Add user message to chat
setMessages((prev) => [
...prev,
{
id: generateId(),
role: "user",
content,
timestamp: new Date(),
},
]);
setIsLoading(true);
// Send to server
wsRef.current.send(
JSON.stringify({
type: "message",
content,
}),
);
},
[onError],
);
const disconnect = useCallback(() => {
reconnectAttempts.current = maxReconnectAttempts; // Prevent reconnection
if (pingIntervalRef.current) {
clearInterval(pingIntervalRef.current);
pingIntervalRef.current = null;
}
if (wsRef.current) {
wsRef.current.close();
wsRef.current = null;
}
setConnectionStatus("disconnected");
}, []);
const clearMessages = useCallback(() => {
setMessages([]);
setConversationId(null);
}, []);
return {
messages,
isLoading,
connectionStatus,
conversationId,
start,
sendMessage,
disconnect,
clearMessages,
};
}