ftui-web 0.7.0

WASM backend implementation for FrankenTUI (host-driven, deterministic).
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
//! First-party integration adapters for the FrankenTermJS SDK
//! (bd-2vr05.9.3).
//!
//! # Why this exists
//!
//! Replacing xterm.js in a real app is mostly a *lifecycle wiring* problem:
//! when to call `init`, when it is safe to `attachConnect`, how resize
//! observation feeds `resize`, and — the classic integration bug — what
//! happens when React StrictMode runs your effect twice. The SDK contract
//! (`docs/spec/frankenterm-web-api.md`) defines the method surface; this
//! module defines the **recommended wiring** as an executable, deterministic
//! model instead of prose:
//!
//! * [`AdapterLifecycle`] — a pure state machine encoding the legal call
//!   order for a host adapter (mount → attach → resize/input → detach →
//!   dispose), with per-host-kind semantics: the React adapter treats
//!   StrictMode-style repeated mounts/cleanups as idempotent no-ops, the
//!   vanilla adapter reports them as misuse.
//! * [`recommended_wiring`] — the ordered wiring steps for each adapter
//!   kind, each step naming the *real* stable contract method it calls.
//! * [`vanilla_example`] / [`react_example`] — canonical example sources.
//!   The committed files under `crates/ftui-web/sdk/examples/` must be
//!   byte-identical to these generators (lockstep-tested, exactly like the
//!   `.d.ts` in [`crate::sdk_event_model`]), so shipped examples can never
//!   drift from the modeled lifecycle.
//!
//! Like the rest of the durable SDK surface, this lives in-tree in `ftui-web`
//! while the `frankenterm-web` WASM packaging crate remains out-of-tree; the
//! examples reference only the **Stable Method Surface (`1.0.0`)** from the
//! canonical contract.
//!
//! # Determinism and logging
//!
//! Every applied action (and every rejected misuse) yields a JSONL line with
//! a monotone `seq` and the host-chosen `adapter_id` as the correlation key.
//! The model is timestamp-free so identical action sequences produce
//! byte-identical logs; wall-clock timestamps are added by the E2E harness
//! layer (`scripts/frankenterm_js_sdk_adapter_e2e.sh`) where determinism is
//! not required.

use core::fmt;

/// Schema version for adapter lifecycle events and wiring tables.
pub const ADAPTER_SCHEMA_VERSION: &str = "1.0.0";

/// Stable contract methods referenced by the adapter wiring (a subset of the
/// **Stable Method Surface (`1.0.0`)** in `docs/spec/frankenterm-web-api.md`).
pub const WIRING_CONTRACT_METHODS: [&str; 9] = [
    "apiContract",
    "init",
    "fitToContainer",
    "resize",
    "attachConnect",
    "input",
    "drainEventSubscriptionJsonl",
    "attachClose",
    "destroy",
];

// ============================================================================
// Adapter kinds
// ============================================================================

/// First-party adapter flavors.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AdapterKind {
    /// Plain-DOM adapter: explicit create/dispose, misuse is an error.
    Vanilla,
    /// React adapter (also the Next.js wiring): effect-driven lifecycle where
    /// StrictMode intentionally double-invokes mount/cleanup, so repeated
    /// idempotent actions are deduplicated instead of rejected.
    React,
}

impl AdapterKind {
    /// Both adapter kinds, in stable order.
    pub const ALL: [AdapterKind; 2] = [AdapterKind::Vanilla, AdapterKind::React];

    /// Stable wire string.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            AdapterKind::Vanilla => "vanilla",
            AdapterKind::React => "react",
        }
    }
}

impl fmt::Display for AdapterKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

// ============================================================================
// Lifecycle phases and actions
// ============================================================================

/// Adapter lifecycle phase.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AdapterPhase {
    /// Constructed; no DOM or engine resources yet.
    Created,
    /// Engine initialized into a container (`init` called).
    Mounted,
    /// Transport attached (`attachConnect` succeeded).
    Attached,
    /// Transport detached (`attachClose`), engine still mounted.
    Detached,
    /// Fully torn down (`destroy`); terminal state.
    Disposed,
}

impl AdapterPhase {
    /// Stable wire string.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            AdapterPhase::Created => "created",
            AdapterPhase::Mounted => "mounted",
            AdapterPhase::Attached => "attached",
            AdapterPhase::Detached => "detached",
            AdapterPhase::Disposed => "disposed",
        }
    }
}

/// Host action applied to the adapter (each maps to stable contract calls).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AdapterAction {
    /// Initialize the engine into a container (`init`, then `fitToContainer`).
    Mount,
    /// Connect the transport (`attachConnect`).
    Attach,
    /// Propagate a size change (`resize(cols, rows)`).
    Resize {
        /// New column count.
        cols: u16,
        /// New row count.
        rows: u16,
    },
    /// Forward host input (`input`).
    Input {
        /// Number of host-encoded input bytes forwarded.
        bytes: u32,
    },
    /// Close the transport (`attachClose`).
    Detach,
    /// Tear everything down (`destroy`).
    Dispose,
}

impl AdapterAction {
    /// Stable wire label.
    #[must_use]
    pub const fn label(&self) -> &'static str {
        match self {
            AdapterAction::Mount => "mount",
            AdapterAction::Attach => "attach",
            AdapterAction::Resize { .. } => "resize",
            AdapterAction::Input { .. } => "input",
            AdapterAction::Detach => "detach",
            AdapterAction::Dispose => "dispose",
        }
    }

    fn detail(&self) -> String {
        match self {
            AdapterAction::Resize { cols, rows } => format!("cols={cols} rows={rows}"),
            AdapterAction::Input { bytes } => format!("bytes={bytes}"),
            _ => String::new(),
        }
    }
}

// ============================================================================
// Outcomes, events, and misuse
// ============================================================================

/// How an accepted action was handled.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AdapterOutcome {
    /// The action performed its contract calls and advanced the phase.
    Applied,
    /// React-only: the action repeated an already-satisfied idempotent step
    /// (StrictMode double mount / double cleanup) and was deduplicated.
    StrictModeDeduped,
}

impl AdapterOutcome {
    /// Stable wire string.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            AdapterOutcome::Applied => "applied",
            AdapterOutcome::StrictModeDeduped => "strict_mode_deduped",
        }
    }
}

/// One accepted lifecycle transition (JSONL-serializable).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AdapterEvent {
    /// Monotone per-adapter sequence number (correlation within a run).
    pub seq: u64,
    /// Host-chosen correlation id.
    pub adapter_id: String,
    /// Adapter flavor.
    pub kind: AdapterKind,
    /// Action label.
    pub action: &'static str,
    /// Phase before the action.
    pub phase_before: AdapterPhase,
    /// Phase after the action.
    pub phase_after: AdapterPhase,
    /// Applied vs deduplicated.
    pub outcome: AdapterOutcome,
    /// Action-specific detail (`cols=..`, `bytes=..`), possibly empty.
    pub detail: String,
}

impl AdapterEvent {
    /// Deterministic single-line JSON (timestamp-free; see module docs).
    #[must_use]
    pub fn to_jsonl(&self) -> String {
        format!(
            "{{\"event\":\"adapter_transition\",\"schema\":\"{}\",\"seq\":{},\"adapter_id\":\"{}\",\"adapter\":\"{}\",\"action\":\"{}\",\"phase_before\":\"{}\",\"phase_after\":\"{}\",\"outcome\":\"{}\",\"detail\":\"{}\"}}",
            ADAPTER_SCHEMA_VERSION,
            self.seq,
            escape_json(&self.adapter_id),
            self.kind.as_str(),
            self.action,
            self.phase_before.as_str(),
            self.phase_after.as_str(),
            self.outcome.as_str(),
            escape_json(&self.detail),
        )
    }
}

/// A rejected action: the host called the adapter out of order.
///
/// This is the *adapter-layer* misuse taxonomy (lifecycle ordering), distinct
/// from the engine-level [`crate::sdk_event_model::SdkErrorKind`] taxonomy
/// (protocol/capability/input errors).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AdapterMisuse {
    /// Monotone per-adapter sequence number (shared with accepted events).
    pub seq: u64,
    /// Host-chosen correlation id.
    pub adapter_id: String,
    /// Adapter flavor.
    pub kind: AdapterKind,
    /// Stable dotted misuse code.
    pub code: &'static str,
    /// Action that was rejected.
    pub action: &'static str,
    /// Phase the adapter was in.
    pub phase: AdapterPhase,
    /// Human-readable explanation with the recommended fix.
    pub explanation: &'static str,
}

impl AdapterMisuse {
    /// All stable misuse codes (sorted), for contract tests.
    pub const CODES: [&'static str; 5] = [
        "adapter.already_attached",
        "adapter.disposed",
        "adapter.double_mount",
        "adapter.not_attached",
        "adapter.not_mounted",
    ];

    /// Deterministic single-line JSON for the error timeline.
    #[must_use]
    pub fn to_jsonl(&self) -> String {
        format!(
            "{{\"event\":\"adapter_misuse\",\"schema\":\"{}\",\"seq\":{},\"adapter_id\":\"{}\",\"adapter\":\"{}\",\"code\":\"{}\",\"action\":\"{}\",\"phase\":\"{}\",\"explanation\":\"{}\"}}",
            ADAPTER_SCHEMA_VERSION,
            self.seq,
            escape_json(&self.adapter_id),
            self.kind.as_str(),
            self.code,
            self.action,
            self.phase.as_str(),
            escape_json(self.explanation),
        )
    }
}

fn escape_json(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for ch in text.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out
}

// ============================================================================
// The lifecycle state machine
// ============================================================================

/// Deterministic adapter lifecycle model.
#[derive(Debug, Clone)]
pub struct AdapterLifecycle {
    kind: AdapterKind,
    adapter_id: String,
    phase: AdapterPhase,
    seq: u64,
}

impl AdapterLifecycle {
    /// Create a fresh adapter in [`AdapterPhase::Created`].
    #[must_use]
    pub fn new(kind: AdapterKind, adapter_id: impl Into<String>) -> Self {
        Self {
            kind,
            adapter_id: adapter_id.into(),
            phase: AdapterPhase::Created,
            seq: 0,
        }
    }

    /// Current phase.
    #[must_use]
    pub const fn phase(&self) -> AdapterPhase {
        self.phase
    }

    /// Adapter flavor.
    #[must_use]
    pub const fn kind(&self) -> AdapterKind {
        self.kind
    }

    /// Apply a host action, returning the transition event or the misuse.
    ///
    /// # Errors
    ///
    /// Returns [`AdapterMisuse`] when the action is illegal in the current
    /// phase for this adapter kind. React deduplicates StrictMode-style
    /// repeats of idempotent steps (mount when mounted, detach when detached,
    /// dispose when disposed); vanilla reports them as misuse.
    pub fn apply(&mut self, action: &AdapterAction) -> Result<AdapterEvent, AdapterMisuse> {
        self.seq += 1;
        let phase_before = self.phase;
        let react = self.kind == AdapterKind::React;

        let decided: Result<(AdapterPhase, AdapterOutcome), (&'static str, &'static str)> =
            match (action, self.phase) {
                // ── Mount ────────────────────────────────────────────
                (AdapterAction::Mount, AdapterPhase::Created) => {
                    Ok((AdapterPhase::Mounted, AdapterOutcome::Applied))
                }
                // Detached still has an INITIALIZED engine (attachClose only
                // closed the transport), so a second `init` is a double
                // mount, not a re-mount: React dedups it (mount already
                // satisfied, phase unchanged), vanilla reports misuse.
                (AdapterAction::Mount, AdapterPhase::Mounted | AdapterPhase::Detached) if react => {
                    Ok((self.phase, AdapterOutcome::StrictModeDeduped))
                }
                (
                    AdapterAction::Mount,
                    AdapterPhase::Mounted | AdapterPhase::Attached | AdapterPhase::Detached,
                ) => Err((
                    "adapter.double_mount",
                    "init was already called for this container; vanilla hosts must dispose \
                     before re-mounting (React hosts get StrictMode dedup instead)",
                )),
                // ── Attach ───────────────────────────────────────────
                (AdapterAction::Attach, AdapterPhase::Mounted | AdapterPhase::Detached) => {
                    Ok((AdapterPhase::Attached, AdapterOutcome::Applied))
                }
                (AdapterAction::Attach, AdapterPhase::Attached) if react => {
                    Ok((AdapterPhase::Attached, AdapterOutcome::StrictModeDeduped))
                }
                (AdapterAction::Attach, AdapterPhase::Attached) => Err((
                    "adapter.already_attached",
                    "attachConnect was already called; call attachClose before reconnecting",
                )),
                (AdapterAction::Attach, AdapterPhase::Created) => Err((
                    "adapter.not_mounted",
                    "call init (mount) before attachConnect: the engine must exist before \
                     the transport",
                )),
                // ── Resize ───────────────────────────────────────────
                // Legal from Mounted onward: fitToContainer/resize commonly
                // run between init and attachConnect.
                (
                    AdapterAction::Resize { .. },
                    AdapterPhase::Mounted | AdapterPhase::Attached | AdapterPhase::Detached,
                ) => Ok((self.phase, AdapterOutcome::Applied)),
                (AdapterAction::Resize { .. }, AdapterPhase::Created) => Err((
                    "adapter.not_mounted",
                    "resize requires an initialized engine; call init first",
                )),
                // ── Input ────────────────────────────────────────────
                (AdapterAction::Input { .. }, AdapterPhase::Attached) => {
                    Ok((AdapterPhase::Attached, AdapterOutcome::Applied))
                }
                (
                    AdapterAction::Input { .. },
                    AdapterPhase::Created | AdapterPhase::Mounted | AdapterPhase::Detached,
                ) => Err((
                    "adapter.not_attached",
                    "input requires a connected transport; call attachConnect first",
                )),
                // ── Detach ───────────────────────────────────────────
                (AdapterAction::Detach, AdapterPhase::Attached) => {
                    Ok((AdapterPhase::Detached, AdapterOutcome::Applied))
                }
                (AdapterAction::Detach, AdapterPhase::Detached | AdapterPhase::Mounted)
                    if react =>
                {
                    Ok((self.phase, AdapterOutcome::StrictModeDeduped))
                }
                (
                    AdapterAction::Detach,
                    AdapterPhase::Created | AdapterPhase::Mounted | AdapterPhase::Detached,
                ) => Err((
                    "adapter.not_attached",
                    "attachClose requires a connected transport",
                )),
                // ── Dispose ──────────────────────────────────────────
                // Teardown must be reachable from every live phase.
                (AdapterAction::Dispose, AdapterPhase::Disposed) if react => {
                    Ok((AdapterPhase::Disposed, AdapterOutcome::StrictModeDeduped))
                }
                (AdapterAction::Dispose, AdapterPhase::Disposed) => Err((
                    "adapter.disposed",
                    "destroy was already called; create a new adapter instead of reusing \
                     a disposed one",
                )),
                (AdapterAction::Dispose, _) => {
                    Ok((AdapterPhase::Disposed, AdapterOutcome::Applied))
                }
                // ── Anything on a disposed adapter ───────────────────
                (_, AdapterPhase::Disposed) => Err((
                    "adapter.disposed",
                    "the adapter was destroyed; create a new adapter instead of reusing \
                     a disposed one",
                )),
            };

        match decided {
            Ok((phase_after, outcome)) => {
                self.phase = phase_after;
                Ok(AdapterEvent {
                    seq: self.seq,
                    adapter_id: self.adapter_id.clone(),
                    kind: self.kind,
                    action: action.label(),
                    phase_before,
                    phase_after,
                    outcome,
                    detail: action.detail(),
                })
            }
            Err((code, explanation)) => Err(AdapterMisuse {
                seq: self.seq,
                adapter_id: self.adapter_id.clone(),
                kind: self.kind,
                code,
                action: action.label(),
                phase: phase_before,
                explanation,
            }),
        }
    }
}

// ============================================================================
// Recommended wiring
// ============================================================================

/// One ordered wiring step for an adapter integration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WiringStep {
    /// 1-based order.
    pub step: u8,
    /// Stable contract method (or `"-"` for host-side-only steps).
    pub method: &'static str,
    /// What the step does and why it sits at this position.
    pub note: &'static str,
}

/// The recommended, ordered wiring for each adapter kind, naming the real
/// stable contract methods. This is the executable form of the "framework
/// wiring" section in the migration guide.
#[must_use]
pub fn recommended_wiring(kind: AdapterKind) -> Vec<WiringStep> {
    let mut steps = Vec::new();
    let mut order = 0u8;
    let mut push = |method: &'static str, note: &'static str| {
        order += 1;
        steps.push(WiringStep {
            step: order,
            method,
            note,
        });
    };

    if kind == AdapterKind::React {
        push(
            "-",
            "mark the component `'use client'` and guard on `typeof window` so Next.js \
             never runs engine code during SSR",
        );
    }
    push(
        "apiContract",
        "pin the contract: verify apiLine and a `1.` apiVersion before any other call",
    );
    push(
        "init",
        "initialize the engine into the container element (the Mount action)",
    );
    push(
        "fitToContainer",
        "size the grid to the container before first paint; wire a ResizeObserver to \
         keep it sized (fitToContainer again, or resize(cols, rows) when the host \
         computes the grid itself)",
    );
    push(
        "attachConnect",
        "connect the transport only after init succeeded (the Attach action)",
    );
    push(
        "input",
        "forward host-encoded input; input is only legal while attached",
    );
    push(
        "drainEventSubscriptionJsonl",
        "drain typed events on the host's schedule (drain-driven, not push-driven)",
    );
    push(
        "attachClose",
        "teardown step 1: close the transport (the Detach action)",
    );
    push(
        "destroy",
        "teardown step 2: destroy the engine (the Dispose action); React cleanup runs \
         this in the effect destructor and StrictMode may run it twice — the adapter \
         dedups the repeat",
    );
    steps
}

// ============================================================================
// Canonical examples (lockstep with crates/ftui-web/sdk/examples/)
// ============================================================================

/// Canonical vanilla-DOM adapter example. The committed
/// `sdk/examples/frankenterm-adapter-vanilla.js` must equal this exactly.
#[must_use]
pub fn vanilla_example() -> String {
    VANILLA_EXAMPLE.to_string()
}

/// Canonical React/Next adapter example. The committed
/// `sdk/examples/frankenterm-adapter-react.tsx` must equal this exactly.
#[must_use]
pub fn react_example() -> String {
    REACT_EXAMPLE.to_string()
}

const VANILLA_EXAMPLE: &str = r#"// FrankenTermJS first-party vanilla adapter (bd-2vr05.9.3).
// Lifecycle contract: mount -> attach -> resize/input -> detach -> dispose.
// Generated in lockstep with ftui-web's sdk_adapter model; do not hand-edit.

export function createFrankenTermAdapter(FrankenTermWeb, container, transportUrl) {
  // Step 1: pin the contract before any other call.
  const contract = FrankenTermWeb.apiContract();
  if (contract.apiLine !== "frankenterm-js" || !String(contract.apiVersion).startsWith("1.")) {
    throw new Error(`unsupported FrankenTermWeb contract: ${contract.apiVersion}`);
  }

  // Step 2 (Mount): initialize the engine into the container.
  const term = FrankenTermWeb.init(container);

  // Step 3: size to the container now, then keep sizing on changes.
  term.fitToContainer();
  const resizeObserver = new ResizeObserver(() => term.fitToContainer());
  resizeObserver.observe(container);

  // Step 4 (Attach): connect the transport only after init succeeded.
  term.attachConnect(transportUrl);

  // Step 5: forward host input (legal only while attached).
  const onKeyDown = (domEvent) => term.input(domEvent);
  container.addEventListener("keydown", onKeyDown);

  // Step 6: drain typed events on the host's schedule (drain-driven).
  const drainTimer = setInterval(() => {
    for (const line of term.drainEventSubscriptionJsonl()) {
      handleTerminalEvent(JSON.parse(line));
    }
  }, 16);

  // Teardown order matters: detach the transport, then destroy the engine.
  // Vanilla hosts must call dispose() exactly once.
  return {
    term,
    dispose() {
      clearInterval(drainTimer);
      container.removeEventListener("keydown", onKeyDown);
      resizeObserver.disconnect();
      term.attachClose(); // Detach
      term.destroy(); // Dispose
    },
  };
}

function handleTerminalEvent(event) {
  // Route by the typed taxonomy from sdk/frankenterm-js-events.d.ts.
  console.debug("frankenterm event", event.type, event);
}
"#;

const REACT_EXAMPLE: &str = r#"// FrankenTermJS first-party React adapter — also the Next.js wiring
// (bd-2vr05.9.3). Lifecycle contract: mount -> attach -> resize/input ->
// detach -> dispose, driven from a single effect. React StrictMode runs
// setup -> cleanup -> setup in development; because the cleanup below fully
// tears down the engine, the second setup starts from a clean container.
// (The adapter model additionally dedups repeated idempotent steps for
// hosts that keep one adapter instance across effect runs.)
// Generated in lockstep with ftui-web's sdk_adapter model; do not hand-edit.
"use client";

import { useEffect, useRef } from "react";

export function FrankenTerm({ FrankenTermWeb, transportUrl, onEvent }) {
  const containerRef = useRef(null);

  useEffect(() => {
    // Next.js SSR guard: engine code is browser-only.
    if (typeof window === "undefined" || !containerRef.current) {
      return undefined;
    }
    const container = containerRef.current;

    // Step 1: pin the contract before any other call.
    const contract = FrankenTermWeb.apiContract();
    if (contract.apiLine !== "frankenterm-js" || !String(contract.apiVersion).startsWith("1.")) {
      throw new Error(`unsupported FrankenTermWeb contract: ${contract.apiVersion}`);
    }

    // Step 2 (Mount): initialize the engine into the container.
    const term = FrankenTermWeb.init(container);

    // Step 3: size to the container now, then keep sizing on changes.
    term.fitToContainer();
    const resizeObserver = new ResizeObserver(() => term.fitToContainer());
    resizeObserver.observe(container);

    // Step 4 (Attach): connect the transport only after init succeeded.
    term.attachConnect(transportUrl);

    // Step 5: forward host input (legal only while attached).
    const onKeyDown = (domEvent) => term.input(domEvent);
    container.addEventListener("keydown", onKeyDown);

    // Step 6: drain typed events on the host's schedule (drain-driven).
    const drainTimer = setInterval(() => {
      for (const line of term.drainEventSubscriptionJsonl()) {
        onEvent?.(JSON.parse(line));
      }
    }, 16);

    // Effect cleanup IS the teardown: detach, then destroy. StrictMode runs
    // cleanup between its two dev-mode setups, so each setup gets a fresh
    // engine; the adapter model dedups repeats defensively.
    return () => {
      clearInterval(drainTimer);
      container.removeEventListener("keydown", onKeyDown);
      resizeObserver.disconnect();
      term.attachClose(); // Detach
      term.destroy(); // Dispose
    };
  }, [FrankenTermWeb, transportUrl, onEvent]);

  return <div ref={containerRef} style={{ width: "100%", height: "100%" }} />;
}
"#;

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn lifecycle(kind: AdapterKind) -> AdapterLifecycle {
        AdapterLifecycle::new(kind, "term-1")
    }

    #[test]
    fn happy_path_reaches_disposed_for_both_kinds() {
        for kind in AdapterKind::ALL {
            let mut adapter = lifecycle(kind);
            for action in [
                AdapterAction::Mount,
                AdapterAction::Resize { cols: 80, rows: 24 },
                AdapterAction::Attach,
                AdapterAction::Input { bytes: 3 },
                AdapterAction::Resize {
                    cols: 100,
                    rows: 30,
                },
                AdapterAction::Detach,
                AdapterAction::Dispose,
            ] {
                let event = adapter
                    .apply(&action)
                    .unwrap_or_else(|m| panic!("{kind}: {} rejected: {}", action.label(), m.code));
                assert_eq!(event.outcome, AdapterOutcome::Applied);
            }
            assert_eq!(adapter.phase(), AdapterPhase::Disposed);
        }
    }

    #[test]
    fn react_strict_mode_double_mount_and_double_cleanup_are_deduped() {
        let mut adapter = lifecycle(AdapterKind::React);
        assert_eq!(
            adapter.apply(&AdapterAction::Mount).expect("mount").outcome,
            AdapterOutcome::Applied
        );
        // StrictMode re-runs the effect body.
        assert_eq!(
            adapter
                .apply(&AdapterAction::Mount)
                .expect("strict remount")
                .outcome,
            AdapterOutcome::StrictModeDeduped
        );
        adapter.apply(&AdapterAction::Attach).expect("attach");
        assert_eq!(
            adapter
                .apply(&AdapterAction::Attach)
                .expect("strict reattach")
                .outcome,
            AdapterOutcome::StrictModeDeduped
        );
        adapter.apply(&AdapterAction::Detach).expect("detach");
        // StrictMode re-runs the cleanup.
        assert_eq!(
            adapter
                .apply(&AdapterAction::Detach)
                .expect("strict re-detach")
                .outcome,
            AdapterOutcome::StrictModeDeduped
        );
        adapter.apply(&AdapterAction::Dispose).expect("dispose");
        assert_eq!(
            adapter
                .apply(&AdapterAction::Dispose)
                .expect("strict re-dispose")
                .outcome,
            AdapterOutcome::StrictModeDeduped
        );
        assert_eq!(adapter.phase(), AdapterPhase::Disposed);
    }

    #[test]
    fn vanilla_rejects_the_same_repeats_react_dedups() {
        let mut adapter = lifecycle(AdapterKind::Vanilla);
        adapter.apply(&AdapterAction::Mount).expect("mount");
        let misuse = adapter
            .apply(&AdapterAction::Mount)
            .expect_err("double mount");
        assert_eq!(misuse.code, "adapter.double_mount");

        adapter.apply(&AdapterAction::Attach).expect("attach");
        let misuse = adapter
            .apply(&AdapterAction::Attach)
            .expect_err("double attach");
        assert_eq!(misuse.code, "adapter.already_attached");

        adapter.apply(&AdapterAction::Detach).expect("detach");
        adapter.apply(&AdapterAction::Dispose).expect("dispose");
        let misuse = adapter
            .apply(&AdapterAction::Dispose)
            .expect_err("double dispose");
        assert_eq!(misuse.code, "adapter.disposed");
    }

    #[test]
    fn ordering_violations_are_named_misuse() {
        // Attach before mount.
        let mut adapter = lifecycle(AdapterKind::Vanilla);
        assert_eq!(
            adapter
                .apply(&AdapterAction::Attach)
                .expect_err("no mount")
                .code,
            "adapter.not_mounted"
        );
        // Input before attach.
        adapter.apply(&AdapterAction::Mount).expect("mount");
        assert_eq!(
            adapter
                .apply(&AdapterAction::Input { bytes: 1 })
                .expect_err("no attach")
                .code,
            "adapter.not_attached"
        );
        // Resize before mount.
        let mut fresh = lifecycle(AdapterKind::React);
        assert_eq!(
            fresh
                .apply(&AdapterAction::Resize { cols: 1, rows: 1 })
                .expect_err("no mount")
                .code,
            "adapter.not_mounted"
        );
        // Anything after dispose (React dedups only dispose itself).
        let mut disposed = lifecycle(AdapterKind::React);
        disposed.apply(&AdapterAction::Mount).expect("mount");
        disposed.apply(&AdapterAction::Dispose).expect("dispose");
        assert_eq!(
            disposed
                .apply(&AdapterAction::Mount)
                .expect_err("mount after dispose")
                .code,
            "adapter.disposed"
        );
    }

    #[test]
    fn dispose_is_reachable_from_every_live_phase() {
        for kind in AdapterKind::ALL {
            // From Created.
            let mut a = lifecycle(kind);
            a.apply(&AdapterAction::Dispose)
                .expect("dispose from created");
            // From Mounted.
            let mut b = lifecycle(kind);
            b.apply(&AdapterAction::Mount).expect("mount");
            b.apply(&AdapterAction::Dispose)
                .expect("dispose from mounted");
            // From Attached.
            let mut c = lifecycle(kind);
            c.apply(&AdapterAction::Mount).expect("mount");
            c.apply(&AdapterAction::Attach).expect("attach");
            c.apply(&AdapterAction::Dispose)
                .expect("dispose from attached");
        }
    }

    #[test]
    fn mount_while_detached_is_a_double_mount_not_a_remount() {
        // Detached keeps the engine initialized; a second init must never be
        // silently accepted as a fresh mount.
        let mut vanilla = lifecycle(AdapterKind::Vanilla);
        vanilla.apply(&AdapterAction::Mount).expect("mount");
        vanilla.apply(&AdapterAction::Attach).expect("attach");
        vanilla.apply(&AdapterAction::Detach).expect("detach");
        let misuse = vanilla
            .apply(&AdapterAction::Mount)
            .expect_err("init on a detached-but-initialized engine");
        assert_eq!(misuse.code, "adapter.double_mount");
        assert_eq!(vanilla.phase(), AdapterPhase::Detached, "phase unchanged");

        let mut react = lifecycle(AdapterKind::React);
        react.apply(&AdapterAction::Mount).expect("mount");
        react.apply(&AdapterAction::Attach).expect("attach");
        react.apply(&AdapterAction::Detach).expect("detach");
        let event = react
            .apply(&AdapterAction::Mount)
            .expect("react dedups the repeat");
        assert_eq!(event.outcome, AdapterOutcome::StrictModeDeduped);
        assert_eq!(react.phase(), AdapterPhase::Detached, "dedup keeps phase");
    }

    #[test]
    fn remount_after_detach_supports_reconnect_flows() {
        let mut adapter = lifecycle(AdapterKind::Vanilla);
        adapter.apply(&AdapterAction::Mount).expect("mount");
        adapter.apply(&AdapterAction::Attach).expect("attach");
        adapter.apply(&AdapterAction::Detach).expect("detach");
        // Reconnect without re-initializing the engine.
        adapter.apply(&AdapterAction::Attach).expect("re-attach");
        assert_eq!(adapter.phase(), AdapterPhase::Attached);
    }

    #[test]
    fn jsonl_lines_are_deterministic_and_carry_correlation() {
        let run = || -> Vec<String> {
            let mut adapter = AdapterLifecycle::new(AdapterKind::React, "corr-7");
            let mut lines = Vec::new();
            for action in [
                AdapterAction::Mount,
                AdapterAction::Attach,
                AdapterAction::Input { bytes: 5 },
                AdapterAction::Dispose,
            ] {
                lines.push(adapter.apply(&action).expect("apply").to_jsonl());
            }
            lines.push(
                adapter
                    .apply(&AdapterAction::Attach)
                    .expect_err("attach after dispose")
                    .to_jsonl(),
            );
            lines
        };
        let a = run();
        let b = run();
        assert_eq!(a, b, "identical action sequences must log byte-identically");
        for line in &a {
            let parsed: serde_json::Value =
                serde_json::from_str(line).expect("adapter JSONL parses");
            assert_eq!(parsed["adapter_id"].as_str(), Some("corr-7"));
            assert_eq!(parsed["schema"].as_str(), Some(ADAPTER_SCHEMA_VERSION));
            assert!(parsed["seq"].as_u64().is_some());
        }
        // seq is monotone across accepted AND rejected actions.
        let seqs: Vec<u64> = a
            .iter()
            .map(|l| {
                serde_json::from_str::<serde_json::Value>(l).expect("parses")["seq"]
                    .as_u64()
                    .expect("seq")
            })
            .collect();
        assert!(seqs.windows(2).all(|w| w[1] == w[0] + 1));
    }

    #[test]
    fn misuse_codes_are_stable_sorted_and_exercised() {
        let mut sorted = AdapterMisuse::CODES;
        sorted.sort_unstable();
        assert_eq!(sorted, AdapterMisuse::CODES, "codes must stay sorted");
    }

    #[test]
    fn wiring_references_only_stable_contract_methods() {
        for kind in AdapterKind::ALL {
            let wiring = recommended_wiring(kind);
            assert!(wiring.len() >= 8, "wiring must cover the full lifecycle");
            for step in &wiring {
                assert!(
                    step.method == "-" || WIRING_CONTRACT_METHODS.contains(&step.method),
                    "wiring step {} references non-contract method `{}`",
                    step.step,
                    step.method
                );
            }
            // Order is 1..=n with no gaps.
            for (idx, step) in wiring.iter().enumerate() {
                assert_eq!(usize::from(step.step), idx + 1);
            }
            // Teardown order: attachClose strictly before destroy.
            let pos = |m: &str| wiring.iter().position(|s| s.method == m).expect(m);
            assert!(pos("init") < pos("attachConnect"));
            assert!(pos("attachConnect") < pos("attachClose"));
            assert!(pos("attachClose") < pos("destroy"));
        }
        // React wiring leads with the SSR guard.
        assert_eq!(recommended_wiring(AdapterKind::React)[0].method, "-");
    }

    #[test]
    fn examples_embed_every_wired_contract_method() {
        for (example, kind) in [
            (vanilla_example(), AdapterKind::Vanilla),
            (react_example(), AdapterKind::React),
        ] {
            for step in recommended_wiring(kind) {
                if step.method != "-" {
                    assert!(
                        example.contains(step.method),
                        "{kind} example is missing wired method `{}`",
                        step.method
                    );
                }
            }
        }
        // The React example must carry the Next.js markers.
        let react = react_example();
        assert!(react.contains("\"use client\""));
        assert!(react.contains("typeof window"));
        assert!(react.contains("StrictMode"));
    }

    #[test]
    fn committed_example_files_are_in_lockstep_with_the_generators() {
        let vanilla_committed = include_str!("../sdk/examples/frankenterm-adapter-vanilla.js");
        assert_eq!(
            vanilla_committed,
            vanilla_example(),
            "sdk/examples/frankenterm-adapter-vanilla.js drifted from sdk_adapter::vanilla_example()"
        );
        let react_committed = include_str!("../sdk/examples/frankenterm-adapter-react.tsx");
        assert_eq!(
            react_committed,
            react_example(),
            "sdk/examples/frankenterm-adapter-react.tsx drifted from sdk_adapter::react_example()"
        );
    }
}