diff --git a/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/local/AppDatabase.kt b/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/local/AppDatabase.kt new file mode 100644 index 0000000000..b3a7280f8a --- /dev/null +++ b/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/local/AppDatabase.kt @@ -0,0 +1,34 @@ +package com.example.llama.revamp.data.local + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase + +/** + * Main database for the application. + */ +@Database(entities = [SystemPromptEntity::class], version = 1, exportSchema = false) +abstract class AppDatabase : RoomDatabase() { + + abstract fun systemPromptDao(): SystemPromptDao + + companion object { + @Volatile + private var INSTANCE: AppDatabase? = null + + fun getDatabase(context: Context): AppDatabase { + return INSTANCE ?: synchronized(this) { + val instance = Room.databaseBuilder( + context.applicationContext, + AppDatabase::class.java, + "llama_app_database" + ) + .fallbackToDestructiveMigration() + .build() + INSTANCE = instance + instance + } + } + } +} diff --git a/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/local/SystemPromptDao.kt b/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/local/SystemPromptDao.kt new file mode 100644 index 0000000000..786583f4ed --- /dev/null +++ b/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/local/SystemPromptDao.kt @@ -0,0 +1,44 @@ +package com.example.llama.revamp.data.local + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import kotlinx.coroutines.flow.Flow + +/** + * Data Access Object for System Prompts. + */ +@Dao +interface SystemPromptDao { + + @Query("SELECT * FROM system_prompts ORDER BY timestamp DESC") + fun getAllPrompts(): Flow> + + @Query("SELECT * FROM system_prompts WHERE id = :id") + suspend fun getPromptById(id: String): SystemPromptEntity? + + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun insertPrompt(prompt: SystemPromptEntity) + + @Delete + suspend fun deletePrompt(prompt: SystemPromptEntity) + + @Query("DELETE FROM system_prompts WHERE id = :id") + suspend fun deletePromptById(id: String) + + @Query("DELETE FROM system_prompts") + suspend fun deleteAllPrompts() + + @Query("SELECT COUNT(*) FROM system_prompts") + suspend fun getPromptCount(): Int + + // Get the most recent prompts, limited by count + @Query("SELECT * FROM system_prompts ORDER BY timestamp DESC LIMIT :count") + fun getRecentPrompts(count: Int): Flow> + + // Update the timestamp of an existing prompt to make it the most recent + @Query("UPDATE system_prompts SET timestamp = :timestamp WHERE id = :id") + suspend fun updatePromptTimestamp(id: String, timestamp: Long) +} diff --git a/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/local/SystemPromptEntity.kt b/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/local/SystemPromptEntity.kt new file mode 100644 index 0000000000..2ad77818d7 --- /dev/null +++ b/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/local/SystemPromptEntity.kt @@ -0,0 +1,62 @@ +package com.example.llama.revamp.data.local + +import androidx.room.Entity +import androidx.room.PrimaryKey +import com.example.llama.revamp.data.model.SystemPrompt + +/** + * Database entity for storing system prompts. + */ +@Entity(tableName = "system_prompts") +data class SystemPromptEntity( + @PrimaryKey + val id: String, + val content: String, + val name: String?, + val timestamp: Long, + val isPreset: Boolean +) { + /** + * Convert to domain model. + */ + fun toDomainModel(): SystemPrompt { + return if (isPreset) { + SystemPrompt.Preset( + id = id, + content = content, + name = name ?: "Unnamed Preset", + timestamp = timestamp + ) + } else { + SystemPrompt.Custom( + id = id, + content = content, + timestamp = timestamp + ) + } + } + + companion object { + /** + * Create an entity from a domain model. + */ + fun fromDomainModel(prompt: SystemPrompt): SystemPromptEntity { + return when (prompt) { + is SystemPrompt.Preset -> SystemPromptEntity( + id = prompt.id, + content = prompt.content, + name = prompt.name, + timestamp = prompt.timestamp ?: System.currentTimeMillis(), + isPreset = true + ) + is SystemPrompt.Custom -> SystemPromptEntity( + id = prompt.id, + content = prompt.content, + name = null, + timestamp = prompt.timestamp, + isPreset = false + ) + } + } + } +}