minicode-ai 0.13.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/LICENSE +21 -21
- package/README.md +4 -0
- package/cli/args.ts +50 -2
- package/cli/commands/acp.ts +18 -3
- package/cli/commands/daemon.ts +114 -19
- package/cli/commands/exec.ts +95 -7
- package/cli/commands/mcp.ts +66 -5
- package/cli/commands/memory.ts +46 -14
- package/cli/commands.ts +5 -0
- package/cli/index.ts +30 -5
- package/cli/minicode.js +6 -0
- package/cli/setup.ts +365 -24
- package/cli/tui.ts +72 -27
- package/docs/TERMINAL_CONTRACT.md +91 -24
- package/docs/agents.md +2 -2
- package/docs/cli.md +1 -0
- package/docs/environment.md +1 -0
- package/docs/memory-sessions.md +5 -0
- package/package.json +3 -2
- package/src/app/session.ts +153 -35
- package/src/app/tool-layer.ts +67 -61
- package/src/daemon/approvals.ts +25 -1
- package/src/daemon/client.ts +51 -1
- package/src/daemon/control.ts +8 -0
- package/src/daemon/protocol.ts +47 -1
- package/src/daemon/server.ts +45 -9
- package/src/daemon/subscription.ts +25 -1
- package/src/daemon/telemetry.ts +4 -0
- package/src/hooks/run.ts +5 -1
- package/src/mcp/client.ts +349 -90
- package/src/mcp/http-transport.ts +7 -0
- package/src/mcp/transport.ts +5 -0
- package/src/memory/files.ts +69 -3
- package/src/memory/vector.ts +556 -160
- package/src/policy/approval-binding.ts +199 -0
- package/src/policy/context.ts +20 -1
- package/src/policy/executor.ts +44 -5
- package/src/policy/permission.ts +243 -26
- package/src/policy/tool-contract.ts +77 -0
- package/src/policy/usage.ts +3 -1
- package/src/policy/work-mode.ts +49 -1
- package/src/presentation/adapter.ts +157 -7
- package/src/presentation/events.ts +173 -1
- package/src/presentation/model.ts +37 -0
- package/src/presentation/projection.ts +107 -18
- package/src/presentation/reducer.ts +134 -10
- package/src/sandbox/os.ts +18 -1
- package/src/session/persistence.ts +153 -11
- package/src/task/store.ts +43 -3
- package/src/tools/memory.ts +70 -10
- package/src/tools/task.ts +7 -1
- package/src/tools/todo.ts +14 -4
- package/src/ui/approval/prompt.ts +52 -7
- package/src/ui/assistant/simple.ts +160 -57
- package/src/ui/contract.ts +46 -0
- package/src/ui/footer.ts +14 -5
- package/src/ui/i18n/en.ts +9 -0
- package/src/ui/i18n/id.ts +9 -0
- package/src/ui/render/collapse.ts +24 -52
- package/src/ui/render/detail.ts +14 -9
- package/src/ui/render/errors.ts +8 -0
- package/src/ui/render/reasoning.ts +13 -8
- package/src/ui/session-state.ts +243 -0
- package/src/ui/tui/app.ts +7 -3
- package/src/ui/tui/transcript.ts +271 -30
- package/vendor/minicore/src/core/budget.ts +25 -25
- package/vendor/minicore/src/core/compact.ts +75 -75
- package/vendor/minicore/src/core/errors.ts +59 -59
- package/vendor/minicore/src/core/events.ts +73 -73
- package/vendor/minicore/src/core/executor.ts +139 -139
- package/vendor/minicore/src/core/history.ts +31 -31
- package/vendor/minicore/src/core/index.ts +41 -41
- package/vendor/minicore/src/core/loop.ts +355 -355
- package/vendor/minicore/src/core/permission.ts +22 -22
- package/vendor/minicore/src/core/provider.ts +39 -39
- package/vendor/minicore/src/core/recovery.ts +35 -35
- package/vendor/minicore/src/core/session.ts +327 -327
- package/vendor/minicore/src/core/snapshot.ts +146 -146
- package/vendor/minicore/src/core/tokens.ts +107 -107
- package/vendor/minicore/src/core/tool.ts +115 -115
- package/vendor/minicore/src/core/types.ts +50 -50
- package/vendor/minicore/src/providers/openai-compat.ts +417 -417
|
@@ -1,59 +1,47 @@
|
|
|
1
1
|
// Satu sumber state "section collapse" untuk output besar saat turn
|
|
2
|
-
// (thinking, bash, edit/apply_patch, content tool).
|
|
3
|
-
//
|
|
4
|
-
//
|
|
2
|
+
// (thinking, bash, edit/apply_patch, content tool).
|
|
3
|
+
//
|
|
4
|
+
// [F-ENV-1] State module-local di-bootstrap SATU KALI dari env saat import; setter
|
|
5
|
+
// TIDAK lagi menulis `process.env` (pilihan UI bukan konfigurasi yang boleh
|
|
6
|
+
// diwarisi sub-proses). Sebelumnya `MINICODE_MINIMIZE_ANSWER/_TOOL` ditulis ulang.
|
|
7
|
+
//
|
|
8
|
+
// [Area A · PROMPT 06] API modul ini kini HANYA delegator ke instance default
|
|
9
|
+
// `UiSessionState`. State sebenarnya tinggal di `src/ui/session-state.ts` dan
|
|
10
|
+
// dimiliki SESI (composition root membuat satu instance per sesi lalu
|
|
11
|
+
// meneruskannya ke Transcript + printer linier). Delegator di sini dipertahankan
|
|
12
|
+
// agar call-site lama (one-shot, export test) tetap jalan tanpa DI besar.
|
|
5
13
|
//
|
|
6
14
|
// Semantik tombol: `+` = expand (lihat isi), `-` = minimize (satu baris).
|
|
7
15
|
// Minimized = isi TIDAK dicetak, hanya ` + label` — isi di-buffer untuk
|
|
8
16
|
// /expand. Thinking memakai reasoning.visible sebagai invers dari minimize
|
|
9
17
|
// (expanded = visible), jadi /thinking/Ctrl+T lama tetap satu arti.
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
18
|
+
import {
|
|
19
|
+
type CollapsedSectionView,
|
|
20
|
+
type CollapseSection,
|
|
21
|
+
defaultUiState,
|
|
22
|
+
} from "../session-state.ts"
|
|
14
23
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
// Buffer konten yang dikecilkan pada turn terakhir — dibuka via /expand.
|
|
18
|
-
export interface CollapsedSectionView {
|
|
19
|
-
label: string
|
|
20
|
-
text: string
|
|
21
|
-
/** Stream asal konten (kontrak Unix dipertahankan saat /expand mencetak). */
|
|
22
|
-
stream: "stdout" | "stderr"
|
|
23
|
-
}
|
|
24
|
-
const MAX_SECTION_CHARS = 200_000
|
|
25
|
-
const MAX_BUFFER_TOTAL = 500_000
|
|
26
|
-
const collapsedSectionViews: CollapsedSectionView[] = []
|
|
24
|
+
export type { CollapsedSectionView, CollapseSection }
|
|
27
25
|
|
|
28
26
|
export const collapse = {
|
|
29
27
|
/** Section yang sedang aktif — keputusan tombol + / - saat turn berjalan. */
|
|
30
28
|
get activeSection(): CollapseSection | null {
|
|
31
|
-
return activeSection
|
|
29
|
+
return defaultUiState.activeSection
|
|
32
30
|
},
|
|
33
31
|
setActiveSection(k: CollapseSection | null): void {
|
|
34
|
-
|
|
32
|
+
defaultUiState.setActiveSection(k)
|
|
35
33
|
},
|
|
36
34
|
}
|
|
37
35
|
|
|
38
36
|
/** Section sedang dikecilkan? Thinking = invers reasoning.visible. */
|
|
39
37
|
export function sectionMinimized(section: CollapseSection): boolean {
|
|
40
|
-
|
|
41
|
-
if (section === "answer") return process.env.MINICODE_MINIMIZE_ANSWER === "1"
|
|
42
|
-
return process.env.MINICODE_MINIMIZE_TOOL === "1"
|
|
38
|
+
return defaultUiState.sectionMinimized(section)
|
|
43
39
|
}
|
|
44
40
|
|
|
45
41
|
/** Set eksplisit (nilai minimize) atau toggle bila `next` tidak diberikan.
|
|
46
42
|
* Return nilai minimize terbaru. */
|
|
47
43
|
export function setSectionMinimized(section: CollapseSection, next?: boolean): boolean {
|
|
48
|
-
|
|
49
|
-
const vis = setReasoningVisible(next === undefined ? undefined : !next)
|
|
50
|
-
return !vis
|
|
51
|
-
}
|
|
52
|
-
const key = section === "answer" ? "MINICODE_MINIMIZE_ANSWER" : "MINICODE_MINIMIZE_TOOL"
|
|
53
|
-
const cur = process.env[key] === "1"
|
|
54
|
-
const nxt = next ?? !cur
|
|
55
|
-
process.env[key] = nxt ? "1" : "0"
|
|
56
|
-
return nxt
|
|
44
|
+
return defaultUiState.setSectionMinimized(section, next)
|
|
57
45
|
}
|
|
58
46
|
|
|
59
47
|
/** Simpan isi section yang dikecilkan (cap per-entry + total). */
|
|
@@ -62,30 +50,14 @@ export function rememberCollapsedSection(
|
|
|
62
50
|
text: string,
|
|
63
51
|
stream: CollapsedSectionView["stream"] = "stderr",
|
|
64
52
|
): void {
|
|
65
|
-
|
|
66
|
-
// Cap jangan belah surrogate pair (U+FFFD di /expand) dan jangan sisakan
|
|
67
|
-
// ekor escape parsial (SGR gantung ikut tercetak saat /expand).
|
|
68
|
-
let cut = text.length > MAX_SECTION_CHARS ? text.slice(0, MAX_SECTION_CHARS) : text
|
|
69
|
-
const last = cut.charCodeAt(cut.length - 1)
|
|
70
|
-
if (cut.length > 0 && last >= 0xd800 && last <= 0xdbff) cut = cut.slice(0, -1)
|
|
71
|
-
cut = splitTrailingEscape(cut).head
|
|
72
|
-
collapsedSectionViews.push({ label, text: cut, stream })
|
|
73
|
-
// Buang yang tertua sampai total dalam budget — terbaru selalu dipertahankan.
|
|
74
|
-
let total = 0
|
|
75
|
-
let keepFrom = collapsedSectionViews.length
|
|
76
|
-
for (let i = collapsedSectionViews.length - 1; i >= 0; i--) {
|
|
77
|
-
total += collapsedSectionViews[i]!.text.length
|
|
78
|
-
if (total > MAX_BUFFER_TOTAL) break
|
|
79
|
-
keepFrom = i
|
|
80
|
-
}
|
|
81
|
-
if (keepFrom > 0) collapsedSectionViews.splice(0, keepFrom)
|
|
53
|
+
defaultUiState.rememberCollapsedSection(label, text, stream)
|
|
82
54
|
}
|
|
83
55
|
|
|
84
56
|
export function clearCollapsedSections(): void {
|
|
85
|
-
|
|
57
|
+
defaultUiState.clearCollapsedSections()
|
|
86
58
|
}
|
|
87
59
|
|
|
88
60
|
/** Konten section yang dikecilkan pada turn terakhir (untuk /expand). */
|
|
89
61
|
export function collapsedSectionsSnapshot(): CollapsedSectionView[] {
|
|
90
|
-
return
|
|
62
|
+
return defaultUiState.collapsedSectionsSnapshot()
|
|
91
63
|
}
|
package/src/ui/render/detail.ts
CHANGED
|
@@ -3,22 +3,27 @@
|
|
|
3
3
|
// Posisi Minicode = agentic Unix shell: tool call transparan di aliran output.
|
|
4
4
|
// Default tergantung konteks: one-shot/exec = expanded; REPL interaktif
|
|
5
5
|
// mengaktifkan compact saat start (lihat runRepl) kecuali env diset eksplisit.
|
|
6
|
-
// `MINICODE_COMPACT=1`/`0` atau `/compact` selalu menang.
|
|
7
|
-
//
|
|
8
|
-
// env
|
|
6
|
+
// `MINICODE_COMPACT=1`/`0` atau `/compact` selalu menang.
|
|
7
|
+
//
|
|
8
|
+
// [F-ENV-1] State module-local di-bootstrap SATU KALI dari env saat import; setter
|
|
9
|
+
// TIDAK lagi menulis `process.env` (pilihan UI bukan konfigurasi yang boleh
|
|
10
|
+
// diwarisi sub-proses). Getter dibaca saat dipakai agar toggle runtime langsung
|
|
11
|
+
// berlaku — jangan simpan `detail.compact` ke const di module scope.
|
|
12
|
+
//
|
|
13
|
+
// [Area A · PROMPT 06] Delegator ke instance default `UiSessionState`; state
|
|
14
|
+
// sebenarnya dimiliki SESI (lihat `src/ui/session-state.ts`).
|
|
15
|
+
import { defaultUiState } from "../session-state.ts"
|
|
16
|
+
|
|
9
17
|
export const detail = {
|
|
10
18
|
get compact(): boolean {
|
|
11
|
-
return
|
|
19
|
+
return defaultUiState.compact
|
|
12
20
|
},
|
|
13
21
|
set compact(v: boolean) {
|
|
14
|
-
|
|
22
|
+
defaultUiState.compact = v
|
|
15
23
|
},
|
|
16
24
|
}
|
|
17
25
|
|
|
18
26
|
/** Set eksplisit (on/off) atau toggle bila `next` tidak diberikan. */
|
|
19
27
|
export function setCompactMode(next?: boolean): boolean {
|
|
20
|
-
|
|
21
|
-
const nextVal = next ?? !cur
|
|
22
|
-
detail.compact = nextVal
|
|
23
|
-
return nextVal
|
|
28
|
+
return defaultUiState.setCompactMode(next)
|
|
24
29
|
}
|
package/src/ui/render/errors.ts
CHANGED
|
@@ -247,6 +247,14 @@ export function friendlyError(raw: string): FriendlyError {
|
|
|
247
247
|
message: t("err.steps"),
|
|
248
248
|
fix: t("err.stepsFix"),
|
|
249
249
|
}
|
|
250
|
+
// [Live Hardening 0.13.x] Satu-satunya throw kernel ber-kind budget_exceeded
|
|
251
|
+
// tanpa flag operator adalah kehabisan jendela konteks SETELAH kompaksi
|
|
252
|
+
// ("context window exceeded after compaction", vendor loop). Catch-all
|
|
253
|
+
// "budget" di bawah memmask pesan itu menjadi "Session budget exceeded"
|
|
254
|
+
// dan menyarankan --budget — saran yang salah total (uji live tiprouter:
|
|
255
|
+
// riwayat membengkak ~72k token hingga critical). Kenali lebih dulu.
|
|
256
|
+
if (lower.includes("context window"))
|
|
257
|
+
return { message: t("err.context"), fix: t("err.contextFix") }
|
|
250
258
|
if (lower.includes("budget")) return { message: t("err.budget"), fix: t("err.budgetFix") }
|
|
251
259
|
if (lower.includes("busy")) return { message: t("err.busy"), fix: t("err.busyFix") }
|
|
252
260
|
if (lower.includes("aborted")) return { message: t("err.aborted") }
|
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
// Satu sumber state "tampilkan reasoning".
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
3
|
+
// [F-ENV-1] State module-local, di-bootstrap SATU KALI dari env saat import.
|
|
4
|
+
// Setter TIDAK lagi menulis `process.env`: pilihan tampilan adalah state
|
|
5
|
+
// instance proses, bukan konfigurasi yang boleh diwarisi sub-agent/MCP (sebelumnya
|
|
6
|
+
// `MINICODE_SHOW_THINKING` ditulis ke env → bocor ke anak). Getter tetap dibaca
|
|
7
|
+
// saat dipakai agar toggle runtime langsung berlaku tanpa re-import.
|
|
8
|
+
//
|
|
9
|
+
// [Area A · PROMPT 06] Delegator ke instance default `UiSessionState`; state
|
|
10
|
+
// sebenarnya dimiliki SESI (lihat `src/ui/session-state.ts`).
|
|
11
|
+
import { defaultUiState } from "../session-state.ts"
|
|
12
|
+
|
|
5
13
|
export const reasoning = {
|
|
6
14
|
get visible(): boolean {
|
|
7
|
-
return
|
|
15
|
+
return defaultUiState.reasoningVisible
|
|
8
16
|
},
|
|
9
17
|
set visible(v: boolean) {
|
|
10
|
-
|
|
18
|
+
defaultUiState.reasoningVisible = v
|
|
11
19
|
},
|
|
12
20
|
}
|
|
13
21
|
|
|
14
22
|
/** Set eksplisit (on/off) atau toggle bila `next` tidak diberikan. */
|
|
15
23
|
export function setReasoningVisible(next?: boolean): boolean {
|
|
16
|
-
|
|
17
|
-
const nextVal = next ?? !cur
|
|
18
|
-
reasoning.visible = nextVal
|
|
19
|
-
return nextVal
|
|
24
|
+
return defaultUiState.setReasoningVisible(next)
|
|
20
25
|
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// State UI per-sesi (PROMPT 06 · Area A — isolasi antar-instance).
|
|
2
|
+
//
|
|
3
|
+
// Kenapa TIDAK boleh singleton: `process.env` sudah dihapus sebagai media
|
|
4
|
+
// penyimpanan (F-ENV-1), tapi state module-local tetap SATU objek untuk
|
|
5
|
+
// seluruh proses. Dua sesi bersamaan (daemon multi-sesi; TUI sesi A + printer
|
|
6
|
+
// linier sesi B) saling menimpa — minimize tool di A mengubah tampilan B.
|
|
7
|
+
// Buffer /copy + error tertunda punya lifetime sama dengan state render, jadi
|
|
8
|
+
// memisahkannya ke singleton lain hanya menambah sumber kebenaran.
|
|
9
|
+
//
|
|
10
|
+
// Bentuk: SATU objek per sesi, dibuat composition root (`cli/setup.ts`) dan
|
|
11
|
+
// diteruskan lewat opsi eksplisit ke Transcript TUI + printer linier. Modul
|
|
12
|
+
// `render/collapse|detail|reasoning` tetap mengekspor API delegasi ke instance
|
|
13
|
+
// DEFAULT untuk call-site lama/one-shot; permukaan produksi tak memakainya.
|
|
14
|
+
//
|
|
15
|
+
// Env hanya NILAI AWAL (bootstrap), bukan tempat penyimpanan: setter di sini
|
|
16
|
+
// tak pernah menulis `process.env`.
|
|
17
|
+
|
|
18
|
+
import { splitTrailingEscape } from "./render/sanitize.ts"
|
|
19
|
+
|
|
20
|
+
export type CollapseSection = "thinking" | "tool" | "answer"
|
|
21
|
+
|
|
22
|
+
/** Isi section yang dikecilkan pada turn terakhir (untuk /expand). */
|
|
23
|
+
export interface CollapsedSectionView {
|
|
24
|
+
label: string
|
|
25
|
+
text: string
|
|
26
|
+
/** Stream asal konten (kontrak Unix dipertahankan saat /expand mencetak). */
|
|
27
|
+
stream: "stdout" | "stderr"
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Cap per-entry + total untuk buffer section (/expand). */
|
|
31
|
+
const MAX_SECTION_CHARS = 200_000
|
|
32
|
+
const MAX_BUFFER_TOTAL = 500_000
|
|
33
|
+
|
|
34
|
+
/** Cap buffer /copy. */
|
|
35
|
+
export const LAST_TURN_MAX_CHARS = 200_000
|
|
36
|
+
export const MAX_COPY_TURNS = 10
|
|
37
|
+
|
|
38
|
+
export interface UiSessionStateOptions {
|
|
39
|
+
minimizeAnswer?: boolean
|
|
40
|
+
minimizeTool?: boolean
|
|
41
|
+
compact?: boolean
|
|
42
|
+
reasoningVisible?: boolean
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* State milik SATU sesi (atau SATU renderer yang butuh state sendiri).
|
|
47
|
+
*
|
|
48
|
+
* Semua getter membaca field langsung saat dipanggil — jangan menyimpan
|
|
49
|
+
* `state.compact` ke `const` di module scope (nilai beku saat import).
|
|
50
|
+
*/
|
|
51
|
+
export class UiSessionState {
|
|
52
|
+
#activeSection: CollapseSection | null = null
|
|
53
|
+
#minimizeAnswer: boolean
|
|
54
|
+
#minimizeTool: boolean
|
|
55
|
+
#compact: boolean
|
|
56
|
+
#reasoningVisible: boolean
|
|
57
|
+
#views: CollapsedSectionView[] = []
|
|
58
|
+
|
|
59
|
+
// Buffer /copy + error tertunda (logger linier). Berlifecycle per sesi:
|
|
60
|
+
// bertahan lintas re-attach per turn (lihat `cli/setup.ts` attachUI).
|
|
61
|
+
#lastTurnText = ""
|
|
62
|
+
#previousTurnTexts: string[] = []
|
|
63
|
+
#pendingError: string | null = null
|
|
64
|
+
|
|
65
|
+
constructor(opts: UiSessionStateOptions = {}) {
|
|
66
|
+
// Env = NILAI AWAL saja. `??` dipakai supaya option eksplisit menang atas
|
|
67
|
+
// env (bootstrap env tak bisa menimpa keputusan instance).
|
|
68
|
+
this.#minimizeAnswer = opts.minimizeAnswer ?? process.env.MINICODE_MINIMIZE_ANSWER === "1"
|
|
69
|
+
this.#minimizeTool = opts.minimizeTool ?? process.env.MINICODE_MINIMIZE_TOOL === "1"
|
|
70
|
+
this.#compact = opts.compact ?? process.env.MINICODE_COMPACT === "1"
|
|
71
|
+
this.#reasoningVisible = opts.reasoningVisible ?? process.env.MINICODE_SHOW_THINKING === "1"
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ── Collapse ────────────────────────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
/** Section yang sedang aktif — keputusan tombol + / - saat turn berjalan. */
|
|
77
|
+
get activeSection(): CollapseSection | null {
|
|
78
|
+
return this.#activeSection
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
setActiveSection(k: CollapseSection | null): void {
|
|
82
|
+
this.#activeSection = k
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Section sedang dikecilkan? Thinking = invers reasoningVisible. */
|
|
86
|
+
sectionMinimized(section: CollapseSection): boolean {
|
|
87
|
+
if (section === "thinking") return !this.#reasoningVisible
|
|
88
|
+
if (section === "answer") return this.#minimizeAnswer
|
|
89
|
+
return this.#minimizeTool
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Set eksplisit (nilai minimize) atau toggle bila `next` tidak diberikan.
|
|
93
|
+
* Return nilai minimize terbaru. */
|
|
94
|
+
setSectionMinimized(section: CollapseSection, next?: boolean): boolean {
|
|
95
|
+
if (section === "thinking") {
|
|
96
|
+
const vis = this.setReasoningVisible(next === undefined ? undefined : !next)
|
|
97
|
+
return !vis
|
|
98
|
+
}
|
|
99
|
+
const cur = section === "answer" ? this.#minimizeAnswer : this.#minimizeTool
|
|
100
|
+
const nxt = next ?? !cur
|
|
101
|
+
if (section === "answer") this.#minimizeAnswer = nxt
|
|
102
|
+
else this.#minimizeTool = nxt
|
|
103
|
+
return nxt
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Simpan isi section yang dikecilkan (cap per-entry + total). */
|
|
107
|
+
rememberCollapsedSection(
|
|
108
|
+
label: string,
|
|
109
|
+
text: string,
|
|
110
|
+
stream: CollapsedSectionView["stream"] = "stderr",
|
|
111
|
+
): void {
|
|
112
|
+
if (!text) return
|
|
113
|
+
// Cap jangan belah surrogate pair (U+FFFD di /expand) dan jangan sisakan
|
|
114
|
+
// ekor escape parsial (SGR gantung ikut tercetak saat /expand).
|
|
115
|
+
let cut = text.length > MAX_SECTION_CHARS ? text.slice(0, MAX_SECTION_CHARS) : text
|
|
116
|
+
const last = cut.charCodeAt(cut.length - 1)
|
|
117
|
+
if (cut.length > 0 && last >= 0xd800 && last <= 0xdbff) cut = cut.slice(0, -1)
|
|
118
|
+
cut = splitTrailingEscape(cut).head
|
|
119
|
+
this.#views.push({ label, text: cut, stream })
|
|
120
|
+
// Buang yang tertua sampai total dalam budget — terbaru selalu dipertahankan.
|
|
121
|
+
let total = 0
|
|
122
|
+
let keepFrom = this.#views.length
|
|
123
|
+
for (let i = this.#views.length - 1; i >= 0; i--) {
|
|
124
|
+
total += this.#views[i]!.text.length
|
|
125
|
+
if (total > MAX_BUFFER_TOTAL) break
|
|
126
|
+
keepFrom = i
|
|
127
|
+
}
|
|
128
|
+
if (keepFrom > 0) this.#views.splice(0, keepFrom)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
clearCollapsedSections(): void {
|
|
132
|
+
this.#views.length = 0
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Konten section yang dikecilkan pada turn terakhir (untuk /expand). */
|
|
136
|
+
collapsedSectionsSnapshot(): CollapsedSectionView[] {
|
|
137
|
+
return [...this.#views]
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// ── Detail (compact vs expanded) ────────────────────────────────────────
|
|
141
|
+
|
|
142
|
+
get compact(): boolean {
|
|
143
|
+
return this.#compact
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
set compact(v: boolean) {
|
|
147
|
+
this.#compact = v
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Set eksplisit (on/off) atau toggle bila `next` tidak diberikan. */
|
|
151
|
+
setCompactMode(next?: boolean): boolean {
|
|
152
|
+
const nextVal = next ?? !this.#compact
|
|
153
|
+
this.#compact = nextVal
|
|
154
|
+
return nextVal
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// ── Reasoning ───────────────────────────────────────────────────────────
|
|
158
|
+
|
|
159
|
+
get reasoningVisible(): boolean {
|
|
160
|
+
return this.#reasoningVisible
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
set reasoningVisible(v: boolean) {
|
|
164
|
+
this.#reasoningVisible = v
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Set eksplisit (on/off) atau toggle bila `next` tidak diberikan. */
|
|
168
|
+
setReasoningVisible(next?: boolean): boolean {
|
|
169
|
+
const nextVal = next ?? !this.#reasoningVisible
|
|
170
|
+
this.#reasoningVisible = nextVal
|
|
171
|
+
return nextVal
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ── Buffer /copy (logger linier) ────────────────────────────────────────
|
|
175
|
+
|
|
176
|
+
/** Isi output turn terakhir (teks model + hasil tool). */
|
|
177
|
+
getLastTurnText(): string {
|
|
178
|
+
return this.#lastTurnText
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** N turn terakhir, oldest-to-newest. */
|
|
182
|
+
getLastTurnTexts(count = 1): string[] {
|
|
183
|
+
const all = this.#lastTurnText
|
|
184
|
+
? [...this.#previousTurnTexts, this.#lastTurnText]
|
|
185
|
+
: [...this.#previousTurnTexts]
|
|
186
|
+
const n = Number.isFinite(count) ? Math.max(1, Math.floor(count)) : 1
|
|
187
|
+
return all.slice(-n)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Geser turn berjalan ke arsip (dipanggil saat turn baru mulai). */
|
|
191
|
+
archiveTurn(): void {
|
|
192
|
+
if (!this.#lastTurnText) return
|
|
193
|
+
this.#previousTurnTexts.push(this.#lastTurnText)
|
|
194
|
+
if (this.#previousTurnTexts.length > MAX_COPY_TURNS) this.#previousTurnTexts.shift()
|
|
195
|
+
this.#lastTurnText = ""
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Tambah teks ke buffer turn berjalan (dibatasi). */
|
|
199
|
+
appendTurnText(s: string): void {
|
|
200
|
+
if (!s) return
|
|
201
|
+
this.#lastTurnText += s
|
|
202
|
+
if (this.#lastTurnText.length > LAST_TURN_MAX_CHARS)
|
|
203
|
+
this.#lastTurnText = this.#lastTurnText.slice(-LAST_TURN_MAX_CHARS)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// ── Error tertunda (logger linier) ──────────────────────────────────────
|
|
207
|
+
|
|
208
|
+
setPendingError(e: string | null): void {
|
|
209
|
+
this.#pendingError = e
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Ambil + kosongkan error tertunda (consume-once per turn). */
|
|
213
|
+
takePendingError(): string | null {
|
|
214
|
+
const e = this.#pendingError
|
|
215
|
+
this.#pendingError = null
|
|
216
|
+
return e
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Kosongkan buffer /copy + error tertunda.
|
|
221
|
+
*
|
|
222
|
+
* [Area A] Reset ini EKSPLISIT, bukan implisit di `attachSimpleLogger`.
|
|
223
|
+
* Sebelumnya attach (dipanggil tiap turn) wipe buffer — membuat `/copy [n]`
|
|
224
|
+
* untuk n>1 selalu mengembalikan satu turn saja. Memindahkan reset ke sini
|
|
225
|
+
* membuat batasnya jelas: sesi yang memulai sesi baru/uji yang butuh
|
|
226
|
+
* kekaburan memanggil ini; pergantian turn biasa TIDAK.
|
|
227
|
+
*/
|
|
228
|
+
resetCopyBuffer(): void {
|
|
229
|
+
this.#lastTurnText = ""
|
|
230
|
+
this.#previousTurnTexts = []
|
|
231
|
+
this.#pendingError = null
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Instance default — dipakai API modul lama (`render/collapse|detail|reasoning`)
|
|
237
|
+
* dan jalur yang tak punya state sesi (one-shot/ekspor test).
|
|
238
|
+
*
|
|
239
|
+
* PERINGATAN: permukaan produksi yang punya sesi (TUI, printer linier) WAJIB
|
|
240
|
+
* memakai instance miliknya sendiri; memakai default berarti state bersama
|
|
241
|
+
* proses — persis cacat Area A yang ditutup di sini.
|
|
242
|
+
*/
|
|
243
|
+
export const defaultUiState = new UiSessionState()
|
package/src/ui/tui/app.ts
CHANGED
|
@@ -368,12 +368,16 @@ export class TuiApp {
|
|
|
368
368
|
;(this.repaintTimer as unknown as { unref?: () => void }).unref?.()
|
|
369
369
|
} catch {}
|
|
370
370
|
}
|
|
371
|
-
|
|
371
|
+
// [F-TYPE-1] Payload tiap call-site dianotasi; `sub` generik (E diinfer dari
|
|
372
|
+
// handler) menjaga anotasi itu, bukan melunturkannya ke `any`. `UiBus.on`
|
|
373
|
+
// tetap longgar di tepi (dokumentasi src/ui/contract.ts:62-71); satu-satunya
|
|
374
|
+
// cast `event as E` menandai perpindahan unknown → bentuk call-site.
|
|
375
|
+
const sub = <E = unknown>(type: Parameters<UiBus["on"]>[0], handler?: (event: E) => void) => {
|
|
372
376
|
try {
|
|
373
377
|
this.busUnsubs.push(
|
|
374
|
-
this.host.bus.on(type, (event:
|
|
378
|
+
this.host.bus.on(type, (event: unknown) => {
|
|
375
379
|
try {
|
|
376
|
-
handler?.(event)
|
|
380
|
+
handler?.(event as E)
|
|
377
381
|
} finally {
|
|
378
382
|
requestPaint()
|
|
379
383
|
}
|