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.
- package/AGENTS.md +45 -0
- package/CHANGELOG.md +59 -4
- package/PLAN.md +491 -0
- package/README.md +2 -2
- package/cli/commands/acp.ts +485 -0
- package/cli/commands/auth.ts +7 -4
- package/cli/commands/config.ts +41 -27
- package/cli/commands/exec.ts +33 -12
- package/cli/commands/mcp.ts +3 -2
- package/cli/commands/memory.ts +1 -1
- package/cli/commands/pricing.ts +6 -4
- package/cli/commands/providers.ts +18 -8
- package/cli/commands/sessions.ts +11 -4
- package/cli/commands/skills.ts +3 -3
- package/cli/commands/stats.ts +3 -1
- package/cli/commands.ts +6 -0
- package/cli/index.ts +82 -57
- package/cli/model-manager.ts +6 -1
- package/cli/router.ts +7 -0
- package/cli/setup.ts +341 -5
- package/cli/tui.ts +54 -18
- package/docs/ARCHITECTURE.html +29 -22
- package/docs/CONTROL-PLANE-MAP.md +139 -0
- package/docs/HARNESS.md +150 -0
- package/docs/PLAN_UIUX_V6.md +209 -0
- package/docs/README.md +1 -1
- package/docs/SUMMARY.md +52 -0
- package/docs/TERMINAL_CONTRACT.md +367 -0
- package/docs/UI_RENDER_PIPELINE.md +165 -0
- package/docs/USAGE.md +11 -0
- package/docs/agents.md +31 -0
- package/docs/architecture.md +3 -3
- package/docs/choosing-mode.md +34 -0
- package/docs/cli.md +130 -0
- package/docs/concepts.md +53 -0
- package/docs/config-providers.md +87 -0
- package/docs/config.md +63 -0
- package/docs/contributing.md +79 -0
- package/docs/environment.md +95 -0
- package/docs/exec.md +73 -0
- package/docs/getting-started.md +102 -0
- package/docs/glossary.md +37 -0
- package/docs/mcp-lsp.md +54 -0
- package/docs/memory-sessions.md +63 -0
- package/docs/policy-sandbox.md +72 -0
- package/docs/pricing-budget.md +49 -0
- package/docs/quickstart.md +60 -0
- package/docs/repl.md +67 -0
- package/docs/security-model.md +95 -0
- package/docs/security.md +91 -0
- package/docs/skills.md +48 -0
- package/docs/terminal.md +79 -0
- package/docs/tools.md +123 -0
- package/docs/troubleshooting.md +40 -0
- package/docs/verify-benchmark.md +58 -0
- package/package.json +8 -5
- package/src/app/provider-layer.ts +12 -4
- package/src/app/session.ts +6 -0
- package/src/app/tool-layer.ts +2 -1
- package/src/constants.ts +35 -0
- package/src/lib/net.ts +6 -1
- package/src/lsp/client.ts +41 -8
- package/src/mcp/http-transport.ts +52 -15
- package/src/mcp/transport.ts +32 -1
- package/src/memory/files.ts +6 -5
- package/src/policy/allowlist.ts +1 -1
- package/src/policy/bash-guard.ts +154 -14
- package/src/policy/compaction.ts +57 -3
- package/src/policy/context.ts +4 -0
- package/src/policy/jail.ts +49 -4
- package/src/policy/permission.ts +152 -12
- package/src/policy/pricing.ts +12 -9
- package/src/presentation/adapter.ts +721 -0
- package/src/presentation/events.ts +324 -0
- package/src/presentation/label.ts +140 -0
- package/src/presentation/model.ts +172 -0
- package/src/presentation/reducer.ts +486 -0
- package/src/presentation/store.ts +256 -0
- package/src/providers/anthropic.ts +22 -4
- package/src/providers/detect.ts +2 -1
- package/src/providers/router.ts +13 -0
- package/src/repo/repomap.ts +2 -29
- package/src/session/checkpoint.ts +27 -9
- package/src/session/journal.ts +27 -5
- package/src/skills/loader.ts +88 -5
- package/src/telemetry/trace.ts +36 -15
- package/src/tools/ask_user.ts +73 -4
- package/src/tools/bash.ts +15 -17
- package/src/tools/glob.ts +10 -11
- package/src/tools/lsp.ts +3 -56
- package/src/tools/memory.ts +13 -12
- package/src/tools/task.ts +1 -0
- package/src/ui/approval/prompt.ts +14 -6
- package/src/ui/assistant/simple.ts +137 -26
- package/src/ui/assistant/turn-status.ts +16 -3
- package/src/ui/contract.ts +119 -0
- package/src/ui/footer.ts +21 -2
- package/src/ui/i18n/en.ts +34 -6
- package/src/ui/i18n/id.ts +34 -6
- package/src/ui/i18n/locale.ts +3 -1
- package/src/ui/input/input.ts +51 -4
- package/src/ui/input/prompt-engine.ts +46 -4
- package/src/ui/render/collapse.ts +13 -13
- package/src/ui/render/diff.ts +7 -2
- package/src/ui/render/format.ts +16 -8
- package/src/ui/render/highlight.ts +7 -3
- package/src/ui/render/markdown.ts +5 -1
- package/src/ui/render/sanitize.ts +29 -2
- package/src/ui/render/table.ts +8 -5
- package/src/ui/render/theme.ts +18 -4
- package/src/ui/render/width.ts +89 -10
- package/src/ui/runtime/motion.ts +16 -0
- package/src/ui/runtime/screen.ts +45 -0
- package/src/ui/runtime/spinner.ts +50 -4
- package/src/ui/runtime/statusline.ts +50 -1
- package/src/ui/screens/form.ts +42 -15
- package/src/ui/screens/model-manager.ts +26 -18
- package/src/ui/screens/picker.ts +47 -23
- package/src/ui/screens/provider-manager.ts +18 -12
- package/src/ui/screens/wizard.ts +24 -8
- package/src/ui/tui/app.ts +233 -27
- package/src/ui/tui/transcript.ts +269 -65
- package/vendor/minicore/LICENSE +21 -0
- package/vendor/minicore/VENDOR.md +6 -3
- package/vendor/minicore/package.json +6 -3
- package/vendor/minicore/src/core/loop.ts +12 -2
- package/vendor/minicore/src/core/snapshot.ts +4 -0
- package/vendor/minicore/src/providers/openai-compat.ts +76 -25
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto"
|
|
2
|
+
import { resolve as resolvePath } from "node:path"
|
|
3
|
+
import { createInterface } from "node:readline"
|
|
4
|
+
import { LIMITS } from "../../src/constants.ts"
|
|
5
|
+
import { scrubSecrets } from "../../src/policy/scrub.ts"
|
|
6
|
+
import { budgetStatus } from "../../src/policy/usage.ts"
|
|
7
|
+
import { clearSubmittedResult, getSubmittedResult } from "../../src/tools/submit_result.ts"
|
|
8
|
+
import { formatError } from "../../src/ui/assistant/simple.ts"
|
|
9
|
+
import type { UiPresentationEvent, UiPresentationSnapshot } from "../../src/ui/contract.ts"
|
|
10
|
+
import { formatUsd } from "../../src/ui/render/money.ts"
|
|
11
|
+
import { createCliSession } from "../setup.ts"
|
|
12
|
+
|
|
13
|
+
// minicode acp — server JSON-RPC via stdio untuk IDE (Fase 5, subset minimal).
|
|
14
|
+
//
|
|
15
|
+
// BUKAN klaim kompatibel ACP penuh (Zed/JetBrains): hanya tiga method yang
|
|
16
|
+
// didokumentasikan di bawah. Protokol: satu JSON per baris di stdin;
|
|
17
|
+
// respons/notifikasi satu JSON per baris di stdout. stdout WAJIB murni
|
|
18
|
+
// mesin — semua diagnostik manusia ke stderr (kontrak I2/I6).
|
|
19
|
+
//
|
|
20
|
+
// -> {"id":1,"method":"initialize","params":{"client":"zed"}}
|
|
21
|
+
// <- {"id":1,"result":{"server":"minicode-acp","capabilities":{...}}}
|
|
22
|
+
// -> {"id":2,"method":"run","params":{"prompt":"...","cwd":"...","model":"...","maxSteps":50,"timeoutMs":600000,"budget":0.5,"mode":"auto"|"plan"}}
|
|
23
|
+
// <- {"type":"text","delta":"..."} ... (notifikasi, tanpa id)
|
|
24
|
+
// <- {"type":"tool","name":"read_file"} ... (notifikasi legacy, tanpa id)
|
|
25
|
+
// <- {"type":"tool.started","toolCallId":"...","name":"read_file"} ...
|
|
26
|
+
// <- {"type":"tool.completed|failed|denied|cancelled",...} ...
|
|
27
|
+
// <- {"type":"approval.requested|settled",...} ...
|
|
28
|
+
// <- {"type":"turn.started|completed|failed|cancelled",...} ...
|
|
29
|
+
// <- {"id":2,"result":{"ok":true,"tokens":123,"steps":4,"turns":1,"text":"..."}}
|
|
30
|
+
// -> {"id":3,"method":"cancel"} (menggugurkan run yang berjalan)
|
|
31
|
+
// -> {"id":4,"method":"shutdown"} (keluar 0 setelah run selesai/dibatalkan)
|
|
32
|
+
//
|
|
33
|
+
// Batasan v1 yang jujur: tiap `run` = sesi BARU (tanpa thread/resume);
|
|
34
|
+
// approval interaktif tak ada — kebijakan headless fail-closed berlaku
|
|
35
|
+
// (tool gated tanpa TTY = deny, sama seperti exec/CI); hanya SATU run dalam
|
|
36
|
+
// terbang (run kedua ditolak dengan error, bukan antre).
|
|
37
|
+
|
|
38
|
+
export interface AcpRequest {
|
|
39
|
+
id?: number | string
|
|
40
|
+
method?: string
|
|
41
|
+
params?: Record<string, unknown>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Parse satu baris stdin: JSON valid + method string, selain itu null
|
|
45
|
+
* (baris rusak diabaikan + warn ke stderr agar stream mesin tetap bersih).
|
|
46
|
+
* Murni + diekspor untuk test. */
|
|
47
|
+
export function parseAcpLine(line: string): AcpRequest | null {
|
|
48
|
+
const t = line.trim()
|
|
49
|
+
if (!t) return null
|
|
50
|
+
try {
|
|
51
|
+
const o = JSON.parse(t) as AcpRequest
|
|
52
|
+
if (o && typeof o === "object" && typeof o.method === "string") return o
|
|
53
|
+
return null
|
|
54
|
+
} catch {
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function acpOk(id: number | string | undefined, result: unknown): string {
|
|
60
|
+
return JSON.stringify({ id: id ?? null, result })
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function acpErr(id: number | string | undefined, message: string): string {
|
|
64
|
+
return JSON.stringify({ id: id ?? null, error: { message } })
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface AcpRunParams {
|
|
68
|
+
prompt: string
|
|
69
|
+
cwd?: string
|
|
70
|
+
model?: string
|
|
71
|
+
maxSteps?: number
|
|
72
|
+
timeoutMs?: number
|
|
73
|
+
budget?: number
|
|
74
|
+
mode?: "auto" | "plan"
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Validasi params `run`: prompt wajib non-kosong; angka harus hingga-positif;
|
|
78
|
+
* mode hanya auto/plan. Murni + diekspor untuk test. */
|
|
79
|
+
export function parseRunParams(
|
|
80
|
+
params: unknown,
|
|
81
|
+
): { ok: true; value: AcpRunParams } | { ok: false; error: string } {
|
|
82
|
+
if (!params || typeof params !== "object") return { ok: false, error: "params must be an object" }
|
|
83
|
+
const p = params as Record<string, unknown>
|
|
84
|
+
const prompt = typeof p.prompt === "string" ? p.prompt.trim() : ""
|
|
85
|
+
if (!prompt) return { ok: false, error: "params.prompt is required" }
|
|
86
|
+
const num = (v: unknown): number | undefined =>
|
|
87
|
+
typeof v === "number" && Number.isFinite(v) && v > 0 ? v : undefined
|
|
88
|
+
if (p.maxSteps !== undefined && num(p.maxSteps) === undefined)
|
|
89
|
+
return { ok: false, error: "params.maxSteps must be a positive number" }
|
|
90
|
+
if (p.timeoutMs !== undefined && num(p.timeoutMs) === undefined)
|
|
91
|
+
return { ok: false, error: "params.timeoutMs must be a positive number" }
|
|
92
|
+
if (
|
|
93
|
+
p.budget !== undefined &&
|
|
94
|
+
!(typeof p.budget === "number" && Number.isFinite(p.budget) && p.budget >= 0)
|
|
95
|
+
)
|
|
96
|
+
return { ok: false, error: "params.budget must be a non-negative number" }
|
|
97
|
+
const mode =
|
|
98
|
+
p.mode === undefined || p.mode === "auto" ? "auto" : p.mode === "plan" ? "plan" : null
|
|
99
|
+
if (mode === null) return { ok: false, error: 'params.mode must be "auto" or "plan"' }
|
|
100
|
+
const out: AcpRunParams = { prompt, mode }
|
|
101
|
+
if (typeof p.cwd === "string" && p.cwd) out.cwd = p.cwd
|
|
102
|
+
if (typeof p.model === "string" && p.model) out.model = p.model
|
|
103
|
+
const ms = num(p.maxSteps)
|
|
104
|
+
if (ms !== undefined) out.maxSteps = Math.floor(ms)
|
|
105
|
+
const to = num(p.timeoutMs)
|
|
106
|
+
if (to !== undefined) out.timeoutMs = Math.floor(to)
|
|
107
|
+
if (typeof p.budget === "number") out.budget = p.budget
|
|
108
|
+
return { ok: true, value: out }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function projectAcpLifecycle(
|
|
112
|
+
event: UiPresentationEvent,
|
|
113
|
+
snapshot?: UiPresentationSnapshot | null,
|
|
114
|
+
): Record<string, unknown> | null {
|
|
115
|
+
const activity = event.toolCallId
|
|
116
|
+
? snapshot?.activities.find((item) => item.toolCallId === event.toolCallId)
|
|
117
|
+
: undefined
|
|
118
|
+
const base = {
|
|
119
|
+
type: event.type,
|
|
120
|
+
...(event.seq !== undefined ? { eventSeq: event.seq } : {}),
|
|
121
|
+
...(event.turnId !== undefined ? { turnId: event.turnId } : {}),
|
|
122
|
+
}
|
|
123
|
+
if (event.type === "turn.started") return base
|
|
124
|
+
if (event.type === "turn.completed")
|
|
125
|
+
return { ...base, ...(event.summary ? { summary: event.summary } : {}) }
|
|
126
|
+
if (event.type === "turn.failed")
|
|
127
|
+
return {
|
|
128
|
+
...base,
|
|
129
|
+
...(event.error ? { error: event.error } : {}),
|
|
130
|
+
...(event.cause ? { cause: event.cause } : {}),
|
|
131
|
+
}
|
|
132
|
+
if (event.type === "turn.cancelled")
|
|
133
|
+
return { ...base, ...(event.reason ? { reason: event.reason } : {}) }
|
|
134
|
+
if (event.type === "approval.requested")
|
|
135
|
+
return {
|
|
136
|
+
...base,
|
|
137
|
+
...(event.approvalId ? { approvalId: event.approvalId } : {}),
|
|
138
|
+
...(event.toolCallId ? { toolCallId: event.toolCallId } : {}),
|
|
139
|
+
...(event.name ? { name: event.name } : {}),
|
|
140
|
+
...(event.qualified ? { qualified: event.qualified } : {}),
|
|
141
|
+
...(event.target ? { target: event.target } : {}),
|
|
142
|
+
...(event.via ? { via: event.via } : {}),
|
|
143
|
+
}
|
|
144
|
+
if (event.type === "approval.settled")
|
|
145
|
+
return {
|
|
146
|
+
...base,
|
|
147
|
+
...(event.approvalId ? { approvalId: event.approvalId } : {}),
|
|
148
|
+
...(event.toolCallId ? { toolCallId: event.toolCallId } : {}),
|
|
149
|
+
...(event.outcome ? { outcome: event.outcome } : {}),
|
|
150
|
+
}
|
|
151
|
+
if (event.type === "tool.started")
|
|
152
|
+
return {
|
|
153
|
+
...base,
|
|
154
|
+
...(event.toolCallId ? { toolCallId: event.toolCallId } : {}),
|
|
155
|
+
...(event.name ? { name: event.name } : {}),
|
|
156
|
+
...(event.qualified ? { qualified: event.qualified } : {}),
|
|
157
|
+
...(event.target ? { target: event.target } : {}),
|
|
158
|
+
status: "running",
|
|
159
|
+
...(event.tsStart !== undefined ? { tsStart: event.tsStart } : {}),
|
|
160
|
+
}
|
|
161
|
+
if (
|
|
162
|
+
event.type === "tool.completed" ||
|
|
163
|
+
event.type === "tool.failed" ||
|
|
164
|
+
event.type === "tool.denied" ||
|
|
165
|
+
event.type === "tool.cancelled"
|
|
166
|
+
) {
|
|
167
|
+
return {
|
|
168
|
+
...base,
|
|
169
|
+
...(event.toolCallId ? { toolCallId: event.toolCallId } : {}),
|
|
170
|
+
...((event.name ?? activity?.name) ? { name: event.name ?? activity?.name } : {}),
|
|
171
|
+
...((event.qualified ?? activity?.qualified)
|
|
172
|
+
? { qualified: event.qualified ?? activity?.qualified }
|
|
173
|
+
: {}),
|
|
174
|
+
...((event.target ?? activity?.target) ? { target: event.target ?? activity?.target } : {}),
|
|
175
|
+
...((event.status ?? activity?.status) ? { status: event.status ?? activity?.status } : {}),
|
|
176
|
+
...((event.durationMs ?? activity?.durationMs)
|
|
177
|
+
? { durationMs: event.durationMs ?? activity?.durationMs }
|
|
178
|
+
: {}),
|
|
179
|
+
...(event.message ? { message: event.message } : {}),
|
|
180
|
+
...(event.cause ? { cause: event.cause } : {}),
|
|
181
|
+
...(event.reason ? { reason: event.reason } : {}),
|
|
182
|
+
...(event.type === "tool.failed" && activity?.error ? { error: activity.error } : {}),
|
|
183
|
+
...(activity?.denyReason ? { denyReason: activity.denyReason } : {}),
|
|
184
|
+
...(activity?.receipt ? { receipt: activity.receipt } : {}),
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return null
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Satu run dalam terbang; cancel menggugurkan via signal. Box bermethod
|
|
191
|
+
* (bukan `let` telanjang): assignment terjadi di closure runAcpSession
|
|
192
|
+
* sehingga narrowing TS tak melihatnya — method selalu mulai dari tipe
|
|
193
|
+
* deklarasi. Diekspor agar interaksi cancel-during-run teruji utuh. */
|
|
194
|
+
export function createFlight(): {
|
|
195
|
+
start: (a: () => void) => void
|
|
196
|
+
active: () => boolean
|
|
197
|
+
stop: () => boolean
|
|
198
|
+
clear: () => void
|
|
199
|
+
} {
|
|
200
|
+
let aborter: (() => void) | null = null
|
|
201
|
+
return {
|
|
202
|
+
start(a: () => void): void {
|
|
203
|
+
aborter = a
|
|
204
|
+
},
|
|
205
|
+
active(): boolean {
|
|
206
|
+
return aborter !== null
|
|
207
|
+
},
|
|
208
|
+
/** true bila ada yang digugurkan. */
|
|
209
|
+
stop(): boolean {
|
|
210
|
+
const a = aborter
|
|
211
|
+
aborter = null
|
|
212
|
+
if (a) {
|
|
213
|
+
a()
|
|
214
|
+
return true
|
|
215
|
+
}
|
|
216
|
+
return false
|
|
217
|
+
},
|
|
218
|
+
clear(): void {
|
|
219
|
+
aborter = null
|
|
220
|
+
},
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Dependensi satu `run` — factory sesi di-inject (default produksi) agar
|
|
225
|
+
* alur teruji tanpa provider. Diekspor untuk test. */
|
|
226
|
+
export interface AcpSessionDeps {
|
|
227
|
+
write: (line: string) => void
|
|
228
|
+
/** Dipanggil di finally (sukses/gagal/batal) — pemilik membersihkan flight. */
|
|
229
|
+
onDone: () => void
|
|
230
|
+
startFlight: (abort: () => void) => void
|
|
231
|
+
shouldExit: () => boolean
|
|
232
|
+
exit: (code: number) => void
|
|
233
|
+
createSession?: typeof createCliSession
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Satu `run`: validasi → sesi headless → stream notifikasi → hasil/error
|
|
237
|
+
* tepat sekali. Diekspor untuk test. */
|
|
238
|
+
export async function runAcpSession(
|
|
239
|
+
id: number | string | undefined,
|
|
240
|
+
rawParams: unknown,
|
|
241
|
+
deps: AcpSessionDeps,
|
|
242
|
+
): Promise<void> {
|
|
243
|
+
const { write } = deps
|
|
244
|
+
// Bersihkan state submit_result dari run sebelumnya — tanpa ini, run
|
|
245
|
+
// ACP berturut-turut bisa membaca hasil run lama.
|
|
246
|
+
clearSubmittedResult()
|
|
247
|
+
const createSession = deps.createSession ?? createCliSession
|
|
248
|
+
const parsed = parseRunParams(rawParams)
|
|
249
|
+
if (!parsed.ok) {
|
|
250
|
+
write(acpErr(id, parsed.error))
|
|
251
|
+
return
|
|
252
|
+
}
|
|
253
|
+
const rp = parsed.value
|
|
254
|
+
const sessionId = randomUUID().slice(0, 8)
|
|
255
|
+
const ac = new AbortController()
|
|
256
|
+
deps.startFlight(() => ac.abort())
|
|
257
|
+
const t0 = Date.now()
|
|
258
|
+
let text = ""
|
|
259
|
+
try {
|
|
260
|
+
const ctx = await createSession({
|
|
261
|
+
cwd: rp.cwd ? resolvePath(rp.cwd) : undefined,
|
|
262
|
+
sessionId,
|
|
263
|
+
modelOverride: rp.model,
|
|
264
|
+
prompt: rp.prompt,
|
|
265
|
+
enterRepl: false,
|
|
266
|
+
verbose: false,
|
|
267
|
+
allowAll: false,
|
|
268
|
+
ask: false,
|
|
269
|
+
plan: rp.mode === "plan",
|
|
270
|
+
allowlist: false,
|
|
271
|
+
verify: false,
|
|
272
|
+
budget: rp.budget,
|
|
273
|
+
budgetStrict: false,
|
|
274
|
+
...(rp.maxSteps ? { maxSteps: rp.maxSteps } : {}),
|
|
275
|
+
...(rp.timeoutMs ? { timeoutMs: rp.timeoutMs } : {}),
|
|
276
|
+
})
|
|
277
|
+
const projection = ctx as unknown as {
|
|
278
|
+
onPresentationEvent?: (handler: (event: UiPresentationEvent) => void) => () => void
|
|
279
|
+
getPresentationSnapshot?: () => UiPresentationSnapshot
|
|
280
|
+
}
|
|
281
|
+
const lifecycle = typeof projection.onPresentationEvent === "function"
|
|
282
|
+
let unsubPresentation = (): void => {}
|
|
283
|
+
if (lifecycle) {
|
|
284
|
+
unsubPresentation = projection.onPresentationEvent!((event) => {
|
|
285
|
+
const note = projectAcpLifecycle(event, projection.getPresentationSnapshot?.())
|
|
286
|
+
if (note) write(scrubSecrets(JSON.stringify(note)))
|
|
287
|
+
})
|
|
288
|
+
}
|
|
289
|
+
const unsub = ctx.session.events.on("*", (ev) => {
|
|
290
|
+
try {
|
|
291
|
+
const e = ev as { type?: string; text?: string; execution?: { call?: { name?: string } } }
|
|
292
|
+
if (e.type === "provider:text" && typeof e.text === "string" && e.text) {
|
|
293
|
+
// Cap akumulasi juga (bukan hanya slice akhir): run panjang bisa
|
|
294
|
+
// menumpuk megabyte di memori sebelum result dirakit.
|
|
295
|
+
if (text.length < LIMITS.MCP_OUTPUT_MAX_CHARS) text += e.text
|
|
296
|
+
write(scrubSecrets(JSON.stringify({ type: "text", delta: e.text })))
|
|
297
|
+
} else if (e.type === "execution:started" && !lifecycle) {
|
|
298
|
+
const name = e.execution?.call?.name ?? "?"
|
|
299
|
+
write(JSON.stringify({ type: "tool", name }))
|
|
300
|
+
}
|
|
301
|
+
} catch {}
|
|
302
|
+
})
|
|
303
|
+
try {
|
|
304
|
+
await ctx.runPromptWithVerify(rp.prompt, ac.signal)
|
|
305
|
+
const ue = ctx.usage.getSession(ctx.modelRef.current)
|
|
306
|
+
const bStatus = budgetStatus(rp.budget, ue.cost, false, ue.totalTokens)
|
|
307
|
+
if (bStatus !== "ok") {
|
|
308
|
+
const msg =
|
|
309
|
+
bStatus === "over" && ue.cost != null && rp.budget != null
|
|
310
|
+
? `[budget] ${formatUsd(ue.cost)} > ${formatUsd(rp.budget)} - over budget, stopping.`
|
|
311
|
+
: `[budget] cost unknown (model without pricing) with ${ue.totalTokens} tokens spent - over budget, stopping.`
|
|
312
|
+
write(acpErr(id, msg))
|
|
313
|
+
} else {
|
|
314
|
+
const submitted = getSubmittedResult()
|
|
315
|
+
// submitted verbatim seperti exec --json (pipeline tak menebak
|
|
316
|
+
// batas); teks dis-scrub + di-cap (bisa megabyte).
|
|
317
|
+
write(
|
|
318
|
+
acpOk(id, {
|
|
319
|
+
ok: true,
|
|
320
|
+
sessionId,
|
|
321
|
+
model: ctx.modelRef.current,
|
|
322
|
+
tokens: ue.totalTokens,
|
|
323
|
+
inputTokens: ue.inputTokens,
|
|
324
|
+
outputTokens: ue.outputTokens,
|
|
325
|
+
...(ue.cost !== undefined ? { cost: ue.cost } : {}),
|
|
326
|
+
steps: ctx.session.state.stepCount,
|
|
327
|
+
turns: ctx.session.state.turnCount,
|
|
328
|
+
durationMs: Date.now() - t0,
|
|
329
|
+
text: scrubSecrets(text.slice(0, LIMITS.MCP_OUTPUT_MAX_CHARS)),
|
|
330
|
+
...(submitted ? { submitted: submitted.result } : {}),
|
|
331
|
+
}),
|
|
332
|
+
)
|
|
333
|
+
}
|
|
334
|
+
} finally {
|
|
335
|
+
unsub()
|
|
336
|
+
unsubPresentation()
|
|
337
|
+
await ctx.close()
|
|
338
|
+
}
|
|
339
|
+
} catch (e) {
|
|
340
|
+
// Respons error tepat sekali per run: setup gagal (termasuk tanpa
|
|
341
|
+
// provider — koneksi tetap hidup untuk request berikut), run gagal,
|
|
342
|
+
// atau dibatalkan user. formatError menutupi NoProviderError juga.
|
|
343
|
+
const msg = ac.signal.aborted ? "run cancelled" : formatError(e)
|
|
344
|
+
write(acpErr(id, msg))
|
|
345
|
+
} finally {
|
|
346
|
+
deps.onDone()
|
|
347
|
+
if (deps.shouldExit()) deps.exit(0)
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** State loop yang dibutuhkan dispatch satu baris — stdio tetap di
|
|
352
|
+
* handleAcp; yang di sini murni logika pesan. Diekspor untuk test. */
|
|
353
|
+
export interface AcpLoop {
|
|
354
|
+
write: (line: string) => void
|
|
355
|
+
runOne: (id: number | string | undefined, params: unknown) => Promise<void>
|
|
356
|
+
flightActive: () => boolean
|
|
357
|
+
cancelFlight: () => boolean
|
|
358
|
+
isShuttingDown: () => boolean
|
|
359
|
+
beginShutdown: () => void
|
|
360
|
+
exit: (code: number) => void
|
|
361
|
+
warnMalformed: () => void
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Satu baris stdin → aksi. Diekspor agar loop teruji tanpa stdio. */
|
|
365
|
+
export async function dispatchAcpLine(line: string, loop: AcpLoop): Promise<void> {
|
|
366
|
+
const req = parseAcpLine(line)
|
|
367
|
+
if (!req) {
|
|
368
|
+
if (line.trim()) loop.warnMalformed()
|
|
369
|
+
return
|
|
370
|
+
}
|
|
371
|
+
if (req.method === "initialize") {
|
|
372
|
+
loop.write(
|
|
373
|
+
acpOk(req.id, {
|
|
374
|
+
server: "minicode-acp",
|
|
375
|
+
subset: "minicode-1",
|
|
376
|
+
capabilities: { run: true, streamText: true, cancel: true, approval: "deny-headless" },
|
|
377
|
+
note: "Minimal subset, not full ACP. Each run = a fresh session. Gated tools are denied without TTY.",
|
|
378
|
+
}),
|
|
379
|
+
)
|
|
380
|
+
} else if (req.method === "run") {
|
|
381
|
+
if (loop.flightActive()) {
|
|
382
|
+
loop.write(acpErr(req.id, "a run is already in flight (v1: one at a time)"))
|
|
383
|
+
return
|
|
384
|
+
}
|
|
385
|
+
if (loop.isShuttingDown()) {
|
|
386
|
+
loop.write(acpErr(req.id, "server is shutting down"))
|
|
387
|
+
return
|
|
388
|
+
}
|
|
389
|
+
// Dispatch ini await runOne (satu run per pemanggilan). Konkruensi
|
|
390
|
+
// cancel-while-running datang dari pemanggil (handleAcp) yang TIDAK
|
|
391
|
+
// await dispatch — prefik sinkron sampai startFlight tetap berurutan.
|
|
392
|
+
await loop.runOne(req.id, req.params)
|
|
393
|
+
} else if (req.method === "cancel") {
|
|
394
|
+
loop.write(acpOk(req.id, { cancelled: loop.cancelFlight() }))
|
|
395
|
+
} else if (req.method === "shutdown") {
|
|
396
|
+
loop.beginShutdown()
|
|
397
|
+
if (loop.flightActive()) {
|
|
398
|
+
loop.cancelFlight()
|
|
399
|
+
loop.write(acpOk(req.id, { shuttingDown: true }))
|
|
400
|
+
// exit(0) terjadi di finally runAcpSession via shouldExit.
|
|
401
|
+
} else {
|
|
402
|
+
loop.write(acpOk(req.id, { shuttingDown: true }))
|
|
403
|
+
loop.exit(0)
|
|
404
|
+
}
|
|
405
|
+
} else {
|
|
406
|
+
loop.write(acpErr(req.id, `unknown method: ${req.method}`))
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export async function handleAcp(): Promise<void> {
|
|
411
|
+
const write = (line: string): void => {
|
|
412
|
+
// Klien bisa mati di tengah run (pipe tertutup): tulis yang gagal
|
|
413
|
+
// jangan melempar dan membunuh server dengan observasi setengah jalan.
|
|
414
|
+
try {
|
|
415
|
+
process.stdout.write(`${line}\n`)
|
|
416
|
+
} catch {}
|
|
417
|
+
}
|
|
418
|
+
const flight = createFlight()
|
|
419
|
+
let shuttingDown = false
|
|
420
|
+
// Run sedang berjalan (termasuk antrean setup): flight.active() saja TIDAK
|
|
421
|
+
// cukup — stop() mengosongkan aborter sementara run masih await, sehingga
|
|
422
|
+
// close mengira tak ada run dan exit prematur (respons run hilang).
|
|
423
|
+
let busy = false
|
|
424
|
+
const runOne = async (id: number | string | undefined, rawParams: unknown): Promise<void> => {
|
|
425
|
+
busy = true
|
|
426
|
+
try {
|
|
427
|
+
await runAcpSession(id, rawParams, {
|
|
428
|
+
write,
|
|
429
|
+
onDone: () => flight.clear(),
|
|
430
|
+
startFlight: (abort) => flight.start(abort),
|
|
431
|
+
shouldExit: () => shuttingDown,
|
|
432
|
+
exit: (code) => process.exit(code),
|
|
433
|
+
})
|
|
434
|
+
} finally {
|
|
435
|
+
busy = false
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
const loop: AcpLoop = {
|
|
439
|
+
write,
|
|
440
|
+
runOne,
|
|
441
|
+
flightActive: () => flight.active(),
|
|
442
|
+
cancelFlight: () => flight.stop(),
|
|
443
|
+
isShuttingDown: () => shuttingDown,
|
|
444
|
+
beginShutdown: () => {
|
|
445
|
+
shuttingDown = true
|
|
446
|
+
},
|
|
447
|
+
exit: (code) => process.exit(code),
|
|
448
|
+
warnMalformed: () => process.stderr.write("[acp] ignoring malformed line\n"),
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const rl = createInterface({ input: process.stdin, crlfDelay: Infinity })
|
|
452
|
+
// Event-driven, BUKAN for-await sekuensial: `cancel`/`shutdown` harus bisa
|
|
453
|
+
// menyela run yang berjalan. for-await menahan baris berikutnya sampai run
|
|
454
|
+
// selesai — cancel di stdin tak pernah terbaca (fitur mati di produksi).
|
|
455
|
+
// Tiap baris didispatch tanpa await; prefik sinkron dispatch (parse →
|
|
456
|
+
// runOne → startFlight) tetap berurutan per event, jadi run kedua melihat
|
|
457
|
+
// flight aktif dan ditolak jujur.
|
|
458
|
+
let inflight: Promise<void> | null = null
|
|
459
|
+
rl.on("line", (line: string) => {
|
|
460
|
+
const p = dispatchAcpLine(line, loop).catch((e: unknown) => {
|
|
461
|
+
// runOne tak pernah melempar (semua jalur jadi respons error), tapi
|
|
462
|
+
// jangan biarkan satu baris jahat membunuh server diam-diam.
|
|
463
|
+
process.stderr.write(`[acp] dispatch failed: ${(e as Error)?.message ?? e}\n`)
|
|
464
|
+
})
|
|
465
|
+
inflight = p
|
|
466
|
+
void p.finally(() => {
|
|
467
|
+
if (inflight === p) inflight = null
|
|
468
|
+
})
|
|
469
|
+
})
|
|
470
|
+
// Klien menutup stdin (atau EOF): run yang masih berjalan digugurkan
|
|
471
|
+
// lalu keluar lewat finally sesi (shouldExit); tanpa run langsung bersih.
|
|
472
|
+
rl.on("close", () => {
|
|
473
|
+
if (busy) {
|
|
474
|
+
shuttingDown = true
|
|
475
|
+
flight.stop()
|
|
476
|
+
} else {
|
|
477
|
+
process.exit(0)
|
|
478
|
+
}
|
|
479
|
+
})
|
|
480
|
+
// Server hidup selama proses hidup: JANGAN pernah return — kembalinya
|
|
481
|
+
// handleAcp membuat dispatch selesai dan index.ts lanjut membuat sesi
|
|
482
|
+
// one-shot "acp" (NoProviderError + exit 1) yang berlomba dengan stdin.
|
|
483
|
+
// Semua jalan keluar lewat process.exit di atas/di dispatch.
|
|
484
|
+
await new Promise<never>(() => {})
|
|
485
|
+
}
|
package/cli/commands/auth.ts
CHANGED
|
@@ -57,10 +57,12 @@ export async function handleAuth(args: string[]): Promise<never> {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
if (sub === "logout") {
|
|
60
|
-
|
|
60
|
+
// Tolak flag sebagai id seperti config (positionalArg): `auth logout
|
|
61
|
+
// --cwd X` adalah lupa argumen, bukan kredensial bernama "--cwd".
|
|
62
|
+
const id = args[2] && !args[2]!.startsWith("-") ? args[2] : undefined
|
|
61
63
|
if (!id) {
|
|
62
64
|
console.error("usage: minicode auth logout <provider>")
|
|
63
|
-
process.exit(
|
|
65
|
+
process.exit(2)
|
|
64
66
|
}
|
|
65
67
|
const ok = await removeAuth(id)
|
|
66
68
|
console.log(
|
|
@@ -70,9 +72,10 @@ export async function handleAuth(args: string[]): Promise<never> {
|
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
if (sub !== "login") {
|
|
73
|
-
// Subcommand asing = salah pakai, bukan permintaan bantuan
|
|
75
|
+
// Subcommand asing = salah pakai (exit 2), bukan permintaan bantuan
|
|
76
|
+
// dan bukan gagal runtime (exit 1).
|
|
74
77
|
console.error(`unknown auth subcommand: ${sub}`)
|
|
75
|
-
usage(
|
|
78
|
+
usage(2)
|
|
76
79
|
}
|
|
77
80
|
|
|
78
81
|
// ── login ──
|