ai_agent/utils/
notebook.rs1use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct NotebookCell {
9 pub cell_type: String,
10 pub source: Vec<String>,
11 pub metadata: serde_json::Value,
12 pub outputs: Option<Vec<serde_json::Value>>,
13 pub execution_count: Option<u32>,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct Notebook {
19 pub nbformat: u32,
20 pub nbformat_minor: u32,
21 pub metadata: NotebookMetadata,
22 pub cells: Vec<NotebookCell>,
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct NotebookMetadata {
28 pub kernelspec: Option<KernelSpec>,
29 pub language_info: Option<LanguageInfo>,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct KernelSpec {
35 pub name: String,
36 pub display_name: String,
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct LanguageInfo {
42 pub name: String,
43 pub version: Option<String>,
44}
45
46pub fn is_notebook_file(path: &str) -> bool {
48 path.ends_with(".ipynb")
49}
50
51pub fn parse_notebook(json: &str) -> Result<Notebook, serde_json::Error> {
53 serde_json::from_str(json)
54}
55
56pub fn extract_code_cells(notebook: &Notebook) -> Vec<String> {
58 notebook
59 .cells
60 .iter()
61 .filter(|c| c.cell_type == "code")
62 .map(|c| c.source.join(""))
63 .collect()
64}