minicode-ai 0.21.0 → 0.23.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.
- package/README.md +1 -1
- package/cli/commands/exec.ts +44 -1
- package/cli/commands.ts +15 -2
- package/cli/index.ts +11 -0
- package/cli/setup.ts +88 -33
- package/cli/tui.ts +3 -3
- package/docs/TERMINAL_CONTRACT.md +69 -13
- package/docs/architecture.md +9 -5
- package/docs/concepts.md +1 -1
- package/docs/contributing.md +2 -1
- package/docs/environment.md +4 -0
- package/docs/policy-sandbox.md +6 -1
- package/docs/security.md +12 -4
- package/docs/skills.md +1 -1
- package/package.json +2 -2
- package/src/app/session.ts +129 -20
- package/src/config.ts +108 -8
- package/src/constants.ts +29 -4
- package/src/daemon/discovery.ts +24 -0
- package/src/policy/bash-guard.ts +170 -6
- package/src/policy/compaction.ts +61 -7
- package/src/policy/context-budget.ts +132 -0
- package/src/policy/context-escalation.ts +140 -0
- package/src/policy/context-window.ts +47 -9
- package/src/policy/context.ts +10 -1
- package/src/policy/jail.ts +10 -22
- package/src/policy/microcompact.ts +162 -0
- package/src/policy/permission.ts +9 -6
- package/src/presentation/projection.ts +1 -1
- package/src/providers/anthropic.ts +31 -7
- package/src/providers/detect.ts +128 -13
- package/src/providers/provision.ts +78 -1
- package/src/providers/responses.ts +14 -2
- package/src/providers/sse-bounds.ts +16 -2
- package/src/session/persistence.ts +56 -0
- package/src/ui/approval/prompt.ts +17 -6
- package/src/ui/assistant/simple.ts +1 -1
- package/src/ui/footer.ts +2 -2
- package/src/ui/i18n/en.ts +28 -5
- package/src/ui/i18n/id.ts +30 -5
- package/src/ui/input/input.ts +2 -2
- package/src/ui/input/prompt-engine.ts +6 -0
- package/src/ui/render/activity-panel.ts +21 -3
- package/src/ui/render/diff.ts +51 -2
- package/src/ui/render/highlight.ts +68 -37
- package/src/ui/render/hint.ts +28 -0
- package/src/ui/render/logo.ts +91 -0
- package/src/ui/render/sanitize.ts +5 -1
- package/src/ui/render/session-summary.ts +28 -5
- package/src/ui/render/table-grid.ts +123 -13
- package/src/ui/render/table.ts +4 -2
- package/src/ui/render/theme.ts +16 -3
- package/src/ui/render/width.ts +35 -6
- package/src/ui/render/wrap.ts +28 -6
- package/src/ui/runtime/loading-bar.ts +17 -5
- package/src/ui/runtime/screen.ts +4 -2
- package/src/ui/screens/dialog.ts +23 -2
- package/src/ui/screens/form.ts +71 -33
- package/src/ui/screens/lifecycle.ts +80 -0
- package/src/ui/screens/model-manager.ts +85 -83
- package/src/ui/screens/picker.ts +39 -25
- package/src/ui/screens/provider-manager.ts +58 -55
- package/src/ui/screens/watchdog.ts +27 -0
- package/src/ui/screens/wizard.ts +12 -1
- package/src/ui/tui/app.ts +62 -22
- package/src/ui/tui/transcript.ts +93 -23
- package/vendor/minicore/src/core/compact.ts +10 -0
- package/vendor/minicore/src/core/errors.ts +11 -1
- package/vendor/minicore/src/core/executor.ts +6 -1
- package/vendor/minicore/src/core/loop.ts +92 -2
- package/vendor/minicore/src/core/session.ts +8 -0
- package/vendor/minicore/src/providers/openai-compat.ts +45 -3
package/src/ui/render/theme.ts
CHANGED
|
@@ -92,7 +92,7 @@ const TOKENS = {
|
|
|
92
92
|
error: "38;2;244;135;113",
|
|
93
93
|
warning: "38;2;204;167;0",
|
|
94
94
|
info: "38;2;117;190;255",
|
|
95
|
-
accent: "38;2;
|
|
95
|
+
accent: "38;2;74;144;226",
|
|
96
96
|
// String literal kode (VS Code Dark+ `string` #CE9178) — sengaja terpisah dari
|
|
97
97
|
// `success` (green): string harus tampak jingga-salm ala VS Code, sementara
|
|
98
98
|
// status sukses tetap hijau. Alias lama `green` tetap menyasar success.
|
|
@@ -345,6 +345,11 @@ export const glyphs = {
|
|
|
345
345
|
get spinnerFrames() {
|
|
346
346
|
return supportsUtf8() ? ["·", "··", "···"] : [".", "..", "..."]
|
|
347
347
|
},
|
|
348
|
+
/** Karakter mask field secret (form & prompt inline) — satu sumber.
|
|
349
|
+
* Sengaja getter (bukan const): MINICODE_ASCII bisa berubah saat runtime. */
|
|
350
|
+
get mask() {
|
|
351
|
+
return supportsUtf8() ? "•" : "*"
|
|
352
|
+
},
|
|
348
353
|
// Blok progress bar loading (baris tunggal). Fallback ASCII untuk terminal
|
|
349
354
|
// tanpa UTF-8 (MINICODE_ASCII / konsol Windows legacy).
|
|
350
355
|
get blockFilled() {
|
|
@@ -405,7 +410,15 @@ export const ESC = String.fromCharCode(27)
|
|
|
405
410
|
// hanya cocok untuk `ESC[P`/`ESC[_` dst. sehingga payload DCS bocor.
|
|
406
411
|
export const ANSI_PATTERN = `${ESC}(?:\\[[0-9;?<=>]*[@-~]|\\[[0-9;?<=>]*|\\][^${ESC}\\u0007]*(?:\\u0007|${ESC}\\\\)|(?:P|_|\\^|X)[^${ESC}\\u0007]*(?:\\u0007|${ESC}\\\\)|[()#][0-~]|[0-~])`
|
|
407
412
|
|
|
413
|
+
/** Regex bersama untuk stripAnsi.
|
|
414
|
+
*
|
|
415
|
+
* Dulu `new RegExp(...)` dibuat tiap panggilan — meski hasilnya benar (lastIndex
|
|
416
|
+
* `replace` selalu di-reset), ini hot path yang dipanggil per baris. Regex
|
|
417
|
+
* global dicomile sekali; `String.replace` dengan flag `g` mereset `lastIndex`
|
|
418
|
+
* sendiri sehingga aman dipakai bersama antar pemanggil (audit 2026-11-12).
|
|
419
|
+
*/
|
|
420
|
+
const ANSI_STRIP = new RegExp(ANSI_PATTERN, "g")
|
|
421
|
+
|
|
408
422
|
export function stripAnsi(str: string): string {
|
|
409
|
-
|
|
410
|
-
return str.replace(new RegExp(ANSI_PATTERN, "g"), "")
|
|
423
|
+
return str.replace(ANSI_STRIP, "")
|
|
411
424
|
}
|
package/src/ui/render/width.ts
CHANGED
|
@@ -186,7 +186,16 @@ function isSgrSeq(seq: string): boolean {
|
|
|
186
186
|
return params.split(/[:;]/).every((p) => p === "" || /^[0-9]+$/.test(p))
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
-
/**
|
|
189
|
+
/**
|
|
190
|
+
* True bila string berakhir dengan gaya SGR masih terbuka (butuh reset).
|
|
191
|
+
*
|
|
192
|
+
* Parameter diproses BERURUTAN, bukan "ada OFF di mana pun": `\x1b[0;31m`
|
|
193
|
+
* berarti reset lalu merah (= masih terbuka), sedangkan `some(SGR_OFF)` lama
|
|
194
|
+
* menyimpulkan tertutup dan tak menambahkan reset → warna bocor ke sisa baris
|
|
195
|
+
* (audit 2026-11-12, HIGH-7). Parameter extended color (`38;2;r;g;b`,
|
|
196
|
+
* `38;5;n`) juga dikonsumsi sebagai satu atribut: komponen warnanya bisa
|
|
197
|
+
* bernilai 0 dan tidak boleh disalahartikan sebagai reset.
|
|
198
|
+
*/
|
|
190
199
|
function sgrLeftOpen(s: string): boolean {
|
|
191
200
|
let open = false
|
|
192
201
|
let i = 0
|
|
@@ -195,11 +204,7 @@ function sgrLeftOpen(s: string): boolean {
|
|
|
195
204
|
const len = escapeLength(s, i)
|
|
196
205
|
if (len > 0) {
|
|
197
206
|
const seq = s.slice(i, i + len)
|
|
198
|
-
if (isSgrSeq(seq))
|
|
199
|
-
const params = seq.slice(2, -1).split(/[:;]/)
|
|
200
|
-
if (params.some((p) => SGR_OFF.has(p) || p === "")) open = false
|
|
201
|
-
else if (params.some((p) => p !== "")) open = true
|
|
202
|
-
}
|
|
207
|
+
if (isSgrSeq(seq)) open = sgrParamsEndOpen(seq.slice(2, -1).split(/[:;]/), open)
|
|
203
208
|
i += len
|
|
204
209
|
continue
|
|
205
210
|
}
|
|
@@ -209,6 +214,30 @@ function sgrLeftOpen(s: string): boolean {
|
|
|
209
214
|
}
|
|
210
215
|
return open
|
|
211
216
|
}
|
|
217
|
+
|
|
218
|
+
/** State "masih terbuka" setelah satu sekuens SGR, mengikuti urutan parameter. */
|
|
219
|
+
function sgrParamsEndOpen(params: string[], open: boolean): boolean {
|
|
220
|
+
let state = open
|
|
221
|
+
for (let i = 0; i < params.length; i++) {
|
|
222
|
+
const p = params[i]!
|
|
223
|
+
if (p === "" || p === "0") {
|
|
224
|
+
state = false
|
|
225
|
+
continue
|
|
226
|
+
}
|
|
227
|
+
// Extended color: 38/48/58 diikuti 5;n atau 2;r;g;b — satu atribut utuh.
|
|
228
|
+
if (p === "38" || p === "48" || p === "58") {
|
|
229
|
+
const mode = params[i + 1]
|
|
230
|
+
i += mode === "5" ? 2 : mode === "2" ? 4 : 1
|
|
231
|
+
state = true
|
|
232
|
+
continue
|
|
233
|
+
}
|
|
234
|
+
// OFF hanya menutup satu atribut; atribut lain bisa tetap aktif, jadi
|
|
235
|
+
// status dibiarkan (arah aman: reset ekstra tak berbahaya).
|
|
236
|
+
if (SGR_OFF.has(p)) continue
|
|
237
|
+
state = true
|
|
238
|
+
}
|
|
239
|
+
return state
|
|
240
|
+
}
|
|
212
241
|
/** Panjang sekuens escape yang mulai di `i`, atau 0 bila bukan escape. */
|
|
213
242
|
export function escapeLength(s: string, i: number): number {
|
|
214
243
|
if (s[i] !== "\x1b") return 0
|
package/src/ui/render/wrap.ts
CHANGED
|
@@ -48,24 +48,46 @@ export function wordWrap(text: string, width: number): string {
|
|
|
48
48
|
for (const tok of line.split(/(\s+)/)) {
|
|
49
49
|
if (tok === "") continue
|
|
50
50
|
if (/^\s+$/.test(tok)) {
|
|
51
|
-
|
|
51
|
+
// Run whitespace di AWAL baris = indentasi, bukan pemisah: ia harus
|
|
52
|
+
// ikut baris pertama. Dulu run ini dibuang saat `current` masih kosong
|
|
53
|
+
// sehingga indentasi & marker list panjang hilang di jalur linear
|
|
54
|
+
// (audit 2026-11-12, HIGH-6).
|
|
55
|
+
current += tok
|
|
52
56
|
continue
|
|
53
57
|
}
|
|
54
58
|
const word = tok
|
|
55
59
|
// Kata tunggal lebih lebar dari baris: pecah paksa per kolom.
|
|
56
60
|
if (displayWidth(word) > width) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
+
// Indentasi (bila ada) menempel ke potongan PERTAMA dan menyusutkan
|
|
62
|
+
// lebar potongan itu — tanpa ini indentasi hilang atau baris meluap.
|
|
63
|
+
const lead = current.trim() === "" ? current : ""
|
|
64
|
+
// Teks yang sudah terkumpul (bila ada) ditutup dulu; hanya indentasi
|
|
65
|
+
// murni yang ikut menempel ke potongan pertama.
|
|
66
|
+
if (current.trim() !== "") out.push(current.trimEnd())
|
|
67
|
+
current = ""
|
|
68
|
+
const avail = Math.max(1, width - displayWidth(lead))
|
|
69
|
+
const pieces = chunkByWidth(word, avail)
|
|
70
|
+
if (lead && pieces.length > 0) {
|
|
71
|
+
out.push(lead + (pieces[0] ?? ""))
|
|
72
|
+
for (let i = 1; i < pieces.length - 1; i++) out.push(pieces[i]!)
|
|
73
|
+
current = pieces[pieces.length - 1] ?? ""
|
|
74
|
+
if (pieces.length === 1) current = ""
|
|
75
|
+
} else {
|
|
76
|
+
for (let i = 0; i < pieces.length - 1; i++) out.push(pieces[i]!)
|
|
77
|
+
current = pieces[pieces.length - 1] ?? ""
|
|
78
|
+
}
|
|
61
79
|
continue
|
|
62
80
|
}
|
|
63
81
|
// `current` sudah memuat run whitespace aslinya; tambah satu spasi
|
|
64
82
|
// hanya bila tidak ada pemisah di antara dua kata.
|
|
65
83
|
const glued = !current || /\s$/.test(current) ? `${current}${word}` : `${current} ${word}`
|
|
66
|
-
if (displayWidth(glued) > width && current) {
|
|
84
|
+
if (displayWidth(glued) > width && current.trim() !== "") {
|
|
67
85
|
out.push(current.trimEnd())
|
|
68
86
|
current = word
|
|
87
|
+
} else if (displayWidth(glued) > width) {
|
|
88
|
+
// Indentasi + kata tak muat bersama: indentasi dilepas untuk baris ini
|
|
89
|
+
// (baris tetap <= width; lebih baik daripada meluap/memecah kata).
|
|
90
|
+
current = word
|
|
69
91
|
} else {
|
|
70
92
|
current = glued
|
|
71
93
|
}
|
|
@@ -15,10 +15,19 @@ import { truncateToWidth } from "../render/width.ts"
|
|
|
15
15
|
import { motionReduced } from "./motion.ts"
|
|
16
16
|
import { acquireTransientPaint, paintWrite } from "./statusline.ts"
|
|
17
17
|
|
|
18
|
-
// Jaga agar kursor terminal selalu ter-restore meski proses dihentikan
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
// Jaga agar kursor terminal selalu ter-restore meski proses dihentikan
|
|
19
|
+
// (SIGINT/dll). Hook didaftarkan LAZY saat bar benar-benar menyembunyikan
|
|
20
|
+
// kursor: hook di scope modul menulis `ESC[?25h` ke stderr pada SETIAP proses
|
|
21
|
+
// yang sekadar meng-import modul ini — byte asing yang mendarat di alt-screen
|
|
22
|
+
// TUI walau bar tak pernah dibuat (audit 2026-11-12).
|
|
23
|
+
let exitHookInstalled = false
|
|
24
|
+
function installExitHook(): void {
|
|
25
|
+
if (exitHookInstalled) return
|
|
26
|
+
exitHookInstalled = true
|
|
27
|
+
process.on("exit", () => {
|
|
28
|
+
if (process.stderr.isTTY) process.stderr.write("\x1b[?25h")
|
|
29
|
+
})
|
|
30
|
+
}
|
|
22
31
|
|
|
23
32
|
export interface LoadingBar {
|
|
24
33
|
/** Ganti label + persen (0..100). Tak pernah melampaui target yang diberikan. */
|
|
@@ -73,7 +82,10 @@ export function createLoadingBar(
|
|
|
73
82
|
started = true
|
|
74
83
|
// Sembunyikan kursor hanya saat animasi normal; mode gerak-dikurangi
|
|
75
84
|
// cukup melukis baris tanpa mengubah visibilitas kursor.
|
|
76
|
-
if (!reduced)
|
|
85
|
+
if (!reduced) {
|
|
86
|
+
installExitHook()
|
|
87
|
+
paintWrite("\x1b[?25l")
|
|
88
|
+
}
|
|
77
89
|
owned = acquireTransientPaint("loading-bar", paint)
|
|
78
90
|
paint()
|
|
79
91
|
}
|
package/src/ui/runtime/screen.ts
CHANGED
|
@@ -214,7 +214,9 @@ export function openAltScreen(): AltScreen {
|
|
|
214
214
|
if (cursor) {
|
|
215
215
|
const row = Math.max(1, Math.floor(cursor.row))
|
|
216
216
|
const col = Math.max(1, Math.floor(cursor.col))
|
|
217
|
-
|
|
217
|
+
// DECSCUSR `6 q` = bar tegak stabil (tak berkedip): caret tak menutupi
|
|
218
|
+
// karakter di bawahnya (blok penuh), dan lebih tenang = clean & minimal.
|
|
219
|
+
out += `\x1b[?25l\x1b[${row};${col}H\x1b[6 q\x1b[?25h`
|
|
218
220
|
}
|
|
219
221
|
out += SYNC_END
|
|
220
222
|
process.stdout.write(out)
|
|
@@ -226,7 +228,7 @@ export function openAltScreen(): AltScreen {
|
|
|
226
228
|
// Sync block + reset atribut: kursor tak boleh mewarisi warna run
|
|
227
229
|
// sebelumnya, dan perpindahan kursor tak perlu menyentuh satu sel pun.
|
|
228
230
|
process.stdout.write(
|
|
229
|
-
`${SYNC_START}${RESET_ATTRS}\x1b[?25l\x1b[${r};${c}H\x1b[?25h${SYNC_END}`,
|
|
231
|
+
`${SYNC_START}${RESET_ATTRS}\x1b[?25l\x1b[${r};${c}H\x1b[6 q\x1b[?25h${SYNC_END}`,
|
|
230
232
|
)
|
|
231
233
|
},
|
|
232
234
|
paintRegion(lines: string[], topRow: number): void {
|
package/src/ui/screens/dialog.ts
CHANGED
|
@@ -28,6 +28,13 @@ export interface DialogSpec {
|
|
|
28
28
|
minWidth?: number
|
|
29
29
|
/** Tinggi kotak maksimum (inklusif border). Default: setinggi layar. */
|
|
30
30
|
maxHeight?: number
|
|
31
|
+
/**
|
|
32
|
+
* Indeks body (0-based) yang WAJIB terlihat, mis. field aktif form. Bila
|
|
33
|
+
* body melebihi tinggi kotak, jendela digeser agar baris ini ikut tampil —
|
|
34
|
+
* tanpa ini user mengetik ke field yang tidak ada di layar (audit
|
|
35
|
+
* 2026-11-12, HIGH-2).
|
|
36
|
+
*/
|
|
37
|
+
focusRow?: number
|
|
31
38
|
}
|
|
32
39
|
|
|
33
40
|
interface BoxChars {
|
|
@@ -63,6 +70,11 @@ export interface DialogBox {
|
|
|
63
70
|
* (bug nyata di 4 view).
|
|
64
71
|
*/
|
|
65
72
|
bodyTop: number
|
|
73
|
+
/**
|
|
74
|
+
* Indeks body pertama yang benar-benar dirender (0 kecuali body digeser
|
|
75
|
+
* karena `focusRow`). Kursor = `bodyTop + (activeRow - bodyStart)`.
|
|
76
|
+
*/
|
|
77
|
+
bodyStart: number
|
|
66
78
|
}
|
|
67
79
|
|
|
68
80
|
/**
|
|
@@ -132,7 +144,16 @@ export function dialogBox(spec: DialogSpec, cols: number, rows: number): DialogB
|
|
|
132
144
|
const room = Math.max(0, boxBudget - chromeFixed)
|
|
133
145
|
const clipped = spec.body.length > room
|
|
134
146
|
const shown = clipped ? Math.max(0, room - 1) : spec.body.length
|
|
135
|
-
|
|
147
|
+
// Jendela body: default dari atas, tapi bila pemanggil menandai baris fokus
|
|
148
|
+
// (form: field aktif) jendela digeser agar baris itu terlihat. Tanpa ini
|
|
149
|
+
// `activeRow` menunjuk baris di luar yang dirender sehingga kursor parkir di
|
|
150
|
+
// marker/border dan ketikan user tak terlihat (audit 2026-11-12, HIGH-2).
|
|
151
|
+
const focus = spec.focusRow
|
|
152
|
+
const bodyStart =
|
|
153
|
+
clipped && focus != null && shown > 0 && Number.isFinite(focus)
|
|
154
|
+
? Math.max(0, Math.min(Math.floor(focus) - shown + 1, spec.body.length - shown))
|
|
155
|
+
: 0
|
|
156
|
+
const body = spec.body.slice(bodyStart, bodyStart + shown)
|
|
136
157
|
for (const ln of body) boxed.push(`${box.v} ${padToWidth(cut(ln), innerW)} ${box.v}`)
|
|
137
158
|
if (clipped)
|
|
138
159
|
boxed.push(
|
|
@@ -148,7 +169,7 @@ export function dialogBox(spec: DialogSpec, cols: number, rows: number): DialogB
|
|
|
148
169
|
const left = Math.max(0, Math.floor((c0 - displayWidth(ln)) / 2))
|
|
149
170
|
return `${" ".repeat(left)}${ln}`
|
|
150
171
|
})
|
|
151
|
-
return { lines, topRow, height: boxed.length, bodyTop: 1 + (hasTitle ? 1 : 0) }
|
|
172
|
+
return { lines, topRow, height: boxed.length, bodyTop: 1 + (hasTitle ? 1 : 0), bodyStart }
|
|
152
173
|
}
|
|
153
174
|
|
|
154
175
|
/**
|
package/src/ui/screens/form.ts
CHANGED
|
@@ -19,10 +19,11 @@ import {
|
|
|
19
19
|
toGraphemes,
|
|
20
20
|
} from "../input/prompt-engine.ts"
|
|
21
21
|
import { sanitizeAnsiLine } from "../render/sanitize.ts"
|
|
22
|
-
import { c, stripAnsi } from "../render/theme.ts"
|
|
22
|
+
import { c, glyphs, stripAnsi } from "../render/theme.ts"
|
|
23
23
|
import { displayWidth, truncateToWidth } from "../render/width.ts"
|
|
24
24
|
import { type AltScreen, SYNC_UPDATE_END, SYNC_UPDATE_START } from "../runtime/screen.ts"
|
|
25
25
|
import { boxLeftPad, dialogBox } from "./dialog.ts"
|
|
26
|
+
import { createIdleTimer } from "./lifecycle.ts"
|
|
26
27
|
|
|
27
28
|
export interface FormField {
|
|
28
29
|
id: string
|
|
@@ -108,7 +109,10 @@ function toState(def: FormField): FieldState {
|
|
|
108
109
|
}
|
|
109
110
|
return {
|
|
110
111
|
def,
|
|
111
|
-
|
|
112
|
+
// Prefill berasal dari config/berkas (teks tak-terpercaya). Disanitasi di
|
|
113
|
+
// STATE, bukan saat render: kursor & viewStart dihitung dari `text`, jadi
|
|
114
|
+
// escape di dalamnya akan menggeser kolom kursor dari yang terlihat.
|
|
115
|
+
text: typeof def.initial === "string" ? sanitizeAnsiLine(def.initial) : "",
|
|
112
116
|
cursor: 0,
|
|
113
117
|
selectIdx: 0,
|
|
114
118
|
confirmed: false,
|
|
@@ -153,29 +157,23 @@ export async function runForm(spec: FormSpec, screen: AltScreen): Promise<FormRe
|
|
|
153
157
|
const fields = spec.fields.map(toState)
|
|
154
158
|
let idx = 0
|
|
155
159
|
let done = false
|
|
156
|
-
let idleTimer: ReturnType<typeof setTimeout> | undefined
|
|
157
160
|
let idleWarned = false
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
idleTimer = setTimeout(() => {
|
|
167
|
-
// Tahap 1: peringatan (draft TIDAK dibuang — render footer merah).
|
|
161
|
+
// Idle dua-fase via helper bersama (audit D1): 80 dtk diam → peringatan
|
|
162
|
+
// di footer kotak (draft TIDAK dibuang); 10 dtk lagi hening → batal.
|
|
163
|
+
// TOTAL tetap 90 dtk (semantik anti-hang dipertahankan). `finish`/`render`
|
|
164
|
+
// dirujuk sebagai closure — dievaluasi jauh setelah keduanya terdefinisi.
|
|
165
|
+
const idle = createIdleTimer({
|
|
166
|
+
warnMs: IDLE_WARN_MS,
|
|
167
|
+
cancelMs: IDLE_CANCEL_MS,
|
|
168
|
+
onWarn: () => {
|
|
168
169
|
idleWarned = true
|
|
169
170
|
render()
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
try {
|
|
177
|
-
;(idleTimer as unknown as { unref?: () => void }).unref?.()
|
|
178
|
-
} catch {}
|
|
171
|
+
},
|
|
172
|
+
onCancel: () => finish(true),
|
|
173
|
+
})
|
|
174
|
+
const resetIdle = () => {
|
|
175
|
+
idleWarned = false
|
|
176
|
+
idle.reset()
|
|
179
177
|
}
|
|
180
178
|
|
|
181
179
|
// Lebar konten = lebar kotak TETAP (64) dikurangi chrome (border+padding
|
|
@@ -193,7 +191,8 @@ export async function runForm(spec: FormSpec, screen: AltScreen): Promise<FormRe
|
|
|
193
191
|
fields.forEach((st, fi) => {
|
|
194
192
|
const active = fi === idx
|
|
195
193
|
if (st.def.kind === "text" || st.def.kind === "secret") {
|
|
196
|
-
const shown =
|
|
194
|
+
const shown =
|
|
195
|
+
st.def.kind === "secret" ? glyphs.mask.repeat(toGraphemes(st.text).length) : st.text
|
|
197
196
|
// Scroll horizontal dalam KOLOM (bukan grapheme): CJK/emoji 2 kolom,
|
|
198
197
|
// jadi hitung lebar tampil, bukan jumlah unit. Gagal-di-kode-lama:
|
|
199
198
|
// kursor meleset 1 per glyph lebar + scroll prematur/terlambat.
|
|
@@ -249,6 +248,9 @@ export async function runForm(spec: FormSpec, screen: AltScreen): Promise<FormRe
|
|
|
249
248
|
// bernapas saat error muncul/hilang).
|
|
250
249
|
minWidth: FORM_BOX_W,
|
|
251
250
|
maxWidth: FORM_BOX_W,
|
|
251
|
+
// Field aktif WAJIB terlihat: dialog menggeser jendela body bila
|
|
252
|
+
// muat-tidaknya terpaksa memotong (audit 2026-11-12, HIGH-2).
|
|
253
|
+
focusRow: activeRow >= 0 ? activeRow : undefined,
|
|
252
254
|
},
|
|
253
255
|
screen.cols,
|
|
254
256
|
screen.rows,
|
|
@@ -260,7 +262,12 @@ export async function runForm(spec: FormSpec, screen: AltScreen): Promise<FormRe
|
|
|
260
262
|
// dari lebar baris (yang sudah termasuk padding) terbukti off-by-N.
|
|
261
263
|
try {
|
|
262
264
|
if (activeRow >= 0) {
|
|
263
|
-
|
|
265
|
+
// bodyStart = offset jendela body yang benar-benar dirender; tanpa
|
|
266
|
+
// dikurangi, kursor mendarat di baris marker/border saat body dipotong.
|
|
267
|
+
const li = Math.max(
|
|
268
|
+
0,
|
|
269
|
+
Math.min(box.lines.length - 1, box.bodyTop + (activeRow - box.bodyStart)),
|
|
270
|
+
)
|
|
264
271
|
const row = box.topRow + li
|
|
265
272
|
const stripped = stripAnsi(box.lines[li] ?? "")
|
|
266
273
|
const leftPad = boxLeftPad(stripped)
|
|
@@ -279,7 +286,7 @@ export async function runForm(spec: FormSpec, screen: AltScreen): Promise<FormRe
|
|
|
279
286
|
const finish = (cancelled: boolean) => {
|
|
280
287
|
if (done) return
|
|
281
288
|
done = true
|
|
282
|
-
|
|
289
|
+
idle.dispose()
|
|
283
290
|
try {
|
|
284
291
|
formPump.dispose()
|
|
285
292
|
} catch {}
|
|
@@ -352,25 +359,56 @@ export async function runForm(spec: FormSpec, screen: AltScreen): Promise<FormRe
|
|
|
352
359
|
st.error = null
|
|
353
360
|
}
|
|
354
361
|
|
|
362
|
+
/**
|
|
363
|
+
* Esc tahap-1: kembalikan nilai bawaan field AKTIF, apa pun jenisnya.
|
|
364
|
+
*
|
|
365
|
+
* Kontrak I21 menyebut "semua input teks/pilihan", tapi dulu hanya
|
|
366
|
+
* text/secret yang di-revert — pada select/confirm Esc langsung menutup
|
|
367
|
+
* seluruh form dan perubahan field lain ikut hilang (audit 2026-11-12,
|
|
368
|
+
* MEDIUM-6). `false` = sudah di nilai bawaan → lanjut ke tahap-2 (batal).
|
|
369
|
+
*/
|
|
370
|
+
const revertField = (cur: FieldState): boolean => {
|
|
371
|
+
if (cur.def.kind === "select") {
|
|
372
|
+
const n = Math.max(1, cur.def.options?.length ?? 1)
|
|
373
|
+
const init = typeof cur.def.initial === "number" ? cur.def.initial : 0
|
|
374
|
+
const target = Math.min(Math.max(0, init), n - 1)
|
|
375
|
+
if (cur.selectIdx === target) return false
|
|
376
|
+
cur.selectIdx = target
|
|
377
|
+
cur.error = null
|
|
378
|
+
return true
|
|
379
|
+
}
|
|
380
|
+
if (cur.def.kind === "confirm") {
|
|
381
|
+
const target = typeof cur.def.initial === "boolean" ? cur.def.initial : false
|
|
382
|
+
if (cur.confirmed === target) return false
|
|
383
|
+
cur.confirmed = target
|
|
384
|
+
cur.error = null
|
|
385
|
+
return true
|
|
386
|
+
}
|
|
387
|
+
const initial = typeof cur.def.initial === "string" ? cur.def.initial : ""
|
|
388
|
+
if (cur.text === initial) return false
|
|
389
|
+
cur.text = initial
|
|
390
|
+
cur.cursor = toGraphemes(initial).length
|
|
391
|
+
cur.viewStart = 0
|
|
392
|
+
cur.error = null
|
|
393
|
+
return true
|
|
394
|
+
}
|
|
395
|
+
|
|
355
396
|
const handleKeys = (keys: DecodedKey[]): boolean => {
|
|
356
397
|
if (done) return true
|
|
357
398
|
resetIdle()
|
|
358
|
-
const st = fields[idx]!
|
|
359
399
|
for (const d of keys) {
|
|
360
400
|
const k = d.key
|
|
401
|
+
// Field STATE dibaca ulang tiap iterasi: Tab/↑/↓ di dalam chunk yang
|
|
402
|
+
// sama memindahkan `idx`, dan pointer yang dicapture sebelum loop
|
|
403
|
+
// membuat ketikan masuk ke field SEBELUMNYA (audit 2026-11-12, HIGH-1).
|
|
404
|
+
const st = fields[idx]!
|
|
361
405
|
// Esc/Ctrl+C dua-tahap (anti-hilang draft): field kotor → kembalikan
|
|
362
406
|
// ke nilai bawaan (prefill edit tak hilang sekali tekan — dulu
|
|
363
407
|
// langsung dikosongkan tanpa undo); sudah bawaan/kosong → batal
|
|
364
408
|
// total. Ctrl+D = batal langsung (konvensi EOF, tidak dipakai
|
|
365
409
|
// menghapus).
|
|
366
410
|
if (k.type === "esc" || k.type === "ctrl-c") {
|
|
367
|
-
|
|
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
|
|
411
|
+
if (revertField(fields[idx]!)) {
|
|
374
412
|
render()
|
|
375
413
|
continue
|
|
376
414
|
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Lifecycle popup bersama — satu sumber untuk idle-timer jendela interaktif
|
|
2
|
+
// (picker, form, model/provider manager, wizard).
|
|
3
|
+
//
|
|
4
|
+
// Masalah yang dipecahkan (audit D1): keempat layar menulis ulang pola idle
|
|
5
|
+
// yang sama dengan nuansa kecil berbeda — raw-mode digenggam tanpa input saat
|
|
6
|
+
// ConPTY mati/fokus hilang membuat popup "macet" (dan Bun Windows rawan
|
|
7
|
+
// segfault setelah ±133 dtk idle, lihat picker.ts). Dengan satu helper,
|
|
8
|
+
// semantiknya seragam: SEMUA timer di-unref (tak menahan proses), fase batal
|
|
9
|
+
// selalu ada, dan `dispose()` membuat reset berikutnya no-op (anti-timer-bocor
|
|
10
|
+
// sesudah selesai).
|
|
11
|
+
//
|
|
12
|
+
// Dua gaya yang didukung:
|
|
13
|
+
// 1. Satu tahap (picker/manager): cancelMs tercapai → langsung batal.
|
|
14
|
+
// 2. Dua tahap (form, audit TUI-004): warnMs → peringatan (draft DIPERTAHAN-
|
|
15
|
+
// kan, render footer merah); cancelMs lagi tanpa keypress → batal.
|
|
16
|
+
// TOTAL tetap warnMs + cancelMs.
|
|
17
|
+
|
|
18
|
+
export interface IdleTimerOptions {
|
|
19
|
+
/** Fase 1 (opsional): jeda diam → peringatan. Tanpa ini = langsung batal. */
|
|
20
|
+
warnMs?: number
|
|
21
|
+
/** Fase 1 (tanpa warnMs) / fase 2 (dengan warnMs): jeda → batal. */
|
|
22
|
+
cancelMs: number
|
|
23
|
+
/** Dipanggil saat peringatan (hanya bila warnMs diisi). */
|
|
24
|
+
onWarn?: () => void
|
|
25
|
+
/** Dipanggil saat batal karena diam. Wajib. */
|
|
26
|
+
onCancel: () => void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface IdleTimer {
|
|
30
|
+
/** Restart timer (dipanggil tiap keypress/aktivitas). No-op sesudah dispose. */
|
|
31
|
+
reset(): void
|
|
32
|
+
/** Hentikan timer tanpa mematikan (dipanggil suspend/pause). */
|
|
33
|
+
clear(): void
|
|
34
|
+
/** Matikan selamanya: clear + reset berikutnya no-op (dipakai saat finish). */
|
|
35
|
+
dispose(): void
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Buat idle-timer dua-fase dengan unref otomatis. */
|
|
39
|
+
export function createIdleTimer(opts: IdleTimerOptions): IdleTimer {
|
|
40
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
41
|
+
let disposed = false
|
|
42
|
+
const unref = (t: ReturnType<typeof setTimeout>) => {
|
|
43
|
+
// jangan tahan proses hidup hanya karena timer ini (stdin stub test bisa
|
|
44
|
+
// tanpa API unref — anggap aman).
|
|
45
|
+
try {
|
|
46
|
+
;(t as unknown as { unref?: () => void }).unref?.()
|
|
47
|
+
} catch {}
|
|
48
|
+
}
|
|
49
|
+
const clear = () => {
|
|
50
|
+
if (timer) clearTimeout(timer)
|
|
51
|
+
timer = undefined
|
|
52
|
+
}
|
|
53
|
+
const fireCancel = () => {
|
|
54
|
+
if (disposed) return
|
|
55
|
+
opts.onCancel()
|
|
56
|
+
}
|
|
57
|
+
const reset = () => {
|
|
58
|
+
if (disposed) return
|
|
59
|
+
clear()
|
|
60
|
+
if (opts.warnMs != null) {
|
|
61
|
+
timer = setTimeout(() => {
|
|
62
|
+
// Tahap 1: peringatan — konten tidak dibuang, render ulang oleh caller.
|
|
63
|
+
if (disposed) return
|
|
64
|
+
try {
|
|
65
|
+
opts.onWarn?.()
|
|
66
|
+
} catch {}
|
|
67
|
+
timer = setTimeout(fireCancel, opts.cancelMs)
|
|
68
|
+
unref(timer)
|
|
69
|
+
}, opts.warnMs)
|
|
70
|
+
} else {
|
|
71
|
+
timer = setTimeout(fireCancel, opts.cancelMs)
|
|
72
|
+
}
|
|
73
|
+
unref(timer)
|
|
74
|
+
}
|
|
75
|
+
const dispose = () => {
|
|
76
|
+
disposed = true
|
|
77
|
+
clear()
|
|
78
|
+
}
|
|
79
|
+
return { reset, clear, dispose }
|
|
80
|
+
}
|