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
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { Message } from "#minicore/core/types.ts"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Microcompact — pangkas ISI hasil tool lama jadi sentinel pendek: tanpa
|
|
5
|
+
* membuang pesan, tanpa LLM, tanpa menyentuh pasangan `assistant(toolCalls)`
|
|
6
|
+
* → `tool` yang masih berlangsung.
|
|
7
|
+
*
|
|
8
|
+
* KENAPA: pada sesi panjang hasil tool adalah penyumbang payload terbesar
|
|
9
|
+
* (>90% karakter konteks di lapangan). Kompaksi penuh mahal + lossy; memangkas
|
|
10
|
+
* isi lama jauh lebih murah dan mempertahankan struktur percakapan. Lapisan
|
|
11
|
+
* pertama sebelum kompaksi penuh, bukan penggantinya.
|
|
12
|
+
*
|
|
13
|
+
* KENAPA keepRecent=5: model masih butuh hasil terakhir untuk menyimpulkan
|
|
14
|
+
* langkah berikutnya; 5 = titik kumpul lintas implementasi harness produksi.
|
|
15
|
+
*
|
|
16
|
+
* KENAPA gating tiga syarat: memangkas hasil kecil tak menghemat apa pun tapi
|
|
17
|
+
* membuang informasi. Sepadan hanya bila (a) hemat berarti, (b) hasil tool
|
|
18
|
+
* mendominasi, (c) bukan di tengah batch tool yang belum selesai.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** Tool yang hasilnya DAPAT dipulihkan (dibaca ulang kapan saja). Tool
|
|
22
|
+
* kontrol-sesi (`todo`, `memory`) SENGAJA tidak masuk: isinya keadaan yang tak
|
|
23
|
+
* bisa direkonstruksi dari filesystem — memangkasnya menghapus kebenaran. */
|
|
24
|
+
export const MICROCOMPACT_RECOVERABLE_TOOLS: ReadonlySet<string> = new Set([
|
|
25
|
+
"read_file",
|
|
26
|
+
"grep",
|
|
27
|
+
"glob",
|
|
28
|
+
"bash",
|
|
29
|
+
"bash_output",
|
|
30
|
+
"code_run",
|
|
31
|
+
"web_fetch",
|
|
32
|
+
"web_search",
|
|
33
|
+
"list_dir",
|
|
34
|
+
"view_file",
|
|
35
|
+
])
|
|
36
|
+
|
|
37
|
+
/** Awalan sentinel. Stabil supaya bisa dikenali ulang (idempoten) dan supaya
|
|
38
|
+
* model tahu isinya dipangkas, bukan kosong. */
|
|
39
|
+
export const MICROCOMPACT_SENTINEL_PREFIX = "[hasil tool lama dipangkas"
|
|
40
|
+
|
|
41
|
+
export interface MicrocompactOptions {
|
|
42
|
+
/** Hasil tool TERBARU yang dipertahankan utuh. Default 5. */
|
|
43
|
+
keepRecent?: number
|
|
44
|
+
/** Estimator token (disuntik agar modul tetap murni). */
|
|
45
|
+
estimateTokens: (text: string) => number
|
|
46
|
+
/** Hemat minimum agar pemangkasan sepadan. Default 20.000 token. */
|
|
47
|
+
minSavingTokens?: number
|
|
48
|
+
/** Porsi total hasil tool agar pemangkasan dipertimbangkan (default 40%). */
|
|
49
|
+
minToolShare?: number
|
|
50
|
+
/** Budget konteks efektif (jendela - reserve). Wajib untuk syarat (b). */
|
|
51
|
+
budgetTokens?: number
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface MicrocompactResult {
|
|
55
|
+
readonly messages: readonly Message[]
|
|
56
|
+
readonly clearedCount: number
|
|
57
|
+
readonly savedTokens: number
|
|
58
|
+
/** Diisi bila tak memangkas — dibedakan dari "tak ada yang bisa dipangkas"
|
|
59
|
+
* supaya pemanggil tidak melaporkan no-op palsu. */
|
|
60
|
+
readonly skippedReason?: "below-saving" | "below-share" | "no-candidates" | "in-flight-batch"
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Konten pesan → teks untuk diukur. Objek bersarang di-stringify aman. */
|
|
64
|
+
function contentText(content: unknown): string {
|
|
65
|
+
if (typeof content === "string") return content
|
|
66
|
+
if (content === null || content === undefined) return ""
|
|
67
|
+
try {
|
|
68
|
+
return JSON.stringify(content) ?? ""
|
|
69
|
+
} catch {
|
|
70
|
+
return String(content)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Indeks hasil tool yang berada dalam batch AKTIF (toolCall terakhir dari
|
|
76
|
+
* assistant terakhir yang belum punya hasil lengkap).
|
|
77
|
+
*
|
|
78
|
+
* KENAPA penting: memangkas sebagian hasil dari satu batch multi-tool membuat
|
|
79
|
+
* model melihat observasi setengah jadi — lebih menyesatkan daripada konteks
|
|
80
|
+
* penuh. Batch dianggap aktif selama ia adalah grup `tool` terakhir.
|
|
81
|
+
*/
|
|
82
|
+
function activeBatchIds(messages: readonly Message[]): Set<string> {
|
|
83
|
+
const lastAssistantIdx = (() => {
|
|
84
|
+
for (let i = messages.length - 1; i >= 0; i--) if (messages[i]?.role === "assistant") return i
|
|
85
|
+
return -1
|
|
86
|
+
})()
|
|
87
|
+
if (lastAssistantIdx < 0) return new Set()
|
|
88
|
+
// Semua hasil tool SETELAH assistant terakhir (bila ada) adalah batch aktif.
|
|
89
|
+
const ids = new Set<string>()
|
|
90
|
+
for (let i = lastAssistantIdx + 1; i < messages.length; i++) {
|
|
91
|
+
const m = messages[i]
|
|
92
|
+
if (m?.role === "tool") ids.add(m.toolCallId)
|
|
93
|
+
}
|
|
94
|
+
return ids
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Murni (input tak dimutasi). Semua syarat harus lulus, jika tidak pesan
|
|
99
|
+
* dikembalikan apa adanya: (1) kandidat = hasil tool yang dapat dipulihkan di
|
|
100
|
+
* luar batch aktif, (2) melebihi `keepRecent`, (3) hemat ≥ `minSavingTokens`,
|
|
101
|
+
* (4) total hasil tool ≥ `minToolShare` × budget.
|
|
102
|
+
*/
|
|
103
|
+
export function applyMicrocompact(
|
|
104
|
+
messages: readonly Message[],
|
|
105
|
+
opts: MicrocompactOptions,
|
|
106
|
+
): MicrocompactResult {
|
|
107
|
+
const keepRecent = Math.max(1, opts.keepRecent ?? 5)
|
|
108
|
+
const minSaving = Math.max(0, opts.minSavingTokens ?? 20_000)
|
|
109
|
+
const minShare = opts.minToolShare ?? 0.4
|
|
110
|
+
const inFlight = activeBatchIds(messages)
|
|
111
|
+
|
|
112
|
+
// Dari belakang: `keepRecent` terbaru dipertahankan.
|
|
113
|
+
const candidates: number[] = []
|
|
114
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
115
|
+
const m = messages[i]
|
|
116
|
+
if (m?.role !== "tool") continue
|
|
117
|
+
if (!MICROCOMPACT_RECOVERABLE_TOOLS.has(m.name)) continue
|
|
118
|
+
if (inFlight.has(m.toolCallId)) continue
|
|
119
|
+
const text = contentText(m.content)
|
|
120
|
+
if (text.startsWith(MICROCOMPACT_SENTINEL_PREFIX)) continue // sudah dipangkas
|
|
121
|
+
candidates.push(i)
|
|
122
|
+
}
|
|
123
|
+
if (candidates.length === 0)
|
|
124
|
+
return { messages, clearedCount: 0, savedTokens: 0, skippedReason: "no-candidates" }
|
|
125
|
+
// `candidates` terurut dari belakang; `keepRecent` pertama = terbaru.
|
|
126
|
+
const victims = candidates.slice(keepRecent)
|
|
127
|
+
if (victims.length === 0)
|
|
128
|
+
return { messages, clearedCount: 0, savedTokens: 0, skippedReason: "no-candidates" }
|
|
129
|
+
|
|
130
|
+
// (b): hasil tool harus mendominasi budget, kalau tidak ia hanya membuang
|
|
131
|
+
// informasi demi sedikit token.
|
|
132
|
+
const toolTokens = candidates.reduce(
|
|
133
|
+
(sum, i) => sum + opts.estimateTokens(contentText(messages[i]!.content)),
|
|
134
|
+
0,
|
|
135
|
+
)
|
|
136
|
+
if (opts.budgetTokens !== undefined && opts.budgetTokens > 0) {
|
|
137
|
+
if (toolTokens < opts.budgetTokens * minShare)
|
|
138
|
+
return { messages, clearedCount: 0, savedTokens: 0, skippedReason: "below-share" }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let clearedCount = 0
|
|
142
|
+
let savedTokens = 0
|
|
143
|
+
const out = messages.slice()
|
|
144
|
+
for (const idx of victims) {
|
|
145
|
+
const m = messages[idx] as Extract<Message, { role: "tool" }>
|
|
146
|
+
const original = contentText(m.content)
|
|
147
|
+
const wasError = m.isError === true
|
|
148
|
+
const sentinel = `${MICROCOMPACT_SENTINEL_PREFIX}: ${m.name} ${original.length} char${wasError ? ", gagal" : ""}. Jalankan ulang tool ini bila isinya masih dibutuhkan.]`
|
|
149
|
+
// Hemat = token asli - token sentinel (sentinel tetap memakan token).
|
|
150
|
+
const saving = opts.estimateTokens(original) - opts.estimateTokens(sentinel)
|
|
151
|
+
out[idx] = { ...m, content: sentinel }
|
|
152
|
+
clearedCount++
|
|
153
|
+
// Sentinel bisa lebih panjang dari aslinya: jangan catat "hemat" palsu.
|
|
154
|
+
savedTokens += Math.max(0, saving)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// (c): hemat harus berarti. Bila tidak, batalkan seluruh operasi.
|
|
158
|
+
if (savedTokens < minSaving)
|
|
159
|
+
return { messages, clearedCount: 0, savedTokens: 0, skippedReason: "below-saving" }
|
|
160
|
+
|
|
161
|
+
return { messages: out, clearedCount, savedTokens }
|
|
162
|
+
}
|
|
@@ -68,7 +68,10 @@ export function createAnthropicProvider(config: AnthropicConfig): ModelProvider
|
|
|
68
68
|
.replace(/\/+$/, "")
|
|
69
69
|
.replace(/\/v1$/, "")
|
|
70
70
|
const endpoint = `${baseUrl}/v1/messages`
|
|
71
|
-
|
|
71
|
+
// Prompt caching aktif default; kill-switch config `enablePromptCaching:false`
|
|
72
|
+
// (statis) + env `MINICODE_PROMPT_CACHE=0` (runtime, pola microcompact/probe).
|
|
73
|
+
const enableCache =
|
|
74
|
+
config.enablePromptCaching !== false && process.env.MINICODE_PROMPT_CACHE !== "0"
|
|
72
75
|
|
|
73
76
|
// brand `kind` dipakai router agar tahu konten Uint8Array ditangani native
|
|
74
77
|
const provider: ModelProvider & { kind: "anthropic" } = {
|
|
@@ -101,11 +104,19 @@ export function createAnthropicProvider(config: AnthropicConfig): ModelProvider
|
|
|
101
104
|
const effectiveMaxTokens = legacyBudget
|
|
102
105
|
? Math.max(config.maxTokens ?? 8192, legacyBudget + 1024)
|
|
103
106
|
: (config.maxTokens ?? 8192)
|
|
107
|
+
// Fase 7: cache prefix percakapan (system+tools+histori) — tandai pesan
|
|
108
|
+
// stabil terakhir (kedua dari akhir; yang terakhir = pesan user saat ini)
|
|
109
|
+
// agar tool result besar ikut ter-cache tiap turn (bukan dikirim ulang).
|
|
110
|
+
const messagesPayload = toAnthropicMessages(request.messages)
|
|
111
|
+
if (enableCache && messagesPayload.length >= 2) {
|
|
112
|
+
const lastStable = messagesPayload[messagesPayload.length - 2] as Record<string, unknown>
|
|
113
|
+
lastStable.cache_control = { type: "ephemeral" }
|
|
114
|
+
}
|
|
104
115
|
const body = JSON.stringify({
|
|
105
116
|
model: request.model ?? config.defaultModel ?? config.models[0],
|
|
106
117
|
max_tokens: effectiveMaxTokens,
|
|
107
118
|
system: systemPayload,
|
|
108
|
-
messages:
|
|
119
|
+
messages: messagesPayload,
|
|
109
120
|
tools: toolsPayload,
|
|
110
121
|
...(legacyBudget ? { thinking: { type: "enabled", budget_tokens: legacyBudget } } : {}),
|
|
111
122
|
// Adaptive: tanpa budget & tanpa beta thinking (dikelola model);
|
|
@@ -387,7 +398,10 @@ function toContent(content: Content): unknown {
|
|
|
387
398
|
)
|
|
388
399
|
}
|
|
389
400
|
|
|
390
|
-
function toAnthropicTools(
|
|
401
|
+
export function toAnthropicTools(
|
|
402
|
+
tools: readonly ToolSchema[],
|
|
403
|
+
enableCache: boolean = true,
|
|
404
|
+
): unknown[] {
|
|
391
405
|
return tools.map((t, idx) => {
|
|
392
406
|
const isLast = idx === tools.length - 1
|
|
393
407
|
return {
|
package/src/providers/detect.ts
CHANGED
|
@@ -2,10 +2,24 @@ import { LIMITS } from "../constants.ts"
|
|
|
2
2
|
|
|
3
3
|
export interface DetectedModel {
|
|
4
4
|
id: string
|
|
5
|
+
/**
|
|
6
|
+
* Jendela konteks yang DILAPORKAN endpoint `/models`, bila ada.
|
|
7
|
+
*
|
|
8
|
+
* KENAPA dibaca: kapasitas endpoint yang benar-benar dipanggil adalah sumber
|
|
9
|
+
* otoritatif — katalog publik bisa basi atau tidak mengenali alias gateway.
|
|
10
|
+
* Nilai ini tidak dikarang: hanya diisi bila endpoint menyertakannya.
|
|
11
|
+
*/
|
|
12
|
+
contextLength?: number
|
|
13
|
+
/** Batas output model (`max_output_tokens`) bila dilaporkan. Dipakai sebagai
|
|
14
|
+
* reserve output proporsional, bukan angka tetap. */
|
|
15
|
+
maxOutputTokens?: number
|
|
5
16
|
}
|
|
6
17
|
|
|
7
18
|
export interface DetectResult {
|
|
8
19
|
models: string[]
|
|
20
|
+
/** Metadata per model, hanya untuk model yang endpoint-nya melaporkan
|
|
21
|
+
* kapasitas. Model tanpa metadata tidak muncul di sini (tidak dikarang). */
|
|
22
|
+
modelMetadata?: Record<string, DetectedModel>
|
|
9
23
|
providerHint: "openai" | "anthropic" | "responses" | "unknown"
|
|
10
24
|
/**
|
|
11
25
|
* True bila server menjawab 401/403 di salah satu attempt: key salah/
|
|
@@ -40,11 +54,111 @@ function hybridHeaders(apiKey: string): Record<string, string>[] {
|
|
|
40
54
|
]
|
|
41
55
|
}
|
|
42
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Nama field yang dipakai berbagai gateway/agregator untuk melaporkan
|
|
59
|
+
* kapasitas konteks. Tidak ada standar tunggal: OpenAI-compat memakai
|
|
60
|
+
* `context_length`, Anthropic-style memakai `max_input_tokens`, agregator
|
|
61
|
+
* memakai `context_window` atau menyarangkan di `top_provider`.
|
|
62
|
+
*
|
|
63
|
+
* KENAPA daftar eksplisit, bukan tebakan berbasis substring: field bernama
|
|
64
|
+
* `max_tokens` pada sebagian endpoint berarti batas OUTPUT, bukan konteks —
|
|
65
|
+
* memetakannya sebagai konteks akan mengecilkan jendela secara diam-diam,
|
|
66
|
+
* persis kelas bug yang ingin dihilangkan.
|
|
67
|
+
*/
|
|
68
|
+
const CONTEXT_FIELD_ALIASES = [
|
|
69
|
+
"context_length",
|
|
70
|
+
"context_window",
|
|
71
|
+
"max_context_length",
|
|
72
|
+
"max_context_tokens",
|
|
73
|
+
"max_input_tokens",
|
|
74
|
+
"input_token_limit",
|
|
75
|
+
] as const
|
|
76
|
+
|
|
77
|
+
const OUTPUT_FIELD_ALIASES = [
|
|
78
|
+
"max_output_tokens",
|
|
79
|
+
"max_completion_tokens",
|
|
80
|
+
"max_output",
|
|
81
|
+
"output_token_limit",
|
|
82
|
+
] as const
|
|
83
|
+
|
|
84
|
+
function positiveInt(value: unknown): number | undefined {
|
|
85
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return undefined
|
|
86
|
+
const n = Math.floor(value)
|
|
87
|
+
// Batas atas jauh di atas kapasitas model publik mana pun (2M) — menolak
|
|
88
|
+
// nilai rusak/absurd tanpa menolak model nyata.
|
|
89
|
+
return n > 0 && n <= 20_000_000 ? n : undefined
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function pickField(obj: Record<string, unknown>, aliases: readonly string[]): number | undefined {
|
|
93
|
+
for (const key of aliases) {
|
|
94
|
+
const v = positiveInt(obj[key])
|
|
95
|
+
if (v !== undefined) return v
|
|
96
|
+
}
|
|
97
|
+
return undefined
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Ekstrak metadata kapasitas dari satu entri `/models`.
|
|
102
|
+
*
|
|
103
|
+
* Diekspor untuk test: perilaku yang paling penting diuji di sini — payload
|
|
104
|
+
* tanpa field kapasitas harus menghasilkan `undefined` (bukan angka karangan).
|
|
105
|
+
*/
|
|
106
|
+
export function extractModelCapacity(entry: unknown): {
|
|
107
|
+
contextLength?: number
|
|
108
|
+
maxOutputTokens?: number
|
|
109
|
+
} {
|
|
110
|
+
if (!entry || typeof entry !== "object") return {}
|
|
111
|
+
const obj = entry as Record<string, unknown>
|
|
112
|
+
// Agregator menyarangkan kapasitas di `top_provider`, tapi sebagian juga
|
|
113
|
+
// menduplikasinya di level atas — cek keduanya, level atas menang karena
|
|
114
|
+
// itu yang paling spesifik untuk model ini.
|
|
115
|
+
const nested =
|
|
116
|
+
obj.top_provider && typeof obj.top_provider === "object"
|
|
117
|
+
? (obj.top_provider as Record<string, unknown>)
|
|
118
|
+
: undefined
|
|
119
|
+
const contextLength =
|
|
120
|
+
pickField(obj, CONTEXT_FIELD_ALIASES) ??
|
|
121
|
+
(nested ? pickField(nested, CONTEXT_FIELD_ALIASES) : undefined)
|
|
122
|
+
const maxOutputTokens =
|
|
123
|
+
pickField(obj, OUTPUT_FIELD_ALIASES) ??
|
|
124
|
+
(nested ? pickField(nested, OUTPUT_FIELD_ALIASES) : undefined)
|
|
125
|
+
return {
|
|
126
|
+
...(contextLength !== undefined ? { contextLength } : {}),
|
|
127
|
+
...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Petakan entri mentah `/models` → id + metadata (hanya yang dilaporkan). */
|
|
132
|
+
function mapEntries(raw: unknown[]): {
|
|
133
|
+
models: string[]
|
|
134
|
+
modelMetadata?: Record<string, DetectedModel>
|
|
135
|
+
} {
|
|
136
|
+
const models: string[] = []
|
|
137
|
+
let metadata: Record<string, DetectedModel> | undefined
|
|
138
|
+
for (const entry of raw) {
|
|
139
|
+
if (!entry || typeof entry !== "object") continue
|
|
140
|
+
const id = (entry as { id?: unknown }).id
|
|
141
|
+
if (typeof id !== "string" || !id) continue
|
|
142
|
+
models.push(id)
|
|
143
|
+
const capacity = extractModelCapacity(entry)
|
|
144
|
+
if (capacity.contextLength !== undefined || capacity.maxOutputTokens !== undefined) {
|
|
145
|
+
metadata ??= {}
|
|
146
|
+
metadata[id] = { id, ...capacity }
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return { models, ...(metadata ? { modelMetadata: metadata } : {}) }
|
|
150
|
+
}
|
|
151
|
+
|
|
43
152
|
async function tryFetchModels(
|
|
44
153
|
baseUrl: string,
|
|
45
154
|
headers: Record<string, string>,
|
|
46
155
|
signal: AbortSignal,
|
|
47
|
-
): Promise<{
|
|
156
|
+
): Promise<{
|
|
157
|
+
models: string[] | null
|
|
158
|
+
modelMetadata?: Record<string, DetectedModel>
|
|
159
|
+
contacted: boolean
|
|
160
|
+
sawAuth: boolean
|
|
161
|
+
}> {
|
|
48
162
|
const urls = [`${baseUrl.replace(/\/+$/, "")}/models`, `${baseUrl.replace(/\/+$/, "")}/v1/models`]
|
|
49
163
|
let contacted = false
|
|
50
164
|
let sawAuth = false
|
|
@@ -61,17 +175,17 @@ async function tryFetchModels(
|
|
|
61
175
|
// terlihat sukses tersimpan).
|
|
62
176
|
if (res.status === 401 || res.status === 403) sawAuth = true
|
|
63
177
|
if (!res.ok) continue
|
|
64
|
-
const json = (await res.json()) as { data?:
|
|
178
|
+
const json = (await res.json()) as { data?: unknown[]; models?: unknown[] }
|
|
65
179
|
const data = json.data ?? json.models ?? []
|
|
66
|
-
if (Array.isArray(data) && data.length)
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
180
|
+
if (Array.isArray(data) && data.length) {
|
|
181
|
+
const mapped = mapEntries(data)
|
|
182
|
+
if (mapped.models.length)
|
|
183
|
+
return {
|
|
184
|
+
models: mapped.models,
|
|
185
|
+
...(mapped.modelMetadata ? { modelMetadata: mapped.modelMetadata } : {}),
|
|
186
|
+
contacted,
|
|
187
|
+
sawAuth,
|
|
188
|
+
}
|
|
75
189
|
}
|
|
76
190
|
} catch {}
|
|
77
191
|
}
|
|
@@ -94,7 +208,7 @@ export async function detectModels(
|
|
|
94
208
|
let everAuthFailed = false
|
|
95
209
|
for (const h of hybridHeaders(apiKey)) {
|
|
96
210
|
if (sig.aborted) break
|
|
97
|
-
const { models, contacted, sawAuth } = await tryFetchModels(baseUrl, h, sig)
|
|
211
|
+
const { models, modelMetadata, contacted, sawAuth } = await tryFetchModels(baseUrl, h, sig)
|
|
98
212
|
everContacted = everContacted || contacted
|
|
99
213
|
everAuthFailed = everAuthFailed || sawAuth
|
|
100
214
|
if (models?.length) {
|
|
@@ -110,8 +224,9 @@ export async function detectModels(
|
|
|
110
224
|
? "anthropic"
|
|
111
225
|
: "openai"
|
|
112
226
|
const uniqueModels = Array.from(new Set(models))
|
|
113
|
-
const result = {
|
|
227
|
+
const result: DetectResult = {
|
|
114
228
|
models: uniqueModels,
|
|
229
|
+
...(modelMetadata ? { modelMetadata } : {}),
|
|
115
230
|
providerHint: hint as DetectResult["providerHint"],
|
|
116
231
|
authFailed: everAuthFailed,
|
|
117
232
|
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { readFile } from "node:fs/promises"
|
|
6
6
|
import { resolve } from "node:path"
|
|
7
7
|
import {
|
|
8
|
+
type ContextWindowSource,
|
|
8
9
|
globalConfigPath,
|
|
9
10
|
LOCAL,
|
|
10
11
|
loadConfig,
|
|
@@ -106,6 +107,66 @@ export function deriveProviderId(baseUrl: string, existingIds: string[], id?: st
|
|
|
106
107
|
return uniqId
|
|
107
108
|
}
|
|
108
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Gabungkan metadata kapasitas yang DITEMUKAN endpoint ke entri provider.
|
|
112
|
+
*
|
|
113
|
+
* Aturan presedensi (alasannya sama dengan `resolveContextWindow`):
|
|
114
|
+
* - Nilai ber-`source: "user"` milik operator TIDAK PERNAH ditimpa. Config lama
|
|
115
|
+
* tanpa catatan asal diperlakukan `user` supaya perilaku tidak berubah diam-diam.
|
|
116
|
+
* - Nilai `endpoint`/`probe` boleh diperbarui oleh deteksi berikutnya — endpoint
|
|
117
|
+
* yang dipanggil hari ini lebih tahu daripada hasil deteksi kemarin.
|
|
118
|
+
* - Model yang hilang dari `/models` (sudah tak ada) dibuang agar config tidak
|
|
119
|
+
* mengumpulkan nilai basi untuk model yang tak bisa dipakai lagi.
|
|
120
|
+
*
|
|
121
|
+
* Diekspor untuk test: ini satu-satunya tempat yang menentukan "siapa menang".
|
|
122
|
+
*/
|
|
123
|
+
export function mergeDiscoveredCapacity(
|
|
124
|
+
previous: ProviderEntry | undefined,
|
|
125
|
+
detected: DetectResult,
|
|
126
|
+
): Pick<ProviderEntry, "contextWindows" | "contextWindowSources" | "maxOutputTokens"> {
|
|
127
|
+
const metadata = detected.modelMetadata
|
|
128
|
+
if (!metadata || Object.keys(metadata).length === 0) return {}
|
|
129
|
+
const prevWindows = previous?.contextWindows ?? {}
|
|
130
|
+
const prevSources = previous?.contextWindowSources ?? {}
|
|
131
|
+
const prevMaxOut = previous?.maxOutputTokens ?? {}
|
|
132
|
+
const windows: Record<string, number> = {}
|
|
133
|
+
const sources: Record<string, ContextWindowSource> = {}
|
|
134
|
+
const maxOut: Record<string, number> = {}
|
|
135
|
+
for (const [model, meta] of Object.entries(metadata)) {
|
|
136
|
+
// Buang entri untuk model yang tak lagi ditawarkan endpoint (basi).
|
|
137
|
+
// Model di luar metadata tetap dipertahankan dari config lama di bawah.
|
|
138
|
+
const prevSource = prevSources[model]
|
|
139
|
+
const userOwned =
|
|
140
|
+
prevSource === undefined ? prevWindows[model] !== undefined : prevSource === "user"
|
|
141
|
+
if (meta.contextLength !== undefined && !userOwned) {
|
|
142
|
+
windows[model] = meta.contextLength
|
|
143
|
+
sources[model] = "endpoint"
|
|
144
|
+
}
|
|
145
|
+
if (meta.maxOutputTokens !== undefined) maxOut[model] = meta.maxOutputTokens
|
|
146
|
+
}
|
|
147
|
+
// Pertahankan yang tak dilaporkan endpoint kali ini (endpoint bisa parsial:
|
|
148
|
+
// sebagian gateway hanya menyertakan kapasitas untuk model yang baru
|
|
149
|
+
// ditambahkan). `live` hanya dipakai untuk membuang model yang endpoint
|
|
150
|
+
// TIDAK menawarkan lagi — bukan untuk membuang nilai yang belum dilaporkan.
|
|
151
|
+
const live = new Set(detected.models)
|
|
152
|
+
for (const [model, tokens] of Object.entries(prevWindows)) {
|
|
153
|
+
if (live.size > 0 && !live.has(model)) continue
|
|
154
|
+
if (windows[model] === undefined) {
|
|
155
|
+
windows[model] = tokens
|
|
156
|
+
sources[model] = prevSources[model] ?? "user"
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
for (const [model, tokens] of Object.entries(prevMaxOut)) {
|
|
160
|
+
if (live.size > 0 && !live.has(model)) continue
|
|
161
|
+
if (maxOut[model] === undefined) maxOut[model] = tokens
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
...(Object.keys(windows).length > 0 ? { contextWindows: windows } : {}),
|
|
165
|
+
...(Object.keys(sources).length > 0 ? { contextWindowSources: sources } : {}),
|
|
166
|
+
...(Object.keys(maxOut).length > 0 ? { maxOutputTokens: maxOut } : {}),
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
109
170
|
export async function detectAndSave(
|
|
110
171
|
baseUrl: string,
|
|
111
172
|
apiKey: string,
|
|
@@ -158,6 +219,9 @@ export async function detectAndSave(
|
|
|
158
219
|
// ini tiap add/edit me-reset effort ke default diam-diam ("hilang").
|
|
159
220
|
const previous = prevCfg.providers.find((p) => p.id === uniqId)
|
|
160
221
|
const prevEffort = previous?.reasoningEffort
|
|
222
|
+
// Kapasitas dari metadata endpoint (bila dilaporkan) diutamakan atas tebakan
|
|
223
|
+
// statis, tapi tak pernah menimpa nilai `user` — lihat mergeDiscoveredCapacity.
|
|
224
|
+
const capacity = mergeDiscoveredCapacity(previous, detected)
|
|
161
225
|
const entry: ProviderEntry = {
|
|
162
226
|
id: uniqId,
|
|
163
227
|
baseUrl,
|
|
@@ -165,6 +229,7 @@ export async function detectAndSave(
|
|
|
165
229
|
models: detected.models,
|
|
166
230
|
providerHint: detected.providerHint,
|
|
167
231
|
...(previous?.contextWindows ? { contextWindows: previous.contextWindows } : {}),
|
|
232
|
+
...capacity,
|
|
168
233
|
...(prevEffort ? { reasoningEffort: prevEffort } : {}),
|
|
169
234
|
}
|
|
170
235
|
await saveProvider(entry, opts)
|
|
@@ -250,7 +315,14 @@ export async function refreshProviderModels(
|
|
|
250
315
|
try {
|
|
251
316
|
const detected = await detectModels(p.baseUrl, apiKey)
|
|
252
317
|
if (detected.models.length) {
|
|
253
|
-
|
|
318
|
+
// Metadata kapasitas ikut disegarkan: gateway yang menambah/mengubah
|
|
319
|
+
// model akan memperbarui jendela konteksnya tanpa user menyentuh apa pun.
|
|
320
|
+
updated.set(p.id, {
|
|
321
|
+
...p,
|
|
322
|
+
models: detected.models,
|
|
323
|
+
providerHint: detected.providerHint,
|
|
324
|
+
...mergeDiscoveredCapacity(p, detected),
|
|
325
|
+
})
|
|
254
326
|
} else if (detected.authFailed) {
|
|
255
327
|
// Key ditolak (401/403): bedakan dari provider tanpa endpoint
|
|
256
328
|
// (Anthropic-style, tetap diam) — operator harus periksa key-nya.
|
|
@@ -290,6 +362,11 @@ export async function refreshProviderModels(
|
|
|
290
362
|
if (nu) {
|
|
291
363
|
p.models = nu.models
|
|
292
364
|
p.providerHint = nu.providerHint
|
|
365
|
+
// Kapasitas hanya ditulis bila hasil merge memang punya isi — tanpa
|
|
366
|
+
// guard ini provider tanpa metadata akan menghapus nilai lama.
|
|
367
|
+
if (nu.contextWindows) p.contextWindows = nu.contextWindows
|
|
368
|
+
if (nu.contextWindowSources) p.contextWindowSources = nu.contextWindowSources
|
|
369
|
+
if (nu.maxOutputTokens) p.maxOutputTokens = nu.maxOutputTokens
|
|
293
370
|
changed = true
|
|
294
371
|
}
|
|
295
372
|
}
|
|
@@ -262,12 +262,23 @@ export function createResponsesProvider(config: ResponsesConfig): ModelProvider
|
|
|
262
262
|
// budget/cost (buta spend). Bentuk shape sama dengan adapter
|
|
263
263
|
// openai-compat agar kolektor tak perlu tahu provider.
|
|
264
264
|
const u = resp?.usage as
|
|
265
|
-
| {
|
|
265
|
+
| {
|
|
266
|
+
input_tokens?: unknown
|
|
267
|
+
output_tokens?: unknown
|
|
268
|
+
total_tokens?: unknown
|
|
269
|
+
cached_tokens?: unknown
|
|
270
|
+
input_tokens_details?: { cached_tokens?: unknown }
|
|
271
|
+
}
|
|
266
272
|
| undefined
|
|
267
273
|
if (
|
|
268
274
|
u &&
|
|
269
275
|
(u.input_tokens != null || u.output_tokens != null || u.total_tokens != null)
|
|
270
276
|
) {
|
|
277
|
+
// Fase 7: cache-hit alias (input_tokens_details.cached_tokens / cached_tokens).
|
|
278
|
+
const cached =
|
|
279
|
+
(u.input_tokens_details?.cached_tokens !== undefined
|
|
280
|
+
? u.input_tokens_details.cached_tokens
|
|
281
|
+
: u.cached_tokens) ?? undefined
|
|
271
282
|
yield {
|
|
272
283
|
type: "extension",
|
|
273
284
|
kind: "usage",
|
|
@@ -276,6 +287,7 @@ export function createResponsesProvider(config: ResponsesConfig): ModelProvider
|
|
|
276
287
|
outputTokens:
|
|
277
288
|
typeof u.output_tokens === "number" ? u.output_tokens : undefined,
|
|
278
289
|
totalTokens: typeof u.total_tokens === "number" ? u.total_tokens : undefined,
|
|
290
|
+
...(typeof cached === "number" ? { cacheReadTokens: cached } : {}),
|
|
279
291
|
},
|
|
280
292
|
}
|
|
281
293
|
}
|
|
@@ -891,6 +891,22 @@ export function loadPresentationEvents(id: string, cwd?: string): DomainEvent[]
|
|
|
891
891
|
return loadPresentationEventsWithStats(id, cwd).events
|
|
892
892
|
}
|
|
893
893
|
|
|
894
|
+
/** Hitung kompaksi durable via kolom `type` (jangan muat+decode seluruh
|
|
895
|
+
* presentation_events — resume dulu tarik semua baris hanya untuk hitung revisi). */
|
|
896
|
+
export function countDurableCompactionEvents(sessionId: string, cwd?: string): number {
|
|
897
|
+
const db = open(cwd)
|
|
898
|
+
try {
|
|
899
|
+
const row = db
|
|
900
|
+
.prepare(
|
|
901
|
+
"SELECT COUNT(*) AS n FROM presentation_events WHERE session_id = ? AND type = 'context.compacted'",
|
|
902
|
+
)
|
|
903
|
+
.get(sessionId) as { n: number } | null
|
|
904
|
+
return row?.n ?? 0
|
|
905
|
+
} finally {
|
|
906
|
+
db.close()
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
894
910
|
// P2.12 — baca rentang untuk replay konsumen. WAJIB ada: `loadPresentationEvents`
|
|
895
911
|
// memuat SELURUH riwayat ke memori, dan konsumen yang attach dengan offset lama
|
|
896
912
|
// akan menarik seluruh sesi (ratusan ribu baris) hanya untuk 200 event terakhir
|
|
@@ -1557,6 +1573,46 @@ export function loadSession(
|
|
|
1557
1573
|
}
|
|
1558
1574
|
}
|
|
1559
1575
|
|
|
1576
|
+
/**
|
|
1577
|
+
* Meta sesi TANPA decode histori pesan. Jalur resume hanya butuh turnCount/
|
|
1578
|
+
* workMode/system/cwd; histori penuh dibaca SEKALI lewat `loadThreadHistoryWithSeq`.
|
|
1579
|
+
* Memisahkan meta dari decode menutup bug perf: dulu resume memanggil `loadSession`
|
|
1580
|
+
* (decode SEMUA messages) + `loadThreadHistoryWithSeq` (decode lagi) — dua kali
|
|
1581
|
+
* JSON.parse seluruh histori.
|
|
1582
|
+
*/
|
|
1583
|
+
export function loadSessionMeta(
|
|
1584
|
+
id: string,
|
|
1585
|
+
cwd?: string,
|
|
1586
|
+
): {
|
|
1587
|
+
system?: string
|
|
1588
|
+
cwd?: string
|
|
1589
|
+
turnCount: number
|
|
1590
|
+
workMode?: "DISCUSS" | "PLAN" | "EXECUTE"
|
|
1591
|
+
} | null {
|
|
1592
|
+
const db = open(cwd)
|
|
1593
|
+
try {
|
|
1594
|
+
const sess = db.prepare("SELECT system, cwd, work_mode FROM sessions WHERE id = ?").get(id) as {
|
|
1595
|
+
system: string
|
|
1596
|
+
cwd: string
|
|
1597
|
+
work_mode: string | null
|
|
1598
|
+
} | null
|
|
1599
|
+
if (!sess) return null
|
|
1600
|
+
const turnRow = db
|
|
1601
|
+
.prepare("SELECT MAX(turn_idx) AS m FROM turns WHERE session_id = ?")
|
|
1602
|
+
.get(id) as { m: number | null } | null
|
|
1603
|
+
const turnCount = turnRow?.m != null ? turnRow.m + 1 : 0
|
|
1604
|
+
const workMode = parseStoredWorkMode(sess.work_mode)
|
|
1605
|
+
return {
|
|
1606
|
+
system: sess.system,
|
|
1607
|
+
cwd: sess.cwd,
|
|
1608
|
+
turnCount,
|
|
1609
|
+
...(workMode ? { workMode } : {}),
|
|
1610
|
+
}
|
|
1611
|
+
} finally {
|
|
1612
|
+
db.close()
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1560
1616
|
// P2.4: baca histori SATU thread (ketat: tanpa NULL transisional).
|
|
1561
1617
|
// Beda dari loadSession (kompatibel): di sini thread scope eksplisit.
|
|
1562
1618
|
export function loadThreadHistory(sessionId: string, threadId: string, cwd?: string): unknown[] {
|
|
@@ -615,7 +615,7 @@ export function attachSimpleLogger(bus: UiBus, opts: SimpleOptions = {}): () =>
|
|
|
615
615
|
const lines = renderActivityPanel(snap.activities, { maxRows: 10 })
|
|
616
616
|
if (lines.length > 0) {
|
|
617
617
|
wErr(c.muted(`${t("panel.header")}\n`))
|
|
618
|
-
for (const line of lines) wErr(
|
|
618
|
+
for (const line of lines) wErr(`${line}\n`)
|
|
619
619
|
}
|
|
620
620
|
}
|
|
621
621
|
}
|