qwenproxy-cli 1.0.0 → 1.0.2
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/LICENSE +14 -14
- package/README.md +906 -906
- package/bin/qwenproxy.js +5 -1
- package/package.json +77 -78
- package/src/api/error-classifier.ts +159 -159
- package/src/api/error-helpers.ts +118 -118
- package/src/api/models.ts +261 -261
- package/src/api/server.ts +860 -859
- package/src/cache/memory-cache.ts +385 -385
- package/src/clean-cache.ts +204 -204
- package/src/core/account-concurrency.ts +671 -671
- package/src/core/account-manager.ts +301 -297
- package/src/core/account-priority.ts +163 -163
- package/src/core/accounts.ts +186 -186
- package/src/core/config.ts +383 -383
- package/src/core/crypto-utils.ts +79 -79
- package/src/core/database.ts +276 -276
- package/src/core/errors.ts +118 -118
- package/src/core/logger.ts +269 -269
- package/src/core/memory-usage.ts +84 -84
- package/src/core/metrics.ts +291 -291
- package/src/core/model-alias.ts +77 -77
- package/src/core/model-registry.ts +544 -544
- package/src/core/mutex.ts +119 -119
- package/src/core/paths.ts +199 -199
- package/src/core/prompt-limits.ts +214 -214
- package/src/core/reasoning-effort.ts +102 -102
- package/src/core/stream-registry.ts +96 -96
- package/src/core/waf-isolation.ts +117 -117
- package/src/core/watchdog.ts +195 -195
- package/src/delete-chats.ts +23 -23
- package/src/index.ts +65 -64
- package/src/login.ts +147 -147
- package/src/reset-cooldowns.ts +11 -11
- package/src/routes/anthropic/index.ts +355 -355
- package/src/routes/anthropic/translate.ts +522 -522
- package/src/routes/anthropic/types.ts +154 -154
- package/src/routes/anthropic/validation.ts +144 -144
- package/src/routes/chat/account.ts +1817 -1817
- package/src/routes/chat/context.ts +241 -241
- package/src/routes/chat/errors.ts +85 -85
- package/src/routes/chat/helpers.ts +268 -268
- package/src/routes/chat/index.ts +618 -618
- package/src/routes/chat/media.ts +285 -285
- package/src/routes/chat/retry-policy.ts +754 -754
- package/src/routes/chat/stop.ts +98 -98
- package/src/routes/chat/streaming.ts +2710 -2710
- package/src/routes/chat/validation.ts +526 -526
- package/src/routes/chat.ts +2 -2
- package/src/routes/completions.ts +290 -290
- package/src/routes/images.ts +139 -139
- package/src/routes/responses/adapter.ts +503 -503
- package/src/routes/responses/index.ts +405 -405
- package/src/routes/responses/state.ts +230 -230
- package/src/routes/responses/streaming.ts +528 -528
- package/src/routes/responses/types.ts +285 -285
- package/src/routes/responses/validation.ts +202 -202
- package/src/routes/upload.ts +731 -731
- package/src/routes/videos.ts +214 -214
- package/src/services/auth-playwright.ts +173 -173
- package/src/services/captcha-coordinator.ts +161 -161
- package/src/services/captcha-solver.ts +553 -553
- package/src/services/chat-cleanup.ts +80 -80
- package/src/services/context-meter.ts +317 -317
- package/src/services/fingerprint.ts +242 -242
- package/src/services/human-behavior.ts +173 -173
- package/src/services/media-generation.ts +1748 -1748
- package/src/services/playwright.ts +2878 -2800
- package/src/services/qwen-chat-pool.ts +345 -345
- package/src/services/qwen-errors.ts +133 -133
- package/src/services/qwen-headers.ts +79 -79
- package/src/services/qwen-thread-state.ts +393 -393
- package/src/services/qwen-url.ts +19 -19
- package/src/services/qwen.ts +3126 -3126
- package/src/services/session-keeper.ts +88 -88
- package/src/services/token-estimation-metrics.ts +118 -118
- package/src/sync/claude-code.ts +75 -75
- package/src/sync/codex.ts +123 -123
- package/src/sync/index.ts +362 -362
- package/src/sync/omp.ts +105 -105
- package/src/sync/opencode.ts +214 -214
- package/src/sync/types.ts +53 -53
- package/src/sync/utils.ts +27 -27
- package/src/sync-clients.ts +189 -189
- package/src/tools/instructions.ts +137 -137
- package/src/tools/manifest.ts +81 -81
- package/src/tools/parser.ts +2989 -2989
- package/src/tools/toolcall-tags.ts +142 -142
- package/src/tui/app.ts +259 -264
- package/src/tui/index.ts +61 -61
- package/src/tui/markdown.ts +258 -258
- package/src/tui/proxy-client.ts +331 -326
- package/src/tui/screen.ts +294 -278
- package/src/tui/server-manager.ts +270 -270
- package/src/tui/theme.ts +432 -432
- package/src/tui/types.ts +33 -33
- package/src/tui/views/accounts-view.ts +656 -656
- package/src/tui/views/chat-view.ts +1018 -823
- package/src/tui/views/logs-view.ts +479 -413
- package/src/tui/views/status-view.ts +204 -204
- package/src/tui/views/storage-view.ts +304 -291
- package/src/tui/views/sync-view.ts +409 -409
- package/src/types/ali-oss.d.ts +32 -32
- package/src/update-cli.ts +121 -0
- package/src/utils/context-truncation.ts +84 -84
- package/src/utils/json.ts +380 -380
- package/src/utils/session-id.ts +37 -37
- package/src/utils/tool-call-guard.ts +84 -84
- package/src/utils/types.ts +109 -109
package/src/tui/markdown.ts
CHANGED
|
@@ -1,258 +1,258 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* QwenProxy TUI - Lightweight Terminal Markdown Formatter
|
|
3
|
-
* Formats Markdown bold, italic, code blocks, bullet lists, and headers into ANSI styles
|
|
4
|
-
* with word-wrapping that respects terminal column constraints.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { theme, stringWidth } from "./theme.ts";
|
|
8
|
-
export interface MarkdownOptions {
|
|
9
|
-
dim?: boolean;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Formats Qwen's internal reasoning into a uniform, subdued, all-italic monologue.
|
|
14
|
-
* Strips distracting markdown formatting and keeps every thought in a homogeneous opaque tone.
|
|
15
|
-
*/
|
|
16
|
-
export function formatReasoning(content: string, maxWidth: number): string[] {
|
|
17
|
-
const rawLines = content.split("\n");
|
|
18
|
-
const result: string[] = [];
|
|
19
|
-
const dimItalic = (s: string) => `\x1b[3m\x1b[38;2;120;125;145m${s}\x1b[39m\x1b[23m`;
|
|
20
|
-
|
|
21
|
-
for (const raw of rawLines) {
|
|
22
|
-
const trimmed = raw.trimEnd();
|
|
23
|
-
if (!trimmed) {
|
|
24
|
-
result.push("");
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
// Strip markdown asterisks and backticks so thoughts don't have clashing colors
|
|
28
|
-
const cleanText = trimmed
|
|
29
|
-
.replace(/(\*\*|__)(.*?)\1/g, "$2")
|
|
30
|
-
.replace(/`([^`]+)`/g, "$1")
|
|
31
|
-
.replace(/^#+\s*/, "");
|
|
32
|
-
|
|
33
|
-
const wrapped = wrapAnsiLine(cleanText, maxWidth);
|
|
34
|
-
for (const w of wrapped) {
|
|
35
|
-
result.push(dimItalic(w));
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Renders an elegant OSC 8 interactive card for images generated by Qwen.
|
|
44
|
-
*/
|
|
45
|
-
export function formatImageCard(alt: string, url: string, maxWidth: number): string[] {
|
|
46
|
-
const cardW = Math.max(30, Math.min(maxWidth, 70));
|
|
47
|
-
const cleanAlt =
|
|
48
|
-
alt && alt !== "Generated image" && alt !== "image"
|
|
49
|
-
? ` ${alt.trim()} `
|
|
50
|
-
: " Imagem Gerada ";
|
|
51
|
-
const innerW = cardW - 2;
|
|
52
|
-
const linkLabel = `[ 🔗 Clique para abrir a imagem no navegador ]`;
|
|
53
|
-
const osc8 = `\x1b]8;;${url}\x1b\\${theme.cyan(theme.underline(linkLabel))}\x1b]8;;\x1b\\`;
|
|
54
|
-
const labelVisualW = stringWidth(linkLabel);
|
|
55
|
-
const paddingRight = " ".repeat(Math.max(0, innerW - labelVisualW - 2));
|
|
56
|
-
|
|
57
|
-
const titleW = stringWidth(cleanAlt);
|
|
58
|
-
const topHr = "─".repeat(Math.max(2, innerW - titleW - 5));
|
|
59
|
-
const bottomHr = "─".repeat(innerW);
|
|
60
|
-
|
|
61
|
-
return [
|
|
62
|
-
` ${theme.borderActive(`╭─ 🖼️ ${cleanAlt}${topHr}╮`)}`,
|
|
63
|
-
` ${theme.borderActive("│")} ${osc8}${paddingRight}${theme.borderActive("│")}`,
|
|
64
|
-
` ${theme.borderActive(`╰${bottomHr}╯`)}`,
|
|
65
|
-
];
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Formats inline Markdown elements (bold, italic, code, strikethrough).
|
|
69
|
-
*/
|
|
70
|
-
export function formatMarkdownInline(text: string, options?: MarkdownOptions): string {
|
|
71
|
-
const dim = options?.dim ?? false;
|
|
72
|
-
let res = text;
|
|
73
|
-
|
|
74
|
-
if (dim) {
|
|
75
|
-
const muted = "\x1b[38;2;120;125;145m";
|
|
76
|
-
const reset = "\x1b[39m";
|
|
77
|
-
// Uniform dimmed italic mode
|
|
78
|
-
res = res.replace(/(\*\*|__)(.*?)\1/g, (_m, _d, content) => `\x1b[3m${content}\x1b[23m`);
|
|
79
|
-
res = res.replace(/(?<!\w)(\*|_)(.*?)\1(?!\w)/g, (_m, _d, content) => `\x1b[3m${content}\x1b[23m`);
|
|
80
|
-
res = res.replace(/`([^`]+)`/g, (_m, content) => `\x1b[3m${content}\x1b[23m`);
|
|
81
|
-
res = res.replace(/~~(.*?)~~/g, (_m, content) => `\x1b[9m${content}\x1b[29m`);
|
|
82
|
-
return `\x1b[3m${muted}${res}${reset}\x1b[23m`;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Standard vibrant TrueColor mode (for assistant final responses)
|
|
86
|
-
res = res.replace(/(\*\*|__)(.*?)\1/g, (_m, _d, content) => theme.bold(theme.white(content)));
|
|
87
|
-
res = res.replace(/(?<!\w)(\*|_)(.*?)\1(?!\w)/g, (_m, _d, content) =>
|
|
88
|
-
theme.italic(theme.lavender(content)),
|
|
89
|
-
);
|
|
90
|
-
res = res.replace(/`([^`]+)`/g, (_m, content) => theme.cyan(content));
|
|
91
|
-
res = res.replace(/~~(.*?)~~/g, (_m, content) => `\x1b[9m${content}\x1b[29m`);
|
|
92
|
-
|
|
93
|
-
return res;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Word-wraps a single ANSI-styled string into multiple lines without breaking ANSI sequences.
|
|
98
|
-
*/
|
|
99
|
-
export function wrapAnsiLine(line: string, maxWidth: number): string[] {
|
|
100
|
-
if (maxWidth <= 0) return [line];
|
|
101
|
-
if (stringWidth(line) <= maxWidth) return [line];
|
|
102
|
-
|
|
103
|
-
const words = line.split(" ");
|
|
104
|
-
const lines: string[] = [];
|
|
105
|
-
let currentLine = "";
|
|
106
|
-
|
|
107
|
-
for (const word of words) {
|
|
108
|
-
const testLine = currentLine ? `${currentLine} ${word}` : word;
|
|
109
|
-
if (stringWidth(testLine) <= maxWidth) {
|
|
110
|
-
currentLine = testLine;
|
|
111
|
-
} else {
|
|
112
|
-
if (currentLine) lines.push(currentLine);
|
|
113
|
-
currentLine = word;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (currentLine) lines.push(currentLine);
|
|
118
|
-
return lines.length > 0 ? lines : [""];
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Parses full multi-line Markdown text and returns an array of formatted, word-wrapped lines.
|
|
123
|
-
*/
|
|
124
|
-
export function formatMarkdown(
|
|
125
|
-
content: string,
|
|
126
|
-
maxWidth: number,
|
|
127
|
-
options?: MarkdownOptions,
|
|
128
|
-
): string[] {
|
|
129
|
-
const rawLines = content.split("\n");
|
|
130
|
-
const result: string[] = [];
|
|
131
|
-
let inCodeBlock = false;
|
|
132
|
-
const isDim = options?.dim ?? false;
|
|
133
|
-
|
|
134
|
-
for (const rawLine of rawLines) {
|
|
135
|
-
const trimmed = rawLine.trimEnd();
|
|
136
|
-
|
|
137
|
-
// Image Markdown: 
|
|
138
|
-
const imgRegex = /!\[(.*?)\]\((https?:\/\/[^\s)]+)\)/g;
|
|
139
|
-
if (imgRegex.test(trimmed)) {
|
|
140
|
-
const parts = trimmed.split(/(!\[.*?\]\(https?:\/\/[^\s)]+\))/g);
|
|
141
|
-
for (const part of parts) {
|
|
142
|
-
const m = part.match(/^!\[(.*?)\]\((https?:\/\/[^\s)]+)\)$/);
|
|
143
|
-
if (m) {
|
|
144
|
-
result.push(...formatImageCard(m[1], m[2], maxWidth));
|
|
145
|
-
} else if (part.trim()) {
|
|
146
|
-
const inlineFormatted = formatMarkdownInline(part.trim(), options);
|
|
147
|
-
result.push(...wrapAnsiLine(inlineFormatted, maxWidth));
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Code blocks: ```lang ... ```
|
|
154
|
-
if (trimmed.startsWith("```")) {
|
|
155
|
-
inCodeBlock = !inCodeBlock;
|
|
156
|
-
const lang = trimmed.slice(3).trim();
|
|
157
|
-
if (inCodeBlock) {
|
|
158
|
-
const border = isDim ? theme.dark : theme.blue;
|
|
159
|
-
result.push(border(`┌─── ${lang || "code"} ───────────────────────────`));
|
|
160
|
-
} else {
|
|
161
|
-
const border = isDim ? theme.dark : theme.blue;
|
|
162
|
-
result.push(border("└───"));
|
|
163
|
-
}
|
|
164
|
-
continue;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (inCodeBlock) {
|
|
168
|
-
if (isDim) {
|
|
169
|
-
result.push(theme.dark(`│ `) + theme.muted(trimmed));
|
|
170
|
-
} else {
|
|
171
|
-
result.push(theme.blue(`│ `) + theme.cyan(trimmed));
|
|
172
|
-
}
|
|
173
|
-
continue;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// Headers
|
|
177
|
-
if (/^#{1,3}\s+/.test(trimmed)) {
|
|
178
|
-
const level = trimmed.match(/^#+/)?.[0].length || 1;
|
|
179
|
-
const cleanHeader = trimmed.replace(/^#+\s*/, "");
|
|
180
|
-
const formattedHeader = formatMarkdownInline(cleanHeader, options);
|
|
181
|
-
if (isDim) {
|
|
182
|
-
result.push(theme.bold(theme.muted(`■ ${cleanHeader}`)));
|
|
183
|
-
} else {
|
|
184
|
-
if (level === 1) {
|
|
185
|
-
result.push(theme.bold(theme.lavender(`■ ${formattedHeader}`)));
|
|
186
|
-
} else if (level === 2) {
|
|
187
|
-
result.push(theme.bold(theme.blue(`● ${formattedHeader}`)));
|
|
188
|
-
} else {
|
|
189
|
-
result.push(theme.bold(theme.cyan(`▸ ${formattedHeader}`)));
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
continue;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// Blockquotes
|
|
196
|
-
if (trimmed.startsWith(">")) {
|
|
197
|
-
const quoteText = trimmed.replace(/^>\s*/, "");
|
|
198
|
-
const formattedQuote = formatMarkdownInline(quoteText, options);
|
|
199
|
-
const wrapped = wrapAnsiLine(formattedQuote, Math.max(10, maxWidth - 4));
|
|
200
|
-
for (const wl of wrapped) {
|
|
201
|
-
result.push(theme.muted("│ ") + theme.italic(wl));
|
|
202
|
-
}
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// Bullet lists (- or * or •)
|
|
207
|
-
if (/^(\s*)[-*•]\s+/.test(trimmed)) {
|
|
208
|
-
const match = trimmed.match(/^(\s*)[-*•]\s+(.*)$/);
|
|
209
|
-
if (match) {
|
|
210
|
-
const indent = match[1] || "";
|
|
211
|
-
const itemText = formatMarkdownInline(match[2], options);
|
|
212
|
-
const wrapped = wrapAnsiLine(itemText, Math.max(10, maxWidth - indent.length - 4));
|
|
213
|
-
const bullet = isDim ? theme.dark("•") : theme.cyan("•");
|
|
214
|
-
wrapped.forEach((wl, idx) => {
|
|
215
|
-
if (idx === 0) {
|
|
216
|
-
result.push(`${indent} ${bullet} ${wl}`);
|
|
217
|
-
} else {
|
|
218
|
-
result.push(`${indent} ${wl}`);
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
continue;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// Numbered lists (1. 2.)
|
|
226
|
-
if (/^(\s*)\d+\.\s+/.test(trimmed)) {
|
|
227
|
-
const match = trimmed.match(/^(\s*)(\d+\.)\s+(.*)$/);
|
|
228
|
-
if (match) {
|
|
229
|
-
const indent = match[1] || "";
|
|
230
|
-
const num = match[2];
|
|
231
|
-
const itemText = formatMarkdownInline(match[3], options);
|
|
232
|
-
const wrapped = wrapAnsiLine(itemText, Math.max(10, maxWidth - indent.length - num.length - 3));
|
|
233
|
-
const numStyled = isDim ? theme.muted(num) : theme.blue(num);
|
|
234
|
-
wrapped.forEach((wl, idx) => {
|
|
235
|
-
if (idx === 0) {
|
|
236
|
-
result.push(`${indent} ${numStyled} ${wl}`);
|
|
237
|
-
} else {
|
|
238
|
-
result.push(`${indent} ${wl}`);
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
continue;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Empty lines
|
|
246
|
-
if (!trimmed) {
|
|
247
|
-
result.push("");
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Standard paragraph with inline formatting and word-wrap
|
|
252
|
-
const inlineFormatted = formatMarkdownInline(trimmed, options);
|
|
253
|
-
const wrapped = wrapAnsiLine(inlineFormatted, maxWidth);
|
|
254
|
-
result.push(...wrapped);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
return result;
|
|
258
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* QwenProxy TUI - Lightweight Terminal Markdown Formatter
|
|
3
|
+
* Formats Markdown bold, italic, code blocks, bullet lists, and headers into ANSI styles
|
|
4
|
+
* with word-wrapping that respects terminal column constraints.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { theme, stringWidth } from "./theme.ts";
|
|
8
|
+
export interface MarkdownOptions {
|
|
9
|
+
dim?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Formats Qwen's internal reasoning into a uniform, subdued, all-italic monologue.
|
|
14
|
+
* Strips distracting markdown formatting and keeps every thought in a homogeneous opaque tone.
|
|
15
|
+
*/
|
|
16
|
+
export function formatReasoning(content: string, maxWidth: number): string[] {
|
|
17
|
+
const rawLines = content.split("\n");
|
|
18
|
+
const result: string[] = [];
|
|
19
|
+
const dimItalic = (s: string) => `\x1b[3m\x1b[38;2;120;125;145m${s}\x1b[39m\x1b[23m`;
|
|
20
|
+
|
|
21
|
+
for (const raw of rawLines) {
|
|
22
|
+
const trimmed = raw.trimEnd();
|
|
23
|
+
if (!trimmed) {
|
|
24
|
+
result.push("");
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
// Strip markdown asterisks and backticks so thoughts don't have clashing colors
|
|
28
|
+
const cleanText = trimmed
|
|
29
|
+
.replace(/(\*\*|__)(.*?)\1/g, "$2")
|
|
30
|
+
.replace(/`([^`]+)`/g, "$1")
|
|
31
|
+
.replace(/^#+\s*/, "");
|
|
32
|
+
|
|
33
|
+
const wrapped = wrapAnsiLine(cleanText, maxWidth);
|
|
34
|
+
for (const w of wrapped) {
|
|
35
|
+
result.push(dimItalic(w));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Renders an elegant OSC 8 interactive card for images generated by Qwen.
|
|
44
|
+
*/
|
|
45
|
+
export function formatImageCard(alt: string, url: string, maxWidth: number): string[] {
|
|
46
|
+
const cardW = Math.max(30, Math.min(maxWidth, 70));
|
|
47
|
+
const cleanAlt =
|
|
48
|
+
alt && alt !== "Generated image" && alt !== "image"
|
|
49
|
+
? ` ${alt.trim()} `
|
|
50
|
+
: " Imagem Gerada ";
|
|
51
|
+
const innerW = cardW - 2;
|
|
52
|
+
const linkLabel = `[ 🔗 Clique para abrir a imagem no navegador ]`;
|
|
53
|
+
const osc8 = `\x1b]8;;${url}\x1b\\${theme.cyan(theme.underline(linkLabel))}\x1b]8;;\x1b\\`;
|
|
54
|
+
const labelVisualW = stringWidth(linkLabel);
|
|
55
|
+
const paddingRight = " ".repeat(Math.max(0, innerW - labelVisualW - 2));
|
|
56
|
+
|
|
57
|
+
const titleW = stringWidth(cleanAlt);
|
|
58
|
+
const topHr = "─".repeat(Math.max(2, innerW - titleW - 5));
|
|
59
|
+
const bottomHr = "─".repeat(innerW);
|
|
60
|
+
|
|
61
|
+
return [
|
|
62
|
+
` ${theme.borderActive(`╭─ 🖼️ ${cleanAlt}${topHr}╮`)}`,
|
|
63
|
+
` ${theme.borderActive("│")} ${osc8}${paddingRight}${theme.borderActive("│")}`,
|
|
64
|
+
` ${theme.borderActive(`╰${bottomHr}╯`)}`,
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Formats inline Markdown elements (bold, italic, code, strikethrough).
|
|
69
|
+
*/
|
|
70
|
+
export function formatMarkdownInline(text: string, options?: MarkdownOptions): string {
|
|
71
|
+
const dim = options?.dim ?? false;
|
|
72
|
+
let res = text;
|
|
73
|
+
|
|
74
|
+
if (dim) {
|
|
75
|
+
const muted = "\x1b[38;2;120;125;145m";
|
|
76
|
+
const reset = "\x1b[39m";
|
|
77
|
+
// Uniform dimmed italic mode
|
|
78
|
+
res = res.replace(/(\*\*|__)(.*?)\1/g, (_m, _d, content) => `\x1b[3m${content}\x1b[23m`);
|
|
79
|
+
res = res.replace(/(?<!\w)(\*|_)(.*?)\1(?!\w)/g, (_m, _d, content) => `\x1b[3m${content}\x1b[23m`);
|
|
80
|
+
res = res.replace(/`([^`]+)`/g, (_m, content) => `\x1b[3m${content}\x1b[23m`);
|
|
81
|
+
res = res.replace(/~~(.*?)~~/g, (_m, content) => `\x1b[9m${content}\x1b[29m`);
|
|
82
|
+
return `\x1b[3m${muted}${res}${reset}\x1b[23m`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Standard vibrant TrueColor mode (for assistant final responses)
|
|
86
|
+
res = res.replace(/(\*\*|__)(.*?)\1/g, (_m, _d, content) => theme.bold(theme.white(content)));
|
|
87
|
+
res = res.replace(/(?<!\w)(\*|_)(.*?)\1(?!\w)/g, (_m, _d, content) =>
|
|
88
|
+
theme.italic(theme.lavender(content)),
|
|
89
|
+
);
|
|
90
|
+
res = res.replace(/`([^`]+)`/g, (_m, content) => theme.cyan(content));
|
|
91
|
+
res = res.replace(/~~(.*?)~~/g, (_m, content) => `\x1b[9m${content}\x1b[29m`);
|
|
92
|
+
|
|
93
|
+
return res;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Word-wraps a single ANSI-styled string into multiple lines without breaking ANSI sequences.
|
|
98
|
+
*/
|
|
99
|
+
export function wrapAnsiLine(line: string, maxWidth: number): string[] {
|
|
100
|
+
if (maxWidth <= 0) return [line];
|
|
101
|
+
if (stringWidth(line) <= maxWidth) return [line];
|
|
102
|
+
|
|
103
|
+
const words = line.split(" ");
|
|
104
|
+
const lines: string[] = [];
|
|
105
|
+
let currentLine = "";
|
|
106
|
+
|
|
107
|
+
for (const word of words) {
|
|
108
|
+
const testLine = currentLine ? `${currentLine} ${word}` : word;
|
|
109
|
+
if (stringWidth(testLine) <= maxWidth) {
|
|
110
|
+
currentLine = testLine;
|
|
111
|
+
} else {
|
|
112
|
+
if (currentLine) lines.push(currentLine);
|
|
113
|
+
currentLine = word;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (currentLine) lines.push(currentLine);
|
|
118
|
+
return lines.length > 0 ? lines : [""];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Parses full multi-line Markdown text and returns an array of formatted, word-wrapped lines.
|
|
123
|
+
*/
|
|
124
|
+
export function formatMarkdown(
|
|
125
|
+
content: string,
|
|
126
|
+
maxWidth: number,
|
|
127
|
+
options?: MarkdownOptions,
|
|
128
|
+
): string[] {
|
|
129
|
+
const rawLines = content.split("\n");
|
|
130
|
+
const result: string[] = [];
|
|
131
|
+
let inCodeBlock = false;
|
|
132
|
+
const isDim = options?.dim ?? false;
|
|
133
|
+
|
|
134
|
+
for (const rawLine of rawLines) {
|
|
135
|
+
const trimmed = rawLine.trimEnd();
|
|
136
|
+
|
|
137
|
+
// Image Markdown: 
|
|
138
|
+
const imgRegex = /!\[(.*?)\]\((https?:\/\/[^\s)]+)\)/g;
|
|
139
|
+
if (imgRegex.test(trimmed)) {
|
|
140
|
+
const parts = trimmed.split(/(!\[.*?\]\(https?:\/\/[^\s)]+\))/g);
|
|
141
|
+
for (const part of parts) {
|
|
142
|
+
const m = part.match(/^!\[(.*?)\]\((https?:\/\/[^\s)]+)\)$/);
|
|
143
|
+
if (m) {
|
|
144
|
+
result.push(...formatImageCard(m[1], m[2], maxWidth));
|
|
145
|
+
} else if (part.trim()) {
|
|
146
|
+
const inlineFormatted = formatMarkdownInline(part.trim(), options);
|
|
147
|
+
result.push(...wrapAnsiLine(inlineFormatted, maxWidth));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Code blocks: ```lang ... ```
|
|
154
|
+
if (trimmed.startsWith("```")) {
|
|
155
|
+
inCodeBlock = !inCodeBlock;
|
|
156
|
+
const lang = trimmed.slice(3).trim();
|
|
157
|
+
if (inCodeBlock) {
|
|
158
|
+
const border = isDim ? theme.dark : theme.blue;
|
|
159
|
+
result.push(border(`┌─── ${lang || "code"} ───────────────────────────`));
|
|
160
|
+
} else {
|
|
161
|
+
const border = isDim ? theme.dark : theme.blue;
|
|
162
|
+
result.push(border("└───"));
|
|
163
|
+
}
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (inCodeBlock) {
|
|
168
|
+
if (isDim) {
|
|
169
|
+
result.push(theme.dark(`│ `) + theme.muted(trimmed));
|
|
170
|
+
} else {
|
|
171
|
+
result.push(theme.blue(`│ `) + theme.cyan(trimmed));
|
|
172
|
+
}
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Headers
|
|
177
|
+
if (/^#{1,3}\s+/.test(trimmed)) {
|
|
178
|
+
const level = trimmed.match(/^#+/)?.[0].length || 1;
|
|
179
|
+
const cleanHeader = trimmed.replace(/^#+\s*/, "");
|
|
180
|
+
const formattedHeader = formatMarkdownInline(cleanHeader, options);
|
|
181
|
+
if (isDim) {
|
|
182
|
+
result.push(theme.bold(theme.muted(`■ ${cleanHeader}`)));
|
|
183
|
+
} else {
|
|
184
|
+
if (level === 1) {
|
|
185
|
+
result.push(theme.bold(theme.lavender(`■ ${formattedHeader}`)));
|
|
186
|
+
} else if (level === 2) {
|
|
187
|
+
result.push(theme.bold(theme.blue(`● ${formattedHeader}`)));
|
|
188
|
+
} else {
|
|
189
|
+
result.push(theme.bold(theme.cyan(`▸ ${formattedHeader}`)));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Blockquotes
|
|
196
|
+
if (trimmed.startsWith(">")) {
|
|
197
|
+
const quoteText = trimmed.replace(/^>\s*/, "");
|
|
198
|
+
const formattedQuote = formatMarkdownInline(quoteText, options);
|
|
199
|
+
const wrapped = wrapAnsiLine(formattedQuote, Math.max(10, maxWidth - 4));
|
|
200
|
+
for (const wl of wrapped) {
|
|
201
|
+
result.push(theme.muted("│ ") + theme.italic(wl));
|
|
202
|
+
}
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Bullet lists (- or * or •)
|
|
207
|
+
if (/^(\s*)[-*•]\s+/.test(trimmed)) {
|
|
208
|
+
const match = trimmed.match(/^(\s*)[-*•]\s+(.*)$/);
|
|
209
|
+
if (match) {
|
|
210
|
+
const indent = match[1] || "";
|
|
211
|
+
const itemText = formatMarkdownInline(match[2], options);
|
|
212
|
+
const wrapped = wrapAnsiLine(itemText, Math.max(10, maxWidth - indent.length - 4));
|
|
213
|
+
const bullet = isDim ? theme.dark("•") : theme.cyan("•");
|
|
214
|
+
wrapped.forEach((wl, idx) => {
|
|
215
|
+
if (idx === 0) {
|
|
216
|
+
result.push(`${indent} ${bullet} ${wl}`);
|
|
217
|
+
} else {
|
|
218
|
+
result.push(`${indent} ${wl}`);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Numbered lists (1. 2.)
|
|
226
|
+
if (/^(\s*)\d+\.\s+/.test(trimmed)) {
|
|
227
|
+
const match = trimmed.match(/^(\s*)(\d+\.)\s+(.*)$/);
|
|
228
|
+
if (match) {
|
|
229
|
+
const indent = match[1] || "";
|
|
230
|
+
const num = match[2];
|
|
231
|
+
const itemText = formatMarkdownInline(match[3], options);
|
|
232
|
+
const wrapped = wrapAnsiLine(itemText, Math.max(10, maxWidth - indent.length - num.length - 3));
|
|
233
|
+
const numStyled = isDim ? theme.muted(num) : theme.blue(num);
|
|
234
|
+
wrapped.forEach((wl, idx) => {
|
|
235
|
+
if (idx === 0) {
|
|
236
|
+
result.push(`${indent} ${numStyled} ${wl}`);
|
|
237
|
+
} else {
|
|
238
|
+
result.push(`${indent} ${wl}`);
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Empty lines
|
|
246
|
+
if (!trimmed) {
|
|
247
|
+
result.push("");
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Standard paragraph with inline formatting and word-wrap
|
|
252
|
+
const inlineFormatted = formatMarkdownInline(trimmed, options);
|
|
253
|
+
const wrapped = wrapAnsiLine(inlineFormatted, maxWidth);
|
|
254
|
+
result.push(...wrapped);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return result;
|
|
258
|
+
}
|