tuitab 0.8.0

Terminal tabular data explorer — CSV/JSON/YAML/TOML/Parquet/Excel/SQLite viewer with filtering, sorting, pivot tables, and charts
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! Changing a database table from the server, in two steps.
//!
//! The terminal guards a write with a popup showing every statement and waiting for a
//! keypress.  A model cannot be shown a popup, so it gets the same guarantee in a shape
//! it can honour: [`plan`] works out exactly what would run and returns it without
//! touching the file, and [`apply`] executes that plan and nothing else.  Neither tool
//! exists unless the human who started the server passed `--mcp-write`.
//!
//! Everything under the handshake is the terminal's own machinery — the frame is loaded
//! with its `TableSource`, mutated with the same `DataFrame` methods a keypress uses,
//! and handed to `build_plan`/`apply`.  There is no second engine and no second set of
//! refusals, so a change the terminal would not make is not one the server can make
//! either.

use super::{render, source, Server};
use crate::data::dataframe::{DataFrame, NULL_INPUT};
use crate::data::io::db_write::{self, TableSource, WritePlan};
use crate::mcp::tools::CallError;
use serde_json::{json, Value};

/// Statements shown back in phase one, and the most a caller may ask for.  The plan
/// holds every value twice over — once bound, once spelled out for reading — so a change
/// over a large table would otherwise answer with megabytes of SQL.  The ceiling is what
/// keeps that true when the caller does the asking.
const DEFAULT_SHOWN: usize = 20;
const MAX_SHOWN: usize = 200;

/// Rows a single call may insert.
const MAX_INSERT: usize = 1000;

/// A plan waiting for its second call.
pub struct Pending {
    pub id: String,
    pub plan: WritePlan,
    pub src: TableSource,
    pub container: String,
}

/// What the caller asked to change.
enum Change {
    Set(serde_json::Map<String, Value>),
    Delete,
    Insert(Vec<Value>),
    Alter(Value),
}

fn one_change(args: &Value) -> Result<Change, CallError> {
    let mut found = Vec::new();
    if args.get("set").is_some() {
        found.push("set");
    }
    if args.get("delete").and_then(Value::as_bool) == Some(true) {
        found.push("delete");
    }
    if args.get("insert").is_some() {
        found.push("insert");
    }
    if args.get("alter").is_some() {
        found.push("alter");
    }
    match found.as_slice() {
        [] => Err(CallError::Failed(
            "Give one of 'set', 'delete', 'insert' or 'alter'.".to_string(),
        )),
        ["set"] => args
            .get("set")
            .and_then(Value::as_object)
            .cloned()
            .map(Change::Set)
            .ok_or_else(|| CallError::Failed("'set' takes an object of column to value".into())),
        ["delete"] => Ok(Change::Delete),
        ["insert"] => args
            .get("insert")
            .and_then(Value::as_array)
            .cloned()
            .map(Change::Insert)
            .ok_or_else(|| CallError::Failed("'insert' takes an array of row objects".into())),
        ["alter"] => Ok(Change::Alter(
            args.get("alter").cloned().unwrap_or_default(),
        )),
        many => Err(CallError::Failed(format!(
            "Give one change per call, not {}.",
            many.join(" and ")
        ))),
    }
}

/// A JSON value as the cell text the frame stores.  `null` is the one that matters:
/// it becomes the sentinel `set_cell` already reads as SQL NULL.
fn cell_text(v: &Value) -> String {
    match v {
        Value::Null => NULL_INPUT.to_string(),
        Value::String(s) => s.clone(),
        other => other.to_string(),
    }
}

fn column_index(df: &DataFrame, name: &str) -> Result<usize, CallError> {
    df.column_index(name).map_err(CallError::Failed)
}

/// Work out what a change would do, and say so without doing it.
pub fn plan(server: &mut Server, args: &Value) -> Result<Value, CallError> {
    // Taken first, and unconditionally: a second call means the model has moved on, and
    // leaving the old plan applicable through a *failed* second call is how
    // `tuitab_write_apply` ends up running something nobody is looking at any more.
    let displaced = server.pending.take().map(|p| p.id);
    let src_arg = super::tools::source_arg(args)?;
    if !db_write::is_db_ext(&src_arg.path) {
        return Err(CallError::Failed(
            "tuitab_write changes a table in a database. Give a .sqlite or .duckdb source."
                .to_string(),
        ));
    }
    let Some(container) = src_arg.container.clone() else {
        return Err(CallError::Failed(
            "Give the table to change as 'container'. tuitab_inspect lists them.".to_string(),
        ));
    };

    // Loaded fresh, never from the cache: the cache holds no `TableSource`, and a
    // snapshot reused across two writes in one session would trip the drift check on
    // the model's own first write.
    let (mut df, source) = source::load_db_table(&src_arg.path, &container)?;
    let Some(source) = source else {
        return Err(CallError::Failed(format!(
            "'{}' is a view, or has no row identity of its own, so it cannot be written to. \
             Views can be read but not changed.",
            container
        )));
    };

    let change = one_change(args)?;
    let clauses = super::tools::where_arg(args)?;
    // Emptying a table is not something a missing argument should be able to ask for,
    // and unlike a replaced table it does not announce itself in the plan as a DROP.
    if matches!(change, Change::Delete) && clauses.is_empty() {
        return Err(CallError::Failed(
            "'delete' needs a 'where'. Deleting every row is not something this tool will \
             plan for you."
                .to_string(),
        ));
    }
    let matched = if clauses.is_empty() {
        (0..df.df.height()).collect::<Vec<_>>()
    } else {
        crate::data::filter::matching_rows(&df, &clauses).map_err(CallError::Failed)?
    };

    // The rows as they stand, captured before anything is changed.  A model reading
    // these can see a mis-aimed 'where' for what it is; rowids in the SQL cannot be
    // checked against anything.
    let preview_rows = args
        .get("preview_rows")
        .and_then(Value::as_u64)
        .unwrap_or(10)
        .min(100) as usize;
    let mut before = df.clone();
    before.row_order = std::sync::Arc::new(matched.clone());
    let affected = render::table(&before, preview_rows).map_err(CallError::Failed)?;

    let wanted_schema = matches!(change, Change::Alter(_));
    apply_change(&mut df, &source, change, &matched)?;

    // Verbatim, here and in `apply`: these are tuitab's own refusals, written to be
    // read by a person, and wrapping them would bury the sentence that matters.
    let plan = db_write::build_plan(&source, &df).map_err(|e| CallError::Failed(e.to_string()))?;
    if !wanted_schema && plan.schema > 0 {
        return Err(CallError::Failed(
            "That change would alter the table's shape, which was not what was asked for."
                .to_string(),
        ));
    }
    if plan.is_empty() {
        return Ok(json!({
            "summary": "no change",
            "rows_matched": matched.len(),
            "statements": [],
            "warnings": plan.warnings,
            "note": note_with_displaced(
                "Nothing would change, so there is no plan to apply.",
                displaced.as_deref(),
            ),
        }));
    }

    server.plan_seq += 1;
    let id = format!("write-{}", server.plan_seq);
    let show = args
        .get("show_statements")
        .and_then(Value::as_u64)
        .unwrap_or(DEFAULT_SHOWN as u64)
        .clamp(1, MAX_SHOWN as u64) as usize;
    let shown: Vec<&str> = plan
        .stmts
        .iter()
        .take(show)
        // Past `DISPLAY_CAP` the plan keeps no readable text at all, so an empty one is
        // a statement that exists and cannot be shown, not a statement that is blank.
        .filter(|s| !s.display.is_empty())
        .map(|s| s.display.as_str())
        .collect();
    let out = json!({
        "plan_id": id,
        "summary": plan.summary(),
        "schema": plan.schema,
        "updates": plan.updates,
        "inserts": plan.inserts,
        "deletes": plan.deletes,
        "rows_matched": matched.len(),
        "statements": shown,
        "statements_total": plan.stmts.len(),
        "statements_not_shown": plan.stmts.len().saturating_sub(shown.len()),
        "warnings": plan.warnings,
        "affected_rows": affected,
        "note": note_with_displaced(
            &format!(
                "Nothing has been written. Read these statements back to the user, then \
                 call tuitab_write_apply with plan_id '{}' to run exactly them.",
                id
            ),
            displaced.as_deref(),
        ),
    });
    server.pending = Some(Pending {
        id,
        plan,
        src: source,
        container,
    });
    Ok(out)
}

/// Say that an earlier plan is gone, when there was one.
///
/// A model that planned twice has to know the first plan is no longer applicable, or it
/// will read a refusal from `tuitab_write_apply` as a bug rather than as the answer.
fn note_with_displaced(note: &str, displaced: Option<&str>) -> String {
    match displaced {
        Some(id) => format!(
            "{} Plan '{}' was replaced and can no longer be applied.",
            note, id
        ),
        None => note.to_string(),
    }
}

/// Turn the requested change into frame mutations, which `build_plan` then reads.
fn apply_change(
    df: &mut DataFrame,
    src: &TableSource,
    change: Change,
    matched: &[usize],
) -> Result<(), CallError> {
    match change {
        Change::Set(values) => {
            if values.is_empty() {
                return Err(CallError::Failed("'set' needs at least one column".into()));
            }
            let rows: std::collections::HashSet<usize> = matched.iter().copied().collect();
            for (name, value) in values {
                let col = column_index(df, &name)?;
                df.set_cells_bulk(&rows, col, cell_text(&value))
                    .map_err(CallError::Failed)?;
            }
        }
        Change::Delete => df.record_deleted_rows(matched.to_vec()),
        Change::Insert(rows) => {
            if rows.is_empty() {
                return Err(CallError::Failed("'insert' needs at least one row".into()));
            }
            if rows.len() > MAX_INSERT {
                return Err(CallError::Failed(format!(
                    "'insert' takes at most {} rows per call; you gave {}.",
                    MAX_INSERT,
                    rows.len()
                )));
            }
            insert_rows(df, src, &rows)?;
        }
        Change::Alter(spec) => alter(df, &spec)?,
    }
    Ok(())
}

/// Append the given rows in one go.
///
/// Building the whole block and stacking it once, rather than a row at a time through
/// `insert_empty_row`, because the latter rebuilds every column per call.
fn insert_rows(df: &mut DataFrame, src: &TableSource, rows: &[Value]) -> Result<(), CallError> {
    use polars::prelude::{Column, NamedFrom, Series};

    // A column the table insists on and does not default would fail inside the
    // transaction; naming it here is a sentence instead of an engine error.
    for col in &src.columns {
        if col.notnull && col.default_sql.is_none() && !col.generated {
            let missing = rows
                .iter()
                .any(|r| r.get(&col.name).map(|v| v.is_null()).unwrap_or(true));
            if missing {
                return Err(CallError::Failed(format!(
                    "Column '{}' is NOT NULL and has no default, so every new row must give \
                     it a value.",
                    col.name
                )));
            }
        }
    }

    let mut series_vec: Vec<Column> = Vec::with_capacity(df.columns.len());
    for (i, meta) in df.columns.iter().enumerate() {
        let values: Vec<Option<String>> = rows
            .iter()
            .map(|r| match r.get(&meta.name) {
                // A column left out is NULL, not the column's DEFAULT: the INSERT names
                // every column, so a default never gets the chance to apply.
                None | Some(Value::Null) => None,
                Some(v) => Some(cell_text(v)),
            })
            .collect();
        let series = Series::new(meta.name.as_str().into(), values);
        let target = df.df.columns()[i].dtype();
        let series = series.strict_cast(target).map_err(|_| {
            CallError::Failed(format!(
                "Column '{}' holds {}, and one of the values given is not",
                meta.name, target
            ))
        })?;
        series_vec.push(series.into());
    }

    let block = polars::prelude::DataFrame::new(rows.len(), series_vec)
        .map_err(|e| CallError::Failed(format!("Could not build the rows to insert: {}", e)))?;
    let first = df.df.height();
    df.df
        .vstack_mut(&block)
        .map_err(|e| CallError::Failed(format!("Could not add the rows to the table: {}", e)))?;
    for i in 0..rows.len() {
        std::sync::Arc::make_mut(&mut df.row_order).push(first + i);
        std::sync::Arc::make_mut(&mut df.original_order).push(first + i);
    }
    df.record_added_rows(rows.len());
    df.modified = true;
    Ok(())
}

/// Add, drop and rename columns, through the same frame operations the terminal uses.
fn alter(df: &mut DataFrame, spec: &Value) -> Result<(), CallError> {
    use crate::types::ColumnType;

    // Anything not read below would leave the plan empty and the answer "no change",
    // which reads as "the table already looks like that" rather than "that word means
    // nothing here".  A typo has to fail loudly or it fails silently.
    let Some(keys) = spec.as_object() else {
        return Err(CallError::Failed(
            "'alter' takes an object with 'add', 'drop' or 'rename'.".to_string(),
        ));
    };
    if keys.is_empty() {
        return Err(CallError::Failed(
            "'alter' needs one of 'add', 'drop' or 'rename'.".to_string(),
        ));
    }
    for key in keys.keys() {
        if !matches!(key.as_str(), "add" | "drop" | "rename") {
            return Err(CallError::Failed(format!(
                "'{}' is not something alter does. It takes 'add', 'drop' and 'rename'; \
                 changing an existing column's type and reordering columns are only in \
                 the terminal.",
                key
            )));
        }
    }

    if let Some(renames) = spec.get("rename").and_then(Value::as_object) {
        for (from, to) in renames {
            let col = column_index(df, from)?;
            let to = to
                .as_str()
                .ok_or_else(|| CallError::Failed("'rename' maps old name to new name".into()))?;
            df.rename_column(col, to).map_err(CallError::Failed)?;
        }
    }
    if let Some(drops) = spec.get("drop").and_then(Value::as_array) {
        for name in drops {
            let name = name
                .as_str()
                .ok_or_else(|| CallError::Failed("'drop' takes column names".into()))?;
            let col = column_index(df, name)?;
            df.drop_column(col).map_err(CallError::Failed)?;
        }
    }
    if let Some(adds) = spec.get("add").and_then(Value::as_array) {
        for add in adds {
            let name = add
                .get("name")
                .and_then(Value::as_str)
                .ok_or_else(|| CallError::Failed("every 'add' needs a 'name'".into()))?;
            let type_name = add
                .get("type")
                .and_then(Value::as_str)
                .unwrap_or("string")
                .to_ascii_lowercase();
            let col_type = *ColumnType::all()
                .iter()
                .find(|t| t.name() == type_name)
                .ok_or_else(|| {
                    CallError::Failed(format!(
                        "'{}' is not a column type. Use string, integer, float, boolean, date \
                         or datetime.",
                        type_name
                    ))
                })?;
            // Percentage, currency and file size are ways of *showing* a number — a
            // percentage is stored divided by 100 — so they are not types a table can
            // be given.
            if matches!(
                col_type,
                ColumnType::Percentage | ColumnType::Currency | ColumnType::FileSize
            ) {
                return Err(CallError::Failed(format!(
                    "'{}' is a display format, not a storage type.",
                    type_name
                )));
            }
            let at = df.col_count();
            df.insert_empty_column(at, name)
                .map_err(CallError::Failed)?;
            if col_type != ColumnType::String {
                // `insert_empty_column` fills with empty strings, which is right for a
                // column someone is about to type into and wrong for one the database
                // is about to create: a new column holds nothing, and nothing is NULL.
                let col = column_index(df, name)?;
                let dtype = polars_dtype(col_type);
                let nulls = polars::prelude::Series::full_null(name.into(), df.df.height(), &dtype);
                df.df.with_column(nulls.into()).map_err(|e| {
                    CallError::Failed(format!("Could not add the column '{}': {}", name, e))
                })?;
                df.columns[col].col_type = col_type;
            }
        }
    }
    Ok(())
}

/// The polars dtype behind a column type, for a column being created empty.
fn polars_dtype(t: crate::types::ColumnType) -> polars::prelude::DataType {
    use crate::types::ColumnType as C;
    use polars::prelude::DataType as D;
    match t {
        C::Integer | C::FileSize => D::Int64,
        C::Float | C::Percentage | C::Currency => D::Float64,
        C::Boolean => D::Boolean,
        _ => D::String,
    }
}

/// Run a plan made by [`plan`], and nothing else.
pub fn apply(server: &mut Server, args: &Value) -> Result<Value, CallError> {
    let wanted = args
        .get("plan_id")
        .and_then(Value::as_str)
        .ok_or_else(|| CallError::Failed("tuitab_write_apply needs a 'plan_id'".to_string()))?;

    // Taken before the write: a plan that failed is invalid by definition — after a
    // drift error the data it was made against is gone — so it must not be retryable.
    let pending = match server.pending.take() {
        Some(p) if p.id == wanted => p,
        Some(other) => {
            let id = other.id.clone();
            server.pending = Some(other);
            return Err(CallError::Failed(format!(
                "No plan '{}'. The plan waiting to be applied is '{}'.",
                wanted, id
            )));
        }
        None => {
            return Err(CallError::Failed(format!(
                "No plan '{}' is waiting. Call tuitab_write to make one.",
                wanted
            )))
        }
    };

    db_write::apply(&pending.src, &pending.plan).map_err(|e| CallError::Failed(e.to_string()))?;
    // The cached frame predates the write; leaving it would let the next inspect answer
    // with values that are no longer there.
    server.cache.clear();

    Ok(json!({
        "applied": true,
        "path": pending.src.db_path.to_string_lossy(),
        "container": pending.container,
        "summary": pending.plan.summary(),
        "schema": pending.plan.schema,
        "updates": pending.plan.updates,
        "inserts": pending.plan.inserts,
        "deletes": pending.plan.deletes,
    }))
}