tern-sdk 0.1.0

Talk to Tern over the Tern Surface Protocol: typed node builders, diffing with derived ids, flow control, event routing, print/ask helpers and a plain-text fallback.
Documentation
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
//! Layer 1: the wire. Constants, the messages a program sends and receives,
//! the encoder (framing, chunking, blobs) and the decoder of replies and
//! events.
//!
//! Every message is one APC string, `ESC _ tsp;<verb>[;k=v]*;<body> ESC \`.
//! A body over the negotiated `apc` limit is cut by [`chunks`] exactly as
//! Tern's own `tsp_chunks` cuts it, and each piece is sent with `c=<id>`
//! (and `m=1` on all but the last).

use std::{collections::BTreeMap, fmt};

use base64::Engine as _;
use serde::{Deserialize, Serialize, Serializer, de::DeserializeOwned, ser::SerializeSeq};
use serde_json::{Map, Value};
use sha2::{Digest as _, Sha256};

use crate::Error;

/// The protocol version this SDK speaks.
pub const VERSION: u32 = 1;
/// The largest body sent in one message unless the `hello` reply says
/// otherwise.
pub const APC_LIMIT: usize = 65_536;
/// How many frames may be unacknowledged unless the `hello` reply says
/// otherwise.
pub const CREDITS: u32 = 2;
/// The largest blob Tern takes, in decoded bytes.
pub const MAX_BLOB: usize = 16 << 20;

/// Node kinds, as the `k` of a node and the `kinds` of the `hello` reply.
pub mod kind {
	/// A vertical stack.
	pub const COL: &str = "col";
	/// A horizontal flex row.
	pub const ROW: &str = "row";
	/// A card with a head and a body.
	pub const CARD: &str = "card";
	/// A disclosure group with no ring.
	pub const SECTION: &str = "section";
	/// A divider hairline.
	pub const RULE: &str = "rule";
	/// Vertical space.
	pub const SPACER: &str = "spacer";
	/// Styled spans.
	pub const TEXT: &str = "text";
	/// Markdown.
	pub const MD: &str = "md";
	/// Highlighted code.
	pub const CODE: &str = "code";
	/// A unified or split diff.
	pub const DIFF: &str = "diff";
	/// Output with escape sequences.
	pub const ANSI: &str = "ansi";
	/// Pre-rendered ANSI rows.
	pub const ROWS: &str = "rows";
	/// TeX math.
	pub const MATH: &str = "math";
	/// An aligned key/value list.
	pub const KV: &str = "kv";
	/// A table.
	pub const TABLE: &str = "table";
	/// A disclosure tree.
	pub const TREE: &str = "tree";
	/// A pill chip.
	pub const BADGE: &str = "badge";
	/// Keycaps.
	pub const KBD: &str = "kbd";
	/// A named icon.
	pub const ICON: &str = "icon";
	/// An image from a blob.
	pub const IMAGE: &str = "image";
	/// A selectable list.
	pub const LIST: &str = "list";
	/// One list row.
	pub const ITEM: &str = "item";
	/// A tab strip.
	pub const TABS: &str = "tabs";
	/// A searchable picker sheet.
	pub const PICKER: &str = "picker";
	/// An activity indicator.
	pub const SPINNER: &str = "spinner";
	/// A shimmering label.
	pub const SHIMMER: &str = "shimmer";
	/// A live timer.
	pub const ELAPSED: &str = "elapsed";
	/// A number easing between updates.
	pub const RATE: &str = "rate";
	/// A progress bar.
	pub const PROGRESS: &str = "progress";
	/// A value as a bar, ring or grid.
	pub const METER: &str = "meter";
	/// Bars, a sparkline or a heatmap.
	pub const CHART: &str = "chart";
	/// A thinking-effort glyph.
	pub const EFFORT: &str = "effort";
	/// A multi-line text field.
	pub const EDITOR: &str = "editor";
	/// A single-line text field.
	pub const INPUT: &str = "input";
	/// A status bar strip.
	pub const STATUS: &str = "status";
	/// A status bar segment.
	pub const SEG: &str = "seg";
	/// A transient notice.
	pub const TOAST: &str = "toast";
	/// A floating panel.
	pub const OVERLAY: &str = "overlay";
	/// A coding-agent tool call.
	pub const TOOL: &str = "tool";
	/// A subagent row.
	pub const AGENT: &str = "agent";
	/// A todo list.
	pub const CHECKLIST: &str = "checklist";
	/// A shell command's output (Tern's own).
	pub const BLOCK: &str = "block";
	/// A settings page.
	pub const PREFS: &str = "prefs";
	/// A plain HTML element.
	pub const EL: &str = "el";

	/// Every kind of protocol version 1.
	pub const ALL: [&str; 44] = [
		COL, ROW, CARD, SECTION, RULE, SPACER, TEXT, MD, CODE, DIFF, ANSI, ROWS, MATH, KV, TABLE,
		TREE, BADGE, KBD, ICON, IMAGE, LIST, ITEM, TABS, PICKER, SPINNER, SHIMMER, ELAPSED, RATE,
		PROGRESS, METER, CHART, EFFORT, EDITOR, INPUT, STATUS, SEG, TOAST, OVERLAY, TOOL, AGENT,
		CHECKLIST, BLOCK, PREFS, EL,
	];

	/// The kinds whose `text` prop is primary text, updated with `text` and
	/// `splice` ops.
	pub const TEXT_KINDS: [&str; 9] = [TEXT, MD, CODE, ANSI, MATH, EDITOR, INPUT, SHIMMER, EL];

	/// Whether `kind` keeps primary text in its `text` prop.
	pub fn has_text(kind: &str) -> bool {
		TEXT_KINDS.contains(&kind)
	}
}

/// Feature names of the `hello` query (program) and reply (terminal).
pub mod feature {
	/// Program: applies `edit` events (native editing).
	pub const EDIT: &str = "edit";
	/// Program: applies `undo` events.
	pub const UNDO: &str = "undo";
	/// Program: submits text from `send` events.
	pub const SEND: &str = "send";

	/// Terminal: the `b` verb and the `blobs` query.
	pub const BLOBS: &str = "blobs";
	/// Terminal: the `settle` op is a useful hint.
	pub const SETTLE: &str = "settle";
	/// Terminal: `o` with `adopt:true` reopens a closed surface.
	pub const ADOPT: &str = "adopt";
	/// Terminal: the `dock` region sticks to the pane's bottom.
	pub const DOCK: &str = "dock";
	/// Terminal: the `t` verb colors the surface.
	pub const PROGRAM_PALETTE: &str = "program-palette";
	/// Terminal: `reduceMotion` and `motion` events are reported.
	pub const REDUCE_MOTION: &str = "reduce-motion";
	/// Terminal: a `prefs` node in an inline surface's `layer` docks beside
	/// the pane.
	pub const ASIDE: &str = "aside";
	/// Terminal: the `scroll` op.
	pub const SCROLL: &str = "scroll";
	/// Terminal: the `s` verb (stylesheets).
	pub const STYLES: &str = "styles";
	/// Terminal: `flow` surfaces and `listen:false`.
	pub const FLOW: &str = "flow";
}

// ---------------------------------------------------------------------------
// Program → terminal
// ---------------------------------------------------------------------------

/// A surface mode.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Mode {
	/// At the cursor row, owning the pane while live (a session program).
	#[default]
	Inline,
	/// Over the whole pane, like the alternate screen.
	Screen,
	/// At the cursor row among the output around it, kept as output.
	Flow,
}

/// A `q` query.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Query {
	/// `{"q":"hello","v":[1],"app"?,"ver"?,"features"?}`.
	Hello {
		/// The program's name.
		app:      Option<String>,
		/// The program's version.
		ver:      Option<String>,
		/// Program features (`edit`, `undo`, `send`).
		features: Vec<String>,
	},
	/// `{"q":"blobs","ids":[…]}`: which blobs Tern still holds.
	Blobs {
		/// Blob ids (lowercase SHA-256 hex).
		ids: Vec<String>,
	},
}

impl Serialize for Query {
	fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
		use serde::ser::SerializeMap as _;
		match self {
			Self::Hello { app, ver, features } => {
				let mut m = s.serialize_map(None)?;
				m.serialize_entry("q", "hello")?;
				m.serialize_entry("v", &[VERSION])?;
				if let Some(app) = app {
					m.serialize_entry("app", app)?;
				}
				if let Some(ver) = ver {
					m.serialize_entry("ver", ver)?;
				}
				if !features.is_empty() {
					m.serialize_entry("features", features)?;
				}
				m.end()
			},
			Self::Blobs { ids } => {
				let mut m = s.serialize_map(Some(2))?;
				m.serialize_entry("q", "blobs")?;
				m.serialize_entry("ids", ids)?;
				m.end()
			},
		}
	}
}

/// An `o` message: open a surface.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
pub struct Open {
	/// The surface id; frames and events carry it as `sf`.
	pub id:     String,
	/// The mode (`inline` when absent).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub mode:   Option<Mode>,
	/// Names the pane until the program sets its own title.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub title:  Option<String>,
	/// `data-surface` on the region elements, for stylesheets.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub role:   Option<String>,
	/// `false`: the program never reads its input.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub listen: Option<bool>,
	/// `true` reopens a closed inline surface with this id.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub adopt:  Option<bool>,
}

/// An `f` message: an atomic batch of ops on one surface.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Frame {
	/// The surface id.
	pub sf:  String,
	/// The frame's sequence number, from 1 per surface.
	pub s:   u64,
	/// The ops, applied in order.
	pub ops: Vec<Op>,
}

/// How a `text` op changes primary text.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum TextMode {
	/// Add to the end (the streaming path).
	Append,
	/// Set the whole text.
	Replace,
}

/// Where a `reveal` op scrolls a node.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum RevealAt {
	/// The node's start at the scroller's start.
	Start,
	/// The node's end at the scroller's end.
	End,
	/// The least scrolling that shows the node.
	#[default]
	Nearest,
}

/// How far a `scroll` op moves.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ScrollBy {
	/// One line up.
	LineUp,
	/// One line down.
	LineDown,
	/// A viewport less a line up.
	PageUp,
	/// A viewport less a line down.
	PageDown,
	/// To the start.
	Start,
	/// To the end (resumes following).
	End,
}

/// A node with its id, as an `add` op carries it.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct WireNode {
	/// The node id.
	pub id: String,
	/// The kind.
	pub k:  String,
	/// Props (omitted when empty).
	#[serde(default, skip_serializing_if = "Map::is_empty")]
	pub p:  Map<String, Value>,
	/// Children (omitted when empty).
	#[serde(default, skip_serializing_if = "Vec::is_empty")]
	pub c:  Vec<Self>,
}

/// One frame op.
#[derive(Clone, Debug, PartialEq)]
pub enum Op {
	/// `["add", id, parent, before|null, node]`.
	Add {
		/// Parent id.
		parent: String,
		/// The sibling to insert before, or last.
		before: Option<String>,
		/// The subtree; its id is the op's.
		node:   WireNode,
	},
	/// `["set", id, props]`: merge props shallowly; `null` deletes.
	Set {
		/// Node id.
		id:    String,
		/// Props to replace.
		props: Map<String, Value>,
	},
	/// `["text", id, mode, string]`.
	Text {
		/// Node id.
		id:   String,
		/// Append or replace.
		mode: TextMode,
		/// The text.
		text: String,
	},
	/// `["splice", id, at, del, string]` in UTF-16 units.
	Splice {
		/// Node id.
		id:   String,
		/// Offset in UTF-16 units.
		at:   usize,
		/// UTF-16 units removed.
		del:  usize,
		/// Text inserted.
		text: String,
	},
	/// `["move", id, parent, before|null]`.
	Move {
		/// Node id.
		id:     String,
		/// New parent id.
		parent: String,
		/// The sibling to move before, or last.
		before: Option<String>,
	},
	/// `["del", id]`.
	Del {
		/// Node id.
		id: String,
	},
	/// `["settle", id]`: a hint that the subtree won't change soon.
	Settle {
		/// Node id.
		id: String,
	},
	/// `["focus", id|null]`: the field owning the caret.
	Focus {
		/// Field id, or none.
		id: Option<String>,
	},
	/// `["reveal", id, at]`.
	Reveal {
		/// Node id.
		id: String,
		/// Where to scroll it.
		at: RevealAt,
	},
	/// `["scroll", id, by]`.
	Scroll {
		/// A node in the scroll container.
		id: String,
		/// How far.
		by: ScrollBy,
	},
	/// `["suspend"]`: hand the pane back to the grid.
	Suspend,
	/// `["resume"]`: take the pane again.
	Resume,
}

impl Serialize for Op {
	fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
		let mut seq = s.serialize_seq(None)?;
		match self {
			Self::Add { parent, before, node } => {
				seq.serialize_element("add")?;
				seq.serialize_element(&node.id)?;
				seq.serialize_element(parent)?;
				seq.serialize_element(before)?;
				seq.serialize_element(node)?;
			},
			Self::Set { id, props } => {
				seq.serialize_element("set")?;
				seq.serialize_element(id)?;
				seq.serialize_element(props)?;
			},
			Self::Text { id, mode, text } => {
				seq.serialize_element("text")?;
				seq.serialize_element(id)?;
				seq.serialize_element(mode)?;
				seq.serialize_element(text)?;
			},
			Self::Splice { id, at, del, text } => {
				seq.serialize_element("splice")?;
				seq.serialize_element(id)?;
				seq.serialize_element(at)?;
				seq.serialize_element(del)?;
				seq.serialize_element(text)?;
			},
			Self::Move { id, parent, before } => {
				seq.serialize_element("move")?;
				seq.serialize_element(id)?;
				seq.serialize_element(parent)?;
				seq.serialize_element(before)?;
			},
			Self::Del { id } => {
				seq.serialize_element("del")?;
				seq.serialize_element(id)?;
			},
			Self::Settle { id } => {
				seq.serialize_element("settle")?;
				seq.serialize_element(id)?;
			},
			Self::Focus { id } => {
				seq.serialize_element("focus")?;
				seq.serialize_element(id)?;
			},
			Self::Reveal { id, at } => {
				seq.serialize_element("reveal")?;
				seq.serialize_element(id)?;
				seq.serialize_element(at)?;
			},
			Self::Scroll { id, by } => {
				seq.serialize_element("scroll")?;
				seq.serialize_element(id)?;
				seq.serialize_element(by)?;
			},
			Self::Suspend => seq.serialize_element("suspend")?,
			Self::Resume => seq.serialize_element("resume")?,
		}
		seq.end()
	}
}

/// The names of a palette's two variants.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
pub struct PaletteNames {
	/// The dark variant's name.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub dark:  Option<String>,
	/// The light variant's name.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub light: Option<String>,
}

/// A `t` message: the program palette, token → `#rrggbb` per variant.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
pub struct Palette {
	/// The surface (the live one when absent).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub sf:    Option<String>,
	/// The dark variant.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub dark:  Option<BTreeMap<String, String>>,
	/// The light variant.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub light: Option<BTreeMap<String, String>>,
	/// The variants' names.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub name:  Option<PaletteNames>,
}

/// An `s` message: install, replace or (without `css`) remove a stylesheet.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
pub struct Sheet {
	/// The surface (the live one when absent).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub sf:   Option<String>,
	/// The sheet's name (1–64 of `A-Z a-z 0-9 _ -`).
	pub name: String,
	/// The CSS; absent removes the sheet.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub css:  Option<String>,
}

/// An `x` message: close a surface.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct Close {
	/// The surface id.
	pub id:   String,
	/// Keep it on screen (`main` stays in the scrollback).
	pub keep: bool,
}

/// A `b` message: binary data named by its SHA-256.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Blob {
	/// The lowercase hex SHA-256 of `data`.
	pub id:   String,
	/// The MIME type.
	pub mime: Option<String>,
	/// The bytes.
	pub data: Vec<u8>,
}

impl Blob {
	/// A blob of `data`, its id computed.
	///
	/// # Errors
	/// [`Error::BlobTooLarge`] over [`MAX_BLOB`] bytes.
	pub fn new(data: Vec<u8>, mime: Option<String>) -> Result<Self, Error> {
		if data.len() > MAX_BLOB {
			return Err(Error::BlobTooLarge(data.len()));
		}
		Ok(Self { id: blob_id(&data), mime, data })
	}
}

/// The id of a blob: the lowercase hex SHA-256 of its bytes.
pub fn blob_id(data: &[u8]) -> String {
	use fmt::Write as _;
	let digest = Sha256::digest(data);
	let mut out = String::with_capacity(64);
	for b in digest {
		let _ = write!(out, "{b:02x}");
	}
	out
}

/// Any program → terminal message.
#[derive(Clone, Debug, PartialEq)]
pub enum Message {
	/// `q`.
	Query(Query),
	/// `o`.
	Open(Open),
	/// `f`.
	Frame(Frame),
	/// `b`.
	Blob(Blob),
	/// `t`.
	Palette(Palette),
	/// `s`.
	Sheet(Sheet),
	/// `x`.
	Close(Close),
}

impl Message {
	/// The one-letter verb.
	pub const fn verb(&self) -> &'static str {
		match self {
			Self::Query(_) => "q",
			Self::Open(_) => "o",
			Self::Frame(_) => "f",
			Self::Blob(_) => "b",
			Self::Palette(_) => "t",
			Self::Sheet(_) => "s",
			Self::Close(_) => "x",
		}
	}

	/// The parameters, in order (only a blob has any).
	pub fn params(&self) -> Vec<(&'static str, &str)> {
		match self {
			Self::Blob(b) => {
				let mut out = vec![("id", b.id.as_str())];
				if let Some(mime) = &b.mime {
					out.push(("mime", mime.as_str()));
				}
				out
			},
			_ => Vec::new(),
		}
	}

	/// The body: compact JSON, or base64 for a blob.
	///
	/// # Errors
	/// When serialization fails (it doesn't for these types).
	pub fn body(&self) -> Result<String, Error> {
		Ok(match self {
			Self::Query(m) => serde_json::to_string(m)?,
			Self::Open(m) => serde_json::to_string(m)?,
			Self::Frame(m) => serde_json::to_string(m)?,
			Self::Blob(b) => base64::engine::general_purpose::STANDARD.encode(&b.data),
			Self::Palette(m) => serde_json::to_string(m)?,
			Self::Sheet(m) => serde_json::to_string(m)?,
			Self::Close(m) => serde_json::to_string(m)?,
		})
	}
}

/// A byte of a parameter key.
const fn key_byte(b: u8) -> bool {
	b.is_ascii_alphanumeric() || b == b'_' || b == b'-'
}

/// A byte of a parameter value: printable ASCII other than `;`.
const fn value_byte(b: u8) -> bool {
	b >= 0x21 && b <= 0x7e && b != b';'
}

/// Whether `rest`, placed after a message's parameters, would be read as one
/// more parameter: a `key=value` segment with a `;` after it.
fn leads_with_parameter(rest: &[u8]) -> bool {
	let key = rest.iter().take_while(|&&b| key_byte(b)).count();
	if key == 0 || rest.get(key) != Some(&b'=') {
		return false;
	}
	rest[key + 1..].iter().find(|&&b| !value_byte(b)) == Some(&b';')
}

/// Where the chunk of `body` starting at `start` ends.
fn chunk_end(body: &[u8], start: usize, limit: usize) -> usize {
	let target = start.saturating_add(limit.max(1));
	if target >= body.len() {
		return body.len();
	}
	let safe = |at: usize| body[at] & 0xc0 != 0x80 && !leads_with_parameter(&body[at..]);
	(start + 1..=target)
		.rev()
		.find(|&at| safe(at))
		.or_else(|| (target + 1..body.len()).find(|&at| safe(at)))
		.unwrap_or(body.len())
}

/// Splits a body into chunk bodies exactly as Tern's `tsp_chunks` does.
///
/// Each piece is at most `limit` bytes, cut where a cut is safe: on a UTF-8
/// boundary, and never where the next piece would lead with a
/// parameter-shaped segment. A body within `limit` is one chunk; an empty
/// body has none.
pub fn chunks(body: &[u8], limit: usize) -> impl Iterator<Item = &[u8]> {
	let mut start = 0;
	std::iter::from_fn(move || {
		(start < body.len()).then(|| {
			let end = chunk_end(body, start, limit);
			let piece = &body[start..end];
			start = end;
			piece
		})
	})
}

/// Appends one APC string `ESC _ tsp;<verb>;<params…>;<body> ESC \`.
fn push_apc(out: &mut Vec<u8>, verb: &str, params: &[(&str, &str)], body: &[u8]) {
	out.extend_from_slice(b"\x1b_tsp;");
	out.extend_from_slice(verb.as_bytes());
	out.push(b';');
	for (k, v) in params {
		out.extend_from_slice(k.as_bytes());
		out.push(b'=');
		out.extend_from_slice(v.as_bytes());
		out.push(b';');
	}
	out.extend_from_slice(body);
	out.extend_from_slice(b"\x1b\\");
}

/// Encodes one message of `verb` with `params` and an already serialized
/// `body`, chunked under `limit` with chunk id `chunk` when it is longer.
///
/// # Example
/// ```
/// let out = tern_sdk::wire::encode("o", &[], br#"{"id":"s1"}"#, 65536, "1");
/// assert_eq!(out, b"\x1b_tsp;o;{\"id\":\"s1\"}\x1b\\");
/// ```
pub fn encode(
	verb: &str,
	params: &[(&str, &str)],
	body: &[u8],
	limit: usize,
	chunk: &str,
) -> Vec<u8> {
	let mut out = Vec::with_capacity(body.len() + 16);
	if body.len() <= limit {
		push_apc(&mut out, verb, params, body);
		return out;
	}
	let pieces: Vec<&[u8]> = chunks(body, limit).collect();
	let last = pieces.len() - 1;
	for (i, piece) in pieces.into_iter().enumerate() {
		let mut ps: Vec<(&str, &str)> = if i == 0 { params.to_vec() } else { Vec::new() };
		ps.push(("c", chunk));
		if i < last {
			ps.push(("m", "1"));
		}
		push_apc(&mut out, verb, &ps, piece);
	}
	out
}

/// Encodes messages under the session's `apc` limit, numbering chunk
/// sequences with a base-36 counter.
#[derive(Clone, Debug)]
pub struct Encoder {
	/// The largest body per APC string.
	limit:      usize,
	/// The next chunk id.
	next_chunk: u64,
}

impl Default for Encoder {
	fn default() -> Self {
		Self::new(APC_LIMIT)
	}
}

impl Encoder {
	/// An encoder chunking bodies over `limit` bytes.
	pub const fn new(limit: usize) -> Self {
		Self { limit, next_chunk: 1 }
	}

	/// The limit in use.
	pub const fn limit(&self) -> usize {
		self.limit
	}

	/// Changes the limit (the `hello` reply's `apc`).
	pub const fn set_limit(&mut self, limit: usize) {
		self.limit = limit;
	}

	/// The bytes of `message`, chunked when its body is over the limit.
	///
	/// # Errors
	/// When the body can't be serialized.
	pub fn encode(&mut self, message: &Message) -> Result<Vec<u8>, Error> {
		let body = message.body()?;
		let chunk = if body.len() > self.limit {
			let id = base36(self.next_chunk);
			self.next_chunk += 1;
			id
		} else {
			String::new()
		};
		Ok(encode(message.verb(), &message.params(), body.as_bytes(), self.limit, &chunk))
	}
}

/// `n` in lowercase base 36.
fn base36(mut n: u64) -> String {
	const DIGITS: &[u8; 36] = b"0123456789abcdefghijklmnopqrstuvwxyz";
	let mut out = Vec::new();
	loop {
		out.push(DIGITS[(n % 36) as usize]);
		n /= 36;
		if n == 0 {
			break;
		}
	}
	out.reverse();
	String::from_utf8(out).unwrap_or_default()
}

// ---------------------------------------------------------------------------
// Terminal → program
// ---------------------------------------------------------------------------

/// A cell size in pixels.
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Cell {
	/// Width.
	#[serde(default)]
	pub w: f64,
	/// Height.
	#[serde(default)]
	pub h: f64,
}

/// The `hello` reply: what the terminal draws and supports.
#[derive(Clone, Debug, Default, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Hello {
	/// The version picked (`1`).
	#[serde(default)]
	pub v:             u32,
	/// The terminal (`tern`).
	#[serde(default)]
	pub term:          String,
	/// The terminal's version.
	#[serde(default)]
	pub ver:           String,
	/// Every kind it draws.
	#[serde(default)]
	pub kinds:         Vec<String>,
	/// Terminal features.
	#[serde(default)]
	pub features:      Vec<String>,
	/// The largest body per message.
	#[serde(default = "default_apc")]
	pub apc:           usize,
	/// Frames that may be unacknowledged.
	#[serde(default = "default_credits")]
	pub credits:       u32,
	/// The pane's width in columns.
	#[serde(default)]
	pub cols:          u32,
	/// The cell size.
	#[serde(default)]
	pub cell:          Option<Cell>,
	/// Whether the terminal shows its dark appearance.
	#[serde(default)]
	pub dark:          bool,
	/// Whether Reduce Motion is on.
	#[serde(default)]
	pub reduce_motion: bool,
	/// Whether the user's system reads a 12-hour clock (`3:05 PM`); false
	/// (24-hour) on terminals that don't say.
	#[serde(default)]
	pub hour12:        bool,
	/// The reply as received.
	#[serde(skip)]
	pub raw:           Map<String, Value>,
}

/// The default of `apc`.
const fn default_apc() -> usize {
	APC_LIMIT
}

/// The default of `credits`.
const fn default_credits() -> u32 {
	CREDITS
}

/// The `blobs` reply: which of the asked blobs Tern holds.
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
pub struct BlobsReply {
	/// The ids held, in the order asked.
	#[serde(default)]
	pub have: Vec<String>,
	/// The reply as received.
	#[serde(skip)]
	pub raw:  Map<String, Value>,
}

/// An `r` message: the reply to a query.
#[derive(Clone, Debug, PartialEq)]
pub enum Reply {
	/// `hello`.
	Hello(Hello),
	/// `blobs`.
	Blobs(BlobsReply),
	/// Any other reply, kept raw.
	Unknown(Map<String, Value>),
}

/// Types decoded from a raw object that keep it.
trait Raw: DeserializeOwned {
	/// Stores the raw object.
	fn set_raw(&mut self, raw: Map<String, Value>);
}

/// Decodes `map` as `T`, or `None` when its fields don't fit.
fn typed<T: Raw>(map: Map<String, Value>) -> Result<T, Map<String, Value>> {
	let value = Value::Object(map);
	let decoded = T::deserialize(&value);
	let Value::Object(map) = value else {
		unreachable!("built as an object")
	};
	match decoded {
		Ok(mut t) => {
			t.set_raw(map);
			Ok(t)
		},
		Err(_) => Err(map),
	}
}

impl Reply {
	/// Decodes a reply body; fields that don't fit keep it raw.
	pub fn decode(map: Map<String, Value>) -> Self {
		match map.get("r").and_then(Value::as_str) {
			Some("hello") => typed(map).map_or_else(Self::Unknown, Self::Hello),
			Some("blobs") => typed(map).map_or_else(Self::Unknown, Self::Blobs),
			_ => Self::Unknown(map),
		}
	}

	/// The reply as received.
	pub const fn raw(&self) -> &Map<String, Value> {
		match self {
			Self::Hello(r) => &r.raw,
			Self::Blobs(r) => &r.raw,
			Self::Unknown(r) => r,
		}
	}
}

/// Defines an event payload struct that keeps its raw object.
macro_rules! event_struct {
	($(#[$m:meta])* $name:ident { $($(#[$fm:meta])* $field:ident : $ty:ty),* $(,)? }) => {
		$(#[$m])*
		#[derive(Clone, Debug, Default, PartialEq, Deserialize)]
		#[allow(clippy::derive_partial_eq_without_eq, reason = "some payloads hold floats")]
		pub struct $name {
			$($(#[$fm])* #[serde(default)] pub $field: $ty,)*
			/// The event as received.
			#[serde(skip)]
			pub raw: Map<String, Value>,
		}

		impl Raw for $name {
			fn set_raw(&mut self, raw: Map<String, Value>) {
				self.raw = raw;
			}
		}
	};
}

impl Raw for Hello {
	fn set_raw(&mut self, raw: Map<String, Value>) {
		self.raw = raw;
	}
}

impl Raw for BlobsReply {
	fn set_raw(&mut self, raw: Map<String, Value>) {
		self.raw = raw;
	}
}

event_struct!(
	/// `ack`: the highest frame applied and drawn.
	Ack {
		/// The surface.
		sf: String,
		/// The frame's sequence number.
		s:  u64,
	}
);

event_struct!(
	/// `resize`: the pane's width or cell size changed.
	Resize {
		/// The surface.
		sf: Option<String>,
		/// The width in columns.
		cols: u32,
		/// The cell size.
		cell: Option<Cell>,
		/// Whether the pane is visible.
		visible: Option<bool>,
	}
);

event_struct!(
	/// `theme`: the appearance switched.
	Theme {
		/// Whether it is dark now.
		dark: bool,
	}
);

event_struct!(
	/// `motion`: Reduce Motion was toggled.
	Motion {
		/// Whether motion is reduced now.
		reduce: bool,
	}
);

event_struct!(
	/// `visible`: the pane was hidden or shown.
	Visible {
		/// The surface.
		sf: Option<String>,
		/// Whether it shows.
		visible: bool,
	}
);

event_struct!(
	/// `toggle`: a collapsible node was folded or unfolded.
	Toggle {
		/// The surface.
		sf: Option<String>,
		/// The node.
		id: String,
		/// Whether it is folded now.
		collapsed: bool,
		/// The node's `key` prop (or a `tree` item).
		key: Option<Value>,
	}
);

event_struct!(
	/// `select` or `activate`: an item was picked or opened.
	Select {
		/// The surface.
		sf: Option<String>,
		/// The node (the list, for a list's item).
		id: String,
		/// The item.
		item: String,
		/// Form values, inside an `el` form.
		values: Option<Map<String, Value>>,
	}
);

event_struct!(
	/// `action`: a pointer action with a custom name.
	Action {
		/// The surface.
		sf: Option<String>,
		/// The node.
		id: String,
		/// The action's name (before any `=`).
		act: String,
		/// The part after `=` in the action's name.
		value: Option<String>,
		/// Modifiers held, in the order `shift`, `ctrl`, `alt`, `meta`.
		mods: Vec<String>,
		/// Form values, inside an `el` form.
		values: Option<Map<String, Value>>,
	}
);

event_struct!(
	/// `change`: an `el` control flipped, or a `prefs` row changed.
	Change {
		/// The surface.
		sf: Option<String>,
		/// The node.
		id: String,
		/// The control's value, or the `prefs` row's new value.
		value: Value,
		/// The control's new state.
		checked: Option<bool>,
		/// The control's name.
		name: Option<String>,
		/// The `prefs` row.
		item: Option<String>,
		/// Form values, inside an `el` form.
		values: Option<Map<String, Value>>,
	}
);

event_struct!(
	/// `focus`: a click asked for the keys in a field.
	Focus {
		/// The surface.
		sf: Option<String>,
		/// The field.
		id: String,
	}
);

event_struct!(
	/// `edit`: native editing replaced UTF-16 `[from, to)` with `text`.
	Edit {
		/// The surface.
		sf: Option<String>,
		/// The field.
		id: String,
		/// Start, UTF-16 units.
		from: usize,
		/// End, UTF-16 units.
		to: usize,
		/// The replacement.
		text: String,
		/// The caret afterwards.
		cursor: usize,
		/// The length of the text Tern saw.
		len: usize,
	}
);

event_struct!(
	/// `undo`: undo the last change to a field.
	Undo {
		/// The surface.
		sf: Option<String>,
		/// The field.
		id: String,
	}
);

event_struct!(
	/// `send`: submit text through a composer.
	SendText {
		/// The surface.
		sf: Option<String>,
		/// The field.
		id: String,
		/// The text.
		text: String,
	}
);

event_struct!(
	/// `error`: a rejected op, message, stylesheet or element.
	ErrorEvent {
		/// The surface, when the message named one.
		sf: Option<String>,
		/// The frame.
		s: Option<u64>,
		/// The op's index in the frame.
		op: Option<u64>,
		/// What went wrong.
		msg: String,
		/// The stylesheet.
		sheet: Option<String>,
		/// The element.
		id: Option<String>,
	}
);

event_struct!(
	/// `gone`: nodes or a surface Tern dropped.
	Gone {
		/// The surface.
		sf: Option<String>,
		/// The ids dropped.
		ids: Vec<String>,
	}
);

/// An `e` message: an event.
#[derive(Clone, Debug, PartialEq)]
pub enum Event {
	/// `ack`.
	Ack(Ack),
	/// `resize`.
	Resize(Resize),
	/// `theme`.
	Theme(Theme),
	/// `motion`.
	Motion(Motion),
	/// `visible`.
	Visible(Visible),
	/// `toggle`.
	Toggle(Toggle),
	/// `select`.
	Select(Select),
	/// `activate`.
	Activate(Select),
	/// `action`.
	Action(Action),
	/// `change`.
	Change(Change),
	/// `focus`.
	Focus(Focus),
	/// `edit`.
	Edit(Edit),
	/// `undo`.
	Undo(Undo),
	/// `send`.
	Send(SendText),
	/// `error`.
	Error(ErrorEvent),
	/// `gone`.
	Gone(Gone),
	/// Any other event, or a known one whose fields don't fit, kept raw.
	Unknown(Map<String, Value>),
}

impl Event {
	/// Decodes an event body; fields that don't fit keep it raw.
	pub fn decode(map: Map<String, Value>) -> Self {
		let ev = map
			.get("ev")
			.and_then(Value::as_str)
			.unwrap_or_default()
			.to_owned();
		match ev.as_str() {
			"ack" => typed(map).map_or_else(Self::Unknown, Self::Ack),
			"resize" => typed(map).map_or_else(Self::Unknown, Self::Resize),
			"theme" => typed(map).map_or_else(Self::Unknown, Self::Theme),
			"motion" => typed(map).map_or_else(Self::Unknown, Self::Motion),
			"visible" => typed(map).map_or_else(Self::Unknown, Self::Visible),
			"toggle" => typed(map).map_or_else(Self::Unknown, Self::Toggle),
			"select" => typed(map).map_or_else(Self::Unknown, Self::Select),
			"activate" => typed(map).map_or_else(Self::Unknown, Self::Activate),
			"action" => typed(map).map_or_else(Self::Unknown, Self::Action),
			"change" => typed(map).map_or_else(Self::Unknown, Self::Change),
			"focus" => typed(map).map_or_else(Self::Unknown, Self::Focus),
			"edit" => typed(map).map_or_else(Self::Unknown, Self::Edit),
			"undo" => typed(map).map_or_else(Self::Unknown, Self::Undo),
			"send" => typed(map).map_or_else(Self::Unknown, Self::Send),
			"error" => typed(map).map_or_else(Self::Unknown, Self::Error),
			"gone" => typed(map).map_or_else(Self::Unknown, Self::Gone),
			_ => Self::Unknown(map),
		}
	}

	/// The event as received.
	pub const fn raw(&self) -> &Map<String, Value> {
		match self {
			Self::Ack(e) => &e.raw,
			Self::Resize(e) => &e.raw,
			Self::Theme(e) => &e.raw,
			Self::Motion(e) => &e.raw,
			Self::Visible(e) => &e.raw,
			Self::Toggle(e) => &e.raw,
			Self::Select(e) | Self::Activate(e) => &e.raw,
			Self::Action(e) => &e.raw,
			Self::Change(e) => &e.raw,
			Self::Focus(e) => &e.raw,
			Self::Edit(e) => &e.raw,
			Self::Undo(e) => &e.raw,
			Self::Send(e) => &e.raw,
			Self::Error(e) => &e.raw,
			Self::Gone(e) => &e.raw,
			Self::Unknown(e) => e,
		}
	}

	/// The event's name (`ev`).
	pub fn name(&self) -> &str {
		self
			.raw()
			.get("ev")
			.and_then(Value::as_str)
			.unwrap_or_default()
	}

	/// The surface the event is about, when it names one.
	pub fn sf(&self) -> Option<&str> {
		self.raw().get("sf").and_then(Value::as_str)
	}

	/// The node the event is about, when it names one.
	pub fn id(&self) -> Option<&str> {
		self.raw().get("id").and_then(Value::as_str)
	}
}