minicode-ai 0.25.0 → 0.27.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/README.md +3 -1
- package/cli/commands/config.ts +123 -0
- package/cli/commands.ts +23 -2
- package/cli/context-command-run.ts +159 -0
- package/cli/context-command.ts +87 -0
- package/cli/index.ts +48 -0
- package/cli/setup.ts +107 -8
- package/cli/tui.ts +8 -9
- package/docs/TERMINAL_CONTRACT.md +32 -1
- package/docs/config.md +29 -1
- package/docs/environment.md +3 -1
- package/docs/getting-started.md +11 -3
- package/docs/repl.md +1 -0
- package/package.json +1 -1
- package/src/app/session.ts +187 -17
- package/src/config.ts +19 -8
- package/src/constants.ts +33 -4
- package/src/lib/net.ts +6 -15
- package/src/lib/public-http.ts +128 -0
- package/src/policy/auto-resume.ts +63 -0
- package/src/policy/compaction.ts +354 -70
- package/src/policy/context-budget.ts +4 -6
- package/src/policy/context-window-assign.ts +114 -0
- package/src/policy/context-window-catalog.ts +4 -1
- package/src/policy/context-window-presets.ts +165 -0
- package/src/policy/context-window.ts +105 -4
- package/src/policy/context.ts +27 -10
- package/src/policy/executor.ts +69 -27
- package/src/policy/microcompact.ts +76 -15
- package/src/policy/recovery-budget.ts +281 -0
- package/src/policy/step-escalation.ts +82 -0
- package/src/policy/tokenizer-registry.ts +1 -0
- package/src/providers/detect.ts +13 -7
- package/src/providers/guards.ts +41 -2
- package/src/providers/provision.ts +8 -4
- package/src/repo/repomap.ts +180 -23
- package/src/runtime/child-execution.ts +4 -2
- package/src/runtime/execution-backend.ts +3 -0
- package/src/sandbox/os.ts +53 -9
- package/src/session/context-assembly.ts +11 -7
- package/src/session/persistence.ts +19 -3
- package/src/tools/bash.ts +12 -9
- package/src/tools/code_run.ts +19 -20
- package/src/tools/task.ts +4 -1
- package/src/tools/web_fetch.ts +13 -19
- package/src/ui/assistant/simple.ts +61 -10
- package/src/ui/i18n/en.ts +15 -0
- package/src/ui/i18n/id.ts +15 -0
- package/src/ui/input/prompt-engine.ts +53 -21
- package/src/ui/render/activity-group.ts +8 -0
- package/src/ui/render/activity-panel.ts +20 -2
- package/src/ui/screens/model-manager.ts +6 -1
- package/src/ui/tui/app.ts +26 -11
- package/src/ui/tui/transcript.ts +175 -29
- package/vendor/minicore/src/core/loop.ts +20 -1
- package/vendor/minicore/src/core/session.ts +6 -0
package/README.md
CHANGED
|
@@ -9,10 +9,12 @@ Interactive sessions run as a fullscreen TUI; one-shot and automation runs flow
|
|
|
9
9
|
Requires Bun >=1.1.13 for development, local execution, and the published CLI:
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
|
|
12
|
+
bun add -g minicode-ai
|
|
13
13
|
minicode
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
+
`npm install -g minicode-ai` installs the same binary if you prefer npm.
|
|
17
|
+
|
|
16
18
|
To run from a clone:
|
|
17
19
|
|
|
18
20
|
```bash
|
package/cli/commands/config.ts
CHANGED
|
@@ -10,6 +10,10 @@ import { sanitizeAnsiLine } from "../../src/ui/render/sanitize.ts"
|
|
|
10
10
|
import { renderTable } from "../../src/ui/render/table.ts"
|
|
11
11
|
import { c, glyphs } from "../../src/ui/render/theme.ts"
|
|
12
12
|
|
|
13
|
+
// Akhiran baris untuk tabel yang dicetak bersama judul. Konstanta bernama
|
|
14
|
+
// (bukan `"\n"` lepas) supaya tidak tertukar dengan escape di template literal.
|
|
15
|
+
const NEWLINE = "\n"
|
|
16
|
+
|
|
13
17
|
// Help kontekstual per subcommand. Sebelumnya ketiga cabang ini mencetak HELP
|
|
14
18
|
// global 45 baris dan exit 0 — user tidak tahu apa yang salah dan skrip tidak
|
|
15
19
|
// bisa mendeteksi kegagalan.
|
|
@@ -19,6 +23,10 @@ const CONFIG_HELP = `minicode config — provider, MCP, and LSP
|
|
|
19
23
|
minicode config list
|
|
20
24
|
minicode config remove <id> [--global|--local]
|
|
21
25
|
minicode config detect --baseUrl <url> --apiKey <key>
|
|
26
|
+
minicode config context-window list all explicit windows
|
|
27
|
+
minicode config context-window <provider> windows of one provider
|
|
28
|
+
minicode config context-window <provider> <model> <v> set (128k, 200k, 1m, 1024x1024)
|
|
29
|
+
minicode config context-window <provider> <model> auto back to auto-detection
|
|
22
30
|
minicode config set-key <id> [--global|--local] move API key to OS store (DPAPI on Windows)
|
|
23
31
|
minicode config delete-key <id> forget stored key (provider stays disabled)
|
|
24
32
|
|
|
@@ -151,6 +159,121 @@ export async function handleConfig(
|
|
|
151
159
|
`${c.green(glyphs.check)} Removed provider ${sanitizeAnsiLine(id)} (${!args.includes("--local") ? "global" : "local"})`,
|
|
152
160
|
)
|
|
153
161
|
process.exit(0)
|
|
162
|
+
} else if (sub === "context-window") {
|
|
163
|
+
// Jendela konteks MANUAL untuk model yang providernya tidak melaporkan
|
|
164
|
+
// kapasitas. Tanpa ini satu-satunya jalan adalah mengedit JSON dan menebak
|
|
165
|
+
// angka; salah tebak ke atas = request ditolak 400 di tengah turn, ke bawah
|
|
166
|
+
// = kompaksi prematur.
|
|
167
|
+
//
|
|
168
|
+
// Bentuk:
|
|
169
|
+
// minicode config context-window → daftar semua nilai
|
|
170
|
+
// minicode config context-window <provider> → nilai provider itu
|
|
171
|
+
// minicode config context-window <provider> <model> <nilai>
|
|
172
|
+
// minicode config context-window <provider> <model> auto → kembali deteksi
|
|
173
|
+
const pid = positionalArg(args, 2)
|
|
174
|
+
const model = positionalArg(args, 3)
|
|
175
|
+
const value = positionalArg(args, 4)
|
|
176
|
+
const { assignContextWindow, summarizeContextWindows } = await import(
|
|
177
|
+
"../../src/policy/context-window-assign.ts"
|
|
178
|
+
)
|
|
179
|
+
const { describeContextWindow } = await import("../../src/policy/context-window-presets.ts")
|
|
180
|
+
const global = !args.includes("--local")
|
|
181
|
+
const cwdArg = getArg("--cwd")
|
|
182
|
+
// Config dimuat lokal untuk cabang ini: daftar/nilai harus mencerminkan
|
|
183
|
+
// berkas yang benar-benar akan ditulis (global vs lokal), sama seperti
|
|
184
|
+
// cabang `add`/`remove`.
|
|
185
|
+
const cfg = await loadConfig(cwdArg, { allowLocal: allowLocalHere(args) })
|
|
186
|
+
|
|
187
|
+
if (!pid) {
|
|
188
|
+
// Daftar ringkas seluruh provider: model mana yang punya nilai eksplisit.
|
|
189
|
+
const rows = cfg.providers.flatMap((p) =>
|
|
190
|
+
summarizeContextWindows(p).map((w) => ({
|
|
191
|
+
provider: p.id,
|
|
192
|
+
model: w.model === "*" ? c.dim("* (default)") : w.model,
|
|
193
|
+
window: describeContextWindow(w.tokens),
|
|
194
|
+
source: w.source === "user" ? c.cyan("manual") : c.dim(w.source),
|
|
195
|
+
})),
|
|
196
|
+
)
|
|
197
|
+
if (rows.length === 0) {
|
|
198
|
+
console.log(
|
|
199
|
+
`${c.dim("no explicit context windows — all models use automatic detection")}\n` +
|
|
200
|
+
` ${c.dim("set one: minicode config context-window <provider> <model> 128k")}`,
|
|
201
|
+
)
|
|
202
|
+
process.exit(0)
|
|
203
|
+
}
|
|
204
|
+
console.log(
|
|
205
|
+
`
|
|
206
|
+
${c.bold("Context windows")}
|
|
207
|
+
` +
|
|
208
|
+
renderTable(
|
|
209
|
+
[
|
|
210
|
+
{ header: "Provider", key: "provider", width: 18 },
|
|
211
|
+
{ header: "Model", key: "model", width: 30 },
|
|
212
|
+
{ header: "Window", key: "window", width: 10, align: "right" },
|
|
213
|
+
{ header: "Origin", key: "source", width: 10 },
|
|
214
|
+
],
|
|
215
|
+
rows,
|
|
216
|
+
) +
|
|
217
|
+
NEWLINE,
|
|
218
|
+
)
|
|
219
|
+
process.exit(0)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const provider = cfg.providers.find((p) => p.id === pid)
|
|
223
|
+
if (!provider) {
|
|
224
|
+
console.error(`provider ${c.bold(sanitizeAnsiLine(pid))} not found — see: minicode providers`)
|
|
225
|
+
process.exit(1)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (!model || !value) {
|
|
229
|
+
const rows = summarizeContextWindows(provider).map((w) => ({
|
|
230
|
+
model: w.model === "*" ? c.dim("* (default)") : sanitizeAnsiLine(w.model),
|
|
231
|
+
window: describeContextWindow(w.tokens),
|
|
232
|
+
source: w.source === "user" ? c.cyan("manual") : c.dim(w.source),
|
|
233
|
+
}))
|
|
234
|
+
if (rows.length === 0)
|
|
235
|
+
console.log(`${c.dim(`${provider.id}: no explicit windows (automatic detection)`)}`)
|
|
236
|
+
else
|
|
237
|
+
console.log(
|
|
238
|
+
`
|
|
239
|
+
${c.bold(`Context windows — ${sanitizeAnsiLine(provider.id)}`)}
|
|
240
|
+
` +
|
|
241
|
+
renderTable(
|
|
242
|
+
[
|
|
243
|
+
{ header: "Model", key: "model", width: 34 },
|
|
244
|
+
{ header: "Window", key: "window", width: 10, align: "right" },
|
|
245
|
+
{ header: "Origin", key: "source", width: 10 },
|
|
246
|
+
],
|
|
247
|
+
rows,
|
|
248
|
+
) +
|
|
249
|
+
NEWLINE,
|
|
250
|
+
)
|
|
251
|
+
console.log(
|
|
252
|
+
` ${c.dim(
|
|
253
|
+
`set: minicode config context-window ${sanitizeAnsiLine(provider.id)} <model> <128k|1m|1024x1024|auto>`,
|
|
254
|
+
)}`,
|
|
255
|
+
)
|
|
256
|
+
process.exit(0)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const outcome = assignContextWindow(provider, model, value)
|
|
260
|
+
if (!outcome.ok) {
|
|
261
|
+
console.error(`${c.red(glyphs.cross ?? "x")} ${outcome.error}`)
|
|
262
|
+
process.exit(1)
|
|
263
|
+
}
|
|
264
|
+
const { saveProvider } = await import("../../src/providers/provision.ts")
|
|
265
|
+
await saveProvider(outcome.provider, { global, ...(cwdArg ? { cwd: cwdArg } : {}) })
|
|
266
|
+
const shown = sanitizeAnsiLine(model)
|
|
267
|
+
const where = global ? "global" : "local"
|
|
268
|
+
if (outcome.tokens === null) {
|
|
269
|
+
console.log(`${c.green(glyphs.check)} ${shown}: back to automatic detection (${where})`)
|
|
270
|
+
} else {
|
|
271
|
+
console.log(
|
|
272
|
+
`${c.green(glyphs.check)} ${shown}: ${describeContextWindow(outcome.tokens)} ` +
|
|
273
|
+
`(${outcome.tokens.toLocaleString("en-US")} tokens) — ${where}`,
|
|
274
|
+
)
|
|
275
|
+
}
|
|
276
|
+
process.exit(0)
|
|
154
277
|
} else if (sub === "set-key" || sub === "delete-key") {
|
|
155
278
|
// Pindahkan API key provider dari config plaintext ke penyimpanan OS
|
|
156
279
|
// (Windows: DPAPI user-scope; selain itu: berkas chmod 600 + peringatan
|
package/cli/commands.ts
CHANGED
|
@@ -134,6 +134,7 @@ export const BUILTIN_COMMANDS: BuiltinCommand[] = [
|
|
|
134
134
|
{ name: "help", descKey: "help.desc.help" },
|
|
135
135
|
{ name: "provider", descKey: "help.desc.provider" },
|
|
136
136
|
{ name: "model", descKey: "help.desc.model" },
|
|
137
|
+
{ name: "context", descKey: "help.desc.context" },
|
|
137
138
|
{ name: "sync", descKey: "help.desc.sync" },
|
|
138
139
|
{ name: "status", descKey: "help.desc.status" },
|
|
139
140
|
{ name: "sessions", descKey: "help.desc.sessions" },
|
|
@@ -167,13 +168,17 @@ export const DRIVER_HELP_COMMANDS: BuiltinCommand[] = [
|
|
|
167
168
|
/** Grup kurasi untuk /help: kategori semantik (bukan urutan registrasi). Dipakai
|
|
168
169
|
* render full help supaya mudah dipindai; nama di sini harus 1:1 dengan isi
|
|
169
170
|
* BUILTIN_COMMANDS + DRIVER_HELP_COMMANDS. */
|
|
170
|
-
const HELP_GROUPS: readonly { key: MsgKey; names: readonly string[] }[] = [
|
|
171
|
+
export const HELP_GROUPS: readonly { key: MsgKey; names: readonly string[] }[] = [
|
|
171
172
|
{
|
|
172
173
|
key: "help.gGeneral",
|
|
173
174
|
names: ["help", "status", "sessions", "history", "clear", "copy", "init", "exit"],
|
|
174
175
|
},
|
|
175
|
-
{ key: "help.gModel", names: ["model", "provider", "sync"] },
|
|
176
|
+
{ key: "help.gModel", names: ["model", "context", "provider", "sync"] },
|
|
176
177
|
{ key: "help.gMode", names: ["mode", "work-mode", "undo", "redo", "lang"] },
|
|
178
|
+
{
|
|
179
|
+
key: "help.gView",
|
|
180
|
+
names: ["compact", "thinking", "expand", "minimize", "fold"],
|
|
181
|
+
},
|
|
177
182
|
{ key: "help.gTask", names: ["scheduler"] },
|
|
178
183
|
]
|
|
179
184
|
|
|
@@ -589,6 +594,22 @@ export async function handleBuiltinCommand(
|
|
|
589
594
|
return { handled: true }
|
|
590
595
|
}
|
|
591
596
|
|
|
597
|
+
case "context": {
|
|
598
|
+
// `/context` = bagian "temukan" dari fitur jendela konteks manual. Tanpa
|
|
599
|
+
// entri di /help dan dropdown, fitur ini praktis tidak ada (temuan audit:
|
|
600
|
+
// pengguna tidak menemukannya sama sekali). Tanpa argumen = tabel angka
|
|
601
|
+
// EFEKTIF (rantai resolusi yang sama dengan router, bukan hitungan ulang);
|
|
602
|
+
// dengan argumen = setel untuk model aktif.
|
|
603
|
+
const { applyContextWindowArg, formatContextOutput } = await import(
|
|
604
|
+
"./context-command-run.ts"
|
|
605
|
+
)
|
|
606
|
+
// Satu penulis untuk seluruh blok: inventaris writer (OAP-008) menghitung
|
|
607
|
+
// pemanggilan tulis per berkas, jadi perakitan string dilakukan di modul
|
|
608
|
+
// perintah, bukan di sini.
|
|
609
|
+
console.log(formatContextOutput(await applyContextWindowArg(args, ctx)))
|
|
610
|
+
return { handled: true }
|
|
611
|
+
}
|
|
612
|
+
|
|
592
613
|
case "sync": {
|
|
593
614
|
// Re-detect model dari semua provider -> config diperbarui otomatis.
|
|
594
615
|
// Meneruskan flag local sesi: tanpa opt-in /sync tak boleh menghubungi
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// Eksekusi `/context`: baca config, resolve jendela efektif, dan (bila diminta)
|
|
2
|
+
// tulis penugasan manual.
|
|
3
|
+
//
|
|
4
|
+
// Dipisah dari `cli/commands.ts` karena butuh `loadConfig` + picker + penulisan
|
|
5
|
+
// config — sementara `commands.ts` memanggilnya lewat `import()` dinamis supaya
|
|
6
|
+
// jalur satu-shot (yang tak pernah membuka `/context`) tidak membayar biaya
|
|
7
|
+
// modul ini. Alasan yang sama dipakai `/model` (`./model-manager.ts`).
|
|
8
|
+
|
|
9
|
+
import type { ProviderEntry } from "../src/config.ts"
|
|
10
|
+
import { loadConfig } from "../src/config.ts"
|
|
11
|
+
import { resolveContextWindow } from "../src/policy/context-window.ts"
|
|
12
|
+
import { assignContextWindow } from "../src/policy/context-window-assign.ts"
|
|
13
|
+
import {
|
|
14
|
+
CONTEXT_WINDOW_AUTO,
|
|
15
|
+
describeContextWindow,
|
|
16
|
+
parseContextWindowInput,
|
|
17
|
+
} from "../src/policy/context-window-presets.ts"
|
|
18
|
+
import { t } from "../src/ui/i18n/locale.ts"
|
|
19
|
+
import { sanitizeAnsiLine } from "../src/ui/render/sanitize.ts"
|
|
20
|
+
import { renderTable } from "../src/ui/render/table.ts"
|
|
21
|
+
import type { CommandContext } from "./commands.ts"
|
|
22
|
+
import {
|
|
23
|
+
contextWindowRow,
|
|
24
|
+
contextWindowSavedNotice,
|
|
25
|
+
contextWindowTableHeader,
|
|
26
|
+
} from "./context-command.ts"
|
|
27
|
+
|
|
28
|
+
/** Baris `provider::model` aktif, diturunkan dari id yang dipakai sesi. */
|
|
29
|
+
function activeRef(ctx: CommandContext): { provider: string; model: string } | undefined {
|
|
30
|
+
const id = ctx.currentModel
|
|
31
|
+
if (!id?.includes("::")) return undefined
|
|
32
|
+
const sep = id.indexOf("::")
|
|
33
|
+
return { provider: id.slice(0, sep), model: id.slice(sep + 2) }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Cetak tabel jendela efektif untuk SEMUA model yang dikonfigurasi.
|
|
38
|
+
*
|
|
39
|
+
* Kenapa semua model, bukan hanya yang aktif: pertanyaan "berapa konteks saya"
|
|
40
|
+
* hampir selalu muncul saat memilih model, dan tabel satu baris memaksa buka
|
|
41
|
+
* `/context` berkali-kali. Baris aktif ditandai supaya mata langsung menemukan
|
|
42
|
+
* yang relevan.
|
|
43
|
+
*/
|
|
44
|
+
async function rowsFor(ctx: CommandContext): Promise<string[]> {
|
|
45
|
+
const cfg = await loadConfig(ctx.cwd, { allowLocal: ctx.allowLocalConfig })
|
|
46
|
+
const active = activeRef(ctx)
|
|
47
|
+
const rows: { model: string; window: string; origin: string }[] = []
|
|
48
|
+
for (const p of cfg.providers) {
|
|
49
|
+
for (const model of p.models ?? []) {
|
|
50
|
+
const res = resolveContextWindow(cfg, `${p.id}::${model}`)
|
|
51
|
+
// Angka jendela = data operasional, nama model = input tak terpercaya
|
|
52
|
+
// (bisa datang dari /models jaringan): sanitasi keduanya sebelum tampil.
|
|
53
|
+
rows.push({
|
|
54
|
+
model: `${sanitizeAnsiLine(`${p.id}::${model}`)}${active && active.provider === p.id && active.model === model ? " *" : ""}`,
|
|
55
|
+
window: sanitizeAnsiLine(contextWindowRow(model, res, describeContextWindow).window),
|
|
56
|
+
origin: sanitizeAnsiLine(contextWindowRow(model, res, describeContextWindow).origin),
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (!rows.length) return [t("ctx.empty")]
|
|
61
|
+
const head = contextWindowTableHeader()
|
|
62
|
+
return [
|
|
63
|
+
"",
|
|
64
|
+
renderTable(
|
|
65
|
+
[
|
|
66
|
+
{ header: head.model, key: "model", width: 40 },
|
|
67
|
+
{ header: head.window, key: "window", width: 10, align: "right" },
|
|
68
|
+
{ header: head.origin, key: "origin", width: 14 },
|
|
69
|
+
],
|
|
70
|
+
rows,
|
|
71
|
+
),
|
|
72
|
+
` ${t("ctx.usage")}`,
|
|
73
|
+
"",
|
|
74
|
+
].flatMap((line) => line.split("\n"))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Tulis nilai manual untuk model aktif. Mengembalikan baris untuk dicetak.
|
|
79
|
+
*
|
|
80
|
+
* Nilai `auto` MENGHAPUS kunci (lihat `assignContextWindow`), bukan menyimpan
|
|
81
|
+
* penanda — sehingga perilaku kembali persis ke rantai resolusi normal.
|
|
82
|
+
*/
|
|
83
|
+
async function save(ctx: CommandContext, input: string): Promise<string[]> {
|
|
84
|
+
const active = activeRef(ctx)
|
|
85
|
+
if (!active) return [`${t("ctx.saveFailed", { msg: "no active model — pick one with /model" })}`]
|
|
86
|
+
const cfg = await loadConfig(ctx.cwd, { allowLocal: ctx.allowLocalConfig })
|
|
87
|
+
const provider = cfg.providers.find((p) => p.id === active.provider)
|
|
88
|
+
if (!provider) return [`${t("ctx.saveFailed", { msg: `provider ${active.provider} not found` })}`]
|
|
89
|
+
|
|
90
|
+
const outcome = assignContextWindow(provider as ProviderEntry, active.model, input)
|
|
91
|
+
if (!outcome.ok) return [`${t("ctx.saveFailed", { msg: outcome.error })}`]
|
|
92
|
+
|
|
93
|
+
// Sesi ini menyimpan snapshot config saat start; tanpa mendaftarkan nilai
|
|
94
|
+
// baru ke registry live, "saved" di layar tidak mengubah apa pun sampai
|
|
95
|
+
// restart. Daftarkan sebelum/bersamaan dengan tulis disk.
|
|
96
|
+
const { setLiveContextWindow, clearLiveContextWindow } = await import(
|
|
97
|
+
"../src/policy/context-window.ts"
|
|
98
|
+
)
|
|
99
|
+
if (outcome.tokens === null) clearLiveContextWindow(active.provider, active.model)
|
|
100
|
+
else setLiveContextWindow(active.provider, active.model, outcome.tokens)
|
|
101
|
+
|
|
102
|
+
const { saveProvider } = await import("../src/providers/provision.ts")
|
|
103
|
+
// Scope tulis = scope tempat provider itu benar-benar didefinisikan, BUKAN
|
|
104
|
+
// hasil merge. Menyimpan entri merge ke file global menduplikat provider ke
|
|
105
|
+
// scope lain, lalu merge memprioritaskan salinan itu sehingga editan di scope
|
|
106
|
+
// asal terlihat "balik lagi" (kelas yang sama dengan `cli/model-manager.ts`).
|
|
107
|
+
// Provider lokal hanya terbaca bila sesi mengizinkannya (`--allow-local-config`)
|
|
108
|
+
// — tanpa opt-in kita tak boleh menyentuh berkas repo.
|
|
109
|
+
const inLocal =
|
|
110
|
+
!!ctx.allowLocalConfig &&
|
|
111
|
+
(await loadConfig(ctx.cwd, { allowLocal: true })).providers.some(
|
|
112
|
+
(p) => p.id === active.provider,
|
|
113
|
+
)
|
|
114
|
+
try {
|
|
115
|
+
await saveProvider(outcome.provider, {
|
|
116
|
+
...(inLocal ? { global: false, cwd: ctx.cwd ?? process.cwd() } : { global: true }),
|
|
117
|
+
})
|
|
118
|
+
} catch (e) {
|
|
119
|
+
return [t("ctx.saveFailed", { msg: sanitizeAnsiLine(String((e as Error)?.message ?? e)) })]
|
|
120
|
+
}
|
|
121
|
+
return [
|
|
122
|
+
sanitizeAnsiLine(
|
|
123
|
+
contextWindowSavedNotice(outcome.tokens, describeContextWindow).replace(
|
|
124
|
+
"{model}",
|
|
125
|
+
`${active.provider}::${active.model}`,
|
|
126
|
+
),
|
|
127
|
+
),
|
|
128
|
+
]
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Baris yang dicetak sebagai satu blok — dirakit di sini, bukan di
|
|
132
|
+
* `cli/commands.ts`, supaya berkas itu tetap satu penulis untuk perintah ini
|
|
133
|
+
* (inventaris writer OAP-008 menghitung pemanggilan tulis per berkas). */
|
|
134
|
+
export function formatContextOutput(lines: string[]): string {
|
|
135
|
+
return lines.join("\n")
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Entri tunggal `/context`: tanpa argumen = tabel, dengan argumen = setel. */
|
|
139
|
+
export async function applyContextWindowArg(args: string, ctx: CommandContext): Promise<string[]> {
|
|
140
|
+
const raw = args.trim()
|
|
141
|
+
if (!raw) {
|
|
142
|
+
// Tanpa argumen = HANYA membaca. Sengaja tanpa picker: membuka picker di
|
|
143
|
+
// sini berarti perintah menahan turn sampai pengguna memilih, sehingga
|
|
144
|
+
// `/context` yang diniatkan sebagai "lihat angka" malah mengunci TUI —
|
|
145
|
+
// kelas keluhan "macet" yang justru sedang kita bereskan. Mengubah nilai
|
|
146
|
+
// selalu eksplisit: `/context 200k`.
|
|
147
|
+
return rowsFor(ctx)
|
|
148
|
+
}
|
|
149
|
+
// `auto` diterima apa adanya; selain itu WAJIB terbaca sebagai angka/preset.
|
|
150
|
+
// Menolak tebakan di sini penting: salah ketik yang diterima diam-diam akan
|
|
151
|
+
// memotong konteks model tanpa gejala sampai kompaksi prematur muncul.
|
|
152
|
+
if (raw.toLowerCase() !== CONTEXT_WINDOW_AUTO && parseContextWindowInput(raw) === undefined)
|
|
153
|
+
return [
|
|
154
|
+
t("ctx.saveFailed", {
|
|
155
|
+
msg: `cannot read "${sanitizeAnsiLine(raw)}" as a window (try 128k, 1m, 1024x1024, auto)`,
|
|
156
|
+
}),
|
|
157
|
+
]
|
|
158
|
+
return save(ctx, raw)
|
|
159
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// `/context` — jendela konteks: LIHAT kapasitas efektif, lalu UBAH bila perlu.
|
|
2
|
+
//
|
|
3
|
+
// ── Kenapa perintah ini ada ────────────────────────────────────────────────
|
|
4
|
+
// Fitur jendela konteks manual sebelumnya hanya hidup di `minicode config
|
|
5
|
+
// context-window <provider> <model> <nilai>`: subcommand CLI yang tak muncul di
|
|
6
|
+
// `/help`, tak punya tombol di `/model`, dan tak punya umpan balik di UI. Hasil
|
|
7
|
+
// audit pengguna: "fitur ini tidak ada". Mesinnya benar; penemuannya nol.
|
|
8
|
+
//
|
|
9
|
+
// `/context` menutup celah itu di tempat pengguna sudah melihat model: satu
|
|
10
|
+
// perintah yang (a) MENAMPILKAN angka efektif beserta asalnya, dan (b) membuka
|
|
11
|
+
// picker preset untuk menimpanya. Tabel tidak dikarang di sini — `resolveContextWindow`
|
|
12
|
+
// adalah rantai yang sama yang dipakai router/kernel, jadi angka yang tampil
|
|
13
|
+
// tidak bisa menyimpang dari angka yang benar-benar dipakai turn berikutnya.
|
|
14
|
+
|
|
15
|
+
import type { ContextWindowResolution } from "../src/policy/context-window.ts"
|
|
16
|
+
import { t } from "../src/ui/i18n/locale.ts"
|
|
17
|
+
|
|
18
|
+
/** Satu baris tabel: model, kapasitas efektif, dan dari mana angka itu datang. */
|
|
19
|
+
export interface ContextWindowRow {
|
|
20
|
+
model: string
|
|
21
|
+
window: string
|
|
22
|
+
/** Label asal yang jujur (bukan tebakan): lihat `originLabel`. */
|
|
23
|
+
origin: string
|
|
24
|
+
/** True bila angka ini berasal dari pilihan operator (boleh diubah/dihapus). */
|
|
25
|
+
manual: boolean
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Label asal resolusi. Sengaja dibedakan antara "manual" (operator memilih) dan
|
|
30
|
+
* "endpoint"/"catalog"/"models-dev" (temuan otomatis yang bisa berubah) karena
|
|
31
|
+
* satu-satunya pertanyaan yang berguna bagi pengguna adalah: apakah angka ini
|
|
32
|
+
* keputusan saya atau tebakan mesin?
|
|
33
|
+
*/
|
|
34
|
+
export function originLabel(res: ContextWindowResolution): string {
|
|
35
|
+
switch (res.source) {
|
|
36
|
+
case "override":
|
|
37
|
+
return "flag"
|
|
38
|
+
case "model":
|
|
39
|
+
case "provider-default":
|
|
40
|
+
// `origin` membedakan nilai `user` (dikunci operator) dari `endpoint`
|
|
41
|
+
// (ditemukan dari /models, boleh ditimpa sync berikutnya).
|
|
42
|
+
return res.origin === "user" ? "manual" : (res.origin ?? "config")
|
|
43
|
+
case "catalog":
|
|
44
|
+
return "katalog"
|
|
45
|
+
case "models-dev":
|
|
46
|
+
return "models.dev"
|
|
47
|
+
case "safe-default":
|
|
48
|
+
return "default aman"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Baris tabel untuk satu model. `describe` disuntik agar modul ini bebas dari
|
|
53
|
+
* format angka (dan mudah diuji tanpa tabel preset). */
|
|
54
|
+
export function contextWindowRow(
|
|
55
|
+
model: string,
|
|
56
|
+
res: ContextWindowResolution,
|
|
57
|
+
describe: (tokens: number) => string,
|
|
58
|
+
): ContextWindowRow {
|
|
59
|
+
return {
|
|
60
|
+
model,
|
|
61
|
+
window: describe(res.tokens),
|
|
62
|
+
origin: originLabel(res),
|
|
63
|
+
manual: res.source === "model" || res.source === "provider-default",
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Judul baris tabel — satu sumber untuk /context. */
|
|
68
|
+
export function contextWindowTableHeader(): { model: string; window: string; origin: string } {
|
|
69
|
+
return {
|
|
70
|
+
model: t("ctx.colModel"),
|
|
71
|
+
window: t("ctx.colWindow"),
|
|
72
|
+
origin: t("ctx.colOrigin"),
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Teks hasil setelah operator memilih nilai: menyebutkan ANGKA, bukan hanya
|
|
78
|
+
* label. `describe` memberi label pendek ("128K"); angka eksak disertakan
|
|
79
|
+
* karena satu-satunya sumber kebingungan adalah "128K" genap atau 128.000.
|
|
80
|
+
*/
|
|
81
|
+
export function contextWindowSavedNotice(
|
|
82
|
+
tokens: number | null,
|
|
83
|
+
describe: (tokens: number) => string,
|
|
84
|
+
): string {
|
|
85
|
+
if (tokens === null) return t("ctx.savedAuto")
|
|
86
|
+
return t("ctx.saved", { label: describe(tokens), tokens: tokens.toLocaleString("en-US") })
|
|
87
|
+
}
|
package/cli/index.ts
CHANGED
|
@@ -36,6 +36,51 @@ import {
|
|
|
36
36
|
import { dispatch } from "./router.ts"
|
|
37
37
|
import { createCliSession } from "./setup.ts"
|
|
38
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Jaring terakhir proses (audit freeze/ceket 2026-11-16).
|
|
41
|
+
*
|
|
42
|
+
* Tanpa handler ini, SATU rejection/throw yang lolos (dari hook async, timer,
|
|
43
|
+
* stream provider yang ditutup, atau fire-and-forget di UI) membunuh proses
|
|
44
|
+
* dengan exit 1 dan meninggalkan terminal dalam raw mode + alt-screen — gejala
|
|
45
|
+
* persis "ceket": layar rusak, tak ada pesan, sesi harus ditutup manual.
|
|
46
|
+
*
|
|
47
|
+
* Perilaku lama tidak dipertahankan sebagai default karena tak ada satu pun
|
|
48
|
+
* jalur yang memang menginginkan proses mati diam-diam: kegagalan yang perlu
|
|
49
|
+
* menghentikan sesi sudah lewat `App.quit()`/`AgentError`.
|
|
50
|
+
*
|
|
51
|
+
* Kontrak yang dipilih:
|
|
52
|
+
* - Cetak ringkas ke stderr (tanpa stack penuh kecuali MINICODE_DEBUG=1).
|
|
53
|
+
* - Kembalikan terminal (raw mode, alt-screen, kursor) SEBELUM keluar.
|
|
54
|
+
* - Exit code 1 agar CI/skrip tetap melihat kegagalan.
|
|
55
|
+
* - Idempoten: penanganan kedua (mis. error saat cleanup) tidak rekursi.
|
|
56
|
+
*/
|
|
57
|
+
let crashGuardArmed = false
|
|
58
|
+
function installCrashGuard(): void {
|
|
59
|
+
if (crashGuardArmed) return
|
|
60
|
+
crashGuardArmed = true
|
|
61
|
+
const restoreAndExit = (kind: string, error: unknown): void => {
|
|
62
|
+
try {
|
|
63
|
+
// Kembalikan terminal: tanpa ini shell pengguna tetap dalam raw mode.
|
|
64
|
+
const stdin = process.stdin as typeof process.stdin & { setRawMode?: (b: boolean) => void }
|
|
65
|
+
stdin.setRawMode?.(false)
|
|
66
|
+
} catch {}
|
|
67
|
+
try {
|
|
68
|
+
// 1049 = keluar alt-screen; 25h = tampilkan kursor; 0m = reset atribut.
|
|
69
|
+
process.stdout.write("\u001b[?1049l\u001b[?25h\u001b[0m")
|
|
70
|
+
} catch {}
|
|
71
|
+
try {
|
|
72
|
+
const debug = process.env.MINICODE_DEBUG === "1"
|
|
73
|
+
const msg = error instanceof Error ? error.message : String(error)
|
|
74
|
+
const detail = debug && error instanceof Error && error.stack ? `\n${error.stack}` : ""
|
|
75
|
+
process.stderr.write(`\n[minicode] ${kind}: ${msg}${detail}\n`)
|
|
76
|
+
} catch {}
|
|
77
|
+
// 1 = gagal. Dipakai CI dan skrip pemanggil untuk membedakan crash dari exit normal.
|
|
78
|
+
process.exit(1)
|
|
79
|
+
}
|
|
80
|
+
process.on("uncaughtException", (error) => restoreAndExit("internal error", error))
|
|
81
|
+
process.on("unhandledRejection", (reason) => restoreAndExit("unhandled rejection", reason))
|
|
82
|
+
}
|
|
83
|
+
|
|
39
84
|
/** Versi dibaca dari package.json — satu sumber, tidak di-hardcode dua tempat. */
|
|
40
85
|
function readVersion(): string {
|
|
41
86
|
try {
|
|
@@ -102,6 +147,9 @@ Subcommand flags (config/mcp/sessions/skills/exec/...): see 'minicode <cmd> --he
|
|
|
102
147
|
`
|
|
103
148
|
|
|
104
149
|
const args = process.argv.slice(2)
|
|
150
|
+
// Dipasang SEBELUM dispatch: crash di jalur mana pun (termasuk config/mcp/exec)
|
|
151
|
+
// tetap memulihkan terminal, bukan meninggalkan sesi dalam raw mode.
|
|
152
|
+
installCrashGuard()
|
|
105
153
|
function getArg(name: string): string | undefined {
|
|
106
154
|
return rawGetArg(args, name)
|
|
107
155
|
}
|