minicode-ai 0.27.0 → 0.30.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 (145) hide show
  1. package/README.md +1 -1
  2. package/cli/args.ts +184 -57
  3. package/cli/bootstrap/presentation-bridge.ts +383 -0
  4. package/cli/commands/acp.ts +45 -29
  5. package/cli/commands/auth.ts +17 -17
  6. package/cli/commands/config.ts +65 -66
  7. package/cli/commands/daemon.ts +44 -16
  8. package/cli/commands/doctor.ts +18 -15
  9. package/cli/commands/exec.ts +41 -89
  10. package/cli/commands/mcp.ts +25 -8
  11. package/cli/commands/memory.ts +16 -24
  12. package/cli/commands/openwork.ts +6 -2
  13. package/cli/commands/pricing.ts +14 -12
  14. package/cli/commands/providers.ts +36 -57
  15. package/cli/commands/sessions.ts +24 -18
  16. package/cli/commands/skills.ts +33 -18
  17. package/cli/commands/stats.ts +15 -30
  18. package/cli/commands.ts +73 -44
  19. package/cli/context-command-run.ts +30 -18
  20. package/cli/index.ts +51 -100
  21. package/cli/model-manager.ts +7 -2
  22. package/cli/provider-manager.ts +33 -18
  23. package/cli/router.ts +41 -12
  24. package/cli/setup.ts +241 -485
  25. package/cli/tui.ts +303 -100
  26. package/docs/TERMINAL_CONTRACT.md +258 -73
  27. package/docs/UI_RENDER_PIPELINE.md +9 -1
  28. package/docs/agents.md +2 -2
  29. package/docs/architecture.md +17 -32
  30. package/docs/choosing-mode.md +7 -9
  31. package/docs/cli.md +3 -5
  32. package/docs/environment.md +1 -2
  33. package/docs/policy-sandbox.md +3 -3
  34. package/docs/repl.md +2 -2
  35. package/docs/tools.md +1 -1
  36. package/package.json +1 -1
  37. package/src/app/mentions.ts +60 -13
  38. package/src/app/rag-layer.ts +17 -20
  39. package/src/app/session.ts +144 -68
  40. package/src/app/tool-layer.ts +38 -16
  41. package/src/config.ts +64 -104
  42. package/src/constants.ts +4 -6
  43. package/src/daemon/capability.ts +8 -1
  44. package/src/daemon/control.ts +3 -2
  45. package/src/daemon/server.ts +57 -0
  46. package/src/hooks/run.ts +73 -41
  47. package/src/lib/atomic-write.ts +31 -59
  48. package/src/lib/db-path.ts +47 -0
  49. package/src/lib/process-tree.ts +62 -0
  50. package/src/lib/public-http.ts +88 -23
  51. package/src/lib/trusted-exec.ts +48 -1
  52. package/src/lsp/client.ts +62 -17
  53. package/src/mcp/client.ts +85 -38
  54. package/src/mcp/http-transport.ts +28 -16
  55. package/src/mcp/transport.ts +42 -6
  56. package/src/memory/files.ts +164 -134
  57. package/src/memory/vector.ts +371 -420
  58. package/src/policy/approval-binding.ts +122 -45
  59. package/src/policy/bash-guard.ts +18 -7
  60. package/src/policy/compaction.ts +51 -18
  61. package/src/policy/context-window.ts +31 -8
  62. package/src/policy/context.ts +62 -36
  63. package/src/policy/executor.ts +17 -7
  64. package/src/policy/jail.ts +6 -8
  65. package/src/policy/microcompact.ts +4 -3
  66. package/src/policy/no-progress.ts +103 -22
  67. package/src/policy/permission.ts +36 -12
  68. package/src/policy/tool-contract.ts +74 -416
  69. package/src/policy/usage.ts +24 -2
  70. package/src/policy/verifier.ts +50 -2
  71. package/src/policy/work-mode.ts +28 -54
  72. package/src/presentation/adapter.ts +24 -16
  73. package/src/providers/anthropic.ts +16 -5
  74. package/src/providers/provision.ts +6 -56
  75. package/src/providers/responses.ts +38 -7
  76. package/src/providers/sse-bounds.ts +6 -4
  77. package/src/runtime/execution-journal.ts +4 -29
  78. package/src/sandbox/docker.ts +58 -34
  79. package/src/sandbox/mask.ts +157 -0
  80. package/src/sandbox/os.ts +107 -73
  81. package/src/session/authority.ts +29 -10
  82. package/src/session/context-projection.ts +6 -5
  83. package/src/session/db/runs.ts +397 -0
  84. package/src/session/db/schema.ts +358 -0
  85. package/src/session/open-work.ts +26 -3
  86. package/src/session/persistence.ts +74 -998
  87. package/src/session/verification.ts +170 -20
  88. package/src/skills/loader.ts +31 -16
  89. package/src/task/db/schema.ts +325 -0
  90. package/src/task/db/types.ts +269 -0
  91. package/src/task/production-scheduler.ts +13 -1
  92. package/src/task/store.ts +92 -1021
  93. package/src/telemetry/trace.ts +27 -0
  94. package/src/tools/ask_user.ts +1 -13
  95. package/src/tools/bash.ts +27 -92
  96. package/src/tools/code_run.ts +1 -1
  97. package/src/tools/context.ts +133 -0
  98. package/src/tools/delete_file.ts +3 -2
  99. package/src/tools/edit.ts +37 -72
  100. package/src/tools/git.ts +86 -9
  101. package/src/tools/glob.ts +26 -51
  102. package/src/tools/grep.ts +67 -57
  103. package/src/tools/hashline.ts +4 -0
  104. package/src/tools/lsp.ts +11 -8
  105. package/src/tools/mcp_call.ts +9 -6
  106. package/src/tools/memory.ts +56 -33
  107. package/src/tools/move_file.ts +36 -10
  108. package/src/tools/patch.ts +13 -21
  109. package/src/tools/read_file.ts +18 -25
  110. package/src/tools/read_image.ts +35 -33
  111. package/src/tools/submit_result.ts +29 -8
  112. package/src/tools/task.ts +74 -17
  113. package/src/tools/todo.ts +181 -74
  114. package/src/tools/web_fetch.ts +42 -2
  115. package/src/tools/web_search.ts +47 -8
  116. package/src/ui/approval/prompt.ts +206 -47
  117. package/src/ui/assistant/turn-status.ts +14 -5
  118. package/src/ui/footer.ts +4 -4
  119. package/src/ui/i18n/en.ts +47 -23
  120. package/src/ui/i18n/id.ts +76 -51
  121. package/src/ui/input/input.ts +4 -3
  122. package/src/ui/input/prompt-engine.ts +35 -5
  123. package/src/ui/render/activity-panel.ts +4 -2
  124. package/src/ui/render/hint.ts +7 -4
  125. package/src/ui/render/list.ts +32 -0
  126. package/src/ui/render/markdown.ts +6 -3
  127. package/src/ui/render/notice.ts +19 -0
  128. package/src/ui/render/table-grid.ts +7 -8
  129. package/src/ui/render/theme.ts +113 -39
  130. package/src/ui/runtime/screen.ts +74 -10
  131. package/src/ui/runtime/spinner.ts +19 -6
  132. package/src/ui/screens/dialog.ts +19 -1
  133. package/src/ui/screens/form.ts +80 -35
  134. package/src/ui/screens/model-manager.ts +82 -61
  135. package/src/ui/screens/output-window.ts +263 -0
  136. package/src/ui/screens/picker.ts +94 -81
  137. package/src/ui/screens/provider-manager.ts +100 -69
  138. package/src/ui/screens/wizard.ts +117 -28
  139. package/src/ui/tui/app.ts +196 -33
  140. package/src/ui/tui/transcript.ts +158 -25
  141. package/vendor/minicore/src/core/executor.ts +2 -0
  142. package/vendor/minicore/src/core/loop.ts +17 -6
  143. package/vendor/minicore/src/core/session.ts +3 -0
  144. package/vendor/minicore/src/core/tool.ts +1 -0
  145. package/vendor/minicore/src/providers/openai-compat.ts +35 -19
@@ -9,12 +9,15 @@ import {
9
9
  type DecodedKey,
10
10
  type DecoderState,
11
11
  } from "../input/prompt-engine.ts"
12
+ import { formatError } from "../render/errors.ts"
12
13
  import { hintLine } from "../render/hint.ts"
13
- import { cleanUntrusted, sanitizeAnsiLine } from "../render/sanitize.ts"
14
- import { c, glyphs, stripAnsi } from "../render/theme.ts"
15
- import { truncateToWidth } from "../render/width.ts"
14
+ import { listRow } from "../render/list.ts"
15
+ import { type NoticeKind, paintNoticeLines } from "../render/notice.ts"
16
+ import { sanitizeAnsiLine } from "../render/sanitize.ts"
17
+ import { c, glyphs } from "../render/theme.ts"
18
+ import { displayWidth, truncateToWidth, wrapByWidth } from "../render/width.ts"
16
19
  import { type AltScreen, openAltScreen } from "../runtime/screen.ts"
17
- import { boxLeftPad, dialogBox, popupBoxWidth } from "./dialog.ts"
20
+ import { canRenderModal, dialogBox, popupBoxWidth } from "./dialog.ts"
18
21
  import { runForm, validateRequired, validateUrl } from "./form.ts"
19
22
  import { createIdleTimer } from "./lifecycle.ts"
20
23
  import { runPicker } from "./picker.ts"
@@ -82,6 +85,7 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
82
85
  // baru. Pengecualian sadar: "Detecting models…" (progres jaringan saat
83
86
  // popup tersuspend — tanpa itu tunggu terasa hang).
84
87
  let notice: string | null = null
88
+ let noticeKind: NoticeKind = "info"
85
89
 
86
90
  async function reload() {
87
91
  providers = await opts.loadRows()
@@ -90,22 +94,21 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
90
94
  }
91
95
 
92
96
  return new Promise<void>((resolve) => {
97
+ const width = () => Math.max(8, providerBoxWidth() - 4)
98
+ const renderedNoticeLines = (): string[] =>
99
+ notice ? paintNoticeLines(noticeKind, notice).flatMap((l) => wrapByWidth(l, width())) : []
93
100
  // Ukuran mengikuti terminal sungguhan; satu baris border atas/bawah dan
94
101
  // kemungkinan notice tetap masuk ke perhitungan tinggi.
95
102
  const visibleRows = () => {
96
103
  const rows = process.stdout.rows || 24
97
- // Chrome tetap: 2 border + 1 baris hint aksi; notice (transient)
98
- // menyusutkan daftar satu baris — paritas model-manager, agar hint
99
- // tak pernah jadi korban potong atas dialogBox.
100
- const fixed = 3 + (notice ? 1 : 0)
104
+ // Chrome tetap: 2 border + 1 judul + 1 footer (dipin `dialogBox`);
105
+ // notice (transient) menyusutkan daftar agar kotak tetap ringkas.
106
+ const fixed = 4 + renderedNoticeLines().length
101
107
  return Math.max(1, Math.min(rows - fixed, 14))
102
108
  }
103
- // Lebar konten = lebar kotak (dibekukan saat mount) dikurangi chrome
104
- // dialog (lihat picker.ts).
105
- const width = () => Math.max(8, providerBoxWidth() - 4)
106
109
 
107
- // Isi popup ringkas: nama provider dan hasil aksi sementara.
108
- const bodyLines = (): string[] => {
110
+ // Isi popup ringkas: nama provider + jumlah model redup dan status aktif.
111
+ const bodyLines = (): { lines: string[]; footer: string } => {
109
112
  const v = visibleRows()
110
113
  if (sel < scroll) scroll = sel
111
114
  if (sel >= scroll + v) scroll = sel - v + 1
@@ -119,40 +122,36 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
119
122
  for (let i = 0; i < rows.length; i++) {
120
123
  const it = rows[i]!
121
124
  const picked = i === sel - scroll
122
- // Nama provider dan status aktif cukup untuk memilih tanpa memenuhi
123
- // daftar dengan URL dan jumlah model.
124
125
  const aktif = opts.currentModel?.startsWith(`${it.id}::`)
125
126
  ? ` (${t("common.active")})`
126
127
  : ""
127
- const label = `${truncateToWidth(sanitizeAnsiLine(it.id), Math.max(8, w - 4 - aktif.length), "…")}${aktif}`
128
- if (picked) lines.push(` ${c.accent("›")} ${c.accent(c.bold(label))}`)
129
- else lines.push(` ${dim(label)}`)
128
+ const countTag = it.models > 0 && w >= 28 ? ` (${it.models})` : ""
129
+ const suffix = `${countTag}${aktif}`
130
+ const nameBudget = Math.max(8, w - 4 - displayWidth(suffix))
131
+ const label = `${truncateToWidth(sanitizeAnsiLine(it.id), nameBudget, glyphs.ellipsis)}${suffix}`
132
+ lines.push(listRow(label, picked))
130
133
  }
131
134
  }
132
- // Struk transient (jalur legacy maupun modal).
133
- if (notice) lines.push(cut(dim(sanitizeAnsiLine(notice))))
134
- // Hint aksi minimalis (kunci terang, label redup) — paritas /model.
135
- lines.push(
136
- cut(
137
- hintLine([
138
- { key: "Enter", label: t("hint.select") },
139
- { key: "a", label: t("hint.add") },
140
- { key: "e", label: t("hint.edit") },
141
- { key: "d", label: t("hint.delete") },
142
- { key: "Esc", label: t("hint.close") },
143
- ]),
144
- ),
145
- )
146
- return lines
135
+ // Struk transient (jalur modal — notice dapat di-clip tanpa memotong footer).
136
+ for (const line of renderedNoticeLines()) lines.push(cut(line))
137
+ const footer = hintLine([
138
+ { key: "Enter", label: t("hint.select") },
139
+ { key: "a", label: t("hint.add") },
140
+ { key: "e", label: t("hint.edit") },
141
+ { key: "d", label: t("hint.delete") },
142
+ { key: "Esc", label: t("hint.cancel") },
143
+ ])
144
+ return { lines, footer }
147
145
  }
148
146
 
149
147
  const render = () => {
150
- // Popup komposit: HANYA region kotak (tanpa clear) di atas konten
151
- // Lebar beku (min=max, paritas picker/model).
148
+ const { lines, footer } = bodyLines()
149
+ // Popup komposit: HANYA region kotak (tanpa clear) di atas konten.
152
150
  const box = dialogBox(
153
151
  {
154
- title: "",
155
- body: bodyLines(),
152
+ title: t("out.titleProviders"),
153
+ body: lines,
154
+ footer,
156
155
  minWidth: providerBoxWidth(),
157
156
  maxWidth: providerBoxWidth(),
158
157
  },
@@ -160,20 +159,14 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
160
159
  screen.rows,
161
160
  )
162
161
  screen.paintRegion(box.lines, box.topRow)
163
- // Parkir kursor di baris item aktif (paritas picker/form). Indeks via
164
- // box.bodyTop — tebak manual meleset tiap judul berubah.
165
- const itemRow = Math.min(Math.max(0, sel - scroll), Math.max(0, visibleRows() - 1))
166
- const li = Math.min(box.lines.length - 1, box.bodyTop + itemRow)
167
- const row = box.topRow + li
168
- try {
169
- const leftPad = boxLeftPad(stripAnsi(box.lines[li] ?? ""))
170
- process.stdout.write(`\x1b[${row};${Math.max(1, leftPad + 1)}H\x1b[?25h`)
171
- } catch {}
162
+ // Daftar murni (tanpa input ketik) — sembunyikan kursor supaya bersih.
163
+ screen.setCursorVisible(false)
172
164
  }
173
165
 
174
166
  // Popup butuh layar mampu; selain itu tolak BERSUARA + batal.
175
167
  const screen: AltScreen = openAltScreen()
176
- const useModal = screen.ok && (process.stdout.rows || 24) >= 10
168
+ const useModal =
169
+ screen.ok && canRenderModal(process.stdout.columns || 80, process.stdout.rows || 24)
177
170
  if (!useModal) {
178
171
  try {
179
172
  console.log(`⚠ ${t("gate.tuiProvider")}`)
@@ -214,7 +207,14 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
214
207
  const suspend = () => {
215
208
  idle.clear()
216
209
  attached = false
217
- process.stdout.write("\x1b[0m\x1b[?25h")
210
+ // Hapus region kotak induk sebelum sub-popup tampil agar bingkai tak
211
+ // tertinggal terpotong; saat exit final `done` sudah true.
212
+ if (!done) {
213
+ try {
214
+ screen.clearRegion()
215
+ } catch {}
216
+ screen.setCursorVisible(false)
217
+ }
218
218
  // setRawMode dibungkus try/catch: di Windows handle bisa rusak dan
219
219
  // melempar — tanpa ini suspend melempar lalu Promise tak pernah settle.
220
220
  // TANPA pause — stdin mengalir seumur proses (lihat cleanup askLine).
@@ -286,8 +286,9 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
286
286
  try {
287
287
  await fn()
288
288
  } catch (e) {
289
- // Pesan controller/jaringan = input tak terpercaya: sanitasi.
290
- notice = `${glyphs.cross} ${sanitizeAnsiLine((e as Error).message)}`
289
+ // Formatter kanonik: redaksi credential + saran tindakan.
290
+ notice = formatError(e)
291
+ noticeKind = "error"
291
292
  } finally {
292
293
  busy = false
293
294
  await reload().catch(() => {})
@@ -316,6 +317,9 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
316
317
  try {
317
318
  void runPicker({
318
319
  title: t("prov.addTitle"),
320
+ // Jendela command: judul tampil seragam dengan window lain.
321
+ showTitle: true,
322
+ screen,
319
323
  items: [
320
324
  ...opts.presets.map((p) => ({ name: p.label, provider: "", value: p.id })),
321
325
  { name: t("prov.customUrl"), provider: "", value: CUSTOM },
@@ -330,6 +334,7 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
330
334
  })
331
335
  if (picked == null) {
332
336
  notice = t("common.canceled")
337
+ noticeKind = "info"
333
338
  return
334
339
  }
335
340
  const preset = picked === CUSTOM ? undefined : opts.presets.find((p) => p.id === picked)
@@ -373,10 +378,12 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
373
378
  const form = await runForm({ title: t("prov.addTitle"), fields }, screen)
374
379
  if (form.cancelled || !form.values) {
375
380
  notice = t("common.canceled")
381
+ noticeKind = "info"
376
382
  return
377
383
  }
378
384
  if (duplicate && form.values.overwrite !== "y") {
379
385
  notice = t("common.canceled")
386
+ noticeKind = "info"
380
387
  return
381
388
  }
382
389
  const baseUrl = (form.values.url ?? "").trim()
@@ -385,16 +392,22 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
385
392
  // Progres terlihat: render notice DULU (masih suspend = tanpa
386
393
  // listener ganda), baru await jaringan.
387
394
  notice = t("ntc.detecting")
395
+ noticeKind = "info"
388
396
  render()
389
397
  const res = await withWatchdog(() => opts.onAdd({ preset, baseUrl, apiKey, scope }))
390
- // Hasil dari jaringan/config — sanitasi sebelum tampil.
391
- if (res.ok) notice = `${glyphs.check} ${sanitizeAnsiLine(res.ok)}`
392
- else if (res.err) notice = `${glyphs.cross} ${sanitizeAnsiLine(res.err)}`
398
+ // Hasil dari jaringan/config — formatter kanonik (redaksi + sanitasi).
399
+ if (res.ok) {
400
+ notice = res.ok
401
+ noticeKind = "success"
402
+ } else if (res.err) {
403
+ notice = formatError(res.err)
404
+ noticeKind = "error"
405
+ }
393
406
  } catch (e) {
394
- // Pesan error jaringan/controller = input tak-terpercaya: tanpa
395
- // sanitasi, body provider bisa menyuntik `ESC[2J` ke popup dan
396
- // menghapus layar TUI (audit 2026-11-12, MEDIUM-2).
397
- notice = `${glyphs.cross} ${sanitizeAnsiLine((e as Error).message)}`
407
+ // Body provider bisa menyuntik `ESC[2J`; formatError menyanitasi +
408
+ // meredaksi credential sebelum dirender.
409
+ notice = formatError(e)
410
+ noticeKind = "error"
398
411
  } finally {
399
412
  busy = false
400
413
  await reload().catch(() => {})
@@ -425,17 +438,27 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
425
438
  kind: "confirm",
426
439
  },
427
440
  ],
428
- footer: t("prov.deleteFooter"),
441
+ footerHints: [
442
+ { key: `${glyphs.leftRight}/y/n`, label: t("hint.select") },
443
+ { key: "Enter", label: t("hint.delete") },
444
+ { key: "Esc", label: t("hint.cancel") },
445
+ ],
429
446
  },
430
447
  screen,
431
448
  )
432
449
  if (form.cancelled || form.values?.ok !== "y") {
433
450
  notice = t("common.canceled")
451
+ noticeKind = "info"
434
452
  return
435
453
  }
436
454
  const res = await withWatchdog(() => opts.onDelete(target))
437
- if (res.ok) notice = `${glyphs.check} ${sanitizeAnsiLine(res.ok)}`
438
- else if (res.err) notice = `${glyphs.cross} ${sanitizeAnsiLine(res.err)}`
455
+ if (res.ok) {
456
+ notice = res.ok
457
+ noticeKind = "success"
458
+ } else if (res.err) {
459
+ notice = formatError(res.err)
460
+ noticeKind = "error"
461
+ }
439
462
  })
440
463
  }
441
464
 
@@ -447,6 +470,7 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
447
470
  const defaults = await opts.onEditDefaults(target)
448
471
  if (!defaults) {
449
472
  notice = t("ntc.notFound")
473
+ noticeKind = "error"
450
474
  return
451
475
  }
452
476
  // Form edit dalam popup (prefill defaults). Batal di mana pun =
@@ -465,12 +489,12 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
465
489
  // Key kosong = pertahankan lama (tanpa validate required).
466
490
  { id: "key", label: t("prov.keyKeepLabel"), kind: "secret", initial: "" },
467
491
  ],
468
- footer: t("form.footerDefault"),
469
492
  },
470
493
  screen,
471
494
  )
472
495
  if (form.cancelled || !form.values) {
473
496
  notice = t("common.canceled")
497
+ noticeKind = "info"
474
498
  return
475
499
  }
476
500
  // Key kosong = pertahankan lama (placeholder [****] memberi tahu).
@@ -478,12 +502,19 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
478
502
  const apiKey = (form.values.key ?? "").trim() || defaults.apiKey
479
503
  if (baseUrl === defaults.baseUrl && apiKey === defaults.apiKey) {
480
504
  notice = t("ntc.noChange")
505
+ noticeKind = "info"
481
506
  } else {
482
507
  notice = t("ntc.detecting")
508
+ noticeKind = "info"
483
509
  render()
484
510
  const res = await withWatchdog(() => opts.onEditSave(target, { baseUrl, apiKey }))
485
- if (res.ok) notice = `${glyphs.check} ${sanitizeAnsiLine(res.ok)}`
486
- else if (res.err) notice = `${glyphs.cross} ${sanitizeAnsiLine(res.err)}`
511
+ if (res.ok) {
512
+ notice = res.ok
513
+ noticeKind = "success"
514
+ } else if (res.err) {
515
+ notice = formatError(res.err)
516
+ noticeKind = "error"
517
+ }
487
518
  }
488
519
  })
489
520
  }
@@ -497,16 +528,20 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
497
528
  // Jalur tunggal key (sinkron + flush lone-ESC 50ms). True = selesai.
498
529
  const handleProviderKeys = (keys: DecodedKey[]): boolean => {
499
530
  if (busy || done) return true
531
+ if (!keys.length) return false
500
532
  // Struk lama dibersihkan tiap keypress baru (transient, bukan arsip).
501
533
  notice = null
534
+ noticeKind = "info"
502
535
  try {
503
536
  for (const d of keys) {
504
537
  switch (d.key.type) {
505
538
  case "up":
539
+ case "wheelup":
506
540
  sel = Math.max(0, sel - 1)
507
541
  render()
508
542
  break
509
543
  case "down":
544
+ case "wheeldown":
510
545
  // Daftar kosong: jangan biarkan sel=-1 (Enter lalu salah tutup).
511
546
  sel = providers.length ? Math.min(providers.length - 1, sel + 1) : 0
512
547
  render()
@@ -609,7 +644,7 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
609
644
 
610
645
  const onResize = () => render()
611
646
 
612
- process.stdout.write("\x1b[?25l")
647
+ screen.setCursorVisible(false)
613
648
  try {
614
649
  process.stdin.setRawMode(true)
615
650
  process.stdin.resume()
@@ -627,11 +662,7 @@ export async function runProviderManagerView(opts: ProviderManagerViewOptions):
627
662
  } catch {}
628
663
  try {
629
664
  // Pesan setup gagal (bisa memuat body jaringan) = teks tak-terpercaya.
630
- console.log(
631
- `⚠ ${t("prov.failOpen", {
632
- msg: cleanUntrusted(String((e as Error)?.message ?? e), !!process.stdout.isTTY),
633
- })}`,
634
- )
665
+ console.log(`⚠ ${t("prov.failOpen", { msg: formatError(e) })}`)
635
666
  } catch {}
636
667
  try {
637
668
  resolve()
@@ -4,11 +4,15 @@
4
4
  // ketikan di luar popup.
5
5
  import { formatError } from "../assistant/simple.ts"
6
6
  import { t } from "../i18n/locale.ts"
7
+ import { createDecoderState, createKeyStreamPump } from "../input/prompt-engine.ts"
8
+ import { hintLine } from "../render/hint.ts"
7
9
  import { sanitizeAnsiLine } from "../render/sanitize.ts"
8
- import { c, glyphs } from "../render/theme.ts"
10
+ import { c } from "../render/theme.ts"
11
+ import { wrapByWidth } from "../render/width.ts"
9
12
  import { openAltScreen } from "../runtime/screen.ts"
10
- import { dialogBox } from "./dialog.ts"
11
- import { runForm, validateUrl } from "./form.ts"
13
+ import { canRenderModal, dialogBox, popupBoxWidth } from "./dialog.ts"
14
+ import { IDLE_CANCEL_MS, IDLE_WARN_MS, runForm, validateUrl } from "./form.ts"
15
+ import { createIdleTimer } from "./lifecycle.ts"
12
16
  import { runPicker } from "./picker.ts"
13
17
  import { withTimeout } from "./watchdog.ts"
14
18
 
@@ -33,14 +37,74 @@ export interface SetupWizardViewOptions {
33
37
  onSubmit(baseUrl: string, apiKey: string): Promise<string>
34
38
  }
35
39
 
36
- /** Lebar kotak wizard TETAP (paritas picker/form/manager — tak bernapas antar langkah). */
37
- const WIZARD_BOX_W = 64
40
+ /** Tunggu tombol tutup (Enter/Esc/Ctrl+C/Ctrl+D) pada dialog error wizard. */
41
+ async function waitForWizardDismiss(renderErr: () => void): Promise<void> {
42
+ return new Promise<void>((resolve) => {
43
+ let done = false
44
+ let onData!: (chunk: Buffer) => void
45
+ let onResize!: () => void
46
+ const idle = createIdleTimer({
47
+ cancelMs: IDLE_WARN_MS + IDLE_CANCEL_MS,
48
+ onCancel: () => finish(),
49
+ })
50
+ const finish = () => {
51
+ if (done) return
52
+ done = true
53
+ idle.dispose()
54
+ try {
55
+ pump.dispose()
56
+ } catch {}
57
+ try {
58
+ process.stdin.removeListener("data", onData)
59
+ } catch {}
60
+ try {
61
+ process.stdout.removeListener("resize", onResize)
62
+ } catch {}
63
+ try {
64
+ process.stdin.setRawMode(false)
65
+ } catch {}
66
+ resolve()
67
+ }
68
+ const pump = createKeyStreamPump({
69
+ state: createDecoderState(),
70
+ onKeys: (keys) => {
71
+ for (const d of keys) {
72
+ const k = d.key.type
73
+ if (k === "enter" || k === "esc" || k === "ctrl-c" || k === "ctrl-d") {
74
+ finish()
75
+ return true
76
+ }
77
+ }
78
+ idle.reset()
79
+ return false
80
+ },
81
+ })
82
+ onData = (chunk: Buffer) => {
83
+ idle.reset()
84
+ try {
85
+ pump.push(new Uint8Array(chunk))
86
+ } catch {
87
+ finish()
88
+ }
89
+ }
90
+ onResize = () => {
91
+ try {
92
+ renderErr()
93
+ } catch {}
94
+ }
95
+ try {
96
+ process.stdin.setRawMode(true)
97
+ process.stdin.resume()
98
+ process.stdin.on("data", onData)
99
+ process.stdout.on("resize", onResize)
100
+ idle.reset()
101
+ renderErr()
102
+ } catch {
103
+ finish()
104
+ }
105
+ })
106
+ }
38
107
 
39
- /**
40
- * Tunggu tanpa menahan process hidup (unref): jeda sukses/gagal wizard tak
41
- * boleh menahan exit bila stdin sudah ditutup. Gagal-di-kode-lama: timer
42
- * polos menahan event-loop 1.2/2.5 dtk.
43
- */
44
108
  function sleepUnref(ms: number): Promise<void> {
45
109
  return new Promise((r) => {
46
110
  const timer = setTimeout(r, ms)
@@ -59,7 +123,12 @@ function sleepUnref(ms: number): Promise<void> {
59
123
  export async function runSetupWizardView(opts: SetupWizardViewOptions): Promise<boolean> {
60
124
  if (!process.stdin.isTTY) return false
61
125
  const screen = openAltScreen()
62
- if (!screen.ok) return false
126
+ if (!screen.ok || !canRenderModal(process.stdout.columns || 80, process.stdout.rows || 24)) {
127
+ try {
128
+ screen.close()
129
+ } catch {}
130
+ return false
131
+ }
63
132
  let exitMsg: string | null = null
64
133
  try {
65
134
  const CUSTOM = "\u0000custom"
@@ -73,6 +142,9 @@ export async function runSetupWizardView(opts: SetupWizardViewOptions): Promise<
73
142
  title: t("wiz.gateway"),
74
143
  items,
75
144
  filterable: true,
145
+ // Step [1/3] butuh judul; picker biasa tetap tanpa judul (kontrak v7.8).
146
+ showTitle: true,
147
+ screen,
76
148
  onPick: (v) => {
77
149
  picked = v
78
150
  },
@@ -146,49 +218,66 @@ export async function runSetupWizardView(opts: SetupWizardViewOptions): Promise<
146
218
  }
147
219
  }
148
220
 
221
+ const statusWidth = popupBoxWidth(screen.cols || 80)
149
222
  // Progres sebagai baris dalam kotak (bukan spinner liar di bawah popup).
150
223
  const box = dialogBox(
151
224
  {
152
225
  title: t("wiz.title"),
153
226
  body: [c.muted(t("wiz.detecting"))],
154
- minWidth: WIZARD_BOX_W,
155
- maxWidth: WIZARD_BOX_W,
227
+ minWidth: statusWidth,
228
+ maxWidth: statusWidth,
156
229
  },
157
230
  screen.cols,
158
231
  screen.rows,
159
232
  )
160
233
  screen.paintRegion(box.lines, box.topRow)
234
+ screen.setCursorVisible(false)
161
235
  try {
162
236
  const message = await withTimeout(() => opts.onSubmit(targetUrl, apiKey), 60_000)
163
237
  // Pesan dari jaringan (nama model/gateway) — sanitasi sebelum tampil.
164
238
  screen.clearRegion()
239
+ const doneWidth = popupBoxWidth(screen.cols || 80)
165
240
  const doneBox = dialogBox(
166
241
  {
167
242
  title: t("wiz.title"),
168
- body: [`${glyphs.check} ${sanitizeAnsiLine(message)}`, "", t("wiz.done")],
169
- minWidth: WIZARD_BOX_W,
170
- maxWidth: WIZARD_BOX_W,
243
+ body: [sanitizeAnsiLine(message), "", t("wiz.done")],
244
+ minWidth: doneWidth,
245
+ maxWidth: doneWidth,
171
246
  },
172
247
  screen.cols,
173
248
  screen.rows,
174
249
  )
175
250
  screen.paintRegion(doneBox.lines, doneBox.topRow)
251
+ screen.setCursorVisible(false)
176
252
  await sleepUnref(1200)
177
253
  return true
178
254
  } catch (e) {
179
255
  screen.clearRegion()
180
- const errBox = dialogBox(
181
- {
182
- title: t("wiz.title"),
183
- body: [c.error(`${glyphs.cross} ${t("wiz.detectFail", { msg: formatError(e) })}`)],
184
- minWidth: WIZARD_BOX_W,
185
- maxWidth: WIZARD_BOX_W,
186
- },
187
- screen.cols,
188
- screen.rows,
189
- )
190
- screen.paintRegion(errBox.lines, errBox.topRow)
191
- await sleepUnref(2500)
256
+ const errRaw = t("wiz.detectFail", { msg: formatError(e) })
257
+ const renderErr = () => {
258
+ const w = popupBoxWidth(screen.cols || 80)
259
+ const innerW = Math.max(10, Math.min(w - 4, (screen.cols || 80) - 6))
260
+ const wrapped = errRaw
261
+ .split("\n")
262
+ .flatMap((line) => (line.trim() ? wrapByWidth(c.error(line), innerW) : [""]))
263
+ const errBox = dialogBox(
264
+ {
265
+ title: t("wiz.title"),
266
+ body: wrapped,
267
+ footer: hintLine([
268
+ { key: "Enter", label: t("hint.close") },
269
+ { key: "Esc", label: t("hint.cancel") },
270
+ ]),
271
+ minWidth: w,
272
+ maxWidth: w,
273
+ },
274
+ screen.cols,
275
+ screen.rows,
276
+ )
277
+ screen.paintRegion(errBox.lines, errBox.topRow)
278
+ screen.setCursorVisible(false)
279
+ }
280
+ await waitForWizardDismiss(renderErr)
192
281
  return false
193
282
  }
194
283
  } finally {