mocode-ai 1.8.4 → 1.8.6
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/dist/agent/index.js +15 -0
- package/dist/agent/model-turn.js +214 -25
- package/dist/agent/request-governor.js +128 -0
- package/dist/agent/stages/history-manager.js +37 -1
- package/dist/attachments/image.js +63 -27
- package/dist/config/index.js +10 -0
- package/dist/context/request-limit.js +143 -0
- package/dist/context/vision-window.js +341 -0
- package/dist/headless.js +7 -0
- package/dist/repl/message-format.js +93 -3
- package/dist/session/scheduler.js +20 -3
- package/dist/ui/markdown.js +123 -36
- package/dist/ui/render.js +110 -4
- package/package.json +1 -1
|
@@ -7,6 +7,7 @@ import { pruneStaleArtifacts, refreshArtifactFreshness } from '../context/artifa
|
|
|
7
7
|
import { pruneSuperseded } from '../context/relevance.js';
|
|
8
8
|
import { clearRetrievableResults } from '../context/clearing.js';
|
|
9
9
|
import { createAgeAwareEncodingState } from '../context/age-aware.js';
|
|
10
|
+
import { applyImageBudget } from '../context/vision-window.js';
|
|
10
11
|
function emptyPressure(report) {
|
|
11
12
|
return {
|
|
12
13
|
triggered: false,
|
|
@@ -27,10 +28,25 @@ export function createBudgetScheduler(state = contextState, runtime = defaultCom
|
|
|
27
28
|
async runStep(history, step, activeTools = chatTools, ephemeralTokens = 0, signal) {
|
|
28
29
|
// External file changes and mutations only update artifact metadata here.
|
|
29
30
|
refreshArtifactFreshness(state, history);
|
|
30
|
-
|
|
31
|
+
let report = evaluate(history, step, activeTools, ephemeralTokens);
|
|
32
|
+
const pressureLine = report.window * DEFAULT_BUDGET_POLICY.pressureTriggerRatio;
|
|
33
|
+
// 图片先行:压力线触发时先剥图(只留最新一张非屏幕帧图)再重估。图按像素计 token、又会随
|
|
34
|
+
// history 每步重发,但摘要只对旧区剥图、保留区的图原样留着 —— 为图压缩既压不掉图又白花一次
|
|
35
|
+
// LLM 调用。剥完低于线就根本不摘要;仍高于线说明文本本身超了,此时摘要才有意义。
|
|
36
|
+
// 屏幕帧(computer-)由 vision-window 管,这里不碰。
|
|
37
|
+
let imagesRelieved = 0;
|
|
38
|
+
if (Math.max(report.rawTotal, report.total) >= pressureLine) {
|
|
39
|
+
const relief = applyImageBudget(history, { maxChars: 1 });
|
|
40
|
+
if (relief.changed) {
|
|
41
|
+
history.splice(0, history.length, ...relief.messages);
|
|
42
|
+
imagesRelieved = relief.evicted;
|
|
43
|
+
report = evaluate(history, step, activeTools, ephemeralTokens);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
31
46
|
const pressure = emptyPressure(report);
|
|
47
|
+
pressure.imagesRelieved = imagesRelieved;
|
|
32
48
|
const occupancy = Math.max(report.rawTotal, report.total);
|
|
33
|
-
pressure.triggered = occupancy >=
|
|
49
|
+
pressure.triggered = occupancy >= pressureLine;
|
|
34
50
|
pressure.lowTriggered = !pressure.triggered && occupancy >= report.window * runtime.config.lowPressureRatio;
|
|
35
51
|
if (pressure.triggered) {
|
|
36
52
|
// A single 80% pressure event owns every history rewrite. Run all enabled
|
|
@@ -70,7 +86,8 @@ export function createBudgetScheduler(state = contextState, runtime = defaultCom
|
|
|
70
86
|
const actions = pressure.triggered ? scheduleActions(report) : [];
|
|
71
87
|
let compactHistoryCalled = false;
|
|
72
88
|
let historyRebuilt = false;
|
|
73
|
-
let contentMutated =
|
|
89
|
+
let contentMutated = imagesRelieved > 0 ||
|
|
90
|
+
pressure.superseded > 0 ||
|
|
74
91
|
pressure.staleArtifacts > 0 ||
|
|
75
92
|
pressure.encodedLogsAndSearches > 0 ||
|
|
76
93
|
pressure.cleared > 0;
|
package/dist/ui/markdown.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
// clipAnsiLine 兜底,绝不溢出。颜色一律嵌入式 ANSI(ui.*)写进字符串。
|
|
14
14
|
import { getCliHighlight } from './cli-highlight-lazy.js';
|
|
15
15
|
import { ui, getThemeVersion } from './theme.js';
|
|
16
|
-
import {
|
|
16
|
+
import { graphemeAt, displayWidth, ansiDisplayWidth, stripAnsi } from './render.js';
|
|
17
17
|
const RESET = '\x1b[0m';
|
|
18
18
|
// theme 无 italic/strike,自备(TTY 感知:!isTTY 退化为空串,同 ui.* 契约)。
|
|
19
19
|
const ITALIC = ui.isTTY ? '\x1b[3m' : '';
|
|
@@ -34,13 +34,16 @@ function clipAnsiLine(str, width) {
|
|
|
34
34
|
out += parts[i];
|
|
35
35
|
continue;
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
const seg = parts[i];
|
|
38
|
+
for (let gi = 0; gi < seg.length;) {
|
|
39
|
+
const g = graphemeAt(seg, gi);
|
|
40
|
+
const ch = seg.slice(gi, g.end);
|
|
41
|
+
gi = g.end;
|
|
42
|
+
if (g.width > 0 && w + g.width > width) {
|
|
40
43
|
return out + RESET;
|
|
41
44
|
}
|
|
42
|
-
if (
|
|
43
|
-
w +=
|
|
45
|
+
if (g.width > 0)
|
|
46
|
+
w += g.width;
|
|
44
47
|
out += ch;
|
|
45
48
|
}
|
|
46
49
|
}
|
|
@@ -262,8 +265,11 @@ function wrapAnsiString(str, width) {
|
|
|
262
265
|
activeSgr += tok;
|
|
263
266
|
continue;
|
|
264
267
|
}
|
|
265
|
-
for (
|
|
266
|
-
const
|
|
268
|
+
for (let gi = 0; gi < tok.length;) {
|
|
269
|
+
const g = graphemeAt(tok, gi);
|
|
270
|
+
const ch = tok.slice(gi, g.end);
|
|
271
|
+
gi = g.end;
|
|
272
|
+
const cw = g.width;
|
|
267
273
|
if (cw <= 0) {
|
|
268
274
|
cur += ch;
|
|
269
275
|
continue;
|
|
@@ -311,9 +317,41 @@ function parseTableRow(line) {
|
|
|
311
317
|
let s = line.trim();
|
|
312
318
|
if (s.startsWith('|'))
|
|
313
319
|
s = s.slice(1);
|
|
314
|
-
|
|
320
|
+
// 末尾 | 只有在未被转义时才是边框(`a \|` 结尾的 \| 是单元格内容)
|
|
321
|
+
if (s.endsWith('|') && !s.endsWith('\\|'))
|
|
315
322
|
s = s.slice(0, -1);
|
|
316
|
-
|
|
323
|
+
// 按「未转义、且不在行内代码里」的 | 切分;\| 还原成 |(GFM 规则:转义管道符在代码里也生效)
|
|
324
|
+
const cells = [];
|
|
325
|
+
let cur = '';
|
|
326
|
+
let ticks = 0; // 当前打开的行内代码反引号数(0 = 不在代码里)
|
|
327
|
+
for (let k = 0; k < s.length; k++) {
|
|
328
|
+
const ch = s[k];
|
|
329
|
+
if (ch === '\\' && s[k + 1] === '|') {
|
|
330
|
+
cur += '|';
|
|
331
|
+
k++;
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
if (ch === '`') {
|
|
335
|
+
let run = 1;
|
|
336
|
+
while (s[k + run] === '`')
|
|
337
|
+
run++;
|
|
338
|
+
if (ticks === 0)
|
|
339
|
+
ticks = run;
|
|
340
|
+
else if (ticks === run)
|
|
341
|
+
ticks = 0;
|
|
342
|
+
cur += '`'.repeat(run);
|
|
343
|
+
k += run - 1;
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
if (ch === '|' && ticks === 0) {
|
|
347
|
+
cells.push(cur.trim());
|
|
348
|
+
cur = '';
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
cur += ch;
|
|
352
|
+
}
|
|
353
|
+
cells.push(cur.trim());
|
|
354
|
+
return cells;
|
|
317
355
|
}
|
|
318
356
|
/** 分隔行每 cell :?-+:? → 对齐(left 默认)。 */
|
|
319
357
|
function parseAlign(sep) {
|
|
@@ -324,6 +362,33 @@ function parseAlign(sep) {
|
|
|
324
362
|
return l && r ? 'center' : r ? 'right' : 'left';
|
|
325
363
|
});
|
|
326
364
|
}
|
|
365
|
+
/**
|
|
366
|
+
* 宽容匹配 GFM 表格:src[i] 为表头、src[i+1] 为分隔行。
|
|
367
|
+
* 分隔行本身带 |、每格只含 `-` 与 `:`、至少一格含 `-` 即可(模型常写出列数与表头不一致的分隔行、
|
|
368
|
+
* 漏写竖线的粘连格 `--:----:`、单列表格);列数以表头为准,缺的列按左对齐。
|
|
369
|
+
* 不带 | 的 `---`(hr / setext)与 `~ | ==` 等非法写法返回 null,仍走普通文本。
|
|
370
|
+
* 数据行读到空行或不含 | 的行为止。next 为表格后第一行的下标。
|
|
371
|
+
*/
|
|
372
|
+
function matchTable(src, i) {
|
|
373
|
+
if (i + 1 >= src.length || !src[i].includes('|') || !src[i + 1].includes('|'))
|
|
374
|
+
return null;
|
|
375
|
+
const header = parseTableRow(src[i]);
|
|
376
|
+
const sepCells = parseTableRow(src[i + 1]);
|
|
377
|
+
if (header.length < 1)
|
|
378
|
+
return null;
|
|
379
|
+
if (!sepCells.some((c) => c.includes('-')) || !sepCells.every((c) => /^[:-]+$/.test(c)))
|
|
380
|
+
return null;
|
|
381
|
+
const rows = [];
|
|
382
|
+
let j = i + 2;
|
|
383
|
+
while (j < src.length) {
|
|
384
|
+
const r = src[j];
|
|
385
|
+
if (r.trim() === '' || !r.includes('|'))
|
|
386
|
+
break;
|
|
387
|
+
rows.push(parseTableRow(r));
|
|
388
|
+
j++;
|
|
389
|
+
}
|
|
390
|
+
return { header, aligns: parseAlign(sepCells), rows, next: j };
|
|
391
|
+
}
|
|
327
392
|
/** ANSI 感知按可见宽度 pad(左/右/中),补空格;不截断(超宽原样返回,由 clipAnsiLine 兜底)。 */
|
|
328
393
|
function padAnsi(str, width, align) {
|
|
329
394
|
const w = ansiDisplayWidth(str);
|
|
@@ -576,11 +641,33 @@ function renderMarkdownImpl(text, cols) {
|
|
|
576
641
|
flushPara();
|
|
577
642
|
const prefix = ` ${ui.dim}│${ui.reset} `;
|
|
578
643
|
const cont = ' '.repeat(ansiDisplayWidth(prefix));
|
|
579
|
-
|
|
580
|
-
|
|
644
|
+
// 收齐连续的引用行,去掉 > 前缀后再识别表格(否则引用块里的表格会逐行显示成竖线原文)
|
|
645
|
+
const inner = [];
|
|
646
|
+
let j = i;
|
|
647
|
+
while (j < src.length) {
|
|
648
|
+
const m = src[j].match(/^>\s?(.*)$/);
|
|
649
|
+
if (!m)
|
|
650
|
+
break;
|
|
651
|
+
inner.push(m[1]);
|
|
652
|
+
j++;
|
|
653
|
+
}
|
|
654
|
+
let k = 0;
|
|
655
|
+
while (k < inner.length) {
|
|
656
|
+
const t = matchTable(inner, k);
|
|
657
|
+
if (t) {
|
|
658
|
+
const tw = Math.max(1, cols - ansiDisplayWidth(prefix));
|
|
659
|
+
for (const l of renderTable(t.header, t.aligns, t.rows, tw))
|
|
660
|
+
out.push(clipAnsiLine(prefix + l, cols));
|
|
661
|
+
k = t.next;
|
|
662
|
+
continue;
|
|
663
|
+
}
|
|
664
|
+
for (const l of wrapWithPrefix(renderInline(inner[k]), prefix, cont, cols)) {
|
|
665
|
+
out.push(l);
|
|
666
|
+
}
|
|
667
|
+
k++;
|
|
581
668
|
}
|
|
582
669
|
prevBlank = false;
|
|
583
|
-
i
|
|
670
|
+
i = j;
|
|
584
671
|
continue;
|
|
585
672
|
}
|
|
586
673
|
// 无序列表 - * +
|
|
@@ -612,30 +699,30 @@ function renderMarkdownImpl(text, cols) {
|
|
|
612
699
|
i++;
|
|
613
700
|
continue;
|
|
614
701
|
}
|
|
615
|
-
// GFM 表格:表头
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
prevBlank = true;
|
|
636
|
-
continue;
|
|
637
|
-
}
|
|
702
|
+
// GFM 表格:表头 + 紧跟分隔行 → 收齐整块渲染(宽容匹配规则见 matchTable / renderTable)
|
|
703
|
+
const tbl = matchTable(src, i);
|
|
704
|
+
if (tbl) {
|
|
705
|
+
flushPara();
|
|
706
|
+
for (const l of renderTable(tbl.header, tbl.aligns, tbl.rows, cols))
|
|
707
|
+
out.push(l);
|
|
708
|
+
out.push('');
|
|
709
|
+
prevBlank = true;
|
|
710
|
+
i = tbl.next;
|
|
711
|
+
continue;
|
|
712
|
+
}
|
|
713
|
+
// 没有合法分隔行的竖线行(模型漏写分隔行 / 写法非法):保持逐行显示,
|
|
714
|
+
// 不能并进段落——段落会把多行 join 成一行,竖线文字糊成一团。
|
|
715
|
+
if (/^\s*\|/.test(line)) {
|
|
716
|
+
flushPara();
|
|
717
|
+
while (i < src.length && /^\s*\|/.test(src[i]) && !(i > 0 && matchTable(src, i))) {
|
|
718
|
+
const segs = renderInline(src[i].trim());
|
|
719
|
+
for (const l of wrapAnsiString(segsToAnsi(segs), cols))
|
|
720
|
+
out.push(clipAnsiLine(l, cols));
|
|
721
|
+
i++;
|
|
638
722
|
}
|
|
723
|
+
out.push('');
|
|
724
|
+
prevBlank = true;
|
|
725
|
+
continue;
|
|
639
726
|
}
|
|
640
727
|
// 段落:累积(连续非空非特殊行 join ' ' 后渲染)
|
|
641
728
|
para.push(line);
|
package/dist/ui/render.js
CHANGED
|
@@ -60,14 +60,120 @@ export function charWidth(cp) {
|
|
|
60
60
|
return 2; // 表情
|
|
61
61
|
if (cp >= 0x20000 && cp <= 0x2fffd)
|
|
62
62
|
return 2; // CJK 扩展 B-F
|
|
63
|
-
if (cp
|
|
64
|
-
return 2; // ⚡
|
|
63
|
+
if (isWideBmpEmoji(cp))
|
|
64
|
+
return 2; // ⚡ ✅ ❌ ⭐ 等:终端按 emoji 2 格渲染,算 1 会与 chip 重叠产生残影、表格列错位
|
|
65
65
|
return 1;
|
|
66
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* BMP 内 East Asian Width = W 的 emoji(Emoji_Presentation=Yes,无需 VS16 就按 2 格画)。
|
|
69
|
+
* ✔ (U+2714) / ⚠ (U+26A0) 等文本呈现符号默认是窄的,不在此表。
|
|
70
|
+
* 补充平面(U+1F300+)已由上面的区间覆盖。
|
|
71
|
+
*/
|
|
72
|
+
const WIDE_BMP_EMOJI = [
|
|
73
|
+
[0x231a, 0x231b],
|
|
74
|
+
[0x23e9, 0x23ec],
|
|
75
|
+
[0x23f0, 0x23f0],
|
|
76
|
+
[0x23f3, 0x23f3],
|
|
77
|
+
[0x25fd, 0x25fe],
|
|
78
|
+
[0x2614, 0x2615],
|
|
79
|
+
[0x2648, 0x2653],
|
|
80
|
+
[0x267f, 0x267f],
|
|
81
|
+
[0x2693, 0x2693],
|
|
82
|
+
[0x26a1, 0x26a1],
|
|
83
|
+
[0x26aa, 0x26ab],
|
|
84
|
+
[0x26bd, 0x26be],
|
|
85
|
+
[0x26c4, 0x26c5],
|
|
86
|
+
[0x26ce, 0x26ce],
|
|
87
|
+
[0x26d4, 0x26d4],
|
|
88
|
+
[0x26ea, 0x26ea],
|
|
89
|
+
[0x26f2, 0x26f3],
|
|
90
|
+
[0x26f5, 0x26f5],
|
|
91
|
+
[0x26fa, 0x26fa],
|
|
92
|
+
[0x26fd, 0x26fd],
|
|
93
|
+
[0x2705, 0x2705],
|
|
94
|
+
[0x270a, 0x270b],
|
|
95
|
+
[0x2728, 0x2728],
|
|
96
|
+
[0x274c, 0x274c],
|
|
97
|
+
[0x274e, 0x274e],
|
|
98
|
+
[0x2753, 0x2755],
|
|
99
|
+
[0x2757, 0x2757],
|
|
100
|
+
[0x2795, 0x2797],
|
|
101
|
+
[0x27b0, 0x27b0],
|
|
102
|
+
[0x27bf, 0x27bf],
|
|
103
|
+
[0x2b1b, 0x2b1c],
|
|
104
|
+
[0x2b50, 0x2b50],
|
|
105
|
+
[0x2b55, 0x2b55],
|
|
106
|
+
];
|
|
107
|
+
function isWideBmpEmoji(cp) {
|
|
108
|
+
if (cp < 0x231a || cp > 0x2b55)
|
|
109
|
+
return false;
|
|
110
|
+
for (const [lo, hi] of WIDE_BMP_EMOJI) {
|
|
111
|
+
if (cp >= lo && cp <= hi)
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 读 str 中 UTF-16 下标 i 处的一个「显示簇」,返回它占的终端列数与簇末下标(不含)。
|
|
118
|
+
* 终端会把下面几类码点序列合成一个字形,只画 emoji 的 2 格;逐码点累加宽度会算宽,表格边框就凹进去:
|
|
119
|
+
* - 区域旗帜:两个 Regional Indicator(🇨🇳)→ 2 格,而不是 2+2
|
|
120
|
+
* - ZWJ 序列:👨💻 / 👩👧 / ❤️🔥 → 以首个字符为准,ZWJ 后并入的 emoji 不再计宽
|
|
121
|
+
* - 肤色修饰符:👍🏽 → 并入前一字符
|
|
122
|
+
* - 后随 FE0F(VS16):⚠️ ✔️ ❤️ 默认文本呈现的窄符号切成 emoji 呈现 → 2 格;keycap(1️⃣)同为 2 格
|
|
123
|
+
* 组合符 / 控制符仍按 charWidth 算 0 宽。ZWJ 只与 U+2190 以上的符号 / emoji 合并,
|
|
124
|
+
* 避免把印地语等文字里的 ZWJ 误当 emoji 连接。
|
|
125
|
+
*/
|
|
126
|
+
export function graphemeAt(str, i) {
|
|
127
|
+
const cp = str.codePointAt(i) ?? 0;
|
|
128
|
+
let end = i + (cp > 0xffff ? 2 : 1);
|
|
129
|
+
if (cp >= 0x1f1e6 && cp <= 0x1f1ff) {
|
|
130
|
+
const next = str.codePointAt(end);
|
|
131
|
+
if (next !== undefined && next >= 0x1f1e6 && next <= 0x1f1ff)
|
|
132
|
+
end += 2;
|
|
133
|
+
return { width: 2, end };
|
|
134
|
+
}
|
|
135
|
+
let width = charWidth(cp);
|
|
136
|
+
for (;;) {
|
|
137
|
+
const n = str.codePointAt(end);
|
|
138
|
+
if (n === undefined)
|
|
139
|
+
break;
|
|
140
|
+
if (n === 0xfe0f) {
|
|
141
|
+
if (width === 1 && cp >= 0x80)
|
|
142
|
+
width = 2;
|
|
143
|
+
end += 1;
|
|
144
|
+
}
|
|
145
|
+
else if (n === 0xfe0e) {
|
|
146
|
+
end += 1;
|
|
147
|
+
}
|
|
148
|
+
else if (n === 0x20e3) {
|
|
149
|
+
if (width > 0)
|
|
150
|
+
width = 2;
|
|
151
|
+
end += 1;
|
|
152
|
+
}
|
|
153
|
+
else if (n >= 0x1f3fb && n <= 0x1f3ff && width > 0) {
|
|
154
|
+
end += 2;
|
|
155
|
+
}
|
|
156
|
+
else if (n === 0x200d) {
|
|
157
|
+
const m = str.codePointAt(end + 1);
|
|
158
|
+
if (m !== undefined && cp >= 0x2190 && m >= 0x2190)
|
|
159
|
+
end += 1 + (m > 0xffff ? 2 : 1);
|
|
160
|
+
else
|
|
161
|
+
end += 1;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return { width, end };
|
|
168
|
+
}
|
|
67
169
|
export function displayWidth(str) {
|
|
68
170
|
let w = 0;
|
|
69
|
-
|
|
70
|
-
|
|
171
|
+
let i = 0;
|
|
172
|
+
while (i < str.length) {
|
|
173
|
+
const g = graphemeAt(str, i);
|
|
174
|
+
w += g.width;
|
|
175
|
+
i = g.end;
|
|
176
|
+
}
|
|
71
177
|
return w;
|
|
72
178
|
}
|
|
73
179
|
/**
|