Skip to content

Commit

Permalink
use generic configuration for profile
Browse files Browse the repository at this point in the history
  • Loading branch information
aprxi committed Sep 15, 2024
1 parent 8a607cf commit 6adfbed
Show file tree
Hide file tree
Showing 15 changed files with 359 additions and 194 deletions.
10 changes: 5 additions & 5 deletions lumni/src/apps/builtin/llm/prompt/src/chat/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ mod conversations;
mod display;
mod encryption;
mod model;
mod settings;
mod store;
mod user_profile;

pub use conversations::ConversationDbHandler;
pub use encryption::EncryptionHandler;
pub use lumni::Timestamp;
pub use model::{ModelIdentifier, ModelSpec};
use serde::{Deserialize, Serialize};
pub use store::ConversationDatabase;
pub use user_profile::{
MaskMode, ProviderConfig, ProviderConfigOptions, UserProfile,
UserProfileDbHandler,
pub use settings::{
DatabaseConfigurationItem, MaskMode, ProviderConfig, ProviderConfigOptions,
UserProfile, UserProfileDbHandler,
};
pub use store::ConversationDatabase;

pub use super::ConversationCache;
use super::{ModelBackend, ModelServer, PromptRole};
Expand Down
6 changes: 3 additions & 3 deletions lumni/src/apps/builtin/llm/prompt/src/chat/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ CREATE TABLE metadata (
value TEXT NOT NULL
);

CREATE TABLE user_profiles (
CREATE TABLE configuration (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
options TEXT NOT NULL, -- JSON string
section TEXT NOT NULL CHECK(section IN ('profile', 'provider', 'prompt')),
parameters TEXT NOT NULL, -- JSON string
is_default INTEGER DEFAULT 0,
encryption_key_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Expand Down Expand Up @@ -121,6 +122,5 @@ CREATE INDEX idx_message_conversation_created ON messages(conversation_id, creat
CREATE INDEX idx_message_previous ON messages(previous_message_id);
CREATE INDEX idx_attachment_conversation ON attachments(conversation_id);
CREATE INDEX idx_conversation_pinned_updated ON conversations(is_pinned DESC, updated_at DESC);
CREATE UNIQUE INDEX idx_user_profiles_default ON user_profiles(is_default) WHERE is_default = 1;
CREATE INDEX idx_conversations_workspace_id ON conversations(workspace_id);
CREATE INDEX idx_conversations_no_workspace ON conversations(workspace_id) WHERE workspace_id IS NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{
use crate::external as lumni;

impl UserProfileDbHandler {
pub fn process_settings(
pub fn process_parameters(
&self,
value: &JsonValue,
encryption_mode: EncryptionMode,
Expand All @@ -28,7 +28,7 @@ impl UserProfileDbHandler {
let new_arr: Result<Vec<JsonValue>, _> = arr
.iter()
.map(|v| {
self.process_settings(v, encryption_mode, mask_mode)
self.process_parameters(v, encryption_mode, mask_mode)
})
.collect();
Ok(JsonValue::Array(new_arr?))
Expand All @@ -37,7 +37,7 @@ impl UserProfileDbHandler {
}
}

pub fn process_settings_with_metadata(
pub fn process_parameters_with_metadata(
&self,
value: &JsonValue,
encryption_mode: EncryptionMode,
Expand All @@ -60,7 +60,7 @@ impl UserProfileDbHandler {
let new_arr: Result<Vec<JsonValue>, _> = arr
.iter()
.map(|v| {
self.process_settings_with_metadata(
self.process_parameters_with_metadata(
v,
encryption_mode,
mask_mode,
Expand All @@ -73,7 +73,7 @@ impl UserProfileDbHandler {
}
}

pub fn merge_settings(
pub fn merge_parameters(
&self,
current_data: &JsonValue,
new_settings: &JsonValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ impl UserProfileDbHandler {
.to_string(),
)
})?;

// Check if the profile exists in the database and compare encryption handlers
let db = self.db.clone();
let result = tokio::task::block_in_place(|| {
Expand All @@ -180,22 +179,21 @@ impl UserProfileDbHandler {
.query_row(
"SELECT encryption_keys.file_path, \
encryption_keys.sha256_hash
FROM user_profiles
FROM configuration
JOIN encryption_keys ON \
user_profiles.encryption_key_id = \
configuration.encryption_key_id = \
encryption_keys.id
WHERE user_profiles.name = ?",
WHERE configuration.name = ? AND \
configuration.section = 'profile'",
params![profile.name],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.optional()
.map_err(DatabaseOperationError::SqliteError)?;

if let Some((_, existing_hash)) = existing_key {
let new_path = encryption_handler.get_key_path();
let new_hash =
EncryptionHandler::get_private_key_hash(&new_path)?;

if existing_hash != new_hash {
return Err(
DatabaseOperationError::ApplicationError(
Expand All @@ -213,14 +211,12 @@ impl UserProfileDbHandler {
})
})
});

result.map_err(|e| match e {
DatabaseOperationError::SqliteError(sqlite_err) => {
ApplicationError::DatabaseError(sqlite_err.to_string())
}
DatabaseOperationError::ApplicationError(app_err) => app_err,
})?;

// If we've made it this far, either the profile doesn't exist yet or the encryption handler matches
self.encryption_handler = Some(encryption_handler);
Ok(())
Expand Down Expand Up @@ -381,10 +377,11 @@ impl UserProfileDbHandler {
db.process_queue_with_result(|tx| {
tx.query_row(
"SELECT encryption_keys.file_path, encryption_keys.sha256_hash
FROM user_profiles
JOIN encryption_keys ON user_profiles.encryption_key_id = \
FROM configuration
JOIN encryption_keys ON configuration.encryption_key_id = \
encryption_keys.id
WHERE user_profiles.id = ?",
WHERE configuration.id = ? AND configuration.section = \
'profile'",
params![self.profile.as_ref().map(|p| p.id).unwrap_or(0)],
|row| Ok((row.get(0)?, row.get(1)?)),
)
Expand Down
Loading

0 comments on commit 6adfbed

Please sign in to comment.