nb-fabric 0.6.0

CLI for managing Microsoft Fabric notebooks; create, edit cells, execute interactively, schedule, and query OneLake data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// #region Imports
use anyhow::{Context, Result};
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use reqwest::Client;
use std::io::Read as _;

use crate::auth;
use crate::client;
use crate::spinner::{Spinner, DIM, GREEN, RED, RESET, ERASE};
use super::cells::cell_source_string;
// #endregion

// #region Variables
const LIVY_API_VERSION: &str = "2023-12-01";
// #endregion


/// Handle `nb exec code <workspace/lakehouse> <code>` command.
/// Ephemeral execution: creates a session, runs code, prints output, cleans up.
/// No notebook artifact required. Pass `-` as code to read from stdin.
pub async fn run_exec_quick(
    http: &Client,
    reference: &str,
    code: &str,
) -> Result<()> {
    let (ws_name, lh_name) = client::parse_ref(reference)
        .context("Expected format: \"Workspace/Lakehouse\" or \"Workspace.Workspace/LH.Lakehouse\"\n  nb exec code \"MyWS/MyLH.Lakehouse\" \"print('hello')\"")?;

    // Strip .Lakehouse suffix if present
    let lh_clean = lh_name.trim_end_matches(".Lakehouse");

    // Read code from stdin if "-"
    let exec_code = if code == "-" {
        let mut buf = String::new();
        std::io::stdin().read_to_string(&mut buf)
            .context("Failed to read code from stdin")?;
        buf
    } else {
        code.to_string()
    };

    if exec_code.trim().is_empty() {
        anyhow::bail!("No code to execute.\n  nb exec code \"Workspace/Lakehouse\" \"print('hello')\"");
    }

    let ws_id = client::resolve_workspace(http, ws_name).await?;
    let lh = client::resolve_item(http, &ws_id, lh_clean, "Lakehouse").await?;
    let session_name = format!("nb-cli-{}", lh_clean);

    run_in_session(http, &ws_id, &lh.id, "pyspark", &session_name, &exec_code).await
}


/// Handle `nb exec cell <workspace/notebook> <index>` command.
/// Executes a notebook cell in a session against the notebook's attached lakehouse.
/// Returns the cell output.
pub async fn run_exec(
    http: &Client,
    reference: &str,
    code: Option<&str>,
    cell_index: Option<usize>,
    lakehouse: Option<&str>,
) -> Result<()> {
    let (ws_name, nb_name) = client::parse_ref(reference)?;
    let ws_id = client::resolve_workspace(http, ws_name).await?;
    let nb = client::resolve_item(http, &ws_id, nb_name, "Notebook").await?;

    // Resolve the code to execute
    let exec_code = match (code, cell_index) {
        (Some(c), _) => c.to_string(),
        (None, Some(idx)) => {
            let def = client::get_definition(http, &ws_id, &nb.id, "ipynb").await?;
            let parts = def.definition.context("No definition returned")?.parts;
            let part = parts
                .iter()
                .find(|p| p.path.contains("notebook-content") || p.path.ends_with(".ipynb"))
                .or_else(|| parts.first())
                .context("No parts in definition")?;

            let decoded = BASE64
                .decode(&part.payload)
                .context("Failed to decode base64 payload")?;
            let ipynb: serde_json::Value =
                serde_json::from_slice(&decoded).context("Failed to parse ipynb JSON")?;

            let cells = ipynb
                .get("cells")
                .and_then(|c| c.as_array())
                .context("No cells array")?;

            let cell = cells
                .get(idx)
                .context(format!("Cell index {} out of range", idx))?;

            cell_source_string(cell)
        }
        (None, None) => anyhow::bail!(
            "Provide --code or a cell index.\n  nb exec cell \"Workspace/Notebook\" 0"
        ),
    };

    // Resolve lakehouse ID (from --lakehouse flag, or detect from notebook metadata)
    let lh_id = match lakehouse {
        Some(lh_name) => {
            let lh = client::resolve_item(http, &ws_id, lh_name, "Lakehouse").await?;
            lh.id
        }
        None => {
            detect_lakehouse(http, &ws_id, &nb.id).await
                .context("No lakehouse attached to notebook. Use --lakehouse <name> to specify one.")?
        }
    };

    // Detect kernel type
    let kernel = detect_kernel(http, &ws_id, &nb.id).await.unwrap_or_else(|_| "jupyter".to_string());
    let kind = if kernel == "synapse_pyspark" { "pyspark" } else { "python" };
    let session_name = format!("nb-cli-{}", nb.display_name);

    run_in_session(http, &ws_id, &lh_id, kind, &session_name, &exec_code).await
}


/// Shared session lifecycle: create session, wait, submit code, poll result, print, cleanup.
/// Always cleans up the session, even on errors after creation.
/// Returns a non-zero exit (Err) when the submitted code itself fails.
async fn run_in_session(
    http: &Client,
    ws_id: &str,
    lh_id: &str,
    kind: &str,
    session_name: &str,
    code: &str,
) -> Result<()> {
    let start = std::time::Instant::now();

    let sp = Spinner::start("Creating session...");

    // Create session
    let token = auth::get_fabric_token()?;
    let session_url = format!(
        "https://api.fabric.microsoft.com/v1/workspaces/{}/lakehouses/{}/livyApi/versions/{}/sessions",
        ws_id, lh_id, LIVY_API_VERSION
    );

    let session_body = serde_json::json!({
        "kind": kind,
        "name": session_name
    });

    let resp = http
        .post(&session_url)
        .bearer_auth(&token)
        .json(&session_body)
        .send()
        .await
        .context("Failed to create session")?;

    let status = resp.status();
    if !status.is_success() {
        let body = resp.text().await.unwrap_or_default();
        sp.finish("Failed to create session", false).await;
        anyhow::bail!("Failed to create session ({}): {}", status, body);
    }

    let session: serde_json::Value = resp.json().await?;
    let session_id = session
        .get("id")
        .map(|v| match v {
            serde_json::Value::String(s) => s.clone(),
            serde_json::Value::Number(n) => n.to_string(),
            _ => String::new(),
        })
        .filter(|s| !s.is_empty())
        .context("No session ID returned")?;

    let short_id = session_id[..8].to_string();

    // From here on, always clean up the session -- even on Ctrl+C
    let result = tokio::select! {
        res = execute_and_print(http, ws_id, lh_id, &session_id, kind, code, sp) => res,
        _ = tokio::signal::ctrl_c() => {
            eprintln!("{ERASE}  {RED}!!{RESET} {DIM}Interrupted{RESET}");
            Err(anyhow::anyhow!("Interrupted"))
        }
    };

    // Always clean up
    eprintln!();
    let _ = cleanup_session(http, ws_id, lh_id, &session_id).await;

    let elapsed = start.elapsed();
    let ok = result.is_ok();
    let status_str = if ok { format!("{GREEN}\u{2714}{RESET}") } else { format!("{RED}\u{2718}{RESET}") };
    eprintln!("  {DIM}---{RESET}");
    eprintln!("  {DIM}session{RESET}  {short_id}  {DIM}status{RESET}  {status_str}  {DIM}duration{RESET}  {:.1}s", elapsed.as_secs_f64());

    result
}


/// Wait for session, submit code, poll result, and print output.
/// Separated from run_in_session so cleanup always runs via the caller.
async fn execute_and_print(
    http: &Client,
    ws_id: &str,
    lh_id: &str,
    session_id: &str,
    kind: &str,
    code: &str,
    sp: Spinner,
) -> Result<()> {
    // Wait for session to be ready
    wait_for_session_idle(http, ws_id, lh_id, session_id, &sp).await?;

    sp.set_message("Submitting code...");

    // Submit statement
    let stmt_url = format!(
        "https://api.fabric.microsoft.com/v1/workspaces/{}/lakehouses/{}/livyApi/versions/{}/sessions/{}/statements",
        ws_id, lh_id, LIVY_API_VERSION, session_id
    );

    let stmt_body = serde_json::json!({
        "code": code,
        "kind": kind
    });

    let token = auth::get_fabric_token()?;
    let stmt_resp = http
        .post(&stmt_url)
        .bearer_auth(&token)
        .json(&stmt_body)
        .send()
        .await
        .context("Failed to submit statement")?;

    let stmt: serde_json::Value = stmt_resp.json().await?;
    let stmt_id = stmt
        .get("id")
        .and_then(|v| v.as_u64())
        .context("No statement ID returned")?;

    // Poll for statement result
    let result = poll_statement(http, ws_id, lh_id, session_id, stmt_id, &sp).await?;

    // Print output
    let output_status = result
        .pointer("/output/status")
        .and_then(|v| v.as_str())
        .unwrap_or("unknown");

    if output_status == "ok" {
        sp.finish("Done", true).await;
        eprintln!();
        if let Some(text) = result.pointer("/output/data/text~1plain").and_then(|v| v.as_str()) {
            println!("{}", text);
        }
        Ok(())
    } else {
        let ename = result.pointer("/output/ename").and_then(|v| v.as_str()).unwrap_or("");
        let evalue = result.pointer("/output/evalue").and_then(|v| v.as_str()).unwrap_or("");
        sp.finish(&format!("{ename}: {evalue}"), false).await;
        if let Some(traceback) = result.pointer("/output/traceback").and_then(|v| v.as_array()) {
            for line in traceback {
                if let Some(s) = line.as_str() {
                    eprintln!("  {DIM}{s}{RESET}");
                }
            }
        }
        anyhow::bail!("Code execution failed: {} {}", ename, evalue)
    }
}


/// Clean up a session by sending a DELETE request.
/// Errors are silently ignored; this is best-effort cleanup.
async fn cleanup_session(
    http: &Client,
    ws_id: &str,
    lh_id: &str,
    session_id: &str,
) -> Result<()> {
    let token = auth::get_fabric_token()?;
    let url = format!(
        "https://api.fabric.microsoft.com/v1/workspaces/{}/lakehouses/{}/livyApi/versions/{}/sessions/{}",
        ws_id, lh_id, LIVY_API_VERSION, session_id
    );
    let _ = http.delete(&url).bearer_auth(&token).send().await;
    eprintln!("  {GREEN}\u{2714}{RESET} {DIM}Session cleaned up{RESET}");
    Ok(())
}


/// Wait for a session to reach 'idle' state. Updates the spinner message.
async fn wait_for_session_idle(
    http: &Client,
    ws_id: &str,
    lh_id: &str,
    session_id: &str,
    sp: &Spinner,
) -> Result<()> {
    let check_url = format!(
        "https://api.fabric.microsoft.com/v1/workspaces/{}/lakehouses/{}/livyApi/versions/{}/sessions/{}",
        ws_id, lh_id, LIVY_API_VERSION, session_id
    );

    for _ in 0..120 {
        let token = auth::get_fabric_token()?;
        let resp = http
            .get(&check_url)
            .bearer_auth(&token)
            .send()
            .await?;

        let session: serde_json::Value = resp.json().await?;
        let state = session.get("state").and_then(|v| v.as_str()).unwrap_or("unknown");

        match state {
            "idle" => return Ok(()),
            "dead" | "error" | "killed" => {
                anyhow::bail!("Session entered {} state", state);
            }
            _ => {
                sp.set_message("Starting session...");
                tokio::time::sleep(std::time::Duration::from_secs(5)).await;
            }
        }
    }

    anyhow::bail!("Session did not become idle within 10 minutes")
}


/// Poll a statement until it's available. Updates the spinner message.
async fn poll_statement(
    http: &Client,
    ws_id: &str,
    lh_id: &str,
    session_id: &str,
    stmt_id: u64,
    sp: &Spinner,
) -> Result<serde_json::Value> {
    let stmt_url = format!(
        "https://api.fabric.microsoft.com/v1/workspaces/{}/lakehouses/{}/livyApi/versions/{}/sessions/{}/statements/{}",
        ws_id, lh_id, LIVY_API_VERSION, session_id, stmt_id
    );

    for _ in 0..120 {
        let token = auth::get_fabric_token()?;
        let resp = http
            .get(&stmt_url)
            .bearer_auth(&token)
            .send()
            .await?;

        let result: serde_json::Value = resp.json().await?;
        let state = result.get("state").and_then(|v| v.as_str()).unwrap_or("unknown");

        match state {
            "available" => return Ok(result),
            "waiting" | "running" => {
                sp.set_message("Running...");
                tokio::time::sleep(std::time::Duration::from_secs(2)).await;
            }
            "error" | "cancelled" | "cancelling" => {
                return Ok(result);
            }
            _ => {
                sp.set_message("Running...");
                tokio::time::sleep(std::time::Duration::from_secs(2)).await;
            }
        }
    }

    anyhow::bail!("Statement did not complete within timeout")
}


/// Detect kernel name from notebook metadata.
async fn detect_kernel(http: &Client, ws_id: &str, item_id: &str) -> Result<String> {
    let def = client::get_definition(http, ws_id, item_id, "ipynb").await?;
    let parts = def.definition.context("No definition")?.parts;
    let part = parts
        .iter()
        .find(|p| p.path.contains("notebook-content") || p.path.ends_with(".ipynb"))
        .or_else(|| parts.first())
        .context("No parts")?;

    let decoded = BASE64.decode(&part.payload)?;
    let ipynb: serde_json::Value = serde_json::from_slice(&decoded)?;

    let kernel = ipynb
        .pointer("/metadata/kernel_info/name")
        .or_else(|| ipynb.pointer("/metadata/kernelspec/name"))
        .and_then(|v| v.as_str())
        .unwrap_or("jupyter")
        .to_string();

    Ok(kernel)
}


/// Detect the default lakehouse ID from notebook metadata.
async fn detect_lakehouse(http: &Client, ws_id: &str, item_id: &str) -> Result<String> {
    let def = client::get_definition(http, ws_id, item_id, "ipynb").await?;
    let parts = def.definition.context("No definition")?.parts;
    let part = parts
        .iter()
        .find(|p| p.path.contains("notebook-content") || p.path.ends_with(".ipynb"))
        .or_else(|| parts.first())
        .context("No parts")?;

    let decoded = BASE64.decode(&part.payload)?;
    let ipynb: serde_json::Value = serde_json::from_slice(&decoded)?;

    ipynb
        .pointer("/metadata/dependencies/lakehouse/default_lakehouse")
        .and_then(|v| v.as_str())
        .filter(|s| !s.is_empty())
        .map(|s| s.to_string())
        .context("No default_lakehouse in notebook metadata")
}


// #endregion