Skip to main content

ai_agent/utils/
notebook.rs

1// Source: /data/home/swei/claudecode/openclaudecode/src/types/notebook.ts
2//! Jupyter notebook utilities.
3
4use serde::{Deserialize, Serialize};
5
6/// A Jupyter notebook cell
7#[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/// A Jupyter notebook
17#[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/// Notebook metadata
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct NotebookMetadata {
28    pub kernelspec: Option<KernelSpec>,
29    pub language_info: Option<LanguageInfo>,
30}
31
32/// Kernel specification
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct KernelSpec {
35    pub name: String,
36    pub display_name: String,
37}
38
39/// Language info
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct LanguageInfo {
42    pub name: String,
43    pub version: Option<String>,
44}
45
46/// Check if a file is a Jupyter notebook
47pub fn is_notebook_file(path: &str) -> bool {
48    path.ends_with(".ipynb")
49}
50
51/// Parse a notebook from JSON
52pub fn parse_notebook(json: &str) -> Result<Notebook, serde_json::Error> {
53    serde_json::from_str(json)
54}
55
56/// Extract code from notebook cells
57pub 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}