minicode-ai 0.9.29 → 0.11.0

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 (141) hide show
  1. package/AGENTS.md +45 -0
  2. package/CHANGELOG.md +79 -1
  3. package/PLAN.md +491 -0
  4. package/README.md +2 -2
  5. package/cli/args.ts +1 -0
  6. package/cli/commands/acp.ts +485 -0
  7. package/cli/commands/auth.ts +7 -4
  8. package/cli/commands/config.ts +41 -27
  9. package/cli/commands/exec.ts +33 -12
  10. package/cli/commands/mcp.ts +3 -2
  11. package/cli/commands/memory.ts +1 -1
  12. package/cli/commands/pricing.ts +6 -4
  13. package/cli/commands/providers.ts +18 -8
  14. package/cli/commands/sessions.ts +11 -4
  15. package/cli/commands/skills.ts +3 -3
  16. package/cli/commands/stats.ts +3 -1
  17. package/cli/commands.ts +225 -103
  18. package/cli/index.ts +89 -62
  19. package/cli/model-manager.ts +8 -8
  20. package/cli/provider-manager.ts +13 -13
  21. package/cli/router.ts +7 -0
  22. package/cli/setup.ts +349 -4
  23. package/cli/tui.ts +542 -0
  24. package/cli/wizard.ts +2 -1
  25. package/docs/ARCHITECTURE.html +62 -36
  26. package/docs/CONTROL-PLANE-MAP.md +139 -0
  27. package/docs/HARNESS.md +150 -0
  28. package/docs/PLAN_UIUX_V6.md +209 -0
  29. package/docs/README.md +1 -1
  30. package/docs/SUMMARY.md +52 -0
  31. package/docs/TERMINAL_CONTRACT.md +367 -0
  32. package/docs/UI_RENDER_PIPELINE.md +165 -0
  33. package/docs/USAGE.md +11 -0
  34. package/docs/agents.md +31 -0
  35. package/docs/architecture.md +3 -3
  36. package/docs/choosing-mode.md +34 -0
  37. package/docs/cli.md +130 -0
  38. package/docs/concepts.md +53 -0
  39. package/docs/config-providers.md +87 -0
  40. package/docs/config.md +63 -0
  41. package/docs/contributing.md +79 -0
  42. package/docs/environment.md +95 -0
  43. package/docs/exec.md +73 -0
  44. package/docs/getting-started.md +102 -0
  45. package/docs/glossary.md +37 -0
  46. package/docs/mcp-lsp.md +54 -0
  47. package/docs/memory-sessions.md +63 -0
  48. package/docs/policy-sandbox.md +72 -0
  49. package/docs/pricing-budget.md +49 -0
  50. package/docs/quickstart.md +60 -0
  51. package/docs/repl.md +67 -0
  52. package/docs/security-model.md +95 -0
  53. package/docs/security.md +91 -0
  54. package/docs/skills.md +48 -0
  55. package/docs/terminal.md +79 -0
  56. package/docs/tools.md +123 -0
  57. package/docs/troubleshooting.md +40 -0
  58. package/docs/verify-benchmark.md +58 -0
  59. package/package.json +8 -5
  60. package/src/app/provider-layer.ts +12 -4
  61. package/src/app/rag-layer.ts +7 -2
  62. package/src/app/session.ts +6 -0
  63. package/src/app/tool-layer.ts +2 -1
  64. package/src/config.ts +30 -1
  65. package/src/constants.ts +35 -0
  66. package/src/lib/net.ts +6 -1
  67. package/src/lsp/client.ts +41 -8
  68. package/src/mcp/http-transport.ts +52 -15
  69. package/src/mcp/transport.ts +32 -1
  70. package/src/memory/files.ts +6 -5
  71. package/src/policy/allowlist.ts +1 -1
  72. package/src/policy/bash-guard.ts +213 -17
  73. package/src/policy/compaction.ts +57 -3
  74. package/src/policy/context.ts +4 -0
  75. package/src/policy/jail.ts +49 -4
  76. package/src/policy/permission.ts +152 -12
  77. package/src/policy/pricing.ts +12 -9
  78. package/src/policy/update-check.ts +4 -1
  79. package/src/presentation/adapter.ts +721 -0
  80. package/src/presentation/events.ts +324 -0
  81. package/src/presentation/label.ts +140 -0
  82. package/src/presentation/model.ts +172 -0
  83. package/src/presentation/reducer.ts +486 -0
  84. package/src/presentation/store.ts +256 -0
  85. package/src/providers/anthropic.ts +22 -4
  86. package/src/providers/detect.ts +2 -1
  87. package/src/providers/router.ts +13 -0
  88. package/src/repo/repomap.ts +2 -29
  89. package/src/session/checkpoint.ts +27 -9
  90. package/src/session/journal.ts +27 -5
  91. package/src/skills/loader.ts +88 -5
  92. package/src/telemetry/trace.ts +36 -15
  93. package/src/tools/ask_user.ts +73 -4
  94. package/src/tools/bash.ts +23 -17
  95. package/src/tools/glob.ts +10 -11
  96. package/src/tools/lsp.ts +3 -56
  97. package/src/tools/memory.ts +13 -12
  98. package/src/tools/task.ts +7 -1
  99. package/src/tools/web_fetch.ts +11 -2
  100. package/src/tools/web_search.ts +4 -1
  101. package/src/ui/approval/prompt.ts +96 -22
  102. package/src/ui/assistant/simple.ts +188 -44
  103. package/src/ui/assistant/turn-status.ts +32 -10
  104. package/src/ui/contract.ts +119 -0
  105. package/src/ui/footer.ts +35 -14
  106. package/src/ui/i18n/en.ts +253 -0
  107. package/src/ui/i18n/id.ts +255 -0
  108. package/src/ui/i18n/locale.ts +68 -0
  109. package/src/ui/input/input.ts +189 -81
  110. package/src/ui/input/prompt-engine.ts +139 -4
  111. package/src/ui/render/collapse.ts +20 -13
  112. package/src/ui/render/diff.ts +7 -2
  113. package/src/ui/render/errors.ts +75 -50
  114. package/src/ui/render/format.ts +26 -8
  115. package/src/ui/render/highlight.ts +12 -5
  116. package/src/ui/render/markdown.ts +5 -1
  117. package/src/ui/render/sanitize.ts +29 -2
  118. package/src/ui/render/table.ts +12 -6
  119. package/src/ui/render/theme.ts +23 -4
  120. package/src/ui/render/width.ts +104 -17
  121. package/src/ui/runtime/motion.ts +16 -0
  122. package/src/ui/runtime/screen.ts +242 -0
  123. package/src/ui/runtime/spinner.ts +50 -4
  124. package/src/ui/runtime/statusline.ts +50 -1
  125. package/src/ui/screens/dialog.ts +174 -0
  126. package/src/ui/screens/form.ts +486 -0
  127. package/src/ui/screens/model-manager.ts +289 -134
  128. package/src/ui/screens/picker.ts +264 -109
  129. package/src/ui/screens/provider-manager.ts +385 -172
  130. package/src/ui/screens/wizard.ts +165 -102
  131. package/src/ui/tui/app.ts +951 -0
  132. package/src/ui/tui/transcript.ts +475 -0
  133. package/vendor/minicore/LICENSE +21 -0
  134. package/vendor/minicore/VENDOR.md +6 -3
  135. package/vendor/minicore/package.json +6 -3
  136. package/vendor/minicore/src/core/loop.ts +12 -2
  137. package/vendor/minicore/src/core/snapshot.ts +4 -0
  138. package/vendor/minicore/src/providers/openai-compat.ts +76 -25
  139. package/cli/repl.ts +0 -740
  140. package/src/ui/runtime/chrome.ts +0 -369
  141. package/src/ui/screens/overlay.ts +0 -61
@@ -0,0 +1,486 @@
1
+ // Form dalam popup — SEMUA input teks/pilihan terjadi DI DALAM kotak.
2
+ //
3
+ // Latar: popup komposit hanya bisa melukis daftar; tiap input teks memakai
4
+ // askLine warisan yang menggambar di kursor terminal (di LUAR kotak).
5
+ // Hasilnya jendela yang mengusir user keluar untuk mengetik. Form ini
6
+ // menggantikannya: field berlabel + validasi + error inline, satu kotak,
7
+ // Tab pindah field, Enter simpan, Esc batal. Secret di-mask `•`.
8
+ //
9
+ // Pemilik layar (App/popup parent) tetap di luar: form melukis regionnya via
10
+ // screen.handle sendiri (openAltScreen nested = paint-through) dan memarkir
11
+ // kursor terminal di posisi ketik. Tutup = clearRegion + close.
12
+
13
+ import { t } from "../i18n/locale.ts"
14
+ import {
15
+ createDecoderState,
16
+ createKeyStreamPump,
17
+ type DecodedKey,
18
+ type DecoderState,
19
+ toGraphemes,
20
+ } from "../input/prompt-engine.ts"
21
+ import { sanitizeAnsiLine } from "../render/sanitize.ts"
22
+ import { c, stripAnsi } from "../render/theme.ts"
23
+ import { displayWidth, truncateToWidth } from "../render/width.ts"
24
+ import { type AltScreen, SYNC_UPDATE_END, SYNC_UPDATE_START } from "../runtime/screen.ts"
25
+ import { boxLeftPad, dialogBox } from "./dialog.ts"
26
+
27
+ export interface FormField {
28
+ id: string
29
+ label: string
30
+ kind: "text" | "secret" | "select" | "confirm"
31
+ /** Nilai awal (text/secret) atau indeks opsi (select). Confirm: boolean. */
32
+ initial?: string | number | boolean
33
+ /** Pilihan untuk select (min 1). Confirm memakai Ya/Tidak bawaan. */
34
+ options?: string[]
35
+ /** Validasi: pesan error Indonesia atau null bila sah. */
36
+ validate?: (value: string) => string | null
37
+ }
38
+
39
+ export interface FormSpec {
40
+ title: string
41
+ fields: FormField[]
42
+ footer?: string
43
+ }
44
+
45
+ export interface FormResult {
46
+ cancelled: boolean
47
+ values?: Record<string, string>
48
+ }
49
+
50
+ /** Validasi umum: wajib isi. */
51
+ export function validateRequired(v: string): string | null {
52
+ return v.trim() ? null : t("form.required")
53
+ }
54
+
55
+ /** Validasi umum: URL http(s) yang sah. */
56
+ export function validateUrl(v: string): string | null {
57
+ if (!v.trim()) return t("form.required")
58
+ try {
59
+ const u = new URL(v.trim())
60
+ if (!["http:", "https:"].includes(u.protocol)) return t("form.httpOnly")
61
+ } catch {
62
+ return t("form.invalidUrl")
63
+ }
64
+ return null
65
+ }
66
+
67
+ interface FieldState {
68
+ def: FormField
69
+ /** Text/secret: nilai ketik. Select: indeks opsi. Confirm: boolean. */
70
+ text: string
71
+ cursor: number // grapheme
72
+ selectIdx: number
73
+ confirmed: boolean
74
+ error: string | null
75
+ /** Offset scroll horizontal nilai (kursor selalu terlihat). */
76
+ viewStart: number
77
+ }
78
+
79
+ const dim = (s: string): string => c.dim(s)
80
+
81
+ /** Lebar kotak form TETAP (keputusan rasa: geometri stabil). */
82
+ const FORM_BOX_W = 64
83
+
84
+ function toState(def: FormField): FieldState {
85
+ if (def.kind === "select") {
86
+ const n = Math.max(1, def.options?.length ?? 1)
87
+ const init = typeof def.initial === "number" ? def.initial : 0
88
+ return {
89
+ def,
90
+ text: "",
91
+ cursor: 0,
92
+ selectIdx: Math.min(Math.max(0, init), n - 1),
93
+ confirmed: false,
94
+ error: null,
95
+ viewStart: 0,
96
+ }
97
+ }
98
+ if (def.kind === "confirm") {
99
+ return {
100
+ def,
101
+ text: "",
102
+ cursor: 0,
103
+ selectIdx: 0,
104
+ confirmed: typeof def.initial === "boolean" ? def.initial : false,
105
+ error: null,
106
+ viewStart: 0,
107
+ }
108
+ }
109
+ return {
110
+ def,
111
+ text: typeof def.initial === "string" ? def.initial : "",
112
+ cursor: 0,
113
+ selectIdx: 0,
114
+ confirmed: false,
115
+ error: null,
116
+ viewStart: 0,
117
+ }
118
+ }
119
+
120
+ function fieldValue(st: FieldState): string {
121
+ if (st.def.kind === "select") return st.def.options?.[st.selectIdx] ?? ""
122
+ if (st.def.kind === "confirm") return st.confirmed ? "y" : "n"
123
+ return st.text
124
+ }
125
+
126
+ /**
127
+ * Potong deretan grapheme agar muat dalam `maxV` KOLOM (CJK/emoji = 2).
128
+ * Dipakai jendela nilai field: slice grapheme mentah bisa meluap 2×.
129
+ */
130
+ function sliceByWidth(pts: string[], start: number, maxV: number): string[] {
131
+ const out: string[] = []
132
+ let w = 0
133
+ for (let i = start; i < pts.length; i++) {
134
+ const cw = displayWidth(pts[i] ?? "")
135
+ if (w + cw > maxV) break
136
+ out.push(pts[i]!)
137
+ w += cw
138
+ }
139
+ return out
140
+ }
141
+
142
+ // Jeda sebelum peringatan idle & jeda peringatan → batal (temuan audit
143
+ // TUI-004): raw-mode digenggam tanpa input rawan macet saat ConPTY mati,
144
+ // tapi batal diam-diam membuang draft yang sudah diketik. Dua-tahap: 80 dtk
145
+ // diam → peringatan di footer kotak; 10 dtk lagi tanpa keypress → batal.
146
+ // TOTAL tetap 90 dtk (semantik anti-hang dipertahankan).
147
+ export const IDLE_WARN_MS = 80_000
148
+ export const IDLE_CANCEL_MS = 10_000
149
+
150
+ export async function runForm(spec: FormSpec, screen: AltScreen): Promise<FormResult> {
151
+ if (!spec.fields.length) return { cancelled: true }
152
+ return new Promise<FormResult>((resolve) => {
153
+ const fields = spec.fields.map(toState)
154
+ let idx = 0
155
+ let done = false
156
+ let idleTimer: ReturnType<typeof setTimeout> | undefined
157
+ let idleWarned = false
158
+ const clearIdle = () => {
159
+ if (idleTimer) clearTimeout(idleTimer)
160
+ idleTimer = undefined
161
+ }
162
+ const resetIdle = () => {
163
+ if (done) return
164
+ clearIdle()
165
+ idleWarned = false
166
+ idleTimer = setTimeout(() => {
167
+ // Tahap 1: peringatan (draft TIDAK dibuang — render footer merah).
168
+ idleWarned = true
169
+ render()
170
+ // Tahap 2: batal — hanya bila tetap hening.
171
+ idleTimer = setTimeout(() => finish(true), IDLE_CANCEL_MS)
172
+ try {
173
+ ;(idleTimer as unknown as { unref?: () => void }).unref?.()
174
+ } catch {}
175
+ }, IDLE_WARN_MS)
176
+ try {
177
+ ;(idleTimer as unknown as { unref?: () => void }).unref?.()
178
+ } catch {}
179
+ }
180
+
181
+ // Lebar konten = lebar kotak TETAP (64) dikurangi chrome (border+padding
182
+ // = 4) — sepakat dengan dialogBox min=max 64 (tanpa potong-ganda).
183
+ const width = () => Math.max(20, Math.min((screen.cols || 80) - 6, FORM_BOX_W))
184
+ const innerW = () => Math.max(8, width() - 4)
185
+
186
+ // Baris nilai field aktif (indeks dalam body) untuk parkir kursor.
187
+ const render = (): { activeRow: number; activeCol: number } => {
188
+ const w = innerW()
189
+ const cut = (s: string) => truncateToWidth(s, w)
190
+ const body: string[] = []
191
+ let activeRow = -1
192
+ let activeCol = -1
193
+ fields.forEach((st, fi) => {
194
+ const active = fi === idx
195
+ if (st.def.kind === "text" || st.def.kind === "secret") {
196
+ const shown = st.def.kind === "secret" ? "•".repeat(toGraphemes(st.text).length) : st.text
197
+ // Scroll horizontal dalam KOLOM (bukan grapheme): CJK/emoji 2 kolom,
198
+ // jadi hitung lebar tampil, bukan jumlah unit. Gagal-di-kode-lama:
199
+ // kursor meleset 1 per glyph lebar + scroll prematur/terlambat.
200
+ const maxV = Math.max(8, w - displayWidth(sanitizeAnsiLine(st.def.label)) - 4)
201
+ const pts = toGraphemes(shown)
202
+ let start = Math.max(0, Math.min(st.viewStart, st.cursor))
203
+ if (st.cursor < start) start = st.cursor
204
+ while (start < st.cursor && displayWidth(pts.slice(start, st.cursor).join("")) > maxV - 1)
205
+ start++
206
+ st.viewStart = start
207
+ const vis = sliceByWidth(pts, start, maxV).join("")
208
+ const prefix = `${active ? c.accent("›") : " "} ${sanitizeAnsiLine(st.def.label)}: `
209
+ // Kolom kursor terminal = lebar tampil prefix + lebar tampil nilai
210
+ // sebelum kursor (BUKAN jumlah grapheme).
211
+ if (active) {
212
+ activeCol =
213
+ displayWidth(stripAnsi(prefix)) +
214
+ displayWidth(pts.slice(start, st.cursor).join("")) +
215
+ 1
216
+ }
217
+ body.push(cut(`${prefix}${vis}`))
218
+ activeRow = active ? body.length - 1 : activeRow
219
+ } else if (st.def.kind === "select") {
220
+ const opts = st.def.options ?? []
221
+ const cur = sanitizeAnsiLine(opts[st.selectIdx] ?? "")
222
+ const marker = active ? c.accent("›") : " "
223
+ if (active) activeCol = -1 // select tanpa kursor ketik
224
+ body.push(
225
+ cut(`${marker} ${sanitizeAnsiLine(st.def.label)}: ${dim("‹")} ${cur} ${dim("›")}`),
226
+ )
227
+ activeRow = active ? body.length - 1 : activeRow
228
+ } else {
229
+ // Confirm: Ya/Tidak toggle.
230
+ const marker = active ? c.accent("›") : " "
231
+ const val = st.confirmed ? c.accent(t("form.yes")) : t("form.no")
232
+ if (active) activeCol = -1
233
+ body.push(cut(`${marker} ${sanitizeAnsiLine(st.def.label)}: ${val}`))
234
+ activeRow = active ? body.length - 1 : activeRow
235
+ }
236
+ if (st.error) body.push(cut(c.error(` ${sanitizeAnsiLine(st.error)}`)))
237
+ })
238
+ const box = dialogBox(
239
+ {
240
+ title: spec.title,
241
+ body,
242
+ // Peringatan idle (TUI-004): footer berubah merah 10 dtk sebelum
243
+ // batal sendiri — user tahu kenapa dan bisa mencegah (tekan apa
244
+ // saja), draft tidak hilang tanpa jejak.
245
+ footer: idleWarned
246
+ ? c.error(t("form.idleWarning", { s: Math.ceil(IDLE_CANCEL_MS / 1000) }))
247
+ : (spec.footer ?? t("form.footerDefault")),
248
+ // Lebar TETAP (paritas picker/manager — geometri stabil, tak
249
+ // bernapas saat error muncul/hilang).
250
+ minWidth: FORM_BOX_W,
251
+ maxWidth: FORM_BOX_W,
252
+ },
253
+ screen.cols,
254
+ screen.rows,
255
+ )
256
+ screen.paintRegion(box.lines, box.topRow)
257
+ // Parkir kursor terminal di posisi ketik field aktif (select/confirm:
258
+ // sembunyikan di ujung baris). Kolom absolut = padding kiri + border +
259
+ // spasi + offset konten + 1; clamp ke layar. Menghitung ulang center
260
+ // dari lebar baris (yang sudah termasuk padding) terbukti off-by-N.
261
+ try {
262
+ if (activeRow >= 0) {
263
+ const li = Math.min(box.lines.length - 1, box.bodyTop + activeRow)
264
+ const row = box.topRow + li
265
+ const stripped = stripAnsi(box.lines[li] ?? "")
266
+ const leftPad = boxLeftPad(stripped)
267
+ const contentW = displayWidth(stripped) - leftPad
268
+ const raw = activeCol >= 0 ? leftPad + activeCol + 2 : leftPad + contentW + 1
269
+ const col = Math.max(1, Math.min(screen.cols || 80, raw))
270
+ // Di dalam blok sync-update yang SAMA dengan paintRegion di atas
271
+ // (temuan audit TUI-007): kursor yang muncul di luar sync berpotensi
272
+ // tearing di emulator tanpa ?2026.
273
+ process.stdout.write(`${SYNC_UPDATE_START}\x1b[${row};${col}H\x1b[?25h${SYNC_UPDATE_END}`)
274
+ }
275
+ } catch {}
276
+ return { activeRow, activeCol }
277
+ }
278
+
279
+ const finish = (cancelled: boolean) => {
280
+ if (done) return
281
+ done = true
282
+ clearIdle()
283
+ try {
284
+ formPump.dispose()
285
+ } catch {}
286
+ try {
287
+ screen.clearRegion()
288
+ } catch {}
289
+ try {
290
+ process.stdin.setRawMode(false)
291
+ } catch {}
292
+ if (onData) process.stdin.removeListener("data", onData)
293
+ if (onResize) process.stdout.removeListener("resize", onResize)
294
+ if (cancelled) resolve({ cancelled: true })
295
+ else {
296
+ const values: Record<string, string> = {}
297
+ for (const st of fields) values[st.def.id] = fieldValue(st)
298
+ resolve({ cancelled: false, values })
299
+ }
300
+ }
301
+
302
+ const validateAll = (): boolean => {
303
+ let firstBad = -1
304
+ fields.forEach((st, fi) => {
305
+ st.error = null
306
+ if (st.def.kind === "text" || st.def.kind === "secret") {
307
+ const err = st.def.validate?.(st.text) ?? null
308
+ if (err) {
309
+ st.error = err
310
+ if (firstBad < 0) firstBad = fi
311
+ }
312
+ }
313
+ })
314
+ if (firstBad >= 0) {
315
+ idx = firstBad
316
+ render()
317
+ return false
318
+ }
319
+ return true
320
+ }
321
+
322
+ const moveCursor = (st: FieldState, d: number) => {
323
+ const n = toGraphemes(st.text).length
324
+ st.cursor = Math.max(0, Math.min(n, st.cursor + d))
325
+ }
326
+
327
+ const insertText = (st: FieldState, ins: string) => {
328
+ const clean = ins
329
+ .replace(/\r\n|\r/g, " ")
330
+ .replace(/\t/g, " ")
331
+ // Kontrol dibuang (kecuali newline yang sudah jadi spasi).
332
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: sanitasi input form
333
+ .replace(/[\u0000-\u001f\u007f]/g, "")
334
+ if (!clean) return
335
+ const pts = toGraphemes(st.text)
336
+ const at = Math.max(0, Math.min(st.cursor, pts.length))
337
+ pts.splice(at, 0, ...toGraphemes(clean))
338
+ st.text = pts.join("")
339
+ st.cursor = at + toGraphemes(clean).length
340
+ st.error = null
341
+ }
342
+
343
+ const deletePrev = (st: FieldState) => {
344
+ const pts = toGraphemes(st.text)
345
+ const at = Math.max(0, Math.min(st.cursor, pts.length))
346
+ if (at === 0) return
347
+ // Hapus satu grapheme (emoji/ZWJ/flag utuh).
348
+ const head = pts.slice(0, at - 1)
349
+ const tail = pts.slice(at)
350
+ st.text = [...head, ...tail].join("")
351
+ st.cursor = at - 1
352
+ st.error = null
353
+ }
354
+
355
+ const handleKeys = (keys: DecodedKey[]): boolean => {
356
+ if (done) return true
357
+ resetIdle()
358
+ const st = fields[idx]!
359
+ for (const d of keys) {
360
+ const k = d.key
361
+ // Esc/Ctrl+C dua-tahap (anti-hilang draft): field kotor → kembalikan
362
+ // ke nilai bawaan (prefill edit tak hilang sekali tekan — dulu
363
+ // langsung dikosongkan tanpa undo); sudah bawaan/kosong → batal
364
+ // total. Ctrl+D = batal langsung (konvensi EOF, tidak dipakai
365
+ // menghapus).
366
+ if (k.type === "esc" || k.type === "ctrl-c") {
367
+ const cur = fields[idx]!
368
+ const initial = typeof cur.def.initial === "string" ? cur.def.initial : ""
369
+ if ((cur.def.kind === "text" || cur.def.kind === "secret") && cur.text !== initial) {
370
+ cur.text = initial
371
+ cur.cursor = toGraphemes(initial).length
372
+ cur.viewStart = 0
373
+ cur.error = null
374
+ render()
375
+ continue
376
+ }
377
+ finish(true)
378
+ return true
379
+ }
380
+ if (k.type === "ctrl-d") {
381
+ finish(true)
382
+ return true
383
+ }
384
+ if (k.type === "enter") {
385
+ if (validateAll()) finish(false)
386
+ return false
387
+ }
388
+ if (k.type === "tab") {
389
+ idx = (idx + 1) % fields.length
390
+ continue
391
+ }
392
+ if (k.type === "shift-tab") {
393
+ idx = (idx - 1 + fields.length) % fields.length
394
+ continue
395
+ }
396
+ if (k.type === "up") {
397
+ idx = (idx - 1 + fields.length) % fields.length
398
+ continue
399
+ }
400
+ if (k.type === "down") {
401
+ idx = (idx + 1) % fields.length
402
+ continue
403
+ }
404
+ if (st.def.kind === "select") {
405
+ const n = Math.max(1, st.def.options?.length ?? 1)
406
+ if (k.type === "left") st.selectIdx = (st.selectIdx - 1 + n) % n
407
+ else if (k.type === "right") st.selectIdx = (st.selectIdx + 1) % n
408
+ continue
409
+ }
410
+ if (st.def.kind === "confirm") {
411
+ if (k.type === "left" || k.type === "right") {
412
+ st.confirmed = !st.confirmed
413
+ continue
414
+ }
415
+ if (k.type === "char") {
416
+ // Terima dwibahasa (label tampil per locale): y/yes/ya, n/no/tidak/t.
417
+ const ch = k.ch.toLowerCase()
418
+ if (ch === "y" || ch === "ya") st.confirmed = true
419
+ else if (ch === "n" || ch === "t") st.confirmed = false
420
+ continue
421
+ }
422
+ continue
423
+ }
424
+ // Text/secret editing.
425
+ if (k.type === "char") insertText(st, k.ch)
426
+ else if (k.type === "backspace") deletePrev(st)
427
+ else if (k.type === "delete") {
428
+ const pts = toGraphemes(st.text)
429
+ const at = Math.max(0, Math.min(st.cursor, pts.length))
430
+ if (at < pts.length) {
431
+ pts.splice(at, 1)
432
+ st.text = pts.join("")
433
+ st.error = null
434
+ }
435
+ } else if (k.type === "left") moveCursor(st, -1)
436
+ else if (k.type === "right") moveCursor(st, 1)
437
+ else if (k.type === "home") st.cursor = 0
438
+ else if (k.type === "end") st.cursor = toGraphemes(st.text).length
439
+ else if (k.type === "ctrl-u") {
440
+ st.text = ""
441
+ st.cursor = 0
442
+ st.viewStart = 0
443
+ st.error = null
444
+ } else if (k.type === "ctrl-w") {
445
+ const pts = toGraphemes(st.text)
446
+ const at = Math.max(0, Math.min(st.cursor, pts.length))
447
+ const before = pts.slice(0, at).join("")
448
+ const trimmed = before.replace(/\S+\s*$/, "")
449
+ st.text = trimmed + pts.slice(at).join("")
450
+ st.cursor = toGraphemes(trimmed).length
451
+ st.error = null
452
+ }
453
+ }
454
+ render()
455
+ return false
456
+ }
457
+
458
+ const decoder: DecoderState = createDecoderState()
459
+ const formPump = createKeyStreamPump({ state: decoder, onKeys: handleKeys })
460
+ let onData!: (chunk: Buffer) => void
461
+ let onResize!: () => void
462
+ onData = (chunk: Buffer) => {
463
+ resetIdle()
464
+ try {
465
+ formPump.push(chunk)
466
+ } catch {
467
+ finish(true)
468
+ }
469
+ }
470
+ onResize = () => render()
471
+
472
+ // Form berjalan di atas screen milik pemanggil (view membuka
473
+ // openAltScreen sendiri; nested = paint-through). Listener stdin milik
474
+ // form selama hidup — pemanggil wajib suspend dulu (pola popup komposit).
475
+ try {
476
+ process.stdin.setRawMode(true)
477
+ process.stdin.resume()
478
+ process.stdin.on("data", onData)
479
+ process.stdout.on("resize", onResize)
480
+ resetIdle()
481
+ render()
482
+ } catch {
483
+ finish(true)
484
+ }
485
+ })
486
+ }