minicode-ai 0.9.28 → 0.10.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 (48) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/cli/args.ts +1 -0
  3. package/cli/commands.ts +219 -103
  4. package/cli/index.ts +8 -6
  5. package/cli/model-manager.ts +2 -7
  6. package/cli/provider-manager.ts +13 -13
  7. package/cli/setup.ts +10 -1
  8. package/cli/tui.ts +506 -0
  9. package/cli/wizard.ts +2 -1
  10. package/docs/ARCHITECTURE.html +41 -22
  11. package/docs/README.md +1 -0
  12. package/package.json +3 -3
  13. package/src/app/rag-layer.ts +7 -2
  14. package/src/config.ts +30 -1
  15. package/src/policy/bash-guard.ts +64 -8
  16. package/src/policy/update-check.ts +4 -1
  17. package/src/tools/bash.ts +8 -0
  18. package/src/tools/task.ts +6 -1
  19. package/src/tools/web_fetch.ts +11 -2
  20. package/src/tools/web_search.ts +4 -1
  21. package/src/ui/approval/prompt.ts +84 -18
  22. package/src/ui/assistant/simple.ts +55 -22
  23. package/src/ui/assistant/turn-status.ts +16 -7
  24. package/src/ui/footer.ts +14 -12
  25. package/src/ui/i18n/en.ts +225 -0
  26. package/src/ui/i18n/id.ts +227 -0
  27. package/src/ui/i18n/locale.ts +66 -0
  28. package/src/ui/input/input.ts +138 -77
  29. package/src/ui/input/prompt-engine.ts +97 -4
  30. package/src/ui/render/collapse.ts +8 -1
  31. package/src/ui/render/errors.ts +75 -50
  32. package/src/ui/render/format.ts +15 -5
  33. package/src/ui/render/highlight.ts +5 -2
  34. package/src/ui/render/table.ts +7 -4
  35. package/src/ui/render/theme.ts +8 -3
  36. package/src/ui/render/width.ts +17 -9
  37. package/src/ui/runtime/screen.ts +197 -0
  38. package/src/ui/screens/dialog.ts +174 -0
  39. package/src/ui/screens/form.ts +459 -0
  40. package/src/ui/screens/model-manager.ts +274 -127
  41. package/src/ui/screens/picker.ts +229 -98
  42. package/src/ui/screens/provider-manager.ts +371 -164
  43. package/src/ui/screens/wizard.ts +149 -102
  44. package/src/ui/tui/app.ts +745 -0
  45. package/src/ui/tui/transcript.ts +271 -0
  46. package/cli/repl.ts +0 -740
  47. package/src/ui/runtime/chrome.ts +0 -369
  48. package/src/ui/screens/overlay.ts +0 -61
@@ -0,0 +1,271 @@
1
+ // Transkrip TUI — kolektor append-only event bus menjadi baris logis.
2
+ //
3
+ // Kenapa kelas sendiri: App butuh sumber kebenaran tunggal untuk viewport
4
+ // (paint, scroll, resize) yang terpisah dari pengambilan input. Menyimpan
5
+ // baris LOGIS (tanpa wrap) supaya resize me-wrap ulang saat paint — lebar
6
+ // terminal dibaca saat paint, bukan saat event (kontrak I11).
7
+ //
8
+ // Mode compact saja di Fase 1: teks model mengalir, ledger tool satu baris
9
+ // ` › nama target` (grammar sama dengan REPL), error merah satu baris.
10
+ import type { UiBus } from "../contract.ts"
11
+ import { t } from "../i18n/locale.ts"
12
+ import { reasoning } from "../render/reasoning.ts"
13
+ import { sanitizeAnsi, sanitizeAnsiLine } from "../render/sanitize.ts"
14
+ import { c } from "../render/theme.ts"
15
+ import { chunkByWidth, truncateToWidth } from "../render/width.ts"
16
+
17
+ /** Cap memori: baris logis tertua dibuang diam-diam (kontrak I12). */
18
+ export const TRANSCRIPT_CAP = 5000
19
+ /** Isi tool per section untuk /expand: maks 10 section × 2000 char. */
20
+ const EXPAND_MAX_SECTIONS = 10
21
+ const EXPAND_MAX_CHARS = 2000
22
+
23
+ /**
24
+ * Sink approval TUI: pemilik layar (cli/tui.ts) mendaftarkan transkrip +
25
+ * repaint + suspend/resume agar prompt persetujuan tercatat di transkrip dan
26
+ * terlihat SEBELUM user menjawab. Suspend selama askLine menunggu jawaban:
27
+ * tanpa ini listener App + repaint live (stream/PgUp) berlomba dengan
28
+ * askLine — prompt tak terlihat, user mengetik buta. Tanpa sink
29
+ * (one-shot/exec/headless) approval memakai tulis langsung warisan.
30
+ * Satu layer (src/ui) sehingga impor aman dari siklus.
31
+ */
32
+ export interface ApprovalSink {
33
+ pushBlock(lines: string[]): void
34
+ repaint(): void
35
+ suspend(): void
36
+ resume(): void
37
+ }
38
+
39
+ let approvalSink: ApprovalSink | null = null
40
+
41
+ export function setApprovalSink(sink: ApprovalSink | null): void {
42
+ approvalSink = sink
43
+ }
44
+
45
+ export function getApprovalSink(): ApprovalSink | null {
46
+ return approvalSink
47
+ }
48
+
49
+ export interface BufferedSection {
50
+ label: string
51
+ text: string
52
+ }
53
+
54
+ function ledgerTarget(args: Record<string, unknown>): string | undefined {
55
+ if (typeof args.path === "string" && args.path) return args.path
56
+ const cmd = args.cmd ?? args.command
57
+ if (typeof cmd === "string" && cmd) return `$ ${cmd}`
58
+ return undefined
59
+ }
60
+
61
+ export class Transcript {
62
+ private lines: string[] = []
63
+ private pending = ""
64
+ private sections: BufferedSection[] = []
65
+ /** Thinking terakumulasi (tersanitasi). Minimized: penanda hidup + buffer
66
+ * /expand; expanded: mengalir redup seperti teks. */
67
+ private thinkingBuf = ""
68
+ private thinkingTail = ""
69
+ private unsubs: (() => void)[] = []
70
+
71
+ constructor(bus: UiBus) {
72
+ // Gagal subscribe = transcript mati total; biarkan throw (fail-closed).
73
+ this.unsubs = [
74
+ bus.on("provider:text", (e: { text: string }) => this.stream(e.text)),
75
+ bus.on("turn:started", () => {
76
+ this.commit()
77
+ this.commitThinking()
78
+ }),
79
+ bus.on("turn:completed", () => {
80
+ this.commit()
81
+ this.commitThinking()
82
+ }),
83
+ bus.on("execution:started", () => {
84
+ this.commit()
85
+ this.commitThinking()
86
+ }),
87
+ bus.on("execution:completed", (e) => this.ledger(e)),
88
+ bus.on("provider:extension", (e: { kind: string; data: unknown }) => this.extension(e)),
89
+ bus.on("context:compacted", (e: { reason: string }) =>
90
+ this.push(c.muted(t("ts.compacted", { reason: sanitizeAnsiLine(e.reason ?? "") }))),
91
+ ),
92
+ ]
93
+ }
94
+
95
+ /** Teks model mengalir — ditahan sebagai ekor hidup sampai commit. */
96
+ stream(text: string): void {
97
+ if (text) {
98
+ // Jawaban dimulai = fase thinking selesai.
99
+ this.commitThinking()
100
+ this.pending += text
101
+ }
102
+ }
103
+
104
+ /** Chunk reasoning (`provider:extension` kind reasoning, data {text}). */
105
+ extension(e: { kind: string; data: unknown }): void {
106
+ if (e.kind !== "reasoning") return
107
+ const text = (e.data as { text?: string } | null)?.text ?? ""
108
+ if (!text) return
109
+ const clean = sanitizeAnsi(text)
110
+ if (reasoning.visible) {
111
+ // Expanded: alir redup per baris (line-buffered, ekor hidup).
112
+ this.thinkingTail += clean
113
+ const parts = this.thinkingTail.split("\n")
114
+ for (let i = 0; i < parts.length - 1; i++) this.append(c.muted(parts[i] ?? ""))
115
+ this.thinkingTail = parts[parts.length - 1] ?? ""
116
+ } else {
117
+ // Minimized: penanda hidup + buffer untuk /expand (cap 20k).
118
+ this.thinkingBuf = (this.thinkingBuf + clean).slice(-20000)
119
+ }
120
+ }
121
+
122
+ /** Selesaikan fase thinking: minimized → buffer /expand (tanpa baris
123
+ * transkrip, tetap bersih); expanded → flush ekor redup. */
124
+ private commitThinking(): void {
125
+ if (this.thinkingTail) {
126
+ this.append(c.muted(this.thinkingTail))
127
+ this.thinkingTail = ""
128
+ }
129
+ if (this.thinkingBuf.trim()) {
130
+ this.sections.push({ label: "thinking", text: this.thinkingBuf.trim() })
131
+ if (this.sections.length > EXPAND_MAX_SECTIONS) {
132
+ this.sections.splice(0, this.sections.length - EXPAND_MAX_SECTIONS)
133
+ }
134
+ }
135
+ this.thinkingBuf = ""
136
+ }
137
+
138
+ /** Dorong baris logis (sudah final, mis. gema prompt user). */
139
+ push(line: string): void {
140
+ this.commit()
141
+ this.append(line)
142
+ }
143
+
144
+ /** Gema prompt user ala shell: `minicode › baris-1`, lanjutan menjorok. */
145
+ pushUser(prompt: string): void {
146
+ const rows = sanitizeAnsi(prompt).split("\n")
147
+ const [first, ...rest] = rows
148
+ this.push(`${c.accent("minicode")} ${c.muted("›")} ${first ?? ""}`)
149
+ for (const r of rest) this.append(` ${r}`)
150
+ }
151
+
152
+ pushError(message: string): void {
153
+ this.push(c.error(`✗ ${sanitizeAnsiLine(message)}`))
154
+ }
155
+
156
+ pushInfo(lines: string[]): void {
157
+ this.commit()
158
+ for (const l of lines) this.append(l)
159
+ }
160
+
161
+ /** Kosongkan transkrip (`/clear`): viewport kembali ke layar kosong. */
162
+ clear(): void {
163
+ this.lines = []
164
+ this.pending = ""
165
+ this.sections = []
166
+ this.thinkingBuf = ""
167
+ this.thinkingTail = ""
168
+ }
169
+
170
+ /**
171
+ * Ambil isi tool yang dibuffer untuk `/expand` (sekali ambil = habis,
172
+ * seperti membuka arsip). Kosong = tak ada yang disembunyikan.
173
+ */
174
+ takeBufferedSections(): BufferedSection[] {
175
+ const out = this.sections
176
+ this.sections = []
177
+ return out
178
+ }
179
+
180
+ /** Jumlah baris logis (untuk test; bukan API paint). */
181
+ size(): number {
182
+ return this.lines.length + (this.pending ? 1 : 0)
183
+ }
184
+
185
+ /**
186
+ * Baris tampil untuk viewport: wrap ke `width`, ambil `height` baris
187
+ * terakhir dikurangi `scrollBack` (0 = ikut ekor). Selalu kembalikan
188
+ * TEPAT `height` string (padding "" bila kurang) supaya frame penuh.
189
+ */
190
+ view(width: number, height: number, scrollBack: number): string[] {
191
+ const w = Math.max(10, width)
192
+ const wrapped: string[] = []
193
+ for (const logical of this.lines) {
194
+ const clean = logical === "" ? [""] : chunkByWidth(logical, w)
195
+ for (const r of clean) wrapped.push(r)
196
+ }
197
+ if (this.pending) {
198
+ for (const chunk of sanitizeAnsi(this.pending).split("\n")) {
199
+ const rows = chunk === "" ? [""] : chunkByWidth(chunk, w)
200
+ for (const r of rows) wrapped.push(r)
201
+ }
202
+ }
203
+ // Ekor hidup thinking: expanded = sisa baris redup; minimized = satu
204
+ // penanda redup (transkrip tetap bersih, isi di /expand).
205
+ if (this.thinkingTail) {
206
+ for (const chunk of this.thinkingTail.split("\n")) {
207
+ const rows = chunk === "" ? [""] : chunkByWidth(chunk, w)
208
+ for (const r of rows) wrapped.push(c.muted(r))
209
+ }
210
+ } else if (this.thinkingBuf.trim()) {
211
+ wrapped.push(c.muted(t("ts.thinking")))
212
+ }
213
+ const back = Math.max(0, Math.min(scrollBack, Math.max(0, wrapped.length - height)))
214
+ const tail = wrapped.slice(0, wrapped.length - back)
215
+ const shown = tail.slice(Math.max(0, tail.length - height))
216
+ while (shown.length < height) shown.unshift("")
217
+ return shown
218
+ }
219
+
220
+ dispose(): void {
221
+ for (const u of this.unsubs) u()
222
+ this.unsubs = []
223
+ }
224
+
225
+ private commit(): void {
226
+ if (!this.pending) return
227
+ const text = this.pending
228
+ this.pending = ""
229
+ for (const row of sanitizeAnsi(text).split("\n")) this.append(row)
230
+ }
231
+
232
+ private ledger(e: {
233
+ execution: {
234
+ call: { name: string; args?: unknown }
235
+ result: { isError?: boolean; content?: unknown }
236
+ }
237
+ }): void {
238
+ this.commit()
239
+ const rawName = e.execution.call.name ?? "tool"
240
+ const name = sanitizeAnsiLine(String(rawName))
241
+ const args = (e.execution.call.args ?? {}) as Record<string, unknown>
242
+ const r = e.execution.result
243
+ if (r?.isError) {
244
+ const msg =
245
+ truncateToWidth(sanitizeAnsi(String(r.content ?? "")), 200, "…").split("\n")[0] ?? ""
246
+ this.append(c.error(` › ${name}: ${msg}`))
247
+ return
248
+ }
249
+ const target = ledgerTarget(args)
250
+ const label = target ? ` ${truncateToWidth(sanitizeAnsiLine(target), 120, "")}` : ""
251
+ // Glyph ledger memakai arrow tema (› di UTF-8, > di ASCII) — konsisten
252
+ // dengan grammar REPL walau bentuknya disusun manual di sini.
253
+ this.append(c.success(` ›${name ? ` ${name}` : ""}${label}`))
254
+ // Isi tool sukses dibuffer untuk /expand (compact default: isi milik
255
+ // model untuk dibaca, bukan untuk membanjiri viewport).
256
+ if (typeof r?.content === "string" && r.content.trim()) {
257
+ const text = sanitizeAnsi(r.content.trim()).slice(0, EXPAND_MAX_CHARS)
258
+ this.sections.push({ label: `${name}${label}`, text })
259
+ if (this.sections.length > EXPAND_MAX_SECTIONS) {
260
+ this.sections.splice(0, this.sections.length - EXPAND_MAX_SECTIONS)
261
+ }
262
+ }
263
+ }
264
+
265
+ private append(line: string): void {
266
+ this.lines.push(line)
267
+ if (this.lines.length > TRANSCRIPT_CAP) {
268
+ this.lines.splice(0, this.lines.length - TRANSCRIPT_CAP)
269
+ }
270
+ }
271
+ }