minicode-ai 0.9.26
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/CHANGELOG.md +935 -0
- package/LICENSE +21 -0
- package/README.md +200 -0
- package/cli/args.ts +195 -0
- package/cli/auto-update.ts +108 -0
- package/cli/commands/auth.ts +166 -0
- package/cli/commands/config.ts +382 -0
- package/cli/commands/doctor.ts +128 -0
- package/cli/commands/exec.ts +179 -0
- package/cli/commands/mcp.ts +35 -0
- package/cli/commands/memory.ts +107 -0
- package/cli/commands/pricing.ts +100 -0
- package/cli/commands/providers.ts +162 -0
- package/cli/commands/sessions.ts +128 -0
- package/cli/commands/skills.ts +61 -0
- package/cli/commands/stats.ts +82 -0
- package/cli/commands.ts +317 -0
- package/cli/index.ts +539 -0
- package/cli/model-manager.ts +112 -0
- package/cli/provider-manager.ts +111 -0
- package/cli/repl.ts +734 -0
- package/cli/router.ts +124 -0
- package/cli/setup.ts +780 -0
- package/cli/wizard.ts +21 -0
- package/docs/ARCHITECTURE.html +1222 -0
- package/docs/README.md +109 -0
- package/docs/architecture.md +44 -0
- package/docs/changelog.md +3 -0
- package/package.json +91 -0
- package/src/agents/pool.ts +43 -0
- package/src/app/mentions.ts +50 -0
- package/src/app/provider-layer.ts +118 -0
- package/src/app/rag-layer.ts +105 -0
- package/src/app/session.ts +154 -0
- package/src/app/tool-layer.ts +56 -0
- package/src/config.ts +372 -0
- package/src/constants.ts +119 -0
- package/src/hooks/run.ts +89 -0
- package/src/lib/atomic-write.ts +88 -0
- package/src/lib/db-path.ts +36 -0
- package/src/lib/git-hardening.ts +91 -0
- package/src/lib/ignore.ts +58 -0
- package/src/lib/keystore.ts +202 -0
- package/src/lib/net.ts +70 -0
- package/src/lib/safe-open.ts +138 -0
- package/src/lib/session-id.ts +23 -0
- package/src/lib/trash.ts +38 -0
- package/src/lib/trusted-exec.ts +81 -0
- package/src/lsp/client.ts +395 -0
- package/src/mcp/client.ts +400 -0
- package/src/mcp/http-transport.ts +344 -0
- package/src/mcp/server.ts +538 -0
- package/src/mcp/transport.ts +150 -0
- package/src/memory/files.ts +159 -0
- package/src/memory/vector.ts +775 -0
- package/src/policy/allowlist.ts +67 -0
- package/src/policy/bash-guard.ts +465 -0
- package/src/policy/compaction.ts +271 -0
- package/src/policy/context.ts +161 -0
- package/src/policy/executor.ts +199 -0
- package/src/policy/jail.ts +127 -0
- package/src/policy/permission.ts +517 -0
- package/src/policy/pricing.ts +336 -0
- package/src/policy/ratelimit.ts +54 -0
- package/src/policy/sandbox-policy.ts +157 -0
- package/src/policy/scrub.ts +138 -0
- package/src/policy/update-check.ts +200 -0
- package/src/policy/usage.ts +292 -0
- package/src/policy/verifier.ts +181 -0
- package/src/providers/anthropic.ts +401 -0
- package/src/providers/auth-store.ts +139 -0
- package/src/providers/build.ts +136 -0
- package/src/providers/detect.ts +126 -0
- package/src/providers/effort.ts +165 -0
- package/src/providers/guards.ts +107 -0
- package/src/providers/oauth.ts +326 -0
- package/src/providers/presets.ts +122 -0
- package/src/providers/provision.ts +298 -0
- package/src/providers/responses.ts +371 -0
- package/src/providers/router.ts +247 -0
- package/src/repo/repomap.ts +470 -0
- package/src/sandbox/docker.ts +129 -0
- package/src/sandbox/os.ts +193 -0
- package/src/session/checkpoint.ts +543 -0
- package/src/session/journal.ts +1347 -0
- package/src/session/persistence.ts +446 -0
- package/src/session/shadow-git.ts +351 -0
- package/src/session/turn-marker.ts +118 -0
- package/src/skills/loader.ts +117 -0
- package/src/telemetry/trace.ts +283 -0
- package/src/tools/ask_user.ts +54 -0
- package/src/tools/bash.ts +400 -0
- package/src/tools/code_run.ts +96 -0
- package/src/tools/delete_file.ts +31 -0
- package/src/tools/edit.ts +204 -0
- package/src/tools/git.ts +301 -0
- package/src/tools/glob.ts +90 -0
- package/src/tools/grep.ts +281 -0
- package/src/tools/hashline.ts +66 -0
- package/src/tools/index.ts +103 -0
- package/src/tools/lsp.ts +346 -0
- package/src/tools/mcp_call.ts +163 -0
- package/src/tools/memory.ts +144 -0
- package/src/tools/move_file.ts +47 -0
- package/src/tools/patch.ts +86 -0
- package/src/tools/read_file.ts +130 -0
- package/src/tools/read_image.ts +80 -0
- package/src/tools/submit_result.ts +49 -0
- package/src/tools/task.ts +346 -0
- package/src/tools/todo.ts +201 -0
- package/src/tools/web_fetch.ts +161 -0
- package/src/tools/web_search.ts +156 -0
- package/src/tools/write_file.ts +42 -0
- package/src/ui/approval/prompt.ts +76 -0
- package/src/ui/assistant/simple.ts +568 -0
- package/src/ui/assistant/turn-status.ts +303 -0
- package/src/ui/contract.ts +55 -0
- package/src/ui/footer.ts +105 -0
- package/src/ui/input/input.ts +805 -0
- package/src/ui/input/prompt-engine.ts +606 -0
- package/src/ui/render/collapse.ts +84 -0
- package/src/ui/render/detail.ts +24 -0
- package/src/ui/render/diff.ts +181 -0
- package/src/ui/render/errors.ts +239 -0
- package/src/ui/render/format.ts +54 -0
- package/src/ui/render/highlight.ts +294 -0
- package/src/ui/render/markdown.ts +120 -0
- package/src/ui/render/money.ts +12 -0
- package/src/ui/render/reasoning.ts +20 -0
- package/src/ui/render/sanitize.ts +80 -0
- package/src/ui/render/table.ts +88 -0
- package/src/ui/render/theme.ts +339 -0
- package/src/ui/render/width.ts +256 -0
- package/src/ui/render/wrap.ts +120 -0
- package/src/ui/runtime/bus-debug.ts +83 -0
- package/src/ui/runtime/chrome.ts +301 -0
- package/src/ui/runtime/spinner.ts +77 -0
- package/src/ui/runtime/statusline.ts +227 -0
- package/src/ui/screens/model-manager.ts +434 -0
- package/src/ui/screens/overlay.ts +61 -0
- package/src/ui/screens/picker.ts +307 -0
- package/src/ui/screens/provider-manager.ts +450 -0
- package/src/ui/screens/wizard.ts +132 -0
- package/vendor/minicore/VENDOR.md +29 -0
- package/vendor/minicore/package.json +21 -0
- package/vendor/minicore/src/core/budget.ts +26 -0
- package/vendor/minicore/src/core/compact.ts +76 -0
- package/vendor/minicore/src/core/errors.ts +60 -0
- package/vendor/minicore/src/core/events.ts +74 -0
- package/vendor/minicore/src/core/executor.ts +139 -0
- package/vendor/minicore/src/core/history.ts +32 -0
- package/vendor/minicore/src/core/index.ts +42 -0
- package/vendor/minicore/src/core/loop.ts +344 -0
- package/vendor/minicore/src/core/permission.ts +24 -0
- package/vendor/minicore/src/core/provider.ts +39 -0
- package/vendor/minicore/src/core/recovery.ts +34 -0
- package/vendor/minicore/src/core/session.ts +327 -0
- package/vendor/minicore/src/core/snapshot.ts +142 -0
- package/vendor/minicore/src/core/tokens.ts +111 -0
- package/vendor/minicore/src/core/tool.ts +118 -0
- package/vendor/minicore/src/core/types.ts +50 -0
- package/vendor/minicore/src/providers/openai-compat.ts +366 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { c } from "./theme.ts"
|
|
2
|
+
|
|
3
|
+
const TS_KEYWORDS = new Set([
|
|
4
|
+
"const",
|
|
5
|
+
"let",
|
|
6
|
+
"var",
|
|
7
|
+
"function",
|
|
8
|
+
"return",
|
|
9
|
+
"if",
|
|
10
|
+
"else",
|
|
11
|
+
"for",
|
|
12
|
+
"while",
|
|
13
|
+
"do",
|
|
14
|
+
"switch",
|
|
15
|
+
"case",
|
|
16
|
+
"break",
|
|
17
|
+
"continue",
|
|
18
|
+
"default",
|
|
19
|
+
"try",
|
|
20
|
+
"catch",
|
|
21
|
+
"finally",
|
|
22
|
+
"throw",
|
|
23
|
+
"async",
|
|
24
|
+
"await",
|
|
25
|
+
"import",
|
|
26
|
+
"export",
|
|
27
|
+
"from",
|
|
28
|
+
"as",
|
|
29
|
+
"class",
|
|
30
|
+
"extends",
|
|
31
|
+
"implements",
|
|
32
|
+
"interface",
|
|
33
|
+
"type",
|
|
34
|
+
"enum",
|
|
35
|
+
"namespace",
|
|
36
|
+
"new",
|
|
37
|
+
"this",
|
|
38
|
+
"super",
|
|
39
|
+
"typeof",
|
|
40
|
+
"instanceof",
|
|
41
|
+
"in",
|
|
42
|
+
"of",
|
|
43
|
+
"yield",
|
|
44
|
+
"void",
|
|
45
|
+
"null",
|
|
46
|
+
"undefined",
|
|
47
|
+
"true",
|
|
48
|
+
"false",
|
|
49
|
+
])
|
|
50
|
+
|
|
51
|
+
const PY_KEYWORDS = new Set([
|
|
52
|
+
"def",
|
|
53
|
+
"return",
|
|
54
|
+
"if",
|
|
55
|
+
"elif",
|
|
56
|
+
"else",
|
|
57
|
+
"for",
|
|
58
|
+
"while",
|
|
59
|
+
"break",
|
|
60
|
+
"continue",
|
|
61
|
+
"try",
|
|
62
|
+
"except",
|
|
63
|
+
"finally",
|
|
64
|
+
"raise",
|
|
65
|
+
"import",
|
|
66
|
+
"from",
|
|
67
|
+
"as",
|
|
68
|
+
"class",
|
|
69
|
+
"pass",
|
|
70
|
+
"with",
|
|
71
|
+
"async",
|
|
72
|
+
"await",
|
|
73
|
+
"yield",
|
|
74
|
+
"lambda",
|
|
75
|
+
"global",
|
|
76
|
+
"nonlocal",
|
|
77
|
+
"assert",
|
|
78
|
+
"True",
|
|
79
|
+
"False",
|
|
80
|
+
"None",
|
|
81
|
+
"and",
|
|
82
|
+
"or",
|
|
83
|
+
"not",
|
|
84
|
+
"is",
|
|
85
|
+
"in",
|
|
86
|
+
])
|
|
87
|
+
|
|
88
|
+
const SH_KEYWORDS = new Set([
|
|
89
|
+
"if",
|
|
90
|
+
"then",
|
|
91
|
+
"else",
|
|
92
|
+
"elif",
|
|
93
|
+
"fi",
|
|
94
|
+
"for",
|
|
95
|
+
"in",
|
|
96
|
+
"do",
|
|
97
|
+
"done",
|
|
98
|
+
"while",
|
|
99
|
+
"until",
|
|
100
|
+
"case",
|
|
101
|
+
"esac",
|
|
102
|
+
"function",
|
|
103
|
+
"return",
|
|
104
|
+
"exit",
|
|
105
|
+
"export",
|
|
106
|
+
"set",
|
|
107
|
+
"unset",
|
|
108
|
+
"echo",
|
|
109
|
+
"cd",
|
|
110
|
+
"mkdir",
|
|
111
|
+
"rm",
|
|
112
|
+
"cp",
|
|
113
|
+
"mv",
|
|
114
|
+
"touch",
|
|
115
|
+
"cat",
|
|
116
|
+
"grep",
|
|
117
|
+
"find",
|
|
118
|
+
"curl",
|
|
119
|
+
"git",
|
|
120
|
+
])
|
|
121
|
+
|
|
122
|
+
export function highlightCode(code: string, lang: string = ""): string {
|
|
123
|
+
const language = lang.trim().toLowerCase()
|
|
124
|
+
const lines = code.split("\n")
|
|
125
|
+
|
|
126
|
+
return lines
|
|
127
|
+
.map((line) => {
|
|
128
|
+
if (language === "json") return highlightJsonLine(line)
|
|
129
|
+
if (language === "python" || language === "py") return highlightPythonLine(line)
|
|
130
|
+
if (language === "bash" || language === "sh" || language === "zsh" || language === "shell")
|
|
131
|
+
return highlightShellLine(line)
|
|
132
|
+
if (language === "diff") return highlightDiffLine(line)
|
|
133
|
+
// default: ts / js / tsx / jsx / generic
|
|
134
|
+
return highlightTsLine(line)
|
|
135
|
+
})
|
|
136
|
+
.join("\n")
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Cari penanda komentar (`//`, `#`) di LUAR string literal.
|
|
141
|
+
*
|
|
142
|
+
* Sebelumnya `indexOf("//")`/`indexOf("#")` menyalahartikan
|
|
143
|
+
* `const url = "https://a.com"` (semua setelah `//` dianggap komentar) dan
|
|
144
|
+
* `color = "#fff"` (`#fff"` jadi komentar). Scanner di bawah melompati isi
|
|
145
|
+
* string kutip tunggal/ganda/backtick termasuk escape.
|
|
146
|
+
*/
|
|
147
|
+
function findCommentIndex(line: string, marker: string): number {
|
|
148
|
+
let quote: string | null = null
|
|
149
|
+
let i = 0
|
|
150
|
+
while (i < line.length) {
|
|
151
|
+
const ch = line[i]!
|
|
152
|
+
if (quote !== null) {
|
|
153
|
+
if (ch === "\\") i += 2
|
|
154
|
+
else if (ch === quote) quote = null
|
|
155
|
+
i++
|
|
156
|
+
continue
|
|
157
|
+
}
|
|
158
|
+
if (ch === '"' || ch === "'" || ch === "`") {
|
|
159
|
+
quote = ch
|
|
160
|
+
i++
|
|
161
|
+
continue
|
|
162
|
+
}
|
|
163
|
+
if (line.startsWith(marker, i)) return i
|
|
164
|
+
i++
|
|
165
|
+
}
|
|
166
|
+
return -1
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function highlightTsLine(line: string): string {
|
|
170
|
+
// Comments
|
|
171
|
+
const commentIdx = findCommentIndex(line, "//")
|
|
172
|
+
if (commentIdx !== -1) {
|
|
173
|
+
const codePart = line.slice(0, commentIdx)
|
|
174
|
+
const commentPart = line.slice(commentIdx)
|
|
175
|
+
return highlightTsTokens(codePart) + c.gray(c.italic(commentPart))
|
|
176
|
+
}
|
|
177
|
+
return highlightTsTokens(line)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function highlightTsTokens(text: string): string {
|
|
181
|
+
// Regex to match string literals, numbers, identifiers, operators
|
|
182
|
+
const tokenRe =
|
|
183
|
+
/("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`(?:\\.|[^`\\])*`|\b\d+(?:\.\d+)?\b|\b[A-Za-z_$][A-Za-z0-9_$]*\b|[{}()[\];:,.<>=+\-*/%!&|^~?]+|\s+)/g
|
|
184
|
+
return text.replace(tokenRe, (token) => {
|
|
185
|
+
if (token.startsWith('"') || token.startsWith("'") || token.startsWith("`")) {
|
|
186
|
+
return c.green(token)
|
|
187
|
+
}
|
|
188
|
+
if (/^\d+(?:\.\d+)?$/.test(token)) {
|
|
189
|
+
return c.brightYellow(token)
|
|
190
|
+
}
|
|
191
|
+
if (TS_KEYWORDS.has(token)) {
|
|
192
|
+
return c.brightMagenta(c.bold(token))
|
|
193
|
+
}
|
|
194
|
+
if (/^[A-Z][A-Za-z0-9_$]*$/.test(token)) {
|
|
195
|
+
// Type / Class name
|
|
196
|
+
return c.brightCyan(token)
|
|
197
|
+
}
|
|
198
|
+
// Tidak ada cabang khusus untuk true/false/null/undefined: keempatnya sudah
|
|
199
|
+
// ada di TS_KEYWORDS, jadi cabang di atas selalu menang. Cabang terpisah
|
|
200
|
+
// yang pernah ada di sini tak pernah terjangkau (terlihat saat coverage
|
|
201
|
+
// highlight.ts dinaikkan: satu baris mustahil dicapai test apa pun).
|
|
202
|
+
return token
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function highlightPythonLine(line: string): string {
|
|
207
|
+
const commentIdx = findCommentIndex(line, "#")
|
|
208
|
+
if (commentIdx !== -1) {
|
|
209
|
+
const codePart = line.slice(0, commentIdx)
|
|
210
|
+
const commentPart = line.slice(commentIdx)
|
|
211
|
+
return highlightPythonTokens(codePart) + c.gray(c.italic(commentPart))
|
|
212
|
+
}
|
|
213
|
+
return highlightPythonTokens(line)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function highlightPythonTokens(text: string): string {
|
|
217
|
+
const tokenRe =
|
|
218
|
+
/("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\b\d+(?:\.\d+)?\b|\b[A-Za-z_][A-Za-z0-9_]*\b|[{}()[\];:,.<>=+\-*/%!&|^~?]+|\s+)/g
|
|
219
|
+
return text.replace(tokenRe, (token) => {
|
|
220
|
+
if (token.startsWith('"') || token.startsWith("'")) {
|
|
221
|
+
return c.green(token)
|
|
222
|
+
}
|
|
223
|
+
if (/^\d+/.test(token)) return c.brightYellow(token)
|
|
224
|
+
if (PY_KEYWORDS.has(token)) return c.brightMagenta(c.bold(token))
|
|
225
|
+
if (/^[A-Z][A-Za-z0-9_]*$/.test(token)) return c.brightCyan(token)
|
|
226
|
+
return token
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function highlightJsonLine(line: string): string {
|
|
231
|
+
// Key vs string value vs number/bool
|
|
232
|
+
return line.replace(
|
|
233
|
+
/("(?:\\.|[^"\\])*")(\s*:)?|\b(true|false|null)\b|\b(-?\d+(?:\.\d+)?)\b/g,
|
|
234
|
+
(match, str, colon, bool, num) => {
|
|
235
|
+
if (str) {
|
|
236
|
+
if (colon) return c.brightCyan(str) + colon
|
|
237
|
+
return c.green(str)
|
|
238
|
+
}
|
|
239
|
+
if (bool) return c.yellow(bool)
|
|
240
|
+
if (num) return c.brightYellow(num)
|
|
241
|
+
return match
|
|
242
|
+
},
|
|
243
|
+
)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function highlightShellLine(line: string): string {
|
|
247
|
+
const commentIdx = findCommentIndex(line, "#")
|
|
248
|
+
if (commentIdx !== -1) {
|
|
249
|
+
const codePart = line.slice(0, commentIdx)
|
|
250
|
+
const commentPart = line.slice(commentIdx)
|
|
251
|
+
return highlightShellTokens(codePart) + c.gray(c.italic(commentPart))
|
|
252
|
+
}
|
|
253
|
+
return highlightShellTokens(line)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function highlightShellTokens(text: string): string {
|
|
257
|
+
return text.replace(
|
|
258
|
+
/("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\$[A-Za-z0-9_]+|\b[A-Za-z_][A-Za-z0-9_-]*\b|\s+)/g,
|
|
259
|
+
(token) => {
|
|
260
|
+
if (token.startsWith('"') || token.startsWith("'")) return c.green(token)
|
|
261
|
+
if (token.startsWith("$")) return c.brightCyan(token)
|
|
262
|
+
if (SH_KEYWORDS.has(token)) return c.brightMagenta(c.bold(token))
|
|
263
|
+
if (token.startsWith("-")) return c.yellow(token)
|
|
264
|
+
return token
|
|
265
|
+
},
|
|
266
|
+
)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function highlightDiffLine(line: string): string {
|
|
270
|
+
if (line.startsWith("+") && !line.startsWith("+++")) return c.green(line)
|
|
271
|
+
if (line.startsWith("-") && !line.startsWith("---")) return c.red(line)
|
|
272
|
+
if (line.startsWith("@@")) return c.cyan(line)
|
|
273
|
+
return line
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export function formatCodeBlock(code: string, lang: string = "", maxLines?: number): string {
|
|
277
|
+
const highlighted = highlightCode(code, lang)
|
|
278
|
+
const lines = highlighted.split("\n")
|
|
279
|
+
const displayLines =
|
|
280
|
+
maxLines && lines.length > maxLines
|
|
281
|
+
? [...lines.slice(0, maxLines), c.dim(` ... (${lines.length - maxLines} more lines)`)]
|
|
282
|
+
: lines
|
|
283
|
+
|
|
284
|
+
const header = lang ? ` ${lang.toLowerCase()} ` : " code "
|
|
285
|
+
const topBorder = c.muted(`── ${header.trim()}`)
|
|
286
|
+
const bottomBorder = c.muted("──")
|
|
287
|
+
|
|
288
|
+
const formattedLines = displayLines.map((l, i) => {
|
|
289
|
+
const lineNum = c.muted(`${String(i + 1).padStart(4, " ")} `)
|
|
290
|
+
return `${lineNum}${l}`
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
return `${topBorder}\n${formattedLines.join("\n")}\n${bottomBorder}`
|
|
294
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Markdown decoration - Ubuntu Server style.
|
|
2
|
+
// Heading -> bold, bullet -> indentasi, code fence -> indentasi + syntax highlight.
|
|
3
|
+
// Inline: **bold**, *italic*, `code`, ~~strike~~, [text](url) -> teks accent (no underline).
|
|
4
|
+
|
|
5
|
+
import { highlightCode } from "./highlight.ts"
|
|
6
|
+
import { c } from "./theme.ts"
|
|
7
|
+
|
|
8
|
+
export interface FenceMatch {
|
|
9
|
+
char: string
|
|
10
|
+
len: number
|
|
11
|
+
lang: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Parse satu baris fence markdown (``` atau ~~~) → info pembuka, atau null.
|
|
16
|
+
*
|
|
17
|
+
* Diekspor agar simple.ts (printer linier streaming) memakai parser yang SAMA
|
|
18
|
+
* dengan decorateMarkdown — sebelumnya ia men-toggle `inFence` naif per baris
|
|
19
|
+
* (`line.includes("```")`) yang tidak kenal ~~~ dan tidak cek char/panjang,
|
|
20
|
+
* sehingga fence berbahasa kehilangan highlight dan state desync.
|
|
21
|
+
*/
|
|
22
|
+
export function parseFence(line: string): FenceMatch | null {
|
|
23
|
+
const m = /^\s*(```+|~~~+)([A-Za-z0-9_+.-]*)\s*$/.exec(line)
|
|
24
|
+
if (!m) return null
|
|
25
|
+
return { char: m[1]![0]!, len: m[1]!.length, lang: m[2] ?? "" }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ── Inline markdown -> ANSI ──
|
|
29
|
+
// Urutan replace lama: `code` diganti dulu jadi `c.brightCyan(...)`, lalu
|
|
30
|
+
// regex **bold** masih menemukan `**` DI DALAM hasil cyan → `` `**a**` ``
|
|
31
|
+
// menjadi bold+cyan ganda. Isi inline code tidak boleh di-dekorasi lagi.
|
|
32
|
+
const CODE_PLACEHOLDER = "\u0000"
|
|
33
|
+
export function renderInline(text: string): string {
|
|
34
|
+
// Simpan isi `code` dulu, restore TERAKHIR supaya bold/italic/strike tidak
|
|
35
|
+
// menyentuhnya. Placeholder ASCII-null tidak mungkin muncul di teks model
|
|
36
|
+
// (sanitizeAnsi membuang semua C0 termasuk null).
|
|
37
|
+
const codes: string[] = []
|
|
38
|
+
let t = text.replace(/`([^`]+)`/g, (_m, code: string) => {
|
|
39
|
+
codes.push(code)
|
|
40
|
+
return CODE_PLACEHOLDER
|
|
41
|
+
})
|
|
42
|
+
// [text](url) -> accent bold text (url hidden, no underline - terminal can't click)
|
|
43
|
+
t = t.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_m, label: string) => c.accent(c.bold(label)))
|
|
44
|
+
// **bold**
|
|
45
|
+
t = t.replace(/\*\*([^*]+)\*\*/g, (_m, bold: string) => c.bold(bold))
|
|
46
|
+
// *italic*
|
|
47
|
+
t = t.replace(
|
|
48
|
+
/(^|[^*])\*([^*\s][^*]*?)\*(?!\*)/g,
|
|
49
|
+
(_m, pre: string, ital: string) => `${pre}${c.italic(ital)}`,
|
|
50
|
+
)
|
|
51
|
+
// __bold__
|
|
52
|
+
t = t.replace(/__([^_]+)__/g, (_m, bold: string) => c.bold(bold))
|
|
53
|
+
// ~~strike~~
|
|
54
|
+
t = t.replace(/~~([^~]+)~~/g, (_m, del: string) => c.muted(del))
|
|
55
|
+
// Restore code blocks (bisa memuat * _ ~ ` → lindungi dari regex di atas).
|
|
56
|
+
let ci = 0
|
|
57
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: placeholder internal (ASCII null)
|
|
58
|
+
return t.replace(/\u0000/g, () => c.brightCyan(codes[ci++] ?? ""))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function decorateMarkdown(text: string): string {
|
|
62
|
+
const lines = text.split("\n")
|
|
63
|
+
const out: string[] = []
|
|
64
|
+
let inFence = false
|
|
65
|
+
let fenceLang = ""
|
|
66
|
+
let fenceChar = ""
|
|
67
|
+
let fenceLen = 0
|
|
68
|
+
|
|
69
|
+
for (const line of lines) {
|
|
70
|
+
// Code fence handling — catat char & panjang pembuka, hanya tutup bila cocok.
|
|
71
|
+
const fence = parseFence(line)
|
|
72
|
+
if (fence) {
|
|
73
|
+
if (!inFence) {
|
|
74
|
+
inFence = true
|
|
75
|
+
fenceChar = fence.char
|
|
76
|
+
fenceLen = fence.len
|
|
77
|
+
fenceLang = fence.lang
|
|
78
|
+
} else {
|
|
79
|
+
if (fence.char === fenceChar && fence.len >= fenceLen) {
|
|
80
|
+
inFence = false
|
|
81
|
+
fenceChar = ""
|
|
82
|
+
fenceLen = 0
|
|
83
|
+
fenceLang = ""
|
|
84
|
+
} else {
|
|
85
|
+
// fence di dalam fence dengan char/len berbeda → anggap konten
|
|
86
|
+
out.push(` ${fenceLang ? highlightCode(line, fenceLang) : line}`)
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
continue
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Di DALAM fence: isi kode tidak boleh disentuh markdown.
|
|
94
|
+
//
|
|
95
|
+
// Sebelumnya hanya fence BERBAHASA yang dilindungi (`if (inFence && fenceLang)`),
|
|
96
|
+
// sehingga fence tanpa bahasa jatuh ke renderInline di bawah dan
|
|
97
|
+
// `npm run build -- --flag=*value*` kehilangan bintangnya karena dianggap
|
|
98
|
+
// italic. Fence tanpa bahasa justru bentuk paling umum untuk perintah shell.
|
|
99
|
+
if (inFence) {
|
|
100
|
+
out.push(` ${fenceLang ? highlightCode(line, fenceLang) : line}`)
|
|
101
|
+
continue
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Headings (# ## ###) -> bold with vertical spacing
|
|
105
|
+
if (/^#{1,3}\s/.test(line)) {
|
|
106
|
+
if (out.length > 0 && out[out.length - 1] !== "") out.push("")
|
|
107
|
+
out.push(c.bold(line))
|
|
108
|
+
continue
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Bullet list -> indentasi
|
|
112
|
+
if (/^\s*[-*]\s/.test(line)) {
|
|
113
|
+
out.push(` ${renderInline(line.trimStart())}`)
|
|
114
|
+
continue
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
out.push(renderInline(line))
|
|
118
|
+
}
|
|
119
|
+
return out.join("\n")
|
|
120
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Formatter uang untuk UI biaya/anggaran.
|
|
2
|
+
//
|
|
3
|
+
// `toFixed(2)` merusak nilai kecil: `--budget 0.001` tampil sebagai `$0.00`,
|
|
4
|
+
// sehingga pesan pemutusnya berbunyi "$0.0601 > $0.00 - lewat batas" — user
|
|
5
|
+
// membaca batas nol padahal ia menyetel seperseribu dolar.
|
|
6
|
+
//
|
|
7
|
+
// Aturan: di bawah $1 pakai 4 desimal (satuan biaya per-run memang sekecil itu),
|
|
8
|
+
// $1 ke atas pakai 2 desimal seperti harga biasa.
|
|
9
|
+
export function formatUsd(value: number): string {
|
|
10
|
+
if (!Number.isFinite(value)) return "$?"
|
|
11
|
+
return Math.abs(value) < 1 ? `$${value.toFixed(4)}` : `$${value.toFixed(2)}`
|
|
12
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Satu sumber state "tampilkan reasoning".
|
|
2
|
+
//
|
|
3
|
+
// Getter — jangan simpan `reasoning.visible` ke const di module scope; lihat
|
|
4
|
+
// detail.ts. Perubahan env harus terbaca saat pakai.
|
|
5
|
+
export const reasoning = {
|
|
6
|
+
get visible(): boolean {
|
|
7
|
+
return process.env.MINICODE_SHOW_THINKING === "1"
|
|
8
|
+
},
|
|
9
|
+
set visible(v: boolean) {
|
|
10
|
+
process.env.MINICODE_SHOW_THINKING = v ? "1" : "0"
|
|
11
|
+
},
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Set eksplisit (on/off) atau toggle bila `next` tidak diberikan. */
|
|
15
|
+
export function setReasoningVisible(next?: boolean): boolean {
|
|
16
|
+
const cur = reasoning.visible
|
|
17
|
+
const nextVal = next ?? !cur
|
|
18
|
+
reasoning.visible = nextVal
|
|
19
|
+
return nextVal
|
|
20
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Sanitasi sekuens ANSI dari sumber tidak terpercaya (teks model, hasil tool,
|
|
2
|
+
// isi berkas).
|
|
3
|
+
//
|
|
4
|
+
// Renderer sengaja MEMPERTAHANKAN warna — itulah kenapa diff card hijau/merah
|
|
5
|
+
// dan markdown bold bekerja. Tapi mempertahankan semua sekuens berarti model
|
|
6
|
+
// bisa mengirim:
|
|
7
|
+
// ESC[2J ESC[H bersihkan layar & pindahkan kursor
|
|
8
|
+
// ESC[?1049h/l masuk/keluar alternate screen (merusak TUI)
|
|
9
|
+
// ESC]0;judul BEL ubah judul jendela terminal
|
|
10
|
+
// ESC[999B geser kursor keluar area yang dihitung renderer
|
|
11
|
+
// ESC[?25l sembunyikan kursor secara permanen
|
|
12
|
+
// Terverifikasi: teks model `aman\x1b[2J\x1b[H\x1b[?1049hJAHAT` benar-benar
|
|
13
|
+
// sampai ke terminal utuh.
|
|
14
|
+
//
|
|
15
|
+
// Kebijakan: HANYA SGR (`ESC[…m`) yang dipertahankan — itu satu-satunya yang
|
|
16
|
+
// dibutuhkan untuk warna dan atribut teks. Semua sekuens lain dibuang, termasuk
|
|
17
|
+
// OSC, DCS, dan CSI non-SGR. Karakter kontrol C0 selain tab dibuang juga
|
|
18
|
+
// (BEL membunyikan bel terminal, BS/CR memindahkan kursor mundur).
|
|
19
|
+
|
|
20
|
+
import { escapeLength } from "./width.ts"
|
|
21
|
+
|
|
22
|
+
/** Apakah sekuens di posisi `i` adalah SGR (pewarnaan) yang boleh lewat? */
|
|
23
|
+
function isSgr(s: string, i: number, len: number): boolean {
|
|
24
|
+
if (s[i + 1] !== "[") return false
|
|
25
|
+
// SGR berakhir dengan 'm' dan parameternya hanya digit/;/: (bukan '?', '<', dll).
|
|
26
|
+
if (s[i + len - 1] !== "m") return false
|
|
27
|
+
for (let k = i + 2; k < i + len - 1; k++) {
|
|
28
|
+
const ch = s[k]!
|
|
29
|
+
if (!(ch >= "0" && ch <= "9") && ch !== ";" && ch !== ":") return false
|
|
30
|
+
}
|
|
31
|
+
return true
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Buang semua sekuens kontrol kecuali SGR. Teks tampak tidak berubah.
|
|
36
|
+
*
|
|
37
|
+
* Dipakai pada SEMUA teks yang berasal dari luar: `provider:text`, isi hasil
|
|
38
|
+
* tool, dan konten berkas yang ditampilkan.
|
|
39
|
+
*/
|
|
40
|
+
export function sanitizeAnsi(s: string): string {
|
|
41
|
+
let out = ""
|
|
42
|
+
let i = 0
|
|
43
|
+
while (i < s.length) {
|
|
44
|
+
const ch = s[i]!
|
|
45
|
+
if (ch === "\x1b") {
|
|
46
|
+
const len = escapeLength(s, i)
|
|
47
|
+
if (len > 0) {
|
|
48
|
+
if (isSgr(s, i, len)) out += s.slice(i, i + len)
|
|
49
|
+
i += len
|
|
50
|
+
continue
|
|
51
|
+
}
|
|
52
|
+
// ESC tunggal tanpa sekuens yang dikenali: buang.
|
|
53
|
+
i += 1
|
|
54
|
+
continue
|
|
55
|
+
}
|
|
56
|
+
// Kontrol C0: tab & newline dipertahankan (pemanggil yang memecah baris),
|
|
57
|
+
// sisanya dibuang. CR khususnya berbahaya — ia menimpa baris yang sudah
|
|
58
|
+
// digambar.
|
|
59
|
+
const code = ch.charCodeAt(0)
|
|
60
|
+
if (code < 0x20 && ch !== "\t" && ch !== "\n") {
|
|
61
|
+
i += 1
|
|
62
|
+
continue
|
|
63
|
+
}
|
|
64
|
+
if (code === 0x7f) {
|
|
65
|
+
i += 1
|
|
66
|
+
continue
|
|
67
|
+
}
|
|
68
|
+
out += ch
|
|
69
|
+
i += 1
|
|
70
|
+
}
|
|
71
|
+
return out
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Versi satu-baris: newline juga dibuang (jadi spasi).
|
|
76
|
+
* Untuk tempat yang menggambar tepat satu baris — judul, label, sel tabel.
|
|
77
|
+
*/
|
|
78
|
+
export function sanitizeAnsiLine(s: string): string {
|
|
79
|
+
return sanitizeAnsi(s).replace(/\n/g, " ")
|
|
80
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { sanitizeAnsiLine } from "./sanitize.ts"
|
|
2
|
+
import { c, getTerminalWidth } from "./theme.ts"
|
|
3
|
+
import { displayWidth, padToWidth, truncateToWidth } from "./width.ts"
|
|
4
|
+
|
|
5
|
+
export interface ColumnDef {
|
|
6
|
+
header: string
|
|
7
|
+
/** Lebar kolom dalam KOLOM terminal sebagai BATAS KERAS; isi lebih panjang dipotong "…". */
|
|
8
|
+
width?: number
|
|
9
|
+
key: string
|
|
10
|
+
align?: "left" | "right"
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const ELLIPSIS = "\u2026"
|
|
14
|
+
/** Batas otomatis bila `width` tidak diberikan. */
|
|
15
|
+
const AUTO_MAX = 50
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Nilai sel siap tampil: newline/tab/karakter kontrol dibuang.
|
|
19
|
+
*
|
|
20
|
+
* Satu newline dalam nilai memecah baris tabel menjadi dua dan seluruh
|
|
21
|
+
* kolom setelahnya bergeser — pemanggil (`config list`, `providers`, `skills`)
|
|
22
|
+
* mengambil nilai dari config/frontmatter yang bisa berisi apa pun.
|
|
23
|
+
*/
|
|
24
|
+
function sanitizeCell(v: unknown): string {
|
|
25
|
+
const s = v == null ? "" : String(v)
|
|
26
|
+
// Sekuens non-SGR dibuang, SGR dipertahankan — sel tabel bisa berisi data tidak terpercaya.
|
|
27
|
+
return sanitizeAnsiLine(s)
|
|
28
|
+
.replace(/[\t\v\f\u0085\u2028\u2029]/g, " ")
|
|
29
|
+
.replace(/\r/g, " ")
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Table minimal - kolom aligned + separator header, tanpa border.
|
|
33
|
+
export function renderTable(columns: ColumnDef[], data: Record<string, unknown>[]): string {
|
|
34
|
+
if (columns.length === 0) return c.muted("(no columns)")
|
|
35
|
+
if (data.length === 0) return c.muted("(no entries)")
|
|
36
|
+
|
|
37
|
+
const cells = data.map((row) => columns.map((col) => sanitizeCell(row[col.key])))
|
|
38
|
+
|
|
39
|
+
const termW = getTerminalWidth()
|
|
40
|
+
// Terminal sangat sempit + banyak kolom: budget per kolom (~10) membuat
|
|
41
|
+
// semua isi jadi "…" — render vertikal `key: value` agar tetap terbaca.
|
|
42
|
+
if (termW < 40 && columns.length > 3) {
|
|
43
|
+
return data
|
|
44
|
+
.map((row) =>
|
|
45
|
+
columns
|
|
46
|
+
.map(
|
|
47
|
+
(col) =>
|
|
48
|
+
`${c.bold(col.header)}: ${truncateToWidth(sanitizeCell(row[col.key]), termW, ELLIPSIS)}`,
|
|
49
|
+
)
|
|
50
|
+
.join("\n"),
|
|
51
|
+
)
|
|
52
|
+
.join("\n\n")
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// `width` yang dideklarasikan adalah batas keras dalam KOLOM terminal
|
|
56
|
+
// (CJK/emoji dihitung dua). Sebelumnya ia hanya MINIMUM dan diukur per
|
|
57
|
+
// karakter, sehingga satu nilai panjang mendorong kolom melebar dan header
|
|
58
|
+
// berhenti berbaris dengan body.
|
|
59
|
+
const widths = columns.map((col, i) => {
|
|
60
|
+
// Nilai negatif/NaN dari pemanggil tidak boleh membuat "".repeat() melempar.
|
|
61
|
+
if (col.width != null && Number.isFinite(col.width)) return Math.max(0, Math.trunc(col.width))
|
|
62
|
+
let max = displayWidth(col.header)
|
|
63
|
+
for (const row of cells) {
|
|
64
|
+
const w = displayWidth(row[i] ?? "")
|
|
65
|
+
if (w > max) max = w
|
|
66
|
+
}
|
|
67
|
+
// AUTO_MAX dihormati, tapi di terminal sempit kurangi agar tidak overflow.
|
|
68
|
+
const budget = Math.max(10, Math.floor((termW - columns.length * 3) / columns.length))
|
|
69
|
+
return Math.min(max, Math.min(AUTO_MAX, budget))
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
const cell = (text: string, width: number, align: "left" | "right" = "left"): string =>
|
|
73
|
+
padToWidth(truncateToWidth(text, width, ELLIPSIS), width, align)
|
|
74
|
+
|
|
75
|
+
const header = columns
|
|
76
|
+
.map((col, i) => ` ${c.bold(c.accent(cell(col.header, widths[i]!, col.align)))} `)
|
|
77
|
+
.join(" ")
|
|
78
|
+
|
|
79
|
+
// Separator (─ antara header dan body). Lebarnya harus SAMA dengan baris
|
|
80
|
+
// data: tiap sel adalah " isi " (width+2) dan antar sel disatukan satu spasi.
|
|
81
|
+
const sep = c.dim(widths.map((w) => "─".repeat(w + 2)).join(" "))
|
|
82
|
+
|
|
83
|
+
const rows = cells.map((row) =>
|
|
84
|
+
columns.map((col, i) => ` ${cell(row[i] ?? "", widths[i]!, col.align)} `).join(" "),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
return [sep, header, ...rows].join("\n")
|
|
88
|
+
}
|