minicode-ai 0.10.0 → 0.11.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.
Files changed (128) hide show
  1. package/AGENTS.md +45 -0
  2. package/CHANGELOG.md +59 -4
  3. package/PLAN.md +491 -0
  4. package/README.md +2 -2
  5. package/cli/commands/acp.ts +485 -0
  6. package/cli/commands/auth.ts +7 -4
  7. package/cli/commands/config.ts +41 -27
  8. package/cli/commands/exec.ts +33 -12
  9. package/cli/commands/mcp.ts +3 -2
  10. package/cli/commands/memory.ts +1 -1
  11. package/cli/commands/pricing.ts +6 -4
  12. package/cli/commands/providers.ts +18 -8
  13. package/cli/commands/sessions.ts +11 -4
  14. package/cli/commands/skills.ts +3 -3
  15. package/cli/commands/stats.ts +3 -1
  16. package/cli/commands.ts +6 -0
  17. package/cli/index.ts +82 -57
  18. package/cli/model-manager.ts +6 -1
  19. package/cli/router.ts +7 -0
  20. package/cli/setup.ts +341 -5
  21. package/cli/tui.ts +54 -18
  22. package/docs/ARCHITECTURE.html +29 -22
  23. package/docs/CONTROL-PLANE-MAP.md +139 -0
  24. package/docs/HARNESS.md +150 -0
  25. package/docs/PLAN_UIUX_V6.md +209 -0
  26. package/docs/README.md +1 -1
  27. package/docs/SUMMARY.md +52 -0
  28. package/docs/TERMINAL_CONTRACT.md +367 -0
  29. package/docs/UI_RENDER_PIPELINE.md +165 -0
  30. package/docs/USAGE.md +11 -0
  31. package/docs/agents.md +31 -0
  32. package/docs/architecture.md +3 -3
  33. package/docs/choosing-mode.md +34 -0
  34. package/docs/cli.md +130 -0
  35. package/docs/concepts.md +53 -0
  36. package/docs/config-providers.md +87 -0
  37. package/docs/config.md +63 -0
  38. package/docs/contributing.md +79 -0
  39. package/docs/environment.md +95 -0
  40. package/docs/exec.md +73 -0
  41. package/docs/getting-started.md +102 -0
  42. package/docs/glossary.md +37 -0
  43. package/docs/mcp-lsp.md +54 -0
  44. package/docs/memory-sessions.md +63 -0
  45. package/docs/policy-sandbox.md +72 -0
  46. package/docs/pricing-budget.md +49 -0
  47. package/docs/quickstart.md +60 -0
  48. package/docs/repl.md +67 -0
  49. package/docs/security-model.md +95 -0
  50. package/docs/security.md +91 -0
  51. package/docs/skills.md +48 -0
  52. package/docs/terminal.md +79 -0
  53. package/docs/tools.md +123 -0
  54. package/docs/troubleshooting.md +40 -0
  55. package/docs/verify-benchmark.md +58 -0
  56. package/package.json +8 -5
  57. package/src/app/provider-layer.ts +12 -4
  58. package/src/app/session.ts +6 -0
  59. package/src/app/tool-layer.ts +2 -1
  60. package/src/constants.ts +35 -0
  61. package/src/lib/net.ts +6 -1
  62. package/src/lsp/client.ts +41 -8
  63. package/src/mcp/http-transport.ts +52 -15
  64. package/src/mcp/transport.ts +32 -1
  65. package/src/memory/files.ts +6 -5
  66. package/src/policy/allowlist.ts +1 -1
  67. package/src/policy/bash-guard.ts +154 -14
  68. package/src/policy/compaction.ts +57 -3
  69. package/src/policy/context.ts +4 -0
  70. package/src/policy/jail.ts +49 -4
  71. package/src/policy/permission.ts +152 -12
  72. package/src/policy/pricing.ts +12 -9
  73. package/src/presentation/adapter.ts +721 -0
  74. package/src/presentation/events.ts +324 -0
  75. package/src/presentation/label.ts +140 -0
  76. package/src/presentation/model.ts +172 -0
  77. package/src/presentation/reducer.ts +486 -0
  78. package/src/presentation/store.ts +256 -0
  79. package/src/providers/anthropic.ts +22 -4
  80. package/src/providers/detect.ts +2 -1
  81. package/src/providers/router.ts +13 -0
  82. package/src/repo/repomap.ts +2 -29
  83. package/src/session/checkpoint.ts +27 -9
  84. package/src/session/journal.ts +27 -5
  85. package/src/skills/loader.ts +88 -5
  86. package/src/telemetry/trace.ts +36 -15
  87. package/src/tools/ask_user.ts +73 -4
  88. package/src/tools/bash.ts +15 -17
  89. package/src/tools/glob.ts +10 -11
  90. package/src/tools/lsp.ts +3 -56
  91. package/src/tools/memory.ts +13 -12
  92. package/src/tools/task.ts +1 -0
  93. package/src/ui/approval/prompt.ts +14 -6
  94. package/src/ui/assistant/simple.ts +137 -26
  95. package/src/ui/assistant/turn-status.ts +16 -3
  96. package/src/ui/contract.ts +119 -0
  97. package/src/ui/footer.ts +21 -2
  98. package/src/ui/i18n/en.ts +34 -6
  99. package/src/ui/i18n/id.ts +34 -6
  100. package/src/ui/i18n/locale.ts +3 -1
  101. package/src/ui/input/input.ts +51 -4
  102. package/src/ui/input/prompt-engine.ts +46 -4
  103. package/src/ui/render/collapse.ts +13 -13
  104. package/src/ui/render/diff.ts +7 -2
  105. package/src/ui/render/format.ts +16 -8
  106. package/src/ui/render/highlight.ts +7 -3
  107. package/src/ui/render/markdown.ts +5 -1
  108. package/src/ui/render/sanitize.ts +29 -2
  109. package/src/ui/render/table.ts +8 -5
  110. package/src/ui/render/theme.ts +18 -4
  111. package/src/ui/render/width.ts +89 -10
  112. package/src/ui/runtime/motion.ts +16 -0
  113. package/src/ui/runtime/screen.ts +45 -0
  114. package/src/ui/runtime/spinner.ts +50 -4
  115. package/src/ui/runtime/statusline.ts +50 -1
  116. package/src/ui/screens/form.ts +42 -15
  117. package/src/ui/screens/model-manager.ts +26 -18
  118. package/src/ui/screens/picker.ts +47 -23
  119. package/src/ui/screens/provider-manager.ts +18 -12
  120. package/src/ui/screens/wizard.ts +24 -8
  121. package/src/ui/tui/app.ts +233 -27
  122. package/src/ui/tui/transcript.ts +269 -65
  123. package/vendor/minicore/LICENSE +21 -0
  124. package/vendor/minicore/VENDOR.md +6 -3
  125. package/vendor/minicore/package.json +6 -3
  126. package/vendor/minicore/src/core/loop.ts +12 -2
  127. package/vendor/minicore/src/core/snapshot.ts +4 -0
  128. package/vendor/minicore/src/providers/openai-compat.ts +76 -25
@@ -0,0 +1,324 @@
1
+ // Kosakata semantik presentasi agen — V2.1 (AGENT_PRESENTATION_ARCHITECTURE_V2_1.md).
2
+ //
3
+ // Kenapa berkas ini ada: runtime (MiniCore) meng-emit 9 event miskin tanpa
4
+ // lifecycle gagal/batal/deny dan tanpa identitas yang selamat sampai UI
5
+ // (vendor/minicore/src/core/events.ts, executor.ts:44-73 return tanpa emit,
6
+ // session.ts:248 hanya sukses). Dua sink UI menafsir event mentah
7
+ // sendiri-sendiri dan divergen. Berkas ini mendefinisikan SATU bahasa yang
8
+ // dipakai adaptor → reducer → proyeksi, sehingga TUI/linear/ACP kelak berbagi
9
+ // makna yang sama tanpa masing-masing merekonstruksi semantik runtime.
10
+ //
11
+ // Bukan framework event-sourcing: hanya tipe + tabel durability. Logika di
12
+ // adapter.ts (observasi) dan reducer.ts (Fase 3, state). Renderer tidak boleh
13
+ // mengimpor berkas ini untuk keputusan semantik — ia hanya menerima string
14
+ // final dari proyeksi.
15
+
16
+ /** Asal tool — di-parse SEKALI di adaptor, bukan di renderer. */
17
+ export interface ToolIdentity {
18
+ /** "builtin" = tool bawaan minicode; "mcp" = `serverid.toolname` runtime. */
19
+ origin: "builtin" | "mcp"
20
+ /** Id server MCP (`srv` pada `srv.search`); undefined untuk builtin. */
21
+ namespace?: string
22
+ /** Nama lokal (`search`, BUKAN `srv.search`). */
23
+ name: string
24
+ /** Bentuk string tunggal yang beredar (`search` | `srv.search`). */
25
+ qualified: string
26
+ }
27
+
28
+ /** Ringkasan argumen yang aman tampil (tanpa isi file/kode penuh). */
29
+ export interface ArgsSummary {
30
+ /** Target utama (`path` / `$ cmd` / pola / kueri) — untuk baris ledger. */
31
+ target?: string
32
+ /** Ringkasan satu-baris (di-scrub secret). */
33
+ text: string
34
+ }
35
+
36
+ /** Alasan penolakan kebijakan — nilai dikenal + string mentah ≤80 char. */
37
+ export type DenyReason =
38
+ | "jail"
39
+ | "sensitive"
40
+ | "allowlist"
41
+ | "bash-guard"
42
+ | "mode"
43
+ | "no-approval"
44
+ | "user"
45
+ | (string & {})
46
+
47
+ /** Penyebab kegagalan tool — minimal, bukan taksonomi enterprise. */
48
+ export type FailCause =
49
+ /** Kontrak salah: unknown tool + invalid args (model salah panggil). */
50
+ | "invalid"
51
+ /** Tool melempar / exit≠0 / executor error. */
52
+ | "exec"
53
+ /** Kategori provider (auth/rate_limit/network/…). */
54
+ | "provider"
55
+ /** Batas agen: max_steps / recovery habis. */
56
+ | "agent"
57
+
58
+ /** Alasan pembatalan — "budget" adalah refinement repo-grounded (AgentError
59
+ * kind budget_exceeded ada di kernel; V2.1 generik tidak menyebutnya). */
60
+ export type CancelReason = "user" | "timeout" | "budget" | "parent-ended" | "parent-aborted"
61
+
62
+ /** Outcome persetujuan — satu event terminal `settled`, bukan dua tipe. */
63
+ export type ApprovalOutcome =
64
+ | { decision: "allow"; by: "user" | "system" }
65
+ /** Jawab `[a]` — efek samping allowlist di luar model, keputusan tetap tercatat. */
66
+ | { decision: "allow-always"; by: "user" }
67
+ | {
68
+ decision: "deny"
69
+ by: "user" | "system"
70
+ reason?: "declined" | "headless" | "no-ask" | "no-view" | "prompt-error" | "parent-aborted"
71
+ }
72
+ | {
73
+ decision: "cancelled"
74
+ by: "system"
75
+ reason: "parent-aborted" | "parent-ended" | "timeout" | "renderer-failure"
76
+ }
77
+
78
+ /** Ringkasan turn — agregat derived (dihitung adaptor/reducer, bukan runtime). */
79
+ export interface TurnSummary {
80
+ toolsOk: number
81
+ toolsFailed: number
82
+ toolsDenied: number
83
+ toolsCancelled: number
84
+ toolsInterrupted: number
85
+ filesChanged: number
86
+ checkpointId?: string
87
+ durationMs: number
88
+ }
89
+
90
+ /** Bukti konsekuensi (receipt) — view atas journal/checkpoint, bukan tracker baru. */
91
+ export interface Receipt {
92
+ toolCallId: string
93
+ journalSeq?: number
94
+ checkpointId?: string
95
+ paths?: string[]
96
+ stats?: { added?: number; removed?: number }
97
+ test?: { passed: number; failed: number; summary: string }
98
+ cmd?: { exit?: number }
99
+ }
100
+
101
+ /** Referensi konten besar — model hanya menyimpan ini, bukan isinya. */
102
+ export interface ContentRef {
103
+ toolCallId: string
104
+ idx: number
105
+ /** True bila store meng-evict entry (Fase 4): proyeksi tampilkan penanda retensi. */
106
+ dead?: boolean
107
+ }
108
+
109
+ /**
110
+ * Tautan forward sub-anak → call `delegate_task` pemanggil (Fase 2).
111
+ * parentToolCallId tidak tersedia di ToolContext execute — adaptor memasangkan
112
+ * `execution:started` delegate_task (call.id) dengan forward bertag
113
+ * `forwardedChild` saat tepat satu call induk masih terbuka (§12 V2.1);
114
+ * paralel ambigu TIDAK ditebak (orphan, bukan taut salah).
115
+ */
116
+ export interface ChildSessionLink {
117
+ parentToolCallId: string
118
+ childSessionId: string
119
+ parentSessionId?: string
120
+ }
121
+
122
+ interface Base {
123
+ eventSeq: number
124
+ ts: number
125
+ sessionId: string
126
+ turnId: number
127
+ }
128
+
129
+ export interface TurnStartedEvent extends Base {
130
+ type: "turn.started"
131
+ promptRef: string
132
+ }
133
+ export interface TurnCompletedEvent extends Base {
134
+ type: "turn.completed"
135
+ summary: TurnSummary
136
+ }
137
+ export interface TurnFailedEvent extends Base {
138
+ type: "turn.failed"
139
+ error: { cause: FailCause | "provider" | "system"; message: string }
140
+ }
141
+ export interface TurnCancelledEvent extends Base {
142
+ type: "turn.cancelled"
143
+ reason: CancelReason
144
+ }
145
+ export interface ModelDeltaEvent extends Base {
146
+ type: "model.delta"
147
+ delta: string
148
+ }
149
+ export interface ModelCompletedEvent extends Base {
150
+ type: "model.completed"
151
+ text: string
152
+ truncated: boolean
153
+ expandRef?: ContentRef
154
+ }
155
+ export interface ReasoningDeltaEvent extends Base {
156
+ type: "reasoning.delta"
157
+ delta: string
158
+ }
159
+ export interface ReasoningCompletedEvent extends Base {
160
+ type: "reasoning.completed"
161
+ truncated: boolean
162
+ expandRef: ContentRef
163
+ }
164
+ export interface ToolStartedEvent extends Base {
165
+ type: "tool.started"
166
+ toolCallId: string
167
+ stepId: number
168
+ identity: ToolIdentity
169
+ argsSummary: ArgsSummary
170
+ /** Forward anak: taut ke delegate_task pemanggil (absen = tool parent biasa). */
171
+ parentLink?: ChildSessionLink
172
+ }
173
+ export interface ToolProgressEvent extends Base {
174
+ type: "tool.progress"
175
+ toolCallId: string
176
+ message: string
177
+ }
178
+ export interface ToolCompletedEvent extends Base {
179
+ type: "tool.completed"
180
+ toolCallId: string
181
+ durationMs: number
182
+ summary: string
183
+ expandRef: ContentRef
184
+ receipt?: Receipt
185
+ parentLink?: ChildSessionLink
186
+ }
187
+ export interface ToolFailedEvent extends Base {
188
+ type: "tool.failed"
189
+ toolCallId: string
190
+ durationMs: number
191
+ cause: FailCause
192
+ message: string
193
+ hint?: string
194
+ expandRef: ContentRef
195
+ parentLink?: ChildSessionLink
196
+ }
197
+ export interface ToolDeniedEvent extends Base {
198
+ type: "tool.denied"
199
+ toolCallId: string
200
+ reason: DenyReason
201
+ message: string
202
+ parentLink?: ChildSessionLink
203
+ }
204
+ export interface ToolCancelledEvent extends Base {
205
+ type: "tool.cancelled"
206
+ toolCallId: string
207
+ reason: CancelReason
208
+ parentLink?: ChildSessionLink
209
+ }
210
+ export interface ApprovalRequestedEvent extends Base {
211
+ type: "approval.requested"
212
+ approvalId: string
213
+ /** Absen = pertanyaan ask_user langsung (bukan gate tool). */
214
+ toolCallId?: string
215
+ identity: ToolIdentity
216
+ argsSummary: ArgsSummary
217
+ via: "prompt" | "system"
218
+ }
219
+ export interface ApprovalSettledEvent extends Base {
220
+ type: "approval.settled"
221
+ approvalId: string
222
+ toolCallId?: string
223
+ outcome: ApprovalOutcome
224
+ }
225
+ export interface FileChangedEvent extends Base {
226
+ type: "file.changed"
227
+ toolCallId: string
228
+ paths: string[]
229
+ journalSeq?: number
230
+ checkpointId?: string
231
+ }
232
+ export interface TestCompletedEvent extends Base {
233
+ type: "test.completed"
234
+ toolCallId: string
235
+ passed: number
236
+ failed: number
237
+ summary: string
238
+ }
239
+ export interface ContextCompactedEvent extends Base {
240
+ type: "context.compacted"
241
+ reason: string
242
+ }
243
+
244
+ export type DomainEvent =
245
+ | TurnStartedEvent
246
+ | TurnCompletedEvent
247
+ | TurnFailedEvent
248
+ | TurnCancelledEvent
249
+ | ModelDeltaEvent
250
+ | ModelCompletedEvent
251
+ | ReasoningDeltaEvent
252
+ | ReasoningCompletedEvent
253
+ | ToolStartedEvent
254
+ | ToolProgressEvent
255
+ | ToolCompletedEvent
256
+ | ToolFailedEvent
257
+ | ToolDeniedEvent
258
+ | ToolCancelledEvent
259
+ | ApprovalRequestedEvent
260
+ | ApprovalSettledEvent
261
+ | FileChangedEvent
262
+ | TestCompletedEvent
263
+ | ContextCompactedEvent
264
+
265
+ export type DomainEventType = DomainEvent["type"]
266
+
267
+ /** Kebijakan durable vs live (§7 V2.1): restart merekonstruksi tanpa delta. */
268
+ export const DURABILITY: Record<DomainEventType, { durable: boolean; replayable: boolean }> = {
269
+ "turn.started": { durable: true, replayable: true },
270
+ "turn.completed": { durable: true, replayable: true },
271
+ "turn.failed": { durable: true, replayable: true },
272
+ "turn.cancelled": { durable: true, replayable: true },
273
+ "model.delta": { durable: false, replayable: false },
274
+ "model.completed": { durable: true, replayable: true },
275
+ "reasoning.delta": { durable: false, replayable: false },
276
+ "reasoning.completed": { durable: true, replayable: true },
277
+ "tool.started": { durable: true, replayable: true },
278
+ "tool.progress": { durable: false, replayable: false },
279
+ "tool.completed": { durable: true, replayable: true },
280
+ "tool.failed": { durable: true, replayable: true },
281
+ "tool.denied": { durable: true, replayable: true },
282
+ "tool.cancelled": { durable: true, replayable: true },
283
+ "approval.requested": { durable: true, replayable: true },
284
+ "approval.settled": { durable: true, replayable: true },
285
+ "file.changed": { durable: true, replayable: true },
286
+ "test.completed": { durable: true, replayable: true },
287
+ "context.compacted": { durable: true, replayable: true },
288
+ }
289
+
290
+ /**
291
+ * Bus sumber yang dibaca adaptor — struktural seperti UiBus (handler `any`
292
+ * disengaja karena `on` kernel generik-kondisional; lihat contract.ts:46-55).
293
+ */
294
+ export interface EventBusLike {
295
+ on(type: string, handler: (event: any) => void): () => void
296
+ }
297
+
298
+ // ── Hook persetujuan (permission/ask_user → adaptor, via DI dari cli/) ──
299
+ //
300
+ // Policy layer tidak mengimpor UI dan tidak tahu PresentationState; ia hanya
301
+ // memanggil hook ini di samping callback keputusan. Adaptor yang mengisi
302
+ // eventSeq/ts/sessionId/turnId + identity/argsSummary (satu-satunya parser).
303
+
304
+ export interface ApprovalPromptCall {
305
+ id?: string
306
+ name: string
307
+ args?: unknown
308
+ }
309
+
310
+ export type ApprovalHookEvent =
311
+ | {
312
+ kind: "requested"
313
+ approvalId: string
314
+ call: ApprovalPromptCall
315
+ via: "prompt" | "system"
316
+ }
317
+ | {
318
+ kind: "settled"
319
+ approvalId: string
320
+ call: ApprovalPromptCall
321
+ outcome: ApprovalOutcome
322
+ }
323
+
324
+ export type ApprovalEventHook = (e: ApprovalHookEvent) => void
@@ -0,0 +1,140 @@
1
+ // Labeler tunggal presentasi — satu sumber untuk target, preview, dan ringkasan
2
+ // tool. Murni, tanpa IO/clock.
3
+ //
4
+ // Kenapa di sini: 4 labeler lama + 3 angka cap berbeda; reducer (summary) dan
5
+ // proyeksi (label) harus berbagi satu sumber. Fase 3: fungsi siap pakai +
6
+ // unit test; Fase 6: simple.ts/toolStatus delegasi ke sini, Fase 7 hapus lama.
7
+ //
8
+ // Batas lapisan: src/presentation TIDAK boleh impor src/ui (ui-boundary) —
9
+ // sanitasi tampilan tetap di renderer (sanitizeAnsi defense-in-depth). Label
10
+ // di sini hanya memotong & membuang control-char kasar agar aman disimpan
11
+ // di state sebelum proyeksi.
12
+
13
+ import type { ArgsSummary, ToolIdentity } from "./events.ts"
14
+
15
+ /** Predikat nama tool ter-namespace; dipanggil policy/session tanpa parse string. */
16
+ export function isMcpToolName(name: string): boolean {
17
+ return name.indexOf(".") > 0
18
+ }
19
+
20
+ export const MAX_TARGET = 80
21
+ /** Cap preview args di label satu-baris. */
22
+ export const MAX_PREVIEW = 60
23
+ /** Cap summary ringkas tool. */
24
+ export const MAX_SUMMARY = 120
25
+ /** Cap label status bar (nama+target). */
26
+ export const MAX_LABEL = 200
27
+
28
+ /** Potong per code point (jangan belah surrogate pair emoji di batas). */
29
+ function safeSlice(s: string, n: number): string {
30
+ if (s.length <= n) return s
31
+ return Array.from(s).slice(0, n).join("")
32
+ }
33
+
34
+ /** Buang C0/C1 + ESC kasar; label = satu baris plain sebelum paint sanitize. */
35
+ function stripControl(s: string): string {
36
+ let out = ""
37
+ for (const ch of s) {
38
+ const c = ch.codePointAt(0) ?? 0
39
+ if (c === 0x1b || c === 0x7f || (c < 0x20 && ch !== "\t") || (c >= 0x80 && c <= 0x9f)) continue
40
+ out += ch === "\n" || ch === "\r" ? " " : ch
41
+ }
42
+ return out.trim()
43
+ }
44
+
45
+ function clip(s: string, max: number): string {
46
+ const t = stripControl(s)
47
+ return t.length > max ? `${safeSlice(t, max)}…` : t
48
+ }
49
+
50
+ /**
51
+ * Target utama argumen — urutan mengikuti konvensi ledger:
52
+ * path/from-to → cmd/command → pattern → query → prompt-slice.
53
+ */
54
+ export function targetOf(args: unknown): string | undefined {
55
+ if (typeof args !== "object" || args === null) return undefined
56
+ const a = args as Record<string, unknown>
57
+ if (typeof a.path === "string" && a.path) return a.path
58
+ if (typeof a.from === "string" && a.from) {
59
+ const to = typeof a.to === "string" ? a.to : ""
60
+ return to ? `${a.from} → ${to}` : a.from
61
+ }
62
+ const cmd = a.cmd ?? a.command
63
+ if (typeof cmd === "string" && cmd) return `$ ${cmd}`
64
+ if (typeof a.pattern === "string" && a.pattern) return a.pattern
65
+ if (typeof a.query === "string" && a.query) return a.query
66
+ if (typeof a.prompt === "string" && a.prompt) return a.prompt
67
+ if (typeof a.file === "string" && a.file) return a.file
68
+ return undefined
69
+ }
70
+
71
+ /** Preview args satu baris (fallback bila ArgsSummary.target kosong). */
72
+ export function previewArgs(args: unknown): string {
73
+ if (typeof args !== "object" || args === null) return ""
74
+ const t = targetOf(args)
75
+ if (t) return t
76
+ try {
77
+ return clip(JSON.stringify(args), MAX_PREVIEW)
78
+ } catch {
79
+ return "[args]"
80
+ }
81
+ }
82
+
83
+ export interface ToolLabel {
84
+ target?: string
85
+ summary: string
86
+ }
87
+
88
+ /**
89
+ * Label semantik tool dari identity + argsSummary (sudah di-parse di adapter).
90
+ * `summary` = baris ledger; `target` = path/$cmd untuk kolom.
91
+ */
92
+ export function labelTool(identity: ToolIdentity, args: ArgsSummary): ToolLabel {
93
+ const name = identity.qualified
94
+ const rawTarget = args.target ?? undefined
95
+ const target = rawTarget ? clip(rawTarget, MAX_TARGET) : undefined
96
+ if (name === "bash" && rawTarget) {
97
+ return { target, summary: clip(`bash ${rawTarget}`, MAX_SUMMARY) }
98
+ }
99
+ if ((name === "todo_write" || name === "todo_read") && args.text) {
100
+ const m = /todos=(\d+)/.exec(args.text)
101
+ const n = m?.[1]
102
+ if (n && n !== "0") return { target, summary: clip(`${name} ${n} items`, MAX_SUMMARY) }
103
+ }
104
+ const summary = target ? `${name} ${target}` : name
105
+ return { target, summary: clip(summary, MAX_SUMMARY) }
106
+ }
107
+
108
+ /** Label status-bar — nama + target, cap MAX_LABEL. */
109
+ export function statusLabel(identity: ToolIdentity, args: ArgsSummary): string {
110
+ const { target } = labelTool(identity, args)
111
+ const line = target ? `${identity.qualified} ${target}` : identity.qualified
112
+ return clip(line, MAX_LABEL)
113
+ }
114
+
115
+ /**
116
+ * Ringkasan hasil tool per jenis — satu baris, cap MAX_SUMMARY.
117
+ * `result` = ToolResult kernel (isError + content) atau string mentah.
118
+ */
119
+ export function summarizeResult(name: string, result: unknown): string {
120
+ if (typeof result === "string") return clip(result, MAX_SUMMARY)
121
+ if (typeof result !== "object" || result === null) return clip(String(result ?? ""), MAX_SUMMARY)
122
+ const r = result as { isError?: boolean; content?: unknown }
123
+ const content = r.content
124
+ let text = ""
125
+ if (typeof content === "string") text = content
126
+ else if (Array.isArray(content)) {
127
+ text = content
128
+ .map((p) => (typeof p === "string" ? p : typeof p?.text === "string" ? p.text : ""))
129
+ .join(" ")
130
+ } else if (content != null) {
131
+ try {
132
+ text = JSON.stringify(content)
133
+ } catch {
134
+ text = String(content)
135
+ }
136
+ }
137
+ const first = text.split(/\r?\n/, 1)[0] ?? ""
138
+ if (r.isError && name === "bash") return clip(`✗ ${first}`, MAX_SUMMARY)
139
+ return clip(first || name, MAX_SUMMARY)
140
+ }
@@ -0,0 +1,172 @@
1
+ // Bentuk state presentasi V2.1 (§14 plan) — murni tipe + konstruktor.
2
+ //
3
+ // Kenapa terpisah dari reducer: proyeksi dan cli boleh mengimpor tipe tanpa
4
+ // menarik logika reduce; coverage floor reducer diuji terpisah. Tanpa IO/
5
+ // clock/env — jam hanya di adapter (tepi).
6
+
7
+ import type {
8
+ ApprovalOutcome,
9
+ ContentRef,
10
+ DenyReason,
11
+ FailCause,
12
+ Receipt,
13
+ ToolIdentity,
14
+ TurnSummary,
15
+ } from "./events.ts"
16
+
17
+ /** Status final tool — `running` hanya live; `interrupted` disimpulkan saat rebuild. */
18
+ export type ToolStatus = "running" | "completed" | "failed" | "denied" | "cancelled" | "interrupted"
19
+
20
+ /** Status final turn — `interrupted` = proses mati tanpa turn-settle durable. */
21
+ export type TurnStatus = "running" | "completed" | "failed" | "cancelled" | "interrupted"
22
+
23
+ export interface ActivityEntry {
24
+ kind: "tool"
25
+ seq: number
26
+ turnId: number
27
+ stepId: number
28
+ toolCallId: string
29
+ sessionId: string
30
+ parentToolCallId?: string
31
+ identity: ToolIdentity
32
+ target?: string
33
+ status: ToolStatus
34
+ tsStart: number
35
+ tsEnd?: number
36
+ durationMs?: number
37
+ /** eventSeq terminal — dasar aturan supersedes (old terminal < new started). */
38
+ endSeq?: number
39
+ progress?: string
40
+ summary?: string
41
+ denyReason?: DenyReason
42
+ error?: { cause: FailCause; message: string; hint?: string }
43
+ supersedes?: string
44
+ approvalId?: string
45
+ expandRef?: ContentRef
46
+ receipt?: Receipt
47
+ /** Terminal tanpa tool.started (race/crash) — observability, jangan throw. */
48
+ incomplete?: boolean
49
+ }
50
+
51
+ export interface TurnEntry {
52
+ kind: "turn"
53
+ seq: number
54
+ turnId: number
55
+ sessionId: string
56
+ status: TurnStatus
57
+ tsStart: number
58
+ tsEnd?: number
59
+ summary?: TurnSummary
60
+ error?: string
61
+ }
62
+
63
+ export interface ApprovalEntry {
64
+ kind: "approval"
65
+ seq: number
66
+ turnId: number
67
+ approvalId: string
68
+ toolCallId: string
69
+ identity: ToolIdentity
70
+ state: "requested" | "settled"
71
+ outcome?: ApprovalOutcome
72
+ }
73
+
74
+ export interface ConversationEntry {
75
+ kind: "message"
76
+ seq: number
77
+ turnId: number
78
+ role: "user" | "assistant"
79
+ text: string
80
+ truncated: boolean
81
+ expandRef?: ContentRef
82
+ }
83
+
84
+ export interface ReasoningEntry {
85
+ kind: "reasoning"
86
+ seq: number
87
+ turnId: number
88
+ truncated: boolean
89
+ expandRef: ContentRef
90
+ }
91
+
92
+ export interface SystemEntry {
93
+ kind: "system"
94
+ seq: number
95
+ turnId: number
96
+ text: string
97
+ }
98
+
99
+ export type PresentationEntry =
100
+ | ActivityEntry
101
+ | TurnEntry
102
+ | ApprovalEntry
103
+ | ConversationEntry
104
+ | ReasoningEntry
105
+ | SystemEntry
106
+
107
+ /** Penunjuk urutan tampil — resolve ke entry via Map/array pemilik. */
108
+ export interface EntryRef {
109
+ kind: PresentationEntry["kind"]
110
+ /** toolCallId | approvalId | `${sessionId}:${turnId}` | `${kind}:${seq}` */
111
+ id: string
112
+ seq: number
113
+ }
114
+
115
+ /**
116
+ * Core state presentasi — kecil, replayable; konten besar hanya via ContentRef
117
+ * (ContentStore sibling Fase 4, bukan nested di sini).
118
+ */
119
+ export interface PresentationState {
120
+ sessionId: string
121
+ /** eventSeq terakhir yang diterapkan — basis replay/determinisme. */
122
+ seq: number
123
+ /** key = `${sessionId}:${turnId}` (anak numbering sendiri). */
124
+ turns: Map<string, TurnEntry>
125
+ /** key = `${sessionId}:${toolCallId}`. */
126
+ activities: Map<string, ActivityEntry>
127
+ approvals: Map<string, ApprovalEntry>
128
+ conversation: ConversationEntry[]
129
+ order: EntryRef[]
130
+ }
131
+
132
+ export function turnKey(sessionId: string, turnId: number): string {
133
+ return `${sessionId}:${turnId}`
134
+ }
135
+
136
+ export function activityKey(sessionId: string, toolCallId: string): string {
137
+ return `${sessionId}:${toolCallId}`
138
+ }
139
+
140
+ export function createInitialState(sessionId: string): PresentationState {
141
+ return {
142
+ sessionId,
143
+ seq: 0,
144
+ turns: new Map(),
145
+ activities: new Map(),
146
+ approvals: new Map(),
147
+ conversation: [],
148
+ order: [],
149
+ }
150
+ }
151
+
152
+ /** Snapshot deep-equal-friendly (test determinisme/replay; bukan jalur panas). */
153
+ export function cloneState(s: PresentationState): PresentationState {
154
+ return {
155
+ sessionId: s.sessionId,
156
+ seq: s.seq,
157
+ turns: new Map([...s.turns].map(([k, v]) => [k, { ...v }])),
158
+ activities: new Map(
159
+ [...s.activities].map(([k, v]) => [
160
+ k,
161
+ {
162
+ ...v,
163
+ error: v.error ? { ...v.error } : undefined,
164
+ receipt: v.receipt ? { ...v.receipt } : undefined,
165
+ },
166
+ ]),
167
+ ),
168
+ approvals: new Map([...s.approvals].map(([k, v]) => [k, { ...v }])),
169
+ conversation: s.conversation.map((m) => ({ ...m })),
170
+ order: s.order.map((o) => ({ ...o })),
171
+ }
172
+ }