Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import '@/i18n'
import 'react-native-reanimated'

import { DATABASE_NAME, db, expoDb } from '@db'
import { db, expoDb } from '@db'
import { DarkTheme, DefaultTheme, NavigationContainer } from '@react-navigation/native'
import { useMigrations } from 'drizzle-orm/expo-sqlite/migrator'
import { useDrizzleStudio } from 'expo-drizzle-studio-plugin'
import { useFonts } from 'expo-font'
import * as SplashScreen from 'expo-splash-screen'
import { SQLiteProvider } from 'expo-sqlite'
import { HeroUINativeProvider } from 'heroui-native'
import React, { Suspense, useEffect } from 'react'
import { ActivityIndicator } from 'react-native'
Expand Down Expand Up @@ -138,11 +137,7 @@ export default function App() {
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<SafeAreaProvider>
<Suspense fallback={<ActivityIndicator size="large" />}>
<SQLiteProvider databaseName={DATABASE_NAME} options={{ enableChangeListener: true }} useSuspense>
<AppWithRedux />
</SQLiteProvider>
</Suspense>
<AppWithRedux />
</SafeAreaProvider>
</GestureHandlerRootView>
)
Expand Down
25 changes: 15 additions & 10 deletions src/componentsV2/features/Assistant/PromptTabContent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MotiView } from 'moti'
import React, { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Pressable } from 'react-native'
import { KeyboardAvoidingView } from 'react-native-keyboard-controller'

import TextField from '@/componentsV2/base/TextField'
Expand Down Expand Up @@ -69,7 +70,8 @@ export function PromptTabContent({ assistant, updateAssistant }: PromptTabConten
<TextField.Label className="text-foreground-secondary text-sm font-medium">
{t('common.prompt')}
</TextField.Label>
<TextField.Input
<Pressable
className="flex-1"
onPress={() => {
presentPromptDetailSheet(
formData.prompt,
Expand All @@ -81,15 +83,18 @@ export function PromptTabContent({ assistant, updateAssistant }: PromptTabConten
}
}
)
}}
editable={false}
className="flex-1 rounded-lg px-3 py-3 text-sm"
placeholder={t('common.prompt')}
multiline
numberOfLines={20}
textAlignVertical="top"
value={formData.prompt}
/>
}}>
<TextField.Input
editable={false}
pointerEvents="none"
className="flex-1 rounded-lg px-3 py-3 text-sm"
placeholder={t('common.prompt')}
multiline
numberOfLines={20}
textAlignVertical="top"
value={formData.prompt}
/>
</Pressable>
</TextField>
</YStack>
</KeyboardAvoidingView>
Expand Down
51 changes: 11 additions & 40 deletions src/services/AssistantService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,18 +602,12 @@ export class AssistantService {
}

/**
* Perform optimistic assistant update with rollback on failure
* Perform assistant update with database-first approach
*
* Sequence: Database Transaction → Cache Update → Notify Subscribers
* This prevents race conditions with dual database connections
*/
private async performAssistantUpdate(assistantId: string, updates: Partial<Omit<Assistant, 'id'>>): Promise<void> {
// Save old data for rollback
const oldSystemAssistant = this.systemAssistantsCache.get(assistantId)
? { ...this.systemAssistantsCache.get(assistantId)! }
: null
const oldLRUAssistant = this.assistantCache.get(assistantId) ? { ...this.assistantCache.get(assistantId)! } : null
const oldAllAssistant = this.allAssistantsCache.get(assistantId)
? { ...this.allAssistantsCache.get(assistantId)! }
: null

try {
// Fetch current assistant data
let currentAssistantData: Assistant
Expand All @@ -639,44 +633,21 @@ export class AssistantService {
id: assistantId
}

// Optimistic update: update all caches
// Persist to database FIRST (before any cache updates or notifications)
await assistantDatabase.upsertAssistants([updatedAssistant])

// Update caches after successful database operation
this.updateAssistantInCache(assistantId, updatedAssistant)

// Notify subscribers (UI updates immediately)
// Notify subscribers after database and cache are in sync
this.notifyAssistantSubscribers(assistantId)

// Persist to database
await assistantDatabase.upsertAssistants([updatedAssistant])

// Notify other subscribers
this.notifyGlobalSubscribers()
this.notifyAllAssistantsSubscribers()

logger.debug(`Assistant updated successfully: ${assistantId}`)
} catch (error) {
// Rollback on failure
logger.error('Failed to update assistant, rolling back:', error as Error)

if (oldSystemAssistant) {
this.systemAssistantsCache.set(assistantId, oldSystemAssistant)
} else {
this.systemAssistantsCache.delete(assistantId)
}

if (oldLRUAssistant) {
this.assistantCache.set(assistantId, oldLRUAssistant)
} else {
this.assistantCache.delete(assistantId)
}

if (oldAllAssistant) {
this.allAssistantsCache.set(assistantId, oldAllAssistant)
} else {
this.allAssistantsCache.delete(assistantId)
}

this.notifyAssistantSubscribers(assistantId)

// No rollback needed since we didn't update caches before transaction
logger.error('Failed to update assistant:', error as Error)
throw error
}
}
Expand Down