minicode-ai 0.11.0 → 0.13.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 +98 -0
- package/LICENSE +21 -21
- package/README.md +4 -4
- package/cli/args.ts +46 -0
- package/cli/commands/acp.ts +163 -10
- package/cli/commands/config.ts +33 -18
- package/cli/commands/daemon.ts +515 -0
- package/cli/commands/exec.ts +145 -53
- package/cli/commands/openwork.ts +986 -0
- package/cli/commands/sessions.ts +4 -1
- package/cli/commands/stats.ts +34 -4
- package/cli/commands.ts +217 -2
- package/cli/index.ts +146 -14
- package/cli/recovery-warning.ts +115 -0
- package/cli/router.ts +32 -1
- package/cli/setup.ts +2332 -171
- package/cli/tui.ts +113 -7
- package/docs/OUTPUT_ARCHITECTURE_AUDIT.md +489 -0
- package/docs/OUTPUT_EVENT_MODEL.md +289 -0
- package/docs/OUTPUT_IMPLEMENTATION_PLAN.md +391 -0
- package/docs/OUTPUT_PROTOCOL_SPEC.md +285 -0
- package/docs/OUTPUT_RENDERING_SPEC.md +248 -0
- package/docs/OUTPUT_UX_RULES.md +449 -0
- package/docs/PLAN_UIUX_V6.md +2 -2
- package/docs/README.md +4 -3
- package/docs/TERMINAL_CONTRACT.md +278 -66
- package/docs/architecture.md +89 -7
- package/docs/changelog.md +1 -1
- package/docs/choosing-mode.md +2 -2
- package/docs/cli.md +9 -5
- package/docs/concepts.md +1 -1
- package/docs/contributing.md +9 -2
- package/docs/environment.md +6 -2
- package/docs/exec.md +46 -5
- package/docs/memory-sessions.md +2 -0
- package/docs/policy-sandbox.md +1 -1
- package/docs/pricing-budget.md +1 -1
- package/docs/quickstart.md +2 -2
- package/docs/repl.md +6 -2
- package/docs/security-model.md +1 -1
- package/docs/security.md +1 -1
- package/docs/skills.md +2 -2
- package/docs/terminal.md +9 -5
- package/docs/tools.md +1 -1
- package/package.json +35 -5
- package/src/app/session.ts +261 -15
- package/src/app/tool-layer.ts +94 -7
- package/src/daemon/approvals.ts +361 -0
- package/src/daemon/capability.ts +145 -0
- package/src/daemon/children.ts +142 -0
- package/src/daemon/client.ts +267 -0
- package/src/daemon/control.ts +350 -0
- package/src/daemon/discovery.ts +234 -0
- package/src/daemon/feed.ts +155 -0
- package/src/daemon/framing.ts +94 -0
- package/src/daemon/host.ts +316 -0
- package/src/daemon/protocol.ts +217 -0
- package/src/daemon/resource.ts +170 -0
- package/src/daemon/sanitize.ts +57 -0
- package/src/daemon/server.ts +627 -0
- package/src/daemon/store.ts +317 -0
- package/src/daemon/subscription.ts +270 -0
- package/src/daemon/telemetry.ts +81 -0
- package/src/hooks/run.ts +12 -1
- package/src/lib/atomic-write.ts +29 -3
- package/src/lib/db-path.ts +26 -0
- package/src/lib/keystore.ts +544 -42
- package/src/lib/net.ts +43 -1
- package/src/lib/safe-open.ts +116 -1
- package/src/lib/session-id.ts +15 -0
- package/src/lsp/client.ts +123 -5
- package/src/mcp/client.ts +139 -17
- package/src/mcp/server.ts +177 -50
- package/src/mcp/transport.ts +16 -19
- package/src/memory/vector.ts +770 -55
- package/src/policy/allowlist.ts +14 -2
- package/src/policy/bash-guard.ts +105 -10
- package/src/policy/compaction.ts +82 -5
- package/src/policy/consequence-engine.ts +220 -0
- package/src/policy/context.ts +132 -0
- package/src/policy/executor.ts +187 -121
- package/src/policy/no-progress.ts +559 -0
- package/src/policy/permission.ts +4 -8
- package/src/policy/pricing.ts +17 -3
- package/src/policy/scrub.ts +69 -2
- package/src/policy/tool-contract.ts +515 -0
- package/src/policy/usage.ts +310 -24
- package/src/policy/verifier.ts +6 -3
- package/src/policy/work-mode.ts +182 -0
- package/src/presentation/adapter.ts +533 -26
- package/src/presentation/events.ts +151 -0
- package/src/presentation/model.ts +180 -27
- package/src/presentation/projection.ts +858 -0
- package/src/presentation/reducer.ts +645 -310
- package/src/presentation/store.ts +12 -7
- package/src/providers/anthropic.ts +18 -10
- package/src/providers/responses.ts +17 -7
- package/src/runtime/capability.ts +409 -0
- package/src/runtime/child-execution.ts +274 -0
- package/src/runtime/composition.ts +958 -0
- package/src/runtime/dispatch.ts +591 -0
- package/src/runtime/execution-backend.ts +632 -0
- package/src/runtime/execution-events.ts +272 -0
- package/src/runtime/execution-id.ts +230 -0
- package/src/runtime/execution-journal.ts +926 -0
- package/src/runtime/execution-kernel.ts +456 -0
- package/src/runtime/execution-registry.ts +379 -0
- package/src/runtime/process-supervision.ts +276 -0
- package/src/runtime/production-execution.ts +551 -0
- package/src/runtime/production-runtime.ts +217 -0
- package/src/runtime/recovery-safety.ts +70 -0
- package/src/runtime/recovery.ts +477 -0
- package/src/runtime/runtime-capabilities.ts +179 -0
- package/src/runtime/runtime-diagnostics.ts +141 -0
- package/src/runtime/runtime-host.ts +365 -0
- package/src/runtime/runtime-status-facts.ts +222 -0
- package/src/runtime/runtime-status-provider.ts +74 -0
- package/src/runtime/runtime-status.ts +459 -0
- package/src/runtime/runtime-task-admission.ts +101 -0
- package/src/runtime/supervisor.ts +444 -0
- package/src/session/authority.ts +168 -0
- package/src/session/checkpoint.ts +88 -1
- package/src/session/context-adapter.ts +251 -0
- package/src/session/context-assembly.ts +242 -0
- package/src/session/context-identity.ts +389 -0
- package/src/session/context-projection.ts +248 -0
- package/src/session/context-selector.ts +500 -0
- package/src/session/cross-session-adoption.ts +249 -0
- package/src/session/full-history-fold.ts +171 -0
- package/src/session/identity.ts +192 -0
- package/src/session/journal.ts +805 -102
- package/src/session/open-work-intent.ts +732 -0
- package/src/session/open-work.ts +767 -0
- package/src/session/pending-tools.ts +94 -0
- package/src/session/persistence.ts +3569 -127
- package/src/session/runtime-header.ts +208 -0
- package/src/session/shadow-git.ts +13 -5
- package/src/session/verification.ts +1623 -0
- package/src/task/assignment.ts +136 -0
- package/src/task/autonomous-adapter.ts +168 -0
- package/src/task/autonomous-context.ts +630 -0
- package/src/task/autonomous-policy.ts +490 -0
- package/src/task/continuity.ts +179 -0
- package/src/task/execution-cancellation.ts +165 -0
- package/src/task/graph-validate.ts +379 -0
- package/src/task/graph.ts +305 -0
- package/src/task/identity.ts +230 -0
- package/src/task/model.ts +417 -0
- package/src/task/production-scheduler.ts +293 -0
- package/src/task/readiness.ts +229 -0
- package/src/task/scheduler-observability.ts +387 -0
- package/src/task/scheduler.ts +1197 -0
- package/src/task/scheduling-policy.ts +165 -0
- package/src/task/session-authority.ts +148 -0
- package/src/task/session-ownership.ts +211 -0
- package/src/task/store.ts +2887 -0
- package/src/task/sync.ts +343 -0
- package/src/task/trigger.ts +260 -0
- package/src/tools/bash.ts +377 -211
- package/src/tools/code_run.ts +1 -1
- package/src/tools/delete_file.ts +1 -1
- package/src/tools/edit.ts +33 -6
- package/src/tools/evidence.ts +30 -0
- package/src/tools/git.ts +4 -4
- package/src/tools/glob.ts +12 -3
- package/src/tools/grep.ts +1 -1
- package/src/tools/lsp.ts +4 -4
- package/src/tools/mcp_call.ts +11 -4
- package/src/tools/memory.ts +153 -18
- package/src/tools/move_file.ts +1 -1
- package/src/tools/patch.ts +1 -1
- package/src/tools/read_file.ts +1 -1
- package/src/tools/read_image.ts +1 -1
- package/src/tools/submit_result.ts +1 -1
- package/src/tools/task.ts +437 -41
- package/src/tools/todo.ts +662 -35
- package/src/tools/web_fetch.ts +4 -2
- package/src/tools/web_search.ts +9 -3
- package/src/tools/write_file.ts +1 -1
- package/src/ui/assistant/simple.ts +123 -50
- package/src/ui/assistant/turn-status.ts +17 -3
- package/src/ui/contract.ts +184 -0
- package/src/ui/footer.ts +54 -18
- package/src/ui/i18n/en.ts +62 -6
- package/src/ui/i18n/id.ts +62 -6
- package/src/ui/input/prompt-engine.ts +120 -25
- package/src/ui/render/errors.ts +39 -1
- package/src/ui/render/markdown-table.ts +455 -0
- package/src/ui/render/markdown.ts +22 -1
- package/src/ui/render/table-grid.ts +184 -0
- package/src/ui/render/table.ts +15 -76
- package/src/ui/runtime/screen.ts +7 -2
- package/src/ui/runtime/statusline.ts +25 -6
- package/src/ui/tui/app.ts +598 -133
- package/src/ui/tui/transcript.ts +531 -89
- package/vendor/minicore/VENDOR.md +1 -1
- 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
- package/AGENTS.md +0 -45
- package/PLAN.md +0 -491
- package/docs/ARCHITECTURE.html +0 -1248
- package/docs/CONTROL-PLANE-MAP.md +0 -139
- package/docs/HARNESS.md +0 -150
- package/docs/SUMMARY.md +0 -52
- package/docs/UI_RENDER_PIPELINE.md +0 -165
- package/docs/USAGE.md +0 -11
package/src/ui/tui/app.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
// Batas lapisan: semua yang berbau sesi/model/turn datang via TuiHost yang
|
|
9
9
|
// di-inject dari cli/ (composition root). src/ui TAK BOLEH impor cli/.
|
|
10
10
|
import type { UiBus, UiPresentationActivity } from "../contract.ts"
|
|
11
|
-
import { type FooterStatus, renderFooter } from "../footer.ts"
|
|
11
|
+
import { type FooterStatus, renderFooter, type TimerHighlight } from "../footer.ts"
|
|
12
12
|
import { t } from "../i18n/locale.ts"
|
|
13
13
|
import {
|
|
14
14
|
applyKey,
|
|
@@ -22,18 +22,19 @@ import {
|
|
|
22
22
|
type PromptState,
|
|
23
23
|
toGraphemes,
|
|
24
24
|
} from "../input/prompt-engine.ts"
|
|
25
|
-
import {
|
|
25
|
+
import { formatError } from "../render/errors.ts"
|
|
26
|
+
import { sanitizeAnsi, sanitizeAnsiLine, stripSgr } from "../render/sanitize.ts"
|
|
26
27
|
import { c } from "../render/theme.ts"
|
|
27
|
-
import { chunkByWidth, displayWidth, truncateToWidth } from "../render/width.ts"
|
|
28
|
-
import { motionReduced } from "../runtime/motion.ts"
|
|
29
28
|
import {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
} from "../
|
|
36
|
-
import
|
|
29
|
+
charWidth,
|
|
30
|
+
chunkByWidth,
|
|
31
|
+
displayWidth,
|
|
32
|
+
escapeLength,
|
|
33
|
+
truncateToWidth,
|
|
34
|
+
} from "../render/width.ts"
|
|
35
|
+
import { motionReduced } from "../runtime/motion.ts"
|
|
36
|
+
import { type AltScreen, forceRestoreScreenForSignal, openAltScreen } from "../runtime/screen.ts"
|
|
37
|
+
import type { Transcript, TranscriptPoint, TranscriptRow } from "./transcript.ts"
|
|
37
38
|
|
|
38
39
|
export interface TuiStatusSnapshot {
|
|
39
40
|
footer: FooterStatus
|
|
@@ -53,8 +54,9 @@ export interface TuiHost {
|
|
|
53
54
|
* sebagai baris error (tak ada jalur diam).
|
|
54
55
|
*/
|
|
55
56
|
submit(text: string): Promise<{ quit?: boolean } | undefined>
|
|
56
|
-
/** Batalkan turn yang berjalan (Esc
|
|
57
|
+
/** Batalkan turn yang berjalan (Esc saat busy). */
|
|
57
58
|
abort(): void
|
|
59
|
+
copySelection(text: string): boolean
|
|
58
60
|
/** Putar mode permission (Tab / Shift+Tab). */
|
|
59
61
|
cycleMode(dir: 1 | -1): void
|
|
60
62
|
/** Toggle compact / reasoning (Ctrl+O / Ctrl+T). */
|
|
@@ -77,6 +79,56 @@ const MIN_COLS = 20
|
|
|
77
79
|
|
|
78
80
|
const PROMPT_FIRST = "minicode › "
|
|
79
81
|
const PROMPT_CONT = " · "
|
|
82
|
+
const TIMER_TEXT = "00.00.00"
|
|
83
|
+
const DOT_INTERVAL_MS = 200
|
|
84
|
+
const DOT_PHASES = ["...", "..", ".", ".."] as const
|
|
85
|
+
const MOUSE_DRAG_ON = "\x1b[?1002h\x1b[?1006h"
|
|
86
|
+
const MOUSE_WHEEL_ON = "\x1b[?1000h\x1b[?1006h"
|
|
87
|
+
const MOUSE_OFF = "\x1b[?1006l\x1b[?1002l\x1b[?1000l"
|
|
88
|
+
const MAX_SELECTION_CHARS = 200_000
|
|
89
|
+
type MouseMode = "off" | "wheel" | "drag"
|
|
90
|
+
|
|
91
|
+
function mouseSelectionEnabled(): boolean {
|
|
92
|
+
const value = process.env.MINICODE_MOUSE_SELECTION?.toLowerCase()
|
|
93
|
+
return value !== "0" && value !== "false" && value !== "off" && value !== "no"
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function thinkingDots(now = Date.now(), startedAt = now, reduced = motionReduced()): string {
|
|
97
|
+
if (reduced) return "..."
|
|
98
|
+
const elapsed = Math.max(0, now - startedAt)
|
|
99
|
+
return DOT_PHASES[Math.floor(elapsed / DOT_INTERVAL_MS) % DOT_PHASES.length]!
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function formatTimer(ms: number): string {
|
|
103
|
+
const seconds = Math.max(0, Math.floor(ms / 1000))
|
|
104
|
+
const hours = Math.floor(seconds / 3600)
|
|
105
|
+
const minutes = Math.floor((seconds % 3600) / 60)
|
|
106
|
+
const rest = seconds % 60
|
|
107
|
+
return `${String(hours).padStart(2, "0")}.${String(minutes).padStart(2, "0")}.${String(rest).padStart(2, "0")}`
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function timerHighlight(ms: number): TimerHighlight {
|
|
111
|
+
const seconds = Math.max(0, Math.floor(ms / 1000))
|
|
112
|
+
if (seconds >= 3600) return "hours"
|
|
113
|
+
if (seconds >= 60) return "minutes"
|
|
114
|
+
return "seconds"
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
interface FrameLayout {
|
|
118
|
+
frame: string[]
|
|
119
|
+
cursor: { row: number; col: number }
|
|
120
|
+
transcriptRows: TranscriptRow[]
|
|
121
|
+
transcriptTop: number
|
|
122
|
+
composerTop: number
|
|
123
|
+
composerLines: string[]
|
|
124
|
+
composerOffset: number
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
interface SelectionState {
|
|
128
|
+
anchor: TranscriptPoint
|
|
129
|
+
focus: TranscriptPoint
|
|
130
|
+
dragging: boolean
|
|
131
|
+
}
|
|
80
132
|
|
|
81
133
|
interface RawStdin {
|
|
82
134
|
setRawMode(v: boolean): void
|
|
@@ -87,15 +139,67 @@ interface RawStdin {
|
|
|
87
139
|
isTTY?: boolean
|
|
88
140
|
}
|
|
89
141
|
|
|
142
|
+
function highlightColumns(text: string, start: number, end: number): string {
|
|
143
|
+
if (end <= start) return text
|
|
144
|
+
let out = ""
|
|
145
|
+
let column = 0
|
|
146
|
+
let active = false
|
|
147
|
+
let i = 0
|
|
148
|
+
while (i < text.length) {
|
|
149
|
+
if (text[i] === "\x1b") {
|
|
150
|
+
const length = escapeLength(text, i)
|
|
151
|
+
if (length > 0) {
|
|
152
|
+
out += text.slice(i, i + length)
|
|
153
|
+
if (active) out += "\x1b[7m"
|
|
154
|
+
i += length
|
|
155
|
+
continue
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const codePoint = text.codePointAt(i)!
|
|
159
|
+
const char = String.fromCodePoint(codePoint)
|
|
160
|
+
const width = charWidth(codePoint)
|
|
161
|
+
const next = column + width
|
|
162
|
+
const inside = column < end && next > start
|
|
163
|
+
if (inside && !active) {
|
|
164
|
+
out += "\x1b[7m"
|
|
165
|
+
active = true
|
|
166
|
+
} else if (!inside && active) {
|
|
167
|
+
out += "\x1b[27m"
|
|
168
|
+
active = false
|
|
169
|
+
}
|
|
170
|
+
out += char
|
|
171
|
+
column = next
|
|
172
|
+
i += char.length
|
|
173
|
+
}
|
|
174
|
+
if (active) out += "\x1b[27m"
|
|
175
|
+
return out
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function graphemeOffsetAtColumn(text: string, column: number): number {
|
|
179
|
+
const target = Math.max(0, column)
|
|
180
|
+
let used = 0
|
|
181
|
+
const units = toGraphemes(text)
|
|
182
|
+
for (let i = 0; i < units.length; i++) {
|
|
183
|
+
const width = displayWidth(units[i]!)
|
|
184
|
+
if (target <= used + width / 2) return i
|
|
185
|
+
used += width
|
|
186
|
+
if (target <= used) return i + 1
|
|
187
|
+
}
|
|
188
|
+
return units.length
|
|
189
|
+
}
|
|
190
|
+
|
|
90
191
|
export class TuiApp {
|
|
91
192
|
private state: PromptState = createState()
|
|
92
193
|
private scrollBack = 0
|
|
93
194
|
private history: string[] = []
|
|
94
195
|
private histIdx = -1
|
|
95
196
|
private draft = ""
|
|
197
|
+
private editing = true
|
|
96
198
|
private busy = false
|
|
97
|
-
private
|
|
98
|
-
|
|
199
|
+
private thinkingActive = false
|
|
200
|
+
private turnStartedAt = 0
|
|
201
|
+
private activityTimer: ReturnType<typeof setTimeout> | undefined
|
|
202
|
+
/** Timestamp abort terakhir (ms) — deteksi double-tap Esc = quit. */
|
|
99
203
|
private lastAbortAt = 0
|
|
100
204
|
/** Hint "tekan lagi untuk keluar" tampil sekali setelah abort pertama. */
|
|
101
205
|
private abortHintOn = false
|
|
@@ -113,6 +217,7 @@ export class TuiApp {
|
|
|
113
217
|
*/
|
|
114
218
|
private onSignalTerm: (() => void) | null = null
|
|
115
219
|
private onSignalHup: (() => void) | null = null
|
|
220
|
+
private onSignalInt: (() => void) | null = null
|
|
116
221
|
|
|
117
222
|
private wasRaw = false
|
|
118
223
|
/**
|
|
@@ -123,6 +228,10 @@ export class TuiApp {
|
|
|
123
228
|
private stdin: RawStdin | null = null
|
|
124
229
|
/** True setelah releaseTerminal (spawn anak): paint/repaint ditahan. */
|
|
125
230
|
private released = false
|
|
231
|
+
private mouseMode: MouseMode = "off"
|
|
232
|
+
private selection: SelectionState | null = null
|
|
233
|
+
private lastLayout: FrameLayout | null = null
|
|
234
|
+
private onExit: (() => void) | null = null
|
|
126
235
|
/**
|
|
127
236
|
* Langganan bus untuk repaint live (streaming jawaban, ledger tool,
|
|
128
237
|
* thinking): TANPA ini layar buta selama turn — teks baru hanya tampil
|
|
@@ -214,6 +323,7 @@ export class TuiApp {
|
|
|
214
323
|
try {
|
|
215
324
|
stdin.removeListener("data", this.onData!)
|
|
216
325
|
} catch {}
|
|
326
|
+
this.setMouseMode("off")
|
|
217
327
|
forceRestoreScreenForSignal()
|
|
218
328
|
// exit() eksplisit memicu handler "exit" yang tersisa (spinner,
|
|
219
329
|
// turn-status) — best-effort sinkron. 128+n = konvensi "mati oleh
|
|
@@ -223,13 +333,28 @@ export class TuiApp {
|
|
|
223
333
|
}
|
|
224
334
|
this.onSignalTerm = () => restoreAndExit(143)
|
|
225
335
|
this.onSignalHup = () => restoreAndExit(129)
|
|
336
|
+
this.onSignalInt = () => this.requestAbort()
|
|
226
337
|
try {
|
|
227
338
|
process.on("SIGTERM", this.onSignalTerm)
|
|
228
|
-
process.on("SIGHUP", this.onSignalHup)
|
|
229
339
|
} catch {
|
|
230
340
|
this.onSignalTerm = null
|
|
341
|
+
}
|
|
342
|
+
try {
|
|
343
|
+
process.on("SIGHUP", this.onSignalHup)
|
|
344
|
+
} catch {
|
|
231
345
|
this.onSignalHup = null
|
|
232
346
|
}
|
|
347
|
+
try {
|
|
348
|
+
process.on("SIGINT", this.onSignalInt)
|
|
349
|
+
} catch {
|
|
350
|
+
this.onSignalInt = null
|
|
351
|
+
}
|
|
352
|
+
this.onExit = () => this.setMouseMode("off")
|
|
353
|
+
try {
|
|
354
|
+
process.on("exit", this.onExit)
|
|
355
|
+
} catch {
|
|
356
|
+
this.onExit = null
|
|
357
|
+
}
|
|
233
358
|
// Repaint live mengikuti event bus (stream teks, ledger, thinking).
|
|
234
359
|
// Suspend menahan repaint (popup melukis sendiri); quit menahan semua.
|
|
235
360
|
const requestPaint = () => {
|
|
@@ -243,20 +368,43 @@ export class TuiApp {
|
|
|
243
368
|
;(this.repaintTimer as unknown as { unref?: () => void }).unref?.()
|
|
244
369
|
} catch {}
|
|
245
370
|
}
|
|
246
|
-
const sub = (type: Parameters<UiBus["on"]>[0]) => {
|
|
371
|
+
const sub = (type: Parameters<UiBus["on"]>[0], handler?: (event: any) => void) => {
|
|
247
372
|
try {
|
|
248
|
-
this.busUnsubs.push(
|
|
373
|
+
this.busUnsubs.push(
|
|
374
|
+
this.host.bus.on(type, (event: any) => {
|
|
375
|
+
try {
|
|
376
|
+
handler?.(event)
|
|
377
|
+
} finally {
|
|
378
|
+
requestPaint()
|
|
379
|
+
}
|
|
380
|
+
}),
|
|
381
|
+
)
|
|
249
382
|
} catch {}
|
|
250
383
|
}
|
|
251
|
-
sub("
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
sub("
|
|
384
|
+
sub("turn:started", () => {
|
|
385
|
+
this.thinkingActive = false
|
|
386
|
+
this.turnStartedAt = Date.now()
|
|
387
|
+
this.startActivityClock()
|
|
388
|
+
})
|
|
389
|
+
sub("provider:extension", (event: { kind?: string }) => {
|
|
390
|
+
if (event?.kind === "reasoning") this.thinkingActive = true
|
|
391
|
+
})
|
|
392
|
+
sub("provider:text", () => {
|
|
393
|
+
this.thinkingActive = false
|
|
394
|
+
})
|
|
395
|
+
sub("execution:started", () => {
|
|
396
|
+
this.thinkingActive = false
|
|
397
|
+
})
|
|
398
|
+
sub("execution:completed", () => {
|
|
399
|
+
this.thinkingActive = false
|
|
400
|
+
})
|
|
401
|
+
sub("turn:completed", () => {
|
|
402
|
+
this.thinkingActive = false
|
|
403
|
+
})
|
|
257
404
|
sub("context:compacted")
|
|
258
405
|
this.tailSize = this.transcript.total()
|
|
259
406
|
this.scrollBase = this.tailSize
|
|
407
|
+
this.setMouseMode(mouseSelectionEnabled() ? "drag" : "off")
|
|
260
408
|
this.paint(screen)
|
|
261
409
|
await done
|
|
262
410
|
// Cleanup: urutan penting — lepas listener dulu agar byte liar pasca-quit
|
|
@@ -267,6 +415,7 @@ export class TuiApp {
|
|
|
267
415
|
clearTimeout(this.repaintTimer)
|
|
268
416
|
this.repaintTimer = undefined
|
|
269
417
|
}
|
|
418
|
+
this.stopActivityClock()
|
|
270
419
|
for (const u of this.busUnsubs) {
|
|
271
420
|
try {
|
|
272
421
|
u()
|
|
@@ -287,6 +436,18 @@ export class TuiApp {
|
|
|
287
436
|
} catch {}
|
|
288
437
|
this.onSignalHup = null
|
|
289
438
|
}
|
|
439
|
+
if (this.onSignalInt) {
|
|
440
|
+
try {
|
|
441
|
+
process.off("SIGINT", this.onSignalInt)
|
|
442
|
+
} catch {}
|
|
443
|
+
this.onSignalInt = null
|
|
444
|
+
}
|
|
445
|
+
if (this.onExit) {
|
|
446
|
+
try {
|
|
447
|
+
process.off("exit", this.onExit)
|
|
448
|
+
} catch {}
|
|
449
|
+
this.onExit = null
|
|
450
|
+
}
|
|
290
451
|
this.stdin = null
|
|
291
452
|
this.pump.dispose()
|
|
292
453
|
this.pump = null
|
|
@@ -300,6 +461,7 @@ export class TuiApp {
|
|
|
300
461
|
// Tutup layar AKTIF (bisa diganti reacquireTerminal setelah spawn
|
|
301
462
|
// gagal — menutup handle run() yang basi akan meninggalkan alt-screen
|
|
302
463
|
// yatim; close() idempoten sehingga aman dua kali).
|
|
464
|
+
this.setMouseMode("off")
|
|
303
465
|
this.currentScreen?.close()
|
|
304
466
|
} catch {}
|
|
305
467
|
this.currentScreen = null
|
|
@@ -308,6 +470,22 @@ export class TuiApp {
|
|
|
308
470
|
|
|
309
471
|
// ── Penanganan key ──
|
|
310
472
|
|
|
473
|
+
private requestAbort(): void {
|
|
474
|
+
if (!this.isBusy()) return
|
|
475
|
+
try {
|
|
476
|
+
this.host.abort()
|
|
477
|
+
} catch {}
|
|
478
|
+
const now = Date.now()
|
|
479
|
+
const doubleTap = now - this.lastAbortAt < 1500
|
|
480
|
+
if (!doubleTap) {
|
|
481
|
+
this.abortHintOn = true
|
|
482
|
+
this.startActivityClock()
|
|
483
|
+
}
|
|
484
|
+
this.lastAbortAt = now
|
|
485
|
+
if (doubleTap) this.quit()
|
|
486
|
+
else this.paintCurrent()
|
|
487
|
+
}
|
|
488
|
+
|
|
311
489
|
private handleKeys(keys: { key: PromptKey }[]): boolean {
|
|
312
490
|
// Garda suspend: timer flush lone-ESC yang dipersenjatai SEBELUM suspend
|
|
313
491
|
// tetap menembak setelah popup terbuka (listener dilepas, timer tidak).
|
|
@@ -322,6 +500,14 @@ export class TuiApp {
|
|
|
322
500
|
}
|
|
323
501
|
|
|
324
502
|
private handleKey(key: PromptKey): void {
|
|
503
|
+
if (key.type === "mouse") {
|
|
504
|
+
this.handleMouse(key)
|
|
505
|
+
return
|
|
506
|
+
}
|
|
507
|
+
if (key.type === "wheelup" || key.type === "wheeldown") {
|
|
508
|
+
this.scrollBy(key.type === "wheelup" ? 0.5 : -0.5)
|
|
509
|
+
return
|
|
510
|
+
}
|
|
325
511
|
// Scroll SELALU tersedia (menu buka/tutup, busy/idle, layar menciut) —
|
|
326
512
|
// di-intercept sebelum engine supaya tak jadi histori maupun teks.
|
|
327
513
|
// Dulu di bawah gate tooSmall: terminal menciut + turn busy = tak bisa
|
|
@@ -354,36 +540,34 @@ export class TuiApp {
|
|
|
354
540
|
this.applyEngine(key)
|
|
355
541
|
return
|
|
356
542
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
//
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
this.lastAbortAt = now
|
|
375
|
-
if (doubleTap) this.quit()
|
|
376
|
-
else this.paintCurrent()
|
|
543
|
+
if (key.type === "ctrl-c") {
|
|
544
|
+
if (this.selection && !this.selection.dragging) this.copySelection()
|
|
545
|
+
return
|
|
546
|
+
}
|
|
547
|
+
// Abort/batal: Esc — berlaku juga saat layar menciut (jalan keluar saat
|
|
548
|
+
// terminal mengecil di tengah turn busy). Jalan keluar saat abort macet
|
|
549
|
+
// (provider/tool non-kooperatif abaikan sinyal): Esc dua kali dalam 1.5 dtk,
|
|
550
|
+
// atau Ctrl+D baris-kosong, = abort + quit. Tanpa ini sesi hanya bisa
|
|
551
|
+
// dibunuh kill -9 (kontrak I14).
|
|
552
|
+
if (key.type === "esc" && this.selection) {
|
|
553
|
+
this.selection = null
|
|
554
|
+
this.paintCurrent()
|
|
555
|
+
return
|
|
556
|
+
}
|
|
557
|
+
if (key.type === "esc") {
|
|
558
|
+
if (this.isBusy()) {
|
|
559
|
+
this.requestAbort()
|
|
377
560
|
return
|
|
378
561
|
}
|
|
379
562
|
if (this.tooSmall()) return
|
|
380
|
-
if (
|
|
563
|
+
if (this.state.menuOpen) {
|
|
381
564
|
this.applyEngine(key)
|
|
382
565
|
return
|
|
383
566
|
}
|
|
384
|
-
if (this.state.line) {
|
|
567
|
+
if (this.state.line || this.editing) {
|
|
385
568
|
this.state = createState()
|
|
386
569
|
this.histIdx = -1
|
|
570
|
+
this.editing = false
|
|
387
571
|
this.paintCurrent()
|
|
388
572
|
}
|
|
389
573
|
return
|
|
@@ -395,10 +579,10 @@ export class TuiApp {
|
|
|
395
579
|
if (key.type === "ctrl-d") this.quit()
|
|
396
580
|
return
|
|
397
581
|
}
|
|
398
|
-
// Saat turn berjalan SEMUA input dibekukan kecuali abort (Esc
|
|
399
|
-
//
|
|
582
|
+
// Saat turn berjalan SEMUA input dibekukan kecuali abort (Esc), scroll
|
|
583
|
+
// (mouse wheel/PgUp/PgDn di atas), dan Ctrl+D baris-kosong (= abort + quit,
|
|
400
584
|
// jalan keluar saat abort macet — kontrak I14).
|
|
401
|
-
if (this.
|
|
585
|
+
if (this.isBusy()) {
|
|
402
586
|
if (key.type === "ctrl-d" && !this.state.line) {
|
|
403
587
|
try {
|
|
404
588
|
this.host.abort()
|
|
@@ -470,6 +654,7 @@ export class TuiApp {
|
|
|
470
654
|
}
|
|
471
655
|
|
|
472
656
|
private applyEngine(key: PromptKey): void {
|
|
657
|
+
if (["char", "backspace", "delete", "enter", "ctrl-j"].includes(key.type)) this.selection = null
|
|
473
658
|
const hints = (line: string) => (line.startsWith("/") ? this.host.listCommands(line) : [])
|
|
474
659
|
const { state, action } = applyKey(this.state, key, hints)
|
|
475
660
|
this.state = state
|
|
@@ -478,6 +663,7 @@ export class TuiApp {
|
|
|
478
663
|
} else if (action === "cancel") {
|
|
479
664
|
// Engine cancel (ctrl-c/ctrl-d) sudah ditangani di atas; tak terjangkau.
|
|
480
665
|
} else if (action === "render" || action === "none") {
|
|
666
|
+
if (action === "render" && key.type === "char") this.editing = true
|
|
481
667
|
// Ketikan baru = kembali ke ekor (I18); stream turn TIDAK me-reset
|
|
482
668
|
// (user yang sedang membaca ke atas tak dirampas).
|
|
483
669
|
if (key.type === "char" || key.type === "backspace" || key.type === "delete") {
|
|
@@ -491,6 +677,7 @@ export class TuiApp {
|
|
|
491
677
|
const line = rawLine.trim()
|
|
492
678
|
this.state = createState()
|
|
493
679
|
this.histIdx = -1
|
|
680
|
+
this.editing = false
|
|
494
681
|
this.followTail()
|
|
495
682
|
this.abortHintOn = false
|
|
496
683
|
if (!line) {
|
|
@@ -500,21 +687,35 @@ export class TuiApp {
|
|
|
500
687
|
this.rememberHistory(line)
|
|
501
688
|
this.transcript.pushUser(rawLine)
|
|
502
689
|
this.busy = true
|
|
690
|
+
this.thinkingActive = false
|
|
691
|
+
this.turnStartedAt = Date.now()
|
|
503
692
|
this.lastAbortAt = 0
|
|
693
|
+
this.startActivityClock()
|
|
504
694
|
this.paintCurrent()
|
|
505
695
|
try {
|
|
506
696
|
const res = await this.host.submit(line)
|
|
507
697
|
if (res?.quit) this.quit()
|
|
508
698
|
} catch (e) {
|
|
509
|
-
|
|
699
|
+
// [P0 UI Security] Pesan error = TEKS TAK DIPERCAYA: bisa memuat body
|
|
700
|
+
// provider, header `Authorization: Bearer …`, atau escape ANSI. Jalur
|
|
701
|
+
// lama `e.message` mentah membocorkan credential ke layar TUI — kini
|
|
702
|
+
// lewat formatter kanonik (redaksi credential + sanitasi + pemotongan).
|
|
703
|
+
this.transcript.pushError(formatError(e))
|
|
510
704
|
} finally {
|
|
511
705
|
this.busy = false
|
|
706
|
+
this.editing = true
|
|
707
|
+
this.turnStartedAt = 0
|
|
708
|
+
this.stopActivityClock()
|
|
512
709
|
if (!this.quitRequested) this.paintCurrent()
|
|
513
710
|
}
|
|
514
711
|
}
|
|
515
712
|
|
|
516
713
|
private quit(): void {
|
|
517
714
|
if (this.quitRequested) return
|
|
715
|
+
try {
|
|
716
|
+
this.host.abort()
|
|
717
|
+
} catch {}
|
|
718
|
+
this.stopActivityClock()
|
|
518
719
|
this.quitRequested = true
|
|
519
720
|
this.finish?.()
|
|
520
721
|
}
|
|
@@ -535,6 +736,9 @@ export class TuiApp {
|
|
|
535
736
|
suspend(): void {
|
|
536
737
|
if (!this.currentScreen || this.quitRequested) return
|
|
537
738
|
if (this.suspended++ > 0) return
|
|
739
|
+
if (this.selection) this.selection.dragging = false
|
|
740
|
+
this.decoder.pending = []
|
|
741
|
+
this.setMouseMode("wheel")
|
|
538
742
|
try {
|
|
539
743
|
if (this.onData) this.stdin?.removeListener("data", this.onData)
|
|
540
744
|
} catch {}
|
|
@@ -556,6 +760,8 @@ export class TuiApp {
|
|
|
556
760
|
try {
|
|
557
761
|
if (this.onData) this.stdin?.on("data", this.onData)
|
|
558
762
|
} catch {}
|
|
763
|
+
this.setMouseMode(mouseSelectionEnabled() ? "drag" : "off")
|
|
764
|
+
if (this.isBusy()) this.startActivityClock()
|
|
559
765
|
this.paintCurrent()
|
|
560
766
|
}
|
|
561
767
|
|
|
@@ -567,6 +773,7 @@ export class TuiApp {
|
|
|
567
773
|
* setelah anak keluar.
|
|
568
774
|
*/
|
|
569
775
|
releaseTerminal(): void {
|
|
776
|
+
this.stopActivityClock()
|
|
570
777
|
try {
|
|
571
778
|
if (this.onData) this.stdin?.removeListener("data", this.onData)
|
|
572
779
|
} catch {}
|
|
@@ -574,9 +781,11 @@ export class TuiApp {
|
|
|
574
781
|
this.stdin?.setRawMode(false)
|
|
575
782
|
} catch {}
|
|
576
783
|
try {
|
|
784
|
+
this.setMouseMode("off")
|
|
577
785
|
this.currentScreen?.close()
|
|
578
786
|
} catch {}
|
|
579
787
|
this.currentScreen = null
|
|
788
|
+
this.lastLayout = null
|
|
580
789
|
this.released = true
|
|
581
790
|
}
|
|
582
791
|
|
|
@@ -593,9 +802,11 @@ export class TuiApp {
|
|
|
593
802
|
if (!screen.ok) return false
|
|
594
803
|
this.currentScreen = screen
|
|
595
804
|
this.released = false
|
|
805
|
+
this.setMouseMode(!mouseSelectionEnabled() ? "off" : this.suspended > 0 ? "wheel" : "drag")
|
|
596
806
|
try {
|
|
597
807
|
this.stdin?.setRawMode(true)
|
|
598
808
|
} catch {}
|
|
809
|
+
if (this.isBusy()) this.startActivityClock()
|
|
599
810
|
this.paintCurrent()
|
|
600
811
|
return true
|
|
601
812
|
}
|
|
@@ -627,21 +838,24 @@ export class TuiApp {
|
|
|
627
838
|
const cols = screen.cols
|
|
628
839
|
const rows = screen.rows
|
|
629
840
|
const snap = this.host.getStatus()
|
|
630
|
-
const
|
|
631
|
-
|
|
632
|
-
const
|
|
633
|
-
const inputH = Math.min(MAX_INPUT_ROWS, Math.max(1, inputAll.length))
|
|
634
|
-
const viewH = Math.max(1, rows - 1 - inputH - menu.length)
|
|
635
|
-
const body = this.transcript.view(cols, viewH, this.scrollBack)
|
|
636
|
-
const frame: string[] = [...body, ...menu, ...inputAll.slice(-inputH), statusLine]
|
|
637
|
-
while (frame.length < rows) frame.unshift("")
|
|
638
|
-
const dimmed = frame.slice(-rows).map((ln) => TuiApp.applyBackdropDim(ln))
|
|
841
|
+
const layout = this.buildLayout(rows, cols, snap)
|
|
842
|
+
this.lastLayout = layout
|
|
843
|
+
const dimmed = layout.frame.map((ln) => TuiApp.applyBackdropDim(ln))
|
|
639
844
|
try {
|
|
640
845
|
screen.paint(dimmed)
|
|
641
846
|
} catch {}
|
|
642
847
|
this.frames++
|
|
643
848
|
}
|
|
644
849
|
|
|
850
|
+
private isBusy(): boolean {
|
|
851
|
+
if (this.busy) return true
|
|
852
|
+
try {
|
|
853
|
+
return this.host.getStatus().busy
|
|
854
|
+
} catch {
|
|
855
|
+
return false
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
|
|
645
859
|
// ── Histori prompt (memori sesi; tak persist ke berkas REPL) ──
|
|
646
860
|
|
|
647
861
|
private rememberHistory(line: string): void {
|
|
@@ -691,6 +905,7 @@ export class TuiApp {
|
|
|
691
905
|
if (r.action !== "render" && r.action !== "none") break
|
|
692
906
|
}
|
|
693
907
|
this.state = s
|
|
908
|
+
this.editing = true
|
|
694
909
|
this.paintCurrent()
|
|
695
910
|
}
|
|
696
911
|
|
|
@@ -752,10 +967,76 @@ export class TuiApp {
|
|
|
752
967
|
this.paintCurrent()
|
|
753
968
|
}
|
|
754
969
|
|
|
970
|
+
private startActivityClock(): void {
|
|
971
|
+
if (this.activityTimer || this.quitRequested) return
|
|
972
|
+
const tick = () => {
|
|
973
|
+
this.activityTimer = undefined
|
|
974
|
+
if (!this.isBusy() || this.quitRequested) return
|
|
975
|
+
if (this.suspended === 0 && !this.released) this.paintCurrent()
|
|
976
|
+
this.startActivityClock()
|
|
977
|
+
}
|
|
978
|
+
this.activityTimer = setTimeout(tick, DOT_INTERVAL_MS)
|
|
979
|
+
try {
|
|
980
|
+
;(this.activityTimer as unknown as { unref?: () => void }).unref?.()
|
|
981
|
+
} catch {}
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
private stopActivityClock(): void {
|
|
985
|
+
if (this.activityTimer !== undefined) {
|
|
986
|
+
clearTimeout(this.activityTimer)
|
|
987
|
+
this.activityTimer = undefined
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
private activityStatus(snap: TuiStatusSnapshot): string {
|
|
992
|
+
const activity = snap.pinnedActivity
|
|
993
|
+
if (!activity) return ""
|
|
994
|
+
const name = sanitizeAnsiLine(String(activity.name ?? ""))
|
|
995
|
+
const target = activity.target ? ` ${sanitizeAnsiLine(String(activity.target))}` : ""
|
|
996
|
+
return sanitizeAnsiLine(`${name}${target}`)
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
private activityRow(content: string, cols: number): string {
|
|
1000
|
+
const width = Math.max(1, Math.floor(cols) - 1)
|
|
1001
|
+
return truncateToWidth(sanitizeAnsiLine(content), width, "")
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
private composerRows(
|
|
1005
|
+
cols: number,
|
|
1006
|
+
snap: TuiStatusSnapshot,
|
|
1007
|
+
busy: boolean,
|
|
1008
|
+
now: number,
|
|
1009
|
+
): string[] {
|
|
1010
|
+
if (busy) {
|
|
1011
|
+
const start = this.turnStartedAt || now
|
|
1012
|
+
const status = this.activityStatus(snap)
|
|
1013
|
+
const content = this.thinkingActive ? thinkingDots(now, start) : status
|
|
1014
|
+
return [this.activityRow(content, cols)]
|
|
1015
|
+
}
|
|
1016
|
+
if (!this.editing) return [""]
|
|
1017
|
+
return this.inputRows(cols)
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
private composerHeight(cols: number, busy = this.busy): number {
|
|
1021
|
+
if (busy || !this.editing) return 1
|
|
1022
|
+
return Math.min(MAX_INPUT_ROWS, Math.max(1, this.inputRows(cols).length))
|
|
1023
|
+
}
|
|
1024
|
+
|
|
755
1025
|
// ── Paint ──
|
|
756
1026
|
|
|
757
1027
|
private currentScreen: AltScreen | null = null
|
|
758
1028
|
|
|
1029
|
+
private setMouseMode(mode: MouseMode): void {
|
|
1030
|
+
if (this.mouseMode === mode) return
|
|
1031
|
+
const sequence = mode === "drag" ? MOUSE_DRAG_ON : mode === "wheel" ? MOUSE_WHEEL_ON : MOUSE_OFF
|
|
1032
|
+
try {
|
|
1033
|
+
if (mode === "off" || this.mouseMode === "off") process.stdout.write(sequence)
|
|
1034
|
+
else if (mode === "drag") process.stdout.write(`\x1b[?1000l${sequence}`)
|
|
1035
|
+
else process.stdout.write(`\x1b[?1002l${sequence}`)
|
|
1036
|
+
this.mouseMode = mode
|
|
1037
|
+
} catch {}
|
|
1038
|
+
}
|
|
1039
|
+
|
|
759
1040
|
private paintCurrent(): void {
|
|
760
1041
|
if (this.currentScreen) this.paint(this.currentScreen)
|
|
761
1042
|
}
|
|
@@ -770,19 +1051,18 @@ export class TuiApp {
|
|
|
770
1051
|
}
|
|
771
1052
|
|
|
772
1053
|
private viewportHeight(rows: number, cols: number): number {
|
|
773
|
-
const
|
|
774
|
-
const
|
|
775
|
-
|
|
1054
|
+
const busy = this.isBusy()
|
|
1055
|
+
const inputH = this.composerHeight(cols, busy)
|
|
1056
|
+
const menuH = this.menuHeight(rows, cols, busy)
|
|
1057
|
+
return Math.max(1, rows - 2 - inputH - menuH)
|
|
776
1058
|
}
|
|
777
1059
|
|
|
778
1060
|
private inputRows(cols: number): string[] {
|
|
1061
|
+
if (!this.editing) return []
|
|
779
1062
|
const w = Math.max(10, cols)
|
|
780
1063
|
const out: string[] = []
|
|
781
|
-
// Baris kosong = placeholder redup (edukasi tanpa banjir): cara keluar
|
|
782
|
-
// selalu terlihat (Ctrl+D), tanpa menuh-menuhi transkrip.
|
|
783
1064
|
if (!this.state.line) {
|
|
784
|
-
|
|
785
|
-
out.push(`${PROMPT_FIRST}${hint}`)
|
|
1065
|
+
out.push(PROMPT_FIRST)
|
|
786
1066
|
return out
|
|
787
1067
|
}
|
|
788
1068
|
const logical = this.state.line.split("\n")
|
|
@@ -790,22 +1070,17 @@ export class TuiApp {
|
|
|
790
1070
|
const prefix = idx === 0 ? PROMPT_FIRST : PROMPT_CONT
|
|
791
1071
|
const full = `${prefix}${ln}`
|
|
792
1072
|
const wrapped = chunkByWidth(full, w)
|
|
793
|
-
// Baris logis kosong tetap menempati 1 baris visual.
|
|
794
1073
|
if (!wrapped.length) out.push(full)
|
|
795
1074
|
else for (const r of wrapped) out.push(r)
|
|
796
1075
|
})
|
|
797
1076
|
return out
|
|
798
1077
|
}
|
|
799
1078
|
|
|
800
|
-
private
|
|
801
|
-
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
private menuRows(rows: number, cols: number): string[] {
|
|
805
|
-
if (!this.state.menuOpen || !this.state.line.startsWith("/")) return []
|
|
1079
|
+
private menuRows(rows: number, cols: number, busy = this.isBusy()): string[] {
|
|
1080
|
+
if (!this.editing || busy || !this.state.menuOpen || !this.state.line.startsWith("/")) return []
|
|
806
1081
|
const hints = this.host.listCommands(this.state.line)
|
|
807
1082
|
if (!hints.length) return []
|
|
808
|
-
const room = Math.max(0, rows -
|
|
1083
|
+
const room = Math.max(0, rows - 2 - this.composerHeight(cols, busy) - 3)
|
|
809
1084
|
const shown = hints.slice(0, Math.min(MAX_DROPDOWN_ROWS, room))
|
|
810
1085
|
const w = Math.max(10, cols)
|
|
811
1086
|
const out: string[] = [c.muted(t("app.menuHeader"))]
|
|
@@ -819,8 +1094,252 @@ export class TuiApp {
|
|
|
819
1094
|
return out.map((l) => chunkByWidth(l, w)[0] ?? "")
|
|
820
1095
|
}
|
|
821
1096
|
|
|
822
|
-
private menuHeight(rows: number, cols: number): number {
|
|
823
|
-
return this.menuRows(rows, cols).length
|
|
1097
|
+
private menuHeight(rows: number, cols: number, busy = this.isBusy()): number {
|
|
1098
|
+
return this.menuRows(rows, cols, busy).length
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
private selectedRange(row: TranscriptRow): { start: number; end: number } | null {
|
|
1102
|
+
const selection = this.selection
|
|
1103
|
+
if (!selection || !row.selectable || row.sourceId < 0) return null
|
|
1104
|
+
const first =
|
|
1105
|
+
this.transcript.comparePoints(selection.anchor, selection.focus) <= 0
|
|
1106
|
+
? selection.anchor
|
|
1107
|
+
: selection.focus
|
|
1108
|
+
const last = first === selection.anchor ? selection.focus : selection.anchor
|
|
1109
|
+
const rowStart = { id: row.sourceId, offset: row.sourceStart }
|
|
1110
|
+
const rowEnd = { id: row.sourceId, offset: row.sourceEnd }
|
|
1111
|
+
if (this.transcript.comparePoints(last, rowStart) < 0) return null
|
|
1112
|
+
if (this.transcript.comparePoints(first, rowEnd) > 0) return null
|
|
1113
|
+
const start =
|
|
1114
|
+
first.id === row.sourceId ? Math.max(row.sourceStart, first.offset) : row.sourceStart
|
|
1115
|
+
const end = last.id === row.sourceId ? Math.min(row.sourceEnd, last.offset) : row.sourceEnd
|
|
1116
|
+
if (end <= start) return null
|
|
1117
|
+
return {
|
|
1118
|
+
start: this.transcript.columnAtOffset(row, start),
|
|
1119
|
+
end: this.transcript.columnAtOffset(row, end),
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
private scheduleSelectionPaint(): void {
|
|
1124
|
+
if (this.repaintTimer !== undefined) return
|
|
1125
|
+
this.repaintTimer = setTimeout(() => {
|
|
1126
|
+
this.repaintTimer = undefined
|
|
1127
|
+
if (!this.quitRequested && this.suspended === 0 && !this.released) this.paintCurrent()
|
|
1128
|
+
}, 16)
|
|
1129
|
+
try {
|
|
1130
|
+
;(this.repaintTimer as unknown as { unref?: () => void }).unref?.()
|
|
1131
|
+
} catch {}
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
private movePromptCursor(
|
|
1135
|
+
layout: FrameLayout,
|
|
1136
|
+
key: Extract<PromptKey, { type: "mouse" }>,
|
|
1137
|
+
): boolean {
|
|
1138
|
+
if (this.isBusy() || !this.editing) return false
|
|
1139
|
+
const relativeRow = key.y - 1 - layout.composerTop
|
|
1140
|
+
const targetRow = layout.composerOffset + relativeRow
|
|
1141
|
+
if (relativeRow < 0 || targetRow < 0) return false
|
|
1142
|
+
const w = Math.max(10, process.stdout.columns || 80)
|
|
1143
|
+
const logical = this.state.line.split("\n")
|
|
1144
|
+
let rowIndex = 0
|
|
1145
|
+
let baseCodeUnits = 0
|
|
1146
|
+
for (let lineIndex = 0; lineIndex < logical.length; lineIndex++) {
|
|
1147
|
+
const line = logical[lineIndex] ?? ""
|
|
1148
|
+
const prefix = lineIndex === 0 ? PROMPT_FIRST : PROMPT_CONT
|
|
1149
|
+
const rows = chunkByWidth(`${prefix}${line}`, w)
|
|
1150
|
+
if (targetRow < rowIndex + rows.length) {
|
|
1151
|
+
const row = rows[targetRow - rowIndex] ?? ""
|
|
1152
|
+
const plain = stripSgr(sanitizeAnsi(row))
|
|
1153
|
+
const body = plain.startsWith(prefix) ? plain.slice(prefix.length) : plain
|
|
1154
|
+
const targetColumn = Math.max(
|
|
1155
|
+
0,
|
|
1156
|
+
key.x - 1 - (plain.startsWith(prefix) ? displayWidth(prefix) : 0),
|
|
1157
|
+
)
|
|
1158
|
+
const local = graphemeOffsetAtColumn(body, targetColumn)
|
|
1159
|
+
const base = toGraphemes(this.state.line.slice(0, baseCodeUnits)).length
|
|
1160
|
+
this.state = { ...this.state, cursor: base + local }
|
|
1161
|
+
this.histIdx = -1
|
|
1162
|
+
this.editing = true
|
|
1163
|
+
this.selection = null
|
|
1164
|
+
this.paintCurrent()
|
|
1165
|
+
return true
|
|
1166
|
+
}
|
|
1167
|
+
rowIndex += rows.length
|
|
1168
|
+
baseCodeUnits += line.length + 1
|
|
1169
|
+
}
|
|
1170
|
+
return false
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
private handleMouse(key: Extract<PromptKey, { type: "mouse" }>): void {
|
|
1174
|
+
if (key.action === "move" || (key.action !== "release" && key.button !== 0)) return
|
|
1175
|
+
const layout = this.lastLayout
|
|
1176
|
+
if (!layout) return
|
|
1177
|
+
const rowIndex = key.y - 1 - layout.transcriptTop
|
|
1178
|
+
const row = rowIndex >= 0 ? layout.transcriptRows[rowIndex] : undefined
|
|
1179
|
+
if (key.action === "press") {
|
|
1180
|
+
if (!row) {
|
|
1181
|
+
if (this.movePromptCursor(layout, key)) return
|
|
1182
|
+
this.selection = null
|
|
1183
|
+
this.paintCurrent()
|
|
1184
|
+
return
|
|
1185
|
+
}
|
|
1186
|
+
const point = this.transcript.pointAt(row, key.x - 1)
|
|
1187
|
+
if (!point) {
|
|
1188
|
+
this.selection = null
|
|
1189
|
+
this.paintCurrent()
|
|
1190
|
+
return
|
|
1191
|
+
}
|
|
1192
|
+
this.selection = { anchor: point, focus: point, dragging: true }
|
|
1193
|
+
this.paintCurrent()
|
|
1194
|
+
return
|
|
1195
|
+
}
|
|
1196
|
+
if (key.action === "drag") {
|
|
1197
|
+
if (!this.selection?.dragging || !row) return
|
|
1198
|
+
const point = this.transcript.pointAt(row, key.x - 1)
|
|
1199
|
+
if (!point) return
|
|
1200
|
+
this.selection.focus = point
|
|
1201
|
+
this.scheduleSelectionPaint()
|
|
1202
|
+
return
|
|
1203
|
+
}
|
|
1204
|
+
if (key.action === "release") {
|
|
1205
|
+
if (!this.selection?.dragging) return
|
|
1206
|
+
if (row) {
|
|
1207
|
+
const point = this.transcript.pointAt(row, key.x - 1)
|
|
1208
|
+
if (point) this.selection.focus = point
|
|
1209
|
+
}
|
|
1210
|
+
this.selection.dragging = false
|
|
1211
|
+
this.paintCurrent()
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
private copySelection(): void {
|
|
1216
|
+
const selection = this.selection
|
|
1217
|
+
if (!selection) return
|
|
1218
|
+
const text = this.transcript.selectionText(selection.anchor, selection.focus).trim()
|
|
1219
|
+
if (!text) {
|
|
1220
|
+
this.selection = null
|
|
1221
|
+
this.paintCurrent()
|
|
1222
|
+
return
|
|
1223
|
+
}
|
|
1224
|
+
if (text.length > MAX_SELECTION_CHARS) {
|
|
1225
|
+
this.transcript.pushInfo([c.warning(t("tui.selectionTooLarge"))])
|
|
1226
|
+
this.paintCurrent()
|
|
1227
|
+
return
|
|
1228
|
+
}
|
|
1229
|
+
let copied = false
|
|
1230
|
+
try {
|
|
1231
|
+
copied = this.host.copySelection(text)
|
|
1232
|
+
} catch {}
|
|
1233
|
+
if (copied) this.transcript.pushInfo([c.muted(t("tui.selectionCopied"))])
|
|
1234
|
+
else this.transcript.pushInfo([c.muted(t("tui.selectionCopyFailed"))])
|
|
1235
|
+
this.paintCurrent()
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
private buildLayout(rows: number, cols: number, snap: TuiStatusSnapshot): FrameLayout {
|
|
1239
|
+
const height = Math.max(1, Math.floor(rows))
|
|
1240
|
+
const width = Math.max(1, Math.floor(cols))
|
|
1241
|
+
const busy = this.busy || snap.busy
|
|
1242
|
+
const now = Date.now()
|
|
1243
|
+
if (busy) {
|
|
1244
|
+
if (!this.turnStartedAt) this.turnStartedAt = now
|
|
1245
|
+
this.startActivityClock()
|
|
1246
|
+
} else {
|
|
1247
|
+
this.thinkingActive = false
|
|
1248
|
+
this.turnStartedAt = 0
|
|
1249
|
+
}
|
|
1250
|
+
const newCount = this.scrollBack > 0 ? this.transcript.total() - this.scrollBase : 0
|
|
1251
|
+
const indicator: string[] = []
|
|
1252
|
+
if (busy && this.abortHintOn) indicator.push(c.warning(t("app.abortHint")))
|
|
1253
|
+
else if (newCount > 0) indicator.push(c.muted(t("app.newBelow", { n: newCount })))
|
|
1254
|
+
|
|
1255
|
+
const allMenu = this.menuRows(height, width, busy)
|
|
1256
|
+
const allComposer = this.composerRows(width, snap, busy, now)
|
|
1257
|
+
const contentBudget = Math.max(1, height - 3 - indicator.length)
|
|
1258
|
+
let menu = allMenu
|
|
1259
|
+
let composerH = Math.min(MAX_INPUT_ROWS, Math.max(1, allComposer.length))
|
|
1260
|
+
if (menu.length + composerH > contentBudget) {
|
|
1261
|
+
menu = menu.slice(0, Math.max(0, contentBudget - 1))
|
|
1262
|
+
composerH = Math.max(1, contentBudget - menu.length)
|
|
1263
|
+
}
|
|
1264
|
+
const viewH = Math.max(1, height - 2 - composerH - menu.length - indicator.length)
|
|
1265
|
+
let viewport = this.transcript.viewport(width, viewH, this.scrollBack)
|
|
1266
|
+
const curWrapped = viewport.totalRows
|
|
1267
|
+
if (this.scrollBack > 0 && this.lastWrapped > 0) {
|
|
1268
|
+
const nextScrollBack = Math.max(
|
|
1269
|
+
0,
|
|
1270
|
+
Math.min(this.scrollBack + (curWrapped - this.lastWrapped), Math.max(0, curWrapped - 1)),
|
|
1271
|
+
)
|
|
1272
|
+
if (nextScrollBack !== this.scrollBack) {
|
|
1273
|
+
this.scrollBack = nextScrollBack
|
|
1274
|
+
viewport = this.transcript.viewport(width, viewH, this.scrollBack)
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
this.lastWrapped = curWrapped
|
|
1278
|
+
if (
|
|
1279
|
+
this.selection &&
|
|
1280
|
+
(!this.transcript.hasSource(this.selection.anchor.id) ||
|
|
1281
|
+
!this.transcript.hasSource(this.selection.focus.id) ||
|
|
1282
|
+
(!this.selection.dragging &&
|
|
1283
|
+
!this.transcript.selectionText(this.selection.anchor, this.selection.focus)))
|
|
1284
|
+
) {
|
|
1285
|
+
this.selection = null
|
|
1286
|
+
}
|
|
1287
|
+
const bodyRows = viewport.rows
|
|
1288
|
+
const body = bodyRows.map((row) => {
|
|
1289
|
+
const selected = this.selectedRange(row)
|
|
1290
|
+
return selected ? highlightColumns(row.text, selected.start, selected.end) : row.text
|
|
1291
|
+
})
|
|
1292
|
+
|
|
1293
|
+
let shownComposer: string[]
|
|
1294
|
+
let cursorRow: number
|
|
1295
|
+
let cursorCol = 1
|
|
1296
|
+
let composerOffset = 0
|
|
1297
|
+
if (!busy && this.editing) {
|
|
1298
|
+
const cursor = this.cursorIndex(width)
|
|
1299
|
+
const winStart = Math.max(0, Math.min(allComposer.length - composerH, cursor.visIdx))
|
|
1300
|
+
composerOffset = winStart
|
|
1301
|
+
shownComposer = allComposer.slice(winStart, winStart + composerH)
|
|
1302
|
+
cursorRow = viewH + menu.length + indicator.length + (cursor.visIdx - winStart) + 1
|
|
1303
|
+
cursorCol = cursor.col
|
|
1304
|
+
} else {
|
|
1305
|
+
shownComposer = allComposer.slice(0, composerH)
|
|
1306
|
+
cursorRow = viewH + menu.length + indicator.length + 1
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
const started = this.turnStartedAt || now
|
|
1310
|
+
const sparkFrame =
|
|
1311
|
+
busy && !motionReduced() ? Math.max(1, Math.floor((now - started) / DOT_INTERVAL_MS) + 1) : 0
|
|
1312
|
+
const elapsed = now - started
|
|
1313
|
+
const timer = busy ? formatTimer(elapsed) : TIMER_TEXT
|
|
1314
|
+
const footer =
|
|
1315
|
+
renderFooter(
|
|
1316
|
+
{
|
|
1317
|
+
...snap.footer,
|
|
1318
|
+
sparkFrame,
|
|
1319
|
+
timer,
|
|
1320
|
+
timerActive: busy,
|
|
1321
|
+
timerHighlight: timerHighlight(elapsed),
|
|
1322
|
+
},
|
|
1323
|
+
width,
|
|
1324
|
+
)[0] ?? ""
|
|
1325
|
+
const frame = [...body, ...menu, ...indicator, ...shownComposer, "", footer]
|
|
1326
|
+
const rawLength = frame.length
|
|
1327
|
+
const crop = Math.max(0, rawLength - height)
|
|
1328
|
+
const topPad = Math.max(0, height - rawLength)
|
|
1329
|
+
while (frame.length < height) frame.unshift("")
|
|
1330
|
+
const out = frame.slice(-height)
|
|
1331
|
+
return {
|
|
1332
|
+
frame: out,
|
|
1333
|
+
cursor: {
|
|
1334
|
+
row: Math.max(1, Math.min(height, cursorRow + topPad - crop)),
|
|
1335
|
+
col: Math.max(1, cursorCol),
|
|
1336
|
+
},
|
|
1337
|
+
transcriptRows: bodyRows,
|
|
1338
|
+
transcriptTop: topPad - crop,
|
|
1339
|
+
composerTop: topPad - crop + body.length + menu.length + indicator.length,
|
|
1340
|
+
composerLines: allComposer,
|
|
1341
|
+
composerOffset,
|
|
1342
|
+
}
|
|
824
1343
|
}
|
|
825
1344
|
|
|
826
1345
|
/** True bila terminal menciut di bawah minimum TUI (MIN_ROWS/MIN_COLS). */
|
|
@@ -834,10 +1353,8 @@ export class TuiApp {
|
|
|
834
1353
|
this.currentScreen = screen
|
|
835
1354
|
const cols = screen.cols
|
|
836
1355
|
const rows = screen.rows
|
|
837
|
-
// Layar menciut tengah sesi: satu bingkai pesan jujur (tepat `rows`
|
|
838
|
-
// baris), bukan layout rusak. Kembali normal otomatis saat resize
|
|
839
|
-
// (onResize → paint). Gagal-di-kode-lama: wrap pecah + kursor liar.
|
|
840
1356
|
if (this.tooSmall()) {
|
|
1357
|
+
this.lastLayout = null
|
|
841
1358
|
const w = Math.max(1, cols)
|
|
842
1359
|
const frame = [c.warning(truncateToWidth(t("tui.tooSmall"), w))]
|
|
843
1360
|
while (frame.length < Math.max(1, rows)) frame.push("")
|
|
@@ -847,62 +1364,10 @@ export class TuiApp {
|
|
|
847
1364
|
this.frames++
|
|
848
1365
|
return
|
|
849
1366
|
}
|
|
850
|
-
const
|
|
851
|
-
|
|
852
|
-
// busy tak boleh bergantung pada animasi (aksesibilitas / rec / SSH lambat).
|
|
853
|
-
this.tick = snap.busy ? this.tick + 1 : 0
|
|
854
|
-
const statusLine =
|
|
855
|
-
renderFooter(
|
|
856
|
-
{ ...snap.footer, sparkFrame: snap.busy && !motionReduced() ? this.tick : 0 },
|
|
857
|
-
cols,
|
|
858
|
-
)[0] ?? ""
|
|
859
|
-
const menu = this.menuRows(rows, cols)
|
|
860
|
-
const inputAll = this.inputRows(cols)
|
|
861
|
-
const inputH = Math.min(MAX_INPUT_ROWS, Math.max(1, inputAll.length))
|
|
862
|
-
// Indikator "output baru di bawah": user membaca ke atas saat stream
|
|
863
|
-
// masuk — tanpa ini output baru lewat diam-diam. Makan 1 baris viewport.
|
|
864
|
-
// total() monotonik: evict cap tak membuat hitungan negatif/hilang.
|
|
865
|
-
const newCount = this.scrollBack > 0 ? this.transcript.total() - this.scrollBase : 0
|
|
866
|
-
// Indikator + hint abort saling eksklusif (satu slot): scroll memakai slot
|
|
867
|
-
// untuk "baris baru"; abort memakainya untuk petunjuk keluar.
|
|
868
|
-
const indicator: string[] = []
|
|
869
|
-
if (newCount > 0) indicator.push(c.muted(t("app.newBelow", { n: newCount })))
|
|
870
|
-
else if (this.busy && this.abortHintOn) indicator.push(c.warning(t("app.abortHint")))
|
|
871
|
-
const viewH = Math.max(1, rows - 1 - inputH - menu.length - indicator.length)
|
|
872
|
-
// Kunci posisi baca: stream yang menambah baris saat user scroll ke atas
|
|
873
|
-
// ikut menggeser scrollBack agar jendela menunjuk baris absolut yang sama
|
|
874
|
-
// (dulu viewport merayap mengikuti ekor — kontrak I17).
|
|
875
|
-
const curWrapped = this.transcript.wrappedLength(cols)
|
|
876
|
-
if (this.scrollBack > 0 && this.lastWrapped > 0) {
|
|
877
|
-
this.scrollBack = Math.max(
|
|
878
|
-
0,
|
|
879
|
-
Math.min(this.scrollBack + (curWrapped - this.lastWrapped), Math.max(0, curWrapped - 1)),
|
|
880
|
-
)
|
|
881
|
-
}
|
|
882
|
-
this.lastWrapped = curWrapped
|
|
883
|
-
const body = this.transcript.view(cols, viewH, this.scrollBack)
|
|
884
|
-
// Jendela input mengikuti kursor: bila kursor di atas jendela ekor
|
|
885
|
-
// (mis. paste 10 baris lalu Home), gulir ke atas agar kursor terlihat.
|
|
886
|
-
// Tanpa ini baris atas tak terjangkau visual tapi kursor diparkir di
|
|
887
|
-
// baris yang salah (dulu selalu slice ekor).
|
|
888
|
-
const cursor = this.cursorIndex(cols)
|
|
889
|
-
const winStart = Math.max(0, Math.min(inputAll.length - inputH, cursor.visIdx))
|
|
890
|
-
const shownInput = inputAll.slice(winStart, winStart + inputH)
|
|
891
|
-
const frame: string[] = [...body, ...menu, ...indicator, ...shownInput, statusLine]
|
|
892
|
-
while (frame.length < rows) frame.unshift("")
|
|
893
|
-
const out = frame.slice(-rows)
|
|
1367
|
+
const layout = this.buildLayout(rows, cols, this.host.getStatus())
|
|
1368
|
+
this.lastLayout = layout
|
|
894
1369
|
try {
|
|
895
|
-
screen.paint(
|
|
896
|
-
} catch {}
|
|
897
|
-
// Parkir kursor di posisi ketik (kolom terminal, bukan karakter).
|
|
898
|
-
// Di dalam blok sync-update yang SAMA dengan frame (temuan audit TUI-007):
|
|
899
|
-
// kursor yang ditulis setelah SYNC_END berpotensi tearing di emulator
|
|
900
|
-
// tanpa ?2026 (frame tampil, kursor satu posisi lama).
|
|
901
|
-
const cursorRow = viewH + menu.length + indicator.length + (cursor.visIdx - winStart) + 1
|
|
902
|
-
try {
|
|
903
|
-
process.stdout.write(
|
|
904
|
-
`${SYNC_UPDATE_START}\x1b[?25l\x1b[${Math.max(1, cursorRow)};${Math.max(1, cursor.col)}H\x1b[?25h${SYNC_UPDATE_END}`,
|
|
905
|
-
)
|
|
1370
|
+
screen.paint(layout.frame, layout.cursor)
|
|
906
1371
|
} catch {}
|
|
907
1372
|
this.frames++
|
|
908
1373
|
}
|