mocode-ai 1.8.7 → 1.8.9

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.
@@ -29,10 +29,10 @@ import { composePlanLines, drawStatusBar, startTurnTimer, stopTurnTimer } from '
29
29
  import { paintInput, repaint } from './input-paint.js';
30
30
  import { WHEEL_LINES, screenRowToAbsLine, repaintViewport, scrollBy, scrollWheel, resetScroll, lockScrollToBottom, } from './scroll.js';
31
31
  import { contentWrite, contentInsertAfter, contentDeleteFrom, contentReplaceLine, reflowContentForResize, flushMdSegment, } from './content-write.js';
32
- export { isTuiActive, getGeo, setRegion, contentMode, isStreamingPaused } from './screen.js';
32
+ export { isTuiActive, getGeo, getEffectiveContentBottom, setRegion, contentMode, isStreamingPaused } from './screen.js';
33
33
  export { drawStatusBar, setStatus, stopTurnTimer, startTurnTimerIfRunning, paintLiveAtCursor, clearLiveAtCursor, setLiveUsage, setQueuedTurn, setStatusBase, } from './statusbar.js';
34
34
  export { paintInput, paintRunningInput, repaint } from './input-paint.js';
35
- export { isScrolled, repaintViewport, repaintContentLine, scrollBy, scrollWheel, resetScroll, lockScrollToBottom, unlockScroll, isScrollLocked, } from './scroll.js';
35
+ export { isScrolled, repaintViewport, repaintContentLine, scrollBy, scrollWheel, resetScroll, setMenuReserve, lockScrollToBottom, unlockScroll, isScrollLocked, } from './scroll.js';
36
36
  export { contentWrite, writeBanner, rewriteBanner, bannerHeight, contentWriteMd, contentWriteMdOnce, clearContent, rewindContent, writeWelcomeBlock, dismissWelcomeBlock, contentInsertAfter, contentDeleteFrom, totalRows, isLastContentRowBlank, normalizeMutationBoundary, normalizeInputBoundary, contentReplaceLine, notifyContentReset, } from './content-write.js';
37
37
  // ── 裸 console 防御 ──
38
38
  // 第三方库(如 openai SDK)可能用 console.log 直写 stdout,在 RUNNING 态会落到光标所在的底栏
@@ -565,6 +565,12 @@ export function enterAltScreen() {
565
565
  const colsChanged = latest.cols !== state.lastTerminalCols;
566
566
  reflowContentForResize(latest.cols, colsChanged);
567
567
  state.lastTerminalCols = latest.cols;
568
+ // 介入面板开着时菜单预留行可能因新窗口高度而越界:重新钳到 contentBottom-1。
569
+ // 菜单本身的重排由 intervention 的 resize 监听负责(它会按新几何重算 menuLines 并再设一次预留);
570
+ // 这里只保证视口几何不留错值,避免正文重排到菜单带里。
571
+ if (state.menuReserveRows > 0) {
572
+ state.menuReserveRows = Math.min(state.menuReserveRows, latest.contentBottom - 1);
573
+ }
568
574
  repaintViewport(); // 内容区按最新高度和列宽重画;markdown 历史已按新列宽 reflow
569
575
  repaint(); // 底栏重画
570
576
  }, 100);
@@ -5,7 +5,7 @@ import { charWidth, displayWidth, truncateDisplay, truncateDisplayHead, wrapByDi
5
5
  import { ui } from '../theme.js';
6
6
  import * as content from '../content.js';
7
7
  import { state } from './state.js';
8
- import { esc, cup, getGeo, setRegion, runningCaretPos } from './screen.js';
8
+ import { esc, cup, getGeo, getEffectiveContentBottom, setRegion, runningCaretPos } from './screen.js';
9
9
  import { SEL_OPEN, SEL_OFF, normalizeInputSelection, inputViewSig } from './selection.js';
10
10
  import { composeSpinnerLine, composeModelLine, composePlanLines, drawStatusBar } from './statusbar.js';
11
11
  // ── 输入区(底栏输入框 + 向上菜单)──
@@ -147,11 +147,14 @@ export function paintInput(view) {
147
147
  }
148
148
  g = setRegion(needFooterH);
149
149
  }
150
- // 2b. 重画内容末行(底栏上一行)从缓冲:防 WT 边距漏影(状态行重复到该行),并保证该行显正确内容
150
+ // 2b. 重画内容末行(有效视口底行)从缓冲:防 WT 边距漏影(状态行重复到该行),并保证该行显正确内容。
151
+ // 介入面板预留菜单行时,末行是菜单上沿那一行(getEffectiveContentBottom),不是物理 contentBottom——
152
+ // 后者属于菜单预留带,由第 5 步画菜单,这里不能碰,否则会在选项之间漏出正文。
151
153
  {
152
- const slice = content.sliceFromEnd(state.scrollOffset, g.contentBottom);
153
- const line = slice[g.contentBottom - 1] ?? '';
154
- buf += cup(g.contentBottom, 1) + esc.clearLine + line;
154
+ const effBottom = getEffectiveContentBottom();
155
+ const slice = content.sliceFromEnd(state.scrollOffset, effBottom);
156
+ const line = slice[effBottom - 1] ?? '';
157
+ buf += cup(effBottom, 1) + esc.clearLine + line;
155
158
  }
156
159
  // 2c. 虚拟空行(内容区与状态栏之间的视觉间隔,属底栏非内容):
157
160
  // - 无活跃 plan:清空保留作分隔(原设计)
@@ -245,9 +248,14 @@ export function paintInput(view) {
245
248
  }
246
249
  // 4b. 下线(输入框底):满屏宽细线 ─(cyan),在 model 行上一行(model 行占屏底 rows)
247
250
  buf += cup(g.rows - 1, 1) + esc.clearLine + ui.accent + '─'.repeat(g.cols) + ui.reset;
248
- // 5. 向上菜单(画在内容区底,底栏正上方)
251
+ // 5. 向上菜单(画在内容区底,底栏正上方)。
252
+ // 介入面板经 setMenuReserve 预留了等宽的底部行,正文已重排到菜单上沿之上,故菜单画进预留带、
253
+ // 不再覆盖任何正文;无预留(命令补全等普通 picker)时退回旧的 overlay 行为,直接贴内容区底。
249
254
  if (view.menu && view.menu.lines.length > 0) {
250
- const menuRows = Math.min(view.menu.lines.length, g.contentBottom);
255
+ const reserve = state.menuReserveRows > 0;
256
+ const menuRows = reserve
257
+ ? Math.min(view.menu.lines.length, state.menuReserveRows)
258
+ : Math.min(view.menu.lines.length, g.contentBottom);
251
259
  const menuStart = g.contentBottom - menuRows + 1;
252
260
  for (let i = 0; i < menuRows; i++) {
253
261
  buf += cup(menuStart + i, 1) + esc.clearLine + view.menu.lines[i];
@@ -66,6 +66,15 @@ export function getGeo(fh = state.footerH) {
66
66
  contentBottom: Math.max(1, rows - f),
67
67
  };
68
68
  }
69
+ /**
70
+ * 正文当前真正可用的**内容区底行**:有预留菜单(介入面板)时为 contentBottom - menuReserveRows。
71
+ * viewport 切片、滚动钳位、续写末行重画都用它,保证正文只画到菜单上沿、不与菜单重叠。
72
+ * 无菜单预留时与 getGeo().contentBottom 相同。
73
+ */
74
+ export function getEffectiveContentBottom() {
75
+ const g = getGeo();
76
+ return Math.max(1, g.contentBottom - state.menuReserveRows);
77
+ }
69
78
  /** (重)设滚动区域 [1, rows-footerH];设完 \x1B[H 归位(conhost 设 DECSTBM 时光标留旧位置行为未定义)。 */
70
79
  export function setRegion(fh) {
71
80
  const rows = stdout.rows || 24;
@@ -5,7 +5,7 @@ import { displayWidth, truncateDisplayHead, truncateAnsi, ansiDisplayWidth, padE
5
5
  import { ui } from '../theme.js';
6
6
  import * as content from '../content.js';
7
7
  import { state } from './state.js';
8
- import { esc, cup, getGeo, runningCaretPos } from './screen.js';
8
+ import { esc, cup, getGeo, getEffectiveContentBottom, runningCaretPos } from './screen.js';
9
9
  import { normalizeSelection, highlightRange } from './selection.js';
10
10
  import { repaint } from './input-paint.js';
11
11
  // ── 纯常量(可变运行态在 state.ts)──
@@ -20,10 +20,10 @@ export function isScrolled() {
20
20
  }
21
21
  /** viewport 窗口起点绝对行索引(0-based,对齐 content.sliceFromEnd 的 start)。 */
22
22
  export function viewportAbsStart() {
23
- const g = getGeo();
23
+ const h = getEffectiveContentBottom();
24
24
  const total = content.totalRows();
25
25
  const end = Math.max(0, total - state.scrollOffset);
26
- return Math.max(0, end - g.contentBottom);
26
+ return Math.max(0, end - h);
27
27
  }
28
28
  /** 屏行(1-based,内容区内)→ 绝对缓冲行索引(0-based)。 */
29
29
  export function screenRowToAbsLine(row) {
@@ -43,13 +43,13 @@ export function screenRowToAbsLine(row) {
43
43
  export function repaintContentLine(absIdx, line) {
44
44
  if (!state.active || state.overlayActive)
45
45
  return; // 覆盖层打开中不写屏(缓冲已由调用方更新)
46
- const g = getGeo();
46
+ const bottom = getEffectiveContentBottom();
47
47
  const row = absIdx - viewportAbsStart() + 1;
48
- if (row < 1 || row > g.contentBottom)
49
- return; // 不在视口内:不猜映射,交由上层处理
48
+ if (row < 1 || row > bottom)
49
+ return; // 不在有效视口内(含被预留菜单占住的行):不写
50
50
  // 已提交行自带完整 SGR 前缀(breakRow 落盘的是 `rowStartSgr + curRaw + reset`,见 content.ts),
51
51
  // 故单行重画无需补前缀,只需按当前列宽钳宽(窄窗兜底,防 auto-wrap 破坏「一行=一物理行」)。
52
- const text = truncateAnsi(line, g.cols);
52
+ const text = truncateAnsi(line, getGeo().cols);
53
53
  let out = cup(row, 1) + esc.clearLine + text;
54
54
  // 与 repaintViewport 同约定:运行态光标归输入框(供 IME 锚定),INPUT 态归续写位。
55
55
  if (state.mode === 'running') {
@@ -57,7 +57,7 @@ export function repaintContentLine(absIdx, line) {
57
57
  out += cup(c.row, c.col);
58
58
  }
59
59
  else {
60
- out += cup(state.scrollOffset === 0 ? state.contentRow : g.contentBottom, state.scrollOffset === 0 ? state.contentCol : 1);
60
+ out += cup(state.scrollOffset === 0 ? state.contentRow : bottom, state.scrollOffset === 0 ? state.contentCol : 1);
61
61
  }
62
62
  stdout.write(out);
63
63
  }
@@ -78,7 +78,9 @@ export function repaintViewport() {
78
78
  if (!state.active || state.overlayActive)
79
79
  return;
80
80
  const g = getGeo();
81
- const h = g.contentBottom;
81
+ // 介入面板预留菜单行时,正文只画到菜单上沿;菜单区(其下到 contentBottom)留给 paintInput,
82
+ // 这里绝不能 cup+clearLine 进去,否则会把菜单擦掉或在选项之间漏出正文。
83
+ const h = getEffectiveContentBottom();
82
84
  const slice = content.sliceFromEnd(state.scrollOffset, h);
83
85
  const sel = normalizeSelection();
84
86
  // viewportAbsStart() 无选区时也用:sticky banner 必须按真实窗口头算,不能降级 0(否则 banner 永不出现)。
@@ -140,15 +142,16 @@ export function repaintViewport() {
140
142
  }
141
143
  stdout.write(p);
142
144
  }
143
- /** 滚动 delta 行(正=往新、负=往旧);钳 [0, max(0, total-contentBottom)];变则重画 + 刷底栏(显指示、光标回输入框)。 */
145
+ /** 滚动 delta 行(正=往新、负=往旧);钳 [0, max(0, total-视口高)];变则重画 + 刷底栏(显指示、光标回输入框)。 */
144
146
  export function scrollBy(delta) {
145
147
  if (!state.active)
146
148
  return;
147
149
  if (Date.now() < state.scrollLockUntil)
148
150
  return; // 轮首滚动锁:吸收发消息前后 stdin 残留滚轮事件,保 agent 输出从底部开始
151
+ const h = getEffectiveContentBottom(); // 介入面板期按菜单上沿之上的压缩视口算滚动范围
149
152
  const g = getGeo();
150
153
  const total = content.totalRows();
151
- const maxOff = Math.max(0, total - g.contentBottom);
154
+ const maxOff = Math.max(0, total - h);
152
155
  const off = Math.max(0, Math.min(state.scrollOffset + delta, maxOff));
153
156
  if (off === state.scrollOffset)
154
157
  return;
@@ -188,6 +191,41 @@ export function lockScrollToBottom(ms = SCROLL_LOCK_MS) {
188
191
  export function unlockScroll() {
189
192
  state.scrollLockUntil = 0;
190
193
  }
194
+ /**
195
+ * 设置向上菜单对内容区**底部**的预留行数(介入面板专用)。
196
+ * - rows > 0:正文视口压缩为 contentBottom-rows 行,整体重排到菜单上沿之上,菜单不再盖住正文;
197
+ * - rows = 0:菜单关闭,恢复满内容区。
198
+ * 仅在数值变化时重画正文(↑↓ 导航若菜单高度不变无需重排),避免每帧整屏闪烁。
199
+ * 预留区里被腾出来的行(rows 变小/归零时新增可见的行)由本函数用清行 + 重画补齐,
200
+ * 调用方随后 paintInput 把菜单画在预留区内。
201
+ */
202
+ export function setMenuReserve(rows) {
203
+ if (!state.active)
204
+ return;
205
+ const g = getGeo();
206
+ const clamped = Math.max(0, Math.min(rows, g.contentBottom - 1));
207
+ if (clamped === state.menuReserveRows)
208
+ return;
209
+ const prev = state.menuReserveRows;
210
+ state.menuReserveRows = clamped;
211
+ // 预留变小(菜单缩短/关闭):旧菜单区里 [新上沿+1 .. 旧上沿] 这几行需要先清掉旧菜单残留,
212
+ // 再由 repaintViewport 用正文重画;repaintViewport 只画到新上沿,故这里先清再画。
213
+ if (clamped < prev) {
214
+ const newBottom = g.contentBottom - clamped;
215
+ const oldBottom = g.contentBottom - prev;
216
+ let p = '';
217
+ for (let r = newBottom + 1; r <= oldBottom; r++)
218
+ p += cup(r, 1) + esc.clearLine;
219
+ if (p)
220
+ stdout.write(p);
221
+ }
222
+ // 滚动偏移可能超过压缩视口的新上限:钳回,避免顶部出现空窗。
223
+ const h = g.contentBottom - clamped;
224
+ const maxOff = Math.max(0, content.totalRows() - h);
225
+ if (state.scrollOffset > maxOff)
226
+ state.scrollOffset = maxOff;
227
+ repaintViewport();
228
+ }
191
229
  /** 轮首滚动锁是否生效(测试用)。 */
192
230
  export function isScrollLocked() {
193
231
  return Date.now() < state.scrollLockUntil;
@@ -84,6 +84,14 @@ export const state = {
84
84
  lastView: null,
85
85
  lastMenuStartRow: 0, // 上次菜单起始屏行(供擦除)
86
86
  lastMenuRows: 0,
87
+ /**
88
+ * 当前向上菜单占用内容区**底部**的物理行数(paintInput 画菜单期间 >0)。
89
+ * 介入面板(ask_human / 权限)在 RUNNING 态弹出时,最关键的命令/diff/ask_human 行
90
+ * 恰好位于内容区底——若菜单直接盖上去,用户看不到决策依据。该值让视口有效高度
91
+ * 收缩为 contentBottom - menuReserveRows:正文重排到菜单上沿之上,二者不重叠;
92
+ * 面板内 PgUp/滚轮也只在压缩后的视口里滚动。菜单关闭复位 0,repaintViewport 全量恢复。
93
+ */
94
+ menuReserveRows: 0,
87
95
  /** 上次画过输入框的 lastView 签名(lines 数 + 每行 display_w 累加);变了说明 prompt.ts 改了文本 → 选区锚点失效,清掉。 */
88
96
  lastInputSig: '',
89
97
  pasteHandler: null,
@@ -1,7 +1,7 @@
1
1
  // L1 状态栏:两行式底栏(spinner 行 + model 行)的拼装与绘制、走时心跳。
2
2
  // 从 layout-internal/core.ts 按依赖分层拆出;可变运行态集中在 ./state.ts。
3
3
  import { stdout } from 'node:process';
4
- import { displayWidth, truncateDisplay, ansiDisplayWidth, fmtElapsed, stripAnsi } from '../render.js';
4
+ import { displayWidth, truncateDisplay, ansiDisplayWidth, fmtElapsed, fmtCompactTokens, stripAnsi } from '../render.js';
5
5
  import { ui } from '../theme.js';
6
6
  import * as content from '../content.js';
7
7
  import { t } from '../../i18n/index.js';
@@ -155,9 +155,9 @@ function formatTurnTokenChip(usage) {
155
155
  if (!usage || !usage.totalTokens)
156
156
  return '';
157
157
  const n = usage.totalTokens;
158
- const text = n < 1000 ? `${n}` : `${(n / 1000).toFixed(n >= 10000 ? 0 : 1)}k`;
158
+ const text = fmtCompactTokens(n);
159
159
  const cached = usage.cachedTokens ?? 0;
160
- const cacheTag = cached > 0 ? ` ↻ ${cached < 1000 ? cached : `${(cached / 1000).toFixed(cached >= 10000 ? 0 : 1)}k`}` : '';
160
+ const cacheTag = cached > 0 ? ` ↻ ${fmtCompactTokens(cached)}` : '';
161
161
  // chip 用 mid 灰(降优先级)— 模式仍是主色
162
162
  return `${ui.dim}${text} tokens${cacheTag}${ui.reset}`;
163
163
  }
@@ -165,7 +165,7 @@ function formatTurnTokenChip(usage) {
165
165
  * ↓completion,cache 命中带 ↻ 标记;缩写与轮末摘要 / 左侧 turn chip 同款。
166
166
  * 流式期为估算值,每步末尾 usage chunk 到达后换实测;dim 灰降优先级,不与进度条阈值警示抢色。 */
167
167
  function formatLiveUsageChip(u) {
168
- const fmt = (n) => (n < 1000 ? `${n}` : `${(n / 1000).toFixed(n >= 10000 ? 0 : 1)}k`);
168
+ const fmt = fmtCompactTokens;
169
169
  const cached = u.cachedTokens ?? 0;
170
170
  const billable = Math.max(0, u.promptTokens - cached);
171
171
  const cacheTag = cached > 0 ? ` ↻ ${fmt(cached)}` : '';
package/dist/ui/render.js CHANGED
@@ -194,6 +194,19 @@ export function fmtElapsed(ms) {
194
194
  const rm = m % 60;
195
195
  return `${h}h ${rm}m`;
196
196
  }
197
+ /**
198
+ * 紧凑 token 计数:用于底栏用量 chip / 轮末 token 行等信息密度高的位置。
199
+ * - < 1k:整数(512)
200
+ * - 1k–<10k:一位小数 k(8.3k);10k–<100k:整数 k(876k)
201
+ * - ≥ 100k:以 M 计,<1M 显一位小数(0.1M 起),≥1M 整数 M(1M、12M)
202
+ */
203
+ export function fmtCompactTokens(n) {
204
+ if (n < 1000)
205
+ return `${Math.round(n)}`;
206
+ if (n < 1_000_000)
207
+ return `${(n / 1000).toFixed(n >= 10000 ? 0 : 1)}k`;
208
+ return `${(n / 1_000_000).toFixed(n >= 10_000_000 ? 0 : 1)}M`;
209
+ }
197
210
  /** 去除 SGR 颜色转义(\x1B[…m),用于度量带色串的真实可见宽度。 */
198
211
  export function stripAnsi(s) {
199
212
  return s.replace(/\x1b\[[0-9;]*m/g, '');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mocode-ai",
3
- "version": "1.8.7",
3
+ "version": "1.8.9",
4
4
  "description": "终端编码 agent:LLM + tool-call 循环 + 流式输出(含思考)+ 27 个工具,接任意 OpenAI 兼容后端。",
5
5
  "type": "module",
6
6
  "bin": {
@@ -49,6 +49,9 @@
49
49
  "cli-highlight": "^2.1.11",
50
50
  "dotenv": "^16.0.0",
51
51
  "fast-glob": "^3.0.0",
52
+ "imapflow": "^2.9.1",
53
+ "mailparser": "^3.12.4",
54
+ "nodemailer": "^10.2.1",
52
55
  "openai": "^4.0.0",
53
56
  "playwright": "1.62.1",
54
57
  "playwright-core": "1.62.1",
@@ -57,7 +60,9 @@
57
60
  },
58
61
  "devDependencies": {
59
62
  "@mocode/protocol": "0.0.0",
63
+ "@types/mailparser": "^3.9.0",
60
64
  "@types/node": "^22.0.0",
65
+ "@types/nodemailer": "^8.0.2",
61
66
  "@types/ws": "8.5.13",
62
67
  "eslint": "^9.39.5",
63
68
  "fast-check": "3.23.1",