@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
package/src/translations/rust.rs
DELETED
|
@@ -1,737 +0,0 @@
|
|
|
1
|
-
use std::cell::RefCell;
|
|
2
|
-
use std::collections::{HashMap, VecDeque};
|
|
3
|
-
use std::future::Future;
|
|
4
|
-
use std::hash::Hash;
|
|
5
|
-
use std::marker::PhantomData;
|
|
6
|
-
use std::pin::Pin;
|
|
7
|
-
use std::sync::Arc;
|
|
8
|
-
use anyhow::Error;
|
|
9
|
-
use futures::future::BoxFuture;
|
|
10
|
-
use futures::StreamExt;
|
|
11
|
-
use log::{debug, info};
|
|
12
|
-
use once_cell::sync::OnceCell;
|
|
13
|
-
use tokio::runtime::Runtime;
|
|
14
|
-
use prompt_graph_core::build_runtime_graph::graph_parse::{CleanedDefinitionGraph, CleanIndividualNode, construct_query_from_output_type, derive_for_individual_node};
|
|
15
|
-
use prompt_graph_core::graph_definition::{create_code_node, create_custom_node, create_prompt_node, create_vector_memory_node, SourceNodeType};
|
|
16
|
-
use prompt_graph_core::proto2::{ChangeValue, ChangeValueWithCounter, Empty, ExecutionStatus, File, FileAddressedChangeValueWithCounter, FilteredPollNodeWillExecuteEventsRequest, Item, ListBranchesRes, NodeWillExecute, NodeWillExecuteOnBranch, Path, Query, QueryAtFrame, QueryAtFrameResponse, RequestAckNodeWillExecuteEvent, RequestAtFrame, RequestFileMerge, RequestListBranches, RequestNewBranch, RequestOnlyId, RespondPollNodeWillExecuteEvents, SerializedValue, SerializedValueArray, SerializedValueObject};
|
|
17
|
-
use prompt_graph_core::proto2::execution_runtime_client::ExecutionRuntimeClient;
|
|
18
|
-
use prompt_graph_core::proto2::serialized_value::Val;
|
|
19
|
-
use prompt_graph_exec::tonic_runtime::run_server;
|
|
20
|
-
use neon_serde3;
|
|
21
|
-
use serde::{Deserialize, Serialize};
|
|
22
|
-
use tonic::Status;
|
|
23
|
-
use prompt_graph_core::templates::json_value_to_serialized_value;
|
|
24
|
-
use crate::translations::shared::json_value_to_paths;
|
|
25
|
-
pub use prompt_graph_core::utils::serialized_value_to_string;
|
|
26
|
-
|
|
27
|
-
async fn get_client(url: String) -> Result<ExecutionRuntimeClient<tonic::transport::Channel>, tonic::transport::Error> {
|
|
28
|
-
ExecutionRuntimeClient::connect(url.clone()).await
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
type CallbackHandler = Box<dyn Fn(NodeWillExecuteOnBranch) -> BoxFuture<'static, anyhow::Result<serde_json::Value>> + Send + Sync>;
|
|
32
|
-
|
|
33
|
-
pub struct Handler {
|
|
34
|
-
pub(crate) callback: CallbackHandler
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
impl Handler {
|
|
38
|
-
pub fn new<F>(f: F) -> Self
|
|
39
|
-
where
|
|
40
|
-
F: Fn(NodeWillExecuteOnBranch) -> BoxFuture<'static, anyhow::Result<serde_json::Value>> + Send + Sync + 'static
|
|
41
|
-
{
|
|
42
|
-
Handler {
|
|
43
|
-
callback: Box::new(f),
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
#[derive(Clone)]
|
|
50
|
-
pub struct Chidori {
|
|
51
|
-
file_id: String,
|
|
52
|
-
current_head: u64,
|
|
53
|
-
current_branch: u64,
|
|
54
|
-
url: String,
|
|
55
|
-
pub(crate) custom_node_handlers: HashMap<String, Arc<Handler>>
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
impl Chidori {
|
|
59
|
-
|
|
60
|
-
pub fn new(file_id: String, url: String) -> Self {
|
|
61
|
-
if !url.contains("://") {
|
|
62
|
-
panic!("Invalid url, must include protocol");
|
|
63
|
-
}
|
|
64
|
-
// let api_token = cx.argument_opt(2)?.value(&mut cx);
|
|
65
|
-
debug!("Creating new Chidori instance with file_id={}, url={}, api_token={:?}", file_id, url, "".to_string());
|
|
66
|
-
Chidori {
|
|
67
|
-
file_id,
|
|
68
|
-
current_head: 0,
|
|
69
|
-
current_branch: 0,
|
|
70
|
-
url,
|
|
71
|
-
custom_node_handlers: HashMap::new(),
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
pub async fn start_server(&self, file_path: Option<String>) -> anyhow::Result<()> {
|
|
76
|
-
let url_server = self.url.clone();
|
|
77
|
-
std::thread::spawn(move || {
|
|
78
|
-
let result = run_server(url_server, file_path);
|
|
79
|
-
match result {
|
|
80
|
-
Ok(_) => {
|
|
81
|
-
println!("Server exited");
|
|
82
|
-
},
|
|
83
|
-
Err(e) => {
|
|
84
|
-
println!("Error running server: {}", e);
|
|
85
|
-
},
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
let url = self.url.clone();
|
|
90
|
-
loop {
|
|
91
|
-
match get_client(url.clone()).await {
|
|
92
|
-
Ok(connection) => {
|
|
93
|
-
eprintln!("Connection successfully established {:?}", &url);
|
|
94
|
-
return Ok(());
|
|
95
|
-
},
|
|
96
|
-
Err(e) => {
|
|
97
|
-
eprintln!("Error connecting to server: {} with Error {}. Retrying...", &url, &e.to_string());
|
|
98
|
-
std::thread::sleep(std::time::Duration::from_millis(1000));
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
pub async fn play(&self, branch: u64, frame: u64) -> anyhow::Result<ExecutionStatus> {
|
|
105
|
-
let file_id = self.file_id.clone();
|
|
106
|
-
let url = self.url.clone();
|
|
107
|
-
let mut client = get_client(url).await?;
|
|
108
|
-
let result = client.play(RequestAtFrame {
|
|
109
|
-
id: file_id,
|
|
110
|
-
frame,
|
|
111
|
-
branch,
|
|
112
|
-
}).await?;
|
|
113
|
-
Ok(result.into_inner())
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
pub async fn pause(&self, frame: u64) -> anyhow::Result<ExecutionStatus> {
|
|
117
|
-
let file_id = self.file_id.clone();
|
|
118
|
-
let url = self.url.clone();
|
|
119
|
-
let branch = self.current_branch.clone();
|
|
120
|
-
|
|
121
|
-
let mut client = get_client(url).await?;
|
|
122
|
-
let result = client.pause(RequestAtFrame {
|
|
123
|
-
id: file_id,
|
|
124
|
-
frame,
|
|
125
|
-
branch,
|
|
126
|
-
}).await?;
|
|
127
|
-
Ok(result.into_inner())
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
pub async fn query( &self, query: String, branch: u64, frame: u64, ) -> anyhow::Result<QueryAtFrameResponse> {
|
|
131
|
-
let file_id = self.file_id.clone();
|
|
132
|
-
let url = self.url.clone();
|
|
133
|
-
let mut client = get_client(url).await?;
|
|
134
|
-
let result = client.run_query(QueryAtFrame {
|
|
135
|
-
id: file_id,
|
|
136
|
-
query: Some(Query {
|
|
137
|
-
query: Some(query)
|
|
138
|
-
}),
|
|
139
|
-
frame,
|
|
140
|
-
branch,
|
|
141
|
-
}).await?;
|
|
142
|
-
Ok(result.into_inner())
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
pub async fn branch( &self, branch: u64, frame: u64, ) -> anyhow::Result<ExecutionStatus> {
|
|
146
|
-
let file_id = self.file_id.clone();
|
|
147
|
-
let url = self.url.clone();
|
|
148
|
-
let mut client = get_client(url).await?;
|
|
149
|
-
let result = client.branch(RequestNewBranch {
|
|
150
|
-
id: file_id,
|
|
151
|
-
source_branch_id: branch,
|
|
152
|
-
diverges_at_counter: frame
|
|
153
|
-
}).await?;
|
|
154
|
-
Ok(result.into_inner())
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
pub async fn list_branches( &self) -> anyhow::Result<ListBranchesRes> {
|
|
158
|
-
let file_id = self.file_id.clone();
|
|
159
|
-
let url = self.url.clone();
|
|
160
|
-
let mut client = get_client(url).await?;
|
|
161
|
-
let result = client.list_branches(RequestListBranches {
|
|
162
|
-
id: file_id,
|
|
163
|
-
}).await?;
|
|
164
|
-
Ok(result.into_inner())
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
pub async fn display_graph_structure( &self, branch: u64) -> anyhow::Result<String> {
|
|
168
|
-
let file_id = self.file_id.clone();
|
|
169
|
-
let url = self.url.clone();
|
|
170
|
-
let mut client = get_client(url).await?;
|
|
171
|
-
let file = client.current_file_state(RequestOnlyId {
|
|
172
|
-
id: file_id,
|
|
173
|
-
branch
|
|
174
|
-
}).await?;
|
|
175
|
-
let mut file = file.into_inner();
|
|
176
|
-
let mut g = CleanedDefinitionGraph::zero();
|
|
177
|
-
g.merge_file(&mut file).unwrap();
|
|
178
|
-
Ok(g.get_dot_graph())
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
pub async fn list_registered_graphs(&self) -> anyhow::Result<()> {
|
|
182
|
-
let file_id = self.file_id.clone();
|
|
183
|
-
let url = self.url.clone();
|
|
184
|
-
let mut client = get_client(url).await?;
|
|
185
|
-
let resp = client.list_registered_graphs(Empty { }).await?;
|
|
186
|
-
let mut stream = resp.into_inner();
|
|
187
|
-
while let Some(x) = stream.next().await {
|
|
188
|
-
// callback.call(py, (x,), None);
|
|
189
|
-
info!("Registered Graph = {:?}", x);
|
|
190
|
-
};
|
|
191
|
-
Ok(())
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
//
|
|
195
|
-
// // TODO: need to figure out how to handle callbacks
|
|
196
|
-
// // fn list_input_proposals<'a>(
|
|
197
|
-
// // mut self_: PyRefMut<'_, Self>,
|
|
198
|
-
// // py: Python<'a>,
|
|
199
|
-
// // callback: PyObject
|
|
200
|
-
// // ) -> PyResult<&'a PyAny> {
|
|
201
|
-
// // let file_id = self_.file_id.clone();
|
|
202
|
-
// // let url = self_.url.clone();
|
|
203
|
-
// // let branch = self_.current_branch;
|
|
204
|
-
// // pyo3_asyncio::tokio::future_into_py(py, async move {
|
|
205
|
-
// // let mut client = get_client(url).await?;
|
|
206
|
-
// // let resp = client.list_input_proposals(RequestOnlyId {
|
|
207
|
-
// // id: file_id,
|
|
208
|
-
// // branch,
|
|
209
|
-
// // }).await.map_err(PyErrWrapper::from)?;
|
|
210
|
-
// // let mut stream = resp.into_inner();
|
|
211
|
-
// // while let Some(x) = stream.next().await {
|
|
212
|
-
// // // callback.call(py, (x,), None);
|
|
213
|
-
// // info!("InputProposals = {:?}", x);
|
|
214
|
-
// // };
|
|
215
|
-
// // Ok(())
|
|
216
|
-
// // })
|
|
217
|
-
// // }
|
|
218
|
-
//
|
|
219
|
-
// // fn respond_to_input_proposal(mut self_: PyRefMut<'_, Self>) -> PyResult<()> {
|
|
220
|
-
// // Ok(())
|
|
221
|
-
// // }
|
|
222
|
-
//
|
|
223
|
-
// // TODO: need to figure out how to handle callbacks
|
|
224
|
-
// // fn list_change_events<'a>(
|
|
225
|
-
// // mut self_: PyRefMut<'_, Self>,
|
|
226
|
-
// // py: Python<'a>,
|
|
227
|
-
// // callback: PyObject
|
|
228
|
-
// // ) -> PyResult<&'a PyAny> {
|
|
229
|
-
// // let file_id = self_.file_id.clone();
|
|
230
|
-
// // let url = self_.url.clone();
|
|
231
|
-
// // let branch = self_.current_branch;
|
|
232
|
-
// // pyo3_asyncio::tokio::future_into_py(py, async move {
|
|
233
|
-
// // let mut client = get_client(url).await?;
|
|
234
|
-
// // let resp = client.list_change_events(RequestOnlyId {
|
|
235
|
-
// // id: file_id,
|
|
236
|
-
// // branch,
|
|
237
|
-
// // }).await.map_err(PyErrWrapper::from)?;
|
|
238
|
-
// // let mut stream = resp.into_inner();
|
|
239
|
-
// // while let Some(x) = stream.next().await {
|
|
240
|
-
// // Python::with_gil(|py| pyo3_asyncio::tokio::into_future(callback.as_ref(py).call((x.map(ChangeValueWithCounterWrapper).map_err(PyErrWrapper::from)?,), None)?))?
|
|
241
|
-
// // .await?;
|
|
242
|
-
// // };
|
|
243
|
-
// // Ok(())
|
|
244
|
-
// // })
|
|
245
|
-
// // }
|
|
246
|
-
//
|
|
247
|
-
//
|
|
248
|
-
//
|
|
249
|
-
// // TODO: this should accept an "Object" instead of args
|
|
250
|
-
// // TODO: nodes that are added should return a clean definition of what their addition looks like
|
|
251
|
-
// // TODO: adding a node should also display any errors
|
|
252
|
-
|
|
253
|
-
pub fn register_custom_node_handle(&mut self, key: String, handler: Handler) {
|
|
254
|
-
self.custom_node_handlers.insert(key, Arc::new(handler));
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
pub async fn poll_local_code_node_execution(&self) -> anyhow::Result<RespondPollNodeWillExecuteEvents> {
|
|
258
|
-
let file_id = self.file_id.clone();
|
|
259
|
-
let url = self.url.clone();
|
|
260
|
-
let mut client = get_client(url).await?;
|
|
261
|
-
let req = FilteredPollNodeWillExecuteEventsRequest { id: file_id.clone() };
|
|
262
|
-
let result = client.poll_custom_node_will_execute_events(req).await?;
|
|
263
|
-
Ok(result.into_inner())
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
pub async fn ack_local_code_node_execution(&self, branch: u64, counter : u64) -> anyhow::Result<ExecutionStatus> {
|
|
267
|
-
let file_id = self.file_id.clone();
|
|
268
|
-
let url = self.url.clone();
|
|
269
|
-
let mut client = get_client(url).await?;
|
|
270
|
-
let result = client.ack_node_will_execute_event(RequestAckNodeWillExecuteEvent {
|
|
271
|
-
id: file_id.clone(),
|
|
272
|
-
branch,
|
|
273
|
-
counter,
|
|
274
|
-
}).await?;
|
|
275
|
-
Ok(result.into_inner())
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
pub async fn respond_local_code_node_execution<T: Serialize>(
|
|
279
|
-
&self,
|
|
280
|
-
branch: u64,
|
|
281
|
-
counter: u64,
|
|
282
|
-
node_name: String,
|
|
283
|
-
response: T
|
|
284
|
-
) -> anyhow::Result<ExecutionStatus> {
|
|
285
|
-
let file_id = self.file_id.clone();
|
|
286
|
-
let url = self.url.clone();
|
|
287
|
-
|
|
288
|
-
let json_object = serde_json::to_value(response)?;
|
|
289
|
-
let response_paths = json_value_to_paths(&json_object);
|
|
290
|
-
let filled_values = response_paths.into_iter().map(|path| {
|
|
291
|
-
ChangeValue {
|
|
292
|
-
path: Some(Path {
|
|
293
|
-
address: path.0,
|
|
294
|
-
}),
|
|
295
|
-
value: Some(path.1),
|
|
296
|
-
branch,
|
|
297
|
-
}
|
|
298
|
-
}).collect();
|
|
299
|
-
|
|
300
|
-
// TODO: need parent counters from the original change
|
|
301
|
-
// TODO: need source node
|
|
302
|
-
let mut client = get_client(url).await?;
|
|
303
|
-
|
|
304
|
-
// TODO: need to add the output table paths to these
|
|
305
|
-
// TODO: this needs to look more like a real change
|
|
306
|
-
Ok(client.push_worker_event(FileAddressedChangeValueWithCounter {
|
|
307
|
-
branch,
|
|
308
|
-
counter,
|
|
309
|
-
node_name: node_name.clone(),
|
|
310
|
-
id: file_id.clone(),
|
|
311
|
-
change: Some(ChangeValueWithCounter {
|
|
312
|
-
filled_values,
|
|
313
|
-
parent_monotonic_counters: vec![],
|
|
314
|
-
monotonic_counter: counter,
|
|
315
|
-
branch,
|
|
316
|
-
source_node: node_name.clone(),
|
|
317
|
-
})
|
|
318
|
-
}).await?.into_inner())
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
pub async fn run_custom_node_loop(&self) -> anyhow::Result<()> {
|
|
322
|
-
loop {
|
|
323
|
-
let mut backoff = 2;
|
|
324
|
-
let events = self.poll_local_code_node_execution().await?;
|
|
325
|
-
if events.node_will_execute_events.len() <= 0 {
|
|
326
|
-
backoff = backoff * backoff;
|
|
327
|
-
tokio::time::sleep(std::time::Duration::from_millis(100 * backoff)).await;
|
|
328
|
-
continue;
|
|
329
|
-
} else {
|
|
330
|
-
backoff = 2;
|
|
331
|
-
for ev in &events.node_will_execute_events {
|
|
332
|
-
// ACK messages
|
|
333
|
-
let NodeWillExecuteOnBranch { branch, counter, node, ..} = ev;
|
|
334
|
-
let node_name = &node.as_ref().unwrap().source_node;
|
|
335
|
-
if let Some(x) = self.custom_node_handlers.get(&ev.custom_node_type_name.clone().unwrap()) {
|
|
336
|
-
self.ack_local_code_node_execution(*branch, *counter).await?;
|
|
337
|
-
let result = (x.as_ref().callback)(ev.clone()).await?;
|
|
338
|
-
dbg!(&result);
|
|
339
|
-
self.respond_local_code_node_execution(*branch, *counter, node_name.clone(), result).await?;
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
fn default_queries() -> Option<Vec<String>> {
|
|
349
|
-
Some(vec!["None".to_string()])
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
#[derive(serde::Serialize, serde::Deserialize)]
|
|
353
|
-
pub struct PromptNodeCreateOpts {
|
|
354
|
-
pub name: String,
|
|
355
|
-
pub queries: Option<Vec<String>>,
|
|
356
|
-
pub output_tables: Option<Vec<String>>,
|
|
357
|
-
pub template: String,
|
|
358
|
-
pub model: Option<String>
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
impl Default for PromptNodeCreateOpts {
|
|
362
|
-
fn default() -> Self {
|
|
363
|
-
PromptNodeCreateOpts {
|
|
364
|
-
name: "".to_string(),
|
|
365
|
-
queries: default_queries(),
|
|
366
|
-
output_tables: None,
|
|
367
|
-
template: "".to_string(),
|
|
368
|
-
model: Some("GPT_3_5_TURBO".to_string()),
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
impl PromptNodeCreateOpts {
|
|
373
|
-
pub fn merge(&mut self, other: PromptNodeCreateOpts) {
|
|
374
|
-
self.name = other.name;
|
|
375
|
-
self.queries = other.queries.or(self.queries.take());
|
|
376
|
-
self.output_tables = other.output_tables.or(self.output_tables.take());
|
|
377
|
-
self.template = other.template;
|
|
378
|
-
self.model = other.model.or(self.model.take());
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
#[derive(serde::Serialize, serde::Deserialize)]
|
|
385
|
-
pub struct CustomNodeCreateOpts {
|
|
386
|
-
pub name: String,
|
|
387
|
-
pub queries: Option<Vec<String>>,
|
|
388
|
-
pub output_tables: Option<Vec<String>>,
|
|
389
|
-
pub output: Option<String>,
|
|
390
|
-
pub node_type_name: String
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
impl Default for CustomNodeCreateOpts {
|
|
395
|
-
fn default() -> Self {
|
|
396
|
-
CustomNodeCreateOpts {
|
|
397
|
-
name: "".to_string(),
|
|
398
|
-
queries: default_queries(),
|
|
399
|
-
output_tables: None,
|
|
400
|
-
output: None,
|
|
401
|
-
node_type_name: "".to_string(),
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
impl CustomNodeCreateOpts {
|
|
406
|
-
pub fn merge(&mut self, other: CustomNodeCreateOpts) {
|
|
407
|
-
self.name = other.name;
|
|
408
|
-
self.queries = other.queries.or(self.queries.take());
|
|
409
|
-
self.output_tables = other.output_tables.or(self.output_tables.take());
|
|
410
|
-
self.output = other.output.or(self.output.take());
|
|
411
|
-
self.node_type_name = other.node_type_name;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
#[derive(serde::Serialize, serde::Deserialize)]
|
|
418
|
-
pub struct DenoCodeNodeCreateOpts {
|
|
419
|
-
pub name: String,
|
|
420
|
-
pub queries: Option<Vec<String>>,
|
|
421
|
-
pub output_tables: Option<Vec<String>>,
|
|
422
|
-
pub output: Option<String>,
|
|
423
|
-
pub code: String,
|
|
424
|
-
pub is_template: Option<bool>
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
impl Default for DenoCodeNodeCreateOpts {
|
|
428
|
-
fn default() -> Self {
|
|
429
|
-
DenoCodeNodeCreateOpts {
|
|
430
|
-
name: "".to_string(),
|
|
431
|
-
queries: default_queries(),
|
|
432
|
-
output_tables: None,
|
|
433
|
-
output: None,
|
|
434
|
-
code: "".to_string(),
|
|
435
|
-
is_template: None,
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
impl DenoCodeNodeCreateOpts {
|
|
441
|
-
pub fn merge(&mut self, other: DenoCodeNodeCreateOpts) {
|
|
442
|
-
self.name = other.name;
|
|
443
|
-
self.queries = other.queries.or(self.queries.take());
|
|
444
|
-
self.output_tables = other.output_tables.or(self.output_tables.take());
|
|
445
|
-
self.output = other.output.or(self.output.take());
|
|
446
|
-
self.code = other.code;
|
|
447
|
-
self.is_template = other.is_template.or(self.is_template.take());
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
#[derive(serde::Serialize, serde::Deserialize)]
|
|
452
|
-
pub struct VectorMemoryNodeCreateOpts {
|
|
453
|
-
pub name: String,
|
|
454
|
-
pub queries: Option<Vec<String>>,
|
|
455
|
-
pub output_tables: Option<Vec<String>>,
|
|
456
|
-
pub output: Option<String>,
|
|
457
|
-
pub template: Option<String>, // TODO: default is the contents of the query
|
|
458
|
-
pub action: Option<String>, // TODO: default WRITE
|
|
459
|
-
pub embedding_model: Option<String>, // TODO: default TEXT_EMBEDDING_ADA_002
|
|
460
|
-
pub db_vendor: Option<String>, // TODO: default QDRANT
|
|
461
|
-
pub collection_name: String,
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
impl Default for VectorMemoryNodeCreateOpts {
|
|
466
|
-
fn default() -> Self {
|
|
467
|
-
VectorMemoryNodeCreateOpts {
|
|
468
|
-
name: "".to_string(),
|
|
469
|
-
queries: None,
|
|
470
|
-
output_tables: None,
|
|
471
|
-
output: None,
|
|
472
|
-
template: None,
|
|
473
|
-
action: None,
|
|
474
|
-
embedding_model: None,
|
|
475
|
-
db_vendor: None,
|
|
476
|
-
collection_name: "".to_string(),
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
impl VectorMemoryNodeCreateOpts {
|
|
483
|
-
pub fn merge(&mut self, other: VectorMemoryNodeCreateOpts) {
|
|
484
|
-
self.name = other.name;
|
|
485
|
-
self.queries = other.queries.or(self.queries.take());
|
|
486
|
-
self.output_tables = other.output_tables.or(self.output_tables.take());
|
|
487
|
-
self.output = other.output.or(self.output.take());
|
|
488
|
-
self.template = other.template.or(self.template.take());
|
|
489
|
-
self.action = other.action.or(self.action.take());
|
|
490
|
-
self.embedding_model = other.embedding_model.or(self.embedding_model.take());
|
|
491
|
-
self.db_vendor = other.db_vendor.or(self.db_vendor.take());
|
|
492
|
-
self.collection_name = other.collection_name;
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
fn remap_queries(queries: Option<Vec<String>>) -> Vec<Option<String>> {
|
|
497
|
-
let queries: Vec<Option<String>> = if let Some(queries) = queries {
|
|
498
|
-
queries.into_iter().map(|q| {
|
|
499
|
-
if q == "None".to_string() {
|
|
500
|
-
None
|
|
501
|
-
} else {
|
|
502
|
-
Some(q)
|
|
503
|
-
}
|
|
504
|
-
}).collect()
|
|
505
|
-
} else {
|
|
506
|
-
vec![]
|
|
507
|
-
};
|
|
508
|
-
queries
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
#[derive(Clone)]
|
|
512
|
-
pub struct GraphBuilder {
|
|
513
|
-
clean_graph: CleanedDefinitionGraph,
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
impl GraphBuilder {
|
|
517
|
-
pub fn new() -> Self {
|
|
518
|
-
GraphBuilder {
|
|
519
|
-
clean_graph: CleanedDefinitionGraph::zero()
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
pub fn prompt_node(&mut self, arg: PromptNodeCreateOpts) -> anyhow::Result<NodeHandle> {
|
|
523
|
-
let mut def = PromptNodeCreateOpts::default();
|
|
524
|
-
def.merge(arg);
|
|
525
|
-
let node = create_prompt_node(
|
|
526
|
-
def.name.clone(),
|
|
527
|
-
remap_queries(def.queries),
|
|
528
|
-
def.template,
|
|
529
|
-
def.model.unwrap_or("GPT_3_5_TURBO".to_string()),
|
|
530
|
-
def.output_tables.unwrap_or(vec![]))?;
|
|
531
|
-
self.clean_graph.merge_file(&File { nodes: vec![node.clone()], ..Default::default() })?;
|
|
532
|
-
Ok(NodeHandle::from(node)?)
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
pub fn custom_node(&mut self, arg: CustomNodeCreateOpts) -> anyhow::Result<NodeHandle> {
|
|
536
|
-
let mut def = CustomNodeCreateOpts::default();
|
|
537
|
-
def.merge(arg);
|
|
538
|
-
let node = create_custom_node(
|
|
539
|
-
def.name.clone(),
|
|
540
|
-
remap_queries(def.queries.clone()),
|
|
541
|
-
def.output.unwrap_or("{}".to_string()),
|
|
542
|
-
def.node_type_name,
|
|
543
|
-
def.output_tables.unwrap_or(vec![])
|
|
544
|
-
);
|
|
545
|
-
self.clean_graph.merge_file(&File { nodes: vec![node.clone()], ..Default::default() })?;
|
|
546
|
-
Ok(NodeHandle::from(node)?)
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
pub fn deno_code_node(&mut self, arg: DenoCodeNodeCreateOpts) -> anyhow::Result<NodeHandle> {
|
|
551
|
-
let mut def = DenoCodeNodeCreateOpts::default();
|
|
552
|
-
def.merge(arg);
|
|
553
|
-
let node = create_code_node(
|
|
554
|
-
def.name.clone(),
|
|
555
|
-
remap_queries(def.queries.clone()),
|
|
556
|
-
def.output.unwrap_or("{}".to_string()),
|
|
557
|
-
SourceNodeType::Code("DENO".to_string(), def.code, def.is_template.unwrap_or(false)),
|
|
558
|
-
def.output_tables.unwrap_or(vec![])
|
|
559
|
-
);
|
|
560
|
-
self.clean_graph.merge_file(&File { nodes: vec![node.clone()], ..Default::default() })?;
|
|
561
|
-
Ok(NodeHandle::from(node)?)
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
pub fn vector_memory_node(&mut self, arg: VectorMemoryNodeCreateOpts) -> anyhow::Result<NodeHandle> {
|
|
566
|
-
let mut def = VectorMemoryNodeCreateOpts::default();
|
|
567
|
-
def.merge(arg);
|
|
568
|
-
let node = create_vector_memory_node(
|
|
569
|
-
def.name.clone(),
|
|
570
|
-
remap_queries(def.queries.clone()),
|
|
571
|
-
def.output.unwrap_or("{}".to_string()),
|
|
572
|
-
def.action.unwrap_or("READ".to_string()),
|
|
573
|
-
def.embedding_model.unwrap_or("TEXT_EMBEDDING_ADA_002".to_string()),
|
|
574
|
-
def.template.unwrap_or("".to_string()),
|
|
575
|
-
def.db_vendor.unwrap_or("QDRANT".to_string()),
|
|
576
|
-
def.collection_name,
|
|
577
|
-
def.output_tables.unwrap_or(vec![])
|
|
578
|
-
)?;
|
|
579
|
-
self.clean_graph.merge_file(&File { nodes: vec![node.clone()], ..Default::default() })?;
|
|
580
|
-
Ok(NodeHandle::from(node)?)
|
|
581
|
-
}
|
|
582
|
-
//
|
|
583
|
-
//
|
|
584
|
-
// //
|
|
585
|
-
// // fn observation_node(mut self_: PyRefMut<'_, Self>, name: String, query_def: Option<String>, template: String, model: String) -> PyResult<()> {
|
|
586
|
-
// // let file_id = self_.file_id.clone();
|
|
587
|
-
// // let node = create_observation_node(
|
|
588
|
-
// // "".to_string(),
|
|
589
|
-
// // None,
|
|
590
|
-
// // "".to_string(),
|
|
591
|
-
// // );
|
|
592
|
-
// // executor::block_on(self_.client.merge(RequestFileMerge {
|
|
593
|
-
// // id: file_id,
|
|
594
|
-
// // file: Some(File {
|
|
595
|
-
// // nodes: vec![node],
|
|
596
|
-
// // ..Default::default()
|
|
597
|
-
// // }),
|
|
598
|
-
// // branch: 0,
|
|
599
|
-
// // }));
|
|
600
|
-
// // Ok(())
|
|
601
|
-
// // }
|
|
602
|
-
|
|
603
|
-
// // TODO: need to figure out passing a buffer of bytes
|
|
604
|
-
// // TODO: nodes that are added should return a clean definition of what their addition looks like
|
|
605
|
-
// // TODO: adding a node should also display any errors
|
|
606
|
-
// /// x = None
|
|
607
|
-
// /// with open("/Users/coltonpierson/Downloads/files_and_dirs.zip", "rb") as zip_file:
|
|
608
|
-
// /// contents = zip_file.read()
|
|
609
|
-
// /// x = await p.load_zip_file("LoadZip", """ output: String """, contents)
|
|
610
|
-
// /// x
|
|
611
|
-
// // #[pyo3(signature = (name=String::new(), output_tables=vec![], output=String::new(), bytes=vec![]))]
|
|
612
|
-
// // fn load_zip_file<'a>(
|
|
613
|
-
// // mut self_: PyRefMut<'_, Self>,
|
|
614
|
-
// // py: Python<'a>,
|
|
615
|
-
// // name: String,
|
|
616
|
-
// // output_tables: Vec<String>,
|
|
617
|
-
// // output: String,
|
|
618
|
-
// // bytes: Vec<u8>
|
|
619
|
-
// // ) -> PyResult<&'a PyAny> {
|
|
620
|
-
// // let file_id = self_.file_id.clone();
|
|
621
|
-
// // let url = self_.url.clone();
|
|
622
|
-
// // pyo3_asyncio::tokio::future_into_py(py, async move {
|
|
623
|
-
// // let node = create_loader_node(
|
|
624
|
-
// // name,
|
|
625
|
-
// // vec![],
|
|
626
|
-
// // output,
|
|
627
|
-
// // LoadFrom::ZipfileBytes(bytes),
|
|
628
|
-
// // output_tables
|
|
629
|
-
// // );
|
|
630
|
-
// // Ok(push_file_merge(&url, &file_id, node).await?)
|
|
631
|
-
// // })
|
|
632
|
-
// // }
|
|
633
|
-
|
|
634
|
-
pub async fn commit(&self, c: &Chidori, branch: u64) -> anyhow::Result<ExecutionStatus> {
|
|
635
|
-
let url = &c.url;
|
|
636
|
-
let file_id = &c.file_id;
|
|
637
|
-
let mut client = get_client(url.clone()).await?;
|
|
638
|
-
let nodes = self.clean_graph.node_by_name.clone().into_values().collect();
|
|
639
|
-
|
|
640
|
-
Ok(client.merge(RequestFileMerge {
|
|
641
|
-
id: file_id.clone(),
|
|
642
|
-
file: Some(File { nodes, ..Default::default() }),
|
|
643
|
-
branch: 0,
|
|
644
|
-
}).await.map(|x| x.into_inner())?)
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
// Node handle
|
|
650
|
-
#[derive(Clone)]
|
|
651
|
-
pub struct NodeHandle {
|
|
652
|
-
pub node: Item,
|
|
653
|
-
indiv: CleanIndividualNode
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
impl NodeHandle {
|
|
657
|
-
fn from(node: Item) -> anyhow::Result<NodeHandle> {
|
|
658
|
-
let indiv = derive_for_individual_node(&node)?;
|
|
659
|
-
Ok(NodeHandle {
|
|
660
|
-
node,
|
|
661
|
-
indiv
|
|
662
|
-
})
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
impl NodeHandle {
|
|
668
|
-
pub(crate) fn get_name(&self) -> String {
|
|
669
|
-
self.node.core.as_ref().unwrap().name.clone()
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
fn get_output_type(&self) -> Vec<Vec<String>> {
|
|
673
|
-
self.indiv.output_paths.clone()
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
pub fn run_when(&mut self, graph_builder: &mut GraphBuilder, other_node: &NodeHandle) -> anyhow::Result<bool> {
|
|
677
|
-
let queries = &mut self.node.core.as_mut().unwrap().queries;
|
|
678
|
-
|
|
679
|
-
// Remove null query if it is the only one present
|
|
680
|
-
if queries.len() == 1 && queries[0].query.is_none() {
|
|
681
|
-
queries.remove(0);
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
let q = construct_query_from_output_type(
|
|
685
|
-
&other_node.get_name(),
|
|
686
|
-
&other_node.get_name(),
|
|
687
|
-
&other_node.get_output_type()
|
|
688
|
-
).unwrap();
|
|
689
|
-
queries.push(Query { query: Some(q)});
|
|
690
|
-
graph_builder.clean_graph.merge_file(&File { nodes: vec![self.node.clone()], ..Default::default() })?;
|
|
691
|
-
Ok(true)
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
pub async fn query(&self, file_id: String, url: String, branch: u64, frame: u64) -> anyhow::Result<HashMap<String, SerializedValue>> {
|
|
696
|
-
let name = &self.node.core.as_ref().unwrap().name;
|
|
697
|
-
let query = construct_query_from_output_type(&name, &name, &self.indiv.output_paths).unwrap();
|
|
698
|
-
let mut client = get_client(url).await?;
|
|
699
|
-
let result = client.run_query(QueryAtFrame {
|
|
700
|
-
id: file_id,
|
|
701
|
-
query: Some(Query {
|
|
702
|
-
query: Some(query)
|
|
703
|
-
}),
|
|
704
|
-
frame,
|
|
705
|
-
branch,
|
|
706
|
-
}).await?;
|
|
707
|
-
let res = result.into_inner();
|
|
708
|
-
let mut obj = HashMap::new();
|
|
709
|
-
for value in res.values.iter() {
|
|
710
|
-
let c = value.change_value.as_ref().unwrap();
|
|
711
|
-
let k = c.path.as_ref().unwrap().address.join(":");
|
|
712
|
-
let v = c.value.as_ref().unwrap().clone();
|
|
713
|
-
obj.insert(k, v).unwrap();
|
|
714
|
-
}
|
|
715
|
-
Ok(obj)
|
|
716
|
-
}
|
|
717
|
-
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
#[macro_export]
|
|
721
|
-
macro_rules! register_node_handle {
|
|
722
|
-
($c:expr, $name:expr, $handler:expr) => {
|
|
723
|
-
$c.register_custom_node_handle($name.to_string(), Handler::new(
|
|
724
|
-
move |n| Box::pin(async move { ($handler)(n).await })
|
|
725
|
-
));
|
|
726
|
-
};
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
#[cfg(test)]
|
|
731
|
-
mod tests {
|
|
732
|
-
use super::*;
|
|
733
|
-
|
|
734
|
-
#[test]
|
|
735
|
-
fn test_new_graph() {
|
|
736
|
-
}
|
|
737
|
-
}
|