minicode-ai 0.9.26
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/CHANGELOG.md +935 -0
- package/LICENSE +21 -0
- package/README.md +200 -0
- package/cli/args.ts +195 -0
- package/cli/auto-update.ts +108 -0
- package/cli/commands/auth.ts +166 -0
- package/cli/commands/config.ts +382 -0
- package/cli/commands/doctor.ts +128 -0
- package/cli/commands/exec.ts +179 -0
- package/cli/commands/mcp.ts +35 -0
- package/cli/commands/memory.ts +107 -0
- package/cli/commands/pricing.ts +100 -0
- package/cli/commands/providers.ts +162 -0
- package/cli/commands/sessions.ts +128 -0
- package/cli/commands/skills.ts +61 -0
- package/cli/commands/stats.ts +82 -0
- package/cli/commands.ts +317 -0
- package/cli/index.ts +539 -0
- package/cli/model-manager.ts +112 -0
- package/cli/provider-manager.ts +111 -0
- package/cli/repl.ts +734 -0
- package/cli/router.ts +124 -0
- package/cli/setup.ts +780 -0
- package/cli/wizard.ts +21 -0
- package/docs/ARCHITECTURE.html +1222 -0
- package/docs/README.md +109 -0
- package/docs/architecture.md +44 -0
- package/docs/changelog.md +3 -0
- package/package.json +91 -0
- package/src/agents/pool.ts +43 -0
- package/src/app/mentions.ts +50 -0
- package/src/app/provider-layer.ts +118 -0
- package/src/app/rag-layer.ts +105 -0
- package/src/app/session.ts +154 -0
- package/src/app/tool-layer.ts +56 -0
- package/src/config.ts +372 -0
- package/src/constants.ts +119 -0
- package/src/hooks/run.ts +89 -0
- package/src/lib/atomic-write.ts +88 -0
- package/src/lib/db-path.ts +36 -0
- package/src/lib/git-hardening.ts +91 -0
- package/src/lib/ignore.ts +58 -0
- package/src/lib/keystore.ts +202 -0
- package/src/lib/net.ts +70 -0
- package/src/lib/safe-open.ts +138 -0
- package/src/lib/session-id.ts +23 -0
- package/src/lib/trash.ts +38 -0
- package/src/lib/trusted-exec.ts +81 -0
- package/src/lsp/client.ts +395 -0
- package/src/mcp/client.ts +400 -0
- package/src/mcp/http-transport.ts +344 -0
- package/src/mcp/server.ts +538 -0
- package/src/mcp/transport.ts +150 -0
- package/src/memory/files.ts +159 -0
- package/src/memory/vector.ts +775 -0
- package/src/policy/allowlist.ts +67 -0
- package/src/policy/bash-guard.ts +465 -0
- package/src/policy/compaction.ts +271 -0
- package/src/policy/context.ts +161 -0
- package/src/policy/executor.ts +199 -0
- package/src/policy/jail.ts +127 -0
- package/src/policy/permission.ts +517 -0
- package/src/policy/pricing.ts +336 -0
- package/src/policy/ratelimit.ts +54 -0
- package/src/policy/sandbox-policy.ts +157 -0
- package/src/policy/scrub.ts +138 -0
- package/src/policy/update-check.ts +200 -0
- package/src/policy/usage.ts +292 -0
- package/src/policy/verifier.ts +181 -0
- package/src/providers/anthropic.ts +401 -0
- package/src/providers/auth-store.ts +139 -0
- package/src/providers/build.ts +136 -0
- package/src/providers/detect.ts +126 -0
- package/src/providers/effort.ts +165 -0
- package/src/providers/guards.ts +107 -0
- package/src/providers/oauth.ts +326 -0
- package/src/providers/presets.ts +122 -0
- package/src/providers/provision.ts +298 -0
- package/src/providers/responses.ts +371 -0
- package/src/providers/router.ts +247 -0
- package/src/repo/repomap.ts +470 -0
- package/src/sandbox/docker.ts +129 -0
- package/src/sandbox/os.ts +193 -0
- package/src/session/checkpoint.ts +543 -0
- package/src/session/journal.ts +1347 -0
- package/src/session/persistence.ts +446 -0
- package/src/session/shadow-git.ts +351 -0
- package/src/session/turn-marker.ts +118 -0
- package/src/skills/loader.ts +117 -0
- package/src/telemetry/trace.ts +283 -0
- package/src/tools/ask_user.ts +54 -0
- package/src/tools/bash.ts +400 -0
- package/src/tools/code_run.ts +96 -0
- package/src/tools/delete_file.ts +31 -0
- package/src/tools/edit.ts +204 -0
- package/src/tools/git.ts +301 -0
- package/src/tools/glob.ts +90 -0
- package/src/tools/grep.ts +281 -0
- package/src/tools/hashline.ts +66 -0
- package/src/tools/index.ts +103 -0
- package/src/tools/lsp.ts +346 -0
- package/src/tools/mcp_call.ts +163 -0
- package/src/tools/memory.ts +144 -0
- package/src/tools/move_file.ts +47 -0
- package/src/tools/patch.ts +86 -0
- package/src/tools/read_file.ts +130 -0
- package/src/tools/read_image.ts +80 -0
- package/src/tools/submit_result.ts +49 -0
- package/src/tools/task.ts +346 -0
- package/src/tools/todo.ts +201 -0
- package/src/tools/web_fetch.ts +161 -0
- package/src/tools/web_search.ts +156 -0
- package/src/tools/write_file.ts +42 -0
- package/src/ui/approval/prompt.ts +76 -0
- package/src/ui/assistant/simple.ts +568 -0
- package/src/ui/assistant/turn-status.ts +303 -0
- package/src/ui/contract.ts +55 -0
- package/src/ui/footer.ts +105 -0
- package/src/ui/input/input.ts +805 -0
- package/src/ui/input/prompt-engine.ts +606 -0
- package/src/ui/render/collapse.ts +84 -0
- package/src/ui/render/detail.ts +24 -0
- package/src/ui/render/diff.ts +181 -0
- package/src/ui/render/errors.ts +239 -0
- package/src/ui/render/format.ts +54 -0
- package/src/ui/render/highlight.ts +294 -0
- package/src/ui/render/markdown.ts +120 -0
- package/src/ui/render/money.ts +12 -0
- package/src/ui/render/reasoning.ts +20 -0
- package/src/ui/render/sanitize.ts +80 -0
- package/src/ui/render/table.ts +88 -0
- package/src/ui/render/theme.ts +339 -0
- package/src/ui/render/width.ts +256 -0
- package/src/ui/render/wrap.ts +120 -0
- package/src/ui/runtime/bus-debug.ts +83 -0
- package/src/ui/runtime/chrome.ts +301 -0
- package/src/ui/runtime/spinner.ts +77 -0
- package/src/ui/runtime/statusline.ts +227 -0
- package/src/ui/screens/model-manager.ts +434 -0
- package/src/ui/screens/overlay.ts +61 -0
- package/src/ui/screens/picker.ts +307 -0
- package/src/ui/screens/provider-manager.ts +450 -0
- package/src/ui/screens/wizard.ts +132 -0
- package/vendor/minicore/VENDOR.md +29 -0
- package/vendor/minicore/package.json +21 -0
- package/vendor/minicore/src/core/budget.ts +26 -0
- package/vendor/minicore/src/core/compact.ts +76 -0
- package/vendor/minicore/src/core/errors.ts +60 -0
- package/vendor/minicore/src/core/events.ts +74 -0
- package/vendor/minicore/src/core/executor.ts +139 -0
- package/vendor/minicore/src/core/history.ts +32 -0
- package/vendor/minicore/src/core/index.ts +42 -0
- package/vendor/minicore/src/core/loop.ts +344 -0
- package/vendor/minicore/src/core/permission.ts +24 -0
- package/vendor/minicore/src/core/provider.ts +39 -0
- package/vendor/minicore/src/core/recovery.ts +34 -0
- package/vendor/minicore/src/core/session.ts +327 -0
- package/vendor/minicore/src/core/snapshot.ts +142 -0
- package/vendor/minicore/src/core/tokens.ts +111 -0
- package/vendor/minicore/src/core/tool.ts +118 -0
- package/vendor/minicore/src/core/types.ts +50 -0
- package/vendor/minicore/src/providers/openai-compat.ts +366 -0
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
// Prompt engine - PURE functions untuk logika input interaktif.
|
|
2
|
+
// Sama sekali tidak menyentuh stdin/stdout - hanya data -> data.
|
|
3
|
+
// Dipakai askLine (src/ui/input/input.ts) + unit test.
|
|
4
|
+
|
|
5
|
+
import { displayWidth } from "../render/width.ts"
|
|
6
|
+
|
|
7
|
+
export interface PromptState {
|
|
8
|
+
line: string
|
|
9
|
+
/**
|
|
10
|
+
* Posisi kursor dalam satuan CODE POINT (bukan UTF-16 unit), 0..len.
|
|
11
|
+
* Tanpa ini tidak ada editing di tengah baris: user harus menghapus seluruh
|
|
12
|
+
* sisa prompt untuk memperbaiki satu kata.
|
|
13
|
+
*/
|
|
14
|
+
cursor: number
|
|
15
|
+
sel: number // index seleksi dropdown, -1 = tidak ada
|
|
16
|
+
menuOpen: boolean
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const MAX_VISIBLE = 10
|
|
20
|
+
|
|
21
|
+
// Hasil render satu frame - pure spec, renderer (input.ts) yang menulis ke stdout.
|
|
22
|
+
export interface RenderSpec {
|
|
23
|
+
inputLine: string // prompt + line (baris 1)
|
|
24
|
+
/** Kolom kursor (0-based) relatif awal inputLine — untuk ESC[<n>G. */
|
|
25
|
+
cursorCol: number
|
|
26
|
+
rows: { kind: "header" | "item"; text: string; picked: boolean }[] // baris dropdown
|
|
27
|
+
moreCount: number // jumlah item tersembunyi (0 = tidak ada)
|
|
28
|
+
totalRows: number // jumlah baris dropdown termasuk baris "more"
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function createState(): PromptState {
|
|
32
|
+
return { line: "", cursor: 0, sel: -1, menuOpen: false }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Keypress ter-normalisasi (hasil decode binari stdin).
|
|
36
|
+
export type PromptKey =
|
|
37
|
+
| { type: "char"; ch: string }
|
|
38
|
+
| { type: "backspace" }
|
|
39
|
+
| { type: "delete" } // hapus karakter DI kursor (Del)
|
|
40
|
+
| { type: "left" }
|
|
41
|
+
| { type: "right" }
|
|
42
|
+
| { type: "home" }
|
|
43
|
+
| { type: "end" }
|
|
44
|
+
| { type: "up" }
|
|
45
|
+
| { type: "down" }
|
|
46
|
+
| { type: "tab" }
|
|
47
|
+
| { type: "enter" }
|
|
48
|
+
| { type: "esc" }
|
|
49
|
+
| { type: "ctrl-c" }
|
|
50
|
+
| { type: "ctrl-d" }
|
|
51
|
+
| { type: "ctrl-u" } // clear line
|
|
52
|
+
| { type: "ctrl-w" } // delete previous word
|
|
53
|
+
| { type: "ctrl-o" } // expand detail (TUI)
|
|
54
|
+
| { type: "ctrl-r" } // reverse-i-search history
|
|
55
|
+
| { type: "ctrl-t" } // toggle expand/minimize reasoning di REPL
|
|
56
|
+
| { type: "ctrl-n" } // tambah (model-manager: Ctrl+N = add model)
|
|
57
|
+
| { type: "ctrl-j" } // sisipkan newline (multiline opt-in; Enter=\r tetap submit)
|
|
58
|
+
| { type: "shift-tab" } // cycle mode (REPL linier) — ESC[Z didekode decodeKey
|
|
59
|
+
| { type: "ignore" } // sekuens yang sengaja dibuang (mis. byte mouse)
|
|
60
|
+
|
|
61
|
+
// Terapkan satu keypress -> state baru + render spec + action (submit/cancel).
|
|
62
|
+
export type PromptAction = "none" | "render" | "submit" | "cancel"
|
|
63
|
+
|
|
64
|
+
// ── Helper grapheme ──
|
|
65
|
+
// String JS diindeks per UTF-16 unit; emoji memakai dua. Array.from memecah per
|
|
66
|
+
// code point, tapi grapheme cluster (ZWJ 👨👩👧, flag 🇮🇩, emoji + VS16 ❤️) tetap
|
|
67
|
+
// terbelah — backspace lalu menyisakan setengah. Intl.Segmenter membagi di
|
|
68
|
+
// batas grapheme sungguhan (Node 16+ / Bun tersedia); fallback ke code point
|
|
69
|
+
// di runtime tanpa dukungan.
|
|
70
|
+
let segmenter: Intl.Segmenter | undefined
|
|
71
|
+
export function toGraphemes(s: string): string[] {
|
|
72
|
+
try {
|
|
73
|
+
segmenter ??= new Intl.Segmenter("und", { granularity: "grapheme" })
|
|
74
|
+
return [...segmenter.segment(s)].map((seg) => seg.segment)
|
|
75
|
+
} catch {
|
|
76
|
+
return Array.from(s)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Panjang dalam satuan grapheme (bukan code point / UTF-16 unit). */
|
|
81
|
+
export function pointLength(s: string): number {
|
|
82
|
+
return toGraphemes(s).length
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Tanda diakritik Thai/Lao yang menempel: satu grapheme Thai bisa terdiri
|
|
86
|
+
// dari konsonan + 1-2 tanda (mis. U+0E19 U+0E49 U+0E33 = satu suku kata).
|
|
87
|
+
// Menghapus per grapheme (aturan emoji ZWJ) membuat satu backspace menelan
|
|
88
|
+
// SELURUH suku kata sekaligus — platform menghapus per code point untuk
|
|
89
|
+
// aksara ini (bug Claude #83449). deletePrevUnit memilih satuan hapus yang
|
|
90
|
+
// benar: diakritik terakhir dulu, lalu bertahap.
|
|
91
|
+
// (Range ditulis \u eksplisit — literal combining char pernah rusak jadi U+FFFD
|
|
92
|
+
// oleh pipeline PowerShell tanpa encoding, dijaga test/import-convention.)
|
|
93
|
+
const THAI_DEPENDENT = new RegExp(
|
|
94
|
+
`[${String.fromCharCode(0x0e31)}-${String.fromCharCode(0x0e4e)}${String.fromCharCode(0x0ec8)}-${String.fromCharCode(0x0ecd)}]`,
|
|
95
|
+
)
|
|
96
|
+
export function deletePrevUnit(line: string, cursor: number): { line: string; cursor: number } {
|
|
97
|
+
if (cursor <= 0) return { line, cursor }
|
|
98
|
+
const pts = toGraphemes(line)
|
|
99
|
+
const target = pts[cursor - 1] ?? ""
|
|
100
|
+
if (THAI_DEPENDENT.test(target)) {
|
|
101
|
+
// Potong satu code point terakhir cluster via indeks UTF-16 mentah.
|
|
102
|
+
// Sisa cluster tetap 1 grapheme di posisi sama, jadi kursor BERTAHAN
|
|
103
|
+
// (bukan mundur): backspace berikut menghapus diakritik berikutnya —
|
|
104
|
+
// persis perilaku platform (satu suku kata butuh N kali tekan).
|
|
105
|
+
// clampCursor menangani kasus sisa kosong (cluster tinggal 1 tanda).
|
|
106
|
+
const at = unitIndex(line, cursor)
|
|
107
|
+
const cps = Array.from(target)
|
|
108
|
+
const lastLen = cps[cps.length - 1]?.length ?? 1
|
|
109
|
+
const next = line.slice(0, at - lastLen) + line.slice(at)
|
|
110
|
+
return { line: next, cursor: clampCursor(next, cursor) }
|
|
111
|
+
}
|
|
112
|
+
pts.splice(cursor - 1, 1)
|
|
113
|
+
return { line: pts.join(""), cursor: cursor - 1 }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Konversi indeks grapheme -> indeks UTF-16, untuk slice(). */
|
|
117
|
+
function unitIndex(s: string, point: number): number {
|
|
118
|
+
const pts = toGraphemes(s)
|
|
119
|
+
let units = 0
|
|
120
|
+
for (let i = 0; i < point && i < pts.length; i++) units += pts[i]!.length
|
|
121
|
+
return units
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function clampCursor(line: string, cursor: number): number {
|
|
125
|
+
const max = pointLength(line)
|
|
126
|
+
return cursor < 0 ? 0 : cursor > max ? max : cursor
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Sisipkan `ins` pada posisi kursor; kursor maju sepanjang teks yang disisipkan. */
|
|
130
|
+
function insertAt(line: string, cursor: number, ins: string): { line: string; cursor: number } {
|
|
131
|
+
const at = unitIndex(line, cursor)
|
|
132
|
+
return {
|
|
133
|
+
line: line.slice(0, at) + ins + line.slice(at),
|
|
134
|
+
cursor: cursor + pointLength(ins),
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function applyKey(
|
|
139
|
+
state: PromptState,
|
|
140
|
+
key: PromptKey,
|
|
141
|
+
hints: (line: string) => string[],
|
|
142
|
+
): { state: PromptState; action: PromptAction } {
|
|
143
|
+
const countOf = (line: string): number => hints(line).length
|
|
144
|
+
const pickSelected = (line: string) => {
|
|
145
|
+
const rows = hints(line)
|
|
146
|
+
if (!rows.length) return null
|
|
147
|
+
return state.sel >= 0 && state.sel < rows.length ? rows[state.sel]! : null
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Setiap perubahan baris memakai aturan menu yang sama: menu terbuka bila
|
|
151
|
+
// baris dimulai "/", dan seleksi dijepit ke jumlah hint yang baru.
|
|
152
|
+
const withLine = (line: string, cursor: number): PromptState => {
|
|
153
|
+
const menuOpen = line.startsWith("/")
|
|
154
|
+
return {
|
|
155
|
+
line,
|
|
156
|
+
cursor: clampCursor(line, cursor),
|
|
157
|
+
sel: menuOpen ? Math.min(Math.max(state.sel, -1), countOf(line) - 1) : -1,
|
|
158
|
+
menuOpen,
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
// Melengkapi baris dari dropdown: kursor selalu ke ujung teks hasil.
|
|
162
|
+
const completeTo = (text: string): PromptState => ({
|
|
163
|
+
line: text,
|
|
164
|
+
cursor: pointLength(text),
|
|
165
|
+
sel: -1,
|
|
166
|
+
menuOpen: false,
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
switch (key.type) {
|
|
170
|
+
case "char": {
|
|
171
|
+
// Paste bisa memuat newline/tab/kontrol. Dulu "\n" digepeng jadi spasi
|
|
172
|
+
// agar tidak melebihi tinggi terminal, tapi itu membuat copas code
|
|
173
|
+
// multilines cuma baris pertama (keluhan nyata). Sekarang "\n" dipertahankan
|
|
174
|
+
// sebagai baris baru — renderer `scrollableMultiline` sudah memperhitungkan
|
|
175
|
+
// tinggi (`nInGuess`, `maxVisible - footerReserveRows`) sehingga tidak
|
|
176
|
+
// menabrak footer. Tab jadi spasi; kontrol lain dibuang.
|
|
177
|
+
const ins = key.ch
|
|
178
|
+
.replace(/\r\n|\r/g, "\n")
|
|
179
|
+
.replace(/\t/g, " ")
|
|
180
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: membuang byte kontrol dari paste
|
|
181
|
+
.replace(/[\u0000-\u0008\u000b-\u001f\u007f]/g, "")
|
|
182
|
+
if (!ins) return { state, action: "none" }
|
|
183
|
+
const { line, cursor } = insertAt(state.line, state.cursor, ins)
|
|
184
|
+
return { state: withLine(line, cursor), action: "render" }
|
|
185
|
+
}
|
|
186
|
+
case "ctrl-j": {
|
|
187
|
+
// Multiline opt-in eksplisit — BERBEDA dari paste yang digepeng jadi
|
|
188
|
+
// spasi: keypress sadar user boleh membawa newline mentah. Render dan
|
|
189
|
+
// history menanganinya per baris visual.
|
|
190
|
+
const ins = insertAt(state.line, state.cursor, "\n")
|
|
191
|
+
return { state: withLine(ins.line, ins.cursor), action: "render" }
|
|
192
|
+
}
|
|
193
|
+
case "backspace": {
|
|
194
|
+
if (state.cursor === 0) return { state, action: "none" }
|
|
195
|
+
const { line, cursor } = deletePrevUnit(state.line, state.cursor)
|
|
196
|
+
return { state: withLine(line, cursor), action: "render" }
|
|
197
|
+
}
|
|
198
|
+
case "delete": {
|
|
199
|
+
const pts = toGraphemes(state.line)
|
|
200
|
+
if (state.cursor >= pts.length) return { state, action: "none" }
|
|
201
|
+
pts.splice(state.cursor, 1)
|
|
202
|
+
return { state: withLine(pts.join(""), state.cursor), action: "render" }
|
|
203
|
+
}
|
|
204
|
+
case "left": {
|
|
205
|
+
if (state.cursor === 0) return { state, action: "none" }
|
|
206
|
+
return { state: { ...state, cursor: state.cursor - 1 }, action: "render" }
|
|
207
|
+
}
|
|
208
|
+
case "right": {
|
|
209
|
+
if (state.cursor >= pointLength(state.line)) return { state, action: "none" }
|
|
210
|
+
return { state: { ...state, cursor: state.cursor + 1 }, action: "render" }
|
|
211
|
+
}
|
|
212
|
+
case "home": {
|
|
213
|
+
if (state.cursor === 0) return { state, action: "none" }
|
|
214
|
+
return { state: { ...state, cursor: 0 }, action: "render" }
|
|
215
|
+
}
|
|
216
|
+
case "end": {
|
|
217
|
+
const max = pointLength(state.line)
|
|
218
|
+
if (state.cursor === max) return { state, action: "none" }
|
|
219
|
+
return { state: { ...state, cursor: max }, action: "render" }
|
|
220
|
+
}
|
|
221
|
+
case "up": {
|
|
222
|
+
if (!state.menuOpen) return { state, action: "none" }
|
|
223
|
+
const n = countOf(state.line)
|
|
224
|
+
if (!n) return { state, action: "none" }
|
|
225
|
+
return {
|
|
226
|
+
state: { ...state, sel: state.sel <= 0 ? n - 1 : (((state.sel - 1) % n) + n) % n },
|
|
227
|
+
action: "render",
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
case "down": {
|
|
231
|
+
if (!state.menuOpen) return { state, action: "none" }
|
|
232
|
+
const n = countOf(state.line)
|
|
233
|
+
if (!n) return { state, action: "none" }
|
|
234
|
+
return { state: { ...state, sel: (state.sel + 1) % n }, action: "render" }
|
|
235
|
+
}
|
|
236
|
+
case "tab": {
|
|
237
|
+
// Tab sekarang HANYA putar mode (cycle `auto→ask→plan→allowlist`),
|
|
238
|
+
// ditangani di REPL via `onKey` agar selalu jalan bahkan saat mengetik.
|
|
239
|
+
// Completion dropdown tidak lagi pakai Tab — user pilih via ↑/↓ lalu
|
|
240
|
+
// Enter (lengkapi + kirim). Tanpa ini Tab saat mengetik tidak bisa
|
|
241
|
+
// ganti mode (keluhan nyata) dan menabrak completion.
|
|
242
|
+
return { state, action: "none" }
|
|
243
|
+
}
|
|
244
|
+
case "enter": {
|
|
245
|
+
const pick = pickSelected(state.line)
|
|
246
|
+
const finalLine = pick ?? state.line
|
|
247
|
+
return { state: completeTo(finalLine), action: "submit" }
|
|
248
|
+
}
|
|
249
|
+
case "esc": {
|
|
250
|
+
if (!state.menuOpen) return { state, action: "none" }
|
|
251
|
+
return { state: { ...state, sel: -1, menuOpen: false }, action: "render" }
|
|
252
|
+
}
|
|
253
|
+
case "ctrl-c":
|
|
254
|
+
case "ctrl-d":
|
|
255
|
+
return { state, action: "cancel" }
|
|
256
|
+
case "ctrl-o": // toggle compact — ditangani REPL lewat onKey askLine
|
|
257
|
+
case "ctrl-r": // reverse-i-search — ditangani askLine (input.ts) sebelum applyKey
|
|
258
|
+
case "ctrl-t": // toggle expand/minimize reasoning di REPL (onKey)
|
|
259
|
+
case "ctrl-n": // tambah model — ditangani view yang membutuhkan (model-manager); di prompt teks diabaikan
|
|
260
|
+
case "shift-tab": // cycle mode — ditangani REPL lewat onKey askLine
|
|
261
|
+
case "ignore": // byte mouse dsb: dibuang, tidak boleh jadi teks
|
|
262
|
+
return { state, action: "none" }
|
|
263
|
+
case "ctrl-u": {
|
|
264
|
+
if (!state.line.length) return { state, action: "none" }
|
|
265
|
+
return { state: createState(), action: "render" }
|
|
266
|
+
}
|
|
267
|
+
case "ctrl-w": {
|
|
268
|
+
if (state.cursor === 0) return { state, action: "none" }
|
|
269
|
+
const at = unitIndex(state.line, state.cursor)
|
|
270
|
+
const before = state.line.slice(0, at)
|
|
271
|
+
const trimmed = before.replace(/\S+\s*$/, "")
|
|
272
|
+
const line = trimmed + state.line.slice(at)
|
|
273
|
+
return { state: withLine(line, pointLength(trimmed)), action: "render" }
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Pure render - spec yang digambar renderer.
|
|
279
|
+
// `groupOf` opsional: menandai item sebagai command/skill -> header grup dinamis.
|
|
280
|
+
export function buildRenderSpec(
|
|
281
|
+
state: PromptState,
|
|
282
|
+
prompt: string,
|
|
283
|
+
hints: string[],
|
|
284
|
+
groupOf?: (text: string) => string,
|
|
285
|
+
maxVisible = MAX_VISIBLE,
|
|
286
|
+
): RenderSpec {
|
|
287
|
+
// Dropdown tidak boleh melebihi tinggi terminal sungguhan. MAX_VISIBLE=10
|
|
288
|
+
// konstan membuat overlay 11 baris di terminal 8 baris (dulu membungkus).
|
|
289
|
+
const limit = Math.max(1, maxVisible)
|
|
290
|
+
const inputLine = `${prompt}${state.line}`
|
|
291
|
+
const visible = hints.slice(0, limit)
|
|
292
|
+
const moreCount = Math.max(0, hints.length - limit)
|
|
293
|
+
const rows: RenderSpec["rows"] = []
|
|
294
|
+
let lastGroup: string | undefined
|
|
295
|
+
for (const text of visible) {
|
|
296
|
+
const group = groupOf?.(text)
|
|
297
|
+
if (group !== undefined && group !== lastGroup) {
|
|
298
|
+
lastGroup = group
|
|
299
|
+
rows.push({ kind: "header", text: group.toUpperCase(), picked: false })
|
|
300
|
+
}
|
|
301
|
+
rows.push({ kind: "item", text, picked: text === visible[state.sel] })
|
|
302
|
+
}
|
|
303
|
+
// Kolom kursor diukur dalam KOLOM terminal: sekuens ANSI pada prompt tidak
|
|
304
|
+
// menempati kolom, dan CJK/emoji menempati dua. Menghitung panjang string
|
|
305
|
+
// mentah membuat kursor terminal salah posisi begitu prompt diwarnai.
|
|
306
|
+
const cursorPoints = toGraphemes(state.line)
|
|
307
|
+
.slice(0, clampCursor(state.line, state.cursor))
|
|
308
|
+
.join("")
|
|
309
|
+
return {
|
|
310
|
+
inputLine,
|
|
311
|
+
cursorCol: displayWidth(prompt) + displayWidth(cursorPoints),
|
|
312
|
+
rows,
|
|
313
|
+
moreCount,
|
|
314
|
+
totalRows: rows.length + (moreCount > 0 ? 1 : 0),
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// ── Binari dari chunk stdin -> keystroke stream ──
|
|
319
|
+
// Rust-style manual parsing: ESC [ A/B/C/D = arrows, ESC = esc, dsb.
|
|
320
|
+
export function decodeKeys(buf: Uint8Array): DecodedKey[] {
|
|
321
|
+
const out: DecodedKey[] = []
|
|
322
|
+
const s = new TextDecoder("utf-8").decode(buf)
|
|
323
|
+
let i = 0
|
|
324
|
+
while (i < s.length) {
|
|
325
|
+
const d = decodeKey(s, i)
|
|
326
|
+
d && out.push(d)
|
|
327
|
+
i += d?.width ?? 1
|
|
328
|
+
}
|
|
329
|
+
return out
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export interface DecodedKey {
|
|
333
|
+
key: PromptKey
|
|
334
|
+
width: number
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// ── Streaming decoder ──
|
|
338
|
+
// `decodeKeys` di atas bekerja pada SATU chunk utuh. Data stdin datang per
|
|
339
|
+
// chunk (Bun/Node tidak menjamin batas UTF-8, bracketed paste, atau byte mouse
|
|
340
|
+
// jatuh di satu event). Decoder di bawah memakai buffer byte agar emoji yang
|
|
341
|
+
// terbelah 2+2, paste `ESC[200~…` yang terbelah, dan `ESC[M`/`ESC[<…M` yang
|
|
342
|
+
// terpotong tidak bocor jadi karakter pengganti/teks (dulu: emoji rusak, paste
|
|
343
|
+
// jadi teks "200~", mouse jadi "00").
|
|
344
|
+
export interface DecoderState {
|
|
345
|
+
/** Byte yang belum lengkap jadi satu key (tail dari chunk sebelumnya). */
|
|
346
|
+
pending: number[]
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export function createDecoderState(): DecoderState {
|
|
350
|
+
return { pending: [] }
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function isCsiParam(b: number): boolean {
|
|
354
|
+
return b >= 0x30 && b <= 0x3f
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function isCsiFinal(b: number): boolean {
|
|
358
|
+
return b >= 0x40 && b <= 0x7e
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function utf8CharLen(b: number): number {
|
|
362
|
+
if (b < 0x80) return 1
|
|
363
|
+
if ((b & 0xe0) === 0xc0) return 2
|
|
364
|
+
if ((b & 0xf0) === 0xe0) return 3
|
|
365
|
+
if ((b & 0xf8) === 0xf0) return 4
|
|
366
|
+
return 1
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function decodeUtf8(bytes: number[]): string {
|
|
370
|
+
return new TextDecoder().decode(Uint8Array.from(bytes))
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Pemetaan byte ASCII/control → key; null bila printable biasa. */
|
|
374
|
+
function asciiKey(b: number): DecodedKey | null {
|
|
375
|
+
switch (b) {
|
|
376
|
+
case 0x01:
|
|
377
|
+
return { key: { type: "home" }, width: 0 }
|
|
378
|
+
case 0x05:
|
|
379
|
+
return { key: { type: "end" }, width: 0 }
|
|
380
|
+
case 0x0f:
|
|
381
|
+
return { key: { type: "ctrl-o" }, width: 0 }
|
|
382
|
+
case 0x12:
|
|
383
|
+
return { key: { type: "ctrl-r" }, width: 0 }
|
|
384
|
+
case 0x14:
|
|
385
|
+
return { key: { type: "ctrl-t" }, width: 0 }
|
|
386
|
+
case 0x0e:
|
|
387
|
+
return { key: { type: "ctrl-n" }, width: 0 }
|
|
388
|
+
case 0x7f:
|
|
389
|
+
case 0x08:
|
|
390
|
+
return { key: { type: "backspace" }, width: 0 }
|
|
391
|
+
// LF (Ctrl+J) BUKAN submit: ia menyisipkan newline (multiline opt-in).
|
|
392
|
+
// Hanya CR (tombol Enter) yang submit. Paste bracketed tak tersentuh
|
|
393
|
+
// (segelintir chunk → char) sehingga perilaku paste lama tidak berubah.
|
|
394
|
+
case 0x0a:
|
|
395
|
+
return { key: { type: "ctrl-j" }, width: 0 }
|
|
396
|
+
case 0x0d:
|
|
397
|
+
return { key: { type: "enter" }, width: 0 }
|
|
398
|
+
case 0x09:
|
|
399
|
+
return { key: { type: "tab" }, width: 0 }
|
|
400
|
+
case 0x03:
|
|
401
|
+
return { key: { type: "ctrl-c" }, width: 0 }
|
|
402
|
+
case 0x04:
|
|
403
|
+
return { key: { type: "ctrl-d" }, width: 0 }
|
|
404
|
+
case 0x15:
|
|
405
|
+
return { key: { type: "ctrl-u" }, width: 0 }
|
|
406
|
+
case 0x17:
|
|
407
|
+
return { key: { type: "ctrl-w" }, width: 0 }
|
|
408
|
+
default:
|
|
409
|
+
if (b < 0x20) return { key: { type: "ignore" }, width: 0 }
|
|
410
|
+
return null
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Dekode chunk stdin streaming: konsumsi key yang LENGKAP, sisanya disimpan
|
|
416
|
+
* di `state.pending` untuk chunk berikutnya. Key dikembalikan berurutan.
|
|
417
|
+
*/
|
|
418
|
+
export function decodeKeysStream(chunk: Uint8Array, state: DecoderState): DecodedKey[] {
|
|
419
|
+
const buf = state.pending.concat([...chunk])
|
|
420
|
+
const out: DecodedKey[] = []
|
|
421
|
+
let i = 0
|
|
422
|
+
while (i < buf.length) {
|
|
423
|
+
const b = buf[i]!
|
|
424
|
+
if (b === 0x1b) {
|
|
425
|
+
const n1 = buf[i + 1]
|
|
426
|
+
// Bracketed paste start: ESC[200~ … ESC[201~ — tahan sampai penutup.
|
|
427
|
+
if (
|
|
428
|
+
n1 === 0x5b &&
|
|
429
|
+
buf[i + 2] === 0x32 &&
|
|
430
|
+
buf[i + 3] === 0x30 &&
|
|
431
|
+
buf[i + 4] === 0x30 &&
|
|
432
|
+
buf[i + 5] === 0x7e
|
|
433
|
+
) {
|
|
434
|
+
let term = -1
|
|
435
|
+
for (let j = i + 6; j + 5 < buf.length; j++) {
|
|
436
|
+
if (
|
|
437
|
+
buf[j] === 0x1b &&
|
|
438
|
+
buf[j + 1] === 0x5b &&
|
|
439
|
+
buf[j + 2] === 0x32 &&
|
|
440
|
+
buf[j + 3] === 0x30 &&
|
|
441
|
+
buf[j + 4] === 0x31 &&
|
|
442
|
+
buf[j + 5] === 0x7e
|
|
443
|
+
) {
|
|
444
|
+
term = j
|
|
445
|
+
break
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
if (term === -1) break // penutup belum tiba — tahan
|
|
449
|
+
out.push({ key: { type: "char", ch: decodeUtf8(buf.slice(i + 6, term)) }, width: 0 })
|
|
450
|
+
i = term + 6
|
|
451
|
+
continue
|
|
452
|
+
}
|
|
453
|
+
// Mouse X10: ESC[M + 3 byte koordinat MENTAH (bukan UTF-8).
|
|
454
|
+
if (n1 === 0x5b && buf[i + 2] === 0x4d) {
|
|
455
|
+
if (i + 6 > buf.length) break // byte koordinat terpotong — tahan
|
|
456
|
+
out.push({ key: { type: "ignore" }, width: 0 })
|
|
457
|
+
i += 6
|
|
458
|
+
continue
|
|
459
|
+
}
|
|
460
|
+
// Mouse SGR: ESC[< … M|m
|
|
461
|
+
if (n1 === 0x5b && buf[i + 2] === 0x3c) {
|
|
462
|
+
let j = i + 3
|
|
463
|
+
while (j < buf.length && buf[j] !== 0x4d && buf[j] !== 0x6d) j++
|
|
464
|
+
if (j >= buf.length) break // terminator belum tiba — tahan
|
|
465
|
+
out.push({ key: { type: "ignore" }, width: 0 })
|
|
466
|
+
i = j + 1
|
|
467
|
+
continue
|
|
468
|
+
}
|
|
469
|
+
// CSI (ESC[) / SS3 (ESC O): sekuens ASCII sampai byte final.
|
|
470
|
+
if (n1 === 0x5b || n1 === 0x4f) {
|
|
471
|
+
let j = i + 2
|
|
472
|
+
while (
|
|
473
|
+
j < buf.length &&
|
|
474
|
+
!isCsiFinal(buf[j]!) &&
|
|
475
|
+
(isCsiParam(buf[j]!) || (buf[j]! >= 0x20 && buf[j]! <= 0x2f))
|
|
476
|
+
) {
|
|
477
|
+
j++
|
|
478
|
+
}
|
|
479
|
+
if (j < buf.length && isCsiFinal(buf[j]!)) {
|
|
480
|
+
const seq = String.fromCharCode(...buf.slice(i, j + 1))
|
|
481
|
+
const d = decodeKey(seq, 0)
|
|
482
|
+
out.push(d ?? { key: { type: "ignore" }, width: 0 })
|
|
483
|
+
i = j + 1
|
|
484
|
+
continue
|
|
485
|
+
}
|
|
486
|
+
if (j === i + 2) {
|
|
487
|
+
// Hanya "ESC["/"ESC O" tanpa apa pun → ESC biasa, lengkap.
|
|
488
|
+
out.push({ key: { type: "esc" }, width: 0 })
|
|
489
|
+
i += 2
|
|
490
|
+
continue
|
|
491
|
+
}
|
|
492
|
+
break // ada parameter tapi belum ada byte final — tahan
|
|
493
|
+
}
|
|
494
|
+
// ESC + byte lain: ESC tunggal (lengkap, tidak menunggu apa pun).
|
|
495
|
+
out.push({ key: { type: "esc" }, width: 0 })
|
|
496
|
+
i += 1
|
|
497
|
+
continue
|
|
498
|
+
}
|
|
499
|
+
if (b < 0x80) {
|
|
500
|
+
const d = asciiKey(b)
|
|
501
|
+
out.push(d ?? { key: { type: "char", ch: String.fromCharCode(b) }, width: 0 })
|
|
502
|
+
i += 1
|
|
503
|
+
continue
|
|
504
|
+
}
|
|
505
|
+
// Multi-byte UTF-8 — pastikan seluruh sekuens sudah tiba.
|
|
506
|
+
const n = utf8CharLen(b)
|
|
507
|
+
if (i + n > buf.length) break
|
|
508
|
+
const bytes = buf.slice(i, i + n)
|
|
509
|
+
const contOk = bytes.slice(1).every((x) => (x & 0xc0) === 0x80)
|
|
510
|
+
if (!contOk) {
|
|
511
|
+
out.push({ key: { type: "char", ch: "\ufffd" }, width: 0 })
|
|
512
|
+
i += 1
|
|
513
|
+
continue
|
|
514
|
+
}
|
|
515
|
+
out.push({ key: { type: "char", ch: decodeUtf8(bytes) }, width: 0 })
|
|
516
|
+
i += n
|
|
517
|
+
}
|
|
518
|
+
state.pending = buf.slice(i)
|
|
519
|
+
return out
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
export function decodeKey(s: string, i: number): DecodedKey | null {
|
|
523
|
+
const c = s[i]!
|
|
524
|
+
const code = c.charCodeAt(0)
|
|
525
|
+
if (code === 0x1b) {
|
|
526
|
+
// Bracketed paste: ESC[200~ … ESC[201~ — emit satu char "paste" per segmen
|
|
527
|
+
if (
|
|
528
|
+
s[i + 1] === "[" &&
|
|
529
|
+
s[i + 2] === "2" &&
|
|
530
|
+
s[i + 3] === "0" &&
|
|
531
|
+
s[i + 4] === "0" &&
|
|
532
|
+
s[i + 5] === "~"
|
|
533
|
+
) {
|
|
534
|
+
const endIdx = s.indexOf("\x1b[201~", i + 6)
|
|
535
|
+
if (endIdx !== -1) {
|
|
536
|
+
return { key: { type: "char", ch: s.slice(i + 6, endIdx) }, width: endIdx + 6 - i }
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
// Laporan mouse. X10: ESC [ M + 3 byte mentah (yang BUKAN huruf final, jadi
|
|
540
|
+
// scanCsi tidak bisa mengukurnya). SGR: ESC [ < … M/m. Keduanya dibuang —
|
|
541
|
+
// tanpa ini byte koordinat masuk sebagai teks ("teks" jadi "teks 00").
|
|
542
|
+
if (s[i + 1] === "[" && s[i + 2] === "M") return { key: { type: "ignore" }, width: 6 }
|
|
543
|
+
if (s[i + 1] === "[" && s[i + 2] === "<") {
|
|
544
|
+
let j = i + 3
|
|
545
|
+
while (j < s.length && s[j] !== "M" && s[j] !== "m") j++
|
|
546
|
+
return { key: { type: "ignore" }, width: j - i + 1 }
|
|
547
|
+
}
|
|
548
|
+
if (s[i + 1] === "[" || s[i + 1] === "O") {
|
|
549
|
+
const kind = s[i + 2]
|
|
550
|
+
if (kind === "A") return { key: { type: "up" }, width: 3 }
|
|
551
|
+
if (kind === "B") return { key: { type: "down" }, width: 3 }
|
|
552
|
+
if (kind === "C") return { key: { type: "right" }, width: 3 }
|
|
553
|
+
if (kind === "D") return { key: { type: "left" }, width: 3 }
|
|
554
|
+
if (kind === "H") return { key: { type: "home" }, width: 3 }
|
|
555
|
+
if (kind === "F") return { key: { type: "end" }, width: 3 }
|
|
556
|
+
// Varian VT: ESC[1~ Home, ESC[4~ End, ESC[3~ Delete, ESC[7~/[8~ Home/End
|
|
557
|
+
if (kind === "1" && s[i + 3] === "~") return { key: { type: "home" }, width: 4 }
|
|
558
|
+
if (kind === "7" && s[i + 3] === "~") return { key: { type: "home" }, width: 4 }
|
|
559
|
+
if (kind === "4" && s[i + 3] === "~") return { key: { type: "end" }, width: 4 }
|
|
560
|
+
if (kind === "8" && s[i + 3] === "~") return { key: { type: "end" }, width: 4 }
|
|
561
|
+
if (kind === "3" && s[i + 3] === "~") return { key: { type: "delete" }, width: 4 }
|
|
562
|
+
// Shift+Tab (backtab) ESC [ Z — dipakai REPL linier untuk cycle mode.
|
|
563
|
+
// Tanpa cabang eksplisit ini ia jatuh ke catch-all "esc" di bawah,
|
|
564
|
+
// sehingga tipe "shift-tab" tidak pernah dihasilkan decodeKeys.
|
|
565
|
+
if (kind === "Z") return { key: { type: "shift-tab" }, width: 3 }
|
|
566
|
+
// ESC [ … lainnya -> konsumsi saja
|
|
567
|
+
return { key: { type: "esc" }, width: Math.max(3, scanCsi(s, i)) }
|
|
568
|
+
}
|
|
569
|
+
return { key: { type: "esc" }, width: 1 }
|
|
570
|
+
}
|
|
571
|
+
if (code === 0x01) return { key: { type: "home" }, width: 1 } // Ctrl+A
|
|
572
|
+
if (code === 0x05) return { key: { type: "end" }, width: 1 } // Ctrl+E
|
|
573
|
+
// Ctrl+O (15) & Ctrl+R (18) sebagai key types sendiri
|
|
574
|
+
if (code === 0x0f) return { key: { type: "ctrl-o" }, width: 1 }
|
|
575
|
+
if (code === 0x12) return { key: { type: "ctrl-r" }, width: 1 }
|
|
576
|
+
if (code === 0x14) return { key: { type: "ctrl-t" }, width: 1 }
|
|
577
|
+
if (code === 0x0e) return { key: { type: "ctrl-n" }, width: 1 } // Ctrl+N
|
|
578
|
+
if (code === 0x7f || code === 0x08) return { key: { type: "backspace" }, width: 1 }
|
|
579
|
+
// Sinkron dengan asciiKey di atas: LF = newline, CR = submit.
|
|
580
|
+
if (c === "\n") return { key: { type: "ctrl-j" }, width: 1 }
|
|
581
|
+
if (c === "\r") return { key: { type: "enter" }, width: 1 }
|
|
582
|
+
if (c === "\t") return { key: { type: "tab" }, width: 1 }
|
|
583
|
+
if (code === 0x03) return { key: { type: "ctrl-c" }, width: 1 }
|
|
584
|
+
if (code === 0x04) return { key: { type: "ctrl-d" }, width: 1 }
|
|
585
|
+
if (code === 0x15) return { key: { type: "ctrl-u" }, width: 1 }
|
|
586
|
+
if (code === 0x17) return { key: { type: "ctrl-w" }, width: 1 }
|
|
587
|
+
// Kontrol C0 lain yang tidak punya arti di sini (Ctrl+L, Ctrl+K, Ctrl+Z,
|
|
588
|
+
// dst.) DIBUANG, bukan diteruskan sebagai karakter.
|
|
589
|
+
//
|
|
590
|
+
// Sebelumnya semuanya jatuh ke cabang "char" di bawah dan masuk ke baris
|
|
591
|
+
// input sebagai byte tak tampak — Ctrl+L+Ctrl+K+Ctrl+T pada "teks"
|
|
592
|
+
// mengirimkan "teks\f\u000b\u0014" ke model. Tidak terlihat di layar, tapi
|
|
593
|
+
// ikut terkirim dan bisa membingungkan model atau merusak render.
|
|
594
|
+
if (code < 0x20 || code === 0x7f) return { key: { type: "ignore" }, width: 1 }
|
|
595
|
+
// Multi-byte: s sudah decoded UTF-16 - ukur unit per code point.
|
|
596
|
+
const width = code >= 0xd800 && code <= 0xdbff ? 2 : 1
|
|
597
|
+
const ch = s.slice(i, i + width)
|
|
598
|
+
return { key: { type: "char", ch }, width }
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
function scanCsi(s: string, i: number): number {
|
|
602
|
+
// konsumsi sampai huruf final (mis. ESC [ 1 ; 5 A)
|
|
603
|
+
let j = i + 2
|
|
604
|
+
while (j < s.length && !/[a-zA-Z]/.test(s[j]!)) j++
|
|
605
|
+
return j - i + 1
|
|
606
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Satu sumber state "section collapse" untuk output besar saat turn
|
|
2
|
+
// (thinking, bash, edit/apply_patch, content tool). Pola detail.ts/reasoning.ts:
|
|
3
|
+
// getter dibaca per-event dari env, jadi toggle runtime (tombol + / - saat
|
|
4
|
+
// busy, /expand /minimize di prompt) langsung berlaku tanpa re-import.
|
|
5
|
+
//
|
|
6
|
+
// Semantik tombol: `+` = expand (lihat isi), `-` = minimize (satu baris).
|
|
7
|
+
// Minimized = isi TIDAK dicetak, hanya ` + label` — isi di-buffer untuk
|
|
8
|
+
// /expand. Thinking memakai reasoning.visible sebagai invers dari minimize
|
|
9
|
+
// (expanded = visible), jadi /thinking/Ctrl+T lama tetap satu arti.
|
|
10
|
+
import { reasoning, setReasoningVisible } from "./reasoning.ts"
|
|
11
|
+
|
|
12
|
+
export type CollapseSection = "thinking" | "tool" | "answer"
|
|
13
|
+
|
|
14
|
+
let activeSection: CollapseSection | null = null
|
|
15
|
+
|
|
16
|
+
// Buffer konten yang dikecilkan pada turn terakhir — dibuka via /expand.
|
|
17
|
+
export interface BufferedSection {
|
|
18
|
+
label: string
|
|
19
|
+
text: string
|
|
20
|
+
/** Stream asal konten (kontrak Unix dipertahankan saat /expand mencetak). */
|
|
21
|
+
stream: "stdout" | "stderr"
|
|
22
|
+
}
|
|
23
|
+
const MAX_SECTION_CHARS = 200_000
|
|
24
|
+
const MAX_BUFFER_TOTAL = 500_000
|
|
25
|
+
const bufferedSections: BufferedSection[] = []
|
|
26
|
+
|
|
27
|
+
export const collapse = {
|
|
28
|
+
/** Section yang sedang aktif — keputusan tombol + / - saat turn berjalan. */
|
|
29
|
+
get activeSection(): CollapseSection | null {
|
|
30
|
+
return activeSection
|
|
31
|
+
},
|
|
32
|
+
setActiveSection(k: CollapseSection | null): void {
|
|
33
|
+
activeSection = k
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Section sedang dikecilkan? Thinking = invers reasoning.visible. */
|
|
38
|
+
export function sectionMinimized(section: CollapseSection): boolean {
|
|
39
|
+
if (section === "thinking") return !reasoning.visible
|
|
40
|
+
if (section === "answer") return process.env.MINICODE_MINIMIZE_ANSWER === "1"
|
|
41
|
+
return process.env.MINICODE_MINIMIZE_TOOL === "1"
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Set eksplisit (nilai minimize) atau toggle bila `next` tidak diberikan.
|
|
45
|
+
* Return nilai minimize terbaru. */
|
|
46
|
+
export function setSectionMinimized(section: CollapseSection, next?: boolean): boolean {
|
|
47
|
+
if (section === "thinking") {
|
|
48
|
+
const vis = setReasoningVisible(next === undefined ? undefined : !next)
|
|
49
|
+
return !vis
|
|
50
|
+
}
|
|
51
|
+
const key = section === "answer" ? "MINICODE_MINIMIZE_ANSWER" : "MINICODE_MINIMIZE_TOOL"
|
|
52
|
+
const cur = process.env[key] === "1"
|
|
53
|
+
const nxt = next ?? !cur
|
|
54
|
+
process.env[key] = nxt ? "1" : "0"
|
|
55
|
+
return nxt
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Simpan isi section yang dikecilkan (cap per-entry + total). */
|
|
59
|
+
export function bufferSection(
|
|
60
|
+
label: string,
|
|
61
|
+
text: string,
|
|
62
|
+
stream: BufferedSection["stream"] = "stderr",
|
|
63
|
+
): void {
|
|
64
|
+
if (!text) return
|
|
65
|
+
bufferedSections.push({ label, text: text.slice(0, MAX_SECTION_CHARS), stream })
|
|
66
|
+
// Buang yang tertua sampai total dalam budget — terbaru selalu dipertahankan.
|
|
67
|
+
let total = 0
|
|
68
|
+
let keepFrom = bufferedSections.length
|
|
69
|
+
for (let i = bufferedSections.length - 1; i >= 0; i--) {
|
|
70
|
+
total += bufferedSections[i]!.text.length
|
|
71
|
+
if (total > MAX_BUFFER_TOTAL) break
|
|
72
|
+
keepFrom = i
|
|
73
|
+
}
|
|
74
|
+
if (keepFrom > 0) bufferedSections.splice(0, keepFrom)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function resetBufferedSections(): void {
|
|
78
|
+
bufferedSections.length = 0
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Konten section yang dikecilkan pada turn terakhir (untuk /expand). */
|
|
82
|
+
export function getBufferedSections(): BufferedSection[] {
|
|
83
|
+
return bufferedSections
|
|
84
|
+
}
|