minicode-ai 0.22.0 → 0.23.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/cli/commands.ts +10 -0
- package/cli/index.ts +11 -0
- package/cli/setup.ts +64 -33
- package/cli/tui.ts +3 -3
- package/docs/TERMINAL_CONTRACT.md +39 -6
- package/docs/environment.md +3 -0
- package/package.json +1 -1
- package/src/app/session.ts +129 -20
- package/src/config.ts +100 -2
- package/src/constants.ts +29 -4
- package/src/policy/compaction.ts +27 -0
- package/src/policy/context-budget.ts +132 -0
- package/src/policy/context-escalation.ts +140 -0
- package/src/policy/context-window.ts +47 -9
- package/src/policy/context.ts +10 -1
- package/src/policy/microcompact.ts +162 -0
- package/src/providers/anthropic.ts +17 -3
- package/src/providers/detect.ts +128 -13
- package/src/providers/provision.ts +78 -1
- package/src/providers/responses.ts +13 -1
- package/src/session/persistence.ts +56 -0
- package/src/ui/assistant/simple.ts +1 -1
- package/src/ui/i18n/en.ts +19 -4
- package/src/ui/i18n/id.ts +21 -4
- package/src/ui/input/input.ts +2 -2
- package/src/ui/render/activity-panel.ts +21 -3
- package/src/ui/render/hint.ts +28 -0
- package/src/ui/render/logo.ts +91 -0
- package/src/ui/render/session-summary.ts +22 -1
- package/src/ui/render/theme.ts +5 -0
- package/src/ui/runtime/screen.ts +4 -2
- package/src/ui/screens/form.ts +19 -23
- package/src/ui/screens/lifecycle.ts +80 -0
- package/src/ui/screens/model-manager.ts +77 -81
- package/src/ui/screens/picker.ts +28 -22
- package/src/ui/screens/provider-manager.ts +47 -52
- package/src/ui/screens/watchdog.ts +27 -0
- package/src/ui/screens/wizard.ts +3 -20
- package/src/ui/tui/app.ts +5 -2
- package/src/ui/tui/transcript.ts +51 -16
- package/vendor/minicore/src/core/compact.ts +10 -0
- package/vendor/minicore/src/core/errors.ts +11 -1
- package/vendor/minicore/src/core/executor.ts +6 -1
- package/vendor/minicore/src/core/loop.ts +92 -2
- package/vendor/minicore/src/core/session.ts +8 -0
- package/vendor/minicore/src/providers/openai-compat.ts +15 -2
package/src/ui/tui/transcript.ts
CHANGED
|
@@ -323,7 +323,7 @@ export class Transcript {
|
|
|
323
323
|
}),
|
|
324
324
|
bus.on("provider:extension", (e: { kind: string; data: unknown }) => this.extension(e)),
|
|
325
325
|
bus.on("context:compacted", (e: { reason: string }) =>
|
|
326
|
-
this.push(c.muted(
|
|
326
|
+
this.push(c.muted(this.compactedLabel(e.reason ?? ""))),
|
|
327
327
|
),
|
|
328
328
|
]
|
|
329
329
|
if (opts.onPresentationEvent) {
|
|
@@ -331,6 +331,37 @@ export class Transcript {
|
|
|
331
331
|
}
|
|
332
332
|
}
|
|
333
333
|
|
|
334
|
+
/**
|
|
335
|
+
* Label hasil kompaksi yang JUJUR soal sebab dan akibat.
|
|
336
|
+
*
|
|
337
|
+
* Kernel mengirim `pressure:<level>` saat benar-benar menyusut dan
|
|
338
|
+
* `pressure:<level>:no-op` saat tak ada yang bisa dibuang. Dulu keduanya
|
|
339
|
+
* tampil sama ("dipadatkan: pressure:critical:no-op") sehingga sesi yang
|
|
340
|
+
* konteksnya TIDAK menyusut terlihat seakan berhasil dipadatkan — user lalu
|
|
341
|
+
* bingung kenapa turn berikut tetap gagal. Pemetaan ini memisahkan empat
|
|
342
|
+
* kondisi yang berbeda tindak lanjutnya: no-op, fixpoint, mustahil (biaya
|
|
343
|
+
* tetap), dan ringkasan LLM gagal.
|
|
344
|
+
*/
|
|
345
|
+
private compactedLabel(reason: string): string {
|
|
346
|
+
const clean = sanitizeAnsiLine(reason)
|
|
347
|
+
if (!clean) return t("ts.compacted", { reason: "-" })
|
|
348
|
+
if (clean.endsWith(":no-op")) {
|
|
349
|
+
const base = clean.slice(0, -":no-op".length)
|
|
350
|
+
return t("ts.compactedNoOp", { reason: base })
|
|
351
|
+
}
|
|
352
|
+
const kept = /^fixpoint:(\d+)$/.exec(clean)
|
|
353
|
+
if (kept) return t("ts.compactedFixpoint", { kept: kept[1] ?? "0" })
|
|
354
|
+
const impossible = /^impossible:(\d+):(\d+)$/.exec(clean)
|
|
355
|
+
if (impossible)
|
|
356
|
+
return t("ts.compactedImpossible", {
|
|
357
|
+
fixed: impossible[1] ?? "0",
|
|
358
|
+
trigger: impossible[2] ?? "0",
|
|
359
|
+
})
|
|
360
|
+
if (clean.startsWith("llm-failed:"))
|
|
361
|
+
return t("ts.compactedLlmFailed", { reason: clean.slice("llm-failed:".length) })
|
|
362
|
+
return t("ts.compacted", { reason: clean })
|
|
363
|
+
}
|
|
364
|
+
|
|
334
365
|
/** Teks model mengalir — ditahan sebagai ekor hidup sampai commit. */
|
|
335
366
|
stream(text: string): void {
|
|
336
367
|
if (!text) return
|
|
@@ -755,7 +786,9 @@ export class Transcript {
|
|
|
755
786
|
}
|
|
756
787
|
// thinking/running aktif: bangun segar (jarang — hanya saat turn berjalan,
|
|
757
788
|
// dan repaint-nya sudah dicoalesce 30ms, bukan per-keypress).
|
|
758
|
-
|
|
789
|
+
// Urutan: committed → activity (thinking/running/panel) → pending. Jawaban di
|
|
790
|
+
// ekor agar jangkar stabil saat streaming (activity mendorong histori, bukan jawaban).
|
|
791
|
+
const rows: TranscriptRow[] = [...this.committedRows(w)]
|
|
759
792
|
if (!this.thinkingAreaMode() && this.thinkingTail) {
|
|
760
793
|
for (const chunk of this.thinkingTail.split("\n")) {
|
|
761
794
|
const chunkRows = chunk === "" ? [""] : wrapByWidth(chunk, w)
|
|
@@ -790,8 +823,10 @@ export class Transcript {
|
|
|
790
823
|
})
|
|
791
824
|
}
|
|
792
825
|
// Panel aktivitas (Fase 2): baris grup tool terminal — tampil di bawah
|
|
793
|
-
// running, di atas
|
|
826
|
+
// running, di atas jawaban (pending). Hanya saat ada activity.
|
|
794
827
|
rows.push(...panel)
|
|
828
|
+
// Jawaban streaming berada di ekor (di bawah semua activity turn berjalan).
|
|
829
|
+
rows.push(...this.pendingRows(w))
|
|
795
830
|
return rows
|
|
796
831
|
}
|
|
797
832
|
|
|
@@ -1129,20 +1164,22 @@ export class Transcript {
|
|
|
1129
1164
|
return
|
|
1130
1165
|
}
|
|
1131
1166
|
if (event.type === "turn.completed") {
|
|
1132
|
-
this.commit()
|
|
1133
1167
|
this.commitThinking()
|
|
1168
|
+
// Flush panel SEBELUM commit jawaban agar urutan persist = transient (panel di
|
|
1169
|
+
// atas jawaban; tanpa ini panel "lompat" ke bawah saat turn selesai).
|
|
1170
|
+
this.flushActivityFold()
|
|
1171
|
+
this.commit()
|
|
1134
1172
|
// minimizeActivity ON / activityPanel ON → fold meringkas sendiri (nama
|
|
1135
1173
|
// tool + ok/gagal); turnSummary (turn · files · durasi) dilewati agar
|
|
1136
1174
|
// tidak ada dua ringkasan.
|
|
1137
1175
|
if (!this.uiState.minimizeActivity && !this.uiState.activityPanel)
|
|
1138
1176
|
this.turnSummary(event.summary, event)
|
|
1139
|
-
this.flushActivityFold()
|
|
1140
1177
|
return
|
|
1141
1178
|
}
|
|
1142
1179
|
if (event.type === "turn.failed" || event.type === "turn.cancelled") {
|
|
1143
|
-
this.commit()
|
|
1144
1180
|
this.commitThinking()
|
|
1145
1181
|
this.flushActivityFold()
|
|
1182
|
+
this.commit()
|
|
1146
1183
|
return
|
|
1147
1184
|
}
|
|
1148
1185
|
// F-TASK-1: status task kanonik (t<n>) + DAG dirender sebagai baris ledger,
|
|
@@ -1341,13 +1378,11 @@ export class Transcript {
|
|
|
1341
1378
|
? `: ${truncateToWidth(sanitizeAnsiLine(String(rationale)), 200, "…")}`
|
|
1342
1379
|
: ""
|
|
1343
1380
|
const next = event.nextAction ? ` next=${sanitizeAnsiLine(String(event.nextAction))}` : ""
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
: "?"
|
|
1350
|
-
const line = ` ${glyph} consequence ${sanitizeAnsiLine(verdict)}${rule}${next}${reason}`
|
|
1381
|
+
// Paritas renderer linear (simple.ts): marker netral `glyphs.bullet`
|
|
1382
|
+
// (●/*). Logika lama memakai glyphs.check/cross (="" setelah ikon
|
|
1383
|
+
// dihapus) + "?", sehingga baris PASS tampil tanpa penanda (dua spasi)
|
|
1384
|
+
// dan divergen antar permukaan — kelas OAP-001.
|
|
1385
|
+
const line = ` ${glyphs.bullet} consequence ${sanitizeAnsiLine(verdict)}${rule}${next}${reason}`
|
|
1351
1386
|
const paint =
|
|
1352
1387
|
verdict === "FAIL" || verdict === "BLOCKED"
|
|
1353
1388
|
? c.error(line)
|
|
@@ -1370,7 +1405,7 @@ export class Transcript {
|
|
|
1370
1405
|
this.commit()
|
|
1371
1406
|
if (event.type === "context.cutoff") {
|
|
1372
1407
|
const reason = event.reason ? `: ${sanitizeAnsiLine(String(event.reason))}` : ""
|
|
1373
|
-
const line = `
|
|
1408
|
+
const line = ` ${glyphs.bullet} context cutoff${reason}`
|
|
1374
1409
|
this.append(c.warning(line), {
|
|
1375
1410
|
kind: "system",
|
|
1376
1411
|
...(event.seq !== undefined ? { seq: event.seq } : {}),
|
|
@@ -1385,7 +1420,7 @@ export class Transcript {
|
|
|
1385
1420
|
used != null
|
|
1386
1421
|
? ` ${used.toLocaleString()}${window ? `/${window.toLocaleString()}` : ""} tokens`
|
|
1387
1422
|
: ""
|
|
1388
|
-
const line = `
|
|
1423
|
+
const line = ` ${glyphs.bullet} context window${tokens}${pct}`
|
|
1389
1424
|
this.append(c.muted(line), {
|
|
1390
1425
|
kind: "system",
|
|
1391
1426
|
...(event.seq !== undefined ? { seq: event.seq } : {}),
|
|
@@ -1416,7 +1451,7 @@ export class Transcript {
|
|
|
1416
1451
|
? " [verified]"
|
|
1417
1452
|
: ` [${event.taskVerification?.verdict ?? "unverified"}]`
|
|
1418
1453
|
: ""
|
|
1419
|
-
const line = `
|
|
1454
|
+
const line = ` ${glyphs.bullet} ${sanitizeAnsiLine(id)} ${status}${parent}${deps}${rev}${verify}`
|
|
1420
1455
|
const paint =
|
|
1421
1456
|
raw === "FAILED"
|
|
1422
1457
|
? c.error(line)
|
|
@@ -17,6 +17,16 @@ export interface CompactionStrategy {
|
|
|
17
17
|
* The signal is the turn's abort signal (user abort, session.abort, timeout).
|
|
18
18
|
*/
|
|
19
19
|
compactAsync?(store: ContextStore, opts: { keepRecentTurns: number }, signal: AbortSignal): Promise<readonly Message[]>;
|
|
20
|
+
/**
|
|
21
|
+
* Lapisan pertama sebelum kompaksi penuh: pangkas ISI hasil tool lama
|
|
22
|
+
* menjadi sentinel pendek. Opsional dan aditif — tanpa ini perilaku lama utuh.
|
|
23
|
+
*
|
|
24
|
+
* Mengembalikan daftar pesan baru (atau yang sama bila tak memangkas) dan
|
|
25
|
+
* WAJIB mempertahankan pasangan `assistant(toolCalls)` → `tool` serta pesan
|
|
26
|
+
* lain apa adanya. Loop memakai hasilnya hanya bila jumlah token benar-benar
|
|
27
|
+
* turun, jadi implementasi tak perlu (dan tak boleh) menebak sendiri.
|
|
28
|
+
*/
|
|
29
|
+
microcompact?(store: ContextStore, opts: { budgetTokens?: number }): readonly Message[];
|
|
20
30
|
}
|
|
21
31
|
|
|
22
32
|
export const mechanicalCompaction: CompactionStrategy = {
|
|
@@ -15,10 +15,20 @@ export class AgentError extends Error {
|
|
|
15
15
|
constructor(
|
|
16
16
|
readonly kind: AgentErrorKind,
|
|
17
17
|
message: string,
|
|
18
|
-
options?: {
|
|
18
|
+
options?: {
|
|
19
|
+
cause?: unknown
|
|
20
|
+
/** Diagnostik terstruktur untuk kegagalan yang butuh ANGKA, bukan hanya
|
|
21
|
+
* prosa (mis. `budget_exceeded` dengan `reason: "fixpoint"` + berapa
|
|
22
|
+
* token tersisa). Pemanggil bisa menampilkan sebab yang tepat alih-alih
|
|
23
|
+
* menyuruh user "coba lagi" pada kondisi yang tak akan berubah. */
|
|
24
|
+
details?: Record<string, unknown>
|
|
25
|
+
},
|
|
19
26
|
) {
|
|
20
27
|
super(message, options);
|
|
28
|
+
if (options?.details) this.details = options.details;
|
|
21
29
|
}
|
|
30
|
+
/** Rincian mesin-terbaca; `undefined` bila tak ada. */
|
|
31
|
+
readonly details?: Record<string, unknown>;
|
|
22
32
|
}
|
|
23
33
|
|
|
24
34
|
/**
|
|
@@ -135,5 +135,10 @@ function serializeContent(value: unknown, maxTokens: number): unknown {
|
|
|
135
135
|
if (text.length <= maxChars) return text;
|
|
136
136
|
const headLen = Math.max(0, Math.floor(maxChars * 0.7));
|
|
137
137
|
const tailLen = Math.max(0, maxChars - headLen - 12);
|
|
138
|
-
|
|
138
|
+
// Penanda menyebut CARA mengambil sisa, bukan sekadar "terpotong": tanpa itu
|
|
139
|
+
// model tahu ada yang hilang tapi tak tahu harus berbuat apa, lalu menebak
|
|
140
|
+
// atau menyerah. Pola head+tail dipilih agar awal dan akhir (lokasi error /
|
|
141
|
+
// ringkasan hasil) tetap terbaca.
|
|
142
|
+
const omitted = text.length - headLen - tailLen;
|
|
143
|
+
return `${text.slice(0, headLen)}\n…[output dipotong: ${omitted} karakter tidak ditampilkan; jalankan ulang tool dengan batas lebih sempit (mis. path/baris/pola spesifik) atau baca bertahap]…\n${text.slice(-tailLen)}`;
|
|
139
144
|
}
|
|
@@ -5,7 +5,7 @@ import type { RecoveryAction } from "./errors.ts";
|
|
|
5
5
|
import type { ExecutorDeps } from "./executor.ts";
|
|
6
6
|
import type { StreamRequest, FinishReason } from "./provider.ts";
|
|
7
7
|
import { snapshotMessages, snapshotState, snapshotStep, snapshotToolCall, snapshotToolResult, snapshotToolSchemas } from "./snapshot.ts";
|
|
8
|
-
import { estimateSessionContext } from "./tokens.ts";
|
|
8
|
+
import { estimateSessionContext, estimateSystem, estimateTools } from "./tokens.ts";
|
|
9
9
|
import type { ToolSchema } from "./tool.ts";
|
|
10
10
|
import type { ToolCall, ToolResult } from "./types.ts";
|
|
11
11
|
import type { SessionInternal, Step, TurnResult } from "./session.ts";
|
|
@@ -51,6 +51,9 @@ export async function executeTurn(
|
|
|
51
51
|
// turn. The hard cap prevents a no-progress compactor from looping forever.
|
|
52
52
|
let budgetCompactions = 0;
|
|
53
53
|
const maxBudgetCompactions = 3;
|
|
54
|
+
// Berapa kali berturut-turut kompaksi tidak mengurangi apa pun. Dipakai untuk
|
|
55
|
+
// membedakan "konteks penuh tapi bisa dipadatkan" dari "sudah di titik tetap".
|
|
56
|
+
let noProgressCompactions = 0;
|
|
54
57
|
let compactedForRecovery = false;
|
|
55
58
|
|
|
56
59
|
// Resolve once for the turn: model switching is a between-turn operation,
|
|
@@ -78,7 +81,46 @@ export async function executeTurn(
|
|
|
78
81
|
limitTokens: contextWindowTokens,
|
|
79
82
|
});
|
|
80
83
|
let reduced = false;
|
|
84
|
+
// Lapisan termurah lebih dulu: pangkas isi hasil tool lama. Ini TIDAK
|
|
85
|
+
// memanggil model apa pun, jadi aman dicoba di setiap titik tekanan — bila
|
|
86
|
+
// ia berhasil menurunkan token di bawah ambang, kompaksi penuh (mahal +
|
|
87
|
+
// lossy) tidak perlu dijalankan sama sekali.
|
|
88
|
+
if (s.budget.shouldCompact(pressure) && s.compaction.microcompact) {
|
|
89
|
+
const beforeMicro = contextTokens(s);
|
|
90
|
+
try {
|
|
91
|
+
const trimmed = s.compaction.microcompact(s.store, { budgetTokens: contextWindowTokens });
|
|
92
|
+
if (trimmed !== s.store.messages) {
|
|
93
|
+
s.store.replace(0, s.store.messages.length, trimmed);
|
|
94
|
+
}
|
|
95
|
+
} catch {
|
|
96
|
+
// Microcompact adalah optimisasi; kegagalannya tidak boleh menggagalkan
|
|
97
|
+
// turn. Kompaksi penuh di bawah tetap menjadi jaringnya.
|
|
98
|
+
}
|
|
99
|
+
const afterMicro = contextTokens(s);
|
|
100
|
+
if (afterMicro < beforeMicro) {
|
|
101
|
+
const freed = beforeMicro - afterMicro;
|
|
102
|
+
s.events.emit({ type: "context:compacted", reason: `microcompact:${freed}` });
|
|
103
|
+
pressure = s.budget.evaluate({ usedTokens: afterMicro, limitTokens: contextWindowTokens });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
81
106
|
if (s.budget.shouldCompact(pressure) && budgetCompactions < maxBudgetCompactions) {
|
|
107
|
+
// Biaya TETAP (system prompt + skema tool + pesan yang tak boleh dibuang)
|
|
108
|
+
// tidak pernah tersentuh kompaksi. Bila biaya itu sendiri sudah melebihi
|
|
109
|
+
// jendela, mencoba kompaksi hanya membakar satu panggilan LLM untuk
|
|
110
|
+
// menghasilkan no-op — laporkan sekali dengan angka, lalu berhenti
|
|
111
|
+
// mencoba. Ini membedakan "perlu dipadatkan" dari "mustahil dipadatkan".
|
|
112
|
+
const fixed = fixedContextTokens(s);
|
|
113
|
+
if (fixed > 0 && fixed >= contextWindowTokens) {
|
|
114
|
+
s.events.emit({
|
|
115
|
+
type: "context:compacted",
|
|
116
|
+
reason: `impossible:${fixed}:${contextWindowTokens}`,
|
|
117
|
+
});
|
|
118
|
+
throw new AgentError(
|
|
119
|
+
"budget_exceeded",
|
|
120
|
+
`context window exceeded before compaction (fixed instructions and tool schemas alone cost ${fixed} > ${contextWindowTokens}); compaction cannot help — reduce the fixed instructions/tools or select a model with a larger context window`,
|
|
121
|
+
{ details: { reason: "impossible", fixedTokens: fixed, windowTokens: contextWindowTokens } },
|
|
122
|
+
);
|
|
123
|
+
}
|
|
82
124
|
const before = s.store.messages.length;
|
|
83
125
|
await compactStore(s, signal);
|
|
84
126
|
budgetCompactions++;
|
|
@@ -95,15 +137,40 @@ export async function executeTurn(
|
|
|
95
137
|
usedTokens: contextTokens(s),
|
|
96
138
|
limitTokens: contextWindowTokens,
|
|
97
139
|
});
|
|
140
|
+
if (!reduced) noProgressCompactions++;
|
|
141
|
+
else noProgressCompactions = 0;
|
|
98
142
|
}
|
|
99
143
|
if (s.budget.shouldCompact(pressure) && reduced && budgetCompactions < maxBudgetCompactions) {
|
|
100
144
|
continue;
|
|
101
145
|
}
|
|
102
146
|
if (pressure === "critical") {
|
|
103
147
|
const used = contextTokens(s);
|
|
148
|
+
// Bila kompaksi berulang TIDAK mengurangi apa pun, penyebabnya bukan
|
|
149
|
+
// pesan yang bisa dibuang melainkan biaya tetap: system prompt + skema
|
|
150
|
+
// tool + ekor verbatim `keepRecentTurns` yang tak bisa dipotong lagi.
|
|
151
|
+
// Mencoba lagi tak akan berubah; laporkan SEBAB-nya dengan angka supaya
|
|
152
|
+
// user tahu harus mengurangi instruksi/alat atau memilih model berjendela
|
|
153
|
+
// lebih besar — bukan sekadar pesan "context window exceeded" yang
|
|
154
|
+
// menyiratkan "coba lagi".
|
|
155
|
+
const fixed = contextTokens(s);
|
|
156
|
+
if (noProgressCompactions > 0) {
|
|
157
|
+
throw new AgentError(
|
|
158
|
+
"budget_exceeded",
|
|
159
|
+
`context compaction cannot reduce this session further (fixpoint: ${fixed} > ${contextWindowTokens} after ${noProgressCompactions} compaction attempt(s) with ${s.store.messages.length} message(s) left); the remaining cost is fixed instructions, tool schemas, or the retained tail — reduce them or select a model with a larger context window`,
|
|
160
|
+
{
|
|
161
|
+
details: {
|
|
162
|
+
reason: "fixpoint",
|
|
163
|
+
fixedTokens: fixed,
|
|
164
|
+
windowTokens: contextWindowTokens,
|
|
165
|
+
keptMessages: s.store.messages.length,
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
);
|
|
169
|
+
}
|
|
104
170
|
throw new AgentError(
|
|
105
171
|
"budget_exceeded",
|
|
106
172
|
`context window exceeded after compaction (${used} > ${contextWindowTokens}); reduce the prompt, tool output, or fixed instructions, or select a model with a larger context window`,
|
|
173
|
+
{ details: { reason: "critical", fixedTokens: used, windowTokens: contextWindowTokens } },
|
|
107
174
|
);
|
|
108
175
|
}
|
|
109
176
|
|
|
@@ -381,6 +448,22 @@ function contextTokens(s: SessionInternal): number {
|
|
|
381
448
|
return estimateSessionContext(s.store, system, s.registry.list(), s.estimator);
|
|
382
449
|
}
|
|
383
450
|
|
|
451
|
+
/**
|
|
452
|
+
* Biaya yang TIDAK bisa dikurangi kompaksi: system prompt + skema tool.
|
|
453
|
+
*
|
|
454
|
+
* KENAPA dipisah dari `contextTokens`: kompaksi hanya bisa membuang/memadatkan
|
|
455
|
+
* PESAN. Bila system + tools saja sudah melebihi jendela, setiap percobaan
|
|
456
|
+
* kompaksi dijamin no-op. Memisahkan angka ini membuat loop bisa menyimpulkan
|
|
457
|
+
* "mustahil dipadatkan" tanpa membakar panggilan LLM.
|
|
458
|
+
*/
|
|
459
|
+
function fixedContextTokens(s: SessionInternal): number {
|
|
460
|
+
let system = s.system;
|
|
461
|
+
try {
|
|
462
|
+
system = s.systemForContext?.() ?? system;
|
|
463
|
+
} catch {}
|
|
464
|
+
return estimateSystem(system, s.estimator) + estimateTools(s.registry.list(), s.estimator);
|
|
465
|
+
}
|
|
466
|
+
|
|
384
467
|
// The single compaction seam. Prefers the strategy's optional async method
|
|
385
468
|
// (LLM summary) and always falls back to the sync strategy on failure — an
|
|
386
469
|
// async compactor must never be able to crash the loop.
|
|
@@ -393,7 +476,14 @@ async function compactStore(s: SessionInternal, signal: AbortSignal): Promise<vo
|
|
|
393
476
|
return;
|
|
394
477
|
} catch (error) {
|
|
395
478
|
if (signal.aborted) throw abortError(signal);
|
|
396
|
-
//
|
|
479
|
+
// Kegagalan ringkasan LLM TIDAK boleh diam: fallback mekanikal memang
|
|
480
|
+
// benar, tapi tanpa jejak ini sesi panjang terlihat "dipadatkan" padahal
|
|
481
|
+
// isinya hasil pemangkasan mekanis yang jauh lebih miskin. Laporkan
|
|
482
|
+
// sebabnya agar penyebab (timeout, thrash, model menolak) bisa dibedah.
|
|
483
|
+
const reason = String((error as Error)?.message ?? error)
|
|
484
|
+
.replace(/\s+/g, " ")
|
|
485
|
+
.slice(0, 160)
|
|
486
|
+
s.events.emit({ type: "context:compacted", reason: `llm-failed:${reason}` })
|
|
397
487
|
}
|
|
398
488
|
}
|
|
399
489
|
s.store.replace(0, s.store.messages.length, s.compaction.compact(s.store, opts));
|
|
@@ -64,6 +64,12 @@ export interface SessionConfig {
|
|
|
64
64
|
/** Resolve a model-specific context limit for each run. Additive app seam;
|
|
65
65
|
* the numeric option remains the fallback for existing library callers. */
|
|
66
66
|
contextWindowTokensForModel?: (model?: string) => number;
|
|
67
|
+
/** Batas output model (`max_output_tokens`) bila endpoint melaporkannya.
|
|
68
|
+
* Seam aditif: dipakai loop untuk menghitung reserve output proporsional —
|
|
69
|
+
* tanpa ini kernel hanya bisa memakai reserve angka tetap, yang terlalu
|
|
70
|
+
* besar untuk model berbatas output kecil dan terlalu kecil untuk jendela
|
|
71
|
+
* raksasa. `undefined` = tak diketahui (pemanggil memakai default). */
|
|
72
|
+
maxOutputTokensForModel?: (model?: string) => number | undefined;
|
|
67
73
|
toolResultMaxTokens?: number;
|
|
68
74
|
keepRecentTurns?: number;
|
|
69
75
|
/**
|
|
@@ -154,6 +160,7 @@ export interface SessionInternal {
|
|
|
154
160
|
readonly maxSteps: number;
|
|
155
161
|
readonly contextWindowTokens: number;
|
|
156
162
|
readonly contextWindowTokensForModel?: (model?: string) => number;
|
|
163
|
+
readonly maxOutputTokensForModel?: (model?: string) => number | undefined;
|
|
157
164
|
readonly toolResultMaxTokens: number;
|
|
158
165
|
readonly keepRecentTurns: number;
|
|
159
166
|
readonly timeoutMs: number;
|
|
@@ -196,6 +203,7 @@ export function createSession(config: SessionConfig): Session {
|
|
|
196
203
|
maxSteps: config.maxSteps ?? DEFAULT_MAX_STEPS,
|
|
197
204
|
contextWindowTokens: config.contextWindowTokens ?? DEFAULT_CONTEXT_WINDOW_TOKENS,
|
|
198
205
|
contextWindowTokensForModel: config.contextWindowTokensForModel,
|
|
206
|
+
maxOutputTokensForModel: config.maxOutputTokensForModel,
|
|
199
207
|
toolResultMaxTokens: config.toolResultMaxTokens ?? DEFAULT_TOOL_RESULT_MAX_TOKENS,
|
|
200
208
|
keepRecentTurns: config.keepRecentTurns ?? DEFAULT_KEEP_RECENT_TURNS,
|
|
201
209
|
timeoutMs: config.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
@@ -132,14 +132,27 @@ export function createOpenAICompatProvider(config: OpenAICompatConfig): ModelPro
|
|
|
132
132
|
// the kernel would see (and execute) the same tool call twice.
|
|
133
133
|
let finished = false;
|
|
134
134
|
for await (const chunk of sse(response.body, signal)) {
|
|
135
|
-
const usage = chunk.usage as {
|
|
135
|
+
const usage = chunk.usage as {
|
|
136
|
+
prompt_tokens?: number;
|
|
137
|
+
completion_tokens?: number;
|
|
138
|
+
total_tokens?: number;
|
|
139
|
+
// Fase 7: cache-hit alias (prompt_tokens_details.cached_tokens / cached_tokens).
|
|
140
|
+
prompt_tokens_details?: { cached_tokens?: number };
|
|
141
|
+
cached_tokens?: number;
|
|
142
|
+
} | undefined;
|
|
136
143
|
// P8 + race: beberapa provider (b.ai dll) mengirim `usage` pada chunk
|
|
137
144
|
// TERAKHIR setelah finish_reason. Jangan di-drop karena `finished`.
|
|
138
145
|
if (usage && (usage.prompt_tokens != null || usage.completion_tokens != null || usage.total_tokens != null)) {
|
|
146
|
+
const cached = usage.prompt_tokens_details?.cached_tokens ?? usage.cached_tokens;
|
|
139
147
|
yield {
|
|
140
148
|
type: "extension",
|
|
141
149
|
kind: "usage",
|
|
142
|
-
data: {
|
|
150
|
+
data: {
|
|
151
|
+
inputTokens: usage.prompt_tokens,
|
|
152
|
+
outputTokens: usage.completion_tokens,
|
|
153
|
+
totalTokens: usage.total_tokens,
|
|
154
|
+
...(typeof cached === "number" ? { cacheReadTokens: cached } : {}),
|
|
155
|
+
},
|
|
143
156
|
};
|
|
144
157
|
}
|
|
145
158
|
if (finished) {
|