minicode-ai 0.28.0 → 0.30.1
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/args.ts +93 -70
- package/cli/bootstrap/presentation-bridge.ts +383 -0
- package/cli/commands/acp.ts +7 -1
- package/cli/commands/doctor.ts +7 -3
- package/cli/commands/exec.ts +6 -16
- package/cli/index.ts +3 -2
- package/cli/setup.ts +13 -380
- package/cli/tui.ts +3 -4
- package/docs/TERMINAL_CONTRACT.md +28 -21
- package/docs/UI_RENDER_PIPELINE.md +3 -3
- package/docs/architecture.md +14 -29
- package/docs/environment.md +1 -1
- package/package.json +1 -1
- package/src/app/session.ts +201 -53
- package/src/app/tool-layer.ts +27 -0
- package/src/lib/atomic-write.ts +31 -59
- package/src/policy/jail.ts +6 -8
- package/src/providers/anthropic.ts +16 -5
- package/src/providers/responses.ts +37 -7
- package/src/providers/sse-bounds.ts +6 -4
- package/src/session/db/runs.ts +397 -0
- package/src/session/db/schema.ts +358 -0
- package/src/session/persistence.ts +64 -955
- package/src/task/db/schema.ts +325 -0
- package/src/task/db/types.ts +269 -0
- package/src/task/store.ts +74 -986
- package/src/ui/assistant/simple.ts +25 -19
- package/src/ui/i18n/en.ts +0 -3
- package/src/ui/i18n/id.ts +0 -3
- package/src/ui/render/logo.ts +15 -18
- package/src/ui/render/markdown-layout.ts +293 -0
- package/src/ui/render/markdown.ts +36 -44
- package/src/ui/render/session-summary.ts +1 -1
- package/src/ui/render/table-grid.ts +16 -7
- package/src/ui/render/width.ts +71 -18
- package/src/ui/render/wrap.ts +39 -22
- package/src/ui/session-state.ts +4 -4
- package/src/ui/tui/app.ts +15 -36
- package/src/ui/tui/transcript.ts +111 -50
- package/vendor/minicore/src/providers/openai-compat.ts +7 -0
|
@@ -18,6 +18,7 @@ import { formatArgsPreview, formatProviderError, formatUsage } from "../render/f
|
|
|
18
18
|
import { highlightCode } from "../render/highlight.ts"
|
|
19
19
|
import { paintLedger, runMarker, statusWord } from "../render/ledger.ts"
|
|
20
20
|
import { decorateMarkdown, type FenceMatch, parseFence, renderInline } from "../render/markdown.ts"
|
|
21
|
+
import { effectiveAssistantMargin, MarkdownFlowStepper } from "../render/markdown-layout.ts"
|
|
21
22
|
import { MarkdownTableStream } from "../render/markdown-table.ts"
|
|
22
23
|
import {
|
|
23
24
|
cleanUntrusted,
|
|
@@ -236,9 +237,9 @@ export function attachSimpleLogger(bus: UiBus, opts: SimpleOptions = {}): () =>
|
|
|
236
237
|
// /copy tak tersentuh (rememberTurn selalu jalan sebelum sink).
|
|
237
238
|
let answerBuf = ""
|
|
238
239
|
let answerTruncated = false
|
|
239
|
-
//
|
|
240
|
-
//
|
|
241
|
-
let
|
|
240
|
+
// State aliran markdown untuk memberi jeda bersih antar-poin, heading,
|
|
241
|
+
// blockquote, tabel, dan code fence (paritas penuh dengan TUI Transcript).
|
|
242
|
+
let answerFlow = new MarkdownFlowStepper()
|
|
242
243
|
let activeProviderAttempt: number | null = null
|
|
243
244
|
let attemptCopyText = ""
|
|
244
245
|
let attemptAnswerBuf = ""
|
|
@@ -247,6 +248,7 @@ export function attachSimpleLogger(bus: UiBus, opts: SimpleOptions = {}): () =>
|
|
|
247
248
|
let attemptReasoningLine = ""
|
|
248
249
|
let attemptThinkState: "off" | "min" | "exp" = "off"
|
|
249
250
|
let attemptFence: FenceMatch | null = null
|
|
251
|
+
let attemptAnswerFlow = answerFlow.clone()
|
|
250
252
|
const ANSWER_BUF_MAX = 1_000_000
|
|
251
253
|
// Jawaban tak pernah disembunyikan di pipa/CI (output mesin harus utuh)
|
|
252
254
|
// dan --verbose selalu menampilkan semua.
|
|
@@ -356,28 +358,29 @@ export function attachSimpleLogger(bus: UiBus, opts: SimpleOptions = {}): () =>
|
|
|
356
358
|
const flushLine = (line: string) => {
|
|
357
359
|
const w = process.stdout.columns || 80
|
|
358
360
|
const f = parseFence(line)
|
|
361
|
+
if (fence !== null) {
|
|
362
|
+
if (f && f.char === fence.char && f.len >= fence.len) {
|
|
363
|
+
fence = null
|
|
364
|
+
answerFlow.onFenceClose()
|
|
365
|
+
} else {
|
|
366
|
+
const margin = " ".repeat(effectiveAssistantMargin(w))
|
|
367
|
+
const content = fence.lang ? highlightCode(line, fence.lang) : line
|
|
368
|
+
emitAnswer(line.trim() === "" ? "\n" : `${margin}${content}\n`)
|
|
369
|
+
}
|
|
370
|
+
return
|
|
371
|
+
}
|
|
359
372
|
if (f) {
|
|
360
|
-
if (
|
|
361
|
-
|
|
362
|
-
// Baris pembuka/penutup fence adalah delimiter — tidak dicetak.
|
|
373
|
+
if (answerFlow.onFenceOpen()) emitAnswer("\n")
|
|
374
|
+
fence = f
|
|
363
375
|
return
|
|
364
376
|
}
|
|
365
|
-
if (
|
|
366
|
-
|
|
367
|
-
// bahasa. Tanpa bahasa, tetap apa adanya (perbaikan V8: fence tanpa
|
|
368
|
-
// bahasa tidak boleh kehilangan *value* karena dianggap italic).
|
|
369
|
-
const content = fence.lang ? highlightCode(line, fence.lang) : line
|
|
370
|
-
emitAnswer(` ${content}\n`)
|
|
371
|
-
answerHasLine = true
|
|
377
|
+
if (line.trim() === "") {
|
|
378
|
+
if (answerFlow.onBlank()) emitAnswer("\n")
|
|
372
379
|
return
|
|
373
380
|
}
|
|
374
|
-
|
|
375
|
-
// paritas dengan decorateMarkdown TUI (spasi vertikal sebelum heading).
|
|
376
|
-
const isHeading = /^#{1,3}\s/.test(line)
|
|
377
|
-
if (isHeading && answerHasLine) emitAnswer("\n")
|
|
381
|
+
if (answerFlow.onLine(line)) emitAnswer("\n")
|
|
378
382
|
emitAnswer(formatWrapped(decorateMarkdown(line), w))
|
|
379
383
|
emitAnswer("\n")
|
|
380
|
-
answerHasLine = answerHasLine || line.trim() !== ""
|
|
381
384
|
}
|
|
382
385
|
const consumePlainText = (text: string): void => {
|
|
383
386
|
const combined = plainLineBuffer + text
|
|
@@ -392,6 +395,7 @@ export function attachSimpleLogger(bus: UiBus, opts: SimpleOptions = {}): () =>
|
|
|
392
395
|
continue
|
|
393
396
|
}
|
|
394
397
|
const width = process.stdout.columns || 80
|
|
398
|
+
if (answerFlow.onTable()) emitAnswer("\n")
|
|
395
399
|
const rendered = process.stdout.isTTY
|
|
396
400
|
? renderMarkdownTable(event.table, width, renderInline)
|
|
397
401
|
: event.raw
|
|
@@ -625,7 +629,7 @@ export function attachSimpleLogger(bus: UiBus, opts: SimpleOptions = {}): () =>
|
|
|
625
629
|
reasoningLine = ""
|
|
626
630
|
answerBuf = ""
|
|
627
631
|
answerTruncated = false
|
|
628
|
-
|
|
632
|
+
answerFlow = new MarkdownFlowStepper()
|
|
629
633
|
activeProviderAttempt = null
|
|
630
634
|
attemptCopyText = ""
|
|
631
635
|
ui.clearCollapsedSections()
|
|
@@ -714,6 +718,7 @@ export function attachSimpleLogger(bus: UiBus, opts: SimpleOptions = {}): () =>
|
|
|
714
718
|
attemptReasoningLine = reasoningLine
|
|
715
719
|
attemptThinkState = thinkState
|
|
716
720
|
attemptFence = fence ? { ...fence } : null
|
|
721
|
+
attemptAnswerFlow = answerFlow.clone()
|
|
717
722
|
textSan = createStreamSanitizer()
|
|
718
723
|
reasoningSan = createStreamSanitizer()
|
|
719
724
|
} else if (id === activeProviderAttempt && data?.phase === "completed") {
|
|
@@ -731,6 +736,7 @@ export function attachSimpleLogger(bus: UiBus, opts: SimpleOptions = {}): () =>
|
|
|
731
736
|
reasoningLine = attemptReasoningLine
|
|
732
737
|
thinkState = attemptThinkState
|
|
733
738
|
fence = attemptFence ? { ...attemptFence } : null
|
|
739
|
+
answerFlow = attemptAnswerFlow.clone()
|
|
734
740
|
plainLineBuffer = ""
|
|
735
741
|
tableStream.reset()
|
|
736
742
|
textSan.flush()
|
package/src/ui/i18n/en.ts
CHANGED
|
@@ -4,9 +4,6 @@
|
|
|
4
4
|
export const en = {
|
|
5
5
|
"app.newBelow": "↓ {n} new lines below · PgDn to bottom",
|
|
6
6
|
"app.abortHint": "abort sent — press Esc again to quit",
|
|
7
|
-
"app.welcomeTitle": "minicode {dot} {model}",
|
|
8
|
-
"app.welcomeCwd": "{cwd}",
|
|
9
|
-
"app.welcomeHints": "Tab mode {dot} Shift+Tab permission {dot} / menu {dot} Ctrl+R history",
|
|
10
7
|
"tui.selectionCopied": "selection copied to clipboard",
|
|
11
8
|
"tui.selectionCopyFailed": "selection could not be copied (OSC 52 unavailable)",
|
|
12
9
|
"tui.selectionTooLarge": "selection too large to copy",
|
package/src/ui/i18n/id.ts
CHANGED
|
@@ -5,9 +5,6 @@ import type { MsgKey } from "./locale.ts"
|
|
|
5
5
|
export const id: Record<MsgKey, string> = {
|
|
6
6
|
"app.newBelow": "↓ {n} baris baru di bawah · PgDn ke ekor",
|
|
7
7
|
"app.abortHint": "abort terkirim — tekan Esc sekali lagi untuk keluar",
|
|
8
|
-
"app.welcomeTitle": "minicode {dot} {model}",
|
|
9
|
-
"app.welcomeCwd": "{cwd}",
|
|
10
|
-
"app.welcomeHints": "Tab mode {dot} Shift+Tab izin {dot} / menu {dot} Ctrl+R riwayat",
|
|
11
8
|
"tui.selectionCopied": "seleksi disalin ke clipboard",
|
|
12
9
|
"tui.selectionCopyFailed": "seleksi tidak bisa disalin (OSC 52 tidak tersedia)",
|
|
13
10
|
"tui.selectionTooLarge": "seleksi terlalu besar untuk disalin",
|
package/src/ui/render/logo.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
// Block logo minicode: mark wordmark pemilik
|
|
2
|
-
// (CorelDRAW) menjadi grid 17 kolom × 9 baris sel.
|
|
1
|
+
// Block logo minicode: mark wordmark pemilik pada grid 14 kolom × 9 baris sel.
|
|
3
2
|
//
|
|
4
3
|
// ASPEK PER-SEL — inti modul ini. Satu karakter terminal TIDAK persegi:
|
|
5
4
|
// tingginya ±2× lebarnya. Menggambar satu sel = satu karakter penuh membuat
|
|
@@ -13,34 +12,32 @@
|
|
|
13
12
|
// atas█ bawah█ → '█' (kedua sel terisi)
|
|
14
13
|
// atas█ bawah∅ → '▀' (hanya sel atas)
|
|
15
14
|
// atas∅ bawah█ → '▄' (hanya sel bawah)
|
|
16
|
-
// Hasil:
|
|
17
|
-
// karena tinggi logis 9 sel — ganjil). Rasio kotak fisik ≈
|
|
18
|
-
//
|
|
15
|
+
// Hasil: 14 kolom × 5 baris karakter (baris terakhir hanya berisi sel ke-9,
|
|
16
|
+
// karena tinggi logis 9 sel — ganjil). Rasio kotak fisik ≈ 14:9, sesuai grid
|
|
17
|
+
// piksel pemilik; tidak lagi "dipipihkan" seperti blok karakter penuh.
|
|
19
18
|
//
|
|
20
19
|
// Fallback: terminal tanpa UTF-8 (MINICODE_ASCII/mini console legacy) memakai
|
|
21
20
|
// '#' 9 baris — aspek setengah-sel tidak tersedia di sana, jadi bentuk penuh
|
|
22
21
|
// dipertahankan agar pola tidak terpotong.
|
|
23
22
|
//
|
|
24
|
-
//
|
|
25
|
-
// 495.18 × 17 kolom, 4456.66 = 495.18 × 9 baris (sel seragam 495.18²).
|
|
26
|
-
// Setiap '#' = satu sel terisi (75 polygon → 75 sel).
|
|
23
|
+
// Grid referensi pemilik: 14 kolom × 9 baris sel seragam (62 sel '#' terisi).
|
|
27
24
|
|
|
28
25
|
import { supportsUtf8 } from "./theme.ts"
|
|
29
26
|
|
|
30
27
|
const ROWS = [
|
|
31
|
-
"
|
|
32
|
-
" ###
|
|
33
|
-
"#####
|
|
34
|
-
"##
|
|
35
|
-
"##
|
|
36
|
-
"##
|
|
37
|
-
"##
|
|
38
|
-
"
|
|
39
|
-
"
|
|
28
|
+
" ### ",
|
|
29
|
+
" ### ## ##",
|
|
30
|
+
"##### ## ##",
|
|
31
|
+
"## ## ## ##",
|
|
32
|
+
"## ## ##",
|
|
33
|
+
"## ## ##",
|
|
34
|
+
"## ## ## ##",
|
|
35
|
+
"## ## #####",
|
|
36
|
+
" ### ### ",
|
|
40
37
|
] as const
|
|
41
38
|
|
|
42
39
|
/** Dimensi logis logo (sel), untuk pemanggil yang butuh geometri/centering. */
|
|
43
|
-
export const LOGO_WIDTH =
|
|
40
|
+
export const LOGO_WIDTH = 14
|
|
44
41
|
export const LOGO_HEIGHT = 9
|
|
45
42
|
|
|
46
43
|
/** Glyph sel terisi (fallback ASCII). */
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// Layout teks & spasi presentasi asisten (dibagi bersama TUI Transcript dan
|
|
2
|
+
// printer linier agar margin kiri, hanging indent multi-tingkat, dan jeda antar
|
|
3
|
+
// poin paragraf selalu konsisten).
|
|
4
|
+
|
|
5
|
+
import { stripSgr } from "./sanitize.ts"
|
|
6
|
+
import { displayWidth } from "./width.ts"
|
|
7
|
+
|
|
8
|
+
export interface FenceMatch {
|
|
9
|
+
char: string
|
|
10
|
+
len: number
|
|
11
|
+
lang: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Parse satu baris fence markdown (``` atau ~~~) → info pembuka, atau null.
|
|
16
|
+
*/
|
|
17
|
+
export function parseFence(line: string): FenceMatch | null {
|
|
18
|
+
const m = /^\s*(```+|~~~+)([A-Za-z0-9_+.-]*)(?:\s+[^\r\n]*)?\s*$/.exec(line)
|
|
19
|
+
if (!m) return null
|
|
20
|
+
return { char: m[1]![0]!, len: m[1]!.length, lang: m[2] ?? "" }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Margin kiri standar area teks asisten agar lega dari tepi kiri terminal. */
|
|
24
|
+
export const ASSISTANT_LEFT_MARGIN = 3
|
|
25
|
+
|
|
26
|
+
/** Margin kiri adaptif: lega (3 kolom) di terminal normal, menyusut di layar sempit. */
|
|
27
|
+
export function effectiveAssistantMargin(width: number): number {
|
|
28
|
+
if (width >= 36) return ASSISTANT_LEFT_MARGIN
|
|
29
|
+
if (width >= 20) return 2
|
|
30
|
+
if (width >= 14) return 1
|
|
31
|
+
return 0
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Lebar isi minimum yang wajib disisakan saat mengitung hanging indent agar
|
|
36
|
+
* list/paragraf bersarang dalam di terminal sempit tak memeras teks menjadi 1–2 huruf.
|
|
37
|
+
*/
|
|
38
|
+
function minReadableContentCols(width: number): number {
|
|
39
|
+
return Math.min(16, Math.max(6, Math.floor(width / 3)))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Hitung indentasi gantung (kolom spasi pada baris lanjutan wrap) untuk baris
|
|
44
|
+
* teks yang SUDAH memuat margin luarnya.
|
|
45
|
+
*
|
|
46
|
+
* Contoh (margin = 3):
|
|
47
|
+
* - `- item panjang` -> continuation 5 spasi (sejajar tepat di bawah huruf i)
|
|
48
|
+
* - `1. item panjang` -> continuation 6 spasi
|
|
49
|
+
* - ` - sub-poin panjang` -> continuation 8 spasi (tak bablas ke level induk/kolom 0)
|
|
50
|
+
* - ` Paragraf berindentasi` -> continuation 6 spasi (tetap di tingkat paragraf)
|
|
51
|
+
* - `> kutipan panjang` -> continuation 5 spasi (tetap di dalam kutipan)
|
|
52
|
+
*/
|
|
53
|
+
export function computeHangingIndent(line: string, width: number): string {
|
|
54
|
+
if (width <= 0) return ""
|
|
55
|
+
const plain = stripSgr(line)
|
|
56
|
+
if (plain.trim() === "") return ""
|
|
57
|
+
|
|
58
|
+
const maxHangCols = Math.max(0, width - minReadableContentCols(width))
|
|
59
|
+
let desiredCols = 0
|
|
60
|
+
|
|
61
|
+
// Heading: tetap sejajar di indentasi awalan barisnya.
|
|
62
|
+
const heading = /^(\s*)#{1,6}\s+/.exec(plain)
|
|
63
|
+
if (heading) {
|
|
64
|
+
desiredCols = displayWidth(heading[1]!)
|
|
65
|
+
} else {
|
|
66
|
+
// Daftar (bullet / bernomor) beserta checkbox opsional `- [x] `:
|
|
67
|
+
// lanjutan teks sejajar di kolom isi, bukan di bawah marker/angka.
|
|
68
|
+
const listItem = /^(\s*)([-*+]|\d+[.)])\s+(\[[ xX]\]\s+)?(.*)$/.exec(plain)
|
|
69
|
+
if (listItem) {
|
|
70
|
+
desiredCols =
|
|
71
|
+
displayWidth(listItem[1]!) +
|
|
72
|
+
displayWidth(listItem[2]!) +
|
|
73
|
+
1 +
|
|
74
|
+
displayWidth(listItem[3] ?? "")
|
|
75
|
+
} else {
|
|
76
|
+
// Kutipan bersarang (`> `, `> > `, ` > `): teks lanjutan sejajar di dalam kutipan.
|
|
77
|
+
const blockquote = /^(\s*(?:>\s*)+)/.exec(plain)
|
|
78
|
+
if (blockquote) {
|
|
79
|
+
desiredCols = displayWidth(blockquote[1]!)
|
|
80
|
+
} else {
|
|
81
|
+
// Paragraf biasa atau paragraf berindentasi multi-tingkat:
|
|
82
|
+
// pertahankan seluruh spasi awal sebagai batas kiri tingkatnya.
|
|
83
|
+
const leading = /^(\s+)/.exec(plain)
|
|
84
|
+
if (leading) desiredCols = displayWidth(leading[1]!)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const clampedCols = Math.min(desiredCols, maxHangCols)
|
|
90
|
+
return clampedCols > 0 ? " ".repeat(clampedCols) : ""
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface AssistantLineLayout {
|
|
94
|
+
firstLead: string
|
|
95
|
+
hangingIndent: string
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Hitung prefix margin baris-1 & hanging indent wrap untuk satu baris logis asisten. */
|
|
99
|
+
export function computeAssistantLineLayout(
|
|
100
|
+
rawLine: string,
|
|
101
|
+
outerMargin: number,
|
|
102
|
+
width: number,
|
|
103
|
+
opts: { inFence?: boolean } = {},
|
|
104
|
+
): AssistantLineLayout {
|
|
105
|
+
if (rawLine.trim() === "" || outerMargin <= 0) {
|
|
106
|
+
const hangingIndent = opts.inFence ? "" : computeHangingIndent(rawLine, width)
|
|
107
|
+
return { firstLead: "", hangingIndent }
|
|
108
|
+
}
|
|
109
|
+
const firstLead = " ".repeat(outerMargin)
|
|
110
|
+
if (opts.inFence) {
|
|
111
|
+
// Di dalam code fence, pertahankan margin area teks + opsional indentasi
|
|
112
|
+
// kode secukupnya agar potongan kode panjang tak menempel kembali ke tepi kiri.
|
|
113
|
+
return { firstLead, hangingIndent: firstLead }
|
|
114
|
+
}
|
|
115
|
+
const hangingIndent = computeHangingIndent(`${firstLead}${rawLine}`, width)
|
|
116
|
+
return { firstLead, hangingIndent }
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export type FlowLineKind = "none" | "heading" | "list" | "blockquote" | "fenceClose" | "prose"
|
|
120
|
+
|
|
121
|
+
export interface MarkdownFlowState {
|
|
122
|
+
prevKind: FlowLineKind
|
|
123
|
+
topListIndent: number | null
|
|
124
|
+
prevWasBlank: boolean
|
|
125
|
+
hasContent: boolean
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function classifyNonFenceLine(line: string): {
|
|
129
|
+
kind: "heading" | "list" | "blockquote" | "prose"
|
|
130
|
+
indentCols: number
|
|
131
|
+
} {
|
|
132
|
+
if (/^#{1,6}\s/.test(line)) return { kind: "heading", indentCols: 0 }
|
|
133
|
+
const listItem = /^(\s*)([-*+]|\d+[.)])\s+/.exec(line)
|
|
134
|
+
if (listItem) return { kind: "list", indentCols: displayWidth(listItem[1]!) }
|
|
135
|
+
if (/^\s*>/.test(line)) return { kind: "blockquote", indentCols: 0 }
|
|
136
|
+
const leading = /^(\s*)/.exec(line)
|
|
137
|
+
return { kind: "prose", indentCols: displayWidth(leading?.[1] ?? "") }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Mesin status transisi jeda vertikal antar-blok markdown.
|
|
142
|
+
*
|
|
143
|
+
* Menambahkan 1 baris kosong (bila belum ada) agar setiap poin utama daftar,
|
|
144
|
+
* paragraf pengantar/penutup, heading, blockquote, dan code fence punya ruang
|
|
145
|
+
* napas yang nyaman dibaca tanpa menumpuk jadi dinding teks atau menghasilkan
|
|
146
|
+
* baris kosong ganda berlebihan.
|
|
147
|
+
*/
|
|
148
|
+
export class MarkdownFlowStepper {
|
|
149
|
+
private state: MarkdownFlowState = {
|
|
150
|
+
prevKind: "none",
|
|
151
|
+
topListIndent: null,
|
|
152
|
+
prevWasBlank: false,
|
|
153
|
+
hasContent: false,
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
get hasEmitted(): boolean {
|
|
157
|
+
return this.state.hasContent
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
clone(): MarkdownFlowStepper {
|
|
161
|
+
const copy = new MarkdownFlowStepper()
|
|
162
|
+
copy.state = { ...this.state }
|
|
163
|
+
return copy
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Catat baris kosong; return true bila boleh dicetak (mencegah blank berulang/awal). */
|
|
167
|
+
onBlank(): boolean {
|
|
168
|
+
if (!this.state.hasContent || this.state.prevWasBlank) return false
|
|
169
|
+
this.state.prevWasBlank = true
|
|
170
|
+
return true
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Transisi ke pembuka code fence; return true bila perlu baris kosong pemisah sebelumnya. */
|
|
174
|
+
onFenceOpen(): boolean {
|
|
175
|
+
const needSpacer = this.state.hasContent && !this.state.prevWasBlank
|
|
176
|
+
this.state.hasContent = true
|
|
177
|
+
this.state.prevWasBlank = false
|
|
178
|
+
this.state.prevKind = "fenceClose"
|
|
179
|
+
this.state.topListIndent = null
|
|
180
|
+
return needSpacer
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Tutup code fence agar baris sesudahnya diberi jeda bersih. */
|
|
184
|
+
onFenceClose(): void {
|
|
185
|
+
this.state.hasContent = true
|
|
186
|
+
this.state.prevWasBlank = false
|
|
187
|
+
this.state.prevKind = "fenceClose"
|
|
188
|
+
this.state.topListIndent = null
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Catat tabel blok terpisah (paritas pemisah terhadap paragraf di sekitarnya). */
|
|
192
|
+
onTable(): boolean {
|
|
193
|
+
const needSpacer = this.state.hasContent && !this.state.prevWasBlank
|
|
194
|
+
this.state.hasContent = true
|
|
195
|
+
this.state.prevWasBlank = false
|
|
196
|
+
this.state.prevKind = "fenceClose"
|
|
197
|
+
this.state.topListIndent = null
|
|
198
|
+
return needSpacer
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Catat satu baris teks markdown non-kosong di luar fence; return true bila perlu jeda sebelumnya. */
|
|
202
|
+
onLine(line: string): boolean {
|
|
203
|
+
const curr = classifyNonFenceLine(line)
|
|
204
|
+
let needSpacer = false
|
|
205
|
+
|
|
206
|
+
if (this.state.hasContent && !this.state.prevWasBlank) {
|
|
207
|
+
if (curr.kind === "heading") {
|
|
208
|
+
needSpacer = true
|
|
209
|
+
} else if (this.state.prevKind === "heading" || this.state.prevKind === "fenceClose") {
|
|
210
|
+
needSpacer = true
|
|
211
|
+
} else if (curr.kind === "blockquote") {
|
|
212
|
+
needSpacer = this.state.prevKind !== "blockquote"
|
|
213
|
+
} else if (this.state.prevKind === "blockquote") {
|
|
214
|
+
needSpacer = true
|
|
215
|
+
} else if (curr.kind === "list") {
|
|
216
|
+
if (this.state.topListIndent === null) {
|
|
217
|
+
// Masuk ke blok daftar dari paragraf/elemen lain → pisahkan dengan 1 jeda.
|
|
218
|
+
needSpacer = true
|
|
219
|
+
} else if (curr.indentCols <= this.state.topListIndent) {
|
|
220
|
+
// Poin utama / level terluar berikutnya dalam daftar (atau keluar dari
|
|
221
|
+
// sub-poin kembali ke poin utama) → pisahkan antar-poin. Sub-item bersarang
|
|
222
|
+
// (`indentCols > topListIndent`) tetap merapat di bawah induknya.
|
|
223
|
+
needSpacer = true
|
|
224
|
+
}
|
|
225
|
+
} else {
|
|
226
|
+
// `curr.kind === "prose"`: bila baru keluar dari daftar ke paragraf level
|
|
227
|
+
// terluar (bukan paragraf lanjutan berindentasi di dalam poin), beri jeda.
|
|
228
|
+
if (this.state.topListIndent !== null && curr.indentCols <= this.state.topListIndent) {
|
|
229
|
+
needSpacer = true
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (curr.kind === "list") {
|
|
235
|
+
this.state.topListIndent =
|
|
236
|
+
this.state.topListIndent === null
|
|
237
|
+
? curr.indentCols
|
|
238
|
+
: Math.min(this.state.topListIndent, curr.indentCols)
|
|
239
|
+
} else if (
|
|
240
|
+
curr.kind === "heading" ||
|
|
241
|
+
this.state.topListIndent === null ||
|
|
242
|
+
curr.indentCols <= this.state.topListIndent
|
|
243
|
+
) {
|
|
244
|
+
this.state.topListIndent = null
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
this.state.hasContent = true
|
|
248
|
+
this.state.prevWasBlank = false
|
|
249
|
+
this.state.prevKind = curr.kind
|
|
250
|
+
return needSpacer
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Rapikan spasi presentasi jawaban model: hapus baris kosong di tepi, batasi
|
|
256
|
+
* jeda berulang di luar fence, dan sisipkan jeda 1 baris antar-poin utama /
|
|
257
|
+
* paragraf / heading / blok kode. Isi code fence tetap literal 100%.
|
|
258
|
+
*/
|
|
259
|
+
export function formatAssistantSpacing(text: string): string {
|
|
260
|
+
const lines = text.replace(/\r\n?/g, "\n").split("\n")
|
|
261
|
+
const out: string[] = []
|
|
262
|
+
const flow = new MarkdownFlowStepper()
|
|
263
|
+
let openFence: { char: string; len: number } | null = null
|
|
264
|
+
|
|
265
|
+
for (const line of lines) {
|
|
266
|
+
const fence = parseFence(line)
|
|
267
|
+
if (openFence) {
|
|
268
|
+
out.push(line)
|
|
269
|
+
if (fence && fence.char === openFence.char && fence.len >= openFence.len) {
|
|
270
|
+
openFence = null
|
|
271
|
+
flow.onFenceClose()
|
|
272
|
+
}
|
|
273
|
+
continue
|
|
274
|
+
}
|
|
275
|
+
if (fence) {
|
|
276
|
+
if (flow.onFenceOpen()) out.push("")
|
|
277
|
+
openFence = fence
|
|
278
|
+
out.push(line)
|
|
279
|
+
continue
|
|
280
|
+
}
|
|
281
|
+
if (!line.trim()) {
|
|
282
|
+
if (flow.onBlank()) out.push("")
|
|
283
|
+
continue
|
|
284
|
+
}
|
|
285
|
+
if (flow.onLine(line)) out.push("")
|
|
286
|
+
out.push(line)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Jika fence belum ditutup, baris kosong di ekor masih bagian dari contoh
|
|
290
|
+
// kode dan harus tetap literal. Hanya whitespace di luar fence yang dirapatkan.
|
|
291
|
+
if (!openFence) while (out.length && out[out.length - 1] === "") out.pop()
|
|
292
|
+
return out.join("\n")
|
|
293
|
+
}
|
|
@@ -3,38 +3,15 @@
|
|
|
3
3
|
// Inline: **bold**, *italic*, `code`, ~~strike~~, [text](url) -> teks accent (no underline).
|
|
4
4
|
|
|
5
5
|
import { highlightCode } from "./highlight.ts"
|
|
6
|
+
import { effectiveAssistantMargin, formatAssistantSpacing, parseFence } from "./markdown-layout.ts"
|
|
6
7
|
import { type MarkdownTable, parseMarkdownBlocks } from "./markdown-table.ts"
|
|
7
8
|
import { sanitizeAnsi } from "./sanitize.ts"
|
|
8
9
|
import { renderMarkdownTable } from "./table-grid.ts"
|
|
9
10
|
import { c, glyphs } from "./theme.ts"
|
|
10
11
|
|
|
12
|
+
export { type FenceMatch, parseFence } from "./markdown-layout.ts"
|
|
11
13
|
export { renderMarkdownTable, renderMarkdownTableLines } from "./table-grid.ts"
|
|
12
14
|
|
|
13
|
-
export interface FenceMatch {
|
|
14
|
-
char: string
|
|
15
|
-
len: number
|
|
16
|
-
lang: string
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Parse satu baris fence markdown (``` atau ~~~) → info pembuka, atau null.
|
|
21
|
-
*
|
|
22
|
-
* Diekspor agar simple.ts (printer linier streaming) memakai parser yang SAMA
|
|
23
|
-
* dengan decorateMarkdown — sebelumnya ia men-toggle `inFence` naif per baris
|
|
24
|
-
* (`line.includes("```")`) yang tidak kenal ~~~ dan tidak cek char/panjang,
|
|
25
|
-
* sehingga fence berbahasa kehilangan highlight dan state desync.
|
|
26
|
-
*/
|
|
27
|
-
export function parseFence(line: string): FenceMatch | null {
|
|
28
|
-
// Info-string setelah bahasa diperbolehkan (````ts title="demo"` atau
|
|
29
|
-
// ` ```js #tag `). Hanya token pertama (non-spasi) yang dibaca sebagai
|
|
30
|
-
// bahasa — sisanya adalah metadata yang tak memengaruhi highlight. Regex
|
|
31
|
-
// lama menolak seluruh baris ber-info-string sehingga kode jatuh ke dekorasi
|
|
32
|
-
// inline (`const x = *a*` jadi `const x = a`).
|
|
33
|
-
const m = /^\s*(```+|~~~+)([A-Za-z0-9_+.-]*)(?:\s+[^\r\n]*)?\s*$/.exec(line)
|
|
34
|
-
if (!m) return null
|
|
35
|
-
return { char: m[1]![0]!, len: m[1]!.length, lang: m[2] ?? "" }
|
|
36
|
-
}
|
|
37
|
-
|
|
38
15
|
// ── Inline markdown -> ANSI ──
|
|
39
16
|
// Urutan replace lama: `code` diganti dulu jadi `c.brightCyan(...)`, lalu
|
|
40
17
|
// regex **bold** masih menemukan `**` DI DALAM hasil cyan → `` `**a**` ``
|
|
@@ -72,7 +49,11 @@ export function decorateMarkdown(text: string): string {
|
|
|
72
49
|
// Teks model tak-terpercaya: sanitasi SEKALI di hulu agar fence-content,
|
|
73
50
|
// renderInline, dan highlight tak pernah melihat escape mentah. Urutan ini
|
|
74
51
|
// juga melindungi placeholder `\u0000` (sanitize membuang null model).
|
|
75
|
-
const
|
|
52
|
+
const sanitized = sanitizeAnsi(text)
|
|
53
|
+
const spaced = sanitized.includes("\n") ? formatAssistantSpacing(sanitized) : sanitized
|
|
54
|
+
const lines = spaced.split("\n")
|
|
55
|
+
const cols = process.stdout.columns || 80
|
|
56
|
+
const margin = " ".repeat(effectiveAssistantMargin(cols))
|
|
76
57
|
const tableStarts = new Map<number, MarkdownTable>()
|
|
77
58
|
for (const block of parseMarkdownBlocks(lines.join("\n"))) {
|
|
78
59
|
if (block.type === "table") tableStarts.set(block.startLine, block.table)
|
|
@@ -87,9 +68,7 @@ export function decorateMarkdown(text: string): string {
|
|
|
87
68
|
const table = tableStarts.get(lineIndex)
|
|
88
69
|
if (table) {
|
|
89
70
|
if (process.stdout.isTTY)
|
|
90
|
-
out.push(
|
|
91
|
-
...renderMarkdownTable(table, process.stdout.columns || 80, renderInline).split("\n"),
|
|
92
|
-
)
|
|
71
|
+
out.push(...renderMarkdownTable(table, cols, renderInline).split("\n"))
|
|
93
72
|
else
|
|
94
73
|
for (let tableLine = lineIndex; tableLine <= table.endLine; tableLine++)
|
|
95
74
|
out.push(lines[tableLine]!)
|
|
@@ -105,7 +84,7 @@ export function decorateMarkdown(text: string): string {
|
|
|
105
84
|
fenceChar = fence.char
|
|
106
85
|
fenceLen = fence.len
|
|
107
86
|
fenceLang = fence.lang
|
|
108
|
-
if (fenceLang) out.push(c.dim(
|
|
87
|
+
if (fenceLang) out.push(`${margin}${c.dim(`${glyphs.dot} ${fenceLang}`)}`)
|
|
109
88
|
} else {
|
|
110
89
|
if (fence.char === fenceChar && fence.len >= fenceLen) {
|
|
111
90
|
inFence = false
|
|
@@ -115,7 +94,7 @@ export function decorateMarkdown(text: string): string {
|
|
|
115
94
|
} else {
|
|
116
95
|
// fence di dalam fence dengan char/len berbeda → anggap konten
|
|
117
96
|
const bar = fenceLang ? c.dim(glyphs.vLine) : glyphs.vLine
|
|
118
|
-
out.push(
|
|
97
|
+
out.push(`${margin}${bar} ${fenceLang ? highlightCode(line, fenceLang) : line}`)
|
|
119
98
|
continue
|
|
120
99
|
}
|
|
121
100
|
}
|
|
@@ -129,32 +108,45 @@ export function decorateMarkdown(text: string): string {
|
|
|
129
108
|
// `npm run build -- --flag=*value*` kehilangan bintangnya karena dianggap
|
|
130
109
|
// italic. Fence tanpa bahasa justru bentuk paling umum untuk perintah shell.
|
|
131
110
|
if (inFence) {
|
|
111
|
+
if (line.trim() === "") {
|
|
112
|
+
out.push("")
|
|
113
|
+
continue
|
|
114
|
+
}
|
|
132
115
|
const bar = fenceLang ? c.dim(glyphs.vLine) : glyphs.vLine
|
|
133
|
-
out.push(
|
|
116
|
+
out.push(`${margin}${bar} ${fenceLang ? highlightCode(line, fenceLang) : line}`)
|
|
134
117
|
continue
|
|
135
118
|
}
|
|
136
119
|
|
|
137
|
-
|
|
138
|
-
|
|
120
|
+
if (line.trim() === "") {
|
|
121
|
+
out.push("")
|
|
122
|
+
continue
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Headings (# .. ######) -> bold dengan margin area teks.
|
|
126
|
+
if (/^#{1,6}\s/.test(line)) {
|
|
139
127
|
if (out.length > 0 && out[out.length - 1] !== "") out.push("")
|
|
140
|
-
|
|
141
|
-
out.push(` ${c.bold(line)}`)
|
|
128
|
+
out.push(`${margin}${c.bold(line)}`)
|
|
142
129
|
continue
|
|
143
130
|
}
|
|
144
131
|
|
|
145
|
-
// Daftar (bullet `-`/`*`/`+` atau bernomor `1.`/`2)`) — indentasi
|
|
146
|
-
//
|
|
147
|
-
//
|
|
132
|
+
// Daftar (bullet `-`/`*`/`+` atau bernomor `1.`/`2)`) — pertahankan indentasi
|
|
133
|
+
// sumber bersarang relatif di atas margin terluar agar sub-item tetap sejajar
|
|
134
|
+
// tepat di bawah teks induknya saat di-wrap.
|
|
148
135
|
const listItem = /^(\s*)([-*+]|\d+[.)])\s+(.*)$/.exec(line)
|
|
149
136
|
if (listItem) {
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
137
|
+
const sourceIndent = listItem[1]!.slice(0, 16)
|
|
138
|
+
out.push(`${margin}${sourceIndent}${listItem[2]} ${renderInline(listItem[3] ?? "")}`)
|
|
139
|
+
continue
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Kutipan (`> `) atau paragraf biasa / berindentasi multi-tingkat.
|
|
143
|
+
const blockquote = /^(\s*(?:>\s*)+)(.*)$/.exec(line)
|
|
144
|
+
if (blockquote) {
|
|
145
|
+
out.push(`${margin}${blockquote[1]}${renderInline(blockquote[2] ?? "")}`)
|
|
153
146
|
continue
|
|
154
147
|
}
|
|
155
148
|
|
|
156
|
-
|
|
157
|
-
out.push(` ${renderInline(line)}`)
|
|
149
|
+
out.push(`${margin}${renderInline(line)}`)
|
|
158
150
|
}
|
|
159
151
|
return out.join("\n")
|
|
160
152
|
}
|
|
@@ -21,7 +21,7 @@ function paintWordmark(): string {
|
|
|
21
21
|
return c.bold(WORDMARK)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
/** Block logo pemilik (9
|
|
24
|
+
/** Block logo pemilik (14×9) dicat dengan aksen brand — jatuh ke plain saat
|
|
25
25
|
* NO_COLOR, dan ke '#' saat terminal tanpa UTF-8 (fallback di logo.ts). */
|
|
26
26
|
function paintLogo(): string[] {
|
|
27
27
|
return renderLogoRows().map((row) => c.accent(row))
|