minicode-ai 0.14.2 → 0.14.5
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 +34 -178
- package/cli/args.ts +10 -18
- package/cli/commands/acp.ts +20 -27
- package/cli/commands/daemon.ts +6 -7
- package/cli/commands/exec.ts +12 -10
- package/cli/commands.ts +6 -9
- package/cli/index.ts +30 -15
- package/cli/provider-manager.ts +1 -3
- package/cli/setup.ts +188 -143
- package/cli/tui.ts +15 -23
- package/docs/CONTROL-PLANE-MAP.md +145 -0
- package/docs/OUTPUT_EVENT_MODEL.md +1 -1
- package/docs/OUTPUT_PROTOCOL_SPEC.md +2 -2
- package/docs/OUTPUT_RENDERING_SPEC.md +7 -2
- package/docs/OUTPUT_UX_RULES.md +1 -1
- package/docs/README.md +14 -102
- package/docs/SUMMARY.md +53 -0
- package/docs/TERMINAL_CONTRACT.md +84 -51
- package/docs/UI_RENDER_PIPELINE.md +204 -0
- package/docs/agents.md +5 -5
- package/docs/architecture.md +23 -12
- package/docs/choosing-mode.md +7 -7
- package/docs/cli.md +22 -29
- package/docs/concepts.md +1 -1
- package/docs/config.md +10 -5
- package/docs/contributing.md +6 -6
- package/docs/environment.md +3 -1
- package/docs/exec.md +5 -5
- package/docs/glossary.md +8 -6
- package/docs/memory-sessions.md +7 -5
- package/docs/policy-sandbox.md +16 -14
- package/docs/quickstart.md +2 -2
- package/docs/repl.md +4 -3
- package/docs/security-model.md +14 -15
- package/docs/security.md +8 -29
- package/docs/skills.md +1 -1
- package/docs/tools.md +2 -2
- package/package.json +5 -3
- package/scripts/vendor-overlays/minicode-context.patch +109 -0
- package/src/app/provider-layer.ts +13 -1
- package/src/app/session.ts +231 -13
- package/src/config.ts +30 -3
- package/src/constants.ts +2 -0
- package/src/daemon/control.ts +1 -1
- package/src/mcp/server.ts +2 -3
- package/src/policy/compaction.ts +272 -16
- package/src/policy/context-window-catalog.ts +64 -0
- package/src/policy/context-window.ts +173 -0
- package/src/policy/context.ts +55 -3
- package/src/policy/pricing.ts +52 -0
- package/src/policy/ratelimit.ts +47 -18
- package/src/policy/usage.ts +15 -8
- package/src/policy/work-mode.ts +1 -1
- package/src/providers/guards.ts +61 -4
- package/src/providers/provision.ts +12 -3
- package/src/providers/router.ts +56 -23
- package/src/session/context-projection.ts +14 -0
- package/src/session/context-selector.ts +58 -20
- package/src/session/persistence.ts +1 -1
- package/src/task/model.ts +21 -70
- package/src/tools/task.ts +7 -0
- package/src/ui/footer.ts +131 -66
- package/src/ui/i18n/en.ts +0 -11
- package/src/ui/i18n/id.ts +0 -11
- package/src/ui/input/input.ts +2 -3
- package/src/ui/input/prompt-engine.ts +2 -5
- package/src/ui/screens/model-manager.ts +3 -10
- package/src/ui/screens/picker.ts +23 -40
- package/src/ui/screens/provider-manager.ts +12 -33
- package/src/ui/screens/wizard.ts +0 -1
- package/src/ui/tui/app.ts +10 -6
- package/src/ui/tui/transcript.ts +48 -8
- package/vendor/minicore/VENDOR.md +9 -5
- package/vendor/minicore/src/core/loop.ts +47 -12
- package/vendor/minicore/src/core/provider.ts +4 -0
- package/vendor/minicore/src/core/session.ts +14 -1
- package/docs/OUTPUT_ARCHITECTURE_AUDIT.md +0 -489
- package/docs/OUTPUT_IMPLEMENTATION_PLAN.md +0 -391
package/src/policy/context.ts
CHANGED
|
@@ -14,8 +14,60 @@ import { isRealPathOutsideRoot } from "./jail.ts"
|
|
|
14
14
|
|
|
15
15
|
const execFileAsync = promisify(execFile)
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
const denseTokenCharacters =
|
|
18
|
+
/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Extended_Pictographic}]/gu
|
|
19
|
+
const hasDenseTokenCharacter =
|
|
20
|
+
/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Extended_Pictographic}]/u
|
|
21
|
+
|
|
22
|
+
export function countDenseTokenCharacters(text: string): number {
|
|
23
|
+
let dense = 0
|
|
24
|
+
for (const match of text.matchAll(denseTokenCharacters)) {
|
|
25
|
+
if (match[0]) dense++
|
|
26
|
+
}
|
|
27
|
+
return dense
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Track a streamed text field without retaining/re-tokenizing its full prefix. */
|
|
31
|
+
export function createIncrementalTokenCounter(): {
|
|
32
|
+
add(chunk: string, factor?: number): number
|
|
33
|
+
} {
|
|
34
|
+
let codeUnits = 0
|
|
35
|
+
let denseCharacters = 0
|
|
36
|
+
let pendingHighSurrogate = ""
|
|
37
|
+
let previousEstimate = 0
|
|
38
|
+
return {
|
|
39
|
+
add(chunk, factor = 1) {
|
|
40
|
+
let complete = pendingHighSurrogate + chunk
|
|
41
|
+
pendingHighSurrogate = ""
|
|
42
|
+
if (complete.length > 0) {
|
|
43
|
+
const last = complete.charCodeAt(complete.length - 1)
|
|
44
|
+
if (last >= 0xd800 && last <= 0xdbff) {
|
|
45
|
+
pendingHighSurrogate = complete.slice(-1)
|
|
46
|
+
complete = complete.slice(0, -1)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
codeUnits += complete.length
|
|
50
|
+
denseCharacters += countDenseTokenCharacters(complete)
|
|
51
|
+
const baseEstimate = Math.ceil(codeUnits / DEFAULT_CHARS_PER_TOKEN + denseCharacters * 0.75)
|
|
52
|
+
const safeFactor = Number.isFinite(factor) && factor >= 1 ? factor : 1
|
|
53
|
+
const estimate = Math.ceil(baseEstimate * safeFactor)
|
|
54
|
+
const added = Math.max(0, estimate - previousEstimate)
|
|
55
|
+
previousEstimate = Math.max(previousEstimate, estimate)
|
|
56
|
+
return added
|
|
57
|
+
},
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const minicodeEstimator: TokenEstimator = (text: string) => {
|
|
62
|
+
// Keep the cheap chars/4 baseline for ordinary text, but price CJK and emoji
|
|
63
|
+
// closer to one token per code point. Plain UTF-16 chars/4 can undercount a
|
|
64
|
+
// multilingual prompt by several times before the first provider usage sample.
|
|
65
|
+
if (!hasDenseTokenCharacter.test(text)) {
|
|
66
|
+
return Math.ceil(text.length / DEFAULT_CHARS_PER_TOKEN)
|
|
67
|
+
}
|
|
68
|
+
const dense = countDenseTokenCharacters(text)
|
|
69
|
+
return Math.ceil(text.length / DEFAULT_CHARS_PER_TOKEN + dense * 0.75)
|
|
70
|
+
}
|
|
19
71
|
|
|
20
72
|
// helper for image content: base64 overhead 4/3 — use when estimating tool image results
|
|
21
73
|
export function estimateImageTokens(bytes: number): number {
|
|
@@ -90,7 +142,7 @@ export function createCalibratedEstimator(opts: { maxFactor?: number } = {}): Ca
|
|
|
90
142
|
// mengirim `usage` progresif-kumulatif per-chunk SSE. Sampel "actual naik,
|
|
91
143
|
// local beku" adalah artefak penghitungan intra-request — bukan drift
|
|
92
144
|
// tokenizer — dan sampel jauh di atas baseline lokal yang beku (actual >
|
|
93
|
-
// local × 3) bisa membangkaskan factor
|
|
145
|
+
// local × 3) bisa membangkaskan factor model ini (uji live tiprouter:
|
|
94
146
|
// local=5026 beku sementara input merambat 6413 → 28024 = 5.58× → seluruh
|
|
95
147
|
// estimasi konteks terinflasi → kompaksi palsu → turn dibunuh
|
|
96
148
|
// `context window exceeded after compaction`). Sampel demikian DIKIP yang
|
package/src/policy/pricing.ts
CHANGED
|
@@ -63,8 +63,20 @@ export function pricingCachePath(): string {
|
|
|
63
63
|
return join(homeDir(), ".minicode", "pricing.json")
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
/** Cache terpisah untuk jendela konteks (diisi dari `models.dev` saat sync). */
|
|
67
|
+
export function contextWindowsCachePath(): string {
|
|
68
|
+
return join(homeDir(), ".minicode", "context-windows.json")
|
|
69
|
+
}
|
|
70
|
+
|
|
66
71
|
const MODELS_DEV_URL = "https://models.dev/api.json"
|
|
67
72
|
|
|
73
|
+
interface ContextWindowsCache {
|
|
74
|
+
fetchedAt: number
|
|
75
|
+
source: string
|
|
76
|
+
/** modelId → jendela konteks (token). Kunci di-lowercase. */
|
|
77
|
+
models: Record<string, number>
|
|
78
|
+
}
|
|
79
|
+
|
|
68
80
|
interface PricingCache {
|
|
69
81
|
fetchedAt: number
|
|
70
82
|
source: string
|
|
@@ -157,6 +169,7 @@ export interface ModelsDevPayload {
|
|
|
157
169
|
{
|
|
158
170
|
id?: string
|
|
159
171
|
cost?: { input?: number; output?: number; cache_read?: number; cache_write?: number }
|
|
172
|
+
context_length?: unknown
|
|
160
173
|
}
|
|
161
174
|
>
|
|
162
175
|
}
|
|
@@ -201,6 +214,36 @@ export function extractPricing(payload: ModelsDevPayload): Record<string, ModelP
|
|
|
201
214
|
return out
|
|
202
215
|
}
|
|
203
216
|
|
|
217
|
+
/** Normalisasi field `context_length` models.dev (bisa number atau objek). */
|
|
218
|
+
function contextLengthNumber(value: unknown): number | undefined {
|
|
219
|
+
if (typeof value === "number" && Number.isFinite(value) && value > 0) return Math.floor(value)
|
|
220
|
+
if (value && typeof value === "object") {
|
|
221
|
+
const v = value as { total?: unknown; input?: unknown; output?: unknown }
|
|
222
|
+
for (const c of [v.total, v.input, v.output]) {
|
|
223
|
+
if (typeof c === "number" && Number.isFinite(c) && c > 0) return Math.floor(c)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return undefined
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Ekstrak jendela konteks (token total) dari payload models.dev. Diekspor untuk test. */
|
|
230
|
+
export function extractContextWindows(payload: ModelsDevPayload): Record<string, number> {
|
|
231
|
+
const out: Record<string, number> = {}
|
|
232
|
+
for (const provider of Object.values(payload ?? {})) {
|
|
233
|
+
for (const [key, model] of Object.entries(provider?.models ?? {})) {
|
|
234
|
+
const cl = contextLengthNumber(model?.context_length)
|
|
235
|
+
if (cl === undefined) continue
|
|
236
|
+
const id = (model?.id ?? key).toLowerCase()
|
|
237
|
+
if (!id) continue
|
|
238
|
+
// Context length adalah properti model (bukan provider); bila satu id
|
|
239
|
+
// muncul di beberapa provider dengan nilai beda, ambil max (kapasitas
|
|
240
|
+
// asli model). Over-estimasi menurun anggun via recovery compact-and-retry.
|
|
241
|
+
out[id] = out[id] === undefined ? cl : Math.max(out[id]!, cl)
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return out
|
|
245
|
+
}
|
|
246
|
+
|
|
204
247
|
/** Median harga, mengabaikan entri gratis bila ada yang berbayar. Diekspor untuk test. */
|
|
205
248
|
export function pickRepresentativePrice(list: ModelPrice[]): ModelPrice | undefined {
|
|
206
249
|
if (list.length === 0) return undefined
|
|
@@ -283,6 +326,15 @@ export async function syncPricing(url: string = MODELS_DEV_URL): Promise<SyncRes
|
|
|
283
326
|
await atomicWriteText(pricingCachePath(), serialized)
|
|
284
327
|
overlay = models
|
|
285
328
|
overlayMeta = { fetchedAt: cache.fetchedAt, count }
|
|
329
|
+
|
|
330
|
+
// Sekalian simpan jendela konteks (field `context_length`) ke cache terpisah;
|
|
331
|
+
// satu fetch, dua artefak. Gagal menulis cache jendela jangan menggagalkan
|
|
332
|
+
// sync harga — harga tetap lebih penting dan path-nya sudah teruji.
|
|
333
|
+
const windows = extractContextWindows(payload)
|
|
334
|
+
if (Object.keys(windows).length > 0) {
|
|
335
|
+
const wCache: ContextWindowsCache = { fetchedAt: Date.now(), source: url, models: windows }
|
|
336
|
+
await atomicWriteText(contextWindowsCachePath(), JSON.stringify(wCache))
|
|
337
|
+
}
|
|
286
338
|
return { count, bytes: serialized.length, path: pricingCachePath() }
|
|
287
339
|
}
|
|
288
340
|
|
package/src/policy/ratelimit.ts
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
1
|
// Token bucket rate limiter — mencegah request LLM beruntun kena 429.
|
|
2
2
|
// Capacity = burst allowance; refill per ms dari rpm.
|
|
3
|
+
import { LIMITS } from "../constants.ts"
|
|
3
4
|
|
|
4
5
|
export class RateLimiter {
|
|
6
|
+
private readonly capacity: number
|
|
7
|
+
private readonly refillPerMs: number
|
|
5
8
|
private tokens: number
|
|
6
9
|
private lastRefill: number
|
|
7
10
|
|
|
8
|
-
constructor(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.
|
|
13
|
-
this.lastRefill = Date.now()
|
|
11
|
+
constructor(capacity: number, refillPerMs: number) {
|
|
12
|
+
this.capacity = Number.isSafeInteger(capacity) && capacity >= 1 ? capacity : 1
|
|
13
|
+
this.refillPerMs = Number.isFinite(refillPerMs) && refillPerMs > 0 ? refillPerMs : 1 / 60_000
|
|
14
|
+
this.tokens = this.capacity
|
|
15
|
+
this.lastRefill = performance.now()
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
private refill(): void {
|
|
17
|
-
const now =
|
|
19
|
+
const now = performance.now()
|
|
18
20
|
const elapsed = now - this.lastRefill
|
|
19
21
|
if (elapsed > 0) {
|
|
20
22
|
this.tokens = Math.min(this.capacity, this.tokens + elapsed * this.refillPerMs)
|
|
@@ -22,17 +24,21 @@ export class RateLimiter {
|
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
/** Ambil 1 token; tunggu bila
|
|
26
|
-
async acquire(): Promise<void> {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
this.
|
|
30
|
-
|
|
27
|
+
/** Ambil 1 token; tunggu dibatalkan bila pemanggilnya timeout/cancel. */
|
|
28
|
+
async acquire(signal?: AbortSignal): Promise<void> {
|
|
29
|
+
while (true) {
|
|
30
|
+
if (signal?.aborted) throw signal.reason ?? new DOMException("Aborted", "AbortError")
|
|
31
|
+
this.refill()
|
|
32
|
+
if (this.tokens >= 1) {
|
|
33
|
+
this.tokens -= 1
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
const waitMs = Math.max(1, Math.ceil((1 - this.tokens) / this.refillPerMs))
|
|
37
|
+
// Node/Bun clamp delays above 2^31-1ms to 1ms. Chunk long waits so a
|
|
38
|
+
// very low configured RPM cannot accidentally become a hot loop.
|
|
39
|
+
const boundedWaitMs = Math.min(waitMs, LIMITS.MAX_TIMER_DELAY_MS)
|
|
40
|
+
await waitAbortable(boundedWaitMs, signal)
|
|
31
41
|
}
|
|
32
|
-
const waitMs = Math.ceil((1 - this.tokens) / this.refillPerMs)
|
|
33
|
-
await new Promise((r) => setTimeout(r, Math.max(0, waitMs)))
|
|
34
|
-
this.refill()
|
|
35
|
-
this.tokens = Math.max(0, this.tokens - 1)
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
/** Info status (untuk /status). */
|
|
@@ -48,7 +54,30 @@ export class RateLimiter {
|
|
|
48
54
|
|
|
49
55
|
// Buat limiter dari rpm (requests per minute). Burst = min(rpm, 10).
|
|
50
56
|
export function createRateLimiter(rpm: number): RateLimiter {
|
|
51
|
-
const safe =
|
|
57
|
+
const safe = Number.isFinite(rpm) && rpm > 0 ? rpm : 60
|
|
52
58
|
const capacity = Math.max(1, Math.min(Math.floor(safe), 10))
|
|
53
59
|
return new RateLimiter(capacity, safe / 60_000)
|
|
54
60
|
}
|
|
61
|
+
|
|
62
|
+
export function waitAbortable(ms: number, signal?: AbortSignal): Promise<void> {
|
|
63
|
+
return new Promise((resolve, reject) => {
|
|
64
|
+
if (signal?.aborted) {
|
|
65
|
+
reject(signal.reason ?? new DOMException("Aborted", "AbortError"))
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
const timer = setTimeout(() => {
|
|
69
|
+
cleanup()
|
|
70
|
+
resolve()
|
|
71
|
+
}, ms)
|
|
72
|
+
const onAbort = () => {
|
|
73
|
+
clearTimeout(timer)
|
|
74
|
+
cleanup()
|
|
75
|
+
reject(signal?.reason ?? new DOMException("Aborted", "AbortError"))
|
|
76
|
+
}
|
|
77
|
+
signal?.addEventListener("abort", onAbort, { once: true })
|
|
78
|
+
if (signal?.aborted) onAbort()
|
|
79
|
+
function cleanup() {
|
|
80
|
+
signal?.removeEventListener("abort", onAbort)
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
}
|
package/src/policy/usage.ts
CHANGED
|
@@ -236,13 +236,15 @@ export function watchBudgetLimit(opts: {
|
|
|
236
236
|
export function watchContextWindowLimit(opts: {
|
|
237
237
|
bus: EventBus
|
|
238
238
|
/** Batas window konteks (W). Bila 0/undefined → no-op. */
|
|
239
|
-
windowTokens: number
|
|
239
|
+
windowTokens: number | (() => number)
|
|
240
240
|
/** Snapshot token request provider aktif; window berlaku per request. */
|
|
241
241
|
getRequestTokens: () => { inputTokens: number; outputTokens: number } | null
|
|
242
|
-
onOver: (allowed: number, actual: number) => void
|
|
242
|
+
onOver: (allowed: number, actual: number, inputTokens?: number) => void
|
|
243
243
|
}): () => void {
|
|
244
|
-
const
|
|
245
|
-
|
|
244
|
+
const readWindow = () =>
|
|
245
|
+
typeof opts.windowTokens === "function" ? opts.windowTokens() : opts.windowTokens
|
|
246
|
+
const initialWindow = readWindow()
|
|
247
|
+
if (!Number.isFinite(initialWindow) || initialWindow <= 0) return () => {}
|
|
246
248
|
let fired = false
|
|
247
249
|
const check = () => {
|
|
248
250
|
if (fired) return
|
|
@@ -250,15 +252,20 @@ export function watchContextWindowLimit(opts: {
|
|
|
250
252
|
// Belum ada usage untuk request aktif: jangan memakai snapshot request
|
|
251
253
|
// sebelumnya atau total token kumulatif turn sebagai pengganti.
|
|
252
254
|
if (!request) return
|
|
253
|
-
const
|
|
255
|
+
const W = readWindow()
|
|
256
|
+
if (!Number.isFinite(W) || W <= 0) return
|
|
257
|
+
const allowedOutput = Math.max(0, Math.floor(W - request.inputTokens))
|
|
254
258
|
const output = request.outputTokens
|
|
255
|
-
if (output > allowedOutput) {
|
|
259
|
+
if (request.inputTokens > W || output > allowedOutput) {
|
|
256
260
|
fired = true
|
|
257
|
-
opts.onOver(allowedOutput, output)
|
|
261
|
+
opts.onOver(allowedOutput, output, request.inputTokens)
|
|
258
262
|
}
|
|
259
263
|
}
|
|
260
264
|
const offExt = opts.bus.on("provider:extension", (e) => {
|
|
261
|
-
if (e.kind === "usage")
|
|
265
|
+
if (e.kind === "usage") {
|
|
266
|
+
const data = e.data as { source?: unknown } | undefined
|
|
267
|
+
if (data?.source !== "compaction") check()
|
|
268
|
+
}
|
|
262
269
|
})
|
|
263
270
|
check()
|
|
264
271
|
return () => {
|
package/src/policy/work-mode.ts
CHANGED
|
@@ -30,7 +30,7 @@ export class VerifyWorkModeConflictError extends Error {
|
|
|
30
30
|
readonly workMode: Exclude<WorkMode, "EXECUTE">
|
|
31
31
|
constructor(workMode: Exclude<WorkMode, "EXECUTE">) {
|
|
32
32
|
super(
|
|
33
|
-
`[work-mode] --verify cannot be combined with WorkMode ${workMode} (verification runs shell commands and self-heal turns; use --work-mode
|
|
33
|
+
`[work-mode] --verify cannot be combined with WorkMode ${workMode} (verification runs shell commands and self-heal turns; use --work-mode BUILD or omit --verify)`,
|
|
34
34
|
)
|
|
35
35
|
this.name = "VerifyWorkModeConflictError"
|
|
36
36
|
this.workMode = workMode
|
package/src/providers/guards.ts
CHANGED
|
@@ -25,7 +25,13 @@ export function withStreamGuards(
|
|
|
25
25
|
maxTotalToolArgBytes?: number
|
|
26
26
|
} = {},
|
|
27
27
|
): ModelProvider {
|
|
28
|
-
const
|
|
28
|
+
const configuredTimeout = opts.timeoutMs
|
|
29
|
+
const timeoutMs =
|
|
30
|
+
typeof configuredTimeout === "number" &&
|
|
31
|
+
Number.isFinite(configuredTimeout) &&
|
|
32
|
+
configuredTimeout > 0
|
|
33
|
+
? Math.min(LIMITS.MAX_TIMER_DELAY_MS, Math.max(1, Math.floor(configuredTimeout)))
|
|
34
|
+
: LIMITS.PROVIDER_REQUEST_TIMEOUT_MS
|
|
29
35
|
const maxTextChars = opts.maxTextChars ?? LIMITS.PROVIDER_TEXT_MAX_CHARS
|
|
30
36
|
// Reasoning = bukan bagian jawaban; cap agresif (2MB logis ≈ 500k token —
|
|
31
37
|
// jauh di atas reasoning sah mana pun, jauh di bawah OOM).
|
|
@@ -61,8 +67,6 @@ export function withStreamGuards(
|
|
|
61
67
|
timedOut = true
|
|
62
68
|
ctl.abort(new Error(`provider request timed out after ${timeoutMs}ms`))
|
|
63
69
|
}, timeoutMs)
|
|
64
|
-
// Jangan menahan exit proses bila turn sudah selesai dari sisi lain.
|
|
65
|
-
;(timer as unknown as { unref?: () => void }).unref?.()
|
|
66
70
|
let textChars = 0
|
|
67
71
|
let reasonChars = 0
|
|
68
72
|
let toolArgumentBytes = 0
|
|
@@ -71,8 +75,13 @@ export function withStreamGuards(
|
|
|
71
75
|
let finishReason: "stop" | "tool_calls" | "length" | "abort" | "error" | undefined
|
|
72
76
|
let hadVisibleOutput = false
|
|
73
77
|
let invalidated = false
|
|
78
|
+
let iterator: AsyncIterator<ProviderEvent> | undefined
|
|
74
79
|
try {
|
|
75
|
-
|
|
80
|
+
iterator = provider.stream(request, ctl.signal)[Symbol.asyncIterator]()
|
|
81
|
+
while (true) {
|
|
82
|
+
const next = await nextOrAbort(iterator, ctl.signal)
|
|
83
|
+
if (next.done) break
|
|
84
|
+
const ev = next.value
|
|
76
85
|
if (
|
|
77
86
|
finishReason !== undefined &&
|
|
78
87
|
(ev.type === "text" || ev.type === "tool_call" || ev.type === "finish")
|
|
@@ -186,7 +195,55 @@ export function withStreamGuards(
|
|
|
186
195
|
} finally {
|
|
187
196
|
clearTimeout(timer)
|
|
188
197
|
signal.removeEventListener("abort", onParentAbort)
|
|
198
|
+
if (!ctl.signal.aborted && finishReason !== "stop" && finishReason !== "tool_calls") {
|
|
199
|
+
ctl.abort(new DOMException("Provider stream closed", "AbortError"))
|
|
200
|
+
}
|
|
201
|
+
if (ctl.signal.aborted && iterator?.return) {
|
|
202
|
+
try {
|
|
203
|
+
void Promise.resolve(iterator.return()).catch(() => {})
|
|
204
|
+
} catch {}
|
|
205
|
+
}
|
|
189
206
|
}
|
|
190
207
|
},
|
|
191
208
|
}
|
|
192
209
|
}
|
|
210
|
+
|
|
211
|
+
function nextOrAbort<T>(
|
|
212
|
+
iterator: AsyncIterator<T>,
|
|
213
|
+
signal: AbortSignal,
|
|
214
|
+
): Promise<IteratorResult<T>> {
|
|
215
|
+
if (signal.aborted)
|
|
216
|
+
return Promise.reject(signal.reason ?? new DOMException("Aborted", "AbortError"))
|
|
217
|
+
return new Promise((resolve, reject) => {
|
|
218
|
+
const onAbort = () => {
|
|
219
|
+
cleanup()
|
|
220
|
+
reject(signal.reason ?? new DOMException("Aborted", "AbortError"))
|
|
221
|
+
}
|
|
222
|
+
signal.addEventListener("abort", onAbort, { once: true })
|
|
223
|
+
if (signal.aborted) {
|
|
224
|
+
onAbort()
|
|
225
|
+
return
|
|
226
|
+
}
|
|
227
|
+
let next: Promise<IteratorResult<T>>
|
|
228
|
+
try {
|
|
229
|
+
next = Promise.resolve(iterator.next())
|
|
230
|
+
} catch (error) {
|
|
231
|
+
cleanup()
|
|
232
|
+
reject(error)
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
next.then(
|
|
236
|
+
(value) => {
|
|
237
|
+
cleanup()
|
|
238
|
+
resolve(value)
|
|
239
|
+
},
|
|
240
|
+
(error) => {
|
|
241
|
+
cleanup()
|
|
242
|
+
reject(error)
|
|
243
|
+
},
|
|
244
|
+
)
|
|
245
|
+
function cleanup() {
|
|
246
|
+
signal.removeEventListener("abort", onAbort)
|
|
247
|
+
}
|
|
248
|
+
})
|
|
249
|
+
}
|
|
@@ -62,8 +62,15 @@ export async function saveProvider(
|
|
|
62
62
|
if ((e as NodeJS.ErrnoException).code !== "ENOENT") throw e
|
|
63
63
|
}
|
|
64
64
|
const idx = cfg.providers.findIndex((p) => p.id === entry.id)
|
|
65
|
-
if (idx >= 0)
|
|
66
|
-
|
|
65
|
+
if (idx >= 0) {
|
|
66
|
+
const previous = cfg.providers[idx]!
|
|
67
|
+
cfg.providers[idx] = {
|
|
68
|
+
...entry,
|
|
69
|
+
...(entry.contextWindows === undefined && previous.contextWindows
|
|
70
|
+
? { contextWindows: previous.contextWindows }
|
|
71
|
+
: {}),
|
|
72
|
+
}
|
|
73
|
+
} else cfg.providers.push(entry)
|
|
67
74
|
await writeConfigAtomic(path, cfg)
|
|
68
75
|
})
|
|
69
76
|
}
|
|
@@ -149,13 +156,15 @@ export async function detectAndSave(
|
|
|
149
156
|
const uniqId = deriveProviderId(baseUrl, existing, id)
|
|
150
157
|
// Pertahankan knob user (thinking effort) bila provider sudah ada — tanpa
|
|
151
158
|
// ini tiap add/edit me-reset effort ke default diam-diam ("hilang").
|
|
152
|
-
const
|
|
159
|
+
const previous = prevCfg.providers.find((p) => p.id === uniqId)
|
|
160
|
+
const prevEffort = previous?.reasoningEffort
|
|
153
161
|
const entry: ProviderEntry = {
|
|
154
162
|
id: uniqId,
|
|
155
163
|
baseUrl,
|
|
156
164
|
apiKey,
|
|
157
165
|
models: detected.models,
|
|
158
166
|
providerHint: detected.providerHint,
|
|
167
|
+
...(previous?.contextWindows ? { contextWindows: previous.contextWindows } : {}),
|
|
159
168
|
...(prevEffort ? { reasoningEffort: prevEffort } : {}),
|
|
160
169
|
}
|
|
161
170
|
await saveProvider(entry, opts)
|
package/src/providers/router.ts
CHANGED
|
@@ -3,17 +3,25 @@ import { ProviderError } from "#minicore/core/errors.ts"
|
|
|
3
3
|
import type { ModelProvider, ProviderEvent, StreamRequest } from "#minicore/core/provider.ts"
|
|
4
4
|
import type { Message } from "#minicore/core/types.ts"
|
|
5
5
|
import { LIMITS } from "../constants.ts"
|
|
6
|
-
import
|
|
6
|
+
import { DEFAULT_FALLBACK_CONTEXT_WINDOW_TOKENS } from "../policy/context-window.ts"
|
|
7
|
+
import { type RateLimiter, waitAbortable } from "../policy/ratelimit.ts"
|
|
7
8
|
|
|
8
9
|
export interface RouterConfig {
|
|
9
10
|
providers: ModelProvider[]
|
|
10
11
|
defaultProviderId?: string
|
|
12
|
+
contextWindowTokensForModel?: (model?: string) => number
|
|
11
13
|
// P2 cap
|
|
12
14
|
maxRetryAfterMs?: number // default LIMITS.RETRY_AFTER_MAX_MS
|
|
13
15
|
// Token bucket rate limiter (opsional) — cegah request beruntun kena 429
|
|
14
16
|
limiter?: RateLimiter
|
|
15
17
|
}
|
|
16
18
|
|
|
19
|
+
export type RouterProvider = ModelProvider & {
|
|
20
|
+
updateProviders: (list: ModelProvider[]) => void
|
|
21
|
+
updateContextWindowResolver: (resolver: (model?: string) => number) => void
|
|
22
|
+
contextModelIdentity: (model?: string) => string | undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
17
25
|
// C4 fix: convert Uint8Array tool content to base64 before an openai-compat
|
|
18
26
|
// provider sees it. Anthropic handles Uint8Array natively (image →
|
|
19
27
|
// source.base64 + media_type in toAnthropicMessages) — converting up-front
|
|
@@ -75,15 +83,21 @@ function requestFor(
|
|
|
75
83
|
return { req: fixed, substituted: false }
|
|
76
84
|
}
|
|
77
85
|
|
|
78
|
-
export function createRouterProvider(config: RouterConfig):
|
|
79
|
-
const
|
|
86
|
+
export function createRouterProvider(config: RouterConfig): RouterProvider {
|
|
87
|
+
const configuredMaxRetry = config.maxRetryAfterMs
|
|
88
|
+
const maxRetry =
|
|
89
|
+
typeof configuredMaxRetry === "number" &&
|
|
90
|
+
Number.isSafeInteger(configuredMaxRetry) &&
|
|
91
|
+
configuredMaxRetry >= 0
|
|
92
|
+
? configuredMaxRetry
|
|
93
|
+
: LIMITS.RETRY_AFTER_MAX_MS
|
|
80
94
|
// byId dan defaultId dihitung per-stream agar provider yang baru ditambah via /provider
|
|
81
95
|
// langsung dikenali tanpa restart. Provider list bisa berubah mid-session.
|
|
82
96
|
const getById = () => new Map(config.providers.map((p) => [p.id, p]))
|
|
83
97
|
const getDefaultId = () => config.defaultProviderId ?? config.providers[0]?.id ?? "router"
|
|
84
98
|
const getModels = () => config.providers.flatMap((p) => [...p.models])
|
|
85
99
|
|
|
86
|
-
const router:
|
|
100
|
+
const router: RouterProvider = {
|
|
87
101
|
id: "router",
|
|
88
102
|
get models() {
|
|
89
103
|
return getModels()
|
|
@@ -91,6 +105,21 @@ export function createRouterProvider(config: RouterConfig): ModelProvider {
|
|
|
91
105
|
updateProviders(list: ModelProvider[]) {
|
|
92
106
|
config.providers.splice(0, config.providers.length, ...list)
|
|
93
107
|
},
|
|
108
|
+
updateContextWindowResolver(resolver) {
|
|
109
|
+
config.contextWindowTokensForModel = resolver
|
|
110
|
+
},
|
|
111
|
+
contextWindowTokensForModel(model?: string) {
|
|
112
|
+
return config.contextWindowTokensForModel?.(model) ?? DEFAULT_FALLBACK_CONTEXT_WINDOW_TOKENS
|
|
113
|
+
},
|
|
114
|
+
contextModelIdentity(model?: string) {
|
|
115
|
+
if (model?.includes("::")) return model
|
|
116
|
+
let provider = model
|
|
117
|
+
? config.providers.find((entry) => entry.models.includes(model))
|
|
118
|
+
: (getById().get(getDefaultId()) ?? config.providers[0])
|
|
119
|
+
provider ??= getById().get(getDefaultId()) ?? config.providers[0]
|
|
120
|
+
const effectiveModel = model && provider?.models.includes(model) ? model : provider?.models[0]
|
|
121
|
+
return provider && effectiveModel ? `${provider.id}::${effectiveModel}` : effectiveModel
|
|
122
|
+
},
|
|
94
123
|
async *stream(request: StreamRequest, signal: AbortSignal): AsyncIterable<ProviderEvent> {
|
|
95
124
|
// route by model name — first match wins (default/daftar urutan provider)
|
|
96
125
|
// Format "providerId::modelName" = PIN eksplisit (kontrak): provider itu
|
|
@@ -144,7 +173,7 @@ export function createRouterProvider(config: RouterConfig): ModelProvider {
|
|
|
144
173
|
let hasYieldedContent = false
|
|
145
174
|
try {
|
|
146
175
|
// rate limit: tunggu token bucket sebelum tiap request
|
|
147
|
-
if (config.limiter) await config.limiter.acquire()
|
|
176
|
+
if (config.limiter) await config.limiter.acquire(signal)
|
|
148
177
|
const fixed = needsBinaryFix(current) ? fixRequest(request) : request
|
|
149
178
|
const withSys = needsSystemMessage(current) ? withSystemMessage(fixed) : fixed
|
|
150
179
|
const { req, effectiveModel, substituted } = requestFor(current, { ...withSys, model })
|
|
@@ -180,7 +209,19 @@ export function createRouterProvider(config: RouterConfig): ModelProvider {
|
|
|
180
209
|
) {
|
|
181
210
|
hasYieldedContent = true
|
|
182
211
|
}
|
|
183
|
-
|
|
212
|
+
if (ev.type === "extension" && ev.kind === "usage") {
|
|
213
|
+
const data = ev.data && typeof ev.data === "object" ? ev.data : {}
|
|
214
|
+
yield {
|
|
215
|
+
...ev,
|
|
216
|
+
data: {
|
|
217
|
+
...(data as Record<string, unknown>),
|
|
218
|
+
model: req.model ?? model,
|
|
219
|
+
provider: current.id,
|
|
220
|
+
},
|
|
221
|
+
}
|
|
222
|
+
} else {
|
|
223
|
+
yield ev
|
|
224
|
+
}
|
|
184
225
|
}
|
|
185
226
|
return
|
|
186
227
|
} catch (e) {
|
|
@@ -189,8 +230,13 @@ export function createRouterProvider(config: RouterConfig): ModelProvider {
|
|
|
189
230
|
if (hasYieldedContent) throw e
|
|
190
231
|
// cap retryAfter without mutating original
|
|
191
232
|
let err: ProviderError = e
|
|
192
|
-
if (
|
|
193
|
-
|
|
233
|
+
if (
|
|
234
|
+
e.retryAfterMs != null &&
|
|
235
|
+
(!Number.isFinite(e.retryAfterMs) || e.retryAfterMs < 0)
|
|
236
|
+
) {
|
|
237
|
+
err = new ProviderError(e.category, e.message, undefined, e.cause)
|
|
238
|
+
} else if (e.retryAfterMs != null && e.retryAfterMs > maxRetry) {
|
|
239
|
+
err = new ProviderError(e.category, e.message, maxRetry, e.cause)
|
|
194
240
|
}
|
|
195
241
|
// P11 P1.3 — honori retry-after. Audit #14: fallback DULU bila ada
|
|
196
242
|
// alternatif — jangan bakar sleep (maks 30 dtk) saat provider
|
|
@@ -212,20 +258,7 @@ export function createRouterProvider(config: RouterConfig): ModelProvider {
|
|
|
212
258
|
}
|
|
213
259
|
}
|
|
214
260
|
const waitMs = Math.min(err.retryAfterMs, maxRetry)
|
|
215
|
-
|
|
216
|
-
let onAbort: (() => void) | undefined
|
|
217
|
-
try {
|
|
218
|
-
await Promise.race([
|
|
219
|
-
Bun.sleep(waitMs),
|
|
220
|
-
new Promise<void>((_, rej) => {
|
|
221
|
-
onAbort = () => rej(new DOMException("Aborted", "AbortError"))
|
|
222
|
-
if (signal.aborted) onAbort()
|
|
223
|
-
else signal.addEventListener("abort", onAbort, { once: true })
|
|
224
|
-
}),
|
|
225
|
-
])
|
|
226
|
-
} finally {
|
|
227
|
-
if (onAbort) signal.removeEventListener("abort", onAbort)
|
|
228
|
-
}
|
|
261
|
+
await waitAbortable(waitMs, signal)
|
|
229
262
|
// Pin: tetap di provider yang dipilih (tunggu lalu ulangi di
|
|
230
263
|
// tempat) — jangan pindah ke provider lain.
|
|
231
264
|
if (pinned) continue
|
|
@@ -256,5 +289,5 @@ export function createRouterProvider(config: RouterConfig): ModelProvider {
|
|
|
256
289
|
}
|
|
257
290
|
},
|
|
258
291
|
}
|
|
259
|
-
return router
|
|
292
|
+
return router
|
|
260
293
|
}
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
|
|
30
30
|
import { ContextStore, mechanicalCompaction } from "#minicore"
|
|
31
31
|
import type { Message } from "#minicore/core/types.ts"
|
|
32
|
+
import { LIMITS } from "../constants.ts"
|
|
32
33
|
import { renderFullHistoryFold } from "./full-history-fold.ts"
|
|
33
34
|
import {
|
|
34
35
|
buildProjection,
|
|
@@ -100,6 +101,18 @@ export function deriveSummaryFromCanonical(
|
|
|
100
101
|
return { summaryText, baseSeq }
|
|
101
102
|
}
|
|
102
103
|
|
|
104
|
+
function boundProjectionSummary(text: string): string {
|
|
105
|
+
const cap = LIMITS.COMPACTION_SUMMARY_MAX_CHARS
|
|
106
|
+
if (text.length <= cap) return text
|
|
107
|
+
const marker = "\n[projection summary truncated; older details may be missing]"
|
|
108
|
+
let head = text.slice(0, Math.max(0, cap - marker.length))
|
|
109
|
+
if (head.length > 0) {
|
|
110
|
+
const code = head.charCodeAt(head.length - 1)
|
|
111
|
+
if (code >= 0xd800 && code <= 0xdbff) head = head.slice(0, -1)
|
|
112
|
+
}
|
|
113
|
+
return `${head}${marker}`
|
|
114
|
+
}
|
|
115
|
+
|
|
103
116
|
/**
|
|
104
117
|
* Bungkus P3.7 `renderFullHistoryFold` untuk seam produsen: ubah baris thread
|
|
105
118
|
* menjadi input renderer, dan ubah `FoldError` (cakupan tak-terbukti) menjadi
|
|
@@ -182,6 +195,7 @@ export function produceSummaryProjection(
|
|
|
182
195
|
if (baseSeq === undefined || baseSeq <= 0) {
|
|
183
196
|
return { produced: false, detail: "no explicit/derivable baseSeq — refusing empty projection" }
|
|
184
197
|
}
|
|
198
|
+
summaryText = boundProjectionSummary(summaryText)
|
|
185
199
|
// Tulis lewat primitif P2.7 (epoch-fenced, satu txn, delete+insert, sumber
|
|
186
200
|
// kanonik UTUH). Satu-satunya efek tulis produsen = history_projections.
|
|
187
201
|
const row = buildProjection(sessionId, threadId, summaryText, cwd, {
|