@perspective-dev/viewer 5.5.1 → 5.6.1

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.
Files changed (206) hide show
  1. package/README.md +82 -3
  2. package/dist/cdn/perspective-viewer.js +9 -9
  3. package/dist/cdn/perspective-viewer.js.map +4 -4
  4. package/dist/css/blueprint.css +1 -1
  5. package/dist/css/botanical.css +1 -1
  6. package/dist/css/dracula.css +1 -1
  7. package/dist/css/eggplant.css +1 -1
  8. package/dist/css/gruvbox-dark.css +1 -1
  9. package/dist/css/gruvbox.css +1 -1
  10. package/dist/css/icons.css +1 -1
  11. package/dist/css/intl/ar.css +1 -1
  12. package/dist/css/intl/de.css +1 -1
  13. package/dist/css/intl/es.css +1 -1
  14. package/dist/css/intl/fr.css +1 -1
  15. package/dist/css/intl/ja.css +1 -1
  16. package/dist/css/intl/pt.css +1 -1
  17. package/dist/css/intl/zh.css +1 -1
  18. package/dist/css/intl.css +1 -1
  19. package/dist/css/ledger.css +1 -1
  20. package/dist/css/monokai.css +1 -1
  21. package/dist/css/nord.css +1 -1
  22. package/dist/css/phosphor.css +1 -1
  23. package/dist/css/pro-dark.css +1 -1
  24. package/dist/css/pro.css +1 -1
  25. package/dist/css/solarized-dark.css +1 -1
  26. package/dist/css/solarized.css +1 -1
  27. package/dist/css/themes.css +1 -1
  28. package/dist/css/vaporwave.css +1 -1
  29. package/dist/css/velvet.css +1 -1
  30. package/dist/docs/perspective-docs.json +1 -1
  31. package/dist/esm/column-format.js +2 -0
  32. package/dist/esm/column-format.js.map +7 -0
  33. package/dist/esm/extensions.d.ts +3 -13
  34. package/dist/esm/perspective-viewer.d.ts +1 -1
  35. package/dist/esm/perspective-viewer.js +9 -9
  36. package/dist/esm/perspective-viewer.js.map +4 -4
  37. package/dist/esm/plugin.d.ts +4 -1
  38. package/dist/esm/select-detail.d.ts +15 -0
  39. package/dist/esm/select-detail.js +2 -0
  40. package/dist/esm/select-detail.js.map +7 -0
  41. package/dist/esm/ts-rs/DerivedTableConfig.d.ts +35 -0
  42. package/dist/esm/ts-rs/Layout.d.ts +2 -2
  43. package/dist/esm/ts-rs/Orientation.d.ts +2 -1
  44. package/dist/esm/ts-rs/PanelViewerConfig.d.ts +7 -6
  45. package/dist/esm/ts-rs/RestoreOptions.d.ts +3 -7
  46. package/dist/esm/ts-rs/SplitLayout.d.ts +13 -0
  47. package/dist/esm/ts-rs/TabLayout.d.ts +3 -0
  48. package/dist/esm/ts-rs/TableRef.d.ts +9 -0
  49. package/dist/esm/ts-rs/ViewerConfig.d.ts +7 -6
  50. package/dist/esm/ts-rs/ViewerConfigUpdate.d.ts +7 -5
  51. package/dist/esm/ts-rs/WorkspaceConfig.d.ts +9 -1
  52. package/dist/esm/ts-rs/WorkspaceConfigUpdate.d.ts +11 -1
  53. package/dist/wasm/perspective-viewer.d.ts +115 -35
  54. package/dist/wasm/perspective-viewer.js +312 -188
  55. package/dist/wasm/perspective-viewer.wasm +0 -0
  56. package/dist/wasm/perspective-viewer.wasm.d.ts +21 -17
  57. package/package.json +32 -19
  58. package/src/css/column-selector.css +205 -50
  59. package/src/css/ui/containers/context-menu.css +12 -1
  60. package/src/css/ui/containers/dropdown-menu-items.css +29 -0
  61. package/src/css/ui/containers/scroll-panel.css +4 -1
  62. package/src/rust/agent/client/protocol.rs +0 -89
  63. package/src/rust/agent/markdown/mod.rs +0 -238
  64. package/src/rust/agent/preamble.md +3 -0
  65. package/src/rust/agent/tools.rs +282 -17
  66. package/src/rust/components/column_selector/active_column.rs +15 -5
  67. package/src/rust/components/column_selector/add_expression_button.rs +9 -26
  68. package/src/rust/components/column_selector/config_selector.rs +35 -7
  69. package/src/rust/components/column_selector/derived_control.rs +229 -0
  70. package/src/rust/components/column_selector/expr_edit_button.rs +3 -2
  71. package/src/rust/components/column_selector/filter_column.rs +2 -4
  72. package/src/rust/components/column_selector/inactive_column.rs +4 -5
  73. package/src/rust/components/column_selector/pivot_column.rs +21 -1
  74. package/src/rust/components/column_selector/sort_column.rs +4 -3
  75. package/src/rust/components/column_selector.rs +21 -1
  76. package/src/rust/components/column_settings_sidebar/save_settings.rs +5 -3
  77. package/src/rust/components/column_settings_sidebar/style_tab/symbol/row_selector.rs +1 -1
  78. package/src/rust/components/column_settings_sidebar/style_tab.rs +1 -0
  79. package/src/rust/components/column_settings_sidebar.rs +25 -19
  80. package/src/rust/components/debug_panel.rs +38 -29
  81. package/src/rust/components/dragdrop_list.rs +8 -9
  82. package/src/rust/components/empty_row.rs +3 -3
  83. package/src/rust/components/filter_dropdown.rs +100 -101
  84. package/src/rust/components/main_panel/msg.rs +6 -4
  85. package/src/rust/components/main_panel/reconcile.rs +67 -25
  86. package/src/rust/components/main_panel/render.rs +70 -51
  87. package/src/rust/components/main_panel/update.rs +20 -21
  88. package/src/rust/components/main_panel.rs +87 -60
  89. package/src/rust/components/new_panel_menu.rs +69 -1
  90. package/src/rust/components/panel_menu.rs +63 -12
  91. package/src/rust/components/plugin_tab.rs +3 -2
  92. package/src/rust/components/render_warning.rs +2 -1
  93. package/src/rust/components/settings_panel.rs +4 -4
  94. package/src/rust/components/status_bar.rs +74 -24
  95. package/src/rust/components/status_indicator.rs +8 -1
  96. package/src/rust/components/viewer/filters.rs +13 -1
  97. package/src/rust/components/viewer/msg.rs +4 -0
  98. package/src/rust/components/viewer/panels.rs +41 -1
  99. package/src/rust/components/viewer/render.rs +1 -0
  100. package/src/rust/components/viewer/settings.rs +0 -22
  101. package/src/rust/components/viewer/snapshots.rs +2 -2
  102. package/src/rust/components/viewer/wiring.rs +14 -0
  103. package/src/rust/components/viewer.rs +45 -25
  104. package/src/rust/components/window_editor.rs +5 -4
  105. package/src/rust/config/column_config_schema.rs +38 -377
  106. package/src/rust/config/number_string_format.rs +0 -94
  107. package/src/rust/config/options.rs +3 -7
  108. package/src/rust/config/plugin_static_config.rs +0 -42
  109. package/src/rust/config/viewer_config.rs +47 -17
  110. package/src/rust/config/workspace_config.rs +160 -129
  111. package/src/rust/custom_elements/viewer.rs +187 -131
  112. package/src/rust/custom_events.rs +33 -18
  113. package/src/rust/derived.rs +266 -0
  114. package/src/rust/exprtk/tokenize.rs +0 -54
  115. package/src/rust/js/plugin.rs +1 -1
  116. package/src/rust/js/regular_layout.rs +225 -3
  117. package/src/rust/lib.rs +3 -0
  118. package/src/rust/presentation.rs +23 -4
  119. package/src/rust/queries/column_locator.rs +55 -48
  120. package/src/rust/queries/get_viewer_config.rs +1 -1
  121. package/src/rust/queries/plugin_column_styles.rs +22 -23
  122. package/src/rust/renderer/dispatch.rs +2 -0
  123. package/src/rust/renderer/plugin_config.rs +652 -487
  124. package/src/rust/renderer/props.rs +4 -0
  125. package/src/rust/renderer/registry.rs +2 -6
  126. package/src/rust/renderer/state.rs +205 -0
  127. package/src/rust/renderer.rs +201 -59
  128. package/src/rust/session/metadata.rs +30 -9
  129. package/src/rust/session/op_queue.rs +430 -0
  130. package/src/rust/session/panel_state.rs +327 -0
  131. package/src/rust/session/view_subscription.rs +42 -3
  132. package/src/rust/session.rs +900 -395
  133. package/src/rust/tasks/apply_global_filters.rs +49 -35
  134. package/src/rust/tasks/auto_pause.rs +22 -12
  135. package/src/rust/tasks/copy_export.rs +5 -0
  136. package/src/rust/tasks/create_panel.rs +34 -9
  137. package/src/rust/tasks/derive.rs +312 -0
  138. package/src/rust/tasks/mod.rs +6 -2
  139. package/src/rust/tasks/pipeline.rs +145 -89
  140. package/src/rust/tasks/reset_all.rs +31 -20
  141. package/src/rust/tasks/restore_derived.rs +200 -0
  142. package/src/rust/tasks/restore_panel.rs +153 -141
  143. package/src/rust/tasks/send_column_config.rs +56 -29
  144. package/src/rust/tasks/send_plugin_config.rs +52 -23
  145. package/src/rust/tasks/set_edit_mode.rs +33 -25
  146. package/src/rust/tasks/sync_update_panels.rs +30 -12
  147. package/src/rust/tasks/table_lifecycle.rs +70 -34
  148. package/src/rust/tasks/transactional_restore.rs +387 -0
  149. package/src/rust/tasks/update_theme.rs +45 -6
  150. package/src/rust/ui/containers/scroll_panel.rs +17 -10
  151. package/src/rust/ui/containers/split_panel.rs +0 -41
  152. package/src/rust/ui/context_menu.rs +157 -18
  153. package/src/rust/ui/font_loader.rs +23 -35
  154. package/src/rust/ui/form/mirrored_textarea.rs +0 -12
  155. package/src/rust/ui/form/multi_stop_gradient_selector.rs +0 -89
  156. package/src/rust/ui/form/palette_selector.rs +0 -51
  157. package/src/rust/ui/portal.rs +4 -10
  158. package/src/rust/utils/browser/mod.rs +2 -3
  159. package/src/{ts/perspective-viewer.inline.ts → rust/utils/browser/mouse.rs} +11 -10
  160. package/src/rust/utils/browser/request_animation_frame.rs +27 -8
  161. package/src/rust/utils/css.rs +0 -360
  162. package/src/rust/utils/local_fonts.rs +0 -36
  163. package/src/rust/utils/mod.rs +2 -3
  164. package/src/rust/utils/modal_position.rs +15 -21
  165. package/src/rust/utils/{browser/tests/mod.rs → plugin_host_state.rs} +31 -1
  166. package/src/rust/utils/pubsub.rs +0 -1
  167. package/src/rust/workspace.rs +187 -140
  168. package/src/svg/derived.svg +6 -0
  169. package/src/themes/icons.css +1 -0
  170. package/src/themes/intl/ar.css +11 -0
  171. package/src/themes/intl/de.css +11 -0
  172. package/src/themes/intl/es.css +11 -0
  173. package/src/themes/intl/fr.css +11 -0
  174. package/src/themes/intl/ja.css +11 -0
  175. package/src/themes/intl/pt.css +11 -0
  176. package/src/themes/intl/zh.css +11 -0
  177. package/src/themes/intl.css +11 -0
  178. package/src/ts/extensions.ts +3 -36
  179. package/src/ts/perspective-viewer.ts +1 -1
  180. package/src/ts/plugin.ts +4 -1
  181. package/src/{rust/utils/tests/request_animation_frame.rs → ts/select-detail.ts} +32 -26
  182. package/src/ts/ts-rs/DerivedTableConfig.ts +22 -0
  183. package/src/ts/ts-rs/Layout.ts +2 -2
  184. package/src/ts/ts-rs/Orientation.ts +2 -1
  185. package/src/ts/ts-rs/PanelViewerConfig.ts +7 -6
  186. package/src/ts/ts-rs/RestoreOptions.ts +3 -7
  187. package/src/ts/ts-rs/SplitLayout.ts +17 -1
  188. package/src/ts/ts-rs/TabLayout.ts +3 -0
  189. package/src/ts/ts-rs/TableRef.ts +9 -0
  190. package/src/ts/ts-rs/ViewerConfig.ts +7 -6
  191. package/src/ts/ts-rs/ViewerConfigUpdate.ts +7 -5
  192. package/src/ts/ts-rs/WorkspaceConfig.ts +7 -1
  193. package/src/ts/ts-rs/WorkspaceConfigUpdate.ts +9 -1
  194. package/dist/esm/perspective-viewer.inline.d.ts +0 -3
  195. package/dist/esm/perspective-viewer.inline.js +0 -21
  196. package/dist/esm/perspective-viewer.inline.js.map +0 -7
  197. package/src/rust/tasks/restore_and_render.rs +0 -107
  198. package/src/rust/utils/browser/tests/debounce.rs +0 -163
  199. package/src/rust/utils/tests/mod.rs +0 -14
  200. package/src/rust/utils/tests/pubsub.rs +0 -95
  201. /package/dist/wasm/snippets/{perspective-viewer-6813b568219c6763 → perspective-viewer-7fc45549159e01e4}/inline0.js +0 -0
  202. /package/dist/wasm/snippets/{perspective-viewer-6813b568219c6763 → perspective-viewer-7fc45549159e01e4}/inline1.js +0 -0
  203. /package/dist/wasm/snippets/{perspective-viewer-6813b568219c6763 → perspective-viewer-7fc45549159e01e4}/inline2.js +0 -0
  204. /package/dist/wasm/snippets/{perspective-viewer-6813b568219c6763 → perspective-viewer-7fc45549159e01e4}/inline3.js +0 -0
  205. /package/dist/wasm/snippets/{perspective-viewer-6813b568219c6763 → perspective-viewer-7fc45549159e01e4}/inline4.js +0 -0
  206. /package/dist/wasm/snippets/{perspective-viewer-6813b568219c6763 → perspective-viewer-7fc45549159e01e4}/inline5.js +0 -0
@@ -226,241 +226,3 @@ impl Writer {
226
226
  }
227
227
  }
228
228
  }
229
-
230
- #[cfg(test)]
231
- mod tests {
232
- use super::events::{Align, Event, Tag};
233
-
234
- fn parse(text: &str) -> Vec<Event> {
235
- super::block::parse(text)
236
- }
237
-
238
- fn start(tag: Tag) -> Event {
239
- Event::Start(tag)
240
- }
241
-
242
- fn text(x: &str) -> Event {
243
- Event::Text(x.to_owned())
244
- }
245
-
246
- #[test]
247
- fn paragraph_and_emphasis() {
248
- assert_eq!(parse("**Done!** Key *stats* here."), vec![
249
- start(Tag::Paragraph),
250
- start(Tag::Strong),
251
- text("Done!"),
252
- Event::End,
253
- text(" Key "),
254
- start(Tag::Emphasis),
255
- text("stats"),
256
- Event::End,
257
- text(" here."),
258
- Event::End,
259
- ]);
260
- }
261
-
262
- #[test]
263
- fn snake_case_is_not_emphasis() {
264
- assert_eq!(parse("call get_schema then set_view_config"), vec![
265
- start(Tag::Paragraph),
266
- text("call get_schema then set_view_config"),
267
- Event::End,
268
- ]);
269
- }
270
-
271
- #[test]
272
- fn nested_strong_emphasis() {
273
- assert_eq!(parse("**bold with *ital* inside**"), vec![
274
- start(Tag::Paragraph),
275
- start(Tag::Strong),
276
- text("bold with "),
277
- start(Tag::Emphasis),
278
- text("ital"),
279
- Event::End,
280
- text(" inside"),
281
- Event::End,
282
- Event::End,
283
- ]);
284
- }
285
-
286
- #[test]
287
- fn unpaired_delimiters_stay_literal() {
288
- // Space on both sides ⇒ the runs can neither open nor close, so
289
- // they stay in the running text buffer.
290
- assert_eq!(parse("2 * 3 * 4 and a ~ tilde"), vec![
291
- start(Tag::Paragraph),
292
- text("2 * 3 * 4 and a ~ tilde"),
293
- Event::End,
294
- ]);
295
- }
296
-
297
- #[test]
298
- fn code_spans_and_strikethrough() {
299
- assert_eq!(parse("run `pnpm test` and ~~skip~~ this"), vec![
300
- start(Tag::Paragraph),
301
- text("run "),
302
- Event::Code("pnpm test".to_owned()),
303
- text(" and "),
304
- start(Tag::Strikethrough),
305
- text("skip"),
306
- Event::End,
307
- text(" this"),
308
- Event::End,
309
- ]);
310
- }
311
-
312
- #[test]
313
- fn heading_rule_and_fence() {
314
- assert_eq!(parse("## Stats\n\n---\n\n```sql\nSELECT 1;\n```"), vec![
315
- start(Tag::Heading(2)),
316
- text("Stats"),
317
- Event::End,
318
- Event::Rule,
319
- start(Tag::CodeBlock(Some("sql".to_owned()))),
320
- text("SELECT 1;\n"),
321
- Event::End,
322
- ]);
323
- }
324
-
325
- #[test]
326
- fn nested_list_with_code_block_in_item() {
327
- assert_eq!(
328
- parse("- first\n- second:\n ```\n x = 1\n ```\n - inner"),
329
- vec![
330
- start(Tag::List(None)),
331
- start(Tag::Item),
332
- text("first"),
333
- Event::End,
334
- start(Tag::Item),
335
- text("second:"),
336
- start(Tag::CodeBlock(None)),
337
- text("x = 1\n"),
338
- Event::End,
339
- start(Tag::List(None)),
340
- start(Tag::Item),
341
- text("inner"),
342
- Event::End,
343
- Event::End,
344
- Event::End,
345
- Event::End,
346
- ]
347
- );
348
- }
349
-
350
- #[test]
351
- fn ordered_list_start() {
352
- assert_eq!(parse("3. three\n4. four"), vec![
353
- start(Tag::List(Some(3))),
354
- start(Tag::Item),
355
- text("three"),
356
- Event::End,
357
- start(Tag::Item),
358
- text("four"),
359
- Event::End,
360
- Event::End,
361
- ]);
362
- }
363
-
364
- #[test]
365
- fn table_with_alignment_and_pipe_in_code() {
366
- assert_eq!(
367
- parse("| Region | Sales |\n| :--- | ---: |\n| West | `a\\|b` |"),
368
- vec![
369
- start(Tag::Table(vec![Align::Left, Align::Right])),
370
- start(Tag::TableHead),
371
- start(Tag::TableCell),
372
- text("Region"),
373
- Event::End,
374
- start(Tag::TableCell),
375
- text("Sales"),
376
- Event::End,
377
- Event::End,
378
- start(Tag::TableRow),
379
- start(Tag::TableCell),
380
- text("West"),
381
- Event::End,
382
- start(Tag::TableCell),
383
- Event::Code("a|b".to_owned()),
384
- Event::End,
385
- Event::End,
386
- Event::End,
387
- ]
388
- );
389
- }
390
-
391
- #[test]
392
- fn dashes_after_paragraph_are_a_rule_not_a_table() {
393
- assert_eq!(parse("Title\n---"), vec![
394
- start(Tag::Paragraph),
395
- text("Title"),
396
- Event::End,
397
- Event::Rule,
398
- ]);
399
- }
400
-
401
- #[test]
402
- fn links_images_and_autolinks() {
403
- assert_eq!(
404
- parse("[safe](https://example.com \"hi\") ![leak](https://example.com/x.png) <https://a.co>"),
405
- vec![
406
- start(Tag::Paragraph),
407
- start(Tag::Link {
408
- dest_url: "https://example.com".to_owned(),
409
- title: "hi".to_owned(),
410
- }),
411
- text("safe"),
412
- Event::End,
413
- text(" "),
414
- start(Tag::Image {
415
- dest_url: "https://example.com/x.png".to_owned(),
416
- }),
417
- text("leak"),
418
- Event::End,
419
- text(" "),
420
- start(Tag::Link {
421
- dest_url: "https://a.co".to_owned(),
422
- title: String::new(),
423
- }),
424
- text("https://a.co"),
425
- Event::End,
426
- Event::End,
427
- ]
428
- );
429
- }
430
-
431
- #[test]
432
- fn raw_html_is_literal_text() {
433
- assert_eq!(parse("<script>alert(1)</script>"), vec![
434
- start(Tag::Paragraph),
435
- text("<script>alert(1)</script>"),
436
- Event::End,
437
- ]);
438
- }
439
-
440
- #[test]
441
- fn blockquote_and_breaks() {
442
- assert_eq!(parse("> quoted\n> more\n\nafter \nbreak"), vec![
443
- start(Tag::BlockQuote),
444
- start(Tag::Paragraph),
445
- text("quoted"),
446
- Event::SoftBreak,
447
- text("more"),
448
- Event::End,
449
- Event::End,
450
- start(Tag::Paragraph),
451
- text("after"),
452
- Event::HardBreak,
453
- text("break"),
454
- Event::End,
455
- ]);
456
- }
457
-
458
- #[test]
459
- fn entities_decode_or_stay_literal() {
460
- assert_eq!(parse("a &amp; b &#65; &unknown; c"), vec![
461
- start(Tag::Paragraph),
462
- text("a & b A &unknown; c"),
463
- Event::End,
464
- ]);
465
- }
466
- }
@@ -9,8 +9,11 @@ Workflow:
9
9
 
10
10
  Panels: the viewer is a dashboard that can hold multiple panels (independent side-by-side views). View tools act on the active panel unless you pass `panel`; manage the layout with `list_panels`, `add_panel`, `remove_panel` and `activate_panel`. `add_panel` takes the new panel's complete configuration up front - `table` and `columns` are required. To duplicate a panel, pass its `get_view_config` result to `add_panel`.
11
11
 
12
+ Dashboards: to build one, create each panel with `add_panel` (its complete config, including a short `title`), then arrange ALL panels in ONE `set_workspace` call whose `layout` names every panel id exactly once. A layout is nested `split-layout`s (`orientation` `horizontal` = side by side, `vertical` = stacked; `sizes` are fractions summing to 1, one per child) whose leaves are `tab-layout` stacks of panel ids - e.g. two charts side by side above a grid is a `vertical` split of [a `horizontal` split of the two charts, the grid]. Call `get_workspace` to read the current arrangement before changing it. A panel that reads a pivoted or filtered copy of a table needs that derived table to exist first: define it in `set_workspace`'s `derived_tables` (the complete set - keep existing entries), then `add_panel` with its name as `table`.
13
+
12
14
  Rules:
13
15
  - Column names in configs must match the schema exactly, including case.
16
+ - A `set_view_config` or `add_panel` call that returns an error changed NOTHING - the viewer is exactly as it was, so there is nothing to undo.
14
17
  - If a tool returns an error, correct your input and retry. If the same call fails twice with the same error, stop and report the problem instead.
15
18
  - You have a fixed budget of model requests per prompt - be economical with tool calls.
16
19
  - After acting, answer with a single short sentence describing what changed. If the user asks a question you can answer from tool results, answer it directly.
@@ -122,11 +122,13 @@ fn panel_property() -> Value {
122
122
  /// config properties the AGENT contract requires beyond the public type
123
123
  /// (unioned into the definition's `required`, stated outright in the
124
124
  /// fallback) — the tool body must enforce the same fields at dispatch.
125
+ /// `omit` lists public properties the agent contract withholds.
125
126
  fn spliced_config(
126
127
  schemas: Option<&HashMap<String, Value>>,
127
128
  key: &str,
128
129
  description: &str,
129
130
  require: &[&str],
131
+ omit: &[&str],
130
132
  ) -> (Value, Option<Value>) {
131
133
  match schemas.and_then(|x| x.get(key)) {
132
134
  Some(schema) => {
@@ -141,7 +143,14 @@ fn spliced_config(
141
143
  .clone()
142
144
  .unwrap_or_else(|| json!({ "type": "object" }));
143
145
 
146
+ if let Some(properties) = config["properties"].as_object_mut() {
147
+ for field in omit {
148
+ properties.remove(*field);
149
+ }
150
+ }
151
+
144
152
  let mut required = config["required"].as_array().cloned().unwrap_or_default();
153
+ required.retain(|x| !omit.iter().any(|field| x == field));
145
154
  for field in require {
146
155
  if !required.iter().any(|x| x == field) {
147
156
  required.push(json!(field));
@@ -163,6 +172,8 @@ fn spliced_config(
163
172
  definitions.insert(key.to_owned(), original);
164
173
  }
165
174
 
175
+ let reachable = reachable_definitions(&config, &definitions);
176
+ definitions.retain(|name, _| reachable.contains(name));
166
177
  let definitions = (!definitions.is_empty()).then(|| Value::Object(definitions));
167
178
  (config, definitions)
168
179
  },
@@ -177,14 +188,59 @@ fn spliced_config(
177
188
  }
178
189
  }
179
190
 
191
+ fn definition_refs(node: &Value, out: &mut Vec<String>) {
192
+ match node {
193
+ Value::Object(map) => {
194
+ for (key, value) in map {
195
+ match value
196
+ .as_str()
197
+ .and_then(|x| x.strip_prefix("#/definitions/"))
198
+ {
199
+ Some(name) if key == "$ref" => out.push(name.to_owned()),
200
+ _ => definition_refs(value, out),
201
+ }
202
+ }
203
+ },
204
+ Value::Array(values) => {
205
+ for value in values {
206
+ definition_refs(value, out);
207
+ }
208
+ },
209
+ _ => {},
210
+ }
211
+ }
212
+
213
+ fn reachable_definitions(
214
+ root: &Value,
215
+ definitions: &serde_json::Map<String, Value>,
216
+ ) -> Vec<String> {
217
+ let mut pending = Vec::new();
218
+ definition_refs(root, &mut pending);
219
+ let mut reachable = Vec::new();
220
+ while let Some(name) = pending.pop() {
221
+ if reachable.contains(&name) {
222
+ continue;
223
+ }
224
+
225
+ if let Some(definition) = definitions.get(&name) {
226
+ definition_refs(definition, &mut pending);
227
+ }
228
+
229
+ reachable.push(name);
230
+ }
231
+
232
+ reachable
233
+ }
234
+
180
235
  fn config_parameters(
181
236
  schemas: Option<&HashMap<String, Value>>,
182
237
  key: &str,
183
238
  description: &str,
184
239
  with_panel: bool,
185
240
  require: &[&str],
241
+ omit: &[&str],
186
242
  ) -> Value {
187
- let (config, definitions) = spliced_config(schemas, key, description, require);
243
+ let (config, definitions) = spliced_config(schemas, key, description, require, omit);
188
244
  let mut properties = json!({ "config": config });
189
245
  if with_panel {
190
246
  properties["panel"] = panel_property();
@@ -253,6 +309,7 @@ static TOOLS: &[Tool] = &[
253
309
  "A partial ViewerConfigUpdate patch",
254
310
  true,
255
311
  &[],
312
+ &[],
256
313
  )
257
314
  },
258
315
  call: |elem, ctx, args| Box::pin(elem.tool_set_view_config(ctx, args)),
@@ -348,6 +405,7 @@ static TOOLS: &[Tool] = &[
348
405
  "The new panel's ViewerConfigInitial (`table` and `columns` required)",
349
406
  false,
350
407
  &["table", "columns"],
408
+ &[],
351
409
  )
352
410
  },
353
411
  call: |elem, ctx, args| Box::pin(elem.tool_add_panel(ctx, args)),
@@ -392,6 +450,45 @@ static TOOLS: &[Tool] = &[
392
450
  },
393
451
  call: |elem, ctx, args| Box::pin(elem.tool_activate_panel(ctx, args)),
394
452
  },
453
+ Tool {
454
+ name: "get_workspace",
455
+ description: "Get the dashboard's workspace state: the `layout` tree arranging the \
456
+ panels, each panel's `title`, `plugin` and `table`, the active panel, and \
457
+ the element-level `derived_tables`, `masters`, `global_filters` and \
458
+ `palette`. Call this before `set_workspace` - its `layout` must name every \
459
+ panel id listed here.",
460
+ entitlement: Entitlement::ReadView,
461
+ available: always,
462
+ parameters: |_| json!({ "type": "object", "properties": {} }),
463
+ call: |elem, ctx, args| Box::pin(elem.tool_get_workspace(ctx, args)),
464
+ },
465
+ Tool {
466
+ name: "set_workspace",
467
+ description: "Apply a field-wise update to the dashboard's workspace state; absent fields \
468
+ are unchanged and `null` resets. `layout` arranges the panels into a grid: \
469
+ nested `split-layout`s (`orientation` `horizontal` = side by side, \
470
+ `vertical` = stacked; `sizes` are fractions summing to 1, one per child) \
471
+ whose leaves are `tab-layout` stacks of panel ids, naming EVERY panel \
472
+ exactly once. `derived_tables` is the COMPLETE set of derived tables (keep \
473
+ existing entries), each a pivoted/filtered view of `table` that panels can \
474
+ then bind by name via add_panel. `masters` are panels whose row selection \
475
+ cross-filters the others through `global_filters`. Panels themselves are \
476
+ created and removed with add_panel / remove_panel. Returns the workspace \
477
+ state after the update.",
478
+ entitlement: Entitlement::ManageLayout,
479
+ available: always,
480
+ parameters: |schemas| {
481
+ config_parameters(
482
+ schemas,
483
+ "WorkspaceConfigUpdate",
484
+ "A partial WorkspaceConfigUpdate (`panels` excluded)",
485
+ false,
486
+ &[],
487
+ &["panels"],
488
+ )
489
+ },
490
+ call: |elem, ctx, args| Box::pin(elem.tool_set_workspace(ctx, args)),
491
+ },
395
492
  Tool {
396
493
  name: "search_docs",
397
494
  description: "Search the reference documentation: Perspective features, configuration \
@@ -482,7 +579,7 @@ pub struct ToolError(String);
482
579
  /// applied config.
483
580
  fn retyped_columns(
484
581
  session: &crate::session::Session,
485
- config: &crate::config::ViewerConfig,
582
+ config: &crate::config::ViewerConfig<String, String>,
486
583
  ) -> Vec<Value> {
487
584
  let metadata = session.metadata();
488
585
  config
@@ -577,6 +674,11 @@ struct PanelNameArgs {
577
674
  panel: String,
578
675
  }
579
676
 
677
+ #[derive(Deserialize)]
678
+ struct SetWorkspaceArgs {
679
+ config: serde_json::Map<String, Value>,
680
+ }
681
+
580
682
  #[derive(Deserialize)]
581
683
  struct SearchDocsArgs {
582
684
  query: String,
@@ -796,6 +898,29 @@ fn schema_object(schema: &ColumnConfigSchema) -> Value {
796
898
 
797
899
  const SEARCH_DOCS_LIMIT: usize = 4;
798
900
 
901
+ /// A `restoreWorkspace` update returning each field of `update` to its value
902
+ /// in the `saveWorkspace` token `snapshot`.
903
+ fn workspace_rollback(snapshot: &Value, update: &serde_json::Map<String, Value>) -> Value {
904
+ let mut rollback = serde_json::Map::new();
905
+ for key in update.keys() {
906
+ let saved = snapshot.get(key).filter(|x| !x.is_null()).cloned();
907
+ let value = match key.as_str() {
908
+ "layout" => saved,
909
+ "derived_tables" => Some(saved.unwrap_or_else(|| json!({}))),
910
+ "active" | "masters" | "global_filters" | "palette" => {
911
+ Some(saved.unwrap_or(Value::Null))
912
+ },
913
+ _ => None,
914
+ };
915
+
916
+ if let Some(value) = value {
917
+ rollback.insert(key.clone(), value);
918
+ }
919
+ }
920
+
921
+ Value::Object(rollback)
922
+ }
923
+
799
924
  /// Tool bodies and their shared helpers — plain (non-bindgen) methods.
800
925
  impl PerspectiveViewerElement {
801
926
  /// Resolve a tool's panel target: a named panel, or the active panel
@@ -873,22 +998,8 @@ impl PerspectiveViewerElement {
873
998
  async fn tool_set_view_config(&self, ctx: &ToolCtx, args: Value) -> Result<Value, ToolError> {
874
999
  let args: SetViewConfigArgs = serde_json::from_value(args)?;
875
1000
  let target = args.panel.as_deref();
876
- let panel = self.agent_panel(target)?;
877
- let snapshot =
878
- get_viewer_config(&panel.session, &panel.renderer, &self.presentation).await?;
879
-
1001
+ self.agent_panel(target)?;
880
1002
  if let Err(err) = self.agent_restore(target, &args.config).await {
881
- let rolled = self
882
- .agent_restore(target, &serde_json::to_value(&snapshot)?)
883
- .await;
884
-
885
- let err = match rolled {
886
- Ok(()) => err,
887
- Err(rollback_err) => ToolError(format!(
888
- "{err} (additionally, restoring the prior config failed: {rollback_err})"
889
- )),
890
- };
891
-
892
1003
  return Err(with_docs_hint(ctx, err));
893
1004
  }
894
1005
 
@@ -1088,6 +1199,160 @@ impl PerspectiveViewerElement {
1088
1199
  client.get_hosted_table_names().await.ok()
1089
1200
  }
1090
1201
 
1202
+ async fn tool_get_workspace(&self, _ctx: &ToolCtx, _args: Value) -> Result<Value, ToolError> {
1203
+ let token = self.agent_save_workspace().await?;
1204
+ Ok(self.agent_workspace_summary(&token))
1205
+ }
1206
+
1207
+ /// Applies via the public `restoreWorkspace()`, restoring the touched
1208
+ /// fields from a `saveWorkspace()` snapshot on failure.
1209
+ async fn tool_set_workspace(&self, ctx: &ToolCtx, args: Value) -> Result<Value, ToolError> {
1210
+ let args: SetWorkspaceArgs = serde_json::from_value(args)?;
1211
+ if args.config.contains_key("panels") {
1212
+ return Err(ToolError(
1213
+ "`panels` cannot be set with set_workspace - create, configure and remove panels \
1214
+ with `add_panel`, `set_view_config` and `remove_panel`, then arrange them with \
1215
+ `layout`."
1216
+ .to_owned(),
1217
+ ));
1218
+ }
1219
+
1220
+ let snapshot = self.agent_save_workspace().await?;
1221
+ if let Some(derived) = args.config.get("derived_tables") {
1222
+ self.agent_check_derived(&snapshot, derived).await?;
1223
+ }
1224
+
1225
+ let update = Value::Object(args.config.clone());
1226
+ if let Err(err) = self.agent_restore_workspace(&update).await {
1227
+ let rollback = workspace_rollback(&snapshot, &args.config);
1228
+ if let Err(rollback_err) = self.agent_restore_workspace(&rollback).await {
1229
+ return Err(ToolError(format!(
1230
+ "{err} (restoring the previous workspace also failed: {rollback_err})"
1231
+ )));
1232
+ }
1233
+
1234
+ return Err(with_docs_hint(ctx, err));
1235
+ }
1236
+
1237
+ let token = self.agent_save_workspace().await?;
1238
+ Ok(self.agent_workspace_summary(&token))
1239
+ }
1240
+
1241
+ /// Reject a `derived_tables` set that drops a table a panel reads, or
1242
+ /// whose entries read a table that will not exist.
1243
+ async fn agent_check_derived(
1244
+ &self,
1245
+ snapshot: &Value,
1246
+ derived: &Value,
1247
+ ) -> Result<(), ToolError> {
1248
+ let Some(derived) = derived.as_object() else {
1249
+ return Ok(());
1250
+ };
1251
+
1252
+ let current = snapshot["derived_tables"]
1253
+ .as_object()
1254
+ .map(|x| x.keys().cloned().collect::<Vec<_>>())
1255
+ .unwrap_or_default();
1256
+
1257
+ let mut dropped = snapshot["panels"]
1258
+ .as_object()
1259
+ .into_iter()
1260
+ .flat_map(|panels| panels.values())
1261
+ .filter_map(|config| config["table"].as_str())
1262
+ .filter(|table| current.iter().any(|x| x == table) && !derived.contains_key(*table))
1263
+ .map(str::to_owned)
1264
+ .collect::<Vec<_>>();
1265
+
1266
+ dropped.sort();
1267
+ dropped.dedup();
1268
+ if !dropped.is_empty() {
1269
+ return Err(ToolError(format!(
1270
+ "`derived_tables` is the COMPLETE set of derived tables, not a patch: {dropped:?} \
1271
+ are read by panels and must stay in it (current derived tables: {current:?})"
1272
+ )));
1273
+ }
1274
+
1275
+ let Some(hosted) = self.agent_hosted_table_names().await else {
1276
+ return Ok(());
1277
+ };
1278
+
1279
+ let loaded = hosted
1280
+ .into_iter()
1281
+ .filter(|x| !current.contains(x))
1282
+ .collect::<Vec<_>>();
1283
+
1284
+ for (name, config) in derived {
1285
+ let Some(parent) = config["table"].as_str() else {
1286
+ continue;
1287
+ };
1288
+
1289
+ if !derived.contains_key(parent) && !loaded.iter().any(|x| x == parent) {
1290
+ return Err(ToolError(format!(
1291
+ "Derived table `{name}` reads unknown table `{parent}` - its `table` must be \
1292
+ a loaded table or another entry in `derived_tables` (loaded tables: \
1293
+ {loaded:?})"
1294
+ )));
1295
+ }
1296
+ }
1297
+
1298
+ Ok(())
1299
+ }
1300
+
1301
+ fn agent_workspace_summary(&self, token: &Value) -> Value {
1302
+ let panels = token["panels"]
1303
+ .as_object()
1304
+ .into_iter()
1305
+ .flatten()
1306
+ .map(|(id, config)| {
1307
+ (
1308
+ id.clone(),
1309
+ json!({
1310
+ "title": config["title"],
1311
+ "plugin": config["plugin"],
1312
+ "table": config["table"],
1313
+ }),
1314
+ )
1315
+ })
1316
+ .collect::<serde_json::Map<_, _>>();
1317
+
1318
+ let or = |key: &str, default: Value| {
1319
+ token
1320
+ .get(key)
1321
+ .filter(|x| !x.is_null())
1322
+ .cloned()
1323
+ .unwrap_or(default)
1324
+ };
1325
+
1326
+ json!({
1327
+ "active": self.agent_active_name(),
1328
+ "layout": token["layout"],
1329
+ "panels": panels,
1330
+ "derived_tables": or("derived_tables", json!({})),
1331
+ "masters": or("masters", json!([])),
1332
+ "global_filters": or("global_filters", json!([])),
1333
+ "palette": or("palette", json!({})),
1334
+ })
1335
+ }
1336
+
1337
+ async fn agent_save_workspace(&self) -> Result<Value, ToolError> {
1338
+ let options = js_sys::Object::new();
1339
+ js_sys::Reflect::set(&options, &JsValue::from_str("full_palette"), &JsValue::TRUE)?;
1340
+
1341
+ let saved = self.saveWorkspace(Some(options.unchecked_into()));
1342
+ let promise: js_sys::Promise = JsValue::from(saved).unchecked_into();
1343
+ let token = wasm_bindgen_futures::JsFuture::from(promise).await?;
1344
+ let json = String::from(js_sys::JSON::stringify(&token)?);
1345
+ Ok(serde_json::from_str(&json)?)
1346
+ }
1347
+
1348
+ async fn agent_restore_workspace(&self, update: &Value) -> Result<(), ToolError> {
1349
+ let js_update = js_sys::JSON::parse(&update.to_string())?;
1350
+ let restored = self.restoreWorkspace(js_update.unchecked_into(), None);
1351
+ let promise: js_sys::Promise = JsValue::from(restored).unchecked_into();
1352
+ wasm_bindgen_futures::JsFuture::from(promise).await?;
1353
+ Ok(())
1354
+ }
1355
+
1091
1356
  async fn tool_remove_panel(&self, _ctx: &ToolCtx, args: Value) -> Result<Value, ToolError> {
1092
1357
  let args: PanelNameArgs = serde_json::from_value(args)?;
1093
1358
  self.agent_panel(Some(&args.panel))?;