|
| 1 | + |
| 2 | +// DEFAULT CODE |
| 3 | +// use helix_db::helix_engine::traversal_core::config::Config; |
| 4 | + |
| 5 | +// pub fn config() -> Option<Config> { |
| 6 | +// None |
| 7 | +// } |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +use bumpalo::Bump; |
| 12 | +use heed3::RoTxn; |
| 13 | +use helix_macros::{handler, tool_call, mcp_handler, migration}; |
| 14 | +use helix_db::{ |
| 15 | + helix_engine::{ |
| 16 | + traversal_core::{ |
| 17 | + config::{Config, GraphConfig, VectorConfig}, |
| 18 | + ops::{ |
| 19 | + bm25::search_bm25::SearchBM25Adapter, |
| 20 | + g::G, |
| 21 | + in_::{in_::InAdapter, in_e::InEdgesAdapter, to_n::ToNAdapter, to_v::ToVAdapter}, |
| 22 | + out::{ |
| 23 | + from_n::FromNAdapter, from_v::FromVAdapter, out::OutAdapter, out_e::OutEdgesAdapter, |
| 24 | + }, |
| 25 | + source::{ |
| 26 | + add_e::AddEAdapter, |
| 27 | + add_n::AddNAdapter, |
| 28 | + e_from_id::EFromIdAdapter, |
| 29 | + e_from_type::EFromTypeAdapter, |
| 30 | + n_from_id::NFromIdAdapter, |
| 31 | + n_from_index::NFromIndexAdapter, |
| 32 | + n_from_type::NFromTypeAdapter, |
| 33 | + v_from_id::VFromIdAdapter, |
| 34 | + v_from_type::VFromTypeAdapter |
| 35 | + }, |
| 36 | + util::{ |
| 37 | + dedup::DedupAdapter, drop::Drop, exist::Exist, filter_mut::FilterMut, |
| 38 | + filter_ref::FilterRefAdapter, map::MapAdapter, paths::{PathAlgorithm, ShortestPathAdapter}, |
| 39 | + range::RangeAdapter, update::UpdateAdapter, order::OrderByAdapter, |
| 40 | + aggregate::AggregateAdapter, group_by::GroupByAdapter, count::CountAdapter, |
| 41 | + }, |
| 42 | + vectors::{ |
| 43 | + brute_force_search::BruteForceSearchVAdapter, insert::InsertVAdapter, |
| 44 | + search::SearchVAdapter, |
| 45 | + }, |
| 46 | + }, |
| 47 | + traversal_value::TraversalValue, |
| 48 | + }, |
| 49 | + types::GraphError, |
| 50 | + vector_core::vector::HVector, |
| 51 | + }, |
| 52 | + helix_gateway::{ |
| 53 | + embedding_providers::{EmbeddingModel, get_embedding_model}, |
| 54 | + router::router::{HandlerInput, IoContFn}, |
| 55 | + mcp::mcp::{MCPHandlerSubmission, MCPToolInput, MCPHandler} |
| 56 | + }, |
| 57 | + node_matches, props, embed, embed_async, |
| 58 | + field_addition_from_old_field, field_type_cast, field_addition_from_value, |
| 59 | + protocol::{ |
| 60 | + response::Response, |
| 61 | + value::{casting::{cast, CastType}, Value}, |
| 62 | + format::Format, |
| 63 | + }, |
| 64 | + utils::{ |
| 65 | + count::Count, |
| 66 | + id::{ID, uuid_str}, |
| 67 | + items::{Edge, Node}, |
| 68 | + properties::ImmutablePropertiesMap, |
| 69 | + }, |
| 70 | +}; |
| 71 | +use sonic_rs::{Deserialize, Serialize, json}; |
| 72 | +use std::collections::{HashMap, HashSet}; |
| 73 | +use std::sync::Arc; |
| 74 | +use std::time::Instant; |
| 75 | +use chrono::{DateTime, Utc}; |
| 76 | + |
| 77 | +// Re-export scalar types for generated code |
| 78 | +type I8 = i8; |
| 79 | +type I16 = i16; |
| 80 | +type I32 = i32; |
| 81 | +type I64 = i64; |
| 82 | +type U8 = u8; |
| 83 | +type U16 = u16; |
| 84 | +type U32 = u32; |
| 85 | +type U64 = u64; |
| 86 | +type U128 = u128; |
| 87 | +type F32 = f32; |
| 88 | +type F64 = f64; |
| 89 | + |
| 90 | +pub fn config() -> Option<Config> { |
| 91 | +return Some(Config { |
| 92 | +vector_config: Some(VectorConfig { |
| 93 | +m: Some(16), |
| 94 | +ef_construction: Some(128), |
| 95 | +ef_search: Some(768), |
| 96 | +}), |
| 97 | +graph_config: Some(GraphConfig { |
| 98 | +secondary_indices: Some(vec![]), |
| 99 | +}), |
| 100 | +db_max_size_gb: Some(10), |
| 101 | +mcp: Some(true), |
| 102 | +bm25: Some(true), |
| 103 | +schema: Some(r#"{ |
| 104 | + "schema": { |
| 105 | + "nodes": [ |
| 106 | + { |
| 107 | + "name": "Person", |
| 108 | + "properties": { |
| 109 | + "id": "ID", |
| 110 | + "label": "String", |
| 111 | + "name": "String" |
| 112 | + } |
| 113 | + } |
| 114 | + ], |
| 115 | + "vectors": [], |
| 116 | + "edges": [ |
| 117 | + { |
| 118 | + "name": "Knows", |
| 119 | + "from": "Person", |
| 120 | + "to": "Person", |
| 121 | + "properties": { |
| 122 | + "since": "String" |
| 123 | + } |
| 124 | + } |
| 125 | + ] |
| 126 | + }, |
| 127 | + "queries": [ |
| 128 | + { |
| 129 | + "name": "AddEdges", |
| 130 | + "parameters": { |
| 131 | + "edges": "Array({to_id: IDsince: Stringfrom_id: ID})" |
| 132 | + }, |
| 133 | + "returns": [] |
| 134 | + } |
| 135 | + ] |
| 136 | +}"#.to_string()), |
| 137 | +embedding_model: Some("text-embedding-ada-002".to_string()), |
| 138 | +graphvis_node_label: None, |
| 139 | +}) |
| 140 | +} |
| 141 | + |
| 142 | +pub struct Person { |
| 143 | + pub name: String, |
| 144 | +} |
| 145 | + |
| 146 | +pub struct Knows { |
| 147 | + pub from: Person, |
| 148 | + pub to: Person, |
| 149 | + pub since: String, |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +#[derive(Serialize, Deserialize, Clone)] |
| 154 | +pub struct AddEdgesInput { |
| 155 | + |
| 156 | +pub edges: Vec<edgesData> |
| 157 | +} |
| 158 | +#[derive(Serialize, Deserialize, Clone)] |
| 159 | +pub struct edgesData { |
| 160 | + pub to_id: ID, |
| 161 | + pub since: String, |
| 162 | + pub from_id: ID, |
| 163 | +} |
| 164 | +#[handler] |
| 165 | +pub fn AddEdges (input: HandlerInput) -> Result<Response, GraphError> { |
| 166 | +let db = Arc::clone(&input.graph.storage); |
| 167 | +let data = input.request.in_fmt.deserialize::<AddEdgesInput>(&input.request.body)?; |
| 168 | +let arena = Bump::new(); |
| 169 | +let mut txn = db.graph_env.write_txn().map_err(|e| GraphError::New(format!("Failed to start write transaction: {:?}", e)))?; |
| 170 | + for edgesData { from_id, to_id, since } in &data.edges { |
| 171 | + G::new_mut(&db, &arena, &mut txn) |
| 172 | +.add_edge("Knows", Some(ImmutablePropertiesMap::new(1, vec![("since", Value::from(since.clone()))].into_iter(), &arena)), from_id.id(), to_id.id(), false).collect_to_obj(); |
| 173 | +} |
| 174 | +; |
| 175 | +let response = json!({ |
| 176 | + "data": "Edges added successfully" |
| 177 | +}); |
| 178 | +txn.commit().map_err(|e| GraphError::New(format!("Failed to commit transaction: {:?}", e)))?; |
| 179 | +Ok(input.request.out_fmt.create_response(&response)) |
| 180 | +} |
| 181 | + |
| 182 | + |
0 commit comments