Files
openhuman/src/store/threadSlice.ts
T
6acdfef488 Implement hybrid local/API thread storage with message persistence (#154)
* feat: add .mcp.json for MCP server configuration

- Introduced `.mcp.json` with server details for managing MCP integrations
- Defines `readme` server with HTTP type and URL endpoint configuration

* fix: Update skills submodule with Telegram error handling improvements

- Fixed setup flow showing false success when TDLib errors occurred
- Improved async error handling in setup steps
- Added client reset mechanisms for error recovery
- Enhanced error messaging for better user experience
- Cleaned up excessive debug logging while maintaining error logs

Resolves issue where Telegram setup showed success modal despite underlying TDLib errors.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Add daemon lifecycle management system to frontend UI

This implementation integrates the Rust daemon's health monitoring and lifecycle management into the React frontend. Key features include real-time health indicators, automatic start/error recovery, and detailed control panels, enhancing both user experience and operational reliability.

* Enhance daemon health monitoring with detailed logging, event tracking, and state management improvements.

* Add extensive logging for Tauri socket lifecycle and event listeners

This update improves troubleshooting and debugging by adding detailed logs throughout Tauri socket initialization, event listener setup, and error handling processes.

* Define global Tauri interface types in TypeScript

* Fix daemon service management and health communication issues

This comprehensive fix resolves two critical daemon-related issues:

1. **Launchctl "Input/output error" fix**:
   - Add intelligent state checking before service operations
   - Only load LaunchAgent if not already loaded
   - Treat "already running" as success, not failure
   - Add helper functions for cross-platform service state detection
   - Implement idempotent service start operations

2. **Daemon health communication fix**:
   - Add ALPHAHUMAN_DAEMON_INTERNAL environment variable to external service plist
   - Force external daemon to use file-based communication (daemon_state.json)
   - Implement file watching bridge in main app to emit Tauri events
   - Ensure frontend receives proper health updates from external daemon

**Key Changes**:
- Enhanced `start()` function with state checking across all platforms
- Added `is_service_loaded_macos()`, `is_service_enabled_linux()`, `is_task_exists_windows()`
- Modified macOS plist to include `ALPHAHUMAN_DAEMON_INTERNAL=false`
- Added `watch_daemon_health_file()` function for external daemon communication
- Updated daemon mode detection logic for cross-platform consistency
- Added comprehensive logging for service management operations

**Expected Results**:
- No more "Input/output error" when clicking daemon start button
- External daemon status shows "connected" instead of "disconnected"
- Idempotent service operations (safe to run multiple times)
- Single daemon process prevents resource conflicts
- Cross-platform daemon service reliability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Add `ALPHAHUMAN_DAEMON_INTERNAL` env variable to configuration documentation

* Optimize imports in `TauriCommandsPanel.tsx` by removing unused variables and adding necessary hooks.

* Add OAuth login support with configurable provider types and UI components

This update introduces OAuth login functionality, including support for multiple providers (Google, GitHub, Twitter, Discord). Added reusable components for provider buttons and a login section, along with styled configurations and type definitions. Includes enhancements for token handling and updated login flows.

* Add OAuth debug mode support for development environments

Introduce `IS_DEV` checks to enable OAuth debug mode, allowing detailed logging and adjustable OAuth URLs with debug parameters during development.

* Implement hybrid local/API thread storage with message persistence

- Add local-first thread and message storage using Redux Persist
- Persist threads, messages, and UI state (panelWidth, lastViewedAt, selectedThreadId) locally
- Remove API dependencies for thread/message fetching (fetchThreads, fetchThreadMessages)
- Add local thread management actions (createThreadLocal, deleteThreadLocal, addMessageLocal)
- Fix message persistence issue: ensure both user and AI messages survive navigation
- Maintain API integration for sendMessage functionality with AI responses
- Preserve optimistic updates and error handling behavior
- Enable instant app startup and offline conversation viewing

Key changes:
- Enhanced threadSlice with messagesByThreadId storage and local management actions
- Updated Redux persist config to include thread data
- Modified Conversations component to use local thread operations
- Fixed addInferenceResponse to persist preceding user messages
- Maintained all existing UI patterns and error states

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-02 18:13:01 +03:00

367 lines
12 KiB
TypeScript

import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { threadApi } from '../services/api/threadApi';
import type { Thread, ThreadMessage } from '../types/thread';
interface ThreadState {
// Existing local data (will be persisted)
threads: Thread[];
selectedThreadId: string | null;
panelWidth: number;
lastViewedAt: Record<string, number>;
// NEW: Add efficient message storage
messagesByThreadId: Record<string, ThreadMessage[]>;
// Current messages view (not persisted)
messages: ThreadMessage[];
// Keep these API states (NOT persisted)
isLoadingMessages: boolean; // For AI response waiting
messagesError: string | null; // For send API errors
sendStatus: 'idle' | 'loading' | 'success' | 'error';
sendError: string | null;
deleteStatus: 'idle' | 'loading' | 'success' | 'error';
purgeStatus: 'idle' | 'loading' | 'success' | 'error';
suggestedQuestions: Array<{ text: string; confidence: number }>;
isLoadingSuggestions: boolean;
suggestError: string | null;
}
const initialState: ThreadState = {
threads: [],
selectedThreadId: null,
panelWidth: 320,
lastViewedAt: {},
messagesByThreadId: {},
messages: [],
isLoadingMessages: false,
messagesError: null,
sendStatus: 'idle',
sendError: null,
deleteStatus: 'idle',
purgeStatus: 'idle',
suggestedQuestions: [],
isLoadingSuggestions: false,
suggestError: null,
};
// Removed fetchThreads - threads are now managed locally
// Removed fetchThreadMessages - messages are now managed locally
// Removed createThread - using local thread creation
// Removed deleteThread - using local thread deletion
export const purgeThreads = createAsyncThunk(
'thread/purgeThreads',
async (_, { dispatch, rejectWithValue }) => {
try {
const data = await threadApi.purge({
messages: false,
agentThreads: true,
deleteEverything: true,
});
// Clear local threads after successful purge
dispatch({ type: 'thread/clearAllThreads' });
return data;
} catch (error) {
const msg =
error && typeof error === 'object' && 'error' in error
? String(error.error)
: 'Failed to purge threads';
return rejectWithValue(msg);
}
}
);
export const sendMessage = createAsyncThunk(
'thread/sendMessage',
async (
{ threadId, message }: { threadId: string; message: string },
{ dispatch, getState, rejectWithValue }
) => {
// 1. Add user message locally immediately (optimistic update)
const userMessage: ThreadMessage = {
id: `msg_${Date.now()}_${Math.random()}`,
content: message,
type: 'text',
extraMetadata: {},
sender: 'user',
createdAt: new Date().toISOString(),
};
try {
dispatch(addMessageLocal({ threadId, message: userMessage }));
// 2. Send to API (existing logic)
const data = await threadApi.sendMessage(message, threadId);
// 3. For now, we'll handle AI response via the existing inference API
// The AI response will be added separately via addInferenceResponse
return data;
} catch (error) {
// Remove optimistic user message on failure
const state = (getState() as { thread: ThreadState }).thread;
const messages = state.messagesByThreadId[threadId] || [];
const filteredMessages = messages.filter(m => m.id !== userMessage.id);
dispatch(updateMessagesForThread({ threadId, messages: filteredMessages }));
const msg =
error && typeof error === 'object' && 'error' in error
? String((error as { error: unknown }).error)
: 'Failed to send message';
return rejectWithValue(msg);
}
}
);
export const fetchSuggestedQuestions = createAsyncThunk(
'thread/fetchSuggestedQuestions',
async (conversationId: string | undefined, { rejectWithValue }) => {
try {
const data = await threadApi.getSuggestQuestions(conversationId);
return data.suggestions;
} catch (error) {
const msg =
error && typeof error === 'object' && 'error' in error
? String(error.error)
: 'Failed to load suggestions';
return rejectWithValue(msg);
}
}
);
const threadSlice = createSlice({
name: 'thread',
initialState,
reducers: {
setSelectedThread: (state, action: { payload: string }) => {
state.selectedThreadId = action.payload;
// Load messages from local storage instead of clearing
state.messages = state.messagesByThreadId[action.payload] || [];
state.messagesError = null;
state.suggestedQuestions = [];
state.suggestError = null;
},
clearSelectedThread: state => {
state.selectedThreadId = null;
state.messages = [];
state.messagesError = null;
state.suggestedQuestions = [];
state.suggestError = null;
},
clearSuggestedQuestions: state => {
state.suggestedQuestions = [];
state.suggestError = null;
},
clearDeleteStatus: state => {
state.deleteStatus = 'idle';
},
clearPurgeStatus: state => {
state.purgeStatus = 'idle';
},
addOptimisticMessage: (state, action: { payload: { content: string } }) => {
state.messages.push({
id: `optimistic-${Date.now()}`,
content: action.payload.content,
type: 'text',
extraMetadata: {},
sender: 'user',
createdAt: new Date().toISOString(),
});
},
addInferenceResponse: (state, action: { payload: { content: string } }) => {
const aiMessage: ThreadMessage = {
id: `inference-${Date.now()}`,
content: action.payload.content,
type: 'text',
extraMetadata: {},
sender: 'agent',
createdAt: new Date().toISOString(),
};
// Add to current messages view
state.messages.push(aiMessage);
// Also add to persistent storage
if (state.selectedThreadId) {
if (!state.messagesByThreadId[state.selectedThreadId]) {
state.messagesByThreadId[state.selectedThreadId] = [];
}
// CRITICAL FIX: Ensure the preceding user message is also persisted
// Find the last user message that might not be in persistent storage yet
const lastUserMessage = state.messages
.filter(m => m.sender === 'user')
.pop();
if (lastUserMessage) {
const persistedMessages = state.messagesByThreadId[state.selectedThreadId];
const userMessageExists = persistedMessages.some(m => m.id === lastUserMessage.id);
// If user message isn't persisted yet, add it first
if (!userMessageExists) {
persistedMessages.push(lastUserMessage);
}
}
// Now add the AI response
state.messagesByThreadId[state.selectedThreadId].push(aiMessage);
// Update thread metadata
const thread = state.threads.find(t => t.id === state.selectedThreadId);
if (thread) {
thread.messageCount = state.messagesByThreadId[state.selectedThreadId].length;
thread.lastMessageAt = aiMessage.createdAt;
}
}
},
removeOptimisticMessages: state => {
state.messages = state.messages.filter(m => !m.id.startsWith('optimistic-'));
},
clearSendError: state => {
state.sendError = null;
},
setPanelWidth: (state, action: { payload: number }) => {
state.panelWidth = action.payload;
},
setLastViewed: (state, action: { payload: string }) => {
const ts = Date.now();
state.lastViewedAt[action.payload] = ts;
},
// Local thread management
createThreadLocal: (state, action: { payload: { id: string; title: string; createdAt: string } }) => {
const newThread: Thread = {
id: action.payload.id,
title: action.payload.title,
chatId: null,
isActive: true,
messageCount: 0,
lastMessageAt: action.payload.createdAt,
createdAt: action.payload.createdAt,
};
state.threads.unshift(newThread);
state.messagesByThreadId[action.payload.id] = [];
},
addMessageLocal: (state, action: { payload: { threadId: string; message: ThreadMessage } }) => {
const { threadId, message } = action.payload;
if (!state.messagesByThreadId[threadId]) {
state.messagesByThreadId[threadId] = [];
}
state.messagesByThreadId[threadId].push(message);
// Update thread metadata
const thread = state.threads.find(t => t.id === threadId);
if (thread) {
thread.messageCount = state.messagesByThreadId[threadId].length;
thread.lastMessageAt = message.createdAt;
}
},
deleteThreadLocal: (state, action: { payload: string }) => {
const threadId = action.payload;
state.threads = state.threads.filter(t => t.id !== threadId);
delete state.messagesByThreadId[threadId];
delete state.lastViewedAt[threadId];
if (state.selectedThreadId === threadId) {
state.selectedThreadId = null;
}
},
updateMessagesForThread: (state, action: { payload: { threadId: string; messages: ThreadMessage[] } }) => {
const { threadId, messages } = action.payload;
state.messagesByThreadId[threadId] = messages;
// Update thread metadata
const thread = state.threads.find(t => t.id === threadId);
if (thread) {
thread.messageCount = messages.length;
thread.lastMessageAt = messages.length > 0 ? messages[messages.length - 1].createdAt : thread.createdAt;
}
},
clearAllThreads: (state) => {
state.threads = [];
state.messagesByThreadId = {};
state.selectedThreadId = null;
state.messages = [];
state.lastViewedAt = {};
},
},
extraReducers: builder => {
builder
// Removed fetchThreads and fetchThreadMessages cases
// Removed createThread cases - using local thread creation
// Removed deleteThread cases - using local thread deletion
// purgeThreads
.addCase(purgeThreads.pending, state => {
state.purgeStatus = 'loading';
})
.addCase(purgeThreads.fulfilled, state => {
state.purgeStatus = 'success';
// clearAllThreads is dispatched from the thunk
})
.addCase(purgeThreads.rejected, state => {
state.purgeStatus = 'error';
})
// clearAllThreads action
.addCase('thread/clearAllThreads', state => {
state.threads = [];
state.messagesByThreadId = {};
state.selectedThreadId = null;
state.messages = [];
state.lastViewedAt = {};
})
// sendMessage
.addCase(sendMessage.pending, state => {
state.sendStatus = 'loading';
state.sendError = null;
})
.addCase(sendMessage.fulfilled, state => {
state.sendStatus = 'success';
state.suggestedQuestions = [];
state.suggestError = null;
})
.addCase(sendMessage.rejected, (state, action) => {
state.sendStatus = 'error';
state.sendError = action.payload as string;
// Remove optimistic messages so the user doesn't see phantom messages
state.messages = state.messages.filter(m => !m.id.startsWith('optimistic-'));
})
// fetchSuggestedQuestions
.addCase(fetchSuggestedQuestions.pending, state => {
state.isLoadingSuggestions = true;
state.suggestError = null;
})
.addCase(fetchSuggestedQuestions.fulfilled, (state, action) => {
state.isLoadingSuggestions = false;
state.suggestedQuestions = action.payload;
})
.addCase(fetchSuggestedQuestions.rejected, (state, action) => {
state.isLoadingSuggestions = false;
state.suggestError = action.payload as string;
state.suggestedQuestions = [];
});
},
});
export const {
setSelectedThread,
clearSelectedThread,
clearDeleteStatus,
clearPurgeStatus,
addOptimisticMessage,
addInferenceResponse,
removeOptimisticMessages,
clearSendError,
clearSuggestedQuestions,
setPanelWidth,
setLastViewed,
createThreadLocal,
addMessageLocal,
deleteThreadLocal,
updateMessagesForThread,
clearAllThreads,
} = threadSlice.actions;
export default threadSlice.reducer;