Elffuss BraidHub

kikocis/continue public

3ff29c567dd0 · 4.7 KiBRawPermalink
1use std::path::Path;
2mod db;
3mod gitignore;
4mod sync;
5mod sync_db;
6mod utils;
7
8use neon::prelude::*;
9
10fn build_js_array<'a>(
11 rs_array: Vec<(String, String)>,
12 cx: &mut FunctionContext<'a>,
13) -> Handle<'a, JsArray> {
14 let js_array = JsArray::new(cx, rs_array.len() as u32);
15 for (i, (name, hash)) in rs_array.iter().enumerate() {
16 let js_object = JsObject::new(cx);
17
18 let name = JsString::new(cx, &name);
19 let _ = js_object.set(cx, "name", name);
20 let hash = JsString::new(cx, &hash);
21 let _ = js_object.set(cx, "hash", hash);
22
23 let _ = js_array.set(cx, i as u32, js_object);
24 }
25
26 return js_array;
27}
28
29fn sync_results(mut cx: FunctionContext) -> JsResult<JsArray> {
30 let dir = cx.argument::<JsString>(0)?.value(&mut cx);
31 let branch = cx.argument::<JsString>(1)?.value(&mut cx);
32 let provider_id = cx.argument::<JsString>(2)?.value(&mut cx);
33
34 let tag = sync::Tag {
35 dir: Path::new(&dir),
36 branch: &branch,
37 provider_id: &provider_id.to_string(),
38 };
39
40 let compute = sync_db::sync_db(&tag);
41 let compute_js_array = build_js_array(compute, &mut cx);
42
43 return Ok(compute_js_array);
44}
45
46fn db_add_chunk(mut cx: FunctionContext) -> JsResult<JsUndefined> {
47 let chunk_obj = cx.argument::<JsObject>(0)?;
48
49 let hash = chunk_obj
50 .get::<JsString, _, _>(&mut cx, "digest")?
51 .value(&mut cx);
52
53 let content = chunk_obj
54 .get::<JsString, _, _>(&mut cx, "content")?
55 .value(&mut cx);
56
57 let start_line = chunk_obj
58 .get::<JsNumber, _, _>(&mut cx, "startLine")?
59 .value(&mut cx) as usize;
60
61 let end_line = chunk_obj
62 .get::<JsNumber, _, _>(&mut cx, "endLine")?
63 .value(&mut cx) as usize;
64
65 let file_path = chunk_obj
66 .get::<JsString, _, _>(&mut cx, "filepath")?
67 .value(&mut cx);
68
69 let index = chunk_obj
70 .get::<JsNumber, _, _>(&mut cx, "index")?
71 .value(&mut cx) as usize;
72
73 let tags_vec = cx.argument::<JsArray>(1)?.to_vec(&mut cx).unwrap();
74 let mut tags: Vec<String> = Vec::new();
75 for item in tags_vec {
76 let tag = item
77 .downcast::<JsString, _>(&mut cx)
78 .unwrap()
79 .value(&mut cx);
80 tags.push(tag);
81 }
82
83 let embedding_vec = cx.argument::<JsArray>(2)?.to_vec(&mut cx).unwrap();
84 let mut embedding: Vec<f32> = Vec::new();
85 for item in embedding_vec {
86 let float = item
87 .downcast::<JsNumber, _>(&mut cx)
88 .unwrap()
89 .value(&mut cx) as f32;
90 embedding.push(float);
91 }
92
93 let chunk = db::Chunk {
94 hash,
95 content,
96 start_line,
97 end_line,
98 file_path,
99 index,
100 embedding,
101 };
102
103 db::add_chunk(chunk, tags);
104
105 return Ok(JsUndefined::new(&mut cx));
106}
107
108fn db_retrieve(mut cx: FunctionContext) -> JsResult<JsArray> {
109 let n = cx.argument::<JsNumber>(0)?.value(&mut cx) as usize;
110
111 let tags_vec = cx.argument::<JsArray>(1)?.to_vec(&mut cx).unwrap();
112 let mut tags: Vec<String> = Vec::new();
113 for item in tags_vec {
114 let tag = item
115 .downcast::<JsString, _>(&mut cx)
116 .unwrap()
117 .value(&mut cx);
118 tags.push(tag);
119 }
120
121 let v_vec = cx.argument::<JsArray>(2)?.to_vec(&mut cx).unwrap();
122 let mut v: Vec<f32> = Vec::new();
123 for item in v_vec {
124 let float = item
125 .downcast::<JsNumber, _>(&mut cx)
126 .unwrap()
127 .value(&mut cx) as f32;
128 v.push(float);
129 }
130
131 let results = db::retrieve(n, tags, v);
132
133 let js_array = JsArray::new(&mut cx, results.len() as u32);
134 for (i, chunk) in results.iter().enumerate() {
135 let js_object = JsObject::new(&mut cx);
136
137 let hash = JsString::new(&mut cx, &chunk.hash);
138 let _ = js_object.set(&mut cx, "hash", hash);
139
140 let content = JsString::new(&mut cx, &chunk.content);
141 let _ = js_object.set(&mut cx, "content", content);
142
143 let start_line = JsNumber::new(&mut cx, chunk.start_line as f64);
144 let _ = js_object.set(&mut cx, "startLine", start_line);
145
146 let end_line = JsNumber::new(&mut cx, chunk.end_line as f64);
147 let _ = js_object.set(&mut cx, "endLine", end_line);
148
149 let file_path = JsString::new(&mut cx, &chunk.file_path);
150 let _ = js_object.set(&mut cx, "filepath", file_path);
151
152 let index = JsNumber::new(&mut cx, chunk.index as f64);
153 let _ = js_object.set(&mut cx, "index", index);
154
155 let _ = js_array.set(&mut cx, i as u32, js_object);
156 }
157
158 return Ok(js_array);
159}
160
161#[neon::main]
162fn main(mut cx: ModuleContext) -> NeonResult<()> {
163 let _ = cx.export_function("sync_results", sync_results)?;
164 let _ = cx.export_function("add_chunk", db_add_chunk);
165 let _ = cx.export_function("retrieve", db_retrieve);
166 Ok(())
167}