@1kbirds/chidori 0.1.26 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +138 -6
- package/dist/agent-env.d.ts +38 -0
- package/dist/agent-env.js +1 -0
- package/dist/agent.d.ts +487 -0
- package/dist/agent.js +46 -0
- package/dist/index.d.ts +332 -0
- package/dist/index.js +318 -0
- package/package.json +34 -33
- package/src/agent-env.ts +43 -0
- package/src/agent.ts +548 -0
- package/src/index.ts +580 -0
- package/CHANGELOG.md +0 -33
- package/Cargo.toml +0 -83
- package/build.rs +0 -7
- package/package_node/index.d.ts +0 -0
- package/package_node/index.js +0 -130
- package/pyproject.toml +0 -35
- package/src/lib.rs +0 -23
- package/src/translations/mod.rs +0 -9
- package/src/translations/nodejs.rs +0 -739
- package/src/translations/python.rs +0 -921
- package/src/translations/rust.rs +0 -737
- package/src/translations/shared.rs +0 -49
- package/src/translations/wasm.rs +0 -1
- package/tests/nodejs/chidori.test.js +0 -41
- package/tests/nodejs/nodeHandle.test.js +0 -15
- package/tests/python/test_chidori.py +0 -30
|
@@ -1,739 +0,0 @@
|
|
|
1
|
-
use std::cell::RefCell;
|
|
2
|
-
use std::collections::{HashMap, VecDeque};
|
|
3
|
-
use std::future::Future;
|
|
4
|
-
use std::marker::PhantomData;
|
|
5
|
-
use std::sync::{Arc};
|
|
6
|
-
use tokio::sync::{mpsc, Mutex};
|
|
7
|
-
use anyhow::Error;
|
|
8
|
-
use futures::StreamExt;
|
|
9
|
-
use log::{debug, info};
|
|
10
|
-
use neon::{prelude::*, types::Deferred};
|
|
11
|
-
use neon::handle::Managed;
|
|
12
|
-
use neon::result::Throw;
|
|
13
|
-
use once_cell::sync::OnceCell;
|
|
14
|
-
use tokio::runtime::Runtime;
|
|
15
|
-
use prompt_graph_core::build_runtime_graph::graph_parse::{CleanedDefinitionGraph, CleanIndividualNode, construct_query_from_output_type, derive_for_individual_node};
|
|
16
|
-
use prompt_graph_core::graph_definition::{create_code_node, create_custom_node, create_prompt_node, create_vector_memory_node, SourceNodeType};
|
|
17
|
-
use prompt_graph_core::proto2::{ChangeValue, ChangeValueWithCounter, Empty, ExecutionStatus, File, FileAddressedChangeValueWithCounter, FilteredPollNodeWillExecuteEventsRequest, Item, ListBranchesRes, Path, Query, QueryAtFrame, QueryAtFrameResponse, RequestAckNodeWillExecuteEvent, RequestAtFrame, RequestFileMerge, RequestListBranches, RequestNewBranch, RequestOnlyId, SerializedValue, SerializedValueArray, SerializedValueObject};
|
|
18
|
-
use prompt_graph_core::proto2::execution_runtime_client::ExecutionRuntimeClient;
|
|
19
|
-
use prompt_graph_core::proto2::serialized_value::Val;
|
|
20
|
-
use prompt_graph_exec::tonic_runtime::run_server;
|
|
21
|
-
use neon_serde3;
|
|
22
|
-
use prost::bytes::Buf;
|
|
23
|
-
use serde::{Deserialize, Serialize};
|
|
24
|
-
use crate::translations::rust::{Chidori, CustomNodeCreateOpts, DenoCodeNodeCreateOpts, GraphBuilder, Handler, NodeHandle, PromptNodeCreateOpts, VectorMemoryNodeCreateOpts};
|
|
25
|
-
|
|
26
|
-
// Return a global tokio runtime or create one if it doesn't exist.
|
|
27
|
-
// Throws a JavaScript exception if the `Runtime` fails to create.
|
|
28
|
-
// TODO: note that oncecell has been recently stablized in rust stdlib, so we can probably use that instead
|
|
29
|
-
fn runtime<'a, C: Context<'a>>(cx: &mut C) -> NeonResult<&'static Runtime> {
|
|
30
|
-
static RUNTIME: OnceCell<Runtime> = OnceCell::new();
|
|
31
|
-
RUNTIME.get_or_try_init(|| Runtime::new().or_else(|err| cx.throw_error(err.to_string())))
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
async fn get_client(url: String) -> Result<ExecutionRuntimeClient<tonic::transport::Channel>, tonic::transport::Error> {
|
|
36
|
-
ExecutionRuntimeClient::connect(url.clone()).await
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
#[derive(Debug)]
|
|
41
|
-
pub struct SerializedValueWrapper(SerializedValue);
|
|
42
|
-
|
|
43
|
-
impl SerializedValueWrapper {
|
|
44
|
-
fn to_object<'a, T>(&self, cx: &mut T) -> JsResult<'a, JsValue> where
|
|
45
|
-
T: Context<'a> {
|
|
46
|
-
if let None = self.0.val {
|
|
47
|
-
let x: Option<bool> = None;
|
|
48
|
-
return Ok(cx.undefined().upcast());
|
|
49
|
-
}
|
|
50
|
-
let result: Handle<JsValue> = match self.0.val.as_ref().unwrap() {
|
|
51
|
-
Val::Float(x) => { cx.number(*x as f64).upcast() }
|
|
52
|
-
Val::Number(x) => { cx.number(*x as f64).upcast() }
|
|
53
|
-
Val::String(x) => { cx.string(x).upcast() }
|
|
54
|
-
Val::Boolean(x) => { cx.boolean(*x).upcast() }
|
|
55
|
-
Val::Array(val) => {
|
|
56
|
-
let mut js_list = cx.empty_array();
|
|
57
|
-
for (idx, item) in val.values.iter().enumerate() {
|
|
58
|
-
let js = SerializedValueWrapper(item.clone()).to_object(cx);
|
|
59
|
-
js_list.set(cx, idx as u32, js?)?;
|
|
60
|
-
}
|
|
61
|
-
js_list.upcast()
|
|
62
|
-
}
|
|
63
|
-
Val::Object(val) => {
|
|
64
|
-
let mut js_obj = cx.empty_object();
|
|
65
|
-
for (key, value) in &val.values {
|
|
66
|
-
let js = SerializedValueWrapper(value.clone()).to_object(cx);
|
|
67
|
-
js_obj.set(cx, key.as_str(), js?).unwrap();
|
|
68
|
-
}
|
|
69
|
-
js_obj.upcast()
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
Ok(result)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
fn from_js_value<'a, C: Context<'a>>(cx: &mut C, value: Handle<JsValue>) -> NeonResult<SerializedValue> {
|
|
78
|
-
if value.is_a::<JsUndefined, _>(cx) {
|
|
79
|
-
return Ok(SerializedValue { val: None });
|
|
80
|
-
} else if let Ok(num) = value.downcast::<JsNumber, _>(cx) {
|
|
81
|
-
return Ok(SerializedValue { val: Some(Val::Float(num.value(cx) as f32))});
|
|
82
|
-
} else if let Ok(bool) = value.downcast::<JsBoolean, _>(cx) {
|
|
83
|
-
return Ok(SerializedValue { val: Some(Val::Boolean(bool.value(cx)))});
|
|
84
|
-
} else if let Ok(str) = value.downcast::<JsString, _>(cx) {
|
|
85
|
-
return Ok(SerializedValue { val: Some(Val::String(str.value(cx)))});
|
|
86
|
-
} else if let Ok(arr) = value.downcast::<JsArray, _>(cx) {
|
|
87
|
-
let mut vals = Vec::new();
|
|
88
|
-
for i in 0..arr.len(cx) {
|
|
89
|
-
let v = arr.get(cx, i)?;
|
|
90
|
-
vals.push(from_js_value(cx, v)?);
|
|
91
|
-
}
|
|
92
|
-
return Ok(SerializedValue { val: Some(Val::Array(SerializedValueArray { values: vals }))});
|
|
93
|
-
} else if let Ok(obj) = value.downcast::<JsObject, _>(cx) {
|
|
94
|
-
let mut vals = HashMap::new();
|
|
95
|
-
for key in obj.get_own_property_names(cx)?.to_vec(cx)? {
|
|
96
|
-
let v = obj.get(cx, key)?;
|
|
97
|
-
let k = key.downcast::<JsString, _>(cx);
|
|
98
|
-
vals.insert(k.unwrap().value(cx), from_js_value(cx, v)?);
|
|
99
|
-
}
|
|
100
|
-
return Ok(SerializedValue { val: Some(Val::Object(SerializedValueObject { values: vals }))});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
cx.throw_error("Unsupported type")
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
macro_rules! return_or_throw_deferred {
|
|
107
|
-
($channel:expr, $deferred:expr, $m:expr) => {
|
|
108
|
-
if let Ok(result) = $m {
|
|
109
|
-
$deferred.settle_with($channel, move |mut cx| {
|
|
110
|
-
neon_serde3::to_value(&mut cx, &result)
|
|
111
|
-
.or_else(|e| cx.throw_error(e.to_string()))
|
|
112
|
-
});
|
|
113
|
-
} else {
|
|
114
|
-
$deferred.settle_with($channel, move |mut cx| {
|
|
115
|
-
cx.throw_error("Error playing")
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
// Node handle
|
|
123
|
-
#[derive(Clone)]
|
|
124
|
-
pub struct NodeNodeHandle {
|
|
125
|
-
n: NodeHandle
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
impl NodeNodeHandle {
|
|
129
|
-
fn from(n: NodeHandle) -> NodeNodeHandle {
|
|
130
|
-
NodeNodeHandle{ n }
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
impl Finalize for NodeNodeHandle {}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
impl NodeNodeHandle {
|
|
138
|
-
fn get_name(&self) -> String {
|
|
139
|
-
self.n.get_name()
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
pub fn run_when(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
143
|
-
let channel = cx.channel();
|
|
144
|
-
let (deferred, promise) = cx.promise();
|
|
145
|
-
let self_ = cx.this()
|
|
146
|
-
.downcast_or_throw::<JsBox<RefCell<NodeNodeHandle>>, _>(&mut cx)?;
|
|
147
|
-
|
|
148
|
-
let graph_builder = cx.argument::<JsBox<NodeGraphBuilder>>(0)?;
|
|
149
|
-
let other_node_handle = cx.argument::<JsBox<RefCell<NodeNodeHandle>>>(1)?;
|
|
150
|
-
|
|
151
|
-
let mut n = &mut self_.borrow_mut().n;
|
|
152
|
-
let g = &graph_builder.g;
|
|
153
|
-
let mut graph_builder = g.blocking_lock();
|
|
154
|
-
let other_node = &other_node_handle.borrow().n;
|
|
155
|
-
let m = n.run_when(&mut graph_builder, &other_node);
|
|
156
|
-
deferred.settle_with((&channel), move |mut cx| {
|
|
157
|
-
if let Ok(result) = m {
|
|
158
|
-
Ok(cx.boolean(result))
|
|
159
|
-
} else {
|
|
160
|
-
cx.throw_error("Error playing")
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
Ok(promise)
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// pub fn query(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
168
|
-
//
|
|
169
|
-
// }
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
fn obj_to_paths<'a, C: Context<'a>>(cx: &mut C, d: Handle<JsObject>) -> NeonResult<Vec<(Vec<String>, SerializedValue)>> {
|
|
175
|
-
let mut paths = vec![];
|
|
176
|
-
let mut queue: VecDeque<(Vec<String>, Handle<JsObject>)> = VecDeque::new();
|
|
177
|
-
queue.push_back((Vec::new(), d));
|
|
178
|
-
|
|
179
|
-
while let Some((mut path, dict)) = queue.pop_front() {
|
|
180
|
-
let keys = dict.get_own_property_names(cx)?;
|
|
181
|
-
let len = keys.len(cx);
|
|
182
|
-
|
|
183
|
-
for i in 0..len {
|
|
184
|
-
let key = keys.get::<JsArray, _, u32>(cx, i).unwrap().downcast::<JsString, _>(cx).unwrap().value(cx);
|
|
185
|
-
path.push(key.clone());
|
|
186
|
-
|
|
187
|
-
let val: Handle<JsValue> = dict.get(cx, key.as_str())?;
|
|
188
|
-
if val.is_a::<JsObject, _>(cx) {
|
|
189
|
-
let sub_dict = val.downcast::<JsObject, _>(cx).unwrap();
|
|
190
|
-
queue.push_back((path.clone(), sub_dict));
|
|
191
|
-
} else {
|
|
192
|
-
let v = from_js_value(cx, val)?;
|
|
193
|
-
paths.push((path.clone(), v));
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
path.pop();
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
Ok(paths)
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
struct NodeChidori {
|
|
204
|
-
c: Arc<Mutex<Chidori>>
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
impl Finalize for NodeChidori {}
|
|
208
|
-
|
|
209
|
-
impl NodeChidori {
|
|
210
|
-
|
|
211
|
-
fn js_new(mut cx: FunctionContext) -> JsResult<JsBox<NodeChidori>> {
|
|
212
|
-
let file_id = cx.argument::<JsString>(0)?.value(&mut cx);
|
|
213
|
-
let url = cx.argument::<JsString>(1)?.value(&mut cx);
|
|
214
|
-
|
|
215
|
-
if !url.contains("://") {
|
|
216
|
-
return cx.throw_error("Invalid url, must include protocol");
|
|
217
|
-
}
|
|
218
|
-
// let api_token = cx.argument_opt(2)?.value(&mut cx);
|
|
219
|
-
debug!("Creating new Chidori instance with file_id={}, url={}, api_token={:?}", file_id, url, "".to_string());
|
|
220
|
-
Ok(cx.boxed(NodeChidori {
|
|
221
|
-
c: Arc::new(Mutex::new(Chidori::new(file_id, url))),
|
|
222
|
-
}))
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
fn start_server(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
226
|
-
let channel = cx.channel();
|
|
227
|
-
let self_ = cx.this()
|
|
228
|
-
.downcast_or_throw::<JsBox<NodeChidori>, _>(&mut cx)?;
|
|
229
|
-
let (deferred, promise) = cx.promise();
|
|
230
|
-
let file_path = cx.argument_opt(0).map(|x| x.downcast::<JsString, _>(&mut cx).unwrap().value(&mut cx));
|
|
231
|
-
let c = Arc::clone(&self_.c);
|
|
232
|
-
let rt = runtime(&mut cx)?;
|
|
233
|
-
rt.spawn(async move {
|
|
234
|
-
let mut c = c.lock().await;
|
|
235
|
-
let m = c.start_server(file_path).await;
|
|
236
|
-
deferred.settle_with((&channel), move |mut cx| {
|
|
237
|
-
if let Ok(_) = m {
|
|
238
|
-
Ok(cx.undefined())
|
|
239
|
-
} else {
|
|
240
|
-
cx.throw_error("Error playing")
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
});
|
|
244
|
-
Ok(promise)
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
fn play(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
248
|
-
let channel = cx.channel();
|
|
249
|
-
let (deferred, promise) = cx.promise();
|
|
250
|
-
let self_ = cx.this()
|
|
251
|
-
.downcast_or_throw::<JsBox<NodeChidori>, _>(&mut cx)?;
|
|
252
|
-
let branch = cx.argument::<JsNumber>(0).unwrap_or(JsNumber::new(&mut cx, 0.0)).value(&mut cx) as u64;
|
|
253
|
-
let frame = cx.argument::<JsNumber>(1).unwrap_or(JsNumber::new(&mut cx, 0.0)).value(&mut cx) as u64;
|
|
254
|
-
let c = Arc::clone(&self_.c);
|
|
255
|
-
let rt = runtime(&mut cx)?;
|
|
256
|
-
rt.spawn(async move {
|
|
257
|
-
let c = c.lock().await;
|
|
258
|
-
let m = c.play(branch, frame).await;
|
|
259
|
-
deferred.settle_with((&channel), move |mut cx| {
|
|
260
|
-
if let Ok(result) = m {
|
|
261
|
-
neon_serde3::to_value(&mut cx, &result)
|
|
262
|
-
.or_else(|e| cx.throw_error(e.to_string()))
|
|
263
|
-
} else {
|
|
264
|
-
cx.throw_error("Error playing")
|
|
265
|
-
}
|
|
266
|
-
});
|
|
267
|
-
});
|
|
268
|
-
Ok(promise)
|
|
269
|
-
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
fn pause(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
273
|
-
let channel = cx.channel();
|
|
274
|
-
let (deferred, promise) = cx.promise();
|
|
275
|
-
let self_ = cx.this()
|
|
276
|
-
.downcast_or_throw::<JsBox<NodeChidori>, _>(&mut cx)?;
|
|
277
|
-
let frame = cx.argument::<JsNumber>(0).unwrap_or(JsNumber::new(&mut cx, 0.0)).value(&mut cx) as u64;
|
|
278
|
-
let c = Arc::clone(&self_.c);
|
|
279
|
-
let rt = runtime(&mut cx)?;
|
|
280
|
-
rt.spawn(async move {
|
|
281
|
-
let c = c.lock().await;
|
|
282
|
-
let m = c.pause(frame).await;
|
|
283
|
-
deferred.settle_with((&channel), move |mut cx| {
|
|
284
|
-
if let Ok(result) = m {
|
|
285
|
-
neon_serde3::to_value(&mut cx, &result)
|
|
286
|
-
.or_else(|e| cx.throw_error(e.to_string()))
|
|
287
|
-
} else {
|
|
288
|
-
cx.throw_error("Error playing")
|
|
289
|
-
}
|
|
290
|
-
});
|
|
291
|
-
});
|
|
292
|
-
Ok(promise)
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
fn branch(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
296
|
-
let channel = cx.channel();
|
|
297
|
-
let (deferred, promise) = cx.promise();
|
|
298
|
-
let self_ = cx.this()
|
|
299
|
-
.downcast_or_throw::<JsBox<NodeChidori>, _>(&mut cx)?;
|
|
300
|
-
let branch = cx.argument::<JsNumber>(0).unwrap_or(JsNumber::new(&mut cx, 0.0)).value(&mut cx) as u64;
|
|
301
|
-
let frame = cx.argument::<JsNumber>(1).unwrap_or(JsNumber::new(&mut cx, 0.0)).value(&mut cx) as u64;
|
|
302
|
-
let c = Arc::clone(&self_.c);
|
|
303
|
-
let rt = runtime(&mut cx)?;
|
|
304
|
-
rt.spawn(async move {
|
|
305
|
-
let c = c.lock().await;
|
|
306
|
-
let m = c.branch(branch, frame).await;
|
|
307
|
-
deferred.settle_with((&channel), move |mut cx| {
|
|
308
|
-
if let Ok(result) = m {
|
|
309
|
-
neon_serde3::to_value(&mut cx, &result)
|
|
310
|
-
.or_else(|e| cx.throw_error(e.to_string()))
|
|
311
|
-
} else {
|
|
312
|
-
cx.throw_error("Error playing")
|
|
313
|
-
}
|
|
314
|
-
});
|
|
315
|
-
});
|
|
316
|
-
Ok(promise)
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
fn query(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
320
|
-
let channel = cx.channel();
|
|
321
|
-
let (deferred, promise) = cx.promise();
|
|
322
|
-
let self_ = cx.this()
|
|
323
|
-
.downcast_or_throw::<JsBox<NodeChidori>, _>(&mut cx)?;
|
|
324
|
-
let query = cx.argument::<JsString>(0).unwrap_or(JsString::new(&mut cx, "")).value(&mut cx);
|
|
325
|
-
let branch = cx.argument::<JsNumber>(1).unwrap_or(JsNumber::new(&mut cx, 0.0)).value(&mut cx) as u64;
|
|
326
|
-
let frame = cx.argument::<JsNumber>(2).unwrap_or(JsNumber::new(&mut cx, 0.0)).value(&mut cx) as u64;
|
|
327
|
-
|
|
328
|
-
let c = Arc::clone(&self_.c);
|
|
329
|
-
let rt = runtime(&mut cx)?;
|
|
330
|
-
rt.spawn(async move {
|
|
331
|
-
let c = c.lock().await;
|
|
332
|
-
let m = c.query(query, branch, frame).await;
|
|
333
|
-
deferred.settle_with((&channel), move |mut cx| {
|
|
334
|
-
if let Ok(result) = m {
|
|
335
|
-
neon_serde3::to_value(&mut cx, &result)
|
|
336
|
-
.or_else(|e| cx.throw_error(e.to_string()))
|
|
337
|
-
} else {
|
|
338
|
-
cx.throw_error("Error playing")
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
});
|
|
342
|
-
Ok(promise)
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
fn list_branches(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
346
|
-
let channel = cx.channel();
|
|
347
|
-
let (deferred, promise) = cx.promise();
|
|
348
|
-
let self_ = cx.this()
|
|
349
|
-
.downcast_or_throw::<JsBox<NodeChidori>, _>(&mut cx)?;
|
|
350
|
-
let c = Arc::clone(&self_.c);
|
|
351
|
-
let rt = runtime(&mut cx)?;
|
|
352
|
-
rt.spawn(async move {
|
|
353
|
-
let c = c.lock().await;
|
|
354
|
-
let m = c.list_branches().await;
|
|
355
|
-
deferred.settle_with((&channel), move |mut cx| {
|
|
356
|
-
if let Ok(result) = m {
|
|
357
|
-
neon_serde3::to_value(&mut cx, &result)
|
|
358
|
-
.or_else(|e| cx.throw_error(e.to_string()))
|
|
359
|
-
} else {
|
|
360
|
-
cx.throw_error("Error playing")
|
|
361
|
-
}
|
|
362
|
-
});
|
|
363
|
-
});
|
|
364
|
-
Ok(promise)
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
fn display_graph_structure(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
368
|
-
let channel = cx.channel();
|
|
369
|
-
let (deferred, promise) = cx.promise();
|
|
370
|
-
let self_ = cx.this()
|
|
371
|
-
.downcast_or_throw::<JsBox<NodeChidori>, _>(&mut cx)?;
|
|
372
|
-
let branch = cx.argument::<JsNumber>(0).unwrap_or(JsNumber::new(&mut cx, 0.0)).value(&mut cx) as u64;
|
|
373
|
-
let c = Arc::clone(&self_.c);
|
|
374
|
-
let rt = runtime(&mut cx)?;
|
|
375
|
-
rt.spawn(async move {
|
|
376
|
-
let c = c.lock().await;
|
|
377
|
-
let r= c.display_graph_structure(branch).await;
|
|
378
|
-
deferred.settle_with(&channel, move |mut cx| {
|
|
379
|
-
if let Ok(r) = r {
|
|
380
|
-
Ok(cx.string(r))
|
|
381
|
-
} else {
|
|
382
|
-
cx.throw_error("Error displaying graph structure")
|
|
383
|
-
}
|
|
384
|
-
});
|
|
385
|
-
});
|
|
386
|
-
Ok(promise)
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
fn list_registered_graphs(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
390
|
-
let channel = cx.channel();
|
|
391
|
-
let (deferred, promise) = cx.promise();
|
|
392
|
-
let self_ = cx.this()
|
|
393
|
-
.downcast_or_throw::<JsBox<NodeChidori>, _>(&mut cx)?;
|
|
394
|
-
let c = Arc::clone(&self_.c);
|
|
395
|
-
let rt = runtime(&mut cx)?;
|
|
396
|
-
rt.spawn(async move {
|
|
397
|
-
let c = c.lock().await;
|
|
398
|
-
let _ = c.list_registered_graphs().await;
|
|
399
|
-
deferred.settle_with(&channel, move |mut cx| {
|
|
400
|
-
Ok(cx.undefined())
|
|
401
|
-
});
|
|
402
|
-
});
|
|
403
|
-
Ok(promise)
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
//
|
|
407
|
-
// // TODO: need to figure out how to handle callbacks
|
|
408
|
-
// // fn list_input_proposals<'a>(
|
|
409
|
-
// // mut self_: PyRefMut<'_, Self>,
|
|
410
|
-
// // py: Python<'a>,
|
|
411
|
-
// // callback: PyObject
|
|
412
|
-
// // ) -> PyResult<&'a PyAny> {
|
|
413
|
-
// // let file_id = self_.file_id.clone();
|
|
414
|
-
// // let url = self_.url.clone();
|
|
415
|
-
// // let branch = self_.current_branch;
|
|
416
|
-
// // pyo3_asyncio::tokio::future_into_py(py, async move {
|
|
417
|
-
// // let mut client = get_client(url).await?;
|
|
418
|
-
// // let resp = client.list_input_proposals(RequestOnlyId {
|
|
419
|
-
// // id: file_id,
|
|
420
|
-
// // branch,
|
|
421
|
-
// // }).await.map_err(PyErrWrapper::from)?;
|
|
422
|
-
// // let mut stream = resp.into_inner();
|
|
423
|
-
// // while let Some(x) = stream.next().await {
|
|
424
|
-
// // // callback.call(py, (x,), None);
|
|
425
|
-
// // info!("InputProposals = {:?}", x);
|
|
426
|
-
// // };
|
|
427
|
-
// // Ok(())
|
|
428
|
-
// // })
|
|
429
|
-
// // }
|
|
430
|
-
//
|
|
431
|
-
// // fn respond_to_input_proposal(mut self_: PyRefMut<'_, Self>) -> PyResult<()> {
|
|
432
|
-
// // Ok(())
|
|
433
|
-
// // }
|
|
434
|
-
//
|
|
435
|
-
// // TODO: need to figure out how to handle callbacks
|
|
436
|
-
// // fn list_change_events<'a>(
|
|
437
|
-
// // mut self_: PyRefMut<'_, Self>,
|
|
438
|
-
// // py: Python<'a>,
|
|
439
|
-
// // callback: PyObject
|
|
440
|
-
// // ) -> PyResult<&'a PyAny> {
|
|
441
|
-
// // let file_id = self_.file_id.clone();
|
|
442
|
-
// // let url = self_.url.clone();
|
|
443
|
-
// // let branch = self_.current_branch;
|
|
444
|
-
// // pyo3_asyncio::tokio::future_into_py(py, async move {
|
|
445
|
-
// // let mut client = get_client(url).await?;
|
|
446
|
-
// // let resp = client.list_change_events(RequestOnlyId {
|
|
447
|
-
// // id: file_id,
|
|
448
|
-
// // branch,
|
|
449
|
-
// // }).await.map_err(PyErrWrapper::from)?;
|
|
450
|
-
// // let mut stream = resp.into_inner();
|
|
451
|
-
// // while let Some(x) = stream.next().await {
|
|
452
|
-
// // Python::with_gil(|py| pyo3_asyncio::tokio::into_future(callback.as_ref(py).call((x.map(ChangeValueWithCounterWrapper).map_err(PyErrWrapper::from)?,), None)?))?
|
|
453
|
-
// // .await?;
|
|
454
|
-
// // };
|
|
455
|
-
// // Ok(())
|
|
456
|
-
// // })
|
|
457
|
-
// // }
|
|
458
|
-
//
|
|
459
|
-
//
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
fn register_custom_node_handle(mut cx: FunctionContext) -> JsResult<JsValue> {
|
|
464
|
-
let channel = cx.channel();
|
|
465
|
-
let self_ = cx.this()
|
|
466
|
-
.downcast_or_throw::<JsBox<NodeChidori>, _>(&mut cx)?;
|
|
467
|
-
|
|
468
|
-
let function_name: String = cx.argument::<JsString>(0)?.value(&mut cx);
|
|
469
|
-
let callback = cx.argument::<JsFunction>(1)?.root(&mut cx);
|
|
470
|
-
|
|
471
|
-
let h = callback.to_inner(&mut cx);
|
|
472
|
-
let callback = Arc::new(callback);
|
|
473
|
-
let c = Arc::clone(&self_.c);
|
|
474
|
-
|
|
475
|
-
let rt = runtime(&mut cx)?;
|
|
476
|
-
rt.spawn(async move {
|
|
477
|
-
let mut c = c.lock().await;
|
|
478
|
-
c.register_custom_node_handle(function_name, Handler::new(
|
|
479
|
-
move |n| {
|
|
480
|
-
let channel_clone = channel.clone();
|
|
481
|
-
let handler_clone = Arc::clone(&callback);
|
|
482
|
-
Box::pin(async move {
|
|
483
|
-
// TODO: clean this up, can't use ?
|
|
484
|
-
let (tx, mut rx) = mpsc::channel::<serde_json::Value>(1);
|
|
485
|
-
if let Ok(_) = channel_clone.send(move |mut cx| {
|
|
486
|
-
if let Ok(v) = neon_serde3::to_value(&mut cx, &n) {
|
|
487
|
-
let js_function = JsFunction::new(&mut cx, move |mut cx| {
|
|
488
|
-
if let Ok(v) = cx.argument::<JsValue>(0) {
|
|
489
|
-
let value: Result<serde_json::Value, _> = neon_serde3::from_value(&mut cx, v);
|
|
490
|
-
if let Ok(value) = value {
|
|
491
|
-
tx.blocking_send(value).unwrap();
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
Ok(cx.undefined())
|
|
495
|
-
})?;
|
|
496
|
-
let callback = handler_clone.to_inner(&mut cx);
|
|
497
|
-
let _: JsResult<JsValue> = callback.call_with(&mut cx).arg(v).arg(js_function).apply(&mut cx);
|
|
498
|
-
}
|
|
499
|
-
Ok(serde_json::Value::Null)
|
|
500
|
-
}).join() {
|
|
501
|
-
// block until we receive the result from the channel
|
|
502
|
-
if let Some(value) = rx.recv().await {
|
|
503
|
-
Ok(value)
|
|
504
|
-
} else {
|
|
505
|
-
Ok(serde_json::Value::Null)
|
|
506
|
-
}
|
|
507
|
-
} else {
|
|
508
|
-
Err(anyhow::anyhow!("Failed to send result"))
|
|
509
|
-
}
|
|
510
|
-
})
|
|
511
|
-
}
|
|
512
|
-
));
|
|
513
|
-
});
|
|
514
|
-
Ok(cx.undefined().upcast())
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
fn run_custom_node_loop(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
519
|
-
let channel = cx.channel();
|
|
520
|
-
let (deferred, promise) = cx.promise();
|
|
521
|
-
let self_ = cx.this()
|
|
522
|
-
.downcast_or_throw::<JsBox<NodeChidori>, _>(&mut cx)?;
|
|
523
|
-
let c = Arc::clone(&self_.c);
|
|
524
|
-
let rt = runtime(&mut cx)?;
|
|
525
|
-
rt.spawn(async move {
|
|
526
|
-
let mut c = c.lock().await;
|
|
527
|
-
let _ = c.run_custom_node_loop().await;
|
|
528
|
-
deferred.settle_with((&channel), move |mut cx| {
|
|
529
|
-
Ok(cx.undefined())
|
|
530
|
-
});
|
|
531
|
-
|
|
532
|
-
});
|
|
533
|
-
// This promise is never resolved
|
|
534
|
-
Ok(promise)
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
struct NodeGraphBuilder {
|
|
542
|
-
g: Arc<Mutex<GraphBuilder>>,
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
impl Finalize for NodeGraphBuilder {}
|
|
546
|
-
|
|
547
|
-
impl NodeGraphBuilder {
|
|
548
|
-
fn js_new(mut cx: FunctionContext) -> JsResult<JsBox<NodeGraphBuilder>> {
|
|
549
|
-
Ok(cx.boxed(NodeGraphBuilder {
|
|
550
|
-
g: Arc::new(Mutex::new(GraphBuilder::new())),
|
|
551
|
-
}))
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
// // TODO: need to figure out passing a buffer of bytes
|
|
555
|
-
// // TODO: nodes that are added should return a clean definition of what their addition looks like
|
|
556
|
-
// // TODO: adding a node should also display any errors
|
|
557
|
-
// /// x = None
|
|
558
|
-
// /// with open("/Users/coltonpierson/Downloads/files_and_dirs.zip", "rb") as zip_file:
|
|
559
|
-
// /// contents = zip_file.read()
|
|
560
|
-
// /// x = await p.load_zip_file("LoadZip", """ output: String """, contents)
|
|
561
|
-
// /// x
|
|
562
|
-
// // #[pyo3(signature = (name=String::new(), output_tables=vec![], output=String::new(), bytes=vec![]))]
|
|
563
|
-
// // fn load_zip_file<'a>(
|
|
564
|
-
// // mut self_: PyRefMut<'_, Self>,
|
|
565
|
-
// // py: Python<'a>,
|
|
566
|
-
// // name: String,
|
|
567
|
-
// // output_tables: Vec<String>,
|
|
568
|
-
// // output: String,
|
|
569
|
-
// // bytes: Vec<u8>
|
|
570
|
-
// // ) -> PyResult<&'a PyAny> {
|
|
571
|
-
// // let file_id = self_.file_id.clone();
|
|
572
|
-
// // let url = self_.url.clone();
|
|
573
|
-
// // pyo3_asyncio::tokio::future_into_py(py, async move {
|
|
574
|
-
// // let node = create_loader_node(
|
|
575
|
-
// // name,
|
|
576
|
-
// // vec![],
|
|
577
|
-
// // output,
|
|
578
|
-
// // LoadFrom::ZipfileBytes(bytes),
|
|
579
|
-
// // output_tables
|
|
580
|
-
// // );
|
|
581
|
-
// // Ok(push_file_merge(&url, &file_id, node).await?)
|
|
582
|
-
// // })
|
|
583
|
-
// // }
|
|
584
|
-
//
|
|
585
|
-
// // TODO: this should accept an "Object" instead of args
|
|
586
|
-
// // TODO: nodes that are added should return a clean definition of what their addition looks like
|
|
587
|
-
// // TODO: adding a node should also display any errors
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
fn prompt_node(mut cx: FunctionContext) -> JsResult<JsBox<RefCell<NodeNodeHandle>>> {
|
|
591
|
-
let self_ = cx.this()
|
|
592
|
-
.downcast_or_throw::<JsBox<NodeGraphBuilder>, _>(&mut cx)?;
|
|
593
|
-
let arg0 = cx.argument::<JsValue>(0)?;
|
|
594
|
-
let arg0_value: PromptNodeCreateOpts = match neon_serde3::from_value(&mut cx, arg0) {
|
|
595
|
-
Ok(value) => value,
|
|
596
|
-
Err(e) => {
|
|
597
|
-
return cx.throw_error(e.to_string());
|
|
598
|
-
}
|
|
599
|
-
};
|
|
600
|
-
let mut g = self_.g.blocking_lock();
|
|
601
|
-
match g.prompt_node(arg0_value) {
|
|
602
|
-
Ok(result) => Ok(cx.boxed(RefCell::new(NodeNodeHandle::from(result)))),
|
|
603
|
-
Err(e) => cx.throw_error(e.to_string())
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
fn custom_node(mut cx: FunctionContext) -> JsResult<JsBox<RefCell<NodeNodeHandle>>> {
|
|
609
|
-
let self_ = cx.this()
|
|
610
|
-
.downcast_or_throw::<JsBox<NodeGraphBuilder>, _>(&mut cx)?;
|
|
611
|
-
let arg0 = cx.argument::<JsValue>(0)?;
|
|
612
|
-
let arg0_value: CustomNodeCreateOpts = match neon_serde3::from_value(&mut cx, arg0) {
|
|
613
|
-
Ok(value) => value,
|
|
614
|
-
Err(e) => {
|
|
615
|
-
return cx.throw_error(e.to_string());
|
|
616
|
-
}
|
|
617
|
-
};
|
|
618
|
-
let mut g = self_.g.blocking_lock();
|
|
619
|
-
match g.custom_node(arg0_value) {
|
|
620
|
-
Ok(result) => Ok(cx.boxed(RefCell::new(NodeNodeHandle::from(result)))),
|
|
621
|
-
Err(e) => cx.throw_error(e.to_string())
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
fn deno_code_node(mut cx: FunctionContext) -> JsResult<JsBox<RefCell<NodeNodeHandle>>> {
|
|
626
|
-
let self_ = cx.this()
|
|
627
|
-
.downcast_or_throw::<JsBox<NodeGraphBuilder>, _>(&mut cx)?;
|
|
628
|
-
let arg0 = cx.argument::<JsValue>(0)?;
|
|
629
|
-
let arg0_value: DenoCodeNodeCreateOpts = match neon_serde3::from_value(&mut cx, arg0) {
|
|
630
|
-
Ok(value) => value,
|
|
631
|
-
Err(e) => {
|
|
632
|
-
return cx.throw_error(e.to_string());
|
|
633
|
-
}
|
|
634
|
-
};
|
|
635
|
-
let mut g = self_.g.blocking_lock();
|
|
636
|
-
match g.deno_code_node(arg0_value) {
|
|
637
|
-
Ok(result) => Ok(cx.boxed(RefCell::new(NodeNodeHandle::from(result)))),
|
|
638
|
-
Err(e) => cx.throw_error(e.to_string())
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
fn vector_memory_node(mut cx: FunctionContext) -> JsResult<JsBox<RefCell<NodeNodeHandle>>> {
|
|
643
|
-
let self_ = cx.this()
|
|
644
|
-
.downcast_or_throw::<JsBox<NodeGraphBuilder>, _>(&mut cx)?;
|
|
645
|
-
let arg0 = cx.argument::<JsValue>(0)?;
|
|
646
|
-
let arg0_value: VectorMemoryNodeCreateOpts = match neon_serde3::from_value(&mut cx, arg0) {
|
|
647
|
-
Ok(value) => value,
|
|
648
|
-
Err(e) => {
|
|
649
|
-
return cx.throw_error(e.to_string());
|
|
650
|
-
}
|
|
651
|
-
};
|
|
652
|
-
let mut g = self_.g.blocking_lock();
|
|
653
|
-
match g.vector_memory_node(arg0_value) {
|
|
654
|
-
Ok(result) => Ok(cx.boxed(RefCell::new(NodeNodeHandle::from(result)))),
|
|
655
|
-
Err(e) => cx.throw_error(e.to_string())
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
fn commit(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
662
|
-
let channel = cx.channel();
|
|
663
|
-
let (deferred, promise) = cx.promise();
|
|
664
|
-
let self_ = cx.this()
|
|
665
|
-
.downcast_or_throw::<JsBox<NodeGraphBuilder>, _>(&mut cx)?;
|
|
666
|
-
let node_chidori = cx.argument::<JsBox<NodeChidori>>(0)?;
|
|
667
|
-
let branch = cx.argument::<JsNumber>(1).unwrap_or(JsNumber::new(&mut cx, 0.0)).value(&mut cx) as u64;
|
|
668
|
-
|
|
669
|
-
let c = Arc::clone(&node_chidori.c);
|
|
670
|
-
let g = Arc::clone(&self_.g);
|
|
671
|
-
|
|
672
|
-
let rt = runtime(&mut cx)?;
|
|
673
|
-
rt.spawn(async move {
|
|
674
|
-
let mut graph_builder = g.lock().await;
|
|
675
|
-
let mut chidori = c.lock().await;
|
|
676
|
-
let m = graph_builder.commit(&mut chidori, branch).await;
|
|
677
|
-
deferred.settle_with((&channel), move |mut cx| {
|
|
678
|
-
if let Ok(result) = m {
|
|
679
|
-
neon_serde3::to_value(&mut cx, &result)
|
|
680
|
-
.or_else(|e| cx.throw_error(e.to_string()))
|
|
681
|
-
} else {
|
|
682
|
-
cx.throw_error("Error playing")
|
|
683
|
-
}
|
|
684
|
-
});
|
|
685
|
-
});
|
|
686
|
-
Ok(promise)
|
|
687
|
-
}
|
|
688
|
-
//
|
|
689
|
-
//
|
|
690
|
-
// //
|
|
691
|
-
// // fn observation_node(mut self_: PyRefMut<'_, Self>, name: String, query_def: Option<String>, template: String, model: String) -> PyResult<()> {
|
|
692
|
-
// // let file_id = self_.file_id.clone();
|
|
693
|
-
// // let node = create_observation_node(
|
|
694
|
-
// // "".to_string(),
|
|
695
|
-
// // None,
|
|
696
|
-
// // "".to_string(),
|
|
697
|
-
// // );
|
|
698
|
-
// // executor::block_on(self_.client.merge(RequestFileMerge {
|
|
699
|
-
// // id: file_id,
|
|
700
|
-
// // file: Some(File {
|
|
701
|
-
// // nodes: vec![node],
|
|
702
|
-
// // ..Default::default()
|
|
703
|
-
// // }),
|
|
704
|
-
// // branch: 0,
|
|
705
|
-
// // }));
|
|
706
|
-
// // Ok(())
|
|
707
|
-
// // }
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
fn neon_simple_fun(mut cx: FunctionContext) -> JsResult<JsString> {
|
|
712
|
-
let port = cx.argument::<JsString>(0)?.value(&mut cx);
|
|
713
|
-
Ok(cx.string(port))
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
#[neon::main]
|
|
717
|
-
fn main(mut cx: ModuleContext) -> NeonResult<()> {
|
|
718
|
-
env_logger::init();
|
|
719
|
-
cx.export_function("nodehandleRunWhen", NodeNodeHandle::run_when)?;
|
|
720
|
-
// cx.export_function("nodehandleQuery", NodeNodeHandle::query)?;
|
|
721
|
-
cx.export_function("chidoriNew", NodeChidori::js_new)?;
|
|
722
|
-
cx.export_function("chidoriStartServer", NodeChidori::start_server)?;
|
|
723
|
-
cx.export_function("chidoriPlay", NodeChidori::play)?;
|
|
724
|
-
cx.export_function("chidoriPause", NodeChidori::pause)?;
|
|
725
|
-
cx.export_function("chidoriBranch", NodeChidori::branch)?;
|
|
726
|
-
cx.export_function("chidoriQuery", NodeChidori::query)?;
|
|
727
|
-
cx.export_function("chidoriGraphStructure", NodeChidori::display_graph_structure)?;
|
|
728
|
-
cx.export_function("chidoriRegisterCustomNodeHandle", NodeChidori::register_custom_node_handle)?;
|
|
729
|
-
cx.export_function("chidoriRunCustomNodeLoop", NodeChidori::run_custom_node_loop)?;
|
|
730
|
-
|
|
731
|
-
cx.export_function("graphbuilderNew", NodeGraphBuilder::js_new)?;
|
|
732
|
-
cx.export_function("graphbuilderCustomNode", NodeGraphBuilder::custom_node)?;
|
|
733
|
-
cx.export_function("graphbuilderDenoCodeNode", NodeGraphBuilder::deno_code_node)?;
|
|
734
|
-
cx.export_function("graphbuilderPromptNode", NodeGraphBuilder::prompt_node)?;
|
|
735
|
-
cx.export_function("graphbuilderVectorMemoryNode", NodeGraphBuilder::vector_memory_node)?;
|
|
736
|
-
cx.export_function("graphbuilderCommit", NodeGraphBuilder::commit)?;
|
|
737
|
-
cx.export_function("simpleFun", neon_simple_fun)?;
|
|
738
|
-
Ok(())
|
|
739
|
-
}
|