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
// Foundation work for #1116/#1175 — see the equivalent header note in
// composer/key_hint.rs for the full rationale. The blanket allow goes away
// when PR 2 wires up the consumers.
//! Reduced port of codex's `codex-rs/protocol/src/user_input.rs`.
//!
//! ## Provenance
//!
//! Selected types ported from `codex-rs/protocol/src/user_input.rs` at
//! upstream commit `d55479488e125ef7a0a8584505d839a22eaf6204`
//! (codex `main` as of 2026-05-01).
//!
//! Verified byte-identical between this SHA and our previous reference
//! `7e8594fc198615068018b198ab86a9ae0a541dff` (`git diff` returns 0
//! lines), so the re-stamp is a pure attribution change.
//!
//! Original work: Copyright (c) OpenAI / codex contributors,
//! licensed under the Apache License, Version 2.0.
//! See `LICENSES/codex-APACHE-2.0` (vendored at the workspace root) for
//! the full license text.
//!
//! ## What was kept vs dropped
//!
//! - **Kept:** [`ByteRange`], [`TextElement`], [`MAX_USER_INPUT_TEXT_CHARS`].
//! These are the primitives the ported [`super::textarea::TextArea`] needs
//! to track non-text spans (e.g. image placeholders) embedded in the
//! composer buffer.
//! - **Dropped:** the `UserInput` enum and its protocol-serialization
//! derive (`schemars::JsonSchema`, `serde::{Deserialize, Serialize}`,
//! `ts_rs::TS`). koda's protocol layer is separate from codex's; we
//! only need the in-memory composer types here. Adding those derives
//! would drag four crates into `koda-cli` for no current benefit.
//!
//! ## Adaptations
//!
//! - Removed all `#[derive(Deserialize, Serialize, TS, JsonSchema)]`
//! from the ported types.
//! - Removed the `UserInput` enum entirely (not used by textarea).
// PR 2 of #1178 swapped the consumers (chat handlers + viewport) to use this
// module, but the codex-port surface includes advanced features (vim mode
// toggle, paste-burst detection, named/highlighted elements, masked render,
// key hints) that are scoped for PR 3+. The unused-warnings will go away as
// each follow-up PR wires them up; until then, allow them at the module
// level so the faithful port can land without piecemeal #[allow] tags.
// PR 5 of #1178 wired the textarea's `insert_element` / element-aware
// rendering into koda's @-mention completion path, which exercises the
// `TextElement` struct + `ByteRange` type alias from this module via the
// textarea's internal use of them. The remaining surface
// (`MAX_USER_INPUT_TEXT_CHARS`, `map_range`, `set_placeholder`,
// `placeholder`) is preserved verbatim from the codex port for sync
// parity — the next codex resync stays a trivial diff. Module-level
// allow rather than per-item because most of the file is sync-parity
// surface; per-item would add 4+ tags to a 167-line file for no gain.
use Range;
/// Conservative cap so one user message cannot monopolize a large context window.
///
/// Ported verbatim from codex; unchanged for koda since the rationale
/// (model context-window protection) is identical.
pub const MAX_USER_INPUT_TEXT_CHARS: usize = 1 << 20;
/// A non-text span inside the composer buffer.
///
/// Used by [`super::textarea::TextArea`] to track placeholders such as
/// pasted image references that should be displayed/persisted as a
/// single logical unit even though they're embedded in the UTF-8 text
/// stream. Cursor motion across an element treats it as one grapheme;
/// deletion removes the whole span atomically.
/// A byte range within a UTF-8 text buffer.
///
/// `start` is inclusive, `end` is exclusive. The range is expected to
/// align to UTF-8 character boundaries (the textarea ensures this on
/// insertion).