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,568 @@
|
|
|
1
|
+
// Printer linier — satu-satunya renderer output agen (one-shot & REPL linier).
|
|
2
|
+
// Output append-only ke scrollback terminal, tanpa alternate screen. Tool call
|
|
3
|
+
// inline dan EXPANDED by default (transparansi shell); mode compact via
|
|
4
|
+
// MINICODE_COMPACT=1 atau setCompactMode (/compact).
|
|
5
|
+
import { Buffer } from "node:buffer"
|
|
6
|
+
import type { UiBus, UiStep } from "../contract.ts"
|
|
7
|
+
import {
|
|
8
|
+
bufferSection,
|
|
9
|
+
collapse,
|
|
10
|
+
resetBufferedSections,
|
|
11
|
+
sectionMinimized,
|
|
12
|
+
} from "../render/collapse.ts"
|
|
13
|
+
import { detail } from "../render/detail.ts"
|
|
14
|
+
import { renderDiffCard } from "../render/diff.ts"
|
|
15
|
+
import { formatFriendly, friendlyError, friendlyFromCategory } from "../render/errors.ts"
|
|
16
|
+
import { formatArgsPreview, formatProviderError, formatUsage } from "../render/format.ts"
|
|
17
|
+
import { highlightCode } from "../render/highlight.ts"
|
|
18
|
+
import { decorateMarkdown, type FenceMatch, parseFence } from "../render/markdown.ts"
|
|
19
|
+
import { reasoning } from "../render/reasoning.ts"
|
|
20
|
+
import { sanitizeAnsi, sanitizeAnsiLine } from "../render/sanitize.ts"
|
|
21
|
+
import { c, glyphs, stripAnsi } from "../render/theme.ts"
|
|
22
|
+
import { displayWidth, truncateToWidth } from "../render/width.ts"
|
|
23
|
+
import { formatWrapped } from "../render/wrap.ts"
|
|
24
|
+
import { runWithoutStatus } from "../runtime/statusline.ts"
|
|
25
|
+
|
|
26
|
+
export interface SimpleOptions {
|
|
27
|
+
verbose?: boolean
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Buffer output turn terakhir untuk /copy: teks model (sudah sanitize, sama
|
|
31
|
+
// seperti yang terlihat) + isi hasil tool string. BUKAN transcript penuh —
|
|
32
|
+
// scrollback milik terminal; ini konten yang berguna ditempel ulang. Cap agar
|
|
33
|
+
// sesi panjang tak membengkakkan memori.
|
|
34
|
+
const LAST_TURN_MAX_CHARS = 200_000
|
|
35
|
+
let lastTurnText = ""
|
|
36
|
+
/** Isi output turn terakhir (teks model + hasil tool). */
|
|
37
|
+
export function getLastTurnText(): string {
|
|
38
|
+
return lastTurnText
|
|
39
|
+
}
|
|
40
|
+
const rememberTurn = (s: string) => {
|
|
41
|
+
if (!s) return
|
|
42
|
+
lastTurnText += s
|
|
43
|
+
if (lastTurnText.length > LAST_TURN_MAX_CHARS)
|
|
44
|
+
lastTurnText = lastTurnText.slice(-LAST_TURN_MAX_CHARS)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Tulis teks ke clipboard terminal via OSC 52 (`ESC ] 52 ; c ; base64 BEL`).
|
|
49
|
+
*
|
|
50
|
+
* Sekuens ini SENGAJA dikecualikan dari sanitizeAnsi: sanitizer menjaga teks
|
|
51
|
+
* TAK TERPERCAYA (model/tool), sedangkan payload di sini dibuat sendiri dari
|
|
52
|
+
* buffer yang sudah sanitize + base64 murni — tidak ada byte kontrol asing
|
|
53
|
+
* yang bisa lolos. Banyak terminal memblokir OSC 52 default; pemanggil wajib
|
|
54
|
+
* menyampaikan fallback-nya ke user. Return false bila bukan TTY.
|
|
55
|
+
*/
|
|
56
|
+
export function writeClipboardOsc52(text: string): boolean {
|
|
57
|
+
if (!process.stdout.isTTY) return false
|
|
58
|
+
const b64 = Buffer.from(text, "utf8").toString("base64")
|
|
59
|
+
process.stdout.write(`\x1b]52;c;${b64}\x07`)
|
|
60
|
+
return true
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const wOut = (s: string) => runWithoutStatus(() => process.stdout.write(s))
|
|
64
|
+
const wErr = (s: string) => runWithoutStatus(() => process.stderr.write(s))
|
|
65
|
+
|
|
66
|
+
// Error provider terakhir turn ini — diingat, BUKAN dicetak langsung.
|
|
67
|
+
// Alasan: error tengah-turn sering pulih via fallback router; mencetak tiap
|
|
68
|
+
// event + sekali lagi di catch driver = dua blok ✗ untuk satu kegagalan.
|
|
69
|
+
// Driver (index.ts/repl.ts) mencetak sekali via takePendingError().
|
|
70
|
+
let pendingError: string | null = null
|
|
71
|
+
/** Ambil + kosongkan error tertunda (consume-once per turn). */
|
|
72
|
+
export function takePendingError(): string | null {
|
|
73
|
+
const e = pendingError
|
|
74
|
+
pendingError = null
|
|
75
|
+
return e
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Batas tampilan expanded — ADAPTIF terhadap tinggi terminal, bukan konstanta
|
|
79
|
+
// mati. Scrollback memang tak terbatas, tapi satu tool yang memuntahkan 10
|
|
80
|
+
// ribu baris di terminal 10 baris tetap menenggelamkan konteks; sebaliknya di
|
|
81
|
+
// terminal 60 baris preview 20 baris pelit. Dievaluasi LAZY per render (jangan
|
|
82
|
+
// simpan ke const — lihat P0.1).
|
|
83
|
+
const termRows = (): number => process.stdout.rows || 24
|
|
84
|
+
const TOOL_OUT_MAX_LINES = (): number => Math.max(10, termRows() - 6)
|
|
85
|
+
const CONTENT_PREVIEW_LINES = (): number => Math.max(6, Math.floor(termRows() / 3))
|
|
86
|
+
const DIFF_MAX_LINES = 24
|
|
87
|
+
// Tool yang hasilnya adalah KONTEN yang memang ingin dilihat user (bukan cuma
|
|
88
|
+
// status aksi) — di mode expanded isinya ikut dicetak.
|
|
89
|
+
const CONTENT_TOOLS = new Set([
|
|
90
|
+
"read_file",
|
|
91
|
+
"grep",
|
|
92
|
+
"glob",
|
|
93
|
+
"web_fetch",
|
|
94
|
+
"web_search",
|
|
95
|
+
"bash_output",
|
|
96
|
+
])
|
|
97
|
+
|
|
98
|
+
/** Gabungkan blok search/replace apply_patch menjadi sepasang teks lama/baru. */
|
|
99
|
+
function patchBlocks(patches: unknown): [string, string] {
|
|
100
|
+
const list = Array.isArray(patches) ? (patches as { search?: string; replace?: string }[]) : []
|
|
101
|
+
return [list.map((p) => p.search ?? "").join("\n"), list.map((p) => p.replace ?? "").join("\n")]
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function attachSimpleLogger(bus: UiBus, opts: SimpleOptions = {}): () => void {
|
|
105
|
+
let streamBuffer = ""
|
|
106
|
+
// State fence dipegang DI SINI, bukan di decorateMarkdown: baris datang per
|
|
107
|
+
// event streaming, sedangkan decorateMarkdown memproses satu teks utuh.
|
|
108
|
+
// Sebelumnya `line.includes("```")` toggle naif → fence ~~~ tidak dikenal,
|
|
109
|
+
// fence berbahasa kehilangan highlight, dan wrap salah setelah fence.
|
|
110
|
+
let fence: FenceMatch | null = null
|
|
111
|
+
|
|
112
|
+
// Section thinking: "off" (belum ada), "min" (minimized → buffer),
|
|
113
|
+
// "exp" (expanded → stream live). Header ` + thinking` / ` − thinking`
|
|
114
|
+
// dicetak pada transisi state — scrollback append-only, jadi toggle
|
|
115
|
+
// menambah baris header baru sebagai ganti menimpa yang lama.
|
|
116
|
+
let thinkState: "off" | "min" | "exp" = "off"
|
|
117
|
+
let thinkingBuf = ""
|
|
118
|
+
// Sisa baris reasoning yang belum ber-newline (mode expanded): di-flush
|
|
119
|
+
// per baris utuh agar tampilan rapi, bukan salad fragmen per chunk.
|
|
120
|
+
let reasoningLine = ""
|
|
121
|
+
|
|
122
|
+
// Section answer (jawaban model): default minimize di REPL — teks lengkap
|
|
123
|
+
// TETAP ditulis (buffer), tampilan default satu baris ` + answer (N)`.
|
|
124
|
+
// /copy tak tersentuh (rememberTurn selalu jalan sebelum sink).
|
|
125
|
+
let answerBuf = ""
|
|
126
|
+
let answerTruncated = false
|
|
127
|
+
const ANSWER_BUF_MAX = 1_000_000
|
|
128
|
+
// Jawaban tak pernah disembunyikan di pipa/CI (output mesin harus utuh)
|
|
129
|
+
// dan --verbose selalu menampilkan semua.
|
|
130
|
+
const answerHidden = (): boolean =>
|
|
131
|
+
sectionMinimized("answer") && !!process.stdout.isTTY && !opts.verbose
|
|
132
|
+
const emitAnswer = (s: string) => {
|
|
133
|
+
if (!answerHidden()) {
|
|
134
|
+
wOut(s)
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
if (answerBuf.length < ANSWER_BUF_MAX) {
|
|
138
|
+
answerBuf += s.slice(0, ANSWER_BUF_MAX - answerBuf.length)
|
|
139
|
+
if (answerBuf.length >= ANSWER_BUF_MAX) answerTruncated = true
|
|
140
|
+
} else {
|
|
141
|
+
answerTruncated = true
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Potong ke ≤max KOLOM terminal di batas kata (fallback potong keras).
|
|
146
|
+
* Versi lama memakai s.length/slice karakter: CJK/emoji 2-kolom meluap dan
|
|
147
|
+
* slice mentah bisa membelah sekuens SGR/surrogate — truncateToWidth aman
|
|
148
|
+
* untuk keduanya (tak pernah belah escape, tutup atribut terbuka). */
|
|
149
|
+
const truncateWords = (s: string, max: number): string => {
|
|
150
|
+
if (displayWidth(s) <= max) return s
|
|
151
|
+
let out = ""
|
|
152
|
+
for (const w of s.split(" ")) {
|
|
153
|
+
const cand = out ? `${out} ${w}` : w
|
|
154
|
+
if (displayWidth(cand) > max) break
|
|
155
|
+
out = cand
|
|
156
|
+
}
|
|
157
|
+
// Satu kata raksasa tanpa spasi: potong keras yang aman-SGR, tanpa elipsis
|
|
158
|
+
// (label ringkas, bukan tajuk terpotong).
|
|
159
|
+
if (!out) return truncateToWidth(s, max, "")
|
|
160
|
+
return out
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Label satu-baris ringkas per tool (bukan dump JSON argumen). */
|
|
164
|
+
const toolSummary = (name: string, args: Record<string, unknown>, target?: string): string => {
|
|
165
|
+
if (name === "todo_write" || name === "todo_read") {
|
|
166
|
+
const list = args.todos
|
|
167
|
+
const n = Array.isArray(list) ? list.length : 0
|
|
168
|
+
return n > 0 ? `${name} ${n} items` : name
|
|
169
|
+
}
|
|
170
|
+
if (name === "bash") {
|
|
171
|
+
const cmdStr = (args.cmd as string) ?? (args.command as string)
|
|
172
|
+
if (typeof cmdStr === "string")
|
|
173
|
+
return `bash $ ${truncateWords(sanitizeAnsiLine(String(cmdStr)), 80)}`
|
|
174
|
+
return name
|
|
175
|
+
}
|
|
176
|
+
const short = truncateWords(sanitizeAnsiLine(target ?? formatArgsPreview(args)), 120)
|
|
177
|
+
// Nama tool ikut dari event model — sanitasi di label akhir (perbandingan
|
|
178
|
+
// === di atas tetap pakai nama mentah).
|
|
179
|
+
const tagged = short ? `${name} ${short}` : name
|
|
180
|
+
return sanitizeAnsiLine(tagged)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const flushReasoningTail = () => {
|
|
184
|
+
if (reasoningLine) {
|
|
185
|
+
wErr(c.muted(`${reasoningLine}\n`))
|
|
186
|
+
reasoningLine = ""
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Sisa jawaban yang dikecilkan difinalkan: satu baris ringkas + isi masuk
|
|
191
|
+
// buffer /expand (dibuka via stdout agar kontrak stream terjaga).
|
|
192
|
+
// Dipakai turn:completed DAN detach — abort tanpa completed tak boleh
|
|
193
|
+
// menghilangkan konten diam-diam (perilaku lama menampilkannya live).
|
|
194
|
+
const finalizeAnswer = () => {
|
|
195
|
+
if (answerBuf.length === 0) return
|
|
196
|
+
const n = stripAnsi(answerBuf).length
|
|
197
|
+
const cap = answerTruncated ? ", capped 1MB" : ""
|
|
198
|
+
// Petunjuk /expand WAJIB di baris ini: tanpa itu jawaban yang dikecilkan
|
|
199
|
+
// terlihat "bisu" (tak ada cara membuka yang bisa ditemukan user).
|
|
200
|
+
wErr(c.info(` + answer (${n} chars${cap}) — /expand to read\n`))
|
|
201
|
+
bufferSection("answer", answerBuf, "stdout")
|
|
202
|
+
answerBuf = ""
|
|
203
|
+
answerTruncated = false
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const flushThinking = () => {
|
|
207
|
+
flushReasoningTail()
|
|
208
|
+
if (thinkState === "min" && thinkingBuf) {
|
|
209
|
+
bufferSection("thinking", thinkingBuf)
|
|
210
|
+
thinkingBuf = ""
|
|
211
|
+
}
|
|
212
|
+
thinkState = "off"
|
|
213
|
+
collapse.setActiveSection(null)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const flushLine = (line: string) => {
|
|
217
|
+
const w = process.stdout.columns || 80
|
|
218
|
+
const f = parseFence(line)
|
|
219
|
+
if (f) {
|
|
220
|
+
if (fence === null) fence = f
|
|
221
|
+
else if (f.char === fence.char && f.len >= fence.len) fence = null
|
|
222
|
+
// Baris pembuka/penutup fence adalah delimiter — tidak dicetak.
|
|
223
|
+
return
|
|
224
|
+
}
|
|
225
|
+
if (fence) {
|
|
226
|
+
// Di dalam fence: isi TIDAK disentuh markdown; di-highlight bila ada
|
|
227
|
+
// bahasa. Tanpa bahasa, tetap apa adanya (perbaikan V8: fence tanpa
|
|
228
|
+
// bahasa tidak boleh kehilangan *value* karena dianggap italic).
|
|
229
|
+
const content = fence.lang ? highlightCode(line, fence.lang) : line
|
|
230
|
+
emitAnswer(` ${content}\n`)
|
|
231
|
+
return
|
|
232
|
+
}
|
|
233
|
+
emitAnswer(formatWrapped(decorateMarkdown(line), w, true))
|
|
234
|
+
emitAnswer("\n")
|
|
235
|
+
}
|
|
236
|
+
const flushBuf = () => {
|
|
237
|
+
if (!streamBuffer) return
|
|
238
|
+
const parts = streamBuffer.split("\n")
|
|
239
|
+
for (let i = 0; i < parts.length - 1; i++) flushLine(parts[i]!)
|
|
240
|
+
streamBuffer = parts[parts.length - 1] ?? ""
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const offs: (() => void)[] = []
|
|
244
|
+
offs.push(
|
|
245
|
+
bus.on("turn:started", (e) => {
|
|
246
|
+
// Turn baru: buffer turn sebelumnya dibuang (lihat /copy yang juga
|
|
247
|
+
// reset di sini) — /expand hanya untuk turn yang baru selesai.
|
|
248
|
+
thinkState = "off"
|
|
249
|
+
thinkingBuf = ""
|
|
250
|
+
reasoningLine = ""
|
|
251
|
+
answerBuf = ""
|
|
252
|
+
answerTruncated = false
|
|
253
|
+
resetBufferedSections()
|
|
254
|
+
collapse.setActiveSection(null)
|
|
255
|
+
lastTurnText = ""
|
|
256
|
+
pendingError = null
|
|
257
|
+
if (opts.verbose) wErr(c.muted(`\n── Turn ${e.turn} ──\n`))
|
|
258
|
+
}),
|
|
259
|
+
)
|
|
260
|
+
offs.push(
|
|
261
|
+
bus.on("turn:completed", () => {
|
|
262
|
+
flushBuf()
|
|
263
|
+
if (streamBuffer) {
|
|
264
|
+
flushLine(streamBuffer)
|
|
265
|
+
streamBuffer = ""
|
|
266
|
+
}
|
|
267
|
+
flushThinking()
|
|
268
|
+
finalizeAnswer()
|
|
269
|
+
if (opts.verbose) wErr(c.muted(`\n done\n`))
|
|
270
|
+
}),
|
|
271
|
+
)
|
|
272
|
+
offs.push(
|
|
273
|
+
bus.on("provider:text", (e) => {
|
|
274
|
+
// Teks model TIDAK terpercaya: tanpa sanitasi ia bisa menyisipkan sekuens
|
|
275
|
+
// kontrol (bersihkan layar, ubah judul jendela) langsung ke scrollback.
|
|
276
|
+
// Versi sanitize yang SAMA masuk buffer /copy (apa yang terlihat).
|
|
277
|
+
// Teks = jawaban; section-nya ikut collapse (minimize → buffer + expand
|
|
278
|
+
// via + / /expand), kecuali pipa/CI/verbose yang selalu stream.
|
|
279
|
+
flushThinking()
|
|
280
|
+
collapse.setActiveSection("answer")
|
|
281
|
+
if (!answerHidden() && answerBuf.length > 0) {
|
|
282
|
+
wErr(c.muted(" − answer\n"))
|
|
283
|
+
wOut(answerBuf)
|
|
284
|
+
answerBuf = ""
|
|
285
|
+
answerTruncated = false
|
|
286
|
+
}
|
|
287
|
+
const clean = sanitizeAnsi(e.text)
|
|
288
|
+
streamBuffer += clean
|
|
289
|
+
rememberTurn(clean)
|
|
290
|
+
flushBuf()
|
|
291
|
+
}),
|
|
292
|
+
)
|
|
293
|
+
offs.push(
|
|
294
|
+
bus.on("provider:extension", (e) => {
|
|
295
|
+
if (e.kind === "reasoning") {
|
|
296
|
+
// Section thinking: reasoning.visible = expanded (stream), else
|
|
297
|
+
// minimized (satu baris ` + thinking`, isi di-buffer untuk /expand).
|
|
298
|
+
// Toggle runtime /thinking, Ctrl+T, atau tombol + / - saat busy.
|
|
299
|
+
collapse.setActiveSection("thinking")
|
|
300
|
+
const d = e.data as { text?: string }
|
|
301
|
+
const text = d.text ?? ""
|
|
302
|
+
// --verbose = semua terlihat (expanded). Kalau tidak, reasoning.visible
|
|
303
|
+
// menentukan: true = stream live, false = ` + thinking` + buffer.
|
|
304
|
+
const expanded = opts.verbose || reasoning.visible
|
|
305
|
+
if (expanded) {
|
|
306
|
+
if (thinkState !== "exp") {
|
|
307
|
+
thinkState = "exp"
|
|
308
|
+
wErr(c.muted(` − thinking\n`))
|
|
309
|
+
if (thinkingBuf) {
|
|
310
|
+
wErr(c.muted(thinkingBuf))
|
|
311
|
+
thinkingBuf = ""
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
// Line-buffered: kumpulkan sampai newline agar tampilan rapi
|
|
315
|
+
// (bukan salad fragmen per chunk); sisa di-flush saat fase berakhir.
|
|
316
|
+
if (text) {
|
|
317
|
+
// Teks reasoning TAK TERPERCAYA seperti provider:text — sanitasi
|
|
318
|
+
// saat masuk (bukan saat flush) agar tak ada jalur cetak yang lupa.
|
|
319
|
+
reasoningLine += sanitizeAnsi(text)
|
|
320
|
+
const parts = reasoningLine.split("\n")
|
|
321
|
+
for (let i = 0; i < parts.length - 1; i++) wErr(c.muted(`${parts[i]}\n`))
|
|
322
|
+
reasoningLine = parts[parts.length - 1] ?? ""
|
|
323
|
+
}
|
|
324
|
+
} else {
|
|
325
|
+
if (thinkState !== "min") {
|
|
326
|
+
thinkState = "min"
|
|
327
|
+
// Sisa baris expanded yang belum selesai dicetak dulu (terlihat),
|
|
328
|
+
// baru header minimize — konten tak hilang, tak menempel.
|
|
329
|
+
flushReasoningTail()
|
|
330
|
+
wErr(c.info(` + thinking\n`))
|
|
331
|
+
}
|
|
332
|
+
if (text) {
|
|
333
|
+
// Sama: buffer thinking ikut tercemar bila mentah (flush expanded
|
|
334
|
+
// mencetaknya verbatim + /expand menampilkannya lagi).
|
|
335
|
+
thinkingBuf += sanitizeAnsi(text)
|
|
336
|
+
if (thinkingBuf.length > 200_000) thinkingBuf = thinkingBuf.slice(-200_000)
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
} else if (e.kind === "usage") {
|
|
340
|
+
const u = e.data as { inputTokens?: number; outputTokens?: number }
|
|
341
|
+
const txt = formatUsage(u)
|
|
342
|
+
if (txt && opts.verbose) wErr(c.muted(` ${txt}\n`))
|
|
343
|
+
} else if (e.kind === "bash-output") {
|
|
344
|
+
// Progres bash inkremental — hanya di --verbose supaya output default
|
|
345
|
+
// tidak dibanjiri log build. Ringkasan tetap muncul di execution:completed.
|
|
346
|
+
if (opts.verbose) {
|
|
347
|
+
const d = e.data as { text?: string }
|
|
348
|
+
if (d.text) wErr(c.muted(sanitizeAnsi(d.text)))
|
|
349
|
+
}
|
|
350
|
+
} else if (e.kind === "error") {
|
|
351
|
+
const d = e.data as { message?: string; category?: string }
|
|
352
|
+
pendingError = formatProviderError(d)
|
|
353
|
+
} else if (e.kind === "content_filter") {
|
|
354
|
+
wErr(c.warning(`\n! Content filter blocked\n`))
|
|
355
|
+
}
|
|
356
|
+
}),
|
|
357
|
+
)
|
|
358
|
+
offs.push(
|
|
359
|
+
bus.on("step:started", (e: { step: UiStep }) => {
|
|
360
|
+
if (!opts.verbose) return
|
|
361
|
+
const calls = e.step.toolCalls
|
|
362
|
+
// Nama + argumen dari model (tak terpercaya): sanitasi sebelum tampil.
|
|
363
|
+
.map(
|
|
364
|
+
(tc) =>
|
|
365
|
+
`${c.info(sanitizeAnsiLine(tc.name))}(${c.muted(sanitizeAnsiLine(formatArgsPreview(tc.args)))})`,
|
|
366
|
+
)
|
|
367
|
+
.join(", ")
|
|
368
|
+
wErr(c.muted(` Step ${e.step.index}: ${calls}\n`))
|
|
369
|
+
}),
|
|
370
|
+
)
|
|
371
|
+
offs.push(
|
|
372
|
+
bus.on("execution:started", (e) => {
|
|
373
|
+
// Section aktif = tool yang sedang jalan — target tombol + / - saat busy.
|
|
374
|
+
collapse.setActiveSection("tool")
|
|
375
|
+
// Ganti fase: sisa baris thinking yang belum ber-newline dicetak dulu
|
|
376
|
+
// agar tak menempel ke baris tool.
|
|
377
|
+
flushReasoningTail()
|
|
378
|
+
if (detail.compact) {
|
|
379
|
+
// Mode compact: TIDAK ada baris start. Aktivitas ditampilkan oleh
|
|
380
|
+
// garis status "Thinking"/nama tool (TTY) atau diam (non-TTY) — baris
|
|
381
|
+
// `running x... ` lama tanpa newline menempel ke baris ✓ berikutnya
|
|
382
|
+
// di log non-interaktif, dan mencemari stderr redirect.
|
|
383
|
+
return
|
|
384
|
+
}
|
|
385
|
+
// Expanded: user melihat tool apa yang mulai berjalan SEBELUM hasilnya,
|
|
386
|
+
// inline di aliran output — transparansi ala shell (`set -x`).
|
|
387
|
+
const args = (e.execution.call.args ?? {}) as Record<string, unknown>
|
|
388
|
+
wErr(
|
|
389
|
+
c.muted(
|
|
390
|
+
` ${glyphs.arrow} ${sanitizeAnsiLine(e.execution.call.name)} ${sanitizeAnsiLine(formatArgsPreview(args))}\n`,
|
|
391
|
+
),
|
|
392
|
+
)
|
|
393
|
+
}),
|
|
394
|
+
)
|
|
395
|
+
offs.push(
|
|
396
|
+
bus.on("execution:completed", (e) => {
|
|
397
|
+
const r = e.execution.result
|
|
398
|
+
const name = e.execution.call.name
|
|
399
|
+
const args = (e.execution.call.args ?? {}) as Record<string, unknown>
|
|
400
|
+
// Hasil string ikut ke buffer /copy (versi sanitize, cap per-add agar
|
|
401
|
+
// satu read_file raksasa tak langsung memenuhi buffer sendirian).
|
|
402
|
+
if (!r.isError && typeof r.content === "string")
|
|
403
|
+
rememberTurn(truncateToWidth(sanitizeAnsi(r.content), 20000, ""))
|
|
404
|
+
if (r.isError) {
|
|
405
|
+
// Error tool SELALU tampil penuh — tidak pernah dikecilkan.
|
|
406
|
+
collapse.setActiveSection(null)
|
|
407
|
+
wErr(
|
|
408
|
+
c.error(
|
|
409
|
+
` ${glyphs.arrow} ${sanitizeAnsiLine(name)}: ${truncateToWidth(sanitizeAnsi(String(r.content)), 200, "")}\n`,
|
|
410
|
+
),
|
|
411
|
+
)
|
|
412
|
+
return
|
|
413
|
+
}
|
|
414
|
+
const target = typeof args.path === "string" ? args.path : undefined
|
|
415
|
+
// Section tool dikecilkan: satu baris ` + label`, isi di-buffer untuk
|
|
416
|
+
// /expand. Ledger lain (chevron) tetap satu baris — ini menggantikan
|
|
417
|
+
// pencetakan isi (bash/edit/diff/content tool), bukan marker.
|
|
418
|
+
if (sectionMinimized("tool")) {
|
|
419
|
+
collapse.setActiveSection(null)
|
|
420
|
+
const label = toolSummary(name, args, target)
|
|
421
|
+
wErr(c.info(` + ${label}\n`))
|
|
422
|
+
bufferSection(label, sanitizeAnsi(String(r.content ?? "")).trim())
|
|
423
|
+
return
|
|
424
|
+
}
|
|
425
|
+
if (name === "write_file" && target) {
|
|
426
|
+
const size = typeof r.content === "string" ? `${(r.content as string).length} chars` : ""
|
|
427
|
+
// Target dari argumen model — sanitasi agar path berisi escape tak
|
|
428
|
+
// membersihkan layar saat receipt sukses tampil.
|
|
429
|
+
const cleanTarget = sanitizeAnsiLine(target)
|
|
430
|
+
wOut(
|
|
431
|
+
c.success(
|
|
432
|
+
` ${glyphs.arrow} write_file ${cleanTarget}${size ? c.muted(` (${size})`) : ""}\n`,
|
|
433
|
+
),
|
|
434
|
+
)
|
|
435
|
+
return
|
|
436
|
+
}
|
|
437
|
+
if ((name === "edit" || name === "apply_patch") && target) {
|
|
438
|
+
const [oldT, newT] =
|
|
439
|
+
name === "edit"
|
|
440
|
+
? [String(args.oldString ?? ""), String(args.newString ?? "")]
|
|
441
|
+
: patchBlocks(args.patches)
|
|
442
|
+
// Expanded: diff adalah inti perubahan — tampilkan inline. Compact
|
|
443
|
+
// jatuh ke baris ringkasan seperti sebelumnya.
|
|
444
|
+
if (!detail.compact && (oldT || newT)) {
|
|
445
|
+
wOut(
|
|
446
|
+
`${renderDiffCard(sanitizeAnsiLine(target), sanitizeAnsi(oldT), sanitizeAnsi(newT), {
|
|
447
|
+
maxLines: DIFF_MAX_LINES,
|
|
448
|
+
})}\n`,
|
|
449
|
+
)
|
|
450
|
+
return
|
|
451
|
+
}
|
|
452
|
+
wOut(c.success(` ${glyphs.arrow} ${sanitizeAnsiLine(name)} ${sanitizeAnsiLine(target)}\n`))
|
|
453
|
+
return
|
|
454
|
+
}
|
|
455
|
+
// todo_write: tampilkan daftarnya utuh — ini rencana kerja, bukan noise.
|
|
456
|
+
if (name === "todo_write" || name === "todo_read") {
|
|
457
|
+
wErr(
|
|
458
|
+
c.success(` ${glyphs.arrow} ${name}\n`) +
|
|
459
|
+
c.muted(`${sanitizeAnsi(String(r.content))}\n`),
|
|
460
|
+
)
|
|
461
|
+
return
|
|
462
|
+
}
|
|
463
|
+
const cmdStr = (args.cmd as string) ?? (args.command as string)
|
|
464
|
+
if (name === "bash" && typeof cmdStr === "string") {
|
|
465
|
+
const cmdLabel = truncateToWidth(sanitizeAnsiLine(String(cmdStr)), 80, "")
|
|
466
|
+
// Output tool tak terpercaya (bisa berisi isi berkas): sanitasi SEKALI
|
|
467
|
+
// di sini — cabang compact di bawah dan expanded map di-sanitize lagi
|
|
468
|
+
// (idempoten) sehingga tak ada cabang yang mentah.
|
|
469
|
+
const lines = sanitizeAnsi(String(r.content)).trim().split("\n").filter(Boolean)
|
|
470
|
+
if (detail.compact) {
|
|
471
|
+
const preview =
|
|
472
|
+
lines.length > 3
|
|
473
|
+
? lines.slice(0, 3).join("\n ") + c.muted(`\n ... (${lines.length - 3} more)`)
|
|
474
|
+
: lines.join("\n ")
|
|
475
|
+
wErr(c.success(` ${glyphs.arrow} $ ${cmdLabel}\n`) + c.muted(` ${preview}\n`))
|
|
476
|
+
return
|
|
477
|
+
}
|
|
478
|
+
const maxLines = TOOL_OUT_MAX_LINES()
|
|
479
|
+
const shown = lines.slice(0, maxLines).map((l) => ` ${sanitizeAnsi(l)}`)
|
|
480
|
+
const more =
|
|
481
|
+
lines.length > maxLines ? c.muted(`\n … (${lines.length - maxLines} more lines)`) : ""
|
|
482
|
+
wErr(
|
|
483
|
+
c.success(` ${glyphs.arrow} $ ${cmdLabel}\n`) +
|
|
484
|
+
(shown.length ? `${c.muted(shown.join("\n")) + more}\n` : ""),
|
|
485
|
+
)
|
|
486
|
+
return
|
|
487
|
+
}
|
|
488
|
+
// Tool penghasil KONTEN (isi berkas, hasil cari) di mode compact cukup
|
|
489
|
+
// satu baris › + target — isinya milik model untuk dibaca, bukan untuk
|
|
490
|
+
// membanjiri scrollback pengguna. Konten tetap bisa dilihat via expanded.
|
|
491
|
+
if (detail.compact && CONTENT_TOOLS.has(name)) {
|
|
492
|
+
const label = truncateWords(sanitizeAnsiLine(target ?? formatArgsPreview(args)), 120)
|
|
493
|
+
wErr(c.success(` ${glyphs.arrow} ${sanitizeAnsiLine(name)}${label ? ` ${label}` : ""}\n`))
|
|
494
|
+
return
|
|
495
|
+
}
|
|
496
|
+
if (!detail.compact && CONTENT_TOOLS.has(name)) {
|
|
497
|
+
// Expanded: hasil berupa KONTEN (isi berkas, hasil cari) ikut mengalir.
|
|
498
|
+
const raw = sanitizeAnsi(String(r.content ?? "")).trim()
|
|
499
|
+
const lines = raw ? raw.split("\n") : []
|
|
500
|
+
const maxPreview = CONTENT_PREVIEW_LINES()
|
|
501
|
+
const preview = lines
|
|
502
|
+
.slice(0, maxPreview)
|
|
503
|
+
.map((l) => ` ${l}`)
|
|
504
|
+
.join("\n")
|
|
505
|
+
const more =
|
|
506
|
+
lines.length > maxPreview
|
|
507
|
+
? c.muted(`\n … (${lines.length - maxPreview} more lines)`)
|
|
508
|
+
: ""
|
|
509
|
+
const label = sanitizeAnsiLine(target ?? formatArgsPreview(args))
|
|
510
|
+
wErr(
|
|
511
|
+
c.success(` ${glyphs.arrow} ${sanitizeAnsiLine(name)} ${label}\n`) +
|
|
512
|
+
(preview ? `${c.muted(preview) + more}\n` : ""),
|
|
513
|
+
)
|
|
514
|
+
return
|
|
515
|
+
}
|
|
516
|
+
// Sisa tool (compact & expanded): satu baris › + label. WAJIB diakhiri
|
|
517
|
+
// newline — tanpa itu baris berikutnya menempel (overlap di stderr log).
|
|
518
|
+
const label = truncateWords(sanitizeAnsiLine(target ?? formatArgsPreview(args)), 120)
|
|
519
|
+
const first = sanitizeAnsi(String(r.content)).trim().split("\n")[0] ?? ""
|
|
520
|
+
const preview = truncateToWidth(first, 80, "")
|
|
521
|
+
const cleanName = sanitizeAnsiLine(name)
|
|
522
|
+
if (!detail.compact && preview) {
|
|
523
|
+
wErr(
|
|
524
|
+
c.success(
|
|
525
|
+
` ${glyphs.arrow} ${cleanName}${label ? ` ${label}` : ""} ${c.muted(preview)}\n`,
|
|
526
|
+
),
|
|
527
|
+
)
|
|
528
|
+
return
|
|
529
|
+
}
|
|
530
|
+
wErr(c.success(` ${glyphs.arrow} ${cleanName}${label ? ` ${label}` : ""}\n`))
|
|
531
|
+
}),
|
|
532
|
+
)
|
|
533
|
+
offs.push(bus.on("context:compacted", (e) => wErr(c.warning(` ── compacted: ${e.reason}\n`))))
|
|
534
|
+
|
|
535
|
+
return () => {
|
|
536
|
+
// Sisa parsal di-flush dulu (jangan hilang diam-diam), baru lepas.
|
|
537
|
+
// Tanpa ini detach di tengah baris membuang ekornya — dan di jalur abort
|
|
538
|
+
// (tanpa turn:completed) sisa streamBuffer bocor ke turn berikutnya.
|
|
539
|
+
if (streamBuffer) {
|
|
540
|
+
flushLine(streamBuffer)
|
|
541
|
+
streamBuffer = ""
|
|
542
|
+
}
|
|
543
|
+
flushReasoningTail()
|
|
544
|
+
flushThinking()
|
|
545
|
+
finalizeAnswer()
|
|
546
|
+
for (const off of offs) off()
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Error apa pun → satu pesan siap tampil.
|
|
552
|
+
*
|
|
553
|
+
* Sebelumnya mengembalikan `${kind}: ${message}` mentah, sehingga baris terakhir
|
|
554
|
+
* yang dilihat user setelah run gagal adalah dump JSON provider — pada uji live
|
|
555
|
+
* OpenRouter: `provider: rate limited (429): {"error":{...400 karakter...}}`.
|
|
556
|
+
* Kini kategori dipetakan lewat src/ui/render/errors.ts, sama seperti event error.
|
|
557
|
+
*/
|
|
558
|
+
export function formatError(e: unknown): string {
|
|
559
|
+
const obj = e as { kind?: string; category?: string; message?: string } | undefined
|
|
560
|
+
// ProviderError punya `category`; AgentError punya `kind`.
|
|
561
|
+
if (obj?.category) return formatFriendly(friendlyFromCategory(obj.category, obj.message ?? ""))
|
|
562
|
+
if (obj?.kind) {
|
|
563
|
+
const friendly = friendlyError(`${obj.kind}: ${obj.message ?? ""}`)
|
|
564
|
+
return formatFriendly(friendly)
|
|
565
|
+
}
|
|
566
|
+
if (e instanceof Error) return formatFriendly(friendlyError(e.message))
|
|
567
|
+
return String(e)
|
|
568
|
+
}
|