knowledge type select instead of input

This commit is contained in:
Per Stark
2025-02-18 14:47:33 +01:00
parent ab52616c8b
commit e5dd88fd1c
4 changed files with 37 additions and 17 deletions

File diff suppressed because one or more lines are too long

View File

@@ -20,7 +20,8 @@ use crate::{
storage::{ storage::{
db::delete_item, db::delete_item,
types::{ types::{
knowledge_entity::KnowledgeEntity, knowledge_relationship::KnowledgeRelationship, knowledge_entity::{KnowledgeEntity, KnowledgeEntityType},
knowledge_relationship::KnowledgeRelationship,
user::User, user::User,
}, },
}, },
@@ -146,6 +147,7 @@ pub async fn show_knowledge_page(
#[derive(Serialize)] #[derive(Serialize)]
pub struct EntityData { pub struct EntityData {
entity: KnowledgeEntity, entity: KnowledgeEntity,
entity_types: Vec<String>,
user: User, user: User,
} }
@@ -160,6 +162,12 @@ pub async fn show_edit_knowledge_entity_form(
None => return Ok(Redirect::to("/signin").into_response()), None => return Ok(Redirect::to("/signin").into_response()),
}; };
// Get entity types
let entity_types: Vec<String> = KnowledgeEntityType::variants()
.iter()
.map(|s| s.to_string())
.collect();
// Get the entity and validate ownership // Get the entity and validate ownership
let entity = User::get_and_validate_knowledge_entity(&id, &user.id, &state.surreal_db_client) let entity = User::get_and_validate_knowledge_entity(&id, &user.id, &state.surreal_db_client)
.await .await
@@ -167,7 +175,11 @@ pub async fn show_edit_knowledge_entity_form(
let output = render_template( let output = render_template(
"knowledge/edit_knowledge_entity_modal.html", "knowledge/edit_knowledge_entity_modal.html",
EntityData { entity, user }, EntityData {
entity,
user,
entity_types,
},
state.templates, state.templates,
)?; )?;
@@ -184,6 +196,7 @@ pub struct EntityListData {
pub struct PatchKnowledgeEntityParams { pub struct PatchKnowledgeEntityParams {
pub id: String, pub id: String,
pub name: String, pub name: String,
pub entity_type: String,
pub description: String, pub description: String,
} }
@@ -199,17 +212,18 @@ pub async fn patch_knowledge_entity(
}; };
// Get the existing entity and validate that the user is allowed // Get the existing entity and validate that the user is allowed
let existing_entity = User::get_and_validate_knowledge_entity(&form.id, &user.id, &state.surreal_db_client)
User::get_and_validate_knowledge_entity(&form.id, &user.id, &state.surreal_db_client) .await
.await .map_err(|e| HtmlError::new(e, state.templates.clone()))?;
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let entity_type: KnowledgeEntityType = KnowledgeEntityType::from(form.entity_type);
// Update the entity // Update the entity
KnowledgeEntity::patch( KnowledgeEntity::patch(
&form.id, &form.id,
&form.name, &form.name,
&form.description, &form.description,
&existing_entity.entity_type, &entity_type,
&state.surreal_db_client, &state.surreal_db_client,
&state.openai_client, &state.openai_client,
) )

View File

@@ -2,10 +2,7 @@ use crate::{
error::AppError, storage::db::SurrealDbClient, stored_object, error::AppError, storage::db::SurrealDbClient, stored_object,
utils::embedding::generate_embedding, utils::embedding::generate_embedding,
}; };
use async_openai::{ use async_openai::{config::OpenAIConfig, Client};
config::{Config, OpenAIConfig},
Client,
};
use uuid::Uuid; use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@@ -17,6 +14,11 @@ pub enum KnowledgeEntityType {
TextSnippet, TextSnippet,
// Add more types as needed // Add more types as needed
} }
impl KnowledgeEntityType {
pub fn variants() -> &'static [&'static str] {
&["Idea", "Project", "Document", "Page", "TextSnippet"]
}
}
impl From<String> for KnowledgeEntityType { impl From<String> for KnowledgeEntityType {
fn from(s: String) -> Self { fn from(s: String) -> Self {
@@ -101,6 +103,7 @@ impl KnowledgeEntity {
SET name = $name, SET name = $name,
description = $description, description = $description,
updated_at = $updated_at, updated_at = $updated_at,
entity_type = $entity_type,
embedding = $embedding embedding = $embedding
RETURN AFTER", RETURN AFTER",
) )
@@ -108,6 +111,7 @@ impl KnowledgeEntity {
.bind(("id", id.to_string())) .bind(("id", id.to_string()))
.bind(("name", name.to_string())) .bind(("name", name.to_string()))
.bind(("updated_at", Utc::now())) .bind(("updated_at", Utc::now()))
.bind(("entity_type", entity_type.to_owned()))
.bind(("embedding", embedding)) .bind(("embedding", embedding))
.bind(("description", description.to_string())) .bind(("description", description.to_string()))
.await?; .await?;

View File

@@ -16,11 +16,13 @@ hx-swap="outerHTML"
</label> </label>
</div> </div>
<div class="form-control"> <div class="form-control relative" style="margin-top: -1.5rem;">
<label class="floating-label"> <span class="absolute left-2.5 top-2.5 z-[100] p-0.5 bg-white text-xs text-light">Type</span>
<span class="label-text">Type</span> <select name="entity_type" class="select select-bordered w-full">
<input type="text" name="name" value="{{ entity.entity_type}}" class="input input-bordered w-full"> {% for et in entity_types %}
</label> <option value="{{ et }}" {% if entity.entity_type==et %}selected{% endif %}>{{ et }}</option>
{% endfor %}
</select>
</div> </div>
<input type="text" name="id" value="{{ entity.id }}" class="hidden"> <input type="text" name="id" value="{{ entity.id }}" class="hidden">