Files
chat-api/src/mappers/database_to_core.rs
T

70 lines
2.3 KiB
Rust

use crate::core;
use crate::databases::postgres;
impl From<postgres::chat::types::ConversationSummary>
for core::databases::conversations::ConversationSummary
{
fn from(c: postgres::chat::types::ConversationSummary) -> Self {
Self {
id: c.id,
title: c.title.unwrap_or("No title Generated".to_string()),
created_at: c.created_at,
updated_at: c.updated_at,
}
}
}
impl From<postgres::chat::types::MessageSummary>
for core::databases::conversations::MessageSummary
{
fn from(m: postgres::chat::types::MessageSummary) -> Self {
Self {
id: m.id,
parent_id: m.parent_id,
role: match m.role {
postgres::chat::types::MessageRole::User => core::llm::ChatRole::User,
postgres::chat::types::MessageRole::Assistant => core::llm::ChatRole::Assistant,
postgres::chat::types::MessageRole::System => core::llm::ChatRole::System,
},
content: m.content,
created_at: m.created_at,
tokens: m.tokens,
}
}
}
impl From<postgres::api_key::types::Role> for core::auth::api_key::KeyRole {
fn from(role: postgres::api_key::types::Role) -> Self {
match role {
postgres::api_key::types::Role::User => core::auth::api_key::KeyRole::User,
postgres::api_key::types::Role::Admin => core::auth::api_key::KeyRole::Admin,
}
}
}
impl From<postgres::api_key::types::AuthContext> for core::auth::api_key::AuthContext {
fn from(m: postgres::api_key::types::AuthContext) -> Self {
Self {
user_id: m.user_id,
_api_key_id: m.api_key_id,
roles: m.roles.into_iter().map(Into::into).collect(),
}
}
}
impl From<postgres::chat::types::ConversationState>
for core::databases::conversations::ConversationResult
{
fn from(m: postgres::chat::types::ConversationState) -> Self {
match m {
postgres::chat::types::ConversationState::Existing(id) => {
core::databases::conversations::ConversationResult::Existing(id)
}
postgres::chat::types::ConversationState::Created(id) => {
core::databases::conversations::ConversationResult::Created(id)
}
}
}
}